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
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//! IR diff tool for comparing graphs and expressions.
//!
//! This module provides utilities to compare two IR structures and
//! identify differences, useful for debugging and validation.

use crate::{EinsumGraph, EinsumNode, OpType, TLExpr};
use std::collections::HashSet;

/// Difference between two expressions
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExprDiff {
    /// Expressions are identical
    Identical,
    /// Different expression types
    TypeMismatch { left: String, right: String },
    /// Different predicate names or arities
    PredicateMismatch { left: String, right: String },
    /// Different subexpressions
    SubexprMismatch {
        path: Vec<String>,
        left: String,
        right: String,
    },
    /// Different quantifier variables or domains
    QuantifierMismatch {
        left_var: String,
        right_var: String,
        left_domain: String,
        right_domain: String,
    },
}

/// Difference between two graphs
#[derive(Debug, Clone)]
pub struct GraphDiff {
    /// Tensors only in left graph
    pub left_only_tensors: Vec<String>,
    /// Tensors only in right graph
    pub right_only_tensors: Vec<String>,
    /// Nodes only in left graph
    pub left_only_nodes: usize,
    /// Nodes only in right graph
    pub right_only_nodes: usize,
    /// Different node operations
    pub node_differences: Vec<NodeDiff>,
    /// Output differences
    pub output_differences: Vec<String>,
}

/// Difference in a specific node
#[derive(Debug, Clone)]
pub struct NodeDiff {
    pub node_index: usize,
    pub description: String,
}

impl ExprDiff {
    /// Check if expressions are identical
    pub fn is_identical(&self) -> bool {
        matches!(self, ExprDiff::Identical)
    }

    /// Get human-readable description
    pub fn description(&self) -> String {
        match self {
            ExprDiff::Identical => "Expressions are identical".to_string(),
            ExprDiff::TypeMismatch { left, right } => {
                format!("Type mismatch: left={}, right={}", left, right)
            }
            ExprDiff::PredicateMismatch { left, right } => {
                format!("Predicate mismatch: left={}, right={}", left, right)
            }
            ExprDiff::SubexprMismatch { path, left, right } => {
                format!(
                    "Subexpression mismatch at {}: left={}, right={}",
                    path.join("/"),
                    left,
                    right
                )
            }
            ExprDiff::QuantifierMismatch {
                left_var,
                right_var,
                left_domain,
                right_domain,
            } => {
                format!(
                    "Quantifier mismatch: left=({}, {}), right=({}, {})",
                    left_var, left_domain, right_var, right_domain
                )
            }
        }
    }
}

impl GraphDiff {
    /// Check if graphs are identical
    pub fn is_identical(&self) -> bool {
        self.left_only_tensors.is_empty()
            && self.right_only_tensors.is_empty()
            && self.left_only_nodes == 0
            && self.right_only_nodes == 0
            && self.node_differences.is_empty()
            && self.output_differences.is_empty()
    }

    /// Get summary of differences
    pub fn summary(&self) -> String {
        if self.is_identical() {
            return "Graphs are identical".to_string();
        }

        let mut parts = Vec::new();

        if !self.left_only_tensors.is_empty() {
            parts.push(format!(
                "{} tensors only in left",
                self.left_only_tensors.len()
            ));
        }
        if !self.right_only_tensors.is_empty() {
            parts.push(format!(
                "{} tensors only in right",
                self.right_only_tensors.len()
            ));
        }
        if self.left_only_nodes > 0 {
            parts.push(format!("{} nodes only in left", self.left_only_nodes));
        }
        if self.right_only_nodes > 0 {
            parts.push(format!("{} nodes only in right", self.right_only_nodes));
        }
        if !self.node_differences.is_empty() {
            parts.push(format!("{} node differences", self.node_differences.len()));
        }
        if !self.output_differences.is_empty() {
            parts.push(format!(
                "{} output differences",
                self.output_differences.len()
            ));
        }

        parts.join(", ")
    }
}

/// Compare two expressions
pub fn diff_exprs(left: &TLExpr, right: &TLExpr) -> ExprDiff {
    diff_exprs_impl(left, right, &mut Vec::new())
}

