fp-library 0.17.0

A functional programming library for Rust featuring your favourite higher-kinded types and type classes.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
//! The `Bazaar` profunctor, used to characterize traversals.
//!
//! `Bazaar<A, B, S, T>` wraps a decomposition function `S -> BazaarList<A, B, T>` that extracts
//! a list of foci from a source and provides a rebuild function.
//!
//! This is a Rust port of PureScript's `Data.Lens.Internal.Bazaar`, specialized to the function
//! profunctor (`p = (->)`). The rank-2 polymorphism over applicatives is handled via the
//! [`BazaarList`] decomposition: instead of storing `forall f. Applicative f => (a -> f b) -> s -> f t`,
//! we store `s -> (Vec<a>, Vec<b> -> t)` and interpret it with any applicative at run time.

#[fp_macros::document_module]
mod inner {
	use {
		crate::{
			Apply,
			brands::{
				VecBrand,
				optics::*,
			},
			classes::{
				ApplyFirst,
				ApplySecond,
				Lift,
				optics::traversal::TraversalFunc,
				profunctor::{
					Choice,
					Profunctor,
					Strong,
					Wander,
				},
				*,
			},
			impl_kind,
			kinds::*,
		},
		fp_macros::*,
	};

	/// Type alias to extract the pointer brand from a `CloneFn` implementor.
	type Ptr<FunctionBrand> = <FunctionBrand as CloneFn>::PointerBrand;

	// -- BazaarList --

