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::explicit::*,
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 /// explicit::bimap,
57 /// *,
58 /// },
59 /// };
60 ///
61 /// let ok: Result<i32, i32> = Ok(5);
62 /// let err: Result<i32, i32> = Err(3);
63 ///
64 /// // Identity: bimap((identity, identity), p) = p
65 /// assert_eq!(bimap::<ResultBrand, _, _, _, _, _, _>((identity, identity), ok), ok);
66 /// assert_eq!(bimap::<ResultBrand, _, _, _, _, _, _>((identity, identity), err), err);
67 ///
68 /// // Composition: bimap((compose(f, g), compose(h, i)), p)
69 /// // = bimap((f, h), bimap((g, i), p))
70 /// let f = |x: i32| x + 1;
71 /// let g = |x: i32| x * 2;
72 /// let h = |x: i32| x + 10;
73 /// let i = |x: i32| x * 3;
74 /// assert_eq!(
75 /// bimap::<ResultBrand, _, _, _, _, _, _>((compose(f, g), compose(h, i)), ok),
76 /// bimap::<ResultBrand, _, _, _, _, _, _>(
77 /// (f, h),
78 /// bimap::<ResultBrand, _, _, _, _, _, _>((g, i), ok)
79 /// ),
80 /// );
81 /// assert_eq!(
82 /// bimap::<ResultBrand, _, _, _, _, _, _>((compose(f, g), compose(h, i)), err),
83 /// bimap::<ResultBrand, _, _, _, _, _, _>(
84 /// (f, h),
85 /// bimap::<ResultBrand, _, _, _, _, _, _>((g, i), err)
86 /// ),
87 /// );
88 /// ```
89 #[kind(type Of<'a, A: 'a, B: 'a>: 'a;)]
90 pub trait Bifunctor {
91 /// Maps functions over the values in the bifunctor context.
92 ///
93 /// This method applies two functions to the values inside the bifunctor context, producing a new bifunctor context with the transformed values.
94 #[document_signature]
95 ///
96 #[document_type_parameters(
97 "The lifetime of the values.",
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::explicit::*,
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 fn bimap<'a, 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!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
129 ) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>);
130
131 /// Maps a function over the first type argument of the bifunctor.
132 ///
133 /// This is a convenience method that maps only over the first (left) type argument,
134 /// leaving the second unchanged.
135 /// Corresponds to `lmap` in both Haskell and PureScript.
136 #[document_signature]
137 ///
138 #[document_type_parameters(
139 "The lifetime of the values.",
140 "The type of the first value.",
141 "The type of the first result.",
142 "The type of the second value."
143 )]
144 ///
145 #[document_parameters(
146 "The function to apply to the first value.",
147 "The bifunctor instance."
148 )]
149 ///
150 #[document_returns("A new bifunctor instance with the first value transformed.")]
151 #[document_examples]
152 ///
153 /// ```
154 /// use fp_library::{
155 /// brands::*,
156 /// functions::explicit::*,
157 /// };
158 ///
159 /// let x = Result::<i32, i32>::Err(5);
160 /// let y = map_first::<ResultBrand, _, _, _, _, _>(|e| e * 2, x);
161 /// assert_eq!(y, Err(10));
162 ///
163 /// let x = Result::<i32, i32>::Ok(5);
164 /// let y = map_first::<ResultBrand, _, _, _, _, _>(|e| e * 2, x);
165 /// assert_eq!(y, Ok(5));
166 /// ```
167 fn map_first<'a, A: 'a, B: 'a, C: 'a>(
168 f: impl Fn(A) -> B + 'a,
169 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
170 ) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, C>) {
171 Self::bimap(f, crate::functions::identity, p)
172 }
173
174 /// Maps a function over the second type argument of the bifunctor.
175 ///
176 /// This is a convenience method that maps only over the second (right) type argument,
177 /// leaving the first unchanged.
178 /// Corresponds to `rmap` in both Haskell and PureScript.
179 #[document_signature]
180 ///
181 #[document_type_parameters(
182 "The lifetime of the values.",
183 "The type of the first value.",
184 "The type of the second value.",
185 "The type of the second result."
186 )]
187 ///
188 #[document_parameters(
189 "The function to apply to the second value.",
190 "The bifunctor instance."
191 )]
192 ///
193 #[document_returns("A new bifunctor instance with the second value transformed.")]
194 #[document_examples]
195 ///
196 /// ```
197 /// use fp_library::{
198 /// brands::*,
199 /// functions::explicit::*,
200 /// };
201 ///
202 /// let x = Result::<i32, i32>::Ok(5);
203 /// let y = map_second::<ResultBrand, _, _, _, _, _>(|s| s * 2, x);
204 /// assert_eq!(y, Ok(10));
205 ///
206 /// let x = Result::<i32, i32>::Err(5);
207 /// let y = map_second::<ResultBrand, _, _, _, _, _>(|s| s * 2, x);
208 /// assert_eq!(y, Err(5));
209 /// ```
210 fn map_second<'a, A: 'a, B: 'a, C: 'a>(
211 g: impl Fn(B) -> C + 'a,
212 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
213 ) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>) {
214 Self::bimap(crate::functions::identity, g, p)
215 }
216 }
217
218 /// Maps functions over the values in the bifunctor context.
219 ///
220 /// Free function version that dispatches to [the type class' associated function][`Bifunctor::bimap`].
221 #[document_signature]
222 ///
223 #[document_type_parameters(
224 "The lifetime of the values.",
225 "The brand of the bifunctor.",
226 "The type of the first value.",
227 "The type of the first result.",
228 "The type of the second value.",
229 "The type of the second result."
230 )]
231 ///
232 #[document_parameters(
233 "The function to apply to the first value.",
234 "The function to apply to the second value.",
235 "The bifunctor instance."
236 )]
237 ///
238 #[document_returns(
239 "A new bifunctor instance containing the results of applying the functions."
240 )]
241 #[document_examples]
242 ///
243 /// ```
244 /// use fp_library::{
245 /// brands::*,
246 /// functions::explicit::*,
247 /// };
248 ///
249 /// let x = Result::<i32, i32>::Ok(5);
250 /// let y = bimap::<ResultBrand, _, _, _, _, _, _>((|e| e + 1, |s| s * 2), x);
251 /// assert_eq!(y, Ok(10));
252 /// ```
253 pub fn bimap<'a, Brand: Bifunctor, A: 'a, B: 'a, C: 'a, D: 'a>(
254 f: impl Fn(A) -> B + 'a,
255 g: impl Fn(C) -> D + 'a,
256 p: Apply!(<Brand as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
257 ) -> Apply!(<Brand as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>) {
258 Brand::bimap(f, g, p)
259 }
260
261 /// Maps a function over the first type argument of the bifunctor.
262 ///
263 /// Corresponds to `lmap` in both Haskell and PureScript.
264 ///
265 /// Free function version that dispatches to [the type class' associated function][`Bifunctor::map_first`].
266 #[document_signature]
267 ///
268 #[document_type_parameters(
269 "The lifetime of the values.",
270 "The brand of the bifunctor.",
271 "The type of the first value.",
272 "The type of the first result.",
273 "The type of the second value."
274 )]
275 ///
276 #[document_parameters("The function to apply to the first value.", "The bifunctor instance.")]
277 ///
278 #[document_returns("A new bifunctor instance with the first value transformed.")]
279 #[document_examples]
280 ///
281 /// ```
282 /// use fp_library::{
283 /// brands::*,
284 /// functions::explicit::*,
285 /// };
286 ///
287 /// let x = Result::<i32, i32>::Err(5);
288 /// let y = map_first::<ResultBrand, _, _, _, _, _>(|e| e * 2, x);
289 /// assert_eq!(y, Err(10));
290 ///
291 /// let x = Result::<i32, i32>::Ok(5);
292 /// let y = map_first::<ResultBrand, _, _, _, _, _>(|e| e * 2, x);
293 /// assert_eq!(y, Ok(5));
294 /// ```
295 pub fn map_first<'a, Brand: Bifunctor, A: 'a, B: 'a, C: 'a>(
296 f: impl Fn(A) -> B + 'a,
297 p: Apply!(<Brand as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
298 ) -> Apply!(<Brand as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, C>) {
299 Brand::map_first(f, p)
300 }
301
302 /// Maps a function over the second type argument of the bifunctor.
303 ///
304 /// Corresponds to `rmap` in both Haskell and PureScript.
305 ///
306 /// Free function version that dispatches to [the type class' associated function][`Bifunctor::map_second`].
307 #[document_signature]
308 ///
309 #[document_type_parameters(
310 "The lifetime of the values.",
311 "The brand of the bifunctor.",
312 "The type of the first value.",
313 "The type of the second value.",
314 "The type of the second result."
315 )]
316 ///
317 #[document_parameters("The function to apply to the second value.", "The bifunctor instance.")]
318 ///
319 #[document_returns("A new bifunctor instance with the second value transformed.")]
320 #[document_examples]
321 ///
322 /// ```
323 /// use fp_library::{
324 /// brands::*,
325 /// functions::explicit::*,
326 /// };
327 ///
328 /// let x = Result::<i32, i32>::Ok(5);
329 /// let y = map_second::<ResultBrand, _, _, _, _, _>(|s| s * 2, x);
330 /// assert_eq!(y, Ok(10));
331 ///
332 /// let x = Result::<i32, i32>::Err(5);
333 /// let y = map_second::<ResultBrand, _, _, _, _, _>(|s| s * 2, x);
334 /// assert_eq!(y, Err(5));
335 /// ```
336 pub fn map_second<'a, Brand: Bifunctor, A: 'a, B: 'a, C: 'a>(
337 g: impl Fn(B) -> C + 'a,
338 p: Apply!(<Brand as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
339 ) -> Apply!(<Brand as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>) {
340 Brand::map_second(g, p)
341 }
342
343 impl_kind! {
344 impl<Brand: Bifunctor, A: 'static> for BifunctorFirstAppliedBrand<Brand, A> {
345 type Of<'a, B: 'a>: 'a = Apply!(<Brand as Kind!(type Of<'a, T: 'a, U: 'a>: 'a;)>::Of<'a, A, B>);
346 }
347 }
348
349 /// [`Functor`] instance for [`BifunctorFirstAppliedBrand`].
350 ///
351 /// Maps over the first type parameter of a bifunctor by delegating to [`Bifunctor::bimap`]
352 /// with [`identity`](crate::functions::identity) for the second argument.
353 #[document_type_parameters("The bifunctor brand.", "The fixed second type parameter.")]
354 impl<Brand: Bifunctor, A: 'static> Functor for BifunctorFirstAppliedBrand<Brand, A> {
355 /// Map a function over the first type parameter.
356 #[document_signature]
357 #[document_type_parameters(
358 "The lifetime of the values.",
359 "The input type.",
360 "The output type."
361 )]
362 #[document_parameters("The function to apply.", "The bifunctor value to map over.")]
363 #[document_returns("The mapped bifunctor value.")]
364 #[document_examples]
365 ///
366 /// ```
367 /// use fp_library::{
368 /// brands::*,
369 /// functions::explicit::*,
370 /// };
371 ///
372 /// let x = Result::<i32, i32>::Ok(5);
373 /// let y = map::<BifunctorFirstAppliedBrand<ResultBrand, i32>, _, _, _, _>(|s| s * 2, x);
374 /// assert_eq!(y, Ok(10));
375 /// ```
376 fn map<'a, B: 'a, C: 'a>(
377 f: impl Fn(B) -> C + 'a,
378 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
379 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) {
380 Brand::bimap(crate::functions::identity, f, fa)
381 }
382 }
383
384 impl_kind! {
385 impl<Brand: Bifunctor, B: 'static> for BifunctorSecondAppliedBrand<Brand, B> {
386 type Of<'a, A: 'a>: 'a = Apply!(<Brand as Kind!(type Of<'a, T: 'a, U: 'a>: 'a;)>::Of<'a, A, B>);
387 }
388 }
389
390 /// [`Functor`] instance for [`BifunctorSecondAppliedBrand`].
391 ///
392 /// Maps over the second type parameter of a bifunctor by delegating to [`Bifunctor::bimap`]
393 /// with [`identity`](crate::functions::identity) for the first argument.
394 #[document_type_parameters("The bifunctor brand.", "The fixed first type parameter.")]
395 impl<Brand: Bifunctor, B: 'static> Functor for BifunctorSecondAppliedBrand<Brand, B> {
396 /// Map a function over the second type parameter.
397 #[document_signature]
398 #[document_type_parameters(
399 "The lifetime of the values.",
400 "The input type.",
401 "The output type."
402 )]
403 #[document_parameters("The function to apply.", "The bifunctor value to map over.")]
404 #[document_returns("The mapped bifunctor value.")]
405 #[document_examples]
406 ///
407 /// ```
408 /// use fp_library::{
409 /// brands::*,
410 /// functions::explicit::*,
411 /// };
412 ///
413 /// let x = Result::<i32, i32>::Err(5);
414 /// let y = map::<BifunctorSecondAppliedBrand<ResultBrand, i32>, _, _, _, _>(|e| e * 2, x);
415 /// assert_eq!(y, Err(10));
416 /// ```
417 fn map<'a, A: 'a, C: 'a>(
418 f: impl Fn(A) -> C + 'a,
419 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
420 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) {
421 Brand::bimap(f, crate::functions::identity, fa)
422 }
423 }
424}
425
426pub use inner::*;