logify 0.1.0

A high-performance, portable boolean logic engine. Turns abstract logic into optimized data structures that can be serialized, cached, and evaluated against arbitrary user data (Bitmaps, Sets, JSON, etc).
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
use std::marker::PhantomData;

use hashbrown::HashMap;

use bitflags::bitflags;

use crate::expr::{Expression, Node, NodeId};

bitflags! {
    #[derive(Clone, Copy, PartialEq)]
    pub(crate) struct MergeRelation: u8 {
        const TRIVIAL	= 0; // A and B are not related

        const SUBSET	= 0b0001; // A sub B
        const SUPERSET	= 0b0010; // A sup B
        const DISJOINT	= 0b0100; // A disjoint B
        const COVER		= 0b1000; // A | B == Universal

        const EQUAL			= Self::SUBSET.bits() | Self::SUPERSET.bits(); // (A sub B) and (A sup B)
        const COMPLEMENTARY	= Self::DISJOINT.bits() | Self::COVER.bits(); // (A disj B) and (A | B == Universal)
    }
}

impl MergeRelation {
    pub(crate) fn flip(self) -> Self {
        match self {
            MergeRelation::SUBSET => MergeRelation::SUPERSET,
            MergeRelation::SUPERSET => MergeRelation::SUBSET,
            _ => self,
        }
    }

    // simple checks
    pub(crate) fn is_subset(&self) -> bool {
        self.contains(Self::SUBSET)
    }
    pub(crate) fn is_superset(&self) -> bool {
        self.contains(Self::SUPERSET)
    }
    pub(crate) fn is_disjoint(&self) -> bool {
        self.contains(Self::DISJOINT)
    }
    pub(crate) fn is_cover(&self) -> bool {
        self.contains(Self::COVER)
    }
}

/// Describes how two sets relate to one another.
///
/// This is returned by [`Mergeable::get_relation`]. The optimizer uses this to remove
/// redundant logic (e.g., if `A` is a subset of `B`, then `A & B` simplifies to `A`).
///
/// # Hierarchy
/// Return the most specific relationship possible.
/// 1. **Equal:** Sets are identical.
/// 2. **Subset / Superset:** One set strictly contains the other.
/// 3. **Complementary:** Sets are disjoint AND fill the universe.
/// 4. **Cover:** Union fills the universe.
/// 5. **Disjoint:** Intersection is empty.
/// 6. **Trivial:** No special relationship.
///
/// One or more results can be left out of the return. However, it may prevent optimizations.
///
/// **Subet / Superset** depend on each other, so returning only one may prevent optimizations for the other.
pub enum SetRelation {
    /// No known relationship.
    Trivial,
    /// `A` is contained entirely within `B`.
    Subset,
    /// `A` entirely contains `B`.
    Superset,
    /// `A` and `B` share no elements (Intersection is Empty).
    Disjoint,
    /// `A` and `B` cover the entire universe (Union is Universal).
    Cover,
    /// `A` is the exact inverse of `B`.
    Complementary,
    /// `A` and `B` contain exactly the same elements.
    Equal,
}

impl From<SetRelation> for MergeRelation {
    fn from(r: SetRelation) -> Self {
        match r {
            SetRelation::Trivial => MergeRelation::TRIVIAL,
            SetRelation::Subset => MergeRelation::SUBSET,
            SetRelation::Superset => MergeRelation::SUPERSET,
            SetRelation::Disjoint => MergeRelation::DISJOINT,
            SetRelation::Cover => MergeRelation::COVER,
            SetRelation::Complementary => MergeRelation::COMPLEMENTARY,
            SetRelation::Equal => MergeRelation::EQUAL,
        }
    }
}

/// The outcome of a custom merge operation.
pub enum MergeResult<T> {
    /// The merge resulted in an empty set.
    Empty,
    /// The merge resulted in a universal set.
    Universal,
    /// The merge resulted in a new set `T`.
    ///
    /// The boolean flag indicates negation:
    /// * `false`: The result is `Set`.
    /// * `true`: The result is `NOT Set`.
    Set(T, bool),
}

impl<T> From<T> for MergeResult<T> {
    fn from(value: T) -> Self {
        MergeResult::Set(value, false)
    }
}

