3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-06-20 04:43:39 +00:00
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2019-03-28 16:11:16 -07:00
parent 7a6823aef1
commit 1c694fd42f
7 changed files with 130 additions and 63 deletions

View file

@ -2122,6 +2122,7 @@ public:
app * mk_or(expr * arg1, expr * arg2) { return mk_app(m_basic_family_id, OP_OR, arg1, arg2); } app * mk_or(expr * arg1, expr * arg2) { return mk_app(m_basic_family_id, OP_OR, arg1, arg2); }
app * mk_and(expr * arg1, expr * arg2) { return mk_app(m_basic_family_id, OP_AND, arg1, arg2); } app * mk_and(expr * arg1, expr * arg2) { return mk_app(m_basic_family_id, OP_AND, arg1, arg2); }
app * mk_or(expr * arg1, expr * arg2, expr * arg3) { return mk_app(m_basic_family_id, OP_OR, arg1, arg2, arg3); } app * mk_or(expr * arg1, expr * arg2, expr * arg3) { return mk_app(m_basic_family_id, OP_OR, arg1, arg2, arg3); }
app * mk_or(expr* a, expr* b, expr* c, expr* d) { expr* args[4] = { a, b, c, d }; return mk_app(m_basic_family_id, OP_OR, 4, args); }
app * mk_and(expr * arg1, expr * arg2, expr * arg3) { return mk_app(m_basic_family_id, OP_AND, arg1, arg2, arg3); } app * mk_and(expr * arg1, expr * arg2, expr * arg3) { return mk_app(m_basic_family_id, OP_AND, arg1, arg2, arg3); }
app * mk_implies(expr * arg1, expr * arg2) { return mk_app(m_basic_family_id, OP_IMPLIES, arg1, arg2); } app * mk_implies(expr * arg1, expr * arg2) { return mk_app(m_basic_family_id, OP_IMPLIES, arg1, arg2); }
app * mk_not(expr * n) { return mk_app(m_basic_family_id, OP_NOT, n); } app * mk_not(expr * n) { return mk_app(m_basic_family_id, OP_NOT, n); }

View file

