oxiz-solver 0.2.0

Main CDCL(T) Solver API for OxiZ
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
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
//! Interface Equality Management for Theory Combination.
//!
//! This module manages interface equalities in Nelson-Oppen combination:
//! - Minimal interface equality generation
//! - Equality generation strategies
//! - Interface equality scheduling
//! - Equality minimization algorithms
//!
//! ## Interface Equalities
//!
//! Interface equalities are equalities between shared terms that must be
//! communicated between theories. The goal is to generate a **minimal**
//! set of equalities that allows theories to infer all relevant equalities.
//!
//! ## Generation Strategies
//!
//! - **Eager**: Generate all possible interface equalities upfront
//! - **Lazy**: Generate equalities on-demand
//! - **Minimal**: Generate only necessary equalities (star topology)
//! - **Incremental**: Add equalities incrementally as needed
//!
//! ## Star Topology
//!
//! For a set of equivalent terms {t1, t2, ..., tn}, we can use a "star"
//! topology: pick one term as the representative and only share equalities
//! t1 = rep, t2 = rep, ..., tn = rep. This requires O(n) equalities instead
//! of O(n²).
//!
//! ## References
//!
//! - Nelson & Oppen (1979): "Simplification by Cooperating Decision Procedures"
//! - Shostak (1984): "Deciding Combinations of Theories"
//! - Z3's `smt/theory_combine.cpp`

#[allow(unused_imports)]
use crate::prelude::*;
use core::cmp::Ordering;

/// Term identifier.
pub type TermId = u32;

/// Theory identifier.
pub type TheoryId = u32;

/// Decision level.
pub type DecisionLevel = u32;

/// Equality between two terms.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Equality {
    /// Left-hand side.
    pub lhs: TermId,
    /// Right-hand side.
    pub rhs: TermId,
}

impl Equality {
    /// Create new equality (normalized).
    pub fn new(lhs: TermId, rhs: TermId) -> Self {
        if lhs <= rhs {
            Self { lhs, rhs }
        } else {
            Self { lhs: rhs, rhs: lhs }
        }
    }
}

/// Equality generation strategy.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GenerationStrategy {
    /// Generate all equalities eagerly.
    Eager,
    /// Generate equalities lazily on-demand.
    Lazy,
    /// Generate minimal set (star topology).
    Minimal,
    /// Incremental generation.
    Incremental,
    /// Adaptive strategy based on heuristics.
    Adaptive,
}

/// Priority for interface equality.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EqualityPriority {
    /// Priority level (higher = more important).
    pub level: u32,
    /// Relevancy score.
    pub relevancy: u32,
    /// Decision level.
    pub decision_level: DecisionLevel,
}

impl Ord for EqualityPriority {
    fn cmp(&self, other: &Self) -> Ordering {
        self.level
            .cmp(&other.level)
            .then_with(|| self.relevancy.cmp(&other.relevancy))
            .then_with(|| other.decision_level.cmp(&self.decision_level))
    }
}

impl PartialOrd for EqualityPriority {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// Interface equality with metadata.
#[derive(Debug, Clone)]
pub struct InterfaceEquality {
    /// The equality.
    pub equality: Equality,
    /// Theories involved.
    pub theories: FxHashSet<TheoryId>,
    /// Priority.
    pub priority: EqualityPriority,
    /// Is this equality necessary?
    pub is_necessary: bool,
    /// Generation timestamp.
    pub timestamp: u64,
}

impl PartialEq for InterfaceEquality {
    fn eq(&self, other: &Self) -> bool {
        self.equality == other.equality
    }
}

impl Eq for InterfaceEquality {}

impl Ord for InterfaceEquality {
    fn cmp(&self, other: &Self) -> Ordering {
        self.priority.cmp(&other.priority)
    }
}

impl PartialOrd for InterfaceEquality {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// Equivalence class for interface terms.
#[derive(Debug, Clone)]
pub struct InterfaceEClass {
    /// Representative term.
    pub representative: TermId,
    /// All terms in the class.
    pub members: FxHashSet<TermId>,
    /// Theories using terms in this class.
    pub theories: FxHashSet<TheoryId>,
    /// Generation strategy for this class.
    pub strategy: GenerationStrategy,
}

impl InterfaceEClass {
    /// Create new equivalence class.
    fn new(representative: TermId, theory: TheoryId) -> Self {
        let mut members = FxHashSet::default();
        members.insert(representative);

        let mut theories = FxHashSet::default();
        theories.insert(theory);

        Self {
            representative,
            members,
            theories,
            strategy: GenerationStrategy::Minimal,
        }
    }

