cvc5-rs 0.3.0

High-level Rust bindings for the cvc5 SMT solver
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
492
493
494
use cvc5_sys::*;
use std::ffi::CString;
use std::fmt;

use crate::{Sort, Term};

// ---------------------------------------------------------------------------
// DatatypeConstructorDecl
// ---------------------------------------------------------------------------

/// A declaration for a datatype constructor (before the datatype is resolved).
pub struct DatatypeConstructorDecl {
    pub(crate) inner: Cvc5DatatypeConstructorDecl,
}

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

impl Drop for DatatypeConstructorDecl {
    fn drop(&mut self) {
        unsafe { cvc5_dt_cons_decl_release(self.inner) }
    }
}

impl DatatypeConstructorDecl {
    pub(crate) fn from_raw(raw: Cvc5DatatypeConstructorDecl) -> Self {
        Self { inner: raw }
    }

    /// Add a selector with the given name and codomain sort.
    pub fn add_selector(&mut self, name: &str, sort: Sort) {
        let c = CString::new(name).unwrap();
        unsafe { cvc5_dt_cons_decl_add_selector(self.inner, c.as_ptr(), sort.inner) }
    }

    /// Add a selector whose codomain is the datatype itself (self-reference).
    pub fn add_selector_self(&mut self, name: &str) {
        let c = CString::new(name).unwrap();
        unsafe { cvc5_dt_cons_decl_add_selector_self(self.inner, c.as_ptr()) }
    }

    /// Add a selector whose codomain is an unresolved datatype with the given name.
    pub fn add_selector_unresolved(&mut self, name: &str, unres_name: &str) {
        let n = CString::new(name).unwrap();
        let u = CString::new(unres_name).unwrap();
        unsafe { cvc5_dt_cons_decl_add_selector_unresolved(self.inner, n.as_ptr(), u.as_ptr()) }
    }
}

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

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

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

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

// ---------------------------------------------------------------------------
// DatatypeDecl
// ---------------------------------------------------------------------------

/// A declaration for a datatype (before it is resolved into a sort).
pub struct DatatypeDecl {
    pub(crate) inner: Cvc5DatatypeDecl,
}

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

impl Drop for DatatypeDecl {
    fn drop(&mut self) {
        unsafe { cvc5_dt_decl_release(self.inner) }
    }
}

impl DatatypeDecl {
    pub(crate) fn from_raw(raw: Cvc5DatatypeDecl) -> Self {
        Self { inner: raw }
    }

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

    /// Add a constructor declaration to this datatype.
    pub fn add_constructor(&mut self, ctor: &DatatypeConstructorDecl) {
        unsafe { cvc5_dt_decl_add_constructor(self.inner, ctor.inner) }
    }

    /// Get the number of constructors in this declaration.
    pub fn num_constructors(&self) -> usize {
        unsafe { cvc5_dt_decl_get_num_constructors(self.inner) }
    }

    /// Return `true` if this datatype declaration is parametric.
    pub fn is_parametric(&self) -> bool {
        unsafe { cvc5_dt_decl_is_parametric(self.inner) }
    }

    /// Return `true` if this datatype declaration has been resolved.
    pub fn is_resolved(&self) -> bool {
        unsafe { cvc5_dt_decl_is_resolved(self.inner) }
    }

    /// Get the name of this datatype declaration.
    pub fn name(&self) -> &str {
        unsafe {
            std::ffi::CStr::from_ptr(cvc5_dt_decl_get_name(self.inner))
                .to_str()
                .unwrap_or("")
        }
    }
}

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

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

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

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

// ---------------------------------------------------------------------------
// DatatypeSelector
// ---------------------------------------------------------------------------

/// A selector of a resolved datatype constructor.
pub struct DatatypeSelector {
    pub(crate) inner: Cvc5DatatypeSelector,
}

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

impl Drop for DatatypeSelector {
    fn drop(&mut self) {
        unsafe { cvc5_dt_sel_release(self.inner) }
    }
}

impl DatatypeSelector {
    pub(crate) fn from_raw(raw: Cvc5DatatypeSelector) -> Self {
        Self { inner: raw }
    }

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

    /// Get the name of this selector.
    pub fn name(&self) -> &str {
        unsafe {
            std::ffi::CStr::from_ptr(cvc5_dt_sel_get_name(self.inner))
                .to_str()
                .unwrap_or("")
        }
    }

    /// Get the selector function as a term.
    pub fn term(&self) -> Term {
        Term::from_raw(unsafe { cvc5_dt_sel_get_term(self.inner) })
    }

    /// Get the updater function as a term.
    pub fn updater_term(&self) -> Term {
        Term::from_raw(unsafe { cvc5_dt_sel_get_updater_term(self.inner) })
    }

    /// Get the codomain (return) sort of this selector.
    pub fn codomain_sort(&self) -> Sort {
        Sort::from_raw(unsafe { cvc5_dt_sel_get_codomain_sort(self.inner) })
    }
}

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

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

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

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

// ---------------------------------------------------------------------------
// DatatypeConstructor
// ---------------------------------------------------------------------------

