more type aliases

This commit is contained in:
AF 2023-05-25 13:46:56 +00:00
parent 258a2c8f80
commit a935ba68f5
24 changed files with 199 additions and 243 deletions

View File

@ -9,7 +9,7 @@ pub type KeyRc<'a, BT> = Rc<<BT as BinaryTrees<'a>>::Key>;
pub type Split<'a, BT> = (TreeRc<'a, BT>, TreeRc<'a, BT>, KeyRc<'a, BT>);
pub type Wrapped<'a, BT, A> = <<BT as BinaryTrees<'a>>::T as WeakFunctor>::F<'a, A>;
pub type Wrapped<'a, BT, A> = Wrap<'a, A, <BT as BinaryTrees<'a>>::T>;
pub trait BinaryTrees<'a>: 'a {
type Node: 'a;

View File

@ -58,7 +58,7 @@ pub trait TraversibleBinaryNode<'a, T: 'a + Monad, A: 'a, D: 'a + PartialEq>: 'a
}
pub trait TraversibleBinaryReference<'a, T: 'a + Monad, A: 'a, D: 'a + PartialEq>: 'a {
fn resolve(&self) -> <T as WeakFunctor>::F<'a, Rc<dyn TraversibleBinaryNode<'a, T, A, D>>>;
fn resolve(&self) -> Wrap<'a, Rc<dyn TraversibleBinaryNode<'a, T, A, D>>, T>;
/// This should be enough to compare reference for equality.
fn data(&self) -> D;

View File

