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
//! Predicate Combinators
//!
//! > *"Ex pluribus unum"*
//! > — From many, one. (Latin)
//!
//! This module provides combinators for composing predicates using
//! logical operators (and, or, not).

use core::marker::PhantomData;

use super::Praedicatum;

// =============================================================================
// Not Combinator
// =============================================================================

/// Negation of a predicate.
///
/// `Non<P>` is true when `P` is false.
///
/// # Latin Etymology
/// *Non* = not.
pub struct Non<P> {
    _predicate: PhantomData<P>,
}

impl<T, P: Praedicatum<T>> Praedicatum<T> for Non<P> {
    #[inline]
    fn check(value: &T) -> bool {
        !P::check(value)
    }

    #[inline]
    fn description() -> &'static str {
        "NOT condition"
    }

    #[inline]
    fn name() -> &'static str {
        "Non"
    }
}

/// Alias for Not combinator.
pub type Not<P> = Non<P>;

// =============================================================================
// And Combinator
// =============================================================================

/// Conjunction of two predicates.
///
/// `Et<P1, P2>` is true when both `P1` and `P2` are true.
///
/// # Latin Etymology
/// *Et* = and.
pub struct Et<P1, P2> {
    _predicates: PhantomData<(P1, P2)>,
}

impl<T, P1: Praedicatum<T>, P2: Praedicatum<T>> Praedicatum<T> for Et<P1, P2> {
    #[inline]
    fn check(value: &T) -> bool {
        P1::check(value) && P2::check(value)
    }

    #[inline]
    fn description() -> &'static str {
        "both conditions must be true"
    }

    #[inline]
    fn name() -> &'static str {
        "Et"
    }
}

/// Alias for And combinator.
pub type And<P1, P2> = Et<P1, P2>;

// =============================================================================
// Or Combinator
// =============================================================================

/// Disjunction of two predicates.
///
/// `Vel<P1, P2>` is true when either `P1` or `P2` (or both) are true.
///
/// # Latin Etymology
/// *Vel* = or (inclusive).
pub struct Vel<P1, P2> {
    _predicates: PhantomData<(P1, P2)>,
}

impl<T, P1: Praedicatum<T>, P2: Praedicatum<T>> Praedicatum<T> for Vel<P1, P2> {
    #[inline]
    fn check(value: &T) -> bool {
        P1::check(value) || P2::check(value)
    }

    #[inline]
    fn description() -> &'static str {
        "at least one condition must be true"
    }

    #[inline]
    fn name() -> &'static str {
        "Vel"
    }
}

/// Alias for Or combinator.
pub type Or<P1, P2> = Vel<P1, P2>;

// =============================================================================
// Xor Combinator
// =============================================================================

/// Exclusive disjunction of two predicates.
///
/// `Aut<P1, P2>` is true when exactly one of `P1` or `P2` is true.
///
/// # Latin Etymology
/// *Aut* = or (exclusive).
pub struct Aut<P1, P2> {
    _predicates: PhantomData<(P1, P2)>,
}

impl<T, P1: Praedicatum<T>, P2: Praedicatum<T>> Praedicatum<T> for Aut<P1, P2> {
    #[inline]
    fn check(value: &T) -> bool {
        P1::check(value) ^ P2::check(value)
    }

    #[inline]
    fn description() -> &'static str {
        "exactly one condition must be true"
    }

    #[inline]
    fn name() -> &'static str {
        "Aut"
    }
}

/// Alias for Xor combinator.
pub type Xor<P1, P2> = Aut<P1, P2>;

// =============================================================================
// Implication Combinator
// =============================================================================

/// Implication of two predicates.
///
/// `Implicatio<P1, P2>` is true when P1 implies P2 (if P1 then P2).
/// Equivalent to `!P1 || P2`.
///
/// # Latin Etymology
/// *Implicatio* = implication, involvement.
pub struct Implicatio<P1, P2> {
    _predicates: PhantomData<(P1, P2)>,
}

