ordofp_core 0.1.0

OrdoFP core provides developers with HList, Disiunctio, NominataUniversalis, Universalis, and functional 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
//! Divisio (Prism) - Focus on a single variant within a sum type (enum).
//!
//! > *"Divisio est distributio generis in species."*
//! > — Division is the distribution of a genus into species. (Scholastic)
//!
//! A divisio provides a way to:
//! - **Preview** a value (returns `Some` if the variant matches, `None` otherwise)
//! - **Review** a value (construct the variant from the inner value)
//! - **Modify** a value if the variant matches
//!
//! Divisio are composable, allowing you to focus on nested enum variants.

/// A divisio (prism) focusing on a value of type `A` within a sum type `S`.
///
/// A divisio is defined by two functions:
/// - `preview: &S -> Option<A>` - Extract the value if the variant matches
/// - `review: A -> S` - Construct the variant from a value
///
/// # Type Parameters
/// - `S` - The source/whole sum type
/// - `A` - The target/part type (the variant's inner value)
/// - `PreviewFn` - The preview function type
/// - `ReviewFn` - The review function type
pub struct Divisio<S, A, PreviewFn, ReviewFn>
where
    PreviewFn: Fn(&S) -> Option<A>,
    ReviewFn: Fn(A) -> S,
{
    preview_fn: PreviewFn,
    review_fn: ReviewFn,
    _phantom: core::marker::PhantomData<fn(&S) -> Option<A>>,
}

impl<S, A, PreviewFn, ReviewFn> Clone for Divisio<S, A, PreviewFn, ReviewFn>
where
    PreviewFn: Fn(&S) -> Option<A> + Clone,
    ReviewFn: Fn(A) -> S + Clone,
{
    fn clone(&self) -> Self {
        Self {
            preview_fn: self.preview_fn.clone(),
            review_fn: self.review_fn.clone(),
            _phantom: core::marker::PhantomData,
        }
    }
}