@ -23,7 +23,7 @@ pub struct UnbalancedNode<'a, T: 'a + Monad, A: 'a> {
key: Rc<A>,
}
pub type UnbalancedResolution<'a, T, A> = <T as WeakFunctor>::F<'a, Rc<UnbalancedNode<'a, T, A>>>;
pub type UnbalancedResolution<'a, T, A> = Wrap<'a, Rc<UnbalancedNode<'a, T, A>>, T>;
pub struct UnbalancedReference<'a, T: 'a + Monad, A: 'a>(
Box<dyn 'a + Fn() -> UnbalancedResolution<'a, T, A>>,
@ -89,9 +89,7 @@ impl<'a, T: 'a + Monad, A: 'a> TraversibleBinaryNode<'a, T, A, UnbalancedData>
impl<'a, T: 'a + Monad, A: 'a> TraversibleBinaryReference<'a, T, A, UnbalancedData>
for UnbalancedReference<'a, T, A>
{
fn resolve(
&self,
) -> <T as WeakFunctor>::F<'a, Rc<dyn TraversibleBinaryNode<'a, T, A, UnbalancedData>>> {
fn resolve(&self) -> Wrap<'a, Rc<dyn TraversibleBinaryNode<'a, T, A, UnbalancedData>>, T> {
<T as Functor>::fmap(
|rc| rc as Rc<dyn TraversibleBinaryNode<'a, T, A, UnbalancedData>>,
self.0(),

View File

@ -39,6 +39,8 @@ pub trait WeakFunctor {
Self: 'a;
}
pub type Wrap<'a, A, T> = <T as WeakFunctor>::F<'a, A>;
/// Rust-specific implementation of [`Functor`], respecting `move` semantics.
///
/// Cannot insantiate for e.g. multi-element collections:
@ -276,23 +278,19 @@ pub trait MonadFailAny {
/// Associated infallible [`Monad`].
type T: Monad;
fn unstuff<'a, A: 'a, E: 'a>(
wa: <Self::W<E> as WeakFunctor>::F<'a, A>,
) -> <Self::T as WeakFunctor>::F<'a, Result<A, E>>
fn unstuff<'a, A: 'a, E: 'a>(wa: WrapE<'a, A, E, Self>) -> Wrap<'a, Result<A, E>, Self::T>
where
Self: 'a;
fn stuff<'a, A: 'a, E: 'a>(
fa: <Self::T as WeakFunctor>::F<'a, Result<A, E>>,
) -> <Self::W<E> as WeakFunctor>::F<'a, A>
fn stuff<'a, A: 'a, E: 'a>(fa: Wrap<'a, Result<A, E>, Self::T>) -> WrapE<'a, A, E, Self>
where
Self: 'a;
/// Equivalent of [`Result::map_err`].
fn map_err<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(E0) -> E1,
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
) -> WrapE<'a, A, E1, Self>
where
Self: 'a,
{
@ -301,9 +299,9 @@ pub trait MonadFailAny {
/// Equivalent of `catch`/`except`. To inject errors on success, see [`MonadFailAny::bind`].
fn bind_err<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
f: impl 'a + FnOnce(E0) -> <Self::W<E1> as WeakFunctor>::F<'a, A>,
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(E0) -> WrapE<'a, A, E1, Self>,
) -> WrapE<'a, A, E1, Self>
where
Self: 'a,
{
@ -319,9 +317,9 @@ pub trait MonadFailAny {
/// Note: Reasonably it is expected to lack fail semantics for the underlying result.
/// Therefore the default implementation descends into the non-fail monad [`MonadFailAny::T`].
fn bind<'a, A: 'a, B: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
f: impl 'a + FnOnce(Result<A, E0>) -> <Self::W<E1> as WeakFunctor>::F<'a, B>,
) -> <Self::W<E1> as WeakFunctor>::F<'a, B>
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(Result<A, E0>) -> WrapE<'a, B, E1, Self>,
) -> WrapE<'a, B, E1, Self>
where
Self: 'a,
{
@ -334,8 +332,8 @@ pub trait MonadFailAny {
///
/// Note: default implementation doesn't depend on [`Monad::join`].
fn join<'a, A: 'a, E0: 'a, E1: 'a>(
wwa: <Self::W<E1> as WeakFunctor>::F<'a, <Self::W<E0> as WeakFunctor>::F<'a, A>>,
) -> <Self::W<Result<E0, E1>> as WeakFunctor>::F<'a, A>
wwa: WrapE<'a, WrapE<'a, A, E0, Self>, E1, Self>,
) -> WrapE<'a, A, Result<E0, E1>, Self>
where
Self: 'a,
{
@ -347,8 +345,8 @@ pub trait MonadFailAny {
/// Lift the error.
fn rotate_out<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, Result<A, E1>>,
) -> <Self::W<Result<E1, E0>> as WeakFunctor>::F<'a, A>
wa: WrapE<'a, Result<A, E1>, E0, Self>,
) -> WrapE<'a, A, Result<E1, E0>, Self>
where
Self: 'a,
{
@ -359,15 +357,17 @@ pub trait MonadFailAny {
}
}
pub type WrapE<'a, A, E, Fallible> = Wrap<'a, A, <Fallible as MonadFailAny>::W<E>>;
pub trait MonadFailAnyExt: MonadFailAny {
fn pure<'a, E: 'a, A: 'a>(a: A) -> <Self::W<E> as WeakFunctor>::F<'a, A>
fn pure<'a, E: 'a, A: 'a>(a: A) -> WrapE<'a, A, E, Self>
where
Self: 'a,
{
<Self::W<E> as Pure>::pure(a)
}
fn fail<'a, E: 'a, A: 'a>(e: E) -> <Self::W<E> as WeakFunctor>::F<'a, A>
fn fail<'a, E: 'a, A: 'a>(e: E) -> WrapE<'a, A, E, Self>
where
Self: 'a,
{
@ -375,9 +375,9 @@ pub trait MonadFailAnyExt: MonadFailAny {
}
fn speculative<'a, A: 'a, B: 'a, E0: 'a, E1: 'a>(
wwa: <Self::W<E1> as WeakFunctor>::F<'a, <Self::W<E0> as WeakFunctor>::F<'a, A>>,
wwb: <Self::W<E1> as WeakFunctor>::F<'a, <Self::W<E0> as WeakFunctor>::F<'a, B>>,
) -> <Self::W<Result<E0, E1>> as WeakFunctor>::F<'a, (A, B)>
wwa: WrapE<'a, WrapE<'a, A, E0, Self>, E1, Self>,
wwb: WrapE<'a, WrapE<'a, B, E0, Self>, E1, Self>,
) -> WrapE<'a, (A, B), Result<E0, E1>, Self>
where
Self: 'a,
{

View File

@ -5,7 +5,7 @@ pub enum Selected<'a, A: 'a, B: 'a, T: ?Sized + 'a + WeakFunctor> {
B(T::F<'a, A>, B),
}
pub type SelectedWrapped<'a, A, B, T> = <T as WeakFunctor>::F<'a, Selected<'a, A, B, T>>;
pub type SelectedWrapped<'a, A, B, T> = Wrap<'a, Selected<'a, A, B, T>, T>;
pub trait ApplicativeSelect: Functor {
fn select<'a, A: 'a, B: 'a>(

View File

@ -73,7 +73,7 @@ impl<
/// Next [Iterative] state, wrapped.
pub type IterativeWrapped<'a, F> =
<<F as Iterative<'a>>::T as WeakFunctor>::F<'a, ControlFlow<<F as Iterative<'a>>::B, F>>;
Wrap<'a, ControlFlow<<F as Iterative<'a>>::B, F>, <F as Iterative<'a>>::T>;
/// Value passed to [`Monad::iterate`].
pub trait Iterative<'a>: 'a + Sized {

View File

@ -142,7 +142,7 @@ impl<
F: Iterative<'a, T = CompositionInstance<U, V>>,
> Iterative<'a> for ComposedIterative<F>
{
type B = <V as WeakFunctor>::F<'a, F::B>;
type B = Wrap<'a, F::B, V>;
type T = U;
fn next(self) -> IterativeWrapped<'a, Self> {

View File

@ -176,45 +176,7 @@ impl<Ex, Fallible: MonadFailAny> MonadFailAny for DeriveFailAny<Ex, Fallible> {
type T = Fallible::W<Ex>;
fn map_err<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
f: impl 'a + FnOnce(E0) -> E1,
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
where
Self: 'a,
{
Fallible::map_err(wa, |err| err.map(f))
}
fn bind_err<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
f: impl 'a + FnOnce(E0) -> <Self::W<E1> as WeakFunctor>::F<'a, A>,
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
where
Self: 'a,
{
Fallible::bind_err(wa, |err| match err {
Ok(e0) => f(e0),
Err(ex) => Fallible::fail(Err(ex)),
})
}
fn bind<'a, A: 'a, B: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
f: impl 'a + FnOnce(Result<A, E0>) -> <Self::W<E1> as WeakFunctor>::F<'a, B>,
) -> <Self::W<E1> as WeakFunctor>::F<'a, B>
where
Self: 'a,
{
Fallible::bind(wa, |result| match result {
Ok(a) => f(Ok(a)),
Err(Ok(e0)) => f(Err(e0)),
Err(Err(ex)) => Fallible::fail(Err(ex)),
})
}
fn unstuff<'a, A: 'a, E: 'a>(
wa: <Self::W<E> as WeakFunctor>::F<'a, A>,
) -> <Fallible::W<Ex> as WeakFunctor>::F<'a, Result<A, E>>
fn unstuff<'a, A: 'a, E: 'a>(wa: WrapE<'a, A, E, Self>) -> Wrap<'a, Result<A, E>, Self::T>
where
Self: 'a,
Fallible::W<Ex>: 'a,
@ -225,9 +187,7 @@ impl<Ex, Fallible: MonadFailAny> MonadFailAny for DeriveFailAny<Ex, Fallible> {
})
}
fn stuff<'a, A: 'a, E: 'a>(
fa: <Fallible::W<Ex> as WeakFunctor>::F<'a, Result<A, E>>,
) -> <Self::W<E> as WeakFunctor>::F<'a, A>
fn stuff<'a, A: 'a, E: 'a>(fa: Wrap<'a, Result<A, E>, Self::T>) -> WrapE<'a, A, E, Self>
where
Self: 'a,
Fallible::W<Ex>: 'a,
@ -238,6 +198,43 @@ impl<Ex, Fallible: MonadFailAny> MonadFailAny for DeriveFailAny<Ex, Fallible> {
Err(ex) => Fallible::fail(Err(ex)),
})
}
fn map_err<'a, A: 'a, E0: 'a, E1: 'a>(
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(E0) -> E1,
) -> WrapE<'a, A, E1, Self>
where
Self: 'a,
{
Fallible::map_err(wa, |err| err.map(f))
}
fn bind_err<'a, 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>
where
Self: 'a,
{
Fallible::bind_err(wa, |err| match err {
Ok(e0) => f(e0),
Err(ex) => Fallible::fail(Err(ex)),
})
}
fn bind<'a, 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>
where
Self: 'a,
{
Fallible::bind(wa, |result| match result {
Ok(a) => f(Ok(a)),
Err(Ok(e0)) => f(Err(e0)),
Err(Err(ex)) => Fallible::fail(Err(ex)),
})
}
}
impl<T: SharedFunctor, O: DeriveWeakFunctor> SharedFunctor for OverloadInstance<T, O> {

View File

@ -189,10 +189,24 @@ impl MonadFailAny for ResultFailAny {
type T = instances::solo::SoloInstance;
fn unstuff<'a, A: 'a, E: 'a>(wa: WrapE<'a, A, E, Self>) -> Wrap<'a, Result<A, E>, Self::T>
where
Self: 'a,
{
wa
}
fn stuff<'a, A: 'a, E: 'a>(fa: Wrap<'a, Result<A, E>, Self::T>) -> WrapE<'a, A, E, Self>
where
Self: 'a,
{
fa
}
fn map_err<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(E0) -> E1,
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
) -> WrapE<'a, A, E1, Self>
where
Self: 'a,
{
@ -200,9 +214,9 @@ impl MonadFailAny for ResultFailAny {
}
fn bind_err<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
f: impl 'a + FnOnce(E0) -> <Self::W<E1> as WeakFunctor>::F<'a, A>,
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(E0) -> WrapE<'a, A, E1, Self>,
) -> WrapE<'a, A, E1, Self>
where
Self: 'a,
{
@ -210,9 +224,9 @@ impl MonadFailAny for ResultFailAny {
}
fn bind<'a, A: 'a, B: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
f: impl 'a + FnOnce(Result<A, E0>) -> <Self::W<E1> as WeakFunctor>::F<'a, B>,
) -> <Self::W<E1> as WeakFunctor>::F<'a, B>
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(Result<A, E0>) -> WrapE<'a, B, E1, Self>,
) -> WrapE<'a, B, E1, Self>
where
Self: 'a,
{
@ -220,8 +234,8 @@ impl MonadFailAny for ResultFailAny {
}
fn rotate_out<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, Result<A, E1>>,
) -> <Self::W<Result<E1, E0>> as WeakFunctor>::F<'a, A>
wa: WrapE<'a, Result<A, E1>, E0, Self>,
) -> WrapE<'a, A, Result<E1, E0>, Self>
where
Self: 'a,
{
@ -231,24 +245,6 @@ impl MonadFailAny for ResultFailAny {
Err(e) => Err(Err(e)),
}
}
fn unstuff<'a, A: 'a, E: 'a>(
wa: <Self::W<E> as WeakFunctor>::F<'a, A>,
) -> <Self::T as WeakFunctor>::F<'a, Result<A, E>>
where
Self: 'a,
{
wa
}
fn stuff<'a, A: 'a, E: 'a>(
fa: <Self::T as WeakFunctor>::F<'a, Result<A, E>>,
) -> <Self::W<E> as WeakFunctor>::F<'a, A>
where
Self: 'a,
{
fa
}
}
pub struct ResultFailOver<T: Monad>(T);
@ -258,10 +254,24 @@ impl<T: Monad> MonadFailAny for ResultFailOver<T> {
type T = T;
fn unstuff<'a, A: 'a, E: 'a>(wa: WrapE<'a, A, E, Self>) -> Wrap<'a, Result<A, E>, Self::T>
where
Self: 'a,
{
wa
}
fn stuff<'a, A: 'a, E: 'a>(fa: Wrap<'a, Result<A, E>, Self::T>) -> WrapE<'a, A, E, Self>
where
Self: 'a,
{
fa
}
fn map_err<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(E0) -> E1,
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
) -> WrapE<'a, A, E1, Self>
where
Self: 'a,
{
@ -269,9 +279,9 @@ impl<T: Monad> MonadFailAny for ResultFailOver<T> {
}
fn bind_err<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
f: impl 'a + FnOnce(E0) -> <Self::W<E1> as WeakFunctor>::F<'a, A>,
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(E0) -> WrapE<'a, A, E1, Self>,
) -> WrapE<'a, A, E1, Self>
where
Self: 'a,
{
@ -282,9 +292,9 @@ impl<T: Monad> MonadFailAny for ResultFailOver<T> {
}
fn bind<'a, A: 'a, B: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
f: impl 'a + FnOnce(Result<A, E0>) -> <Self::W<E1> as WeakFunctor>::F<'a, B>,
) -> <Self::W<E1> as WeakFunctor>::F<'a, B>
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(Result<A, E0>) -> WrapE<'a, B, E1, Self>,
) -> WrapE<'a, B, E1, Self>
where
Self: 'a,
{
@ -292,29 +302,11 @@ impl<T: Monad> MonadFailAny for ResultFailOver<T> {
}
fn rotate_out<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, Result<A, E1>>,
) -> <Self::W<Result<E1, E0>> as WeakFunctor>::F<'a, A>
wa: WrapE<'a, Result<A, E1>, E0, Self>,
) -> WrapE<'a, A, Result<E1, E0>, Self>
where
Self: 'a,
{
T::fmap(<ResultFailAny as MonadFailAny>::rotate_out, wa)
}
fn unstuff<'a, A: 'a, E: 'a>(
wa: <Self::W<E> as WeakFunctor>::F<'a, A>,
) -> <Self::T as WeakFunctor>::F<'a, Result<A, E>>
where
Self: 'a,
{
wa
}
fn stuff<'a, A: 'a, E: 'a>(
fa: <Self::T as WeakFunctor>::F<'a, Result<A, E>>,
) -> <Self::W<E> as WeakFunctor>::F<'a, A>
where
Self: 'a,
{
fa
}
}

