remove AIterative, extract iterate_mut

This commit is contained in:
AF 2023-05-23 12:05:04 +00:00
parent b6980cc050
commit feb5e5daec
11 changed files with 30 additions and 393 deletions

View File

@ -23,8 +23,8 @@ pub use std::ops::ControlFlow;
pub use radn_derive::{CovariantFunctor, SharedFunctor};
pub use self::applicative_select::{ApplicativeSelect, Selected};
pub use self::controlflow::{AIterative, AIterativeWrapped, Iterative, IterativeWrapped};
use self::controlflow::{ArgumentedIterative, BindableMut, ControlFlowClass};
use self::controlflow::{BindableMut, ControlFlowClass};
pub use self::controlflow::{Iterative, IterativeWrapped};
/// Part of Haskell's `Functor f` responsible for having `f a`.
///
@ -180,39 +180,10 @@ pub trait Monad: Applicative {
where
Self: 'a;
/// Simplified, [`FnMut`] version of [`Monad::iterate_argument`].
/// Reasoning for this method existing at all is that
/// most usecases are better modelled with [`FnMut`]
/// rather than some dedicated state type.
///
/// Note: hypothetically, you can implement [`Monad::iterate_argument`] from [`Monad::iterate_mut`],
/// but it's highly discouraged.
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,
{
Self::iterate_argument(a, BindableMut::new(f))
}
/// Kleisli category composition special case used to represent loops.
/// Included for optimisation and clarity.
/// Generally, [`Monad::bind`] should be enough implement it.
/// See [`classes::stackless::StacklessClass::iterate`] for a generic, though less-than ideal, blanket implementation.
/// On practice, you generally shouldn't be using [`Monad::bind`]/[`Pure::pure`]/[`Functor::fmap`] here.
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,
{
Self::iterate(ArgumentedIterative(a, f))
}
/// Iteration without composition semantics of [`Monad::iterate_argument`].
fn iterate<'a, B: 'a>(f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
where
Self: 'a;
@ -227,6 +198,24 @@ pub trait Monad: Applicative {
}
}
pub trait MonadExt: Monad {
/// [`FnMut`] version of [`Monad::iterate`].
/// Reasoning for this method existing at all is that
/// most usecases are better modelled with [`FnMut`]
/// rather than some dedicated state type.
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,
{
Self::iterate(BindableMut::new(a, f))
}
}
impl<T: Monad> MonadExt for T {}
/// Part of [`MonadFail`] responsible for Haskell's `fail`.
pub trait Fail<E>: WeakFunctor {
/// Equivalent of Haskell's `fail`.

View File

@ -119,29 +119,6 @@ impl<U: Monad, V: Monad + LocalFunctor> Monad for CompositionClass<U, V> {
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<B, A>>,
) -> 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,
@ -158,33 +135,6 @@ impl<U: Monad, V: Monad + LocalFunctor> Monad for CompositionClass<U, V> {
}
}
struct AComposedIterative<F>(F);
impl<
'a,
U: 'a + Monad,
V: 'a + Monad + LocalFunctor,
F: AIterative<'a, T = CompositionClass<U, V>>,
> AIterative<'a> for AComposedIterative<F>
{
type A = F::A;
type B = <V as WeakFunctor>::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>(F);
impl<

View File

@ -108,40 +108,6 @@ impl Monad for FutureClass {
Box::pin(async { f(fa.await).await })
}
fn iterate_mut<'a, A: 'a, B: 'a>(
mut a: A,
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
) -> Self::F<'a, B>
where
Self: 'a,
{
Box::pin(async move {
loop {
match f(a).await {
ControlFlow::Continue(next_a) => a = next_a,
ControlFlow::Break(b) => return b,
};
}
})
}
fn iterate_argument<'a, A: 'a, B: 'a>(
mut a: A,
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
) -> Self::F<'a, B>
where
Self: 'a,
{
Box::pin(async move {
loop {
match f.next(a).await {
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
ControlFlow::Break(b) => return b,
}
}
})
}
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
where
Self: 'a,

View File

