Z3_ast parse_smtlib2_stream(bool exec, Z3_context c, std::istream& is, unsigned num_sorts, Z3_symbol const sort_names[], Z3_sort const sorts[], unsigned num_decls, Z3_symbol const decl_names[], Z3_func_decl const decls[]) { Z3_TRY; cmd_context ctx(&mk_c(c)->fparams(), false, &(mk_c(c)->m())); ctx.set_ignore_check(true); if (exec) { ctx.set_solver(alloc(z3_context_solver, *mk_c(c))); } for (unsigned i = 0; i < num_decls; ++i) { ctx.insert(to_symbol(decl_names[i]), to_func_decl(decls[i])); } for (unsigned i = 0; i < num_sorts; ++i) { psort* ps = ctx.pm().mk_psort_cnst(to_sort(sorts[i])); ctx.insert(ctx.pm().mk_psort_user_decl(0, to_symbol(sort_names[i]), ps)); } if (!parse_smt2_commands(ctx, is)) { SET_ERROR_CODE(Z3_PARSER_ERROR); return of_ast(mk_c(c)->m().mk_true()); } ptr_vector<expr>::const_iterator it = ctx.begin_assertions(); ptr_vector<expr>::const_iterator end = ctx.end_assertions(); unsigned size = static_cast<unsigned>(end - it); return of_ast(mk_c(c)->mk_and(size, it)); Z3_CATCH_RETURN(0); }
Z3_ast Z3_API Z3_get_model_func_entry_value(Z3_context c, Z3_model m, unsigned i, unsigned j) { Z3_TRY; LOG_Z3_get_model_func_entry_value(c, m, i, j); RESET_ERROR_CODE(); CHECK_NON_NULL(m, 0); if (j >= get_model_func_num_entries_core(c, m, i)) { SET_ERROR_CODE(Z3_IOB); RETURN_Z3(0); } Z3_func_decl d = get_model_func_decl_core(c, m, i); if (d) { model * _m = to_model_ref(m); func_interp * g = _m->get_func_interp(to_func_decl(d)); if (g && j < g->num_entries()) { func_entry const* e = g->get_entry(j); expr* a = e->get_result(); mk_c(c)->save_ast_trail(a); RETURN_Z3(of_ast(a)); } SET_ERROR_CODE(Z3_IOB); RETURN_Z3(0); } RETURN_Z3(0); Z3_CATCH_RETURN(0); }
bool Z3_API Z3_model_has_interp(Z3_context c, Z3_model m, Z3_func_decl a) { Z3_TRY; LOG_Z3_model_has_interp(c, m, a); CHECK_NON_NULL(m, 0); return to_model_ref(m)->has_interpretation(to_func_decl(a)); Z3_CATCH_RETURN(false); }
bool fpa_util::contains_floats(ast * a) { switch (a->get_kind()) { case AST_APP: { app * aa = to_app(a); if (contains_floats(aa->get_decl())) return true; else for (unsigned i = 0; i < aa->get_num_args(); i++) if (contains_floats(aa->get_arg(i))) return true; break; } case AST_VAR: return contains_floats(to_var(a)->get_sort()); break; case AST_QUANTIFIER: { quantifier * q = to_quantifier(a); for (unsigned i = 0; i < q->get_num_children(); i++) if (contains_floats(q->get_child(i))) return true; for (unsigned i = 0; i < q->get_num_decls(); i++) if (contains_floats(q->get_decl_sort(i))) return true; if (contains_floats(q->get_expr())) return true; break; } case AST_SORT: { sort * s = to_sort(a); if (is_float(s) || is_rm(s)) return true; else { for (unsigned i = 0; i < s->get_num_parameters(); i++) { parameter const & pi = s->get_parameter(i); if (pi.is_ast() && contains_floats(pi.get_ast())) return true; } } break; } case AST_FUNC_DECL: { func_decl * f = to_func_decl(a); for (unsigned i = 0; i < f->get_arity(); i++) if (contains_floats(f->get_domain(i))) return true; if (contains_floats(f->get_range())) return true; for (unsigned i = 0; i < f->get_num_parameters(); i++) { parameter const & pi = f->get_parameter(i); if (pi.is_ast() && contains_floats(pi.get_ast())) return true; } break; } default: UNREACHABLE(); } return false; }
unsigned Z3_API Z3_fixedpoint_get_num_levels(Z3_context c, Z3_fixedpoint d, Z3_func_decl pred) { Z3_TRY; LOG_Z3_fixedpoint_get_num_levels(c, d, pred); RESET_ERROR_CODE(); return to_fixedpoint_ref(d)->get_num_levels(to_func_decl(pred)); Z3_CATCH_RETURN(0) }
void Z3_API Z3_fixedpoint_add_cover(Z3_context c, Z3_fixedpoint d, int level, Z3_func_decl pred, Z3_ast property) { Z3_TRY; LOG_Z3_fixedpoint_add_cover(c, d, level, pred, property); RESET_ERROR_CODE(); to_fixedpoint_ref(d)->add_cover(level, to_func_decl(pred), to_expr(property)); Z3_CATCH; }
void Z3_API Z3_fixedpoint_add_invariant(Z3_context c, Z3_fixedpoint d, Z3_func_decl pred, Z3_ast property) { Z3_TRY; LOG_Z3_fixedpoint_add_invariant(c, d, pred, property); RESET_ERROR_CODE(); to_fixedpoint_ref(d)->ctx ().add_invariant(to_func_decl(pred), to_expr(property)); Z3_CATCH; }
void Z3_API Z3_fixedpoint_add_fact(Z3_context c, Z3_fixedpoint d, Z3_func_decl r, unsigned num_args, unsigned args[]) { Z3_TRY; LOG_Z3_fixedpoint_add_fact(c, d, r, num_args, args); RESET_ERROR_CODE(); to_fixedpoint_ref(d)->add_table_fact(to_func_decl(r), num_args, args); Z3_CATCH; }
Z3_ast Z3_API Z3_fixedpoint_get_cover_delta(Z3_context c, Z3_fixedpoint d, int level, Z3_func_decl pred) { Z3_TRY; LOG_Z3_fixedpoint_get_cover_delta(c, d, level, pred); RESET_ERROR_CODE(); expr_ref r = to_fixedpoint_ref(d)->get_cover_delta(level, to_func_decl(pred)); mk_c(c)->save_ast_trail(r); RETURN_Z3(of_expr(r.get())); Z3_CATCH_RETURN(0); }
Z3_ast Z3_API Z3_fixedpoint_get_reachable(Z3_context c, Z3_fixedpoint d, Z3_func_decl pred) { Z3_TRY; LOG_Z3_fixedpoint_get_reachable(c, d, pred); RESET_ERROR_CODE(); expr_ref r = to_fixedpoint_ref(d)->ctx().get_reachable(to_func_decl(pred)); mk_c(c)->save_ast_trail(r); RETURN_Z3(of_expr(r.get())); Z3_CATCH_RETURN(nullptr); }
Z3_bool Z3_API Z3_model_has_interp(Z3_context c, Z3_model m, Z3_func_decl a) { Z3_TRY; LOG_Z3_model_has_interp(c, m, a); CHECK_NON_NULL(m, 0); if (to_model_ref(m)->has_interpretation(to_func_decl(a))) { return Z3_TRUE; } else { return Z3_FALSE; } Z3_CATCH_RETURN(Z3_FALSE); }
Z3_func_decl Z3_API Z3_get_as_array_func_decl(Z3_context c, Z3_ast a) { Z3_TRY; LOG_Z3_get_as_array_func_decl(c, a); RESET_ERROR_CODE(); if (a && is_expr(to_ast(a)) && is_app_of(to_expr(a), mk_c(c)->get_array_fid(), OP_AS_ARRAY)) { RETURN_Z3(of_func_decl(to_func_decl(to_app(a)->get_decl()->get_parameter(0).get_ast()))); } else { SET_ERROR_CODE(Z3_INVALID_ARG, nullptr); RETURN_Z3(nullptr); } Z3_CATCH_RETURN(nullptr); }
Z3_ast Z3_API Z3_model_get_const_interp(Z3_context c, Z3_model m, Z3_func_decl a) { Z3_TRY; LOG_Z3_model_get_const_interp(c, m, a); RESET_ERROR_CODE(); CHECK_NON_NULL(m, 0); expr * r = to_model_ref(m)->get_const_interp(to_func_decl(a)); if (!r) { SET_ERROR_CODE(Z3_INVALID_ARG); RETURN_Z3(0); } RETURN_Z3(of_expr(r)); Z3_CATCH_RETURN(0); }
Z3_ast_opt Z3_API Z3_model_get_const_interp(Z3_context c, Z3_model m, Z3_func_decl a) { Z3_TRY; LOG_Z3_model_get_const_interp(c, m, a); RESET_ERROR_CODE(); CHECK_NON_NULL(m, nullptr); expr * r = to_model_ref(m)->get_const_interp(to_func_decl(a)); if (!r) { RETURN_Z3(nullptr); } mk_c(c)->save_ast_trail(r); RETURN_Z3(of_expr(r)); Z3_CATCH_RETURN(nullptr); }
Z3_func_interp Z3_API Z3_add_func_interp(Z3_context c, Z3_model m, Z3_func_decl f, Z3_ast else_val) { Z3_TRY; LOG_Z3_add_func_interp(c, m, f, else_val); RESET_ERROR_CODE(); func_decl* d = to_func_decl(f); model* mdl = to_model_ref(m); Z3_func_interp_ref * f_ref = alloc(Z3_func_interp_ref, *mk_c(c), mdl); f_ref->m_func_interp = alloc(func_interp, mk_c(c)->m(), d->get_arity()); mk_c(c)->save_object(f_ref); mdl->register_decl(d, f_ref->m_func_interp); f_ref->m_func_interp->set_else(to_expr(else_val)); RETURN_Z3(of_func_interp(f_ref)); Z3_CATCH_RETURN(nullptr); }
void Z3_API Z3_add_const_interp(Z3_context c, Z3_model m, Z3_func_decl f, Z3_ast a) { Z3_TRY; LOG_Z3_add_const_interp(c, m, f, a); RESET_ERROR_CODE(); func_decl* d = to_func_decl(f); if (!d || d->get_arity() != 0) { SET_ERROR_CODE(Z3_INVALID_ARG, nullptr); } else { model* mdl = to_model_ref(m); mdl->register_decl(d, to_expr(a)); } Z3_CATCH; }
void decl_collector::visit(ast* n) { ptr_vector<ast> todo; todo.push_back(n); while (!todo.empty()) { n = todo.back(); todo.pop_back(); if (!m_visited.is_marked(n)) { m_visited.mark(n, true); switch(n->get_kind()) { case AST_APP: { app * a = to_app(n); for (unsigned i = 0; i < a->get_num_args(); ++i) { todo.push_back(a->get_arg(i)); } todo.push_back(a->get_decl()); break; } case AST_QUANTIFIER: { quantifier * q = to_quantifier(n); unsigned num_decls = q->get_num_decls(); for (unsigned i = 0; i < num_decls; ++i) { todo.push_back(q->get_decl_sort(i)); } todo.push_back(q->get_expr()); for (unsigned i = 0; i < q->get_num_patterns(); ++i) { todo.push_back(q->get_pattern(i)); } break; } case AST_SORT: visit_sort(to_sort(n)); break; case AST_FUNC_DECL: { func_decl * d = to_func_decl(n); for (unsigned i = 0; i < d->get_arity(); ++i) { todo.push_back(d->get_domain(i)); } todo.push_back(d->get_range()); visit_func(d); break; } case AST_VAR: break; default: UNREACHABLE(); } } } }
void decl_collector::visit(ast* n) { datatype_util util(m()); m_todo.push_back(n); while (!m_todo.empty()) { n = m_todo.back(); m_todo.pop_back(); if (!m_visited.is_marked(n)) { switch(n->get_kind()) { case AST_APP: { app * a = to_app(n); for (expr* arg : *a) { m_todo.push_back(arg); } m_todo.push_back(a->get_decl()); break; } case AST_QUANTIFIER: { quantifier * q = to_quantifier(n); unsigned num_decls = q->get_num_decls(); for (unsigned i = 0; i < num_decls; ++i) { m_todo.push_back(q->get_decl_sort(i)); } m_todo.push_back(q->get_expr()); for (unsigned i = 0; i < q->get_num_patterns(); ++i) { m_todo.push_back(q->get_pattern(i)); } break; } case AST_SORT: visit_sort(to_sort(n)); break; case AST_FUNC_DECL: { func_decl * d = to_func_decl(n); for (sort* srt : *d) { m_todo.push_back(srt); } m_todo.push_back(d->get_range()); visit_func(d); break; } case AST_VAR: break; default: UNREACHABLE(); } m_visited.mark(n, true); } } }
void Z3_API Z3_fixedpoint_set_predicate_representation( Z3_context c, Z3_fixedpoint d, Z3_func_decl f, unsigned num_relations, Z3_symbol const relation_kinds[]) { Z3_TRY; LOG_Z3_fixedpoint_set_predicate_representation(c, d, f, num_relations, relation_kinds); svector<symbol> kinds; for (unsigned i = 0; i < num_relations; ++i) { kinds.push_back(to_symbol(relation_kinds[i])); } to_fixedpoint_ref(d)->ctx().set_predicate_representation(to_func_decl(f), num_relations, kinds.c_ptr()); Z3_CATCH; }
unsigned get_model_func_num_entries_core(Z3_context c, Z3_model m, unsigned i) { RESET_ERROR_CODE(); CHECK_NON_NULL(m, 0); Z3_func_decl d = get_model_func_decl_core(c, m, i); if (d) { model * _m = to_model_ref(m); func_interp * g = _m->get_func_interp(to_func_decl(d)); if (g) { return g->num_entries(); } SET_ERROR_CODE(Z3_IOB, nullptr); return 0; } return 0; }
Z3_func_interp Z3_API Z3_model_get_func_interp(Z3_context c, Z3_model m, Z3_func_decl f) { Z3_TRY; LOG_Z3_model_get_func_interp(c, m, f); RESET_ERROR_CODE(); CHECK_NON_NULL(m, nullptr); func_interp * _fi = to_model_ref(m)->get_func_interp(to_func_decl(f)); if (!_fi) { SET_ERROR_CODE(Z3_INVALID_ARG, nullptr); RETURN_Z3(nullptr); } Z3_func_interp_ref * fi = alloc(Z3_func_interp_ref, *mk_c(c), to_model_ref(m)); fi->m_func_interp = _fi; mk_c(c)->save_object(fi); RETURN_Z3(of_func_interp(fi)); Z3_CATCH_RETURN(nullptr); }
unsigned get_model_func_entry_num_args_core(Z3_context c, Z3_model m, unsigned i, unsigned j) { RESET_ERROR_CODE(); CHECK_NON_NULL(m, 0); if (j >= get_model_func_num_entries_core(c, m, i)) { SET_ERROR_CODE(Z3_IOB); return 0; } Z3_func_decl d = get_model_func_decl_core(c, m, i); if (d) { model * _m = to_model_ref(m); func_interp * g = _m->get_func_interp(to_func_decl(d)); return g->get_arity(); } return 0; }
void init_smtlib_parser(Z3_context c, unsigned num_sorts, Z3_symbol const sort_names[], Z3_sort const types[], unsigned num_decls, Z3_symbol const decl_names[], Z3_func_decl const decls[]) { mk_c(c)->reset_parser(); mk_c(c)->m_smtlib_parser = smtlib::parser::create(mk_c(c)->m()); mk_c(c)->m_smtlib_parser->initialize_smtlib(); smtlib::symtable * table = mk_c(c)->m_smtlib_parser->get_benchmark()->get_symtable(); for (unsigned i = 0; i < num_sorts; i++) { table->insert(to_symbol(sort_names[i]), to_sort(types[i])); } for (unsigned i = 0; i < num_decls; i++) { table->insert(to_symbol(decl_names[i]), to_func_decl(decls[i])); } }
Z3_ast parse_smtlib2_stream(bool exec, Z3_context c, std::istream& is, unsigned num_sorts, Z3_symbol const sort_names[], Z3_sort const sorts[], unsigned num_decls, Z3_symbol const decl_names[], Z3_func_decl const decls[]) { Z3_TRY; scoped_ptr<cmd_context> ctx = alloc(cmd_context, false, &(mk_c(c)->m())); ctx->set_ignore_check(true); for (unsigned i = 0; i < num_decls; ++i) { ctx->insert(to_symbol(decl_names[i]), to_func_decl(decls[i])); } for (unsigned i = 0; i < num_sorts; ++i) { sort* srt = to_sort(sorts[i]); symbol name(to_symbol(sort_names[i])); if (!ctx->find_psort_decl(name)) { psort* ps = ctx->pm().mk_psort_cnst(srt); ctx->insert(ctx->pm().mk_psort_user_decl(0, name, ps)); } } std::stringstream errstrm; ctx->set_regular_stream(errstrm); try { if (!parse_smt2_commands(*ctx.get(), is)) { ctx = nullptr; mk_c(c)->m_parser_error_buffer = errstrm.str(); SET_ERROR_CODE(Z3_PARSER_ERROR); return of_ast(mk_c(c)->m().mk_true()); } } catch (z3_exception& e) { errstrm << e.msg(); mk_c(c)->m_parser_error_buffer = errstrm.str(); ctx = nullptr; SET_ERROR_CODE(Z3_PARSER_ERROR); return of_ast(mk_c(c)->m().mk_true()); } ptr_vector<expr>::const_iterator it = ctx->begin_assertions(); ptr_vector<expr>::const_iterator end = ctx->end_assertions(); unsigned size = static_cast<unsigned>(end - it); return of_ast(mk_c(c)->mk_and(size, it)); Z3_CATCH_RETURN(0); }
func_decl * datatype_decl_plugin::mk_update_field( unsigned num_parameters, parameter const * parameters, unsigned arity, sort * const * domain, sort * range) { decl_kind k = OP_DT_UPDATE_FIELD; ast_manager& m = *m_manager; if (num_parameters != 1 || !parameters[0].is_ast()) { m.raise_exception("invalid parameters for datatype field update"); return 0; } if (arity != 2) { m.raise_exception("invalid number of arguments for datatype field update"); return 0; } func_decl* acc = 0; if (is_func_decl(parameters[0].get_ast())) { acc = to_func_decl(parameters[0].get_ast()); } if (acc && !get_util().is_accessor(acc)) { acc = 0; } if (!acc) { m.raise_exception("datatype field update requires a datatype accessor as the second argument"); return 0; } sort* dom = acc->get_domain(0); sort* rng = acc->get_range(); if (dom != domain[0]) { m.raise_exception("first argument to field update should be a data-type"); return 0; } if (rng != domain[1]) { std::ostringstream buffer; buffer << "second argument to field update should be " << mk_ismt2_pp(rng, m) << " instead of " << mk_ismt2_pp(domain[1], m); m.raise_exception(buffer.str().c_str()); return 0; } range = domain[0]; func_decl_info info(m_family_id, k, num_parameters, parameters); return m.mk_func_decl(symbol("update_field"), arity, domain, range, info); }
Z3_ast Z3_API Z3_get_model_func_else(Z3_context c, Z3_model m, unsigned i) { Z3_TRY; LOG_Z3_get_model_func_else(c, m, i); RESET_ERROR_CODE(); CHECK_NON_NULL(m, 0); Z3_func_decl d = get_model_func_decl_core(c, m, i); if (d) { model * _m = to_model_ref(m); func_interp * g = _m->get_func_interp(to_func_decl(d)); if (g) { expr * e = g->get_else(); mk_c(c)->save_ast_trail(e); RETURN_Z3(of_ast(e)); } SET_ERROR_CODE(Z3_IOB); RETURN_Z3(0); } RETURN_Z3(0); Z3_CATCH_RETURN(0); }
Z3_bool Z3_API Z3_eval_func_decl(Z3_context c, Z3_model m, Z3_func_decl decl, Z3_ast* v) { Z3_TRY; LOG_Z3_eval_func_decl(c, m, decl, v); RESET_ERROR_CODE(); CHECK_NON_NULL(m, Z3_FALSE); ast_manager & mgr = mk_c(c)->m(); model * _m = to_model_ref(m); expr_ref result(mgr); if( _m->eval(to_func_decl(decl), result)) { mk_c(c)->save_ast_trail(result.get()); *v = of_ast(result.get()); RETURN_Z3_eval_func_decl Z3_TRUE; } else { return Z3_FALSE; } Z3_CATCH_RETURN(Z3_FALSE); }
Z3_bool Z3_API Z3_eval_decl(Z3_context c, Z3_model m, Z3_func_decl d, unsigned num_args, Z3_ast const args[], Z3_ast* v) { Z3_TRY; LOG_Z3_eval_decl(c, m, d, num_args, args, v); RESET_ERROR_CODE(); CHECK_NON_NULL(m, Z3_FALSE); ast_manager & mgr = mk_c(c)->m(); model * _m = to_model_ref(m); app_ref app(mgr); app = mgr.mk_app(to_func_decl(d), num_args, to_exprs(args)); expr_ref result(mgr); _m->eval(app.get(), result); mk_c(c)->save_ast_trail(result.get()); *v = of_ast(result.get()); RETURN_Z3_eval_decl Z3_TRUE; Z3_CATCH_RETURN(Z3_FALSE); }
Z3_ast Z3_API Z3_mk_map(Z3_context c, Z3_func_decl f, unsigned n, Z3_ast const* args) { Z3_TRY; LOG_Z3_mk_map(c, f, n, args); RESET_ERROR_CODE(); if (n == 0) { SET_ERROR_CODE(Z3_INVALID_ARG); RETURN_Z3(0); } ast_manager & m = mk_c(c)->m(); func_decl* _f = to_func_decl(f); expr* const* _args = to_exprs(args); ptr_vector<sort> domain; for (unsigned i = 0; i < n; ++i) { domain.push_back(m.get_sort(_args[i])); } parameter param(_f); func_decl * d = m.mk_func_decl(mk_c(c)->get_array_fid(), OP_ARRAY_MAP, 1, ¶m, n, domain.c_ptr()); app* r = m.mk_app(d, n, _args); mk_c(c)->save_ast_trail(r); check_sorts(c, r); RETURN_Z3(of_ast(r)); Z3_CATCH_RETURN(0); }
void Z3_API Z3_fixedpoint_register_relation(Z3_context c,Z3_fixedpoint d, Z3_func_decl f) { Z3_TRY; LOG_Z3_fixedpoint_register_relation(c, d, f); to_fixedpoint_ref(d)->ctx().register_predicate(to_func_decl(f), true); Z3_CATCH; }