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 bifunctor instance [`ResultBrand`](crate::brands::ResultBrand) and 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		#[multi_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::semiapplicative::apply as explicit_apply,
724		/// 	functions::*,
725		/// };
726		///
727		/// let f: Result<_, ()> = Ok(lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
728		/// assert_eq!(explicit_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!(explicit_apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(f, Err(1)), Err(1));
731		///
732		/// let f_err: Result<_, i32> = Err(1);
733		/// assert_eq!(
734		/// 	explicit_apply::<RcFnBrand, ResultErrAppliedBrand<i32>, i32, i32>(f_err, Ok(5)),
735		/// 	Err(1)
736		/// );
737		/// ```
738		fn apply<'a, FnBrand: 'a + CloneFn, A: 'a + Clone, B: 'a>(
739			ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, A, B>>),
740			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
741		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
742			match (ff, fa) {
743				(Ok(f), Ok(a)) => Ok(f(a)),
744				(Err(e), _) => Err(e),
745				(_, Err(e)) => Err(e),
746			}
747		}
748	}
749
750	#[document_type_parameters("The error type.")]
751	impl<E: Clone + 'static> Semimonad for ResultErrAppliedBrand<E> {
752		/// Chains result computations.
753		///
754		/// This method chains two computations, where the second computation depends on the result of the first.
755		#[document_signature]
756		///
757		#[document_type_parameters(
758			"The lifetime of the values.",
759			"The type of the result of the first computation.",
760			"The type of the result of the second computation."
761		)]
762		///
763		#[document_parameters(
764			"The first result.",
765			"The function to apply to the value inside the result."
766		)]
767		///
768		#[document_returns(
769			"The result of applying `f` to the value if `ma` is `Ok`, otherwise the original error."
770		)]
771		#[document_examples]
772		///
773		/// ```
774		/// use fp_library::{
775		/// 	brands::ResultErrAppliedBrand,
776		/// 	functions::*,
777		/// };
778		///
779		/// assert_eq!(
780		/// 	explicit::bind::<ResultErrAppliedBrand<()>, _, _, _, _>(Ok(5), |x| Ok(x * 2)),
781		/// 	Ok(10)
782		/// );
783		/// assert_eq!(
784		/// 	explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(Ok(5), |_| Err::<i32, _>(1)),
785		/// 	Err(1)
786		/// );
787		/// assert_eq!(
788		/// 	explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(Err(1), |x: i32| Ok(x * 2)),
789		/// 	Err(1)
790		/// );
791		/// ```
792		fn bind<'a, A: 'a, B: 'a>(
793			ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
794			func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
795		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
796			ma.and_then(func)
797		}
798	}
799
800	#[document_type_parameters("The error type.")]
801	impl<E: 'static> Foldable for ResultErrAppliedBrand<E> {
802		/// Folds the result from the right.
803		///
804		/// This method performs a right-associative fold of the result.
805		#[document_signature]
806		///
807		#[document_type_parameters(
808			"The lifetime of the values.",
809			"The brand of the cloneable function to use.",
810			"The type of the elements in the structure.",
811			"The type of the accumulator."
812		)]
813		///
814		#[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
815		///
816		#[document_returns("`func(a, initial)` if `fa` is `Ok(a)`, otherwise `initial`.")]
817		///
818		#[document_examples]
819		///
820		/// ```
821		/// use fp_library::{
822		/// 	brands::*,
823		/// 	functions::*,
824		/// };
825		///
826		/// assert_eq!(
827		/// 	explicit::fold_right::<RcFnBrand, ResultErrAppliedBrand<()>, _, _, _, _>(
828		/// 		|x, acc| x + acc,
829		/// 		0,
830		/// 		Ok(5)
831		/// 	),
832		/// 	5
833		/// );
834		/// assert_eq!(
835		/// 	explicit::fold_right::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _, _, _>(
836		/// 		|x: i32, acc| x + acc,
837		/// 		0,
838		/// 		Err(1)
839		/// 	),
840		/// 	0
841		/// );
842		/// ```
843		fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
844			func: impl Fn(A, B) -> B + 'a,
845			initial: B,
846			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
847		) -> B
848		where
849			FnBrand: CloneFn + 'a, {
850			match fa {
851				Ok(a) => func(a, initial),
852				Err(_) => initial,
853			}
854		}
855
856		/// Folds the result from the left.
857		///
858		/// This method performs a left-associative fold of the result.
859		#[document_signature]
860		///
861		#[document_type_parameters(
862			"The lifetime of the values.",
863			"The brand of the cloneable function to use.",
864			"The type of the elements in the structure.",
865			"The type of the accumulator."
866		)]
867		///
868		#[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
869		///
870		#[document_returns("`func(initial, a)` if `fa` is `Ok(a)`, otherwise `initial`.")]
871		///
872		#[document_examples]
873		///
874		/// ```
875		/// use fp_library::{
876		/// 	brands::*,
877		/// 	functions::*,
878		/// };
879		///
880		/// assert_eq!(
881		/// 	explicit::fold_left::<RcFnBrand, ResultErrAppliedBrand<()>, _, _, _, _>(
882		/// 		|acc, x| acc + x,
883		/// 		0,
884		/// 		Ok(5)
885		/// 	),
886		/// 	5
887		/// );
888		/// assert_eq!(
889		/// 	explicit::fold_left::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _, _, _>(
890		/// 		|acc, x: i32| acc + x,
891		/// 		0,
892		/// 		Err(1)
893		/// 	),
894		/// 	0
895		/// );
896		/// ```
897		fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
898			func: impl Fn(B, A) -> B + 'a,
899			initial: B,
900			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
901		) -> B
902		where
903			FnBrand: CloneFn + 'a, {
904			match fa {
905				Ok(a) => func(initial, a),
906				Err(_) => initial,
907			}
908		}
909
910		/// Maps the value to a monoid and returns it.
911		///
912		/// This method maps the element of the result to a monoid and then returns it.
913		#[document_signature]
914		///
915		#[document_type_parameters(
916			"The lifetime of the values.",
917			"The brand of the cloneable function to use.",
918			"The type of the elements in the structure.",
919			"The type of the monoid."
920		)]
921		///
922		#[document_parameters("The mapping function.", "The result to fold.")]
923		///
924		#[document_returns("`func(a)` if `fa` is `Ok(a)`, otherwise `M::empty()`.")]
925		///
926		#[document_examples]
927		///
928		/// ```
929		/// use fp_library::{
930		/// 	brands::*,
931		/// 	functions::*,
932		/// };
933		///
934		/// assert_eq!(
935		/// 	explicit::fold_map::<RcFnBrand, ResultErrAppliedBrand<()>, _, _, _, _>(
936		/// 		|x: i32| x.to_string(),
937		/// 		Ok(5)
938		/// 	),
939		/// 	"5".to_string()
940		/// );
941		/// assert_eq!(
942		/// 	explicit::fold_map::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _, _, _>(
943		/// 		|x: i32| x.to_string(),
944		/// 		Err(1)
945		/// 	),
946		/// 	"".to_string()
947		/// );
948		/// ```
949		fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
950			func: impl Fn(A) -> M + 'a,
951			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
952		) -> M
953		where
954			M: Monoid + 'a,
955			FnBrand: CloneFn + 'a, {
956			match fa {
957				Ok(a) => func(a),
958				Err(_) => M::empty(),
959			}
960		}
961	}
962
963	#[document_type_parameters("The error type.")]
964	impl<E: Clone + 'static> Traversable for ResultErrAppliedBrand<E> {
965		/// Traverses the result with an applicative function.
966		///
967		/// This method maps the element of the result to a computation, evaluates it, and combines the result into an applicative context.
968		#[document_signature]
969		///
970		#[document_type_parameters(
971			"The lifetime of the values.",
972			"The type of the elements in the traversable structure.",
973			"The type of the elements in the resulting traversable structure.",
974			"The applicative context."
975		)]
976		///
977		#[document_parameters("The function to apply.", "The result to traverse.")]
978		///
979		#[document_returns("The result wrapped in the applicative context.")]
980		///
981		#[document_examples]
982		///
983		/// ```
984		/// use fp_library::{
985		/// 	brands::*,
986		/// 	functions::*,
987		/// };
988		///
989		/// assert_eq!(
990		/// 	explicit::traverse::<RcFnBrand, ResultErrAppliedBrand<()>, _, _, OptionBrand, _, _>(
991		/// 		|x| Some(x * 2),
992		/// 		Ok(5)
993		/// 	),
994		/// 	Some(Ok(10))
995		/// );
996		/// assert_eq!(
997		/// 	explicit::traverse::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _, OptionBrand, _, _>(
998		/// 		|x: i32| Some(x * 2),
999		/// 		Err(1)
1000		/// 	),
1001		/// 	Some(Err(1))
1002		/// );
1003		/// assert_eq!(
1004		/// 	explicit::traverse::<RcFnBrand, ResultErrAppliedBrand<()>, _, _, OptionBrand, _, _>(
1005		/// 		|_| None::<i32>,
1006		/// 		Ok(5)
1007		/// 	),
1008		/// 	None
1009		/// );
1010		/// ```
1011		fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1012			func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1013			ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1014		) -> 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>)>)
1015		where
1016			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1017			match ta {
1018				Ok(a) => F::map(|b| Ok(b), func(a)),
1019				Err(e) => F::pure(Err(e)),
1020			}
1021		}
1022
1023		/// Sequences a result of applicative.
1024		///
1025		/// This method evaluates the computation inside the result and accumulates the result into an applicative context.
1026		#[document_signature]
1027		///
1028		#[document_type_parameters(
1029			"The lifetime of the values.",
1030			"The type of the elements in the traversable structure.",
1031			"The applicative context."
1032		)]
1033		///
1034		#[document_parameters("The result containing the applicative value.")]
1035		///
1036		#[document_returns("The result wrapped in the applicative context.")]
1037		///
1038		#[document_examples]
1039		///
1040		/// ```
1041		/// use fp_library::{
1042		/// 	brands::{
1043		/// 		OptionBrand,
1044		/// 		ResultErrAppliedBrand,
1045		/// 	},
1046		/// 	functions::*,
1047		/// };
1048		///
1049		/// assert_eq!(sequence::<ResultErrAppliedBrand<()>, _, OptionBrand>(Ok(Some(5))), Some(Ok(5)));
1050		/// assert_eq!(
1051		/// 	sequence::<ResultErrAppliedBrand<i32>, i32, OptionBrand>(Err::<Option<i32>, _>(1)),
1052		/// 	Some(Err::<i32, i32>(1))
1053		/// );
1054		/// assert_eq!(sequence::<ResultErrAppliedBrand<()>, _, OptionBrand>(Ok(None::<i32>)), None);
1055		/// ```
1056		fn sequence<'a, A: 'a + Clone, F: Applicative>(
1057			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>)>)
1058		) -> 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>)>)
1059		where
1060			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1061			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
1062			match ta {
1063				Ok(fa) => F::map(|a| Ok(a), fa),
1064				Err(e) => F::pure(Err(e)),
1065			}
1066		}
1067	}
1068
1069	// ResultOkAppliedBrand<T> (Functor over E)
1070
1071	impl_kind! {
1072		#[multi_brand]
1073		#[document_type_parameters("The success type.")]
1074		impl<T: 'static> for ResultOkAppliedBrand<T> {
1075			type Of<'a, A: 'a>: 'a = Result<T, A>;
1076		}
1077	}
1078
1079	#[document_type_parameters("The success type.")]
1080	impl<T: 'static> Functor for ResultOkAppliedBrand<T> {
1081		/// Maps a function over the error value in the result.
1082		///
1083		/// 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.
1084		#[document_signature]
1085		///
1086		#[document_type_parameters(
1087			"The lifetime of the values.",
1088			"The type of the error value inside the result.",
1089			"The type of the result of applying the function."
1090		)]
1091		///
1092		#[document_parameters("The function to apply to the error.", "The result to map over.")]
1093		///
1094		#[document_returns(
1095			"A new result containing the mapped error, or the original success value."
1096		)]
1097		///
1098		#[document_examples]
1099		///
1100		/// ```
1101		/// use fp_library::{
1102		/// 	brands::*,
1103		/// 	functions::*,
1104		/// };
1105		///
1106		/// assert_eq!(
1107		/// 	explicit::map::<ResultOkAppliedBrand<i32>, _, _, _, _>(|x: i32| x * 2, Err(5)),
1108		/// 	Err(10)
1109		/// );
1110		/// assert_eq!(
1111		/// 	explicit::map::<ResultOkAppliedBrand<i32>, _, _, _, _>(|x: i32| x * 2, Ok(1)),
1112		/// 	Ok(1)
1113		/// );
1114		/// ```
1115		fn map<'a, A: 'a, B: 'a>(
1116			func: impl Fn(A) -> B + 'a,
1117			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1118		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1119			match fa {
1120				Ok(t) => Ok(t),
1121				Err(e) => Err(func(e)),
1122			}
1123		}
1124	}
1125
1126	#[document_type_parameters("The success type.")]
1127	impl<T: Clone + 'static> Lift for ResultOkAppliedBrand<T> {
1128		/// Lifts a binary function into the result context (over error).
1129		///
1130		/// This method lifts a binary function to operate on error values within the result context.
1131		#[document_signature]
1132		///
1133		#[document_type_parameters(
1134			"The lifetime of the values.",
1135			"The type of the first error value.",
1136			"The type of the second error value.",
1137			"The type of the result error value."
1138		)]
1139		///
1140		#[document_parameters(
1141			"The binary function to apply to the errors.",
1142			"The first result.",
1143			"The second result."
1144		)]
1145		///
1146		#[document_returns(
1147			"`Err(f(a, b))` if both results are `Err`, otherwise the first success encountered."
1148		)]
1149		#[document_examples]
1150		///
1151		/// ```
1152		/// use fp_library::{
1153		/// 	brands::*,
1154		/// 	functions::*,
1155		/// };
1156		///
1157		/// assert_eq!(
1158		/// 	explicit::lift2::<ResultOkAppliedBrand<i32>, _, _, _, _, _, _>(
1159		/// 		|x: i32, y: i32| x + y,
1160		/// 		Err(1),
1161		/// 		Err(2)
1162		/// 	),
1163		/// 	Err(3)
1164		/// );
1165		/// assert_eq!(
1166		/// 	explicit::lift2::<ResultOkAppliedBrand<i32>, _, _, _, _, _, _>(
1167		/// 		|x: i32, y: i32| x + y,
1168		/// 		Err(1),
1169		/// 		Ok(2)
1170		/// 	),
1171		/// 	Ok(2)
1172		/// );
1173		/// assert_eq!(
1174		/// 	explicit::lift2::<ResultOkAppliedBrand<i32>, _, _, _, _, _, _>(
1175		/// 		|x: i32, y: i32| x + y,
1176		/// 		Ok(1),
1177		/// 		Err(2)
1178		/// 	),
1179		/// 	Ok(1)
1180		/// );
1181		/// assert_eq!(
1182		/// 	explicit::lift2::<ResultOkAppliedBrand<i32>, _, _, _, _, _, _>(
1183		/// 		|x: i32, y: i32| x + y,
1184		/// 		Ok(1),
1185		/// 		Ok(2)
1186		/// 	),
1187		/// 	Ok(1)
1188		/// );
1189		/// ```
1190		fn lift2<'a, A, B, C>(
1191			func: impl Fn(A, B) -> C + 'a,
1192			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1193			fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1194		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
1195		where
1196			A: Clone + 'a,
1197			B: Clone + 'a,
1198			C: 'a, {
1199			match (fa, fb) {
1200				(Err(a), Err(b)) => Err(func(a, b)),
1201				(Ok(t), _) => Ok(t),
1202				(_, Ok(t)) => Ok(t),
1203			}
1204		}
1205	}
1206
1207	#[document_type_parameters("The success type.")]
1208	impl<T: 'static> Pointed for ResultOkAppliedBrand<T> {
1209		/// Wraps a value in a result (as error).
1210		///
1211		/// This method wraps a value in the `Err` variant of a `Result`.
1212		#[document_signature]
1213		///
1214		#[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
1215		///
1216		#[document_parameters("The value to wrap.")]
1217		///
1218		#[document_returns("`Err(a)`.")]
1219		///
1220		#[document_examples]
1221		///
1222		/// ```
1223		/// use fp_library::{
1224		/// 	brands::ResultOkAppliedBrand,
1225		/// 	functions::*,
1226		/// };
1227		///
1228		/// assert_eq!(pure::<ResultOkAppliedBrand<()>, _>(5), Err(5));
1229		/// ```
1230		fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1231			Err(a)
1232		}
1233	}
1234
1235	#[document_type_parameters("The success type.")]
1236	impl<T: Clone + 'static> ApplyFirst for ResultOkAppliedBrand<T> {}
1237
1238	#[document_type_parameters("The success type.")]
1239	impl<T: Clone + 'static> ApplySecond for ResultOkAppliedBrand<T> {}
1240
1241	#[document_type_parameters("The success type.")]
1242	impl<T: Clone + 'static> Semiapplicative for ResultOkAppliedBrand<T> {
1243		/// Applies a wrapped function to a wrapped value (over error).
1244		///
1245		/// This method applies a function wrapped in a result (as error) to a value wrapped in a result (as error).
1246		#[document_signature]
1247		///
1248		#[document_type_parameters(
1249			"The lifetime of the values.",
1250			"The brand of the cloneable function wrapper.",
1251			"The type of the input value.",
1252			"The type of the output value."
1253		)]
1254		///
1255		#[document_parameters(
1256			"The result containing the function (in Err).",
1257			"The result containing the value (in Err)."
1258		)]
1259		///
1260		#[document_returns(
1261			"`Err(f(a))` if both are `Err`, otherwise the first success encountered."
1262		)]
1263		#[document_examples]
1264		///
1265		/// ```
1266		/// use fp_library::{
1267		/// 	Apply,
1268		/// 	Kind,
1269		/// 	brands::*,
1270		/// 	classes::semiapplicative::apply as explicit_apply,
1271		/// 	functions::*,
1272		/// };
1273		///
1274		/// let f: Result<(), _> = Err(lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
1275		/// assert_eq!(explicit_apply::<RcFnBrand, ResultOkAppliedBrand<()>, _, _>(f, Err(5)), Err(10));
1276		/// let f: Result<i32, _> = Err(lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
1277		/// assert_eq!(explicit_apply::<RcFnBrand, ResultOkAppliedBrand<i32>, _, _>(f, Ok(1)), Ok(1));
1278		///
1279		/// let f_ok: Result<i32, _> = Ok(1);
1280		/// assert_eq!(
1281		/// 	explicit_apply::<RcFnBrand, ResultOkAppliedBrand<i32>, i32, i32>(f_ok, Err(5)),
1282		/// 	Ok(1)
1283		/// );
1284		/// ```
1285		fn apply<'a, FnBrand: 'a + CloneFn, A: 'a + Clone, B: 'a>(
1286			ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, A, B>>),
1287			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1288		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1289			match (ff, fa) {
1290				(Err(f), Err(a)) => Err(f(a)),
1291				(Ok(t), _) => Ok(t),
1292				(_, Ok(t)) => Ok(t),
1293			}
1294		}
1295	}
1296
1297	#[document_type_parameters("The success type.")]
1298	impl<T: Clone + 'static> Semimonad for ResultOkAppliedBrand<T> {
1299		/// Chains result computations (over error).
1300		///
1301		/// This method chains two computations, where the second computation depends on the result of the first (over error).
1302		#[document_signature]
1303		///
1304		#[document_type_parameters(
1305			"The lifetime of the values.",
1306			"The type of the result of the first computation.",
1307			"The type of the result of the second computation."
1308		)]
1309		///
1310		#[document_parameters("The first result.", "The function to apply to the error value.")]
1311		///
1312		#[document_returns(
1313			"The result of applying `f` to the error if `ma` is `Err`, otherwise the original success."
1314		)]
1315		///
1316		#[document_examples]
1317		///
1318		/// ```
1319		/// use fp_library::{
1320		/// 	brands::ResultOkAppliedBrand,
1321		/// 	functions::*,
1322		/// };
1323		///
1324		/// assert_eq!(
1325		/// 	explicit::bind::<ResultOkAppliedBrand<()>, _, _, _, _>(Err(5), |x| Err(x * 2)),
1326		/// 	Err(10)
1327		/// );
1328		/// assert_eq!(
1329		/// 	explicit::bind::<ResultOkAppliedBrand<i32>, _, _, _, _>(Err(5), |_| Ok::<_, i32>(1)),
1330		/// 	Ok(1)
1331		/// );
1332		/// assert_eq!(
1333		/// 	explicit::bind::<ResultOkAppliedBrand<i32>, _, _, _, _>(Ok(1), |x: i32| Err(x * 2)),
1334		/// 	Ok(1)
1335		/// );
1336		/// ```
1337		fn bind<'a, A: 'a, B: 'a>(
1338			ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1339			func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1340		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1341			match ma {
1342				Ok(t) => Ok(t),
1343				Err(e) => func(e),
1344			}
1345		}
1346	}
1347
1348	#[document_type_parameters("The success type.")]
1349	impl<T: 'static> Foldable for ResultOkAppliedBrand<T> {
1350		/// Folds the result from the right (over error).
1351		///
1352		/// This method performs a right-associative fold of the result (over error).
1353		#[document_signature]
1354		///
1355		#[document_type_parameters(
1356			"The lifetime of the values.",
1357			"The brand of the cloneable function to use.",
1358			"The type of the elements in the structure.",
1359			"The type of the accumulator."
1360		)]
1361		///
1362		#[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
1363		///
1364		#[document_returns("`func(a, initial)` if `fa` is `Err(a)`, otherwise `initial`.")]
1365		///
1366		#[document_examples]
1367		///
1368		/// ```
1369		/// use fp_library::{
1370		/// 	brands::*,
1371		/// 	functions::*,
1372		/// };
1373		///
1374		/// assert_eq!(
1375		/// 	explicit::fold_right::<RcFnBrand, ResultOkAppliedBrand<i32>, _, _, _, _>(
1376		/// 		|x: i32, acc| x + acc,
1377		/// 		0,
1378		/// 		Err(1)
1379		/// 	),
1380		/// 	1
1381		/// );
1382		/// assert_eq!(
1383		/// 	explicit::fold_right::<RcFnBrand, ResultOkAppliedBrand<()>, _, _, _, _>(
1384		/// 		|x: i32, acc| x + acc,
1385		/// 		0,
1386		/// 		Ok(())
1387		/// 	),
1388		/// 	0
1389		/// );
1390		/// ```
1391		fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
1392			func: impl Fn(A, B) -> B + 'a,
1393			initial: B,
1394			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1395		) -> B
1396		where
1397			FnBrand: CloneFn + 'a, {
1398			match fa {
1399				Err(e) => func(e, initial),
1400				Ok(_) => initial,
1401			}
1402		}
1403
1404		/// Folds the result from the left (over error).
1405		///
1406		/// This method performs a left-associative fold of the result (over error).
1407		#[document_signature]
1408		///
1409		#[document_type_parameters(
1410			"The lifetime of the values.",
1411			"The brand of the cloneable function to use.",
1412			"The type of the elements in the structure.",
1413			"The type of the accumulator."
1414		)]
1415		///
1416		#[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
1417		///
1418		#[document_returns("`func(initial, a)` if `fa` is `Err(a)`, otherwise `initial`.")]
1419		///
1420		#[document_examples]
1421		///
1422		/// ```
1423		/// use fp_library::{
1424		/// 	brands::*,
1425		/// 	functions::*,
1426		/// };
1427		///
1428		/// assert_eq!(
1429		/// 	explicit::fold_left::<RcFnBrand, ResultOkAppliedBrand<()>, _, _, _, _>(
1430		/// 		|acc, x: i32| acc + x,
1431		/// 		0,
1432		/// 		Err(5)
1433		/// 	),
1434		/// 	5
1435		/// );
1436		/// assert_eq!(
1437		/// 	explicit::fold_left::<RcFnBrand, ResultOkAppliedBrand<i32>, _, _, _, _>(
1438		/// 		|acc, x: i32| acc + x,
1439		/// 		0,
1440		/// 		Ok(1)
1441		/// 	),
1442		/// 	0
1443		/// );
1444		/// ```
1445		fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
1446			func: impl Fn(B, A) -> B + 'a,
1447			initial: B,
1448			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1449		) -> B
1450		where
1451			FnBrand: CloneFn + 'a, {
1452			match fa {
1453				Err(e) => func(initial, e),
1454				Ok(_) => initial,
1455			}
1456		}
1457
1458		/// Maps the value to a monoid and returns it (over error).
1459		///
1460		/// This method maps the element of the result to a monoid and then returns it (over error).
1461		#[document_signature]
1462		///
1463		#[document_type_parameters(
1464			"The lifetime of the values.",
1465			"The brand of the cloneable function to use.",
1466			"The type of the elements in the structure.",
1467			"The type of the monoid."
1468		)]
1469		///
1470		#[document_parameters("The mapping function.", "The result to fold.")]
1471		///
1472		#[document_returns("`func(a)` if `fa` is `Err(a)`, otherwise `M::empty()`.")]
1473		///
1474		#[document_examples]
1475		///
1476		/// ```
1477		/// use fp_library::{
1478		/// 	brands::*,
1479		/// 	functions::*,
1480		/// };
1481		///
1482		/// assert_eq!(
1483		/// 	explicit::fold_map::<RcFnBrand, ResultOkAppliedBrand<()>, _, _, _, _>(
1484		/// 		|x: i32| x.to_string(),
1485		/// 		Err(5)
1486		/// 	),
1487		/// 	"5".to_string()
1488		/// );
1489		/// assert_eq!(
1490		/// 	explicit::fold_map::<RcFnBrand, ResultOkAppliedBrand<i32>, _, _, _, _>(
1491		/// 		|x: i32| x.to_string(),
1492		/// 		Ok(1)
1493		/// 	),
1494		/// 	"".to_string()
1495		/// );
1496		/// ```
1497		fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
1498			func: impl Fn(A) -> M + 'a,
1499			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1500		) -> M
1501		where
1502			M: Monoid + 'a,
1503			FnBrand: CloneFn + 'a, {
1504			match fa {
1505				Err(e) => func(e),
1506				Ok(_) => M::empty(),
1507			}
1508		}
1509	}
1510
1511	#[document_type_parameters("The success type.")]
1512	impl<T: Clone + 'static> Traversable for ResultOkAppliedBrand<T> {
1513		/// Traverses the result with an applicative function (over error).
1514		///
1515		/// This method maps the element of the result to a computation, evaluates it, and combines the result into an applicative context (over error).
1516		#[document_signature]
1517		///
1518		#[document_type_parameters(
1519			"The lifetime of the values.",
1520			"The type of the elements in the traversable structure.",
1521			"The type of the elements in the resulting traversable structure.",
1522			"The applicative context."
1523		)]
1524		///
1525		#[document_parameters("The function to apply.", "The result to traverse.")]
1526		///
1527		#[document_returns("The result wrapped in the applicative context.")]
1528		///
1529		#[document_examples]
1530		///
1531		/// ```
1532		/// use fp_library::{
1533		/// 	brands::*,
1534		/// 	functions::*,
1535		/// };
1536		///
1537		/// assert_eq!(
1538		/// 	explicit::traverse::<RcFnBrand, ResultOkAppliedBrand<()>, _, _, OptionBrand, _, _>(
1539		/// 		|x| Some(x * 2),
1540		/// 		Err(5)
1541		/// 	),
1542		/// 	Some(Err(10))
1543		/// );
1544		/// assert_eq!(
1545		/// 	explicit::traverse::<RcFnBrand, ResultOkAppliedBrand<i32>, _, _, OptionBrand, _, _>(
1546		/// 		|x: i32| Some(x * 2),
1547		/// 		Ok(1)
1548		/// 	),
1549		/// 	Some(Ok(1))
1550		/// );
1551		/// assert_eq!(
1552		/// 	explicit::traverse::<RcFnBrand, ResultOkAppliedBrand<()>, _, _, OptionBrand, _, _>(
1553		/// 		|_| None::<i32>,
1554		/// 		Err(5)
1555		/// 	),
1556		/// 	None
1557		/// );
1558		/// ```
1559		fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1560			func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1561			ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1562		) -> 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>)>)
1563		where
1564			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1565			match ta {
1566				Err(e) => F::map(|b| Err(b), func(e)),
1567				Ok(t) => F::pure(Ok(t)),
1568			}
1569		}
1570
1571		/// Sequences a result of applicative (over error).
1572		///
1573		/// This method evaluates the computation inside the result and accumulates the result into an applicative context (over error).
1574		#[document_signature]
1575		///
1576		#[document_type_parameters(
1577			"The lifetime of the values.",
1578			"The type of the elements in the traversable structure.",
1579			"The applicative context."
1580		)]
1581		///
1582		#[document_parameters("The result containing the applicative value.")]
1583		///
1584		#[document_returns("The result wrapped in the applicative context.")]
1585		///
1586		#[document_examples]
1587		///
1588		/// ```
1589		/// use fp_library::{
1590		/// 	brands::{
1591		/// 		OptionBrand,
1592		/// 		ResultOkAppliedBrand,
1593		/// 	},
1594		/// 	functions::*,
1595		/// };
1596		///
1597		/// assert_eq!(sequence::<ResultOkAppliedBrand<()>, _, OptionBrand>(Err(Some(5))), Some(Err(5)));
1598		/// assert_eq!(
1599		/// 	sequence::<ResultOkAppliedBrand<i32>, i32, OptionBrand>(Ok::<_, Option<i32>>(1)),
1600		/// 	Some(Ok::<i32, i32>(1))
1601		/// );
1602		/// assert_eq!(sequence::<ResultOkAppliedBrand<()>, _, OptionBrand>(Err(None::<i32>)), None);
1603		/// ```
1604		fn sequence<'a, A: 'a + Clone, F: Applicative>(
1605			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>)>)
1606		) -> 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>)>)
1607		where
1608			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1609			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
1610			match ta {
1611				Err(fe) => F::map(|e| Err(e), fe),
1612				Ok(t) => F::pure(Ok(t)),
1613			}
1614		}
1615	}
1616
1617	/// [`MonadRec`] implementation for [`ResultOkAppliedBrand`].
1618	#[document_type_parameters("The success type.")]
1619	impl<T: Clone + 'static> MonadRec for ResultOkAppliedBrand<T> {
1620		/// Performs tail-recursive monadic computation over [`Result`] (error channel).
1621		///
1622		/// Iteratively applies the step function. If the function returns [`Ok`],
1623		/// the computation short-circuits with that success value. If it returns
1624		/// `Err(ControlFlow::Continue(a))`, the loop continues with the new state. If it returns
1625		/// `Err(ControlFlow::Break(b))`, the computation completes with `Err(b)`.
1626		#[document_signature]
1627		///
1628		#[document_type_parameters(
1629			"The lifetime of the computation.",
1630			"The type of the initial value and loop state.",
1631			"The type of the result."
1632		)]
1633		///
1634		#[document_parameters("The step function.", "The initial value.")]
1635		///
1636		#[document_returns(
1637			"The result of the computation, or a success if the step function returned `Ok`."
1638		)]
1639		///
1640		#[document_examples]
1641		///
1642		/// ```
1643		/// use {
1644		/// 	core::ops::ControlFlow,
1645		/// 	fp_library::{
1646		/// 		brands::*,
1647		/// 		functions::*,
1648		/// 		types::*,
1649		/// 	},
1650		/// };
1651		///
1652		/// let result = tail_rec_m::<ResultOkAppliedBrand<&str>, _, _>(
1653		/// 	|n| {
1654		/// 		if n < 10 { Err(ControlFlow::Continue(n + 1)) } else { Err(ControlFlow::Break(n)) }
1655		/// 	},
1656		/// 	0,
1657		/// );
1658		/// assert_eq!(result, Err(10));
1659		/// ```
1660		fn tail_rec_m<'a, A: 'a, B: 'a>(
1661			func: impl Fn(
1662				A,
1663			)
1664				-> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
1665			+ 'a,
1666			initial: A,
1667		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1668			let mut current = initial;
1669			loop {
1670				match func(current) {
1671					Ok(t) => return Ok(t),
1672					Err(ControlFlow::Continue(next)) => current = next,
1673					Err(ControlFlow::Break(b)) => return Err(b),
1674				}
1675			}
1676		}
1677	}
1678
1679	// -- By-reference trait implementations for ResultErrAppliedBrand --
1680
1681	#[document_type_parameters("The error type.")]
1682	impl<E: Clone + 'static> RefFunctor for ResultErrAppliedBrand<E> {
1683		/// Maps a function over the result by reference.
1684		#[document_signature]
1685		#[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1686		#[document_parameters("The function.", "The result.")]
1687		#[document_returns("The mapped result.")]
1688		#[document_examples]
1689		///
1690		/// ```
1691		/// use fp_library::{
1692		/// 	brands::*,
1693		/// 	functions::*,
1694		/// };
1695		/// assert_eq!(
1696		/// 	explicit::map::<ResultErrAppliedBrand<()>, _, _, _, _>(|x: &i32| *x * 2, &Ok(5)),
1697		/// 	Ok(10)
1698		/// );
1699		/// assert_eq!(
1700		/// 	explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(
1701		/// 		|x: &i32| *x * 2,
1702		/// 		&Err::<i32, _>(1)
1703		/// 	),
1704		/// 	Err(1)
1705		/// );
1706		/// ```
1707		fn ref_map<'a, A: 'a, B: 'a>(
1708			func: impl Fn(&A) -> B + 'a,
1709			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1710		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1711			match fa {
1712				Ok(a) => Ok(func(a)),
1713				Err(e) => Err(e.clone()),
1714			}
1715		}
1716	}
1717
1718	#[document_type_parameters("The error type.")]
1719	impl<E: Clone + 'static> RefFoldable for ResultErrAppliedBrand<E> {
1720		/// Folds the result by reference.
1721		#[document_signature]
1722		#[document_type_parameters(
1723			"The lifetime.",
1724			"The brand.",
1725			"The element type.",
1726			"The monoid type."
1727		)]
1728		#[document_parameters("The mapping function.", "The result.")]
1729		#[document_returns("The monoid value.")]
1730		#[document_examples]
1731		///
1732		/// ```
1733		/// use fp_library::{
1734		/// 	brands::*,
1735		/// 	functions::*,
1736		/// };
1737		/// let result = explicit::fold_map::<RcFnBrand, ResultErrAppliedBrand<()>, _, _, _, _>(
1738		/// 	|x: &i32| x.to_string(),
1739		/// 	&Ok(5),
1740		/// );
1741		/// assert_eq!(result, "5");
1742		/// ```
1743		fn ref_fold_map<'a, FnBrand, A: 'a + Clone, M>(
1744			func: impl Fn(&A) -> M + 'a,
1745			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1746		) -> M
1747		where
1748			FnBrand: LiftFn + 'a,
1749			M: Monoid + 'a, {
1750			match fa {
1751				Ok(a) => func(a),
1752				Err(_) => Monoid::empty(),
1753			}
1754		}
1755	}
1756
1757	#[document_type_parameters("The error type.")]
1758	impl<E: Clone + 'static> RefTraversable for ResultErrAppliedBrand<E> {
1759		/// Traverses the result by reference.
1760		#[document_signature]
1761		#[document_type_parameters(
1762			"The lifetime.",
1763			"The brand.",
1764			"The input type.",
1765			"The output type.",
1766			"The applicative."
1767		)]
1768		#[document_parameters("The function.", "The result.")]
1769		#[document_returns("The traversed result.")]
1770		#[document_examples]
1771		///
1772		/// ```
1773		/// use fp_library::{
1774		/// 	brands::*,
1775		/// 	functions::*,
1776		/// };
1777		/// let result: Vec<Result<String, ()>> =
1778		/// 	ref_traverse::<ResultErrAppliedBrand<()>, RcFnBrand, _, _, VecBrand>(
1779		/// 		|x: &i32| vec![x.to_string()],
1780		/// 		&Ok(5),
1781		/// 	);
1782		/// assert_eq!(result, vec![Ok("5".to_string())]);
1783		/// ```
1784		fn ref_traverse<'a, FnBrand, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1785			func: impl Fn(&A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1786			ta: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1787		) -> 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>)>)
1788		where
1789			FnBrand: LiftFn + 'a,
1790			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
1791			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1792			match ta {
1793				Ok(a) => F::map(Ok, func(a)),
1794				Err(e) => F::pure(Err(e.clone())),
1795			}
1796		}
1797	}
1798
1799	#[document_type_parameters("The error type.")]
1800	impl<E: 'static> RefPointed for ResultErrAppliedBrand<E> {
1801		/// Creates an `Ok` from a reference by cloning.
1802		#[document_signature]
1803		#[document_type_parameters("The lifetime of the value.", "The type of the value.")]
1804		#[document_parameters("The reference to the value to wrap.")]
1805		#[document_returns("An `Ok` containing a clone of the value.")]
1806		#[document_examples]
1807		///
1808		/// ```
1809		/// use fp_library::{
1810		/// 	brands::*,
1811		/// 	functions::*,
1812		/// };
1813		///
1814		/// let x = 42;
1815		/// let result: Result<i32, ()> = ref_pure::<ResultErrAppliedBrand<()>, _>(&x);
1816		/// assert_eq!(result, Ok(42));
1817		/// ```
1818		fn ref_pure<'a, A: Clone + 'a>(
1819			a: &A
1820		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1821			Ok(a.clone())
1822		}
1823	}
1824
1825	#[document_type_parameters("The error type.")]
1826	impl<E: Clone + 'static> RefLift for ResultErrAppliedBrand<E> {
1827		/// Combines two `Result` values with a by-reference binary function.
1828		#[document_signature]
1829		#[document_type_parameters(
1830			"The lifetime.",
1831			"First input type.",
1832			"Second input type.",
1833			"Output type."
1834		)]
1835		#[document_parameters("The binary function.", "The first result.", "The second result.")]
1836		#[document_returns("The combined result, or the first error encountered.")]
1837		#[document_examples]
1838		///
1839		/// ```
1840		/// use fp_library::{
1841		/// 	brands::*,
1842		/// 	functions::*,
1843		/// };
1844		///
1845		/// let result = explicit::lift2::<ResultErrAppliedBrand<()>, _, _, _, _, _, _>(
1846		/// 	|a: &i32, b: &i32| *a + *b,
1847		/// 	&Ok(1),
1848		/// 	&Ok(2),
1849		/// );
1850		/// assert_eq!(result, Ok(3));
1851		/// ```
1852		fn ref_lift2<'a, A: 'a, B: 'a, C: 'a>(
1853			func: impl Fn(&A, &B) -> C + 'a,
1854			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1855			fb: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1856		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) {
1857			match (fa, fb) {
1858				(Ok(a), Ok(b)) => Ok(func(a, b)),
1859				(Err(e), _) => Err(e.clone()),
1860				(_, Err(e)) => Err(e.clone()),
1861			}
1862		}
1863	}
1864
1865	#[document_type_parameters("The error type.")]
1866	impl<E: Clone + 'static> RefSemiapplicative for ResultErrAppliedBrand<E> {
1867		/// Applies a wrapped by-ref function to a `Result` value.
1868		#[document_signature]
1869		#[document_type_parameters(
1870			"The lifetime.",
1871			"The function brand.",
1872			"The input type.",
1873			"The output type."
1874		)]
1875		#[document_parameters(
1876			"The result containing the by-ref function.",
1877			"The result containing the value."
1878		)]
1879		#[document_returns("The result of applying the function, or the first error.")]
1880		#[document_examples]
1881		///
1882		/// ```
1883		/// use fp_library::{
1884		/// 	brands::*,
1885		/// 	classes::*,
1886		/// 	functions::*,
1887		/// };
1888		///
1889		/// let f: Result<std::rc::Rc<dyn Fn(&i32) -> i32>, ()> = Ok(std::rc::Rc::new(|x: &i32| *x + 1));
1890		/// let result = ref_apply::<RcFnBrand, ResultErrAppliedBrand<()>, _, _>(&f, &Ok(5));
1891		/// assert_eq!(result, Ok(6));
1892		/// ```
1893		fn ref_apply<'a, FnBrand: 'a + CloneFn<Ref>, A: 'a, B: 'a>(
1894			ff: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn<Ref>>::Of<'a, A, B>>),
1895			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1896		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1897			match (ff, fa) {
1898				(Ok(f), Ok(a)) => Ok((**f)(a)),
1899				(Err(e), _) => Err(e.clone()),
1900				(_, Err(e)) => Err(e.clone()),
1901			}
1902		}
1903	}
1904
1905	#[document_type_parameters("The error type.")]
1906	impl<E: Clone + 'static> RefSemimonad for ResultErrAppliedBrand<E> {
1907		/// Chains `Result` computations by reference.
1908		#[document_signature]
1909		#[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1910		#[document_parameters("The input result.", "The function to apply by reference.")]
1911		#[document_returns("The result of applying the function, or the original error.")]
1912		#[document_examples]
1913		///
1914		/// ```
1915		/// use fp_library::{
1916		/// 	brands::*,
1917		/// 	functions::*,
1918		/// };
1919		///
1920		/// let result: Result<String, ()> =
1921		/// 	explicit::bind::<ResultErrAppliedBrand<()>, _, _, _, _>(&Ok(42), |x: &i32| {
1922		/// 		Ok(x.to_string())
1923		/// 	});
1924		/// assert_eq!(result, Ok("42".to_string()));
1925		/// ```
1926		fn ref_bind<'a, A: 'a, B: 'a>(
1927			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1928			f: impl Fn(&A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1929		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1930			match fa {
1931				Ok(a) => f(a),
1932				Err(e) => Err(e.clone()),
1933			}
1934		}
1935	}
1936
1937	// -- By-reference trait implementations for ResultOkAppliedBrand --
1938
1939	#[document_type_parameters("The success type.")]
1940	impl<T: Clone + 'static> RefFunctor for ResultOkAppliedBrand<T> {
1941		/// Maps a function over the error value in the result by reference.
1942		#[document_signature]
1943		#[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1944		#[document_parameters("The function.", "The result.")]
1945		#[document_returns("The mapped result.")]
1946		#[document_examples]
1947		///
1948		/// ```
1949		/// use fp_library::{
1950		/// 	brands::*,
1951		/// 	functions::*,
1952		/// };
1953		/// assert_eq!(
1954		/// 	explicit::map::<ResultOkAppliedBrand<i32>, _, _, _, _>(|x: &i32| *x * 2, &Err(5)),
1955		/// 	Err(10)
1956		/// );
1957		/// assert_eq!(
1958		/// 	explicit::map::<ResultOkAppliedBrand<i32>, _, _, _, _>(|x: &i32| *x * 2, &Ok::<_, i32>(1)),
1959		/// 	Ok(1)
1960		/// );
1961		/// ```
1962		fn ref_map<'a, A: 'a, B: 'a>(
1963			func: impl Fn(&A) -> B + 'a,
1964			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1965		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1966			match fa {
1967				Err(e) => Err(func(e)),
1968				Ok(t) => Ok(t.clone()),
1969			}
1970		}
1971	}
1972
1973	#[document_type_parameters("The success type.")]
1974	impl<T: Clone + 'static> RefFoldable for ResultOkAppliedBrand<T> {
1975		/// Folds the result by reference (over error).
1976		#[document_signature]
1977		#[document_type_parameters(
1978			"The lifetime.",
1979			"The brand.",
1980			"The element type.",
1981			"The monoid type."
1982		)]
1983		#[document_parameters("The mapping function.", "The result.")]
1984		#[document_returns("The monoid value.")]
1985		#[document_examples]
1986		///
1987		/// ```
1988		/// use fp_library::{
1989		/// 	brands::*,
1990		/// 	functions::*,
1991		/// };
1992		/// let result = explicit::fold_map::<RcFnBrand, ResultOkAppliedBrand<()>, _, _, _, _>(
1993		/// 	|x: &i32| x.to_string(),
1994		/// 	&Err(5),
1995		/// );
1996		/// assert_eq!(result, "5");
1997		/// ```
1998		fn ref_fold_map<'a, FnBrand, A: 'a + Clone, M>(
1999			func: impl Fn(&A) -> M + 'a,
2000			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2001		) -> M
2002		where
2003			FnBrand: LiftFn + 'a,
2004			M: Monoid + 'a, {
2005			match fa {
2006				Err(e) => func(e),
2007				Ok(_) => Monoid::empty(),
2008			}
2009		}
2010	}
2011
2012	#[document_type_parameters("The success type.")]
2013	impl<T: Clone + 'static> RefTraversable for ResultOkAppliedBrand<T> {
2014		/// Traverses the result by reference (over error).
2015		#[document_signature]
2016		#[document_type_parameters(
2017			"The lifetime.",
2018			"The brand.",
2019			"The input type.",
2020			"The output type.",
2021			"The applicative."
2022		)]
2023		#[document_parameters("The function.", "The result.")]
2024		#[document_returns("The traversed result.")]
2025		#[document_examples]
2026		///
2027		/// ```
2028		/// use fp_library::{
2029		/// 	brands::*,
2030		/// 	functions::*,
2031		/// };
2032		/// let result: Vec<Result<(), String>> =
2033		/// 	ref_traverse::<ResultOkAppliedBrand<()>, RcFnBrand, _, _, VecBrand>(
2034		/// 		|x: &i32| vec![x.to_string()],
2035		/// 		&Err(5),
2036		/// 	);
2037		/// assert_eq!(result, vec![Err("5".to_string())]);
2038		/// ```
2039		fn ref_traverse<'a, FnBrand, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
2040			func: impl Fn(&A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
2041			ta: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2042		) -> 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>)>)
2043		where
2044			FnBrand: LiftFn + 'a,
2045			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
2046			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
2047			match ta {
2048				Err(e) => F::map(Err, func(e)),
2049				Ok(t) => F::pure(Ok(t.clone())),
2050			}
2051		}
2052	}
2053
2054	#[document_type_parameters("The success type.")]
2055	impl<T: 'static> RefPointed for ResultOkAppliedBrand<T> {
2056		/// Creates an `Err` from a reference by cloning.
2057		#[document_signature]
2058		#[document_type_parameters("The lifetime of the value.", "The type of the value.")]
2059		#[document_parameters("The reference to the value to wrap.")]
2060		#[document_returns("An `Err` containing a clone of the value.")]
2061		#[document_examples]
2062		///
2063		/// ```
2064		/// use fp_library::{
2065		/// 	brands::*,
2066		/// 	functions::*,
2067		/// };
2068		///
2069		/// let x = 42;
2070		/// let result: Result<(), i32> = ref_pure::<ResultOkAppliedBrand<()>, _>(&x);
2071		/// assert_eq!(result, Err(42));
2072		/// ```
2073		fn ref_pure<'a, A: Clone + 'a>(
2074			a: &A
2075		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
2076			Err(a.clone())
2077		}
2078	}
2079
2080	#[document_type_parameters("The success type.")]
2081	impl<T: Clone + 'static> RefLift for ResultOkAppliedBrand<T> {
2082		/// Combines two `Result` values with a by-reference binary function (over error).
2083		#[document_signature]
2084		#[document_type_parameters(
2085			"The lifetime.",
2086			"First input type.",
2087			"Second input type.",
2088			"Output type."
2089		)]
2090		#[document_parameters("The binary function.", "The first result.", "The second result.")]
2091		#[document_returns("The combined result, or the first success encountered.")]
2092		#[document_examples]
2093		///
2094		/// ```
2095		/// use fp_library::{
2096		/// 	brands::*,
2097		/// 	functions::*,
2098		/// };
2099		///
2100		/// let result = explicit::lift2::<ResultOkAppliedBrand<i32>, _, _, _, _, _, _>(
2101		/// 	|a: &i32, b: &i32| *a + *b,
2102		/// 	&Err(1),
2103		/// 	&Err(2),
2104		/// );
2105		/// assert_eq!(result, Err(3));
2106		/// ```
2107		fn ref_lift2<'a, A: 'a, B: 'a, C: 'a>(
2108			func: impl Fn(&A, &B) -> C + 'a,
2109			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2110			fb: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
2111		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) {
2112			match (fa, fb) {
2113				(Err(a), Err(b)) => Err(func(a, b)),
2114				(Ok(t), _) => Ok(t.clone()),
2115				(_, Ok(t)) => Ok(t.clone()),
2116			}
2117		}
2118	}
2119
2120	#[document_type_parameters("The success type.")]
2121	impl<T: Clone + 'static> RefSemiapplicative for ResultOkAppliedBrand<T> {
2122		/// Applies a wrapped by-ref function to a `Result` value (over error).
2123		#[document_signature]
2124		#[document_type_parameters(
2125			"The lifetime.",
2126			"The function brand.",
2127			"The input type.",
2128			"The output type."
2129		)]
2130		#[document_parameters(
2131			"The result containing the by-ref function (in Err).",
2132			"The result containing the value (in Err)."
2133		)]
2134		#[document_returns("The result of applying the function, or the first success.")]
2135		#[document_examples]
2136		///
2137		/// ```
2138		/// use fp_library::{
2139		/// 	brands::*,
2140		/// 	classes::*,
2141		/// 	functions::*,
2142		/// };
2143		///
2144		/// let f: Result<(), std::rc::Rc<dyn Fn(&i32) -> i32>> = Err(std::rc::Rc::new(|x: &i32| *x + 1));
2145		/// let result = ref_apply::<RcFnBrand, ResultOkAppliedBrand<()>, _, _>(&f, &Err(5));
2146		/// assert_eq!(result, Err(6));
2147		/// ```
2148		fn ref_apply<'a, FnBrand: 'a + CloneFn<Ref>, A: 'a, B: 'a>(
2149			ff: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn<Ref>>::Of<'a, A, B>>),
2150			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2151		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2152			match (ff, fa) {
2153				(Err(f), Err(a)) => Err((**f)(a)),
2154				(Ok(t), _) => Ok(t.clone()),
2155				(_, Ok(t)) => Ok(t.clone()),
2156			}
2157		}
2158	}
2159
2160	#[document_type_parameters("The success type.")]
2161	impl<T: Clone + 'static> RefSemimonad for ResultOkAppliedBrand<T> {
2162		/// Chains `Result` computations by reference (over error).
2163		#[document_signature]
2164		#[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
2165		#[document_parameters("The input result.", "The function to apply by reference.")]
2166		#[document_returns("The result of applying the function, or the original success.")]
2167		#[document_examples]
2168		///
2169		/// ```
2170		/// use fp_library::{
2171		/// 	brands::*,
2172		/// 	functions::*,
2173		/// };
2174		///
2175		/// let result: Result<(), String> =
2176		/// 	explicit::bind::<ResultOkAppliedBrand<()>, _, _, _, _>(&Err(42), |x: &i32| {
2177		/// 		Err(x.to_string())
2178		/// 	});
2179		/// assert_eq!(result, Err("42".to_string()));
2180		/// ```
2181		fn ref_bind<'a, A: 'a, B: 'a>(
2182			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2183			f: impl Fn(&A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
2184		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2185			match fa {
2186				Err(e) => f(e),
2187				Ok(t) => Ok(t.clone()),
2188			}
2189		}
2190	}
2191
2192	/// [`MonadRec`] implementation for [`ResultErrAppliedBrand`].
2193	#[document_type_parameters("The error type.")]
2194	impl<E: Clone + 'static> MonadRec for ResultErrAppliedBrand<E> {
2195		/// Performs tail-recursive monadic computation over [`Result`].
2196		///
2197		/// Iteratively applies the step function. If the function returns [`Err`],
2198		/// the computation short-circuits with that error. If it returns
2199		/// `Ok(ControlFlow::Continue(a))`, the loop continues with the new state. If it returns
2200		/// `Ok(ControlFlow::Break(b))`, the computation completes with `Ok(b)`.
2201		#[document_signature]
2202		///
2203		#[document_type_parameters(
2204			"The lifetime of the computation.",
2205			"The type of the initial value and loop state.",
2206			"The type of the result."
2207		)]
2208		///
2209		#[document_parameters("The step function.", "The initial value.")]
2210		///
2211		#[document_returns(
2212			"The result of the computation, or an error if the step function returned `Err`."
2213		)]
2214		///
2215		#[document_examples]
2216		///
2217		/// ```
2218		/// use {
2219		/// 	core::ops::ControlFlow,
2220		/// 	fp_library::{
2221		/// 		brands::*,
2222		/// 		functions::*,
2223		/// 		types::*,
2224		/// 	},
2225		/// };
2226		///
2227		/// let result = tail_rec_m::<ResultErrAppliedBrand<&str>, _, _>(
2228		/// 	|n| {
2229		/// 		if n < 10 { Ok(ControlFlow::Continue(n + 1)) } else { Ok(ControlFlow::Break(n)) }
2230		/// 	},
2231		/// 	0,
2232		/// );
2233		/// assert_eq!(result, Ok(10));
2234		/// ```
2235		fn tail_rec_m<'a, A: 'a, B: 'a>(
2236			func: impl Fn(
2237				A,
2238			)
2239				-> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
2240			+ 'a,
2241			initial: A,
2242		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2243			let mut current = initial;
2244			loop {
2245				match func(current) {
2246					Err(e) => return Err(e),
2247					Ok(ControlFlow::Continue(next)) => current = next,
2248					Ok(ControlFlow::Break(b)) => return Ok(b),
2249				}
2250			}
2251		}
2252	}
2253}
2254
2255#[cfg(test)]
2256mod tests {
2257
2258	use {
2259		crate::{
2260			brands::*,
2261			classes::{
2262				semiapplicative::apply as explicit_apply,
2263				*,
2264			},
2265			functions::*,
2266		},
2267		quickcheck_macros::quickcheck,
2268	};
2269
2270	// Bifunctor Tests
2271
2272	/// Tests `bimap` on `Ok` and `Err`.
2273	#[test]
2274	fn test_bimap() {
2275		let x: Result<i32, i32> = Ok(5);
2276		assert_eq!(
2277			explicit::bimap::<ResultBrand, _, _, _, _, _, _>((|e| e + 1, |s| s * 2), x),
2278			Ok(10)
2279		);
2280
2281		let y: Result<i32, i32> = Err(5);
2282		assert_eq!(
2283			explicit::bimap::<ResultBrand, _, _, _, _, _, _>((|e| e + 1, |s| s * 2), y),
2284			Err(6)
2285		);
2286	}
2287
2288	// Bifunctor Laws
2289
2290	/// Tests the identity law for Bifunctor.
2291	#[quickcheck]
2292	fn bifunctor_identity(x: Result<i32, i32>) -> bool {
2293		explicit::bimap::<ResultBrand, _, _, _, _, _, _>((identity, identity), x) == x
2294	}
2295
2296	/// Tests the composition law for Bifunctor.
2297	#[quickcheck]
2298	fn bifunctor_composition(x: Result<i32, i32>) -> bool {
2299		let f = |x: i32| x.wrapping_add(1);
2300		let g = |x: i32| x.wrapping_mul(2);
2301		let h = |x: i32| x.wrapping_sub(1);
2302		let i = |x: i32| if x == 0 { 0 } else { x.wrapping_div(2) };
2303
2304		explicit::bimap::<ResultBrand, _, _, _, _, _, _>((compose(f, g), compose(h, i)), x)
2305			== explicit::bimap::<ResultBrand, _, _, _, _, _, _>(
2306				(f, h),
2307				explicit::bimap::<ResultBrand, _, _, _, _, _, _>((g, i), x),
2308			)
2309	}
2310
2311	// Functor Laws
2312
2313	/// Tests the identity law for Functor.
2314	#[quickcheck]
2315	fn functor_identity(x: Result<i32, i32>) -> bool {
2316		explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(identity, x) == x
2317	}
2318
2319	/// Tests the composition law for Functor.
2320	#[quickcheck]
2321	fn functor_composition(x: Result<i32, i32>) -> bool {
2322		let f = |x: i32| x.wrapping_add(1);
2323		let g = |x: i32| x.wrapping_mul(2);
2324		explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(compose(f, g), x)
2325			== explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2326				f,
2327				explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(g, x),
2328			)
2329	}
2330
2331	// Applicative Laws
2332
2333	/// Tests the identity law for Applicative.
2334	#[quickcheck]
2335	fn applicative_identity(v: Result<i32, i32>) -> bool {
2336		explicit_apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
2337			pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(identity)),
2338			v,
2339		) == v
2340	}
2341
2342	/// Tests the homomorphism law for Applicative.
2343	#[quickcheck]
2344	fn applicative_homomorphism(x: i32) -> bool {
2345		let f = |x: i32| x.wrapping_mul(2);
2346		explicit_apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
2347			pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(f)),
2348			pure::<ResultErrAppliedBrand<i32>, _>(x),
2349		) == pure::<ResultErrAppliedBrand<i32>, _>(f(x))
2350	}
2351
2352	/// Tests the composition law for Applicative.
2353	#[quickcheck]
2354	fn applicative_composition(
2355		w: Result<i32, i32>,
2356		u_is_ok: bool,
2357		v_is_ok: bool,
2358	) -> bool {
2359		let v_fn = |x: i32| x.wrapping_mul(2);
2360		let u_fn = |x: i32| x.wrapping_add(1);
2361
2362		let v = if v_is_ok {
2363			pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(v_fn))
2364		} else {
2365			Err(100)
2366		};
2367		let u = if u_is_ok {
2368			pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(u_fn))
2369		} else {
2370			Err(200)
2371		};
2372
2373		// RHS: u <*> (v <*> w)
2374		let vw = explicit_apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(v.clone(), w);
2375		let rhs = explicit_apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(u.clone(), vw);
2376
2377		// LHS: pure(compose) <*> u <*> v <*> w
2378		// equivalent to (u . v) <*> w
2379		let uv = match (u, v) {
2380			(Ok(uf), Ok(vf)) => {
2381				let composed = move |x| uf(vf(x));
2382				Ok(<RcFnBrand as LiftFn>::new(composed))
2383			}
2384			(Err(e), _) => Err(e),
2385			(_, Err(e)) => Err(e),
2386		};
2387
2388		let lhs = explicit_apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(uv, w);
2389
2390		lhs == rhs
2391	}
2392
2393	/// Tests the interchange law for Applicative.
2394	#[quickcheck]
2395	fn applicative_interchange(y: i32) -> bool {
2396		// u <*> pure y = pure ($ y) <*> u
2397		let f = |x: i32| x.wrapping_mul(2);
2398		let u = pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(f));
2399
2400		let lhs = explicit_apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
2401			u.clone(),
2402			pure::<ResultErrAppliedBrand<i32>, _>(y),
2403		);
2404
2405		let rhs_fn = <RcFnBrand as LiftFn>::new(move |f: std::rc::Rc<dyn Fn(i32) -> i32>| f(y));
2406		let rhs = explicit_apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
2407			pure::<ResultErrAppliedBrand<i32>, _>(rhs_fn),
2408			u,
2409		);
2410
2411		lhs == rhs
2412	}
2413
2414	// Monad Laws
2415
2416	/// Tests the left identity law for Monad.
2417	#[quickcheck]
2418	fn monad_left_identity(a: i32) -> bool {
2419		let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
2420		explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2421			pure::<ResultErrAppliedBrand<i32>, _>(a),
2422			f,
2423		) == f(a)
2424	}
2425
2426	/// Tests the right identity law for Monad.
2427	#[quickcheck]
2428	fn monad_right_identity(m: Result<i32, i32>) -> bool {
2429		explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2430			m,
2431			pure::<ResultErrAppliedBrand<i32>, _>,
2432		) == m
2433	}
2434
2435	/// Tests the associativity law for Monad.
2436	#[quickcheck]
2437	fn monad_associativity(m: Result<i32, i32>) -> bool {
2438		let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
2439		let g = |x: i32| -> Result<i32, i32> { Err(x.wrapping_add(1)) };
2440		explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2441			explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(m, f),
2442			g,
2443		) == explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(m, |x| {
2444			explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(f(x), g)
2445		})
2446	}
2447
2448	// Edge Cases
2449
2450	/// Tests `map` on `Err`.
2451	#[test]
2452	fn map_err() {
2453		assert_eq!(
2454			explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2455				|x: i32| x + 1,
2456				Err::<i32, i32>(1)
2457			),
2458			Err(1)
2459		);
2460	}
2461
2462	/// Tests `bind` on `Err`.
2463	#[test]
2464	fn bind_err() {
2465		assert_eq!(
2466			explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2467				Err::<i32, i32>(1),
2468				|x: i32| Ok(x + 1)
2469			),
2470			Err(1)
2471		);
2472	}
2473
2474	/// Tests `bind` returning `Err`.
2475	#[test]
2476	fn bind_returning_err() {
2477		assert_eq!(
2478			explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(Ok(1), |_| Err::<i32, i32>(2)),
2479			Err(2)
2480		);
2481	}
2482
2483	/// Tests `fold_right` on `Err`.
2484	#[test]
2485	fn fold_right_err() {
2486		assert_eq!(
2487			crate::functions::explicit::fold_right::<
2488				RcFnBrand,
2489				ResultErrAppliedBrand<i32>,
2490				_,
2491				_,
2492				_,
2493				_,
2494			>(|x: i32, acc| x + acc, 0, Err(1)),
2495			0
2496		);
2497	}
2498
2499	/// Tests `fold_left` on `Err`.
2500	#[test]
2501	fn fold_left_err() {
2502		assert_eq!(
2503			crate::functions::explicit::fold_left::<
2504				RcFnBrand,
2505				ResultErrAppliedBrand<i32>,
2506				_,
2507				_,
2508				_,
2509				_,
2510			>(|acc, x: i32| acc + x, 0, Err(1)),
2511			0
2512		);
2513	}
2514
2515	/// Tests `traverse` on `Err`.
2516	#[test]
2517	fn traverse_err() {
2518		assert_eq!(
2519			crate::classes::traversable::traverse::<ResultErrAppliedBrand<i32>, _, _, OptionBrand>(
2520				|x: i32| Some(x + 1),
2521				Err(1)
2522			),
2523			Some(Err(1))
2524		);
2525	}
2526
2527	/// Tests `traverse` returning `Err`.
2528	#[test]
2529	fn traverse_returning_err() {
2530		assert_eq!(
2531			crate::classes::traversable::traverse::<ResultErrAppliedBrand<i32>, _, _, OptionBrand>(
2532				|_: i32| None::<i32>,
2533				Ok(1)
2534			),
2535			None
2536		);
2537	}
2538
2539	// MonadRec tests
2540
2541	/// Tests the MonadRec identity law: `tail_rec_m(|a| pure(Done(a)), x) == pure(x)`.
2542	#[quickcheck]
2543	fn monad_rec_identity(x: i32) -> bool {
2544		use {
2545			crate::classes::monad_rec::tail_rec_m,
2546			core::ops::ControlFlow,
2547		};
2548		tail_rec_m::<ResultErrAppliedBrand<()>, _, _>(|a| Ok(ControlFlow::Break(a)), x) == Ok(x)
2549	}
2550
2551	/// Tests a recursive computation that sums a range via `tail_rec_m`.
2552	#[test]
2553	fn monad_rec_sum_range() {
2554		use {
2555			crate::classes::monad_rec::tail_rec_m,
2556			core::ops::ControlFlow,
2557		};
2558		let result = tail_rec_m::<ResultErrAppliedBrand<&str>, _, _>(
2559			|(n, acc)| {
2560				if n == 0 {
2561					Ok(ControlFlow::Break(acc))
2562				} else {
2563					Ok(ControlFlow::Continue((n - 1, acc + n)))
2564				}
2565			},
2566			(100i64, 0i64),
2567		);
2568		assert_eq!(result, Ok(5050));
2569	}
2570
2571	/// Tests that `tail_rec_m` short-circuits on `Err`.
2572	#[test]
2573	fn monad_rec_short_circuit() {
2574		use {
2575			crate::classes::monad_rec::tail_rec_m,
2576			core::ops::ControlFlow,
2577		};
2578		let result: Result<i32, &str> = tail_rec_m::<ResultErrAppliedBrand<&str>, _, _>(
2579			|n| {
2580				if n == 5 { Err("stopped") } else { Ok(ControlFlow::Continue(n + 1)) }
2581			},
2582			0,
2583		);
2584		assert_eq!(result, Err("stopped"));
2585	}
2586
2587	/// Tests stack safety: `tail_rec_m` handles large iteration counts.
2588	#[test]
2589	fn monad_rec_stack_safety() {
2590		use {
2591			crate::classes::monad_rec::tail_rec_m,
2592			core::ops::ControlFlow,
2593		};
2594		let iterations: i64 = 200_000;
2595		let result = tail_rec_m::<ResultErrAppliedBrand<()>, _, _>(
2596			|acc| {
2597				if acc < iterations {
2598					Ok(ControlFlow::Continue(acc + 1))
2599				} else {
2600					Ok(ControlFlow::Break(acc))
2601				}
2602			},
2603			0i64,
2604		);
2605		assert_eq!(result, Ok(iterations));
2606	}
2607
2608	// MonadRec tests for ResultOkAppliedBrand
2609
2610	/// Tests the MonadRec identity law for `ResultOkAppliedBrand`:
2611	/// `tail_rec_m(|a| Err(Done(a)), x) == Err(x)`.
2612	#[quickcheck]
2613	fn monad_rec_ok_applied_identity(x: i32) -> bool {
2614		use {
2615			crate::classes::monad_rec::tail_rec_m,
2616			core::ops::ControlFlow,
2617		};
2618		tail_rec_m::<ResultOkAppliedBrand<()>, _, _>(|a| Err(ControlFlow::Break(a)), x) == Err(x)
2619	}
2620
2621	/// Tests a recursive computation that sums a range via `tail_rec_m` on the error channel.
2622	#[test]
2623	fn monad_rec_ok_applied_sum_range() {
2624		use {
2625			crate::classes::monad_rec::tail_rec_m,
2626			core::ops::ControlFlow,
2627		};
2628		let result = tail_rec_m::<ResultOkAppliedBrand<&str>, _, _>(
2629			|(n, acc)| {
2630				if n == 0 {
2631					Err(ControlFlow::Break(acc))
2632				} else {
2633					Err(ControlFlow::Continue((n - 1, acc + n)))
2634				}
2635			},
2636			(100i64, 0i64),
2637		);
2638		assert_eq!(result, Err(5050));
2639	}
2640
2641	/// Tests that `tail_rec_m` on `ResultOkAppliedBrand` short-circuits on `Ok`.
2642	#[test]
2643	fn monad_rec_ok_applied_short_circuit() {
2644		use {
2645			crate::classes::monad_rec::tail_rec_m,
2646			core::ops::ControlFlow,
2647		};
2648		let result: Result<&str, i32> = tail_rec_m::<ResultOkAppliedBrand<&str>, _, _>(
2649			|n| {
2650				if n == 5 { Ok("stopped") } else { Err(ControlFlow::Continue(n + 1)) }
2651			},
2652			0,
2653		);
2654		assert_eq!(result, Ok("stopped"));
2655	}
2656
2657	/// Tests stack safety: `tail_rec_m` on `ResultOkAppliedBrand` handles large iteration counts.
2658	#[test]
2659	fn monad_rec_ok_applied_stack_safety() {
2660		use {
2661			crate::classes::monad_rec::tail_rec_m,
2662			core::ops::ControlFlow,
2663		};
2664		let iterations: i64 = 200_000;
2665		let result = tail_rec_m::<ResultOkAppliedBrand<()>, _, _>(
2666			|acc| {
2667				if acc < iterations {
2668					Err(ControlFlow::Continue(acc + 1))
2669				} else {
2670					Err(ControlFlow::Break(acc))
2671				}
2672			},
2673			0i64,
2674		);
2675		assert_eq!(result, Err(iterations));
2676	}
2677
2678	// -- RefBifunctor --
2679
2680	/// RefBifunctor identity: bimap((Clone::clone, Clone::clone), &p) == p
2681	#[quickcheck]
2682	fn ref_bifunctor_identity(x: Result<i32, i32>) -> bool {
2683		explicit::bimap::<ResultBrand, _, _, _, _, _, _>((|a: &i32| *a, |c: &i32| *c), &x) == x
2684	}
2685
2686	/// RefBifunctor composition
2687	#[quickcheck]
2688	fn ref_bifunctor_composition(x: Result<i32, i32>) -> bool {
2689		let f1 = |a: &i32| a.wrapping_add(1);
2690		let f2 = |a: &i32| a.wrapping_mul(2);
2691		let g1 = |c: &i32| c.wrapping_add(10);
2692		let g2 = |c: &i32| c.wrapping_mul(3);
2693		explicit::bimap::<ResultBrand, _, _, _, _, _, _>(
2694			(|a: &i32| f2(&f1(a)), |c: &i32| g2(&g1(c))),
2695			&x,
2696		) == explicit::bimap::<ResultBrand, _, _, _, _, _, _>(
2697			(f2, g2),
2698			&explicit::bimap::<ResultBrand, _, _, _, _, _, _>((f1, g1), &x),
2699		)
2700	}
2701
2702	// -- RefBifoldable --
2703
2704	/// RefBifoldable: bi_fold_map with ref closures produces same result as manual fold
2705	#[quickcheck]
2706	fn ref_bifoldable_fold_map(x: Result<i32, i32>) -> bool {
2707		let result = explicit::bi_fold_map::<RcFnBrand, ResultBrand, _, _, _, _, _>(
2708			(|a: &i32| a.to_string(), |b: &i32| b.to_string()),
2709			&x,
2710		);
2711		let expected = match &x {
2712			Err(a) => a.to_string(),
2713			Ok(b) => b.to_string(),
2714		};
2715		result == expected
2716	}
2717
2718	// -- RefBitraversable --
2719
2720	/// RefBitraversable: bi_traverse with ref closures consistent with ref_bi_sequence . bimap ref
2721	#[quickcheck]
2722	fn ref_bitraversable_consistency(x: Result<i32, i32>) -> bool {
2723		let f = |a: &i32| Some(a.wrapping_add(1));
2724		let g = |c: &i32| Some(c.wrapping_mul(2));
2725		let traversed =
2726			explicit::bi_traverse::<RcFnBrand, ResultBrand, _, _, _, _, OptionBrand, _, _>(
2727				(f, g),
2728				&x,
2729			);
2730		let mapped_then_sequenced =
2731			ref_bi_sequence::<ResultBrand, RcFnBrand, _, _, OptionBrand>(&explicit::bimap::<
2732				ResultBrand,
2733				_,
2734				_,
2735				_,
2736				_,
2737				_,
2738				_,
2739			>((f, g), &x));
2740		traversed == mapped_then_sequenced
2741	}
2742}