    /// Add term to class.
    fn add_term(&mut self, term: TermId, theory: TheoryId) {
        self.members.insert(term);
        self.theories.insert(theory);
    }

    /// Merge another class into this one.
    fn merge(&mut self, other: &InterfaceEClass) {
        for &term in &other.members {
            self.members.insert(term);
        }
        for &theory in &other.theories {
            self.theories.insert(theory);
        }
    }

    /// Check if class is shared between multiple theories.
    fn is_shared(&self) -> bool {
        self.theories.len() > 1
    }

    /// Generate equalities for this class using current strategy.
    fn generate_equalities(
        &self,
        timestamp: u64,
        decision_level: DecisionLevel,
    ) -> Vec<InterfaceEquality> {
        match self.strategy {
            GenerationStrategy::Eager => self.generate_eager(timestamp, decision_level),
            GenerationStrategy::Lazy => Vec::new(), // Generated on-demand
            GenerationStrategy::Minimal => self.generate_minimal(timestamp, decision_level),
            GenerationStrategy::Incremental => self.generate_incremental(timestamp, decision_level),
            GenerationStrategy::Adaptive => self.generate_adaptive(timestamp, decision_level),
        }
    }

    /// Generate all pairwise equalities (eager).
    fn generate_eager(
        &self,
        timestamp: u64,
        decision_level: DecisionLevel,
    ) -> Vec<InterfaceEquality> {
        let mut equalities = Vec::new();
        let members: Vec<_> = self.members.iter().copied().collect();

        for i in 0..members.len() {
            for j in (i + 1)..members.len() {
                equalities.push(InterfaceEquality {
                    equality: Equality::new(members[i], members[j]),
                    theories: self.theories.clone(),
                    priority: EqualityPriority {
                        level: 100,
                        relevancy: 50,
                        decision_level,
                    },
                    is_necessary: false,
                    timestamp,
                });
            }
        }

        equalities
    }

    /// Generate minimal equalities (star topology).
    fn generate_minimal(
        &self,
        timestamp: u64,
        decision_level: DecisionLevel,
    ) -> Vec<InterfaceEquality> {
        let mut equalities = Vec::new();
        let rep = self.representative;

        for &term in &self.members {
            if term != rep {
                equalities.push(InterfaceEquality {
                    equality: Equality::new(term, rep),
                    theories: self.theories.clone(),
                    priority: EqualityPriority {
                        level: 100,
                        relevancy: 50,
                        decision_level,
                    },
                    is_necessary: true,
                    timestamp,
                });
            }
        }

        equalities
    }

    /// Generate incremental equalities.
    fn generate_incremental(
        &self,
        timestamp: u64,
        decision_level: DecisionLevel,
    ) -> Vec<InterfaceEquality> {
        // Similar to minimal, but could be enhanced with incremental logic
        self.generate_minimal(timestamp, decision_level)
    }

    /// Generate adaptive equalities based on heuristics.
    fn generate_adaptive(
        &self,
        timestamp: u64,
        decision_level: DecisionLevel,
    ) -> Vec<InterfaceEquality> {
        // Use minimal for small classes, eager for very small classes
        if self.members.len() <= 2 {
            self.generate_eager(timestamp, decision_level)
        } else {
            self.generate_minimal(timestamp, decision_level)
        }
    }
}

/// Configuration for interface equality management.
#[derive(Debug, Clone)]
pub struct InterfaceEqualityConfig {
    /// Default generation strategy.
    pub default_strategy: GenerationStrategy,

