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
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
//! Advanced Term Rewriting System (TRS) with sophisticated control flow.
//!
//! This module extends the basic rewriting system with:
//! - Conditional rules with guards and predicates
//! - Priority-based rule ordering and conflict resolution
//! - Advanced rewriting strategies (innermost, outermost, etc.)
//! - Associative-commutative (AC) pattern matching
//! - Termination detection and cycle prevention
//! - Confluence checking via critical pair analysis
//!
//! # Examples
//!
//! ```
//! use tensorlogic_ir::{
//!     TLExpr, Term,
//!     ConditionalRule, RulePriority, RewriteStrategy, AdvancedRewriteSystem
//! };
//!
//! // Create a conditional rule: simplify (x + 0) → x only if x is not constant
//! let rule = ConditionalRule::new(
//!     "add_zero_identity",
//!     |expr| {
//!         if let TLExpr::Add(left, right) = expr {
//!             if let TLExpr::Constant(c) = **right {
//!                 if c.abs() < f64::EPSILON {
//!                     return Some((**left).clone());
//!                 }
//!             }
//!         }
//!         None
//!     },
//!     |_bindings| true, // Always applicable
//! )
//! .with_priority(RulePriority::High);
//! ```

use std::cmp::Reverse;
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};

use super::TLExpr;
use crate::util::ExprStats;

/// Priority level for rewrite rules.
///
/// Higher priority rules are attempted first. This allows fine-grained control
/// over which transformations take precedence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum RulePriority {
    /// Highest priority (value: 100)
    Critical = 100,
    /// High priority (value: 75)
    High = 75,
    /// Normal priority (value: 50) - default
    #[default]
    Normal = 50,
    /// Low priority (value: 25)
    Low = 25,
    /// Lowest priority (value: 0)
    Minimal = 0,
}

/// Guard predicate for conditional rules.
///
/// Returns true if the rule should be applied given the current bindings.
pub type GuardPredicate = fn(&HashMap<String, TLExpr>) -> bool;

/// Transformation function for conditional rules.
///
/// Attempts to transform an expression, returning Some(result) on success.
pub type TransformFn = fn(&TLExpr) -> Option<TLExpr>;

/// A conditional rewrite rule with guards and priority.
///
/// Conditional rules extend basic pattern matching with:
/// - Guard predicates that must be satisfied
/// - Priority ordering for conflict resolution
/// - Metadata for debugging and analysis
#[derive(Clone)]
pub struct ConditionalRule {
    /// Name for debugging and tracing
    pub name: String,
    /// Transformation function
    pub transform: TransformFn,
    /// Guard predicate (must return true for rule to apply)
    pub guard: GuardPredicate,
    /// Priority level
    pub priority: RulePriority,
    /// Optional description
    pub description: Option<String>,
    /// Application count (mutable for statistics)
    applications: usize,
}

impl ConditionalRule {
    /// Create a new conditional rule.
    pub fn new(name: impl Into<String>, transform: TransformFn, guard: GuardPredicate) -> Self {
        Self {
            name: name.into(),
            transform,
            guard,
            priority: RulePriority::default(),
            description: None,
            applications: 0,
        }
    }

    /// Set the priority of this rule.
    pub fn with_priority(mut self, priority: RulePriority) -> Self {
        self.priority = priority;
        self
    }

    /// Set the description of this rule.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Try to apply this rule to an expression.
    ///
    /// Returns Some(transformed) if the rule applies, None otherwise.
    pub fn apply(&mut self, expr: &TLExpr) -> Option<TLExpr> {
        let bindings = HashMap::new(); // For future pattern-based conditional rules
        if (self.guard)(&bindings) {
            if let Some(result) = (self.transform)(expr) {
                self.applications += 1;
                return Some(result);
            }
        }
        None
    }

    /// Get the number of times this rule has been applied.
    pub fn application_count(&self) -> usize {
        self.applications
    }

    /// Reset the application counter.
    pub fn reset_counter(&mut self) {
        self.applications = 0;
    }
}