impl<S, A, PreviewFn, ReviewFn> Divisio<S, A, PreviewFn, ReviewFn>
where
    PreviewFn: Fn(&S) -> Option<A>,
    ReviewFn: Fn(A) -> S,
{
    /// Create a new divisio from preview and review functions.
    ///
    /// # Arguments
    /// - `preview_fn` - Function to extract the value if the variant matches
    /// - `review_fn` - Function to construct the variant from a value
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::optics::Divisio;
    ///
    /// #[derive(Clone, Debug, PartialEq)]
    /// enum Either<L, R> {
    ///     Left(L),
    ///     Right(R),
    /// }
    ///
    /// let left_divisio: Divisio<Either<i32, String>, i32, _, _> = Divisio::new(
    ///     |e| match e {
    ///         Either::Left(l) => Some(*l),
    ///         _ => None,
    ///     },
    ///     Either::Left,
    /// );
    ///
    /// let left: Either<i32, String> = Either::Left(42);
    /// assert_eq!(left_divisio.preview(&left), Some(42));
    /// ```
    #[inline]
    pub fn new(preview_fn: PreviewFn, review_fn: ReviewFn) -> Self {
        Self {
            preview_fn,
            review_fn,
            _phantom: core::marker::PhantomData,
        }
    }

    /// Attempt to extract the focused value from the sum type.
    ///
    /// Returns `Some(value)` if the variant matches, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::optics::divisio;
    ///
    /// #[derive(Clone, Debug, PartialEq)]
    /// enum Shape {
    ///     Circle(f64),
    ///     Rectangle(f64, f64),
    /// }
    ///
    /// let circle_divisio = divisio(
    ///     |s: &Shape| match s {
    ///         Shape::Circle(r) => Some(*r),
    ///         _ => None,
    ///     },
    ///     Shape::Circle,
    /// );
    ///
    /// assert_eq!(circle_divisio.preview(&Shape::Circle(5.0)), Some(5.0));
    /// assert_eq!(circle_divisio.preview(&Shape::Rectangle(3.0, 4.0)), None);
    /// ```
    #[inline]
    pub fn preview(&self, source: &S) -> Option<A> {
        (self.preview_fn)(source)
    }

    /// Construct the variant from a value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::optics::divisio;
    ///
    /// #[derive(Clone, Debug, PartialEq)]
    /// enum Shape {
    ///     Circle(f64),
    ///     Rectangle(f64, f64),
    /// }
    ///
    /// let circle_divisio = divisio(
    ///     |s: &Shape| match s {
    ///         Shape::Circle(r) => Some(*r),
    ///         _ => None,
    ///     },
    ///     Shape::Circle,
    /// );
    ///
    /// assert_eq!(circle_divisio.review(10.0), Shape::Circle(10.0));
    /// ```
    #[inline]
    pub fn review(&self, value: A) -> S {
        (self.review_fn)(value)
    }

    /// Modify the focused value if the variant matches.
    ///
    /// Returns `Some(new_value)` if the variant matched and was modified,
    /// `None` if the variant didn't match.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::optics::divisio;
    ///
    /// #[derive(Clone, Debug, PartialEq)]
    /// enum Shape {
    ///     Circle(f64),
    ///     Rectangle(f64, f64),
    /// }
    ///
    /// let circle_divisio = divisio(
    ///     |s: &Shape| match s {
    ///         Shape::Circle(r) => Some(*r),
    ///         _ => None,
    ///     },
    ///     Shape::Circle,
    /// );
    ///
    /// let circle = Shape::Circle(5.0);
    /// let doubled = circle_divisio.modify(&circle, |r| r * 2.0);
    /// assert_eq!(doubled, Some(Shape::Circle(10.0)));
    ///
    /// let rect = Shape::Rectangle(3.0, 4.0);
    /// let result = circle_divisio.modify(&rect, |r| r * 2.0);
    /// assert_eq!(result, None);
    /// ```
    #[inline]
    pub fn modify<F>(&self, source: &S, f: F) -> Option<S>
    where
        F: FnOnce(A) -> A,
    {
        self.preview(source).map(|value| self.review(f(value)))
    }

    /// Modify the focused value, or return the original if the variant doesn't match.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::optics::divisio;
    ///
    /// #[derive(Clone, Debug, PartialEq)]
    /// enum Shape {
    ///     Circle(f64),
    ///     Rectangle(f64, f64),
    /// }
    ///
    /// let circle_divisio = divisio(
    ///     |s: &Shape| match s {
    ///         Shape::Circle(r) => Some(*r),
    ///         _ => None,
    ///     },
    ///     Shape::Circle,
    /// );
    ///
    /// let circle = Shape::Circle(5.0);
    /// let doubled = circle_divisio.modify_or_identity(&circle, |r| r * 2.0);
    /// assert_eq!(doubled, Shape::Circle(10.0));
    ///
    /// let rect = Shape::Rectangle(3.0, 4.0);
    /// let result = circle_divisio.modify_or_identity(&rect, |r| r * 2.0);
    /// assert_eq!(result, Shape::Rectangle(3.0, 4.0)); // Unchanged
    /// ```
    #[inline]
    pub fn modify_or_identity<F>(&self, source: &S, f: F) -> S
    where
        F: FnOnce(A) -> A,
        S: Clone,
    {
        self.modify(source, f).unwrap_or_else(|| source.clone())
    }

    /// Set a new value if the variant matches.
    ///
    /// Returns `Some(new_value)` if the variant matched,
    /// `None` if the variant didn't match.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::optics::divisio;
    ///
    /// #[derive(Clone, Debug, PartialEq)]
    /// enum Shape {
    ///     Circle(f64),
    ///     Rectangle(f64, f64),
    /// }
    ///
    /// let circle_divisio = divisio(
    ///     |s: &Shape| match s {
    ///         Shape::Circle(r) => Some(*r),
    ///         _ => None,
    ///     },
    ///     Shape::Circle,
    /// );
    ///
    /// let circle = Shape::Circle(5.0);
    /// let updated = circle_divisio.set_if_matches(&circle, 100.0);
    /// assert_eq!(updated, Some(Shape::Circle(100.0)));
    /// ```
    #[inline]
    pub fn set_if_matches(&self, source: &S, value: A) -> Option<S> {
        if self.preview(source).is_some() {
            Some(self.review(value))
        } else {
            None
        }
    }

    /// Check if the variant matches.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::optics::divisio;
    ///
    /// #[derive(Clone, Debug, PartialEq)]
    /// enum Shape {
    ///     Circle(f64),
    ///     Rectangle(f64, f64),
    /// }
    ///
    /// let circle_divisio = divisio(
    ///     |s: &Shape| match s {
    ///         Shape::Circle(r) => Some(*r),
    ///         _ => None,
    ///     },
    ///     Shape::Circle,
    /// );
    ///
    /// assert!(circle_divisio.matches(&Shape::Circle(5.0)));
    /// assert!(!circle_divisio.matches(&Shape::Rectangle(3.0, 4.0)));
    /// ```
    #[inline]
    pub fn matches(&self, source: &S) -> bool {
        self.preview(source).is_some()
    }

    /// Compose this divisio with another divisio to focus on nested variants.
    ///
    /// If `self` focuses on `A` within `S`, and `other` focuses on `B` within `A`,
    /// the composed divisio focuses on `B` within `S`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::optics::divisio;
    ///
    /// #[derive(Clone, Debug, PartialEq)]
    /// enum Outer {
    ///     A(Inner),
    ///     B,
    /// }
    ///
    /// #[derive(Clone, Debug, PartialEq)]
    /// enum Inner {
    ///     X(i32),
    ///     Y,
    /// }
    ///
    /// let a_divisio = divisio(
    ///     |o: &Outer| match o {
    ///         Outer::A(i) => Some(i.clone()),
    ///         _ => None,
    ///     },
    ///     Outer::A,
    /// );
    ///
    /// let x_divisio = divisio(
    ///     |i: &Inner| match i {
    ///         Inner::X(n) => Some(*n),
    ///         _ => None,
    ///     },
    ///     Inner::X,
    /// );
    ///
    /// let a_x = a_divisio.compose(&x_divisio);
    ///
    /// let outer = Outer::A(Inner::X(42));
    /// assert_eq!(a_x.preview(&outer), Some(42));
    ///
    /// let outer_b = Outer::B;
    /// assert_eq!(a_x.preview(&outer_b), None);
    /// ```
    #[inline]
    pub fn compose<B, PreviewFn2, ReviewFn2>(
        &self,
        other: &Divisio<A, B, PreviewFn2, ReviewFn2>,
    ) -> ComposedDivisio<S, A, B, PreviewFn, ReviewFn, PreviewFn2, ReviewFn2>
    where
        PreviewFn: Clone,
        ReviewFn: Clone,
        PreviewFn2: Fn(&A) -> Option<B> + Clone,
        ReviewFn2: Fn(B) -> A + Clone,
    {
        ComposedDivisio {
            outer: self.clone(),
            inner: other.clone(),
        }
    }
}

