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
//! `ParLinearis` - Linear pairs and tensor products
//!
//! > *"Duo simul consumenda"*
//! > — Two to be consumed together. (Neo-Latin)
//!
//! This module provides linear pair types corresponding to linear logic's
//! multiplicative and additive products.

use core::fmt;

// =============================================================================
// ParLinearis - Tensor Product (⊗)
// =============================================================================

/// A linear pair (tensor product) where both components must be used.
///
/// In linear logic, `A ⊗ B` (tensor product) means "use both A and B".
/// This is the multiplicative conjunction - both resources are available
/// and both must be consumed.
///
/// # Latin Etymology
///
/// *Par* = pair, equal
///
/// # Example
///
/// ```rust
/// use ordofp_core::quantitative::ParLinearis;
///
/// let pair = ParLinearis::new(42, "hello");
///
/// // Must consume both components
/// let (n, s) = pair.split();
/// assert_eq!(n, 42);
/// assert_eq!(s, "hello");
/// ```
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ParLinearis<A, B> {
    first: A,
    second: B,
}

impl<A, B> ParLinearis<A, B> {
    /// Create a new linear pair.
    ///
    /// Both components must eventually be consumed.
    #[inline]
    pub const fn new(first: A, second: B) -> Self {
        ParLinearis { first, second }
    }

    /// Split the pair, consuming it and returning both components.
    ///
    /// This is the elimination rule for tensor products.
    #[inline]
    pub fn split(self) -> (A, B) {
        (self.first, self.second)
    }

    /// Consume the pair with a function that takes both components.
    #[inline]
    pub fn consume_with<C, F>(self, f: F) -> C
    where
        F: FnOnce(A, B) -> C,
    {
        f(self.first, self.second)
    }

    /// Map a function over the first component.
    #[inline]
    pub fn map_first<C, F>(self, f: F) -> ParLinearis<C, B>
    where
        F: FnOnce(A) -> C,
    {
        ParLinearis::new(f(self.first), self.second)
    }

    /// Map a function over the second component.
    #[inline]
    pub fn map_second<C, F>(self, f: F) -> ParLinearis<A, C>
    where
        F: FnOnce(B) -> C,
    {
        ParLinearis::new(self.first, f(self.second))
    }

    /// Map functions over both components.
    #[inline]
    pub fn bimap<C, D, F, G>(self, f: F, g: G) -> ParLinearis<C, D>
    where
        F: FnOnce(A) -> C,
        G: FnOnce(B) -> D,
    {
        ParLinearis::new(f(self.first), g(self.second))
    }

    /// Swap the components.
    #[inline]
    pub fn swap(self) -> ParLinearis<B, A> {
        ParLinearis::new(self.second, self.first)
    }
}

impl<A, B, C> ParLinearis<A, ParLinearis<B, C>> {
    /// Associate a nested tensor to the left.
    ///
    /// `A ⊗ (B ⊗ C) → (A ⊗ B) ⊗ C`
    ///
    /// The mirror of [`ParLinearis::assoc_right`].
    #[inline]
    pub fn assoc_left(self) -> ParLinearis<ParLinearis<A, B>, C> {
        let (a, bc) = self.split();
        let (b, c) = bc.split();
        ParLinearis::new(ParLinearis::new(a, b), c)
    }
}

impl<A, B, C> ParLinearis<ParLinearis<A, B>, C> {
    /// Associate a nested tensor to the right.
    ///
    /// `(A ⊗ B) ⊗ C → A ⊗ (B ⊗ C)`
    #[inline]
    pub fn assoc_right(self) -> ParLinearis<A, ParLinearis<B, C>> {
        let (ab, c) = self.split();
        let (a, b) = ab.split();
        ParLinearis::new(a, ParLinearis::new(b, c))
    }
}

impl<A: Default, B: Default> Default for ParLinearis<A, B> {
    #[inline]
    fn default() -> Self {
        ParLinearis::new(A::default(), B::default())
    }
}

impl<A: fmt::Debug, B: fmt::Debug> fmt::Debug for ParLinearis<A, B> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ParLinearis")
            .field("first", &self.first)
            .field("second", &self.second)
            .finish()
    }
}

impl<A: fmt::Display, B: fmt::Display> fmt::Display for ParLinearis<A, B> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "({}{})", self.first, self.second)
    }
}