fn diff_exprs_impl(left: &TLExpr, right: &TLExpr, path: &mut Vec<String>) -> ExprDiff {
    match (left, right) {
        (TLExpr::Pred { name: n1, args: a1 }, TLExpr::Pred { name: n2, args: a2 }) => {
            if n1 != n2 || a1.len() != a2.len() {
                ExprDiff::PredicateMismatch {
                    left: format!("{}({})", n1, a1.len()),
                    right: format!("{}({})", n2, a2.len()),
                }
            } else {
                ExprDiff::Identical
            }
        }
        (TLExpr::And(l1, r1), TLExpr::And(l2, r2))
        | (TLExpr::Or(l1, r1), TLExpr::Or(l2, r2))
        | (TLExpr::Imply(l1, r1), TLExpr::Imply(l2, r2))
        | (TLExpr::Add(l1, r1), TLExpr::Add(l2, r2))
        | (TLExpr::Sub(l1, r1), TLExpr::Sub(l2, r2))
        | (TLExpr::Mul(l1, r1), TLExpr::Mul(l2, r2))
        | (TLExpr::Div(l1, r1), TLExpr::Div(l2, r2))
        | (TLExpr::Pow(l1, r1), TLExpr::Pow(l2, r2))
        | (TLExpr::Mod(l1, r1), TLExpr::Mod(l2, r2))
        | (TLExpr::Min(l1, r1), TLExpr::Min(l2, r2))
        | (TLExpr::Max(l1, r1), TLExpr::Max(l2, r2))
        | (TLExpr::Eq(l1, r1), TLExpr::Eq(l2, r2))
        | (TLExpr::Lt(l1, r1), TLExpr::Lt(l2, r2))
        | (TLExpr::Gt(l1, r1), TLExpr::Gt(l2, r2))
        | (TLExpr::Lte(l1, r1), TLExpr::Lte(l2, r2))
        | (TLExpr::Gte(l1, r1), TLExpr::Gte(l2, r2)) => {
            path.push("left".to_string());
            let left_diff = diff_exprs_impl(l1, l2, path);
            path.pop();

            if !left_diff.is_identical() {
                return left_diff;
            }

            path.push("right".to_string());
            let right_diff = diff_exprs_impl(r1, r2, path);
            path.pop();

            right_diff
        }
        (TLExpr::Not(e1), TLExpr::Not(e2))
        | (TLExpr::Score(e1), TLExpr::Score(e2))
        | (TLExpr::Abs(e1), TLExpr::Abs(e2))
        | (TLExpr::Floor(e1), TLExpr::Floor(e2))
        | (TLExpr::Ceil(e1), TLExpr::Ceil(e2))
        | (TLExpr::Round(e1), TLExpr::Round(e2))
        | (TLExpr::Sqrt(e1), TLExpr::Sqrt(e2))
        | (TLExpr::Exp(e1), TLExpr::Exp(e2))
        | (TLExpr::Log(e1), TLExpr::Log(e2))
        | (TLExpr::Sin(e1), TLExpr::Sin(e2))
        | (TLExpr::Cos(e1), TLExpr::Cos(e2))
        | (TLExpr::Tan(e1), TLExpr::Tan(e2)) => {
            path.push("inner".to_string());
            let diff = diff_exprs_impl(e1, e2, path);
            path.pop();
            diff
        }
        (
            TLExpr::Exists {
                var: v1,
                domain: d1,
                body: b1,
            },
            TLExpr::Exists {
                var: v2,
                domain: d2,
                body: b2,
            },
        )
        | (
            TLExpr::ForAll {
                var: v1,
                domain: d1,
                body: b1,
            },
            TLExpr::ForAll {
                var: v2,
                domain: d2,
                body: b2,
            },
        ) => {
            if v1 != v2 || d1 != d2 {
                return ExprDiff::QuantifierMismatch {
                    left_var: v1.clone(),
                    right_var: v2.clone(),
                    left_domain: d1.clone(),
                    right_domain: d2.clone(),
                };
            }

            path.push("body".to_string());
            let diff = diff_exprs_impl(b1, b2, path);
            path.pop();
            diff
        }
        (
            TLExpr::IfThenElse {
                condition: c1,
                then_branch: t1,
                else_branch: e1,
            },
            TLExpr::IfThenElse {
                condition: c2,
                then_branch: t2,
                else_branch: e2,
            },
        ) => {
            path.push("condition".to_string());
            let cond_diff = diff_exprs_impl(c1, c2, path);
            path.pop();

            if !cond_diff.is_identical() {
                return cond_diff;
            }

            path.push("then".to_string());
            let then_diff = diff_exprs_impl(t1, t2, path);
            path.pop();

            if !then_diff.is_identical() {
                return then_diff;
            }

            path.push("else".to_string());
            let else_diff = diff_exprs_impl(e1, e2, path);
            path.pop();

            else_diff
        }
        (TLExpr::Constant(c1), TLExpr::Constant(c2)) => {
            if (c1 - c2).abs() < f64::EPSILON {
                ExprDiff::Identical
            } else {
                ExprDiff::SubexprMismatch {
                    path: path.clone(),
                    left: format!("{}", c1),
                    right: format!("{}", c2),
                }
            }
        }
        (TLExpr::SymbolLiteral(s1), TLExpr::SymbolLiteral(s2)) => {
            if s1 == s2 {
                ExprDiff::Identical
            } else {
                ExprDiff::SubexprMismatch {
                    path: path.clone(),
                    left: format!(":{s1}"),
                    right: format!(":{s2}"),
                }
            }
        }
        (
            TLExpr::Match {
                scrutinee: s1,
                arms: a1,
            },
            TLExpr::Match {
                scrutinee: s2,
                arms: a2,
            },
        ) => {
            path.push("scrutinee".to_string());
            let sd = diff_exprs_impl(s1, s2, path);
            path.pop();
            if !matches!(sd, ExprDiff::Identical) {
                return sd;
            }
            if a1.len() != a2.len() {
                return ExprDiff::SubexprMismatch {
                    path: path.clone(),
                    left: format!("{} arms", a1.len()),
                    right: format!("{} arms", a2.len()),
                };
            }
            for (i, ((p1, b1), (p2, b2))) in a1.iter().zip(a2.iter()).enumerate() {
                if p1 != p2 {
                    return ExprDiff::SubexprMismatch {
                        path: path.clone(),
                        left: format!("arm[{i}] pattern {p1}"),
                        right: format!("arm[{i}] pattern {p2}"),
                    };
                }
                path.push(format!("arm[{i}]"));
                let bd = diff_exprs_impl(b1, b2, path);
                path.pop();
                if !matches!(bd, ExprDiff::Identical) {
                    return bd;
                }
            }
            ExprDiff::Identical
        }
        _ => ExprDiff::TypeMismatch {
            left: format!("{:?}", left)
                .split('(')
                .next()
                .unwrap_or("unknown")
                .to_string(),
            right: format!("{:?}", right)
                .split('(')
                .next()
                .unwrap_or("unknown")
                .to_string(),
        },
    }
}

