tensorlogic-ir 0.1.0

Intermediate representation (IR) and AST types for TensorLogic
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
//! Distributive law transformations for logical expressions.
//!
//! This module implements distributive law transformations that can convert expressions
//! between equivalent forms, which is useful for optimization and normalization.
//!
//! # Distributive Laws
//!
//! ## Basic Distributive Laws
//! - **AND over OR**: A ∧ (B ∨ C) ≡ (A ∧ B) ∨ (A ∧ C)
//! - **OR over AND**: A ∨ (B ∧ C) ≡ (A ∨ B) ∧ (A ∨ C)
//!
//! ## Quantifier Distribution
//! - **∀ over ∧**: ∀x.(P(x) ∧ Q(x)) ≡ (∀x.P(x)) ∧ (∀x.Q(x))
//! - **∃ over ∨**: ∃x.(P(x) ∨ Q(x)) ≡ (∃x.P(x)) ∨ (∃x.Q(x))
//!
//! ## Modal Distribution
//! - **□ over ∧**: □(P ∧ Q) ≡ □P ∧ □Q
//! - **◇ over ∨**: ◇(P ∨ Q) ≡ ◇P ∨ ◇Q

use super::TLExpr;

/// Strategy for applying distributive laws.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DistributiveStrategy {
    /// Distribute AND over OR: A ∧ (B ∨ C) → (A ∧ B) ∨ (A ∧ C)
    AndOverOr,
    /// Distribute OR over AND: A ∨ (B ∧ C) → (A ∨ B) ∧ (A ∨ C)
    OrOverAnd,
    /// Distribute quantifiers when possible
    Quantifiers,
    /// Distribute modal operators
    Modal,
    /// Apply all distributive laws
    All,
}

/// Apply distributive laws to an expression.
///
/// # Arguments
/// * `expr` - The expression to transform
/// * `strategy` - Which distributive laws to apply
///
/// # Returns
/// A transformed expression with distributive laws applied.
pub fn apply_distributive_laws(expr: &TLExpr, strategy: DistributiveStrategy) -> TLExpr {
    match strategy {
        DistributiveStrategy::AndOverOr => distribute_and_over_or(expr),
        DistributiveStrategy::OrOverAnd => distribute_or_over_and(expr),
        DistributiveStrategy::Quantifiers => distribute_quantifiers(expr),
        DistributiveStrategy::Modal => distribute_modal(expr),
        DistributiveStrategy::All => {
            let mut result = expr.clone();
            result = distribute_and_over_or(&result);
            result = distribute_or_over_and(&result);
            result = distribute_quantifiers(&result);
            result = distribute_modal(&result);
            result
        }
    }
}

/// Distribute AND over OR: A ∧ (B ∨ C) → (A ∧ B) ∨ (A ∧ C)
fn distribute_and_over_or(expr: &TLExpr) -> TLExpr {
    match expr {
        // A ∧ (B ∨ C) → (A ∧ B) ∨ (A ∧ C)
        TLExpr::And(a, b) if matches!(**b, TLExpr::Or(_, _)) => {
            if let TLExpr::Or(b1, b2) = &**b {
                let left = TLExpr::and(distribute_and_over_or(a), distribute_and_over_or(b1));
                let right = TLExpr::and(distribute_and_over_or(a), distribute_and_over_or(b2));
                TLExpr::or(left, right)
            } else {
                expr.clone()
            }
        }
        // (A ∨ B) ∧ C → (A ∧ C) ∨ (B ∧ C)
        TLExpr::And(a, c) if matches!(**a, TLExpr::Or(_, _)) => {
            if let TLExpr::Or(a1, a2) = &**a {
                let left = TLExpr::and(distribute_and_over_or(a1), distribute_and_over_or(c));
                let right = TLExpr::and(distribute_and_over_or(a2), distribute_and_over_or(c));
                TLExpr::or(left, right)
            } else {
                expr.clone()
            }
        }
        // Recursively apply to subexpressions
        TLExpr::And(l, r) => TLExpr::and(distribute_and_over_or(l), distribute_and_over_or(r)),
        TLExpr::Or(l, r) => TLExpr::or(distribute_and_over_or(l), distribute_and_over_or(r)),
        TLExpr::Not(e) => TLExpr::negate(distribute_and_over_or(e)),
        TLExpr::Imply(l, r) => TLExpr::imply(distribute_and_over_or(l), distribute_and_over_or(r)),
        TLExpr::Exists { var, domain, body } => {
            TLExpr::exists(var.clone(), domain.clone(), distribute_and_over_or(body))
        }
        TLExpr::ForAll { var, domain, body } => {
            TLExpr::forall(var.clone(), domain.clone(), distribute_and_over_or(body))
        }
        TLExpr::Box(e) => TLExpr::modal_box(distribute_and_over_or(e)),
        TLExpr::Diamond(e) => TLExpr::modal_diamond(distribute_and_over_or(e)),
        TLExpr::Next(e) => TLExpr::next(distribute_and_over_or(e)),
        TLExpr::Eventually(e) => TLExpr::eventually(distribute_and_over_or(e)),
        TLExpr::Always(e) => TLExpr::always(distribute_and_over_or(e)),
        TLExpr::Until { before, after } => TLExpr::until(
            distribute_and_over_or(before),
            distribute_and_over_or(after),
        ),
        _ => expr.clone(),
    }
}

