218 lines
5.6 KiB
Rust
218 lines
5.6 KiB
Rust
use crate::func::*;
|
|
|
|
pub struct CompositionInstance<U, V>(U, V);
|
|
|
|
impl<U: WeakFunctor, V: WeakFunctor> WeakFunctor for CompositionInstance<U, V> {
|
|
type F<'a, A: 'a> = U::F<'a, V::F<'a, A>> where Self: 'a;
|
|
}
|
|
|
|
impl<U: Functor, V: Functor> Functor for CompositionInstance<U, V> {
|
|
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,
|
|
{
|
|
U::fmap(|ua| V::fmap(f, ua), fa)
|
|
}
|
|
|
|
fn replace<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, b: B) -> Self::F<'a, B>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
U::fmap(|ua| V::replace(ua, b), fa)
|
|
}
|
|
|
|
fn void<'a, A: 'a>(fa: Self::F<'a, A>) -> Self::F<'a, ()>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
U::fmap(|ua| V::void(ua), fa)
|
|
}
|
|
}
|
|
|
|
impl<U: Pure, V: Pure> Pure for CompositionInstance<U, V> {
|
|
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
U::pure(V::pure(a))
|
|
}
|
|
}
|
|
|
|
impl<U: ApplicativeLA2, V: ApplicativeSeq> ApplicativeSeq for CompositionInstance<U, V> {
|
|
fn seq<'a, A: 'a, B: 'a>(
|
|
ff: Self::F<'a, impl 'a + FnOnce(A) -> B>,
|
|
fa: Self::F<'a, A>,
|
|
) -> Self::F<'a, B>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
U::la2(|uf, ua| V::seq(uf, ua), ff, fa)
|
|
}
|
|
}
|
|
|
|
impl<U: ApplicativeLA2, V: ApplicativeLA2> ApplicativeLA2 for CompositionInstance<U, V> {
|
|
fn la2<'a, 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>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
U::la2(|ua, ub| V::la2(f, ua, ub), fa, fb)
|
|
}
|
|
}
|
|
|
|
impl<U: ApplicativeTuple, V: ApplicativeTuple> ApplicativeTuple for CompositionInstance<U, V> {
|
|
fn tuple<'a, A: 'a, B: 'a>(fab: (Self::F<'a, A>, Self::F<'a, B>)) -> Self::F<'a, (A, B)>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
U::fmap(V::tuple, U::tuple(fab))
|
|
}
|
|
}
|
|
|
|
impl<U: ApplicativeSelect, V: Functor> ApplicativeSelect for CompositionInstance<U, V> {
|
|
fn select<'a, A: 'a, B: 'a>(
|
|
fa: Self::F<'a, A>,
|
|
fb: Self::F<'a, B>,
|
|
) -> SelectedWrapped<'a, A, B, Self>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
U::fmap(
|
|
|selected| match selected {
|
|
Selected::A(ua, fb) => V::fmap(|a| Selected::A(a, fb), ua),
|
|
Selected::B(fa, ub) => V::fmap(|b| Selected::B(fa, b), ub),
|
|
},
|
|
U::select(fa, fb),
|
|
)
|
|
}
|
|
}
|
|
|
|
impl<U: Applicative, V: Applicative> Applicative for CompositionInstance<U, V> {
|
|
fn discard_first<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, B>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
U::la2(|ua, ub| V::discard_first(ua, ub), fa, fb)
|
|
}
|
|
|
|
fn discard_second<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, A>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
U::la2(|ua, ub| V::discard_second(ua, ub), fa, fb)
|
|
}
|
|
}
|
|
|
|
impl<U: Monad, V: Monad + LocalFunctor> Monad for CompositionInstance<U, V> {
|
|
fn bind<'a, A: 'a, B: 'a>(
|
|
fa: Self::F<'a, A>,
|
|
f: impl 'a + FnOnce(A) -> Self::F<'a, B>,
|
|
) -> Self::F<'a, B>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
U::bind(fa, |ua| U::fmap(V::join, V::stuff::<_, U>(V::fmap(f, ua))))
|
|
}
|
|
|
|
fn iterate<'a, B: 'a>(f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
U::iterate(ComposedIterative(f))
|
|
}
|
|
|
|
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A>
|
|
where
|
|
Self::F<'a, A>: 'a,
|
|
Self: 'a,
|
|
{
|
|
U::join(U::fmap(|ufa| U::fmap(V::join, V::stuff::<_, U>(ufa)), ffa))
|
|
}
|
|
}
|
|
|
|
struct ComposedIterative<F>(F);
|
|
|
|
impl<
|
|
'a,
|
|
U: 'a + Monad,
|
|
V: 'a + Monad + LocalFunctor,
|
|
F: Iterative<'a, T = CompositionInstance<U, V>>,
|
|
> Iterative<'a> for ComposedIterative<F>
|
|
{
|
|
type B = <V as WeakFunctor>::F<'a, F::B>;
|
|
type T = U;
|
|
|
|
fn next(self) -> IterativeWrapped<'a, Self> {
|
|
let fstate = self.0.next();
|
|
U::fmap(
|
|
|ustate| match V::unstuff(ustate) {
|
|
ControlFlow::Continue(next_f) => ControlFlow::Continue(Self(next_f)),
|
|
ControlFlow::Break(b) => ControlFlow::Break(b),
|
|
},
|
|
fstate,
|
|
)
|
|
}
|
|
}
|
|
|
|
impl<E, U: Monad, V: Fail<E> + LocalFunctor> Fail<E> for CompositionInstance<U, V> {
|
|
fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A>
|
|
where
|
|
Self: 'a,
|
|
E: 'a,
|
|
{
|
|
U::pure(V::fail(e))
|
|
}
|
|
}
|
|
|
|
impl<U: LocalFunctor + Functor, V: LocalFunctor> LocalFunctor for CompositionInstance<U, V> {
|
|
fn unstuff<'a, A: 'a, B: 'a>(
|
|
state: Self::F<'a, ControlFlow<B, A>>,
|
|
) -> ControlFlow<Self::F<'a, B>, A>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
U::unstuff(U::fmap(V::unstuff, state))
|
|
}
|
|
|
|
fn stuff<'a, A: 'a, T: 'a + Pure>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
U::stuff::<_, T>(U::fmap(V::stuff::<_, T>, fa))
|
|
}
|
|
}
|
|
|
|
impl<U: SharedFunctor + Functor, V: SharedFunctor> SharedFunctor for CompositionInstance<U, V> {
|
|
type Shared<'a, A: 'a + Clone> = U::Shared<'a, V::Shared<'a, A>>
|
|
where
|
|
Self: 'a;
|
|
|
|
fn share<'a, A: 'a + Clone>(fa: Self::F<'a, A>) -> Self::Shared<'a, A>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
U::share(U::fmap(V::share, fa))
|
|
}
|
|
|
|
fn unshare<'a, A: 'a + Clone>(sa: Self::Shared<'a, A>) -> Self::F<'a, A>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
U::fmap(V::unshare, U::unshare(sa))
|
|
}
|
|
}
|
|
|
|
impl<U: CovariantFunctor + Functor, V: CovariantFunctor> CovariantFunctor
|
|
for CompositionInstance<U, V>
|
|
{
|
|
fn variate<'a: 'b, 'b, A: 'a>(fa: Self::F<'a, A>) -> Self::F<'b, A>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
U::fmap(V::variate, U::variate(fa))
|
|
}
|
|
}
|