impl<A, B> From<(A, B)> for ParLinearis<A, B> {
    #[inline]
    fn from((a, b): (A, B)) -> Self {
        ParLinearis::new(a, b)
    }
}

impl<A, B> From<ParLinearis<A, B>> for (A, B) {
    #[inline]
    fn from(pair: ParLinearis<A, B>) -> Self {
        pair.split()
    }
}

// =============================================================================
// TensorLinearis - Named Tensor Product
// =============================================================================

/// Type alias for tensor product with explicit linear marker.
pub type TensorLinearis<A, B> = ParLinearis<A, B>;

// =============================================================================
// WithLinearis - Additive Product (&)
// =============================================================================

/// An additive product (with) where exactly one component is used.
///
/// In linear logic, `A & B` (with) means "choose to use either A or B".
/// This is the additive conjunction - both resources are available
/// but only one can be consumed.
///
/// # Latin Etymology
///
/// *Cum* = with
///
/// # Example
///
/// ```rust
/// use ordofp_core::quantitative::WithLinearis;
///
/// // Must choose one; each choice consumes the whole pair, so a fresh
/// // `WithLinearis` is needed per choice.
/// let choice_a = WithLinearis::new(42, "hello");
/// let left = choice_a.choose_left(); // Consumes the whole thing
/// assert_eq!(left, 42);
///
/// let choice_b = WithLinearis::new(42, "hello");
/// let right = choice_b.choose_right(); // Alternative
/// assert_eq!(right, "hello");
/// ```
pub struct WithLinearis<A, B> {
    left: A,
    right: B,
}

impl<A, B> WithLinearis<A, B> {
    /// Create a new additive product.
    ///
    /// Exactly one component must eventually be chosen and consumed.
    #[inline]
    pub const fn new(left: A, right: B) -> Self {
        WithLinearis { left, right }
    }

    /// Choose the left component, discarding the right.
    #[inline]
    pub fn choose_left(self) -> A {
        self.left
        // right is dropped
    }

    /// Choose the right component, discarding the left.
    #[inline]
    pub fn choose_right(self) -> B {
        self.right
        // left is dropped
    }

    /// Choose based on a boolean: true = left, false = right.
    #[inline]
    pub fn choose(self, left: bool) -> AdditiveChoice<A, B> {
        if left {
            AdditiveChoice::Left(self.left)
        } else {
            AdditiveChoice::Right(self.right)
        }
    }

    /// Project the left component (non-consuming peek).
    ///
    /// Note: This breaks strict linearity but is useful for inspection.
    #[inline]
    pub fn project_left(&self) -> &A {
        &self.left
    }

    /// Project the right component (non-consuming peek).
    #[inline]
    pub fn project_right(&self) -> &B {
        &self.right
    }

    /// Map a function over the left component.
    #[inline]
    pub fn map_left<C, F>(self, f: F) -> WithLinearis<C, B>
    where
        F: FnOnce(A) -> C,
    {
        WithLinearis::new(f(self.left), self.right)
    }

    /// Map a function over the right component.
    #[inline]
    pub fn map_right<C, F>(self, f: F) -> WithLinearis<A, C>
    where
        F: FnOnce(B) -> C,
    {
        WithLinearis::new(self.left, f(self.right))
    }

    /// Map functions over both components (lazy).
    #[inline]
    pub fn bimap<C, D, F, G>(self, f: F, g: G) -> WithLinearis<C, D>
    where
        F: FnOnce(A) -> C,
        G: FnOnce(B) -> D,
    {
        WithLinearis::new(f(self.left), g(self.right))
    }

    /// Swap the components.
    #[inline]
    pub fn swap(self) -> WithLinearis<B, A> {
        WithLinearis::new(self.right, self.left)
    }
}

impl<A: Clone, B: Clone> Clone for WithLinearis<A, B> {
    #[inline]
    fn clone(&self) -> Self {
        WithLinearis::new(self.left.clone(), self.right.clone())
    }
}

impl<A: fmt::Debug, B: fmt::Debug> fmt::Debug for WithLinearis<A, B> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("WithLinearis")
            .field("left", &self.left)
            .field("right", &self.right)
            .finish()
    }
}

impl<A: fmt::Display, B: fmt::Display> fmt::Display for WithLinearis<A, B> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "({} & {})", self.left, self.right)
    }
}

