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
//! Traits for optics.
pub mod fold;
pub mod indexed_traversal;
pub mod traversal;
pub use {
fold::*,
indexed_traversal::*,
traversal::*,
};
#[fp_macros::document_module]
mod inner {
use {
crate::{
brands::{
optics::*,
*,
},
classes::{
profunctor::*,
*,
},
kinds::*,
},
fp_macros::*,
};
/// A trait for optics that can be evaluated with any profunctor constraint.
///
/// This trait allows optics to be first-class values that can be composed
/// and stored while preserving their polymorphism over profunctor types.
#[document_type_parameters(
"The lifetime of the values.",
"The profunctor type.",
"The source type of the structure.",
"The target type of the structure after an update.",
"The source type of the focus.",
"The target type of the focus after an update."
)]
#[document_parameters("The optic instance.")]
pub trait Optic<'a, P: Profunctor, S: 'a, T: 'a, A: 'a, B: 'a> {
/// Evaluate the optic with a profunctor.
///
/// This method applies the optic transformation to a profunctor value.
#[document_signature]
///
#[document_parameters("The profunctor value to transform.")]
///
#[document_returns("The transformed profunctor value.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::optics::*,
/// functions::*,
/// types::optics::*,
/// };
///
/// let l: LensPrime<RcBrand, (i32, String), i32> =
/// LensPrime::from_view_set(|(x, _)| x, |((_, s), x)| (x, s));
///
/// let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
/// let modifier =
/// <LensPrime<RcBrand, (i32, String), i32> as Optic<RcFnBrand, _, _, _, _>>::evaluate(&l, f);
/// assert_eq!(modifier((21, "hello".to_string())), (42, "hello".to_string()));
/// ```
fn evaluate(
&self,
pab: Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, A, B>),
) -> Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, T>);
}
/// An isomorphism optic.
#[document_type_parameters(
"The lifetime of the values.",
"The source type of the structure.",
"The target type of the structure after an update.",
"The source type of the focus.",
"The target type of the focus after an update."
)]
#[document_parameters("The optic instance.")]
pub trait IsoOptic<'a, S: 'a, T: 'a, A: 'a, B: 'a> {
/// Evaluate the optic with any profunctor.
#[document_signature]
///
#[document_type_parameters("The profunctor type.")]
///
#[document_parameters("The profunctor value to transform.")]
///
#[document_returns("The transformed profunctor value.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::optics::*,
/// functions::*,
/// types::optics::*,
/// };
///
/// let iso: IsoPrime<RcBrand, i32, i32> = IsoPrime::new(|x| x, |x| x);
/// let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
/// let modifier = <IsoPrime<RcBrand, i32, i32> as IsoOptic<i32, i32, i32, i32>>::evaluate::<
/// RcFnBrand,
/// >(&iso, f);
/// assert_eq!(modifier(21), 42);
/// ```
fn evaluate<P: Profunctor + 'static>(
&self,
pab: Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, A, B>),
) -> Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, T>);
}
/// A lens optic.
#[document_type_parameters(
"The lifetime of the values.",
"The source type of the structure.",
"The target type of the structure after an update.",
"The source type of the focus.",
"The target type of the focus after an update."
)]
#[document_parameters("The optic instance.")]
pub trait LensOptic<'a, S: 'a, T: 'a, A: 'a, B: 'a> {
/// Evaluate the optic with a strong profunctor.
#[document_signature]
///
#[document_type_parameters("The profunctor type.")]
///
#[document_parameters("The profunctor value to transform.")]
///
#[document_returns("The transformed profunctor value.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::optics::*,
/// functions::*,
/// types::optics::*,
/// };
///
/// let l: LensPrime<RcBrand, (i32, String), i32> =
/// LensPrime::from_view_set(|(x, _)| x, |((_, s), x)| (x, s));
/// let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
/// let modifier = <LensPrime<RcBrand, (i32, String), i32> as LensOptic<
/// (i32, String),
/// (i32, String),
/// i32,
/// i32,
/// >>::evaluate::<RcFnBrand>(&l, f);
/// assert_eq!(modifier((21, "hi".to_string())), (42, "hi".to_string()));
/// ```
fn evaluate<P: Strong>(
&self,
pab: Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, A, B>),
) -> Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, T>);
}
/// A prism optic.
#[document_type_parameters(
"The lifetime of the values.",
"The source type of the structure.",
"The target type of the structure after an update.",
"The source type of the focus.",
"The target type of the focus after an update."
)]
#[document_parameters("The optic instance.")]
pub trait PrismOptic<'a, S: 'a, T: 'a, A: 'a, B: 'a> {
/// Evaluate the optic with a choice profunctor.
#[document_signature]
///
#[document_type_parameters("The profunctor type.")]
///
#[document_parameters("The profunctor value to transform.")]
///
#[document_returns("The transformed profunctor value.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::optics::*,
/// functions::*,
/// types::optics::*,
/// };
///
/// let p: PrismPrime<RcBrand, Option<i32>, i32> = PrismPrime::from_option(|o| o, Some);
/// let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
/// let modifier = <PrismPrime<RcBrand, Option<i32>, i32> as PrismOptic<
/// Option<i32>,
/// Option<i32>,
/// i32,
/// i32,
/// >>::evaluate::<RcFnBrand>(&p, f);
/// assert_eq!(modifier(Some(21)), Some(42));
/// ```
fn evaluate<P: Choice>(
&self,
pab: Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, A, B>),
) -> Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, T>);
}
/// An affine traversal optic.
#[document_type_parameters(
"The lifetime of the values.",
"The source type of the structure.",
"The target type of the structure after an update.",
"The source type of the focus.",
"The target type of the focus after an update."
)]
#[document_parameters("The optic instance.")]
pub trait AffineTraversalOptic<'a, S: 'a, T: 'a, A: 'a, B: 'a> {
/// Evaluate the optic with a strong and choice profunctor.
#[document_signature]
///
#[document_type_parameters("The profunctor type.")]
///
#[document_parameters("The profunctor value to transform.")]
///
#[document_returns("The transformed profunctor value.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::optics::*,
/// functions::*,
/// types::optics::*,
/// };
///
/// let at: AffineTraversalPrime<RcBrand, (i32, String), i32> =
/// AffineTraversalPrime::from_preview_set(|(x, _)| Some(x), |((_, s), x)| (x, s));
/// let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
/// let modifier = <AffineTraversalPrime<RcBrand, (i32, String), i32> as AffineTraversalOptic<
/// (i32, String),
/// (i32, String),
/// i32,
/// i32,
/// >>::evaluate::<RcFnBrand>(&at, f);
/// assert_eq!(modifier((21, "hi".to_string())), (42, "hi".to_string()));
/// ```
fn evaluate<P: Strong + Choice>(
&self,
pab: Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, A, B>),
) -> Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, T>);
}
/// A traversal optic.
#[document_type_parameters(
"The lifetime of the values.",
"The source type of the structure.",
"The target type of the structure after an update.",
"The source type of the focus.",
"The target type of the focus after an update."
)]
#[document_parameters("The optic instance.")]
pub trait TraversalOptic<'a, S: 'a, T: 'a, A: 'a, B: 'a> {
/// Evaluate the optic with a wander profunctor.
#[document_signature]
///
#[document_type_parameters("The profunctor type.")]
///
#[document_parameters("The profunctor value to transform.")]
///
#[document_returns("The transformed profunctor value.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::optics::*,
/// functions::*,
/// types::optics::*,
/// };
///
/// // Use Vec's built-in optic support
/// let v = vec![1, 2, 3];
/// let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
/// let result = f(v[0]); // Simple example showing the concept
/// assert_eq!(result, 2);
/// ```
fn evaluate<P: Wander>(
&self,
pab: Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, A, B>),
) -> Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, T>);
}
/// A getter optic.
#[document_type_parameters(
"The lifetime of the values.",
"The source type of the structure.",
"The focus type."
)]
#[document_parameters("The optic instance.")]
pub trait GetterOptic<'a, S: 'a, A: 'a> {
/// Evaluate the optic with the forget profunctor.
#[document_signature]
///
#[document_type_parameters(
"The return type of the forget profunctor.",
"The reference-counted pointer type."
)]
///
#[document_parameters("The profunctor value to transform.")]
///
#[document_returns("The transformed forget profunctor value.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::optics::*,
/// functions::*,
/// types::optics::*,
/// };
///
/// let g: GetterPrime<RcBrand, (i32, String), i32> = GetterPrime::new(|(x, _)| x);
/// let f = Forget::<RcBrand, i32, i32, i32>::new(|x| x);
/// let folded =
/// <GetterPrime<RcBrand, (i32, String), i32> as GetterOptic<(i32, String), i32>>::evaluate::<
/// i32,
/// RcBrand,
/// >(&g, f);
/// assert_eq!(folded.run((42, "hi".to_string())), 42);
/// ```
fn evaluate<R: 'a + 'static, PointerBrand: ToDynCloneFn + 'static>(
&self,
pab: Apply!(<ForgetBrand<PointerBrand, R> as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, A, A>),
) -> Apply!(<ForgetBrand<PointerBrand, R> as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, S>);
}
/// A fold optic.
#[document_type_parameters(
"The lifetime of the values.",
"The source type of the structure.",
"The focus type."
)]
#[document_parameters("The optic instance.")]
pub trait FoldOptic<'a, S: 'a, A: 'a> {
/// Evaluate the optic with the forget profunctor for any monoid.
#[document_signature]
///
#[document_type_parameters("The monoid type.", "The reference-counted pointer type.")]
///
#[document_parameters("The profunctor value to transform.")]
///
#[document_returns("The transformed forget profunctor value.")]
///
/// ### `R: Clone` Requirement
///
/// The result monoid `R` must implement [`Clone`] because the fold is internally implemented
/// via [`Wander`] with the [`Forget`](crate::types::optics::Forget) profunctor, which stores
/// `R` inside [`Const`](crate::types::const_val::Const). The traversal applies
/// [`TraversalFunc::apply`] with
/// [`ConstBrand<R>`](crate::brands::ConstBrand) as the applicative, and that
/// requires `Const<R, B>: Clone`, which in turn requires `R: Clone`.
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::{
/// Monoid,
/// optics::*,
/// },
/// functions::*,
/// types::optics::*,
/// };
/// let f_optic: FoldPrime<RcBrand, Vec<i32>, i32, _> =
/// FoldPrime::new(IterableFoldFn(|v: Vec<i32>| v));
/// let f = Forget::<RcBrand, String, i32, i32>::new(|x| x.to_string());
/// let folded = <FoldPrime<RcBrand, Vec<i32>, i32, _> as FoldOptic<Vec<i32>, i32>>::evaluate::<
/// String,
/// RcBrand,
/// >(&f_optic, f);
/// assert_eq!(folded.run(vec![1, 2, 3]), "123".to_string());
/// ```
fn evaluate<R: 'a + Monoid + Clone + 'static, PointerBrand: ToDynCloneFn + 'static>(
&self,
pab: Apply!(<ForgetBrand<PointerBrand, R> as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, A, A>),
) -> Apply!(<ForgetBrand<PointerBrand, R> as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, S>);
}
/// A setter optic.
#[document_type_parameters(
"The lifetime of the values.",
"The reference-counted pointer type.",
"The source type of the structure.",
"The target type of the structure after an update.",
"The source type of the focus.",
"The target type of the focus after an update."
)]
#[document_parameters("The optic instance.")]
pub trait SetterOptic<'a, PointerBrand: ToDynCloneFn, S: 'a, T: 'a, A: 'a, B: 'a> {
/// Evaluate the optic with the function profunctor.
#[document_signature]
///
#[document_parameters("The profunctor value to transform.")]
///
#[document_returns("The transformed function profunctor value.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::optics::*,
/// functions::*,
/// types::optics::*,
/// };
///
/// let s: SetterPrime<RcBrand, (i32, String), i32> =
/// SetterPrime::new(|(s, f): ((i32, String), Box<dyn Fn(i32) -> i32>)| (f(s.0), s.1));
/// let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
/// let modifier = <SetterPrime<RcBrand, (i32, String), i32> as SetterOptic<
/// RcBrand,
/// (i32, String),
/// (i32, String),
/// i32,
/// i32,
/// >>::evaluate(&s, f);
/// assert_eq!(modifier((21, "hi".to_string())), (42, "hi".to_string()));
/// ```
fn evaluate(
&self,
pab: Apply!(<FnBrand<PointerBrand> as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, A, B>),
) -> Apply!(<FnBrand<PointerBrand> as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, T>);
}
/// An indexed lens optic.
#[document_type_parameters(
"The lifetime of the values.",
"The index type.",
"The source type of the structure.",
"The target type of the structure after an update.",
"The source type of the focus.",
"The target type of the focus after an update."
)]
#[document_parameters("The optic instance.")]
pub trait IndexedLensOptic<'a, I: 'a, S: 'a, T: 'a, A: 'a, B: 'a> {
/// Evaluate the optic with a strong profunctor.
#[document_signature]
///
#[document_type_parameters("The profunctor type.")]
///
#[document_parameters("The indexed profunctor value to transform.")]
///
#[document_returns("The transformed profunctor value.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::optics::*,
/// functions::*,
/// types::optics::*,
/// };
///
/// let l: LensPrime<RcBrand, (i32, String), i32> =
/// LensPrime::from_view_set(|(x, _)| x, |((_, s), x)| (x, s));
/// assert_eq!(l.over((21, "hi".to_string()), |x| x * 2), (42, "hi".to_string()));
/// ```
fn evaluate<P: Strong>(
&self,
pab: crate::types::optics::Indexed<'a, P, I, A, B>,
) -> Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, T>);
}
/// An indexed traversal optic.
#[document_type_parameters(
"The lifetime of the values.",
"The index type.",
"The source type of the structure.",
"The target type of the structure after an update.",
"The source type of the focus.",
"The target type of the focus after an update."
)]
#[document_parameters("The optic instance.")]
pub trait IndexedTraversalOptic<'a, I: 'a, S: 'a, T: 'a, A: 'a, B: 'a> {
/// Evaluate the optic with a wander profunctor.
#[document_signature]
///
#[document_type_parameters("The profunctor type.")]
///
#[document_parameters("The indexed profunctor value to transform.")]
///
#[document_returns("The transformed profunctor value.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::optics::*,
/// functions::*,
/// types::optics::*,
/// };
///
/// let v = vec![1, 2, 3];
/// let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
/// assert_eq!(f(v[0]), 2);
/// ```
fn evaluate<P: Wander>(
&self,
pab: crate::types::optics::Indexed<'a, P, I, A, B>,
) -> Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, T>);
}
/// An indexed getter optic.
#[document_type_parameters(
"The lifetime of the values.",
"The index type.",
"The source type of the structure.",
"The focus type."
)]
#[document_parameters("The optic instance.")]
pub trait IndexedGetterOptic<'a, I: 'a, S: 'a, A: 'a> {
/// Evaluate the optic with the forget profunctor.
#[document_signature]
///
#[document_type_parameters(
"The return type of the forget profunctor.",
"The reference-counted pointer type."
)]
///
#[document_parameters("The indexed profunctor value to transform.")]
///
#[document_returns("The transformed forget profunctor value.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::optics::*,
/// functions::*,
/// types::optics::*,
/// };
///
/// let g: GetterPrime<RcBrand, (i32, String), i32> = GetterPrime::new(|(x, _)| x);
/// let f = Forget::<RcBrand, i32, i32, i32>::new(|x| x);
/// let folded =
/// <GetterPrime<RcBrand, (i32, String), i32> as GetterOptic<(i32, String), i32>>::evaluate::<
/// i32,
/// RcBrand,
/// >(&g, f);
/// assert_eq!(folded.run((42, "hi".to_string())), 42);
/// ```
fn evaluate<R: 'a + 'static, PointerBrand: ToDynCloneFn + 'static>(
&self,
pab: crate::types::optics::Indexed<'a, ForgetBrand<PointerBrand, R>, I, A, A>,
) -> Apply!(<ForgetBrand<PointerBrand, R> as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, S>);
}
/// An indexed fold optic.
#[document_type_parameters(
"The lifetime of the values.",
"The index type.",
"The source type of the structure.",
"The focus type."
)]
#[document_parameters("The optic instance.")]
pub trait IndexedFoldOptic<'a, I: 'a, S: 'a, A: 'a> {
/// Evaluate the optic with the forget profunctor for any monoid.
#[document_signature]
///
#[document_type_parameters("The monoid type.", "The reference-counted pointer type.")]
///
#[document_parameters("The indexed profunctor value to transform.")]
///
#[document_returns("The transformed forget profunctor value.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::{
/// Monoid,
/// optics::*,
/// },
/// functions::*,
/// types::optics::*,
/// };
/// let f_optic: FoldPrime<RcBrand, Vec<i32>, i32, _> =
/// FoldPrime::new(IterableFoldFn(|v: Vec<i32>| v));
/// let f = Forget::<RcBrand, String, i32, i32>::new(|x| x.to_string());
/// let folded = <FoldPrime<RcBrand, Vec<i32>, i32, _> as FoldOptic<Vec<i32>, i32>>::evaluate::<
/// String,
/// RcBrand,
/// >(&f_optic, f);
/// assert_eq!(folded.run(vec![1, 2, 3]), "123".to_string());
/// ```
///
/// ### `R: Clone` Requirement
///
/// The result monoid `R` must implement [`Clone`] because the fold is internally implemented
/// via [`Wander`] with the [`Forget`](crate::types::optics::Forget) profunctor, which stores
/// `R` inside [`Const`](crate::types::const_val::Const). The traversal applies
/// [`IndexedTraversalFunc::apply`](crate::classes::optics::IndexedTraversalFunc::apply) with
/// [`ConstBrand<R>`](crate::brands::ConstBrand) as the applicative, and that
/// requires `Const<R, B>: Clone`, which in turn requires `R: Clone`.
fn evaluate<R: 'a + Monoid + Clone + 'static, PointerBrand: ToDynCloneFn + 'static>(
&self,
pab: crate::types::optics::Indexed<'a, ForgetBrand<PointerBrand, R>, I, A, A>,
) -> Apply!(<ForgetBrand<PointerBrand, R> as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, S>);
}
/// An indexed setter optic.
#[document_type_parameters(
"The lifetime of the values.",
"The reference-counted pointer type.",
"The index type.",
"The source type of the structure.",
"The target type of the structure after an update.",
"The source type of the focus.",
"The target type of the focus after an update."
)]
#[document_parameters("The optic instance.")]
pub trait IndexedSetterOptic<'a, PointerBrand: ToDynCloneFn, I: 'a, S: 'a, T: 'a, A: 'a, B: 'a>
{
/// Evaluate the optic with the function profunctor.
#[document_signature]
///
#[document_parameters("The indexed profunctor value to transform.")]
///
#[document_returns("The transformed function profunctor value.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::optics::*,
/// functions::*,
/// types::optics::*,
/// };
///
/// let s: SetterPrime<RcBrand, (i32, String), i32> =
/// SetterPrime::new(|(s, f): ((i32, String), Box<dyn Fn(i32) -> i32>)| (f(s.0), s.1));
/// let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
/// let modifier = <SetterPrime<RcBrand, (i32, String), i32> as SetterOptic<
/// RcBrand,
/// (i32, String),
/// (i32, String),
/// i32,
/// i32,
/// >>::evaluate(&s, f);
/// assert_eq!(modifier((21, "hi".to_string())), (42, "hi".to_string()));
/// ```
fn evaluate(
&self,
pab: crate::types::optics::Indexed<'a, FnBrand<PointerBrand>, I, A, B>,
) -> Apply!(<FnBrand<PointerBrand> as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, T>);
}
/// A grate optic.
#[document_type_parameters(
"The lifetime of the values.",
"The cloneable function brand used by the profunctor's `Closed` instance.",
"The source type of the structure.",
"The target type of the structure after an update.",
"The source type of the focus.",
"The target type of the focus after an update."
)]
#[document_parameters("The optic instance.")]
pub trait GrateOptic<'a, FunctionBrand: LiftFn, S: 'a, T: 'a, A: 'a, B: 'a> {
/// Evaluate the optic with a closed profunctor.
#[document_signature]
///
#[document_type_parameters("The profunctor type.")]
///
#[document_parameters("The profunctor value to transform.")]
///
#[document_returns("The transformed profunctor value.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::optics::*,
/// functions::*,
/// types::optics::*,
/// };
///
/// // Simple example showing the grate concept
/// let pair = (21, 10);
/// let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
/// assert_eq!(f(pair.0), 42);
/// assert_eq!(f(pair.1), 20);
/// ```
fn evaluate<P: Closed<FunctionBrand>>(
&self,
pab: Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, A, B>),
) -> Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, T>);
}
/// A review optic.
#[document_type_parameters(
"The lifetime of the values.",
"The source type of the structure.",
"The target type of the structure after an update.",
"The source type of the focus.",
"The target type of the focus after an update."
)]
#[document_parameters("The optic instance.")]
pub trait ReviewOptic<'a, S: 'a, T: 'a, A: 'a, B: 'a> {
/// Evaluate the optic with the tagged profunctor.
#[document_signature]
///
#[document_parameters("The profunctor value to transform.")]
///
#[document_returns("The transformed tagged profunctor value.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::optics::*,
/// functions::*,
/// types::optics::*,
/// };
///
/// let r: PrismPrime<RcBrand, Option<i32>, i32> = PrismPrime::from_option(|o| o, Some);
/// let f = Tagged::new(21);
/// let reviewed = <PrismPrime<RcBrand, Option<i32>, i32> as ReviewOptic<
/// Option<i32>,
/// Option<i32>,
/// i32,
/// i32,
/// >>::evaluate(&r, f);
/// assert_eq!(reviewed.0, Some(21));
/// ```
fn evaluate(
&self,
pab: Apply!(<TaggedBrand as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, A, B>),
) -> Apply!(<TaggedBrand as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, T>);
}
/// Helper trait for `optics_un_index`.
#[document_type_parameters(
"The lifetime of the values.",
"The profunctor type.",
"The index type.",
"The source type of the structure.",
"The target type of the structure.",
"The source type of the focus.",
"The target type of the focus."
)]
#[document_parameters("The adapter instance.")]
pub trait IndexedOpticAdapter<'a, P: Profunctor, I, S, T, A, B> {
/// Evaluate the optic with an indexed profunctor.
#[document_signature]
///
#[document_parameters("The indexed profunctor value.")]
///
#[document_returns("The transformed profunctor value.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::optics::*,
/// functions::*,
/// types::optics::*,
/// };
///
/// let l: LensPrime<RcBrand, (i32, String), i32> =
/// LensPrime::from_view_set(|(x, _)| x, |((_, s), x)| (x, s));
/// assert_eq!(l.over((21, "hi".to_string()), |x| x * 2), (42, "hi".to_string()));
/// ```
fn evaluate_indexed(
&self,
pab: crate::types::optics::Indexed<'a, P, I, A, B>,
) -> Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, T>);
}
/// Helper trait for `optics_as_index`.
#[document_type_parameters(
"The lifetime of the values.",
"The profunctor type.",
"The index type.",
"The source type of the structure.",
"The target type of the structure.",
"The source type of the focus.",
"The target type of the focus."
)]
#[document_parameters("The adapter instance.")]
pub trait IndexedOpticAdapterDiscardsFocus<'a, P: Profunctor, I, S, T, A, B> {
/// Evaluate the optic with an indexed profunctor, discarding the focus.
#[document_signature]
///
#[document_parameters("The indexed profunctor value.")]
///
#[document_returns("The transformed profunctor value.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::{
/// optics::*,
/// *,
/// },
/// classes::optics::*,
/// functions::*,
/// types::optics::*,
/// };
///
/// let l: LensPrime<RcBrand, (i32, String), i32> =
/// LensPrime::from_view_set(|(x, _)| x, |((_, s), x)| (x, s));
/// assert_eq!(l.over((21, "hi".to_string()), |x| x * 2), (42, "hi".to_string()));
/// ```
fn evaluate_indexed_discards_focus(
&self,
pab: crate::types::optics::Indexed<'a, P, I, A, B>,
) -> Apply!(<P as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, T>);
}
}
pub use inner::*;