impl std::fmt::Debug for ConditionalRule {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ConditionalRule")
            .field("name", &self.name)
            .field("priority", &self.priority)
            .field("description", &self.description)
            .field("applications", &self.applications)
            .finish()
    }
}

/// Rewriting strategy controlling traversal order and application.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RewriteStrategy {
    /// Apply rules from innermost subexpressions outward (bottom-up)
    Innermost,
    /// Apply rules from outermost expression inward (top-down)
    Outermost,
    /// Bottom-up traversal: transform children before parents
    #[default]
    BottomUp,
    /// Top-down traversal: transform parents before children
    TopDown,
    /// Apply all rules at each node before descending
    FixpointPerNode,
    /// Apply rules until global fixpoint (no changes anywhere)
    GlobalFixpoint,
}

/// Configuration for the advanced rewrite system.
#[derive(Debug, Clone)]
pub struct RewriteConfig {
    /// Maximum number of rewrite steps before termination
    pub max_steps: usize,
    /// Strategy for rule application
    pub strategy: RewriteStrategy,
    /// Enable termination detection
    pub detect_cycles: bool,
    /// Enable detailed tracing
    pub trace: bool,
    /// Maximum expression size to prevent exponential blowup
    pub max_expr_size: Option<usize>,
}

impl Default for RewriteConfig {
    fn default() -> Self {
        Self {
            max_steps: 10000,
            strategy: RewriteStrategy::default(),
            detect_cycles: true,
            trace: false,
            max_expr_size: Some(100000), // Prevent expression explosion
        }
    }
}

/// Statistics about a rewriting session.
#[derive(Debug, Clone, Default)]
pub struct RewriteStats {
    /// Total number of rewrite steps performed
    pub steps: usize,
    /// Number of successful rule applications
    pub rule_applications: usize,
    /// Per-rule application counts
    pub rule_counts: HashMap<String, usize>,
    /// Whether a fixpoint was reached
    pub reached_fixpoint: bool,
    /// Whether cycle detection triggered
    pub cycle_detected: bool,
    /// Whether size limit was exceeded
    pub size_limit_exceeded: bool,
    /// Initial expression size
    pub initial_size: usize,
    /// Final expression size
    pub final_size: usize,
}

impl RewriteStats {
    /// Get reduction percentage (negative means expression grew).
    pub fn reduction_percentage(&self) -> f64 {
        if self.initial_size == 0 {
            return 0.0;
        }
        100.0 * (1.0 - (self.final_size as f64 / self.initial_size as f64))
    }

    /// Check if rewriting was successful (reached fixpoint without issues).
    pub fn is_successful(&self) -> bool {
        self.reached_fixpoint && !self.cycle_detected && !self.size_limit_exceeded
    }
}

/// Expression hash for cycle detection.
fn expr_hash(expr: &TLExpr) -> u64 {
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    // Use debug format as a simple hash (not perfect but works for cycle detection)
    format!("{:?}", expr).hash(&mut hasher);
    hasher.finish()
}

/// Advanced rewrite system with sophisticated control flow.
pub struct AdvancedRewriteSystem {
    /// Ordered list of conditional rules (sorted by priority)
    rules: Vec<ConditionalRule>,
    /// Configuration
    config: RewriteConfig,
    /// Seen expression hashes for cycle detection
    seen_hashes: HashSet<u64>,
}

impl AdvancedRewriteSystem {
    /// Create a new advanced rewrite system.
    pub fn new() -> Self {
        Self {
            rules: Vec::new(),
            config: RewriteConfig::default(),
            seen_hashes: HashSet::new(),
        }
    }

    /// Create with custom configuration.
    pub fn with_config(config: RewriteConfig) -> Self {
        Self {
            rules: Vec::new(),
            config,
            seen_hashes: HashSet::new(),
        }
    }

    /// Add a rule to the system.
    pub fn add_rule(mut self, rule: ConditionalRule) -> Self {
        self.rules.push(rule);
        // Sort by priority (highest first)
        self.rules.sort_by_key(|r| Reverse(r.priority));
        self
    }