/// A trait for injecting domain-specific logic into the optimizer.
///
/// Implementing this allows the [`Expression::optimize`](crate::expr::Expression::optimize)
/// function to understand relationships between your specific data types, enabling simplifications
/// that pure boolean logic cannot see.
///
/// # Rules
/// * **Consistency:** Logic defined here must be permanent. Do not optimize based on transient
///   data (like "Current Time").
/// * **Partial Implementation:** You do not need to handle every case. Returning
///   [`SetRelation::Trivial`] or `None` is always safe; it just means less optimization.
///
/// # Example: Role-Based Permissions
///
/// Imagine a system where the `Admin` role automatically inherits everything the `User` role has.
///
/// ```rust
/// use logify::opt::{Mergeable, SetRelation};
///
/// #[derive(PartialEq, Hash)]
/// enum Role { User, Admin, Guest }
///
/// // 1. Define a custom merger struct
/// struct RoleMerger;
///
/// // 2. Implement the trait for your struct
/// impl Mergeable<Role> for RoleMerger {
///     fn get_relation(&mut self, a: &Role, b: &Role) -> SetRelation {
///         match (a, b) {
///             // "Admin implies User" means every Admin is also a User.
///             // Therefore, the set of Admins is a SUBSET of the set of Users.
///             (Role::Admin, Role::User) => SetRelation::Subset,
///             (Role::User, Role::Admin) => SetRelation::Superset,
///             
///             // Guests and Admins don't share roles in this example
///             (Role::Guest, Role::Admin) => SetRelation::Disjoint,
///             
///             // Other cases have no relation.
///             _ => SetRelation::Trivial,
///         }
///     }
/// }
/// ```
pub trait Mergeable<T> {
    /// Determines the relationship between two sets.
    ///
    /// # Recommendations
    /// * If `a == b`, return [`SetRelation::Equal`].
    /// * If `a` implies `b`, return [`SetRelation::Subset`].
    /// * If `b` implies `a`, return [`SetRelation::Superset`].
    fn get_relation(&mut self, _a: &T, _b: &T) -> SetRelation {
        SetRelation::Trivial
    }

    /// Attempts to combine two sets using a Union (OR) operation.
    ///
    /// Return `Some` if the sets can be merged into a single node (or constant).
    ///
    /// * `a_neg`/`b_neg`: True if the set being passed in is effectively `NOT Set`.
    ///
    /// # Example
    /// * Interval merging: `[0, 5]` OR `[5, 10]` becomes `[0, 10]`.
    fn merge_union(
        &mut self,
        _a: &T,
        _a_neg: bool,
        _b: &T,
        _b_neg: bool,
    ) -> Option<MergeResult<T>> {
        None
    }

    /// Attempts to combine two sets using an Intersection (AND) operation.
    ///
    /// Return `Some` if the sets can be merged into a single node (or constant).
    ///
    /// # Example
    /// * Interval filtering: `[0, 10]` AND `[5, 15]` becomes `[5, 10]`.
    fn merge_intersection(
        &mut self,
        _a: &T,
        _a_neg: bool,
        _b: &T,
        _b_neg: bool,
    ) -> Option<MergeResult<T>> {
        None
    }
}

impl<T> Mergeable<T> for () {}

pub(crate) struct Merger<'a, T, M: Mergeable<T>> {
    pub mergeable: &'a mut M,
    cache: HashMap<(usize, usize), (MergeRelation, usize)>,
    _mergeable_type: PhantomData<T>,
}

impl<'a, T, M: Mergeable<T>> Merger<'a, T, M> {
    pub(crate) fn new(mergeable: &'a mut M) -> Self {
        Self {
            mergeable,
            cache: HashMap::new(),
            _mergeable_type: PhantomData,
        }
    }

    pub(crate) fn get_relation(
        &mut self,
        expr: &Expression<T>,
        a: NodeId,
        b: NodeId,
        depth: usize,
    ) -> MergeRelation {
        // quick returns that don't require self.mergeable.get_relation()
        if a == b {
            return MergeRelation::EQUAL;
        }
        if a == b.not() {
            return MergeRelation::COMPLEMENTARY;
        }

        self.get_relation_recursive(expr, a, b, depth)
    }