/// Distribute OR over AND: A ∨ (B ∧ C) → (A ∨ B) ∧ (A ∨ C)
fn distribute_or_over_and(expr: &TLExpr) -> TLExpr {
    match expr {
        // A ∨ (B ∧ C) → (A ∨ B) ∧ (A ∨ C)
        TLExpr::Or(a, b) if matches!(**b, TLExpr::And(_, _)) => {
            if let TLExpr::And(b1, b2) = &**b {
                let left = TLExpr::or(distribute_or_over_and(a), distribute_or_over_and(b1));
                let right = TLExpr::or(distribute_or_over_and(a), distribute_or_over_and(b2));
                TLExpr::and(left, right)
            } else {
                expr.clone()
            }
        }
        // (A ∧ B) ∨ C → (A ∨ C) ∧ (B ∨ C)
        TLExpr::Or(a, c) if matches!(**a, TLExpr::And(_, _)) => {
            if let TLExpr::And(a1, a2) = &**a {
                let left = TLExpr::or(distribute_or_over_and(a1), distribute_or_over_and(c));
                let right = TLExpr::or(distribute_or_over_and(a2), distribute_or_over_and(c));
                TLExpr::and(left, right)
            } else {
                expr.clone()
            }
        }
        // Recursively apply to subexpressions
        TLExpr::And(l, r) => TLExpr::and(distribute_or_over_and(l), distribute_or_over_and(r)),
        TLExpr::Or(l, r) => TLExpr::or(distribute_or_over_and(l), distribute_or_over_and(r)),
        TLExpr::Not(e) => TLExpr::negate(distribute_or_over_and(e)),
        TLExpr::Imply(l, r) => TLExpr::imply(distribute_or_over_and(l), distribute_or_over_and(r)),
        TLExpr::Exists { var, domain, body } => {
            TLExpr::exists(var.clone(), domain.clone(), distribute_or_over_and(body))
        }
        TLExpr::ForAll { var, domain, body } => {
            TLExpr::forall(var.clone(), domain.clone(), distribute_or_over_and(body))
        }
        TLExpr::Box(e) => TLExpr::modal_box(distribute_or_over_and(e)),
        TLExpr::Diamond(e) => TLExpr::modal_diamond(distribute_or_over_and(e)),
        TLExpr::Next(e) => TLExpr::next(distribute_or_over_and(e)),
        TLExpr::Eventually(e) => TLExpr::eventually(distribute_or_over_and(e)),
        TLExpr::Always(e) => TLExpr::always(distribute_or_over_and(e)),
        TLExpr::Until { before, after } => TLExpr::until(
            distribute_or_over_and(before),
            distribute_or_over_and(after),
        ),
        _ => expr.clone(),
    }
}