impl<T, P1: Praedicatum<T>, P2: Praedicatum<T>> Praedicatum<T> for Implicatio<P1, P2> {
    #[inline]
    fn check(value: &T) -> bool {
        !P1::check(value) || P2::check(value)
    }

    #[inline]
    fn description() -> &'static str {
        "if first condition then second condition"
    }

    #[inline]
    fn name() -> &'static str {
        "Implicatio"
    }
}

/// Alias for Implies combinator.
pub type Implies<P1, P2> = Implicatio<P1, P2>;

// =============================================================================
// Equivalence Combinator
// =============================================================================

/// Bi-conditional (equivalence) of two predicates.
///
/// `Aequivalentia<P1, P2>` is true when P1 and P2 have the same truth value.
///
/// # Latin Etymology
/// *Aequivalentia* = equivalence.
pub struct Aequivalentia<P1, P2> {
    _predicates: PhantomData<(P1, P2)>,
}

impl<T, P1: Praedicatum<T>, P2: Praedicatum<T>> Praedicatum<T> for Aequivalentia<P1, P2> {
    #[inline]
    fn check(value: &T) -> bool {
        P1::check(value) == P2::check(value)
    }

    #[inline]
    fn description() -> &'static str {
        "conditions must have same truth value"
    }

    #[inline]
    fn name() -> &'static str {
        "Aequivalentia"
    }
}

/// Alias for Iff (if and only if) combinator.
pub type Iff<P1, P2> = Aequivalentia<P1, P2>;

// =============================================================================
// All Combinator (Variadic And)
// =============================================================================

/// All predicates must be true (3-way and).
///
/// # Latin Etymology
/// *Omnes* = all.
pub struct Omnes<P1, P2, P3> {
    _predicates: PhantomData<(P1, P2, P3)>,
}

impl<T, P1, P2, P3> Praedicatum<T> for Omnes<P1, P2, P3>
where
    P1: Praedicatum<T>,
    P2: Praedicatum<T>,
    P3: Praedicatum<T>,
{
    #[inline]
    fn check(value: &T) -> bool {
        P1::check(value) && P2::check(value) && P3::check(value)
    }

    #[inline]
    fn description() -> &'static str {
        "all conditions must be true"
    }

    #[inline]
    fn name() -> &'static str {
        "Omnes"
    }
}

/// Alias for All combinator.
pub type All<P1, P2, P3> = Omnes<P1, P2, P3>;

// =============================================================================
// Any Combinator (Variadic Or)
// =============================================================================

/// At least one predicate must be true (3-way or).
///
/// # Latin Etymology
/// *Aliquis* = any, some.
pub struct Aliquis<P1, P2, P3> {
    _predicates: PhantomData<(P1, P2, P3)>,
}

impl<T, P1, P2, P3> Praedicatum<T> for Aliquis<P1, P2, P3>
where
    P1: Praedicatum<T>,
    P2: Praedicatum<T>,
    P3: Praedicatum<T>,
{
    #[inline]
    fn check(value: &T) -> bool {
        P1::check(value) || P2::check(value) || P3::check(value)
    }

    #[inline]
    fn description() -> &'static str {
        "at least one condition must be true"
    }

    #[inline]
    fn name() -> &'static str {
        "Aliquis"
    }
}

/// Alias for Any combinator.
pub type Any<P1, P2, P3> = Aliquis<P1, P2, P3>;

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

#[cfg(test)]
mod tests {
    use super::super::common::{Even, Negative, NonNegative, Positive};
    use super::*;

    #[test]
    fn test_not() {
        // Not Positive = non-positive (zero or negative)
        assert!(!Non::<Positive>::check(&42i32));
        assert!(Non::<Positive>::check(&0i32));
        assert!(Non::<Positive>::check(&-1i32));
    }

