mirror of
https://github.com/Z3Prover/z3
synced 2025-04-24 09:35:32 +00:00
merge with unstable
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
commit
23e811d136
95 changed files with 24076 additions and 414 deletions
|
@ -209,6 +209,7 @@ extern "C" {
|
|||
MK_BINARY(Z3_mk_xor, mk_c(c)->get_basic_fid(), OP_XOR, SKIP);
|
||||
MK_NARY(Z3_mk_and, mk_c(c)->get_basic_fid(), OP_AND, SKIP);
|
||||
MK_NARY(Z3_mk_or, mk_c(c)->get_basic_fid(), OP_OR, SKIP);
|
||||
MK_UNARY(Z3_mk_interp, mk_c(c)->get_basic_fid(), OP_INTERP, SKIP);
|
||||
|
||||
Z3_ast mk_ite_core(Z3_context c, Z3_ast t1, Z3_ast t2, Z3_ast t3) {
|
||||
expr * result = mk_c(c)->m().mk_ite(to_expr(t1), to_expr(t2), to_expr(t3));
|
||||
|
@ -923,6 +924,7 @@ extern "C" {
|
|||
case OP_NOT: return Z3_OP_NOT;
|
||||
case OP_IMPLIES: return Z3_OP_IMPLIES;
|
||||
case OP_OEQ: return Z3_OP_OEQ;
|
||||
case OP_INTERP: return Z3_OP_INTERP;
|
||||
|
||||
case PR_UNDEF: return Z3_OP_PR_UNDEF;
|
||||
case PR_TRUE: return Z3_OP_PR_TRUE;
|
||||
|
|
622
src/api/api_interp.cpp
Normal file
622
src/api/api_interp.cpp
Normal file
|
@ -0,0 +1,622 @@
|
|||
/*++
|
||||
Copyright (c) 2011 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
api_interp.cpp
|
||||
|
||||
Abstract:
|
||||
API for interpolation
|
||||
|
||||
Author:
|
||||
|
||||
Ken McMillan
|
||||
|
||||
Revision History:
|
||||
|
||||
--*/
|
||||
#include<iostream>
|
||||
#include<sstream>
|
||||
#include"z3.h"
|
||||
#include"api_log_macros.h"
|
||||
#include"api_context.h"
|
||||
#include"api_tactic.h"
|
||||
#include"api_solver.h"
|
||||
#include"api_model.h"
|
||||
#include"api_stats.h"
|
||||
#include"api_ast_vector.h"
|
||||
#include"tactic2solver.h"
|
||||
#include"scoped_ctrl_c.h"
|
||||
#include"cancel_eh.h"
|
||||
#include"scoped_timer.h"
|
||||
#include"smt_strategic_solver.h"
|
||||
#include"smt_solver.h"
|
||||
#include"smt_implied_equalities.h"
|
||||
#include"iz3interp.h"
|
||||
#include"iz3profiling.h"
|
||||
#include"iz3hash.h"
|
||||
#include"iz3pp.h"
|
||||
#include"iz3checker.h"
|
||||
|
||||
#ifndef WIN32
|
||||
using namespace stl_ext;
|
||||
#endif
|
||||
|
||||
#ifndef WIN32
|
||||
// WARNING: don't make a hash_map with this if the range type
|
||||
// has a destructor: you'll get an address dependency!!!
|
||||
namespace stl_ext {
|
||||
template <>
|
||||
class hash<Z3_ast> {
|
||||
public:
|
||||
size_t operator()(const Z3_ast p) const {
|
||||
return (size_t) p;
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
typedef interpolation_options_struct *Z3_interpolation_options;
|
||||
|
||||
extern "C" {
|
||||
|
||||
Z3_context Z3_mk_interpolation_context(Z3_config cfg){
|
||||
if(!cfg) cfg = Z3_mk_config();
|
||||
Z3_set_param_value(cfg, "PROOF", "true");
|
||||
Z3_set_param_value(cfg, "MODEL", "true");
|
||||
// Z3_set_param_value(cfg, "PRE_SIMPLIFIER","false");
|
||||
// Z3_set_param_value(cfg, "SIMPLIFY_CLAUSES","false");
|
||||
|
||||
Z3_context ctx = Z3_mk_context(cfg);
|
||||
Z3_del_config(cfg);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
void Z3_interpolate_proof(Z3_context ctx,
|
||||
Z3_ast proof,
|
||||
int num,
|
||||
Z3_ast *cnsts,
|
||||
unsigned *parents,
|
||||
Z3_params options,
|
||||
Z3_ast *interps,
|
||||
int num_theory,
|
||||
Z3_ast *theory
|
||||
){
|
||||
|
||||
if(num > 1){ // if we have interpolants to compute
|
||||
|
||||
ptr_vector<ast> pre_cnsts_vec(num); // get constraints in a vector
|
||||
for(int i = 0; i < num; i++){
|
||||
ast *a = to_ast(cnsts[i]);
|
||||
pre_cnsts_vec[i] = a;
|
||||
}
|
||||
|
||||
::vector<int> pre_parents_vec; // get parents in a vector
|
||||
if(parents){
|
||||
pre_parents_vec.resize(num);
|
||||
for(int i = 0; i < num; i++)
|
||||
pre_parents_vec[i] = parents[i];
|
||||
}
|
||||
|
||||
ptr_vector<ast> theory_vec; // get background theory in a vector
|
||||
if(theory){
|
||||
theory_vec.resize(num_theory);
|
||||
for(int i = 0; i < num_theory; i++)
|
||||
theory_vec[i] = to_ast(theory[i]);
|
||||
}
|
||||
|
||||
ptr_vector<ast> interpolants(num-1); // make space for result
|
||||
|
||||
ast_manager &_m = mk_c(ctx)->m();
|
||||
iz3interpolate(_m,
|
||||
to_ast(proof),
|
||||
pre_cnsts_vec,
|
||||
pre_parents_vec,
|
||||
interpolants,
|
||||
theory_vec,
|
||||
0); // ignore params for now FIXME
|
||||
|
||||
// copy result back
|
||||
for(unsigned i = 0; i < interpolants.size(); i++){
|
||||
mk_c(ctx)->save_ast_trail(interpolants[i]);
|
||||
interps[i] = of_ast(interpolants[i]);
|
||||
_m.dec_ref(interpolants[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Z3_lbool Z3_interpolate(Z3_context ctx,
|
||||
int num,
|
||||
Z3_ast *cnsts,
|
||||
unsigned *parents,
|
||||
Z3_params options,
|
||||
Z3_ast *interps,
|
||||
Z3_model *model,
|
||||
Z3_literals *labels,
|
||||
int incremental,
|
||||
int num_theory,
|
||||
Z3_ast *theory
|
||||
){
|
||||
|
||||
|
||||
profiling::timer_start("Solve");
|
||||
|
||||
if(!incremental){
|
||||
|
||||
profiling::timer_start("Z3 assert");
|
||||
|
||||
Z3_push(ctx); // so we can rewind later
|
||||
|
||||
for(int i = 0; i < num; i++)
|
||||
Z3_assert_cnstr(ctx,cnsts[i]); // assert all the constraints
|
||||
|
||||
if(theory){
|
||||
for(int i = 0; i < num_theory; i++)
|
||||
Z3_assert_cnstr(ctx,theory[i]);
|
||||
}
|
||||
|
||||
profiling::timer_stop("Z3 assert");
|
||||
}
|
||||
|
||||
|
||||
// Get a proof of unsat
|
||||
|
||||
Z3_ast proof;
|
||||
Z3_lbool result;
|
||||
|
||||
profiling::timer_start("Z3 solving");
|
||||
result = Z3_check_assumptions(ctx, 0, 0, model, &proof, 0, 0);
|
||||
profiling::timer_stop("Z3 solving");
|
||||
|
||||
switch (result) {
|
||||
case Z3_L_FALSE:
|
||||
|
||||
Z3_interpolate_proof(ctx,
|
||||
proof,
|
||||
num,
|
||||
cnsts,
|
||||
parents,
|
||||
options,
|
||||
interps,
|
||||
num_theory,
|
||||
theory);
|
||||
|
||||
if(!incremental)
|
||||
for(int i = 0; i < num-1; i++)
|
||||
Z3_persist_ast(ctx,interps[i],1);
|
||||
break;
|
||||
|
||||
case Z3_L_UNDEF:
|
||||
if(labels)
|
||||
*labels = Z3_get_relevant_labels(ctx);
|
||||
break;
|
||||
|
||||
case Z3_L_TRUE:
|
||||
if(labels)
|
||||
*labels = Z3_get_relevant_labels(ctx);
|
||||
break;
|
||||
}
|
||||
|
||||
profiling::timer_start("Z3 pop");
|
||||
if(!incremental)
|
||||
Z3_pop(ctx,1);
|
||||
profiling::timer_stop("Z3 pop");
|
||||
|
||||
profiling::timer_stop("Solve");
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
static std::ostringstream itp_err;
|
||||
|
||||
int Z3_check_interpolant(Z3_context ctx,
|
||||
int num,
|
||||
Z3_ast *cnsts,
|
||||
int *parents,
|
||||
Z3_ast *itp,
|
||||
const char **error,
|
||||
int num_theory,
|
||||
Z3_ast *theory){
|
||||
|
||||
ast_manager &_m = mk_c(ctx)->m();
|
||||
itp_err.clear();
|
||||
|
||||
// need a solver -- make one here, but how?
|
||||
params_ref p = params_ref::get_empty(); //FIXME
|
||||
scoped_ptr<solver_factory> sf(mk_smt_solver_factory());
|
||||
scoped_ptr<solver> sp((*(sf))(_m, p, false, true, false, symbol("AUFLIA")));
|
||||
|
||||
ptr_vector<ast> cnsts_vec(num); // get constraints in a vector
|
||||
for(int i = 0; i < num; i++){
|
||||
ast *a = to_ast(cnsts[i]);
|
||||
cnsts_vec[i] = a;
|
||||
}
|
||||
|
||||
ptr_vector<ast> itp_vec(num); // get interpolants in a vector
|
||||
for(int i = 0; i < num-1; i++){
|
||||
ast *a = to_ast(itp[i]);
|
||||
itp_vec[i] = a;
|
||||
}
|
||||
|
||||
::vector<int> parents_vec; // get parents in a vector
|
||||
if(parents){
|
||||
parents_vec.resize(num);
|
||||
for(int i = 0; i < num; i++)
|
||||
parents_vec[i] = parents[i];
|
||||
}
|
||||
|
||||
ptr_vector<ast> theory_vec; // get background theory in a vector
|
||||
if(theory){
|
||||
theory_vec.resize(num_theory);
|
||||
for(int i = 0; i < num_theory; i++)
|
||||
theory_vec[i] = to_ast(theory[i]);
|
||||
}
|
||||
|
||||
bool res = iz3check(_m,
|
||||
sp.get(),
|
||||
itp_err,
|
||||
cnsts_vec,
|
||||
parents_vec,
|
||||
itp_vec,
|
||||
theory_vec);
|
||||
|
||||
*error = res ? 0 : itp_err.str().c_str();
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static std::string Z3_profile_string;
|
||||
|
||||
Z3_string Z3_interpolation_profile(Z3_context ctx){
|
||||
std::ostringstream f;
|
||||
profiling::print(f);
|
||||
Z3_profile_string = f.str();
|
||||
return Z3_profile_string.c_str();
|
||||
}
|
||||
|
||||
|
||||
Z3_interpolation_options
|
||||
Z3_mk_interpolation_options(){
|
||||
return (Z3_interpolation_options) new interpolation_options_struct;
|
||||
}
|
||||
|
||||
void
|
||||
Z3_del_interpolation_options(Z3_interpolation_options opts){
|
||||
delete opts;
|
||||
}
|
||||
|
||||
void
|
||||
Z3_set_interpolation_option(Z3_interpolation_options opts,
|
||||
Z3_string name,
|
||||
Z3_string value){
|
||||
opts->map[name] = value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
static void tokenize(const std::string &str, std::vector<std::string> &tokens){
|
||||
for(unsigned i = 0; i < str.size();){
|
||||
if(str[i] == ' '){i++; continue;}
|
||||
unsigned beg = i;
|
||||
while(i < str.size() && str[i] != ' ')i++;
|
||||
if(i > beg)
|
||||
tokens.push_back(str.substr(beg,i-beg));
|
||||
}
|
||||
}
|
||||
|
||||
static void get_file_params(const char *filename, hash_map<std::string,std::string> ¶ms){
|
||||
std::ifstream f(filename);
|
||||
if(f){
|
||||
std::string first_line;
|
||||
std::getline(f,first_line);
|
||||
// std::cout << "first line: '" << first_line << "'" << std::endl;
|
||||
if(first_line.size() >= 2 && first_line[0] == ';' && first_line[1] == '!'){
|
||||
std::vector<std::string> tokens;
|
||||
tokenize(first_line.substr(2,first_line.size()-2),tokens);
|
||||
for(unsigned i = 0; i < tokens.size(); i++){
|
||||
std::string &tok = tokens[i];
|
||||
size_t eqpos = tok.find('=');
|
||||
if(eqpos >= 0 && eqpos < tok.size()){
|
||||
std::string left = tok.substr(0,eqpos);
|
||||
std::string right = tok.substr(eqpos+1,tok.size()-eqpos-1);
|
||||
params[left] = right;
|
||||
}
|
||||
}
|
||||
}
|
||||
f.close();
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
#if 0
|
||||
static void iZ3_write_seq(Z3_context ctx, int num, Z3_ast *cnsts, const char *filename, int num_theory, Z3_ast *theory){
|
||||
int num_fmlas = num+num_theory;
|
||||
std::vector<Z3_ast> fmlas(num_fmlas);
|
||||
if(num_theory)
|
||||
std::copy(theory,theory+num_theory,fmlas.begin());
|
||||
for(int i = 0; i < num_theory; i++)
|
||||
fmlas[i] = Z3_mk_implies(ctx,Z3_mk_true(ctx),fmlas[i]);
|
||||
std::copy(cnsts,cnsts+num,fmlas.begin()+num_theory);
|
||||
Z3_string smt = Z3_benchmark_to_smtlib_string(ctx,"none","AUFLIA","unknown","",num_fmlas-1,&fmlas[0],fmlas[num_fmlas-1]);
|
||||
std::ofstream f(filename);
|
||||
if(num_theory)
|
||||
f << ";! THEORY=" << num_theory << "\n";
|
||||
f << smt;
|
||||
f.close();
|
||||
}
|
||||
|
||||
void Z3_write_interpolation_problem(Z3_context ctx, int num, Z3_ast *cnsts, int *parents, const char *filename, int num_theory, Z3_ast *theory){
|
||||
if(!parents){
|
||||
iZ3_write_seq(ctx,num,cnsts,filename,num_theory,theory);
|
||||
return;
|
||||
}
|
||||
std::vector<Z3_ast> tcnsts(num);
|
||||
hash_map<int,Z3_ast> syms;
|
||||
for(int j = 0; j < num - 1; j++){
|
||||
std::ostringstream oss;
|
||||
oss << "$P" << j;
|
||||
std::string name = oss.str();
|
||||
Z3_symbol s = Z3_mk_string_symbol(ctx, name.c_str());
|
||||
Z3_ast symbol = Z3_mk_const(ctx, s, Z3_mk_bool_sort(ctx));
|
||||
syms[j] = symbol;
|
||||
tcnsts[j] = Z3_mk_implies(ctx,cnsts[j],symbol);
|
||||
}
|
||||
tcnsts[num-1] = Z3_mk_implies(ctx,cnsts[num-1],Z3_mk_false(ctx));
|
||||
for(int j = num-2; j >= 0; j--){
|
||||
int parent = parents[j];
|
||||
// assert(parent >= 0 && parent < num);
|
||||
tcnsts[parent] = Z3_mk_implies(ctx,syms[j],tcnsts[parent]);
|
||||
}
|
||||
iZ3_write_seq(ctx,num,&tcnsts[0],filename,num_theory,theory);
|
||||
}
|
||||
#else
|
||||
|
||||
|
||||
static Z3_ast and_vec(Z3_context ctx,svector<Z3_ast> &c){
|
||||
return (c.size() > 1) ? Z3_mk_and(ctx,c.size(),&c[0]) : c[0];
|
||||
}
|
||||
|
||||
static Z3_ast parents_vector_to_tree(Z3_context ctx, int num, Z3_ast *cnsts, int *parents){
|
||||
Z3_ast res;
|
||||
if(!parents){
|
||||
res = Z3_mk_interp(ctx,cnsts[0]);
|
||||
for(int i = 1; i < num-1; i++){
|
||||
Z3_ast bar[2] = {res,cnsts[i]};
|
||||
res = Z3_mk_interp(ctx,Z3_mk_and(ctx,2,bar));
|
||||
}
|
||||
if(num > 1){
|
||||
Z3_ast bar[2] = {res,cnsts[num-1]};
|
||||
res = Z3_mk_and(ctx,2,bar);
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::vector<svector<Z3_ast> > chs(num);
|
||||
for(int i = 0; i < num-1; i++){
|
||||
svector<Z3_ast> &c = chs[i];
|
||||
c.push_back(cnsts[i]);
|
||||
Z3_ast foo = Z3_mk_interp(ctx,and_vec(ctx,c));
|
||||
chs[parents[i]].push_back(foo);
|
||||
}
|
||||
{
|
||||
svector<Z3_ast> &c = chs[num-1];
|
||||
c.push_back(cnsts[num-1]);
|
||||
res = and_vec(ctx,c);
|
||||
}
|
||||
}
|
||||
Z3_inc_ref(ctx,res);
|
||||
return res;
|
||||
}
|
||||
|
||||
void Z3_write_interpolation_problem(Z3_context ctx, int num, Z3_ast *cnsts, int *parents, const char *filename, int num_theory, Z3_ast *theory){
|
||||
std::ofstream f(filename);
|
||||
if(num > 0){
|
||||
ptr_vector<expr> cnsts_vec(num); // get constraints in a vector
|
||||
for(int i = 0; i < num; i++){
|
||||
expr *a = to_expr(cnsts[i]);
|
||||
cnsts_vec[i] = a;
|
||||
}
|
||||
Z3_ast tree = parents_vector_to_tree(ctx,num,cnsts,parents);
|
||||
for(int i = 0; i < num_theory; i++){
|
||||
expr *a = to_expr(theory[i]);
|
||||
cnsts_vec.push_back(a);
|
||||
}
|
||||
iz3pp(mk_c(ctx)->m(),cnsts_vec,to_expr(tree),f);
|
||||
Z3_dec_ref(ctx,tree);
|
||||
}
|
||||
f.close();
|
||||
|
||||
#if 0
|
||||
|
||||
|
||||
if(!parents){
|
||||
iZ3_write_seq(ctx,num,cnsts,filename,num_theory,theory);
|
||||
return;
|
||||
}
|
||||
std::vector<Z3_ast> tcnsts(num);
|
||||
hash_map<int,Z3_ast> syms;
|
||||
for(int j = 0; j < num - 1; j++){
|
||||
std::ostringstream oss;
|
||||
oss << "$P" << j;
|
||||
std::string name = oss.str();
|
||||
Z3_symbol s = Z3_mk_string_symbol(ctx, name.c_str());
|
||||
Z3_ast symbol = Z3_mk_const(ctx, s, Z3_mk_bool_sort(ctx));
|
||||
syms[j] = symbol;
|
||||
tcnsts[j] = Z3_mk_implies(ctx,cnsts[j],symbol);
|
||||
}
|
||||
tcnsts[num-1] = Z3_mk_implies(ctx,cnsts[num-1],Z3_mk_false(ctx));
|
||||
for(int j = num-2; j >= 0; j--){
|
||||
int parent = parents[j];
|
||||
// assert(parent >= 0 && parent < num);
|
||||
tcnsts[parent] = Z3_mk_implies(ctx,syms[j],tcnsts[parent]);
|
||||
}
|
||||
iZ3_write_seq(ctx,num,&tcnsts[0],filename,num_theory,theory);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
static std::vector<Z3_ast> read_cnsts;
|
||||
static std::vector<int> read_parents;
|
||||
static std::ostringstream read_error;
|
||||
static std::string read_msg;
|
||||
static std::vector<Z3_ast> read_theory;
|
||||
|
||||
static bool iZ3_parse(Z3_context ctx, const char *filename, const char **error, svector<Z3_ast> &assertions){
|
||||
read_error.clear();
|
||||
try {
|
||||
std::string foo(filename);
|
||||
if(foo.size() >= 5 && foo.substr(foo.size()-5) == ".smt2"){
|
||||
Z3_ast ass = Z3_parse_smtlib2_file(ctx, filename, 0, 0, 0, 0, 0, 0);
|
||||
Z3_app app = Z3_to_app(ctx,ass);
|
||||
int nconjs = Z3_get_app_num_args(ctx,app);
|
||||
assertions.resize(nconjs);
|
||||
for(int k = 0; k < nconjs; k++)
|
||||
assertions[k] = Z3_get_app_arg(ctx,app,k);
|
||||
}
|
||||
else {
|
||||
Z3_parse_smtlib_file(ctx, filename, 0, 0, 0, 0, 0, 0);
|
||||
int numa = Z3_get_smtlib_num_assumptions(ctx);
|
||||
int numf = Z3_get_smtlib_num_formulas(ctx);
|
||||
int num = numa + numf;
|
||||
|
||||
assertions.resize(num);
|
||||
for(int j = 0; j < num; j++){
|
||||
if(j < numa)
|
||||
assertions[j] = Z3_get_smtlib_assumption(ctx,j);
|
||||
else
|
||||
assertions[j] = Z3_get_smtlib_formula(ctx,j-numa);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(...) {
|
||||
read_error << "SMTLIB parse error: " << Z3_get_smtlib_error(ctx);
|
||||
read_msg = read_error.str();
|
||||
*error = read_msg.c_str();
|
||||
return false;
|
||||
}
|
||||
Z3_set_error_handler(ctx, 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int Z3_read_interpolation_problem(Z3_context ctx, int *_num, Z3_ast **cnsts, int **parents, const char *filename, const char **error, int *ret_num_theory, Z3_ast **theory ){
|
||||
|
||||
hash_map<std::string,std::string> file_params;
|
||||
get_file_params(filename,file_params);
|
||||
|
||||
unsigned num_theory = 0;
|
||||
if(file_params.find("THEORY") != file_params.end())
|
||||
num_theory = atoi(file_params["THEORY"].c_str());
|
||||
|
||||
svector<Z3_ast> assertions;
|
||||
if(!iZ3_parse(ctx,filename,error,assertions))
|
||||
return false;
|
||||
|
||||
if(num_theory > assertions.size())
|
||||
num_theory = assertions.size();
|
||||
unsigned num = assertions.size() - num_theory;
|
||||
|
||||
read_cnsts.resize(num);
|
||||
read_parents.resize(num);
|
||||
read_theory.resize(num_theory);
|
||||
|
||||
for(unsigned j = 0; j < num_theory; j++)
|
||||
read_theory[j] = assertions[j];
|
||||
for(unsigned j = 0; j < num; j++)
|
||||
read_cnsts[j] = assertions[j+num_theory];
|
||||
|
||||
if(ret_num_theory)
|
||||
*ret_num_theory = num_theory;
|
||||
if(theory)
|
||||
*theory = &read_theory[0];
|
||||
|
||||
if(!parents){
|
||||
*_num = num;
|
||||
*cnsts = &read_cnsts[0];
|
||||
return true;
|
||||
}
|
||||
|
||||
for(unsigned j = 0; j < num; j++)
|
||||
read_parents[j] = SHRT_MAX;
|
||||
|
||||
hash_map<Z3_ast,int> pred_map;
|
||||
|
||||
for(unsigned j = 0; j < num; j++){
|
||||
Z3_ast lhs = 0, rhs = read_cnsts[j];
|
||||
|
||||
if(Z3_get_decl_kind(ctx,Z3_get_app_decl(ctx,Z3_to_app(ctx,rhs))) == Z3_OP_IMPLIES){
|
||||
Z3_app app1 = Z3_to_app(ctx,rhs);
|
||||
Z3_ast lhs1 = Z3_get_app_arg(ctx,app1,0);
|
||||
Z3_ast rhs1 = Z3_get_app_arg(ctx,app1,1);
|
||||
if(Z3_get_decl_kind(ctx,Z3_get_app_decl(ctx,Z3_to_app(ctx,lhs1))) == Z3_OP_AND){
|
||||
Z3_app app2 = Z3_to_app(ctx,lhs1);
|
||||
int nconjs = Z3_get_app_num_args(ctx,app2);
|
||||
for(int k = nconjs - 1; k >= 0; --k)
|
||||
rhs1 = Z3_mk_implies(ctx,Z3_get_app_arg(ctx,app2,k),rhs1);
|
||||
rhs = rhs1;
|
||||
}
|
||||
}
|
||||
|
||||
while(1){
|
||||
Z3_app app = Z3_to_app(ctx,rhs);
|
||||
Z3_func_decl func = Z3_get_app_decl(ctx,app);
|
||||
Z3_decl_kind dk = Z3_get_decl_kind(ctx,func);
|
||||
if(dk == Z3_OP_IMPLIES){
|
||||
if(lhs){
|
||||
Z3_ast child = lhs;
|
||||
if(pred_map.find(child) == pred_map.end()){
|
||||
read_error << "formula " << j+1 << ": unknown: " << Z3_ast_to_string(ctx,child);
|
||||
goto fail;
|
||||
}
|
||||
int child_num = pred_map[child];
|
||||
if(read_parents[child_num] != SHRT_MAX){
|
||||
read_error << "formula " << j+1 << ": multiple reference: " << Z3_ast_to_string(ctx,child);
|
||||
goto fail;
|
||||
}
|
||||
read_parents[child_num] = j;
|
||||
}
|
||||
lhs = Z3_get_app_arg(ctx,app,0);
|
||||
rhs = Z3_get_app_arg(ctx,app,1);
|
||||
}
|
||||
else {
|
||||
if(!lhs){
|
||||
read_error << "formula " << j+1 << ": should be (implies {children} fmla parent)";
|
||||
goto fail;
|
||||
}
|
||||
read_cnsts[j] = lhs;
|
||||
Z3_ast name = rhs;
|
||||
if(pred_map.find(name) != pred_map.end()){
|
||||
read_error << "formula " << j+1 << ": duplicate symbol";
|
||||
goto fail;
|
||||
}
|
||||
pred_map[name] = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(unsigned j = 0; j < num-1; j++)
|
||||
if(read_parents[j] == SHRT_MIN){
|
||||
read_error << "formula " << j+1 << ": unreferenced";
|
||||
goto fail;
|
||||
}
|
||||
|
||||
*_num = num;
|
||||
*cnsts = &read_cnsts[0];
|
||||
*parents = &read_parents[0];
|
||||
return true;
|
||||
|
||||
fail:
|
||||
read_msg = read_error.str();
|
||||
*error = read_msg.c_str();
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
|
@ -36,4 +36,3 @@ using System.Security.Permissions;
|
|||
// [assembly: AssemblyVersion("4.2.0.0")]
|
||||
[assembly: AssemblyVersion("4.3.2.0")]
|
||||
[assembly: AssemblyFileVersion("4.3.2.0")]
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@ public class BitVecNum extends BitVecExpr
|
|||
{
|
||||
Native.LongPtr res = new Native.LongPtr();
|
||||
if (Native.getNumeralInt64(getContext().nCtx(), getNativeObject(), res) ^ true)
|
||||
throw new Z3Exception("Numeral is not an int64");
|
||||
throw new Z3Exception("Numeral is not a long");
|
||||
return res.value;
|
||||
}
|
||||
|
||||
|
|
|
@ -6424,7 +6424,7 @@ class Tactic:
|
|||
_z3_assert(isinstance(goal, Goal) or isinstance(goal, BoolRef), "Z3 Goal or Boolean expressions expected")
|
||||
goal = _to_goal(goal)
|
||||
if len(arguments) > 0 or len(keywords) > 0:
|
||||
p = args2params(arguments, keywords, a.ctx)
|
||||
p = args2params(arguments, keywords, self.ctx)
|
||||
return ApplyResult(Z3_tactic_apply_ex(self.ctx.ref(), self.tactic, goal.goal, p.params), self.ctx)
|
||||
else:
|
||||
return ApplyResult(Z3_tactic_apply(self.ctx.ref(), self.tactic, goal.goal), self.ctx)
|
||||
|
|
218
src/api/z3_api.h
218
src/api/z3_api.h
|
@ -251,6 +251,8 @@ typedef enum
|
|||
- Z3_OP_OEQ Binary equivalence modulo namings. This binary predicate is used in proof terms.
|
||||
It captures equisatisfiability and equivalence modulo renamings.
|
||||
|
||||
- Z3_OP_INTERP Marks a sub-formula for interpolation.
|
||||
|
||||
- Z3_OP_ANUM Arithmetic numeral.
|
||||
|
||||
- Z3_OP_AGNUM Arithmetic algebraic numeral. Algebraic numbers are used to represent irrational numbers in Z3.
|
||||
|
@ -901,6 +903,7 @@ typedef enum {
|
|||
Z3_OP_NOT,
|
||||
Z3_OP_IMPLIES,
|
||||
Z3_OP_OEQ,
|
||||
Z3_OP_INTERP,
|
||||
|
||||
// Arithmetic
|
||||
Z3_OP_ANUM = 0x200,
|
||||
|
@ -2121,6 +2124,16 @@ END_MLAPI_EXCLUDE
|
|||
*/
|
||||
Z3_ast Z3_API Z3_mk_not(__in Z3_context c, __in Z3_ast a);
|
||||
|
||||
/**
|
||||
\brief \mlh mk_interp c a \endmlh
|
||||
Create an AST node marking a formula position for interpolation.
|
||||
|
||||
The node \c a must have Boolean sort.
|
||||
|
||||
def_API('Z3_mk_interp', AST, (_in(CONTEXT), _in(AST)))
|
||||
*/
|
||||
Z3_ast Z3_API Z3_mk_interp(__in Z3_context c, __in Z3_ast a);
|
||||
|
||||
/**
|
||||
\brief \mlh mk_ite c t1 t2 t2 \endmlh
|
||||
Create an AST node representing an if-then-else: <tt>ite(t1, t2,
|
||||
|
@ -7863,6 +7876,211 @@ END_MLAPI_EXCLUDE
|
|||
|
||||
/*@}*/
|
||||
|
||||
/**
|
||||
@name Interpolation
|
||||
*/
|
||||
/*@{*/
|
||||
|
||||
/** \brief This function generates a Z3 context suitable for generation of
|
||||
interpolants. Formulas can be generated as abstract syntx trees in
|
||||
this context using the Z3 C API.
|
||||
|
||||
Interpolants are also generated as AST's in this context.
|
||||
|
||||
If cfg is non-null, it will be used as the base configuration
|
||||
for the Z3 context. This makes it possible to set Z3 options
|
||||
to be used during interpolation. This feature should be used
|
||||
with some caution however, as it may be that certain Z3 options
|
||||
are incompatible with interpolation.
|
||||
|
||||
def_API('Z3_mk_interpolation_context', CONTEXT, (_in(CONFIG),))
|
||||
|
||||
*/
|
||||
|
||||
Z3_context Z3_API Z3_mk_interpolation_context(__in Z3_config cfg);
|
||||
|
||||
|
||||
/** Constant reprepresenting a root of a formula tree for tree interpolation */
|
||||
#define IZ3_ROOT SHRT_MAX
|
||||
|
||||
/** This function uses Z3 to determine satisfiability of a set of
|
||||
constraints. If UNSAT, an interpolant is returned, based on the
|
||||
refutation generated by Z3. If SAT, a model is returned.
|
||||
|
||||
If "parents" is non-null, computes a tree interpolant. The tree is
|
||||
defined by the array "parents". This array maps each formula in
|
||||
the tree to its parent, where formulas are indicated by their
|
||||
integer index in "cnsts". The parent of formula n must have index
|
||||
greater than n. The last formula is the root of the tree. Its
|
||||
parent entry should be the constant IZ3_ROOT.
|
||||
|
||||
If "parents" is null, computes a sequence interpolant.
|
||||
|
||||
\param ctx The Z3 context. Must be generated by iz3_mk_context
|
||||
\param num The number of constraints in the sequence
|
||||
\param cnsts Array of constraints (AST's in context ctx)
|
||||
\param parents The parents vector defining the tree structure
|
||||
\param options Interpolation options (may be NULL)
|
||||
\param interps Array to return interpolants (size at least num-1, may be NULL)
|
||||
\param model Returns a Z3 model if constraints SAT (may be NULL)
|
||||
\param labels Returns relevant labels if SAT (may be NULL)
|
||||
\param incremental
|
||||
|
||||
VERY IMPORTANT: All the Z3 formulas in cnsts must be in Z3
|
||||
context ctx. The model and interpolants returned are also
|
||||
in this context.
|
||||
|
||||
The return code is as in Z3_check_assumptions, that is,
|
||||
|
||||
Z3_L_FALSE = constraints UNSAT (interpolants returned)
|
||||
Z3_L_TRUE = constraints SAT (model returned)
|
||||
Z3_L_UNDEF = Z3 produced no result, or interpolation not possible
|
||||
|
||||
Currently, this function supports integer and boolean variables,
|
||||
as well as arrays over these types, with linear arithmetic,
|
||||
uninterpreted functions and quantifiers over integers (that is
|
||||
AUFLIA). Interpolants are produced in AUFLIA. However, some
|
||||
uses of array operations may cause quantifiers to appear in the
|
||||
interpolants even when there are no quantifiers in the input formulas.
|
||||
Although quantifiers may appear in the input formulas, Z3 may give up in
|
||||
this case, returning Z3_L_UNDEF.
|
||||
|
||||
If "incremental" is true, cnsts must contain exactly the set of
|
||||
formulas that are currently asserted in the context. If false,
|
||||
there must be no formulas currently asserted in the context.
|
||||
Setting "incremental" to true makes it posisble to incrementally
|
||||
add and remove constraints from the context until the context
|
||||
becomes UNSAT, at which point an interpolant is computed. Caution
|
||||
must be used, however. Before popping the context, if you wish to
|
||||
keep the interolant formulas, you *must* preserve them by using
|
||||
Z3_persist_ast. Also, if you want to simplify the interpolant
|
||||
formulas using Z3_simplify, you must first pop all of the
|
||||
assertions in the context (or use a different context). Otherwise,
|
||||
the formulas will be simplified *relative* to these constraints,
|
||||
which is almost certainly not what you want.
|
||||
|
||||
|
||||
Current limitations on tree interpolants. In a tree interpolation
|
||||
problem, each constant (0-ary function symbol) must occur only
|
||||
along one path from root to leaf. Function symbols (of arity > 0)
|
||||
are considered to have global scope (i.e., may appear in any
|
||||
interpolant formula).
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
Z3_lbool Z3_API Z3_interpolate(__in Z3_context ctx,
|
||||
__in int num,
|
||||
__in_ecount(num) Z3_ast *cnsts,
|
||||
__in_ecount(num) unsigned *parents,
|
||||
__in Z3_params options,
|
||||
__out_ecount(num-1) Z3_ast *interps,
|
||||
__out Z3_model *model,
|
||||
__out Z3_literals *labels,
|
||||
__in int incremental,
|
||||
__in int num_theory,
|
||||
__in_ecount(num_theory) Z3_ast *theory);
|
||||
|
||||
/** Return a string summarizing cumulative time used for
|
||||
interpolation. This string is purely for entertainment purposes
|
||||
and has no semantics.
|
||||
|
||||
\param ctx The context (currently ignored)
|
||||
|
||||
def_API('Z3_interpolation_profile', STRING, (_in(CONTEXT),))
|
||||
*/
|
||||
|
||||
Z3_string Z3_API Z3_interpolation_profile(__in Z3_context ctx);
|
||||
|
||||
/**
|
||||
\brief Read an interpolation problem from file.
|
||||
|
||||
\param ctx The Z3 context. This resets the error handler of ctx.
|
||||
\param filename The file name to read.
|
||||
\param num Returns length of sequence.
|
||||
\param cnsts Returns sequence of formulas (do not free)
|
||||
\param parents Returns the parents vector (or NULL for sequence)
|
||||
\param error Returns an error message in case of failure (do not free the string)
|
||||
|
||||
Returns true on success.
|
||||
|
||||
File formats: Currently two formats are supported, based on
|
||||
SMT-LIB2. For sequence interpolants, the sequence of constraints is
|
||||
represented by the sequence of "assert" commands in the file.
|
||||
|
||||
For tree interpolants, one symbol of type bool is associated to
|
||||
each vertex of the tree. For each vertex v there is an "assert"
|
||||
of the form:
|
||||
|
||||
(implies (and c1 ... cn f) v)
|
||||
|
||||
where c1 .. cn are the children of v (which must precede v in the file)
|
||||
and f is the formula assiciated to node v. The last formula in the
|
||||
file is the root vertex, and is represented by the predicate "false".
|
||||
|
||||
A solution to a tree interpolation problem can be thought of as a
|
||||
valuation of the vertices that makes all the implications true
|
||||
where each value is represented using the common symbols between
|
||||
the formulas in the subtree and the remainder of the formulas.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
int Z3_API Z3_read_interpolation_problem(__in Z3_context ctx,
|
||||
__out int *num,
|
||||
__out_ecount(*num) Z3_ast **cnsts,
|
||||
__out_ecount(*num) int **parents,
|
||||
__in const char *filename,
|
||||
__out const char **error,
|
||||
__out int *num_theory,
|
||||
__out_ecount(*num_theory) Z3_ast **theory);
|
||||
|
||||
|
||||
|
||||
/** Check the correctness of an interpolant. The Z3 context must
|
||||
have no constraints asserted when this call is made. That means
|
||||
that after interpolating, you must first fully pop the Z3
|
||||
context before calling this. See Z3_interpolate for meaning of parameters.
|
||||
|
||||
\param ctx The Z3 context. Must be generated by Z3_mk_interpolation_context
|
||||
\param num The number of constraints in the sequence
|
||||
\param cnsts Array of constraints (AST's in context ctx)
|
||||
\param parents The parents vector (or NULL for sequence)
|
||||
\param interps The interpolant to check
|
||||
\param error Returns an error message if interpolant incorrect (do not free the string)
|
||||
|
||||
Return value is Z3_L_TRUE if interpolant is verified, Z3_L_FALSE if
|
||||
incorrect, and Z3_L_UNDEF if unknown.
|
||||
|
||||
*/
|
||||
|
||||
int Z3_API Z3_check_interpolant(Z3_context ctx, int num, Z3_ast *cnsts, int *parents, Z3_ast *interps, const char **error,
|
||||
int num_theory, Z3_ast *theory);
|
||||
|
||||
/** Write an interpolation problem to file suitable for reading with
|
||||
Z3_read_interpolation_problem. The output file is a sequence
|
||||
of SMT-LIB2 format commands, suitable for reading with command-line Z3
|
||||
or other interpolating solvers.
|
||||
|
||||
\param ctx The Z3 context. Must be generated by z3_mk_interpolation_context
|
||||
\param num The number of constraints in the sequence
|
||||
\param cnsts Array of constraints
|
||||
\param parents The parents vector (or NULL for sequence)
|
||||
\param filename The file name to write
|
||||
|
||||
*/
|
||||
|
||||
void Z3_API Z3_write_interpolation_problem(Z3_context ctx,
|
||||
int num,
|
||||
Z3_ast *cnsts,
|
||||
int *parents,
|
||||
const char *filename,
|
||||
int num_theory,
|
||||
Z3_ast *theory);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue