smtlite 0.2.0

A small, pure-Rust QF_BV SMT solver: bitvector expressions bit-blasted to CNF and solved by a self-contained DPLL core.
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
//! The theory of arrays: `select` and `store`, lowered to bitvectors plus
//! read-over-write lemmas.
//!
//! An array is a total function from an index word to an element word. Two
//! operations matter:
//!
//! - `select(a, i)` is the element stored at `i`;
//! - `store(a, i, v)` is `a` with position `i` overwritten by `v`.
//!
//! Their axiom is *read-over-write*, and it is the whole theory:
//!
//! ```text
//! select(store(a, i, v), j) = (j == i) ? v : select(a, j)
//! ```
//!
//! Together with congruence for the base array - equal indices select equal
//! elements - that is enough to reason about keyed maps and keyed storage
//! without ever deciding a formula about the entire index space.
//!
//! ## Encoding
//!
//! Every `select` term is a fresh `Bv`; the theory records which array and
//! which index term it was taken at. This module's job at model-check time is
//! to instantiate the axiom instance for the one branch the model chose:
//!
//! - when the model makes the stored index and the selected index **equal**,
//!   the select must equal the stored value;
//! - when it makes them **different**, the select must equal the parent array's
//!   select at the same index (which was created when the term was built, so a
//!   chain of stores becomes a chain of already-existing terms).
//!
//! A base (symbolic) array gets the congruence instance instead: two selects at
//! model-equal indices must agree.
//!
//! This is the standard lazy array encoding, and it fits the crate's soundness
//! law exactly: lemmas are monotone, so `Unsat` is a fact the moment it is
//! seen; a `Sat` is returned only after the model survives every instantiation;
//! and the round budget running out is `Unknown`.
//!
//! ## What this replaces
//!
//! A symbolic executor that wants to know "does this store land on cell C?"
//! otherwise keeps a fixed-size list of guarded writes and folds them with a
//! chain of `ite`s - an array theory with its arms cut to a constant. That is
//! sound but runs out after a handful of stores; here the number of stores is
//! bounded by the query, not by the encoding.
//!
//! ## Cost
//!
//! A select through a chain of `k` stores creates `k` terms (one per level) and
//! at validation each level is one comparison plus one implication. The model
//! check is linear in the number of select terms plus, for base arrays, the
//! pairwise congruence scan. Nothing is quadratic in the array's *index space*:
//! the index width may be 128 and the encoding never enumerates it.

use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt;
use std::rc::Rc;

use crate::Solver;
use crate::bv::Bv;
use crate::theory::{ModelView, Theory};

/// One array term: a base array or a store over another array term.
#[derive(Clone)]
enum Node {
    /// A symbolic array, or a constant array (every index selects `constant`).
    Base {
        constant: Option<Bv>,
        index_width: u32,
        elem_width: u32,
    },
    /// `parent` with `index` overwritten by `value`.
    Store { parent: usize, index: Bv, value: Bv },
}

/// A distinct `select` term: the array it reads, the index it reads at, the
/// fresh result, and - for a store - the parent array's select at that same
/// index (the read-over-write fallback).
struct Select {
    array: usize,
    index: Bv,
    result: Bv,
    parent: Option<Bv>,
}

struct Inner {
    nodes: RefCell<Vec<Node>>,
    /// `(array id, index node address) -> select record`, so rebuilding the same
    /// term returns the same result instead of a second independent cell.
    by_key: RefCell<HashMap<(usize, usize), usize>>,
    /// `(parent id, index address, value address) -> store id`, so two identical
    /// stores are one array term.
    store_key: RefCell<HashMap<(usize, usize, usize), usize>>,
    selects: RefCell<Vec<Select>>,
}

impl fmt::Debug for Inner {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ArrayTheory")
            .field("arrays", &self.nodes.borrow().len())
            .field("selects", &self.selects.borrow().len())
            .finish()
    }
}

impl Inner {
    fn base(&self, constant: Option<Bv>, index_width: u32, elem_width: u32) -> usize {
        let mut nodes = self.nodes.borrow_mut();
        nodes.push(Node::Base {
            constant,
            index_width,
            elem_width,
        });
        nodes.len() - 1
    }

    fn push_select(&self, array: usize, index: Bv, result: Bv, parent: Option<Bv>) -> usize {
        let key = (array, index.ptr_key());
        let i = {
            let mut selects = self.selects.borrow_mut();
            selects.push(Select {
                array,
                index,
                result,
                parent,
            });
            selects.len() - 1
        };
        self.by_key.borrow_mut().insert(key, i);
        i
    }
}

