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