View File

@ -187,10 +187,24 @@ impl MonadFailAny for FutureFailAny {
type T = instances::future::FutureInstance;
fn unstuff<'a, A: 'a, E: 'a>(wa: WrapE<'a, A, E, Self>) -> Wrap<'a, Result<A, E>, Self::T>
where
Self: 'a,
{
wa
}
fn stuff<'a, A: 'a, E: 'a>(fa: Wrap<'a, Result<A, E>, Self::T>) -> WrapE<'a, A, E, Self>
where
Self: 'a,
{
fa
}
fn map_err<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(E0) -> E1,
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
) -> WrapE<'a, A, E1, Self>
where
Self: 'a,
{
@ -198,9 +212,9 @@ impl MonadFailAny for FutureFailAny {
}
fn bind_err<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
f: impl 'a + FnOnce(E0) -> <Self::W<E1> as WeakFunctor>::F<'a, A>,
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(E0) -> WrapE<'a, A, E1, Self>,
) -> WrapE<'a, A, E1, Self>
where
Self: 'a,
{
@ -213,9 +227,9 @@ impl MonadFailAny for FutureFailAny {
}
fn bind<'a, A: 'a, B: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
f: impl 'a + FnOnce(Result<A, E0>) -> <Self::W<E1> as WeakFunctor>::F<'a, B>,
) -> <Self::W<E1> as WeakFunctor>::F<'a, B>
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(Result<A, E0>) -> WrapE<'a, B, E1, Self>,
) -> WrapE<'a, B, E1, Self>
where
Self: 'a,
{
@ -223,8 +237,8 @@ impl MonadFailAny for FutureFailAny {
}
fn rotate_out<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, Result<A, E1>>,
) -> <Self::W<Result<E1, E0>> as WeakFunctor>::F<'a, A>
wa: WrapE<'a, Result<A, E1>, E0, Self>,
) -> WrapE<'a, A, Result<E1, E0>, Self>
where
Self: 'a,
{
@ -236,22 +250,4 @@ impl MonadFailAny for FutureFailAny {
}
})
}
fn unstuff<'a, A: 'a, E: 'a>(
wa: <Self::W<E> as WeakFunctor>::F<'a, A>,
) -> <Self::T as WeakFunctor>::F<'a, Result<A, E>>
where
Self: 'a,
{
wa
}
fn stuff<'a, A: 'a, E: 'a>(
fa: <Self::T as WeakFunctor>::F<'a, Result<A, E>>,
) -> <Self::W<E> as WeakFunctor>::F<'a, A>
where
Self: 'a,
{
fa
}
}

