cvc5-rs 0.3.1

High-level Rust bindings for the cvc5 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
use cvc5_sys::*;
use std::fmt;

use crate::Datatype;

/// A cvc5 sort (type).
///
/// Sorts represent types in the SMT solver — Boolean, Integer, Real,
/// BitVector, Array, Datatype, etc.
pub struct Sort {
    pub(crate) inner: Cvc5Sort,
}

impl Clone for Sort {
    fn clone(&self) -> Self {
        Self {
            inner: unsafe { cvc5_sort_copy(self.inner) },
        }
    }
}

impl Drop for Sort {
    fn drop(&mut self) {
        unsafe { cvc5_sort_release(self.inner) }
    }
}

impl Sort {
    pub(crate) fn from_raw(raw: Cvc5Sort) -> Self {
        Self { inner: raw }
    }

    /// Get the kind of this sort.
    pub fn kind(&self) -> Cvc5SortKind {
        unsafe { cvc5_sort_get_kind(self.inner) }
    }

    /// Create a copy of this sort (increments the internal reference count).
    pub fn copy(&self) -> Sort {
        Sort::from_raw(unsafe { cvc5_sort_copy(self.inner) })
    }

    /// Check disequality with another sort.
    pub fn is_disequal(&self, other: &Sort) -> bool {
        unsafe { cvc5_sort_is_disequal(self.inner, other.inner) }
    }

    /// Return `true` if this sort has a symbol (name).
    pub fn has_symbol(&self) -> bool {
        unsafe { cvc5_sort_has_symbol(self.inner) }
    }

    /// Get the symbol (name) of this sort.
    pub fn symbol(&self) -> &str {
        unsafe {
            let s = cvc5_sort_get_symbol(self.inner);
            std::ffi::CStr::from_ptr(s).to_str().unwrap_or("")
        }
    }