/// A handle on the array theory's term log. Clones share it, so the handle used
/// to build terms and the one registered with the solver are the same theory.
///
/// One theory serves every array in a solver; each array declares its own index
/// and element widths, so arrays of different shapes coexist.
///
/// ```
/// use smtlite::{ArrayTheory, Bv, Solution, Solver};
///
/// let mut s = Solver::new();
/// let arrays = ArrayTheory::new();
/// let a = arrays.var(&mut s, 8, 8);
/// let i = s.var("i", 8);
/// let v = s.var("v", 8);
/// let b = a.store(&mut s, &i, &v);
/// // Reading back the cell just written is the written value, by construction.
/// let read = b.select(&mut s, &i);
/// s.assert(read.ne(&v));
/// s.register_theory(arrays.clone());
/// assert!(matches!(s.check_theories(200_000, None), Solution::Unsat));
/// ```
#[derive(Clone, Debug)]
pub struct ArrayTheory {
    inner: Rc<Inner>,
}

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

impl ArrayTheory {
    /// A fresh array theory with no arrays.
    #[must_use]
    pub fn new() -> Self {
        Self {
            inner: Rc::new(Inner {
                nodes: RefCell::new(Vec::new()),
                by_key: RefCell::new(HashMap::new()),
                store_key: RefCell::new(HashMap::new()),
                selects: RefCell::new(Vec::new()),
            }),
        }
    }

    /// A symbolic array `index_width -> elem_width`.
    ///
    /// # Panics
    ///
    /// Panics unless both widths are 1..=128.
    #[must_use]
    pub fn var(&self, _solver: &mut Solver, index_width: u32, elem_width: u32) -> Array {
        assert!(
            (1..=128).contains(&index_width),
            "array index width must be 1..=128, got {index_width}"
        );
        assert!(
            (1..=128).contains(&elem_width),
            "array element width must be 1..=128, got {elem_width}"
        );
        let id = self.inner.base(None, index_width, elem_width);
        Array {
            inner: Rc::clone(&self.inner),
            id,
        }
    }

    /// The constant array: every index selects `element`.
    ///
    /// `index_width` only fixes the width a later [`store`](Array::store) must
    /// use; the elements never depend on it.
    #[must_use]
    pub fn constant(&self, element: &Bv, index_width: u32) -> Array {
        assert!(
            (1..=128).contains(&index_width),
            "array index width must be 1..=128, got {index_width}"
        );
        let id = self
            .inner
            .base(Some(element.clone()), index_width, element.width());
        Array {
            inner: Rc::clone(&self.inner),
            id,
        }
    }

    /// The number of array terms created (bases plus stores).
    #[must_use]
    pub fn arrays(&self) -> usize {
        self.inner.nodes.borrow().len()
    }

    /// The number of distinct `select` terms created.
    #[must_use]
    pub fn selects(&self) -> usize {
        self.inner.selects.borrow().len()
    }
}

/// An array term of a fixed shape. Created by [`ArrayTheory`].
#[derive(Clone, Debug)]
pub struct Array {
    inner: Rc<Inner>,
    id: usize,
}

impl Array {
    /// The index and element widths, `(index, element)`.
    ///
    /// # Panics
    ///
    /// Panics if the internal id does not name an array - unreachable, because
    /// ids only come from this module.
    #[must_use]
    pub fn shape(&self) -> (u32, u32) {
        match &self.inner.nodes.borrow()[self.id] {
            Node::Base {
                index_width,
                elem_width,
                ..
            } => (*index_width, *elem_width),
            Node::Store { parent, .. } => {
                let shape = &self.inner.nodes.borrow()[*parent];
                match shape {
                    Node::Base {
                        index_width,
                        elem_width,
                        ..
                    } => (*index_width, *elem_width),
                    // Stores always sit on an array of some shape; walk down
                    // rather than trusting the immediate parent's variant.
                    Node::Store { .. } => Array {
                        inner: Rc::clone(&self.inner),
                        id: *parent,
                    }
                    .shape(),
                }
            }
        }
    }

    /// The element at `index`.
    ///
    /// # Panics
    ///
    /// Panics if `index`'s width differs from the array's index width.
    pub fn select(&self, solver: &mut Solver, index: &Bv) -> Bv {
        let (iw, _) = self.shape();
        assert_eq!(
            index.width(),
            iw,
            "select index must be {iw} bits, got {}",
            index.width()
        );
        self.select_at(solver, self.id, index)
    }

    fn select_at(&self, solver: &mut Solver, array: usize, index: &Bv) -> Bv {
        // Same array term and same index term: one cell, one result.
        let key = (array, index.ptr_key());
        if let Some(&i) = self.inner.by_key.borrow().get(&key) {
            return self.inner.selects.borrow()[i].result.clone();
        }
        match self.inner.nodes.borrow()[array].clone() {
            Node::Base {
                constant: Some(c), ..
            } => c,
            Node::Base {
                constant: None,
                elem_width,
                ..
            } => {
                let n = self.inner.selects.borrow().len();
                let result = solver.var(&format!("arr#{array}[{n}]"), elem_width);
                self.inner
                    .push_select(array, index.clone(), result.clone(), None);
                result
            }
            Node::Store {
                parent,
                index: stored,
                value,
            } => {
                // The syntactic fast path: the same term is the same cell.
                if stored.ptr_eq(index) {
                    return value;
                }
                // Build the fallback term now, so the lemma instantiated
                // later names a term the solver already knows.
                let parent_sel = self.select_at(solver, parent, index);
                let n = self.inner.selects.borrow().len();
                let result = solver.var(&format!("arr#{array}[{n}]"), value.width());
                self.inner
                    .push_select(array, index.clone(), result.clone(), Some(parent_sel));
                result
            }
        }
    }

