Maybe I missed something, but what is the way of constructing an if-then-else expression using the z3 C++ API ?
I could use the C API for that, but I\'m wondering wh
We can mix the C and C++ APIs. The file examples/c++/example.cpp
contains an example using the C API to create the if-then-else expression. The function to_expr
is essentially wrapping a Z3_ast
with the C++ "smart pointer" expr
that automatically manages the reference counters for us.
void ite_example() {
std::cout << "if-then-else example\n";
context c;
expr f = c.bool_val(false);
expr one = c.int_val(1);
expr zero = c.int_val(0);
expr ite = to_expr(c, Z3_mk_ite(c, f, one, zero));
std::cout << "term: " << ite << "\n";
}
I just added the ite function to the C++ API. It will be available in the next release (v4.3.2). If you want you can add to the z3++.h
file in your system. A good place to include is after the function implies
:
/**
\brief Create the if-then-else expression ite(c, t, e)
\pre c.is_bool()
*/
friend expr ite(expr const & c, expr const & t, expr const & e) {
check_context(c, t); check_context(c, e);
assert(c.is_bool());
Z3_ast r = Z3_mk_ite(c.ctx(), c, t, e);
c.check_error();
return expr(c.ctx(), r);
}
Using this function, we can write:
void ite_example2() {
std::cout << "if-then-else example2\n";
context c;
expr b = c.bool_const("b");
expr x = c.int_const("x");
expr y = c.int_const("y");
std::cout << (ite(b, x, y) > 0) << "\n";
}