/// A composed divisio focusing on `B` within `S` through an intermediate `A`.
#[derive(Clone)]
pub struct ComposedDivisio<S, A, B, PreviewFn1, ReviewFn1, PreviewFn2, ReviewFn2>
where
    PreviewFn1: Fn(&S) -> Option<A>,
    ReviewFn1: Fn(A) -> S,
    PreviewFn2: Fn(&A) -> Option<B>,
    ReviewFn2: Fn(B) -> A,
{
    outer: Divisio<S, A, PreviewFn1, ReviewFn1>,
    inner: Divisio<A, B, PreviewFn2, ReviewFn2>,
}

impl<S, A, B, PreviewFn1, ReviewFn1, PreviewFn2, ReviewFn2>
    ComposedDivisio<S, A, B, PreviewFn1, ReviewFn1, PreviewFn2, ReviewFn2>
where
    PreviewFn1: Fn(&S) -> Option<A>,
    ReviewFn1: Fn(A) -> S,
    PreviewFn2: Fn(&A) -> Option<B>,
    ReviewFn2: Fn(B) -> A,
{
    /// Attempt to extract the focused value.
    #[inline]
    pub fn preview(&self, source: &S) -> Option<B> {
        self.outer
            .preview(source)
            .and_then(|a| self.inner.preview(&a))
    }

    /// Construct the nested variant from a value.
    #[inline]
    pub fn review(&self, value: B) -> S {
        self.outer.review(self.inner.review(value))
    }

    /// Modify the focused value if both variants match.
    #[inline]
    pub fn modify<F>(&self, source: &S, f: F) -> Option<S>
    where
        F: FnOnce(B) -> B,
    {
        self.preview(source).map(|value| self.review(f(value)))
    }

    /// Check if both variants match.
    #[inline]
    pub fn matches(&self, source: &S) -> bool {
        self.preview(source).is_some()
    }
}

/// Create a new divisio from preview and review functions.
///
/// This is a convenience function equivalent to `Divisio::new`.
///
/// # Example
///
/// ```rust
/// use ordofp_core::optics::divisio;
///
/// #[derive(Clone, Debug, PartialEq)]
/// enum Option2<T> {
///     Some(T),
///     None,
/// }
///
/// let some_divisio = divisio(
///     |o: &Option2<i32>| match o {
///         Option2::Some(x) => Some(*x),
///         _ => None,
///     },
///     Option2::Some,
/// );
///
/// assert_eq!(some_divisio.preview(&Option2::Some(42)), Some(42));
/// assert_eq!(some_divisio.preview(&Option2::None), None);
/// assert_eq!(some_divisio.review(100), Option2::Some(100));
/// ```
#[inline]
pub fn divisio<S, A, PreviewFn, ReviewFn>(
    preview_fn: PreviewFn,
    review_fn: ReviewFn,
) -> Divisio<S, A, PreviewFn, ReviewFn>
where
    PreviewFn: Fn(&S) -> Option<A>,
    ReviewFn: Fn(A) -> S,
{
    Divisio::new(preview_fn, review_fn)
}