View File

@ -1,15 +1,13 @@
use super::*;
type Fwa<'a, A, E0, E1, Fallible> = <<Fallible as MonadFailAny>::T as WeakFunctor>::F<
'a,
Result<<<Fallible as MonadFailAny>::W<E0> as WeakFunctor>::F<'a, A>, E1>,
>;
type Fwa<'a, A, E0, E1, Fallible> =
Wrap<'a, Result<WrapE<'a, A, E0, Fallible>, E1>, <Fallible as MonadFailAny>::T>;
pub trait SpeculativeFail: MonadFailAny {
fn _speculative_a_wb<'a, A: 'a, B: 'a, E0: 'a, E1: 'a>(
a: Result<A, E0>,
wb: Result<<Self::W<E0> as WeakFunctor>::F<'a, B>, E1>,
) -> <Self::W<Result<E0, E1>> as WeakFunctor>::F<'a, (A, B)>
wb: Result<WrapE<'a, B, E0, Self>, E1>,
) -> WrapE<'a, (A, B), Result<E0, E1>, Self>
where
Self: 'a,
{
@ -25,7 +23,7 @@ pub trait SpeculativeFail: MonadFailAny {
fn _speculative_a_fwb<'a, A: 'a, B: 'a, E0: 'a, E1: 'a>(
a: Result<A, E0>,
fwb: Fwa<'a, B, E0, E1, Self>,
) -> <Self::W<Result<E0, E1>> as WeakFunctor>::F<'a, (A, B)>
) -> WrapE<'a, (A, B), Result<E0, E1>, Self>
where
Self: 'a,
{
@ -35,9 +33,9 @@ pub trait SpeculativeFail: MonadFailAny {
}
fn _speculative_fa_wb<'a, A: 'a, B: 'a, E0: 'a>(
fa: <Self::T as WeakFunctor>::F<'a, Result<A, E0>>,
wb: <Self::W<E0> as WeakFunctor>::F<'a, B>,
) -> <Self::W<E0> as WeakFunctor>::F<'a, (A, B)>
fa: Wrap<'a, Result<A, E0>, Self::T>,
wb: WrapE<'a, B, E0, Self>,
) -> WrapE<'a, (A, B), E0, Self>
where
Self: 'a,
{
@ -45,9 +43,9 @@ pub trait SpeculativeFail: MonadFailAny {
}
fn _speculative_wa_fwb<'a, A: 'a, B: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
wa: WrapE<'a, A, E0, Self>,
fwb: Fwa<'a, B, E0, E1, Self>,
) -> <Self::W<Result<E0, E1>> as WeakFunctor>::F<'a, (A, B)>
) -> WrapE<'a, (A, B), Result<E0, E1>, Self>
where
Self: 'a,
{
@ -70,8 +68,8 @@ pub trait SpeculativeFail: MonadFailAny {
fn _speculative_fwa_wb<'a, A: 'a, B: 'a, E0: 'a, E1: 'a>(
fwa: Fwa<'a, A, E0, E1, Self>,
wb: <Self::W<E0> as WeakFunctor>::F<'a, B>,
) -> <Self::W<Result<E0, E1>> as WeakFunctor>::F<'a, (A, B)>
wb: WrapE<'a, B, E0, Self>,
) -> WrapE<'a, (A, B), Result<E0, E1>, Self>
where
Self: 'a,
{
@ -82,9 +80,9 @@ pub trait SpeculativeFail: MonadFailAny {
}
fn speculative<'a, A: 'a, B: 'a, E0: 'a, E1: 'a>(
wwa: <Self::W<E1> as WeakFunctor>::F<'a, <Self::W<E0> as WeakFunctor>::F<'a, A>>,
wwb: <Self::W<E1> as WeakFunctor>::F<'a, <Self::W<E0> as WeakFunctor>::F<'a, B>>,
) -> <Self::W<Result<E0, E1>> as WeakFunctor>::F<'a, (A, B)>
wwa: WrapE<'a, WrapE<'a, A, E0, Self>, E1, Self>,
wwb: WrapE<'a, WrapE<'a, B, E0, Self>, E1, Self>,
) -> WrapE<'a, (A, B), Result<E0, E1>, Self>
where
Self: 'a,
{

View File

@ -55,7 +55,7 @@ pub trait Context {
}
/// Helper alias for [`WeakFunctor::F`] of [`Context::T`].
pub type Wrapped<'a, Ctx, A> = <<Ctx as Context>::T as WeakFunctor>::F<'a, A>;
pub type Wrapped<'a, Ctx, A> = Wrap<'a, A, <Ctx as Context>::T>;
/// Fundamental trait for ADN objects.
pub trait Mentionable<'a, Ctx: 'a + Context>: 'a + Serializable {
@ -76,9 +76,10 @@ pub trait Mentionable<'a, Ctx: 'a + Context>: 'a + Serializable {
fn points_typed(&self, points: &mut impl TakesPoints<'a, Ctx>);
}
pub type Fctr<'a, Ctx, A> = <A as Mentionable<'a, Ctx>>::Fctr;
/// Shorthand for the type of vaalues returned by [`Factory::deserialize`].
pub type ParseResult<'a, Ctx, F> =
Result<<F as Factory<'a, Ctx>>::Mtbl, <F as Factory<'a, Ctx>>::ParseError>;
pub type ParseResult<'a, Ctx, F> = Result<Mtbl<'a, Ctx, F>, <F as Factory<'a, Ctx>>::ParseError>;
/// Trait representing deserialisation rules for [Mentionable]s.
/// Crucial for [`crate::rstd::typeless`].
@ -99,6 +100,10 @@ pub trait Factory<'a, Ctx: 'a + Context>: 'a + Send + Sync + Clone {
fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError;
}
pub type Mtbl<'a, Ctx, F> = <F as Factory<'a, Ctx>>::Mtbl;
pub type ParseError<'a, Ctx, F> = <F as Factory<'a, Ctx>>::ParseError;
/// Extension trait for factories.
pub trait ExtFactory<'a, Ctx: 'a + Context>: Factory<'a, Ctx> {
/// Parse the object from a slice.