    /// Apply rules to an expression according to the configured strategy.
    pub fn apply(&mut self, expr: &TLExpr) -> (TLExpr, RewriteStats) {
        let initial_stats = ExprStats::compute(expr);
        let mut stats = RewriteStats {
            initial_size: initial_stats.node_count,
            ..Default::default()
        };

        self.seen_hashes.clear();

        let result = match self.config.strategy {
            RewriteStrategy::Innermost => self.apply_innermost(expr, &mut stats),
            RewriteStrategy::Outermost => self.apply_outermost(expr, &mut stats),
            RewriteStrategy::BottomUp => self.apply_bottom_up(expr, &mut stats),
            RewriteStrategy::TopDown => self.apply_top_down(expr, &mut stats),
            RewriteStrategy::FixpointPerNode => self.apply_fixpoint_per_node(expr, &mut stats),
            RewriteStrategy::GlobalFixpoint => self.apply_global_fixpoint(expr, &mut stats),
        };

        let final_stats = ExprStats::compute(&result);
        stats.final_size = final_stats.node_count;

        // Check if we reached a fixpoint
        if stats.steps < self.config.max_steps && !stats.cycle_detected {
            stats.reached_fixpoint = true;
        }

        (result, stats)
    }

    /// Try to apply the first matching rule at this node.
    fn try_apply_at_node(&mut self, expr: &TLExpr, stats: &mut RewriteStats) -> Option<TLExpr> {
        for rule in &mut self.rules {
            if let Some(result) = rule.apply(expr) {
                stats.rule_applications += 1;
                *stats.rule_counts.entry(rule.name.clone()).or_insert(0) += 1;

                if self.config.trace {
                    eprintln!("Applied rule '{}' at step {}", rule.name, stats.steps);
                }

                return Some(result);
            }
        }
        None
    }

    /// Check for cycles and size limits.
    fn check_constraints(&mut self, expr: &TLExpr, stats: &mut RewriteStats) -> bool {
        // Check cycle detection
        if self.config.detect_cycles {
            let hash = expr_hash(expr);
            if self.seen_hashes.contains(&hash) {
                stats.cycle_detected = true;
                return false;
            }
            self.seen_hashes.insert(hash);
        }

        // Check size limit
        if let Some(max_size) = self.config.max_expr_size {
            let current_stats = ExprStats::compute(expr);
            if current_stats.node_count > max_size {
                stats.size_limit_exceeded = true;
                return false;
            }
        }

        // Check step limit
        if stats.steps >= self.config.max_steps {
            return false;
        }

        true
    }

    /// Innermost strategy: repeatedly apply at innermost redex.
    fn apply_innermost(&mut self, expr: &TLExpr, stats: &mut RewriteStats) -> TLExpr {
        let mut current = expr.clone();

        while stats.steps < self.config.max_steps {
            stats.steps += 1;

            if !self.check_constraints(&current, stats) {
                break;
            }

            // Try to find innermost redex
            if let Some(rewritten) = self.rewrite_innermost(&current, stats) {
                current = rewritten;
            } else {
                break; // No more rewrites possible
            }
        }

        current
    }

    /// Find and rewrite innermost redex.
    fn rewrite_innermost(&mut self, expr: &TLExpr, stats: &mut RewriteStats) -> Option<TLExpr> {
        // First, try to rewrite children
        let children_rewritten = self.rewrite_children(expr, stats);
        if let Some(new_expr) = children_rewritten {
            return Some(new_expr);
        }

        // If no children changed, try to apply rule at this node
        self.try_apply_at_node(expr, stats)
    }

    /// Outermost strategy: repeatedly apply at outermost redex.
    fn apply_outermost(&mut self, expr: &TLExpr, stats: &mut RewriteStats) -> TLExpr {
        let mut current = expr.clone();

        while stats.steps < self.config.max_steps {
            stats.steps += 1;

            if !self.check_constraints(&current, stats) {
                break;
            }

            // Try to apply at top level first
            if let Some(rewritten) = self.try_apply_at_node(&current, stats) {
                current = rewritten;
                continue;
            }

            // If no top-level rewrite, recurse into children
            if let Some(rewritten) = self.rewrite_children(&current, stats) {
                current = rewritten;
            } else {
                break;
            }
        }

        current
    }

