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.
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		#[no_inferable_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::*,
1364		/// 		functions::*,
1365		/// 	},
1366		/// };
1367		///
1368		/// let f: ControlFlow<_, ()> = ControlFlow::Break(lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
1369		/// assert_eq!(
1370		/// 	apply::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _>(f, ControlFlow::Break(5)),
1371		/// 	ControlFlow::Break(10)
1372		/// );
1373		/// ```
1374		fn apply<'a, FnBrand: 'a + CloneFn, A: 'a + Clone, B: 'a>(
1375			ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, A, B>>),
1376			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1377		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1378			match (ff, fa) {
1379				(ControlFlow::Break(f), ControlFlow::Break(a)) => ControlFlow::Break(f(a)),
1380				(ControlFlow::Continue(e), _) => ControlFlow::Continue(e),
1381				(_, ControlFlow::Continue(e)) => ControlFlow::Continue(e),
1382			}
1383		}
1384	}
1385
1386	#[document_type_parameters("The continue type.")]
1387	impl<ContinueType: Clone + 'static> Semimonad for ControlFlowContinueAppliedBrand<ContinueType> {
1388		/// Chains control flow computations.
1389		///
1390		/// This method chains two computations, where the second computation depends on the result of the first.
1391		#[document_signature]
1392		///
1393		#[document_type_parameters(
1394			"The lifetime of the values.",
1395			"The type of the result of the first computation.",
1396			"The type of the result of the second computation."
1397		)]
1398		///
1399		#[document_parameters(
1400			"The first control flow.",
1401			"The function to apply to the value inside the control flow."
1402		)]
1403		///
1404		#[document_returns(
1405			"The result of applying `f` to the value if `ma` is `Break`, otherwise the original continue."
1406		)]
1407		#[document_examples]
1408		///
1409		/// ```
1410		/// use {
1411		/// 	core::ops::ControlFlow,
1412		/// 	fp_library::{
1413		/// 		brands::*,
1414		/// 		functions::*,
1415		/// 	},
1416		/// };
1417		///
1418		/// assert_eq!(
1419		/// 	explicit::bind::<ControlFlowContinueAppliedBrand<()>, _, _, _, _>(
1420		/// 		ControlFlow::Break(5),
1421		/// 		|x| { ControlFlow::Break(x * 2) }
1422		/// 	),
1423		/// 	ControlFlow::<_, ()>::Break(10)
1424		/// );
1425		/// ```
1426		fn bind<'a, A: 'a, B: 'a>(
1427			ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1428			func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1429		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1430			ControlFlowBrand::bind_break(ma, func)
1431		}
1432	}
1433
1434	#[document_type_parameters("The continue type.")]
1435	impl<ContinueType: 'static> Foldable for ControlFlowContinueAppliedBrand<ContinueType> {
1436		/// Folds the control flow from the right.
1437		///
1438		/// This method performs a right-associative fold of the control flow.
1439		#[document_signature]
1440		///
1441		#[document_type_parameters(
1442			"The lifetime of the values.",
1443			"The brand of the cloneable function to use.",
1444			"The type of the elements in the structure.",
1445			"The type of the accumulator."
1446		)]
1447		///
1448		#[document_parameters(
1449			"The folding function.",
1450			"The initial value.",
1451			"The control flow to fold."
1452		)]
1453		///
1454		#[document_returns("`func(a, initial)` if `fa` is `Break(a)`, otherwise `initial`.")]
1455		///
1456		#[document_examples]
1457		///
1458		/// ```
1459		/// use {
1460		/// 	core::ops::ControlFlow,
1461		/// 	fp_library::{
1462		/// 		brands::*,
1463		/// 		functions::*,
1464		/// 	},
1465		/// };
1466		///
1467		/// assert_eq!(
1468		/// 	explicit::fold_right::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _, _, _>(
1469		/// 		|x, acc| x + acc,
1470		/// 		0,
1471		/// 		ControlFlow::Break(5)
1472		/// 	),
1473		/// 	5
1474		/// );
1475		/// assert_eq!(
1476		/// 	explicit::fold_right::<RcFnBrand, ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(
1477		/// 		|x: i32, acc| x + acc,
1478		/// 		0,
1479		/// 		ControlFlow::Continue(1)
1480		/// 	),
1481		/// 	0
1482		/// );
1483		/// ```
1484		fn fold_right<'a, FnBrand, A: 'a, B: 'a>(
1485			func: impl Fn(A, B) -> B + 'a,
1486			initial: B,
1487			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1488		) -> B
1489		where
1490			FnBrand: CloneFn + 'a, {
1491			ControlFlowBrand::fold_right(fa, func, initial)
1492		}
1493
1494		/// Folds the control flow from the left.
1495		///
1496		/// This method performs a left-associative fold of the control flow.
1497		#[document_signature]
1498		///
1499		#[document_type_parameters(
1500			"The lifetime of the values.",
1501			"The brand of the cloneable function to use.",
1502			"The type of the elements in the structure.",
1503			"The type of the accumulator."
1504		)]
1505		///
1506		#[document_parameters(
1507			"The folding function.",
1508			"The initial value.",
1509			"The control flow to fold."
1510		)]
1511		///
1512		#[document_returns("`func(initial, a)` if `fa` is `Break(a)`, otherwise `initial`.")]
1513		///
1514		#[document_examples]
1515		///
1516		/// ```
1517		/// use {
1518		/// 	core::ops::ControlFlow,
1519		/// 	fp_library::{
1520		/// 		brands::*,
1521		/// 		functions::*,
1522		/// 	},
1523		/// };
1524		///
1525		/// assert_eq!(
1526		/// 	explicit::fold_left::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _, _, _>(
1527		/// 		|acc, x| acc + x,
1528		/// 		0,
1529		/// 		ControlFlow::Break(5)
1530		/// 	),
1531		/// 	5
1532		/// );
1533		/// assert_eq!(
1534		/// 	explicit::fold_left::<RcFnBrand, ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(
1535		/// 		|acc, x: i32| acc + x,
1536		/// 		0,
1537		/// 		ControlFlow::Continue(1)
1538		/// 	),
1539		/// 	0
1540		/// );
1541		/// ```
1542		fn fold_left<'a, FnBrand, A: 'a, B: 'a>(
1543			func: impl Fn(B, A) -> B + 'a,
1544			initial: B,
1545			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1546		) -> B
1547		where
1548			FnBrand: CloneFn + 'a, {
1549			ControlFlowBrand::fold_left(fa, func, initial)
1550		}
1551
1552		/// Maps the value to a monoid and returns it.
1553		///
1554		/// This method maps the element of the control flow to a monoid and then returns it.
1555		#[document_signature]
1556		///
1557		#[document_type_parameters(
1558			"The lifetime of the values.",
1559			"The brand of the cloneable function to use.",
1560			"The type of the elements in the structure.",
1561			"The type of the monoid."
1562		)]
1563		///
1564		#[document_parameters("The mapping function.", "The control flow to fold.")]
1565		///
1566		#[document_returns("`func(a)` if `fa` is `Break(a)`, otherwise `M::empty()`.")]
1567		///
1568		#[document_examples]
1569		///
1570		/// ```
1571		/// use {
1572		/// 	core::ops::ControlFlow,
1573		/// 	fp_library::{
1574		/// 		brands::*,
1575		/// 		functions::*,
1576		/// 	},
1577		/// };
1578		///
1579		/// assert_eq!(
1580		/// 	explicit::fold_map::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _, _, _>(
1581		/// 		|x: i32| x.to_string(),
1582		/// 		ControlFlow::Break(5)
1583		/// 	),
1584		/// 	"5".to_string()
1585		/// );
1586		/// assert_eq!(
1587		/// 	explicit::fold_map::<RcFnBrand, ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(
1588		/// 		|x: i32| x.to_string(),
1589		/// 		ControlFlow::Continue(1)
1590		/// 	),
1591		/// 	"".to_string()
1592		/// );
1593		/// ```
1594		fn fold_map<'a, FnBrand, A: 'a, M>(
1595			func: impl Fn(A) -> M + 'a,
1596			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1597		) -> M
1598		where
1599			M: Monoid + 'a,
1600			FnBrand: CloneFn + 'a, {
1601			ControlFlowBrand::fold_map(fa, func)
1602		}
1603	}
1604
1605	#[document_type_parameters("The continue type.")]
1606	impl<ContinueType: Clone + 'static> Traversable for ControlFlowContinueAppliedBrand<ContinueType> {
1607		/// Traverses the control flow with an applicative function.
1608		///
1609		/// This method maps the element of the control flow to a computation, evaluates it, and combines the result into an applicative context.
1610		#[document_signature]
1611		///
1612		#[document_type_parameters(
1613			"The lifetime of the values.",
1614			"The type of the elements in the traversable structure.",
1615			"The type of the elements in the resulting traversable structure.",
1616			"The applicative context."
1617		)]
1618		///
1619		#[document_parameters("The function to apply.", "The control flow to traverse.")]
1620		///
1621		#[document_returns("The control flow wrapped in the applicative context.")]
1622		///
1623		#[document_examples]
1624		///
1625		/// ```
1626		/// use {
1627		/// 	core::ops::ControlFlow,
1628		/// 	fp_library::{
1629		/// 		brands::*,
1630		/// 		functions::*,
1631		/// 	},
1632		/// };
1633		///
1634		/// assert_eq!(
1635		/// 	explicit::traverse::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _, OptionBrand, _, _>(
1636		/// 		|x| Some(x * 2),
1637		/// 		ControlFlow::Break(5)
1638		/// 	),
1639		/// 	Some(ControlFlow::Break(10))
1640		/// );
1641		/// assert_eq!(
1642		/// 	explicit::traverse::<
1643		/// 		RcFnBrand,
1644		/// 		ControlFlowContinueAppliedBrand<i32>,
1645		/// 		_,
1646		/// 		_,
1647		/// 		OptionBrand,
1648		/// 		_,
1649		/// 		_,
1650		/// 	>(|x: i32| Some(x * 2), ControlFlow::Continue(1)),
1651		/// 	Some(ControlFlow::Continue(1))
1652		/// );
1653		/// ```
1654		fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1655			func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1656			ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1657		) -> 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>)>)
1658		where
1659			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1660			match ta {
1661				ControlFlow::Break(a) => F::map(|b| ControlFlow::Break(b), func(a)),
1662				ControlFlow::Continue(e) => F::pure(ControlFlow::Continue(e)),
1663			}
1664		}
1665
1666		/// Sequences a control flow of applicative.
1667		///
1668		/// This method evaluates the computation inside the control flow and accumulates the result into an applicative context.
1669		#[document_signature]
1670		///
1671		#[document_type_parameters(
1672			"The lifetime of the values.",
1673			"The type of the elements in the traversable structure.",
1674			"The applicative context."
1675		)]
1676		///
1677		#[document_parameters("The control flow containing the applicative value.")]
1678		///
1679		#[document_returns("The control flow wrapped in the applicative context.")]
1680		///
1681		#[document_examples]
1682		///
1683		/// ```
1684		/// use {
1685		/// 	core::ops::ControlFlow,
1686		/// 	fp_library::{
1687		/// 		brands::*,
1688		/// 		functions::*,
1689		/// 	},
1690		/// };
1691		///
1692		/// assert_eq!(
1693		/// 	sequence::<ControlFlowContinueAppliedBrand<()>, _, OptionBrand>(ControlFlow::Break(Some(
1694		/// 		5
1695		/// 	))),
1696		/// 	Some(ControlFlow::Break(5))
1697		/// );
1698		/// assert_eq!(
1699		/// 	sequence::<ControlFlowContinueAppliedBrand<i32>, i32, OptionBrand>(ControlFlow::<
1700		/// 		Option<i32>,
1701		/// 		i32,
1702		/// 	>::Continue(1)),
1703		/// 	Some(ControlFlow::<i32, i32>::Continue(1))
1704		/// );
1705		/// ```
1706		fn sequence<'a, A: 'a + Clone, F: Applicative>(
1707			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>)>)
1708		) -> 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>)>)
1709		where
1710			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1711			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
1712			match ta {
1713				ControlFlow::Break(fa) => F::map(|a| ControlFlow::Break(a), fa),
1714				ControlFlow::Continue(e) => F::pure(ControlFlow::Continue(e)),
1715			}
1716		}
1717	}
1718
1719	// ControlFlowBreakAppliedBrand<BreakType> (Functor over C, the Continue type)
1720
1721	impl_kind! {
1722		#[no_inferable_brand]
1723		impl<BreakType: 'static> for ControlFlowBreakAppliedBrand<BreakType> {
1724			type Of<'a, C: 'a>: 'a = ControlFlow<BreakType, C>;
1725		}
1726	}
1727
1728	#[document_type_parameters("The break type.")]
1729	impl<BreakType: 'static> Functor for ControlFlowBreakAppliedBrand<BreakType> {
1730		/// Maps a function over the continue value in the control flow.
1731		///
1732		/// 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.
1733		#[document_signature]
1734		///
1735		#[document_type_parameters(
1736			"The lifetime of the values.",
1737			"The type of the continue value.",
1738			"The type of the result of applying the function."
1739		)]
1740		///
1741		#[document_parameters(
1742			"The function to apply to the continue value.",
1743			"The control flow to map over."
1744		)]
1745		///
1746		#[document_returns(
1747			"A new control flow containing the result of applying the function to the continue value."
1748		)]
1749		///
1750		#[document_examples]
1751		///
1752		/// ```
1753		/// use {
1754		/// 	core::ops::ControlFlow,
1755		/// 	fp_library::{
1756		/// 		brands::*,
1757		/// 		functions::*,
1758		/// 	},
1759		/// };
1760		///
1761		/// assert_eq!(
1762		/// 	explicit::map::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
1763		/// 		|x: i32| x * 2,
1764		/// 		ControlFlow::<i32, i32>::Continue(5)
1765		/// 	),
1766		/// 	ControlFlow::Continue(10)
1767		/// );
1768		/// ```
1769		fn map<'a, A: 'a, B: 'a>(
1770			func: impl Fn(A) -> B + 'a,
1771			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1772		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1773			ControlFlowBrand::map_continue(fa, func)
1774		}
1775	}
1776
1777	#[document_type_parameters("The break type.")]
1778	impl<BreakType: Clone + 'static> Lift for ControlFlowBreakAppliedBrand<BreakType> {
1779		/// Lifts a binary function into the control flow context (over continue).
1780		///
1781		/// This method lifts a binary function to operate on continue values within the control flow context.
1782		#[document_signature]
1783		///
1784		#[document_type_parameters(
1785			"The lifetime of the values.",
1786			"The type of the first continue value.",
1787			"The type of the second continue value.",
1788			"The type of the result continue value."
1789		)]
1790		///
1791		#[document_parameters(
1792			"The binary function to apply to the continues.",
1793			"The first control flow.",
1794			"The second control flow."
1795		)]
1796		///
1797		#[document_returns(
1798			"`Continue(f(a, b))` if both are `Continue`, otherwise the first break encountered."
1799		)]
1800		#[document_examples]
1801		///
1802		/// ```
1803		/// use {
1804		/// 	core::ops::ControlFlow,
1805		/// 	fp_library::{
1806		/// 		brands::*,
1807		/// 		functions::*,
1808		/// 	},
1809		/// };
1810		///
1811		/// assert_eq!(
1812		/// 	explicit::lift2::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _, _, _>(
1813		/// 		|x: i32, y: i32| x + y,
1814		/// 		ControlFlow::Continue(1),
1815		/// 		ControlFlow::Continue(2)
1816		/// 	),
1817		/// 	ControlFlow::Continue(3)
1818		/// );
1819		/// assert_eq!(
1820		/// 	explicit::lift2::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _, _, _>(
1821		/// 		|x: i32, y: i32| x + y,
1822		/// 		ControlFlow::Continue(1),
1823		/// 		ControlFlow::Break(2)
1824		/// 	),
1825		/// 	ControlFlow::Break(2)
1826		/// );
1827		/// ```
1828		fn lift2<'a, A, B, C>(
1829			func: impl Fn(A, B) -> C + 'a,
1830			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1831			fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1832		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
1833		where
1834			A: Clone + 'a,
1835			B: Clone + 'a,
1836			C: 'a, {
1837			match (fa, fb) {
1838				(ControlFlow::Continue(a), ControlFlow::Continue(b)) =>
1839					ControlFlow::Continue(func(a, b)),
1840				(ControlFlow::Break(t), _) => ControlFlow::Break(t),
1841				(_, ControlFlow::Break(t)) => ControlFlow::Break(t),
1842			}
1843		}
1844	}
1845
1846	#[document_type_parameters("The break type.")]
1847	impl<BreakType: 'static> Pointed for ControlFlowBreakAppliedBrand<BreakType> {
1848		/// Wraps a value in a control flow (as continue).
1849		///
1850		/// This method wraps a value in the `Continue` variant of a `ControlFlow`.
1851		#[document_signature]
1852		///
1853		#[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
1854		///
1855		#[document_parameters("The value to wrap.")]
1856		///
1857		#[document_returns("`Continue(a)`.")]
1858		///
1859		#[document_examples]
1860		///
1861		/// ```
1862		/// use {
1863		/// 	core::ops::ControlFlow,
1864		/// 	fp_library::{
1865		/// 		brands::*,
1866		/// 		functions::*,
1867		/// 	},
1868		/// };
1869		///
1870		/// assert_eq!(pure::<ControlFlowBreakAppliedBrand<()>, _>(5), ControlFlow::<(), _>::Continue(5));
1871		/// ```
1872		fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1873			ControlFlow::Continue(a)
1874		}
1875	}
1876
1877	#[document_type_parameters("The break type.")]
1878	impl<BreakType: Clone + 'static> ApplyFirst for ControlFlowBreakAppliedBrand<BreakType> {}
1879
1880	#[document_type_parameters("The break type.")]
1881	impl<BreakType: Clone + 'static> ApplySecond for ControlFlowBreakAppliedBrand<BreakType> {}
1882
1883	#[document_type_parameters("The break type.")]
1884	impl<BreakType: Clone + 'static> Semiapplicative for ControlFlowBreakAppliedBrand<BreakType> {
1885		/// Applies a wrapped function to a wrapped value (over continue).
1886		///
1887		/// This method applies a function wrapped in a control flow (as continue) to a value wrapped in a control flow (as continue).
1888		#[document_signature]
1889		///
1890		#[document_type_parameters(
1891			"The lifetime of the values.",
1892			"The brand of the cloneable function wrapper.",
1893			"The type of the input value.",
1894			"The type of the output value."
1895		)]
1896		///
1897		#[document_parameters(
1898			"The control flow containing the function (in Continue).",
1899			"The control flow containing the value (in Continue)."
1900		)]
1901		///
1902		#[document_returns(
1903			"`Continue(f(a))` if both are `Continue`, otherwise the first break encountered."
1904		)]
1905		#[document_examples]
1906		///
1907		/// ```
1908		/// use {
1909		/// 	core::ops::ControlFlow,
1910		/// 	fp_library::{
1911		/// 		brands::*,
1912		/// 		classes::*,
1913		/// 		functions::*,
1914		/// 	},
1915		/// };
1916		///
1917		/// let f: ControlFlow<(), _> =
1918		/// 	ControlFlow::Continue(lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
1919		/// assert_eq!(
1920		/// 	apply::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _>(f, ControlFlow::Continue(5)),
1921		/// 	ControlFlow::Continue(10)
1922		/// );
1923		/// ```
1924		fn apply<'a, FnBrand: 'a + CloneFn, A: 'a + Clone, B: 'a>(
1925			ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, A, B>>),
1926			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1927		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1928			match (ff, fa) {
1929				(ControlFlow::Continue(f), ControlFlow::Continue(a)) => ControlFlow::Continue(f(a)),
1930				(ControlFlow::Break(t), _) => ControlFlow::Break(t),
1931				(_, ControlFlow::Break(t)) => ControlFlow::Break(t),
1932			}
1933		}
1934	}
1935
1936	#[document_type_parameters("The break type.")]
1937	impl<BreakType: Clone + 'static> Semimonad for ControlFlowBreakAppliedBrand<BreakType> {
1938		/// Chains control flow computations (over continue).
1939		///
1940		/// This method chains two computations, where the second computation depends on the result of the first (over continue).
1941		#[document_signature]
1942		///
1943		#[document_type_parameters(
1944			"The lifetime of the values.",
1945			"The type of the result of the first computation.",
1946			"The type of the result of the second computation."
1947		)]
1948		///
1949		#[document_parameters(
1950			"The first control flow.",
1951			"The function to apply to the continue value."
1952		)]
1953		///
1954		#[document_returns(
1955			"The result of applying `f` to the continue if `ma` is `Continue`, otherwise the original break."
1956		)]
1957		///
1958		#[document_examples]
1959		///
1960		/// ```
1961		/// use {
1962		/// 	core::ops::ControlFlow,
1963		/// 	fp_library::{
1964		/// 		brands::*,
1965		/// 		functions::*,
1966		/// 	},
1967		/// };
1968		///
1969		/// assert_eq!(
1970		/// 	explicit::bind::<ControlFlowBreakAppliedBrand<()>, _, _, _, _>(
1971		/// 		ControlFlow::Continue(5),
1972		/// 		|x| { ControlFlow::Continue(x * 2) }
1973		/// 	),
1974		/// 	ControlFlow::<(), _>::Continue(10)
1975		/// );
1976		/// ```
1977		fn bind<'a, A: 'a, B: 'a>(
1978			ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1979			func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1980		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1981			ControlFlowBrand::bind_continue(ma, func)
1982		}
1983	}
1984
1985	#[document_type_parameters("The break type.")]
1986	impl<BreakType: 'static> Foldable for ControlFlowBreakAppliedBrand<BreakType> {
1987		/// Folds the control flow from the right (over continue).
1988		///
1989		/// This method performs a right-associative fold of the control flow (over continue).
1990		#[document_signature]
1991		///
1992		#[document_type_parameters(
1993			"The lifetime of the values.",
1994			"The brand of the cloneable function to use.",
1995			"The type of the elements in the structure.",
1996			"The type of the accumulator."
1997		)]
1998		///
1999		#[document_parameters(
2000			"The folding function.",
2001			"The initial value.",
2002			"The control flow to fold."
2003		)]
2004		///
2005		#[document_returns("`func(a, initial)` if `fa` is `Continue(a)`, otherwise `initial`.")]
2006		///
2007		#[document_examples]
2008		///
2009		/// ```
2010		/// use {
2011		/// 	core::ops::ControlFlow,
2012		/// 	fp_library::{
2013		/// 		brands::*,
2014		/// 		functions::*,
2015		/// 	},
2016		/// };
2017		///
2018		/// assert_eq!(
2019		/// 	explicit::fold_right::<RcFnBrand, ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
2020		/// 		|x: i32, acc| x + acc,
2021		/// 		0,
2022		/// 		ControlFlow::Continue(1)
2023		/// 	),
2024		/// 	1
2025		/// );
2026		/// assert_eq!(
2027		/// 	explicit::fold_right::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _, _, _>(
2028		/// 		|x: i32, acc| x + acc,
2029		/// 		0,
2030		/// 		ControlFlow::Break(())
2031		/// 	),
2032		/// 	0
2033		/// );
2034		/// ```
2035		fn fold_right<'a, FnBrand, A: 'a, B: 'a>(
2036			func: impl Fn(A, B) -> B + 'a,
2037			initial: B,
2038			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2039		) -> B
2040		where
2041			FnBrand: CloneFn + 'a, {
2042			match fa {
2043				ControlFlow::Continue(e) => func(e, initial),
2044				ControlFlow::Break(_) => initial,
2045			}
2046		}
2047
2048		/// Folds the control flow from the left (over continue).
2049		///
2050		/// This method performs a left-associative fold of the control flow (over continue).
2051		#[document_signature]
2052		///
2053		#[document_type_parameters(
2054			"The lifetime of the values.",
2055			"The brand of the cloneable function to use.",
2056			"The type of the elements in the structure.",
2057			"The type of the accumulator."
2058		)]
2059		///
2060		#[document_parameters(
2061			"The folding function.",
2062			"The initial value.",
2063			"The control flow to fold."
2064		)]
2065		///
2066		#[document_returns("`func(initial, a)` if `fa` is `Continue(a)`, otherwise `initial`.")]
2067		///
2068		#[document_examples]
2069		///
2070		/// ```
2071		/// use {
2072		/// 	core::ops::ControlFlow,
2073		/// 	fp_library::{
2074		/// 		brands::*,
2075		/// 		functions::*,
2076		/// 	},
2077		/// };
2078		///
2079		/// assert_eq!(
2080		/// 	explicit::fold_left::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _, _, _>(
2081		/// 		|acc, x: i32| acc + x,
2082		/// 		0,
2083		/// 		ControlFlow::Continue(5)
2084		/// 	),
2085		/// 	5
2086		/// );
2087		/// assert_eq!(
2088		/// 	explicit::fold_left::<RcFnBrand, ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
2089		/// 		|acc, x: i32| acc + x,
2090		/// 		0,
2091		/// 		ControlFlow::Break(1)
2092		/// 	),
2093		/// 	0
2094		/// );
2095		/// ```
2096		fn fold_left<'a, FnBrand, A: 'a, B: 'a>(
2097			func: impl Fn(B, A) -> B + 'a,
2098			initial: B,
2099			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2100		) -> B
2101		where
2102			FnBrand: CloneFn + 'a, {
2103			match fa {
2104				ControlFlow::Continue(e) => func(initial, e),
2105				ControlFlow::Break(_) => initial,
2106			}
2107		}
2108
2109		/// Maps the value to a monoid and returns it (over continue).
2110		///
2111		/// This method maps the element of the control flow to a monoid and then returns it (over continue).
2112		#[document_signature]
2113		///
2114		#[document_type_parameters(
2115			"The lifetime of the values.",
2116			"The brand of the cloneable function to use.",
2117			"The type of the elements in the structure.",
2118			"The type of the monoid."
2119		)]
2120		///
2121		#[document_parameters("The mapping function.", "The control flow to fold.")]
2122		///
2123		#[document_returns("`func(a)` if `fa` is `Continue(a)`, otherwise `M::empty()`.")]
2124		///
2125		#[document_examples]
2126		///
2127		/// ```
2128		/// use {
2129		/// 	core::ops::ControlFlow,
2130		/// 	fp_library::{
2131		/// 		brands::*,
2132		/// 		functions::*,
2133		/// 	},
2134		/// };
2135		///
2136		/// assert_eq!(
2137		/// 	explicit::fold_map::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _, _, _>(
2138		/// 		|x: i32| x.to_string(),
2139		/// 		ControlFlow::Continue(5)
2140		/// 	),
2141		/// 	"5".to_string()
2142		/// );
2143		/// assert_eq!(
2144		/// 	explicit::fold_map::<RcFnBrand, ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
2145		/// 		|x: i32| x.to_string(),
2146		/// 		ControlFlow::Break(1)
2147		/// 	),
2148		/// 	"".to_string()
2149		/// );
2150		/// ```
2151		fn fold_map<'a, FnBrand, A: 'a, M>(
2152			func: impl Fn(A) -> M + 'a,
2153			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2154		) -> M
2155		where
2156			M: Monoid + 'a,
2157			FnBrand: CloneFn + 'a, {
2158			match fa {
2159				ControlFlow::Continue(e) => func(e),
2160				ControlFlow::Break(_) => M::empty(),
2161			}
2162		}
2163	}
2164
2165	#[document_type_parameters("The break type.")]
2166	impl<BreakType: Clone + 'static> Traversable for ControlFlowBreakAppliedBrand<BreakType> {
2167		/// Traverses the control flow with an applicative function (over continue).
2168		///
2169		/// This method maps the element of the control flow to a computation, evaluates it, and combines the result into an applicative context (over continue).
2170		#[document_signature]
2171		///
2172		#[document_type_parameters(
2173			"The lifetime of the values.",
2174			"The type of the elements in the traversable structure.",
2175			"The type of the elements in the resulting traversable structure.",
2176			"The applicative context."
2177		)]
2178		///
2179		#[document_parameters("The function to apply.", "The control flow to traverse.")]
2180		///
2181		#[document_returns("The control flow wrapped in the applicative context.")]
2182		///
2183		#[document_examples]
2184		///
2185		/// ```
2186		/// use {
2187		/// 	core::ops::ControlFlow,
2188		/// 	fp_library::{
2189		/// 		brands::*,
2190		/// 		functions::*,
2191		/// 	},
2192		/// };
2193		///
2194		/// assert_eq!(
2195		/// 	explicit::traverse::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _, OptionBrand, _, _>(
2196		/// 		|x| Some(x * 2),
2197		/// 		ControlFlow::Continue(5)
2198		/// 	),
2199		/// 	Some(ControlFlow::Continue(10))
2200		/// );
2201		/// assert_eq!(
2202		/// 	explicit::traverse::<RcFnBrand, ControlFlowBreakAppliedBrand<i32>, _, _, OptionBrand, _, _>(
2203		/// 		|x: i32| Some(x * 2),
2204		/// 		ControlFlow::Break(1)
2205		/// 	),
2206		/// 	Some(ControlFlow::Break(1))
2207		/// );
2208		/// ```
2209		fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
2210			func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
2211			ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2212		) -> 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>)>)
2213		where
2214			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
2215			match ta {
2216				ControlFlow::Continue(e) => F::map(|b| ControlFlow::Continue(b), func(e)),
2217				ControlFlow::Break(t) => F::pure(ControlFlow::Break(t)),
2218			}
2219		}
2220
2221		/// Sequences a control flow of applicative (over continue).
2222		///
2223		/// This method evaluates the computation inside the control flow and accumulates the result into an applicative context (over continue).
2224		#[document_signature]
2225		///
2226		#[document_type_parameters(
2227			"The lifetime of the values.",
2228			"The type of the elements in the traversable structure.",
2229			"The applicative context."
2230		)]
2231		///
2232		#[document_parameters("The control flow containing the applicative value.")]
2233		///
2234		#[document_returns("The control flow wrapped in the applicative context.")]
2235		///
2236		#[document_examples]
2237		///
2238		/// ```
2239		/// use {
2240		/// 	core::ops::ControlFlow,
2241		/// 	fp_library::{
2242		/// 		brands::*,
2243		/// 		functions::*,
2244		/// 	},
2245		/// };
2246		///
2247		/// assert_eq!(
2248		/// 	sequence::<ControlFlowBreakAppliedBrand<()>, _, OptionBrand>(ControlFlow::Continue(Some(
2249		/// 		5
2250		/// 	))),
2251		/// 	Some(ControlFlow::Continue(5))
2252		/// );
2253		/// assert_eq!(
2254		/// 	sequence::<ControlFlowBreakAppliedBrand<i32>, i32, OptionBrand>(ControlFlow::<
2255		/// 		i32,
2256		/// 		Option<i32>,
2257		/// 	>::Break(1)),
2258		/// 	Some(ControlFlow::<i32, i32>::Break(1))
2259		/// );
2260		/// ```
2261		fn sequence<'a, A: 'a + Clone, F: Applicative>(
2262			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>)>)
2263		) -> 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>)>)
2264		where
2265			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
2266			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
2267			match ta {
2268				ControlFlow::Continue(fe) => F::map(|e| ControlFlow::Continue(e), fe),
2269				ControlFlow::Break(t) => F::pure(ControlFlow::Break(t)),
2270			}
2271		}
2272	}
2273
2274	/// [`MonadRec`] implementation for [`ControlFlowContinueAppliedBrand`].
2275	#[document_type_parameters("The continue type.")]
2276	impl<ContinueType: Clone + 'static> MonadRec for ControlFlowContinueAppliedBrand<ContinueType> {
2277		/// Performs tail-recursive monadic computation over [`ControlFlow`] (break channel).
2278		///
2279		/// Iteratively applies the step function. If the function returns [`ControlFlow::Continue`],
2280		/// the computation short-circuits with that continue value. If it returns
2281		/// `Break(ControlFlow::Continue(a))`, the loop continues with the new state. If it returns
2282		/// `Break(ControlFlow::Break(b))`, the computation completes with `Break(b)`.
2283		#[document_signature]
2284		///
2285		#[document_type_parameters(
2286			"The lifetime of the computation.",
2287			"The type of the initial value and loop state.",
2288			"The type of the result."
2289		)]
2290		///
2291		#[document_parameters("The step function.", "The initial value.")]
2292		///
2293		#[document_returns(
2294			"The result of the computation, or a continue if the step function returned `Continue`."
2295		)]
2296		///
2297		#[document_examples]
2298		///
2299		/// ```
2300		/// use {
2301		/// 	core::ops::ControlFlow,
2302		/// 	fp_library::{
2303		/// 		brands::*,
2304		/// 		functions::*,
2305		/// 	},
2306		/// };
2307		///
2308		/// let result = tail_rec_m::<ControlFlowContinueAppliedBrand<&str>, _, _>(
2309		/// 	|n| {
2310		/// 		if n < 10 {
2311		/// 			ControlFlow::Break(ControlFlow::Continue(n + 1))
2312		/// 		} else {
2313		/// 			ControlFlow::Break(ControlFlow::Break(n))
2314		/// 		}
2315		/// 	},
2316		/// 	0,
2317		/// );
2318		/// assert_eq!(result, ControlFlow::Break(10));
2319		/// ```
2320		fn tail_rec_m<'a, A: 'a, B: 'a>(
2321			func: impl Fn(
2322				A,
2323			)
2324				-> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
2325			+ 'a,
2326			initial: A,
2327		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2328			let mut current = initial;
2329			loop {
2330				match func(current) {
2331					ControlFlow::Continue(l) => return ControlFlow::Continue(l),
2332					ControlFlow::Break(ControlFlow::Continue(next)) => current = next,
2333					ControlFlow::Break(ControlFlow::Break(b)) => return ControlFlow::Break(b),
2334				}
2335			}
2336		}
2337	}
2338
2339	/// [`MonadRec`] implementation for [`ControlFlowBreakAppliedBrand`].
2340	#[document_type_parameters("The break type.")]
2341	impl<BreakType: Clone + 'static> MonadRec for ControlFlowBreakAppliedBrand<BreakType> {
2342		/// Performs tail-recursive monadic computation over [`ControlFlow`] (continue channel).
2343		///
2344		/// Iteratively applies the step function. If the function returns [`ControlFlow::Break`],
2345		/// the computation short-circuits with that break value. If it returns
2346		/// `Continue(ControlFlow::Continue(a))`, the loop continues with the new state. If it returns
2347		/// `Continue(ControlFlow::Break(b))`, the computation completes with `Continue(b)`.
2348		#[document_signature]
2349		///
2350		#[document_type_parameters(
2351			"The lifetime of the computation.",
2352			"The type of the initial value and loop state.",
2353			"The type of the result."
2354		)]
2355		///
2356		#[document_parameters("The step function.", "The initial value.")]
2357		///
2358		#[document_returns(
2359			"The result of the computation, or a break if the step function returned `Break`."
2360		)]
2361		///
2362		#[document_examples]
2363		///
2364		/// ```
2365		/// use {
2366		/// 	core::ops::ControlFlow,
2367		/// 	fp_library::{
2368		/// 		brands::*,
2369		/// 		functions::*,
2370		/// 	},
2371		/// };
2372		///
2373		/// let result = tail_rec_m::<ControlFlowBreakAppliedBrand<&str>, _, _>(
2374		/// 	|n| {
2375		/// 		if n < 10 {
2376		/// 			ControlFlow::Continue(ControlFlow::Continue(n + 1))
2377		/// 		} else {
2378		/// 			ControlFlow::Continue(ControlFlow::Break(n))
2379		/// 		}
2380		/// 	},
2381		/// 	0,
2382		/// );
2383		/// assert_eq!(result, ControlFlow::Continue(10));
2384		/// ```
2385		fn tail_rec_m<'a, A: 'a, B: 'a>(
2386			func: impl Fn(
2387				A,
2388			)
2389				-> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
2390			+ 'a,
2391			initial: A,
2392		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2393			let mut current = initial;
2394			loop {
2395				match func(current) {
2396					ControlFlow::Break(d) => return ControlFlow::Break(d),
2397					ControlFlow::Continue(ControlFlow::Continue(next)) => current = next,
2398					ControlFlow::Continue(ControlFlow::Break(b)) =>
2399						return ControlFlow::Continue(b),
2400				}
2401			}
2402		}
2403	}
2404}
2405
2406#[cfg(test)]
2407mod tests {
2408	use {
2409		crate::{
2410			brands::*,
2411			functions::*,
2412		},
2413		core::ops::ControlFlow,
2414		quickcheck::{
2415			Arbitrary,
2416			Gen,
2417		},
2418		quickcheck_macros::quickcheck,
2419	};
2420
2421	impl<B: Arbitrary, C: Arbitrary> Arbitrary for ControlFlowWrapper<B, C> {
2422		fn arbitrary(g: &mut Gen) -> Self {
2423			if bool::arbitrary(g) {
2424				ControlFlowWrapper(ControlFlow::Continue(C::arbitrary(g)))
2425			} else {
2426				ControlFlowWrapper(ControlFlow::Break(B::arbitrary(g)))
2427			}
2428		}
2429	}
2430
2431	/// Newtype wrapper for `ControlFlow` to implement `Arbitrary`.
2432	#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2433	struct ControlFlowWrapper<B, C>(ControlFlow<B, C>);
2434
2435	impl<B, C> ControlFlowWrapper<B, C> {
2436		fn into_inner(self) -> ControlFlow<B, C> {
2437			self.0
2438		}
2439	}
2440
2441	/// Tests the `is_continue` method.
2442	///
2443	/// Verifies that `is_continue` returns true for `Continue` variants and false for `Break` variants.
2444	#[test]
2445	fn test_is_continue() {
2446		let cf: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2447		assert!(ControlFlowBrand::is_continue(&cf));
2448		assert!(!ControlFlowBrand::is_break(&cf));
2449	}
2450
2451	/// Tests the `is_break` method.
2452	///
2453	/// Verifies that `is_break` returns true for `Break` variants and false for `Continue` variants.
2454	#[test]
2455	fn test_is_break() {
2456		let cf: ControlFlow<i32, i32> = ControlFlow::Break(1);
2457		assert!(ControlFlowBrand::is_break(&cf));
2458		assert!(!ControlFlowBrand::is_continue(&cf));
2459	}
2460
2461	/// Tests the `map_continue` method.
2462	///
2463	/// Verifies that `map_continue` transforms the value inside a `Continue` variant and leaves a `Break` variant unchanged.
2464	#[test]
2465	fn test_map_continue() {
2466		let cf: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2467		let mapped = ControlFlowBrand::map_continue(cf, |x| x + 1);
2468		assert_eq!(mapped, ControlFlow::Continue(2));
2469
2470		let brk: ControlFlow<i32, i32> = ControlFlow::Break(1);
2471		let mapped_brk = ControlFlowBrand::map_continue(brk, |x| x + 1);
2472		assert_eq!(mapped_brk, ControlFlow::Break(1));
2473	}
2474
2475	/// Tests the `map_break` method.
2476	///
2477	/// Verifies that `map_break` transforms the value inside a `Break` variant and leaves a `Continue` variant unchanged.
2478	#[test]
2479	fn test_map_break() {
2480		let cf: ControlFlow<i32, i32> = ControlFlow::Break(1);
2481		let mapped = ControlFlowBrand::map_break(cf, |x| x + 1);
2482		assert_eq!(mapped, ControlFlow::Break(2));
2483
2484		let cont: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2485		let mapped_cont = ControlFlowBrand::map_break(cont, |x| x + 1);
2486		assert_eq!(mapped_cont, ControlFlow::Continue(1));
2487	}
2488
2489	/// Tests the `bimap` method.
2490	///
2491	/// Verifies that `bimap` transforms the value inside both `Continue` and `Break` variants using the appropriate function.
2492	#[test]
2493	fn test_bimap() {
2494		let cf: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2495		let mapped = ControlFlowBrand::bimap(cf, |x| x + 1, |x| x * 2);
2496		assert_eq!(mapped, ControlFlow::Continue(2));
2497
2498		let brk: ControlFlow<i32, i32> = ControlFlow::Break(1);
2499		let mapped_brk = ControlFlowBrand::bimap(brk, |x| x + 1, |x| x * 2);
2500		assert_eq!(mapped_brk, ControlFlow::Break(2));
2501	}
2502
2503	/// Tests `Functor` implementation for `ControlFlowContinueAppliedBrand`.
2504	#[test]
2505	fn test_functor_with_continue() {
2506		let cf: ControlFlow<i32, i32> = ControlFlow::Break(5);
2507		assert_eq!(
2508			explicit::map::<ControlFlowContinueAppliedBrand<_>, _, _, _, _>(|x: i32| x * 2, cf),
2509			ControlFlow::Break(10)
2510		);
2511
2512		let cont: ControlFlow<i32, i32> = ControlFlow::Continue(5);
2513		assert_eq!(
2514			explicit::map::<ControlFlowContinueAppliedBrand<_>, _, _, _, _>(|x: i32| x * 2, cont),
2515			ControlFlow::Continue(5)
2516		);
2517	}
2518
2519	/// Tests `Functor` implementation for `ControlFlowBreakAppliedBrand`.
2520	#[test]
2521	fn test_functor_with_break() {
2522		let cf: ControlFlow<i32, i32> = ControlFlow::Continue(5);
2523		assert_eq!(
2524			explicit::map::<ControlFlowBreakAppliedBrand<_>, _, _, _, _>(|x: i32| x * 2, cf),
2525			ControlFlow::Continue(10)
2526		);
2527
2528		let brk: ControlFlow<i32, i32> = ControlFlow::Break(5);
2529		assert_eq!(
2530			explicit::map::<ControlFlowBreakAppliedBrand<_>, _, _, _, _>(|x: i32| x * 2, brk),
2531			ControlFlow::Break(5)
2532		);
2533	}
2534
2535	/// Tests `Bifunctor` implementation for `ControlFlowBrand`.
2536	#[test]
2537	fn test_bifunctor() {
2538		let cf: ControlFlow<i32, i32> = ControlFlow::Continue(5);
2539		assert_eq!(
2540			explicit::bimap::<ControlFlowBrand, _, _, _, _, _, _>((|c| c + 1, |b| b * 2), cf),
2541			ControlFlow::Continue(6)
2542		);
2543
2544		let brk: ControlFlow<i32, i32> = ControlFlow::Break(5);
2545		assert_eq!(
2546			explicit::bimap::<ControlFlowBrand, _, _, _, _, _, _>((|c| c + 1, |b| b * 2), brk),
2547			ControlFlow::Break(10)
2548		);
2549	}
2550
2551	// Functor Laws for ControlFlowContinueAppliedBrand
2552
2553	#[quickcheck]
2554	fn functor_identity_with_continue(x: ControlFlowWrapper<i32, i32>) -> bool {
2555		let x = x.into_inner();
2556		explicit::map::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(identity, x) == x
2557	}
2558
2559	#[quickcheck]
2560	fn functor_composition_with_continue(x: ControlFlowWrapper<i32, i32>) -> bool {
2561		let x = x.into_inner();
2562		let f = |x: i32| x.wrapping_add(1);
2563		let g = |x: i32| x.wrapping_mul(2);
2564		explicit::map::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(compose(f, g), x)
2565			== explicit::map::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(
2566				f,
2567				explicit::map::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(g, x),
2568			)
2569	}
2570
2571	// Functor Laws for ControlFlowBreakAppliedBrand
2572
2573	#[quickcheck]
2574	fn functor_identity_with_break(x: ControlFlowWrapper<i32, i32>) -> bool {
2575		let x = x.into_inner();
2576		explicit::map::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(identity, x) == x
2577	}
2578
2579	#[quickcheck]
2580	fn functor_composition_with_break(x: ControlFlowWrapper<i32, i32>) -> bool {
2581		let x = x.into_inner();
2582		let f = |x: i32| x.wrapping_add(1);
2583		let g = |x: i32| x.wrapping_mul(2);
2584		explicit::map::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(compose(f, g), x)
2585			== explicit::map::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
2586				f,
2587				explicit::map::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(g, x),
2588			)
2589	}
2590
2591	// Bifunctor Laws for ControlFlowBrand
2592
2593	#[quickcheck]
2594	fn bifunctor_identity(x: ControlFlowWrapper<i32, i32>) -> bool {
2595		let x = x.into_inner();
2596		explicit::bimap::<ControlFlowBrand, _, _, _, _, _, _>((identity, identity), x) == x
2597	}
2598
2599	#[quickcheck]
2600	fn bifunctor_composition(x: ControlFlowWrapper<i32, i32>) -> bool {
2601		let x = x.into_inner();
2602		let f = |x: i32| x.wrapping_add(1);
2603		let g = |x: i32| x.wrapping_mul(2);
2604		let h = |x: i32| x.wrapping_sub(1);
2605		let i = |x: i32| if x == 0 { 0 } else { x.wrapping_div(2) };
2606
2607		explicit::bimap::<ControlFlowBrand, _, _, _, _, _, _>((compose(f, g), compose(h, i)), x)
2608			== explicit::bimap::<ControlFlowBrand, _, _, _, _, _, _>(
2609				(f, h),
2610				explicit::bimap::<ControlFlowBrand, _, _, _, _, _, _>((g, i), x),
2611			)
2612	}
2613
2614	// Lift Tests
2615
2616	/// Tests the `lift2` function for `ControlFlowContinueAppliedBrand`.
2617	///
2618	/// Verifies that `lift2` correctly combines two `ControlFlow` values using a binary function,
2619	/// handling `Break` and `Continue` variants according to the `Lift` implementation.
2620	#[test]
2621	fn test_lift2_with_continue() {
2622		let s1: ControlFlow<i32, i32> = ControlFlow::Break(1);
2623		let s2: ControlFlow<i32, i32> = ControlFlow::Break(2);
2624		let s3: ControlFlow<i32, i32> = ControlFlow::Continue(3);
2625
2626		assert_eq!(
2627			explicit::lift2::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _, _, _>(
2628				|x, y| x + y,
2629				s1,
2630				s2
2631			),
2632			ControlFlow::Break(3)
2633		);
2634		assert_eq!(
2635			explicit::lift2::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _, _, _>(
2636				|x, y| x + y,
2637				s1,
2638				s3
2639			),
2640			ControlFlow::Continue(3)
2641		);
2642	}
2643
2644	/// Tests the `lift2` function for `ControlFlowBreakAppliedBrand`.
2645	///
2646	/// Verifies that `lift2` correctly combines two `ControlFlow` values using a binary function,
2647	/// handling `Break` and `Continue` variants according to the `Lift` implementation.
2648	#[test]
2649	fn test_lift2_with_break() {
2650		let s1: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2651		let s2: ControlFlow<i32, i32> = ControlFlow::Continue(2);
2652		let s3: ControlFlow<i32, i32> = ControlFlow::Break(3);
2653
2654		assert_eq!(
2655			explicit::lift2::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _, _, _>(
2656				|x, y| x + y,
2657				s1,
2658				s2
2659			),
2660			ControlFlow::Continue(3)
2661		);
2662		assert_eq!(
2663			explicit::lift2::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _, _, _>(
2664				|x, y| x + y,
2665				s1,
2666				s3
2667			),
2668			ControlFlow::Break(3)
2669		);
2670	}
2671
2672	// Pointed Tests
2673
2674	/// Tests the `pure` function for `ControlFlowContinueAppliedBrand`.
2675	///
2676	/// Verifies that `pure` wraps a value into a `ControlFlow::Break` variant.
2677	#[test]
2678	fn test_pointed_with_continue() {
2679		assert_eq!(
2680			pure::<ControlFlowContinueAppliedBrand<()>, _>(5),
2681			ControlFlow::<_, ()>::Break(5)
2682		);
2683	}
2684
2685	/// Tests the `pure` function for `ControlFlowBreakAppliedBrand`.
2686	///
2687	/// Verifies that `pure` wraps a value into a `ControlFlow::Continue` variant.
2688	#[test]
2689	fn test_pointed_with_break() {
2690		assert_eq!(
2691			pure::<ControlFlowBreakAppliedBrand<()>, _>(5),
2692			ControlFlow::<(), _>::Continue(5)
2693		);
2694	}
2695
2696	// Semiapplicative Tests
2697
2698	/// Tests the `apply` function for `ControlFlowContinueAppliedBrand`.
2699	///
2700	/// Verifies that `apply` correctly applies a wrapped function to a wrapped value,
2701	/// handling `Break` and `Continue` variants.
2702	#[test]
2703	fn test_apply_with_continue() {
2704		let f = pure::<ControlFlowContinueAppliedBrand<()>, _>(lift_fn_new::<RcFnBrand, _, _>(
2705			|x: i32| x * 2,
2706		));
2707		let x = pure::<ControlFlowContinueAppliedBrand<()>, _>(5);
2708		assert_eq!(
2709			apply::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _>(f, x),
2710			ControlFlow::Break(10)
2711		);
2712
2713		let cont: ControlFlow<_, i32> = ControlFlow::Continue(1);
2714		let f_cont = pure::<ControlFlowContinueAppliedBrand<i32>, _>(
2715			lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2),
2716		);
2717		assert_eq!(
2718			apply::<RcFnBrand, ControlFlowContinueAppliedBrand<i32>, _, _>(f_cont, cont),
2719			ControlFlow::Continue(1)
2720		);
2721	}
2722
2723	/// Tests the `apply` function for `ControlFlowBreakAppliedBrand`.
2724	///
2725	/// Verifies that `apply` correctly applies a wrapped function to a wrapped value,
2726	/// handling `Break` and `Continue` variants.
2727	#[test]
2728	fn test_apply_with_break() {
2729		let f = pure::<ControlFlowBreakAppliedBrand<()>, _>(lift_fn_new::<RcFnBrand, _, _>(
2730			|x: i32| x * 2,
2731		));
2732		let x = pure::<ControlFlowBreakAppliedBrand<()>, _>(5);
2733		assert_eq!(
2734			apply::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _>(f, x),
2735			ControlFlow::Continue(10)
2736		);
2737
2738		let brk: ControlFlow<i32, _> = ControlFlow::Break(1);
2739		let f_brk = pure::<ControlFlowBreakAppliedBrand<i32>, _>(lift_fn_new::<RcFnBrand, _, _>(
2740			|x: i32| x * 2,
2741		));
2742		assert_eq!(
2743			apply::<RcFnBrand, ControlFlowBreakAppliedBrand<i32>, _, _>(f_brk, brk),
2744			ControlFlow::Break(1)
2745		);
2746	}
2747
2748	// Semimonad Tests
2749
2750	/// Tests the `bind` function for `ControlFlowContinueAppliedBrand`.
2751	///
2752	/// Verifies that `bind` correctly chains computations, handling `Break` and `Continue` variants.
2753	#[test]
2754	fn test_bind_with_continue() {
2755		let x = pure::<ControlFlowContinueAppliedBrand<()>, _>(5);
2756		assert_eq!(
2757			explicit::bind::<ControlFlowContinueAppliedBrand<()>, _, _, _, _>(x, |i| pure::<
2758				ControlFlowContinueAppliedBrand<()>,
2759				_,
2760			>(i * 2)),
2761			ControlFlow::Break(10)
2762		);
2763
2764		let cont: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2765		assert_eq!(
2766			explicit::bind::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(cont, |i| pure::<
2767				ControlFlowContinueAppliedBrand<i32>,
2768				_,
2769			>(
2770				i * 2
2771			)),
2772			ControlFlow::Continue(1)
2773		);
2774	}
2775
2776	/// Tests the `bind` function for `ControlFlowBreakAppliedBrand`.
2777	///
2778	/// Verifies that `bind` correctly chains computations, handling `Break` and `Continue` variants.
2779	#[test]
2780	fn test_bind_with_break() {
2781		let x = pure::<ControlFlowBreakAppliedBrand<()>, _>(5);
2782		assert_eq!(
2783			explicit::bind::<ControlFlowBreakAppliedBrand<()>, _, _, _, _>(x, |i| pure::<
2784				ControlFlowBreakAppliedBrand<()>,
2785				_,
2786			>(i * 2)),
2787			ControlFlow::Continue(10)
2788		);
2789
2790		let brk: ControlFlow<i32, i32> = ControlFlow::Break(1);
2791		assert_eq!(
2792			explicit::bind::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(brk, |i| pure::<
2793				ControlFlowBreakAppliedBrand<i32>,
2794				_,
2795			>(i * 2)),
2796			ControlFlow::Break(1)
2797		);
2798	}
2799
2800	// Foldable Tests
2801
2802	/// Tests `Foldable` methods for `ControlFlowContinueAppliedBrand`.
2803	///
2804	/// Verifies `fold_right`, `fold_left`, and `fold_map` behavior for `Break` and `Continue` variants.
2805	#[test]
2806	fn test_foldable_with_continue() {
2807		let x = pure::<ControlFlowContinueAppliedBrand<()>, _>(5);
2808		assert_eq!(
2809			explicit::fold_right::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _, _, _>(
2810				|a, b| a + b,
2811				10,
2812				x
2813			),
2814			15
2815		);
2816		assert_eq!(
2817			explicit::fold_left::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _, _, _>(
2818				|b, a| b + a,
2819				10,
2820				x
2821			),
2822			15
2823		);
2824		assert_eq!(
2825			explicit::fold_map::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _, _, _>(
2826				|a: i32| a.to_string(),
2827				x
2828			),
2829			"5"
2830		);
2831
2832		let cont: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2833		assert_eq!(
2834			explicit::fold_right::<RcFnBrand, ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(
2835				|a, b| a + b,
2836				10,
2837				cont
2838			),
2839			10
2840		);
2841	}
2842
2843	/// Tests `Foldable` methods for `ControlFlowBreakAppliedBrand`.
2844	///
2845	/// Verifies `fold_right`, `fold_left`, and `fold_map` behavior for `Break` and `Continue` variants.
2846	#[test]
2847	fn test_foldable_with_break() {
2848		let x = pure::<ControlFlowBreakAppliedBrand<()>, _>(5);
2849		assert_eq!(
2850			explicit::fold_right::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _, _, _>(
2851				|a, b| a + b,
2852				10,
2853				x
2854			),
2855			15
2856		);
2857		assert_eq!(
2858			explicit::fold_left::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _, _, _>(
2859				|b, a| b + a,
2860				10,
2861				x
2862			),
2863			15
2864		);
2865		assert_eq!(
2866			explicit::fold_map::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _, _, _>(
2867				|a: i32| a.to_string(),
2868				x
2869			),
2870			"5"
2871		);
2872
2873		let brk: ControlFlow<i32, i32> = ControlFlow::Break(1);
2874		assert_eq!(
2875			explicit::fold_right::<RcFnBrand, ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
2876				|a, b| a + b,
2877				10,
2878				brk
2879			),
2880			10
2881		);
2882	}
2883
2884	// Traversable Tests
2885
2886	/// Tests the `traverse` function for `ControlFlowContinueAppliedBrand`.
2887	///
2888	/// Verifies that `traverse` correctly maps and sequences effects over `ControlFlow`.
2889	#[test]
2890	fn test_traversable_with_continue() {
2891		let x = pure::<ControlFlowContinueAppliedBrand<()>, _>(5);
2892		assert_eq!(
2893			explicit::traverse::<
2894				RcFnBrand,
2895				ControlFlowContinueAppliedBrand<()>,
2896				_,
2897				_,
2898				OptionBrand,
2899				_,
2900				_,
2901			>(|a| Some(a * 2), x),
2902			Some(ControlFlow::Break(10))
2903		);
2904
2905		let cont: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2906		assert_eq!(
2907			explicit::traverse::<
2908				RcFnBrand,
2909				ControlFlowContinueAppliedBrand<i32>,
2910				_,
2911				_,
2912				OptionBrand,
2913				_,
2914				_,
2915			>(|a| Some(a * 2), cont),
2916			Some(ControlFlow::Continue(1))
2917		);
2918	}
2919
2920	/// Tests the `traverse` function for `ControlFlowBreakAppliedBrand`.
2921	///
2922	/// Verifies that `traverse` correctly maps and sequences effects over `ControlFlow`.
2923	#[test]
2924	fn test_traversable_with_break() {
2925		let x = pure::<ControlFlowBreakAppliedBrand<()>, _>(5);
2926		assert_eq!(
2927			explicit::traverse::<
2928				RcFnBrand,
2929				ControlFlowBreakAppliedBrand<()>,
2930				_,
2931				_,
2932				OptionBrand,
2933				_,
2934				_,
2935			>(|a| Some(a * 2), x),
2936			Some(ControlFlow::Continue(10))
2937		);
2938
2939		let brk: ControlFlow<i32, i32> = ControlFlow::Break(1);
2940		assert_eq!(
2941			explicit::traverse::<
2942				RcFnBrand,
2943				ControlFlowBreakAppliedBrand<i32>,
2944				_,
2945				_,
2946				OptionBrand,
2947				_,
2948				_,
2949			>(|a| Some(a * 2), brk),
2950			Some(ControlFlow::Break(1))
2951		);
2952	}
2953
2954	// Monad Laws for ControlFlowContinueAppliedBrand
2955
2956	/// Verifies the Left Identity law for `ControlFlowContinueAppliedBrand` Monad.
2957	#[quickcheck]
2958	fn monad_left_identity_with_continue(a: i32) -> bool {
2959		let f = |x: i32| pure::<ControlFlowContinueAppliedBrand<i32>, _>(x.wrapping_mul(2));
2960		explicit::bind::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(
2961			pure::<ControlFlowContinueAppliedBrand<i32>, _>(a),
2962			f,
2963		) == f(a)
2964	}
2965
2966	/// Verifies the Right Identity law for `ControlFlowContinueAppliedBrand` Monad.
2967	#[quickcheck]
2968	fn monad_right_identity_with_continue(x: ControlFlowWrapper<i32, i32>) -> bool {
2969		let x = x.into_inner();
2970		explicit::bind::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(
2971			x,
2972			pure::<ControlFlowContinueAppliedBrand<i32>, _>,
2973		) == x
2974	}
2975
2976	/// Verifies the Associativity law for `ControlFlowContinueAppliedBrand` Monad.
2977	#[quickcheck]
2978	fn monad_associativity_with_continue(x: ControlFlowWrapper<i32, i32>) -> bool {
2979		let x = x.into_inner();
2980		let f = |x: i32| pure::<ControlFlowContinueAppliedBrand<i32>, _>(x.wrapping_mul(2));
2981		let g = |x: i32| pure::<ControlFlowContinueAppliedBrand<i32>, _>(x.wrapping_add(1));
2982		explicit::bind::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(
2983			explicit::bind::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(x, f),
2984			g,
2985		) == explicit::bind::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(x, |a| {
2986			explicit::bind::<ControlFlowContinueAppliedBrand<i32>, _, _, _, _>(f(a), g)
2987		})
2988	}
2989
2990	// Monad Laws for ControlFlowBreakAppliedBrand
2991
2992	/// Verifies the Left Identity law for `ControlFlowBreakAppliedBrand` Monad.
2993	#[quickcheck]
2994	fn monad_left_identity_with_break(a: i32) -> bool {
2995		let f = |x: i32| pure::<ControlFlowBreakAppliedBrand<i32>, _>(x.wrapping_mul(2));
2996		explicit::bind::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
2997			pure::<ControlFlowBreakAppliedBrand<i32>, _>(a),
2998			f,
2999		) == f(a)
3000	}
3001
3002	/// Verifies the Right Identity law for `ControlFlowBreakAppliedBrand` Monad.
3003	#[quickcheck]
3004	fn monad_right_identity_with_break(x: ControlFlowWrapper<i32, i32>) -> bool {
3005		let x = x.into_inner();
3006		explicit::bind::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
3007			x,
3008			pure::<ControlFlowBreakAppliedBrand<i32>, _>,
3009		) == x
3010	}
3011
3012	/// Verifies the Associativity law for `ControlFlowBreakAppliedBrand` Monad.
3013	#[quickcheck]
3014	fn monad_associativity_with_break(x: ControlFlowWrapper<i32, i32>) -> bool {
3015		let x = x.into_inner();
3016		let f = |x: i32| pure::<ControlFlowBreakAppliedBrand<i32>, _>(x.wrapping_mul(2));
3017		let g = |x: i32| pure::<ControlFlowBreakAppliedBrand<i32>, _>(x.wrapping_add(1));
3018		explicit::bind::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(
3019			explicit::bind::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(x, f),
3020			g,
3021		) == explicit::bind::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(x, |a| {
3022			explicit::bind::<ControlFlowBreakAppliedBrand<i32>, _, _, _, _>(f(a), g)
3023		})
3024	}
3025
3026	// Applicative and Monad marker trait verification
3027
3028	/// Verifies that `ControlFlowContinueAppliedBrand` satisfies the `Applicative` trait.
3029	#[test]
3030	fn test_applicative_continue_applied() {
3031		fn assert_applicative<B: crate::classes::Applicative>() {}
3032		assert_applicative::<ControlFlowContinueAppliedBrand<i32>>();
3033	}
3034
3035	/// Verifies that `ControlFlowBreakAppliedBrand` satisfies the `Applicative` trait.
3036	#[test]
3037	fn test_applicative_break_applied() {
3038		fn assert_applicative<B: crate::classes::Applicative>() {}
3039		assert_applicative::<ControlFlowBreakAppliedBrand<i32>>();
3040	}
3041
3042	/// Verifies that `ControlFlowContinueAppliedBrand` satisfies the `Monad` trait.
3043	#[test]
3044	fn test_monad_continue_applied() {
3045		fn assert_monad<B: crate::classes::Monad>() {}
3046		assert_monad::<ControlFlowContinueAppliedBrand<i32>>();
3047	}
3048
3049	/// Verifies that `ControlFlowBreakAppliedBrand` satisfies the `Monad` trait.
3050	#[test]
3051	fn test_monad_break_applied() {
3052		fn assert_monad<B: crate::classes::Monad>() {}
3053		assert_monad::<ControlFlowBreakAppliedBrand<i32>>();
3054	}
3055
3056	// MonadRec tests for ControlFlowContinueAppliedBrand
3057
3058	/// Tests the MonadRec identity law for `ControlFlowContinueAppliedBrand`:
3059	/// `tail_rec_m(|a| Break(Break(a)), x) == Break(x)`.
3060	#[quickcheck]
3061	fn monad_rec_continue_applied_identity(x: i32) -> bool {
3062		tail_rec_m::<ControlFlowContinueAppliedBrand<()>, _, _>(
3063			|a| ControlFlow::Break(ControlFlow::Break(a)),
3064			x,
3065		) == ControlFlow::Break(x)
3066	}
3067
3068	/// Tests a recursive computation that sums a range via `tail_rec_m`
3069	/// on the break channel of `ControlFlowContinueAppliedBrand`.
3070	#[test]
3071	fn monad_rec_continue_applied_sum_range() {
3072		let result = tail_rec_m::<ControlFlowContinueAppliedBrand<&str>, _, _>(
3073			|(n, acc)| {
3074				if n == 0 {
3075					ControlFlow::Break(ControlFlow::Break(acc))
3076				} else {
3077					ControlFlow::Break(ControlFlow::Continue((n - 1, acc + n)))
3078				}
3079			},
3080			(100i64, 0i64),
3081		);
3082		assert_eq!(result, ControlFlow::Break(5050));
3083	}
3084
3085	/// Tests that `tail_rec_m` short-circuits on `Continue` for `ControlFlowContinueAppliedBrand`.
3086	#[test]
3087	fn monad_rec_continue_applied_short_circuit() {
3088		let result = tail_rec_m::<ControlFlowContinueAppliedBrand<&str>, _, _>(
3089			|n| {
3090				if n == 5 {
3091					ControlFlow::Continue("stopped")
3092				} else {
3093					ControlFlow::Break(ControlFlow::Continue(n + 1))
3094				}
3095			},
3096			0,
3097		);
3098		assert_eq!(result, ControlFlow::<i32, &str>::Continue("stopped"));
3099	}
3100
3101	/// Tests stack safety: `tail_rec_m` handles large iteration counts
3102	/// for `ControlFlowContinueAppliedBrand`.
3103	#[test]
3104	fn monad_rec_continue_applied_stack_safety() {
3105		let iterations: i64 = 200_000;
3106		let result = tail_rec_m::<ControlFlowContinueAppliedBrand<()>, _, _>(
3107			|acc| {
3108				if acc < iterations {
3109					ControlFlow::Break(ControlFlow::Continue(acc + 1))
3110				} else {
3111					ControlFlow::Break(ControlFlow::Break(acc))
3112				}
3113			},
3114			0i64,
3115		);
3116		assert_eq!(result, ControlFlow::Break(iterations));
3117	}
3118
3119	// MonadRec tests for ControlFlowBreakAppliedBrand
3120
3121	/// Tests the MonadRec identity law for `ControlFlowBreakAppliedBrand`:
3122	/// `tail_rec_m(|a| Continue(Break(a)), x) == Continue(x)`.
3123	#[quickcheck]
3124	fn monad_rec_break_applied_identity(x: i32) -> bool {
3125		tail_rec_m::<ControlFlowBreakAppliedBrand<()>, _, _>(
3126			|a| ControlFlow::Continue(ControlFlow::Break(a)),
3127			x,
3128		) == ControlFlow::Continue(x)
3129	}
3130
3131	/// Tests a recursive computation that sums a range via `tail_rec_m`
3132	/// on the continue channel of `ControlFlowBreakAppliedBrand`.
3133	#[test]
3134	fn monad_rec_break_applied_sum_range() {
3135		let result = tail_rec_m::<ControlFlowBreakAppliedBrand<&str>, _, _>(
3136			|(n, acc)| {
3137				if n == 0 {
3138					ControlFlow::Continue(ControlFlow::Break(acc))
3139				} else {
3140					ControlFlow::Continue(ControlFlow::Continue((n - 1, acc + n)))
3141				}
3142			},
3143			(100i64, 0i64),
3144		);
3145		assert_eq!(result, ControlFlow::Continue(5050));
3146	}
3147
3148	/// Tests that `tail_rec_m` short-circuits on `Break` for `ControlFlowBreakAppliedBrand`.
3149	#[test]
3150	fn monad_rec_break_applied_short_circuit() {
3151		let result = tail_rec_m::<ControlFlowBreakAppliedBrand<&str>, _, _>(
3152			|n| {
3153				if n == 5 {
3154					ControlFlow::Break("stopped")
3155				} else {
3156					ControlFlow::Continue(ControlFlow::Continue(n + 1))
3157				}
3158			},
3159			0,
3160		);
3161		assert_eq!(result, ControlFlow::<&str, i32>::Break("stopped"));
3162	}
3163
3164	/// Tests stack safety: `tail_rec_m` handles large iteration counts
3165	/// for `ControlFlowBreakAppliedBrand`.
3166	#[test]
3167	fn monad_rec_break_applied_stack_safety() {
3168		let iterations: i64 = 200_000;
3169		let result = tail_rec_m::<ControlFlowBreakAppliedBrand<()>, _, _>(
3170			|acc| {
3171				if acc < iterations {
3172					ControlFlow::Continue(ControlFlow::Continue(acc + 1))
3173				} else {
3174					ControlFlow::Continue(ControlFlow::Break(acc))
3175				}
3176			},
3177			0i64,
3178		);
3179		assert_eq!(result, ControlFlow::Continue(iterations));
3180	}
3181
3182	// MonadRec marker trait verification
3183
3184	/// Verifies that `ControlFlowContinueAppliedBrand` satisfies the `MonadRec` trait.
3185	#[test]
3186	fn test_monad_rec_continue_applied() {
3187		fn assert_monad_rec<B: crate::classes::MonadRec>() {}
3188		assert_monad_rec::<ControlFlowContinueAppliedBrand<i32>>();
3189	}
3190
3191	/// Verifies that `ControlFlowBreakAppliedBrand` satisfies the `MonadRec` trait.
3192	#[test]
3193	fn test_monad_rec_break_applied() {
3194		fn assert_monad_rec<B: crate::classes::MonadRec>() {}
3195		assert_monad_rec::<ControlFlowBreakAppliedBrand<i32>>();
3196	}
3197
3198	/// Tests the `break_val` method.
3199	///
3200	/// Verifies that `break_val` returns `Some(b)` for `Break(b)` and `None` for `Continue(_)`.
3201	#[test]
3202	fn test_break_val() {
3203		let cf: ControlFlow<i32, i32> = ControlFlow::Break(42);
3204		assert_eq!(ControlFlowBrand::break_val(cf), Some(42));
3205
3206		let cf: ControlFlow<i32, i32> = ControlFlow::Continue(1);
3207		assert_eq!(ControlFlowBrand::break_val(cf), None);
3208	}
3209
3210	/// Tests the `continue_val` method.
3211	///
3212	/// Verifies that `continue_val` returns `Some(c)` for `Continue(c)` and `None` for `Break(_)`.
3213	#[test]
3214	fn test_continue_val() {
3215		let cf: ControlFlow<i32, i32> = ControlFlow::Continue(7);
3216		assert_eq!(ControlFlowBrand::continue_val(cf), Some(7));
3217
3218		let cf: ControlFlow<i32, i32> = ControlFlow::Break(42);
3219		assert_eq!(ControlFlowBrand::continue_val(cf), None);
3220	}
3221
3222	/// Tests the `swap` method.
3223	///
3224	/// Verifies that `swap` maps `Continue(c)` to `Break(c)` and `Break(b)` to `Continue(b)`.
3225	#[test]
3226	fn test_swap() {
3227		let cf: ControlFlow<&str, i32> = ControlFlow::Continue(1);
3228		assert_eq!(ControlFlowBrand::swap(cf), ControlFlow::Break(1));
3229
3230		let cf: ControlFlow<&str, i32> = ControlFlow::Break("hello");
3231		assert_eq!(ControlFlowBrand::swap(cf), ControlFlow::Continue("hello"));
3232	}
3233
3234	/// Property test: `break_val` and `continue_val` are complementary.
3235	#[quickcheck]
3236	fn break_and_continue_val_complementary(x: ControlFlowWrapper<i32, i32>) -> bool {
3237		let cf = x.into_inner();
3238		ControlFlowBrand::break_val(cf).is_some() != ControlFlowBrand::continue_val(cf).is_some()
3239	}
3240
3241	/// Property test: swapping twice is identity.
3242	#[quickcheck]
3243	fn swap_involution(x: ControlFlowWrapper<i32, i32>) -> bool {
3244		let cf = x.into_inner();
3245		ControlFlowBrand::swap(ControlFlowBrand::swap(cf)) == cf
3246	}
3247}