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
//! Type-Level Peano Natural Numbers
//!
//! > *"Numerorum omnium principium est unitas."*
//! > — The principle of all numbers is unity. (Boethius)
//!
//! This module provides type-level natural numbers using the Peano encoding,
//! enabling compile-time arithmetic and length tracking.
//!
//! # Design
//!
//! Peano numbers are defined inductively:
//! - `Zero` represents 0
//! - `Succ<N>` represents N + 1
//!
//! This allows the type system to reason about quantities at compile time.
//!
//! # Example
//!
//! ```rust
//! use ordofp_core::dependent::peano::{Zero, Succ, N1, N2, N3, Naturalis};
//!
//! // Type-level numbers
//! type Five = Succ<Succ<Succ<Succ<Succ<Zero>>>>>;
//!
//! // Get runtime value
//! let n: usize = <N3 as Naturalis>::VALUE;
//! assert_eq!(n, 3);
//! ```

use core::marker::PhantomData;
use core::ops::Add;

// =============================================================================
// Core Peano Types
// =============================================================================

/// Type-level zero.
///
/// # Latin Etymology
/// *Nihil* means "nothing" - representing the absence of quantity.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Zero;

/// Type-level successor (N + 1).
///
/// # Latin Etymology
/// *Successor* means "one who follows" - representing the next natural number.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Succ<N>(PhantomData<N>);

impl<N> Succ<N> {
    /// Create a new successor type.
    pub const fn new() -> Self {
        Succ(PhantomData)
    }
}

// =============================================================================
// Type Aliases for Common Numbers
// =============================================================================

/// Type alias for 1
pub type N1 = Succ<Zero>;
/// Type alias for 2
pub type N2 = Succ<N1>;
/// Type alias for 3
pub type N3 = Succ<N2>;
/// Type alias for 4
pub type N4 = Succ<N3>;
/// Type alias for 5
pub type N5 = Succ<N4>;
/// Type alias for 6
pub type N6 = Succ<N5>;
/// Type alias for 7
pub type N7 = Succ<N6>;
/// Type alias for 8
pub type N8 = Succ<N7>;
/// Type alias for 9
pub type N9 = Succ<N8>;
/// Type alias for 10
pub type N10 = Succ<N9>;

// =============================================================================
// Naturalis Trait - Runtime Value Extraction
// =============================================================================

/// Trait for type-level natural numbers.
///
/// Provides the ability to get the runtime value of a type-level number.
///
/// # Latin Etymology
/// *Naturalis* means "natural" - as in natural numbers.
pub trait Naturalis {
    /// The runtime value of this type-level number.
    const VALUE: usize;

    /// Get the value at runtime.
    #[inline]
    fn value() -> usize {
        Self::VALUE
    }
}

impl Naturalis for Zero {
    const VALUE: usize = 0;
}

impl<N: Naturalis> Naturalis for Succ<N> {
    const VALUE: usize = N::VALUE + 1;
}

// =============================================================================
// NonNihil - Proof of Non-Zero
// =============================================================================

/// Marker trait for non-zero naturals.
///
/// # Latin Etymology
/// *Non Nihil* means "not nothing" - a non-zero number.
pub trait NonNihil: Naturalis {}

impl<N> NonNihil for Succ<N> where Succ<N>: Naturalis {}

// =============================================================================
// Type-Level Arithmetic
// =============================================================================

/// Type-level addition.
///
/// Computes `N + M` at the type level.
///
/// # Latin Etymology
/// *Additio* means "addition".
pub trait Additio<M>: Naturalis {
    /// The result of adding M to Self.
    type Summa: Naturalis;
}

impl<M: Naturalis> Additio<M> for Zero {
    type Summa = M;
}

impl<N: Naturalis + Additio<M>, M: Naturalis> Additio<M> for Succ<N>
where
    <N as Additio<M>>::Summa: Naturalis,
{
    type Summa = Succ<<N as Additio<M>>::Summa>;
}

/// Type-level multiplication.
///
/// Computes `N * M` at the type level.
///
/// # Latin Etymology
/// *Multiplicatio* means "multiplication".
pub trait Multiplicatio<M>: Naturalis {
    /// The result of multiplying Self by M.
    type Productum: Naturalis;
}

impl<M: Naturalis> Multiplicatio<M> for Zero {
    type Productum = Zero;
}