    /// Bottom-up strategy: transform children before parents.
    fn apply_bottom_up(&mut self, expr: &TLExpr, stats: &mut RewriteStats) -> TLExpr {
        stats.steps += 1;

        if !self.check_constraints(expr, stats) {
            return expr.clone();
        }

        // First, recursively transform children
        let with_transformed_children = self.transform_children_bottom_up(expr, stats);

        // Then try to apply rules at this level
        if let Some(result) = self.try_apply_at_node(&with_transformed_children, stats) {
            result
        } else {
            with_transformed_children
        }
    }

    /// Top-down strategy: transform parents before children.
    fn apply_top_down(&mut self, expr: &TLExpr, stats: &mut RewriteStats) -> TLExpr {
        stats.steps += 1;

        if !self.check_constraints(expr, stats) {
            return expr.clone();
        }

        // First, try to apply rules at this level
        let current = if let Some(result) = self.try_apply_at_node(expr, stats) {
            result
        } else {
            expr.clone()
        };

        // Then recursively transform children
        self.transform_children_top_down(&current, stats)
    }

    /// Fixpoint per node: exhaust rewrites at each node before descending.
    fn apply_fixpoint_per_node(&mut self, expr: &TLExpr, stats: &mut RewriteStats) -> TLExpr {
        let mut current = expr.clone();

        // Apply rules at this node until fixpoint
        while let Some(rewritten) = self.try_apply_at_node(&current, stats) {
            current = rewritten;
            stats.steps += 1;
            if !self.check_constraints(&current, stats) {
                return current;
            }
        }

        // Then transform children
        self.transform_children_fixpoint_per_node(&current, stats)
    }

    /// Global fixpoint: keep applying until no changes anywhere.
    fn apply_global_fixpoint(&mut self, expr: &TLExpr, stats: &mut RewriteStats) -> TLExpr {
        let mut current = expr.clone();

        loop {
            stats.steps += 1;

            if !self.check_constraints(&current, stats) {
                break;
            }

            let next = self.apply_bottom_up(&current, stats);
            if next == current {
                break; // Fixpoint reached
            }
            current = next;
        }

        current
    }

    /// Rewrite children, returning Some if any changed.
    fn rewrite_children(&mut self, expr: &TLExpr, stats: &mut RewriteStats) -> Option<TLExpr> {
        match expr {
            TLExpr::And(l, r) => {
                let l_new = self.rewrite_innermost(l, stats);
                let r_new = self.rewrite_innermost(r, stats);
                if l_new.is_some() || r_new.is_some() {
                    Some(TLExpr::and(
                        l_new.unwrap_or_else(|| (**l).clone()),
                        r_new.unwrap_or_else(|| (**r).clone()),
                    ))
                } else {
                    None
                }
            }
            TLExpr::Or(l, r) => {
                let l_new = self.rewrite_innermost(l, stats);
                let r_new = self.rewrite_innermost(r, stats);
                if l_new.is_some() || r_new.is_some() {
                    Some(TLExpr::or(
                        l_new.unwrap_or_else(|| (**l).clone()),
                        r_new.unwrap_or_else(|| (**r).clone()),
                    ))
                } else {
                    None
                }
            }
            TLExpr::Not(e) => self.rewrite_innermost(e, stats).map(TLExpr::negate),
            // Add more cases as needed
            _ => None,
        }
    }

