fp_library/typeclasses/
monad.rs

1use crate::typeclasses::{Applicative, Bind};
2
3pub trait Monad: Applicative + Bind {}
4
5/// Blanket implementation for the [`Monad`] typeclass.
6///
7/// Any type that implements all the required supertraits automatically implements [`Monad`].
8impl<T> Monad for T where T: Applicative + Bind {}
9
10#[cfg(test)]
11mod tests {
12	use crate::{
13		brands::OptionBrand,
14		typeclasses::Monad,
15		types::{ResultWithErrBrand, ResultWithOkBrand, SoloBrand, VecBrand},
16	};
17
18	/// Asserts that a type implements [`Monad`].
19	fn assert_monad<T: Monad>() {}
20
21	#[test]
22	/// Assert that brands implementing the required supertraits
23	/// ([`Applicative`], [`Bind`]) also implement [`Monad`].
24	fn test_brands_implement_monad() {
25		assert_monad::<SoloBrand>();
26		assert_monad::<OptionBrand>();
27		assert_monad::<ResultWithErrBrand<()>>();
28		assert_monad::<ResultWithOkBrand<()>>();
29		assert_monad::<VecBrand>();
30	}
31}