// =============================================================================
// AdditiveChoice - Result of choosing from With
// =============================================================================

/// The result of choosing from an additive product.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AdditiveChoice<A, B> {
    /// The left component was chosen.
    Left(A),
    /// The right component was chosen.
    Right(B),
}

impl<A, B> AdditiveChoice<A, B> {
    /// Check if the left was chosen.
    #[inline]
    pub const fn is_left(&self) -> bool {
        matches!(self, AdditiveChoice::Left(_))
    }

    /// Check if the right was chosen.
    #[inline]
    pub const fn is_right(&self) -> bool {
        matches!(self, AdditiveChoice::Right(_))
    }

    /// Get the left value if present.
    #[inline]
    pub fn left(self) -> Option<A> {
        match self {
            AdditiveChoice::Left(a) => Some(a),
            AdditiveChoice::Right(_) => None,
        }
    }

    /// Get the right value if present.
    #[inline]
    pub fn right(self) -> Option<B> {
        match self {
            AdditiveChoice::Left(_) => None,
            AdditiveChoice::Right(b) => Some(b),
        }
    }

    /// Map functions over both variants.
    #[inline]
    pub fn bimap<C, D, F, G>(self, f: F, g: G) -> AdditiveChoice<C, D>
    where
        F: FnOnce(A) -> C,
        G: FnOnce(B) -> D,
    {
        match self {
            AdditiveChoice::Left(a) => AdditiveChoice::Left(f(a)),
            AdditiveChoice::Right(b) => AdditiveChoice::Right(g(b)),
        }
    }

    /// Fold the choice into a single value.
    #[inline]
    pub fn fold<C, F, G>(self, f: F, g: G) -> C
    where
        F: FnOnce(A) -> C,
        G: FnOnce(B) -> C,
    {
        match self {
            AdditiveChoice::Left(a) => f(a),
            AdditiveChoice::Right(b) => g(b),
        }
    }
}

