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
//! Value Management for Model Construction.
//!
//! Manages theory-specific values during model building, handling type
//! conversions, normalization, and consistency checks.
//!
//! ## Value Types
//!
//! - **Boolean**: True/False
//! - **Integer**: Arbitrary precision integers
//! - **Rational**: Arbitrary precision rationals
//! - **BitVector**: Fixed-width bit strings
//! - **Array**: Function from indices to values
//! - **Datatype**: ADT constructor applications
//! - **Uninterpreted**: Abstract values with equality
//!
//! ## References
//!
//! - Z3's `model/model.cpp`
//! - "Model Construction in SMT" (Barrett et al., 2009)

#[allow(unused_imports)]
use crate::prelude::*;
use num_bigint::BigInt;
use num_rational::BigRational;

use crate::{Sort, Term, TermId};

/// A value in a model.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ModelValue {
    /// Boolean value.
    Bool(bool),
    /// Integer value.
    Int(BigInt),
    /// Rational value.
    Rational(BigRational),
    /// Bit-vector value (width, value).
    BitVector(usize, BigInt),
    /// Array value (default + explicit mappings).
    Array {
        default: Box<ModelValue>,
        entries: Vec<(ModelValue, ModelValue)>,
    },
    /// Datatype constructor application.
    Datatype {
        constructor: String,
        fields: Vec<ModelValue>,
    },
    /// Uninterpreted value (sort + identifier).
    Uninterpreted(Sort, usize),
    /// Function interpretation (args -> result).
    Function {
        domain: Vec<Sort>,
        codomain: Sort,
        mappings: Vec<(Vec<ModelValue>, ModelValue)>,
        default: Option<Box<ModelValue>>,
    },
}

impl ModelValue {
    /// Check if this is a boolean value.
    pub fn is_bool(&self) -> bool {
        matches!(self, ModelValue::Bool(_))
    }

    /// Try to extract as boolean.
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            ModelValue::Bool(b) => Some(*b),
            _ => None,
        }
    }

    /// Try to extract as integer.
    pub fn as_int(&self) -> Option<&BigInt> {
        match self {
            ModelValue::Int(i) => Some(i),
            _ => None,
        }
    }

    /// Try to extract as rational.
    pub fn as_rational(&self) -> Option<&BigRational> {
        match self {
            ModelValue::Rational(r) => Some(r),
            _ => None,
        }
    }

    /// Check type compatibility with a sort.
    pub fn compatible_with_sort(&self, sort: &Sort) -> bool {
        // Simplified: would do proper type checking
        true
    }
}

/// Configuration for value management.
#[derive(Debug, Clone)]
pub struct ValueManagerConfig {
    /// Enable value normalization.
    pub normalize_values: bool,
    /// Cache converted values.
    pub enable_caching: bool,
    /// Maximum uninterpreted value ID.
    pub max_uninterpreted_id: usize,
}

impl Default for ValueManagerConfig {
    fn default() -> Self {
        Self {
            normalize_values: true,
            enable_caching: true,
            max_uninterpreted_id: 100000,
        }
    }
}

/// Statistics for value management.
#[derive(Debug, Clone, Default)]
pub struct ValueManagerStats {
    /// Values created.
    pub values_created: u64,
    /// Conversions performed.
    pub conversions: u64,
    /// Cache hits.
    pub cache_hits: u64,
    /// Cache misses.
    pub cache_misses: u64,
}

/// Value manager for model construction.
pub struct ValueManager {
    /// Configuration.
    config: ValueManagerConfig,
    /// Statistics.
    stats: ValueManagerStats,
    /// Term to value mapping.
    term_values: FxHashMap<TermId, ModelValue>,
    /// Next uninterpreted value ID per sort.
    next_uninterp_id: FxHashMap<Sort, usize>,
    /// Value cache (for normalization).
    cache: FxHashMap<ModelValue, ModelValue>,
}

impl ValueManager {
    /// Create a new value manager.
    pub fn new(config: ValueManagerConfig) -> Self {
        Self {
            config,
            stats: ValueManagerStats::default(),
            term_values: FxHashMap::default(),
            next_uninterp_id: FxHashMap::default(),
            cache: FxHashMap::default(),
        }
    }

    /// Create with default configuration.
    pub fn default_config() -> Self {
        Self::new(ValueManagerConfig::default())
    }

    /// Create a boolean value.
    pub fn mk_bool(&mut self, value: bool) -> ModelValue {
        self.stats.values_created += 1;
        ModelValue::Bool(value)
    }

    /// Create an integer value.
    pub fn mk_int(&mut self, value: BigInt) -> ModelValue {
        self.stats.values_created += 1;
        let val = ModelValue::Int(value);

        if self.config.normalize_values {
            self.normalize(val)
        } else {
            val
        }
    }

    /// Create a rational value.
    pub fn mk_rational(&mut self, value: BigRational) -> ModelValue {
        self.stats.values_created += 1;
        let val = ModelValue::Rational(value);

        if self.config.normalize_values {
            self.normalize(val)
        } else {
            val
        }
    }

    /// Create a bit-vector value.
    pub fn mk_bitvector(&mut self, width: usize, value: BigInt) -> ModelValue {
        self.stats.values_created += 1;

        // Mask to width
        let mask = (BigInt::from(1) << width) - 1;
        let masked_value = value & mask;

        ModelValue::BitVector(width, masked_value)
    }

