tensorlogic-compiler 0.1.0-rc.1

Compiler for transforming logic expressions into tensor computation graphs
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
//! Common Subexpression Elimination (CSE) for TLExpr.

use std::collections::HashMap;

use tensorlogic_ir::TLExpr;

/// CSE result containing optimized expression and statistics
#[derive(Debug, Clone)]
pub struct CseResult {
    pub optimized_expr: TLExpr,
    pub eliminated_count: usize,
}

/// Perform common subexpression elimination on a TLExpr
pub fn eliminate_common_subexpressions(expr: &TLExpr) -> CseResult {
    let mut cache: HashMap<String, TLExpr> = HashMap::new();
    let mut eliminated_count = 0;

    let optimized = cse_recursive(expr, &mut cache, &mut eliminated_count);

    CseResult {
        optimized_expr: optimized,
        eliminated_count,
    }
}

fn cse_recursive(
    expr: &TLExpr,
    cache: &mut HashMap<String, TLExpr>,
    eliminated_count: &mut usize,
) -> TLExpr {
    // Compute a hash/key for this expression
    let key = expr_to_key(expr);

    // Check if we've seen this exact subexpression before
    if let Some(cached) = cache.get(&key) {
        *eliminated_count += 1;
        return cached.clone();
    }

    // Recursively process subexpressions
    let result = match expr {
        TLExpr::Pred { .. } => {
            // Predicates are atomic, just cache them
            expr.clone()
        }
        TLExpr::And(left, right) => {
            let left_opt = cse_recursive(left, cache, eliminated_count);
            let right_opt = cse_recursive(right, cache, eliminated_count);
            TLExpr::and(left_opt, right_opt)
        }
        TLExpr::Or(left, right) => {
            let left_opt = cse_recursive(left, cache, eliminated_count);
            let right_opt = cse_recursive(right, cache, eliminated_count);
            TLExpr::or(left_opt, right_opt)
        }
        TLExpr::Imply(premise, conclusion) => {
            let premise_opt = cse_recursive(premise, cache, eliminated_count);
            let conclusion_opt = cse_recursive(conclusion, cache, eliminated_count);
            TLExpr::imply(premise_opt, conclusion_opt)
        }
        TLExpr::Not(inner) => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::negate(inner_opt)
        }
        TLExpr::Exists { var, domain, body } => {
            let body_opt = cse_recursive(body, cache, eliminated_count);
            TLExpr::exists(var, domain, body_opt)
        }
        TLExpr::ForAll { var, domain, body } => {
            let body_opt = cse_recursive(body, cache, eliminated_count);
            TLExpr::forall(var, domain, body_opt)
        }
        TLExpr::Aggregate {
            op,
            var,
            domain,
            body,
            group_by,
        } => {
            let body_opt = cse_recursive(body, cache, eliminated_count);
            TLExpr::aggregate_with_group_by(
                op.clone(),
                var,
                domain,
                body_opt,
                group_by.clone().unwrap_or_default(),
            )
        }
        TLExpr::Score(inner) => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::score(inner_opt)
        }
        // Arithmetic operations
        TLExpr::Add(left, right) => {
            let left_opt = cse_recursive(left, cache, eliminated_count);
            let right_opt = cse_recursive(right, cache, eliminated_count);
            TLExpr::add(left_opt, right_opt)
        }
        TLExpr::Sub(left, right) => {
            let left_opt = cse_recursive(left, cache, eliminated_count);
            let right_opt = cse_recursive(right, cache, eliminated_count);
            TLExpr::sub(left_opt, right_opt)
        }
        TLExpr::Mul(left, right) => {
            let left_opt = cse_recursive(left, cache, eliminated_count);
            let right_opt = cse_recursive(right, cache, eliminated_count);
            TLExpr::mul(left_opt, right_opt)
        }
        TLExpr::Div(left, right) => {
            let left_opt = cse_recursive(left, cache, eliminated_count);
            let right_opt = cse_recursive(right, cache, eliminated_count);
            TLExpr::div(left_opt, right_opt)
        }
        // Comparison operations
        TLExpr::Eq(left, right) => {
            let left_opt = cse_recursive(left, cache, eliminated_count);
            let right_opt = cse_recursive(right, cache, eliminated_count);
            TLExpr::eq(left_opt, right_opt)
        }
        TLExpr::Lt(left, right) => {
            let left_opt = cse_recursive(left, cache, eliminated_count);
            let right_opt = cse_recursive(right, cache, eliminated_count);
            TLExpr::lt(left_opt, right_opt)
        }
        TLExpr::Gt(left, right) => {
            let left_opt = cse_recursive(left, cache, eliminated_count);
            let right_opt = cse_recursive(right, cache, eliminated_count);
            TLExpr::gt(left_opt, right_opt)
        }
        TLExpr::Lte(left, right) => {
            let left_opt = cse_recursive(left, cache, eliminated_count);
            let right_opt = cse_recursive(right, cache, eliminated_count);
            TLExpr::lte(left_opt, right_opt)
        }
        TLExpr::Gte(left, right) => {
            let left_opt = cse_recursive(left, cache, eliminated_count);
            let right_opt = cse_recursive(right, cache, eliminated_count);
            TLExpr::gte(left_opt, right_opt)
        }
        TLExpr::Pow(left, right) => {
            let left_opt = cse_recursive(left, cache, eliminated_count);
            let right_opt = cse_recursive(right, cache, eliminated_count);
            TLExpr::pow(left_opt, right_opt)
        }
        TLExpr::Mod(left, right) => {
            let left_opt = cse_recursive(left, cache, eliminated_count);
            let right_opt = cse_recursive(right, cache, eliminated_count);
            TLExpr::modulo(left_opt, right_opt)
        }
        TLExpr::Min(left, right) => {
            let left_opt = cse_recursive(left, cache, eliminated_count);
            let right_opt = cse_recursive(right, cache, eliminated_count);
            TLExpr::min(left_opt, right_opt)
        }
        TLExpr::Max(left, right) => {
            let left_opt = cse_recursive(left, cache, eliminated_count);
            let right_opt = cse_recursive(right, cache, eliminated_count);
            TLExpr::max(left_opt, right_opt)
        }
        // Unary math operations
        TLExpr::Abs(inner) => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::abs(inner_opt)
        }
        TLExpr::Floor(inner) => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::floor(inner_opt)
        }
        TLExpr::Ceil(inner) => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::ceil(inner_opt)
        }
        TLExpr::Round(inner) => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::round(inner_opt)
        }
        TLExpr::Sqrt(inner) => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::sqrt(inner_opt)
        }
        TLExpr::Exp(inner) => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::exp(inner_opt)
        }
        TLExpr::Log(inner) => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::log(inner_opt)
        }
        TLExpr::Sin(inner) => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::sin(inner_opt)
        }
        TLExpr::Cos(inner) => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::cos(inner_opt)
        }
        TLExpr::Tan(inner) => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::tan(inner_opt)
        }
        // Let binding
        TLExpr::Let { var, value, body } => {
            let value_opt = cse_recursive(value, cache, eliminated_count);
            let body_opt = cse_recursive(body, cache, eliminated_count);
            TLExpr::let_binding(var, value_opt, body_opt)
        }
        // Conditional
        TLExpr::IfThenElse {
            condition,
            then_branch,
            else_branch,
        } => {
            let cond_opt = cse_recursive(condition, cache, eliminated_count);
            let then_opt = cse_recursive(then_branch, cache, eliminated_count);
            let else_opt = cse_recursive(else_branch, cache, eliminated_count);
            TLExpr::if_then_else(cond_opt, then_opt, else_opt)
        }
        // Constant
        TLExpr::Constant(_) => {
            // Constants are atomic, just cache them
            expr.clone()
        }

        // Modal/temporal logic operators - not yet implemented, pass through with recursion
        TLExpr::Box(inner) => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::Box(Box::new(inner_opt))
        }
        TLExpr::Diamond(inner) => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::Diamond(Box::new(inner_opt))
        }
        TLExpr::Next(inner) => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::Next(Box::new(inner_opt))
        }
        TLExpr::Eventually(inner) => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::Eventually(Box::new(inner_opt))
        }
        TLExpr::Always(inner) => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::Always(Box::new(inner_opt))
        }
        TLExpr::Until { before, after } => {
            let before_opt = cse_recursive(before, cache, eliminated_count);
            let after_opt = cse_recursive(after, cache, eliminated_count);
            TLExpr::Until {
                before: Box::new(before_opt),
                after: Box::new(after_opt),
            }
        }
        // Fuzzy logic operators
        TLExpr::TNorm { kind, left, right } => {
            let left_opt = cse_recursive(left, cache, eliminated_count);
            let right_opt = cse_recursive(right, cache, eliminated_count);
            TLExpr::TNorm {
                kind: *kind,
                left: Box::new(left_opt),
                right: Box::new(right_opt),
            }
        }
        TLExpr::TCoNorm { kind, left, right } => {
            let left_opt = cse_recursive(left, cache, eliminated_count);
            let right_opt = cse_recursive(right, cache, eliminated_count);
            TLExpr::TCoNorm {
                kind: *kind,
                left: Box::new(left_opt),
                right: Box::new(right_opt),
            }
        }
        TLExpr::FuzzyNot { kind, expr: inner } => {
            let inner_opt = cse_recursive(inner, cache, eliminated_count);
            TLExpr::FuzzyNot {
                kind: *kind,
                expr: Box::new(inner_opt),
            }
        }
        TLExpr::FuzzyImplication {
            kind,
            premise,
            conclusion,
        } => {
            let premise_opt = cse_recursive(premise, cache, eliminated_count);
            let conclusion_opt = cse_recursive(conclusion, cache, eliminated_count);
            TLExpr::FuzzyImplication {
                kind: *kind,
                premise: Box::new(premise_opt),
                conclusion: Box::new(conclusion_opt),
            }
        }
        // Soft quantifiers
        TLExpr::SoftExists {
            var,
            domain,
            body,
            temperature,
        } => {
            let body_opt = cse_recursive(body, cache, eliminated_count);
            TLExpr::SoftExists {
                var: var.clone(),
                domain: domain.clone(),
                body: Box::new(body_opt),
                temperature: *temperature,
            }
        }
        TLExpr::SoftForAll {
            var,
            domain,
            body,
            temperature,
        } => {
            let body_opt = cse_recursive(body, cache, eliminated_count);
            TLExpr::SoftForAll {
                var: var.clone(),
                domain: domain.clone(),
                body: Box::new(body_opt),
                temperature: *temperature,
            }
        }
        // Weighted/probabilistic operators
        TLExpr::WeightedRule { weight, rule } => {
            let rule_opt = cse_recursive(rule, cache, eliminated_count);
            TLExpr::WeightedRule {
                weight: *weight,
                rule: Box::new(rule_opt),
            }
        }
        TLExpr::ProbabilisticChoice { alternatives } => {
            let alts_opt: Vec<(f64, TLExpr)> = alternatives
                .iter()
                .map(|(prob, expr)| (*prob, cse_recursive(expr, cache, eliminated_count)))
                .collect();
            TLExpr::ProbabilisticChoice {
                alternatives: alts_opt,
            }
        }
        // Extended temporal operators
        TLExpr::Release { released, releaser } => {
            let released_opt = cse_recursive(released, cache, eliminated_count);
            let releaser_opt = cse_recursive(releaser, cache, eliminated_count);
            TLExpr::Release {
                released: Box::new(released_opt),
                releaser: Box::new(releaser_opt),
            }
        }
        TLExpr::WeakUntil { before, after } => {
            let before_opt = cse_recursive(before, cache, eliminated_count);
            let after_opt = cse_recursive(after, cache, eliminated_count);
            TLExpr::WeakUntil {
                before: Box::new(before_opt),
                after: Box::new(after_opt),
            }
        }
        TLExpr::StrongRelease { released, releaser } => {
            let released_opt = cse_recursive(released, cache, eliminated_count);
            let releaser_opt = cse_recursive(releaser, cache, eliminated_count);
            TLExpr::StrongRelease {
                released: Box::new(released_opt),
                releaser: Box::new(releaser_opt),
            }
        }
        // All other expression types (enhancements)
        _ => expr.clone(),
    };

    // Cache this result
    cache.insert(key, result.clone());
    result
}