impl<N, M> Multiplicatio<M> for Succ<N>
where
    N: Naturalis + Multiplicatio<M>,
    M: Naturalis + Additio<<N as Multiplicatio<M>>::Productum>,
    <N as Multiplicatio<M>>::Productum: Naturalis,
    <M as Additio<<N as Multiplicatio<M>>::Productum>>::Summa: Naturalis,
{
    // (n+1) * m = m + (n * m)
    type Productum = <M as Additio<<N as Multiplicatio<M>>::Productum>>::Summa;
}

// =============================================================================
// Type-Level Comparison
// =============================================================================

/// Type-level less-than comparison.
///
/// `Minor<M>` is implemented when `Self < M`.
///
/// # Latin Etymology
/// *Minor* means "smaller, less".
pub trait Minor<M>: Naturalis {}

// Zero is less than any successor
impl<M: Naturalis> Minor<Succ<M>> for Zero {}

// Succ<N> < Succ<M> when N < M
impl<N: Naturalis + Minor<M>, M: Naturalis> Minor<Succ<M>> for Succ<N> {}

/// Type-level less-than-or-equal comparison.
///
/// `MinorVelAequus<M>` is implemented when `Self <= M`.
///
/// # Latin Etymology
/// *Minor vel Aequus* means "smaller or equal".
pub trait MinorVelAequus<M>: Naturalis {}

// 0 <= anything
impl<M: Naturalis> MinorVelAequus<M> for Zero {}

// Succ<N> <= Succ<M> when N <= M
impl<N: Naturalis + MinorVelAequus<M>, M: Naturalis> MinorVelAequus<Succ<M>> for Succ<N> {}

/// Type-level greater-than comparison.
///
/// `Maior<M>` is implemented when `Self > M`.
///
/// # Latin Etymology
/// *Maior* means "greater, larger".
pub trait Maior<M>: Naturalis {}

// Any successor is greater than zero
impl<N: Naturalis> Maior<Zero> for Succ<N> {}

// Succ<N> > Succ<M> when N > M
impl<N: Naturalis + Maior<M>, M: Naturalis> Maior<Succ<M>> for Succ<N> {}

/// Type-level equality.
///
/// `Aequus<M>` is implemented when `Self == M`.
///
/// # Latin Etymology
/// *Aequus* means "equal, level".
pub trait Aequus<M>: Naturalis {}

impl Aequus<Zero> for Zero {}

impl<N: Naturalis + Aequus<M>, M: Naturalis> Aequus<Succ<M>> for Succ<N> {}

// =============================================================================
// Type-Level Predecessor
// =============================================================================

/// Type-level predecessor (N - 1).
///
/// Only implemented for non-zero numbers.
///
/// # Latin Etymology
/// *Praecessor* means "one who goes before".
pub trait Praecessor: NonNihil {
    /// The predecessor type (N - 1).
    type Prior: Naturalis;
}

impl<N: Naturalis> Praecessor for Succ<N> {
    type Prior = N;
}

// =============================================================================
// Type-Level Subtraction (Saturating)
// =============================================================================

/// Type-level saturating subtraction.
///
/// Computes `max(Self - M, 0)` at the type level.
///
/// # Latin Etymology
/// *Subtractio* means "subtraction, taking away".
pub trait Subtractio<M>: Naturalis {
    /// The result of subtracting M from Self (saturating at zero).
    type Differentia: Naturalis;
}

// n - 0 = n
impl<N: Naturalis> Subtractio<Zero> for N {
    type Differentia = N;
}

// 0 - m = 0 (saturating)
impl<M: Naturalis> Subtractio<Succ<M>> for Zero {
    type Differentia = Zero;
}

// (n+1) - (m+1) = n - m
impl<N, M> Subtractio<Succ<M>> for Succ<N>
where
    N: Naturalis + Subtractio<M>,
    M: Naturalis,
{
    type Differentia = <N as Subtractio<M>>::Differentia;
}

// =============================================================================
// Type-Level Minimum and Maximum
// =============================================================================

/// Type-level minimum.
///
/// # Latin Etymology
/// *Minimus* means "smallest".
pub trait Minimus<M>: Naturalis {
    /// The smaller of Self and M.
    type Min: Naturalis;
}

impl<M: Naturalis> Minimus<M> for Zero {
    type Min = Zero;
}

