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
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
//! Model-Based Instantiation Engine
//!
//! This module implements the core instantiation logic for MBQI. It handles:
//! - Extracting instantiations from models and counterexamples
//! - Conflict-driven instantiation
//! - Pattern matching and trigger selection
//! - Instantiation deduplication and filtering

#![allow(missing_docs)]
#![allow(dead_code)]

#[allow(unused_imports)]
use crate::prelude::*;
use num_bigint::BigInt;
use oxiz_core::ast::{TermId, TermKind, TermManager};
use oxiz_core::interner::Spur;
use oxiz_core::sort::SortId;
use smallvec::SmallVec;

use super::counterexample::CounterExampleGenerator;
use super::model_completion::CompletedModel;
use super::{Instantiation, InstantiationReason, QuantifiedFormula};

/// Context for instantiation
#[derive(Debug)]
pub struct InstantiationContext {
    /// Term manager
    pub manager: TermManager,
    /// Current model
    pub model: CompletedModel,
    /// Generation counter
    pub generation: u32,
    /// E-graph for equality reasoning (simplified)
    pub equalities: FxHashMap<TermId, TermId>,
}

impl InstantiationContext {
    /// Create a new instantiation context
    pub fn new(manager: TermManager) -> Self {
        Self {
            manager,
            model: CompletedModel::new(),
            generation: 0,
            equalities: FxHashMap::default(),
        }
    }

    /// Set the current model
    pub fn set_model(&mut self, model: CompletedModel) {
        self.model = model;
    }

    /// Increment generation
    pub fn next_generation(&mut self) {
        self.generation += 1;
    }

    /// Add an equality
    pub fn add_equality(&mut self, lhs: TermId, rhs: TermId) {
        self.equalities.insert(lhs, rhs);
        self.equalities.insert(rhs, lhs);
    }

    /// Find representative in equality graph
    pub fn find_representative(&self, term: TermId) -> TermId {
        let mut current = term;
        let mut visited = FxHashSet::default();

        while let Some(&next) = self.equalities.get(&current) {
            if visited.contains(&next) {
                break; // Cycle detected
            }
            visited.insert(current);
            current = next;
        }

        current
    }
}

/// Pattern for instantiation (E-matching style)
#[derive(Debug, Clone)]
pub struct InstantiationPattern {
    /// Terms that form the pattern
    pub terms: Vec<TermId>,
    /// Variables that must be matched
    pub vars: FxHashSet<Spur>,
    /// Number of variables
    pub num_vars: usize,
    /// Pattern quality (higher = better)
    pub quality: f64,
}

impl InstantiationPattern {
    /// Create a new pattern
    pub fn new(terms: Vec<TermId>) -> Self {
        Self {
            terms,
            vars: FxHashSet::default(),
            num_vars: 0,
            quality: 1.0,
        }
    }

    /// Extract patterns from a quantified formula
    pub fn extract_patterns(quantifier: &QuantifiedFormula, manager: &TermManager) -> Vec<Self> {
        let mut patterns = Vec::new();

        // Use explicit patterns if available
        if !quantifier.patterns.is_empty() {
            for pattern_terms in &quantifier.patterns {
                let mut pattern = Self::new(pattern_terms.clone());
                pattern.collect_vars(manager);
                pattern.calculate_quality(manager);
                patterns.push(pattern);
            }
        } else {
            // Auto-generate patterns from the body
            let generated = Self::generate_patterns(quantifier.body, manager);
            patterns.extend(generated);
        }

        patterns
    }

    /// Generate patterns from a term
    fn generate_patterns(term: TermId, manager: &TermManager) -> Vec<Self> {
        let mut patterns = Vec::new();
        let candidates = Self::collect_pattern_candidates(term, manager);

        for candidate in candidates {
            let mut pattern = Self::new(vec![candidate]);
            pattern.collect_vars(manager);
            if pattern.num_vars > 0 {
                pattern.calculate_quality(manager);
                patterns.push(pattern);
            }
        }

        patterns
    }

