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!(type Of<'a, A: 'a, B: 'a>: 'a;)`](crate::kinds::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 #[document_examples]
49 ///
50 /// Bifunctor laws for [`Result`]:
51 ///
52 /// ```
53 /// use fp_library::{
54 /// brands::*,
55 /// functions::*,
56 /// };
57 ///
58 /// let ok: Result<i32, i32> = Ok(5);
59 /// let err: Result<i32, i32> = Err(3);
60 ///
61 /// // Identity: bimap(identity, identity, p) = p
62 /// assert_eq!(bimap::<ResultBrand, _, _, _, _>(identity, identity, ok), ok);
63 /// assert_eq!(bimap::<ResultBrand, _, _, _, _>(identity, identity, err), err);
64 ///
65 /// // Composition: bimap(compose(f, g), compose(h, i), p)
66 /// // = bimap(f, h, bimap(g, i, p))
67 /// let f = |x: i32| x + 1;
68 /// let g = |x: i32| x * 2;
69 /// let h = |x: i32| x + 10;
70 /// let i = |x: i32| x * 3;
71 /// assert_eq!(
72 /// bimap::<ResultBrand, _, _, _, _>(compose(f, g), compose(h, i), ok),
73 /// bimap::<ResultBrand, _, _, _, _>(f, h, bimap::<ResultBrand, _, _, _, _>(g, i, ok)),
74 /// );
75 /// assert_eq!(
76 /// bimap::<ResultBrand, _, _, _, _>(compose(f, g), compose(h, i), err),
77 /// bimap::<ResultBrand, _, _, _, _>(f, h, bimap::<ResultBrand, _, _, _, _>(g, i, err)),
78 /// );
79 /// ```
80 #[kind(type Of<'a, A: 'a, B: 'a>: 'a;)]
81 pub trait Bifunctor {
82 /// Maps functions over the values in the bifunctor context.
83 ///
84 /// This method applies two functions to the values inside the bifunctor context, producing a new bifunctor context with the transformed values.
85 #[document_signature]
86 ///
87 #[document_type_parameters(
88 "The lifetime of the values.",
89 "The type of the first value.",
90 "The type of the first result.",
91 "The type of the second value.",
92 "The type of the second result."
93 )]
94 ///
95 #[document_parameters(
96 "The function to apply to the first value.",
97 "The function to apply to the second value.",
98 "The bifunctor instance."
99 )]
100 ///
101 #[document_returns(
102 "A new bifunctor instance containing the results of applying the functions."
103 )]
104 #[document_examples]
105 ///
106 /// ```
107 /// use fp_library::{
108 /// brands::*,
109 /// functions::*,
110 /// };
111 ///
112 /// let x = Result::<i32, i32>::Ok(5);
113 /// let y = bimap::<ResultBrand, _, _, _, _>(|e| e + 1, |s| s * 2, x);
114 /// assert_eq!(y, Ok(10));
115 /// ```
116 fn bimap<'a, A: 'a, B: 'a, C: 'a, D: 'a>(
117 f: impl Fn(A) -> B + 'a,
118 g: impl Fn(C) -> D + 'a,
119 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
120 ) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>);
121 }
122
123 /// Maps functions over the values in the bifunctor context.
124 ///
125 /// Free function version that dispatches to [the type class' associated function][`Bifunctor::bimap`].
126 #[document_signature]
127 ///
128 #[document_type_parameters(
129 "The lifetime of the values.",
130 "The brand of the bifunctor.",
131 "The type of the first value.",
132 "The type of the first result.",
133 "The type of the second value.",
134 "The type of the second result."
135 )]
136 ///
137 #[document_parameters(
138 "The function to apply to the first value.",
139 "The function to apply to the second value.",
140 "The bifunctor instance."
141 )]
142 ///
143 #[document_returns(
144 "A new bifunctor instance containing the results of applying the functions."
145 )]
146 #[document_examples]
147 ///
148 /// ```
149 /// use fp_library::{
150 /// brands::*,
151 /// functions::*,
152 /// };
153 ///
154 /// let x = Result::<i32, i32>::Ok(5);
155 /// let y = bimap::<ResultBrand, _, _, _, _>(|e| e + 1, |s| s * 2, x);
156 /// assert_eq!(y, Ok(10));
157 /// ```
158 pub fn bimap<'a, Brand: Bifunctor, A: 'a, B: 'a, C: 'a, D: 'a>(
159 f: impl Fn(A) -> B + 'a,
160 g: impl Fn(C) -> D + 'a,
161 p: Apply!(<Brand as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
162 ) -> Apply!(<Brand as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>) {
163 Brand::bimap(f, g, p)
164 }
165
166 impl_kind! {
167 impl<Brand: Bifunctor, A: 'static> for BifunctorFirstAppliedBrand<Brand, A> {
168 type Of<'a, B: 'a>: 'a = Apply!(<Brand as Kind!(type Of<'a, T: 'a, U: 'a>: 'a;)>::Of<'a, A, B>);
169 }
170 }
171
172 /// [`Functor`] instance for [`BifunctorFirstAppliedBrand`].
173 ///
174 /// Maps over the first type parameter of a bifunctor by delegating to [`Bifunctor::bimap`]
175 /// with [`identity`](crate::functions::identity) for the second argument.
176 #[document_type_parameters("The bifunctor brand.", "The fixed second type parameter.")]
177 impl<Brand: Bifunctor, A: 'static> Functor for BifunctorFirstAppliedBrand<Brand, A> {
178 /// Map a function over the first type parameter.
179 #[document_signature]
180 #[document_type_parameters(
181 "The lifetime of the values.",
182 "The input type.",
183 "The output type."
184 )]
185 #[document_parameters("The function to apply.", "The bifunctor value to map over.")]
186 #[document_returns("The mapped bifunctor value.")]
187 #[document_examples]
188 ///
189 /// ```
190 /// use fp_library::{
191 /// brands::*,
192 /// functions::*,
193 /// };
194 ///
195 /// let x = Result::<i32, i32>::Ok(5);
196 /// let y = map::<BifunctorFirstAppliedBrand<ResultBrand, i32>, _, _>(|s| s * 2, x);
197 /// assert_eq!(y, Ok(10));
198 /// ```
199 fn map<'a, B: 'a, C: 'a>(
200 f: impl Fn(B) -> C + 'a,
201 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
202 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) {
203 Brand::bimap(crate::functions::identity, f, fa)
204 }
205 }
206
207 impl_kind! {
208 impl<Brand: Bifunctor, B: 'static> for BifunctorSecondAppliedBrand<Brand, B> {
209 type Of<'a, A: 'a>: 'a = Apply!(<Brand as Kind!(type Of<'a, T: 'a, U: 'a>: 'a;)>::Of<'a, A, B>);
210 }
211 }
212
213 /// [`Functor`] instance for [`BifunctorSecondAppliedBrand`].
214 ///
215 /// Maps over the second type parameter of a bifunctor by delegating to [`Bifunctor::bimap`]
216 /// with [`identity`](crate::functions::identity) for the first argument.
217 #[document_type_parameters("The bifunctor brand.", "The fixed first type parameter.")]
218 impl<Brand: Bifunctor, B: 'static> Functor for BifunctorSecondAppliedBrand<Brand, B> {
219 /// Map a function over the second type parameter.
220 #[document_signature]
221 #[document_type_parameters(
222 "The lifetime of the values.",
223 "The input type.",
224 "The output type."
225 )]
226 #[document_parameters("The function to apply.", "The bifunctor value to map over.")]
227 #[document_returns("The mapped bifunctor value.")]
228 #[document_examples]
229 ///
230 /// ```
231 /// use fp_library::{
232 /// brands::*,
233 /// functions::*,
234 /// };
235 ///
236 /// let x = Result::<i32, i32>::Err(5);
237 /// let y = map::<BifunctorSecondAppliedBrand<ResultBrand, i32>, _, _>(|e| e * 2, x);
238 /// assert_eq!(y, Err(10));
239 /// ```
240 fn map<'a, A: 'a, C: 'a>(
241 f: impl Fn(A) -> C + 'a,
242 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
243 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) {
244 Brand::bimap(f, crate::functions::identity, fa)
245 }
246 }
247}
248
249pub use inner::*;