Skip to main content

fp_library/classes/
semigroupoid.rs

1//! Semigroupoids, representing objects and composable relationships (morphisms) between them.
2//!
3//! ### Examples
4//!
5//! ```
6//! use fp_library::{brands::*, classes::*, functions::*};
7//!
8//! let f = cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
9//! let g = cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x + 1);
10//! let h = semigroupoid_compose::<RcFnBrand, _, _, _>(f, g);
11//! assert_eq!(h(5), 12); // (5 + 1) * 2
12//! ```
13
14use crate::{Apply, kinds::*};
15use fp_macros::doc_params;
16use fp_macros::doc_type_params;
17use fp_macros::hm_signature;
18
19/// A type class for semigroupoids.
20///
21/// A `Semigroupoid` is a set of objects and composable relationships
22/// (morphisms) between them.
23///
24/// ### Laws
25///
26/// Semigroupoid instances must satisfy the associative law:
27/// * Associativity: `compose(p, compose(q, r)) = compose(compose(p, q), r)`.
28pub trait Semigroupoid: Kind_140eb1e35dc7afb3 {
29	/// Takes morphisms `f` and `g` and returns the morphism `f . g` (`f` composed with `g`).
30	///
31	/// This method composes two morphisms `f` and `g` to produce a new morphism that represents the application of `g` followed by `f`.
32	///
33	/// ### Type Signature
34	///
35	#[hm_signature(Semigroupoid)]
36	///
37	/// ### Type Parameters
38	///
39	#[doc_type_params(
40		"The lifetime of the morphisms.",
41		"The source type of the first morphism.",
42		"The target type of the first morphism and the source type of the second morphism.",
43		"The target type of the second morphism."
44	)]
45	///
46	/// ### Parameters
47	///
48	#[doc_params(
49		"The second morphism to apply (from C to D).",
50		"The first morphism to apply (from B to C)."
51	)]
52	///
53	/// ### Returns
54	///
55	/// The composed morphism (from B to D).
56	///
57	/// ### Examples
58	///
59	/// ```
60	/// use fp_library::{brands::*, classes::*, functions::*};
61	///
62	/// let f = cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
63	/// let g = cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x + 1);
64	/// let h = semigroupoid_compose::<RcFnBrand, _, _, _>(f, g);
65	/// assert_eq!(h(5), 12); // (5 + 1) * 2
66	/// ```
67	fn compose<'a, B: 'a, C: 'a, D: 'a>(
68		f: Apply!(<Self as Kind!( type Of<'a, T, U>; )>::Of<'a, C, D>),
69		g: Apply!(<Self as Kind!( type Of<'a, T, U>; )>::Of<'a, B, C>),
70	) -> Apply!(<Self as Kind!( type Of<'a, T, U>; )>::Of<'a, B, D>);
71}
72
73/// Takes morphisms `f` and `g` and returns the morphism `f . g` (`f` composed with `g`).
74///
75/// Free function version that dispatches to [the type class' associated function][`Semigroupoid::compose`].
76///
77/// ### Type Signature
78///
79#[hm_signature(Semigroupoid)]
80///
81/// ### Type Parameters
82///
83#[doc_type_params(
84	"The lifetime of the morphisms.",
85	"The brand of the semigroupoid.",
86	"The source type of the first morphism.",
87	"The target type of the first morphism and the source type of the second morphism.",
88	"The target type of the second morphism."
89)]
90///
91/// ### Parameters
92///
93#[doc_params(
94	"The second morphism to apply (from C to D).",
95	"The first morphism to apply (from B to C)."
96)]
97///
98/// ### Returns
99///
100/// The composed morphism (from B to D).
101///
102/// ### Examples
103///
104/// ```
105/// use fp_library::{brands::*, classes::*, functions::*};
106///
107/// let f = cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
108/// let g = cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x + 1);
109/// let h = semigroupoid_compose::<RcFnBrand, _, _, _>(f, g);
110/// assert_eq!(h(5), 12); // (5 + 1) * 2
111/// ```
112pub fn compose<'a, Brand: Semigroupoid, B: 'a, C: 'a, D: 'a>(
113	f: Apply!(<Brand as Kind!( type Of<'a, T, U>; )>::Of<'a, C, D>),
114	g: Apply!(<Brand as Kind!( type Of<'a, T, U>; )>::Of<'a, B, C>),
115) -> Apply!(<Brand as Kind!( type Of<'a, T, U>; )>::Of<'a, B, D>) {
116	Brand::compose::<B, C, D>(f, g)
117}