    /// Return `true` if this is the Boolean sort.
    pub fn is_boolean(&self) -> bool {
        unsafe { cvc5_sort_is_boolean(self.inner) }
    }
    /// Return `true` if this is the Integer sort.
    pub fn is_integer(&self) -> bool {
        unsafe { cvc5_sort_is_integer(self.inner) }
    }
    /// Return `true` if this is the Real sort.
    pub fn is_real(&self) -> bool {
        unsafe { cvc5_sort_is_real(self.inner) }
    }
    /// Return `true` if this is the String sort.
    pub fn is_string(&self) -> bool {
        unsafe { cvc5_sort_is_string(self.inner) }
    }
    /// Return `true` if this is the RegExp sort.
    pub fn is_regexp(&self) -> bool {
        unsafe { cvc5_sort_is_regexp(self.inner) }
    }
    /// Return `true` if this is the rounding mode sort.
    pub fn is_rm(&self) -> bool {
        unsafe { cvc5_sort_is_rm(self.inner) }
    }
    /// Return `true` if this is a bit-vector sort.
    pub fn is_bv(&self) -> bool {
        unsafe { cvc5_sort_is_bv(self.inner) }
    }
    /// Return `true` if this is a floating-point sort.
    pub fn is_fp(&self) -> bool {
        unsafe { cvc5_sort_is_fp(self.inner) }
    }
    /// Return `true` if this is a datatype sort.
    pub fn is_dt(&self) -> bool {
        unsafe { cvc5_sort_is_dt(self.inner) }
    }
    /// Return `true` if this is a datatype constructor sort.
    pub fn is_dt_constructor(&self) -> bool {
        unsafe { cvc5_sort_is_dt_constructor(self.inner) }
    }
    /// Return `true` if this is a datatype selector sort.
    pub fn is_dt_selector(&self) -> bool {
        unsafe { cvc5_sort_is_dt_selector(self.inner) }
    }
    /// Return `true` if this is a datatype tester sort.
    pub fn is_dt_tester(&self) -> bool {
        unsafe { cvc5_sort_is_dt_tester(self.inner) }
    }
    /// Return `true` if this is a datatype updater sort.
    pub fn is_dt_updater(&self) -> bool {
        unsafe { cvc5_sort_is_dt_updater(self.inner) }
    }
    /// Return `true` if this is a function sort.
    pub fn is_fun(&self) -> bool {
        unsafe { cvc5_sort_is_fun(self.inner) }
    }
    /// Return `true` if this is a predicate sort.
    pub fn is_predicate(&self) -> bool {
        unsafe { cvc5_sort_is_predicate(self.inner) }
    }
    /// Return `true` if this is a tuple sort.
    pub fn is_tuple(&self) -> bool {
        unsafe { cvc5_sort_is_tuple(self.inner) }
    }
    /// Return `true` if this is a nullable sort.
    pub fn is_nullable(&self) -> bool {
        unsafe { cvc5_sort_is_nullable(self.inner) }
    }
    /// Return `true` if this is a record sort.
    pub fn is_record(&self) -> bool {
        unsafe { cvc5_sort_is_record(self.inner) }
    }
    /// Return `true` if this is an array sort.
    pub fn is_array(&self) -> bool {
        unsafe { cvc5_sort_is_array(self.inner) }
    }
    /// Return `true` if this is a finite field sort.
    pub fn is_ff(&self) -> bool {
        unsafe { cvc5_sort_is_ff(self.inner) }
    }
    /// Return `true` if this is a set sort.
    pub fn is_set(&self) -> bool {
        unsafe { cvc5_sort_is_set(self.inner) }
    }
    /// Return `true` if this is a bag sort.
    pub fn is_bag(&self) -> bool {
        unsafe { cvc5_sort_is_bag(self.inner) }
    }
    /// Return `true` if this is a sequence sort.
    pub fn is_sequence(&self) -> bool {
        unsafe { cvc5_sort_is_sequence(self.inner) }
    }
    /// Return `true` if this is an abstract sort.
    pub fn is_abstract(&self) -> bool {
        unsafe { cvc5_sort_is_abstract(self.inner) }
    }
    /// Return `true` if this is an uninterpreted sort.
    pub fn is_uninterpreted_sort(&self) -> bool {
        unsafe { cvc5_sort_is_uninterpreted_sort(self.inner) }
    }
    /// Return `true` if this is an uninterpreted sort constructor.
    pub fn is_uninterpreted_sort_constructor(&self) -> bool {
        unsafe { cvc5_sort_is_uninterpreted_sort_constructor(self.inner) }
    }
    /// Return `true` if this sort is an instantiated (parametric) sort.
    pub fn is_instantiated(&self) -> bool {
        unsafe { cvc5_sort_is_instantiated(self.inner) }
    }

    /// Get the associated uninterpreted sort constructor of an instantiated sort.
    pub fn uninterpreted_sort_constructor(&self) -> Sort {
        Sort::from_raw(unsafe { cvc5_sort_get_uninterpreted_sort_constructor(self.inner) })
    }

    /// Get the datatype associated with a datatype sort.
    pub fn datatype(&self) -> Datatype {
        Datatype::from_raw(unsafe { cvc5_sort_get_datatype(self.inner) })
    }

    /// Instantiate a parametric sort with the given sort parameters.
    pub fn instantiate(&self, params: &[Sort]) -> Sort {
        let raw: Vec<Cvc5Sort> = params.iter().map(|s| s.inner).collect();
        Sort::from_raw(unsafe { cvc5_sort_instantiate(self.inner, raw.len(), raw.as_ptr()) })
    }

    /// Get the sort parameters of an instantiated sort.
    pub fn instantiated_parameters(&self) -> Vec<Sort> {
        let mut size = 0usize;
        let ptr = unsafe { cvc5_sort_get_instantiated_parameters(self.inner, &mut size) };
        (0..size)
            .map(|i| Sort::from_raw(unsafe { *ptr.add(i) }))
            .collect()
    }

