首页 文章

在Z3中 Build 自定义理论

提问于
浏览
0

这个问题是对以下问题的跟进 .

Procedural Attachment in Z3

我有一个谓词(我在这种情况下使用“更重”的名称)超过两个整数,我需要使用自定义算法进行评估 . 我编写了以下代码来完成它 . 但我发现传递给函数CMTh_reduce_app()的参数不是实际的整数,而是整数类型的consts . 我需要的是2个整数,这样我就可以评估谓词并返回结果(现在函数CMTh_reduce_app()中完成的操作毫无意义) .

#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);


}

我知道用户理论插件不再受支持,但我真的需要让它工作,所以如果我能获得任何信息,那将非常有用 . 我试着查看源代码,但我不知道从哪里开始构建新的理论 . 所以,我很欣赏理论插件的一些帮助 .

1 回答

  • 0

    您不会从已弃用的理论插件提供的抽象中访问模型 . 问题是模型是在游戏后期构建的 . 它需要重写一些内部来适应这一点(这不是不可能的,而是一个非常公平的工作) .

    我的印象是,只使用与Z3的基本交互,将谓词声明为未解释,检查SAT会更简单 . 然后,如果当前约束是可满足的,则使用当前模型来评估参数 . 如果你有值,这与你的内置程序附件相矛盾,那么就断言新的事实来统治这些 Value (以及尽可能多的其他不可行的 Value ) . 我称之为“懒惰循环方法” . 此交互模型对应于SMT求解器如何在不提供理论传播的情况下使用SAT求解器(在分配新原子时传播真值) . 在冲突分析/解决过程中,您需要做更多的工作才能产生强烈的引理 . 因此,内置理论和惰性循环方法之间的混合可能最终得以解决 . 但在到达那里之前,我建议按原样使用Z3并使用当前模型来计算新的阻塞子句 . 当然,你会失去一些东西:量词的实例化将会有些急切地进行,很可能这种懒惰的循环方法在量词存在的情况下效果不佳 .

相关问题