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
//! Qtt - Quantitative Type Theory wrapper
//!
//! > *"Quantitas determinat usum"*
//! > — Quantity determines use. (Neo-Latin)
//!
//! This module provides the `Qtt` wrapper type that encodes multiplicity
//! at the type level, enabling compile-time tracking of value usage.

use core::fmt;
use core::marker::PhantomData;
use core::ops::Deref;

use super::multiplicitas::{Multiplicitas, Nihil, Omega, Semel, Usage};

// =============================================================================
// Qtt - The Core QTT Wrapper
// =============================================================================

/// A value with explicit multiplicity annotation.
///
/// `Qtt<A, M>` wraps a value of type `A` with a type-level multiplicity `M`.
/// The multiplicity constrains how the value can be used:
///
/// - `Qtt<A, Nihil>`: Compile-time-only intent — *meant* to be erased and
///   unused. Note this is advisory: `consume`/`consume_with` are available
///   for every multiplicity (each represents one use), so `Nihil` does not
///   actually prevent consumption; it only forbids duplication.
/// - `Qtt<A, Semel>`: Must be used exactly once
/// - `Qtt<A, Omega>`: Can be used any number of times
///
/// # Type Parameters
///
/// * `A` - The wrapped value type
/// * `M` - The multiplicity marker (`Nihil`, `Semel`, or `Omega`)
///
/// # Example
///
/// ```rust
/// use ordofp_core::quantitative::{Qtt, Semel, Omega};
///
/// // A linear value
/// let linear: Qtt<i32, Semel> = Qtt::new(42);
/// let result = linear.consume(); // Must consume exactly once
///
/// // An unrestricted value
/// let free: Qtt<i32, Omega> = Qtt::new(42);
/// let copy1 = free.dup(); // Can duplicate
/// let copy2 = free.dup();
/// ```
pub struct Qtt<A, M: Usage> {
    value: A,
    _multiplicity: PhantomData<M>,
}

impl<A, M: Usage> Qtt<A, M> {
    /// Create a new quantitative value with the given multiplicity.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::quantitative::{Qtt, Semel};
    ///
    /// let linear: Qtt<i32, Semel> = Qtt::new(42);
    /// ```
    #[inline]
    pub const fn new(value: A) -> Self {
        Qtt {
            value,
            _multiplicity: PhantomData,
        }
    }

    /// Get the runtime multiplicity value.
    #[inline]
    pub const fn multiplicity(&self) -> Multiplicitas {
        M::VALUE
    }

    /// Check if this value can be discarded without use.
    #[inline]
    pub const fn can_discard(&self) -> bool {
        M::ALLOWS_DISCARD
    }

    /// Check if this value can be duplicated.
    #[inline]
    pub const fn can_dup(&self) -> bool {
        M::ALLOWS_DUP
    }

    /// Consume the value, returning the inner value.
    ///
    /// This is always available regardless of multiplicity,
    /// representing one use of the value.
    #[inline]
    pub fn consume(self) -> A {
        self.value
    }

    /// Map a function over the value, preserving multiplicity.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::quantitative::{Qtt, Semel};
    ///
    /// let x: Qtt<i32, Semel> = Qtt::new(5);
    /// let y: Qtt<i32, Semel> = x.fmap(|n| n * 2);
    /// assert_eq!(y.consume(), 10);
    /// ```
    #[inline]
    pub fn fmap<B, F>(self, f: F) -> Qtt<B, M>
    where
        F: FnOnce(A) -> B,
    {
        Qtt::new(f(self.value))
    }

    /// Apply a function to the value, consuming it.
    #[inline]
    pub fn consume_with<B, F>(self, f: F) -> B
    where
        F: FnOnce(A) -> B,
    {
        f(self.value)
    }

    /// Convert to an Option, always returning Some.
    #[inline]
    pub fn into_option(self) -> Option<A> {
        Some(self.value)
    }