    /// Substitute `s` with `replacement` in this sort.
    pub fn substitute(&self, s: Sort, replacement: Sort) -> Sort {
        Sort::from_raw(unsafe { cvc5_sort_substitute(self.inner, s.inner, replacement.inner) })
    }

    /// Simultaneously substitute `sorts` with `replacements` in this sort.
    pub fn substitute_sorts(&self, sorts: &[Sort], replacements: &[Sort]) -> Sort {
        let s: Vec<Cvc5Sort> = sorts.iter().map(|s| s.inner).collect();
        let r: Vec<Cvc5Sort> = replacements.iter().map(|s| s.inner).collect();
        Sort::from_raw(unsafe {
            cvc5_sort_substitute_sorts(self.inner, s.len(), s.as_ptr(), r.as_ptr())
        })
    }

    // -- Datatype constructor sort accessors --

    /// Get the arity of a datatype constructor sort.
    pub fn dt_constructor_arity(&self) -> usize {
        unsafe { cvc5_sort_dt_constructor_get_arity(self.inner) }
    }

    /// Get the domain sorts of a datatype constructor sort.
    pub fn dt_constructor_domain(&self) -> Vec<Sort> {
        let mut size = 0usize;
        let ptr = unsafe { cvc5_sort_dt_constructor_get_domain(self.inner, &mut size) };
        (0..size)
            .map(|i| Sort::from_raw(unsafe { *ptr.add(i) }))
            .collect()
    }

    /// Get the codomain sort of a datatype constructor sort.
    pub fn dt_constructor_codomain(&self) -> Sort {
        Sort::from_raw(unsafe { cvc5_sort_dt_constructor_get_codomain(self.inner) })
    }

    // -- Datatype selector sort accessors --

    /// Get the domain sort of a datatype selector sort.
    pub fn dt_selector_domain(&self) -> Sort {
        Sort::from_raw(unsafe { cvc5_sort_dt_selector_get_domain(self.inner) })
    }

    /// Get the codomain sort of a datatype selector sort.
    pub fn dt_selector_codomain(&self) -> Sort {
        Sort::from_raw(unsafe { cvc5_sort_dt_selector_get_codomain(self.inner) })
    }

    // -- Datatype tester sort accessors --

    /// Get the domain sort of a datatype tester sort.
    pub fn dt_tester_domain(&self) -> Sort {
        Sort::from_raw(unsafe { cvc5_sort_dt_tester_get_domain(self.inner) })
    }

    /// Get the codomain sort of a datatype tester sort.
    pub fn dt_tester_codomain(&self) -> Sort {
        Sort::from_raw(unsafe { cvc5_sort_dt_tester_get_codomain(self.inner) })
    }

    // -- Function sort accessors --

    /// Get the arity of a function sort.
    pub fn fun_arity(&self) -> usize {
        unsafe { cvc5_sort_fun_get_arity(self.inner) }
    }

    /// Get the domain sorts of a function sort.
    pub fn fun_domain(&self) -> Vec<Sort> {
        let mut size = 0usize;
        let ptr = unsafe { cvc5_sort_fun_get_domain(self.inner, &mut size) };
        (0..size)
            .map(|i| Sort::from_raw(unsafe { *ptr.add(i) }))
            .collect()
    }

    /// Get the codomain sort of a function sort.
    pub fn fun_codomain(&self) -> Sort {
        Sort::from_raw(unsafe { cvc5_sort_fun_get_codomain(self.inner) })
    }

    // -- Array sort accessors --

    /// Get the index sort of an array sort.
    pub fn array_index_sort(&self) -> Sort {
        Sort::from_raw(unsafe { cvc5_sort_array_get_index_sort(self.inner) })
    }

    /// Get the element sort of an array sort.
    pub fn array_element_sort(&self) -> Sort {
        Sort::from_raw(unsafe { cvc5_sort_array_get_element_sort(self.inner) })
    }

    // -- Set / Bag / Sequence element sort --

