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
135
136
137
138
//! The higher-kinded trait of a `Monad`.

use super::applicative::Applicative;
use super::*;
use super::types::*;

pub trait Monad: Applicative {
    fn bind<A, B, F: Fn(A) -> K1<Self, B>>(a: K1<Self, A>, f: F) -> K1<Self, B>;
}

impl Monad for OptionC {
    fn bind<A, B, G: Fn(A) -> K1<Self, B>>(a: K1<Self, A>, g: G) -> K1<Self, B> {
        match a.into_inner() {
            Some(x) => g(x),
            None => OptionC::new(None),
        }
    }
}

impl Monad for VecC {
    fn bind<A, B, G: Fn(A) -> K1<Self, B>>(a: K1<Self, A>, g: G) -> K1<Self, B> {
        Self::new(
            a.into_inner()
                .into_iter()
                .flat_map(move |x| g(x).into_inner())
                .collect()
        )
    }
}

#[cfg(test)]
mod test {
    use super::*;

    fn check_law_left_identity<M: Monad, A, B, F>(a: A, f: F)
    where
        M: Kind1<B>,
        A: Clone,
        F: Fn(A) -> K1<M, B> + Clone,
        K1<M, B>: PartialEq + Clone + core::fmt::Debug,
    {
        let return_bind = M::bind(M::pure(a.clone()), f.clone());
        let just_call = f(a);
        assert_eq!(return_bind, just_call);
    }

    fn check_law_right_identity<T: Clone, M: Monad>(m: K1<M, T>)
    where
        M: Kind1<T>,
        K1<M, T>: PartialEq + Clone + core::fmt::Debug,
    {
        let m_return = M::bind(m.clone(), M::pure);
        assert_eq!(m_return, m);
    }

    fn check_law_associativity<A, B, C, M: Monad, F, G>(m: K1<M, A>, f: F, g: G)
    where
        M: Kind1<A> + Kind1<C>,
        K1<M, A>: Clone,
        K1<M, C>: PartialEq + Clone + core::fmt::Debug,
        F: Fn(A) -> K1<M, B> + Clone,
        G: Fn(B) -> K1<M, C> + Clone,
    {
        let left_first = M::bind(M::bind(m.clone(), f.clone()), g.clone());
        let right_first = M::bind(m, move |x| M::bind(f(x), g.clone()));
        assert_eq!(left_first, right_first);
    }

    #[test]
    fn option_monad_law_left_identity() {
        check_law_left_identity(42, |x| OptionC::new(Some(x + 1)));
    }

    #[test]
    fn option_monad_law_right_identity() {
        check_law_right_identity::<i32, _>(OptionC::new(None));
        check_law_right_identity(OptionC::new(Some(42)));
    }

    #[test]
    fn option_monad_law_associativity() {
        check_law_associativity(
            OptionC::new(Some(42)),
            |x| OptionC::new(Some(x + 1)),
            |x| OptionC::new(Some(x * 2)),
        );
        check_law_associativity::<i32, _, _, _, _, _>(
            OptionC::new(None),
            |x| OptionC::new(Some(x + 1)),
            |x| OptionC::new(Some(x * 2)),
        );
        check_law_associativity::<_, i32, _, _, _, _>(
            OptionC::new(Some(42)),
            |_| OptionC::new(None),
            |x| OptionC::new(Some(x * 2)),
        );
        check_law_associativity::<_, _, i32, _, _, _>(
            OptionC::new(Some(42)),
            |x| OptionC::new(Some(x + 1)),
            |_| OptionC::new(None),
        );
    }

    #[test]
    fn vec_monad_law_left_identity() {
        check_law_left_identity(42, |x| VecC::new(vec![x + 1]));
    }

    #[test]
    fn vec_monad_law_right_identity() {
        check_law_right_identity::<i32, _>(VecC::new(vec![]));
        check_law_right_identity(VecC::new(vec![42]));
    }

    #[test]
    fn vec_monad_law_associativity() {
        check_law_associativity(
            VecC::new(vec![42, 0]),
            |x| VecC::new(vec![x + 1, x - 1]),
            |x| VecC::new(vec![x * 2, x]),
        );
        check_law_associativity::<i32, _, _, _, _, _>(
            VecC::new(vec![]),
            |x| VecC::new(vec![x + 1, x - 1]),
            |x| VecC::new(vec![x * 2, x]),
        );
        check_law_associativity::<_, i32, _, _, _, _>(
            VecC::new(vec![42, 0]),
            |_| VecC::new(vec![]),
            |x| VecC::new(vec![x * 2, x]),
        );
        check_law_associativity::<_, _, i32, _, _, _>(
            VecC::new(vec![42, 0]),
            |x| VecC::new(vec![x + 1, x - 1]),
            |_| VecC::new(vec![]),
        );
    }
}