    /// Enable equality minimization.
    pub enable_minimization: bool,

    /// Enable priority-based scheduling.
    pub enable_priority: bool,

    /// Maximum equalities per batch.
    pub max_batch_size: usize,

    /// Enable relevancy tracking.
    pub track_relevancy: bool,

    /// Adaptive strategy threshold.
    pub adaptive_threshold: usize,
}

impl Default for InterfaceEqualityConfig {
    fn default() -> Self {
        Self {
            default_strategy: GenerationStrategy::Minimal,
            enable_minimization: true,
            enable_priority: true,
            max_batch_size: 1000,
            track_relevancy: true,
            adaptive_threshold: 10,
        }
    }
}

/// Statistics for interface equality management.
#[derive(Debug, Clone, Default)]
pub struct InterfaceEqualityStats {
    /// Equalities generated.
    pub equalities_generated: u64,
    /// Equalities minimized away.
    pub equalities_minimized: u64,
    /// Eager generations.
    pub eager_generations: u64,
    /// Lazy generations.
    pub lazy_generations: u64,
    /// Minimal generations.
    pub minimal_generations: u64,
    /// Equivalence classes.
    pub eclasses: u64,
    /// Batches sent.
    pub batches_sent: u64,
}

/// Interface equality manager.
pub struct InterfaceEqualityManager {
    /// Configuration.
    config: InterfaceEqualityConfig,

    /// Statistics.
    stats: InterfaceEqualityStats,

    /// Term to equivalence class mapping.
    term_to_eclass: FxHashMap<TermId, usize>,

    /// Equivalence classes.
    eclasses: Vec<InterfaceEClass>,

    /// Pending equalities (priority queue).
    pending: BinaryHeap<InterfaceEquality>,

    /// Generated equalities (deduplication).
    generated: FxHashSet<Equality>,

    /// Current timestamp.
    timestamp: u64,

    /// Current decision level.
    decision_level: DecisionLevel,

    /// Relevancy scores for terms.
    relevancy: FxHashMap<TermId, u32>,

    /// Equality generation history for backtracking.
    history: FxHashMap<DecisionLevel, Vec<Equality>>,
}

impl InterfaceEqualityManager {
    /// Create new manager.
    pub fn new() -> Self {
        Self::with_config(InterfaceEqualityConfig::default())
    }

    /// Create with configuration.
    pub fn with_config(config: InterfaceEqualityConfig) -> Self {
        Self {
            config,
            stats: InterfaceEqualityStats::default(),
            term_to_eclass: FxHashMap::default(),
            eclasses: Vec::new(),
            pending: BinaryHeap::new(),
            generated: FxHashSet::default(),
            timestamp: 0,
            decision_level: 0,
            relevancy: FxHashMap::default(),
            history: FxHashMap::default(),
        }
    }

    /// Get statistics.
    pub fn stats(&self) -> &InterfaceEqualityStats {
        &self.stats
    }

    /// Register a term with theory.
    pub fn register_term(&mut self, term: TermId, theory: TheoryId) {
        if let Some(&eclass_id) = self.term_to_eclass.get(&term) {
            self.eclasses[eclass_id].add_term(term, theory);
        } else {
            let eclass_id = self.eclasses.len();
            self.eclasses.push(InterfaceEClass::new(term, theory));
            self.term_to_eclass.insert(term, eclass_id);
            self.stats.eclasses += 1;
        }
    }

