fp_library/classes/category.rs
1use crate::{classes::Semigroupoid, hkt::Apply1L2T};
2
3/// A type class for categories.
4///
5/// `Category` extends [`Semigroupoid`] with an identity element.
6///
7/// # Laws
8///
9/// `Category` instances must satisfy the identity law:
10/// * Identity: `compose(identity)(p) = compose(p)(identity)`.
11pub trait Category: Semigroupoid {
12 /// Returns the identity morphism.
13 ///
14 /// # Type Signature
15 ///
16 /// `forall a. Category c => () -> c a a`
17 ///
18 /// # Returns
19 ///
20 /// The identity morphism.
21 fn identity<'a, A: 'a>() -> Apply1L2T<'a, Self, A, A>;
22}
23
24/// Returns the identity morphism.
25///
26/// Free function version that dispatches to [the type class' associated function][`Category::identity`].
27///
28/// # Type Signature
29///
30/// `forall a. Category c => () -> c a a`
31///
32/// # Returns
33///
34/// The identity morphism.
35///
36/// # Examples
37///
38/// ```
39/// use fp_library::{brands::RcFnBrand, functions::category_identity};
40///
41/// assert_eq!(category_identity::<RcFnBrand, _>()(()), ());
42/// ```
43pub fn category_identity<'a, Brand: Category, A: 'a>() -> Apply1L2T<'a, Brand, A, A> {
44 Brand::identity::<'a, _>()
45}