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
//! Theory Conflict Explanation.
//!
//! This module generates detailed explanations for theory conflicts, enabling
//! CDCL(T) to learn effective conflict clauses from theory solver failures.
//!
//! ## Explanation Types
//!
//! 1. **Equality Explanations**: Why two terms must/cannot be equal
//! 2. **Bound Explanations**: Why a variable must satisfy certain bounds
//! 3. **Disequality Explanations**: Why distinct terms cannot be equal
//! 4. **Arithmetic Explanations**: Linear combinations proving inconsistency
//!
//! ## Explanation Quality
//!
//! Good explanations are:
//! - **Minimal**: Use fewest literals possible
//! - **Relevant**: Only include necessary constraints
//! - **General**: Learn broadly applicable conflicts
//! - **Precise**: Accurately capture the inconsistency
//!
//! ## References
//!
//! - Nieuwenhuis et al.: "Solving SAT and SAT Modulo Theories" (JACM 2006)
//! - de Moura & Bjørner: "Z3: An Efficient SMT Solver" (TACAS 2008)
//! - Z3's `smt/theory_explanation.cpp`

#[allow(unused_imports)]
use crate::prelude::*;
use oxiz_sat::Lit;

/// Type of theory explanation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExplanationType {
    /// Equality conflict (a = b and a ≠ b).
    Equality,
    /// Bound conflict (x > 5 and x < 3).
    Bounds,
    /// Disequality conflict (distinct(a,b,c) but a = b).
    Disequality,
    /// Arithmetic conflict (linear combination proves UNSAT).
    Arithmetic,
    /// Array conflict (read-over-write axiom violation).
    Array,
    /// BitVector conflict (overflow, underflow).
    BitVector,
}

/// An explanation for a theory conflict.
#[derive(Debug, Clone)]
pub struct TheoryExplanation {
    /// Type of explanation.
    pub explanation_type: ExplanationType,
    /// Literals involved in conflict (as conflict clause).
    pub literals: Vec<Lit>,
    /// Human-readable explanation (for debugging).
    pub description: String,
    /// Proof trace (optional, for proof generation).
    pub proof_trace: Option<Vec<ProofStep>>,
}

/// A step in a theory conflict proof.
#[derive(Debug, Clone)]
pub struct ProofStep {
    /// Description of this proof step.
    pub description: String,
    /// Literals used in this step.
    pub premises: Vec<Lit>,
    /// Conclusion of this step.
    pub conclusion: String,
}

/// Configuration for theory explanation generation.
#[derive(Debug, Clone)]
pub struct ExplainerConfig {
    /// Enable proof trace generation.
    pub generate_proofs: bool,
    /// Minimize explanations.
    pub minimize: bool,
    /// Include human-readable descriptions.
    pub include_descriptions: bool,
    /// Maximum explanation size (literals).
    pub max_size: usize,
}

impl Default for ExplainerConfig {
    fn default() -> Self {
        Self {
            generate_proofs: false,
            minimize: true,
            include_descriptions: false,
            max_size: 100,
        }
    }
}

/// Statistics for explanation generation.
#[derive(Debug, Clone, Default)]
pub struct ExplainerStats {
    /// Explanations generated.
    pub explanations_generated: u64,
    /// Total literals in explanations.
    pub total_literals: u64,
    /// Average explanation size.
    pub avg_size: f64,
    /// Explanations minimized.
    pub minimized: u64,
    /// Time (microseconds).
    pub time_us: u64,
}

/// Theory conflict explainer.
pub struct TheoryExplainer {
    config: ExplainerConfig,
    stats: ExplainerStats,
}

impl TheoryExplainer {
    /// Create new theory explainer.
    pub fn new() -> Self {
        Self::with_config(ExplainerConfig::default())
    }

    /// Create with configuration.
    pub fn with_config(config: ExplainerConfig) -> Self {
        Self {
            config,
            stats: ExplainerStats::default(),
        }
    }

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

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