/// A constructor of a resolved datatype.
pub struct DatatypeConstructor {
    pub(crate) inner: Cvc5DatatypeConstructor,
}

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

impl Drop for DatatypeConstructor {
    fn drop(&mut self) {
        unsafe { cvc5_dt_cons_release(self.inner) }
    }
}

impl DatatypeConstructor {
    pub(crate) fn from_raw(raw: Cvc5DatatypeConstructor) -> Self {
        Self { inner: raw }
    }

    /// Get the name of this constructor.
    pub fn name(&self) -> &str {
        unsafe {
            std::ffi::CStr::from_ptr(cvc5_dt_cons_get_name(self.inner))
                .to_str()
                .unwrap_or("")
        }
    }

    /// Get the constructor function as a term.
    pub fn term(&self) -> Term {
        Term::from_raw(unsafe { cvc5_dt_cons_get_term(self.inner) })
    }

    /// Get the constructor term instantiated for the given parametric datatype sort.
    pub fn instantiated_term(&self, sort: Sort) -> Term {
        Term::from_raw(unsafe { cvc5_dt_cons_get_instantiated_term(self.inner, sort.inner) })
    }

    /// Get the tester (discriminator) function as a term.
    pub fn tester_term(&self) -> Term {
        Term::from_raw(unsafe { cvc5_dt_cons_get_tester_term(self.inner) })
    }

    /// Get the number of selectors of this constructor.
    pub fn num_selectors(&self) -> usize {
        unsafe { cvc5_dt_cons_get_num_selectors(self.inner) }
    }

    /// Get the selector at the given index.
    pub fn selector(&self, index: usize) -> DatatypeSelector {
        DatatypeSelector::from_raw(unsafe { cvc5_dt_cons_get_selector(self.inner, index) })
    }

    /// Get a selector by name.
    pub fn selector_by_name(&self, name: &str) -> DatatypeSelector {
        let c = CString::new(name).unwrap();
        DatatypeSelector::from_raw(unsafe {
            cvc5_dt_cons_get_selector_by_name(self.inner, c.as_ptr())
        })
    }
}

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

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

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

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

// ---------------------------------------------------------------------------
// Datatype
// ---------------------------------------------------------------------------

/// A resolved datatype.
///
/// Obtained from a sort via [`Sort::datatype`] after the datatype has been
/// created with [`TermManager::mk_dt_sort`](crate::TermManager::mk_dt_sort).
pub struct Datatype {
    pub(crate) inner: Cvc5Datatype,
}

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

impl Drop for Datatype {
    fn drop(&mut self) {
        unsafe { cvc5_dt_release(self.inner) }
    }
}

impl Datatype {
    pub(crate) fn from_raw(raw: Cvc5Datatype) -> Self {
        Self { inner: raw }
    }

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

    /// Get the constructor at the given index.
    pub fn constructor(&self, index: usize) -> DatatypeConstructor {
        DatatypeConstructor::from_raw(unsafe { cvc5_dt_get_constructor(self.inner, index) })
    }

    /// Get a constructor by name.
    pub fn constructor_by_name(&self, name: &str) -> DatatypeConstructor {
        let c = CString::new(name).unwrap();
        DatatypeConstructor::from_raw(unsafe {
            cvc5_dt_get_constructor_by_name(self.inner, c.as_ptr())
        })
    }

    /// Get a selector by name (searches all constructors).
    pub fn selector(&self, name: &str) -> DatatypeSelector {
        let c = CString::new(name).unwrap();
        DatatypeSelector::from_raw(unsafe { cvc5_dt_get_selector(self.inner, c.as_ptr()) })
    }

    /// Get the name of this datatype.
    pub fn name(&self) -> &str {
        unsafe {
            std::ffi::CStr::from_ptr(cvc5_dt_get_name(self.inner))
                .to_str()
                .unwrap_or("")
        }
    }

    /// Get the number of constructors.
    pub fn num_constructors(&self) -> usize {
        unsafe { cvc5_dt_get_num_constructors(self.inner) }
    }

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

    /// Return `true` if this datatype is parametric.
    pub fn is_parametric(&self) -> bool {
        unsafe { cvc5_dt_is_parametric(self.inner) }
    }

    /// Return `true` if this is a codatatype.
    pub fn is_codatatype(&self) -> bool {
        unsafe { cvc5_dt_is_codatatype(self.inner) }
    }

    /// Return `true` if this is a tuple datatype.
    pub fn is_tuple(&self) -> bool {
        unsafe { cvc5_dt_is_tuple(self.inner) }
    }

    /// Return `true` if this is a record datatype.
    pub fn is_record(&self) -> bool {
        unsafe { cvc5_dt_is_record(self.inner) }
    }

    /// Return `true` if this datatype is finite.
    pub fn is_finite(&self) -> bool {
        unsafe { cvc5_dt_is_finite(self.inner) }
    }

    /// Return `true` if this datatype is well-founded.
    pub fn is_well_founded(&self) -> bool {
        unsafe { cvc5_dt_is_well_founded(self.inner) }
    }
}

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

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

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

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