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
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
//! Enhanced error diagnostics and messaging.

use anyhow::{anyhow, Result};
use tensorlogic_ir::{IrError, SourceSpan, TLExpr, Term};

use super::scope_analysis::analyze_scopes;

/// Diagnostic level
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiagnosticLevel {
    Error,
    Warning,
    Info,
    Hint,
}

/// A diagnostic message with location and context
#[derive(Debug, Clone)]
pub struct Diagnostic {
    pub level: DiagnosticLevel,
    pub message: String,
    pub span: Option<SourceSpan>,
    pub help: Option<String>,
    pub related: Vec<(String, Option<SourceSpan>)>,
}

impl Diagnostic {
    pub fn error(message: impl Into<String>) -> Self {
        Diagnostic {
            level: DiagnosticLevel::Error,
            message: message.into(),
            span: None,
            help: None,
            related: Vec::new(),
        }
    }

    pub fn warning(message: impl Into<String>) -> Self {
        Diagnostic {
            level: DiagnosticLevel::Warning,
            message: message.into(),
            span: None,
            help: None,
            related: Vec::new(),
        }
    }

    pub fn with_span(mut self, span: SourceSpan) -> Self {
        self.span = Some(span);
        self
    }

    pub fn with_help(mut self, help: impl Into<String>) -> Self {
        self.help = Some(help.into());
        self
    }

    pub fn with_related(mut self, msg: impl Into<String>, span: Option<SourceSpan>) -> Self {
        self.related.push((msg.into(), span));
        self
    }

    /// Format the diagnostic for display
    pub fn format(&self) -> String {
        let mut output = String::new();

        let level_str = match self.level {
            DiagnosticLevel::Error => "error",
            DiagnosticLevel::Warning => "warning",
            DiagnosticLevel::Info => "info",
            DiagnosticLevel::Hint => "hint",
        };

        if let Some(ref span) = self.span {
            output.push_str(&format!("{}: {}: {}\n", level_str, span, self.message));
        } else {
            output.push_str(&format!("{}: {}\n", level_str, self.message));
        }

        if let Some(ref help) = self.help {
            output.push_str(&format!("  help: {}\n", help));
        }

        for (msg, span_opt) in &self.related {
            if let Some(span) = span_opt {
                output.push_str(&format!("  note: {}: {}\n", span, msg));
            } else {
                output.push_str(&format!("  note: {}\n", msg));
            }
        }

        output
    }
}

/// Enhanced error message builder
pub struct DiagnosticBuilder {
    diagnostics: Vec<Diagnostic>,
}

impl DiagnosticBuilder {
    pub fn new() -> Self {
        DiagnosticBuilder {
            diagnostics: Vec::new(),
        }
    }

    pub fn add(&mut self, diagnostic: Diagnostic) {
        self.diagnostics.push(diagnostic);
    }

    pub fn has_errors(&self) -> bool {
        self.diagnostics
            .iter()
            .any(|d| d.level == DiagnosticLevel::Error)
    }

    pub fn error_count(&self) -> usize {
        self.diagnostics
            .iter()
            .filter(|d| d.level == DiagnosticLevel::Error)
            .count()
    }

    pub fn into_result(self) -> Result<()> {
        if self.has_errors() {
            let mut msg = String::new();
            for diag in &self.diagnostics {
                msg.push_str(&diag.format());
            }
            Err(anyhow!("{}", msg))
        } else {
            Ok(())
        }
    }

    pub fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }
}