    /// Collect pattern candidates from a term
    fn collect_pattern_candidates(term: TermId, manager: &TermManager) -> Vec<TermId> {
        let mut candidates = Vec::new();
        let mut visited = FxHashSet::default();
        Self::collect_candidates_rec(term, &mut candidates, &mut visited, manager);
        candidates
    }

    fn collect_candidates_rec(
        term: TermId,
        candidates: &mut Vec<TermId>,
        visited: &mut FxHashSet<TermId>,
        manager: &TermManager,
    ) {
        if visited.contains(&term) {
            return;
        }
        visited.insert(term);

        let Some(t) = manager.get(term) else {
            return;
        };

        // Function applications are good pattern candidates
        if matches!(t.kind, TermKind::Apply { .. }) {
            candidates.push(term);
        }

        // Recurse into children
        match &t.kind {
            TermKind::Apply { args, .. } => {
                for &arg in args.iter() {
                    Self::collect_candidates_rec(arg, candidates, visited, manager);
                }
            }
            TermKind::And(args) | TermKind::Or(args) => {
                for &arg in args {
                    Self::collect_candidates_rec(arg, candidates, visited, manager);
                }
            }
            TermKind::Eq(lhs, rhs) | TermKind::Lt(lhs, rhs) | TermKind::Le(lhs, rhs) => {
                Self::collect_candidates_rec(*lhs, candidates, visited, manager);
                Self::collect_candidates_rec(*rhs, candidates, visited, manager);
            }
            _ => {}
        }
    }

    /// Collect variables in the pattern
    fn collect_vars(&mut self, manager: &TermManager) {
        self.vars.clear();
        let mut visited = FxHashSet::default();

        // Collect terms first to avoid borrow checker issues
        let terms: Vec<_> = self.terms.to_vec();
        for term in terms {
            self.collect_vars_rec(term, &mut visited, manager);
        }

        self.num_vars = self.vars.len();
    }

    fn collect_vars_rec(
        &mut self,
        term: TermId,
        visited: &mut FxHashSet<TermId>,
        manager: &TermManager,
    ) {
        if visited.contains(&term) {
            return;
        }
        visited.insert(term);

        let Some(t) = manager.get(term) else {
            return;
        };

        if let TermKind::Var(name) = t.kind {
            self.vars.insert(name);
        }

        // Recurse into children
        match &t.kind {
            TermKind::Apply { args, .. } => {
                for &arg in args.iter() {
                    self.collect_vars_rec(arg, visited, manager);
                }
            }
            TermKind::Not(arg) | TermKind::Neg(arg) => {
                self.collect_vars_rec(*arg, visited, manager);
            }
            TermKind::And(args) | TermKind::Or(args) => {
                for &arg in args {
                    self.collect_vars_rec(arg, visited, manager);
                }
            }
            _ => {}
        }
    }

    /// Calculate pattern quality
    fn calculate_quality(&mut self, manager: &TermManager) {
        // Quality factors:
        // - More variables = better (more specific)
        // - Fewer terms = better (simpler)
        // - Contains function applications = better

        let var_factor = 1.0 + (self.num_vars as f64);
        let term_factor = 1.0 / (1.0 + self.terms.len() as f64);
        let func_factor = if self.has_function_applications(manager) {
            2.0
        } else {
            1.0
        };

        self.quality = var_factor * term_factor * func_factor;
    }

    fn has_function_applications(&self, manager: &TermManager) -> bool {
        for &term in &self.terms {
            if let Some(t) = manager.get(term)
                && matches!(t.kind, TermKind::Apply { .. })
            {
                return true;
            }
        }
        false
    }
}

/// Quantifier instantiator
#[derive(Debug)]
pub struct QuantifierInstantiator {
    /// Counterexample generator
    cex_generator: CounterExampleGenerator,
    /// Deduplication cache
    dedup_cache: FxHashSet<InstantiationKey>,
    /// Statistics
    stats: InstantiatorStats,
}

impl QuantifierInstantiator {
    /// Create a new instantiator
    pub fn new() -> Self {
        Self {
            cex_generator: CounterExampleGenerator::new(),
            dedup_cache: FxHashSet::default(),
            stats: InstantiatorStats::default(),
        }
    }