@ -90,36 +90,6 @@ impl Monad for LazyClass {
Box::new(|| f(fa())())
}
fn iterate_mut<'a, A: 'a, B: 'a>(
mut a: A,
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
) -> Self::F<'a, B>
where
Self: 'a,
{
Box::new(move || loop {
match f(a)() {
ControlFlow::Continue(next_a) => a = next_a,
ControlFlow::Break(b) => return b,
};
})
}
fn iterate_argument<'a, A: 'a, B: 'a>(
mut a: A,
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
) -> Self::F<'a, B>
where
Self: 'a,
{
loop {
match f.next(a)() {
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
ControlFlow::Break(b) => return Self::pure(b),
}
}
}
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
where
Self: 'a,

View File

@ -94,36 +94,6 @@ impl Monad for OptionClass {
f(fa?)
}
fn iterate_mut<'a, A: 'a, B: 'a>(
mut a: A,
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
) -> Self::F<'a, B>
where
Self: 'a,
{
loop {
match f(a)? {
ControlFlow::Continue(next_a) => a = next_a,
ControlFlow::Break(b) => return Self::pure(b),
};
}
}
fn iterate_argument<'a, A: 'a, B: 'a>(
mut a: A,
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
) -> Self::F<'a, B>
where
Self: 'a,
{
loop {
match f.next(a)? {
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
ControlFlow::Break(b) => return Self::pure(b),
}
}
}
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
where
Self: 'a,

View File

@ -110,28 +110,6 @@ impl<F, O> OverloadIterative<F, O> {
}
}
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>
{
@ -161,26 +139,6 @@ impl<T: Monad, O: DeriveMonad> Monad for OverloadClass<T, O> {
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,

View File

@ -115,36 +115,6 @@ impl<E> Monad for ResultClass<E> {
f(fa?)
}
fn iterate_mut<'a, A: 'a, B: 'a>(
mut a: A,
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
) -> Self::F<'a, B>
where
Self: 'a,
{
loop {
match f(a)? {
ControlFlow::Continue(next_a) => a = next_a,
ControlFlow::Break(b) => return Self::pure(b),
};
}
}
fn iterate_argument<'a, A: 'a, B: 'a>(
mut a: A,
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
) -> Self::F<'a, B>
where
Self: 'a,
{
loop {
match f.next(a)? {
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
ControlFlow::Break(b) => return Self::pure(b),
}
}
}
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
where
Self: 'a,

View File

@ -82,36 +82,6 @@ impl Monad for SoloClass {
f(fa)
}
fn iterate_mut<'a, A: 'a, B: 'a>(
mut a: A,
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
) -> Self::F<'a, B>
where
Self: 'a,
{
loop {
match f(a) {
ControlFlow::Continue(next_a) => a = next_a,
ControlFlow::Break(b) => return b,
};
}
}
fn iterate_argument<'a, A: 'a, B: 'a>(
mut a: A,
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
) -> Self::F<'a, B>
where
Self: 'a,
{
loop {
match f.next(a) {
ControlFlow::Continue((new_a, new_f)) => (a, f) = (new_a, new_f),
ControlFlow::Break(b) => return b,
}
}
}
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
where
Self: 'a,

View File

@ -226,36 +226,6 @@ impl Monad for StacklessClass {
fa.bind(f)
}
fn iterate_mut<'a, A: 'a, B: 'a>(
a: A,
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
) -> Self::F<'a, B>
where
Self: 'a,
{
Self::pure(a).bind(move |a| {
f(a).bind(|state| match state {
ControlFlow::Continue(next_a) => Self::iterate_mut(next_a, f),
ControlFlow::Break(b) => Self::pure(b),
})
})
}
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,
{
Self::pure(a).bind(move |a| {
f.next(a).bind(|state| match state {
ControlFlow::Continue((next_a, next_f)) => Self::iterate_argument(next_a, next_f),
ControlFlow::Break(b) => Self::pure(b),
})
})
}
fn iterate<'a, B: 'a>(f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
where
Self: 'a,

View File

@ -129,40 +129,6 @@ impl<E> Monad for TryFutureClass<E> {
Box::pin(async { f(fa.await?).await })
}
fn iterate_mut<'a, A: 'a, B: 'a>(
mut a: A,
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
) -> Self::F<'a, B>
where
Self: 'a,
{
Box::pin(async move {
loop {
match f(a).await? {
ControlFlow::Continue(next_a) => a = next_a,
ControlFlow::Break(b) => return Ok(b),
};
}
})
}
fn iterate_argument<'a, A: 'a, B: 'a>(
mut a: A,
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
) -> Self::F<'a, B>
where
Self: 'a,
{
Box::pin(async move {
loop {
match f.next(a).await? {
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
ControlFlow::Break(b) => return Ok(b),
}
}
})
}
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
where
Self: 'a,

