oxiz-proof 0.2.2

Proof generation and checking for OxiZ SMT solver
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
//! Premise tracking for proof generation during solving.
//!
//! This module provides infrastructure to track which premises (axioms, assertions)
//! are used in each step of the solving process, enabling precise proof generation.

use rustc_hash::{FxHashMap, FxHashSet};
use std::fmt;

/// Unique identifier for a premise (assertion, axiom, or assumption).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PremiseId(pub u32);

impl fmt::Display for PremiseId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "p{}", self.0)
    }
}

/// A premise (assertion or assumption) in the original problem.
#[derive(Debug, Clone)]
pub struct Premise {
    /// Unique identifier.
    pub id: PremiseId,
    /// String representation of the premise (SMT-LIB term).
    pub term: String,
    /// Whether this premise is an assumption (vs assertion).
    pub is_assumption: bool,
}

/// Tracks premise dependencies during solving.
#[derive(Debug, Clone)]
pub struct PremiseTracker {
    /// All registered premises.
    premises: Vec<Premise>,
    /// Next available premise ID.
    next_id: u32,
    /// Map from terms to premise IDs (for deduplication).
    term_to_id: FxHashMap<String, PremiseId>,
    /// Active premises in the current scope.
    active_premises: FxHashSet<PremiseId>,
}

impl PremiseTracker {
    /// Create a new premise tracker.
    #[must_use]
    pub fn new() -> Self {
        Self {
            premises: Vec::new(),
            next_id: 0,
            term_to_id: FxHashMap::default(),
            active_premises: FxHashSet::default(),
        }
    }

    /// Register a new premise (assertion or assumption).
    ///
    /// Returns the premise ID. If the term was already registered,
    /// returns the existing ID.
    pub fn add_premise(&mut self, term: impl Into<String>, is_assumption: bool) -> PremiseId {
        let term = term.into();

        // Check if already registered
        if let Some(&id) = self.term_to_id.get(&term) {
            return id;
        }

        let id = PremiseId(self.next_id);
        self.next_id += 1;

        self.premises.push(Premise {
            id,
            term: term.clone(),
            is_assumption,
        });

        self.term_to_id.insert(term, id);
        self.active_premises.insert(id);

        id
    }

    /// Register an assertion.
    pub fn add_assertion(&mut self, term: impl Into<String>) -> PremiseId {
        self.add_premise(term, false)
    }

    /// Register an assumption (for incremental solving).
    pub fn add_assumption(&mut self, term: impl Into<String>) -> PremiseId {
        self.add_premise(term, true)
    }

    /// Get a premise by ID.
    #[must_use]
    pub fn get_premise(&self, id: PremiseId) -> Option<&Premise> {
        self.premises.get(id.0 as usize)
    }

    /// Get the premise ID for a term, if it exists.
    #[must_use]
    pub fn get_id(&self, term: &str) -> Option<PremiseId> {
        self.term_to_id.get(term).copied()
    }

    /// Check if a premise is active.
    #[must_use]
    pub fn is_active(&self, id: PremiseId) -> bool {
        self.active_premises.contains(&id)
    }

    /// Activate a premise.
    pub fn activate(&mut self, id: PremiseId) {
        self.active_premises.insert(id);
    }

    /// Deactivate a premise (e.g., when popping scope).
    pub fn deactivate(&mut self, id: PremiseId) {
        self.active_premises.remove(&id);
    }