impl Default for DiagnosticBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Enhance an IrError with better diagnostics
pub fn enhance_error(error: IrError) -> Diagnostic {
    match error {
        IrError::ArityMismatch {
            name,
            expected,
            actual,
        } => Diagnostic::error(format!(
            "Predicate '{}' arity mismatch: expected {} arguments, got {}",
            name, expected, actual
        ))
        .with_help(format!(
            "Change the number of arguments to match the expected arity of {}",
            expected
        )),
        IrError::TypeMismatch {
            name,
            arg_index,
            expected,
            actual,
        } => Diagnostic::error(format!(
            "Type mismatch in predicate '{}' at argument {}: expected '{}', got '{}'",
            name, arg_index, expected, actual
        ))
        .with_help(format!(
            "Change argument {} to have type '{}'",
            arg_index, expected
        )),
        IrError::UnboundVariable { var } => {
            Diagnostic::error(format!("Variable '{}' is not bound by any quantifier", var))
                .with_help(format!(
                    "Add a quantifier: ∀{}. <expr> or ∃{}. <expr>",
                    var, var
                ))
        }
        IrError::InconsistentTypes { var, type1, type2 } => Diagnostic::error(format!(
            "Variable '{}' used with inconsistent types: '{}' and '{}'",
            var, type1, type2
        ))
        .with_help("Ensure the variable has the same type in all uses".to_string()),
        _ => Diagnostic::error(format!("{}", error)),
    }
}

/// Generate diagnostics for an expression
pub fn diagnose_expression(expr: &TLExpr) -> Vec<Diagnostic> {
    let mut diagnostics = Vec::new();

    // Check for unbound variables
    if let Ok(scope_result) = analyze_scopes(expr) {
        for unbound_var in &scope_result.unbound_variables {
            let diag = Diagnostic::error(format!("Unbound variable '{}'", unbound_var)).with_help(
                format!(
                    "Consider adding a universal quantifier: ∀{}. <expr>",
                    unbound_var
                ),
            );
            diagnostics.push(diag);
        }

        // Check for type conflicts
        for conflict in &scope_result.type_conflicts {
            let diag = Diagnostic::error(format!(
                "Variable '{}' has conflicting types: '{}' and '{}'",
                conflict.variable, conflict.type1, conflict.type2
            ))
            .with_help("Ensure the variable has consistent types across all uses".to_string());
            diagnostics.push(diag);
        }
    }

    // Warn about variables bound but never used
    diagnose_unused_bindings(expr, &mut diagnostics);

    diagnostics
}

fn diagnose_unused_bindings(expr: &TLExpr, diagnostics: &mut Vec<Diagnostic>) {
    match expr {
        TLExpr::Exists {
            var,
            domain: _,
            body,
        }
        | TLExpr::ForAll {
            var,
            domain: _,
            body,
        }
        | TLExpr::SoftExists {
            var,
            domain: _,
            body,
            ..
        }
        | TLExpr::SoftForAll {
            var,
            domain: _,
            body,
            ..
        }
        | TLExpr::Aggregate {
            var,
            domain: _,
            body,
            ..
        } => {
            // Check if variable is actually used in the body
            if !uses_variable(body, var) {
                diagnostics.push(
                    Diagnostic::warning(format!("Variable '{}' is bound but never used", var))
                        .with_help(format!("Consider removing the quantifier for '{}'", var)),
                );
            }
            diagnose_unused_bindings(body, diagnostics);
        }
        TLExpr::And(left, right)
        | TLExpr::Or(left, right)
        | TLExpr::Imply(left, right)
        | TLExpr::Add(left, right)
        | TLExpr::Sub(left, right)
        | TLExpr::Mul(left, right)
        | TLExpr::Div(left, right)
        | TLExpr::Pow(left, right)
        | TLExpr::Mod(left, right)
        | TLExpr::Min(left, right)
        | TLExpr::Max(left, right)
        | TLExpr::Eq(left, right)
        | TLExpr::Lt(left, right)
        | TLExpr::Gt(left, right)
        | TLExpr::Lte(left, right)
        | TLExpr::Gte(left, right)
        | TLExpr::TNorm { left, right, .. }
        | TLExpr::TCoNorm { left, right, .. }
        | TLExpr::FuzzyImplication {
            premise: left,
            conclusion: right,
            ..
        } => {
            diagnose_unused_bindings(left, diagnostics);
            diagnose_unused_bindings(right, diagnostics);
        }
        TLExpr::Not(inner)
        | TLExpr::Score(inner)
        | TLExpr::Abs(inner)
        | TLExpr::Floor(inner)
        | TLExpr::Ceil(inner)
        | TLExpr::Round(inner)
        | TLExpr::Sqrt(inner)
        | TLExpr::Exp(inner)
        | TLExpr::Log(inner)
        | TLExpr::Sin(inner)
        | TLExpr::Cos(inner)
        | TLExpr::Tan(inner)
        | TLExpr::FuzzyNot { expr: inner, .. }
        | TLExpr::WeightedRule { rule: inner, .. } => {
            diagnose_unused_bindings(inner, diagnostics);
        }
        TLExpr::Let {
            var: _,
            value,
            body,
        } => {
            diagnose_unused_bindings(value, diagnostics);
            diagnose_unused_bindings(body, diagnostics);
        }
        TLExpr::IfThenElse {
            condition,
            then_branch,
            else_branch,
        } => {
            diagnose_unused_bindings(condition, diagnostics);
            diagnose_unused_bindings(then_branch, diagnostics);
            diagnose_unused_bindings(else_branch, diagnostics);
        }

        // Modal/temporal logic operators - not yet implemented, pass through with recursion
        TLExpr::Box(inner)
        | TLExpr::Diamond(inner)
        | TLExpr::Next(inner)
        | TLExpr::Eventually(inner)
        | TLExpr::Always(inner) => {
            diagnose_unused_bindings(inner, diagnostics);
        }
        TLExpr::Until { before, after }
        | TLExpr::Release {
            released: before,
            releaser: after,
        }
        | TLExpr::WeakUntil { before, after }
        | TLExpr::StrongRelease {
            released: before,
            releaser: after,
        } => {
            diagnose_unused_bindings(before, diagnostics);
            diagnose_unused_bindings(after, diagnostics);
        }
        TLExpr::ProbabilisticChoice { alternatives } => {
            for (_weight, alt_expr) in alternatives {
                diagnose_unused_bindings(alt_expr, diagnostics);
            }
        }

        TLExpr::Pred { .. } => {}
        TLExpr::Constant(_) => {}
        // All other expression types (enhancements)
        _ => {}
    }
}