    /// `self` with `index` overwritten by `value`.
    ///
    /// # Panics
    ///
    /// Panics if either width differs from the array's shape.
    pub fn store(&self, _solver: &mut Solver, index: &Bv, value: &Bv) -> Array {
        let (iw, ew) = self.shape();
        assert_eq!(
            index.width(),
            iw,
            "store index must be {iw} bits, got {}",
            index.width()
        );
        assert_eq!(
            value.width(),
            ew,
            "stored value must be {ew} bits, got {}",
            value.width()
        );
        let key = (self.id, index.ptr_key(), value.ptr_key());
        if let Some(&id) = self.inner.store_key.borrow().get(&key) {
            return Array {
                inner: Rc::clone(&self.inner),
                id,
            };
        }
        let id = {
            let mut nodes = self.inner.nodes.borrow_mut();
            nodes.push(Node::Store {
                parent: self.id,
                index: index.clone(),
                value: value.clone(),
            });
            nodes.len() - 1
        };
        self.inner.store_key.borrow_mut().insert(key, id);
        Array {
            inner: Rc::clone(&self.inner),
            id,
        }
    }
}

impl Theory for ArrayTheory {
    fn name(&self) -> &'static str {
        "arrays"
    }

    fn violated_lemmas(&self, model: &ModelView<'_>) -> Vec<Bv> {
        let nodes = self.inner.nodes.borrow();
        let selects = self.inner.selects.borrow();
        let mut out = Vec::new();

        // Read-over-write: instantiate the branch the model chose.
        for s in selects.iter() {
            let Node::Store {
                index: stored,
                value,
                ..
            } = &nodes[s.array]
            else {
                continue;
            };
            let (Some(sv), Some(iv)) = (model.eval128(stored), model.eval128(&s.index)) else {
                continue;
            };
            if sv == iv {
                if model.eval128(&s.result) != model.eval128(value) {
                    out.push(stored.eq(&s.index).implies(&s.result.eq(value)));
                }
            } else if let Some(parent) = &s.parent {
                if model.eval128(&s.result) != model.eval128(parent) {
                    out.push(stored.ne(&s.index).implies(&s.result.eq(parent)));
                }
            }
        }

        // Congruence at a base array: equal indices select equal elements.
        for i in 0..selects.len() {
            if !matches!(nodes[selects[i].array], Node::Base { constant: None, .. }) {
                continue;
            }
            for j in (i + 1)..selects.len() {
                if selects[j].array != selects[i].array {
                    continue;
                }
                let (Some(a), Some(b)) = (
                    model.eval128(&selects[i].index),
                    model.eval128(&selects[j].index),
                ) else {
                    continue;
                };
                if a != b {
                    continue;
                }
                let results_differ = match (
                    model.eval128(&selects[i].result),
                    model.eval128(&selects[j].result),
                ) {
                    (Some(x), Some(y)) => x != y,
                    _ => false,
                };
                if results_differ {
                    out.push(
                        selects[i]
                            .index
                            .eq(&selects[j].index)
                            .implies(&selects[i].result.eq(&selects[j].result)),
                    );
                }
            }
        }
        out
    }

    fn eager_lemmas(&self) -> Vec<Bv> {
        let nodes = self.inner.nodes.borrow();
        let selects = self.inner.selects.borrow();
        let mut out = Vec::new();
        for s in selects.iter() {
            match &nodes[s.array] {
                Node::Store {
                    index: stored,
                    value,
                    ..
                } => {
                    // Both branches of read-over-write, always: the disjunction
                    // is exhaustive, so emitting both is the standard eager
                    // encoding and cannot exclude a real model.
                    out.push(stored.eq(&s.index).implies(&s.result.eq(value)));
                    if let Some(parent) = &s.parent {
                        out.push(stored.ne(&s.index).implies(&s.result.eq(parent)));
                    }
                }
                Node::Base { .. } => {}
            }
        }
        for i in 0..selects.len() {
            if !matches!(nodes[selects[i].array], Node::Base { constant: None, .. }) {
                continue;
            }
            for j in (i + 1)..selects.len() {
                if selects[j].array != selects[i].array {
                    continue;
                }
                out.push(
                    selects[i]
                        .index
                        .eq(&selects[j].index)
                        .implies(&selects[i].result.eq(&selects[j].result)),
                );
            }
        }
        out
    }
}