radn-rs/src/func/classes/composition.rs

180 lines
4.5 KiB
Rust

use crate::func::*;
pub struct CompositionClass<U, V>(U, V);
impl<U: WeakFunctor, V: WeakFunctor> WeakFunctor for CompositionClass<U, V> {
type F<'a, A: 'a> = U::F<'a, V::F<'a, A>> where Self: 'a;
}
impl<U: Functor, V: Functor> Functor for CompositionClass<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 CompositionClass<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 CompositionClass<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 CompositionClass<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 CompositionClass<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: Applicative, V: Applicative> Applicative for CompositionClass<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 CompositionClass<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 ibind<'a, A: 'a, B: 'a>(
a: A,
mut f: impl 'a + FnMut(A) -> Self::F<'a, IState<A, B>>,
) -> Self::F<'a, B>
where
Self: 'a,
{
U::ibind(a, move |a| {
let fx = f(a);
U::fmap(|ustate| V::unstuff(ustate), fx)
})
}
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))
}
}
impl<E, U: Monad, V: MonadFail<E> + LocalFunctor> MonadFail<E> for CompositionClass<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 CompositionClass<U, V> {
fn unstuff<'a, A: 'a, B: 'a>(state: Self::F<'a, IState<A, B>>) -> IState<A, Self::F<'a, B>>
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 CompositionClass<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 CompositionClass<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))
}
}