View File

@ -5,7 +5,9 @@ pub trait Origin<'a, Ctx: 'a + Context>: 'a {
/// Type of the associated object.
type Mtbl: Mentionable<'a, Ctx>;
/// Value of the associated factory.
fn factory(&self) -> <Self::Mtbl as Mentionable<'a, Ctx>>::Fctr;
fn factory(&self) -> OFctr<'a, Ctx, Self>;
/// Try resolving the value.
fn resolve(self: Rc<Self>) -> Resolution<'a, Ctx, Self::Mtbl>;
}
pub type OFctr<'a, Ctx, O> = Fctr<'a, Ctx, <O as Origin<'a, Ctx>>::Mtbl>;

View File

@ -10,10 +10,8 @@ pub enum ResolutionError<L, P> {
}
/// See [`ResolutionResult`].
pub type ResolutionFailure<'a, Ctx, A> = ResolutionError<
<Ctx as Context>::LookupError<'a>,
<<A as Mentionable<'a, Ctx>>::Fctr as Factory<'a, Ctx>>::ParseError,
>;
pub type ResolutionFailure<'a, Ctx, A> =
ResolutionError<<Ctx as Context>::LookupError<'a>, ParseError<'a, Ctx, Fctr<'a, Ctx, A>>>;
/// Result yielded by [`Origin`].
pub type ResolutionResult<'a, Ctx, A> = Result<Rc<A>, ResolutionFailure<'a, Ctx, A>>;