    #[test]
    fn test_and() {
        // Positive AND Even = positive even numbers
        assert!(Et::<Positive, Even>::check(&42i32));
        assert!(!Et::<Positive, Even>::check(&41i32)); // positive but odd
        assert!(!Et::<Positive, Even>::check(&-2i32)); // even but negative
        assert!(!Et::<Positive, Even>::check(&0i32)); // even but not positive
    }

    #[test]
    fn test_or() {
        // Positive OR Even
        assert!(Vel::<Positive, Even>::check(&42i32)); // both
        assert!(Vel::<Positive, Even>::check(&41i32)); // positive only
        assert!(Vel::<Positive, Even>::check(&-2i32)); // even only
        assert!(!Vel::<Positive, Even>::check(&-1i32)); // neither
    }

    #[test]
    fn test_xor() {
        // Positive XOR Even = exactly one
        assert!(!Aut::<Positive, Even>::check(&42i32)); // both - false
        assert!(Aut::<Positive, Even>::check(&41i32)); // positive only - true
        assert!(Aut::<Positive, Even>::check(&-2i32)); // even only - true
        assert!(!Aut::<Positive, Even>::check(&-1i32)); // neither - false
    }

    #[test]
    fn test_implies() {
        // Positive => Even (if positive then even)
        assert!(Implicatio::<Positive, Even>::check(&42i32)); // P=T, Q=T -> T
        assert!(!Implicatio::<Positive, Even>::check(&41i32)); // P=T, Q=F -> F
        assert!(Implicatio::<Positive, Even>::check(&-2i32)); // P=F, Q=T -> T
        assert!(Implicatio::<Positive, Even>::check(&-1i32)); // P=F, Q=F -> T
    }

    #[test]
    fn test_iff() {
        // Positive <=> Even (both same truth value)
        assert!(Aequivalentia::<Positive, Even>::check(&42i32)); // T, T -> T
        assert!(!Aequivalentia::<Positive, Even>::check(&41i32)); // T, F -> F
        assert!(!Aequivalentia::<Positive, Even>::check(&-2i32)); // F, T -> F
        assert!(Aequivalentia::<Positive, Even>::check(&-1i32)); // F, F -> T
    }

    #[test]
    fn test_all() {
        // Positive AND Even AND NonNegative
        assert!(All::<Positive, Even, NonNegative>::check(&42i32));
        assert!(!All::<Positive, Even, NonNegative>::check(&41i32)); // odd
        assert!(!All::<Positive, Even, NonNegative>::check(&0i32)); // not positive
    }

    #[test]
    fn test_any() {
        // Positive OR Even OR Negative
        assert!(Any::<Positive, Even, Negative>::check(&42i32)); // positive and even
        assert!(Any::<Positive, Even, Negative>::check(&41i32)); // positive
        assert!(Any::<Positive, Even, Negative>::check(&-2i32)); // even and negative
        assert!(Any::<Positive, Even, Negative>::check(&-1i32)); // negative
    }

    #[test]
    fn test_combined() {
        // (Positive AND Even) OR Negative
        type PositiveEven = And<Positive, Even>;
        type Combined = Or<PositiveEven, Negative>;

        assert!(Combined::check(&42i32)); // positive and even
        assert!(!Combined::check(&41i32)); // positive but odd
        assert!(Combined::check(&-1i32)); // negative
        assert!(!Combined::check(&0i32)); // neither
    }

    #[test]
    fn test_double_negation() {
        // Not(Not(P)) = P
        type DoubleNot = Not<Not<Positive>>;

        assert!(DoubleNot::check(&42i32));
        assert!(!DoubleNot::check(&-1i32));
    }

    #[test]
    fn test_de_morgan() {
        // Not(P AND Q) = Not(P) OR Not(Q)
        type Lhs = Not<And<Positive, Even>>;
        type Rhs = Or<Not<Positive>, Not<Even>>;

        // Test De Morgan's law
        for val in [-10, -1, 0, 1, 2, 10, 42, 43] {
            assert_eq!(
                Lhs::check(&val),
                Rhs::check(&val),
                "De Morgan failed for {val}"
            );
        }
    }
}