Skip to main content

fp_library/classes/
bifunctor.rs

1//! Types that can be mapped over two type arguments simultaneously.
2//!
3//! ### Examples
4//!
5//! ```
6//! use fp_library::{
7//! 	brands::*,
8//! 	functions::*,
9//! };
10//!
11//! let x = Result::<i32, i32>::Ok(5);
12//! let y = bimap::<ResultBrand, _, _, _, _>(|e| e + 1, |s| s * 2, x);
13//! assert_eq!(y, Ok(10));
14//! ```
15#[fp_macros::document_module]
16mod inner {
17	use {
18		crate::{
19			brands::*,
20			classes::*,
21			kinds::*,
22		},
23		fp_macros::*,
24	};
25
26	/// A type class for types that can be mapped over two type arguments.
27	///
28	/// A `Bifunctor` represents a context or container with two type parameters,
29	/// allowing functions to be applied to values of both types.
30	///
31	/// ### Hierarchy Unification
32	///
33	/// This trait inherits from [`Kind_266801a817966495`], ensuring that all bifunctor
34	/// contexts satisfy the strict lifetime requirements where both type arguments must
35	/// outlive the context's application lifetime.
36	///
37	/// By explicitly requiring that both type parameters outlive the application lifetime `'a`,
38	/// we provide the compiler with the necessary guarantees to handle trait objects
39	/// (like `dyn Fn`) commonly used in bifunctor implementations. This resolves potential
40	/// E0310 errors where the compiler cannot otherwise prove that captured variables in
41	/// closures satisfy the required lifetime bounds.
42	///
43	/// ### Laws
44	///
45	/// `Bifunctor` instances must satisfy the following laws:
46	/// * Identity: `bimap(identity, identity, p) = p`.
47	/// * Composition: `bimap(compose(f, g), compose(h, i), p) = bimap(f, h, bimap(g, i, p))`.
48	pub trait Bifunctor: Kind_266801a817966495 {
49		/// Maps functions over the values in the bifunctor context.
50		///
51		/// This method applies two functions to the values inside the bifunctor context, producing a new bifunctor context with the transformed values.
52		#[document_signature]
53		///
54		#[document_type_parameters(
55			"The lifetime of the values.",
56			"The type of the first value.",
57			"The type of the first result.",
58			"The type of the second value.",
59			"The type of the second result."
60		)]
61		///
62		#[document_parameters(
63			"The function to apply to the first value.",
64			"The function to apply to the second value.",
65			"The bifunctor instance."
66		)]
67		///
68		#[document_returns(
69			"A new bifunctor instance containing the results of applying the functions."
70		)]
71		#[document_examples]
72		///
73		/// ```
74		/// use fp_library::{
75		/// 	brands::*,
76		/// 	functions::*,
77		/// };
78		///
79		/// let x = Result::<i32, i32>::Ok(5);
80		/// let y = bimap::<ResultBrand, _, _, _, _>(|e| e + 1, |s| s * 2, x);
81		/// assert_eq!(y, Ok(10));
82		/// ```
83		fn bimap<'a, A: 'a, B: 'a, C: 'a, D: 'a>(
84			f: impl Fn(A) -> B + 'a,
85			g: impl Fn(C) -> D + 'a,
86			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
87		) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>);
88	}
89
90	/// Maps functions over the values in the bifunctor context.
91	///
92	/// Free function version that dispatches to [the type class' associated function][`Bifunctor::bimap`].
93	#[document_signature]
94	///
95	#[document_type_parameters(
96		"The lifetime of the values.",
97		"The brand of the bifunctor.",
98		"The type of the first value.",
99		"The type of the first result.",
100		"The type of the second value.",
101		"The type of the second result."
102	)]
103	///
104	#[document_parameters(
105		"The function to apply to the first value.",
106		"The function to apply to the second value.",
107		"The bifunctor instance."
108	)]
109	///
110	#[document_returns(
111		"A new bifunctor instance containing the results of applying the functions."
112	)]
113	#[document_examples]
114	///
115	/// ```
116	/// use fp_library::{
117	/// 	brands::*,
118	/// 	functions::*,
119	/// };
120	///
121	/// let x = Result::<i32, i32>::Ok(5);
122	/// let y = bimap::<ResultBrand, _, _, _, _>(|e| e + 1, |s| s * 2, x);
123	/// assert_eq!(y, Ok(10));
124	/// ```
125	pub fn bimap<'a, Brand: Bifunctor, A: 'a, B: 'a, C: 'a, D: 'a>(
126		f: impl Fn(A) -> B + 'a,
127		g: impl Fn(C) -> D + 'a,
128		p: Apply!(<Brand as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
129	) -> Apply!(<Brand as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>) {
130		Brand::bimap(f, g, p)
131	}
132
133	impl_kind! {
134		impl<Brand: Bifunctor, A: 'static> for BifunctorFirstAppliedBrand<Brand, A> {
135			type Of<'a, B: 'a>: 'a = Apply!(<Brand as Kind!(type Of<'a, T: 'a, U: 'a>: 'a;)>::Of<'a, A, B>);
136		}
137	}
138
139	/// [`Functor`] instance for [`BifunctorFirstAppliedBrand`].
140	///
141	/// Maps over the first type parameter of a bifunctor by delegating to [`Bifunctor::bimap`]
142	/// with [`identity`](crate::functions::identity) for the second argument.
143	#[document_type_parameters("The bifunctor brand.", "The fixed second type parameter.")]
144	impl<Brand: Bifunctor, A: 'static> Functor for BifunctorFirstAppliedBrand<Brand, A> {
145		/// Map a function over the first type parameter.
146		#[document_signature]
147		#[document_type_parameters(
148			"The lifetime of the values.",
149			"The input type.",
150			"The output type."
151		)]
152		#[document_parameters("The function to apply.", "The bifunctor value to map over.")]
153		#[document_returns("The mapped bifunctor value.")]
154		#[document_examples]
155		///
156		/// ```
157		/// use fp_library::{
158		/// 	brands::*,
159		/// 	functions::*,
160		/// };
161		///
162		/// let x = Result::<i32, i32>::Ok(5);
163		/// let y = map::<BifunctorFirstAppliedBrand<ResultBrand, i32>, _, _>(|s| s * 2, x);
164		/// assert_eq!(y, Ok(10));
165		/// ```
166		fn map<'a, B: 'a, C: 'a>(
167			f: impl Fn(B) -> C + 'a,
168			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
169		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) {
170			Brand::bimap(crate::functions::identity, f, fa)
171		}
172	}
173
174	impl_kind! {
175		impl<Brand: Bifunctor, B: 'static> for BifunctorSecondAppliedBrand<Brand, B> {
176			type Of<'a, A: 'a>: 'a = Apply!(<Brand as Kind!(type Of<'a, T: 'a, U: 'a>: 'a;)>::Of<'a, A, B>);
177		}
178	}
179
180	/// [`Functor`] instance for [`BifunctorSecondAppliedBrand`].
181	///
182	/// Maps over the second type parameter of a bifunctor by delegating to [`Bifunctor::bimap`]
183	/// with [`identity`](crate::functions::identity) for the first argument.
184	#[document_type_parameters("The bifunctor brand.", "The fixed first type parameter.")]
185	impl<Brand: Bifunctor, B: 'static> Functor for BifunctorSecondAppliedBrand<Brand, B> {
186		/// Map a function over the second type parameter.
187		#[document_signature]
188		#[document_type_parameters(
189			"The lifetime of the values.",
190			"The input type.",
191			"The output type."
192		)]
193		#[document_parameters("The function to apply.", "The bifunctor value to map over.")]
194		#[document_returns("The mapped bifunctor value.")]
195		#[document_examples]
196		///
197		/// ```
198		/// use fp_library::{
199		/// 	brands::*,
200		/// 	functions::*,
201		/// };
202		///
203		/// let x = Result::<i32, i32>::Err(5);
204		/// let y = map::<BifunctorSecondAppliedBrand<ResultBrand, i32>, _, _>(|e| e * 2, x);
205		/// assert_eq!(y, Err(10));
206		/// ```
207		fn map<'a, A: 'a, C: 'a>(
208			f: impl Fn(A) -> C + 'a,
209			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
210		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) {
211			Brand::bimap(f, crate::functions::identity, fa)
212		}
213	}
214}
215
216pub use inner::*;