    /// Generate instantiations for a quantifier using model-based approach
    pub fn instantiate_from_model(
        &mut self,
        quantifier: &QuantifiedFormula,
        model: &CompletedModel,
        manager: &mut TermManager,
    ) -> Vec<Instantiation> {
        self.stats.num_instantiation_attempts += 1;

        let mut instantiations = Vec::new();

        // Generate counterexamples
        let cex_result = self.cex_generator.generate(quantifier, model, manager);

        // Convert counterexamples to instantiations
        for cex in cex_result.counterexamples {
            // Apply substitution to get ground instance
            let ground_body = self.apply_substitution(quantifier.body, &cex.assignment, manager);

            let inst = cex.to_instantiation(ground_body);

            // Check for duplicates
            if self.is_duplicate(&inst) {
                self.stats.num_duplicates_filtered += 1;
                continue;
            }

            self.record_instantiation(&inst);
            instantiations.push(inst);
        }

        self.stats.num_instantiations_generated += instantiations.len();
        instantiations
    }

    /// Generate instantiations using conflict-driven approach
    pub fn instantiate_from_conflict(
        &mut self,
        quantifier: &QuantifiedFormula,
        conflict: &[TermId],
        model: &CompletedModel,
        manager: &mut TermManager,
    ) -> Vec<Instantiation> {
        let mut instantiations = Vec::new();

        // Analyze the conflict to extract relevant terms
        let conflict_terms = self.extract_relevant_terms(conflict, manager);

        // Try to build instantiations from conflict terms
        for assignment in
            self.build_assignments_from_terms(&quantifier.bound_vars, &conflict_terms, manager)
        {
            let ground_body = self.apply_substitution(quantifier.body, &assignment, manager);

            let inst = Instantiation::with_reason(
                quantifier.term,
                assignment,
                ground_body,
                model.generation,
                InstantiationReason::Conflict,
            );

            if !self.is_duplicate(&inst) {
                self.record_instantiation(&inst);
                instantiations.push(inst);
            }
        }

        instantiations
    }

    /// Apply substitution to a term
    fn apply_substitution(
        &self,
        term: TermId,
        subst: &FxHashMap<Spur, TermId>,
        manager: &mut TermManager,
    ) -> TermId {
        let mut cache = FxHashMap::default();
        self.apply_substitution_cached(term, subst, manager, &mut cache)
    }

