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