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
use crate::ast::{Ast, BV, Real, binop};
use crate::ast::{Bool, unop, varop};
use crate::{Context, Sort, Symbol};
#[cfg(feature = "num")]
use num::BigInt;
use std::ffi::CString;
use std::str::FromStr;
use z3_sys::*;
/// [`Ast`] node representing an integer value.
pub struct Int {
pub(crate) ctx: Context,
pub(crate) z3_ast: Z3_ast,
}
#[cfg(feature = "num")]
impl Int {
pub fn from_big_int(value: &BigInt) -> Int {
Int::from_str(&value.to_str_radix(10)).unwrap()
}
}
impl Int {
pub fn new_const<S: Into<Symbol>>(name: S) -> Int {
let ctx = &Context::thread_local();
let sort = Sort::int();
unsafe {
Self::wrap(ctx, {
Z3_mk_const(ctx.z3_ctx.0, name.into().as_z3_symbol(), sort.z3_sort).unwrap()
})
}
}
/// Declare and create a fresh Integer uninterpreted constant with name `prefix`.
pub fn fresh_const(prefix: &str) -> Int {
let ctx = &Context::thread_local();
let sort = Sort::int();
unsafe {
Self::wrap(ctx, {
let pp = CString::new(prefix).unwrap();
let p = pp.as_ptr();
Z3_mk_fresh_const(ctx.z3_ctx.0, p, sort.z3_sort).unwrap()
})
}
}
/// Create an AST node representing the integer value `i`.
pub fn from_i64(i: i64) -> Int {
let ctx = &Context::thread_local();
let sort = Sort::int();
unsafe { Self::wrap(ctx, Z3_mk_int64(ctx.z3_ctx.0, i, sort.z3_sort).unwrap()) }
}
/// Create an AST node representing the integer value `u`.
pub fn from_u64(u: u64) -> Int {
let ctx = &Context::thread_local();
let sort = Sort::int();
unsafe {
Self::wrap(
ctx,
Z3_mk_unsigned_int64(ctx.z3_ctx.0, u, sort.z3_sort).unwrap(),
)
}
}
pub fn as_i64(&self) -> Option<i64> {
unsafe {
let mut tmp: ::std::os::raw::c_longlong = 0;
if Z3_get_numeral_int64(self.ctx.z3_ctx.0, self.z3_ast, &mut tmp) {
Some(tmp)
} else {
None
}
}
}
pub fn as_u64(&self) -> Option<u64> {
unsafe {
let mut tmp: ::std::os::raw::c_ulonglong = 0;
if Z3_get_numeral_uint64(self.ctx.z3_ctx.0, self.z3_ast, &mut tmp) {
Some(tmp)
} else {
None
}
}
}
pub fn from_real(ast: &Real) -> Int {
unsafe {
Self::wrap(
&ast.ctx,
Z3_mk_real2int(ast.ctx.z3_ctx.0, ast.z3_ast).unwrap(),
)
}
}
/// Create a real from an integer.
/// This is just a convenience wrapper around
/// [`Real::from_int()`]; see notes there.
pub fn to_real(&self) -> Real {
Real::from_int(self)
}
/// Create an integer from a bitvector.
///
/// Signed and unsigned version.
///
/// # Examples
/// ```
/// # use z3::{ast, Config, Context, SatResult, Solver};
/// # use z3::ast::Ast;
/// # let solver = Solver::new();
/// let bv = ast::BV::new_const("x", 32);
/// solver.assert(&bv._eq(&ast::BV::from_i64(-3, 32)));
///
/// let x = ast::Int::from_bv(&bv, true);
///
/// assert_eq!(solver.check(), SatResult::Sat);
/// let model = solver.get_model().unwrap();
///
/// assert_eq!(-3, model.eval(&x, true).unwrap().as_i64().unwrap());
/// ```
pub fn from_bv(ast: &BV, signed: bool) -> Int {
unsafe {
Self::wrap(&ast.ctx, {
Z3_mk_bv2int(ast.ctx.z3_ctx.0, ast.z3_ast, signed).unwrap()
})
}
}
/// Create a bitvector from an integer.
/// This is just a convenience wrapper around
/// [`BV::from_int()`]; see notes there.
pub fn to_ast(&self, sz: u32) -> BV {
BV::from_int(self, sz)
}
varop! {
add(Z3_mk_add, Self);
sub(Z3_mk_sub, Self);
mul(Z3_mk_mul, Self);
}
unop! {
unary_minus(Z3_mk_unary_minus, Self);
}
binop! {
div(Z3_mk_div, Self);
rem(Z3_mk_rem, Self);
modulo(Z3_mk_mod, Self);
power(Z3_mk_power, Real);
lt(Z3_mk_lt, Bool);
le(Z3_mk_le, Bool);
gt(Z3_mk_gt, Bool);
ge(Z3_mk_ge, Bool);
}
// Z3 does support mixing ints and reals in add(), sub(), mul(), div(), and power()
// (but not rem(), modulo(), lt(), le(), gt(), or ge()).
// TODO: we could consider expressing this by having a Numeric trait with these methods.
// Int and Real would have the Numeric trait, but not the other Asts.
// For example:
// fn add(&self, other: &impl Numeric) -> Dynamic { ... }
// Note the return type would have to be Dynamic I think (?), as the exact result type
// depends on the particular types of the inputs.
// Alternately, we could just have
// Int::add_real(&self, other: &Real) -> Real
// and
// Real::add_int(&self, other: &Int) -> Real
// This might be cleaner because we know exactly what the output type will be for these methods.
}
macro_rules! into_int {
($t:ty) => {
impl From<$t> for Int {
fn from(value: $t) -> Self {
Int::from_u64(value as u64)
}
}
};
}
macro_rules! into_int_signed {
($t:ty) => {
impl From<$t> for Int {
fn from(value: $t) -> Self {
Int::from_i64(value as i64)
}
}
};
}
into_int!(u8);
into_int!(u16);
into_int!(u32);
into_int!(u64);
into_int_signed!(i8);
into_int_signed!(i16);
into_int_signed!(i32);
into_int_signed!(i64);
#[cfg(feature = "num")]
impl From<BigInt> for Int {
fn from(value: BigInt) -> Self {
Int::from_big_int(&value)
}
}
// todo: when we add a proper error type return that instead
impl FromStr for Int {
type Err = ();
fn from_str(value: &str) -> Result<Int, Self::Err> {
let ctx = &Context::thread_local();
let sort = Sort::int();
let ast = unsafe {
let int_cstring = CString::new(value).unwrap();
Z3_mk_numeral(ctx.z3_ctx.0, int_cstring.as_ptr(), sort.z3_sort)
}
.ok_or(())?;
Ok(unsafe { Int::wrap(ctx, ast) })
}
}