您可以使用Z3_substitute
API。这是一个例子:
void substitute_example() {
std::cout << "substitute example\n";
context c;
expr x(c);
x = c.int_const("x");
expr f(c);
f = (x == 2) || (x == 1);
std::cout << f << std::endl;
expr two(c), three(c);
two = c.int_val(2);
three = c.int_val(3);
Z3_ast from[] = { two };
Z3_ast to[] = { three };
expr new_f(c);
// Replaces the expressions in from with to in f.
// The third argument is the size of the arrays from and to.
new_f = to_expr(c, Z3_substitute(c, f, 1, from, to));
std::cout << new_f << std::endl;
}
更新如果我们想x == 2
用x == 3
公式替换,我们应该写。
void substitute_example() {
std::cout << "substitute example\n";
context c;
expr x(c), y(c);
x = c.int_const("x");
y = c.int_const("y");
expr f(c);
f = (x == 2) || (y == 2);
std::cout << f << std::endl;
expr from(c), to(c);
from = x == 2;
to = x == 3;
Z3_ast _from[] = { from };
Z3_ast _to[] = { to };
expr new_f(c);
// Replaces the expressions in from with to in f.
// The third argument is the size of the arrays from and to.
new_f = to_expr(c, Z3_substitute(c, f, 1, _from, _to));
std::cout << new_f << std::endl;
}