impl<N: Naturalis> Minimus<Zero> for Succ<N> {
    type Min = Zero;
}

impl<N, M> Minimus<Succ<M>> for Succ<N>
where
    N: Naturalis + Minimus<M>,
    M: Naturalis,
    <N as Minimus<M>>::Min: Naturalis,
{
    type Min = Succ<<N as Minimus<M>>::Min>;
}

/// Type-level maximum.
///
/// # Latin Etymology
/// *Maximus* means "greatest".
pub trait Maximus<M>: Naturalis {
    /// The greater of Self and M.
    type Max: Naturalis;
}

impl<M: Naturalis> Maximus<M> for Zero {
    type Max = M;
}

impl<N: Naturalis> Maximus<Zero> for Succ<N> {
    type Max = Succ<N>;
}

impl<N, M> Maximus<Succ<M>> for Succ<N>
where
    N: Naturalis + Maximus<M>,
    M: Naturalis,
    <N as Maximus<M>>::Max: Naturalis,
{
    type Max = Succ<<N as Maximus<M>>::Max>;
}

// =============================================================================
// Convenience Type Operators
// =============================================================================

/// Convenience type alias for addition.
pub type Sum<A, B> = <A as Additio<B>>::Summa;

/// Convenience type alias for multiplication.
pub type Prod<A, B> = <A as Multiplicatio<B>>::Productum;

/// Convenience type alias for subtraction.
pub type Diff<A, B> = <A as Subtractio<B>>::Differentia;

/// Convenience type alias for minimum.
pub type Min<A, B> = <A as Minimus<B>>::Min;

/// Convenience type alias for maximum.
pub type Max<A, B> = <A as Maximus<B>>::Max;

/// Convenience type alias for predecessor.
pub type Pred<N> = <N as Praecessor>::Prior;

// =============================================================================
// std::ops implementations for value-level convenience
// =============================================================================

impl Add<Zero> for Zero {
    type Output = Zero;
    #[inline]
    fn add(self, _: Zero) -> Zero {
        Zero
    }
}

impl<N> Add<Succ<N>> for Zero {
    type Output = Succ<N>;
    #[inline]
    fn add(self, rhs: Succ<N>) -> Succ<N> {
        rhs
    }
}

impl<N, M> Add<M> for Succ<N>
where
    N: Add<M>,
{
    type Output = Succ<N::Output>;
    #[inline]
    fn add(self, _rhs: M) -> Self::Output {
        Succ::new()
    }
}

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

#[cfg(test)]
// Witness fns like `is_less_than<N: Minor<M>, M>()` use their type params only
// as bounds — instantiating them is the compile-time proof.
#[allow(clippy::extra_unused_type_parameters)]
mod tests {
    use super::*;

    #[test]
    fn test_naturalis_value() {
        assert_eq!(Zero::VALUE, 0);
        assert_eq!(<N1 as Naturalis>::VALUE, 1);
        assert_eq!(<N2 as Naturalis>::VALUE, 2);
        assert_eq!(<N3 as Naturalis>::VALUE, 3);
        assert_eq!(<N5 as Naturalis>::VALUE, 5);
        assert_eq!(<N10 as Naturalis>::VALUE, 10);
    }

    #[test]
    fn test_non_nihil() {
        fn requires_non_zero<N: NonNihil>() -> usize {
            N::VALUE
        }

        assert_eq!(requires_non_zero::<N1>(), 1);
        assert_eq!(requires_non_zero::<N5>(), 5);
        // This would not compile:
        // requires_non_zero::<Zero>();
    }

    #[test]
    fn test_additio() {
        // 0 + 3 = 3
        type ZeroPlusThree = Sum<Zero, N3>;
        assert_eq!(<ZeroPlusThree as Naturalis>::VALUE, 3);

        // 2 + 3 = 5
        type TwoPlusThree = Sum<N2, N3>;
        assert_eq!(<TwoPlusThree as Naturalis>::VALUE, 5);

        // 3 + 0 = 3
        type ThreePlusZero = Sum<N3, Zero>;
        assert_eq!(<ThreePlusZero as Naturalis>::VALUE, 3);
    }