    /// Assert equality and merge equivalence classes.
    pub fn assert_equality(&mut self, lhs: TermId, rhs: TermId) -> Result<(), String> {
        let lhs_class = self.find_or_create(lhs);
        let rhs_class = self.find_or_create(rhs);

        if lhs_class == rhs_class {
            return Ok(());
        }

        // Merge smaller into larger
        let (small, large) =
            if self.eclasses[lhs_class].members.len() < self.eclasses[rhs_class].members.len() {
                (lhs_class, rhs_class)
            } else {
                (rhs_class, lhs_class)
            };

        let small_eclass = self.eclasses[small].clone();
        self.eclasses[large].merge(&small_eclass);

        // Update term mappings
        for &term in &small_eclass.members {
            self.term_to_eclass.insert(term, large);
        }

        // Generate interface equalities if the merged class is shared
        if self.eclasses[large].is_shared() {
            self.generate_equalities_for_class(large)?;
        }

        Ok(())
    }

    /// Find or create equivalence class for term.
    fn find_or_create(&mut self, term: TermId) -> usize {
        if let Some(&eclass_id) = self.term_to_eclass.get(&term) {
            eclass_id
        } else {
            let eclass_id = self.eclasses.len();
            self.eclasses.push(InterfaceEClass::new(term, 0));
            self.term_to_eclass.insert(term, eclass_id);
            self.stats.eclasses += 1;
            eclass_id
        }
    }

    /// Generate equalities for an equivalence class.
    fn generate_equalities_for_class(&mut self, eclass_id: usize) -> Result<(), String> {
        if eclass_id >= self.eclasses.len() {
            return Err("Invalid eclass ID".to_string());
        }

        let eclass = &self.eclasses[eclass_id];
        let equalities = eclass.generate_equalities(self.timestamp, self.decision_level);

        for eq in equalities {
            if !self.generated.contains(&eq.equality) {
                self.generated.insert(eq.equality);
                self.pending.push(eq);
                self.stats.equalities_generated += 1;

                // Update strategy-specific stats
                match eclass.strategy {
                    GenerationStrategy::Eager => self.stats.eager_generations += 1,
                    GenerationStrategy::Lazy => self.stats.lazy_generations += 1,
                    GenerationStrategy::Minimal => self.stats.minimal_generations += 1,
                    _ => {}
                }
            }
        }

        self.timestamp += 1;
        Ok(())
    }

    /// Get pending equalities (batch).
    pub fn get_pending_batch(&mut self) -> Vec<InterfaceEquality> {
        let mut batch = Vec::new();

        while batch.len() < self.config.max_batch_size {
            if let Some(eq) = self.pending.pop() {
                batch.push(eq);
            } else {
                break;
            }
        }

        if !batch.is_empty() {
            self.stats.batches_sent += 1;
        }

        batch
    }

    /// Get all pending equalities.
    pub fn get_all_pending(&mut self) -> Vec<InterfaceEquality> {
        let mut all = Vec::new();

        while let Some(eq) = self.pending.pop() {
            all.push(eq);
        }

        if !all.is_empty() {
            self.stats.batches_sent += 1;
        }

        all
    }

    /// Set generation strategy for a term's equivalence class.
    pub fn set_strategy(
        &mut self,
        term: TermId,
        strategy: GenerationStrategy,
    ) -> Result<(), String> {
        let eclass_id = self
            .term_to_eclass
            .get(&term)
            .ok_or("Term not registered")?;
        self.eclasses[*eclass_id].strategy = strategy;
        Ok(())
    }

