1#[fp_macros::document_module]
20mod inner {
21 use {
22 crate::{
23 Apply,
24 brands::{
25 StepBrand,
26 StepDoneAppliedBrand,
27 StepLoopAppliedBrand,
28 },
29 classes::{
30 Applicative,
31 ApplyFirst,
32 ApplySecond,
33 Bifunctor,
34 CloneableFn,
35 Foldable,
36 Functor,
37 Lift,
38 Monoid,
39 ParFoldable,
40 Pointed,
41 Semiapplicative,
42 Semimonad,
43 SendCloneableFn,
44 Traversable,
45 },
46 impl_kind,
47 kinds::*,
48 },
49 fp_macros::*,
50 };
51
52 #[document_type_parameters(
67 r#"The "loop" type - when we return `Loop(a)`, we continue with `a`."#,
68 r#"The "done" type - when we return `Done(b)`, we're finished."#
69 )]
70 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
76 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
77 pub enum Step<A, B> {
78 Loop(A),
80 Done(B),
82 }
83
84 #[document_type_parameters(
85 r#"The "loop" type - when we return `Loop(a)`, we continue with `a`."#,
86 r#"The "done" type - when we return `Done(b)`, we're finished."#
87 )]
88 #[document_parameters("The step value.")]
89 impl<A, B> Step<A, B> {
90 #[document_signature]
92 #[document_returns("`true` if the step is a loop, `false` otherwise.")]
94 #[inline]
96 #[document_examples]
97 pub fn is_loop(&self) -> bool {
105 matches!(self, Step::Loop(_))
106 }
107
108 #[document_signature]
110 #[document_returns("`true` if the step is done, `false` otherwise.")]
112 #[inline]
114 #[document_examples]
115 pub fn is_done(&self) -> bool {
123 matches!(self, Step::Done(_))
124 }
125
126 #[document_signature]
128 #[document_type_parameters("The new loop type.")]
130 #[document_parameters("The function to apply to the loop value.")]
132 #[document_returns("A new `Step` with the loop value transformed.")]
134 #[document_examples]
136 pub fn map_loop<C>(
145 self,
146 f: impl FnOnce(A) -> C,
147 ) -> Step<C, B> {
148 match self {
149 Step::Loop(a) => Step::Loop(f(a)),
150 Step::Done(b) => Step::Done(b),
151 }
152 }
153
154 #[document_signature]
156 #[document_type_parameters("The new done type.")]
158 #[document_parameters("The function to apply to the done value.")]
160 #[document_returns("A new `Step` with the done value transformed.")]
162 #[document_examples]
164 pub fn map_done<C>(
173 self,
174 f: impl FnOnce(B) -> C,
175 ) -> Step<A, C> {
176 match self {
177 Step::Loop(a) => Step::Loop(a),
178 Step::Done(b) => Step::Done(f(b)),
179 }
180 }
181
182 #[document_signature]
184 #[document_type_parameters("The new loop type.", "The new done type.")]
186 #[document_parameters(
188 "The function to apply to the loop value.",
189 "The function to apply to the done value."
190 )]
191 #[document_returns("A new `Step` with both values transformed.")]
193 #[document_examples]
194 pub fn bimap<C, D>(
203 self,
204 f: impl FnOnce(A) -> C,
205 g: impl FnOnce(B) -> D,
206 ) -> Step<C, D> {
207 match self {
208 Step::Loop(a) => Step::Loop(f(a)),
209 Step::Done(b) => Step::Done(g(b)),
210 }
211 }
212 }
213
214 impl_kind! {
215 for StepBrand {
216 type Of<A, B> = Step<A, B>;
217 }
218 }
219
220 impl_kind! {
221 for StepBrand {
222 type Of<'a, A: 'a, B: 'a>: 'a = Step<A, B>;
223 }
224 }
225
226 impl Bifunctor for StepBrand {
227 #[document_signature]
231 #[document_type_parameters(
233 "The lifetime of the values.",
234 "The type of the loop value.",
235 "The type of the mapped loop value.",
236 "The type of the done value.",
237 "The type of the mapped done value."
238 )]
239 #[document_parameters(
241 "The function to apply to the loop value.",
242 "The function to apply to the done value.",
243 "The step to map over."
244 )]
245 #[document_returns("A new step containing the mapped values.")]
247 #[document_examples]
248 fn bimap<'a, A: 'a, B: 'a, C: 'a, D: 'a>(
261 f: impl Fn(A) -> B + 'a,
262 g: impl Fn(C) -> D + 'a,
263 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
264 ) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>) {
265 p.bimap(f, g)
266 }
267 }
268
269 impl_kind! {
272 impl<LoopType: 'static> for StepLoopAppliedBrand<LoopType> {
273 type Of<'a, B: 'a>: 'a = Step<LoopType, B>;
274 }
275 }
276
277 #[document_type_parameters("The loop type.")]
278 impl<LoopType: 'static> Functor for StepLoopAppliedBrand<LoopType> {
279 #[document_signature]
283 #[document_type_parameters(
285 "The lifetime of the values.",
286 "The type of the done value.",
287 "The type of the result of applying the function."
288 )]
289 #[document_parameters("The function to apply to the done value.", "The step to map over.")]
291 #[document_returns(
293 "A new step containing the result of applying the function to the done value."
294 )]
295 #[document_examples]
297 fn map<'a, A: 'a, B: 'a>(
311 func: impl Fn(A) -> B + 'a,
312 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
313 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
314 fa.map_done(func)
315 }
316 }
317
318 #[document_type_parameters("The loop type.")]
319 impl<LoopType: Clone + 'static> Lift for StepLoopAppliedBrand<LoopType> {
320 #[document_signature]
324 #[document_type_parameters(
326 "The lifetime of the values.",
327 "The type of the first value.",
328 "The type of the second value.",
329 "The type of the result."
330 )]
331 #[document_parameters(
333 "The binary function to apply.",
334 "The first step.",
335 "The second step."
336 )]
337 #[document_returns(
339 "`Done(f(a, b))` if both steps are `Done`, otherwise the first loop encountered."
340 )]
341 #[document_examples]
342 fn lift2<'a, A, B, C>(
368 func: impl Fn(A, B) -> C + 'a,
369 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
370 fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
371 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
372 where
373 A: Clone + 'a,
374 B: Clone + 'a,
375 C: 'a, {
376 match (fa, fb) {
377 (Step::Done(a), Step::Done(b)) => Step::Done(func(a, b)),
378 (Step::Loop(e), _) => Step::Loop(e),
379 (_, Step::Loop(e)) => Step::Loop(e),
380 }
381 }
382 }
383
384 #[document_type_parameters("The loop type.")]
385 impl<LoopType: 'static> Pointed for StepLoopAppliedBrand<LoopType> {
386 #[document_signature]
390 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
392 #[document_parameters("The value to wrap.")]
394 #[document_returns("`Done(a)`.")]
396 #[document_examples]
398 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
409 Step::Done(a)
410 }
411 }
412
413 #[document_type_parameters("The loop type.")]
414 impl<LoopType: Clone + 'static> ApplyFirst for StepLoopAppliedBrand<LoopType> {}
415
416 #[document_type_parameters("The loop type.")]
417 impl<LoopType: Clone + 'static> ApplySecond for StepLoopAppliedBrand<LoopType> {}
418
419 #[document_type_parameters("The loop type.")]
420 impl<LoopType: Clone + 'static> Semiapplicative for StepLoopAppliedBrand<LoopType> {
421 #[document_signature]
425 #[document_type_parameters(
427 "The lifetime of the values.",
428 "The brand of the cloneable function wrapper.",
429 "The type of the input value.",
430 "The type of the output value."
431 )]
432 #[document_parameters(
434 "The step containing the function.",
435 "The step containing the value."
436 )]
437 #[document_returns(
439 "`Done(f(a))` if both are `Done`, otherwise the first loop encountered."
440 )]
441 #[document_examples]
442 fn apply<'a, FnBrand: 'a + CloneableFn, A: 'a + Clone, B: 'a>(
458 ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneableFn>::Of<'a, A, B>>),
459 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
460 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
461 match (ff, fa) {
462 (Step::Done(f), Step::Done(a)) => Step::Done(f(a)),
463 (Step::Loop(e), _) => Step::Loop(e),
464 (_, Step::Loop(e)) => Step::Loop(e),
465 }
466 }
467 }
468
469 #[document_type_parameters("The loop type.")]
470 impl<LoopType: Clone + 'static> Semimonad for StepLoopAppliedBrand<LoopType> {
471 #[document_signature]
475 #[document_type_parameters(
477 "The lifetime of the values.",
478 "The type of the result of the first computation.",
479 "The type of the result of the second computation."
480 )]
481 #[document_parameters(
483 "The first step.",
484 "The function to apply to the value inside the step."
485 )]
486 #[document_returns(
488 "The result of applying `f` to the value if `ma` is `Done`, otherwise the original loop."
489 )]
490 #[document_examples]
491 fn bind<'a, A: 'a, B: 'a>(
505 ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
506 func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
507 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
508 match ma {
509 Step::Done(a) => func(a),
510 Step::Loop(e) => Step::Loop(e),
511 }
512 }
513 }
514
515 #[document_type_parameters("The loop type.")]
516 impl<LoopType: 'static> Foldable for StepLoopAppliedBrand<LoopType> {
517 #[document_signature]
521 #[document_type_parameters(
523 "The lifetime of the values.",
524 "The brand of the cloneable function to use.",
525 "The type of the elements in the structure.",
526 "The type of the accumulator."
527 )]
528 #[document_parameters("The folding function.", "The initial value.", "The step to fold.")]
530 #[document_returns("`func(a, initial)` if `fa` is `Done(a)`, otherwise `initial`.")]
532 #[document_examples]
534 fn fold_right<'a, FnBrand, A: 'a, B: 'a>(
556 func: impl Fn(A, B) -> B + 'a,
557 initial: B,
558 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
559 ) -> B
560 where
561 FnBrand: CloneableFn + 'a, {
562 match fa {
563 Step::Done(a) => func(a, initial),
564 Step::Loop(_) => initial,
565 }
566 }
567
568 #[document_signature]
572 #[document_type_parameters(
574 "The lifetime of the values.",
575 "The brand of the cloneable function to use.",
576 "The type of the elements in the structure.",
577 "The type of the accumulator."
578 )]
579 #[document_parameters("The folding function.", "The initial value.", "The step to fold.")]
581 #[document_returns("`func(initial, a)` if `fa` is `Done(a)`, otherwise `initial`.")]
583 #[document_examples]
585 fn fold_left<'a, FnBrand, A: 'a, B: 'a>(
607 func: impl Fn(B, A) -> B + 'a,
608 initial: B,
609 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
610 ) -> B
611 where
612 FnBrand: CloneableFn + 'a, {
613 match fa {
614 Step::Done(a) => func(initial, a),
615 Step::Loop(_) => initial,
616 }
617 }
618
619 #[document_signature]
623 #[document_type_parameters(
625 "The lifetime of the values.",
626 "The brand of the cloneable function to use.",
627 "The type of the elements in the structure.",
628 "The type of the monoid."
629 )]
630 #[document_parameters("The mapping function.", "The step to fold.")]
632 #[document_returns("`func(a)` if `fa` is `Done(a)`, otherwise `M::empty()`.")]
634 #[document_examples]
636 fn fold_map<'a, FnBrand, A: 'a, M>(
660 func: impl Fn(A) -> M + 'a,
661 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
662 ) -> M
663 where
664 M: Monoid + 'a,
665 FnBrand: CloneableFn + 'a, {
666 match fa {
667 Step::Done(a) => func(a),
668 Step::Loop(_) => M::empty(),
669 }
670 }
671 }
672
673 #[document_type_parameters("The loop type.")]
674 impl<LoopType: Clone + 'static> Traversable for StepLoopAppliedBrand<LoopType> {
675 #[document_signature]
679 #[document_type_parameters(
681 "The lifetime of the values.",
682 "The type of the elements in the traversable structure.",
683 "The type of the elements in the resulting traversable structure.",
684 "The applicative context."
685 )]
686 #[document_parameters("The function to apply.", "The step to traverse.")]
688 #[document_returns("The step wrapped in the applicative context.")]
690 #[document_examples]
692 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
713 func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
714 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
715 ) -> 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>)>)
716 where
717 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
718 match ta {
719 Step::Done(a) => F::map(|b| Step::Done(b), func(a)),
720 Step::Loop(e) => F::pure(Step::Loop(e)),
721 }
722 }
723
724 #[document_signature]
728 #[document_type_parameters(
730 "The lifetime of the values.",
731 "The type of the elements in the traversable structure.",
732 "The applicative context."
733 )]
734 #[document_parameters("The step containing the applicative value.")]
736 #[document_returns("The step wrapped in the applicative context.")]
738 #[document_examples]
740 fn sequence<'a, A: 'a + Clone, F: Applicative>(
758 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>)>)
759 ) -> 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>)>)
760 where
761 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
762 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
763 match ta {
764 Step::Done(fa) => F::map(|a| Step::Done(a), fa),
765 Step::Loop(e) => F::pure(Step::Loop(e)),
766 }
767 }
768 }
769
770 #[document_type_parameters("The loop type.")]
771 impl<LoopType: 'static> ParFoldable for StepLoopAppliedBrand<LoopType> {
772 #[document_signature]
776 #[document_type_parameters(
778 "The lifetime of the values.",
779 "The brand of the cloneable function wrapper.",
780 "The element type.",
781 "The monoid type."
782 )]
783 #[document_parameters(
785 "The thread-safe function to map each element to a monoid.",
786 "The step to fold."
787 )]
788 #[document_returns("The combined monoid value.")]
790 #[document_examples]
791 fn par_fold_map<'a, FnBrand, A, M>(
814 func: <FnBrand as SendCloneableFn>::SendOf<'a, A, M>,
815 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
816 ) -> M
817 where
818 FnBrand: 'a + SendCloneableFn,
819 A: 'a + Clone + Send + Sync,
820 M: Monoid + Send + Sync + 'a, {
821 match fa {
822 Step::Done(a) => func(a),
823 Step::Loop(_) => M::empty(),
824 }
825 }
826
827 #[document_signature]
831 #[document_type_parameters(
833 "The lifetime of the values.",
834 "The brand of the cloneable function wrapper.",
835 "The element type.",
836 "The accumulator type."
837 )]
838 #[document_parameters(
840 "The thread-safe function to apply to each element and the accumulator.",
841 "The initial value.",
842 "The step to fold."
843 )]
844 #[document_returns("The final accumulator value.")]
846 #[document_examples]
847 fn par_fold_right<'a, FnBrand, A, B>(
864 func: <FnBrand as SendCloneableFn>::SendOf<'a, (A, B), B>,
865 initial: B,
866 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
867 ) -> B
868 where
869 FnBrand: 'a + SendCloneableFn,
870 A: 'a + Clone + Send + Sync,
871 B: Send + Sync + 'a, {
872 match fa {
873 Step::Done(a) => func((a, initial)),
874 Step::Loop(_) => initial,
875 }
876 }
877 }
878
879 impl_kind! {
882 impl<DoneType: 'static> for StepDoneAppliedBrand<DoneType> {
883 type Of<'a, A: 'a>: 'a = Step<A, DoneType>;
884 }
885 }
886
887 #[document_type_parameters("The done type.")]
888 impl<DoneType: 'static> Functor for StepDoneAppliedBrand<DoneType> {
889 #[document_signature]
893 #[document_type_parameters(
895 "The lifetime of the values.",
896 "The type of the loop value.",
897 "The type of the result of applying the function."
898 )]
899 #[document_parameters("The function to apply to the loop value.", "The step to map over.")]
901 #[document_returns(
903 "A new step containing the result of applying the function to the loop value."
904 )]
905 #[document_examples]
907 fn map<'a, A: 'a, B: 'a>(
921 func: impl Fn(A) -> B + 'a,
922 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
923 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
924 fa.map_loop(func)
925 }
926 }
927
928 #[document_type_parameters("The done type.")]
929 impl<DoneType: Clone + 'static> Lift for StepDoneAppliedBrand<DoneType> {
930 #[document_signature]
934 #[document_type_parameters(
936 "The lifetime of the values.",
937 "The type of the first loop value.",
938 "The type of the second loop value.",
939 "The type of the result loop value."
940 )]
941 #[document_parameters(
943 "The binary function to apply to the loops.",
944 "The first step.",
945 "The second step."
946 )]
947 #[document_returns(
949 "`Loop(f(a, b))` if both steps are `Loop`, otherwise the first done encountered."
950 )]
951 #[document_examples]
952 fn lift2<'a, A, B, C>(
978 func: impl Fn(A, B) -> C + 'a,
979 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
980 fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
981 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
982 where
983 A: Clone + 'a,
984 B: Clone + 'a,
985 C: 'a, {
986 match (fa, fb) {
987 (Step::Loop(a), Step::Loop(b)) => Step::Loop(func(a, b)),
988 (Step::Done(t), _) => Step::Done(t),
989 (_, Step::Done(t)) => Step::Done(t),
990 }
991 }
992 }
993
994 #[document_type_parameters("The done type.")]
995 impl<DoneType: 'static> Pointed for StepDoneAppliedBrand<DoneType> {
996 #[document_signature]
1000 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
1002 #[document_parameters("The value to wrap.")]
1004 #[document_returns("`Loop(a)`.")]
1006 #[document_examples]
1008 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1019 Step::Loop(a)
1020 }
1021 }
1022
1023 #[document_type_parameters("The done type.")]
1024 impl<DoneType: Clone + 'static> ApplyFirst for StepDoneAppliedBrand<DoneType> {}
1025
1026 #[document_type_parameters("The done type.")]
1027 impl<DoneType: Clone + 'static> ApplySecond for StepDoneAppliedBrand<DoneType> {}
1028
1029 #[document_type_parameters("The done type.")]
1030 impl<DoneType: Clone + 'static> Semiapplicative for StepDoneAppliedBrand<DoneType> {
1031 #[document_signature]
1035 #[document_type_parameters(
1037 "The lifetime of the values.",
1038 "The brand of the cloneable function wrapper.",
1039 "The type of the input value.",
1040 "The type of the output value."
1041 )]
1042 #[document_parameters(
1044 "The step containing the function (in Loop).",
1045 "The step containing the value (in Loop)."
1046 )]
1047 #[document_returns(
1049 "`Loop(f(a))` if both are `Loop`, otherwise the first done encountered."
1050 )]
1051 #[document_examples]
1052 fn apply<'a, FnBrand: 'a + CloneableFn, A: 'a + Clone, B: 'a>(
1068 ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneableFn>::Of<'a, A, B>>),
1069 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1070 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1071 match (ff, fa) {
1072 (Step::Loop(f), Step::Loop(a)) => Step::Loop(f(a)),
1073 (Step::Done(t), _) => Step::Done(t),
1074 (_, Step::Done(t)) => Step::Done(t),
1075 }
1076 }
1077 }
1078
1079 #[document_type_parameters("The done type.")]
1080 impl<DoneType: Clone + 'static> Semimonad for StepDoneAppliedBrand<DoneType> {
1081 #[document_signature]
1085 #[document_type_parameters(
1087 "The lifetime of the values.",
1088 "The type of the result of the first computation.",
1089 "The type of the result of the second computation."
1090 )]
1091 #[document_parameters("The first step.", "The function to apply to the loop value.")]
1093 #[document_returns(
1095 "The result of applying `f` to the loop if `ma` is `Loop`, otherwise the original done."
1096 )]
1097 #[document_examples]
1099 fn bind<'a, A: 'a, B: 'a>(
1113 ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1114 func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1115 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1116 match ma {
1117 Step::Done(t) => Step::Done(t),
1118 Step::Loop(e) => func(e),
1119 }
1120 }
1121 }
1122
1123 #[document_type_parameters("The done type.")]
1124 impl<DoneType: 'static> Foldable for StepDoneAppliedBrand<DoneType> {
1125 #[document_signature]
1129 #[document_type_parameters(
1131 "The lifetime of the values.",
1132 "The brand of the cloneable function to use.",
1133 "The type of the elements in the structure.",
1134 "The type of the accumulator."
1135 )]
1136 #[document_parameters("The folding function.", "The initial value.", "The step to fold.")]
1138 #[document_returns("`func(a, initial)` if `fa` is `Loop(a)`, otherwise `initial`.")]
1140 #[document_examples]
1142 fn fold_right<'a, FnBrand, A: 'a, B: 'a>(
1168 func: impl Fn(A, B) -> B + 'a,
1169 initial: B,
1170 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1171 ) -> B
1172 where
1173 FnBrand: CloneableFn + 'a, {
1174 match fa {
1175 Step::Loop(e) => func(e, initial),
1176 Step::Done(_) => initial,
1177 }
1178 }
1179
1180 #[document_signature]
1184 #[document_type_parameters(
1186 "The lifetime of the values.",
1187 "The brand of the cloneable function to use.",
1188 "The type of the elements in the structure.",
1189 "The type of the accumulator."
1190 )]
1191 #[document_parameters("The folding function.", "The initial value.", "The step to fold.")]
1193 #[document_returns("`func(initial, a)` if `fa` is `Loop(a)`, otherwise `initial`.")]
1195 #[document_examples]
1197 fn fold_left<'a, FnBrand, A: 'a, B: 'a>(
1223 func: impl Fn(B, A) -> B + 'a,
1224 initial: B,
1225 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1226 ) -> B
1227 where
1228 FnBrand: CloneableFn + 'a, {
1229 match fa {
1230 Step::Loop(e) => func(initial, e),
1231 Step::Done(_) => initial,
1232 }
1233 }
1234
1235 #[document_signature]
1239 #[document_type_parameters(
1241 "The lifetime of the values.",
1242 "The brand of the cloneable function to use.",
1243 "The type of the elements in the structure.",
1244 "The type of the monoid."
1245 )]
1246 #[document_parameters("The mapping function.", "The step to fold.")]
1248 #[document_returns("`func(a)` if `fa` is `Loop(a)`, otherwise `M::empty()`.")]
1250 #[document_examples]
1252 fn fold_map<'a, FnBrand, A: 'a, M>(
1276 func: impl Fn(A) -> M + 'a,
1277 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1278 ) -> M
1279 where
1280 M: Monoid + 'a,
1281 FnBrand: CloneableFn + 'a, {
1282 match fa {
1283 Step::Loop(e) => func(e),
1284 Step::Done(_) => M::empty(),
1285 }
1286 }
1287 }
1288
1289 #[document_type_parameters("The done type.")]
1290 impl<DoneType: Clone + 'static> Traversable for StepDoneAppliedBrand<DoneType> {
1291 #[document_signature]
1295 #[document_type_parameters(
1297 "The lifetime of the values.",
1298 "The type of the elements in the traversable structure.",
1299 "The type of the elements in the resulting traversable structure.",
1300 "The applicative context."
1301 )]
1302 #[document_parameters("The function to apply.", "The step to traverse.")]
1304 #[document_returns("The step wrapped in the applicative context.")]
1306 #[document_examples]
1308 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1329 func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1330 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1331 ) -> 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>)>)
1332 where
1333 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1334 match ta {
1335 Step::Loop(e) => F::map(|b| Step::Loop(b), func(e)),
1336 Step::Done(t) => F::pure(Step::Done(t)),
1337 }
1338 }
1339
1340 #[document_signature]
1344 #[document_type_parameters(
1346 "The lifetime of the values.",
1347 "The type of the elements in the traversable structure.",
1348 "The applicative context."
1349 )]
1350 #[document_parameters("The step containing the applicative value.")]
1352 #[document_returns("The step wrapped in the applicative context.")]
1354 #[document_examples]
1356 fn sequence<'a, A: 'a + Clone, F: Applicative>(
1374 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>)>)
1375 ) -> 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>)>)
1376 where
1377 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1378 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
1379 match ta {
1380 Step::Loop(fe) => F::map(|e| Step::Loop(e), fe),
1381 Step::Done(t) => F::pure(Step::Done(t)),
1382 }
1383 }
1384 }
1385
1386 #[document_type_parameters("The done type.")]
1387 impl<DoneType: 'static> ParFoldable for StepDoneAppliedBrand<DoneType> {
1388 #[document_signature]
1392 #[document_type_parameters(
1394 "The lifetime of the values.",
1395 "The brand of the cloneable function wrapper.",
1396 "The element type.",
1397 "The monoid type."
1398 )]
1399 #[document_parameters(
1401 "The thread-safe function to map each element to a monoid.",
1402 "The step to fold."
1403 )]
1404 #[document_returns("The combined monoid value.")]
1406 #[document_examples]
1407 fn par_fold_map<'a, FnBrand, A, M>(
1430 func: <FnBrand as SendCloneableFn>::SendOf<'a, A, M>,
1431 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1432 ) -> M
1433 where
1434 FnBrand: 'a + SendCloneableFn,
1435 A: 'a + Clone + Send + Sync,
1436 M: Monoid + Send + Sync + 'a, {
1437 match fa {
1438 Step::Loop(e) => func(e),
1439 Step::Done(_) => M::empty(),
1440 }
1441 }
1442
1443 #[document_signature]
1447 #[document_type_parameters(
1449 "The lifetime of the values.",
1450 "The brand of the cloneable function wrapper.",
1451 "The element type.",
1452 "The accumulator type."
1453 )]
1454 #[document_parameters(
1456 "The thread-safe function to apply to each element and the accumulator.",
1457 "The initial value.",
1458 "The step to fold."
1459 )]
1460 #[document_returns("The final accumulator value.")]
1462 #[document_examples]
1463 fn par_fold_right<'a, FnBrand, A, B>(
1480 func: <FnBrand as SendCloneableFn>::SendOf<'a, (A, B), B>,
1481 initial: B,
1482 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1483 ) -> B
1484 where
1485 FnBrand: 'a + SendCloneableFn,
1486 A: 'a + Clone + Send + Sync,
1487 B: Send + Sync + 'a, {
1488 match fa {
1489 Step::Loop(e) => func((e, initial)),
1490 Step::Done(_) => initial,
1491 }
1492 }
1493 }
1494}
1495pub use inner::*;
1496
1497#[cfg(test)]
1498mod tests {
1499 use {
1500 super::*,
1501 crate::{
1502 brands::*,
1503 classes::{
1504 bifunctor::*,
1505 foldable::*,
1506 functor::*,
1507 lift::*,
1508 par_foldable::*,
1509 pointed::*,
1510 semiapplicative::*,
1511 semimonad::*,
1512 traversable::*,
1513 },
1514 functions::*,
1515 },
1516 quickcheck::{
1517 Arbitrary,
1518 Gen,
1519 },
1520 quickcheck_macros::quickcheck,
1521 };
1522
1523 impl<A: Arbitrary, B: Arbitrary> Arbitrary for Step<A, B> {
1524 fn arbitrary(g: &mut Gen) -> Self {
1525 if bool::arbitrary(g) {
1526 Step::Loop(A::arbitrary(g))
1527 } else {
1528 Step::Done(B::arbitrary(g))
1529 }
1530 }
1531 }
1532
1533 #[test]
1537 fn test_is_loop() {
1538 let step: Step<i32, i32> = Step::Loop(1);
1539 assert!(step.is_loop());
1540 assert!(!step.is_done());
1541 }
1542
1543 #[test]
1547 fn test_is_done() {
1548 let step: Step<i32, i32> = Step::Done(1);
1549 assert!(step.is_done());
1550 assert!(!step.is_loop());
1551 }
1552
1553 #[test]
1557 fn test_map_loop() {
1558 let step: Step<i32, i32> = Step::Loop(1);
1559 let mapped = step.map_loop(|x| x + 1);
1560 assert_eq!(mapped, Step::Loop(2));
1561
1562 let done: Step<i32, i32> = Step::Done(1);
1563 let mapped_done = done.map_loop(|x| x + 1);
1564 assert_eq!(mapped_done, Step::Done(1));
1565 }
1566
1567 #[test]
1571 fn test_map_done() {
1572 let step: Step<i32, i32> = Step::Done(1);
1573 let mapped = step.map_done(|x| x + 1);
1574 assert_eq!(mapped, Step::Done(2));
1575
1576 let loop_step: Step<i32, i32> = Step::Loop(1);
1577 let mapped_loop = loop_step.map_done(|x| x + 1);
1578 assert_eq!(mapped_loop, Step::Loop(1));
1579 }
1580
1581 #[test]
1585 fn test_bimap() {
1586 let step: Step<i32, i32> = Step::Loop(1);
1587 let mapped = step.bimap(|x| x + 1, |x| x * 2);
1588 assert_eq!(mapped, Step::Loop(2));
1589
1590 let done: Step<i32, i32> = Step::Done(1);
1591 let mapped_done = done.bimap(|x| x + 1, |x| x * 2);
1592 assert_eq!(mapped_done, Step::Done(2));
1593 }
1594
1595 #[test]
1597 fn test_functor_step_with_loop() {
1598 let step: Step<i32, i32> = Step::Done(5);
1599 assert_eq!(map::<StepLoopAppliedBrand<_>, _, _>(|x: i32| x * 2, step), Step::Done(10));
1600
1601 let loop_step: Step<i32, i32> = Step::Loop(5);
1602 assert_eq!(map::<StepLoopAppliedBrand<_>, _, _>(|x: i32| x * 2, loop_step), Step::Loop(5));
1603 }
1604
1605 #[test]
1607 fn test_functor_step_with_done() {
1608 let step: Step<i32, i32> = Step::Loop(5);
1609 assert_eq!(map::<StepDoneAppliedBrand<_>, _, _>(|x: i32| x * 2, step), Step::Loop(10));
1610
1611 let done_step: Step<i32, i32> = Step::Done(5);
1612 assert_eq!(map::<StepDoneAppliedBrand<_>, _, _>(|x: i32| x * 2, done_step), Step::Done(5));
1613 }
1614
1615 #[test]
1617 fn test_bifunctor_step() {
1618 let step: Step<i32, i32> = Step::Loop(5);
1619 assert_eq!(bimap::<StepBrand, _, _, _, _>(|a| a + 1, |b| b * 2, step), Step::Loop(6));
1620
1621 let done: Step<i32, i32> = Step::Done(5);
1622 assert_eq!(bimap::<StepBrand, _, _, _, _>(|a| a + 1, |b| b * 2, done), Step::Done(10));
1623 }
1624
1625 #[quickcheck]
1628 fn functor_identity_step_with_loop(x: Step<i32, i32>) -> bool {
1629 map::<StepLoopAppliedBrand<i32>, _, _>(identity, x) == x
1630 }
1631
1632 #[quickcheck]
1633 fn functor_composition_step_with_loop(x: Step<i32, i32>) -> bool {
1634 let f = |x: i32| x.wrapping_add(1);
1635 let g = |x: i32| x.wrapping_mul(2);
1636 map::<StepLoopAppliedBrand<i32>, _, _>(compose(f, g), x)
1637 == map::<StepLoopAppliedBrand<i32>, _, _>(
1638 f,
1639 map::<StepLoopAppliedBrand<i32>, _, _>(g, x),
1640 )
1641 }
1642
1643 #[quickcheck]
1646 fn functor_identity_step_with_done(x: Step<i32, i32>) -> bool {
1647 map::<StepDoneAppliedBrand<i32>, _, _>(identity, x) == x
1648 }
1649
1650 #[quickcheck]
1651 fn functor_composition_step_with_done(x: Step<i32, i32>) -> bool {
1652 let f = |x: i32| x.wrapping_add(1);
1653 let g = |x: i32| x.wrapping_mul(2);
1654 map::<StepDoneAppliedBrand<i32>, _, _>(compose(f, g), x)
1655 == map::<StepDoneAppliedBrand<i32>, _, _>(
1656 f,
1657 map::<StepDoneAppliedBrand<i32>, _, _>(g, x),
1658 )
1659 }
1660
1661 #[quickcheck]
1664 fn bifunctor_identity_step(x: Step<i32, i32>) -> bool {
1665 bimap::<StepBrand, _, _, _, _>(identity, identity, x) == x
1666 }
1667
1668 #[quickcheck]
1669 fn bifunctor_composition_step(x: Step<i32, i32>) -> bool {
1670 let f = |x: i32| x.wrapping_add(1);
1671 let g = |x: i32| x.wrapping_mul(2);
1672 let h = |x: i32| x.wrapping_sub(1);
1673 let i = |x: i32| if x == 0 { 0 } else { x.wrapping_div(2) };
1674
1675 bimap::<StepBrand, _, _, _, _>(compose(f, g), compose(h, i), x)
1676 == bimap::<StepBrand, _, _, _, _>(f, h, bimap::<StepBrand, _, _, _, _>(g, i, x))
1677 }
1678
1679 #[test]
1686 fn test_lift2_step_with_loop() {
1687 let s1: Step<i32, i32> = Step::Done(1);
1688 let s2: Step<i32, i32> = Step::Done(2);
1689 let s3: Step<i32, i32> = Step::Loop(3);
1690
1691 assert_eq!(
1692 lift2::<StepLoopAppliedBrand<i32>, _, _, _>(|x, y| x + y, s1, s2),
1693 Step::Done(3)
1694 );
1695 assert_eq!(
1696 lift2::<StepLoopAppliedBrand<i32>, _, _, _>(|x, y| x + y, s1, s3),
1697 Step::Loop(3)
1698 );
1699 }
1700
1701 #[test]
1706 fn test_lift2_step_with_done() {
1707 let s1: Step<i32, i32> = Step::Loop(1);
1708 let s2: Step<i32, i32> = Step::Loop(2);
1709 let s3: Step<i32, i32> = Step::Done(3);
1710
1711 assert_eq!(
1712 lift2::<StepDoneAppliedBrand<i32>, _, _, _>(|x, y| x + y, s1, s2),
1713 Step::Loop(3)
1714 );
1715 assert_eq!(
1716 lift2::<StepDoneAppliedBrand<i32>, _, _, _>(|x, y| x + y, s1, s3),
1717 Step::Done(3)
1718 );
1719 }
1720
1721 #[test]
1727 fn test_pointed_step_with_loop() {
1728 assert_eq!(pure::<StepLoopAppliedBrand<()>, _>(5), Step::Done(5));
1729 }
1730
1731 #[test]
1735 fn test_pointed_step_with_done() {
1736 assert_eq!(pure::<StepDoneAppliedBrand<()>, _>(5), Step::Loop(5));
1737 }
1738
1739 #[test]
1746 fn test_apply_step_with_loop() {
1747 let f =
1748 pure::<StepLoopAppliedBrand<()>, _>(cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| {
1749 x * 2
1750 }));
1751 let x = pure::<StepLoopAppliedBrand<()>, _>(5);
1752 assert_eq!(apply::<RcFnBrand, StepLoopAppliedBrand<()>, _, _>(f, x), Step::Done(10));
1753
1754 let loop_step: Step<i32, _> = Step::Loop(1);
1755 let f_loop =
1756 pure::<StepLoopAppliedBrand<i32>, _>(cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| {
1757 x * 2
1758 }));
1759 assert_eq!(
1760 apply::<RcFnBrand, StepLoopAppliedBrand<i32>, _, _>(f_loop, loop_step),
1761 Step::Loop(1)
1762 );
1763 }
1764
1765 #[test]
1770 fn test_apply_step_with_done() {
1771 let f =
1772 pure::<StepDoneAppliedBrand<()>, _>(cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| {
1773 x * 2
1774 }));
1775 let x = pure::<StepDoneAppliedBrand<()>, _>(5);
1776 assert_eq!(apply::<RcFnBrand, StepDoneAppliedBrand<()>, _, _>(f, x), Step::Loop(10));
1777
1778 let done_step: Step<_, i32> = Step::Done(1);
1779 let f_done =
1780 pure::<StepDoneAppliedBrand<i32>, _>(cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| {
1781 x * 2
1782 }));
1783 assert_eq!(
1784 apply::<RcFnBrand, StepDoneAppliedBrand<i32>, _, _>(f_done, done_step),
1785 Step::Done(1)
1786 );
1787 }
1788
1789 #[test]
1795 fn test_bind_step_with_loop() {
1796 let x = pure::<StepLoopAppliedBrand<()>, _>(5);
1797 assert_eq!(
1798 bind::<StepLoopAppliedBrand<()>, _, _>(x, |i| pure::<StepLoopAppliedBrand<()>, _>(
1799 i * 2
1800 )),
1801 Step::Done(10)
1802 );
1803
1804 let loop_step: Step<i32, i32> = Step::Loop(1);
1805 assert_eq!(
1806 bind::<StepLoopAppliedBrand<i32>, _, _>(loop_step, |i| pure::<
1807 StepLoopAppliedBrand<i32>,
1808 _,
1809 >(i * 2)),
1810 Step::Loop(1)
1811 );
1812 }
1813
1814 #[test]
1818 fn test_bind_step_with_done() {
1819 let x = pure::<StepDoneAppliedBrand<()>, _>(5);
1820 assert_eq!(
1821 bind::<StepDoneAppliedBrand<()>, _, _>(x, |i| pure::<StepDoneAppliedBrand<()>, _>(
1822 i * 2
1823 )),
1824 Step::Loop(10)
1825 );
1826
1827 let done_step: Step<i32, i32> = Step::Done(1);
1828 assert_eq!(
1829 bind::<StepDoneAppliedBrand<i32>, _, _>(done_step, |i| pure::<
1830 StepDoneAppliedBrand<i32>,
1831 _,
1832 >(i * 2)),
1833 Step::Done(1)
1834 );
1835 }
1836
1837 #[test]
1843 fn test_foldable_step_with_loop() {
1844 let x = pure::<StepLoopAppliedBrand<()>, _>(5);
1845 assert_eq!(
1846 fold_right::<RcFnBrand, StepLoopAppliedBrand<()>, _, _>(|a, b| a + b, 10, x),
1847 15
1848 );
1849 assert_eq!(fold_left::<RcFnBrand, StepLoopAppliedBrand<()>, _, _>(|b, a| b + a, 10, x), 15);
1850 assert_eq!(
1851 fold_map::<RcFnBrand, StepLoopAppliedBrand<()>, _, _>(|a: i32| a.to_string(), x),
1852 "5"
1853 );
1854
1855 let loop_step: Step<i32, i32> = Step::Loop(1);
1856 assert_eq!(
1857 fold_right::<RcFnBrand, StepLoopAppliedBrand<i32>, _, _>(|a, b| a + b, 10, loop_step),
1858 10
1859 );
1860 }
1861
1862 #[test]
1866 fn test_foldable_step_with_done() {
1867 let x = pure::<StepDoneAppliedBrand<()>, _>(5);
1868 assert_eq!(
1869 fold_right::<RcFnBrand, StepDoneAppliedBrand<()>, _, _>(|a, b| a + b, 10, x),
1870 15
1871 );
1872 assert_eq!(fold_left::<RcFnBrand, StepDoneAppliedBrand<()>, _, _>(|b, a| b + a, 10, x), 15);
1873 assert_eq!(
1874 fold_map::<RcFnBrand, StepDoneAppliedBrand<()>, _, _>(|a: i32| a.to_string(), x),
1875 "5"
1876 );
1877
1878 let done_step: Step<i32, i32> = Step::Done(1);
1879 assert_eq!(
1880 fold_right::<RcFnBrand, StepDoneAppliedBrand<i32>, _, _>(|a, b| a + b, 10, done_step),
1881 10
1882 );
1883 }
1884
1885 #[test]
1891 fn test_traversable_step_with_loop() {
1892 let x = pure::<StepLoopAppliedBrand<()>, _>(5);
1893 assert_eq!(
1894 traverse::<StepLoopAppliedBrand<()>, _, _, OptionBrand>(|a| Some(a * 2), x),
1895 Some(Step::Done(10))
1896 );
1897
1898 let loop_step: Step<i32, i32> = Step::Loop(1);
1899 assert_eq!(
1900 traverse::<StepLoopAppliedBrand<i32>, _, _, OptionBrand>(|a| Some(a * 2), loop_step),
1901 Some(Step::Loop(1))
1902 );
1903 }
1904
1905 #[test]
1909 fn test_traversable_step_with_done() {
1910 let x = pure::<StepDoneAppliedBrand<()>, _>(5);
1911 assert_eq!(
1912 traverse::<StepDoneAppliedBrand<()>, _, _, OptionBrand>(|a| Some(a * 2), x),
1913 Some(Step::Loop(10))
1914 );
1915
1916 let done_step: Step<i32, i32> = Step::Done(1);
1917 assert_eq!(
1918 traverse::<StepDoneAppliedBrand<i32>, _, _, OptionBrand>(|a| Some(a * 2), done_step),
1919 Some(Step::Done(1))
1920 );
1921 }
1922
1923 #[test]
1929 fn test_par_foldable_step_with_loop() {
1930 let x = pure::<StepLoopAppliedBrand<()>, _>(5);
1931 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|a: i32| a.to_string());
1932 assert_eq!(par_fold_map::<ArcFnBrand, StepLoopAppliedBrand<()>, _, _>(f, x), "5");
1933 }
1934
1935 #[test]
1939 fn test_par_foldable_step_with_done() {
1940 let x = pure::<StepDoneAppliedBrand<()>, _>(5);
1941 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|a: i32| a.to_string());
1942 assert_eq!(par_fold_map::<ArcFnBrand, StepDoneAppliedBrand<()>, _, _>(f, x), "5");
1943 }
1944
1945 #[quickcheck]
1949 fn monad_left_identity_step_with_loop(a: i32) -> bool {
1950 let f = |x: i32| pure::<StepLoopAppliedBrand<i32>, _>(x.wrapping_mul(2));
1951 bind::<StepLoopAppliedBrand<i32>, _, _>(pure::<StepLoopAppliedBrand<i32>, _>(a), f) == f(a)
1952 }
1953
1954 #[quickcheck]
1956 fn monad_right_identity_step_with_loop(x: Step<i32, i32>) -> bool {
1957 bind::<StepLoopAppliedBrand<i32>, _, _>(x, pure::<StepLoopAppliedBrand<i32>, _>) == x
1958 }
1959
1960 #[quickcheck]
1962 fn monad_associativity_step_with_loop(x: Step<i32, i32>) -> bool {
1963 let f = |x: i32| pure::<StepLoopAppliedBrand<i32>, _>(x.wrapping_mul(2));
1964 let g = |x: i32| pure::<StepLoopAppliedBrand<i32>, _>(x.wrapping_add(1));
1965 bind::<StepLoopAppliedBrand<i32>, _, _>(bind::<StepLoopAppliedBrand<i32>, _, _>(x, f), g)
1966 == bind::<StepLoopAppliedBrand<i32>, _, _>(x, |a| {
1967 bind::<StepLoopAppliedBrand<i32>, _, _>(f(a), g)
1968 })
1969 }
1970
1971 #[quickcheck]
1975 fn monad_left_identity_step_with_done(a: i32) -> bool {
1976 let f = |x: i32| pure::<StepDoneAppliedBrand<i32>, _>(x.wrapping_mul(2));
1977 bind::<StepDoneAppliedBrand<i32>, _, _>(pure::<StepDoneAppliedBrand<i32>, _>(a), f) == f(a)
1978 }
1979
1980 #[quickcheck]
1982 fn monad_right_identity_step_with_done(x: Step<i32, i32>) -> bool {
1983 bind::<StepDoneAppliedBrand<i32>, _, _>(x, pure::<StepDoneAppliedBrand<i32>, _>) == x
1984 }
1985
1986 #[quickcheck]
1988 fn monad_associativity_step_with_done(x: Step<i32, i32>) -> bool {
1989 let f = |x: i32| pure::<StepDoneAppliedBrand<i32>, _>(x.wrapping_mul(2));
1990 let g = |x: i32| pure::<StepDoneAppliedBrand<i32>, _>(x.wrapping_add(1));
1991 bind::<StepDoneAppliedBrand<i32>, _, _>(bind::<StepDoneAppliedBrand<i32>, _, _>(x, f), g)
1992 == bind::<StepDoneAppliedBrand<i32>, _, _>(x, |a| {
1993 bind::<StepDoneAppliedBrand<i32>, _, _>(f(a), g)
1994 })
1995 }
1996}