    /// Get all active premises.
    pub fn active_premises(&self) -> impl Iterator<Item = PremiseId> + '_ {
        self.active_premises.iter().copied()
    }

    /// Get all premises.
    #[must_use]
    pub fn all_premises(&self) -> &[Premise] {
        &self.premises
    }

    /// Get the number of premises.
    #[must_use]
    pub fn len(&self) -> usize {
        self.premises.len()
    }

    /// Check if there are no premises.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.premises.is_empty()
    }

    /// Clear all premises.
    pub fn clear(&mut self) {
        self.premises.clear();
        self.next_id = 0;
        self.term_to_id.clear();
        self.active_premises.clear();
    }

    /// Push a new scope level.
    ///
    /// Returns the current set of active premises (for restoration on pop).
    #[must_use]
    pub fn push_scope(&self) -> FxHashSet<PremiseId> {
        self.active_premises.clone()
    }

    /// Pop a scope level, restoring the given set of active premises.
    pub fn pop_scope(&mut self, saved_premises: FxHashSet<PremiseId>) {
        self.active_premises = saved_premises;
    }
}

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

/// Tracks premise dependencies for derived clauses/terms.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct PremiseDependency {
    /// The set of premises that this clause/term depends on.
    dependencies: FxHashMap<u32, FxHashSet<PremiseId>>,
    /// Next available clause/term ID.
    next_id: u32,
}

#[allow(dead_code)]
impl PremiseDependency {
    /// Create a new premise dependency tracker.
    #[must_use]
    pub fn new() -> Self {
        Self {
            dependencies: FxHashMap::default(),
            next_id: 0,
        }
    }

    /// Register a derived clause/term with its premise dependencies.
    ///
    /// Returns a unique ID for this derived item.
    pub fn add_derived(&mut self, premises: impl IntoIterator<Item = PremiseId>) -> u32 {
        let id = self.next_id;
        self.next_id += 1;

        let premise_set: FxHashSet<PremiseId> = premises.into_iter().collect();
        self.dependencies.insert(id, premise_set);

        id
    }

    /// Get the premises that a derived item depends on.
    #[must_use]
    pub fn get_dependencies(&self, id: u32) -> Option<&FxHashSet<PremiseId>> {
        self.dependencies.get(&id)
    }

    /// Merge dependencies from multiple derived items.
    ///
    /// Returns the union of all their premise dependencies.
    #[must_use]
    pub fn merge_dependencies(&self, ids: &[u32]) -> FxHashSet<PremiseId> {
        let mut merged = FxHashSet::default();
        for &id in ids {
            if let Some(deps) = self.dependencies.get(&id) {
                merged.extend(deps);
            }
        }
        merged
    }

    /// Clear all dependency information.
    pub fn clear(&mut self) {
        self.dependencies.clear();
        self.next_id = 0;
    }

    /// Get the number of tracked derived items.
    #[must_use]
    pub fn len(&self) -> usize {
        self.dependencies.len()
    }