    fn get_relation_recursive(
        &mut self,
        expr: &Expression<T>,
        a: NodeId,
        b: NodeId,
        depth: usize,
    ) -> MergeRelation
    where
        M: Mergeable<T>,
    {
        // quick checks (all cases for same positives)
        if a == b {
            return MergeRelation::EQUAL;
        }
        if a == b.not() {
            return MergeRelation::COMPLEMENTARY;
        }

        // check cache
        let (min, max) = if a.idx() <= b.idx() { (a, b) } else { (b, a) };
        let key = (min.idx(), max.idx());

        // if the realationship is already cached at a depth greater than or equal to own, return that relationship
        if let Some(&(cached_rel, cached_depth)) = self.cache.get(&key)
            && cached_depth >= depth
        {
            // Determine if we need to flip the result based on input order
            let mut final_rel = cached_rel;
            if a != min {
                final_rel = final_rel.flip();
            }
            return self.apply_negation_logic(final_rel, a.is_neg(), b.is_neg());
        }

        // don't cache, a higher depth will always replace it
        if depth == 0 {
            return MergeRelation::TRIVIAL;
        }

        let node_min = &expr.nodes[min.idx()];
        let node_max = &expr.nodes[max.idx()];

        let rel = match (node_min, node_max) {
            // EMPTY is equal to EMPTY
            (Node::Empty, Node::Empty) => MergeRelation::EQUAL, // handled by a==b, but just to make sure
            // EMPTY is disjoint from everything
            (Node::Empty, _) | (_, Node::Empty) => MergeRelation::DISJOINT,
            // Set and Set
            (Node::Set(set_min), Node::Set(set_max)) => {
                self.mergeable.get_relation(set_min, set_max).into()
            }
            // Set and Group
            (Node::Set(_), Node::Union(kids_b)) | (Node::Set(_), Node::Intersection(kids_b)) => {
                let is_union = matches!(node_max, Node::Union(_));
                self.get_groups_relation(expr, &[min], is_union, kids_b, is_union, depth - 1)
            }
            // Group and Set
            (Node::Union(kids_a), Node::Set(_)) | (Node::Intersection(kids_a), Node::Set(_)) => {
                let is_union = matches!(node_min, Node::Union(_));
                self.get_groups_relation(expr, kids_a, is_union, &[max], is_union, depth - 1)
            }
            // Group and Group
            (Node::Union(kids_a), Node::Union(kids_b))
            | (Node::Union(kids_a), Node::Intersection(kids_b))
            | (Node::Intersection(kids_a), Node::Union(kids_b))
            | (Node::Intersection(kids_a), Node::Intersection(kids_b)) => self.get_groups_relation(
                expr,
                kids_a,
                matches!(node_min, Node::Union(_)),
                kids_b,
                matches!(node_max, Node::Union(_)),
                depth - 1,
            ),
        };

        // equal and complementary can't be improved
        let stored_depth = if rel == MergeRelation::EQUAL || rel == MergeRelation::COMPLEMENTARY {
            usize::MAX
        } else {
            depth
        };
        self.cache.insert(key, (rel, stored_depth)); // push to cache

        // return the relationship
        let mut final_rel = rel;
        if a != min {
            final_rel = final_rel.flip();
        }
        self.apply_negation_logic(final_rel, a.is_neg(), b.is_neg())
    }

    fn apply_negation_logic(&self, rel: MergeRelation, neg_a: bool, neg_b: bool) -> MergeRelation {
        if !neg_a && !neg_b {
            return rel;
        }

        // start with trivial relationship
        let mut result = MergeRelation::TRIVIAL;

        // A == B
        if rel == MergeRelation::EQUAL {
            return if neg_a == neg_b {
                // A' == B'
                MergeRelation::EQUAL
            } else {
                // A' comp B, A comp B'
                MergeRelation::COMPLEMENTARY
            };
        }

        // A comp B
        if rel == MergeRelation::COMPLEMENTARY {
            return if neg_a == neg_b {
                // A' comp B'
                MergeRelation::COMPLEMENTARY
            } else {
                // A' == B, B' == A
                MergeRelation::EQUAL
            };
        }

        // A sub B
        if rel.is_subset() {
            match (neg_a, neg_b) {
                (true, true) => result |= MergeRelation::SUPERSET, // A' sup B'
                (false, true) => result |= MergeRelation::DISJOINT, // A disj B'
                _ => {}
            }
        }

        // A sup B
        if rel.is_superset() {
            match (neg_a, neg_b) {
                (true, true) => result |= MergeRelation::SUBSET, // A' sub B'
                (true, false) => result |= MergeRelation::DISJOINT, // A' disj B
                _ => {}
            }
        }

        // A disj B
        if rel.is_disjoint() {
            match (neg_a, neg_b) {
                (false, true) => result |= MergeRelation::SUBSET, // A sub B'
                (true, false) => result |= MergeRelation::SUPERSET, // A' sup B
                _ => {}
            }
        }

        // A | B = U
        if rel.is_cover() {
            match (neg_a, neg_b) {
                (false, true) => result |= MergeRelation::SUPERSET, // A sup B'
                (true, false) => result |= MergeRelation::SUBSET,   // A' sub B
                _ => {}
            }
        }

        // return modified result
        result
    }