View File

@ -31,27 +31,7 @@ impl<C> Pure for ControlFlowClass<C> {
}
}
/// Next [AIterative] state, wrapped.
pub type AIterativeWrapped<'a, F> = <<F as AIterative<'a>>::T as WeakFunctor>::F<
'a,
ControlFlow<<F as AIterative<'a>>::B, (<F as AIterative<'a>>::A, F)>,
>;
/// Value passed to [`Monad::iterate_argument`].
/// `A` prefix meaning "with an **a**rgument" given current implementation
/// relying on the argument getting passed around.
pub trait AIterative<'a>: 'a + Sized {
/// [`ControlFlow::Continue`].
type A: 'a;
/// [`ControlFlow::Break`].
type B: 'a;
/// Corresponding [`WeakFunctor`].
type T: 'a + ?Sized + WeakFunctor;
/// Get next state.
fn next(self, a: Self::A) -> AIterativeWrapped<'a, Self>;
}
pub struct BindableMut<T: ?Sized, A, B, F>(F, PhantomData<A>, PhantomData<B>, PhantomData<T>);
pub struct BindableMut<T: ?Sized, A, B, F>(A, F, PhantomData<B>, PhantomData<T>);
impl<
'a,
@ -61,8 +41,8 @@ impl<
F: 'a + FnMut(A) -> T::F<'a, ControlFlow<B, A>>,
> BindableMut<T, A, B, F>
{
pub fn new(f: F) -> Self {
BindableMut(f, PhantomData, PhantomData, PhantomData)
pub fn new(a: A, f: F) -> Self {
BindableMut(a, f, PhantomData, PhantomData)
}
}
@ -72,20 +52,21 @@ impl<
A: 'a,
B: 'a,
F: 'a + FnMut(A) -> T::F<'a, ControlFlow<B, A>>,
> AIterative<'a> for BindableMut<T, A, B, F>
> Iterative<'a> for BindableMut<T, A, B, F>
{
type A = A;
type B = B;
type T = T;
fn next(mut self, a: Self::A) -> AIterativeWrapped<'a, Self> {
let fa = self.0(a);
fn next(mut self) -> IterativeWrapped<'a, Self> {
let fstate = self.1(self.0);
T::fmap(
move |state| match state {
ControlFlow::Continue(next_a) => ControlFlow::Continue((next_a, self)),
ControlFlow::Continue(next_a) => {
ControlFlow::Continue(BindableMut::new(next_a, self.1))
}
ControlFlow::Break(b) => ControlFlow::Break(b),
},
fa,
fstate,
)
}
}
@ -103,26 +84,3 @@ pub trait Iterative<'a>: 'a + Sized {
/// Get next state.
fn next(self) -> IterativeWrapped<'a, Self>;
}
pub struct ArgumentedIterative<A, F>(pub A, pub F);
impl<'a, A: 'a, F: AIterative<'a, A = A>> Iterative<'a> for ArgumentedIterative<A, F>
where
F::T: Functor,
{
type B = F::B;
type T = F::T;
fn next(self) -> IterativeWrapped<'a, Self> {
Self::T::fmap(
|state| match state {
ControlFlow::Continue((next_a, next_f)) => {
ControlFlow::Continue(ArgumentedIterative(next_a, next_f))
}
ControlFlow::Break(b) => ControlFlow::Break(b),
},
self.1.next(self.0),
)
}
}