    /// Minimize pending equalities.
    ///
    /// Remove redundant equalities that can be inferred from others.
    pub fn minimize_equalities(&mut self) {
        if !self.config.enable_minimization {
            return;
        }

        // Convert pending to vector for processing
        let all_pending: Vec<_> = self.pending.drain().collect();
        let mut necessary = Vec::new();

        // Group by equivalence class
        let mut by_class: FxHashMap<usize, Vec<InterfaceEquality>> = FxHashMap::default();

        for eq in all_pending {
            if let Some(&eclass_id) = self.term_to_eclass.get(&eq.equality.lhs) {
                by_class.entry(eclass_id).or_default().push(eq);
            }
        }

        // For each class, keep only necessary equalities (star topology)
        for (_eclass_id, mut equalities) in by_class {
            if equalities.len() <= 2 {
                necessary.extend(equalities);
                continue;
            }

            // Find representative
            let rep = equalities[0].equality.lhs;

            // Keep only equalities involving the representative
            equalities.retain(|eq| eq.equality.lhs == rep || eq.equality.rhs == rep);

            let before = equalities.len();
            let minimized = equalities.len();
            self.stats.equalities_minimized += (before - minimized) as u64;

            necessary.extend(equalities);
        }

        // Re-add necessary equalities
        for eq in necessary {
            self.pending.push(eq);
        }
    }

    /// Update relevancy score for a term.
    pub fn update_relevancy(&mut self, term: TermId, score: u32) {
        if !self.config.track_relevancy {
            return;
        }

        self.relevancy.insert(term, score);

        // Update priority of pending equalities involving this term
        let all_pending: Vec<_> = self.pending.drain().collect();

        for mut eq in all_pending {
            if eq.equality.lhs == term || eq.equality.rhs == term {
                eq.priority.relevancy = score;
            }
            self.pending.push(eq);
        }
    }

    /// Push new decision level.
    pub fn push_decision_level(&mut self) {
        self.decision_level += 1;
    }

    /// Backtrack to decision level.
    pub fn backtrack(&mut self, level: DecisionLevel) -> Result<(), String> {
        if level > self.decision_level {
            return Err("Cannot backtrack to future level".to_string());
        }

        // Remove equalities from higher levels
        let all_pending: Vec<_> = self.pending.drain().collect();

        for eq in all_pending {
            if eq.priority.decision_level <= level {
                self.pending.push(eq);
            } else {
                self.generated.remove(&eq.equality);
            }
        }

        // Remove history above this level
        let levels_to_remove: Vec<_> = self
            .history
            .keys()
            .filter(|&&l| l > level)
            .copied()
            .collect();

        for l in levels_to_remove {
            self.history.remove(&l);
        }

        self.decision_level = level;
        Ok(())
    }

    /// Clear all state.
    pub fn clear(&mut self) {
        self.term_to_eclass.clear();
        self.eclasses.clear();
        self.pending.clear();
        self.generated.clear();
        self.timestamp = 0;
        self.decision_level = 0;
        self.relevancy.clear();
        self.history.clear();
    }

    /// Reset statistics.
    pub fn reset_stats(&mut self) {
        self.stats = InterfaceEqualityStats::default();
    }

    /// Get number of pending equalities.
    pub fn pending_count(&self) -> usize {
        self.pending.len()
    }

    /// Check if equality has been generated.
    pub fn is_generated(&self, eq: &Equality) -> bool {
        self.generated.contains(eq)
    }

    /// Force generation of all equalities for shared classes.
    pub fn force_generate_all(&mut self) -> Result<(), String> {
        for eclass_id in 0..self.eclasses.len() {
            if self.eclasses[eclass_id].is_shared() {
                self.generate_equalities_for_class(eclass_id)?;
            }
        }
        Ok(())
    }

    /// Get equivalence class for a term.
    pub fn get_eclass(&self, term: TermId) -> Option<&InterfaceEClass> {
        self.term_to_eclass
            .get(&term)
            .and_then(|&id| self.eclasses.get(id))
    }

    /// Get representative for a term.
    pub fn get_representative(&self, term: TermId) -> Option<TermId> {
        self.get_eclass(term).map(|ec| ec.representative)
    }

    /// Check if two terms are in the same equivalence class.
    pub fn are_equal(&self, lhs: TermId, rhs: TermId) -> bool {
        if let (Some(&lhs_class), Some(&rhs_class)) =
            (self.term_to_eclass.get(&lhs), self.term_to_eclass.get(&rhs))
        {
            lhs_class == rhs_class
        } else {
            false
        }
    }
}

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

/// Equality scheduler for coordinating propagation.
pub struct EqualityScheduler {
    /// Scheduled equalities by priority.
    scheduled: BinaryHeap<InterfaceEquality>,
    /// Scheduling policy.
    policy: SchedulingPolicy,
}

/// Scheduling policy for equalities.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SchedulingPolicy {
    /// FIFO (first-in-first-out).
    Fifo,
    /// Priority-based.
    Priority,
    /// Relevancy-based.
    Relevancy,
    /// Round-robin between theories.
    RoundRobin,
}

impl EqualityScheduler {
    /// Create new scheduler.
    pub fn new(policy: SchedulingPolicy) -> Self {
        Self {
            scheduled: BinaryHeap::new(),
            policy,
        }
    }

    /// Schedule an equality.
    pub fn schedule(&mut self, equality: InterfaceEquality) {
        self.scheduled.push(equality);
    }

    /// Get next equality to propagate.
    #[allow(clippy::should_implement_trait)]
    pub fn next(&mut self) -> Option<InterfaceEquality> {
        match self.policy {
            SchedulingPolicy::Fifo => {
                // Convert to FIFO by ignoring priority
                let all: Vec<_> = self.scheduled.drain().collect();

                all.into_iter().next()
            }
            SchedulingPolicy::Priority | SchedulingPolicy::Relevancy => self.scheduled.pop(),
            SchedulingPolicy::RoundRobin => {
                // Simplified round-robin
                self.scheduled.pop()
            }
        }
    }

    /// Get batch of equalities.
    pub fn next_batch(&mut self, size: usize) -> Vec<InterfaceEquality> {
        let mut batch = Vec::new();

        for _ in 0..size {
            if let Some(eq) = self.next() {
                batch.push(eq);
            } else {
                break;
            }
        }

        batch
    }

    /// Clear scheduler.
    pub fn clear(&mut self) {
        self.scheduled.clear();
    }
}

/// Minimizer for interface equalities.
pub struct EqualityMinimizer {
    /// Union-find for transitivity.
    parent: FxHashMap<TermId, TermId>,
    /// Rank for union-by-rank.
    rank: FxHashMap<TermId, usize>,
}

impl EqualityMinimizer {
    /// Create new minimizer.
    pub fn new() -> Self {
        Self {
            parent: FxHashMap::default(),
            rank: FxHashMap::default(),
        }
    }

    /// Add equality.
    pub fn add_equality(&mut self, eq: Equality) {
        let lhs_root = self.find(eq.lhs);
        let rhs_root = self.find(eq.rhs);

        if lhs_root == rhs_root {
            return;
        }

        let lhs_rank = self.rank.get(&lhs_root).copied().unwrap_or(0);
        let rhs_rank = self.rank.get(&rhs_root).copied().unwrap_or(0);

        if lhs_rank < rhs_rank {
            self.parent.insert(lhs_root, rhs_root);
        } else if lhs_rank > rhs_rank {
            self.parent.insert(rhs_root, lhs_root);
        } else {
            self.parent.insert(lhs_root, rhs_root);
            self.rank.insert(rhs_root, rhs_rank + 1);
        }
    }

    /// Find representative.
    fn find(&mut self, mut term: TermId) -> TermId {
        let mut path = Vec::new();

        while let Some(&parent) = self.parent.get(&term) {
            if parent == term {
                break;
            }
            path.push(term);
            term = parent;
        }

        for node in path {
            self.parent.insert(node, term);
        }

        term
    }

    /// Check if equality is redundant.
    pub fn is_redundant(&mut self, eq: &Equality) -> bool {
        self.find(eq.lhs) == self.find(eq.rhs)
    }

    /// Minimize a set of equalities.
    pub fn minimize(&mut self, equalities: Vec<Equality>) -> Vec<Equality> {
        let mut minimal = Vec::new();

        for eq in equalities {
            if !self.is_redundant(&eq) {
                self.add_equality(eq);
                minimal.push(eq);
            }
        }

        minimal
    }