/// A divisio that returns references instead of cloning.
///
/// This is useful when you only need to inspect values.
pub struct DivisioRef<S, A, PreviewFn>
where
    PreviewFn: Fn(&S) -> Option<&A>,
{
    preview_fn: PreviewFn,
    // `fn(&S) -> Option<&A>` is always `Clone`/`Copy`, so (unlike
    // `PhantomData<(S, A)>` with `#[derive(Clone)]`) cloning does not
    // spuriously require `S: Clone`.
    _phantom: core::marker::PhantomData<fn(&S) -> Option<&A>>,
}

impl<S, A, PreviewFn> Clone for DivisioRef<S, A, PreviewFn>
where
    PreviewFn: Fn(&S) -> Option<&A> + Clone,
{
    #[inline]
    fn clone(&self) -> Self {
        Self {
            preview_fn: self.preview_fn.clone(),
            _phantom: core::marker::PhantomData,
        }
    }
}

impl<S, A, PreviewFn> DivisioRef<S, A, PreviewFn>
where
    PreviewFn: Fn(&S) -> Option<&A>,
{
    /// Create a new reference divisio.
    #[inline]
    pub fn new(preview_fn: PreviewFn) -> Self {
        Self {
            preview_fn,
            _phantom: core::marker::PhantomData,
        }
    }

    /// Get a reference to the focused value if the variant matches.
    #[inline]
    pub fn preview<'a>(&self, source: &'a S) -> Option<&'a A> {
        (self.preview_fn)(source)
    }

    /// Check if the variant matches.
    #[inline]
    pub fn matches(&self, source: &S) -> bool {
        self.preview(source).is_some()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    extern crate alloc;
    use alloc::string::{String, ToString};

    #[derive(Clone, Debug, PartialEq)]
    enum Either<L, R> {
        Left(L),
        Right(R),
    }

    #[derive(Clone, Debug, PartialEq)]
    enum Nested {
        A(Either<i32, String>),
        B,
    }

    #[test]
    fn test_divisio_basic() {
        let left_divisio: Divisio<Either<i32, String>, i32, _, _> = divisio(
            |e| match e {
                Either::Left(l) => Some(*l),
                _ => None,
            },
            Either::Left,
        );

        let left: Either<i32, String> = Either::Left(42);
        let right: Either<i32, String> = Either::Right("hello".to_string());

        assert_eq!(left_divisio.preview(&left), Some(42));
        assert_eq!(left_divisio.preview(&right), None);
        assert_eq!(left_divisio.review(100), Either::Left(100));
    }

    #[test]
    fn test_divisio_modify() {
        let left_divisio: Divisio<Either<i32, String>, i32, _, _> = divisio(
            |e| match e {
                Either::Left(l) => Some(*l),
                _ => None,
            },
            Either::Left,
        );

        let left: Either<i32, String> = Either::Left(42);
        let right: Either<i32, String> = Either::Right("hello".to_string());

        assert_eq!(
            left_divisio.modify(&left, |x| x * 2),
            Some(Either::Left(84))
        );
        assert_eq!(left_divisio.modify(&right, |x| x * 2), None);
    }

    #[test]
    fn test_divisio_composition() {
        let a_divisio: Divisio<Nested, Either<i32, String>, _, _> = divisio(
            |n| match n {
                Nested::A(e) => Some(e.clone()),
                _ => None,
            },
            Nested::A,
        );

        let left_divisio: Divisio<Either<i32, String>, i32, _, _> = divisio(
            |e| match e {
                Either::Left(l) => Some(*l),
                _ => None,
            },
            Either::Left,
        );

        let composed = a_divisio.compose(&left_divisio);

        let nested_left = Nested::A(Either::Left(42));
        let nested_right = Nested::A(Either::Right("hello".to_string()));
        let nested_b = Nested::B;

        assert_eq!(composed.preview(&nested_left), Some(42));
        assert_eq!(composed.preview(&nested_right), None);
        assert_eq!(composed.preview(&nested_b), None);
        assert_eq!(composed.review(100), Nested::A(Either::Left(100)));
    }

    #[test]
    fn test_divisio_ref() {
        let left_ref: DivisioRef<Either<i32, String>, i32, _> = DivisioRef::new(|e| match e {
            Either::Left(l) => Some(l),
            _ => None,
        });

        let left: Either<i32, String> = Either::Left(42);
        let right: Either<i32, String> = Either::Right("hello".to_string());

        assert_eq!(left_ref.preview(&left), Some(&42));
        assert_eq!(left_ref.preview(&right), None);
        assert!(left_ref.matches(&left));
        assert!(!left_ref.matches(&right));
    }
}