use std::convert::Infallible;
use crate::func::class_prelude::*;
#[derive(SharedFunctorAny)]
pub struct OptionInstance;
impl WeakFunctorAny for OptionInstance {
type FAny<'a, A: 'a + Send> = Option<A>;
}
impl<'a> Functor<'a> for OptionInstance {
fn fmap<A: 'a + Send, B: 'a + Send>(
fa: Self::F<A>,
f: impl 'a + Send + FnOnce(A) -> B,
) -> Self::F<B> {
fa.map(f)
}
fn replace<A: 'a + Send, B: 'a + Send>(fa: Self::F<A>, b: B) -> Self::F<B> {
fa?;
Self::pure(b)
}
fn void<A: 'a + Send>(fa: Self::F<A>) -> Self::F<()> {
fa?;
Self::pure(())
}
}
impl<'a> Pure<'a> for OptionInstance {
fn pure<A: 'a + Send>(a: A) -> Self::F<A> {
Some(a)
}
}
impl<'a> ApplicativeSeq<'a> for OptionInstance {
fn seq<A: 'a + Send, B: 'a + Send>(
ff: Self::F<impl 'a + Send + FnOnce(A) -> B>,
fa: Self::F<A>,
) -> Self::F<B> {
Self::pure(ff?(fa?))
}
}
impl<'a> ApplicativeLA2<'a> for OptionInstance {
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> {
Self::pure(f(fa?, fb?))
}
}
impl<'a> ApplicativeTuple<'a> for OptionInstance {
fn tuple<A: 'a + Send, B: 'a + Send>((fa, fb): (Self::F<A>, Self::F<B>)) -> Self::F<(A, B)> {
Self::pure((fa?, fb?))
}
}
impl ApplicativeSelect<'_> for OptionInstance {}
impl<'a> Applicative<'a> for OptionInstance {
fn discard_first<A: 'a + Send, B: 'a + Send>(fa: Self::F<A>, fb: Self::F<B>) -> Self::F<B> {
fa?;
fb
}
fn discard_second<A: 'a + Send, B: 'a + Send>(fa: Self::F<A>, fb: Self::F<B>) -> Self::F<A> {
let a = fa?;
fb.map(|_| a)
}
}
impl<'a> Monad<'a> for OptionInstance {
fn bind<A: 'a + Send, B: 'a + Send>(
fa: Self::F<A>,
f: impl 'a + FnOnce(A) -> Self::F<B>,
) -> Self::F<B> {
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> {
ffa?
}
}
impl<'a> LocalFunctor<'a> for OptionInstance {
fn unstuff<A: 'a + Send, B: 'a + Send>(
state: Self::F<ControlFlow<B, A>>,
) -> ControlFlow<Self::F<B>, A> {
match state {
Some(ControlFlow::Continue(a)) => ControlFlow::Continue(a),
Some(ControlFlow::Break(b)) => ControlFlow::Break(Some(b)),
None => ControlFlow::Break(None),
}
}
fn stuff<A: 'a + Send, T: Pure<'a>>(fa: Self::F<T::F<A>>) -> T::F<Self::F<A>> {
match fa {
Some(ua) => T::fmap(ua, Some),
None => T::pure(None),
}
}
}
impl<'a> Fail<'a, Option<Infallible>> for OptionInstance {
fn fail<A: 'a + Send>(e: Option<Infallible>) -> Self::F<A> {
match e {
Some(some) => match some {},
None => None,
}
}
}
#[cfg(test)]
mod option_tests {
use std::convert::Infallible;
use std::sync::Arc;
use super::{test_suite, tests, Functor};
use super::OptionInstance as T;
impl<'a> tests::ToEq<'a> for T {
type Eq<A: 'a + Send + PartialEq + std::fmt::Debug> = Self::F<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(|_| None));
f(Arc::new(|a| Some(a)));
}
}
impl<'a> test_suite::FailTestSuite<'a, Option<Infallible>> for T {
fn sample_fail<F: FnMut(Arc<dyn 'a + Send + Sync + Fn() -> Option<Infallible>>)>(mut f: F) {
f(Arc::new(|| None))
}
}
#[test]
fn fmap_f_none_is_none() {
assert_eq!(T::fmap(None, |_: ()| ()), None);
}
#[test]
fn fmap_f_some_a_is_some_f_a() {
assert_eq!(T::fmap(Some(2), |x| x * x), Some(4));
}
#[test]
fn replace_none_b_is_none() {
assert_eq!(T::replace(None::<i32>, 1), None);
assert_eq!(T::void(None::<i32>), None);
}
#[test]
fn replace_some_a_b_is_some_b() {
assert_eq!(T::replace(Some(1), 2), Some(2));
assert_eq!(T::void(Some(1)), Some(()));
}
#[test]
fn monad_follows_laws() {
test_suite::monad_follows_laws::<T>().unwrap();
}
#[test]
fn shared_follows_laws() {
test_suite::shared_follows_laws::<T>().unwrap();
}
#[test]
fn fail_follows_laws() {
test_suite::fail_functor_follows_laws::<T, _>().unwrap();
}
#[test]
fn local_follows_laws() {
test_suite::local_follows_laws::<T>().unwrap();
}
}