    /// Clear state.
    pub fn clear(&mut self) {
        self.parent.clear();
        self.rank.clear();
    }
}

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

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

    #[test]
    fn test_interface_eclass() {
        let mut eclass = InterfaceEClass::new(1, 0);
        eclass.add_term(2, 1);

        assert_eq!(eclass.members.len(), 2);
        assert!(eclass.is_shared());
    }

    #[test]
    fn test_minimal_generation() {
        let mut eclass = InterfaceEClass::new(1, 0);
        eclass.add_term(2, 0);
        eclass.add_term(3, 0);

        let equalities = eclass.generate_minimal(0, 0);
        assert_eq!(equalities.len(), 2); // {1,2}, {1,3}
    }

    #[test]
    fn test_manager_creation() {
        let manager = InterfaceEqualityManager::new();
        assert_eq!(manager.stats().equalities_generated, 0);
    }

    #[test]
    fn test_register_term() {
        let mut manager = InterfaceEqualityManager::new();
        manager.register_term(1, 0);
        manager.register_term(1, 1);

        assert_eq!(manager.stats().eclasses, 1);
    }

    #[test]
    fn test_assert_equality() {
        let mut manager = InterfaceEqualityManager::new();
        manager.register_term(1, 0);
        manager.register_term(2, 1);

        manager.assert_equality(1, 2).expect("Assert failed");
        assert!(manager.are_equal(1, 2));
    }

    #[test]
    fn test_get_pending() {
        let mut manager = InterfaceEqualityManager::new();
        manager.register_term(1, 0);
        manager.register_term(2, 1);
        manager.register_term(1, 1); // Make shared

        manager.assert_equality(1, 2).expect("Assert failed");

        let pending = manager.get_all_pending();
        assert!(!pending.is_empty());
    }

    #[test]
    fn test_minimization() {
        let mut manager = InterfaceEqualityManager::new();

        // Create a class with multiple terms
        for i in 1..=5 {
            manager.register_term(i, 0);
            manager.register_term(i, 1);
        }

        for i in 2..=5 {
            manager.assert_equality(1, i).expect("Assert failed");
        }

        manager.minimize_equalities();

        let pending = manager.get_all_pending();
        // Should have minimal equalities (star topology)
        assert!(pending.len() <= 4);
    }

    #[test]
    fn test_scheduler() {
        let mut scheduler = EqualityScheduler::new(SchedulingPolicy::Priority);

        let eq = InterfaceEquality {
            equality: Equality::new(1, 2),
            theories: FxHashSet::default(),
            priority: EqualityPriority {
                level: 100,
                relevancy: 50,
                decision_level: 0,
            },
            is_necessary: true,
            timestamp: 0,
        };

        scheduler.schedule(eq);
        assert!(scheduler.next().is_some());
    }

    #[test]
    fn test_minimizer() {
        let mut minimizer = EqualityMinimizer::new();

        let eq1 = Equality::new(1, 2);
        let eq2 = Equality::new(2, 3);
        let eq3 = Equality::new(1, 3); // Redundant

        minimizer.add_equality(eq1);
        minimizer.add_equality(eq2);

        assert!(minimizer.is_redundant(&eq3));
    }

    #[test]
    fn test_backtrack() {
        let mut manager = InterfaceEqualityManager::new();

        manager.push_decision_level();
        manager.register_term(1, 0);

        manager.backtrack(0).expect("Backtrack failed");
    }

    #[test]
    fn test_set_strategy() {
        let mut manager = InterfaceEqualityManager::new();
        manager.register_term(1, 0);

        manager
            .set_strategy(1, GenerationStrategy::Eager)
            .expect("Set strategy failed");

        let eclass = manager.get_eclass(1).expect("No eclass");
        assert_eq!(eclass.strategy, GenerationStrategy::Eager);
    }
}