5

也许我错过了一些东西,但是使用 z3 C++ API 构造 if-then-else 表达式的方法是什么?

我可以为此使用 C API,但我想知道为什么 C++ API 中没有这样的功能。

问候,朱利安

4

1 回答 1

9

我们可以混合使用 C 和 C++ API。该文件examples/c++/example.cpp包含一个使用 C API 创建 if-then-else 表达式的示例。该函数to_expr本质上是Z3_ast用 C++“智能指针”包装 a,expr它自动为我们管理引用计数器。

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";
}

我刚刚将该ite函数添加到 C++ API。它将在下一个版本 (v4.3.2) 中提供。如果需要,您可以添加到z3++.h系统中的文件中。一个包含的好地方是在函数之后implies

/**
   \brief Create the if-then-else expression <tt>ite(c, t, e)</tt>

   \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);
}

使用这个函数,我们可以写:

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";
}
于 2013-01-04T16:25:59.427 回答