//! Generic functional concepts implemented in Rust. //! Some choices are intentionally less generic due to specifics of the domain. //! That, for example, includes the almost exclusive focus on [`FnOnce`] category. //! //! Sources: //! * //! * //! * use self::applicative_select::ApplicativeSelect; use self::controlflow::{ControlFlow, Iterative}; pub use self::extensions::MonadExt; pub mod applicative_select; pub mod class_prelude; pub mod context; pub mod controlflow; pub mod derivations; pub mod extensions; pub mod fail; pub mod instances; pub mod local; pub mod shared; #[cfg(test)] pub mod test_suite; #[cfg(test)] pub mod tests; pub mod weakfunctorany; /// Part of Haskell's `Functor f` responsible for having `f a`. /// /// pub trait WeakFunctor<'a>: 'a + Send { type F: 'a + Send; } pub type Wrap<'a, A, T> = >::F; /// Rust-specific implementation of [`Functor`], respecting `move` semantics. /// /// pub trait Functor<'a>: WeakFunctor<'a> { /// Equivalent or Haskell's `fmap`. /// Due to Rust limitations, it's not a `function->function` conversion. /// For that see [`derivations::fmap`]. fn fmap( fa: Self::F, f: impl 'a + Send + FnOnce(A) -> B, ) -> Self::F; /// Equivalent of Haskell's `$>`/`<$`. fn replace(fa: Self::F, b: B) -> Self::F { Self::fmap(fa, |_| b) } /// Equivalent of Haskell's `void`. fn void(fa: Self::F) -> Self::F<()> { Self::replace(fa, ()) } } /// Part of [`Applicative`] responsible for Haskell's value lifting, `pure`. pub trait Pure<'a>: Functor<'a> { /// Equivalent of Haskell's `pure`/`return`. fn pure(a: A) -> Self::F; } /// Part of [`Applicative`] responsible for Haskell's sequential application `<*>`. pub trait ApplicativeSeq<'a>: Functor<'a> { /// Equivalent of Haskell's `<*>`. fn seq( ff: Self::F B>, fa: Self::F, ) -> Self::F; } /// Part of [`Applicative`] responsible for Haskell's result combination `listA2`. pub trait ApplicativeLA2<'a>: Functor<'a> { /// Equivalent of Haskell's `listA2`. fn la2( fa: Self::F, fb: Self::F, f: impl 'a + Send + FnOnce(A, B) -> C, ) -> Self::F; } /// Part of [`Applicative`] responsible for Rust-style result combination, specifically for tuples. pub trait ApplicativeTuple<'a>: Functor<'a> { /// Similar to Haskell's `listA2` but with [Iterator::collect]-ish semantics. fn tuple(fab: (Self::F, Self::F)) -> Self::F<(A, B)>; } /// Equivalent of Haskell's `Applicative`. /// Split into [`Pure`], [`ApplicativeSeq`], [`ApplicativeLA2`] and [`ApplicativeTuple`] due to Rust limitations. /// /// pub trait Applicative<'a>: Pure<'a> + ApplicativeSeq<'a> + ApplicativeLA2<'a> + ApplicativeTuple<'a> + ApplicativeSelect<'a> { /// Equivalent of Haskell's `*>`/`>>`. fn discard_first(fa: Self::F, fb: Self::F) -> Self::F { Self::seq(Self::replace(fa, |b| b), fb) } /// Equivalent of Haskell's `<*`. fn discard_second(fa: Self::F, fb: Self::F) -> Self::F { Self::la2(fa, fb, |a, _| a) } } /// Equivalent of Haskell's `Monad`. /// /// pub trait Monad<'a>: Applicative<'a> { /// Equivalent of Haskell's `>==`. /// Due to Rust limitations, it's not a `function->function` conversion. /// For that see [`derivations::bind`]. fn bind( fa: Self::F, f: impl 'a + Send + FnOnce(A) -> Self::F, ) -> Self::F; /// Included for optimisation and clarity. /// Generally, [`Monad::bind`] should be enough implement it. /// See [`StacklessInstance::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. /// /// [`StacklessInstance::iterate`]: instances::stackless::StacklessInstance::iterate fn iterate(f: impl Iterative<'a, T = Self, B = B>) -> Self::F; /// Equivalent of Haskell's `join`. fn join(ffa: Self::F>) -> Self::F { Self::bind(ffa, |fa| fa) } }