    /// Create a fresh uninterpreted value.
    pub fn mk_uninterpreted(&mut self, sort: Sort) -> ModelValue {
        self.stats.values_created += 1;

        let id = *self.next_uninterp_id.entry(sort.clone()).or_insert(0);
        self.next_uninterp_id.insert(sort.clone(), id + 1);

        ModelValue::Uninterpreted(sort, id)
    }

    /// Create an array value.
    pub fn mk_array(
        &mut self,
        default: ModelValue,
        entries: Vec<(ModelValue, ModelValue)>,
    ) -> ModelValue {
        self.stats.values_created += 1;

        ModelValue::Array {
            default: Box::new(default),
            entries,
        }
    }

    /// Create a datatype value.
    pub fn mk_datatype(&mut self, constructor: String, fields: Vec<ModelValue>) -> ModelValue {
        self.stats.values_created += 1;

        ModelValue::Datatype { constructor, fields }
    }

    /// Normalize a value.
    fn normalize(&mut self, value: ModelValue) -> ModelValue {
        if !self.config.enable_caching {
            return value;
        }

        // Check cache
        if let Some(cached) = self.cache.get(&value) {
            self.stats.cache_hits += 1;
            return cached.clone();
        }

        self.stats.cache_misses += 1;

        // Perform normalization based on value type
        let normalized = match &value {
            ModelValue::Rational(r) => {
                // Reduce fraction to lowest terms (already done by BigRational)
                value.clone()
            }
            ModelValue::BitVector(w, v) => {
                // Ensure value fits in width
                let mask = (BigInt::from(1) << w) - 1;
                ModelValue::BitVector(*w, v & mask)
            }
            _ => value.clone(),
        };

        // Cache the result
        self.cache.insert(value, normalized.clone());

        normalized
    }

    /// Assign a value to a term.
    pub fn assign(&mut self, term: TermId, value: ModelValue) {
        self.term_values.insert(term, value);
    }

    /// Get the value assigned to a term.
    pub fn get_value(&self, term: TermId) -> Option<&ModelValue> {
        self.term_values.get(&term)
    }

    /// Convert a value to a term representation.
    pub fn value_to_term(&mut self, value: &ModelValue) -> Option<TermId> {
        self.stats.conversions += 1;

        // Simplified: would construct actual terms
        // For now, return None
        None
    }

    /// Evaluate a term given current assignments.
    pub fn eval(&self, term: TermId) -> Option<ModelValue> {
        // Simplified: would recursively evaluate term
        self.term_values.get(&term).cloned()
    }

    /// Check if a value satisfies type constraints.
    pub fn check_type(&self, value: &ModelValue, sort: &Sort) -> bool {
        value.compatible_with_sort(sort)
    }

    /// Get number of assigned values.
    pub fn num_assignments(&self) -> usize {
        self.term_values.len()
    }

    /// Clear all assignments.
    pub fn clear(&mut self) {
        self.term_values.clear();
        self.cache.clear();
    }

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

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

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

    #[test]
    fn test_manager_creation() {
        let manager = ValueManager::default_config();
        assert_eq!(manager.stats().values_created, 0);
    }

    #[test]
    fn test_create_bool() {
        let mut manager = ValueManager::default_config();

        let val = manager.mk_bool(true);
        assert_eq!(val.as_bool(), Some(true));
        assert_eq!(manager.stats().values_created, 1);
    }

    #[test]
    fn test_create_int() {
        let mut manager = ValueManager::default_config();

        let val = manager.mk_int(BigInt::from(42));
        assert_eq!(val.as_int(), Some(&BigInt::from(42)));
    }

    #[test]
    fn test_create_bitvector() {
        let mut manager = ValueManager::default_config();

        // Create 8-bit value with value 255
        let val = manager.mk_bitvector(8, BigInt::from(255));

        if let ModelValue::BitVector(width, value) = val {
            assert_eq!(width, 8);
            assert_eq!(value, BigInt::from(255));
        } else {
            panic!("expected bitvector");
        }
    }

    #[test]
    fn test_bitvector_masking() {
        let mut manager = ValueManager::default_config();

        // Create 4-bit value with 255 (should be masked to 15)
        let val = manager.mk_bitvector(4, BigInt::from(255));

        if let ModelValue::BitVector(width, value) = val {
            assert_eq!(width, 4);
            assert_eq!(value, BigInt::from(15)); // 0b1111
        } else {
            panic!("expected bitvector");
        }
    }

    #[test]
    fn test_uninterpreted() {
        let mut manager = ValueManager::default_config();

        let sort = Sort::Uninterpreted("A".to_string());
        let val1 = manager.mk_uninterpreted(sort.clone());
        let val2 = manager.mk_uninterpreted(sort.clone());

        // Should get different IDs
        if let (ModelValue::Uninterpreted(_, id1), ModelValue::Uninterpreted(_, id2)) =
            (&val1, &val2)
        {
            assert_ne!(id1, id2);
        } else {
            panic!("expected uninterpreted values");
        }
    }

    #[test]
    fn test_assign_and_get() {
        let mut manager = ValueManager::default_config();

        let term = TermId::new(0);
        let value = manager.mk_bool(true);

        manager.assign(term, value.clone());

        assert_eq!(manager.get_value(term), Some(&value));
        assert_eq!(manager.num_assignments(), 1);
    }

    #[test]
    fn test_clear() {
        let mut manager = ValueManager::default_config();

        let term = TermId::new(0);
        let value = manager.mk_bool(false);

        manager.assign(term, value);
        assert_eq!(manager.num_assignments(), 1);

        manager.clear();
        assert_eq!(manager.num_assignments(), 0);
    }
}