parent
38f6aef9d0
commit
4fe248f77b
27
src/func.rs
27
src/func.rs
@ -21,8 +21,8 @@ pub use std::ops::ControlFlow;
|
|||||||
|
|
||||||
pub use radn_derive::{CovariantFunctor, SharedFunctor};
|
pub use radn_derive::{CovariantFunctor, SharedFunctor};
|
||||||
|
|
||||||
pub use self::controlflow::{AIterative, AIterativeWrapped};
|
pub use self::controlflow::{AIterative, AIterativeWrapped, Iterative, IterativeWrapped};
|
||||||
use self::controlflow::{BindableMut, ControlFlowClass};
|
use self::controlflow::{ArgumentedIterative, BindableMut, ControlFlowClass};
|
||||||
|
|
||||||
/// Part of Haskell's `Functor f` responsible for having `f a`.
|
/// Part of Haskell's `Functor f` responsible for having `f a`.
|
||||||
///
|
///
|
||||||
@ -173,31 +173,40 @@ pub trait Monad: Applicative {
|
|||||||
where
|
where
|
||||||
Self: 'a;
|
Self: 'a;
|
||||||
|
|
||||||
/// Simplified, [`FnMut`] version of [`Monad::iibind`].
|
/// Simplified, [`FnMut`] version of [`Monad::iterate_argument`].
|
||||||
/// Reasoning for this method existing at all is that
|
/// Reasoning for this method existing at all is that
|
||||||
/// most usecases are better modelled
|
/// most usecases are better modelled with [`FnMut`]
|
||||||
|
/// rather than some dedicated state type.
|
||||||
///
|
///
|
||||||
/// Note: hypothetically, you can implement [`Monad::iibind`] from [`Monad::ibind`],
|
/// Note: hypothetically, you can implement [`Monad::iterate_argument`] from [`Monad::iterate_mut`],
|
||||||
/// but it's highly discouraged.
|
/// but it's highly discouraged.
|
||||||
fn ibind<'a, A: 'a, B: 'a>(
|
fn iterate_mut<'a, A: 'a, B: 'a>(
|
||||||
a: A,
|
a: A,
|
||||||
f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
where
|
where
|
||||||
Self: 'a,
|
Self: 'a,
|
||||||
{
|
{
|
||||||
Self::iibind(a, BindableMut::new(f))
|
Self::iterate_argument(a, BindableMut::new(f))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Kleisli category composition special case used to represent loops.
|
/// Kleisli category composition special case used to represent loops.
|
||||||
/// Included for optimisation and clarity.
|
/// Included for optimisation and clarity.
|
||||||
/// Generally, [`Monad::bind`] should be enough implement it.
|
/// Generally, [`Monad::bind`] should be enough implement it.
|
||||||
/// See [`classes::stackless::StacklessClass::ibind`] for a generic, though less-than ideal, blanket implementation.
|
/// 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.
|
/// On practice, you generally shouldn't be using [`Monad::bind`]/[`Pure::pure`]/[`Functor::fmap`] here.
|
||||||
fn iibind<'a, A: 'a, B: 'a>(
|
fn iterate_argument<'a, A: 'a, B: 'a>(
|
||||||
a: A,
|
a: A,
|
||||||
f: impl AIterative<'a, T = Self, A = A, B = B>,
|
f: impl AIterative<'a, T = Self, A = A, B = B>,
|
||||||
) -> Self::F<'a, 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
|
where
|
||||||
Self: 'a;
|
Self: 'a;
|
||||||
|
|
||||||
|
@ -99,27 +99,34 @@ 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))))
|
U::bind(fa, |ua| U::fmap(V::join, V::stuff::<_, U>(V::fmap(f, ua))))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ibind<'a, A: 'a, B: 'a>(
|
fn iterate_mut<'a, A: 'a, B: 'a>(
|
||||||
a: A,
|
a: A,
|
||||||
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
where
|
where
|
||||||
Self: 'a,
|
Self: 'a,
|
||||||
{
|
{
|
||||||
U::ibind(a, move |a| {
|
U::iterate_mut(a, move |a| {
|
||||||
let fstate = f(a);
|
let fstate = f(a);
|
||||||
U::fmap(|ustate| V::unstuff(ustate), fstate)
|
U::fmap(|ustate| V::unstuff(ustate), fstate)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iibind<'a, A: 'a, B: 'a>(
|
fn iterate_argument<'a, A: 'a, B: 'a>(
|
||||||
a: A,
|
a: A,
|
||||||
f: impl AIterative<'a, T = Self, A = A, B = B>,
|
f: impl AIterative<'a, T = Self, A = A, B = B>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
where
|
where
|
||||||
Self: 'a,
|
Self: 'a,
|
||||||
{
|
{
|
||||||
U::iibind(a, ComposedBindable(f))
|
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>
|
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A>
|
||||||
@ -131,25 +138,25 @@ impl<U: Monad, V: Monad + LocalFunctor> Monad for CompositionClass<U, V> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ComposedBindable<F>(F);
|
struct AComposedIterative<F>(F);
|
||||||
|
|
||||||
impl<
|
impl<
|
||||||
'a,
|
'a,
|
||||||
U: 'a + Monad,
|
U: 'a + Monad,
|
||||||
V: 'a + Monad + LocalFunctor,
|
V: 'a + Monad + LocalFunctor,
|
||||||
F: AIterative<'a, T = CompositionClass<U, V>>,
|
F: AIterative<'a, T = CompositionClass<U, V>>,
|
||||||
> AIterative<'a> for ComposedBindable<F>
|
> AIterative<'a> for AComposedIterative<F>
|
||||||
{
|
{
|
||||||
type A = F::A;
|
type A = F::A;
|
||||||
type B = <V as WeakFunctor>::F<'a, F::B>;
|
type B = <V as WeakFunctor>::F<'a, F::B>;
|
||||||
type T = U;
|
type T = U;
|
||||||
|
|
||||||
fn iterate(self, a: Self::A) -> AIterativeWrapped<'a, Self> {
|
fn next(self, a: Self::A) -> AIterativeWrapped<'a, Self> {
|
||||||
let fstate = self.0.iterate(a);
|
let fstate = self.0.next(a);
|
||||||
U::fmap(
|
U::fmap(
|
||||||
|ustate| match V::unstuff(ustate) {
|
|ustate| match V::unstuff(ustate) {
|
||||||
ControlFlow::Continue((next_a, next_f)) => {
|
ControlFlow::Continue((next_a, next_f)) => {
|
||||||
ControlFlow::Continue((next_a, ComposedBindable(next_f)))
|
ControlFlow::Continue((next_a, Self(next_f)))
|
||||||
}
|
}
|
||||||
ControlFlow::Break(b) => ControlFlow::Break(b),
|
ControlFlow::Break(b) => ControlFlow::Break(b),
|
||||||
},
|
},
|
||||||
@ -158,6 +165,30 @@ impl<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ComposedIterative<F>(F);
|
||||||
|
|
||||||
|
impl<
|
||||||
|
'a,
|
||||||
|
U: 'a + Monad,
|
||||||
|
V: 'a + Monad + LocalFunctor,
|
||||||
|
F: Iterative<'a, T = CompositionClass<U, V>>,
|
||||||
|
> Iterative<'a> for ComposedIterative<F>
|
||||||
|
{
|
||||||
|
type B = <V as WeakFunctor>::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<E, U: Monad, V: MonadFail<E> + LocalFunctor> MonadFail<E> for CompositionClass<U, V> {
|
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>
|
fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A>
|
||||||
where
|
where
|
||||||
|
@ -87,7 +87,7 @@ impl Monad for FutureClass {
|
|||||||
Box::pin(async { f(fa.await).await })
|
Box::pin(async { f(fa.await).await })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ibind<'a, A: 'a, B: 'a>(
|
fn iterate_mut<'a, A: 'a, B: 'a>(
|
||||||
mut a: A,
|
mut a: A,
|
||||||
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
@ -104,7 +104,7 @@ impl Monad for FutureClass {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iibind<'a, A: 'a, B: 'a>(
|
fn iterate_argument<'a, A: 'a, B: 'a>(
|
||||||
mut a: A,
|
mut a: A,
|
||||||
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
@ -113,7 +113,7 @@ impl Monad for FutureClass {
|
|||||||
{
|
{
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
loop {
|
loop {
|
||||||
match f.iterate(a).await {
|
match f.next(a).await {
|
||||||
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
||||||
ControlFlow::Break(b) => return b,
|
ControlFlow::Break(b) => return b,
|
||||||
}
|
}
|
||||||
@ -121,6 +121,20 @@ impl Monad for FutureClass {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
||||||
|
where
|
||||||
|
Self: 'a,
|
||||||
|
{
|
||||||
|
Box::pin(async move {
|
||||||
|
loop {
|
||||||
|
match f.next().await {
|
||||||
|
ControlFlow::Continue(next_f) => f = next_f,
|
||||||
|
ControlFlow::Break(b) => return b,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> {
|
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> {
|
||||||
Box::pin(async { ffa.await.await })
|
Box::pin(async { ffa.await.await })
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,7 @@ impl Monad for LazyClass {
|
|||||||
Box::new(|| f(fa())())
|
Box::new(|| f(fa())())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ibind<'a, A: 'a, B: 'a>(
|
fn iterate_mut<'a, A: 'a, B: 'a>(
|
||||||
mut a: A,
|
mut a: A,
|
||||||
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
@ -103,7 +103,7 @@ impl Monad for LazyClass {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iibind<'a, A: 'a, B: 'a>(
|
fn iterate_argument<'a, A: 'a, B: 'a>(
|
||||||
mut a: A,
|
mut a: A,
|
||||||
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
@ -111,13 +111,25 @@ impl Monad for LazyClass {
|
|||||||
Self: 'a,
|
Self: 'a,
|
||||||
{
|
{
|
||||||
loop {
|
loop {
|
||||||
match f.iterate(a)() {
|
match f.next(a)() {
|
||||||
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
||||||
ControlFlow::Break(b) => return Self::pure(b),
|
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,
|
||||||
|
{
|
||||||
|
loop {
|
||||||
|
match f.next()() {
|
||||||
|
ControlFlow::Continue(next_f) => f = next_f,
|
||||||
|
ControlFlow::Break(b) => return Self::pure(b),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> {
|
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> {
|
||||||
Box::new(|| ffa()())
|
Box::new(|| ffa()())
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,7 @@ impl Monad for OptionClass {
|
|||||||
f(fa?)
|
f(fa?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ibind<'a, A: 'a, B: 'a>(
|
fn iterate_mut<'a, A: 'a, B: 'a>(
|
||||||
mut a: A,
|
mut a: A,
|
||||||
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
@ -107,7 +107,7 @@ impl Monad for OptionClass {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iibind<'a, A: 'a, B: 'a>(
|
fn iterate_argument<'a, A: 'a, B: 'a>(
|
||||||
mut a: A,
|
mut a: A,
|
||||||
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
@ -115,13 +115,25 @@ impl Monad for OptionClass {
|
|||||||
Self: 'a,
|
Self: 'a,
|
||||||
{
|
{
|
||||||
loop {
|
loop {
|
||||||
match f.iterate(a)? {
|
match f.next(a)? {
|
||||||
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
||||||
ControlFlow::Break(b) => return Self::pure(b),
|
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,
|
||||||
|
{
|
||||||
|
loop {
|
||||||
|
match f.next()? {
|
||||||
|
ControlFlow::Continue(next_f) => f = next_f,
|
||||||
|
ControlFlow::Break(b) => return Self::pure(b),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> {
|
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> {
|
||||||
ffa?
|
ffa?
|
||||||
}
|
}
|
||||||
|
@ -113,7 +113,7 @@ impl<E> Monad for ResultClass<E> {
|
|||||||
f(fa?)
|
f(fa?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ibind<'a, A: 'a, B: 'a>(
|
fn iterate_mut<'a, A: 'a, B: 'a>(
|
||||||
mut a: A,
|
mut a: A,
|
||||||
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
@ -128,7 +128,7 @@ impl<E> Monad for ResultClass<E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iibind<'a, A: 'a, B: 'a>(
|
fn iterate_argument<'a, A: 'a, B: 'a>(
|
||||||
mut a: A,
|
mut a: A,
|
||||||
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
@ -136,13 +136,25 @@ impl<E> Monad for ResultClass<E> {
|
|||||||
Self: 'a,
|
Self: 'a,
|
||||||
{
|
{
|
||||||
loop {
|
loop {
|
||||||
match f.iterate(a)? {
|
match f.next(a)? {
|
||||||
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
||||||
ControlFlow::Break(b) => return Self::pure(b),
|
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,
|
||||||
|
{
|
||||||
|
loop {
|
||||||
|
match f.next()? {
|
||||||
|
ControlFlow::Continue(next_f) => f = next_f,
|
||||||
|
ControlFlow::Break(b) => return Self::pure(b),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A>
|
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A>
|
||||||
where
|
where
|
||||||
Self: 'a,
|
Self: 'a,
|
||||||
|
@ -80,7 +80,7 @@ impl Monad for SoloClass {
|
|||||||
f(fa)
|
f(fa)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ibind<'a, A: 'a, B: 'a>(
|
fn iterate_mut<'a, A: 'a, B: 'a>(
|
||||||
mut a: A,
|
mut a: A,
|
||||||
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
@ -95,7 +95,7 @@ impl Monad for SoloClass {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iibind<'a, A: 'a, B: 'a>(
|
fn iterate_argument<'a, A: 'a, B: 'a>(
|
||||||
mut a: A,
|
mut a: A,
|
||||||
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
@ -103,13 +103,25 @@ impl Monad for SoloClass {
|
|||||||
Self: 'a,
|
Self: 'a,
|
||||||
{
|
{
|
||||||
loop {
|
loop {
|
||||||
match f.iterate(a) {
|
match f.next(a) {
|
||||||
ControlFlow::Continue((new_a, new_f)) => (a, f) = (new_a, new_f),
|
ControlFlow::Continue((new_a, new_f)) => (a, f) = (new_a, new_f),
|
||||||
ControlFlow::Break(b) => return b,
|
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,
|
||||||
|
{
|
||||||
|
loop {
|
||||||
|
match f.next() {
|
||||||
|
ControlFlow::Continue(next_f) => f = next_f,
|
||||||
|
ControlFlow::Break(b) => return b,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> {
|
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> {
|
||||||
ffa
|
ffa
|
||||||
}
|
}
|
||||||
|
@ -224,7 +224,7 @@ impl Monad for StacklessClass {
|
|||||||
fa.bind(f)
|
fa.bind(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ibind<'a, A: 'a, B: 'a>(
|
fn iterate_mut<'a, A: 'a, B: 'a>(
|
||||||
a: A,
|
a: A,
|
||||||
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
@ -233,13 +233,13 @@ impl Monad for StacklessClass {
|
|||||||
{
|
{
|
||||||
Self::pure(a).bind(move |a| {
|
Self::pure(a).bind(move |a| {
|
||||||
f(a).bind(|state| match state {
|
f(a).bind(|state| match state {
|
||||||
ControlFlow::Continue(next_a) => Self::ibind(next_a, f),
|
ControlFlow::Continue(next_a) => Self::iterate_mut(next_a, f),
|
||||||
ControlFlow::Break(b) => Self::pure(b),
|
ControlFlow::Break(b) => Self::pure(b),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iibind<'a, A: 'a, B: 'a>(
|
fn iterate_argument<'a, A: 'a, B: 'a>(
|
||||||
a: A,
|
a: A,
|
||||||
f: impl AIterative<'a, T = Self, A = A, B = B>,
|
f: impl AIterative<'a, T = Self, A = A, B = B>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
@ -247,8 +247,20 @@ impl Monad for StacklessClass {
|
|||||||
Self: 'a,
|
Self: 'a,
|
||||||
{
|
{
|
||||||
Self::pure(a).bind(move |a| {
|
Self::pure(a).bind(move |a| {
|
||||||
f.iterate(a).bind(|state| match state {
|
f.next(a).bind(|state| match state {
|
||||||
ControlFlow::Continue((next_a, next_f)) => Self::iibind(next_a, next_f),
|
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,
|
||||||
|
{
|
||||||
|
Self::pure(()).bind(move |_| {
|
||||||
|
f.next().bind(|state| match state {
|
||||||
|
ControlFlow::Continue(next_f) => Self::iterate(next_f),
|
||||||
ControlFlow::Break(b) => Self::pure(b),
|
ControlFlow::Break(b) => Self::pure(b),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -32,12 +32,12 @@ impl<C> Pure for ControlFlowClass<C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Next [AIterative] state, wrapped.
|
/// Next [AIterative] state, wrapped.
|
||||||
pub type AIterativeWrapped<'a, G> = <<G as AIterative<'a>>::T as WeakFunctor>::F<
|
pub type AIterativeWrapped<'a, I> = <<I as AIterative<'a>>::T as WeakFunctor>::F<
|
||||||
'a,
|
'a,
|
||||||
ControlFlow<<G as AIterative<'a>>::B, (<G as AIterative<'a>>::A, G)>,
|
ControlFlow<<I as AIterative<'a>>::B, (<I as AIterative<'a>>::A, I)>,
|
||||||
>;
|
>;
|
||||||
|
|
||||||
/// Value passed to [`Monad::iibind`].
|
/// Value passed to [`Monad::iterate_argument`].
|
||||||
/// `A` prefix meaning "with an **a**rgument" given current implementation
|
/// `A` prefix meaning "with an **a**rgument" given current implementation
|
||||||
/// relying on the argument getting passed around.
|
/// relying on the argument getting passed around.
|
||||||
pub trait AIterative<'a>: 'a + Sized {
|
pub trait AIterative<'a>: 'a + Sized {
|
||||||
@ -48,7 +48,7 @@ pub trait AIterative<'a>: 'a + Sized {
|
|||||||
/// Corresponding [`WeakFunctor`].
|
/// Corresponding [`WeakFunctor`].
|
||||||
type T: 'a + ?Sized + WeakFunctor;
|
type T: 'a + ?Sized + WeakFunctor;
|
||||||
/// Get next state.
|
/// Get next state.
|
||||||
fn iterate(self, a: Self::A) -> AIterativeWrapped<'a, Self>;
|
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>(F, PhantomData<A>, PhantomData<B>, PhantomData<T>);
|
||||||
@ -78,7 +78,7 @@ impl<
|
|||||||
type B = B;
|
type B = B;
|
||||||
type T = T;
|
type T = T;
|
||||||
|
|
||||||
fn iterate(mut self, a: Self::A) -> AIterativeWrapped<'a, Self> {
|
fn next(mut self, a: Self::A) -> AIterativeWrapped<'a, Self> {
|
||||||
let fa = self.0(a);
|
let fa = self.0(a);
|
||||||
T::fmap(
|
T::fmap(
|
||||||
move |state| match state {
|
move |state| match state {
|
||||||
@ -89,3 +89,40 @@ impl<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Next [Iterative] state, wrapped.
|
||||||
|
pub type IterativeWrapped<'a, I> =
|
||||||
|
<<I as Iterative<'a>>::T as WeakFunctor>::F<'a, ControlFlow<<I as Iterative<'a>>::B, I>>;
|
||||||
|
|
||||||
|
/// Value passed to [`Monad::iterate`].
|
||||||
|
pub trait Iterative<'a>: 'a + Sized {
|
||||||
|
/// [`ControlFlow::Break`].
|
||||||
|
type B: 'a;
|
||||||
|
/// Corresponding [`WeakFunctor`].
|
||||||
|
type T: 'a + ?Sized + WeakFunctor;
|
||||||
|
/// 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),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -157,7 +157,7 @@ impl<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>> ExtStack<'a, Ctx, A> for St
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
fn vec(self) -> StackVecWrapped<'a, Ctx, A> {
|
fn vec(self) -> StackVecWrapped<'a, Ctx, A> {
|
||||||
Ctx::T::ibind((vec![], self), |(mut vec, stack)| match stack {
|
Ctx::T::iterate_mut((vec![], self), |(mut vec, stack)| match stack {
|
||||||
Nullable::Null(_) => Ctx::T::pure(ControlFlow::Break(Ok(vec))),
|
Nullable::Null(_) => Ctx::T::pure(ControlFlow::Break(Ok(vec))),
|
||||||
Nullable::NotNull(point) => Ctx::T::fmap(
|
Nullable::NotNull(point) => Ctx::T::fmap(
|
||||||
|resolved| {
|
|resolved| {
|
||||||
|
@ -164,7 +164,7 @@ impl Monad for TracedClass {
|
|||||||
f(fa.a).after(fa.t)
|
f(fa.a).after(fa.t)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ibind<'a, A: 'a, B: 'a>(
|
fn iterate_mut<'a, A: 'a, B: 'a>(
|
||||||
mut a: A,
|
mut a: A,
|
||||||
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
@ -182,7 +182,7 @@ impl Monad for TracedClass {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iibind<'a, A: 'a, B: 'a>(
|
fn iterate_argument<'a, A: 'a, B: 'a>(
|
||||||
mut a: A,
|
mut a: A,
|
||||||
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
@ -191,7 +191,7 @@ impl Monad for TracedClass {
|
|||||||
{
|
{
|
||||||
let mut t = TraceBox::pure();
|
let mut t = TraceBox::pure();
|
||||||
loop {
|
loop {
|
||||||
let fa = f.iterate(a);
|
let fa = f.next(a);
|
||||||
t = fa.t.after(t);
|
t = fa.t.after(t);
|
||||||
match fa.a {
|
match fa.a {
|
||||||
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
||||||
@ -200,6 +200,21 @@ impl Monad for TracedClass {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
||||||
|
where
|
||||||
|
Self: 'a,
|
||||||
|
{
|
||||||
|
let mut t = TraceBox::pure();
|
||||||
|
loop {
|
||||||
|
let fa = f.next();
|
||||||
|
t = fa.t.after(t);
|
||||||
|
match fa.a {
|
||||||
|
ControlFlow::Continue(next_f) => f = next_f,
|
||||||
|
ControlFlow::Break(b) => return b.with_trace(t),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A>
|
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A>
|
||||||
where
|
where
|
||||||
Self::F<'a, A>: 'a,
|
Self::F<'a, A>: 'a,
|
||||||
|
@ -146,7 +146,7 @@ impl Monad for CountedClass {
|
|||||||
f(fa.a).add(fa.n)
|
f(fa.a).add(fa.n)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ibind<'a, A: 'a, B: 'a>(
|
fn iterate_mut<'a, A: 'a, B: 'a>(
|
||||||
mut a: A,
|
mut a: A,
|
||||||
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
@ -164,7 +164,7 @@ impl Monad for CountedClass {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iibind<'a, A: 'a, B: 'a>(
|
fn iterate_argument<'a, A: 'a, B: 'a>(
|
||||||
mut a: A,
|
mut a: A,
|
||||||
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
||||||
) -> Self::F<'a, B>
|
) -> Self::F<'a, B>
|
||||||
@ -173,7 +173,7 @@ impl Monad for CountedClass {
|
|||||||
{
|
{
|
||||||
let mut n = 0;
|
let mut n = 0;
|
||||||
loop {
|
loop {
|
||||||
let fa = f.iterate(a);
|
let fa = f.next(a);
|
||||||
n += fa.n;
|
n += fa.n;
|
||||||
match fa.a {
|
match fa.a {
|
||||||
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
||||||
@ -182,6 +182,21 @@ impl Monad for CountedClass {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
||||||
|
where
|
||||||
|
Self: 'a,
|
||||||
|
{
|
||||||
|
let mut n = 0;
|
||||||
|
loop {
|
||||||
|
let fa = f.next();
|
||||||
|
n += fa.n;
|
||||||
|
match fa.a {
|
||||||
|
ControlFlow::Continue(next_f) => f = next_f,
|
||||||
|
ControlFlow::Break(b) => return b.with_count(n),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A>
|
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A>
|
||||||
where
|
where
|
||||||
Self::F<'a, A>: 'a,
|
Self::F<'a, A>: 'a,
|
||||||
|
Loading…
Reference in New Issue
Block a user