use crate::func::*; pub struct CompositionClass(U, V); impl WeakFunctor for CompositionClass { type F<'a, A: 'a> = U::F<'a, V::F<'a, A>> where Self: 'a; } impl Functor for CompositionClass { 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 CompositionClass { fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> where Self: 'a, { U::pure(V::pure(a)) } } impl ApplicativeSeq for CompositionClass { 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 CompositionClass { 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 CompositionClass { 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 Applicative for CompositionClass { 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 CompositionClass { 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_mut<'a, A: 'a, B: 'a>( a: A, mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow>, ) -> Self::F<'a, B> where Self: 'a, { U::iterate_mut(a, move |a| { let fstate = f(a); U::fmap(|ustate| V::unstuff(ustate), fstate) }) } fn iterate_argument<'a, A: 'a, B: 'a>( a: A, f: impl AIterative<'a, T = Self, A = A, B = B>, ) -> Self::F<'a, B> where Self: 'a, { U::iterate_argument(a, AComposedIterative(f)) } 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 AComposedIterative(F); impl< 'a, U: 'a + Monad, V: 'a + Monad + LocalFunctor, F: AIterative<'a, T = CompositionClass>, > AIterative<'a> for AComposedIterative { type A = F::A; type B = ::F<'a, F::B>; type T = U; fn next(self, a: Self::A) -> AIterativeWrapped<'a, Self> { let fstate = self.0.next(a); U::fmap( |ustate| match V::unstuff(ustate) { ControlFlow::Continue((next_a, next_f)) => { ControlFlow::Continue((next_a, Self(next_f))) } ControlFlow::Break(b) => ControlFlow::Break(b), }, fstate, ) } } struct ComposedIterative(F); impl< 'a, U: 'a + Monad, V: 'a + Monad + LocalFunctor, F: Iterative<'a, T = CompositionClass>, > Iterative<'a> for ComposedIterative { type B = ::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 + LocalFunctor> Fail for CompositionClass { fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A> where Self: 'a, E: 'a, { U::pure(V::fail(e)) } } impl LocalFunctor for CompositionClass { 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 CompositionClass { 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 CompositionClass { 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)) } }