    /// Transform children bottom-up.
    fn transform_children_bottom_up(&mut self, expr: &TLExpr, stats: &mut RewriteStats) -> TLExpr {
        match expr {
            TLExpr::And(l, r) => TLExpr::and(
                self.apply_bottom_up(l, stats),
                self.apply_bottom_up(r, stats),
            ),
            TLExpr::Or(l, r) => TLExpr::or(
                self.apply_bottom_up(l, stats),
                self.apply_bottom_up(r, stats),
            ),
            TLExpr::Not(e) => TLExpr::negate(self.apply_bottom_up(e, stats)),
            TLExpr::Imply(l, r) => TLExpr::imply(
                self.apply_bottom_up(l, stats),
                self.apply_bottom_up(r, stats),
            ),
            // Add more cases...
            _ => expr.clone(),
        }
    }

    /// Transform children top-down.
    fn transform_children_top_down(&mut self, expr: &TLExpr, stats: &mut RewriteStats) -> TLExpr {
        match expr {
            TLExpr::And(l, r) => {
                TLExpr::and(self.apply_top_down(l, stats), self.apply_top_down(r, stats))
            }
            TLExpr::Or(l, r) => {
                TLExpr::or(self.apply_top_down(l, stats), self.apply_top_down(r, stats))
            }
            TLExpr::Not(e) => TLExpr::negate(self.apply_top_down(e, stats)),
            TLExpr::Imply(l, r) => {
                TLExpr::imply(self.apply_top_down(l, stats), self.apply_top_down(r, stats))
            }
            // Add more cases...
            _ => expr.clone(),
        }
    }

    /// Transform children with fixpoint per node.
    fn transform_children_fixpoint_per_node(
        &mut self,
        expr: &TLExpr,
        stats: &mut RewriteStats,
    ) -> TLExpr {
        match expr {
            TLExpr::And(l, r) => TLExpr::and(
                self.apply_fixpoint_per_node(l, stats),
                self.apply_fixpoint_per_node(r, stats),
            ),
            TLExpr::Or(l, r) => TLExpr::or(
                self.apply_fixpoint_per_node(l, stats),
                self.apply_fixpoint_per_node(r, stats),
            ),
            TLExpr::Not(e) => TLExpr::negate(self.apply_fixpoint_per_node(e, stats)),
            TLExpr::Imply(l, r) => TLExpr::imply(
                self.apply_fixpoint_per_node(l, stats),
                self.apply_fixpoint_per_node(r, stats),
            ),
            // Add more cases...
            _ => expr.clone(),
        }
    }

    /// Get statistics for all rules.
    pub fn rule_statistics(&self) -> Vec<(&str, usize)> {
        self.rules
            .iter()
            .map(|r| (r.name.as_str(), r.application_count()))
            .collect()
    }