/// Distribute quantifiers: ∀x.(P(x) ∧ Q(x)) → (∀x.P(x)) ∧ (∀x.Q(x))
fn distribute_quantifiers(expr: &TLExpr) -> TLExpr {
    match expr {
        // ∀x.(P(x) ∧ Q(x)) → (∀x.P(x)) ∧ (∀x.Q(x))
        TLExpr::ForAll { var, domain, body } if matches!(**body, TLExpr::And(_, _)) => {
            if let TLExpr::And(p, q) = &**body {
                let left = TLExpr::forall(var.clone(), domain.clone(), distribute_quantifiers(p));
                let right = TLExpr::forall(var.clone(), domain.clone(), distribute_quantifiers(q));
                TLExpr::and(left, right)
            } else {
                expr.clone()
            }
        }
        // ∃x.(P(x) ∨ Q(x)) → (∃x.P(x)) ∨ (∃x.Q(x))
        TLExpr::Exists { var, domain, body } if matches!(**body, TLExpr::Or(_, _)) => {
            if let TLExpr::Or(p, q) = &**body {
                let left = TLExpr::exists(var.clone(), domain.clone(), distribute_quantifiers(p));
                let right = TLExpr::exists(var.clone(), domain.clone(), distribute_quantifiers(q));
                TLExpr::or(left, right)
            } else {
                expr.clone()
            }
        }
        // Recursively apply to subexpressions
        TLExpr::And(l, r) => TLExpr::and(distribute_quantifiers(l), distribute_quantifiers(r)),
        TLExpr::Or(l, r) => TLExpr::or(distribute_quantifiers(l), distribute_quantifiers(r)),
        TLExpr::Not(e) => TLExpr::negate(distribute_quantifiers(e)),
        TLExpr::Imply(l, r) => TLExpr::imply(distribute_quantifiers(l), distribute_quantifiers(r)),
        TLExpr::Exists { var, domain, body } => {
            TLExpr::exists(var.clone(), domain.clone(), distribute_quantifiers(body))
        }
        TLExpr::ForAll { var, domain, body } => {
            TLExpr::forall(var.clone(), domain.clone(), distribute_quantifiers(body))
        }
        TLExpr::Box(e) => TLExpr::modal_box(distribute_quantifiers(e)),
        TLExpr::Diamond(e) => TLExpr::modal_diamond(distribute_quantifiers(e)),
        _ => expr.clone(),
    }
}

