Skip to main content

fp_library/types/
identity.rs

1//! Trivial wrapper that contains a single value.
2//!
3//! The simplest possible container type, often used as a base case for higher-kinded types or when a container is required but no additional effect is needed.
4
5#[fp_macros::document_module]
6mod inner {
7	use {
8		crate::{
9			Apply,
10			brands::IdentityBrand,
11			classes::*,
12			dispatch::Ref,
13			impl_kind,
14			kinds::*,
15		},
16		core::ops::ControlFlow,
17		fp_macros::*,
18	};
19
20	/// Wraps a value.
21	///
22	/// The `Identity` type represents a trivial wrapper around a value. It is the simplest possible container.
23	/// It is often used as a base case for higher-kinded types or when a container is required but no additional effect is needed.
24	///
25	/// ### Higher-Kinded Type Representation
26	///
27	/// The higher-kinded representation of this type constructor is [`IdentityBrand`](crate::brands::IdentityBrand),
28	/// which is fully polymorphic over the wrapped value type.
29	///
30	/// ### Serialization
31	///
32	/// This type supports serialization and deserialization via [`serde`](https://serde.rs) when the `serde` feature is enabled.
33	#[document_type_parameters("The type of the wrapped value.")]
34	///
35	#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
36	#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
37	pub struct Identity<A>(
38		/// The wrapped value.
39		pub A,
40	);
41
42	impl_kind! {
43		for IdentityBrand {
44			type Of<'a, A: 'a>: 'a = Identity<A>;
45		}
46	}
47
48	#[document_type_parameters("The type of the wrapped value.")]
49	#[document_parameters("The identity instance.")]
50	impl<A> Identity<A> {
51		/// Maps a function over the value in the identity.
52		///
53		/// This is the inherent version of [`Functor::map`], accepting
54		/// `FnOnce` instead of `Fn` since it consumes `self`.
55		#[document_signature]
56		///
57		#[document_type_parameters("The type of the result of applying the function.")]
58		///
59		#[document_parameters("The function to apply.")]
60		///
61		#[document_returns("A new identity containing the result of applying the function.")]
62		///
63		#[document_examples]
64		///
65		/// ```
66		/// use fp_library::types::*;
67		///
68		/// let x = Identity(5);
69		/// let y = x.map(|i| i * 2);
70		/// assert_eq!(y, Identity(10));
71		/// ```
72		pub fn map<B>(
73			self,
74			f: impl FnOnce(A) -> B,
75		) -> Identity<B> {
76			Identity(f(self.0))
77		}
78
79		/// Lifts a binary function to operate on two identities.
80		///
81		/// See [`Lift::lift2`] for the type class version.
82		#[document_signature]
83		///
84		#[document_type_parameters(
85			"The type of the other identity's value.",
86			"The return type of the function."
87		)]
88		///
89		#[document_parameters("The other identity.", "The binary function to apply.")]
90		///
91		#[document_returns("A new identity containing the result of applying the function.")]
92		///
93		#[document_examples]
94		///
95		/// ```
96		/// use fp_library::types::*;
97		///
98		/// let x = Identity(1);
99		/// let y = Identity(2);
100		/// let z = x.lift2(y, |a, b| a + b);
101		/// assert_eq!(z, Identity(3));
102		/// ```
103		pub fn lift2<B, C>(
104			self,
105			other: Identity<B>,
106			f: impl FnOnce(A, B) -> C,
107		) -> Identity<C> {
108			Identity(f(self.0, other.0))
109		}
110
111		/// Applies a wrapped function to a value.
112		///
113		/// See [`Semiapplicative::apply`] for the type class version.
114		#[document_signature]
115		///
116		#[document_type_parameters("The return type of the wrapped function.")]
117		///
118		#[document_parameters("The identity containing the function.")]
119		///
120		#[document_returns("A new identity containing the result.")]
121		///
122		#[document_examples]
123		///
124		/// ```
125		/// use fp_library::types::*;
126		///
127		/// let f = Identity(|x: i32| x * 2);
128		/// let x = Identity(5);
129		/// let y = x.apply(f);
130		/// assert_eq!(y, Identity(10));
131		/// ```
132		pub fn apply<B>(
133			self,
134			ff: Identity<impl FnOnce(A) -> B>,
135		) -> Identity<B> {
136			Identity(ff.0(self.0))
137		}
138
139		/// Chains identity computations.
140		///
141		/// This is the inherent version of [`Semimonad::bind`], accepting
142		/// `FnOnce` instead of `Fn` since it consumes `self`.
143		#[document_signature]
144		///
145		#[document_type_parameters("The type of the result of the chained computation.")]
146		///
147		#[document_parameters("The function to apply to the value inside the identity.")]
148		///
149		#[document_returns("The result of applying `f` to the value.")]
150		///
151		#[document_examples]
152		///
153		/// ```
154		/// use fp_library::types::*;
155		///
156		/// let x = Identity(5);
157		/// let y = x.bind(|i| Identity(i * 2));
158		/// assert_eq!(y, Identity(10));
159		/// ```
160		pub fn bind<B>(
161			self,
162			f: impl FnOnce(A) -> Identity<B>,
163		) -> Identity<B> {
164			f(self.0)
165		}
166
167		/// Folds the identity from the right.
168		///
169		/// See [`Foldable::fold_right`] for the type class version.
170		#[document_signature]
171		///
172		#[document_type_parameters("The type of the accumulator.")]
173		///
174		#[document_parameters(
175			"The function to apply to the element and the accumulator.",
176			"The initial value of the accumulator."
177		)]
178		///
179		#[document_returns("The final accumulator value.")]
180		///
181		#[document_examples]
182		///
183		/// ```
184		/// use fp_library::types::*;
185		///
186		/// let x = Identity(5);
187		/// let y = x.fold_right(|a, b| a + b, 10);
188		/// assert_eq!(y, 15);
189		/// ```
190		pub fn fold_right<B>(
191			self,
192			f: impl FnOnce(A, B) -> B,
193			initial: B,
194		) -> B {
195			f(self.0, initial)
196		}
197
198		/// Folds the identity from the left.
199		///
200		/// See [`Foldable::fold_left`] for the type class version.
201		#[document_signature]
202		///
203		#[document_type_parameters("The type of the accumulator.")]
204		///
205		#[document_parameters(
206			"The function to apply to the accumulator and the element.",
207			"The initial value of the accumulator."
208		)]
209		///
210		#[document_returns("The final accumulator value.")]
211		///
212		#[document_examples]
213		///
214		/// ```
215		/// use fp_library::types::*;
216		///
217		/// let x = Identity(5);
218		/// let y = x.fold_left(|b, a| b + a, 10);
219		/// assert_eq!(y, 15);
220		/// ```
221		pub fn fold_left<B>(
222			self,
223			f: impl FnOnce(B, A) -> B,
224			initial: B,
225		) -> B {
226			f(initial, self.0)
227		}
228
229		/// Maps the value to a monoid and returns it.
230		///
231		/// See [`Foldable::fold_map`] for the type class version.
232		#[document_signature]
233		///
234		#[document_type_parameters("The monoid type.")]
235		///
236		#[document_parameters("The mapping function.")]
237		///
238		#[document_returns("The monoid value.")]
239		///
240		#[document_examples]
241		///
242		/// ```
243		/// use fp_library::types::*;
244		///
245		/// let x = Identity(5);
246		/// let y = x.fold_map(|a: i32| a.to_string());
247		/// assert_eq!(y, "5".to_string());
248		/// ```
249		pub fn fold_map<M>(
250			self,
251			f: impl FnOnce(A) -> M,
252		) -> M {
253			f(self.0)
254		}
255	}
256
257	#[document_type_parameters("The lifetime of the values.", "The type of the wrapped value.")]
258	#[document_parameters("The identity instance.")]
259	impl<'a, A: 'a> Identity<A> {
260		/// Traverses the identity with an applicative function.
261		///
262		/// See [`Traversable::traverse`] for the type class version.
263		#[document_signature]
264		///
265		#[document_type_parameters(
266			"The type of the elements in the resulting identity.",
267			"The applicative context."
268		)]
269		///
270		#[document_parameters(
271			"The function to apply, returning a value in an applicative context."
272		)]
273		///
274		#[document_returns("The identity wrapped in the applicative context.")]
275		///
276		#[document_examples]
277		///
278		/// ```
279		/// use fp_library::{
280		/// 	brands::*,
281		/// 	types::*,
282		/// };
283		///
284		/// let x = Identity(5);
285		/// let y = x.traverse::<_, OptionBrand>(|a| Some(a * 2));
286		/// assert_eq!(y, Some(Identity(10)));
287		/// ```
288		pub fn traverse<B: 'a + Clone, F: Applicative>(
289			self,
290			f: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
291		) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Identity<B>>)
292		where
293			Identity<B>: Clone, {
294			F::map(|b| Identity(b), f(self.0))
295		}
296
297		/// Sequences an identity containing an applicative value.
298		///
299		/// See [`Traversable::sequence`] for the type class version.
300		#[document_signature]
301		///
302		#[document_type_parameters(
303			"The inner type wrapped in the applicative context.",
304			"The applicative context."
305		)]
306		///
307		#[document_returns("The identity wrapped in the applicative context.")]
308		///
309		#[document_examples]
310		///
311		/// ```
312		/// use fp_library::{
313		/// 	brands::*,
314		/// 	types::*,
315		/// };
316		///
317		/// let x = Identity(Some(5));
318		/// let y: Option<Identity<i32>> = x.sequence::<i32, OptionBrand>();
319		/// assert_eq!(y, Some(Identity(5)));
320		/// ```
321		pub fn sequence<InnerA: 'a + Clone, F: Applicative>(
322			self
323		) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Identity<InnerA>>)
324		where
325			A: Into<Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, InnerA>)>,
326			Identity<InnerA>: Clone, {
327			F::map(|a| Identity(a), self.0.into())
328		}
329	}
330
331	impl Functor for IdentityBrand {
332		/// Maps a function over the value in the identity.
333		///
334		/// This method applies a function to the value inside the identity, producing a new identity with the transformed value.
335		#[document_signature]
336		///
337		#[document_type_parameters(
338			"The lifetime of the value.",
339			"The type of the value inside the identity.",
340			"The type of the result of applying the function."
341		)]
342		///
343		#[document_parameters("The function to apply.", "The identity to map over.")]
344		///
345		#[document_returns("A new identity containing the result of applying the function.")]
346		///
347		#[document_examples]
348		///
349		/// ```
350		/// use fp_library::{
351		/// 	brands::*,
352		/// 	functions::*,
353		/// 	types::*,
354		/// };
355		///
356		/// let x = Identity(5);
357		/// let y = explicit::map::<IdentityBrand, _, _, _, _>(|i| i * 2, x);
358		/// assert_eq!(y, Identity(10));
359		/// ```
360		fn map<'a, A: 'a, B: 'a>(
361			func: impl Fn(A) -> B + 'a,
362			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
363		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
364			fa.map(func)
365		}
366	}
367
368	impl Lift for IdentityBrand {
369		/// Lifts a binary function into the identity context.
370		///
371		/// This method lifts a binary function to operate on values within the identity context.
372		#[document_signature]
373		///
374		#[document_type_parameters(
375			"The lifetime of the values.",
376			"The type of the first identity's value.",
377			"The type of the second identity's value.",
378			"The return type of the function."
379		)]
380		///
381		#[document_parameters(
382			"The binary function to apply.",
383			"The first identity.",
384			"The second identity."
385		)]
386		///
387		#[document_returns("A new identity containing the result of applying the function.")]
388		#[document_examples]
389		///
390		/// ```
391		/// use fp_library::{
392		/// 	brands::*,
393		/// 	functions::*,
394		/// 	types::*,
395		/// };
396		///
397		/// let x = Identity(1);
398		/// let y = Identity(2);
399		/// let z = explicit::lift2::<IdentityBrand, _, _, _, _, _, _>(|a, b| a + b, x, y);
400		/// assert_eq!(z, Identity(3));
401		/// ```
402		fn lift2<'a, A, B, C>(
403			func: impl Fn(A, B) -> C + 'a,
404			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
405			fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
406		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
407		where
408			A: 'a,
409			B: 'a,
410			C: 'a, {
411			fa.lift2(fb, func)
412		}
413	}
414
415	impl Pointed for IdentityBrand {
416		/// Wraps a value in an identity.
417		///
418		/// This method wraps a value in an identity context.
419		#[document_signature]
420		///
421		#[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
422		///
423		#[document_parameters("The value to wrap.")]
424		///
425		#[document_returns("An identity containing the value.")]
426		///
427		#[document_examples]
428		///
429		/// ```
430		/// use fp_library::{
431		/// 	brands::*,
432		/// 	functions::*,
433		/// 	types::*,
434		/// };
435		///
436		/// let x = pure::<IdentityBrand, _>(5);
437		/// assert_eq!(x, Identity(5));
438		/// ```
439		fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
440			Identity(a) // Identity constructor is already equivalent to pure
441		}
442	}
443
444	impl ApplyFirst for IdentityBrand {}
445	impl ApplySecond for IdentityBrand {}
446
447	impl Semiapplicative for IdentityBrand {
448		/// Applies a wrapped function to a wrapped value.
449		///
450		/// This method applies a function wrapped in an identity to a value wrapped in an identity.
451		#[document_signature]
452		///
453		#[document_type_parameters(
454			"The lifetime of the values.",
455			"The brand of the cloneable function wrapper.",
456			"The type of the input value.",
457			"The type of the output value."
458		)]
459		///
460		#[document_parameters(
461			"The identity containing the function.",
462			"The identity containing the value."
463		)]
464		///
465		#[document_returns("A new identity containing the result of applying the function.")]
466		#[document_examples]
467		///
468		/// ```
469		/// use fp_library::{
470		/// 	brands::*,
471		/// 	functions::*,
472		/// 	types::*,
473		/// };
474		///
475		/// let f = Identity(lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
476		/// let x = Identity(5);
477		/// let y = apply::<RcFnBrand, IdentityBrand, _, _>(f, x);
478		/// assert_eq!(y, Identity(10));
479		/// ```
480		fn apply<'a, FnBrand: 'a + CloneFn, A: 'a + Clone, B: 'a>(
481			ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, A, B>>),
482			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
483		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
484			fa.apply(ff.map(|f| move |a| f(a)))
485		}
486	}
487
488	impl Semimonad for IdentityBrand {
489		/// Chains identity computations.
490		///
491		/// This method chains two identity computations, where the second computation depends on the result of the first.
492		#[document_signature]
493		///
494		#[document_type_parameters(
495			"The lifetime of the values.",
496			"The type of the result of the first computation.",
497			"The type of the result of the second computation."
498		)]
499		///
500		#[document_parameters(
501			"The first identity.",
502			"The function to apply to the value inside the identity."
503		)]
504		///
505		#[document_returns("The result of applying `f` to the value.")]
506		#[document_examples]
507		///
508		/// ```
509		/// use fp_library::{
510		/// 	brands::*,
511		/// 	functions::*,
512		/// 	types::*,
513		/// };
514		///
515		/// let x = Identity(5);
516		/// let y = explicit::bind::<IdentityBrand, _, _, _, _>(x, |i| Identity(i * 2));
517		/// assert_eq!(y, Identity(10));
518		/// ```
519		fn bind<'a, A: 'a, B: 'a>(
520			ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
521			func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
522		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
523			ma.bind(func)
524		}
525	}
526
527	impl Foldable for IdentityBrand {
528		/// Folds the identity from the right.
529		///
530		/// This method performs a right-associative fold of the identity. Since `Identity` contains only one element, this is equivalent to applying the function to the element and the initial value.
531		#[document_signature]
532		///
533		#[document_type_parameters(
534			"The lifetime of the values.",
535			"The brand of the cloneable function to use.",
536			"The type of the elements in the structure.",
537			"The type of the accumulator."
538		)]
539		///
540		#[document_parameters(
541			"The function to apply to each element and the accumulator.",
542			"The initial value of the accumulator.",
543			"The identity to fold."
544		)]
545		///
546		#[document_returns("The final accumulator value.")]
547		#[document_examples]
548		///
549		/// ```
550		/// use fp_library::{
551		/// 	brands::*,
552		/// 	functions::*,
553		/// 	types::*,
554		/// };
555		///
556		/// let x = Identity(5);
557		/// let y = explicit::fold_right::<RcFnBrand, IdentityBrand, _, _, _, _>(|a, b| a + b, 10, x);
558		/// assert_eq!(y, 15);
559		/// ```
560		fn fold_right<'a, FnBrand, A: 'a, B: 'a>(
561			func: impl Fn(A, B) -> B + 'a,
562			initial: B,
563			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
564		) -> B
565		where
566			FnBrand: CloneFn + 'a, {
567			fa.fold_right(func, initial)
568		}
569
570		/// Folds the identity from the left.
571		///
572		/// This method performs a left-associative fold of the identity. Since `Identity` contains only one element, this is equivalent to applying the function to the initial value and the element.
573		#[document_signature]
574		///
575		#[document_type_parameters(
576			"The lifetime of the values.",
577			"The brand of the cloneable function to use.",
578			"The type of the elements in the structure.",
579			"The type of the accumulator."
580		)]
581		///
582		#[document_parameters(
583			"The function to apply to the accumulator and each element.",
584			"The initial value of the accumulator.",
585			"The structure to fold."
586		)]
587		///
588		#[document_returns("The final accumulator value.")]
589		#[document_examples]
590		///
591		/// ```
592		/// use fp_library::{
593		/// 	brands::*,
594		/// 	functions::*,
595		/// 	types::*,
596		/// };
597		///
598		/// let x = Identity(5);
599		/// let y = explicit::fold_left::<RcFnBrand, IdentityBrand, _, _, _, _>(|b, a| b + a, 10, x);
600		/// assert_eq!(y, 15);
601		/// ```
602		fn fold_left<'a, FnBrand, A: 'a, B: 'a>(
603			func: impl Fn(B, A) -> B + 'a,
604			initial: B,
605			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
606		) -> B
607		where
608			FnBrand: CloneFn + 'a, {
609			fa.fold_left(func, initial)
610		}
611
612		/// Maps the value to a monoid and returns it.
613		///
614		/// This method maps the element of the identity to a monoid.
615		#[document_signature]
616		///
617		#[document_type_parameters(
618			"The lifetime of the values.",
619			"The brand of the cloneable function to use.",
620			"The type of the elements in the structure.",
621			"The type of the monoid."
622		)]
623		///
624		#[document_parameters("The mapping function.", "The identity to fold.")]
625		///
626		#[document_returns("The monoid value.")]
627		///
628		#[document_examples]
629		///
630		/// ```
631		/// use fp_library::{
632		/// 	brands::*,
633		/// 	functions::*,
634		/// 	types::*,
635		/// };
636		///
637		/// let x = Identity(5);
638		/// let y = explicit::fold_map::<RcFnBrand, IdentityBrand, _, _, _, _>(|a: i32| a.to_string(), x);
639		/// assert_eq!(y, "5".to_string());
640		/// ```
641		fn fold_map<'a, FnBrand, A: 'a, M>(
642			func: impl Fn(A) -> M + 'a,
643			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
644		) -> M
645		where
646			M: Monoid + 'a,
647			FnBrand: CloneFn + 'a, {
648			fa.fold_map(func)
649		}
650	}
651
652	impl Traversable for IdentityBrand {
653		/// Traverses the identity with an applicative function.
654		///
655		/// This method maps the element of the identity to a computation, evaluates it, and wraps the result in the applicative context.
656		#[document_signature]
657		///
658		#[document_type_parameters(
659			"The lifetime of the values.",
660			"The type of the elements in the traversable structure.",
661			"The type of the elements in the resulting traversable structure.",
662			"The applicative context."
663		)]
664		///
665		#[document_parameters(
666			"The function to apply to each element, returning a value in an applicative context.",
667			"The identity to traverse."
668		)]
669		///
670		#[document_returns("The identity wrapped in the applicative context.")]
671		#[document_examples]
672		///
673		/// ```
674		/// use fp_library::{
675		/// 	brands::*,
676		/// 	functions::*,
677		/// 	types::*,
678		/// };
679		///
680		/// let x = Identity(5);
681		/// let y =
682		/// 	explicit::traverse::<RcFnBrand, IdentityBrand, _, _, OptionBrand, _, _>(|a| Some(a * 2), x);
683		/// assert_eq!(y, Some(Identity(10)));
684		/// ```
685		fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
686			func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
687			ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
688		) -> 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>)>)
689		where
690			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
691			ta.traverse::<B, F>(func)
692		}
693
694		/// Sequences an identity of applicative.
695		///
696		/// This method evaluates the computation inside the identity and wraps the result in the applicative context.
697		#[document_signature]
698		///
699		#[document_type_parameters(
700			"The lifetime of the values.",
701			"The type of the elements in the traversable structure.",
702			"The applicative context."
703		)]
704		///
705		#[document_parameters("The identity containing the applicative value.")]
706		///
707		#[document_returns("The result of the traversal.")]
708		///
709		/// # Returns
710		///
711		/// The identity wrapped in the applicative context.
712		#[document_examples]
713		///
714		/// ```
715		/// use fp_library::{
716		/// 	brands::*,
717		/// 	functions::*,
718		/// 	types::*,
719		/// };
720		///
721		/// let x = Identity(Some(5));
722		/// let y = sequence::<IdentityBrand, _, OptionBrand>(x);
723		/// assert_eq!(y, Some(Identity(5)));
724		/// ```
725		fn sequence<'a, A: 'a + Clone, F: Applicative>(
726			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>)>)
727		) -> 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>)>)
728		where
729			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
730			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
731			ta.traverse::<A, F>(|a| a)
732		}
733	}
734
735	impl MonadRec for IdentityBrand {
736		/// Performs tail-recursive monadic computation over [`Identity`].
737		///
738		/// Since `Identity` has no effect, this simply loops on the inner value
739		/// until the step function returns [`ControlFlow::Break`].
740		#[document_signature]
741		///
742		#[document_type_parameters(
743			"The lifetime of the computation.",
744			"The type of the initial value and loop state.",
745			"The type of the result."
746		)]
747		///
748		#[document_parameters("The step function.", "The initial value.")]
749		///
750		#[document_returns("An identity containing the result of the computation.")]
751		///
752		#[document_examples]
753		///
754		/// ```
755		/// use {
756		/// 	core::ops::ControlFlow,
757		/// 	fp_library::{
758		/// 		brands::*,
759		/// 		functions::*,
760		/// 		types::*,
761		/// 	},
762		/// };
763		///
764		/// let result = tail_rec_m::<IdentityBrand, _, _>(
765		/// 	|n| {
766		/// 		if n < 10 {
767		/// 			Identity(ControlFlow::Continue(n + 1))
768		/// 		} else {
769		/// 			Identity(ControlFlow::Break(n))
770		/// 		}
771		/// 	},
772		/// 	0,
773		/// );
774		/// assert_eq!(result, Identity(10));
775		/// ```
776		fn tail_rec_m<'a, A: 'a, B: 'a>(
777			func: impl Fn(
778				A,
779			)
780				-> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
781			+ 'a,
782			initial: A,
783		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
784			let mut current = initial;
785			loop {
786				match func(current).0 {
787					ControlFlow::Continue(next) => current = next,
788					ControlFlow::Break(b) => return Identity(b),
789				}
790			}
791		}
792	}
793
794	impl Extract for IdentityBrand {
795		/// Extracts the inner value from an `Identity` by unwrapping it.
796		///
797		/// Since `Identity` is a trivial wrapper, extraction simply returns the
798		/// contained value.
799		#[document_signature]
800		///
801		#[document_type_parameters(
802			"The lifetime of the value.",
803			"The type of the value inside the identity."
804		)]
805		///
806		#[document_parameters("The identity to extract from.")]
807		///
808		#[document_returns("The inner value.")]
809		///
810		#[document_examples]
811		///
812		/// ```
813		/// use fp_library::{
814		/// 	brands::*,
815		/// 	functions::*,
816		/// 	types::*,
817		/// };
818		///
819		/// let id = Identity(42);
820		/// assert_eq!(extract::<IdentityBrand, _>(id), 42);
821		/// ```
822		fn extract<'a, A: 'a>(
823			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)
824		) -> A {
825			fa.0
826		}
827	}
828
829	impl Extend for IdentityBrand {
830		/// Extends a local computation to the `Identity` context.
831		///
832		/// Applies the function to the entire `Identity` and wraps the result in
833		/// a new `Identity`. Since `Identity` contains exactly one value, extending
834		/// is equivalent to applying the function and re-wrapping.
835		#[document_signature]
836		///
837		#[document_type_parameters(
838			"The lifetime of the values.",
839			"The type of the value inside the identity.",
840			"The result type of the extension function."
841		)]
842		///
843		#[document_parameters(
844			"The function that consumes an `Identity` and produces a value.",
845			"The identity to extend over."
846		)]
847		///
848		#[document_returns("A new identity containing the result of applying the function.")]
849		///
850		#[document_examples]
851		///
852		/// ```
853		/// use fp_library::{
854		/// 	brands::*,
855		/// 	functions::*,
856		/// 	types::*,
857		/// };
858		///
859		/// let id = Identity(5);
860		/// let result = extend::<IdentityBrand, _, _>(|w: Identity<i32>| w.0 * 2, id);
861		/// assert_eq!(result, Identity(10));
862		/// ```
863		fn extend<'a, A: 'a + Clone, B: 'a>(
864			f: impl Fn(Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)) -> B + 'a,
865			wa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
866		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
867			Identity(f(wa))
868		}
869	}
870	// -- By-reference trait implementations --
871
872	impl RefFunctor for IdentityBrand {
873		/// Maps a function over the identity by reference.
874		#[document_signature]
875		///
876		#[document_type_parameters(
877			"The lifetime of the value.",
878			"The type of the wrapped value.",
879			"The type of the resulting value."
880		)]
881		///
882		#[document_parameters(
883			"The function to apply to the value reference.",
884			"The identity to map over."
885		)]
886		///
887		#[document_returns("A new identity containing the result.")]
888		///
889		#[document_examples]
890		///
891		/// ```
892		/// use fp_library::{
893		/// 	brands::*,
894		/// 	functions::*,
895		/// 	types::*,
896		/// };
897		///
898		/// assert_eq!(
899		/// 	explicit::map::<IdentityBrand, _, _, _, _>(|x: &i32| *x * 2, &Identity(5)),
900		/// 	Identity(10)
901		/// );
902		/// ```
903		fn ref_map<'a, A: 'a, B: 'a>(
904			func: impl Fn(&A) -> B + 'a,
905			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
906		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
907			Identity(func(&fa.0))
908		}
909	}
910
911	impl RefFoldable for IdentityBrand {
912		/// Folds the identity by reference using a monoid.
913		#[document_signature]
914		///
915		#[document_type_parameters(
916			"The lifetime of the value.",
917			"The brand of the cloneable function wrapper.",
918			"The type of the wrapped value.",
919			"The monoid type."
920		)]
921		///
922		#[document_parameters(
923			"The function to map the value reference to a monoid.",
924			"The identity to fold."
925		)]
926		///
927		#[document_returns("The monoid value.")]
928		///
929		#[document_examples]
930		///
931		/// ```
932		/// use fp_library::{
933		/// 	brands::*,
934		/// 	functions::*,
935		/// 	types::*,
936		/// };
937		///
938		/// let result = explicit::fold_map::<RcFnBrand, IdentityBrand, _, _, _, _>(
939		/// 	|x: &i32| x.to_string(),
940		/// 	&Identity(5),
941		/// );
942		/// assert_eq!(result, "5");
943		/// ```
944		fn ref_fold_map<'a, FnBrand, A: 'a + Clone, M>(
945			func: impl Fn(&A) -> M + 'a,
946			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
947		) -> M
948		where
949			FnBrand: LiftFn + 'a,
950			M: Monoid + 'a, {
951			func(&fa.0)
952		}
953	}
954
955	impl RefTraversable for IdentityBrand {
956		/// Traverses the identity by reference.
957		#[document_signature]
958		///
959		#[document_type_parameters(
960			"The lifetime of the value.",
961			"The brand of the cloneable function wrapper.",
962			"The type of the wrapped value.",
963			"The type of the resulting value.",
964			"The applicative functor brand."
965		)]
966		///
967		#[document_parameters(
968			"The function to apply to the value reference.",
969			"The identity to traverse."
970		)]
971		///
972		#[document_returns("The result in the applicative context.")]
973		///
974		#[document_examples]
975		///
976		/// ```
977		/// use fp_library::{
978		/// 	brands::*,
979		/// 	functions::*,
980		/// 	types::*,
981		/// };
982		///
983		/// let result: Option<Identity<String>> =
984		/// 	ref_traverse::<IdentityBrand, RcFnBrand, _, _, OptionBrand>(
985		/// 		|x: &i32| Some(x.to_string()),
986		/// 		&Identity(42),
987		/// 	);
988		/// assert_eq!(result, Some(Identity("42".to_string())));
989		/// ```
990		fn ref_traverse<'a, FnBrand, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
991			func: impl Fn(&A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
992			ta: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
993		) -> 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>)>)
994		where
995			FnBrand: LiftFn + 'a,
996			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
997			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
998			F::map(Identity, func(&ta.0))
999		}
1000	}
1001
1002	// -- WithIndex trait implementations --
1003
1004	impl WithIndex for IdentityBrand {
1005		type Index = ();
1006	}
1007
1008	impl FunctorWithIndex for IdentityBrand {
1009		/// Maps with index over Identity (index is always `()`).
1010		#[document_signature]
1011		#[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1012		#[document_parameters("The function to apply with index.", "The Identity value.")]
1013		#[document_returns("The transformed Identity value.")]
1014		#[document_examples]
1015		///
1016		/// ```
1017		/// use fp_library::{
1018		/// 	brands::*,
1019		/// 	functions::*,
1020		/// 	types::Identity,
1021		/// };
1022		///
1023		/// let result = explicit::map_with_index::<IdentityBrand, _, _, _, _>(|(), x| x * 2, Identity(5));
1024		/// assert_eq!(result, Identity(10));
1025		/// ```
1026		fn map_with_index<'a, A: 'a, B: 'a>(
1027			func: impl Fn((), A) -> B + 'a,
1028			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1029		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1030			Identity(func((), fa.0))
1031		}
1032	}
1033
1034	impl FoldableWithIndex for IdentityBrand {
1035		/// Folds with index over Identity (index is always `()`).
1036		#[document_signature]
1037		#[document_type_parameters(
1038			"The lifetime.",
1039			"The brand of the cloneable function to use.",
1040			"The element type.",
1041			"The monoid type."
1042		)]
1043		#[document_parameters("The function to apply with index.", "The Identity value.")]
1044		#[document_returns("The monoid result.")]
1045		#[document_examples]
1046		///
1047		/// ```
1048		/// use fp_library::{
1049		/// 	brands::*,
1050		/// 	functions::*,
1051		/// 	types::Identity,
1052		/// };
1053		///
1054		/// let result: String = explicit::fold_map_with_index::<RcFnBrand, IdentityBrand, _, _, _, _>(
1055		/// 	|(), x: i32| x.to_string(),
1056		/// 	Identity(42),
1057		/// );
1058		/// assert_eq!(result, "42");
1059		/// ```
1060		fn fold_map_with_index<'a, FnBrand, A: 'a + Clone, R: Monoid + 'a>(
1061			func: impl Fn((), A) -> R + 'a,
1062			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1063		) -> R
1064		where
1065			FnBrand: LiftFn + 'a, {
1066			func((), fa.0)
1067		}
1068	}
1069
1070	impl TraversableWithIndex for IdentityBrand {
1071		/// Traverses with index over Identity (index is always `()`).
1072		#[document_signature]
1073		#[document_type_parameters(
1074			"The lifetime.",
1075			"The element type.",
1076			"The output type.",
1077			"The applicative brand."
1078		)]
1079		#[document_parameters("The function to apply with index.", "The Identity value.")]
1080		#[document_returns("The result in the applicative context.")]
1081		#[document_examples]
1082		///
1083		/// ```
1084		/// use fp_library::{
1085		/// 	brands::*,
1086		/// 	functions::*,
1087		/// 	types::Identity,
1088		/// };
1089		///
1090		/// let result: Option<Identity<String>> =
1091		/// 	explicit::traverse_with_index::<RcFnBrand, IdentityBrand, _, _, OptionBrand, _, _>(
1092		/// 		|(), x: i32| Some(x.to_string()),
1093		/// 		Identity(42),
1094		/// 	);
1095		/// assert_eq!(result, Some(Identity("42".to_string())));
1096		/// ```
1097		fn traverse_with_index<'a, A: 'a, B: 'a + Clone, M: Applicative>(
1098			func: impl Fn((), A) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1099			ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1100		) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)>)
1101		where
1102			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
1103			Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1104			M::map::<B, Identity<B>>(Identity, func((), ta.0))
1105		}
1106	}
1107
1108	// -- By-reference WithIndex implementations --
1109
1110	impl RefFunctorWithIndex for IdentityBrand {
1111		/// Maps with index over Identity by reference.
1112		#[document_signature]
1113		#[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1114		#[document_parameters("The function to apply with index.", "The Identity value.")]
1115		#[document_returns("The transformed Identity value.")]
1116		#[document_examples]
1117		///
1118		/// ```
1119		/// use fp_library::{
1120		/// 	brands::*,
1121		/// 	functions::*,
1122		/// 	types::Identity,
1123		/// };
1124		///
1125		/// let result =
1126		/// 	explicit::map_with_index::<IdentityBrand, _, _, _, _>(|(), x: &i32| *x * 2, &Identity(5));
1127		/// assert_eq!(result, Identity(10));
1128		/// ```
1129		fn ref_map_with_index<'a, A: 'a, B: 'a>(
1130			func: impl Fn((), &A) -> B + 'a,
1131			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1132		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1133			Identity(func((), &fa.0))
1134		}
1135	}
1136
1137	impl RefFoldableWithIndex for IdentityBrand {
1138		/// Folds with index over Identity by reference.
1139		#[document_signature]
1140		#[document_type_parameters(
1141			"The lifetime.",
1142			"The brand of the cloneable function.",
1143			"The element type.",
1144			"The monoid type."
1145		)]
1146		#[document_parameters("The function to apply with index.", "The Identity value.")]
1147		#[document_returns("The monoid result.")]
1148		#[document_examples]
1149		///
1150		/// ```
1151		/// use fp_library::{
1152		/// 	brands::*,
1153		/// 	functions::*,
1154		/// 	types::Identity,
1155		/// };
1156		///
1157		/// let result: String = explicit::fold_map_with_index::<RcFnBrand, IdentityBrand, _, _, _, _>(
1158		/// 	|(), x: &i32| x.to_string(),
1159		/// 	&Identity(42),
1160		/// );
1161		/// assert_eq!(result, "42");
1162		/// ```
1163		fn ref_fold_map_with_index<'a, FnBrand, A: 'a + Clone, R: Monoid + 'a>(
1164			func: impl Fn((), &A) -> R + 'a,
1165			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1166		) -> R
1167		where
1168			FnBrand: LiftFn + 'a, {
1169			func((), &fa.0)
1170		}
1171	}
1172
1173	impl RefTraversableWithIndex for IdentityBrand {
1174		/// Traverses with index over Identity by reference.
1175		#[document_signature]
1176		#[document_type_parameters(
1177			"The lifetime.",
1178			"The element type.",
1179			"The output type.",
1180			"The applicative brand."
1181		)]
1182		#[document_parameters("The function to apply with index.", "The Identity value.")]
1183		#[document_returns("The result in the applicative context.")]
1184		#[document_examples]
1185		///
1186		/// ```
1187		/// use fp_library::{
1188		/// 	brands::*,
1189		/// 	functions::*,
1190		/// 	types::Identity,
1191		/// };
1192		///
1193		/// let result: Option<Identity<String>> =
1194		/// 	explicit::traverse_with_index::<RcFnBrand, IdentityBrand, _, _, OptionBrand, _, _>(
1195		/// 		|(), x: &i32| Some(x.to_string()),
1196		/// 		&Identity(42),
1197		/// 	);
1198		/// assert_eq!(result, Some(Identity("42".to_string())));
1199		/// ```
1200		fn ref_traverse_with_index<'a, A: 'a + Clone, B: 'a + Clone, M: Applicative>(
1201			f: impl Fn((), &A) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1202			ta: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1203		) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)>)
1204		where
1205			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
1206			Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1207			M::map(Identity, f((), &ta.0))
1208		}
1209	}
1210
1211	// -- By-reference monadic trait implementations --
1212
1213	impl RefPointed for IdentityBrand {
1214		/// Creates an Identity from a reference by cloning.
1215		#[document_signature]
1216		#[document_type_parameters("The lifetime.", "The value type.")]
1217		#[document_parameters("The reference to wrap.")]
1218		#[document_returns("An Identity containing a clone of the value.")]
1219		#[document_examples]
1220		///
1221		/// ```
1222		/// use fp_library::{
1223		/// 	brands::*,
1224		/// 	functions::*,
1225		/// 	types::Identity,
1226		/// };
1227		///
1228		/// let x = 42;
1229		/// let result: Identity<i32> = ref_pure::<IdentityBrand, _>(&x);
1230		/// assert_eq!(result, Identity(42));
1231		/// ```
1232		fn ref_pure<'a, A: Clone + 'a>(
1233			a: &A
1234		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1235			Identity(a.clone())
1236		}
1237	}
1238
1239	impl RefLift for IdentityBrand {
1240		/// Combines two Identity values with a by-reference binary function.
1241		#[document_signature]
1242		#[document_type_parameters("The lifetime.", "First input.", "Second input.", "Output.")]
1243		#[document_parameters(
1244			"The binary function.",
1245			"The first Identity.",
1246			"The second Identity."
1247		)]
1248		#[document_returns("The combined Identity.")]
1249		#[document_examples]
1250		///
1251		/// ```
1252		/// use fp_library::{
1253		/// 	brands::*,
1254		/// 	functions::*,
1255		/// 	types::Identity,
1256		/// };
1257		///
1258		/// let result = explicit::lift2::<IdentityBrand, _, _, _, _, _, _>(
1259		/// 	|a: &i32, b: &i32| *a + *b,
1260		/// 	&Identity(1),
1261		/// 	&Identity(2),
1262		/// );
1263		/// assert_eq!(result, Identity(3));
1264		/// ```
1265		fn ref_lift2<'a, A: 'a, B: 'a, C: 'a>(
1266			func: impl Fn(&A, &B) -> C + 'a,
1267			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1268			fb: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1269		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) {
1270			Identity(func(&fa.0, &fb.0))
1271		}
1272	}
1273
1274	impl RefSemiapplicative for IdentityBrand {
1275		/// Applies a wrapped by-ref function within Identity.
1276		#[document_signature]
1277		#[document_type_parameters(
1278			"The lifetime.",
1279			"The function brand.",
1280			"The input type.",
1281			"The output type."
1282		)]
1283		#[document_parameters(
1284			"The Identity containing the function.",
1285			"The Identity containing the value."
1286		)]
1287		#[document_returns("The Identity containing the result.")]
1288		#[document_examples]
1289		///
1290		/// ```
1291		/// use fp_library::{
1292		/// 	brands::*,
1293		/// 	classes::*,
1294		/// 	functions::*,
1295		/// 	types::Identity,
1296		/// };
1297		///
1298		/// let f: std::rc::Rc<dyn Fn(&i32) -> i32> = std::rc::Rc::new(|x: &i32| *x + 1);
1299		/// let result = ref_apply::<RcFnBrand, IdentityBrand, _, _>(&Identity(f), &Identity(5));
1300		/// assert_eq!(result, Identity(6));
1301		/// ```
1302		fn ref_apply<'a, FnBrand: 'a + CloneFn<Ref>, A: 'a, B: 'a>(
1303			ff: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn<Ref>>::Of<'a, A, B>>),
1304			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1305		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1306			Identity((*ff.0)(&fa.0))
1307		}
1308	}
1309
1310	impl RefSemimonad for IdentityBrand {
1311		/// Chains Identity computations by reference.
1312		#[document_signature]
1313		#[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1314		#[document_parameters("The input Identity.", "The function to apply by reference.")]
1315		#[document_returns("The resulting Identity.")]
1316		#[document_examples]
1317		///
1318		/// ```
1319		/// use fp_library::{
1320		/// 	brands::*,
1321		/// 	functions::*,
1322		/// 	types::Identity,
1323		/// };
1324		///
1325		/// let result: Identity<String> =
1326		/// 	explicit::bind::<IdentityBrand, _, _, _, _>(&Identity(42), |x: &i32| {
1327		/// 		Identity(x.to_string())
1328		/// 	});
1329		/// assert_eq!(result, Identity("42".to_string()));
1330		/// ```
1331		fn ref_bind<'a, A: 'a, B: 'a>(
1332			fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1333			f: 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			f(&fa.0)
1336		}
1337	}
1338}
1339pub use inner::*;
1340
1341#[cfg(test)]
1342mod tests {
1343	use {
1344		super::inner::Identity,
1345		crate::{
1346			brands::{
1347				IdentityBrand,
1348				OptionBrand,
1349				RcFnBrand,
1350			},
1351			classes::*,
1352			functions::*,
1353		},
1354		quickcheck_macros::quickcheck,
1355	};
1356
1357	// Functor Laws
1358
1359	/// Tests the identity law for Functor.
1360	#[quickcheck]
1361	fn functor_identity(x: i32) -> bool {
1362		let x = Identity(x);
1363		explicit::map::<IdentityBrand, _, _, _, _>(identity, x) == x
1364	}
1365
1366	/// Tests the composition law for Functor.
1367	#[quickcheck]
1368	fn functor_composition(x: i32) -> bool {
1369		let x = Identity(x);
1370		let f = |x: i32| x.wrapping_add(1);
1371		let g = |x: i32| x.wrapping_mul(2);
1372		explicit::map::<IdentityBrand, _, _, _, _>(compose(f, g), x)
1373			== explicit::map::<IdentityBrand, _, _, _, _>(
1374				f,
1375				explicit::map::<IdentityBrand, _, _, _, _>(g, x),
1376			)
1377	}
1378
1379	// Applicative Laws
1380
1381	/// Tests the identity law for Applicative.
1382	#[quickcheck]
1383	fn applicative_identity(v: i32) -> bool {
1384		let v = Identity(v);
1385		apply::<RcFnBrand, IdentityBrand, _, _>(
1386			pure::<IdentityBrand, _>(<RcFnBrand as LiftFn>::new(identity)),
1387			v,
1388		) == v
1389	}
1390
1391	/// Tests the homomorphism law for Applicative.
1392	#[quickcheck]
1393	fn applicative_homomorphism(x: i32) -> bool {
1394		let f = |x: i32| x.wrapping_mul(2);
1395		apply::<RcFnBrand, IdentityBrand, _, _>(
1396			pure::<IdentityBrand, _>(<RcFnBrand as LiftFn>::new(f)),
1397			pure::<IdentityBrand, _>(x),
1398		) == pure::<IdentityBrand, _>(f(x))
1399	}
1400
1401	/// Tests the composition law for Applicative.
1402	#[quickcheck]
1403	fn applicative_composition(
1404		w: i32,
1405		u_val: i32,
1406		v_val: i32,
1407	) -> bool {
1408		let w = Identity(w);
1409		let v_fn = move |x: i32| x.wrapping_mul(v_val);
1410		let u_fn = move |x: i32| x.wrapping_add(u_val);
1411
1412		let v = pure::<IdentityBrand, _>(<RcFnBrand as LiftFn>::new(v_fn));
1413		let u = pure::<IdentityBrand, _>(<RcFnBrand as LiftFn>::new(u_fn));
1414
1415		// RHS: u <*> (v <*> w)
1416		let vw = apply::<RcFnBrand, IdentityBrand, _, _>(v.clone(), w);
1417		let rhs = apply::<RcFnBrand, IdentityBrand, _, _>(u.clone(), vw);
1418
1419		// LHS: pure(compose) <*> u <*> v <*> w
1420		// equivalent to (u . v) <*> w
1421		let composed = move |x| u_fn(v_fn(x));
1422		let uv = pure::<IdentityBrand, _>(<RcFnBrand as LiftFn>::new(composed));
1423
1424		let lhs = apply::<RcFnBrand, IdentityBrand, _, _>(uv, w);
1425
1426		lhs == rhs
1427	}
1428
1429	/// Tests the interchange law for Applicative.
1430	#[quickcheck]
1431	fn applicative_interchange(y: i32) -> bool {
1432		// u <*> pure y = pure ($ y) <*> u
1433		let f = |x: i32| x.wrapping_mul(2);
1434		let u = pure::<IdentityBrand, _>(<RcFnBrand as LiftFn>::new(f));
1435
1436		let lhs = apply::<RcFnBrand, IdentityBrand, _, _>(u.clone(), pure::<IdentityBrand, _>(y));
1437
1438		let rhs_fn = <RcFnBrand as LiftFn>::new(move |f: std::rc::Rc<dyn Fn(i32) -> i32>| f(y));
1439		let rhs = apply::<RcFnBrand, IdentityBrand, _, _>(pure::<IdentityBrand, _>(rhs_fn), u);
1440
1441		lhs == rhs
1442	}
1443
1444	// Monad Laws
1445
1446	/// Tests the left identity law for Monad.
1447	#[quickcheck]
1448	fn monad_left_identity(a: i32) -> bool {
1449		let f = |x: i32| Identity(x.wrapping_mul(2));
1450		explicit::bind::<IdentityBrand, _, _, _, _>(pure::<IdentityBrand, _>(a), f) == f(a)
1451	}
1452
1453	/// Tests the right identity law for Monad.
1454	#[quickcheck]
1455	fn monad_right_identity(m: i32) -> bool {
1456		let m = Identity(m);
1457		explicit::bind::<IdentityBrand, _, _, _, _>(m, pure::<IdentityBrand, _>) == m
1458	}
1459
1460	/// Tests the associativity law for Monad.
1461	#[quickcheck]
1462	fn monad_associativity(m: i32) -> bool {
1463		let m = Identity(m);
1464		let f = |x: i32| Identity(x.wrapping_mul(2));
1465		let g = |x: i32| Identity(x.wrapping_add(1));
1466		explicit::bind::<IdentityBrand, _, _, _, _>(
1467			explicit::bind::<IdentityBrand, _, _, _, _>(m, f),
1468			g,
1469		) == explicit::bind::<IdentityBrand, _, _, _, _>(m, |x| {
1470			explicit::bind::<IdentityBrand, _, _, _, _>(f(x), g)
1471		})
1472	}
1473
1474	// Edge Cases
1475
1476	/// Tests the `map` function.
1477	#[test]
1478	fn map_test() {
1479		assert_eq!(
1480			explicit::map::<IdentityBrand, _, _, _, _>(|x: i32| x + 1, Identity(1)),
1481			Identity(2)
1482		);
1483	}
1484
1485	/// Tests the `bind` function.
1486	#[test]
1487	fn bind_test() {
1488		assert_eq!(
1489			explicit::bind::<IdentityBrand, _, _, _, _>(Identity(1), |x| Identity(x + 1)),
1490			Identity(2)
1491		);
1492	}
1493
1494	/// Tests the `fold_right` function.
1495	#[test]
1496	fn fold_right_test() {
1497		assert_eq!(
1498			crate::functions::explicit::fold_right::<RcFnBrand, IdentityBrand, _, _, _, _>(
1499				|x: i32, acc| x + acc,
1500				0,
1501				Identity(1)
1502			),
1503			1
1504		);
1505	}
1506
1507	/// Tests the `fold_left` function.
1508	#[test]
1509	fn fold_left_test() {
1510		assert_eq!(
1511			crate::functions::explicit::fold_left::<RcFnBrand, IdentityBrand, _, _, _, _>(
1512				|acc, x: i32| acc + x,
1513				0,
1514				Identity(1)
1515			),
1516			1
1517		);
1518	}
1519
1520	/// Tests the `traverse` function.
1521	#[test]
1522	fn traverse_test() {
1523		assert_eq!(
1524			crate::classes::traversable::traverse::<IdentityBrand, _, _, OptionBrand>(
1525				|x: i32| Some(x + 1),
1526				Identity(1)
1527			),
1528			Some(Identity(2))
1529		);
1530	}
1531
1532	// MonadRec tests
1533
1534	/// Tests the MonadRec identity law: `tail_rec_m(|a| pure(Done(a)), x) == pure(x)`.
1535	#[quickcheck]
1536	fn monad_rec_identity(x: i32) -> bool {
1537		use {
1538			crate::classes::monad_rec::tail_rec_m,
1539			core::ops::ControlFlow,
1540		};
1541		tail_rec_m::<IdentityBrand, _, _>(|a| Identity(ControlFlow::Break(a)), x) == Identity(x)
1542	}
1543
1544	/// Tests a recursive computation that sums a range via `tail_rec_m`.
1545	#[test]
1546	fn monad_rec_sum_range() {
1547		use {
1548			crate::classes::monad_rec::tail_rec_m,
1549			core::ops::ControlFlow,
1550		};
1551		let result = tail_rec_m::<IdentityBrand, _, _>(
1552			|(n, acc)| {
1553				if n == 0 {
1554					Identity(ControlFlow::Break(acc))
1555				} else {
1556					Identity(ControlFlow::Continue((n - 1, acc + n)))
1557				}
1558			},
1559			(100i64, 0i64),
1560		);
1561		assert_eq!(result, Identity(5050));
1562	}
1563
1564	/// Tests stack safety: `tail_rec_m` handles large iteration counts.
1565	#[test]
1566	fn monad_rec_stack_safety() {
1567		use {
1568			crate::classes::monad_rec::tail_rec_m,
1569			core::ops::ControlFlow,
1570		};
1571		let iterations: i64 = 200_000;
1572		let result = tail_rec_m::<IdentityBrand, _, _>(
1573			|acc| {
1574				if acc < iterations {
1575					Identity(ControlFlow::Continue(acc + 1))
1576				} else {
1577					Identity(ControlFlow::Break(acc))
1578				}
1579			},
1580			0i64,
1581		);
1582		assert_eq!(result, Identity(iterations));
1583	}
1584
1585	// Extract / Extend / Comonad Laws
1586
1587	/// Extract pure-extract law: `extract(pure(x)) == x`.
1588	#[quickcheck]
1589	fn extract_pure(x: i32) -> bool {
1590		use crate::classes::extract::extract;
1591		extract::<IdentityBrand, _>(pure::<IdentityBrand, _>(x)) == x
1592	}
1593
1594	/// Comonad left identity: `extract(extend(f, wa)) == f(wa)`.
1595	#[quickcheck]
1596	fn comonad_left_identity(x: i32) -> bool {
1597		use crate::classes::{
1598			extend::extend,
1599			extract::extract,
1600		};
1601		let f = |w: Identity<i32>| w.0.wrapping_mul(3);
1602		let wa = Identity(x);
1603		extract::<IdentityBrand, _>(extend::<IdentityBrand, _, _>(f, wa)) == f(wa)
1604	}
1605
1606	/// Comonad right identity: `extend(extract, wa) == wa`.
1607	#[quickcheck]
1608	fn comonad_right_identity(x: i32) -> bool {
1609		use crate::classes::{
1610			extend::extend,
1611			extract::extract,
1612		};
1613		extend::<IdentityBrand, _, _>(extract::<IdentityBrand, _>, Identity(x)) == Identity(x)
1614	}
1615
1616	/// Extend associativity: `extend(f, extend(g, w)) == extend(|w| f(extend(g, w)), w)`.
1617	#[quickcheck]
1618	fn extend_associativity(x: i32) -> bool {
1619		use crate::classes::extend::extend;
1620		let g = |w: Identity<i32>| w.0.wrapping_mul(2);
1621		let f = |w: Identity<i32>| w.0.wrapping_add(1);
1622		let wa = Identity(x);
1623		let lhs = extend::<IdentityBrand, _, _>(f, extend::<IdentityBrand, _, _>(g, wa));
1624		let rhs = extend::<IdentityBrand, _, _>(
1625			|w: Identity<i32>| f(extend::<IdentityBrand, _, _>(g, w)),
1626			wa,
1627		);
1628		lhs == rhs
1629	}
1630
1631	/// Map-extract law: `extract(map(f, wa)) == f(extract(wa))`.
1632	#[quickcheck]
1633	fn comonad_map_extract(x: i32) -> bool {
1634		use crate::classes::extract::extract;
1635		let f = |a: i32| a.wrapping_mul(5);
1636		let wa = Identity(x);
1637		extract::<IdentityBrand, _>(explicit::map::<IdentityBrand, _, _, _, _>(f, wa))
1638			== f(extract::<IdentityBrand, _>(wa))
1639	}
1640
1641	/// Tests basic `extract` on `Identity`.
1642	#[test]
1643	fn extract_test() {
1644		use crate::classes::extract::extract;
1645		assert_eq!(extract::<IdentityBrand, _>(Identity(42)), 42);
1646	}
1647
1648	/// Tests basic `extend` on `Identity`.
1649	#[test]
1650	fn extend_test() {
1651		use crate::classes::extend::extend;
1652		let result = extend::<IdentityBrand, _, _>(|w: Identity<i32>| w.0 * 2, Identity(21));
1653		assert_eq!(result, Identity(42));
1654	}
1655
1656	// RefFunctor Laws
1657
1658	/// Tests the identity law for RefFunctor: `ref_map(|x| *x, Identity(v)) == Identity(v)`.
1659	#[quickcheck]
1660	fn ref_functor_identity(v: i32) -> bool {
1661		use crate::classes::ref_functor::RefFunctor;
1662		IdentityBrand::ref_map(|x: &i32| *x, &Identity(v)) == Identity(v)
1663	}
1664
1665	/// Tests the composition law for RefFunctor.
1666	#[quickcheck]
1667	fn ref_functor_composition(v: i32) -> bool {
1668		use crate::classes::ref_functor::RefFunctor;
1669		let f = |x: &i32| x.wrapping_add(1);
1670		let g = |x: &i32| x.wrapping_mul(2);
1671		IdentityBrand::ref_map(|x: &i32| f(&g(x)), &Identity(v))
1672			== IdentityBrand::ref_map(f, &IdentityBrand::ref_map(g, &Identity(v)))
1673	}
1674
1675	// RefSemimonad Laws
1676
1677	/// Tests the left identity law for RefSemimonad: `ref_bind(Identity(x), |a| Identity(*a)) == Identity(x)`.
1678	#[quickcheck]
1679	fn ref_semimonad_left_identity(x: i32) -> bool {
1680		use crate::classes::ref_semimonad::RefSemimonad;
1681		IdentityBrand::ref_bind(&Identity(x), |a: &i32| Identity(*a)) == Identity(x)
1682	}
1683}