    /// Check if there are no tracked dependencies.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.dependencies.is_empty()
    }
}

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

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

    #[test]
    fn test_premise_tracker_basic() {
        let mut tracker = PremiseTracker::new();

        let p1 = tracker.add_assertion("(> x 5)");
        let p2 = tracker.add_assertion("(< x 10)");
        let p3 = tracker.add_assumption("(= y 7)");

        assert_eq!(tracker.len(), 3);
        assert!(tracker.is_active(p1));
        assert!(tracker.is_active(p2));
        assert!(tracker.is_active(p3));

        let premise = tracker
            .get_premise(p1)
            .expect("test operation should succeed");
        assert_eq!(premise.term, "(> x 5)");
        assert!(!premise.is_assumption);

        let assumption = tracker
            .get_premise(p3)
            .expect("test operation should succeed");
        assert!(assumption.is_assumption);
    }

    #[test]
    fn test_premise_deduplication() {
        let mut tracker = PremiseTracker::new();

        let p1 = tracker.add_assertion("(> x 5)");
        let p2 = tracker.add_assertion("(> x 5)");

        assert_eq!(p1, p2);
        assert_eq!(tracker.len(), 1);
    }

    #[test]
    fn test_premise_activation() {
        let mut tracker = PremiseTracker::new();

        let p1 = tracker.add_assertion("(> x 5)");
        assert!(tracker.is_active(p1));

        tracker.deactivate(p1);
        assert!(!tracker.is_active(p1));

        tracker.activate(p1);
        assert!(tracker.is_active(p1));
    }

    #[test]
    fn test_premise_scope() {
        let mut tracker = PremiseTracker::new();

        let p1 = tracker.add_assertion("(> x 5)");
        let saved = tracker.push_scope();

        let p2 = tracker.add_assertion("(< x 10)");
        assert!(tracker.is_active(p1));
        assert!(tracker.is_active(p2));

        tracker.pop_scope(saved);
        assert!(tracker.is_active(p1));
        // p2 is still in the tracker but we restored the active set
        assert!(!tracker.is_active(p2));
    }

    #[test]
    fn test_premise_get_id() {
        let mut tracker = PremiseTracker::new();

        let p1 = tracker.add_assertion("(> x 5)");
        let found = tracker.get_id("(> x 5)");

        assert_eq!(found, Some(p1));
        assert_eq!(tracker.get_id("(< x 10)"), None);
    }

    #[test]
    fn test_premise_clear() {
        let mut tracker = PremiseTracker::new();

        tracker.add_assertion("(> x 5)");
        tracker.add_assertion("(< x 10)");

        assert_eq!(tracker.len(), 2);
        tracker.clear();
        assert_eq!(tracker.len(), 0);
        assert!(tracker.is_empty());
    }

    #[test]
    fn test_premise_dependency_basic() {
        let mut dep = PremiseDependency::new();

        let p1 = PremiseId(0);
        let p2 = PremiseId(1);

        let d1 = dep.add_derived(vec![p1, p2]);

        let deps = dep
            .get_dependencies(d1)
            .expect("test operation should succeed");
        assert_eq!(deps.len(), 2);
        assert!(deps.contains(&p1));
        assert!(deps.contains(&p2));
    }

    #[test]
    fn test_premise_dependency_merge() {
        let mut dep = PremiseDependency::new();

        let p1 = PremiseId(0);
        let p2 = PremiseId(1);
        let p3 = PremiseId(2);

        let d1 = dep.add_derived(vec![p1, p2]);
        let d2 = dep.add_derived(vec![p2, p3]);

        let merged = dep.merge_dependencies(&[d1, d2]);
        assert_eq!(merged.len(), 3);
        assert!(merged.contains(&p1));
        assert!(merged.contains(&p2));
        assert!(merged.contains(&p3));
    }

    #[test]
    fn test_premise_dependency_empty() {
        let dep = PremiseDependency::new();

        let merged = dep.merge_dependencies(&[0, 1, 2]);
        assert!(merged.is_empty());
    }

    #[test]
    fn test_premise_dependency_clear() {
        let mut dep = PremiseDependency::new();

        dep.add_derived(vec![PremiseId(0)]);
        dep.add_derived(vec![PremiseId(1)]);

        assert_eq!(dep.len(), 2);
        dep.clear();
        assert_eq!(dep.len(), 0);
        assert!(dep.is_empty());
    }

    #[test]
    fn test_premise_id_display() {
        let id = PremiseId(42);
        assert_eq!(format!("{}", id), "p42");
    }

    #[test]
    fn test_premise_id_ordering() {
        let p1 = PremiseId(1);
        let p2 = PremiseId(2);
        let p3 = PremiseId(3);

        assert!(p1 < p2);
        assert!(p2 < p3);
        assert!(p1 < p3);
    }

    #[test]
    fn test_active_premises_iterator() {
        let mut tracker = PremiseTracker::new();

        let p1 = tracker.add_assertion("(> x 5)");
        let p2 = tracker.add_assertion("(< x 10)");
        let _p3 = tracker.add_assertion("(= y 7)");

        tracker.deactivate(p2);

        let active: Vec<PremiseId> = tracker.active_premises().collect();
        assert_eq!(active.len(), 2);
        assert!(active.contains(&p1));
        assert!(!active.contains(&p2));
    }
}