fn uses_variable(expr: &TLExpr, var_name: &str) -> bool {
    match expr {
        TLExpr::Pred { name: _, args } => args.iter().any(|term| match term {
            Term::Var(v) => v == var_name,
            Term::Typed { value, .. } => uses_variable_in_term(value, var_name),
            _ => false,
        }),
        TLExpr::And(left, right)
        | TLExpr::Or(left, right)
        | TLExpr::Imply(left, right)
        | TLExpr::Add(left, right)
        | TLExpr::Sub(left, right)
        | TLExpr::Mul(left, right)
        | TLExpr::Div(left, right)
        | TLExpr::Pow(left, right)
        | TLExpr::Mod(left, right)
        | TLExpr::Min(left, right)
        | TLExpr::Max(left, right)
        | TLExpr::Eq(left, right)
        | TLExpr::Lt(left, right)
        | TLExpr::Gt(left, right)
        | TLExpr::Lte(left, right)
        | TLExpr::Gte(left, right)
        | TLExpr::TNorm { left, right, .. }
        | TLExpr::TCoNorm { left, right, .. }
        | TLExpr::FuzzyImplication {
            premise: left,
            conclusion: right,
            ..
        } => uses_variable(left, var_name) || uses_variable(right, var_name),
        TLExpr::Not(inner)
        | TLExpr::Score(inner)
        | TLExpr::Abs(inner)
        | TLExpr::Floor(inner)
        | TLExpr::Ceil(inner)
        | TLExpr::Round(inner)
        | TLExpr::Sqrt(inner)
        | TLExpr::Exp(inner)
        | TLExpr::Log(inner)
        | TLExpr::Sin(inner)
        | TLExpr::Cos(inner)
        | TLExpr::Tan(inner)
        | TLExpr::FuzzyNot { expr: inner, .. }
        | TLExpr::WeightedRule { rule: inner, .. } => uses_variable(inner, var_name),
        TLExpr::Let {
            var: _,
            value,
            body,
        } => uses_variable(value, var_name) || uses_variable(body, var_name),
        TLExpr::Exists {
            var: _,
            domain: _,
            body,
        }
        | TLExpr::ForAll {
            var: _,
            domain: _,
            body,
        }
        | TLExpr::SoftExists {
            var: _,
            domain: _,
            body,
            ..
        }
        | TLExpr::SoftForAll {
            var: _,
            domain: _,
            body,
            ..
        }
        | TLExpr::Aggregate {
            var: _,
            domain: _,
            body,
            ..
        } => uses_variable(body, var_name),
        TLExpr::IfThenElse {
            condition,
            then_branch,
            else_branch,
        } => {
            uses_variable(condition, var_name)
                || uses_variable(then_branch, var_name)
                || uses_variable(else_branch, var_name)
        }

        // Modal/temporal logic operators - not yet implemented, pass through with recursion
        TLExpr::Box(inner)
        | TLExpr::Diamond(inner)
        | TLExpr::Next(inner)
        | TLExpr::Eventually(inner)
        | TLExpr::Always(inner) => uses_variable(inner, var_name),
        TLExpr::Until { before, after }
        | TLExpr::Release {
            released: before,
            releaser: after,
        }
        | TLExpr::WeakUntil { before, after }
        | TLExpr::StrongRelease {
            released: before,
            releaser: after,
        } => uses_variable(before, var_name) || uses_variable(after, var_name),
        TLExpr::ProbabilisticChoice { alternatives } => alternatives
            .iter()
            .any(|(_weight, alt_expr)| uses_variable(alt_expr, var_name)),

        TLExpr::Constant(_) => false,
        // All other expression types (enhancements)
        _ => false,
    }
}

