Skip to main content

fp_library/types/
control_flow.rs

1//! Helpers and type class implementations for [`ControlFlow`](core::ops::ControlFlow) in tail-recursive computations.
2//!
3//! Used by [`MonadRec`](crate::classes::monad_rec::MonadRec) to implement stack-safe tail recursion. [`ControlFlow::Continue`](core::ops::ControlFlow::Continue) continues iteration, while [`ControlFlow::Break`](core::ops::ControlFlow::Break) terminates with a result. The corresponding brands are [`ControlFlowBrand`](crate::brands::ControlFlowBrand) (bifunctor), [`ControlFlowBreakAppliedBrand`](crate::brands::ControlFlowBreakAppliedBrand) (functor over the continue value), and [`ControlFlowContinueAppliedBrand`](crate::brands::ControlFlowContinueAppliedBrand) (functor over the break value).
4//!
5//! ### Examples
6//!
7//! ```
8//! use core::ops::ControlFlow;
9//!
10//! // Count down from n to 0, accumulating the sum
11//! fn sum_to_zero(
12//! 	n: i32,
13//! 	acc: i32,
14//! ) -> ControlFlow<i32, (i32, i32)> {
15//! 	if n <= 0 { ControlFlow::Break(acc) } else { ControlFlow::Continue((n - 1, acc + n)) }
16//! }
17//! ```
18
19#[fp_macros::document_module]
20mod inner {
21	use {
22		crate::{
23			Apply,
24			brands::{
25				ControlFlowBrand,
26				ControlFlowBreakAppliedBrand,
27				ControlFlowContinueAppliedBrand,
28			},
29			classes::*,
30			impl_kind,
31			kinds::*,
32		},
33		core::ops::ControlFlow,
34		fp_macros::*,
35	};
36
37	/// Static helper methods for [`ControlFlow`] values.
38	///
39	/// These methods mirror the inherent methods that were previously on the
40	/// `Step` type, now provided as associated functions on the brand.
41	///
42	/// ### Higher-Kinded Type Representation
43	///
44	/// [`ControlFlow`] has multiple higher-kinded representations:
45	/// - [`ControlFlowBrand`]: fully polymorphic over both continue and break types (bifunctor).
46	/// - [`ControlFlowContinueAppliedBrand<C>`](crate::brands::ControlFlowContinueAppliedBrand): the continue type is fixed, polymorphic over the break type (functor over break).
47	/// - [`ControlFlowBreakAppliedBrand<B>`](crate::brands::ControlFlowBreakAppliedBrand): the break type is fixed, polymorphic over the continue type (functor over continue).
48	impl ControlFlowBrand {
49		/// Returns `true` if this is a `Continue` variant.
50		#[document_signature]
51		///
52		#[document_type_parameters("The break type.", "The continue type.")]
53		///
54		#[document_parameters("The control flow value.")]
55		///
56		#[document_returns("`true` if the value is `Continue`, `false` otherwise.")]
57		///
58		#[inline]
59		#[document_examples]
60		///
61		/// ```
62		/// use {
63		/// 	core::ops::ControlFlow,
64		/// 	fp_library::brands::ControlFlowBrand,
65		/// };
66		///
67		/// let cf: ControlFlow<i32, i32> = ControlFlow::Continue(1);
68		/// assert!(ControlFlowBrand::is_continue(&cf));
69		/// ```
70		pub fn is_continue<B, C>(cf: &ControlFlow<B, C>) -> bool {
71			matches!(cf, ControlFlow::Continue(_))
72		}
73
74		/// Returns `true` if this is a `Break` variant.
75		#[document_signature]
76		///
77		#[document_type_parameters("The break type.", "The continue type.")]
78		///
79		#[document_parameters("The control flow value.")]
80		///
81		#[document_returns("`true` if the value is `Break`, `false` otherwise.")]
82		///
83		#[inline]
84		#[document_examples]
85		///
86		/// ```
87		/// use {
88		/// 	core::ops::ControlFlow,
89		/// 	fp_library::brands::ControlFlowBrand,
90		/// };
91		///
92		/// let cf: ControlFlow<i32, i32> = ControlFlow::Break(1);
93		/// assert!(ControlFlowBrand::is_break(&cf));
94		/// ```
95		pub fn is_break<B, C>(cf: &ControlFlow<B, C>) -> bool {
96			matches!(cf, ControlFlow::Break(_))
97		}
98
99		/// Maps a function over the `Continue` variant.
100		#[document_signature]
101		///
102		#[document_type_parameters(
103			"The break type.",
104			"The original continue type.",
105			"The new continue type."
106		)]
107		///
108		#[document_parameters(
109			"The control flow value.",
110			"The function to apply to the continue value."
111		)]
112		///
113		#[document_returns("A new `ControlFlow` with the continue value transformed.")]
114		///
115		#[document_examples]
116		///
117		/// ```
118		/// use {
119		/// 	core::ops::ControlFlow,
120		/// 	fp_library::brands::ControlFlowBrand,
121		/// };
122		///
123		/// let cf: ControlFlow<i32, i32> = ControlFlow::Continue(1);
124		/// let mapped = ControlFlowBrand::map_continue(cf, |x| x + 1);
125		/// assert_eq!(mapped, ControlFlow::Continue(2));
126		/// ```
127		pub fn map_continue<B, C, C2>(
128			cf: ControlFlow<B, C>,
129			f: impl FnOnce(C) -> C2,
130		) -> ControlFlow<B, C2> {
131			match cf {
132				ControlFlow::Continue(c) => ControlFlow::Continue(f(c)),
133				ControlFlow::Break(b) => ControlFlow::Break(b),
134			}
135		}
136
137		/// Maps a function over the `Break` variant.
138		#[document_signature]
139		///
140		#[document_type_parameters(
141			"The original break type.",
142			"The continue type.",
143			"The new break type."
144		)]
145		///
146		#[document_parameters(
147			"The control flow value.",
148			"The function to apply to the break value."
149		)]
150		///
151		#[document_returns("A new `ControlFlow` with the break value transformed.")]
152		///
153		#[document_examples]
154		///
155		/// ```
156		/// use {
157		/// 	core::ops::ControlFlow,
158		/// 	fp_library::brands::ControlFlowBrand,
159		/// };
160		///
161		/// let cf: ControlFlow<i32, i32> = ControlFlow::Break(1);
162		/// let mapped = ControlFlowBrand::map_break(cf, |x| x + 1);
163		/// assert_eq!(mapped, ControlFlow::Break(2));
164		/// ```
165		pub fn map_break<B, C, B2>(
166			cf: ControlFlow<B, C>,
167			f: impl FnOnce(B) -> B2,
168		) -> ControlFlow<B2, C> {
169			match cf {
170				ControlFlow::Continue(c) => ControlFlow::Continue(c),
171				ControlFlow::Break(b) => ControlFlow::Break(f(b)),
172			}
173		}
174
175		/// Applies functions to both variants (bifunctor map).
176		#[document_signature]
177		///
178		#[document_type_parameters(
179			"The original break type.",
180			"The original continue type.",
181			"The new break type.",
182			"The new continue type."
183		)]
184		///
185		#[document_parameters(
186			"The control flow value.",
187			"The function to apply to the continue value.",
188			"The function to apply to the break value."
189		)]
190		///
191		#[document_returns("A new `ControlFlow` with both values transformed.")]
192		#[document_examples]
193		///
194		/// ```
195		/// use {
196		/// 	core::ops::ControlFlow,
197		/// 	fp_library::brands::ControlFlowBrand,
198		/// };
199		///
200		/// let cf: ControlFlow<i32, i32> = ControlFlow::Continue(1);
201		/// let mapped = ControlFlowBrand::bimap(cf, |x| x + 1, |x| x * 2);
202		/// assert_eq!(mapped, ControlFlow::Continue(2));
203		/// ```
204		pub fn bimap<B, C, B2, C2>(
205			cf: ControlFlow<B, C>,
206			f: impl FnOnce(C) -> C2,
207			g: impl FnOnce(B) -> B2,
208		) -> ControlFlow<B2, C2> {
209			match cf {
210				ControlFlow::Continue(c) => ControlFlow::Continue(f(c)),
211				ControlFlow::Break(b) => ControlFlow::Break(g(b)),
212			}
213		}
214
215		/// Folds the control flow from right to left using two step functions.
216		///
217		/// See [`Bifoldable::bi_fold_right`] for the type class version.
218		#[document_signature]
219		///
220		#[document_type_parameters(
221			"The break type.",
222			"The continue type.",
223			"The accumulator type."
224		)]
225		///
226		#[document_parameters(
227			"The control flow value.",
228			"The step function for the Continue variant.",
229			"The step function for the Break variant.",
230			"The initial accumulator."
231		)]
232		///
233		#[document_returns("The result of folding.")]
234		///
235		#[document_examples]
236		///
237		/// ```
238		/// use {
239		/// 	core::ops::ControlFlow,
240		/// 	fp_library::brands::ControlFlowBrand,
241		/// };
242		///
243		/// let x: ControlFlow<i32, i32> = ControlFlow::Continue(3);
244		/// assert_eq!(ControlFlowBrand::bi_fold_right(x, |c, acc| acc - c, |b, acc| acc + b, 10), 7);
245		/// ```
246		pub fn bi_fold_right<B, C, Acc>(
247			cf: ControlFlow<B, C>,
248			f: impl FnOnce(C, Acc) -> Acc,
249			g: impl FnOnce(B, Acc) -> Acc,
250			z: Acc,
251		) -> Acc {
252			match cf {
253				ControlFlow::Continue(c) => f(c, z),
254				ControlFlow::Break(b) => g(b, z),
255			}
256		}
257
258		/// Folds the control flow from left to right using two step functions.
259		///
260		/// See [`Bifoldable::bi_fold_left`] for the type class version.
261		#[document_signature]
262		///
263		#[document_type_parameters(
264			"The break type.",
265			"The continue type.",
266			"The accumulator type."
267		)]
268		///
269		#[document_parameters(
270			"The control flow value.",
271			"The step function for the Continue variant.",
272			"The step function for the Break variant.",
273			"The initial accumulator."
274		)]
275		///
276		#[document_returns("The result of folding.")]
277		///
278		#[document_examples]
279		///
280		/// ```
281		/// use {
282		/// 	core::ops::ControlFlow,
283		/// 	fp_library::brands::ControlFlowBrand,
284		/// };
285		///
286		/// let x: ControlFlow<i32, i32> = ControlFlow::Break(5);
287		/// assert_eq!(ControlFlowBrand::bi_fold_left(x, |acc, c| acc - c, |acc, b| acc + b, 10), 15);
288		/// ```
289		pub fn bi_fold_left<B, C, Acc>(
290			cf: ControlFlow<B, C>,
291			f: impl FnOnce(Acc, C) -> Acc,
292			g: impl FnOnce(Acc, B) -> Acc,
293			z: Acc,
294		) -> Acc {
295			match cf {
296				ControlFlow::Continue(c) => f(z, c),
297				ControlFlow::Break(b) => g(z, b),
298			}
299		}
300
301		/// Maps the value to a monoid depending on the variant.
302		///
303		/// See [`Bifoldable::bi_fold_map`] for the type class version.
304		#[document_signature]
305		///
306		#[document_type_parameters("The break type.", "The continue type.", "The monoid type.")]
307		///
308		#[document_parameters(
309			"The control flow value.",
310			"The function mapping the Continue value to the monoid.",
311			"The function mapping the Break value to the monoid."
312		)]
313		///
314		#[document_returns("The monoid value.")]
315		///
316		#[document_examples]
317		///
318		/// ```
319		/// use {
320		/// 	core::ops::ControlFlow,
321		/// 	fp_library::brands::ControlFlowBrand,
322		/// };
323		///
324		/// let x: ControlFlow<i32, i32> = ControlFlow::Continue(3);
325		/// assert_eq!(
326		/// 	ControlFlowBrand::bi_fold_map(x, |c: i32| c.to_string(), |b: i32| b.to_string()),
327		/// 	"3"
328		/// );
329		/// ```
330		pub fn bi_fold_map<B, C, M>(
331			cf: ControlFlow<B, C>,
332			f: impl FnOnce(C) -> M,
333			g: impl FnOnce(B) -> M,
334		) -> M {
335			match cf {
336				ControlFlow::Continue(c) => f(c),
337				ControlFlow::Break(b) => g(b),
338			}
339		}
340
341		/// Folds the Break value, returning `initial` for Continue.
342		///
343		/// See [`Foldable::fold_right`] for the type class version
344		/// (via [`ControlFlowContinueAppliedBrand`]).
345		#[document_signature]
346		///
347		#[document_type_parameters(
348			"The break type.",
349			"The continue type.",
350			"The accumulator type."
351		)]
352		///
353		#[document_parameters(
354			"The control flow value.",
355			"The function to apply to the Break value and the accumulator.",
356			"The initial accumulator."
357		)]
358		///
359		#[document_returns("The result of folding.")]
360		///
361		#[document_examples]
362		///
363		/// ```
364		/// use {
365		/// 	core::ops::ControlFlow,
366		/// 	fp_library::brands::ControlFlowBrand,
367		/// };
368		///
369		/// let x: ControlFlow<i32, ()> = ControlFlow::Break(5);
370		/// assert_eq!(ControlFlowBrand::fold_right(x, |b, acc| b + acc, 10), 15);
371		/// ```
372		pub fn fold_right<B, C, Acc>(
373			cf: ControlFlow<B, C>,
374			f: impl FnOnce(B, Acc) -> Acc,
375			initial: Acc,
376		) -> Acc {
377			match cf {
378				ControlFlow::Continue(_) => initial,
379				ControlFlow::Break(b) => f(b, initial),
380			}
381		}
382
383		/// Folds the Break value from the left, returning `initial` for Continue.
384		///
385		/// See [`Foldable::fold_left`] for the type class version
386		/// (via [`ControlFlowContinueAppliedBrand`]).
387		#[document_signature]
388		///
389		#[document_type_parameters(
390			"The break type.",
391			"The continue type.",
392			"The accumulator type."
393		)]
394		///
395		#[document_parameters(
396			"The control flow value.",
397			"The function to apply to the accumulator and the Break value.",
398			"The initial accumulator."
399		)]
400		///
401		#[document_returns("The result of folding.")]
402		///
403		#[document_examples]
404		///
405		/// ```
406		/// use {
407		/// 	core::ops::ControlFlow,
408		/// 	fp_library::brands::ControlFlowBrand,
409		/// };
410		///
411		/// let x: ControlFlow<i32, ()> = ControlFlow::Break(5);
412		/// assert_eq!(ControlFlowBrand::fold_left(x, |acc, b| acc + b, 10), 15);
413		/// ```
414		pub fn fold_left<B, C, Acc>(
415			cf: ControlFlow<B, C>,
416			f: impl FnOnce(Acc, B) -> Acc,
417			initial: Acc,
418		) -> Acc {
419			match cf {
420				ControlFlow::Continue(_) => initial,
421				ControlFlow::Break(b) => f(initial, b),
422			}
423		}
424
425		/// Maps the Break value to a monoid, returning `M::empty()` for Continue.
426		///
427		/// See [`Foldable::fold_map`] for the type class version
428		/// (via [`ControlFlowContinueAppliedBrand`]).
429		#[document_signature]
430		///
431		#[document_type_parameters("The break type.", "The continue type.", "The monoid type.")]
432		///
433		#[document_parameters("The control flow value.", "The mapping function.")]
434		///
435		#[document_returns("The monoid value.")]
436		///
437		#[document_examples]
438		///
439		/// ```
440		/// use {
441		/// 	core::ops::ControlFlow,
442		/// 	fp_library::brands::ControlFlowBrand,
443		/// };
444		///
445		/// let x: ControlFlow<i32, ()> = ControlFlow::Break(5);
446		/// assert_eq!(ControlFlowBrand::fold_map(x, |b: i32| b.to_string()), "5".to_string());
447		/// ```
448		pub fn fold_map<B, C, M: Monoid>(
449			cf: ControlFlow<B, C>,
450			f: impl FnOnce(B) -> M,
451		) -> M {
452			match cf {
453				ControlFlow::Continue(_) => M::empty(),
454				ControlFlow::Break(b) => f(b),
455			}
456		}
457
458		/// Chains the Break value into a new computation, passing through Continue.
459		///
460		/// See [`Semimonad::bind`] for the type class version
461		/// (via [`ControlFlowContinueAppliedBrand`]).
462		#[document_signature]
463		///
464		#[document_type_parameters(
465			"The original break type.",
466			"The continue type.",
467			"The type of the resulting break value."
468		)]
469		///
470		#[document_parameters(
471			"The control flow value.",
472			"The function to apply to the Break value."
473		)]
474		///
475		#[document_returns("The result of the computation.")]
476		///
477		#[document_examples]
478		///
479		/// ```
480		/// use {
481		/// 	core::ops::ControlFlow,
482		/// 	fp_library::brands::ControlFlowBrand,
483		/// };
484		///
485		/// let x: ControlFlow<i32, i32> = ControlFlow::Break(5);
486		/// let y = ControlFlowBrand::bind_break(x, |b| ControlFlow::Break(b * 2));
487		/// assert_eq!(y, ControlFlow::Break(10));
488		/// ```
489		pub fn bind_break<B, C, B2>(
490			cf: ControlFlow<B, C>,
491			f: impl FnOnce(B) -> ControlFlow<B2, C>,
492		) -> ControlFlow<B2, C> {
493			match cf {
494				ControlFlow::Continue(c) => ControlFlow::Continue(c),
495				ControlFlow::Break(b) => f(b),
496			}
497		}
498
499		/// Chains the Continue value into a new computation, passing through Break.
500		///
501		/// See [`Semimonad::bind`] for the type class version
502		/// (via [`ControlFlowBreakAppliedBrand`]).
503		#[document_signature]
504		///
505		#[document_type_parameters(
506			"The break type.",
507			"The original continue type.",
508			"The type of the resulting continue value."
509		)]
510		///
511		#[document_parameters(
512			"The control flow value.",
513			"The function to apply to the Continue value."
514		)]
515		///
516		#[document_returns("The result of the computation.")]
517		///
518		#[document_examples]
519		///
520		/// ```
521		/// use {
522		/// 	core::ops::ControlFlow,
523		/// 	fp_library::brands::ControlFlowBrand,
524		/// };
525		///
526		/// let x: ControlFlow<i32, i32> = ControlFlow::Continue(5);
527		/// let y = ControlFlowBrand::bind_continue(x, |c| ControlFlow::Continue(c * 2));
528		/// assert_eq!(y, ControlFlow::Continue(10));
529		/// ```
530		pub fn bind_continue<B, C, C2>(
531			cf: ControlFlow<B, C>,
532			f: impl FnOnce(C) -> ControlFlow<B, C2>,
533		) -> ControlFlow<B, C2> {
534			match cf {
535				ControlFlow::Continue(c) => f(c),
536				ControlFlow::Break(b) => ControlFlow::Break(b),
537			}
538		}
539
540		/// Extracts the `Break` value, returning `None` if this is a `Continue`.
541		#[document_signature]
542		///
543		#[document_type_parameters("The break type.", "The continue type.")]
544		///
545		#[document_parameters("The control flow value.")]
546		///
547		#[document_returns("`Some(b)` if `Break(b)`, `None` if `Continue(_)`.")]
548		///
549		#[inline]
550		#[document_examples]
551		///
552		/// ```
553		/// use {
554		/// 	core::ops::ControlFlow,
555		/// 	fp_library::brands::ControlFlowBrand,
556		/// };
557		///
558		/// let cf: ControlFlow<i32, i32> = ControlFlow::Break(42);
559		/// assert_eq!(ControlFlowBrand::break_val(cf), Some(42));
560		///
561		/// let cf: ControlFlow<i32, i32> = ControlFlow::Continue(1);
562		/// assert_eq!(ControlFlowBrand::break_val(cf), None);
563		/// ```
564		pub fn break_val<B, C>(cf: ControlFlow<B, C>) -> Option<B> {
565			match cf {
566				ControlFlow::Break(b) => Some(b),
567				ControlFlow::Continue(_) => None,
568			}
569		}
570
571		/// Extracts the `Continue` value, returning `None` if this is `Break`.
572		#[document_signature]
573		///
574		#[document_type_parameters("The break type.", "The continue type.")]
575		///
576		#[document_parameters("The control flow value.")]
577		///
578		#[document_returns("`Some(c)` if `Continue(c)`, `None` if `Break(_)`.")]
579		///
580		#[inline]
581		#[document_examples]
582		///
583		/// ```
584		/// use {
585		/// 	core::ops::ControlFlow,
586		/// 	fp_library::brands::ControlFlowBrand,
587		/// };
588		///
589		/// let cf: ControlFlow<i32, i32> = ControlFlow::Continue(7);
590		/// assert_eq!(ControlFlowBrand::continue_val(cf), Some(7));
591		///
592		/// let cf: ControlFlow<i32, i32> = ControlFlow::Break(42);
593		/// assert_eq!(ControlFlowBrand::continue_val(cf), None);
594		/// ```
595		pub fn continue_val<B, C>(cf: ControlFlow<B, C>) -> Option<C> {
596			match cf {
597				ControlFlow::Continue(c) => Some(c),
598				ControlFlow::Break(_) => None,
599			}
600		}
601
602		/// Swaps the type parameters, mapping `Continue(c)` to `Break(c)` and `Break(b)` to `Continue(b)`.
603		#[document_signature]
604		///
605		#[document_type_parameters("The break type.", "The continue type.")]
606		///
607		#[document_parameters("The control flow value.")]
608		///
609		#[document_returns("A new `ControlFlow` with the variants swapped.")]
610		///
611		#[inline]
612		#[document_examples]
613		///
614		/// ```
615		/// use {
616		/// 	core::ops::ControlFlow,
617		/// 	fp_library::brands::ControlFlowBrand,
618		/// };
619		///
620		/// let cf: ControlFlow<&str, i32> = ControlFlow::Continue(1);
621		/// assert_eq!(ControlFlowBrand::swap(cf), ControlFlow::Break(1));
622		///
623		/// let cf: ControlFlow<&str, i32> = ControlFlow::Break("hello");
624		/// assert_eq!(ControlFlowBrand::swap(cf), ControlFlow::Continue("hello"));
625		/// ```
626		pub fn swap<B, C>(cf: ControlFlow<B, C>) -> ControlFlow<C, B> {
627			match cf {
628				ControlFlow::Continue(c) => ControlFlow::Break(c),
629				ControlFlow::Break(b) => ControlFlow::Continue(b),
630			}
631		}
632
633		/// Traverses the control flow with two effectful functions.
634		///
635		/// See [`Bitraversable::bi_traverse`] for the type class version.
636		#[document_signature]
637		///
638		#[document_type_parameters(
639			"The lifetime of the values.",
640			"The break type.",
641			"The continue type.",
642			"The output type for the Continue value.",
643			"The output type for the Break value.",
644			"The applicative context."
645		)]
646		///
647		#[document_parameters(
648			"The control flow value.",
649			"The function for the Continue value.",
650			"The function for the Break value."
651		)]
652		///
653		#[document_returns("The transformed control flow wrapped in the applicative context.")]
654		///
655		#[document_examples]
656		///
657		/// ```
658		/// use {
659		/// 	core::ops::ControlFlow,
660		/// 	fp_library::brands::{
661		/// 		ControlFlowBrand,
662		/// 		OptionBrand,
663		/// 	},
664		/// };
665		///
666		/// let x: ControlFlow<i32, i32> = ControlFlow::Continue(3);
667		/// let y = ControlFlowBrand::bi_traverse::<_, _, _, _, OptionBrand>(
668		/// 	x,
669		/// 	|c| Some(c + 1),
670		/// 	|b| Some(b * 2),
671		/// );
672		/// assert_eq!(y, Some(ControlFlow::Continue(4)));
673		/// ```
674		pub fn bi_traverse<'a, B: 'a, C: 'a, C2: 'a + Clone, B2: 'a + Clone, F: Applicative>(
675			cf: ControlFlow<B, C>,
676			f: impl Fn(C) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C2>) + 'a,
677			g: impl Fn(B) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B2>) + 'a,
678		) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B2, C2>>)
679		where
680			ControlFlow<B2, C2>: Clone, {
681			match cf {
682				ControlFlow::Continue(c) => F::map(|c2| ControlFlow::Continue(c2), f(c)),
683				ControlFlow::Break(b) => F::map(|b2| ControlFlow::Break(b2), g(b)),
684			}
685		}
686	}
687
688	// HKT Branding
689
690	impl_kind! {
691		for ControlFlowBrand {
692			type Of<C, B> = ControlFlow<B, C>;
693		}
694	}
695
696	impl_kind! {
697		for ControlFlowBrand {
698			type Of<'a, C: 'a, B: 'a>: 'a = ControlFlow<B, C>;
699		}
700	}
701
702	impl Bifunctor for ControlFlowBrand {
703		/// Maps functions over the values in the control flow.
704		///
705		/// This method applies one function to the continue value and another to the break value.
706		#[document_signature]
707		///
708		#[document_type_parameters(
709			"The lifetime of the values.",
710			"The type of the continue value.",
711			"The type of the mapped continue value.",
712			"The type of the break value.",
713			"The type of the mapped break value."
714		)]
715		///
716		#[document_parameters(
717			"The function to apply to the continue value.",
718			"The function to apply to the break value.",
719			"The control flow to map over."
720		)]
721		///
722		#[document_returns("A new control flow containing the mapped values.")]
723		#[document_examples]
724		///
725		/// ```
726		/// use {
727		/// 	core::ops::ControlFlow,
728		/// 	fp_library::{
729		/// 		brands::*,
730		/// 		functions::*,
731		/// 	},
732		/// };
733		///
734		/// let x = ControlFlow::<i32, i32>::Continue(1);
735		/// assert_eq!(
736		/// 	explicit::bimap::<ControlFlowBrand, _, _, _, _, _, _>((|c| c + 1, |b: i32| b * 2), x),
737		/// 	ControlFlow::Continue(2)
738		/// );
739		/// ```
740		fn bimap<'a, A: 'a, B: 'a, C: 'a, D: 'a>(
741			f: impl Fn(A) -> B + 'a,
742			g: impl Fn(C) -> D + 'a,
743			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
744		) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>) {
745			ControlFlowBrand::bimap(p, f, g)
746		}
747	}
748
749	impl RefBifunctor for ControlFlowBrand {
750		/// Maps functions over the values in the control flow by reference.
751		///
752		/// This method applies one function to a reference of the continue value and another
753		/// to a reference of the break value, producing a new control flow with mapped values.
754		/// The original control flow is borrowed, not consumed.
755		#[document_signature]
756		///
757		#[document_type_parameters(
758			"The lifetime of the values.",
759			"The type of the continue value.",
760			"The type of the mapped continue value.",
761			"The type of the break value.",
762			"The type of the mapped break value."
763		)]
764		///
765		#[document_parameters(
766			"The function to apply to a reference of the continue value.",
767			"The function to apply to a reference of the break value.",
768			"The control flow to map over by reference."
769		)]
770		///
771		#[document_returns("A new control flow containing the mapped values.")]
772		#[document_examples]
773		///
774		/// ```
775		/// use {
776		/// 	core::ops::ControlFlow,
777		/// 	fp_library::{
778		/// 		brands::*,
779		/// 		classes::ref_bifunctor::*,
780		/// 		functions::*,
781		/// 	},
782		/// };
783		///
784		/// let x = ControlFlow::<i32, i32>::Continue(1);
785		/// assert_eq!(
786		/// 	ref_bimap::<ControlFlowBrand, _, _, _, _>(|c| *c + 1, |b: &i32| *b * 2, &x),
787		/// 	ControlFlow::Continue(2)
788		/// );
789		///
790		/// let y = ControlFlow::<i32, i32>::Break(3);
791		/// assert_eq!(
792		/// 	ref_bimap::<ControlFlowBrand, _, _, _, _>(|c| *c + 1, |b: &i32| *b * 2, &y),
793		/// 	ControlFlow::Break(6)
794		/// );
795		/// ```
796		fn ref_bimap<'a, A: 'a, B: 'a, C: 'a, D: 'a>(
797			f: impl Fn(&A) -> B + 'a,
798			g: impl Fn(&C) -> D + 'a,
799			p: &Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
800		) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>) {
801			match p {
802				ControlFlow::Continue(a) => ControlFlow::Continue(f(a)),
803				ControlFlow::Break(c) => ControlFlow::Break(g(c)),
804			}
805		}
806	}
807
808	impl RefBifoldable for ControlFlowBrand {
809		/// Folds the control flow from right to left by reference using two step functions.
810		///
811		/// Applies `f` to a reference of the Continue value or `g` to a reference of the
812		/// Break value, returning the folded result without consuming the control flow.
813		#[document_signature]
814		///
815		#[document_type_parameters(
816			"The lifetime of the values.",
817			"The brand of the cloneable function to use.",
818			"The type of the Continue value.",
819			"The type of the Break value.",
820			"The accumulator type."
821		)]
822		///
823		#[document_parameters(
824			"The step function for a reference to the Continue variant.",
825			"The step function for a reference to the Break variant.",
826			"The initial accumulator.",
827			"The control flow to fold by reference."
828		)]
829		///
830		#[document_returns("The folded result.")]
831		#[document_examples]
832		///
833		/// ```
834		/// use {
835		/// 	core::ops::ControlFlow,
836		/// 	fp_library::{
837		/// 		brands::*,
838		/// 		functions::*,
839		/// 	},
840		/// };
841		///
842		/// let x: ControlFlow<i32, i32> = ControlFlow::Continue(3);
843		/// assert_eq!(
844		/// 	explicit::bi_fold_right::<RcFnBrand, ControlFlowBrand, _, _, _, _, _>(
845		/// 		(|c: &i32, acc| acc - *c, |b: &i32, acc| acc + *b),
846		/// 		10,
847		/// 		&x,
848		/// 	),
849		/// 	7
850		/// );
851		///
852		/// let y: ControlFlow<i32, i32> = ControlFlow::Break(5);
853		/// assert_eq!(
854		/// 	explicit::bi_fold_right::<RcFnBrand, ControlFlowBrand, _, _, _, _, _>(
855		/// 		(|c: &i32, acc| acc - *c, |b: &i32, acc| acc + *b),
856		/// 		10,
857		/// 		&y,
858		/// 	),
859		/// 	15
860		/// );
861		/// ```
862		fn ref_bi_fold_right<'a, FnBrand: LiftFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
863			f: impl Fn(&A, C) -> C + 'a,
864			g: impl Fn(&B, C) -> C + 'a,
865			z: C,
866			p: &Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
867		) -> C {
868			match p {
869				ControlFlow::Continue(a) => f(a, z),
870				ControlFlow::Break(b) => g(b, z),
871			}
872		}
873	}
874
875	impl RefBitraversable for ControlFlowBrand {
876		/// Traverses a control flow by reference with two effectful functions.
877		///
878		/// Applies `f` to a reference of the Continue value or `g` to a reference of
879		/// the Break value, wrapping the result in the applicative context `F`.
880		#[document_signature]
881		///
882		#[document_type_parameters(
883			"The lifetime of the values.",
884			"The brand of the cloneable function wrapper.",
885			"The type of the Continue value.",
886			"The type of the Break value.",
887			"The output type for Continue.",
888			"The output type for Break.",
889			"The applicative context."
890		)]
891		///
892		#[document_parameters(
893			"The function applied to a reference of the Continue value.",
894			"The function applied to a reference of the Break value.",
895			"The control flow to traverse by reference."
896		)]
897		///
898		#[document_returns(
899			"`f(&a)` wrapped in context for `Continue(a)`, or `g(&b)` wrapped in context for `Break(b)`."
900		)]
901		#[document_examples]
902		///
903		/// ```
904		/// use {
905		/// 	core::ops::ControlFlow,
906		/// 	fp_library::{
907		/// 		brands::*,
908		/// 		functions::*,
909		/// 	},
910		/// };
911		///
912		/// let x: ControlFlow<i32, i32> = ControlFlow::Continue(3);
913		/// assert_eq!(
914		/// 	explicit::bi_traverse::<RcFnBrand, ControlFlowBrand, _, _, _, _, OptionBrand, _, _>(
915		/// 		(|c: &i32| Some(c + 1), |b: &i32| Some(b * 2)),
916		/// 		&x,
917		/// 	),
918		/// 	Some(ControlFlow::Continue(4))
919		/// );
920		///
921		/// let y: ControlFlow<i32, i32> = ControlFlow::Break(5);
922		/// assert_eq!(
923		/// 	explicit::bi_traverse::<RcFnBrand, ControlFlowBrand, _, _, _, _, OptionBrand, _, _>(
924		/// 		(|c: &i32| Some(c + 1), |b: &i32| Some(b * 2)),
925		/// 		&y,
926		/// 	),
927		/// 	Some(ControlFlow::Break(10))
928		/// );
929		/// ```
930		fn ref_bi_traverse<
931			'a,
932			FnBrand,
933			A: 'a + Clone,
934			B: 'a + Clone,
935			C: 'a + Clone,
936			D: 'a + Clone,
937			F: Applicative,
938		>(
939			f: impl Fn(&A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) + 'a,
940			g: impl Fn(&B) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, D>) + 'a,
941			p: &Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
942		) -> 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>)>)
943		where
944			FnBrand: LiftFn + 'a,
945			Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, C, D>): Clone,
946			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>): Clone,
947			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, D>): Clone, {
948			match p {
949				ControlFlow::Continue(a) => F::map(|c| ControlFlow::Continue(c), f(a)),
950				ControlFlow::Break(b) => F::map(|d| ControlFlow::Break(d), g(b)),
951			}
952		}
953	}
954
955	impl Bifoldable for ControlFlowBrand {
956		/// Folds the control flow from right to left using two step functions.
957		///
958		/// Applies `f` to the Continue value or `g` to the Break value.
959		#[document_signature]
960		///
961		#[document_type_parameters(
962			"The lifetime of the values.",
963			"The brand of the cloneable function to use.",
964			"The type of the Continue value.",
965			"The type of the Break value.",
966			"The accumulator type."
967		)]
968		///
969		#[document_parameters(
970			"The step function for the Continue variant.",
971			"The step function for the Break variant.",
972			"The initial accumulator.",
973			"The control flow to fold."
974		)]
975		///
976		#[document_returns("The folded result.")]
977		#[document_examples]
978		///
979		/// ```
980		/// use {
981		/// 	core::ops::ControlFlow,
982		/// 	fp_library::{
983		/// 		brands::*,
984		/// 		functions::*,
985		/// 	},
986		/// };
987		///
988		/// let x: ControlFlow<i32, i32> = ControlFlow::Continue(3);
989		/// assert_eq!(
990		/// 	explicit::bi_fold_right::<RcFnBrand, ControlFlowBrand, _, _, _, _, _>(
991		/// 		(|c, acc| acc - c, |b, acc| acc + b),
992		/// 		10,
993		/// 		x,
994		/// 	),
995		/// 	7
996		/// );
997		/// ```
998		fn bi_fold_right<'a, FnBrand: CloneFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
999			f: impl Fn(A, C) -> C + 'a,
1000			g: impl Fn(B, C) -> C + 'a,
1001			z: C,
1002			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
1003		) -> C {
1004			ControlFlowBrand::bi_fold_right(p, f, g, z)
1005		}
1006
1007		/// Folds the control flow from left to right using two step functions.
1008		///
1009		/// Applies `f` to the Continue value or `g` to the Break value.
1010		#[document_signature]
1011		///
1012		#[document_type_parameters(
1013			"The lifetime of the values.",
1014			"The brand of the cloneable function to use.",
1015			"The type of the Continue value.",
1016			"The type of the Break value.",
1017			"The accumulator type."
1018		)]
1019		///
1020		#[document_parameters(
1021			"The step function for the Continue variant.",
1022			"The step function for the Break variant.",
1023			"The initial accumulator.",
1024			"The control flow to fold."
1025		)]
1026		///
1027		#[document_returns("The folded result.")]
1028		#[document_examples]
1029		///
1030		/// ```
1031		/// use {
1032		/// 	core::ops::ControlFlow,
1033		/// 	fp_library::{
1034		/// 		brands::*,
1035		/// 		functions::*,
1036		/// 	},
1037		/// };
1038		///
1039		/// let x: ControlFlow<i32, i32> = ControlFlow::Break(5);
1040		/// assert_eq!(
1041		/// 	explicit::bi_fold_left::<RcFnBrand, ControlFlowBrand, _, _, _, _, _>(
1042		/// 		(|acc, c| acc - c, |acc, b| acc + b),
1043		/// 		10,
1044		/// 		x,
1045		/// 	),
1046		/// 	15
1047		/// );
1048		/// ```
1049		fn bi_fold_left<'a, FnBrand: CloneFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
1050			f: impl Fn(C, A) -> C + 'a,
1051			g: impl Fn(C, B) -> C + 'a,
1052			z: C,
1053			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
1054		) -> C {
1055			ControlFlowBrand::bi_fold_left(p, f, g, z)
1056		}
1057
1058		/// Maps the value to a monoid depending on the variant.
1059		///
1060		/// Applies `f` if Continue, `g` if Break.
1061		#[document_signature]
1062		///
1063		#[document_type_parameters(
1064			"The lifetime of the values.",
1065			"The brand of the cloneable function to use.",
1066			"The type of the Continue value.",
1067			"The type of the Break value.",
1068			"The monoid type."
1069		)]
1070		///
1071		#[document_parameters(
1072			"The function mapping the Continue value to the monoid.",
1073			"The function mapping the Break value to the monoid.",
1074			"The control flow to fold."
1075		)]
1076		///
1077		#[document_returns("The monoid value.")]
1078		#[document_examples]
1079		///
1080		/// ```
1081		/// use {
1082		/// 	core::ops::ControlFlow,
1083		/// 	fp_library::{
1084		/// 		brands::*,
1085		/// 		functions::*,
1086		/// 	},
1087		/// };
1088		///
1089		/// let x: ControlFlow<i32, i32> = ControlFlow::Continue(3);
1090		/// assert_eq!(
1091		/// 	explicit::bi_fold_map::<RcFnBrand, ControlFlowBrand, _, _, _, _, _>(
1092		/// 		(|c: i32| c.to_string(), |b: i32| b.to_string()),
1093		/// 		x,
1094		/// 	),
1095		/// 	"3".to_string()
1096		/// );
1097		/// ```
1098		fn bi_fold_map<'a, FnBrand: CloneFn + 'a, A: 'a + Clone, B: 'a + Clone, M>(
1099			f: impl Fn(A) -> M + 'a,
1100			g: impl Fn(B) -> M + 'a,
1101			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
1102		) -> M
1103		where
1104			M: Monoid + 'a, {
1105			ControlFlowBrand::bi_fold_map(p, f, g)
1106		}
1107	}
1108
1109	impl Bitraversable for ControlFlowBrand {
1110		/// Traverses the control flow with two effectful functions.
1111		///
1112		/// Applies `f` to the Continue value or `g` to the Break value,
1113		/// wrapping the result in the applicative context.
1114		#[document_signature]
1115		///
1116		#[document_type_parameters(
1117			"The lifetime of the values.",
1118			"The type of the Continue value.",
1119			"The type of the Break value.",
1120			"The output type for Continue.",
1121			"The output type for Break.",
1122			"The applicative context."
1123		)]
1124		///
1125		#[document_parameters(
1126			"The function applied to the Continue value.",
1127			"The function applied to the Break value.",
1128			"The control flow to traverse."
1129		)]
1130		///
1131		#[document_returns("The transformed control flow wrapped in the applicative context.")]
1132		#[document_examples]
1133		///
1134		/// ```
1135		/// use {
1136		/// 	core::ops::ControlFlow,
1137		/// 	fp_library::{
1138		/// 		brands::*,
1139		/// 		functions::*,
1140		/// 	},
1141		/// };
1142		///
1143		/// let x: ControlFlow<i32, i32> = ControlFlow::Continue(3);
1144		/// assert_eq!(
1145		/// 	explicit::bi_traverse::<RcFnBrand, ControlFlowBrand, _, _, _, _, OptionBrand, _, _>(
1146		/// 		(|c: i32| Some(c + 1), |b: i32| Some(b * 2)),
1147		/// 		x,
1148		/// 	),
1149		/// 	Some(ControlFlow::Continue(4))
1150		/// );
1151		/// ```
1152		fn bi_traverse<
1153			'a,
1154			A: 'a + Clone,
1155			B: 'a + Clone,
1156			C: 'a + Clone,
1157			D: 'a + Clone,
1158			F: Applicative,
1159		>(
1160			f: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) + 'a,
1161			g: impl Fn(B) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, D>) + 'a,
1162			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
1163		) -> 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>)>)
1164		{
1165			ControlFlowBrand::bi_traverse::<_, _, C, D, F>(p, f, g)
1166		}
1167	}
1168
1169	// ControlFlowContinueAppliedBrand<ContinueType> (Functor over B, the Break type)
1170
1171	impl_kind! {
1172		#[multi_brand]
1173		impl<ContinueType: 'static> for ControlFlowContinueAppliedBrand<ContinueType> {
1174			type Of<'a, B: 'a>: 'a = ControlFlow<B, ContinueType>;
1175		}
1176	}
1177
1178	#[document_type_parameters("The continue type.")]
1179	impl<ContinueType: 'static> Functor for ControlFlowContinueAppliedBrand<ContinueType> {
1180		/// Maps a function over the break value in the control flow.
1181		///
1182		/// This method applies a function to the break value inside the control flow, producing a new control flow with the transformed break value. The continue value remains unchanged.
1183		#[document_signature]
1184		///
1185		#[document_type_parameters(
1186			"The lifetime of the values.",
1187			"The type of the break value.",
1188			"The type of the result of applying the function."
1189		)]
1190		///
1191		#[document_parameters(
1192			"The function to apply to the break value.",
1193			"The control flow to map over."
1194		)]
1195		///
1196		#[document_returns(
1197			"A new control flow containing the result of applying the function to the break value."
1198		)]
1199		///
1200		#[document_examples]
1201		///
1202		/// ```
1203		/// use {
1204		/// 	core::ops::ControlFlow,
1205		/// 	fp_library::{
1206		/// 		brands::*,
1207		/// 		functions::*,
1208		/// 	},
1209		/// };
1210		///
1211		/// assert_eq!(
1212		/// 	explicit::map::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(
1213		/// 		|x: i32| x * 2,
1214		/// 		ControlFlow::<i32, i32>::Break(5)
1215		/// 	),
1216		/// 	ControlFlow::Break(10)
1217		/// );
1218		/// ```
1219		fn map<'a, A: 'a, B: 'a>(
1220			func: impl Fn(A) -> B + 'a,
1221			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1222		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1223			ControlFlowBrand::map_break(fa, func)
1224		}
1225	}
1226
1227	#[document_type_parameters("The continue type.")]
1228	impl<ContinueType: Clone + 'static> Lift for ControlFlowContinueAppliedBrand<ContinueType> {
1229		/// Lifts a binary function into the control flow context.
1230		///
1231		/// This method lifts a binary function to operate on values within the control flow context.
1232		#[document_signature]
1233		///
1234		#[document_type_parameters(
1235			"The lifetime of the values.",
1236			"The type of the first value.",
1237			"The type of the second value.",
1238			"The type of the result."
1239		)]
1240		///
1241		#[document_parameters(
1242			"The binary function to apply.",
1243			"The first control flow.",
1244			"The second control flow."
1245		)]
1246		///
1247		#[document_returns(
1248			"`Break(f(a, b))` if both are `Break`, otherwise the first continue encountered."
1249		)]
1250		#[document_examples]
1251		///
1252		/// ```
1253		/// use {
1254		/// 	core::ops::ControlFlow,
1255		/// 	fp_library::{
1256		/// 		brands::*,
1257		/// 		functions::*,
1258		/// 	},
1259		/// };
1260		///
1261		/// assert_eq!(
1262		/// 	explicit::lift2::<ControlFlowContinueAppliedBrand<()>, _, _, _, _, _, _>(
1263		/// 		|x: i32, y: i32| x + y,
1264		/// 		ControlFlow::Break(1),
1265		/// 		ControlFlow::Break(2)
1266		/// 	),
1267		/// 	ControlFlow::Break(3)
1268		/// );
1269		/// assert_eq!(
1270		/// 	explicit::lift2::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _, _, _>(
1271		/// 		|x: i32, y: i32| x + y,
1272		/// 		ControlFlow::Break(1),
1273		/// 		ControlFlow::Continue(2)
1274		/// 	),
1275		/// 	ControlFlow::Continue(2)
1276		/// );
1277		/// ```
1278		fn lift2<'a, A, B, C>(
1279			func: impl Fn(A, B) -> C + 'a,
1280			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1281			fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1282		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
1283		where
1284			A: Clone + 'a,
1285			B: Clone + 'a,
1286			C: 'a, {
1287			match (fa, fb) {
1288				(ControlFlow::Break(a), ControlFlow::Break(b)) => ControlFlow::Break(func(a, b)),
1289				(ControlFlow::Continue(e), _) => ControlFlow::Continue(e),
1290				(_, ControlFlow::Continue(e)) => ControlFlow::Continue(e),
1291			}
1292		}
1293	}
1294
1295	#[document_type_parameters("The continue type.")]
1296	impl<ContinueType: 'static> Pointed for ControlFlowContinueAppliedBrand<ContinueType> {
1297		/// Wraps a value in a control flow.
1298		///
1299		/// This method wraps a value in the `Break` variant of a `ControlFlow`.
1300		#[document_signature]
1301		///
1302		#[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
1303		///
1304		#[document_parameters("The value to wrap.")]
1305		///
1306		#[document_returns("`Break(a)`.")]
1307		///
1308		#[document_examples]
1309		///
1310		/// ```
1311		/// use {
1312		/// 	core::ops::ControlFlow,
1313		/// 	fp_library::{
1314		/// 		brands::*,
1315		/// 		functions::*,
1316		/// 	},
1317		/// };
1318		///
1319		/// assert_eq!(pure::<ControlFlowContinueAppliedBrand<()>, _>(5), ControlFlow::<_, ()>::Break(5));
1320		/// ```
1321		fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1322			ControlFlow::Break(a)
1323		}
1324	}
1325
1326	#[document_type_parameters("The continue type.")]
1327	impl<ContinueType: Clone + 'static> ApplyFirst for ControlFlowContinueAppliedBrand<ContinueType> {}
1328
1329	#[document_type_parameters("The continue type.")]
1330	impl<ContinueType: Clone + 'static> ApplySecond for ControlFlowContinueAppliedBrand<ContinueType> {}
1331
1332	#[document_type_parameters("The continue type.")]
1333	impl<ContinueType: Clone + 'static> Semiapplicative
1334		for ControlFlowContinueAppliedBrand<ContinueType>
1335	{
1336		/// Applies a wrapped function to a wrapped value.
1337		///
1338		/// This method applies a function wrapped in a control flow to a value wrapped in a control flow.
1339		#[document_signature]
1340		///
1341		#[document_type_parameters(
1342			"The lifetime of the values.",
1343			"The brand of the cloneable function wrapper.",
1344			"The type of the input value.",
1345			"The type of the output value."
1346		)]
1347		///
1348		#[document_parameters(
1349			"The control flow containing the function.",
1350			"The control flow containing the value."
1351		)]
1352		///
1353		#[document_returns(
1354			"`Break(f(a))` if both are `Break`, otherwise the first continue encountered."
1355		)]
1356		#[document_examples]
1357		///
1358		/// ```
1359		/// use {
1360		/// 	core::ops::ControlFlow,
1361		/// 	fp_library::{
1362		/// 		brands::*,
1363		/// 		classes::semiapplicative::apply as explicit_apply,
1364		/// 		functions::*,
1365		/// 	},
1366		/// };
1367		///
1368		/// let f: ControlFlow<_, ()> = ControlFlow::Break(lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
1369		/// assert_eq!(
1370		/// 	explicit_apply::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _>(
1371		/// 		f,
1372		/// 		ControlFlow::Break(5)
1373		/// 	),
1374		/// 	ControlFlow::Break(10)
1375		/// );
1376		/// ```
1377		fn apply<'a, FnBrand: 'a + CloneFn, A: 'a + Clone, B: 'a>(
1378			ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, A, B>>),
1379			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1380		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1381			match (ff, fa) {
1382				(ControlFlow::Break(f), ControlFlow::Break(a)) => ControlFlow::Break(f(a)),
1383				(ControlFlow::Continue(e), _) => ControlFlow::Continue(e),
1384				(_, ControlFlow::Continue(e)) => ControlFlow::Continue(e),
1385			}
1386		}
1387	}
1388
1389	#[document_type_parameters("The continue type.")]
1390	impl<ContinueType: Clone + 'static> Semimonad for ControlFlowContinueAppliedBrand<ContinueType> {
1391		/// Chains control flow computations.
1392		///
1393		/// This method chains two computations, where the second computation depends on the result of the first.
1394		#[document_signature]
1395		///
1396		#[document_type_parameters(
1397			"The lifetime of the values.",
1398			"The type of the result of the first computation.",
1399			"The type of the result of the second computation."
1400		)]
1401		///
1402		#[document_parameters(
1403			"The first control flow.",
1404			"The function to apply to the value inside the control flow."
1405		)]
1406		///
1407		#[document_returns(
1408			"The result of applying `f` to the value if `ma` is `Break`, otherwise the original continue."
1409		)]
1410		#[document_examples]
1411		///
1412		/// ```
1413		/// use {
1414		/// 	core::ops::ControlFlow,
1415		/// 	fp_library::{
1416		/// 		brands::*,
1417		/// 		functions::*,
1418		/// 	},
1419		/// };
1420		///
1421		/// assert_eq!(
1422		/// 	explicit::bind::<ControlFlowContinueAppliedBrand<()>, _, _, _, _>(
1423		/// 		ControlFlow::Break(5),
1424		/// 		|x| { ControlFlow::Break(x * 2) }
1425		/// 	),
1426		/// 	ControlFlow::<_, ()>::Break(10)
1427		/// );
1428		/// ```
1429		fn bind<'a, A: 'a, B: 'a>(
1430			ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1431			func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1432		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1433			ControlFlowBrand::bind_break(ma, func)
1434		}
1435	}
1436
1437	#[document_type_parameters("The continue type.")]
1438	impl<ContinueType: 'static> Foldable for ControlFlowContinueAppliedBrand<ContinueType> {
1439		/// Folds the control flow from the right.
1440		///
1441		/// This method performs a right-associative fold of the control flow.
1442		#[document_signature]
1443		///
1444		#[document_type_parameters(
1445			"The lifetime of the values.",
1446			"The brand of the cloneable function to use.",
1447			"The type of the elements in the structure.",
1448			"The type of the accumulator."
1449		)]
1450		///
1451		#[document_parameters(
1452			"The folding function.",
1453			"The initial value.",
1454			"The control flow to fold."
1455		)]
1456		///
1457		#[document_returns("`func(a, initial)` if `fa` is `Break(a)`, otherwise `initial`.")]
1458		///
1459		#[document_examples]
1460		///
1461		/// ```
1462		/// use {
1463		/// 	core::ops::ControlFlow,
1464		/// 	fp_library::{
1465		/// 		brands::*,
1466		/// 		functions::*,
1467		/// 	},
1468		/// };
1469		///
1470		/// assert_eq!(
1471		/// 	explicit::fold_right::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _, _, _>(
1472		/// 		|x, acc| x + acc,
1473		/// 		0,
1474		/// 		ControlFlow::Break(5)
1475		/// 	),
1476		/// 	5
1477		/// );
1478		/// assert_eq!(
1479		/// 	explicit::fold_right::<RcFnBrand, ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(
1480		/// 		|x: i32, acc| x + acc,
1481		/// 		0,
1482		/// 		ControlFlow::Continue(1)
1483		/// 	),
1484		/// 	0
1485		/// );
1486		/// ```
1487		fn fold_right<'a, FnBrand, A: 'a, B: 'a>(
1488			func: impl Fn(A, B) -> B + 'a,
1489			initial: B,
1490			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1491		) -> B
1492		where
1493			FnBrand: CloneFn + 'a, {
1494			ControlFlowBrand::fold_right(fa, func, initial)
1495		}
1496
1497		/// Folds the control flow from the left.
1498		///
1499		/// This method performs a left-associative fold of the control flow.
1500		#[document_signature]
1501		///
1502		#[document_type_parameters(
1503			"The lifetime of the values.",
1504			"The brand of the cloneable function to use.",
1505			"The type of the elements in the structure.",
1506			"The type of the accumulator."
1507		)]
1508		///
1509		#[document_parameters(
1510			"The folding function.",
1511			"The initial value.",
1512			"The control flow to fold."
1513		)]
1514		///
1515		#[document_returns("`func(initial, a)` if `fa` is `Break(a)`, otherwise `initial`.")]
1516		///
1517		#[document_examples]
1518		///
1519		/// ```
1520		/// use {
1521		/// 	core::ops::ControlFlow,
1522		/// 	fp_library::{
1523		/// 		brands::*,
1524		/// 		functions::*,
1525		/// 	},
1526		/// };
1527		///
1528		/// assert_eq!(
1529		/// 	explicit::fold_left::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _, _, _>(
1530		/// 		|acc, x| acc + x,
1531		/// 		0,
1532		/// 		ControlFlow::Break(5)
1533		/// 	),
1534		/// 	5
1535		/// );
1536		/// assert_eq!(
1537		/// 	explicit::fold_left::<RcFnBrand, ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(
1538		/// 		|acc, x: i32| acc + x,
1539		/// 		0,
1540		/// 		ControlFlow::Continue(1)
1541		/// 	),
1542		/// 	0
1543		/// );
1544		/// ```
1545		fn fold_left<'a, FnBrand, A: 'a, B: 'a>(
1546			func: impl Fn(B, A) -> B + 'a,
1547			initial: B,
1548			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1549		) -> B
1550		where
1551			FnBrand: CloneFn + 'a, {
1552			ControlFlowBrand::fold_left(fa, func, initial)
1553		}
1554
1555		/// Maps the value to a monoid and returns it.
1556		///
1557		/// This method maps the element of the control flow to a monoid and then returns it.
1558		#[document_signature]
1559		///
1560		#[document_type_parameters(
1561			"The lifetime of the values.",
1562			"The brand of the cloneable function to use.",
1563			"The type of the elements in the structure.",
1564			"The type of the monoid."
1565		)]
1566		///
1567		#[document_parameters("The mapping function.", "The control flow to fold.")]
1568		///
1569		#[document_returns("`func(a)` if `fa` is `Break(a)`, otherwise `M::empty()`.")]
1570		///
1571		#[document_examples]
1572		///
1573		/// ```
1574		/// use {
1575		/// 	core::ops::ControlFlow,
1576		/// 	fp_library::{
1577		/// 		brands::*,
1578		/// 		functions::*,
1579		/// 	},
1580		/// };
1581		///
1582		/// assert_eq!(
1583		/// 	explicit::fold_map::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _, _, _>(
1584		/// 		|x: i32| x.to_string(),
1585		/// 		ControlFlow::Break(5)
1586		/// 	),
1587		/// 	"5".to_string()
1588		/// );
1589		/// assert_eq!(
1590		/// 	explicit::fold_map::<RcFnBrand, ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(
1591		/// 		|x: i32| x.to_string(),
1592		/// 		ControlFlow::Continue(1)
1593		/// 	),
1594		/// 	"".to_string()
1595		/// );
1596		/// ```
1597		fn fold_map<'a, FnBrand, A: 'a, M>(
1598			func: impl Fn(A) -> M + 'a,
1599			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1600		) -> M
1601		where
1602			M: Monoid + 'a,
1603			FnBrand: CloneFn + 'a, {
1604			ControlFlowBrand::fold_map(fa, func)
1605		}
1606	}
1607
1608	#[document_type_parameters("The continue type.")]
1609	impl<ContinueType: Clone + 'static> Traversable for ControlFlowContinueAppliedBrand<ContinueType> {
1610		/// Traverses the control flow with an applicative function.
1611		///
1612		/// This method maps the element of the control flow to a computation, evaluates it, and combines the result into an applicative context.
1613		#[document_signature]
1614		///
1615		#[document_type_parameters(
1616			"The lifetime of the values.",
1617			"The type of the elements in the traversable structure.",
1618			"The type of the elements in the resulting traversable structure.",
1619			"The applicative context."
1620		)]
1621		///
1622		#[document_parameters("The function to apply.", "The control flow to traverse.")]
1623		///
1624		#[document_returns("The control flow wrapped in the applicative context.")]
1625		///
1626		#[document_examples]
1627		///
1628		/// ```
1629		/// use {
1630		/// 	core::ops::ControlFlow,
1631		/// 	fp_library::{
1632		/// 		brands::*,
1633		/// 		functions::*,
1634		/// 	},
1635		/// };
1636		///
1637		/// assert_eq!(
1638		/// 	explicit::traverse::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _, OptionBrand, _, _>(
1639		/// 		|x| Some(x * 2),
1640		/// 		ControlFlow::Break(5)
1641		/// 	),
1642		/// 	Some(ControlFlow::Break(10))
1643		/// );
1644		/// assert_eq!(
1645		/// 	explicit::traverse::<
1646		/// 		RcFnBrand,
1647		/// 		ControlFlowContinueAppliedBrand<i32>,
1648		/// 		_,
1649		/// 		_,
1650		/// 		OptionBrand,
1651		/// 		_,
1652		/// 		_,
1653		/// 	>(|x: i32| Some(x * 2), ControlFlow::Continue(1)),
1654		/// 	Some(ControlFlow::Continue(1))
1655		/// );
1656		/// ```
1657		fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1658			func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1659			ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1660		) -> 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>)>)
1661		where
1662			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1663			match ta {
1664				ControlFlow::Break(a) => F::map(|b| ControlFlow::Break(b), func(a)),
1665				ControlFlow::Continue(e) => F::pure(ControlFlow::Continue(e)),
1666			}
1667		}
1668
1669		/// Sequences a control flow of applicative.
1670		///
1671		/// This method evaluates the computation inside the control flow and accumulates the result into an applicative context.
1672		#[document_signature]
1673		///
1674		#[document_type_parameters(
1675			"The lifetime of the values.",
1676			"The type of the elements in the traversable structure.",
1677			"The applicative context."
1678		)]
1679		///
1680		#[document_parameters("The control flow containing the applicative value.")]
1681		///
1682		#[document_returns("The control flow wrapped in the applicative context.")]
1683		///
1684		#[document_examples]
1685		///
1686		/// ```
1687		/// use {
1688		/// 	core::ops::ControlFlow,
1689		/// 	fp_library::{
1690		/// 		brands::*,
1691		/// 		functions::*,
1692		/// 	},
1693		/// };
1694		///
1695		/// assert_eq!(
1696		/// 	sequence::<ControlFlowContinueAppliedBrand<()>, _, OptionBrand>(ControlFlow::Break(Some(
1697		/// 		5
1698		/// 	))),
1699		/// 	Some(ControlFlow::Break(5))
1700		/// );
1701		/// assert_eq!(
1702		/// 	sequence::<ControlFlowContinueAppliedBrand<i32>, i32, OptionBrand>(ControlFlow::<
1703		/// 		Option<i32>,
1704		/// 		i32,
1705		/// 	>::Continue(1)),
1706		/// 	Some(ControlFlow::<i32, i32>::Continue(1))
1707		/// );
1708		/// ```
1709		fn sequence<'a, A: 'a + Clone, F: Applicative>(
1710			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>)>)
1711		) -> 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>)>)
1712		where
1713			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1714			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
1715			match ta {
1716				ControlFlow::Break(fa) => F::map(|a| ControlFlow::Break(a), fa),
1717				ControlFlow::Continue(e) => F::pure(ControlFlow::Continue(e)),
1718			}
1719		}
1720	}
1721
1722	// ControlFlowBreakAppliedBrand<BreakType> (Functor over C, the Continue type)
1723
1724	impl_kind! {
1725		#[multi_brand]
1726		impl<BreakType: 'static> for ControlFlowBreakAppliedBrand<BreakType> {
1727			type Of<'a, C: 'a>: 'a = ControlFlow<BreakType, C>;
1728		}
1729	}
1730
1731	#[document_type_parameters("The break type.")]
1732	impl<BreakType: 'static> Functor for ControlFlowBreakAppliedBrand<BreakType> {
1733		/// Maps a function over the continue value in the control flow.
1734		///
1735		/// This method applies a function to the continue value inside the control flow, producing a new control flow with the transformed continue value. The break value remains unchanged.
1736		#[document_signature]
1737		///
1738		#[document_type_parameters(
1739			"The lifetime of the values.",
1740			"The type of the continue value.",
1741			"The type of the result of applying the function."
1742		)]
1743		///
1744		#[document_parameters(
1745			"The function to apply to the continue value.",
1746			"The control flow to map over."
1747		)]
1748		///
1749		#[document_returns(
1750			"A new control flow containing the result of applying the function to the continue value."
1751		)]
1752		///
1753		#[document_examples]
1754		///
1755		/// ```
1756		/// use {
1757		/// 	core::ops::ControlFlow,
1758		/// 	fp_library::{
1759		/// 		brands::*,
1760		/// 		functions::*,
1761		/// 	},
1762		/// };
1763		///
1764		/// assert_eq!(
1765		/// 	explicit::map::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
1766		/// 		|x: i32| x * 2,
1767		/// 		ControlFlow::<i32, i32>::Continue(5)
1768		/// 	),
1769		/// 	ControlFlow::Continue(10)
1770		/// );
1771		/// ```
1772		fn map<'a, A: 'a, B: 'a>(
1773			func: impl Fn(A) -> B + 'a,
1774			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1775		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1776			ControlFlowBrand::map_continue(fa, func)
1777		}
1778	}
1779
1780	#[document_type_parameters("The break type.")]
1781	impl<BreakType: Clone + 'static> Lift for ControlFlowBreakAppliedBrand<BreakType> {
1782		/// Lifts a binary function into the control flow context (over continue).
1783		///
1784		/// This method lifts a binary function to operate on continue values within the control flow context.
1785		#[document_signature]
1786		///
1787		#[document_type_parameters(
1788			"The lifetime of the values.",
1789			"The type of the first continue value.",
1790			"The type of the second continue value.",
1791			"The type of the result continue value."
1792		)]
1793		///
1794		#[document_parameters(
1795			"The binary function to apply to the continues.",
1796			"The first control flow.",
1797			"The second control flow."
1798		)]
1799		///
1800		#[document_returns(
1801			"`Continue(f(a, b))` if both are `Continue`, otherwise the first break encountered."
1802		)]
1803		#[document_examples]
1804		///
1805		/// ```
1806		/// use {
1807		/// 	core::ops::ControlFlow,
1808		/// 	fp_library::{
1809		/// 		brands::*,
1810		/// 		functions::*,
1811		/// 	},
1812		/// };
1813		///
1814		/// assert_eq!(
1815		/// 	explicit::lift2::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _, _, _>(
1816		/// 		|x: i32, y: i32| x + y,
1817		/// 		ControlFlow::Continue(1),
1818		/// 		ControlFlow::Continue(2)
1819		/// 	),
1820		/// 	ControlFlow::Continue(3)
1821		/// );
1822		/// assert_eq!(
1823		/// 	explicit::lift2::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _, _, _>(
1824		/// 		|x: i32, y: i32| x + y,
1825		/// 		ControlFlow::Continue(1),
1826		/// 		ControlFlow::Break(2)
1827		/// 	),
1828		/// 	ControlFlow::Break(2)
1829		/// );
1830		/// ```
1831		fn lift2<'a, A, B, C>(
1832			func: impl Fn(A, B) -> C + 'a,
1833			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1834			fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1835		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
1836		where
1837			A: Clone + 'a,
1838			B: Clone + 'a,
1839			C: 'a, {
1840			match (fa, fb) {
1841				(ControlFlow::Continue(a), ControlFlow::Continue(b)) =>
1842					ControlFlow::Continue(func(a, b)),
1843				(ControlFlow::Break(t), _) => ControlFlow::Break(t),
1844				(_, ControlFlow::Break(t)) => ControlFlow::Break(t),
1845			}
1846		}
1847	}
1848
1849	#[document_type_parameters("The break type.")]
1850	impl<BreakType: 'static> Pointed for ControlFlowBreakAppliedBrand<BreakType> {
1851		/// Wraps a value in a control flow (as continue).
1852		///
1853		/// This method wraps a value in the `Continue` variant of a `ControlFlow`.
1854		#[document_signature]
1855		///
1856		#[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
1857		///
1858		#[document_parameters("The value to wrap.")]
1859		///
1860		#[document_returns("`Continue(a)`.")]
1861		///
1862		#[document_examples]
1863		///
1864		/// ```
1865		/// use {
1866		/// 	core::ops::ControlFlow,
1867		/// 	fp_library::{
1868		/// 		brands::*,
1869		/// 		functions::*,
1870		/// 	},
1871		/// };
1872		///
1873		/// assert_eq!(pure::<ControlFlowBreakAppliedBrand<()>, _>(5), ControlFlow::<(), _>::Continue(5));
1874		/// ```
1875		fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1876			ControlFlow::Continue(a)
1877		}
1878	}
1879
1880	#[document_type_parameters("The break type.")]
1881	impl<BreakType: Clone + 'static> ApplyFirst for ControlFlowBreakAppliedBrand<BreakType> {}
1882
1883	#[document_type_parameters("The break type.")]
1884	impl<BreakType: Clone + 'static> ApplySecond for ControlFlowBreakAppliedBrand<BreakType> {}
1885
1886	#[document_type_parameters("The break type.")]
1887	impl<BreakType: Clone + 'static> Semiapplicative for ControlFlowBreakAppliedBrand<BreakType> {
1888		/// Applies a wrapped function to a wrapped value (over continue).
1889		///
1890		/// This method applies a function wrapped in a control flow (as continue) to a value wrapped in a control flow (as continue).
1891		#[document_signature]
1892		///
1893		#[document_type_parameters(
1894			"The lifetime of the values.",
1895			"The brand of the cloneable function wrapper.",
1896			"The type of the input value.",
1897			"The type of the output value."
1898		)]
1899		///
1900		#[document_parameters(
1901			"The control flow containing the function (in Continue).",
1902			"The control flow containing the value (in Continue)."
1903		)]
1904		///
1905		#[document_returns(
1906			"`Continue(f(a))` if both are `Continue`, otherwise the first break encountered."
1907		)]
1908		#[document_examples]
1909		///
1910		/// ```
1911		/// use {
1912		/// 	core::ops::ControlFlow,
1913		/// 	fp_library::{
1914		/// 		brands::*,
1915		/// 		classes::semiapplicative::apply as explicit_apply,
1916		/// 		functions::*,
1917		/// 	},
1918		/// };
1919		///
1920		/// let f: ControlFlow<(), _> =
1921		/// 	ControlFlow::Continue(lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
1922		/// assert_eq!(
1923		/// 	explicit_apply::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _>(
1924		/// 		f,
1925		/// 		ControlFlow::Continue(5)
1926		/// 	),
1927		/// 	ControlFlow::Continue(10)
1928		/// );
1929		/// ```
1930		fn apply<'a, FnBrand: 'a + CloneFn, A: 'a + Clone, B: 'a>(
1931			ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, A, B>>),
1932			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1933		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1934			match (ff, fa) {
1935				(ControlFlow::Continue(f), ControlFlow::Continue(a)) => ControlFlow::Continue(f(a)),
1936				(ControlFlow::Break(t), _) => ControlFlow::Break(t),
1937				(_, ControlFlow::Break(t)) => ControlFlow::Break(t),
1938			}
1939		}
1940	}
1941
1942	#[document_type_parameters("The break type.")]
1943	impl<BreakType: Clone + 'static> Semimonad for ControlFlowBreakAppliedBrand<BreakType> {
1944		/// Chains control flow computations (over continue).
1945		///
1946		/// This method chains two computations, where the second computation depends on the result of the first (over continue).
1947		#[document_signature]
1948		///
1949		#[document_type_parameters(
1950			"The lifetime of the values.",
1951			"The type of the result of the first computation.",
1952			"The type of the result of the second computation."
1953		)]
1954		///
1955		#[document_parameters(
1956			"The first control flow.",
1957			"The function to apply to the continue value."
1958		)]
1959		///
1960		#[document_returns(
1961			"The result of applying `f` to the continue if `ma` is `Continue`, otherwise the original break."
1962		)]
1963		///
1964		#[document_examples]
1965		///
1966		/// ```
1967		/// use {
1968		/// 	core::ops::ControlFlow,
1969		/// 	fp_library::{
1970		/// 		brands::*,
1971		/// 		functions::*,
1972		/// 	},
1973		/// };
1974		///
1975		/// assert_eq!(
1976		/// 	explicit::bind::<ControlFlowBreakAppliedBrand<()>, _, _, _, _>(
1977		/// 		ControlFlow::Continue(5),
1978		/// 		|x| { ControlFlow::Continue(x * 2) }
1979		/// 	),
1980		/// 	ControlFlow::<(), _>::Continue(10)
1981		/// );
1982		/// ```
1983		fn bind<'a, A: 'a, B: 'a>(
1984			ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1985			func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1986		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1987			ControlFlowBrand::bind_continue(ma, func)
1988		}
1989	}
1990
1991	#[document_type_parameters("The break type.")]
1992	impl<BreakType: 'static> Foldable for ControlFlowBreakAppliedBrand<BreakType> {
1993		/// Folds the control flow from the right (over continue).
1994		///
1995		/// This method performs a right-associative fold of the control flow (over continue).
1996		#[document_signature]
1997		///
1998		#[document_type_parameters(
1999			"The lifetime of the values.",
2000			"The brand of the cloneable function to use.",
2001			"The type of the elements in the structure.",
2002			"The type of the accumulator."
2003		)]
2004		///
2005		#[document_parameters(
2006			"The folding function.",
2007			"The initial value.",
2008			"The control flow to fold."
2009		)]
2010		///
2011		#[document_returns("`func(a, initial)` if `fa` is `Continue(a)`, otherwise `initial`.")]
2012		///
2013		#[document_examples]
2014		///
2015		/// ```
2016		/// use {
2017		/// 	core::ops::ControlFlow,
2018		/// 	fp_library::{
2019		/// 		brands::*,
2020		/// 		functions::*,
2021		/// 	},
2022		/// };
2023		///
2024		/// assert_eq!(
2025		/// 	explicit::fold_right::<RcFnBrand, ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
2026		/// 		|x: i32, acc| x + acc,
2027		/// 		0,
2028		/// 		ControlFlow::Continue(1)
2029		/// 	),
2030		/// 	1
2031		/// );
2032		/// assert_eq!(
2033		/// 	explicit::fold_right::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _, _, _>(
2034		/// 		|x: i32, acc| x + acc,
2035		/// 		0,
2036		/// 		ControlFlow::Break(())
2037		/// 	),
2038		/// 	0
2039		/// );
2040		/// ```
2041		fn fold_right<'a, FnBrand, A: 'a, B: 'a>(
2042			func: impl Fn(A, B) -> B + 'a,
2043			initial: B,
2044			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2045		) -> B
2046		where
2047			FnBrand: CloneFn + 'a, {
2048			match fa {
2049				ControlFlow::Continue(e) => func(e, initial),
2050				ControlFlow::Break(_) => initial,
2051			}
2052		}
2053
2054		/// Folds the control flow from the left (over continue).
2055		///
2056		/// This method performs a left-associative fold of the control flow (over continue).
2057		#[document_signature]
2058		///
2059		#[document_type_parameters(
2060			"The lifetime of the values.",
2061			"The brand of the cloneable function to use.",
2062			"The type of the elements in the structure.",
2063			"The type of the accumulator."
2064		)]
2065		///
2066		#[document_parameters(
2067			"The folding function.",
2068			"The initial value.",
2069			"The control flow to fold."
2070		)]
2071		///
2072		#[document_returns("`func(initial, a)` if `fa` is `Continue(a)`, otherwise `initial`.")]
2073		///
2074		#[document_examples]
2075		///
2076		/// ```
2077		/// use {
2078		/// 	core::ops::ControlFlow,
2079		/// 	fp_library::{
2080		/// 		brands::*,
2081		/// 		functions::*,
2082		/// 	},
2083		/// };
2084		///
2085		/// assert_eq!(
2086		/// 	explicit::fold_left::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _, _, _>(
2087		/// 		|acc, x: i32| acc + x,
2088		/// 		0,
2089		/// 		ControlFlow::Continue(5)
2090		/// 	),
2091		/// 	5
2092		/// );
2093		/// assert_eq!(
2094		/// 	explicit::fold_left::<RcFnBrand, ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
2095		/// 		|acc, x: i32| acc + x,
2096		/// 		0,
2097		/// 		ControlFlow::Break(1)
2098		/// 	),
2099		/// 	0
2100		/// );
2101		/// ```
2102		fn fold_left<'a, FnBrand, A: 'a, B: 'a>(
2103			func: impl Fn(B, A) -> B + 'a,
2104			initial: B,
2105			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2106		) -> B
2107		where
2108			FnBrand: CloneFn + 'a, {
2109			match fa {
2110				ControlFlow::Continue(e) => func(initial, e),
2111				ControlFlow::Break(_) => initial,
2112			}
2113		}
2114
2115		/// Maps the value to a monoid and returns it (over continue).
2116		///
2117		/// This method maps the element of the control flow to a monoid and then returns it (over continue).
2118		#[document_signature]
2119		///
2120		#[document_type_parameters(
2121			"The lifetime of the values.",
2122			"The brand of the cloneable function to use.",
2123			"The type of the elements in the structure.",
2124			"The type of the monoid."
2125		)]
2126		///
2127		#[document_parameters("The mapping function.", "The control flow to fold.")]
2128		///
2129		#[document_returns("`func(a)` if `fa` is `Continue(a)`, otherwise `M::empty()`.")]
2130		///
2131		#[document_examples]
2132		///
2133		/// ```
2134		/// use {
2135		/// 	core::ops::ControlFlow,
2136		/// 	fp_library::{
2137		/// 		brands::*,
2138		/// 		functions::*,
2139		/// 	},
2140		/// };
2141		///
2142		/// assert_eq!(
2143		/// 	explicit::fold_map::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _, _, _>(
2144		/// 		|x: i32| x.to_string(),
2145		/// 		ControlFlow::Continue(5)
2146		/// 	),
2147		/// 	"5".to_string()
2148		/// );
2149		/// assert_eq!(
2150		/// 	explicit::fold_map::<RcFnBrand, ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
2151		/// 		|x: i32| x.to_string(),
2152		/// 		ControlFlow::Break(1)
2153		/// 	),
2154		/// 	"".to_string()
2155		/// );
2156		/// ```
2157		fn fold_map<'a, FnBrand, A: 'a, M>(
2158			func: impl Fn(A) -> M + 'a,
2159			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2160		) -> M
2161		where
2162			M: Monoid + 'a,
2163			FnBrand: CloneFn + 'a, {
2164			match fa {
2165				ControlFlow::Continue(e) => func(e),
2166				ControlFlow::Break(_) => M::empty(),
2167			}
2168		}
2169	}
2170
2171	#[document_type_parameters("The break type.")]
2172	impl<BreakType: Clone + 'static> Traversable for ControlFlowBreakAppliedBrand<BreakType> {
2173		/// Traverses the control flow with an applicative function (over continue).
2174		///
2175		/// This method maps the element of the control flow to a computation, evaluates it, and combines the result into an applicative context (over continue).
2176		#[document_signature]
2177		///
2178		#[document_type_parameters(
2179			"The lifetime of the values.",
2180			"The type of the elements in the traversable structure.",
2181			"The type of the elements in the resulting traversable structure.",
2182			"The applicative context."
2183		)]
2184		///
2185		#[document_parameters("The function to apply.", "The control flow to traverse.")]
2186		///
2187		#[document_returns("The control flow wrapped in the applicative context.")]
2188		///
2189		#[document_examples]
2190		///
2191		/// ```
2192		/// use {
2193		/// 	core::ops::ControlFlow,
2194		/// 	fp_library::{
2195		/// 		brands::*,
2196		/// 		functions::*,
2197		/// 	},
2198		/// };
2199		///
2200		/// assert_eq!(
2201		/// 	explicit::traverse::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _, OptionBrand, _, _>(
2202		/// 		|x| Some(x * 2),
2203		/// 		ControlFlow::Continue(5)
2204		/// 	),
2205		/// 	Some(ControlFlow::Continue(10))
2206		/// );
2207		/// assert_eq!(
2208		/// 	explicit::traverse::<RcFnBrand, ControlFlowBreakAppliedBrand<i32>, _, _, OptionBrand, _, _>(
2209		/// 		|x: i32| Some(x * 2),
2210		/// 		ControlFlow::Break(1)
2211		/// 	),
2212		/// 	Some(ControlFlow::Break(1))
2213		/// );
2214		/// ```
2215		fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
2216			func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
2217			ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2218		) -> 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>)>)
2219		where
2220			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
2221			match ta {
2222				ControlFlow::Continue(e) => F::map(|b| ControlFlow::Continue(b), func(e)),
2223				ControlFlow::Break(t) => F::pure(ControlFlow::Break(t)),
2224			}
2225		}
2226
2227		/// Sequences a control flow of applicative (over continue).
2228		///
2229		/// This method evaluates the computation inside the control flow and accumulates the result into an applicative context (over continue).
2230		#[document_signature]
2231		///
2232		#[document_type_parameters(
2233			"The lifetime of the values.",
2234			"The type of the elements in the traversable structure.",
2235			"The applicative context."
2236		)]
2237		///
2238		#[document_parameters("The control flow containing the applicative value.")]
2239		///
2240		#[document_returns("The control flow wrapped in the applicative context.")]
2241		///
2242		#[document_examples]
2243		///
2244		/// ```
2245		/// use {
2246		/// 	core::ops::ControlFlow,
2247		/// 	fp_library::{
2248		/// 		brands::*,
2249		/// 		functions::*,
2250		/// 	},
2251		/// };
2252		///
2253		/// assert_eq!(
2254		/// 	sequence::<ControlFlowBreakAppliedBrand<()>, _, OptionBrand>(ControlFlow::Continue(Some(
2255		/// 		5
2256		/// 	))),
2257		/// 	Some(ControlFlow::Continue(5))
2258		/// );
2259		/// assert_eq!(
2260		/// 	sequence::<ControlFlowBreakAppliedBrand<i32>, i32, OptionBrand>(ControlFlow::<
2261		/// 		i32,
2262		/// 		Option<i32>,
2263		/// 	>::Break(1)),
2264		/// 	Some(ControlFlow::<i32, i32>::Break(1))
2265		/// );
2266		/// ```
2267		fn sequence<'a, A: 'a + Clone, F: Applicative>(
2268			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>)>)
2269		) -> 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>)>)
2270		where
2271			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
2272			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
2273			match ta {
2274				ControlFlow::Continue(fe) => F::map(|e| ControlFlow::Continue(e), fe),
2275				ControlFlow::Break(t) => F::pure(ControlFlow::Break(t)),
2276			}
2277		}
2278	}
2279
2280	/// [`MonadRec`] implementation for [`ControlFlowContinueAppliedBrand`].
2281	#[document_type_parameters("The continue type.")]
2282	impl<ContinueType: Clone + 'static> MonadRec for ControlFlowContinueAppliedBrand<ContinueType> {
2283		/// Performs tail-recursive monadic computation over [`ControlFlow`] (break channel).
2284		///
2285		/// Iteratively applies the step function. If the function returns [`ControlFlow::Continue`],
2286		/// the computation short-circuits with that continue value. If it returns
2287		/// `Break(ControlFlow::Continue(a))`, the loop continues with the new state. If it returns
2288		/// `Break(ControlFlow::Break(b))`, the computation completes with `Break(b)`.
2289		#[document_signature]
2290		///
2291		#[document_type_parameters(
2292			"The lifetime of the computation.",
2293			"The type of the initial value and loop state.",
2294			"The type of the result."
2295		)]
2296		///
2297		#[document_parameters("The step function.", "The initial value.")]
2298		///
2299		#[document_returns(
2300			"The result of the computation, or a continue if the step function returned `Continue`."
2301		)]
2302		///
2303		#[document_examples]
2304		///
2305		/// ```
2306		/// use {
2307		/// 	core::ops::ControlFlow,
2308		/// 	fp_library::{
2309		/// 		brands::*,
2310		/// 		functions::*,
2311		/// 	},
2312		/// };
2313		///
2314		/// let result = tail_rec_m::<ControlFlowContinueAppliedBrand<&str>, _, _>(
2315		/// 	|n| {
2316		/// 		if n < 10 {
2317		/// 			ControlFlow::Break(ControlFlow::Continue(n + 1))
2318		/// 		} else {
2319		/// 			ControlFlow::Break(ControlFlow::Break(n))
2320		/// 		}
2321		/// 	},
2322		/// 	0,
2323		/// );
2324		/// assert_eq!(result, ControlFlow::Break(10));
2325		/// ```
2326		fn tail_rec_m<'a, A: 'a, B: 'a>(
2327			func: impl Fn(
2328				A,
2329			)
2330				-> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
2331			+ 'a,
2332			initial: A,
2333		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2334			let mut current = initial;
2335			loop {
2336				match func(current) {
2337					ControlFlow::Continue(l) => return ControlFlow::Continue(l),
2338					ControlFlow::Break(ControlFlow::Continue(next)) => current = next,
2339					ControlFlow::Break(ControlFlow::Break(b)) => return ControlFlow::Break(b),
2340				}
2341			}
2342		}
2343	}
2344
2345	/// [`MonadRec`] implementation for [`ControlFlowBreakAppliedBrand`].
2346	#[document_type_parameters("The break type.")]
2347	impl<BreakType: Clone + 'static> MonadRec for ControlFlowBreakAppliedBrand<BreakType> {
2348		/// Performs tail-recursive monadic computation over [`ControlFlow`] (continue channel).
2349		///
2350		/// Iteratively applies the step function. If the function returns [`ControlFlow::Break`],
2351		/// the computation short-circuits with that break value. If it returns
2352		/// `Continue(ControlFlow::Continue(a))`, the loop continues with the new state. If it returns
2353		/// `Continue(ControlFlow::Break(b))`, the computation completes with `Continue(b)`.
2354		#[document_signature]
2355		///
2356		#[document_type_parameters(
2357			"The lifetime of the computation.",
2358			"The type of the initial value and loop state.",
2359			"The type of the result."
2360		)]
2361		///
2362		#[document_parameters("The step function.", "The initial value.")]
2363		///
2364		#[document_returns(
2365			"The result of the computation, or a break if the step function returned `Break`."
2366		)]
2367		///
2368		#[document_examples]
2369		///
2370		/// ```
2371		/// use {
2372		/// 	core::ops::ControlFlow,
2373		/// 	fp_library::{
2374		/// 		brands::*,
2375		/// 		functions::*,
2376		/// 	},
2377		/// };
2378		///
2379		/// let result = tail_rec_m::<ControlFlowBreakAppliedBrand<&str>, _, _>(
2380		/// 	|n| {
2381		/// 		if n < 10 {
2382		/// 			ControlFlow::Continue(ControlFlow::Continue(n + 1))
2383		/// 		} else {
2384		/// 			ControlFlow::Continue(ControlFlow::Break(n))
2385		/// 		}
2386		/// 	},
2387		/// 	0,
2388		/// );
2389		/// assert_eq!(result, ControlFlow::Continue(10));
2390		/// ```
2391		fn tail_rec_m<'a, A: 'a, B: 'a>(
2392			func: impl Fn(
2393				A,
2394			)
2395				-> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
2396			+ 'a,
2397			initial: A,
2398		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2399			let mut current = initial;
2400			loop {
2401				match func(current) {
2402					ControlFlow::Break(d) => return ControlFlow::Break(d),
2403					ControlFlow::Continue(ControlFlow::Continue(next)) => current = next,
2404					ControlFlow::Continue(ControlFlow::Break(b)) =>
2405						return ControlFlow::Continue(b),
2406				}
2407			}
2408		}
2409	}
2410}
2411
2412#[cfg(test)]
2413mod tests {
2414	use {
2415		crate::{
2416			brands::*,
2417			classes::semiapplicative::apply as explicit_apply,
2418			functions::*,
2419		},
2420		core::ops::ControlFlow,
2421		quickcheck::{
2422			Arbitrary,
2423			Gen,
2424		},
2425		quickcheck_macros::quickcheck,
2426	};
2427
2428	impl<B: Arbitrary, C: Arbitrary> Arbitrary for ControlFlowWrapper<B, C> {
2429		fn arbitrary(g: &mut Gen) -> Self {
2430			if bool::arbitrary(g) {
2431				ControlFlowWrapper(ControlFlow::Continue(C::arbitrary(g)))
2432			} else {
2433				ControlFlowWrapper(ControlFlow::Break(B::arbitrary(g)))
2434			}
2435		}
2436	}
2437
2438	/// Newtype wrapper for `ControlFlow` to implement `Arbitrary`.
2439	#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2440	struct ControlFlowWrapper<B, C>(ControlFlow<B, C>);
2441
2442	impl<B, C> ControlFlowWrapper<B, C> {
2443		fn into_inner(self) -> ControlFlow<B, C> {
2444			self.0
2445		}
2446	}
2447
2448	/// Tests the `is_continue` method.
2449	///
2450	/// Verifies that `is_continue` returns true for `Continue` variants and false for `Break` variants.
2451	#[test]
2452	fn test_is_continue() {
2453		let cf: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2454		assert!(ControlFlowBrand::is_continue(&cf));
2455		assert!(!ControlFlowBrand::is_break(&cf));
2456	}
2457
2458	/// Tests the `is_break` method.
2459	///
2460	/// Verifies that `is_break` returns true for `Break` variants and false for `Continue` variants.
2461	#[test]
2462	fn test_is_break() {
2463		let cf: ControlFlow<i32, i32> = ControlFlow::Break(1);
2464		assert!(ControlFlowBrand::is_break(&cf));
2465		assert!(!ControlFlowBrand::is_continue(&cf));
2466	}
2467
2468	/// Tests the `map_continue` method.
2469	///
2470	/// Verifies that `map_continue` transforms the value inside a `Continue` variant and leaves a `Break` variant unchanged.
2471	#[test]
2472	fn test_map_continue() {
2473		let cf: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2474		let mapped = ControlFlowBrand::map_continue(cf, |x| x + 1);
2475		assert_eq!(mapped, ControlFlow::Continue(2));
2476
2477		let brk: ControlFlow<i32, i32> = ControlFlow::Break(1);
2478		let mapped_brk = ControlFlowBrand::map_continue(brk, |x| x + 1);
2479		assert_eq!(mapped_brk, ControlFlow::Break(1));
2480	}
2481
2482	/// Tests the `map_break` method.
2483	///
2484	/// Verifies that `map_break` transforms the value inside a `Break` variant and leaves a `Continue` variant unchanged.
2485	#[test]
2486	fn test_map_break() {
2487		let cf: ControlFlow<i32, i32> = ControlFlow::Break(1);
2488		let mapped = ControlFlowBrand::map_break(cf, |x| x + 1);
2489		assert_eq!(mapped, ControlFlow::Break(2));
2490
2491		let cont: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2492		let mapped_cont = ControlFlowBrand::map_break(cont, |x| x + 1);
2493		assert_eq!(mapped_cont, ControlFlow::Continue(1));
2494	}
2495
2496	/// Tests the `bimap` method.
2497	///
2498	/// Verifies that `bimap` transforms the value inside both `Continue` and `Break` variants using the appropriate function.
2499	#[test]
2500	fn test_bimap() {
2501		let cf: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2502		let mapped = ControlFlowBrand::bimap(cf, |x| x + 1, |x| x * 2);
2503		assert_eq!(mapped, ControlFlow::Continue(2));
2504
2505		let brk: ControlFlow<i32, i32> = ControlFlow::Break(1);
2506		let mapped_brk = ControlFlowBrand::bimap(brk, |x| x + 1, |x| x * 2);
2507		assert_eq!(mapped_brk, ControlFlow::Break(2));
2508	}
2509
2510	/// Tests `Functor` implementation for `ControlFlowContinueAppliedBrand`.
2511	#[test]
2512	fn test_functor_with_continue() {
2513		let cf: ControlFlow<i32, i32> = ControlFlow::Break(5);
2514		assert_eq!(
2515			explicit::map::<ControlFlowContinueAppliedBrand<_>, _, _, _, _>(|x: i32| x * 2, cf),
2516			ControlFlow::Break(10)
2517		);
2518
2519		let cont: ControlFlow<i32, i32> = ControlFlow::Continue(5);
2520		assert_eq!(
2521			explicit::map::<ControlFlowContinueAppliedBrand<_>, _, _, _, _>(|x: i32| x * 2, cont),
2522			ControlFlow::Continue(5)
2523		);
2524	}
2525
2526	/// Tests `Functor` implementation for `ControlFlowBreakAppliedBrand`.
2527	#[test]
2528	fn test_functor_with_break() {
2529		let cf: ControlFlow<i32, i32> = ControlFlow::Continue(5);
2530		assert_eq!(
2531			explicit::map::<ControlFlowBreakAppliedBrand<_>, _, _, _, _>(|x: i32| x * 2, cf),
2532			ControlFlow::Continue(10)
2533		);
2534
2535		let brk: ControlFlow<i32, i32> = ControlFlow::Break(5);
2536		assert_eq!(
2537			explicit::map::<ControlFlowBreakAppliedBrand<_>, _, _, _, _>(|x: i32| x * 2, brk),
2538			ControlFlow::Break(5)
2539		);
2540	}
2541
2542	/// Tests `Bifunctor` implementation for `ControlFlowBrand`.
2543	#[test]
2544	fn test_bifunctor() {
2545		let cf: ControlFlow<i32, i32> = ControlFlow::Continue(5);
2546		assert_eq!(
2547			explicit::bimap::<ControlFlowBrand, _, _, _, _, _, _>((|c| c + 1, |b| b * 2), cf),
2548			ControlFlow::Continue(6)
2549		);
2550
2551		let brk: ControlFlow<i32, i32> = ControlFlow::Break(5);
2552		assert_eq!(
2553			explicit::bimap::<ControlFlowBrand, _, _, _, _, _, _>((|c| c + 1, |b| b * 2), brk),
2554			ControlFlow::Break(10)
2555		);
2556	}
2557
2558	// Functor Laws for ControlFlowContinueAppliedBrand
2559
2560	#[quickcheck]
2561	fn functor_identity_with_continue(x: ControlFlowWrapper<i32, i32>) -> bool {
2562		let x = x.into_inner();
2563		explicit::map::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(identity, x) == x
2564	}
2565
2566	#[quickcheck]
2567	fn functor_composition_with_continue(x: ControlFlowWrapper<i32, i32>) -> bool {
2568		let x = x.into_inner();
2569		let f = |x: i32| x.wrapping_add(1);
2570		let g = |x: i32| x.wrapping_mul(2);
2571		explicit::map::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(compose(f, g), x)
2572			== explicit::map::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(
2573				f,
2574				explicit::map::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(g, x),
2575			)
2576	}
2577
2578	// Functor Laws for ControlFlowBreakAppliedBrand
2579
2580	#[quickcheck]
2581	fn functor_identity_with_break(x: ControlFlowWrapper<i32, i32>) -> bool {
2582		let x = x.into_inner();
2583		explicit::map::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(identity, x) == x
2584	}
2585
2586	#[quickcheck]
2587	fn functor_composition_with_break(x: ControlFlowWrapper<i32, i32>) -> bool {
2588		let x = x.into_inner();
2589		let f = |x: i32| x.wrapping_add(1);
2590		let g = |x: i32| x.wrapping_mul(2);
2591		explicit::map::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(compose(f, g), x)
2592			== explicit::map::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
2593				f,
2594				explicit::map::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(g, x),
2595			)
2596	}
2597
2598	// Bifunctor Laws for ControlFlowBrand
2599
2600	#[quickcheck]
2601	fn bifunctor_identity(x: ControlFlowWrapper<i32, i32>) -> bool {
2602		let x = x.into_inner();
2603		explicit::bimap::<ControlFlowBrand, _, _, _, _, _, _>((identity, identity), x) == x
2604	}
2605
2606	#[quickcheck]
2607	fn bifunctor_composition(x: ControlFlowWrapper<i32, i32>) -> bool {
2608		let x = x.into_inner();
2609		let f = |x: i32| x.wrapping_add(1);
2610		let g = |x: i32| x.wrapping_mul(2);
2611		let h = |x: i32| x.wrapping_sub(1);
2612		let i = |x: i32| if x == 0 { 0 } else { x.wrapping_div(2) };
2613
2614		explicit::bimap::<ControlFlowBrand, _, _, _, _, _, _>((compose(f, g), compose(h, i)), x)
2615			== explicit::bimap::<ControlFlowBrand, _, _, _, _, _, _>(
2616				(f, h),
2617				explicit::bimap::<ControlFlowBrand, _, _, _, _, _, _>((g, i), x),
2618			)
2619	}
2620
2621	// Lift Tests
2622
2623	/// Tests the `lift2` function for `ControlFlowContinueAppliedBrand`.
2624	///
2625	/// Verifies that `lift2` correctly combines two `ControlFlow` values using a binary function,
2626	/// handling `Break` and `Continue` variants according to the `Lift` implementation.
2627	#[test]
2628	fn test_lift2_with_continue() {
2629		let s1: ControlFlow<i32, i32> = ControlFlow::Break(1);
2630		let s2: ControlFlow<i32, i32> = ControlFlow::Break(2);
2631		let s3: ControlFlow<i32, i32> = ControlFlow::Continue(3);
2632
2633		assert_eq!(
2634			explicit::lift2::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _, _, _>(
2635				|x, y| x + y,
2636				s1,
2637				s2
2638			),
2639			ControlFlow::Break(3)
2640		);
2641		assert_eq!(
2642			explicit::lift2::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _, _, _>(
2643				|x, y| x + y,
2644				s1,
2645				s3
2646			),
2647			ControlFlow::Continue(3)
2648		);
2649	}
2650
2651	/// Tests the `lift2` function for `ControlFlowBreakAppliedBrand`.
2652	///
2653	/// Verifies that `lift2` correctly combines two `ControlFlow` values using a binary function,
2654	/// handling `Break` and `Continue` variants according to the `Lift` implementation.
2655	#[test]
2656	fn test_lift2_with_break() {
2657		let s1: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2658		let s2: ControlFlow<i32, i32> = ControlFlow::Continue(2);
2659		let s3: ControlFlow<i32, i32> = ControlFlow::Break(3);
2660
2661		assert_eq!(
2662			explicit::lift2::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _, _, _>(
2663				|x, y| x + y,
2664				s1,
2665				s2
2666			),
2667			ControlFlow::Continue(3)
2668		);
2669		assert_eq!(
2670			explicit::lift2::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _, _, _>(
2671				|x, y| x + y,
2672				s1,
2673				s3
2674			),
2675			ControlFlow::Break(3)
2676		);
2677	}
2678
2679	// Pointed Tests
2680
2681	/// Tests the `pure` function for `ControlFlowContinueAppliedBrand`.
2682	///
2683	/// Verifies that `pure` wraps a value into a `ControlFlow::Break` variant.
2684	#[test]
2685	fn test_pointed_with_continue() {
2686		assert_eq!(
2687			pure::<ControlFlowContinueAppliedBrand<()>, _>(5),
2688			ControlFlow::<_, ()>::Break(5)
2689		);
2690	}
2691
2692	/// Tests the `pure` function for `ControlFlowBreakAppliedBrand`.
2693	///
2694	/// Verifies that `pure` wraps a value into a `ControlFlow::Continue` variant.
2695	#[test]
2696	fn test_pointed_with_break() {
2697		assert_eq!(
2698			pure::<ControlFlowBreakAppliedBrand<()>, _>(5),
2699			ControlFlow::<(), _>::Continue(5)
2700		);
2701	}
2702
2703	// Semiapplicative Tests
2704
2705	/// Tests the `apply` function for `ControlFlowContinueAppliedBrand`.
2706	///
2707	/// Verifies that `apply` correctly applies a wrapped function to a wrapped value,
2708	/// handling `Break` and `Continue` variants.
2709	#[test]
2710	fn test_apply_with_continue() {
2711		let f = pure::<ControlFlowContinueAppliedBrand<()>, _>(lift_fn_new::<RcFnBrand, _, _>(
2712			|x: i32| x * 2,
2713		));
2714		let x = pure::<ControlFlowContinueAppliedBrand<()>, _>(5);
2715		assert_eq!(
2716			explicit_apply::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _>(f, x),
2717			ControlFlow::Break(10)
2718		);
2719
2720		let cont: ControlFlow<_, i32> = ControlFlow::Continue(1);
2721		let f_cont = pure::<ControlFlowContinueAppliedBrand<i32>, _>(
2722			lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2),
2723		);
2724		assert_eq!(
2725			explicit_apply::<RcFnBrand, ControlFlowContinueAppliedBrand<i32>, _, _>(f_cont, cont),
2726			ControlFlow::Continue(1)
2727		);
2728	}
2729
2730	/// Tests the `apply` function for `ControlFlowBreakAppliedBrand`.
2731	///
2732	/// Verifies that `apply` correctly applies a wrapped function to a wrapped value,
2733	/// handling `Break` and `Continue` variants.
2734	#[test]
2735	fn test_apply_with_break() {
2736		let f = pure::<ControlFlowBreakAppliedBrand<()>, _>(lift_fn_new::<RcFnBrand, _, _>(
2737			|x: i32| x * 2,
2738		));
2739		let x = pure::<ControlFlowBreakAppliedBrand<()>, _>(5);
2740		assert_eq!(
2741			explicit_apply::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _>(f, x),
2742			ControlFlow::Continue(10)
2743		);
2744
2745		let brk: ControlFlow<i32, _> = ControlFlow::Break(1);
2746		let f_brk = pure::<ControlFlowBreakAppliedBrand<i32>, _>(lift_fn_new::<RcFnBrand, _, _>(
2747			|x: i32| x * 2,
2748		));
2749		assert_eq!(
2750			explicit_apply::<RcFnBrand, ControlFlowBreakAppliedBrand<i32>, _, _>(f_brk, brk),
2751			ControlFlow::Break(1)
2752		);
2753	}
2754
2755	// Semimonad Tests
2756
2757	/// Tests the `bind` function for `ControlFlowContinueAppliedBrand`.
2758	///
2759	/// Verifies that `bind` correctly chains computations, handling `Break` and `Continue` variants.
2760	#[test]
2761	fn test_bind_with_continue() {
2762		let x = pure::<ControlFlowContinueAppliedBrand<()>, _>(5);
2763		assert_eq!(
2764			explicit::bind::<ControlFlowContinueAppliedBrand<()>, _, _, _, _>(x, |i| pure::<
2765				ControlFlowContinueAppliedBrand<()>,
2766				_,
2767			>(i * 2)),
2768			ControlFlow::Break(10)
2769		);
2770
2771		let cont: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2772		assert_eq!(
2773			explicit::bind::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(cont, |i| pure::<
2774				ControlFlowContinueAppliedBrand<i32>,
2775				_,
2776			>(
2777				i * 2
2778			)),
2779			ControlFlow::Continue(1)
2780		);
2781	}
2782
2783	/// Tests the `bind` function for `ControlFlowBreakAppliedBrand`.
2784	///
2785	/// Verifies that `bind` correctly chains computations, handling `Break` and `Continue` variants.
2786	#[test]
2787	fn test_bind_with_break() {
2788		let x = pure::<ControlFlowBreakAppliedBrand<()>, _>(5);
2789		assert_eq!(
2790			explicit::bind::<ControlFlowBreakAppliedBrand<()>, _, _, _, _>(x, |i| pure::<
2791				ControlFlowBreakAppliedBrand<()>,
2792				_,
2793			>(i * 2)),
2794			ControlFlow::Continue(10)
2795		);
2796
2797		let brk: ControlFlow<i32, i32> = ControlFlow::Break(1);
2798		assert_eq!(
2799			explicit::bind::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(brk, |i| pure::<
2800				ControlFlowBreakAppliedBrand<i32>,
2801				_,
2802			>(i * 2)),
2803			ControlFlow::Break(1)
2804		);
2805	}
2806
2807	// Foldable Tests
2808
2809	/// Tests `Foldable` methods for `ControlFlowContinueAppliedBrand`.
2810	///
2811	/// Verifies `fold_right`, `fold_left`, and `fold_map` behavior for `Break` and `Continue` variants.
2812	#[test]
2813	fn test_foldable_with_continue() {
2814		let x = pure::<ControlFlowContinueAppliedBrand<()>, _>(5);
2815		assert_eq!(
2816			explicit::fold_right::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _, _, _>(
2817				|a, b| a + b,
2818				10,
2819				x
2820			),
2821			15
2822		);
2823		assert_eq!(
2824			explicit::fold_left::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _, _, _>(
2825				|b, a| b + a,
2826				10,
2827				x
2828			),
2829			15
2830		);
2831		assert_eq!(
2832			explicit::fold_map::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _, _, _>(
2833				|a: i32| a.to_string(),
2834				x
2835			),
2836			"5"
2837		);
2838
2839		let cont: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2840		assert_eq!(
2841			explicit::fold_right::<RcFnBrand, ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(
2842				|a, b| a + b,
2843				10,
2844				cont
2845			),
2846			10
2847		);
2848	}
2849
2850	/// Tests `Foldable` methods for `ControlFlowBreakAppliedBrand`.
2851	///
2852	/// Verifies `fold_right`, `fold_left`, and `fold_map` behavior for `Break` and `Continue` variants.
2853	#[test]
2854	fn test_foldable_with_break() {
2855		let x = pure::<ControlFlowBreakAppliedBrand<()>, _>(5);
2856		assert_eq!(
2857			explicit::fold_right::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _, _, _>(
2858				|a, b| a + b,
2859				10,
2860				x
2861			),
2862			15
2863		);
2864		assert_eq!(
2865			explicit::fold_left::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _, _, _>(
2866				|b, a| b + a,
2867				10,
2868				x
2869			),
2870			15
2871		);
2872		assert_eq!(
2873			explicit::fold_map::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _, _, _>(
2874				|a: i32| a.to_string(),
2875				x
2876			),
2877			"5"
2878		);
2879
2880		let brk: ControlFlow<i32, i32> = ControlFlow::Break(1);
2881		assert_eq!(
2882			explicit::fold_right::<RcFnBrand, ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
2883				|a, b| a + b,
2884				10,
2885				brk
2886			),
2887			10
2888		);
2889	}
2890
2891	// Traversable Tests
2892
2893	/// Tests the `traverse` function for `ControlFlowContinueAppliedBrand`.
2894	///
2895	/// Verifies that `traverse` correctly maps and sequences effects over `ControlFlow`.
2896	#[test]
2897	fn test_traversable_with_continue() {
2898		let x = pure::<ControlFlowContinueAppliedBrand<()>, _>(5);
2899		assert_eq!(
2900			explicit::traverse::<
2901				RcFnBrand,
2902				ControlFlowContinueAppliedBrand<()>,
2903				_,
2904				_,
2905				OptionBrand,
2906				_,
2907				_,
2908			>(|a| Some(a * 2), x),
2909			Some(ControlFlow::Break(10))
2910		);
2911
2912		let cont: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2913		assert_eq!(
2914			explicit::traverse::<
2915				RcFnBrand,
2916				ControlFlowContinueAppliedBrand<i32>,
2917				_,
2918				_,
2919				OptionBrand,
2920				_,
2921				_,
2922			>(|a| Some(a * 2), cont),
2923			Some(ControlFlow::Continue(1))
2924		);
2925	}
2926
2927	/// Tests the `traverse` function for `ControlFlowBreakAppliedBrand`.
2928	///
2929	/// Verifies that `traverse` correctly maps and sequences effects over `ControlFlow`.
2930	#[test]
2931	fn test_traversable_with_break() {
2932		let x = pure::<ControlFlowBreakAppliedBrand<()>, _>(5);
2933		assert_eq!(
2934			explicit::traverse::<
2935				RcFnBrand,
2936				ControlFlowBreakAppliedBrand<()>,
2937				_,
2938				_,
2939				OptionBrand,
2940				_,
2941				_,
2942			>(|a| Some(a * 2), x),
2943			Some(ControlFlow::Continue(10))
2944		);
2945
2946		let brk: ControlFlow<i32, i32> = ControlFlow::Break(1);
2947		assert_eq!(
2948			explicit::traverse::<
2949				RcFnBrand,
2950				ControlFlowBreakAppliedBrand<i32>,
2951				_,
2952				_,
2953				OptionBrand,
2954				_,
2955				_,
2956			>(|a| Some(a * 2), brk),
2957			Some(ControlFlow::Break(1))
2958		);
2959	}
2960
2961	// Monad Laws for ControlFlowContinueAppliedBrand
2962
2963	/// Verifies the Left Identity law for `ControlFlowContinueAppliedBrand` Monad.
2964	#[quickcheck]
2965	fn monad_left_identity_with_continue(a: i32) -> bool {
2966		let f = |x: i32| pure::<ControlFlowContinueAppliedBrand<i32>, _>(x.wrapping_mul(2));
2967		explicit::bind::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(
2968			pure::<ControlFlowContinueAppliedBrand<i32>, _>(a),
2969			f,
2970		) == f(a)
2971	}
2972
2973	/// Verifies the Right Identity law for `ControlFlowContinueAppliedBrand` Monad.
2974	#[quickcheck]
2975	fn monad_right_identity_with_continue(x: ControlFlowWrapper<i32, i32>) -> bool {
2976		let x = x.into_inner();
2977		explicit::bind::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(
2978			x,
2979			pure::<ControlFlowContinueAppliedBrand<i32>, _>,
2980		) == x
2981	}
2982
2983	/// Verifies the Associativity law for `ControlFlowContinueAppliedBrand` Monad.
2984	#[quickcheck]
2985	fn monad_associativity_with_continue(x: ControlFlowWrapper<i32, i32>) -> bool {
2986		let x = x.into_inner();
2987		let f = |x: i32| pure::<ControlFlowContinueAppliedBrand<i32>, _>(x.wrapping_mul(2));
2988		let g = |x: i32| pure::<ControlFlowContinueAppliedBrand<i32>, _>(x.wrapping_add(1));
2989		explicit::bind::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(
2990			explicit::bind::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(x, f),
2991			g,
2992		) == explicit::bind::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(x, |a| {
2993			explicit::bind::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(f(a), g)
2994		})
2995	}
2996
2997	// Monad Laws for ControlFlowBreakAppliedBrand
2998
2999	/// Verifies the Left Identity law for `ControlFlowBreakAppliedBrand` Monad.
3000	#[quickcheck]
3001	fn monad_left_identity_with_break(a: i32) -> bool {
3002		let f = |x: i32| pure::<ControlFlowBreakAppliedBrand<i32>, _>(x.wrapping_mul(2));
3003		explicit::bind::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
3004			pure::<ControlFlowBreakAppliedBrand<i32>, _>(a),
3005			f,
3006		) == f(a)
3007	}
3008
3009	/// Verifies the Right Identity law for `ControlFlowBreakAppliedBrand` Monad.
3010	#[quickcheck]
3011	fn monad_right_identity_with_break(x: ControlFlowWrapper<i32, i32>) -> bool {
3012		let x = x.into_inner();
3013		explicit::bind::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
3014			x,
3015			pure::<ControlFlowBreakAppliedBrand<i32>, _>,
3016		) == x
3017	}
3018
3019	/// Verifies the Associativity law for `ControlFlowBreakAppliedBrand` Monad.
3020	#[quickcheck]
3021	fn monad_associativity_with_break(x: ControlFlowWrapper<i32, i32>) -> bool {
3022		let x = x.into_inner();
3023		let f = |x: i32| pure::<ControlFlowBreakAppliedBrand<i32>, _>(x.wrapping_mul(2));
3024		let g = |x: i32| pure::<ControlFlowBreakAppliedBrand<i32>, _>(x.wrapping_add(1));
3025		explicit::bind::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
3026			explicit::bind::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(x, f),
3027			g,
3028		) == explicit::bind::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(x, |a| {
3029			explicit::bind::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(f(a), g)
3030		})
3031	}
3032
3033	// Applicative and Monad marker trait verification
3034
3035	/// Verifies that `ControlFlowContinueAppliedBrand` satisfies the `Applicative` trait.
3036	#[test]
3037	fn test_applicative_continue_applied() {
3038		fn assert_applicative<B: crate::classes::Applicative>() {}
3039		assert_applicative::<ControlFlowContinueAppliedBrand<i32>>();
3040	}
3041
3042	/// Verifies that `ControlFlowBreakAppliedBrand` satisfies the `Applicative` trait.
3043	#[test]
3044	fn test_applicative_break_applied() {
3045		fn assert_applicative<B: crate::classes::Applicative>() {}
3046		assert_applicative::<ControlFlowBreakAppliedBrand<i32>>();
3047	}
3048
3049	/// Verifies that `ControlFlowContinueAppliedBrand` satisfies the `Monad` trait.
3050	#[test]
3051	fn test_monad_continue_applied() {
3052		fn assert_monad<B: crate::classes::Monad>() {}
3053		assert_monad::<ControlFlowContinueAppliedBrand<i32>>();
3054	}
3055
3056	/// Verifies that `ControlFlowBreakAppliedBrand` satisfies the `Monad` trait.
3057	#[test]
3058	fn test_monad_break_applied() {
3059		fn assert_monad<B: crate::classes::Monad>() {}
3060		assert_monad::<ControlFlowBreakAppliedBrand<i32>>();
3061	}
3062
3063	// MonadRec tests for ControlFlowContinueAppliedBrand
3064
3065	/// Tests the MonadRec identity law for `ControlFlowContinueAppliedBrand`:
3066	/// `tail_rec_m(|a| Break(Break(a)), x) == Break(x)`.
3067	#[quickcheck]
3068	fn monad_rec_continue_applied_identity(x: i32) -> bool {
3069		tail_rec_m::<ControlFlowContinueAppliedBrand<()>, _, _>(
3070			|a| ControlFlow::Break(ControlFlow::Break(a)),
3071			x,
3072		) == ControlFlow::Break(x)
3073	}
3074
3075	/// Tests a recursive computation that sums a range via `tail_rec_m`
3076	/// on the break channel of `ControlFlowContinueAppliedBrand`.
3077	#[test]
3078	fn monad_rec_continue_applied_sum_range() {
3079		let result = tail_rec_m::<ControlFlowContinueAppliedBrand<&str>, _, _>(
3080			|(n, acc)| {
3081				if n == 0 {
3082					ControlFlow::Break(ControlFlow::Break(acc))
3083				} else {
3084					ControlFlow::Break(ControlFlow::Continue((n - 1, acc + n)))
3085				}
3086			},
3087			(100i64, 0i64),
3088		);
3089		assert_eq!(result, ControlFlow::Break(5050));
3090	}
3091
3092	/// Tests that `tail_rec_m` short-circuits on `Continue` for `ControlFlowContinueAppliedBrand`.
3093	#[test]
3094	fn monad_rec_continue_applied_short_circuit() {
3095		let result = tail_rec_m::<ControlFlowContinueAppliedBrand<&str>, _, _>(
3096			|n| {
3097				if n == 5 {
3098					ControlFlow::Continue("stopped")
3099				} else {
3100					ControlFlow::Break(ControlFlow::Continue(n + 1))
3101				}
3102			},
3103			0,
3104		);
3105		assert_eq!(result, ControlFlow::<i32, &str>::Continue("stopped"));
3106	}
3107
3108	/// Tests stack safety: `tail_rec_m` handles large iteration counts
3109	/// for `ControlFlowContinueAppliedBrand`.
3110	#[test]
3111	fn monad_rec_continue_applied_stack_safety() {
3112		let iterations: i64 = 200_000;
3113		let result = tail_rec_m::<ControlFlowContinueAppliedBrand<()>, _, _>(
3114			|acc| {
3115				if acc < iterations {
3116					ControlFlow::Break(ControlFlow::Continue(acc + 1))
3117				} else {
3118					ControlFlow::Break(ControlFlow::Break(acc))
3119				}
3120			},
3121			0i64,
3122		);
3123		assert_eq!(result, ControlFlow::Break(iterations));
3124	}
3125
3126	// MonadRec tests for ControlFlowBreakAppliedBrand
3127
3128	/// Tests the MonadRec identity law for `ControlFlowBreakAppliedBrand`:
3129	/// `tail_rec_m(|a| Continue(Break(a)), x) == Continue(x)`.
3130	#[quickcheck]
3131	fn monad_rec_break_applied_identity(x: i32) -> bool {
3132		tail_rec_m::<ControlFlowBreakAppliedBrand<()>, _, _>(
3133			|a| ControlFlow::Continue(ControlFlow::Break(a)),
3134			x,
3135		) == ControlFlow::Continue(x)
3136	}
3137
3138	/// Tests a recursive computation that sums a range via `tail_rec_m`
3139	/// on the continue channel of `ControlFlowBreakAppliedBrand`.
3140	#[test]
3141	fn monad_rec_break_applied_sum_range() {
3142		let result = tail_rec_m::<ControlFlowBreakAppliedBrand<&str>, _, _>(
3143			|(n, acc)| {
3144				if n == 0 {
3145					ControlFlow::Continue(ControlFlow::Break(acc))
3146				} else {
3147					ControlFlow::Continue(ControlFlow::Continue((n - 1, acc + n)))
3148				}
3149			},
3150			(100i64, 0i64),
3151		);
3152		assert_eq!(result, ControlFlow::Continue(5050));
3153	}
3154
3155	/// Tests that `tail_rec_m` short-circuits on `Break` for `ControlFlowBreakAppliedBrand`.
3156	#[test]
3157	fn monad_rec_break_applied_short_circuit() {
3158		let result = tail_rec_m::<ControlFlowBreakAppliedBrand<&str>, _, _>(
3159			|n| {
3160				if n == 5 {
3161					ControlFlow::Break("stopped")
3162				} else {
3163					ControlFlow::Continue(ControlFlow::Continue(n + 1))
3164				}
3165			},
3166			0,
3167		);
3168		assert_eq!(result, ControlFlow::<&str, i32>::Break("stopped"));
3169	}
3170
3171	/// Tests stack safety: `tail_rec_m` handles large iteration counts
3172	/// for `ControlFlowBreakAppliedBrand`.
3173	#[test]
3174	fn monad_rec_break_applied_stack_safety() {
3175		let iterations: i64 = 200_000;
3176		let result = tail_rec_m::<ControlFlowBreakAppliedBrand<()>, _, _>(
3177			|acc| {
3178				if acc < iterations {
3179					ControlFlow::Continue(ControlFlow::Continue(acc + 1))
3180				} else {
3181					ControlFlow::Continue(ControlFlow::Break(acc))
3182				}
3183			},
3184			0i64,
3185		);
3186		assert_eq!(result, ControlFlow::Continue(iterations));
3187	}
3188
3189	// MonadRec marker trait verification
3190
3191	/// Verifies that `ControlFlowContinueAppliedBrand` satisfies the `MonadRec` trait.
3192	#[test]
3193	fn test_monad_rec_continue_applied() {
3194		fn assert_monad_rec<B: crate::classes::MonadRec>() {}
3195		assert_monad_rec::<ControlFlowContinueAppliedBrand<i32>>();
3196	}
3197
3198	/// Verifies that `ControlFlowBreakAppliedBrand` satisfies the `MonadRec` trait.
3199	#[test]
3200	fn test_monad_rec_break_applied() {
3201		fn assert_monad_rec<B: crate::classes::MonadRec>() {}
3202		assert_monad_rec::<ControlFlowBreakAppliedBrand<i32>>();
3203	}
3204
3205	/// Tests the `break_val` method.
3206	///
3207	/// Verifies that `break_val` returns `Some(b)` for `Break(b)` and `None` for `Continue(_)`.
3208	#[test]
3209	fn test_break_val() {
3210		let cf: ControlFlow<i32, i32> = ControlFlow::Break(42);
3211		assert_eq!(ControlFlowBrand::break_val(cf), Some(42));
3212
3213		let cf: ControlFlow<i32, i32> = ControlFlow::Continue(1);
3214		assert_eq!(ControlFlowBrand::break_val(cf), None);
3215	}
3216
3217	/// Tests the `continue_val` method.
3218	///
3219	/// Verifies that `continue_val` returns `Some(c)` for `Continue(c)` and `None` for `Break(_)`.
3220	#[test]
3221	fn test_continue_val() {
3222		let cf: ControlFlow<i32, i32> = ControlFlow::Continue(7);
3223		assert_eq!(ControlFlowBrand::continue_val(cf), Some(7));
3224
3225		let cf: ControlFlow<i32, i32> = ControlFlow::Break(42);
3226		assert_eq!(ControlFlowBrand::continue_val(cf), None);
3227	}
3228
3229	/// Tests the `swap` method.
3230	///
3231	/// Verifies that `swap` maps `Continue(c)` to `Break(c)` and `Break(b)` to `Continue(b)`.
3232	#[test]
3233	fn test_swap() {
3234		let cf: ControlFlow<&str, i32> = ControlFlow::Continue(1);
3235		assert_eq!(ControlFlowBrand::swap(cf), ControlFlow::Break(1));
3236
3237		let cf: ControlFlow<&str, i32> = ControlFlow::Break("hello");
3238		assert_eq!(ControlFlowBrand::swap(cf), ControlFlow::Continue("hello"));
3239	}
3240
3241	/// Property test: `break_val` and `continue_val` are complementary.
3242	#[quickcheck]
3243	fn break_and_continue_val_complementary(x: ControlFlowWrapper<i32, i32>) -> bool {
3244		let cf = x.into_inner();
3245		ControlFlowBrand::break_val(cf).is_some() != ControlFlowBrand::continue_val(cf).is_some()
3246	}
3247
3248	/// Property test: swapping twice is identity.
3249	#[quickcheck]
3250	fn swap_involution(x: ControlFlowWrapper<i32, i32>) -> bool {
3251		let cf = x.into_inner();
3252		ControlFlowBrand::swap(ControlFlowBrand::swap(cf)) == cf
3253	}
3254}