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
use std::convert::TryInto;
use std::ffi::CStr;
use std::fmt;
use std::ptr::NonNull;
use z3_sys::*;
use crate::{Context, FuncDecl, Sort, SortDiffers, Symbol};
impl Sort {
pub(crate) unsafe fn wrap(ctx: &Context, z3_sort: Z3_sort) -> Sort {
unsafe {
Z3_inc_ref(ctx.z3_ctx.0, Z3_sort_to_ast(ctx.z3_ctx.0, z3_sort).unwrap());
}
Sort {
ctx: ctx.clone(),
z3_sort,
}
}
pub fn get_z3_sort(&self) -> Z3_sort {
self.z3_sort
}
pub fn uninterpreted(name: Symbol) -> Sort {
let ctx = &Context::thread_local();
unsafe {
Self::wrap(
ctx,
Z3_mk_uninterpreted_sort(ctx.z3_ctx.0, name.as_z3_symbol()).unwrap(),
)
}
}
pub fn bool() -> Sort {
unsafe {
let ctx = &Context::thread_local();
Self::wrap(ctx, Z3_mk_bool_sort(ctx.z3_ctx.0).unwrap())
}
}
pub fn int() -> Sort {
unsafe {
let ctx = &Context::thread_local();
Self::wrap(ctx, Z3_mk_int_sort(ctx.z3_ctx.0).unwrap())
}
}
pub fn real() -> Sort {
unsafe {
let ctx = &Context::thread_local();
Self::wrap(ctx, Z3_mk_real_sort(ctx.z3_ctx.0).unwrap())
}
}
pub fn float(ebits: u32, sbits: u32) -> Sort {
unsafe {
let ctx = &Context::thread_local();
Self::wrap(ctx, Z3_mk_fpa_sort(ctx.z3_ctx.0, ebits, sbits).unwrap())
}
}
pub fn float32() -> Sort {
unsafe {
let ctx = &Context::thread_local();
Self::wrap(ctx, Z3_mk_fpa_sort(ctx.z3_ctx.0, 8, 24).unwrap())
}
}
pub fn double() -> Sort {
unsafe {
let ctx = &Context::thread_local();
Self::wrap(ctx, Z3_mk_fpa_sort(ctx.z3_ctx.0, 11, 53).unwrap())
}
}
pub fn string() -> Sort {
unsafe {
let ctx = &Context::thread_local();
Self::wrap(ctx, Z3_mk_string_sort(ctx.z3_ctx.0).unwrap())
}
}
pub fn bitvector(sz: u32) -> Sort {
let ctx = &Context::thread_local();
unsafe {
Self::wrap(
ctx,
Z3_mk_bv_sort(ctx.z3_ctx.0, sz as ::std::os::raw::c_uint).unwrap(),
)
}
}
pub fn array(domain: &Sort, range: &Sort) -> Sort {
let ctx = &Context::thread_local();
unsafe {
Self::wrap(
ctx,
Z3_mk_array_sort(ctx.z3_ctx.0, domain.z3_sort, range.z3_sort).unwrap(),
)
}
}
pub fn set(elt: &Sort) -> Sort {
let ctx = &Context::thread_local();
unsafe { Self::wrap(ctx, Z3_mk_set_sort(ctx.z3_ctx.0, elt.z3_sort).unwrap()) }
}
pub fn seq(elt: &Sort) -> Sort {
let ctx = &Context::thread_local();
unsafe { Self::wrap(ctx, Z3_mk_seq_sort(ctx.z3_ctx.0, elt.z3_sort).unwrap()) }
}
/// Create an enumeration sort.
///
/// Creates a Z3 enumeration sort with the given `name`.
/// The enum variants will have the names in `enum_names`.
/// Three things are returned:
/// - the created `Sort`,
/// - constants to create the variants,
/// - and testers to check if a value is equal to a variant.
///
/// # Examples
/// ```
/// # use z3::{Config, Context, SatResult, Solver, Sort, Symbol};
/// # let cfg = Config::new();
/// # let solver = Solver::new();
/// let (colors, color_consts, color_testers) = Sort::enumeration(
/// "Color".into(),
/// &[
/// "Red".into(),
/// "Green".into(),
/// "Blue".into(),
/// ],
/// );
///
/// let red_const = color_consts[0].apply(&[]);
/// let red_tester = &color_testers[0];
/// let eq = red_tester.apply(&[&red_const]);
///
/// assert_eq!(solver.check(), SatResult::Sat);
/// let model = solver.get_model().unwrap();;
///
/// assert!(model.eval(&eq, true).unwrap().as_bool().unwrap().as_bool().unwrap());
/// ```
pub fn enumeration(
name: Symbol,
enum_names: &[Symbol],
) -> (Sort, Vec<FuncDecl>, Vec<FuncDecl>) {
let ctx = &Context::thread_local();
let enum_names: Vec<_> = enum_names.iter().map(|s| s.as_z3_symbol()).collect();
let mut enum_consts = vec![std::ptr::null_mut(); enum_names.len()];
let mut enum_testers = vec![std::ptr::null_mut(); enum_names.len()];
let sort = unsafe {
Self::wrap(
ctx,
Z3_mk_enumeration_sort(
ctx.z3_ctx.0,
name.as_z3_symbol(),
enum_names.len().try_into().unwrap(),
enum_names.as_ptr(),
enum_consts.as_mut_ptr(),
enum_testers.as_mut_ptr(),
)
.unwrap(),
)
};
// increase ref counts
for i in &enum_consts {
unsafe {
Z3_inc_ref(
ctx.z3_ctx.0,
Z3_func_decl_to_ast(ctx.z3_ctx.0, NonNull::new(*i).unwrap()).unwrap(),
);
}
}
for i in &enum_testers {
unsafe {
Z3_inc_ref(
ctx.z3_ctx.0,
Z3_func_decl_to_ast(ctx.z3_ctx.0, NonNull::new(*i).unwrap()).unwrap(),
);
}
}
// convert to Rust types
let enum_consts: Vec<_> = enum_consts
.into_iter()
.map(|z3_func_decl| unsafe { FuncDecl::wrap(ctx, NonNull::new(z3_func_decl).unwrap()) })
.collect();
let enum_testers: Vec<_> = enum_testers
.into_iter()
.map(|z3_func_decl| unsafe { FuncDecl::wrap(ctx, NonNull::new(z3_func_decl).unwrap()) })
.collect();
(sort, enum_consts, enum_testers)
}
pub fn kind(&self) -> SortKind {
unsafe { Z3_get_sort_kind(self.ctx.z3_ctx.0, self.z3_sort) }
}
/// Returns `Some(e)` where `e` is the number of exponent bits if the sort
/// is a `FloatingPoint` and `None` otherwise.
pub fn float_exponent_size(&self) -> Option<u32> {
if self.kind() == SortKind::FloatingPoint {
Some(unsafe { Z3_fpa_get_ebits(self.ctx.z3_ctx.0, self.z3_sort) })
} else {
None
}
}
/// Returns `Some(s)` where `s` is the number of significand bits if the sort
/// is a `FloatingPoint` and `None` otherwise.
pub fn float_significand_size(&self) -> Option<u32> {
if self.kind() == SortKind::FloatingPoint {
Some(unsafe { Z3_fpa_get_sbits(self.ctx.z3_ctx.0, self.z3_sort) })
} else {
None
}
}
/// Return if this Sort is for an `Array` or a `Set`.
///
/// # Examples
/// ```
/// # use z3::{Config, Context, Sort, ast::Ast, ast::Int, ast::Bool};
/// let bool_sort = Sort::bool();
/// let int_sort = Sort::int();
/// let array_sort = Sort::array(&int_sort, &bool_sort);
/// let set_sort = Sort::set(&int_sort);
/// assert!(array_sort.is_array());
/// assert!(set_sort.is_array());
/// assert!(!int_sort.is_array());
/// assert!(!bool_sort.is_array());
/// ```
pub fn is_array(&self) -> bool {
self.kind() == SortKind::Array
}
/// Return the `Sort` of the domain for `Array`s of this `Sort`.
///
/// If this `Sort` is an `Array` or `Set`, it has a domain sort, so return it.
/// If this is not an `Array` or `Set` `Sort`, return `None`.
/// # Examples
/// ```
/// # use z3::{Config, Context, Sort, ast::Ast, ast::Int, ast::Bool};
/// let bool_sort = Sort::bool();
/// let int_sort = Sort::int();
/// let array_sort = Sort::array(&int_sort, &bool_sort);
/// let set_sort = Sort::set(&int_sort);
/// assert_eq!(array_sort.array_domain().unwrap(), int_sort);
/// assert_eq!(set_sort.array_domain().unwrap(), int_sort);
/// assert!(int_sort.array_domain().is_none());
/// assert!(bool_sort.array_domain().is_none());
/// ```
pub fn array_domain(&self) -> Option<Sort> {
if self.is_array() {
unsafe {
let domain_sort = Z3_get_array_sort_domain(self.ctx.z3_ctx.0, self.z3_sort)?;
Some(Self::wrap(&self.ctx, domain_sort))
}
} else {
None
}
}
/// Return the `Sort` of the range for `Array`s of this `Sort`.
///
/// If this `Sort` is an `Array` it has a range sort, so return it.
/// If this `Sort` is a `Set`, it has an implied range sort of `Bool`.
/// If this is not an `Array` or `Set` `Sort`, return `None`.
/// # Examples
/// ```
/// # use z3::{Config, Context, Sort, ast::Ast, ast::Int, ast::Bool};
/// let bool_sort = Sort::bool();
/// let int_sort = Sort::int();
/// let array_sort = Sort::array(&int_sort, &bool_sort);
/// let set_sort = Sort::set(&int_sort);
/// assert_eq!(array_sort.array_range().unwrap(), bool_sort);
/// assert_eq!(set_sort.array_range().unwrap(), bool_sort);
/// assert!(int_sort.array_range().is_none());
/// assert!(bool_sort.array_range().is_none());
/// ```
pub fn array_range(&self) -> Option<Sort> {
if self.is_array() {
unsafe {
let range_sort = Z3_get_array_sort_range(self.ctx.z3_ctx.0, self.z3_sort)?;
Some(Self::wrap(&self.ctx, range_sort))
}
} else {
None
}
}
}
impl Clone for Sort {
fn clone(&self) -> Self {
unsafe { Self::wrap(&self.ctx, self.z3_sort) }
}
}
impl fmt::Display for Sort {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let p = unsafe { Z3_sort_to_string(self.ctx.z3_ctx.0, self.z3_sort) };
if p.is_null() {
return Result::Err(fmt::Error);
}
match unsafe { CStr::from_ptr(p) }.to_str() {
Ok(s) => write!(f, "{s}"),
Err(_) => Result::Err(fmt::Error),
}
}
}
impl fmt::Debug for Sort {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
<Self as fmt::Display>::fmt(self, f)
}
}
impl PartialEq<Sort> for Sort {
fn eq(&self, other: &Sort) -> bool {
unsafe { Z3_is_eq_sort(self.ctx.z3_ctx.0, self.z3_sort, other.z3_sort) }
}
}
impl Eq for Sort {}
impl Drop for Sort {
fn drop(&mut self) {
unsafe {
Z3_dec_ref(
self.ctx.z3_ctx.0,
Z3_sort_to_ast(self.ctx.z3_ctx.0, self.z3_sort).unwrap(),
);
}
}
}
impl SortDiffers {
pub fn new(left: Sort, right: Sort) -> Self {
Self { left, right }
}
pub fn left(&self) -> &Sort {
&self.left
}
pub fn right(&self) -> &Sort {
&self.right
}
}
impl fmt::Display for SortDiffers {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(
f,
"Can not compare nodes, Sort does not match. Nodes contain types {} and {}",
self.left, self.right
)
}
}