    fn apply_substitution_cached(
        &self,
        term: TermId,
        subst: &FxHashMap<Spur, TermId>,
        manager: &mut TermManager,
        cache: &mut FxHashMap<TermId, TermId>,
    ) -> TermId {
        if let Some(&cached) = cache.get(&term) {
            return cached;
        }

        let Some(t) = manager.get(term).cloned() else {
            return term;
        };

        let result = match &t.kind {
            TermKind::Var(name) => subst.get(name).copied().unwrap_or(term),
            TermKind::Not(arg) => {
                let new_arg = self.apply_substitution_cached(*arg, subst, manager, cache);
                manager.mk_not(new_arg)
            }
            TermKind::And(args) => {
                let new_args: Vec<_> = args
                    .iter()
                    .map(|&a| self.apply_substitution_cached(a, subst, manager, cache))
                    .collect();
                manager.mk_and(new_args)
            }
            TermKind::Or(args) => {
                let new_args: Vec<_> = args
                    .iter()
                    .map(|&a| self.apply_substitution_cached(a, subst, manager, cache))
                    .collect();
                manager.mk_or(new_args)
            }
            TermKind::Eq(lhs, rhs) => {
                let new_lhs = self.apply_substitution_cached(*lhs, subst, manager, cache);
                let new_rhs = self.apply_substitution_cached(*rhs, subst, manager, cache);
                manager.mk_eq(new_lhs, new_rhs)
            }
            TermKind::Lt(lhs, rhs) => {
                let new_lhs = self.apply_substitution_cached(*lhs, subst, manager, cache);
                let new_rhs = self.apply_substitution_cached(*rhs, subst, manager, cache);
                manager.mk_lt(new_lhs, new_rhs)
            }
            TermKind::Le(lhs, rhs) => {
                let new_lhs = self.apply_substitution_cached(*lhs, subst, manager, cache);
                let new_rhs = self.apply_substitution_cached(*rhs, subst, manager, cache);
                manager.mk_le(new_lhs, new_rhs)
            }
            TermKind::Add(args) => {
                let new_args: SmallVec<[TermId; 4]> = args
                    .iter()
                    .map(|&a| self.apply_substitution_cached(a, subst, manager, cache))
                    .collect();
                manager.mk_add(new_args)
            }
            TermKind::Mul(args) => {
                let new_args: SmallVec<[TermId; 4]> = args
                    .iter()
                    .map(|&a| self.apply_substitution_cached(a, subst, manager, cache))
                    .collect();
                manager.mk_mul(new_args)
            }
            TermKind::Apply { func, args } => {
                let func_name = manager.resolve_str(*func).to_string();
                let new_args: SmallVec<[TermId; 4]> = args
                    .iter()
                    .map(|&a| self.apply_substitution_cached(a, subst, manager, cache))
                    .collect();
                manager.mk_apply(&func_name, new_args, t.sort)
            }
            TermKind::Select(array, index) => {
                let new_array = self.apply_substitution_cached(*array, subst, manager, cache);
                let new_index = self.apply_substitution_cached(*index, subst, manager, cache);
                manager.mk_select(new_array, new_index)
            }
            TermKind::Store(array, index, value) => {
                let new_array = self.apply_substitution_cached(*array, subst, manager, cache);
                let new_index = self.apply_substitution_cached(*index, subst, manager, cache);
                let new_value = self.apply_substitution_cached(*value, subst, manager, cache);
                manager.mk_store(new_array, new_index, new_value)
            }
            // Constants and other terms (Implies, Gt, Ge, Sub, Div, Mod,
            // Neg, Ite) are not traversed here. The MBQIIntegration layer
            // re-applies substitution for instantiation engine results using
            // its own complete implementation.
            _ => term,
        };

        cache.insert(term, result);
        result
    }

    /// Extract relevant terms from conflict clause
    fn extract_relevant_terms(&self, conflict: &[TermId], manager: &TermManager) -> Vec<TermId> {
        let mut terms = Vec::new();
        let mut visited = FxHashSet::default();

        for &term in conflict {
            self.extract_relevant_terms_rec(term, &mut terms, &mut visited, manager);
        }

        terms
    }

    fn extract_relevant_terms_rec(
        &self,
        term: TermId,
        terms: &mut Vec<TermId>,
        visited: &mut FxHashSet<TermId>,
        manager: &TermManager,
    ) {
        if visited.contains(&term) {
            return;
        }
        visited.insert(term);

        let Some(t) = manager.get(term) else {
            return;
        };

        // Collect ground terms
        if self.is_ground_value(term, manager) {
            terms.push(term);
        }

        // Recurse into children
        match &t.kind {
            TermKind::Not(arg) | TermKind::Neg(arg) => {
                self.extract_relevant_terms_rec(*arg, terms, visited, manager);
            }
            TermKind::And(args) | TermKind::Or(args) => {
                for &arg in args {
                    self.extract_relevant_terms_rec(arg, terms, visited, manager);
                }
            }
            TermKind::Eq(lhs, rhs) | TermKind::Lt(lhs, rhs) => {
                self.extract_relevant_terms_rec(*lhs, terms, visited, manager);
                self.extract_relevant_terms_rec(*rhs, terms, visited, manager);
            }
            TermKind::Apply { args, .. } => {
                for &arg in args.iter() {
                    self.extract_relevant_terms_rec(arg, terms, visited, manager);
                }
            }
            _ => {}
        }
    }

    /// Check if a term is a ground value
    fn is_ground_value(&self, term: TermId, manager: &TermManager) -> bool {
        let Some(t) = manager.get(term) else {
            return false;
        };

        matches!(
            t.kind,
            TermKind::True
                | TermKind::False
                | TermKind::IntConst(_)
                | TermKind::RealConst(_)
                | TermKind::BitVecConst { .. }
        )
    }

