C++ 在Z3中构建定制理论
此问题是以下问题的后续问题 我有一个谓词,我在本例中使用的名称是两个整数,我需要使用自定义算法计算它们。我已经写了下面的代码来实现它。但是我看到传递到函数CMTh_reduce_app中的参数不是实际的整数,而是integer类型的常量。我需要的是2个整数,这样我就可以计算谓词并返回结果。现在在函数CMTh_reduce_app中执行的操作没有意义C++ 在Z3中构建定制理论,c++,z3,C++,Z3,此问题是以下问题的后续问题 我有一个谓词,我在本例中使用的名称是两个整数,我需要使用自定义算法计算它们。我已经写了下面的代码来实现它。但是我看到传递到函数CMTh_reduce_app中的参数不是实际的整数,而是integer类型的常量。我需要的是2个整数,这样我就可以计算谓词并返回结果。现在在函数CMTh_reduce_app中执行的操作没有意义 #include<stdio.h> #include<stdlib.h> #include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdarg.h>
#include<memory.h>
#include<z3.h>
#include "z3++.h"
#include<iostream>
using namespace z3;
using namespace std;
struct _CMTheoryData {
Z3_func_decl heavier;
};
typedef struct _CMTheoryData CMTheoryData;
Z3_context ctx;
//Exit function
void exitf(const char* message)
{
fprintf(stderr,"BUG: %s.\n", message);
exit(1);
}
//Check and print model if available
void check(Z3_context ctx)
{
Z3_model m = 0;
Z3_lbool result = Z3_check_and_get_model(ctx, &m);
switch (result) {
case Z3_L_FALSE:
printf("unsat\n");
break;
case Z3_L_UNDEF:
printf("unknown\n");
printf("potential model:\n%s\n", Z3_model_to_string(ctx, m));
break;
case Z3_L_TRUE:
printf("sat\n%s\n", Z3_model_to_string(ctx, m));
break;
}
if (m) {
Z3_del_model(ctx, m);
}
}
//Create logical context. Enable model generation, and set error handler
void error_handler(Z3_error_code e)
{
printf("Error code: %d\n", e);
exitf("incorrect use of Z3");
}
Z3_context mk_context_custom(Z3_config cfg, Z3_error_handler err)
{
Z3_context ctx;
Z3_set_param_value(cfg, "MODEL", "true");
ctx = Z3_mk_context(cfg);
#ifdef TRACING
Z3_trace_to_stderr(ctx);
#endif
Z3_set_error_handler(ctx, err);
return ctx;
}
Z3_context mk_context()
{
Z3_config cfg;
Z3_context ctx;
cfg = Z3_mk_config();
ctx = mk_context_custom(cfg, error_handler);
Z3_del_config(cfg);
return ctx;
}
//Shortcut for binary fn application
Z3_ast mk_binary_app(Z3_context ctx, Z3_func_decl f, Z3_ast x, Z3_ast y)
{
Z3_ast args[2] = {x, y};
return Z3_mk_app(ctx, f, 2, args);
}
//Shortcut to create an int
Z3_ast mk_int(Z3_context ctx, int v)
{
Z3_sort ty = Z3_mk_int_sort(ctx);
return Z3_mk_int(ctx, v, ty);
}
Z3_ast mk_var(Z3_context ctx, const char * name, Z3_sort ty)
{
Z3_symbol s = Z3_mk_string_symbol(ctx, name);
return Z3_mk_const(ctx, s, ty);
}
Z3_ast mk_int_var(Z3_context ctx, const char * name)
{
Z3_sort ty = Z3_mk_int_sort(ctx);
return mk_var(ctx, name, ty);
}
//Callback when final check is to be carried out
Z3_bool CMTh_final_check(Z3_theory t) {
printf("Final check\n");
return Z3_TRUE;
}
//Callback when theory is to be deleted
void CMTh_delete(Z3_theory t) {
CMTheoryData * td = (CMTheoryData *)Z3_theory_get_ext_data(t);
printf("Delete\n");
free(td);
}
//Callback to reduce a function application(definition of custom functions, predicates)
Z3_bool CMTh_reduce_app(Z3_theory t, Z3_func_decl d, unsigned n, Z3_ast const args[], Z3_ast * result) {
CMTheoryData * td = (CMTheoryData*)Z3_theory_get_ext_data(t);
cout<<Z3_ast_to_string(ctx, args[0])<<' '<<Z3_ast_to_string(ctx,args[1])<<endl;
if (d == td->heavier) {
cout<<"Reducing the fn \'heavier\'"<<endl;
if(Z3_is_eq_ast(ctx,mk_int(ctx, 1),args[0])||Z3_is_eq_ast(ctx,mk_int(ctx,2),args[0]))
{
*result = Z3_mk_true(Z3_theory_get_context(t));
return Z3_TRUE;;
}
else
{
*result = Z3_mk_false(Z3_theory_get_context(t));
return Z3_TRUE;;
}
}
return Z3_FALSE; // failed to simplify
}
Z3_theory mk_cm_theory(Z3_context ctx) {
Z3_sort heavier_domain[2];
Z3_symbol heavier_name = Z3_mk_string_symbol(ctx, "heavier");
Z3_sort B = Z3_mk_bool_sort(ctx);
CMTheoryData * td = (CMTheoryData*)malloc(sizeof(CMTheoryData));
Z3_theory Th = Z3_mk_theory(ctx, "cm_th", td);
heavier_domain[0] = Z3_mk_int_sort(ctx);
heavier_domain[1] = Z3_mk_int_sort(ctx);
td->heavier = Z3_theory_mk_func_decl(ctx, Th, heavier_name, 2, heavier_domain, B); //context, theory, name_of_fn, number of arguments, argument type list, return type
Z3_set_delete_callback(Th, CMTh_delete);
Z3_set_reduce_app_callback(Th, CMTh_reduce_app);
Z3_set_final_check_callback(Th, CMTh_final_check);
return Th;
}
main()
{
Z3_ast a_ast, b_ast, c_ast, f1, f3, r;
Z3_sort i;
Z3_pattern p;
Z3_app bound[2];
Z3_theory Th;
CMTheoryData * td;
printf("\nCustom theory example\n");
ctx = mk_context();
Th = mk_cm_theory(ctx);
td = (CMTheoryData*)Z3_theory_get_ext_data(Th);
a_ast = mk_int_var(ctx, "a");
b_ast = mk_int_var(ctx, "b");
bound[0] = (Z3_app)a_ast;
f1=mk_binary_app(ctx, td->heavier, a_ast, b_ast);
r= Z3_mk_exists_const(ctx, 0, 1, bound, 0, 0,f1);
printf("assert axiom:\n%s\n", Z3_ast_to_string(ctx, r));
Z3_assert_cnstr(ctx, r);
check(ctx);
}
我知道用户理论插件已经不受支持了,但我真的需要让它工作起来,所以如果我能得到任何信息,它将非常有用。我试图查看源代码,但我不知道从哪里开始在其中构建新的理论。因此,我非常感谢您对theory插件的帮助。您将无法从 不推荐使用的theory插件提供的抽象。 问题是模型是在游戏的后期构建的。 这将需要重写一些内部构件以适应这种情况 这并非不可能,而是一项相当大的工作 我的印象是,只使用基本的交互会更简单 在Z3中,您将谓词声明为未解释的,请检查SAT。 那么如果当前的约束是可满足的, 使用当前模型计算参数。如果你有价值观,那与你的 内置的程序附件,然后断言排除这些值和尽可能多的新事实 尽可能使用其他不可行的值。我称之为惰性循环方法。 此交互模型对应于SMT解算器可以使用的方式 不提供传播真值理论的SAT解算器 当分配新的原子时。在这段时间里,你需要做更多的工作 冲突分析/解决,以产生强引理。这么说,一个混血儿 在内置理论和惰性循环方法之间,最终可能会成功。 但是在到达那里之前,我建议只使用Z3,并使用当前的模型 计算新的阻塞子句。 当然,您会失去一些东西:量词的实例化将有点急切地进行 很可能这种惰性循环方法在存在的情况下不能很好地工作
量词的定义 嗨,谢谢你的回答。我将尝试惰性循环方法。我猜增量求解在这里应该最有效。我在其他一些答案中读到,Z3的未来版本可能有一个成熟的用户理论插件。我们有可能很快看到它吗?