    #[test]
    fn test_multiplicatio() {
        // 0 * 5 = 0
        type ZeroTimesFive = Prod<Zero, N5>;
        assert_eq!(<ZeroTimesFive as Naturalis>::VALUE, 0);

        // 2 * 3 = 6
        type TwoTimesThree = Prod<N2, N3>;
        assert_eq!(<TwoTimesThree as Naturalis>::VALUE, 6);

        // 3 * 2 = 6
        type ThreeTimesTwo = Prod<N3, N2>;
        assert_eq!(<ThreeTimesTwo as Naturalis>::VALUE, 6);
    }

    #[test]
    fn test_subtractio() {
        // 5 - 3 = 2
        type FiveMinusThree = Diff<N5, N3>;
        assert_eq!(<FiveMinusThree as Naturalis>::VALUE, 2);

        // 3 - 5 = 0 (saturating)
        type ThreeMinusFive = Diff<N3, N5>;
        assert_eq!(<ThreeMinusFive as Naturalis>::VALUE, 0);

        // 5 - 0 = 5
        type FiveMinusZero = Diff<N5, Zero>;
        assert_eq!(<FiveMinusZero as Naturalis>::VALUE, 5);
    }

    #[test]
    fn test_comparison_minor() {
        fn is_less_than<N: Minor<M>, M>() -> bool {
            true
        }

        // 0 < 1
        assert!(is_less_than::<Zero, N1>());
        // 2 < 5
        assert!(is_less_than::<N2, N5>());
        // 0 < 10
        assert!(is_less_than::<Zero, N10>());
    }

    #[test]
    fn test_comparison_maior() {
        fn is_greater_than<N: Maior<M>, M>() -> bool {
            true
        }

        // 1 > 0
        assert!(is_greater_than::<N1, Zero>());
        // 5 > 2
        assert!(is_greater_than::<N5, N2>());
    }

    #[test]
    fn test_comparison_aequus() {
        fn is_equal<N: Aequus<M>, M>() -> bool {
            true
        }

        assert!(is_equal::<Zero, Zero>());
        assert!(is_equal::<N3, N3>());
        assert!(is_equal::<N10, N10>());
    }

    #[test]
    fn test_min_max() {
        // min(3, 5) = 3
        type MinThreeFive = Min<N3, N5>;
        assert_eq!(<MinThreeFive as Naturalis>::VALUE, 3);

        // max(3, 5) = 5
        type MaxThreeFive = Max<N3, N5>;
        assert_eq!(<MaxThreeFive as Naturalis>::VALUE, 5);

        // min(0, 5) = 0
        type MinZeroFive = Min<Zero, N5>;
        assert_eq!(<MinZeroFive as Naturalis>::VALUE, 0);

        // max(0, 5) = 5
        type MaxZeroFive = Max<Zero, N5>;
        assert_eq!(<MaxZeroFive as Naturalis>::VALUE, 5);
    }

    #[test]
    fn test_praecessor() {
        type PredFive = Pred<N5>;
        assert_eq!(<PredFive as Naturalis>::VALUE, 4);

        type PredOne = Pred<N1>;
        assert_eq!(<PredOne as Naturalis>::VALUE, 0);
    }

    #[test]
    fn test_ops_add() {
        let _sum: Succ<Succ<Zero>> = Zero + Succ::<Succ<Zero>>::new();
        let _sum2: Succ<Succ<Succ<Zero>>> = Succ::<Zero>::new() + Succ::<Succ<Zero>>::new();
    }

    /// `MinorVelAequus` (≤) must hold both for strict inequality (`N < M`)
    /// and the boundary case (`N == M`).  The `Zero ≤ anything` base case
    /// and the reflexive case `N ≤ N` are the two most important edge cases
    /// not covered by the `Minor` / `Maior` tests above.
    #[test]
    fn test_comparison_minor_vel_aequus() {
        fn is_leq<N: MinorVelAequus<M>, M>() -> bool {
            true
        }

        // Base case: 0 <= 0 (reflexive equality at zero)
        assert!(is_leq::<Zero, Zero>());
        // 0 <= any successor (Zero is less than all Succ<_>)
        assert!(is_leq::<Zero, N1>());
        assert!(is_leq::<Zero, N5>());
        // Strict inequality: 2 <= 5
        assert!(is_leq::<N2, N5>());
        // Boundary / reflexive: N <= N for a non-zero number
        assert!(is_leq::<N3, N3>());
        assert!(is_leq::<N5, N5>());
    }
}