使用z3_update_term函数进行术语更新

时间:2013-05-21 08:56:39

标签: z3

以下代码在表达式2中将3更改为e1

context z3_cont;
expr x = z3_cont.int_const("x");
expr e1 = (x==2);
expr e2 = (x==3);
Z3_ast ee[2];
ee[0]=e1.arg(0);
ee[1]=e2.arg(1);
e1 = to_expr(z3_cont,Z3_update_term(z3_cont,e1,2,ee));

是否可以更轻松地完成?不幸的是,代码e1.arg(1) = e2.arg(1)不起作用。 第二个问题是如何在Z3_AST的任意深度上更改表达式,例如e1.arg(1).arg(0) = e2.arg(1).arg(1)

1 个答案:

答案 0 :(得分:1)

您可以使用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;
}