    /// Convert to a Result, always returning Ok.
    ///
    /// # Errors
    ///
    /// Never returns `Err`; the error type parameter exists only to satisfy
    /// Result-based call sites. The usage obligation is discharged exactly
    /// once, as the quantity annotation requires.
    #[inline]
    pub fn into_result<E>(self) -> Result<A, E> {
        Ok(self.value)
    }
}

// =============================================================================
// Erased (Nihil) Operations
// =============================================================================

impl<A> Qtt<A, Nihil> {
    /// Create an erased value.
    ///
    /// Erased values exist only at compile time for type checking
    /// and are removed during compilation.
    #[inline]
    pub const fn erased(value: A) -> Self {
        Qtt::new(value)
    }

    /// Witness that erased values can be freely discarded.
    #[inline]
    pub fn discard(self) {
        // Value is simply dropped
    }

    /// Witness that erased values can be "duplicated".
    ///
    /// Since erased values don't exist at runtime, duplication
    /// is trivial.
    #[inline]
    pub fn phantom_dup(&self) -> Qtt<(), Nihil>
    where
        A: Copy,
    {
        Qtt::new(())
    }
}

// =============================================================================
// Linear (Semel) Operations
// =============================================================================

impl<A> Qtt<A, Semel> {
    /// Create a linear value.
    ///
    /// Linear values must be used exactly once.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::quantitative::{Qtt, Semel};
    ///
    /// let linear = Qtt::<i32, Semel>::linear(42);
    /// let result = linear.consume(); // Must use exactly once
    /// ```
    #[inline]
    pub const fn linear(value: A) -> Self {
        Qtt::new(value)
    }

    /// Split a linear value into two parts using a function.
    ///
    /// This is the linear equivalent of pattern matching on a pair.
    #[inline]
    pub fn split<B, C, F>(self, f: F) -> (Qtt<B, Semel>, Qtt<C, Semel>)
    where
        F: FnOnce(A) -> (B, C),
    {
        let (b, c) = f(self.value);
        (Qtt::new(b), Qtt::new(c))
    }

    /// Chain linear computations (monadic bind).
    #[inline]
    pub fn bind_linear<B, F>(self, f: F) -> Qtt<B, Semel>
    where
        F: FnOnce(A) -> Qtt<B, Semel>,
    {
        f(self.value)
    }

    /// Sequence two linear values, keeping the second.
    #[inline]
    pub fn then_linear<B>(self, other: Qtt<B, Semel>) -> Qtt<B, Semel> {
        let _ = self.value;
        other
    }

    /// Convert a linear value to unrestricted if the type allows cloning.
    #[inline]
    pub fn relax(self) -> Qtt<A, Omega>
    where
        A: Clone,
    {
        Qtt::new(self.value)
    }
}

// =============================================================================
// Unrestricted (Omega) Operations
// =============================================================================

impl<A> Qtt<A, Omega> {
    /// Create an unrestricted value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::quantitative::{Qtt, Omega};
    ///
    /// let free = Qtt::<i32, Omega>::unrestricted(42);
    /// let copy = free.dup();
    /// ```
    #[inline]
    pub const fn unrestricted(value: A) -> Self {
        Qtt::new(value)
    }

    /// Duplicate the value.
    ///
    /// Only available for unrestricted values.
    #[inline]
    pub fn dup(&self) -> Qtt<A, Omega>
    where
        A: Clone,
    {
        Qtt::new(self.value.clone())
    }

    /// Discard the value without using it.
    ///
    /// Only available for unrestricted values.
    #[inline]
    pub fn discard(self) {
        // Value is simply dropped
    }

    /// Get a reference to the inner value.
    #[inline]
    pub fn get_ref(&self) -> &A {
        &self.value
    }

    /// Restrict an unrestricted value to linear usage.
    ///
    /// This "forgets" that the value could be duplicated.
    #[inline]
    pub fn restrict(self) -> Qtt<A, Semel> {
        Qtt::new(self.value)
    }

    /// Erase an unrestricted value to zero multiplicity.
    #[inline]
    pub fn erase(self) -> Qtt<A, Nihil> {
        Qtt::new(self.value)
    }
}

// Clone only for Omega - use bitwise copy for Copy types, clone otherwise
// This implementation satisfies both Copy types (bitwise) and non-Copy Clone types,
// so it is deliberately broader than the Copy impl below.
#[allow(clippy::expl_impl_clone_on_copy)]
impl<A: Clone> Clone for Qtt<A, Omega> {
    #[inline]
    fn clone(&self) -> Self {
        Qtt::new(self.value.clone())
    }
}

// Copy only for Omega with Copy inner
impl<A: Copy> Copy for Qtt<A, Omega> {}

// =============================================================================
// Trait Implementations
// =============================================================================

impl<A: PartialEq, M: Usage> PartialEq for Qtt<A, M> {
    fn eq(&self, other: &Self) -> bool {
        self.value == other.value
    }
}

impl<A: Eq, M: Usage> Eq for Qtt<A, M> {}

impl<A: PartialOrd, M: Usage> PartialOrd for Qtt<A, M> {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        self.value.partial_cmp(&other.value)
    }
}

impl<A: Ord, M: Usage> Ord for Qtt<A, M> {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.value.cmp(&other.value)
    }
}

impl<A: core::hash::Hash, M: Usage> core::hash::Hash for Qtt<A, M> {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        self.value.hash(state);
    }
}

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

impl<A: fmt::Debug, M: Usage> fmt::Debug for Qtt<A, M> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Qtt")
            .field("value", &self.value)
            .field("multiplicity", &M::VALUE)
            .finish()
    }
}

impl<A: fmt::Display, M: Usage> fmt::Display for Qtt<A, M> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}", self.value, M::VALUE)
    }
}

impl<A, M: Usage> From<A> for Qtt<A, M> {
    #[inline]
    fn from(value: A) -> Self {
        Qtt::new(value)
    }
}

// Deref only for Omega (unrestricted access)
impl<A> Deref for Qtt<A, Omega> {
    type Target = A;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

// =============================================================================
// Extension Trait
// =============================================================================

/// Extension trait for converting values into Qtt wrappers.
pub trait QttExt: Sized {
    /// Wrap as a linear value.
    #[inline]
    fn into_linear(self) -> Qtt<Self, Semel> {
        Qtt::linear(self)
    }

    /// Wrap as an unrestricted value.
    #[inline]
    fn into_unrestricted(self) -> Qtt<Self, Omega> {
        Qtt::unrestricted(self)
    }

    /// Wrap as an erased value.
    #[inline]
    fn into_erased(self) -> Qtt<Self, Nihil> {
        Qtt::erased(self)
    }
}

impl<T> QttExt for T {}
// =============================================================================

/// A linear value (multiplicity 1).
pub type QttLinearis<A> = Qtt<A, Semel>;

/// An erased value (multiplicity 0).
pub type QttErasum<A> = Qtt<A, Nihil>;

/// An unrestricted value (multiplicity ω).
pub type QttLiber<A> = Qtt<A, Omega>;

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

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

    #[test]
    fn test_linear_creation() {
        let x: Qtt<i32, Semel> = Qtt::linear(42);
        assert_eq!(x.multiplicity(), Multiplicitas::Semel);
        assert!(!x.can_discard());
        assert!(!x.can_dup());
    }

    #[test]
    fn test_linear_consume() {
        let x: Qtt<i32, Semel> = Qtt::linear(42);
        let result = x.consume();
        assert_eq!(result, 42);
    }

    #[test]
    fn test_linear_fmap() {
        let x: Qtt<i32, Semel> = Qtt::linear(5);
        let y = x.fmap(|n| n * 2);
        assert_eq!(y.consume(), 10);
    }

    #[test]
    fn test_linear_bind() {
        let x: Qtt<i32, Semel> = Qtt::linear(5);
        let y = x.bind_linear(|n| Qtt::linear(n + 10));
        assert_eq!(y.consume(), 15);
    }