fn uses_variable_in_term(term: &Term, var_name: &str) -> bool {
    match term {
        Term::Var(v) => v == var_name,
        Term::Typed { value, .. } => uses_variable_in_term(value, var_name),
        _ => false,
    }
}

/// Pretty-print an expression for error messages
pub fn pretty_print_expr(expr: &TLExpr) -> String {
    match expr {
        TLExpr::Pred { name, args } => {
            if args.is_empty() {
                name.clone()
            } else {
                let args_str = args
                    .iter()
                    .map(pretty_print_term)
                    .collect::<Vec<_>>()
                    .join(", ");
                format!("{}({})", name, args_str)
            }
        }
        TLExpr::And(left, right) => {
            format!(
                "({}{})",
                pretty_print_expr(left),
                pretty_print_expr(right)
            )
        }
        TLExpr::Or(left, right) => {
            format!(
                "({}{})",
                pretty_print_expr(left),
                pretty_print_expr(right)
            )
        }
        TLExpr::Not(inner) => format!("¬{}", pretty_print_expr(inner)),
        TLExpr::Imply(premise, conclusion) => {
            format!(
                "({}{})",
                pretty_print_expr(premise),
                pretty_print_expr(conclusion)
            )
        }
        TLExpr::Exists { var, domain, body } => {
            format!("{}:{}. {}", var, domain, pretty_print_expr(body))
        }
        TLExpr::ForAll { var, domain, body } => {
            format!("{}:{}. {}", var, domain, pretty_print_expr(body))
        }
        TLExpr::Aggregate {
            op,
            var,
            domain,
            body,
            group_by,
        } => {
            let group_str = if let Some(gb) = group_by {
                format!(" GROUP BY {}", gb.join(", "))
            } else {
                String::new()
            };
            format!(
                "AGG[{}]({}:{}. {}){}",
                op,
                var,
                domain,
                pretty_print_expr(body),
                group_str
            )
        }
        TLExpr::Score(inner) => format!("score({})", pretty_print_expr(inner)),
        TLExpr::Add(left, right) => {
            format!(
                "({} + {})",
                pretty_print_expr(left),
                pretty_print_expr(right)
            )
        }
        TLExpr::Sub(left, right) => {
            format!(
                "({} - {})",
                pretty_print_expr(left),
                pretty_print_expr(right)
            )
        }
        TLExpr::Mul(left, right) => {
            format!(
                "({} * {})",
                pretty_print_expr(left),
                pretty_print_expr(right)
            )
        }
        TLExpr::Div(left, right) => {
            format!(
                "({} / {})",
                pretty_print_expr(left),
                pretty_print_expr(right)
            )
        }
        TLExpr::Eq(left, right) => {
            format!(
                "({} = {})",
                pretty_print_expr(left),
                pretty_print_expr(right)
            )
        }
        TLExpr::Lt(left, right) => {
            format!(
                "({} < {})",
                pretty_print_expr(left),
                pretty_print_expr(right)
            )
        }
        TLExpr::Gt(left, right) => {
            format!(
                "({} > {})",
                pretty_print_expr(left),
                pretty_print_expr(right)
            )
        }
        TLExpr::Lte(left, right) => {
            format!(
                "({}{})",
                pretty_print_expr(left),
                pretty_print_expr(right)
            )
        }
        TLExpr::Gte(left, right) => {
            format!(
                "({}{})",
                pretty_print_expr(left),
                pretty_print_expr(right)
            )
        }
        TLExpr::Pow(left, right) => {
            format!(
                "({} ^ {})",
                pretty_print_expr(left),
                pretty_print_expr(right)
            )
        }
        TLExpr::Mod(left, right) => {
            format!(
                "({} % {})",
                pretty_print_expr(left),
                pretty_print_expr(right)
            )
        }
        TLExpr::Min(left, right) => {
            format!(
                "min({}, {})",
                pretty_print_expr(left),
                pretty_print_expr(right)
            )
        }
        TLExpr::Max(left, right) => {
            format!(
                "max({}, {})",
                pretty_print_expr(left),
                pretty_print_expr(right)
            )
        }
        TLExpr::Abs(inner) => format!("abs({})", pretty_print_expr(inner)),
        TLExpr::Floor(inner) => format!("floor({})", pretty_print_expr(inner)),
        TLExpr::Ceil(inner) => format!("ceil({})", pretty_print_expr(inner)),
        TLExpr::Round(inner) => format!("round({})", pretty_print_expr(inner)),
        TLExpr::Sqrt(inner) => format!("sqrt({})", pretty_print_expr(inner)),
        TLExpr::Exp(inner) => format!("exp({})", pretty_print_expr(inner)),
        TLExpr::Log(inner) => format!("log({})", pretty_print_expr(inner)),
        TLExpr::Sin(inner) => format!("sin({})", pretty_print_expr(inner)),
        TLExpr::Cos(inner) => format!("cos({})", pretty_print_expr(inner)),
        TLExpr::Tan(inner) => format!("tan({})", pretty_print_expr(inner)),
        TLExpr::Let { var, value, body } => {
            format!(
                "let {} = {} in {}",
                var,
                pretty_print_expr(value),
                pretty_print_expr(body)
            )
        }
        TLExpr::IfThenElse {
            condition,
            then_branch,
            else_branch,
        } => {
            format!(
                "if {} then {} else {}",
                pretty_print_expr(condition),
                pretty_print_expr(then_branch),
                pretty_print_expr(else_branch)
            )
        }

        // Modal/temporal logic operators
        TLExpr::Box(inner) => format!("{}", pretty_print_expr(inner)),
        TLExpr::Diamond(inner) => format!("{}", pretty_print_expr(inner)),
        TLExpr::Next(inner) => format!("X{}", pretty_print_expr(inner)),
        TLExpr::Eventually(inner) => format!("F{}", pretty_print_expr(inner)),
        TLExpr::Always(inner) => format!("G{}", pretty_print_expr(inner)),
        TLExpr::Until { before, after } => {
            format!(
                "({} U {})",
                pretty_print_expr(before),
                pretty_print_expr(after)
            )
        }

        // Fuzzy logic operators
        TLExpr::TNorm { kind, left, right } => {
            format!(
                "({} ⊗_{:?} {})",
                pretty_print_expr(left),
                kind,
                pretty_print_expr(right)
            )
        }
        TLExpr::TCoNorm { kind, left, right } => {
            format!(
                "({} ⊕_{:?} {})",
                pretty_print_expr(left),
                kind,
                pretty_print_expr(right)
            )
        }
        TLExpr::FuzzyNot { kind, expr } => {
            format!("¬_{:?}({})", kind, pretty_print_expr(expr))
        }
        TLExpr::FuzzyImplication {
            kind,
            premise,
            conclusion,
        } => {
            format!(
                "({} →_{:?} {})",
                pretty_print_expr(premise),
                kind,
                pretty_print_expr(conclusion)
            )
        }
        TLExpr::SoftExists {
            var,
            domain,
            body,
            temperature,
        } => {
            format!(
                "{}:{}[T={}]. {}",
                var,
                domain,
                temperature,
                pretty_print_expr(body)
            )
        }
        TLExpr::SoftForAll {
            var,
            domain,
            body,
            temperature,
        } => {
            format!(
                "{}:{}[T={}]. {}",
                var,
                domain,
                temperature,
                pretty_print_expr(body)
            )
        }
        TLExpr::WeightedRule { weight, rule } => {
            format!("{}::{}", weight, pretty_print_expr(rule))
        }
        TLExpr::ProbabilisticChoice { alternatives } => {
            let alt_strs: Vec<String> = alternatives
                .iter()
                .map(|(prob, expr)| format!("{}:{}", prob, pretty_print_expr(expr)))
                .collect();
            format!("CHOICE[{}]", alt_strs.join(", "))
        }
        TLExpr::Release { released, releaser } => {
            format!(
                "({} R {})",
                pretty_print_expr(released),
                pretty_print_expr(releaser)
            )
        }
        TLExpr::WeakUntil { before, after } => {
            format!(
                "({} W {})",
                pretty_print_expr(before),
                pretty_print_expr(after)
            )
        }
        TLExpr::StrongRelease { released, releaser } => {
            format!(
                "({} M {})",
                pretty_print_expr(released),
                pretty_print_expr(releaser)
            )
        }

        TLExpr::Constant(value) => format!("{}", value),
        // All other expression types (enhancements)
        _ => "<expr>".to_string(),
    }
}