    /// Build assignments from terms
    fn build_assignments_from_terms(
        &self,
        bound_vars: &[(Spur, SortId)],
        terms: &[TermId],
        manager: &TermManager,
    ) -> Vec<FxHashMap<Spur, TermId>> {
        let mut assignments = Vec::new();

        // Group terms by sort
        let mut terms_by_sort: FxHashMap<SortId, Vec<TermId>> = FxHashMap::default();
        for &term in terms {
            if let Some(t) = manager.get(term) {
                terms_by_sort.entry(t.sort).or_default().push(term);
            }
        }

        // Build candidate lists for each variable
        let mut candidates = Vec::new();
        for &(_name, sort) in bound_vars {
            let sort_terms = terms_by_sort
                .get(&sort)
                .map(|v| v.as_slice())
                .unwrap_or(&[]);
            candidates.push(sort_terms.to_vec());
        }

        // Enumerate combinations (limited)
        let max_combinations = 10;
        let mut indices = vec![0usize; bound_vars.len()];

        for _ in 0..max_combinations {
            let mut assignment = FxHashMap::default();
            let mut valid = true;

            for (i, &idx) in indices.iter().enumerate() {
                if let Some(cands) = candidates.get(i) {
                    if let Some(&term) = cands.get(idx) {
                        if let Some((name, _)) = bound_vars.get(i) {
                            assignment.insert(*name, term);
                        }
                    } else {
                        valid = false;
                        break;
                    }
                }
            }

            if valid && assignment.len() == bound_vars.len() {
                assignments.push(assignment);
            }

            // Increment indices
            let mut carry = true;
            for (i, idx) in indices.iter_mut().enumerate() {
                if carry {
                    *idx += 1;
                    let limit = candidates.get(i).map_or(1, |c| c.len());
                    if *idx >= limit {
                        *idx = 0;
                    } else {
                        carry = false;
                    }
                }
            }

            if carry {
                break;
            }
        }

        assignments
    }

    /// Check if an instantiation is a duplicate
    fn is_duplicate(&self, inst: &Instantiation) -> bool {
        let key = InstantiationKey::from_instantiation(inst);
        self.dedup_cache.contains(&key)
    }

    /// Record an instantiation for deduplication
    fn record_instantiation(&mut self, inst: &Instantiation) {
        let key = InstantiationKey::from_instantiation(inst);
        self.dedup_cache.insert(key);
    }

    /// Clear deduplication cache
    pub fn clear_cache(&mut self) {
        self.dedup_cache.clear();
        self.cex_generator.clear_cache();
    }

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

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

/// Key for instantiation deduplication
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct InstantiationKey {
    quantifier: TermId,
    binding: Vec<(Spur, TermId)>,
}

impl InstantiationKey {
    fn from_instantiation(inst: &Instantiation) -> Self {
        let mut binding: Vec<_> = inst.substitution.iter().map(|(&k, &v)| (k, v)).collect();
        binding.sort_by_key(|(k, _)| *k);
        Self {
            quantifier: inst.quantifier,
            binding,
        }
    }
}

/// Instantiation engine that coordinates all instantiation strategies
#[derive(Debug)]
pub struct InstantiationEngine {
    /// Main quantifier instantiator
    instantiator: QuantifierInstantiator,
    /// Pattern matcher
    pattern_matcher: PatternMatcher,
    /// Enumerative instantiation
    enumerative: EnumerativeInstantiator,
    /// Statistics
    stats: EngineStats,
}

impl InstantiationEngine {
    /// Create a new instantiation engine
    pub fn new() -> Self {
        Self {
            instantiator: QuantifierInstantiator::new(),
            pattern_matcher: PatternMatcher::new(),
            enumerative: EnumerativeInstantiator::new(),
            stats: EngineStats::default(),
        }
    }

