use crate::func::*; pub struct CompositionInstance(U, V); impl WeakFunctor for CompositionInstance { type F<'a, A: 'a> = U::F<'a, V::F<'a, A>> where Self: 'a; } impl Functor for CompositionInstance { 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 Pure for CompositionInstance { fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> where Self: 'a, { U::pure(V::pure(a)) } } impl ApplicativeSeq for CompositionInstance { 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 ApplicativeLA2 for CompositionInstance { 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 ApplicativeTuple for CompositionInstance { 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 ApplicativeSelect for CompositionInstance { 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 Applicative for CompositionInstance { 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 Monad for CompositionInstance { 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); impl< 'a, U: 'a + Monad, V: 'a + Monad + LocalFunctor, F: Iterative<'a, T = CompositionInstance>, > Iterative<'a> for ComposedIterative { type B = Wrap<'a, F::B, V>; 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 + LocalFunctor> Fail for CompositionInstance { fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A> where Self: 'a, E: 'a, { U::pure(V::fail(e)) } } impl LocalFunctor for CompositionInstance { fn unstuff<'a, A: 'a, B: 'a>( state: Self::F<'a, ControlFlow>, ) -> ControlFlow, 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 SharedFunctor for CompositionInstance { 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 CovariantFunctor for CompositionInstance { 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)) } }