通过C/C+消除Z3中LIA的量词+;美国石油学会

通过C/C+消除Z3中LIA的量词+;美国石油学会,z3,quantifiers,Z3,Quantifiers,我想使用Z3通过C/C++API消除线性整数算术公式中的量词。考虑一个简单的例子:存在(x)(x),简化器不再调用量词消除过程。量词消除和许多其他特殊目的简化重写可在策略上获得。 C++包装器Z3+.h公开了创建策略对象的方法。 您必须为“qe”策略创建一个策略对象。 Z3发行版附带了几个示例,用于使用Z3++.HAPI中的策略。 例如: void tactic_example1() { /* Z3 implements a methodology for orchestrating re

我想使用Z3通过C/C++API消除线性整数算术公式中的量词。考虑一个简单的例子:存在(x)(x),简化器不再调用量词消除过程。量词消除和许多其他特殊目的简化重写可在策略上获得。

C++包装器Z3+.h公开了创建策略对象的方法。 您必须为“qe”策略创建一个策略对象。 Z3发行版附带了几个示例,用于使用Z3++.HAPI中的策略。 例如:

void tactic_example1() {
/*
  Z3 implements a methodology for orchestrating reasoning engines where "big" symbolic
  reasoning steps are represented as functions known as tactics, and tactics are composed
  using combinators known as tacticals. Tactics process sets of formulas called Goals.

  When a tactic is applied to some goal G, four different outcomes are possible. The tactic succeeds
  in showing G to be satisfiable (i.e., feasible); succeeds in showing G to be unsatisfiable (i.e., infeasible); 
  produces a sequence of subgoals; or fails. When reducing a goal G to a sequence of subgoals G1, ..., Gn, 
  we face the problem of model conversion. A model converter construct a model for G using a model for some subgoal Gi.

  In this example, we create a goal g consisting of three formulas, and a tactic t composed of two built-in tactics: 
  simplify and solve-eqs. The tactic simplify apply transformations equivalent to the ones found in the command simplify. 
  The tactic solver-eqs eliminate variables using Gaussian elimination. Actually, solve-eqs is not restricted 
  only to linear arithmetic. It can also eliminate arbitrary variables. 
  Then, sequential composition combinator & applies simplify to the input goal and solve-eqs to each subgoal produced by simplify. 
  In this example, only one subgoal is produced.
*/
std::cout << "tactic example 1\n";
context c;
expr x = c.real_const("x");
expr y = c.real_const("y");
goal g(c);
g.add(x > 0);
g.add(y > 0);
g.add(x == y + 2);
std::cout << g << "\n";
tactic t1(c, "simplify");
tactic t2(c, "solve-eqs");
tactic t = t1 & t2;
apply_result r = t(g);
std::cout << r << "\n";
void策略\u示例1(){
/*
Z3实现了一种编排推理引擎的方法,其中“大”是符号化的
推理步骤表示为称为策略的函数,策略由
使用被称为战术的组合器。战术处理一组称为目标的公式。
当一个策略应用于某个目标G时,可能会有四种不同的结果。该策略成功
证明G是可满足的(即可行的);成功证明G是不可满足的(即不可行的);
生成一系列子目标;或失败。将目标G减少为一系列子目标G1,…,Gn,
我们面临模型转换的问题。模型转换器使用某个子目标Gi的模型为G构建模型。
在本例中,我们创建了由三个公式组成的目标g和由两个内置战术组成的战术t:
简化并求解等式。策略“简化”应用与命令“简化”中找到的转换等效的转换。
策略解算器eqs使用高斯消去法消除变量。实际上,eqs解算不受限制
仅适用于线性算法。它还可以消除任意变量。
然后,顺序组合组合器&将simplify应用于输入目标,并对simplify生成的每个子目标求解等式。
在本例中,只生成一个子目标。
*/
std::cout 0);
g、 添加(y>0);
g、 加(x==y+2);

STD::P> NIKOLAJ已经指出,可以使用策略来执行量词消除。在这篇文章中,我想强调如何安全地混合C++和C API。Z3C++ ++使用引用计数来管理内存。<代码> ExpP>代码>本质上是一个“智能指针”。自动为我们管理引用计数器的。我在以下帖子中讨论了此问题:

因此,当我们调用返回Z3 ast的C API时,我们应该使用函数
to_expr
to_sort
to_func_decl
来包装结果

inline expr to_expr(context & c, Z3_ast a);
通过使用此函数,我们可以避免内存泄漏和崩溃(在访问Z3垃圾收集的对象时)。下面是使用
to_expr()
的完整示例。您可以通过复制Z3发行版
c++
文件夹中
example.cpp
文件中的
示例.cpp来测试此函数

void tst_quantifier() {
    context ctx;
    expr x = ctx.int_const("x"); 
    expr y = ctx.int_const("y"); 
    expr sub_expr = (x <= y) && (y <= 2*x);
    Z3_app vars[] = {(Z3_app) x};

    expr qf = to_expr(ctx, Z3_mk_exists_const(ctx, 0, 1, vars,
                                              0, 0, // patterns don't seem to make sense here.
                                              sub_expr)); //No C++ API for quantifiers :(
    tactic qe(ctx, "qe");
    goal g(ctx);
    g.add(qf);
    std::cout << qe.apply(g);
}
void tst_量词(){
上下文ctx;
expr x=ctx.int_const(“x”);
expr y=ctx.int_const(“y”);

expr sub_expr=(x我明白你的意思,谢谢。P.S.我真的很高兴stackoverflow正在积极讨论Z3和SMT问题,我可以在一天内从Z3的两位作者那里得到答案。感谢你努力澄清问题。感谢你的反馈,Nikolaj。经过莱昂纳多指出的轻微修改,它解决了我的问题。我开始了ally考虑过使用战术和目标,但这一评论吓跑了我:“当一个战术应用于某个目标G时,有四种不同的结果是可能的。该战术成功地显示G是可满足的(即可行的);成功地显示G是不可满足的(即不可行的);产生一系列子目标;或者失败。”这让我觉得它只涉及满意度检查的策略。而且,我没有找到任何关于“量化宽松”策略的信息。
inline expr to_expr(context & c, Z3_ast a);
void tst_quantifier() {
    context ctx;
    expr x = ctx.int_const("x"); 
    expr y = ctx.int_const("y"); 
    expr sub_expr = (x <= y) && (y <= 2*x);
    Z3_app vars[] = {(Z3_app) x};

    expr qf = to_expr(ctx, Z3_mk_exists_const(ctx, 0, 1, vars,
                                              0, 0, // patterns don't seem to make sense here.
                                              sub_expr)); //No C++ API for quantifiers :(
    tactic qe(ctx, "qe");
    goal g(ctx);
    g.add(qf);
    std::cout << qe.apply(g);
}