void add_ineq() { pb_util pb(m); expr_ref fml(m), tmp(m); th_rewriter rw(m); vector<rational> coeffs(vars.size()); expr_ref_vector args(vars); while (true) { rational k(rand(6)); for (unsigned i = 0; i < coeffs.size(); ++i) { int v = 3 - rand(5); coeffs[i] = rational(v); if (coeffs[i].is_neg()) { args[i] = m.mk_not(args[i].get()); coeffs[i].neg(); k += coeffs[i]; } } fml = pb.mk_ge(args.size(), coeffs.c_ptr(), args.c_ptr(), k); rw(fml, tmp); rw(tmp, tmp); if (pb.is_ge(tmp)) { fml = tmp; break; } } std::cout << "(assert " << fml << ")\n"; ctx.assert_expr(fml); }
void operator()(expr * n, proof* p, expr_ref_vector& result, proof_ref_vector& ps) { expr_ref fml(m); proof_ref pr(m); m_todo.reset(); m_proofs.reset(); m_refs.reset(); m_memoize_disj.reset(); m_memoize_proof.reset(); m_fresh_predicates.reset(); m_todo.push_back(n); m_proofs.push_back(p); m_produce_proofs = p != 0; while (!m_todo.empty() && !m_cancel) { fml = m_todo.back(); pr = m_proofs.back(); m_todo.pop_back(); m_proofs.pop_back(); mk_horn(fml, pr); if (fml) { result.push_back(fml); ps.push_back(pr); } } TRACE("hnf", tout << mk_pp(n, m) << "\n==>\n"; for (unsigned i = 0; i < result.size(); ++i) { tout << mk_pp(result[i].get(), m) << "\n"; });
/** \brief Assert in m_aux_context, the constraint sk = e_1 OR ... OR sk = e_n where {e_1, ..., e_n} is the universe. */ void model_checker::restrict_to_universe(expr * sk, obj_hashtable<expr> const & universe) { SASSERT(!universe.empty()); ptr_buffer<expr> eqs; for (expr * e : universe) { eqs.push_back(m.mk_eq(sk, e)); } expr_ref fml(m.mk_or(eqs.size(), eqs.c_ptr()), m); m_aux_context->assert_expr(fml); }
void tst_model_retrieval() { memory::initialize(0); front_end_params params; params.m_model = true; ast_manager m; m.register_decl_plugins(); family_id array_fid = m.get_family_id(symbol("array")); array_util au(m); array_decl_plugin& ad = *static_cast<array_decl_plugin *>(m.get_plugin(array_fid)); // arr_s and select_fn creation copy-pasted from z3.cpp parameter sparams[2] = { parameter(to_sort(m.mk_bool_sort())), parameter(to_sort(m.mk_bool_sort())) }; sort_ref arr_s(m.mk_sort(array_fid, ARRAY_SORT, 2, sparams), m); sort * domain2[2] = {arr_s, m.mk_bool_sort()}; func_decl_ref select_fn( m.mk_func_decl(array_fid, OP_SELECT, 2, arr_s->get_parameters(), 2, domain2), m); app_ref a1(m.mk_const(symbol("a1"), arr_s), m); app_ref a2(m.mk_const(symbol("a2"), arr_s), m); // (= true (select a1 true)) app_ref fml(m.mk_eq(m.mk_true(), m.mk_app(select_fn.get(), a1, m.mk_true())), m); smt::context ctx(m, params); ctx.assert_expr(fml); lbool check_result = ctx.check(); std::cout<<((check_result==l_true) ? "satisfiable" : (check_result==l_false) ? "unsatisfiable" : "unknown")<<"\n"; ref<model> model; ctx.get_model(model); model_v2_pp(std::cout, *model, false); expr_ref a1_val(model->get_const_interp(a1->get_decl()), m); app_ref fml2(m.mk_eq(a2, a1_val), m); ctx.assert_expr(fml2); std::cout<<"--------------------------\n"; ctx.display(std::cout); std::cout<<"--------------------------\n"; check_result = ctx.check(); ctx.display(std::cout); std::cout<<"--------------------------\n"; std::cout<<((check_result==l_true) ? "satisfiable" : (check_result==l_false) ? "unsatisfiable" : "unknown")<<"\n"; }
/** \brief Assert in m_aux_context, the constraint sk = e_1 OR ... OR sk = e_n where {e_1, ..., e_n} is the universe. */ void model_checker::restrict_to_universe(expr * sk, obj_hashtable<expr> const & universe) { SASSERT(!universe.empty()); ptr_buffer<expr> eqs; obj_hashtable<expr>::iterator it = universe.begin(); obj_hashtable<expr>::iterator end = universe.end(); for (; it != end; ++it) { expr * e = *it; eqs.push_back(m.mk_eq(sk, e)); } expr_ref fml(m.mk_or(eqs.size(), eqs.c_ptr()), m); m_aux_context->assert_expr(fml); }
void flush_assertions() const { proof_ref proof(m); expr_ref fml(m); expr_ref_vector fmls(m); for (unsigned i = 0; i < m_assertions.size(); ++i) { m_rewriter(m_assertions[i].get(), fml, proof); m_solver->assert_expr(fml); } m_rewriter.flush_side_constraints(fmls); m_solver->assert_expr(fmls); m_assertions.reset(); }
void mk_coalesce::merge_rules(rule_ref& tgt, rule const& src) { SASSERT(same_body(*tgt.get(), src)); m_sub1.reset(); m_sub2.reset(); m_idx = 0; app_ref pred(m), head(m); expr_ref fml1(m), fml2(m), fml(m); app_ref_vector tail(m); ptr_vector<sort> sorts1, sorts2; expr_ref_vector conjs1(m), conjs(m); rule_ref res(rm); bool_rewriter bwr(m); svector<bool> is_neg; tgt->get_vars(sorts1); src.get_vars(sorts2); mk_pred(head, src.get_head(), tgt->get_head()); for (unsigned i = 0; i < src.get_uninterpreted_tail_size(); ++i) { mk_pred(pred, src.get_tail(i), tgt->get_tail(i)); tail.push_back(pred); is_neg.push_back(src.is_neg_tail(i)); } extract_conjs(m_sub1, src, fml1); extract_conjs(m_sub2, *tgt.get(), fml2); bwr.mk_or(fml1, fml2, fml); SASSERT(is_app(fml)); tail.push_back(to_app(fml)); is_neg.push_back(false); res = rm.mk(head, tail.size(), tail.c_ptr(), is_neg.c_ptr(), tgt->name()); if (m_ctx.generate_proof_trace()) { src.to_formula(fml1); tgt->to_formula(fml2); res->to_formula(fml); #if 0 sort* ps = m.mk_proof_sort(); sort* domain[3] = { ps, ps, m.mk_bool_sort() }; func_decl* merge = m.mk_func_decl(symbol("merge-clauses"), 3, domain, ps); // TBD: ad-hoc proof rule expr* args[3] = { m.mk_asserted(fml1), m.mk_asserted(fml2), fml }; // ...m_pc->insert(m.mk_app(merge, 3, args)); #else svector<std::pair<unsigned, unsigned> > pos; vector<expr_ref_vector> substs; proof* p = src.get_proof(); p = m.mk_hyper_resolve(1, &p, fml, pos, substs); res->set_proof(m, p); #endif } tgt = res; }
void fixedpoint_context::simplify_rules( unsigned num_rules, expr* const* rules, unsigned num_outputs, func_decl* const* outputs, expr_ref_vector& result) { ast_manager& m = m_context.get_manager(); datalog::context ctx(m, m_context.get_fparams()); datalog::rule_manager& rm = ctx.get_rule_manager(); for (unsigned i = 0; i < num_rules; ++i) { expr* rule = rules[i], *body, *head; while (true) { if (is_quantifier(rule)) { rule = to_quantifier(rule)->get_expr(); } else if (m.is_implies(rule, body, head)) { rule = head; } else { break; } } if (is_app(rule)) { func_decl* r = to_app(rule)->get_decl(); if (!ctx.is_predicate(r)) { ctx.register_predicate(r); if (num_outputs == 0) { ctx.set_output_predicate(r); } } } } for (unsigned i = 0; i < num_outputs; ++i) { ctx.set_output_predicate(outputs[i]); } for (unsigned i = 0; i < num_rules; ++i) { expr* rule = rules[i]; ctx.add_rule(rule, symbol::null); } model_converter_ref mc; // not exposed. proof_converter_ref pc; // not exposed. ctx.apply_default_transformation(mc, pc); datalog::rule_set const& new_rules = ctx.get_rules(); datalog::rule_set::iterator it = new_rules.begin(), end = new_rules.end(); for (; it != end; ++it) { datalog::rule* r = *it; expr_ref fml(m); r->to_formula(fml); result.push_back(fml); } }
void flush_assertions() const { if (m_assertions.empty()) return; m_rewriter.updt_params(get_params()); proof_ref proof(m); expr_ref fml1(m), fml(m); expr_ref_vector fmls(m); for (expr* a : m_assertions) { m_th_rewriter(a, fml1, proof); m_rewriter(false, fml1, fml, proof); m_solver->assert_expr(fml); } m_rewriter.flush_side_constraints(fmls); m_solver->assert_expr(fmls); m_assertions.reset(); }
virtual solver* translate(ast_manager& dst_m, params_ref const& p) { ast_translation tr(m, dst_m); if (m_num_scopes > 0) { throw default_exception("Cannot translate sat solver at non-base level"); } inc_sat_solver* result = alloc(inc_sat_solver, dst_m, p); expr_ref fml(dst_m); for (unsigned i = 0; i < m_fmls.size(); ++i) { fml = tr(m_fmls[i].get()); result->m_fmls.push_back(fml); } for (unsigned i = 0; i < m_asmsf.size(); ++i) { fml = tr(m_asmsf[i].get()); result->m_asmsf.push_back(fml); } return result; }
void SafeTimer::stop() { // to prevent multiple simultaneous calls from more threads static Poco::FastMutex fm; Poco::FastMutex::ScopedLock fml(fm); Poco::FastMutex::ScopedLock lock(_mutex); if (_pCallback) { _periodicInterval = 0; _mutex.unlock(); _wakeUp.set(); _done.wait(); // warning: deadlock if called from timer callback thread.join(); _mutex.lock(); delete _pCallback; _pCallback = 0; } }
lbool gia_pareto::operator()() { expr_ref fml(m); lbool is_sat = m_solver->check_sat(0, 0); if (is_sat == l_true) { { solver::scoped_push _s(*m_solver.get()); while (is_sat == l_true) { if (m.canceled()) { return l_undef; } m_solver->get_model(m_model); m_solver->get_labels(m_labels); IF_VERBOSE(1, model_ref mdl(m_model); cb.fix_model(mdl); model_smt2_pp(verbose_stream() << "new model:\n", m, *mdl, 0);); // TBD: we can also use local search to tune solution coordinate-wise. mk_dominates(); is_sat = m_solver->check_sat(0, 0); }
lbool check_sat(unsigned sz, expr * const * assumptions) override { m_solver.pop_to_base_level(); m_core.reset(); if (m_solver.inconsistent()) return l_false; expr_ref_vector _assumptions(m); obj_map<expr, expr*> asm2fml; for (unsigned i = 0; i < sz; ++i) { if (!is_literal(assumptions[i])) { expr_ref a(m.mk_fresh_const("s", m.mk_bool_sort()), m); expr_ref fml(m.mk_eq(a, assumptions[i]), m); assert_expr(fml); _assumptions.push_back(a); asm2fml.insert(a, assumptions[i]); } else { _assumptions.push_back(assumptions[i]); asm2fml.insert(assumptions[i], assumptions[i]); } } TRACE("sat", tout << _assumptions << "\n";);
void display_weighted(std::ostream& out, unsigned sz, expr * const * assumptions, unsigned const* weights) { if (weights != nullptr) { for (unsigned i = 0; i < sz; ++i) m_weights.push_back(weights[i]); } init_preprocess(); m_solver.pop_to_base_level(); dep2asm_t dep2asm; expr_ref_vector asms(m); for (unsigned i = 0; i < sz; ++i) { expr_ref a(m.mk_fresh_const("s", m.mk_bool_sort()), m); expr_ref fml(m.mk_implies(a, assumptions[i]), m); assert_expr(fml); asms.push_back(a); } VERIFY(l_true == internalize_formulas()); VERIFY(l_true == internalize_assumptions(sz, asms.c_ptr(), dep2asm)); svector<unsigned> nweights; for (unsigned i = 0; i < m_asms.size(); ++i) { nweights.push_back((unsigned) m_weights[i]); } m_weights.reset(); m_solver.display_wcnf(out, m_asms.size(), m_asms.c_ptr(), nweights.c_ptr()); }
void operator()(goal & g) { if (g.inconsistent()) return; tactic_report report("symmetry-reduce", g); vector<ptr_vector<app> > P; expr_ref fml(m()); to_formula(g, fml); app_map occs; compute_occurrences(fml, occs); find_candidate_permutations(fml, occs, P); if (P.empty()) { return; } term_set T, cts; unsigned num_sym_break_preds = 0; for (unsigned i = 0; i < P.size(); ++i) { term_set& consts = P[i]; if (invariant_by_permutations(fml, consts)) { cts.reset(); select_terms(fml, consts, T); while (!T.empty() && cts.size() < consts.size()) { app* t = select_most_promising_term(fml, T, cts, consts, occs); T.erase(t); compute_used_in(t, cts, consts); app* c = select_const(consts, cts); if (!c) break; cts.push_back(c); expr* mem = mk_member(t, cts); g.assert_expr(mem); num_sym_break_preds++; TRACE("symmetry_reduce", tout << "member predicate: " << mk_pp(mem, m()) << "\n";); fml = m().mk_and(fml.get(), mem); normalize(fml); } } }
static void test_quant_solver(ast_manager& m, char const* str, bool validate = true) { expr_ref fml(m); app_ref_vector vars(m); parse_fml(str, vars, fml); test_quant_solver(m, vars.size(), vars.c_ptr(), fml, validate); }
static void add_random_ineq( expr_ref_vector& fmls, opt::model_based_opt& mbo, random_gen& r, svector<int> const& values, unsigned max_vars, unsigned max_coeff) { ast_manager& m = fmls.get_manager(); arith_util a(m); unsigned num_vars = values.size(); uint_set used_vars; vector<var_t> vars; int value = 0; for (unsigned i = 0; i < max_vars; ++i) { unsigned x = r(num_vars); if (used_vars.contains(x)) { continue; } used_vars.insert(x); int coeff = r(max_coeff + 1); if (coeff == 0) { continue; } unsigned sign = r(2); coeff = sign == 0 ? coeff : -coeff; vars.push_back(var_t(x, rational(coeff))); value += coeff*values[x]; } unsigned abs_value = value < 0 ? - value : value; // value + k <= 0 // k <= - value // range for k is 2*|value| // k <= - value - range opt::ineq_type rel = opt::t_le; int coeff = 0; if (r(4) == 0) { rel = opt::t_eq; coeff = -value; } else { if (abs_value > 0) { coeff = -value - r(2*abs_value); } else { coeff = 0; } if (coeff != -value && r(3) == 0) { rel = opt::t_lt; } } expr_ref fml(m); app_ref t1(m); app_ref t2(a.mk_numeral(rational(0), a.mk_real()), m); mk_term(vars, rational(coeff), t1); switch (rel) { case opt::t_eq: fml = m.mk_eq(t1, t2); break; case opt::t_lt: fml = a.mk_lt(t1, t2); break; case opt::t_le: fml = a.mk_le(t1, t2); break; case opt::t_mod: NOT_IMPLEMENTED_YET(); break; } fmls.push_back(fml); mbo.add_constraint(vars, rational(coeff), rel); }
static void test_c(app* x, expr_ref_vector const& c) { ast_manager& m = c.get_manager(); expr_ref fml(m); fml = m.mk_and(c.size(), c.c_ptr()); test(x, fml); }
void rule_manager::mk_rule_core(expr* _fml, rule_ref_vector& rules, symbol const& name) { app_ref_vector body(m); app_ref head(m); expr_ref e(m), fml(_fml, m); svector<bool> is_negated; TRACE("dl_rule", tout << mk_pp(fml, m) << "\n";);