    /// Generate instantiations for a quantifier
    pub fn instantiate(
        &mut self,
        quantifier: &QuantifiedFormula,
        model: &CompletedModel,
        manager: &mut TermManager,
    ) -> Vec<Instantiation> {
        let mut instantiations = Vec::new();

        // Strategy 1: Model-based instantiation
        let model_based = self
            .instantiator
            .instantiate_from_model(quantifier, model, manager);
        instantiations.extend(model_based);

        // Strategy 2: Pattern-based instantiation (if patterns exist)
        if !quantifier.patterns.is_empty() {
            let pattern_based = self
                .pattern_matcher
                .match_patterns(quantifier, model, manager);
            instantiations.extend(pattern_based);
        }

        // Strategy 3: Enumerative instantiation (as fallback)
        if instantiations.is_empty() {
            let enumerative = self.enumerative.enumerate(quantifier, model, manager, 10);
            instantiations.extend(enumerative);
        }

        self.stats.num_instantiations += instantiations.len();
        instantiations
    }

    /// Clear all caches
    pub fn clear_caches(&mut self) {
        self.instantiator.clear_cache();
        self.pattern_matcher.clear_cache();
    }

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

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

/// Pattern matcher for E-matching style instantiation
#[derive(Debug)]
struct PatternMatcher {
    /// Match cache
    cache: FxHashMap<TermId, Vec<FxHashMap<Spur, TermId>>>,
}

impl PatternMatcher {
    fn new() -> Self {
        Self {
            cache: FxHashMap::default(),
        }
    }

    fn match_patterns(
        &mut self,
        quantifier: &QuantifiedFormula,
        model: &CompletedModel,
        manager: &mut TermManager,
    ) -> Vec<Instantiation> {
        let mut instantiations = Vec::new();

        // Extract patterns
        let patterns = InstantiationPattern::extract_patterns(quantifier, manager);

        // Match each pattern
        for pattern in patterns {
            let matches = self.match_pattern(&pattern, model, manager);
            for assignment in matches {
                let ground_body = self.apply_substitution(quantifier.body, &assignment, manager);
                let inst = Instantiation::with_reason(
                    quantifier.term,
                    assignment,
                    ground_body,
                    model.generation,
                    InstantiationReason::EMatching,
                );
                instantiations.push(inst);
            }
        }

        instantiations
    }

    fn match_pattern(
        &self,
        _pattern: &InstantiationPattern,
        _model: &CompletedModel,
        _manager: &TermManager,
    ) -> Vec<FxHashMap<Spur, TermId>> {
        // Simplified pattern matching
        // A full implementation would use E-matching algorithms
        Vec::new()
    }

    fn apply_substitution(
        &self,
        term: TermId,
        subst: &FxHashMap<Spur, TermId>,
        manager: &mut TermManager,
    ) -> TermId {
        // Reuse implementation from QuantifierInstantiator
        let instantiator = QuantifierInstantiator::new();
        instantiator.apply_substitution(term, subst, manager)
    }

    fn clear_cache(&mut self) {
        self.cache.clear();
    }
}

/// Enumerative instantiator (brute-force small domain)
#[derive(Debug)]
struct EnumerativeInstantiator;

impl EnumerativeInstantiator {
    fn new() -> Self {
        Self
    }

    fn enumerate(
        &self,
        quantifier: &QuantifiedFormula,
        model: &CompletedModel,
        manager: &mut TermManager,
        max_per_var: usize,
    ) -> Vec<Instantiation> {
        let mut instantiations = Vec::new();

        // Build small domains for each variable
        let domains = self.build_small_domains(&quantifier.bound_vars, model, manager, max_per_var);

        // Enumerate all combinations
        let combinations = self.enumerate_combinations(&domains);

        for combo in combinations {
            let mut assignment = FxHashMap::default();
            for (i, &value) in combo.iter().enumerate() {
                if let Some((name, _)) = quantifier.bound_vars.get(i) {
                    assignment.insert(*name, value);
                }
            }

            let instantiator = QuantifierInstantiator::new();
            let ground_body =
                instantiator.apply_substitution(quantifier.body, &assignment, manager);

            let inst = Instantiation::with_reason(
                quantifier.term,
                assignment,
                ground_body,
                model.generation,
                InstantiationReason::Enumerative,
            );
            instantiations.push(inst);
        }

        instantiations
    }