    /// Get the element sort of a set sort.
    pub fn set_element_sort(&self) -> Sort {
        Sort::from_raw(unsafe { cvc5_sort_set_get_element_sort(self.inner) })
    }

    /// Get the element sort of a bag sort.
    pub fn bag_element_sort(&self) -> Sort {
        Sort::from_raw(unsafe { cvc5_sort_bag_get_element_sort(self.inner) })
    }

    /// Get the element sort of a sequence sort.
    pub fn sequence_element_sort(&self) -> Sort {
        Sort::from_raw(unsafe { cvc5_sort_sequence_get_element_sort(self.inner) })
    }

    // -- Abstract sort --

    /// Get the kind of an abstract sort.
    pub fn abstract_kind(&self) -> Cvc5SortKind {
        unsafe { cvc5_sort_abstract_get_kind(self.inner) }
    }

    // -- Uninterpreted sort constructor --

    /// Get the arity of an uninterpreted sort constructor.
    pub fn uninterpreted_sort_constructor_arity(&self) -> usize {
        unsafe { cvc5_sort_uninterpreted_sort_constructor_get_arity(self.inner) }
    }

    // -- Bit-vector sort --

    /// Get the bit-width of a bit-vector sort.
    pub fn bv_size(&self) -> u32 {
        unsafe { cvc5_sort_bv_get_size(self.inner) }
    }

    // -- Finite field sort --

    /// Get the size (modulus) of a finite field sort as a string.
    pub fn ff_size(&self) -> String {
        unsafe {
            let s = cvc5_sort_ff_get_size(self.inner);
            std::ffi::CStr::from_ptr(s).to_string_lossy().into_owned()
        }
    }

    // -- Floating-point sort --

    /// Get the exponent size of a floating-point sort.
    pub fn fp_exponent_size(&self) -> u32 {
        unsafe { cvc5_sort_fp_get_exp_size(self.inner) }
    }

    /// Get the significand size of a floating-point sort.
    pub fn fp_significand_size(&self) -> u32 {
        unsafe { cvc5_sort_fp_get_sig_size(self.inner) }
    }

    // -- Datatype sort --

    /// Get the arity of a datatype sort.
    pub fn dt_arity(&self) -> usize {
        unsafe { cvc5_sort_dt_get_arity(self.inner) }
    }

    // -- Tuple sort --

    /// Get the length (number of elements) of a tuple sort.
    pub fn tuple_length(&self) -> usize {
        unsafe { cvc5_sort_tuple_get_length(self.inner) }
    }

    /// Get the element sorts of a tuple sort.
    pub fn tuple_element_sorts(&self) -> Vec<Sort> {
        let mut size = 0usize;
        let ptr = unsafe { cvc5_sort_tuple_get_element_sorts(self.inner, &mut size) };
        (0..size)
            .map(|i| Sort::from_raw(unsafe { *ptr.add(i) }))
            .collect()
    }

    // -- Nullable sort --

    /// Get the element sort of a nullable sort.
    pub fn nullable_element_sort(&self) -> Sort {
        Sort::from_raw(unsafe { cvc5_sort_nullable_get_element_sort(self.inner) })
    }
}

impl fmt::Display for Sort {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = unsafe { cvc5_sort_to_string(self.inner) };
        let cs = unsafe { std::ffi::CStr::from_ptr(s) };
        write!(f, "{}", cs.to_string_lossy())
    }
}

impl fmt::Debug for Sort {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Sort({self})")
    }
}

impl PartialEq for Sort {
    fn eq(&self, other: &Self) -> bool {
        unsafe { cvc5_sort_is_equal(self.inner, other.inner) }
    }
}

impl Eq for Sort {}

impl PartialOrd for Sort {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Sort {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        let c = unsafe { cvc5_sort_compare(self.inner, other.inner) };
        c.cmp(&0)
    }
}

impl std::hash::Hash for Sort {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        unsafe { cvc5_sort_hash(self.inner) }.hash(state);
    }
}