radn-rs/src/func/classes/overload.rs
2023-05-20 12:11:06 +00:00

197 lines
4.6 KiB
Rust

use std::marker::PhantomData;
use crate::func::*;
pub struct OverloadClass<T, O>(T, O);
pub trait DeriveWeakFunctor {}
impl<O: DeriveFunctor> DeriveWeakFunctor for O {}
pub trait DeriveFunctor {}
impl<O: DeriveApplicative> DeriveFunctor for O {}
pub trait DeriveApplicative {}
impl<O: DeriveMonad> DeriveApplicative for O {}
pub trait DeriveMonad {}
impl<T: WeakFunctor, O: DeriveWeakFunctor> WeakFunctor for OverloadClass<T, O> {
type F<'a, A: 'a> = T::F<'a, A>
where
Self: 'a;
}
impl<T: Functor, O: DeriveFunctor> Functor for OverloadClass<T, O> {
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,
{
T::fmap(f, fa)
}
fn replace<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, b: B) -> Self::F<'a, B>
where
Self: 'a,
{
T::replace(fa, b)
}
fn void<'a, A: 'a>(fa: Self::F<'a, A>) -> Self::F<'a, ()>
where
Self: 'a,
{
T::void(fa)
}
}
impl<T: Pure, O: DeriveApplicative> Pure for OverloadClass<T, O> {
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A>
where
Self: 'a,
{
T::pure(a)
}
}
impl<T: ApplicativeSeq, O: DeriveApplicative> ApplicativeSeq for OverloadClass<T, O> {
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,
{
T::seq(ff, fa)
}
}
impl<T: ApplicativeLA2, O: DeriveApplicative> ApplicativeLA2 for OverloadClass<T, O> {
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,
{
T::la2(f, fa, fb)
}
}
impl<T: ApplicativeTuple, O: DeriveApplicative> ApplicativeTuple for OverloadClass<T, O> {
fn tuple<'a, A: 'a, B: 'a>(fab: (Self::F<'a, A>, Self::F<'a, B>)) -> Self::F<'a, (A, B)>
where
Self: 'a,
{
T::tuple(fab)
}
}
impl<T: Applicative, O: DeriveApplicative> Applicative for OverloadClass<T, O> {
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,
{
T::discard_first(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,
{
T::discard_second(fa, fb)
}
}
struct OverloadIterative<F, O>(F, PhantomData<O>);
impl<F, O> OverloadIterative<F, O> {
fn new(f: F) -> Self {
Self(f, PhantomData)
}
}
impl<'a, T: 'a + Monad, O: 'a + DeriveMonad, F: AIterative<'a, T = OverloadClass<T, O>>>
AIterative<'a> for OverloadIterative<F, O>
{
type A = F::A;
type B = F::B;
type T = T;
fn next(self, a: Self::A) -> AIterativeWrapped<'a, Self> {
T::fmap(
|state| match state {
ControlFlow::Continue((next_a, next_f)) => {
ControlFlow::Continue((next_a, Self::new(next_f)))
}
ControlFlow::Break(b) => ControlFlow::Break(b),
},
self.0.next(a),
)
}
}
impl<'a, T: 'a + Monad, O: 'a + DeriveMonad, F: Iterative<'a, T = OverloadClass<T, O>>>
Iterative<'a> for OverloadIterative<F, O>
{
type B = F::B;
type T = T;
fn next(self) -> IterativeWrapped<'a, Self> {
T::fmap(
|state| match state {
ControlFlow::Continue(next_f) => ControlFlow::Continue(Self::new(next_f)),
ControlFlow::Break(b) => ControlFlow::Break(b),
},
self.0.next(),
)
}
}
impl<T: Monad, O: DeriveMonad> Monad for OverloadClass<T, O> {
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,
{
T::bind(fa, f)
}
fn iterate_mut<'a, A: 'a, B: 'a>(
a: A,
f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
) -> Self::F<'a, B>
where
Self: 'a,
{
T::iterate_mut(a, f)
}
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,
{
T::iterate_argument(a, OverloadIterative::new(f))
}
fn iterate<'a, B: 'a>(f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
where
Self: 'a,
{
T::iterate(OverloadIterative::new(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,
{
T::join(ffa)
}
}