/// Distribute modal operators: □(P ∧ Q) → □P ∧ □Q
fn distribute_modal(expr: &TLExpr) -> TLExpr {
    match expr {
        // □(P ∧ Q) → □P ∧ □Q
        TLExpr::Box(body) if matches!(**body, TLExpr::And(_, _)) => {
            if let TLExpr::And(p, q) = &**body {
                let left = TLExpr::modal_box(distribute_modal(p));
                let right = TLExpr::modal_box(distribute_modal(q));
                TLExpr::and(left, right)
            } else {
                expr.clone()
            }
        }
        // ◇(P ∨ Q) → ◇P ∨ ◇Q
        TLExpr::Diamond(body) if matches!(**body, TLExpr::Or(_, _)) => {
            if let TLExpr::Or(p, q) = &**body {
                let left = TLExpr::modal_diamond(distribute_modal(p));
                let right = TLExpr::modal_diamond(distribute_modal(q));
                TLExpr::or(left, right)
            } else {
                expr.clone()
            }
        }
        // Recursively apply to subexpressions
        TLExpr::And(l, r) => TLExpr::and(distribute_modal(l), distribute_modal(r)),
        TLExpr::Or(l, r) => TLExpr::or(distribute_modal(l), distribute_modal(r)),
        TLExpr::Not(e) => TLExpr::negate(distribute_modal(e)),
        TLExpr::Imply(l, r) => TLExpr::imply(distribute_modal(l), distribute_modal(r)),
        TLExpr::Exists { var, domain, body } => {
            TLExpr::exists(var.clone(), domain.clone(), distribute_modal(body))
        }
        TLExpr::ForAll { var, domain, body } => {
            TLExpr::forall(var.clone(), domain.clone(), distribute_modal(body))
        }
        TLExpr::Box(e) => TLExpr::modal_box(distribute_modal(e)),
        TLExpr::Diamond(e) => TLExpr::modal_diamond(distribute_modal(e)),
        TLExpr::Next(e) => TLExpr::next(distribute_modal(e)),
        TLExpr::Eventually(e) => TLExpr::eventually(distribute_modal(e)),
        TLExpr::Always(e) => TLExpr::always(distribute_modal(e)),
        _ => expr.clone(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Term;

    #[test]
    fn test_and_over_or_simple() {
        // A ∧ (B ∨ C) → (A ∧ B) ∨ (A ∧ C)
        let a = TLExpr::pred("A", vec![Term::var("x")]);
        let b = TLExpr::pred("B", vec![Term::var("x")]);
        let c = TLExpr::pred("C", vec![Term::var("x")]);

        let expr = TLExpr::and(a.clone(), TLExpr::or(b.clone(), c.clone()));
        let result = apply_distributive_laws(&expr, DistributiveStrategy::AndOverOr);

        // Result should be (A ∧ B) ∨ (A ∧ C)
        assert!(matches!(result, TLExpr::Or(_, _)));
        if let TLExpr::Or(left, right) = result {
            assert!(matches!(*left, TLExpr::And(_, _)));
            assert!(matches!(*right, TLExpr::And(_, _)));
        }
    }

    #[test]
    fn test_and_over_or_left() {
        // (A ∨ B) ∧ C → (A ∧ C) ∨ (B ∧ C)
        let a = TLExpr::pred("A", vec![Term::var("x")]);
        let b = TLExpr::pred("B", vec![Term::var("x")]);
        let c = TLExpr::pred("C", vec![Term::var("x")]);

        let expr = TLExpr::and(TLExpr::or(a.clone(), b.clone()), c.clone());
        let result = apply_distributive_laws(&expr, DistributiveStrategy::AndOverOr);

        // Result should be (A ∧ C) ∨ (B ∧ C)
        assert!(matches!(result, TLExpr::Or(_, _)));
    }

    #[test]
    fn test_or_over_and_simple() {
        // A ∨ (B ∧ C) → (A ∨ B) ∧ (A ∨ C)
        let a = TLExpr::pred("A", vec![Term::var("x")]);
        let b = TLExpr::pred("B", vec![Term::var("x")]);
        let c = TLExpr::pred("C", vec![Term::var("x")]);

        let expr = TLExpr::or(a.clone(), TLExpr::and(b.clone(), c.clone()));
        let result = apply_distributive_laws(&expr, DistributiveStrategy::OrOverAnd);

        // Result should be (A ∨ B) ∧ (A ∨ C)
        assert!(matches!(result, TLExpr::And(_, _)));
        if let TLExpr::And(left, right) = result {
            assert!(matches!(*left, TLExpr::Or(_, _)));
            assert!(matches!(*right, TLExpr::Or(_, _)));
        }
    }

    #[test]
    fn test_or_over_and_left() {
        // (A ∧ B) ∨ C → (A ∨ C) ∧ (B ∨ C)
        let a = TLExpr::pred("A", vec![Term::var("x")]);
        let b = TLExpr::pred("B", vec![Term::var("x")]);
        let c = TLExpr::pred("C", vec![Term::var("x")]);

        let expr = TLExpr::or(TLExpr::and(a.clone(), b.clone()), c.clone());
        let result = apply_distributive_laws(&expr, DistributiveStrategy::OrOverAnd);

        // Result should be (A ∨ C) ∧ (B ∨ C)
        assert!(matches!(result, TLExpr::And(_, _)));
    }

    #[test]
    fn test_forall_over_and() {
        // ∀x.(P(x) ∧ Q(x)) → (∀x.P(x)) ∧ (∀x.Q(x))
        let p = TLExpr::pred("P", vec![Term::var("x")]);
        let q = TLExpr::pred("Q", vec![Term::var("x")]);

        let expr = TLExpr::forall("x", "D", TLExpr::and(p, q));
        let result = apply_distributive_laws(&expr, DistributiveStrategy::Quantifiers);

        // Result should be (∀x.P(x)) ∧ (∀x.Q(x))
        assert!(matches!(result, TLExpr::And(_, _)));
        if let TLExpr::And(left, right) = result {
            assert!(matches!(*left, TLExpr::ForAll { .. }));
            assert!(matches!(*right, TLExpr::ForAll { .. }));
        }
    }

    #[test]
    fn test_exists_over_or() {
        // ∃x.(P(x) ∨ Q(x)) → (∃x.P(x)) ∨ (∃x.Q(x))
        let p = TLExpr::pred("P", vec![Term::var("x")]);
        let q = TLExpr::pred("Q", vec![Term::var("x")]);

        let expr = TLExpr::exists("x", "D", TLExpr::or(p, q));
        let result = apply_distributive_laws(&expr, DistributiveStrategy::Quantifiers);

        // Result should be (∃x.P(x)) ∨ (∃x.Q(x))
        assert!(matches!(result, TLExpr::Or(_, _)));
        if let TLExpr::Or(left, right) = result {
            assert!(matches!(*left, TLExpr::Exists { .. }));
            assert!(matches!(*right, TLExpr::Exists { .. }));
        }
    }

    #[test]
    fn test_box_over_and() {
        // □(P ∧ Q) → □P ∧ □Q
        let p = TLExpr::pred("P", vec![Term::var("x")]);
        let q = TLExpr::pred("Q", vec![Term::var("x")]);

        let expr = TLExpr::modal_box(TLExpr::and(p, q));
        let result = apply_distributive_laws(&expr, DistributiveStrategy::Modal);

        // Result should be □P ∧ □Q
        assert!(matches!(result, TLExpr::And(_, _)));
        if let TLExpr::And(left, right) = result {
            assert!(matches!(*left, TLExpr::Box(_)));
            assert!(matches!(*right, TLExpr::Box(_)));
        }
    }

    #[test]
    fn test_diamond_over_or() {
        // ◇(P ∨ Q) → ◇P ∨ ◇Q
        let p = TLExpr::pred("P", vec![Term::var("x")]);
        let q = TLExpr::pred("Q", vec![Term::var("x")]);

        let expr = TLExpr::modal_diamond(TLExpr::or(p, q));
        let result = apply_distributive_laws(&expr, DistributiveStrategy::Modal);

        // Result should be ◇P ∨ ◇Q
        assert!(matches!(result, TLExpr::Or(_, _)));
        if let TLExpr::Or(left, right) = result {
            assert!(matches!(*left, TLExpr::Diamond(_)));
            assert!(matches!(*right, TLExpr::Diamond(_)));
        }
    }

    #[test]
    fn test_all_strategy() {
        // Test that All strategy applies AndOverOr transformation
        let a = TLExpr::pred("A", vec![Term::var("x")]);
        let b = TLExpr::pred("B", vec![Term::var("x")]);
        let c = TLExpr::pred("C", vec![Term::var("x")]);

        // A ∧ (B ∨ C)
        let expr = TLExpr::and(a, TLExpr::or(b, c));
        let result = apply_distributive_laws(&expr, DistributiveStrategy::AndOverOr);

        // Should be distributed to (A ∧ B) ∨ (A ∧ C)
        assert!(matches!(result, TLExpr::Or(_, _)));
    }

    #[test]
    fn test_no_distribution_needed() {
        // Test that expressions that don't need distribution are unchanged
        let a = TLExpr::pred("A", vec![Term::var("x")]);
        let b = TLExpr::pred("B", vec![Term::var("x")]);

        // A ∧ B (no distribution needed)
        let expr = TLExpr::and(a.clone(), b.clone());
        let result = apply_distributive_laws(&expr, DistributiveStrategy::AndOverOr);

        // Should remain A ∧ B
        assert!(matches!(result, TLExpr::And(_, _)));
    }
}