radn-rs/src/func/instances/lazy.rs
timofey 8ee0a196c6
Some checks failed
buildbot/cargo fmt (1.72) Build done.
buildbot/cargo clippy (1.65) Build done.
buildbot/cargo doc (1.72) Build done.
buildbot/cargo clippy (1.72) Build done.
buildbot/cargo test (1.65) Build done.
simplify WrapFunction
2023-10-15 17:11:43 +00:00

137 lines
3.5 KiB
Rust

//! Helper [Monad]s to defer execution until necessary.
//! Wrapped value is just a box pointing to the constructor function.
//!
//! Due to semantical laziness,
//! [`LazyInstance::replace`] and [`LazyInstance::discard_first`]/[`LazyInstance::discard_second`]
//! actually fully cancel the "unnecessary" computation.
//!
//! For stackless execution see [`stackless`].
//!
//! [`stackless`]: super::stackless
use crate::func::class_prelude::*;
pub struct LazyInstance;
impl WeakFunctorAny for LazyInstance {
type FAny<'a, A: 'a + Send> = Box<dyn 'a + Send + FnOnce() -> A>;
}
impl<'a> Functor<'a> for LazyInstance {
fn fmap<A: 'a + Send, B: 'a + Send>(
fa: Self::F<A>,
f: impl 'a + Send + FnOnce(A) -> B,
) -> Self::F<B> {
Box::new(|| f(fa()))
}
fn replace<A: 'a + Send, B: 'a + Send>(fa: Self::F<A>, b: B) -> Self::F<B> {
drop(fa);
Box::new(|| b)
}
fn void<A: 'a + Send>(fa: Self::F<A>) -> Self::F<()> {
drop(fa);
Box::new(|| ())
}
}
impl<'a> Pure<'a> for LazyInstance {
fn pure<A: 'a + Send>(a: A) -> Self::F<A> {
Box::new(|| a)
}
}
impl<'a> ApplicativeSeq<'a> for LazyInstance {
fn seq<A: 'a + Send, B: 'a + Send>(
ff: Self::F<impl 'a + Send + FnOnce(A) -> B>,
fa: Self::F<A>,
) -> Self::F<B> {
Box::new(|| ff()(fa()))
}
}
impl<'a> ApplicativeLA2<'a> for LazyInstance {
fn la2<A: 'a + Send, B: 'a + Send, C: 'a + Send>(
fa: Self::F<A>,
fb: Self::F<B>,
f: impl 'a + Send + FnOnce(A, B) -> C,
) -> Self::F<C> {
Box::new(|| f(fa(), fb()))
}
}
impl<'a> ApplicativeTuple<'a> for LazyInstance {
fn tuple<A: 'a + Send, B: 'a + Send>((fa, fb): (Self::F<A>, Self::F<B>)) -> Self::F<(A, B)> {
Box::new(|| (fa(), fb()))
}
}
impl<'a> ApplicativeSelect<'a> for LazyInstance {}
impl<'a> Applicative<'a> for LazyInstance {
fn discard_first<A: 'a + Send, B: 'a + Send>(fa: Self::F<A>, fb: Self::F<B>) -> Self::F<B> {
drop(fa);
fb
}
fn discard_second<A: 'a + Send, B: 'a + Send>(fa: Self::F<A>, fb: Self::F<B>) -> Self::F<A> {
drop(fb);
fa
}
}
impl<'a> Monad<'a> for LazyInstance {
fn bind<A: 'a + Send, B: 'a + Send>(
fa: Self::F<A>,
f: impl 'a + Send + FnOnce(A) -> Self::F<B>,
) -> Self::F<B> {
Box::new(|| f(fa())())
}
fn iterate<B: 'a + Send>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<B> {
loop {
match f.next()() {
ControlFlow::Continue(next_f) => f = next_f,
ControlFlow::Break(b) => return Self::pure(b),
}
}
}
fn join<A: 'a + Send>(ffa: Self::F<Self::F<A>>) -> Self::F<A> {
Box::new(|| ffa()())
}
}
#[cfg(test)]
mod lazy_tests {
use std::sync::Arc;
use super::{test_suite, tests, LazyInstance};
type T = LazyInstance;
impl<'a> tests::Eqr<'a> for T {
fn eqr<A: 'a + Send + PartialEq + std::fmt::Debug>(
name: &'a str,
left: Self::F<A>,
right: Self::F<A>,
) -> tests::R {
tests::eqr(name, left(), right())
}
}
impl<'a> test_suite::FunctorTestSuite<'a> for T {
fn sample<A: 'a + Send, F: FnMut(Arc<dyn test_suite::WrapFunction<'a, A, Self::F<A>>>)>(
mut f: F,
) {
f(Arc::new(|a| Box::new(|| a)));
}
}
#[test]
fn monad_follows_laws() {
test_suite::monad_follows_laws::<T>().unwrap();
}
}