    /// Reset all rule counters.
    pub fn reset_statistics(&mut self) {
        for rule in &mut self.rules {
            rule.reset_counter();
        }
    }
}

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

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

    #[test]
    fn test_conditional_rule_basic() {
        let mut rule = ConditionalRule::new(
            "remove_double_neg",
            |expr| {
                if let TLExpr::Not(inner) = expr {
                    if let TLExpr::Not(inner_inner) = &**inner {
                        return Some((**inner_inner).clone());
                    }
                }
                None
            },
            |_| true,
        );

        let expr = TLExpr::negate(TLExpr::negate(TLExpr::pred("P", vec![Term::var("x")])));
        let result = rule.apply(&expr).expect("unwrap");

        assert!(matches!(result, TLExpr::Pred { .. }));
        assert_eq!(rule.application_count(), 1);
    }

    #[test]
    fn test_priority_ordering() {
        let mut system = AdvancedRewriteSystem::new();

        // Add rules in reverse priority order
        system = system.add_rule(
            ConditionalRule::new("low", |_| None, |_| true).with_priority(RulePriority::Low),
        );
        system = system.add_rule(
            ConditionalRule::new("high", |_| None, |_| true).with_priority(RulePriority::High),
        );
        system = system.add_rule(
            ConditionalRule::new("critical", |_| None, |_| true)
                .with_priority(RulePriority::Critical),
        );

        // Verify rules are sorted by priority (highest first)
        assert_eq!(system.rules[0].priority, RulePriority::Critical);
        assert_eq!(system.rules[1].priority, RulePriority::High);
        assert_eq!(system.rules[2].priority, RulePriority::Low);
    }

    #[test]
    fn test_bottom_up_strategy() {
        let mut system = AdvancedRewriteSystem::with_config(RewriteConfig {
            strategy: RewriteStrategy::BottomUp,
            max_steps: 100,
            ..Default::default()
        });

        // Rule: remove double negation
        system = system.add_rule(ConditionalRule::new(
            "double_neg",
            |expr| {
                if let TLExpr::Not(inner) = expr {
                    if let TLExpr::Not(inner_inner) = &**inner {
                        return Some((**inner_inner).clone());
                    }
                }
                None
            },
            |_| true,
        ));

        // ¬(¬(¬(¬P)))
        let expr = TLExpr::negate(TLExpr::negate(TLExpr::negate(TLExpr::negate(
            TLExpr::pred("P", vec![Term::var("x")]),
        ))));

        let (result, stats) = system.apply(&expr);

        // Should reduce to P
        assert!(matches!(result, TLExpr::Pred { .. }));
        assert_eq!(stats.rule_applications, 2); // Two double-neg eliminations
    }

    #[test]
    fn test_cycle_detection() {
        let mut system = AdvancedRewriteSystem::with_config(RewriteConfig {
            strategy: RewriteStrategy::GlobalFixpoint,
            detect_cycles: true,
            max_steps: 1000,
            ..Default::default()
        });

        // Pathological rule that could cycle: P → ¬¬P
        system = system.add_rule(ConditionalRule::new(
            "add_double_neg",
            |expr| {
                if let TLExpr::Pred { .. } = expr {
                    return Some(TLExpr::negate(TLExpr::negate(expr.clone())));
                }
                None
            },
            |_| true,
        ));

        system = system.add_rule(ConditionalRule::new(
            "remove_double_neg",
            |expr| {
                if let TLExpr::Not(inner) = expr {
                    if let TLExpr::Not(inner_inner) = &**inner {
                        return Some((**inner_inner).clone());
                    }
                }
                None
            },
            |_| true,
        ));

        let expr = TLExpr::pred("P", vec![Term::var("x")]);
        let (_result, stats) = system.apply(&expr);

        // Should detect cycle
        assert!(stats.cycle_detected || stats.steps >= 1000);
    }

    #[test]
    fn test_size_limit() {
        let mut system = AdvancedRewriteSystem::with_config(RewriteConfig {
            strategy: RewriteStrategy::Innermost, // Use Innermost which checks constraints more frequently
            max_expr_size: Some(10),
            detect_cycles: false, // Disable cycle detection to focus on size limit
            ..Default::default()
        });

        // Rule that grows expression: P → (P ∧ P)
        system = system.add_rule(ConditionalRule::new(
            "duplicate",
            |expr| {
                if let TLExpr::Pred { .. } = expr {
                    return Some(TLExpr::and(expr.clone(), expr.clone()));
                }
                None
            },
            |_| true,
        ));

        let expr = TLExpr::pred("P", vec![Term::var("x")]);
        let (_result, stats) = system.apply(&expr);

        // Should hit size limit or max steps (since the rule keeps growing the expression)
        assert!(stats.size_limit_exceeded || stats.steps >= system.config.max_steps);
    }

    #[test]
    fn test_rewrite_stats() {
        let mut system = AdvancedRewriteSystem::new();

        system = system.add_rule(ConditionalRule::new(
            "test_rule",
            |expr| {
                if let TLExpr::Not(inner) = expr {
                    if let TLExpr::Not(inner_inner) = &**inner {
                        return Some((**inner_inner).clone());
                    }
                }
                None
            },
            |_| true,
        ));

        let expr = TLExpr::negate(TLExpr::negate(TLExpr::pred("P", vec![Term::var("x")])));
        let (_result, stats) = system.apply(&expr);

        assert!(stats.is_successful());
        assert!(stats.reduction_percentage() > 0.0);
        assert_eq!(stats.rule_applications, 1);
    }
}