/// Compare two graphs
pub fn diff_graphs(left: &EinsumGraph, right: &EinsumGraph) -> GraphDiff {
    let left_tensors: HashSet<_> = left.tensors.iter().collect();
    let right_tensors: HashSet<_> = right.tensors.iter().collect();

    let left_only_tensors: Vec<String> = left_tensors
        .difference(&right_tensors)
        .map(|s| s.to_string())
        .collect();
    let right_only_tensors: Vec<String> = right_tensors
        .difference(&left_tensors)
        .map(|s| s.to_string())
        .collect();

    let node_differences = diff_nodes(&left.nodes, &right.nodes);

    let left_only_nodes = if left.nodes.len() > right.nodes.len() {
        left.nodes.len() - right.nodes.len()
    } else {
        0
    };
    let right_only_nodes = if right.nodes.len() > left.nodes.len() {
        right.nodes.len() - left.nodes.len()
    } else {
        0
    };

    let output_differences = diff_outputs(&left.outputs, &right.outputs);

    GraphDiff {
        left_only_tensors,
        right_only_tensors,
        left_only_nodes,
        right_only_nodes,
        node_differences,
        output_differences,
    }
}

fn diff_nodes(left: &[EinsumNode], right: &[EinsumNode]) -> Vec<NodeDiff> {
    let mut differences = Vec::new();
    let min_len = left.len().min(right.len());

    for i in 0..min_len {
        if let Some(diff) = diff_node(&left[i], &right[i], i) {
            differences.push(diff);
        }
    }

    differences
}

fn diff_node(left: &EinsumNode, right: &EinsumNode, index: usize) -> Option<NodeDiff> {
    if left.inputs != right.inputs {
        return Some(NodeDiff {
            node_index: index,
            description: format!("Different inputs: {:?} vs {:?}", left.inputs, right.inputs),
        });
    }

    if left.outputs != right.outputs {
        return Some(NodeDiff {
            node_index: index,
            description: format!(
                "Different outputs: {:?} vs {:?}",
                left.outputs, right.outputs
            ),
        });
    }

    if !ops_equal(&left.op, &right.op) {
        return Some(NodeDiff {
            node_index: index,
            description: format!("Different operations: {:?} vs {:?}", left.op, right.op),
        });
    }

    None
}

fn ops_equal(left: &OpType, right: &OpType) -> bool {
    // Simple discriminant comparison
    std::mem::discriminant(left) == std::mem::discriminant(right)
}

