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![]),
);
}
}