1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! 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 ApplicativeSelect<'_> 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::ToEq<'a> for T {
        type Eq<A: 'a + Send + PartialEq + std::fmt::Debug> = A;

        fn to_eq<A: 'a + Send + PartialEq + std::fmt::Debug>(fa: Self::F<A>) -> Self::Eq<A> {
            fa()
        }
    }

    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();
    }
}