View File

@ -44,7 +44,7 @@ fn _resolve_origin<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>>(
impl<'a, Ctx: 'a + Context, F: Factory<'a, Ctx>> Origin<'a, Ctx> for ResolverOrigin<'a, Ctx, F> {
type Mtbl = F::Mtbl;
fn factory(&self) -> <Self::Mtbl as Mentionable<'a, Ctx>>::Fctr {
fn factory(&self) -> OFctr<'a, Ctx, Self> {
self.r_factory.clone()
}

View File

@ -44,35 +44,23 @@ impl<S: Serializable> ExtSerializable for S {
pub trait FallibleContext: Context {
/// Convert a fallible wrapped into a wrapped result.
fn unstuff<'a, A: 'a, E: 'a>(
wa: <<Self::Fallible as MonadFailAny>::W<E> as WeakFunctor>::F<'a, A>,
) -> <Self::T as WeakFunctor>::F<'a, Result<A, E>>
where
Self: 'a;
/// Convert a wrapped result into a fallible wrapped.
fn stuff<'a, A: 'a, E: 'a>(
fa: <Self::T as WeakFunctor>::F<'a, Result<A, E>>,
) -> <<Self::Fallible as MonadFailAny>::W<E> as WeakFunctor>::F<'a, A>
where
Self: 'a;
}
impl<Ctx: Context> FallibleContext for Ctx {
fn unstuff<'a, A: 'a, E: 'a>(
wa: <<Self::Fallible as MonadFailAny>::W<E> as WeakFunctor>::F<'a, A>,
) -> <Self::T as WeakFunctor>::F<'a, Result<A, E>>
wa: WrapE<'a, A, E, Self::Fallible>,
) -> Wrap<'a, Result<A, E>, Self::T>
where
Self: 'a,
{
Self::Fallible::unstuff(wa)
}
/// Convert a wrapped result into a fallible wrapped.
fn stuff<'a, A: 'a, E: 'a>(
fa: <Self::T as WeakFunctor>::F<'a, Result<A, E>>,
) -> <<Self::Fallible as MonadFailAny>::W<E> as WeakFunctor>::F<'a, A>
fa: Wrap<'a, Result<A, E>, Self::T>,
) -> WrapE<'a, A, E, Self::Fallible>
where
Self: 'a,
{
Self::Fallible::stuff(fa)
}
}
impl<Ctx: Context> FallibleContext for Ctx {}