    fn build_small_domains(
        &self,
        bound_vars: &[(Spur, SortId)],
        model: &CompletedModel,
        manager: &mut TermManager,
        max_per_var: usize,
    ) -> Vec<Vec<TermId>> {
        let mut domains = Vec::new();

        for &(_name, sort) in bound_vars {
            let mut domain = Vec::new();

            // Use universe if available
            if let Some(universe) = model.universe(sort) {
                domain.extend_from_slice(universe);
            }

            // Add default integer candidates from -2 to 5
            if sort == manager.sorts.int_sort {
                for i in -2i64..=5 {
                    let val = manager.mk_int(BigInt::from(i));
                    if !domain.contains(&val) {
                        domain.push(val);
                    }
                }
            } else if sort == manager.sorts.bool_sort {
                domain.push(manager.mk_true());
                domain.push(manager.mk_false());
            }

            domain.truncate(max_per_var);
            domains.push(domain);
        }

        domains
    }

    fn enumerate_combinations(&self, domains: &[Vec<TermId>]) -> Vec<Vec<TermId>> {
        if domains.is_empty() {
            return vec![vec![]];
        }

        let mut results = Vec::new();
        let mut indices = vec![0usize; domains.len()];
        let max_results = 100; // Limit total combinations

        loop {
            let combo: Vec<TermId> = indices
                .iter()
                .enumerate()
                .filter_map(|(i, &idx)| domains.get(i).and_then(|d| d.get(idx).copied()))
                .collect();

            if combo.len() == domains.len() {
                results.push(combo);
            }

            if results.len() >= max_results {
                break;
            }

            // Increment
            let mut carry = true;
            for (i, idx) in indices.iter_mut().enumerate() {
                if carry {
                    *idx += 1;
                    let limit = domains.get(i).map_or(1, |d| d.len());
                    if *idx >= limit {
                        *idx = 0;
                    } else {
                        carry = false;
                    }
                }
            }

            if carry {
                break;
            }
        }

        results
    }
}

/// Statistics for instantiator
#[derive(Debug, Clone, Default)]
pub struct InstantiatorStats {
    pub num_instantiation_attempts: usize,
    pub num_instantiations_generated: usize,
    pub num_duplicates_filtered: usize,
}

/// Statistics for engine
#[derive(Debug, Clone, Default)]
pub struct EngineStats {
    pub num_instantiations: usize,
}

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

    #[test]
    fn test_instantiation_context_creation() {
        let manager = TermManager::new();
        let ctx = InstantiationContext::new(manager);
        assert_eq!(ctx.generation, 0);
    }

    #[test]
    fn test_instantiation_context_generation() {
        let manager = TermManager::new();
        let mut ctx = InstantiationContext::new(manager);
        ctx.next_generation();
        assert_eq!(ctx.generation, 1);
    }

    #[test]
    fn test_instantiation_pattern_creation() {
        let pattern = InstantiationPattern::new(vec![TermId::new(1)]);
        assert_eq!(pattern.terms.len(), 1);
        assert_eq!(pattern.num_vars, 0);
    }

    #[test]
    fn test_quantifier_instantiator_creation() {
        let inst = QuantifierInstantiator::new();
        assert_eq!(inst.stats.num_instantiation_attempts, 0);
    }

    #[test]
    fn test_instantiation_key_equality() {
        let key1 = InstantiationKey {
            quantifier: TermId::new(1),
            binding: vec![(
                Spur::try_from_usize(1).expect("valid spur"),
                TermId::new(10),
            )],
        };
        let key2 = InstantiationKey {
            quantifier: TermId::new(1),
            binding: vec![(
                Spur::try_from_usize(1).expect("valid spur"),
                TermId::new(10),
            )],
        };
        assert_eq!(key1, key2);
    }

    #[test]
    fn test_instantiation_engine_creation() {
        let engine = InstantiationEngine::new();
        assert_eq!(engine.stats.num_instantiations, 0);
    }

    #[test]
    fn test_pattern_matcher_creation() {
        let matcher = PatternMatcher::new();
        assert_eq!(matcher.cache.len(), 0);
    }

    #[test]
    fn test_enumerative_instantiator_creation() {
        let _enum_inst = EnumerativeInstantiator::new();
    }
}