fp_library/classes/
monad.rs

1use super::{applicative::Applicative, semimonad::Semimonad};
2
3/// A type class for monads.
4///
5/// `Monad` extends [`Applicative`] and [`Semimonad`].
6/// It allows for sequencing computations where the structure of the computation depends on the result of the previous computation.
7///
8/// # Type Signature
9///
10/// `class (Applicative m, Semimonad m) => Monad m`
11///
12/// # Examples
13///
14/// ```
15/// use fp_library::classes::monad::Monad;
16/// use fp_library::classes::pointed::pure;
17/// use fp_library::classes::semimonad::bind;
18/// use fp_library::brands::OptionBrand;
19///
20/// // Monad combines Pointed (pure) and Semimonad (bind)
21/// let x = pure::<OptionBrand, _>(5);
22/// let y = bind::<OptionBrand, _, _, _>(x, |i| pure::<OptionBrand, _>(i * 2));
23/// assert_eq!(y, Some(10));
24/// ```
25pub trait Monad: Applicative + Semimonad {}
26
27impl<Brand> Monad for Brand where Brand: Applicative + Semimonad {}