@ -52,28 +52,50 @@ expr_pattern_match::match_quantifier(quantifier* qf, app_ref_vector& patterns, u
} }
m_regs[0] = qf->get_expr(); m_regs[0] = qf->get_expr();
for (unsigned i = 0; i < m_precompiled.size(); ++i) { for (unsigned i = 0; i < m_precompiled.size(); ++i) {
quantifier* qf2 = m_precompiled[i].get(); if (match_quantifier(i, qf, patterns, weight))
if (qf2->get_kind() != qf->get_kind() || is_lambda(qf)) { return true;
continue; }
} return false;
if (qf2->get_num_decls() != qf->get_num_decls()) { }
continue;
} bool
subst s; expr_pattern_match::match_quantifier(unsigned i, quantifier* qf, app_ref_vector& patterns, unsigned& weight) {
if (match(qf->get_expr(), m_first_instrs[i], s)) { quantifier* qf2 = m_precompiled[i].get();
for (unsigned j = 0; j < qf2->get_num_patterns(); ++j) { if (qf2->get_kind() != qf->get_kind() || is_lambda(qf)) {
app* p = static_cast<app*>(qf2->get_pattern(j)); return false;
expr_ref p_result(m_manager); }
instantiate(p, qf->get_num_decls(), s, p_result); if (qf2->get_num_decls() != qf->get_num_decls()) {
patterns.push_back(to_app(p_result.get())); return false;
} }
weight = qf2->get_weight(); subst s;
return true; if (match(qf->get_expr(), m_first_instrs[i], s)) {
for (unsigned j = 0; j < qf2->get_num_patterns(); ++j) {
app* p = static_cast<app*>(qf2->get_pattern(j));
expr_ref p_result(m_manager);
instantiate(p, qf->get_num_decls(), s, p_result);
patterns.push_back(to_app(p_result.get()));
} }
weight = qf2->get_weight();
return true;
} }
return false; return false;
} }
bool expr_pattern_match::match_quantifier_index(quantifier* qf, app_ref_vector& patterns, unsigned& index) {
if (m_regs.empty()) return false;
m_regs[0] = qf->get_expr();
for (unsigned i = 0; i < m_precompiled.size(); ++i) {
unsigned weight = 0;
if (match_quantifier(i, qf, patterns, weight)) {
index = i;
return true;
}
}
return false;
}
void void
expr_pattern_match::instantiate(expr* a, unsigned num_bound, subst& s, expr_ref& result) { expr_pattern_match::instantiate(expr* a, unsigned num_bound, subst& s, expr_ref& result) {
bound b; bound b;
@ -399,8 +421,16 @@ expr_pattern_match::initialize(char const * spec_string) {
TRACE("expr_pattern_match", display(tout); ); TRACE("expr_pattern_match", display(tout); );
} }
void unsigned expr_pattern_match::initialize(quantifier* q) {
expr_pattern_match::display(std::ostream& out) const { if (m_instrs.empty()) {
m_instrs.push_back(instr(BACKTRACK));
}
compile(q);
return m_precompiled.size() - 1;
}
void expr_pattern_match::display(std::ostream& out) const {
for (unsigned i = 0; i < m_instrs.size(); ++i) { for (unsigned i = 0; i < m_instrs.size(); ++i) {
display(out, m_instrs[i]); display(out, m_instrs[i]);
} }

View file

@ -131,11 +131,14 @@ class expr_pattern_match {
public: public:
expr_pattern_match(ast_manager & manager); expr_pattern_match(ast_manager & manager);
~expr_pattern_match(); ~expr_pattern_match();
virtual bool match_quantifier(quantifier * qf, app_ref_vector & patterns, unsigned & weight); bool match_quantifier(quantifier * qf, app_ref_vector & patterns, unsigned & weight);
virtual void initialize(char const * database); bool match_quantifier_index(quantifier* qf, app_ref_vector & patterns, unsigned& index);
unsigned initialize(quantifier* qf);
void initialize(char const * database);
void display(std::ostream& out) const; void display(std::ostream& out) const;
private: private:
bool match_quantifier(unsigned i, quantifier * qf, app_ref_vector & patterns, unsigned & weight);
void instantiate(expr* a, unsigned num_bound, subst& s, expr_ref& result); void instantiate(expr* a, unsigned num_bound, subst& s, expr_ref& result);
void compile(expr* q); void compile(expr* q);
bool match(expr* a, unsigned init, subst& s); bool match(expr* a, unsigned init, subst& s);

View file

@ -93,4 +93,5 @@ void func_decl_replace::reset() {
m_cache.reset(); m_cache.reset();
m_subst.reset(); m_subst.reset();
m_refs.reset(); m_refs.reset();
m_funs.reset();
} }

View file

@ -27,13 +27,14 @@ class func_decl_replace {
ast_manager& m; ast_manager& m;
obj_map<func_decl, func_decl*> m_subst; obj_map<func_decl, func_decl*> m_subst;
obj_map<expr, expr*> m_cache; obj_map<expr, expr*> m_cache;
ptr_vector<expr> m_todo, m_args; ptr_vector<expr> m_todo, m_args;
expr_ref_vector m_refs; expr_ref_vector m_refs;
func_decl_ref_vector m_funs;
public: public:
func_decl_replace(ast_manager& m): m(m), m_refs(m) {} func_decl_replace(ast_manager& m): m(m), m_refs(m), m_funs(m) {}
void insert(func_decl* src, func_decl* dst) { m_subst.insert(src, dst); } void insert(func_decl* src, func_decl* dst) { m_subst.insert(src, dst); m_funs.push_back(src), m_funs.push_back(dst); }
expr_ref operator()(expr* e); expr_ref operator()(expr* e);

View file

@ -24,23 +24,12 @@ void special_relations_tactic::collect_feature(goal const& g, unsigned idx,
obj_map<func_decl, sp_axioms>& goal_features) { obj_map<func_decl, sp_axioms>& goal_features) {
expr* f = g.form(idx); expr* f = g.form(idx);
func_decl_ref p(m); func_decl_ref p(m);
if (is_transitivity(f, p)) { if (!is_quantifier(f)) return;
insert(goal_features, p, idx, sr_transitive); unsigned index = 0;
} app_ref_vector patterns(m);
else if (is_anti_symmetry(f, p)) { if (m_pm.match_quantifier_index(to_quantifier(f), patterns, index)) {
insert(goal_features, p, idx, sr_antisymmetric); p = to_app(patterns.get(0)->get_arg(0))->get_decl();
} insert(goal_features, p, idx, m_properties[index]);
else if (is_left_tree(f, p)) {
insert(goal_features, p, idx, sr_lefttree);
}
else if (is_right_tree(f, p)) {
insert(goal_features, p, idx, sr_righttree);
}
else if (is_reflexive(f, p)) {
insert(goal_features, p, idx, sr_reflexive);
}
else if (is_total(f, p)) {
insert(goal_features, p, idx, sr_total);
} }
} }
@ -53,32 +42,68 @@ void special_relations_tactic::insert(obj_map<func_decl, sp_axioms>& goal_featur
} }
bool special_relations_tactic::is_transitivity(expr* fml, func_decl_ref& p) { void special_relations_tactic::initialize() {
// match Forall x, y, z . p(x,y) & p(y,z) -> p(x,z) if (!m_properties.empty()) return;
return false; sort_ref A(m);
func_decl_ref R(m.mk_func_decl(symbol("R"), A, A, m.mk_bool_sort()), m);
var_ref x(m.mk_var(0, A), m);
var_ref y(m.mk_var(1, A), m);
var_ref z(m.mk_var(2, A), m);
expr* _x = x, *_y = y, *_z = z;
expr_ref Rxy(m.mk_app(R, _x, y), m);
expr_ref Ryz(m.mk_app(R, _y, z), m);
expr_ref Rxz(m.mk_app(R, _x, z), m);
expr_ref Rxx(m.mk_app(R, _x, x), m);
expr_ref Ryx(m.mk_app(R, _y, x), m);
expr_ref Rzy(m.mk_app(R, _z, y), m);
expr_ref Rzx(m.mk_app(R, _z, x), m);
expr_ref nRxy(m.mk_not(Rxy), m);
expr_ref nRyx(m.mk_not(Ryx), m);
expr_ref nRzx(m.mk_not(Rzx), m);
expr_ref nRxz(m.mk_not(Rxz), m);
sort* As[3] = { A, A, A};
symbol xyz[3] = { symbol("x"), symbol("y"), symbol("z") };
expr_ref fml(m);
quantifier_ref q(m);
expr_ref pat(m.mk_pattern(to_app(Rxy)), m);
expr* pats[1] = { pat };
fml = m.mk_or(m.mk_not(Rxy), m.mk_not(Ryz), Rxz);
q = m.mk_forall(3, As, xyz, fml, 0, symbol::null, symbol::null, 1, pats);
register_pattern(m_pm.initialize(q), sr_transitive);
fml = Rxx;
q = m.mk_forall(1, As, xyz, fml, 0, symbol::null, symbol::null, 1, pats);
register_pattern(m_pm.initialize(q), sr_reflexive);
fml = m.mk_or(nRxy, nRyx, m.mk_eq(x, y));
q = m.mk_forall(2, As, xyz, fml, 0, symbol::null, symbol::null, 1, pats);
register_pattern(m_pm.initialize(q), sr_antisymmetric);
fml = m.mk_or(nRyx, nRzx, Ryz, Rzy);
q = m.mk_forall(3, As, xyz, fml, 0, symbol::null, symbol::null, 1, pats);
register_pattern(m_pm.initialize(q), sr_lefttree);
fml = m.mk_or(nRxy, nRxz, Ryx, Rzy);
q = m.mk_forall(3, As, xyz, fml, 0, symbol::null, symbol::null, 1, pats);
register_pattern(m_pm.initialize(q), sr_righttree);
fml = m.mk_or(Rxy, Ryx);
q = m.mk_forall(2, As, xyz, fml, 0, symbol::null, symbol::null, 1, pats);
register_pattern(m_pm.initialize(q), sr_total);
} }
bool special_relations_tactic::is_anti_symmetry(expr* fml, func_decl_ref& p) {
return false; void special_relations_tactic::register_pattern(unsigned index, sr_property p) {
} SASSERT(index == m_properties.size() + 1);
bool special_relations_tactic::is_left_tree(expr* fml, func_decl_ref& p) { m_properties.push_back(p);
return false;
}
bool special_relations_tactic::is_right_tree(expr* fml, func_decl_ref& p) {
return false;
}
bool special_relations_tactic::is_reflexive(expr* fml, func_decl_ref& p) {
return false;
}
bool special_relations_tactic::is_total(expr* fml, func_decl_ref& p) {
return false;
}
bool special_relations_tactic::is_symmetric(expr* fml, func_decl_ref& p) {
return false;
} }
void special_relations_tactic::operator()(goal_ref const & g, goal_ref_buffer & result) { void special_relations_tactic::operator()(goal_ref const & g, goal_ref_buffer & result) {
tactic_report report("special_relations", *g); tactic_report report("special_relations", *g);
initialize();
obj_map<func_decl, sp_axioms> goal_features; obj_map<func_decl, sp_axioms> goal_features;
unsigned size = g->size(); unsigned size = g->size();
for (unsigned idx = 0; idx < size; idx++) { for (unsigned idx = 0; idx < size; idx++) {

View file

@ -23,10 +23,13 @@ Notes:
#include "tactic/tactic.h" #include "tactic/tactic.h"
#include "tactic/tactical.h" #include "tactic/tactical.h"
#include "ast/special_relations_decl_plugin.h" #include "ast/special_relations_decl_plugin.h"
#include "ast/pattern/expr_pattern_match.h"
class special_relations_tactic : public tactic { class special_relations_tactic : public tactic {
ast_manager& m; ast_manager& m;
params_ref m_params; params_ref m_params;
expr_pattern_match m_pm;
svector<sr_property> m_properties;
struct sp_axioms { struct sp_axioms {
unsigned_vector m_goal_indices; unsigned_vector m_goal_indices;
@ -37,6 +40,9 @@ class special_relations_tactic : public tactic {
void collect_feature(goal const& g, unsigned idx, obj_map<func_decl, sp_axioms>& goal_features); void collect_feature(goal const& g, unsigned idx, obj_map<func_decl, sp_axioms>& goal_features);
void insert(obj_map<func_decl, sp_axioms>& goal_features, func_decl* f, unsigned idx, sr_property p); void insert(obj_map<func_decl, sp_axioms>& goal_features, func_decl* f, unsigned idx, sr_property p);
void initialize();
void register_pattern(unsigned index, sr_property);
bool is_transitivity(expr* fml, func_decl_ref& p); bool is_transitivity(expr* fml, func_decl_ref& p);
bool is_anti_symmetry(expr* fml, func_decl_ref& p); bool is_anti_symmetry(expr* fml, func_decl_ref& p);
bool is_left_tree(expr* fml, func_decl_ref& p); bool is_left_tree(expr* fml, func_decl_ref& p);
@ -47,7 +53,7 @@ class special_relations_tactic : public tactic {
public: public:
special_relations_tactic(ast_manager & m, params_ref const & ref = params_ref()): m(m), m_params(ref) {} special_relations_tactic(ast_manager & m, params_ref const & ref = params_ref()): m(m), m_params(ref), m_pm(m) {}
~special_relations_tactic() override {} ~special_relations_tactic() override {}