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