impl<A: fmt::Display, B: fmt::Display> fmt::Display for AdditiveChoice<A, B> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AdditiveChoice::Left(a) => write!(f, "Left({a})"),
            AdditiveChoice::Right(b) => write!(f, "Right({b})"),
        }
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn assoc_left_reassociates_and_round_trips() {
        let nested = ParLinearis::new(1, ParLinearis::new("b", 3.0)); // A ⊗ (B ⊗ C)
        let left = nested.assoc_left(); // (A ⊗ B) ⊗ C
        let ((a, b), c) = {
            let (ab, c) = left.split();
            (ab.split(), c)
        };
        assert_eq!((a, b, c), (1, "b", 3.0));

        // Round trip: assoc_right undoes assoc_left.
        let back = ParLinearis::new(ParLinearis::new(1, "b"), 3.0).assoc_right();
        let (a2, bc) = back.split();
        let (b2, c2) = bc.split();
        assert_eq!((a2, b2, c2), (1, "b", 3.0));
    }

    #[test]
    fn test_par_linearis_new_split() {
        let pair = ParLinearis::new(42, "hello");
        let (n, s) = pair.split();
        assert_eq!(n, 42);
        assert_eq!(s, "hello");
    }

    #[test]
    fn test_par_linearis_consume_with() {
        let pair = ParLinearis::new(5, 10);
        let result = pair.consume_with(|a, b| a + b);
        assert_eq!(result, 15);
    }

    #[test]
    fn test_par_linearis_map_first() {
        let pair = ParLinearis::new(5, "hello");
        let mapped = pair.map_first(|x| x * 2);
        let (n, s) = mapped.split();
        assert_eq!(n, 10);
        assert_eq!(s, "hello");
    }

    #[test]
    fn test_par_linearis_map_second() {
        let pair = ParLinearis::new(42, 5);
        let mapped = pair.map_second(|x| x * 2);
        let (n, m) = mapped.split();
        assert_eq!(n, 42);
        assert_eq!(m, 10);
    }

    #[test]
    fn test_par_linearis_bimap() {
        let pair = ParLinearis::new(5, 10);
        let mapped = pair.bimap(|x| x * 2, |y| y + 1);
        let (a, b) = mapped.split();
        assert_eq!(a, 10);
        assert_eq!(b, 11);
    }

    #[test]
    fn test_par_linearis_swap() {
        let pair = ParLinearis::new(42, "hello");
        let swapped = pair.swap();
        let (s, n) = swapped.split();
        assert_eq!(s, "hello");
        assert_eq!(n, 42);
    }

    #[test]
    fn test_par_linearis_from_tuple() {
        let pair: ParLinearis<i32, &str> = (42, "hello").into();
        let (n, s) = pair.split();
        assert_eq!(n, 42);
        assert_eq!(s, "hello");
    }

    #[test]
    fn test_par_linearis_into_tuple() {
        let pair = ParLinearis::new(42, "hello");
        let tuple: (i32, &str) = pair.into();
        assert_eq!(tuple, (42, "hello"));
    }

    #[test]
    fn test_par_linearis_display() {
        let pair = ParLinearis::new(42, "hello");
        let s = alloc::format!("{pair}");
        assert_eq!(s, "(42 ⊗ hello)");
    }

    #[test]
    fn test_par_linearis_assoc_right() {
        let nested = ParLinearis::new(ParLinearis::new(1, 2), 3);
        let assoc = nested.assoc_right();
        let (a, bc) = assoc.split();
        let (b, c) = bc.split();
        assert_eq!(a, 1);
        assert_eq!(b, 2);
        assert_eq!(c, 3);
    }

    // WithLinearis tests

    #[test]
    fn test_with_linearis_choose_left() {
        let choice = WithLinearis::new(42, "hello");
        let left = choice.choose_left();
        assert_eq!(left, 42);
    }

    #[test]
    fn test_with_linearis_choose_right() {
        let choice = WithLinearis::new(42, "hello");
        let right = choice.choose_right();
        assert_eq!(right, "hello");
    }

    #[test]
    fn test_with_linearis_choose_bool() {
        let choice1 = WithLinearis::new(42, "hello");
        let result1 = choice1.choose(true);
        assert!(result1.is_left());

        let choice2 = WithLinearis::new(42, "hello");
        let result2 = choice2.choose(false);
        assert!(result2.is_right());
    }

    #[test]
    fn test_with_linearis_project() {
        let choice = WithLinearis::new(42, "hello");
        assert_eq!(*choice.project_left(), 42);
        assert_eq!(*choice.project_right(), "hello");
    }

    #[test]
    fn test_with_linearis_map() {
        let choice = WithLinearis::new(5, 10);
        let mapped = choice.map_left(|x| x * 2).map_right(|x| x + 1);
        assert_eq!(mapped.choose_left(), 10);
    }

    #[test]
    fn test_with_linearis_swap() {
        let choice = WithLinearis::new(42, "hello");
        let swapped = choice.swap();
        assert_eq!(swapped.choose_left(), "hello");
    }

    #[test]
    fn test_with_linearis_display() {
        let choice = WithLinearis::new(42, "hello");
        let s = alloc::format!("{choice}");
        assert_eq!(s, "(42 & hello)");
    }

    // AdditiveChoice tests

    #[test]
    fn test_additive_choice_left() {
        let choice: AdditiveChoice<i32, &str> = AdditiveChoice::Left(42);
        assert!(choice.is_left());
        assert!(!choice.is_right());
        assert_eq!(choice.left(), Some(42));
    }

    #[test]
    fn test_additive_choice_right() {
        let choice: AdditiveChoice<i32, &str> = AdditiveChoice::Right("hello");
        assert!(!choice.is_left());
        assert!(choice.is_right());
        assert_eq!(choice.right(), Some("hello"));
    }

    #[test]
    fn test_additive_choice_bimap() {
        let choice: AdditiveChoice<i32, i32> = AdditiveChoice::Left(5);
        let mapped = choice.bimap(|x| x * 2, |x| x + 1);
        assert_eq!(mapped.left(), Some(10));
    }

    #[test]
    fn test_additive_choice_fold() {
        let left: AdditiveChoice<i32, &str> = AdditiveChoice::Left(42);
        let result = left.fold(|n| n.to_string(), std::string::ToString::to_string);
        assert_eq!(result, "42");

        let right: AdditiveChoice<i32, &str> = AdditiveChoice::Right("hello");
        let result = right.fold(|n| n.to_string(), std::string::ToString::to_string);
        assert_eq!(result, "hello");
    }
}