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