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
use std::ffi::{CStr, CString};
use std::fmt;
use std::result::Result;
use std::str::Utf8Error;
use z3_sys::*;
use crate::{Context, Goal, Probe};
impl Probe {
unsafe fn wrap(ctx: &Context, z3_probe: Z3_probe) -> Probe {
unsafe {
Z3_probe_inc_ref(ctx.z3_ctx.0, z3_probe);
}
Probe {
ctx: ctx.clone(),
z3_probe,
}
}
/// Iterate through the valid probe names.
///
/// # Example
///
/// ```
/// # use z3::Probe;
/// let probes: Vec<_> = Probe::list_all().into_iter().filter_map(|r| r.ok()).collect();
/// assert!(probes.contains(&"is-quasi-pb".to_string()));
/// ```
pub fn list_all() -> Vec<Result<String, Utf8Error>> {
let ctx = &Context::thread_local();
let p = unsafe { Z3_get_num_probes(ctx.z3_ctx.0) };
(0..p)
.map(move |n| {
let t = unsafe { Z3_get_probe_name(ctx.z3_ctx.0, n) };
unsafe { CStr::from_ptr(t).to_str().map(String::from) }
})
.collect()
}
/// Return a string containing a description of the probe with
/// the given `name`.
pub fn describe(name: &str) -> std::result::Result<String, Utf8Error> {
let ctx = &Context::thread_local();
let probe_name = CString::new(name).unwrap();
unsafe {
CStr::from_ptr(Z3_probe_get_descr(ctx.z3_ctx.0, probe_name.as_ptr()))
.to_str()
.map(|s| s.to_string())
}
}
/// Return a probe associated with the given `name`.
///
/// # Example
///
/// ```
/// # use z3::{Config, Context, Probe};
///
/// let probe = Probe::new("is-qfbv");
/// ```
pub fn new(name: &str) -> Probe {
let ctx = &Context::thread_local();
let probe_name = CString::new(name).unwrap();
unsafe { Self::wrap(ctx, Z3_mk_probe(ctx.z3_ctx.0, probe_name.as_ptr()).unwrap()) }
}
/// Execute the probe over the goal.
///
/// The probe always produce a double value. "Boolean" probes return
/// `0.0` for `false`, and a value different from `0.0` for `true`.
pub fn apply(&self, goal: &Goal) -> f64 {
unsafe { Z3_probe_apply(self.ctx.z3_ctx.0, self.z3_probe, goal.z3_goal) }
}
/// Return a probe that always evaluates to val.
/// ```
/// # use z3::Probe;
/// let probe = Probe::constant(1.0);
/// ```
pub fn constant(val: f64) -> Probe {
let ctx = &Context::thread_local();
unsafe { Self::wrap(ctx, Z3_probe_const(ctx.z3_ctx.0, val).unwrap()) }
}
/// Return a probe that evaluates to "true" when the value returned
/// by `self` is less than the value returned by `p`.
///
/// NOTE: For probes, "true" is any value different from 0.0.
pub fn lt(&self, p: &Probe) -> Probe {
unsafe {
Self::wrap(
&self.ctx,
Z3_probe_lt(self.ctx.z3_ctx.0, self.z3_probe, p.z3_probe).unwrap(),
)
}
}
/// Return a probe that evaluates to "true" when the value returned
/// by `self` is greater than the value returned by `p`.
pub fn gt(&self, p: &Probe) -> Probe {
unsafe {
Self::wrap(
&self.ctx,
Z3_probe_gt(self.ctx.z3_ctx.0, self.z3_probe, p.z3_probe).unwrap(),
)
}
}
/// Return a probe that evaluates to "true" when the value returned
/// by `self` is less than or equal to the value returned by `p`.
pub fn le(&self, p: &Probe) -> Probe {
unsafe {
Self::wrap(
&self.ctx,
Z3_probe_le(self.ctx.z3_ctx.0, self.z3_probe, p.z3_probe).unwrap(),
)
}
}
/// Return a probe that evaluates to "true" when the value returned
/// by `self` is greater than or equal to the value returned by `p`.
pub fn ge(&self, p: &Probe) -> Probe {
unsafe {
Self::wrap(
&self.ctx,
Z3_probe_ge(self.ctx.z3_ctx.0, self.z3_probe, p.z3_probe).unwrap(),
)
}
}
/// Return a probe that evaluates to "true" when the value returned
/// by `self` is equal to the value returned by `p`.
pub fn eq(&self, p: &Probe) -> Probe {
unsafe {
Self::wrap(
&self.ctx,
Z3_probe_eq(self.ctx.z3_ctx.0, self.z3_probe, p.z3_probe).unwrap(),
)
}
}
/// Return a probe that evaluates to "true" when `self` and `p` evaluates to true.
pub fn and(&self, p: &Probe) -> Probe {
unsafe {
Self::wrap(
&self.ctx,
Z3_probe_and(self.ctx.z3_ctx.0, self.z3_probe, p.z3_probe).unwrap(),
)
}
}
/// Return a probe that evaluates to "true" when `p1` or `p2` evaluates to true.
pub fn or(&self, p: &Probe) -> Probe {
unsafe {
Self::wrap(
&self.ctx,
Z3_probe_or(self.ctx.z3_ctx.0, self.z3_probe, p.z3_probe).unwrap(),
)
}
}
/// Return a probe that evaluates to "true" when `p` does not evaluate to true.
pub fn not(&self) -> Probe {
unsafe {
Self::wrap(
&self.ctx,
Z3_probe_not(self.ctx.z3_ctx.0, self.z3_probe).unwrap(),
)
}
}
/// Return a probe that evaluates to "true" when the value returned
/// by `self` is not equal to the value returned by `p`.
pub fn ne(&self, p: &Probe) -> Probe {
self.eq(p).not()
}
}
impl Clone for Probe {
fn clone(&self) -> Self {
unsafe { Self::wrap(&self.ctx, self.z3_probe) }
}
}
impl fmt::Display for Probe {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "<z3.probe>")
}
}
impl fmt::Debug for Probe {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
<Self as fmt::Display>::fmt(self, f)
}
}
impl Drop for Probe {
fn drop(&mut self) {
unsafe {
Z3_probe_dec_ref(self.ctx.z3_ctx.0, self.z3_probe);
}
}
}