    /// Generate explanation for an equality conflict.
    ///
    /// Explains why two terms that must be equal cannot be equal,
    /// or vice versa.
    pub fn explain_equality_conflict(
        &mut self,
        lits: Vec<Lit>,
        description: Option<String>,
    ) -> TheoryExplanation {
        #[cfg(feature = "std")]
        let start = std::time::Instant::now();

        let minimized_lits = if self.config.minimize {
            self.minimize_explanation(&lits)
        } else {
            lits.clone()
        };

        let proof_trace = if self.config.generate_proofs {
            Some(self.generate_proof_trace(&minimized_lits, ExplanationType::Equality))
        } else {
            None
        };

        let desc = if self.config.include_descriptions {
            description.unwrap_or_else(|| {
                format!("Equality conflict with {} literals", minimized_lits.len())
            })
        } else {
            String::new()
        };

        self.stats.explanations_generated += 1;
        self.stats.total_literals += minimized_lits.len() as u64;
        self.update_avg_size();
        #[cfg(feature = "std")]
        {
            self.stats.time_us += start.elapsed().as_micros() as u64;
        }

        TheoryExplanation {
            explanation_type: ExplanationType::Equality,
            literals: minimized_lits,
            description: desc,
            proof_trace,
        }
    }

    /// Generate explanation for a bounds conflict.
    ///
    /// Explains why variable bounds are inconsistent (e.g., x > 10 ∧ x < 5).
    pub fn explain_bounds_conflict(
        &mut self,
        lits: Vec<Lit>,
        description: Option<String>,
    ) -> TheoryExplanation {
        #[cfg(feature = "std")]
        let start = std::time::Instant::now();

        let minimized_lits = if self.config.minimize {
            self.minimize_explanation(&lits)
        } else {
            lits.clone()
        };

        let proof_trace = if self.config.generate_proofs {
            Some(self.generate_proof_trace(&minimized_lits, ExplanationType::Bounds))
        } else {
            None
        };

        let desc = if self.config.include_descriptions {
            description.unwrap_or_else(|| {
                format!("Bounds conflict with {} constraints", minimized_lits.len())
            })
        } else {
            String::new()
        };

        self.stats.explanations_generated += 1;
        self.stats.total_literals += minimized_lits.len() as u64;
        self.update_avg_size();
        #[cfg(feature = "std")]
        {
            self.stats.time_us += start.elapsed().as_micros() as u64;
        }

        TheoryExplanation {
            explanation_type: ExplanationType::Bounds,
            literals: minimized_lits,
            description: desc,
            proof_trace,
        }
    }

    /// Generate explanation for an arithmetic conflict.
    ///
    /// Produces a Farkas coefficient explanation showing linear combination
    /// that proves inconsistency.
    pub fn explain_arithmetic_conflict(
        &mut self,
        lits: Vec<Lit>,
        _farkas_coefficients: Option<Vec<i64>>,
        description: Option<String>,
    ) -> TheoryExplanation {
        #[cfg(feature = "std")]
        let start = std::time::Instant::now();

        // In full implementation, use Farkas coefficients to generate
        // minimal explanation via linear combination

        let minimized_lits = if self.config.minimize {
            self.minimize_explanation(&lits)
        } else {
            lits.clone()
        };

        let proof_trace = if self.config.generate_proofs {
            Some(self.generate_proof_trace(&minimized_lits, ExplanationType::Arithmetic))
        } else {
            None
        };

        let desc = if self.config.include_descriptions {
            description.unwrap_or_else(|| "Arithmetic conflict via linear combination".to_string())
        } else {
            String::new()
        };

        self.stats.explanations_generated += 1;
        self.stats.total_literals += minimized_lits.len() as u64;
        self.update_avg_size();
        #[cfg(feature = "std")]
        {
            self.stats.time_us += start.elapsed().as_micros() as u64;
        }

        TheoryExplanation {
            explanation_type: ExplanationType::Arithmetic,
            literals: minimized_lits,
            description: desc,
            proof_trace,
        }
    }