    fn get_groups_relation(
        &mut self,
        expr: &Expression<T>,
        kids_a: &[NodeId],
        is_union_a: bool,
        kids_b: &[NodeId],
        is_union_b: bool,
        depth: usize,
    ) -> MergeRelation
    where
        M: Mergeable<T>,
    {
        // cover test omitted, should be covered with merging

        // begin with trivial relationship
        let mut result = MergeRelation::TRIVIAL;

        // check disjoint
        let is_disjoint = match (is_union_a, is_union_b) {
            // Intersection A, Intersection B, best O(1)
            (false, false) =>
            // any a disjoint from any b
            {
                kids_a.iter().any(|&a| {
                    kids_b
                        .iter()
                        .any(|&b| self.get_relation_recursive(expr, a, b, depth).is_disjoint())
                })
            }
            // Union A, Intersection B, best O(A)
            (true, false) =>
            // all a disjoint from any b
            {
                kids_a.iter().all(|&a| {
                    kids_b
                        .iter()
                        .any(|&b| self.get_relation_recursive(expr, a, b, depth).is_disjoint())
                })
            }
            // Intersection A, Union B, best O(B)
            (false, true) =>
            // all b dijoint from any a
            {
                kids_b.iter().all(|&b| {
                    kids_a
                        .iter()
                        .any(|&a| self.get_relation_recursive(expr, a, b, depth).is_disjoint())
                })
            }
            // Union A, Union B, best O(A*B)
            (true, true) =>
            // all a disjoint from all b
            {
                kids_a.iter().all(|&a| {
                    kids_b
                        .iter()
                        .all(|&b| self.get_relation_recursive(expr, a, b, depth).is_disjoint())
                })
            }
        };
        if is_disjoint {
            result |= MergeRelation::DISJOINT;
        }

        // check subset
        let is_subset = match (is_union_a, is_union_b) {
            (true, true) =>
            // UU, best O(A)
            // all a subset any b
            {
                kids_a.iter().all(|&a| {
                    kids_b
                        .iter()
                        .any(|&b| self.get_relation_recursive(expr, a, b, depth).is_subset())
                })
            }
            (true, false) =>
            // UI, best O(AB)
            // all a subset all b
            {
                kids_a.iter().all(|&a| {
                    kids_b
                        .iter()
                        .all(|&b| self.get_relation_recursive(expr, a, b, depth).is_subset())
                })
            }
            (false, true) =>
            // IU, best O(1)
            // any a subset any b
            {
                kids_a.iter().any(|&a| {
                    kids_b
                        .iter()
                        .any(|&b| self.get_relation_recursive(expr, a, b, depth).is_subset())
                })
            }
            (false, false) =>
            // II, best O(B)
            // all b superset any a
            {
                kids_b.iter().all(|&b| {
                    kids_a
                        .iter()
                        .any(|&a| self.get_relation_recursive(expr, a, b, depth).is_superset())
                })
            }
        };
        if is_subset {
            result |= MergeRelation::SUBSET;
        }

        // check superset
        let is_superset = match (is_union_a, is_union_b) {
            (true, true) =>
            // UU, best O(B)
            // all b subset any a
            {
                kids_b.iter().all(|&b| {
                    kids_a
                        .iter()
                        .any(|&a| self.get_relation_recursive(expr, a, b, depth).is_subset())
                })
            }
            (true, false) =>
            // UI, best O(1)
            // any a superset any b
            {
                kids_a.iter().any(|&a| {
                    kids_b
                        .iter()
                        .any(|&b| self.get_relation_recursive(expr, a, b, depth).is_superset())
                })
            }
            (false, true) =>
            // IU, best O(AB)
            // all a superset all b
            {
                kids_a.iter().all(|&a| {
                    kids_b
                        .iter()
                        .all(|&b| self.get_relation_recursive(expr, a, b, depth).is_superset())
                })
            }
            (false, false) =>
            // II, best O(A)
            // all a superset any b
            {
                kids_a.iter().all(|&a| {
                    kids_b
                        .iter()
                        .any(|&b| self.get_relation_recursive(expr, a, b, depth).is_superset())
                })
            }
        };
        if is_superset {
            result |= MergeRelation::SUPERSET;
        }

        // return modified result
        result
    }
}