fn pretty_print_term(term: &Term) -> String {
    match term {
        Term::Var(v) => v.clone(),
        Term::Const(c) => c.clone(),
        Term::Typed {
            value,
            type_annotation,
        } => {
            format!("{}:{}", pretty_print_term(value), type_annotation.type_name)
        }
    }
}

/// Create a detailed error message for common compilation errors
pub fn create_detailed_error(
    error_type: &str,
    expr: &TLExpr,
    context: &str,
    suggestion: Option<&str>,
) -> Diagnostic {
    let expr_str = pretty_print_expr(expr);
    let truncated = if expr_str.len() > 100 {
        // Safely truncate at a character boundary
        let mut end = 97;
        while end > 0 && !expr_str.is_char_boundary(end) {
            end -= 1;
        }
        format!("{}...", &expr_str[..end])
    } else {
        expr_str
    };

    let mut diag = Diagnostic::error(format!("{}: {}", error_type, context))
        .with_related(format!("In expression: {}", truncated), None);

    if let Some(sugg) = suggestion {
        diag = diag.with_help(sugg.to_string());
    }

    diag
}

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

    #[test]
    fn test_diagnostic_creation() {
        let diag = Diagnostic::error("Test error")
            .with_help("Fix it like this")
            .with_related("Related info", None);

        assert_eq!(diag.level, DiagnosticLevel::Error);
        assert_eq!(diag.message, "Test error");
        assert!(diag.help.is_some());
        assert_eq!(diag.related.len(), 1);
    }

    #[test]
    fn test_diagnostic_format() {
        let diag = Diagnostic::error("Test error").with_help("Fix it");
        let formatted = diag.format();

        assert!(formatted.contains("error"));
        assert!(formatted.contains("Test error"));
        assert!(formatted.contains("help"));
    }

    #[test]
    fn test_diagnostic_with_span() {
        let span = SourceSpan::single(SourceLocation::new("test.tl", 10, 5));
        let diag = Diagnostic::error("Test error").with_span(span);

        let formatted = diag.format();
        assert!(formatted.contains("test.tl"));
        assert!(formatted.contains("10"));
    }

    #[test]
    fn test_diagnostic_builder() {
        let mut builder = DiagnosticBuilder::new();

        builder.add(Diagnostic::error("Error 1"));
        builder.add(Diagnostic::warning("Warning 1"));
        builder.add(Diagnostic::error("Error 2"));

        assert!(builder.has_errors());
        assert_eq!(builder.error_count(), 2);
        assert_eq!(builder.diagnostics().len(), 3);
    }

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

        let diagnostics = diagnose_expression(&expr);
        assert_eq!(diagnostics.len(), 1);
        assert_eq!(diagnostics[0].level, DiagnosticLevel::Error);
        assert!(diagnostics[0].message.contains("Unbound"));
    }

    #[test]
    fn test_diagnose_unused_binding() {
        let expr = TLExpr::exists(
            "x",
            "Domain",
            TLExpr::pred("p", vec![Term::var("y")]), // x is bound but y is used
        );

        let diagnostics = diagnose_expression(&expr);
        // Should have warnings about unused binding
        let warnings: Vec<_> = diagnostics
            .iter()
            .filter(|d| d.level == DiagnosticLevel::Warning)
            .collect();
        assert!(!warnings.is_empty());
    }

    #[test]
    fn test_enhance_arity_error() {
        let error = IrError::ArityMismatch {
            name: "knows".to_string(),
            expected: 2,
            actual: 1,
        };

        let diag = enhance_error(error);
        assert_eq!(diag.level, DiagnosticLevel::Error);
        assert!(diag.message.contains("arity mismatch"));
        assert!(diag.help.is_some());
    }

    #[test]
    fn test_enhance_type_error() {
        let error = IrError::TypeMismatch {
            name: "knows".to_string(),
            arg_index: 1,
            expected: "Person".to_string(),
            actual: "Thing".to_string(),
        };

        let diag = enhance_error(error);
        assert!(diag.message.contains("Type mismatch"));
        assert!(diag.help.is_some());
    }

    #[test]
    fn test_pretty_print_predicate() {
        let expr = TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]);
        let pretty = pretty_print_expr(&expr);
        assert_eq!(pretty, "knows(x, y)");
    }

    #[test]
    fn test_pretty_print_quantifier() {
        let expr = TLExpr::exists(
            "x",
            "Person",
            TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]),
        );
        let pretty = pretty_print_expr(&expr);
        assert!(pretty.contains("∃x:Person"));
        assert!(pretty.contains("knows(x, y)"));
    }

    #[test]
    fn test_pretty_print_complex() {
        let expr = TLExpr::and(
            TLExpr::pred("p", vec![Term::var("x")]),
            TLExpr::negate(TLExpr::pred("q", vec![Term::var("y")])),
        );
        let pretty = pretty_print_expr(&expr);
        assert!(pretty.contains(""));
        assert!(pretty.contains("¬"));
    }

    #[test]
    fn test_pretty_print_arithmetic() {
        let expr = TLExpr::add(
            TLExpr::pred("x", vec![]),
            TLExpr::mul(TLExpr::pred("y", vec![]), TLExpr::constant(2.0)),
        );
        let pretty = pretty_print_expr(&expr);
        assert!(pretty.contains("+"));
        assert!(pretty.contains("*"));
        assert!(pretty.contains("2"));
    }

    #[test]
    fn test_create_detailed_error() {
        let expr = TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]);
        let diag = create_detailed_error(
            "Compilation error",
            &expr,
            "Variable x is unbound",
            Some("Add a quantifier: ∃x. <expr>"),
        );

        assert_eq!(diag.level, DiagnosticLevel::Error);
        assert!(diag.message.contains("Compilation error"));
        assert!(!diag.related.is_empty());
        assert!(diag.help.is_some());
    }

    #[test]
    fn test_pretty_print_truncation() {
        // Create a very long expression
        let mut expr = TLExpr::pred("p", vec![Term::var("x")]);
        for _ in 0..10 {
            expr = TLExpr::and(expr.clone(), TLExpr::pred("q", vec![Term::var("y")]));
        }

        let diag = create_detailed_error("Test", &expr, "context", None);
        // Should truncate if too long
        let related_msg = &diag.related[0].0;
        assert!(related_msg.len() < 200); // Reasonable length
    }
}