    #[test]
    fn test_unrestricted_creation() {
        let x: Qtt<i32, Omega> = Qtt::unrestricted(42);
        assert_eq!(x.multiplicity(), Multiplicitas::Omega);
        assert!(x.can_discard());
        assert!(x.can_dup());
    }

    #[test]
    fn test_unrestricted_dup() {
        let x: Qtt<i32, Omega> = Qtt::unrestricted(42);
        let y = x.dup();
        let z = x.dup();
        assert_eq!(y.consume(), 42);
        assert_eq!(z.consume(), 42);
        assert_eq!(x.consume(), 42);
    }

    #[test]
    fn test_unrestricted_discard() {
        let x: Qtt<i32, Omega> = Qtt::unrestricted(42);
        x.discard(); // Should compile and run
    }

    #[test]
    fn test_unrestricted_deref() {
        let x: Qtt<i32, Omega> = Qtt::unrestricted(42);
        assert_eq!(*x, 42);
    }

    #[test]
    fn test_erased_creation() {
        let x: Qtt<i32, Nihil> = Qtt::erased(42);
        assert_eq!(x.multiplicity(), Multiplicitas::Nihil);
        assert!(x.can_discard());
    }

    #[test]
    fn test_erased_discard() {
        let x: Qtt<i32, Nihil> = Qtt::erased(42);
        x.discard(); // Should compile and run
    }

    #[test]
    fn test_linear_relax() {
        let x: Qtt<i32, Semel> = Qtt::linear(42);
        let y: Qtt<i32, Omega> = x.relax();
        assert_eq!(y.multiplicity(), Multiplicitas::Omega);
        assert_eq!(y.consume(), 42);
    }

    #[test]
    fn test_unrestricted_restrict() {
        let x: Qtt<i32, Omega> = Qtt::unrestricted(42);
        let y: Qtt<i32, Semel> = x.restrict();
        assert_eq!(y.multiplicity(), Multiplicitas::Semel);
        assert_eq!(y.consume(), 42);
    }

    #[test]
    fn test_qtt_ext() {
        let x = 42.into_linear();
        assert_eq!(x.consume(), 42);

        let y = 42.into_unrestricted();
        assert_eq!(y.consume(), 42);

        let z = 42.into_erased();
        z.discard();
    }

    #[test]
    fn test_type_aliases() {
        let _linear: QttLinearis<i32> = Qtt::linear(42);
        let _erased: QttErasum<i32> = Qtt::erased(42);
        let _free: QttLiber<i32> = Qtt::unrestricted(42);
    }

    #[test]
    fn test_display() {
        let x: Qtt<i32, Semel> = Qtt::linear(42);
        assert_eq!(format!("{x}"), "42:1");

        let y: Qtt<i32, Omega> = Qtt::unrestricted(42);
        assert_eq!(format!("{y}"), "42:ω");
    }

    #[test]
    fn test_split() {
        let pair: Qtt<(i32, &str), Semel> = Qtt::linear((42, "hello"));
        let (a, b) = pair.split(|(n, s)| (n, s));
        assert_eq!(a.consume(), 42);
        assert_eq!(b.consume(), "hello");
    }

    #[test]
    fn test_chaining() {
        let result = Qtt::linear(5i32)
            .fmap(|x| x * 2)
            .fmap(|x| x + 1)
            .bind_linear(|x: i32| Qtt::linear(x.to_string()))
            .consume();

        assert_eq!(result, "11");
    }

    #[test]
    fn test_clone_omega() {
        let x: Qtt<i32, Omega> = Qtt::unrestricted(42);
        let y = x;
        assert_eq!(x.consume(), 42);
        assert_eq!(y.consume(), 42);
    }

    #[test]
    fn test_copy_omega() {
        let x: Qtt<i32, Omega> = Qtt::unrestricted(42);
        let y = x; // Copy
        assert_eq!(x.consume(), 42);
        assert_eq!(y.consume(), 42);
    }
}