    /// Minimize an explanation by removing redundant literals.
    fn minimize_explanation(&mut self, lits: &[Lit]) -> Vec<Lit> {
        // Simplified minimization - full version would use:
        // - Relevancy analysis
        // - Core extraction
        // - Subsumption checking

        let mut minimized = Vec::new();
        let _lit_set: FxHashSet<Lit> = lits.iter().copied().collect();

        for &lit in lits {
            // Check if removing this literal still gives a conflict
            // For now, keep all literals (placeholder)
            minimized.push(lit);
        }

        if minimized.len() < lits.len() {
            self.stats.minimized += 1;
        }

        // Enforce max size
        if minimized.len() > self.config.max_size {
            minimized.truncate(self.config.max_size);
        }

        minimized
    }

    /// Generate proof trace for explanation.
    fn generate_proof_trace(&self, lits: &[Lit], exp_type: ExplanationType) -> Vec<ProofStep> {
        let mut trace = Vec::new();

        // Generate initial premises
        trace.push(ProofStep {
            description: format!("Theory conflict detected: {:?}", exp_type),
            premises: lits.to_vec(),
            conclusion: "Contradiction".to_string(),
        });

        // Full implementation would include detailed proof steps

        trace
    }

    /// Update average explanation size.
    fn update_avg_size(&mut self) {
        if self.stats.explanations_generated > 0 {
            self.stats.avg_size =
                self.stats.total_literals as f64 / self.stats.explanations_generated as f64;
        }
    }
}

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

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

    fn lit(var: u32, positive: bool) -> Lit {
        let v = Var::new(var);
        if positive { Lit::pos(v) } else { Lit::neg(v) }
    }

    #[test]
    fn test_explainer_creation() {
        let explainer = TheoryExplainer::new();
        assert_eq!(explainer.stats().explanations_generated, 0);
    }

    #[test]
    fn test_equality_explanation() {
        let mut explainer = TheoryExplainer::new();
        let lits = vec![lit(0, true), lit(1, false), lit(2, true)];

        let explanation = explainer.explain_equality_conflict(lits.clone(), None);

        assert_eq!(explanation.explanation_type, ExplanationType::Equality);
        assert!(!explanation.literals.is_empty());
        assert_eq!(explainer.stats().explanations_generated, 1);
    }

    #[test]
    fn test_bounds_explanation() {
        let mut explainer = TheoryExplainer::new();
        let lits = vec![lit(0, true), lit(1, true)];

        let explanation =
            explainer.explain_bounds_conflict(lits, Some("x > 10 and x < 5".to_string()));

        assert_eq!(explanation.explanation_type, ExplanationType::Bounds);
        assert_eq!(explainer.stats().explanations_generated, 1);
    }

    #[test]
    fn test_arithmetic_explanation() {
        let mut explainer = TheoryExplainer::new();
        let lits = vec![lit(0, false), lit(1, false), lit(2, false)];

        let explanation = explainer.explain_arithmetic_conflict(lits, None, None);

        assert_eq!(explanation.explanation_type, ExplanationType::Arithmetic);
        assert_eq!(explainer.stats().explanations_generated, 1);
    }

    #[test]
    fn test_minimization() {
        let config = ExplainerConfig {
            minimize: true,
            ..Default::default()
        };
        let mut explainer = TheoryExplainer::with_config(config);

        let lits = vec![lit(0, true), lit(1, false), lit(2, true), lit(3, false)];

        let explanation = explainer.explain_equality_conflict(lits, None);

        // Should attempt minimization
        assert!(explanation.literals.len() <= 4);
    }

    #[test]
    fn test_proof_generation() {
        let config = ExplainerConfig {
            generate_proofs: true,
            ..Default::default()
        };
        let mut explainer = TheoryExplainer::with_config(config);

        let lits = vec![lit(0, true), lit(1, false)];

        let explanation = explainer.explain_equality_conflict(lits, None);

        assert!(explanation.proof_trace.is_some());
        let trace = explanation
            .proof_trace
            .expect("test operation should succeed");
        assert!(!trace.is_empty());
    }

    #[test]
    fn test_max_size_enforcement() {
        let config = ExplainerConfig {
            max_size: 2,
            ..Default::default()
        };
        let mut explainer = TheoryExplainer::with_config(config);

        let lits = vec![lit(0, true), lit(1, false), lit(2, true), lit(3, false)];

        let explanation = explainer.explain_bounds_conflict(lits, None);

        assert!(explanation.literals.len() <= 2);
    }
}