Skip to main content

fp_library/types/
result.rs

1//! Functional programming trait implementations for the standard library [`Result`] type.
2//!
3//! Extends `Result` with dual functor/monad instances: [`ResultErrAppliedBrand`](crate::brands::ResultErrAppliedBrand) (standard Result monad) functors over the success value, while [`ResultOkAppliedBrand`](crate::brands::ResultOkAppliedBrand) functors over the error value.
4
5#[fp_macros::document_module]
6mod inner {
7	use {
8		crate::{
9			Apply,
10			brands::{
11				ResultBrand,
12				ResultErrAppliedBrand,
13				ResultOkAppliedBrand,
14			},
15			classes::*,
16			dispatch::Ref,
17			impl_kind,
18			kinds::*,
19		},
20		core::ops::ControlFlow,
21		fp_macros::*,
22	};
23
24	impl_kind! {
25		/// HKT branding for the `Result` type.
26		///
27		/// The type parameters for `Of` are ordered `E`, then `A` (Error, then Success).
28		/// This follows functional programming conventions (like Haskell's `Either e a`)
29		/// where the right-most type parameter is the "success" value, allowing the
30		/// type to form a `Monad` over the success type by fixing the error type.
31		for ResultBrand {
32			type Of<A, B> = Result<B, A>;
33		}
34	}
35
36	impl_kind! {
37		/// HKT branding for the `Result` type with lifetimes.
38		///
39		/// The type parameters for `Of` are ordered `E`, then `A` (Error, then Success).
40		for ResultBrand {
41			type Of<'a, A: 'a, B: 'a>: 'a = Result<B, A>;
42		}
43	}
44
45	impl Bifunctor for ResultBrand {
46		/// Maps functions over the values in the result.
47		///
48		/// This method applies one function to the error value and another to the success value.
49		#[document_signature]
50		///
51		#[document_type_parameters(
52			"The lifetime of the values.",
53			"The type of the error value.",
54			"The type of the mapped error value.",
55			"The type of the success value.",
56			"The type of the mapped success value."
57		)]
58		///
59		#[document_parameters(
60			"The function to apply to the error.",
61			"The function to apply to the success.",
62			"The result to map over."
63		)]
64		///
65		#[document_returns("A new result containing the mapped values.")]
66		#[document_examples]
67		///
68		/// ```
69		/// use fp_library::{
70		/// 	brands::*,
71		/// 	functions::*,
72		/// };
73		///
74		/// let x: Result<i32, i32> = Ok(5);
75		/// assert_eq!(explicit::bimap::<ResultBrand, _, _, _, _, _, _>((|e| e + 1, |s| s * 2), x), Ok(10));
76		///
77		/// let y: Result<i32, i32> = Err(5);
78		/// assert_eq!(explicit::bimap::<ResultBrand, _, _, _, _, _, _>((|e| e + 1, |s| s * 2), y), Err(6));
79		/// ```
80		fn bimap<'a, A: 'a, B: 'a, C: 'a, D: 'a>(
81			f: impl Fn(A) -> B + 'a,
82			g: impl Fn(C) -> D + 'a,
83			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
84		) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>) {
85			match p {
86				Ok(c) => Ok(g(c)),
87				Err(a) => Err(f(a)),
88			}
89		}
90	}
91
92	impl RefBifunctor for ResultBrand {
93		/// Maps functions over the values in the result by reference.
94		///
95		/// This method applies one function to a reference of the error value and another
96		/// to a reference of the success value, producing a new result with mapped values.
97		/// The original result is borrowed, not consumed.
98		#[document_signature]
99		///
100		#[document_type_parameters(
101			"The lifetime of the values.",
102			"The type of the error value.",
103			"The type of the mapped error value.",
104			"The type of the success value.",
105			"The type of the mapped success value."
106		)]
107		///
108		#[document_parameters(
109			"The function to apply to a reference of the error.",
110			"The function to apply to a reference of the success.",
111			"The result to map over by reference."
112		)]
113		///
114		#[document_returns("A new result containing the mapped values.")]
115		#[document_examples]
116		///
117		/// ```
118		/// use fp_library::{
119		/// 	brands::*,
120		/// 	classes::ref_bifunctor::*,
121		/// 	functions::*,
122		/// };
123		///
124		/// let ok: Result<i32, i32> = Ok(5);
125		/// assert_eq!(ref_bimap::<ResultBrand, _, _, _, _>(|e| *e + 1, |s| *s * 2, &ok), Ok(10));
126		///
127		/// let err: Result<i32, i32> = Err(5);
128		/// assert_eq!(ref_bimap::<ResultBrand, _, _, _, _>(|e| *e + 1, |s| *s * 2, &err), Err(6));
129		/// ```
130		fn ref_bimap<'a, A: 'a, B: 'a, C: 'a, D: 'a>(
131			f: impl Fn(&A) -> B + 'a,
132			g: impl Fn(&C) -> D + 'a,
133			p: &Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
134		) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>) {
135			match p {
136				Ok(c) => Ok(g(c)),
137				Err(a) => Err(f(a)),
138			}
139		}
140	}
141
142	impl RefBifoldable for ResultBrand {
143		/// Folds a result by reference using two step functions, right-associatively.
144		///
145		/// Dispatches to `f` for `Err(a)` values and `g` for `Ok(b)` values,
146		/// passing references to the contained value rather than owned values.
147		/// Since `Result` contains at most one element, no cloning is needed.
148		#[document_signature]
149		///
150		#[document_type_parameters(
151			"The lifetime of the values.",
152			"The brand of the cloneable function to use.",
153			"The error type (first position).",
154			"The success type (second position).",
155			"The accumulator type."
156		)]
157		///
158		#[document_parameters(
159			"The step function applied to a reference of the error value.",
160			"The step function applied to a reference of the success value.",
161			"The initial accumulator.",
162			"The result to fold by reference."
163		)]
164		///
165		#[document_returns("`f(&a, z)` for `Err(a)`, or `g(&b, z)` for `Ok(b)`.")]
166		#[document_examples]
167		///
168		/// ```
169		/// use fp_library::{
170		/// 	brands::*,
171		/// 	functions::*,
172		/// };
173		///
174		/// let err: Result<i32, i32> = Err(3);
175		/// assert_eq!(
176		/// 	explicit::bi_fold_right::<RcFnBrand, ResultBrand, _, _, _, _, _>(
177		/// 		(|e: &i32, acc| acc - *e, |s: &i32, acc| acc + *s),
178		/// 		10,
179		/// 		&err,
180		/// 	),
181		/// 	7
182		/// );
183		///
184		/// let ok: Result<i32, i32> = Ok(5);
185		/// assert_eq!(
186		/// 	explicit::bi_fold_right::<RcFnBrand, ResultBrand, _, _, _, _, _>(
187		/// 		(|e: &i32, acc| acc - *e, |s: &i32, acc| acc + *s),
188		/// 		10,
189		/// 		&ok,
190		/// 	),
191		/// 	15
192		/// );
193		/// ```
194		fn ref_bi_fold_right<'a, FnBrand: LiftFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
195			f: impl Fn(&A, C) -> C + 'a,
196			g: impl Fn(&B, C) -> C + 'a,
197			z: C,
198			p: &Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
199		) -> C {
200			match p {
201				Err(a) => f(a, z),
202				Ok(b) => g(b, z),
203			}
204		}
205	}
206
207	impl RefBitraversable for ResultBrand {
208		/// Traverses a result by reference with two effectful functions.
209		///
210		/// Dispatches to `f` for `Err(a)` values and `g` for `Ok(b)` values,
211		/// passing references to the contained value rather than owned values.
212		/// The result is wrapped in the applicative context `F`.
213		#[document_signature]
214		///
215		#[document_type_parameters(
216			"The lifetime of the values.",
217			"The brand of the cloneable function wrapper.",
218			"The error type (first position).",
219			"The success type (second position).",
220			"The output error type.",
221			"The output success type.",
222			"The applicative context."
223		)]
224		///
225		#[document_parameters(
226			"The function applied to a reference of the error value.",
227			"The function applied to a reference of the success value.",
228			"The result to traverse by reference."
229		)]
230		///
231		#[document_returns(
232			"`f(&a)` wrapped in context for `Err(a)`, or `g(&b)` wrapped in context for `Ok(b)`."
233		)]
234		#[document_examples]
235		///
236		/// ```
237		/// use fp_library::{
238		/// 	brands::*,
239		/// 	functions::*,
240		/// };
241		///
242		/// let err: Result<i32, i32> = Err(3);
243		/// assert_eq!(
244		/// 	explicit::bi_traverse::<RcFnBrand, ResultBrand, _, _, _, _, OptionBrand, _, _>(
245		/// 		(|e: &i32| Some(e + 1), |s: &i32| Some(s * 2)),
246		/// 		&err,
247		/// 	),
248		/// 	Some(Err(4))
249		/// );
250		///
251		/// let ok: Result<i32, i32> = Ok(5);
252		/// assert_eq!(
253		/// 	explicit::bi_traverse::<RcFnBrand, ResultBrand, _, _, _, _, OptionBrand, _, _>(
254		/// 		(|e: &i32| Some(e + 1), |s: &i32| Some(s * 2)),
255		/// 		&ok,
256		/// 	),
257		/// 	Some(Ok(10))
258		/// );
259		/// ```
260		fn ref_bi_traverse<
261			'a,
262			FnBrand,
263			A: 'a + Clone,
264			B: 'a + Clone,
265			C: 'a + Clone,
266			D: 'a + Clone,
267			F: Applicative,
268		>(
269			f: impl Fn(&A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) + 'a,
270			g: impl Fn(&B) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, D>) + 'a,
271			p: &Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
272		) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, C, D>)>)
273		where
274			FnBrand: LiftFn + 'a,
275			Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, C, D>): Clone,
276			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>): Clone,
277			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, D>): Clone, {
278			match p {
279				Err(a) => F::map(|c| Err(c), f(a)),
280				Ok(b) => F::map(|d| Ok(d), g(b)),
281			}
282		}
283	}
284
285	impl Bifoldable for ResultBrand {
286		/// Folds a result using two step functions, right-associatively.
287		///
288		/// Dispatches to `f` for `Err(a)` values and `g` for `Ok(b)` values.
289		#[document_signature]
290		///
291		#[document_type_parameters(
292			"The lifetime of the values.",
293			"The brand of the cloneable function to use.",
294			"The error type (first position).",
295			"The success type (second position).",
296			"The accumulator type."
297		)]
298		///
299		#[document_parameters(
300			"The step function applied to the error value.",
301			"The step function applied to the success value.",
302			"The initial accumulator.",
303			"The result to fold."
304		)]
305		///
306		#[document_returns("`f(a, z)` for `Err(a)`, or `g(b, z)` for `Ok(b)`.")]
307		#[document_examples]
308		///
309		/// ```
310		/// use fp_library::{
311		/// 	brands::*,
312		/// 	functions::*,
313		/// };
314		///
315		/// assert_eq!(
316		/// 	explicit::bi_fold_right::<RcFnBrand, ResultBrand, _, _, _, _, _>(
317		/// 		(|e: i32, acc| acc - e, |s: i32, acc| acc + s),
318		/// 		10,
319		/// 		Err(3),
320		/// 	),
321		/// 	7
322		/// );
323		/// assert_eq!(
324		/// 	explicit::bi_fold_right::<RcFnBrand, ResultBrand, _, _, _, _, _>(
325		/// 		(|e: i32, acc| acc - e, |s: i32, acc| acc + s),
326		/// 		10,
327		/// 		Ok(5),
328		/// 	),
329		/// 	15
330		/// );
331		/// ```
332		fn bi_fold_right<'a, FnBrand: CloneFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
333			f: impl Fn(A, C) -> C + 'a,
334			g: impl Fn(B, C) -> C + 'a,
335			z: C,
336			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
337		) -> C {
338			match p {
339				Err(a) => f(a, z),
340				Ok(b) => g(b, z),
341			}
342		}
343
344		/// Folds a result using two step functions, left-associatively.
345		///
346		/// Dispatches to `f` for `Err(a)` values and `g` for `Ok(b)` values.
347		#[document_signature]
348		///
349		#[document_type_parameters(
350			"The lifetime of the values.",
351			"The brand of the cloneable function to use.",
352			"The error type (first position).",
353			"The success type (second position).",
354			"The accumulator type."
355		)]
356		///
357		#[document_parameters(
358			"The step function applied to the error value.",
359			"The step function applied to the success value.",
360			"The initial accumulator.",
361			"The result to fold."
362		)]
363		///
364		#[document_returns("`f(z, a)` for `Err(a)`, or `g(z, b)` for `Ok(b)`.")]
365		#[document_examples]
366		///
367		/// ```
368		/// use fp_library::{
369		/// 	brands::*,
370		/// 	functions::*,
371		/// };
372		///
373		/// assert_eq!(
374		/// 	explicit::bi_fold_left::<RcFnBrand, ResultBrand, _, _, _, _, _>(
375		/// 		(|acc, e: i32| acc - e, |acc, s: i32| acc + s),
376		/// 		10,
377		/// 		Err(3),
378		/// 	),
379		/// 	7
380		/// );
381		/// assert_eq!(
382		/// 	explicit::bi_fold_left::<RcFnBrand, ResultBrand, _, _, _, _, _>(
383		/// 		(|acc, e: i32| acc - e, |acc, s: i32| acc + s),
384		/// 		10,
385		/// 		Ok(5),
386		/// 	),
387		/// 	15
388		/// );
389		/// ```
390		fn bi_fold_left<'a, FnBrand: CloneFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
391			f: impl Fn(C, A) -> C + 'a,
392			g: impl Fn(C, B) -> C + 'a,
393			z: C,
394			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
395		) -> C {
396			match p {
397				Err(a) => f(z, a),
398				Ok(b) => g(z, b),
399			}
400		}
401
402		/// Maps a result's value to a monoid using two functions and returns the result.
403		///
404		/// Dispatches to `f` for `Err(a)` and `g` for `Ok(b)`, returning the monoid value.
405		#[document_signature]
406		///
407		#[document_type_parameters(
408			"The lifetime of the values.",
409			"The brand of the cloneable function to use.",
410			"The error type (first position).",
411			"The success type (second position).",
412			"The monoid type."
413		)]
414		///
415		#[document_parameters(
416			"The function mapping the error to the monoid.",
417			"The function mapping the success to the monoid.",
418			"The result to fold."
419		)]
420		///
421		#[document_returns("`f(a)` for `Err(a)`, or `g(b)` for `Ok(b)`.")]
422		#[document_examples]
423		///
424		/// ```
425		/// use fp_library::{
426		/// 	brands::*,
427		/// 	functions::*,
428		/// };
429		///
430		/// assert_eq!(
431		/// 	explicit::bi_fold_map::<RcFnBrand, ResultBrand, _, _, _, _, _>(
432		/// 		(|e: i32| e.to_string(), |s: i32| s.to_string()),
433		/// 		Err::<i32, i32>(3),
434		/// 	),
435		/// 	"3".to_string()
436		/// );
437		/// assert_eq!(
438		/// 	explicit::bi_fold_map::<RcFnBrand, ResultBrand, _, _, _, _, _>(
439		/// 		(|e: i32| e.to_string(), |s: i32| s.to_string()),
440		/// 		Ok::<i32, i32>(5),
441		/// 	),
442		/// 	"5".to_string()
443		/// );
444		/// ```
445		fn bi_fold_map<'a, FnBrand: CloneFn + 'a, A: 'a + Clone, B: 'a + Clone, M>(
446			f: impl Fn(A) -> M + 'a,
447			g: impl Fn(B) -> M + 'a,
448			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
449		) -> M
450		where
451			M: Monoid + 'a, {
452			match p {
453				Err(a) => f(a),
454				Ok(b) => g(b),
455			}
456		}
457	}
458
459	impl Bitraversable for ResultBrand {
460		/// Traverses a result with two effectful functions.
461		///
462		/// Dispatches to `f` for `Err(a)` values and `g` for `Ok(b)` values,
463		/// wrapping the result in the applicative context.
464		#[document_signature]
465		///
466		#[document_type_parameters(
467			"The lifetime of the values.",
468			"The error type (first position).",
469			"The success type (second position).",
470			"The output error type.",
471			"The output success type.",
472			"The applicative context."
473		)]
474		///
475		#[document_parameters(
476			"The function applied to the error value.",
477			"The function applied to the success value.",
478			"The result to traverse."
479		)]
480		///
481		#[document_returns(
482			"`f(a)` wrapped in context for `Err(a)`, or `g(b)` wrapped in context for `Ok(b)`."
483		)]
484		#[document_examples]
485		///
486		/// ```
487		/// use fp_library::{
488		/// 	brands::*,
489		/// 	functions::*,
490		/// };
491		///
492		/// assert_eq!(
493		/// 	explicit::bi_traverse::<RcFnBrand, ResultBrand, _, _, _, _, OptionBrand, _, _>(
494		/// 		(|e: i32| Some(e + 1), |s: i32| Some(s * 2)),
495		/// 		Err::<i32, i32>(3),
496		/// 	),
497		/// 	Some(Err(4))
498		/// );
499		/// assert_eq!(
500		/// 	explicit::bi_traverse::<RcFnBrand, ResultBrand, _, _, _, _, OptionBrand, _, _>(
501		/// 		(|e: i32| Some(e + 1), |s: i32| Some(s * 2)),
502		/// 		Ok::<i32, i32>(5),
503		/// 	),
504		/// 	Some(Ok(10))
505		/// );
506		/// ```
507		fn bi_traverse<
508			'a,
509			A: 'a + Clone,
510			B: 'a + Clone,
511			C: 'a + Clone,
512			D: 'a + Clone,
513			F: Applicative,
514		>(
515			f: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) + 'a,
516			g: impl Fn(B) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, D>) + 'a,
517			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
518		) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, C, D>)>)
519		{
520			match p {
521				Err(a) => F::map(|c| Err(c), f(a)),
522				Ok(b) => F::map(|d| Ok(d), g(b)),
523			}
524		}
525	}
526
527	// ResultErrAppliedBrand<E> (Functor over T)
528
529	impl_kind! {
530		#[no_inferable_brand]
531		#[document_type_parameters("The error type.")]
532		impl<E: 'static> for ResultErrAppliedBrand<E> {
533			type Of<'a, A: 'a>: 'a = Result<A, E>;
534		}
535	}
536
537	#[document_type_parameters("The error type.")]
538	impl<E: 'static> Functor for ResultErrAppliedBrand<E> {
539		/// Maps a function over the value in the result.
540		///
541		/// This method applies a function to the value inside the result if it is `Ok`, producing a new result with the transformed value. If the result is `Err`, it is returned unchanged.
542		#[document_signature]
543		///
544		#[document_type_parameters(
545			"The lifetime of the values.",
546			"The type of the value inside the result.",
547			"The type of the result of applying the function."
548		)]
549		///
550		#[document_parameters("The function to apply.", "The result to map over.")]
551		///
552		#[document_returns(
553			"A new result containing the result of applying the function, or the original error."
554		)]
555		///
556		#[document_examples]
557		///
558		/// ```
559		/// use fp_library::{
560		/// 	brands::*,
561		/// 	functions::*,
562		/// };
563		///
564		/// assert_eq!(
565		/// 	explicit::map::<ResultErrAppliedBrand<()>, _, _, _, _>(|x: i32| x * 2, Ok(5)),
566		/// 	Ok(10)
567		/// );
568		/// assert_eq!(
569		/// 	explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(|x: i32| x * 2, Err(1)),
570		/// 	Err(1)
571		/// );
572		/// ```
573		fn map<'a, A: 'a, B: 'a>(
574			func: impl Fn(A) -> B + 'a,
575			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
576		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
577			fa.map(func)
578		}
579	}
580
581	#[document_type_parameters("The error type.")]
582	impl<E: Clone + 'static> Lift for ResultErrAppliedBrand<E> {
583		/// Lifts a binary function into the result context.
584		///
585		/// This method lifts a binary function to operate on values within the result context.
586		#[document_signature]
587		///
588		#[document_type_parameters(
589			"The lifetime of the values.",
590			"The type of the first value.",
591			"The type of the second value.",
592			"The type of the result."
593		)]
594		///
595		#[document_parameters(
596			"The binary function to apply.",
597			"The first result.",
598			"The second result."
599		)]
600		///
601		#[document_returns(
602			"`Ok(f(a, b))` if both results are `Ok`, otherwise the first error encountered."
603		)]
604		#[document_examples]
605		///
606		/// ```
607		/// use fp_library::{
608		/// 	brands::*,
609		/// 	functions::*,
610		/// };
611		///
612		/// assert_eq!(
613		/// 	explicit::lift2::<ResultErrAppliedBrand<()>, _, _, _, _, _, _>(
614		/// 		|x: i32, y: i32| x + y,
615		/// 		Ok(1),
616		/// 		Ok(2)
617		/// 	),
618		/// 	Ok(3)
619		/// );
620		/// assert_eq!(
621		/// 	explicit::lift2::<ResultErrAppliedBrand<i32>, _, _, _, _, _, _>(
622		/// 		|x: i32, y: i32| x + y,
623		/// 		Ok(1),
624		/// 		Err(2)
625		/// 	),
626		/// 	Err(2)
627		/// );
628		/// assert_eq!(
629		/// 	explicit::lift2::<ResultErrAppliedBrand<i32>, _, _, _, _, _, _>(
630		/// 		|x: i32, y: i32| x + y,
631		/// 		Err(1),
632		/// 		Ok(2)
633		/// 	),
634		/// 	Err(1)
635		/// );
636		/// assert_eq!(
637		/// 	explicit::lift2::<ResultErrAppliedBrand<i32>, _, _, _, _, _, _>(
638		/// 		|x: i32, y: i32| x + y,
639		/// 		Err(1),
640		/// 		Err(2)
641		/// 	),
642		/// 	Err(1)
643		/// );
644		/// ```
645		fn lift2<'a, A, B, C>(
646			func: impl Fn(A, B) -> C + 'a,
647			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
648			fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
649		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
650		where
651			A: Clone + 'a,
652			B: Clone + 'a,
653			C: 'a, {
654			match (fa, fb) {
655				(Ok(a), Ok(b)) => Ok(func(a, b)),
656				(Err(e), _) => Err(e),
657				(_, Err(e)) => Err(e),
658			}
659		}
660	}
661
662	#[document_type_parameters("The error type.")]
663	impl<E: 'static> Pointed for ResultErrAppliedBrand<E> {
664		/// Wraps a value in a result.
665		///
666		/// This method wraps a value in the `Ok` variant of a `Result`.
667		#[document_signature]
668		///
669		#[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
670		///
671		#[document_parameters("The value to wrap.")]
672		///
673		#[document_returns("`Ok(a)`.")]
674		///
675		#[document_examples]
676		///
677		/// ```
678		/// use fp_library::{
679		/// 	brands::ResultErrAppliedBrand,
680		/// 	functions::*,
681		/// };
682		///
683		/// assert_eq!(pure::<ResultErrAppliedBrand<()>, _>(5), Ok(5));
684		/// ```
685		fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
686			Ok(a)
687		}
688	}
689
690	#[document_type_parameters("The error type.")]
691	impl<E: Clone + 'static> ApplyFirst for ResultErrAppliedBrand<E> {}
692
693	#[document_type_parameters("The error type.")]
694	impl<E: Clone + 'static> ApplySecond for ResultErrAppliedBrand<E> {}
695
696	#[document_type_parameters("The error type.")]
697	impl<E: Clone + 'static> Semiapplicative for ResultErrAppliedBrand<E> {
698		/// Applies a wrapped function to a wrapped value.
699		///
700		/// This method applies a function wrapped in a result to a value wrapped in a result.
701		#[document_signature]
702		///
703		#[document_type_parameters(
704			"The lifetime of the values.",
705			"The brand of the cloneable function wrapper.",
706			"The type of the input value.",
707			"The type of the output value."
708		)]
709		///
710		#[document_parameters(
711			"The result containing the function.",
712			"The result containing the value."
713		)]
714		///
715		#[document_returns("`Ok(f(a))` if both are `Ok`, otherwise the first error encountered.")]
716		#[document_examples]
717		///
718		/// ```
719		/// use fp_library::{
720		/// 	Apply,
721		/// 	Kind,
722		/// 	brands::*,
723		/// 	classes::*,
724		/// 	functions::*,
725		/// };
726		///
727		/// let f: Result<_, ()> = Ok(lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
728		/// assert_eq!(apply::<RcFnBrand, ResultErrAppliedBrand<()>, _, _>(f, Ok(5)), Ok(10));
729		/// let f: Result<_, i32> = Ok(lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
730		/// assert_eq!(apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(f, Err(1)), Err(1));
731		///
732		/// let f_err: Result<_, i32> = Err(1);
733		/// assert_eq!(apply::<RcFnBrand, ResultErrAppliedBrand<i32>, i32, i32>(f_err, Ok(5)), Err(1));
734		/// ```
735		fn apply<'a, FnBrand: 'a + CloneFn, A: 'a + Clone, B: 'a>(
736			ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, A, B>>),
737			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
738		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
739			match (ff, fa) {
740				(Ok(f), Ok(a)) => Ok(f(a)),
741				(Err(e), _) => Err(e),
742				(_, Err(e)) => Err(e),
743			}
744		}
745	}
746
747	#[document_type_parameters("The error type.")]
748	impl<E: Clone + 'static> Semimonad for ResultErrAppliedBrand<E> {
749		/// Chains result computations.
750		///
751		/// This method chains two computations, where the second computation depends on the result of the first.
752		#[document_signature]
753		///
754		#[document_type_parameters(
755			"The lifetime of the values.",
756			"The type of the result of the first computation.",
757			"The type of the result of the second computation."
758		)]
759		///
760		#[document_parameters(
761			"The first result.",
762			"The function to apply to the value inside the result."
763		)]
764		///
765		#[document_returns(
766			"The result of applying `f` to the value if `ma` is `Ok`, otherwise the original error."
767		)]
768		#[document_examples]
769		///
770		/// ```
771		/// use fp_library::{
772		/// 	brands::ResultErrAppliedBrand,
773		/// 	functions::*,
774		/// };
775		///
776		/// assert_eq!(
777		/// 	explicit::bind::<ResultErrAppliedBrand<()>, _, _, _, _>(Ok(5), |x| Ok(x * 2)),
778		/// 	Ok(10)
779		/// );
780		/// assert_eq!(
781		/// 	explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(Ok(5), |_| Err::<i32, _>(1)),
782		/// 	Err(1)
783		/// );
784		/// assert_eq!(
785		/// 	explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(Err(1), |x: i32| Ok(x * 2)),
786		/// 	Err(1)
787		/// );
788		/// ```
789		fn bind<'a, A: 'a, B: 'a>(
790			ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
791			func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
792		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
793			ma.and_then(func)
794		}
795	}
796
797	#[document_type_parameters("The error type.")]
798	impl<E: 'static> Foldable for ResultErrAppliedBrand<E> {
799		/// Folds the result from the right.
800		///
801		/// This method performs a right-associative fold of the result.
802		#[document_signature]
803		///
804		#[document_type_parameters(
805			"The lifetime of the values.",
806			"The brand of the cloneable function to use.",
807			"The type of the elements in the structure.",
808			"The type of the accumulator."
809		)]
810		///
811		#[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
812		///
813		#[document_returns("`func(a, initial)` if `fa` is `Ok(a)`, otherwise `initial`.")]
814		///
815		#[document_examples]
816		///
817		/// ```
818		/// use fp_library::{
819		/// 	brands::*,
820		/// 	functions::*,
821		/// };
822		///
823		/// assert_eq!(
824		/// 	explicit::fold_right::<RcFnBrand, ResultErrAppliedBrand<()>, _, _, _, _>(
825		/// 		|x, acc| x + acc,
826		/// 		0,
827		/// 		Ok(5)
828		/// 	),
829		/// 	5
830		/// );
831		/// assert_eq!(
832		/// 	explicit::fold_right::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _, _, _>(
833		/// 		|x: i32, acc| x + acc,
834		/// 		0,
835		/// 		Err(1)
836		/// 	),
837		/// 	0
838		/// );
839		/// ```
840		fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
841			func: impl Fn(A, B) -> B + 'a,
842			initial: B,
843			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
844		) -> B
845		where
846			FnBrand: CloneFn + 'a, {
847			match fa {
848				Ok(a) => func(a, initial),
849				Err(_) => initial,
850			}
851		}
852
853		/// Folds the result from the left.
854		///
855		/// This method performs a left-associative fold of the result.
856		#[document_signature]
857		///
858		#[document_type_parameters(
859			"The lifetime of the values.",
860			"The brand of the cloneable function to use.",
861			"The type of the elements in the structure.",
862			"The type of the accumulator."
863		)]
864		///
865		#[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
866		///
867		#[document_returns("`func(initial, a)` if `fa` is `Ok(a)`, otherwise `initial`.")]
868		///
869		#[document_examples]
870		///
871		/// ```
872		/// use fp_library::{
873		/// 	brands::*,
874		/// 	functions::*,
875		/// };
876		///
877		/// assert_eq!(
878		/// 	explicit::fold_left::<RcFnBrand, ResultErrAppliedBrand<()>, _, _, _, _>(
879		/// 		|acc, x| acc + x,
880		/// 		0,
881		/// 		Ok(5)
882		/// 	),
883		/// 	5
884		/// );
885		/// assert_eq!(
886		/// 	explicit::fold_left::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _, _, _>(
887		/// 		|acc, x: i32| acc + x,
888		/// 		0,
889		/// 		Err(1)
890		/// 	),
891		/// 	0
892		/// );
893		/// ```
894		fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
895			func: impl Fn(B, A) -> B + 'a,
896			initial: B,
897			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
898		) -> B
899		where
900			FnBrand: CloneFn + 'a, {
901			match fa {
902				Ok(a) => func(initial, a),
903				Err(_) => initial,
904			}
905		}
906
907		/// Maps the value to a monoid and returns it.
908		///
909		/// This method maps the element of the result to a monoid and then returns it.
910		#[document_signature]
911		///
912		#[document_type_parameters(
913			"The lifetime of the values.",
914			"The brand of the cloneable function to use.",
915			"The type of the elements in the structure.",
916			"The type of the monoid."
917		)]
918		///
919		#[document_parameters("The mapping function.", "The result to fold.")]
920		///
921		#[document_returns("`func(a)` if `fa` is `Ok(a)`, otherwise `M::empty()`.")]
922		///
923		#[document_examples]
924		///
925		/// ```
926		/// use fp_library::{
927		/// 	brands::*,
928		/// 	functions::*,
929		/// };
930		///
931		/// assert_eq!(
932		/// 	explicit::fold_map::<RcFnBrand, ResultErrAppliedBrand<()>, _, _, _, _>(
933		/// 		|x: i32| x.to_string(),
934		/// 		Ok(5)
935		/// 	),
936		/// 	"5".to_string()
937		/// );
938		/// assert_eq!(
939		/// 	explicit::fold_map::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _, _, _>(
940		/// 		|x: i32| x.to_string(),
941		/// 		Err(1)
942		/// 	),
943		/// 	"".to_string()
944		/// );
945		/// ```
946		fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
947			func: impl Fn(A) -> M + 'a,
948			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
949		) -> M
950		where
951			M: Monoid + 'a,
952			FnBrand: CloneFn + 'a, {
953			match fa {
954				Ok(a) => func(a),
955				Err(_) => M::empty(),
956			}
957		}
958	}
959
960	#[document_type_parameters("The error type.")]
961	impl<E: Clone + 'static> Traversable for ResultErrAppliedBrand<E> {
962		/// Traverses the result with an applicative function.
963		///
964		/// This method maps the element of the result to a computation, evaluates it, and combines the result into an applicative context.
965		#[document_signature]
966		///
967		#[document_type_parameters(
968			"The lifetime of the values.",
969			"The type of the elements in the traversable structure.",
970			"The type of the elements in the resulting traversable structure.",
971			"The applicative context."
972		)]
973		///
974		#[document_parameters("The function to apply.", "The result to traverse.")]
975		///
976		#[document_returns("The result wrapped in the applicative context.")]
977		///
978		#[document_examples]
979		///
980		/// ```
981		/// use fp_library::{
982		/// 	brands::*,
983		/// 	functions::*,
984		/// };
985		///
986		/// assert_eq!(
987		/// 	explicit::traverse::<RcFnBrand, ResultErrAppliedBrand<()>, _, _, OptionBrand, _, _>(
988		/// 		|x| Some(x * 2),
989		/// 		Ok(5)
990		/// 	),
991		/// 	Some(Ok(10))
992		/// );
993		/// assert_eq!(
994		/// 	explicit::traverse::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _, OptionBrand, _, _>(
995		/// 		|x: i32| Some(x * 2),
996		/// 		Err(1)
997		/// 	),
998		/// 	Some(Err(1))
999		/// );
1000		/// assert_eq!(
1001		/// 	explicit::traverse::<RcFnBrand, ResultErrAppliedBrand<()>, _, _, OptionBrand, _, _>(
1002		/// 		|_| None::<i32>,
1003		/// 		Ok(5)
1004		/// 	),
1005		/// 	None
1006		/// );
1007		/// ```
1008		fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1009			func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1010			ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1011		) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)>)
1012		where
1013			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1014			match ta {
1015				Ok(a) => F::map(|b| Ok(b), func(a)),
1016				Err(e) => F::pure(Err(e)),
1017			}
1018		}
1019
1020		/// Sequences a result of applicative.
1021		///
1022		/// This method evaluates the computation inside the result and accumulates the result into an applicative context.
1023		#[document_signature]
1024		///
1025		#[document_type_parameters(
1026			"The lifetime of the values.",
1027			"The type of the elements in the traversable structure.",
1028			"The applicative context."
1029		)]
1030		///
1031		#[document_parameters("The result containing the applicative value.")]
1032		///
1033		#[document_returns("The result wrapped in the applicative context.")]
1034		///
1035		#[document_examples]
1036		///
1037		/// ```
1038		/// use fp_library::{
1039		/// 	brands::{
1040		/// 		OptionBrand,
1041		/// 		ResultErrAppliedBrand,
1042		/// 	},
1043		/// 	functions::*,
1044		/// };
1045		///
1046		/// assert_eq!(sequence::<ResultErrAppliedBrand<()>, _, OptionBrand>(Ok(Some(5))), Some(Ok(5)));
1047		/// assert_eq!(
1048		/// 	sequence::<ResultErrAppliedBrand<i32>, i32, OptionBrand>(Err::<Option<i32>, _>(1)),
1049		/// 	Some(Err::<i32, i32>(1))
1050		/// );
1051		/// assert_eq!(sequence::<ResultErrAppliedBrand<()>, _, OptionBrand>(Ok(None::<i32>)), None);
1052		/// ```
1053		fn sequence<'a, A: 'a + Clone, F: Applicative>(
1054			ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
1055		) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
1056		where
1057			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1058			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
1059			match ta {
1060				Ok(fa) => F::map(|a| Ok(a), fa),
1061				Err(e) => F::pure(Err(e)),
1062			}
1063		}
1064	}
1065
1066	// ResultOkAppliedBrand<T> (Functor over E)
1067
1068	impl_kind! {
1069		#[no_inferable_brand]
1070		#[document_type_parameters("The success type.")]
1071		impl<T: 'static> for ResultOkAppliedBrand<T> {
1072			type Of<'a, A: 'a>: 'a = Result<T, A>;
1073		}
1074	}
1075
1076	#[document_type_parameters("The success type.")]
1077	impl<T: 'static> Functor for ResultOkAppliedBrand<T> {
1078		/// Maps a function over the error value in the result.
1079		///
1080		/// This method applies a function to the error value inside the result if it is `Err`, producing a new result with the transformed error. If the result is `Ok`, it is returned unchanged.
1081		#[document_signature]
1082		///
1083		#[document_type_parameters(
1084			"The lifetime of the values.",
1085			"The type of the error value inside the result.",
1086			"The type of the result of applying the function."
1087		)]
1088		///
1089		#[document_parameters("The function to apply to the error.", "The result to map over.")]
1090		///
1091		#[document_returns(
1092			"A new result containing the mapped error, or the original success value."
1093		)]
1094		///
1095		#[document_examples]
1096		///
1097		/// ```
1098		/// use fp_library::{
1099		/// 	brands::*,
1100		/// 	functions::*,
1101		/// };
1102		///
1103		/// assert_eq!(
1104		/// 	explicit::map::<ResultOkAppliedBrand<i32>, _, _, _, _>(|x: i32| x * 2, Err(5)),
1105		/// 	Err(10)
1106		/// );
1107		/// assert_eq!(
1108		/// 	explicit::map::<ResultOkAppliedBrand<i32>, _, _, _, _>(|x: i32| x * 2, Ok(1)),
1109		/// 	Ok(1)
1110		/// );
1111		/// ```
1112		fn map<'a, A: 'a, B: 'a>(
1113			func: impl Fn(A) -> B + 'a,
1114			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1115		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1116			match fa {
1117				Ok(t) => Ok(t),
1118				Err(e) => Err(func(e)),
1119			}
1120		}
1121	}
1122
1123	#[document_type_parameters("The success type.")]
1124	impl<T: Clone + 'static> Lift for ResultOkAppliedBrand<T> {
1125		/// Lifts a binary function into the result context (over error).
1126		///
1127		/// This method lifts a binary function to operate on error values within the result context.
1128		#[document_signature]
1129		///
1130		#[document_type_parameters(
1131			"The lifetime of the values.",
1132			"The type of the first error value.",
1133			"The type of the second error value.",
1134			"The type of the result error value."
1135		)]
1136		///
1137		#[document_parameters(
1138			"The binary function to apply to the errors.",
1139			"The first result.",
1140			"The second result."
1141		)]
1142		///
1143		#[document_returns(
1144			"`Err(f(a, b))` if both results are `Err`, otherwise the first success encountered."
1145		)]
1146		#[document_examples]
1147		///
1148		/// ```
1149		/// use fp_library::{
1150		/// 	brands::*,
1151		/// 	functions::*,
1152		/// };
1153		///
1154		/// assert_eq!(
1155		/// 	explicit::lift2::<ResultOkAppliedBrand<i32>, _, _, _, _, _, _>(
1156		/// 		|x: i32, y: i32| x + y,
1157		/// 		Err(1),
1158		/// 		Err(2)
1159		/// 	),
1160		/// 	Err(3)
1161		/// );
1162		/// assert_eq!(
1163		/// 	explicit::lift2::<ResultOkAppliedBrand<i32>, _, _, _, _, _, _>(
1164		/// 		|x: i32, y: i32| x + y,
1165		/// 		Err(1),
1166		/// 		Ok(2)
1167		/// 	),
1168		/// 	Ok(2)
1169		/// );
1170		/// assert_eq!(
1171		/// 	explicit::lift2::<ResultOkAppliedBrand<i32>, _, _, _, _, _, _>(
1172		/// 		|x: i32, y: i32| x + y,
1173		/// 		Ok(1),
1174		/// 		Err(2)
1175		/// 	),
1176		/// 	Ok(1)
1177		/// );
1178		/// assert_eq!(
1179		/// 	explicit::lift2::<ResultOkAppliedBrand<i32>, _, _, _, _, _, _>(
1180		/// 		|x: i32, y: i32| x + y,
1181		/// 		Ok(1),
1182		/// 		Ok(2)
1183		/// 	),
1184		/// 	Ok(1)
1185		/// );
1186		/// ```
1187		fn lift2<'a, A, B, C>(
1188			func: impl Fn(A, B) -> C + 'a,
1189			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1190			fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1191		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
1192		where
1193			A: Clone + 'a,
1194			B: Clone + 'a,
1195			C: 'a, {
1196			match (fa, fb) {
1197				(Err(a), Err(b)) => Err(func(a, b)),
1198				(Ok(t), _) => Ok(t),
1199				(_, Ok(t)) => Ok(t),
1200			}
1201		}
1202	}
1203
1204	#[document_type_parameters("The success type.")]
1205	impl<T: 'static> Pointed for ResultOkAppliedBrand<T> {
1206		/// Wraps a value in a result (as error).
1207		///
1208		/// This method wraps a value in the `Err` variant of a `Result`.
1209		#[document_signature]
1210		///
1211		#[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
1212		///
1213		#[document_parameters("The value to wrap.")]
1214		///
1215		#[document_returns("`Err(a)`.")]
1216		///
1217		#[document_examples]
1218		///
1219		/// ```
1220		/// use fp_library::{
1221		/// 	brands::ResultOkAppliedBrand,
1222		/// 	functions::*,
1223		/// };
1224		///
1225		/// assert_eq!(pure::<ResultOkAppliedBrand<()>, _>(5), Err(5));
1226		/// ```
1227		fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1228			Err(a)
1229		}
1230	}
1231
1232	#[document_type_parameters("The success type.")]
1233	impl<T: Clone + 'static> ApplyFirst for ResultOkAppliedBrand<T> {}
1234
1235	#[document_type_parameters("The success type.")]
1236	impl<T: Clone + 'static> ApplySecond for ResultOkAppliedBrand<T> {}
1237
1238	#[document_type_parameters("The success type.")]
1239	impl<T: Clone + 'static> Semiapplicative for ResultOkAppliedBrand<T> {
1240		/// Applies a wrapped function to a wrapped value (over error).
1241		///
1242		/// This method applies a function wrapped in a result (as error) to a value wrapped in a result (as error).
1243		#[document_signature]
1244		///
1245		#[document_type_parameters(
1246			"The lifetime of the values.",
1247			"The brand of the cloneable function wrapper.",
1248			"The type of the input value.",
1249			"The type of the output value."
1250		)]
1251		///
1252		#[document_parameters(
1253			"The result containing the function (in Err).",
1254			"The result containing the value (in Err)."
1255		)]
1256		///
1257		#[document_returns(
1258			"`Err(f(a))` if both are `Err`, otherwise the first success encountered."
1259		)]
1260		#[document_examples]
1261		///
1262		/// ```
1263		/// use fp_library::{
1264		/// 	Apply,
1265		/// 	Kind,
1266		/// 	brands::*,
1267		/// 	classes::*,
1268		/// 	functions::*,
1269		/// };
1270		///
1271		/// let f: Result<(), _> = Err(lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
1272		/// assert_eq!(apply::<RcFnBrand, ResultOkAppliedBrand<()>, _, _>(f, Err(5)), Err(10));
1273		/// let f: Result<i32, _> = Err(lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
1274		/// assert_eq!(apply::<RcFnBrand, ResultOkAppliedBrand<i32>, _, _>(f, Ok(1)), Ok(1));
1275		///
1276		/// let f_ok: Result<i32, _> = Ok(1);
1277		/// assert_eq!(apply::<RcFnBrand, ResultOkAppliedBrand<i32>, i32, i32>(f_ok, Err(5)), Ok(1));
1278		/// ```
1279		fn apply<'a, FnBrand: 'a + CloneFn, A: 'a + Clone, B: 'a>(
1280			ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, A, B>>),
1281			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1282		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1283			match (ff, fa) {
1284				(Err(f), Err(a)) => Err(f(a)),
1285				(Ok(t), _) => Ok(t),
1286				(_, Ok(t)) => Ok(t),
1287			}
1288		}
1289	}
1290
1291	#[document_type_parameters("The success type.")]
1292	impl<T: Clone + 'static> Semimonad for ResultOkAppliedBrand<T> {
1293		/// Chains result computations (over error).
1294		///
1295		/// This method chains two computations, where the second computation depends on the result of the first (over error).
1296		#[document_signature]
1297		///
1298		#[document_type_parameters(
1299			"The lifetime of the values.",
1300			"The type of the result of the first computation.",
1301			"The type of the result of the second computation."
1302		)]
1303		///
1304		#[document_parameters("The first result.", "The function to apply to the error value.")]
1305		///
1306		#[document_returns(
1307			"The result of applying `f` to the error if `ma` is `Err`, otherwise the original success."
1308		)]
1309		///
1310		#[document_examples]
1311		///
1312		/// ```
1313		/// use fp_library::{
1314		/// 	brands::ResultOkAppliedBrand,
1315		/// 	functions::*,
1316		/// };
1317		///
1318		/// assert_eq!(
1319		/// 	explicit::bind::<ResultOkAppliedBrand<()>, _, _, _, _>(Err(5), |x| Err(x * 2)),
1320		/// 	Err(10)
1321		/// );
1322		/// assert_eq!(
1323		/// 	explicit::bind::<ResultOkAppliedBrand<i32>, _, _, _, _>(Err(5), |_| Ok::<_, i32>(1)),
1324		/// 	Ok(1)
1325		/// );
1326		/// assert_eq!(
1327		/// 	explicit::bind::<ResultOkAppliedBrand<i32>, _, _, _, _>(Ok(1), |x: i32| Err(x * 2)),
1328		/// 	Ok(1)
1329		/// );
1330		/// ```
1331		fn bind<'a, A: 'a, B: 'a>(
1332			ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1333			func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1334		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1335			match ma {
1336				Ok(t) => Ok(t),
1337				Err(e) => func(e),
1338			}
1339		}
1340	}
1341
1342	#[document_type_parameters("The success type.")]
1343	impl<T: 'static> Foldable for ResultOkAppliedBrand<T> {
1344		/// Folds the result from the right (over error).
1345		///
1346		/// This method performs a right-associative fold of the result (over error).
1347		#[document_signature]
1348		///
1349		#[document_type_parameters(
1350			"The lifetime of the values.",
1351			"The brand of the cloneable function to use.",
1352			"The type of the elements in the structure.",
1353			"The type of the accumulator."
1354		)]
1355		///
1356		#[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
1357		///
1358		#[document_returns("`func(a, initial)` if `fa` is `Err(a)`, otherwise `initial`.")]
1359		///
1360		#[document_examples]
1361		///
1362		/// ```
1363		/// use fp_library::{
1364		/// 	brands::*,
1365		/// 	functions::*,
1366		/// };
1367		///
1368		/// assert_eq!(
1369		/// 	explicit::fold_right::<RcFnBrand, ResultOkAppliedBrand<i32>, _, _, _, _>(
1370		/// 		|x: i32, acc| x + acc,
1371		/// 		0,
1372		/// 		Err(1)
1373		/// 	),
1374		/// 	1
1375		/// );
1376		/// assert_eq!(
1377		/// 	explicit::fold_right::<RcFnBrand, ResultOkAppliedBrand<()>, _, _, _, _>(
1378		/// 		|x: i32, acc| x + acc,
1379		/// 		0,
1380		/// 		Ok(())
1381		/// 	),
1382		/// 	0
1383		/// );
1384		/// ```
1385		fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
1386			func: impl Fn(A, B) -> B + 'a,
1387			initial: B,
1388			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1389		) -> B
1390		where
1391			FnBrand: CloneFn + 'a, {
1392			match fa {
1393				Err(e) => func(e, initial),
1394				Ok(_) => initial,
1395			}
1396		}
1397
1398		/// Folds the result from the left (over error).
1399		///
1400		/// This method performs a left-associative fold of the result (over error).
1401		#[document_signature]
1402		///
1403		#[document_type_parameters(
1404			"The lifetime of the values.",
1405			"The brand of the cloneable function to use.",
1406			"The type of the elements in the structure.",
1407			"The type of the accumulator."
1408		)]
1409		///
1410		#[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
1411		///
1412		#[document_returns("`func(initial, a)` if `fa` is `Err(a)`, otherwise `initial`.")]
1413		///
1414		#[document_examples]
1415		///
1416		/// ```
1417		/// use fp_library::{
1418		/// 	brands::*,
1419		/// 	functions::*,
1420		/// };
1421		///
1422		/// assert_eq!(
1423		/// 	explicit::fold_left::<RcFnBrand, ResultOkAppliedBrand<()>, _, _, _, _>(
1424		/// 		|acc, x: i32| acc + x,
1425		/// 		0,
1426		/// 		Err(5)
1427		/// 	),
1428		/// 	5
1429		/// );
1430		/// assert_eq!(
1431		/// 	explicit::fold_left::<RcFnBrand, ResultOkAppliedBrand<i32>, _, _, _, _>(
1432		/// 		|acc, x: i32| acc + x,
1433		/// 		0,
1434		/// 		Ok(1)
1435		/// 	),
1436		/// 	0
1437		/// );
1438		/// ```
1439		fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
1440			func: impl Fn(B, A) -> B + 'a,
1441			initial: B,
1442			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1443		) -> B
1444		where
1445			FnBrand: CloneFn + 'a, {
1446			match fa {
1447				Err(e) => func(initial, e),
1448				Ok(_) => initial,
1449			}
1450		}
1451
1452		/// Maps the value to a monoid and returns it (over error).
1453		///
1454		/// This method maps the element of the result to a monoid and then returns it (over error).
1455		#[document_signature]
1456		///
1457		#[document_type_parameters(
1458			"The lifetime of the values.",
1459			"The brand of the cloneable function to use.",
1460			"The type of the elements in the structure.",
1461			"The type of the monoid."
1462		)]
1463		///
1464		#[document_parameters("The mapping function.", "The result to fold.")]
1465		///
1466		#[document_returns("`func(a)` if `fa` is `Err(a)`, otherwise `M::empty()`.")]
1467		///
1468		#[document_examples]
1469		///
1470		/// ```
1471		/// use fp_library::{
1472		/// 	brands::*,
1473		/// 	functions::*,
1474		/// };
1475		///
1476		/// assert_eq!(
1477		/// 	explicit::fold_map::<RcFnBrand, ResultOkAppliedBrand<()>, _, _, _, _>(
1478		/// 		|x: i32| x.to_string(),
1479		/// 		Err(5)
1480		/// 	),
1481		/// 	"5".to_string()
1482		/// );
1483		/// assert_eq!(
1484		/// 	explicit::fold_map::<RcFnBrand, ResultOkAppliedBrand<i32>, _, _, _, _>(
1485		/// 		|x: i32| x.to_string(),
1486		/// 		Ok(1)
1487		/// 	),
1488		/// 	"".to_string()
1489		/// );
1490		/// ```
1491		fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
1492			func: impl Fn(A) -> M + 'a,
1493			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1494		) -> M
1495		where
1496			M: Monoid + 'a,
1497			FnBrand: CloneFn + 'a, {
1498			match fa {
1499				Err(e) => func(e),
1500				Ok(_) => M::empty(),
1501			}
1502		}
1503	}
1504
1505	#[document_type_parameters("The success type.")]
1506	impl<T: Clone + 'static> Traversable for ResultOkAppliedBrand<T> {
1507		/// Traverses the result with an applicative function (over error).
1508		///
1509		/// This method maps the element of the result to a computation, evaluates it, and combines the result into an applicative context (over error).
1510		#[document_signature]
1511		///
1512		#[document_type_parameters(
1513			"The lifetime of the values.",
1514			"The type of the elements in the traversable structure.",
1515			"The type of the elements in the resulting traversable structure.",
1516			"The applicative context."
1517		)]
1518		///
1519		#[document_parameters("The function to apply.", "The result to traverse.")]
1520		///
1521		#[document_returns("The result wrapped in the applicative context.")]
1522		///
1523		#[document_examples]
1524		///
1525		/// ```
1526		/// use fp_library::{
1527		/// 	brands::*,
1528		/// 	functions::*,
1529		/// };
1530		///
1531		/// assert_eq!(
1532		/// 	explicit::traverse::<RcFnBrand, ResultOkAppliedBrand<()>, _, _, OptionBrand, _, _>(
1533		/// 		|x| Some(x * 2),
1534		/// 		Err(5)
1535		/// 	),
1536		/// 	Some(Err(10))
1537		/// );
1538		/// assert_eq!(
1539		/// 	explicit::traverse::<RcFnBrand, ResultOkAppliedBrand<i32>, _, _, OptionBrand, _, _>(
1540		/// 		|x: i32| Some(x * 2),
1541		/// 		Ok(1)
1542		/// 	),
1543		/// 	Some(Ok(1))
1544		/// );
1545		/// assert_eq!(
1546		/// 	explicit::traverse::<RcFnBrand, ResultOkAppliedBrand<()>, _, _, OptionBrand, _, _>(
1547		/// 		|_| None::<i32>,
1548		/// 		Err(5)
1549		/// 	),
1550		/// 	None
1551		/// );
1552		/// ```
1553		fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1554			func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1555			ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1556		) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)>)
1557		where
1558			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1559			match ta {
1560				Err(e) => F::map(|b| Err(b), func(e)),
1561				Ok(t) => F::pure(Ok(t)),
1562			}
1563		}
1564
1565		/// Sequences a result of applicative (over error).
1566		///
1567		/// This method evaluates the computation inside the result and accumulates the result into an applicative context (over error).
1568		#[document_signature]
1569		///
1570		#[document_type_parameters(
1571			"The lifetime of the values.",
1572			"The type of the elements in the traversable structure.",
1573			"The applicative context."
1574		)]
1575		///
1576		#[document_parameters("The result containing the applicative value.")]
1577		///
1578		#[document_returns("The result wrapped in the applicative context.")]
1579		///
1580		#[document_examples]
1581		///
1582		/// ```
1583		/// use fp_library::{
1584		/// 	brands::{
1585		/// 		OptionBrand,
1586		/// 		ResultOkAppliedBrand,
1587		/// 	},
1588		/// 	functions::*,
1589		/// };
1590		///
1591		/// assert_eq!(sequence::<ResultOkAppliedBrand<()>, _, OptionBrand>(Err(Some(5))), Some(Err(5)));
1592		/// assert_eq!(
1593		/// 	sequence::<ResultOkAppliedBrand<i32>, i32, OptionBrand>(Ok::<_, Option<i32>>(1)),
1594		/// 	Some(Ok::<i32, i32>(1))
1595		/// );
1596		/// assert_eq!(sequence::<ResultOkAppliedBrand<()>, _, OptionBrand>(Err(None::<i32>)), None);
1597		/// ```
1598		fn sequence<'a, A: 'a + Clone, F: Applicative>(
1599			ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
1600		) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
1601		where
1602			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1603			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
1604			match ta {
1605				Err(fe) => F::map(|e| Err(e), fe),
1606				Ok(t) => F::pure(Ok(t)),
1607			}
1608		}
1609	}
1610
1611	/// [`MonadRec`] implementation for [`ResultOkAppliedBrand`].
1612	#[document_type_parameters("The success type.")]
1613	impl<T: Clone + 'static> MonadRec for ResultOkAppliedBrand<T> {
1614		/// Performs tail-recursive monadic computation over [`Result`] (error channel).
1615		///
1616		/// Iteratively applies the step function. If the function returns [`Ok`],
1617		/// the computation short-circuits with that success value. If it returns
1618		/// `Err(ControlFlow::Continue(a))`, the loop continues with the new state. If it returns
1619		/// `Err(ControlFlow::Break(b))`, the computation completes with `Err(b)`.
1620		#[document_signature]
1621		///
1622		#[document_type_parameters(
1623			"The lifetime of the computation.",
1624			"The type of the initial value and loop state.",
1625			"The type of the result."
1626		)]
1627		///
1628		#[document_parameters("The step function.", "The initial value.")]
1629		///
1630		#[document_returns(
1631			"The result of the computation, or a success if the step function returned `Ok`."
1632		)]
1633		///
1634		#[document_examples]
1635		///
1636		/// ```
1637		/// use {
1638		/// 	core::ops::ControlFlow,
1639		/// 	fp_library::{
1640		/// 		brands::*,
1641		/// 		functions::*,
1642		/// 		types::*,
1643		/// 	},
1644		/// };
1645		///
1646		/// let result = tail_rec_m::<ResultOkAppliedBrand<&str>, _, _>(
1647		/// 	|n| {
1648		/// 		if n < 10 { Err(ControlFlow::Continue(n + 1)) } else { Err(ControlFlow::Break(n)) }
1649		/// 	},
1650		/// 	0,
1651		/// );
1652		/// assert_eq!(result, Err(10));
1653		/// ```
1654		fn tail_rec_m<'a, A: 'a, B: 'a>(
1655			func: impl Fn(
1656				A,
1657			)
1658				-> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
1659			+ 'a,
1660			initial: A,
1661		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1662			let mut current = initial;
1663			loop {
1664				match func(current) {
1665					Ok(t) => return Ok(t),
1666					Err(ControlFlow::Continue(next)) => current = next,
1667					Err(ControlFlow::Break(b)) => return Err(b),
1668				}
1669			}
1670		}
1671	}
1672
1673	// -- By-reference trait implementations for ResultErrAppliedBrand --
1674
1675	#[document_type_parameters("The error type.")]
1676	impl<E: Clone + 'static> RefFunctor for ResultErrAppliedBrand<E> {
1677		/// Maps a function over the result by reference.
1678		#[document_signature]
1679		#[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1680		#[document_parameters("The function.", "The result.")]
1681		#[document_returns("The mapped result.")]
1682		#[document_examples]
1683		///
1684		/// ```
1685		/// use fp_library::{
1686		/// 	brands::*,
1687		/// 	functions::*,
1688		/// };
1689		/// assert_eq!(
1690		/// 	explicit::map::<ResultErrAppliedBrand<()>, _, _, _, _>(|x: &i32| *x * 2, &Ok(5)),
1691		/// 	Ok(10)
1692		/// );
1693		/// assert_eq!(
1694		/// 	explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(
1695		/// 		|x: &i32| *x * 2,
1696		/// 		&Err::<i32, _>(1)
1697		/// 	),
1698		/// 	Err(1)
1699		/// );
1700		/// ```
1701		fn ref_map<'a, A: 'a, B: 'a>(
1702			func: impl Fn(&A) -> B + 'a,
1703			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1704		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1705			match fa {
1706				Ok(a) => Ok(func(a)),
1707				Err(e) => Err(e.clone()),
1708			}
1709		}
1710	}
1711
1712	#[document_type_parameters("The error type.")]
1713	impl<E: Clone + 'static> RefFoldable for ResultErrAppliedBrand<E> {
1714		/// Folds the result by reference.
1715		#[document_signature]
1716		#[document_type_parameters(
1717			"The lifetime.",
1718			"The brand.",
1719			"The element type.",
1720			"The monoid type."
1721		)]
1722		#[document_parameters("The mapping function.", "The result.")]
1723		#[document_returns("The monoid value.")]
1724		#[document_examples]
1725		///
1726		/// ```
1727		/// use fp_library::{
1728		/// 	brands::*,
1729		/// 	functions::*,
1730		/// };
1731		/// let result = explicit::fold_map::<RcFnBrand, ResultErrAppliedBrand<()>, _, _, _, _>(
1732		/// 	|x: &i32| x.to_string(),
1733		/// 	&Ok(5),
1734		/// );
1735		/// assert_eq!(result, "5");
1736		/// ```
1737		fn ref_fold_map<'a, FnBrand, A: 'a + Clone, M>(
1738			func: impl Fn(&A) -> M + 'a,
1739			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1740		) -> M
1741		where
1742			FnBrand: LiftFn + 'a,
1743			M: Monoid + 'a, {
1744			match fa {
1745				Ok(a) => func(a),
1746				Err(_) => Monoid::empty(),
1747			}
1748		}
1749	}
1750
1751	#[document_type_parameters("The error type.")]
1752	impl<E: Clone + 'static> RefTraversable for ResultErrAppliedBrand<E> {
1753		/// Traverses the result by reference.
1754		#[document_signature]
1755		#[document_type_parameters(
1756			"The lifetime.",
1757			"The brand.",
1758			"The input type.",
1759			"The output type.",
1760			"The applicative."
1761		)]
1762		#[document_parameters("The function.", "The result.")]
1763		#[document_returns("The traversed result.")]
1764		#[document_examples]
1765		///
1766		/// ```
1767		/// use fp_library::{
1768		/// 	brands::*,
1769		/// 	functions::*,
1770		/// };
1771		/// let result: Vec<Result<String, ()>> =
1772		/// 	ref_traverse::<ResultErrAppliedBrand<()>, RcFnBrand, _, _, VecBrand>(
1773		/// 		|x: &i32| vec![x.to_string()],
1774		/// 		&Ok(5),
1775		/// 	);
1776		/// assert_eq!(result, vec![Ok("5".to_string())]);
1777		/// ```
1778		fn ref_traverse<'a, FnBrand, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1779			func: impl Fn(&A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1780			ta: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1781		) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)>)
1782		where
1783			FnBrand: LiftFn + 'a,
1784			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
1785			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1786			match ta {
1787				Ok(a) => F::map(Ok, func(a)),
1788				Err(e) => F::pure(Err(e.clone())),
1789			}
1790		}
1791	}
1792
1793	#[document_type_parameters("The error type.")]
1794	impl<E: 'static> RefPointed for ResultErrAppliedBrand<E> {
1795		/// Creates an `Ok` from a reference by cloning.
1796		#[document_signature]
1797		#[document_type_parameters("The lifetime of the value.", "The type of the value.")]
1798		#[document_parameters("The reference to the value to wrap.")]
1799		#[document_returns("An `Ok` containing a clone of the value.")]
1800		#[document_examples]
1801		///
1802		/// ```
1803		/// use fp_library::{
1804		/// 	brands::*,
1805		/// 	functions::*,
1806		/// };
1807		///
1808		/// let x = 42;
1809		/// let result: Result<i32, ()> = ref_pure::<ResultErrAppliedBrand<()>, _>(&x);
1810		/// assert_eq!(result, Ok(42));
1811		/// ```
1812		fn ref_pure<'a, A: Clone + 'a>(
1813			a: &A
1814		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1815			Ok(a.clone())
1816		}
1817	}
1818
1819	#[document_type_parameters("The error type.")]
1820	impl<E: Clone + 'static> RefLift for ResultErrAppliedBrand<E> {
1821		/// Combines two `Result` values with a by-reference binary function.
1822		#[document_signature]
1823		#[document_type_parameters(
1824			"The lifetime.",
1825			"First input type.",
1826			"Second input type.",
1827			"Output type."
1828		)]
1829		#[document_parameters("The binary function.", "The first result.", "The second result.")]
1830		#[document_returns("The combined result, or the first error encountered.")]
1831		#[document_examples]
1832		///
1833		/// ```
1834		/// use fp_library::{
1835		/// 	brands::*,
1836		/// 	functions::*,
1837		/// };
1838		///
1839		/// let result = explicit::lift2::<ResultErrAppliedBrand<()>, _, _, _, _, _, _>(
1840		/// 	|a: &i32, b: &i32| *a + *b,
1841		/// 	&Ok(1),
1842		/// 	&Ok(2),
1843		/// );
1844		/// assert_eq!(result, Ok(3));
1845		/// ```
1846		fn ref_lift2<'a, A: 'a, B: 'a, C: 'a>(
1847			func: impl Fn(&A, &B) -> C + 'a,
1848			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1849			fb: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1850		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) {
1851			match (fa, fb) {
1852				(Ok(a), Ok(b)) => Ok(func(a, b)),
1853				(Err(e), _) => Err(e.clone()),
1854				(_, Err(e)) => Err(e.clone()),
1855			}
1856		}
1857	}
1858
1859	#[document_type_parameters("The error type.")]
1860	impl<E: Clone + 'static> RefSemiapplicative for ResultErrAppliedBrand<E> {
1861		/// Applies a wrapped by-ref function to a `Result` value.
1862		#[document_signature]
1863		#[document_type_parameters(
1864			"The lifetime.",
1865			"The function brand.",
1866			"The input type.",
1867			"The output type."
1868		)]
1869		#[document_parameters(
1870			"The result containing the by-ref function.",
1871			"The result containing the value."
1872		)]
1873		#[document_returns("The result of applying the function, or the first error.")]
1874		#[document_examples]
1875		///
1876		/// ```
1877		/// use fp_library::{
1878		/// 	brands::*,
1879		/// 	classes::*,
1880		/// 	functions::*,
1881		/// };
1882		///
1883		/// let f: Result<std::rc::Rc<dyn Fn(&i32) -> i32>, ()> = Ok(std::rc::Rc::new(|x: &i32| *x + 1));
1884		/// let result = ref_apply::<RcFnBrand, ResultErrAppliedBrand<()>, _, _>(&f, &Ok(5));
1885		/// assert_eq!(result, Ok(6));
1886		/// ```
1887		fn ref_apply<'a, FnBrand: 'a + CloneFn<Ref>, A: 'a, B: 'a>(
1888			ff: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn<Ref>>::Of<'a, A, B>>),
1889			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1890		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1891			match (ff, fa) {
1892				(Ok(f), Ok(a)) => Ok((**f)(a)),
1893				(Err(e), _) => Err(e.clone()),
1894				(_, Err(e)) => Err(e.clone()),
1895			}
1896		}
1897	}
1898
1899	#[document_type_parameters("The error type.")]
1900	impl<E: Clone + 'static> RefSemimonad for ResultErrAppliedBrand<E> {
1901		/// Chains `Result` computations by reference.
1902		#[document_signature]
1903		#[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1904		#[document_parameters("The input result.", "The function to apply by reference.")]
1905		#[document_returns("The result of applying the function, or the original error.")]
1906		#[document_examples]
1907		///
1908		/// ```
1909		/// use fp_library::{
1910		/// 	brands::*,
1911		/// 	functions::*,
1912		/// };
1913		///
1914		/// let result: Result<String, ()> =
1915		/// 	explicit::bind::<ResultErrAppliedBrand<()>, _, _, _, _>(&Ok(42), |x: &i32| {
1916		/// 		Ok(x.to_string())
1917		/// 	});
1918		/// assert_eq!(result, Ok("42".to_string()));
1919		/// ```
1920		fn ref_bind<'a, A: 'a, B: 'a>(
1921			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1922			f: impl Fn(&A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1923		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1924			match fa {
1925				Ok(a) => f(a),
1926				Err(e) => Err(e.clone()),
1927			}
1928		}
1929	}
1930
1931	// -- By-reference trait implementations for ResultOkAppliedBrand --
1932
1933	#[document_type_parameters("The success type.")]
1934	impl<T: Clone + 'static> RefFunctor for ResultOkAppliedBrand<T> {
1935		/// Maps a function over the error value in the result by reference.
1936		#[document_signature]
1937		#[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1938		#[document_parameters("The function.", "The result.")]
1939		#[document_returns("The mapped result.")]
1940		#[document_examples]
1941		///
1942		/// ```
1943		/// use fp_library::{
1944		/// 	brands::*,
1945		/// 	functions::*,
1946		/// };
1947		/// assert_eq!(
1948		/// 	explicit::map::<ResultOkAppliedBrand<i32>, _, _, _, _>(|x: &i32| *x * 2, &Err(5)),
1949		/// 	Err(10)
1950		/// );
1951		/// assert_eq!(
1952		/// 	explicit::map::<ResultOkAppliedBrand<i32>, _, _, _, _>(|x: &i32| *x * 2, &Ok::<_, i32>(1)),
1953		/// 	Ok(1)
1954		/// );
1955		/// ```
1956		fn ref_map<'a, A: 'a, B: 'a>(
1957			func: impl Fn(&A) -> B + 'a,
1958			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1959		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1960			match fa {
1961				Err(e) => Err(func(e)),
1962				Ok(t) => Ok(t.clone()),
1963			}
1964		}
1965	}
1966
1967	#[document_type_parameters("The success type.")]
1968	impl<T: Clone + 'static> RefFoldable for ResultOkAppliedBrand<T> {
1969		/// Folds the result by reference (over error).
1970		#[document_signature]
1971		#[document_type_parameters(
1972			"The lifetime.",
1973			"The brand.",
1974			"The element type.",
1975			"The monoid type."
1976		)]
1977		#[document_parameters("The mapping function.", "The result.")]
1978		#[document_returns("The monoid value.")]
1979		#[document_examples]
1980		///
1981		/// ```
1982		/// use fp_library::{
1983		/// 	brands::*,
1984		/// 	functions::*,
1985		/// };
1986		/// let result = explicit::fold_map::<RcFnBrand, ResultOkAppliedBrand<()>, _, _, _, _>(
1987		/// 	|x: &i32| x.to_string(),
1988		/// 	&Err(5),
1989		/// );
1990		/// assert_eq!(result, "5");
1991		/// ```
1992		fn ref_fold_map<'a, FnBrand, A: 'a + Clone, M>(
1993			func: impl Fn(&A) -> M + 'a,
1994			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1995		) -> M
1996		where
1997			FnBrand: LiftFn + 'a,
1998			M: Monoid + 'a, {
1999			match fa {
2000				Err(e) => func(e),
2001				Ok(_) => Monoid::empty(),
2002			}
2003		}
2004	}
2005
2006	#[document_type_parameters("The success type.")]
2007	impl<T: Clone + 'static> RefTraversable for ResultOkAppliedBrand<T> {
2008		/// Traverses the result by reference (over error).
2009		#[document_signature]
2010		#[document_type_parameters(
2011			"The lifetime.",
2012			"The brand.",
2013			"The input type.",
2014			"The output type.",
2015			"The applicative."
2016		)]
2017		#[document_parameters("The function.", "The result.")]
2018		#[document_returns("The traversed result.")]
2019		#[document_examples]
2020		///
2021		/// ```
2022		/// use fp_library::{
2023		/// 	brands::*,
2024		/// 	functions::*,
2025		/// };
2026		/// let result: Vec<Result<(), String>> =
2027		/// 	ref_traverse::<ResultOkAppliedBrand<()>, RcFnBrand, _, _, VecBrand>(
2028		/// 		|x: &i32| vec![x.to_string()],
2029		/// 		&Err(5),
2030		/// 	);
2031		/// assert_eq!(result, vec![Err("5".to_string())]);
2032		/// ```
2033		fn ref_traverse<'a, FnBrand, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
2034			func: impl Fn(&A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
2035			ta: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2036		) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)>)
2037		where
2038			FnBrand: LiftFn + 'a,
2039			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
2040			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
2041			match ta {
2042				Err(e) => F::map(Err, func(e)),
2043				Ok(t) => F::pure(Ok(t.clone())),
2044			}
2045		}
2046	}
2047
2048	#[document_type_parameters("The success type.")]
2049	impl<T: 'static> RefPointed for ResultOkAppliedBrand<T> {
2050		/// Creates an `Err` from a reference by cloning.
2051		#[document_signature]
2052		#[document_type_parameters("The lifetime of the value.", "The type of the value.")]
2053		#[document_parameters("The reference to the value to wrap.")]
2054		#[document_returns("An `Err` containing a clone of the value.")]
2055		#[document_examples]
2056		///
2057		/// ```
2058		/// use fp_library::{
2059		/// 	brands::*,
2060		/// 	functions::*,
2061		/// };
2062		///
2063		/// let x = 42;
2064		/// let result: Result<(), i32> = ref_pure::<ResultOkAppliedBrand<()>, _>(&x);
2065		/// assert_eq!(result, Err(42));
2066		/// ```
2067		fn ref_pure<'a, A: Clone + 'a>(
2068			a: &A
2069		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
2070			Err(a.clone())
2071		}
2072	}
2073
2074	#[document_type_parameters("The success type.")]
2075	impl<T: Clone + 'static> RefLift for ResultOkAppliedBrand<T> {
2076		/// Combines two `Result` values with a by-reference binary function (over error).
2077		#[document_signature]
2078		#[document_type_parameters(
2079			"The lifetime.",
2080			"First input type.",
2081			"Second input type.",
2082			"Output type."
2083		)]
2084		#[document_parameters("The binary function.", "The first result.", "The second result.")]
2085		#[document_returns("The combined result, or the first success encountered.")]
2086		#[document_examples]
2087		///
2088		/// ```
2089		/// use fp_library::{
2090		/// 	brands::*,
2091		/// 	functions::*,
2092		/// };
2093		///
2094		/// let result = explicit::lift2::<ResultOkAppliedBrand<i32>, _, _, _, _, _, _>(
2095		/// 	|a: &i32, b: &i32| *a + *b,
2096		/// 	&Err(1),
2097		/// 	&Err(2),
2098		/// );
2099		/// assert_eq!(result, Err(3));
2100		/// ```
2101		fn ref_lift2<'a, A: 'a, B: 'a, C: 'a>(
2102			func: impl Fn(&A, &B) -> C + 'a,
2103			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2104			fb: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
2105		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) {
2106			match (fa, fb) {
2107				(Err(a), Err(b)) => Err(func(a, b)),
2108				(Ok(t), _) => Ok(t.clone()),
2109				(_, Ok(t)) => Ok(t.clone()),
2110			}
2111		}
2112	}
2113
2114	#[document_type_parameters("The success type.")]
2115	impl<T: Clone + 'static> RefSemiapplicative for ResultOkAppliedBrand<T> {
2116		/// Applies a wrapped by-ref function to a `Result` value (over error).
2117		#[document_signature]
2118		#[document_type_parameters(
2119			"The lifetime.",
2120			"The function brand.",
2121			"The input type.",
2122			"The output type."
2123		)]
2124		#[document_parameters(
2125			"The result containing the by-ref function (in Err).",
2126			"The result containing the value (in Err)."
2127		)]
2128		#[document_returns("The result of applying the function, or the first success.")]
2129		#[document_examples]
2130		///
2131		/// ```
2132		/// use fp_library::{
2133		/// 	brands::*,
2134		/// 	classes::*,
2135		/// 	functions::*,
2136		/// };
2137		///
2138		/// let f: Result<(), std::rc::Rc<dyn Fn(&i32) -> i32>> = Err(std::rc::Rc::new(|x: &i32| *x + 1));
2139		/// let result = ref_apply::<RcFnBrand, ResultOkAppliedBrand<()>, _, _>(&f, &Err(5));
2140		/// assert_eq!(result, Err(6));
2141		/// ```
2142		fn ref_apply<'a, FnBrand: 'a + CloneFn<Ref>, A: 'a, B: 'a>(
2143			ff: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn<Ref>>::Of<'a, A, B>>),
2144			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2145		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2146			match (ff, fa) {
2147				(Err(f), Err(a)) => Err((**f)(a)),
2148				(Ok(t), _) => Ok(t.clone()),
2149				(_, Ok(t)) => Ok(t.clone()),
2150			}
2151		}
2152	}
2153
2154	#[document_type_parameters("The success type.")]
2155	impl<T: Clone + 'static> RefSemimonad for ResultOkAppliedBrand<T> {
2156		/// Chains `Result` computations by reference (over error).
2157		#[document_signature]
2158		#[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
2159		#[document_parameters("The input result.", "The function to apply by reference.")]
2160		#[document_returns("The result of applying the function, or the original success.")]
2161		#[document_examples]
2162		///
2163		/// ```
2164		/// use fp_library::{
2165		/// 	brands::*,
2166		/// 	functions::*,
2167		/// };
2168		///
2169		/// let result: Result<(), String> =
2170		/// 	explicit::bind::<ResultOkAppliedBrand<()>, _, _, _, _>(&Err(42), |x: &i32| {
2171		/// 		Err(x.to_string())
2172		/// 	});
2173		/// assert_eq!(result, Err("42".to_string()));
2174		/// ```
2175		fn ref_bind<'a, A: 'a, B: 'a>(
2176			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2177			f: impl Fn(&A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
2178		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2179			match fa {
2180				Err(e) => f(e),
2181				Ok(t) => Ok(t.clone()),
2182			}
2183		}
2184	}
2185
2186	/// [`MonadRec`] implementation for [`ResultErrAppliedBrand`].
2187	#[document_type_parameters("The error type.")]
2188	impl<E: Clone + 'static> MonadRec for ResultErrAppliedBrand<E> {
2189		/// Performs tail-recursive monadic computation over [`Result`].
2190		///
2191		/// Iteratively applies the step function. If the function returns [`Err`],
2192		/// the computation short-circuits with that error. If it returns
2193		/// `Ok(ControlFlow::Continue(a))`, the loop continues with the new state. If it returns
2194		/// `Ok(ControlFlow::Break(b))`, the computation completes with `Ok(b)`.
2195		#[document_signature]
2196		///
2197		#[document_type_parameters(
2198			"The lifetime of the computation.",
2199			"The type of the initial value and loop state.",
2200			"The type of the result."
2201		)]
2202		///
2203		#[document_parameters("The step function.", "The initial value.")]
2204		///
2205		#[document_returns(
2206			"The result of the computation, or an error if the step function returned `Err`."
2207		)]
2208		///
2209		#[document_examples]
2210		///
2211		/// ```
2212		/// use {
2213		/// 	core::ops::ControlFlow,
2214		/// 	fp_library::{
2215		/// 		brands::*,
2216		/// 		functions::*,
2217		/// 		types::*,
2218		/// 	},
2219		/// };
2220		///
2221		/// let result = tail_rec_m::<ResultErrAppliedBrand<&str>, _, _>(
2222		/// 	|n| {
2223		/// 		if n < 10 { Ok(ControlFlow::Continue(n + 1)) } else { Ok(ControlFlow::Break(n)) }
2224		/// 	},
2225		/// 	0,
2226		/// );
2227		/// assert_eq!(result, Ok(10));
2228		/// ```
2229		fn tail_rec_m<'a, A: 'a, B: 'a>(
2230			func: impl Fn(
2231				A,
2232			)
2233				-> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
2234			+ 'a,
2235			initial: A,
2236		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2237			let mut current = initial;
2238			loop {
2239				match func(current) {
2240					Err(e) => return Err(e),
2241					Ok(ControlFlow::Continue(next)) => current = next,
2242					Ok(ControlFlow::Break(b)) => return Ok(b),
2243				}
2244			}
2245		}
2246	}
2247}
2248
2249#[cfg(test)]
2250mod tests {
2251
2252	use {
2253		crate::{
2254			brands::*,
2255			classes::*,
2256			functions::*,
2257		},
2258		quickcheck_macros::quickcheck,
2259	};
2260
2261	// Bifunctor Tests
2262
2263	/// Tests `bimap` on `Ok` and `Err`.
2264	#[test]
2265	fn test_bimap() {
2266		let x: Result<i32, i32> = Ok(5);
2267		assert_eq!(
2268			explicit::bimap::<ResultBrand, _, _, _, _, _, _>((|e| e + 1, |s| s * 2), x),
2269			Ok(10)
2270		);
2271
2272		let y: Result<i32, i32> = Err(5);
2273		assert_eq!(
2274			explicit::bimap::<ResultBrand, _, _, _, _, _, _>((|e| e + 1, |s| s * 2), y),
2275			Err(6)
2276		);
2277	}
2278
2279	// Bifunctor Laws
2280
2281	/// Tests the identity law for Bifunctor.
2282	#[quickcheck]
2283	fn bifunctor_identity(x: Result<i32, i32>) -> bool {
2284		explicit::bimap::<ResultBrand, _, _, _, _, _, _>((identity, identity), x) == x
2285	}
2286
2287	/// Tests the composition law for Bifunctor.
2288	#[quickcheck]
2289	fn bifunctor_composition(x: Result<i32, i32>) -> bool {
2290		let f = |x: i32| x.wrapping_add(1);
2291		let g = |x: i32| x.wrapping_mul(2);
2292		let h = |x: i32| x.wrapping_sub(1);
2293		let i = |x: i32| if x == 0 { 0 } else { x.wrapping_div(2) };
2294
2295		explicit::bimap::<ResultBrand, _, _, _, _, _, _>((compose(f, g), compose(h, i)), x)
2296			== explicit::bimap::<ResultBrand, _, _, _, _, _, _>(
2297				(f, h),
2298				explicit::bimap::<ResultBrand, _, _, _, _, _, _>((g, i), x),
2299			)
2300	}
2301
2302	// Functor Laws
2303
2304	/// Tests the identity law for Functor.
2305	#[quickcheck]
2306	fn functor_identity(x: Result<i32, i32>) -> bool {
2307		explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(identity, x) == x
2308	}
2309
2310	/// Tests the composition law for Functor.
2311	#[quickcheck]
2312	fn functor_composition(x: Result<i32, i32>) -> bool {
2313		let f = |x: i32| x.wrapping_add(1);
2314		let g = |x: i32| x.wrapping_mul(2);
2315		explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(compose(f, g), x)
2316			== explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2317				f,
2318				explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(g, x),
2319			)
2320	}
2321
2322	// Applicative Laws
2323
2324	/// Tests the identity law for Applicative.
2325	#[quickcheck]
2326	fn applicative_identity(v: Result<i32, i32>) -> bool {
2327		apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
2328			pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(identity)),
2329			v,
2330		) == v
2331	}
2332
2333	/// Tests the homomorphism law for Applicative.
2334	#[quickcheck]
2335	fn applicative_homomorphism(x: i32) -> bool {
2336		let f = |x: i32| x.wrapping_mul(2);
2337		apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
2338			pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(f)),
2339			pure::<ResultErrAppliedBrand<i32>, _>(x),
2340		) == pure::<ResultErrAppliedBrand<i32>, _>(f(x))
2341	}
2342
2343	/// Tests the composition law for Applicative.
2344	#[quickcheck]
2345	fn applicative_composition(
2346		w: Result<i32, i32>,
2347		u_is_ok: bool,
2348		v_is_ok: bool,
2349	) -> bool {
2350		let v_fn = |x: i32| x.wrapping_mul(2);
2351		let u_fn = |x: i32| x.wrapping_add(1);
2352
2353		let v = if v_is_ok {
2354			pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(v_fn))
2355		} else {
2356			Err(100)
2357		};
2358		let u = if u_is_ok {
2359			pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(u_fn))
2360		} else {
2361			Err(200)
2362		};
2363
2364		// RHS: u <*> (v <*> w)
2365		let vw = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(v.clone(), w);
2366		let rhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(u.clone(), vw);
2367
2368		// LHS: pure(compose) <*> u <*> v <*> w
2369		// equivalent to (u . v) <*> w
2370		let uv = match (u, v) {
2371			(Ok(uf), Ok(vf)) => {
2372				let composed = move |x| uf(vf(x));
2373				Ok(<RcFnBrand as LiftFn>::new(composed))
2374			}
2375			(Err(e), _) => Err(e),
2376			(_, Err(e)) => Err(e),
2377		};
2378
2379		let lhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(uv, w);
2380
2381		lhs == rhs
2382	}
2383
2384	/// Tests the interchange law for Applicative.
2385	#[quickcheck]
2386	fn applicative_interchange(y: i32) -> bool {
2387		// u <*> pure y = pure ($ y) <*> u
2388		let f = |x: i32| x.wrapping_mul(2);
2389		let u = pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(f));
2390
2391		let lhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
2392			u.clone(),
2393			pure::<ResultErrAppliedBrand<i32>, _>(y),
2394		);
2395
2396		let rhs_fn = <RcFnBrand as LiftFn>::new(move |f: std::rc::Rc<dyn Fn(i32) -> i32>| f(y));
2397		let rhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
2398			pure::<ResultErrAppliedBrand<i32>, _>(rhs_fn),
2399			u,
2400		);
2401
2402		lhs == rhs
2403	}
2404
2405	// Monad Laws
2406
2407	/// Tests the left identity law for Monad.
2408	#[quickcheck]
2409	fn monad_left_identity(a: i32) -> bool {
2410		let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
2411		explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2412			pure::<ResultErrAppliedBrand<i32>, _>(a),
2413			f,
2414		) == f(a)
2415	}
2416
2417	/// Tests the right identity law for Monad.
2418	#[quickcheck]
2419	fn monad_right_identity(m: Result<i32, i32>) -> bool {
2420		explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2421			m,
2422			pure::<ResultErrAppliedBrand<i32>, _>,
2423		) == m
2424	}
2425
2426	/// Tests the associativity law for Monad.
2427	#[quickcheck]
2428	fn monad_associativity(m: Result<i32, i32>) -> bool {
2429		let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
2430		let g = |x: i32| -> Result<i32, i32> { Err(x.wrapping_add(1)) };
2431		explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2432			explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(m, f),
2433			g,
2434		) == explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(m, |x| {
2435			explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(f(x), g)
2436		})
2437	}
2438
2439	// Edge Cases
2440
2441	/// Tests `map` on `Err`.
2442	#[test]
2443	fn map_err() {
2444		assert_eq!(
2445			explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2446				|x: i32| x + 1,
2447				Err::<i32, i32>(1)
2448			),
2449			Err(1)
2450		);
2451	}
2452
2453	/// Tests `bind` on `Err`.
2454	#[test]
2455	fn bind_err() {
2456		assert_eq!(
2457			explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2458				Err::<i32, i32>(1),
2459				|x: i32| Ok(x + 1)
2460			),
2461			Err(1)
2462		);
2463	}
2464
2465	/// Tests `bind` returning `Err`.
2466	#[test]
2467	fn bind_returning_err() {
2468		assert_eq!(
2469			explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(Ok(1), |_| Err::<i32, i32>(2)),
2470			Err(2)
2471		);
2472	}
2473
2474	/// Tests `fold_right` on `Err`.
2475	#[test]
2476	fn fold_right_err() {
2477		assert_eq!(
2478			crate::functions::explicit::fold_right::<
2479				RcFnBrand,
2480				ResultErrAppliedBrand<i32>,
2481				_,
2482				_,
2483				_,
2484				_,
2485			>(|x: i32, acc| x + acc, 0, Err(1)),
2486			0
2487		);
2488	}
2489
2490	/// Tests `fold_left` on `Err`.
2491	#[test]
2492	fn fold_left_err() {
2493		assert_eq!(
2494			crate::functions::explicit::fold_left::<
2495				RcFnBrand,
2496				ResultErrAppliedBrand<i32>,
2497				_,
2498				_,
2499				_,
2500				_,
2501			>(|acc, x: i32| acc + x, 0, Err(1)),
2502			0
2503		);
2504	}
2505
2506	/// Tests `traverse` on `Err`.
2507	#[test]
2508	fn traverse_err() {
2509		assert_eq!(
2510			crate::classes::traversable::traverse::<ResultErrAppliedBrand<i32>, _, _, OptionBrand>(
2511				|x: i32| Some(x + 1),
2512				Err(1)
2513			),
2514			Some(Err(1))
2515		);
2516	}
2517
2518	/// Tests `traverse` returning `Err`.
2519	#[test]
2520	fn traverse_returning_err() {
2521		assert_eq!(
2522			crate::classes::traversable::traverse::<ResultErrAppliedBrand<i32>, _, _, OptionBrand>(
2523				|_: i32| None::<i32>,
2524				Ok(1)
2525			),
2526			None
2527		);
2528	}
2529
2530	// MonadRec tests
2531
2532	/// Tests the MonadRec identity law: `tail_rec_m(|a| pure(Done(a)), x) == pure(x)`.
2533	#[quickcheck]
2534	fn monad_rec_identity(x: i32) -> bool {
2535		use {
2536			crate::classes::monad_rec::tail_rec_m,
2537			core::ops::ControlFlow,
2538		};
2539		tail_rec_m::<ResultErrAppliedBrand<()>, _, _>(|a| Ok(ControlFlow::Break(a)), x) == Ok(x)
2540	}
2541
2542	/// Tests a recursive computation that sums a range via `tail_rec_m`.
2543	#[test]
2544	fn monad_rec_sum_range() {
2545		use {
2546			crate::classes::monad_rec::tail_rec_m,
2547			core::ops::ControlFlow,
2548		};
2549		let result = tail_rec_m::<ResultErrAppliedBrand<&str>, _, _>(
2550			|(n, acc)| {
2551				if n == 0 {
2552					Ok(ControlFlow::Break(acc))
2553				} else {
2554					Ok(ControlFlow::Continue((n - 1, acc + n)))
2555				}
2556			},
2557			(100i64, 0i64),
2558		);
2559		assert_eq!(result, Ok(5050));
2560	}
2561
2562	/// Tests that `tail_rec_m` short-circuits on `Err`.
2563	#[test]
2564	fn monad_rec_short_circuit() {
2565		use {
2566			crate::classes::monad_rec::tail_rec_m,
2567			core::ops::ControlFlow,
2568		};
2569		let result: Result<i32, &str> = tail_rec_m::<ResultErrAppliedBrand<&str>, _, _>(
2570			|n| {
2571				if n == 5 { Err("stopped") } else { Ok(ControlFlow::Continue(n + 1)) }
2572			},
2573			0,
2574		);
2575		assert_eq!(result, Err("stopped"));
2576	}
2577
2578	/// Tests stack safety: `tail_rec_m` handles large iteration counts.
2579	#[test]
2580	fn monad_rec_stack_safety() {
2581		use {
2582			crate::classes::monad_rec::tail_rec_m,
2583			core::ops::ControlFlow,
2584		};
2585		let iterations: i64 = 200_000;
2586		let result = tail_rec_m::<ResultErrAppliedBrand<()>, _, _>(
2587			|acc| {
2588				if acc < iterations {
2589					Ok(ControlFlow::Continue(acc + 1))
2590				} else {
2591					Ok(ControlFlow::Break(acc))
2592				}
2593			},
2594			0i64,
2595		);
2596		assert_eq!(result, Ok(iterations));
2597	}
2598
2599	// MonadRec tests for ResultOkAppliedBrand
2600
2601	/// Tests the MonadRec identity law for `ResultOkAppliedBrand`:
2602	/// `tail_rec_m(|a| Err(Done(a)), x) == Err(x)`.
2603	#[quickcheck]
2604	fn monad_rec_ok_applied_identity(x: i32) -> bool {
2605		use {
2606			crate::classes::monad_rec::tail_rec_m,
2607			core::ops::ControlFlow,
2608		};
2609		tail_rec_m::<ResultOkAppliedBrand<()>, _, _>(|a| Err(ControlFlow::Break(a)), x) == Err(x)
2610	}
2611
2612	/// Tests a recursive computation that sums a range via `tail_rec_m` on the error channel.
2613	#[test]
2614	fn monad_rec_ok_applied_sum_range() {
2615		use {
2616			crate::classes::monad_rec::tail_rec_m,
2617			core::ops::ControlFlow,
2618		};
2619		let result = tail_rec_m::<ResultOkAppliedBrand<&str>, _, _>(
2620			|(n, acc)| {
2621				if n == 0 {
2622					Err(ControlFlow::Break(acc))
2623				} else {
2624					Err(ControlFlow::Continue((n - 1, acc + n)))
2625				}
2626			},
2627			(100i64, 0i64),
2628		);
2629		assert_eq!(result, Err(5050));
2630	}
2631
2632	/// Tests that `tail_rec_m` on `ResultOkAppliedBrand` short-circuits on `Ok`.
2633	#[test]
2634	fn monad_rec_ok_applied_short_circuit() {
2635		use {
2636			crate::classes::monad_rec::tail_rec_m,
2637			core::ops::ControlFlow,
2638		};
2639		let result: Result<&str, i32> = tail_rec_m::<ResultOkAppliedBrand<&str>, _, _>(
2640			|n| {
2641				if n == 5 { Ok("stopped") } else { Err(ControlFlow::Continue(n + 1)) }
2642			},
2643			0,
2644		);
2645		assert_eq!(result, Ok("stopped"));
2646	}
2647
2648	/// Tests stack safety: `tail_rec_m` on `ResultOkAppliedBrand` handles large iteration counts.
2649	#[test]
2650	fn monad_rec_ok_applied_stack_safety() {
2651		use {
2652			crate::classes::monad_rec::tail_rec_m,
2653			core::ops::ControlFlow,
2654		};
2655		let iterations: i64 = 200_000;
2656		let result = tail_rec_m::<ResultOkAppliedBrand<()>, _, _>(
2657			|acc| {
2658				if acc < iterations {
2659					Err(ControlFlow::Continue(acc + 1))
2660				} else {
2661					Err(ControlFlow::Break(acc))
2662				}
2663			},
2664			0i64,
2665		);
2666		assert_eq!(result, Err(iterations));
2667	}
2668
2669	// -- RefBifunctor --
2670
2671	/// RefBifunctor identity: bimap((Clone::clone, Clone::clone), &p) == p
2672	#[quickcheck]
2673	fn ref_bifunctor_identity(x: Result<i32, i32>) -> bool {
2674		explicit::bimap::<ResultBrand, _, _, _, _, _, _>((|a: &i32| *a, |c: &i32| *c), &x) == x
2675	}
2676
2677	/// RefBifunctor composition
2678	#[quickcheck]
2679	fn ref_bifunctor_composition(x: Result<i32, i32>) -> bool {
2680		let f1 = |a: &i32| a.wrapping_add(1);
2681		let f2 = |a: &i32| a.wrapping_mul(2);
2682		let g1 = |c: &i32| c.wrapping_add(10);
2683		let g2 = |c: &i32| c.wrapping_mul(3);
2684		explicit::bimap::<ResultBrand, _, _, _, _, _, _>(
2685			(|a: &i32| f2(&f1(a)), |c: &i32| g2(&g1(c))),
2686			&x,
2687		) == explicit::bimap::<ResultBrand, _, _, _, _, _, _>(
2688			(f2, g2),
2689			&explicit::bimap::<ResultBrand, _, _, _, _, _, _>((f1, g1), &x),
2690		)
2691	}
2692
2693	// -- RefBifoldable --
2694
2695	/// RefBifoldable: bi_fold_map with ref closures produces same result as manual fold
2696	#[quickcheck]
2697	fn ref_bifoldable_fold_map(x: Result<i32, i32>) -> bool {
2698		let result = explicit::bi_fold_map::<RcFnBrand, ResultBrand, _, _, _, _, _>(
2699			(|a: &i32| a.to_string(), |b: &i32| b.to_string()),
2700			&x,
2701		);
2702		let expected = match &x {
2703			Err(a) => a.to_string(),
2704			Ok(b) => b.to_string(),
2705		};
2706		result == expected
2707	}
2708
2709	// -- RefBitraversable --
2710
2711	/// RefBitraversable: bi_traverse with ref closures consistent with ref_bi_sequence . bimap ref
2712	#[quickcheck]
2713	fn ref_bitraversable_consistency(x: Result<i32, i32>) -> bool {
2714		let f = |a: &i32| Some(a.wrapping_add(1));
2715		let g = |c: &i32| Some(c.wrapping_mul(2));
2716		let traversed =
2717			explicit::bi_traverse::<RcFnBrand, ResultBrand, _, _, _, _, OptionBrand, _, _>(
2718				(f, g),
2719				&x,
2720			);
2721		let mapped_then_sequenced =
2722			ref_bi_sequence::<ResultBrand, RcFnBrand, _, _, OptionBrand>(&explicit::bimap::<
2723				ResultBrand,
2724				_,
2725				_,
2726				_,
2727				_,
2728				_,
2729				_,
2730			>((f, g), &x));
2731		traversed == mapped_then_sequenced
2732	}
2733}