fn diff_outputs(left: &[usize], right: &[usize]) -> Vec<String> {
    let mut differences = Vec::new();

    if left.len() != right.len() {
        differences.push(format!(
            "Different number of outputs: {} vs {}",
            left.len(),
            right.len()
        ));
    }

    for (i, (l, r)) in left.iter().zip(right.iter()).enumerate() {
        if l != r {
            differences.push(format!("Output {} differs: {} vs {}", i, l, r));
        }
    }

    differences
}

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

    #[test]
    fn test_identical_exprs() {
        let expr1 = TLExpr::pred("p", vec![Term::var("x")]);
        let expr2 = TLExpr::pred("p", vec![Term::var("x")]);

        let diff = diff_exprs(&expr1, &expr2);
        assert!(diff.is_identical());
    }

    #[test]
    fn test_different_predicates() {
        let expr1 = TLExpr::pred("p", vec![Term::var("x")]);
        let expr2 = TLExpr::pred("q", vec![Term::var("x")]);

        let diff = diff_exprs(&expr1, &expr2);
        assert!(!diff.is_identical());
        assert!(matches!(diff, ExprDiff::PredicateMismatch { .. }));
    }

    #[test]
    fn test_different_types() {
        let expr1 = TLExpr::pred("p", vec![Term::var("x")]);
        let expr2 = TLExpr::constant(1.0);

        let diff = diff_exprs(&expr1, &expr2);
        assert!(!diff.is_identical());
        assert!(matches!(diff, ExprDiff::TypeMismatch { .. }));
    }

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

        let diff = diff_exprs(&expr1, &expr2);
        assert!(!diff.is_identical());
    }

    #[test]
    fn test_quantifier_difference() {
        let expr1 = TLExpr::exists("x", "Domain1", TLExpr::pred("p", vec![Term::var("x")]));
        let expr2 = TLExpr::exists("y", "Domain2", TLExpr::pred("p", vec![Term::var("y")]));

        let diff = diff_exprs(&expr1, &expr2);
        assert!(!diff.is_identical());
        assert!(matches!(diff, ExprDiff::QuantifierMismatch { .. }));
    }

    #[test]
    fn test_identical_graphs() {
        let graph1 = EinsumGraph {
            tensors: vec!["t0".to_string()],
            nodes: vec![],
            inputs: vec![],
            outputs: vec![0],
            tensor_metadata: std::collections::HashMap::new(),
        };
        let graph2 = EinsumGraph {
            tensors: vec!["t0".to_string()],
            nodes: vec![],
            inputs: vec![],
            outputs: vec![0],
            tensor_metadata: std::collections::HashMap::new(),
        };

        let diff = diff_graphs(&graph1, &graph2);
        assert!(diff.is_identical());
    }

    #[test]
    fn test_different_tensor_count() {
        let graph1 = EinsumGraph {
            tensors: vec!["t0".to_string(), "t1".to_string()],
            nodes: vec![],
            inputs: vec![],
            outputs: vec![],
            tensor_metadata: std::collections::HashMap::new(),
        };
        let graph2 = EinsumGraph {
            tensors: vec!["t0".to_string()],
            nodes: vec![],
            inputs: vec![],
            outputs: vec![],
            tensor_metadata: std::collections::HashMap::new(),
        };

        let diff = diff_graphs(&graph1, &graph2);
        assert!(!diff.is_identical());
        assert_eq!(diff.left_only_tensors.len(), 1);
    }

    #[test]
    fn test_different_outputs() {
        let graph1 = EinsumGraph {
            tensors: vec!["t0".to_string()],
            nodes: vec![],
            inputs: vec![],
            outputs: vec![0],
            tensor_metadata: std::collections::HashMap::new(),
        };
        let graph2 = EinsumGraph {
            tensors: vec!["t0".to_string()],
            nodes: vec![],
            inputs: vec![],
            outputs: vec![1],
            tensor_metadata: std::collections::HashMap::new(),
        };

        let diff = diff_graphs(&graph1, &graph2);
        assert!(!diff.is_identical());
        assert!(!diff.output_differences.is_empty());
    }

    #[test]
    fn test_diff_summary() {
        let diff = GraphDiff {
            left_only_tensors: vec!["t1".to_string()],
            right_only_tensors: vec!["t2".to_string()],
            left_only_nodes: 0,
            right_only_nodes: 0,
            node_differences: vec![],
            output_differences: vec![],
        };

        let summary = diff.summary();
        assert!(summary.contains("tensors only in left"));
        assert!(summary.contains("tensors only in right"));
    }
}