/// Convert an expression to a hashable key
fn expr_to_key(expr: &TLExpr) -> String {
    // Use debug format as a simple hash
    // In production, you'd want a proper hash function
    format!("{:?}", expr)
}

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

    #[test]
    fn test_cse_no_duplicates() {
        let expr = TLExpr::and(
            TLExpr::pred("p", vec![Term::var("x")]),
            TLExpr::pred("q", vec![Term::var("y")]),
        );

        let result = eliminate_common_subexpressions(&expr);
        assert_eq!(result.eliminated_count, 0);
    }

    #[test]
    fn test_cse_duplicate_predicates() {
        // p(x) ∧ p(x) - should detect duplicate
        let p_x = TLExpr::pred("p", vec![Term::var("x")]);
        let expr = TLExpr::and(p_x.clone(), p_x);

        let result = eliminate_common_subexpressions(&expr);
        // Should eliminate at least one duplicate
        assert!(result.eliminated_count > 0);
    }

    #[test]
    fn test_cse_nested_duplicates() {
        // (p(x) ∧ q(y)) ∧ (p(x) ∧ q(y)) - duplicate AND subexpressions
        let p_x = TLExpr::pred("p", vec![Term::var("x")]);
        let q_y = TLExpr::pred("q", vec![Term::var("y")]);
        let sub = TLExpr::and(p_x, q_y);
        let expr = TLExpr::and(sub.clone(), sub);

        let result = eliminate_common_subexpressions(&expr);
        assert!(result.eliminated_count > 0);
    }

    #[test]
    fn test_cse_with_quantifiers() {
        // ∃x. p(x) ∧ ∃x. p(x) - duplicate existentials
        let p_x = TLExpr::pred("p", vec![Term::var("x")]);
        let exists = TLExpr::exists("x", "Domain", p_x);
        let expr = TLExpr::and(exists.clone(), exists);

        let result = eliminate_common_subexpressions(&expr);
        assert!(result.eliminated_count > 0);
    }

    #[test]
    fn test_cse_preserves_semantics() {
        // Verify that CSE doesn't change the structure inappropriately
        let p_x = TLExpr::pred("p", vec![Term::var("x")]);
        let q_y = TLExpr::pred("q", vec![Term::var("y")]);
        let expr = TLExpr::and(p_x.clone(), q_y.clone());

        let result = eliminate_common_subexpressions(&expr);

        // Should still be an AND of two predicates
        match result.optimized_expr {
            TLExpr::And(left, right) => {
                assert!(matches!(*left, TLExpr::Pred { .. }));
                assert!(matches!(*right, TLExpr::Pred { .. }));
            }
            _ => panic!("Expected And expression"),
        }
    }

    #[test]
    fn test_cse_complex_expression() {
        // p(x) ∧ (q(y) ∨ p(x)) - p(x) appears twice
        let p_x = TLExpr::pred("p", vec![Term::var("x")]);
        let q_y = TLExpr::pred("q", vec![Term::var("y")]);
        let or_expr = TLExpr::or(q_y, p_x.clone());
        let expr = TLExpr::and(p_x, or_expr);

        let result = eliminate_common_subexpressions(&expr);
        assert!(result.eliminated_count > 0);
    }
}