View File

@ -148,8 +148,7 @@ where
}
/// Returned by [`TypelessMentionable::cast`].
pub type CastResult<'a, Ctx, A> =
Result<A, <<A as Mentionable<'a, Ctx>>::Fctr as Factory<'a, Ctx>>::ParseError>;
pub type CastResult<'a, Ctx, A> = Result<A, ParseError<'a, Ctx, Fctr<'a, Ctx, A>>>;
impl<'a, Ctx: Context> TypelessMentionable<'a, Ctx>
where

View File

@ -57,10 +57,7 @@ where
type B = B;
type FA = A::Fctr;
type FB = B::Fctr;
type ParseError = PairParseError<
<A::Fctr as Factory<'a, Ctx>>::ParseError,
<B::Fctr as Factory<'a, Ctx>>::ParseError,
>;
type ParseError = PairParseError<ParseError<'a, Ctx, A::Fctr>, ParseError<'a, Ctx, B::Fctr>>;
fn factories(factory_data: &Self::FactoryData) -> (&Self::FA, &Self::FB) {
(&factory_data.a, &factory_data.b)
@ -72,14 +69,14 @@ where
fn from_error_a(
_factory_data: &Self::FactoryData,
error: <Self::FA as Factory<'a, Ctx>>::ParseError,
error: ParseError<'a, Ctx, Self::FA>,
) -> Self::ParseError {
PairParseError::A(error)
}
fn from_error_b(
_factory_data: &Self::FactoryData,
error: <Self::FB as Factory<'a, Ctx>>::ParseError,
error: ParseError<'a, Ctx, Self::FB>,
) -> Self::ParseError {
PairParseError::B(error)
}

View File

@ -6,8 +6,7 @@ use crate::rstd::{fallible::*, *};
use super::*;
pub type TPE<'a, Ctx, A> =
TreeParseError<<<A as Mentionable<'a, Ctx>>::Fctr as Factory<'a, Ctx>>::ParseError>;
pub type TPE<'a, Ctx, A> = TreeParseError<ParseError<'a, Ctx, Fctr<'a, Ctx, A>>>;
pub type TreeFaiure<'a, Ctx, A> =
ResolutionError<<Ctx as Context>::LookupError<'a>, TPE<'a, Ctx, A>>;
@ -90,9 +89,10 @@ impl<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>>
{
fn resolve(
&self,
) -> <SubsetMonad<'a, Ctx, A> as WeakFunctor>::F<
) -> Wrap<
'a,
Rc<dyn TraversibleBinaryNode<'a, SubsetMonad<'a, Ctx, A>, A, RefData>>,
SubsetMonad<'a, Ctx, A>,
> {
<SubsetMonad<'a, Ctx, A> as Functor>::fmap(
|resolved| resolved as Rc<dyn TraversibleBinaryNode<'a, _, _, _>>,
@ -111,9 +111,10 @@ impl<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>>
{
fn resolve(
&self,
) -> <SubsetMonad<'a, Ctx, A> as WeakFunctor>::F<
) -> Wrap<
'a,
Rc<dyn TraversibleBinaryNode<'a, SubsetMonad<'a, Ctx, A>, A, RefData>>,
SubsetMonad<'a, Ctx, A>,
> {
<SubsetMonad<'a, Ctx, A> as Functor>::fmap(
|resolved| resolved as Rc<dyn TraversibleBinaryNode<'a, _, _, _>>,
@ -132,9 +133,10 @@ impl<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>>
{
fn resolve(
&self,
) -> <SubsetMonad<'a, Ctx, A> as WeakFunctor>::F<
) -> Wrap<
'a,
Rc<dyn TraversibleBinaryNode<'a, SubsetMonad<'a, Ctx, A>, A, RefData>>,
SubsetMonad<'a, Ctx, A>,
> {
<SubsetMonad<'a, Ctx, A> as Functor>::fmap(
|resolved| resolved as Rc<dyn TraversibleBinaryNode<'a, _, _, _>>,

View File

@ -80,7 +80,7 @@ impl<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>> Factory<'a, Ctx>
{
type Mtbl = StackNode<'a, Ctx, A>;
type ParseError = StackParseError<<A::Fctr as Factory<'a, Ctx>>::ParseError>;
type ParseError = StackParseError<ParseError<'a, Ctx, A::Fctr>>;
fn deserialize(
&self,

View File

@ -6,4 +6,4 @@ use super::*;
pub type FallibleMonad<Ctx, E> = <<Ctx as Context>::Fallible as MonadFailAny>::W<E>;
/// Preferred [Wrapped] [Result].
pub type FallibleWrapped<'a, Ctx, A, E> = <FallibleMonad<Ctx, E> as WeakFunctor>::F<'a, A>;
pub type FallibleWrapped<'a, Ctx, A, E> = Wrap<'a, A, FallibleMonad<Ctx, E>>;

View File

@ -131,7 +131,7 @@ impl<P: Error> From<P> for CheckedParseError<P> {
/// Returned by [`CheckedParse::deserialize_checked`].
pub type CheckedParseResult<'a, Ctx, F> =
Result<<F as Factory<'a, Ctx>>::Mtbl, CheckedParseError<<F as Factory<'a, Ctx>>::ParseError>>;
Result<Mtbl<'a, Ctx, F>, CheckedParseError<ParseError<'a, Ctx, F>>>;
/// Extension trait for factories that ensures fixed size read.
pub trait CheckedParse<'a, Ctx: 'a + Context>: FixedSizeFactory + Factory<'a, Ctx> {

View File

@ -50,12 +50,12 @@ pub trait StaticPair<'a, Ctx: 'a + Context>:
/// Regularise the error returned while parsing the first element.
fn from_error_a(
factory_data: &Self::FactoryData,
error: <Self::FA as Factory<'a, Ctx>>::ParseError,
error: ParseError<'a, Ctx, Self::FA>,
) -> Self::ParseError;
/// Regularise the error returned while parsing the second element.
fn from_error_b(
factory_data: &Self::FactoryData,
error: <Self::FB as Factory<'a, Ctx>>::ParseError,
error: ParseError<'a, Ctx, Self::FB>,
) -> Self::ParseError;
/// Derive factory data from the object data.
fn factory_data(&self) -> Self::FactoryData;

View File

@ -18,25 +18,8 @@ pub trait MappableOrigin<'a, Ctx: 'a + Context>: Origin<'a, Ctx> {
+ Send
+ Sync
+ Clone
+ Fn(
<<Self::Mtbl as Mentionable<'a, Ctx>>::Fctr as Factory<'a, Ctx>>::ParseError,
) -> <B::Fctr as Factory<'a, Ctx>>::ParseError,
map_factory: impl 'a + FnOnce(<Self::Mtbl as Mentionable<'a, Ctx>>::Fctr) -> B::Fctr,
) -> Rc<dyn Origin<'a, Ctx, Mtbl = B>>;
}
impl<'a, Ctx: 'a + Context, O: ?Sized + Origin<'a, Ctx>> MappableOrigin<'a, Ctx> for O {
fn map<B: Mentionable<'a, Ctx>>(
self: Rc<Self>,
map_ok: impl 'a + Send + Sync + Clone + Fn(Rc<Self::Mtbl>) -> B,
map_err: impl 'a
+ Send
+ Sync
+ Clone
+ Fn(
<<Self::Mtbl as Mentionable<'a, Ctx>>::Fctr as Factory<'a, Ctx>>::ParseError,
) -> <B::Fctr as Factory<'a, Ctx>>::ParseError,
map_factory: impl 'a + FnOnce(<Self::Mtbl as Mentionable<'a, Ctx>>::Fctr) -> B::Fctr,
+ Fn(ParseError<'a, Ctx, OFctr<'a, Ctx, Self>>) -> ParseError<'a, Ctx, B::Fctr>,
map_factory: impl 'a + FnOnce(OFctr<'a, Ctx, Self>) -> B::Fctr,
) -> Rc<dyn Origin<'a, Ctx, Mtbl = B>> {
let origin = self.clone();
let origin: WrappedOrigin<'a, Ctx, B> = WrappedOrigin {
@ -52,11 +35,12 @@ impl<'a, Ctx: 'a + Context, O: ?Sized + Origin<'a, Ctx>> MappableOrigin<'a, Ctx>
}
}
impl<'a, Ctx: 'a + Context, O: ?Sized + Origin<'a, Ctx>> MappableOrigin<'a, Ctx> for O {}
fn map_resolve<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>, B: Mentionable<'a, Ctx>>(
resolve: impl 'a + Fn() -> Resolution<'a, Ctx, A>,
map_ok: impl 'a + Fn(Rc<A>) -> B,
map_err: impl 'a
+ Fn(<A::Fctr as Factory<'a, Ctx>>::ParseError) -> <B::Fctr as Factory<'a, Ctx>>::ParseError,
map_err: impl 'a + Fn(ParseError<'a, Ctx, A::Fctr>) -> ParseError<'a, Ctx, B::Fctr>,
) -> Resolution<'a, Ctx, B> {
Ctx::T::fmap(
move |resolved| match resolved {
@ -80,7 +64,7 @@ struct WrappedOrigin<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>> {
impl<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>> Origin<'a, Ctx> for WrappedOrigin<'a, Ctx, A> {
type Mtbl = A;
fn factory(&self) -> <Self::Mtbl as Mentionable<'a, Ctx>>::Fctr {
fn factory(&self) -> Fctr<'a, Ctx, Self::Mtbl> {
self.w_factory.clone()
}