	/// A decomposed traversal structure: a list of foci paired with a rebuild function.
	///
	/// `BazaarList` is [`Applicative`](crate::classes::Applicative) in `T`, which is the
	/// key property that enables [`Bazaar`] to defer the choice of applicative until
	/// [`run_bazaar`] time.
	#[document_type_parameters(
		"The lifetime of the values.",
		"The cloneable function brand.",
		"The type of focus values extracted from the source.",
		"The type of replacement values used during reconstruction.",
		"The result type after reconstruction."
	)]
	pub struct BazaarList<'a, FunctionBrand: LiftFn, A: 'a, B: 'a, T: 'a> {
		/// The list of focus values extracted from the source.
		pub foci: Vec<A>,
		/// A function that reconstructs the target from a list of replacement values.
		pub rebuild: <FunctionBrand as CloneFn>::Of<'a, Vec<B>, T>,
	}

	impl_kind! {
		impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> for BazaarListBrand<FunctionBrand, A, B> {
			type Of<'a, T: 'a>: 'a = BazaarList<'a, FunctionBrand, A, B, T>;
		}
	}

	#[document_type_parameters(
		"The cloneable function brand.",
		"The focus type.",
		"The replacement type."
	)]
	impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> Functor
		for BazaarListBrand<FunctionBrand, A, B>
	{
		/// Maps a function over the result type of a `BazaarList`.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the values.",
			"The original result type.",
			"The new result type."
		)]
		///
		#[document_parameters("The function to apply.", "The bazaar list to map over.")]
		///
		#[document_returns("A new `BazaarList` with the same foci but a transformed rebuild.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::{
		/// 		optics::*,
		/// 		*,
		/// 	},
		/// 	functions::*,
		/// 	types::optics::*,
		/// };
		///
		/// let bl = BazaarList::<RcFnBrand, i32, i32, i32> {
		/// 	foci: vec![1, 2],
		/// 	rebuild: lift_fn_new::<RcFnBrand, _, _>(|bs: Vec<i32>| bs.iter().sum()),
		/// };
		/// let mapped =
		/// 	explicit::map::<BazaarListBrand<RcFnBrand, i32, i32>, _, _, _, _>(|t: i32| t * 10, bl);
		/// assert_eq!((mapped.rebuild)(vec![3, 4]), 70);
		/// ```
		fn map<'a, T: 'a, U: 'a>(
			func: impl Fn(T) -> U + 'a,
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, T>),
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, U>) {
			let rebuild = fa.rebuild;
			BazaarList {
				foci: fa.foci,
				rebuild: <FunctionBrand as LiftFn>::new(move |bs: Vec<B>| func((*rebuild)(bs))),
			}
		}
	}

	#[document_type_parameters(
		"The cloneable function brand.",
		"The focus type.",
		"The replacement type."
	)]
	impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> Pointed
		for BazaarListBrand<FunctionBrand, A, B>
	{
		/// Wraps a value in a `BazaarList` with no foci.
		#[document_signature]
		///
		#[document_type_parameters("The lifetime of the value.", "The type of the value.")]
		///
		#[document_parameters("The value to wrap.")]
		///
		#[document_returns("A `BazaarList` with empty foci that ignores its input.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::{
		/// 		optics::*,
		/// 		*,
		/// 	},
		/// 	functions::*,
		/// 	types::optics::*,
		/// };
		///
		/// let bl = pure::<BazaarListBrand<RcFnBrand, i32, i32>, _>(42);
		/// assert_eq!((bl.rebuild)(vec![]), 42);
		/// ```
		fn pure<'a, T: 'a>(a: T) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, T>) {
			let a = Ptr::<FunctionBrand>::take_cell_new(a);
			BazaarList {
				foci: vec![],
				rebuild: <FunctionBrand as LiftFn>::new(move |_: Vec<B>| {
					// SAFETY: take_cell_take is called exactly once per the optics rebuild contract
					#[expect(
						clippy::expect_used,
						reason = "Called exactly once per optics rebuild contract"
					)]
					Ptr::<FunctionBrand>::take_cell_take(&a)
						.expect("BazaarList::pure rebuild called more than once")
				}),
			}
		}
	}

	#[document_type_parameters(
		"The cloneable function brand.",
		"The focus type.",
		"The replacement type."
	)]
	impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> Lift
		for BazaarListBrand<FunctionBrand, A, B>
	{
		/// Lifts a binary function to combine two `BazaarList` values.
		///
		/// Concatenates the foci from both lists and splits the replacement vector
		/// at the boundary when rebuilding.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the values.",
			"The result type of the first `BazaarList`.",
			"The result type of the second `BazaarList`.",
			"The combined result type."
		)]
		///
		#[document_parameters(
			"The binary function to combine results.",
			"The first `BazaarList`.",
			"The second `BazaarList`."
		)]
		///
		#[document_returns(
			"A `BazaarList` with concatenated foci whose rebuild splits and delegates."
		)]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::{
		/// 		optics::*,
		/// 		*,
		/// 	},
		/// 	functions::*,
		/// 	types::optics::*,
		/// };
		///
		/// let bl1 = BazaarList::<RcFnBrand, i32, i32, i32> {
		/// 	foci: vec![1],
		/// 	rebuild: lift_fn_new::<RcFnBrand, _, _>(|bs: Vec<i32>| bs[0]),
		/// };
		/// let bl2 = BazaarList::<RcFnBrand, i32, i32, i32> {
		/// 	foci: vec![2],
		/// 	rebuild: lift_fn_new::<RcFnBrand, _, _>(|bs: Vec<i32>| bs[0]),
		/// };
		/// let combined = explicit::lift2::<BazaarListBrand<RcFnBrand, i32, i32>, _, _, _, _, _, _>(
		/// 	|a, b| a + b,
		/// 	bl1,
		/// 	bl2,
		/// );
		/// assert_eq!(combined.foci, vec![1, 2]);
		/// assert_eq!((combined.rebuild)(vec![10, 20]), 30);
		/// ```
		fn lift2<'a, T, U, V>(
			func: impl Fn(T, U) -> V + 'a,
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, T>),
			fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, U>),
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, V>)
		where
			T: Clone + 'a,
			U: Clone + 'a,
			V: 'a, {
			let split_at = fa.foci.len();
			let mut foci = fa.foci;
			foci.extend(fb.foci);
			let rebuild_a = fa.rebuild;
			let rebuild_b = fb.rebuild;
			BazaarList {
				foci,
				rebuild: <FunctionBrand as LiftFn>::new(move |mut bs: Vec<B>| {
					let right = bs.split_off(split_at);
					func((*rebuild_a)(bs), (*rebuild_b)(right))
				}),
			}
		}
	}

	#[document_type_parameters(
		"The cloneable function brand.",
		"The focus type.",
		"The replacement type."
	)]
	impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> Semiapplicative
		for BazaarListBrand<FunctionBrand, A, B>
	{
		/// Applies a `BazaarList` of functions to a `BazaarList` of values.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the values.",
			"The brand of the cloneable function wrapper.",
			"The input type.",
			"The output type."
		)]
		///
		#[document_parameters(
			"The `BazaarList` containing the function.",
			"The `BazaarList` containing the value."
		)]
		///
		#[document_returns("A `BazaarList` with the function applied.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::{
		/// 		optics::*,
		/// 		*,
		/// 	},
		/// 	functions::*,
		/// 	types::optics::*,
		/// };
		///
		/// let bl_f = BazaarList::<RcFnBrand, i32, i32, _> {
		/// 	foci: vec![],
		/// 	rebuild: lift_fn_new::<RcFnBrand, _, _>(|_: Vec<i32>| {
		/// 		lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2)
		/// 	}),
		/// };
		/// let bl_a = BazaarList::<RcFnBrand, i32, i32, i32> {
		/// 	foci: vec![5],
		/// 	rebuild: lift_fn_new::<RcFnBrand, _, _>(|bs: Vec<i32>| bs[0]),
		/// };
		/// let result = apply(bl_f, bl_a);
		/// assert_eq!((result.rebuild)(vec![7]), 14);
		/// ```
		fn apply<'a, FnB: 'a + CloneFn, T: 'a + Clone, U: 'a>(
			ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnB as CloneFn>::Of<'a, T, U>>),
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, T>),
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, U>) {
			let split_at = ff.foci.len();
			let mut foci = ff.foci;
			foci.extend(fa.foci);
			let rebuild_f = ff.rebuild;
			let rebuild_a = fa.rebuild;
			BazaarList {
				foci,
				rebuild: <FunctionBrand as LiftFn>::new(move |mut bs: Vec<B>| {
					let right = bs.split_off(split_at);
					(*rebuild_f)(bs)((*rebuild_a)(right))
				}),
			}
		}
	}

	#[document_type_parameters(
		"The cloneable function brand.",
		"The focus type.",
		"The replacement type."
	)]
	impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> ApplyFirst
		for BazaarListBrand<FunctionBrand, A, B>
	{
	}
	#[document_type_parameters(
		"The cloneable function brand.",
		"The focus type.",
		"The replacement type."
	)]
	impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> ApplySecond
		for BazaarListBrand<FunctionBrand, A, B>
	{
	}

	// -- Bazaar --

	/// The `Bazaar` profunctor, used to characterize traversals.
	///
	/// Wraps a decomposition function from `S` to [`BazaarList`], which extracts
	/// all focus values and provides a rebuild function. Use [`run_bazaar`] to
	/// interpret the `Bazaar` with any [`Applicative`](crate::classes::Applicative).
	///
	/// This is a port of PureScript's `Bazaar ((->) :: Type -> Type -> Type) a b s t`,
	/// specialized to the function profunctor.
	#[document_type_parameters(
		"The lifetime of the values.",
		"The cloneable function brand.",
		"The type of focus values extracted from the source.",
		"The type of replacement values used during reconstruction.",
		"The source type.",
		"The target type."
	)]
	pub struct Bazaar<'a, FunctionBrand: LiftFn + 'a, A: 'a, B: 'a, S: 'a, T: 'a> {
		/// Decomposes a source into a [`BazaarList`] of foci and a rebuild function.
		pub run: <FunctionBrand as CloneFn>::Of<'a, S, BazaarList<'a, FunctionBrand, A, B, T>>,
	}

	#[document_type_parameters(
		"The lifetime of the values.",
		"The cloneable function brand.",
		"The type of focus values extracted from the source.",
		"The type of replacement values used during reconstruction.",
		"The source type.",
		"The target type."
	)]
	impl<'a, FunctionBrand: LiftFn, A: 'a, B: 'a, S: 'a, T: 'a> Bazaar<'a, FunctionBrand, A, B, S, T> {
		/// Creates a new `Bazaar` instance.
		#[document_signature]
		///
		#[document_parameters("The decomposition function from source to `BazaarList`.")]
		///
		#[document_returns("A new instance of the type.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::{
		/// 		optics::*,
		/// 		*,
		/// 	},
		/// 	functions::*,
		/// 	types::optics::*,
		/// };
		///
		/// let bazaar =
		/// 	Bazaar::<RcFnBrand, i32, i32, i32, i32>::new(lift_fn_new::<RcFnBrand, _, _>(|s: i32| {
		/// 		BazaarList {
		/// 			foci: vec![s],
		/// 			rebuild: lift_fn_new::<RcFnBrand, _, _>(|bs: Vec<i32>| bs[0]),
		/// 		}
		/// 	}));
		/// let bl = (bazaar.run)(42);
		/// assert_eq!(bl.foci, vec![42]);
		/// assert_eq!((bl.rebuild)(vec![100]), 100);
		/// ```
		pub fn new(
			run: <FunctionBrand as CloneFn>::Of<'a, S, BazaarList<'a, FunctionBrand, A, B, T>>
		) -> Self {
			Bazaar {
				run,
			}
		}
	}

	/// Interprets a [`Bazaar`] with a specific [`Applicative`](crate::classes::Applicative).
	///
	/// Given a handler `A -> F B` and a source `S`, decomposes the source via the Bazaar,
	/// maps each focus through the handler, sequences the results, and rebuilds the target.
	#[document_signature]
	///
	#[document_type_parameters(
		"The lifetime of the values.",
		"The cloneable function brand.",
		"The type of focus values.",
		"The type of replacement values.",
		"The source type.",
		"The target type.",
		"The applicative context."
	)]
	///
	#[document_parameters(
		"The handler function that lifts each focus into the applicative.",
		"The source value.",
		"The bazaar to interpret."
	)]
	///
	#[document_returns("The target wrapped in the applicative context.")]
	///
	#[document_examples]
	///
	/// ```
	/// use fp_library::{
	/// 	brands::{
	/// 		optics::*,
	/// 		*,
	/// 	},
	/// 	functions::*,
	/// 	types::optics::*,
	/// };
	///
	/// let bazaar =
	/// 	Bazaar::<RcFnBrand, i32, i32, Vec<i32>, Vec<i32>>::new(lift_fn_new::<RcFnBrand, _, _>(
	/// 		|s: Vec<i32>| {
	/// 			let len = s.len();
	/// 			BazaarList {
	/// 				foci: s,
	/// 				rebuild: lift_fn_new::<RcFnBrand, _, _>(move |bs: Vec<i32>| {
	/// 					bs.into_iter().take(len).collect()
	/// 				}),
	/// 			}
	/// 		},
	/// 	));
	/// let result = run_bazaar::<RcFnBrand, _, _, _, _, OptionBrand>(
	/// 	|x: i32| Some(x + 1),
	/// 	vec![1, 2, 3],
	/// 	&bazaar,
	/// );
	/// assert_eq!(result, Some(vec![2, 3, 4]));
	/// ```
	pub fn run_bazaar<'a, FunctionBrand, A, B, S, T, F>(
		handler: impl Fn(A) -> Apply!(<F as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, B>) + 'a,
		s: S,
		bazaar: &Bazaar<'a, FunctionBrand, A, B, S, T>,
	) -> Apply!(<F as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, T>)
	where
		FunctionBrand: LiftFn + 'static,
		A: 'a + Clone,
		B: 'a + Clone,
		S: 'a,
		T: 'a,
		F: crate::classes::Applicative,
		Apply!(<F as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, B>): Clone,
		Apply!(<F as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, Vec<B>>): Clone,
		Apply!(<F as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, T>): Clone, {
		let bl = (bazaar.run)(s);
		let f_bs: Vec<Apply!(<F as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, B>)> =
			bl.foci.into_iter().map(&handler).collect();
		let f_vec_b: Apply!(<F as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, Vec<B>>) =
			VecBrand::sequence::<'a, _, F>(f_bs);
		let rebuild = bl.rebuild;
		F::map(move |bs| (*rebuild)(bs), f_vec_b)
	}

	// -- BazaarBrand --

	impl_kind! {
		impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> for BazaarBrand<FunctionBrand, A, B> {
			#[document_default]
			type Of<'a, S: 'a, T: 'a>: 'a = Bazaar<'a, FunctionBrand, A, B, S, T>;
		}
	}

	#[document_type_parameters(
		"The cloneable function brand.",
		"The focus type.",
		"The replacement type."
	)]
	impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> Profunctor
		for BazaarBrand<FunctionBrand, A, B>
	{
		/// Maps functions over the input and output of the `Bazaar` profunctor.
		///
		/// Corresponds to PureScript's `dimap f g (Bazaar b) = Bazaar \pafb s -> g <$> b pafb (f s)`.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the values.",
			"The new source type.",
			"The original source type.",
			"The original target type.",
			"The new target type."
		)]
		///
		#[document_parameters(
			"The contravariant function to apply to the source.",
			"The covariant function to apply to the target.",
			"The bazaar instance to transform."
		)]
		///
		#[document_returns("A transformed `Bazaar` instance.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::{
		/// 		optics::*,
		/// 		*,
		/// 	},
		/// 	classes::profunctor::*,
		/// 	functions::*,
		/// 	types::optics::*,
		/// };
		///
		/// let bazaar =
		/// 	Bazaar::<RcFnBrand, i32, i32, i32, i32>::new(lift_fn_new::<RcFnBrand, _, _>(|s: i32| {
		/// 		BazaarList {
		/// 			foci: vec![s],
		/// 			rebuild: lift_fn_new::<RcFnBrand, _, _>(|bs: Vec<i32>| bs[0]),
		/// 		}
		/// 	}));
		/// let dimapped = <BazaarBrand<RcFnBrand, i32, i32> as Profunctor>::dimap(
		/// 	|s: String| s.parse::<i32>().unwrap(),
		/// 	|t: i32| t.to_string(),
		/// 	bazaar,
		/// );
		/// let bl = (dimapped.run)("42".to_string());
		/// assert_eq!(bl.foci, vec![42]);
		/// assert_eq!((bl.rebuild)(vec![100]), "100".to_string());
		/// ```
		fn dimap<'a, S: 'a, T: 'a, U: 'a, V: 'a>(
			st: impl Fn(S) -> T + 'a,
			uv: impl Fn(U) -> V + 'a,
			puv: Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, T, U>),
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, S, V>) {
			let run = puv.run;
			let uv = <FunctionBrand as LiftFn>::new(uv);
			Bazaar::new(<FunctionBrand as LiftFn>::new(move |s: S| {
				let bl = (*run)(st(s));
				let rebuild = bl.rebuild;
				let uv = uv.clone();
				BazaarList {
					foci: bl.foci,
					rebuild: <FunctionBrand as LiftFn>::new(move |bs: Vec<B>| {
						(*uv)((*rebuild)(bs))
					}),
				}
			}))
		}
	}

	#[document_type_parameters(
		"The cloneable function brand.",
		"The focus type.",
		"The replacement type."
	)]
	impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> Strong
		for BazaarBrand<FunctionBrand, A, B>
	{
		/// Lifts the `Bazaar` profunctor to operate on the first component of a tuple.
		///
		/// Corresponds to PureScript's `first (Bazaar b) = Bazaar (\pafb (Tuple x y) -> flip Tuple y <$> b pafb x)`.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the values.",
			"The source type.",
			"The target type.",
			"The type of the second component."
		)]
		///
		#[document_parameters("The bazaar instance to lift.")]
		///
		#[document_returns("A `Bazaar` that operates on pairs.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::{
		/// 		optics::*,
		/// 		*,
		/// 	},
		/// 	classes::profunctor::*,
		/// 	functions::*,
		/// 	types::optics::*,
		/// };
		///
		/// let bazaar =
		/// 	Bazaar::<RcFnBrand, i32, i32, i32, i32>::new(lift_fn_new::<RcFnBrand, _, _>(|s: i32| {
		/// 		BazaarList {
		/// 			foci: vec![s],
		/// 			rebuild: lift_fn_new::<RcFnBrand, _, _>(|bs: Vec<i32>| bs[0]),
		/// 		}
		/// 	}));
		/// let lifted = <BazaarBrand<RcFnBrand, i32, i32> as Strong>::first::<i32, i32, String>(bazaar);
		/// let bl = (lifted.run)((42, "hello".to_string()));
		/// assert_eq!(bl.foci, vec![42]);
		/// assert_eq!((bl.rebuild)(vec![100]), (100, "hello".to_string()));
		/// ```
		fn first<'a, S: 'a, T: 'a, C: 'a>(
			pab: Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, S, T>)
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, (S, C), (T, C)>) {
			let run = pab.run;
			Bazaar::new(<FunctionBrand as LiftFn>::new(move |(s, c): (S, C)| {
				let bl = (*run)(s);
				let rebuild = bl.rebuild;
				let c = Ptr::<FunctionBrand>::take_cell_new(c);
				BazaarList {
					foci: bl.foci,
					rebuild: <FunctionBrand as LiftFn>::new(move |bs: Vec<B>| {
						// SAFETY: take_cell_take is called exactly once per the optics rebuild contract
						#[expect(
							clippy::expect_used,
							reason = "Called exactly once per optics rebuild contract"
						)]
						let c = Ptr::<FunctionBrand>::take_cell_take(&c)
							.expect("BazaarList rebuild called more than once");
						((*rebuild)(bs), c)
					}),
				}
			}))
		}

		/// Lifts the `Bazaar` profunctor to operate on the second component of a tuple.
		///
		/// Corresponds to PureScript's `second (Bazaar b) = Bazaar (\pafb (Tuple x y) -> Tuple x <$> b pafb y)`.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the values.",
			"The source type.",
			"The target type.",
			"The type of the first component."
		)]
		///
		#[document_parameters("The bazaar instance to lift.")]
		///
		#[document_returns("A `Bazaar` that operates on pairs.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::{
		/// 		optics::*,
		/// 		*,
		/// 	},
		/// 	classes::profunctor::*,
		/// 	functions::*,
		/// 	types::optics::*,
		/// };
		///
		/// let bazaar =
		/// 	Bazaar::<RcFnBrand, i32, i32, i32, i32>::new(lift_fn_new::<RcFnBrand, _, _>(|s: i32| {
		/// 		BazaarList {
		/// 			foci: vec![s],
		/// 			rebuild: lift_fn_new::<RcFnBrand, _, _>(|bs: Vec<i32>| bs[0]),
		/// 		}
		/// 	}));
		/// let lifted = <BazaarBrand<RcFnBrand, i32, i32> as Strong>::second::<i32, i32, String>(bazaar);
		/// let bl = (lifted.run)(("hello".to_string(), 42));
		/// assert_eq!(bl.foci, vec![42]);
		/// assert_eq!((bl.rebuild)(vec![100]), ("hello".to_string(), 100));
		/// ```
		fn second<'a, S: 'a, T: 'a, C: 'a>(
			pab: Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, S, T>)
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, (C, S), (C, T)>) {
			let run = pab.run;
			Bazaar::new(<FunctionBrand as LiftFn>::new(move |(c, s): (C, S)| {
				let bl = (*run)(s);
				let rebuild = bl.rebuild;
				let c = Ptr::<FunctionBrand>::take_cell_new(c);
				BazaarList {
					foci: bl.foci,
					rebuild: <FunctionBrand as LiftFn>::new(move |bs: Vec<B>| {
						// SAFETY: take_cell_take is called exactly once per the optics rebuild contract
						#[expect(
							clippy::expect_used,
							reason = "Called exactly once per optics rebuild contract"
						)]
						let c = Ptr::<FunctionBrand>::take_cell_take(&c)
							.expect("BazaarList rebuild called more than once");
						(c, (*rebuild)(bs))
					}),
				}
			}))
		}
	}

	#[document_type_parameters(
		"The cloneable function brand.",
		"The focus type.",
		"The replacement type."
	)]
	impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> Choice
		for BazaarBrand<FunctionBrand, A, B>
	{
		/// Lifts the `Bazaar` profunctor to operate on the `Err` variant of a `Result`.
		///
		/// Corresponds to PureScript's `left (Bazaar b) = Bazaar (\pafb e -> bitraverse (b pafb) pure e)`.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the values.",
			"The source type.",
			"The target type.",
			"The type of the `Ok` variant."
		)]
		///
		#[document_parameters("The bazaar instance to lift.")]
		///
		#[document_returns("A `Bazaar` that operates on `Result` types.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::{
		/// 		optics::*,
		/// 		*,
		/// 	},
		/// 	classes::profunctor::*,
		/// 	functions::*,
		/// 	types::optics::*,
		/// };
		///
		/// let bazaar =
		/// 	Bazaar::<RcFnBrand, i32, i32, i32, i32>::new(lift_fn_new::<RcFnBrand, _, _>(|s: i32| {
		/// 		BazaarList {
		/// 			foci: vec![s],
		/// 			rebuild: lift_fn_new::<RcFnBrand, _, _>(|bs: Vec<i32>| bs[0]),
		/// 		}
		/// 	}));
		/// let lifted = <BazaarBrand<RcFnBrand, i32, i32> as Choice>::left::<i32, i32, String>(bazaar);
		/// let bl_err = (lifted.run)(Err(42));
		/// assert_eq!(bl_err.foci, vec![42]);
		/// assert_eq!((bl_err.rebuild)(vec![100]), Err(100));
		/// let bl_ok = (lifted.run)(Ok("hello".to_string()));
		/// assert_eq!(bl_ok.foci.len(), 0);
		/// assert_eq!((bl_ok.rebuild)(vec![]), Ok("hello".to_string()));
		/// ```
		fn left<'a, S: 'a, T: 'a, C: 'a>(
			pab: Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, S, T>)
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, Result<C, S>, Result<C, T>>)
		{
			let run = pab.run;
			Bazaar::new(<FunctionBrand as LiftFn>::new(move |r: Result<C, S>| match r {
				Err(s) => {
					let bl = (*run)(s);
					let rebuild = bl.rebuild;
					BazaarList {
						foci: bl.foci,
						rebuild: <FunctionBrand as LiftFn>::new(move |bs: Vec<B>| {
							Err((*rebuild)(bs))
						}),
					}
				}
				Ok(c) => {
					let c = Ptr::<FunctionBrand>::take_cell_new(c);
					BazaarList {
						foci: vec![],
						rebuild: <FunctionBrand as LiftFn>::new(move |_: Vec<B>| {
							// SAFETY: take_cell_take is called exactly once per the optics rebuild contract
							#[expect(
								clippy::expect_used,
								reason = "Called exactly once per optics rebuild contract"
							)]
							Ok(Ptr::<FunctionBrand>::take_cell_take(&c)
								.expect("BazaarList rebuild called more than once"))
						}),
					}
				}
			}))
		}

		/// Lifts the `Bazaar` profunctor to operate on the `Ok` variant of a `Result`.
		///
		/// Corresponds to PureScript's `right (Bazaar b) = Bazaar (\pafb e -> traverse (b pafb) e)`.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the values.",
			"The source type.",
			"The target type.",
			"The type of the `Err` variant."
		)]
		///
		#[document_parameters("The bazaar instance to lift.")]
		///
		#[document_returns("A `Bazaar` that operates on `Result` types.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::{
		/// 		optics::*,
		/// 		*,
		/// 	},
		/// 	classes::profunctor::*,
		/// 	functions::*,
		/// 	types::optics::*,
		/// };
		///
		/// let bazaar =
		/// 	Bazaar::<RcFnBrand, i32, i32, i32, i32>::new(lift_fn_new::<RcFnBrand, _, _>(|s: i32| {
		/// 		BazaarList {
		/// 			foci: vec![s],
		/// 			rebuild: lift_fn_new::<RcFnBrand, _, _>(|bs: Vec<i32>| bs[0]),
		/// 		}
		/// 	}));
		/// let lifted = <BazaarBrand<RcFnBrand, i32, i32> as Choice>::right::<i32, i32, String>(bazaar);
		/// let bl_ok = (lifted.run)(Ok(42));
		/// assert_eq!(bl_ok.foci, vec![42]);
		/// assert_eq!((bl_ok.rebuild)(vec![100]), Ok(100));
		/// let bl_err = (lifted.run)(Err("oops".to_string()));
		/// assert_eq!(bl_err.foci.len(), 0);
		/// assert_eq!((bl_err.rebuild)(vec![]), Err("oops".to_string()));
		/// ```
		fn right<'a, S: 'a, T: 'a, C: 'a>(
			pab: Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, S, T>)
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, Result<S, C>, Result<T, C>>)
		{
			let run = pab.run;
			Bazaar::new(<FunctionBrand as LiftFn>::new(move |r: Result<S, C>| match r {
				Ok(s) => {
					let bl = (*run)(s);
					let rebuild = bl.rebuild;
					BazaarList {
						foci: bl.foci,
						rebuild: <FunctionBrand as LiftFn>::new(move |bs: Vec<B>| {
							Ok((*rebuild)(bs))
						}),
					}
				}
				Err(c) => {
					let c = Ptr::<FunctionBrand>::take_cell_new(c);
					BazaarList {
						foci: vec![],
						rebuild: <FunctionBrand as LiftFn>::new(move |_: Vec<B>| {
							// SAFETY: take_cell_take is called exactly once per the optics rebuild contract
							#[expect(
								clippy::expect_used,
								reason = "Called exactly once per optics rebuild contract"
							)]
							Err(Ptr::<FunctionBrand>::take_cell_take(&c)
								.expect("BazaarList rebuild called more than once"))
						}),
					}
				}
			}))
		}
	}

	#[document_type_parameters(
		"The cloneable function brand.",
		"The focus type.",
		"The replacement type."
	)]
	impl<FunctionBrand: LiftFn + 'static, A: 'static + Clone, B: 'static + Clone> Wander
		for BazaarBrand<FunctionBrand, A, B>
	{
		/// Lifts the `Bazaar` profunctor through a traversal.
		///
		/// Corresponds to PureScript's `wander w (Bazaar f) = Bazaar (\pafb s -> w (f pafb) s)`.
		/// Uses `BazaarListBrand` as the applicative to decompose the traversal structure.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the values.",
			"The outer source type.",
			"The outer target type.",
			"The inner source type (focus of the traversal).",
			"The inner target type."
		)]
		///
		#[document_parameters("The traversal function.", "The bazaar instance to compose with.")]
		///
		#[document_returns("A `Bazaar` that traverses the outer structure.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	Apply,
		/// 	brands::{
		/// 		optics::*,
		/// 		*,
		/// 	},
		/// 	classes::{
		/// 		Applicative,
		/// 		optics::traversal::TraversalFunc,
		/// 		profunctor::*,
		/// 	},
		/// 	functions::*,
		/// 	kinds::*,
		/// 	types::optics::*,
		/// };
		///
		/// // A traversal over Vec elements
		/// #[derive(Clone)]
		/// struct VecTraversal;
		/// impl<'a, X: 'a + Clone> TraversalFunc<'a, Vec<X>, Vec<X>, X, X> for VecTraversal {
		/// 	fn apply<M: Applicative>(
		/// 		&self,
		/// 		f: Box<dyn Fn(X) -> Apply!(<M as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, X>) + 'a>,
		/// 		s: Vec<X>,
		/// 	) -> Apply!(<M as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, Vec<X>>) {
		/// 		s.into_iter().fold(M::pure(vec![]), |acc, a| {
		/// 			M::lift2(
		/// 				|mut v: Vec<X>, x: X| {
		/// 					v.push(x);
		/// 					v
		/// 				},
		/// 				acc,
		/// 				f(a),
		/// 			)
		/// 		})
		/// 	}
		/// }
		///
		/// // Identity bazaar: each element maps to itself
		/// let id_bazaar =
		/// 	Bazaar::<RcFnBrand, i32, i32, i32, i32>::new(lift_fn_new::<RcFnBrand, _, _>(|s: i32| {
		/// 		BazaarList {
		/// 			foci: vec![s],
		/// 			rebuild: lift_fn_new::<RcFnBrand, _, _>(|bs: Vec<i32>| bs[0]),
		/// 		}
		/// 	}));
		/// let wandered =
		/// 	<BazaarBrand<RcFnBrand, i32, i32> as Wander>::wander::<Vec<i32>, Vec<i32>, i32, i32>(
		/// 		VecTraversal,
		/// 		id_bazaar,
		/// 	);
		/// let bl = (wandered.run)(vec![10, 20, 30]);
		/// assert_eq!(bl.foci, vec![10, 20, 30]);
		/// assert_eq!((bl.rebuild)(vec![1, 2, 3]), vec![1, 2, 3]);
		/// ```
		fn wander<'a, S: 'a, T: 'a, A2: 'a, B2: 'a + Clone>(
			traversal: impl TraversalFunc<'a, S, T, A2, B2> + 'a,
			pab: Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, A2, B2>),
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, S, T>) {
			let run = pab.run;
			Bazaar::new(<FunctionBrand as LiftFn>::new(move |s: S| {
				let run = run.clone();
				traversal.apply::<BazaarListBrand<FunctionBrand, A, B>>(
					Box::new(move |a2: A2| (*run)(a2)),
					s,
				)
			}))
		}
	}
}
pub use inner::*;

impl<'a, FB: crate::classes::clone_fn::LiftFn + 'static, A: Clone + 'a, B: 'a, T: 'a> Clone
	for BazaarList<'a, FB, A, B, T>
where
	<FB as crate::classes::clone_fn::CloneFn>::Of<'a, Vec<B>, T>: Clone,
{
	fn clone(&self) -> Self {
		BazaarList {
			foci: self.foci.clone(),
			rebuild: self.rebuild.clone(),
		}
	}
}