From f39773f15b5937f634855ae0adb7b6b7e948c876 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 30 Aug 2015 15:23:31 -0400 Subject: [PATCH 001/562] update readme --- README | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README b/README index 22fc58c7a..afd4876ca 100644 --- a/README +++ b/README @@ -2,6 +2,8 @@ Z3 is a theorem prover from Microsoft Research. Z3 is licensed under the MIT license. Z3 can be built using Visual Studio Command Prompt and make/g++. +This fork of Z3 adds support for solving equations in the theory of strings. + 1) Building Z3 on Windows using Visual Studio Command Prompt 32-bit builds, start with: From b30d4f757db057253dc64f61b84f12b7f820a7ef Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 2 Sep 2015 18:08:58 -0400 Subject: [PATCH 002/562] ignore Z3-str source --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 93194fc1b..037d9abd7 100644 --- a/.gitignore +++ b/.gitignore @@ -74,3 +74,6 @@ src/api/ml/z3.mllib *.bak doc/api doc/code +# reference code for z3str2 +Z3-str/** + From 1f96e19211eece215d6deaec3d53018d9c53579f Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 2 Sep 2015 18:55:45 -0400 Subject: [PATCH 003/562] failing test case: SMT2 parse string constants --- src/test/smt2print_parse.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/smt2print_parse.cpp b/src/test/smt2print_parse.cpp index 39543d141..349a3c9c8 100644 --- a/src/test/smt2print_parse.cpp +++ b/src/test/smt2print_parse.cpp @@ -98,6 +98,12 @@ void tst_smt2print_parse() { test_parseprint(spec5); + // Test strings + char const* spec6 = + "(assert (= \"abc\" \"abc\"))"; + + test_parseprint(spec6); + // Test ? } From e48ac4a97af1150ac3ce72c454dd2102f35bb32f Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 2 Sep 2015 21:12:03 -0400 Subject: [PATCH 004/562] create and register string theory plugin the parser gets a little bit further now! rejects input with "unexpected character" --- .gitignore | 1 + src/ast/ast.cpp | 5 +++ src/ast/ast.h | 6 +++ src/ast/reg_decl_plugins.cpp | 4 ++ src/ast/str_decl_plugin.cpp | 74 ++++++++++++++++++++++++++++++++ src/ast/str_decl_plugin.h | 75 +++++++++++++++++++++++++++++++++ src/parsers/smt2/smt2parser.cpp | 18 ++++++++ 7 files changed, 183 insertions(+) create mode 100644 src/ast/str_decl_plugin.cpp create mode 100644 src/ast/str_decl_plugin.h diff --git a/.gitignore b/.gitignore index 037d9abd7..97ca67cf4 100644 --- a/.gitignore +++ b/.gitignore @@ -75,5 +75,6 @@ src/api/ml/z3.mllib doc/api doc/code # reference code for z3str2 +Z3-str Z3-str/** diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 3a6275e33..5a2dc4a52 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -17,6 +17,7 @@ Revision History: --*/ #include +#include #include"ast.h" #include"ast_pp.h" #include"ast_ll_pp.h" @@ -58,6 +59,7 @@ parameter& parameter::operator=(parameter const& other) { case PARAM_SYMBOL: new (m_symbol) symbol(other.get_symbol()); break; case PARAM_RATIONAL: new (m_rational) rational(other.get_rational()); break; case PARAM_DOUBLE: m_dval = other.m_dval; break; + case PARAM_STRING: m_string = other.m_string; break; case PARAM_EXTERNAL: m_ext_id = other.m_ext_id; break; default: UNREACHABLE(); @@ -90,6 +92,7 @@ bool parameter::operator==(parameter const & p) const { case PARAM_SYMBOL: return get_symbol() == p.get_symbol(); case PARAM_RATIONAL: return get_rational() == p.get_rational(); case PARAM_DOUBLE: return m_dval == p.m_dval; + case PARAM_STRING: return (m_string == NULL && p.m_string == NULL) || strcmp(m_string, p.m_string)==0; case PARAM_EXTERNAL: return m_ext_id == p.m_ext_id; default: UNREACHABLE(); return false; } @@ -103,6 +106,7 @@ unsigned parameter::hash() const { case PARAM_SYMBOL: b = get_symbol().hash(); break; case PARAM_RATIONAL: b = get_rational().hash(); break; case PARAM_DOUBLE: b = static_cast(m_dval); break; + case PARAM_STRING: /* TODO */ b = 42; break; case PARAM_EXTERNAL: b = m_ext_id; break; } return (b << 2) | m_kind; @@ -115,6 +119,7 @@ std::ostream& parameter::display(std::ostream& out) const { case PARAM_RATIONAL: return out << get_rational(); case PARAM_AST: return out << "#" << get_ast()->get_id(); case PARAM_DOUBLE: return out << m_dval; + case PARAM_STRING: return out << m_string; case PARAM_EXTERNAL: return out << "@" << m_ext_id; default: UNREACHABLE(); diff --git a/src/ast/ast.h b/src/ast/ast.h index a5f5c286f..9c1044ec7 100644 --- a/src/ast/ast.h +++ b/src/ast/ast.h @@ -86,6 +86,7 @@ public: PARAM_SYMBOL, PARAM_RATIONAL, PARAM_DOUBLE, + PARAM_STRING, // PARAM_EXTERNAL is used for handling decl_plugin specific parameters. // For example, it is used for handling mpf numbers in float_decl_plugin, // and irrational algebraic numbers in arith_decl_plugin. @@ -104,6 +105,7 @@ private: char m_symbol[sizeof(symbol)]; // for PARAM_SYMBOL char m_rational[sizeof(rational)]; // for PARAM_RATIONAL double m_dval; // for PARAM_DOUBLE (remark: this is not used in float_decl_plugin) + const char* m_string; // for PARAM_STRING unsigned m_ext_id; // for PARAM_EXTERNAL }; @@ -116,6 +118,7 @@ public: explicit parameter(symbol const & s): m_kind(PARAM_SYMBOL) { new (m_symbol) symbol(s); } explicit parameter(rational const & r): m_kind(PARAM_RATIONAL) { new (m_rational) rational(r); } explicit parameter(double d):m_kind(PARAM_DOUBLE), m_dval(d) {} + explicit parameter(const char *s):m_kind(PARAM_STRING), m_string(s) {} explicit parameter(unsigned ext_id, bool):m_kind(PARAM_EXTERNAL), m_ext_id(ext_id) {} parameter(parameter const&); @@ -129,6 +132,7 @@ public: bool is_symbol() const { return m_kind == PARAM_SYMBOL; } bool is_rational() const { return m_kind == PARAM_RATIONAL; } bool is_double() const { return m_kind == PARAM_DOUBLE; } + bool is_string() const { return m_kind == PARAM_STRING; } bool is_external() const { return m_kind == PARAM_EXTERNAL; } bool is_int(int & i) const { return is_int() && (i = get_int(), true); } @@ -136,6 +140,7 @@ public: bool is_symbol(symbol & s) const { return is_symbol() && (s = get_symbol(), true); } bool is_rational(rational & r) const { return is_rational() && (r = get_rational(), true); } bool is_double(double & d) const { return is_double() && (d = get_double(), true); } + // TODO is_string(char*) bool is_external(unsigned & id) const { return is_external() && (id = get_ext_id(), true); } /** @@ -155,6 +160,7 @@ public: symbol const & get_symbol() const { SASSERT(is_symbol()); return *(reinterpret_cast(m_symbol)); } rational const & get_rational() const { SASSERT(is_rational()); return *(reinterpret_cast(m_rational)); } double get_double() const { SASSERT(is_double()); return m_dval; } + const char * get_string() const { SASSERT(is_string()); return m_string; } unsigned get_ext_id() const { SASSERT(is_external()); return m_ext_id; } bool operator==(parameter const & p) const; diff --git a/src/ast/reg_decl_plugins.cpp b/src/ast/reg_decl_plugins.cpp index f46dd76d4..6a7e7b30c 100644 --- a/src/ast/reg_decl_plugins.cpp +++ b/src/ast/reg_decl_plugins.cpp @@ -25,6 +25,7 @@ Revision History: #include"dl_decl_plugin.h" #include"seq_decl_plugin.h" #include"fpa_decl_plugin.h" +#include"str_decl_plugin.h" void reg_decl_plugins(ast_manager & m) { if (!m.get_plugin(m.mk_family_id(symbol("arith")))) { @@ -48,4 +49,7 @@ void reg_decl_plugins(ast_manager & m) { if (!m.get_plugin(m.mk_family_id(symbol("fpa")))) { m.register_plugin(symbol("fpa"), alloc(fpa_decl_plugin)); } + if (!m.get_plugin(m.mk_family_id(symbol("str")))) { + m.register_plugin(symbol("str"), alloc(str_decl_plugin)); + } } diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp new file mode 100644 index 000000000..540cd89c0 --- /dev/null +++ b/src/ast/str_decl_plugin.cpp @@ -0,0 +1,74 @@ +/*++ +Module Name: + + str_decl_plugin.h + +Abstract: + + + +Author: + + Murphy Berzish (mtrberzi) 2015-09-02. + +Revision History: + +--*/ +#include +#include"str_decl_plugin.h" +#include"string_buffer.h" +#include"warning.h" +#include"ast_pp.h" +#include"ast_smt2_pp.h" + +str_decl_plugin::str_decl_plugin(): + m_strv_sym("String"), + m_str_decl(0){ +} + +str_decl_plugin::~str_decl_plugin(){ +} + +void str_decl_plugin::finalize(void) { + #define DEC_REF(decl) if (decl) { m_manager->dec_ref(decl); } ((void) 0) + DEC_REF(m_str_decl); +} + +void str_decl_plugin::set_manager(ast_manager * m, family_id id) { + decl_plugin::set_manager(m, id); + m_str_decl = m->mk_sort(symbol("String"), sort_info(id, STRING_SORT)); + m->inc_ref(m_str_decl); + sort * s = m_str_decl; + /* TODO mk_pred, etc. */ +} + +decl_plugin * str_decl_plugin::mk_fresh() { + return alloc(str_decl_plugin); +} + +sort * str_decl_plugin::mk_sort(decl_kind k, unsigned num_parameters, parameter const * parameters) { + switch (k) { + case STRING_SORT: return m_str_decl; + default: return 0; + } +} + +func_decl * str_decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters, parameter const * parameters, + unsigned arity, sort * const * domain, sort * range) { + /* TODO */ + m_manager->raise_exception("str_decl_plugin::mk_func_decl() not yet implemented"); return 0; +} + +app * str_decl_plugin::mk_string(const char * val) { + parameter p[1] = {parameter(val)}; + func_decl * d; + d = m_manager->mk_const_decl(m_strv_sym, m_str_decl, func_decl_info(m_family_id, OP_STR, 1, p)); + return m_manager->mk_const(d); +} + +str_util::str_util(ast_manager &m) : + str_recognizers(m.mk_family_id(symbol("str"))), + m_manager(m) { + SASSERT(m.has_plugin(symbol("str"))); + m_plugin = static_cast(m.get_plugin(m.mk_family_id(symbol("str")))); +} diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h new file mode 100644 index 000000000..2fd1db022 --- /dev/null +++ b/src/ast/str_decl_plugin.h @@ -0,0 +1,75 @@ +/*++ +Module Name: + + str_decl_plugin.h + +Abstract: + + + +Author: + + Murphy Berzish (mtrberzi) 2015-09-02. + +Revision History: + +--*/ +#ifndef _STR_DECL_PLUGIN_H_ +#define _STR_DECL_PLUGIN_H_ + +#include"ast.h" + +enum str_sort_kind { + STRING_SORT, +}; + +enum str_op_kind { + OP_STR, /* string constants */ + + LAST_STR_OP +}; + +class str_decl_plugin : public decl_plugin { +protected: + symbol m_strv_sym; + sort * m_str_decl; + + virtual void set_manager(ast_manager * m, family_id id); +public: + str_decl_plugin(); + virtual ~str_decl_plugin(); + virtual void finalize(); + + virtual decl_plugin * mk_fresh(); + virtual sort * mk_sort(decl_kind k, unsigned num_parameters, parameter const * parameters); + virtual func_decl * mk_func_decl(decl_kind k, unsigned num_parameters, parameter const * parameters, + unsigned arity, sort * const * domain, sort * range); + + app * mk_string(const char * val); + // TODO +}; + +class str_recognizers { + family_id m_afid; +public: + str_recognizers(family_id fid):m_afid(fid) {} + family_id get_fid() const { return m_afid; } + family_id get_family_id() const { return get_fid(); } + // TODO +}; + +class str_util : public str_recognizers { + ast_manager & m_manager; + str_decl_plugin * m_plugin; +public: + str_util(ast_manager & m); + ast_manager & get_manager() const { return m_manager; } + str_decl_plugin & plugin() { return *m_plugin; } + + app * mk_string(const char * val) { + return m_plugin->mk_string(val); + } + // TODO +}; + +#endif /* _STR_DECL_PLUGIN_H_ */ diff --git a/src/parsers/smt2/smt2parser.cpp b/src/parsers/smt2/smt2parser.cpp index 785f578f3..af752c82d 100644 --- a/src/parsers/smt2/smt2parser.cpp +++ b/src/parsers/smt2/smt2parser.cpp @@ -22,6 +22,7 @@ Revision History: #include"datatype_decl_plugin.h" #include"bv_decl_plugin.h" #include"arith_decl_plugin.h" +#include"str_decl_plugin.h" #include"ast_pp.h" #include"well_sorted.h" #include"pattern_validation.h" @@ -64,6 +65,7 @@ namespace smt2 { scoped_ptr m_bv_util; scoped_ptr m_arith_util; + scoped_ptr m_str_util; scoped_ptr m_pattern_validator; scoped_ptr m_var_shifter; @@ -272,6 +274,12 @@ namespace smt2 { return *(m_bv_util.get()); } + str_util & strutil() { + if (m_str_util.get() == 0) + m_str_util = alloc(str_util, m()); + return *(m_str_util.get()); + } + pattern_validator & pat_validator() { if (m_pattern_validator.get() == 0) { m_pattern_validator = alloc(pattern_validator, m()); @@ -1054,6 +1062,13 @@ namespace smt2 { next(); } + void parse_string() { + SASSERT(curr() == scanner::STRING_TOKEN); + TRACE("parse_string", tout << "new string constant: " << m_scanner.get_string() << "\n";); + expr_stack().push_back(strutil().mk_string(m_scanner.get_string())); + next(); + } + void push_pattern_frame() { // TODO: It seems the only reliable way to parse patterns is: // Parse as an S-Expr, then try to convert it to an useful pattern. @@ -1713,6 +1728,9 @@ namespace smt2 { case scanner::BV_TOKEN: parse_bv_numeral(); break; + case scanner::STRING_TOKEN: + parse_string(); + break; case scanner::LEFT_PAREN: push_expr_frame(m_num_expr_frames == 0 ? 0 : static_cast(m_stack.top())); break; From 02345ee5f190d7033373523743329ec5de016b78 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 3 Sep 2015 00:17:05 -0400 Subject: [PATCH 005/562] fix string constant representation in parser spec1 loopback OK --- src/ast/ast.h | 4 +++- src/ast/ast_smt_pp.cpp | 9 +++++++++ src/ast/str_decl_plugin.cpp | 8 ++++++++ src/ast/str_decl_plugin.h | 2 ++ src/parsers/smt2/smt2parser.cpp | 9 +++++++-- src/test/smt2print_parse.cpp | 13 +++++++++---- 6 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/ast/ast.h b/src/ast/ast.h index 9c1044ec7..14b869e51 100644 --- a/src/ast/ast.h +++ b/src/ast/ast.h @@ -118,7 +118,9 @@ public: explicit parameter(symbol const & s): m_kind(PARAM_SYMBOL) { new (m_symbol) symbol(s); } explicit parameter(rational const & r): m_kind(PARAM_RATIONAL) { new (m_rational) rational(r); } explicit parameter(double d):m_kind(PARAM_DOUBLE), m_dval(d) {} - explicit parameter(const char *s):m_kind(PARAM_STRING), m_string(s) {} + explicit parameter(const char *s):m_kind(PARAM_STRING), m_string(s) { + TRACE("parse_string", tout << "parameter(const char *): " << s << "\n";); + } explicit parameter(unsigned ext_id, bool):m_kind(PARAM_EXTERNAL), m_ext_id(ext_id) {} parameter(parameter const&); diff --git a/src/ast/ast_smt_pp.cpp b/src/ast/ast_smt_pp.cpp index 805f3070f..0785c7bfc 100644 --- a/src/ast/ast_smt_pp.cpp +++ b/src/ast/ast_smt_pp.cpp @@ -24,6 +24,7 @@ Revision History: #include"ast_smt_pp.h" #include"arith_decl_plugin.h" #include"bv_decl_plugin.h" +#include"str_decl_plugin.h" #include"array_decl_plugin.h" #include"datatype_decl_plugin.h" #include"vector.h" @@ -160,8 +161,10 @@ class smt_printer { unsigned m_num_lets; arith_util m_autil; bv_util m_bvutil; + str_util m_strutil; family_id m_basic_fid; family_id m_bv_fid; + family_id m_str_fid; family_id m_arith_fid; family_id m_array_fid; family_id m_dt_fid; @@ -394,6 +397,7 @@ class smt_printer { void visit_app(app* n) { rational val; + const char *str; bool is_int, pos; buffer names; unsigned bv_size; @@ -436,6 +440,9 @@ class smt_printer { m_out << ") bv1[1])"; } } + else if (m_strutil.is_string(n, &str)) { + m_out << "\"" << str << "\""; + } else if (m_manager.is_label(n, pos, names) && names.size() >= 1) { if (m_is_smt2) { m_out << "(! "; @@ -797,6 +804,7 @@ public: m_num_lets(0), m_autil(m), m_bvutil(m), + m_strutil(m), m_logic(logic), m_AUFLIRA("AUFLIRA"), // It's much easier to read those testcases with that. @@ -809,6 +817,7 @@ public: m_bv_fid = m.mk_family_id("bv"); m_arith_fid = m.mk_family_id("arith"); m_array_fid = m.mk_family_id("array"); + m_str_fid = m.mk_family_id("str"); m_dt_fid = m.mk_family_id("datatype"); } diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 540cd89c0..66b6c23fc 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -66,6 +66,14 @@ app * str_decl_plugin::mk_string(const char * val) { return m_manager->mk_const(d); } +bool str_recognizers::is_string(expr const * n, const char ** val) const { + if (!is_app_of(n, m_afid, OP_STR)) + return false; + func_decl * decl = to_app(n)->get_decl(); + *val = decl->get_parameter(0).get_string(); + return true; +} + str_util::str_util(ast_manager &m) : str_recognizers(m.mk_family_id(symbol("str"))), m_manager(m) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 2fd1db022..2d629e006 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -55,6 +55,8 @@ public: str_recognizers(family_id fid):m_afid(fid) {} family_id get_fid() const { return m_afid; } family_id get_family_id() const { return get_fid(); } + + bool is_string(expr const * n, const char ** val) const; // TODO }; diff --git a/src/parsers/smt2/smt2parser.cpp b/src/parsers/smt2/smt2parser.cpp index af752c82d..5c8d60700 100644 --- a/src/parsers/smt2/smt2parser.cpp +++ b/src/parsers/smt2/smt2parser.cpp @@ -1064,8 +1064,13 @@ namespace smt2 { void parse_string() { SASSERT(curr() == scanner::STRING_TOKEN); - TRACE("parse_string", tout << "new string constant: " << m_scanner.get_string() << "\n";); - expr_stack().push_back(strutil().mk_string(m_scanner.get_string())); + char const *original_token = m_scanner.get_string(); + size_t bufsize = strlen(original_token); + char * buf = alloc_svect(char, bufsize + 1); + strncpy(buf, original_token, bufsize); + buf[bufsize] = '\0'; + TRACE("parse_string", tout << "new string constant: " << buf << " length=" << bufsize << "\n";); + expr_stack().push_back(strutil().mk_string(buf)); next(); } diff --git a/src/test/smt2print_parse.cpp b/src/test/smt2print_parse.cpp index 349a3c9c8..1b491a022 100644 --- a/src/test/smt2print_parse.cpp +++ b/src/test/smt2print_parse.cpp @@ -7,8 +7,9 @@ void test_print(Z3_context ctx, Z3_ast a) { Z3_set_ast_print_mode(ctx, Z3_PRINT_SMTLIB2_COMPLIANT); char const* spec1 = Z3_benchmark_to_smtlib_string(ctx, "test", 0, 0, 0, 0, 0, a); - std::cout << spec1 << "\n"; + std::cout << "spec1: benchmark->string\n" << spec1 << "\n"; + std::cout << "attempting to parse spec1...\n"; Z3_ast b = Z3_parse_smtlib2_string(ctx, spec1, @@ -18,14 +19,14 @@ void test_print(Z3_context ctx, Z3_ast a) { 0, 0, 0); - + std::cout << "parse successful, converting ast->string\n"; char const* spec2 = Z3_ast_to_string(ctx, b); - std::cout << spec2 << "\n"; + std::cout << "spec2: string->ast->string\n" << spec2 << "\n"; } void test_parseprint(char const* spec) { Z3_context ctx = Z3_mk_context(0); - std::cout << spec << "\n"; + std::cout << "spec:\n" << spec << "\n"; Z3_ast a = Z3_parse_smtlib2_string(ctx, @@ -37,8 +38,12 @@ void test_parseprint(char const* spec) { 0, 0); + std::cout << "done parsing\n"; + test_print(ctx, a); + std::cout << "done printing\n"; + Z3_del_context(ctx); } From 744d2e3c9ca7b2407fb66b27ad9f150fe02dbd29 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 3 Sep 2015 01:12:08 -0400 Subject: [PATCH 006/562] pretty-printing of string constants in AST spec2 looks good now --- src/ast/ast_smt2_pp.cpp | 15 +++++++++++++++ src/ast/ast_smt2_pp.h | 7 ++++++- src/ast/str_decl_plugin.cpp | 5 +++++ src/ast/str_decl_plugin.h | 1 + src/cmd_context/cmd_context.cpp | 5 ++++- 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/ast/ast_smt2_pp.cpp b/src/ast/ast_smt2_pp.cpp index 035e228fb..0006d508c 100644 --- a/src/ast/ast_smt2_pp.cpp +++ b/src/ast/ast_smt2_pp.cpp @@ -298,6 +298,18 @@ format * smt2_pp_environment::mk_float(rational const & val) const { return mk_string(get_manager(), s.c_str()); } +format * smt2_pp_environment::pp_str_literal(app * t) { + TRACE("parse_string", tout << "pp_str_literal\n";); + str_util & u = get_strutil(); + SASSERT(u.is_string(t)); + const char * val; + u.is_string(t, &val); + ast_manager & m = get_manager(); + string_buffer<> buf; + buf << "\"" << val << "\""; + return mk_string(m, buf.c_str()); +} + format * smt2_pp_environment::pp_arith_literal(app * t, bool decimal, unsigned decimal_prec) { arith_util & u = get_autil(); SASSERT(u.is_numeral(t) || u.is_irrational_algebraic_numeral(t)); @@ -581,6 +593,9 @@ class smt2_printer { else if (m_env.get_dlutil().is_numeral(c)) { f = m_env.pp_datalog_literal(c); } + else if (m_env.get_strutil().is_string(c)) { + f = m_env.pp_str_literal(c); + } else { buffer names; if (m().is_label_lit(c, names)) { diff --git a/src/ast/ast_smt2_pp.h b/src/ast/ast_smt2_pp.h index 8aac71b8c..17bc322bc 100644 --- a/src/ast/ast_smt2_pp.h +++ b/src/ast/ast_smt2_pp.h @@ -29,6 +29,7 @@ Revision History: #include"array_decl_plugin.h" #include"fpa_decl_plugin.h" #include"dl_decl_plugin.h" +#include"str_decl_plugin.h" #include"smt2_util.h" class smt2_pp_environment { @@ -47,12 +48,14 @@ public: virtual bv_util & get_bvutil() = 0; virtual array_util & get_arutil() = 0; virtual fpa_util & get_futil() = 0; + virtual str_util & get_strutil() = 0; virtual datalog::dl_decl_util& get_dlutil() = 0; virtual bool uses(symbol const & s) const = 0; virtual format_ns::format * pp_fdecl(func_decl * f, unsigned & len); virtual format_ns::format * pp_bv_literal(app * t, bool use_bv_lits, bool bv_neg); virtual format_ns::format * pp_arith_literal(app * t, bool decimal, unsigned prec); virtual format_ns::format * pp_float_literal(app * t, bool use_bv_lits, bool use_float_real_lits); + virtual format_ns::format * pp_str_literal(app * t); virtual format_ns::format * pp_datalog_literal(app * t); virtual format_ns::format * pp_sort(sort * s); virtual format_ns::format * pp_fdecl_ref(func_decl * f); @@ -70,14 +73,16 @@ class smt2_pp_environment_dbg : public smt2_pp_environment { bv_util m_bvutil; array_util m_arutil; fpa_util m_futil; + str_util m_strutil; datalog::dl_decl_util m_dlutil; public: - smt2_pp_environment_dbg(ast_manager & m):m_manager(m), m_autil(m), m_bvutil(m), m_arutil(m), m_futil(m), m_dlutil(m) {} + smt2_pp_environment_dbg(ast_manager & m):m_manager(m), m_autil(m), m_bvutil(m), m_arutil(m), m_futil(m), m_strutil(m), m_dlutil(m) {} virtual ast_manager & get_manager() const { return m_manager; } virtual arith_util & get_autil() { return m_autil; } virtual bv_util & get_bvutil() { return m_bvutil; } virtual array_util & get_arutil() { return m_arutil; } virtual fpa_util & get_futil() { return m_futil; } + virtual str_util & get_strutil() { return m_strutil; } virtual datalog::dl_decl_util& get_dlutil() { return m_dlutil; } virtual bool uses(symbol const & s) const { return false; } }; diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 66b6c23fc..3bd81970d 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -74,6 +74,11 @@ bool str_recognizers::is_string(expr const * n, const char ** val) const { return true; } +bool str_recognizers::is_string(expr const * n) const { + const char * tmp = 0; + return is_string(n, & tmp); +} + str_util::str_util(ast_manager &m) : str_recognizers(m.mk_family_id(symbol("str"))), m_manager(m) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 2d629e006..57829d542 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -57,6 +57,7 @@ public: family_id get_family_id() const { return get_fid(); } bool is_string(expr const * n, const char ** val) const; + bool is_string(expr const * n) const; // TODO }; diff --git a/src/cmd_context/cmd_context.cpp b/src/cmd_context/cmd_context.cpp index 0c60d876b..77cbfe132 100644 --- a/src/cmd_context/cmd_context.cpp +++ b/src/cmd_context/cmd_context.cpp @@ -25,6 +25,7 @@ Notes: #include"datatype_decl_plugin.h" #include"seq_decl_plugin.h" #include"fpa_decl_plugin.h" +#include"str_decl_plugin.h" #include"ast_pp.h" #include"var_subst.h" #include"pp.h" @@ -241,6 +242,7 @@ protected: bv_util m_bvutil; array_util m_arutil; fpa_util m_futil; + str_util m_strutil; datalog::dl_decl_util m_dlutil; format_ns::format * pp_fdecl_name(symbol const & s, func_decls const & fs, func_decl * f, unsigned & len) { @@ -261,13 +263,14 @@ protected: } public: - pp_env(cmd_context & o):m_owner(o), m_autil(o.m()), m_bvutil(o.m()), m_arutil(o.m()), m_futil(o.m()), m_dlutil(o.m()) {} + pp_env(cmd_context & o):m_owner(o), m_autil(o.m()), m_bvutil(o.m()), m_arutil(o.m()), m_futil(o.m()), m_strutil(o.m()), m_dlutil(o.m()) {} virtual ~pp_env() {} virtual ast_manager & get_manager() const { return m_owner.m(); } virtual arith_util & get_autil() { return m_autil; } virtual bv_util & get_bvutil() { return m_bvutil; } virtual array_util & get_arutil() { return m_arutil; } virtual fpa_util & get_futil() { return m_futil; } + virtual str_util & get_strutil() { return m_strutil; } virtual datalog::dl_decl_util& get_dlutil() { return m_dlutil; } virtual bool uses(symbol const & s) const { return From 8137e022e3dc27e579521d4bc32fa944462d473c Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 6 Sep 2015 20:53:08 -0400 Subject: [PATCH 007/562] load str decl plugin; recognize String sorted constants --- src/ast/str_decl_plugin.cpp | 10 ++++ src/ast/str_decl_plugin.h | 4 ++ src/cmd_context/cmd_context.cpp | 9 ++- src/cmd_context/cmd_context.h | 1 + src/smt/smt_setup.cpp | 18 ++++++ src/smt/smt_setup.h | 2 + src/smt/theory_str.cpp | 97 +++++++++++++++++++++++++++++++++ src/smt/theory_str.h | 50 +++++++++++++++++ 8 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 src/smt/theory_str.cpp create mode 100644 src/smt/theory_str.h diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 3bd81970d..b6ec25c46 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -66,6 +66,16 @@ app * str_decl_plugin::mk_string(const char * val) { return m_manager->mk_const(d); } +void str_decl_plugin::get_op_names(svector & op_names, symbol const & logic) { + // TODO + // we would do something like: + // op_names.push_back(builtin_name("<=",OP_LE)); +} + +void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { + sort_names.push_back(builtin_name("String", STRING_SORT)); +} + bool str_recognizers::is_string(expr const * n, const char ** val) const { if (!is_app_of(n, m_afid, OP_STR)) return false; diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 57829d542..854431366 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -46,6 +46,10 @@ public: unsigned arity, sort * const * domain, sort * range); app * mk_string(const char * val); + + virtual void get_op_names(svector & op_names, symbol const & logic); + + virtual void get_sort_names(svector & sort_names, symbol const & logic); // TODO }; diff --git a/src/cmd_context/cmd_context.cpp b/src/cmd_context/cmd_context.cpp index 77cbfe132..a7db2f16c 100644 --- a/src/cmd_context/cmd_context.cpp +++ b/src/cmd_context/cmd_context.cpp @@ -563,6 +563,10 @@ bool cmd_context::logic_has_fpa() const { return !has_logic() || m_logic == "QF_FP" || m_logic == "QF_FPBV"; } +bool cmd_context::logic_has_str() const { + return !has_logic() || m_logic == "QF_S"; +} + bool cmd_context::logic_has_array_core(symbol const & s) const { return s == "QF_AX" || @@ -605,6 +609,7 @@ void cmd_context::init_manager_core(bool new_manager) { register_plugin(symbol("datatype"), alloc(datatype_decl_plugin), logic_has_datatype()); register_plugin(symbol("seq"), alloc(seq_decl_plugin), logic_has_seq()); register_plugin(symbol("fpa"), alloc(fpa_decl_plugin), logic_has_fpa()); + register_plugin(symbol("str"), alloc(str_decl_plugin), logic_has_str()); } else { // the manager was created by an external module @@ -618,6 +623,7 @@ void cmd_context::init_manager_core(bool new_manager) { load_plugin(symbol("datatype"), logic_has_datatype(), fids); load_plugin(symbol("seq"), logic_has_seq(), fids); load_plugin(symbol("fpa"), logic_has_fpa(), fids); + load_plugin(symbol("str"), logic_has_str(), fids); svector::iterator it = fids.begin(); svector::iterator end = fids.end(); @@ -671,7 +677,8 @@ bool cmd_context::supported_logic(symbol const & s) const { logic_has_arith_core(s) || logic_has_bv_core(s) || logic_has_array_core(s) || logic_has_seq_core(s) || logic_has_horn(s) || - s == "QF_FP" || s == "QF_FPBV"; + s == "QF_FP" || s == "QF_FPBV" || + s == "QF_S"; } bool cmd_context::set_logic(symbol const & s) { diff --git a/src/cmd_context/cmd_context.h b/src/cmd_context/cmd_context.h index f9e50e611..37dccab8a 100644 --- a/src/cmd_context/cmd_context.h +++ b/src/cmd_context/cmd_context.h @@ -242,6 +242,7 @@ protected: bool logic_has_array() const; bool logic_has_datatype() const; bool logic_has_fpa() const; + bool logic_has_str() const; bool supported_logic(symbol const & s) const; void print_unsupported_msg() { regular_stream() << "unsupported" << std::endl; } diff --git a/src/smt/smt_setup.cpp b/src/smt/smt_setup.cpp index e6d21a1e2..5e4af91fd 100644 --- a/src/smt/smt_setup.cpp +++ b/src/smt/smt_setup.cpp @@ -31,6 +31,7 @@ Revision History: #include"theory_dl.h" #include"theory_seq_empty.h" #include"theory_fpa.h" +#include"theory_str.h" namespace smt { @@ -117,6 +118,8 @@ namespace smt { setup_QF_FP(); else if (m_logic == "QF_FPBV") setup_QF_FPBV(); + else if (m_logic == "QF_S") + setup_QF_S(); else setup_unknown(); } @@ -158,6 +161,8 @@ namespace smt { setup_QF_BVRE(); else if (m_logic == "QF_AUFLIA") setup_QF_AUFLIA(st); + else if (m_logic == "QF_S") + setup_QF_S(); else if (m_logic == "AUFLIA") setup_AUFLIA(st); else if (m_logic == "AUFLIRA") @@ -694,6 +699,11 @@ namespace smt { m_context.register_plugin(alloc(smt::theory_fpa, m_manager)); } + void setup::setup_QF_S() { + setup_QF_LIA(); + m_context.register_plugin(alloc(smt::theory_str, m_manager)); + } + bool is_arith(static_features const & st) { return st.m_num_arith_ineqs > 0 || st.m_num_arith_terms > 0 || st.m_num_arith_eqs > 0; } @@ -800,6 +810,11 @@ namespace smt { m_context.register_plugin(alloc(theory_fpa, m_manager)); } + void setup::setup_str() { + setup_arith(); + m_context.register_plugin(alloc(theory_str, m_manager)); + } + void setup::setup_unknown() { setup_arith(); setup_arrays(); @@ -808,6 +823,7 @@ namespace smt { setup_dl(); setup_seq(); setup_fpa(); + setup_str(); } void setup::setup_unknown(static_features & st) { @@ -906,6 +922,8 @@ namespace smt { return; } + // TODO setup_str() by features + setup_unknown(); } diff --git a/src/smt/smt_setup.h b/src/smt/smt_setup.h index 6cbcb9602..6beb0b239 100644 --- a/src/smt/smt_setup.h +++ b/src/smt/smt_setup.h @@ -77,6 +77,7 @@ namespace smt { void setup_QF_AUFLIA(static_features const & st); void setup_QF_FP(); void setup_QF_FPBV(); + void setup_QF_S(); void setup_LRA(); void setup_AUFLIA(bool simple_array = true); void setup_AUFLIA(static_features const & st); @@ -98,6 +99,7 @@ namespace smt { void setup_i_arith(); void setup_mi_arith(); void setup_fpa(); + void setup_str(); public: setup(context & c, smt_params & params); diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp new file mode 100644 index 000000000..bc1b59551 --- /dev/null +++ b/src/smt/theory_str.cpp @@ -0,0 +1,97 @@ +/*++ +Module Name: + + theory_str.cpp + +Abstract: + + String Theory Plugin + +Author: + + Murphy Berzish (mtrberzi) 2015-09-03 + +Revision History: + +--*/ +#include"ast_smt2_pp.h" +#include"smt_context.h" +#include"theory_str.h" +#include"smt_model_generator.h" + +namespace smt { + +theory_str::theory_str(ast_manager &m): + theory(m.mk_family_id("str")) +{ +} + +theory_str::~theory_str() { +} + +bool theory_str::internalize_atom(app * atom, bool gate_ctx) { + // TODO I have no idea if this is correct. + TRACE("t_str", tout << "internalizing atom: " << mk_ismt2_pp(atom, get_manager()) << "\n";); + SASSERT(atom->get_family_id() == get_family_id()); + + ast_manager & m = get_manager(); + context & ctx = get_context(); + + if (ctx.b_internalized(atom)) + return true; + + unsigned num_args = atom->get_num_args(); + for (unsigned i = 0; i < num_args; i++) + ctx.internalize(atom->get_arg(i), false); + + literal l(ctx.mk_bool_var(atom)); + ctx.set_var_theory(l.var(), get_id()); + + return true; +} + +bool theory_str::internalize_term(app * term) { + // TODO I have no idea if this is correct either. + ast_manager & m = get_manager(); + context & ctx = get_context(); + TRACE("t_str", tout << "internalizing term: " << mk_ismt2_pp(term, get_manager()) << "\n";); + SASSERT(term->get_family_id() == get_family_id()); + SASSERT(!ctx.e_internalized(term)); + + unsigned num_args = term->get_num_args(); + for (unsigned i = 0; i < num_args; i++) + ctx.internalize(term->get_arg(i), false); + + enode * e = (ctx.e_internalized(term)) ? ctx.get_enode(term) : + ctx.mk_enode(term, false, false, true); + + if (is_attached_to_var(e)) + return false; + + attach_new_th_var(e); + + return true; +} + +void theory_str::attach_new_th_var(enode * n) { + context & ctx = get_context(); + theory_var v = mk_var(n); + ctx.attach_th_var(n, this, v); + TRACE("t_str_detail", tout << "new theory var: " << mk_ismt2_pp(n->get_owner(), get_manager()) << " := " << v << "\n";); +} + +void theory_str::new_eq_eh(theory_var x, theory_var y) { + // TODO + TRACE("t_str", tout << "new eq: " << x << " = " << y << std::endl;); + TRACE("t_str_detail", tout << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " = " << + mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); +} + +void theory_str::new_diseq_eh(theory_var x, theory_var y) { + // TODO + TRACE("t_str", tout << "new diseq: " << x << " != " << y << std::endl;); + TRACE("t_str_detail", tout << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " != " << + mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); +} + +}; /* namespace smt */ diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h new file mode 100644 index 000000000..7bb5b5148 --- /dev/null +++ b/src/smt/theory_str.h @@ -0,0 +1,50 @@ +/*++ +Module Name: + + theory_str.h + +Abstract: + + String Theory Plugin + +Author: + + Murphy Berzish (mtrberzi) 2015-09-03 + +Revision History: + +--*/ +#ifndef _THEORY_STR_H_ +#define _THEORY_STR_H_ + +#include"smt_theory.h" +#include"trail.h" +#include"th_rewriter.h" +#include"value_factory.h" +#include"smt_model_generator.h" + +namespace smt { + + class str_value_factory : public value_factory { + // TODO + }; + + class theory_str : public theory { + // TODO + protected: + virtual bool internalize_atom(app * atom, bool gate_ctx); + virtual bool internalize_term(app * term); + + virtual void new_eq_eh(theory_var, theory_var); + virtual void new_diseq_eh(theory_var, theory_var); + virtual theory* mk_fresh(context*) { return alloc(theory_str, get_manager()); } + public: + theory_str(ast_manager& m); + virtual ~theory_str(); + protected: + void attach_new_th_var(enode * n); + }; + +}; + +#endif /* _THEORY_STR_H_ */ From f0c301e920affab0ae2586a762a74280b488412f Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 6 Sep 2015 21:05:32 -0400 Subject: [PATCH 008/562] register Concat function now reaches str_decl_plugin::mk_func_decl() --- src/ast/str_decl_plugin.cpp | 19 ++++++++++++++----- src/ast/str_decl_plugin.h | 5 ++++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index b6ec25c46..eb309ecf0 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -23,7 +23,8 @@ Revision History: str_decl_plugin::str_decl_plugin(): m_strv_sym("String"), - m_str_decl(0){ + m_str_decl(0), + m_concat_decl(0){ } str_decl_plugin::~str_decl_plugin(){ @@ -39,7 +40,17 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_str_decl = m->mk_sort(symbol("String"), sort_info(id, STRING_SORT)); m->inc_ref(m_str_decl); sort * s = m_str_decl; - /* TODO mk_pred, etc. */ + +#define MK_AC_OP(FIELD, NAME, KIND, SORT) { \ + func_decl_info info(id, KIND); \ + info.set_associative(); \ + info.set_flat_associative(); \ + info.set_commutative(); \ + FIELD = m->mk_func_decl(symbol(NAME), SORT, SORT, SORT, info); \ + m->inc_ref(FIELD); \ + } + + MK_AC_OP(m_concat_decl, "Concat", OP_STRCAT, s); } decl_plugin * str_decl_plugin::mk_fresh() { @@ -67,9 +78,7 @@ app * str_decl_plugin::mk_string(const char * val) { } void str_decl_plugin::get_op_names(svector & op_names, symbol const & logic) { - // TODO - // we would do something like: - // op_names.push_back(builtin_name("<=",OP_LE)); + op_names.push_back(builtin_name("Concat", OP_STRCAT)); } void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 854431366..16e1ef4a3 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -25,7 +25,8 @@ enum str_sort_kind { enum str_op_kind { OP_STR, /* string constants */ - + // + OP_STRCAT, LAST_STR_OP }; @@ -34,6 +35,8 @@ protected: symbol m_strv_sym; sort * m_str_decl; + func_decl * m_concat_decl; + virtual void set_manager(ast_manager * m, family_id id); public: str_decl_plugin(); From 7f0d9157ac9ec470f958902401e957e72871f177 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 6 Sep 2015 21:47:57 -0400 Subject: [PATCH 009/562] at least for now, Concat is no longer associative this means that we'll always have (Concat a b) instead of variadic forms --- src/ast/str_decl_plugin.cpp | 31 ++++++++++++++++++++----------- src/ast/str_decl_plugin.h | 2 ++ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index eb309ecf0..70c8a6ebe 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -41,16 +41,11 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m->inc_ref(m_str_decl); sort * s = m_str_decl; -#define MK_AC_OP(FIELD, NAME, KIND, SORT) { \ - func_decl_info info(id, KIND); \ - info.set_associative(); \ - info.set_flat_associative(); \ - info.set_commutative(); \ - FIELD = m->mk_func_decl(symbol(NAME), SORT, SORT, SORT, info); \ - m->inc_ref(FIELD); \ - } +#define MK_OP(FIELD, NAME, KIND, SORT) \ + FIELD = m->mk_func_decl(symbol(NAME), SORT, SORT, SORT, func_decl_info(id, KIND)); \ + m->inc_ref(FIELD) - MK_AC_OP(m_concat_decl, "Concat", OP_STRCAT, s); + MK_OP(m_concat_decl, "Concat", OP_STRCAT, s); } decl_plugin * str_decl_plugin::mk_fresh() { @@ -64,10 +59,24 @@ sort * str_decl_plugin::mk_sort(decl_kind k, unsigned num_parameters, parameter } } +func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { + switch(k) { + case OP_STRCAT: return m_concat_decl; + default: return 0; + } +} + func_decl * str_decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters, parameter const * parameters, unsigned arity, sort * const * domain, sort * range) { - /* TODO */ - m_manager->raise_exception("str_decl_plugin::mk_func_decl() not yet implemented"); return 0; + if (k == OP_STR) { + m_manager->raise_exception("OP_STR not yet implemented in mk_func_decl!"); + return 0; + } + if (arity == 0) { + m_manager->raise_exception("no arguments supplied to string operator"); + return 0; + } + return mk_func_decl(k); } app * str_decl_plugin::mk_string(const char * val) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 16e1ef4a3..d190e9ff7 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -38,6 +38,8 @@ protected: func_decl * m_concat_decl; virtual void set_manager(ast_manager * m, family_id id); + + func_decl * mk_func_decl(decl_kind k); public: str_decl_plugin(); virtual ~str_decl_plugin(); From dc86385e7fb635484b596fc9fdd7ab17274e0e55 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 7 Sep 2015 16:13:48 -0400 Subject: [PATCH 010/562] add Length function to theory of strings --- src/ast/str_decl_plugin.cpp | 13 +++++++++++++ src/ast/str_decl_plugin.h | 6 ++++++ src/smt/theory_str.cpp | 37 +++++++++++++++++++++++++++++++++++-- src/smt/theory_str.h | 8 ++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 70c8a6ebe..fa0a26f25 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -33,6 +33,9 @@ str_decl_plugin::~str_decl_plugin(){ void str_decl_plugin::finalize(void) { #define DEC_REF(decl) if (decl) { m_manager->dec_ref(decl); } ((void) 0) DEC_REF(m_str_decl); + DEC_REF(m_concat_decl); + DEC_REF(m_length_decl); + DEC_REF(m_int_sort); } void str_decl_plugin::set_manager(ast_manager * m, family_id id) { @@ -41,11 +44,19 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m->inc_ref(m_str_decl); sort * s = m_str_decl; + m_arith_fid = m_manager->mk_family_id("arith"); + m_int_sort = m_manager->mk_sort(m_arith_fid, INT_SORT); + SASSERT(m_int_sort != 0); // arith_decl_plugin must be installed before str_decl_plugin. + m_manager->inc_ref(m_int_sort); + sort * i = m_int_sort; + #define MK_OP(FIELD, NAME, KIND, SORT) \ FIELD = m->mk_func_decl(symbol(NAME), SORT, SORT, SORT, func_decl_info(id, KIND)); \ m->inc_ref(FIELD) MK_OP(m_concat_decl, "Concat", OP_STRCAT, s); + + m_length_decl = m->mk_func_decl(symbol("Length"), s, i); m_manager->inc_ref(m_length_decl); } decl_plugin * str_decl_plugin::mk_fresh() { @@ -62,6 +73,7 @@ sort * str_decl_plugin::mk_sort(decl_kind k, unsigned num_parameters, parameter func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { switch(k) { case OP_STRCAT: return m_concat_decl; + case OP_STRLEN: return m_length_decl; default: return 0; } } @@ -88,6 +100,7 @@ app * str_decl_plugin::mk_string(const char * val) { void str_decl_plugin::get_op_names(svector & op_names, symbol const & logic) { op_names.push_back(builtin_name("Concat", OP_STRCAT)); + op_names.push_back(builtin_name("Length", OP_STRLEN)); } void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index d190e9ff7..3fd5fb7e6 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -18,6 +18,7 @@ Revision History: #define _STR_DECL_PLUGIN_H_ #include"ast.h" +#include"arith_decl_plugin.h" enum str_sort_kind { STRING_SORT, @@ -27,6 +28,7 @@ enum str_op_kind { OP_STR, /* string constants */ // OP_STRCAT, + OP_STRLEN, LAST_STR_OP }; @@ -35,7 +37,11 @@ protected: symbol m_strv_sym; sort * m_str_decl; + sort * m_int_sort; + family_id m_arith_fid; + func_decl * m_concat_decl; + func_decl * m_length_decl; virtual void set_manager(ast_manager * m, family_id id); diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index bc1b59551..2bf67ed81 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -31,7 +31,7 @@ theory_str::~theory_str() { bool theory_str::internalize_atom(app * atom, bool gate_ctx) { // TODO I have no idea if this is correct. - TRACE("t_str", tout << "internalizing atom: " << mk_ismt2_pp(atom, get_manager()) << "\n";); + TRACE("t_str", tout << "internalizing atom: " << mk_ismt2_pp(atom, get_manager()) << std::endl;); SASSERT(atom->get_family_id() == get_family_id()); ast_manager & m = get_manager(); @@ -54,7 +54,7 @@ bool theory_str::internalize_term(app * term) { // TODO I have no idea if this is correct either. ast_manager & m = get_manager(); context & ctx = get_context(); - TRACE("t_str", tout << "internalizing term: " << mk_ismt2_pp(term, get_manager()) << "\n";); + TRACE("t_str", tout << "internalizing term: " << mk_ismt2_pp(term, get_manager()) << std::endl;); SASSERT(term->get_family_id() == get_family_id()); SASSERT(!ctx.e_internalized(term)); @@ -80,6 +80,20 @@ void theory_str::attach_new_th_var(enode * n) { TRACE("t_str_detail", tout << "new theory var: " << mk_ismt2_pp(n->get_owner(), get_manager()) << " := " << v << "\n";); } +void theory_str::init_search_eh() { + ast_manager & m = get_manager(); + context & ctx = get_context(); + TRACE("t_str", + tout << "search started, assignments are:" << std::endl; + expr_ref_vector assignment(m); + ctx.get_assignments(assignment); + for (expr_ref_vector::iterator i = assignment.begin(); i != assignment.end(); ++i) { + expr * ex = *i; + tout << mk_ismt2_pp(ex, m) << std::endl; + } + ); +} + void theory_str::new_eq_eh(theory_var x, theory_var y) { // TODO TRACE("t_str", tout << "new eq: " << x << " = " << y << std::endl;); @@ -94,4 +108,23 @@ void theory_str::new_diseq_eh(theory_var x, theory_var y) { mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); } +void theory_str::relevant_eh(app * n) { + TRACE("t_str", tout << "relevant: " << mk_ismt2_pp(n, get_manager()) << "\n";); +} + +void theory_str::assign_eh(bool_var v, bool is_true) { + context & ctx = get_context(); + TRACE("t_str", tout << "assert: v" << v << " #" << ctx.bool_var2expr(v)->get_id() << " is_true: " << is_true << "\n";); +} + +void theory_str::push_scope_eh() { + TRACE("t_str", tout << "push" << std::endl;); +} + +final_check_status theory_str::final_check_eh() { + // TODO + TRACE("t_str", tout << "final check" << std::endl;); + return FC_DONE; +} + }; /* namespace smt */ diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 7bb5b5148..5ee5502de 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -38,6 +38,14 @@ namespace smt { virtual void new_eq_eh(theory_var, theory_var); virtual void new_diseq_eh(theory_var, theory_var); virtual theory* mk_fresh(context*) { return alloc(theory_str, get_manager()); } + + virtual void init_search_eh(); + + virtual void relevant_eh(app * n); + virtual void assign_eh(bool_var v, bool is_true); + virtual void push_scope_eh(); + + virtual final_check_status final_check_eh(); public: theory_str(ast_manager& m); virtual ~theory_str(); From 9b04f1570f45b55d409f981bd389c46dc14825e0 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 7 Sep 2015 19:40:25 -0400 Subject: [PATCH 011/562] instantiate length axiom for concatenation --- src/ast/str_decl_plugin.cpp | 3 +- src/smt/theory_str.cpp | 80 ++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 15 ++++++- 3 files changed, 94 insertions(+), 4 deletions(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index fa0a26f25..0e74493ff 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -24,7 +24,8 @@ Revision History: str_decl_plugin::str_decl_plugin(): m_strv_sym("String"), m_str_decl(0), - m_concat_decl(0){ + m_concat_decl(0), + m_length_decl(0){ } str_decl_plugin::~str_decl_plugin(){ diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 2bf67ed81..c6d51b1a4 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -18,17 +18,39 @@ Revision History: #include"smt_context.h" #include"theory_str.h" #include"smt_model_generator.h" +#include"ast_pp.h" +#include"ast_ll_pp.h" namespace smt { -theory_str::theory_str(ast_manager &m): - theory(m.mk_family_id("str")) +theory_str::theory_str(ast_manager & m): + theory(m.mk_family_id("str")), + search_started(false), + m_autil(m) { } theory_str::~theory_str() { } +void theory_str::assert_axiom(unsigned num_lits, literal * lits) { + context & ctx = get_context(); + TRACE("t_str_detail", + tout << "assert_axiom: literals:\n"; + for (unsigned i = 0; i < num_lits; ++i) { + expr * e = ctx.bool_var2expr(lits[i].var()); + if (lits[i].sign()) + tout << "not "; + tout << mk_pp(e, get_manager()) << " "; + tout << "\n"; + }); + ctx.mk_th_axiom(get_id(), num_lits, lits); +} + +void theory_str::assert_axiom(literal l) { + assert_axiom(1, &l); +} + bool theory_str::internalize_atom(app * atom, bool gate_ctx) { // TODO I have no idea if this is correct. TRACE("t_str", tout << "internalizing atom: " << mk_ismt2_pp(atom, get_manager()) << std::endl;); @@ -70,9 +92,62 @@ bool theory_str::internalize_term(app * term) { attach_new_th_var(e); + if (is_concat(term)) { + instantiate_concat_axiom(e); + } + return true; } +app * theory_str::mk_strlen(app * e) { + expr * args[1] = {e}; + return get_manager().mk_app(get_id(), OP_STRLEN, 0, 0, 1, args); +} + +/* + * Instantiate an axiom of the following form: + * Length(Concat(x, y)) = Length(x) + Length(y) + */ +void theory_str::instantiate_concat_axiom(enode * cat) { + SASSERT(is_concat(cat)); + app * a_cat = cat->get_owner(); + + context & ctx = get_context(); + ast_manager & m = get_manager(); + + // build LHS + expr_ref len_xy(m); + // TODO re-use ASTs for length subexpressions, like in old Z3-str? + // TODO should we use str_util for these and other expressions? + len_xy = mk_strlen(a_cat); + SASSERT(len_xy); + + // build RHS: start by extracting x and y from Concat(x, y) + unsigned nArgs = a_cat->get_num_args(); + SASSERT(nArgs == 2); + app * a_x = to_app(a_cat->get_arg(0)); + app * a_y = to_app(a_cat->get_arg(1)); + + expr_ref len_x(m); + len_x = mk_strlen(a_x); + SASSERT(len_x); + + expr_ref len_y(m); + len_y = mk_strlen(a_y); + SASSERT(len_y); + + // now build len_x + len_y + app * len_x_plus_len_y = m_autil.mk_add(len_x, len_y); + SASSERT(len_x_plus_len_y); + + TRACE("t_str", tout << mk_bounded_pp(len_xy, m) << " = " << mk_bounded_pp(len_x_plus_len_y, m) << "\n";); + + // finally assert equality between the two subexpressions + literal l(mk_eq(len_xy, len_x_plus_len_y, true)); + ctx.mark_as_relevant(l); + assert_axiom(l); +} + void theory_str::attach_new_th_var(enode * n) { context & ctx = get_context(); theory_var v = mk_var(n); @@ -92,6 +167,7 @@ void theory_str::init_search_eh() { tout << mk_ismt2_pp(ex, m) << std::endl; } ); + search_started = true; } void theory_str::new_eq_eh(theory_var x, theory_var y) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 5ee5502de..867c4316b 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -22,6 +22,7 @@ Revision History: #include"th_rewriter.h" #include"value_factory.h" #include"smt_model_generator.h" +#include"arith_decl_plugin.h" namespace smt { @@ -31,6 +32,8 @@ namespace smt { class theory_str : public theory { // TODO + protected: + bool search_started; protected: virtual bool internalize_atom(app * atom, bool gate_ctx); virtual bool internalize_term(app * term); @@ -46,9 +49,19 @@ namespace smt { virtual void push_scope_eh(); virtual final_check_status final_check_eh(); + + void assert_axiom(unsigned num_lits, literal * lits); + void assert_axiom(literal l); + + app * mk_strlen(app * e); + + bool is_concat(app const * a) const { return a->is_app_of(get_id(), OP_STRCAT); } + bool is_concat(enode const * n) const { return is_concat(n->get_owner()); } + void instantiate_concat_axiom(enode * cat); public: - theory_str(ast_manager& m); + theory_str(ast_manager & m); virtual ~theory_str(); + arith_util m_autil; protected: void attach_new_th_var(enode * n); }; From 799fd07c85f8ec89552454334fe8a3e4c3f0273e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 7 Sep 2015 19:51:52 -0400 Subject: [PATCH 012/562] optimization: return integer consts for strlen() over constant strings --- src/smt/theory_str.cpp | 14 +++++++++++--- src/smt/theory_str.h | 3 ++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index c6d51b1a4..568e6b5ae 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -26,7 +26,8 @@ namespace smt { theory_str::theory_str(ast_manager & m): theory(m.mk_family_id("str")), search_started(false), - m_autil(m) + m_autil(m), + m_strutil(m) { } @@ -100,8 +101,15 @@ bool theory_str::internalize_term(app * term) { } app * theory_str::mk_strlen(app * e) { - expr * args[1] = {e}; - return get_manager().mk_app(get_id(), OP_STRLEN, 0, 0, 1, args); + if (m_strutil.is_string(e)) { + const char * strval = 0; + m_strutil.is_string(e, &strval); + int len = strlen(strval); + return m_autil.mk_numeral(rational(len), true); + } else { + expr * args[1] = {e}; + return get_manager().mk_app(get_id(), OP_STRLEN, 0, 0, 1, args); + } } /* diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 867c4316b..a583a106e 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -34,6 +34,8 @@ namespace smt { // TODO protected: bool search_started; + arith_util m_autil; + str_util m_strutil; protected: virtual bool internalize_atom(app * atom, bool gate_ctx); virtual bool internalize_term(app * term); @@ -61,7 +63,6 @@ namespace smt { public: theory_str(ast_manager & m); virtual ~theory_str(); - arith_util m_autil; protected: void attach_new_th_var(enode * n); }; From 992fff8ba8c882b0724aafca2620bd8dd151365a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 10 Sep 2015 18:43:14 -0400 Subject: [PATCH 013/562] set up theory of arithmetic correctly closes #1 --- src/cmd_context/cmd_context.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cmd_context/cmd_context.cpp b/src/cmd_context/cmd_context.cpp index a7db2f16c..394350879 100644 --- a/src/cmd_context/cmd_context.cpp +++ b/src/cmd_context/cmd_context.cpp @@ -517,7 +517,8 @@ bool cmd_context::logic_has_arith_core(symbol const & s) const { s == "LRA" || s == "QF_FP" || s == "QF_FPBV" || - s == "HORN"; + s == "HORN" || + s == "QF_S"; } bool cmd_context::logic_has_arith() const { From 4d5a0ea53f42258e375be664aea15bbcbbc66a12 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 26 Sep 2015 18:51:02 -0400 Subject: [PATCH 014/562] WIP add axioms --- src/ast/str_decl_plugin.cpp | 9 ++- src/ast/str_decl_plugin.h | 1 + src/smt/theory_str.cpp | 125 ++++++++++++++++++++++++++++-------- src/smt/theory_str.h | 25 ++++---- 4 files changed, 120 insertions(+), 40 deletions(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 0e74493ff..1502e3d3a 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -25,7 +25,10 @@ str_decl_plugin::str_decl_plugin(): m_strv_sym("String"), m_str_decl(0), m_concat_decl(0), - m_length_decl(0){ + m_length_decl(0), + m_arith_plugin(0), + m_arith_fid(0), + m_int_sort(0){ } str_decl_plugin::~str_decl_plugin(){ @@ -45,7 +48,11 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m->inc_ref(m_str_decl); sort * s = m_str_decl; + SASSERT(m_manager->has_plugin(symbol("arith"))); m_arith_fid = m_manager->mk_family_id("arith"); + m_arith_plugin = static_cast(m_manager->get_plugin(m_arith_fid)); + SASSERT(m_arith_plugin); + m_int_sort = m_manager->mk_sort(m_arith_fid, INT_SORT); SASSERT(m_int_sort != 0); // arith_decl_plugin must be installed before str_decl_plugin. m_manager->inc_ref(m_int_sort); diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 3fd5fb7e6..7e75fbaf0 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -37,6 +37,7 @@ protected: symbol m_strv_sym; sort * m_str_decl; + arith_decl_plugin * m_arith_plugin; sort * m_int_sort; family_id m_arith_fid; diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 568e6b5ae..ff4b3dd76 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -34,22 +34,25 @@ theory_str::theory_str(ast_manager & m): theory_str::~theory_str() { } -void theory_str::assert_axiom(unsigned num_lits, literal * lits) { +void theory_str::assert_axiom(ast * a) { + /* + if (search_started) { + // effectively Z3_theory_assert_axiom + NOT_IMPLEMENTED_YET(); + } else { + // effectively Z3_assert_cnstr + context & ctx = get_context(); + ctx.assert_expr(to_expr(a)); + } + */ + TRACE("t_str_detail", tout << "asserting " << mk_ismt2_pp(a, get_manager()) << "\n";); + expr * e = to_expr(a); context & ctx = get_context(); - TRACE("t_str_detail", - tout << "assert_axiom: literals:\n"; - for (unsigned i = 0; i < num_lits; ++i) { - expr * e = ctx.bool_var2expr(lits[i].var()); - if (lits[i].sign()) - tout << "not "; - tout << mk_pp(e, get_manager()) << " "; - tout << "\n"; - }); - ctx.mk_th_axiom(get_id(), num_lits, lits); -} - -void theory_str::assert_axiom(literal l) { - assert_axiom(1, &l); + ctx.internalize(e, false); + literal lit(ctx.get_literal(e)); + ctx.mark_as_relevant(lit); + ctx.mk_th_axiom(get_id(), 1, &lit); + TRACE("t_str_detail", tout << "done asserting " << mk_ismt2_pp(a, get_manager()) << "\n";); } bool theory_str::internalize_atom(app * atom, bool gate_ctx) { @@ -93,15 +96,17 @@ bool theory_str::internalize_term(app * term) { attach_new_th_var(e); + /* if (is_concat(term)) { instantiate_concat_axiom(e); } + */ return true; } app * theory_str::mk_strlen(app * e) { - if (m_strutil.is_string(e)) { + /*if (m_strutil.is_string(e)) {*/ if (false) { const char * strval = 0; m_strutil.is_string(e, &strval); int len = strlen(strval); @@ -145,22 +150,90 @@ void theory_str::instantiate_concat_axiom(enode * cat) { SASSERT(len_y); // now build len_x + len_y - app * len_x_plus_len_y = m_autil.mk_add(len_x, len_y); + expr_ref len_x_plus_len_y(m); + len_x_plus_len_y = m_autil.mk_add(len_x, len_y); SASSERT(len_x_plus_len_y); - TRACE("t_str", tout << mk_bounded_pp(len_xy, m) << " = " << mk_bounded_pp(len_x_plus_len_y, m) << "\n";); - // finally assert equality between the two subexpressions - literal l(mk_eq(len_xy, len_x_plus_len_y, true)); - ctx.mark_as_relevant(l); - assert_axiom(l); + app * eq = m.mk_eq(len_xy, len_x_plus_len_y); + SASSERT(eq); + TRACE("t_str", tout << mk_bounded_pp(eq, m) << std::endl;); + assert_axiom(eq); +} + +/* + * Add axioms that are true for any string variable: + * 1. Length(x) >= 0 + * 2. Length(x) == 0 <=> x == "" + */ +void theory_str::instantiate_basic_string_axioms(enode * str) { + // generate a stronger axiom for constant strings + if (m_strutil.is_string(str->get_owner())) { + // TODO + } else { + // TODO keep track of which enodes we have added axioms for, so we don't add the same ones twice? + app * a_str = str->get_owner(); + context & ctx = get_context(); + ast_manager & m = get_manager(); + + // TODO find out why these are crashing the SMT solver + + // build axiom 1: Length(a_str) >= 0 + { + // build LHS + expr_ref len_str(m); + len_str = mk_strlen(a_str); + SASSERT(len_str); + // build RHS + expr_ref zero(m); + zero = m_autil.mk_numeral(rational(0), true); + SASSERT(zero); + // build LHS >= RHS and assert + app * lhs_ge_rhs = m_autil.mk_ge(len_str, zero); + SASSERT(lhs_ge_rhs); + // TODO verify that this works + TRACE("t_str_detail", tout << "string axiom 1: " << mk_bounded_pp(lhs_ge_rhs, m) << std::endl;); + assert_axiom(lhs_ge_rhs); + } + + /* + // build axiom 2: Length(a_str) == 0 <=> a_str == "" + { + // build LHS of iff + expr_ref len_str(m); + len_str = mk_strlen(a_str); + SASSERT(len_str); + expr_ref zero(m); + zero = m_autil.mk_numeral(rational(0), true); + SASSERT(zero); + expr_ref lhs(m); + lhs = ctx.mk_eq_atom(len_str, zero); + SASSERT(lhs); + // build RHS of iff + expr_ref empty_str(m); + empty_str = m_strutil.mk_string(""); + SASSERT(empty_str); + expr_ref rhs(m); + rhs = ctx.mk_eq_atom(a_str, empty_str); + SASSERT(rhs); + // build LHS <=> RHS and assert + TRACE("t_str_detail", tout << "string axiom 2: " << mk_bounded_pp(lhs, m) << " <=> " << mk_bounded_pp(rhs, m) << std::endl;); + // TODO this is kind of a hack, maybe just ctx.assert_expr() will be enough? + literal l(mk_eq(lhs, rhs, true)); + ctx.mark_as_relevant(l); + assert_axiom(l); + } + */ + } } void theory_str::attach_new_th_var(enode * n) { context & ctx = get_context(); theory_var v = mk_var(n); ctx.attach_th_var(n, this, v); - TRACE("t_str_detail", tout << "new theory var: " << mk_ismt2_pp(n->get_owner(), get_manager()) << " := " << v << "\n";); + TRACE("t_str_detail", tout << "new theory var: " << mk_ismt2_pp(n->get_owner(), get_manager()) << " := v#" << v << std::endl;); + // probably okay...note however that this seems to miss constants and functions + //instantiate_basic_string_axioms(n); } void theory_str::init_search_eh() { @@ -180,14 +253,14 @@ void theory_str::init_search_eh() { void theory_str::new_eq_eh(theory_var x, theory_var y) { // TODO - TRACE("t_str", tout << "new eq: " << x << " = " << y << std::endl;); + TRACE("t_str", tout << "new eq: v#" << x << " = v#" << y << std::endl;); TRACE("t_str_detail", tout << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " = " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); } void theory_str::new_diseq_eh(theory_var x, theory_var y) { // TODO - TRACE("t_str", tout << "new diseq: " << x << " != " << y << std::endl;); + TRACE("t_str", tout << "new diseq: v#" << x << " != v#" << y << std::endl;); TRACE("t_str_detail", tout << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " != " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); } @@ -198,7 +271,7 @@ void theory_str::relevant_eh(app * n) { void theory_str::assign_eh(bool_var v, bool is_true) { context & ctx = get_context(); - TRACE("t_str", tout << "assert: v" << v << " #" << ctx.bool_var2expr(v)->get_id() << " is_true: " << is_true << "\n";); + TRACE("t_str", tout << "assert: v" << v << " #" << ctx.bool_var2expr(v)->get_id() << " is_true: " << is_true << std::endl;); } void theory_str::push_scope_eh() { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index a583a106e..0e7b0bcc8 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -36,6 +36,18 @@ namespace smt { bool search_started; arith_util m_autil; str_util m_strutil; + protected: + void assert_axiom(ast * e); + + app * mk_strlen(app * e); + + bool is_concat(app const * a) const { return a->is_app_of(get_id(), OP_STRCAT); } + bool is_concat(enode const * n) const { return is_concat(n->get_owner()); } + void instantiate_concat_axiom(enode * cat); + void instantiate_basic_string_axioms(enode * str); + public: + theory_str(ast_manager & m); + virtual ~theory_str(); protected: virtual bool internalize_atom(app * atom, bool gate_ctx); virtual bool internalize_term(app * term); @@ -51,19 +63,6 @@ namespace smt { virtual void push_scope_eh(); virtual final_check_status final_check_eh(); - - void assert_axiom(unsigned num_lits, literal * lits); - void assert_axiom(literal l); - - app * mk_strlen(app * e); - - bool is_concat(app const * a) const { return a->is_app_of(get_id(), OP_STRCAT); } - bool is_concat(enode const * n) const { return is_concat(n->get_owner()); } - void instantiate_concat_axiom(enode * cat); - public: - theory_str(ast_manager & m); - virtual ~theory_str(); - protected: void attach_new_th_var(enode * n); }; From f6affe64d0cb72f1e434e05d69d4d7845600fb0a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 26 Sep 2015 21:02:56 -0400 Subject: [PATCH 015/562] deferred addition of basic string axioms no longer crashes the solver and got our first correct UNSAT! --- src/smt/theory_str.cpp | 53 +++++++++++++++++++++++++++++++++++------- src/smt/theory_str.h | 7 ++++++ 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ff4b3dd76..b99087f29 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -117,6 +117,20 @@ app * theory_str::mk_strlen(app * e) { } } +bool theory_str::can_propagate() { + return !m_basicstr_axiom_todo.empty(); +} + +void theory_str::propagate() { + TRACE("t_str_detail", tout << "trying to propagate..." << std::endl;); + while (can_propagate()) { + for (unsigned i = 0; i < m_basicstr_axiom_todo.size(); ++i) { + instantiate_basic_string_axioms(m_basicstr_axiom_todo[i]); + } + m_basicstr_axiom_todo.reset(); + } +} + /* * Instantiate an axiom of the following form: * Length(Concat(x, y)) = Length(x) + Length(y) @@ -165,17 +179,32 @@ void theory_str::instantiate_concat_axiom(enode * cat) { * Add axioms that are true for any string variable: * 1. Length(x) >= 0 * 2. Length(x) == 0 <=> x == "" + * If the term is a string constant, we can assert something stronger: + * Length(x) == strlen(x) */ void theory_str::instantiate_basic_string_axioms(enode * str) { - // generate a stronger axiom for constant strings - if (m_strutil.is_string(str->get_owner())) { - // TODO - } else { - // TODO keep track of which enodes we have added axioms for, so we don't add the same ones twice? - app * a_str = str->get_owner(); - context & ctx = get_context(); - ast_manager & m = get_manager(); + // TODO keep track of which enodes we have added axioms for, so we don't add the same ones twice? + context & ctx = get_context(); + ast_manager & m = get_manager(); + + // generate a stronger axiom for constant strings + app * a_str = str->get_owner(); + if (m_strutil.is_string(str->get_owner())) { + expr_ref len_str(m); + len_str = mk_strlen(a_str); + SASSERT(len_str); + + const char * strconst = 0; + m_strutil.is_string(str->get_owner(), & strconst); + TRACE("t_str_detail", tout << "instantiating constant string axioms for \"" << strconst << "\"" << std::endl;); + int l = strlen(strconst); + expr_ref len(m_autil.mk_numeral(rational(l), true), m); + + literal lit(mk_eq(len_str, len, false)); + ctx.mark_as_relevant(lit); + ctx.mk_th_axiom(get_id(), 1, &lit); + } else { // TODO find out why these are crashing the SMT solver // build axiom 1: Length(a_str) >= 0 @@ -233,7 +262,13 @@ void theory_str::attach_new_th_var(enode * n) { ctx.attach_th_var(n, this, v); TRACE("t_str_detail", tout << "new theory var: " << mk_ismt2_pp(n->get_owner(), get_manager()) << " := v#" << v << std::endl;); // probably okay...note however that this seems to miss constants and functions - //instantiate_basic_string_axioms(n); + m_basicstr_axiom_todo.push_back(n); +} + +void theory_str::reset_eh() { + TRACE("t_str", tout << "resetting" << std::endl;); + m_basicstr_axiom_todo.reset(); + pop_scope_eh(0); } void theory_str::init_search_eh() { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 0e7b0bcc8..a336ec649 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -36,6 +36,8 @@ namespace smt { bool search_started; arith_util m_autil; str_util m_strutil; + + ptr_vector m_basicstr_axiom_todo; protected: void assert_axiom(ast * e); @@ -62,6 +64,11 @@ namespace smt { virtual void assign_eh(bool_var v, bool is_true); virtual void push_scope_eh(); + virtual void reset_eh(); + + virtual bool can_propagate(); + virtual void propagate(); + virtual final_check_status final_check_eh(); void attach_new_th_var(enode * n); }; From 4085db99906b01321921132d458c736ef1ea4239 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 26 Sep 2015 23:35:23 -0400 Subject: [PATCH 016/562] recursive descent through all assertions to discover all String terms set up axioms on these terms to be asserted during propagation --- src/smt/theory_str.cpp | 56 +++++++++++++++++++++++++++++++++--------- src/smt/theory_str.h | 2 ++ 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index b99087f29..63378a700 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -122,13 +122,14 @@ bool theory_str::can_propagate() { } void theory_str::propagate() { - TRACE("t_str_detail", tout << "trying to propagate..." << std::endl;); while (can_propagate()) { + TRACE("t_str_detail", tout << "propagating..." << std::endl;); for (unsigned i = 0; i < m_basicstr_axiom_todo.size(); ++i) { instantiate_basic_string_axioms(m_basicstr_axiom_todo[i]); } m_basicstr_axiom_todo.reset(); } + TRACE("t_str_detail", tout << "done propagating" << std::endl;); } /* @@ -205,8 +206,6 @@ void theory_str::instantiate_basic_string_axioms(enode * str) { ctx.mark_as_relevant(lit); ctx.mk_th_axiom(get_id(), 1, &lit); } else { - // TODO find out why these are crashing the SMT solver - // build axiom 1: Length(a_str) >= 0 { // build LHS @@ -261,8 +260,6 @@ void theory_str::attach_new_th_var(enode * n) { theory_var v = mk_var(n); ctx.attach_th_var(n, this, v); TRACE("t_str_detail", tout << "new theory var: " << mk_ismt2_pp(n->get_owner(), get_manager()) << " := v#" << v << std::endl;); - // probably okay...note however that this seems to miss constants and functions - m_basicstr_axiom_todo.push_back(n); } void theory_str::reset_eh() { @@ -271,18 +268,55 @@ void theory_str::reset_eh() { pop_scope_eh(0); } +void theory_str::set_up_axioms(expr * ex) { + // TODO check to make sure we don't set up axioms on the same term twice + TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << std::endl;); + + ast_manager & m = get_manager(); + context & ctx = get_context(); + + sort * ex_sort = m.get_sort(ex); + sort * str_sort = m.mk_sort(get_family_id(), STRING_SORT); + + if (ex_sort == str_sort) { + TRACE("t_str_detail", tout << "expr is of sort String" << std::endl;); + // set up basic string axioms + enode * n = ctx.get_enode(ex); + SASSERT(n); + m_basicstr_axiom_todo.push_back(n); + } else { + TRACE("t_str_detail", tout << "expr is of wrong sort, ignoring" << std::endl;); + } + + // if expr is an application, recursively inspect all arguments + if (is_app(ex)) { + app * term = (app*)ex; + unsigned num_args = term->get_num_args(); + for (unsigned i = 0; i < num_args; i++) { + set_up_axioms(term->get_arg(i)); + } + } +} + void theory_str::init_search_eh() { ast_manager & m = get_manager(); context & ctx = get_context(); - TRACE("t_str", - tout << "search started, assignments are:" << std::endl; - expr_ref_vector assignment(m); - ctx.get_assignments(assignment); - for (expr_ref_vector::iterator i = assignment.begin(); i != assignment.end(); ++i) { - expr * ex = *i; + + TRACE("t_str_detail", + tout << "dumping all asserted formulas:" << std::endl; + unsigned nFormulas = ctx.get_num_asserted_formulas(); + for (unsigned i = 0; i < nFormulas; ++i) { + expr * ex = ctx.get_asserted_formula(i); tout << mk_ismt2_pp(ex, m) << std::endl; } ); + // recursive descent through all asserted formulas to set up axioms + unsigned nFormulas = ctx.get_num_asserted_formulas(); + for (unsigned i = 0; i < nFormulas; ++i) { + expr * ex = ctx.get_asserted_formula(i); + set_up_axioms(ex); + } + search_started = true; } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index a336ec649..23abc3c9d 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -47,6 +47,8 @@ namespace smt { bool is_concat(enode const * n) const { return is_concat(n->get_owner()); } void instantiate_concat_axiom(enode * cat); void instantiate_basic_string_axioms(enode * str); + + void set_up_axioms(expr * ex); public: theory_str(ast_manager & m); virtual ~theory_str(); From 91e9cf272a58dd87f8f523c1f26320953479584a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 27 Sep 2015 00:12:04 -0400 Subject: [PATCH 017/562] assert string axiom 2 --- src/smt/theory_str.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 63378a700..ee230d027 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -224,7 +224,6 @@ void theory_str::instantiate_basic_string_axioms(enode * str) { assert_axiom(lhs_ge_rhs); } - /* // build axiom 2: Length(a_str) == 0 <=> a_str == "" { // build LHS of iff @@ -246,12 +245,11 @@ void theory_str::instantiate_basic_string_axioms(enode * str) { SASSERT(rhs); // build LHS <=> RHS and assert TRACE("t_str_detail", tout << "string axiom 2: " << mk_bounded_pp(lhs, m) << " <=> " << mk_bounded_pp(rhs, m) << std::endl;); - // TODO this is kind of a hack, maybe just ctx.assert_expr() will be enough? literal l(mk_eq(lhs, rhs, true)); ctx.mark_as_relevant(l); - assert_axiom(l); + ctx.mk_th_axiom(get_id(), 1, &l); } - */ + } } From 114b51dec89319412031c18c71e43f6b63ccc8f2 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 27 Sep 2015 17:26:52 -0400 Subject: [PATCH 018/562] only handle equalities in assignments during init_search_eh --- src/smt/theory_str.cpp | 34 +++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 5 ++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ee230d027..ab0324a57 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -266,6 +266,10 @@ void theory_str::reset_eh() { pop_scope_eh(0); } +void theory_str::handle_equality(expr * lhs, expr * rhs) { + +} + void theory_str::set_up_axioms(expr * ex) { // TODO check to make sure we don't set up axioms on the same term twice TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << std::endl;); @@ -308,13 +312,40 @@ void theory_str::init_search_eh() { tout << mk_ismt2_pp(ex, m) << std::endl; } ); - // recursive descent through all asserted formulas to set up axioms + /* + * Recursive descent through all asserted formulas to set up axioms. + * Note that this is just the input structure and not necessarily things + * that we know to be true or false. We're just doing this to see + * which terms are explicitly mentioned. + */ unsigned nFormulas = ctx.get_num_asserted_formulas(); for (unsigned i = 0; i < nFormulas; ++i) { expr * ex = ctx.get_asserted_formula(i); set_up_axioms(ex); } + /* + * Similar recursive descent, except over all initially assigned terms. + * This is done to find equalities between terms, etc. that we otherwise + * wouldn't get a chance to see. + */ + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { + expr * ex = *i; + TRACE("t_str_detail", tout << "processing assignment " << mk_ismt2_pp(ex, m) << std::endl;); + if (m.is_eq(ex)) { + TRACE("t_str_detail", tout << "expr is equality" << std::endl;); + app * eq = (app*)ex; + SASSERT(eq->get_num_args() == 2); + expr * lhs = eq->get_arg(0); + expr * rhs = eq->get_arg(1); + handle_equality(lhs, rhs); + } else { + TRACE("t_str_detail", tout << "expr ignored" << std::endl;); + } + } + search_started = true; } @@ -323,6 +354,7 @@ void theory_str::new_eq_eh(theory_var x, theory_var y) { TRACE("t_str", tout << "new eq: v#" << x << " = v#" << y << std::endl;); TRACE("t_str_detail", tout << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " = " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); + handle_equality(get_enode(x)->get_owner(), get_enode(y)->get_owner()); } void theory_str::new_diseq_eh(theory_var x, theory_var y) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 23abc3c9d..f58ddea91 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -49,6 +49,7 @@ namespace smt { void instantiate_basic_string_axioms(enode * str); void set_up_axioms(expr * ex); + void handle_equality(expr * lhs, expr * rhs); public: theory_str(ast_manager & m); virtual ~theory_str(); @@ -58,14 +59,12 @@ namespace smt { virtual void new_eq_eh(theory_var, theory_var); virtual void new_diseq_eh(theory_var, theory_var); + virtual theory* mk_fresh(context*) { return alloc(theory_str, get_manager()); } - virtual void init_search_eh(); - virtual void relevant_eh(app * n); virtual void assign_eh(bool_var v, bool is_true); virtual void push_scope_eh(); - virtual void reset_eh(); virtual bool can_propagate(); From 6481fe941ae853e87b462edc0e84b95287c5d37d Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 27 Sep 2015 17:48:53 -0400 Subject: [PATCH 019/562] instantiate string-eq length-eq axiom --- src/smt/theory_str.cpp | 66 +++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 2 ++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ab0324a57..3b9054132 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -118,7 +118,7 @@ app * theory_str::mk_strlen(app * e) { } bool theory_str::can_propagate() { - return !m_basicstr_axiom_todo.empty(); + return !m_basicstr_axiom_todo.empty() || !m_str_eq_length_axiom_todo.empty(); } void theory_str::propagate() { @@ -128,6 +128,14 @@ void theory_str::propagate() { instantiate_basic_string_axioms(m_basicstr_axiom_todo[i]); } m_basicstr_axiom_todo.reset(); + + for (unsigned i = 0; i < m_str_eq_length_axiom_todo.size(); ++i) { + std::pair pair = m_str_eq_length_axiom_todo[i]; + enode * lhs = pair.first; + enode * rhs = pair.second; + instantiate_str_eq_length_axiom(lhs, rhs); + } + m_str_eq_length_axiom_todo.reset(); } TRACE("t_str_detail", tout << "done propagating" << std::endl;); } @@ -253,6 +261,33 @@ void theory_str::instantiate_basic_string_axioms(enode * str) { } } +/* + * Add an axiom of the form: + * (lhs == rhs) -> ( Length(lhs) == Length(rhs) ) + */ +void theory_str::instantiate_str_eq_length_axiom(enode * lhs, enode * rhs) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * a_lhs = lhs->get_owner(); + app * a_rhs = rhs->get_owner(); + + // build premise: (lhs == rhs) + expr_ref premise(ctx.mk_eq_atom(a_lhs, a_rhs), m); + + // build conclusion: ( Length(lhs) == Length(rhs) ) + expr_ref len_lhs(mk_strlen(a_lhs), m); + SASSERT(len_lhs); + expr_ref len_rhs(mk_strlen(a_rhs), m); + SASSERT(len_rhs); + expr_ref conclusion(ctx.mk_eq_atom(len_lhs, len_rhs), m); + + // build (premise -> conclusion) and assert + expr_ref axiom(m.mk_implies(premise, conclusion), m); + TRACE("t_str_detail", tout << "string-eq length-eq axiom: " << mk_bounded_pp(axiom, m) << std::endl;); + assert_axiom(axiom); +} + void theory_str::attach_new_th_var(enode * n) { context & ctx = get_context(); theory_var v = mk_var(n); @@ -263,11 +298,40 @@ void theory_str::attach_new_th_var(enode * n) { void theory_str::reset_eh() { TRACE("t_str", tout << "resetting" << std::endl;); m_basicstr_axiom_todo.reset(); + m_str_eq_length_axiom_todo.reset(); pop_scope_eh(0); } void theory_str::handle_equality(expr * lhs, expr * rhs) { + ast_manager & m = get_manager(); + context & ctx = get_context(); + // both terms must be of sort String + sort * lhs_sort = m.get_sort(lhs); + sort * rhs_sort = m.get_sort(rhs); + sort * str_sort = m.mk_sort(get_family_id(), STRING_SORT); + if (lhs_sort != str_sort || rhs_sort != str_sort) { + TRACE("t_str_detail", tout << "skip equality: not String sort" << std::endl;); + return; + } + + // TODO freeVarAttempt()? + + // TODO simplify concat? + + // TODO newEqCheck()? + + // BEGIN new_eq_handler() in strTheory + + // TODO there's some setup with getLenValue() that I don't think is necessary + // because we should already be generating the string length axioms for all string terms + + // set up string length axiom: + // (lhs == rhs) -> (Length(lhs) == Length(rhs)) + enode * e_lhs = ctx.get_enode(lhs); + enode * e_rhs = ctx.get_enode(rhs); + std::pair eq_pair(e_lhs, e_rhs); + m_str_eq_length_axiom_todo.push_back(eq_pair); } void theory_str::set_up_axioms(expr * ex) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index f58ddea91..b9c11c2f0 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -38,6 +38,7 @@ namespace smt { str_util m_strutil; ptr_vector m_basicstr_axiom_todo; + svector > m_str_eq_length_axiom_todo; protected: void assert_axiom(ast * e); @@ -47,6 +48,7 @@ namespace smt { bool is_concat(enode const * n) const { return is_concat(n->get_owner()); } void instantiate_concat_axiom(enode * cat); void instantiate_basic_string_axioms(enode * str); + void instantiate_str_eq_length_axiom(enode * lhs, enode * rhs); void set_up_axioms(expr * ex); void handle_equality(expr * lhs, expr * rhs); From 86e60877187049d3a8800fa27fda8c07a565e4c7 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 27 Sep 2015 21:30:45 -0400 Subject: [PATCH 020/562] starting solve_concat_eq_str(); currently there is an unsoundness bug --- src/smt/theory_str.cpp | 206 ++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 9 ++ 2 files changed, 212 insertions(+), 3 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 3b9054132..faaa7fb70 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -144,6 +144,7 @@ void theory_str::propagate() { * Instantiate an axiom of the following form: * Length(Concat(x, y)) = Length(x) + Length(y) */ +// TODO this isn't used yet void theory_str::instantiate_concat_axiom(enode * cat) { SASSERT(is_concat(cat)); app * a_cat = cat->get_owner(); @@ -302,6 +303,99 @@ void theory_str::reset_eh() { pop_scope_eh(0); } +/* + * Check equality among equivalence class members of LHS and RHS + * to discover an incorrect LHS == RHS. + * For example, if we have y2 == "str3" + * and the equivalence classes are + * { y2, (Concat ce m2) } + * { "str3", (Concat abc x2) } + * then y2 can't be equal to "str3". + * Then add an assertion: (y2 == (Concat ce m2)) AND ("str3" == (Concat abc x2)) -> (y2 != "str3") + */ +bool theory_str::new_eq_check(expr * lhs, expr * rhs) { + // TODO this involves messing around with enodes and equivalence classes + return true; +} + +void theory_str::group_terms_by_eqc(expr * n, std::set & concats, std::set & vars, std::set & consts) { + ast_manager & m = get_manager(); + context & ctx = get_context(); + enode * nNode = ctx.get_enode(n); + enode * eqcNode = nNode; + do { + app * ast = eqcNode->get_owner(); + if (is_concat(eqcNode)) { + // TODO simplify_concat + /* + Z3_ast simConcat = simplifyConcat(t, eqcNode); + if (simConcat != eqcNode) { + if (isConcatFunc(t, simConcat)) { + concats.insert(simConcat); + } else { + if (isConstStr(t, simConcat)) { + constStrs.insert(simConcat); + } else { + vars.insert(simConcat); + } + } + } else { + concats.insert(simConcat); + } + */ + concats.insert(ast); + } else if (is_string(eqcNode)) { + consts.insert(ast); + } else { + vars.insert(ast); + } + eqcNode = eqcNode->get_next(); + } while (eqcNode != nNode); +} + +void theory_str::simplify_concat_equality(expr * lhs, expr * rhs) { + // TODO strArgmt::simplifyConcatEq() +} + +/* + * strArgmt::solve_concat_eq_str() + * Solve concatenations of the form: + * const == Concat(const, X) + * const == Concat(X, const) + */ +void theory_str::solve_concat_eq_str(expr * concat, expr * str) { + ast_manager & m = get_manager(); + context & ctx = get_context(); + + TRACE("t_str_detail", tout << mk_ismt2_pp(concat, m) << " == " << mk_ismt2_pp(str, m) << std::endl;); + + if (is_concat(to_app(concat)) && is_string(to_app(str))) { + const char * tmp = 0; + m_strutil.is_string(str, & tmp); + std::string const_str(tmp); + app * a_concat = to_app(concat); + SASSERT(a_concat->get_num_args() == 2); + expr * a1 = a_concat->get_arg(0); + expr * a2 = a_concat->get_arg(1); + + if (const_str == "") { + TRACE("t_str", tout << "quick path: concat == \"\"" << std::endl;); + // assert the following axiom: + // ( (Concat a1 a2) == str ) -> ( (a1 == "") AND (a2 == "") ) + expr_ref premise(ctx.mk_eq_atom(concat, str), m); + expr_ref empty_str(m_strutil.mk_string(""), m); + expr_ref c1(ctx.mk_eq_atom(a1, empty_str), m); + expr_ref c2(ctx.mk_eq_atom(a2, empty_str), m); + expr_ref conclusion(m.mk_and(c1, c2), m); + expr_ref axiom(m.mk_implies(premise, conclusion), m); + TRACE("t_str_detail", tout << "learn " << mk_ismt2_pp(axiom, m) << std::endl;); + assert_axiom(axiom); + return; + } + // TODO the rest... + } +} + void theory_str::handle_equality(expr * lhs, expr * rhs) { ast_manager & m = get_manager(); context & ctx = get_context(); @@ -319,7 +413,10 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { // TODO simplify concat? - // TODO newEqCheck()? + // newEqCheck() -- check consistency wrt. existing equivalence classes + if (!new_eq_check(lhs, rhs)) { + return; + } // BEGIN new_eq_handler() in strTheory @@ -332,6 +429,94 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { enode * e_rhs = ctx.get_enode(rhs); std::pair eq_pair(e_lhs, e_rhs); m_str_eq_length_axiom_todo.push_back(eq_pair); + + // group terms by equivalence class (groupNodeInEqc()) + std::set eqc_lhs_concat; + std::set eqc_lhs_var; + std::set eqc_lhs_const; + group_terms_by_eqc(lhs, eqc_lhs_concat, eqc_lhs_var, eqc_lhs_const); + + TRACE("t_str_detail", + tout << "eqc[lhs]:" << std::endl; + tout << "Concats:" << std::endl; + for (std::set::iterator it = eqc_lhs_concat.begin(); it != eqc_lhs_concat.end(); ++it) { + expr * ex = *it; + tout << mk_ismt2_pp(ex, get_manager()) << std::endl; + } + tout << "Variables:" << std::endl; + for (std::set::iterator it = eqc_lhs_var.begin(); it != eqc_lhs_var.end(); ++it) { + expr * ex = *it; + tout << mk_ismt2_pp(ex, get_manager()) << std::endl; + } + tout << "Constants:" << std::endl; + for (std::set::iterator it = eqc_lhs_const.begin(); it != eqc_lhs_const.end(); ++it) { + expr * ex = *it; + tout << mk_ismt2_pp(ex, get_manager()) << std::endl; + } + ); + + std::set eqc_rhs_concat; + std::set eqc_rhs_var; + std::set eqc_rhs_const; + group_terms_by_eqc(rhs, eqc_rhs_concat, eqc_rhs_var, eqc_rhs_const); + + TRACE("t_str_detail", + tout << "eqc[rhs]:" << std::endl; + tout << "Concats:" << std::endl; + for (std::set::iterator it = eqc_rhs_concat.begin(); it != eqc_rhs_concat.end(); ++it) { + expr * ex = *it; + tout << mk_ismt2_pp(ex, get_manager()) << std::endl; + } + tout << "Variables:" << std::endl; + for (std::set::iterator it = eqc_rhs_var.begin(); it != eqc_rhs_var.end(); ++it) { + expr * ex = *it; + tout << mk_ismt2_pp(ex, get_manager()) << std::endl; + } + tout << "Constants:" << std::endl; + for (std::set::iterator it = eqc_rhs_const.begin(); it != eqc_rhs_const.end(); ++it) { + expr * ex = *it; + tout << mk_ismt2_pp(ex, get_manager()) << std::endl; + } + ); + + // step 1: Concat == Concat + bool hasCommon = false; + if (eqc_lhs_concat.size() != 0 && eqc_rhs_concat.size() != 0) { + std::set::iterator itor1 = eqc_lhs_concat.begin(); + std::set::iterator itor2 = eqc_rhs_concat.begin(); + for (; itor1 != eqc_lhs_concat.end(); ++itor1) { + if (eqc_rhs_concat.find(*itor1) != eqc_rhs_concat.end()) { + hasCommon = true; + break; + } + } + for (; !hasCommon && itor2 != eqc_rhs_concat.end(); ++itor2) { + if (eqc_lhs_concat.find(*itor2) != eqc_lhs_concat.end()) { + hasCommon = true; + break; + } + } + if (!hasCommon) { + simplify_concat_equality(*(eqc_lhs_concat.begin()), *(eqc_rhs_concat.begin())); + } + } + + // step 2: Concat == Constant + if (eqc_lhs_const.size() != 0) { + expr * conStr = *(eqc_lhs_const.begin()); + std::set::iterator itor2 = eqc_rhs_concat.begin(); + for (; itor2 != eqc_rhs_concat.end(); ++itor2) { + solve_concat_eq_str(*itor2, conStr); + } + } else if (eqc_rhs_const.size() != 0) { + expr * conStr = *(eqc_rhs_const.begin()); + std::set::iterator itor1 = eqc_lhs_concat.begin(); + for (; itor1 != eqc_lhs_concat.end(); ++itor1) { + solve_concat_eq_str(*itor1, conStr); + } + } + + // TODO regex unroll? (much later) } void theory_str::set_up_axioms(expr * ex) { @@ -368,6 +553,9 @@ void theory_str::init_search_eh() { ast_manager & m = get_manager(); context & ctx = get_context(); + // TODO it would be better to refactor this function so that instead of deferring the axioms + // instead we defer the evaluation of the expression + TRACE("t_str_detail", tout << "dumping all asserted formulas:" << std::endl; unsigned nFormulas = ctx.get_num_asserted_formulas(); @@ -410,11 +598,11 @@ void theory_str::init_search_eh() { } } + TRACE("t_str", tout << "search started" << std::endl;); search_started = true; } void theory_str::new_eq_eh(theory_var x, theory_var y) { - // TODO TRACE("t_str", tout << "new eq: v#" << x << " = v#" << y << std::endl;); TRACE("t_str_detail", tout << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " = " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); @@ -422,7 +610,6 @@ void theory_str::new_eq_eh(theory_var x, theory_var y) { } void theory_str::new_diseq_eh(theory_var x, theory_var y) { - // TODO TRACE("t_str", tout << "new diseq: v#" << x << " != v#" << y << std::endl;); TRACE("t_str_detail", tout << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " != " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); @@ -442,8 +629,21 @@ void theory_str::push_scope_eh() { } final_check_status theory_str::final_check_eh() { + ast_manager & m = get_manager(); + context & ctx = get_context(); // TODO TRACE("t_str", tout << "final check" << std::endl;); + + TRACE("t_str_detail", + tout << "dumping all assignments:" << std::endl; + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { + expr * ex = *i; + tout << mk_ismt2_pp(ex, m) << std::endl; + } + ); + return FC_DONE; } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index b9c11c2f0..286de818a 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -23,6 +23,7 @@ Revision History: #include"value_factory.h" #include"smt_model_generator.h" #include"arith_decl_plugin.h" +#include namespace smt { @@ -46,12 +47,20 @@ namespace smt { bool is_concat(app const * a) const { return a->is_app_of(get_id(), OP_STRCAT); } bool is_concat(enode const * n) const { return is_concat(n->get_owner()); } + bool is_string(app const * a) const { return a->is_app_of(get_id(), OP_STR); } + bool is_string(enode const * n) const { return is_string(n->get_owner()); } void instantiate_concat_axiom(enode * cat); void instantiate_basic_string_axioms(enode * str); void instantiate_str_eq_length_axiom(enode * lhs, enode * rhs); void set_up_axioms(expr * ex); void handle_equality(expr * lhs, expr * rhs); + + void simplify_concat_equality(expr * lhs, expr * rhs); + void solve_concat_eq_str(expr * concat, expr * str); + + bool new_eq_check(expr * lhs, expr * rhs); + void group_terms_by_eqc(expr * n, std::set & concats, std::set & vars, std::set & consts); public: theory_str(ast_manager & m); virtual ~theory_str(); From 02cb329ca5b980c02b825bb54dc163149b3f75dc Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 27 Sep 2015 23:24:41 -0400 Subject: [PATCH 021/562] defer equalities uncovered during init_search --- src/smt/theory_str.cpp | 34 +++++++++++++++------------------- src/smt/theory_str.h | 2 +- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index faaa7fb70..bba37f5f0 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -118,7 +118,7 @@ app * theory_str::mk_strlen(app * e) { } bool theory_str::can_propagate() { - return !m_basicstr_axiom_todo.empty() || !m_str_eq_length_axiom_todo.empty(); + return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty(); } void theory_str::propagate() { @@ -129,13 +129,13 @@ void theory_str::propagate() { } m_basicstr_axiom_todo.reset(); - for (unsigned i = 0; i < m_str_eq_length_axiom_todo.size(); ++i) { - std::pair pair = m_str_eq_length_axiom_todo[i]; + for (unsigned i = 0; i < m_str_eq_todo.size(); ++i) { + std::pair pair = m_str_eq_todo[i]; enode * lhs = pair.first; enode * rhs = pair.second; - instantiate_str_eq_length_axiom(lhs, rhs); + handle_equality(lhs->get_owner(), rhs->get_owner()); } - m_str_eq_length_axiom_todo.reset(); + m_str_eq_todo.reset(); } TRACE("t_str_detail", tout << "done propagating" << std::endl;); } @@ -299,7 +299,7 @@ void theory_str::attach_new_th_var(enode * n) { void theory_str::reset_eh() { TRACE("t_str", tout << "resetting" << std::endl;); m_basicstr_axiom_todo.reset(); - m_str_eq_length_axiom_todo.reset(); + m_str_eq_todo.reset(); pop_scope_eh(0); } @@ -382,10 +382,10 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { TRACE("t_str", tout << "quick path: concat == \"\"" << std::endl;); // assert the following axiom: // ( (Concat a1 a2) == str ) -> ( (a1 == "") AND (a2 == "") ) - expr_ref premise(ctx.mk_eq_atom(concat, str), m); + expr_ref premise(m.mk_eq(concat, str), m); expr_ref empty_str(m_strutil.mk_string(""), m); - expr_ref c1(ctx.mk_eq_atom(a1, empty_str), m); - expr_ref c2(ctx.mk_eq_atom(a2, empty_str), m); + expr_ref c1(m.mk_eq(a1, empty_str), m); + expr_ref c2(m.mk_eq(a2, empty_str), m); expr_ref conclusion(m.mk_and(c1, c2), m); expr_ref axiom(m.mk_implies(premise, conclusion), m); TRACE("t_str_detail", tout << "learn " << mk_ismt2_pp(axiom, m) << std::endl;); @@ -423,12 +423,7 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { // TODO there's some setup with getLenValue() that I don't think is necessary // because we should already be generating the string length axioms for all string terms - // set up string length axiom: - // (lhs == rhs) -> (Length(lhs) == Length(rhs)) - enode * e_lhs = ctx.get_enode(lhs); - enode * e_rhs = ctx.get_enode(rhs); - std::pair eq_pair(e_lhs, e_rhs); - m_str_eq_length_axiom_todo.push_back(eq_pair); + instantiate_str_eq_length_axiom(ctx.get_enode(lhs), ctx.get_enode(rhs)); // group terms by equivalence class (groupNodeInEqc()) std::set eqc_lhs_concat; @@ -553,9 +548,6 @@ void theory_str::init_search_eh() { ast_manager & m = get_manager(); context & ctx = get_context(); - // TODO it would be better to refactor this function so that instead of deferring the axioms - // instead we defer the evaluation of the expression - TRACE("t_str_detail", tout << "dumping all asserted formulas:" << std::endl; unsigned nFormulas = ctx.get_num_asserted_formulas(); @@ -592,7 +584,11 @@ void theory_str::init_search_eh() { SASSERT(eq->get_num_args() == 2); expr * lhs = eq->get_arg(0); expr * rhs = eq->get_arg(1); - handle_equality(lhs, rhs); + + enode * e_lhs = ctx.get_enode(lhs); + enode * e_rhs = ctx.get_enode(rhs); + std::pair eq_pair(e_lhs, e_rhs); + m_str_eq_todo.push_back(eq_pair); } else { TRACE("t_str_detail", tout << "expr ignored" << std::endl;); } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 286de818a..76bef4561 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -39,7 +39,7 @@ namespace smt { str_util m_strutil; ptr_vector m_basicstr_axiom_todo; - svector > m_str_eq_length_axiom_todo; + svector > m_str_eq_todo; protected: void assert_axiom(ast * e); From 0d54e4e4ae99933b4330fba9df9224f9486ee361 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 27 Sep 2015 23:57:41 -0400 Subject: [PATCH 022/562] implement str_decl_plugin::is_value() and ::is_unique_value() we can now prove that (= "abc" "def") is unsatisfiable --- src/ast/str_decl_plugin.cpp | 12 ++++++++++++ src/ast/str_decl_plugin.h | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 1502e3d3a..60db88b63 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -115,6 +115,18 @@ void str_decl_plugin::get_sort_names(svector & sort_names, symbol sort_names.push_back(builtin_name("String", STRING_SORT)); } +bool str_decl_plugin::is_value(app * e) const { + if (e->get_family_id() != m_family_id) { + return false; + } + switch (e->get_decl_kind()) { + case OP_STR: + return true; + default: + return false; + } +} + bool str_recognizers::is_string(expr const * n, const char ** val) const { if (!is_app_of(n, m_afid, OP_STR)) return false; diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 7e75fbaf0..a64e0c05f 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -60,8 +60,10 @@ public: app * mk_string(const char * val); virtual void get_op_names(svector & op_names, symbol const & logic); - virtual void get_sort_names(svector & sort_names, symbol const & logic); + + virtual bool is_value(app * e) const; + virtual bool is_unique_value(app * e) const { return is_value(e); } // TODO }; From 7da3854a8b488188641b4b8c2d691e5d59df1df8 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 28 Sep 2015 01:56:13 -0400 Subject: [PATCH 023/562] really lousy model-building, WIP --- src/smt/theory_str.cpp | 46 ++++++++++++++++++++++++++++-------------- src/smt/theory_str.h | 23 ++++++++++++++++++++- 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index bba37f5f0..aaae3e373 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -35,18 +35,9 @@ theory_str::~theory_str() { } void theory_str::assert_axiom(ast * a) { - /* - if (search_started) { - // effectively Z3_theory_assert_axiom - NOT_IMPLEMENTED_YET(); - } else { - // effectively Z3_assert_cnstr - context & ctx = get_context(); - ctx.assert_expr(to_expr(a)); - } - */ - TRACE("t_str_detail", tout << "asserting " << mk_ismt2_pp(a, get_manager()) << "\n";); expr * e = to_expr(a); + if (get_manager().is_true(e)) return; + TRACE("t_str_detail", tout << "asserting " << mk_ismt2_pp(a, get_manager()) << "\n";); context & ctx = get_context(); ctx.internalize(e, false); literal lit(ctx.get_literal(e)); @@ -381,11 +372,11 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { if (const_str == "") { TRACE("t_str", tout << "quick path: concat == \"\"" << std::endl;); // assert the following axiom: - // ( (Concat a1 a2) == str ) -> ( (a1 == "") AND (a2 == "") ) - expr_ref premise(m.mk_eq(concat, str), m); + // ( (Concat a1 a2) == "" ) -> ( (a1 == "") AND (a2 == "") ) expr_ref empty_str(m_strutil.mk_string(""), m); - expr_ref c1(m.mk_eq(a1, empty_str), m); - expr_ref c2(m.mk_eq(a2, empty_str), m); + expr_ref premise(ctx.mk_eq_atom(concat, empty_str), m); + expr_ref c1(ctx.mk_eq_atom(a1, empty_str), m); + expr_ref c2(ctx.mk_eq_atom(a2, empty_str), m); expr_ref conclusion(m.mk_and(c1, c2), m); expr_ref axiom(m.mk_implies(premise, conclusion), m); TRACE("t_str_detail", tout << "learn " << mk_ismt2_pp(axiom, m) << std::endl;); @@ -643,4 +634,29 @@ final_check_status theory_str::final_check_eh() { return FC_DONE; } +void theory_str::init_model(model_generator & mg) { + TRACE("t_str", tout << "initializing model" << std::endl; display(tout);); + m_factory = alloc(str_value_factory, get_manager(), get_family_id()); + mg.register_factory(m_factory); +} + +model_value_proc * theory_str::mk_value(enode * n, model_generator & mg) { + TRACE("t_str", tout << "mk_value for: " << mk_ismt2_pp(n->get_owner(), get_manager()) << + " (sort " << mk_ismt2_pp(get_manager().get_sort(n->get_owner()), get_manager()) << ")\n";); + ast_manager & m = get_manager(); + context & ctx = get_context(); + app_ref owner(m); + owner = n->get_owner(); + + // If the owner is not internalized, it doesn't have an enode associated. + SASSERT(ctx.e_internalized(owner)); + + if (m_strutil.is_string(owner)) { + return alloc(expr_wrapper_proc, owner); + } + NOT_IMPLEMENTED_YET(); // TODO +} + +void theory_str::finalize_model(model_generator & mg) {} + }; /* namespace smt */ diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 76bef4561..65a401580 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -28,7 +28,22 @@ Revision History: namespace smt { class str_value_factory : public value_factory { - // TODO + str_util m_util; + public: + str_value_factory(ast_manager & m, family_id fid) : + value_factory(m, fid), + m_util(m) {} + virtual ~str_value_factory() {} + virtual expr * get_some_value(sort * s) { + return m_util.mk_string("some value"); + } + virtual bool get_some_values(sort * s, expr_ref & v1, expr_ref & v2) { + v1 = m_util.mk_string("value 1"); + v2 = m_util.mk_string("value 2"); + return true; + } + virtual expr * get_fresh_value(sort * s) { NOT_IMPLEMENTED_YET(); } + virtual void register_value(expr * n) { /* Ignore */ } }; class theory_str : public theory { @@ -38,6 +53,8 @@ namespace smt { arith_util m_autil; str_util m_strutil; + str_value_factory * m_factory; + ptr_vector m_basicstr_axiom_todo; svector > m_str_eq_todo; protected: @@ -83,6 +100,10 @@ namespace smt { virtual final_check_status final_check_eh(); void attach_new_th_var(enode * n); + + virtual void init_model(model_generator & m); + virtual model_value_proc * mk_value(enode * n, model_generator & mg); + virtual void finalize_model(model_generator & mg); }; }; From 87b5765e3d3ef04aface95303666ed3f4daf7026 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 28 Sep 2015 02:04:35 -0400 Subject: [PATCH 024/562] clean up traces and make them much easier to read --- src/smt/theory_str.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index aaae3e373..467d94c5f 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -128,7 +128,6 @@ void theory_str::propagate() { } m_str_eq_todo.reset(); } - TRACE("t_str_detail", tout << "done propagating" << std::endl;); } /* @@ -507,8 +506,6 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { void theory_str::set_up_axioms(expr * ex) { // TODO check to make sure we don't set up axioms on the same term twice - TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << std::endl;); - ast_manager & m = get_manager(); context & ctx = get_context(); @@ -516,13 +513,15 @@ void theory_str::set_up_axioms(expr * ex) { sort * str_sort = m.mk_sort(get_family_id(), STRING_SORT); if (ex_sort == str_sort) { - TRACE("t_str_detail", tout << "expr is of sort String" << std::endl;); + TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << + ": expr is of sort String" << std::endl;); // set up basic string axioms enode * n = ctx.get_enode(ex); SASSERT(n); m_basicstr_axiom_todo.push_back(n); } else { - TRACE("t_str_detail", tout << "expr is of wrong sort, ignoring" << std::endl;); + TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << + ": expr is of wrong sort, ignoring" << std::endl;); } // if expr is an application, recursively inspect all arguments @@ -568,9 +567,9 @@ void theory_str::init_search_eh() { ctx.get_assignments(assignments); for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { expr * ex = *i; - TRACE("t_str_detail", tout << "processing assignment " << mk_ismt2_pp(ex, m) << std::endl;); if (m.is_eq(ex)) { - TRACE("t_str_detail", tout << "expr is equality" << std::endl;); + TRACE("t_str_detail", tout << "processing assignment " << mk_ismt2_pp(ex, m) << + ": expr is equality" << std::endl;); app * eq = (app*)ex; SASSERT(eq->get_num_args() == 2); expr * lhs = eq->get_arg(0); @@ -581,7 +580,8 @@ void theory_str::init_search_eh() { std::pair eq_pair(e_lhs, e_rhs); m_str_eq_todo.push_back(eq_pair); } else { - TRACE("t_str_detail", tout << "expr ignored" << std::endl;); + TRACE("t_str_detail", tout << "processing assignment " << mk_ismt2_pp(ex, m) + << ": expr ignored" << std::endl;); } } @@ -590,15 +590,15 @@ void theory_str::init_search_eh() { } void theory_str::new_eq_eh(theory_var x, theory_var y) { - TRACE("t_str", tout << "new eq: v#" << x << " = v#" << y << std::endl;); - TRACE("t_str_detail", tout << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " = " << + //TRACE("t_str_detail", tout << "new eq: v#" << x << " = v#" << y << std::endl;); + TRACE("t_str", tout << "new eq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " = " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); handle_equality(get_enode(x)->get_owner(), get_enode(y)->get_owner()); } void theory_str::new_diseq_eh(theory_var x, theory_var y) { - TRACE("t_str", tout << "new diseq: v#" << x << " != v#" << y << std::endl;); - TRACE("t_str_detail", tout << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " != " << + //TRACE("t_str_detail", tout << "new diseq: v#" << x << " != v#" << y << std::endl;); + TRACE("t_str", tout << "new diseq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " != " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); } From 5fe129b5716aabca5313adfb0f5e77f3b3ea3fc9 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 28 Sep 2015 02:09:35 -0400 Subject: [PATCH 025/562] use mk_ismt2_pp() instead of mk_bounded_pp() --- src/smt/theory_str.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 467d94c5f..b3780265d 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -171,7 +171,7 @@ void theory_str::instantiate_concat_axiom(enode * cat) { // finally assert equality between the two subexpressions app * eq = m.mk_eq(len_xy, len_x_plus_len_y); SASSERT(eq); - TRACE("t_str", tout << mk_bounded_pp(eq, m) << std::endl;); + TRACE("t_str", tout << mk_ismt2_pp(eq, m) << std::endl;); assert_axiom(eq); } @@ -219,7 +219,7 @@ void theory_str::instantiate_basic_string_axioms(enode * str) { app * lhs_ge_rhs = m_autil.mk_ge(len_str, zero); SASSERT(lhs_ge_rhs); // TODO verify that this works - TRACE("t_str_detail", tout << "string axiom 1: " << mk_bounded_pp(lhs_ge_rhs, m) << std::endl;); + TRACE("t_str_detail", tout << "string axiom 1: " << mk_ismt2_pp(lhs_ge_rhs, m) << std::endl;); assert_axiom(lhs_ge_rhs); } @@ -243,7 +243,7 @@ void theory_str::instantiate_basic_string_axioms(enode * str) { rhs = ctx.mk_eq_atom(a_str, empty_str); SASSERT(rhs); // build LHS <=> RHS and assert - TRACE("t_str_detail", tout << "string axiom 2: " << mk_bounded_pp(lhs, m) << " <=> " << mk_bounded_pp(rhs, m) << std::endl;); + TRACE("t_str_detail", tout << "string axiom 2: " << mk_ismt2_pp(lhs, m) << " <=> " << mk_ismt2_pp(rhs, m) << std::endl;); literal l(mk_eq(lhs, rhs, true)); ctx.mark_as_relevant(l); ctx.mk_th_axiom(get_id(), 1, &l); @@ -275,7 +275,7 @@ void theory_str::instantiate_str_eq_length_axiom(enode * lhs, enode * rhs) { // build (premise -> conclusion) and assert expr_ref axiom(m.mk_implies(premise, conclusion), m); - TRACE("t_str_detail", tout << "string-eq length-eq axiom: " << mk_bounded_pp(axiom, m) << std::endl;); + TRACE("t_str_detail", tout << "string-eq length-eq axiom: " << mk_ismt2_pp(axiom, m) << std::endl;); assert_axiom(axiom); } From bccadedfee53f5d1cd097300044bfc39c1a5bdac Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 28 Sep 2015 03:20:13 -0400 Subject: [PATCH 026/562] instead of building axiom (=> x y), build (or (not x) y) this may be a bug in Z3 as it suggests that implications are ignored e.g. I can assert the axiom (=> true false) and Z3 is okay with this --- src/smt/theory_str.cpp | 23 +++++++++++++++-------- src/smt/theory_str.h | 1 + 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index b3780265d..6d2284d67 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -39,7 +39,9 @@ void theory_str::assert_axiom(ast * a) { if (get_manager().is_true(e)) return; TRACE("t_str_detail", tout << "asserting " << mk_ismt2_pp(a, get_manager()) << "\n";); context & ctx = get_context(); - ctx.internalize(e, false); + if (!ctx.b_internalized(e)) { + ctx.internalize(e, true); + } literal lit(ctx.get_literal(e)); ctx.mark_as_relevant(lit); ctx.mk_th_axiom(get_id(), 1, &lit); @@ -372,14 +374,15 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { TRACE("t_str", tout << "quick path: concat == \"\"" << std::endl;); // assert the following axiom: // ( (Concat a1 a2) == "" ) -> ( (a1 == "") AND (a2 == "") ) - expr_ref empty_str(m_strutil.mk_string(""), m); - expr_ref premise(ctx.mk_eq_atom(concat, empty_str), m); - expr_ref c1(ctx.mk_eq_atom(a1, empty_str), m); - expr_ref c2(ctx.mk_eq_atom(a2, empty_str), m); + + + expr_ref premise(ctx.mk_eq_atom(concat, str), m); + expr_ref c1(ctx.mk_eq_atom(a1, str), m); + expr_ref c2(ctx.mk_eq_atom(a2, str), m); expr_ref conclusion(m.mk_and(c1, c2), m); - expr_ref axiom(m.mk_implies(premise, conclusion), m); - TRACE("t_str_detail", tout << "learn " << mk_ismt2_pp(axiom, m) << std::endl;); + expr_ref axiom(m.mk_or(m.mk_not(premise), conclusion), m); assert_axiom(axiom); + return; } // TODO the rest... @@ -561,7 +564,7 @@ void theory_str::init_search_eh() { /* * Similar recursive descent, except over all initially assigned terms. * This is done to find equalities between terms, etc. that we otherwise - * wouldn't get a chance to see. + * might not get a chance to see. */ expr_ref_vector assignments(m); ctx.get_assignments(assignments); @@ -615,6 +618,10 @@ void theory_str::push_scope_eh() { TRACE("t_str", tout << "push" << std::endl;); } +void theory_str::pop_scope_eh(unsigned num_scopes) { + TRACE("t_str", tout << "pop " << num_scopes << std::endl;); +} + final_check_status theory_str::final_check_eh() { ast_manager & m = get_manager(); context & ctx = get_context(); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 65a401580..afac8b7f1 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -93,6 +93,7 @@ namespace smt { virtual void relevant_eh(app * n); virtual void assign_eh(bool_var v, bool is_true); virtual void push_scope_eh(); + virtual void pop_scope_eh(unsigned num_scopes); virtual void reset_eh(); virtual bool can_propagate(); From 62cd633b63b3452f913faaec383676900ec91052 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 28 Sep 2015 03:26:46 -0400 Subject: [PATCH 027/562] create helper function theory_str::assert_implication() --- src/smt/theory_str.cpp | 23 +++++++++++++---------- src/smt/theory_str.h | 3 ++- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 6d2284d67..3ef2f06e5 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -34,10 +34,9 @@ theory_str::theory_str(ast_manager & m): theory_str::~theory_str() { } -void theory_str::assert_axiom(ast * a) { - expr * e = to_expr(a); +void theory_str::assert_axiom(expr * e) { if (get_manager().is_true(e)) return; - TRACE("t_str_detail", tout << "asserting " << mk_ismt2_pp(a, get_manager()) << "\n";); + TRACE("t_str_detail", tout << "asserting " << mk_ismt2_pp(e, get_manager()) << "\n";); context & ctx = get_context(); if (!ctx.b_internalized(e)) { ctx.internalize(e, true); @@ -45,7 +44,13 @@ void theory_str::assert_axiom(ast * a) { literal lit(ctx.get_literal(e)); ctx.mark_as_relevant(lit); ctx.mk_th_axiom(get_id(), 1, &lit); - TRACE("t_str_detail", tout << "done asserting " << mk_ismt2_pp(a, get_manager()) << "\n";); + TRACE("t_str_detail", tout << "done asserting " << mk_ismt2_pp(e, get_manager()) << "\n";); +} + +void theory_str::assert_implication(expr * premise, expr * conclusion) { + ast_manager & m = get_manager(); + expr_ref axiom(m.mk_or(m.mk_not(premise), conclusion), m); + assert_axiom(axiom); } bool theory_str::internalize_atom(app * atom, bool gate_ctx) { @@ -275,10 +280,9 @@ void theory_str::instantiate_str_eq_length_axiom(enode * lhs, enode * rhs) { SASSERT(len_rhs); expr_ref conclusion(ctx.mk_eq_atom(len_lhs, len_rhs), m); - // build (premise -> conclusion) and assert - expr_ref axiom(m.mk_implies(premise, conclusion), m); - TRACE("t_str_detail", tout << "string-eq length-eq axiom: " << mk_ismt2_pp(axiom, m) << std::endl;); - assert_axiom(axiom); + TRACE("t_str_detail", tout << "string-eq length-eq axiom: " + << mk_ismt2_pp(premise, m) << " -> " << mk_ismt2_pp(conclusion, m) << std::endl;); + assert_implication(premise, conclusion); } void theory_str::attach_new_th_var(enode * n) { @@ -380,8 +384,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { expr_ref c1(ctx.mk_eq_atom(a1, str), m); expr_ref c2(ctx.mk_eq_atom(a2, str), m); expr_ref conclusion(m.mk_and(c1, c2), m); - expr_ref axiom(m.mk_or(m.mk_not(premise), conclusion), m); - assert_axiom(axiom); + assert_implication(premise, conclusion); return; } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index afac8b7f1..c7a4a5952 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -58,7 +58,8 @@ namespace smt { ptr_vector m_basicstr_axiom_todo; svector > m_str_eq_todo; protected: - void assert_axiom(ast * e); + void assert_axiom(expr * e); + void assert_implication(expr * premise, expr * conclusion); app * mk_strlen(app * e); From 9bc685b21d86ffd815300dd77a4a996a73be71de Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 28 Sep 2015 10:43:34 -0400 Subject: [PATCH 028/562] solve_concat_eq_str() for concat(const,const) == const --- src/smt/theory_str.cpp | 107 ++++++++++++++++++++++++++++++++++++++--- src/smt/theory_str.h | 3 ++ 2 files changed, 103 insertions(+), 7 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 3ef2f06e5..f3e6496b7 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -54,11 +54,9 @@ void theory_str::assert_implication(expr * premise, expr * conclusion) { } bool theory_str::internalize_atom(app * atom, bool gate_ctx) { - // TODO I have no idea if this is correct. TRACE("t_str", tout << "internalizing atom: " << mk_ismt2_pp(atom, get_manager()) << std::endl;); SASSERT(atom->get_family_id() == get_family_id()); - ast_manager & m = get_manager(); context & ctx = get_context(); if (ctx.b_internalized(atom)) @@ -75,8 +73,6 @@ bool theory_str::internalize_atom(app * atom, bool gate_ctx) { } bool theory_str::internalize_term(app * term) { - // TODO I have no idea if this is correct either. - ast_manager & m = get_manager(); context & ctx = get_context(); TRACE("t_str", tout << "internalizing term: " << mk_ismt2_pp(term, get_manager()) << std::endl;); SASSERT(term->get_family_id() == get_family_id()); @@ -115,6 +111,16 @@ app * theory_str::mk_strlen(app * e) { } } +app * theory_str::mk_concat(app * e1, app * e2) { + ast_manager & m = get_manager(); + if (e1 == NULL || e2 == NULL) { + m.raise_exception("strings to be concatenated cannot be NULL"); + } + // TODO there's a *TON* of missing code here from strTheory::mk_concat() + expr * args[2] = {e1, e2}; + return get_manager().mk_app(get_id(), OP_STRCAT, 0, 0, 2, args); +} + bool theory_str::can_propagate() { return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty(); } @@ -225,7 +231,6 @@ void theory_str::instantiate_basic_string_axioms(enode * str) { // build LHS >= RHS and assert app * lhs_ge_rhs = m_autil.mk_ge(len_str, zero); SASSERT(lhs_ge_rhs); - // TODO verify that this works TRACE("t_str_detail", tout << "string axiom 1: " << mk_ismt2_pp(lhs_ge_rhs, m) << std::endl;); assert_axiom(lhs_ge_rhs); } @@ -315,7 +320,6 @@ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { } void theory_str::group_terms_by_eqc(expr * n, std::set & concats, std::set & vars, std::set & consts) { - ast_manager & m = get_manager(); context & ctx = get_context(); enode * nNode = ctx.get_enode(n); enode * eqcNode = nNode; @@ -352,6 +356,27 @@ void theory_str::group_terms_by_eqc(expr * n, std::set & concats, std::se void theory_str::simplify_concat_equality(expr * lhs, expr * rhs) { // TODO strArgmt::simplifyConcatEq() } +/* + * Look through the equivalence class of n to find a string constant. + * Return that constant if it is found, and set hasEqcValue to true. + * Otherwise, return n, and set hasEqcValue to false. + */ +expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { + context & ctx = get_context(); + enode * nNode = ctx.get_enode(n); + enode * eqcNode = nNode; + do { + app * ast = eqcNode->get_owner(); + if (is_string(eqcNode)) { + hasEqcValue = true; + return ast; + } + eqcNode = eqcNode->get_next(); + } while (eqcNode != nNode); + // not found + hasEqcValue = false; + return n; +} /* * strArgmt::solve_concat_eq_str() @@ -388,7 +413,75 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { return; } - // TODO the rest... + bool arg1_has_eqc_value = false; + bool arg2_has_eqc_value = false; + expr * arg1 = get_eqc_value(a1, arg1_has_eqc_value); + expr * arg2 = get_eqc_value(a2, arg2_has_eqc_value); + expr_ref newConcat(m); + if (arg1 != a1 || arg2 != a2) { + TRACE("t_str", tout << "resolved concat argument(s) to eqc string constants" << std::endl;); + int iPos = 0; + app * item1[2]; + if (a1 != arg1) { + item1[iPos++] = ctx.mk_eq_atom(a1, arg1); + } + if (a2 != arg2) { + item1[iPos++] = ctx.mk_eq_atom(a2, arg2); + } + expr_ref implyL1(m); + if (iPos == 1) { + implyL1 = item1[0]; + } else { + implyL1 = m.mk_and(item1[0], item1[1]); + } + newConcat = mk_concat(to_app(arg1), to_app(arg2)); + if (newConcat != str) { + expr_ref implyR1(ctx.mk_eq_atom(concat, newConcat), m); + assert_implication(implyL1, implyR1); + } + } else { + newConcat = concat; + } + if (newConcat == str) { + return; + } + if (!is_concat(to_app(newConcat))) { + return; + } + if (arg1_has_eqc_value && arg2_has_eqc_value) { + // Case 1: Concat(const, const) == const + TRACE("t_str", tout << "Case 1: Concat(const, const) == const" << std::endl;); + const char * str1; + m_strutil.is_string(arg1, & str1); + std::string arg1_str(str1); + + const char * str2; + m_strutil.is_string(arg2, & str2); + std::string arg2_str(str2); + + std::string result_str = arg1_str + arg2_str; + if (result_str != const_str) { + // Inconsistency + TRACE("t_str", tout << "inconsistency detected: \"" + << arg1_str << "\" + \"" << arg2_str << + "\" != \"" << const_str << "\"" << std::endl;); + expr_ref equality(ctx.mk_eq_atom(concat, str), m); + expr_ref diseq(m.mk_not(equality), m); + assert_axiom(diseq); + } + } else if (!arg1_has_eqc_value && arg2_has_eqc_value) { + // Case 2: Concat(var, const) == const + TRACE("t_str", tout << "Case 2: Concat(var, const) == const" << std::endl;); + NOT_IMPLEMENTED_YET(); + } else if (arg1_has_eqc_value && !arg2_has_eqc_value) { + // Case 3: Concat(const, var) == const + TRACE("t_str", tout << "Case 3: Concat(const, var) == const" << std::endl;); + NOT_IMPLEMENTED_YET(); + } else { + // Case 4: Concat(var, var) == const + TRACE("t_str", tout << "Case 4: Concat(var, var) == const" << std::endl;); + NOT_IMPLEMENTED_YET(); + } } } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index c7a4a5952..4839b417b 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -62,6 +62,7 @@ namespace smt { void assert_implication(expr * premise, expr * conclusion); app * mk_strlen(app * e); + app * mk_concat(app * e1, app * e2); bool is_concat(app const * a) const { return a->is_app_of(get_id(), OP_STRCAT); } bool is_concat(enode const * n) const { return is_concat(n->get_owner()); } @@ -74,6 +75,8 @@ namespace smt { void set_up_axioms(expr * ex); void handle_equality(expr * lhs, expr * rhs); + expr * get_eqc_value(expr * n, bool & hasEqcValue); + void simplify_concat_equality(expr * lhs, expr * rhs); void solve_concat_eq_str(expr * concat, expr * str); From 876af399e394c1974019c897d081df76160e2177 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 28 Sep 2015 14:44:25 -0400 Subject: [PATCH 029/562] probably fix duplication of mk_string() terms also implement Case 2 of solve_concat_eq_str() --- src/ast/str_decl_plugin.cpp | 22 +++++++++++++++++---- src/ast/str_decl_plugin.h | 7 +++++++ src/smt/theory_str.cpp | 39 ++++++++++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 60db88b63..9398dbf34 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -99,11 +99,25 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters, return mk_func_decl(k); } +app * str_decl_plugin::mk_string(std::string & val) { + std::map::iterator it = string_cache.find(val); + if (it == string_cache.end()) { + char * new_buffer = alloc_svect(char, val.length() + 1); + strcpy(new_buffer, val.c_str()); + parameter p[1] = {parameter(new_buffer)}; + func_decl * d; + d = m_manager->mk_const_decl(m_strv_sym, m_str_decl, func_decl_info(m_family_id, OP_STR, 1, p)); + app * str = m_manager->mk_const(d); + string_cache[val] = str; + return str; + } else { + return it->second; + } +} + app * str_decl_plugin::mk_string(const char * val) { - parameter p[1] = {parameter(val)}; - func_decl * d; - d = m_manager->mk_const_decl(m_strv_sym, m_str_decl, func_decl_info(m_family_id, OP_STR, 1, p)); - return m_manager->mk_const(d); + std::string key(val); + return mk_string(key); } void str_decl_plugin::get_op_names(svector & op_names, symbol const & logic) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index a64e0c05f..f84c1ec31 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -19,6 +19,7 @@ Revision History: #include"ast.h" #include"arith_decl_plugin.h" +#include enum str_sort_kind { STRING_SORT, @@ -44,6 +45,8 @@ protected: func_decl * m_concat_decl; func_decl * m_length_decl; + std::map string_cache; + virtual void set_manager(ast_manager * m, family_id id); func_decl * mk_func_decl(decl_kind k); @@ -58,6 +61,7 @@ public: unsigned arity, sort * const * domain, sort * range); app * mk_string(const char * val); + app * mk_string(std::string & val); virtual void get_op_names(svector & op_names, symbol const & logic); virtual void get_sort_names(svector & sort_names, symbol const & logic); @@ -90,6 +94,9 @@ public: app * mk_string(const char * val) { return m_plugin->mk_string(val); } + app * mk_string(std::string & val) { + return m_plugin->mk_string(val); + } // TODO }; diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index f3e6496b7..08f83fdd3 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -468,11 +468,48 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { expr_ref equality(ctx.mk_eq_atom(concat, str), m); expr_ref diseq(m.mk_not(equality), m); assert_axiom(diseq); + return; } } else if (!arg1_has_eqc_value && arg2_has_eqc_value) { // Case 2: Concat(var, const) == const TRACE("t_str", tout << "Case 2: Concat(var, const) == const" << std::endl;); - NOT_IMPLEMENTED_YET(); + const char * str2; + m_strutil.is_string(arg2, & str2); + std::string arg2_str(str2); + int resultStrLen = const_str.length(); + int arg2StrLen = arg2_str.length(); + if (resultStrLen < arg2StrLen) { + // Inconsistency + TRACE("t_str", tout << "inconsistency detected: \"" + << arg2_str << + "\" is longer than \"" << const_str << "\"," + << " so cannot be concatenated with anything to form it" << std::endl;); + expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); + expr_ref diseq(m.mk_not(equality), m); + assert_axiom(diseq); + return; + } else { + int varStrLen = resultStrLen - arg2StrLen; + std::string firstPart = const_str.substr(0, varStrLen); + std::string secondPart = const_str.substr(varStrLen, arg2StrLen); + if (arg2_str != secondPart) { + // Inconsistency + TRACE("t_str", tout << "inconsistency detected: " + << "suffix of concatenation result expected \"" << secondPart << "\", " + << "actually \"" << arg2_str << "\"" + << std::endl;); + expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); + expr_ref diseq(m.mk_not(equality), m); + assert_axiom(diseq); + return; + } else { + expr_ref tmpStrConst(m_strutil.mk_string(firstPart), m); + expr_ref premise(ctx.mk_eq_atom(newConcat, str), m); + expr_ref conclusion(ctx.mk_eq_atom(arg1, tmpStrConst), m); + assert_implication(premise, conclusion); + return; + } + } } else if (arg1_has_eqc_value && !arg2_has_eqc_value) { // Case 3: Concat(const, var) == const TRACE("t_str", tout << "Case 3: Concat(const, var) == const" << std::endl;); From 871b08bd8cd42dd35a9ed19a8bc4e2d77fca4155 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 28 Sep 2015 14:52:43 -0400 Subject: [PATCH 030/562] solve_concat_eq_str() case 3 --- src/smt/theory_str.cpp | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 08f83fdd3..d6edc2f6b 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -513,7 +513,43 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } else if (arg1_has_eqc_value && !arg2_has_eqc_value) { // Case 3: Concat(const, var) == const TRACE("t_str", tout << "Case 3: Concat(const, var) == const" << std::endl;); - NOT_IMPLEMENTED_YET(); + const char * str1; + m_strutil.is_string(arg1, & str1); + std::string arg1_str(str1); + int resultStrLen = const_str.length(); + int arg1StrLen = arg1_str.length(); + if (resultStrLen < arg1StrLen) { + // Inconsistency + TRACE("t_str", tout << "inconsistency detected: \"" + << arg1_str << + "\" is longer than \"" << const_str << "\"," + << " so cannot be concatenated with anything to form it" << std::endl;); + expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); + expr_ref diseq(m.mk_not(equality), m); + assert_axiom(diseq); + return; + } else { + int varStrLen = resultStrLen - arg1StrLen; + std::string firstPart = const_str.substr(0, arg1StrLen); + std::string secondPart = const_str.substr(arg1StrLen, varStrLen); + if (arg1_str != firstPart) { + // Inconsistency + TRACE("t_str", tout << "inconsistency detected: " + << "prefix of concatenation result expected \"" << secondPart << "\", " + << "actually \"" << arg1_str << "\"" + << std::endl;); + expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); + expr_ref diseq(m.mk_not(equality), m); + assert_axiom(diseq); + return; + } else { + expr_ref tmpStrConst(m_strutil.mk_string(secondPart), m); + expr_ref premise(ctx.mk_eq_atom(newConcat, str), m); + expr_ref conclusion(ctx.mk_eq_atom(arg2, tmpStrConst), m); + assert_implication(premise, conclusion); + return; + } + } } else { // Case 4: Concat(var, var) == const TRACE("t_str", tout << "Case 4: Concat(var, var) == const" << std::endl;); From f473b92d5c20352cb8cafcea0adb9d02fcc87f4a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 28 Sep 2015 17:41:01 -0400 Subject: [PATCH 031/562] solve_concat_eq_str() case 4 WIP --- src/smt/theory_str.cpp | 174 ++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 5 ++ 2 files changed, 177 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index d6edc2f6b..458786110 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -27,7 +27,8 @@ theory_str::theory_str(ast_manager & m): theory(m.mk_family_id("str")), search_started(false), m_autil(m), - m_strutil(m) + m_strutil(m), + tmpXorVarCount(0) { } @@ -99,6 +100,31 @@ bool theory_str::internalize_term(app * term) { return true; } +Z3_ast mk_internal_xor_var(Z3_theory t) { + Z3_context ctx = Z3_theory_get_context(t); + std::stringstream ss; + ss << tmpXorVarCount; + tmpXorVarCount++; + std::string name = "$$_xor_" + ss.str(); + return mk_int_var(ctx, name.c_str()); +} + +app * theory_str::mk_internal_xor_var() { + context & ctx = get_context(); + ast_manager & m = get_manager(); + std::stringstream ss; + ss << tmpXorVarCount; + tmpXorVarCount++; + std::string name = "$$_xor_" + ss.str(); + // Z3_sort r = of_sort(mk_c(c)->m().mk_sort(mk_c(c)->get_arith_fid(), INT_SORT)); + sort * int_sort = m.mk_sort(m_autil.get_family_id(), INT_SORT); + symbol sym(name); + + app* a = m.mk_const(m.mk_const_decl(sym, int_sort)); + // TODO ctx.save_ast_trail(a)? + return a; +} + app * theory_str::mk_strlen(app * e) { /*if (m_strutil.is_string(e)) {*/ if (false) { const char * strval = 0; @@ -553,7 +579,151 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } else { // Case 4: Concat(var, var) == const TRACE("t_str", tout << "Case 4: Concat(var, var) == const" << std::endl;); - NOT_IMPLEMENTED_YET(); + // TODO large additions required in this section + if (true) { /* if (Concat(arg1, arg2) == NULL) { */ + int arg1Len = -1; /* = getLenValue(arg1); */ + int arg2Len = -1; /* = getLenValue(arg2); */ + if (arg1Len != -1 || arg2Len != -1) { + NOT_IMPLEMENTED_YET(); // TODO + } else { + /* + Z3_ast xorFlag = NULL; + std::pair key1(arg1, arg2); + std::pair key2(arg2, arg1); + if (varForBreakConcat.find(key1) == varForBreakConcat.end() && varForBreakConcat.find(key2) == varForBreakConcat.end()) { + xorFlag = mk_internal_xor_var(t); + varForBreakConcat[key1][0] = xorFlag; + } else { + if (varForBreakConcat.find(key1) != varForBreakConcat.end()) { + xorFlag = varForBreakConcat[key1][0]; + } else { + xorFlag = varForBreakConcat[key2][0]; + } + } + + int concatStrLen = const_str.length(); + int xor_pos = 0; + int and_count = 1; + Z3_ast * xor_items = new Z3_ast[concatStrLen + 1]; + Z3_ast * and_items = new Z3_ast[4 * (concatStrLen + 1) + 1]; + Z3_ast arg1_eq = NULL; + Z3_ast arg2_eq = NULL; + for (int i = 0; i < concatStrLen + 1; i++) { + std::string prefixStr = const_str.substr(0, i); + std::string suffixStr = const_str.substr(i, concatStrLen - i); + + // skip invalidate options + if (isConcatFunc(t, arg1) && canConcatEqStr(t, arg1, prefixStr) == 0) { + continue; + } + if (isConcatFunc(t, arg2) && canConcatEqStr(t, arg2, suffixStr) == 0) { + continue; + } + + Z3_ast xorAst = Z3_mk_eq(ctx, xorFlag, mk_int(ctx, xor_pos)); + xor_items[xor_pos++] = xorAst; + + Z3_ast prefixAst = my_mk_str_value(t, prefixStr.c_str()); + arg1_eq = Z3_mk_eq(ctx, arg1, prefixAst); + and_items[and_count++] = Z3_mk_eq(ctx, xorAst, arg1_eq); + + Z3_ast suffixAst = my_mk_str_value(t, suffixStr.c_str()); + arg2_eq = Z3_mk_eq(ctx, arg2, suffixAst); + and_items[and_count++] = Z3_mk_eq(ctx, xorAst, arg2_eq); + } + */ + expr_ref xorFlag(m); + std::pair key1(arg1, arg2); + std::pair key2(arg2, arg1); + std::map, std::map >::iterator varBreak_key1 = + varForBreakConcat.find(key1); + std::map, std::map >::iterator varBreak_key2 = + varForBreakConcat.find(key2); + if (varBreak_key1 == varForBreakConcat.end() && varBreak_key2 == varForBreakConcat.end()) { + xorFlag = mk_internal_xor_var(); + varForBreakConcat[key1][0] = xorFlag; + } else if (varBreak_key1 != varForBreakConcat.end()) { + xorFlag = varForBreakConcat[key1][0]; + } else { // varBreak_key2 != varForBreakConcat.end() + xorFlag = varForBreakConcat[key2][0]; + } + + int concatStrLen = const_str.length(); + int xor_pos = 0; + int and_count = 1; + expr * xor_items[] = new expr*[concatStrLen + 1]; + expr * and_items[] = new expr*[4 * (concatStrLen+1) + 1]; + + expr_ref arg1_eq(m); + expr_ref arg2_eq(m); + + for (int i = 0; i < concatStrLen + 1; ++i) { + std::string prefixStr = const_str.substr(0, i); + std::string suffixStr = const_str.substr(i, concatStrLen - i); + // skip invalid options + // TODO canConcatEqStr() checks: + /* + if (isConcatFunc(t, arg1) && canConcatEqStr(t, arg1, prefixStr) == 0) { + continue; + } + if (isConcatFunc(t, arg2) && canConcatEqStr(t, arg2, suffixStr) == 0) { + continue; + } + */ + expr_ref xorAst(ctx.mk_eq_atom(xorFlag, mk_int(xor_pos)), m); + xor_items[xor_pos++] = xorAst; + + expr_ref prefixAst(m_strutil.mk_string(prefixStr), m); + arg1_eq = ctx.mk_eq_atom(arg1, prefixAst); + and_items[and_count++] = ctx.mk_eq_atom(xorAst, arg1_eq); + + expr_ref suffixAst(m_strutil.mk_string(prefixStr), m); + arg2_eq = ctx.mk_eq_atom(arg2, suffixAst); + and_items[and_count++] = ctx.mk_eq_atom(xorAst, arg2_eq); + } + + expr_ref implyL(ctx.mk_eq_atom(concat, str), m); + expr_ref implyR(m); + if (xor_pos == 0) { + // negate + expr_ref concat_eq_str(ctx.mk_eq_atom(concat, str), m); + expr_ref negate_ast(m.mk_not(concat_eq_str), m); + assert_axiom(negate_ast); + } else { + // TODO + if (xor_pos == 1) { + + } else { + + } + } + delete[] xor_items; + delete[] and_items; + + /* + + Z3_ast implyL = Z3_mk_eq(ctx, concatAst, constStr); + Z3_ast implyR1 = NULL; + if (xor_pos == 0) { + // negate + Z3_ast negateAst = Z3_mk_not(ctx, Z3_mk_eq(ctx, concatAst, constStr)); + addAxiom(t, negateAst, __LINE__); + } else { + if (xor_pos == 1) { + and_items[0] = xor_items[0]; + implyR1 = Z3_mk_and(ctx, and_count, and_items); + } else { + and_items[0] = Z3_mk_or(ctx, xor_pos, xor_items); + implyR1 = Z3_mk_and(ctx, and_count, and_items); + } + Z3_ast implyToAssert = Z3_mk_implies(ctx, implyL, implyR1); + addAxiom(t, implyToAssert, __LINE__); + } + delete[] xor_items; + delete[] and_items; + */ + } + } } } } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 4839b417b..ea6ec8551 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -57,6 +57,9 @@ namespace smt { ptr_vector m_basicstr_axiom_todo; svector > m_str_eq_todo; + + int tmpXorVarCount; + std::map, std::map > varForBreakConcat; protected: void assert_axiom(expr * e); void assert_implication(expr * premise, expr * conclusion); @@ -64,6 +67,8 @@ namespace smt { app * mk_strlen(app * e); app * mk_concat(app * e1, app * e2); + app * mk_internal_xor_var(); + bool is_concat(app const * a) const { return a->is_app_of(get_id(), OP_STRCAT); } bool is_concat(enode const * n) const { return is_concat(n->get_owner()); } bool is_string(app const * a) const { return a->is_app_of(get_id(), OP_STR); } From 2320b6dc48106a2ccbb8748781b7820d62304e5e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 29 Sep 2015 17:46:51 -0400 Subject: [PATCH 032/562] solve_concat_eq_str() case 4: somewhat working something's wrong but it may be very simple to fix --- src/ast/str_decl_plugin.h | 8 +-- src/smt/theory_str.cpp | 120 +++++++------------------------------- 2 files changed, 26 insertions(+), 102 deletions(-) diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index f84c1ec31..61d1bc2f2 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -38,13 +38,13 @@ protected: symbol m_strv_sym; sort * m_str_decl; - arith_decl_plugin * m_arith_plugin; - sort * m_int_sort; - family_id m_arith_fid; - func_decl * m_concat_decl; func_decl * m_length_decl; + arith_decl_plugin * m_arith_plugin; + family_id m_arith_fid; + sort * m_int_sort; + std::map string_cache; virtual void set_manager(ast_manager * m, family_id id); diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 458786110..e2896f4f5 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -37,7 +37,7 @@ theory_str::~theory_str() { void theory_str::assert_axiom(expr * e) { if (get_manager().is_true(e)) return; - TRACE("t_str_detail", tout << "asserting " << mk_ismt2_pp(e, get_manager()) << "\n";); + TRACE("t_str_detail", tout << "asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); context & ctx = get_context(); if (!ctx.b_internalized(e)) { ctx.internalize(e, true); @@ -45,11 +45,12 @@ void theory_str::assert_axiom(expr * e) { literal lit(ctx.get_literal(e)); ctx.mark_as_relevant(lit); ctx.mk_th_axiom(get_id(), 1, &lit); - TRACE("t_str_detail", tout << "done asserting " << mk_ismt2_pp(e, get_manager()) << "\n";); + TRACE("t_str_detail", tout << "done asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); } void theory_str::assert_implication(expr * premise, expr * conclusion) { ast_manager & m = get_manager(); + TRACE("t_str_detail", tout << "asserting implication " << mk_ismt2_pp(premise, m) << " -> " << mk_ismt2_pp(conclusion, m) << std::endl;); expr_ref axiom(m.mk_or(m.mk_not(premise), conclusion), m); assert_axiom(axiom); } @@ -100,15 +101,6 @@ bool theory_str::internalize_term(app * term) { return true; } -Z3_ast mk_internal_xor_var(Z3_theory t) { - Z3_context ctx = Z3_theory_get_context(t); - std::stringstream ss; - ss << tmpXorVarCount; - tmpXorVarCount++; - std::string name = "$$_xor_" + ss.str(); - return mk_int_var(ctx, name.c_str()); -} - app * theory_str::mk_internal_xor_var() { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -118,7 +110,9 @@ app * theory_str::mk_internal_xor_var() { std::string name = "$$_xor_" + ss.str(); // Z3_sort r = of_sort(mk_c(c)->m().mk_sort(mk_c(c)->get_arith_fid(), INT_SORT)); sort * int_sort = m.mk_sort(m_autil.get_family_id(), INT_SORT); - symbol sym(name); + char * new_buffer = alloc_svect(char, name.length() + 1); + strcpy(new_buffer, name.c_str()); + symbol sym(new_buffer); app* a = m.mk_const(m.mk_const_decl(sym, int_sort)); // TODO ctx.save_ast_trail(a)? @@ -585,53 +579,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { int arg2Len = -1; /* = getLenValue(arg2); */ if (arg1Len != -1 || arg2Len != -1) { NOT_IMPLEMENTED_YET(); // TODO - } else { - /* - Z3_ast xorFlag = NULL; - std::pair key1(arg1, arg2); - std::pair key2(arg2, arg1); - if (varForBreakConcat.find(key1) == varForBreakConcat.end() && varForBreakConcat.find(key2) == varForBreakConcat.end()) { - xorFlag = mk_internal_xor_var(t); - varForBreakConcat[key1][0] = xorFlag; - } else { - if (varForBreakConcat.find(key1) != varForBreakConcat.end()) { - xorFlag = varForBreakConcat[key1][0]; - } else { - xorFlag = varForBreakConcat[key2][0]; - } - } - - int concatStrLen = const_str.length(); - int xor_pos = 0; - int and_count = 1; - Z3_ast * xor_items = new Z3_ast[concatStrLen + 1]; - Z3_ast * and_items = new Z3_ast[4 * (concatStrLen + 1) + 1]; - Z3_ast arg1_eq = NULL; - Z3_ast arg2_eq = NULL; - for (int i = 0; i < concatStrLen + 1; i++) { - std::string prefixStr = const_str.substr(0, i); - std::string suffixStr = const_str.substr(i, concatStrLen - i); - - // skip invalidate options - if (isConcatFunc(t, arg1) && canConcatEqStr(t, arg1, prefixStr) == 0) { - continue; - } - if (isConcatFunc(t, arg2) && canConcatEqStr(t, arg2, suffixStr) == 0) { - continue; - } - - Z3_ast xorAst = Z3_mk_eq(ctx, xorFlag, mk_int(ctx, xor_pos)); - xor_items[xor_pos++] = xorAst; - - Z3_ast prefixAst = my_mk_str_value(t, prefixStr.c_str()); - arg1_eq = Z3_mk_eq(ctx, arg1, prefixAst); - and_items[and_count++] = Z3_mk_eq(ctx, xorAst, arg1_eq); - - Z3_ast suffixAst = my_mk_str_value(t, suffixStr.c_str()); - arg2_eq = Z3_mk_eq(ctx, arg2, suffixAst); - and_items[and_count++] = Z3_mk_eq(ctx, xorAst, arg2_eq); - } - */ + } else { /* ! (arg1Len != 1 || arg2Len != 1) */ expr_ref xorFlag(m); std::pair key1(arg1, arg2); std::pair key2(arg2, arg1); @@ -651,11 +599,8 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { int concatStrLen = const_str.length(); int xor_pos = 0; int and_count = 1; - expr * xor_items[] = new expr*[concatStrLen + 1]; - expr * and_items[] = new expr*[4 * (concatStrLen+1) + 1]; - - expr_ref arg1_eq(m); - expr_ref arg2_eq(m); + expr ** xor_items = new expr*[concatStrLen + 1]; + expr ** and_items = new expr*[4 * (concatStrLen+1) + 1]; for (int i = 0; i < concatStrLen + 1; ++i) { std::string prefixStr = const_str.substr(0, i); @@ -670,60 +615,39 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { continue; } */ - expr_ref xorAst(ctx.mk_eq_atom(xorFlag, mk_int(xor_pos)), m); + expr_ref xorAst(ctx.mk_eq_atom(xorFlag, m_autil.mk_numeral(rational(xor_pos), true)), m); xor_items[xor_pos++] = xorAst; expr_ref prefixAst(m_strutil.mk_string(prefixStr), m); - arg1_eq = ctx.mk_eq_atom(arg1, prefixAst); + expr_ref arg1_eq (ctx.mk_eq_atom(arg1, prefixAst), m); and_items[and_count++] = ctx.mk_eq_atom(xorAst, arg1_eq); expr_ref suffixAst(m_strutil.mk_string(prefixStr), m); - arg2_eq = ctx.mk_eq_atom(arg2, suffixAst); + expr_ref arg2_eq (ctx.mk_eq_atom(arg2, suffixAst), m); and_items[and_count++] = ctx.mk_eq_atom(xorAst, arg2_eq); } expr_ref implyL(ctx.mk_eq_atom(concat, str), m); - expr_ref implyR(m); + expr_ref implyR1(m); if (xor_pos == 0) { // negate expr_ref concat_eq_str(ctx.mk_eq_atom(concat, str), m); expr_ref negate_ast(m.mk_not(concat_eq_str), m); assert_axiom(negate_ast); } else { - // TODO if (xor_pos == 1) { - + and_items[0] = xor_items[0]; + implyR1 = m.mk_and(and_count, and_items); } else { - + and_items[0] = m.mk_or(xor_pos, xor_items); + implyR1 = m.mk_and(and_count, and_items); } + assert_implication(implyL, implyR1); } delete[] xor_items; delete[] and_items; - - /* - - Z3_ast implyL = Z3_mk_eq(ctx, concatAst, constStr); - Z3_ast implyR1 = NULL; - if (xor_pos == 0) { - // negate - Z3_ast negateAst = Z3_mk_not(ctx, Z3_mk_eq(ctx, concatAst, constStr)); - addAxiom(t, negateAst, __LINE__); - } else { - if (xor_pos == 1) { - and_items[0] = xor_items[0]; - implyR1 = Z3_mk_and(ctx, and_count, and_items); - } else { - and_items[0] = Z3_mk_or(ctx, xor_pos, xor_items); - implyR1 = Z3_mk_and(ctx, and_count, and_items); - } - Z3_ast implyToAssert = Z3_mk_implies(ctx, implyL, implyR1); - addAxiom(t, implyToAssert, __LINE__); - } - delete[] xor_items; - delete[] and_items; - */ - } - } + } /* (arg1Len != 1 || arg2Len != 1) */ + } /* if (Concat(arg1, arg2) == NULL) */ } } } @@ -945,7 +869,7 @@ void theory_str::new_diseq_eh(theory_var x, theory_var y) { } void theory_str::relevant_eh(app * n) { - TRACE("t_str", tout << "relevant: " << mk_ismt2_pp(n, get_manager()) << "\n";); + TRACE("t_str", tout << "relevant: " << mk_ismt2_pp(n, get_manager()) << std::endl;); } void theory_str::assign_eh(bool_var v, bool is_true) { @@ -988,7 +912,7 @@ void theory_str::init_model(model_generator & mg) { model_value_proc * theory_str::mk_value(enode * n, model_generator & mg) { TRACE("t_str", tout << "mk_value for: " << mk_ismt2_pp(n->get_owner(), get_manager()) << - " (sort " << mk_ismt2_pp(get_manager().get_sort(n->get_owner()), get_manager()) << ")\n";); + " (sort " << mk_ismt2_pp(get_manager().get_sort(n->get_owner()), get_manager()) << ")" << std::endl;); ast_manager & m = get_manager(); context & ctx = get_context(); app_ref owner(m); From 191c50b529310cae9f39463d371eb92b896560d5 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 29 Sep 2015 17:52:19 -0400 Subject: [PATCH 033/562] fix solve_concat_eq_str() case 4: prefixStr should have been suffixStr --- src/smt/theory_str.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index e2896f4f5..f9fb7e3a4 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -622,7 +622,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { expr_ref arg1_eq (ctx.mk_eq_atom(arg1, prefixAst), m); and_items[and_count++] = ctx.mk_eq_atom(xorAst, arg1_eq); - expr_ref suffixAst(m_strutil.mk_string(prefixStr), m); + expr_ref suffixAst(m_strutil.mk_string(suffixStr), m); expr_ref arg2_eq (ctx.mk_eq_atom(arg2, suffixAst), m); and_items[and_count++] = ctx.mk_eq_atom(xorAst, arg2_eq); } From 8ed86d2f19a074dd68d10ca5832a4cfa18351cbb Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 29 Sep 2015 18:02:05 -0400 Subject: [PATCH 034/562] add concatenation axiom --- src/smt/theory_str.cpp | 16 ++++++++++++++-- src/smt/theory_str.h | 1 + 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index f9fb7e3a4..ea7b84d62 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -142,7 +142,7 @@ app * theory_str::mk_concat(app * e1, app * e2) { } bool theory_str::can_propagate() { - return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty(); + return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty() || !m_concat_axiom_todo.empty(); } void theory_str::propagate() { @@ -160,6 +160,11 @@ void theory_str::propagate() { handle_equality(lhs->get_owner(), rhs->get_owner()); } m_str_eq_todo.reset(); + + for (unsigned i = 0; i < m_concat_axiom_todo.empty(); ++i) { + instantiate_concat_axiom(m_concat_axiom_todo[i]); + } + m_concat_axiom_todo.reset(); } } @@ -167,7 +172,6 @@ void theory_str::propagate() { * Instantiate an axiom of the following form: * Length(Concat(x, y)) = Length(x) + Length(y) */ -// TODO this isn't used yet void theory_str::instantiate_concat_axiom(enode * cat) { SASSERT(is_concat(cat)); app * a_cat = cat->get_owner(); @@ -321,6 +325,7 @@ void theory_str::reset_eh() { TRACE("t_str", tout << "resetting" << std::endl;); m_basicstr_axiom_todo.reset(); m_str_eq_todo.reset(); + m_concat_axiom_todo.reset(); pop_scope_eh(0); } @@ -767,6 +772,8 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { } } + // TODO simplify_parent over eqc + // TODO regex unroll? (much later) } @@ -785,6 +792,11 @@ void theory_str::set_up_axioms(expr * ex) { enode * n = ctx.get_enode(ex); SASSERT(n); m_basicstr_axiom_todo.push_back(n); + + // if additionally ex is a concatenation, set up concatenation axioms + if (is_app(ex) && is_concat(to_app(ex))) { + m_concat_axiom_todo.push_back(n); + } } else { TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << ": expr is of wrong sort, ignoring" << std::endl;); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index ea6ec8551..458287392 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -57,6 +57,7 @@ namespace smt { ptr_vector m_basicstr_axiom_todo; svector > m_str_eq_todo; + ptr_vector m_concat_axiom_todo; int tmpXorVarCount; std::map, std::map > varForBreakConcat; From 1cdfe159b8d23a458a5b97c8c76325c86e2fe366 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 29 Sep 2015 20:19:43 -0400 Subject: [PATCH 035/562] simplify_concat_equality() and easy cases there still WIP especially wrt. model generation but what's here does work --- src/ast/str_decl_plugin.cpp | 2 +- src/smt/theory_str.cpp | 392 +++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 12 +- 3 files changed, 401 insertions(+), 5 deletions(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 9398dbf34..c72a5dbc2 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -102,7 +102,7 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters, app * str_decl_plugin::mk_string(std::string & val) { std::map::iterator it = string_cache.find(val); if (it == string_cache.end()) { - char * new_buffer = alloc_svect(char, val.length() + 1); + char * new_buffer = alloc_svect(char, (val.length() + 1)); strcpy(new_buffer, val.c_str()); parameter p[1] = {parameter(new_buffer)}; func_decl * d; diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ea7b84d62..92edbc22b 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -119,7 +119,7 @@ app * theory_str::mk_internal_xor_var() { return a; } -app * theory_str::mk_strlen(app * e) { +app * theory_str::mk_strlen(expr * e) { /*if (m_strutil.is_string(e)) {*/ if (false) { const char * strval = 0; m_strutil.is_string(e, &strval); @@ -378,9 +378,258 @@ void theory_str::group_terms_by_eqc(expr * n, std::set & concats, std::se } while (eqcNode != nNode); } -void theory_str::simplify_concat_equality(expr * lhs, expr * rhs) { - // TODO strArgmt::simplifyConcatEq() +void theory_str::get_nodes_in_concat(expr * node, ptr_vector & nodeList) { + app * a_node = to_app(node); + if (!is_concat(a_node)) { + nodeList.push_back(node); + return; + } else { + SASSERT(a_node->get_num_args() == 2); + expr * leftArg = a_node->get_arg(0); + expr * rightArg = a_node->get_arg(1); + get_nodes_in_concat(leftArg, nodeList); + get_nodes_in_concat(rightArg, nodeList); + } } + +/* + * The inputs: + * ~ nn: non const node + * ~ eq_str: the equivalent constant string of nn + * Iterate the parent of all eqc nodes of nn, looking for: + * ~ concat node + * to see whether some concat nodes can be simplified. + */ + +void theory_str::simplify_parent(expr * nn, expr * eq_str) { + // TODO strTheory::simplifyParent() +} + +expr * theory_str::simplify_concat(expr * node) { + ast_manager & m = get_manager(); + context & ctx = get_context(); + std::map resolvedMap; + ptr_vector argVec; + get_nodes_in_concat(node, argVec); + + for (unsigned i = 0; i < argVec.size(); ++i) { + bool vArgHasEqcValue = false; + expr * vArg = get_eqc_value(argVec[i], vArgHasEqcValue); + if (vArg != argVec[i]) { + resolvedMap[argVec[i]] = vArg; + } + } + + if (resolvedMap.size() == 0) { + // no simplification possible + return node; + } else { + app * resultAst = m_strutil.mk_string(""); + for (unsigned i = 0; i < argVec.size(); ++i) { + bool vArgHasEqcValue = false; + expr * vArg = get_eqc_value(argVec[i], vArgHasEqcValue); + resultAst = mk_concat(to_app(resultAst), to_app(vArg)); + } + TRACE("t_str_detail", tout << mk_ismt2_pp(node, m) << " is simplified to " << mk_ismt2_pp(resultAst, m) << std::endl;); + + if (in_same_eqc(node, resultAst)) { + TRACE("t_str_detail", tout << "SKIP: both concats are already in the same equivalence class" << std::endl;); + } else { + expr ** items = alloc_svect(expr*, resolvedMap.size()); + int pos = 0; + std::map::iterator itor = resolvedMap.begin(); + for (; itor != resolvedMap.end(); ++itor) { + items[pos++] = ctx.mk_eq_atom(itor->first, itor->second); + } + expr_ref premise(m); + if (pos == 1) { + premise = items[0]; + } else { + premise = m.mk_and(pos, items); + } + expr_ref conclusion(ctx.mk_eq_atom(node, resultAst), m); + assert_implication(premise, conclusion); + } + return resultAst; + } + +} + +/* + * Handle two equivalent Concats. + */ +void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { + ast_manager & m = get_manager(); + context & ctx = get_context(); + + app * a_nn1 = to_app(nn1); + SASSERT(a_nn1->get_num_args() == 2); + app * a_nn2 = to_app(nn2); + SASSERT(a_nn2->get_num_args() == 2); + + expr * a1_arg0 = a_nn1->get_arg(0); + expr * a1_arg1 = a_nn1->get_arg(1); + expr * a2_arg0 = a_nn2->get_arg(0); + expr * a2_arg1 = a_nn2->get_arg(1); + + // TODO + /* + int a1_arg0_len = getLenValue(t, a1_arg0); + int a1_arg1_len = getLenValue(t, a1_arg1); + int a2_arg0_len = getLenValue(t, a2_arg0); + int a2_arg1_len = getLenValue(t, a2_arg1); + */ + int a1_arg0_len = -1; + int a1_arg1_len = -1; + int a2_arg0_len = -1; + int a2_arg1_len = -1; + + TRACE("t_str", tout << "nn1 = " << mk_ismt2_pp(nn1, m) << std::endl + << "nn2 = " << mk_ismt2_pp(nn2, m) << std::endl;); + + // TODO inferLenConcatEq(nn1, nn2); + + if (a1_arg0 == a2_arg0) { + if (!in_same_eqc(a1_arg1, a2_arg1)) { + expr_ref premise(ctx.mk_eq_atom(nn1, nn2), m); + expr_ref eq1(ctx.mk_eq_atom(a1_arg1, a2_arg1), m); + expr_ref eq2(ctx.mk_eq_atom(mk_strlen(a1_arg1), mk_strlen(a2_arg1)), m); + expr_ref conclusion(m.mk_and(eq1, eq2), m); + assert_implication(premise, conclusion); + } + TRACE("t_str_detail", tout << "SKIP: a1_arg0 == a2_arg0" << std::endl;); + return; + } + + if (a1_arg1 == a2_arg1) { + if (!in_same_eqc(a1_arg0, a2_arg0)) { + expr_ref premise(ctx.mk_eq_atom(nn1, nn2), m); + expr_ref eq1(ctx.mk_eq_atom(a1_arg0, a2_arg0), m); + expr_ref eq2(ctx.mk_eq_atom(mk_strlen(a1_arg0), mk_strlen(a2_arg0)), m); + expr_ref conclusion(m.mk_and(eq1, eq2), m); + assert_implication(premise, conclusion); + } + TRACE("t_str_detail", tout << "SKIP: a1_arg1 == a2_arg1" << std::endl;); + return; + } + + // quick path + + if (in_same_eqc(a1_arg0, a2_arg0)) { + if (in_same_eqc(a1_arg1, a2_arg1)) { + TRACE("t_str_detail", tout << "SKIP: a1_arg0 =~ a2_arg0 and a1_arg1 =~ a2_arg1" << std::endl;); + return; + } else { + TRACE("t_str_detail", tout << "quick path 1-1: a1_arg0 =~ a2_arg0" << std::endl;); + expr_ref premise(m.mk_and(ctx.mk_eq_atom(nn1, nn2), ctx.mk_eq_atom(a1_arg0, a2_arg0)), m); + expr_ref conclusion(m.mk_and(ctx.mk_eq_atom(a1_arg1, a2_arg1), ctx.mk_eq_atom(mk_strlen(a1_arg1), mk_strlen(a2_arg1))), m); + assert_implication(premise, conclusion); + return; + } + } else { + if (in_same_eqc(a1_arg1, a2_arg1)) { + TRACE("t_str_detail", tout << "quick path 1-2: a1_arg1 =~ a2_arg1" << std::endl;); + expr_ref premise(m.mk_and(ctx.mk_eq_atom(nn1, nn2), ctx.mk_eq_atom(a1_arg1, a2_arg1)), m); + expr_ref conclusion(m.mk_and(ctx.mk_eq_atom(a1_arg0, a2_arg0), ctx.mk_eq_atom(mk_strlen(a1_arg0), mk_strlen(a2_arg0))), m); + assert_implication(premise, conclusion); + return; + } + } + + // TODO quick path 1-2 + /* + if(a1_arg0_len != -1 && a2_arg0_len != -1 && a1_arg0_len == a2_arg0_len){ + if (! inSameEqc(t, a1_arg0, a2_arg0)) { + __debugPrint(logFile, ">> [simplifyConcatEq] Quick Path 2-1: len(nn1.arg0) == len(nn2.arg0)\n"); + Z3_ast ax_l1 = Z3_mk_eq(ctx, nn1, nn2); + Z3_ast ax_l2 = Z3_mk_eq(ctx, mk_length(t, a1_arg0), mk_length(t, a2_arg0)); + Z3_ast ax_r1 = Z3_mk_eq(ctx, a1_arg0, a2_arg0); + Z3_ast ax_r2 = Z3_mk_eq(ctx, a1_arg1, a2_arg1); + Z3_ast toAdd = Z3_mk_implies(ctx, mk_2_and(t, ax_l1, ax_l2), mk_2_and(t, ax_r1, ax_r2)); + addAxiom(t, toAdd, __LINE__); + return; + } + } + + if (a1_arg1_len != -1 && a2_arg1_len != -1 && a1_arg1_len == a2_arg1_len) + { + if (!inSameEqc(t, a1_arg1, a2_arg1)) { + __debugPrint(logFile, ">> [simplifyConcatEq] Quick Path 2-2: len(nn1.arg1) == len(nn2.arg1)\n"); + Z3_ast ax_l1 = Z3_mk_eq(ctx, nn1, nn2); + Z3_ast ax_l2 = Z3_mk_eq(ctx, mk_length(t, a1_arg1), mk_length(t, a2_arg1)); + Z3_ast ax_r1 = Z3_mk_eq(ctx, a1_arg0, a2_arg0); + Z3_ast ax_r2 = Z3_mk_eq(ctx, a1_arg1, a2_arg1); + Z3_ast toAdd = Z3_mk_implies(ctx, mk_2_and(t, ax_l1, ax_l2), mk_2_and(t, ax_r1, ax_r2)); + addAxiom(t, toAdd, __LINE__); + return; + } + } + */ + + expr * new_nn1 = simplify_concat(nn1); + expr * new_nn2 = simplify_concat(nn2); + app * a_new_nn1 = to_app(new_nn1); + app * a_new_nn2 = to_app(new_nn2); + expr * v1_arg0 = a_new_nn1->get_arg(0); + expr * v1_arg1 = a_new_nn1->get_arg(1); + expr * v2_arg0 = a_new_nn2->get_arg(0); + expr * v2_arg1 = a_new_nn2->get_arg(1); + + TRACE("t_str_detail", tout << "new_nn1 = " << mk_ismt2_pp(new_nn1, m) << std::endl + << "new_nn2 = " << mk_ismt2_pp(new_nn2, m) << std::endl;); + + if (new_nn1 == new_nn2) { + TRACE("t_str_detail", tout << "equal concats, return" << std::endl;); + return; + } + + if (!can_two_nodes_eq(new_nn1, new_nn2)) { + expr_ref detected(m.mk_not(ctx.mk_eq_atom(new_nn1, new_nn2)), m); + TRACE("t_str_detail", tout << "inconsistency detected: " << mk_ismt2_pp(detected, m) << std::endl;); + assert_axiom(detected); + return; + } + + // check whether new_nn1 and new_nn2 are still concats + + bool n1IsConcat = is_concat(a_new_nn1); + bool n2IsConcat = is_concat(a_new_nn2); + if (!n1IsConcat && n2IsConcat) { + TRACE("t_str_detail", tout << "nn1_new is not a concat" << std::endl;); + if (is_string(a_new_nn1)) { + simplify_parent(new_nn2, new_nn1); + } + return; + } else if (n1IsConcat && !n2IsConcat) { + TRACE("t_str_detail", tout << "nn2_new is not a concat" << std::endl;); + if (is_string(a_new_nn2)) { + simplify_parent(new_nn1, new_nn2); + } + return; + } + + if (!in_same_eqc(new_nn1, new_nn2) && (nn1 != new_nn1 || nn2 != new_nn2)) { + int ii4 = 0; + expr* item[3]; + if (nn1 != new_nn1) { + item[ii4++] = ctx.mk_eq_atom(nn1, new_nn1); + } + if (nn2 != new_nn2) { + item[ii4++] = ctx.mk_eq_atom(nn2, new_nn2); + } + item[ii4++] = ctx.mk_eq_atom(nn1, nn2); + expr_ref premise(m.mk_and(ii4, item), m); + expr_ref conclusion(ctx.mk_eq_atom(new_nn1, new_nn2), m); + assert_implication(premise, conclusion); + } + + // start to split both concats + + // TODO + NOT_IMPLEMENTED_YET(); + +} + /* * Look through the equivalence class of n to find a string constant. * Return that constant if it is found, and set hasEqcValue to true. @@ -403,6 +652,119 @@ expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { return n; } +/* + * Decide whether n1 and n2 are already in the same equivalence class. + * This only checks whether the core considers them to be equal; + * they may not actually be equal. + */ +bool theory_str::in_same_eqc(expr * n1, expr * n2) { + if (n1 == n2) return true; + context & ctx = get_context(); + enode * n1Node = ctx.get_enode(n1); + enode * n2Node = ctx.get_enode(n2); + + // here's what the old Z3str2 would have done; we can do something much better + /* + n1Node->get_root(); + enode * curr = n1Node->get_next(); + while (curr != n1Node) { + if (curr == n2Node) { + return true; + } + curr = curr->get_next(); + } + return false; + */ + return n1Node->get_root() == n2Node->get_root(); +} + +/* +bool canTwoNodesEq(Z3_theory t, Z3_ast n1, Z3_ast n2) { + Z3_ast n1_curr = n1; + Z3_ast n2_curr = n2; + + // case 0: n1_curr is const string, n2_curr is const string + if (isConstStr(t, n1_curr) && isConstStr(t, n2_curr)) { + if (n1_curr != n2_curr) { + return false; + } + } + // case 1: n1_curr is concat, n2_curr is const string + else if (isConcatFunc(t, n1_curr) && isConstStr(t, n2_curr)) { + std::string n2_curr_str = getConstStrValue(t, n2_curr); + if (canConcatEqStr(t, n1_curr, n2_curr_str) != 1) { + return false; + } + } + // case 2: n2_curr is concat, n1_curr is const string + else if (isConcatFunc(t, n2_curr) && isConstStr(t, n1_curr)) { + std::string n1_curr_str = getConstStrValue(t, n1_curr); + if (canConcatEqStr(t, n2_curr, n1_curr_str) != 1) { + return false; + } + } else if (isConcatFunc(t, n1_curr) && isConcatFunc(t, n2_curr)) { + if (canConcatEqConcat(t, n1_curr, n2_curr) != 1) { + return false; + } + } + + return true; +} +*/ + +bool theory_str::can_concat_eq_str(expr * concat, std::string str) { + // TODO + return true; +} + +bool theory_str::can_concat_eq_concat(expr * concat1, expr * concat2) { + // TODO + return true; +} + +/* + * Check whether n1 and n2 could be equal. + * Returns true if n1 could equal n2 (maybe), + * and false if n1 is definitely not equal to n2 (no). + */ +bool theory_str::can_two_nodes_eq(expr * n1, expr * n2) { + app * n1_curr = to_app(n1); + app * n2_curr = to_app(n2); + + // case 0: n1_curr is const string, n2_curr is const string + if (is_string(n1_curr) && is_string(n2_curr)) { + if (n1_curr != n2_curr) { + return false; + } + } + // case 1: n1_curr is concat, n2_curr is const string + else if (is_concat(n1_curr) && is_string(n2_curr)) { + const char * tmp = 0; + m_strutil.is_string(n2_curr, & tmp); + std::string n2_curr_str(tmp); + if (!can_concat_eq_str(n1_curr, n2_curr_str)) { + return false; + } + } + // case 2: n2_curr is concat, n1_curr is const string + else if (is_concat(n2_curr) && is_string(n1_curr)) { + const char * tmp = 0; + m_strutil.is_string(n1_curr, & tmp); + std::string n1_curr_str(tmp); + if (!can_concat_eq_str(n2_curr, n1_curr_str)) { + return false; + } + } + // case 3: both are concats + else if (is_concat(n1_curr) && is_concat(n2_curr)) { + if (!can_concat_eq_concat(n1_curr, n2_curr)) { + return false; + } + } + + return true; +} + /* * strArgmt::solve_concat_eq_str() * Solve concatenations of the form: @@ -604,8 +966,12 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { int concatStrLen = const_str.length(); int xor_pos = 0; int and_count = 1; + /* expr ** xor_items = new expr*[concatStrLen + 1]; expr ** and_items = new expr*[4 * (concatStrLen+1) + 1]; + */ + expr ** xor_items = alloc_svect(expr*, (concatStrLen+1)); + expr ** and_items = alloc_svect(expr*, (4 * (concatStrLen+1) + 1)); for (int i = 0; i < concatStrLen + 1; ++i) { std::string prefixStr = const_str.substr(0, i); @@ -736,6 +1102,11 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { ); // step 1: Concat == Concat + // I'm disabling this entire code block for now. It may no longer be useful. + // Z3 seems to be putting LHS and RHS into the same equivalence class extremely early. + // As a result, simplify_concat_equality() is never getting called, + // and if it were called, it would probably get called with the same element on both sides. + /* bool hasCommon = false; if (eqc_lhs_concat.size() != 0 && eqc_rhs_concat.size() != 0) { std::set::iterator itor1 = eqc_lhs_concat.begin(); @@ -756,6 +1127,21 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { simplify_concat_equality(*(eqc_lhs_concat.begin()), *(eqc_rhs_concat.begin())); } } + */ + if (eqc_lhs_concat.size() != 0 && eqc_rhs_concat.size() != 0) { + // let's pick the first concat in the LHS's eqc + // and find some concat in the RHS's eqc that is + // distinct from the first one we picked + expr * lhs = *eqc_lhs_concat.begin(); + std::set::iterator itor2 = eqc_rhs_concat.begin(); + for (; itor2 != eqc_rhs_concat.end(); ++itor2) { + expr * rhs = *itor2; + if (lhs != rhs) { + simplify_concat_equality(lhs, rhs); + break; + } + } + } // step 2: Concat == Constant if (eqc_lhs_const.size() != 0) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 458287392..c3641016f 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -65,7 +65,7 @@ namespace smt { void assert_axiom(expr * e); void assert_implication(expr * premise, expr * conclusion); - app * mk_strlen(app * e); + app * mk_strlen(expr * e); app * mk_concat(app * e1, app * e2); app * mk_internal_xor_var(); @@ -82,6 +82,16 @@ namespace smt { void handle_equality(expr * lhs, expr * rhs); expr * get_eqc_value(expr * n, bool & hasEqcValue); + bool in_same_eqc(expr * n1, expr * n2); + + bool can_two_nodes_eq(expr * n1, expr * n2); + bool can_concat_eq_str(expr * concat, std::string str); + bool can_concat_eq_concat(expr * concat1, expr * concat2); + + void get_nodes_in_concat(expr * node, ptr_vector & nodeList); + expr * simplify_concat(expr * node); + + void simplify_parent(expr * nn, expr * eq_str); void simplify_concat_equality(expr * lhs, expr * rhs); void solve_concat_eq_str(expr * concat, expr * str); From a62d15403e434fe5358523b52cfc1442bad11917 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 29 Sep 2015 22:31:11 -0400 Subject: [PATCH 036/562] start simplify_concat_eq(), WIP but some cases OK also fix model generation for concats and nested concats --- src/smt/theory_str.cpp | 116 +++++++++++++++++++++++++++++++++++++---- src/smt/theory_str.h | 4 +- 2 files changed, 108 insertions(+), 12 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 92edbc22b..3f8de3d6f 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -131,14 +131,60 @@ app * theory_str::mk_strlen(expr * e) { } } -app * theory_str::mk_concat(app * e1, app * e2) { +/* + * Returns the simplified concatenation of two expressions, + * where either both expressions are constant strings + * or one expression is the empty string. + * If this precondition does not hold, the function returns NULL. + * (note: this function was strTheory::Concat()) + */ +expr * theory_str::mk_concat_const_str(expr * n1, expr * n2) { + bool n1HasEqcValue = false; + bool n2HasEqcValue = false; + expr * v1 = get_eqc_value(n1, n1HasEqcValue); + expr * v2 = get_eqc_value(n2, n2HasEqcValue); + if (n1HasEqcValue && n2HasEqcValue) { + const char * n1_str_tmp; + m_strutil.is_string(v1, & n1_str_tmp); + std::string n1_str(n1_str_tmp); + const char * n2_str_tmp; + m_strutil.is_string(v2, & n2_str_tmp); + std::string n2_str(n2_str_tmp); + std::string result = n1_str + n2_str; + return m_strutil.mk_string(result); + } else if (n1HasEqcValue && !n2HasEqcValue) { + const char * n1_str_tmp; + m_strutil.is_string(v1, & n1_str_tmp); + if (strcmp(n1_str_tmp, "") == 0) { + return n2; + } + } else if (!n1HasEqcValue && n2HasEqcValue) { + const char * n2_str_tmp; + m_strutil.is_string(v2, & n2_str_tmp); + if (strcmp(n2_str_tmp, "") == 0) { + return n1; + } + } + return NULL; +} + +expr * theory_str::mk_concat(expr * n1, expr * n2) { ast_manager & m = get_manager(); - if (e1 == NULL || e2 == NULL) { + if (n1 == NULL || n2 == NULL) { m.raise_exception("strings to be concatenated cannot be NULL"); } - // TODO there's a *TON* of missing code here from strTheory::mk_concat() - expr * args[2] = {e1, e2}; - return get_manager().mk_app(get_id(), OP_STRCAT, 0, 0, 2, args); + bool n1HasEqcValue = false; + bool n2HasEqcValue = false; + n1 = get_eqc_value(n1, n1HasEqcValue); + n2 = get_eqc_value(n2, n2HasEqcValue); + if (n1HasEqcValue && n2HasEqcValue) { + return mk_concat_const_str(n1, n2); + } else { + // TODO there's a *TON* of missing code here from strTheory::mk_concat() + // if all else fails, just build the application AST + expr * args[2] = {n1, n2}; + return get_manager().mk_app(get_id(), OP_STRCAT, 0, 0, 2, args); + } } bool theory_str::can_propagate() { @@ -424,11 +470,11 @@ expr * theory_str::simplify_concat(expr * node) { // no simplification possible return node; } else { - app * resultAst = m_strutil.mk_string(""); + expr * resultAst = m_strutil.mk_string(""); for (unsigned i = 0; i < argVec.size(); ++i) { bool vArgHasEqcValue = false; expr * vArg = get_eqc_value(argVec[i], vArgHasEqcValue); - resultAst = mk_concat(to_app(resultAst), to_app(vArg)); + resultAst = mk_concat(resultAst, vArg); } TRACE("t_str_detail", tout << mk_ismt2_pp(node, m) << " is simplified to " << mk_ismt2_pp(resultAst, m) << std::endl;); @@ -821,7 +867,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } else { implyL1 = m.mk_and(item1[0], item1[1]); } - newConcat = mk_concat(to_app(arg1), to_app(arg2)); + newConcat = mk_concat(arg1, arg2); if (newConcat != str) { expr_ref implyR1(ctx.mk_eq_atom(concat, newConcat), m); assert_implication(implyL1, implyR1); @@ -1308,6 +1354,52 @@ void theory_str::init_model(model_generator & mg) { mg.register_factory(m_factory); } +/* + * Helper function for mk_value(). + * Attempts to resolve the expression 'n' to a string constant. + * Stronger than get_eqc_value() in that it will perform recursive descent + * through every subexpression and attempt to resolve those to concrete values as well. + * Returns the concrete value obtained from this process, + * guaranteed to satisfy m_strutil.is_string(), + * if one could be obtained, + * or else returns NULL if no concrete value was derived. + */ +app * theory_str::mk_value_helper(app * n) { + if (m_strutil.is_string(n)) { + return n; + } else if (is_concat(n)) { + // recursively call this function on each argument + SASSERT(n->get_num_args() == 2); + expr * a0 = n->get_arg(0); + expr * a1 = n->get_arg(1); + + app * a0_conststr = mk_value_helper(to_app(a0)); + app * a1_conststr = mk_value_helper(to_app(a1)); + + if (a0_conststr != NULL && a1_conststr != NULL) { + const char * a0_str = 0; + m_strutil.is_string(a0_conststr, &a0_str); + + const char * a1_str = 0; + m_strutil.is_string(a1_conststr, &a1_str); + + std::string a0_s(a0_str); + std::string a1_s(a1_str); + std::string result = a0_s + a1_s; + return m_strutil.mk_string(result); + } + } + // fallback path + // try to find some constant string, anything, in the equivalence class of n + bool hasEqc = false; + expr * n_eqc = get_eqc_value(n, hasEqc); + if (hasEqc) { + return to_app(n_eqc); + } else { + return NULL; + } +} + model_value_proc * theory_str::mk_value(enode * n, model_generator & mg) { TRACE("t_str", tout << "mk_value for: " << mk_ismt2_pp(n->get_owner(), get_manager()) << " (sort " << mk_ismt2_pp(get_manager().get_sort(n->get_owner()), get_manager()) << ")" << std::endl;); @@ -1319,10 +1411,12 @@ model_value_proc * theory_str::mk_value(enode * n, model_generator & mg) { // If the owner is not internalized, it doesn't have an enode associated. SASSERT(ctx.e_internalized(owner)); - if (m_strutil.is_string(owner)) { - return alloc(expr_wrapper_proc, owner); + app * val = mk_value_helper(owner); + if (val != NULL) { + return alloc(expr_wrapper_proc, val); + } else { + m.raise_exception("failed to find concrete value"); return NULL; } - NOT_IMPLEMENTED_YET(); // TODO } void theory_str::finalize_model(model_generator & mg) {} diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index c3641016f..daa5656bb 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -66,7 +66,8 @@ namespace smt { void assert_implication(expr * premise, expr * conclusion); app * mk_strlen(expr * e); - app * mk_concat(app * e1, app * e2); + expr * mk_concat(expr * n1, expr * n2); + expr * mk_concat_const_str(expr * n1, expr * n2); app * mk_internal_xor_var(); @@ -81,6 +82,7 @@ namespace smt { void set_up_axioms(expr * ex); void handle_equality(expr * lhs, expr * rhs); + app * mk_value_helper(app * n); expr * get_eqc_value(expr * n, bool & hasEqcValue); bool in_same_eqc(expr * n1, expr * n2); From ed7b343822e2829f63c9eb2378b454b333d01d18 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 30 Sep 2015 05:15:14 -0400 Subject: [PATCH 037/562] detect and process concat eq type 1 (WIP UNTESTED) --- src/smt/theory_str.cpp | 462 ++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 37 +++- 2 files changed, 495 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 3f8de3d6f..fc4548f7a 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -28,7 +28,10 @@ theory_str::theory_str(ast_manager & m): search_started(false), m_autil(m), m_strutil(m), - tmpXorVarCount(0) + tmpStringVarCount(0), + tmpXorVarCount(0), + avoidLoopCut(true), + loopDetected(false) { } @@ -101,6 +104,122 @@ bool theory_str::internalize_term(app * term) { return true; } +static void cut_vars_map_copy(std::map & dest, std::map & src) { + std::map::iterator itor = src.begin(); + for (; itor != src.end(); itor++) { + dest[itor->first] = 1; + } +} + +/* +bool hasSelfCut(Z3_ast n1, Z3_ast n2) { + if (cut_VARMap.find(n1) == cut_VARMap.end()) + return false; + + if (cut_VARMap.find(n2) == cut_VARMap.end()) + return false; + + if (cut_VARMap[n1].empty() || cut_VARMap[n2].empty()) + return false; + + std::map::iterator itor = cut_VARMap[n1].top()->vars.begin(); + for (; itor != cut_VARMap[n1].top()->vars.end(); itor++) { + if (cut_VARMap[n2].top()->vars.find(itor->first) != cut_VARMap[n2].top()->vars.end()) + return true; + } + return false; +} +*/ + +bool theory_str::has_self_cut(expr * n1, expr * n2) { + if (cut_var_map.find(n1) == cut_var_map.end()) { + return false; + } + if (cut_var_map.find(n2) == cut_var_map.end()) { + return false; + } + if (cut_var_map[n1].empty() || cut_var_map[n2].empty()) { + return false; + } + + std::map::iterator itor = cut_var_map[n1].top()->vars.begin(); + for (; itor != cut_var_map[n1].top()->vars.end(); ++itor) { + if (cut_var_map[n2].top()->vars.find(itor->first) != cut_var_map[n2].top()->vars.end()) { + return true; + } + } + return false; +} + +void theory_str::add_cut_info_one_node(expr * baseNode, int slevel, expr * node) { + if (cut_var_map.find(baseNode) == cut_var_map.end()) { + T_cut * varInfo = alloc(T_cut); + varInfo->level = slevel; + varInfo->vars[node] = 1; + cut_var_map[baseNode].push(varInfo); + } else { + if (cut_var_map[baseNode].empty()) { + T_cut * varInfo = alloc(T_cut); + varInfo->level = slevel; + varInfo->vars[node] = 1; + cut_var_map[baseNode].push(varInfo); + } else { + if (cut_var_map[baseNode].top()->level < slevel) { + T_cut * varInfo = alloc(T_cut); + varInfo->level = slevel; + cut_vars_map_copy(varInfo->vars, cut_var_map[baseNode].top()->vars); + varInfo->vars[node] = 1; + cut_var_map[baseNode].push(varInfo); + } else if (cut_var_map[baseNode].top()->level == slevel) { + cut_var_map[baseNode].top()->vars[node] = 1; + } else { + get_manager().raise_exception("entered illegal state during add_cut_info_one_node()"); + } + } + } +} + +void theory_str::add_cut_info_merge(expr * destNode, int slevel, expr * srcNode) { + if (cut_var_map.find(srcNode) == cut_var_map.end()) { + get_manager().raise_exception("illegal state in add_cut_info_merge(): cut_var_map doesn't contain srcNode"); + } + + if (cut_var_map[srcNode].empty()) { + get_manager().raise_exception("illegal state in add_cut_info_merge(): cut_var_map[srcNode] is empty"); + } + + if (cut_var_map.find(destNode) == cut_var_map.end()) { + T_cut * varInfo = alloc(T_cut); + varInfo->level = slevel; + cut_vars_map_copy(varInfo->vars, cut_var_map[srcNode].top()->vars); + cut_var_map[destNode].push(varInfo); + } else { + if (cut_var_map[destNode].empty() || cut_var_map[destNode].top()->level < slevel) { + T_cut * varInfo = alloc(T_cut); + varInfo->level = slevel; + cut_vars_map_copy(varInfo->vars, cut_var_map[destNode].top()->vars); + cut_vars_map_copy(varInfo->vars, cut_var_map[srcNode].top()->vars); + cut_var_map[destNode].push(varInfo); + } else if (cut_var_map[destNode].top()->level == slevel) { + cut_vars_map_copy(cut_var_map[destNode].top()->vars, cut_var_map[srcNode].top()->vars); + } else { + get_manager().raise_exception("illegal state in add_cut_info_merge(): inconsistent slevels"); + } + } +} + +void theory_str::check_and_init_cut_var(expr * node) { + if (cut_var_map.find(node) != cut_var_map.end()) { + return; + } else if (!m_strutil.is_string(node)) { + add_cut_info_one_node(node, -1, node); + } +} + +app * theory_str::mk_int(int n) { + return m_autil.mk_numeral(rational(n), true); +} + app * theory_str::mk_internal_xor_var() { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -119,6 +238,49 @@ app * theory_str::mk_internal_xor_var() { return a; } +/* + Z3_context ctx = Z3_theory_get_context(t); + PATheoryData * td = (PATheoryData *) Z3_theory_get_ext_data(t); + std::stringstream ss; + ss << tmpStringVarCount; + tmpStringVarCount++; + std::string name = "$$_str" + ss.str(); + Z3_ast varAst = mk_var(ctx, name.c_str(), td->String); + nonEmptyStrVarAxiom(t, varAst, __LINE__); + return varAst; +*/ + +app * theory_str::mk_nonempty_str_var() { + context & ctx = get_context(); + ast_manager & m = get_manager(); + std::stringstream ss; + ss << tmpStringVarCount; + tmpStringVarCount++; + std::string name = "$$_str" + ss.str(); + sort * string_sort = m.mk_sort(m_strutil.get_family_id(), STRING_SORT); + char * new_buffer = alloc_svect(char, name.length() + 1); + strcpy(new_buffer, name.c_str()); + symbol sym(new_buffer); + + app* a = m.mk_const(m.mk_const_decl(sym, string_sort)); + // assert a variation of the basic string axioms that ensures this string is nonempty + { + // build LHS + expr_ref len_str(m); + len_str = mk_strlen(a); + SASSERT(len_str); + // build RHS + expr_ref zero(m); + zero = m_autil.mk_numeral(rational(0), true); + SASSERT(zero); + // build LHS > RHS and assert + app * lhs_gt_rhs = m_autil.mk_gt(len_str, zero); + SASSERT(lhs_gt_rhs); + assert_axiom(lhs_gt_rhs); + } + return a; +} + app * theory_str::mk_strlen(expr * e) { /*if (m_strutil.is_string(e)) {*/ if (false) { const char * strval = 0; @@ -372,7 +534,7 @@ void theory_str::reset_eh() { m_basicstr_axiom_todo.reset(); m_str_eq_todo.reset(); m_concat_axiom_todo.reset(); - pop_scope_eh(0); + pop_scope_eh(get_context().get_scope_level()); } /* @@ -670,9 +832,289 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { } // start to split both concats + check_and_init_cut_var(v1_arg0); + check_and_init_cut_var(v1_arg1); + check_and_init_cut_var(v2_arg0); + check_and_init_cut_var(v2_arg1); + //************************************************************* + // case 1: concat(x, y) = concat(m, n) + //************************************************************* + if (is_concat_eq_type1(new_nn1, new_nn2)) { + process_concat_eq_type1(new_nn1, new_nn2); + return; + } + + //************************************************************* + // case 2: concat(x, y) = concat(m, "str") + //************************************************************* + if (is_concat_eq_type2(new_nn1, new_nn2)) { + process_concat_eq_type2(new_nn1, new_nn2); + return; + } + + //************************************************************* + // case 3: concat(x, y) = concat("str", n) + //************************************************************* + if (is_concat_eq_type3(new_nn1, new_nn2)) { + process_concat_eq_type3(new_nn1, new_nn2); + return; + } + + //************************************************************* + // case 4: concat("str1", y) = concat("str2", n) + //************************************************************* + if (is_concat_eq_type4(new_nn1, new_nn2)) { + process_concat_eq_type4(new_nn1, new_nn2); + return; + } + + //************************************************************* + // case 5: concat(x, "str1") = concat(m, "str2") + //************************************************************* + if (is_concat_eq_type5(new_nn1, new_nn2)) { + process_concat_eq_type5(new_nn1, new_nn2); + return; + } + //************************************************************* + // case 6: concat("str1", y) = concat(m, "str2") + //************************************************************* + if (is_concat_eq_type6(new_nn1, new_nn2)) { + process_concat_eq_type6(new_nn1, new_nn2); + return; + } + +} + +bool theory_str::is_concat_eq_type1(expr * concatAst1, expr * concatAst2) { + expr * x = to_app(concatAst1)->get_arg(0); + expr * y = to_app(concatAst1)->get_arg(1); + expr * m = to_app(concatAst2)->get_arg(0); + expr * n = to_app(concatAst2)->get_arg(1); + + if (!m_strutil.is_string(x) && !m_strutil.is_string(y) && !m_strutil.is_string(m) && !m_strutil.is_string(n)) { + return true; + } else { + return false; + } +} + +void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { + ast_manager & mgr = get_manager(); + context & ctx = get_context(); + TRACE("t_str_detail", tout << "process_concat_eq TYPE 1" << std::endl + << "concatAst1 = " << mk_ismt2_pp(concatAst1, m) << std::endl + << "concatAst2 = " << mk_ismt2_pp(concatAst2, m) << std::endl; + ); + + if (!is_concat(to_app(concatAst1))) { + TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); + return; + } + if (!is_concat(to_app(concatAst2))) { + TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); + return; + } + expr * x = to_app(concatAst1)->get_arg(0); + expr * y = to_app(concatAst1)->get_arg(1); + expr * m = to_app(concatAst2)->get_arg(0); + expr * n = to_app(concatAst2)->get_arg(1); + + /* TODO query the integer theory: + int x_len = getLenValue(t, x); + int y_len = getLenValue(t, y); + int m_len = getLenValue(t, m); + int n_len = getLenValue(t, n); + */ + int x_len = -1; + int y_len = -1; + int m_len = -1; + int n_len = -1; + + int splitType = -1; + if (x_len != -1 && m_len != -1) { + if (x_len < m_len) + splitType = 0; + else if (x_len == m_len) + splitType = 1; + else + splitType = 2; + } + + if (splitType == -1 && y_len != -1 && n_len != -1) { + if (y_len > n_len) + splitType = 0; + else if (y_len == n_len) + splitType = 1; + else + splitType = 2; + } + + TRACE("t_str_detail", tout << "split type " << splitType << std::endl;); + + expr * t1 = NULL; + expr * t2 = NULL; + expr * xorFlag = NULL; + + std::pair key1(concatAst1, concatAst2); + std::pair key2(concatAst2, concatAst1); + + if (varForBreakConcat.find(key1) == varForBreakConcat.end() && varForBreakConcat.find(key2) == varForBreakConcat.end()) { + t1 = mk_nonempty_str_var(); + t2 = mk_nonempty_str_var(); + xorFlag = mk_internal_xor_var(); + check_and_init_cut_var(t1); + check_and_init_cut_var(t2); + varForBreakConcat[key1][0] = t1; + varForBreakConcat[key1][1] = t2; + varForBreakConcat[key1][2] = xorFlag; + } else { + // match found + if (varForBreakConcat.find(key1) != varForBreakConcat.end()) { + t1 = varForBreakConcat[key1][0]; + t2 = varForBreakConcat[key1][1]; + xorFlag = varForBreakConcat[key1][2]; + } else { + t1 = varForBreakConcat[key2][0]; + t2 = varForBreakConcat[key2][1]; + xorFlag = varForBreakConcat[key2][2]; + } + } + + // For split types 0 through 2, we can get away with providing + // fewer split options since more length information is available. + if (splitType == 0) { + NOT_IMPLEMENTED_YET(); // TODO + } else if (splitType == 1) { + NOT_IMPLEMENTED_YET(); // TODO + } else if (splitType == 2) { + NOT_IMPLEMENTED_YET(); // TODO + } else if (splitType == -1) { + // Here we don't really have a choice. We have no length information at all... + expr ** or_item = alloc_svect(expr*, 3); + expr ** and_item = alloc_svect(expr*, 20); + int option = 0; + int pos = 1; + + // break option 1: m cuts y + // len(x) < len(m) || len(y) > len(n) + if (!avoidLoopCut || !has_self_cut(m, y)) { + // break down option 1-1 + expr * x_t1 = mk_concat(x, t1); + expr * t1_n = mk_concat(t1, n); + or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(m, x_t1)); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(y, t1_n)); + + expr_ref x_plus_t1(m_autil.mk_add(mk_strlen(x), mk_strlen(t1)), mgr); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(m), x_plus_t1)); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], m_autil.mk_gt(mk_strlen(m), mk_strlen(x))); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], m_autil.mk_gt(mk_strlen(y), mk_strlen(n))); + + option++; + + add_cut_info_merge(t1, ctx.get_scope_level(), m); + add_cut_info_merge(t1, ctx.get_scope_level(), y); + } else { + loopDetected = true; + TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + // TODO printCutVar(m, y); + } + + // break option 2: + // x = m || y = n + if (!avoidLoopCut || !has_self_cut(x, n)) { + // break down option 1-2 + expr * m_t2 = mk_concat(m, t2); + expr * t2_y = mk_concat(t2, y); + or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(x, m_t2)); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(n, t2_y)); + + + expr_ref m_plus_t2(m_autil.mk_add(mk_strlen(m), mk_strlen(t2)), mgr); + + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(x), m_plus_t2)); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(x), mk_strlen(m))); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(n), mk_strlen(y))); + + + option++; + + add_cut_info_merge(t2, sLevel, x); + add_cut_info_merge(t2, sLevel, n); + } else { + loopDetected = true; + TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + // TODO printCutVar(x, n); + } + + if (can_two_nodes_eq(x, m) && can_two_nodes_eq(y, n)) { + or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(x, m)); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(y, n)); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(x), mk_strlen(m))); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(y), mk_strlen(n))); + ++option; + } + + if (option > 0) { + if (option == 1) { + and_item[0] = or_item[0]; + } else { + and_item[0] = mgr.mk_or(option, or_item); + } + expr_ref premise(ctx.mk_eq_atom(concatAst1, concatAst2), m); + expr_ref conclusion(mgr.mk_and(pos, and_item), m); + assert_implication(premise, conclusion); + } else { + TRACE("t_str", tout << "STOP: no split option found for two EQ concats." << std::endl;); + } + } // (splitType == -1) +} + +bool theory_str::is_concat_eq_type2(expr * concatAst1, expr * concatAst2) { // TODO - NOT_IMPLEMENTED_YET(); + return false; +} + +void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { + +} + +bool theory_str::is_concat_eq_type3(expr * concatAst1, expr * concatAst2) { + // TODO + return false; +} + +void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { + +} + +bool theory_str::is_concat_eq_type4(expr * concatAst1, expr * concatAst2) { + // TODO + return false; +} + +void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { + +} + +bool theory_str::is_concat_eq_type5(expr * concatAst1, expr * concatAst2) { + // TODO + return false; +} + +void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { + +} + +bool theory_str::is_concat_eq_type6(expr * concatAst1, expr * concatAst2) { + // TODO + return false; +} + +void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } @@ -1327,6 +1769,20 @@ void theory_str::push_scope_eh() { void theory_str::pop_scope_eh(unsigned num_scopes) { TRACE("t_str", tout << "pop " << num_scopes << std::endl;); + context & ctx = get_context(); + unsigned sLevel = ctx.get_scope_level(); + std::map >::iterator varItor = cut_var_map.begin(); + while (varItor != cut_var_map.end()) { + while ((varItor->second.size() > 0) && (varItor->second.top()->level != 0) && (varItor->second.top()->level >= sLevel)) { + T_cut * aCut = varItor->second.top(); + varItor->second.pop(); + dealloc(aCut); + } + if (varItor->second.size() == 0) { + cut_var_map.erase(varItor); + } + ++varItor; + } } final_check_status theory_str::final_check_eh() { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index daa5656bb..b66eef4ad 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -47,7 +47,15 @@ namespace smt { }; class theory_str : public theory { - // TODO + struct T_cut + { + int level; + std::map vars; + + T_cut() { + level = -100; + } + }; protected: bool search_started; arith_util m_autil; @@ -59,8 +67,13 @@ namespace smt { svector > m_str_eq_todo; ptr_vector m_concat_axiom_todo; + int tmpStringVarCount; int tmpXorVarCount; std::map, std::map > varForBreakConcat; + + bool avoidLoopCut = true; + bool loopDetected = false; + std::map > cut_var_map; protected: void assert_axiom(expr * e); void assert_implication(expr * premise, expr * conclusion); @@ -69,6 +82,14 @@ namespace smt { expr * mk_concat(expr * n1, expr * n2); expr * mk_concat_const_str(expr * n1, expr * n2); + app * mk_int(int n); + + void check_and_init_cut_var(expr * node); + void add_cut_info_one_node(expr * baseNode, int slevel, expr * node); + void add_cut_info_merge(expr * destNode, int slevel, expr * srcNode); + bool has_self_cut(expr * n1, expr * n2); + + app * mk_nonempty_str_var(); app * mk_internal_xor_var(); bool is_concat(app const * a) const { return a->is_app_of(get_id(), OP_STRCAT); } @@ -98,6 +119,20 @@ namespace smt { void simplify_concat_equality(expr * lhs, expr * rhs); void solve_concat_eq_str(expr * concat, expr * str); + bool is_concat_eq_type1(expr * concatAst1, expr * concatAst2); + bool is_concat_eq_type2(expr * concatAst1, expr * concatAst2); + bool is_concat_eq_type3(expr * concatAst1, expr * concatAst2); + bool is_concat_eq_type4(expr * concatAst1, expr * concatAst2); + bool is_concat_eq_type5(expr * concatAst1, expr * concatAst2); + bool is_concat_eq_type6(expr * concatAst1, expr * concatAst2); + + void process_concat_eq_type1(expr * concatAst1, expr * concatAst2); + void process_concat_eq_type2(expr * concatAst1, expr * concatAst2); + void process_concat_eq_type3(expr * concatAst1, expr * concatAst2); + void process_concat_eq_type4(expr * concatAst1, expr * concatAst2); + void process_concat_eq_type5(expr * concatAst1, expr * concatAst2); + void process_concat_eq_type6(expr * concatAst1, expr * concatAst2); + bool new_eq_check(expr * lhs, expr * rhs); void group_terms_by_eqc(expr * n, std::set & concats, std::set & vars, std::set & consts); public: From e2901fff1ea1394bdba0a892e60d06263ff38db4 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 30 Sep 2015 05:21:16 -0400 Subject: [PATCH 038/562] fix compilation errors --- src/smt/theory_str.cpp | 22 +++++++++++----------- src/smt/theory_str.h | 5 +++-- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index fc4548f7a..3458ec60c 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -903,8 +903,8 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { ast_manager & mgr = get_manager(); context & ctx = get_context(); TRACE("t_str_detail", tout << "process_concat_eq TYPE 1" << std::endl - << "concatAst1 = " << mk_ismt2_pp(concatAst1, m) << std::endl - << "concatAst2 = " << mk_ismt2_pp(concatAst2, m) << std::endl; + << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl + << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; ); if (!is_concat(to_app(concatAst1))) { @@ -1041,8 +1041,8 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { option++; - add_cut_info_merge(t2, sLevel, x); - add_cut_info_merge(t2, sLevel, n); + add_cut_info_merge(t2, ctx.get_scope_level(), x); + add_cut_info_merge(t2, ctx.get_scope_level(), n); } else { loopDetected = true; TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); @@ -1064,8 +1064,8 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } else { and_item[0] = mgr.mk_or(option, or_item); } - expr_ref premise(ctx.mk_eq_atom(concatAst1, concatAst2), m); - expr_ref conclusion(mgr.mk_and(pos, and_item), m); + expr_ref premise(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); + expr_ref conclusion(mgr.mk_and(pos, and_item), mgr); assert_implication(premise, conclusion); } else { TRACE("t_str", tout << "STOP: no split option found for two EQ concats." << std::endl;); @@ -1078,7 +1078,7 @@ bool theory_str::is_concat_eq_type2(expr * concatAst1, expr * concatAst2) { return false; } -void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { +void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { } @@ -1087,7 +1087,7 @@ bool theory_str::is_concat_eq_type3(expr * concatAst1, expr * concatAst2) { return false; } -void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { +void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { } @@ -1096,7 +1096,7 @@ bool theory_str::is_concat_eq_type4(expr * concatAst1, expr * concatAst2) { return false; } -void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { +void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { } @@ -1105,7 +1105,7 @@ bool theory_str::is_concat_eq_type5(expr * concatAst1, expr * concatAst2) { return false; } -void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { +void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { } @@ -1114,7 +1114,7 @@ bool theory_str::is_concat_eq_type6(expr * concatAst1, expr * concatAst2) { return false; } -void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { +void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index b66eef4ad..5d0ec96db 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -24,6 +24,7 @@ Revision History: #include"smt_model_generator.h" #include"arith_decl_plugin.h" #include +#include namespace smt { @@ -71,8 +72,8 @@ namespace smt { int tmpXorVarCount; std::map, std::map > varForBreakConcat; - bool avoidLoopCut = true; - bool loopDetected = false; + bool avoidLoopCut; + bool loopDetected; std::map > cut_var_map; protected: void assert_axiom(expr * e); From ecb2116927eecd15ff2898cb03b42ffe77cfc8e9 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 30 Sep 2015 05:23:22 -0400 Subject: [PATCH 039/562] fix memory corruption bug caused by invalid use of delete[] --- src/smt/theory_str.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 3458ec60c..071f07619 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1503,8 +1503,6 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } assert_implication(implyL, implyR1); } - delete[] xor_items; - delete[] and_items; } /* (arg1Len != 1 || arg2Len != 1) */ } /* if (Concat(arg1, arg2) == NULL) */ } From 5189c24d42c1fa8648bee09f2548e127d05ef294 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 30 Sep 2015 05:45:16 -0400 Subject: [PATCH 040/562] fix theory of arithmetic complaints about wanting to write A > B "what could possibly go wrong?" --- src/smt/theory_str.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 071f07619..440aaeb9f 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -274,7 +274,8 @@ app * theory_str::mk_nonempty_str_var() { zero = m_autil.mk_numeral(rational(0), true); SASSERT(zero); // build LHS > RHS and assert - app * lhs_gt_rhs = m_autil.mk_gt(len_str, zero); + // we have to build !(LHS <= RHS) instead + app * lhs_gt_rhs = m.mk_not(m_autil.mk_le(len_str, zero)); SASSERT(lhs_gt_rhs); assert_axiom(lhs_gt_rhs); } @@ -1008,8 +1009,18 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { expr_ref x_plus_t1(m_autil.mk_add(mk_strlen(x), mk_strlen(t1)), mgr); and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(m), x_plus_t1)); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], m_autil.mk_gt(mk_strlen(m), mk_strlen(x))); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], m_autil.mk_gt(mk_strlen(y), mk_strlen(n))); + // TODO these are crashing the solvers because the integer theory + // expects a constant on the right-hand side. + // The things we want to assert here are len(m) > len(x) and len(y) > len(n). + // We rewrite A > B as A-B > 0 and then as not(A-B <= 0), + // and then, *because we aren't allowed to use subtraction*, + // as not(A + -1*B <= 0) + and_item[pos++] = ctx.mk_eq_atom(or_item[option], + mgr.mk_not(m_autil.mk_le(m_autil.mk_add(mk_strlen(m), + m_autil.mk_mul(mk_int(-1), mk_strlen(x))), mk_int(0))) ); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], + mgr.mk_not(m_autil.mk_le(m_autil.mk_add(mk_strlen(y), + m_autil.mk_mul(mk_int(-1), mk_strlen(n))), mk_int(0))) ); option++; From f8c13792a355f5af3505c77e6f9cf883794910ad Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 30 Sep 2015 09:45:00 -0400 Subject: [PATCH 041/562] mark the position of the bug I found so I can recall it later in process_concat_eq_type1() line 1048 --- src/smt/theory_str.cpp | 35 +++++++++++++++++++++++------------ src/smt/theory_str.h | 2 ++ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 440aaeb9f..aad15bec8 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1045,6 +1045,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { expr_ref m_plus_t2(m_autil.mk_add(mk_strlen(m), mk_strlen(t2)), mgr); + // TODO here is the bug: these EQs should be GTs and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(x), m_plus_t2)); and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(x), mk_strlen(m))); and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(n), mk_strlen(y))); @@ -1724,6 +1725,7 @@ void theory_str::init_search_eh() { * This is done to find equalities between terms, etc. that we otherwise * might not get a chance to see. */ + /* expr_ref_vector assignments(m); ctx.get_assignments(assignments); for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { @@ -1745,6 +1747,7 @@ void theory_str::init_search_eh() { << ": expr ignored" << std::endl;); } } + */ TRACE("t_str", tout << "search started" << std::endl;); search_started = true; @@ -1755,12 +1758,16 @@ void theory_str::new_eq_eh(theory_var x, theory_var y) { TRACE("t_str", tout << "new eq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " = " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); handle_equality(get_enode(x)->get_owner(), get_enode(y)->get_owner()); + + TRACE("t_str_detail", dump_assignments();); } void theory_str::new_diseq_eh(theory_var x, theory_var y) { //TRACE("t_str_detail", tout << "new diseq: v#" << x << " != v#" << y << std::endl;); TRACE("t_str", tout << "new diseq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " != " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); + + TRACE("t_str_detail", dump_assignments();); } void theory_str::relevant_eh(app * n) { @@ -1779,7 +1786,7 @@ void theory_str::push_scope_eh() { void theory_str::pop_scope_eh(unsigned num_scopes) { TRACE("t_str", tout << "pop " << num_scopes << std::endl;); context & ctx = get_context(); - unsigned sLevel = ctx.get_scope_level(); + int sLevel = ctx.get_scope_level(); std::map >::iterator varItor = cut_var_map.begin(); while (varItor != cut_var_map.end()) { while ((varItor->second.size() > 0) && (varItor->second.top()->level != 0) && (varItor->second.top()->level >= sLevel)) { @@ -1794,21 +1801,25 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { } } +void theory_str::dump_assignments() { + ast_manager & m = get_manager(); + context & ctx = get_context(); + TRACE("t_str_detail", + tout << "dumping all assignments:" << std::endl; + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { + expr * ex = *i; + tout << mk_ismt2_pp(ex, m) << std::endl; + } + ); +} + final_check_status theory_str::final_check_eh() { - ast_manager & m = get_manager(); - context & ctx = get_context(); // TODO TRACE("t_str", tout << "final check" << std::endl;); - TRACE("t_str_detail", - tout << "dumping all assignments:" << std::endl; - expr_ref_vector assignments(m); - ctx.get_assignments(assignments); - for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { - expr * ex = *i; - tout << mk_ismt2_pp(ex, m) << std::endl; - } - ); + TRACE("t_str_detail", dump_assignments();); return FC_DONE; } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 5d0ec96db..930c8e9c8 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -136,6 +136,8 @@ namespace smt { bool new_eq_check(expr * lhs, expr * rhs); void group_terms_by_eqc(expr * n, std::set & concats, std::set & vars, std::set & consts); + + void dump_assignments(); public: theory_str(ast_manager & m); virtual ~theory_str(); From fb5f3cbc136651a0c4113f7a857d84d3aad13dba Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 30 Sep 2015 11:41:55 -0400 Subject: [PATCH 042/562] fix greater-than bug now we just have to tweak model gen for internal variables --- src/smt/theory_str.cpp | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index aad15bec8..7514f8a23 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1016,11 +1016,13 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { // and then, *because we aren't allowed to use subtraction*, // as not(A + -1*B <= 0) and_item[pos++] = ctx.mk_eq_atom(or_item[option], - mgr.mk_not(m_autil.mk_le(m_autil.mk_add(mk_strlen(m), - m_autil.mk_mul(mk_int(-1), mk_strlen(x))), mk_int(0))) ); + mgr.mk_not(m_autil.mk_le( + m_autil.mk_add(mk_strlen(m), m_autil.mk_mul(mk_int(-1), mk_strlen(x))), + mk_int(0))) ); and_item[pos++] = ctx.mk_eq_atom(or_item[option], - mgr.mk_not(m_autil.mk_le(m_autil.mk_add(mk_strlen(y), - m_autil.mk_mul(mk_int(-1), mk_strlen(n))), mk_int(0))) ); + mgr.mk_not(m_autil.mk_le( + m_autil.mk_add(mk_strlen(y),m_autil.mk_mul(mk_int(-1), mk_strlen(n))), + mk_int(0))) ); option++; @@ -1045,10 +1047,16 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { expr_ref m_plus_t2(m_autil.mk_add(mk_strlen(m), mk_strlen(t2)), mgr); - // TODO here is the bug: these EQs should be GTs and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(x), m_plus_t2)); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(x), mk_strlen(m))); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(n), mk_strlen(y))); + // want len(x) > len(m) and len(n) > len(y) + and_item[pos++] = ctx.mk_eq_atom(or_item[option], + mgr.mk_not(m_autil.mk_le( + m_autil.mk_add(mk_strlen(x), m_autil.mk_mul(mk_int(-1), mk_strlen(m))), + mk_int(0))) ); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], + mgr.mk_not(m_autil.mk_le( + m_autil.mk_add(mk_strlen(n), m_autil.mk_mul(mk_int(-1), mk_strlen(y))), + mk_int(0))) ); option++; @@ -1759,7 +1767,7 @@ void theory_str::new_eq_eh(theory_var x, theory_var y) { mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); handle_equality(get_enode(x)->get_owner(), get_enode(y)->get_owner()); - TRACE("t_str_detail", dump_assignments();); + TRACE("t_str_dump_assign", dump_assignments();); } void theory_str::new_diseq_eh(theory_var x, theory_var y) { @@ -1767,7 +1775,7 @@ void theory_str::new_diseq_eh(theory_var x, theory_var y) { TRACE("t_str", tout << "new diseq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " != " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); - TRACE("t_str_detail", dump_assignments();); + TRACE("t_str_dump_assign", dump_assignments();); } void theory_str::relevant_eh(app * n) { From bdf755156cd761c1247d83cbb034306455b45a28 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 1 Oct 2015 20:31:40 -0400 Subject: [PATCH 043/562] fix model generation: don't build interpretations for Length() --- src/ast/str_decl_plugin.cpp | 16 +++++++++- src/ast/str_decl_plugin.h | 4 +++ src/smt/theory_str.cpp | 60 ++++++++++++++++++++++++++++++++----- src/smt/theory_str.h | 9 +++++- 4 files changed, 80 insertions(+), 9 deletions(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index c72a5dbc2..5589db56c 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -64,7 +64,7 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { MK_OP(m_concat_decl, "Concat", OP_STRCAT, s); - m_length_decl = m->mk_func_decl(symbol("Length"), s, i); m_manager->inc_ref(m_length_decl); + m_length_decl = m->mk_func_decl(symbol("Length"), s, i, func_decl_info(id, OP_STRLEN)); m_manager->inc_ref(m_length_decl); } decl_plugin * str_decl_plugin::mk_fresh() { @@ -120,6 +120,20 @@ app * str_decl_plugin::mk_string(const char * val) { return mk_string(key); } +app * str_decl_plugin::mk_fresh_string() { + // cheating. + // take the longest string in the cache, append the letter "A", and call it fresh. + std::string longestString = ""; + std::map::iterator it = string_cache.begin(); + for (; it != string_cache.end(); ++it) { + if (it->first.length() > longestString.length()) { + longestString = it->first; + } + } + longestString += "A"; + return mk_string(longestString); +} + void str_decl_plugin::get_op_names(svector & op_names, symbol const & logic) { op_names.push_back(builtin_name("Concat", OP_STRCAT)); op_names.push_back(builtin_name("Length", OP_STRLEN)); diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 61d1bc2f2..f1978ab8b 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -62,6 +62,7 @@ public: app * mk_string(const char * val); app * mk_string(std::string & val); + app * mk_fresh_string(); virtual void get_op_names(svector & op_names, symbol const & logic); virtual void get_sort_names(svector & sort_names, symbol const & logic); @@ -97,6 +98,9 @@ public: app * mk_string(std::string & val) { return m_plugin->mk_string(val); } + app * mk_fresh_string() { + return m_plugin->mk_fresh_string(); + } // TODO }; diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 7514f8a23..221f472d2 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -279,6 +279,11 @@ app * theory_str::mk_nonempty_str_var() { SASSERT(lhs_gt_rhs); assert_axiom(lhs_gt_rhs); } + + // add 'a' to variable sets, so we can keep track of it + variable_set.insert(a); + internal_variable_set.insert(a); + return a; } @@ -1685,9 +1690,17 @@ void theory_str::set_up_axioms(expr * ex) { SASSERT(n); m_basicstr_axiom_todo.push_back(n); - // if additionally ex is a concatenation, set up concatenation axioms - if (is_app(ex) && is_concat(to_app(ex))) { - m_concat_axiom_todo.push_back(n); + + if (is_app(ex)) { + app * ap = to_app(ex); + if (is_concat(ap)) { + // if ex is a concat, set up concat axioms later + m_concat_axiom_todo.push_back(n); + } else if (ap->get_num_args() == 0 && !is_string(ap)) { + // if ex is a variable, add it to our list of variables + TRACE("t_str_detail", tout << "tracking variable" << std::endl;); + variable_set.insert(ex); + } } } else { TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << @@ -1824,12 +1837,41 @@ void theory_str::dump_assignments() { } final_check_status theory_str::final_check_eh() { - // TODO - TRACE("t_str", tout << "final check" << std::endl;); + ast_manager & m = get_manager(); + context & ctx = get_context(); + TRACE("t_str", tout << "final check" << std::endl;); TRACE("t_str_detail", dump_assignments();); - return FC_DONE; + // Check every variable to see if it's eq. to some string constant. + // If not, mark it as free. + bool needToAssignFreeVars = false; + std::set free_variables; + for (std::set::iterator it = variable_set.begin(); it != variable_set.end(); ++it) { + bool has_eqc_value = false; + get_eqc_value(*it, has_eqc_value); + if (!has_eqc_value) { + needToAssignFreeVars = true; + free_variables.insert(*it); + } + } + + if (!needToAssignFreeVars) { + TRACE("t_str", tout << "All variables are assigned. Done!" << std::endl;); + return FC_DONE; + } + + for (std::set::iterator it = free_variables.begin(); it != free_variables.end(); ++it) { + expr * var = *it; + if (internal_variable_set.find(var) != internal_variable_set.end()) { + TRACE("t_str", tout << "assigning arbitrary string to internal variable " << mk_ismt2_pp(var, m) << std::endl;); + app * val = m_strutil.mk_string("**unused**"); + assert_axiom(ctx.mk_eq_atom(var, val)); + } else { + NOT_IMPLEMENTED_YET(); // TODO free variable assignment from strTheory::cb_final_check() + } + } + return FC_CONTINUE; } void theory_str::init_model(model_generator & mg) { @@ -1899,7 +1941,11 @@ model_value_proc * theory_str::mk_value(enode * n, model_generator & mg) { if (val != NULL) { return alloc(expr_wrapper_proc, val); } else { - m.raise_exception("failed to find concrete value"); return NULL; + TRACE("t_str", tout << "WARNING: failed to find a concrete value, falling back" << std::endl;); + // TODO make absolutely sure the reason we can't find a concrete value is because of an unassigned temporary + // e.g. for an expression like (Concat X $$_str0) + //return alloc(expr_wrapper_proc, m_strutil.mk_string("")); + NOT_IMPLEMENTED_YET(); } } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 930c8e9c8..1c2e2fbee 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -43,7 +43,11 @@ namespace smt { v2 = m_util.mk_string("value 2"); return true; } - virtual expr * get_fresh_value(sort * s) { NOT_IMPLEMENTED_YET(); } + virtual expr * get_fresh_value(sort * s) { + // TODO this may be causing crashes in model gen? investigate + //return m_util.mk_fresh_string(); + NOT_IMPLEMENTED_YET(); + } virtual void register_value(expr * n) { /* Ignore */ } }; @@ -75,6 +79,9 @@ namespace smt { bool avoidLoopCut; bool loopDetected; std::map > cut_var_map; + + std::set variable_set; + std::set internal_variable_set; protected: void assert_axiom(expr * e); void assert_implication(expr * premise, expr * conclusion); From 96d99dfb3888852cd1cb4521fb3513f7b9c7a817 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 2 Oct 2015 14:05:17 -0400 Subject: [PATCH 044/562] process_concat_eq_type2 implementation, not tested WIP --- src/smt/theory_str.cpp | 184 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 182 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 221f472d2..a6bdc4944 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1098,13 +1098,193 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } // (splitType == -1) } +/************************************************************* + * Type 2: concat(x, y) = concat(m, "str") + *************************************************************/ bool theory_str::is_concat_eq_type2(expr * concatAst1, expr * concatAst2) { - // TODO - return false; + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + if ((!m_strutil.is_string(v1_arg0)) && m_strutil.is_string(v1_arg1) + && (!m_strutil.is_string(v2_arg0)) && (!m_strutil.is_string(v2_arg1))) { + return true; + } else if ((!m_strutil.is_string(v2_arg0)) && m_strutil.is_string(v2_arg1) + && (!m_strutil.is_string(v1_arg0)) && (!m_strutil.is_string(v1_arg1))) { + return true; + } else { + return false; + } } void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { + ast_manager & mgr = get_manager(); + context & ctx = get_context(); + TRACE("t_str_detail", tout << "process_concat_eq TYPE 2" << std::endl + << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl + << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; + ); + if (!is_concat(to_app(concatAst1))) { + TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); + return; + } + if (!is_concat(to_app(concatAst2))) { + TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); + return; + } + + expr * x = NULL; + expr * y = NULL; + expr * strAst = NULL; + expr * m = NULL; + + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + if (m_strutil.is_string(v1_arg1) && !m_strutil.is_string(v2_arg1)) { + m = v1_arg0; + strAst = v1_arg1; + x = v2_arg0; + y = v2_arg1; + } else { + m = v2_arg0; + strAst = v2_arg1; + x = v1_arg0; + y = v1_arg1; + } + + const char * strValue_tmp = 0; + m_strutil.is_string(strAst, &strValue_tmp); + std::string strValue(strValue_tmp); + // TODO integer theory interaction + /* + int x_len = getLenValue(t, x); + int y_len = getLenValue(t, y); + int m_len = getLenValue(t, m); + int str_len = getLenValue(t, strAst); + */ + + int x_len = -1; + int y_len = -1; + int m_len = -1; + int str_len = -1; + + // setup + + expr * xorFlag = NULL; + expr * temp1 = NULL; + std::pair key1(concatAst1, concatAst2); + std::pair key2(concatAst2, concatAst1); + + if (varForBreakConcat.find(key1) == varForBreakConcat.end() + && varForBreakConcat.find(key2) == varForBreakConcat.end()) { + temp1 = mk_nonempty_str_var(); + xorFlag = mk_internal_xor_var(); + varForBreakConcat[key1][0] = temp1; + varForBreakConcat[key1][1] = xorFlag; + } else { + if (varForBreakConcat.find(key1) != varForBreakConcat.end()) { + temp1 = varForBreakConcat[key1][0]; + xorFlag = varForBreakConcat[key1][1]; + } else if (varForBreakConcat.find(key2) != varForBreakConcat.end()) { + temp1 = varForBreakConcat[key2][0]; + xorFlag = varForBreakConcat[key2][1]; + } + } + + int splitType = -1; + if (x_len != -1 && m_len != -1) { + if (x_len < m_len) + splitType = 0; + else if (x_len == m_len) + splitType = 1; + else + splitType = 2; + } + if (splitType == -1 && y_len != -1 && str_len != -1) { + if (y_len > str_len) + splitType = 0; + else if (y_len == str_len) + splitType = 1; + else + splitType = 2; + } + + TRACE("t_str_detail", tout << "Split type " << splitType << std::endl;); + + // Provide fewer split options when length information is available. + + if (splitType == 0) { + NOT_IMPLEMENTED_YET(); // TODO + } else if (splitType == 1) { + NOT_IMPLEMENTED_YET(); // TODO + } else if (splitType == 2) { + NOT_IMPLEMENTED_YET(); // TODO + } else { + // Split type -1: no idea about the length... + int optionTotal = 2 + strValue.length(); + expr ** or_item = alloc_svect(expr*, optionTotal); + expr ** and_item = alloc_svect(expr*, (1 + 6 + 4 * (strValue.length() + 1))); + int option = 0; + int pos = 1; + + expr_ref temp1_strAst(mk_concat(temp1, strAst), mgr); // TODO assert concat axioms? + + // m cuts y + if (can_two_nodes_eq(y, temp1_strAst)) { + if (!avoidLoopCut || !has_self_cut(m, y)) { + // break down option 2-1 + // TODO + or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); + expr_ref x_temp1(mk_concat(x, temp1), mgr); // TODO assert concat axioms? + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(m, x_temp1)); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(y, temp1_strAst)); + + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(m), + m_autil.mk_add(mk_strlen(x), mk_strlen(temp1)))); + + ++option; + add_cut_info_merge(temp1, ctx.get_scope_level(), y); + add_cut_info_merge(temp1, ctx.get_scope_level(), m); + } else { + loopDetected = true; + TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + // TODO printCutVar(m, y) + } + } + + for (int i = 0; i <= (int)strValue.size(); ++i) { + std::string part1Str = strValue.substr(0, i); + std::string part2Str = strValue.substr(i, strValue.size() - i); + expr_ref prefixStr(m_strutil.mk_string(part1Str), mgr); + expr_ref x_concat(mk_concat(m, prefixStr), mgr); // TODO concat axioms? + expr_ref cropStr(m_strutil.mk_string(part2Str), mgr); + if (can_two_nodes_eq(x, x_concat) && can_two_nodes_eq(y, cropStr)) { + // break down option 2-2 + or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(x, x_concat)); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(y, cropStr)); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(y), mk_int(part2Str.length()))); + ++option; + } + } + + if (option > 0) { + if (option == 1) { + and_item[0] = or_item[0]; + } else { + and_item[0] = mgr.mk_or(option, or_item); + } + expr_ref implyR(mgr.mk_and(pos, and_item), mgr); + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } else { + TRACE("t_str", tout << "STOP: Should not split two EQ concats." << std::endl;); + } + } // (splitType == -1) } bool theory_str::is_concat_eq_type3(expr * concatAst1, expr * concatAst2) { From ff4706dd40b3334724d81b385ed2cad1fcc0f8ba Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 3 Oct 2015 12:07:55 -0400 Subject: [PATCH 045/562] process_concat_eq_type3 still wip because i'm just trying to get these all done --- src/smt/theory_str.cpp | 199 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 197 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index a6bdc4944..7b555d6bb 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -892,6 +892,10 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { } +/************************************************************* + * Type 1: concat(x, y) = concat(m, n) + * x, y, m and n all variables + *************************************************************/ bool theory_str::is_concat_eq_type1(expr * concatAst1, expr * concatAst2) { expr * x = to_app(concatAst1)->get_arg(0); expr * y = to_app(concatAst1)->get_arg(1); @@ -1287,12 +1291,203 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { } // (splitType == -1) } +/************************************************************* + * Type 3: concat(x, y) = concat("str", n) + *************************************************************/ bool theory_str::is_concat_eq_type3(expr * concatAst1, expr * concatAst2) { - // TODO - return false; + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + if (m_strutil.is_string(v1_arg0) && (!m_strutil.is_string(v1_arg1)) + && (!m_strutil.is_string(v2_arg0)) && (!m_strutil.is_string(v2_arg1))) { + return true; + } else if (m_strutil.is_string(v2_arg0) && (!m_strutil.is_string(v2_arg1)) + && (!m_strutil.is_string(v1_arg0)) && (!m_strutil.is_string(v1_arg1))) { + return true; + } else { + return false; + } } void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { + ast_manager & mgr = get_manager(); + context & ctx = get_context(); + TRACE("t_str_detail", tout << "process_concat_eq TYPE 3" << std::endl + << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl + << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; + ); + + if (!is_concat(to_app(concatAst1))) { + TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); + return; + } + if (!is_concat(to_app(concatAst2))) { + TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); + return; + } + + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + expr * x = NULL; + expr * y = NULL; + expr * strAst = NULL; + expr * n = NULL; + + if (m_strutil.is_string(v1_arg0) && !m_strutil.is_string(v2_arg0)) { + strAst = v1_arg0; + n = v1_arg1; + x = v2_arg0; + y = v2_arg1; + } else { + strAst = v2_arg0; + n = v2_arg1; + x = v1_arg0; + y = v1_arg1; + } + + const char * strValue_tmp = 0; + m_strutil.is_string(strAst, &strValue_tmp); + std::string strValue(strValue_tmp); + // TODO integer theory interaction + /* + int x_len = getLenValue(t, x); + int y_len = getLenValue(t, y); + int str_len = getLenValue(t, strAst); + int n_len = getLenValue(t, n); + */ + int x_len = -1; + int y_len = -1; + int str_len = -1; + int n_len = -1; + + expr_ref xorFlag(mgr); + expr_ref temp1(mgr); + std::pair key1(concatAst1, concatAst2); + std::pair key2(concatAst2, concatAst1); + if (varForBreakConcat.find(key1) == varForBreakConcat.end() && varForBreakConcat.find(key2) == varForBreakConcat.end()) { + temp1 = mk_nonempty_str_var(); + xorFlag = mk_internal_xor_var(); + + varForBreakConcat[key1][0] = temp1; + varForBreakConcat[key1][1] = xorFlag; + } else { + if (varForBreakConcat.find(key1) != varForBreakConcat.end()) { + temp1 = varForBreakConcat[key1][0]; + xorFlag = varForBreakConcat[key1][1]; + } else if (varForBreakConcat.find(key2) != varForBreakConcat.end()) { + temp1 = varForBreakConcat[key2][0]; + xorFlag = varForBreakConcat[key2][1]; + } + } + + + + int splitType = -1; + if (x_len != -1) { + if (x_len < str_len) + splitType = 0; + else if (x_len == str_len) + splitType = 1; + else + splitType = 2; + } + if (splitType == -1 && y_len != -1 && n_len != -1) { + if (y_len > n_len) + splitType = 0; + else if (y_len == n_len) + splitType = 1; + else + splitType = 2; + } + + TRACE("t_str_detail", tout << "Split type " << splitType << std::endl;); + + // Provide fewer split options when length information is available. + if (splitType == 0) { + NOT_IMPLEMENTED_YET(); // TODO + } + else if (splitType == 1) { + NOT_IMPLEMENTED_YET(); // TODO + } + else if (splitType == 2) { + NOT_IMPLEMENTED_YET(); // TODO + } + else { + // Split type -1. We know nothing about the length... + + int optionTotal = 2 + strValue.length(); + expr ** or_item = alloc_svect(expr*, optionTotal); + int option = 0; + expr ** and_item = alloc_svect(expr*, (2 + 4 * optionTotal)); + int pos = 1; + for (int i = 0; i <= (int) strValue.size(); i++) { + std::string part1Str = strValue.substr(0, i); + std::string part2Str = strValue.substr(i, strValue.size() - i); + expr_ref cropStr(m_strutil.mk_string(part1Str), mgr); + expr_ref suffixStr(m_strutil.mk_string(part2Str), mgr); + expr_ref y_concat(mk_concat(suffixStr, n), mgr); // TODO concat axioms? + + if (can_two_nodes_eq(x, cropStr) && can_two_nodes_eq(y, y_concat)) { + // break down option 3-1 + expr_ref x_eq_str(ctx.mk_eq_atom(x, cropStr), mgr); + or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], x_eq_str); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(y, y_concat)); + + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(x), mk_strlen(cropStr))); + // and_item[pos++] = Z3_mk_eq(ctx, or_item[option], Z3_mk_eq(ctx, mk_length(t, y), mk_length(t, y_concat))); + + // adding length constraint for _ = constStr seems slowing things down. + option++; + } + } + + expr_ref strAst_temp1(mk_concat(strAst, temp1), mgr); + + + //-------------------------------------------------------- + // x cut n + //-------------------------------------------------------- + if (can_two_nodes_eq(x, strAst_temp1)) { + if (!avoidLoopCut || !(has_self_cut(x, n))) { + // break down option 3-2 + or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); + + expr_ref temp1_y(mk_concat(temp1, y), mgr); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(x, strAst_temp1)); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(n, temp1_y)); + + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(x), + m_autil.mk_add(mk_strlen(strAst), mk_strlen(temp1)) )); + option++; + + add_cut_info_merge(temp1, ctx.get_scope_level(), x); + add_cut_info_merge(temp1, ctx.get_scope_level(), n); + } else { + loopDetected = true; + TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); + // TODO printCutVAR(x, n) + } + } + + + if (option > 0) { + if (option == 1) { + and_item[0] = or_item[0]; + } else { + and_item[0] = mgr.mk_or(option, or_item); + } + expr_ref implyR(mgr.mk_and(pos, and_item), mgr); + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } else { + TRACE("t_str", tout << "STOP: should not split two eq. concats" << std::endl;); + } + } } From f7bc785a56f1bd4dade92a5812a843b634500fc5 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 3 Oct 2015 12:19:55 -0400 Subject: [PATCH 046/562] process_concat_eq_type4, still WIP not tested --- src/smt/theory_str.cpp | 82 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 7b555d6bb..5033ca978 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1491,13 +1491,91 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { } +/************************************************************* + * Type 4: concat("str1", y) = concat("str2", n) + *************************************************************/ bool theory_str::is_concat_eq_type4(expr * concatAst1, expr * concatAst2) { - // TODO - return false; + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + if (m_strutil.is_string(v1_arg0) && (!m_strutil.is_string(v1_arg1)) + && m_strutil.is_string(v2_arg0) && (!m_strutil.is_string(v2_arg1))) { + return true; + } else { + return false; + } } void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { + ast_manager & mgr = get_manager(); + context & ctx = get_context(); + TRACE("t_str_detail", tout << "process_concat_eq TYPE 4" << std::endl + << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl + << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; + ); + if (!is_concat(to_app(concatAst1))) { + TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); + return; + } + if (!is_concat(to_app(concatAst2))) { + TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); + return; + } + + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + expr * str1Ast = v1_arg0; + expr * y = v1_arg1; + expr * str2Ast = v2_arg0; + expr * n = v2_arg1; + + const char *tmp = 0; + m_strutil.is_string(str1Ast, &tmp); + std::string str1Value(tmp); + m_strutil.is_string(str2Ast, &tmp); + std::string str2Value(tmp); + + int str1Len = str1Value.length(); + int str2Len = str2Value.length(); + + int commonLen = (str1Len > str2Len) ? str2Len : str1Len; + if (str1Value.substr(0, commonLen) != str2Value.substr(0, commonLen)) { + TRACE("t_str_detail", tout << "Conflict: " << mk_ismt2_pp(concatAst1, mgr) + << " has no common prefix with " << mk_ismt2_pp(concatAst2, mgr) << std::endl;); + expr_ref toNegate(mgr.mk_not(ctx.mk_eq_atom(concatAst1, concatAst2)), mgr); + assert_axiom(toNegate); + return; + } else { + if (str1Len > str2Len) { + std::string deltaStr = str1Value.substr(str2Len, str1Len - str2Len); + expr_ref tmpAst(mk_concat(m_strutil.mk_string(deltaStr), y), mgr); + if (!in_same_eqc(tmpAst, n)) { + // break down option 4-1 + expr_ref implyR(ctx.mk_eq_atom(n, tmpAst), mgr); + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } + } else if (str1Len == str2Len) { + if (!in_same_eqc(n, y)) { + //break down option 4-2 + expr_ref implyR(ctx.mk_eq_atom(n, y), mgr); + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } + } else { + std::string deltaStr = str2Value.substr(str1Len, str2Len - str1Len); + expr_ref tmpAst(mk_concat(m_strutil.mk_string(deltaStr), n), mgr); + if (!in_same_eqc(y, tmpAst)) { + //break down option 4-3 + expr_ref implyR(ctx.mk_eq_atom(y, tmpAst), mgr); + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } + } + } } bool theory_str::is_concat_eq_type5(expr * concatAst1, expr * concatAst2) { From be7972338224e149356632e96171cc18b74112c4 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 3 Oct 2015 12:26:30 -0400 Subject: [PATCH 047/562] process_concat_eq_type5 wip --- src/smt/theory_str.cpp | 80 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 5033ca978..f052d293a 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1578,13 +1578,89 @@ void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { } } +/************************************************************* + * case 5: concat(x, "str1") = concat(m, "str2") + *************************************************************/ bool theory_str::is_concat_eq_type5(expr * concatAst1, expr * concatAst2) { - // TODO - return false; + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + if ((!m_strutil.is_string(v1_arg0)) && m_strutil.is_string(v1_arg1) + && (!m_strutil.is_string(v2_arg0)) && m_strutil.is_string(v2_arg1)) { + return true; + } else { + return false; + } } void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { + ast_manager & mgr = get_manager(); + context & ctx = get_context(); + TRACE("t_str_detail", tout << "process_concat_eq TYPE 5" << std::endl + << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl + << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; + ); + if (!is_concat(to_app(concatAst1))) { + TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); + return; + } + if (!is_concat(to_app(concatAst2))) { + TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); + return; + } + + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + expr * x = v1_arg0; + expr * str1Ast = v1_arg1; + expr * m = v2_arg0; + expr * str2Ast = v2_arg1; + + const char *tmp = 0; + m_strutil.is_string(str1Ast, &tmp); + std::string str1Value(tmp); + m_strutil.is_string(str2Ast, &tmp); + std::string str2Value(tmp); + + int str1Len = str1Value.length(); + int str2Len = str2Value.length(); + + int cLen = (str1Len > str2Len) ? str2Len : str1Len; + if (str1Value.substr(str1Len - cLen, cLen) != str2Value.substr(str2Len - cLen, cLen)) { + TRACE("t_str_detail", tout << "Conflict: " << mk_ismt2_pp(concatAst1, mgr) + << " has no common suffix with " << mk_ismt2_pp(concatAst2, mgr) << std::endl;); + expr_ref toNegate(mgr.mk_not(ctx.mk_eq_atom(concatAst1, concatAst2)), mgr); + assert_axiom(toNegate); + return; + } else { + if (str1Len > str2Len) { + std::string deltaStr = str1Value.substr(0, str1Len - str2Len); + expr_ref x_deltaStr(mk_concat(x, m_strutil.mk_string(deltaStr)), mgr); + if (!in_same_eqc(m, x_deltaStr)) { + expr_ref implyR(ctx.mk_eq_atom(m, x_deltaStr), mgr); + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } + } else if (str1Len == str2Len) { + // test + if (!in_same_eqc(x, m)) { + expr_ref implyR(ctx.mk_eq_atom(x, m), mgr); + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } + } else { + std::string deltaStr = str2Value.substr(0, str2Len - str1Len); + expr_ref m_deltaStr(mk_concat(m, m_strutil.mk_string(deltaStr)), mgr); + if (!in_same_eqc(x, m_deltaStr)) { + expr_ref implyR(ctx.mk_eq_atom(x, m_deltaStr), mgr); + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } + } + } } bool theory_str::is_concat_eq_type6(expr * concatAst1, expr * concatAst2) { From 6791db64c01ac7dfbac7c0c6a883be9692e513da Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 3 Oct 2015 13:34:42 -0400 Subject: [PATCH 048/562] process_concat_eq_type6 that's the last one! --- src/smt/theory_str.cpp | 162 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 160 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index f052d293a..aebaec572 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -20,6 +20,7 @@ Revision History: #include"smt_model_generator.h" #include"ast_pp.h" #include"ast_ll_pp.h" +#include namespace smt { @@ -1663,13 +1664,170 @@ void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { } } +/************************************************************* + * case 6: concat("str1", y) = concat(m, "str2") + *************************************************************/ bool theory_str::is_concat_eq_type6(expr * concatAst1, expr * concatAst2) { - // TODO - return false; + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + if (m_strutil.is_string(v1_arg0) && (!m_strutil.is_string(v1_arg1)) + && (!m_strutil.is_string(v2_arg0)) && m_strutil.is_string(v2_arg1)) { + return true; + } else if (m_strutil.is_string(v2_arg0) && (!m_strutil.is_string(v2_arg1)) + && (!m_strutil.is_string(v1_arg0)) && m_strutil.is_string(v1_arg1)) { + return true; + } else { + return false; + } } void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { + ast_manager & mgr = get_manager(); + context & ctx = get_context(); + TRACE("t_str_detail", tout << "process_concat_eq TYPE 6" << std::endl + << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl + << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; + ); + if (!is_concat(to_app(concatAst1))) { + TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); + return; + } + if (!is_concat(to_app(concatAst2))) { + TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); + return; + } + + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + + expr * str1Ast = NULL; + expr * y = NULL; + expr * m = NULL; + expr * str2Ast = NULL; + + if (m_strutil.is_string(v1_arg0)) { + str1Ast = v1_arg0; + y = v1_arg1; + m = v2_arg0; + str2Ast = v2_arg1; + } else { + str1Ast = v2_arg0; + y = v2_arg1; + m = v1_arg0; + str2Ast = v1_arg1; + } + + const char *tmp = 0; + m_strutil.is_string(str1Ast, &tmp); + std::string str1Value(tmp); + m_strutil.is_string(str2Ast, &tmp); + std::string str2Value(tmp); + + int str1Len = str1Value.length(); + int str2Len = str2Value.length(); + + //---------------------------------------- + //(a) |---str1---|----y----| + // |--m--|-----str2-----| + // + //(b) |---str1---|----y----| + // |-----m----|--str2---| + // + //(c) |---str1---|----y----| + // |------m------|-str2-| + //---------------------------------------- + + std::list overlapLen; + overlapLen.push_back(0); + + for (int i = 1; i <= str1Len && i <= str2Len; i++) { + if (str1Value.substr(str1Len - i, i) == str2Value.substr(0, i)) + overlapLen.push_back(i); + } + + //---------------------------------------------------------------- + expr * commonVar = NULL; + expr * xorFlag = NULL; + std::pair key1(concatAst1, concatAst2); + std::pair key2(concatAst2, concatAst1); + if (varForBreakConcat.find(key1) == varForBreakConcat.end() && varForBreakConcat.find(key2) == varForBreakConcat.end()) { + commonVar = mk_nonempty_str_var(); + xorFlag = mk_internal_xor_var(); + varForBreakConcat[key1][0] = commonVar; + varForBreakConcat[key1][1] = xorFlag; + } else { + if (varForBreakConcat.find(key1) != varForBreakConcat.end()) { + commonVar = varForBreakConcat[key1][0]; + xorFlag = varForBreakConcat[key1][1]; + } else { + commonVar = varForBreakConcat[key2][0]; + xorFlag = varForBreakConcat[key2][1]; + } + } + + expr ** or_item = alloc_svect(expr*, (overlapLen.size() + 1)); + int option = 0; + expr ** and_item = alloc_svect(expr*, (1 + 4 * (overlapLen.size() + 1))); + int pos = 1; + + if (!avoidLoopCut || !has_self_cut(m, y)) { + or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); + + expr_ref str1_commonVar(mk_concat(str1Ast, commonVar), mgr); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(m, str1_commonVar)); + + expr_ref commonVar_str2(mk_concat(commonVar, str2Ast), mgr); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(y, commonVar_str2)); + + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(m), + m_autil.mk_add(mk_strlen(str1Ast), mk_strlen(commonVar)) )); + + // addItems[0] = mk_length(t, commonVar); + // addItems[1] = mk_length(t, str2Ast); + // and_item[pos++] = Z3_mk_eq(ctx, or_item[option], Z3_mk_eq(ctx, mk_length(t, y), Z3_mk_add(ctx, 2, addItems))); + + option++; + } else { + loopDetected = true; + TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); + // TODO printCutVAR(m, y) + } + + for (std::list::iterator itor = overlapLen.begin(); itor != overlapLen.end(); itor++) { + int overLen = *itor; + std::string prefix = str1Value.substr(0, str1Len - overLen); + std::string suffix = str2Value.substr(overLen, str2Len - overLen); + or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); + + expr_ref prefixAst(m_strutil.mk_string(prefix), mgr); + expr_ref x_eq_prefix(ctx.mk_eq_atom(m, prefixAst), mgr); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], x_eq_prefix); + + and_item[pos++] = ctx.mk_eq_atom(or_item[option], + ctx.mk_eq_atom(mk_strlen(m), mk_strlen(prefixAst))); + + // adding length constraint for _ = constStr seems slowing things down. + + expr_ref suffixAst(m_strutil.mk_string(suffix), mgr); + expr_ref y_eq_suffix(ctx.mk_eq_atom(y, suffixAst), mgr); + and_item[pos++] = ctx.mk_eq_atom(or_item[option], y_eq_suffix); + + and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(y), mk_strlen(suffixAst))); + + option++; + } + + // case 6: concat("str1", y) = concat(m, "str2") + and_item[0] = mgr.mk_or(option, or_item); + expr_ref implyR(mgr.mk_and(pos, and_item), mgr); + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); } /* From b494804c9c75b083767dd1a63d75f1abf8ebc717 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 6 Oct 2015 19:31:26 -0400 Subject: [PATCH 049/562] ignore tests dir --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 97ca67cf4..b4a69f69e 100644 --- a/.gitignore +++ b/.gitignore @@ -77,4 +77,7 @@ doc/code # reference code for z3str2 Z3-str Z3-str/** +# test cases +tests +tests/** From e521ab2c3af04c7c4b4082a2ffc81b0c6caf864a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 18 Oct 2015 19:39:55 -0400 Subject: [PATCH 050/562] fix concat_axiom loop in propagate(): compare against size()...... --- src/smt/theory_str.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index aebaec572..47165997d 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -376,7 +376,7 @@ void theory_str::propagate() { } m_str_eq_todo.reset(); - for (unsigned i = 0; i < m_concat_axiom_todo.empty(); ++i) { + for (unsigned i = 0; i < m_concat_axiom_todo.size(); ++i) { instantiate_concat_axiom(m_concat_axiom_todo[i]); } m_concat_axiom_todo.reset(); From 3ee8f27588bcb88220f0ff515526ee0003716c43 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 18 Oct 2015 20:20:09 -0400 Subject: [PATCH 051/562] possibly fix internalization bug mentioned in #2 (this leads to a not-implemented-yet in final_check_eh() due to missing code surrounding free variable production) --- src/smt/theory_str.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 47165997d..15253bcfd 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1837,6 +1837,8 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { */ expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { context & ctx = get_context(); + // I hope this works + ctx.internalize(n, false); enode * nNode = ctx.get_enode(n); enode * eqcNode = nNode; do { From c08f4371f4b9b0f98001eae4c111dbe91cf53d8f Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 21 Oct 2015 21:32:38 -0400 Subject: [PATCH 052/562] begin model generation, wip --- src/smt/theory_str.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++ src/smt/theory_str.h | 5 +++ 2 files changed, 102 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 15253bcfd..3e6b637d1 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2525,6 +2525,88 @@ void theory_str::dump_assignments() { ); } +// NOTE: this function used to take an argument `Z3_ast node`; +// it was not used and so was removed from the signature +void theory_str::classify_ast_by_type_in_positive_context(std::map & varMap, + std::map & concatMap, std::map & unrollMap) { + + context & ctx = get_context(); + ast_manager & m = get_manager(); + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + + for (expr_ref_vector::iterator it = assignments.begin(); it != assignments.end(); ++it) { + expr * argAst = *it; + // TODO + NOT_IMPLEMENTED_YET(); + /* + * according to getNodeType(), the following things are considered "functions": + * Contains, StartsWith, EndsWith, RegexIn + * Length, Indexof, Indexof2, LastIndexof + * Concat, SubString, Replace, Unroll, CharAt + * RegexConcat, RegexStar, RegexPlus, RegexCharRange, RegexUnion, Str2Reg + * something about Z3_ARRAY_SORT? + * Z3 native functions that aren't considered "uninterpreted" + * "real" uninterpreted functions declared in the input (domainSize != 0) + */ + + /* + if (getNodeType(t, argAst) == my_Z3_Func) { + Z3_app func_app = Z3_to_app(ctx, argAst); + Z3_decl_kind func_decl = Z3_get_decl_kind(ctx, Z3_get_app_decl(ctx, func_app)); + + if (isInterestingFuncKind(func_decl)) { + classifyAstByType(t, argAst, varMap, concatMap, unrollMap); + } + } + */ + } +} + +/* + * Dependence analysis from current context assignment + * - "freeVarMap" contains a set of variables that doesn't constrained by Concats. + * But it's possible that it's bounded by unrolls + * For the case of + * (1) var1 = unroll(r1, t1) + * var1 is in the freeVarMap + * > should unroll r1 for var1 + * (2) var1 = unroll(r1, t1) /\ var1 = Concat(var2, var3) + * var2, var3 are all in freeVar + * > should split the unroll function so that var2 and var3 are bounded by new unrolls + */ +int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map & freeVarMap, + std::map > & unrollGroupMap) { + std::map concatMap; + std::map unrollMap; + std::map aliasIndexMap; + std::map var_eq_constStr_map; + std::map concat_eq_constStr_map; + std::map > var_eq_concat_map; + std::map > var_eq_unroll_map; + std::map > concat_eq_concat_map; + std::map > depMap; + + context & ctx = get_context(); + ast_manager & m = get_manager(); + + // note that the old API concatenated these assignments into + // a massive conjunction; we may have the opportunity to avoid that here + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + + // Step 1: get variables / concat AST appearing in the context + // TODO build this map; see strTheory::checkInputVar() + // it should just be variable_set - internal_variable_set? + for(std::map::iterator it = inputVarMap.begin(); it != inputVarMap.end(); ++it) { + strVarMap[it->first] = 1; + } + classify_ast_by_type_in_positive_context(assignments, strVarMap, concatMap, unrollMap); + + // TODO the rest + NOT_IMPLEMENTED_YET(); +} + final_check_status theory_str::final_check_eh() { ast_manager & m = get_manager(); context & ctx = get_context(); @@ -2532,6 +2614,20 @@ final_check_status theory_str::final_check_eh() { TRACE("t_str", tout << "final check" << std::endl;); TRACE("t_str_detail", dump_assignments();); + // run dependence analysis to find free string variables + std::map varAppearInAssign; + std::map freeVar_map; + std::map > unrollGroup_map; + int conflictInDep = ctx_dep_analysis(varAppearInAssign, freeVar_map, unrollGroup_map); + if (conflictInDep == -1) { + // return Z3_TRUE; + return FC_DONE; + } + + // TODO the rest... + NOT_IMPLEMENTED_YET(); + + /* // Check every variable to see if it's eq. to some string constant. // If not, mark it as free. bool needToAssignFreeVars = false; @@ -2561,6 +2657,7 @@ final_check_status theory_str::final_check_eh() { } } return FC_CONTINUE; + */ } void theory_str::init_model(model_generator & mg) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 1c2e2fbee..80e321729 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -144,6 +144,11 @@ namespace smt { bool new_eq_check(expr * lhs, expr * rhs); void group_terms_by_eqc(expr * n, std::set & concats, std::set & vars, std::set & consts); + int ctx_dep_analysis(std::map & strVarMap, std::map & freeVarMap, + std::map > & unrollGroupMap); + void classify_ast_by_type_in_positive_context(std::map & varMap, + std::map & concatMap, std::map & unrollMap) + void dump_assignments(); public: theory_str(ast_manager & m); From 1f3c5cebbf660a52aebae291846a65a021c30520 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 26 Oct 2015 15:43:31 -0400 Subject: [PATCH 053/562] variable classification (WIP) --- src/smt/theory_str.cpp | 82 ++++++++++++++++++++++++++++++------------ src/smt/theory_str.h | 6 +++- 2 files changed, 64 insertions(+), 24 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 3e6b637d1..86aaaaf44 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2525,6 +2525,57 @@ void theory_str::dump_assignments() { ); } +void theory_str::classify_ast_by_type(expr * node, std::map & varMap, + std::map & concatMap, std::map & unrollMap) { + + context & ctx = get_context(); + ast_manager & m = get_manager(); + + // check whether the node is a non-internal string variable; + // testing set membership here bypasses several expensive checks + if (variable_set.find(node) != variable_set.end() + && internal_variable_set.find(node) == internal_variable_set.end()) { + varMap[node] = 1; + } + // check whether the node is a function that we want to inspect + else if (is_app(node)) { // TODO + app * aNode = to_app(node); + if (is_strlen(aNode)) { + // Length + return; + } else if (is_concat(aNode)) { + expr * arg0 = aNode->get_arg(0); + expr * arg1 = aNode->get_arg(1); + bool arg0HasEq = false; + bool arg1HasEq = false; + expr * arg0Val = get_eqc_value(arg0, arg0HasEq); + expr * arg1Val = get_eqc_value(arg1, arg1HasEq); + + int canskip = 0; + if (arg0HasEq && arg0Val == m_strutil.mk_string("")) { + canskip = 1; + } + if (canskip == 0 && arg1HasEq && arg1Val == m_strutil.mk_string("")) { + canskip = 1; + } + if (canskip == 0 && concatMap.find(node) == concatMap.end()) { + concatMap[node] = 1; + } + } else if (false) { // TODO is_unroll() + // Unroll + if (unrollMap.find(node) == unrollMap.end()) { + unrollMap[node] = 1; + } + } + // recursively visit all arguments + app * aNode = to_app(node); + for (unsigned i = 0; i < aNode->get_num_args(); ++i) { + expr * arg = aNode->get_arg(i); + classify_ast_by_type(arg, varMap, concatMap, unrollMap); + } + } +} + // NOTE: this function used to take an argument `Z3_ast node`; // it was not used and so was removed from the signature void theory_str::classify_ast_by_type_in_positive_context(std::map & varMap, @@ -2537,29 +2588,14 @@ void theory_str::classify_ast_by_type_in_positive_context(std::map & for (expr_ref_vector::iterator it = assignments.begin(); it != assignments.end(); ++it) { expr * argAst = *it; - // TODO - NOT_IMPLEMENTED_YET(); - /* - * according to getNodeType(), the following things are considered "functions": - * Contains, StartsWith, EndsWith, RegexIn - * Length, Indexof, Indexof2, LastIndexof - * Concat, SubString, Replace, Unroll, CharAt - * RegexConcat, RegexStar, RegexPlus, RegexCharRange, RegexUnion, Str2Reg - * something about Z3_ARRAY_SORT? - * Z3 native functions that aren't considered "uninterpreted" - * "real" uninterpreted functions declared in the input (domainSize != 0) - */ + // the original code jumped through some hoops to check whether the AST node + // is a function, then checked whether that function is "interesting". + // however, the only thing that's considered "interesting" is an equality predicate. + // so we bypass a huge amount of work by doing the following... - /* - if (getNodeType(t, argAst) == my_Z3_Func) { - Z3_app func_app = Z3_to_app(ctx, argAst); - Z3_decl_kind func_decl = Z3_get_decl_kind(ctx, Z3_get_app_decl(ctx, func_app)); - - if (isInterestingFuncKind(func_decl)) { - classifyAstByType(t, argAst, varMap, concatMap, unrollMap); - } + if (m.is_eq(argAst)) { + classify_ast_by_type(argAst, varMap, concatMap, unrollMap); } - */ } } @@ -2598,10 +2634,10 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map::iterator it = inputVarMap.begin(); it != inputVarMap.end(); ++it) { + for(std::map::iterator it = input_var_map.begin(); it != input_var_map.end(); ++it) { strVarMap[it->first] = 1; } - classify_ast_by_type_in_positive_context(assignments, strVarMap, concatMap, unrollMap); + classify_ast_by_type_in_positive_context(strVarMap, concatMap, unrollMap); // TODO the rest NOT_IMPLEMENTED_YET(); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 80e321729..684526602 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -104,6 +104,8 @@ namespace smt { bool is_concat(enode const * n) const { return is_concat(n->get_owner()); } bool is_string(app const * a) const { return a->is_app_of(get_id(), OP_STR); } bool is_string(enode const * n) const { return is_string(n->get_owner()); } + bool is_strlen(app const * a) const { return a->is_app_of(get_id(), OP_STRLEN); } + bool is_strlen(enode const * n) const { return is_strlen(n->get_owner()); } void instantiate_concat_axiom(enode * cat); void instantiate_basic_string_axioms(enode * str); void instantiate_str_eq_length_axiom(enode * lhs, enode * rhs); @@ -146,8 +148,10 @@ namespace smt { int ctx_dep_analysis(std::map & strVarMap, std::map & freeVarMap, std::map > & unrollGroupMap); + void classify_ast_by_type(expr * node, std::map & varMap, + std::map & concatMap, std::map & unrollMap); void classify_ast_by_type_in_positive_context(std::map & varMap, - std::map & concatMap, std::map & unrollMap) + std::map & concatMap, std::map & unrollMap); void dump_assignments(); public: From 9f01b9dc92203f8cdefa7cd9befe43da6c6565f6 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 4 Nov 2015 16:22:06 -0500 Subject: [PATCH 054/562] more progress on model gen (WIP) --- src/smt/theory_str.cpp | 203 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 199 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 86aaaaf44..c5ca630f9 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2632,13 +2632,208 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map::iterator it = input_var_map.begin(); it != input_var_map.end(); ++it) { - strVarMap[it->first] = 1; + // the thing we iterate over should just be variable_set - internal_variable_set + // so we avoid computing the set difference (but this might be slower) + for(std::set::iterator it = variable_set.begin(); it != variable_set.end(); ++it) { + expr* var = *it; + if (internal_variable_set.find(var) == internal_variable_set.end()) { + strVarMap[*it] = 1; + } } classify_ast_by_type_in_positive_context(strVarMap, concatMap, unrollMap); + // TODO unroll() + /* + std::map aliasUnrollSet; + std::map::iterator unrollItor = unrollMap.begin(); + for (; unrollItor != unrollMap.end(); unrollItor++) { + if (aliasUnrollSet.find(unrollItor->first) != aliasUnrollSet.end()) + continue; + Z3_ast aRoot = NULL; + Z3_ast curr = unrollItor->first; + do { + if (isUnrollFunc(t, curr)) { + if (aRoot == NULL) { + aRoot = curr; + } + aliasUnrollSet[curr] = aRoot; + } + curr = Z3_theory_get_eqc_next(t, curr); + } while (curr != unrollItor->first); + } + + for (unrollItor = unrollMap.begin(); unrollItor != unrollMap.end(); unrollItor++) { + Z3_ast unrFunc = unrollItor->first; + Z3_ast urKey = aliasUnrollSet[unrFunc]; + unrollGroupMap[urKey].insert(unrFunc); + } + */ + + // Step 2: collect alias relation + // e.g. suppose we have the equivalence class {x, y, z}; + // then we set aliasIndexMap[y] = x + // and aliasIndexMap[z] = x + + std::map::iterator varItor = strVarMap.begin(); + for (; varItor != strVarMap.end(); ++varItor) { + if (aliasIndexMap.find(varItor->first) != aliasIndexMap.end()) { + continue; + } + expr * aRoot = NULL; + expr * curr = varItor->first; + do { + if (variable_set.find(curr) != variable_set.end()) { // TODO internal_variable_set? + if (aRoot == NULL) { + aRoot = curr; + } else { + aliasIndexMap[curr] = aRoot; + } + } + // curr = get_eqc_next(curr); + enode * eqcNode = ctx.get_enode(curr); + eqcNode = eqcNode->get_next(); + curr = eqcNode->get_owner(); + } while (curr != varItor->first); + } + + // Step 3: Collect interested cases + + varItor = strVarMap.begin(); + for (; varItor != strVarMap.end(); ++varItor) { + expr * deAliasNode = get_alias_index_ast(aliasIndexMap, varItor->first); + // Case 1: variable = string constant + // e.g. z = "str1" ::= var_eq_constStr_map[z] = "str1" + + if (var_eq_constStr_map.find(deAliasNode) == var_eq_constStr_map.end()) { + bool nodeHasEqcValue = false; + expr * nodeValue = get_eqc_value(deAliasNode, nodeHasEqcValue); + if (nodeHasEqcValue) { + var_eq_constStr_map[deAliasNode] = nodeValue; + } + } + + // Case 2: var_eq_concat + // e.g. z = concat("str1", b) ::= var_eq_concat[z][concat(c, "str2")] = 1 + // var_eq_unroll + // e.g. z = unroll(...) ::= var_eq_unroll[z][unroll(...)] = 1 + + if (var_eq_concat_map.find(deAliasNode) == var_eq_concat_map.end()) { + enode * e_curr = ctx.get_enode(deAliasNode); + expr * curr = e_curr->get_next()->get_owner(); + while (curr != deAliasNode) { + app * aCurr = to_app(curr); + // collect concat + if (is_concat(aCurr)) { + expr * arg0 = aCurr->get_arg(0); + expr * arg1 = aCurr->get_arg(1); + bool arg0HasEqcValue = false; + bool arg1HasEqcValue = false; + expr * arg0_value = get_eqc_value(arg0, arg0HasEqcValue); + expr * arg1_value = get_eqc_value(arg1, arg1HasEqcValue); + + bool is_arg0_emptyStr = false; + if (arg0HasEqcValue) { + const char * strval = 0; + m_strutil.is_string(arg0_value, &strval); + if (strcmp(strval, "") == 0) { + is_arg0_emptyStr = true; + } + } + + bool is_arg1_emptyStr = false; + if (arg1HasEqcValue) { + const char * strval = 0; + m_strutil.is_string(arg1_value, &strval); + if (strcmp(strval, "") == 0) { + is_arg1_emptyStr = true; + } + } + + if (!is_arg0_emptyStr && !is_arg1_emptyStr) { + var_eq_concat_map[deAliasNode][curr] = 1; + } + } + // TODO: collect unroll functions + /* + else if (isUnrollFunc(t, curr)) { + var_eq_unroll_map[deAliasNode][curr] = 1; + } + */ + + // curr = get_eqc_next(curr) + e_curr = ctx.get_enode(curr); + curr = e_curr->get_next()->get_owner(); + } + } + + } // for(varItor in strVarMap) + + // -------------------------------------------------- + // * collect aliasing relation among eq concats + // e.g EQC={concat1, concat2, concat3} + // concats_eq_Index_map[concat2] = concat1 + // concats_eq_Index_map[concat3] = concat1 + // -------------------------------------------------- + + /* + std::map concats_eq_Index_map; + std::map::iterator concatItor = concatMap.begin(); + for (; concatItor != concatMap.end(); concatItor++) { + // simplifyConcatToConst(t, concatItor->first); + + if (concats_eq_Index_map.find(concatItor->first) != concats_eq_Index_map.end()) + continue; + + Z3_ast aRoot = NULL; + Z3_ast curr = concatItor->first; + do { + if (isConcatFunc(t, curr)) { + if (aRoot == NULL) + aRoot = curr; + else + concats_eq_Index_map[curr] = aRoot; + } + curr = Z3_theory_get_eqc_next(t, curr); + } while (curr != concatItor->first); + } + + concatItor = concatMap.begin(); + for (; concatItor != concatMap.end(); concatItor++) { + Z3_ast deAliasConcat = NULL; + if (concats_eq_Index_map.find(concatItor->first) != concats_eq_Index_map.end()) + deAliasConcat = concats_eq_Index_map[concatItor->first]; + else + deAliasConcat = concatItor->first; + + // -------------------------------------------------- + // (3) concat_eq_constStr: + // e.g, concat(a,b) = "str1" + // -------------------------------------------------- + if (concat_eq_constStr_map.find(deAliasConcat) == concat_eq_constStr_map.end()) { + bool nodeHasEqcValue = false; + Z3_ast nodeValue = get_eqc_value(t, deAliasConcat, nodeHasEqcValue); + if (nodeHasEqcValue) + concat_eq_constStr_map[deAliasConcat] = nodeValue; + } + // -------------------------------------------------- + // (4) concat_eq_concat: + // e.g, concat(a,b) = concat("str1", c) /\ z = concat(a, b) /\ z = concat(e, f) + // -------------------------------------------------- + if (concat_eq_concat_map.find(deAliasConcat) == concat_eq_concat_map.end()) { + Z3_ast curr = deAliasConcat; + do { + if (isConcatFunc(t, curr)) { + // curr is not a concat that can be reduced + if (concatMap.find(curr) != concatMap.end()) { + concat_eq_concat_map[deAliasConcat][curr] = 1; + } + } + curr = Z3_theory_get_eqc_next(t, curr); + } while (curr != deAliasConcat); + } + } + */ + // TODO the rest NOT_IMPLEMENTED_YET(); } From 4a8ee88461ab27233c15a9af389bdea3743fc5bd Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 6 Nov 2015 13:43:54 -0500 Subject: [PATCH 055/562] ctx_dep_analysis() done, final_check() WIP --- src/smt/theory_str.cpp | 517 +++++++++++++++++++++++++++++++++++------ src/smt/theory_str.h | 4 + 2 files changed, 451 insertions(+), 70 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index c5ca630f9..7d9aaad7d 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2599,6 +2599,33 @@ void theory_str::classify_ast_by_type_in_positive_context(std::map & } } +inline expr * theory_str::get_alias_index_ast(std::map & aliasIndexMap, expr * node) { + if (aliasIndexMap.find(node) != aliasIndexMap.end()) + return aliasIndexMap[node]; + else + return node; +} + +inline expr * theory_str::getMostLeftNodeInConcat(expr * node) { + app * aNode = to_app(node); + if (!is_concat(aNode)) { + return node; + } else { + expr * concatArgL = aNode->get_arg(0); + return getMostLeftNodeInConcat(concatArgL); + } +} + +inline expr * theory_str::getMostRightNodeInConcat(expr * node) { + app * aNode = to_app(node); + if (!is_concat(aNode)) { + return node; + } else { + expr * concatArgR = aNode->get_arg(1); + return getMostRightNodeInConcat(concatArgR); + } +} + /* * Dependence analysis from current context assignment * - "freeVarMap" contains a set of variables that doesn't constrained by Concats. @@ -2775,67 +2802,429 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map concats_eq_Index_map; - std::map::iterator concatItor = concatMap.begin(); - for (; concatItor != concatMap.end(); concatItor++) { - // simplifyConcatToConst(t, concatItor->first); - - if (concats_eq_Index_map.find(concatItor->first) != concats_eq_Index_map.end()) - continue; - - Z3_ast aRoot = NULL; - Z3_ast curr = concatItor->first; - do { - if (isConcatFunc(t, curr)) { - if (aRoot == NULL) - aRoot = curr; - else - concats_eq_Index_map[curr] = aRoot; - } - curr = Z3_theory_get_eqc_next(t, curr); - } while (curr != concatItor->first); + std::map concats_eq_index_map; + std::map::iterator concatItor = concatMap.begin(); + for(; concatItor != concatMap.end(); ++concatItor) { + if (concats_eq_index_map.find(concatItor->first) != concats_eq_index_map.end()) { + continue; + } + expr * aRoot = NULL; + expr * curr = concatItor->first; + do { + if (is_concat(to_app(curr))) { + if (aRoot == NULL) { + aRoot = curr; + } else { + concats_eq_index_map[curr] = aRoot; + } + } + // curr = get_eqc_next(curr); + enode * e_curr = ctx.get_enode(curr); + curr = e_curr->get_next()->get_owner(); + } while (curr != concatItor->first); } concatItor = concatMap.begin(); - for (; concatItor != concatMap.end(); concatItor++) { - Z3_ast deAliasConcat = NULL; - if (concats_eq_Index_map.find(concatItor->first) != concats_eq_Index_map.end()) - deAliasConcat = concats_eq_Index_map[concatItor->first]; - else - deAliasConcat = concatItor->first; + for(; concatItor != concatMap.end(); ++concatItor) { + expr * deAliasConcat = NULL; + if (concats_eq_index_map.find(concatItor->first) != concats_eq_index_map.end()) { + deAliasConcat = concats_eq_index_map[concatItor->first]; + } else { + deAliasConcat = concatItor->first; + } - // -------------------------------------------------- - // (3) concat_eq_constStr: - // e.g, concat(a,b) = "str1" - // -------------------------------------------------- - if (concat_eq_constStr_map.find(deAliasConcat) == concat_eq_constStr_map.end()) { - bool nodeHasEqcValue = false; - Z3_ast nodeValue = get_eqc_value(t, deAliasConcat, nodeHasEqcValue); - if (nodeHasEqcValue) - concat_eq_constStr_map[deAliasConcat] = nodeValue; - } - // -------------------------------------------------- - // (4) concat_eq_concat: - // e.g, concat(a,b) = concat("str1", c) /\ z = concat(a, b) /\ z = concat(e, f) - // -------------------------------------------------- - if (concat_eq_concat_map.find(deAliasConcat) == concat_eq_concat_map.end()) { - Z3_ast curr = deAliasConcat; - do { - if (isConcatFunc(t, curr)) { - // curr is not a concat that can be reduced - if (concatMap.find(curr) != concatMap.end()) { - concat_eq_concat_map[deAliasConcat][curr] = 1; - } - } - curr = Z3_theory_get_eqc_next(t, curr); - } while (curr != deAliasConcat); - } + // (3) concat_eq_conststr, e.g. concat(a,b) = "str1" + if (concat_eq_constStr_map.find(deAliasConcat) == concat_eq_constStr_map.end()) { + bool nodeHasEqcValue = false; + expr * nodeValue = get_eqc_value(deAliasConcat, nodeHasEqcValue); + if (nodeHasEqcValue) { + concat_eq_constStr_map[deAliasConcat] = nodeValue; + } + } + + // (4) concat_eq_concat, e.g. + // concat(a,b) = concat("str1", c) AND z = concat(a,b) AND z = concat(e,f) + if (concat_eq_concat_map.find(deAliasConcat) == concat_eq_concat_map.end()) { + expr * curr = deAliasConcat; + do { + if (is_concat(to_app(curr))) { + // curr cannot be reduced + if (concatMap.find(curr) != concatMap.end()) { + concat_eq_concat_map[deAliasConcat][curr] = 1; + } + } + // curr = get_eqc_next(curr); + enode * e_curr = ctx.get_enode(curr); + curr = e_curr->get_next()->get_owner(); + } while (curr != deAliasConcat); + } + } + + // TODO this would be a great place to print some debugging information + + // TODO compute Contains + /* + if (containPairBoolMap.size() > 0) { + computeContains(t, aliasIndexMap, concats_eq_Index_map, var_eq_constStr_map, concat_eq_constStr_map, var_eq_concat_map); } */ - // TODO the rest - NOT_IMPLEMENTED_YET(); + // step 4: dependence analysis + + // (1) var = string constant + for (std::map::iterator itor = var_eq_constStr_map.begin(); + itor != var_eq_constStr_map.end(); ++itor) { + expr * var = get_alias_index_ast(aliasIndexMap, itor->first); + expr * strAst = itor->second; + depMap[var][strAst] = 1; + } + + // (2) var = concat + for (std::map >::iterator itor = var_eq_concat_map.begin(); + itor != var_eq_concat_map.end(); ++itor) { + expr * var = get_alias_index_ast(aliasIndexMap, itor->first); + for (std::map::iterator itor1 = itor->second.begin(); itor1 != itor->second.end(); ++itor1) { + expr * concat = itor1->first; + std::map inVarMap; + std::map inConcatMap; + std::map inUnrollMap; + classify_ast_by_type(concat, inVarMap, inConcatMap, inUnrollMap); + for (std::map::iterator itor2 = inVarMap.begin(); itor2 != inVarMap.end(); ++itor2) { + expr * varInConcat = get_alias_index_ast(aliasIndexMap, itor2->first); + if (!(depMap[var].find(varInConcat) != depMap[var].end() && depMap[var][varInConcat] == 1)) { + depMap[var][varInConcat] = 2; + } + } + } + } + + for (std::map >::iterator itor = var_eq_unroll_map.begin(); + itor != var_eq_unroll_map.end(); itor++) { + expr * var = get_alias_index_ast(aliasIndexMap, itor->first); + for (std::map::iterator itor1 = itor->second.begin(); itor1 != itor->second.end(); itor1++) { + expr * unrollFunc = itor1->first; + std::map inVarMap; + std::map inConcatMap; + std::map inUnrollMap; + classify_ast_by_type(unrollFunc, inVarMap, inConcatMap, inUnrollMap); + for (std::map::iterator itor2 = inVarMap.begin(); itor2 != inVarMap.end(); itor2++) { + expr * varInFunc = get_alias_index_ast(aliasIndexMap, itor2->first); + + STRACE("t_str_detail", tout << "var in unroll = " << + mk_ismt2_pp(itor2->first, m) << std::endl + << "dealiased var = " << mk_ismt2_pp(varInFunc) << std::endl;); + + // it's possible that we have both (Unroll $$_regVar_0 $$_unr_0) /\ (Unroll abcd $$_unr_0), + // while $$_regVar_0 = "abcd" + // have to exclude such cases + bool varHasValue = false; + get_eqc_value(varInFunc, varHasValue); + if (varHasValue) + continue; + + if (depMap[var].find(varInFunc) == depMap[var].end()) { + depMap[var][varInFunc] = 6; + } + } + } + } + + // (3) concat = string constant + for (std::map::iterator itor = concat_eq_constStr_map.begin(); + itor != concat_eq_constStr_map.end(); itor++) { + expr * concatAst = itor->first; + expr * constStr = itor->second; + std::map inVarMap; + std::map inConcatMap; + std::map inUnrollMap; + classify_ast_by_type(concatAst, inVarMap, inConcatMap, inUnrollMap); + for (std::map::iterator itor2 = inVarMap.begin(); itor2 != inVarMap.end(); itor2++) { + expr * varInConcat = get_alias_index_ast(aliasIndexMap, itor2->first); + if (!(depMap[varInConcat].find(constStr) != depMap[varInConcat].end() && depMap[varInConcat][constStr] == 1)) + depMap[varInConcat][constStr] = 3; + } + } + + // (4) equivalent concats + // - possibility 1 : concat("str", v1) = concat(concat(v2, v3), v4) = concat(v5, v6) + // ==> v2, v5 are constrained by "str" + // - possibliity 2 : concat(v1, "str") = concat(v2, v3) = concat(v4, v5) + // ==> v2, v4 are constrained by "str" + //-------------------------------------------------------------- + + std::map mostLeftNodes; + std::map mostRightNodes; + + std::map mLIdxMap; + std::map > mLMap; + std::map mRIdxMap; + std::map > mRMap; + std::set nSet; + + for (std::map >::iterator itor = concat_eq_concat_map.begin(); + itor != concat_eq_concat_map.end(); itor++) { + mostLeftNodes.clear(); + mostRightNodes.clear(); + + expr * mLConst = NULL; + expr * mRConst = NULL; + + for (std::map::iterator itor1 = itor->second.begin(); itor1 != itor->second.end(); itor1++) { + expr * concatNode = itor1->first; + expr * mLNode = getMostLeftNodeInConcat(concatNode); + const char * strval; + if (m_strutil.is_string(to_app(mLNode), & strval)) { + if (mLConst == NULL && strcmp(strval, "") != 0) { + mLConst = mLNode; + } + } else { + mostLeftNodes[mLNode] = concatNode; + } + + expr * mRNode = getMostRightNodeInConcat(concatNode); + if (m_strutil.is_string(to_app(mRNode), & strval)) { + if (mRConst == NULL && strcmp(strval, "") != 0) { + mRConst = mRNode; + } + } else { + mostRightNodes[mRNode] = concatNode; + } + } + + if (mLConst != NULL) { + // ------------------------------------------------------------------------------------- + // The left most variable in a concat is constrained by a constant string in eqc concat + // ------------------------------------------------------------------------------------- + // e.g. Concat(x, ...) = Concat("abc", ...) + // ------------------------------------------------------------------------------------- + for (std::map::iterator itor1 = mostLeftNodes.begin(); + itor1 != mostLeftNodes.end(); itor1++) { + expr * deVar = get_alias_index_ast(aliasIndexMap, itor1->first); + if (depMap[deVar].find(mLConst) == depMap[deVar].end() || depMap[deVar][mLConst] != 1) { + depMap[deVar][mLConst] = 4; + } + } + } + + { + // ------------------------------------------------------------------------------------- + // The left most variables in eqc concats are constrained by each other + // ------------------------------------------------------------------------------------- + // e.g. concat(x, ...) = concat(u, ...) = ... + // x and u are constrained by each other + // ------------------------------------------------------------------------------------- + nSet.clear(); + std::map::iterator itl = mostLeftNodes.begin(); + for (; itl != mostLeftNodes.end(); itl++) { + bool lfHasEqcValue = false; + get_eqc_value(itl->first, lfHasEqcValue); + if (lfHasEqcValue) + continue; + expr * deVar = get_alias_index_ast(aliasIndexMap, itl->first); + nSet.insert(deVar); + } + + if (nSet.size() > 1) { + int lId = -1; + for (std::set::iterator itor2 = nSet.begin(); itor2 != nSet.end(); itor2++) { + if (mLIdxMap.find(*itor2) != mLIdxMap.end()) { + lId = mLIdxMap[*itor2]; + break; + } + } + if (lId == -1) + lId = mLMap.size(); + for (std::set::iterator itor2 = nSet.begin(); itor2 != nSet.end(); itor2++) { + bool itorHasEqcValue = false; + get_eqc_value(*itor2, itorHasEqcValue); + if (itorHasEqcValue) + continue; + mLIdxMap[*itor2] = lId; + mLMap[lId].insert(*itor2); + } + } + } + + if (mRConst != NULL) { + for (std::map::iterator itor1 = mostRightNodes.begin(); + itor1 != mostRightNodes.end(); itor1++) { + expr * deVar = get_alias_index_ast(aliasIndexMap, itor1->first); + if (depMap[deVar].find(mRConst) == depMap[deVar].end() || depMap[deVar][mRConst] != 1) { + depMap[deVar][mRConst] = 5; + } + } + } + + { + nSet.clear(); + std::map::iterator itr = mostRightNodes.begin(); + for (; itr != mostRightNodes.end(); itr++) { + expr * deVar = get_alias_index_ast(aliasIndexMap, itr->first); + nSet.insert(deVar); + } + if (nSet.size() > 1) { + int rId = -1; + std::set::iterator itor2 = nSet.begin(); + for (; itor2 != nSet.end(); itor2++) { + if (mRIdxMap.find(*itor2) != mRIdxMap.end()) { + rId = mRIdxMap[*itor2]; + break; + } + } + if (rId == -1) + rId = mRMap.size(); + for (itor2 = nSet.begin(); itor2 != nSet.end(); itor2++) { + bool rHasEqcValue = false; + get_eqc_value(*itor2, rHasEqcValue); + if (rHasEqcValue) + continue; + mRIdxMap[*itor2] = rId; + mRMap[rId].insert(*itor2); + } + } + } + } + + // TODO this would be a great place to print the dependence map + + // step, errr, 5: compute free variables based on the dependence map + + // the case dependence map is empty, every var in VarMap is free + //--------------------------------------------------------------- + // remove L/R most var in eq concat since they are constrained with each other + std::map > lrConstrainedMap; + for (std::map >::iterator itor = mLMap.begin(); itor != mLMap.end(); itor++) { + for (std::set::iterator it1 = itor->second.begin(); it1 != itor->second.end(); it1++) { + std::set::iterator it2 = it1; + it2++; + for (; it2 != itor->second.end(); it2++) { + expr * n1 = *it1; + expr * n2 = *it2; + lrConstrainedMap[n1][n2] = 1; + lrConstrainedMap[n2][n1] = 1; + } + } + } + for (std::map >::iterator itor = mRMap.begin(); itor != mRMap.end(); itor++) { + for (std::set::iterator it1 = itor->second.begin(); it1 != itor->second.end(); it1++) { + std::set::iterator it2 = it1; + it2++; + for (; it2 != itor->second.end(); it2++) { + expr * n1 = *it1; + expr * n2 = *it2; + lrConstrainedMap[n1][n2] = 1; + lrConstrainedMap[n2][n1] = 1; + } + } + } + + if (depMap.size() == 0) { + std::map::iterator itor = strVarMap.begin(); + for (; itor != strVarMap.end(); itor++) { + expr * var = get_alias_index_ast(aliasIndexMap, itor->first); + if (lrConstrainedMap.find(var) == lrConstrainedMap.end()) { + freeVarMap[var] = 1; + } else { + int lrConstainted = 0; + std::map::iterator lrit = freeVarMap.begin(); + for (; lrit != freeVarMap.end(); lrit++) { + if (lrConstrainedMap[var].find(lrit->first) != lrConstrainedMap[var].end()) { + lrConstainted = 1; + break; + } + } + if (lrConstainted == 0) { + freeVarMap[var] = 1; + } + } + } + } else { + // if the keys in aliasIndexMap are not contained in keys in depMap, they are free + // e.g., x= y /\ x = z /\ t = "abc" + // aliasIndexMap[y]= x, aliasIndexMap[z] = x + // depMap t ~ "abc"(1) + // x should be free + std::map::iterator itor2 = strVarMap.begin(); + for (; itor2 != strVarMap.end(); itor2++) { + if (aliasIndexMap.find(itor2->first) != aliasIndexMap.end()) { + expr * var = aliasIndexMap[itor2->first]; + if (depMap.find(var) == depMap.end()) { + if (lrConstrainedMap.find(var) == lrConstrainedMap.end()) { + freeVarMap[var] = 1; + } else { + int lrConstainted = 0; + std::map::iterator lrit = freeVarMap.begin(); + for (; lrit != freeVarMap.end(); lrit++) { + if (lrConstrainedMap[var].find(lrit->first) != lrConstrainedMap[var].end()) { + lrConstainted = 1; + break; + } + } + if (lrConstainted == 0) { + freeVarMap[var] = 1; + } + } + } + } else if (aliasIndexMap.find(itor2->first) == aliasIndexMap.end()) { + // if a variable is not in aliasIndexMap and not in depMap, it's free + if (depMap.find(itor2->first) == depMap.end()) { + expr * var = itor2->first; + if (lrConstrainedMap.find(var) == lrConstrainedMap.end()) { + freeVarMap[var] = 1; + } else { + int lrConstainted = 0; + std::map::iterator lrit = freeVarMap.begin(); + for (; lrit != freeVarMap.end(); lrit++) { + if (lrConstrainedMap[var].find(lrit->first) != lrConstrainedMap[var].end()) { + lrConstainted = 1; + break; + } + } + if (lrConstainted == 0) { + freeVarMap[var] = 1; + } + } + } + } + } + + std::map >::iterator itor = depMap.begin(); + for (; itor != depMap.end(); itor++) { + for (std::map::iterator itor1 = itor->second.begin(); itor1 != itor->second.end(); itor1++) { + if (variable_set.find(itor1->first) != variable_set.end()) { // expr type = var + expr * var = get_alias_index_ast(aliasIndexMap, itor1->first); + // if a var is dep on itself and all dependence are type 2, it's a free variable + // e.g {y --> x(2), y(2), m --> m(2), n(2)} y,m are free + { + if (depMap.find(var) == depMap.end()) { + if (freeVarMap.find(var) == freeVarMap.end()) { + if (lrConstrainedMap.find(var) == lrConstrainedMap.end()) { + freeVarMap[var] = 1; + } else { + int lrConstainted = 0; + std::map::iterator lrit = freeVarMap.begin(); + for (; lrit != freeVarMap.end(); lrit++) { + if (lrConstrainedMap[var].find(lrit->first) != lrConstrainedMap[var].end()) { + lrConstainted = 1; + break; + } + } + if (lrConstainted == 0) { + freeVarMap[var] = 1; + } + } + + } else { + freeVarMap[var] = freeVarMap[var] + 1; + } + } + } + } + } + } + } + + return 0; } final_check_status theory_str::final_check_eh() { @@ -2855,10 +3244,6 @@ final_check_status theory_str::final_check_eh() { return FC_DONE; } - // TODO the rest... - NOT_IMPLEMENTED_YET(); - - /* // Check every variable to see if it's eq. to some string constant. // If not, mark it as free. bool needToAssignFreeVars = false; @@ -2877,18 +3262,10 @@ final_check_status theory_str::final_check_eh() { return FC_DONE; } - for (std::set::iterator it = free_variables.begin(); it != free_variables.end(); ++it) { - expr * var = *it; - if (internal_variable_set.find(var) != internal_variable_set.end()) { - TRACE("t_str", tout << "assigning arbitrary string to internal variable " << mk_ismt2_pp(var, m) << std::endl;); - app * val = m_strutil.mk_string("**unused**"); - assert_axiom(ctx.mk_eq_atom(var, val)); - } else { - NOT_IMPLEMENTED_YET(); // TODO free variable assignment from strTheory::cb_final_check() - } - } - return FC_CONTINUE; - */ + + + // TODO the rest... + NOT_IMPLEMENTED_YET(); } void theory_str::init_model(model_generator & mg) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 684526602..3d0f14ca7 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -153,6 +153,10 @@ namespace smt { void classify_ast_by_type_in_positive_context(std::map & varMap, std::map & concatMap, std::map & unrollMap); + expr * get_alias_index_ast(std::map & aliasIndexMap, expr * node); + expr * getMostLeftNodeInConcat(expr * node); + expr * getMostRightNodeInConcat(expr * node); + void dump_assignments(); public: theory_str(ast_manager & m); From ac8b5e6eae5cf97146fafd4f1cc450cda7dc4a27 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 6 Nov 2015 14:10:18 -0500 Subject: [PATCH 056/562] free variable WIP --- src/smt/theory_str.cpp | 160 ++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 3 + 2 files changed, 161 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 7d9aaad7d..cb31aedde 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3262,10 +3262,166 @@ final_check_status theory_str::final_check_eh() { return FC_DONE; } + // ----------------------------------------------------------- + // variables in freeVar are those not bouned by Concats + // classify variables in freeVarMap: + // (1) freeVar = unroll(r1, t1) + // (2) vars are not bounded by either concat or unroll + // ----------------------------------------------------------- + std::map > fv_unrolls_map; + std::set tmpSet; + expr * constValue = NULL; + for (std::map::iterator fvIt2 = freeVar_map.begin(); fvIt2 != freeVar_map.end(); fvIt2++) { + expr * var = fvIt2->first; + tmpSet.clear(); + get_eqc_allUnroll(var, constValue, tmpSet); + if (tmpSet.size() > 0) { + fv_unrolls_map[var] = tmpSet; + } + } + // erase var bounded by an unroll function from freeVar_map + for (std::map >::iterator fvIt3 = fv_unrolls_map.begin(); + fvIt3 != fv_unrolls_map.end(); fvIt3++) { + expr * var = fvIt3->first; + freeVar_map.erase(var); + } + // collect the case: + // * Concat(X, Y) = unroll(r1, t1) /\ Concat(X, Y) = unroll(r2, t2) + // concatEqUnrollsMap[Concat(X, Y)] = {unroll(r1, t1), unroll(r2, t2)} - // TODO the rest... - NOT_IMPLEMENTED_YET(); + std::map > concatEqUnrollsMap; + for (std::map >::iterator urItor = unrollGroup_map.begin(); + urItor != unrollGroup_map.end(); urItor++) { + expr * unroll = urItor->first; + expr * curr = unroll; + do { + if (is_concat(to_app(curr))) { + concatEqUnrollsMap[curr].insert(unroll); + concatEqUnrollsMap[curr].insert(unrollGroup_map[unroll].begin(), unrollGroup_map[unroll].end()); + } + enode * e_curr = ctx.get_enode(curr); + curr = e_curr->get_next()->get_owner(); + // curr = get_eqc_next(curr); + } while (curr != unroll); + } + + std::map > concatFreeArgsEqUnrollsMap; + std::set fvUnrollSet; + for (std::map >::iterator concatItor = concatEqUnrollsMap.begin(); + concatItor != concatEqUnrollsMap.end(); concatItor++) { + expr * concat = concatItor->first; + expr * concatArg1 = to_app(concat)->get_arg(0); + expr * concatArg2 = to_app(concat)->get_arg(1); + bool arg1Bounded = false; + bool arg2Bounded = false; + // arg1 + if (variable_set.find(concatArg1) != variable_set.end()) { + if (freeVar_map.find(concatArg1) == freeVar_map.end()) { + arg1Bounded = true; + } else { + fvUnrollSet.insert(concatArg1); + } + } else if (is_concat(to_app(concatArg1))) { + if (concatEqUnrollsMap.find(concatArg1) == concatEqUnrollsMap.end()) { + arg1Bounded = true; + } + } + // arg2 + if (variable_set.find(concatArg2) != variable_set.end()) { + if (freeVar_map.find(concatArg2) == freeVar_map.end()) { + arg2Bounded = true; + } else { + fvUnrollSet.insert(concatArg2); + } + } else if (is_concat(to_app(concatArg2))) { + if (concatEqUnrollsMap.find(concatArg2) == concatEqUnrollsMap.end()) { + arg2Bounded = true; + } + } + if (!arg1Bounded && !arg2Bounded) { + concatFreeArgsEqUnrollsMap[concat].insert( + concatEqUnrollsMap[concat].begin(), + concatEqUnrollsMap[concat].end()); + } + } + for (std::set::iterator vItor = fvUnrollSet.begin(); vItor != fvUnrollSet.end(); vItor++) { + freeVar_map.erase(*vItor); + } + + // Assign free variables + std::set fSimpUnroll; + + constValue = NULL; + + // TODO this would be a great place to print debugging information + + // TODO process_concat_eq_unroll() + /* + for (std::map >::iterator fvIt2 = concatFreeArgsEqUnrollsMap.begin(); + fvIt2 != concatFreeArgsEqUnrollsMap.end(); fvIt2++) { + expr * concat = fvIt2->first; + for (std::set::iterator urItor = fvIt2->second.begin(); urItor != fvIt2->second.end(); urItor++) { + Z3_ast unroll = *urItor; + processConcatEqUnroll(concat, unroll); + } + } + */ + + // -------- + // experimental free variable assignment - begin + // * special handling for variables that are not used in concat + // -------- + bool testAssign = true; + if (!testAssign) { + for (std::map::iterator fvIt = freeVar_map.begin(); fvIt != freeVar_map.end(); fvIt++) { + expr * freeVar = fvIt->first; + /* + std::string vName = std::string(Z3_ast_to_string(ctx, freeVar)); + if (vName.length() >= 9 && vName.substr(0, 9) == "$$_regVar") { + continue; + } + */ + // TODO if this variable represents a regular expression, continue + expr * toAssert = gen_len_val_options_for_free_var(freeVar, NULL, ""); + if (toAssert != NULL) { + assert_axiom(toAssert); + } + } + } else { + process_free_var(freeVar_map); + } + // experimental free variable assignment - end + + // TODO more unroll stuff + /* + for (std::map >::iterator fvIt1 = fv_unrolls_map.begin(); + fvIt1 != fv_unrolls_map.end(); fvIt1++) { + Z3_ast var = fvIt1->first; + fSimpUnroll.clear(); + get_eqc_simpleUnroll(t, var, constValue, fSimpUnroll); + if (fSimpUnroll.size() == 0) { + genAssignUnrollReg(t, fv_unrolls_map[var]); + } else { + Z3_ast toAssert = genAssignUnrollStr2Reg(t, var, fSimpUnroll); + if (toAssert != NULL) { + addAxiom(t, toAssert, __LINE__); + } + } + } + */ + + return FC_CONTINUE; // since by this point we've added axioms +} + +expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTesterInCbEq, std::string lenTesterValue) { + // TODO + NOT_IMPLEMENTED_YET(); +} + +void theory_str::process_free_var(std::map & freeVar_map) { + // TODO this one first + NOT_IMPLEMENTED_YET(); } void theory_str::init_model(model_generator & mg) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 3d0f14ca7..60c2c3a8e 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -153,6 +153,9 @@ namespace smt { void classify_ast_by_type_in_positive_context(std::map & varMap, std::map & concatMap, std::map & unrollMap); + expr * gen_len_val_options_for_free_var(expr * freeVar, expr * lenTesterInCbEq, std::string lenTesterValue); + void process_free_var(std::map & freeVar_map); + expr * get_alias_index_ast(std::map & aliasIndexMap, expr * node); expr * getMostLeftNodeInConcat(expr * node); expr * getMostRightNodeInConcat(expr * node); From e9b31f29954ccc2252a5d122bcbe25b9f1601bc1 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 6 Nov 2015 14:13:38 -0500 Subject: [PATCH 057/562] temporarily patched in a get_eqc_allUnroll() implementation --- src/smt/theory_str.cpp | 26 +++++++++++++++++++++++++- src/smt/theory_str.h | 4 ++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index cb31aedde..0965e7873 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3263,7 +3263,7 @@ final_check_status theory_str::final_check_eh() { } // ----------------------------------------------------------- - // variables in freeVar are those not bouned by Concats + // variables in freeVar are those not bounded by Concats // classify variables in freeVarMap: // (1) freeVar = unroll(r1, t1) // (2) vars are not bounded by either concat or unroll @@ -3424,6 +3424,30 @@ void theory_str::process_free_var(std::map & freeVar_map) { NOT_IMPLEMENTED_YET(); } +/* + * Collect all unroll functions + * and constant string in eqc of node n + */ +void theory_str::get_eqc_allUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet) { + constStr = NULL; + unrollFuncSet.clear(); + context & ctx = get_context(); + + expr * curr = n; + do { + if (is_string(to_app(curr))) { + constStr = curr; + } else if (false) /*(td->Unroll == Z3_get_app_decl(ctx, Z3_to_app(ctx, curr)))*/ { // TODO + if (unrollFuncSet.find(curr) == unrollFuncSet.end()) { + unrollFuncSet.insert(curr); + } + } + enode * e_curr = ctx.get_enode(curr); + curr = e_curr->get_next()->get_owner(); + // curr = get_eqc_next(t, curr); + } while (curr != n); +} + void theory_str::init_model(model_generator & mg) { TRACE("t_str", tout << "initializing model" << std::endl; display(tout);); m_factory = alloc(str_value_factory, get_manager(), get_family_id()); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 60c2c3a8e..86f45aea0 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -160,6 +160,10 @@ namespace smt { expr * getMostLeftNodeInConcat(expr * node); expr * getMostRightNodeInConcat(expr * node); + // strRegex + + void get_eqc_allUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet); + void dump_assignments(); public: theory_str(ast_manager & m); From a9b8707d48fb8b474a6b49ba68d1f7e9fdf7d250 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 9 Nov 2015 15:14:34 -0500 Subject: [PATCH 058/562] possibly found a way to do get_parents() --- src/smt/theory_str.cpp | 76 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 0965e7873..da5c858b8 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3420,7 +3420,81 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe } void theory_str::process_free_var(std::map & freeVar_map) { - // TODO this one first + context & ctx = get_context(); + ast_manager & m = get_manager(); + + std::set eqcRepSet; + std::set leafVarSet; + std::map > aloneVars; + + for (std::map::iterator fvIt = freeVar_map.begin(); fvIt != freeVar_map.end(); fvIt++) { + expr * freeVar = fvIt->first; + /* + std::string vName = std::string(Z3_ast_to_string(ctx, freeVar)); + if (vName.length() >= 9 && vName.substr(0, 9) == "$$_regVar") { + continue; + } + */ + // TODO skip all regular expression vars + + // Iterate the EQC of freeVar, its eqc variable should not be in the eqcRepSet. + // If found, have to filter it out + std::set eqVarSet; + get_var_in_eqc(freeVar, eqVarSet); + bool duplicated = false; + expr * dupVar = NULL; + for (std::set::iterator itorEqv = eqVarSet.begin(); itorEqv != eqVarSet.end(); itorEqv++) { + if (eqcRepSet.find(*itorEqv) != eqcRepSet.end()) { + duplicated = true; + dupVar = *itorEqv; + break; + } + } + if (duplicated && dupVar != NULL) { + STRACE("t_str_detail", tout << "Duplicated free variable found:" << mk_ismt2_pp(freeVar, m) + << " = " << mk_ismt2_pp(dupVar, m) << " (SKIP)" << std::endl;); + continue; + } else { + eqcRepSet.insert(freeVar); + } + } + + for (std::set::iterator fvIt = eqcRepSet.begin(); fvIt != eqcRepSet.end(); fvIt++) { + bool standAlone = true; + expr * freeVar = *fvIt; + // has length constraint initially + if (input_var_in_len.find(freeVar) != input_var_in_len.end()) { + standAlone = false; + } + // iterate parents + if (standAlone) { + // I hope this works! + enode * e_freeVar = ctx.get_enode(freeVar); + enode_vector::iterator it = e_freeVar->begin_parents(); + for (; it != e_freeVar->end_parents(); ++it) { + expr * parentAst = (*it)->get_owner(); + if (is_concat(to_app(parentAst))) { + standAlone = false; + break; + } + } + } + + if (standAlone) { + // TODO + // int lenValue = getLenValue(freeVar); + int lenValue = -1; + if (lenValue != -1) { + leafVarSet.insert(freeVar); + } else { + aloneVars[lenValue].insert(freeVar); + } + } else { + leafVarSet.insert(freeVar); + } + } + + // TODO the rest NOT_IMPLEMENTED_YET(); } From 0178872a19c0ad0cec339c2b9ebbe99453aa2c74 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 9 Nov 2015 15:33:52 -0500 Subject: [PATCH 059/562] completed process_free_var(), still WIP working on gen_len_val_options_for_free_var() --- src/smt/theory_str.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- src/smt/theory_str.h | 3 +++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index da5c858b8..a235936d6 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2385,6 +2385,14 @@ void theory_str::set_up_axioms(expr * ex) { if (is_concat(ap)) { // if ex is a concat, set up concat axioms later m_concat_axiom_todo.push_back(n); + } else if (is_strlen(ap)) { + // if the argument is a variable, + // keep track of this for later, we'll need it during model gen + expr * var = ap->get_arg(0); + app * aVar = to_app(var); + if (aVar->get_num_args() == 0 && !is_string(aVar)) { + input_var_in_len.insert(var); + } } else if (ap->get_num_args() == 0 && !is_string(ap)) { // if ex is a variable, add it to our list of variables TRACE("t_str_detail", tout << "tracking variable" << std::endl;); @@ -3419,6 +3427,20 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe NOT_IMPLEMENTED_YET(); } +void theory_str::get_var_in_eqc(expr * n, std::set & varSet) { + context & ctx = get_context(); + + expr * eqcNode = n; + do { + if (variable_set.find(eqcNode) != variable_set.end()) { + varSet.insert(eqcNode); + } + enode * e_eqc = ctx.get_enode(eqcNode); + eqcNode = e_eqc->get_next()->get_owner(); + // eqcNode = Z3_theory_get_eqc_next(t, eqcNode); + } while (eqcNode != n); +} + void theory_str::process_free_var(std::map & freeVar_map) { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -3494,8 +3516,22 @@ void theory_str::process_free_var(std::map & freeVar_map) { } } - // TODO the rest - NOT_IMPLEMENTED_YET(); + // TODO here's a great place for debugging info + + for(std::set::iterator itor1 = leafVarSet.begin(); + itor1 != leafVarSet.end(); ++itor1) { + expr * toAssert = gen_len_val_options_for_free_var(*itor1, NULL, ""); + assert_axiom(toAssert); + } + + for (std::map >::iterator mItor = aloneVars.begin(); + mItor != aloneVars.end(); ++mItor) { + std::set::iterator itor2 = mItor->second.begin(); + for(; itor2 != mItor->second.end(); ++itor2) { + expr * toAssert = gen_len_val_options_for_free_var(*itor2, NULL, ""); + assert_axiom(toAssert); + } + } } /* diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 86f45aea0..a4a89f947 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -82,6 +82,8 @@ namespace smt { std::set variable_set; std::set internal_variable_set; + + std::set input_var_in_len; protected: void assert_axiom(expr * e); void assert_implication(expr * premise, expr * conclusion); @@ -159,6 +161,7 @@ namespace smt { expr * get_alias_index_ast(std::map & aliasIndexMap, expr * node); expr * getMostLeftNodeInConcat(expr * node); expr * getMostRightNodeInConcat(expr * node); + void get_var_in_eqc(expr * n, std::set & varSet); // strRegex From 6374d6316017ff772b2930c104215a39142917bc Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 9 Nov 2015 16:11:00 -0500 Subject: [PATCH 060/562] gen_len_val_options_for_free_var() WIP --- src/smt/theory_str.cpp | 224 ++++++++++++++++++++++++++++++++++++++--- src/smt/theory_str.h | 8 ++ 2 files changed, 219 insertions(+), 13 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index a235936d6..c485c40ff 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -221,6 +221,24 @@ app * theory_str::mk_int(int n) { return m_autil.mk_numeral(rational(n), true); } +expr * theory_str::mk_internal_lenTest_var(expr * node, int lTries) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + std::stringstream ss; + ss << "$$_len_" << mk_ismt2_pp(node, m) << "_" << lTries; + std::string name = ss.str(); + return mk_str_var(name); + + /* + Z3_context ctx = Z3_theory_get_context(t); + std::stringstream ss; + ss << "$$_len_" << Z3_ast_to_string(ctx, node) << "_" << lTries; + std::string name = ss.str(); + return my_mk_str_var(t, name.c_str()); + */ +} + app * theory_str::mk_internal_xor_var() { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -239,17 +257,26 @@ app * theory_str::mk_internal_xor_var() { return a; } -/* - Z3_context ctx = Z3_theory_get_context(t); - PATheoryData * td = (PATheoryData *) Z3_theory_get_ext_data(t); - std::stringstream ss; - ss << tmpStringVarCount; - tmpStringVarCount++; - std::string name = "$$_str" + ss.str(); - Z3_ast varAst = mk_var(ctx, name.c_str(), td->String); - nonEmptyStrVarAxiom(t, varAst, __LINE__); - return varAst; -*/ +app * theory_str::mk_str_var(std::string name) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + sort * string_sort = m.mk_sort(m_strutil.get_family_id(), STRING_SORT); + char * new_buffer = alloc_svect(char, name.length() + 1); + strcpy(new_buffer, name.c_str()); + symbol sym(new_buffer); + + app * a = m.mk_const(m.mk_const_decl(sym, string_sort)); + + // I have a hunch that this may not get internalized for free... + SASSERT(ctx.get_enode(a) != NULL); + m_basicstr_axiom_todo.push_back(ctx.get_enode(a)); + + variable_set.insert(a); + internal_variable_set.insert(a); + + return a; +} app * theory_str::mk_nonempty_str_var() { context & ctx = get_context(); @@ -3422,9 +3449,180 @@ final_check_status theory_str::final_check_eh() { return FC_CONTINUE; // since by this point we've added axioms } +inline std::string int_to_string(int i) { + std::stringstream ss; + ss << i; + return ss.str(); +} + +expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tries) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + expr * freeVarLen = mk_strlen(freeVar); + + ptr_vector orList; + ptr_vector andList; + + int distance = 3; + int l = (tries - 1) * distance; + int h = tries * distance; + + for (int i = l; i < h; ++i) { + orList.push_back(m.mk_eq(indicator, m_strutil.mk_string(int_to_string(i).c_str()))); + andList.push_back(m.mk_eq(orList[orList.size() - 1], m.mk_eq(freeVarLen, mk_int(i)))); + } + + orList.push_back(m.mk_eq(indicator, m_strutil.mk_string("more"))); + andList.push_back(m.mk_eq(orList[orList.size() - 1], m_autil.mk_ge(freeVarLen, mk_int(h)))); + + expr ** or_items = alloc_svect(expr*, orList.size()); + expr ** and_items = alloc_svect(expr*, andList.size() + 1); + + for (int i = 0; i < orList.size(); ++i) { + or_items[i] = orList[i]; + } + + and_items[0] = m.mk_or(orList.size(), or_items); + for(int i = 0; i < andList.size(); ++i) { + and_items[i+1] = andList[i]; + } + expr * lenTestAssert = m.mk_and(andList.size() + 1, and_items); + + expr * assertL = NULL; + int testerCount = tries - 1; + if (testerCount > 0) { + expr ** and_items_LHS = alloc_svect(expr*, testerCount); + expr * moreAst = m_strutil.mk_string("more"); + for (int i = 0; i < testerCount; ++i) { + and_items_LHS[i] = m.mk_eq(fvar_lenTester_map[freeVar][i], moreAst); + } + if (testerCount == 1) { + assertL = and_items_LHS[0]; + } else { + assertL = m.mk_and(testerCount, and_items_LHS); + } + } + + if (assertL != NULL) { + // return the axiom (assertL -> lenTestAssert) + // would like to use mk_implies() here but... + expr_ref lenTestAssert(m.mk_or(m.mk_not(assertL), lenTestAssert), m); + } + + return lenTestAssert; + +} + +// ----------------------------------------------------------------------------------------------------- +// True branch will be taken in final_check: +// - When we discover a variable is "free" for the first time +// lenTesterInCbEq = NULL +// lenTesterValue = "" +// False branch will be taken when invoked by new_eq_eh(). +// - After we set up length tester for a "free" var in final_check, +// when the tester is assigned to some value (e.g. "more" or "4"), +// lenTesterInCbEq != NULL, and its value will be passed by lenTesterValue +// The difference is that in new_eq_eh(), lenTesterInCbEq and its value have NOT been put into a same eqc +// ----------------------------------------------------------------------------------------------------- expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTesterInCbEq, std::string lenTesterValue) { - // TODO - NOT_IMPLEMENTED_YET(); + + context & ctx = get_context(); + ast_manager & m = get_manager(); + + STRACE("t_str_detail", tout << "gen for free var " << mk_ismt2_pp(freeVar, m) << std::endl;); + // no length assertions for this free variable have ever been added. + if (fvar_len_count_map.find(freeVar) == fvar_len_count_map.end()) { + fvar_len_count_map[freeVar] = 1; + unsigned int testNum = fvar_len_count_map[freeVar]; + expr * indicator = mk_internal_lenTest_var(freeVar, testNum); + fvar_lenTester_map[freeVar].push_back(indicator); + lenTester_fvar_map[indicator] = freeVar; + + expr * lenTestAssert = gen_len_test_options(freeVar, indicator, testNum); + return lenTestAssert; + } else { + /* + Z3_ast effectiveLenInd = NULL; + std::string effectiveLenIndiStr = ""; + int lenTesterCount = (int) fvarLenTesterMap[freeVar].size(); + + int i = 0; + for (; i < lenTesterCount; i++) { + Z3_ast len_indicator_pre = fvarLenTesterMap[freeVar][i]; + bool indicatorHasEqcValue = false; + Z3_ast len_indicator_value = get_eqc_value(t, len_indicator_pre, indicatorHasEqcValue); +#ifdef DEBUGLOG + __debugPrint(logFile, "* length indicator "); + printZ3Node(t, len_indicator_pre); + __debugPrint(logFile, " = "); + printZ3Node(t, len_indicator_value); + __debugPrint(logFile, "\n"); +#endif + if (indicatorHasEqcValue) { + std::string len_pIndiStr = getConstStrValue(t, len_indicator_value); + if (len_pIndiStr != "more") { + effectiveLenInd = len_indicator_pre; + effectiveLenIndiStr = len_pIndiStr; + break; + } + } else { + if (lenTesterInCbEq != len_indicator_pre) { +#ifdef DEBUGLOG + __debugPrint(logFile, "\n>> *Warning*: length indicator: "); + printZ3Node(t, len_indicator_pre); + __debugPrint(logFile, " doesn't have an EQC value. i = %d, lenTesterCount = %d\n", i , lenTesterCount); +#endif + if (i > 0) { + effectiveLenInd = fvarLenTesterMap[freeVar][i - 1]; + if (effectiveLenInd == lenTesterInCbEq) { + effectiveLenIndiStr = lenTesterValue; + } else { + bool effectiveHasEqcValue = false; + effectiveLenIndiStr = getConstStrValue(t, get_eqc_value(t, effectiveLenInd, effectiveHasEqcValue)); + } + } + break; + } + // lenTesterInCbEq == len_indicator_pre + else { + if (lenTesterValue != "more") { + effectiveLenInd = len_indicator_pre; + effectiveLenIndiStr = lenTesterValue; + break; + } + } + } + } + + if (effectiveLenIndiStr == "more" || effectiveLenIndiStr == "") { + Z3_ast indicator = NULL; + unsigned int testNum = 0; + + __debugPrint(logFile, "\n>> effectiveLenIndiStr = %s, i = %d, lenTesterCount = %d\n", effectiveLenIndiStr.c_str(), i, lenTesterCount); + + if (i == lenTesterCount) { + fvarLenCountMap[freeVar] = fvarLenCountMap[freeVar] + 1; + testNum = fvarLenCountMap[freeVar]; + indicator = my_mk_internal_lenTest_var(t, freeVar, testNum); + fvarLenTesterMap[freeVar].push_back(indicator); + lenTesterFvarMap[indicator] = freeVar; + } else { + indicator = fvarLenTesterMap[freeVar][i]; + testNum = i + 1; + } + Z3_ast lenTestAssert = genLenTestOptions(t, freeVar, indicator, testNum); + return lenTestAssert; + } else { + // length is fixed + Z3_ast valueAssert = genFreeVarOptions(t, freeVar, effectiveLenInd, effectiveLenIndiStr, NULL, ""); + return valueAssert; + } + */ + + // TODO + NOT_IMPLEMENTED_YET(); + } // fVarLenCountMap.find(...) } void theory_str::get_var_in_eqc(expr * n, std::set & varSet) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index a4a89f947..b7d93ef54 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -84,6 +84,11 @@ namespace smt { std::set internal_variable_set; std::set input_var_in_len; + + std::map fvar_len_count_map; + std::map > fvar_lenTester_map; + std::map lenTester_fvar_map; + protected: void assert_axiom(expr * e); void assert_implication(expr * premise, expr * conclusion); @@ -99,6 +104,7 @@ namespace smt { void add_cut_info_merge(expr * destNode, int slevel, expr * srcNode); bool has_self_cut(expr * n1, expr * n2); + app * mk_str_var(std::string name); app * mk_nonempty_str_var(); app * mk_internal_xor_var(); @@ -155,8 +161,10 @@ namespace smt { void classify_ast_by_type_in_positive_context(std::map & varMap, std::map & concatMap, std::map & unrollMap); + expr * mk_internal_lenTest_var(expr * node, int lTries); expr * gen_len_val_options_for_free_var(expr * freeVar, expr * lenTesterInCbEq, std::string lenTesterValue); void process_free_var(std::map & freeVar_map); + expr * gen_len_test_options(expr * freeVar, expr * indicator, int tries); expr * get_alias_index_ast(std::map & aliasIndexMap, expr * node); expr * getMostLeftNodeInConcat(expr * node); From 3a404c248d49a700ff50ed09db42859f4c660763 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 10 Nov 2015 12:40:01 -0500 Subject: [PATCH 061/562] gen_free_var_options() WIP --- src/smt/theory_str.cpp | 124 +++++++++++++++++++++++++++++------------ src/smt/theory_str.h | 2 + 2 files changed, 90 insertions(+), 36 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index c485c40ff..2101fe7a5 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3455,6 +3455,65 @@ inline std::string int_to_string(int i) { return ss.str(); } +expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, + std::string len_valueStr, expr * valTesterInCbEq, std::string valTesterValueStr) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + int len = atoi(len_valueStr.c_str()); + + if (fvar_valueTester_map[freeVar].find(len) == fvar_valueTester_map[freeVar].end()) { + int tries = 0; + expr * val_indicator = mk_internal_valTest_var(freeVar, len, tries); + valueTester_fvar_map[val_indicator] = freeVar; + fvar_valueTester_map[freeVar][len].push_back(std::make_pair(sLevel, val_indicator)); + print_value_tester_list(fvar_valueTester_map[freeVar][len]); + return gen_val_options(freeVar, len_indicator, val_indicator, len_valueStr, tries); + } else { + // go through all previous value testers + // If some doesn't have an eqc value, add its assertion again. + int testerTotal = fvar_valueTester_map[freeVar][len].size(); + int i = 0; + for (; i < testerTotal; i++) { + expr * aTester = fvarValueTesterMap[freeVar][len][i].second; + + if (aTester == valTesterInCbEq) { + break; + } + + bool anEqcHasValue = false; + // Z3_ast anEqc = get_eqc_value(t, aTester, anEqcHasValue); + get_eqc_value(aTester, anEqcHasValue); + if (!anEqcHasValue) { + STRACE("t_str_detail", "value tester " << mk_ismt2_pp(aTester, m) + << "doesn't have an equivalence class value." << std::endl;); + + expr_ref makeupAssert(gen_val_options(freeVar, len_indicator, aTester, len_valueStr, i), m); + + STRACE("t_str_detail", "var: " << mk_ismt2_pp(freeVar, m) << std::endl + << mk_ismt2_pp(makeupAssert, m) << std::endl;); + assert_axiom(makeupAssert); + } + } + + if (valTesterValueStr == "more") { + expr * valTester = NULL; + if (i + 1 < testerTotal) { + valTester = fvar_valueTester_map[freeVar][len][i + 1].second; + } else { + valTester = mk_internal_valTest_var(freeVar, len, i + 1); + valueTester_fvar_map[valTester] = freeVar; + fvar_valueTester_map[freeVar][len].push_back(std::make_pair(sLevel, valTester)); + print_value_tester_list(fvar_valueTester_map[freeVar][len]); + } + expr_ref nextAssert(gen_val_options(freeVar, len_indicator, valTester, len_valueStr, i + 1), m); + return nextAssert; + } + + return NULL; + } +} + expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tries) { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -3542,25 +3601,22 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe expr * lenTestAssert = gen_len_test_options(freeVar, indicator, testNum); return lenTestAssert; } else { - /* - Z3_ast effectiveLenInd = NULL; + + expr * effectiveLenInd = NULL; std::string effectiveLenIndiStr = ""; - int lenTesterCount = (int) fvarLenTesterMap[freeVar].size(); + int lenTesterCount = (int) fvar_lenTester_map[freeVar].size(); int i = 0; - for (; i < lenTesterCount; i++) { - Z3_ast len_indicator_pre = fvarLenTesterMap[freeVar][i]; + for (; i < lenTesterCount; ++i) { + expr * len_indicator_pre = fvar_lenTester_map[freeVar][i]; bool indicatorHasEqcValue = false; - Z3_ast len_indicator_value = get_eqc_value(t, len_indicator_pre, indicatorHasEqcValue); -#ifdef DEBUGLOG - __debugPrint(logFile, "* length indicator "); - printZ3Node(t, len_indicator_pre); - __debugPrint(logFile, " = "); - printZ3Node(t, len_indicator_value); - __debugPrint(logFile, "\n"); -#endif + expr * len_indicator_value = get_eqc_value(len_indicator_pre, indicatorHasEqcValue); + STRACE("t_str_detail", tout << "length indicator " << mk_ismt2_pp(len_indicator_pre, m) << + " = " << mk_ismt2_pp(len_indicator_value, m) << std::endl;); if (indicatorHasEqcValue) { - std::string len_pIndiStr = getConstStrValue(t, len_indicator_value); + const char * val = 0; + m_strutil.is_string(len_indicator_value, & val); + std::string len_pIndiStr(val); if (len_pIndiStr != "more") { effectiveLenInd = len_indicator_pre; effectiveLenIndiStr = len_pIndiStr; @@ -3568,18 +3624,18 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe } } else { if (lenTesterInCbEq != len_indicator_pre) { -#ifdef DEBUGLOG - __debugPrint(logFile, "\n>> *Warning*: length indicator: "); - printZ3Node(t, len_indicator_pre); - __debugPrint(logFile, " doesn't have an EQC value. i = %d, lenTesterCount = %d\n", i , lenTesterCount); -#endif + STRACE("t_str", tout << "WARNING: length indicator " << mk_ismt2_pp(len_indicator_pre, m) + << " does not have an equivalence class value." + << " i = " << i << ", lenTesterCount = " << lenTesterCount << std::endl;); if (i > 0) { effectiveLenInd = fvarLenTesterMap[freeVar][i - 1]; if (effectiveLenInd == lenTesterInCbEq) { effectiveLenIndiStr = lenTesterValue; } else { bool effectiveHasEqcValue = false; - effectiveLenIndiStr = getConstStrValue(t, get_eqc_value(t, effectiveLenInd, effectiveHasEqcValue)); + const char * val = 0; + m_strutil.is_string(get_eqc_value(effectiveLenInd, effectiveHasEqcValue), & val); + effectiveLenIndiStr = val; } } break; @@ -3592,36 +3648,32 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe break; } } - } - } - + } // !indicatorHasEqcValue + } // for (i : [0..lenTesterCount-1]) if (effectiveLenIndiStr == "more" || effectiveLenIndiStr == "") { - Z3_ast indicator = NULL; + expr * indicator = NULL; unsigned int testNum = 0; - __debugPrint(logFile, "\n>> effectiveLenIndiStr = %s, i = %d, lenTesterCount = %d\n", effectiveLenIndiStr.c_str(), i, lenTesterCount); + STRACE("t_str", tout << "effectiveLenIndiStr = " << effectiveLenIndiStr + << ", i = " << i << ", lenTesterCount = " << lenTesterCount << std::endl;); if (i == lenTesterCount) { - fvarLenCountMap[freeVar] = fvarLenCountMap[freeVar] + 1; - testNum = fvarLenCountMap[freeVar]; - indicator = my_mk_internal_lenTest_var(t, freeVar, testNum); - fvarLenTesterMap[freeVar].push_back(indicator); - lenTesterFvarMap[indicator] = freeVar; + fvar_len_count_map[freeVar] = fvar_len_count_map[freeVar] + 1; + testNum = fvar_len_count_map[freeVar]; + indicator = mk_internal_lenTest_var(freeVar, testNum); + fvar_lenTester_map[freeVar].push_back(indicator); + lenTester_fvar_map[indicator] = freeVar; } else { indicator = fvarLenTesterMap[freeVar][i]; testNum = i + 1; } - Z3_ast lenTestAssert = genLenTestOptions(t, freeVar, indicator, testNum); + expr_ref lenTestAssert(gen_len_test_options(freeVar, indicator, testNum), m); return lenTestAssert; } else { // length is fixed - Z3_ast valueAssert = genFreeVarOptions(t, freeVar, effectiveLenInd, effectiveLenIndiStr, NULL, ""); + expr_ref valueAssert(gen_free_var_options(freeVar, effectiveLenInd, effectiveLenIndiStr, NULL, ""), m); return valueAssert; } - */ - - // TODO - NOT_IMPLEMENTED_YET(); } // fVarLenCountMap.find(...) } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index b7d93ef54..bd26f2564 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -165,6 +165,8 @@ namespace smt { expr * gen_len_val_options_for_free_var(expr * freeVar, expr * lenTesterInCbEq, std::string lenTesterValue); void process_free_var(std::map & freeVar_map); expr * gen_len_test_options(expr * freeVar, expr * indicator, int tries); + expr * gen_free_var_options(expr * freeVar, expr * len_indicator, + std::string len_valueStr, expr * valTesterInCbEq, std::string valTesterValueStr); expr * get_alias_index_ast(std::map & aliasIndexMap, expr * node); expr * getMostLeftNodeInConcat(expr * node); From 8b538f584031907a2802551d36bcbfaf256b9c4a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 11 Nov 2015 15:34:11 -0500 Subject: [PATCH 062/562] started gen_val_options() WIP --- src/smt/theory_str.cpp | 146 ++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 7 ++ 2 files changed, 152 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 2101fe7a5..f86f921de 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -239,6 +239,23 @@ expr * theory_str::mk_internal_lenTest_var(expr * node, int lTries) { */ } +expr * theory_str::mk_internal_valTest_var(expr * node, int len, int vTries) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + std::stringstream ss; + ss << "$$_val_" << mk_ismt2_pp(node, m) << "_" << len << "_" << vTries; + std::string name = ss.str(); + return mk_str_var(name); + + /* + Z3_context ctx = Z3_theory_get_context(t); + std::stringstream ss; + ss << "$$_val_" << Z3_ast_to_string(ctx, node) << "_" << len << "_" << vTries; + std::string name = ss.str(); + return my_mk_str_var(t, name.c_str()); + */ +} + app * theory_str::mk_internal_xor_var() { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -3455,6 +3472,133 @@ inline std::string int_to_string(int i) { return ss.str(); } +inline std::string longlong_to_string(long long i) { + std::stringstream ss; + ss << i; + return ss.str(); +} + +void theory_str::print_value_tester_list(std::vector > & testerList) { + ast_manager & m = get_manager(); + STRACE("t_str_detail", + int ss = testerList.size(); + tout << "valueTesterList = {"; + for (int i = 0; i < ss; ++i) { + if (i % 4 == 0) { + tout << std::endl; + } + tout << "(" << testerList[i].first << ", "; + tout << mk_ismt2_pp(testerList[i].second, m); + tout << "), "; + } + tout << std::endl << "}" << std::endl; + ); +} + +expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * val_indicator, + std::string lenStr, int tries) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + int distance = 32; + + // ---------------------------------------------------------------------------------------- + // generate value options encoding + // encoding is a vector of size (len + 1) + // e.g, len = 2, + // encoding {1, 2, 0} means the value option is "charSet[2]"."charSet[1]" + // the last item in the encoding indicates whether the whole space is covered + // for example, if the charSet = {a, b}. All valid encodings are + // {0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {1, 1, 0} + // if add 1 to the last one, we get + // {0, 0, 1} + // the last item "1" shows this is not a valid encoding, and we have covered all space + // ---------------------------------------------------------------------------------------- + int len = atoi(lenStr.c_str()); + bool coverAll = false; + std::vector > options; + std::vector base; + + if (tries == 0) { + base = std::vector(len + 1, 0); + coverAll = false; + } else { + expr * lastestValIndi = fvar_valueTester_map[freeVar][len][tries - 1].second; + STRACE("t_str_detail", tout << "last value tester = " << mk_ismt2_pp(lastestValIndi, m) << std::endl;); + coverAll = get_next_val_encode(valRangeMap[lastestValIndi], base); + } + + long long l = (tries) * distance; + long long h = l; + for (int i = 0; i < distance; i++) { + if (coverAll) + break; + options.push_back(base); + h++; + coverAll = getNextValEncode(options[options.size() - 1], base); + } + valRangeMap[val_indicator] = options[options.size() - 1]; + + STRACE("t_str_detail", tout << "value tester encoding " << printVectorInt(valRangeMap[val_indicator]) << std::endl;); + + // ---------------------------------------------------------------------------------------- + + std::vector orList; + std::vector andList; + + for (long long i = l; i < h; i++) { + orList.push_back(m.mk_eq(val_indicator, m_strutil.mk_string(longlong_to_string(i).c_str()) )); + std::string aStr = gen_val_string(len, options[i - l]); + expr_ref strAst(m_strutil.mk_string(aStr), m); + andList.push_back(m.mk_eq(orList[orList.size() - 1], m.mk_eq(freeVar, strAst))); + } + if (!coverAll) { + orList.push_back(m.mk_eq(val_indicator, m_strutil.mk_string("more"))); + } + + Z3_ast * or_items = new Z3_ast[orList.size()]; + Z3_ast * and_items = new Z3_ast[andList.size() + 1]; + for (int i = 0; i < (int) orList.size(); i++) { + or_items[i] = orList[i]; + } + if (orList.size() > 1) + and_items[0] = Z3_mk_or(ctx, orList.size(), or_items); + else + and_items[0] = or_items[0]; + + for (int i = 0; i < (int) andList.size(); i++) { + and_items[i + 1] = andList[i]; + } + Z3_ast valTestAssert = Z3_mk_and(ctx, andList.size() + 1, and_items); + delete[] or_items; + delete[] and_items; + + // --------------------------------------- + // IF the new value tester is $$_val_x_16_i + // Should add ($$_len_x_j = 16) /\ ($$_val_x_16_i = "more") + // --------------------------------------- + andList.clear(); + andList.push_back(Z3_mk_eq(ctx, len_indicator, my_mk_str_value(t, lenStr.c_str()))); + for (int i = 0; i < tries; i++) { + Z3_ast vTester = fvarValueTesterMap[freeVar][len][i].second; + if (vTester != val_indicator) + andList.push_back(Z3_mk_eq(ctx, vTester, my_mk_str_value(t, "more"))); + } + Z3_ast assertL = NULL; + if (andList.size() == 1) { + assertL = andList[0]; + } else { + Z3_ast * and_items = new Z3_ast[andList.size()]; + for (int i = 0; i < (int) andList.size(); i++) { + and_items[i] = andList[i]; + } + assertL = Z3_mk_and(ctx, andList.size(), and_items); + } + + valTestAssert = Z3_mk_implies(ctx, assertL, valTestAssert); + return valTestAssert; +} + expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, std::string len_valueStr, expr * valTesterInCbEq, std::string valTesterValueStr) { context & ctx = get_context(); @@ -3475,7 +3619,7 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, int testerTotal = fvar_valueTester_map[freeVar][len].size(); int i = 0; for (; i < testerTotal; i++) { - expr * aTester = fvarValueTesterMap[freeVar][len][i].second; + expr * aTester = fvar_valueTester_map[freeVar][len][i].second; if (aTester == valTesterInCbEq) { break; diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index bd26f2564..c9432921a 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -89,6 +89,9 @@ namespace smt { std::map > fvar_lenTester_map; std::map lenTester_fvar_map; + std::map > > > fvar_valueTester_map; + std::map valueTester_fvar_map; + protected: void assert_axiom(expr * e); void assert_implication(expr * premise, expr * conclusion); @@ -107,6 +110,7 @@ namespace smt { app * mk_str_var(std::string name); app * mk_nonempty_str_var(); app * mk_internal_xor_var(); + expr * mk_internal_valTest_var(expr * node, int len, int vTries); bool is_concat(app const * a) const { return a->is_app_of(get_id(), OP_STRCAT); } bool is_concat(enode const * n) const { return is_concat(n->get_owner()); } @@ -167,6 +171,9 @@ namespace smt { expr * gen_len_test_options(expr * freeVar, expr * indicator, int tries); expr * gen_free_var_options(expr * freeVar, expr * len_indicator, std::string len_valueStr, expr * valTesterInCbEq, std::string valTesterValueStr); + expr * gen_val_options(expr * freeVar, expr * len_indicator, expr * val_indicator, + std::string lenStr, int tries); + void print_value_tester_list(std::vector > & testerList); expr * get_alias_index_ast(std::map & aliasIndexMap, expr * node); expr * getMostLeftNodeInConcat(expr * node); From 9beeb09acf44b8a5d0dc442cf27a1132205f33e9 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 15 Nov 2015 15:18:14 -0500 Subject: [PATCH 063/562] model gen possibly done, but I doubt it works so WIP --- src/smt/theory_str.cpp | 76 ++++++++++++++++++++++++++++++++---------- src/smt/theory_str.h | 6 ++++ 2 files changed, 65 insertions(+), 17 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index f86f921de..1961a3e36 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3495,6 +3495,48 @@ void theory_str::print_value_tester_list(std::vector > & t ); } +std::string theory_str::gen_val_string(int len, std::vector & encoding) { + SASSERT(charSetSize > 0); + + std::string re = std::string(len, charSet[0]); + for (int i = 0; i < (int) encoding.size() - 1; i++) { + int idx = encoding[i]; + re[len - 1 - i] = charSet[idx]; + } + return re; +} + +/* + * The return value indicates whether we covered the search space. + * - If the next encoding is valid, return false + * - Otherwise, return true + */ +bool theory_str::get_next_val_encode(std::vector & base, std::vector & next) { + int s = 0; + int carry = 0; + next.clear(); + + for (int i = 0; i < (int) base.size(); i++) { + if (i == 0) { + s = base[i] + 1; + carry = s / charSetSize; + s = s % charSetSize; + next.push_back(s); + } else { + s = base[i] + carry; + carry = s / charSetSize; + s = s % charSetSize; + next.push_back(s); + } + } + if (next[next.size() - 1] > 0) { + next.clear(); + return true; + } else { + return false; + } +} + expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * val_indicator, std::string lenStr, int tries) { context & ctx = get_context(); @@ -3525,7 +3567,7 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * } else { expr * lastestValIndi = fvar_valueTester_map[freeVar][len][tries - 1].second; STRACE("t_str_detail", tout << "last value tester = " << mk_ismt2_pp(lastestValIndi, m) << std::endl;); - coverAll = get_next_val_encode(valRangeMap[lastestValIndi], base); + coverAll = get_next_val_encode(val_range_map[lastestValIndi], base); } long long l = (tries) * distance; @@ -3535,9 +3577,9 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * break; options.push_back(base); h++; - coverAll = getNextValEncode(options[options.size() - 1], base); + coverAll = get_next_val_encode(options[options.size() - 1], base); } - valRangeMap[val_indicator] = options[options.size() - 1]; + val_range_map[val_indicator] = options[options.size() - 1]; STRACE("t_str_detail", tout << "value tester encoding " << printVectorInt(valRangeMap[val_indicator]) << std::endl;); @@ -3556,46 +3598,46 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * orList.push_back(m.mk_eq(val_indicator, m_strutil.mk_string("more"))); } - Z3_ast * or_items = new Z3_ast[orList.size()]; - Z3_ast * and_items = new Z3_ast[andList.size() + 1]; + expr ** or_items = alloc_svect(expr*, orList.size()); + expr ** and_items = alloc_svect(expr*, andList.size() + 1); + for (int i = 0; i < (int) orList.size(); i++) { or_items[i] = orList[i]; } if (orList.size() > 1) - and_items[0] = Z3_mk_or(ctx, orList.size(), or_items); + and_items[0] = m.mk_or(orList.size(), or_items); else and_items[0] = or_items[0]; for (int i = 0; i < (int) andList.size(); i++) { and_items[i + 1] = andList[i]; } - Z3_ast valTestAssert = Z3_mk_and(ctx, andList.size() + 1, and_items); - delete[] or_items; - delete[] and_items; + expr * valTestAssert = m.mk_and(andList.size() + 1, and_items); // --------------------------------------- - // IF the new value tester is $$_val_x_16_i + // If the new value tester is $$_val_x_16_i // Should add ($$_len_x_j = 16) /\ ($$_val_x_16_i = "more") // --------------------------------------- andList.clear(); - andList.push_back(Z3_mk_eq(ctx, len_indicator, my_mk_str_value(t, lenStr.c_str()))); + andList.push_back(m.mk_eq(len_indicator, m_strutil.mk_string(lenStr.c_str()))); for (int i = 0; i < tries; i++) { - Z3_ast vTester = fvarValueTesterMap[freeVar][len][i].second; + expr * vTester = fvar_valueTester_map[freeVar][len][i].second; if (vTester != val_indicator) - andList.push_back(Z3_mk_eq(ctx, vTester, my_mk_str_value(t, "more"))); + andList.push_back(m.mk_eq(vTester, m_strutil.mk_string("more"))); } - Z3_ast assertL = NULL; + expr * assertL = NULL; if (andList.size() == 1) { assertL = andList[0]; } else { - Z3_ast * and_items = new Z3_ast[andList.size()]; + expr ** and_items = alloc_svect(expr*, andList.size()); for (int i = 0; i < (int) andList.size(); i++) { and_items[i] = andList[i]; } - assertL = Z3_mk_and(ctx, andList.size(), and_items); + assertL = m.mk_and(andList.size(), and_items); } - valTestAssert = Z3_mk_implies(ctx, assertL, valTestAssert); + // (assertL => valTestAssert) <=> (!assertL OR valTestAssert) + valTestAssert = m.mk_or(m.mk_not(assertL), valTestAssert); return valTestAssert; } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index c9432921a..b7a63edb3 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -92,6 +92,10 @@ namespace smt { std::map > > > fvar_valueTester_map; std::map valueTester_fvar_map; + std::map > val_range_map; + + int charSetSize = 0; + protected: void assert_axiom(expr * e); void assert_implication(expr * premise, expr * conclusion); @@ -174,6 +178,8 @@ namespace smt { expr * gen_val_options(expr * freeVar, expr * len_indicator, expr * val_indicator, std::string lenStr, int tries); void print_value_tester_list(std::vector > & testerList); + bool get_next_val_encode(std::vector & base, std::vector & next); + std::string gen_val_string(int len, std::vector & encoding); expr * get_alias_index_ast(std::map & aliasIndexMap, expr * node); expr * getMostLeftNodeInConcat(expr * node); From b34fc06fe95645db09b8b3eb1778c9607e0c1e94 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 20 Nov 2015 12:24:23 -0500 Subject: [PATCH 064/562] fix all compilation errors, now to test it --- src/smt/theory_str.cpp | 71 +++++++++++++++++++++--------------------- src/smt/theory_str.h | 13 ++++---- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 1961a3e36..068745d94 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -32,7 +32,9 @@ theory_str::theory_str(ast_manager & m): tmpStringVarCount(0), tmpXorVarCount(0), avoidLoopCut(true), - loopDetected(false) + loopDetected(false), + char_set(NULL), + charSetSize(0) { } @@ -222,7 +224,6 @@ app * theory_str::mk_int(int n) { } expr * theory_str::mk_internal_lenTest_var(expr * node, int lTries) { - context & ctx = get_context(); ast_manager & m = get_manager(); std::stringstream ss; @@ -240,7 +241,6 @@ expr * theory_str::mk_internal_lenTest_var(expr * node, int lTries) { } expr * theory_str::mk_internal_valTest_var(expr * node, int len, int vTries) { - context & ctx = get_context(); ast_manager & m = get_manager(); std::stringstream ss; ss << "$$_val_" << mk_ismt2_pp(node, m) << "_" << len << "_" << vTries; @@ -257,7 +257,6 @@ expr * theory_str::mk_internal_valTest_var(expr * node, int len, int vTries) { } app * theory_str::mk_internal_xor_var() { - context & ctx = get_context(); ast_manager & m = get_manager(); std::stringstream ss; ss << tmpXorVarCount; @@ -296,7 +295,6 @@ app * theory_str::mk_str_var(std::string name) { } app * theory_str::mk_nonempty_str_var() { - context & ctx = get_context(); ast_manager & m = get_manager(); std::stringstream ss; ss << tmpStringVarCount; @@ -435,12 +433,10 @@ void theory_str::instantiate_concat_axiom(enode * cat) { SASSERT(is_concat(cat)); app * a_cat = cat->get_owner(); - context & ctx = get_context(); ast_manager & m = get_manager(); // build LHS expr_ref len_xy(m); - // TODO re-use ASTs for length subexpressions, like in old Z3-str? // TODO should we use str_util for these and other expressions? len_xy = mk_strlen(a_cat); SASSERT(len_xy); @@ -2580,9 +2576,6 @@ void theory_str::dump_assignments() { void theory_str::classify_ast_by_type(expr * node, std::map & varMap, std::map & concatMap, std::map & unrollMap) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - // check whether the node is a non-internal string variable; // testing set membership here bypasses several expensive checks if (variable_set.find(node) != variable_set.end() @@ -2620,7 +2613,6 @@ void theory_str::classify_ast_by_type(expr * node, std::map & varMap } } // recursively visit all arguments - app * aNode = to_app(node); for (unsigned i = 0; i < aNode->get_num_args(); ++i) { expr * arg = aNode->get_arg(i); classify_ast_by_type(arg, varMap, concatMap, unrollMap); @@ -2964,7 +2956,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map & strVarMap, std::map > & testerList) { +void theory_str::print_value_tester_list(svector > & testerList) { ast_manager & m = get_manager(); STRACE("t_str_detail", int ss = testerList.size(); @@ -3495,13 +3486,14 @@ void theory_str::print_value_tester_list(std::vector > & t ); } -std::string theory_str::gen_val_string(int len, std::vector & encoding) { +std::string theory_str::gen_val_string(int len, int_vector & encoding) { SASSERT(charSetSize > 0); + SASSERT(char_set != NULL); - std::string re = std::string(len, charSet[0]); + std::string re = std::string(len, char_set[0]); for (int i = 0; i < (int) encoding.size() - 1; i++) { int idx = encoding[i]; - re[len - 1 - i] = charSet[idx]; + re[len - 1 - i] = char_set[idx]; } return re; } @@ -3511,10 +3503,10 @@ std::string theory_str::gen_val_string(int len, std::vector & encoding) { * - If the next encoding is valid, return false * - Otherwise, return true */ -bool theory_str::get_next_val_encode(std::vector & base, std::vector & next) { +bool theory_str::get_next_val_encode(int_vector & base, int_vector & next) { int s = 0; int carry = 0; - next.clear(); + next.reset(); for (int i = 0; i < (int) base.size(); i++) { if (i == 0) { @@ -3530,7 +3522,7 @@ bool theory_str::get_next_val_encode(std::vector & base, std::vector & } } if (next[next.size() - 1] > 0) { - next.clear(); + next.reset(); return true; } else { return false; @@ -3539,7 +3531,6 @@ bool theory_str::get_next_val_encode(std::vector & base, std::vector & expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * val_indicator, std::string lenStr, int tries) { - context & ctx = get_context(); ast_manager & m = get_manager(); int distance = 32; @@ -3558,11 +3549,11 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * // ---------------------------------------------------------------------------------------- int len = atoi(lenStr.c_str()); bool coverAll = false; - std::vector > options; - std::vector base; + svector options; + int_vector base; if (tries == 0) { - base = std::vector(len + 1, 0); + base = int_vector(len + 1, 0); coverAll = false; } else { expr * lastestValIndi = fvar_valueTester_map[freeVar][len][tries - 1].second; @@ -3581,12 +3572,20 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * } val_range_map[val_indicator] = options[options.size() - 1]; - STRACE("t_str_detail", tout << "value tester encoding " << printVectorInt(valRangeMap[val_indicator]) << std::endl;); + STRACE("t_str_detail", + tout << "value tester encoding " << "{" << std::endl; + int_vector vec = val_range_map[val_indicator]; + + for (int_vector::iterator it = vec.begin(); it != vec.end(); ++it) { + tout << *it << std::endl; + } + tout << "}" << std::endl; + ); // ---------------------------------------------------------------------------------------- - std::vector orList; - std::vector andList; + ptr_vector orList; + ptr_vector andList; for (long long i = l; i < h; i++) { orList.push_back(m.mk_eq(val_indicator, m_strutil.mk_string(longlong_to_string(i).c_str()) )); @@ -3618,7 +3617,7 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * // If the new value tester is $$_val_x_16_i // Should add ($$_len_x_j = 16) /\ ($$_val_x_16_i = "more") // --------------------------------------- - andList.clear(); + andList.reset(); andList.push_back(m.mk_eq(len_indicator, m_strutil.mk_string(lenStr.c_str()))); for (int i = 0; i < tries; i++) { expr * vTester = fvar_valueTester_map[freeVar][len][i].second; @@ -3646,6 +3645,8 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, context & ctx = get_context(); ast_manager & m = get_manager(); + int sLevel = ctx.get_scope_level(); + int len = atoi(len_valueStr.c_str()); if (fvar_valueTester_map[freeVar].find(len) == fvar_valueTester_map[freeVar].end()) { @@ -3671,12 +3672,12 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, // Z3_ast anEqc = get_eqc_value(t, aTester, anEqcHasValue); get_eqc_value(aTester, anEqcHasValue); if (!anEqcHasValue) { - STRACE("t_str_detail", "value tester " << mk_ismt2_pp(aTester, m) + STRACE("t_str_detail", tout << "value tester " << mk_ismt2_pp(aTester, m) << "doesn't have an equivalence class value." << std::endl;); expr_ref makeupAssert(gen_val_options(freeVar, len_indicator, aTester, len_valueStr, i), m); - STRACE("t_str_detail", "var: " << mk_ismt2_pp(freeVar, m) << std::endl + STRACE("t_str_detail", tout << "var: " << mk_ismt2_pp(freeVar, m) << std::endl << mk_ismt2_pp(makeupAssert, m) << std::endl;); assert_axiom(makeupAssert); } @@ -3701,7 +3702,6 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, } expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tries) { - context & ctx = get_context(); ast_manager & m = get_manager(); expr * freeVarLen = mk_strlen(freeVar); @@ -3724,12 +3724,12 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr expr ** or_items = alloc_svect(expr*, orList.size()); expr ** and_items = alloc_svect(expr*, andList.size() + 1); - for (int i = 0; i < orList.size(); ++i) { + for (unsigned i = 0; i < orList.size(); ++i) { or_items[i] = orList[i]; } and_items[0] = m.mk_or(orList.size(), or_items); - for(int i = 0; i < andList.size(); ++i) { + for(unsigned i = 0; i < andList.size(); ++i) { and_items[i+1] = andList[i]; } expr * lenTestAssert = m.mk_and(andList.size() + 1, and_items); @@ -3772,7 +3772,6 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr // ----------------------------------------------------------------------------------------------------- expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTesterInCbEq, std::string lenTesterValue) { - context & ctx = get_context(); ast_manager & m = get_manager(); STRACE("t_str_detail", tout << "gen for free var " << mk_ismt2_pp(freeVar, m) << std::endl;); @@ -3814,7 +3813,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe << " does not have an equivalence class value." << " i = " << i << ", lenTesterCount = " << lenTesterCount << std::endl;); if (i > 0) { - effectiveLenInd = fvarLenTesterMap[freeVar][i - 1]; + effectiveLenInd = fvar_lenTester_map[freeVar][i - 1]; if (effectiveLenInd == lenTesterInCbEq) { effectiveLenIndiStr = lenTesterValue; } else { @@ -3850,7 +3849,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe fvar_lenTester_map[freeVar].push_back(indicator); lenTester_fvar_map[indicator] = freeVar; } else { - indicator = fvarLenTesterMap[freeVar][i]; + indicator = fvar_lenTester_map[freeVar][i]; testNum = i + 1; } expr_ref lenTestAssert(gen_len_test_options(freeVar, indicator, testNum), m); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index b7a63edb3..2f23ce43e 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -89,12 +89,13 @@ namespace smt { std::map > fvar_lenTester_map; std::map lenTester_fvar_map; - std::map > > > fvar_valueTester_map; + std::map > > > fvar_valueTester_map; std::map valueTester_fvar_map; - std::map > val_range_map; + std::map val_range_map; - int charSetSize = 0; + char * char_set; + int charSetSize; protected: void assert_axiom(expr * e); @@ -177,9 +178,9 @@ namespace smt { std::string len_valueStr, expr * valTesterInCbEq, std::string valTesterValueStr); expr * gen_val_options(expr * freeVar, expr * len_indicator, expr * val_indicator, std::string lenStr, int tries); - void print_value_tester_list(std::vector > & testerList); - bool get_next_val_encode(std::vector & base, std::vector & next); - std::string gen_val_string(int len, std::vector & encoding); + void print_value_tester_list(svector > & testerList); + bool get_next_val_encode(int_vector & base, int_vector & next); + std::string gen_val_string(int len, int_vector & encoding); expr * get_alias_index_ast(std::map & aliasIndexMap, expr * node); expr * getMostLeftNodeInConcat(expr * node); From bf27d41b0824cab5c80280278d6032c4e8c97c30 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 20 Nov 2015 12:27:29 -0500 Subject: [PATCH 065/562] use TRACE instead of STRACE... --- src/smt/theory_str.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 068745d94..429c87e62 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2954,7 +2954,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map::iterator itor2 = inVarMap.begin(); itor2 != inVarMap.end(); itor2++) { expr * varInFunc = get_alias_index_ast(aliasIndexMap, itor2->first); - STRACE("t_str_detail", tout << "var in unroll = " << + TRACE("t_str_detail", tout << "var in unroll = " << mk_ismt2_pp(itor2->first, m) << std::endl << "dealiased var = " << mk_ismt2_pp(varInFunc, m) << std::endl;); @@ -3471,7 +3471,7 @@ inline std::string longlong_to_string(long long i) { void theory_str::print_value_tester_list(svector > & testerList) { ast_manager & m = get_manager(); - STRACE("t_str_detail", + TRACE("t_str_detail", int ss = testerList.size(); tout << "valueTesterList = {"; for (int i = 0; i < ss; ++i) { @@ -3557,7 +3557,7 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * coverAll = false; } else { expr * lastestValIndi = fvar_valueTester_map[freeVar][len][tries - 1].second; - STRACE("t_str_detail", tout << "last value tester = " << mk_ismt2_pp(lastestValIndi, m) << std::endl;); + TRACE("t_str_detail", tout << "last value tester = " << mk_ismt2_pp(lastestValIndi, m) << std::endl;); coverAll = get_next_val_encode(val_range_map[lastestValIndi], base); } @@ -3572,7 +3572,7 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * } val_range_map[val_indicator] = options[options.size() - 1]; - STRACE("t_str_detail", + TRACE("t_str_detail", tout << "value tester encoding " << "{" << std::endl; int_vector vec = val_range_map[val_indicator]; @@ -3672,12 +3672,12 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, // Z3_ast anEqc = get_eqc_value(t, aTester, anEqcHasValue); get_eqc_value(aTester, anEqcHasValue); if (!anEqcHasValue) { - STRACE("t_str_detail", tout << "value tester " << mk_ismt2_pp(aTester, m) + TRACE("t_str_detail", tout << "value tester " << mk_ismt2_pp(aTester, m) << "doesn't have an equivalence class value." << std::endl;); expr_ref makeupAssert(gen_val_options(freeVar, len_indicator, aTester, len_valueStr, i), m); - STRACE("t_str_detail", tout << "var: " << mk_ismt2_pp(freeVar, m) << std::endl + TRACE("t_str_detail", tout << "var: " << mk_ismt2_pp(freeVar, m) << std::endl << mk_ismt2_pp(makeupAssert, m) << std::endl;); assert_axiom(makeupAssert); } @@ -3774,7 +3774,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe ast_manager & m = get_manager(); - STRACE("t_str_detail", tout << "gen for free var " << mk_ismt2_pp(freeVar, m) << std::endl;); + TRACE("t_str_detail", tout << "gen for free var " << mk_ismt2_pp(freeVar, m) << std::endl;); // no length assertions for this free variable have ever been added. if (fvar_len_count_map.find(freeVar) == fvar_len_count_map.end()) { fvar_len_count_map[freeVar] = 1; @@ -3796,7 +3796,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe expr * len_indicator_pre = fvar_lenTester_map[freeVar][i]; bool indicatorHasEqcValue = false; expr * len_indicator_value = get_eqc_value(len_indicator_pre, indicatorHasEqcValue); - STRACE("t_str_detail", tout << "length indicator " << mk_ismt2_pp(len_indicator_pre, m) << + TRACE("t_str_detail", tout << "length indicator " << mk_ismt2_pp(len_indicator_pre, m) << " = " << mk_ismt2_pp(len_indicator_value, m) << std::endl;); if (indicatorHasEqcValue) { const char * val = 0; @@ -3809,7 +3809,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe } } else { if (lenTesterInCbEq != len_indicator_pre) { - STRACE("t_str", tout << "WARNING: length indicator " << mk_ismt2_pp(len_indicator_pre, m) + TRACE("t_str", tout << "WARNING: length indicator " << mk_ismt2_pp(len_indicator_pre, m) << " does not have an equivalence class value." << " i = " << i << ", lenTesterCount = " << lenTesterCount << std::endl;); if (i > 0) { @@ -3839,7 +3839,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe expr * indicator = NULL; unsigned int testNum = 0; - STRACE("t_str", tout << "effectiveLenIndiStr = " << effectiveLenIndiStr + TRACE("t_str", tout << "effectiveLenIndiStr = " << effectiveLenIndiStr << ", i = " << i << ", lenTesterCount = " << lenTesterCount << std::endl;); if (i == lenTesterCount) { @@ -3908,7 +3908,7 @@ void theory_str::process_free_var(std::map & freeVar_map) { } } if (duplicated && dupVar != NULL) { - STRACE("t_str_detail", tout << "Duplicated free variable found:" << mk_ismt2_pp(freeVar, m) + TRACE("t_str_detail", tout << "Duplicated free variable found:" << mk_ismt2_pp(freeVar, m) << " = " << mk_ismt2_pp(dupVar, m) << " (SKIP)" << std::endl;); continue; } else { From 24148bafa3df7df22afedda3a6aac1175f9fc155 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 20 Nov 2015 15:48:06 -0500 Subject: [PATCH 066/562] fixed several AST bugs; need to load charSet now --- src/smt/theory_str.cpp | 55 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 429c87e62..b2f79b7bc 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -285,6 +285,7 @@ app * theory_str::mk_str_var(std::string name) { app * a = m.mk_const(m.mk_const_decl(sym, string_sort)); // I have a hunch that this may not get internalized for free... + ctx.internalize(a, false); SASSERT(ctx.get_enode(a) != NULL); m_basicstr_axiom_todo.push_back(ctx.get_enode(a)); @@ -3504,6 +3505,8 @@ std::string theory_str::gen_val_string(int len, int_vector & encoding) { * - Otherwise, return true */ bool theory_str::get_next_val_encode(int_vector & base, int_vector & next) { + SASSERT(charSetSize > 0); + int s = 0; int carry = 0; next.reset(); @@ -3552,6 +3555,14 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * svector options; int_vector base; + TRACE("t_str_detail", tout + << "freeVar = " << mk_ismt2_pp(freeVar, m) << std::endl + << "len_indicator = " << mk_ismt2_pp(len_indicator, m) << std::endl + << "val_indicator = " << mk_ismt2_pp(val_indicator, m) << std::endl + << "lenstr = " << lenStr << std::endl + << "tries = " << tries << std::endl + ;); + if (tries == 0) { base = int_vector(len + 1, 0); coverAll = false; @@ -3650,6 +3661,7 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, int len = atoi(len_valueStr.c_str()); if (fvar_valueTester_map[freeVar].find(len) == fvar_valueTester_map[freeVar].end()) { + TRACE("t_str_detail", tout << "no previous value testers" << std::endl;); int tries = 0; expr * val_indicator = mk_internal_valTest_var(freeVar, len, tries); valueTester_fvar_map[val_indicator] = freeVar; @@ -3657,6 +3669,7 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, print_value_tester_list(fvar_valueTester_map[freeVar][len]); return gen_val_options(freeVar, len_indicator, val_indicator, len_valueStr, tries); } else { + TRACE("t_str_detail", tout << "checking previous value testers" << std::endl;); // go through all previous value testers // If some doesn't have an eqc value, add its assertion again. int testerTotal = fvar_valueTester_map[freeVar][len].size(); @@ -3704,7 +3717,10 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tries) { ast_manager & m = get_manager(); - expr * freeVarLen = mk_strlen(freeVar); + TRACE("t_str_detail", tout << "entry" << std::endl;); + + expr_ref freeVarLen(mk_strlen(freeVar), m); + SASSERT(freeVarLen); ptr_vector orList; ptr_vector andList; @@ -3713,9 +3729,16 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr int l = (tries - 1) * distance; int h = tries * distance; + TRACE("t_str_detail", tout << "building andList and orList" << std::endl;); + for (int i = l; i < h; ++i) { - orList.push_back(m.mk_eq(indicator, m_strutil.mk_string(int_to_string(i).c_str()))); - andList.push_back(m.mk_eq(orList[orList.size() - 1], m.mk_eq(freeVarLen, mk_int(i)))); + expr * or_expr = m.mk_eq(indicator, m_strutil.mk_string(int_to_string(i).c_str())); + TRACE("t_str_detail", tout << "or_expr = " << mk_ismt2_pp(or_expr, m) << std::endl;); + orList.push_back(or_expr); + + expr * and_expr = m.mk_eq(orList[orList.size() - 1], m.mk_eq(freeVarLen, mk_int(i))); + TRACE("t_str_detail", tout << "and_expr = " << mk_ismt2_pp(and_expr, m) << std::endl;); + andList.push_back(and_expr); } orList.push_back(m.mk_eq(indicator, m_strutil.mk_string("more"))); @@ -3725,14 +3748,20 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr expr ** and_items = alloc_svect(expr*, andList.size() + 1); for (unsigned i = 0; i < orList.size(); ++i) { + SASSERT(orList[i] != NULL); or_items[i] = orList[i]; } and_items[0] = m.mk_or(orList.size(), or_items); + SASSERT(and_items[0] != NULL); for(unsigned i = 0; i < andList.size(); ++i) { + SASSERT(andList[i] != NULL); and_items[i+1] = andList[i]; } expr * lenTestAssert = m.mk_and(andList.size() + 1, and_items); + SASSERT(lenTestAssert != NULL); + + TRACE("t_str_detail", tout << "lenTestAssert = " << mk_ismt2_pp(lenTestAssert, m) << std::endl;); expr * assertL = NULL; int testerCount = tries - 1; @@ -3750,11 +3779,14 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr } if (assertL != NULL) { + TRACE("t_str_detail", tout << "assertL = " << mk_ismt2_pp(assertL, m) << std::endl;); // return the axiom (assertL -> lenTestAssert) // would like to use mk_implies() here but... expr_ref lenTestAssert(m.mk_or(m.mk_not(assertL), lenTestAssert), m); } + TRACE("t_str_detail", tout << "exit" << std::endl;); + return lenTestAssert; } @@ -3777,16 +3809,23 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe TRACE("t_str_detail", tout << "gen for free var " << mk_ismt2_pp(freeVar, m) << std::endl;); // no length assertions for this free variable have ever been added. if (fvar_len_count_map.find(freeVar) == fvar_len_count_map.end()) { + + TRACE("t_str_detail", tout << "no length assertions yet" << std::endl;); + fvar_len_count_map[freeVar] = 1; unsigned int testNum = fvar_len_count_map[freeVar]; + expr * indicator = mk_internal_lenTest_var(freeVar, testNum); + SASSERT(indicator != NULL); + fvar_lenTester_map[freeVar].push_back(indicator); lenTester_fvar_map[indicator] = freeVar; expr * lenTestAssert = gen_len_test_options(freeVar, indicator, testNum); + SASSERT(lenTestAssert != NULL); return lenTestAssert; } else { - + TRACE("t_str_detail", tout << "found previous length assertions" << std::endl;); expr * effectiveLenInd = NULL; std::string effectiveLenIndiStr = ""; int lenTesterCount = (int) fvar_lenTester_map[freeVar].size(); @@ -3836,6 +3875,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe } // !indicatorHasEqcValue } // for (i : [0..lenTesterCount-1]) if (effectiveLenIndiStr == "more" || effectiveLenIndiStr == "") { + TRACE("t_str", tout << "length is not fixed; generating length tester options for free var" << std::endl;); expr * indicator = NULL; unsigned int testNum = 0; @@ -3852,11 +3892,12 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe indicator = fvar_lenTester_map[freeVar][i]; testNum = i + 1; } - expr_ref lenTestAssert(gen_len_test_options(freeVar, indicator, testNum), m); + expr * lenTestAssert = gen_len_test_options(freeVar, indicator, testNum); return lenTestAssert; } else { + TRACE("t_str", tout << "length is fixed; generating models for free var" << std::endl;); // length is fixed - expr_ref valueAssert(gen_free_var_options(freeVar, effectiveLenInd, effectiveLenIndiStr, NULL, ""), m); + expr * valueAssert = gen_free_var_options(freeVar, effectiveLenInd, effectiveLenIndiStr, NULL, ""); return valueAssert; } } // fVarLenCountMap.find(...) @@ -3956,6 +3997,7 @@ void theory_str::process_free_var(std::map & freeVar_map) { for(std::set::iterator itor1 = leafVarSet.begin(); itor1 != leafVarSet.end(); ++itor1) { expr * toAssert = gen_len_val_options_for_free_var(*itor1, NULL, ""); + SASSERT(toAssert != NULL); assert_axiom(toAssert); } @@ -3964,6 +4006,7 @@ void theory_str::process_free_var(std::map & freeVar_map) { std::set::iterator itor2 = mItor->second.begin(); for(; itor2 != mItor->second.end(); ++itor2) { expr * toAssert = gen_len_val_options_for_free_var(*itor2, NULL, ""); + SASSERT(toAssert != NULL); assert_axiom(toAssert); } } From 9010a5c4cf08764988113b3cf221c47e73d5963a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 20 Nov 2015 16:05:43 -0500 Subject: [PATCH 067/562] honest-to-goodness working model gen, i.e. it didn't crash. more testing needed --- src/smt/theory_str.cpp | 79 ++++++++++++++++++++++++++++++++++++++++-- src/smt/theory_str.h | 2 ++ 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index b2f79b7bc..61763b693 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -32,15 +32,88 @@ theory_str::theory_str(ast_manager & m): tmpStringVarCount(0), tmpXorVarCount(0), avoidLoopCut(true), - loopDetected(false), - char_set(NULL), - charSetSize(0) + loopDetected(false) { + initialize_charset(); } theory_str::~theory_str() { } +void theory_str::initialize_charset() { + bool defaultCharset = true; + if (defaultCharset) { + // valid C strings can't contain the null byte ('\0') + charSetSize = 255; + char_set = alloc_svect(char, charSetSize); + int idx = 0; + // small letters + for (int i = 97; i < 123; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + // caps + for (int i = 65; i < 91; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + // numbers + for (int i = 48; i < 58; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + // printable marks - 1 + for (int i = 32; i < 48; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + // printable marks - 2 + for (int i = 58; i < 65; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + // printable marks - 3 + for (int i = 91; i < 97; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + // printable marks - 4 + for (int i = 123; i < 127; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + // non-printable - 1 + for (int i = 1; i < 32; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + // non-printable - 2 + for (int i = 127; i < 256; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + } else { + const char setset[] = { 'a', 'b', 'c' }; + int fSize = sizeof(setset) / sizeof(char); + + char_set = alloc_svect(char, fSize); + charSetSize = fSize; + for (int i = 0; i < charSetSize; i++) { + char_set[i] = setset[i]; + charSetLookupTable[setset[i]] = i; + } + } +} + void theory_str::assert_axiom(expr * e) { if (get_manager().is_true(e)) return; TRACE("t_str_detail", tout << "asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 2f23ce43e..12898d458 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -95,6 +95,7 @@ namespace smt { std::map val_range_map; char * char_set; + std::map charSetLookupTable; int charSetSize; protected: @@ -192,6 +193,7 @@ namespace smt { void get_eqc_allUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet); void dump_assignments(); + void initialize_charset(); public: theory_str(ast_manager & m); virtual ~theory_str(); From 07626a1e030427c9a612c3778e67ee5fda6382d8 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 28 Nov 2015 23:56:30 -0500 Subject: [PATCH 068/562] remove expr_ref stuff, start tracking variables more closely --- src/smt/theory_str.cpp | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 61763b693..9cdc53329 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -350,6 +350,8 @@ app * theory_str::mk_str_var(std::string name) { context & ctx = get_context(); ast_manager & m = get_manager(); + TRACE("t_str_detail", tout << "creating string variable " << name << std::endl;); + sort * string_sort = m.mk_sort(m_strutil.get_family_id(), STRING_SORT); char * new_buffer = alloc_svect(char, name.length() + 1); strcpy(new_buffer, name.c_str()); @@ -360,6 +362,7 @@ app * theory_str::mk_str_var(std::string name) { // I have a hunch that this may not get internalized for free... ctx.internalize(a, false); SASSERT(ctx.get_enode(a) != NULL); + SASSERT(ctx.e_internalized(a)); m_basicstr_axiom_todo.push_back(ctx.get_enode(a)); variable_set.insert(a); @@ -369,26 +372,31 @@ app * theory_str::mk_str_var(std::string name) { } app * theory_str::mk_nonempty_str_var() { + context & ctx = get_context(); ast_manager & m = get_manager(); + std::stringstream ss; ss << tmpStringVarCount; tmpStringVarCount++; std::string name = "$$_str" + ss.str(); + + TRACE("t_str_detail", tout << "creating nonempty string variable " << name << std::endl;); + sort * string_sort = m.mk_sort(m_strutil.get_family_id(), STRING_SORT); char * new_buffer = alloc_svect(char, name.length() + 1); strcpy(new_buffer, name.c_str()); symbol sym(new_buffer); app* a = m.mk_const(m.mk_const_decl(sym, string_sort)); + ctx.internalize(a, false); + SASSERT(ctx.get_enode(a) != NULL); // assert a variation of the basic string axioms that ensures this string is nonempty { // build LHS - expr_ref len_str(m); - len_str = mk_strlen(a); + expr * len_str = mk_strlen(a); SASSERT(len_str); // build RHS - expr_ref zero(m); - zero = m_autil.mk_numeral(rational(0), true); + expr * zero = m_autil.mk_numeral(rational(0), true); SASSERT(zero); // build LHS > RHS and assert // we have to build !(LHS <= RHS) instead @@ -2509,7 +2517,7 @@ void theory_str::set_up_axioms(expr * ex) { } } else if (ap->get_num_args() == 0 && !is_string(ap)) { // if ex is a variable, add it to our list of variables - TRACE("t_str_detail", tout << "tracking variable" << std::endl;); + TRACE("t_str_detail", tout << "tracking variable " << mk_ismt2_pp(ap, get_manager()) << std::endl;); variable_set.insert(ex); } } @@ -3347,6 +3355,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map free_variables; + TRACE("t_str_detail", tout << variable_set.size() << " variables in variable_set" << std::endl;); for (std::set::iterator it = variable_set.begin(); it != variable_set.end(); ++it) { + TRACE("t_str_detail", tout << "checking eqc of variable " << mk_ismt2_pp(*it, m) << std::endl;); bool has_eqc_value = false; get_eqc_value(*it, has_eqc_value); if (!has_eqc_value) { @@ -3674,7 +3685,7 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * for (long long i = l; i < h; i++) { orList.push_back(m.mk_eq(val_indicator, m_strutil.mk_string(longlong_to_string(i).c_str()) )); std::string aStr = gen_val_string(len, options[i - l]); - expr_ref strAst(m_strutil.mk_string(aStr), m); + expr * strAst = m_strutil.mk_string(aStr); andList.push_back(m.mk_eq(orList[orList.size() - 1], m.mk_eq(freeVar, strAst))); } if (!coverAll) { @@ -3761,7 +3772,7 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, TRACE("t_str_detail", tout << "value tester " << mk_ismt2_pp(aTester, m) << "doesn't have an equivalence class value." << std::endl;); - expr_ref makeupAssert(gen_val_options(freeVar, len_indicator, aTester, len_valueStr, i), m); + expr * makeupAssert = gen_val_options(freeVar, len_indicator, aTester, len_valueStr, i); TRACE("t_str_detail", tout << "var: " << mk_ismt2_pp(freeVar, m) << std::endl << mk_ismt2_pp(makeupAssert, m) << std::endl;); @@ -3779,7 +3790,7 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, fvar_valueTester_map[freeVar][len].push_back(std::make_pair(sLevel, valTester)); print_value_tester_list(fvar_valueTester_map[freeVar][len]); } - expr_ref nextAssert(gen_val_options(freeVar, len_indicator, valTester, len_valueStr, i + 1), m); + expr * nextAssert = gen_val_options(freeVar, len_indicator, valTester, len_valueStr, i + 1); return nextAssert; } @@ -3792,7 +3803,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr TRACE("t_str_detail", tout << "entry" << std::endl;); - expr_ref freeVarLen(mk_strlen(freeVar), m); + expr * freeVarLen = mk_strlen(freeVar); SASSERT(freeVarLen); ptr_vector orList; @@ -3855,7 +3866,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr TRACE("t_str_detail", tout << "assertL = " << mk_ismt2_pp(assertL, m) << std::endl;); // return the axiom (assertL -> lenTestAssert) // would like to use mk_implies() here but... - expr_ref lenTestAssert(m.mk_or(m.mk_not(assertL), lenTestAssert), m); + lenTestAssert = m.mk_or(m.mk_not(assertL), lenTestAssert); } TRACE("t_str_detail", tout << "exit" << std::endl;); From dd0bc13be720fbb3abf976d6d6c134900db74058 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 30 Nov 2015 19:22:01 -0500 Subject: [PATCH 069/562] attempt to track popped variables, still segfaults, WIP --- src/smt/theory_str.cpp | 45 ++++++++++++++++++++++++++++++++++++++---- src/smt/theory_str.h | 2 ++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 9cdc53329..8056864a4 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -329,8 +329,18 @@ expr * theory_str::mk_internal_valTest_var(expr * node, int len, int vTries) { */ } +void theory_str::track_variable_scope(expr * var) { + context & ctx = get_context(); + int sLevel = ctx.get_scope_level(); + if (internal_variable_scope_levels.find(sLevel) == internal_variable_scope_levels.end()) { + internal_variable_scope_levels[sLevel] = std::set(); + } + internal_variable_scope_levels[sLevel].insert(var); +} + app * theory_str::mk_internal_xor_var() { ast_manager & m = get_manager(); + context & ctx = get_context(); std::stringstream ss; ss << tmpXorVarCount; tmpXorVarCount++; @@ -342,6 +352,7 @@ app * theory_str::mk_internal_xor_var() { symbol sym(new_buffer); app* a = m.mk_const(m.mk_const_decl(sym, int_sort)); + // TODO ctx.save_ast_trail(a)? return a; } @@ -350,7 +361,8 @@ app * theory_str::mk_str_var(std::string name) { context & ctx = get_context(); ast_manager & m = get_manager(); - TRACE("t_str_detail", tout << "creating string variable " << name << std::endl;); + int sLevel = ctx.get_scope_level(); + TRACE("t_str_detail", tout << "creating string variable " << name << " at scope level " << sLevel << std::endl;); sort * string_sort = m.mk_sort(m_strutil.get_family_id(), STRING_SORT); char * new_buffer = alloc_svect(char, name.length() + 1); @@ -367,6 +379,7 @@ app * theory_str::mk_str_var(std::string name) { variable_set.insert(a); internal_variable_set.insert(a); + track_variable_scope(a); return a; } @@ -380,7 +393,9 @@ app * theory_str::mk_nonempty_str_var() { tmpStringVarCount++; std::string name = "$$_str" + ss.str(); - TRACE("t_str_detail", tout << "creating nonempty string variable " << name << std::endl;); + int sLevel = ctx.get_scope_level(); + + TRACE("t_str_detail", tout << "creating nonempty string variable " << name << " at scope level " << sLevel << std::endl;); sort * string_sort = m.mk_sort(m_strutil.get_family_id(), STRING_SORT); char * new_buffer = alloc_svect(char, name.length() + 1); @@ -408,6 +423,7 @@ app * theory_str::mk_nonempty_str_var() { // add 'a' to variable sets, so we can keep track of it variable_set.insert(a); internal_variable_set.insert(a); + track_variable_scope(a); return a; } @@ -2620,13 +2636,16 @@ void theory_str::assign_eh(bool_var v, bool is_true) { } void theory_str::push_scope_eh() { - TRACE("t_str", tout << "push" << std::endl;); + context & ctx = get_context(); + int sLevel = ctx.get_scope_level(); + TRACE("t_str", tout << "push to " << sLevel << std::endl;); } void theory_str::pop_scope_eh(unsigned num_scopes) { - TRACE("t_str", tout << "pop " << num_scopes << std::endl;); context & ctx = get_context(); int sLevel = ctx.get_scope_level(); + TRACE("t_str", tout << "pop " << num_scopes << " to " << sLevel << std::endl;); + std::map >::iterator varItor = cut_var_map.begin(); while (varItor != cut_var_map.end()) { while ((varItor->second.size() > 0) && (varItor->second.top()->level != 0) && (varItor->second.top()->level >= sLevel)) { @@ -2639,6 +2658,24 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { } ++varItor; } + + // see if any internal variables went out of scope + for (int check_level = sLevel + num_scopes ; check_level > sLevel; --check_level) { + TRACE("t_str_detail", tout << "cleaning up internal variables at scope level " << check_level << std::endl;); + std::map >::iterator it = internal_variable_scope_levels.find(check_level); + if (it != internal_variable_scope_levels.end()) { + unsigned count = 0; + std::set vars = it->second; + for (std::set::iterator var_it = vars.begin(); var_it != vars.end(); ++var_it) { + variable_set.erase(*var_it); + internal_variable_set.erase(*var_it); + count += 1; + } + TRACE("t_str_detail", tout << "cleaned up " << count << " variables" << std::endl;); + vars.clear(); + } + } + } void theory_str::dump_assignments() { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 12898d458..fe2ff4625 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -82,6 +82,7 @@ namespace smt { std::set variable_set; std::set internal_variable_set; + std::map > internal_variable_scope_levels; std::set input_var_in_len; @@ -113,6 +114,7 @@ namespace smt { void add_cut_info_merge(expr * destNode, int slevel, expr * srcNode); bool has_self_cut(expr * n1, expr * n2); + void track_variable_scope(expr * var); app * mk_str_var(std::string name); app * mk_nonempty_str_var(); app * mk_internal_xor_var(); From c44d49b625661065d8bb3a6da1a0a6015100b3e4 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 1 Dec 2015 14:41:11 -0500 Subject: [PATCH 070/562] keep track of search level ourselves --- src/smt/theory_str.cpp | 11 +++-------- src/smt/theory_str.h | 1 + 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 8056864a4..b69bb4ac7 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -29,6 +29,7 @@ theory_str::theory_str(ast_manager & m): search_started(false), m_autil(m), m_strutil(m), + sLevel(0), tmpStringVarCount(0), tmpXorVarCount(0), avoidLoopCut(true), @@ -331,7 +332,6 @@ expr * theory_str::mk_internal_valTest_var(expr * node, int len, int vTries) { void theory_str::track_variable_scope(expr * var) { context & ctx = get_context(); - int sLevel = ctx.get_scope_level(); if (internal_variable_scope_levels.find(sLevel) == internal_variable_scope_levels.end()) { internal_variable_scope_levels[sLevel] = std::set(); } @@ -361,7 +361,6 @@ app * theory_str::mk_str_var(std::string name) { context & ctx = get_context(); ast_manager & m = get_manager(); - int sLevel = ctx.get_scope_level(); TRACE("t_str_detail", tout << "creating string variable " << name << " at scope level " << sLevel << std::endl;); sort * string_sort = m.mk_sort(m_strutil.get_family_id(), STRING_SORT); @@ -393,8 +392,6 @@ app * theory_str::mk_nonempty_str_var() { tmpStringVarCount++; std::string name = "$$_str" + ss.str(); - int sLevel = ctx.get_scope_level(); - TRACE("t_str_detail", tout << "creating nonempty string variable " << name << " at scope level " << sLevel << std::endl;); sort * string_sort = m.mk_sort(m_strutil.get_family_id(), STRING_SORT); @@ -2637,13 +2634,13 @@ void theory_str::assign_eh(bool_var v, bool is_true) { void theory_str::push_scope_eh() { context & ctx = get_context(); - int sLevel = ctx.get_scope_level(); + sLevel += 1; TRACE("t_str", tout << "push to " << sLevel << std::endl;); } void theory_str::pop_scope_eh(unsigned num_scopes) { context & ctx = get_context(); - int sLevel = ctx.get_scope_level(); + sLevel -= num_scopes; TRACE("t_str", tout << "pop " << num_scopes << " to " << sLevel << std::endl;); std::map >::iterator varItor = cut_var_map.begin(); @@ -3777,8 +3774,6 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, context & ctx = get_context(); ast_manager & m = get_manager(); - int sLevel = ctx.get_scope_level(); - int len = atoi(len_valueStr.c_str()); if (fvar_valueTester_map[freeVar].find(len) == fvar_valueTester_map[freeVar].end()) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index fe2ff4625..ce4a0cfc9 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -63,6 +63,7 @@ namespace smt { }; protected: bool search_started; + int sLevel; arith_util m_autil; str_util m_strutil; From 52f0277c99439419336bbcfb46f08d82d32e7041 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 1 Dec 2015 19:19:00 -0500 Subject: [PATCH 071/562] attempt to clean up out-of-scope variables more, still crashing --- src/smt/theory_str.cpp | 50 +++++++++++++++++++++++++++++++++++++----- src/smt/theory_str.h | 4 +++- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index b69bb4ac7..3c6261243 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -32,6 +32,8 @@ theory_str::theory_str(ast_manager & m): sLevel(0), tmpStringVarCount(0), tmpXorVarCount(0), + tmpLenTestVarCount(0), + tmpValTestVarCount(0), avoidLoopCut(true), loopDetected(false) { @@ -297,11 +299,14 @@ app * theory_str::mk_int(int n) { return m_autil.mk_numeral(rational(n), true); } +// We have to work a little bit harder to ensure that all variables we create here are always fresh. + expr * theory_str::mk_internal_lenTest_var(expr * node, int lTries) { ast_manager & m = get_manager(); std::stringstream ss; - ss << "$$_len_" << mk_ismt2_pp(node, m) << "_" << lTries; + ss << "$$_len_" << mk_ismt2_pp(node, m) << "_" << lTries << "_" << tmpLenTestVarCount; + tmpLenTestVarCount += 1; std::string name = ss.str(); return mk_str_var(name); @@ -317,7 +322,8 @@ expr * theory_str::mk_internal_lenTest_var(expr * node, int lTries) { expr * theory_str::mk_internal_valTest_var(expr * node, int len, int vTries) { ast_manager & m = get_manager(); std::stringstream ss; - ss << "$$_val_" << mk_ismt2_pp(node, m) << "_" << len << "_" << vTries; + ss << "$$_val_" << mk_ismt2_pp(node, m) << "_" << len << "_" << vTries << "_" << tmpValTestVarCount; + tmpValTestVarCount += 1; std::string name = ss.str(); return mk_str_var(name); @@ -3923,9 +3929,35 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe ast_manager & m = get_manager(); TRACE("t_str_detail", tout << "gen for free var " << mk_ismt2_pp(freeVar, m) << std::endl;); - // no length assertions for this free variable have ever been added. - if (fvar_len_count_map.find(freeVar) == fvar_len_count_map.end()) { + bool map_effectively_empty = false; + if (fvar_len_count_map.find(freeVar) == fvar_len_count_map.end()) { + TRACE("t_str_detail", tout << "fvar_len_count_map is empty" << std::endl;); + map_effectively_empty = true; + } + + if (!map_effectively_empty) { + // check whether any entries correspond to variables that went out of scope; + // if every entry is out of scope then the map counts as being empty + // TODO: maybe remove them from the map instead? either here or in pop_scope_eh() + + // assume empty and find a counterexample + map_effectively_empty = true; + ptr_vector indicator_set = fvar_lenTester_map[freeVar]; + for (ptr_vector::iterator it = indicator_set.begin(); it != indicator_set.end(); ++it) { + expr * indicator = *it; + if (internal_variable_set.find(indicator) != internal_variable_set.end()) { + TRACE("t_str_detail", tout <<"found active internal variable " << mk_ismt2_pp(indicator, m) + << " in fvar_lenTester_map[freeVar]" << std::endl;); + map_effectively_empty = false; + break; + } + } + CTRACE("t_str_detail", map_effectively_empty, tout << "all variables in fvar_lenTester_map[freeVar] out of scope" << std::endl;); + } + + if (map_effectively_empty) { + // no length assertions for this free variable have ever been added. TRACE("t_str_detail", tout << "no length assertions yet" << std::endl;); fvar_len_count_map[freeVar] = 1; @@ -3934,6 +3966,8 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe expr * indicator = mk_internal_lenTest_var(freeVar, testNum); SASSERT(indicator != NULL); + // since the map is "effectively empty", we can remove those variables that have left scope... + fvar_lenTester_map[freeVar].shrink(0); fvar_lenTester_map[freeVar].push_back(indicator); lenTester_fvar_map[indicator] = freeVar; @@ -3941,7 +3975,8 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe SASSERT(lenTestAssert != NULL); return lenTestAssert; } else { - TRACE("t_str_detail", tout << "found previous length assertions" << std::endl;); + TRACE("t_str_detail", tout << "found previous in-scope length assertions" << std::endl;); + expr * effectiveLenInd = NULL; std::string effectiveLenIndiStr = ""; int lenTesterCount = (int) fvar_lenTester_map[freeVar].size(); @@ -3949,6 +3984,11 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe int i = 0; for (; i < lenTesterCount; ++i) { expr * len_indicator_pre = fvar_lenTester_map[freeVar][i]; + // check whether this is in scope as well + if (internal_variable_set.find(len_indicator_pre) == internal_variable_set.end()) { + continue; + } + bool indicatorHasEqcValue = false; expr * len_indicator_value = get_eqc_value(len_indicator_pre, indicatorHasEqcValue); TRACE("t_str_detail", tout << "length indicator " << mk_ismt2_pp(len_indicator_pre, m) << diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index ce4a0cfc9..f126ca019 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -63,9 +63,9 @@ namespace smt { }; protected: bool search_started; - int sLevel; arith_util m_autil; str_util m_strutil; + int sLevel; str_value_factory * m_factory; @@ -75,6 +75,8 @@ namespace smt { int tmpStringVarCount; int tmpXorVarCount; + int tmpLenTestVarCount; + int tmpValTestVarCount; std::map, std::map > varForBreakConcat; bool avoidLoopCut; From 953a4c5437c5a5d1a2be60e68991f0f15a0b49a2 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 2 Dec 2015 20:48:15 -0500 Subject: [PATCH 072/562] add temporary variables to m_trail --- src/smt/theory_str.cpp | 8 +++++--- src/smt/theory_str.h | 3 +++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 3c6261243..9d0f9d689 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -30,6 +30,7 @@ theory_str::theory_str(ast_manager & m): m_autil(m), m_strutil(m), sLevel(0), + m_trail(m), tmpStringVarCount(0), tmpXorVarCount(0), tmpLenTestVarCount(0), @@ -357,9 +358,8 @@ app * theory_str::mk_internal_xor_var() { strcpy(new_buffer, name.c_str()); symbol sym(new_buffer); - app* a = m.mk_const(m.mk_const_decl(sym, int_sort)); - - // TODO ctx.save_ast_trail(a)? + app * a = m.mk_const(m.mk_const_decl(sym, int_sort)); + m_trail.push_back(a); return a; } @@ -382,6 +382,7 @@ app * theory_str::mk_str_var(std::string name) { SASSERT(ctx.e_internalized(a)); m_basicstr_axiom_todo.push_back(ctx.get_enode(a)); + m_trail.push_back(a); variable_set.insert(a); internal_variable_set.insert(a); track_variable_scope(a); @@ -424,6 +425,7 @@ app * theory_str::mk_nonempty_str_var() { } // add 'a' to variable sets, so we can keep track of it + m_trail.push_back(a); variable_set.insert(a); internal_variable_set.insert(a); track_variable_scope(a); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index f126ca019..ca985cb8f 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -67,6 +67,9 @@ namespace smt { str_util m_strutil; int sLevel; + // TODO make sure that all generated expressions are saved into the trail + expr_ref_vector m_trail; // trail for generated terms + str_value_factory * m_factory; ptr_vector m_basicstr_axiom_todo; From 23150d3b5e81064f7717835457ef2689b28aebe2 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 2 Dec 2015 22:03:12 -0500 Subject: [PATCH 073/562] never ever ever reuse constants in mk_string(). this gets us MUCH farther --- src/ast/str_decl_plugin.cpp | 3 ++- src/smt/theory_str.cpp | 30 ++++++++++++++---------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 5589db56c..550789065 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -101,7 +101,8 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters, app * str_decl_plugin::mk_string(std::string & val) { std::map::iterator it = string_cache.find(val); - if (it == string_cache.end()) { + //if (it == string_cache.end()) { + if (true) { char * new_buffer = alloc_svect(char, (val.length() + 1)); strcpy(new_buffer, val.c_str()); parameter p[1] = {parameter(new_buffer)}; diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 9d0f9d689..9b9cc8fd9 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -300,7 +300,8 @@ app * theory_str::mk_int(int n) { return m_autil.mk_numeral(rational(n), true); } -// We have to work a little bit harder to ensure that all variables we create here are always fresh. + +// TODO refactor all of these so that they don't use variable counters, but use ast_manager::mk_fresh_const instead expr * theory_str::mk_internal_lenTest_var(expr * node, int lTries) { ast_manager & m = get_manager(); @@ -369,12 +370,8 @@ app * theory_str::mk_str_var(std::string name) { TRACE("t_str_detail", tout << "creating string variable " << name << " at scope level " << sLevel << std::endl;); - sort * string_sort = m.mk_sort(m_strutil.get_family_id(), STRING_SORT); - char * new_buffer = alloc_svect(char, name.length() + 1); - strcpy(new_buffer, name.c_str()); - symbol sym(new_buffer); - - app * a = m.mk_const(m.mk_const_decl(sym, string_sort)); + sort * string_sort = m.mk_sort(get_family_id(), STRING_SORT); + app * a = m.mk_fresh_const(name.c_str(), string_sort); // I have a hunch that this may not get internalized for free... ctx.internalize(a, false); @@ -401,12 +398,9 @@ app * theory_str::mk_nonempty_str_var() { TRACE("t_str_detail", tout << "creating nonempty string variable " << name << " at scope level " << sLevel << std::endl;); - sort * string_sort = m.mk_sort(m_strutil.get_family_id(), STRING_SORT); - char * new_buffer = alloc_svect(char, name.length() + 1); - strcpy(new_buffer, name.c_str()); - symbol sym(new_buffer); + sort * string_sort = m.mk_sort(get_family_id(), STRING_SORT); + app * a = m.mk_fresh_const(name.c_str(), string_sort); - app* a = m.mk_const(m.mk_const_decl(sym, string_sort)); ctx.internalize(a, false); SASSERT(ctx.get_enode(a) != NULL); // assert a variation of the basic string axioms that ensures this string is nonempty @@ -3843,7 +3837,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr TRACE("t_str_detail", tout << "entry" << std::endl;); - expr * freeVarLen = mk_strlen(freeVar); + expr_ref freeVarLen(mk_strlen(freeVar), m); SASSERT(freeVarLen); ptr_vector orList; @@ -3856,7 +3850,10 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr TRACE("t_str_detail", tout << "building andList and orList" << std::endl;); for (int i = l; i < h; ++i) { - expr * or_expr = m.mk_eq(indicator, m_strutil.mk_string(int_to_string(i).c_str())); + std::string i_str = int_to_string(i); + expr_ref str_indicator(m_strutil.mk_string(i_str), m); + TRACE("t_str_detail", tout << "just created a string term: " << mk_ismt2_pp(str_indicator, m) << std::endl;); + expr * or_expr = m.mk_eq(indicator, str_indicator); // ARGUMENT 2 IS BOGUS! WRONG SORT TRACE("t_str_detail", tout << "or_expr = " << mk_ismt2_pp(or_expr, m) << std::endl;); orList.push_back(or_expr); @@ -3868,6 +3865,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr orList.push_back(m.mk_eq(indicator, m_strutil.mk_string("more"))); andList.push_back(m.mk_eq(orList[orList.size() - 1], m_autil.mk_ge(freeVarLen, mk_int(h)))); + // TODO refactor this to use expr_ref_vector/svector/buffer instead expr ** or_items = alloc_svect(expr*, orList.size()); expr ** and_items = alloc_svect(expr*, andList.size() + 1); @@ -3965,8 +3963,8 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe fvar_len_count_map[freeVar] = 1; unsigned int testNum = fvar_len_count_map[freeVar]; - expr * indicator = mk_internal_lenTest_var(freeVar, testNum); - SASSERT(indicator != NULL); + expr_ref indicator(mk_internal_lenTest_var(freeVar, testNum), m); + SASSERT(indicator); // since the map is "effectively empty", we can remove those variables that have left scope... fvar_lenTester_map[freeVar].shrink(0); From 1a15b3937deb367adac5c64dae48e9e1298a6b5d Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 2 Dec 2015 22:09:30 -0500 Subject: [PATCH 074/562] in_same_eqc() now checks to ensure both terms are internalized before doing anything else --- src/smt/theory_str.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 9b9cc8fd9..62100cfcd 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1999,6 +1999,20 @@ expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { bool theory_str::in_same_eqc(expr * n1, expr * n2) { if (n1 == n2) return true; context & ctx = get_context(); + ast_manager & m = get_manager(); + + // similar to get_eqc_value(), make absolutely sure + // that we've set this up properly for the context + + if (!ctx.e_internalized(n1)) { + TRACE("t_str_detail", tout << "WARNING: expression " << mk_ismt2_pp(n1, m) << " was not internalized" << std::endl;); + ctx.internalize(n1, false); + } + if (!ctx.e_internalized(n2)) { + TRACE("t_str_detail", tout << "WARNING: expression " << mk_ismt2_pp(n2, m) << " was not internalized" << std::endl;); + ctx.internalize(n2, false); + } + enode * n1Node = ctx.get_enode(n1); enode * n2Node = ctx.get_enode(n2); From f5e94af784b19e021e367c688d295298abf214b8 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 2 Dec 2015 22:15:04 -0500 Subject: [PATCH 075/562] check that both simplified expressions are concats in simplify_concat_equality() this seems to fix all the crashes but the solver takes forever to solve a really simple instance with easy model generation, so I think something is still wrong probably next I will go through and change std::map to obj_map, etc. --- src/smt/theory_str.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 62100cfcd..f6187c7c1 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -922,10 +922,6 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { expr * new_nn2 = simplify_concat(nn2); app * a_new_nn1 = to_app(new_nn1); app * a_new_nn2 = to_app(new_nn2); - expr * v1_arg0 = a_new_nn1->get_arg(0); - expr * v1_arg1 = a_new_nn1->get_arg(1); - expr * v2_arg0 = a_new_nn2->get_arg(0); - expr * v2_arg1 = a_new_nn2->get_arg(1); TRACE("t_str_detail", tout << "new_nn1 = " << mk_ismt2_pp(new_nn1, m) << std::endl << "new_nn2 = " << mk_ismt2_pp(new_nn2, m) << std::endl;); @@ -960,6 +956,13 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { return; } + // TODO what happens if BOTH of these are simplified into non-concat terms? + + expr * v1_arg0 = a_new_nn1->get_arg(0); + expr * v1_arg1 = a_new_nn1->get_arg(1); + expr * v2_arg0 = a_new_nn2->get_arg(0); + expr * v2_arg1 = a_new_nn2->get_arg(1); + if (!in_same_eqc(new_nn1, new_nn2) && (nn1 != new_nn1 || nn2 != new_nn2)) { int ii4 = 0; expr* item[3]; From e010e7c0d606a4059bf8d4fd56777720679402f9 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 2 Dec 2015 23:35:26 -0500 Subject: [PATCH 076/562] add trace message to indicate which free variables are giving us trouble I think I'm onto the issue though --- src/smt/theory_str.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index f6187c7c1..a5584efd8 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -532,6 +532,8 @@ void theory_str::instantiate_concat_axiom(enode * cat) { ast_manager & m = get_manager(); + TRACE("t_str_detail", tout << "instantiating concat axiom for " << mk_ismt2_pp(a_cat, m) << std::endl;); + // build LHS expr_ref len_xy(m); // TODO should we use str_util for these and other expressions? @@ -560,7 +562,6 @@ void theory_str::instantiate_concat_axiom(enode * cat) { // finally assert equality between the two subexpressions app * eq = m.mk_eq(len_xy, len_x_plus_len_y); SASSERT(eq); - TRACE("t_str", tout << mk_ismt2_pp(eq, m) << std::endl;); assert_axiom(eq); } @@ -3443,6 +3444,13 @@ final_check_status theory_str::final_check_eh() { return FC_DONE; } + CTRACE("t_str", needToAssignFreeVars, + tout << "Need to assign values to the following free variables:" << std::endl; + for (std::set::iterator itx = free_variables.begin(); itx != free_variables.end(); ++itx) { + tout << mk_ismt2_pp(*itx, m) << std::endl; + } + ); + // ----------------------------------------------------------- // variables in freeVar are those not bounded by Concats // classify variables in freeVarMap: From cf5eacbf332d175bad5b46b9739016814a3991f3 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 3 Dec 2015 20:58:54 -0500 Subject: [PATCH 077/562] successful run of model generation test case, after assigning all internal variables a bogus value if they are unused --- src/smt/theory_str.cpp | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index a5584efd8..fb74f4c40 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3428,20 +3428,37 @@ final_check_status theory_str::final_check_eh() { // If not, mark it as free. bool needToAssignFreeVars = false; std::set free_variables; + std::set unused_internal_variables; TRACE("t_str_detail", tout << variable_set.size() << " variables in variable_set" << std::endl;); for (std::set::iterator it = variable_set.begin(); it != variable_set.end(); ++it) { TRACE("t_str_detail", tout << "checking eqc of variable " << mk_ismt2_pp(*it, m) << std::endl;); bool has_eqc_value = false; get_eqc_value(*it, has_eqc_value); if (!has_eqc_value) { - needToAssignFreeVars = true; - free_variables.insert(*it); + // if this is an internal variable, it can be ignored...I think + if (internal_variable_set.find(*it) != internal_variable_set.end()) { + TRACE("t_str_detail", tout << "WARNING: free internal variable " << mk_ismt2_pp(*it, m) << std::endl;); + unused_internal_variables.insert(*it); + } else { + needToAssignFreeVars = true; + free_variables.insert(*it); + } } } if (!needToAssignFreeVars) { - TRACE("t_str", tout << "All variables are assigned. Done!" << std::endl;); - return FC_DONE; + if (unused_internal_variables.empty()) { + TRACE("t_str", tout << "All variables are assigned. Done!" << std::endl;); + return FC_DONE; + } else { + TRACE("t_str", tout << "Assigning decoy values to free internal variables." << std::endl;); + for (std::set::iterator it = unused_internal_variables.begin(); it != unused_internal_variables.end(); ++it) { + expr * var = *it; + expr_ref assignment(m.mk_eq(var, m_strutil.mk_string("**unused**")), m); + assert_axiom(assignment); + } + return FC_CONTINUE; + } } CTRACE("t_str", needToAssignFreeVars, @@ -4218,7 +4235,7 @@ void theory_str::get_eqc_allUnroll(expr * n, expr * &constStr, std::set & } void theory_str::init_model(model_generator & mg) { - TRACE("t_str", tout << "initializing model" << std::endl; display(tout);); + //TRACE("t_str", tout << "initializing model" << std::endl; display(tout);); m_factory = alloc(str_value_factory, get_manager(), get_family_id()); mg.register_factory(m_factory); } @@ -4287,7 +4304,7 @@ model_value_proc * theory_str::mk_value(enode * n, model_generator & mg) { TRACE("t_str", tout << "WARNING: failed to find a concrete value, falling back" << std::endl;); // TODO make absolutely sure the reason we can't find a concrete value is because of an unassigned temporary // e.g. for an expression like (Concat X $$_str0) - //return alloc(expr_wrapper_proc, m_strutil.mk_string("")); + //return alloc(expr_wrapper_proc, m_strutil.mk_string("**UNUSED**")); NOT_IMPLEMENTED_YET(); } } From a2d0299621c2c7856329ea32c26aa264ed8ff2c3 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 7 May 2016 14:19:12 -0400 Subject: [PATCH 078/562] call super in push and pop --- src/smt/theory_str.cpp | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index fb74f4c40..61488638c 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -191,26 +191,6 @@ static void cut_vars_map_copy(std::map & dest, std::map } } -/* -bool hasSelfCut(Z3_ast n1, Z3_ast n2) { - if (cut_VARMap.find(n1) == cut_VARMap.end()) - return false; - - if (cut_VARMap.find(n2) == cut_VARMap.end()) - return false; - - if (cut_VARMap[n1].empty() || cut_VARMap[n2].empty()) - return false; - - std::map::iterator itor = cut_VARMap[n1].top()->vars.begin(); - for (; itor != cut_VARMap[n1].top()->vars.end(); itor++) { - if (cut_VARMap[n2].top()->vars.find(itor->first) != cut_VARMap[n2].top()->vars.end()) - return true; - } - return false; -} -*/ - bool theory_str::has_self_cut(expr * n1, expr * n2) { if (cut_var_map.find(n1) == cut_var_map.end()) { return false; @@ -2653,9 +2633,11 @@ void theory_str::assign_eh(bool_var v, bool is_true) { } void theory_str::push_scope_eh() { + theory::push_scope_eh(); context & ctx = get_context(); sLevel += 1; TRACE("t_str", tout << "push to " << sLevel << std::endl;); + TRACE("t_str_dump_assign", dump_assignments();); } void theory_str::pop_scope_eh(unsigned num_scopes) { @@ -2692,7 +2674,7 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { vars.clear(); } } - + theory::pop_scope_eh(num_scopes); } void theory_str::dump_assignments() { @@ -3128,7 +3110,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map v2, v5 are constrained by "str" - // - possibliity 2 : concat(v1, "str") = concat(v2, v3) = concat(v4, v5) + // - possibility 2 : concat(v1, "str") = concat(v2, v3) = concat(v4, v5) // ==> v2, v4 are constrained by "str" //-------------------------------------------------------------- From 1d324877cdf16e6547ef0a909a491e9e99ff3cef Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 7 May 2016 15:40:39 -0400 Subject: [PATCH 079/562] use theory_seq's internalize_term --- src/smt/theory_str.cpp | 67 +++++++++++++++++++++++++++++++++++++++--- src/smt/theory_str.h | 4 ++- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 61488638c..fc9a7f3d5 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -139,6 +139,7 @@ void theory_str::assert_implication(expr * premise, expr * conclusion) { } bool theory_str::internalize_atom(app * atom, bool gate_ctx) { + /* TRACE("t_str", tout << "internalizing atom: " << mk_ismt2_pp(atom, get_manager()) << std::endl;); SASSERT(atom->get_family_id() == get_family_id()); @@ -152,15 +153,21 @@ bool theory_str::internalize_atom(app * atom, bool gate_ctx) { ctx.internalize(atom->get_arg(i), false); literal l(ctx.mk_bool_var(atom)); + ctx.set_var_theory(l.var(), get_id()); return true; + */ + return internalize_term(atom); } bool theory_str::internalize_term(app * term) { context & ctx = get_context(); + ast_manager & m = get_manager(); TRACE("t_str", tout << "internalizing term: " << mk_ismt2_pp(term, get_manager()) << std::endl;); SASSERT(term->get_family_id() == get_family_id()); + + /* // what I had before SASSERT(!ctx.e_internalized(term)); unsigned num_args = term->get_num_args(); @@ -175,15 +182,67 @@ bool theory_str::internalize_term(app * term) { attach_new_th_var(e); - /* - if (is_concat(term)) { - instantiate_concat_axiom(e); - } + //if (is_concat(term)) { + // instantiate_concat_axiom(e); + //} */ + // from theory_seq::internalize_term() + if (ctx.e_internalized(term)) { + enode* e = ctx.get_enode(term); + mk_var(e); + return true; + } + unsigned num_args = term->get_num_args(); + expr* arg; + for (unsigned i = 0; i < num_args; i++) { + arg = term->get_arg(i); + mk_var(ensure_enode(arg)); + } + if (m.is_bool(term)) { + bool_var bv = ctx.mk_bool_var(term); + ctx.set_var_theory(bv, get_id()); + ctx.mark_as_relevant(bv); + } + + enode* e = 0; + if (ctx.e_internalized(term)) { + e = ctx.get_enode(term); + } + else { + e = ctx.mk_enode(term, false, m.is_bool(term), true); + } + mk_var(e); + return true; } +enode* theory_str::ensure_enode(expr* e) { + context& ctx = get_context(); + if (!ctx.e_internalized(e)) { + ctx.internalize(e, false); + } + enode* n = ctx.get_enode(e); + ctx.mark_as_relevant(n); + return n; +} + +theory_var theory_str::mk_var(enode* n) { + if (!m_strutil.is_string(n->get_owner())) { + return null_theory_var; + } + if (is_attached_to_var(n)) { + return n->get_th_var(get_id()); + } + else { + theory_var v = theory::mk_var(n); + // m_find.mk_var(); + get_context().attach_th_var(n, this, v); + get_context().mark_as_relevant(n); + return v; + } +} + static void cut_vars_map_copy(std::map & dest, std::map & src) { std::map::iterator itor = src.begin(); for (; itor != src.end(); itor++) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index ca985cb8f..af2ea1db6 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -208,6 +208,8 @@ namespace smt { protected: virtual bool internalize_atom(app * atom, bool gate_ctx); virtual bool internalize_term(app * term); + virtual enode* ensure_enode(expr* e); + virtual theory_var mk_var(enode * n); virtual void new_eq_eh(theory_var, theory_var); virtual void new_diseq_eh(theory_var, theory_var); @@ -224,7 +226,7 @@ namespace smt { virtual void propagate(); virtual final_check_status final_check_eh(); - void attach_new_th_var(enode * n); + virtual void attach_new_th_var(enode * n); virtual void init_model(model_generator & m); virtual model_value_proc * mk_value(enode * n, model_generator & mg); From 6dfc2dd9100a39f705000c54f80930f19d65a08c Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 7 May 2016 17:16:31 -0400 Subject: [PATCH 080/562] variables of sort String should now correctly be identified as Very Relevant to the string solver --- src/smt/theory_str.cpp | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index fc9a7f3d5..37b68e48c 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -212,7 +212,8 @@ bool theory_str::internalize_term(app * term) { else { e = ctx.mk_enode(term, false, m.is_bool(term), true); } - mk_var(e); + theory_var v = mk_var(e); + TRACE("t_str_detail", tout << "term " << mk_ismt2_pp(term, get_manager()) << " = v#" << v << std::endl;); return true; } @@ -228,9 +229,16 @@ enode* theory_str::ensure_enode(expr* e) { } theory_var theory_str::mk_var(enode* n) { + /* if (!m_strutil.is_string(n->get_owner())) { return null_theory_var; } + */ + // TODO this may require an overhaul of m_strutil.is_string() if things suddenly start working after the following change: + ast_manager & m = get_manager(); + if (!(is_sort_of(m.get_sort(n->get_owner()), m_strutil.get_fid(), STRING_SORT))) { + return null_theory_var; + } if (is_attached_to_var(n)) { return n->get_th_var(get_id()); } @@ -416,6 +424,8 @@ app * theory_str::mk_str_var(std::string name) { ctx.internalize(a, false); SASSERT(ctx.get_enode(a) != NULL); SASSERT(ctx.e_internalized(a)); + // this might help?? + mk_var(ctx.get_enode(a)); m_basicstr_axiom_todo.push_back(ctx.get_enode(a)); m_trail.push_back(a); @@ -442,6 +452,9 @@ app * theory_str::mk_nonempty_str_var() { ctx.internalize(a, false); SASSERT(ctx.get_enode(a) != NULL); + // this might help?? + mk_var(ctx.get_enode(a)); + // assert a variation of the basic string axioms that ensures this string is nonempty { // build LHS @@ -2496,11 +2509,11 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { ); // step 1: Concat == Concat - // I'm disabling this entire code block for now. It may no longer be useful. + // This code block may no longer be useful. // Z3 seems to be putting LHS and RHS into the same equivalence class extremely early. // As a result, simplify_concat_equality() is never getting called, // and if it were called, it would probably get called with the same element on both sides. - /* + bool hasCommon = false; if (eqc_lhs_concat.size() != 0 && eqc_rhs_concat.size() != 0) { std::set::iterator itor1 = eqc_lhs_concat.begin(); @@ -2521,7 +2534,7 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { simplify_concat_equality(*(eqc_lhs_concat.begin()), *(eqc_rhs_concat.begin())); } } - */ + if (eqc_lhs_concat.size() != 0 && eqc_rhs_concat.size() != 0) { // let's pick the first concat in the LHS's eqc // and find some concat in the RHS's eqc that is @@ -2591,6 +2604,10 @@ void theory_str::set_up_axioms(expr * ex) { // if ex is a variable, add it to our list of variables TRACE("t_str_detail", tout << "tracking variable " << mk_ismt2_pp(ap, get_manager()) << std::endl;); variable_set.insert(ex); + ctx.mark_as_relevant(ex); + // this might help?? + theory_var v = mk_var(n); + TRACE("t_str_detail", tout << "variable " << mk_ismt2_pp(ap, get_manager()) << " is #" << v << std::endl;); } } } else { @@ -2637,7 +2654,7 @@ void theory_str::init_search_eh() { * This is done to find equalities between terms, etc. that we otherwise * might not get a chance to see. */ - /* + expr_ref_vector assignments(m); ctx.get_assignments(assignments); for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { @@ -2659,7 +2676,6 @@ void theory_str::init_search_eh() { << ": expr ignored" << std::endl;); } } - */ TRACE("t_str", tout << "search started" << std::endl;); search_started = true; From bcaad06061b2981669c84770462192d0886791e3 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 7 May 2016 17:47:50 -0400 Subject: [PATCH 081/562] add theory name; add debug info for freeVar_map --- src/smt/theory_str.cpp | 11 +++++++---- src/smt/theory_str.h | 2 ++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 37b68e48c..3903508ea 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2655,6 +2655,7 @@ void theory_str::init_search_eh() { * might not get a chance to see. */ + /* expr_ref_vector assignments(m); ctx.get_assignments(assignments); for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { @@ -2676,6 +2677,7 @@ void theory_str::init_search_eh() { << ": expr ignored" << std::endl;); } } + */ TRACE("t_str", tout << "search started" << std::endl;); search_started = true; @@ -2686,16 +2688,12 @@ void theory_str::new_eq_eh(theory_var x, theory_var y) { TRACE("t_str", tout << "new eq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " = " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); handle_equality(get_enode(x)->get_owner(), get_enode(y)->get_owner()); - - TRACE("t_str_dump_assign", dump_assignments();); } void theory_str::new_diseq_eh(theory_var x, theory_var y) { //TRACE("t_str_detail", tout << "new diseq: v#" << x << " != v#" << y << std::endl;); TRACE("t_str", tout << "new diseq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " != " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); - - TRACE("t_str_dump_assign", dump_assignments();); } void theory_str::relevant_eh(app * n) { @@ -3523,6 +3521,11 @@ final_check_status theory_str::final_check_eh() { for (std::set::iterator itx = free_variables.begin(); itx != free_variables.end(); ++itx) { tout << mk_ismt2_pp(*itx, m) << std::endl; } + tout << "freeVar_map has the following entries:" << std::endl; + for (std::map::iterator fvIt = freeVar_map.begin(); fvIt != freeVar_map.end(); fvIt++) { + expr * var = fvIt->first; + tout << mk_ismt2_pp(var, m) << std::endl; + } ); // ----------------------------------------------------------- diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index af2ea1db6..9d56c01fe 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -205,6 +205,8 @@ namespace smt { public: theory_str(ast_manager & m); virtual ~theory_str(); + + virtual char const * get_name() const { return "strings"; } protected: virtual bool internalize_atom(app * atom, bool gate_ctx); virtual bool internalize_term(app * term); From f9e1ed4496a859a771bdb100a34dc2c0cf533f8a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 9 May 2016 18:12:21 -0400 Subject: [PATCH 082/562] add simplify_parent() --- src/ast/str_decl_plugin.cpp | 8 + src/ast/str_decl_plugin.h | 2 + src/smt/theory_str.cpp | 359 +++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 1 + 4 files changed, 368 insertions(+), 2 deletions(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 550789065..cd9cae5a5 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -169,6 +169,14 @@ bool str_recognizers::is_string(expr const * n) const { return is_string(n, & tmp); } +std::string str_recognizers::get_string_constant_value(expr const *n) const { + const char * cstr = 0; + bool isString = is_string(n, & cstr); + SASSERT(isString); + std::string strval(cstr); + return strval; +} + str_util::str_util(ast_manager &m) : str_recognizers(m.mk_family_id(symbol("str"))), m_manager(m) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index f1978ab8b..4f46fa5ac 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -81,6 +81,8 @@ public: bool is_string(expr const * n, const char ** val) const; bool is_string(expr const * n) const; + + std::string get_string_constant_value(expr const *n) const; // TODO }; diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 3903508ea..254d32141 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -797,6 +797,34 @@ void theory_str::get_nodes_in_concat(expr * node, ptr_vector & nodeList) { } } +// previously Concat() in strTheory.cpp +// Evaluates the concatenation (n1 . n2) with respect to +// the current equivalence classes of n1 and n2. +// Returns a constant string expression representing this concatenation +// if one can be determined, or NULL if this is not possible. +expr * theory_str::eval_concat(expr * n1, expr * n2) { + bool n1HasEqcValue = false; + bool n2HasEqcValue = false; + expr * v1 = get_eqc_value(n1, n1HasEqcValue); + expr * v2 = get_eqc_value(n2, n2HasEqcValue); + if (n1HasEqcValue && n2HasEqcValue) { + std::string n1_str = m_strutil.get_string_constant_value(v1); + std::string n2_str = m_strutil.get_string_constant_value(v2); + std::string result = n1_str + n2_str; + return m_strutil.mk_string(result); + } else if (n1HasEqcValue && !n2HasEqcValue) { + if (m_strutil.get_string_constant_value(v1) == "") { + return n2; + } + } else if (n2HasEqcValue && !n1HasEqcValue) { + if (m_strutil.get_string_constant_value(v2) == "") { + return n1; + } + } + // give up + return NULL; +} + /* * The inputs: * ~ nn: non const node @@ -806,8 +834,298 @@ void theory_str::get_nodes_in_concat(expr * node, ptr_vector & nodeList) { * to see whether some concat nodes can be simplified. */ +// TODO NEXT complete this method! void theory_str::simplify_parent(expr * nn, expr * eq_str) { - // TODO strTheory::simplifyParent() + ast_manager & m = get_manager(); + context & ctx = get_context(); + + TRACE("t_str", tout << "simplifying parents of " << mk_ismt2_pp(nn, m) + << " with respect to " << mk_ismt2_pp(eq_str, m) << std::endl;); + + ctx.internalize(nn, false); + enode * n_eq_enode = ctx.get_enode(nn); + enode * nn_enode = n_eq_enode; + + const char * tmp = 0; + m_strutil.is_string(eq_str, & tmp); + std::string eq_strValue(tmp); + + do { + app * n_eqNode = n_eq_enode->get_owner(); + for (enode_vector::iterator parent_it = n_eq_enode->begin_parents(); parent_it != n_eq_enode->end_parents(); parent_it++) { + enode * e_parent = *parent_it; + app * a_parent = e_parent->get_owner(); + TRACE("t_str_detail", tout << "considering parent " << mk_ismt2_pp(a_parent, m) << std::endl;); + + if (is_concat(a_parent)) { + expr * arg0 = a_parent->get_arg(0); + expr * arg1 = a_parent->get_arg(1); + + // TODO getLenValue() + // int parentLen = getLenValue(a_parent) + int parentLen = -1; + if (arg0 == n_eq_enode->get_owner()) { + // TODO getLenValue() + // int arg0Len = getLenValue(eq_str); + // int arg1Len = getLenValue(arg1); + int arg0Len = -1; + int arg1Len = -1; + + TRACE("t_str_detail", + tout << "simplify_parent #1:" << std::endl + << "* parent = " << mk_ismt2_pp(a_parent, m) << std::endl + << "* |parent| = " << parentLen << std::endl + << "* |arg0| = " << arg0Len << std::endl + << "* |arg1| = " << arg1Len << std::endl; + ); + + if (parentLen != -1 && arg1Len == -1) { + // TODO after getLenValue() above + /* + Z3_ast implyL11 = mk_2_and(t, Z3_mk_eq(ctx, mk_length(t, parent), mk_int(ctx, parentLen)), + Z3_mk_eq(ctx, mk_length(t, arg0), mk_int(ctx, arg0Len))); + int makeUpLenArg1 = parentLen - arg0Len; + Z3_ast lenAss = NULL; + if (makeUpLenArg1 >= 0) { + Z3_ast implyR11 = Z3_mk_eq(ctx, mk_length(t, arg1), mk_int(ctx, makeUpLenArg1)); + lenAss = Z3_mk_implies(ctx, implyL11, implyR11); + } else { + lenAss = Z3_mk_not(ctx, implyL11); + } + addAxiom(t, lenAss, __LINE__); + */ + } + + // (Concat n_eqNode arg1) /\ arg1 has eq const + + expr * concatResult = eval_concat(eq_str, arg1); + if (concatResult != NULL) { + bool arg1HasEqcValue = false; + expr * arg1Value = get_eqc_value(arg1, arg1HasEqcValue); + expr_ref implyL(m); + if (arg1 != arg1Value) { + expr_ref eq_ast1(m); + eq_ast1 = ctx.mk_eq_atom(n_eqNode, eq_str); + SASSERT(eq_ast1); + + expr_ref eq_ast2(m); + eq_ast2 = ctx.mk_eq_atom(arg1, arg1Value); + SASSERT(eq_ast2); + + implyL = m.mk_and(eq_ast1, eq_ast2); + } else { + implyL = ctx.mk_eq_atom(n_eqNode, eq_str); + } + + + if (!in_same_eqc(a_parent, concatResult)) { + expr_ref implyR(m); + implyR = ctx.mk_eq_atom(a_parent, concatResult); + SASSERT(implyR); + + assert_implication(implyL, implyR); + } + } else if (is_concat(n_eqNode)) { + expr_ref simpleConcat(m); + simpleConcat = mk_concat(eq_str, arg1); + if (!in_same_eqc(a_parent, simpleConcat)) { + expr_ref implyL(m); + implyL = ctx.mk_eq_atom(n_eqNode, eq_str); + SASSERT(implyL); + + expr_ref implyR(m); + implyR = ctx.mk_eq_atom(a_parent, simpleConcat); + SASSERT(implyR); + assert_implication(implyL, implyR); + } + } + } // if (arg0 == n_eq_enode->get_owner()) + + if (arg1 == n_eq_enode->get_owner()) { + // TODO getLenValue() + // int arg0Len = getLenValue(arg0); + // int arg1Len = getLenValue(eq_str); + int arg0Len = -1; + int arg1Len = -1; + + TRACE("t_str_detail", + tout << "simplify_parent #2:" << std::endl + << "* parent = " << mk_ismt2_pp(a_parent, m) << std::endl + << "* |parent| = " << parentLen << std::endl + << "* |arg0| = " << arg0Len << std::endl + << "* |arg1| = " << arg1Len << std::endl; + ); + + if (parentLen != -1 && arg0Len == -1) { + // TODO after getLenValue() above + /* + Z3_ast implyL11 = mk_2_and(t, Z3_mk_eq(ctx, mk_length(t, parent), mk_int(ctx, parentLen)), + Z3_mk_eq(ctx, mk_length(t, arg1), mk_int(ctx, arg1Len))); + int makeUpLenArg0 = parentLen - arg1Len; + Z3_ast lenAss = NULL; + if (makeUpLenArg0 >= 0) { + Z3_ast implyR11 = Z3_mk_eq(ctx, mk_length(t, arg0), mk_int(ctx, makeUpLenArg0)); + lenAss = Z3_mk_implies(ctx, implyL11, implyR11); + } else { + lenAss = Z3_mk_not(ctx, implyL11); + } + addAxiom(t, lenAss, __LINE__); + */ + } + + // (Concat arg0 n_eqNode) /\ arg0 has eq const + + expr * concatResult = eval_concat(eq_str, arg1); + if (concatResult != NULL) { + bool arg0HasEqcValue = false; + expr * arg0Value = get_eqc_value(arg0, arg0HasEqcValue); + expr_ref implyL(m); + if (arg0 != arg0Value) { + expr_ref eq_ast1(m); + eq_ast1 = ctx.mk_eq_atom(n_eqNode, eq_str); + SASSERT(eq_ast1); + + expr_ref eq_ast2(m); + eq_ast2 = ctx.mk_eq_atom(arg0, arg0Value); + SASSERT(eq_ast2); + + implyL = m.mk_and(eq_ast1, eq_ast2); + } else { + implyL = ctx.mk_eq_atom(n_eqNode, eq_str); + } + + if (!in_same_eqc(a_parent, concatResult)) { + expr_ref implyR(m); + implyR = ctx.mk_eq_atom(a_parent, concatResult); + SASSERT(implyR); + + assert_implication(implyL, implyR); + } + } else if (is_concat(n_eqNode)) { + expr_ref simpleConcat(m); + simpleConcat = mk_concat(arg0, eq_str); + if (!in_same_eqc(a_parent, simpleConcat)) { + expr_ref implyL(m); + implyL = ctx.mk_eq_atom(n_eqNode, eq_str); + SASSERT(implyL); + + expr_ref implyR(m); + implyR = ctx.mk_eq_atom(a_parent, simpleConcat); + SASSERT(implyR); + assert_implication(implyL, implyR); + } + } + } // if (arg1 == n_eq_enode->get_owner + + + //--------------------------------------------------------- + // Case (2-1) begin: (Concat n_eqNode (Concat str var)) + if (arg0 == n_eqNode && is_concat(to_app(arg1))) { + app * a_arg1 = to_app(arg1); + TRACE("t_str_detail", tout << "simplify_parent #3" << std::endl;); + expr * r_concat_arg0 = a_arg1->get_arg(0); + if (m_strutil.is_string(r_concat_arg0)) { + expr * combined_str = eval_concat(eq_str, r_concat_arg0); + SASSERT(combined_str); + expr * r_concat_arg1 = a_arg1->get_arg(1); + expr_ref implyL(m); + implyL = ctx.mk_eq_atom(n_eqNode, eq_str); + expr * simplifiedAst = mk_concat(combined_str, r_concat_arg1); + if (!in_same_eqc(a_parent, simplifiedAst)) { + expr_ref implyR(m); + implyR = ctx.mk_eq_atom(a_parent, simplifiedAst); + assert_implication(implyL, implyR); + } + } + } + // Case (2-1) end: (Concat n_eqNode (Concat str var)) + //--------------------------------------------------------- + + + //--------------------------------------------------------- + // Case (2-2) begin: (Concat (Concat var str) n_eqNode) + if (is_concat(to_app(arg0)) && arg1 == n_eqNode) { + app * a_arg0 = to_app(arg0); + TRACE("t_str_detail", tout << "simplify_parent #4" << std::endl;); + expr * l_concat_arg1 = a_arg0->get_arg(1); + if (m_strutil.is_string(l_concat_arg1)) { + expr * combined_str = eval_concat(l_concat_arg1, eq_str); + SASSERT(combined_str); + expr * l_concat_arg0 = a_arg0->get_arg(0); + expr_ref implyL(m); + implyL = ctx.mk_eq_atom(n_eqNode, eq_str); + expr * simplifiedAst = mk_concat(l_concat_arg0, combined_str); + if (!in_same_eqc(a_parent, simplifiedAst)) { + expr_ref implyR(m); + implyR = ctx.mk_eq_atom(a_parent, simplifiedAst); + assert_implication(implyL, implyR); + } + } + } + // Case (2-2) end: (Concat (Concat var str) n_eqNode) + //--------------------------------------------------------- + + // Have to look up one more layer: if the parent of the concat is another concat + //------------------------------------------------- + // Case (3-1) begin: (Concat (Concat var n_eqNode) str ) + if (arg1 == n_eqNode) { + for (enode_vector::iterator concat_parent_it = e_parent->begin_parents(); + concat_parent_it != e_parent->end_parents(); concat_parent_it++) { + enode * e_concat_parent = *concat_parent_it; + app * concat_parent = e_concat_parent->get_owner(); + if (is_concat(concat_parent)) { + expr * concat_parent_arg0 = concat_parent->get_arg(0); + expr * concat_parent_arg1 = concat_parent->get_arg(1); + if (concat_parent_arg0 == a_parent && m_strutil.is_string(concat_parent_arg1)) { + TRACE("t_str_detail", tout << "simplify_parent #5" << std::endl;); + expr * combinedStr = eval_concat(eq_str, concat_parent_arg1); + SASSERT(combinedStr); + expr_ref implyL(m); + implyL = ctx.mk_eq_atom(n_eqNode, eq_str); + expr * simplifiedAst = mk_concat(arg0, combinedStr); + if (!in_same_eqc(concat_parent, simplifiedAst)) { + expr_ref implyR(m); + implyR = ctx.mk_eq_atom(concat_parent, simplifiedAst); + assert_implication(implyL, implyR); + } + } + } + } + } + // Case (3-1) end: (Concat (Concat var n_eqNode) str ) + // Case (3-2) begin: (Concat str (Concat n_eqNode var) ) + if (arg0 == n_eqNode) { + for (enode_vector::iterator concat_parent_it = e_parent->begin_parents(); + concat_parent_it != e_parent->end_parents(); concat_parent_it++) { + enode * e_concat_parent = *concat_parent_it; + app * concat_parent = e_concat_parent->get_owner(); + if (is_concat(concat_parent)) { + expr * concat_parent_arg0 = concat_parent->get_arg(0); + expr * concat_parent_arg1 = concat_parent->get_arg(1); + if (concat_parent_arg1 == a_parent && m_strutil.is_string(concat_parent_arg0)) { + TRACE("t_str_detail", tout << "simplify_parent #6" << std::endl;); + expr * combinedStr = eval_concat(concat_parent_arg0, eq_str); + SASSERT(combinedStr); + expr_ref implyL(m); + implyL = ctx.mk_eq_atom(n_eqNode, eq_str); + expr * simplifiedAst = mk_concat(combinedStr, arg1); + if (!in_same_eqc(concat_parent, simplifiedAst)) { + expr_ref implyR(m); + implyR = ctx.mk_eq_atom(concat_parent, simplifiedAst); + assert_implication(implyL, implyR); + } + } + } + } + } + // Case (3-2) end: (Concat str (Concat n_eqNode var) ) + } // if is_concat(a_parent) + } // for parent_it : n_eq_enode->begin_parents() + + + // check next EQC member + n_eq_enode = n_eq_enode->get_next(); + } while (n_eq_enode != nn_enode); } expr * theory_str::simplify_concat(expr * node) { @@ -2565,7 +2883,44 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { } } - // TODO simplify_parent over eqc + // simplify parents wrt. the equivalence class of both sides + // TODO this is slightly broken, re-enable it once some semantics have been fixed + // Briefly, Z3str2 expects that as this function is entered, + // lhs and rhs are NOT in the same equivalence class yet. + // However, newer versions of Z3 appear to behave differently, + // putting lhs and rhs into the same equivalence class + // *before* this function is called. + // Instead we do something possibly more aggressive here. + /* + bool lhs_has_eqc_value = false; + bool rhs_has_eqc_value = false; + expr * lhs_value = get_eqc_value(lhs, lhs_has_eqc_value); + expr * rhs_value = get_eqc_value(rhs, rhs_has_eqc_value); + if (lhs_has_eqc_value && !rhs_has_eqc_value) { + simplify_parent(rhs, lhs_value); + } + if (!lhs_has_eqc_value && rhs_has_eqc_value) { + simplify_parent(lhs, rhs_value); + } + */ + + bool lhs_has_eqc_value = false; + bool rhs_has_eqc_value = false; + expr * lhs_value = get_eqc_value(lhs, lhs_has_eqc_value); + expr * rhs_value = get_eqc_value(rhs, rhs_has_eqc_value); + + // TODO this depends on the old, possibly broken, semantics of is_string(). + // we explicitly want to test whether lhs/rhs is actually a string constant. + bool lhs_is_string_constant = m_strutil.is_string(lhs); + bool rhs_is_string_constant = m_strutil.is_string(rhs); + + + if (lhs_has_eqc_value && !rhs_is_string_constant) { + simplify_parent(rhs, lhs_value); + } + if (rhs_has_eqc_value && !lhs_is_string_constant) { + simplify_parent(lhs, rhs_value); + } // TODO regex unroll? (much later) } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 9d56c01fe..e167beb18 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -195,6 +195,7 @@ namespace smt { expr * getMostLeftNodeInConcat(expr * node); expr * getMostRightNodeInConcat(expr * node); void get_var_in_eqc(expr * n, std::set & varSet); + expr * eval_concat(expr * n1, expr * n2); // strRegex From 9fc1410495bb6184606d603ba2f2eda104e23a9e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 17 May 2016 14:53:17 -0400 Subject: [PATCH 083/562] remove incorrect not-null assertions for model gen --- src/smt/theory_str.cpp | 54 +++++++++++++++++++++++++++++++++++------- src/smt/theory_str.h | 2 ++ 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 254d32141..eace51dcb 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2748,6 +2748,35 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } } +bool theory_str::free_var_attempt(expr * nn1, expr * nn2) { + /* + Z3_context ctx = Z3_theory_get_context(t); + if (getNodeType(t, nn1) == my_Z3_Str_Var) { + std::string vName = std::string(Z3_ast_to_string(ctx, nn1)); + if (vName.length() >= 6) { + std::string vPrefix = vName.substr(0, 6); + // length attempts + if (vPrefix == "$$_len") { + if (getNodeType(t, nn2) == my_Z3_ConstStr) { + moreLenTests(t, nn1, getConstStrValue(t, nn2)); + } + return 1; + } + // value attempts + else if (vPrefix == "$$_val") { + if (getNodeType(t, nn2) == my_Z3_ConstStr && "more" == getConstStrValue(t, nn2)) { + moreValueTests(t, nn1, getConstStrValue(t, nn2)); + } + return 1; + } else if (vPrefix == "$$_uRt") { + return 1; + } + } + } + return 0; + */ +} + void theory_str::handle_equality(expr * lhs, expr * rhs) { ast_manager & m = get_manager(); context & ctx = get_context(); @@ -2761,8 +2790,6 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { return; } - // TODO freeVarAttempt()? - // TODO simplify concat? // newEqCheck() -- check consistency wrt. existing equivalence classes @@ -4245,6 +4272,8 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, return gen_val_options(freeVar, len_indicator, val_indicator, len_valueStr, tries); } else { TRACE("t_str_detail", tout << "checking previous value testers" << std::endl;); + print_value_tester_list(fvar_valueTester_map[freeVar][len]); + // go through all previous value testers // If some doesn't have an eqc value, add its assertion again. int testerTotal = fvar_valueTester_map[freeVar][len].size(); @@ -4258,16 +4287,19 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, bool anEqcHasValue = false; // Z3_ast anEqc = get_eqc_value(t, aTester, anEqcHasValue); - get_eqc_value(aTester, anEqcHasValue); + expr * aTester_eqc_value = get_eqc_value(aTester, anEqcHasValue); if (!anEqcHasValue) { TRACE("t_str_detail", tout << "value tester " << mk_ismt2_pp(aTester, m) - << "doesn't have an equivalence class value." << std::endl;); + << " doesn't have an equivalence class value." << std::endl;); expr * makeupAssert = gen_val_options(freeVar, len_indicator, aTester, len_valueStr, i); TRACE("t_str_detail", tout << "var: " << mk_ismt2_pp(freeVar, m) << std::endl << mk_ismt2_pp(makeupAssert, m) << std::endl;); assert_axiom(makeupAssert); + } else { + TRACE("t_str_detail", tout << "value tester " << mk_ismt2_pp(aTester, m) + << " == " << mk_ismt2_pp(aTester_eqc_value, m) << std::endl;); } } @@ -4506,6 +4538,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe testNum = i + 1; } expr * lenTestAssert = gen_len_test_options(freeVar, indicator, testNum); + SASSERT(lenTestAssert != NULL); return lenTestAssert; } else { TRACE("t_str", tout << "length is fixed; generating models for free var" << std::endl;); @@ -4610,8 +4643,11 @@ void theory_str::process_free_var(std::map & freeVar_map) { for(std::set::iterator itor1 = leafVarSet.begin(); itor1 != leafVarSet.end(); ++itor1) { expr * toAssert = gen_len_val_options_for_free_var(*itor1, NULL, ""); - SASSERT(toAssert != NULL); - assert_axiom(toAssert); + // gen_len_val_options_for_free_var() can legally return NULL, + // as methods that it calls may assert their own axioms instead. + if (toAssert != NULL) { + assert_axiom(toAssert); + } } for (std::map >::iterator mItor = aloneVars.begin(); @@ -4619,8 +4655,10 @@ void theory_str::process_free_var(std::map & freeVar_map) { std::set::iterator itor2 = mItor->second.begin(); for(; itor2 != mItor->second.end(); ++itor2) { expr * toAssert = gen_len_val_options_for_free_var(*itor2, NULL, ""); - SASSERT(toAssert != NULL); - assert_axiom(toAssert); + // same deal with returning a NULL axiom here + if(toAssert != NULL) { + assert_axiom(toAssert); + } } } } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index e167beb18..242b37747 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -191,6 +191,8 @@ namespace smt { bool get_next_val_encode(int_vector & base, int_vector & next); std::string gen_val_string(int len, int_vector & encoding); + bool free_var_attempt(expr * nn1, expr * nn2); + expr * get_alias_index_ast(std::map & aliasIndexMap, expr * node); expr * getMostLeftNodeInConcat(expr * node); expr * getMostRightNodeInConcat(expr * node); From 2f80a9d4aecd78e5c483c6f9410f2dd60d81398e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 17 May 2016 16:31:08 -0400 Subject: [PATCH 084/562] add more_len_tests, more_value_tests --- src/smt/theory_str.cpp | 110 +++++++++++++++++++++++++---------------- src/smt/theory_str.h | 5 ++ 2 files changed, 72 insertions(+), 43 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index eace51dcb..ff0aacfa1 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -357,15 +357,9 @@ expr * theory_str::mk_internal_lenTest_var(expr * node, int lTries) { ss << "$$_len_" << mk_ismt2_pp(node, m) << "_" << lTries << "_" << tmpLenTestVarCount; tmpLenTestVarCount += 1; std::string name = ss.str(); - return mk_str_var(name); - - /* - Z3_context ctx = Z3_theory_get_context(t); - std::stringstream ss; - ss << "$$_len_" << Z3_ast_to_string(ctx, node) << "_" << lTries; - std::string name = ss.str(); - return my_mk_str_var(t, name.c_str()); - */ + app * var = mk_str_var(name); + internal_lenTest_vars.insert(var); + return var; } expr * theory_str::mk_internal_valTest_var(expr * node, int len, int vTries) { @@ -374,15 +368,9 @@ expr * theory_str::mk_internal_valTest_var(expr * node, int len, int vTries) { ss << "$$_val_" << mk_ismt2_pp(node, m) << "_" << len << "_" << vTries << "_" << tmpValTestVarCount; tmpValTestVarCount += 1; std::string name = ss.str(); - return mk_str_var(name); - - /* - Z3_context ctx = Z3_theory_get_context(t); - std::stringstream ss; - ss << "$$_val_" << Z3_ast_to_string(ctx, node) << "_" << len << "_" << vTries; - std::string name = ss.str(); - return my_mk_str_var(t, name.c_str()); - */ + app * var = mk_str_var(name); + internal_valTest_vars.insert(var); + return var; } void theory_str::track_variable_scope(expr * var) { @@ -2748,33 +2736,65 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } } -bool theory_str::free_var_attempt(expr * nn1, expr * nn2) { - /* - Z3_context ctx = Z3_theory_get_context(t); - if (getNodeType(t, nn1) == my_Z3_Str_Var) { - std::string vName = std::string(Z3_ast_to_string(ctx, nn1)); - if (vName.length() >= 6) { - std::string vPrefix = vName.substr(0, 6); - // length attempts - if (vPrefix == "$$_len") { - if (getNodeType(t, nn2) == my_Z3_ConstStr) { - moreLenTests(t, nn1, getConstStrValue(t, nn2)); - } - return 1; +void theory_str::more_len_tests(expr * lenTester, std::string lenTesterValue) { + ast_manager & m = get_manager(); + if (lenTester_fvar_map.find(lenTester) != lenTester_fvar_map.end()) { + expr * fVar = lenTester_fvar_map[lenTester]; + expr * toAssert = gen_len_val_options_for_free_var(fVar, lenTester, lenTesterValue); + TRACE("t_str_detail", tout << "asserting more length tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); + if (toAssert != NULL) { + assert_axiom(toAssert); } - // value attempts - else if (vPrefix == "$$_val") { - if (getNodeType(t, nn2) == my_Z3_ConstStr && "more" == getConstStrValue(t, nn2)) { - moreValueTests(t, nn1, getConstStrValue(t, nn2)); - } - return 1; - } else if (vPrefix == "$$_uRt") { - return 1; - } - } } - return 0; - */ +} + +void theory_str::more_value_tests(expr * valTester, std::string valTesterValue) { + ast_manager & m = get_manager(); + + expr * fVar = valueTester_fvar_map[valTester]; + int lenTesterCount = fvar_lenTester_map[fVar].size(); + + expr * effectiveLenInd = NULL; + std::string effectiveLenIndiStr = ""; + for (int i = 0; i < lenTesterCount; ++i) { + expr * len_indicator_pre = fvar_lenTester_map[fVar][i]; + bool indicatorHasEqcValue = false; + expr * len_indicator_value = get_eqc_value(len_indicator_pre, indicatorHasEqcValue); + if (indicatorHasEqcValue) { + std::string len_pIndiStr = m_strutil.get_string_constant_value(len_indicator_value); + if (len_pIndiStr != "more") { + effectiveLenInd = len_indicator_pre; + effectiveLenIndiStr = len_pIndiStr; + break; + } + } + } + expr * valueAssert = gen_free_var_options(fVar, effectiveLenInd, effectiveLenIndiStr, valTester, valTesterValue); + TRACE("t_str_detail", tout << "asserting more value tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); + if (valueAssert != NULL) { + assert_axiom(valueAssert); + } +} + +bool theory_str::free_var_attempt(expr * nn1, expr * nn2) { + ast_manager & m = get_manager(); + + if (internal_lenTest_vars.contains(nn1) && m_strutil.is_string(nn2)) { + TRACE("t_str", tout << "acting on equivalence between length tester var " << mk_ismt2_pp(nn1, m) + << " and constant " << mk_ismt2_pp(nn2, m) << std::endl;); + more_len_tests(nn1, m_strutil.get_string_constant_value(nn2)); + return true; + } else if (internal_valTest_vars.contains(nn1) && m_strutil.is_string(nn2)) { + std::string nn2_str = m_strutil.get_string_constant_value(nn2); + if (nn2_str == "more") { + TRACE("t_str", tout << "acting on equivalence between value var " << mk_ismt2_pp(nn1, m) + << " and constant " << mk_ismt2_pp(nn2, m) << std::endl;); + more_value_tests(nn1, nn2_str); + } + return true; + } else { + return false; + } } void theory_str::handle_equality(expr * lhs, expr * rhs) { @@ -2790,6 +2810,10 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { return; } + if (free_var_attempt(lhs, rhs) || free_var_attempt(rhs, lhs)) { + return; + } + // TODO simplify concat? // newEqCheck() -- check consistency wrt. existing equivalence classes diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 242b37747..3bb3940b6 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -90,6 +90,9 @@ namespace smt { std::set internal_variable_set; std::map > internal_variable_scope_levels; + obj_hashtable internal_lenTest_vars; + obj_hashtable internal_valTest_vars; + std::set input_var_in_len; std::map fvar_len_count_map; @@ -192,6 +195,8 @@ namespace smt { std::string gen_val_string(int len, int_vector & encoding); bool free_var_attempt(expr * nn1, expr * nn2); + void more_len_tests(expr * lenTester, std::string lenTesterValue); + void more_value_tests(expr * valTester, std::string valTesterValue); expr * get_alias_index_ast(std::map & aliasIndexMap, expr * node); expr * getMostLeftNodeInConcat(expr * node); From 866d97f768b0ed0217e66b30762bf6e6aa4ff5ca Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 17 May 2016 16:45:53 -0400 Subject: [PATCH 085/562] fix eval_concat copy-and-paste error in simplify_parent; concat-eq-concat-case3_sat now passing --- src/smt/theory_str.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ff0aacfa1..4c936dea5 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -963,7 +963,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { // (Concat arg0 n_eqNode) /\ arg0 has eq const - expr * concatResult = eval_concat(eq_str, arg1); + expr * concatResult = eval_concat(arg0, eq_str); if (concatResult != NULL) { bool arg0HasEqcValue = false; expr * arg0Value = get_eqc_value(arg0, arg0HasEqcValue); From c8522c5b78a51e537bb36bd5468073be9f588c6b Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 19 May 2016 16:51:43 -0400 Subject: [PATCH 086/562] cleanup before attempting to fix the null enode parent bug --- src/smt/theory_str.cpp | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 4c936dea5..7581baf8d 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -821,8 +821,6 @@ expr * theory_str::eval_concat(expr * n1, expr * n2) { * ~ concat node * to see whether some concat nodes can be simplified. */ - -// TODO NEXT complete this method! void theory_str::simplify_parent(expr * nn, expr * eq_str) { ast_manager & m = get_manager(); context & ctx = get_context(); @@ -834,14 +832,17 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { enode * n_eq_enode = ctx.get_enode(nn); enode * nn_enode = n_eq_enode; - const char * tmp = 0; - m_strutil.is_string(eq_str, & tmp); - std::string eq_strValue(tmp); + std::string eq_strValue = m_strutil.get_string_constant_value(eq_str); do { app * n_eqNode = n_eq_enode->get_owner(); + TRACE("t_str_detail", tout << "considering all parents of " << mk_ismt2_pp(n_eqNode, m) << std::endl + << "associated n_eq_enode has " << n_eq_enode->get_num_parents() << " parents" << std::endl;); for (enode_vector::iterator parent_it = n_eq_enode->begin_parents(); parent_it != n_eq_enode->end_parents(); parent_it++) { enode * e_parent = *parent_it; + // TODO deeper bug hiding here + SASSERT(e_parent != NULL); + app * a_parent = e_parent->get_owner(); TRACE("t_str_detail", tout << "considering parent " << mk_ismt2_pp(a_parent, m) << std::endl;); @@ -2814,7 +2815,32 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { return; } - // TODO simplify concat? + if (is_concat(to_app(lhs)) && is_concat(to_app(rhs))) { + bool nn1HasEqcValue = false; + bool nn2HasEqcValue = false; + expr * nn1_value = get_eqc_value(lhs, nn1HasEqcValue); + expr * nn2_value = get_eqc_value(rhs, nn2HasEqcValue); + if (nn1HasEqcValue && !nn2HasEqcValue) { + simplify_parent(rhs, nn1_value); + } + if (!nn1HasEqcValue && nn2HasEqcValue) { + simplify_parent(lhs, nn2_value); + } + + expr * nn1_arg0 = to_app(lhs)->get_arg(0); + expr * nn1_arg1 = to_app(lhs)->get_arg(1); + expr * nn2_arg0 = to_app(rhs)->get_arg(0); + expr * nn2_arg1 = to_app(rhs)->get_arg(1); + if (nn1_arg0 == nn2_arg0 && in_same_eqc(nn1_arg1, nn2_arg1)) { + TRACE("t_str_detail", tout << "skip: lhs arg0 == rhs arg0" << std::endl;); + return; + } + + if (nn1_arg1 == nn2_arg1 && in_same_eqc(nn1_arg0, nn2_arg0)) { + TRACE("t_str_detail", tout << "skip: lhs arg1 == rhs arg1" << std::endl;); + return; + } + } // newEqCheck() -- check consistency wrt. existing equivalence classes if (!new_eq_check(lhs, rhs)) { From 2f494a96119732443e4d41321a6f97508a16a4ce Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 19 May 2016 16:57:01 -0400 Subject: [PATCH 087/562] fix null parent bug by making a copy of n_eq_enode->m_parents in simplify_parent --- src/smt/theory_str.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 7581baf8d..bc32e14eb 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -838,9 +838,18 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { app * n_eqNode = n_eq_enode->get_owner(); TRACE("t_str_detail", tout << "considering all parents of " << mk_ismt2_pp(n_eqNode, m) << std::endl << "associated n_eq_enode has " << n_eq_enode->get_num_parents() << " parents" << std::endl;); - for (enode_vector::iterator parent_it = n_eq_enode->begin_parents(); parent_it != n_eq_enode->end_parents(); parent_it++) { + + // the goal of this next bit is to avoid dereferencing a bogus e_parent in the following loop. + // what I image is causing this bug is that, for example, we examine some parent, we add an axiom that involves it, + // and the parent_it iterator becomes invalidated, because we indirectly modified the container that we're iterating over. + + enode_vector current_parents; + for (enode_vector::const_iterator parent_it = n_eq_enode->begin_parents(); parent_it != n_eq_enode->end_parents(); parent_it++) { + current_parents.insert(*parent_it); + } + + for (enode_vector::iterator parent_it = current_parents.begin(); parent_it != current_parents.end(); ++parent_it) { enode * e_parent = *parent_it; - // TODO deeper bug hiding here SASSERT(e_parent != NULL); app * a_parent = e_parent->get_owner(); From 2522e35c5e90d2a47f5bf4b2a4ad16f300678c6d Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 20 May 2016 10:22:19 -0400 Subject: [PATCH 088/562] start work on string-integer integration --- src/smt/theory_str.cpp | 33 +++++++++++++++++++++++++++++++++ src/smt/theory_str.h | 2 ++ 2 files changed, 35 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index bc32e14eb..05425c61b 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2363,6 +2363,39 @@ expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { return n; } +/* + * Look through the equivalence class of n to find an integer constant. + * Return that constant if it is found. Otherwise, return -1. + * Note that a return value of -1 should not normally be possible, as + * string length cannot be negative. + */ + +rational theory_str::get_len_value(expr * n) { + ast_manager & m = get_manager(); + context & ctx = get_context(); + ctx.internalize(n, false); + + TRACE("t_str_detail", tout << "checking eqc of " << mk_ismt2_pp(n, m) << " for an integer constant" << std::endl;); + + enode * nNode = ctx.get_enode(n); + enode * eqcNode = nNode; + do { + app * ast = eqcNode->get_owner(); + rational val; + bool is_int; + if (m_autil.is_numeral(n, val, is_int)) { + if (is_int) { + TRACE("t_str_detail", tout << "eqc contains integer constant " << val << std::endl;); + SASSERT(!val.is_neg()); + return val; + } + } + } while (eqcNode != nNode); + // not found + TRACE("t_str_detail", tout << "eqc contains no integer constants" << std::endl;); + return rational(-1); +} + /* * Decide whether n1 and n2 are already in the same equivalence class. * This only checks whether the core considers them to be equal; diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 3bb3940b6..cf7ef0060 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -146,6 +146,8 @@ namespace smt { expr * get_eqc_value(expr * n, bool & hasEqcValue); bool in_same_eqc(expr * n1, expr * n2); + rational get_len_value(expr * n); + bool can_two_nodes_eq(expr * n1, expr * n2); bool can_concat_eq_str(expr * concat, std::string str); bool can_concat_eq_concat(expr * concat1, expr * concat2); From ecb069b7018f52b7621a8ac7445d4ce7968db770 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 20 May 2016 16:34:11 -0400 Subject: [PATCH 089/562] non-fixes to string length code, plus the get_length() code from new Z3 --- src/smt/theory_str.cpp | 135 ++++++++++++++++++++++++++++++++++------- src/smt/theory_str.h | 2 +- 2 files changed, 115 insertions(+), 22 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 05425c61b..d3e842fed 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1440,37 +1440,39 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { expr * m = to_app(concatAst2)->get_arg(0); expr * n = to_app(concatAst2)->get_arg(1); - /* TODO query the integer theory: - int x_len = getLenValue(t, x); - int y_len = getLenValue(t, y); - int m_len = getLenValue(t, m); - int n_len = getLenValue(t, n); - */ - int x_len = -1; - int y_len = -1; - int m_len = -1; - int n_len = -1; + rational x_len = get_len_value(x); + rational y_len = get_len_value(y); + rational m_len = get_len_value(m); + rational n_len = get_len_value(n); int splitType = -1; - if (x_len != -1 && m_len != -1) { - if (x_len < m_len) + if (x_len != rational(-1) && m_len != rational(-1)) { + if (x_len < m_len) { splitType = 0; - else if (x_len == m_len) + } else if (x_len == m_len) { splitType = 1; - else + } else { splitType = 2; + } } - if (splitType == -1 && y_len != -1 && n_len != -1) { - if (y_len > n_len) + if (splitType == -1 && y_len != rational(-1) && n_len != rational(-1)) { + if (y_len > n_len) { splitType = 0; - else if (y_len == n_len) + } else if (y_len == n_len) { splitType = 1; - else + } else { splitType = 2; + } } - TRACE("t_str_detail", tout << "split type " << splitType << std::endl;); + TRACE("t_str_detail", tout + << "len(x) = " << x_len << std::endl + << "len(y) = " << y_len << std::endl + << "len(m) = " << m_len << std::endl + << "len(n) = " << n_len << std::endl + << "split type " << splitType << std::endl; + ); expr * t1 = NULL; expr * t2 = NULL; @@ -2363,6 +2365,93 @@ expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { return n; } +// from Z3: theory_seq.cpp + +/* +static theory_mi_arith* get_th_arith(context& ctx, theory_id afid, expr* e) { + theory* th = ctx.get_theory(afid); + if (th && ctx.e_internalized(e)) { + return dynamic_cast(th); + } + else { + return 0; + } +} + +bool theory_seq::get_value(expr* e, rational& val) const { + context& ctx = get_context(); + theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), e); + expr_ref _val(m); + if (!tha || !tha->get_value(ctx.get_enode(e), _val)) return false; + return m_autil.is_numeral(_val, val) && val.is_int(); +} + +bool theory_seq::lower_bound(expr* _e, rational& lo) const { + context& ctx = get_context(); + expr_ref e(m_util.str.mk_length(_e), m); + theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), e); + expr_ref _lo(m); + if (!tha || !tha->get_lower(ctx.get_enode(e), _lo)) return false; + return m_autil.is_numeral(_lo, lo) && lo.is_int(); +} + +bool theory_seq::upper_bound(expr* _e, rational& hi) const { + context& ctx = get_context(); + expr_ref e(m_util.str.mk_length(_e), m); + theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), e); + expr_ref _hi(m); + if (!tha || !tha->get_upper(ctx.get_enode(e), _hi)) return false; + return m_autil.is_numeral(_hi, hi) && hi.is_int(); +} + +bool theory_seq::get_length(expr* e, rational& val) const { + context& ctx = get_context(); + theory* th = ctx.get_theory(m_autil.get_family_id()); + if (!th) return false; + theory_mi_arith* tha = dynamic_cast(th); + if (!tha) return false; + rational val1; + expr_ref len(m), len_val(m); + expr* e1, *e2; + ptr_vector todo; + todo.push_back(e); + val.reset(); + zstring s; + while (!todo.empty()) { + expr* c = todo.back(); + todo.pop_back(); + if (m_util.str.is_concat(c, e1, e2)) { + todo.push_back(e1); + todo.push_back(e2); + } + else if (m_util.str.is_unit(c)) { + val += rational(1); + } + else if (m_util.str.is_empty(c)) { + continue; + } + else if (m_util.str.is_string(c, s)) { + val += rational(s.length()); + } + else if (!has_length(c)) { + return false; + } + else { + len = m_util.str.mk_length(c); + if (ctx.e_internalized(len) && + tha->get_value(ctx.get_enode(len), len_val) && + m_autil.is_numeral(len_val, val1)) { + val += val1; + } + else { + return false; + } + } + } + return val.is_int(); +} +*/ + /* * Look through the equivalence class of n to find an integer constant. * Return that constant if it is found. Otherwise, return -1. @@ -2370,9 +2459,11 @@ expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { * string length cannot be negative. */ -rational theory_str::get_len_value(expr * n) { +rational theory_str::get_len_value(expr * x) { ast_manager & m = get_manager(); context & ctx = get_context(); + ctx.internalize(x, false); + expr * n = mk_strlen(x); ctx.internalize(n, false); TRACE("t_str_detail", tout << "checking eqc of " << mk_ismt2_pp(n, m) << " for an integer constant" << std::endl;); @@ -2383,13 +2474,15 @@ rational theory_str::get_len_value(expr * n) { app * ast = eqcNode->get_owner(); rational val; bool is_int; - if (m_autil.is_numeral(n, val, is_int)) { + TRACE("t_str_detail", tout << "eqc member: " << mk_ismt2_pp(ast, m) << std::endl;); + if (m_autil.is_numeral(ast, val, is_int)) { if (is_int) { TRACE("t_str_detail", tout << "eqc contains integer constant " << val << std::endl;); SASSERT(!val.is_neg()); return val; } } + eqcNode = eqcNode->get_next(); } while (eqcNode != nNode); // not found TRACE("t_str_detail", tout << "eqc contains no integer constants" << std::endl;); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index cf7ef0060..946340366 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -146,7 +146,7 @@ namespace smt { expr * get_eqc_value(expr * n, bool & hasEqcValue); bool in_same_eqc(expr * n1, expr * n2); - rational get_len_value(expr * n); + rational get_len_value(expr * x); bool can_two_nodes_eq(expr * n1, expr * n2); bool can_concat_eq_str(expr * concat, std::string str); From f8f7014a1855d40931a3b2b9202f8c23ca617bc3 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 1 Jun 2016 16:34:48 -0400 Subject: [PATCH 090/562] use LRA instead of LIA in strings setup, so that the theory_seq integer value code works --- src/smt/smt_setup.cpp | 2 +- src/smt/theory_str.cpp | 68 +++++++++++++++++++++++++----------------- src/smt/theory_str.h | 3 +- 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/src/smt/smt_setup.cpp b/src/smt/smt_setup.cpp index 5e4af91fd..acb03a954 100644 --- a/src/smt/smt_setup.cpp +++ b/src/smt/smt_setup.cpp @@ -700,7 +700,7 @@ namespace smt { } void setup::setup_QF_S() { - setup_QF_LIA(); + setup_QF_LRA(); m_context.register_plugin(alloc(smt::theory_str, m_manager)); } diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index d3e842fed..e2e2f55d1 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -21,6 +21,7 @@ Revision History: #include"ast_pp.h" #include"ast_ll_pp.h" #include +#include"theory_arith.h" namespace smt { @@ -1440,13 +1441,14 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { expr * m = to_app(concatAst2)->get_arg(0); expr * n = to_app(concatAst2)->get_arg(1); - rational x_len = get_len_value(x); - rational y_len = get_len_value(y); - rational m_len = get_len_value(m); - rational n_len = get_len_value(n); + rational x_len, y_len, m_len, n_len; + bool x_len_exists = get_len_value(x, x_len); + bool y_len_exists = get_len_value(y, y_len); + bool m_len_exists = get_len_value(m, m_len); + bool n_len_exists = get_len_value(n, n_len); int splitType = -1; - if (x_len != rational(-1) && m_len != rational(-1)) { + if (x_len_exists && m_len_exists) { if (x_len < m_len) { splitType = 0; } else if (x_len == m_len) { @@ -1456,7 +1458,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } } - if (splitType == -1 && y_len != rational(-1) && n_len != rational(-1)) { + if (splitType == -1 && y_len_exists && n_len_exists) { if (y_len > n_len) { splitType = 0; } else if (y_len == n_len) { @@ -2367,7 +2369,6 @@ expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { // from Z3: theory_seq.cpp -/* static theory_mi_arith* get_th_arith(context& ctx, theory_id afid, expr* e) { theory* th = ctx.get_theory(afid); if (th && ctx.e_internalized(e)) { @@ -2378,15 +2379,18 @@ static theory_mi_arith* get_th_arith(context& ctx, theory_id afid, expr* e) { } } -bool theory_seq::get_value(expr* e, rational& val) const { +bool theory_str::get_value(expr* e, rational& val) const { context& ctx = get_context(); + ast_manager & m = get_manager(); theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), e); expr_ref _val(m); if (!tha || !tha->get_value(ctx.get_enode(e), _val)) return false; return m_autil.is_numeral(_val, val) && val.is_int(); } -bool theory_seq::lower_bound(expr* _e, rational& lo) const { +// TODO bring these in as well +/* +bool theory_str::lower_bound(expr* _e, rational& lo) const { context& ctx = get_context(); expr_ref e(m_util.str.mk_length(_e), m); theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), e); @@ -2395,7 +2399,7 @@ bool theory_seq::lower_bound(expr* _e, rational& lo) const { return m_autil.is_numeral(_lo, lo) && lo.is_int(); } -bool theory_seq::upper_bound(expr* _e, rational& hi) const { +bool theory_str::upper_bound(expr* _e, rational& hi) const { context& ctx = get_context(); expr_ref e(m_util.str.mk_length(_e), m); theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), e); @@ -2403,54 +2407,60 @@ bool theory_seq::upper_bound(expr* _e, rational& hi) const { if (!tha || !tha->get_upper(ctx.get_enode(e), _hi)) return false; return m_autil.is_numeral(_hi, hi) && hi.is_int(); } +*/ -bool theory_seq::get_length(expr* e, rational& val) const { +bool theory_str::get_len_value(expr* e, rational& val) { context& ctx = get_context(); + ast_manager & m = get_manager(); theory* th = ctx.get_theory(m_autil.get_family_id()); - if (!th) return false; + if (!th) { + TRACE("t_str_int", tout << "oops, can't get m_autil's theory" << std::endl;); + return false; + } theory_mi_arith* tha = dynamic_cast(th); - if (!tha) return false; + if (!tha) { + TRACE("t_str_int", tout << "oops, can't cast to theory_mi_arith" << std::endl;); + return false; + } + + TRACE("t_str_int", tout << "checking len value of " << mk_ismt2_pp(e, m) << std::endl;); + rational val1; expr_ref len(m), len_val(m); expr* e1, *e2; ptr_vector todo; todo.push_back(e); val.reset(); - zstring s; while (!todo.empty()) { expr* c = todo.back(); todo.pop_back(); - if (m_util.str.is_concat(c, e1, e2)) { + if (is_concat(to_app(c))) { + e1 = to_app(c)->get_arg(0); + e2 = to_app(c)->get_arg(1); todo.push_back(e1); todo.push_back(e2); } - else if (m_util.str.is_unit(c)) { - val += rational(1); - } - else if (m_util.str.is_empty(c)) { - continue; - } - else if (m_util.str.is_string(c, s)) { - val += rational(s.length()); - } - else if (!has_length(c)) { - return false; + else if (is_string(to_app(c))) { + int sl = m_strutil.get_string_constant_value(c).length(); + val += rational(sl); } else { - len = m_util.str.mk_length(c); + len = mk_strlen(c); if (ctx.e_internalized(len) && tha->get_value(ctx.get_enode(len), len_val) && m_autil.is_numeral(len_val, val1)) { val += val1; + TRACE("t_str_int", tout << "subexpression " << mk_ismt2_pp(len, m) << " has length " << val1 << std::endl;); } else { + TRACE("t_str_int", tout << "subexpression " << mk_ismt2_pp(len, m) << " has no length assignment; bailing out" << std::endl;); return false; } } } + TRACE("t_str_int", tout << "length of " << mk_ismt2_pp(e, m) << " is " << val << std::endl;); return val.is_int(); } -*/ /* * Look through the equivalence class of n to find an integer constant. @@ -2459,6 +2469,7 @@ bool theory_seq::get_length(expr* e, rational& val) const { * string length cannot be negative. */ +/* rational theory_str::get_len_value(expr * x) { ast_manager & m = get_manager(); context & ctx = get_context(); @@ -2488,6 +2499,7 @@ rational theory_str::get_len_value(expr * x) { TRACE("t_str_detail", tout << "eqc contains no integer constants" << std::endl;); return rational(-1); } +*/ /* * Decide whether n1 and n2 are already in the same equivalence class. diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 946340366..da950713f 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -146,7 +146,8 @@ namespace smt { expr * get_eqc_value(expr * n, bool & hasEqcValue); bool in_same_eqc(expr * n1, expr * n2); - rational get_len_value(expr * x); + bool get_value(expr* e, rational& val) const; + bool get_len_value(expr* e, rational& val); bool can_two_nodes_eq(expr * n1, expr * n2); bool can_concat_eq_str(expr * concat, std::string str); From bc79a73779f0b28e10bb98ca22e266362c0c2687 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 1 Jun 2016 17:23:47 -0400 Subject: [PATCH 091/562] lower/upper bound WIP --- src/smt/theory_str.cpp | 21 +++++++++++---------- src/smt/theory_str.h | 2 ++ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index e2e2f55d1..f7d31a80b 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -165,7 +165,6 @@ bool theory_str::internalize_atom(app * atom, bool gate_ctx) { bool theory_str::internalize_term(app * term) { context & ctx = get_context(); ast_manager & m = get_manager(); - TRACE("t_str", tout << "internalizing term: " << mk_ismt2_pp(term, get_manager()) << std::endl;); SASSERT(term->get_family_id() == get_family_id()); /* // what I had before @@ -194,6 +193,7 @@ bool theory_str::internalize_term(app * term) { mk_var(e); return true; } + TRACE("t_str", tout << "internalizing term: " << mk_ismt2_pp(term, get_manager()) << std::endl;); unsigned num_args = term->get_num_args(); expr* arg; for (unsigned i = 0; i < num_args; i++) { @@ -1447,6 +1447,8 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { bool m_len_exists = get_len_value(m, m_len); bool n_len_exists = get_len_value(n, n_len); + // debugging + int splitType = -1; if (x_len_exists && m_len_exists) { if (x_len < m_len) { @@ -2388,26 +2390,25 @@ bool theory_str::get_value(expr* e, rational& val) const { return m_autil.is_numeral(_val, val) && val.is_int(); } -// TODO bring these in as well -/* -bool theory_str::lower_bound(expr* _e, rational& lo) const { +bool theory_str::lower_bound(expr* _e, rational& lo) { context& ctx = get_context(); - expr_ref e(m_util.str.mk_length(_e), m); + ast_manager & m = get_manager(); + expr_ref e(mk_strlen(_e), m); theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), e); expr_ref _lo(m); if (!tha || !tha->get_lower(ctx.get_enode(e), _lo)) return false; return m_autil.is_numeral(_lo, lo) && lo.is_int(); } -bool theory_str::upper_bound(expr* _e, rational& hi) const { +bool theory_str::upper_bound(expr* _e, rational& hi) { context& ctx = get_context(); - expr_ref e(m_util.str.mk_length(_e), m); + ast_manager & m = get_manager(); + expr_ref e(mk_strlen(_e), m); theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), e); expr_ref _hi(m); if (!tha || !tha->get_upper(ctx.get_enode(e), _hi)) return false; return m_autil.is_numeral(_hi, hi) && hi.is_int(); } -*/ bool theory_str::get_len_value(expr* e, rational& val) { context& ctx = get_context(); @@ -2450,10 +2451,10 @@ bool theory_str::get_len_value(expr* e, rational& val) { tha->get_value(ctx.get_enode(len), len_val) && m_autil.is_numeral(len_val, val1)) { val += val1; - TRACE("t_str_int", tout << "subexpression " << mk_ismt2_pp(len, m) << " has length " << val1 << std::endl;); + TRACE("t_str_int", tout << "integer theory: subexpression " << mk_ismt2_pp(len, m) << " has length " << val1 << std::endl;); } else { - TRACE("t_str_int", tout << "subexpression " << mk_ismt2_pp(len, m) << " has no length assignment; bailing out" << std::endl;); + TRACE("t_str_int", tout << "integer theory: subexpression " << mk_ismt2_pp(len, m) << " has no length assignment; bailing out" << std::endl;); return false; } } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index da950713f..45c5f3e06 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -148,6 +148,8 @@ namespace smt { bool get_value(expr* e, rational& val) const; bool get_len_value(expr* e, rational& val); + bool lower_bound(expr* _e, rational& lo); + bool upper_bound(expr* _e, rational& hi); bool can_two_nodes_eq(expr * n1, expr * n2); bool can_concat_eq_str(expr * concat, std::string str); From b5fe473c3ac99b012d6cc945bd5ca0109e9f7f3b Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 1 Jun 2016 17:50:45 -0400 Subject: [PATCH 092/562] fix compilation errors after merge --- src/parsers/smt2/smt2parser.cpp | 3 --- src/smt/theory_str.cpp | 4 ++++ src/smt/theory_str.h | 1 + 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/parsers/smt2/smt2parser.cpp b/src/parsers/smt2/smt2parser.cpp index c203b2faa..c8e9a78b6 100644 --- a/src/parsers/smt2/smt2parser.cpp +++ b/src/parsers/smt2/smt2parser.cpp @@ -1767,9 +1767,6 @@ namespace smt2 { case scanner::BV_TOKEN: parse_bv_numeral(); break; - case scanner::STRING_TOKEN: - parse_string(); - break; case scanner::LEFT_PAREN: push_expr_frame(m_num_expr_frames == 0 ? 0 : static_cast(m_stack.top())); break; diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index f7d31a80b..b65b799b1 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4962,4 +4962,8 @@ model_value_proc * theory_str::mk_value(enode * n, model_generator & mg) { void theory_str::finalize_model(model_generator & mg) {} +void theory_str::display(std::ostream & out) const { + out << "TODO: theory_str display" << std::endl; +} + }; /* namespace smt */ diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 45c5f3e06..ecd7e443f 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -220,6 +220,7 @@ namespace smt { virtual ~theory_str(); virtual char const * get_name() const { return "strings"; } + virtual void display(std::ostream & out) const; protected: virtual bool internalize_atom(app * atom, bool gate_ctx); virtual bool internalize_term(app * term); From 33205cea712041ce5ba6cc3c6bc465e4c3e84d54 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 1 Jun 2016 17:57:00 -0400 Subject: [PATCH 093/562] completely bypass theory_seq; sorry! I'll put it back when I'm done --- src/ast/seq_decl_plugin.cpp | 6 +++--- src/cmd_context/check_logic.cpp | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/ast/seq_decl_plugin.cpp b/src/ast/seq_decl_plugin.cpp index 21af0773a..787648e19 100644 --- a/src/ast/seq_decl_plugin.cpp +++ b/src/ast/seq_decl_plugin.cpp @@ -256,7 +256,7 @@ std::ostream& zstring::operator<<(std::ostream& out) const { seq_decl_plugin::seq_decl_plugin(): m_init(false), - m_stringc_sym("String"), + m_stringc_sym("StringSequence"), m_charc_sym("Char"), m_string(0), m_char(0), @@ -490,7 +490,7 @@ void seq_decl_plugin::set_manager(ast_manager* m, family_id id) { m_char = bv.mk_sort(8); m->inc_ref(m_char); parameter param(m_char); - m_string = m->mk_sort(symbol("String"), sort_info(m_family_id, SEQ_SORT, 1, ¶m)); + m_string = m->mk_sort(symbol("StringSequence"), sort_info(m_family_id, SEQ_SORT, 1, ¶m)); m->inc_ref(m_string); parameter paramS(m_string); m_re = m->mk_sort(m_family_id, RE_SORT, 1, ¶mS); @@ -745,7 +745,7 @@ void seq_decl_plugin::get_sort_names(svector & sort_names, symbol init(); sort_names.push_back(builtin_name("Seq", SEQ_SORT)); sort_names.push_back(builtin_name("RegEx", RE_SORT)); - sort_names.push_back(builtin_name("String", _STRING_SORT)); + sort_names.push_back(builtin_name("StringSequence", _STRING_SORT)); } app* seq_decl_plugin::mk_string(symbol const& s) { diff --git a/src/cmd_context/check_logic.cpp b/src/cmd_context/check_logic.cpp index 733689ac9..a547ab616 100644 --- a/src/cmd_context/check_logic.cpp +++ b/src/cmd_context/check_logic.cpp @@ -21,6 +21,7 @@ Revision History: #include"array_decl_plugin.h" #include"bv_decl_plugin.h" #include"seq_decl_plugin.h" +#include"str_decl_plugin.h" #include"ast_pp.h" #include"for_each_expr.h" @@ -31,6 +32,7 @@ struct check_logic::imp { bv_util m_bv_util; array_util m_ar_util; seq_util m_seq_util; + str_util m_str_util; bool m_uf; // true if the logic supports uninterpreted functions bool m_arrays; // true if the logic supports arbitrary arrays bool m_bv_arrays; // true if the logic supports only bv arrays @@ -42,7 +44,7 @@ struct check_logic::imp { bool m_quantifiers; // true if the logic supports quantifiers bool m_unknown_logic; - imp(ast_manager & _m):m(_m), m_a_util(m), m_bv_util(m), m_ar_util(m), m_seq_util(m) { + imp(ast_manager & _m):m(_m), m_a_util(m), m_bv_util(m), m_ar_util(m), m_seq_util(m), m_str_util(m) { reset(); } @@ -432,6 +434,9 @@ struct check_logic::imp { else if (fid == m_seq_util.get_family_id()) { // nothing to check } + else if (fid == m_str_util.get_family_id()) { + // nothing to check + } else { fail("logic does not support theory"); } From e0df5bc2edf3b68f2d8c6403332c93d0664afe48 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 4 Jun 2016 16:29:10 -0400 Subject: [PATCH 094/562] fixups for string-integer --- src/smt/theory_str.cpp | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index b65b799b1..eb64aae5d 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1447,10 +1447,9 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { bool m_len_exists = get_len_value(m, m_len); bool n_len_exists = get_len_value(n, n_len); - // debugging - int splitType = -1; if (x_len_exists && m_len_exists) { + TRACE("t_str_int", tout << "length values found: x/m" << std::endl;); if (x_len < m_len) { splitType = 0; } else if (x_len == m_len) { @@ -1461,6 +1460,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } if (splitType == -1 && y_len_exists && n_len_exists) { + TRACE("t_str_int", tout << "length values found: y/n" << std::endl;); if (y_len > n_len) { splitType = 0; } else if (y_len == n_len) { @@ -1471,10 +1471,10 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } TRACE("t_str_detail", tout - << "len(x) = " << x_len << std::endl - << "len(y) = " << y_len << std::endl - << "len(m) = " << m_len << std::endl - << "len(n) = " << n_len << std::endl + << "len(x) = " << (x_len_exists ? x_len.to_string() : "?") << std::endl + << "len(y) = " << (y_len_exists ? y_len.to_string() : "?") << std::endl + << "len(m) = " << (m_len_exists ? m_len.to_string() : "?") << std::endl + << "len(n) = " << (n_len_exists ? n_len.to_string() : "?") << std::endl << "split type " << splitType << std::endl; ); @@ -2385,11 +2385,24 @@ bool theory_str::get_value(expr* e, rational& val) const { context& ctx = get_context(); ast_manager & m = get_manager(); theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), e); + if (!tha) { + return false; + } expr_ref _val(m); - if (!tha || !tha->get_value(ctx.get_enode(e), _val)) return false; - return m_autil.is_numeral(_val, val) && val.is_int(); + enode * en_e = ctx.get_enode(e); + enode * it = en_e; + do { + if (tha->get_value(it, _val)) { + // found an arithmetic term + return m_autil.is_numeral(_val, val) && val.is_int(); + } + it = it->get_next(); + } while (it != en_e); + return false; } +// TODO these methods currently crash the solver, find out why + bool theory_str::lower_bound(expr* _e, rational& lo) { context& ctx = get_context(); ast_manager & m = get_manager(); @@ -2447,9 +2460,7 @@ bool theory_str::get_len_value(expr* e, rational& val) { } else { len = mk_strlen(c); - if (ctx.e_internalized(len) && - tha->get_value(ctx.get_enode(len), len_val) && - m_autil.is_numeral(len_val, val1)) { + if (ctx.e_internalized(len) && get_value(len, val1)) { val += val1; TRACE("t_str_int", tout << "integer theory: subexpression " << mk_ismt2_pp(len, m) << " has length " << val1 << std::endl;); } From 62aeff90c53ae03962b907a46ae2de07ca4ae511 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 7 Jun 2016 17:38:57 -0400 Subject: [PATCH 095/562] fix string theory setup so that string-integer integration actually works --- src/smt/smt_setup.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/smt_setup.cpp b/src/smt/smt_setup.cpp index 5a7547609..117b606fd 100644 --- a/src/smt/smt_setup.cpp +++ b/src/smt/smt_setup.cpp @@ -706,7 +706,7 @@ namespace smt { } void setup::setup_QF_S() { - setup_QF_LRA(); + m_context.register_plugin(alloc(smt::theory_mi_arith, m_manager, m_params)); m_context.register_plugin(alloc(smt::theory_str, m_manager)); } From 513b4922eee8ac4b576d8588e31606c99645524e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 7 Jun 2016 17:40:59 -0400 Subject: [PATCH 096/562] tracing code for string-integer integration --- src/smt/theory_arith.h | 14 ++++++++++++- src/smt/theory_arith_core.h | 41 +++++++++++++++++++++++++++++++++++-- src/smt/theory_str.cpp | 31 +++++++++++++++++++++++++--- 3 files changed, 80 insertions(+), 6 deletions(-) diff --git a/src/smt/theory_arith.h b/src/smt/theory_arith.h index 39f991c72..7e594abe5 100644 --- a/src/smt/theory_arith.h +++ b/src/smt/theory_arith.h @@ -576,7 +576,19 @@ namespace smt { return is_free(get_context().get_enode(n)->get_th_var(get_id())); } bool is_fixed(theory_var v) const; - void set_bound_core(theory_var v, bound * new_bound, bool upper) { m_bounds[static_cast(upper)][v] = new_bound; } + void set_bound_core(theory_var v, bound * new_bound, bool upper) { + TRACE("t_str_int", + tout << "setting " << (upper ? "upper" : "lower") << " bound "; + if (new_bound) { + tout << new_bound->get_value(); + } else { + tout << "(NULL)"; + } + tout << " for theory var v#" << v; + tout << std::endl; + ); + m_bounds[static_cast(upper)][v] = new_bound; + } void restore_bound(theory_var v, bound * new_bound, bool upper) { set_bound_core(v, new_bound, upper); } void restore_nl_propagated_flag(unsigned old_trail_size); void set_bound(bound * new_bound, bool upper); diff --git a/src/smt/theory_arith_core.h b/src/smt/theory_arith_core.h index 95c7fdfad..1ce56ffe8 100644 --- a/src/smt/theory_arith_core.h +++ b/src/smt/theory_arith_core.h @@ -3223,13 +3223,50 @@ namespace smt { bool theory_arith::get_value(enode * n, expr_ref & r) { theory_var v = n->get_th_var(get_id()); inf_numeral val; - return v != null_theory_var && (val = get_value(v), (!is_int(v) || val.is_int())) && to_expr(val, is_int(v), r); + // rewrites for tracing purposes + if (v == null_theory_var) { + TRACE("t_str_int", tout << "WARNING: enode " << mk_pp(n->get_owner(), get_manager()) + << " attached to null theory var" << std::endl; + ); + return false; + } else { + val = get_value(v); + TRACE("t_str_int", tout << "enode " << mk_pp(n->get_owner(), get_manager()) + << " attached to theory var v#" << v + << ", has val = " << val + << std::endl; + ); + if (!is_int(v) || val.is_int()) { + return to_expr(val, is_int(v), r); + } else { + return false; + } + } + // return v != null_theory_var && (val = get_value(v), (!is_int(v) || val.is_int())) && to_expr(val, is_int(v), r); } template bool theory_arith::get_lower(enode * n, expr_ref & r) { theory_var v = n->get_th_var(get_id()); - bound* b = (v == null_theory_var) ? 0 : lower(v); + bound * b; + if (v == null_theory_var) { + TRACE("t_str_int", tout << "WARNING: enode " << mk_pp(n->get_owner(), get_manager()) + << " attached to null theory var" << std::endl; + ); + b = 0; + } else { + b = lower(v); + TRACE("t_str_int", + tout << "enode " << mk_pp(n->get_owner(), get_manager()) + << " attached to theory var v#" << v + << std::endl; + if (b) { + tout << "lower bound = " << b->get_value() << std::endl; + } else { + tout << "WARNING: b = NULL" << std::endl; + } + ); + } return b && to_expr(b->get_value(), is_int(v), r); } diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index eb64aae5d..3b59961a3 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1470,6 +1470,14 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } } + { + rational x_lb, x_ub; + bool x_lb_p = lower_bound(x, x_lb); + bool x_ub_p = upper_bound(x, x_ub); + TRACE("t_str_detail", tout << "X [" << x_lb << ":" << x_ub << "]" << std::endl + << "lb? " << (x_lb_p?"yes":"no") << " ub? " << (x_ub_p?"yes":"no") << std::endl;); + } + TRACE("t_str_detail", tout << "len(x) = " << (x_len_exists ? x_len.to_string() : "?") << std::endl << "len(y) = " << (y_len_exists ? y_len.to_string() : "?") << std::endl @@ -2388,21 +2396,25 @@ bool theory_str::get_value(expr* e, rational& val) const { if (!tha) { return false; } + TRACE("t_str_int", tout << "checking eqc of " << mk_pp(e, m) << " for arithmetic value" << std::endl;); expr_ref _val(m); enode * en_e = ctx.get_enode(e); enode * it = en_e; do { if (tha->get_value(it, _val)) { // found an arithmetic term + TRACE("t_str_int", tout << "get_value[" << mk_pp(it->get_owner(), m) << "] = " << mk_pp(_val, m) + << std::endl;); return m_autil.is_numeral(_val, val) && val.is_int(); + } else { + TRACE("t_str_int", tout << "get_value[" << mk_pp(it->get_owner(), m) << "] not found" << std::endl;); } it = it->get_next(); } while (it != en_e); + TRACE("t_str_int", tout << "no arithmetic values found in eqc" << std::endl;); return false; } -// TODO these methods currently crash the solver, find out why - bool theory_str::lower_bound(expr* _e, rational& lo) { context& ctx = get_context(); ast_manager & m = get_manager(); @@ -2460,6 +2472,19 @@ bool theory_str::get_len_value(expr* e, rational& val) { } else { len = mk_strlen(c); + + // debugging + TRACE("t_str_int", { + tout << mk_pp(len, m) << ":" << std::endl + << (ctx.is_relevant(len.get()) ? "relevant" : "not relevant") << std::endl + << (ctx.e_internalized(len) ? "internalized" : "not internalized") << std::endl + ; + if (ctx.e_internalized(len)) { + enode * e_len = ctx.get_enode(len); + tout << "has " << e_len->get_num_th_vars() << " theory vars" << std::endl; + } + }); + if (ctx.e_internalized(len) && get_value(len, val1)) { val += val1; TRACE("t_str_int", tout << "integer theory: subexpression " << mk_ismt2_pp(len, m) << " has length " << val1 << std::endl;); @@ -3225,7 +3250,7 @@ void theory_str::init_search_eh() { unsigned nFormulas = ctx.get_num_asserted_formulas(); for (unsigned i = 0; i < nFormulas; ++i) { expr * ex = ctx.get_asserted_formula(i); - tout << mk_ismt2_pp(ex, m) << std::endl; + tout << mk_ismt2_pp(ex, m) << (ctx.is_relevant(ex) ? " (rel)" : " (NOT REL)") << std::endl; } ); /* From 04fe8f66df6fc10722dade93db0d5f103982dd0e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 8 Jun 2016 16:22:31 -0400 Subject: [PATCH 097/562] concat-eq-concat type 1 split 0 --- src/smt/theory_str.cpp | 54 +++++++++++++++++++++++++++++++++++------- src/smt/theory_str.h | 1 + 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 3b59961a3..cc164ec3b 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -348,6 +348,10 @@ app * theory_str::mk_int(int n) { return m_autil.mk_numeral(rational(n), true); } +app * theory_str::mk_int(rational & q) { + return m_autil.mk_numeral(q, true); +} + // TODO refactor all of these so that they don't use variable counters, but use ast_manager::mk_fresh_const instead @@ -1470,14 +1474,6 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } } - { - rational x_lb, x_ub; - bool x_lb_p = lower_bound(x, x_lb); - bool x_ub_p = upper_bound(x, x_ub); - TRACE("t_str_detail", tout << "X [" << x_lb << ":" << x_ub << "]" << std::endl - << "lb? " << (x_lb_p?"yes":"no") << " ub? " << (x_ub_p?"yes":"no") << std::endl;); - } - TRACE("t_str_detail", tout << "len(x) = " << (x_len_exists ? x_len.to_string() : "?") << std::endl << "len(y) = " << (y_len_exists ? y_len.to_string() : "?") << std::endl @@ -1518,7 +1514,47 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { // For split types 0 through 2, we can get away with providing // fewer split options since more length information is available. if (splitType == 0) { - NOT_IMPLEMENTED_YET(); // TODO + //-------------------------------------- + // Type 0: M cuts Y. + // len(x) < len(m) || len(y) > len(n) + //-------------------------------------- + if (!has_self_cut(m, y)) { + expr ** ax_l_items = alloc_svect(expr*, 3); + expr ** ax_r_items = alloc_svect(expr*, 3); + + ax_l_items[0] = ctx.mk_eq_atom(concatAst1, concatAst2); + + expr_ref x_t1(mk_concat(x, t1), mgr); + expr_ref t1_n(mk_concat(t1, n), mgr); + + ax_r_items[0] = ctx.mk_eq_atom(m, x_t1); + ax_r_items[1] = ctx.mk_eq_atom(y, t1_n); + + if (m_len_exists && x_len_exists) { + ax_l_items[1] = ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len)); + ax_l_items[2] = ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len)); + rational m_sub_x = m_len - x_len; + ax_r_items[2] = ctx.mk_eq_atom(mk_strlen(t1), mk_int(m_sub_x)); + } else { + ax_l_items[1] = ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len)); + ax_l_items[2] = ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len)); + rational y_sub_n = y_len - n_len; + ax_r_items[2] = ctx.mk_eq_atom(mk_strlen(t1), mk_int(y_sub_n)); + } + + expr_ref ax_l(mgr.mk_and(3, ax_l_items), mgr); + expr_ref ax_r(mgr.mk_and(3, ax_r_items), mgr); + + // Cut Info + add_cut_info_merge(t1, sLevel, m); + add_cut_info_merge(t1, sLevel, y); + + assert_implication(ax_l, ax_r); + } else { + loopDetected = true; + TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + // TODO printCutVar(m, y); + } } else if (splitType == 1) { NOT_IMPLEMENTED_YET(); // TODO } else if (splitType == 2) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index ecd7e443f..e8dc6909e 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -117,6 +117,7 @@ namespace smt { expr * mk_concat_const_str(expr * n1, expr * n2); app * mk_int(int n); + app * mk_int(rational & q); void check_and_init_cut_var(expr * node); void add_cut_info_one_node(expr * baseNode, int slevel, expr * node); From bd2b014008483ac42c53338007d5dab59b704880 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 8 Jun 2016 19:32:25 -0400 Subject: [PATCH 098/562] debugging information for dependence analysis --- src/smt/theory_str.cpp | 209 ++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 22 +++++ 2 files changed, 227 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index cc164ec3b..4918c999b 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -27,6 +27,10 @@ namespace smt { theory_str::theory_str(ast_manager & m): theory(m.mk_family_id("str")), + /* Options */ + opt_AggressiveLengthTesting(true), + opt_AggressiveValueTesting(true), + /* Internal setup */ search_started(false), m_autil(m), m_strutil(m), @@ -344,6 +348,14 @@ void theory_str::check_and_init_cut_var(expr * node) { } } +literal theory_str::mk_literal(expr* _e) { + ast_manager & m = get_manager(); + expr_ref e(_e, m); + context& ctx = get_context(); + ensure_enode(e); + return ctx.get_literal(e); +} + app * theory_str::mk_int(int n) { return m_autil.mk_numeral(rational(n), true); } @@ -3513,6 +3525,160 @@ inline expr * theory_str::getMostRightNodeInConcat(expr * node) { } } +void theory_str::trace_ctx_dep(std::ofstream & tout, + std::map & aliasIndexMap, + std::map & var_eq_constStr_map, + std::map > & var_eq_concat_map, + std::map & concat_eq_constStr_map, + std::map > & concat_eq_concat_map) { +#ifdef _TRACE + ast_manager & mgr = get_manager(); + { + tout << "(0) alias: variables" << std::endl; + std::map > aliasSumMap; + std::map::iterator itor0 = aliasIndexMap.begin(); + for (; itor0 != aliasIndexMap.end(); itor0++) { + aliasSumMap[itor0->second][itor0->first] = 1; + } + std::map >::iterator keyItor = aliasSumMap.begin(); + for (; keyItor != aliasSumMap.end(); keyItor++) { + tout << " * "; + tout << mk_pp(keyItor->first, mgr); + tout << " : "; + std::map::iterator innerItor = keyItor->second.begin(); + for (; innerItor != keyItor->second.end(); innerItor++) { + tout << mk_pp(innerItor->first, mgr); + tout << ", "; + } + tout << std::endl; + } + tout << std::endl; + } + + { + tout << "(1) var = constStr:" << std::endl; + std::map::iterator itor1 = var_eq_constStr_map.begin(); + for (; itor1 != var_eq_constStr_map.end(); itor1++) { + tout << " * "; + tout << mk_pp(itor1->first, mgr); + tout << " = "; + tout << mk_pp(itor1->second, mgr); + if (!in_same_eqc(itor1->first, itor1->second)) { + tout << " (not true in ctx)"; + } + tout << std::endl; + } + tout << std::endl; + } + + { + tout << "(2) var = concat:" << std::endl; + std::map >::iterator itor2 = var_eq_concat_map.begin(); + for (; itor2 != var_eq_concat_map.end(); itor2++) { + tout << " * "; + tout << mk_pp(itor2->first, mgr); + tout << " = { "; + std::map::iterator i_itor = itor2->second.begin(); + for (; i_itor != itor2->second.end(); i_itor++) { + tout << mk_pp(i_itor->first, mgr); + tout << ", "; + } + tout << std::endl; + } + tout << std::endl; + } +/*// TODO + { + __debugPrint(logFile, "(3) var = unrollFunc:\n"); + std::map >::iterator itor2 = var_eq_unroll_map.begin(); + for (; itor2 != var_eq_unroll_map.end(); itor2++) { + __debugPrint(logFile, " * "); + printZ3Node(t, itor2->first); + __debugPrint(logFile, " = { "); + std::map::iterator i_itor = itor2->second.begin(); + for (; i_itor != itor2->second.end(); i_itor++) { + printZ3Node(t, i_itor->first); + __debugPrint(logFile, ", "); + } + __debugPrint(logFile, " }\n"); + } + __debugPrint(logFile, "\n"); + } +*/ + { + tout << "(4) concat = constStr:" << std::endl; + std::map::iterator itor3 = concat_eq_constStr_map.begin(); + for (; itor3 != concat_eq_constStr_map.end(); itor3++) { + tout << " * "; + tout << mk_pp(itor3->first, mgr); + tout << " = "; + tout << mk_pp(itor3->second, mgr); + tout << std::endl; + + } + tout << std::endl; + } + + { + tout << "(5) eq concats:" << std::endl; + std::map >::iterator itor4 = concat_eq_concat_map.begin(); + for (; itor4 != concat_eq_concat_map.end(); itor4++) { + if (itor4->second.size() > 1) { + std::map::iterator i_itor = itor4->second.begin(); + tout << " * "; + for (; i_itor != itor4->second.end(); i_itor++) { + tout << mk_pp(i_itor->first, mgr); + tout << " , "; + } + tout << std::endl; + } + } + tout << std::endl; + } +/*// TODO + { + __debugPrint(logFile, "(6) eq unrolls:\n"); + std::map >::iterator itor5 = unrollGroupMap.begin(); + for (; itor5 != unrollGroupMap.end(); itor5++) { + __debugPrint(logFile, " * "); + std::set::iterator i_itor = itor5->second.begin(); + for (; i_itor != itor5->second.end(); i_itor++) { + printZ3Node(t, *i_itor); + __debugPrint(logFile, ", "); + } + __debugPrint(logFile, "\n"); + } + __debugPrint(logFile, "\n"); + } + + { + __debugPrint(logFile, "(7) unroll = concats:\n"); + std::map >::iterator itor5 = unrollGroupMap.begin(); + for (; itor5 != unrollGroupMap.end(); itor5++) { + __debugPrint(logFile, " * "); + Z3_ast unroll = itor5->first; + printZ3Node(t, unroll); + __debugPrint(logFile, "\n"); + Z3_ast curr = unroll; + do { + if (isConcatFunc(t, curr)) { + __debugPrint(logFile, " >>> "); + printZ3Node(t, curr); + __debugPrint(logFile, "\n"); + } + curr = Z3_theory_get_eqc_next(t, curr); + }while (curr != unroll); + __debugPrint(logFile, "\n"); + } + __debugPrint(logFile, "\n"); + } + */ +#else + return; +#endif // _TRACE +} + + /* * Dependence analysis from current context assignment * - "freeVarMap" contains a set of variables that doesn't constrained by Concats. @@ -3747,7 +3913,9 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map & strVarMap, std::map & freeVarMap, std::map > & unrollGroupMap); + void trace_ctx_dep(std::ofstream & tout, + std::map & aliasIndexMap, + std::map & var_eq_constStr_map, + std::map > & var_eq_concat_map, + std::map & concat_eq_constStr_map, + std::map > & concat_eq_concat_map); + void classify_ast_by_type(expr * node, std::map & varMap, std::map & concatMap, std::map & unrollMap); void classify_ast_by_type_in_positive_context(std::map & varMap, From 633237257390ce0e4aa4ebc490a318953adce856 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 8 Jun 2016 20:01:56 -0400 Subject: [PATCH 099/562] more debugging info in theory_str final check; fix variable classification bug --- src/smt/theory_str.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 4918c999b..8523fa29c 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3431,10 +3431,12 @@ void theory_str::dump_assignments() { void theory_str::classify_ast_by_type(expr * node, std::map & varMap, std::map & concatMap, std::map & unrollMap) { - // check whether the node is a non-internal string variable; - // testing set membership here bypasses several expensive checks + // check whether the node is a string variable; + // testing set membership here bypasses several expensive checks. + // note that internal variables don't count if they're only length tester / value tester vars. if (variable_set.find(node) != variable_set.end() - && internal_variable_set.find(node) == internal_variable_set.end()) { + && internal_lenTest_vars.find(node) == internal_lenTest_vars.end() + && internal_valTest_vars.find(node) == internal_valTest_vars.end()) { varMap[node] = 1; } // check whether the node is a function that we want to inspect @@ -4140,7 +4142,20 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map >::iterator itor = depMap.begin(); itor != depMap.end(); itor++) { + tout << mk_pp(itor->first, m); + rational nnLen; + bool nnLen_exists = get_len_value(itor->first, nnLen); + tout << " [len = " << (nnLen_exists ? nnLen.to_string() : "?") << "] \t-->\t"; + for (std::map::iterator itor1 = itor->second.begin(); itor1 != itor->second.end(); itor1++) { + tout << mk_pp(itor1->first, m) << "(" << itor1->second << "), "; + } + tout << std::endl; + } + ); // step, errr, 5: compute free variables based on the dependence map From ae74b47924984a7778b77152fc00c96c918e178c Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 9 Jun 2016 15:41:31 -0400 Subject: [PATCH 100/562] string concat-eq type 1 integer integration --- src/smt/theory_str.cpp | 49 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 8523fa29c..6de5a10b7 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1568,9 +1568,52 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { // TODO printCutVar(m, y); } } else if (splitType == 1) { - NOT_IMPLEMENTED_YET(); // TODO + // Type 1: + // len(x) = len(m) || len(y) = len(n) + expr_ref ax_l1(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); + expr_ref ax_l2(mgr.mk_or(ctx.mk_eq_atom(mk_strlen(x), mk_strlen(m)), ctx.mk_eq_atom(mk_strlen(y), mk_strlen(n))), mgr); + expr_ref ax_l(mgr.mk_and(ax_l1, ax_l2), mgr); + expr_ref ax_r(mgr.mk_and(ctx.mk_eq_atom(x,m), ctx.mk_eq_atom(y,n)), mgr); + assert_implication(ax_l, ax_r); } else if (splitType == 2) { - NOT_IMPLEMENTED_YET(); // TODO + // Type 2: X cuts N. + // len(x) > len(m) || len(y) < len(n) + if (!has_self_cut(x, n)) { + expr_ref m_t2(mk_concat(m, t2), mgr); + expr_ref t2_y(mk_concat(t2, y), mgr); + + expr ** ax_l_items = alloc_svect(expr*, 3); + ax_l_items[0] = ctx.mk_eq_atom(concatAst1, concatAst2); + + expr ** ax_r_items = alloc_svect(expr*, 3); + ax_r_items[0] = ctx.mk_eq_atom(x, m_t2); + ax_r_items[1] = ctx.mk_eq_atom(t2_y, n); + + if (m_len_exists && x_len_exists) { + ax_l_items[1] = ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len)); + ax_l_items[2] = ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len)); + rational x_sub_m = x_len - m_len; + ax_r_items[2] = ctx.mk_eq_atom(mk_strlen(t2), mk_int(x_sub_m)); + } else { + ax_l_items[1] = ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len)); + ax_l_items[2] = ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len)); + rational n_sub_y = n_len - y_len; + ax_r_items[2] = ctx.mk_eq_atom(mk_strlen(t2), mk_int(n_sub_y)); + } + + expr_ref ax_l(mgr.mk_and(3, ax_l_items), mgr); + expr_ref ax_r(mgr.mk_and(3, ax_r_items), mgr); + + // Cut Info + add_cut_info_merge(t2, sLevel, x); + add_cut_info_merge(t2, sLevel, n); + + assert_implication(ax_l, ax_r); + } else { + loopDetected = true; + TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + // TODO printCutVar(m, y); + } } else if (splitType == -1) { // Here we don't really have a choice. We have no length information at all... expr ** or_item = alloc_svect(expr*, 3); @@ -1590,7 +1633,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { expr_ref x_plus_t1(m_autil.mk_add(mk_strlen(x), mk_strlen(t1)), mgr); and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(m), x_plus_t1)); - // TODO these are crashing the solvers because the integer theory + // These were crashing the solver because the integer theory // expects a constant on the right-hand side. // The things we want to assert here are len(m) > len(x) and len(y) > len(n). // We rewrite A > B as A-B > 0 and then as not(A-B <= 0), From 6f5ee2c3ce50ebe3dfe18a4c391aa5765142a4a5 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 9 Jun 2016 16:04:13 -0400 Subject: [PATCH 101/562] string concat-eq type 2 integer integration --- src/smt/theory_str.cpp | 115 ++++++++++++++++++++++++++++++++++------- 1 file changed, 96 insertions(+), 19 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 6de5a10b7..0418fefd7 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1776,21 +1776,14 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { y = v1_arg1; } - const char * strValue_tmp = 0; - m_strutil.is_string(strAst, &strValue_tmp); - std::string strValue(strValue_tmp); - // TODO integer theory interaction - /* - int x_len = getLenValue(t, x); - int y_len = getLenValue(t, y); - int m_len = getLenValue(t, m); - int str_len = getLenValue(t, strAst); - */ + std::string strValue = m_strutil.get_string_constant_value(strAst); - int x_len = -1; - int y_len = -1; - int m_len = -1; - int str_len = -1; + rational x_len, y_len, m_len, str_len; + bool x_len_exists = get_len_value(x, x_len); + bool y_len_exists = get_len_value(y, y_len); + bool m_len_exists = get_len_value(m, m_len); + bool str_len_exists = true; + str_len = rational((unsigned)(strValue.length())); // setup @@ -1816,7 +1809,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { } int splitType = -1; - if (x_len != -1 && m_len != -1) { + if (x_len_exists && m_len_exists) { if (x_len < m_len) splitType = 0; else if (x_len == m_len) @@ -1824,7 +1817,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { else splitType = 2; } - if (splitType == -1 && y_len != -1 && str_len != -1) { + if (splitType == -1 && y_len_exists && str_len_exists) { if (y_len > str_len) splitType = 0; else if (y_len == str_len) @@ -1838,11 +1831,95 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { // Provide fewer split options when length information is available. if (splitType == 0) { - NOT_IMPLEMENTED_YET(); // TODO + // M cuts Y + // | x | y | + // | m | str | + expr_ref temp1_strAst(mk_concat(temp1, strAst), mgr); + if (can_two_nodes_eq(y, temp1_strAst)) { + if (!avoidLoopCut || !(has_self_cut(m, y))) { + // break down option 2-1 + expr ** l_items = alloc_svect(expr*, 3); + l_items[0] = ctx.mk_eq_atom(concatAst1, concatAst2); + + expr ** r_items = alloc_svect(expr*, 3); + expr_ref x_temp1(mk_concat(x, temp1), mgr); + r_items[0] = ctx.mk_eq_atom(m, x_temp1); + r_items[1] = ctx.mk_eq_atom(y, temp1_strAst); + + if (x_len_exists && m_len_exists) { + l_items[1] = ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len)); + l_items[2] = ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len)); + rational m_sub_x = (m_len - x_len); + r_items[2] = ctx.mk_eq_atom(mk_strlen(temp1), mk_int(m_sub_x)); + } else { + l_items[1] = ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len)); + l_items[2] = ctx.mk_eq_atom(mk_strlen(strAst), mk_int(str_len)); + rational y_sub_str = (y_len - str_len); + r_items[2] = ctx.mk_eq_atom(mk_strlen(temp1), mk_int(y_sub_str)); + } + + expr_ref ax_l(mgr.mk_and(3, l_items), mgr); + expr_ref ax_r(mgr.mk_and(3, r_items), mgr); + + add_cut_info_merge(temp1, sLevel, y); + add_cut_info_merge(temp1, sLevel, m); + + assert_implication(ax_l, ax_r); + } else { + loopDetected = true; + TRACE("t_str", tout << "AVOID LOOP: SKIP" << std::endl;); + // TODO printCutVar(m, y); + } + } } else if (splitType == 1) { - NOT_IMPLEMENTED_YET(); // TODO + // | x | y | + // | m | str | + expr_ref ax_l1(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); + expr_ref ax_l2(mgr.mk_or( + ctx.mk_eq_atom(mk_strlen(x), mk_strlen(m)), + ctx.mk_eq_atom(mk_strlen(y), mk_strlen(strAst))), mgr); + expr_ref ax_l(mgr.mk_and(ax_l1, ax_l2), mgr); + expr_ref ax_r(mgr.mk_and(ctx.mk_eq_atom(x, m), ctx.mk_eq_atom(y, strAst)), mgr); + assert_implication(ax_l, ax_r); } else if (splitType == 2) { - NOT_IMPLEMENTED_YET(); // TODO + // m cut y, + // | x | y | + // | m | str | + rational lenDelta; + expr ** l_items = alloc_svect(expr*, 3); + int l_count = 0; + l_items[0] = ctx.mk_eq_atom(concatAst1, concatAst2); + if (x_len_exists && m_len_exists) { + l_items[1] = ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len)); + l_items[2] = ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len)); + l_count = 3; + lenDelta = x_len - m_len; + } else { + l_items[1] = ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len)); + l_count = 2; + lenDelta = str_len - y_len; + } + std::string part1Str = strValue.substr(0, lenDelta.get_unsigned()); + std::string part2Str = strValue.substr(lenDelta.get_unsigned(), strValue.length() - lenDelta.get_unsigned()); + + expr_ref prefixStr(m_strutil.mk_string(part1Str), mgr); + expr_ref x_concat(mk_concat(m, prefixStr), mgr); + expr_ref cropStr(m_strutil.mk_string(part2Str), mgr); + + if (can_two_nodes_eq(x, x_concat) && can_two_nodes_eq(y, cropStr)) { + expr ** r_items = alloc_svect(expr*, 2); + r_items[0] = ctx.mk_eq_atom(x, x_concat); + r_items[1] = ctx.mk_eq_atom(y, cropStr); + expr_ref ax_l(mgr.mk_and(l_count, l_items), mgr); + expr_ref ax_r(mgr.mk_and(2, r_items), mgr); + + assert_implication(ax_l, ax_r); + } else { + // negate! It's impossible to split str with these lengths + TRACE("t_str", tout << "CONFLICT: Impossible to split str with these lengths." << std::endl;); + expr_ref ax_l(mgr.mk_and(l_count, l_items), mgr); + assert_axiom(mgr.mk_not(ax_l)); + } } else { // Split type -1: no idea about the length... int optionTotal = 2 + strValue.length(); From 91d82956b29a91c744390981a1acfd4f5653eadb Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 9 Jun 2016 16:25:19 -0400 Subject: [PATCH 102/562] string concat-eq type 3 integer integration --- src/smt/theory_str.cpp | 107 ++++++++++++++++++++++++++++++++++------- 1 file changed, 89 insertions(+), 18 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 0418fefd7..ba9b503be 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2042,20 +2042,14 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { y = v1_arg1; } - const char * strValue_tmp = 0; - m_strutil.is_string(strAst, &strValue_tmp); - std::string strValue(strValue_tmp); + std::string strValue = m_strutil.get_string_constant_value(strAst); + // TODO integer theory interaction - /* - int x_len = getLenValue(t, x); - int y_len = getLenValue(t, y); - int str_len = getLenValue(t, strAst); - int n_len = getLenValue(t, n); - */ - int x_len = -1; - int y_len = -1; - int str_len = -1; - int n_len = -1; + rational x_len, y_len, str_len, n_len; + bool x_len_exists = get_len_value(x, x_len); + bool y_len_exists = get_len_value(y, y_len); + str_len = rational((unsigned)(strValue.length())); + bool n_len_exists = get_len_value(n, n_len); expr_ref xorFlag(mgr); expr_ref temp1(mgr); @@ -2080,7 +2074,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { int splitType = -1; - if (x_len != -1) { + if (x_len_exists) { if (x_len < str_len) splitType = 0; else if (x_len == str_len) @@ -2088,7 +2082,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { else splitType = 2; } - if (splitType == -1 && y_len != -1 && n_len != -1) { + if (splitType == -1 && y_len_exists && n_len_exists) { if (y_len > n_len) splitType = 0; else if (y_len == n_len) @@ -2101,13 +2095,90 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { // Provide fewer split options when length information is available. if (splitType == 0) { - NOT_IMPLEMENTED_YET(); // TODO + // | x | y | + // | str | n | + expr_ref_vector litems(mgr); + litems.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); + rational prefixLen; + if (!x_len_exists) { + prefixLen = str_len - (y_len - n_len); + litems.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); + litems.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len))); + } else { + prefixLen = x_len; + litems.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); + } + std::string prefixStr = strValue.substr(0, prefixLen.get_unsigned()); + rational str_sub_prefix = str_len - prefixLen; + std::string suffixStr = strValue.substr(prefixLen.get_unsigned(), str_sub_prefix.get_unsigned()); + expr_ref prefixAst(m_strutil.mk_string(prefixStr), mgr); + expr_ref suffixAst(m_strutil.mk_string(suffixStr), mgr); + expr_ref ax_l(mgr.mk_and(litems.size(), litems.c_ptr()), mgr); + + expr_ref suf_n_concat(mk_concat(suffixAst, n), mgr); + if (can_two_nodes_eq(x, prefixAst) && can_two_nodes_eq(y, suf_n_concat)) { + expr ** r_items = alloc_svect(expr*, 2); + r_items[0] = ctx.mk_eq_atom(x, prefixAst); + r_items[1] = ctx.mk_eq_atom(y, suf_n_concat); + assert_implication(ax_l, mgr.mk_and(2, r_items)); + } else { + // negate! It's impossible to split str with these lengths + TRACE("t_str", tout << "CONFLICT: Impossible to split str with these lengths." << std::endl;); + assert_axiom(mgr.mk_not(ax_l)); + } } else if (splitType == 1) { - NOT_IMPLEMENTED_YET(); // TODO + expr_ref ax_l1(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); + expr_ref ax_l2(mgr.mk_or( + ctx.mk_eq_atom(mk_strlen(x), mk_strlen(strAst)), + ctx.mk_eq_atom(mk_strlen(y), mk_strlen(n))), mgr); + expr_ref ax_l(mgr.mk_and(ax_l1, ax_l2), mgr); + expr_ref ax_r(mgr.mk_and(ctx.mk_eq_atom(x, strAst), ctx.mk_eq_atom(y, n)), mgr); + assert_implication(ax_l, ax_r); } else if (splitType == 2) { - NOT_IMPLEMENTED_YET(); // TODO + // | x | y | + // | str | n | + expr_ref_vector litems(mgr); + litems.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); + rational tmpLen; + if (!x_len_exists) { + tmpLen = n_len - y_len; + litems.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); + litems.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len))); + } else { + tmpLen = x_len - str_len; + litems.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); + } + expr_ref ax_l(mgr.mk_and(litems.size(), litems.c_ptr()), mgr); + + expr_ref str_temp1(mk_concat(strAst, temp1), mgr); + expr_ref temp1_y(mk_concat(temp1, y), mgr); + + if (can_two_nodes_eq(x, str_temp1)) { + if (!avoidLoopCut || !(has_self_cut(x, n))) { + expr ** r_items = alloc_svect(expr*, 3); + r_items[0] = ctx.mk_eq_atom(x, str_temp1); + r_items[1] = ctx.mk_eq_atom(n, temp1_y); + r_items[2] = ctx.mk_eq_atom(mk_strlen(temp1), mk_int(tmpLen)); + expr_ref ax_r(mgr.mk_and(3, r_items), mgr); + + //Cut Info + add_cut_info_merge(temp1, sLevel, x); + add_cut_info_merge(temp1, sLevel, n); + + assert_implication(ax_l, ax_r); + } else { + loopDetected = true; + TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + // TODO printCutVar(x, n); + } + } + // else { + // // negate! It's impossible to split str with these lengths + // __debugPrint(logFile, "[Conflict] Negate! It's impossible to split str with these lengths @ %d.\n", __LINE__); + // addAxiom(t, Z3_mk_not(ctx, ax_l), __LINE__); + // } } else { // Split type -1. We know nothing about the length... From 1520760a04de41827bb177d2d67427033c9d2286 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 9 Jun 2016 20:31:21 -0400 Subject: [PATCH 103/562] string-integer integration in free var gen --- src/smt/theory_str.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ba9b503be..f8366ed07 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -5302,13 +5302,12 @@ void theory_str::process_free_var(std::map & freeVar_map) { } if (standAlone) { - // TODO - // int lenValue = getLenValue(freeVar); - int lenValue = -1; - if (lenValue != -1) { + rational len_value; + bool len_value_exists = get_len_value(freeVar, len_value); + if (len_value_exists) { leafVarSet.insert(freeVar); } else { - aloneVars[lenValue].insert(freeVar); + aloneVars[-1].insert(freeVar); } } else { leafVarSet.insert(freeVar); From fd968783a599961a67ccc3afb2b90c4a776f9f38 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 9 Jun 2016 20:35:26 -0400 Subject: [PATCH 104/562] fix model generation for theory_str --- src/smt/theory_str.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 6616e9ffa..99899b365 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -44,9 +44,7 @@ namespace smt { return true; } virtual expr * get_fresh_value(sort * s) { - // TODO this may be causing crashes in model gen? investigate - //return m_util.mk_fresh_string(); - NOT_IMPLEMENTED_YET(); + return m_util.mk_fresh_string(); } virtual void register_value(expr * n) { /* Ignore */ } }; From 08328c5614f26712946d57bd0d5594831608c292 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 12 Jun 2016 17:16:14 -0400 Subject: [PATCH 105/562] add option in theory_str to assert string constant lengths more eagerly now passes z3str/concat-025 --- src/smt/theory_str.cpp | 11 ++++++++++- src/smt/theory_str.h | 10 ++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index f8366ed07..02db2132a 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -30,6 +30,7 @@ theory_str::theory_str(ast_manager & m): /* Options */ opt_AggressiveLengthTesting(true), opt_AggressiveValueTesting(true), + opt_EagerStringConstantLengthAssertions(true), /* Internal setup */ search_started(false), m_autil(m), @@ -191,7 +192,9 @@ bool theory_str::internalize_term(app * term) { //} */ - // from theory_seq::internalize_term() + // TODO do we still need to do instantiate_concat_axiom()? + + // partially from theory_seq::internalize_term() if (ctx.e_internalized(term)) { enode* e = ctx.get_enode(term); mk_var(e); @@ -217,6 +220,12 @@ bool theory_str::internalize_term(app * term) { else { e = ctx.mk_enode(term, false, m.is_bool(term), true); } + + if (opt_EagerStringConstantLengthAssertions && m_strutil.is_string(term)) { + TRACE("t_str", tout << "eagerly asserting length of string term " << mk_pp(term, m) << std::endl;); + m_basicstr_axiom_todo.insert(e); + } + theory_var v = mk_var(e); TRACE("t_str_detail", tout << "term " << mk_ismt2_pp(term, get_manager()) << " = v#" << v << std::endl;); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 99899b365..2b8077a13 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -74,6 +74,15 @@ namespace smt { */ bool opt_AggressiveValueTesting; + /* + * Setting EagerStringConstantLengthAssertions to true allows some methods, + * in particular internalize_term(), to add + * length assertions about relevant string constants. + * Note that currently this should always be set to 'true', or else *no* length assertions + * will be made about string constants. + */ + bool opt_EagerStringConstantLengthAssertions; + bool search_started; arith_util m_autil; str_util m_strutil; @@ -87,6 +96,7 @@ namespace smt { ptr_vector m_basicstr_axiom_todo; svector > m_str_eq_todo; ptr_vector m_concat_axiom_todo; + ptr_vector m_string_constant_length_todo; int tmpStringVarCount; int tmpXorVarCount; From 18cd47dcd02c92a5805b1ccb04b4879d06273aa1 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 12 Jun 2016 20:14:57 -0400 Subject: [PATCH 106/562] add flag for bailing out during a final check infinite loop in theory_str also adds more debugging to free variable gen --- src/smt/theory_str.cpp | 28 +++++++++++++++++++++++++++- src/smt/theory_str.h | 8 ++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 02db2132a..aaeb9ccce 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -31,11 +31,13 @@ theory_str::theory_str(ast_manager & m): opt_AggressiveLengthTesting(true), opt_AggressiveValueTesting(true), opt_EagerStringConstantLengthAssertions(true), + opt_VerifyFinalCheckProgress(true), /* Internal setup */ search_started(false), m_autil(m), m_strutil(m), sLevel(0), + finalCheckProgressIndicator(false), m_trail(m), tmpStringVarCount(0), tmpXorVarCount(0), @@ -125,6 +127,9 @@ void theory_str::initialize_charset() { } void theory_str::assert_axiom(expr * e) { + if (opt_VerifyFinalCheckProgress) { + finalCheckProgressIndicator = true; + } if (get_manager().is_true(e)) return; TRACE("t_str_detail", tout << "asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); context & ctx = get_context(); @@ -4501,6 +4506,10 @@ final_check_status theory_str::final_check_eh() { context & ctx = get_context(); ast_manager & m = get_manager(); + if (opt_VerifyFinalCheckProgress) { + finalCheckProgressIndicator = false; + } + TRACE("t_str", tout << "final check" << std::endl;); TRACE("t_str_detail", dump_assignments();); @@ -4655,7 +4664,19 @@ final_check_status theory_str::final_check_eh() { constValue = NULL; - // TODO this would be a great place to print debugging information + { + TRACE("t_str_detail", tout << "free var map (# " << freeVar_map.size() << "):" << std::endl; + for (std::map::iterator freeVarItor1 = freeVar_map.begin(); freeVarItor1 != freeVar_map.end(); freeVarItor1++) { + expr * freeVar = freeVarItor1->first; + rational lenValue; + bool lenValue_exists = get_len_value(freeVar, lenValue); + // TODO get_bound_strlen() + tout << mk_pp(freeVar, m) << " [depCnt = " << freeVarItor1->second << ", length = " + << (lenValue_exists ? lenValue.to_string() : "?") + << "]" << std::endl; + } + ); + } // TODO process_concat_eq_unroll() /* @@ -4712,6 +4733,11 @@ final_check_status theory_str::final_check_eh() { } */ + if (opt_VerifyFinalCheckProgress && !finalCheckProgressIndicator) { + TRACE("t_str", tout << "BUG: no progress in final check, giving up!!" << std::endl;); + m.raise_exception("no progress in theory_str final check"); + } + return FC_CONTINUE; // since by this point we've added axioms } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 2b8077a13..562f49004 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -83,11 +83,19 @@ namespace smt { */ bool opt_EagerStringConstantLengthAssertions; + /* + * If VerifyFinalCheckProgress is set to true, continuing after final check is invoked + * without asserting any new axioms is considered a bug and will throw an exception. + */ + bool opt_VerifyFinalCheckProgress; + bool search_started; arith_util m_autil; str_util m_strutil; int sLevel; + bool finalCheckProgressIndicator; + // TODO make sure that all generated expressions are saved into the trail expr_ref_vector m_trail; // trail for generated terms From 7d09dbb8ec8685a0cb9b75bdf87733839fc179e1 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 12 Jun 2016 20:46:52 -0400 Subject: [PATCH 107/562] basic infrastructure for string rewriting --- src/ast/rewriter/rewriter.txt | 2 + src/ast/rewriter/str_rewriter.cpp | 66 +++++++++++++++++++++++++++++++ src/ast/rewriter/str_rewriter.h | 46 +++++++++++++++++++++ src/ast/rewriter/th_rewriter.cpp | 8 ++++ 4 files changed, 122 insertions(+) create mode 100644 src/ast/rewriter/str_rewriter.cpp create mode 100644 src/ast/rewriter/str_rewriter.h diff --git a/src/ast/rewriter/rewriter.txt b/src/ast/rewriter/rewriter.txt index cdfba9f0f..a7a9e5eff 100644 --- a/src/ast/rewriter/rewriter.txt +++ b/src/ast/rewriter/rewriter.txt @@ -7,6 +7,8 @@ The following classes implement theory specific rewriting rules: - array_rewriter - datatype_rewriter - fpa_rewriter + - seq_rewriter + - str_rewriter Each of them provide the method br_status mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp new file mode 100644 index 000000000..35a255871 --- /dev/null +++ b/src/ast/rewriter/str_rewriter.cpp @@ -0,0 +1,66 @@ +/*++ +Copyright (c) 2016 Microsoft Corporation + +Module Name: + + str_rewriter.cpp + +Abstract: + + AST rewriting rules for string terms. + +Author: + + Murphy Berzish + +Notes: + +--*/ + +#include"str_rewriter.h" +#include"arith_decl_plugin.h" +#include"ast_pp.h" +#include"ast_util.h" +#include"well_sorted.h" + +br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) { + SASSERT(f->get_family_id() == get_fid()); + + TRACE("t_str_rw", tout << "rewrite app: " << f->get_name() << std::endl;); + + switch(f->get_decl_kind()) { + default: + return BR_FAILED; + } +} + +br_status str_rewriter::mk_eq_core(expr * l, expr * r, expr_ref & result) { + // from seq_rewriter + expr_ref_vector lhs(m()), rhs(m()), res(m()); + bool changed = false; + if (!reduce_eq(l, r, lhs, rhs, changed)) { + result = m().mk_false(); + return BR_DONE; + } + if (!changed) { + return BR_FAILED; + } + for (unsigned i = 0; i < lhs.size(); ++i) { + res.push_back(m().mk_eq(lhs[i].get(), rhs[i].get())); + } + result = mk_and(res); + return BR_REWRITE3; +} + +bool str_rewriter::reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change) { + // TODO inspect seq_rewriter::reduce_eq() + change = false; + return true; +} + +bool str_rewriter::reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change) { + // TODO inspect seq_rewriter::reduce_eq() + change = false; + return true; +} + diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h new file mode 100644 index 000000000..fde36e92e --- /dev/null +++ b/src/ast/rewriter/str_rewriter.h @@ -0,0 +1,46 @@ +/*++ +Copyright (c) 2016 Microsoft Corporation + +Module Name: + + str_rewriter.h + +Abstract: + + AST rewriting rules for string terms. + +Author: + + Murphy Berzish + +Notes: + +--*/ + +#include"str_decl_plugin.h" +#include"arith_decl_plugin.h" +#include"rewriter_types.h" +#include"params.h" + +class str_rewriter { + str_util m_strutil; + arith_util m_autil; + +public: + str_rewriter(ast_manager & m, params_ref const & p = params_ref()) : + m_strutil(m), m_autil(m) { + } + + ast_manager & m() const { return m_strutil.get_manager(); } + family_id get_fid() const { return m_strutil.get_family_id(); } + + void updt_params(params_ref const & p) {} + static void get_param_descrs(param_descrs & r) {} + + br_status mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result); + br_status mk_eq_core(expr * lhs, expr * rhs, expr_ref & result); + + bool reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change); + bool reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change); + +}; diff --git a/src/ast/rewriter/th_rewriter.cpp b/src/ast/rewriter/th_rewriter.cpp index 6f6daf8df..a56ca91d8 100644 --- a/src/ast/rewriter/th_rewriter.cpp +++ b/src/ast/rewriter/th_rewriter.cpp @@ -27,6 +27,7 @@ Notes: #include"dl_rewriter.h" #include"pb_rewriter.h" #include"seq_rewriter.h" +#include"str_rewriter.h" #include"rewriter_def.h" #include"expr_substitution.h" #include"ast_smt2_pp.h" @@ -45,6 +46,7 @@ struct th_rewriter_cfg : public default_rewriter_cfg { dl_rewriter m_dl_rw; pb_rewriter m_pb_rw; seq_rewriter m_seq_rw; + str_rewriter m_str_rw; arith_util m_a_util; bv_util m_bv_util; unsigned long long m_max_memory; // in bytes @@ -79,6 +81,7 @@ struct th_rewriter_cfg : public default_rewriter_cfg { m_ar_rw.updt_params(p); m_f_rw.updt_params(p); m_seq_rw.updt_params(p); + m_str_rw.updt_params(p); updt_local_params(p); } @@ -179,6 +182,8 @@ struct th_rewriter_cfg : public default_rewriter_cfg { st = m_ar_rw.mk_eq_core(args[0], args[1], result); else if (s_fid == m_seq_rw.get_fid()) st = m_seq_rw.mk_eq_core(args[0], args[1], result); + else if (s_fid == m_str_rw.get_fid()) + st = m_str_rw.mk_eq_core(args[0], args[1], result); if (st != BR_FAILED) return st; @@ -207,6 +212,8 @@ struct th_rewriter_cfg : public default_rewriter_cfg { return m_pb_rw.mk_app_core(f, num, args, result); if (fid == m_seq_rw.get_fid()) return m_seq_rw.mk_app_core(f, num, args, result); + if (fid == m_str_rw.get_fid()) + return m_str_rw.mk_app_core(f, num, args, result); return BR_FAILED; } @@ -665,6 +672,7 @@ struct th_rewriter_cfg : public default_rewriter_cfg { m_dl_rw(m), m_pb_rw(m), m_seq_rw(m), + m_str_rw(m), m_a_util(m), m_bv_util(m), m_used_dependencies(m), From 389845180c0f03cc0f808ac7cd1cab3bac691e1d Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 13 Jun 2016 16:34:24 -0400 Subject: [PATCH 108/562] add CharAt to theory_str and basic rewrite rule for constant CharAt exprs --- src/ast/rewriter/str_rewriter.cpp | 44 +++++++++++++++++++++++++++++++ src/ast/rewriter/str_rewriter.h | 2 ++ src/ast/str_decl_plugin.cpp | 10 ++++++- src/ast/str_decl_plugin.h | 6 ++++- 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index 35a255871..3967453d4 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -23,12 +23,56 @@ Notes: #include"ast_util.h" #include"well_sorted.h" +br_status str_rewriter::mk_str_CharAt(expr * arg0, expr * arg1, expr_ref & result) { + TRACE("t_str_rw", tout << "rewrite (CharAt " << mk_pp(arg0, m()) << " " << mk_pp(arg1, m()) << ")" << std::endl;); + // if arg0 is a string constant and arg1 is an integer constant, + // we can rewrite this by evaluating the expression + rational arg1Int; + if (m_strutil.is_string(arg0) && m_autil.is_numeral(arg1, arg1Int)) { + TRACE("t_str_rw", tout << "evaluating constant CharAt expression" << std::endl;); + std::string arg0Str = m_strutil.get_string_constant_value(arg0); + std::string resultStr; + if (arg1Int >= rational(0) && arg1Int <= rational((unsigned)arg0Str.length())) { + resultStr = arg0Str.at(arg1Int.get_unsigned()); + TRACE("t_str_rw", tout << "result is '" << resultStr << "'" << std::endl;); + } else { + resultStr = ""; + TRACE("t_str_rw", tout << "bogus length argument, result is empty string" << std::endl;); + } + result = m_strutil.mk_string(resultStr); + return BR_DONE; + } else { + // TODO NEXT + NOT_IMPLEMENTED_YET(); + /* + Z3_ast ts0 = my_mk_internal_string_var(t); + Z3_ast ts1 = my_mk_internal_string_var(t); + Z3_ast ts2 = my_mk_internal_string_var(t); + + Z3_ast cond = mk_2_and(t, Z3_mk_ge(ctx, args[1], mk_int(ctx, 0)), Z3_mk_lt(ctx, args[1], mk_length(t, args[0]))); + + Z3_ast and_item[3]; + and_item[0] = Z3_mk_eq(ctx, args[0], mk_concat(t, ts0, mk_concat(t, ts1, ts2))); + and_item[1] = Z3_mk_eq(ctx, args[1], mk_length(t, ts0)); + and_item[2] = Z3_mk_eq(ctx, mk_length(t, ts1), mk_int(ctx, 1)); + Z3_ast thenBranch = Z3_mk_and(ctx, 3, and_item); + Z3_ast elseBranch = Z3_mk_eq(ctx, ts1, my_mk_str_value(t, "")); + breakdownAssert = Z3_mk_ite(ctx, cond, thenBranch, elseBranch); + return ts1; + */ + } +} + br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) { SASSERT(f->get_family_id() == get_fid()); TRACE("t_str_rw", tout << "rewrite app: " << f->get_name() << std::endl;); + // TODO more rewrites for really easy cases, e.g. (Concat "abc" "def")... switch(f->get_decl_kind()) { + case OP_STR_CHARAT: + SASSERT(num_args == 2); + return mk_str_CharAt(args[0], args[1], result); default: return BR_FAILED; } diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index fde36e92e..01ccde242 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -40,6 +40,8 @@ public: br_status mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result); br_status mk_eq_core(expr * lhs, expr * rhs, expr_ref & result); + br_status mk_str_CharAt(expr * arg0, expr * arg1, expr_ref & result); + bool reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change); bool reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change); diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index cd9cae5a5..03fde5aeb 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -26,6 +26,7 @@ str_decl_plugin::str_decl_plugin(): m_str_decl(0), m_concat_decl(0), m_length_decl(0), + m_charat_decl(0), m_arith_plugin(0), m_arith_fid(0), m_int_sort(0){ @@ -39,6 +40,7 @@ void str_decl_plugin::finalize(void) { DEC_REF(m_str_decl); DEC_REF(m_concat_decl); DEC_REF(m_length_decl); + DEC_REF(m_charat_decl); DEC_REF(m_int_sort); } @@ -64,7 +66,11 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { MK_OP(m_concat_decl, "Concat", OP_STRCAT, s); - m_length_decl = m->mk_func_decl(symbol("Length"), s, i, func_decl_info(id, OP_STRLEN)); m_manager->inc_ref(m_length_decl); + m_length_decl = m->mk_func_decl(symbol("Length"), s, i, func_decl_info(id, OP_STRLEN)); + m_manager->inc_ref(m_length_decl); + + m_charat_decl = m->mk_func_decl(symbol("CharAt"), s, i, s, func_decl_info(id, OP_STR_CHARAT)); + m_manager->inc_ref(m_charat_decl); } decl_plugin * str_decl_plugin::mk_fresh() { @@ -82,6 +88,7 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { switch(k) { case OP_STRCAT: return m_concat_decl; case OP_STRLEN: return m_length_decl; + case OP_STR_CHARAT: return m_charat_decl; default: return 0; } } @@ -138,6 +145,7 @@ app * str_decl_plugin::mk_fresh_string() { void str_decl_plugin::get_op_names(svector & op_names, symbol const & logic) { op_names.push_back(builtin_name("Concat", OP_STRCAT)); op_names.push_back(builtin_name("Length", OP_STRLEN)); + op_names.push_back(builtin_name("CharAt", OP_STR_CHARAT)); } void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 4f46fa5ac..049ef08ba 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -27,9 +27,12 @@ enum str_sort_kind { enum str_op_kind { OP_STR, /* string constants */ - // + // basic string operators OP_STRCAT, OP_STRLEN, + // higher-level string functions -- these are reduced to basic operations + OP_STR_CHARAT, + // end LAST_STR_OP }; @@ -40,6 +43,7 @@ protected: func_decl * m_concat_decl; func_decl * m_length_decl; + func_decl * m_charat_decl; arith_decl_plugin * m_arith_plugin; family_id m_arith_fid; From be5cc02a4594c33b76fc1ca01586486071e0f272 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 13 Jun 2016 21:57:08 -0400 Subject: [PATCH 109/562] working axiomatization for CharAt --- src/ast/rewriter/str_rewriter.cpp | 4 +-- src/smt/theory_str.cpp | 49 ++++++++++++++++++++++++++++++- src/smt/theory_str.h | 7 +++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index 3967453d4..76c0d25ae 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -42,8 +42,8 @@ br_status str_rewriter::mk_str_CharAt(expr * arg0, expr * arg1, expr_ref & resul result = m_strutil.mk_string(resultStr); return BR_DONE; } else { - // TODO NEXT - NOT_IMPLEMENTED_YET(); + // TODO if we ever figure out how to assert axioms in here, add this code + return BR_FAILED; /* Z3_ast ts0 = my_mk_internal_string_var(t); Z3_ast ts1 = my_mk_internal_string_var(t); diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index aaeb9ccce..fe8f12e81 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -567,7 +567,9 @@ expr * theory_str::mk_concat(expr * n1, expr * n2) { } bool theory_str::can_propagate() { - return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty() || !m_concat_axiom_todo.empty(); + return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty() || !m_concat_axiom_todo.empty() + || !m_axiom_CharAt_todo.empty() + ; } void theory_str::propagate() { @@ -590,6 +592,11 @@ void theory_str::propagate() { instantiate_concat_axiom(m_concat_axiom_todo[i]); } m_concat_axiom_todo.reset(); + + for (unsigned i = 0; i < m_axiom_CharAt_todo.size(); ++i) { + instantiate_axiom_CharAt(m_axiom_CharAt_todo[i]); + } + m_axiom_CharAt_todo.reset(); } } @@ -738,6 +745,44 @@ void theory_str::instantiate_str_eq_length_axiom(enode * lhs, enode * rhs) { assert_implication(premise, conclusion); } +void theory_str::instantiate_axiom_CharAt(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * expr = e->get_owner(); + + TRACE("t_str_detail", tout << "instantiate CharAt axiom for " << mk_pp(expr, m) << std::endl;); + + expr_ref ts0(mk_str_var("ts0"), m); + expr_ref ts1(mk_str_var("ts1"), m); + expr_ref ts2(mk_str_var("ts2"), m); + + expr_ref cond(m.mk_and( + m_autil.mk_ge(expr->get_arg(1), mk_int(0)), + // REWRITE for arithmetic theory: + // m_autil.mk_lt(expr->get_arg(1), mk_strlen(expr->get_arg(0))) + m.mk_not(m_autil.mk_ge(m_autil.mk_add(expr->get_arg(1), m_autil.mk_mul(mk_int(-1), mk_strlen(expr->get_arg(0)))), mk_int(0))) + ), m); + + expr_ref_vector and_item(m); + and_item.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(ts0, mk_concat(ts1, ts2)))); + and_item.push_back(ctx.mk_eq_atom(expr->get_arg(1), mk_strlen(ts0))); + and_item.push_back(ctx.mk_eq_atom(mk_strlen(ts1), mk_int(1))); + + expr_ref thenBranch(m.mk_and(and_item.size(), and_item.c_ptr()), m); + expr_ref elseBranch(ctx.mk_eq_atom(ts1, m_strutil.mk_string("")), m); + + expr_ref axiom(m.mk_ite(cond, thenBranch, elseBranch), m); + expr_ref reductionVar(ctx.mk_eq_atom(expr, ts1), m); + + SASSERT(axiom); + SASSERT(reductionVar); + + expr_ref finalAxiom(m.mk_and(axiom, reductionVar), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); +} + void theory_str::attach_new_th_var(enode * n) { context & ctx = get_context(); theory_var v = mk_var(n); @@ -3469,6 +3514,8 @@ void theory_str::set_up_axioms(expr * ex) { if (aVar->get_num_args() == 0 && !is_string(aVar)) { input_var_in_len.insert(var); } + } else if (is_CharAt(ap)) { + m_axiom_CharAt_todo.push_back(n); } else if (ap->get_num_args() == 0 && !is_string(ap)) { // if ex is a variable, add it to our list of variables TRACE("t_str_detail", tout << "tracking variable " << mk_ismt2_pp(ap, get_manager()) << std::endl;); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 562f49004..c86328d30 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -106,6 +106,9 @@ namespace smt { ptr_vector m_concat_axiom_todo; ptr_vector m_string_constant_length_todo; + // enode lists for term-specific axioms + ptr_vector m_axiom_CharAt_todo; + int tmpStringVarCount; int tmpXorVarCount; int tmpLenTestVarCount; @@ -167,10 +170,14 @@ namespace smt { bool is_string(enode const * n) const { return is_string(n->get_owner()); } bool is_strlen(app const * a) const { return a->is_app_of(get_id(), OP_STRLEN); } bool is_strlen(enode const * n) const { return is_strlen(n->get_owner()); } + bool is_CharAt(app const * a) const { return a->is_app_of(get_id(), OP_STR_CHARAT); } + bool is_CharAt(enode const * n) const { return is_CharAt(n->get_owner()); } void instantiate_concat_axiom(enode * cat); void instantiate_basic_string_axioms(enode * str); void instantiate_str_eq_length_axiom(enode * lhs, enode * rhs); + void instantiate_axiom_CharAt(enode * e); + void set_up_axioms(expr * ex); void handle_equality(expr * lhs, expr * rhs); From 7d8e54c50f2de8655c737a64853d86eaa7633a12 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 13 Jun 2016 22:27:46 -0400 Subject: [PATCH 110/562] decl and rewriter for string StartsWith --- src/ast/rewriter/str_rewriter.cpp | 41 +++++++++++++++++++------------ src/ast/rewriter/str_rewriter.h | 1 + src/ast/str_decl_plugin.cpp | 9 +++++++ src/ast/str_decl_plugin.h | 3 +++ 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index 76c0d25ae..4b7ff9057 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -42,24 +42,30 @@ br_status str_rewriter::mk_str_CharAt(expr * arg0, expr * arg1, expr_ref & resul result = m_strutil.mk_string(resultStr); return BR_DONE; } else { - // TODO if we ever figure out how to assert axioms in here, add this code + // TODO if we ever figure out how to assert axioms in here, add the axiom code from Z3str2's strAstReduce.cpp return BR_FAILED; - /* - Z3_ast ts0 = my_mk_internal_string_var(t); - Z3_ast ts1 = my_mk_internal_string_var(t); - Z3_ast ts2 = my_mk_internal_string_var(t); + } +} - Z3_ast cond = mk_2_and(t, Z3_mk_ge(ctx, args[1], mk_int(ctx, 0)), Z3_mk_lt(ctx, args[1], mk_length(t, args[0]))); - - Z3_ast and_item[3]; - and_item[0] = Z3_mk_eq(ctx, args[0], mk_concat(t, ts0, mk_concat(t, ts1, ts2))); - and_item[1] = Z3_mk_eq(ctx, args[1], mk_length(t, ts0)); - and_item[2] = Z3_mk_eq(ctx, mk_length(t, ts1), mk_int(ctx, 1)); - Z3_ast thenBranch = Z3_mk_and(ctx, 3, and_item); - Z3_ast elseBranch = Z3_mk_eq(ctx, ts1, my_mk_str_value(t, "")); - breakdownAssert = Z3_mk_ite(ctx, cond, thenBranch, elseBranch); - return ts1; - */ +br_status str_rewriter::mk_str_StartsWith(expr * haystack, expr * needle, expr_ref & result) { + TRACE("t_str_rw", tout << "rewrite (StartsWith " << mk_pp(haystack, m()) << " " << mk_pp(needle, m()) << ")" << std::endl;); + if (m_strutil.is_string(haystack) && m_strutil.is_string(needle)) { + TRACE("t_str_rw", tout << "evaluating constant StartsWith predicate" << std::endl;); + std::string haystackStr = m_strutil.get_string_constant_value(haystack); + std::string needleStr = m_strutil.get_string_constant_value(needle); + if (haystackStr.length() < needleStr.length()) { + result = m().mk_false(); + return BR_DONE; + } else { + if (haystackStr.substr(0, needleStr.length()) == needleStr) { + result = m().mk_true(); + } else { + result = m().mk_false(); + } + return BR_DONE; + } + } else { + return BR_FAILED; } } @@ -73,6 +79,9 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con case OP_STR_CHARAT: SASSERT(num_args == 2); return mk_str_CharAt(args[0], args[1], result); + case OP_STR_STARTSWITH: + SASSERT(num_args == 2); + return mk_str_StartsWith(args[0], args[1], result); default: return BR_FAILED; } diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index 01ccde242..8f12a75db 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -41,6 +41,7 @@ public: br_status mk_eq_core(expr * lhs, expr * rhs, expr_ref & result); br_status mk_str_CharAt(expr * arg0, expr * arg1, expr_ref & result); + br_status mk_str_StartsWith(expr * haystack, expr * needle, expr_ref & result); bool reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change); bool reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change); diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 03fde5aeb..c6328d592 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -27,6 +27,7 @@ str_decl_plugin::str_decl_plugin(): m_concat_decl(0), m_length_decl(0), m_charat_decl(0), + m_startswith_decl(0), m_arith_plugin(0), m_arith_fid(0), m_int_sort(0){ @@ -41,6 +42,7 @@ void str_decl_plugin::finalize(void) { DEC_REF(m_concat_decl); DEC_REF(m_length_decl); DEC_REF(m_charat_decl); + DEC_REF(m_startswith_decl); DEC_REF(m_int_sort); } @@ -60,6 +62,8 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_manager->inc_ref(m_int_sort); sort * i = m_int_sort; + sort* boolT = m_manager->mk_bool_sort(); + #define MK_OP(FIELD, NAME, KIND, SORT) \ FIELD = m->mk_func_decl(symbol(NAME), SORT, SORT, SORT, func_decl_info(id, KIND)); \ m->inc_ref(FIELD) @@ -71,6 +75,9 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_charat_decl = m->mk_func_decl(symbol("CharAt"), s, i, s, func_decl_info(id, OP_STR_CHARAT)); m_manager->inc_ref(m_charat_decl); + + m_startswith_decl = m->mk_func_decl(symbol("StartsWith"), s, s, boolT, func_decl_info(id, OP_STR_STARTSWITH)); + m_manager->inc_ref(m_startswith_decl); } decl_plugin * str_decl_plugin::mk_fresh() { @@ -89,6 +96,7 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { case OP_STRCAT: return m_concat_decl; case OP_STRLEN: return m_length_decl; case OP_STR_CHARAT: return m_charat_decl; + case OP_STR_STARTSWITH: return m_startswith_decl; default: return 0; } } @@ -146,6 +154,7 @@ void str_decl_plugin::get_op_names(svector & op_names, symbol cons op_names.push_back(builtin_name("Concat", OP_STRCAT)); op_names.push_back(builtin_name("Length", OP_STRLEN)); op_names.push_back(builtin_name("CharAt", OP_STR_CHARAT)); + op_names.push_back(builtin_name("StartsWith", OP_STR_STARTSWITH)); } void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 049ef08ba..d7bfcf172 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -32,6 +32,7 @@ enum str_op_kind { OP_STRLEN, // higher-level string functions -- these are reduced to basic operations OP_STR_CHARAT, + OP_STR_STARTSWITH, // end LAST_STR_OP }; @@ -43,7 +44,9 @@ protected: func_decl * m_concat_decl; func_decl * m_length_decl; + func_decl * m_charat_decl; + func_decl * m_startswith_decl; arith_decl_plugin * m_arith_plugin; family_id m_arith_fid; From c5ffb012dd3d69c768133221fb391855a9773581 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 14 Jun 2016 16:16:39 -0400 Subject: [PATCH 111/562] axioms for StartsWith; WIP as I need to fix an infinite recursion bug --- src/smt/theory_str.cpp | 53 +++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 4 ++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index fe8f12e81..b87881ea6 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -568,7 +568,7 @@ expr * theory_str::mk_concat(expr * n1, expr * n2) { bool theory_str::can_propagate() { return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty() || !m_concat_axiom_todo.empty() - || !m_axiom_CharAt_todo.empty() + || !m_axiom_CharAt_todo.empty() || !m_axiom_StartsWith_todo.empty() ; } @@ -597,6 +597,10 @@ void theory_str::propagate() { instantiate_axiom_CharAt(m_axiom_CharAt_todo[i]); } m_axiom_CharAt_todo.reset(); + + for (unsigned i = 0; i < m_axiom_StartsWith_todo.size(); ++i) { + instantiate_axiom_StartsWith(m_axiom_StartsWith_todo[i]); + } } } @@ -783,6 +787,39 @@ void theory_str::instantiate_axiom_CharAt(enode * e) { assert_axiom(finalAxiom); } +void theory_str::instantiate_axiom_StartsWith(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + app * expr = e->get_owner(); + + TRACE("t_str_detail", tout << "instantiate StartsWith axiom for " << mk_pp(expr, m) << std::endl;); + + expr_ref ts0(mk_str_var("ts0"), m); + expr_ref ts1(mk_str_var("ts1"), m); + + expr_ref_vector innerItems(m); + innerItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(ts0, ts1))); + innerItems.push_back(ctx.mk_eq_atom(mk_strlen(ts0), mk_strlen(expr->get_arg(1)))); + innerItems.push_back(m.mk_ite(ctx.mk_eq_atom(ts0, expr->get_arg(1)), expr, m.mk_not(expr))); + expr_ref then1(m.mk_and(innerItems.size(), innerItems.c_ptr()), m); + SASSERT(then1); + + // the top-level condition is Length(arg0) >= Length(arg1). + // of course, the integer theory is not so accommodating + expr_ref topLevelCond( + m_autil.mk_ge( + m_autil.mk_add( + mk_strlen(expr->get_arg(0)), m_autil.mk_mul(mk_int(-1), mk_strlen(expr->get_arg(1)))), + mk_int(0)) + , m); + SASSERT(topLevelCond); + + expr_ref finalAxiom(m.mk_ite(topLevelCond, then1, m.mk_not(expr)), m); + + SASSERT(finalAxiom); + assert_axiom(finalAxiom); +} + void theory_str::attach_new_th_var(enode * n) { context & ctx = get_context(); theory_var v = mk_var(n); @@ -3491,6 +3528,7 @@ void theory_str::set_up_axioms(expr * ex) { sort * ex_sort = m.get_sort(ex); sort * str_sort = m.mk_sort(get_family_id(), STRING_SORT); + sort * bool_sort = m.mk_bool_sort(); if (ex_sort == str_sort) { TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << @@ -3526,6 +3564,19 @@ void theory_str::set_up_axioms(expr * ex) { TRACE("t_str_detail", tout << "variable " << mk_ismt2_pp(ap, get_manager()) << " is #" << v << std::endl;); } } + } else if (ex_sort == bool_sort) { + TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << + ": expr is of sort Bool" << std::endl;); + // set up axioms for boolean terms + enode * n = ctx.get_enode(ex); + SASSERT(n); + + if (is_app(ex)) { + app * ap = to_app(ex); + if (is_StartsWith(ap)) { + m_axiom_StartsWith_todo.push_back(n); + } + } } else { TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << ": expr is of wrong sort, ignoring" << std::endl;); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index c86328d30..6c332dbd4 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -108,6 +108,7 @@ namespace smt { // enode lists for term-specific axioms ptr_vector m_axiom_CharAt_todo; + ptr_vector m_axiom_StartsWith_todo; int tmpStringVarCount; int tmpXorVarCount; @@ -172,11 +173,14 @@ namespace smt { bool is_strlen(enode const * n) const { return is_strlen(n->get_owner()); } bool is_CharAt(app const * a) const { return a->is_app_of(get_id(), OP_STR_CHARAT); } bool is_CharAt(enode const * n) const { return is_CharAt(n->get_owner()); } + bool is_StartsWith(app const * a) const { return a->is_app_of(get_id(), OP_STR_STARTSWITH); } + bool is_StartsWith(enode const * n) const { return is_StartsWith(n->get_owner()); } void instantiate_concat_axiom(enode * cat); void instantiate_basic_string_axioms(enode * str); void instantiate_str_eq_length_axiom(enode * lhs, enode * rhs); void instantiate_axiom_CharAt(enode * e); + void instantiate_axiom_StartsWith(enode * e); void set_up_axioms(expr * ex); void handle_equality(expr * lhs, expr * rhs); From 4f131ebba7f3dbd48abbe4c90d9e908aee3e728e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 14 Jun 2016 16:42:46 -0400 Subject: [PATCH 112/562] prevent infinite loop of axiom generation. working StartsWith --- src/smt/theory_str.cpp | 12 ++++++++++++ src/smt/theory_str.h | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index b87881ea6..7bdc9f197 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -601,6 +601,7 @@ void theory_str::propagate() { for (unsigned i = 0; i < m_axiom_StartsWith_todo.size(); ++i) { instantiate_axiom_StartsWith(m_axiom_StartsWith_todo[i]); } + m_axiom_StartsWith_todo.reset(); } } @@ -754,6 +755,11 @@ void theory_str::instantiate_axiom_CharAt(enode * e) { ast_manager & m = get_manager(); app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("t_str_detail", tout << "already set up CharAt axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); TRACE("t_str_detail", tout << "instantiate CharAt axiom for " << mk_pp(expr, m) << std::endl;); @@ -790,7 +796,13 @@ void theory_str::instantiate_axiom_CharAt(enode * e) { void theory_str::instantiate_axiom_StartsWith(enode * e) { context & ctx = get_context(); ast_manager & m = get_manager(); + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("t_str_detail", tout << "already set up StartsWith axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); TRACE("t_str_detail", tout << "instantiate StartsWith axiom for " << mk_pp(expr, m) << std::endl;); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 6c332dbd4..6debaad71 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -110,6 +110,11 @@ namespace smt { ptr_vector m_axiom_CharAt_todo; ptr_vector m_axiom_StartsWith_todo; + // hashtable of all exprs for which we've already set up term-specific axioms -- + // this prevents infinite recursive descent with respect to axioms that + // include an occurrence of the term for which axioms are being generated + obj_hashtable axiomatized_terms; + int tmpStringVarCount; int tmpXorVarCount; int tmpLenTestVarCount; From fd38b4c729c8f03acfb7362c4c5edd89f7f4a7a7 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 14 Jun 2016 17:55:46 -0400 Subject: [PATCH 113/562] EndsWith decl and rewriter, WIP --- src/ast/rewriter/str_rewriter.cpp | 25 +++++++++++++++++++ src/ast/rewriter/str_rewriter.h | 1 + src/ast/str_decl_plugin.cpp | 7 ++++++ src/ast/str_decl_plugin.h | 2 ++ src/smt/theory_str.cpp | 40 ++++++++++++++++++++++++++++++- src/smt/theory_str.h | 5 ++++ 6 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index 4b7ff9057..d6419ba4f 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -69,6 +69,28 @@ br_status str_rewriter::mk_str_StartsWith(expr * haystack, expr * needle, expr_r } } +br_status str_rewriter::mk_str_EndsWith(expr * haystack, expr * needle, expr_ref & result) { + TRACE("t_str_rw", tout << "rewrite (EndsWith " << mk_pp(haystack, m()) << " " << mk_pp(needle, m()) << ")" << std::endl;); + if (m_strutil.is_string(haystack) && m_strutil.is_string(needle)) { + TRACE("t_str_rw", tout << "evaluating constant EndsWith predicate" << std::endl;); + std::string haystackStr = m_strutil.get_string_constant_value(haystack); + std::string needleStr = m_strutil.get_string_constant_value(needle); + if (haystackStr.length() < needleStr.length()) { + result = m().mk_false(); + return BR_DONE; + } else { + if (haystackStr.substr(haystackStr.length() - needleStr.length(), needleStr.length()) == needleStr) { + result = m().mk_true(); + } else { + result = m().mk_false(); + } + return BR_DONE; + } + } else { + return BR_FAILED; + } +} + br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) { SASSERT(f->get_family_id() == get_fid()); @@ -82,6 +104,9 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con case OP_STR_STARTSWITH: SASSERT(num_args == 2); return mk_str_StartsWith(args[0], args[1], result); + case OP_STR_ENDSWITH: + SASSERT(num_args == 2); + return mk_str_EndsWith(args[0], args[1], result); default: return BR_FAILED; } diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index 8f12a75db..b179934c7 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -42,6 +42,7 @@ public: br_status mk_str_CharAt(expr * arg0, expr * arg1, expr_ref & result); br_status mk_str_StartsWith(expr * haystack, expr * needle, expr_ref & result); + br_status mk_str_EndsWith(expr * haystack, expr * needle, expr_ref & result); bool reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change); bool reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change); diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index c6328d592..6453cb244 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -28,6 +28,7 @@ str_decl_plugin::str_decl_plugin(): m_length_decl(0), m_charat_decl(0), m_startswith_decl(0), + m_endswith_decl(0), m_arith_plugin(0), m_arith_fid(0), m_int_sort(0){ @@ -43,6 +44,7 @@ void str_decl_plugin::finalize(void) { DEC_REF(m_length_decl); DEC_REF(m_charat_decl); DEC_REF(m_startswith_decl); + DEC_REF(m_endswith_decl); DEC_REF(m_int_sort); } @@ -78,6 +80,9 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_startswith_decl = m->mk_func_decl(symbol("StartsWith"), s, s, boolT, func_decl_info(id, OP_STR_STARTSWITH)); m_manager->inc_ref(m_startswith_decl); + + m_endswith_decl = m->mk_func_decl(symbol("EndsWith"), s, s, boolT, func_decl_info(id, OP_STR_ENDSWITH)); + m_manager->inc_ref(m_endswith_decl); } decl_plugin * str_decl_plugin::mk_fresh() { @@ -97,6 +102,7 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { case OP_STRLEN: return m_length_decl; case OP_STR_CHARAT: return m_charat_decl; case OP_STR_STARTSWITH: return m_startswith_decl; + case OP_STR_ENDSWITH: return m_endswith_decl; default: return 0; } } @@ -155,6 +161,7 @@ void str_decl_plugin::get_op_names(svector & op_names, symbol cons op_names.push_back(builtin_name("Length", OP_STRLEN)); op_names.push_back(builtin_name("CharAt", OP_STR_CHARAT)); op_names.push_back(builtin_name("StartsWith", OP_STR_STARTSWITH)); + op_names.push_back(builtin_name("EndsWith", OP_STR_ENDSWITH)); } void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index d7bfcf172..4ce258c60 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -33,6 +33,7 @@ enum str_op_kind { // higher-level string functions -- these are reduced to basic operations OP_STR_CHARAT, OP_STR_STARTSWITH, + OP_STR_ENDSWITH, // end LAST_STR_OP }; @@ -47,6 +48,7 @@ protected: func_decl * m_charat_decl; func_decl * m_startswith_decl; + func_decl * m_endswith_decl; arith_decl_plugin * m_arith_plugin; family_id m_arith_fid; diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 7bdc9f197..76835c560 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -568,7 +568,7 @@ expr * theory_str::mk_concat(expr * n1, expr * n2) { bool theory_str::can_propagate() { return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty() || !m_concat_axiom_todo.empty() - || !m_axiom_CharAt_todo.empty() || !m_axiom_StartsWith_todo.empty() + || !m_axiom_CharAt_todo.empty() || !m_axiom_StartsWith_todo.empty() || !m_axiom_EndsWith_todo.empty() ; } @@ -602,6 +602,11 @@ void theory_str::propagate() { instantiate_axiom_StartsWith(m_axiom_StartsWith_todo[i]); } m_axiom_StartsWith_todo.reset(); + + for (unsigned i = 0; i < m_axiom_EndsWith_todo.size(); ++i) { + instantiate_axiom_EndsWith(m_axiom_EndsWith_todo[i]); + } + m_axiom_EndsWith_todo.reset(); } } @@ -832,6 +837,37 @@ void theory_str::instantiate_axiom_StartsWith(enode * e) { assert_axiom(finalAxiom); } +void theory_str::instantiate_axiom_EndsWith(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("t_str_detail", tout << "already set up EndsWith axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); + + TRACE("t_str_detail", tout << "instantiate EndsWith axiom for " << mk_pp(expr, m) << std::endl;); + + // TODO NEXT + NOT_IMPLEMENTED_YET(); + /* + Z3_ast resBoolVar = my_mk_internal_bool_var(t); + Z3_ast ts0 = my_mk_internal_string_var(t); + Z3_ast ts1 = my_mk_internal_string_var(t); + // boolVar = endswith(arg[0], arg[1]) + // -------------------------------------------- + std::vector innerItems; + innerItems.push_back( Z3_mk_eq(ctx, args[0], mk_concat(t, ts0, ts1)) ); + innerItems.push_back( Z3_mk_eq(ctx, mk_length(t, ts1), mk_length(t, args[1])) ); + innerItems.push_back( Z3_mk_ite(ctx, Z3_mk_eq(ctx, ts1, args[1]), Z3_mk_eq(ctx, resBoolVar, Z3_mk_true(ctx)), Z3_mk_eq(ctx, resBoolVar, Z3_mk_false(ctx) ) ) ); + Z3_ast then1 = mk_and_fromVector(t, innerItems); + breakdownAssert = Z3_mk_ite(ctx, Z3_mk_ge(ctx, mk_length(t, args[0]), mk_length(t, args[1])), then1, Z3_mk_eq(ctx, resBoolVar, Z3_mk_false(ctx) ) ); + reduceAst = resBoolVar; + */ +} + void theory_str::attach_new_th_var(enode * n) { context & ctx = get_context(); theory_var v = mk_var(n); @@ -3587,6 +3623,8 @@ void theory_str::set_up_axioms(expr * ex) { app * ap = to_app(ex); if (is_StartsWith(ap)) { m_axiom_StartsWith_todo.push_back(n); + } else if (is_EndsWith(ap)) { + m_axiom_EndsWith_todo.push_back(n); } } } else { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 6debaad71..ae3cc5d52 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -109,6 +109,7 @@ namespace smt { // enode lists for term-specific axioms ptr_vector m_axiom_CharAt_todo; ptr_vector m_axiom_StartsWith_todo; + ptr_vector m_axiom_EndsWith_todo; // hashtable of all exprs for which we've already set up term-specific axioms -- // this prevents infinite recursive descent with respect to axioms that @@ -180,12 +181,16 @@ namespace smt { bool is_CharAt(enode const * n) const { return is_CharAt(n->get_owner()); } bool is_StartsWith(app const * a) const { return a->is_app_of(get_id(), OP_STR_STARTSWITH); } bool is_StartsWith(enode const * n) const { return is_StartsWith(n->get_owner()); } + bool is_EndsWith(app const * a) const { return a->is_app_of(get_id(), OP_STR_ENDSWITH); } + bool is_EndsWith(enode const * n) const { return is_EndsWith(n->get_owner()); } + void instantiate_concat_axiom(enode * cat); void instantiate_basic_string_axioms(enode * str); void instantiate_str_eq_length_axiom(enode * lhs, enode * rhs); void instantiate_axiom_CharAt(enode * e); void instantiate_axiom_StartsWith(enode * e); + void instantiate_axiom_EndsWith(enode * e); void set_up_axioms(expr * ex); void handle_equality(expr * lhs, expr * rhs); From 989d6b577b457931574a0a9e376523f5869f2b88 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 14 Jun 2016 18:05:24 -0400 Subject: [PATCH 114/562] EndsWith axiomatization in theory_str --- src/smt/theory_str.cpp | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 76835c560..508f451a3 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -832,7 +832,6 @@ void theory_str::instantiate_axiom_StartsWith(enode * e) { SASSERT(topLevelCond); expr_ref finalAxiom(m.mk_ite(topLevelCond, then1, m.mk_not(expr)), m); - SASSERT(finalAxiom); assert_axiom(finalAxiom); } @@ -850,22 +849,28 @@ void theory_str::instantiate_axiom_EndsWith(enode * e) { TRACE("t_str_detail", tout << "instantiate EndsWith axiom for " << mk_pp(expr, m) << std::endl;); - // TODO NEXT - NOT_IMPLEMENTED_YET(); - /* - Z3_ast resBoolVar = my_mk_internal_bool_var(t); - Z3_ast ts0 = my_mk_internal_string_var(t); - Z3_ast ts1 = my_mk_internal_string_var(t); - // boolVar = endswith(arg[0], arg[1]) - // -------------------------------------------- - std::vector innerItems; - innerItems.push_back( Z3_mk_eq(ctx, args[0], mk_concat(t, ts0, ts1)) ); - innerItems.push_back( Z3_mk_eq(ctx, mk_length(t, ts1), mk_length(t, args[1])) ); - innerItems.push_back( Z3_mk_ite(ctx, Z3_mk_eq(ctx, ts1, args[1]), Z3_mk_eq(ctx, resBoolVar, Z3_mk_true(ctx)), Z3_mk_eq(ctx, resBoolVar, Z3_mk_false(ctx) ) ) ); - Z3_ast then1 = mk_and_fromVector(t, innerItems); - breakdownAssert = Z3_mk_ite(ctx, Z3_mk_ge(ctx, mk_length(t, args[0]), mk_length(t, args[1])), then1, Z3_mk_eq(ctx, resBoolVar, Z3_mk_false(ctx) ) ); - reduceAst = resBoolVar; - */ + expr_ref ts0(mk_str_var("ts0"), m); + expr_ref ts1(mk_str_var("ts1"), m); + + expr_ref_vector innerItems(m); + innerItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(ts0, ts1))); + innerItems.push_back(ctx.mk_eq_atom(mk_strlen(ts1), mk_strlen(expr->get_arg(1)))); + innerItems.push_back(m.mk_ite(ctx.mk_eq_atom(ts1, expr->get_arg(1)), expr, m.mk_not(expr))); + expr_ref then1(m.mk_and(innerItems.size(), innerItems.c_ptr()), m); + SASSERT(then1); + + // the top-level condition is Length(arg0) >= Length(arg1) + expr_ref topLevelCond( + m_autil.mk_ge( + m_autil.mk_add( + mk_strlen(expr->get_arg(0)), m_autil.mk_mul(mk_int(-1), mk_strlen(expr->get_arg(1)))), + mk_int(0)) + , m); + SASSERT(topLevelCond); + + expr_ref finalAxiom(m.mk_ite(topLevelCond, then1, m.mk_not(expr)), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); } void theory_str::attach_new_th_var(enode * n) { From a3986d6d0e0ad90a62652b92f131dddb30115999 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 14 Jun 2016 18:36:43 -0400 Subject: [PATCH 115/562] decl and rewriter support for Contains (WIP) --- src/ast/rewriter/str_rewriter.cpp | 24 ++++++++++++++++++++++++ src/ast/rewriter/str_rewriter.h | 1 + src/ast/str_decl_plugin.cpp | 7 +++++++ src/ast/str_decl_plugin.h | 2 ++ 4 files changed, 34 insertions(+) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index d6419ba4f..d33194748 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -91,6 +91,27 @@ br_status str_rewriter::mk_str_EndsWith(expr * haystack, expr * needle, expr_ref } } +br_status str_rewriter::mk_str_Contains(expr * haystack, expr * needle, expr_ref & result) { + TRACE("t_str_rw", tout << "rewrite (Contains " << mk_pp(haystack, m()) << " " << mk_pp(needle, m()) << ")" << std::endl;); + if (haystack == needle) { + TRACE("t_str_rw", tout << "eliminate (Contains) over identical terms" << std::endl;); + result = m().mk_true(); + return BR_DONE; + } else if (m_strutil.is_string(haystack) && m_strutil.is_string(needle)) { + TRACE("t_str_rw", tout << "evaluating constant Contains predicate" << std::endl;); + std::string haystackStr = m_strutil.get_string_constant_value(haystack); + std::string needleStr = m_strutil.get_string_constant_value(needle); + if (haystackStr.find(needleStr) != std::string::npos) { + result = m().mk_true(); + } else { + result = m().mk_false(); + } + return BR_DONE; + } else { + return BR_FAILED; + } +} + br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) { SASSERT(f->get_family_id() == get_fid()); @@ -107,6 +128,9 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con case OP_STR_ENDSWITH: SASSERT(num_args == 2); return mk_str_EndsWith(args[0], args[1], result); + case OP_STR_CONTAINS: + SASSERT(num_args == 2); + return mk_str_Contains(args[0], args[1], result); default: return BR_FAILED; } diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index b179934c7..f98f64cc4 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -43,6 +43,7 @@ public: br_status mk_str_CharAt(expr * arg0, expr * arg1, expr_ref & result); br_status mk_str_StartsWith(expr * haystack, expr * needle, expr_ref & result); br_status mk_str_EndsWith(expr * haystack, expr * needle, expr_ref & result); + br_status mk_str_Contains(expr * haystack, expr * needle, expr_ref & result); bool reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change); bool reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change); diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 6453cb244..07e0d07a2 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -29,6 +29,7 @@ str_decl_plugin::str_decl_plugin(): m_charat_decl(0), m_startswith_decl(0), m_endswith_decl(0), + m_contains_decl(0), m_arith_plugin(0), m_arith_fid(0), m_int_sort(0){ @@ -45,6 +46,7 @@ void str_decl_plugin::finalize(void) { DEC_REF(m_charat_decl); DEC_REF(m_startswith_decl); DEC_REF(m_endswith_decl); + DEC_REF(m_contains_decl); DEC_REF(m_int_sort); } @@ -83,6 +85,9 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_endswith_decl = m->mk_func_decl(symbol("EndsWith"), s, s, boolT, func_decl_info(id, OP_STR_ENDSWITH)); m_manager->inc_ref(m_endswith_decl); + + m_contains_decl = m->mk_func_decl(symbol("Contains"), s, s, boolT, func_decl_info(id, OP_STR_CONTAINS)); + m_manager->inc_ref(m_contains_decl); } decl_plugin * str_decl_plugin::mk_fresh() { @@ -103,6 +108,7 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { case OP_STR_CHARAT: return m_charat_decl; case OP_STR_STARTSWITH: return m_startswith_decl; case OP_STR_ENDSWITH: return m_endswith_decl; + case OP_STR_CONTAINS: return m_contains_decl; default: return 0; } } @@ -162,6 +168,7 @@ void str_decl_plugin::get_op_names(svector & op_names, symbol cons op_names.push_back(builtin_name("CharAt", OP_STR_CHARAT)); op_names.push_back(builtin_name("StartsWith", OP_STR_STARTSWITH)); op_names.push_back(builtin_name("EndsWith", OP_STR_ENDSWITH)); + op_names.push_back(builtin_name("Contains", OP_STR_CONTAINS)); } void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 4ce258c60..c4605003d 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -34,6 +34,7 @@ enum str_op_kind { OP_STR_CHARAT, OP_STR_STARTSWITH, OP_STR_ENDSWITH, + OP_STR_CONTAINS, // end LAST_STR_OP }; @@ -49,6 +50,7 @@ protected: func_decl * m_charat_decl; func_decl * m_startswith_decl; func_decl * m_endswith_decl; + func_decl * m_contains_decl; arith_decl_plugin * m_arith_plugin; family_id m_arith_fid; From 7aeeb599ef6ed05ab8eb0f06fc4fb279585c981e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 14 Jun 2016 18:43:51 -0400 Subject: [PATCH 116/562] very very basic Contains support in theory_str not included: the 1200 lines of code that make it very fast --- src/smt/theory_str.cpp | 29 +++++++++++++++++++++++++++++ src/smt/theory_str.h | 4 ++++ 2 files changed, 33 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 508f451a3..faaba596f 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -569,6 +569,7 @@ expr * theory_str::mk_concat(expr * n1, expr * n2) { bool theory_str::can_propagate() { return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty() || !m_concat_axiom_todo.empty() || !m_axiom_CharAt_todo.empty() || !m_axiom_StartsWith_todo.empty() || !m_axiom_EndsWith_todo.empty() + || !m_axiom_Contains_todo.empty() ; } @@ -607,6 +608,11 @@ void theory_str::propagate() { instantiate_axiom_EndsWith(m_axiom_EndsWith_todo[i]); } m_axiom_EndsWith_todo.reset(); + + for (unsigned i = 0; i < m_axiom_Contains_todo.size(); ++i) { + instantiate_axiom_Contains(m_axiom_Contains_todo[i]); + } + m_axiom_Contains_todo.reset(); } } @@ -873,6 +879,27 @@ void theory_str::instantiate_axiom_EndsWith(enode * e) { assert_axiom(finalAxiom); } +void theory_str::instantiate_axiom_Contains(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("t_str_detail", tout << "already set up Contains axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); + + TRACE("t_str_detail", tout << "instantiate Contains axiom for " << mk_pp(expr, m) << std::endl;); + + expr_ref ts0(mk_str_var("ts0"), m); + expr_ref ts1(mk_str_var("ts1"), m); + // TODO NEXT registerContain(expr); + expr_ref breakdownAssert(ctx.mk_eq_atom(expr, ctx.mk_eq_atom(expr->get_arg(0), mk_concat(ts0, mk_concat(expr->get_arg(1), ts1)))), m); + SASSERT(breakdownAssert); + assert_axiom(breakdownAssert); +} + void theory_str::attach_new_th_var(enode * n) { context & ctx = get_context(); theory_var v = mk_var(n); @@ -3630,6 +3657,8 @@ void theory_str::set_up_axioms(expr * ex) { m_axiom_StartsWith_todo.push_back(n); } else if (is_EndsWith(ap)) { m_axiom_EndsWith_todo.push_back(n); + } else if (is_Contains(ap)) { + m_axiom_Contains_todo.push_back(n); } } } else { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index ae3cc5d52..6d1bd597f 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -110,6 +110,7 @@ namespace smt { ptr_vector m_axiom_CharAt_todo; ptr_vector m_axiom_StartsWith_todo; ptr_vector m_axiom_EndsWith_todo; + ptr_vector m_axiom_Contains_todo; // hashtable of all exprs for which we've already set up term-specific axioms -- // this prevents infinite recursive descent with respect to axioms that @@ -183,6 +184,8 @@ namespace smt { bool is_StartsWith(enode const * n) const { return is_StartsWith(n->get_owner()); } bool is_EndsWith(app const * a) const { return a->is_app_of(get_id(), OP_STR_ENDSWITH); } bool is_EndsWith(enode const * n) const { return is_EndsWith(n->get_owner()); } + bool is_Contains(app const * a) const { return a->is_app_of(get_id(), OP_STR_CONTAINS); } + bool is_Contains(enode const * n) const { return is_Contains(n->get_owner()); } void instantiate_concat_axiom(enode * cat); void instantiate_basic_string_axioms(enode * str); @@ -191,6 +194,7 @@ namespace smt { void instantiate_axiom_CharAt(enode * e); void instantiate_axiom_StartsWith(enode * e); void instantiate_axiom_EndsWith(enode * e); + void instantiate_axiom_Contains(enode * e); void set_up_axioms(expr * ex); void handle_equality(expr * lhs, expr * rhs); From db2a5854e9e21dffe51477a2d27f9711a2a85380 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 14 Jun 2016 20:10:06 -0400 Subject: [PATCH 117/562] decl and rewriter for Indexof (WIP) --- src/ast/rewriter/str_rewriter.cpp | 21 +++++++++++++++++++++ src/ast/rewriter/str_rewriter.h | 1 + src/ast/str_decl_plugin.cpp | 7 +++++++ src/ast/str_decl_plugin.h | 2 ++ 4 files changed, 31 insertions(+) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index d33194748..5e61ee3a2 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -112,6 +112,24 @@ br_status str_rewriter::mk_str_Contains(expr * haystack, expr * needle, expr_ref } } +br_status str_rewriter::mk_str_Indexof(expr * haystack, expr * needle, expr_ref & result) { + TRACE("t_str_rw", tout << "rewrite (Indexof " << mk_pp(haystack, m()) << " " << mk_pp(needle, m()) << ")" << std::endl;); + if (m_strutil.is_string(haystack) && m_strutil.is_string(needle)) { + TRACE("t_str_rw", tout << "evaluating constant Indexof expression" << std::endl;); + std::string haystackStr = m_strutil.get_string_constant_value(haystack); + std::string needleStr = m_strutil.get_string_constant_value(needle); + if (haystackStr.find(needleStr) != std::string::npos) { + int index = haystackStr.find(needleStr); + result = m_autil.mk_numeral(rational(index), true); + } else { + result = m_autil.mk_numeral(rational(-1), true); + } + return BR_DONE; + } else { + return BR_FAILED; + } +} + br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) { SASSERT(f->get_family_id() == get_fid()); @@ -131,6 +149,9 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con case OP_STR_CONTAINS: SASSERT(num_args == 2); return mk_str_Contains(args[0], args[1], result); + case OP_STR_INDEXOF: + SASSERT(num_args == 2); + return mk_str_Indexof(args[0], args[1], result); default: return BR_FAILED; } diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index f98f64cc4..f22ac31a7 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -44,6 +44,7 @@ public: br_status mk_str_StartsWith(expr * haystack, expr * needle, expr_ref & result); br_status mk_str_EndsWith(expr * haystack, expr * needle, expr_ref & result); br_status mk_str_Contains(expr * haystack, expr * needle, expr_ref & result); + br_status mk_str_Indexof(expr * haystack, expr * needle, expr_ref & result); bool reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change); bool reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change); diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 07e0d07a2..ea4b0c6d0 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -30,6 +30,7 @@ str_decl_plugin::str_decl_plugin(): m_startswith_decl(0), m_endswith_decl(0), m_contains_decl(0), + m_indexof_decl(0), m_arith_plugin(0), m_arith_fid(0), m_int_sort(0){ @@ -47,6 +48,7 @@ void str_decl_plugin::finalize(void) { DEC_REF(m_startswith_decl); DEC_REF(m_endswith_decl); DEC_REF(m_contains_decl); + DEC_REF(m_indexof_decl); DEC_REF(m_int_sort); } @@ -88,6 +90,9 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_contains_decl = m->mk_func_decl(symbol("Contains"), s, s, boolT, func_decl_info(id, OP_STR_CONTAINS)); m_manager->inc_ref(m_contains_decl); + + m_indexof_decl = m->mk_func_decl(symbol("Indexof"), s, s, i, func_decl_info(id, OP_STR_INDEXOF)); + m_manager->inc_ref(m_indexof_decl); } decl_plugin * str_decl_plugin::mk_fresh() { @@ -109,6 +114,7 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { case OP_STR_STARTSWITH: return m_startswith_decl; case OP_STR_ENDSWITH: return m_endswith_decl; case OP_STR_CONTAINS: return m_contains_decl; + case OP_STR_INDEXOF: return m_indexof_decl; default: return 0; } } @@ -169,6 +175,7 @@ void str_decl_plugin::get_op_names(svector & op_names, symbol cons op_names.push_back(builtin_name("StartsWith", OP_STR_STARTSWITH)); op_names.push_back(builtin_name("EndsWith", OP_STR_ENDSWITH)); op_names.push_back(builtin_name("Contains", OP_STR_CONTAINS)); + op_names.push_back(builtin_name("Indexof", OP_STR_INDEXOF)); } void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index c4605003d..a2a355ba2 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -35,6 +35,7 @@ enum str_op_kind { OP_STR_STARTSWITH, OP_STR_ENDSWITH, OP_STR_CONTAINS, + OP_STR_INDEXOF, // end LAST_STR_OP }; @@ -51,6 +52,7 @@ protected: func_decl * m_startswith_decl; func_decl * m_endswith_decl; func_decl * m_contains_decl; + func_decl * m_indexof_decl; arith_decl_plugin * m_arith_plugin; family_id m_arith_fid; From 881e3056f3a039ec0551b29ed4065c607de54fdc Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 14 Jun 2016 21:28:31 -0400 Subject: [PATCH 118/562] support for IndexOf in theory_str --- src/smt/theory_str.cpp | 113 ++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 6 +++ 2 files changed, 117 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index faaba596f..df77018e9 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -430,6 +430,30 @@ app * theory_str::mk_internal_xor_var() { return a; } +app * theory_str::mk_int_var(std::string name) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + TRACE("t_str_detail", tout << "creating integer variable " << name << " at scope level " << sLevel << std::endl;); + + sort * int_sort = m.mk_sort(m_autil.get_family_id(), INT_SORT); + app * a = m.mk_fresh_const(name.c_str(), int_sort); + + ctx.internalize(a, false); + SASSERT(ctx.get_enode(a) != NULL); + SASSERT(ctx.e_internalized(a)); + ctx.mark_as_relevant(a); + // I'm assuming that this combination will do the correct thing in the integer theory. + + //mk_var(ctx.get_enode(a)); + m_trail.push_back(a); + //variable_set.insert(a); + //internal_variable_set.insert(a); + //track_variable_scope(a); + + return a; +} + app * theory_str::mk_str_var(std::string name) { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -498,6 +522,15 @@ app * theory_str::mk_nonempty_str_var() { return a; } +app * theory_str::mk_contains(expr * haystack, expr * needle) { + expr * args[2] = {haystack, needle}; + app * contains = get_manager().mk_app(get_id(), OP_STR_CONTAINS, 0, 0, 2, args); + // immediately force internalization so that axiom setup does not fail + get_context().internalize(contains, false); + set_up_axioms(contains); + return contains; +} + app * theory_str::mk_strlen(expr * e) { /*if (m_strutil.is_string(e)) {*/ if (false) { const char * strval = 0; @@ -569,7 +602,7 @@ expr * theory_str::mk_concat(expr * n1, expr * n2) { bool theory_str::can_propagate() { return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty() || !m_concat_axiom_todo.empty() || !m_axiom_CharAt_todo.empty() || !m_axiom_StartsWith_todo.empty() || !m_axiom_EndsWith_todo.empty() - || !m_axiom_Contains_todo.empty() + || !m_axiom_Contains_todo.empty() || !m_axiom_Indexof_todo.empty() ; } @@ -613,6 +646,11 @@ void theory_str::propagate() { instantiate_axiom_Contains(m_axiom_Contains_todo[i]); } m_axiom_Contains_todo.reset(); + + for (unsigned i = 0; i < m_axiom_Indexof_todo.size(); ++i) { + instantiate_axiom_Indexof(m_axiom_Indexof_todo[i]); + } + m_axiom_Indexof_todo.reset(); } } @@ -900,6 +938,62 @@ void theory_str::instantiate_axiom_Contains(enode * e) { assert_axiom(breakdownAssert); } +void theory_str::instantiate_axiom_Indexof(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("t_str_detail", tout << "already set up Indexof axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); + + TRACE("t_str_detail", tout << "instantiate Indexof axiom for " << mk_pp(expr, m) << std::endl;); + + expr_ref x1(mk_str_var("x1"), m); + expr_ref x2(mk_str_var("x2"), m); + expr_ref indexAst(mk_int_var("index"), m); + + expr_ref condAst(mk_contains(expr->get_arg(0), expr->get_arg(1)), m); + SASSERT(condAst); + + // ----------------------- + // true branch + expr_ref_vector thenItems(m); + // args[0] = x1 . args[1] . x2 + thenItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x1, mk_concat(expr->get_arg(1), x2)))); + // indexAst = |x1| + thenItems.push_back(ctx.mk_eq_atom(indexAst, mk_strlen(x1))); + // args[0] = x3 . x4 + // /\ |x3| = |x1| + |args[1]| - 1 + // /\ ! contains(x3, args[1]) + expr_ref x3(mk_str_var("x3"), m); + expr_ref x4(mk_str_var("x4"), m); + expr_ref tmpLen(m_autil.mk_add(indexAst, mk_strlen(expr->get_arg(1)), mk_int(-1)), m); + SASSERT(tmpLen); + thenItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x3, x4))); + thenItems.push_back(ctx.mk_eq_atom(mk_strlen(x3), tmpLen)); + thenItems.push_back(m.mk_not(mk_contains(x3, expr->get_arg(1)))); + expr_ref thenBranch(m.mk_and(thenItems.size(), thenItems.c_ptr()), m); + SASSERT(thenBranch); + + // ----------------------- + // false branch + expr_ref elseBranch(ctx.mk_eq_atom(indexAst, mk_int(-1)), m); + SASSERT(elseBranch); + + expr_ref breakdownAssert(m.mk_ite(condAst, thenBranch, elseBranch), m); + SASSERT(breakdownAssert); + + expr_ref reduceToIndex(ctx.mk_eq_atom(expr, indexAst), m); + SASSERT(reduceToIndex); + + expr_ref finalAxiom(m.mk_and(breakdownAssert, reduceToIndex), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); +} + void theory_str::attach_new_th_var(enode * n) { context & ctx = get_context(); theory_var v = mk_var(n); @@ -3602,7 +3696,6 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { } void theory_str::set_up_axioms(expr * ex) { - // TODO check to make sure we don't set up axioms on the same term twice ast_manager & m = get_manager(); context & ctx = get_context(); @@ -3610,6 +3703,9 @@ void theory_str::set_up_axioms(expr * ex) { sort * str_sort = m.mk_sort(get_family_id(), STRING_SORT); sort * bool_sort = m.mk_bool_sort(); + family_id m_arith_fid = m.mk_family_id("arith"); + sort * int_sort = m.mk_sort(m_arith_fid, INT_SORT); + if (ex_sort == str_sort) { TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << ": expr is of sort String" << std::endl;); @@ -3661,6 +3757,19 @@ void theory_str::set_up_axioms(expr * ex) { m_axiom_Contains_todo.push_back(n); } } + } else if (ex_sort == int_sort) { + TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << + ": expr is of sort Int" << std::endl;); + // set up axioms for boolean terms + enode * n = ctx.get_enode(ex); + SASSERT(n); + + if (is_app(ex)) { + app * ap = to_app(ex); + if (is_Indexof(ap)) { + m_axiom_Indexof_todo.push_back(n); + } + } } else { TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << ": expr is of wrong sort, ignoring" << std::endl;); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 6d1bd597f..bf0fef38b 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -111,6 +111,7 @@ namespace smt { ptr_vector m_axiom_StartsWith_todo; ptr_vector m_axiom_EndsWith_todo; ptr_vector m_axiom_Contains_todo; + ptr_vector m_axiom_Indexof_todo; // hashtable of all exprs for which we've already set up term-specific axioms -- // this prevents infinite recursive descent with respect to axioms that @@ -156,6 +157,7 @@ namespace smt { app * mk_strlen(expr * e); expr * mk_concat(expr * n1, expr * n2); expr * mk_concat_const_str(expr * n1, expr * n2); + app * mk_contains(expr * haystack, expr * needle); literal mk_literal(expr* _e); app * mk_int(int n); @@ -168,6 +170,7 @@ namespace smt { void track_variable_scope(expr * var); app * mk_str_var(std::string name); + app * mk_int_var(std::string name); app * mk_nonempty_str_var(); app * mk_internal_xor_var(); expr * mk_internal_valTest_var(expr * node, int len, int vTries); @@ -186,6 +189,8 @@ namespace smt { bool is_EndsWith(enode const * n) const { return is_EndsWith(n->get_owner()); } bool is_Contains(app const * a) const { return a->is_app_of(get_id(), OP_STR_CONTAINS); } bool is_Contains(enode const * n) const { return is_Contains(n->get_owner()); } + bool is_Indexof(app const * a) const { return a->is_app_of(get_id(), OP_STR_INDEXOF); } + bool is_Indexof(enode const * n) const { return is_Indexof(n->get_owner()); } void instantiate_concat_axiom(enode * cat); void instantiate_basic_string_axioms(enode * str); @@ -195,6 +200,7 @@ namespace smt { void instantiate_axiom_StartsWith(enode * e); void instantiate_axiom_EndsWith(enode * e); void instantiate_axiom_Contains(enode * e); + void instantiate_axiom_Indexof(enode * e); void set_up_axioms(expr * ex); void handle_equality(expr * lhs, expr * rhs); From dc5a334d429e1e2c16ac00a9cdc11c2f1e60a236 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 15 Jun 2016 17:37:17 -0400 Subject: [PATCH 119/562] support for Indexof2 in theory_str --- src/ast/rewriter/str_rewriter.cpp | 31 ++++++++++++ src/ast/rewriter/str_rewriter.h | 1 + src/ast/str_decl_plugin.cpp | 10 ++++ src/ast/str_decl_plugin.h | 2 + src/smt/theory_str.cpp | 84 ++++++++++++++++++++++++++++++- src/smt/theory_str.h | 6 +++ 6 files changed, 133 insertions(+), 1 deletion(-) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index 5e61ee3a2..30dcb1d95 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -130,6 +130,34 @@ br_status str_rewriter::mk_str_Indexof(expr * haystack, expr * needle, expr_ref } } +br_status str_rewriter::mk_str_Indexof2(expr * arg0, expr * arg1, expr * arg2, expr_ref & result) { + TRACE("t_str_rw", tout << "rewrite (Indexof2 " << mk_pp(arg0, m()) << " " << mk_pp(arg1, m()) << " " << mk_pp(arg2, m()) << ")" << std::endl;); + //if (getNodeType(t, args[0]) == my_Z3_ConstStr && getNodeType(t, args[1]) == my_Z3_ConstStr && getNodeType(t, args[2]) == my_Z3_Num) { + rational arg2Int; + if (m_strutil.is_string(arg0) && m_strutil.is_string(arg1) && m_autil.is_numeral(arg2, arg2Int)) { + TRACE("t_str_rw", tout << "evaluating constant Indexof2 expression" << std::endl;); + std::string arg0str = m_strutil.get_string_constant_value(arg0); + std::string arg1str = m_strutil.get_string_constant_value(arg1); + if (arg2Int >= rational((unsigned)arg0str.length())) { + result = m_autil.mk_numeral(rational(-1), true); + } else if (arg2Int < rational(0)) { + int index = arg0str.find(arg1str); + result = m_autil.mk_numeral(rational(index), true); + } else { + std::string suffixStr = arg0str.substr(arg2Int.get_unsigned(), arg0str.length() - arg2Int.get_unsigned()); + if (suffixStr.find(arg1str) != std::string::npos) { + int index = suffixStr.find(arg1str) + arg2Int.get_unsigned(); + result = m_autil.mk_numeral(rational(index), true); + } else { + result = m_autil.mk_numeral(rational(-1), true); + } + } + return BR_DONE; + } else { + return BR_FAILED; + } +} + br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) { SASSERT(f->get_family_id() == get_fid()); @@ -152,6 +180,9 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con case OP_STR_INDEXOF: SASSERT(num_args == 2); return mk_str_Indexof(args[0], args[1], result); + case OP_STR_INDEXOF2: + SASSERT(num_args == 3); + return mk_str_Indexof2(args[0], args[1], args[2], result); default: return BR_FAILED; } diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index f22ac31a7..c0bae2881 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -45,6 +45,7 @@ public: br_status mk_str_EndsWith(expr * haystack, expr * needle, expr_ref & result); br_status mk_str_Contains(expr * haystack, expr * needle, expr_ref & result); br_status mk_str_Indexof(expr * haystack, expr * needle, expr_ref & result); + br_status mk_str_Indexof2(expr * arg0, expr * arg1, expr * arg2, expr_ref & result); bool reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change); bool reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change); diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index ea4b0c6d0..f6e458fbd 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -31,6 +31,7 @@ str_decl_plugin::str_decl_plugin(): m_endswith_decl(0), m_contains_decl(0), m_indexof_decl(0), + m_indexof2_decl(0), m_arith_plugin(0), m_arith_fid(0), m_int_sort(0){ @@ -49,6 +50,7 @@ void str_decl_plugin::finalize(void) { DEC_REF(m_endswith_decl); DEC_REF(m_contains_decl); DEC_REF(m_indexof_decl); + DEC_REF(m_indexof2_decl); DEC_REF(m_int_sort); } @@ -93,6 +95,12 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_indexof_decl = m->mk_func_decl(symbol("Indexof"), s, s, i, func_decl_info(id, OP_STR_INDEXOF)); m_manager->inc_ref(m_indexof_decl); + + { + sort * d[3] = { s, s, i }; + m_indexof2_decl = m->mk_func_decl(symbol("Indexof2"), 3, d, i, func_decl_info(id, OP_STR_INDEXOF2)); + m_manager->inc_ref(m_indexof2_decl); + } } decl_plugin * str_decl_plugin::mk_fresh() { @@ -115,6 +123,7 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { case OP_STR_ENDSWITH: return m_endswith_decl; case OP_STR_CONTAINS: return m_contains_decl; case OP_STR_INDEXOF: return m_indexof_decl; + case OP_STR_INDEXOF2: return m_indexof2_decl; default: return 0; } } @@ -176,6 +185,7 @@ void str_decl_plugin::get_op_names(svector & op_names, symbol cons op_names.push_back(builtin_name("EndsWith", OP_STR_ENDSWITH)); op_names.push_back(builtin_name("Contains", OP_STR_CONTAINS)); op_names.push_back(builtin_name("Indexof", OP_STR_INDEXOF)); + op_names.push_back(builtin_name("Indexof2", OP_STR_INDEXOF2)); } void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index a2a355ba2..54762f6b9 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -36,6 +36,7 @@ enum str_op_kind { OP_STR_ENDSWITH, OP_STR_CONTAINS, OP_STR_INDEXOF, + OP_STR_INDEXOF2, // end LAST_STR_OP }; @@ -53,6 +54,7 @@ protected: func_decl * m_endswith_decl; func_decl * m_contains_decl; func_decl * m_indexof_decl; + func_decl * m_indexof2_decl; arith_decl_plugin * m_arith_plugin; family_id m_arith_fid; diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index df77018e9..a5244f7bb 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -531,6 +531,15 @@ app * theory_str::mk_contains(expr * haystack, expr * needle) { return contains; } +app * theory_str::mk_indexof(expr * haystack, expr * needle) { + expr * args[2] = {haystack, needle}; + app * indexof = get_manager().mk_app(get_id(), OP_STR_INDEXOF, 0, 0, 2, args); + // immediately force internalization so that axiom setup does not fail + get_context().internalize(indexof, false); + set_up_axioms(indexof); + return indexof; +} + app * theory_str::mk_strlen(expr * e) { /*if (m_strutil.is_string(e)) {*/ if (false) { const char * strval = 0; @@ -602,7 +611,7 @@ expr * theory_str::mk_concat(expr * n1, expr * n2) { bool theory_str::can_propagate() { return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty() || !m_concat_axiom_todo.empty() || !m_axiom_CharAt_todo.empty() || !m_axiom_StartsWith_todo.empty() || !m_axiom_EndsWith_todo.empty() - || !m_axiom_Contains_todo.empty() || !m_axiom_Indexof_todo.empty() + || !m_axiom_Contains_todo.empty() || !m_axiom_Indexof_todo.empty() || !m_axiom_Indexof2_todo.empty() ; } @@ -651,6 +660,11 @@ void theory_str::propagate() { instantiate_axiom_Indexof(m_axiom_Indexof_todo[i]); } m_axiom_Indexof_todo.reset(); + + for (unsigned i = 0; i < m_axiom_Indexof2_todo.size(); ++i) { + instantiate_axiom_Indexof2(m_axiom_Indexof2_todo[i]); + } + m_axiom_Indexof2_todo.reset(); } } @@ -994,6 +1008,74 @@ void theory_str::instantiate_axiom_Indexof(enode * e) { assert_axiom(finalAxiom); } +void theory_str::instantiate_axiom_Indexof2(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("t_str_detail", tout << "already set up Indexof2 axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); + + TRACE("t_str_detail", tout << "instantiate Indexof2 axiom for " << mk_pp(expr, m) << std::endl;); + + // ------------------------------------------------------------------------------- + // if (arg[2] >= length(arg[0])) // ite2 + // resAst = -1 + // else + // args[0] = prefix . suffix + // /\ indexAst = indexof(suffix, arg[1]) + // /\ args[2] = len(prefix) + // /\ if (indexAst == -1) resAst = indexAst // ite3 + // else resAst = args[2] + indexAst + // ------------------------------------------------------------------------------- + + expr_ref resAst(mk_int_var("res"), m); + expr_ref indexAst(mk_int_var("index"), m); + expr_ref prefix(mk_str_var("prefix"), m); + expr_ref suffix(mk_str_var("suffix"), m); + expr_ref prefixLen(mk_strlen(prefix), m); + expr_ref zeroAst(mk_int(0), m); + expr_ref negOneAst(mk_int(-1), m); + + expr_ref ite3(m.mk_ite( + ctx.mk_eq_atom(indexAst, negOneAst), + ctx.mk_eq_atom(resAst, negOneAst), + ctx.mk_eq_atom(resAst, m_autil.mk_add(expr->get_arg(2), indexAst)) + ),m); + + expr_ref_vector ite2ElseItems(m); + ite2ElseItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(prefix, suffix))); + ite2ElseItems.push_back(ctx.mk_eq_atom(indexAst, mk_indexof(suffix, expr->get_arg(1)))); + ite2ElseItems.push_back(ctx.mk_eq_atom(expr->get_arg(2), prefixLen)); + ite2ElseItems.push_back(ite3); + expr_ref ite2Else(m.mk_and(ite2ElseItems.size(), ite2ElseItems.c_ptr()), m); + SASSERT(ite2Else); + + expr_ref ite2(m.mk_ite( + //m_autil.mk_ge(expr->get_arg(2), mk_strlen(expr->get_arg(0))), + m_autil.mk_ge(m_autil.mk_add(expr->get_arg(2), m_autil.mk_mul(mk_int(-1), mk_strlen(expr->get_arg(0)))), zeroAst), + ctx.mk_eq_atom(resAst, negOneAst), + ite2Else + ), m); + SASSERT(ite2); + + expr_ref ite1(m.mk_ite( + //m_autil.mk_lt(expr->get_arg(2), zeroAst), + m.mk_not(m_autil.mk_ge(expr->get_arg(2), zeroAst)), + ctx.mk_eq_atom(resAst, mk_indexof(expr->get_arg(0), expr->get_arg(1))), + ite2 + ), m); + SASSERT(ite1); + assert_axiom(ite1); + + expr_ref reduceTerm(ctx.mk_eq_atom(expr, resAst), m); + SASSERT(reduceTerm); + assert_axiom(reduceTerm); +} + void theory_str::attach_new_th_var(enode * n) { context & ctx = get_context(); theory_var v = mk_var(n); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index bf0fef38b..c652a3faf 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -107,11 +107,13 @@ namespace smt { ptr_vector m_string_constant_length_todo; // enode lists for term-specific axioms + // TODO maybe refactor this into a generic "library_aware_axiom_todo" list ptr_vector m_axiom_CharAt_todo; ptr_vector m_axiom_StartsWith_todo; ptr_vector m_axiom_EndsWith_todo; ptr_vector m_axiom_Contains_todo; ptr_vector m_axiom_Indexof_todo; + ptr_vector m_axiom_Indexof2_todo; // hashtable of all exprs for which we've already set up term-specific axioms -- // this prevents infinite recursive descent with respect to axioms that @@ -158,6 +160,7 @@ namespace smt { expr * mk_concat(expr * n1, expr * n2); expr * mk_concat_const_str(expr * n1, expr * n2); app * mk_contains(expr * haystack, expr * needle); + app * mk_indexof(expr * haystack, expr * needle); literal mk_literal(expr* _e); app * mk_int(int n); @@ -191,6 +194,8 @@ namespace smt { bool is_Contains(enode const * n) const { return is_Contains(n->get_owner()); } bool is_Indexof(app const * a) const { return a->is_app_of(get_id(), OP_STR_INDEXOF); } bool is_Indexof(enode const * n) const { return is_Indexof(n->get_owner()); } + bool is_Indexof2(app const * a) const { return a->is_app_of(get_id(), OP_STR_INDEXOF2); } + bool is_Indexof2(enode const * n) const { return is_Indexof2(n->get_owner()); } void instantiate_concat_axiom(enode * cat); void instantiate_basic_string_axioms(enode * str); @@ -201,6 +206,7 @@ namespace smt { void instantiate_axiom_EndsWith(enode * e); void instantiate_axiom_Contains(enode * e); void instantiate_axiom_Indexof(enode * e); + void instantiate_axiom_Indexof2(enode * e); void set_up_axioms(expr * ex); void handle_equality(expr * lhs, expr * rhs); From 7c8b882ae6603b5908d6aa6d3ce5b48422c73cb4 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 15 Jun 2016 18:04:33 -0400 Subject: [PATCH 120/562] decl and rewriter support for LastIndexof in theory_str (WIP) --- src/ast/rewriter/str_rewriter.cpp | 21 +++++++++++++++++++++ src/ast/rewriter/str_rewriter.h | 1 + src/ast/str_decl_plugin.cpp | 7 +++++++ src/ast/str_decl_plugin.h | 2 ++ 4 files changed, 31 insertions(+) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index 30dcb1d95..c4f2e634e 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -158,6 +158,24 @@ br_status str_rewriter::mk_str_Indexof2(expr * arg0, expr * arg1, expr * arg2, e } } +br_status str_rewriter::mk_str_LastIndexof(expr * haystack, expr * needle, expr_ref & result) { + TRACE("t_str_rw", tout << "rewrite (LastIndexof " << mk_pp(haystack, m()) << " " << mk_pp(needle, m()) << ")" << std::endl;); + if (m_strutil.is_string(haystack) && m_strutil.is_string(needle)) { + TRACE("t_str_rw", tout << "evaluating constant LastIndexof expression" << std::endl;); + std::string arg0Str = m_strutil.get_string_constant_value(haystack); + std::string arg1Str = m_strutil.get_string_constant_value(needle); + if (arg0Str.rfind(arg1Str) != std::string::npos) { + int index = arg0Str.rfind(arg1Str); + result = m_autil.mk_numeral(rational(index), true); + } else { + result = m_autil.mk_numeral(rational(-1), true); + } + return BR_DONE; + } else { + return BR_FAILED; + } +} + br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) { SASSERT(f->get_family_id() == get_fid()); @@ -183,6 +201,9 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con case OP_STR_INDEXOF2: SASSERT(num_args == 3); return mk_str_Indexof2(args[0], args[1], args[2], result); + case OP_STR_LASTINDEXOF: + SASSERT(num_args == 2); + return mk_str_LastIndexof(args[0], args[1], result); default: return BR_FAILED; } diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index c0bae2881..de399acba 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -46,6 +46,7 @@ public: br_status mk_str_Contains(expr * haystack, expr * needle, expr_ref & result); br_status mk_str_Indexof(expr * haystack, expr * needle, expr_ref & result); br_status mk_str_Indexof2(expr * arg0, expr * arg1, expr * arg2, expr_ref & result); + br_status mk_str_LastIndexof(expr * haystack, expr * needle, expr_ref & result); bool reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change); bool reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change); diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index f6e458fbd..fbdb10263 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -32,6 +32,7 @@ str_decl_plugin::str_decl_plugin(): m_contains_decl(0), m_indexof_decl(0), m_indexof2_decl(0), + m_lastindexof_decl(0), m_arith_plugin(0), m_arith_fid(0), m_int_sort(0){ @@ -51,6 +52,7 @@ void str_decl_plugin::finalize(void) { DEC_REF(m_contains_decl); DEC_REF(m_indexof_decl); DEC_REF(m_indexof2_decl); + DEC_REF(m_lastindexof_decl); DEC_REF(m_int_sort); } @@ -101,6 +103,9 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_indexof2_decl = m->mk_func_decl(symbol("Indexof2"), 3, d, i, func_decl_info(id, OP_STR_INDEXOF2)); m_manager->inc_ref(m_indexof2_decl); } + + m_lastindexof_decl = m->mk_func_decl(symbol("LastIndexof"), s, s, i, func_decl_info(id, OP_STR_LASTINDEXOF)); + m_manager->inc_ref(m_lastindexof_decl); } decl_plugin * str_decl_plugin::mk_fresh() { @@ -124,6 +129,7 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { case OP_STR_CONTAINS: return m_contains_decl; case OP_STR_INDEXOF: return m_indexof_decl; case OP_STR_INDEXOF2: return m_indexof2_decl; + case OP_STR_LASTINDEXOF: return m_lastindexof_decl; default: return 0; } } @@ -186,6 +192,7 @@ void str_decl_plugin::get_op_names(svector & op_names, symbol cons op_names.push_back(builtin_name("Contains", OP_STR_CONTAINS)); op_names.push_back(builtin_name("Indexof", OP_STR_INDEXOF)); op_names.push_back(builtin_name("Indexof2", OP_STR_INDEXOF2)); + op_names.push_back(builtin_name("LastIndexof", OP_STR_LASTINDEXOF)); } void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 54762f6b9..3e9a1d8f5 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -37,6 +37,7 @@ enum str_op_kind { OP_STR_CONTAINS, OP_STR_INDEXOF, OP_STR_INDEXOF2, + OP_STR_LASTINDEXOF, // end LAST_STR_OP }; @@ -55,6 +56,7 @@ protected: func_decl * m_contains_decl; func_decl * m_indexof_decl; func_decl * m_indexof2_decl; + func_decl * m_lastindexof_decl; arith_decl_plugin * m_arith_plugin; family_id m_arith_fid; From be5bf7fb803a036a3b129b90d1cc88c4240493db Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 15 Jun 2016 18:45:01 -0400 Subject: [PATCH 121/562] LastIndexof support --- src/smt/theory_str.cpp | 80 +++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 4 +++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index a5244f7bb..727048c11 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -611,7 +611,7 @@ expr * theory_str::mk_concat(expr * n1, expr * n2) { bool theory_str::can_propagate() { return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty() || !m_concat_axiom_todo.empty() || !m_axiom_CharAt_todo.empty() || !m_axiom_StartsWith_todo.empty() || !m_axiom_EndsWith_todo.empty() - || !m_axiom_Contains_todo.empty() || !m_axiom_Indexof_todo.empty() || !m_axiom_Indexof2_todo.empty() + || !m_axiom_Contains_todo.empty() || !m_axiom_Indexof_todo.empty() || !m_axiom_Indexof2_todo.empty() || !m_axiom_LastIndexof_todo.empty() ; } @@ -665,6 +665,11 @@ void theory_str::propagate() { instantiate_axiom_Indexof2(m_axiom_Indexof2_todo[i]); } m_axiom_Indexof2_todo.reset(); + + for (unsigned i = 0; i < m_axiom_LastIndexof_todo.size(); ++i) { + instantiate_axiom_LastIndexof(m_axiom_LastIndexof_todo[i]); + } + m_axiom_LastIndexof_todo.reset(); } } @@ -1076,6 +1081,75 @@ void theory_str::instantiate_axiom_Indexof2(enode * e) { assert_axiom(reduceTerm); } +void theory_str::instantiate_axiom_LastIndexof(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("t_str_detail", tout << "already set up LastIndexof axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); + + TRACE("t_str_detail", tout << "instantiate LastIndexof axiom for " << mk_pp(expr, m) << std::endl;); + + expr_ref x1(mk_str_var("x1"), m); + expr_ref x2(mk_str_var("x2"), m); + expr_ref indexAst(mk_int_var("index"), m); + expr_ref_vector items(m); + + // args[0] = x1 . args[1] . x2 + expr_ref eq1(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x1, mk_concat(expr->get_arg(1), x2))), m); + expr_ref arg0HasArg1(mk_contains(expr->get_arg(0), expr->get_arg(1)), m); // arg0HasArg1 = Contains(args[0], args[1]) + items.push_back(ctx.mk_eq_atom(arg0HasArg1, eq1)); + + + expr_ref condAst(arg0HasArg1, m); + //---------------------------- + // true branch + expr_ref_vector thenItems(m); + thenItems.push_back(m_autil.mk_ge(indexAst, mk_int(0))); + // args[0] = x1 . args[1] . x2 + // x1 doesn't contain args[1] + thenItems.push_back(m.mk_not(mk_contains(x2, expr->get_arg(1)))); + thenItems.push_back(ctx.mk_eq_atom(indexAst, mk_strlen(x1))); + + bool canSkip = false; + if (m_strutil.is_string(expr->get_arg(1))) { + std::string arg1Str = m_strutil.get_string_constant_value(expr->get_arg(1)); + if (arg1Str.length() == 1) { + canSkip = true; + } + } + + if (!canSkip) { + // args[0] = x3 . x4 /\ |x3| = |x1| + 1 /\ ! contains(x4, args[1]) + expr_ref x3(mk_str_var("x3"), m); + expr_ref x4(mk_str_var("x4"), m); + expr_ref tmpLen(m_autil.mk_add(indexAst, mk_int(1)), m); + thenItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x3, x4))); + thenItems.push_back(ctx.mk_eq_atom(mk_strlen(x3), tmpLen)); + thenItems.push_back(m.mk_not(mk_contains(x4, expr->get_arg(1)))); + } + //---------------------------- + // else branch + expr_ref_vector elseItems(m); + elseItems.push_back(ctx.mk_eq_atom(indexAst, mk_int(-1))); + + items.push_back(m.mk_ite(condAst, m.mk_and(thenItems.size(), thenItems.c_ptr()), m.mk_and(elseItems.size(), elseItems.c_ptr()))); + + expr_ref breakdownAssert(m.mk_and(items.size(), items.c_ptr()), m); + SASSERT(breakdownAssert); + + expr_ref reduceToIndex(ctx.mk_eq_atom(expr, indexAst), m); + SASSERT(reduceToIndex); + + expr_ref finalAxiom(m.mk_and(breakdownAssert, reduceToIndex), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); +} + void theory_str::attach_new_th_var(enode * n) { context & ctx = get_context(); theory_var v = mk_var(n); @@ -3850,6 +3924,10 @@ void theory_str::set_up_axioms(expr * ex) { app * ap = to_app(ex); if (is_Indexof(ap)) { m_axiom_Indexof_todo.push_back(n); + } else if (is_Indexof2(ap)) { + m_axiom_Indexof2_todo.push_back(n); + } else if (is_LastIndexof(ap)) { + m_axiom_LastIndexof_todo.push_back(n); } } } else { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index c652a3faf..70878b45f 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -114,6 +114,7 @@ namespace smt { ptr_vector m_axiom_Contains_todo; ptr_vector m_axiom_Indexof_todo; ptr_vector m_axiom_Indexof2_todo; + ptr_vector m_axiom_LastIndexof_todo; // hashtable of all exprs for which we've already set up term-specific axioms -- // this prevents infinite recursive descent with respect to axioms that @@ -196,6 +197,8 @@ namespace smt { bool is_Indexof(enode const * n) const { return is_Indexof(n->get_owner()); } bool is_Indexof2(app const * a) const { return a->is_app_of(get_id(), OP_STR_INDEXOF2); } bool is_Indexof2(enode const * n) const { return is_Indexof2(n->get_owner()); } + bool is_LastIndexof(app const * a) const { return a->is_app_of(get_id(), OP_STR_LASTINDEXOF); } + bool is_LastIndexof(enode const * n) const { return is_LastIndexof(n->get_owner()); } void instantiate_concat_axiom(enode * cat); void instantiate_basic_string_axioms(enode * str); @@ -207,6 +210,7 @@ namespace smt { void instantiate_axiom_Contains(enode * e); void instantiate_axiom_Indexof(enode * e); void instantiate_axiom_Indexof2(enode * e); + void instantiate_axiom_LastIndexof(enode * e); void set_up_axioms(expr * ex); void handle_equality(expr * lhs, expr * rhs); From fb20951064f2b67567598b987d681d1c68b92b8a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 15 Jun 2016 20:26:07 -0400 Subject: [PATCH 122/562] theory_str Substr support WIP --- src/ast/str_decl_plugin.cpp | 10 +++++++++ src/ast/str_decl_plugin.h | 2 ++ src/smt/theory_str.cpp | 42 +++++++++++++++++++++++++++++++++++++ src/smt/theory_str.h | 4 ++++ 4 files changed, 58 insertions(+) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index fbdb10263..7bd4ec154 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -33,6 +33,7 @@ str_decl_plugin::str_decl_plugin(): m_indexof_decl(0), m_indexof2_decl(0), m_lastindexof_decl(0), + m_substr_decl(0), m_arith_plugin(0), m_arith_fid(0), m_int_sort(0){ @@ -53,6 +54,7 @@ void str_decl_plugin::finalize(void) { DEC_REF(m_indexof_decl); DEC_REF(m_indexof2_decl); DEC_REF(m_lastindexof_decl); + DEC_REF(m_substr_decl); DEC_REF(m_int_sort); } @@ -106,6 +108,12 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_lastindexof_decl = m->mk_func_decl(symbol("LastIndexof"), s, s, i, func_decl_info(id, OP_STR_LASTINDEXOF)); m_manager->inc_ref(m_lastindexof_decl); + + { + sort * d[3] = {s, i, i }; + m_substr_decl = m->mk_func_decl(symbol("Substring"), 3, d, s, func_decl_info(id, OP_STR_SUBSTR)); + m_manager->inc_ref(m_substr_decl); + } } decl_plugin * str_decl_plugin::mk_fresh() { @@ -130,6 +138,7 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { case OP_STR_INDEXOF: return m_indexof_decl; case OP_STR_INDEXOF2: return m_indexof2_decl; case OP_STR_LASTINDEXOF: return m_lastindexof_decl; + case OP_STR_SUBSTR: return m_substr_decl; default: return 0; } } @@ -193,6 +202,7 @@ void str_decl_plugin::get_op_names(svector & op_names, symbol cons op_names.push_back(builtin_name("Indexof", OP_STR_INDEXOF)); op_names.push_back(builtin_name("Indexof2", OP_STR_INDEXOF2)); op_names.push_back(builtin_name("LastIndexof", OP_STR_LASTINDEXOF)); + op_names.push_back(builtin_name("Substring", OP_STR_SUBSTR)); } void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 3e9a1d8f5..bd2a70a1e 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -38,6 +38,7 @@ enum str_op_kind { OP_STR_INDEXOF, OP_STR_INDEXOF2, OP_STR_LASTINDEXOF, + OP_STR_SUBSTR, // end LAST_STR_OP }; @@ -57,6 +58,7 @@ protected: func_decl * m_indexof_decl; func_decl * m_indexof2_decl; func_decl * m_lastindexof_decl; + func_decl * m_substr_decl; arith_decl_plugin * m_arith_plugin; family_id m_arith_fid; diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 727048c11..4f04ede23 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -612,6 +612,7 @@ bool theory_str::can_propagate() { return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty() || !m_concat_axiom_todo.empty() || !m_axiom_CharAt_todo.empty() || !m_axiom_StartsWith_todo.empty() || !m_axiom_EndsWith_todo.empty() || !m_axiom_Contains_todo.empty() || !m_axiom_Indexof_todo.empty() || !m_axiom_Indexof2_todo.empty() || !m_axiom_LastIndexof_todo.empty() + || !m_axiom_Substr_todo.empty() ; } @@ -670,6 +671,11 @@ void theory_str::propagate() { instantiate_axiom_LastIndexof(m_axiom_LastIndexof_todo[i]); } m_axiom_LastIndexof_todo.reset(); + + for (unsigned i = 0; i < m_axiom_Substr_todo.size(); ++i) { + instantiate_axiom_Substr(m_axiom_Substr_todo[i]); + } + m_axiom_Substr_todo.reset(); } } @@ -1150,6 +1156,42 @@ void theory_str::instantiate_axiom_LastIndexof(enode * e) { assert_axiom(finalAxiom); } +void theory_str::instantiate_axiom_Substr(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("t_str_detail", tout << "already set up Substr axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); + + TRACE("t_str_detail", tout << "instantiate Substr axiom for " << mk_pp(expr, m) << std::endl;); + + expr_ref ts0(mk_str_var("ts0"), m); + expr_ref ts1(mk_str_var("ts1"), m); + expr_ref ts2(mk_str_var("ts2"), m); + + expr_ref ts0_contains_ts1(mk_contains(expr->get_arg(0), ts1), m); + + expr_ref_vector and_item(m); + and_item.push_back(ts0_contains_ts1); + and_item.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(ts0, mk_concat(ts1, ts2)))); + and_item.push_back(ctx.mk_eq_atom(expr->get_arg(1), mk_strlen(ts0))); + and_item.push_back(ctx.mk_eq_atom(expr->get_arg(2), mk_strlen(ts1))); + + expr_ref breakdownAssert(m.mk_and(and_item.size(), and_item.c_ptr()), m); + SASSERT(breakdownAssert); + + expr_ref reduceToVar(ctx.mk_eq_atom(expr, ts1), m); + SASSERT(reduceToVar); + + expr_ref finalAxiom(m.mk_and(breakdownAssert, reduceToVar), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); +} + void theory_str::attach_new_th_var(enode * n) { context & ctx = get_context(); theory_var v = mk_var(n); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 70878b45f..35a6fe91b 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -115,6 +115,7 @@ namespace smt { ptr_vector m_axiom_Indexof_todo; ptr_vector m_axiom_Indexof2_todo; ptr_vector m_axiom_LastIndexof_todo; + ptr_vector m_axiom_Substr_todo; // hashtable of all exprs for which we've already set up term-specific axioms -- // this prevents infinite recursive descent with respect to axioms that @@ -199,6 +200,8 @@ namespace smt { bool is_Indexof2(enode const * n) const { return is_Indexof2(n->get_owner()); } bool is_LastIndexof(app const * a) const { return a->is_app_of(get_id(), OP_STR_LASTINDEXOF); } bool is_LastIndexof(enode const * n) const { return is_LastIndexof(n->get_owner()); } + bool is_Substr(app const * a) const { return a->is_app_of(get_id(), OP_STR_SUBSTR); } + bool is_Substr(enode const * n) const { return is_Substr(n->get_owner()); } void instantiate_concat_axiom(enode * cat); void instantiate_basic_string_axioms(enode * str); @@ -211,6 +214,7 @@ namespace smt { void instantiate_axiom_Indexof(enode * e); void instantiate_axiom_Indexof2(enode * e); void instantiate_axiom_LastIndexof(enode * e); + void instantiate_axiom_Substr(enode * e); void set_up_axioms(expr * ex); void handle_equality(expr * lhs, expr * rhs); From 5b3c868c904065a84f00eb3042c20e0e851c2064 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 15 Jun 2016 21:14:54 -0400 Subject: [PATCH 123/562] theory_str Replace method --- src/ast/rewriter/str_rewriter.cpp | 25 +++++++++ src/ast/rewriter/str_rewriter.h | 1 + src/ast/str_decl_plugin.cpp | 10 ++++ src/ast/str_decl_plugin.h | 2 + src/smt/theory_str.cpp | 87 +++++++++++++++++++++++++++---- src/smt/theory_str.h | 4 ++ 6 files changed, 118 insertions(+), 11 deletions(-) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index c4f2e634e..8dc02cc09 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -176,6 +176,28 @@ br_status str_rewriter::mk_str_LastIndexof(expr * haystack, expr * needle, expr_ } } +br_status str_rewriter::mk_str_Replace(expr * base, expr * source, expr * target, expr_ref & result) { + TRACE("t_str_rw", tout << "rewrite (Replace " << mk_pp(base, m()) << " " << mk_pp(source, m()) << " " << mk_pp(target, m()) << ")" << std::endl;); + if (m_strutil.is_string(base) && m_strutil.is_string(source) && m_strutil.is_string(target)) { + std::string arg0Str = m_strutil.get_string_constant_value(base); + std::string arg1Str = m_strutil.get_string_constant_value(source); + std::string arg2Str = m_strutil.get_string_constant_value(target); + if (arg0Str.find(arg1Str) != std::string::npos) { + int index1 = arg0Str.find(arg1Str); + int index2 = index1 + arg1Str.length(); + std::string substr0 = arg0Str.substr(0, index1); + std::string substr2 = arg0Str.substr(index2); + std::string replaced = substr0 + arg2Str + substr2; + result = m_strutil.mk_string(replaced); + } else { + result = base; + } + return BR_DONE; + } else { + return BR_FAILED; + } +} + br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) { SASSERT(f->get_family_id() == get_fid()); @@ -204,6 +226,9 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con case OP_STR_LASTINDEXOF: SASSERT(num_args == 2); return mk_str_LastIndexof(args[0], args[1], result); + case OP_STR_REPLACE: + SASSERT(num_args == 3); + return mk_str_Replace(args[0], args[1], args[2], result); default: return BR_FAILED; } diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index de399acba..69a7c9579 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -47,6 +47,7 @@ public: br_status mk_str_Indexof(expr * haystack, expr * needle, expr_ref & result); br_status mk_str_Indexof2(expr * arg0, expr * arg1, expr * arg2, expr_ref & result); br_status mk_str_LastIndexof(expr * haystack, expr * needle, expr_ref & result); + br_status mk_str_Replace(expr * base, expr * source, expr * target, expr_ref & result); bool reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change); bool reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change); diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 7bd4ec154..526b02f64 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -34,6 +34,7 @@ str_decl_plugin::str_decl_plugin(): m_indexof2_decl(0), m_lastindexof_decl(0), m_substr_decl(0), + m_replace_decl(0), m_arith_plugin(0), m_arith_fid(0), m_int_sort(0){ @@ -55,6 +56,7 @@ void str_decl_plugin::finalize(void) { DEC_REF(m_indexof2_decl); DEC_REF(m_lastindexof_decl); DEC_REF(m_substr_decl); + DEC_REF(m_replace_decl); DEC_REF(m_int_sort); } @@ -114,6 +116,12 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_substr_decl = m->mk_func_decl(symbol("Substring"), 3, d, s, func_decl_info(id, OP_STR_SUBSTR)); m_manager->inc_ref(m_substr_decl); } + + { + sort * d[3] = {s, s, s}; + m_replace_decl = m->mk_func_decl(symbol("Replace"), 3, d, s, func_decl_info(id, OP_STR_REPLACE)); + m_manager->inc_ref(m_replace_decl); + } } decl_plugin * str_decl_plugin::mk_fresh() { @@ -139,6 +147,7 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { case OP_STR_INDEXOF2: return m_indexof2_decl; case OP_STR_LASTINDEXOF: return m_lastindexof_decl; case OP_STR_SUBSTR: return m_substr_decl; + case OP_STR_REPLACE: return m_replace_decl; default: return 0; } } @@ -203,6 +212,7 @@ void str_decl_plugin::get_op_names(svector & op_names, symbol cons op_names.push_back(builtin_name("Indexof2", OP_STR_INDEXOF2)); op_names.push_back(builtin_name("LastIndexof", OP_STR_LASTINDEXOF)); op_names.push_back(builtin_name("Substring", OP_STR_SUBSTR)); + op_names.push_back(builtin_name("Replace", OP_STR_REPLACE)); } void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index bd2a70a1e..ee2432c50 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -39,6 +39,7 @@ enum str_op_kind { OP_STR_INDEXOF2, OP_STR_LASTINDEXOF, OP_STR_SUBSTR, + OP_STR_REPLACE, // end LAST_STR_OP }; @@ -59,6 +60,7 @@ protected: func_decl * m_indexof2_decl; func_decl * m_lastindexof_decl; func_decl * m_substr_decl; + func_decl * m_replace_decl; arith_decl_plugin * m_arith_plugin; family_id m_arith_fid; diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 4f04ede23..1e2107f11 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -612,7 +612,7 @@ bool theory_str::can_propagate() { return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty() || !m_concat_axiom_todo.empty() || !m_axiom_CharAt_todo.empty() || !m_axiom_StartsWith_todo.empty() || !m_axiom_EndsWith_todo.empty() || !m_axiom_Contains_todo.empty() || !m_axiom_Indexof_todo.empty() || !m_axiom_Indexof2_todo.empty() || !m_axiom_LastIndexof_todo.empty() - || !m_axiom_Substr_todo.empty() + || !m_axiom_Substr_todo.empty() || !m_axiom_Replace_todo.empty() ; } @@ -676,6 +676,11 @@ void theory_str::propagate() { instantiate_axiom_Substr(m_axiom_Substr_todo[i]); } m_axiom_Substr_todo.reset(); + + for (unsigned i = 0; i < m_axiom_Replace_todo.size(); ++i) { + instantiate_axiom_Replace(m_axiom_Replace_todo[i]); + } + m_axiom_Replace_todo.reset(); } } @@ -1192,6 +1197,56 @@ void theory_str::instantiate_axiom_Substr(enode * e) { assert_axiom(finalAxiom); } +void theory_str::instantiate_axiom_Replace(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("t_str_detail", tout << "already set up Replace axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); + + TRACE("t_str_detail", tout << "instantiate Replace axiom for " << mk_pp(expr, m) << std::endl;); + + expr_ref x1(mk_str_var("x1"), m); + expr_ref x2(mk_str_var("x2"), m); + expr_ref i1(mk_int_var("i1"), m); + expr_ref result(mk_str_var("result"), m); + + // condAst = Contains(args[0], args[1]) + expr_ref condAst(mk_contains(expr->get_arg(0), expr->get_arg(1)), m); + // ----------------------- + // true branch + expr_ref_vector thenItems(m); + // args[0] = x1 . args[1] . x2 + thenItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x1, mk_concat(expr->get_arg(1), x2)))); + // i1 = |x1| + thenItems.push_back(ctx.mk_eq_atom(i1, mk_strlen(x1))); + // args[0] = x3 . x4 /\ |x3| = |x1| + |args[1]| - 1 /\ ! contains(x3, args[1]) + expr_ref x3(mk_str_var("x3"), m); + expr_ref x4(mk_str_var("x4"), m); + expr_ref tmpLen(m_autil.mk_add(i1, mk_strlen(expr->get_arg(1)), mk_int(-1)), m); + thenItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x3, x4))); + thenItems.push_back(ctx.mk_eq_atom(mk_strlen(x3), tmpLen)); + thenItems.push_back(m.mk_not(mk_contains(x3, expr->get_arg(1)))); + thenItems.push_back(ctx.mk_eq_atom(result, mk_concat(x1, mk_concat(expr->get_arg(2), x2)))); + // ----------------------- + // false branch + expr_ref elseBranch(ctx.mk_eq_atom(result, expr->get_arg(0)), m); + + expr_ref breakdownAssert(m.mk_ite(condAst, m.mk_and(thenItems.size(), thenItems.c_ptr()), elseBranch), m); + SASSERT(breakdownAssert); + + expr_ref reduceToResult(ctx.mk_eq_atom(expr, result), m); + SASSERT(reduceToResult); + + expr_ref finalAxiom(m.mk_and(breakdownAssert, reduceToResult), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); +} + void theory_str::attach_new_th_var(enode * n) { context & ctx = get_context(); theory_var v = mk_var(n); @@ -3928,6 +3983,10 @@ void theory_str::set_up_axioms(expr * ex) { } } else if (is_CharAt(ap)) { m_axiom_CharAt_todo.push_back(n); + } else if (is_Substr(ap)) { + m_axiom_Substr_todo.push_back(n); + } else if (is_Replace(ap)) { + m_axiom_Replace_todo.push_back(n); } else if (ap->get_num_args() == 0 && !is_string(ap)) { // if ex is a variable, add it to our list of variables TRACE("t_str_detail", tout << "tracking variable " << mk_ismt2_pp(ap, get_manager()) << std::endl;); @@ -3942,18 +4001,24 @@ void theory_str::set_up_axioms(expr * ex) { TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << ": expr is of sort Bool" << std::endl;); // set up axioms for boolean terms - enode * n = ctx.get_enode(ex); - SASSERT(n); - if (is_app(ex)) { - app * ap = to_app(ex); - if (is_StartsWith(ap)) { - m_axiom_StartsWith_todo.push_back(n); - } else if (is_EndsWith(ap)) { - m_axiom_EndsWith_todo.push_back(n); - } else if (is_Contains(ap)) { - m_axiom_Contains_todo.push_back(n); + if (ctx.e_internalized(ex)) { + enode * n = ctx.get_enode(ex); + SASSERT(n); + + if (is_app(ex)) { + app * ap = to_app(ex); + if (is_StartsWith(ap)) { + m_axiom_StartsWith_todo.push_back(n); + } else if (is_EndsWith(ap)) { + m_axiom_EndsWith_todo.push_back(n); + } else if (is_Contains(ap)) { + m_axiom_Contains_todo.push_back(n); + } } + } else { + TRACE("t_str_detail", tout << "WARNING: Bool term " << mk_ismt2_pp(ex, get_manager()) << " not internalized. Skipping to prevent a crash." << std::endl;); + return; } } else if (ex_sort == int_sort) { TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 35a6fe91b..7ee1d4281 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -116,6 +116,7 @@ namespace smt { ptr_vector m_axiom_Indexof2_todo; ptr_vector m_axiom_LastIndexof_todo; ptr_vector m_axiom_Substr_todo; + ptr_vector m_axiom_Replace_todo; // hashtable of all exprs for which we've already set up term-specific axioms -- // this prevents infinite recursive descent with respect to axioms that @@ -202,6 +203,8 @@ namespace smt { bool is_LastIndexof(enode const * n) const { return is_LastIndexof(n->get_owner()); } bool is_Substr(app const * a) const { return a->is_app_of(get_id(), OP_STR_SUBSTR); } bool is_Substr(enode const * n) const { return is_Substr(n->get_owner()); } + bool is_Replace(app const * a) const { return a->is_app_of(get_id(), OP_STR_REPLACE); } + bool is_Replace(enode const * n) const { return is_Replace(n->get_owner()); } void instantiate_concat_axiom(enode * cat); void instantiate_basic_string_axioms(enode * str); @@ -215,6 +218,7 @@ namespace smt { void instantiate_axiom_Indexof2(enode * e); void instantiate_axiom_LastIndexof(enode * e); void instantiate_axiom_Substr(enode * e); + void instantiate_axiom_Replace(enode * e); void set_up_axioms(expr * ex); void handle_equality(expr * lhs, expr * rhs); From 89a337ba7eac310745fc1e475ba3e7b59e2274ac Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 19 Jun 2016 18:25:31 -0400 Subject: [PATCH 124/562] quick path with string-integer integration in theory_str::simplify_concat_equality --- src/smt/theory_str.cpp | 74 ++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 1e2107f11..56b86885b 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1733,17 +1733,12 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { expr * a2_arg0 = a_nn2->get_arg(0); expr * a2_arg1 = a_nn2->get_arg(1); - // TODO - /* - int a1_arg0_len = getLenValue(t, a1_arg0); - int a1_arg1_len = getLenValue(t, a1_arg1); - int a2_arg0_len = getLenValue(t, a2_arg0); - int a2_arg1_len = getLenValue(t, a2_arg1); - */ - int a1_arg0_len = -1; - int a1_arg1_len = -1; - int a2_arg0_len = -1; - int a2_arg1_len = -1; + rational a1_arg0_len, a1_arg1_len, a2_arg0_len, a2_arg1_len; + + bool a1_arg0_len_exists = get_len_value(a1_arg0, a1_arg0_len); + bool a1_arg1_len_exists = get_len_value(a1_arg1, a1_arg1_len); + bool a2_arg0_len_exists = get_len_value(a2_arg0, a2_arg0_len); + bool a2_arg1_len_exists = get_len_value(a2_arg1, a2_arg1_len); TRACE("t_str", tout << "nn1 = " << mk_ismt2_pp(nn1, m) << std::endl << "nn2 = " << mk_ismt2_pp(nn2, m) << std::endl;); @@ -1797,35 +1792,38 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { } } - // TODO quick path 1-2 - /* - if(a1_arg0_len != -1 && a2_arg0_len != -1 && a1_arg0_len == a2_arg0_len){ - if (! inSameEqc(t, a1_arg0, a2_arg0)) { - __debugPrint(logFile, ">> [simplifyConcatEq] Quick Path 2-1: len(nn1.arg0) == len(nn2.arg0)\n"); - Z3_ast ax_l1 = Z3_mk_eq(ctx, nn1, nn2); - Z3_ast ax_l2 = Z3_mk_eq(ctx, mk_length(t, a1_arg0), mk_length(t, a2_arg0)); - Z3_ast ax_r1 = Z3_mk_eq(ctx, a1_arg0, a2_arg0); - Z3_ast ax_r2 = Z3_mk_eq(ctx, a1_arg1, a2_arg1); - Z3_ast toAdd = Z3_mk_implies(ctx, mk_2_and(t, ax_l1, ax_l2), mk_2_and(t, ax_r1, ax_r2)); - addAxiom(t, toAdd, __LINE__); - return; - } - } + // quick path 2-1 + if (a1_arg0_len_exists && a2_arg0_len_exists && a1_arg0_len == a2_arg0_len) { + if (!in_same_eqc(a1_arg0, a2_arg0)) { + TRACE("t_str_detail", tout << "quick path 2-1: len(nn1.arg0) == len(nn2.arg0)" << std::endl;); + expr_ref ax_l1(ctx.mk_eq_atom(nn1, nn2), m); + expr_ref ax_l2(ctx.mk_eq_atom(mk_strlen(a1_arg0), mk_strlen(a2_arg0)), m); + expr_ref ax_r1(ctx.mk_eq_atom(a1_arg0, a2_arg0), m); + expr_ref ax_r2(ctx.mk_eq_atom(a1_arg1, a2_arg1), m); - if (a1_arg1_len != -1 && a2_arg1_len != -1 && a1_arg1_len == a2_arg1_len) - { - if (!inSameEqc(t, a1_arg1, a2_arg1)) { - __debugPrint(logFile, ">> [simplifyConcatEq] Quick Path 2-2: len(nn1.arg1) == len(nn2.arg1)\n"); - Z3_ast ax_l1 = Z3_mk_eq(ctx, nn1, nn2); - Z3_ast ax_l2 = Z3_mk_eq(ctx, mk_length(t, a1_arg1), mk_length(t, a2_arg1)); - Z3_ast ax_r1 = Z3_mk_eq(ctx, a1_arg0, a2_arg0); - Z3_ast ax_r2 = Z3_mk_eq(ctx, a1_arg1, a2_arg1); - Z3_ast toAdd = Z3_mk_implies(ctx, mk_2_and(t, ax_l1, ax_l2), mk_2_and(t, ax_r1, ax_r2)); - addAxiom(t, toAdd, __LINE__); - return; + expr_ref premise(m.mk_and(ax_l1, ax_l2), m); + expr_ref conclusion(m.mk_and(ax_r1, ax_r2), m); + + assert_implication(premise, conclusion); + return; + } + } + + if (a1_arg1_len_exists && a2_arg1_len_exists && a1_arg1_len == a2_arg1_len) { + if (!in_same_eqc(a1_arg1, a2_arg1)) { + TRACE("t_str_detail", tout << "quick path 2-2: len(nn1.arg1) == len(nn2.arg1)" << std::endl;); + expr_ref ax_l1(ctx.mk_eq_atom(nn1, nn2), m); + expr_ref ax_l2(ctx.mk_eq_atom(mk_strlen(a1_arg1), mk_strlen(a2_arg1)), m); + expr_ref ax_r1(ctx.mk_eq_atom(a1_arg0, a2_arg0), m); + expr_ref ax_r2(ctx.mk_eq_atom(a1_arg1, a2_arg1), m); + + expr_ref premise(m.mk_and(ax_l1, ax_l2), m); + expr_ref conclusion(m.mk_and(ax_r1, ax_r2), m); + + assert_implication(premise, conclusion); + return; + } } - } - */ expr * new_nn1 = simplify_concat(nn1); expr * new_nn2 = simplify_concat(nn2); From ba42478f9b229154f883abb85c8e59aff13e9c2d Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 20 Jun 2016 20:02:22 -0400 Subject: [PATCH 125/562] string-integer wip --- src/smt/theory_str.cpp | 60 +++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 2 ++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 56b86885b..15e202409 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1716,6 +1716,64 @@ expr * theory_str::simplify_concat(expr * node) { } +void theory_str::infer_len_concat_equality(expr * nn1, expr * nn2) { + rational nnLen; + bool nnLen_exists = get_len_value(nn1, nnLen); + if (!nnLen_exists) { + nnLen_exists = get_len_value(nn2, nnLen); + } + + // case 1: + // Known: a1_arg0 and a1_arg1 + // Unknown: nn1 + + if (is_concat(to_app(nn1))) { + rational nn1ConcatLen; + bool nn1ConcatLen_exists = infer_len_concat(nn1, nn1ConcatLen); + if (nnLen_exists && nn1ConcatLen_exists) { + nnLen = nn1ConcatLen; + } + } + + // case 2: + // Known: a1_arg0 and a1_arg1 + // Unknown: nn1 + + if (is_concat(to_app(nn2))) { + rational nn2ConcatLen; + bool nn2ConcatLen_exists = infer_len_concat(nn2, nn2ConcatLen); + if (nnLen_exists && nn2ConcatLen_exists) { + nnLen = nn2ConcatLen; + } + } + + if (nnLen_exists) { + if (is_concat(to_app(nn1))) { + infer_len_concat_arg(nn1, nnLen); + } + if (is_concat(to_app(nn2))) { + infer_len_concat_arg(nn2, nnLen); + } + } + + /* + if (isConcatFunc(t, nn2)) { + int nn2ConcatLen = inferLenConcat(t, nn2); + if (nnLen == -1 && nn2ConcatLen != -1) + nnLen = nn2ConcatLen; + } + + if (nnLen != -1) { + if (isConcatFunc(t, nn1)) { + inferLenConcatArg(t, nn1, nnLen); + } + if (isConcatFunc(t, nn2)) { + inferLenConcatArg(t, nn2, nnLen); + } + } + */ +} + /* * Handle two equivalent Concats. */ @@ -1743,7 +1801,7 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { TRACE("t_str", tout << "nn1 = " << mk_ismt2_pp(nn1, m) << std::endl << "nn2 = " << mk_ismt2_pp(nn2, m) << std::endl;); - // TODO inferLenConcatEq(nn1, nn2); + infer_len_concat_equality(nn1, nn2); if (a1_arg0 == a2_arg0) { if (!in_same_eqc(a1_arg1, a2_arg1)) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 7ee1d4281..41091f64b 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -244,6 +244,8 @@ namespace smt { void simplify_concat_equality(expr * lhs, expr * rhs); void solve_concat_eq_str(expr * concat, expr * str); + void infer_len_concat_equality(expr * nn1, expr * nn2); + bool is_concat_eq_type1(expr * concatAst1, expr * concatAst2); bool is_concat_eq_type2(expr * concatAst1, expr * concatAst2); bool is_concat_eq_type3(expr * concatAst1, expr * concatAst2); From 1e46782392cc67428e334548f2827aa731b07fbd Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 21 Jun 2016 17:25:28 -0400 Subject: [PATCH 126/562] theory_str infer_len_concat --- src/smt/theory_str.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/smt/theory_str.h | 1 + 2 files changed, 41 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 15e202409..a747ce12d 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1716,6 +1716,46 @@ expr * theory_str::simplify_concat(expr * node) { } +// Modified signature of Z3str2's inferLenConcat(). +// Returns true iff nLen can be inferred by this method +// (i.e. the equivalent of a len_exists flag in get_len_value()). + +bool theory_str::infer_len_concat(expr * n, rational & nLen) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + expr * arg0 = to_app(n)->get_arg(0); + expr * arg1 = to_app(n)->get_arg(1); + + rational arg0_len, arg1_len; + bool arg0_len_exists = get_len_value(arg0, arg0_len); + bool arg1_len_exists = get_len_value(arg1, arg1_len); + rational tmp_len; + bool nLen_exists = get_len_value(n, tmp_len); + + if (arg0_len_exists && arg1_len_exists && !nLen_exists) { + expr_ref_vector l_items(m); + // if (mk_strlen(arg0) != mk_int(arg0_len)) { + { + l_items.push_back(ctx.mk_eq_atom(mk_strlen(arg0), mk_int(arg0_len))); + } + + // if (mk_strlen(arg1) != mk_int(arg1_len)) { + { + l_items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1_len))); + } + + expr_ref axl(m.mk_and(l_items.size(), l_items.c_ptr()), m); + rational nnLen = arg0_len + arg1_len; + expr_ref axr(ctx.mk_eq_atom(mk_strlen(n), mk_int(nnLen)), m); + TRACE("t_str_detail", tout << "inferred (Length " << mk_pp(n, m) << ") = " << nnLen << std::endl;); + assert_implication(axl, axr); + nLen = nnLen; + return true; + } else { + return false; + } +} + void theory_str::infer_len_concat_equality(expr * nn1, expr * nn2) { rational nnLen; bool nnLen_exists = get_len_value(nn1, nnLen); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 41091f64b..e3589d68d 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -245,6 +245,7 @@ namespace smt { void solve_concat_eq_str(expr * concat, expr * str); void infer_len_concat_equality(expr * nn1, expr * nn2); + bool infer_len_concat(expr * n, rational & nLen); bool is_concat_eq_type1(expr * concatAst1, expr * concatAst2); bool is_concat_eq_type2(expr * concatAst1, expr * concatAst2); From a808a8c587d20fae4b130084b6779d6bdd589cb0 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 21 Jun 2016 17:38:49 -0400 Subject: [PATCH 127/562] theory_str infer_len_concat_arg --- src/smt/theory_str.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++ src/smt/theory_str.h | 1 + 2 files changed, 56 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index a747ce12d..e8df17c58 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1756,6 +1756,61 @@ bool theory_str::infer_len_concat(expr * n, rational & nLen) { } } +void theory_str::infer_len_concat_arg(expr * n, rational len) { + if (len.is_neg()) { + return; + } + + context & ctx = get_context(); + ast_manager & m = get_manager(); + + expr * arg0 = to_app(n)->get_arg(0); + expr * arg1 = to_app(n)->get_arg(1); + rational arg0_len, arg1_len; + bool arg0_len_exists = get_len_value(arg0, arg0_len); + bool arg1_len_exists = get_len_value(arg1, arg1_len); + + expr_ref_vector l_items(m); + expr_ref axr(m); + axr.reset(); + + // if (mk_length(t, n) != mk_int(ctx, len)) { + { + l_items.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(len))); + } + + if (!arg0_len_exists && arg1_len_exists) { + //if (mk_length(t, arg1) != mk_int(ctx, arg1_len)) { + { + l_items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1_len))); + } + rational arg0Len = len - arg1_len; + if (arg0Len.is_nonneg()) { + axr = ctx.mk_eq_atom(mk_strlen(arg0), mk_int(arg0Len)); + } else { + // TODO negate? + } + } else if (arg0_len_exists && !arg1_len_exists) { + //if (mk_length(t, arg0) != mk_int(ctx, arg0_len)) { + { + l_items.push_back(ctx.mk_eq_atom(mk_strlen(arg0), mk_int(arg0_len))); + } + rational arg1Len = len - arg0_len; + if (arg1Len.is_nonneg()) { + axr = ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1Len)); + } else { + // TODO negate? + } + } else { + + } + + if (axr) { + expr_ref axl(m.mk_and(l_items.size(), l_items.c_ptr()), m); + assert_implication(axl, axr); + } +} + void theory_str::infer_len_concat_equality(expr * nn1, expr * nn2) { rational nnLen; bool nnLen_exists = get_len_value(nn1, nnLen); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index e3589d68d..151dbc53f 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -246,6 +246,7 @@ namespace smt { void infer_len_concat_equality(expr * nn1, expr * nn2); bool infer_len_concat(expr * n, rational & nLen); + void infer_len_concat_arg(expr * n, rational len); bool is_concat_eq_type1(expr * concatAst1, expr * concatAst2); bool is_concat_eq_type2(expr * concatAst1, expr * concatAst2); From 4c346298064c06fb10554477d14393e2d627ba2e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 21 Jun 2016 21:13:16 -0400 Subject: [PATCH 128/562] starting regex support, rewriter --- src/ast/rewriter/str_rewriter.cpp | 27 +++++++++++++++++++++++++++ src/ast/rewriter/str_rewriter.h | 3 +++ src/ast/str_decl_plugin.cpp | 23 +++++++++++++++++++++++ src/ast/str_decl_plugin.h | 10 ++++++++++ 4 files changed, 63 insertions(+) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index 8dc02cc09..3a0300ae4 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -198,6 +198,27 @@ br_status str_rewriter::mk_str_Replace(expr * base, expr * source, expr * target } } +br_status str_rewriter::mk_re_Str2Reg(expr * str, expr_ref & result) { + // the argument to Str2Reg *must* be a string constant + // TODO is an assertion error too strict here? this basically crashes the solver + VERIFY(m_strutil.is_string(str)); + return BR_FAILED; +} + +br_status str_rewriter::mk_re_RegexIn(expr * str, expr * re, expr_ref & result) { + // fast path: + // (RegexIn E (Str2Reg S)) --> (= E S) + if (m_strutil.is_re_Str2Reg(re)) { + TRACE("t_str_rw", tout << "RegexIn fast path: " << mk_pp(str, m()) << " in " << mk_pp(re, m()) << std::endl;); + expr * regexStr = to_app(re)->get_arg(0); + VERIFY(m_strutil.is_string(regexStr)); + result = m().mk_eq(str, regexStr); + return BR_REWRITE_FULL; + } + + return BR_FAILED; +} + br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) { SASSERT(f->get_family_id() == get_fid()); @@ -229,6 +250,12 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con case OP_STR_REPLACE: SASSERT(num_args == 3); return mk_str_Replace(args[0], args[1], args[2], result); + case OP_RE_STR2REGEX: + SASSERT(num_args == 1); + return mk_re_Str2Reg(args[0], result); + case OP_RE_REGEXIN: + SASSERT(num_args == 2); + return mk_re_RegexIn(args[0], args[1], result); default: return BR_FAILED; } diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index 69a7c9579..5c0e1167f 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -49,6 +49,9 @@ public: br_status mk_str_LastIndexof(expr * haystack, expr * needle, expr_ref & result); br_status mk_str_Replace(expr * base, expr * source, expr * target, expr_ref & result); + br_status mk_re_Str2Reg(expr * str, expr_ref & result); + br_status mk_re_RegexIn(expr * str, expr * re, expr_ref & result); + bool reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change); bool reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change); diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 526b02f64..7cd03fa16 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -24,6 +24,7 @@ Revision History: str_decl_plugin::str_decl_plugin(): m_strv_sym("String"), m_str_decl(0), + m_regex_decl(0), m_concat_decl(0), m_length_decl(0), m_charat_decl(0), @@ -35,6 +36,8 @@ str_decl_plugin::str_decl_plugin(): m_lastindexof_decl(0), m_substr_decl(0), m_replace_decl(0), + m_re_str2regex_decl(0), + m_re_regexin_decl(0), m_arith_plugin(0), m_arith_fid(0), m_int_sort(0){ @@ -46,6 +49,7 @@ str_decl_plugin::~str_decl_plugin(){ void str_decl_plugin::finalize(void) { #define DEC_REF(decl) if (decl) { m_manager->dec_ref(decl); } ((void) 0) DEC_REF(m_str_decl); + DEC_REF(m_regex_decl); DEC_REF(m_concat_decl); DEC_REF(m_length_decl); DEC_REF(m_charat_decl); @@ -57,6 +61,8 @@ void str_decl_plugin::finalize(void) { DEC_REF(m_lastindexof_decl); DEC_REF(m_substr_decl); DEC_REF(m_replace_decl); + DEC_REF(m_re_str2regex_decl); + DEC_REF(m_re_regexin_decl); DEC_REF(m_int_sort); } @@ -66,6 +72,10 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m->inc_ref(m_str_decl); sort * s = m_str_decl; + m_regex_decl = m->mk_sort(symbol("Regex"), sort_info(id, REGEX_SORT)); + m->inc_ref(m_regex_decl); + sort * re = m_regex_decl; + SASSERT(m_manager->has_plugin(symbol("arith"))); m_arith_fid = m_manager->mk_family_id("arith"); m_arith_plugin = static_cast(m_manager->get_plugin(m_arith_fid)); @@ -122,6 +132,13 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_replace_decl = m->mk_func_decl(symbol("Replace"), 3, d, s, func_decl_info(id, OP_STR_REPLACE)); m_manager->inc_ref(m_replace_decl); } + + m_re_str2regex_decl = m->mk_func_decl(symbol("Str2Reg"), s, re, func_decl_info(id, OP_RE_STR2REGEX)); + m_manager->inc_ref(m_re_str2regex_decl); + + m_re_regexin_decl = m->mk_func_decl(symbol("RegexIn"), s, re, boolT, func_decl_info(id, OP_RE_REGEXIN)); + m_manager->inc_ref(m_re_regexin_decl); + } decl_plugin * str_decl_plugin::mk_fresh() { @@ -131,6 +148,7 @@ decl_plugin * str_decl_plugin::mk_fresh() { sort * str_decl_plugin::mk_sort(decl_kind k, unsigned num_parameters, parameter const * parameters) { switch (k) { case STRING_SORT: return m_str_decl; + case REGEX_SORT: return m_regex_decl; default: return 0; } } @@ -148,6 +166,8 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { case OP_STR_LASTINDEXOF: return m_lastindexof_decl; case OP_STR_SUBSTR: return m_substr_decl; case OP_STR_REPLACE: return m_replace_decl; + case OP_RE_STR2REGEX: return m_re_str2regex_decl; + case OP_RE_REGEXIN: return m_re_regexin_decl; default: return 0; } } @@ -213,10 +233,13 @@ void str_decl_plugin::get_op_names(svector & op_names, symbol cons op_names.push_back(builtin_name("LastIndexof", OP_STR_LASTINDEXOF)); op_names.push_back(builtin_name("Substring", OP_STR_SUBSTR)); op_names.push_back(builtin_name("Replace", OP_STR_REPLACE)); + op_names.push_back(builtin_name("Str2Reg", OP_RE_STR2REGEX)); + op_names.push_back(builtin_name("RegexIn", OP_RE_REGEXIN)); } void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { sort_names.push_back(builtin_name("String", STRING_SORT)); + sort_names.push_back(builtin_name("Regex", REGEX_SORT)); } bool str_decl_plugin::is_value(app * e) const { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index ee2432c50..496468e5a 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -23,6 +23,7 @@ Revision History: enum str_sort_kind { STRING_SORT, + REGEX_SORT, }; enum str_op_kind { @@ -40,6 +41,9 @@ enum str_op_kind { OP_STR_LASTINDEXOF, OP_STR_SUBSTR, OP_STR_REPLACE, + // regular expression operators + OP_RE_STR2REGEX, + OP_RE_REGEXIN, // end LAST_STR_OP }; @@ -48,6 +52,7 @@ class str_decl_plugin : public decl_plugin { protected: symbol m_strv_sym; sort * m_str_decl; + sort * m_regex_decl; func_decl * m_concat_decl; func_decl * m_length_decl; @@ -62,6 +67,9 @@ protected: func_decl * m_substr_decl; func_decl * m_replace_decl; + func_decl * m_re_str2regex_decl; + func_decl * m_re_regexin_decl; + arith_decl_plugin * m_arith_plugin; family_id m_arith_fid; sort * m_int_sort; @@ -103,6 +111,8 @@ public: bool is_string(expr const * n, const char ** val) const; bool is_string(expr const * n) const; + bool is_re_Str2Reg(expr const * n) const { return is_app_of(n, get_fid(), OP_RE_STR2REGEX); } + std::string get_string_constant_value(expr const *n) const; // TODO }; From 04803d7a3b5ec9e1eef2945648271880b73983e7 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 23 Jun 2016 15:24:35 -0400 Subject: [PATCH 129/562] starting regex support --- src/ast/str_decl_plugin.cpp | 21 +++++++ src/ast/str_decl_plugin.h | 6 ++ src/smt/theory_str.cpp | 117 ++++++++++++++++++++++++++++++++++-- src/smt/theory_str.h | 18 ++++++ 4 files changed, 157 insertions(+), 5 deletions(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 7cd03fa16..b140e11c3 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -38,6 +38,9 @@ str_decl_plugin::str_decl_plugin(): m_replace_decl(0), m_re_str2regex_decl(0), m_re_regexin_decl(0), + m_re_regexconcat_decl(0), + m_re_regexstar_decl(0), + m_re_regexunion_decl(0), m_arith_plugin(0), m_arith_fid(0), m_int_sort(0){ @@ -63,6 +66,9 @@ void str_decl_plugin::finalize(void) { DEC_REF(m_replace_decl); DEC_REF(m_re_str2regex_decl); DEC_REF(m_re_regexin_decl); + DEC_REF(m_re_regexconcat_decl); + DEC_REF(m_re_regexstar_decl); + DEC_REF(m_re_regexunion_decl); DEC_REF(m_int_sort); } @@ -139,6 +145,15 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_re_regexin_decl = m->mk_func_decl(symbol("RegexIn"), s, re, boolT, func_decl_info(id, OP_RE_REGEXIN)); m_manager->inc_ref(m_re_regexin_decl); + m_re_regexconcat_decl = m->mk_func_decl(symbol("RegexConcat"), re, re, re, func_decl_info(id, OP_RE_REGEXCONCAT)); + m_manager->inc_ref(m_re_regexconcat_decl); + + m_re_regexstar_decl = m->mk_func_decl(symbol("RegexStar"), re, re, func_decl_info(id, OP_RE_REGEXSTAR)); + m_manager->inc_ref(m_re_regexstar_decl); + + m_re_regexunion_decl = m->mk_func_decl(symbol("RegexUnion"), re, re, re, func_decl_info(id, OP_RE_REGEXUNION)); + m_manager->inc_ref(m_re_regexunion_decl); + } decl_plugin * str_decl_plugin::mk_fresh() { @@ -168,6 +183,9 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { case OP_STR_REPLACE: return m_replace_decl; case OP_RE_STR2REGEX: return m_re_str2regex_decl; case OP_RE_REGEXIN: return m_re_regexin_decl; + case OP_RE_REGEXCONCAT: return m_re_regexconcat_decl; + case OP_RE_REGEXSTAR: return m_re_regexstar_decl; + case OP_RE_REGEXUNION: return m_re_regexunion_decl; default: return 0; } } @@ -235,6 +253,9 @@ void str_decl_plugin::get_op_names(svector & op_names, symbol cons op_names.push_back(builtin_name("Replace", OP_STR_REPLACE)); op_names.push_back(builtin_name("Str2Reg", OP_RE_STR2REGEX)); op_names.push_back(builtin_name("RegexIn", OP_RE_REGEXIN)); + op_names.push_back(builtin_name("RegexConcat", OP_RE_REGEXCONCAT)); + op_names.push_back(builtin_name("RegexStar", OP_RE_REGEXSTAR)); + op_names.push_back(builtin_name("RegexUnion", OP_RE_REGEXUNION)); } void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 496468e5a..ccd2915af 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -44,6 +44,9 @@ enum str_op_kind { // regular expression operators OP_RE_STR2REGEX, OP_RE_REGEXIN, + OP_RE_REGEXCONCAT, + OP_RE_REGEXSTAR, + OP_RE_REGEXUNION, // end LAST_STR_OP }; @@ -69,6 +72,9 @@ protected: func_decl * m_re_str2regex_decl; func_decl * m_re_regexin_decl; + func_decl * m_re_regexconcat_decl; + func_decl * m_re_regexstar_decl; + func_decl * m_re_regexunion_decl; arith_decl_plugin * m_arith_plugin; family_id m_arith_fid; diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index e8df17c58..ab7bb13ef 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -479,6 +479,29 @@ app * theory_str::mk_str_var(std::string name) { return a; } +app * theory_str::mk_regex_rep_var() { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + sort * string_sort = m.mk_sort(get_family_id(), STRING_SORT); + app * a = m.mk_fresh_const("regex", string_sort); + + ctx.internalize(a, false); + SASSERT(ctx.get_enode(a) != NULL); + SASSERT(ctx.e_internalized(a)); + mk_var(ctx.get_enode(a)); + m_basicstr_axiom_todo.push_back(ctx.get_enode(a)); + + m_trail.push_back(a); + // TODO cross-check which variable sets we need + variable_set.insert(a); + //internal_variable_set.insert(a); + regex_variable_set.insert(a); + track_variable_scope(a); + + return a; +} + app * theory_str::mk_nonempty_str_var() { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -613,6 +636,7 @@ bool theory_str::can_propagate() { || !m_axiom_CharAt_todo.empty() || !m_axiom_StartsWith_todo.empty() || !m_axiom_EndsWith_todo.empty() || !m_axiom_Contains_todo.empty() || !m_axiom_Indexof_todo.empty() || !m_axiom_Indexof2_todo.empty() || !m_axiom_LastIndexof_todo.empty() || !m_axiom_Substr_todo.empty() || !m_axiom_Replace_todo.empty() + || !m_axiom_RegexIn_todo.empty() ; } @@ -681,6 +705,11 @@ void theory_str::propagate() { instantiate_axiom_Replace(m_axiom_Replace_todo[i]); } m_axiom_Replace_todo.reset(); + + for (unsigned i = 0; i < m_axiom_RegexIn_todo.size(); ++i) { + instantiate_axiom_RegexIn(m_axiom_RegexIn_todo[i]); + } + m_axiom_RegexIn_todo.reset(); } } @@ -1247,6 +1276,84 @@ void theory_str::instantiate_axiom_Replace(enode * e) { assert_axiom(finalAxiom); } +expr * theory_str::mk_RegexIn(expr * str, expr * regexp) { + expr * args[2] = {str, regexp}; + app * regexIn = get_manager().mk_app(get_id(), OP_RE_REGEXIN, 0, 0, 2, args); + // immediately force internalization so that axiom setup does not fail + get_context().internalize(regexIn, false); + set_up_axioms(regexIn); + return regexIn; +} + +void theory_str::instantiate_axiom_RegexIn(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("t_str_detail", tout << "already set up RegexIn axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); + + TRACE("t_str_detail", tout << "instantiate RegexIn axiom for " << mk_pp(expr, m) << std::endl;); + + // I don't think we need to port regexInBoolMap and regexInVarStrMap, + // but they would go here from reduce_regexIn + + expr_ref str(expr->get_arg(0), m); + app * regex = to_app(expr->get_arg(1)); + + if (is_Str2Reg(regex)) { + expr_ref rxStr(regex->get_arg(0), m); + // want to assert 'expr IFF (str == rxStr)' + expr_ref rhs(ctx.mk_eq_atom(str, rxStr), m); + expr_ref finalAxiom(m.mk_iff(expr, rhs), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); + } else if (is_RegexConcat(regex)) { + expr_ref var1(mk_regex_rep_var(), m); + expr_ref var2(mk_regex_rep_var(), m); + expr_ref rhs(mk_concat(var1, var2), m); + expr_ref rx1(regex->get_arg(0), m); + expr_ref rx2(regex->get_arg(1), m); + expr_ref var1InRegex1(mk_RegexIn(var1, rx1), m); + expr_ref var2InRegex2(mk_RegexIn(var2, rx2), m); + + expr_ref_vector items(m); + items.push_back(var1InRegex1); + items.push_back(var2InRegex2); + items.push_back(ctx.mk_eq_atom(expr, ctx.mk_eq_atom(str, rhs))); + + expr_ref finalAxiom(mk_and(items), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); + /* + Z3_ast var1 = mk_regexRepVar(t); + Z3_ast var2 = mk_regexRepVar(t); + rhs = mk_concat(t, var1, var2); + + Z3_ast regex1 = Z3_get_app_arg(ctx, arg1_func_app, 0); + Z3_ast regex2 = Z3_get_app_arg(ctx, arg1_func_app, 1); + Z3_ast var1InRegex1 = mk_2_arg_app(ctx, td->RegexIn, var1, regex1); + Z3_ast var2InRegex2 = mk_2_arg_app(ctx, td->RegexIn, var2, regex2); + std::vector items; + items.push_back(var1InRegex1); + items.push_back(var2InRegex2); + items.push_back(Z3_mk_eq(ctx, resBoolVar, Z3_mk_eq(ctx, args[0], rhs))); + extraAssert = mk_and_fromVector(t, items); + return resBoolVar; + */ + } else if (is_RegexUnion(regex)) { + + } else if (is_RegexStar(regex)) { + + } else { + TRACE("t_str_detail", tout << "ERROR: unknown regex expression " << mk_pp(regex, m) << "!" << std::endl;); + NOT_IMPLEMENTED_YET(); + } +} + void theory_str::attach_new_th_var(enode * n) { context & ctx = get_context(); theory_var v = mk_var(n); @@ -4165,6 +4272,8 @@ void theory_str::set_up_axioms(expr * ex) { m_axiom_EndsWith_todo.push_back(n); } else if (is_Contains(ap)) { m_axiom_Contains_todo.push_back(n); + } else if (is_RegexIn(ap)) { + m_axiom_RegexIn_todo.push_back(n); } } } else { @@ -4319,6 +4428,7 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { for (std::set::iterator var_it = vars.begin(); var_it != vars.end(); ++var_it) { variable_set.erase(*var_it); internal_variable_set.erase(*var_it); + regex_variable_set.erase(*var_it); count += 1; } TRACE("t_str_detail", tout << "cleaned up " << count << " variables" << std::endl;); @@ -5994,13 +6104,10 @@ void theory_str::process_free_var(std::map & freeVar_map) { for (std::map::iterator fvIt = freeVar_map.begin(); fvIt != freeVar_map.end(); fvIt++) { expr * freeVar = fvIt->first; - /* - std::string vName = std::string(Z3_ast_to_string(ctx, freeVar)); - if (vName.length() >= 9 && vName.substr(0, 9) == "$$_regVar") { + // skip all regular expression vars + if (regex_variable_set.find(freeVar) != regex_variable_set.end()) { continue; } - */ - // TODO skip all regular expression vars // Iterate the EQC of freeVar, its eqc variable should not be in the eqcRepSet. // If found, have to filter it out diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 151dbc53f..9aead1105 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -117,6 +117,7 @@ namespace smt { ptr_vector m_axiom_LastIndexof_todo; ptr_vector m_axiom_Substr_todo; ptr_vector m_axiom_Replace_todo; + ptr_vector m_axiom_RegexIn_todo; // hashtable of all exprs for which we've already set up term-specific axioms -- // this prevents infinite recursive descent with respect to axioms that @@ -135,6 +136,7 @@ namespace smt { std::set variable_set; std::set internal_variable_set; + std::set regex_variable_set; std::map > internal_variable_scope_levels; obj_hashtable internal_lenTest_vars; @@ -180,6 +182,7 @@ namespace smt { app * mk_nonempty_str_var(); app * mk_internal_xor_var(); expr * mk_internal_valTest_var(expr * node, int len, int vTries); + app * mk_regex_rep_var(); bool is_concat(app const * a) const { return a->is_app_of(get_id(), OP_STRCAT); } bool is_concat(enode const * n) const { return is_concat(n->get_owner()); } @@ -206,6 +209,18 @@ namespace smt { bool is_Replace(app const * a) const { return a->is_app_of(get_id(), OP_STR_REPLACE); } bool is_Replace(enode const * n) const { return is_Replace(n->get_owner()); } + bool is_RegexIn(app const * a) const { return a->is_app_of(get_id(), OP_RE_REGEXIN); } + bool is_RegexIn(enode const * n) const { return is_RegexIn(n->get_owner()); } + bool is_RegexConcat(app const * a) const { return a->is_app_of(get_id(), OP_RE_REGEXCONCAT); } + bool is_RegexConcat(enode const * n) const { return is_RegexConcat(n->get_owner()); } + bool is_RegexStar(app const * a) const { return a->is_app_of(get_id(), OP_RE_REGEXSTAR); } + bool is_RegexStar(enode const * n) const { return is_RegexStar(n->get_owner()); } + bool is_RegexUnion(app const * a) const { return a->is_app_of(get_id(), OP_RE_REGEXUNION); } + bool is_RegexUnion(enode const * n) const { return is_RegexUnion(n->get_owner()); } + bool is_Str2Reg(app const * a) const { return a->is_app_of(get_id(), OP_RE_STR2REGEX); } + bool is_Str2Reg(enode const * n) const { return is_Str2Reg(n->get_owner()); } + + void instantiate_concat_axiom(enode * cat); void instantiate_basic_string_axioms(enode * str); void instantiate_str_eq_length_axiom(enode * lhs, enode * rhs); @@ -220,6 +235,9 @@ namespace smt { void instantiate_axiom_Substr(enode * e); void instantiate_axiom_Replace(enode * e); + expr * mk_RegexIn(expr * str, expr * regexp); + void instantiate_axiom_RegexIn(enode * e); + void set_up_axioms(expr * ex); void handle_equality(expr * lhs, expr * rhs); From 020e8aef6df7cbec0a19175a99385111289bbb4d Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 23 Jun 2016 17:14:03 -0400 Subject: [PATCH 130/562] regex union --- src/smt/theory_str.cpp | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ab7bb13ef..46248abd2 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1328,26 +1328,21 @@ void theory_str::instantiate_axiom_RegexIn(enode * e) { expr_ref finalAxiom(mk_and(items), m); SASSERT(finalAxiom); assert_axiom(finalAxiom); - /* - Z3_ast var1 = mk_regexRepVar(t); - Z3_ast var2 = mk_regexRepVar(t); - rhs = mk_concat(t, var1, var2); - - Z3_ast regex1 = Z3_get_app_arg(ctx, arg1_func_app, 0); - Z3_ast regex2 = Z3_get_app_arg(ctx, arg1_func_app, 1); - Z3_ast var1InRegex1 = mk_2_arg_app(ctx, td->RegexIn, var1, regex1); - Z3_ast var2InRegex2 = mk_2_arg_app(ctx, td->RegexIn, var2, regex2); - std::vector items; + } else if (is_RegexUnion(regex)) { + expr_ref var1(mk_regex_rep_var(), m); + expr_ref var2(mk_regex_rep_var(), m); + expr_ref orVar(m.mk_or(ctx.mk_eq_atom(str, var1), ctx.mk_eq_atom(str, var2)), m); + expr_ref regex1(regex->get_arg(0), m); + expr_ref regex2(regex->get_arg(1), m); + expr_ref var1InRegex1(mk_RegexIn(var1, regex1), m); + expr_ref var2InRegex2(mk_RegexIn(var2, regex2), m); + expr_ref_vector items(m); items.push_back(var1InRegex1); items.push_back(var2InRegex2); - items.push_back(Z3_mk_eq(ctx, resBoolVar, Z3_mk_eq(ctx, args[0], rhs))); - extraAssert = mk_and_fromVector(t, items); - return resBoolVar; - */ - } else if (is_RegexUnion(regex)) { - + items.push_back(ctx.mk_eq_atom(expr, orVar)); + assert_axiom(mk_and(items)); } else if (is_RegexStar(regex)) { - + NOT_IMPLEMENTED_YET(); } else { TRACE("t_str_detail", tout << "ERROR: unknown regex expression " << mk_pp(regex, m) << "!" << std::endl;); NOT_IMPLEMENTED_YET(); From b31d1a92aa1627c45cb8d3708db292dfce3333a1 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 27 Jun 2016 14:41:57 -0400 Subject: [PATCH 131/562] add more support for unroll (WIP) --- src/ast/str_decl_plugin.cpp | 7 ++ src/ast/str_decl_plugin.h | 2 + src/smt/theory_str.cpp | 150 +++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 9 ++- 4 files changed, 165 insertions(+), 3 deletions(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index b140e11c3..ef94272c7 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -41,6 +41,7 @@ str_decl_plugin::str_decl_plugin(): m_re_regexconcat_decl(0), m_re_regexstar_decl(0), m_re_regexunion_decl(0), + m_re_unroll_decl(0), m_arith_plugin(0), m_arith_fid(0), m_int_sort(0){ @@ -69,6 +70,7 @@ void str_decl_plugin::finalize(void) { DEC_REF(m_re_regexconcat_decl); DEC_REF(m_re_regexstar_decl); DEC_REF(m_re_regexunion_decl); + DEC_REF(m_re_unroll_decl); DEC_REF(m_int_sort); } @@ -154,6 +156,9 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_re_regexunion_decl = m->mk_func_decl(symbol("RegexUnion"), re, re, re, func_decl_info(id, OP_RE_REGEXUNION)); m_manager->inc_ref(m_re_regexunion_decl); + m_re_unroll_decl = m->mk_func_decl(symbol("Unroll"), re, i, s, func_decl_info(id, OP_RE_UNROLL)); + m_manager->inc_ref(m_re_unroll_decl); + } decl_plugin * str_decl_plugin::mk_fresh() { @@ -186,6 +191,7 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { case OP_RE_REGEXCONCAT: return m_re_regexconcat_decl; case OP_RE_REGEXSTAR: return m_re_regexstar_decl; case OP_RE_REGEXUNION: return m_re_regexunion_decl; + case OP_RE_UNROLL: return m_re_unroll_decl; default: return 0; } } @@ -256,6 +262,7 @@ void str_decl_plugin::get_op_names(svector & op_names, symbol cons op_names.push_back(builtin_name("RegexConcat", OP_RE_REGEXCONCAT)); op_names.push_back(builtin_name("RegexStar", OP_RE_REGEXSTAR)); op_names.push_back(builtin_name("RegexUnion", OP_RE_REGEXUNION)); + op_names.push_back(builtin_name("Unroll", OP_RE_UNROLL)); } void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index ccd2915af..c2ad088a4 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -47,6 +47,7 @@ enum str_op_kind { OP_RE_REGEXCONCAT, OP_RE_REGEXSTAR, OP_RE_REGEXUNION, + OP_RE_UNROLL, // end LAST_STR_OP }; @@ -75,6 +76,7 @@ protected: func_decl * m_re_regexconcat_decl; func_decl * m_re_regexstar_decl; func_decl * m_re_regexunion_decl; + func_decl * m_re_unroll_decl; arith_decl_plugin * m_arith_plugin; family_id m_arith_fid; diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 46248abd2..947c35f98 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -454,6 +454,10 @@ app * theory_str::mk_int_var(std::string name) { return a; } +app * theory_str::mk_unroll_bound_var() { + return mk_int_var("unroll"); +} + app * theory_str::mk_str_var(std::string name) { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -545,6 +549,24 @@ app * theory_str::mk_nonempty_str_var() { return a; } +app * theory_str::mk_unroll(expr * n, expr * bound) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + expr * args[2] = {n, bound}; + app * unrollFunc = get_manager().mk_app(get_id(), OP_RE_UNROLL, 0, 0, 2, args); + + expr_ref_vector items(m); + items.push_back(ctx.mk_eq_atom(ctx.mk_eq_atom(bound, mk_int(0)), ctx.mk_eq_atom(unrollFunc, m_strutil.mk_string("")))); + items.push_back(m_autil.mk_ge(bound, mk_int(0))); + items.push_back(m_autil.mk_ge(mk_strlen(unrollFunc), mk_int(0))); + + expr_ref finalAxiom(mk_and(items), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); + return unrollFunc; +} + app * theory_str::mk_contains(expr * haystack, expr * needle) { expr * args[2] = {haystack, needle}; app * contains = get_manager().mk_app(get_id(), OP_STR_CONTAINS, 0, 0, 2, args); @@ -1342,7 +1364,16 @@ void theory_str::instantiate_axiom_RegexIn(enode * e) { items.push_back(ctx.mk_eq_atom(expr, orVar)); assert_axiom(mk_and(items)); } else if (is_RegexStar(regex)) { - NOT_IMPLEMENTED_YET(); + // slightly more complex due to the unrolling step. + expr_ref regex1(regex->get_arg(0), m); + expr_ref unrollCount(mk_unroll_bound_var(), m); + expr_ref unrollFunc(mk_unroll(regex1, unrollCount), m); + expr_ref_vector items(m); + items.push_back(ctx.mk_eq_atom(expr, ctx.mk_eq_atom(str, unrollFunc))); + items.push_back(ctx.mk_eq_atom(ctx.mk_eq_atom(unrollCount, mk_int(0)), ctx.mk_eq_atom(unrollFunc, m_strutil.mk_string("")))); + expr_ref finalAxiom(mk_and(items), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); } else { TRACE("t_str_detail", tout << "ERROR: unknown regex expression " << mk_pp(regex, m) << "!" << std::endl;); NOT_IMPLEMENTED_YET(); @@ -3368,6 +3399,63 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); } +void theory_str::process_unroll_eq_const_str(expr * unrollFunc, expr * constStr) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + if (!is_Unroll(to_app(unrollFunc))) { + return; + } + if (!m_strutil.is_string(constStr)) { + return; + } + + expr * funcInUnroll = to_app(unrollFunc)->get_arg(0); + std::string strValue = m_strutil.get_string_constant_value(constStr); + + TRACE("t_str_detail", tout << "unrollFunc: " << mk_pp(unrollFunc, m) << std::endl + << "constStr: " << mk_pp(constStr, m) << std::endl;); + + if (strValue == "") { + return; + } + + if (is_Str2Reg(to_app(funcInUnroll))) { + unroll_str2reg_constStr(unrollFunc, constStr); + return; + } +} + +void theory_str::unroll_str2reg_constStr(expr * unrollFunc, expr * eqConstStr) { + context & ctx = get_context(); + expr * str2RegFunc = to_app(unrollFunc)->get_arg(0); + expr * strInStr2RegFunc = to_app(str2RegFunc)->get_arg(0); + expr * oriCnt = to_app(unrollFunc)->get_arg(1); + + // TODO NEXT + NOT_IMPLEMENTED_YET(); + + /* + Z3_context ctx = Z3_theory_get_context(t); + Z3_ast str2RegFunc = Z3_get_app_arg(ctx, Z3_to_app(ctx, unrollFunc), 0); + Z3_ast strInStr2RegFunc = Z3_get_app_arg(ctx, Z3_to_app(ctx, str2RegFunc), 0); + Z3_ast oriCnt = Z3_get_app_arg(ctx, Z3_to_app(ctx, unrollFunc), 1); + + std::string strValue = getConstStrValue(t, eqConstStr); + std::string regStrValue = getConstStrValue(t, strInStr2RegFunc); + int strLen = strValue.length(); + int regStrLen = regStrValue.length(); + int cnt = strLen / regStrLen; + + Z3_ast implyL = Z3_mk_eq(ctx, unrollFunc, eqConstStr); + Z3_ast implyR1 = Z3_mk_eq(ctx, oriCnt, mk_int(ctx, cnt)); + Z3_ast implyR2 = Z3_mk_eq(ctx, mk_length(t, unrollFunc), mk_int(ctx, strLen)); + Z3_ast toAssert = Z3_mk_implies(ctx, implyL, mk_2_and(t, implyR1, implyR2)); + + addAxiom(t, toAssert, __LINE__); + */ +} + /* * Look through the equivalence class of n to find a string constant. * Return that constant if it is found, and set hasEqcValue to true. @@ -3392,6 +3480,26 @@ expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { return n; } +void theory_str::get_eqc_all_unroll(expr * n, expr * & constStr, std::set & unrollFuncSet) { + context & ctx = get_context(); + + constStr = NULL; + unrollFuncSet.clear(); + + // iterate over the eqc of 'n' + enode * n_enode = ctx.get_enode(n); + enode * e_curr = n_enode; + do { + app * curr = e_curr->get_owner(); + if (m_strutil.is_string(curr)) { + constStr = curr; + } else if (is_Unroll(curr)) { + unrollFuncSet.insert(curr); + } + e_curr = e_curr->get_next(); + } while (e_curr != n_enode); +} + // from Z3: theory_seq.cpp static theory_mi_arith* get_th_arith(context& ctx, theory_id afid, expr* e) { @@ -4198,7 +4306,45 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { simplify_parent(lhs, rhs_value); } - // TODO regex unroll? (much later) + // regex unroll + /* + Z3_ast nn1EqConst = NULL; + std::set nn1EqUnrollFuncs; + get_eqc_allUnroll(t, nn1, nn1EqConst, nn1EqUnrollFuncs); + Z3_ast nn2EqConst = NULL; + std::set nn2EqUnrollFuncs; + get_eqc_allUnroll(t, nn2, nn2EqConst, nn2EqUnrollFuncs); + + if (nn2EqConst != NULL) { + for (std::set::iterator itor1 = nn1EqUnrollFuncs.begin(); itor1 != nn1EqUnrollFuncs.end(); itor1++) { + processUnrollEqConstStr(t, *itor1, nn2EqConst); + } + } + + if (nn1EqConst != NULL) { + for (std::set::iterator itor2 = nn2EqUnrollFuncs.begin(); itor2 != nn2EqUnrollFuncs.end(); itor2++) { + processUnrollEqConstStr(t, *itor2, nn1EqConst); + } + } + */ + expr * nn1EqConst = NULL; + std::set nn1EqUnrollFuncs; + get_eqc_all_unroll(lhs, nn1EqConst, nn1EqUnrollFuncs); + expr * nn2EqConst = NULL; + std::set nn2EqUnrollFuncs; + get_eqc_all_unroll(rhs, nn2EqConst, nn2EqUnrollFuncs); + + if (nn2EqConst != NULL) { + for (std::set::iterator itor1 = nn1EqUnrollFuncs.begin(); itor1 != nn1EqUnrollFuncs.end(); itor1++) { + process_unroll_eq_const_str(*itor1, nn2EqConst); + } + } + + if (nn1EqConst != NULL) { + for (std::set::iterator itor2 = nn2EqUnrollFuncs.begin(); itor2 != nn2EqUnrollFuncs.end(); itor2++) { + process_unroll_eq_const_str(*itor2, nn1EqConst); + } + } } void theory_str::set_up_axioms(expr * ex) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 9aead1105..5bf30a266 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -183,6 +183,7 @@ namespace smt { app * mk_internal_xor_var(); expr * mk_internal_valTest_var(expr * node, int len, int vTries); app * mk_regex_rep_var(); + app * mk_unroll_bound_var(); bool is_concat(app const * a) const { return a->is_app_of(get_id(), OP_STRCAT); } bool is_concat(enode const * n) const { return is_concat(n->get_owner()); } @@ -219,7 +220,8 @@ namespace smt { bool is_RegexUnion(enode const * n) const { return is_RegexUnion(n->get_owner()); } bool is_Str2Reg(app const * a) const { return a->is_app_of(get_id(), OP_RE_STR2REGEX); } bool is_Str2Reg(enode const * n) const { return is_Str2Reg(n->get_owner()); } - + bool is_Unroll(app const * a) const { return a->is_app_of(get_id(), OP_RE_UNROLL); } + bool is_Unroll(enode const * n) const { return is_Unroll(n->get_owner()); } void instantiate_concat_axiom(enode * cat); void instantiate_basic_string_axioms(enode * str); @@ -237,6 +239,11 @@ namespace smt { expr * mk_RegexIn(expr * str, expr * regexp); void instantiate_axiom_RegexIn(enode * e); + app * mk_unroll(expr * n, expr * bound); + + void get_eqc_all_unroll(expr * n, expr * & constStr, std::set & unrollFuncSet); + void process_unroll_eq_const_str(expr * unrollFunc, expr * constStr); + void unroll_str2reg_constStr(expr * unrollFunc, expr * eqConstStr); void set_up_axioms(expr * ex); void handle_equality(expr * lhs, expr * rhs); From 03827cb487bc1e083c628c0d9997f4352b93edb5 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 30 Jun 2016 01:21:21 -0400 Subject: [PATCH 132/562] add more Unroll support to final_check, ctx_dep_analysis --- src/smt/theory_str.cpp | 256 +++++++++++++++++++++++++---------------- src/smt/theory_str.h | 5 +- 2 files changed, 163 insertions(+), 98 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 947c35f98..83ce88b36 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3426,34 +3426,104 @@ void theory_str::process_unroll_eq_const_str(expr * unrollFunc, expr * constStr) } } +void theory_str::process_concat_eq_unroll(expr * concat, expr * unroll) { + // TODO NEXT + NOT_IMPLEMENTED_YET(); + /* +#ifdef DEBUGLOG + __debugPrint(logFile, ">> processConcatEqUnroll: \n"); + __debugPrint(logFile, " * [concat] "); + printZ3Node(t, concat); + __debugPrint(logFile, "\n"); + __debugPrint(logFile, " * [unroll] "); + printZ3Node(t, unroll); + __debugPrint(logFile, "\n\n"); +#endif + + Z3_context ctx = Z3_theory_get_context(t); + std::pair key = std::make_pair(concat, unroll); + Z3_ast toAssert = NULL; + + if (concatEqUnroll_AstMap.find(key) == concatEqUnroll_AstMap.end()) { + Z3_ast arg1 = Z3_get_app_arg(ctx, Z3_to_app(ctx, concat), 0); + Z3_ast arg2 = Z3_get_app_arg(ctx, Z3_to_app(ctx, concat), 1); + Z3_ast r1 = Z3_get_app_arg(ctx, Z3_to_app(ctx, unroll), 0); + Z3_ast t1 = Z3_get_app_arg(ctx, Z3_to_app(ctx, unroll), 1); + + Z3_ast v1 = mk_regexRepVar(t); + Z3_ast v2 = mk_regexRepVar(t); + Z3_ast v3 = mk_regexRepVar(t); + Z3_ast v4 = mk_regexRepVar(t); + Z3_ast v5 = mk_regexRepVar(t); + + Z3_ast t2 = mk_unrollBoundVar(t); + Z3_ast t3 = mk_unrollBoundVar(t); + Z3_ast emptyStr = my_mk_str_value(t, ""); + + Z3_ast unroll1 = mk_unroll(t, r1, t2); + Z3_ast unroll2 = mk_unroll(t, r1, t3); + + Z3_ast op0 = Z3_mk_eq(ctx, t1, mk_int(ctx, 0)); + Z3_ast op1 = Z3_mk_ge(ctx, t1, mk_int(ctx, 1)); + + std::vector op1Items; + std::vector op2Items; + + op1Items.push_back(Z3_mk_eq(ctx, arg1, emptyStr)); + op1Items.push_back(Z3_mk_eq(ctx, arg2, emptyStr)); + op1Items.push_back(Z3_mk_eq(ctx, mk_length(t, arg1), mk_int(ctx, 0))); + op1Items.push_back(Z3_mk_eq(ctx, mk_length(t, arg2), mk_int(ctx, 0))); + Z3_ast opAnd1 = Z3_mk_eq(ctx, op0, mk_and_fromVector(t, op1Items)); + + Z3_ast v1v2 = mk_concat(t, v1, v2); + op2Items.push_back(Z3_mk_eq(ctx, arg1, v1v2)); + op2Items.push_back(Z3_mk_eq(ctx, mk_length(t, arg1), mk_2_add(t, mk_length(t, v1), mk_length(t, v2)))); + Z3_ast v3v4 = mk_concat(t, v3, v4); + op2Items.push_back(Z3_mk_eq(ctx, arg2, v3v4)); + op2Items.push_back(Z3_mk_eq(ctx, mk_length(t, arg2), mk_2_add(t, mk_length(t, v3), mk_length(t, v4)))); + + op2Items.push_back(Z3_mk_eq(ctx, v1, unroll1)); + op2Items.push_back(Z3_mk_eq(ctx, mk_length(t, v1), mk_length(t, unroll1))); + op2Items.push_back(Z3_mk_eq(ctx, v4, unroll2)); + op2Items.push_back(Z3_mk_eq(ctx, mk_length(t, v4), mk_length(t, unroll2))); + Z3_ast v2v3 = mk_concat(t, v2, v3); + op2Items.push_back(Z3_mk_eq(ctx, v5, v2v3)); + reduceVirtualRegexIn(t, v5, r1, op2Items); + op2Items.push_back(Z3_mk_eq(ctx, mk_length(t, v5), mk_2_add(t, mk_length(t, v2), mk_length(t, v3)))); + op2Items.push_back(Z3_mk_eq(ctx, mk_2_add(t, t2, t3), mk_2_sub(t, t1, mk_int(ctx, 1)))); + Z3_ast opAnd2 = Z3_mk_eq(ctx, op1, mk_and_fromVector(t, op2Items)); + + toAssert = mk_2_and(t, opAnd1, opAnd2); + concatEqUnroll_AstMap[key] = toAssert; + } else { + toAssert = concatEqUnroll_AstMap[key]; + } + + addAxiom(t, toAssert, __LINE__); + */ +} + void theory_str::unroll_str2reg_constStr(expr * unrollFunc, expr * eqConstStr) { context & ctx = get_context(); + ast_manager & m = get_manager(); + expr * str2RegFunc = to_app(unrollFunc)->get_arg(0); expr * strInStr2RegFunc = to_app(str2RegFunc)->get_arg(0); expr * oriCnt = to_app(unrollFunc)->get_arg(1); - // TODO NEXT - NOT_IMPLEMENTED_YET(); - - /* - Z3_context ctx = Z3_theory_get_context(t); - Z3_ast str2RegFunc = Z3_get_app_arg(ctx, Z3_to_app(ctx, unrollFunc), 0); - Z3_ast strInStr2RegFunc = Z3_get_app_arg(ctx, Z3_to_app(ctx, str2RegFunc), 0); - Z3_ast oriCnt = Z3_get_app_arg(ctx, Z3_to_app(ctx, unrollFunc), 1); - - std::string strValue = getConstStrValue(t, eqConstStr); - std::string regStrValue = getConstStrValue(t, strInStr2RegFunc); + std::string strValue = m_strutil.get_string_constant_value(eqConstStr); + std::string regStrValue = m_strutil.get_string_constant_value(strInStr2RegFunc); int strLen = strValue.length(); int regStrLen = regStrValue.length(); - int cnt = strLen / regStrLen; + int cnt = strLen / regStrLen; // TODO prevent DIV/0 on regStrLen - Z3_ast implyL = Z3_mk_eq(ctx, unrollFunc, eqConstStr); - Z3_ast implyR1 = Z3_mk_eq(ctx, oriCnt, mk_int(ctx, cnt)); - Z3_ast implyR2 = Z3_mk_eq(ctx, mk_length(t, unrollFunc), mk_int(ctx, strLen)); - Z3_ast toAssert = Z3_mk_implies(ctx, implyL, mk_2_and(t, implyR1, implyR2)); - - addAxiom(t, toAssert, __LINE__); - */ + expr_ref implyL(ctx.mk_eq_atom(unrollFunc, eqConstStr), m); + expr_ref implyR1(ctx.mk_eq_atom(oriCnt, mk_int(cnt)), m); + expr_ref implyR2(ctx.mk_eq_atom(mk_strlen(unrollFunc), mk_int(strLen)), m); + expr_ref axiomRHS(m.mk_and(implyR1, implyR2), m); + SASSERT(implyL); + SASSERT(axiomRHS); + assert_implication(implyL, axiomRHS); } /* @@ -4628,7 +4698,7 @@ void theory_str::classify_ast_by_type(expr * node, std::map & varMap if (canskip == 0 && concatMap.find(node) == concatMap.end()) { concatMap[node] = 1; } - } else if (false) { // TODO is_unroll() + } else if (is_Unroll(aNode)) { // Unroll if (unrollMap.find(node) == unrollMap.end()) { unrollMap[node] = 1; @@ -4696,9 +4766,12 @@ void theory_str::trace_ctx_dep(std::ofstream & tout, std::map & aliasIndexMap, std::map & var_eq_constStr_map, std::map > & var_eq_concat_map, + std::map > & var_eq_unroll_map, std::map & concat_eq_constStr_map, - std::map > & concat_eq_concat_map) { + std::map > & concat_eq_concat_map, + std::map > & unrollGroupMap) { #ifdef _TRACE + context & ctx = get_context(); ast_manager & mgr = get_manager(); { tout << "(0) alias: variables" << std::endl; @@ -4754,24 +4827,21 @@ void theory_str::trace_ctx_dep(std::ofstream & tout, } tout << std::endl; } -/*// TODO + { - __debugPrint(logFile, "(3) var = unrollFunc:\n"); - std::map >::iterator itor2 = var_eq_unroll_map.begin(); + tout << "(3) var = unrollFunc:" << std::endl; + std::map >::iterator itor2 = var_eq_unroll_map.begin(); for (; itor2 != var_eq_unroll_map.end(); itor2++) { - __debugPrint(logFile, " * "); - printZ3Node(t, itor2->first); - __debugPrint(logFile, " = { "); - std::map::iterator i_itor = itor2->second.begin(); + tout << " * " << mk_pp(itor2->first, mgr) << " = { "; + std::map::iterator i_itor = itor2->second.begin(); for (; i_itor != itor2->second.end(); i_itor++) { - printZ3Node(t, i_itor->first); - __debugPrint(logFile, ", "); + tout << mk_pp(i_itor->first, mgr) << ", "; } - __debugPrint(logFile, " }\n"); + tout << " }" << std::endl; } - __debugPrint(logFile, "\n"); + tout << std::endl; } -*/ + { tout << "(4) concat = constStr:" << std::endl; std::map::iterator itor3 = concat_eq_constStr_map.begin(); @@ -4802,44 +4872,41 @@ void theory_str::trace_ctx_dep(std::ofstream & tout, } tout << std::endl; } -/*// TODO + { - __debugPrint(logFile, "(6) eq unrolls:\n"); - std::map >::iterator itor5 = unrollGroupMap.begin(); + tout << "(6) eq unrolls:" << std::endl; + std::map >::iterator itor5 = unrollGroupMap.begin(); for (; itor5 != unrollGroupMap.end(); itor5++) { - __debugPrint(logFile, " * "); - std::set::iterator i_itor = itor5->second.begin(); + tout << " * "; + std::set::iterator i_itor = itor5->second.begin(); for (; i_itor != itor5->second.end(); i_itor++) { - printZ3Node(t, *i_itor); - __debugPrint(logFile, ", "); + tout << mk_pp(*i_itor, mgr) << ", "; } - __debugPrint(logFile, "\n"); + tout << std::endl; } - __debugPrint(logFile, "\n"); + tout << std::endl; } { - __debugPrint(logFile, "(7) unroll = concats:\n"); - std::map >::iterator itor5 = unrollGroupMap.begin(); + tout << "(7) unroll = concats:" << std::endl; + std::map >::iterator itor5 = unrollGroupMap.begin(); for (; itor5 != unrollGroupMap.end(); itor5++) { - __debugPrint(logFile, " * "); - Z3_ast unroll = itor5->first; - printZ3Node(t, unroll); - __debugPrint(logFile, "\n"); - Z3_ast curr = unroll; + tout << " * "; + expr * unroll = itor5->first; + tout << mk_pp(unroll, mgr) << std::endl; + enode * e_curr = ctx.get_enode(unroll); + enode * e_curr_end = e_curr; do { - if (isConcatFunc(t, curr)) { - __debugPrint(logFile, " >>> "); - printZ3Node(t, curr); - __debugPrint(logFile, "\n"); + app * curr = e_curr->get_owner(); + if (is_concat(curr)) { + tout << " >>> " << mk_pp(curr, mgr) << std::endl; } - curr = Z3_theory_get_eqc_next(t, curr); - }while (curr != unroll); - __debugPrint(logFile, "\n"); + e_curr = e_curr->get_next(); + } while (e_curr != e_curr_end); + tout << std::endl; } - __debugPrint(logFile, "\n"); + tout << std::endl; } - */ #else return; #endif // _TRACE @@ -4889,32 +4956,32 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map aliasUnrollSet; - std::map::iterator unrollItor = unrollMap.begin(); - for (; unrollItor != unrollMap.end(); unrollItor++) { - if (aliasUnrollSet.find(unrollItor->first) != aliasUnrollSet.end()) - continue; - Z3_ast aRoot = NULL; - Z3_ast curr = unrollItor->first; - do { - if (isUnrollFunc(t, curr)) { - if (aRoot == NULL) { - aRoot = curr; - } - aliasUnrollSet[curr] = aRoot; - } - curr = Z3_theory_get_eqc_next(t, curr); - } while (curr != unrollItor->first); + std::map aliasUnrollSet; + std::map::iterator unrollItor = unrollMap.begin(); + for (; unrollItor != unrollMap.end(); ++unrollItor) { + if (aliasUnrollSet.find(unrollItor->first) != aliasUnrollSet.end()) { + continue; + } + expr * aRoot = NULL; + enode * e_currEqc = ctx.get_enode(unrollItor->first); + enode * e_curr = e_currEqc; + do { + app * curr = e_currEqc->get_owner(); + if (is_Unroll(curr)) { + if (aRoot == NULL) { + aRoot = curr; + } + aliasUnrollSet[curr] = aRoot; + } + e_currEqc = e_currEqc->get_next(); + } while (e_currEqc != e_curr); } for (unrollItor = unrollMap.begin(); unrollItor != unrollMap.end(); unrollItor++) { - Z3_ast unrFunc = unrollItor->first; - Z3_ast urKey = aliasUnrollSet[unrFunc]; + expr * unrFunc = unrollItor->first; + expr * urKey = aliasUnrollSet[unrFunc]; unrollGroupMap[urKey].insert(unrFunc); } - */ // Step 2: collect alias relation // e.g. suppose we have the equivalence class {x, y, z}; @@ -4999,13 +5066,9 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map & strVarMap, std::map 0) { - computeContains(t, aliasIndexMap, concats_eq_Index_map, var_eq_constStr_map, concat_eq_constStr_map, var_eq_concat_map); + NOT_IMPLEMENTED_YET(); + compute_contains(aliasIndexMap, concats_eq_Index_map, var_eq_constStr_map, concat_eq_constStr_map, var_eq_concat_map); } */ @@ -5638,17 +5702,14 @@ final_check_status theory_str::final_check_eh() { ); } - // TODO process_concat_eq_unroll() - /* for (std::map >::iterator fvIt2 = concatFreeArgsEqUnrollsMap.begin(); fvIt2 != concatFreeArgsEqUnrollsMap.end(); fvIt2++) { expr * concat = fvIt2->first; for (std::set::iterator urItor = fvIt2->second.begin(); urItor != fvIt2->second.end(); urItor++) { - Z3_ast unroll = *urItor; - processConcatEqUnroll(concat, unroll); + expr * unroll = *urItor; + process_concat_eq_unroll(concat, unroll); } } - */ // -------- // experimental free variable assignment - begin @@ -5675,12 +5736,13 @@ final_check_status theory_str::final_check_eh() { } // experimental free variable assignment - end - // TODO more unroll stuff - /* + // more unroll stuff for (std::map >::iterator fvIt1 = fv_unrolls_map.begin(); fvIt1 != fv_unrolls_map.end(); fvIt1++) { - Z3_ast var = fvIt1->first; + expr * var = fvIt1->first; fSimpUnroll.clear(); + NOT_IMPLEMENTED_YET(); // TODO complete this unroll block + /* get_eqc_simpleUnroll(t, var, constValue, fSimpUnroll); if (fSimpUnroll.size() == 0) { genAssignUnrollReg(t, fv_unrolls_map[var]); @@ -5690,8 +5752,8 @@ final_check_status theory_str::final_check_eh() { addAxiom(t, toAssert, __LINE__); } } + */ } - */ if (opt_VerifyFinalCheckProgress && !finalCheckProgressIndicator) { TRACE("t_str", tout << "BUG: no progress in final check, giving up!!" << std::endl;); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 5bf30a266..467727179 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -244,6 +244,7 @@ namespace smt { void get_eqc_all_unroll(expr * n, expr * & constStr, std::set & unrollFuncSet); void process_unroll_eq_const_str(expr * unrollFunc, expr * constStr); void unroll_str2reg_constStr(expr * unrollFunc, expr * eqConstStr); + void process_concat_eq_unroll(expr * concat, expr * unroll); void set_up_axioms(expr * ex); void handle_equality(expr * lhs, expr * rhs); @@ -296,8 +297,10 @@ namespace smt { std::map & aliasIndexMap, std::map & var_eq_constStr_map, std::map > & var_eq_concat_map, + std::map > & var_eq_unroll_map, std::map & concat_eq_constStr_map, - std::map > & concat_eq_concat_map); + std::map > & concat_eq_concat_map, + std::map > & unrollGroupMap); void classify_ast_by_type(expr * node, std::map & varMap, std::map & concatMap, std::map & unrollMap); From 21f0a50abaee4c6d365a910928c46bfe4bae8d58 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 30 Jun 2016 01:24:43 -0400 Subject: [PATCH 133/562] add Unroll check to get_eqc_allUnroll --- src/smt/theory_str.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 83ce88b36..b3621f61e 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -6406,7 +6406,7 @@ void theory_str::get_eqc_allUnroll(expr * n, expr * &constStr, std::set & do { if (is_string(to_app(curr))) { constStr = curr; - } else if (false) /*(td->Unroll == Z3_get_app_decl(ctx, Z3_to_app(ctx, curr)))*/ { // TODO + } else if (is_Unroll(to_app(curr))) { if (unrollFuncSet.find(curr) == unrollFuncSet.end()) { unrollFuncSet.insert(curr); } From 427632ede398c354abd0141a07d905626699643f Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 30 Jun 2016 01:42:00 -0400 Subject: [PATCH 134/562] let free variable assignment work a bit more towards unrolls --- src/smt/theory_str.cpp | 142 ++++++++++++++++++++++++++++++++--------- src/smt/theory_str.h | 5 +- 2 files changed, 117 insertions(+), 30 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index b3621f61e..64f3d1fc8 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3550,26 +3550,6 @@ expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { return n; } -void theory_str::get_eqc_all_unroll(expr * n, expr * & constStr, std::set & unrollFuncSet) { - context & ctx = get_context(); - - constStr = NULL; - unrollFuncSet.clear(); - - // iterate over the eqc of 'n' - enode * n_enode = ctx.get_enode(n); - enode * e_curr = n_enode; - do { - app * curr = e_curr->get_owner(); - if (m_strutil.is_string(curr)) { - constStr = curr; - } else if (is_Unroll(curr)) { - unrollFuncSet.insert(curr); - } - e_curr = e_curr->get_next(); - } while (e_curr != n_enode); -} - // from Z3: theory_seq.cpp static theory_mi_arith* get_th_arith(context& ctx, theory_id afid, expr* e) { @@ -4399,10 +4379,10 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { */ expr * nn1EqConst = NULL; std::set nn1EqUnrollFuncs; - get_eqc_all_unroll(lhs, nn1EqConst, nn1EqUnrollFuncs); + get_eqc_allUnroll(lhs, nn1EqConst, nn1EqUnrollFuncs); expr * nn2EqConst = NULL; std::set nn2EqUnrollFuncs; - get_eqc_all_unroll(rhs, nn2EqConst, nn2EqUnrollFuncs); + get_eqc_allUnroll(rhs, nn2EqConst, nn2EqUnrollFuncs); if (nn2EqConst != NULL) { for (std::set::iterator itor1 = nn1EqUnrollFuncs.begin(); itor1 != nn1EqUnrollFuncs.end(); itor1++) { @@ -5741,18 +5721,15 @@ final_check_status theory_str::final_check_eh() { fvIt1 != fv_unrolls_map.end(); fvIt1++) { expr * var = fvIt1->first; fSimpUnroll.clear(); - NOT_IMPLEMENTED_YET(); // TODO complete this unroll block - /* - get_eqc_simpleUnroll(t, var, constValue, fSimpUnroll); + get_eqc_simpleUnroll(var, constValue, fSimpUnroll); if (fSimpUnroll.size() == 0) { - genAssignUnrollReg(t, fv_unrolls_map[var]); + gen_assign_unroll_reg(fv_unrolls_map[var]); } else { - Z3_ast toAssert = genAssignUnrollStr2Reg(t, var, fSimpUnroll); + expr * toAssert = gen_assign_unroll_Str2Reg(var, fSimpUnroll); if (toAssert != NULL) { - addAxiom(t, toAssert, __LINE__); + assert_axiom(toAssert); } } - */ } if (opt_VerifyFinalCheckProgress && !finalCheckProgressIndicator) { @@ -6037,6 +6014,89 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, } } +void theory_str::gen_assign_unroll_reg(std::set & unrolls) { + // TODO + NOT_IMPLEMENTED_YET(); +} + +static int computeGCD(int x, int y) { + if (x == 0) { + return y; + } + while (y != 0) { + if (x > y) { + x = x - y; + } else { + y = y - x; + } + } + return x; +} + +static int computeLCM(int a, int b) { + int temp = computeGCD(a, b); + return temp ? (a / temp * b) : 0; +} + +static std::string get_unrolled_string(std::string core, int count) { + std::string res = ""; + for (int i = 0; i < count; i++) { + res += core; + } + return res; +} + +expr * theory_str::gen_assign_unroll_Str2Reg(expr * n, std::set & unrolls) { + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + + int lcm = 1; + int coreValueCount = 0; + expr * oneUnroll = NULL; + std::string oneCoreStr = ""; + for (std::set::iterator itor = unrolls.begin(); itor != unrolls.end(); itor++) { + expr * str2RegFunc = to_app(*itor)->get_arg(0); + expr * coreVal = to_app(str2RegFunc)->get_arg(0); + std::string coreStr = m_strutil.get_string_constant_value(coreVal); + if (oneUnroll == NULL) { + oneUnroll = *itor; + oneCoreStr = coreStr; + } + coreValueCount++; + int core1Len = coreStr.length(); + lcm = computeLCM(lcm, core1Len); + } + // + bool canHaveNonEmptyAssign = true; + expr_ref_vector litems(mgr); + std::string lcmStr = get_unrolled_string(oneCoreStr, (lcm / oneCoreStr.length())); + for (std::set::iterator itor = unrolls.begin(); itor != unrolls.end(); itor++) { + expr * str2RegFunc = to_app(*itor)->get_arg(0); + expr * coreVal = to_app(str2RegFunc)->get_arg(0); + std::string coreStr = m_strutil.get_string_constant_value(coreVal); + int core1Len = coreStr.length(); + std::string uStr = get_unrolled_string(coreStr, (lcm / core1Len)); + if (uStr != lcmStr) { + canHaveNonEmptyAssign = false; + } + litems.push_back(ctx.mk_eq_atom(n, *itor)); + } + + if (canHaveNonEmptyAssign) { + return gen_unroll_conditional_options(n, unrolls, lcmStr); + } else { + expr * implyL = mk_and(litems); + expr * implyR = ctx.mk_eq_atom(n, m_strutil.mk_string("")); + // want to return (implyL -> implyR) + return mgr.mk_or(mgr.mk_not(implyL), implyR); + } +} + +expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & unrolls, std::string lcmStr) { + // TODO NEXT + NOT_IMPLEMENTED_YET(); +} + expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tries) { ast_manager & m = get_manager(); context & ctx = get_context(); @@ -6417,6 +6477,30 @@ void theory_str::get_eqc_allUnroll(expr * n, expr * &constStr, std::set & } while (curr != n); } +// Collect simple Unroll functions (whose core is Str2Reg) and constant strings in the EQC of n. +void theory_str::get_eqc_simpleUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet) { + constStr = NULL; + unrollFuncSet.clear(); + context & ctx = get_context(); + + expr * curr = n; + do { + if (is_string(to_app(curr))) { + constStr = curr; + } else if (is_Unroll(to_app(curr))) { + expr * core = to_app(curr)->get_arg(0); + if (is_Str2Reg(to_app(core))) { + if (unrollFuncSet.find(curr) == unrollFuncSet.end()) { + unrollFuncSet.insert(curr); + } + } + } + enode * e_curr = ctx.get_enode(curr); + curr = e_curr->get_next()->get_owner(); + // curr = get_eqc_next(t, curr); + } while (curr != n); +} + void theory_str::init_model(model_generator & mg) { //TRACE("t_str", tout << "initializing model" << std::endl; display(tout);); m_factory = alloc(str_value_factory, get_manager(), get_family_id()); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 467727179..7ef4ef7d3 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -241,7 +241,6 @@ namespace smt { void instantiate_axiom_RegexIn(enode * e); app * mk_unroll(expr * n, expr * bound); - void get_eqc_all_unroll(expr * n, expr * & constStr, std::set & unrollFuncSet); void process_unroll_eq_const_str(expr * unrollFunc, expr * constStr); void unroll_str2reg_constStr(expr * unrollFunc, expr * eqConstStr); void process_concat_eq_unroll(expr * concat, expr * unroll); @@ -332,6 +331,10 @@ namespace smt { // strRegex void get_eqc_allUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet); + void get_eqc_simpleUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet); + void gen_assign_unroll_reg(std::set & unrolls); + expr * gen_assign_unroll_Str2Reg(expr * n, std::set & unrolls); + expr * gen_unroll_conditional_options(expr * var, std::set & unrolls, std::string lcmStr); void dump_assignments(); void initialize_charset(); From b4110c886f13111c8273c20f00f2e54fc96bda60 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 30 Jun 2016 02:46:16 -0400 Subject: [PATCH 135/562] successful unroll of simple unbounded Str2Reg --- src/smt/theory_str.cpp | 117 ++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 11 ++++ 2 files changed, 126 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 64f3d1fc8..c8170a16a 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -32,6 +32,7 @@ theory_str::theory_str(ast_manager & m): opt_AggressiveValueTesting(true), opt_EagerStringConstantLengthAssertions(true), opt_VerifyFinalCheckProgress(true), + opt_LCMUnrollStep(2), /* Internal setup */ search_started(false), m_autil(m), @@ -458,6 +459,10 @@ app * theory_str::mk_unroll_bound_var() { return mk_int_var("unroll"); } +app * theory_str::mk_unroll_test_var() { + return mk_str_var("unrollTest"); // was uRt +} + app * theory_str::mk_str_var(std::string name) { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -6093,8 +6098,116 @@ expr * theory_str::gen_assign_unroll_Str2Reg(expr * n, std::set & unrolls } expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & unrolls, std::string lcmStr) { - // TODO NEXT - NOT_IMPLEMENTED_YET(); + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + + int dist = opt_LCMUnrollStep; + expr_ref_vector litems(mgr); + expr_ref moreAst(m_strutil.mk_string("more"), mgr); + for (std::set::iterator itor = unrolls.begin(); itor != unrolls.end(); itor++) { + expr_ref item(ctx.mk_eq_atom(var, *itor), mgr); + TRACE("t_str_detail", tout << "considering unroll " << mk_pp(item, mgr) << std::endl;); + litems.push_back(item); + } + + if (unroll_tries_map[var][unrolls].size() == 0) { + unroll_tries_map[var][unrolls].push_back(mk_unroll_test_var()); + } + + int tries = unroll_tries_map[var][unrolls].size(); + for (int i = 0; i < tries; i++) { + expr * tester = unroll_tries_map[var][unrolls][i]; + bool testerHasValue = false; + expr * testerVal = get_eqc_value(tester, testerHasValue); + if (!testerHasValue) { + // generate make-up assertion + int l = i * dist; + int h = (i + 1) * dist; + expr_ref lImp(mk_and(litems), mgr); + expr_ref rImp(gen_unroll_assign(var, lcmStr, tester, l, h), mgr); + + SASSERT(lImp); + TRACE("t_str_detail", tout << "lImp = " << mk_pp(lImp, mgr) << std::endl;); + SASSERT(rImp); + TRACE("t_str_detail", tout << "rImp = " << mk_pp(rImp, mgr) << std::endl;); + + expr_ref toAssert(mgr.mk_or(mgr.mk_not(lImp), rImp), mgr); + SASSERT(toAssert); + TRACE("t_str_detail", tout << "Making up assignments for variable which is equal to unbounded Unroll" << std::endl;); + m_trail.push_back(toAssert); + return toAssert; + + // insert [tester = "more"] to litems so that the implyL for next tester is correct + litems.push_back(ctx.mk_eq_atom(tester, moreAst)); + } else { + std::string testerStr = m_strutil.get_string_constant_value(testerVal); + TRACE("t_str_detail", tout << "Tester [" << mk_pp(tester, mgr) << "] = " << testerStr << std::endl;); + if (testerStr == "more") { + litems.push_back(ctx.mk_eq_atom(tester, moreAst)); + } + } + } + expr * tester = mk_unroll_test_var(); + unroll_tries_map[var][unrolls].push_back(tester); + int l = tries * dist; + int h = (tries + 1) * dist; + expr_ref lImp(mk_and(litems), mgr); + expr_ref rImp(gen_unroll_assign(var, lcmStr, tester, l, h), mgr); + SASSERT(lImp); + SASSERT(rImp); + expr_ref toAssert(mgr.mk_or(mgr.mk_not(lImp), rImp), mgr); + SASSERT(toAssert); + TRACE("t_str_detail", tout << "Generating assignment for variable which is equal to unbounded Unroll" << std::endl;); + m_trail.push_back(toAssert); + return toAssert; +} + +expr * theory_str::gen_unroll_assign(expr * var, std::string lcmStr, expr * testerVar, int l, int h) { + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + + TRACE("t_str_detail", tout << "entry: var = " << mk_pp(var, mgr) << ", lcmStr = " << lcmStr + << ", l = " << l << ", h = " << h << std::endl;); + + expr_ref_vector orItems(mgr); + expr_ref_vector andItems(mgr); + + for (int i = l; i < h; i++) { + std::string iStr = int_to_string(i); + expr_ref testerEqAst(ctx.mk_eq_atom(testerVar, m_strutil.mk_string(iStr)), mgr); + TRACE("t_str_detail", tout << "testerEqAst = " << mk_pp(testerEqAst, mgr) << std::endl;); + orItems.push_back(testerEqAst); + std::string unrollStrInstance = get_unrolled_string(lcmStr, i); + + expr_ref x1(ctx.mk_eq_atom(testerEqAst, ctx.mk_eq_atom(var, m_strutil.mk_string(unrollStrInstance))), mgr); + TRACE("t_str_detail", tout << "x1 = " << mk_pp(x1, mgr) << std::endl;); + andItems.push_back(x1); + + expr_ref x2(ctx.mk_eq_atom(testerEqAst, ctx.mk_eq_atom(mk_strlen(var), mk_int(i * lcmStr.length()))), mgr); + TRACE("t_str_detail", tout << "x2 = " << mk_pp(x2, mgr) << std::endl;); + andItems.push_back(x2); + } + expr_ref testerEqMore(ctx.mk_eq_atom(testerVar, m_strutil.mk_string("more")), mgr); + TRACE("t_str_detail", tout << "testerEqMore = " << mk_pp(testerEqMore, mgr) << std::endl;); + orItems.push_back(testerEqMore); + int nextLowerLenBound = h * lcmStr.length(); + expr_ref more2(ctx.mk_eq_atom(testerEqMore, + //Z3_mk_ge(mk_length(t, var), mk_int(ctx, nextLowerLenBound)) + m_autil.mk_ge(m_autil.mk_add(mk_strlen(var), mk_int(-1 * nextLowerLenBound)), mk_int(0)) + ), mgr); + TRACE("t_str_detail", tout << "more2 = " << mk_pp(more2, mgr) << std::endl;); + andItems.push_back(more2); + + expr_ref finalOR(mgr.mk_or(orItems.size(), orItems.c_ptr()), mgr); + TRACE("t_str_detail", tout << "finalOR = " << mk_pp(finalOR, mgr) << std::endl;); + andItems.push_back(mk_or(orItems)); + + expr_ref finalAND(mgr.mk_and(andItems.size(), andItems.c_ptr()), mgr); + TRACE("t_str_detail", tout << "finalAND = " << mk_pp(finalAND, mgr) << std::endl;); + + // doing the following avoids a segmentation fault + m_trail.push_back(finalAND); + return finalAND; } expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tries) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 7ef4ef7d3..736900ba7 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -89,6 +89,11 @@ namespace smt { */ bool opt_VerifyFinalCheckProgress; + /* + * This constant controls how eagerly we expand unrolls in unbounded regex membership tests. + */ + int opt_LCMUnrollStep; + bool search_started; arith_util m_autil; str_util m_strutil; @@ -153,6 +158,10 @@ namespace smt { std::map val_range_map; + // This can't be an expr_ref_vector because the constructor is wrong, + // we would need to modify the allocator so we pass in ast_manager + std::map, ptr_vector > > unroll_tries_map; + char * char_set; std::map charSetLookupTable; int charSetSize; @@ -184,6 +193,7 @@ namespace smt { expr * mk_internal_valTest_var(expr * node, int len, int vTries); app * mk_regex_rep_var(); app * mk_unroll_bound_var(); + app * mk_unroll_test_var(); bool is_concat(app const * a) const { return a->is_app_of(get_id(), OP_STRCAT); } bool is_concat(enode const * n) const { return is_concat(n->get_owner()); } @@ -335,6 +345,7 @@ namespace smt { void gen_assign_unroll_reg(std::set & unrolls); expr * gen_assign_unroll_Str2Reg(expr * n, std::set & unrolls); expr * gen_unroll_conditional_options(expr * var, std::set & unrolls, std::string lcmStr); + expr * gen_unroll_assign(expr * var, std::string lcmStr, expr * testerVar, int l, int h); void dump_assignments(); void initialize_charset(); From a2d6149df59703b90c9835ea41077624f5192c13 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 30 Jun 2016 04:00:42 -0400 Subject: [PATCH 136/562] add general-case regex unroll model generation WIP as there is currently a SAT-as-UNSAT bug I'm trying to fix This also changes the semantics of lower_bound and upper_bound, no longer wrapping the expr that is passed in with mk_strlen(). This actually makes these methods useful for checking bounds of things other than strings. --- src/smt/theory_str.cpp | 181 +++++++++++++++++++++++++++++++++++++++-- src/smt/theory_str.h | 2 + 2 files changed, 174 insertions(+), 9 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index c8170a16a..27043315b 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -131,6 +131,7 @@ void theory_str::assert_axiom(expr * e) { if (opt_VerifyFinalCheckProgress) { finalCheckProgressIndicator = true; } + // TODO add to m_trail? if (get_manager().is_true(e)) return; TRACE("t_str_detail", tout << "asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); context & ctx = get_context(); @@ -3432,6 +3433,11 @@ void theory_str::process_unroll_eq_const_str(expr * unrollFunc, expr * constStr) } void theory_str::process_concat_eq_unroll(expr * concat, expr * unroll) { + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + + TRACE("t_str_detail", tout << "concat = " << mk_pp(concat, mgr) << ", unroll = " << mk_pp(unroll, mgr) << std::endl;); + // TODO NEXT NOT_IMPLEMENTED_YET(); /* @@ -3596,20 +3602,18 @@ bool theory_str::get_value(expr* e, rational& val) const { bool theory_str::lower_bound(expr* _e, rational& lo) { context& ctx = get_context(); ast_manager & m = get_manager(); - expr_ref e(mk_strlen(_e), m); - theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), e); + theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), _e); expr_ref _lo(m); - if (!tha || !tha->get_lower(ctx.get_enode(e), _lo)) return false; + if (!tha || !tha->get_lower(ctx.get_enode(_e), _lo)) return false; return m_autil.is_numeral(_lo, lo) && lo.is_int(); } bool theory_str::upper_bound(expr* _e, rational& hi) { context& ctx = get_context(); ast_manager & m = get_manager(); - expr_ref e(mk_strlen(_e), m); - theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), e); + theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), _e); expr_ref _hi(m); - if (!tha || !tha->get_upper(ctx.get_enode(e), _hi)) return false; + if (!tha || !tha->get_upper(ctx.get_enode(_e), _hi)) return false; return m_autil.is_numeral(_hi, hi) && hi.is_int(); } @@ -5602,6 +5606,7 @@ final_check_status theory_str::final_check_eh() { for (std::map >::iterator fvIt3 = fv_unrolls_map.begin(); fvIt3 != fv_unrolls_map.end(); fvIt3++) { expr * var = fvIt3->first; + TRACE("t_str_detail", tout << "erase free variable " << mk_pp(var, m) << " from freeVar_map, it is bounded by an Unroll" << std::endl;); freeVar_map.erase(var); } @@ -5665,6 +5670,7 @@ final_check_status theory_str::final_check_eh() { } } for (std::set::iterator vItor = fvUnrollSet.begin(); vItor != fvUnrollSet.end(); vItor++) { + TRACE("t_str_detail", tout << "remove " << mk_pp(*vItor, m) << " from freeVar_map" << std::endl;); freeVar_map.erase(*vItor); } @@ -5721,7 +5727,8 @@ final_check_status theory_str::final_check_eh() { } // experimental free variable assignment - end - // more unroll stuff + // now deal with removed free variables that are bounded by an unroll + TRACE("t_str", tout << "fv_unrolls_map (#" << fv_unrolls_map.size() << ")" << std::endl;); for (std::map >::iterator fvIt1 = fv_unrolls_map.begin(); fvIt1 != fv_unrolls_map.end(); fvIt1++) { expr * var = fvIt1->first; @@ -6019,9 +6026,165 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, } } +void theory_str::reduce_virtual_regex_in(expr * var, expr * regex, expr_ref_vector & items) { + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + + TRACE("t_str_detail", tout << "reduce regex " << mk_pp(regex, mgr) << " with respect to variable " << mk_pp(var, mgr) << std::endl;); + + app * regexFuncDecl = to_app(regex); + if (is_Str2Reg(regexFuncDecl)) { + // --------------------------------------------------------- + // var \in Str2Reg(s1) + // ==> + // var = s1 /\ length(var) = length(s1) + // --------------------------------------------------------- + expr * strInside = to_app(regex)->get_arg(0); + items.push_back(ctx.mk_eq_atom(var, strInside)); + items.push_back(ctx.mk_eq_atom(mk_strlen(var), mk_strlen(strInside))); + return; + } + // RegexUnion + else if (is_RegexUnion(regexFuncDecl)) { + // --------------------------------------------------------- + // var \in RegexUnion(r1, r2) + // ==> + // (var = newVar1 \/ var = newVar2) + // (var = newVar1 --> length(var) = length(newVar1)) /\ (var = newVar2 --> length(var) = length(newVar2)) + // /\ (newVar1 \in r1) /\ (newVar2 \in r2) + // --------------------------------------------------------- + expr_ref newVar1(mk_regex_rep_var(), mgr); + expr_ref newVar2(mk_regex_rep_var(), mgr); + items.push_back(mgr.mk_or(ctx.mk_eq_atom(var, newVar1), ctx.mk_eq_atom(var, newVar2))); + items.push_back(mgr.mk_or( + mgr.mk_not(ctx.mk_eq_atom(var, newVar1)), + ctx.mk_eq_atom(mk_strlen(var), mk_strlen(newVar1)))); + items.push_back(mgr.mk_or( + mgr.mk_not(ctx.mk_eq_atom(var, newVar2)), + ctx.mk_eq_atom(mk_strlen(var), mk_strlen(newVar2)))); + + expr * regArg1 = to_app(regex)->get_arg(0); + reduce_virtual_regex_in(newVar1, regArg1, items); + + expr * regArg2 = to_app(regex)->get_arg(1); + reduce_virtual_regex_in(newVar2, regArg2, items); + + return; + } + // RegexConcat + else if (is_RegexConcat(regexFuncDecl)) { + // --------------------------------------------------------- + // var \in RegexConcat(r1, r2) + // ==> + // (var = newVar1 . newVar2) /\ (length(var) = length(vewVar1 . newVar2) ) + // /\ (newVar1 \in r1) /\ (newVar2 \in r2) + // --------------------------------------------------------- + expr_ref newVar1(mk_regex_rep_var(), mgr); + expr_ref newVar2(mk_regex_rep_var(), mgr); + expr_ref concatAst(mk_concat(newVar1, newVar2), mgr); + items.push_back(ctx.mk_eq_atom(var, concatAst)); + items.push_back(ctx.mk_eq_atom(mk_strlen(var), + m_autil.mk_add(mk_strlen(newVar1), mk_strlen(newVar2)))); + + expr * regArg1 = to_app(regex)->get_arg(0); + reduce_virtual_regex_in(newVar1, regArg1, items); + expr * regArg2 = to_app(regex)->get_arg(1); + reduce_virtual_regex_in(newVar2, regArg2, items); + return; + } + // Unroll + else if (is_RegexStar(regexFuncDecl)) { + // --------------------------------------------------------- + // var \in Star(r1) + // ==> + // var = unroll(r1, t1) /\ |var| = |unroll(r1, t1)| + // --------------------------------------------------------- + expr * regArg = to_app(regex)->get_arg(0); + expr_ref unrollCnt(mk_unroll_bound_var(), mgr); + expr_ref unrollFunc(mk_unroll(regArg, unrollCnt), mgr); + items.push_back(ctx.mk_eq_atom(var, unrollFunc)); + items.push_back(ctx.mk_eq_atom(mk_strlen(var), mk_strlen(unrollFunc))); + return; + } else { + UNREACHABLE(); + } +} + void theory_str::gen_assign_unroll_reg(std::set & unrolls) { - // TODO - NOT_IMPLEMENTED_YET(); + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + + expr_ref_vector items(mgr); + for (std::set::iterator itor = unrolls.begin(); itor != unrolls.end(); itor++) { + expr * unrFunc = *itor; + TRACE("t_str_detail", tout << "generating assignment for unroll " << mk_pp(unrFunc, mgr) << std::endl;); + + expr * regexInUnr = to_app(unrFunc)->get_arg(0); + expr * cntInUnr = to_app(unrFunc)->get_arg(1); + items.reset(); + + rational low, high; + bool low_exists = lower_bound(cntInUnr, low); + bool high_exists = upper_bound(cntInUnr, high); + + TRACE("t_str_detail", + tout << "unroll " << mk_pp(unrFunc, mgr) << std::endl; + rational unrLenValue; + bool unrLenValue_exists = get_len_value(unrFunc, unrLenValue); + tout << "unroll length: " << (unrLenValue_exists ? unrLenValue.to_string() : "?") << std::endl; + rational cntInUnrValue; + bool cntHasValue = get_value(cntInUnr, cntInUnrValue); + tout << "unroll count: " << (cntHasValue ? cntInUnrValue.to_string() : "?") + << " low = " + << (low_exists ? low.to_string() : "?") + << " high = " + << (high_exists ? high.to_string() : "?") + << std::endl; + ); + + expr_ref toAssert(mgr); + if (low.is_neg()) { + toAssert = m_autil.mk_ge(cntInUnr, mk_int(0)); + } else { + if (unroll_var_map.find(unrFunc) == unroll_var_map.end()) { + + expr_ref newVar1(mk_regex_rep_var(), mgr); + expr_ref newVar2(mk_regex_rep_var(), mgr); + expr_ref concatAst(mk_concat(newVar1, newVar2), mgr); + expr_ref newCnt(mk_unroll_bound_var(), mgr); + expr_ref newUnrollFunc(mk_unroll(regexInUnr, newCnt), mgr); + + // unroll(r1, t1) = newVar1 . newVar2 + items.push_back(ctx.mk_eq_atom(unrFunc, concatAst)); + items.push_back(ctx.mk_eq_atom(mk_strlen(unrFunc), m_autil.mk_add(mk_strlen(newVar1), mk_strlen(newVar2)))); + items.push_back(ctx.mk_eq_atom(mk_strlen(unrFunc), mk_strlen(newVar1))); + items.push_back(ctx.mk_eq_atom(mk_strlen(unrFunc), mk_strlen(newVar2))); + // newVar1 \in r1 + reduce_virtual_regex_in(newVar1, regexInUnr, items); + items.push_back(ctx.mk_eq_atom(cntInUnr, m_autil.mk_add(newCnt, mk_int(1)))); + items.push_back(ctx.mk_eq_atom(newVar2, newUnrollFunc)); + items.push_back(ctx.mk_eq_atom(mk_strlen(newVar2), mk_strlen(newUnrollFunc))); + toAssert = ctx.mk_eq_atom( + m_autil.mk_ge(cntInUnr, mk_int(1)), + mk_and(items)); + + // option 0 + expr_ref op0(ctx.mk_eq_atom(cntInUnr, mk_int(0)), mgr); + expr_ref ast1(ctx.mk_eq_atom(unrFunc, m_strutil.mk_string("")), mgr); + expr_ref ast2(ctx.mk_eq_atom(mk_strlen(unrFunc), mk_int(0)), mgr); + expr_ref and1(mgr.mk_and(ast1, ast2), mgr); + + // put together + toAssert = mgr.mk_and(ctx.mk_eq_atom(op0, and1), toAssert); + + unroll_var_map[unrFunc] = toAssert; + } else { + toAssert = unroll_var_map[unrFunc]; + } + } + m_trail.push_back(toAssert); + assert_axiom(toAssert); + } } static int computeGCD(int x, int y) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 736900ba7..c61b3783a 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -161,6 +161,7 @@ namespace smt { // This can't be an expr_ref_vector because the constructor is wrong, // we would need to modify the allocator so we pass in ast_manager std::map, ptr_vector > > unroll_tries_map; + std::map unroll_var_map; char * char_set; std::map charSetLookupTable; @@ -346,6 +347,7 @@ namespace smt { expr * gen_assign_unroll_Str2Reg(expr * n, std::set & unrolls); expr * gen_unroll_conditional_options(expr * var, std::set & unrolls, std::string lcmStr); expr * gen_unroll_assign(expr * var, std::string lcmStr, expr * testerVar, int l, int h); + void reduce_virtual_regex_in(expr * var, expr * regex, expr_ref_vector & items); void dump_assignments(); void initialize_charset(); From b53da182b647b9ca1187538a76884be4534fbda5 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 30 Jun 2016 04:39:09 -0400 Subject: [PATCH 137/562] fix gen_assign_unroll_reg so that it does not assert a contradiction --- src/smt/theory_str.cpp | 30 ++++++++---------------------- src/smt/theory_str.h | 1 + 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 27043315b..843e78e85 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -461,7 +461,9 @@ app * theory_str::mk_unroll_bound_var() { } app * theory_str::mk_unroll_test_var() { - return mk_str_var("unrollTest"); // was uRt + app * v = mk_str_var("unrollTest"); // was uRt + internal_unrollTest_vars.insert(v); + return v; } app * theory_str::mk_str_var(std::string name) { @@ -4159,6 +4161,8 @@ bool theory_str::free_var_attempt(expr * nn1, expr * nn2) { more_value_tests(nn1, nn2_str); } return true; + } else if (internal_unrollTest_vars.contains(nn1)) { + return true; } else { return false; } @@ -4366,26 +4370,7 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { } // regex unroll - /* - Z3_ast nn1EqConst = NULL; - std::set nn1EqUnrollFuncs; - get_eqc_allUnroll(t, nn1, nn1EqConst, nn1EqUnrollFuncs); - Z3_ast nn2EqConst = NULL; - std::set nn2EqUnrollFuncs; - get_eqc_allUnroll(t, nn2, nn2EqConst, nn2EqUnrollFuncs); - if (nn2EqConst != NULL) { - for (std::set::iterator itor1 = nn1EqUnrollFuncs.begin(); itor1 != nn1EqUnrollFuncs.end(); itor1++) { - processUnrollEqConstStr(t, *itor1, nn2EqConst); - } - } - - if (nn1EqConst != NULL) { - for (std::set::iterator itor2 = nn2EqUnrollFuncs.begin(); itor2 != nn2EqUnrollFuncs.end(); itor2++) { - processUnrollEqConstStr(t, *itor2, nn1EqConst); - } - } - */ expr * nn1EqConst = NULL; std::set nn1EqUnrollFuncs; get_eqc_allUnroll(lhs, nn1EqConst, nn1EqUnrollFuncs); @@ -6157,8 +6142,9 @@ void theory_str::gen_assign_unroll_reg(std::set & unrolls) { // unroll(r1, t1) = newVar1 . newVar2 items.push_back(ctx.mk_eq_atom(unrFunc, concatAst)); items.push_back(ctx.mk_eq_atom(mk_strlen(unrFunc), m_autil.mk_add(mk_strlen(newVar1), mk_strlen(newVar2)))); - items.push_back(ctx.mk_eq_atom(mk_strlen(unrFunc), mk_strlen(newVar1))); - items.push_back(ctx.mk_eq_atom(mk_strlen(unrFunc), mk_strlen(newVar2))); + // mk_strlen(unrFunc) >= mk_strlen(newVar{1,2}) + items.push_back(m_autil.mk_ge(m_autil.mk_add(mk_strlen(unrFunc), m_autil.mk_mul(mk_int(-1), mk_strlen(newVar1))), mk_int(0))); + items.push_back(m_autil.mk_ge(m_autil.mk_add(mk_strlen(unrFunc), m_autil.mk_mul(mk_int(-1), mk_strlen(newVar2))), mk_int(0))); // newVar1 \in r1 reduce_virtual_regex_in(newVar1, regexInUnr, items); items.push_back(ctx.mk_eq_atom(cntInUnr, m_autil.mk_add(newCnt, mk_int(1)))); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index c61b3783a..daf534686 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -146,6 +146,7 @@ namespace smt { obj_hashtable internal_lenTest_vars; obj_hashtable internal_valTest_vars; + obj_hashtable internal_unrollTest_vars; std::set input_var_in_len; From 7d903ff1fa0cf04277aee71558d2cc6c961fe7ac Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 30 Jun 2016 04:55:11 -0400 Subject: [PATCH 138/562] implement process_concat_eq_unroll, WIP --- src/smt/theory_str.cpp | 112 ++++++++++++++++++----------------------- src/smt/theory_str.h | 1 + 2 files changed, 50 insertions(+), 63 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 843e78e85..853924a94 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3440,80 +3440,66 @@ void theory_str::process_concat_eq_unroll(expr * concat, expr * unroll) { TRACE("t_str_detail", tout << "concat = " << mk_pp(concat, mgr) << ", unroll = " << mk_pp(unroll, mgr) << std::endl;); - // TODO NEXT - NOT_IMPLEMENTED_YET(); - /* -#ifdef DEBUGLOG - __debugPrint(logFile, ">> processConcatEqUnroll: \n"); - __debugPrint(logFile, " * [concat] "); - printZ3Node(t, concat); - __debugPrint(logFile, "\n"); - __debugPrint(logFile, " * [unroll] "); - printZ3Node(t, unroll); - __debugPrint(logFile, "\n\n"); -#endif + std::pair key = std::make_pair(concat, unroll); + expr_ref toAssert(mgr); - Z3_context ctx = Z3_theory_get_context(t); - std::pair key = std::make_pair(concat, unroll); - Z3_ast toAssert = NULL; + if (concat_eq_unroll_ast_map.find(key) == concat_eq_unroll_ast_map.end()) { + expr_ref arg1(to_app(concat)->get_arg(0), mgr); + expr_ref arg2(to_app(concat)->get_arg(1), mgr); + expr_ref r1(to_app(unroll)->get_arg(0), mgr); + expr_ref t1(to_app(unroll)->get_arg(1), mgr); - if (concatEqUnroll_AstMap.find(key) == concatEqUnroll_AstMap.end()) { - Z3_ast arg1 = Z3_get_app_arg(ctx, Z3_to_app(ctx, concat), 0); - Z3_ast arg2 = Z3_get_app_arg(ctx, Z3_to_app(ctx, concat), 1); - Z3_ast r1 = Z3_get_app_arg(ctx, Z3_to_app(ctx, unroll), 0); - Z3_ast t1 = Z3_get_app_arg(ctx, Z3_to_app(ctx, unroll), 1); + expr_ref v1(mk_regex_rep_var(), mgr); + expr_ref v2(mk_regex_rep_var(), mgr); + expr_ref v3(mk_regex_rep_var(), mgr); + expr_ref v4(mk_regex_rep_var(), mgr); + expr_ref v5(mk_regex_rep_var(), mgr); - Z3_ast v1 = mk_regexRepVar(t); - Z3_ast v2 = mk_regexRepVar(t); - Z3_ast v3 = mk_regexRepVar(t); - Z3_ast v4 = mk_regexRepVar(t); - Z3_ast v5 = mk_regexRepVar(t); + expr_ref t2(mk_unroll_bound_var(), mgr); + expr_ref t3(mk_unroll_bound_var(), mgr); + expr_ref emptyStr(m_strutil.mk_string(""), mgr); - Z3_ast t2 = mk_unrollBoundVar(t); - Z3_ast t3 = mk_unrollBoundVar(t); - Z3_ast emptyStr = my_mk_str_value(t, ""); + expr_ref unroll1(mk_unroll(r1, t2), mgr); + expr_ref unroll2(mk_unroll(r1, t3), mgr); - Z3_ast unroll1 = mk_unroll(t, r1, t2); - Z3_ast unroll2 = mk_unroll(t, r1, t3); + expr_ref op0(ctx.mk_eq_atom(t1, mk_int(0)), mgr); + expr_ref op1(m_autil.mk_ge(t1, mk_int(1)), mgr); - Z3_ast op0 = Z3_mk_eq(ctx, t1, mk_int(ctx, 0)); - Z3_ast op1 = Z3_mk_ge(ctx, t1, mk_int(ctx, 1)); + expr_ref_vector op1Items(mgr); + expr_ref_vector op2Items(mgr); - std::vector op1Items; - std::vector op2Items; + op1Items.push_back(ctx.mk_eq_atom(arg1, emptyStr)); + op1Items.push_back(ctx.mk_eq_atom(arg2, emptyStr)); + op1Items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(0))); + op1Items.push_back(ctx.mk_eq_atom(mk_strlen(arg2), mk_int(0))); + expr_ref opAnd1(ctx.mk_eq_atom(op0, mk_and(op1Items)), mgr); - op1Items.push_back(Z3_mk_eq(ctx, arg1, emptyStr)); - op1Items.push_back(Z3_mk_eq(ctx, arg2, emptyStr)); - op1Items.push_back(Z3_mk_eq(ctx, mk_length(t, arg1), mk_int(ctx, 0))); - op1Items.push_back(Z3_mk_eq(ctx, mk_length(t, arg2), mk_int(ctx, 0))); - Z3_ast opAnd1 = Z3_mk_eq(ctx, op0, mk_and_fromVector(t, op1Items)); + expr_ref v1v2(mk_concat(v1, v2), mgr); + op2Items.push_back(ctx.mk_eq_atom(arg1, v1v2)); + op2Items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), m_autil.mk_add(mk_strlen(v1), mk_strlen(v2)))); + expr_ref v3v4(mk_concat(v3, v4), mgr); + op2Items.push_back(ctx.mk_eq_atom(arg2, v3v4)); + op2Items.push_back(ctx.mk_eq_atom(mk_strlen(arg2), m_autil.mk_add(mk_strlen(v3), mk_strlen(v4)))); - Z3_ast v1v2 = mk_concat(t, v1, v2); - op2Items.push_back(Z3_mk_eq(ctx, arg1, v1v2)); - op2Items.push_back(Z3_mk_eq(ctx, mk_length(t, arg1), mk_2_add(t, mk_length(t, v1), mk_length(t, v2)))); - Z3_ast v3v4 = mk_concat(t, v3, v4); - op2Items.push_back(Z3_mk_eq(ctx, arg2, v3v4)); - op2Items.push_back(Z3_mk_eq(ctx, mk_length(t, arg2), mk_2_add(t, mk_length(t, v3), mk_length(t, v4)))); + op2Items.push_back(ctx.mk_eq_atom(v1, unroll1)); + op2Items.push_back(ctx.mk_eq_atom(mk_strlen(v1), mk_strlen(unroll1))); + op2Items.push_back(ctx.mk_eq_atom(v4, unroll2)); + op2Items.push_back(ctx.mk_eq_atom(mk_strlen(v4), mk_strlen(unroll2))); + expr_ref v2v3(mk_concat(v2, v3), mgr); + op2Items.push_back(ctx.mk_eq_atom(v5, v2v3)); + reduce_virtual_regex_in(v5, r1, op2Items); + op2Items.push_back(ctx.mk_eq_atom(mk_strlen(v5), m_autil.mk_add(mk_strlen(v2), mk_strlen(v3)))); + op2Items.push_back(ctx.mk_eq_atom(m_autil.mk_add(t2, t3), m_autil.mk_add(t1, mk_int(-1)))); + expr_ref opAnd2(ctx.mk_eq_atom(op1, mk_and(op2Items)), mgr); - op2Items.push_back(Z3_mk_eq(ctx, v1, unroll1)); - op2Items.push_back(Z3_mk_eq(ctx, mk_length(t, v1), mk_length(t, unroll1))); - op2Items.push_back(Z3_mk_eq(ctx, v4, unroll2)); - op2Items.push_back(Z3_mk_eq(ctx, mk_length(t, v4), mk_length(t, unroll2))); - Z3_ast v2v3 = mk_concat(t, v2, v3); - op2Items.push_back(Z3_mk_eq(ctx, v5, v2v3)); - reduceVirtualRegexIn(t, v5, r1, op2Items); - op2Items.push_back(Z3_mk_eq(ctx, mk_length(t, v5), mk_2_add(t, mk_length(t, v2), mk_length(t, v3)))); - op2Items.push_back(Z3_mk_eq(ctx, mk_2_add(t, t2, t3), mk_2_sub(t, t1, mk_int(ctx, 1)))); - Z3_ast opAnd2 = Z3_mk_eq(ctx, op1, mk_and_fromVector(t, op2Items)); + toAssert = mgr.mk_and(opAnd1, opAnd2); + m_trail.push_back(toAssert); + concat_eq_unroll_ast_map[key] = toAssert; + } else { + toAssert = concat_eq_unroll_ast_map[key]; + } - toAssert = mk_2_and(t, opAnd1, opAnd2); - concatEqUnroll_AstMap[key] = toAssert; - } else { - toAssert = concatEqUnroll_AstMap[key]; - } - - addAxiom(t, toAssert, __LINE__); - */ + assert_axiom(toAssert); } void theory_str::unroll_str2reg_constStr(expr * unrollFunc, expr * eqConstStr) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index daf534686..154a66c58 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -163,6 +163,7 @@ namespace smt { // we would need to modify the allocator so we pass in ast_manager std::map, ptr_vector > > unroll_tries_map; std::map unroll_var_map; + std::map, expr*> concat_eq_unroll_ast_map; char * char_set; std::map charSetLookupTable; From 9eead64d03a3e9c58313d5116a2891a031edb567 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 6 Jul 2016 17:31:37 -0400 Subject: [PATCH 139/562] prevent assertion of basic string axioms on variables that go out of scope (theory_str) this is testing a crash avoidance feature, the regression is tests/z3str/regex-026.smt2 this also adds some debugging code for a substr() crash but that is WIP --- src/smt/theory_str.cpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 853924a94..c28132feb 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -799,6 +799,13 @@ void theory_str::instantiate_basic_string_axioms(enode * str) { context & ctx = get_context(); ast_manager & m = get_manager(); + // TESTING: attempt to avoid a crash here when a variable goes out of scope + // TODO this seems to work so we probably need to do this for other propagate checks, etc. + if (str->get_iscope_lvl() > ctx.get_scope_level()) { + TRACE("t_str_detail", tout << "WARNING: skipping axiom setup on out-of-scope string term" << std::endl;); + return; + } + // generate a stronger axiom for constant strings app * a_str = str->get_owner(); if (m_strutil.is_string(str->get_owner())) { @@ -1400,6 +1407,7 @@ void theory_str::reset_eh() { m_basicstr_axiom_todo.reset(); m_str_eq_todo.reset(); m_concat_axiom_todo.reset(); + // TODO reset a loooooot more internal stuff pop_scope_eh(get_context().get_scope_level()); } @@ -2714,8 +2722,25 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { l_count = 2; lenDelta = str_len - y_len; } + TRACE("t_str", + tout + << "xLen? " << (x_len_exists ? "yes" : "no") << std::endl + << "mLen? " << (m_len_exists ? "yes" : "no") << std::endl + << "yLen? " << (y_len_exists ? "yes" : "no") << std::endl + << "xLen = " << x_len.to_string() << std::endl + << "yLen = " << y_len.to_string() << std::endl + << "mLen = " << m_len.to_string() << std::endl + << "strLen = " << str_len.to_string() << std::endl + << "lenDelta = " << lenDelta.to_string() << std::endl + << "strValue = \"" << strValue << "\" (len=" << strValue.length() << ")" << std::endl + ; + ); + + TRACE("t_str", tout << "*** MARKER 1 ***" << std::endl;); std::string part1Str = strValue.substr(0, lenDelta.get_unsigned()); + TRACE("t_str", tout << "*** MARKER 2 ***" << std::endl;); std::string part2Str = strValue.substr(lenDelta.get_unsigned(), strValue.length() - lenDelta.get_unsigned()); + TRACE("t_str", tout << "*** MARKER 3 ***" << std::endl;); expr_ref prefixStr(m_strutil.mk_string(part1Str), mgr); expr_ref x_concat(mk_concat(m, prefixStr), mgr); @@ -5495,7 +5520,7 @@ final_check_status theory_str::final_check_eh() { } TRACE("t_str", tout << "final check" << std::endl;); - TRACE("t_str_detail", dump_assignments();); + TRACE("t_str_dump_assign", dump_assignments();); // run dependence analysis to find free string variables std::map varAppearInAssign; From 847a5fc1f82b437b46db0d9cc8b813560141050f Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 7 Jul 2016 16:13:48 -0400 Subject: [PATCH 140/562] replace old mk_value behaviour in theory_str that creates placeholders for unused terms instead of crashing --- src/smt/theory_str.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index c28132feb..0626c6ac5 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -6844,8 +6844,7 @@ model_value_proc * theory_str::mk_value(enode * n, model_generator & mg) { TRACE("t_str", tout << "WARNING: failed to find a concrete value, falling back" << std::endl;); // TODO make absolutely sure the reason we can't find a concrete value is because of an unassigned temporary // e.g. for an expression like (Concat X $$_str0) - //return alloc(expr_wrapper_proc, m_strutil.mk_string("**UNUSED**")); - NOT_IMPLEMENTED_YET(); + return alloc(expr_wrapper_proc, m_strutil.mk_string("**UNUSED**")); } } From 8aa6fee0af66b6e2dd465dc52b4dbc23c1b719b0 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 8 Jul 2016 12:21:11 -0400 Subject: [PATCH 141/562] fixups wip --- src/smt/theory_str.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 0626c6ac5..aaecdb011 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -141,6 +141,10 @@ void theory_str::assert_axiom(expr * e) { literal lit(ctx.get_literal(e)); ctx.mark_as_relevant(lit); ctx.mk_th_axiom(get_id(), 1, &lit); + + // crash/error avoidance: add all axioms to the trail + m_trail.push_back(e); + TRACE("t_str_detail", tout << "done asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); } @@ -4622,6 +4626,7 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { unsigned count = 0; std::set vars = it->second; for (std::set::iterator var_it = vars.begin(); var_it != vars.end(); ++var_it) { + TRACE("t_str_detail", tout << "clean up variable " << mk_pp(*var_it, get_manager()) << std::endl;); variable_set.erase(*var_it); internal_variable_set.erase(*var_it); regex_variable_set.erase(*var_it); From 8d47b082446cf4292643a3fc1e333db2e355e09d Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 10 Jul 2016 13:05:41 -0400 Subject: [PATCH 142/562] fix out-of-scope value tester bug in theory_str::gen_free_var_options() we now pass tests/z3str/charAt-003.smt2 with detailed debugging turned off! --- src/smt/theory_str.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index aaecdb011..06b221acd 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -5968,8 +5968,28 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, int len = atoi(len_valueStr.c_str()); - if (fvar_valueTester_map[freeVar].find(len) == fvar_valueTester_map[freeVar].end()) { - TRACE("t_str_detail", tout << "no previous value testers" << std::endl;); + // check whether any value tester is actually in scope + // TODO NEXT we need to do this check for other tester variables that could potentially go out of scope + TRACE("t_str_detail", tout << "checking scope of previous value testers" << std::endl;); + bool map_effectively_empty = true; + if (fvar_valueTester_map[freeVar].find(len) != fvar_valueTester_map[freeVar].end()) { + // there's *something* in the map, but check its scope + svector > entries = fvar_valueTester_map[freeVar][len]; + for (svector >::iterator it = entries.begin(); it != entries.end(); ++it) { + std::pair entry = *it; + expr * aTester = entry.second; + if (internal_variable_set.find(aTester) == internal_variable_set.end()) { + TRACE("t_str_detail", tout << mk_pp(aTester, m) << " out of scope" << std::endl;); + } else { + TRACE("t_str_detail", tout << mk_pp(aTester, m) << " in scope" << std::endl;); + map_effectively_empty = false; + break; + } + } + } + + if (map_effectively_empty) { + TRACE("t_str_detail", tout << "no previous value testers, or none of them were in scope" << std::endl;); int tries = 0; expr * val_indicator = mk_internal_valTest_var(freeVar, len, tries); valueTester_fvar_map[val_indicator] = freeVar; From 9ffcd135d5d2217c1ddd0328228d352145ec71ec Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 19 Jul 2016 15:47:41 -0400 Subject: [PATCH 143/562] add RegexPlus to theory_str --- src/ast/rewriter/str_rewriter.cpp | 24 ++++++++++++++++++++++++ src/ast/rewriter/str_rewriter.h | 1 + src/ast/str_decl_plugin.cpp | 7 +++++++ src/ast/str_decl_plugin.h | 17 ++++++++++++++++- 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index 3a0300ae4..a40d52aa1 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -219,6 +219,27 @@ br_status str_rewriter::mk_re_RegexIn(expr * str, expr * re, expr_ref & result) return BR_FAILED; } +br_status str_rewriter::mk_re_RegexPlus(expr * re, expr_ref & result) { + /* + * Two optimizations are possible if we inspect 're'. + * If 're' is (RegexPlus X), then reduce to 're'. + * If 're' is (RegexStar X), then reduce to 're'. + * Otherwise, reduce to (RegexConcat re (RegexStar re)). + */ + + if (m_strutil.is_re_RegexPlus(re)) { + result = re; + return BR_REWRITE_FULL; + } else if (m_strutil.is_re_RegexStar(re)) { + // Z3str2 re-created the AST under 're' here, but I don't think we need to do that + result = re; + return BR_REWRITE_FULL; + } else { + result = m_strutil.mk_re_RegexConcat(re, m_strutil.mk_re_RegexStar(re)); + return BR_REWRITE_FULL; + } +} + br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) { SASSERT(f->get_family_id() == get_fid()); @@ -256,6 +277,9 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con case OP_RE_REGEXIN: SASSERT(num_args == 2); return mk_re_RegexIn(args[0], args[1], result); + case OP_RE_REGEXPLUS: + SASSERT(num_args == 1); + return mk_re_RegexPlus(args[0], result); default: return BR_FAILED; } diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index 5c0e1167f..bd79ed7a1 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -51,6 +51,7 @@ public: br_status mk_re_Str2Reg(expr * str, expr_ref & result); br_status mk_re_RegexIn(expr * str, expr * re, expr_ref & result); + br_status mk_re_RegexPlus(expr * re, expr_ref & result); bool reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change); bool reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change); diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index ef94272c7..45ff37b0f 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -42,6 +42,7 @@ str_decl_plugin::str_decl_plugin(): m_re_regexstar_decl(0), m_re_regexunion_decl(0), m_re_unroll_decl(0), + m_re_regexplus_decl(0), m_arith_plugin(0), m_arith_fid(0), m_int_sort(0){ @@ -70,6 +71,7 @@ void str_decl_plugin::finalize(void) { DEC_REF(m_re_regexconcat_decl); DEC_REF(m_re_regexstar_decl); DEC_REF(m_re_regexunion_decl); + DEC_REF(m_re_regexplus_decl); DEC_REF(m_re_unroll_decl); DEC_REF(m_int_sort); } @@ -153,6 +155,9 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_re_regexstar_decl = m->mk_func_decl(symbol("RegexStar"), re, re, func_decl_info(id, OP_RE_REGEXSTAR)); m_manager->inc_ref(m_re_regexstar_decl); + m_re_regexplus_decl = m->mk_func_decl(symbol("RegexPlus"), re, re, func_decl_info(id, OP_RE_REGEXPLUS)); + m_manager->inc_ref(m_re_regexplus_decl); + m_re_regexunion_decl = m->mk_func_decl(symbol("RegexUnion"), re, re, re, func_decl_info(id, OP_RE_REGEXUNION)); m_manager->inc_ref(m_re_regexunion_decl); @@ -190,6 +195,7 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { case OP_RE_REGEXIN: return m_re_regexin_decl; case OP_RE_REGEXCONCAT: return m_re_regexconcat_decl; case OP_RE_REGEXSTAR: return m_re_regexstar_decl; + case OP_RE_REGEXPLUS: return m_re_regexplus_decl; case OP_RE_REGEXUNION: return m_re_regexunion_decl; case OP_RE_UNROLL: return m_re_unroll_decl; default: return 0; @@ -262,6 +268,7 @@ void str_decl_plugin::get_op_names(svector & op_names, symbol cons op_names.push_back(builtin_name("RegexConcat", OP_RE_REGEXCONCAT)); op_names.push_back(builtin_name("RegexStar", OP_RE_REGEXSTAR)); op_names.push_back(builtin_name("RegexUnion", OP_RE_REGEXUNION)); + op_names.push_back(builtin_name("RegexPlus", OP_RE_REGEXPLUS)); op_names.push_back(builtin_name("Unroll", OP_RE_UNROLL)); } diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index c2ad088a4..902e2208f 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -48,6 +48,8 @@ enum str_op_kind { OP_RE_REGEXSTAR, OP_RE_REGEXUNION, OP_RE_UNROLL, + // higher-level regex operators + OP_RE_REGEXPLUS, // end LAST_STR_OP }; @@ -77,6 +79,7 @@ protected: func_decl * m_re_regexstar_decl; func_decl * m_re_regexunion_decl; func_decl * m_re_unroll_decl; + func_decl * m_re_regexplus_decl; arith_decl_plugin * m_arith_plugin; family_id m_arith_fid; @@ -120,6 +123,8 @@ public: bool is_string(expr const * n) const; bool is_re_Str2Reg(expr const * n) const { return is_app_of(n, get_fid(), OP_RE_STR2REGEX); } + bool is_re_RegexStar(expr const * n) const { return is_app_of(n, get_fid(), OP_RE_REGEXSTAR); } + bool is_re_RegexPlus(expr const * n) const { return is_app_of(n, get_fid(), OP_RE_REGEXPLUS); } std::string get_string_constant_value(expr const *n) const; // TODO @@ -142,7 +147,17 @@ public: app * mk_fresh_string() { return m_plugin->mk_fresh_string(); } - // TODO + + app * mk_re_RegexConcat(expr * e1, expr * e2) { + expr * es[2] = {e1, e2}; + return m_manager.mk_app(get_fid(), OP_RE_REGEXCONCAT, 2, es); + } + + app * mk_re_RegexStar(expr * r) { + expr * es[1] = {r}; + return m_manager.mk_app(get_fid(), OP_RE_REGEXSTAR, 1, es); + } + }; #endif /* _STR_DECL_PLUGIN_H_ */ From 0f382037799c9ccab63ecb7cf5ea97c1a1aab3ac Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 19 Jul 2016 16:39:43 -0400 Subject: [PATCH 144/562] add RegexCharRange to theory_str --- src/ast/rewriter/str_rewriter.cpp | 37 ++++++++++++++++++++++++++++--- src/ast/rewriter/str_rewriter.h | 1 + src/ast/str_decl_plugin.cpp | 7 ++++++ src/ast/str_decl_plugin.h | 16 +++++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index a40d52aa1..1449afcc3 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -200,8 +200,7 @@ br_status str_rewriter::mk_str_Replace(expr * base, expr * source, expr * target br_status str_rewriter::mk_re_Str2Reg(expr * str, expr_ref & result) { // the argument to Str2Reg *must* be a string constant - // TODO is an assertion error too strict here? this basically crashes the solver - VERIFY(m_strutil.is_string(str)); + ENSURE(m_strutil.is_string(str)); return BR_FAILED; } @@ -211,7 +210,7 @@ br_status str_rewriter::mk_re_RegexIn(expr * str, expr * re, expr_ref & result) if (m_strutil.is_re_Str2Reg(re)) { TRACE("t_str_rw", tout << "RegexIn fast path: " << mk_pp(str, m()) << " in " << mk_pp(re, m()) << std::endl;); expr * regexStr = to_app(re)->get_arg(0); - VERIFY(m_strutil.is_string(regexStr)); + ENSURE(m_strutil.is_string(regexStr)); result = m().mk_eq(str, regexStr); return BR_REWRITE_FULL; } @@ -240,6 +239,35 @@ br_status str_rewriter::mk_re_RegexPlus(expr * re, expr_ref & result) { } } +br_status str_rewriter::mk_re_RegexCharRange(expr * start, expr * end, expr_ref & result) { + TRACE("t_str_rw", tout << "rewrite (RegexCharRange " << mk_pp(start, m()) << " " << mk_pp(end, m()) << ")" << std::endl;); + // both 'start' and 'end' must be string constants + ENSURE(m_strutil.is_string(start) && m_strutil.is_string(end)); + std::string arg0Value = m_strutil.get_string_constant_value(start); + std::string arg1Value = m_strutil.get_string_constant_value(end); + ENSURE(arg0Value.length() == 1 && arg1Value.length() == 1); + char low = arg0Value[0]; + char high = arg1Value[0]; + if (low > high) { + char t = low; + low = high; + high = t; + } + + char c = low; + std::string cStr; + cStr.push_back(c); + expr * res = m_strutil.mk_re_Str2Reg(cStr); + c++; + for (; c <= high; c++) { + cStr.clear(); + cStr.push_back(c); + res = m_strutil.mk_re_RegexUnion(res, m_strutil.mk_re_Str2Reg(cStr)); + } + result = res; + return BR_DONE; +} + br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) { SASSERT(f->get_family_id() == get_fid()); @@ -280,6 +308,9 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con case OP_RE_REGEXPLUS: SASSERT(num_args == 1); return mk_re_RegexPlus(args[0], result); + case OP_RE_REGEXCHARRANGE: + SASSERT(num_args == 2); + return mk_re_RegexCharRange(args[0], args[1], result); default: return BR_FAILED; } diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index bd79ed7a1..dccf4a6bd 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -52,6 +52,7 @@ public: br_status mk_re_Str2Reg(expr * str, expr_ref & result); br_status mk_re_RegexIn(expr * str, expr * re, expr_ref & result); br_status mk_re_RegexPlus(expr * re, expr_ref & result); + br_status mk_re_RegexCharRange(expr * start, expr * end, expr_ref & result); bool reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change); bool reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change); diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 45ff37b0f..08358d46b 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -43,6 +43,7 @@ str_decl_plugin::str_decl_plugin(): m_re_regexunion_decl(0), m_re_unroll_decl(0), m_re_regexplus_decl(0), + m_re_regexcharrange_decl(0), m_arith_plugin(0), m_arith_fid(0), m_int_sort(0){ @@ -72,6 +73,7 @@ void str_decl_plugin::finalize(void) { DEC_REF(m_re_regexstar_decl); DEC_REF(m_re_regexunion_decl); DEC_REF(m_re_regexplus_decl); + DEC_REF(m_re_regexcharrange_decl); DEC_REF(m_re_unroll_decl); DEC_REF(m_int_sort); } @@ -164,6 +166,9 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_re_unroll_decl = m->mk_func_decl(symbol("Unroll"), re, i, s, func_decl_info(id, OP_RE_UNROLL)); m_manager->inc_ref(m_re_unroll_decl); + m_re_regexcharrange_decl = m->mk_func_decl(symbol("RegexCharRange"), s, s, re, func_decl_info(id, OP_RE_REGEXCHARRANGE)); + m_manager->inc_ref(m_re_regexcharrange_decl); + } decl_plugin * str_decl_plugin::mk_fresh() { @@ -198,6 +203,7 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { case OP_RE_REGEXPLUS: return m_re_regexplus_decl; case OP_RE_REGEXUNION: return m_re_regexunion_decl; case OP_RE_UNROLL: return m_re_unroll_decl; + case OP_RE_REGEXCHARRANGE: return m_re_regexcharrange_decl; default: return 0; } } @@ -270,6 +276,7 @@ void str_decl_plugin::get_op_names(svector & op_names, symbol cons op_names.push_back(builtin_name("RegexUnion", OP_RE_REGEXUNION)); op_names.push_back(builtin_name("RegexPlus", OP_RE_REGEXPLUS)); op_names.push_back(builtin_name("Unroll", OP_RE_UNROLL)); + op_names.push_back(builtin_name("RegexCharRange", OP_RE_REGEXCHARRANGE)); } void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 902e2208f..4b7a8858e 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -50,6 +50,7 @@ enum str_op_kind { OP_RE_UNROLL, // higher-level regex operators OP_RE_REGEXPLUS, + OP_RE_REGEXCHARRANGE, // end LAST_STR_OP }; @@ -80,6 +81,7 @@ protected: func_decl * m_re_regexunion_decl; func_decl * m_re_unroll_decl; func_decl * m_re_regexplus_decl; + func_decl * m_re_regexcharrange_decl; arith_decl_plugin * m_arith_plugin; family_id m_arith_fid; @@ -148,6 +150,20 @@ public: return m_plugin->mk_fresh_string(); } + app * mk_re_Str2Reg(expr * s) { + expr * es[1] = {s}; + return m_manager.mk_app(get_fid(), OP_RE_STR2REGEX, 1, es); + } + + app * mk_re_Str2Reg(std::string s) { + return mk_re_Str2Reg(mk_string(s)); + } + + app * mk_re_RegexUnion(expr * e1, expr * e2) { + expr * es[2] = {e1, e2}; + return m_manager.mk_app(get_fid(), OP_RE_REGEXUNION, 2, es); + } + app * mk_re_RegexConcat(expr * e1, expr * e2) { expr * es[2] = {e1, e2}; return m_manager.mk_app(get_fid(), OP_RE_REGEXCONCAT, 2, es); From ac16aa7c818e5bd3ead0ab45c5d03cc0953d162b Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 23 Jul 2016 16:02:11 -0400 Subject: [PATCH 145/562] fix out-of-scope variable bug in theory_str::process_concat_eq_type6 this fix will have to be made to all functions that use varForBreakConcat --- src/smt/theory_str.cpp | 47 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 06b221acd..28f972164 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3363,18 +3363,53 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { expr * xorFlag = NULL; std::pair key1(concatAst1, concatAst2); std::pair key2(concatAst2, concatAst1); - if (varForBreakConcat.find(key1) == varForBreakConcat.end() && varForBreakConcat.find(key2) == varForBreakConcat.end()) { + + // check the entries in this map to make sure they're still in scope + // before we use them. + // TODO something very similar might have to be done elsewhere when we use this map, if this works. + + std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); + std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); + + bool entry1InScope; + if (entry1 == varForBreakConcat.end()) { + entry1InScope = false; + } else { + if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end() + || internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end()) { + entry1InScope = false; + } else { + entry1InScope = true; + } + } + + bool entry2InScope; + if (entry2 == varForBreakConcat.end()) { + entry2InScope = false; + } else { + if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end() + || internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end()) { + entry2InScope = false; + } else { + entry2InScope = true; + } + } + + TRACE("t_str_detail", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl + << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); + + if (!entry1InScope && !entry2InScope) { commonVar = mk_nonempty_str_var(); xorFlag = mk_internal_xor_var(); varForBreakConcat[key1][0] = commonVar; varForBreakConcat[key1][1] = xorFlag; } else { - if (varForBreakConcat.find(key1) != varForBreakConcat.end()) { - commonVar = varForBreakConcat[key1][0]; - xorFlag = varForBreakConcat[key1][1]; + if (entry1InScope) { + commonVar = (entry1->second)[0]; + xorFlag = (entry1->second)[1]; } else { - commonVar = varForBreakConcat[key2][0]; - xorFlag = varForBreakConcat[key2][1]; + commonVar = (entry2->second)[0]; + xorFlag = (entry2->second)[1]; } } From 02a66c425ee6603961802de8c1a64163f61b4fe4 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 23 Jul 2016 22:43:46 -0400 Subject: [PATCH 146/562] add option to bypass quick returns in integer theory integration in theory_str this might not actually be that useful, if the problem is, as I suspect it to be, that values we get from the integer theory need not correspond with assertions in the core (that can get popped off the stack, etc.) --- src/smt/theory_str.cpp | 212 ++++++++++++++++++++++++++++++++++++----- src/smt/theory_str.h | 9 ++ 2 files changed, 196 insertions(+), 25 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 28f972164..d77290b46 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -33,6 +33,7 @@ theory_str::theory_str(ast_manager & m): opt_EagerStringConstantLengthAssertions(true), opt_VerifyFinalCheckProgress(true), opt_LCMUnrollStep(2), + opt_NoQuickReturn_Concat_IntegerTheory(true), /* Internal setup */ search_started(false), m_autil(m), @@ -145,7 +146,7 @@ void theory_str::assert_axiom(expr * e) { // crash/error avoidance: add all axioms to the trail m_trail.push_back(e); - TRACE("t_str_detail", tout << "done asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); + //TRACE("t_str_detail", tout << "done asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); } void theory_str::assert_implication(expr * premise, expr * conclusion) { @@ -2049,8 +2050,16 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { TRACE("t_str", tout << "nn1 = " << mk_ismt2_pp(nn1, m) << std::endl << "nn2 = " << mk_ismt2_pp(nn2, m) << std::endl;); + TRACE("t_str_detail", tout + << "len(" << mk_pp(a1_arg0, m) << ") = " << (a1_arg0_len_exists ? a1_arg0_len.to_string() : "?") << std::endl + << "len(" << mk_pp(a1_arg1, m) << ") = " << (a1_arg1_len_exists ? a1_arg1_len.to_string() : "?") << std::endl + << "len(" << mk_pp(a2_arg0, m) << ") = " << (a2_arg0_len_exists ? a2_arg0_len.to_string() : "?") << std::endl + << "len(" << mk_pp(a2_arg1, m) << ") = " << (a2_arg1_len_exists ? a2_arg1_len.to_string() : "?") << std::endl + << std::endl;); + infer_len_concat_equality(nn1, nn2); + // TODO we may want to add no-quick-return options for these as well if (a1_arg0 == a2_arg0) { if (!in_same_eqc(a1_arg1, a2_arg1)) { expr_ref premise(ctx.mk_eq_atom(nn1, nn2), m); @@ -2077,6 +2086,8 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { // quick path + // TODO we may want to add no-quick-return options for these as well + if (in_same_eqc(a1_arg0, a2_arg0)) { if (in_same_eqc(a1_arg1, a2_arg1)) { TRACE("t_str_detail", tout << "SKIP: a1_arg0 =~ a2_arg0 and a1_arg1 =~ a2_arg1" << std::endl;); @@ -2111,7 +2122,12 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { expr_ref conclusion(m.mk_and(ax_r1, ax_r2), m); assert_implication(premise, conclusion); - return; + + if (opt_NoQuickReturn_Concat_IntegerTheory) { + TRACE("t_str_detail", tout << "bypassing quick return from the end of this case" << std::endl;); + } else { + return; + } } } @@ -2127,7 +2143,11 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { expr_ref conclusion(m.mk_and(ax_r1, ax_r2), m); assert_implication(premise, conclusion); - return; + if (opt_NoQuickReturn_Concat_IntegerTheory) { + TRACE("t_str_detail", tout << "bypassing quick return from the end of this case" << std::endl;); + } else { + return; + } } } @@ -2328,7 +2348,42 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { std::pair key1(concatAst1, concatAst2); std::pair key2(concatAst2, concatAst1); - if (varForBreakConcat.find(key1) == varForBreakConcat.end() && varForBreakConcat.find(key2) == varForBreakConcat.end()) { + // check the entries in this map to make sure they're still in scope + // before we use them. + + std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); + std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); + + bool entry1InScope; + if (entry1 == varForBreakConcat.end()) { + entry1InScope = false; + } else { + if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end() + || internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end() + || internal_variable_set.find((entry1->second)[2]) == internal_variable_set.end()) { + entry1InScope = false; + } else { + entry1InScope = true; + } + } + + bool entry2InScope; + if (entry2 == varForBreakConcat.end()) { + entry2InScope = false; + } else { + if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end() + || internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end() + || internal_variable_set.find((entry2->second)[2]) == internal_variable_set.end()) { + entry2InScope = false; + } else { + entry2InScope = true; + } + } + + TRACE("t_str_detail", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl + << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); + + if (!entry1InScope && !entry2InScope) { t1 = mk_nonempty_str_var(); t2 = mk_nonempty_str_var(); xorFlag = mk_internal_xor_var(); @@ -2339,7 +2394,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { varForBreakConcat[key1][2] = xorFlag; } else { // match found - if (varForBreakConcat.find(key1) != varForBreakConcat.end()) { + if (entry1InScope) { t1 = varForBreakConcat[key1][0]; t2 = varForBreakConcat[key1][1]; xorFlag = varForBreakConcat[key1][2]; @@ -2619,17 +2674,50 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { std::pair key1(concatAst1, concatAst2); std::pair key2(concatAst2, concatAst1); - if (varForBreakConcat.find(key1) == varForBreakConcat.end() - && varForBreakConcat.find(key2) == varForBreakConcat.end()) { + // check the entries in this map to make sure they're still in scope + // before we use them. + + std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); + std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); + + bool entry1InScope; + if (entry1 == varForBreakConcat.end()) { + entry1InScope = false; + } else { + if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end() + || internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end()) { + entry1InScope = false; + } else { + entry1InScope = true; + } + } + + bool entry2InScope; + if (entry2 == varForBreakConcat.end()) { + entry2InScope = false; + } else { + if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end() + || internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end()) { + entry2InScope = false; + } else { + entry2InScope = true; + } + } + + TRACE("t_str_detail", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl + << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); + + + if (!entry1InScope && !entry2InScope) { temp1 = mk_nonempty_str_var(); xorFlag = mk_internal_xor_var(); varForBreakConcat[key1][0] = temp1; varForBreakConcat[key1][1] = xorFlag; } else { - if (varForBreakConcat.find(key1) != varForBreakConcat.end()) { + if (entry1InScope) { temp1 = varForBreakConcat[key1][0]; xorFlag = varForBreakConcat[key1][1]; - } else if (varForBreakConcat.find(key2) != varForBreakConcat.end()) { + } else if (entry2InScope) { temp1 = varForBreakConcat[key2][0]; xorFlag = varForBreakConcat[key2][1]; } @@ -2888,7 +2976,6 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { std::string strValue = m_strutil.get_string_constant_value(strAst); - // TODO integer theory interaction rational x_len, y_len, str_len, n_len; bool x_len_exists = get_len_value(x, x_len); bool y_len_exists = get_len_value(y, y_len); @@ -2899,14 +2986,49 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { expr_ref temp1(mgr); std::pair key1(concatAst1, concatAst2); std::pair key2(concatAst2, concatAst1); - if (varForBreakConcat.find(key1) == varForBreakConcat.end() && varForBreakConcat.find(key2) == varForBreakConcat.end()) { + + // check the entries in this map to make sure they're still in scope + // before we use them. + + std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); + std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); + + bool entry1InScope; + if (entry1 == varForBreakConcat.end()) { + entry1InScope = false; + } else { + if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end() + || internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end()) { + entry1InScope = false; + } else { + entry1InScope = true; + } + } + + bool entry2InScope; + if (entry2 == varForBreakConcat.end()) { + entry2InScope = false; + } else { + if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end() + || internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end()) { + entry2InScope = false; + } else { + entry2InScope = true; + } + } + + TRACE("t_str_detail", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl + << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); + + + if (!entry1InScope && !entry2InScope) { temp1 = mk_nonempty_str_var(); xorFlag = mk_internal_xor_var(); varForBreakConcat[key1][0] = temp1; varForBreakConcat[key1][1] = xorFlag; } else { - if (varForBreakConcat.find(key1) != varForBreakConcat.end()) { + if (entry1InScope) { temp1 = varForBreakConcat[key1][0]; xorFlag = varForBreakConcat[key1][1]; } else if (varForBreakConcat.find(key2) != varForBreakConcat.end()) { @@ -3366,7 +3488,6 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { // check the entries in this map to make sure they're still in scope // before we use them. - // TODO something very similar might have to be done elsewhere when we use this map, if this works. std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); @@ -4084,16 +4205,44 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { expr_ref xorFlag(m); std::pair key1(arg1, arg2); std::pair key2(arg2, arg1); - std::map, std::map >::iterator varBreak_key1 = - varForBreakConcat.find(key1); - std::map, std::map >::iterator varBreak_key2 = - varForBreakConcat.find(key2); - if (varBreak_key1 == varForBreakConcat.end() && varBreak_key2 == varForBreakConcat.end()) { + + // check the entries in this map to make sure they're still in scope + // before we use them. + + std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); + std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); + + bool entry1InScope; + if (entry1 == varForBreakConcat.end()) { + entry1InScope = false; + } else { + if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end()) { + entry1InScope = false; + } else { + entry1InScope = true; + } + } + + bool entry2InScope; + if (entry2 == varForBreakConcat.end()) { + entry2InScope = false; + } else { + if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end()) { + entry2InScope = false; + } else { + entry2InScope = true; + } + } + + TRACE("t_str_detail", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl + << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); + + if (!entry1InScope && !entry2InScope) { xorFlag = mk_internal_xor_var(); varForBreakConcat[key1][0] = xorFlag; - } else if (varBreak_key1 != varForBreakConcat.end()) { + } else if (entry1InScope) { xorFlag = varForBreakConcat[key1][0]; - } else { // varBreak_key2 != varForBreakConcat.end() + } else { // entry2InScope xorFlag = varForBreakConcat[key2][0]; } @@ -4632,7 +4781,7 @@ void theory_str::push_scope_eh() { context & ctx = get_context(); sLevel += 1; TRACE("t_str", tout << "push to " << sLevel << std::endl;); - TRACE("t_str_dump_assign", dump_assignments();); + TRACE("t_str_dump_assign_on_scope_change", dump_assignments();); } void theory_str::pop_scope_eh(unsigned num_scopes) { @@ -4683,7 +4832,7 @@ void theory_str::dump_assignments() { ctx.get_assignments(assignments); for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { expr * ex = *i; - tout << mk_ismt2_pp(ex, m) << std::endl; + tout << mk_ismt2_pp(ex, m) << (ctx.is_relevant(ex) ? "" : " (NOT REL)") << std::endl; } ); } @@ -4697,6 +4846,9 @@ void theory_str::classify_ast_by_type(expr * node, std::map & varMap if (variable_set.find(node) != variable_set.end() && internal_lenTest_vars.find(node) == internal_lenTest_vars.end() && internal_valTest_vars.find(node) == internal_valTest_vars.end()) { + if (varMap[node] != 1) { + TRACE("t_str_detail", tout << "new variable: " << mk_pp(node, get_manager()) << std::endl;); + } varMap[node] = 1; } // check whether the node is a function that we want to inspect @@ -4755,6 +4907,10 @@ void theory_str::classify_ast_by_type_in_positive_context(std::map & // so we bypass a huge amount of work by doing the following... if (m.is_eq(argAst)) { + TRACE("t_str_detail", tout + << "eq ast " << mk_pp(argAst, m) << " is between args of sort " + << m.get_sort(to_app(argAst)->get_arg(0))->get_name() + << std::endl;); classify_ast_by_type(argAst, varMap, concatMap, unrollMap); } } @@ -4976,6 +5132,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map::iterator it = variable_set.begin(); it != variable_set.end(); ++it) { expr* var = *it; if (internal_variable_set.find(var) == internal_variable_set.end()) { + TRACE("t_str_detail", tout << "new variable: " << mk_pp(var, m) << std::endl;); strVarMap[*it] = 1; } } @@ -5716,7 +5873,7 @@ final_check_status theory_str::final_check_eh() { constValue = NULL; { - TRACE("t_str_detail", tout << "free var map (# " << freeVar_map.size() << "):" << std::endl; + TRACE("t_str_detail", tout << "free var map (#" << freeVar_map.size() << "):" << std::endl; for (std::map::iterator freeVarItor1 = freeVar_map.begin(); freeVarItor1 != freeVar_map.end(); freeVarItor1++) { expr * freeVar = freeVarItor1->first; rational lenValue; @@ -5764,7 +5921,7 @@ final_check_status theory_str::final_check_eh() { // experimental free variable assignment - end // now deal with removed free variables that are bounded by an unroll - TRACE("t_str", tout << "fv_unrolls_map (#" << fv_unrolls_map.size() << ")" << std::endl;); + TRACE("t_str", tout << "fv_unrolls_map (#" << fv_unrolls_map.size() << "):" << std::endl;); for (std::map >::iterator fvIt1 = fv_unrolls_map.begin(); fvIt1 != fv_unrolls_map.end(); fvIt1++) { expr * var = fvIt1->first; @@ -6004,7 +6161,6 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, int len = atoi(len_valueStr.c_str()); // check whether any value tester is actually in scope - // TODO NEXT we need to do this check for other tester variables that could potentially go out of scope TRACE("t_str_detail", tout << "checking scope of previous value testers" << std::endl;); bool map_effectively_empty = true; if (fvar_valueTester_map[freeVar].find(len) != fvar_valueTester_map[freeVar].end()) { @@ -6042,6 +6198,12 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, for (; i < testerTotal; i++) { expr * aTester = fvar_valueTester_map[freeVar][len][i].second; + // it's probably worth checking scope here, actually + if (internal_variable_set.find(aTester) == internal_variable_set.end()) { + TRACE("t_str_detail", tout << "value tester " << mk_pp(aTester, m) << " out of scope, skipping" << std::endl;); + continue; + } + if (aTester == valTesterInCbEq) { break; } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 154a66c58..d2b51a712 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -94,6 +94,15 @@ namespace smt { */ int opt_LCMUnrollStep; + /* + * If NoQuickReturn_Concat_IntegerTheory is set to true, + * the integer theory integration conditionals in simplify_concat_equality() + * will not return from the function after asserting their axioms. + * This means that control will fall through to the type 1-6 axioms, + * causing those to be added as well. + */ + bool opt_NoQuickReturn_Concat_IntegerTheory; + bool search_started; arith_util m_autil; str_util m_strutil; From f555074e27c6b570546cd27bd5410cf88af3faf1 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 23 Jul 2016 23:29:56 -0400 Subject: [PATCH 147/562] add option to disable integer theory integration in theory_str; this is currently ENABLED --- src/smt/theory_str.cpp | 21 +++++++++++++++++++++ src/smt/theory_str.h | 16 ++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index d77290b46..35bc7ab20 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -34,6 +34,7 @@ theory_str::theory_str(ast_manager & m): opt_VerifyFinalCheckProgress(true), opt_LCMUnrollStep(2), opt_NoQuickReturn_Concat_IntegerTheory(true), + opt_DisableIntegerTheoryIntegration(true), /* Internal setup */ search_started(false), m_autil(m), @@ -3747,6 +3748,11 @@ static theory_mi_arith* get_th_arith(context& ctx, theory_id afid, expr* e) { } bool theory_str::get_value(expr* e, rational& val) const { + if (opt_DisableIntegerTheoryIntegration) { + TRACE("t_str_detail", tout << "WARNING: integer theory integration disabled" << std::endl;); + return false; + } + context& ctx = get_context(); ast_manager & m = get_manager(); theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), e); @@ -3773,6 +3779,11 @@ bool theory_str::get_value(expr* e, rational& val) const { } bool theory_str::lower_bound(expr* _e, rational& lo) { + if (opt_DisableIntegerTheoryIntegration) { + TRACE("t_str_detail", tout << "WARNING: integer theory integration disabled" << std::endl;); + return false; + } + context& ctx = get_context(); ast_manager & m = get_manager(); theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), _e); @@ -3782,6 +3793,11 @@ bool theory_str::lower_bound(expr* _e, rational& lo) { } bool theory_str::upper_bound(expr* _e, rational& hi) { + if (opt_DisableIntegerTheoryIntegration) { + TRACE("t_str_detail", tout << "WARNING: integer theory integration disabled" << std::endl;); + return false; + } + context& ctx = get_context(); ast_manager & m = get_manager(); theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), _e); @@ -3791,6 +3807,11 @@ bool theory_str::upper_bound(expr* _e, rational& hi) { } bool theory_str::get_len_value(expr* e, rational& val) { + if (opt_DisableIntegerTheoryIntegration) { + TRACE("t_str_detail", tout << "WARNING: integer theory integration disabled" << std::endl;); + return false; + } + context& ctx = get_context(); ast_manager & m = get_manager(); theory* th = ctx.get_theory(m_autil.get_family_id()); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index d2b51a712..0ba4a1a4d 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -100,9 +100,25 @@ namespace smt { * will not return from the function after asserting their axioms. * This means that control will fall through to the type 1-6 axioms, * causing those to be added as well. + * The default behaviour of Z3str2 is to set this to 'false'. */ bool opt_NoQuickReturn_Concat_IntegerTheory; + /* + * If DisableIntegerTheoryIntegration is set to true, + * ALL calls to the integer theory integration methods + * (get_value, get_len_value, lower_bound, upper_bound) + * will ignore what the arithmetic solver believes about length terms, + * and will return no information. + * + * This reduces performance significantly, but can be useful to enable + * if it is suspected that string-integer integration, or the arithmetic solver itself, + * might have a bug. + * + * The default behaviour of Z3str2 is to set this to 'false'. + */ + bool opt_DisableIntegerTheoryIntegration; + bool search_started; arith_util m_autil; str_util m_strutil; From 1c518be61d8d133e48e3cafdd2153dc30ff7e4d1 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 27 Jul 2016 12:46:35 -0400 Subject: [PATCH 148/562] new_eq_handler improvements in theory_str, WIP --- src/smt/theory_str.cpp | 173 +++++++++++++++++++++++++++++++---------- src/smt/theory_str.h | 2 + 2 files changed, 133 insertions(+), 42 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 35bc7ab20..a0daa021a 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -33,8 +33,8 @@ theory_str::theory_str(ast_manager & m): opt_EagerStringConstantLengthAssertions(true), opt_VerifyFinalCheckProgress(true), opt_LCMUnrollStep(2), - opt_NoQuickReturn_Concat_IntegerTheory(true), - opt_DisableIntegerTheoryIntegration(true), + opt_NoQuickReturn_Concat_IntegerTheory(false), + opt_DisableIntegerTheoryIntegration(false), /* Internal setup */ search_started(false), m_autil(m), @@ -1428,7 +1428,57 @@ void theory_str::reset_eh() { * Then add an assertion: (y2 == (Concat ce m2)) AND ("str3" == (Concat abc x2)) -> (y2 != "str3") */ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { - // TODO this involves messing around with enodes and equivalence classes + context & ctx = get_context(); + ast_manager & m = get_manager(); + + // Previously we did the check between LHS and RHS equivalence classes. + // However these have since been merged. + // We start by asserting that the EQCs, in fact, really are merged. + if (!in_same_eqc(lhs, rhs)) { + TRACE("t_str", tout << "BUG: lhs and rhs not in same eqc in new_eq_eh(), loss of invariant!" << std::endl;); + UNREACHABLE(); + } + + check_concat_len_in_eqc(lhs); + check_concat_len_in_eqc(rhs); + + // Now we iterate over all pairs of terms in the (shared) eqc + // and check whether we can show that any pair of distinct terms + // cannot possibly be equal. + // If that's the case, we assert an axiom to that effect and stop. + + enode * eqc_root = ctx.get_enode(lhs)->get_root(); + enode * eqc_iterator1 = eqc_root; + do { + enode * eqc_iterator2 = eqc_iterator1; + do { + if (eqc_iterator1 == eqc_iterator2) { + continue; + } + // pull terms out of the enodes + app * eqc_nn1 = eqc_iterator1->get_owner(); + app * eqc_nn2 = eqc_iterator2->get_owner(); + TRACE("t_str_detail", tout << "checking whether " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " can be equal" << std::endl;); + if (!can_two_nodes_eq(eqc_nn1, eqc_nn2)) { + TRACE("t_str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " cannot be equal to " << mk_pp(eqc_nn2, m) << std::endl;); + expr_ref to_assert(m.mk_not(ctx.mk_eq_atom(eqc_nn1, eqc_nn2)), m); + assert_axiom(to_assert); + return false; + } + if (!check_length_consistency(eqc_nn1, eqc_nn2)) { + TRACE("t_str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " have inconsistent lengths" << std::endl;); + return false; + } + eqc_iterator2 = eqc_iterator2->get_next(); + } while (eqc_iterator2 != eqc_root); + + eqc_iterator1 = eqc_iterator1->get_next(); + } while (eqc_iterator1 != eqc_root); + + // TODO containPairBoolMap + // TODO regexInBoolMap + + // okay, all checks here passed return true; } @@ -2681,12 +2731,17 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); + // prevent checking scope for the XOR term, as it's always in the same scope as the split var + // TODO probably make this change everywhere else in process_concat_eq*, + // and also make sure this is correct. + bool entry1InScope; if (entry1 == varForBreakConcat.end()) { entry1InScope = false; } else { if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end() - || internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end()) { + /*|| internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end()*/ + ) { entry1InScope = false; } else { entry1InScope = true; @@ -2698,7 +2753,8 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { entry2InScope = false; } else { if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end() - || internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end()) { + /*|| internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end()*/ + ) { entry2InScope = false; } else { entry2InScope = true; @@ -3954,48 +4010,58 @@ bool theory_str::in_same_eqc(expr * n1, expr * n2) { return n1Node->get_root() == n2Node->get_root(); } -/* -bool canTwoNodesEq(Z3_theory t, Z3_ast n1, Z3_ast n2) { - Z3_ast n1_curr = n1; - Z3_ast n2_curr = n2; - - // case 0: n1_curr is const string, n2_curr is const string - if (isConstStr(t, n1_curr) && isConstStr(t, n2_curr)) { - if (n1_curr != n2_curr) { - return false; - } - } - // case 1: n1_curr is concat, n2_curr is const string - else if (isConcatFunc(t, n1_curr) && isConstStr(t, n2_curr)) { - std::string n2_curr_str = getConstStrValue(t, n2_curr); - if (canConcatEqStr(t, n1_curr, n2_curr_str) != 1) { - return false; - } - } - // case 2: n2_curr is concat, n1_curr is const string - else if (isConcatFunc(t, n2_curr) && isConstStr(t, n1_curr)) { - std::string n1_curr_str = getConstStrValue(t, n1_curr); - if (canConcatEqStr(t, n2_curr, n1_curr_str) != 1) { - return false; - } - } else if (isConcatFunc(t, n1_curr) && isConcatFunc(t, n2_curr)) { - if (canConcatEqConcat(t, n1_curr, n2_curr) != 1) { - return false; - } - } - - return true; -} -*/ - bool theory_str::can_concat_eq_str(expr * concat, std::string str) { - // TODO - return true; + /* + int strLen = str.length(); + if (isConcatFunc(t, concat)) { + std::vector args; + getNodesInConcat(t, concat, args); + Z3_ast ml_node = args[0]; + Z3_ast mr_node = args[args.size() - 1]; + + if (isConstStr(t, ml_node)) { + std::string ml_str = getConstStrValue(t, ml_node); + int ml_len = ml_str.length(); + if (ml_len > strLen) + return 0; + int cLen = ml_len; + if (ml_str != str.substr(0, cLen)) + return 0; + } + + if (isConstStr(t, mr_node)) { + std::string mr_str = getConstStrValue(t, mr_node); + int mr_len = mr_str.length(); + if (mr_len > strLen) + return 0; + int cLen = mr_len; + if (mr_str != str.substr(strLen - cLen, cLen)) + return 0; + } + + int sumLen = 0; + for (unsigned int i = 0; i < args.size(); i++) { + Z3_ast oneArg = args[i]; + if (isConstStr(t, oneArg)) { + std::string arg_str = getConstStrValue(t, oneArg); + if (str.find(arg_str) == std::string::npos) { + return 0; + } + sumLen += getConstStrValue(t, oneArg).length(); + } + } + if (sumLen > strLen) + return 0; + } + return 1; + */ + // TODO NEXT + NOT_IMPLEMENTED_YET(); return true; } bool theory_str::can_concat_eq_concat(expr * concat1, expr * concat2) { // TODO - return true; + NOT_IMPLEMENTED_YET(); return true; } /* @@ -4041,6 +4107,27 @@ bool theory_str::can_two_nodes_eq(expr * n1, expr * n2) { return true; } +bool theory_str::check_length_consistency(expr * n1, expr * n2) { + // TODO NEXT + NOT_IMPLEMENTED_YET(); return true; +} + +void theory_str::check_concat_len_in_eqc(expr * concat) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + enode * eqc_base = ctx.get_enode(concat); + enode * eqc_it = eqc_base; + do { + app * eqc_n = eqc_it->get_owner(); + if (is_concat(eqc_n)) { + rational unused; + infer_len_concat(eqc_n, unused); + } + eqc_it = eqc_it->get_next(); + } while (eqc_it != eqc_base); +} + /* * strArgmt::solve_concat_eq_str() * Solve concatenations of the form: @@ -4499,6 +4586,8 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { // As a result, simplify_concat_equality() is never getting called, // and if it were called, it would probably get called with the same element on both sides. + + // TODO improve these checks with an all-pairs match over LHS and RHS wrt. other concats bool hasCommon = false; if (eqc_lhs_concat.size() != 0 && eqc_rhs_concat.size() != 0) { std::set::iterator itor1 = eqc_lhs_concat.begin(); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 0ba4a1a4d..d213f6271 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -298,6 +298,8 @@ namespace smt { bool can_two_nodes_eq(expr * n1, expr * n2); bool can_concat_eq_str(expr * concat, std::string str); bool can_concat_eq_concat(expr * concat1, expr * concat2); + void check_concat_len_in_eqc(expr * concat); + bool check_length_consistency(expr * n1, expr * n2); void get_nodes_in_concat(expr * node, ptr_vector & nodeList); expr * simplify_concat(expr * node); From ceed3f3ff0203b7ec0c5a793bc1a6530c6e0b609 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 27 Jul 2016 15:15:01 -0400 Subject: [PATCH 149/562] add theory_str::can_concat_eq_str --- src/smt/theory_str.cpp | 89 ++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 43 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index a0daa021a..bb7c1c9be 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4011,52 +4011,55 @@ bool theory_str::in_same_eqc(expr * n1, expr * n2) { } bool theory_str::can_concat_eq_str(expr * concat, std::string str) { - /* - int strLen = str.length(); - if (isConcatFunc(t, concat)) { - std::vector args; - getNodesInConcat(t, concat, args); - Z3_ast ml_node = args[0]; - Z3_ast mr_node = args[args.size() - 1]; + // TODO this method could use some traces and debugging info + int strLen = str.length(); + if (is_concat(to_app(concat))) { + ptr_vector args; + get_nodes_in_concat(concat, args); + expr * ml_node = args[0]; + expr * mr_node = args[args.size() - 1]; - if (isConstStr(t, ml_node)) { - std::string ml_str = getConstStrValue(t, ml_node); - int ml_len = ml_str.length(); - if (ml_len > strLen) - return 0; - int cLen = ml_len; - if (ml_str != str.substr(0, cLen)) - return 0; - } + if (m_strutil.is_string(ml_node)) { + std::string ml_str = m_strutil.get_string_constant_value(ml_node); + int ml_len = ml_str.length(); + if (ml_len > strLen) { + return false; + } + int cLen = ml_len; + if (ml_str != str.substr(0, cLen)) { + return false; + } + } - if (isConstStr(t, mr_node)) { - std::string mr_str = getConstStrValue(t, mr_node); - int mr_len = mr_str.length(); - if (mr_len > strLen) - return 0; - int cLen = mr_len; - if (mr_str != str.substr(strLen - cLen, cLen)) - return 0; - } + if (m_strutil.is_string(mr_node)) { + std::string mr_str = m_strutil.get_string_constant_value(mr_node); + int mr_len = mr_str.length(); + if (mr_len > strLen) { + return false; + } + int cLen = mr_len; + if (mr_str != str.substr(strLen - cLen, cLen)) { + return false; + } + } - int sumLen = 0; - for (unsigned int i = 0; i < args.size(); i++) { - Z3_ast oneArg = args[i]; - if (isConstStr(t, oneArg)) { - std::string arg_str = getConstStrValue(t, oneArg); - if (str.find(arg_str) == std::string::npos) { - return 0; - } - sumLen += getConstStrValue(t, oneArg).length(); - } - } - if (sumLen > strLen) - return 0; - } - return 1; - */ - // TODO NEXT - NOT_IMPLEMENTED_YET(); return true; + int sumLen = 0; + for (unsigned int i = 0 ; i < args.size() ; i++) { + expr * oneArg = args[i]; + if (m_strutil.is_string(oneArg)) { + std::string arg_str = m_strutil.get_string_constant_value(oneArg); + if (str.find(arg_str) == std::string::npos) { + return false; + } + sumLen += arg_str.length(); + } + } + + if (sumLen > strLen) { + return false; + } + } + return true; } bool theory_str::can_concat_eq_concat(expr * concat1, expr * concat2) { From a31a948a5bf0f720fdebcd23eaa838e796eb06fe Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 27 Jul 2016 15:21:33 -0400 Subject: [PATCH 150/562] add theory_str::can_concat_eq_concat --- src/smt/theory_str.cpp | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index bb7c1c9be..3ff4ff2de 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4063,8 +4063,39 @@ bool theory_str::can_concat_eq_str(expr * concat, std::string str) { } bool theory_str::can_concat_eq_concat(expr * concat1, expr * concat2) { - // TODO - NOT_IMPLEMENTED_YET(); return true; + // TODO this method could use some traces and debugging info + if (is_concat(to_app(concat1)) && is_concat(to_app(concat2))) { + { + // Suppose concat1 = (Concat X Y) and concat2 = (Concat M N). + expr * concat1_mostL = getMostLeftNodeInConcat(concat1); + expr * concat2_mostL = getMostLeftNodeInConcat(concat2); + // if both X and M are constant strings, check whether they have the same prefix + if (m_strutil.is_string(concat1_mostL) && m_strutil.is_string(concat2_mostL)) { + std::string concat1_mostL_str = m_strutil.get_string_constant_value(concat1_mostL); + std::string concat2_mostL_str = m_strutil.get_string_constant_value(concat2_mostL); + int cLen = std::min(concat1_mostL_str.length(), concat2_mostL_str.length()); + if (concat1_mostL_str.substr(0, cLen) != concat2_mostL_str.substr(0, cLen)) { + return false; + } + } + } + + { + // Similarly, if both Y and N are constant strings, check whether they have the same suffix + expr * concat1_mostR = getMostRightNodeInConcat(concat1); + expr * concat2_mostR = getMostRightNodeInConcat(concat2); + if (m_strutil.is_string(concat1_mostR) && m_strutil.is_string(concat2_mostR)) { + std::string concat1_mostR_str = m_strutil.get_string_constant_value(concat1_mostR); + std::string concat2_mostR_str = m_strutil.get_string_constant_value(concat2_mostR); + int cLen = std::min(concat1_mostR_str.length(), concat2_mostR_str.length()); + if (concat1_mostR_str.substr(concat1_mostR_str.length() - cLen, cLen) != + concat2_mostR_str.substr(concat2_mostR_str.length() - cLen, cLen)) { + return false; + } + } + } + } + return true; } /* From 95f1cfa5a6b7d13ff0ad927c3416450932f83b5b Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 27 Jul 2016 16:18:05 -0400 Subject: [PATCH 151/562] add theory_str::check_length_consistency, WIP --- src/smt/theory_str.cpp | 31 +++++++++++++++++++++++++++++-- src/smt/theory_str.h | 2 ++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 3ff4ff2de..24aefe3a6 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4141,9 +4141,36 @@ bool theory_str::can_two_nodes_eq(expr * n1, expr * n2) { return true; } +// was checkLength2ConstStr() in Z3str2 +// returns true if everything is OK, or false if inconsistency detected +// - note that these are different from the semantics in Z3str2 +bool theory_str::check_length_const_string(expr * n1, expr * constStr) { + // TODO NEXT + NOT_IMPLEMENTED_YET(); return true; +} + +// returns true if everything is OK, or false if inconsistency detected +// - note that these are different from the semantics in Z3str2 +bool theory_str::check_length_eq_var_concat(expr * n1, expr * n2) { + // TODO NEXT + NOT_IMPLEMENTED_YET(); return true; +} + +// returns false if an inconsistency is detected, or true if no inconsistencies were found +// - note that these are different from the semantics of checkLengConsistency() in Z3str2 bool theory_str::check_length_consistency(expr * n1, expr * n2) { - // TODO NEXT - NOT_IMPLEMENTED_YET(); return true; + if (m_strutil.is_string(n1) && m_strutil.is_string(n2)) { + // consistency has already been checked in can_two_nodes_eq(). + return true; + } else if (m_strutil.is_string(n1) && (!m_strutil.is_string(n2))) { + return check_length_const_string(n2, n1); + } else if (m_strutil.is_string(n2) && (!m_strutil.is_string(n1))) { + return check_length_const_string(n1, n2); + } else { + // n1 and n2 are vars or concats + return check_length_eq_var_concat(n1, n2); + } + return 0; } void theory_str::check_concat_len_in_eqc(expr * concat) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index d213f6271..d4681856c 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -300,6 +300,8 @@ namespace smt { bool can_concat_eq_concat(expr * concat1, expr * concat2); void check_concat_len_in_eqc(expr * concat); bool check_length_consistency(expr * n1, expr * n2); + bool check_length_const_string(expr * n1, expr * constStr); + bool check_length_eq_var_concat(expr * n1, expr * n2); void get_nodes_in_concat(expr * node, ptr_vector & nodeList); expr * simplify_concat(expr * node); From 76ceac6664032e8daf8935ad51ac1d96f048f4a0 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 28 Jul 2016 16:31:40 -0400 Subject: [PATCH 152/562] add theory_str::check_length_const_string --- src/smt/theory_str.cpp | 50 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 24aefe3a6..042ff5808 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4145,8 +4145,54 @@ bool theory_str::can_two_nodes_eq(expr * n1, expr * n2) { // returns true if everything is OK, or false if inconsistency detected // - note that these are different from the semantics in Z3str2 bool theory_str::check_length_const_string(expr * n1, expr * constStr) { - // TODO NEXT - NOT_IMPLEMENTED_YET(); return true; + ast_manager & mgr = get_manager(); + context & ctx = get_context(); + + rational strLen((unsigned) (m_strutil.get_string_constant_value(constStr).length())); + + if (is_concat(to_app(n1))) { + ptr_vector args; + expr_ref_vector items(mgr); + + get_nodes_in_concat(n1, args); + + rational sumLen(0); + for (unsigned int i = 0; i < args.size(); ++i) { + rational argLen; + bool argLen_exists = get_len_value(args[i], argLen); + if (argLen_exists) { + if (!m_strutil.is_string(args[i])) { + items.push_back(ctx.mk_eq_atom(mk_strlen(args[i]), mk_int(argLen))); + } + TRACE("t_str_detail", tout << "concat arg: " << mk_pp(args[i], mgr) << " has len = " << argLen.to_string() << std::endl;); + sumLen += argLen; + if (sumLen > strLen) { + items.push_back(ctx.mk_eq_atom(n1, constStr)); + expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); + TRACE("t_str_detail", tout << "inconsistent length: concat (len = " << sumLen << ") <==> string constant (len = " << strLen << ")" << std::endl;); + assert_axiom(toAssert); + return false; + } + } + } + } else { // !is_concat(n1) + rational oLen; + bool oLen_exists = get_len_value(n1, oLen); + if (oLen_exists && oLen != strLen) { + TRACE("t_str_detail", tout << "inconsistent length: var (len = " << oLen << ") <==> string constant (len = " << strLen << ")" << std::endl;); + expr_ref l(ctx.mk_eq_atom(n1, constStr), mgr); + expr_ref r(ctx.mk_eq_atom(mk_strlen(n1), mk_strlen(constStr)), mgr); + assert_implication(l, r); + return false; + } + } + rational unused; + if (get_len_value(n1, unused) == false) { + expr_ref l(ctx.mk_eq_atom(n1, constStr), mgr); + expr_ref r(ctx.mk_eq_atom(mk_strlen(n1), mk_strlen(constStr)), mgr); + assert_implication(l, r); + } + return true; } // returns true if everything is OK, or false if inconsistency detected From 999420485b2b5fb8e58cbe987aad2be855df91d7 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 28 Jul 2016 16:49:39 -0400 Subject: [PATCH 153/562] add theory_str::check_length_eq_var_concat and helper methods --- src/smt/theory_str.cpp | 149 ++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 3 + 2 files changed, 150 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 042ff5808..07fe3e6f6 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4195,11 +4195,156 @@ bool theory_str::check_length_const_string(expr * n1, expr * constStr) { return true; } +bool theory_str::check_length_concat_concat(expr * n1, expr * n2) { + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + + ptr_vector concat1Args; + ptr_vector concat2Args; + get_nodes_in_concat(n1, concat1Args); + get_nodes_in_concat(n2, concat2Args); + + bool concat1LenFixed = true; + bool concat2LenFixed = true; + + expr_ref_vector items(mgr); + + rational sum1(0), sum2(0); + + for (unsigned int i = 0; i < concat1Args.size(); ++i) { + expr * oneArg = concat1Args[i]; + rational argLen; + bool argLen_exists = get_len_value(oneArg, argLen); + if (argLen_exists) { + sum1 += argLen; + if (!m_strutil.is_string(oneArg)) { + items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); + } + } else { + concat1LenFixed = false; + } + } + + for (unsigned int i = 0; i < concat2Args.size(); ++i) { + expr * oneArg = concat2Args[i]; + rational argLen; + bool argLen_exists = get_len_value(oneArg, argLen); + if (argLen_exists) { + sum2 += argLen; + if (!m_strutil.is_string(oneArg)) { + items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); + } + } else { + concat2LenFixed = false; + } + } + + items.push_back(ctx.mk_eq_atom(n1, n2)); + + expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); + + bool conflict = false; + + if (concat1LenFixed && concat2LenFixed) { + if (sum1 != sum2) { + conflict = true; + } + } else if (!concat1LenFixed && concat2LenFixed) { + if (sum1 > sum2) { + conflict = true; + } + } else if (concat1LenFixed && !concat2LenFixed) { + if (sum1 < sum2) { + conflict = true; + } + } + + if (conflict) { + TRACE("t_str_detail", tout << "inconsistent length detected in concat <==> concat" << std::endl;); + return false; + } + return true; +} + +bool theory_str::check_length_concat_var(expr * concat, expr * var) { + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + + rational varLen; + bool varLen_exists = get_len_value(var, varLen); + if (!varLen_exists) { + return true; + } else { + rational sumLen(0); + ptr_vector args; + expr_ref_vector items(mgr); + get_nodes_in_concat(concat, args); + for (unsigned int i = 0; i < args.size(); ++i) { + expr * oneArg = args[i]; + rational argLen; + bool argLen_exists = get_len_value(oneArg, argLen); + if (argLen_exists) { + if (!m_strutil.is_string(oneArg) && !argLen.is_zero()) { + items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); + } + sumLen += argLen; + if (sumLen > varLen) { + TRACE("t_str_detail", tout << "inconsistent length detected in concat <==> var" << std::endl;); + items.push_back(ctx.mk_eq_atom(mk_strlen(var), mk_int(varLen))); + items.push_back(ctx.mk_eq_atom(concat, var)); + expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); + assert_axiom(toAssert); + return false; + } + } + } + return true; + } +} + +bool theory_str::check_length_var_var(expr * var1, expr * var2) { + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + + rational var1Len, var2Len; + bool var1Len_exists = get_len_value(var1, var1Len); + bool var2Len_exists = get_len_value(var2, var2Len); + + if (var1Len_exists && var2Len_exists && var1Len != var2Len) { + TRACE("t_str_detail", tout << "inconsistent length detected in var <==> var" << std::endl;); + expr_ref_vector items(mgr); + items.push_back(ctx.mk_eq_atom(mk_strlen(var1), mk_int(var1Len))); + items.push_back(ctx.mk_eq_atom(mk_strlen(var2), mk_int(var2Len))); + items.push_back(ctx.mk_eq_atom(var1, var2)); + expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); + assert_axiom(toAssert); + return false; + } + return true; +} + // returns true if everything is OK, or false if inconsistency detected // - note that these are different from the semantics in Z3str2 bool theory_str::check_length_eq_var_concat(expr * n1, expr * n2) { - // TODO NEXT - NOT_IMPLEMENTED_YET(); return true; + // n1 and n2 are not const string: either variable or concat + bool n1Concat = is_concat(to_app(n1)); + bool n2Concat = is_concat(to_app(n2)); + if (n1Concat && n2Concat) { + return check_length_concat_concat(n1, n2); + } + // n1 is concat, n2 is variable + else if (n1Concat && (!n2Concat)) { + return check_length_concat_var(n1, n2); + } + // n1 is variable, n2 is concat + else if ((!n1Concat) && n2Concat) { + return check_length_concat_var(n2, n1); + } + // n1 and n2 are both variables + else { + return check_length_var_var(n1, n2); + } + return 0; } // returns false if an inconsistency is detected, or true if no inconsistencies were found diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index d4681856c..bd66e64d4 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -302,6 +302,9 @@ namespace smt { bool check_length_consistency(expr * n1, expr * n2); bool check_length_const_string(expr * n1, expr * constStr); bool check_length_eq_var_concat(expr * n1, expr * n2); + bool check_length_concat_concat(expr * n1, expr * n2); + bool check_length_concat_var(expr * concat, expr * var); + bool check_length_var_var(expr * var1, expr * var2); void get_nodes_in_concat(expr * node, ptr_vector & nodeList); expr * simplify_concat(expr * node); From 244b611f1ca2ebabb1338fbcf0cdb8eb960e0683 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 28 Jul 2016 17:10:41 -0400 Subject: [PATCH 154/562] fix infinite loop bug in theory_str::new_eq_check --- src/smt/theory_str.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 07fe3e6f6..17246ccf8 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1452,24 +1452,24 @@ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { do { enode * eqc_iterator2 = eqc_iterator1; do { - if (eqc_iterator1 == eqc_iterator2) { - continue; - } - // pull terms out of the enodes - app * eqc_nn1 = eqc_iterator1->get_owner(); - app * eqc_nn2 = eqc_iterator2->get_owner(); - TRACE("t_str_detail", tout << "checking whether " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " can be equal" << std::endl;); - if (!can_two_nodes_eq(eqc_nn1, eqc_nn2)) { - TRACE("t_str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " cannot be equal to " << mk_pp(eqc_nn2, m) << std::endl;); - expr_ref to_assert(m.mk_not(ctx.mk_eq_atom(eqc_nn1, eqc_nn2)), m); - assert_axiom(to_assert); - return false; - } - if (!check_length_consistency(eqc_nn1, eqc_nn2)) { - TRACE("t_str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " have inconsistent lengths" << std::endl;); - return false; + if (eqc_iterator1 != eqc_iterator2) { + // pull terms out of the enodes + app * eqc_nn1 = eqc_iterator1->get_owner(); + app * eqc_nn2 = eqc_iterator2->get_owner(); + TRACE("t_str_detail", tout << "checking whether " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " can be equal" << std::endl;); + if (!can_two_nodes_eq(eqc_nn1, eqc_nn2)) { + TRACE("t_str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " cannot be equal to " << mk_pp(eqc_nn2, m) << std::endl;); + expr_ref to_assert(m.mk_not(ctx.mk_eq_atom(eqc_nn1, eqc_nn2)), m); + assert_axiom(to_assert); + return false; + } + if (!check_length_consistency(eqc_nn1, eqc_nn2)) { + TRACE("t_str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " have inconsistent lengths" << std::endl;); + return false; + } } eqc_iterator2 = eqc_iterator2->get_next(); + } while (eqc_iterator2 != eqc_root); eqc_iterator1 = eqc_iterator1->get_next(); From 6f67e9cdda33df556b7026cb215a555b3db4170f Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 28 Jul 2016 17:18:56 -0400 Subject: [PATCH 155/562] fix theory_str::check_length_concat_concat to actually assert the conflict axiom --- src/smt/theory_str.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 17246ccf8..6393d8154 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4261,6 +4261,7 @@ bool theory_str::check_length_concat_concat(expr * n1, expr * n2) { if (conflict) { TRACE("t_str_detail", tout << "inconsistent length detected in concat <==> concat" << std::endl;); + assert_axiom(toAssert); return false; } return true; From 7f3a260eda927dea298761f67aa138fb02f1f5f6 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 30 Jul 2016 16:58:59 -0400 Subject: [PATCH 156/562] more aggressive simplifications in theory_str::handle equality, WIP, not tested yet --- src/smt/theory_str.cpp | 141 +++++++++++++++++------------------------ 1 file changed, 59 insertions(+), 82 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 6393d8154..fe68b02ad 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4779,117 +4779,94 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { // BEGIN new_eq_handler() in strTheory - // TODO there's some setup with getLenValue() that I don't think is necessary - // because we should already be generating the string length axioms for all string terms + { + rational nn1Len, nn2Len; + bool nn1Len_exists = get_len_value(lhs, nn1Len); + bool nn2Len_exists = get_len_value(rhs, nn2Len); + expr * emptyStr = m_strutil.mk_string(""); + + if (nn1Len_exists && nn1Len.is_zero()) { + if (!in_same_eqc(lhs, emptyStr) && rhs != emptyStr) { + expr_ref eql(ctx.mk_eq_atom(mk_strlen(lhs), mk_int(0)), m); + expr_ref eqr(ctx.mk_eq_atom(lhs, emptyStr), m); + expr_ref toAssert(ctx.mk_eq_atom(eql, eqr), m); + assert_axiom(toAssert); + } + } + + if (nn2Len_exists && nn2Len.is_zero()) { + if (!in_same_eqc(rhs, emptyStr) && lhs != emptyStr) { + expr_ref eql(ctx.mk_eq_atom(mk_strlen(rhs), mk_int(0)), m); + expr_ref eqr(ctx.mk_eq_atom(rhs, emptyStr), m); + expr_ref toAssert(ctx.mk_eq_atom(eql, eqr), m); + assert_axiom(toAssert); + } + } + } + + // TODO some setup with haveEQLength() which I skip for now, not sure if necessary instantiate_str_eq_length_axiom(ctx.get_enode(lhs), ctx.get_enode(rhs)); // group terms by equivalence class (groupNodeInEqc()) - std::set eqc_lhs_concat; - std::set eqc_lhs_var; - std::set eqc_lhs_const; - group_terms_by_eqc(lhs, eqc_lhs_concat, eqc_lhs_var, eqc_lhs_const); + // Previously we did the check between LHS and RHS equivalence classes. + // However these have since been merged. + // We start by asserting that the EQCs, in fact, really are merged. + if (!in_same_eqc(lhs, rhs)) { + TRACE("t_str", tout << "BUG: lhs and rhs not in same eqc in new_eq_eh(), loss of invariant!" << std::endl;); + UNREACHABLE(); + } + + std::set eqc_concat; + std::set eqc_var; + std::set eqc_const; + group_terms_by_eqc(lhs, eqc_concat, eqc_var, eqc_const); TRACE("t_str_detail", - tout << "eqc[lhs]:" << std::endl; + tout << "eqc:" << std::endl; tout << "Concats:" << std::endl; - for (std::set::iterator it = eqc_lhs_concat.begin(); it != eqc_lhs_concat.end(); ++it) { + for (std::set::iterator it = eqc_concat.begin(); it != eqc_concat.end(); ++it) { expr * ex = *it; tout << mk_ismt2_pp(ex, get_manager()) << std::endl; } tout << "Variables:" << std::endl; - for (std::set::iterator it = eqc_lhs_var.begin(); it != eqc_lhs_var.end(); ++it) { + for (std::set::iterator it = eqc_var.begin(); it != eqc_var.end(); ++it) { expr * ex = *it; tout << mk_ismt2_pp(ex, get_manager()) << std::endl; } tout << "Constants:" << std::endl; - for (std::set::iterator it = eqc_lhs_const.begin(); it != eqc_lhs_const.end(); ++it) { - expr * ex = *it; - tout << mk_ismt2_pp(ex, get_manager()) << std::endl; - } - ); - - std::set eqc_rhs_concat; - std::set eqc_rhs_var; - std::set eqc_rhs_const; - group_terms_by_eqc(rhs, eqc_rhs_concat, eqc_rhs_var, eqc_rhs_const); - - TRACE("t_str_detail", - tout << "eqc[rhs]:" << std::endl; - tout << "Concats:" << std::endl; - for (std::set::iterator it = eqc_rhs_concat.begin(); it != eqc_rhs_concat.end(); ++it) { - expr * ex = *it; - tout << mk_ismt2_pp(ex, get_manager()) << std::endl; - } - tout << "Variables:" << std::endl; - for (std::set::iterator it = eqc_rhs_var.begin(); it != eqc_rhs_var.end(); ++it) { - expr * ex = *it; - tout << mk_ismt2_pp(ex, get_manager()) << std::endl; - } - tout << "Constants:" << std::endl; - for (std::set::iterator it = eqc_rhs_const.begin(); it != eqc_rhs_const.end(); ++it) { + for (std::set::iterator it = eqc_const.begin(); it != eqc_const.end(); ++it) { expr * ex = *it; tout << mk_ismt2_pp(ex, get_manager()) << std::endl; } ); // step 1: Concat == Concat - // This code block may no longer be useful. - // Z3 seems to be putting LHS and RHS into the same equivalence class extremely early. - // As a result, simplify_concat_equality() is never getting called, - // and if it were called, it would probably get called with the same element on both sides. - - // TODO improve these checks with an all-pairs match over LHS and RHS wrt. other concats - bool hasCommon = false; - if (eqc_lhs_concat.size() != 0 && eqc_rhs_concat.size() != 0) { - std::set::iterator itor1 = eqc_lhs_concat.begin(); - std::set::iterator itor2 = eqc_rhs_concat.begin(); - for (; itor1 != eqc_lhs_concat.end(); ++itor1) { - if (eqc_rhs_concat.find(*itor1) != eqc_rhs_concat.end()) { - hasCommon = true; - break; - } - } - for (; !hasCommon && itor2 != eqc_rhs_concat.end(); ++itor2) { - if (eqc_lhs_concat.find(*itor2) != eqc_lhs_concat.end()) { - hasCommon = true; - break; - } - } - if (!hasCommon) { - simplify_concat_equality(*(eqc_lhs_concat.begin()), *(eqc_rhs_concat.begin())); - } - } - - if (eqc_lhs_concat.size() != 0 && eqc_rhs_concat.size() != 0) { - // let's pick the first concat in the LHS's eqc - // and find some concat in the RHS's eqc that is - // distinct from the first one we picked - expr * lhs = *eqc_lhs_concat.begin(); - std::set::iterator itor2 = eqc_rhs_concat.begin(); - for (; itor2 != eqc_rhs_concat.end(); ++itor2) { - expr * rhs = *itor2; - if (lhs != rhs) { - simplify_concat_equality(lhs, rhs); - break; + // enhancement from Z3str2: all-pairs match over LHS and RHS wrt. other concats + if (eqc_concat.size() != 0) { + std::set::iterator itor1, itor2; + for (itor1 = eqc_concat.begin(); itor1 != eqc_concat.end(); ++itor1) { + for (itor2 = itor1; itor2 != eqc_concat.end(); ++itor2) { + if (itor1 == itor2) { + continue; + } + expr * e1 = *itor1; + expr * e2 = *itor2; + TRACE("t_str_detail", tout << "simplify concat-concat pair " << mk_pp(e1, m) << " and " << mk_pp(e2, m) << std::endl;); + simplify_concat_equality(e1, e2); } } } // step 2: Concat == Constant - if (eqc_lhs_const.size() != 0) { - expr * conStr = *(eqc_lhs_const.begin()); - std::set::iterator itor2 = eqc_rhs_concat.begin(); - for (; itor2 != eqc_rhs_concat.end(); ++itor2) { + // same enhancement as above wrt. Z3str2's behaviour + if (eqc_const.size() != 0) { + expr * conStr = *(eqc_const.begin()); + std::set::iterator itor2; + for (itor2 = eqc_concat.begin(); itor2 != eqc_concat.end(); ++itor2) { solve_concat_eq_str(*itor2, conStr); } - } else if (eqc_rhs_const.size() != 0) { - expr * conStr = *(eqc_rhs_const.begin()); - std::set::iterator itor1 = eqc_lhs_concat.begin(); - for (; itor1 != eqc_lhs_concat.end(); ++itor1) { - solve_concat_eq_str(*itor1, conStr); - } } // simplify parents wrt. the equivalence class of both sides From 8958eea27cf282b86945d7fa86d03d6e60ef6273 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 31 Jul 2016 11:22:04 -0400 Subject: [PATCH 157/562] crash avoidance in theory_str cut_var_map writes --- src/smt/theory_str.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index fe68b02ad..624892ee1 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -306,6 +306,9 @@ bool theory_str::has_self_cut(expr * n1, expr * n2) { } void theory_str::add_cut_info_one_node(expr * baseNode, int slevel, expr * node) { + // crash avoidance? + m_trail.push_back(baseNode); + m_trail.push_back(node); if (cut_var_map.find(baseNode) == cut_var_map.end()) { T_cut * varInfo = alloc(T_cut); varInfo->level = slevel; @@ -334,6 +337,9 @@ void theory_str::add_cut_info_one_node(expr * baseNode, int slevel, expr * node) } void theory_str::add_cut_info_merge(expr * destNode, int slevel, expr * srcNode) { + // crash avoidance? + m_trail.push_back(destNode); + m_trail.push_back(srcNode); if (cut_var_map.find(srcNode) == cut_var_map.end()) { get_manager().raise_exception("illegal state in add_cut_info_merge(): cut_var_map doesn't contain srcNode"); } From f5b82740c36da4279be9eb27f1a151bdc6a6fb6e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 31 Jul 2016 16:26:56 -0400 Subject: [PATCH 158/562] debugging length testers in theory_str::gen_len_val_options_for_free_var --- src/smt/theory_str.cpp | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 624892ee1..a2b3e731b 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -7105,11 +7105,23 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe std::string effectiveLenIndiStr = ""; int lenTesterCount = (int) fvar_lenTester_map[freeVar].size(); + TRACE("t_str_detail", + tout << lenTesterCount << " length testers in fvar_lenTester_map[" << mk_pp(freeVar, m) << "]:" << std::endl; + for (int i = 0; i < lenTesterCount; ++i) { + expr * len_indicator = fvar_lenTester_map[freeVar][i]; + tout << mk_pp(len_indicator, m) << ": "; + bool effectiveInScope = (internal_variable_set.find(len_indicator) != internal_variable_set.end()); + tout << (effectiveInScope ? "in scope" : "NOT in scope"); + tout << std::endl; + } + ); + int i = 0; for (; i < lenTesterCount; ++i) { expr * len_indicator_pre = fvar_lenTester_map[freeVar][i]; // check whether this is in scope as well if (internal_variable_set.find(len_indicator_pre) == internal_variable_set.end()) { + TRACE("t_str_detail", tout << "length indicator " << mk_pp(len_indicator_pre, m) << " not in scope" << std::endl;); continue; } @@ -7133,13 +7145,26 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe << " i = " << i << ", lenTesterCount = " << lenTesterCount << std::endl;); if (i > 0) { effectiveLenInd = fvar_lenTester_map[freeVar][i - 1]; + bool effectiveHasEqcValue; + expr * effective_eqc_value = get_eqc_value(effectiveLenInd, effectiveHasEqcValue); + bool effectiveInScope = (internal_variable_set.find(effectiveLenInd) != internal_variable_set.end()); + TRACE("t_str_detail", tout << "checking effective length indicator " << mk_pp(effectiveLenInd, m) << ": " + << (effectiveInScope ? "in scope" : "NOT in scope") << ", "; + if (effectiveHasEqcValue) { + tout << "~= " << mk_pp(effective_eqc_value, m); + } else { + tout << "no eqc string constant"; + } + tout << std::endl;); if (effectiveLenInd == lenTesterInCbEq) { effectiveLenIndiStr = lenTesterValue; } else { - bool effectiveHasEqcValue = false; - const char * val = 0; - m_strutil.is_string(get_eqc_value(effectiveLenInd, effectiveHasEqcValue), & val); - effectiveLenIndiStr = val; + if (effectiveHasEqcValue) { + effectiveLenIndiStr = m_strutil.get_string_constant_value(effective_eqc_value); + } else { + // TODO this should be unreachable, but can we really do anything here? + NOT_IMPLEMENTED_YET(); + } } } break; @@ -7169,6 +7194,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe fvar_lenTester_map[freeVar].push_back(indicator); lenTester_fvar_map[indicator] = freeVar; } else { + // TODO make absolutely sure this is safe to do if 'indicator' is technically out of scope indicator = fvar_lenTester_map[freeVar][i]; testNum = i + 1; } From 41497f44c12236b74b6abda63bcf2d225c171873 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 31 Jul 2016 16:30:52 -0400 Subject: [PATCH 159/562] prevent checking scope of XOR variables in theory_str::process_concat_eq --- src/smt/theory_str.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index a2b3e731b..a6db9112f 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2417,7 +2417,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } else { if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end() || internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end() - || internal_variable_set.find((entry1->second)[2]) == internal_variable_set.end()) { + /*|| internal_variable_set.find((entry1->second)[2]) == internal_variable_set.end() */) { entry1InScope = false; } else { entry1InScope = true; @@ -2430,7 +2430,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } else { if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end() || internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end() - || internal_variable_set.find((entry2->second)[2]) == internal_variable_set.end()) { + /* || internal_variable_set.find((entry2->second)[2]) == internal_variable_set.end() */) { entry2InScope = false; } else { entry2InScope = true; @@ -2738,8 +2738,6 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); // prevent checking scope for the XOR term, as it's always in the same scope as the split var - // TODO probably make this change everywhere else in process_concat_eq*, - // and also make sure this is correct. bool entry1InScope; if (entry1 == varForBreakConcat.end()) { @@ -3061,7 +3059,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { entry1InScope = false; } else { if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end() - || internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end()) { + /* || internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end() */) { entry1InScope = false; } else { entry1InScope = true; @@ -3073,7 +3071,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { entry2InScope = false; } else { if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end() - || internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end()) { + /* || internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end() */) { entry2InScope = false; } else { entry2InScope = true; @@ -3560,7 +3558,7 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { entry1InScope = false; } else { if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end() - || internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end()) { + /* || internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end() */) { entry1InScope = false; } else { entry1InScope = true; @@ -3572,7 +3570,7 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { entry2InScope = false; } else { if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end() - || internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end()) { + /* || internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end() */) { entry2InScope = false; } else { entry2InScope = true; From 9ceb2df28f868e700c3ed5be33d69d7c02d84181 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 31 Jul 2016 16:51:35 -0400 Subject: [PATCH 160/562] add integer integration to theory_str::simplify_parent --- src/smt/theory_str.cpp | 88 +++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 45 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index a6db9112f..4560de950 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1564,6 +1564,14 @@ expr * theory_str::eval_concat(expr * n1, expr * n2) { return NULL; } +static inline std::string rational_to_string_if_exists(const rational & x, bool x_exists) { + if (x_exists) { + return x.to_string(); + } else { + return "?"; + } +} + /* * The inputs: * ~ nn: non const node @@ -1610,39 +1618,34 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { expr * arg0 = a_parent->get_arg(0); expr * arg1 = a_parent->get_arg(1); - // TODO getLenValue() - // int parentLen = getLenValue(a_parent) - int parentLen = -1; + rational parentLen; + bool parentLen_exists = get_len_value(a_parent, parentLen); + if (arg0 == n_eq_enode->get_owner()) { - // TODO getLenValue() - // int arg0Len = getLenValue(eq_str); - // int arg1Len = getLenValue(arg1); - int arg0Len = -1; - int arg1Len = -1; + rational arg0Len, arg1Len; + bool arg0Len_exists = get_len_value(eq_str, arg0Len); + bool arg1Len_exists = get_len_value(arg1, arg1Len); TRACE("t_str_detail", tout << "simplify_parent #1:" << std::endl << "* parent = " << mk_ismt2_pp(a_parent, m) << std::endl - << "* |parent| = " << parentLen << std::endl - << "* |arg0| = " << arg0Len << std::endl - << "* |arg1| = " << arg1Len << std::endl; + << "* |parent| = " << rational_to_string_if_exists(parentLen, parentLen_exists) << std::endl + << "* |arg0| = " << rational_to_string_if_exists(arg0Len, arg0Len_exists) << std::endl + << "* |arg1| = " << rational_to_string_if_exists(arg1Len, arg1Len_exists) << std::endl; ); - if (parentLen != -1 && arg1Len == -1) { - // TODO after getLenValue() above - /* - Z3_ast implyL11 = mk_2_and(t, Z3_mk_eq(ctx, mk_length(t, parent), mk_int(ctx, parentLen)), - Z3_mk_eq(ctx, mk_length(t, arg0), mk_int(ctx, arg0Len))); - int makeUpLenArg1 = parentLen - arg0Len; - Z3_ast lenAss = NULL; - if (makeUpLenArg1 >= 0) { - Z3_ast implyR11 = Z3_mk_eq(ctx, mk_length(t, arg1), mk_int(ctx, makeUpLenArg1)); - lenAss = Z3_mk_implies(ctx, implyL11, implyR11); + if (parentLen_exists && !arg1Len_exists) { + TRACE("t_str_detail", tout << "make up len for arg1" << std::endl;); + expr_ref implyL11(m.mk_and(ctx.mk_eq_atom(mk_strlen(a_parent), mk_int(parentLen)), + ctx.mk_eq_atom(mk_strlen(arg0), mk_int(arg0Len))), m); + rational makeUpLenArg1 = parentLen - arg0Len; + if (makeUpLenArg1.is_nonneg()) { + expr_ref implyR11(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(makeUpLenArg1)), m); + assert_implication(implyL11, implyR11); } else { - lenAss = Z3_mk_not(ctx, implyL11); + expr_ref neg(m.mk_not(implyL11), m); + assert_axiom(neg); } - addAxiom(t, lenAss, __LINE__); - */ } // (Concat n_eqNode arg1) /\ arg1 has eq const @@ -1691,35 +1694,30 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { } // if (arg0 == n_eq_enode->get_owner()) if (arg1 == n_eq_enode->get_owner()) { - // TODO getLenValue() - // int arg0Len = getLenValue(arg0); - // int arg1Len = getLenValue(eq_str); - int arg0Len = -1; - int arg1Len = -1; + rational arg0Len, arg1Len; + bool arg0Len_exists = get_len_value(arg0, arg0Len); + bool arg1Len_exists = get_len_value(eq_str, arg1Len); TRACE("t_str_detail", tout << "simplify_parent #2:" << std::endl << "* parent = " << mk_ismt2_pp(a_parent, m) << std::endl - << "* |parent| = " << parentLen << std::endl - << "* |arg0| = " << arg0Len << std::endl - << "* |arg1| = " << arg1Len << std::endl; + << "* |parent| = " << rational_to_string_if_exists(parentLen, parentLen_exists) << std::endl + << "* |arg0| = " << rational_to_string_if_exists(arg0Len, arg0Len_exists) << std::endl + << "* |arg1| = " << rational_to_string_if_exists(arg1Len, arg1Len_exists) << std::endl; ); - if (parentLen != -1 && arg0Len == -1) { - // TODO after getLenValue() above - /* - Z3_ast implyL11 = mk_2_and(t, Z3_mk_eq(ctx, mk_length(t, parent), mk_int(ctx, parentLen)), - Z3_mk_eq(ctx, mk_length(t, arg1), mk_int(ctx, arg1Len))); - int makeUpLenArg0 = parentLen - arg1Len; - Z3_ast lenAss = NULL; - if (makeUpLenArg0 >= 0) { - Z3_ast implyR11 = Z3_mk_eq(ctx, mk_length(t, arg0), mk_int(ctx, makeUpLenArg0)); - lenAss = Z3_mk_implies(ctx, implyL11, implyR11); + if (parentLen_exists && !arg0Len_exists) { + TRACE("t_str_detail", tout << "make up len for arg0" << std::endl;); + expr_ref implyL11(m.mk_and(ctx.mk_eq_atom(mk_strlen(a_parent), mk_int(parentLen)), + ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1Len))), m); + rational makeUpLenArg0 = parentLen - arg1Len; + if (makeUpLenArg0.is_nonneg()) { + expr_ref implyR11(ctx.mk_eq_atom(mk_strlen(arg0), mk_int(makeUpLenArg0)), m); + assert_implication(implyL11, implyR11); } else { - lenAss = Z3_mk_not(ctx, implyL11); + expr_ref neg(m.mk_not(implyL11), m); + assert_axiom(neg); } - addAxiom(t, lenAss, __LINE__); - */ } // (Concat arg0 n_eqNode) /\ arg0 has eq const From 778c0a5563734d08f3e80a97ed18fc61a8b67ac9 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 31 Jul 2016 16:55:17 -0400 Subject: [PATCH 161/562] improve theory_str::group_terms_by_eqc now that we have simplify_concat --- src/smt/theory_str.cpp | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 4560de950..0ad47f828 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1495,23 +1495,20 @@ void theory_str::group_terms_by_eqc(expr * n, std::set & concats, std::se do { app * ast = eqcNode->get_owner(); if (is_concat(eqcNode)) { - // TODO simplify_concat - /* - Z3_ast simConcat = simplifyConcat(t, eqcNode); - if (simConcat != eqcNode) { - if (isConcatFunc(t, simConcat)) { - concats.insert(simConcat); + expr * simConcat = simplify_concat(ast); + if (simConcat != ast) { + if (is_concat(to_app(simConcat))) { + concats.insert(simConcat); } else { - if (isConstStr(t, simConcat)) { - constStrs.insert(simConcat); - } else { - vars.insert(simConcat); - } + if (m_strutil.is_string(simConcat)) { + consts.insert(simConcat); + } else { + vars.insert(simConcat); + } } - } else { + } else { concats.insert(simConcat); - } - */ + } concats.insert(ast); } else if (is_string(eqcNode)) { consts.insert(ast); From 6e348720b1c9994bc117283ae456af7fbbf5a46a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 31 Jul 2016 18:12:57 -0400 Subject: [PATCH 162/562] add integer theory integration to theory_str::solve_concat_eq_str case 4 --- src/smt/theory_str.cpp | 96 +++++++++++++++++++++++++++++++++--------- 1 file changed, 77 insertions(+), 19 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 0ad47f828..2b94b0c40 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4555,12 +4555,74 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } else { // Case 4: Concat(var, var) == const TRACE("t_str", tout << "Case 4: Concat(var, var) == const" << std::endl;); - // TODO large additions required in this section - if (true) { /* if (Concat(arg1, arg2) == NULL) { */ - int arg1Len = -1; /* = getLenValue(arg1); */ - int arg2Len = -1; /* = getLenValue(arg2); */ - if (arg1Len != -1 || arg2Len != -1) { - NOT_IMPLEMENTED_YET(); // TODO + if (eval_concat(arg1, arg2) == NULL) { + rational arg1Len, arg2Len; + bool arg1Len_exists = get_len_value(arg1, arg1Len); + bool arg2Len_exists = get_len_value(arg2, arg2Len); + rational concatStrLen((unsigned)const_str.length()); + if (arg1Len_exists || arg2Len_exists) { + expr_ref ax_l1(ctx.mk_eq_atom(concat, str), m); + expr_ref ax_l2(m); + std::string prefixStr, suffixStr; + if (arg1Len_exists) { + if (arg1Len.is_neg()) { + TRACE("t_str_detail", tout << "length conflict: arg1Len = " << arg1Len << ", concatStrLen = " << concatStrLen << std::endl;); + expr_ref toAssert(m_autil.mk_ge(mk_strlen(arg1), mk_int(0)), m); + assert_axiom(toAssert); + return; + } else if (arg1Len > concatStrLen) { + TRACE("t_str_detail", tout << "length conflict: arg1Len = " << arg1Len << ", concatStrLen = " << concatStrLen << std::endl;); + expr_ref ax_r1(m_autil.mk_le(mk_strlen(arg1), mk_int(concatStrLen)), m); + assert_implication(ax_l1, ax_r1); + return; + } + + prefixStr = const_str.substr(0, arg1Len.get_unsigned()); + rational concat_minus_arg1 = concatStrLen - arg1Len; + suffixStr = const_str.substr(arg1Len.get_unsigned(), concat_minus_arg1.get_unsigned()); + ax_l2 = ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1Len)); + } else { + // arg2's length is available + if (arg2Len.is_neg()) { + TRACE("t_str_detail", tout << "length conflict: arg2Len = " << arg2Len << ", concatStrLen = " << concatStrLen << std::endl;); + expr_ref toAssert(m_autil.mk_ge(mk_strlen(arg2), mk_int(0)), m); + assert_axiom(toAssert); + return; + } else if (arg2Len > concatStrLen) { + TRACE("t_str_detail", tout << "length conflict: arg2Len = " << arg2Len << ", concatStrLen = " << concatStrLen << std::endl;); + expr_ref ax_r1(m_autil.mk_le(mk_strlen(arg2), mk_int(concatStrLen)), m); + assert_implication(ax_l1, ax_r1); + return; + } + + rational concat_minus_arg2 = concatStrLen - arg2Len; + prefixStr = const_str.substr(0, concat_minus_arg2.get_unsigned()); + suffixStr = const_str.substr(concat_minus_arg2.get_unsigned(), arg2Len.get_unsigned()); + ax_l2 = ctx.mk_eq_atom(mk_strlen(arg2), mk_int(arg2Len)); + } + // consistency check + if (is_concat(to_app(arg1)) && !can_concat_eq_str(arg1, prefixStr)) { + expr_ref ax_r(m.mk_not(ax_l2), m); + assert_implication(ax_l1, ax_r); + return; + } + if (is_concat(to_app(arg2)) && !can_concat_eq_str(arg2, suffixStr)) { + expr_ref ax_r(m.mk_not(ax_l2), m); + assert_implication(ax_l1, ax_r); + return; + } + expr_ref_vector r_items(m); + r_items.push_back(ctx.mk_eq_atom(arg1, m_strutil.mk_string(prefixStr))); + r_items.push_back(ctx.mk_eq_atom(arg2, m_strutil.mk_string(suffixStr))); + if (!arg1Len_exists) { + r_items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(prefixStr.size()))); + } + if (!arg2Len_exists) { + r_items.push_back(ctx.mk_eq_atom(mk_strlen(arg2), mk_int(suffixStr.size()))); + } + expr_ref lhs(m.mk_and(ax_l1, ax_l2), m); + expr_ref rhs(mk_and(r_items), m); + assert_implication(lhs, rhs); } else { /* ! (arg1Len != 1 || arg2Len != 1) */ expr_ref xorFlag(m); std::pair key1(arg1, arg2); @@ -4569,6 +4631,8 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { // check the entries in this map to make sure they're still in scope // before we use them. + // TODO XOR variables will always show up as "not in scope" because of how we update internal_variable_set + std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); @@ -4609,10 +4673,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { int concatStrLen = const_str.length(); int xor_pos = 0; int and_count = 1; - /* - expr ** xor_items = new expr*[concatStrLen + 1]; - expr ** and_items = new expr*[4 * (concatStrLen+1) + 1]; - */ + expr ** xor_items = alloc_svect(expr*, (concatStrLen+1)); expr ** and_items = alloc_svect(expr*, (4 * (concatStrLen+1) + 1)); @@ -4620,15 +4681,12 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { std::string prefixStr = const_str.substr(0, i); std::string suffixStr = const_str.substr(i, concatStrLen - i); // skip invalid options - // TODO canConcatEqStr() checks: - /* - if (isConcatFunc(t, arg1) && canConcatEqStr(t, arg1, prefixStr) == 0) { - continue; - } - if (isConcatFunc(t, arg2) && canConcatEqStr(t, arg2, suffixStr) == 0) { - continue; - } - */ + if (is_concat(to_app(arg1)) && !can_concat_eq_str(arg1, prefixStr)) { + continue; + } + if (is_concat(to_app(arg2)) && !can_concat_eq_str(arg2, suffixStr)) { + continue; + } expr_ref xorAst(ctx.mk_eq_atom(xorFlag, m_autil.mk_numeral(rational(xor_pos), true)), m); xor_items[xor_pos++] = xorAst; From ee1af96f1bf92d1546449dc30573b62f27c34fae Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 1 Aug 2016 17:05:02 -0400 Subject: [PATCH 163/562] add opt_NoQuickReturn_IntegerTheory check in theory_str::new_eq_check() This allows us to assert an "inconsistent length" axiom from the integer theory while continuing in new_eq_handler(). Currently active when opt_NoQuickReturn_IntegerTheory is 'true' but this may be necessary here and in other places, in general, to fix integer theory integration. --- src/smt/theory_str.cpp | 13 +++++++++---- src/smt/theory_str.h | 10 ++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 2b94b0c40..a80fd2165 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -33,7 +33,7 @@ theory_str::theory_str(ast_manager & m): opt_EagerStringConstantLengthAssertions(true), opt_VerifyFinalCheckProgress(true), opt_LCMUnrollStep(2), - opt_NoQuickReturn_Concat_IntegerTheory(false), + opt_NoQuickReturn_IntegerTheory(true), opt_DisableIntegerTheoryIntegration(false), /* Internal setup */ search_started(false), @@ -1467,11 +1467,16 @@ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { TRACE("t_str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " cannot be equal to " << mk_pp(eqc_nn2, m) << std::endl;); expr_ref to_assert(m.mk_not(ctx.mk_eq_atom(eqc_nn1, eqc_nn2)), m); assert_axiom(to_assert); + // this shouldn't use the integer theory at all, so we don't allow the option of quick-return return false; } if (!check_length_consistency(eqc_nn1, eqc_nn2)) { TRACE("t_str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " have inconsistent lengths" << std::endl;); - return false; + if (opt_NoQuickReturn_IntegerTheory){ + TRACE("t_str_detail", tout << "continuing in new_eq_check() due to opt_NoQuickReturn_IntegerTheory" << std::endl;); + } else { + return false; + } } } eqc_iterator2 = eqc_iterator2->get_next(); @@ -2175,7 +2180,7 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { assert_implication(premise, conclusion); - if (opt_NoQuickReturn_Concat_IntegerTheory) { + if (opt_NoQuickReturn_IntegerTheory) { TRACE("t_str_detail", tout << "bypassing quick return from the end of this case" << std::endl;); } else { return; @@ -2195,7 +2200,7 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { expr_ref conclusion(m.mk_and(ax_r1, ax_r2), m); assert_implication(premise, conclusion); - if (opt_NoQuickReturn_Concat_IntegerTheory) { + if (opt_NoQuickReturn_IntegerTheory) { TRACE("t_str_detail", tout << "bypassing quick return from the end of this case" << std::endl;); } else { return; diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index bd66e64d4..74c1786df 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -95,14 +95,12 @@ namespace smt { int opt_LCMUnrollStep; /* - * If NoQuickReturn_Concat_IntegerTheory is set to true, - * the integer theory integration conditionals in simplify_concat_equality() + * If NoQuickReturn_IntegerTheory is set to true, + * integer theory integration checks that assert axioms * will not return from the function after asserting their axioms. - * This means that control will fall through to the type 1-6 axioms, - * causing those to be added as well. - * The default behaviour of Z3str2 is to set this to 'false'. + * The default behaviour of Z3str2 is to set this to 'false'. This may be incorrect. */ - bool opt_NoQuickReturn_Concat_IntegerTheory; + bool opt_NoQuickReturn_IntegerTheory; /* * If DisableIntegerTheoryIntegration is set to true, From 97f07a8a7c6a8c1379da46abcf9bb7c58652c0e4 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 1 Aug 2016 18:14:56 -0400 Subject: [PATCH 164/562] fix debugging statements in theory_str::gen_len_test_options this fixes charAt-007.smt2 and prevents two unique crashes --- src/smt/theory_str.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index a80fd2165..ee17edb9c 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -6998,7 +6998,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr ast_manager & m = get_manager(); context & ctx = get_context(); - TRACE("t_str_detail", tout << "entry" << std::endl;); + //TRACE("t_str_detail", tout << "entry" << std::endl;); expr_ref freeVarLen(mk_strlen(freeVar), m); SASSERT(freeVarLen); @@ -7020,9 +7020,9 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr for (int i = l; i < h; ++i) { std::string i_str = int_to_string(i); expr_ref str_indicator(m_strutil.mk_string(i_str), m); - TRACE("t_str_detail", tout << "just created a string term: " << mk_ismt2_pp(str_indicator, m) << std::endl;); + //TRACE("t_str_detail", tout << "just created a string term: " << mk_ismt2_pp(str_indicator, m) << std::endl;); expr * or_expr = m.mk_eq(indicator, str_indicator); // ARGUMENT 2 IS BOGUS! WRONG SORT - TRACE("t_str_detail", tout << "or_expr = " << mk_ismt2_pp(or_expr, m) << std::endl;); + //TRACE("t_str_detail", tout << "or_expr = " << mk_ismt2_pp(or_expr, m) << std::endl;); orList.push_back(or_expr); if (opt_AggressiveLengthTesting) { @@ -7032,7 +7032,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr } expr * and_expr = m.mk_eq(orList[orList.size() - 1], m.mk_eq(freeVarLen, mk_int(i))); - TRACE("t_str_detail", tout << "and_expr = " << mk_ismt2_pp(and_expr, m) << std::endl;); + //TRACE("t_str_detail", tout << "and_expr = " << mk_ismt2_pp(and_expr, m) << std::endl;); andList.push_back(and_expr); } @@ -7063,7 +7063,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr expr * lenTestAssert = m.mk_and(andList.size() + 1, and_items); SASSERT(lenTestAssert != NULL); - TRACE("t_str_detail", tout << "lenTestAssert = " << mk_ismt2_pp(lenTestAssert, m) << std::endl;); + //TRACE("t_str_detail", tout << "lenTestAssert = " << mk_ismt2_pp(lenTestAssert, m) << std::endl;); expr * assertL = NULL; int testerCount = tries - 1; @@ -7081,13 +7081,13 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr } if (assertL != NULL) { - TRACE("t_str_detail", tout << "assertL = " << mk_ismt2_pp(assertL, m) << std::endl;); + m_trail.push_back(assertL); // return the axiom (assertL -> lenTestAssert) // would like to use mk_implies() here but... lenTestAssert = m.mk_or(m.mk_not(assertL), lenTestAssert); } - TRACE("t_str_detail", tout << "exit" << std::endl;); + //TRACE("t_str_detail", tout << "exit" << std::endl;); return lenTestAssert; From a51ad07e3f59f2db46f7534283e68729d33863b0 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 1 Aug 2016 20:52:42 -0400 Subject: [PATCH 165/562] crash avoidance in propagation of basic string axioms and gen_len_test_options --- src/smt/theory_str.cpp | 99 +++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 44 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ee17edb9c..b96e454eb 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -237,6 +237,7 @@ bool theory_str::internalize_term(app * term) { if (opt_EagerStringConstantLengthAssertions && m_strutil.is_string(term)) { TRACE("t_str", tout << "eagerly asserting length of string term " << mk_pp(term, m) << std::endl;); m_basicstr_axiom_todo.insert(e); + TRACE("t_str_axiom_bug", tout << "add " << mk_pp(e->get_owner(), m) << " to m_basicstr_axiom_todo" << std::endl;); } theory_var v = mk_var(e); @@ -404,6 +405,7 @@ expr * theory_str::mk_internal_lenTest_var(expr * node, int lTries) { std::string name = ss.str(); app * var = mk_str_var(name); internal_lenTest_vars.insert(var); + m_trail.push_back(var); return var; } @@ -415,6 +417,7 @@ expr * theory_str::mk_internal_valTest_var(expr * node, int len, int vTries) { std::string name = ss.str(); app * var = mk_str_var(name); internal_valTest_vars.insert(var); + m_trail.push_back(var); return var; } @@ -494,6 +497,7 @@ app * theory_str::mk_str_var(std::string name) { // this might help?? mk_var(ctx.get_enode(a)); m_basicstr_axiom_todo.push_back(ctx.get_enode(a)); + TRACE("t_str_axiom_bug", tout << "add " << mk_pp(a, m) << " to m_basicstr_axiom_todo" << std::endl;); m_trail.push_back(a); variable_set.insert(a); @@ -515,6 +519,7 @@ app * theory_str::mk_regex_rep_var() { SASSERT(ctx.e_internalized(a)); mk_var(ctx.get_enode(a)); m_basicstr_axiom_todo.push_back(ctx.get_enode(a)); + TRACE("t_str_axiom_bug", tout << "add " << mk_pp(a, m) << " to m_basicstr_axiom_todo" << std::endl;); m_trail.push_back(a); // TODO cross-check which variable sets we need @@ -689,6 +694,7 @@ void theory_str::propagate() { instantiate_basic_string_axioms(m_basicstr_axiom_todo[i]); } m_basicstr_axiom_todo.reset(); + TRACE("t_str_axiom_bug", tout << "reset m_basicstr_axiom_todo" << std::endl;); for (unsigned i = 0; i < m_str_eq_todo.size(); ++i) { std::pair pair = m_str_eq_todo[i]; @@ -811,6 +817,8 @@ void theory_str::instantiate_basic_string_axioms(enode * str) { context & ctx = get_context(); ast_manager & m = get_manager(); + TRACE("t_str_axiom_bug", tout << "set up basic string axioms on " << mk_pp(str->get_owner(), m) << std::endl;); + // TESTING: attempt to avoid a crash here when a variable goes out of scope // TODO this seems to work so we probably need to do this for other propagate checks, etc. if (str->get_iscope_lvl() > ctx.get_scope_level()) { @@ -5010,6 +5018,7 @@ void theory_str::set_up_axioms(expr * ex) { enode * n = ctx.get_enode(ex); SASSERT(n); m_basicstr_axiom_todo.push_back(n); + TRACE("t_str_axiom_bug", tout << "add " << mk_pp(ex, m) << " to m_basicstr_axiom_todo" << std::endl;); if (is_app(ex)) { @@ -5222,6 +5231,22 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { vars.clear(); } } + + // TODO if this works, possibly remove axioms from other vectors as well + ptr_vector new_m_basicstr; + for (ptr_vector::iterator it = m_basicstr_axiom_todo.begin(); it != m_basicstr_axiom_todo.end(); ++it) { + enode * e = *it; + app * a = e->get_owner(); + TRACE("t_str_axiom_bug", tout << "consider deleting " << mk_pp(a, get_manager()) + << ", enode scope level is " << e->get_iscope_lvl() + << std::endl;); + if (e->get_iscope_lvl() <= (unsigned)sLevel) { + new_m_basicstr.push_back(e); + } + } + m_basicstr_axiom_todo.reset(); + m_basicstr_axiom_todo = new_m_basicstr; + theory::pop_scope_eh(num_scopes); } @@ -6998,13 +7023,11 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr ast_manager & m = get_manager(); context & ctx = get_context(); - //TRACE("t_str_detail", tout << "entry" << std::endl;); - expr_ref freeVarLen(mk_strlen(freeVar), m); SASSERT(freeVarLen); - ptr_vector orList; - ptr_vector andList; + expr_ref_vector orList(m); + expr_ref_vector andList(m); int distance = 3; int l = (tries - 1) * distance; @@ -7020,9 +7043,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr for (int i = l; i < h; ++i) { std::string i_str = int_to_string(i); expr_ref str_indicator(m_strutil.mk_string(i_str), m); - //TRACE("t_str_detail", tout << "just created a string term: " << mk_ismt2_pp(str_indicator, m) << std::endl;); - expr * or_expr = m.mk_eq(indicator, str_indicator); // ARGUMENT 2 IS BOGUS! WRONG SORT - //TRACE("t_str_detail", tout << "or_expr = " << mk_ismt2_pp(or_expr, m) << std::endl;); + expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); // ARGUMENT 2 IS BOGUS! WRONG SORT orList.push_back(or_expr); if (opt_AggressiveLengthTesting) { @@ -7031,8 +7052,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr ctx.force_phase(l); } - expr * and_expr = m.mk_eq(orList[orList.size() - 1], m.mk_eq(freeVarLen, mk_int(i))); - //TRACE("t_str_detail", tout << "and_expr = " << mk_ismt2_pp(and_expr, m) << std::endl;); + expr_ref and_expr(ctx.mk_eq_atom(orList.get(orList.size() - 1), m.mk_eq(freeVarLen, mk_int(i))), m); andList.push_back(and_expr); } @@ -7043,54 +7063,44 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr ctx.force_phase(~l); } - andList.push_back(m.mk_eq(orList[orList.size() - 1], m_autil.mk_ge(freeVarLen, mk_int(h)))); + andList.push_back(ctx.mk_eq_atom(orList.get(orList.size() - 1), m_autil.mk_ge(freeVarLen, mk_int(h)))); - // TODO refactor this to use expr_ref_vector/svector/buffer instead - expr ** or_items = alloc_svect(expr*, orList.size()); - expr ** and_items = alloc_svect(expr*, andList.size() + 1); + expr_ref_vector or_items(m); + expr_ref_vector and_items(m); for (unsigned i = 0; i < orList.size(); ++i) { - SASSERT(orList[i] != NULL); - or_items[i] = orList[i]; + or_items.push_back(orList.get(i)); } - and_items[0] = m.mk_or(orList.size(), or_items); - SASSERT(and_items[0] != NULL); + and_items.push_back(mk_or(or_items)); for(unsigned i = 0; i < andList.size(); ++i) { - SASSERT(andList[i] != NULL); - and_items[i+1] = andList[i]; + and_items.push_back(andList.get(i)); } - expr * lenTestAssert = m.mk_and(andList.size() + 1, and_items); - SASSERT(lenTestAssert != NULL); - //TRACE("t_str_detail", tout << "lenTestAssert = " << mk_ismt2_pp(lenTestAssert, m) << std::endl;); + TRACE("t_str_detail", tout << "check: " << mk_pp(mk_and(and_items), m) << std::endl;); + + expr_ref lenTestAssert = mk_and(and_items); + SASSERT(lenTestAssert); + TRACE("t_str_detail", tout << "crash avoidance lenTestAssert: " << mk_pp(lenTestAssert, m) << std::endl;); - expr * assertL = NULL; int testerCount = tries - 1; if (testerCount > 0) { - expr ** and_items_LHS = alloc_svect(expr*, testerCount); - expr * moreAst = m_strutil.mk_string("more"); + expr_ref_vector and_items_LHS(m); + expr_ref moreAst(m_strutil.mk_string("more"), m); for (int i = 0; i < testerCount; ++i) { - and_items_LHS[i] = m.mk_eq(fvar_lenTester_map[freeVar][i], moreAst); - } - if (testerCount == 1) { - assertL = and_items_LHS[0]; - } else { - assertL = m.mk_and(testerCount, and_items_LHS); + and_items_LHS.push_back(ctx.mk_eq_atom(fvar_lenTester_map[freeVar][i], moreAst)); } + expr_ref assertL(mk_and(and_items_LHS), m); + SASSERT(assertL); + expr * finalAxiom = m.mk_or(m.mk_not(assertL), lenTestAssert.get()); + SASSERT(finalAxiom != NULL); + TRACE("t_str_detail", tout << "crash avoidance finalAxiom: " << mk_pp(finalAxiom, m) << std::endl;); + return finalAxiom; + } else { + TRACE("t_str_detail", tout << "crash avoidance lenTestAssert.get(): " << mk_pp(lenTestAssert.get(), m) << std::endl;); + m_trail.push_back(lenTestAssert.get()); + return lenTestAssert.get(); } - - if (assertL != NULL) { - m_trail.push_back(assertL); - // return the axiom (assertL -> lenTestAssert) - // would like to use mk_implies() here but... - lenTestAssert = m.mk_or(m.mk_not(assertL), lenTestAssert); - } - - //TRACE("t_str_detail", tout << "exit" << std::endl;); - - return lenTestAssert; - } // ----------------------------------------------------------------------------------------------------- @@ -7237,7 +7247,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe } // for (i : [0..lenTesterCount-1]) if (effectiveLenIndiStr == "more" || effectiveLenIndiStr == "") { TRACE("t_str", tout << "length is not fixed; generating length tester options for free var" << std::endl;); - expr * indicator = NULL; + expr_ref indicator(m); unsigned int testNum = 0; TRACE("t_str", tout << "effectiveLenIndiStr = " << effectiveLenIndiStr @@ -7261,6 +7271,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe TRACE("t_str", tout << "length is fixed; generating models for free var" << std::endl;); // length is fixed expr * valueAssert = gen_free_var_options(freeVar, effectiveLenInd, effectiveLenIndiStr, NULL, ""); + SASSERT(valueAssert != NULL); return valueAssert; } } // fVarLenCountMap.find(...) From 45c495495975a9b88b2996b4c06e46627a87bd01 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 2 Aug 2016 14:52:44 -0400 Subject: [PATCH 166/562] add debugging to theory_str::get_len_value in preparation for fixes --- src/smt/theory_str.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index b96e454eb..395e1dab6 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -33,7 +33,7 @@ theory_str::theory_str(ast_manager & m): opt_EagerStringConstantLengthAssertions(true), opt_VerifyFinalCheckProgress(true), opt_LCMUnrollStep(2), - opt_NoQuickReturn_IntegerTheory(true), + opt_NoQuickReturn_IntegerTheory(false), opt_DisableIntegerTheoryIntegration(false), /* Internal setup */ search_started(false), @@ -3882,6 +3882,7 @@ bool theory_str::get_len_value(expr* e, rational& val) { context& ctx = get_context(); ast_manager & m = get_manager(); + theory* th = ctx.get_theory(m_autil.get_family_id()); if (!th) { TRACE("t_str_int", tout << "oops, can't get m_autil's theory" << std::endl;); @@ -3926,6 +3927,18 @@ bool theory_str::get_len_value(expr* e, rational& val) { if (ctx.e_internalized(len)) { enode * e_len = ctx.get_enode(len); tout << "has " << e_len->get_num_th_vars() << " theory vars" << std::endl; + + // eqc debugging + { + tout << "dump equivalence class of " << mk_pp(len, get_manager()) << std::endl; + enode * nNode = ctx.get_enode(len); + enode * eqcNode = nNode; + do { + app * ast = eqcNode->get_owner(); + tout << mk_pp(ast, get_manager()) << std::endl; + eqcNode = eqcNode->get_next(); + } while (eqcNode != nNode); + } } }); @@ -3939,6 +3952,7 @@ bool theory_str::get_len_value(expr* e, rational& val) { } } } + TRACE("t_str_int", tout << "length of " << mk_ismt2_pp(e, m) << " is " << val << std::endl;); return val.is_int(); } From 3c2fe497de4fee845f36e7a7bfc9b4860a6d265f Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 2 Aug 2016 16:44:54 -0400 Subject: [PATCH 167/562] modify theory_str::get_value() to check EQC for a numeral Instead of asking the arithmetic theory for the current assignment, we return the (unique) numeral in the equivalence class of the term whose length we want to know. This is because the arithmetic theory may return a default / internal value that doesn't correspond to anything actually asserted by the core solver. --- src/smt/theory_str.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 395e1dab6..2ae5dcec5 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3832,13 +3832,13 @@ bool theory_str::get_value(expr* e, rational& val) const { enode * en_e = ctx.get_enode(e); enode * it = en_e; do { - if (tha->get_value(it, _val)) { + if (m_autil.is_numeral(it->get_owner(), val) && val.is_int()) { // found an arithmetic term - TRACE("t_str_int", tout << "get_value[" << mk_pp(it->get_owner(), m) << "] = " << mk_pp(_val, m) + TRACE("t_str_int", tout << mk_pp(it->get_owner(), m) << " is an integer ( ~= " << val << " )" << std::endl;); - return m_autil.is_numeral(_val, val) && val.is_int(); + return true; } else { - TRACE("t_str_int", tout << "get_value[" << mk_pp(it->get_owner(), m) << "] not found" << std::endl;); + TRACE("t_str_int", tout << mk_pp(it->get_owner(), m) << " not a numeral" << std::endl;); } it = it->get_next(); } while (it != en_e); From bc91d182bf97c64649d2b75d22b0163db9c29598 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 3 Aug 2016 13:39:14 -0400 Subject: [PATCH 168/562] mk_concat fixes WIP --- src/smt/theory_str.cpp | 79 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 7 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 2ae5dcec5..6ae9fbf66 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -661,21 +661,86 @@ expr * theory_str::mk_concat_const_str(expr * n1, expr * n2) { expr * theory_str::mk_concat(expr * n1, expr * n2) { ast_manager & m = get_manager(); - if (n1 == NULL || n2 == NULL) { - m.raise_exception("strings to be concatenated cannot be NULL"); - } + ENSURE(n1 != NULL); + ENSURE(n2 != NULL); bool n1HasEqcValue = false; bool n2HasEqcValue = false; n1 = get_eqc_value(n1, n1HasEqcValue); n2 = get_eqc_value(n2, n2HasEqcValue); if (n1HasEqcValue && n2HasEqcValue) { return mk_concat_const_str(n1, n2); + } else if (n1HasEqcValue && !n2HasEqcValue) { + bool n2_isConcatFunc = is_concat(to_app(n2)); + if (m_strutil.get_string_constant_value(n1) == "") { + return n2; + } + if (n2_isConcatFunc) { + expr * n2_arg0 = to_app(n2)->get_arg(0); + expr * n2_arg1 = to_app(n2)->get_arg(1); + if (m_strutil.is_string(n2_arg0)) { + n1 = mk_concat_const_str(n1, n2_arg0); // n1 will be a constant + n2 = n2_arg1; + } + } + } else if (!n1HasEqcValue && n2HasEqcValue) { + if (m_strutil.get_string_constant_value(n2) == "") { + return n1; + } + + if (is_concat(to_app(n1))) { + expr * n1_arg0 = to_app(n1)->get_arg(0); + expr * n1_arg1 = to_app(n1)->get_arg(1); + if (m_strutil.is_string(n1_arg1)) { + n1 = n1_arg0; + n2 = mk_concat_const_str(n1_arg1, n2); // n2 will be a constant + } + } } else { - // TODO there's a *TON* of missing code here from strTheory::mk_concat() - // if all else fails, just build the application AST - expr * args[2] = {n1, n2}; - return get_manager().mk_app(get_id(), OP_STRCAT, 0, 0, 2, args); + if (is_concat(to_app(n1)) && is_concat(to_app(n2))) { + expr * n1_arg0 = to_app(n1)->get_arg(0); + expr * n1_arg1 = to_app(n1)->get_arg(1); + expr * n2_arg0 = to_app(n2)->get_arg(0); + expr * n2_arg1 = to_app(n2)->get_arg(1); + if (m_strutil.is_string(n1_arg1) && m_strutil.is_string(n2_arg0)) { + expr * tmpN1 = n1_arg0; + expr * tmpN2 = mk_concat_const_str(n1_arg1, n2_arg0); + n1 = mk_concat(tmpN1, tmpN2); + n2 = n2_arg1; + } + } } + + //------------------------------------------------------ + // * expr * ast1 = mk_2_arg_app(ctx, td->Concat, n1, n2); + // * expr * ast2 = mk_2_arg_app(ctx, td->Concat, n1, n2); + // Z3 treats (ast1) and (ast2) as two different nodes. + //------------------------------------------------------- + std::pair concatArgs(n1, n2); + expr * concatAst = NULL; + // TODO NEXT clarify semantics of this, I think we can get around this check. + NOT_IMPLEMENTED_YET(); + /* + if (concat_astNode_map.find(concatArgs) == concat_astNode_map.end()) { + concatAst = mk_2_arg_app(ctx, td->Concat, n1, n2); + concat_astNode_map[concatArgs] = concatAst; + + Z3_ast concat_length = mk_length(t, concatAst); + + std::vector childrenVector; + getNodesInConcat(t, concatAst, childrenVector); + Z3_ast * items = new Z3_ast[childrenVector.size()]; + for (unsigned int i = 0; i < childrenVector.size(); i++) { + items[i] = mk_length(t, childrenVector[i]); + } + Z3_ast lenAssert = Z3_mk_eq(ctx, concat_length, Z3_mk_add(ctx, childrenVector.size(), items)); + addAxiom(t, lenAssert, __LINE__, false); + delete[] items; + + } else { + concatAst = concat_astNode_map[concatArgs]; + } + */ + return concatAst; } bool theory_str::can_propagate() { From 0c4e7259025207a9e88f221a70147513a371ea7f Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 4 Aug 2016 16:40:05 -0400 Subject: [PATCH 169/562] finish theory_str::mk_concat, no caching of generated terms yet --- src/smt/theory_str.cpp | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 6ae9fbf66..78ab30dd7 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -660,6 +660,7 @@ expr * theory_str::mk_concat_const_str(expr * n1, expr * n2) { } expr * theory_str::mk_concat(expr * n1, expr * n2) { + context & ctx = get_context(); ast_manager & m = get_manager(); ENSURE(n1 != NULL); ENSURE(n2 != NULL); @@ -717,29 +718,29 @@ expr * theory_str::mk_concat(expr * n1, expr * n2) { //------------------------------------------------------- std::pair concatArgs(n1, n2); expr * concatAst = NULL; - // TODO NEXT clarify semantics of this, I think we can get around this check. - NOT_IMPLEMENTED_YET(); + // TODO NEXT add cache lookups. I think we need to be more careful than just using std:: data structures here /* if (concat_astNode_map.find(concatArgs) == concat_astNode_map.end()) { - concatAst = mk_2_arg_app(ctx, td->Concat, n1, n2); - concat_astNode_map[concatArgs] = concatAst; - - Z3_ast concat_length = mk_length(t, concatAst); - - std::vector childrenVector; - getNodesInConcat(t, concatAst, childrenVector); - Z3_ast * items = new Z3_ast[childrenVector.size()]; - for (unsigned int i = 0; i < childrenVector.size(); i++) { - items[i] = mk_length(t, childrenVector[i]); - } - Z3_ast lenAssert = Z3_mk_eq(ctx, concat_length, Z3_mk_add(ctx, childrenVector.size(), items)); - addAxiom(t, lenAssert, __LINE__, false); - delete[] items; - - } else { - concatAst = concat_astNode_map[concatArgs]; - } */ + if (true) { + expr * args[2] = {n1, n2}; + concatAst = m.mk_app(get_id(), OP_STRCAT, 0, 0, 2, args); + // concat_astNode_map[concatArgs] = concatAst; + + expr_ref concat_length(mk_strlen(concatAst), m); + + ptr_vector childrenVector; + get_nodes_in_concat(concatAst, childrenVector); + expr_ref_vector items(m); + for (unsigned int i = 0; i < childrenVector.size(); i++) { + items.push_back(mk_strlen(childrenVector.get(i))); + } + expr_ref lenAssert(ctx.mk_eq_atom(concat_length, m_autil.mk_add(items.size(), items.c_ptr())), m); + assert_axiom(lenAssert); + } else { + // concatAst = concat_astNode_map[concatArgs]; + NOT_IMPLEMENTED_YET(); + } return concatAst; } From 91c336d7eeea124836c908195b91d634f23cb476 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 6 Aug 2016 15:32:37 -0400 Subject: [PATCH 170/562] fix erroneous vector double-insert in theory_str::group_terms_by_eqc() --- src/smt/theory_str.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 78ab30dd7..296041a39 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1588,7 +1588,6 @@ void theory_str::group_terms_by_eqc(expr * n, std::set & concats, std::se } else { concats.insert(simConcat); } - concats.insert(ast); } else if (is_string(eqcNode)) { consts.insert(ast); } else { From 2c91f388dfa02fa74c933af9d7249a61a276942e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 6 Aug 2016 15:35:47 -0400 Subject: [PATCH 171/562] add defensive double-non-concat check in theory_str::simplify_concat_equality() --- src/smt/theory_str.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 296041a39..ac897ee7d 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2317,10 +2317,13 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { simplify_parent(new_nn1, new_nn2); } return; + } else if (!n1IsConcat && !n2IsConcat) { + // normally this should never happen, because group_terms_by_eqc() should have pre-simplified + // as much as possible. however, we make a defensive check here just in case + TRACE("t_str_detail", tout << "WARNING: nn1_new and nn2_new both simplify to non-concat terms" << std::endl;); + return; } - // TODO what happens if BOTH of these are simplified into non-concat terms? - expr * v1_arg0 = a_new_nn1->get_arg(0); expr * v1_arg1 = a_new_nn1->get_arg(1); expr * v2_arg0 = a_new_nn2->get_arg(0); From 43b0cd5010fe4ebb9060142d9dad127b630f4d76 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 6 Aug 2016 15:38:58 -0400 Subject: [PATCH 172/562] clean up unused variables in theory_str.cpp --- src/smt/theory_str.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ac897ee7d..7b96a57f7 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -422,7 +422,6 @@ expr * theory_str::mk_internal_valTest_var(expr * node, int len, int vTries) { } void theory_str::track_variable_scope(expr * var) { - context & ctx = get_context(); if (internal_variable_scope_levels.find(sLevel) == internal_variable_scope_levels.end()) { internal_variable_scope_levels[sLevel] = std::set(); } @@ -431,7 +430,6 @@ void theory_str::track_variable_scope(expr * var) { app * theory_str::mk_internal_xor_var() { ast_manager & m = get_manager(); - context & ctx = get_context(); std::stringstream ss; ss << tmpXorVarCount; tmpXorVarCount++; @@ -3730,7 +3728,6 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { } void theory_str::process_unroll_eq_const_str(expr * unrollFunc, expr * constStr) { - context & ctx = get_context(); ast_manager & m = get_manager(); if (!is_Unroll(to_app(unrollFunc))) { @@ -4461,7 +4458,6 @@ bool theory_str::check_length_consistency(expr * n1, expr * n2) { void theory_str::check_concat_len_in_eqc(expr * concat) { context & ctx = get_context(); - ast_manager & m = get_manager(); enode * eqc_base = ctx.get_enode(concat); enode * eqc_it = eqc_base; @@ -5271,14 +5267,12 @@ void theory_str::assign_eh(bool_var v, bool is_true) { void theory_str::push_scope_eh() { theory::push_scope_eh(); - context & ctx = get_context(); sLevel += 1; TRACE("t_str", tout << "push to " << sLevel << std::endl;); TRACE("t_str_dump_assign_on_scope_change", dump_assignments();); } void theory_str::pop_scope_eh(unsigned num_scopes) { - context & ctx = get_context(); sLevel -= num_scopes; TRACE("t_str", tout << "pop " << num_scopes << " to " << sLevel << std::endl;); @@ -6664,7 +6658,6 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, std::string len_valueStr, expr * valTesterInCbEq, std::string valTesterValueStr) { - context & ctx = get_context(); ast_manager & m = get_manager(); int len = atoi(len_valueStr.c_str()); From 395ec4543c1332434733f78ff18f5d162e537120 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 6 Aug 2016 22:19:10 -0400 Subject: [PATCH 173/562] avoid crash in theory_str, this leaks memory --- src/smt/theory_str.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 7b96a57f7..2e95020a7 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -315,12 +315,14 @@ void theory_str::add_cut_info_one_node(expr * baseNode, int slevel, expr * node) varInfo->level = slevel; varInfo->vars[node] = 1; cut_var_map[baseNode].push(varInfo); + TRACE("t_str_cut_var_map", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << std::endl;); } else { if (cut_var_map[baseNode].empty()) { T_cut * varInfo = alloc(T_cut); varInfo->level = slevel; varInfo->vars[node] = 1; cut_var_map[baseNode].push(varInfo); + TRACE("t_str_cut_var_map", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << std::endl;); } else { if (cut_var_map[baseNode].top()->level < slevel) { T_cut * varInfo = alloc(T_cut); @@ -328,8 +330,10 @@ void theory_str::add_cut_info_one_node(expr * baseNode, int slevel, expr * node) cut_vars_map_copy(varInfo->vars, cut_var_map[baseNode].top()->vars); varInfo->vars[node] = 1; cut_var_map[baseNode].push(varInfo); + TRACE("t_str_cut_var_map", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << std::endl;); } else if (cut_var_map[baseNode].top()->level == slevel) { cut_var_map[baseNode].top()->vars[node] = 1; + TRACE("t_str_cut_var_map", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << std::endl;); } else { get_manager().raise_exception("entered illegal state during add_cut_info_one_node()"); } @@ -354,6 +358,7 @@ void theory_str::add_cut_info_merge(expr * destNode, int slevel, expr * srcNode) varInfo->level = slevel; cut_vars_map_copy(varInfo->vars, cut_var_map[srcNode].top()->vars); cut_var_map[destNode].push(varInfo); + TRACE("t_str_cut_var_map", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << std::endl;); } else { if (cut_var_map[destNode].empty() || cut_var_map[destNode].top()->level < slevel) { T_cut * varInfo = alloc(T_cut); @@ -361,8 +366,10 @@ void theory_str::add_cut_info_merge(expr * destNode, int slevel, expr * srcNode) cut_vars_map_copy(varInfo->vars, cut_var_map[destNode].top()->vars); cut_vars_map_copy(varInfo->vars, cut_var_map[srcNode].top()->vars); cut_var_map[destNode].push(varInfo); + TRACE("t_str_cut_var_map", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << std::endl;); } else if (cut_var_map[destNode].top()->level == slevel) { cut_vars_map_copy(cut_var_map[destNode].top()->vars, cut_var_map[srcNode].top()->vars); + TRACE("t_str_cut_var_map", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << std::endl;); } else { get_manager().raise_exception("illegal state in add_cut_info_merge(): inconsistent slevels"); } @@ -5281,12 +5288,13 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { while ((varItor->second.size() > 0) && (varItor->second.top()->level != 0) && (varItor->second.top()->level >= sLevel)) { T_cut * aCut = varItor->second.top(); varItor->second.pop(); - dealloc(aCut); + // dealloc(aCut); // TODO find a safer way to do this, it is causing a crash } if (varItor->second.size() == 0) { - cut_var_map.erase(varItor); + cut_var_map.erase(varItor++); + } else { + varItor++; } - ++varItor; } // see if any internal variables went out of scope From cb566ad5ced242cad991c8381322522bcddc98eb Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 7 Aug 2016 15:43:08 -0400 Subject: [PATCH 174/562] fix model validation for theory_str --- src/ast/rewriter/str_rewriter.cpp | 17 +++++++++++++++++ src/ast/rewriter/str_rewriter.h | 1 + src/model/model_evaluator.cpp | 7 +++++++ 3 files changed, 25 insertions(+) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index 1449afcc3..37b6b6cbf 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -23,6 +23,20 @@ Notes: #include"ast_util.h" #include"well_sorted.h" +br_status str_rewriter::mk_str_Concat(expr * arg0, expr * arg1, expr_ref & result) { + TRACE("t_str_rw", tout << "rewrite (Concat " << mk_pp(arg0, m()) << " " << mk_pp(arg1, m()) << ")" << std::endl;); + if(m_strutil.is_string(arg0) && m_strutil.is_string(arg1)) { + TRACE("t_str_rw", tout << "evaluating Concat of two constant strings" << std::endl;); + std::string arg0Str = m_strutil.get_string_constant_value(arg0); + std::string arg1Str = m_strutil.get_string_constant_value(arg1); + std::string resultStr = arg0Str + arg1Str; + result = m_strutil.mk_string(resultStr); + return BR_DONE; + } else { + return BR_FAILED; + } +} + br_status str_rewriter::mk_str_CharAt(expr * arg0, expr * arg1, expr_ref & result) { TRACE("t_str_rw", tout << "rewrite (CharAt " << mk_pp(arg0, m()) << " " << mk_pp(arg1, m()) << ")" << std::endl;); // if arg0 is a string constant and arg1 is an integer constant, @@ -275,6 +289,9 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con // TODO more rewrites for really easy cases, e.g. (Concat "abc" "def")... switch(f->get_decl_kind()) { + case OP_STRCAT: + SASSERT(num_args == 2); + return mk_str_Concat(args[0], args[1], result); case OP_STR_CHARAT: SASSERT(num_args == 2); return mk_str_CharAt(args[0], args[1], result); diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index dccf4a6bd..58e88591b 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -40,6 +40,7 @@ public: br_status mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result); br_status mk_eq_core(expr * lhs, expr * rhs, expr_ref & result); + br_status mk_str_Concat(expr * arg0, expr * arg1, expr_ref & result); br_status mk_str_CharAt(expr * arg0, expr * arg1, expr_ref & result); br_status mk_str_StartsWith(expr * haystack, expr * needle, expr_ref & result); br_status mk_str_EndsWith(expr * haystack, expr * needle, expr_ref & result); diff --git a/src/model/model_evaluator.cpp b/src/model/model_evaluator.cpp index eb2259263..fd420bad6 100644 --- a/src/model/model_evaluator.cpp +++ b/src/model/model_evaluator.cpp @@ -28,6 +28,7 @@ Revision History: #include"datatype_rewriter.h" #include"array_rewriter.h" #include"fpa_rewriter.h" +#include"str_rewriter.h" #include"rewriter_def.h" #include"cooperate.h" #include"ast_pp.h" @@ -44,6 +45,7 @@ struct evaluator_cfg : public default_rewriter_cfg { pb_rewriter m_pb_rw; fpa_rewriter m_f_rw; seq_rewriter m_seq_rw; + str_rewriter m_str_rw; array_util m_ar; unsigned long long m_max_memory; unsigned m_max_steps; @@ -63,6 +65,7 @@ struct evaluator_cfg : public default_rewriter_cfg { m_pb_rw(m), m_f_rw(m), m_seq_rw(m), + m_str_rw(m), m_ar(m) { bool flat = true; m_b_rw.set_flat(flat); @@ -152,6 +155,8 @@ struct evaluator_cfg : public default_rewriter_cfg { st = m_f_rw.mk_eq_core(args[0], args[1], result); else if (s_fid == m_seq_rw.get_fid()) st = m_seq_rw.mk_eq_core(args[0], args[1], result); + else if (s_fid == m_str_rw.get_fid()) + st = m_str_rw.mk_eq_core(args[0], args[1], result); else if (s_fid == m_ar_rw.get_fid()) st = mk_array_eq(args[0], args[1], result); if (st != BR_FAILED) @@ -174,6 +179,8 @@ struct evaluator_cfg : public default_rewriter_cfg { st = m_f_rw.mk_app_core(f, num, args, result); else if (fid == m_seq_rw.get_fid()) st = m_seq_rw.mk_app_core(f, num, args, result); + else if (fid == m_str_rw.get_fid()) + st = m_str_rw.mk_app_core(f, num, args, result); else if (fid == m().get_label_family_id() && num == 1) { result = args[0]; st = BR_DONE; From 3dff240bb3c552a869a9ce3d7bbdf96a7db738de Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 7 Aug 2016 15:50:41 -0400 Subject: [PATCH 175/562] theory_str model validation for Length --- src/ast/rewriter/str_rewriter.cpp | 20 ++++++++++++++++++-- src/ast/rewriter/str_rewriter.h | 1 + 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index 37b6b6cbf..fdb67f89e 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -26,7 +26,7 @@ Notes: br_status str_rewriter::mk_str_Concat(expr * arg0, expr * arg1, expr_ref & result) { TRACE("t_str_rw", tout << "rewrite (Concat " << mk_pp(arg0, m()) << " " << mk_pp(arg1, m()) << ")" << std::endl;); if(m_strutil.is_string(arg0) && m_strutil.is_string(arg1)) { - TRACE("t_str_rw", tout << "evaluating Concat of two constant strings" << std::endl;); + TRACE("t_str_rw", tout << "evaluating concat of two constant strings" << std::endl;); std::string arg0Str = m_strutil.get_string_constant_value(arg0); std::string arg1Str = m_strutil.get_string_constant_value(arg1); std::string resultStr = arg0Str + arg1Str; @@ -37,6 +37,20 @@ br_status str_rewriter::mk_str_Concat(expr * arg0, expr * arg1, expr_ref & resul } } +br_status str_rewriter::mk_str_Length(expr * arg0, expr_ref & result) { + TRACE("t_str_rw", tout << "rewrite (Length " << mk_pp(arg0, m()) << ")" << std::endl;); + if (m_strutil.is_string(arg0)) { + TRACE("t_str_rw", tout << "evaluating length of constant string" << std::endl;); + std::string arg0Str = m_strutil.get_string_constant_value(arg0); + rational arg0Len((unsigned)arg0Str.length()); + result = m_autil.mk_numeral(arg0Len, true); + TRACE("t_str_rw", tout << "result is " << mk_pp(result, m()) << std::endl;); + return BR_DONE; + } else { + return BR_FAILED; + } +} + br_status str_rewriter::mk_str_CharAt(expr * arg0, expr * arg1, expr_ref & result) { TRACE("t_str_rw", tout << "rewrite (CharAt " << mk_pp(arg0, m()) << " " << mk_pp(arg1, m()) << ")" << std::endl;); // if arg0 is a string constant and arg1 is an integer constant, @@ -287,11 +301,13 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con TRACE("t_str_rw", tout << "rewrite app: " << f->get_name() << std::endl;); - // TODO more rewrites for really easy cases, e.g. (Concat "abc" "def")... switch(f->get_decl_kind()) { case OP_STRCAT: SASSERT(num_args == 2); return mk_str_Concat(args[0], args[1], result); + case OP_STRLEN: + SASSERT(num_args == 1); + return mk_str_Length(args[0], result); case OP_STR_CHARAT: SASSERT(num_args == 2); return mk_str_CharAt(args[0], args[1], result); diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index 58e88591b..2235425be 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -41,6 +41,7 @@ public: br_status mk_eq_core(expr * lhs, expr * rhs, expr_ref & result); br_status mk_str_Concat(expr * arg0, expr * arg1, expr_ref & result); + br_status mk_str_Length(expr * arg0, expr_ref & result); br_status mk_str_CharAt(expr * arg0, expr * arg1, expr_ref & result); br_status mk_str_StartsWith(expr * haystack, expr * needle, expr_ref & result); br_status mk_str_EndsWith(expr * haystack, expr * needle, expr_ref & result); From f7ba3ff0843c8ebd9df440678266c7614abac344 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 9 Aug 2016 20:11:25 -0400 Subject: [PATCH 176/562] crash avoidance in theory_str search start, fixes length-001.smt2 regression --- src/smt/theory_str.cpp | 19 ++++++++++++++++--- src/smt/theory_str.h | 3 +++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 2e95020a7..fd0b15b19 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -42,6 +42,7 @@ theory_str::theory_str(ast_manager & m): sLevel(0), finalCheckProgressIndicator(false), m_trail(m), + m_delayed_axiom_setup_terms(m), tmpStringVarCount(0), tmpXorVarCount(0), tmpLenTestVarCount(0), @@ -755,10 +756,12 @@ bool theory_str::can_propagate() { || !m_axiom_Contains_todo.empty() || !m_axiom_Indexof_todo.empty() || !m_axiom_Indexof2_todo.empty() || !m_axiom_LastIndexof_todo.empty() || !m_axiom_Substr_todo.empty() || !m_axiom_Replace_todo.empty() || !m_axiom_RegexIn_todo.empty() + || !m_delayed_axiom_setup_terms.empty(); ; } void theory_str::propagate() { + context & ctx = get_context(); while (can_propagate()) { TRACE("t_str_detail", tout << "propagating..." << std::endl;); for (unsigned i = 0; i < m_basicstr_axiom_todo.size(); ++i) { @@ -829,6 +832,13 @@ void theory_str::propagate() { instantiate_axiom_RegexIn(m_axiom_RegexIn_todo[i]); } m_axiom_RegexIn_todo.reset(); + + for (unsigned i = 0; i < m_delayed_axiom_setup_terms.size(); ++i) { + // I think this is okay + ctx.internalize(m_delayed_axiom_setup_terms[i].get(), false); + set_up_axioms(m_delayed_axiom_setup_terms[i].get()); + } + m_delayed_axiom_setup_terms.reset(); } } @@ -5140,6 +5150,7 @@ void theory_str::set_up_axioms(expr * ex) { ": expr is of sort Bool" << std::endl;); // set up axioms for boolean terms + ensure_enode(ex); if (ctx.e_internalized(ex)) { enode * n = ctx.get_enode(ex); SASSERT(n); @@ -5157,14 +5168,16 @@ void theory_str::set_up_axioms(expr * ex) { } } } else { - TRACE("t_str_detail", tout << "WARNING: Bool term " << mk_ismt2_pp(ex, get_manager()) << " not internalized. Skipping to prevent a crash." << std::endl;); + TRACE("t_str_detail", tout << "WARNING: Bool term " << mk_ismt2_pp(ex, get_manager()) << " not internalized. Delaying axiom setup to prevent a crash." << std::endl;); + ENSURE(!search_started); // infinite loop prevention + m_delayed_axiom_setup_terms.push_back(ex); return; } } else if (ex_sort == int_sort) { TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << ": expr is of sort Int" << std::endl;); - // set up axioms for boolean terms - enode * n = ctx.get_enode(ex); + // set up axioms for integer terms + enode * n = ensure_enode(ex); SASSERT(n); if (is_app(ex)) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 74c1786df..7b4ff8ce0 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -129,6 +129,9 @@ namespace smt { str_value_factory * m_factory; + // terms we couldn't go through set_up_axioms() with because they weren't internalized + expr_ref_vector m_delayed_axiom_setup_terms; + ptr_vector m_basicstr_axiom_todo; svector > m_str_eq_todo; ptr_vector m_concat_axiom_todo; From 66129710492304874508f7d4c76f236604da6e00 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 14 Aug 2016 14:15:29 -0400 Subject: [PATCH 177/562] start adding Contains checks to theory_str --- src/smt/theory_str.cpp | 116 ++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 7 +++ 2 files changed, 121 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index fd0b15b19..aed46e868 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1124,12 +1124,13 @@ void theory_str::instantiate_axiom_Contains(enode * e) { return; } axiomatized_terms.insert(expr); + contains_map.push_back(expr); // replaces registerContain() in Z3str2 TRACE("t_str_detail", tout << "instantiate Contains axiom for " << mk_pp(expr, m) << std::endl;); expr_ref ts0(mk_str_var("ts0"), m); expr_ref ts1(mk_str_var("ts1"), m); - // TODO NEXT registerContain(expr); + expr_ref breakdownAssert(ctx.mk_eq_atom(expr, ctx.mk_eq_atom(expr->get_arg(0), mk_concat(ts0, mk_concat(expr->get_arg(1), ts1)))), m); SASSERT(breakdownAssert); assert_axiom(breakdownAssert); @@ -1575,7 +1576,11 @@ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { eqc_iterator1 = eqc_iterator1->get_next(); } while (eqc_iterator1 != eqc_root); - // TODO containPairBoolMap + + if (!contains_map.empty()) { + check_contain_in_new_eq(lhs, rhs); + } + // TODO regexInBoolMap // okay, all checks here passed @@ -4118,6 +4123,113 @@ bool theory_str::in_same_eqc(expr * n1, expr * n2) { return n1Node->get_root() == n2Node->get_root(); } +expr * theory_str::collect_eq_nodes(expr * n, expr_ref_vector & eqcSet) { + context & ctx = get_context(); + expr * constStrNode = NULL; + + enode * e_base = ctx.get_enode(n); + enode * e_curr = e_base; + do { + app * ex = e_curr->get_owner(); + if (m_strutil.is_string(ex)) { + constStrNode = ex; + } + eqcSet.push_back(ex); + + e_curr = e_curr->get_next(); + } while (e_curr != e_base); + return constStrNode; +} + +void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { + NOT_IMPLEMENTED_YET(); // TODO NEXT +} + +void theory_str::check_contain_by_substr(expr * varNode, expr_ref_vector & willEqClass) { + NOT_IMPLEMENTED_YET(); // TODO NEXT +} + +void theory_str::check_contain_by_eq_nodes(expr * n1, expr * n2) { + NOT_IMPLEMENTED_YET(); // TODO NEXT +} + +void theory_str::check_contain_in_new_eq(expr * n1, expr * n2) { + if (contains_map.empty()) { + return; + } + + context & ctx = get_context(); + ast_manager & m = get_manager(); + TRACE("t_str_detail", tout << "consistency check for contains wrt. " << mk_pp(n1, m) << " and " << mk_pp(n2, m) << std::endl;); + + // Modification from Z3str2: the EQC of n1 and n2 *are* now merged. + // So we don't have to do anything too special + // to prepare willEqClass any more, we just use the EQC from n1 / n2. + expr_ref_vector willEqClass(m); + expr * constStrAst = collect_eq_nodes(n1, willEqClass); + + TRACE("t_str_detail", tout << "eqc of n1 is {"; + for (ptr_vector::iterator it = willEqClass.begin(); it != willEqClass.end(); ++it) { + expr * el = *it; + tout << " " << mk_pp(el, m); + } + tout << std::endl; + if (constStrAst == NULL) { + tout << "constStrAst = NULL" << std::endl; + } else { + tout << "constStrAst = " << mk_pp(constStrAst, m) << std::endl; + } + ); + + // step 1: we may have constant values for Contains checks now + if (constStrAst != NULL) { + ptr_vector::iterator itAst = willEqClass.begin(); + for (; itAst != willEqClass.end(); itAst++) { + if (*itAst == constStrAst) { + continue; + } + check_contain_by_eqc_val(*itAst, constStrAst); + } + } else { + // no concrete value to be put in eqc, solely based on context + // Check here is used to detected the facts as follows: + // * known: contains(Z, Y) /\ Z = "abcdefg" /\ Y = M + // * new fact: M = concat(..., "jio", ...) + // Note that in this branch, either M or concat(..., "jio", ...) has a constant value + // So, only need to check + // * "EQC(M) U EQC(concat(..., "jio", ...))" as substr and + // * If strAst registered has an eqc constant in the context + // ------------------------------------------------------------- + ptr_vector::iterator itAst = willEqClass.begin(); + for (; itAst != willEqClass.end(); ++itAst) { + check_contain_by_substr(*itAst, willEqClass); + } + } + + // ------------------------------------------ + // step 2: check for b1 = contains(x, m), b2 = contains(y, n) + // (1) x = y /\ m = n ==> b1 = b2 + // (2) x = y /\ Contains(const(m), const(n)) ==> (b1 -> b2) + // (3) x = y /\ Contains(const(n), const(m)) ==> (b2 -> b1) + // (4) x = y /\ containPairBoolMap[] ==> (b1 -> b2) + // (5) x = y /\ containPairBoolMap[] ==> (b2 -> b1) + // (6) Contains(const(x), const(y)) /\ m = n ==> (b2 -> b1) + // (7) Contains(const(y), const(x)) /\ m = n ==> (b1 -> b2) + // (8) containPairBoolMap[] /\ m = n ==> (b2 -> b1) + // (9) containPairBoolMap[] /\ m = n ==> (b1 -> b2) + // ------------------------------------------ + + expr_ref_vector::iterator varItor1 = willEqClass.begin(); + for (; varItor1 != willEqClass.end(); ++varItor1) { + expr * varAst1 = *varItor1; + expr_ref_vector::iterator varItor2 = varItor1; + for (; varItor2 != willEqClass.end(); ++varItor2) { + expr * varAst2 = *varItor2; + check_contain_by_eq_nodes(varAst1, varAst2); + } + } +} + bool theory_str::can_concat_eq_str(expr * concat, std::string str) { // TODO this method could use some traces and debugging info int strLen = str.length(); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 7b4ff8ce0..61eefece8 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -191,6 +191,8 @@ namespace smt { std::map unroll_var_map; std::map, expr*> concat_eq_unroll_ast_map; + expr_ref_vector contains_map; // was containPairBoolMap in Z3str2 + char * char_set; std::map charSetLookupTable; int charSetSize; @@ -290,6 +292,7 @@ namespace smt { app * mk_value_helper(app * n); expr * get_eqc_value(expr * n, bool & hasEqcValue); bool in_same_eqc(expr * n1, expr * n2); + expr * collect_eq_nodes(expr * n, expr_ref_vector & eqcSet); bool get_value(expr* e, rational& val) const; bool get_len_value(expr* e, rational& val); @@ -306,6 +309,10 @@ namespace smt { bool check_length_concat_concat(expr * n1, expr * n2); bool check_length_concat_var(expr * concat, expr * var); bool check_length_var_var(expr * var1, expr * var2); + void check_contain_in_new_eq(expr * n1, expr * n2); + void check_contain_by_eqc_val(expr * varNode, expr * constNode); + void check_contain_by_substr(expr * varNode, expr_ref_vector & willEqClass); + void check_contain_by_eq_nodes(expr * n1, expr * n2); void get_nodes_in_concat(expr * node, ptr_vector & nodeList); expr * simplify_concat(expr * node); From 1f594b190a8a00a167dcb234c43caf3d684d9d1c Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 14 Aug 2016 14:55:29 -0400 Subject: [PATCH 178/562] add theory_str::check_contain_by_eqc_val --- src/smt/theory_str.cpp | 169 ++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 2 + 2 files changed, 170 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index aed46e868..18157e4be 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4141,8 +4141,161 @@ expr * theory_str::collect_eq_nodes(expr * n, expr_ref_vector & eqcSet) { return constStrNode; } +/* + * Collect constant strings (from left to right) in an AST node. + */ +void theory_str::get_const_str_asts_in_node(expr * node, expr_ref_vector & astList) { + ast_manager & m = get_manager(); + if (m_strutil.is_string(node)) { + astList.push_back(node); + //} else if (getNodeType(t, node) == my_Z3_Func) { + } else if (is_app(node)) { + app * func_app = to_app(node); + unsigned int argCount = func_app->get_num_args(); + for (unsigned int i = 0; i < argCount; i++) { + expr * argAst = func_app->get_arg(i); + get_const_str_asts_in_node(argAst, astList); + } + } +} + void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { - NOT_IMPLEMENTED_YET(); // TODO NEXT + context & ctx = get_context(); + ast_manager & m = get_manager(); + + TRACE("t_str_detail", tout << "varNode = " << mk_pp(varNode, m) << ", constNode = " << mk_pp(constNode, m) << std::endl;); + + expr_ref_vector litems(m); + + // Modification from Z3str: + // since we don't track containPairIdxMap any more, + // we check each element of contains_map to see whether + // either of its arguments are equal to varNode. + // This could possibly be made faster if we had a map class that + // let us use an expr_ref as a key. + + expr_ref_vector::iterator itor1 = contains_map.begin(); + for (; itor1 != contains_map.end(); ++itor1) { + expr * boolVar = *itor1; + // boolVar is actually a Contains term + app * containsApp = to_app(boolVar); + expr * strAst = containsApp->get_arg(0); + expr * substrAst = containsApp->get_arg(1); + + // we only want to inspect the Contains terms where either of strAst or substrAst + // are equal to varNode. + + TRACE("t_str_detail", tout << "considering Contains with strAst = " << mk_pp(strAst, m) << ", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); + + if (varNode != strAst && varNode != substrAst) { + TRACE("t_str_detail", tout << "varNode not equal to strAst or substrAst, skip" << std::endl;); + continue; + } + TRACE("t_str_detail", tout << "varNode matched one of strAst or substrAst. Continuing" << std::endl;); + + // varEqcNode is str + if (strAst == varNode) { + expr_ref implyR(m); + litems.reset(); + + if (strAst != constNode) { + litems.push_back(ctx.mk_eq_atom(strAst, constNode)); + } + std::string strConst = m_strutil.get_string_constant_value(constNode); + bool subStrHasEqcValue = false; + expr * substrValue = get_eqc_value(substrAst, subStrHasEqcValue); + if (substrValue != substrAst) { + litems.push_back(ctx.mk_eq_atom(substrAst, substrValue)); + } + + if (subStrHasEqcValue) { + // subStr has an eqc constant value + std::string subStrConst = m_strutil.get_string_constant_value(substrValue); + + TRACE("t_str_detail", tout << "strConst = " << strConst << ", subStrConst = " << subStrConst << std::endl;); + + if (strConst.find(subStrConst) != std::string::npos) { + //implyR = ctx.mk_eq(ctx, boolVar, Z3_mk_true(ctx)); + implyR = boolVar; + } else { + //implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_false(ctx)); + implyR = m.mk_not(boolVar); + } + } else { + // ------------------------------------------------------------------------------------------------ + // subStr doesn't have an eqc contant value + // however, subStr equals to some concat(arg_1, arg_2, ..., arg_n) + // if arg_j is a constant and is not a part of the strConst, it's sure that the contains is false + // ** This check is needed here because the "strConst" and "strAst" may not be in a same eqc yet + // ------------------------------------------------------------------------------------------------ + // collect eqc concat + std::set eqcConcats; + get_concats_in_eqc(substrAst, eqcConcats); + for (std::set::iterator concatItor = eqcConcats.begin(); + concatItor != eqcConcats.end(); concatItor++) { + expr_ref_vector constList(m); + bool counterEgFound = false; + // get constant strings in concat + expr * aConcat = *concatItor; + get_const_str_asts_in_node(aConcat, constList); + for (expr_ref_vector::iterator cstItor = constList.begin(); + cstItor != constList.end(); cstItor++) { + std::string pieceStr = m_strutil.get_string_constant_value(*cstItor); + if (strConst.find(pieceStr) == std::string::npos) { + counterEgFound = true; + if (aConcat != substrAst) { + litems.push_back(ctx.mk_eq_atom(substrAst, aConcat)); + } + //implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_false(ctx)); + implyR = m.mk_not(boolVar); + break; + } + } + if (counterEgFound) { + TRACE("t_str_detail", tout << "Inconsistency found!" << std::endl;); + break; + } + } + } + // add assertion + if (implyR != NULL) { + expr_ref implyLHS(mk_and(litems), m); + assert_implication(implyLHS, implyR); + } + } + // varEqcNode is subStr + else if (substrAst == varNode) { + expr_ref implyR(m); + litems.reset(); + + if (substrAst != constNode) { + litems.push_back(ctx.mk_eq_atom(substrAst, constNode)); + } + bool strHasEqcValue = false; + expr * strValue = get_eqc_value(strAst, strHasEqcValue); + if (strValue != strAst) { + litems.push_back(ctx.mk_eq_atom(strAst, strValue)); + } + + if (strHasEqcValue) { + std::string strConst = m_strutil.get_string_constant_value(strValue); + std::string subStrConst = m_strutil.get_string_constant_value(constNode); + if (strConst.find(subStrConst) != std::string::npos) { + //implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_true(ctx)); + implyR = boolVar; + } else { + // implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_false(ctx)); + implyR = m.mk_not(boolVar); + } + } + + // add assertion + if (implyR != NULL) { + expr_ref implyLHS(mk_and(litems), m); + assert_implication(implyLHS, implyR); + } + } + } // for (itor1 : contains_map) } void theory_str::check_contain_by_substr(expr * varNode, expr_ref_vector & willEqClass) { @@ -7485,6 +7638,20 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe } // fVarLenCountMap.find(...) } +void theory_str::get_concats_in_eqc(expr * n, std::set & concats) { + context & ctx = get_context(); + + expr * eqcNode = n; + do { + if (is_concat(to_app(eqcNode))) { + concats.insert(eqcNode); + } + enode * e_eqc = ctx.get_enode(eqcNode); + eqcNode = e_eqc->get_next()->get_owner(); + // eqcNode = Z3_theory_get_eqc_next(t, eqcNode); + } while (eqcNode != n); +} + void theory_str::get_var_in_eqc(expr * n, std::set & varSet) { context & ctx = get_context(); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 61eefece8..476519e5c 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -379,6 +379,8 @@ namespace smt { expr * getMostLeftNodeInConcat(expr * node); expr * getMostRightNodeInConcat(expr * node); void get_var_in_eqc(expr * n, std::set & varSet); + void get_concats_in_eqc(expr * n, std::set & concats); + void get_const_str_asts_in_node(expr * node, expr_ref_vector & constList); expr * eval_concat(expr * n1, expr * n2); // strRegex From ee6f1eef6919b72640f4f12a33795cc54b4346de Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 14 Aug 2016 15:14:48 -0400 Subject: [PATCH 179/562] add theory_str::check_contain_by_substr --- src/smt/theory_str.cpp | 67 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 18157e4be..2258646bb 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4299,7 +4299,72 @@ void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { } void theory_str::check_contain_by_substr(expr * varNode, expr_ref_vector & willEqClass) { - NOT_IMPLEMENTED_YET(); // TODO NEXT + context & ctx = get_context(); + ast_manager & m = get_manager(); + expr_ref_vector litems(m); + + // same deal as before, we do not track containPairIdxMap + // and so we check elements of contains_map instead + + expr_ref_vector::iterator itor1 = contains_map.begin(); + for (; itor1 != contains_map.end(); ++itor1) { + expr * boolVar = *itor1; + // boolVar is actually a Contains term + app * containsApp = to_app(boolVar); + expr * strAst = containsApp->get_arg(0); + expr * substrAst = containsApp->get_arg(1); + + // we only want to inspect the Contains terms where either of strAst or substrAst + // are equal to varNode. + + TRACE("t_str_detail", tout << "considering Contains with strAst = " << mk_pp(strAst, m) << ", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); + + if (varNode != strAst && varNode != substrAst) { + TRACE("t_str_detail", tout << "varNode not equal to strAst or substrAst, skip" << std::endl;); + continue; + } + TRACE("t_str_detail", tout << "varNode matched one of strAst or substrAst. Continuing" << std::endl;); + + if (substrAst == varNode) { + bool strAstHasVal = false; + expr * strValue = get_eqc_value(strAst, strAstHasVal); + if (strAstHasVal) { + TRACE("t_str_detail", tout << mk_pp(strAst, m) << " has constant eqc value " << mk_pp(strValue) << std::endl;); + if (strValue != strAst) { + litems.push_back(ctx.mk_eq_atom(strAst, strValue)); + } + std::string strConst = m_strutil.get_string_constant_value(strValue); + // iterate eqc (also eqc-to-be) of substr + for (expr_ref_vector::iterator itAst = willEqClass.begin(); itAst != willEqClass.end(); itAst++) { + bool counterEgFound = false; + if (is_concat(to_app(*itAst))) { + expr_ref_vector constList(m); + // get constant strings in concat + app * aConcat = to_app(*itAst); + get_const_str_asts_in_node(aConcat, constList); + for (expr_ref_vector::iterator cstItor = constList.begin(); + cstItor != constList.end(); cstItor++) { + std::string pieceStr = m_strutil.get_string_constant_value(*cstItor); + if (strConst.find(pieceStr) == std::string::npos) { + TRACE("t_str_detail", tout << "Inconsistency found!" << std::endl;); + counterEgFound = true; + if (aConcat != substrAst) { + litems.push_back(ctx.mk_eq_atom(substrAst, aConcat)); + } + expr_ref implyLHS(mk_and(litems), m); + expr_ref implyR(m.mk_not(boolVar), m); + assert_implication(implyLHS, implyR); + break; + } + } + } + if (counterEgFound) { + break; + } + } + } + } + } } void theory_str::check_contain_by_eq_nodes(expr * n1, expr * n2) { From f48377e78004f9c503b9bbba8e4dd588450ce107 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 14 Aug 2016 16:14:48 -0400 Subject: [PATCH 180/562] temporarily disable a third Contains check for testing purposes --- src/smt/theory_str.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 2258646bb..28b310196 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -48,7 +48,8 @@ theory_str::theory_str(ast_manager & m): tmpLenTestVarCount(0), tmpValTestVarCount(0), avoidLoopCut(true), - loopDetected(false) + loopDetected(false), + contains_map(m) { initialize_charset(); } @@ -4258,7 +4259,7 @@ void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { } } // add assertion - if (implyR != NULL) { + if (implyR) { expr_ref implyLHS(mk_and(litems), m); assert_implication(implyLHS, implyR); } @@ -4290,7 +4291,7 @@ void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { } // add assertion - if (implyR != NULL) { + if (implyR) { expr_ref implyLHS(mk_and(litems), m); assert_implication(implyLHS, implyR); } @@ -4329,7 +4330,7 @@ void theory_str::check_contain_by_substr(expr * varNode, expr_ref_vector & willE bool strAstHasVal = false; expr * strValue = get_eqc_value(strAst, strAstHasVal); if (strAstHasVal) { - TRACE("t_str_detail", tout << mk_pp(strAst, m) << " has constant eqc value " << mk_pp(strValue) << std::endl;); + TRACE("t_str_detail", tout << mk_pp(strAst, m) << " has constant eqc value " << mk_pp(strValue, m) << std::endl;); if (strValue != strAst) { litems.push_back(ctx.mk_eq_atom(strAst, strValue)); } @@ -4387,7 +4388,7 @@ void theory_str::check_contain_in_new_eq(expr * n1, expr * n2) { expr * constStrAst = collect_eq_nodes(n1, willEqClass); TRACE("t_str_detail", tout << "eqc of n1 is {"; - for (ptr_vector::iterator it = willEqClass.begin(); it != willEqClass.end(); ++it) { + for (expr_ref_vector::iterator it = willEqClass.begin(); it != willEqClass.end(); ++it) { expr * el = *it; tout << " " << mk_pp(el, m); } @@ -4401,7 +4402,7 @@ void theory_str::check_contain_in_new_eq(expr * n1, expr * n2) { // step 1: we may have constant values for Contains checks now if (constStrAst != NULL) { - ptr_vector::iterator itAst = willEqClass.begin(); + expr_ref_vector::iterator itAst = willEqClass.begin(); for (; itAst != willEqClass.end(); itAst++) { if (*itAst == constStrAst) { continue; @@ -4418,7 +4419,7 @@ void theory_str::check_contain_in_new_eq(expr * n1, expr * n2) { // * "EQC(M) U EQC(concat(..., "jio", ...))" as substr and // * If strAst registered has an eqc constant in the context // ------------------------------------------------------------- - ptr_vector::iterator itAst = willEqClass.begin(); + expr_ref_vector::iterator itAst = willEqClass.begin(); for (; itAst != willEqClass.end(); ++itAst) { check_contain_by_substr(*itAst, willEqClass); } @@ -4443,7 +4444,9 @@ void theory_str::check_contain_in_new_eq(expr * n1, expr * n2) { expr_ref_vector::iterator varItor2 = varItor1; for (; varItor2 != willEqClass.end(); ++varItor2) { expr * varAst2 = *varItor2; - check_contain_by_eq_nodes(varAst1, varAst2); + // for testing purposes + TRACE("t_str", tout << "WARNING: some Contains checks disabled!" << std::endl;); + // check_contain_by_eq_nodes(varAst1, varAst2); } } } From d28ef1d47185d6d67a95e1cb3b6251ed9e72c6da Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 15 Aug 2016 17:38:24 -0400 Subject: [PATCH 181/562] add theory_str::check_contain_by_eq_nodes --- src/smt/theory_str.cpp | 382 ++++++++++++++++++++++++++++++++++++++-- src/smt/theory_str.h | 27 ++- src/util/obj_pair_set.h | 5 + 3 files changed, 394 insertions(+), 20 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 28b310196..93173402c 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -152,6 +152,11 @@ void theory_str::assert_axiom(expr * e) { //TRACE("t_str_detail", tout << "done asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); } +expr * theory_str::rewrite_implication(expr * premise, expr * conclusion) { + ast_manager & m = get_manager(); + return m.mk_or(m.mk_not(premise), conclusion); +} + void theory_str::assert_implication(expr * premise, expr * conclusion) { ast_manager & m = get_manager(); TRACE("t_str_detail", tout << "asserting implication " << mk_ismt2_pp(premise, m) << " -> " << mk_ismt2_pp(conclusion, m) << std::endl;); @@ -1119,20 +1124,28 @@ void theory_str::instantiate_axiom_Contains(enode * e) { context & ctx = get_context(); ast_manager & m = get_manager(); - app * expr = e->get_owner(); - if (axiomatized_terms.contains(expr)) { - TRACE("t_str_detail", tout << "already set up Contains axiom for " << mk_pp(expr, m) << std::endl;); + app * ex = e->get_owner(); + if (axiomatized_terms.contains(ex)) { + TRACE("t_str_detail", tout << "already set up Contains axiom for " << mk_pp(ex, m) << std::endl;); return; } - axiomatized_terms.insert(expr); - contains_map.push_back(expr); // replaces registerContain() in Z3str2 + axiomatized_terms.insert(ex); + { // register Contains() + expr * str = ex->get_arg(0); + expr * substr = ex->get_arg(1); + contains_map.push_back(ex); + std::pair key = std::pair(str, substr); + contain_pair_bool_map.insert(str, substr, ex); + contain_pair_idx_map[str].insert(key); + contain_pair_idx_map[substr].insert(key); + } - TRACE("t_str_detail", tout << "instantiate Contains axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("t_str_detail", tout << "instantiate Contains axiom for " << mk_pp(ex, m) << std::endl;); expr_ref ts0(mk_str_var("ts0"), m); expr_ref ts1(mk_str_var("ts1"), m); - expr_ref breakdownAssert(ctx.mk_eq_atom(expr, ctx.mk_eq_atom(expr->get_arg(0), mk_concat(ts0, mk_concat(expr->get_arg(1), ts1)))), m); + expr_ref breakdownAssert(ctx.mk_eq_atom(ex, ctx.mk_eq_atom(ex->get_arg(0), mk_concat(ts0, mk_concat(ex->get_arg(1), ts1)))), m); SASSERT(breakdownAssert); assert_axiom(breakdownAssert); } @@ -4168,12 +4181,7 @@ void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { expr_ref_vector litems(m); - // Modification from Z3str: - // since we don't track containPairIdxMap any more, - // we check each element of contains_map to see whether - // either of its arguments are equal to varNode. - // This could possibly be made faster if we had a map class that - // let us use an expr_ref as a key. + // TODO refactor to use the new contain_pair_idx_map expr_ref_vector::iterator itor1 = contains_map.begin(); for (; itor1 != contains_map.end(); ++itor1) { @@ -4304,8 +4312,7 @@ void theory_str::check_contain_by_substr(expr * varNode, expr_ref_vector & willE ast_manager & m = get_manager(); expr_ref_vector litems(m); - // same deal as before, we do not track containPairIdxMap - // and so we check elements of contains_map instead + // TODO refactor to use the new contain_pair_idx_map expr_ref_vector::iterator itor1 = contains_map.begin(); for (; itor1 != contains_map.end(); ++itor1) { @@ -4368,8 +4375,347 @@ void theory_str::check_contain_by_substr(expr * varNode, expr_ref_vector & willE } } +bool theory_str::in_contain_idx_map(expr * n) { + return contain_pair_idx_map.contains(n); +} + void theory_str::check_contain_by_eq_nodes(expr * n1, expr * n2) { - NOT_IMPLEMENTED_YET(); // TODO NEXT + context & ctx = get_context(); + ast_manager & m = get_manager(); + + if (in_contain_idx_map(n1) && in_contain_idx_map(n2)) { + obj_pair_set::iterator keysItor1 = contain_pair_idx_map[n1].begin(); + obj_pair_set::iterator keysItor2; + + for (; keysItor1 != contain_pair_idx_map[n1].end(); keysItor1++) { + // keysItor1 is on set {<.., n1>, ..., , ...} + std::pair key1 = *keysItor1; + if (key1.first == n1 && key1.second == n2) { + expr_ref implyL(m); + expr_ref implyR(contain_pair_bool_map[key1], m); + if (n1 != n2) { + implyL = ctx.mk_eq_atom(n1, n2); + assert_implication(implyL, implyR); + } else { + assert_axiom(implyR); + } + } + + for (keysItor2 = contain_pair_idx_map[n2].begin(); + keysItor2 != contain_pair_idx_map[n2].end(); keysItor2++) { + // keysItor2 is on set {<.., n2>, ..., , ...} + std::pair key2 = *keysItor2; + // skip if the pair is eq + if (key1 == key2) { + continue; + } + + // *************************** + // Case 1: Contains(m, ...) /\ Contains(n, ) /\ m = n + // *************************** + if (key1.first == n1 && key2.first == n2) { + expr * subAst1 = key1.second; + expr * subAst2 = key2.second; + bool subAst1HasValue = false; + bool subAst2HasValue = false; + expr * subValue1 = get_eqc_value(subAst1, subAst1HasValue); + expr * subValue2 = get_eqc_value(subAst2, subAst2HasValue); + + TRACE("t_str_detail", + tout << "(Contains " << mk_pp(n1, m) << " " << mk_pp(subAst1, m) << ")" << std::endl; + tout << "(Contains " << mk_pp(n2, m) << " " << mk_pp(subAst2, m) << ")" << std::endl; + if (subAst1 != subValue1) { + tout << mk_pp(subAst1, m) << " = " << mk_pp(subValue1, m) << std::endl; + } + if (subAst2 != subValue2) { + tout << mk_pp(subAst2, m) << " = " << mk_pp(subValue2, m) << std::endl; + } + ); + + if (subAst1HasValue && subAst2HasValue) { + expr_ref_vector litems1(m); + if (n1 != n2) { + litems1.push_back(ctx.mk_eq_atom(n1, n2)); + } + if (subValue1 != subAst1) { + litems1.push_back(ctx.mk_eq_atom(subAst1, subValue1)); + } + if (subValue2 != subAst2) { + litems1.push_back(ctx.mk_eq_atom(subAst2, subValue2)); + } + + std::string subConst1 = m_strutil.get_string_constant_value(subValue1); + std::string subConst2 = m_strutil.get_string_constant_value(subValue2); + expr_ref implyR(m); + if (subConst1 == subConst2) { + // key1.first = key2.first /\ key1.second = key2.second + // ==> (containPairBoolMap[key1] = containPairBoolMap[key2]) + implyR = ctx.mk_eq_atom(contain_pair_bool_map[key1], contain_pair_bool_map[key2]); + } else if (subConst1.find(subConst2) != std::string::npos) { + // key1.first = key2.first /\ Contains(key1.second, key2.second) + // ==> (containPairBoolMap[key1] --> containPairBoolMap[key2]) + implyR = rewrite_implication(contain_pair_bool_map[key1], contain_pair_bool_map[key2]); + } else if (subConst2.find(subConst1) != std::string::npos) { + // key1.first = key2.first /\ Contains(key2.second, key1.second) + // ==> (containPairBoolMap[key2] --> containPairBoolMap[key1]) + implyR = rewrite_implication(contain_pair_bool_map[key2], contain_pair_bool_map[key1]); + } + + if (implyR) { + if (litems1.empty()) { + assert_axiom(implyR); + } else { + assert_implication(mk_and(litems1), implyR); + } + } + } else { + expr_ref_vector subAst1Eqc(m); + expr_ref_vector subAst2Eqc(m); + collect_eq_nodes(subAst1, subAst1Eqc); + collect_eq_nodes(subAst2, subAst2Eqc); + + if (subAst1Eqc.contains(subAst2)) { + // ----------------------------------------------------------- + // * key1.first = key2.first /\ key1.second = key2.second + // --> containPairBoolMap[key1] = containPairBoolMap[key2] + // ----------------------------------------------------------- + expr_ref_vector litems2(m); + if (n1 != n2) { + litems2.push_back(ctx.mk_eq_atom(n1, n2)); + } + if (subAst1 != subAst2) { + litems2.push_back(ctx.mk_eq_atom(subAst1, subAst2)); + } + expr_ref implyR(ctx.mk_eq_atom(contain_pair_bool_map[key1], contain_pair_bool_map[key2]), m); + if (litems2.empty()) { + assert_axiom(implyR); + } else { + assert_implication(mk_and(litems2), implyR); + } + } else { + // ----------------------------------------------------------- + // * key1.first = key2.first + // check eqc(key1.second) and eqc(key2.second) + // ----------------------------------------------------------- + expr_ref_vector::iterator eqItorSub1 = subAst1Eqc.begin(); + for (; eqItorSub1 != subAst1Eqc.end(); eqItorSub1++) { + expr_ref_vector::iterator eqItorSub2 = subAst2Eqc.begin(); + for (; eqItorSub2 != subAst2Eqc.end(); eqItorSub2++) { + // ------------ + // key1.first = key2.first /\ containPairBoolMap[] + // ==> (containPairBoolMap[key1] --> containPairBoolMap[key2]) + // ------------ + { + expr_ref_vector litems3(m); + if (n1 != n2) { + litems3.push_back(ctx.mk_eq_atom(n1, n2)); + } + expr * eqSubVar1 = *eqItorSub1; + if (eqSubVar1 != subAst1) { + litems3.push_back(ctx.mk_eq_atom(subAst1, eqSubVar1)); + } + expr * eqSubVar2 = *eqItorSub2; + if (eqSubVar2 != subAst2) { + litems3.push_back(ctx.mk_eq_atom(subAst2, eqSubVar2)); + } + std::pair tryKey1 = std::make_pair(eqSubVar1, eqSubVar2); + if (contain_pair_bool_map.contains(tryKey1)) { + TRACE("t_str_detail", tout << "(Contains " << mk_pp(eqSubVar1, m) << " " << mk_pp(eqSubVar2, m) << ")" << std::endl;); + litems3.push_back(contain_pair_bool_map[tryKey1]); + expr_ref implR(rewrite_implication(contain_pair_bool_map[key1], contain_pair_bool_map[key2]), m); + assert_implication(mk_and(litems3), implR); + } + } + // ------------ + // key1.first = key2.first /\ containPairBoolMap[] + // ==> (containPairBoolMap[key2] --> containPairBoolMap[key1]) + // ------------ + { + expr_ref_vector litems4(m); + if (n1 != n2) { + litems4.push_back(ctx.mk_eq_atom(n1, n2)); + } + expr * eqSubVar1 = *eqItorSub1; + if (eqSubVar1 != subAst1) { + litems4.push_back(ctx.mk_eq_atom(subAst1, eqSubVar1)); + } + expr * eqSubVar2 = *eqItorSub2; + if (eqSubVar2 != subAst2) { + litems4.push_back(ctx.mk_eq_atom(subAst2, eqSubVar2)); + } + std::pair tryKey2 = std::make_pair(eqSubVar2, eqSubVar1); + if (contain_pair_bool_map.contains(tryKey2)) { + TRACE("t_str_detail", tout << "(Contains " << mk_pp(eqSubVar2, m) << " " << mk_pp(eqSubVar1, m) << ")" << std::endl;); + litems4.push_back(contain_pair_bool_map[tryKey2]); + expr_ref implR(rewrite_implication(contain_pair_bool_map[key2], contain_pair_bool_map[key1]), m); + assert_implication(mk_and(litems4), implR); + } + } + } + } + } + } + } + // *************************** + // Case 2: Contains(..., m) /\ Contains(... , n) /\ m = n + // *************************** + else if (key1.second == n1 && key2.second == n2) { + expr * str1 = key1.first; + expr * str2 = key2.first; + bool str1HasValue = false; + bool str2HasValue = false; + expr * strVal1 = get_eqc_value(str1, str1HasValue); + expr * strVal2 = get_eqc_value(str2, str2HasValue); + + TRACE("t_str_detail", + tout << "(Contains " << mk_pp(str1, m) << " " << mk_pp(n1, m) << ")" << std::endl; + tout << "(Contains " << mk_pp(str2, m) << " " << mk_pp(n2, m) << ")" << std::endl; + if (str1 != strVal1) { + tout << mk_pp(str1, m) << " = " << mk_pp(strVal1, m) << std::endl; + } + if (str2 != strVal2) { + tout << mk_pp(str2, m) << " = " << mk_pp(strVal2, m) << std::endl; + } + ); + + if (str1HasValue && str2HasValue) { + expr_ref_vector litems1(m); + if (n1 != n2) { + litems1.push_back(ctx.mk_eq_atom(n1, n2)); + } + if (strVal1 != str1) { + litems1.push_back(ctx.mk_eq_atom(str1, strVal1)); + } + if (strVal2 != str2) { + litems1.push_back(ctx.mk_eq_atom(str2, strVal2)); + } + + std::string const1 = m_strutil.get_string_constant_value(strVal1); + std::string const2 = m_strutil.get_string_constant_value(strVal2); + expr_ref implyR(m); + + if (const1 == const2) { + // key1.second = key2.second /\ key1.first = key2.first + // ==> (containPairBoolMap[key1] = containPairBoolMap[key2]) + implyR = ctx.mk_eq_atom(contain_pair_bool_map[key1], contain_pair_bool_map[key2]); + } else if (const1.find(const2) != std::string::npos) { + // key1.second = key2.second /\ Contains(key1.first, key2.first) + // ==> (containPairBoolMap[key2] --> containPairBoolMap[key1]) + implyR = rewrite_implication(contain_pair_bool_map[key2], contain_pair_bool_map[key1]); + } else if (const2.find(const1) != std::string::npos) { + // key1.first = key2.first /\ Contains(key2.first, key1.first) + // ==> (containPairBoolMap[key1] --> containPairBoolMap[key2]) + implyR = rewrite_implication(contain_pair_bool_map[key1], contain_pair_bool_map[key2]); + } + + if (implyR) { + if (litems1.size() == 0) { + assert_axiom(implyR); + } else { + assert_implication(mk_and(litems1), implyR); + } + } + } + + else { + expr_ref_vector str1Eqc(m); + expr_ref_vector str2Eqc(m); + collect_eq_nodes(str1, str1Eqc); + collect_eq_nodes(str2, str2Eqc); + + if (str1Eqc.contains(str2)) { + // ----------------------------------------------------------- + // * key1.first = key2.first /\ key1.second = key2.second + // --> containPairBoolMap[key1] = containPairBoolMap[key2] + // ----------------------------------------------------------- + expr_ref_vector litems2(m); + if (n1 != n2) { + litems2.push_back(ctx.mk_eq_atom(n1, n2)); + } + if (str1 != str2) { + litems2.push_back(ctx.mk_eq_atom(str1, str2)); + } + expr_ref implyR(ctx.mk_eq_atom(contain_pair_bool_map[key1], contain_pair_bool_map[key2]), m); + if (litems2.empty()) { + assert_axiom(implyR); + } else { + assert_implication(mk_and(litems2), implyR); + } + } else { + // ----------------------------------------------------------- + // * key1.second = key2.second + // check eqc(key1.first) and eqc(key2.first) + // ----------------------------------------------------------- + expr_ref_vector::iterator eqItorStr1 = str1Eqc.begin(); + for (; eqItorStr1 != str1Eqc.end(); eqItorStr1++) { + expr_ref_vector::iterator eqItorStr2 = str2Eqc.begin(); + for (; eqItorStr2 != str2Eqc.end(); eqItorStr2++) { + { + expr_ref_vector litems3(m); + if (n1 != n2) { + litems3.push_back(ctx.mk_eq_atom(n1, n2)); + } + expr * eqStrVar1 = *eqItorStr1; + if (eqStrVar1 != str1) { + litems3.push_back(ctx.mk_eq_atom(str1, eqStrVar1)); + } + expr * eqStrVar2 = *eqItorStr2; + if (eqStrVar2 != str2) { + litems3.push_back(ctx.mk_eq_atom(str2, eqStrVar2)); + } + std::pair tryKey1 = std::make_pair(eqStrVar1, eqStrVar2); + if (contain_pair_bool_map.contains(tryKey1)) { + TRACE("t_str_detail", tout << "(Contains " << mk_pp(eqStrVar1, m) << " " << mk_pp(eqStrVar2, m) << ")" << std::endl;); + litems3.push_back(contain_pair_bool_map[tryKey1]); + + // ------------ + // key1.second = key2.second /\ containPairBoolMap[] + // ==> (containPairBoolMap[key2] --> containPairBoolMap[key1]) + // ------------ + expr_ref implR(rewrite_implication(contain_pair_bool_map[key2], contain_pair_bool_map[key1]), m); + assert_implication(mk_and(litems3), implR); + } + } + + { + expr_ref_vector litems4(m); + if (n1 != n2) { + litems4.push_back(ctx.mk_eq_atom(n1, n2)); + } + expr * eqStrVar1 = *eqItorStr1; + if (eqStrVar1 != str1) { + litems4.push_back(ctx.mk_eq_atom(str1, eqStrVar1)); + } + expr *eqStrVar2 = *eqItorStr2; + if (eqStrVar2 != str2) { + litems4.push_back(ctx.mk_eq_atom(str2, eqStrVar2)); + } + std::pair tryKey2 = std::make_pair(eqStrVar2, eqStrVar1); + + if (contain_pair_bool_map.contains(tryKey2)) { + TRACE("t_str_detail", tout << "(Contains " << mk_pp(eqStrVar2, m) << " " << mk_pp(eqStrVar1, m) << ")" << std::endl;); + litems4.push_back(contain_pair_bool_map[tryKey2]); + // ------------ + // key1.first = key2.first /\ containPairBoolMap[] + // ==> (containPairBoolMap[key1] --> containPairBoolMap[key2]) + // ------------ + expr_ref implR(ctx.mk_eq_atom(contain_pair_bool_map[key1], contain_pair_bool_map[key2]), m); + assert_implication(mk_and(litems4), implR); + } + } + } + } + } + } + + } + } + + if (n1 == n2) { + break; + } + } + } // (in_contain_idx_map(n1) && in_contain_idx_map(n2)) } void theory_str::check_contain_in_new_eq(expr * n1, expr * n2) { @@ -4444,9 +4790,7 @@ void theory_str::check_contain_in_new_eq(expr * n1, expr * n2) { expr_ref_vector::iterator varItor2 = varItor1; for (; varItor2 != willEqClass.end(); ++varItor2) { expr * varAst2 = *varItor2; - // for testing purposes - TRACE("t_str", tout << "WARNING: some Contains checks disabled!" << std::endl;); - // check_contain_by_eq_nodes(varAst1, varAst2); + check_contain_by_eq_nodes(varAst1, varAst2); } } } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 476519e5c..e5fd25894 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -49,6 +49,26 @@ namespace smt { virtual void register_value(expr * n) { /* Ignore */ } }; + // rather than modify obj_pair_map I inherit from it and add my own helper methods + class theory_str_contain_pair_bool_map_t : public obj_pair_map { + public: + expr * operator[](std::pair key) const { + expr * value; + bool found = this->find(key.first, key.second, value); + if (found) { + return value; + } else { + TRACE("t_str", tout << "WARNING: lookup miss in contain_pair_bool_map!" << std::endl;); + return NULL; + } + } + + bool contains(std::pair key) const { + expr * unused; + return this->find(key.first, key.second, unused); + } + }; + class theory_str : public theory { struct T_cut { @@ -191,7 +211,10 @@ namespace smt { std::map unroll_var_map; std::map, expr*> concat_eq_unroll_ast_map; - expr_ref_vector contains_map; // was containPairBoolMap in Z3str2 + expr_ref_vector contains_map; + + theory_str_contain_pair_bool_map_t contain_pair_bool_map; + obj_map > contain_pair_idx_map; char * char_set; std::map charSetLookupTable; @@ -200,6 +223,7 @@ namespace smt { protected: void assert_axiom(expr * e); void assert_implication(expr * premise, expr * conclusion); + expr * rewrite_implication(expr * premise, expr * conclusion); app * mk_strlen(expr * e); expr * mk_concat(expr * n1, expr * n2); @@ -313,6 +337,7 @@ namespace smt { void check_contain_by_eqc_val(expr * varNode, expr * constNode); void check_contain_by_substr(expr * varNode, expr_ref_vector & willEqClass); void check_contain_by_eq_nodes(expr * n1, expr * n2); + bool in_contain_idx_map(expr * n); void get_nodes_in_concat(expr * node, ptr_vector & nodeList); expr * simplify_concat(expr * node); diff --git a/src/util/obj_pair_set.h b/src/util/obj_pair_set.h index 29139a51d..c4212977c 100644 --- a/src/util/obj_pair_set.h +++ b/src/util/obj_pair_set.h @@ -46,6 +46,11 @@ public: bool contains(obj_pair const & p) const { return m_set.contains(p); } void reset() { m_set.reset(); } bool empty() const { return m_set.empty(); } + + typedef typename chashtable::iterator iterator; + + iterator begin() { return m_set.begin(); } + iterator end() { return m_set.end(); } }; #endif From 685edbb268cb1034c580fda269c1e226118da17e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 15 Aug 2016 18:58:36 -0400 Subject: [PATCH 182/562] pull out incorrectly-used data structures in theory_str for contains check, this will need to be revisited --- src/smt/theory_str.cpp | 6 +++--- src/smt/theory_str.h | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 93173402c..40745b069 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4376,7 +4376,7 @@ void theory_str::check_contain_by_substr(expr * varNode, expr_ref_vector & willE } bool theory_str::in_contain_idx_map(expr * n) { - return contain_pair_idx_map.contains(n); + return contain_pair_idx_map.find(n) != contain_pair_idx_map.end(); } void theory_str::check_contain_by_eq_nodes(expr * n1, expr * n2) { @@ -4384,8 +4384,8 @@ void theory_str::check_contain_by_eq_nodes(expr * n1, expr * n2) { ast_manager & m = get_manager(); if (in_contain_idx_map(n1) && in_contain_idx_map(n2)) { - obj_pair_set::iterator keysItor1 = contain_pair_idx_map[n1].begin(); - obj_pair_set::iterator keysItor2; + std::set >::iterator keysItor1 = contain_pair_idx_map[n1].begin(); + std::set >::iterator keysItor2; for (; keysItor1 != contain_pair_idx_map[n1].end(); keysItor1++) { // keysItor1 is on set {<.., n1>, ..., , ...} diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index e5fd25894..fd93edfd4 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -214,7 +214,9 @@ namespace smt { expr_ref_vector contains_map; theory_str_contain_pair_bool_map_t contain_pair_bool_map; - obj_map > contain_pair_idx_map; + //obj_map > contain_pair_idx_map; + // TODO Find a better data structure, this is 100% a hack right now + std::map > > contain_pair_idx_map; char * char_set; std::map charSetLookupTable; From 48081864b01904f096f219cd30d119b05c0b8961 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 16 Aug 2016 18:07:31 -0400 Subject: [PATCH 183/562] add regex validation in str_rewriter --- src/ast/rewriter/str_rewriter.cpp | 200 ++++++++++++++++++++++++++++++ src/ast/str_decl_plugin.h | 4 +- 2 files changed, 202 insertions(+), 2 deletions(-) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index fdb67f89e..54e0dd443 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -22,6 +22,192 @@ Notes: #include"ast_pp.h" #include"ast_util.h" #include"well_sorted.h" +#include +#include +#include + +class nfa { +protected: + str_util & m_strutil; + + bool m_valid; + unsigned m_next_id; + + unsigned next_id() { + unsigned retval = m_next_id; + ++m_next_id; + return retval; + } + + unsigned m_start_state; + unsigned m_end_state; + + std::map > transition_map; + std::map > epsilon_map; + + void make_transition(unsigned start, char symbol, unsigned end) { + transition_map[start][symbol] = end; + } + + void make_epsilon_move(unsigned start, unsigned end) { + epsilon_map[start].insert(end); + } + + // Convert a regular expression to an e-NFA using Thompson's construction + void convert_re(expr * e, unsigned & start, unsigned & end) { + start = next_id(); + end = next_id(); + if (m_strutil.is_re_Str2Reg(e)) { + app * a = to_app(e); + expr * arg_str = a->get_arg(0); + if (m_strutil.is_string(arg_str)) { + std::string str = m_strutil.get_string_constant_value(arg_str); + TRACE("t_str_rw", tout << "build NFA for '" << str << "'" << std::endl;); + + // TODO this assumes the string is not empty + /* + * For an n-character string, we make (n-1) intermediate states, + * labelled i_(0) through i_(n-2). + * Then we construct the following transitions: + * start --str[0]--> i_(0) --str[1]--> i_(1) --...--> i_(n-2) --str[n-1]--> final + */ + unsigned last = start; + for (unsigned i = 0; i <= str.length() - 2; ++i) { + unsigned i_state = next_id(); + make_transition(last, str.at(i), i_state); + TRACE("t_str_rw", tout << "string transition " << last << "--" << str.at(i) << "--> " << i_state << std::endl;); + last = i_state; + } + make_transition(last, str.at(str.length() - 1), end); + TRACE("t_str_rw", tout << "string transition " << last << "--" << str.at(str.length() - 1) << "--> " << end << std::endl;); + TRACE("t_str_rw", tout << "string NFA: start = " << start << ", end = " << end << std::endl;); + } else { + TRACE("t_str_rw", tout << "invalid string constant in Str2Reg" << std::endl;); + m_valid = false; + return; + } + } else if (m_strutil.is_re_RegexConcat(e)){ + app * a = to_app(e); + expr * re1 = a->get_arg(0); + expr * re2 = a->get_arg(1); + unsigned start1, end1; + convert_re(re1, start1, end1); + unsigned start2, end2; + convert_re(re2, start2, end2); + // start --e--> start1 --...--> end1 --e--> start2 --...--> end2 --e--> end + make_epsilon_move(start, start1); + make_epsilon_move(end1, start2); + make_epsilon_move(end2, end); + TRACE("t_str_rw", tout << "concat NFA: start = " << start << ", end = " << end << std::endl;); + } else if (m_strutil.is_re_RegexUnion(e)) { + app * a = to_app(e); + expr * re1 = a->get_arg(0); + expr * re2 = a->get_arg(1); + unsigned start1, end1; + convert_re(re1, start1, end1); + unsigned start2, end2; + convert_re(re2, start2, end2); + + // start --e--> start1 ; start --e--> start2 + // end1 --e--> end ; end2 --e--> end + make_epsilon_move(start, start1); + make_epsilon_move(start, start2); + make_epsilon_move(end1, end); + make_epsilon_move(end2, end); + TRACE("t_str_rw", tout << "union NFA: start = " << start << ", end = " << end << std::endl;); + } else if (m_strutil.is_re_RegexStar(e)) { + app * a = to_app(e); + expr * subex = a->get_arg(0); + unsigned start_subex, end_subex; + convert_re(subex, start_subex, end_subex); + // start --e--> start_subex, start --e--> end + // end_subex --e--> start_subex, end_subex --e--> end + make_epsilon_move(start, start_subex); + make_epsilon_move(start, end); + make_epsilon_move(end_subex, start_subex); + make_epsilon_move(end_subex, end); + TRACE("t_str_rw", tout << "star NFA: start = " << start << ", end = " << end << std::endl;); + } else { + TRACE("t_str_rw", tout << "invalid regular expression" << std::endl;); + m_valid = false; + return; + } + } + +public: + nfa(str_util & m_strutil, expr * e) +: m_strutil(m_strutil), + m_valid(true), m_next_id(0), m_start_state(0), m_end_state(0) { + convert_re(e, m_start_state, m_end_state); + } + + bool is_valid() const { + return m_valid; + } + + void epsilon_closure(unsigned start, std::set & closure) { + std::deque worklist; + closure.insert(start); + worklist.push_back(start); + + while(!worklist.empty()) { + unsigned state = worklist.front(); + worklist.pop_front(); + if (epsilon_map.find(state) != epsilon_map.end()) { + for (std::set::iterator it = epsilon_map[state].begin(); + it != epsilon_map[state].end(); ++it) { + unsigned new_state = *it; + if (closure.find(new_state) == closure.end()) { + closure.insert(new_state); + worklist.push_back(new_state); + } + } + } + } + } + + bool matches(std::string input) { + /* + * Keep a set of all states the NFA can currently be in. + * Initially this is the e-closure of m_start_state + * For each character A in the input string, + * the set of next states contains + * all states in transition_map[S][A] for each S in current_states, + * and all states in epsilon_map[S] for each S in current_states. + * After consuming the entire input string, + * the match is successful iff current_states contains m_end_state. + */ + std::set current_states; + epsilon_closure(m_start_state, current_states); + for (unsigned i = 0; i < input.length(); ++i) { + char A = input.at(i); + std::set next_states; + for (std::set::iterator it = current_states.begin(); + it != current_states.end(); ++it) { + unsigned S = *it; + // check transition_map + if (transition_map[S].find(A) != transition_map[S].end()) { + next_states.insert(transition_map[S][A]); + } + } + + // take e-closure over next_states to compute the actual next_states + std::set epsilon_next_states; + for (std::set::iterator it = next_states.begin(); it != next_states.end(); ++it) { + unsigned S = *it; + std::set closure; + epsilon_closure(S, closure); + epsilon_next_states.insert(closure.begin(), closure.end()); + } + current_states = epsilon_next_states; + } + if (current_states.find(m_end_state) != current_states.end()) { + return true; + } else { + return false; + } + } +}; br_status str_rewriter::mk_str_Concat(expr * arg0, expr * arg1, expr_ref & result) { TRACE("t_str_rw", tout << "rewrite (Concat " << mk_pp(arg0, m()) << " " << mk_pp(arg1, m()) << ")" << std::endl;); @@ -243,6 +429,20 @@ br_status str_rewriter::mk_re_RegexIn(expr * str, expr * re, expr_ref & result) return BR_REWRITE_FULL; } + // necessary for model validation + if (m_strutil.is_string(str)) { + TRACE("t_str_rw", tout << "RegexIn with constant string argument" << std::endl;); + nfa regex_nfa(m_strutil, re); + ENSURE(regex_nfa.is_valid()); + std::string input = m_strutil.get_string_constant_value(str); + if (regex_nfa.matches(input)) { + result = m().mk_true(); + } else { + result = m().mk_false(); + } + return BR_DONE; + } + return BR_FAILED; } diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 4b7a8858e..5b0ca2a3a 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -111,7 +111,6 @@ public: virtual bool is_value(app * e) const; virtual bool is_unique_value(app * e) const { return is_value(e); } - // TODO }; class str_recognizers { @@ -125,11 +124,12 @@ public: bool is_string(expr const * n) const; bool is_re_Str2Reg(expr const * n) const { return is_app_of(n, get_fid(), OP_RE_STR2REGEX); } + bool is_re_RegexConcat(expr const * n) const { return is_app_of(n, get_fid(), OP_RE_REGEXCONCAT); } + bool is_re_RegexUnion(expr const * n) const { return is_app_of(n, get_fid(), OP_RE_REGEXUNION); } bool is_re_RegexStar(expr const * n) const { return is_app_of(n, get_fid(), OP_RE_REGEXSTAR); } bool is_re_RegexPlus(expr const * n) const { return is_app_of(n, get_fid(), OP_RE_REGEXPLUS); } std::string get_string_constant_value(expr const *n) const; - // TODO }; class str_util : public str_recognizers { From 0834229b394754ef6cec69f1d4de7206a669bcaf Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 17 Aug 2016 15:33:02 -0400 Subject: [PATCH 184/562] theory_str model validation for substr --- src/ast/rewriter/str_rewriter.cpp | 20 ++++++++++++++++++++ src/ast/rewriter/str_rewriter.h | 1 + 2 files changed, 21 insertions(+) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index 54e0dd443..fe434575e 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -412,6 +412,23 @@ br_status str_rewriter::mk_str_Replace(expr * base, expr * source, expr * target } } +br_status str_rewriter::mk_str_Substr(expr * base, expr * start, expr * len, expr_ref & result) { + TRACE("t_str_rw", tout << "rewrite (Substr " << mk_pp(base, m()) << " " << mk_pp(start, m()) << " " << mk_pp(len, m()) << ")" << std::endl;); + rational startVal, lenVal; + if (m_strutil.is_string(base) && m_autil.is_numeral(start, startVal) && m_autil.is_numeral(len, lenVal)) { + std::string baseStr = m_strutil.get_string_constant_value(base); + // TODO handling for invalid start/len + if (startVal.is_nonneg() && lenVal.is_nonneg() && startVal.get_unsigned() <= baseStr.length()) { + TRACE("t_str_rw", tout << "rewriting constant Substr expression" << std::endl;); + std::string substr = baseStr.substr(startVal.get_unsigned(), lenVal.get_unsigned()); + result = m_strutil.mk_string(substr); + return BR_DONE; + } + } + + return BR_FAILED; +} + br_status str_rewriter::mk_re_Str2Reg(expr * str, expr_ref & result) { // the argument to Str2Reg *must* be a string constant ENSURE(m_strutil.is_string(str)); @@ -532,6 +549,9 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con case OP_STR_REPLACE: SASSERT(num_args == 3); return mk_str_Replace(args[0], args[1], args[2], result); + case OP_STR_SUBSTR: + SASSERT(num_args == 3); + return mk_str_Substr(args[0], args[1], args[2], result); case OP_RE_STR2REGEX: SASSERT(num_args == 1); return mk_re_Str2Reg(args[0], result); diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index 2235425be..862fc3e7e 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -50,6 +50,7 @@ public: br_status mk_str_Indexof2(expr * arg0, expr * arg1, expr * arg2, expr_ref & result); br_status mk_str_LastIndexof(expr * haystack, expr * needle, expr_ref & result); br_status mk_str_Replace(expr * base, expr * source, expr * target, expr_ref & result); + br_status mk_str_Substr(expr * base, expr * start, expr * len, expr_ref & result); br_status mk_re_Str2Reg(expr * str, expr_ref & result); br_status mk_re_RegexIn(expr * str, expr * re, expr_ref & result); From 71ad4d3a4abc08f4e2638fa7e0b23b14ab8575d4 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 17 Aug 2016 16:21:19 -0400 Subject: [PATCH 185/562] add regex_in_bool_map to theory_str --- src/smt/theory_str.cpp | 74 ++++++++++++++++++++++++++++++++++-------- src/smt/theory_str.h | 4 +++ 2 files changed, 65 insertions(+), 13 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 40745b069..087bf6ad0 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1438,30 +1438,78 @@ expr * theory_str::mk_RegexIn(expr * str, expr * regexp) { return regexIn; } +static std::string str2RegexStr(std::string str) { + std::string res = ""; + int len = str.size(); + for (int i = 0; i < len; i++) { + char nc = str[i]; + // 12 special chars + if (nc == '\\' || nc == '^' || nc == '$' || nc == '.' || nc == '|' || nc == '?' + || nc == '*' || nc == '+' || nc == '(' || nc == ')' || nc == '[' || nc == '{') { + res.append(1, '\\'); + } + res.append(1, str[i]); + } + return res; +} + +std::string theory_str::get_std_regex_str(expr * regex) { + app * a_regex = to_app(regex); + if (is_Str2Reg(a_regex)) { + expr * regAst = a_regex->get_arg(0); + std::string regStr = str2RegexStr(m_strutil.get_string_constant_value(regAst)); + return regStr; + } else if (is_RegexConcat(a_regex)) { + expr * reg1Ast = a_regex->get_arg(0); + expr * reg2Ast = a_regex->get_arg(1); + std::string reg1Str = get_std_regex_str(reg1Ast); + std::string reg2Str = get_std_regex_str(reg2Ast); + return "(" + reg1Str + ")(" + reg2Str + ")"; + } else if (is_RegexUnion(a_regex)) { + expr * reg1Ast = a_regex->get_arg(0); + expr * reg2Ast = a_regex->get_arg(1); + std::string reg1Str = get_std_regex_str(reg1Ast); + std::string reg2Str = get_std_regex_str(reg2Ast); + return "(" + reg1Str + ")|(" + reg2Str + ")"; + } else if (is_RegexStar(a_regex)) { + expr * reg1Ast = a_regex->get_arg(0); + std::string reg1Str = get_std_regex_str(reg1Ast); + return "(" + reg1Str + ")*"; + } else { + TRACE("t_str", tout << "BUG: unrecognized regex term " << mk_pp(regex, get_manager()) << std::endl;); + UNREACHABLE(); return ""; + } +} + void theory_str::instantiate_axiom_RegexIn(enode * e) { context & ctx = get_context(); ast_manager & m = get_manager(); - app * expr = e->get_owner(); - if (axiomatized_terms.contains(expr)) { - TRACE("t_str_detail", tout << "already set up RegexIn axiom for " << mk_pp(expr, m) << std::endl;); + app * ex = e->get_owner(); + if (axiomatized_terms.contains(ex)) { + TRACE("t_str_detail", tout << "already set up RegexIn axiom for " << mk_pp(ex, m) << std::endl;); return; } - axiomatized_terms.insert(expr); + axiomatized_terms.insert(ex); - TRACE("t_str_detail", tout << "instantiate RegexIn axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("t_str_detail", tout << "instantiate RegexIn axiom for " << mk_pp(ex, m) << std::endl;); - // I don't think we need to port regexInBoolMap and regexInVarStrMap, - // but they would go here from reduce_regexIn + { + std::string regexStr = get_std_regex_str(ex->get_arg(1)); + std::pair key1(ex->get_arg(0), regexStr); + // skip Z3str's map check, because we already check if we set up axioms on this term + regex_in_bool_map[key1] = ex; + regex_in_var_reg_str_map[ex->get_arg(0)].insert(regexStr); + } - expr_ref str(expr->get_arg(0), m); - app * regex = to_app(expr->get_arg(1)); + expr_ref str(ex->get_arg(0), m); + app * regex = to_app(ex->get_arg(1)); if (is_Str2Reg(regex)) { expr_ref rxStr(regex->get_arg(0), m); // want to assert 'expr IFF (str == rxStr)' expr_ref rhs(ctx.mk_eq_atom(str, rxStr), m); - expr_ref finalAxiom(m.mk_iff(expr, rhs), m); + expr_ref finalAxiom(m.mk_iff(ex, rhs), m); SASSERT(finalAxiom); assert_axiom(finalAxiom); } else if (is_RegexConcat(regex)) { @@ -1476,7 +1524,7 @@ void theory_str::instantiate_axiom_RegexIn(enode * e) { expr_ref_vector items(m); items.push_back(var1InRegex1); items.push_back(var2InRegex2); - items.push_back(ctx.mk_eq_atom(expr, ctx.mk_eq_atom(str, rhs))); + items.push_back(ctx.mk_eq_atom(ex, ctx.mk_eq_atom(str, rhs))); expr_ref finalAxiom(mk_and(items), m); SASSERT(finalAxiom); @@ -1492,7 +1540,7 @@ void theory_str::instantiate_axiom_RegexIn(enode * e) { expr_ref_vector items(m); items.push_back(var1InRegex1); items.push_back(var2InRegex2); - items.push_back(ctx.mk_eq_atom(expr, orVar)); + items.push_back(ctx.mk_eq_atom(ex, orVar)); assert_axiom(mk_and(items)); } else if (is_RegexStar(regex)) { // slightly more complex due to the unrolling step. @@ -1500,7 +1548,7 @@ void theory_str::instantiate_axiom_RegexIn(enode * e) { expr_ref unrollCount(mk_unroll_bound_var(), m); expr_ref unrollFunc(mk_unroll(regex1, unrollCount), m); expr_ref_vector items(m); - items.push_back(ctx.mk_eq_atom(expr, ctx.mk_eq_atom(str, unrollFunc))); + items.push_back(ctx.mk_eq_atom(ex, ctx.mk_eq_atom(str, unrollFunc))); items.push_back(ctx.mk_eq_atom(ctx.mk_eq_atom(unrollCount, mk_int(0)), ctx.mk_eq_atom(unrollFunc, m_strutil.mk_string("")))); expr_ref finalAxiom(mk_and(items), m); SASSERT(finalAxiom); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index fd93edfd4..06a72c3e2 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -218,6 +218,9 @@ namespace smt { // TODO Find a better data structure, this is 100% a hack right now std::map > > contain_pair_idx_map; + std::map, expr*> regex_in_bool_map; + std::map > regex_in_var_reg_str_map; + char * char_set; std::map charSetLookupTable; int charSetSize; @@ -419,6 +422,7 @@ namespace smt { expr * gen_unroll_conditional_options(expr * var, std::set & unrolls, std::string lcmStr); expr * gen_unroll_assign(expr * var, std::string lcmStr, expr * testerVar, int l, int h); void reduce_virtual_regex_in(expr * var, expr * regex, expr_ref_vector & items); + std::string get_std_regex_str(expr * regex); void dump_assignments(); void initialize_charset(); From 6263391c11278ca6653d61f9cc059b9b9232b4e5 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 17 Aug 2016 20:58:57 -0400 Subject: [PATCH 186/562] fix out-of-range integer comparison bug in string NFA --- src/ast/rewriter/str_rewriter.cpp | 290 +++++++++++++----------------- src/ast/rewriter/str_rewriter.h | 47 +++++ src/smt/theory_str.cpp | 59 +++++- src/smt/theory_str.h | 12 ++ 4 files changed, 243 insertions(+), 165 deletions(-) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index fe434575e..c644ecd46 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -26,188 +26,150 @@ Notes: #include #include -class nfa { -protected: - str_util & m_strutil; +// Convert a regular expression to an e-NFA using Thompson's construction +void nfa::convert_re(expr * e, unsigned & start, unsigned & end, str_util & m_strutil) { + start = next_id(); + end = next_id(); + if (m_strutil.is_re_Str2Reg(e)) { + app * a = to_app(e); + expr * arg_str = a->get_arg(0); + if (m_strutil.is_string(arg_str)) { + std::string str = m_strutil.get_string_constant_value(arg_str); + TRACE("t_str_rw", tout << "build NFA for '" << str << "'" << std::endl;); - bool m_valid; - unsigned m_next_id; - - unsigned next_id() { - unsigned retval = m_next_id; - ++m_next_id; - return retval; - } - - unsigned m_start_state; - unsigned m_end_state; - - std::map > transition_map; - std::map > epsilon_map; - - void make_transition(unsigned start, char symbol, unsigned end) { - transition_map[start][symbol] = end; - } - - void make_epsilon_move(unsigned start, unsigned end) { - epsilon_map[start].insert(end); - } - - // Convert a regular expression to an e-NFA using Thompson's construction - void convert_re(expr * e, unsigned & start, unsigned & end) { - start = next_id(); - end = next_id(); - if (m_strutil.is_re_Str2Reg(e)) { - app * a = to_app(e); - expr * arg_str = a->get_arg(0); - if (m_strutil.is_string(arg_str)) { - std::string str = m_strutil.get_string_constant_value(arg_str); - TRACE("t_str_rw", tout << "build NFA for '" << str << "'" << std::endl;); - - // TODO this assumes the string is not empty - /* - * For an n-character string, we make (n-1) intermediate states, - * labelled i_(0) through i_(n-2). - * Then we construct the following transitions: - * start --str[0]--> i_(0) --str[1]--> i_(1) --...--> i_(n-2) --str[n-1]--> final - */ - unsigned last = start; - for (unsigned i = 0; i <= str.length() - 2; ++i) { - unsigned i_state = next_id(); - make_transition(last, str.at(i), i_state); - TRACE("t_str_rw", tout << "string transition " << last << "--" << str.at(i) << "--> " << i_state << std::endl;); - last = i_state; - } - make_transition(last, str.at(str.length() - 1), end); - TRACE("t_str_rw", tout << "string transition " << last << "--" << str.at(str.length() - 1) << "--> " << end << std::endl;); - TRACE("t_str_rw", tout << "string NFA: start = " << start << ", end = " << end << std::endl;); - } else { - TRACE("t_str_rw", tout << "invalid string constant in Str2Reg" << std::endl;); - m_valid = false; - return; + // TODO this assumes the string is not empty + /* + * For an n-character string, we make (n-1) intermediate states, + * labelled i_(0) through i_(n-2). + * Then we construct the following transitions: + * start --str[0]--> i_(0) --str[1]--> i_(1) --...--> i_(n-2) --str[n-1]--> final + */ + unsigned last = start; + for (int i = 0; i <= ((int)str.length()) - 2; ++i) { + unsigned i_state = next_id(); + make_transition(last, str.at(i), i_state); + TRACE("t_str_rw", tout << "string transition " << last << "--" << str.at(i) << "--> " << i_state << std::endl;); + last = i_state; } - } else if (m_strutil.is_re_RegexConcat(e)){ - app * a = to_app(e); - expr * re1 = a->get_arg(0); - expr * re2 = a->get_arg(1); - unsigned start1, end1; - convert_re(re1, start1, end1); - unsigned start2, end2; - convert_re(re2, start2, end2); - // start --e--> start1 --...--> end1 --e--> start2 --...--> end2 --e--> end - make_epsilon_move(start, start1); - make_epsilon_move(end1, start2); - make_epsilon_move(end2, end); - TRACE("t_str_rw", tout << "concat NFA: start = " << start << ", end = " << end << std::endl;); - } else if (m_strutil.is_re_RegexUnion(e)) { - app * a = to_app(e); - expr * re1 = a->get_arg(0); - expr * re2 = a->get_arg(1); - unsigned start1, end1; - convert_re(re1, start1, end1); - unsigned start2, end2; - convert_re(re2, start2, end2); - - // start --e--> start1 ; start --e--> start2 - // end1 --e--> end ; end2 --e--> end - make_epsilon_move(start, start1); - make_epsilon_move(start, start2); - make_epsilon_move(end1, end); - make_epsilon_move(end2, end); - TRACE("t_str_rw", tout << "union NFA: start = " << start << ", end = " << end << std::endl;); - } else if (m_strutil.is_re_RegexStar(e)) { - app * a = to_app(e); - expr * subex = a->get_arg(0); - unsigned start_subex, end_subex; - convert_re(subex, start_subex, end_subex); - // start --e--> start_subex, start --e--> end - // end_subex --e--> start_subex, end_subex --e--> end - make_epsilon_move(start, start_subex); - make_epsilon_move(start, end); - make_epsilon_move(end_subex, start_subex); - make_epsilon_move(end_subex, end); - TRACE("t_str_rw", tout << "star NFA: start = " << start << ", end = " << end << std::endl;); + make_transition(last, str.at(str.length() - 1), end); + TRACE("t_str_rw", tout << "string transition " << last << "--" << str.at(str.length() - 1) << "--> " << end << std::endl;); + TRACE("t_str_rw", tout << "string NFA: start = " << start << ", end = " << end << std::endl;); } else { - TRACE("t_str_rw", tout << "invalid regular expression" << std::endl;); + TRACE("t_str_rw", tout << "invalid string constant in Str2Reg" << std::endl;); m_valid = false; return; } + } else if (m_strutil.is_re_RegexConcat(e)){ + app * a = to_app(e); + expr * re1 = a->get_arg(0); + expr * re2 = a->get_arg(1); + unsigned start1, end1; + convert_re(re1, start1, end1, m_strutil); + unsigned start2, end2; + convert_re(re2, start2, end2, m_strutil); + // start --e--> start1 --...--> end1 --e--> start2 --...--> end2 --e--> end + make_epsilon_move(start, start1); + make_epsilon_move(end1, start2); + make_epsilon_move(end2, end); + TRACE("t_str_rw", tout << "concat NFA: start = " << start << ", end = " << end << std::endl;); + } else if (m_strutil.is_re_RegexUnion(e)) { + app * a = to_app(e); + expr * re1 = a->get_arg(0); + expr * re2 = a->get_arg(1); + unsigned start1, end1; + convert_re(re1, start1, end1, m_strutil); + unsigned start2, end2; + convert_re(re2, start2, end2, m_strutil); + + // start --e--> start1 ; start --e--> start2 + // end1 --e--> end ; end2 --e--> end + make_epsilon_move(start, start1); + make_epsilon_move(start, start2); + make_epsilon_move(end1, end); + make_epsilon_move(end2, end); + TRACE("t_str_rw", tout << "union NFA: start = " << start << ", end = " << end << std::endl;); + } else if (m_strutil.is_re_RegexStar(e)) { + app * a = to_app(e); + expr * subex = a->get_arg(0); + unsigned start_subex, end_subex; + convert_re(subex, start_subex, end_subex, m_strutil); + // start --e--> start_subex, start --e--> end + // end_subex --e--> start_subex, end_subex --e--> end + make_epsilon_move(start, start_subex); + make_epsilon_move(start, end); + make_epsilon_move(end_subex, start_subex); + make_epsilon_move(end_subex, end); + TRACE("t_str_rw", tout << "star NFA: start = " << start << ", end = " << end << std::endl;); + } else { + TRACE("t_str_rw", tout << "invalid regular expression" << std::endl;); + m_valid = false; + return; } +} -public: - nfa(str_util & m_strutil, expr * e) -: m_strutil(m_strutil), - m_valid(true), m_next_id(0), m_start_state(0), m_end_state(0) { - convert_re(e, m_start_state, m_end_state); - } +void nfa::epsilon_closure(unsigned start, std::set & closure) { + std::deque worklist; + closure.insert(start); + worklist.push_back(start); - bool is_valid() const { - return m_valid; - } - - void epsilon_closure(unsigned start, std::set & closure) { - std::deque worklist; - closure.insert(start); - worklist.push_back(start); - - while(!worklist.empty()) { - unsigned state = worklist.front(); - worklist.pop_front(); - if (epsilon_map.find(state) != epsilon_map.end()) { - for (std::set::iterator it = epsilon_map[state].begin(); - it != epsilon_map[state].end(); ++it) { - unsigned new_state = *it; - if (closure.find(new_state) == closure.end()) { - closure.insert(new_state); - worklist.push_back(new_state); - } + while(!worklist.empty()) { + unsigned state = worklist.front(); + worklist.pop_front(); + if (epsilon_map.find(state) != epsilon_map.end()) { + for (std::set::iterator it = epsilon_map[state].begin(); + it != epsilon_map[state].end(); ++it) { + unsigned new_state = *it; + if (closure.find(new_state) == closure.end()) { + closure.insert(new_state); + worklist.push_back(new_state); } } } } +} - bool matches(std::string input) { - /* - * Keep a set of all states the NFA can currently be in. - * Initially this is the e-closure of m_start_state - * For each character A in the input string, - * the set of next states contains - * all states in transition_map[S][A] for each S in current_states, - * and all states in epsilon_map[S] for each S in current_states. - * After consuming the entire input string, - * the match is successful iff current_states contains m_end_state. - */ - std::set current_states; - epsilon_closure(m_start_state, current_states); - for (unsigned i = 0; i < input.length(); ++i) { - char A = input.at(i); - std::set next_states; - for (std::set::iterator it = current_states.begin(); - it != current_states.end(); ++it) { - unsigned S = *it; - // check transition_map - if (transition_map[S].find(A) != transition_map[S].end()) { - next_states.insert(transition_map[S][A]); - } +bool nfa::matches(std::string input) { + /* + * Keep a set of all states the NFA can currently be in. + * Initially this is the e-closure of m_start_state + * For each character A in the input string, + * the set of next states contains + * all states in transition_map[S][A] for each S in current_states, + * and all states in epsilon_map[S] for each S in current_states. + * After consuming the entire input string, + * the match is successful iff current_states contains m_end_state. + */ + std::set current_states; + epsilon_closure(m_start_state, current_states); + for (unsigned i = 0; i < input.length(); ++i) { + char A = input.at(i); + std::set next_states; + for (std::set::iterator it = current_states.begin(); + it != current_states.end(); ++it) { + unsigned S = *it; + // check transition_map + if (transition_map[S].find(A) != transition_map[S].end()) { + next_states.insert(transition_map[S][A]); } + } - // take e-closure over next_states to compute the actual next_states - std::set epsilon_next_states; - for (std::set::iterator it = next_states.begin(); it != next_states.end(); ++it) { - unsigned S = *it; - std::set closure; - epsilon_closure(S, closure); - epsilon_next_states.insert(closure.begin(), closure.end()); - } - current_states = epsilon_next_states; - } - if (current_states.find(m_end_state) != current_states.end()) { - return true; - } else { - return false; + // take e-closure over next_states to compute the actual next_states + std::set epsilon_next_states; + for (std::set::iterator it = next_states.begin(); it != next_states.end(); ++it) { + unsigned S = *it; + std::set closure; + epsilon_closure(S, closure); + epsilon_next_states.insert(closure.begin(), closure.end()); } + current_states = epsilon_next_states; } -}; + if (current_states.find(m_end_state) != current_states.end()) { + return true; + } else { + return false; + } +} + br_status str_rewriter::mk_str_Concat(expr * arg0, expr * arg1, expr_ref & result) { TRACE("t_str_rw", tout << "rewrite (Concat " << mk_pp(arg0, m()) << " " << mk_pp(arg1, m()) << ")" << std::endl;); diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index 862fc3e7e..c64d086f9 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -21,6 +21,8 @@ Notes: #include"arith_decl_plugin.h" #include"rewriter_types.h" #include"params.h" +#include +#include class str_rewriter { str_util m_strutil; @@ -61,3 +63,48 @@ public: bool reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change); }; + +class nfa { +protected: + bool m_valid; + unsigned m_next_id; + + unsigned next_id() { + unsigned retval = m_next_id; + ++m_next_id; + return retval; + } + + unsigned m_start_state; + unsigned m_end_state; + + std::map > transition_map; + std::map > epsilon_map; + + void make_transition(unsigned start, char symbol, unsigned end) { + transition_map[start][symbol] = end; + } + + void make_epsilon_move(unsigned start, unsigned end) { + epsilon_map[start].insert(end); + } + + // Convert a regular expression to an e-NFA using Thompson's construction + void convert_re(expr * e, unsigned & start, unsigned & end, str_util & m_strutil); + +public: + nfa(str_util & m_strutil, expr * e) +: m_valid(true), m_next_id(0), m_start_state(0), m_end_state(0) { + convert_re(e, m_start_state, m_end_state, m_strutil); + } + + nfa() : m_valid(false), m_next_id(0), m_start_state(0), m_end_state(0) {} + + bool is_valid() const { + return m_valid; + } + + void epsilon_closure(unsigned start, std::set & closure); + + bool matches(std::string input); +}; diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 087bf6ad0..d249649c7 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -35,6 +35,7 @@ theory_str::theory_str(ast_manager & m): opt_LCMUnrollStep(2), opt_NoQuickReturn_IntegerTheory(false), opt_DisableIntegerTheoryIntegration(false), + opt_NoCheckRegexIn(false), /* Internal setup */ search_started(false), m_autil(m), @@ -1643,7 +1644,14 @@ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { check_contain_in_new_eq(lhs, rhs); } - // TODO regexInBoolMap + if (!regex_in_bool_map.empty()) { + if (opt_NoCheckRegexIn) { + TRACE("t_str", tout << "WARNING: skipping check_regex_in()" << std::endl;); + } else { + TRACE("t_str", tout << "checking regex consistency" << std::endl;); + check_regex_in(lhs, rhs); + } + } // okay, all checks here passed return true; @@ -5213,6 +5221,55 @@ void theory_str::check_concat_len_in_eqc(expr * concat) { } while (eqc_it != eqc_base); } +void theory_str::check_regex_in(expr * nn1, expr * nn2) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + expr_ref_vector eqNodeSet(m); + expr * constStr = collect_eq_nodes(nn1, eqNodeSet); + + if (constStr == NULL) { + return; + } else { + expr_ref_vector::iterator itor = eqNodeSet.begin(); + for (; itor != eqNodeSet.end(); itor++) { + if (regex_in_var_reg_str_map.find(*itor) != regex_in_var_reg_str_map.end()) { + std::set::iterator strItor = regex_in_var_reg_str_map[*itor].begin(); + for (; strItor != regex_in_var_reg_str_map[*itor].end(); strItor++) { + std::string regStr = *strItor; + std::string constStrValue = m_strutil.get_string_constant_value(constStr); + std::pair key1 = std::make_pair(*itor, regStr); + if (regex_in_bool_map.find(key1) != regex_in_bool_map.end()) { + expr * boolVar = regex_in_bool_map[key1]; // actually the RegexIn term + app * a_regexIn = to_app(boolVar); + expr * regexTerm = a_regexIn->get_arg(1); + + if (regex_nfa_cache.find(regexTerm) == regex_nfa_cache.end()) { + TRACE("t_str_detail", tout << "regex_nfa_cache: cache miss" << std::endl;); + regex_nfa_cache[regexTerm] = nfa(m_strutil, regexTerm); + } else { + TRACE("t_str_detail", tout << "regex_nfa_cache: cache hit" << std::endl;); + } + + nfa regexNFA = regex_nfa_cache[regexTerm]; + ENSURE(regexNFA.is_valid()); + bool matchRes = regexNFA.matches(constStrValue); + + TRACE("t_str_detail", tout << mk_pp(*itor, m) << " in " << regStr << " : " << (matchRes ? "yes" : "no") << std::endl;); + + expr_ref implyL(ctx.mk_eq_atom(*itor, constStr), m); + if (matchRes) { + assert_implication(implyL, boolVar); + } else { + assert_implication(implyL, m.mk_not(boolVar)); + } + } + } + } + } + } +} + /* * strArgmt::solve_concat_eq_str() * Solve concatenations of the form: diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 06a72c3e2..8acdb4f02 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -25,6 +25,7 @@ Revision History: #include"arith_decl_plugin.h" #include #include +#include"str_rewriter.h" namespace smt { @@ -137,6 +138,14 @@ namespace smt { */ bool opt_DisableIntegerTheoryIntegration; + /* + * If NoCheckRegexIn is set to true, + * an expensive regular expression membership test is skipped. + * This option is for experiment purposes only and should be set to 'false' + * as skipping this check impacts the correctness of the solver. + */ + bool opt_NoCheckRegexIn; + bool search_started; arith_util m_autil; str_util m_strutil; @@ -221,6 +230,8 @@ namespace smt { std::map, expr*> regex_in_bool_map; std::map > regex_in_var_reg_str_map; + std::map regex_nfa_cache; // Regex term --> NFA + char * char_set; std::map charSetLookupTable; int charSetSize; @@ -423,6 +434,7 @@ namespace smt { expr * gen_unroll_assign(expr * var, std::string lcmStr, expr * testerVar, int l, int h); void reduce_virtual_regex_in(expr * var, expr * regex, expr_ref_vector & items); std::string get_std_regex_str(expr * regex); + void check_regex_in(expr * nn1, expr * nn2); void dump_assignments(); void initialize_charset(); From 54d7e4bbb59f7b255bc70c11dce9acdef6daf30b Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 17 Aug 2016 21:12:19 -0400 Subject: [PATCH 187/562] remove the option to bypass check_regex_in in theory_str --- src/smt/theory_str.cpp | 9 ++------- src/smt/theory_str.h | 8 -------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index d249649c7..ae002f979 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -35,7 +35,6 @@ theory_str::theory_str(ast_manager & m): opt_LCMUnrollStep(2), opt_NoQuickReturn_IntegerTheory(false), opt_DisableIntegerTheoryIntegration(false), - opt_NoCheckRegexIn(false), /* Internal setup */ search_started(false), m_autil(m), @@ -1645,12 +1644,8 @@ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { } if (!regex_in_bool_map.empty()) { - if (opt_NoCheckRegexIn) { - TRACE("t_str", tout << "WARNING: skipping check_regex_in()" << std::endl;); - } else { - TRACE("t_str", tout << "checking regex consistency" << std::endl;); - check_regex_in(lhs, rhs); - } + TRACE("t_str", tout << "checking regex consistency" << std::endl;); + check_regex_in(lhs, rhs); } // okay, all checks here passed diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 8acdb4f02..527753b73 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -138,14 +138,6 @@ namespace smt { */ bool opt_DisableIntegerTheoryIntegration; - /* - * If NoCheckRegexIn is set to true, - * an expensive regular expression membership test is skipped. - * This option is for experiment purposes only and should be set to 'false' - * as skipping this check impacts the correctness of the solver. - */ - bool opt_NoCheckRegexIn; - bool search_started; arith_util m_autil; str_util m_strutil; From 3c8b833eebd7ac8c946958720d3f95c14840355a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 18 Aug 2016 17:03:32 -0400 Subject: [PATCH 188/562] fix expression dereference error in theory_str::gen_assign_unroll_Str2Reg --- src/smt/theory_str.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ae002f979..05cbe8803 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -7772,10 +7772,11 @@ expr * theory_str::gen_assign_unroll_Str2Reg(expr * n, std::set & unrolls if (canHaveNonEmptyAssign) { return gen_unroll_conditional_options(n, unrolls, lcmStr); } else { - expr * implyL = mk_and(litems); - expr * implyR = ctx.mk_eq_atom(n, m_strutil.mk_string("")); + expr_ref implyL(mk_and(litems), mgr); + expr_ref implyR(ctx.mk_eq_atom(n, m_strutil.mk_string("")), mgr); // want to return (implyL -> implyR) - return mgr.mk_or(mgr.mk_not(implyL), implyR); + expr * final_axiom = rewrite_implication(implyL, implyR); + return final_axiom; } } From 8598a48e3b459c0b4e2ce491b40f8438211a0e07 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 18 Aug 2016 19:14:50 -0400 Subject: [PATCH 189/562] fix weird Contains rewriter behaviour in theory_str --- src/smt/theory_str.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 05cbe8803..360bfa26a 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1130,6 +1130,22 @@ void theory_str::instantiate_axiom_Contains(enode * e) { return; } axiomatized_terms.insert(ex); + + // quick path, because this is necessary due to rewriter behaviour + // (at minimum it should fix z3str/concat-006.smt2 + // TODO: see if it's necessary for other such terms + if (m_strutil.is_string(ex->get_arg(0)) && m_strutil.is_string(ex->get_arg(1))) { + TRACE("t_str_detail", tout << "eval constant Contains term " << mk_pp(ex, m) << std::endl;); + std::string haystackStr = m_strutil.get_string_constant_value(ex->get_arg(0)); + std::string needleStr = m_strutil.get_string_constant_value(ex->get_arg(1)); + if (haystackStr.find(needleStr) != std::string::npos) { + assert_axiom(ex); + } else { + assert_axiom(m.mk_not(ex)); + } + return; + } + { // register Contains() expr * str = ex->get_arg(0); expr * substr = ex->get_arg(1); From 481e97a274c994d66f9c34bd615c8b4af214439d Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 19 Aug 2016 22:53:36 -0400 Subject: [PATCH 190/562] propagate early in theory_str to set up contains/regex maps this fixes an unsat-as-sat error in a regex test and flips around some timeouts so more work will be required to track this down --- src/smt/theory_str.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 360bfa26a..c781cae04 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -6052,6 +6052,10 @@ void theory_str::init_search_eh() { } */ + // this might be cheating but we need to make sure that certain maps are populated + // before the first call to new_eq_eh() + propagate(); + TRACE("t_str", tout << "search started" << std::endl;); search_started = true; } From 1a75781a3ceb28f7dad6be75140c5b9685c935a7 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 20 Aug 2016 23:09:08 -0400 Subject: [PATCH 191/562] add experimental option to defer new_eq_check to final_check in theory_str --- src/smt/theory_str.cpp | 87 +++++++++++++++++++++++++++++++++++++----- src/smt/theory_str.h | 9 ++++- 2 files changed, 86 insertions(+), 10 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index c781cae04..f53eefb6d 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -35,6 +35,7 @@ theory_str::theory_str(ast_manager & m): opt_LCMUnrollStep(2), opt_NoQuickReturn_IntegerTheory(false), opt_DisableIntegerTheoryIntegration(false), + opt_DeferEQCConsistencyCheck(true), /* Internal setup */ search_started(false), m_autil(m), @@ -1613,8 +1614,11 @@ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { UNREACHABLE(); } - check_concat_len_in_eqc(lhs); - check_concat_len_in_eqc(rhs); + // skip this check if we defer consistency checking, as we can do it for every EQC in final check + if (!opt_DeferEQCConsistencyCheck) { + check_concat_len_in_eqc(lhs); + check_concat_len_in_eqc(rhs); + } // Now we iterate over all pairs of terms in the (shared) eqc // and check whether we can show that any pair of distinct terms @@ -5197,7 +5201,7 @@ bool theory_str::check_length_eq_var_concat(expr * n1, expr * n2) { else { return check_length_var_var(n1, n2); } - return 0; + return true; } // returns false if an inconsistency is detected, or true if no inconsistencies were found @@ -5214,22 +5218,31 @@ bool theory_str::check_length_consistency(expr * n1, expr * n2) { // n1 and n2 are vars or concats return check_length_eq_var_concat(n1, n2); } - return 0; + return true; } -void theory_str::check_concat_len_in_eqc(expr * concat) { +// Modified signature: returns true if nothing was learned, or false if at least one axiom was asserted. +// (This is used for deferred consistency checking) +bool theory_str::check_concat_len_in_eqc(expr * concat) { context & ctx = get_context(); + bool no_assertions = true; + enode * eqc_base = ctx.get_enode(concat); enode * eqc_it = eqc_base; do { app * eqc_n = eqc_it->get_owner(); if (is_concat(eqc_n)) { rational unused; - infer_len_concat(eqc_n, unused); + bool status = infer_len_concat(eqc_n, unused); + if (status) { + no_assertions = false; + } } eqc_it = eqc_it->get_next(); } while (eqc_it != eqc_base); + + return no_assertions; } void theory_str::check_regex_in(expr * nn1, expr * nn2) { @@ -5730,9 +5743,13 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { } } - // newEqCheck() -- check consistency wrt. existing equivalence classes - if (!new_eq_check(lhs, rhs)) { - return; + if (opt_DeferEQCConsistencyCheck) { + TRACE("t_str_detail", tout << "opt_DeferEQCConsistencyCheck is set; deferring new_eq_check call" << std::endl;); + } else { + // newEqCheck() -- check consistency wrt. existing equivalence classes + if (!new_eq_check(lhs, rhs)) { + return; + } } // BEGIN new_eq_handler() in strTheory @@ -7040,6 +7057,58 @@ final_check_status theory_str::final_check_eh() { TRACE("t_str", tout << "final check" << std::endl;); TRACE("t_str_dump_assign", dump_assignments();); + if (opt_DeferEQCConsistencyCheck) { + TRACE("t_str_detail", tout << "performing deferred EQC consistency check" << std::endl;); + std::set eqc_roots; + for (ptr_vector::const_iterator it = ctx.begin_enodes(); it != ctx.end_enodes(); ++it) { + enode * e = *it; + enode * root = e->get_root(); + eqc_roots.insert(root); + } + + bool found_inconsistency = false; + + for (std::set::iterator it = eqc_roots.begin(); it != eqc_roots.end(); ++it) { + enode * e = *it; + app * a = e->get_owner(); + if (!(is_sort_of(m.get_sort(a), m_strutil.get_fid(), STRING_SORT))) { + TRACE("t_str_detail", tout << "EQC root " << mk_pp(a, m) << " not a string term; skipping" << std::endl;); + } else { + TRACE("t_str_detail", tout << "EQC root " << mk_pp(a, m) << " is a string term. Checking this EQC" << std::endl;); + // first call check_concat_len_in_eqc() on each member of the eqc + enode * e_it = e; + enode * e_root = e_it; + do { + bool status = check_concat_len_in_eqc(e_it->get_owner()); + if (!status) { + TRACE("t_str_detail", tout << "concat-len check asserted an axiom on " << mk_pp(e_it->get_owner(), m) << std::endl;); + found_inconsistency = true; + } + e_it = e_it->get_next(); + } while (e_it != e_root); + + // now grab any two distinct elements from the EQC and call new_eq_check() on them + enode * e1 = e; + enode * e2 = e1->get_next(); + if (e1 != e2) { + TRACE("t_str_detail", tout << "deferred new_eq_check() over EQC of " << mk_pp(e1->get_owner(), m) << " and " << mk_pp(e2->get_owner(), m) << std::endl;); + bool result = new_eq_check(e1->get_owner(), e2->get_owner()); + if (!result) { + TRACE("t_str_detail", tout << "new_eq_check found inconsistencies" << std::endl;); + found_inconsistency = true; + } + } + } + } + + if (found_inconsistency) { + TRACE("t_str", tout << "Found inconsistency in final check! Returning to search." << std::endl;); + return FC_CONTINUE; + } else { + TRACE("t_str", tout << "Deferred consistency check passed. Continuing in final check." << std::endl;); + } + } + // run dependence analysis to find free string variables std::map varAppearInAssign; std::map freeVar_map; diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 527753b73..60a1d70e2 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -138,6 +138,13 @@ namespace smt { */ bool opt_DisableIntegerTheoryIntegration; + /* + * If DeferEQCConsistencyCheck is set to true, + * expensive calls to new_eq_check() will be deferred until final check, + * at which time the consistency of *all* string equivalence classes will be validated. + */ + bool opt_DeferEQCConsistencyCheck; + bool search_started; arith_util m_autil; str_util m_strutil; @@ -334,7 +341,7 @@ namespace smt { bool can_two_nodes_eq(expr * n1, expr * n2); bool can_concat_eq_str(expr * concat, std::string str); bool can_concat_eq_concat(expr * concat1, expr * concat2); - void check_concat_len_in_eqc(expr * concat); + bool check_concat_len_in_eqc(expr * concat); bool check_length_consistency(expr * n1, expr * n2); bool check_length_const_string(expr * n1, expr * constStr); bool check_length_eq_var_concat(expr * n1, expr * n2); From 7b3203b48e2f53775e08921acb8f6792f4579623 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 21 Aug 2016 00:30:29 -0400 Subject: [PATCH 192/562] disable aggressive length/value testing in theory_str, it seems to be detrimental --- src/smt/theory_str.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index f53eefb6d..a0b06bcbf 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -28,8 +28,8 @@ namespace smt { theory_str::theory_str(ast_manager & m): theory(m.mk_family_id("str")), /* Options */ - opt_AggressiveLengthTesting(true), - opt_AggressiveValueTesting(true), + opt_AggressiveLengthTesting(false), + opt_AggressiveValueTesting(false), opt_EagerStringConstantLengthAssertions(true), opt_VerifyFinalCheckProgress(true), opt_LCMUnrollStep(2), From 2a199294a1bfd8511caa0ee0b421c2d2f65de1da Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 21 Aug 2016 00:43:00 -0400 Subject: [PATCH 193/562] remove incorrect null pointer check from theory_str::gen_len_val_options_for_free_var everything that calls this method knows that it can legally return null --- src/smt/theory_str.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index a0b06bcbf..28420de26 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -8234,7 +8234,6 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe TRACE("t_str", tout << "length is fixed; generating models for free var" << std::endl;); // length is fixed expr * valueAssert = gen_free_var_options(freeVar, effectiveLenInd, effectiveLenIndiStr, NULL, ""); - SASSERT(valueAssert != NULL); return valueAssert; } } // fVarLenCountMap.find(...) From 89d5f4ffb4a8d6e4b159c6c919964cffcb12754b Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 21 Aug 2016 21:37:46 -0400 Subject: [PATCH 194/562] add compute_contains check to theory_str this may cause a crash in indexof-002.smt2 but I cannot reproduce it --- src/smt/theory_str.cpp | 341 +++++++++++++++++++++++++++++++++++++++-- src/smt/theory_str.h | 14 ++ 2 files changed, 346 insertions(+), 9 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 28420de26..90b0992d6 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -21,6 +21,7 @@ Revision History: #include"ast_pp.h" #include"ast_ll_pp.h" #include +#include #include"theory_arith.h" namespace smt { @@ -3102,15 +3103,14 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { int option = 0; int pos = 1; - expr_ref temp1_strAst(mk_concat(temp1, strAst), mgr); // TODO assert concat axioms? + expr_ref temp1_strAst(mk_concat(temp1, strAst), mgr); // m cuts y if (can_two_nodes_eq(y, temp1_strAst)) { if (!avoidLoopCut || !has_self_cut(m, y)) { // break down option 2-1 - // TODO or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); - expr_ref x_temp1(mk_concat(x, temp1), mgr); // TODO assert concat axioms? + expr_ref x_temp1(mk_concat(x, temp1), mgr); and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(m, x_temp1)); and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(y, temp1_strAst)); @@ -3131,7 +3131,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { std::string part1Str = strValue.substr(0, i); std::string part2Str = strValue.substr(i, strValue.size() - i); expr_ref prefixStr(m_strutil.mk_string(part1Str), mgr); - expr_ref x_concat(mk_concat(m, prefixStr), mgr); // TODO concat axioms? + expr_ref x_concat(mk_concat(m, prefixStr), mgr); expr_ref cropStr(m_strutil.mk_string(part2Str), mgr); if (can_two_nodes_eq(x, x_concat) && can_two_nodes_eq(y, cropStr)) { // break down option 2-2 @@ -4866,6 +4866,332 @@ void theory_str::check_contain_in_new_eq(expr * n1, expr * n2) { } } +expr * theory_str::dealias_node(expr * node, std::map & varAliasMap, std::map & concatAliasMap) { + if (variable_set.find(node) != variable_set.end()) { + return get_alias_index_ast(varAliasMap, node); + } else if (is_concat(to_app(node))) { + return get_alias_index_ast(concatAliasMap, node); + } + return node; +} + +void theory_str::get_grounded_concats(expr* node, std::map & varAliasMap, + std::map & concatAliasMap, std::map & varConstMap, + std::map & concatConstMap, std::map > & varEqConcatMap, + std::map, std::set > > & groundedMap) { + if (is_Unroll(to_app(node))) { + return; + } + // ************************************************** + // first deAlias the node if it is a var or concat + // ************************************************** + node = dealias_node(node, varAliasMap, concatAliasMap); + + if (groundedMap.find(node) != groundedMap.end()) { + return; + } + + // haven't computed grounded concats for "node" (de-aliased) + // --------------------------------------------------------- + + context & ctx = get_context(); + ast_manager & m = get_manager(); + + // const strings: node is de-aliased + if (m_strutil.is_string(node)) { + std::vector concatNodes; + concatNodes.push_back(node); + groundedMap[node][concatNodes].clear(); // no condition + } + // Concat functions + else if (is_concat(to_app(node))) { + // if "node" equals to a constant string, thenjust push the constant into the concat vector + // Again "node" has been de-aliased at the very beginning + if (concatConstMap.find(node) != concatConstMap.end()) { + std::vector concatNodes; + concatNodes.push_back(concatConstMap[node]); + groundedMap[node][concatNodes].clear(); + groundedMap[node][concatNodes].insert(ctx.mk_eq_atom(node, concatConstMap[node])); + } + // node doesn't have eq constant value. Process its children. + else { + // merge arg0 and arg1 + expr * arg0 = to_app(node)->get_arg(0); + expr * arg1 = to_app(node)->get_arg(1); + expr * arg0DeAlias = dealias_node(arg0, varAliasMap, concatAliasMap); + expr * arg1DeAlias = dealias_node(arg1, varAliasMap, concatAliasMap); + get_grounded_concats(arg0DeAlias, varAliasMap, concatAliasMap, varConstMap, concatConstMap, varEqConcatMap, groundedMap); + get_grounded_concats(arg1DeAlias, varAliasMap, concatAliasMap, varConstMap, concatConstMap, varEqConcatMap, groundedMap); + + std::map, std::set >::iterator arg0_grdItor = groundedMap[arg0DeAlias].begin(); + std::map, std::set >::iterator arg1_grdItor; + for (; arg0_grdItor != groundedMap[arg0DeAlias].end(); arg0_grdItor++) { + arg1_grdItor = groundedMap[arg1DeAlias].begin(); + for (; arg1_grdItor != groundedMap[arg1DeAlias].end(); arg1_grdItor++) { + std::vector ndVec; + ndVec.insert(ndVec.end(), arg0_grdItor->first.begin(), arg0_grdItor->first.end()); + int arg0VecSize = arg0_grdItor->first.size(); + int arg1VecSize = arg1_grdItor->first.size(); + if (arg0VecSize > 0 && arg1VecSize > 0 && m_strutil.is_string(arg0_grdItor->first[arg0VecSize - 1]) && m_strutil.is_string(arg1_grdItor->first[0])) { + ndVec.pop_back(); + ndVec.push_back(mk_concat(arg0_grdItor->first[arg0VecSize - 1], arg1_grdItor->first[0])); + for (int i = 1; i < arg1VecSize; i++) { + ndVec.push_back(arg1_grdItor->first[i]); + } + } else { + ndVec.insert(ndVec.end(), arg1_grdItor->first.begin(), arg1_grdItor->first.end()); + } + // only insert if we don't know "node = concat(ndVec)" since one set of condition leads to this is enough + if (groundedMap[node].find(ndVec) == groundedMap[node].end()) { + groundedMap[node][ndVec]; + if (arg0 != arg0DeAlias) { + groundedMap[node][ndVec].insert(ctx.mk_eq_atom(arg0, arg0DeAlias)); + } + groundedMap[node][ndVec].insert(arg0_grdItor->second.begin(), arg0_grdItor->second.end()); + + if (arg1 != arg1DeAlias) { + groundedMap[node][ndVec].insert(ctx.mk_eq_atom(arg1, arg1DeAlias)); + } + groundedMap[node][ndVec].insert(arg1_grdItor->second.begin(), arg1_grdItor->second.end()); + } + } + } + } + } + // string variables + else if (variable_set.find(node) != variable_set.end()) { + // deAliasedVar = Constant + if (varConstMap.find(node) != varConstMap.end()) { + std::vector concatNodes; + concatNodes.push_back(varConstMap[node]); + groundedMap[node][concatNodes].clear(); + groundedMap[node][concatNodes].insert(ctx.mk_eq_atom(node, varConstMap[node])); + } + // deAliasedVar = someConcat + else if (varEqConcatMap.find(node) != varEqConcatMap.end()) { + expr * eqConcat = varEqConcatMap[node].begin()->first; + expr * deAliasedEqConcat = dealias_node(eqConcat, varAliasMap, concatAliasMap); + get_grounded_concats(deAliasedEqConcat, varAliasMap, concatAliasMap, varConstMap, concatConstMap, varEqConcatMap, groundedMap); + + std::map, std::set >::iterator grdItor = groundedMap[deAliasedEqConcat].begin(); + for (; grdItor != groundedMap[deAliasedEqConcat].end(); grdItor++) { + std::vector ndVec; + ndVec.insert(ndVec.end(), grdItor->first.begin(), grdItor->first.end()); + // only insert if we don't know "node = concat(ndVec)" since one set of condition leads to this is enough + if (groundedMap[node].find(ndVec) == groundedMap[node].end()) { + // condition: node = deAliasedEqConcat + groundedMap[node][ndVec].insert(ctx.mk_eq_atom(node, deAliasedEqConcat)); + // appending conditions for "deAliasedEqConcat = CONCAT(ndVec)" + groundedMap[node][ndVec].insert(grdItor->second.begin(), grdItor->second.end()); + } + } + } + // node (has been de-aliased) != constant && node (has been de-aliased) != any concat + // just push in the deAliasedVar + else { + std::vector concatNodes; + concatNodes.push_back(node); + groundedMap[node][concatNodes]; // TODO ??? + } + } +} + +void theory_str::print_grounded_concat(expr * node, std::map, std::set > > & groundedMap) { + ast_manager & m = get_manager(); + TRACE("t_str_detail", tout << mk_pp(node, m) << std::endl;); + if (groundedMap.find(node) != groundedMap.end()) { + std::map, std::set >::iterator itor = groundedMap[node].begin(); + for (; itor != groundedMap[node].end(); ++itor) { + TRACE("t_str_detail", + tout << "\t[grounded] "; + std::vector::const_iterator vIt = itor->first.begin(); + for (; vIt != itor->first.end(); ++vIt) { + tout << mk_pp(*vIt, m) << ", "; + } + tout << std::endl; + tout << "\t[condition] "; + std::set::iterator sIt = itor->second.begin(); + for (; sIt != itor->second.end(); sIt++) { + tout << mk_pp(*sIt, m) << ", "; + } + tout << std::endl; + ); + } + } else { + TRACE("t_str_detail", tout << "not found" << std::endl;); + } +} + +bool theory_str::is_partial_in_grounded_concat(const std::vector & strVec, const std::vector & subStrVec) { + int strCnt = strVec.size(); + int subStrCnt = subStrVec.size(); + + if (strCnt == 0 || subStrCnt == 0) { + return false; + } + + // The assumption is that all consecutive constant strings are merged into one node + if (strCnt < subStrCnt) { + return false; + } + + if (subStrCnt == 1) { + if (m_strutil.is_string(subStrVec[0])) { + std::string subStrVal = m_strutil.get_string_constant_value(subStrVec[0]); + for (int i = 0; i < strCnt; i++) { + if (m_strutil.is_string(strVec[i])) { + std::string strVal = m_strutil.get_string_constant_value(strVec[i]); + if (strVal.find(subStrVal) != std::string::npos) { + return true; + } + } + } + } else { + for (int i = 0; i < strCnt; i++) { + if (strVec[i] == subStrVec[0]) { + return true; + } + } + } + return false; + } else { + for (int i = 0; i <= (strCnt - subStrCnt); i++) { + // The first node in subStrVect should be + // * constant: a suffix of a note in strVec[i] + // * variable: + bool firstNodesOK = true; + if (m_strutil.is_string(subStrVec[0])) { + std::string subStrHeadVal = m_strutil.get_string_constant_value(subStrVec[0]); + if (m_strutil.is_string(strVec[i])) { + std::string strHeadVal = m_strutil.get_string_constant_value(strVec[i]); + if (strHeadVal.size() >= subStrHeadVal.size()) { + std::string suffix = strHeadVal.substr(strHeadVal.size() - subStrHeadVal.size(), subStrHeadVal.size()); + if (suffix != subStrHeadVal) { + firstNodesOK = false; + } + } else { + firstNodesOK = false; + } + } else { + if (subStrVec[0] != strVec[i]) { + firstNodesOK = false; + } + } + } + if (!firstNodesOK) { + continue; + } + + // middle nodes + bool midNodesOK = true; + for (int j = 1; j < subStrCnt - 1; j++) { + if (subStrVec[j] != strVec[i + j]) { + midNodesOK = false; + break; + } + } + if (!midNodesOK) { + continue; + } + + // tail nodes + int tailIdx = i + subStrCnt - 1; + if (m_strutil.is_string(subStrVec[subStrCnt - 1])) { + std::string subStrTailVal = m_strutil.get_string_constant_value(subStrVec[subStrCnt - 1]); + if (m_strutil.is_string(strVec[tailIdx])) { + std::string strTailVal = m_strutil.get_string_constant_value(strVec[tailIdx]); + if (strTailVal.size() >= subStrTailVal.size()) { + std::string prefix = strTailVal.substr(0, subStrTailVal.size()); + if (prefix == subStrTailVal) { + return true; + } else { + continue; + } + } else { + continue; + } + } + } else { + if (subStrVec[subStrCnt - 1] == strVec[tailIdx]) { + return true; + } else { + continue; + } + } + } + return false; + } +} + +void theory_str::check_subsequence(expr* str, expr* strDeAlias, expr* subStr, expr* subStrDeAlias, expr* boolVar, + std::map, std::set > > & groundedMap) { + + context & ctx = get_context(); + ast_manager & m = get_manager(); + std::map, std::set >::iterator itorStr = groundedMap[strDeAlias].begin(); + std::map, std::set >::iterator itorSubStr; + for (; itorStr != groundedMap[strDeAlias].end(); itorStr++) { + itorSubStr = groundedMap[subStrDeAlias].begin(); + for (; itorSubStr != groundedMap[subStrDeAlias].end(); itorSubStr++) { + bool contain = is_partial_in_grounded_concat(itorStr->first, itorSubStr->first); + if (contain) { + expr_ref_vector litems(m); + if (str != strDeAlias) { + litems.push_back(ctx.mk_eq_atom(str, strDeAlias)); + } + if (subStr != subStrDeAlias) { + litems.push_back(ctx.mk_eq_atom(subStr, subStrDeAlias)); + } + + //litems.insert(itorStr->second.begin(), itorStr->second.end()); + //litems.insert(itorSubStr->second.begin(), itorSubStr->second.end()); + for (std::set::const_iterator i1 = itorStr->second.begin(); + i1 != itorStr->second.end(); ++i1) { + litems.push_back(*i1); + } + for (std::set::const_iterator i1 = itorSubStr->second.begin(); + i1 != itorSubStr->second.end(); ++i1) { + litems.push_back(*i1); + } + + expr_ref implyR(boolVar, m); + + if (litems.empty()) { + assert_axiom(implyR); + } else { + expr_ref implyL(mk_and(litems), m); + assert_implication(implyL, implyR); + } + + } + } + } +} + +void theory_str::compute_contains(std::map & varAliasMap, + std::map & concatAliasMap, std::map & varConstMap, + std::map & concatConstMap, std::map > & varEqConcatMap) { + std::map, std::set > > groundedMap; + theory_str_contain_pair_bool_map_t::iterator containItor = contain_pair_bool_map.begin(); + for (; containItor != contain_pair_bool_map.end(); containItor++) { + expr* containBoolVar = containItor->get_value(); + expr* str = containItor->get_key1(); + expr* subStr = containItor->get_key2(); + + expr* strDeAlias = dealias_node(str, varAliasMap, concatAliasMap); + expr* subStrDeAlias = dealias_node(subStr, varAliasMap, concatAliasMap); + + get_grounded_concats(strDeAlias, varAliasMap, concatAliasMap, varConstMap, concatConstMap, varEqConcatMap, groundedMap); + get_grounded_concats(subStrDeAlias, varAliasMap, concatAliasMap, varConstMap, concatConstMap, varEqConcatMap, groundedMap); + + // debugging + print_grounded_concat(strDeAlias, groundedMap); + print_grounded_concat(subStrDeAlias, groundedMap); + + check_subsequence(str, strDeAlias, subStr, subStrDeAlias, containBoolVar, groundedMap); + } +} + bool theory_str::can_concat_eq_str(expr * concat, std::string str) { // TODO this method could use some traces and debugging info int strLen = str.length(); @@ -6668,12 +6994,9 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map 0) { - NOT_IMPLEMENTED_YET(); - compute_contains(aliasIndexMap, concats_eq_Index_map, var_eq_constStr_map, concat_eq_constStr_map, var_eq_concat_map); + if (!contain_pair_bool_map.empty()) { + compute_contains(aliasIndexMap, concats_eq_index_map, var_eq_constStr_map, concat_eq_constStr_map, var_eq_concat_map); } - */ // step 4: dependence analysis diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 60a1d70e2..ba132a579 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -25,6 +25,7 @@ Revision History: #include"arith_decl_plugin.h" #include #include +#include #include"str_rewriter.h" namespace smt { @@ -353,6 +354,19 @@ namespace smt { void check_contain_by_substr(expr * varNode, expr_ref_vector & willEqClass); void check_contain_by_eq_nodes(expr * n1, expr * n2); bool in_contain_idx_map(expr * n); + // TODO refactor these methods to use expr_ref_vector instead of std::vector + void compute_contains(std::map & varAliasMap, + std::map & concatAliasMap, std::map & varConstMap, + std::map & concatConstMap, std::map > & varEqConcatMap); + expr * dealias_node(expr * node, std::map & varAliasMap, std::map & concatAliasMap); + void get_grounded_concats(expr* node, std::map & varAliasMap, + std::map & concatAliasMap, std::map & varConstMap, + std::map & concatConstMap, std::map > & varEqConcatMap, + std::map, std::set > > & groundedMap); + void print_grounded_concat(expr * node, std::map, std::set > > & groundedMap); + void check_subsequence(expr* str, expr* strDeAlias, expr* subStr, expr* subStrDeAlias, expr* boolVar, + std::map, std::set > > & groundedMap); + bool is_partial_in_grounded_concat(const std::vector & strVec, const std::vector & subStrVec); void get_nodes_in_concat(expr * node, ptr_vector & nodeList); expr * simplify_concat(expr * node); From 5e22bc57c8a484d0891340ab2b095bbc9648cc4a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 31 Aug 2016 19:19:23 -0400 Subject: [PATCH 195/562] theory_str cleanup --- src/smt/theory_str.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 90b0992d6..9c69f9716 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3401,7 +3401,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { std::string part2Str = strValue.substr(i, strValue.size() - i); expr_ref cropStr(m_strutil.mk_string(part1Str), mgr); expr_ref suffixStr(m_strutil.mk_string(part2Str), mgr); - expr_ref y_concat(mk_concat(suffixStr, n), mgr); // TODO concat axioms? + expr_ref y_concat(mk_concat(suffixStr, n), mgr); if (can_two_nodes_eq(x, cropStr) && can_two_nodes_eq(y, y_concat)) { // break down option 3-1 @@ -6435,6 +6435,7 @@ void theory_str::push_scope_eh() { void theory_str::pop_scope_eh(unsigned num_scopes) { sLevel -= num_scopes; TRACE("t_str", tout << "pop " << num_scopes << " to " << sLevel << std::endl;); + TRACE("t_str_dump_assign_on_scope_change", dump_assignments();); std::map >::iterator varItor = cut_var_map.begin(); while (varItor != cut_var_map.end()) { From f9b4f21683c19cf629dbc7f6d49793788c87f696 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 31 Aug 2016 19:22:04 -0400 Subject: [PATCH 196/562] add rewrite for theory_str rewriter RegexPlus fixes regex-013.smt2 --- src/ast/rewriter/str_rewriter.cpp | 12 ++++++++++++ src/ast/rewriter/str_rewriter.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index c644ecd46..015898a64 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -425,6 +425,15 @@ br_status str_rewriter::mk_re_RegexIn(expr * str, expr * re, expr_ref & result) return BR_FAILED; } +br_status str_rewriter::mk_re_RegexStar(expr * re, expr_ref & result) { + if (m_strutil.is_re_RegexStar(re)) { + result = re; + return BR_REWRITE_FULL; + } else { + return BR_FAILED; + } +} + br_status str_rewriter::mk_re_RegexPlus(expr * re, expr_ref & result) { /* * Two optimizations are possible if we inspect 're'. @@ -523,6 +532,9 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con case OP_RE_REGEXPLUS: SASSERT(num_args == 1); return mk_re_RegexPlus(args[0], result); + case OP_RE_REGEXSTAR: + SASSERT(num_args == 1); + return mk_re_RegexStar(args[0], result); case OP_RE_REGEXCHARRANGE: SASSERT(num_args == 2); return mk_re_RegexCharRange(args[0], args[1], result); diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index c64d086f9..d147e82e8 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -57,6 +57,7 @@ public: br_status mk_re_Str2Reg(expr * str, expr_ref & result); br_status mk_re_RegexIn(expr * str, expr * re, expr_ref & result); br_status mk_re_RegexPlus(expr * re, expr_ref & result); + br_status mk_re_RegexStar(expr * re, expr_ref & result); br_status mk_re_RegexCharRange(expr * start, expr * end, expr_ref & result); bool reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change); From d3062a8eff28019af492691c6030235ffe18cff1 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 2 Sep 2016 18:23:41 -0400 Subject: [PATCH 197/562] omit out-of-scope length testers from axiom premise in theory_str::gen_len_test_options this fixes a regression in charAt-007.smt2 --- src/smt/theory_str.cpp | 52 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 9c69f9716..7faca9922 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -22,6 +22,7 @@ Revision History: #include"ast_ll_pp.h" #include #include +#include #include"theory_arith.h" namespace smt { @@ -8375,7 +8376,14 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr expr_ref_vector and_items_LHS(m); expr_ref moreAst(m_strutil.mk_string("more"), m); for (int i = 0; i < testerCount; ++i) { - and_items_LHS.push_back(ctx.mk_eq_atom(fvar_lenTester_map[freeVar][i], moreAst)); + expr * indicator = fvar_lenTester_map[freeVar][i]; + if (internal_variable_set.find(indicator) == internal_variable_set.end()) { + TRACE("t_str_detail", tout << "indicator " << mk_pp(indicator, m) << " out of scope; continuing" << std::endl;); + continue; + } else { + TRACE("t_str_detail", tout << "indicator " << mk_pp(indicator, m) << " in scope" << std::endl;); + and_items_LHS.push_back(ctx.mk_eq_atom(indicator, moreAst)); + } } expr_ref assertL(mk_and(and_items_LHS), m); SASSERT(assertL); @@ -8591,6 +8599,12 @@ void theory_str::get_var_in_eqc(expr * n, std::set & varSet) { } while (eqcNode != n); } +bool cmpvarnames(expr * lhs, expr * rhs) { + symbol lhs_name = to_app(lhs)->get_decl()->get_name(); + symbol rhs_name = to_app(rhs)->get_decl()->get_name(); + return lhs_name.str() < rhs_name.str(); +} + void theory_str::process_free_var(std::map & freeVar_map) { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -8664,14 +8678,34 @@ void theory_str::process_free_var(std::map & freeVar_map) { // TODO here's a great place for debugging info - for(std::set::iterator itor1 = leafVarSet.begin(); - itor1 != leafVarSet.end(); ++itor1) { - expr * toAssert = gen_len_val_options_for_free_var(*itor1, NULL, ""); - // gen_len_val_options_for_free_var() can legally return NULL, - // as methods that it calls may assert their own axioms instead. - if (toAssert != NULL) { - assert_axiom(toAssert); - } + // testing: iterate over leafVarSet deterministically + if (false) { + // *** TESTING CODE + std::vector sortedLeafVarSet; + for (std::set::iterator itor1 = leafVarSet.begin(); itor1 != leafVarSet.end(); ++itor1) { + sortedLeafVarSet.push_back(*itor1); + } + std::sort(sortedLeafVarSet.begin(), sortedLeafVarSet.end(), cmpvarnames); + for(std::vector::iterator itor1 = sortedLeafVarSet.begin(); + itor1 != sortedLeafVarSet.end(); ++itor1) { + expr * toAssert = gen_len_val_options_for_free_var(*itor1, NULL, ""); + // gen_len_val_options_for_free_var() can legally return NULL, + // as methods that it calls may assert their own axioms instead. + if (toAssert != NULL) { + assert_axiom(toAssert); + } + } + } else { + // *** CODE FROM BEFORE + for(std::set::iterator itor1 = leafVarSet.begin(); + itor1 != leafVarSet.end(); ++itor1) { + expr * toAssert = gen_len_val_options_for_free_var(*itor1, NULL, ""); + // gen_len_val_options_for_free_var() can legally return NULL, + // as methods that it calls may assert their own axioms instead. + if (toAssert != NULL) { + assert_axiom(toAssert); + } + } } for (std::map >::iterator mItor = aloneVars.begin(); From 2b8f165cc47e197dfc7a6aef0849c2af0c067018 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 2 Sep 2016 19:04:20 -0400 Subject: [PATCH 198/562] patch UNSAT to UNKNOWN in cmd_context for theory_str --- src/smt/smt_context.cpp | 20 ++++++++++++++++++++ src/smt/theory_str.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 046e2028e..251cf3b9b 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -37,6 +37,7 @@ Revision History: #include"model_pp.h" #include"ast_smt2_pp.h" #include"ast_translation.h" +#include"theory_str.h" namespace smt { @@ -3086,6 +3087,25 @@ namespace smt { if (r == l_true && get_cancel_flag()) { r = l_undef; } + + // PATCH for theory_str: + // UNSAT + overlapping variables => UNKNOWN + if (r == l_false) { + ptr_vector::iterator it = m_theory_set.begin(); + ptr_vector::iterator end = m_theory_set.end(); + for (; it != end; ++it) { + theory * th = *it; + if (strcmp(th->get_name(), "strings") == 0) { + theory_str * str = (theory_str*)th; + if (str->overlapping_variables_detected()) { + TRACE("t_str", tout << "WARNING: overlapping variables detected, UNSAT changed to UNKNOWN!" << std::endl;); + r = l_undef; + } + break; + } + } + } + return r; } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index ba132a579..1fad18293 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -457,6 +457,8 @@ namespace smt { virtual char const * get_name() const { return "strings"; } virtual void display(std::ostream & out) const; + + bool overlapping_variables_detected() const { return loopDetected; } protected: virtual bool internalize_atom(app * atom, bool gate_ctx); virtual bool internalize_term(app * term); From 347f441517c2f6ca2d44eec295361d376b52baa8 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 2 Sep 2016 20:44:14 -0400 Subject: [PATCH 199/562] add a check for variable scope to theory_str --- src/smt/theory_str.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++ src/smt/theory_str.h | 12 ++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 7faca9922..f19553864 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -38,6 +38,7 @@ theory_str::theory_str(ast_manager & m): opt_NoQuickReturn_IntegerTheory(false), opt_DisableIntegerTheoryIntegration(false), opt_DeferEQCConsistencyCheck(true), + opt_CheckVariableScope(true), /* Internal setup */ search_started(false), m_autil(m), @@ -6433,6 +6434,54 @@ void theory_str::push_scope_eh() { TRACE("t_str_dump_assign_on_scope_change", dump_assignments();); } +void theory_str::recursive_check_variable_scope(expr * ex) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + if (is_app(ex)) { + app * a = to_app(ex); + if (a->get_num_args() == 0) { + // we only care about string variables + sort * s = m.get_sort(ex); + sort * string_sort = m.mk_sort(get_family_id(), STRING_SORT); + if (s != string_sort) { + return; + } + // base case: string constant / var + if (m_strutil.is_string(a)) { + return; + } else { + // assume var + if (variable_set.find(ex) == variable_set.end() + && internal_variable_set.find(ex) == internal_variable_set.end()) { + TRACE("t_str_detail", tout << "WARNING: possible reference to out-of-scope variable " << mk_pp(ex, m) << std::endl;); + } + } + } else { + for (unsigned i = 0; i < a->get_num_args(); ++i) { + recursive_check_variable_scope(a->get_arg(i)); + } + } + } +} + +void theory_str::check_variable_scope() { + if (!opt_CheckVariableScope) { + return; + } + TRACE("t_str_detail", tout << "checking scopes of variables in the current assignment" << std::endl;); + + context & ctx = get_context(); + ast_manager & m = get_manager(); + + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { + expr * ex = *i; + recursive_check_variable_scope(ex); + } +} + void theory_str::pop_scope_eh(unsigned num_scopes) { sLevel -= num_scopes; TRACE("t_str", tout << "pop " << num_scopes << " to " << sLevel << std::endl;); @@ -6487,6 +6536,8 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { m_basicstr_axiom_todo = new_m_basicstr; theory::pop_scope_eh(num_scopes); + + check_variable_scope(); } void theory_str::dump_assignments() { @@ -7381,6 +7432,7 @@ final_check_status theory_str::final_check_eh() { TRACE("t_str", tout << "final check" << std::endl;); TRACE("t_str_dump_assign", dump_assignments();); + check_variable_scope(); if (opt_DeferEQCConsistencyCheck) { TRACE("t_str_detail", tout << "performing deferred EQC consistency check" << std::endl;); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 1fad18293..8a0a2ea81 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -146,6 +146,14 @@ namespace smt { */ bool opt_DeferEQCConsistencyCheck; + /* + * If CheckVariableScope is set to true, + * pop_scope_eh() and final_check_eh() will run extra checks + * to determine whether the current assignment + * contains references to any internal variables that are no longer in scope. + */ + bool opt_CheckVariableScope; + bool search_started; arith_util m_autil; str_util m_strutil; @@ -451,6 +459,10 @@ namespace smt { void dump_assignments(); void initialize_charset(); + + void check_variable_scope(); + void recursive_check_variable_scope(expr * ex); + public: theory_str(ast_manager & m); virtual ~theory_str(); From 7b34efada78d48d2d1d55896662092605d57e5d1 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 4 Sep 2016 18:48:15 -0400 Subject: [PATCH 200/562] add aggressive unroll test option to theory_str --- src/smt/theory_str.cpp | 18 ++++++++++++++++++ src/smt/theory_str.h | 6 ++++++ 2 files changed, 24 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index f19553864..749b9c036 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -32,6 +32,7 @@ theory_str::theory_str(ast_manager & m): /* Options */ opt_AggressiveLengthTesting(false), opt_AggressiveValueTesting(false), + opt_AggressiveUnrollTesting(true), opt_EagerStringConstantLengthAssertions(true), opt_VerifyFinalCheckProgress(true), opt_LCMUnrollStep(2), @@ -8265,6 +8266,7 @@ expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & int tries = unroll_tries_map[var][unrolls].size(); for (int i = 0; i < tries; i++) { + // TODO possibly missing a scope check here expr * tester = unroll_tries_map[var][unrolls][i]; bool testerHasValue = false; expr * testerVal = get_eqc_value(tester, testerHasValue); @@ -8318,6 +8320,10 @@ expr * theory_str::gen_unroll_assign(expr * var, std::string lcmStr, expr * test TRACE("t_str_detail", tout << "entry: var = " << mk_pp(var, mgr) << ", lcmStr = " << lcmStr << ", l = " << l << ", h = " << h << std::endl;); + if (opt_AggressiveUnrollTesting) { + TRACE("t_str_detail", tout << "note: aggressive unroll testing is active" << std::endl;); + } + expr_ref_vector orItems(mgr); expr_ref_vector andItems(mgr); @@ -8325,6 +8331,12 @@ expr * theory_str::gen_unroll_assign(expr * var, std::string lcmStr, expr * test std::string iStr = int_to_string(i); expr_ref testerEqAst(ctx.mk_eq_atom(testerVar, m_strutil.mk_string(iStr)), mgr); TRACE("t_str_detail", tout << "testerEqAst = " << mk_pp(testerEqAst, mgr) << std::endl;); + if (opt_AggressiveUnrollTesting) { + literal l = mk_eq(testerVar, m_strutil.mk_string(iStr), false); + ctx.mark_as_relevant(l); + ctx.force_phase(l); + } + orItems.push_back(testerEqAst); std::string unrollStrInstance = get_unrolled_string(lcmStr, i); @@ -8338,6 +8350,12 @@ expr * theory_str::gen_unroll_assign(expr * var, std::string lcmStr, expr * test } expr_ref testerEqMore(ctx.mk_eq_atom(testerVar, m_strutil.mk_string("more")), mgr); TRACE("t_str_detail", tout << "testerEqMore = " << mk_pp(testerEqMore, mgr) << std::endl;); + if (opt_AggressiveUnrollTesting) { + literal l = mk_eq(testerVar, m_strutil.mk_string("more"), false); + ctx.mark_as_relevant(l); + ctx.force_phase(~l); + } + orItems.push_back(testerEqMore); int nextLowerLenBound = h * lcmStr.length(); expr_ref more2(ctx.mk_eq_atom(testerEqMore, diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 8a0a2ea81..6ce46abb4 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -96,6 +96,12 @@ namespace smt { */ bool opt_AggressiveValueTesting; + /* + * If AggressiveUnrollTesting is true, we manipulate the phase of regex unroll tester equalities + * to prioritize trying concrete unroll counts over choosing the "more" option. + */ + bool opt_AggressiveUnrollTesting; + /* * Setting EagerStringConstantLengthAssertions to true allows some methods, * in particular internalize_term(), to add From c83e39d3b8ab55ab0309bfc160ee951f244979e3 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 5 Sep 2016 17:45:10 -0400 Subject: [PATCH 201/562] fix incorrect axiom in theory_str for Contains check this partially fixes a regression in contains-034.smt2, which now is at least not a SAT-as-UNSAT --- src/smt/theory_str.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 749b9c036..421a45e57 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4773,7 +4773,7 @@ void theory_str::check_contain_by_eq_nodes(expr * n1, expr * n2) { // key1.first = key2.first /\ containPairBoolMap[] // ==> (containPairBoolMap[key1] --> containPairBoolMap[key2]) // ------------ - expr_ref implR(ctx.mk_eq_atom(contain_pair_bool_map[key1], contain_pair_bool_map[key2]), m); + expr_ref implR(rewrite_implication(contain_pair_bool_map[key1], contain_pair_bool_map[key2]), m); assert_implication(mk_and(litems4), implR); } } From 82e07aae8c921eb6a0d552bf3e0604b38241b840 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 8 Sep 2016 19:55:08 -0400 Subject: [PATCH 202/562] disable deferred eqc check in theory_str --- src/smt/theory_str.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 421a45e57..d393f1cdb 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -38,7 +38,7 @@ theory_str::theory_str(ast_manager & m): opt_LCMUnrollStep(2), opt_NoQuickReturn_IntegerTheory(false), opt_DisableIntegerTheoryIntegration(false), - opt_DeferEQCConsistencyCheck(true), + opt_DeferEQCConsistencyCheck(false), opt_CheckVariableScope(true), /* Internal setup */ search_started(false), From 2c5569aa1f0f4881d6354af3ccc4b29284e6e949 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 12 Sep 2016 15:43:58 -0400 Subject: [PATCH 203/562] change cut_var_map to obj_map --- src/smt/theory_str.cpp | 41 +++++++++++++++++++++++++++-------------- src/smt/theory_str.h | 2 +- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index d393f1cdb..511585fa8 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -299,10 +299,10 @@ static void cut_vars_map_copy(std::map & dest, std::map } bool theory_str::has_self_cut(expr * n1, expr * n2) { - if (cut_var_map.find(n1) == cut_var_map.end()) { + if (!cut_var_map.contains(n1)) { return false; } - if (cut_var_map.find(n2) == cut_var_map.end()) { + if (!cut_var_map.contains(n2)) { return false; } if (cut_var_map[n1].empty() || cut_var_map[n2].empty()) { @@ -322,10 +322,11 @@ void theory_str::add_cut_info_one_node(expr * baseNode, int slevel, expr * node) // crash avoidance? m_trail.push_back(baseNode); m_trail.push_back(node); - if (cut_var_map.find(baseNode) == cut_var_map.end()) { + if (!cut_var_map.contains(baseNode)) { T_cut * varInfo = alloc(T_cut); varInfo->level = slevel; varInfo->vars[node] = 1; + cut_var_map.insert(baseNode, std::stack()); cut_var_map[baseNode].push(varInfo); TRACE("t_str_cut_var_map", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << std::endl;); } else { @@ -357,7 +358,7 @@ void theory_str::add_cut_info_merge(expr * destNode, int slevel, expr * srcNode) // crash avoidance? m_trail.push_back(destNode); m_trail.push_back(srcNode); - if (cut_var_map.find(srcNode) == cut_var_map.end()) { + if (!cut_var_map.contains(srcNode)) { get_manager().raise_exception("illegal state in add_cut_info_merge(): cut_var_map doesn't contain srcNode"); } @@ -365,10 +366,11 @@ void theory_str::add_cut_info_merge(expr * destNode, int slevel, expr * srcNode) get_manager().raise_exception("illegal state in add_cut_info_merge(): cut_var_map[srcNode] is empty"); } - if (cut_var_map.find(destNode) == cut_var_map.end()) { + if (!cut_var_map.contains(destNode)) { T_cut * varInfo = alloc(T_cut); varInfo->level = slevel; cut_vars_map_copy(varInfo->vars, cut_var_map[srcNode].top()->vars); + cut_var_map.insert(destNode, std::stack()); cut_var_map[destNode].push(varInfo); TRACE("t_str_cut_var_map", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << std::endl;); } else { @@ -389,7 +391,7 @@ void theory_str::add_cut_info_merge(expr * destNode, int slevel, expr * srcNode) } void theory_str::check_and_init_cut_var(expr * node) { - if (cut_var_map.find(node) != cut_var_map.end()) { + if (cut_var_map.contains(node)) { return; } else if (!m_strutil.is_string(node)) { add_cut_info_one_node(node, -1, node); @@ -6488,18 +6490,29 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { TRACE("t_str", tout << "pop " << num_scopes << " to " << sLevel << std::endl;); TRACE("t_str_dump_assign_on_scope_change", dump_assignments();); - std::map >::iterator varItor = cut_var_map.begin(); + // list of expr* to remove from cut_var_map + ptr_vector cutvarmap_removes; + + obj_map >::iterator varItor = cut_var_map.begin(); while (varItor != cut_var_map.end()) { - while ((varItor->second.size() > 0) && (varItor->second.top()->level != 0) && (varItor->second.top()->level >= sLevel)) { - T_cut * aCut = varItor->second.top(); - varItor->second.pop(); + std::stack & val = cut_var_map[varItor->m_key]; + while ((val.size() > 0) && (val.top()->level != 0) && (val.top()->level >= sLevel)) { + T_cut * aCut = val.top(); + val.pop(); // dealloc(aCut); // TODO find a safer way to do this, it is causing a crash } - if (varItor->second.size() == 0) { - cut_var_map.erase(varItor++); - } else { - varItor++; + if (val.size() == 0) { + cutvarmap_removes.insert(varItor->m_key); } + varItor++; + } + + if (!cutvarmap_removes.empty()) { + ptr_vector::iterator it = cutvarmap_removes.begin(); + for (; it != cutvarmap_removes.end(); ++it) { + expr * ex = *it; + cut_var_map.remove(ex); + } } // see if any internal variables went out of scope diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 6ce46abb4..bb2fc01d6 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -206,7 +206,7 @@ namespace smt { bool avoidLoopCut; bool loopDetected; - std::map > cut_var_map; + obj_map > cut_var_map; std::set variable_set; std::set internal_variable_set; From b3fddf47076faa65964735c5c64963dfe202af11 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 12 Sep 2016 16:41:35 -0400 Subject: [PATCH 204/562] performance optimization in theory_str::classify_ast_by_type --- src/smt/theory_str.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 511585fa8..ecc3b7247 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -6597,10 +6597,10 @@ void theory_str::classify_ast_by_type(expr * node, std::map & varMap expr * arg1Val = get_eqc_value(arg1, arg1HasEq); int canskip = 0; - if (arg0HasEq && arg0Val == m_strutil.mk_string("")) { + if (arg0HasEq && m_strutil.get_string_constant_value(arg0Val).empty()) { canskip = 1; } - if (canskip == 0 && arg1HasEq && arg1Val == m_strutil.mk_string("")) { + if (canskip == 0 && arg1HasEq && m_strutil.get_string_constant_value(arg1Val).empty()) { canskip = 1; } if (canskip == 0 && concatMap.find(node) == concatMap.end()) { From 015016c92b43e4b01eadd00e00e151d59952362a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 12 Sep 2016 16:57:05 -0400 Subject: [PATCH 205/562] disable variable scope check if not tracing in theory_str --- src/smt/theory_str.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ecc3b7247..29bedbe86 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -6472,6 +6472,11 @@ void theory_str::check_variable_scope() { if (!opt_CheckVariableScope) { return; } + + if (!is_trace_enabled("t_str_detail")) { + return; + } + TRACE("t_str_detail", tout << "checking scopes of variables in the current assignment" << std::endl;); context & ctx = get_context(); From ca71a20ab75e8d868bfafb28115a7597717ea7f2 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 12 Sep 2016 17:17:17 -0400 Subject: [PATCH 206/562] add caching to theory_str::mk_concat, WIP --- src/smt/theory_str.cpp | 15 +++++---------- src/smt/theory_str.h | 2 ++ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 29bedbe86..f44cb8322 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -735,16 +735,14 @@ expr * theory_str::mk_concat(expr * n1, expr * n2) { // * expr * ast2 = mk_2_arg_app(ctx, td->Concat, n1, n2); // Z3 treats (ast1) and (ast2) as two different nodes. //------------------------------------------------------- - std::pair concatArgs(n1, n2); + expr * concatAst = NULL; - // TODO NEXT add cache lookups. I think we need to be more careful than just using std:: data structures here - /* - if (concat_astNode_map.find(concatArgs) == concat_astNode_map.end()) { - */ - if (true) { + + if (!concat_astNode_map.find(n1, n2, concatAst)) { expr * args[2] = {n1, n2}; concatAst = m.mk_app(get_id(), OP_STRCAT, 0, 0, 2, args); - // concat_astNode_map[concatArgs] = concatAst; + m_trail.push_back(concatAst); + concat_astNode_map.insert(n1, n2, concatAst); expr_ref concat_length(mk_strlen(concatAst), m); @@ -756,9 +754,6 @@ expr * theory_str::mk_concat(expr * n1, expr * n2) { } expr_ref lenAssert(ctx.mk_eq_atom(concat_length, m_autil.mk_add(items.size(), items.c_ptr())), m); assert_axiom(lenAssert); - } else { - // concatAst = concat_astNode_map[concatArgs]; - NOT_IMPLEMENTED_YET(); } return concatAst; } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index bb2fc01d6..9f7d51a8f 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -250,6 +250,8 @@ namespace smt { std::map charSetLookupTable; int charSetSize; + obj_pair_map concat_astNode_map; + protected: void assert_axiom(expr * e); void assert_implication(expr * premise, expr * conclusion); From aea0032aa7d5c2ed5022517217383becf861db24 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 13 Sep 2016 18:01:45 -0400 Subject: [PATCH 207/562] manage our own union-find structure in theory_str concat-086.smt2 passes with this, for the first time ever --- src/smt/theory_str.cpp | 213 ++++++++++++++++++++++++++--------------- src/smt/theory_str.h | 17 ++++ 2 files changed, 154 insertions(+), 76 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index f44cb8322..939a63160 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -54,12 +54,15 @@ theory_str::theory_str(ast_manager & m): tmpValTestVarCount(0), avoidLoopCut(true), loopDetected(false), - contains_map(m) + contains_map(m), + m_find(*this), + m_trail_stack(*this) { initialize_charset(); } theory_str::~theory_str() { + m_trail_stack.reset(); } void theory_str::initialize_charset() { @@ -284,7 +287,7 @@ theory_var theory_str::mk_var(enode* n) { } else { theory_var v = theory::mk_var(n); - // m_find.mk_var(); + m_find.mk_var(); get_context().attach_th_var(n, this, v); get_context().mark_as_relevant(n); return v; @@ -1586,6 +1589,8 @@ void theory_str::attach_new_th_var(enode * n) { void theory_str::reset_eh() { TRACE("t_str", tout << "resetting" << std::endl;); + m_trail_stack.reset(); + m_basicstr_axiom_todo.reset(); m_str_eq_todo.reset(); m_concat_axiom_todo.reset(); @@ -1673,13 +1678,40 @@ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { return true; } +// support for user_smt_theory-style EQC handling + +app * theory_str::get_ast(theory_var i) { + return get_enode(i)->get_owner(); +} + +theory_var theory_str::get_var(expr * n) const { + if (!is_app(n)) { + return null_theory_var; + } + context & ctx = get_context(); + if (ctx.e_internalized(to_app(n))) { + enode * e = ctx.get_enode(to_app(n)); + return e->get_th_var(get_id()); + } + return null_theory_var; +} + +// simulate Z3_theory_get_eqc_next() +expr * theory_str::get_eqc_next(expr * n) { + theory_var v = get_var(n); + if (v != null_theory_var) { + theory_var r = m_find.next(v); + return get_ast(r); + } + return n; +} + void theory_str::group_terms_by_eqc(expr * n, std::set & concats, std::set & vars, std::set & consts) { context & ctx = get_context(); - enode * nNode = ctx.get_enode(n); - enode * eqcNode = nNode; + expr * eqcNode = n; do { - app * ast = eqcNode->get_owner(); - if (is_concat(eqcNode)) { + app * ast = to_app(eqcNode); + if (is_concat(ast)) { expr * simConcat = simplify_concat(ast); if (simConcat != ast) { if (is_concat(to_app(simConcat))) { @@ -1694,13 +1726,13 @@ void theory_str::group_terms_by_eqc(expr * n, std::set & concats, std::se } else { concats.insert(simConcat); } - } else if (is_string(eqcNode)) { + } else if (is_string(ast)) { consts.insert(ast); } else { vars.insert(ast); } - eqcNode = eqcNode->get_next(); - } while (eqcNode != nNode); + eqcNode = get_eqc_next(eqcNode); + } while (eqcNode != n); } void theory_str::get_nodes_in_concat(expr * node, ptr_vector & nodeList) { @@ -3975,6 +4007,22 @@ expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { return n; } +// Simulate the behaviour of get_eqc_value() from Z3str2. +// We only check m_find for a string constant. + +expr * theory_str::z3str2_get_eqc_value(expr * n , bool & hasEqcValue) { + expr * curr = n; + do { + if (m_strutil.is_string(curr)) { + hasEqcValue = true; + return curr; + } + curr = get_eqc_next(curr); + } while (curr != n); + hasEqcValue = false; + return n; +} + // from Z3: theory_seq.cpp static theory_mi_arith* get_th_arith(context& ctx, theory_id afid, expr* e) { @@ -6110,106 +6158,107 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { instantiate_str_eq_length_axiom(ctx.get_enode(lhs), ctx.get_enode(rhs)); // group terms by equivalence class (groupNodeInEqc()) - // Previously we did the check between LHS and RHS equivalence classes. - // However these have since been merged. - // We start by asserting that the EQCs, in fact, really are merged. - if (!in_same_eqc(lhs, rhs)) { - TRACE("t_str", tout << "BUG: lhs and rhs not in same eqc in new_eq_eh(), loss of invariant!" << std::endl;); - UNREACHABLE(); - } - std::set eqc_concat; - std::set eqc_var; - std::set eqc_const; - group_terms_by_eqc(lhs, eqc_concat, eqc_var, eqc_const); + std::set eqc_concat_lhs; + std::set eqc_var_lhs; + std::set eqc_const_lhs; + group_terms_by_eqc(lhs, eqc_concat_lhs, eqc_var_lhs, eqc_const_lhs); + + std::set eqc_concat_rhs; + std::set eqc_var_rhs; + std::set eqc_const_rhs; + group_terms_by_eqc(rhs, eqc_concat_rhs, eqc_var_rhs, eqc_const_rhs); TRACE("t_str_detail", - tout << "eqc:" << std::endl; + tout << "lhs eqc:" << std::endl; tout << "Concats:" << std::endl; - for (std::set::iterator it = eqc_concat.begin(); it != eqc_concat.end(); ++it) { + for (std::set::iterator it = eqc_concat_lhs.begin(); it != eqc_concat_lhs.end(); ++it) { expr * ex = *it; tout << mk_ismt2_pp(ex, get_manager()) << std::endl; } tout << "Variables:" << std::endl; - for (std::set::iterator it = eqc_var.begin(); it != eqc_var.end(); ++it) { + for (std::set::iterator it = eqc_var_lhs.begin(); it != eqc_var_lhs.end(); ++it) { expr * ex = *it; tout << mk_ismt2_pp(ex, get_manager()) << std::endl; } tout << "Constants:" << std::endl; - for (std::set::iterator it = eqc_const.begin(); it != eqc_const.end(); ++it) { + for (std::set::iterator it = eqc_const_lhs.begin(); it != eqc_const_lhs.end(); ++it) { + expr * ex = *it; + tout << mk_ismt2_pp(ex, get_manager()) << std::endl; + } + + tout << "rhs eqc:" << std::endl; + tout << "Concats:" << std::endl; + for (std::set::iterator it = eqc_concat_rhs.begin(); it != eqc_concat_rhs.end(); ++it) { + expr * ex = *it; + tout << mk_ismt2_pp(ex, get_manager()) << std::endl; + } + tout << "Variables:" << std::endl; + for (std::set::iterator it = eqc_var_rhs.begin(); it != eqc_var_rhs.end(); ++it) { + expr * ex = *it; + tout << mk_ismt2_pp(ex, get_manager()) << std::endl; + } + tout << "Constants:" << std::endl; + for (std::set::iterator it = eqc_const_rhs.begin(); it != eqc_const_rhs.end(); ++it) { expr * ex = *it; tout << mk_ismt2_pp(ex, get_manager()) << std::endl; } ); // step 1: Concat == Concat - - // enhancement from Z3str2: all-pairs match over LHS and RHS wrt. other concats - if (eqc_concat.size() != 0) { - std::set::iterator itor1, itor2; - for (itor1 = eqc_concat.begin(); itor1 != eqc_concat.end(); ++itor1) { - for (itor2 = itor1; itor2 != eqc_concat.end(); ++itor2) { - if (itor1 == itor2) { - continue; - } - expr * e1 = *itor1; - expr * e2 = *itor2; - TRACE("t_str_detail", tout << "simplify concat-concat pair " << mk_pp(e1, m) << " and " << mk_pp(e2, m) << std::endl;); - simplify_concat_equality(e1, e2); + int hasCommon = 0; + if (eqc_concat_lhs.size() != 0 && eqc_concat_rhs.size() != 0) { + std::set::iterator itor1 = eqc_concat_lhs.begin(); + std::set::iterator itor2 = eqc_concat_rhs.begin(); + for (; itor1 != eqc_concat_lhs.end(); itor1++) { + if (eqc_concat_rhs.find(*itor1) != eqc_concat_rhs.end()) { + hasCommon = 1; + break; } } + for (; itor2 != eqc_concat_rhs.end(); itor2++) { + if (eqc_concat_lhs.find(*itor2) != eqc_concat_lhs.end()) { + hasCommon = 1; + break; + } + } + if (hasCommon == 0) { + simplify_concat_equality(*(eqc_concat_lhs.begin()), *(eqc_concat_rhs.begin())); + } } // step 2: Concat == Constant - // same enhancement as above wrt. Z3str2's behaviour - if (eqc_const.size() != 0) { - expr * conStr = *(eqc_const.begin()); - std::set::iterator itor2; - for (itor2 = eqc_concat.begin(); itor2 != eqc_concat.end(); ++itor2) { + + if (eqc_const_lhs.size() != 0) { + expr * conStr = *(eqc_const_lhs.begin()); + std::set::iterator itor2 = eqc_const_rhs.begin(); + for (; itor2 != eqc_const_rhs.end(); itor2++) { solve_concat_eq_str(*itor2, conStr); } + } else if (eqc_const_rhs.size() != 0) { + expr* conStr = *(eqc_const_rhs.begin()); + std::set::iterator itor1 = eqc_const_lhs.begin(); + for (; itor1 != eqc_const_lhs.end(); itor1++) { + solve_concat_eq_str(*itor1, conStr); + } } // simplify parents wrt. the equivalence class of both sides - // TODO this is slightly broken, re-enable it once some semantics have been fixed - // Briefly, Z3str2 expects that as this function is entered, - // lhs and rhs are NOT in the same equivalence class yet. - // However, newer versions of Z3 appear to behave differently, - // putting lhs and rhs into the same equivalence class - // *before* this function is called. - // Instead we do something possibly more aggressive here. - /* - bool lhs_has_eqc_value = false; - bool rhs_has_eqc_value = false; - expr * lhs_value = get_eqc_value(lhs, lhs_has_eqc_value); - expr * rhs_value = get_eqc_value(rhs, rhs_has_eqc_value); - if (lhs_has_eqc_value && !rhs_has_eqc_value) { - simplify_parent(rhs, lhs_value); + bool nn1HasEqcValue = false; + bool nn2HasEqcValue = false; + // we want the Z3str2 eqc check here... + expr * nn1_value = z3str2_get_eqc_value(lhs, nn1HasEqcValue); + expr * nn2_value = z3str2_get_eqc_value(rhs, nn2HasEqcValue); + if (nn1HasEqcValue && !nn2HasEqcValue) { + simplify_parent(rhs, nn1_value); } - if (!lhs_has_eqc_value && rhs_has_eqc_value) { - simplify_parent(lhs, rhs_value); - } - */ - bool lhs_has_eqc_value = false; - bool rhs_has_eqc_value = false; - expr * lhs_value = get_eqc_value(lhs, lhs_has_eqc_value); - expr * rhs_value = get_eqc_value(rhs, rhs_has_eqc_value); - - // TODO this depends on the old, possibly broken, semantics of is_string(). - // we explicitly want to test whether lhs/rhs is actually a string constant. - bool lhs_is_string_constant = m_strutil.is_string(lhs); - bool rhs_is_string_constant = m_strutil.is_string(rhs); - - - if (lhs_has_eqc_value && !rhs_is_string_constant) { - simplify_parent(rhs, lhs_value); - } - if (rhs_has_eqc_value && !lhs_is_string_constant) { - simplify_parent(lhs, rhs_value); + if (!nn1HasEqcValue && nn2HasEqcValue) { + simplify_parent(lhs, nn2_value); } // regex unroll + // TODO NEXT check EQC semantics here too expr * nn1EqConst = NULL; std::set nn1EqUnrollFuncs; @@ -6229,6 +6278,7 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { process_unroll_eq_const_str(*itor2, nn1EqConst); } } + } void theory_str::set_up_axioms(expr * ex) { @@ -6407,7 +6457,15 @@ void theory_str::new_eq_eh(theory_var x, theory_var y) { //TRACE("t_str_detail", tout << "new eq: v#" << x << " = v#" << y << std::endl;); TRACE("t_str", tout << "new eq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " = " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); + /* + if (m_find.find(x) == m_find.find(y)) { + return; + } + */ handle_equality(get_enode(x)->get_owner(), get_enode(y)->get_owner()); + + // replicate Z3str2 behaviour: merge eqc **AFTER** handle_equality + m_find.merge(x, y); } void theory_str::new_diseq_eh(theory_var x, theory_var y) { @@ -6427,6 +6485,8 @@ void theory_str::assign_eh(bool_var v, bool is_true) { void theory_str::push_scope_eh() { theory::push_scope_eh(); + m_trail_stack.push_scope(); + sLevel += 1; TRACE("t_str", tout << "push to " << sLevel << std::endl;); TRACE("t_str_dump_assign_on_scope_change", dump_assignments();); @@ -6549,6 +6609,7 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { m_basicstr_axiom_todo.reset(); m_basicstr_axiom_todo = new_m_basicstr; + m_trail_stack.pop_scope(num_scopes); theory::pop_scope_eh(num_scopes); check_variable_scope(); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 9f7d51a8f..58b104209 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -27,6 +27,7 @@ Revision History: #include #include #include"str_rewriter.h" +#include"union_find.h" namespace smt { @@ -81,6 +82,10 @@ namespace smt { level = -100; } }; + + typedef trail_stack th_trail_stack; + typedef union_find th_union_find; + protected: // Some options that control how the solver operates. @@ -252,6 +257,12 @@ namespace smt { obj_pair_map concat_astNode_map; + th_union_find m_find; + th_trail_stack m_trail_stack; + theory_var get_var(expr * n) const; + expr * get_eqc_next(expr * n); + app * get_ast(theory_var i); + protected: void assert_axiom(expr * e); void assert_implication(expr * premise, expr * conclusion); @@ -347,6 +358,7 @@ namespace smt { app * mk_value_helper(app * n); expr * get_eqc_value(expr * n, bool & hasEqcValue); + expr * z3str2_get_eqc_value(expr * n , bool & hasEqcValue); bool in_same_eqc(expr * n1, expr * n2); expr * collect_eq_nodes(expr * n, expr_ref_vector & eqcSet); @@ -479,6 +491,11 @@ namespace smt { virtual void display(std::ostream & out) const; bool overlapping_variables_detected() const { return loopDetected; } + + th_trail_stack& get_trail_stack() { return m_trail_stack; } + void merge_eh(theory_var, theory_var, theory_var v1, theory_var v2) {} + void after_merge_eh(theory_var r1, theory_var r2, theory_var v1, theory_var v2) { } + void unmerge_eh(theory_var v1, theory_var v2) {} protected: virtual bool internalize_atom(app * atom, bool gate_ctx); virtual bool internalize_term(app * term); From 8f636e1f57be63f2cdb071b44fc7baae0d05e924 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 13 Sep 2016 18:16:21 -0400 Subject: [PATCH 208/562] fix typo'ed set reference in handle_equality --- src/smt/theory_str.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 939a63160..c3ae176b7 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -6231,14 +6231,14 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { if (eqc_const_lhs.size() != 0) { expr * conStr = *(eqc_const_lhs.begin()); - std::set::iterator itor2 = eqc_const_rhs.begin(); - for (; itor2 != eqc_const_rhs.end(); itor2++) { + std::set::iterator itor2 = eqc_concat_rhs.begin(); + for (; itor2 != eqc_concat_rhs.end(); itor2++) { solve_concat_eq_str(*itor2, conStr); } } else if (eqc_const_rhs.size() != 0) { expr* conStr = *(eqc_const_rhs.begin()); - std::set::iterator itor1 = eqc_const_lhs.begin(); - for (; itor1 != eqc_const_lhs.end(); itor1++) { + std::set::iterator itor1 = eqc_concat_lhs.begin(); + for (; itor1 != eqc_concat_lhs.end(); itor1++) { solve_concat_eq_str(*itor1, conStr); } } From 34dc65515041928c4ef5116871c959a55f42fc08 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 13 Sep 2016 18:24:59 -0400 Subject: [PATCH 209/562] z3str2 eqc semantics for theory_str unroll checks --- src/smt/theory_str.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index c3ae176b7..6d4fb1aab 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -8883,9 +8883,7 @@ void theory_str::get_eqc_allUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet.insert(curr); } } - enode * e_curr = ctx.get_enode(curr); - curr = e_curr->get_next()->get_owner(); - // curr = get_eqc_next(t, curr); + curr = get_eqc_next(curr); } while (curr != n); } @@ -8907,9 +8905,7 @@ void theory_str::get_eqc_simpleUnroll(expr * n, expr * &constStr, std::setget_next()->get_owner(); - // curr = get_eqc_next(t, curr); + curr = get_eqc_next(curr); } while (curr != n); } From 9481601b4b315ada3abf46dd90b85b99136d2c94 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 14 Sep 2016 15:15:47 -0400 Subject: [PATCH 210/562] restore z3str2 eqc semantics in theory_str::new_eq_check --- src/smt/theory_str.cpp | 73 +++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 48 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 6d4fb1aab..1238eb069 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -32,7 +32,7 @@ theory_str::theory_str(ast_manager & m): /* Options */ opt_AggressiveLengthTesting(false), opt_AggressiveValueTesting(false), - opt_AggressiveUnrollTesting(true), + opt_AggressiveUnrollTesting(false), opt_EagerStringConstantLengthAssertions(true), opt_VerifyFinalCheckProgress(true), opt_LCMUnrollStep(2), @@ -143,7 +143,7 @@ void theory_str::assert_axiom(expr * e) { if (opt_VerifyFinalCheckProgress) { finalCheckProgressIndicator = true; } - // TODO add to m_trail? + if (get_manager().is_true(e)) return; TRACE("t_str_detail", tout << "asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); context & ctx = get_context(); @@ -1612,58 +1612,42 @@ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { context & ctx = get_context(); ast_manager & m = get_manager(); - // Previously we did the check between LHS and RHS equivalence classes. - // However these have since been merged. - // We start by asserting that the EQCs, in fact, really are merged. - if (!in_same_eqc(lhs, rhs)) { - TRACE("t_str", tout << "BUG: lhs and rhs not in same eqc in new_eq_eh(), loss of invariant!" << std::endl;); - UNREACHABLE(); - } - // skip this check if we defer consistency checking, as we can do it for every EQC in final check if (!opt_DeferEQCConsistencyCheck) { check_concat_len_in_eqc(lhs); check_concat_len_in_eqc(rhs); } - // Now we iterate over all pairs of terms in the (shared) eqc + // Now we iterate over all pairs of terms across both EQCs // and check whether we can show that any pair of distinct terms // cannot possibly be equal. // If that's the case, we assert an axiom to that effect and stop. - enode * eqc_root = ctx.get_enode(lhs)->get_root(); - enode * eqc_iterator1 = eqc_root; + expr * eqc_nn1 = lhs; do { - enode * eqc_iterator2 = eqc_iterator1; + expr * eqc_nn2 = rhs; do { - if (eqc_iterator1 != eqc_iterator2) { - // pull terms out of the enodes - app * eqc_nn1 = eqc_iterator1->get_owner(); - app * eqc_nn2 = eqc_iterator2->get_owner(); - TRACE("t_str_detail", tout << "checking whether " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " can be equal" << std::endl;); - if (!can_two_nodes_eq(eqc_nn1, eqc_nn2)) { - TRACE("t_str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " cannot be equal to " << mk_pp(eqc_nn2, m) << std::endl;); - expr_ref to_assert(m.mk_not(ctx.mk_eq_atom(eqc_nn1, eqc_nn2)), m); - assert_axiom(to_assert); - // this shouldn't use the integer theory at all, so we don't allow the option of quick-return + TRACE("t_str_detail", tout << "checking whether " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " can be equal" << std::endl;); + // inconsistency check: value + if (!can_two_nodes_eq(eqc_nn1, eqc_nn2)) { + TRACE("t_str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " cannot be equal to " << mk_pp(eqc_nn2, m) << std::endl;); + expr_ref to_assert(m.mk_not(ctx.mk_eq_atom(eqc_nn1, eqc_nn2)), m); + assert_axiom(to_assert); + // this shouldn't use the integer theory at all, so we don't allow the option of quick-return + return false; + } + if (!check_length_consistency(eqc_nn1, eqc_nn2)) { + TRACE("t_str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " have inconsistent lengths" << std::endl;); + if (opt_NoQuickReturn_IntegerTheory){ + TRACE("t_str_detail", tout << "continuing in new_eq_check() due to opt_NoQuickReturn_IntegerTheory" << std::endl;); + } else { return false; } - if (!check_length_consistency(eqc_nn1, eqc_nn2)) { - TRACE("t_str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " have inconsistent lengths" << std::endl;); - if (opt_NoQuickReturn_IntegerTheory){ - TRACE("t_str_detail", tout << "continuing in new_eq_check() due to opt_NoQuickReturn_IntegerTheory" << std::endl;); - } else { - return false; - } - } } - eqc_iterator2 = eqc_iterator2->get_next(); - - } while (eqc_iterator2 != eqc_root); - - eqc_iterator1 = eqc_iterator1->get_next(); - } while (eqc_iterator1 != eqc_root); - + eqc_nn2 = get_eqc_next(eqc_nn2); + } while (eqc_nn2 != rhs); + eqc_nn1 = get_eqc_next(eqc_nn1); + } while (eqc_nn1 != lhs); if (!contains_map.empty()) { check_contain_in_new_eq(lhs, rhs); @@ -2327,7 +2311,6 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { infer_len_concat_equality(nn1, nn2); - // TODO we may want to add no-quick-return options for these as well if (a1_arg0 == a2_arg0) { if (!in_same_eqc(a1_arg1, a2_arg1)) { expr_ref premise(ctx.mk_eq_atom(nn1, nn2), m); @@ -2354,8 +2337,6 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { // quick path - // TODO we may want to add no-quick-return options for these as well - if (in_same_eqc(a1_arg0, a2_arg0)) { if (in_same_eqc(a1_arg1, a2_arg1)) { TRACE("t_str_detail", tout << "SKIP: a1_arg0 =~ a2_arg0 and a1_arg1 =~ a2_arg1" << std::endl;); @@ -4846,9 +4827,8 @@ void theory_str::check_contain_in_new_eq(expr * n1, expr * n2) { ast_manager & m = get_manager(); TRACE("t_str_detail", tout << "consistency check for contains wrt. " << mk_pp(n1, m) << " and " << mk_pp(n2, m) << std::endl;); - // Modification from Z3str2: the EQC of n1 and n2 *are* now merged. - // So we don't have to do anything too special - // to prepare willEqClass any more, we just use the EQC from n1 / n2. + // Modification from Z3str2: if we use the merged EQC directly from the context, + // we don't have to do anything special to merge n1/n2's EQCs. expr_ref_vector willEqClass(m); expr * constStrAst = collect_eq_nodes(n1, willEqClass); @@ -6257,9 +6237,6 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { simplify_parent(lhs, nn2_value); } - // regex unroll - // TODO NEXT check EQC semantics here too - expr * nn1EqConst = NULL; std::set nn1EqUnrollFuncs; get_eqc_allUnroll(lhs, nn1EqConst, nn1EqUnrollFuncs); From ec9e1686f75d5171983f821c1a3e44312c7a9f19 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 14 Sep 2016 15:32:49 -0400 Subject: [PATCH 211/562] fix semantics of collect_eq_nodes and simplify_parent --- src/smt/theory_str.cpp | 78 ++++++++++-------------------------------- 1 file changed, 18 insertions(+), 60 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 1238eb069..3727e15e1 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1785,18 +1785,16 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { << " with respect to " << mk_ismt2_pp(eq_str, m) << std::endl;); ctx.internalize(nn, false); - enode * n_eq_enode = ctx.get_enode(nn); - enode * nn_enode = n_eq_enode; std::string eq_strValue = m_strutil.get_string_constant_value(eq_str); - + app * n_eqNode = nn; do { - app * n_eqNode = n_eq_enode->get_owner(); + enode * n_eq_enode = ctx.get_enode(n_eqNode); TRACE("t_str_detail", tout << "considering all parents of " << mk_ismt2_pp(n_eqNode, m) << std::endl << "associated n_eq_enode has " << n_eq_enode->get_num_parents() << " parents" << std::endl;); // the goal of this next bit is to avoid dereferencing a bogus e_parent in the following loop. - // what I image is causing this bug is that, for example, we examine some parent, we add an axiom that involves it, + // what I imagine is causing this bug is that, for example, we examine some parent, we add an axiom that involves it, // and the parent_it iterator becomes invalidated, because we indirectly modified the container that we're iterating over. enode_vector current_parents; @@ -2068,8 +2066,8 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { // check next EQC member - n_eq_enode = n_eq_enode->get_next(); - } while (n_eq_enode != nn_enode); + n_eqNode = get_eqc_next(n_eqNode); + } while (n_eqNode != nn); } expr * theory_str::simplify_concat(expr * node) { @@ -4158,45 +4156,6 @@ bool theory_str::get_len_value(expr* e, rational& val) { return val.is_int(); } -/* - * Look through the equivalence class of n to find an integer constant. - * Return that constant if it is found. Otherwise, return -1. - * Note that a return value of -1 should not normally be possible, as - * string length cannot be negative. - */ - -/* -rational theory_str::get_len_value(expr * x) { - ast_manager & m = get_manager(); - context & ctx = get_context(); - ctx.internalize(x, false); - expr * n = mk_strlen(x); - ctx.internalize(n, false); - - TRACE("t_str_detail", tout << "checking eqc of " << mk_ismt2_pp(n, m) << " for an integer constant" << std::endl;); - - enode * nNode = ctx.get_enode(n); - enode * eqcNode = nNode; - do { - app * ast = eqcNode->get_owner(); - rational val; - bool is_int; - TRACE("t_str_detail", tout << "eqc member: " << mk_ismt2_pp(ast, m) << std::endl;); - if (m_autil.is_numeral(ast, val, is_int)) { - if (is_int) { - TRACE("t_str_detail", tout << "eqc contains integer constant " << val << std::endl;); - SASSERT(!val.is_neg()); - return val; - } - } - eqcNode = eqcNode->get_next(); - } while (eqcNode != nNode); - // not found - TRACE("t_str_detail", tout << "eqc contains no integer constants" << std::endl;); - return rational(-1); -} -*/ - /* * Decide whether n1 and n2 are already in the same equivalence class. * This only checks whether the core considers them to be equal; @@ -4241,17 +4200,15 @@ expr * theory_str::collect_eq_nodes(expr * n, expr_ref_vector & eqcSet) { context & ctx = get_context(); expr * constStrNode = NULL; - enode * e_base = ctx.get_enode(n); - enode * e_curr = e_base; + app * ex = n; do { - app * ex = e_curr->get_owner(); if (m_strutil.is_string(ex)) { constStrNode = ex; } eqcSet.push_back(ex); - e_curr = e_curr->get_next(); - } while (e_curr != e_base); + ex = get_eqc_next(ex); + } while (ex != n); return constStrNode; } @@ -4827,10 +4784,10 @@ void theory_str::check_contain_in_new_eq(expr * n1, expr * n2) { ast_manager & m = get_manager(); TRACE("t_str_detail", tout << "consistency check for contains wrt. " << mk_pp(n1, m) << " and " << mk_pp(n2, m) << std::endl;); - // Modification from Z3str2: if we use the merged EQC directly from the context, - // we don't have to do anything special to merge n1/n2's EQCs. expr_ref_vector willEqClass(m); - expr * constStrAst = collect_eq_nodes(n1, willEqClass); + expr * constStrAst_1 = collect_eq_nodes(n1, willEqClass); + expr * constStrAst_2 = collect_eq_nodes(n2, willEqClass); + expr * constStrAst = (constStrAst_1 != NULL) ? constStrAst_1 : constStrAst_2; TRACE("t_str_detail", tout << "eqc of n1 is {"; for (expr_ref_vector::iterator it = willEqClass.begin(); it != willEqClass.end(); ++it) { @@ -5582,10 +5539,8 @@ bool theory_str::check_concat_len_in_eqc(expr * concat) { bool no_assertions = true; - enode * eqc_base = ctx.get_enode(concat); - enode * eqc_it = eqc_base; + app * eqc_n = concat; do { - app * eqc_n = eqc_it->get_owner(); if (is_concat(eqc_n)) { rational unused; bool status = infer_len_concat(eqc_n, unused); @@ -5593,8 +5548,8 @@ bool theory_str::check_concat_len_in_eqc(expr * concat) { no_assertions = false; } } - eqc_it = eqc_it->get_next(); - } while (eqc_it != eqc_base); + eqc_n = get_eqc_next(eqc_n); + } while (eqc_n != concat); return no_assertions; } @@ -5604,7 +5559,10 @@ void theory_str::check_regex_in(expr * nn1, expr * nn2) { ast_manager & m = get_manager(); expr_ref_vector eqNodeSet(m); - expr * constStr = collect_eq_nodes(nn1, eqNodeSet); + + expr * constStr_1 = collect_eq_nodes(nn1, eqNodeSet); + expr * constStr_2 = collect_eq_nodes(nn2, eqNodeSet); + expr * constStr = (constStr_1 != NULL) ? constStr_1 : constStr_2; if (constStr == NULL) { return; From 87d61d6d6ed4a685df7d0364e5f37f1297d54175 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 14 Sep 2016 15:35:37 -0400 Subject: [PATCH 212/562] fix semantics of in_same_eqc --- src/smt/theory_str.cpp | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 3727e15e1..ce5aabb6c 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4178,22 +4178,13 @@ bool theory_str::in_same_eqc(expr * n1, expr * n2) { ctx.internalize(n2, false); } - enode * n1Node = ctx.get_enode(n1); - enode * n2Node = ctx.get_enode(n2); - - // here's what the old Z3str2 would have done; we can do something much better - /* - n1Node->get_root(); - enode * curr = n1Node->get_next(); - while (curr != n1Node) { - if (curr == n2Node) { + expr * curr = get_eqc_next(n1); + while (curr != n1) { + if (curr == n2) return true; - } - curr = curr->get_next(); + curr = get_eqc_next(curr); } return false; - */ - return n1Node->get_root() == n2Node->get_root(); } expr * theory_str::collect_eq_nodes(expr * n, expr_ref_vector & eqcSet) { From 50353168ef008a87bc38d37de305bfdfe43627e1 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 14 Sep 2016 15:36:36 -0400 Subject: [PATCH 213/562] fix semantics of get_concats_in_eqc and get_var_in_eqc --- src/smt/theory_str.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ce5aabb6c..a773e0d6d 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -8649,9 +8649,7 @@ void theory_str::get_concats_in_eqc(expr * n, std::set & concats) { if (is_concat(to_app(eqcNode))) { concats.insert(eqcNode); } - enode * e_eqc = ctx.get_enode(eqcNode); - eqcNode = e_eqc->get_next()->get_owner(); - // eqcNode = Z3_theory_get_eqc_next(t, eqcNode); + eqcNode = get_eqc_next(eqcNode); } while (eqcNode != n); } @@ -8663,9 +8661,7 @@ void theory_str::get_var_in_eqc(expr * n, std::set & varSet) { if (variable_set.find(eqcNode) != variable_set.end()) { varSet.insert(eqcNode); } - enode * e_eqc = ctx.get_enode(eqcNode); - eqcNode = e_eqc->get_next()->get_owner(); - // eqcNode = Z3_theory_get_eqc_next(t, eqcNode); + eqcNode = get_eqc_next(eqcNode); } while (eqcNode != n); } From 804009a75754864c26e19adffdefc0d31c4016cd Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 14 Sep 2016 15:37:48 -0400 Subject: [PATCH 214/562] use z3str2 eqc semantics for get_eqc_value --- src/smt/theory_str.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index a773e0d6d..c3c8a50cf 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3967,6 +3967,7 @@ void theory_str::unroll_str2reg_constStr(expr * unrollFunc, expr * eqConstStr) { * Return that constant if it is found, and set hasEqcValue to true. * Otherwise, return n, and set hasEqcValue to false. */ +/* expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { context & ctx = get_context(); // I hope this works @@ -3985,6 +3986,12 @@ expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { hasEqcValue = false; return n; } +*/ + +expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { + return z3str2_get_eqc_value(n, hasEqcValue); +} + // Simulate the behaviour of get_eqc_value() from Z3str2. // We only check m_find for a string constant. From e46fc7b0b68b30f952ab3681a841e7a53e55cc92 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 14 Sep 2016 15:51:33 -0400 Subject: [PATCH 215/562] fix expr-app conversion --- src/smt/theory_str.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index c3c8a50cf..14d30d4a6 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1787,7 +1787,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { ctx.internalize(nn, false); std::string eq_strValue = m_strutil.get_string_constant_value(eq_str); - app * n_eqNode = nn; + expr * n_eqNode = nn; do { enode * n_eq_enode = ctx.get_enode(n_eqNode); TRACE("t_str_detail", tout << "considering all parents of " << mk_ismt2_pp(n_eqNode, m) << std::endl @@ -1872,7 +1872,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { assert_implication(implyL, implyR); } - } else if (is_concat(n_eqNode)) { + } else if (is_concat(to_app(n_eqNode))) { expr_ref simpleConcat(m); simpleConcat = mk_concat(eq_str, arg1); if (!in_same_eqc(a_parent, simpleConcat)) { @@ -1943,7 +1943,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { assert_implication(implyL, implyR); } - } else if (is_concat(n_eqNode)) { + } else if (is_concat(to_app(n_eqNode))) { expr_ref simpleConcat(m); simpleConcat = mk_concat(arg0, eq_str); if (!in_same_eqc(a_parent, simpleConcat)) { @@ -4198,9 +4198,9 @@ expr * theory_str::collect_eq_nodes(expr * n, expr_ref_vector & eqcSet) { context & ctx = get_context(); expr * constStrNode = NULL; - app * ex = n; + expr * ex = n; do { - if (m_strutil.is_string(ex)) { + if (m_strutil.is_string(to_app(ex))) { constStrNode = ex; } eqcSet.push_back(ex); @@ -5537,9 +5537,9 @@ bool theory_str::check_concat_len_in_eqc(expr * concat) { bool no_assertions = true; - app * eqc_n = concat; + expr * eqc_n = concat; do { - if (is_concat(eqc_n)) { + if (is_concat(to_app(eqc_n))) { rational unused; bool status = infer_len_concat(eqc_n, unused); if (status) { From a294c145dc0a4c55e5c89a6b5573ca8c1f84795a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 14 Sep 2016 16:18:03 -0400 Subject: [PATCH 216/562] add theory_str::try_eval_concat to work around rewriter behaviour this fixes a regression in concat-013.smt2 --- src/smt/theory_str.cpp | 62 +++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 2 ++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 14d30d4a6..3687dc9b7 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -762,7 +762,7 @@ expr * theory_str::mk_concat(expr * n1, expr * n2) { } bool theory_str::can_propagate() { - return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty() || !m_concat_axiom_todo.empty() + return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty() || !m_concat_axiom_todo.empty() || !m_concat_eval_todo.empty() || !m_axiom_CharAt_todo.empty() || !m_axiom_StartsWith_todo.empty() || !m_axiom_EndsWith_todo.empty() || !m_axiom_Contains_todo.empty() || !m_axiom_Indexof_todo.empty() || !m_axiom_Indexof2_todo.empty() || !m_axiom_LastIndexof_todo.empty() || !m_axiom_Substr_todo.empty() || !m_axiom_Replace_todo.empty() @@ -794,6 +794,11 @@ void theory_str::propagate() { } m_concat_axiom_todo.reset(); + for (unsigned i = 0; i < m_concat_eval_todo.size(); ++i) { + try_eval_concat(m_concat_eval_todo[i]); + } + m_concat_eval_todo.reset(); + for (unsigned i = 0; i < m_axiom_CharAt_todo.size(); ++i) { instantiate_axiom_CharAt(m_axiom_CharAt_todo[i]); } @@ -853,6 +858,58 @@ void theory_str::propagate() { } } +/* + * Attempt to evaluate a concat over constant strings, + * and if this is possible, assert equality between the + * flattened string and the original term. + */ + +void theory_str::try_eval_concat(enode * cat) { + SASSERT(is_concat(cat)); + app * a_cat = cat->get_owner(); + + context & ctx = get_context(); + ast_manager & m = get_manager(); + + TRACE("t_str_detail", tout << "attempting to flatten " << mk_pp(a_cat, m) << std::endl;); + + std::stack worklist; + std::string flattenedString(""); + bool constOK = true; + + { + app * arg0 = to_app(a_cat->get_arg(0)); + app * arg1 = to_app(a_cat->get_arg(1)); + + worklist.push(arg1); + worklist.push(arg0); + } + + while (constOK && !worklist.empty()) { + app * evalArg = worklist.top(); worklist.pop(); + if (m_strutil.is_string(evalArg)) { + std::string nextStr = m_strutil.get_string_constant_value(evalArg); + flattenedString.append(nextStr); + } else if (is_concat(evalArg)) { + app * arg0 = to_app(evalArg->get_arg(0)); + app * arg1 = to_app(evalArg->get_arg(1)); + + worklist.push(arg1); + worklist.push(arg0); + } else { + TRACE("t_str_detail", tout << "non-constant term in concat -- giving up." << std::endl;); + constOK = false; + break; + } + } + if (constOK) { + TRACE("t_str_detail", tout << "flattened to \"" << flattenedString << "\"" << std::endl;); + expr_ref constStr(m_strutil.mk_string(flattenedString), m); + expr_ref axiom(ctx.mk_eq_atom(a_cat, constStr), m); + assert_axiom(axiom); + } +} + /* * Instantiate an axiom of the following form: * Length(Concat(x, y)) = Length(x) + Length(y) @@ -6240,6 +6297,9 @@ void theory_str::set_up_axioms(expr * ex) { if (is_concat(ap)) { // if ex is a concat, set up concat axioms later m_concat_axiom_todo.push_back(n); + // we also want to check whether we can eval this concat, + // in case the rewriter did not totally finish with this term + m_concat_eval_todo.push_back(n); } else if (is_strlen(ap)) { // if the argument is a variable, // keep track of this for later, we'll need it during model gen diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 58b104209..745d22ac2 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -184,6 +184,7 @@ namespace smt { svector > m_str_eq_todo; ptr_vector m_concat_axiom_todo; ptr_vector m_string_constant_length_todo; + ptr_vector m_concat_eval_todo; // enode lists for term-specific axioms // TODO maybe refactor this into a generic "library_aware_axiom_todo" list @@ -332,6 +333,7 @@ namespace smt { bool is_Unroll(enode const * n) const { return is_Unroll(n->get_owner()); } void instantiate_concat_axiom(enode * cat); + void try_eval_concat(enode * cat); void instantiate_basic_string_axioms(enode * str); void instantiate_str_eq_length_axiom(enode * lhs, enode * rhs); From 9ee7326a19fd46cb0aa61719558c941c5e560051 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 14 Sep 2016 17:26:52 -0400 Subject: [PATCH 217/562] tweaks to process_concat_eq_type_3 --- src/smt/theory_str.cpp | 63 +++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 3687dc9b7..27c12b267 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3393,10 +3393,10 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { expr_ref suf_n_concat(mk_concat(suffixAst, n), mgr); if (can_two_nodes_eq(x, prefixAst) && can_two_nodes_eq(y, suf_n_concat)) { - expr ** r_items = alloc_svect(expr*, 2); - r_items[0] = ctx.mk_eq_atom(x, prefixAst); - r_items[1] = ctx.mk_eq_atom(y, suf_n_concat); - assert_implication(ax_l, mgr.mk_and(2, r_items)); + expr_ref_vector r_items(mgr); + r_items.push_back(ctx.mk_eq_atom(x, prefixAst)); + r_items.push_back(ctx.mk_eq_atom(y, suf_n_concat)); + assert_implication(ax_l, mk_and(r_items)); } else { // negate! It's impossible to split str with these lengths TRACE("t_str", tout << "CONFLICT: Impossible to split str with these lengths." << std::endl;); @@ -3433,11 +3433,11 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { if (can_two_nodes_eq(x, str_temp1)) { if (!avoidLoopCut || !(has_self_cut(x, n))) { - expr ** r_items = alloc_svect(expr*, 3); - r_items[0] = ctx.mk_eq_atom(x, str_temp1); - r_items[1] = ctx.mk_eq_atom(n, temp1_y); - r_items[2] = ctx.mk_eq_atom(mk_strlen(temp1), mk_int(tmpLen)); - expr_ref ax_r(mgr.mk_and(3, r_items), mgr); + expr_ref_vector r_items(mgr); + r_items.push_back(ctx.mk_eq_atom(x, str_temp1)); + r_items.push_back(ctx.mk_eq_atom(n, temp1_y)); + r_items.push_back(ctx.mk_eq_atom(mk_strlen(temp1), mk_int(tmpLen))); + expr_ref ax_r(mk_and(r_items), mgr); //Cut Info add_cut_info_merge(temp1, sLevel, x); @@ -3460,9 +3460,9 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { // Split type -1. We know nothing about the length... int optionTotal = 2 + strValue.length(); - expr ** or_item = alloc_svect(expr*, optionTotal); - int option = 0; - expr ** and_item = alloc_svect(expr*, (2 + 4 * optionTotal)); + expr_ref_vector or_item(mgr); + unsigned option = 0; + expr_ref_vector and_item(mgr); int pos = 1; for (int i = 0; i <= (int) strValue.size(); i++) { std::string part1Str = strValue.substr(0, i); @@ -3474,11 +3474,11 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { if (can_two_nodes_eq(x, cropStr) && can_two_nodes_eq(y, y_concat)) { // break down option 3-1 expr_ref x_eq_str(ctx.mk_eq_atom(x, cropStr), mgr); - or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], x_eq_str); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(y, y_concat)); + or_item.push_back(ctx.mk_eq_atom(xorFlag, mk_int(option))); + and_item.push_back(ctx.mk_eq_atom(or_item.get(option), x_eq_str)); ++pos; + and_item.push_back(ctx.mk_eq_atom(or_item.get(option), ctx.mk_eq_atom(y, y_concat))); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(x), mk_strlen(cropStr))); + and_item.push_back(ctx.mk_eq_atom(or_item.get(option), ctx.mk_eq_atom(mk_strlen(x), mk_strlen(cropStr)))); ++pos; // and_item[pos++] = Z3_mk_eq(ctx, or_item[option], Z3_mk_eq(ctx, mk_length(t, y), mk_length(t, y_concat))); // adding length constraint for _ = constStr seems slowing things down. @@ -3495,18 +3495,18 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { if (can_two_nodes_eq(x, strAst_temp1)) { if (!avoidLoopCut || !(has_self_cut(x, n))) { // break down option 3-2 - or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); + or_item.push_back(ctx.mk_eq_atom(xorFlag, mk_int(option))); expr_ref temp1_y(mk_concat(temp1, y), mgr); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(x, strAst_temp1)); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(n, temp1_y)); + and_item.push_back(ctx.mk_eq_atom(or_item.get(option), ctx.mk_eq_atom(x, strAst_temp1))); ++pos; + and_item.push_back(ctx.mk_eq_atom(or_item.get(option), ctx.mk_eq_atom(n, temp1_y))); ++pos; - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(x), - m_autil.mk_add(mk_strlen(strAst), mk_strlen(temp1)) )); + and_item.push_back(ctx.mk_eq_atom(or_item.get(option), ctx.mk_eq_atom(mk_strlen(x), + m_autil.mk_add(mk_strlen(strAst), mk_strlen(temp1)) )) ); ++pos; option++; - add_cut_info_merge(temp1, ctx.get_scope_level(), x); - add_cut_info_merge(temp1, ctx.get_scope_level(), n); + add_cut_info_merge(temp1, sLevel, x); + add_cut_info_merge(temp1, sLevel, n); } else { loopDetected = true; TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); @@ -3517,11 +3517,11 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { if (option > 0) { if (option == 1) { - and_item[0] = or_item[0]; + and_item.push_back(or_item.get(0)); } else { - and_item[0] = mgr.mk_or(option, or_item); + and_item.push_back(mk_or(or_item)); } - expr_ref implyR(mgr.mk_and(pos, and_item), mgr); + expr_ref implyR(mk_and(and_item), mgr); assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); } else { TRACE("t_str", tout << "STOP: should not split two eq. concats" << std::endl;); @@ -6531,11 +6531,11 @@ void theory_str::check_variable_scope() { ast_manager & m = get_manager(); expr_ref_vector assignments(m); - ctx.get_assignments(assignments); - for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { - expr * ex = *i; - recursive_check_variable_scope(ex); - } + ctx.get_assignments(assignments); + for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { + expr * ex = *i; + recursive_check_variable_scope(ex); + } } void theory_str::pop_scope_eh(unsigned num_scopes) { @@ -6587,6 +6587,7 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { } } + // TODO use the trail stack to do this for us! requires lots of refactoring // TODO if this works, possibly remove axioms from other vectors as well ptr_vector new_m_basicstr; for (ptr_vector::iterator it = m_basicstr_axiom_todo.begin(); it != m_basicstr_axiom_todo.end(); ++it) { From f22f4da023fae6347d96167211ad5244c00f8714 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 14 Sep 2016 17:33:47 -0400 Subject: [PATCH 218/562] remove unused variable --- src/smt/theory_str.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 27c12b267..5ef3518d7 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -151,6 +151,7 @@ void theory_str::assert_axiom(expr * e) { ctx.internalize(e, true); } literal lit(ctx.get_literal(e)); + // TESTING! ctx.mark_as_relevant(lit); ctx.mk_th_axiom(get_id(), 1, &lit); @@ -3459,7 +3460,6 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { else { // Split type -1. We know nothing about the length... - int optionTotal = 2 + strValue.length(); expr_ref_vector or_item(mgr); unsigned option = 0; expr_ref_vector and_item(mgr); From d334403720f1da03219307edf2d976e5fdd90121 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 14 Sep 2016 17:42:40 -0400 Subject: [PATCH 219/562] remove relevancy testing experiment --- src/smt/theory_str.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 5ef3518d7..6034395fc 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -151,7 +151,6 @@ void theory_str::assert_axiom(expr * e) { ctx.internalize(e, true); } literal lit(ctx.get_literal(e)); - // TESTING! ctx.mark_as_relevant(lit); ctx.mk_th_axiom(get_id(), 1, &lit); From 15055c8041d3244a303f017692d4e86ba383baeb Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 14 Sep 2016 19:01:14 -0400 Subject: [PATCH 220/562] use mk_int_var to make xor terms --- src/smt/theory_str.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 6034395fc..34ac58d18 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -32,7 +32,7 @@ theory_str::theory_str(ast_manager & m): /* Options */ opt_AggressiveLengthTesting(false), opt_AggressiveValueTesting(false), - opt_AggressiveUnrollTesting(false), + opt_AggressiveUnrollTesting(true), opt_EagerStringConstantLengthAssertions(true), opt_VerifyFinalCheckProgress(true), opt_LCMUnrollStep(2), @@ -453,6 +453,7 @@ void theory_str::track_variable_scope(expr * var) { } app * theory_str::mk_internal_xor_var() { + /* ast_manager & m = get_manager(); std::stringstream ss; ss << tmpXorVarCount; @@ -460,6 +461,7 @@ app * theory_str::mk_internal_xor_var() { std::string name = "$$_xor_" + ss.str(); // Z3_sort r = of_sort(mk_c(c)->m().mk_sort(mk_c(c)->get_arith_fid(), INT_SORT)); sort * int_sort = m.mk_sort(m_autil.get_family_id(), INT_SORT); + char * new_buffer = alloc_svect(char, name.length() + 1); strcpy(new_buffer, name.c_str()); symbol sym(new_buffer); @@ -467,6 +469,8 @@ app * theory_str::mk_internal_xor_var() { app * a = m.mk_const(m.mk_const_decl(sym, int_sort)); m_trail.push_back(a); return a; + */ + return mk_int_var("$$_xor"); } app * theory_str::mk_int_var(std::string name) { From ad7247df51042eba8d509e8d9cd801ae21720e15 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 14 Sep 2016 19:32:14 -0400 Subject: [PATCH 221/562] make calls to theory_str::dump_assignments depend on the correct trace flags --- src/smt/theory_str.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 34ac58d18..dbf5d2f38 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -6485,7 +6485,7 @@ void theory_str::push_scope_eh() { sLevel += 1; TRACE("t_str", tout << "push to " << sLevel << std::endl;); - TRACE("t_str_dump_assign_on_scope_change", dump_assignments();); + TRACE_CODE(dump_assignments();); } void theory_str::recursive_check_variable_scope(expr * ex) { @@ -6544,7 +6544,7 @@ void theory_str::check_variable_scope() { void theory_str::pop_scope_eh(unsigned num_scopes) { sLevel -= num_scopes; TRACE("t_str", tout << "pop " << num_scopes << " to " << sLevel << std::endl;); - TRACE("t_str_dump_assign_on_scope_change", dump_assignments();); + TRACE_CODE(dump_assignments();); // list of expr* to remove from cut_var_map ptr_vector cutvarmap_removes; @@ -6615,7 +6615,7 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { void theory_str::dump_assignments() { ast_manager & m = get_manager(); context & ctx = get_context(); - TRACE("t_str_detail", + TRACE("t_str_dump_assign_on_scope_change", tout << "dumping all assignments:" << std::endl; expr_ref_vector assignments(m); ctx.get_assignments(assignments); @@ -7503,7 +7503,7 @@ final_check_status theory_str::final_check_eh() { } TRACE("t_str", tout << "final check" << std::endl;); - TRACE("t_str_dump_assign", dump_assignments();); + TRACE_CODE(dump_assignments();); check_variable_scope(); if (opt_DeferEQCConsistencyCheck) { From bed40c45b80d83ed7ad82ef14ec92f3b8352854b Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 14 Sep 2016 21:48:27 -0400 Subject: [PATCH 222/562] cleanup --- src/smt/theory_str.cpp | 10 ++++++---- src/smt/theory_str.h | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index dbf5d2f38..1dcfb0b29 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2161,6 +2161,7 @@ expr * theory_str::simplify_concat(expr * node) { if (in_same_eqc(node, resultAst)) { TRACE("t_str_detail", tout << "SKIP: both concats are already in the same equivalence class" << std::endl;); } else { + // TODO refactor expr ** items = alloc_svect(expr*, resolvedMap.size()); int pos = 0; std::map::iterator itor = resolvedMap.begin(); @@ -2459,8 +2460,8 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { } } - expr * new_nn1 = simplify_concat(nn1); - expr * new_nn2 = simplify_concat(nn2); + expr_ref new_nn1(simplify_concat(nn1), m); + expr_ref new_nn2(simplify_concat(nn2), m); app * a_new_nn1 = to_app(new_nn1); app * a_new_nn2 = to_app(new_nn2); @@ -5466,8 +5467,6 @@ bool theory_str::check_length_concat_concat(expr * n1, expr * n2) { items.push_back(ctx.mk_eq_atom(n1, n2)); - expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); - bool conflict = false; if (concat1LenFixed && concat2LenFixed) { @@ -5486,6 +5485,7 @@ bool theory_str::check_length_concat_concat(expr * n1, expr * n2) { if (conflict) { TRACE("t_str_detail", tout << "inconsistent length detected in concat <==> concat" << std::endl;); + expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); assert_axiom(toAssert); return false; } @@ -6619,10 +6619,12 @@ void theory_str::dump_assignments() { tout << "dumping all assignments:" << std::endl; expr_ref_vector assignments(m); ctx.get_assignments(assignments); + /* for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { expr * ex = *i; tout << mk_ismt2_pp(ex, m) << (ctx.is_relevant(ex) ? "" : " (NOT REL)") << std::endl; } + */ ); } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 745d22ac2..7af6ab1ca 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -29,6 +29,8 @@ Revision History: #include"str_rewriter.h" #include"union_find.h" +// TODO refactor: anything that returns an expr* instead returns an expr_ref + namespace smt { class str_value_factory : public value_factory { From 8776b97841c80e476bf18dc32304c6efb5146050 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 14 Sep 2016 22:08:40 -0400 Subject: [PATCH 223/562] variable scope correctness hack in theory_str --- src/smt/theory_str.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 1dcfb0b29..d73d55dc3 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -6483,6 +6483,12 @@ void theory_str::push_scope_eh() { theory::push_scope_eh(); m_trail_stack.push_scope(); + // TODO out-of-scope term debugging, see comment in pop_scope_eh() + context & ctx = get_context(); + ast_manager & m = get_manager(); + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + sLevel += 1; TRACE("t_str", tout << "push to " << sLevel << std::endl;); TRACE_CODE(dump_assignments();); @@ -6544,6 +6550,12 @@ void theory_str::check_variable_scope() { void theory_str::pop_scope_eh(unsigned num_scopes) { sLevel -= num_scopes; TRACE("t_str", tout << "pop " << num_scopes << " to " << sLevel << std::endl;); + // TODO: figure out what's going out of scope and why + context & ctx = get_context(); + ast_manager & m = get_manager(); + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + TRACE_CODE(dump_assignments();); // list of expr* to remove from cut_var_map @@ -7500,6 +7512,10 @@ final_check_status theory_str::final_check_eh() { context & ctx = get_context(); ast_manager & m = get_manager(); + // TODO out-of-scope term debugging, see comment in pop_scope_eh() + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + if (opt_VerifyFinalCheckProgress) { finalCheckProgressIndicator = false; } From e7c0c29ae5675afbac72b38ceab0f6e28b7ef525 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 15 Sep 2016 15:59:56 -0400 Subject: [PATCH 224/562] potentially fix out-of-scope infinite loop bug in theory_str gen_unroll_conditional_options --- src/smt/theory_str.cpp | 57 +++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index d73d55dc3..43d14ccf1 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -504,6 +504,7 @@ app * theory_str::mk_unroll_bound_var() { app * theory_str::mk_unroll_test_var() { app * v = mk_str_var("unrollTest"); // was uRt internal_unrollTest_vars.insert(v); + track_variable_scope(v); return v; } @@ -6595,6 +6596,7 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { variable_set.erase(*var_it); internal_variable_set.erase(*var_it); regex_variable_set.erase(*var_it); + internal_unrollTest_vars.erase(*var_it); count += 1; } TRACE("t_str_detail", tout << "cleaned up " << count << " variables" << std::endl;); @@ -8349,13 +8351,35 @@ expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & litems.push_back(item); } + // handle out-of-scope entries in unroll_tries_map + + ptr_vector outOfScopeTesters; + // TODO refactor unroll_tries_map and internal_unrollTest_vars to use m_trail_stack + + for (ptr_vector::iterator it = unroll_tries_map[var][unrolls].begin(); + it != unroll_tries_map[var][unrolls].end(); ++it) { + expr * tester = *it; + bool inScope = (internal_unrollTest_vars.find(tester) != internal_unrollTest_vars.end()); + TRACE("t_str_detail", tout << "unroll test var " << mk_pp(tester, mgr) + << (inScope ? " in scope" : " out of scope") + << std::endl;); + if (!inScope) { + outOfScopeTesters.push_back(tester); + } + } + + for (ptr_vector::iterator it = outOfScopeTesters.begin(); + it != outOfScopeTesters.end(); ++it) { + unroll_tries_map[var][unrolls].erase(*it); + } + + if (unroll_tries_map[var][unrolls].size() == 0) { unroll_tries_map[var][unrolls].push_back(mk_unroll_test_var()); } int tries = unroll_tries_map[var][unrolls].size(); for (int i = 0; i < tries; i++) { - // TODO possibly missing a scope check here expr * tester = unroll_tries_map[var][unrolls][i]; bool testerHasValue = false; expr * testerVal = get_eqc_value(tester, testerHasValue); @@ -8377,6 +8401,9 @@ expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & m_trail.push_back(toAssert); return toAssert; + // note: this is how the code looks in Z3str2's strRegex.cpp:genUnrollConditionalOptions. + // the return is in the same place + // insert [tester = "more"] to litems so that the implyL for next tester is correct litems.push_back(ctx.mk_eq_atom(tester, moreAst)); } else { @@ -8881,21 +8908,21 @@ void theory_str::process_free_var(std::map & freeVar_map) { * and constant string in eqc of node n */ void theory_str::get_eqc_allUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet) { - constStr = NULL; - unrollFuncSet.clear(); - context & ctx = get_context(); + constStr = NULL; + unrollFuncSet.clear(); + context & ctx = get_context(); - expr * curr = n; - do { - if (is_string(to_app(curr))) { - constStr = curr; - } else if (is_Unroll(to_app(curr))) { - if (unrollFuncSet.find(curr) == unrollFuncSet.end()) { - unrollFuncSet.insert(curr); - } - } - curr = get_eqc_next(curr); - } while (curr != n); + expr * curr = n; + do { + if (is_string(to_app(curr))) { + constStr = curr; + } else if (is_Unroll(to_app(curr))) { + if (unrollFuncSet.find(curr) == unrollFuncSet.end()) { + unrollFuncSet.insert(curr); + } + } + curr = get_eqc_next(curr); + } while (curr != n); } // Collect simple Unroll functions (whose core is Str2Reg) and constant strings in the EQC of n. From 91b625768c4ef1c202817e9c1cff66f6c34b2f15 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 15 Sep 2016 17:01:59 -0400 Subject: [PATCH 225/562] fix tracing in theory_str --- src/smt/theory_str.cpp | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 43d14ccf1..22df46980 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -148,7 +148,7 @@ void theory_str::assert_axiom(expr * e) { TRACE("t_str_detail", tout << "asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); context & ctx = get_context(); if (!ctx.b_internalized(e)) { - ctx.internalize(e, true); + ctx.internalize(e, false); } literal lit(ctx.get_literal(e)); ctx.mark_as_relevant(lit); @@ -6492,7 +6492,7 @@ void theory_str::push_scope_eh() { sLevel += 1; TRACE("t_str", tout << "push to " << sLevel << std::endl;); - TRACE_CODE(dump_assignments();); + TRACE_CODE(if (is_trace_enabled("t_str_dump_assign_on_scope_change")) { dump_assignments(); }); } void theory_str::recursive_check_variable_scope(expr * ex) { @@ -6557,7 +6557,7 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { expr_ref_vector assignments(m); ctx.get_assignments(assignments); - TRACE_CODE(dump_assignments();); + TRACE_CODE(if (is_trace_enabled("t_str_dump_assign_on_scope_change")) { dump_assignments(); }); // list of expr* to remove from cut_var_map ptr_vector cutvarmap_removes; @@ -6627,18 +6627,16 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { } void theory_str::dump_assignments() { - ast_manager & m = get_manager(); - context & ctx = get_context(); - TRACE("t_str_dump_assign_on_scope_change", - tout << "dumping all assignments:" << std::endl; - expr_ref_vector assignments(m); - ctx.get_assignments(assignments); - /* - for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { - expr * ex = *i; - tout << mk_ismt2_pp(ex, m) << (ctx.is_relevant(ex) ? "" : " (NOT REL)") << std::endl; - } - */ + TRACE_CODE( + ast_manager & m = get_manager(); + context & ctx = get_context(); + tout << "dumping all assignments:" << std::endl; + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { + expr * ex = *i; + tout << mk_ismt2_pp(ex, m) << (ctx.is_relevant(ex) ? "" : " (NOT REL)") << std::endl; + } ); } @@ -7523,7 +7521,7 @@ final_check_status theory_str::final_check_eh() { } TRACE("t_str", tout << "final check" << std::endl;); - TRACE_CODE(dump_assignments();); + TRACE_CODE(if (is_trace_enabled("t_str_dump_assign")) { dump_assignments(); }); check_variable_scope(); if (opt_DeferEQCConsistencyCheck) { From c38f63dd2a6ddd2b19e2cd5bb75837ba08128728 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 19 Sep 2016 19:42:16 -0400 Subject: [PATCH 226/562] fix eqc management and unroll test var gen in theory_str::final_check --- src/smt/theory_str.cpp | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 22df46980..4ba9aa0ff 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -228,7 +228,7 @@ bool theory_str::internalize_term(app * term) { mk_var(e); return true; } - TRACE("t_str", tout << "internalizing term: " << mk_ismt2_pp(term, get_manager()) << std::endl;); + TRACE("t_str_detail", tout << "internalizing term: " << mk_ismt2_pp(term, get_manager()) << std::endl;); unsigned num_args = term->get_num_args(); expr* arg; for (unsigned i = 0; i < num_args; i++) { @@ -517,6 +517,9 @@ app * theory_str::mk_str_var(std::string name) { sort * string_sort = m.mk_sort(get_family_id(), STRING_SORT); app * a = m.mk_fresh_const(name.c_str(), string_sort); + TRACE("t_str_detail", tout << "a->get_family_id() = " << a->get_family_id() << std::endl + << "this->get_family_id() = " << this->get_family_id() << std::endl;); + // I have a hunch that this may not get internalized for free... ctx.internalize(a, false); SASSERT(ctx.get_enode(a) != NULL); @@ -6584,6 +6587,7 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { } } + /* // see if any internal variables went out of scope for (int check_level = sLevel + num_scopes ; check_level > sLevel; --check_level) { TRACE("t_str_detail", tout << "cleaning up internal variables at scope level " << check_level << std::endl;); @@ -6603,6 +6607,7 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { vars.clear(); } } + */ // TODO use the trail stack to do this for us! requires lots of refactoring // TODO if this works, possibly remove axioms from other vectors as well @@ -6623,7 +6628,7 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { m_trail_stack.pop_scope(num_scopes); theory::pop_scope_eh(num_scopes); - check_variable_scope(); + //check_variable_scope(); } void theory_str::dump_assignments() { @@ -6648,7 +6653,8 @@ void theory_str::classify_ast_by_type(expr * node, std::map & varMap // note that internal variables don't count if they're only length tester / value tester vars. if (variable_set.find(node) != variable_set.end() && internal_lenTest_vars.find(node) == internal_lenTest_vars.end() - && internal_valTest_vars.find(node) == internal_valTest_vars.end()) { + && internal_valTest_vars.find(node) == internal_valTest_vars.end() + && internal_unrollTest_vars.find(node) == internal_unrollTest_vars.end()) { if (varMap[node] != 1) { TRACE("t_str_detail", tout << "new variable: " << mk_pp(node, get_manager()) << std::endl;); } @@ -6988,10 +6994,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::mapget_next(); - curr = eqcNode->get_owner(); + curr = get_eqc_next(curr); } while (curr != varItor->first); } @@ -7017,8 +7020,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::mapget_next()->get_owner(); + expr * curr = get_eqc_next(deAliasNode); while (curr != deAliasNode) { app * aCurr = to_app(curr); // collect concat @@ -7055,9 +7057,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::mapget_next()->get_owner(); + curr = get_eqc_next(curr); } } @@ -7086,9 +7086,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::mapget_next()->get_owner(); + curr = get_eqc_next(curr); } while (curr != concatItor->first); } @@ -7121,9 +7119,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::mapget_next()->get_owner(); + curr = get_eqc_next(curr); } while (curr != deAliasConcat); } } From 9615b191dedc90f2ca939ff21e23a5e07333620b Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 19 Sep 2016 23:40:17 -0400 Subject: [PATCH 227/562] theory_str hacking for theory var stuff WIP --- src/smt/smt_context.cpp | 6 +++++ src/smt/theory_str.cpp | 53 +++++++++++++++++++++++++++++++++++++++-- src/smt/theory_str.h | 3 +++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 251cf3b9b..c712135d3 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -1691,6 +1691,12 @@ namespace smt { for (unsigned i = 0; i < m_th_eq_propagation_queue.size() && !inconsistent(); i++) { new_th_eq curr = m_th_eq_propagation_queue[i]; theory * th = get_theory(curr.m_th_id); + TRACE("t_str_eq_bug", tout + << "th->name = " << th->get_name() << std::endl + << "m_th_id = " << curr.m_th_id << std::endl + << "m_lhs = " << curr.m_lhs << std::endl + << "m_rhs = " << curr.m_rhs << std::endl + << std::endl;); SASSERT(th); th->new_eq_eh(curr.m_lhs, curr.m_rhs); #ifdef Z3DEBUG diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 4ba9aa0ff..8cd7c227c 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -200,6 +200,43 @@ bool theory_str::internalize_term(app * term) { ast_manager & m = get_manager(); SASSERT(term->get_family_id() == get_family_id()); + TRACE("t_str_detail", tout << "internalizing term: " << mk_ismt2_pp(term, get_manager()) << std::endl;); + + // emulation of user_smt_theory::internalize_term() + + unsigned num_args = term->get_num_args(); + for (unsigned i = 0; i < num_args; ++i) { + ctx.internalize(term->get_arg(i), false); + } + if (ctx.e_internalized(term)) { + enode * e = ctx.get_enode(term); + mk_var(e); + return true; + } + // m_parents.push_back(term); + enode * e = ctx.mk_enode(term, false, m.is_bool(term), true); + if (m.is_bool(term)) { + bool_var bv = ctx.mk_bool_var(term); + ctx.set_var_theory(bv, get_id()); + ctx.set_enode_flag(bv, true); + } + // make sure every argument is attached to a theory variable + for (unsigned i = 0; i < num_args; ++i) { + enode * arg = e->get_arg(i); + theory_var v_arg = mk_var(arg); + TRACE("t_str_detail", tout << "arg has theory var #" << v_arg << std::endl;); + } + + theory_var v = mk_var(e); + TRACE("t_str_detail", tout << "term has theory var #" << v << std::endl;); + + if (opt_EagerStringConstantLengthAssertions && m_strutil.is_string(term)) { + TRACE("t_str", tout << "eagerly asserting length of string term " << mk_pp(term, m) << std::endl;); + m_basicstr_axiom_todo.insert(e); + TRACE("t_str_axiom_bug", tout << "add " << mk_pp(e->get_owner(), m) << " to m_basicstr_axiom_todo" << std::endl;); + } + return true; + /* // what I had before SASSERT(!ctx.e_internalized(term)); @@ -223,6 +260,7 @@ bool theory_str::internalize_term(app * term) { // TODO do we still need to do instantiate_concat_axiom()? // partially from theory_seq::internalize_term() + /* if (ctx.e_internalized(term)) { enode* e = ctx.get_enode(term); mk_var(e); @@ -259,6 +297,7 @@ bool theory_str::internalize_term(app * term) { TRACE("t_str_detail", tout << "term " << mk_ismt2_pp(term, get_manager()) << " = v#" << v << std::endl;); return true; + */ } enode* theory_str::ensure_enode(expr* e) { @@ -271,7 +310,14 @@ enode* theory_str::ensure_enode(expr* e) { return n; } +void theory_str::refresh_theory_var(expr * e) { + enode * en = ensure_enode(e); + theory_var v = mk_var(en); + TRACE("t_str_detail", tout << "refresh " << mk_pp(e, get_manager()) << ": v#" << v << std::endl;); +} + theory_var theory_str::mk_var(enode* n) { + TRACE("t_str_detail", tout << "mk_var for " << mk_pp(n->get_owner(), get_manager()) << std::endl;); /* if (!m_strutil.is_string(n->get_owner())) { return null_theory_var; @@ -283,11 +329,12 @@ theory_var theory_str::mk_var(enode* n) { return null_theory_var; } if (is_attached_to_var(n)) { + TRACE("t_str_detail", tout << "already attached to theory var" << std::endl;); return n->get_th_var(get_id()); - } - else { + } else { theory_var v = theory::mk_var(n); m_find.mk_var(); + TRACE("t_str_detail", tout << "new theory var v#" << v << std::endl;); get_context().attach_th_var(n, this, v); get_context().mark_as_relevant(n); return v; @@ -8375,6 +8422,8 @@ expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & int tries = unroll_tries_map[var][unrolls].size(); for (int i = 0; i < tries; i++) { expr * tester = unroll_tries_map[var][unrolls][i]; + // TESTING + refresh_theory_var(tester); bool testerHasValue = false; expr * testerVal = get_eqc_value(tester, testerHasValue); if (!testerHasValue) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 7af6ab1ca..85209c631 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -487,6 +487,9 @@ namespace smt { void check_variable_scope(); void recursive_check_variable_scope(expr * ex); + // TESTING + void refresh_theory_var(expr * e); + public: theory_str(ast_manager & m); virtual ~theory_str(); From f1d7ffcdced6635717911f18c57d46b2af0c69bf Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 20 Sep 2016 00:14:38 -0400 Subject: [PATCH 228/562] fix regression regex-020 --- src/smt/theory_str.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 8cd7c227c..44a4b0d7c 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -7641,9 +7641,9 @@ final_check_status theory_str::final_check_eh() { get_eqc_value(*it, has_eqc_value); if (!has_eqc_value) { // if this is an internal variable, it can be ignored...I think - if (internal_variable_set.find(*it) != internal_variable_set.end()) { + if (internal_variable_set.find(*it) != internal_variable_set.end() || regex_variable_set.find(*it) != regex_variable_set.end()) { TRACE("t_str_detail", tout << "WARNING: free internal variable " << mk_ismt2_pp(*it, m) << std::endl;); - unused_internal_variables.insert(*it); + //unused_internal_variables.insert(*it); } else { needToAssignFreeVars = true; free_variables.insert(*it); From 447c6e4ce362a71f2c2b31f1a1eff7a2a4b91213 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 20 Sep 2016 00:28:29 -0400 Subject: [PATCH 229/562] refresh length tester in theory_str::gen_len_val_options_for_free_var fixes charAt-007.smt2 --- src/smt/theory_str.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 44a4b0d7c..9f0975609 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -8786,6 +8786,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe } else { // TODO make absolutely sure this is safe to do if 'indicator' is technically out of scope indicator = fvar_lenTester_map[freeVar][i]; + refresh_theory_var(indicator); testNum = i + 1; } expr * lenTestAssert = gen_len_test_options(freeVar, indicator, testNum); From 48eaa6159cc06f0935d78fe16916c9b147ee79d9 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 20 Sep 2016 01:10:27 -0400 Subject: [PATCH 230/562] disable aggressive unroll testing in theory_str, it may be doing more harm than good --- src/smt/theory_str.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 9f0975609..b2e0e70ba 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -32,7 +32,7 @@ theory_str::theory_str(ast_manager & m): /* Options */ opt_AggressiveLengthTesting(false), opt_AggressiveValueTesting(false), - opt_AggressiveUnrollTesting(true), + opt_AggressiveUnrollTesting(false), opt_EagerStringConstantLengthAssertions(true), opt_VerifyFinalCheckProgress(true), opt_LCMUnrollStep(2), From feef85c129aca77b1f160851681a61042c9f5a66 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 20 Sep 2016 15:37:29 -0400 Subject: [PATCH 231/562] override scope check in theory_str::solve_concat_eq_str fixes indexof2-009.smt2 --- src/smt/theory_str.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index b2e0e70ba..798e16e7c 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -5973,24 +5973,40 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { bool entry1InScope; if (entry1 == varForBreakConcat.end()) { + TRACE("t_str_detail", tout << "key1 no entry" << std::endl;); entry1InScope = false; } else { + // OVERRIDE. + entry1InScope = true; + TRACE("t_str_detail", tout << "key1 entry" << std::endl;); + /* if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end()) { + TRACE("t_str_detail", tout << "key1 entry not in scope" << std::endl;); entry1InScope = false; } else { + TRACE("t_str_detail", tout << "key1 entry in scope" << std::endl;); entry1InScope = true; } + */ } bool entry2InScope; if (entry2 == varForBreakConcat.end()) { + TRACE("t_str_detail", tout << "key2 no entry" << std::endl;); entry2InScope = false; } else { + // OVERRIDE. + entry2InScope = true; + TRACE("t_str_detail", tout << "key2 entry" << std::endl;); + /* if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end()) { + TRACE("t_str_detail", tout << "key2 entry not in scope" << std::endl;); entry2InScope = false; } else { + TRACE("t_str_detail", tout << "key2 entry in scope" << std::endl;); entry2InScope = true; } + */ } TRACE("t_str_detail", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl From 4433417b6ebdf34384e46462fbe5c2f647437200 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 20 Sep 2016 16:25:28 -0400 Subject: [PATCH 232/562] faster push_scope in theory_str --- src/smt/theory_str.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 798e16e7c..59db86212 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -32,7 +32,7 @@ theory_str::theory_str(ast_manager & m): /* Options */ opt_AggressiveLengthTesting(false), opt_AggressiveValueTesting(false), - opt_AggressiveUnrollTesting(false), + opt_AggressiveUnrollTesting(true), opt_EagerStringConstantLengthAssertions(true), opt_VerifyFinalCheckProgress(true), opt_LCMUnrollStep(2), @@ -6551,10 +6551,12 @@ void theory_str::push_scope_eh() { m_trail_stack.push_scope(); // TODO out-of-scope term debugging, see comment in pop_scope_eh() + /* context & ctx = get_context(); ast_manager & m = get_manager(); expr_ref_vector assignments(m); ctx.get_assignments(assignments); + */ sLevel += 1; TRACE("t_str", tout << "push to " << sLevel << std::endl;); From 1061cdf58ab4f3a95675fc13530df3b1aa259136 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 22 Sep 2016 15:40:43 -0400 Subject: [PATCH 233/562] fix value tester theory var reuse in theory_str fixes release regression in charAt-007 --- src/smt/theory_str.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 59db86212..ab6a9f229 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -8131,6 +8131,7 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, if (!anEqcHasValue) { TRACE("t_str_detail", tout << "value tester " << mk_ismt2_pp(aTester, m) << " doesn't have an equivalence class value." << std::endl;); + refresh_theory_var(aTester); expr * makeupAssert = gen_val_options(freeVar, len_indicator, aTester, len_valueStr, i); @@ -8147,6 +8148,7 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, expr * valTester = NULL; if (i + 1 < testerTotal) { valTester = fvar_valueTester_map[freeVar][len][i + 1].second; + refresh_theory_var(valTester); } else { valTester = mk_internal_valTest_var(freeVar, len, i + 1); valueTester_fvar_map[valTester] = freeVar; From dc8062ba6727e59dc789ba1a4e27c2fe48eecbb5 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 22 Sep 2016 20:14:42 -0400 Subject: [PATCH 234/562] patch out contains check for substr reduction fixes all regressions in release build, we may want to revisit this later --- src/smt/theory_str.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ab6a9f229..23b2af0fb 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1496,7 +1496,8 @@ void theory_str::instantiate_axiom_Substr(enode * e) { expr_ref ts0_contains_ts1(mk_contains(expr->get_arg(0), ts1), m); expr_ref_vector and_item(m); - and_item.push_back(ts0_contains_ts1); + // TODO simulate this contains check; it causes problems with a few regressions but we might need it for performance + //and_item.push_back(ts0_contains_ts1); and_item.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(ts0, mk_concat(ts1, ts2)))); and_item.push_back(ctx.mk_eq_atom(expr->get_arg(1), mk_strlen(ts0))); and_item.push_back(ctx.mk_eq_atom(expr->get_arg(2), mk_strlen(ts1))); From ce53b368647c27176d87aa20b97414b7cc7eddf4 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 14 Oct 2016 12:34:11 -0400 Subject: [PATCH 235/562] theory_str API started --- src/api/api_ast.cpp | 16 ++++++++ src/api/api_context.cpp | 2 + src/api/api_context.h | 6 +++ src/api/api_str.cpp | 80 +++++++++++++++++++++++++++++++++++++ src/api/z3_api.h | 57 ++++++++++++++++++++++++++ src/ast/str_decl_plugin.cpp | 1 + src/ast/str_decl_plugin.h | 5 +++ 7 files changed, 167 insertions(+) create mode 100644 src/api/api_str.cpp diff --git a/src/api/api_ast.cpp b/src/api/api_ast.cpp index 1f16b2d35..7774efcd9 100644 --- a/src/api/api_ast.cpp +++ b/src/api/api_ast.cpp @@ -647,6 +647,12 @@ extern "C" { else if (fid == mk_c(c)->get_seq_fid() && k == RE_SORT) { return Z3_RE_SORT; } + else if (fid == mk_c(c)->get_str_fid() && k == STRING_SORT) { + return Z3_STRING_SORT; + } + else if (fid == mk_c(c)->get_str_fid() && k == REGEX_SORT) { + return Z3_REGEX_SORT; + } else { return Z3_UNKNOWN_SORT; } @@ -1139,6 +1145,16 @@ extern "C" { } } + if (mk_c(c)->get_str_fid() == _d->get_family_id()) { + switch (_d->get_decl_kind()) { + // TODO(z3str2) add others + case OP_STRCAT: return Z3_OP_STR_CONCAT; + case OP_STRLEN: return Z3_OP_STR_LENGTH; + default: + return Z3_OP_UNINTERPRETED; + } + } + if (mk_c(c)->get_fpa_fid() == _d->get_family_id()) { switch (_d->get_decl_kind()) { case OP_FPA_RM_NEAREST_TIES_TO_EVEN: return Z3_OP_FPA_RM_NEAREST_TIES_TO_EVEN; diff --git a/src/api/api_context.cpp b/src/api/api_context.cpp index bc48874a7..8fbb02598 100644 --- a/src/api/api_context.cpp +++ b/src/api/api_context.cpp @@ -74,6 +74,7 @@ namespace api { m_fpa_util(m()), m_dtutil(m()), m_sutil(m()), + m_strutil(m()), m_last_result(m()), m_ast_trail(m()), m_pmanager(m_limit) { @@ -98,6 +99,7 @@ namespace api { m_datalog_fid = m().mk_family_id("datalog_relation"); m_fpa_fid = m().mk_family_id("fpa"); m_seq_fid = m().mk_family_id("seq"); + m_str_fid = m().mk_family_id("str"); m_dt_plugin = static_cast(m().get_plugin(m_dt_fid)); install_tactics(*this); diff --git a/src/api/api_context.h b/src/api/api_context.h index fa6754120..0f2104a2b 100644 --- a/src/api/api_context.h +++ b/src/api/api_context.h @@ -26,6 +26,7 @@ Revision History: #include"arith_decl_plugin.h" #include"bv_decl_plugin.h" #include"seq_decl_plugin.h" +#include"str_decl_plugin.h" #include"datatype_decl_plugin.h" #include"dl_decl_plugin.h" #include"fpa_decl_plugin.h" @@ -61,6 +62,8 @@ namespace api { datatype_util m_dtutil; seq_util m_sutil; + str_util m_strutil; + // Support for old solver API smt_params m_fparams; // ------------------------------- @@ -79,6 +82,7 @@ namespace api { family_id m_pb_fid; family_id m_fpa_fid; family_id m_seq_fid; + family_id m_str_fid; datatype_decl_plugin * m_dt_plugin; std::string m_string_buffer; // temporary buffer used to cache strings sent to the "external" world. @@ -123,6 +127,7 @@ namespace api { fpa_util & fpautil() { return m_fpa_util; } datatype_util& dtutil() { return m_dtutil; } seq_util& sutil() { return m_sutil; } + str_util& strutil() { return m_strutil; } family_id get_basic_fid() const { return m_basic_fid; } family_id get_array_fid() const { return m_array_fid; } family_id get_arith_fid() const { return m_arith_fid; } @@ -132,6 +137,7 @@ namespace api { family_id get_pb_fid() const { return m_pb_fid; } family_id get_fpa_fid() const { return m_fpa_fid; } family_id get_seq_fid() const { return m_seq_fid; } + family_id get_str_fid() const { return m_str_fid; } datatype_decl_plugin * get_dt_plugin() const { return m_dt_plugin; } Z3_error_code get_error_code() const { return m_error_code; } diff --git a/src/api/api_str.cpp b/src/api/api_str.cpp new file mode 100644 index 000000000..a42600f2f --- /dev/null +++ b/src/api/api_str.cpp @@ -0,0 +1,80 @@ +/*++ +Copyright (c) 2016 Microsoft Corporation + +Module Name: + + api_str.cpp + +Abstract: + + API for strings and regular expressions (Z3str2 implementation). + +Author: + + Murphy Berzish (mtrberzi) 2016-10-03. + +Revision History: + +--*/ +#include +#include"z3.h" +#include"api_log_macros.h" +#include"api_context.h" +#include"api_util.h" +#include"ast_pp.h" + +extern "C" { + + Z3_sort Z3_API Z3_mk_str_sort(Z3_context c) { + Z3_TRY; + LOG_Z3_mk_str_sort(c); + RESET_ERROR_CODE(); + sort * ty = mk_c(c)->strutil().mk_string_sort(); + mk_c(c)->save_ast_trail(ty); + RETURN_Z3(of_sort(ty)); + Z3_CATCH_RETURN(0); + } + + Z3_bool Z3_API Z3_is_str_sort(Z3_context c, Z3_sort s) { + Z3_TRY; + LOG_Z3_is_str_sort(c, s); + RESET_ERROR_CODE(); + bool result = mk_c(c)->strutil().is_str_sort(to_sort(s)); + return result?Z3_TRUE:Z3_FALSE; + Z3_CATCH_RETURN(Z3_FALSE); + } + + Z3_bool Z3_API Z3_is_str(Z3_context c, Z3_ast s) { + Z3_TRY; + LOG_Z3_is_str(c, s); + RESET_ERROR_CODE(); + bool result = mk_c(c)->strutil().is_string(to_expr(s)); + return result ? Z3_TRUE : Z3_FALSE; + Z3_CATCH_RETURN(Z3_FALSE); + } + + Z3_string Z3_API Z3_get_str(Z3_context c, Z3_ast s) { + Z3_TRY; + LOG_Z3_get_str(c, s); + RESET_ERROR_CODE(); + if (!mk_c(c)->strutil().is_string(to_expr(s))) { + SET_ERROR_CODE(Z3_INVALID_ARG); + return ""; + } + std::string result = mk_c(c)->strutil().get_string_constant_value(to_expr(s)); + return mk_c(c)->mk_external_string(result); + Z3_CATCH_RETURN(""); + } + + Z3_ast Z3_API Z3_mk_str(Z3_context c, Z3_string str) { + Z3_TRY; + LOG_Z3_mk_str(c, str); + RESET_ERROR_CODE(); + std::string s(str); + app * a = mk_c(c)->strutil().mk_string(str); + mk_c(c)->save_ast_trail(a); + RETURN_Z3(of_ast(a)); + Z3_CATCH_RETURN(0); + } + +}; diff --git a/src/api/z3_api.h b/src/api/z3_api.h index 114490015..7afba979e 100644 --- a/src/api/z3_api.h +++ b/src/api/z3_api.h @@ -165,6 +165,8 @@ typedef enum Z3_ROUNDING_MODE_SORT, Z3_SEQ_SORT, Z3_RE_SORT, + Z3_STRING_SORT, + Z3_REGEX_SORT, Z3_UNKNOWN_SORT = 1000 } Z3_sort_kind; @@ -1150,6 +1152,10 @@ typedef enum { Z3_OP_RE_CONCAT, Z3_OP_RE_UNION, + // theory_str + Z3_OP_STR_CONCAT, + Z3_OP_STR_LENGTH, + // Auxiliary Z3_OP_LABEL = 0x700, Z3_OP_LABEL_LIT, @@ -3145,6 +3151,57 @@ extern "C" { /*@}*/ + /** @name Strings and regular expressions (Z3str2 implementation) */ + /*@{*/ + + /** + \brief Create a string sort for 8-bit ASCII strings. + + This function creates a sort for ASCII strings. + Each character is 8 bits. + + def_API('Z3_mk_str_sort', SORT, (_in(CONTEXT), )) + */ + Z3_sort Z3_API Z3_mk_str_sort(Z3_context c); + + /** + \brief Check if \c s is a string sort. + + def_API('Z3_is_str_sort', BOOL, (_in(CONTEXT), _in(SORT))) + */ + + Z3_bool Z3_API Z3_is_str_sort(Z3_context c, Z3_sort s); + + /** + \brief Determine if \c s is a string constant. + + def_API('Z3_is_str', BOOL, (_in(CONTEXT), _in(AST))) + */ + + Z3_bool Z3_API Z3_is_str(Z3_context c, Z3_ast s); + + /** + \brief Retrieve the string constant stored in \c s. + + \pre Z3_is_str(c, s) + + def_API('Z3_get_str', STRING, (_in(CONTEXT), _in(AST))) + */ + + Z3_string Z3_API Z3_get_str(Z3_context c, Z3_ast s); + + /** + \brief Create a string constant. + + \param c logical context. + \param str The ASCII representation of the string constant. + + def_API('Z3_mk_str', AST, (_in(CONTEXT), _in(STRING))) + */ + Z3_ast Z3_API Z3_mk_str(Z3_context c, Z3_string str); + + /*@}*/ + /** @name Sequences and regular expressions */ /*@{*/ diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 08358d46b..aa12e5946 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -322,4 +322,5 @@ str_util::str_util(ast_manager &m) : m_manager(m) { SASSERT(m.has_plugin(symbol("str"))); m_plugin = static_cast(m.get_plugin(m.mk_family_id(symbol("str")))); + m_fid = m_plugin->get_family_id(); } diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 5b0ca2a3a..aa8204459 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -120,6 +120,8 @@ public: family_id get_fid() const { return m_afid; } family_id get_family_id() const { return get_fid(); } + bool is_str_sort(sort* s) const { return is_sort_of(s, m_afid, STRING_SORT); } + bool is_string(expr const * n, const char ** val) const; bool is_string(expr const * n) const; @@ -135,11 +137,14 @@ public: class str_util : public str_recognizers { ast_manager & m_manager; str_decl_plugin * m_plugin; + family_id m_fid; public: str_util(ast_manager & m); ast_manager & get_manager() const { return m_manager; } str_decl_plugin & plugin() { return *m_plugin; } + sort* mk_string_sort() const { return get_manager().mk_sort(m_fid, STRING_SORT, 0, 0); } + app * mk_string(const char * val) { return m_plugin->mk_string(val); } From d57c92f69e14db91d460e32b1430acf8c428adc2 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 20 Oct 2016 12:25:52 -0400 Subject: [PATCH 236/562] theory_str api: concat, length --- src/api/api_str.cpp | 3 +++ src/api/z3_api.h | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/api/api_str.cpp b/src/api/api_str.cpp index a42600f2f..e28c6a501 100644 --- a/src/api/api_str.cpp +++ b/src/api/api_str.cpp @@ -77,4 +77,7 @@ extern "C" { Z3_CATCH_RETURN(0); } + MK_BINARY(Z3_mk_str_concat, mk_c(c)->get_str_fid(), OP_STRCAT, SKIP); + MK_UNARY(Z3_mk_str_length, mk_c(c)->get_str_fid(), OP_STRLEN, SKIP); + }; diff --git a/src/api/z3_api.h b/src/api/z3_api.h index 7afba979e..c938678d6 100644 --- a/src/api/z3_api.h +++ b/src/api/z3_api.h @@ -3200,6 +3200,19 @@ extern "C" { */ Z3_ast Z3_API Z3_mk_str(Z3_context c, Z3_string str); + /** + \brief Create a string concatenation term. + def_API('Z3_mk_str_concat', AST, (_in(CONTEXT), _in(AST), _in(AST))) + */ + Z3_ast Z3_API Z3_mk_str_concat(Z3_context c, Z3_ast s1, Z3_ast s2); + + /** + \brief Create a string length term. (Integer representation) + def_API('Z3_mk_str_length', AST, (_in(CONTEXT), _in(AST))) + */ + Z3_ast Z3_API Z3_mk_str_length(Z3_context c, Z3_ast s); + + /*@}*/ /** @name Sequences and regular expressions */ From 05dfa5509a0263f48a29e33bf2ebc24a9590b472 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 20 Oct 2016 15:36:54 -0400 Subject: [PATCH 237/562] theory_str high-level and regex API --- src/api/api_str.cpp | 78 +++++++++++++++++++++++++++++++++++++++++ src/api/z3_api.h | 84 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) diff --git a/src/api/api_str.cpp b/src/api/api_str.cpp index e28c6a501..1a1debb5b 100644 --- a/src/api/api_str.cpp +++ b/src/api/api_str.cpp @@ -79,5 +79,83 @@ extern "C" { MK_BINARY(Z3_mk_str_concat, mk_c(c)->get_str_fid(), OP_STRCAT, SKIP); MK_UNARY(Z3_mk_str_length, mk_c(c)->get_str_fid(), OP_STRLEN, SKIP); + MK_BINARY(Z3_mk_str_at, mk_c(c)->get_str_fid(), OP_STR_CHARAT, SKIP); + // translate prefixof/suffixof to StartsWith/EndsWith + // TODO string standardization might just remove StartsWith/EndsWith in future + Z3_ast Z3_API Z3_mk_str_prefixof(Z3_context c, Z3_ast pre, Z3_ast full) { + LOG_Z3_mk_str_prefixof(c, pre, full); + Z3_TRY; + RESET_ERROR_CODE(); + expr * args[2] = { to_expr(full), to_expr(pre) }; // reverse args + ast * a = mk_c(c)->m().mk_app(mk_c(c)->get_str_fid(), OP_STR_STARTSWITH, 0, 0, 2, args); + mk_c(c)->save_ast_trail(a); + check_sorts(c, a); + RETURN_Z3(of_ast(a)); + Z3_CATCH_RETURN(0); + } + Z3_ast Z3_API Z3_mk_str_suffixof(Z3_context c, Z3_ast suf, Z3_ast full) { + LOG_Z3_mk_str_suffixof(c, suf, full); + Z3_TRY; + RESET_ERROR_CODE(); + expr * args[2] = { to_expr(full), to_expr(suf) }; // reverse args + ast * a = mk_c(c)->m().mk_app(mk_c(c)->get_str_fid(), OP_STR_ENDSWITH, 0, 0, 2, args); + mk_c(c)->save_ast_trail(a); + check_sorts(c, a); + RETURN_Z3(of_ast(a)); + Z3_CATCH_RETURN(0); + } + + MK_BINARY(Z3_mk_str_contains, mk_c(c)->get_str_fid(), OP_STR_CONTAINS, SKIP); + MK_TERNARY(Z3_mk_str_indexof, mk_c(c)->get_str_fid(), OP_STR_INDEXOF, SKIP); + MK_TERNARY(Z3_mk_str_substr, mk_c(c)->get_str_fid(), OP_STR_SUBSTR, SKIP); + MK_TERNARY(Z3_mk_str_replace, mk_c(c)->get_str_fid(), OP_STR_REPLACE, SKIP); + + Z3_ast Z3_API Z3_mk_str_to_regex(Z3_context c, Z3_string str) { + LOG_Z3_mk_str_to_regex(c, str); + Z3_TRY; + RESET_ERROR_CODE(); + std::string s(str); + app * a = mk_c(c)->strutil().mk_string(str); + mk_c(c)->save_ast_trail(a); + + expr * args[1] = { to_expr(a) }; + ast * re = mk_c(c)->m().mk_app(mk_c(c)->get_str_fid(), OP_RE_STR2REGEX, 0, 0, 1, args); + mk_c(c)->save_ast_trail(re); + check_sorts(c, re); + RETURN_Z3(of_ast(re)); + Z3_CATCH_RETURN(0); + } + + MK_BINARY(Z3_mk_str_in_regex, mk_c(c)->get_str_fid(), OP_RE_REGEXIN, SKIP); + MK_BINARY(Z3_mk_regex_concat, mk_c(c)->get_str_fid(), OP_RE_REGEXCONCAT, SKIP); + MK_BINARY(Z3_mk_regex_union, mk_c(c)->get_str_fid(), OP_RE_REGEXUNION, SKIP); + MK_UNARY(Z3_mk_regex_star, mk_c(c)->get_str_fid(), OP_RE_REGEXSTAR, SKIP); + MK_UNARY(Z3_mk_regex_plus, mk_c(c)->get_str_fid(), OP_RE_REGEXPLUS, SKIP); + + Z3_ast Z3_API Z3_mk_regex_range(Z3_context c, Z3_string start, Z3_string end) { + LOG_Z3_mk_regex_range(c, start, end); + Z3_TRY; + RESET_ERROR_CODE(); + + std::string cStart(start); + std::string cEnd(end); + if(cStart.length() != 1 || cEnd.length() != 1) { + SET_ERROR_CODE(Z3_INVALID_ARG); + return 0; + } + + app * a1 = mk_c(c)->strutil().mk_string(cStart); + mk_c(c)->save_ast_trail(a1); + app * a2 = mk_c(c)->strutil().mk_string(cEnd); + mk_c(c)->save_ast_trail(a2); + + expr * args[2] = { to_expr(a1), to_expr(a2) }; + ast * range = mk_c(c)->m().mk_app(mk_c(c)->get_str_fid(), OP_RE_REGEXCHARRANGE, 0, 0, 2, args); + mk_c(c)->save_ast_trail(range); + check_sorts(c, range); + RETURN_Z3(of_ast(range)); + + Z3_CATCH_RETURN(0); + } }; diff --git a/src/api/z3_api.h b/src/api/z3_api.h index c938678d6..7494bcb17 100644 --- a/src/api/z3_api.h +++ b/src/api/z3_api.h @@ -3212,6 +3212,90 @@ extern "C" { */ Z3_ast Z3_API Z3_mk_str_length(Z3_context c, Z3_ast s); + /** + \brief Create 'character at index' term. + def_API('Z3_mk_str_at', AST, (_in(CONTEXT), _in(AST), _in(AST))) + */ + Z3_ast Z3_API Z3_mk_str_at(Z3_context c, Z3_ast s, Z3_ast idx); + + /** + \brief Create 'str.prefixof' term. + def_API('Z3_mk_str_prefixof', AST, (_in(CONTEXT), _in(AST), _in(AST))) + */ + Z3_ast Z3_API Z3_mk_str_prefixof(Z3_context c, Z3_ast pre, Z3_ast full); + + /** + \brief Create 'str.suffixof' term. + def_API('Z3_mk_str_suffixof', AST, (_in(CONTEXT), _in(AST), _in(AST))) + */ + Z3_ast Z3_API Z3_mk_str_suffixof(Z3_context c, Z3_ast suf, Z3_ast full); + + /** + \brief Create 'str.contains' term. + def_API('Z3_mk_str_contains', AST, (_in(CONTEXT), _in(AST), _in(AST))) + */ + Z3_ast Z3_API Z3_mk_str_contains(Z3_context c, Z3_ast needle, Z3_ast haystack); + + /** + \brief Create 'str.indexof' term. + def_API('Z3_mk_str_indexof', AST, (_in(CONTEXT), _in(AST), _in(AST), _in(AST))) + */ + Z3_ast Z3_API Z3_mk_str_indexof(Z3_context c, Z3_ast haystack, Z3_ast needle, Z3_ast start); + + /** + \brief Create 'str.substr' term. + def_API('Z3_mk_str_substr', AST, (_in(CONTEXT), _in(AST), _in(AST), _in(AST))) + */ + Z3_ast Z3_API Z3_mk_str_substr(Z3_context c, Z3_ast s, Z3_ast start, Z3_ast count); + + /** + \brief Create 'str.replace' term. + def_API('Z3_mk_str_replace', AST, (_in(CONTEXT), _in(AST), _in(AST), _in(AST))) + */ + Z3_ast Z3_API Z3_mk_str_replace(Z3_context c, Z3_ast base, Z3_ast target, Z3_ast replacement); + + + /** + \brief Create a regular expression that matches the given string constant. + def_API('Z3_mk_str_to_regex', AST, (_in(CONTEXT), _in(STRING))) + */ + Z3_ast Z3_API Z3_mk_str_to_regex(Z3_context c, Z3_string str); + + /** + \brief Create a regular expression membership predicate. + def_API('Z3_mk_str_in_regex', AST, (_in(CONTEXT), _in(AST), _in(AST))) + */ + Z3_ast Z3_API Z3_mk_str_in_regex(Z3_context c, Z3_ast str, Z3_ast regex); + + /** + \brief Create a regex concatenation term. + def_API('Z3_mk_regex_concat', AST, (_in(CONTEXT), _in(AST), _in(AST))) + */ + Z3_ast Z3_API Z3_mk_regex_concat(Z3_context c, Z3_ast r1, Z3_ast r2); + + /** + \brief Create a regex union term. + def_API('Z3_mk_regex_union', AST, (_in(CONTEXT), _in(AST), _in(AST))) + */ + Z3_ast Z3_API Z3_mk_regex_union(Z3_context c, Z3_ast r1, Z3_ast r2); + + /** + \brief Create a regex Kleene star term. + def_API('Z3_mk_regex_star', AST, (_in(CONTEXT), _in(AST))) + */ + Z3_ast Z3_API Z3_mk_regex_star(Z3_context c, Z3_ast r); + + /** + \brief Create a regex plus term. + def_API('Z3_mk_regex_plus', AST, (_in(CONTEXT), _in(AST))) + */ + Z3_ast Z3_API Z3_mk_regex_plus(Z3_context c, Z3_ast r); + + /** + \brief Create a regex character range term. + def_API('Z3_mk_regex_range', AST, (_in(CONTEXT), _in(STRING), _in(STRING))) + */ + Z3_ast Z3_API Z3_mk_regex_range(Z3_context c, Z3_string start, Z3_string end); /*@}*/ From ef0f6f1de346ddac51a5ff1bb114120312bde0f1 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 20 Oct 2016 16:01:51 -0400 Subject: [PATCH 238/562] add str.to-int in theory_str WIP --- src/ast/rewriter/str_rewriter.cpp | 39 +++++++++++++++++++++++++++++++ src/ast/rewriter/str_rewriter.h | 1 + src/ast/str_decl_plugin.cpp | 7 ++++++ src/ast/str_decl_plugin.h | 3 +++ 4 files changed, 50 insertions(+) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index 015898a64..db3885f28 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -25,6 +25,7 @@ Notes: #include #include #include +#include // Convert a regular expression to an e-NFA using Thompson's construction void nfa::convert_re(expr * e, unsigned & start, unsigned & end, str_util & m_strutil) { @@ -374,6 +375,41 @@ br_status str_rewriter::mk_str_Replace(expr * base, expr * source, expr * target } } +br_status str_rewriter::mk_str_to_int(expr * arg0, expr_ref & result) { + TRACE("t_str_rw", tout << "rewrite (str.to-int " << mk_pp(arg0, m()) << ")" << std::endl;); + + if (m_strutil.is_string(arg0)) { + std::string str = m_strutil.get_string_constant_value(arg0); + if (str.length() == 0) { + result = m_autil.mk_numeral(rational::zero(), true); + return BR_DONE; + } + + // interpret str as a natural number and rewrite to the corresponding integer. + // if this is not valid, rewrite to -1 + // TODO leading zeroes? + rational convertedRepresentation(0); + rational ten(10); + for (unsigned i = 0; i < str.length(); ++i) { + char digit = str.at(i); + if (isdigit((int)digit)) { + std::string sDigit(1, digit); + int val = atoi(sDigit.c_str()); + convertedRepresentation = (ten * convertedRepresentation) + rational(val); + } else { + // not a digit, invalid + TRACE("t_str_rw", tout << "str.to-int argument contains non-digit character '" << digit << "'" << std::endl;); + convertedRepresentation = rational::minus_one(); + break; + } + } + result = m_autil.mk_numeral(convertedRepresentation, true); + return BR_DONE; + } + return BR_FAILED; + +} + br_status str_rewriter::mk_str_Substr(expr * base, expr * start, expr * len, expr_ref & result) { TRACE("t_str_rw", tout << "rewrite (Substr " << mk_pp(base, m()) << " " << mk_pp(start, m()) << " " << mk_pp(len, m()) << ")" << std::endl;); rational startVal, lenVal; @@ -520,6 +556,9 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con case OP_STR_REPLACE: SASSERT(num_args == 3); return mk_str_Replace(args[0], args[1], args[2], result); + case OP_STR_STR2INT: + SASSERT(num_args == 1); + return mk_str_to_int(args[0], result); case OP_STR_SUBSTR: SASSERT(num_args == 3); return mk_str_Substr(args[0], args[1], args[2], result); diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index d147e82e8..10898eae7 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -53,6 +53,7 @@ public: br_status mk_str_LastIndexof(expr * haystack, expr * needle, expr_ref & result); br_status mk_str_Replace(expr * base, expr * source, expr * target, expr_ref & result); br_status mk_str_Substr(expr * base, expr * start, expr * len, expr_ref & result); + br_status mk_str_to_int(expr * arg0, expr_ref & result); br_status mk_re_Str2Reg(expr * str, expr_ref & result); br_status mk_re_RegexIn(expr * str, expr * re, expr_ref & result); diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index aa12e5946..e8455ffbd 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -36,6 +36,7 @@ str_decl_plugin::str_decl_plugin(): m_lastindexof_decl(0), m_substr_decl(0), m_replace_decl(0), + m_str2int_decl(0), m_re_str2regex_decl(0), m_re_regexin_decl(0), m_re_regexconcat_decl(0), @@ -67,6 +68,7 @@ void str_decl_plugin::finalize(void) { DEC_REF(m_lastindexof_decl); DEC_REF(m_substr_decl); DEC_REF(m_replace_decl); + DEC_REF(m_str2int_decl); DEC_REF(m_re_str2regex_decl); DEC_REF(m_re_regexin_decl); DEC_REF(m_re_regexconcat_decl); @@ -145,6 +147,9 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_manager->inc_ref(m_replace_decl); } + m_str2int_decl = m->mk_func_decl(symbol("str.to-int"), s, i, func_decl_info(id, OP_STR_STR2INT)); + m_manager->inc_ref(m_str2int_decl); + m_re_str2regex_decl = m->mk_func_decl(symbol("Str2Reg"), s, re, func_decl_info(id, OP_RE_STR2REGEX)); m_manager->inc_ref(m_re_str2regex_decl); @@ -196,6 +201,7 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { case OP_STR_LASTINDEXOF: return m_lastindexof_decl; case OP_STR_SUBSTR: return m_substr_decl; case OP_STR_REPLACE: return m_replace_decl; + case OP_STR_STR2INT: return m_str2int_decl; case OP_RE_STR2REGEX: return m_re_str2regex_decl; case OP_RE_REGEXIN: return m_re_regexin_decl; case OP_RE_REGEXCONCAT: return m_re_regexconcat_decl; @@ -269,6 +275,7 @@ void str_decl_plugin::get_op_names(svector & op_names, symbol cons op_names.push_back(builtin_name("LastIndexof", OP_STR_LASTINDEXOF)); op_names.push_back(builtin_name("Substring", OP_STR_SUBSTR)); op_names.push_back(builtin_name("Replace", OP_STR_REPLACE)); + op_names.push_back(builtin_name("str.to-int", OP_STR_STR2INT)); op_names.push_back(builtin_name("Str2Reg", OP_RE_STR2REGEX)); op_names.push_back(builtin_name("RegexIn", OP_RE_REGEXIN)); op_names.push_back(builtin_name("RegexConcat", OP_RE_REGEXCONCAT)); diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index aa8204459..ba2b4f751 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -41,6 +41,8 @@ enum str_op_kind { OP_STR_LASTINDEXOF, OP_STR_SUBSTR, OP_STR_REPLACE, + // string-integer conversion + OP_STR_STR2INT, // regular expression operators OP_RE_STR2REGEX, OP_RE_REGEXIN, @@ -73,6 +75,7 @@ protected: func_decl * m_lastindexof_decl; func_decl * m_substr_decl; func_decl * m_replace_decl; + func_decl * m_str2int_decl; func_decl * m_re_str2regex_decl; func_decl * m_re_regexin_decl; From b06b9f9264ec75c3dcbe324d666e0dc3e61193fd Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 21 Oct 2016 13:35:35 -0400 Subject: [PATCH 239/562] str.to-int WIP --- src/smt/theory_str.cpp | 120 ++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 12 +++++ 2 files changed, 131 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 23b2af0fb..ffaf098f7 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -55,6 +55,7 @@ theory_str::theory_str(ast_manager & m): avoidLoopCut(true), loopDetected(false), contains_map(m), + string_int_conversion_terms(m), m_find(*this), m_trail_stack(*this) { @@ -821,7 +822,7 @@ bool theory_str::can_propagate() { || !m_axiom_CharAt_todo.empty() || !m_axiom_StartsWith_todo.empty() || !m_axiom_EndsWith_todo.empty() || !m_axiom_Contains_todo.empty() || !m_axiom_Indexof_todo.empty() || !m_axiom_Indexof2_todo.empty() || !m_axiom_LastIndexof_todo.empty() || !m_axiom_Substr_todo.empty() || !m_axiom_Replace_todo.empty() - || !m_axiom_RegexIn_todo.empty() + || !m_axiom_RegexIn_todo.empty() || !m_library_aware_axiom_todo.empty() || !m_delayed_axiom_setup_terms.empty(); ; } @@ -904,6 +905,17 @@ void theory_str::propagate() { } m_axiom_RegexIn_todo.reset(); + for (unsigned i = 0; i < m_library_aware_axiom_todo.size(); ++i) { + enode * e = m_library_aware_axiom_todo[i]; + if (is_str_to_int(e)) { + instantiate_axiom_str_to_int(e); + } else { + TRACE("t_str", tout << "BUG: unhandled library-aware term " << mk_pp(e->get_owner(), get_manager()) << std::endl;); + NOT_IMPLEMENTED_YET(); + } + } + m_library_aware_axiom_todo.reset(); + for (unsigned i = 0; i < m_delayed_axiom_setup_terms.size(); ++i) { // I think this is okay ctx.internalize(m_delayed_axiom_setup_terms[i].get(), false); @@ -1563,6 +1575,53 @@ void theory_str::instantiate_axiom_Replace(enode * e) { assert_axiom(finalAxiom); } +void theory_str::instantiate_axiom_str_to_int(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * ex = e->get_owner(); + if (axiomatized_terms.contains(ex)) { + TRACE("t_str_detail", tout << "already set up str.to-int axiom for " << mk_pp(ex, m) << std::endl;); + return; + } + axiomatized_terms.insert(ex); + + TRACE("t_str_detail", tout << "instantiate str.to-int axiom for " << mk_pp(ex, m) << std::endl;); + + // let expr = (str.to-int S) + // axiom 1: expr >= -1 + // axiom 2: expr = 0 <==> S = "0" + // axiom 3: expr >= 1 ==> len(S) > 0 AND S[0] != "0" + + expr * S = ex->get_arg(0); + { + expr_ref axiom1(m_autil.mk_ge(ex, m_autil.mk_numeral(rational::minus_one(), true)), m); + SASSERT(axiom1); + assert_axiom(axiom1); + } + + { + expr_ref lhs(ctx.mk_eq_atom(ex, m_autil.mk_numeral(rational::zero(), true)), m); + expr_ref rhs(ctx.mk_eq_atom(S, m_strutil.mk_string("0")), m); + expr_ref axiom2(ctx.mk_eq_atom(lhs, rhs), m); + SASSERT(axiom2); + assert_axiom(axiom2); + } + + { + expr_ref premise(m_autil.mk_ge(ex, m_autil.mk_numeral(rational::one(), true)), m); + expr_ref hd(mk_str_var("hd"), m); + expr_ref tl(mk_str_var("tl"), m); + expr_ref conclusion1(ctx.mk_eq_atom(S, mk_concat(hd, tl)), m); + expr_ref conclusion2(ctx.mk_eq_atom(mk_strlen(hd), m_autil.mk_numeral(rational::one(), true)), m); + expr_ref conclusion3(m.mk_not(ctx.mk_eq_atom(hd, m_strutil.mk_string("0"))), m); + expr_ref conclusion(m.mk_and(conclusion1, conclusion2, conclusion3), m); + SASSERT(premise); + SASSERT(conclusion); + assert_implication(premise, conclusion); + } +} + expr * theory_str::mk_RegexIn(expr * str, expr * regexp) { expr * args[2] = {str, regexp}; app * regexIn = get_manager().mk_app(get_id(), OP_RE_REGEXIN, 0, 0, 2, args); @@ -6438,6 +6497,9 @@ void theory_str::set_up_axioms(expr * ex) { m_axiom_Indexof2_todo.push_back(n); } else if (is_LastIndexof(ap)) { m_axiom_LastIndexof_todo.push_back(n); + } else if (is_str_to_int(ap)) { + string_int_conversion_terms.push_back(ap); + m_library_aware_axiom_todo.push_back(n); } } } else { @@ -7570,6 +7632,42 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::mapget_arg(0); + + // check integer theory + rational Ival; + bool Ival_exists = get_value(a, Ival); + if (Ival_exists) { + TRACE("t_str_detail", tout << "integer theory assigns " << mk_pp(a, m) << " = " << Ival.to_string() << std::endl;); + // if that value is not -1, we can assert (str.to-int S) = Ival --> S = "Ival" + if (!Ival.is_minus_one()) { + std::string Ival_str = Ival.to_string(); + expr_ref premise(ctx.mk_eq_atom(a, m_autil.mk_numeral(Ival, true)), m); + expr_ref conclusion(ctx.mk_eq_atom(S, m_strutil.mk_string(Ival_str)), m); + expr_ref axiom(m.mk_or(m.mk_not(premise), conclusion), m); + if (!string_int_axioms.contains(axiom)) { + string_int_axioms.insert(axiom); + assert_axiom(axiom); + m_trail_stack.push(insert_obj_trail(string_int_axioms, axiom)); + axiomAdd = true; + } + } + } else { + TRACE("t_str_detail", tout << "integer theory has no assignment for " << mk_pp(a, m) << std::endl;); + NOT_IMPLEMENTED_YET(); + } + // TODO also check assignment in string theory + + return axiomAdd; +} + final_check_status theory_str::final_check_eh() { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -7671,6 +7769,26 @@ final_check_status theory_str::final_check_eh() { } if (!needToAssignFreeVars) { + + // check string-int terms + bool addedStrIntAxioms = false; + for (unsigned i = 0; i < string_int_conversion_terms.size(); ++i) { + app * ex = to_app(string_int_conversion_terms[i].get()); + if (is_str_to_int(ex)) { + bool axiomAdd = finalcheck_str2int(ex); + if (axiomAdd) { + addedStrIntAxioms = true; + } + } else { + // TODO int.to-str + NOT_IMPLEMENTED_YET(); + } + } + if (addedStrIntAxioms) { + TRACE("t_str", tout << "Resuming search due to addition of string-integer conversion axioms." << std::endl;); + return FC_CONTINUE; + } + if (unused_internal_variables.empty()) { TRACE("t_str", tout << "All variables are assigned. Done!" << std::endl;); return FC_DONE; diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 85209c631..5fd2e980b 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -201,6 +201,9 @@ namespace smt { ptr_vector m_axiom_Replace_todo; ptr_vector m_axiom_RegexIn_todo; + // TODO refactor everything to use this worklist + ptr_vector m_library_aware_axiom_todo; + // hashtable of all exprs for which we've already set up term-specific axioms -- // this prevents infinite recursive descent with respect to axioms that // include an occurrence of the term for which axioms are being generated @@ -260,6 +263,10 @@ namespace smt { obj_pair_map concat_astNode_map; + // all (str.to-int) and (int.to-str) terms + expr_ref_vector string_int_conversion_terms; + obj_hashtable string_int_axioms; + th_union_find m_find; th_trail_stack m_trail_stack; theory_var get_var(expr * n) const; @@ -320,6 +327,8 @@ namespace smt { bool is_Substr(enode const * n) const { return is_Substr(n->get_owner()); } bool is_Replace(app const * a) const { return a->is_app_of(get_id(), OP_STR_REPLACE); } bool is_Replace(enode const * n) const { return is_Replace(n->get_owner()); } + bool is_str_to_int(app const * a) const { return a->is_app_of(get_id(), OP_STR_STR2INT); } + bool is_str_to_int(enode const * n) const { return is_str_to_int(n->get_owner()); } bool is_RegexIn(app const * a) const { return a->is_app_of(get_id(), OP_RE_REGEXIN); } bool is_RegexIn(enode const * n) const { return is_RegexIn(n->get_owner()); } @@ -348,6 +357,7 @@ namespace smt { void instantiate_axiom_LastIndexof(enode * e); void instantiate_axiom_Substr(enode * e); void instantiate_axiom_Replace(enode * e); + void instantiate_axiom_str_to_int(enode * e); expr * mk_RegexIn(expr * str, expr * regexp); void instantiate_axiom_RegexIn(enode * e); @@ -469,6 +479,8 @@ namespace smt { void get_const_str_asts_in_node(expr * node, expr_ref_vector & constList); expr * eval_concat(expr * n1, expr * n2); + bool finalcheck_str2int(app * a); + // strRegex void get_eqc_allUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet); From 452eed662603e658372712f37250fd3a1bde2832 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 29 Oct 2016 12:19:24 -0400 Subject: [PATCH 240/562] move get_std_regex_str to str_util --- src/ast/str_decl_plugin.cpp | 43 +++++++++++++++++++++++++++++++++++ src/ast/str_decl_plugin.h | 2 ++ src/smt/theory_str.cpp | 45 +------------------------------------ src/smt/theory_str.h | 1 - 4 files changed, 46 insertions(+), 45 deletions(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index e8455ffbd..333ae2a02 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -331,3 +331,46 @@ str_util::str_util(ast_manager &m) : m_plugin = static_cast(m.get_plugin(m.mk_family_id(symbol("str")))); m_fid = m_plugin->get_family_id(); } + +static std::string str2RegexStr(std::string str) { + std::string res = ""; + int len = str.size(); + for (int i = 0; i < len; i++) { + char nc = str[i]; + // 12 special chars + if (nc == '\\' || nc == '^' || nc == '$' || nc == '.' || nc == '|' || nc == '?' + || nc == '*' || nc == '+' || nc == '(' || nc == ')' || nc == '[' || nc == '{') { + res.append(1, '\\'); + } + res.append(1, str[i]); + } + return res; +} + +std::string str_util::get_std_regex_str(expr * regex) { + app * a_regex = to_app(regex); + if (is_re_Str2Reg(a_regex)) { + expr * regAst = a_regex->get_arg(0); + std::string regStr = str2RegexStr(get_string_constant_value(regAst)); + return regStr; + } else if (is_re_RegexConcat(a_regex)) { + expr * reg1Ast = a_regex->get_arg(0); + expr * reg2Ast = a_regex->get_arg(1); + std::string reg1Str = get_std_regex_str(reg1Ast); + std::string reg2Str = get_std_regex_str(reg2Ast); + return "(" + reg1Str + ")(" + reg2Str + ")"; + } else if (is_re_RegexUnion(a_regex)) { + expr * reg1Ast = a_regex->get_arg(0); + expr * reg2Ast = a_regex->get_arg(1); + std::string reg1Str = get_std_regex_str(reg1Ast); + std::string reg2Str = get_std_regex_str(reg2Ast); + return "(" + reg1Str + ")|(" + reg2Str + ")"; + } else if (is_re_RegexStar(a_regex)) { + expr * reg1Ast = a_regex->get_arg(0); + std::string reg1Str = get_std_regex_str(reg1Ast); + return "(" + reg1Str + ")*"; + } else { + TRACE("t_str", tout << "BUG: unrecognized regex term " << mk_pp(regex, get_manager()) << std::endl;); + UNREACHABLE(); return ""; + } +} diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index ba2b4f751..e9ab43865 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -182,6 +182,8 @@ public: return m_manager.mk_app(get_fid(), OP_RE_REGEXSTAR, 1, es); } + std::string get_std_regex_str(expr * regex); + }; #endif /* _STR_DECL_PLUGIN_H_ */ diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 23b2af0fb..1050e4a66 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1572,49 +1572,6 @@ expr * theory_str::mk_RegexIn(expr * str, expr * regexp) { return regexIn; } -static std::string str2RegexStr(std::string str) { - std::string res = ""; - int len = str.size(); - for (int i = 0; i < len; i++) { - char nc = str[i]; - // 12 special chars - if (nc == '\\' || nc == '^' || nc == '$' || nc == '.' || nc == '|' || nc == '?' - || nc == '*' || nc == '+' || nc == '(' || nc == ')' || nc == '[' || nc == '{') { - res.append(1, '\\'); - } - res.append(1, str[i]); - } - return res; -} - -std::string theory_str::get_std_regex_str(expr * regex) { - app * a_regex = to_app(regex); - if (is_Str2Reg(a_regex)) { - expr * regAst = a_regex->get_arg(0); - std::string regStr = str2RegexStr(m_strutil.get_string_constant_value(regAst)); - return regStr; - } else if (is_RegexConcat(a_regex)) { - expr * reg1Ast = a_regex->get_arg(0); - expr * reg2Ast = a_regex->get_arg(1); - std::string reg1Str = get_std_regex_str(reg1Ast); - std::string reg2Str = get_std_regex_str(reg2Ast); - return "(" + reg1Str + ")(" + reg2Str + ")"; - } else if (is_RegexUnion(a_regex)) { - expr * reg1Ast = a_regex->get_arg(0); - expr * reg2Ast = a_regex->get_arg(1); - std::string reg1Str = get_std_regex_str(reg1Ast); - std::string reg2Str = get_std_regex_str(reg2Ast); - return "(" + reg1Str + ")|(" + reg2Str + ")"; - } else if (is_RegexStar(a_regex)) { - expr * reg1Ast = a_regex->get_arg(0); - std::string reg1Str = get_std_regex_str(reg1Ast); - return "(" + reg1Str + ")*"; - } else { - TRACE("t_str", tout << "BUG: unrecognized regex term " << mk_pp(regex, get_manager()) << std::endl;); - UNREACHABLE(); return ""; - } -} - void theory_str::instantiate_axiom_RegexIn(enode * e) { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -1629,7 +1586,7 @@ void theory_str::instantiate_axiom_RegexIn(enode * e) { TRACE("t_str_detail", tout << "instantiate RegexIn axiom for " << mk_pp(ex, m) << std::endl;); { - std::string regexStr = get_std_regex_str(ex->get_arg(1)); + std::string regexStr = m_strutil.get_std_regex_str(ex->get_arg(1)); std::pair key1(ex->get_arg(0), regexStr); // skip Z3str's map check, because we already check if we set up axioms on this term regex_in_bool_map[key1] = ex; diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 85209c631..43552f31a 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -478,7 +478,6 @@ namespace smt { expr * gen_unroll_conditional_options(expr * var, std::set & unrolls, std::string lcmStr); expr * gen_unroll_assign(expr * var, std::string lcmStr, expr * testerVar, int l, int h); void reduce_virtual_regex_in(expr * var, expr * regex, expr_ref_vector & items); - std::string get_std_regex_str(expr * regex); void check_regex_in(expr * nn1, expr * nn2); void dump_assignments(); From 3da78f9d8015676b36e3869a3f9a665aa29f1d10 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 1 Nov 2016 20:35:01 -0400 Subject: [PATCH 241/562] experimental cached length testers in theory_str --- src/smt/theory_str.cpp | 24 +++++++++++++++++++++--- src/smt/theory_str.h | 12 ++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 1050e4a66..45b715247 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -40,6 +40,7 @@ theory_str::theory_str(ast_manager & m): opt_DisableIntegerTheoryIntegration(false), opt_DeferEQCConsistencyCheck(false), opt_CheckVariableScope(true), + opt_UseFastLengthTesterCache(true), /* Internal setup */ search_started(false), m_autil(m), @@ -8536,9 +8537,25 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr ); for (int i = l; i < h; ++i) { - std::string i_str = int_to_string(i); - expr_ref str_indicator(m_strutil.mk_string(i_str), m); - expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); // ARGUMENT 2 IS BOGUS! WRONG SORT + expr_ref str_indicator(m); + if (opt_UseFastLengthTesterCache) { + rational ri(i); + expr * lookup_val; + if(lengthTesterCache.find(ri, lookup_val)) { + str_indicator = expr_ref(lookup_val, m); + } else { + // no match; create and insert + std::string i_str = int_to_string(i); + expr_ref new_val(m_strutil.mk_string(i_str), m); + lengthTesterCache.insert(ri, new_val); + m_trail.push_back(new_val); + str_indicator = expr_ref(new_val, m); + } + } else { + std::string i_str = int_to_string(i); + expr_ref str_indicator(m_strutil.mk_string(i_str), m); + } + expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); orList.push_back(or_expr); if (opt_AggressiveLengthTesting) { @@ -8551,6 +8568,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr andList.push_back(and_expr); } + // TODO cache mk_string("more") orList.push_back(m.mk_eq(indicator, m_strutil.mk_string("more"))); if (opt_AggressiveLengthTesting) { literal l = mk_eq(indicator, m_strutil.mk_string("more"), false); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 43552f31a..9b41c583b 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -88,6 +88,8 @@ namespace smt { typedef trail_stack th_trail_stack; typedef union_find th_union_find; + typedef map, default_eq > rational_map; + protected: // Some options that control how the solver operates. @@ -167,6 +169,13 @@ namespace smt { */ bool opt_CheckVariableScope; + /* + * If UseFastLengthTesterCache is set to true, + * length tester terms will not be generated from scratch each time they are needed, + * but will be saved in a map and looked up. + */ + bool opt_UseFastLengthTesterCache; + bool search_started; arith_util m_autil; str_util m_strutil; @@ -260,6 +269,9 @@ namespace smt { obj_pair_map concat_astNode_map; + // used when opt_FastLengthTesterCache is true + rational_map lengthTesterCache; + th_union_find m_find; th_trail_stack m_trail_stack; theory_var get_var(expr * n) const; From a61e1f17e872075cc784e06de4061a5b24941d5e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 2 Nov 2016 12:35:14 -0400 Subject: [PATCH 242/562] fix crash in gen_len_test_options when fast length testers are disabled --- src/smt/theory_str.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 45b715247..5f024dead 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -8553,7 +8553,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr } } else { std::string i_str = int_to_string(i); - expr_ref str_indicator(m_strutil.mk_string(i_str), m); + str_indicator = expr_ref(m_strutil.mk_string(i_str), m); } expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); orList.push_back(or_expr); From 3ae336fa6f178aa41403140c98d061a2ea560411 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 2 Nov 2016 13:05:16 -0400 Subject: [PATCH 243/562] add experimental value tester caching to theory_str --- src/smt/theory_str.cpp | 13 ++++++++++++- src/smt/theory_str.h | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 5f024dead..c7cbff04e 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -41,6 +41,7 @@ theory_str::theory_str(ast_manager & m): opt_DeferEQCConsistencyCheck(false), opt_CheckVariableScope(true), opt_UseFastLengthTesterCache(true), + opt_UseFastValueTesterCache(true), /* Internal setup */ search_started(false), m_autil(m), @@ -7967,6 +7968,7 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * ptr_vector andList; for (long long i = l; i < h; i++) { + // TODO can we share the val_indicator constants with the length tester cache? orList.push_back(m.mk_eq(val_indicator, m_strutil.mk_string(longlong_to_string(i).c_str()) )); if (opt_AggressiveValueTesting) { literal l = mk_eq(val_indicator, m_strutil.mk_string(longlong_to_string(i).c_str()), false); @@ -7975,7 +7977,16 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * } std::string aStr = gen_val_string(len, options[i - l]); - expr * strAst = m_strutil.mk_string(aStr); + expr * strAst; + if (opt_UseFastValueTesterCache) { + if (!valueTesterCache.find(aStr, strAst)) { + strAst = m_strutil.mk_string(aStr); + valueTesterCache.insert(aStr, strAst); + m_trail.push_back(strAst); + } + } else { + strAst = m_strutil.mk_string(aStr); + } andList.push_back(m.mk_eq(orList[orList.size() - 1], m.mk_eq(freeVar, strAst))); } if (!coverAll) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 9b41c583b..b04e21fca 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -89,6 +89,12 @@ namespace smt { typedef union_find th_union_find; typedef map, default_eq > rational_map; + struct str_hash_proc { + unsigned operator()(std::string const & s) const { + return string_hash(s.c_str(), static_cast(s.length()), 17); + } + }; + typedef map > string_map; protected: // Some options that control how the solver operates. @@ -176,6 +182,13 @@ namespace smt { */ bool opt_UseFastLengthTesterCache; + /* + * If UseFastValueTesterCache is set to true, + * value tester terms will not be generated from scratch each time they are needed, + * but will be saved in a map and looked up. + */ + bool opt_UseFastValueTesterCache; + bool search_started; arith_util m_autil; str_util m_strutil; @@ -271,6 +284,8 @@ namespace smt { // used when opt_FastLengthTesterCache is true rational_map lengthTesterCache; + // used when opt_FastValueTesterCache is true + string_map valueTesterCache; th_union_find m_find; th_trail_stack m_trail_stack; From 521e0e175be66fcb5140ff3ac766ebb35ef3cd56 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 8 Nov 2016 14:23:10 -0500 Subject: [PATCH 244/562] refresh reused split vars in theory_str this fixes kaluza/unsat/big/7907, now SAT in ~30s --- src/smt/theory_str.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index c7cbff04e..a9a290ab1 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2726,6 +2726,9 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { t2 = varForBreakConcat[key2][1]; xorFlag = varForBreakConcat[key2][2]; } + // TODO do I need to refresh the xorFlag, which is an integer var, and if so, how? + refresh_theory_var(t1); + refresh_theory_var(t2); } // For split types 0 through 2, we can get away with providing @@ -3048,6 +3051,8 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { temp1 = varForBreakConcat[key2][0]; xorFlag = varForBreakConcat[key2][1]; } + // TODO refresh xorFlag? + refresh_theory_var(temp1); } int splitType = -1; @@ -3361,6 +3366,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { temp1 = varForBreakConcat[key2][0]; xorFlag = varForBreakConcat[key2][1]; } + refresh_theory_var(temp1); } @@ -3857,6 +3863,7 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { commonVar = (entry2->second)[0]; xorFlag = (entry2->second)[1]; } + refresh_theory_var(commonVar); } expr ** or_item = alloc_svect(expr*, (overlapLen.size() + 1)); From 61d1d5e8b0a87381ac429a06786c3db28c4e9aa2 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 8 Nov 2016 15:20:47 -0500 Subject: [PATCH 245/562] add cache for length terms to theory_str, but it seems to slow things down so I disabled it --- src/smt/theory_str.cpp | 17 +++++++++++++++-- src/smt/theory_str.h | 3 +++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index a9a290ab1..4e5e45ce7 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -696,8 +696,21 @@ app * theory_str::mk_strlen(expr * e) { int len = strlen(strval); return m_autil.mk_numeral(rational(len), true); } else { - expr * args[1] = {e}; - return get_manager().mk_app(get_id(), OP_STRLEN, 0, 0, 1, args); + if (false) { + // use cache + app * lenTerm = NULL; + if (!length_ast_map.find(e, lenTerm)) { + expr * args[1] = {e}; + lenTerm = get_manager().mk_app(get_id(), OP_STRLEN, 0, 0, 1, args); + length_ast_map.insert(e, lenTerm); + m_trail.push_back(lenTerm); + } + return lenTerm; + } else { + // always regen + expr * args[1] = {e}; + return get_manager().mk_app(get_id(), OP_STRLEN, 0, 0, 1, args); + } } } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index b04e21fca..48ebf049b 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -287,6 +287,9 @@ namespace smt { // used when opt_FastValueTesterCache is true string_map valueTesterCache; + // cache mapping each string S to Length(S) + obj_map length_ast_map; + th_union_find m_find; th_trail_stack m_trail_stack; theory_var get_var(expr * n) const; From fff1fadf3b4d4b84c8bf925fab82762182d0366f Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 9 Nov 2016 15:54:22 -0500 Subject: [PATCH 246/562] add str.from-int in theory_str rewriter --- src/ast/rewriter/str_rewriter.cpp | 22 ++++++++++++++++++++++ src/ast/rewriter/str_rewriter.h | 1 + src/ast/str_decl_plugin.cpp | 7 +++++++ src/ast/str_decl_plugin.h | 2 ++ 4 files changed, 32 insertions(+) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index db3885f28..875343655 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -410,6 +410,25 @@ br_status str_rewriter::mk_str_to_int(expr * arg0, expr_ref & result) { } +br_status str_rewriter::mk_str_from_int(expr * arg0, expr_ref & result) { + TRACE("t_str_rw", tout << "rewrite (str.from-int " << mk_pp(arg0, m()) << ")" << std::endl;); + rational arg0Int; + if (m_autil.is_numeral(arg0, arg0Int)) { + // (str.from-int N) with N non-negative is the corresponding string in decimal notation. + // otherwise it is the empty string + if (arg0Int.is_nonneg()) { + std::string str = arg0Int.to_string(); + result = m_strutil.mk_string(str); + TRACE("t_str_rw", tout << "convert non-negative integer constant to " << str << std::endl;); + } else { + result = m_strutil.mk_string(""); + TRACE("t_str_rw", tout << "convert invalid integer constant to empty string" << std::endl;); + } + return BR_DONE; + } + return BR_FAILED; +} + br_status str_rewriter::mk_str_Substr(expr * base, expr * start, expr * len, expr_ref & result) { TRACE("t_str_rw", tout << "rewrite (Substr " << mk_pp(base, m()) << " " << mk_pp(start, m()) << " " << mk_pp(len, m()) << ")" << std::endl;); rational startVal, lenVal; @@ -559,6 +578,9 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con case OP_STR_STR2INT: SASSERT(num_args == 1); return mk_str_to_int(args[0], result); + case OP_STR_INT2STR: + SASSERT(num_args == 1); + return mk_str_from_int(args[0], result); case OP_STR_SUBSTR: SASSERT(num_args == 3); return mk_str_Substr(args[0], args[1], args[2], result); diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index 10898eae7..822fb1ea8 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -54,6 +54,7 @@ public: br_status mk_str_Replace(expr * base, expr * source, expr * target, expr_ref & result); br_status mk_str_Substr(expr * base, expr * start, expr * len, expr_ref & result); br_status mk_str_to_int(expr * arg0, expr_ref & result); + br_status mk_str_from_int(expr * arg0, expr_ref & result); br_status mk_re_Str2Reg(expr * str, expr_ref & result); br_status mk_re_RegexIn(expr * str, expr * re, expr_ref & result); diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 333ae2a02..4f9dcb7aa 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -37,6 +37,7 @@ str_decl_plugin::str_decl_plugin(): m_substr_decl(0), m_replace_decl(0), m_str2int_decl(0), + m_int2str_decl(0), m_re_str2regex_decl(0), m_re_regexin_decl(0), m_re_regexconcat_decl(0), @@ -69,6 +70,7 @@ void str_decl_plugin::finalize(void) { DEC_REF(m_substr_decl); DEC_REF(m_replace_decl); DEC_REF(m_str2int_decl); + DEC_REF(m_int2str_decl); DEC_REF(m_re_str2regex_decl); DEC_REF(m_re_regexin_decl); DEC_REF(m_re_regexconcat_decl); @@ -150,6 +152,9 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_str2int_decl = m->mk_func_decl(symbol("str.to-int"), s, i, func_decl_info(id, OP_STR_STR2INT)); m_manager->inc_ref(m_str2int_decl); + m_int2str_decl = m->mk_func_decl(symbol("str.from-int"), i, s, func_decl_info(id, OP_STR_INT2STR)); + m_manager->inc_ref(m_int2str_decl); + m_re_str2regex_decl = m->mk_func_decl(symbol("Str2Reg"), s, re, func_decl_info(id, OP_RE_STR2REGEX)); m_manager->inc_ref(m_re_str2regex_decl); @@ -202,6 +207,7 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { case OP_STR_SUBSTR: return m_substr_decl; case OP_STR_REPLACE: return m_replace_decl; case OP_STR_STR2INT: return m_str2int_decl; + case OP_STR_INT2STR: return m_int2str_decl; case OP_RE_STR2REGEX: return m_re_str2regex_decl; case OP_RE_REGEXIN: return m_re_regexin_decl; case OP_RE_REGEXCONCAT: return m_re_regexconcat_decl; @@ -276,6 +282,7 @@ void str_decl_plugin::get_op_names(svector & op_names, symbol cons op_names.push_back(builtin_name("Substring", OP_STR_SUBSTR)); op_names.push_back(builtin_name("Replace", OP_STR_REPLACE)); op_names.push_back(builtin_name("str.to-int", OP_STR_STR2INT)); + op_names.push_back(builtin_name("str.from-int", OP_STR_INT2STR)); op_names.push_back(builtin_name("Str2Reg", OP_RE_STR2REGEX)); op_names.push_back(builtin_name("RegexIn", OP_RE_REGEXIN)); op_names.push_back(builtin_name("RegexConcat", OP_RE_REGEXCONCAT)); diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index e9ab43865..29b2ce9c3 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -43,6 +43,7 @@ enum str_op_kind { OP_STR_REPLACE, // string-integer conversion OP_STR_STR2INT, + OP_STR_INT2STR, // regular expression operators OP_RE_STR2REGEX, OP_RE_REGEXIN, @@ -76,6 +77,7 @@ protected: func_decl * m_substr_decl; func_decl * m_replace_decl; func_decl * m_str2int_decl; + func_decl * m_int2str_decl; func_decl * m_re_str2regex_decl; func_decl * m_re_regexin_decl; From 5635016205279623e2c00420934525c9fe22d801 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 9 Nov 2016 18:06:02 -0500 Subject: [PATCH 247/562] theory_str str.from-int very WIP --- src/smt/theory_str.cpp | 98 ++++++++++++++++++++++++++++++++++++++++-- src/smt/theory_str.h | 4 ++ 2 files changed, 98 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 40dc3f42f..881045815 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -924,6 +924,8 @@ void theory_str::propagate() { enode * e = m_library_aware_axiom_todo[i]; if (is_str_to_int(e)) { instantiate_axiom_str_to_int(e); + } else if (is_int_to_str(e)) { + instantiate_axiom_int_to_str(e); } else { TRACE("t_str", tout << "BUG: unhandled library-aware term " << mk_pp(e->get_owner(), get_manager()) << std::endl;); NOT_IMPLEMENTED_YET(); @@ -1637,6 +1639,30 @@ void theory_str::instantiate_axiom_str_to_int(enode * e) { } } +void theory_str::instantiate_axiom_int_to_str(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * ex = e->get_owner(); + if (axiomatized_terms.contains(ex)) { + TRACE("t_str_detail", tout << "already set up str.from-int axiom for " << mk_pp(ex, m) << std::endl;); + return; + } + axiomatized_terms.insert(ex); + + TRACE("t_str_detail", tout << "instantiate str.from-int axiom for " << mk_pp(ex, m) << std::endl;); + + // axiom 1: N < 0 <==> (str.from-int N) = "" + expr * N = ex->get_arg(0); + { + expr_ref axiom1_lhs(m.mk_not(m_autil.mk_ge(N, m_autil.mk_numeral(rational::zero(), true))), m); + expr_ref axiom1_rhs(ctx.mk_eq_atom(ex, m_strutil.mk_string("")), m); + expr_ref axiom1(ctx.mk_eq_atom(axiom1_lhs, axiom1_rhs), m); + SASSERT(axiom1); + assert_axiom(axiom1); + } +} + expr * theory_str::mk_RegexIn(expr * str, expr * regexp) { expr * args[2] = {str, regexp}; app * regexIn = get_manager().mk_app(get_id(), OP_RE_REGEXIN, 0, 0, 2, args); @@ -6476,7 +6502,7 @@ void theory_str::set_up_axioms(expr * ex) { m_axiom_Indexof2_todo.push_back(n); } else if (is_LastIndexof(ap)) { m_axiom_LastIndexof_todo.push_back(n); - } else if (is_str_to_int(ap)) { + } else if (is_str_to_int(ap) || is_int_to_str(ap)) { string_int_conversion_terms.push_back(ap); m_library_aware_axiom_todo.push_back(n); } @@ -7630,7 +7656,7 @@ bool theory_str::finalcheck_str2int(app * a) { std::string Ival_str = Ival.to_string(); expr_ref premise(ctx.mk_eq_atom(a, m_autil.mk_numeral(Ival, true)), m); expr_ref conclusion(ctx.mk_eq_atom(S, m_strutil.mk_string(Ival_str)), m); - expr_ref axiom(m.mk_or(m.mk_not(premise), conclusion), m); + expr_ref axiom(rewrite_implication(premise, conclusion), m); if (!string_int_axioms.contains(axiom)) { string_int_axioms.insert(axiom); assert_axiom(axiom); @@ -7647,6 +7673,66 @@ bool theory_str::finalcheck_str2int(app * a) { return axiomAdd; } +bool theory_str::finalcheck_int2str(app * a) { + bool axiomAdd = false; + context & ctx = get_context(); + ast_manager & m = get_manager(); + + expr * N = a->get_arg(0); + + // check string theory + bool Sval_expr_exists; + expr * Sval_expr = get_eqc_value(a, Sval_expr_exists); + if (Sval_expr_exists) { + std::string Sval = m_strutil.get_string_constant_value(Sval_expr); + TRACE("t_str_detail", tout << "string theory assigns \"" << mk_pp(a, m) << " = " << Sval << std::endl;); + // empty string --> integer value < 0 + if (Sval.empty()) { + // ignore this. we should already assert the axiom for what happens when the string is "" + } else { + // nonempty string --> convert to correct integer value, or disallow it + // TODO think about whether we need to persist the axiom in this case? + rational convertedRepresentation(0); + rational ten(10); + bool conversionOK = true; + for (unsigned i = 0; i < Sval.length(); ++i) { + char digit = Sval.at(i); + if (isdigit((int)digit)) { + std::string sDigit(1, digit); + int val = atoi(sDigit.c_str()); + convertedRepresentation = (ten * convertedRepresentation) + rational(val); + } else { + // not a digit, invalid + TRACE("t_str_rw", tout << "str.to-int argument contains non-digit character '" << digit << "'" << std::endl;); + conversionOK = false; + break; + } + } + if (conversionOK) { + expr_ref premise(ctx.mk_eq_atom(a, m_strutil.mk_string(Sval)), m); + expr_ref conclusion(ctx.mk_eq_atom(N, m_autil.mk_numeral(convertedRepresentation, true)), m); + expr_ref axiom(rewrite_implication(premise, conclusion), m); + if (!string_int_axioms.contains(axiom)) { + string_int_axioms.insert(axiom); + assert_axiom(axiom); + m_trail_stack.push(insert_obj_trail(string_int_axioms, axiom)); + axiomAdd = true; + } + } else { + expr_ref axiom(m.mk_not(ctx.mk_eq_atom(a, m_strutil.mk_string(Sval))), m); + // always assert this axiom because this is a conflict clause + assert_axiom(axiom); + axiomAdd = true; + } + } + } else { + TRACE("t_str_detail", tout << "string theory has no assignment for " << mk_pp(a, m) << std::endl;); + NOT_IMPLEMENTED_YET(); + } + // TODO also check assignment in integer theory + return axiomAdd; +} + final_check_status theory_str::final_check_eh() { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -7758,9 +7844,13 @@ final_check_status theory_str::final_check_eh() { if (axiomAdd) { addedStrIntAxioms = true; } + } else if (is_int_to_str(ex)) { + bool axiomAdd = finalcheck_int2str(ex); + if (axiomAdd) { + addedStrIntAxioms = true; + } } else { - // TODO int.to-str - NOT_IMPLEMENTED_YET(); + UNREACHABLE(); } } if (addedStrIntAxioms) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index e99774034..c7d931d1e 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -359,6 +359,8 @@ namespace smt { bool is_Replace(enode const * n) const { return is_Replace(n->get_owner()); } bool is_str_to_int(app const * a) const { return a->is_app_of(get_id(), OP_STR_STR2INT); } bool is_str_to_int(enode const * n) const { return is_str_to_int(n->get_owner()); } + bool is_int_to_str(app const * a) const { return a->is_app_of(get_id(), OP_STR_INT2STR); } + bool is_int_to_str(enode const * n) const { return is_int_to_str(n->get_owner()); } bool is_RegexIn(app const * a) const { return a->is_app_of(get_id(), OP_RE_REGEXIN); } bool is_RegexIn(enode const * n) const { return is_RegexIn(n->get_owner()); } @@ -388,6 +390,7 @@ namespace smt { void instantiate_axiom_Substr(enode * e); void instantiate_axiom_Replace(enode * e); void instantiate_axiom_str_to_int(enode * e); + void instantiate_axiom_int_to_str(enode * e); expr * mk_RegexIn(expr * str, expr * regexp); void instantiate_axiom_RegexIn(enode * e); @@ -510,6 +513,7 @@ namespace smt { expr * eval_concat(expr * n1, expr * n2); bool finalcheck_str2int(app * a); + bool finalcheck_int2str(app * a); // strRegex From fbaee080b202380b65a5df39c5c038ca68ed0442 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 11 Nov 2016 00:32:50 -0500 Subject: [PATCH 248/562] fix performance regression introduced with theory_str str.from-int more investigation is required to understand why this works. --- src/ast/str_decl_plugin.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 29b2ce9c3..8905d66bc 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -43,7 +43,7 @@ enum str_op_kind { OP_STR_REPLACE, // string-integer conversion OP_STR_STR2INT, - OP_STR_INT2STR, + OP_STR_INT2STR, OP_STR_PLACEHOLDER1, OP_STR_PLACEHOLDER2, // regular expression operators OP_RE_STR2REGEX, OP_RE_REGEXIN, From 02aacab04e58d5fd8e6f1c79beb8259a1e294b24 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 11 Nov 2016 17:52:18 -0500 Subject: [PATCH 249/562] add z3str2-style free variable check to theory_str --- src/smt/theory_str.cpp | 62 ++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 881045815..fa205ac32 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -7811,26 +7811,54 @@ final_check_status theory_str::final_check_eh() { return FC_DONE; } - // Check every variable to see if it's eq. to some string constant. - // If not, mark it as free. bool needToAssignFreeVars = false; std::set free_variables; std::set unused_internal_variables; - TRACE("t_str_detail", tout << variable_set.size() << " variables in variable_set" << std::endl;); - for (std::set::iterator it = variable_set.begin(); it != variable_set.end(); ++it) { - TRACE("t_str_detail", tout << "checking eqc of variable " << mk_ismt2_pp(*it, m) << std::endl;); - bool has_eqc_value = false; - get_eqc_value(*it, has_eqc_value); - if (!has_eqc_value) { - // if this is an internal variable, it can be ignored...I think - if (internal_variable_set.find(*it) != internal_variable_set.end() || regex_variable_set.find(*it) != regex_variable_set.end()) { - TRACE("t_str_detail", tout << "WARNING: free internal variable " << mk_ismt2_pp(*it, m) << std::endl;); - //unused_internal_variables.insert(*it); - } else { - needToAssignFreeVars = true; - free_variables.insert(*it); - } - } + if (true) { // Z3str2 free variables check + std::map::iterator itor = varAppearInAssign.begin(); + for (; itor != varAppearInAssign.end(); ++itor) { + /* + std::string vName = std::string(Z3_ast_to_string(ctx, itor->first)); + if (vName.length() >= 3 && vName.substr(0, 3) == "$$_") + continue; + */ + if (internal_variable_set.find(itor->first) != internal_variable_set.end() + || regex_variable_set.find(itor->first) != regex_variable_set.end()) { + // this can be ignored, I think + TRACE("t_str_detail", tout << "free internal variable " << mk_pp(itor->first, m) << " ignored" << std::endl;); + continue; + } + bool hasEqcValue = false; + expr * eqcString = get_eqc_value(itor->first, hasEqcValue); + if (!hasEqcValue) { + TRACE("t_str_detail", tout << "found free variable " << mk_pp(itor->first, m) << std::endl;); + needToAssignFreeVars = true; + free_variables.insert(itor->first); + // break; + } else { + // debug + TRACE("t_str_detail", tout << "variable " << mk_pp(itor->first, m) << " = " << mk_pp(eqcString, m) << std::endl;); + } + } + } else { // new, possibly incorrect free variables check + // Check every variable to see if it's eq. to some string constant. + // If not, mark it as free. + TRACE("t_str_detail", tout << variable_set.size() << " variables in variable_set" << std::endl;); + for (std::set::iterator it = variable_set.begin(); it != variable_set.end(); ++it) { + TRACE("t_str_detail", tout << "checking eqc of variable " << mk_ismt2_pp(*it, m) << std::endl;); + bool has_eqc_value = false; + get_eqc_value(*it, has_eqc_value); + if (!has_eqc_value) { + // if this is an internal variable, it can be ignored...I think + if (internal_variable_set.find(*it) != internal_variable_set.end() || regex_variable_set.find(*it) != regex_variable_set.end()) { + TRACE("t_str_detail", tout << "WARNING: free internal variable " << mk_ismt2_pp(*it, m) << std::endl;); + //unused_internal_variables.insert(*it); + } else { + needToAssignFreeVars = true; + free_variables.insert(*it); + } + } + } } if (!needToAssignFreeVars) { From df6b4611174ad4159ca816bd9f791d9c6448f15a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 14 Nov 2016 12:33:23 -0500 Subject: [PATCH 250/562] enhanced backpropagation in theory_str final_check for var=concat terms fixes kaluza sat/big/709.smt2 --- src/smt/theory_str.cpp | 47 ++++++++++++++++++++++++++++++++++++------ src/smt/theory_str.h | 2 +- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index fa205ac32..21a9ace97 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2028,7 +2028,6 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { expr_ref eq_ast2(m); eq_ast2 = ctx.mk_eq_atom(arg1, arg1Value); SASSERT(eq_ast2); - implyL = m.mk_and(eq_ast1, eq_ast2); } else { implyL = ctx.mk_eq_atom(n_eqNode, eq_str); @@ -2070,7 +2069,6 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { << "* |arg0| = " << rational_to_string_if_exists(arg0Len, arg0Len_exists) << std::endl << "* |arg1| = " << rational_to_string_if_exists(arg1Len, arg1Len_exists) << std::endl; ); - if (parentLen_exists && !arg0Len_exists) { TRACE("t_str_detail", tout << "make up len for arg0" << std::endl;); expr_ref implyL11(m.mk_and(ctx.mk_eq_atom(mk_strlen(a_parent), mk_int(parentLen)), @@ -2096,7 +2094,6 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { expr_ref eq_ast1(m); eq_ast1 = ctx.mk_eq_atom(n_eqNode, eq_str); SASSERT(eq_ast1); - expr_ref eq_ast2(m); eq_ast2 = ctx.mk_eq_atom(arg0, arg0Value); SASSERT(eq_ast2); @@ -7049,13 +7046,12 @@ void theory_str::trace_ctx_dep(std::ofstream & tout, * > should split the unroll function so that var2 and var3 are bounded by new unrolls */ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map & freeVarMap, - std::map > & unrollGroupMap) { + std::map > & unrollGroupMap, std::map > & var_eq_concat_map) { std::map concatMap; std::map unrollMap; std::map aliasIndexMap; std::map var_eq_constStr_map; std::map concat_eq_constStr_map; - std::map > var_eq_concat_map; std::map > var_eq_unroll_map; std::map > concat_eq_concat_map; std::map > depMap; @@ -7805,12 +7801,51 @@ final_check_status theory_str::final_check_eh() { std::map varAppearInAssign; std::map freeVar_map; std::map > unrollGroup_map; - int conflictInDep = ctx_dep_analysis(varAppearInAssign, freeVar_map, unrollGroup_map); + std::map > var_eq_concat_map; + int conflictInDep = ctx_dep_analysis(varAppearInAssign, freeVar_map, unrollGroup_map, var_eq_concat_map); if (conflictInDep == -1) { // return Z3_TRUE; return FC_DONE; } + // enhancement: improved backpropagation of string constants into var=concat terms + bool backpropagation_occurred = false; + for (std::map >::iterator veqc_map_it = var_eq_concat_map.begin(); + veqc_map_it != var_eq_concat_map.end(); ++veqc_map_it) { + expr * var = veqc_map_it->first; + for (std::map::iterator concat_map_it = veqc_map_it->second.begin(); + concat_map_it != veqc_map_it->second.end(); ++concat_map_it) { + app * concat = to_app(concat_map_it->first); + expr * concat_lhs = concat->get_arg(0); + expr * concat_rhs = concat->get_arg(1); + // If the concat LHS and RHS both have a string constant in their EQC, + // but the var does not, then we assert an axiom of the form + // (lhs = "lhs" AND rhs = "rhs") --> (Concat lhs rhs) = "lhsrhs" + bool concat_lhs_haseqc, concat_rhs_haseqc, var_haseqc; + expr * concat_lhs_str = get_eqc_value(concat_lhs, concat_lhs_haseqc); + expr * concat_rhs_str = get_eqc_value(concat_rhs, concat_rhs_haseqc); + expr * var_str = get_eqc_value(var, var_haseqc); + if (concat_lhs_haseqc && concat_rhs_haseqc && !var_haseqc) { + TRACE("t_str_detail", tout << "backpropagate into " << mk_pp(var, m) << " = " << mk_pp(concat, m) << std::endl + << "LHS ~= " << mk_pp(concat_lhs_str, m) << " RHS ~= " << mk_pp(concat_rhs_str, m) << std::endl;); + std::string lhsString = m_strutil.get_string_constant_value(concat_lhs_str); + std::string rhsString = m_strutil.get_string_constant_value(concat_rhs_str); + std::string concatString = lhsString + rhsString; + expr_ref lhs1(ctx.mk_eq_atom(concat_lhs, concat_lhs_str), m); + expr_ref lhs2(ctx.mk_eq_atom(concat_rhs, concat_rhs_str), m); + expr_ref lhs(m.mk_and(lhs1, lhs2), m); + expr_ref rhs(ctx.mk_eq_atom(concat, m_strutil.mk_string(concatString)), m); + assert_implication(lhs, rhs); + backpropagation_occurred = true; + } + } + } + + if (backpropagation_occurred) { + TRACE("t_str", tout << "Resuming search due to axioms added by backpropagation." << std::endl;); + return FC_CONTINUE; + } + bool needToAssignFreeVars = false; std::set free_variables; std::set unused_internal_variables; diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index c7d931d1e..5b8f644eb 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -473,7 +473,7 @@ namespace smt { void group_terms_by_eqc(expr * n, std::set & concats, std::set & vars, std::set & consts); int ctx_dep_analysis(std::map & strVarMap, std::map & freeVarMap, - std::map > & unrollGroupMap); + std::map > & unrollGroupMap, std::map > & var_eq_concat_map); void trace_ctx_dep(std::ofstream & tout, std::map & aliasIndexMap, std::map & var_eq_constStr_map, From 977142860042aaad55c80deda252b50d054ae524 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 15 Nov 2016 15:18:07 -0500 Subject: [PATCH 251/562] experimental modification to simplify_parent call in theory_str, WIP --- src/smt/theory_str.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 21a9ace97..3874f9f1d 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2267,19 +2267,12 @@ expr * theory_str::simplify_concat(expr * node) { if (in_same_eqc(node, resultAst)) { TRACE("t_str_detail", tout << "SKIP: both concats are already in the same equivalence class" << std::endl;); } else { - // TODO refactor - expr ** items = alloc_svect(expr*, resolvedMap.size()); - int pos = 0; + expr_ref_vector items(m); std::map::iterator itor = resolvedMap.begin(); for (; itor != resolvedMap.end(); ++itor) { - items[pos++] = ctx.mk_eq_atom(itor->first, itor->second); - } - expr_ref premise(m); - if (pos == 1) { - premise = items[0]; - } else { - premise = m.mk_and(pos, items); + items.push_back(ctx.mk_eq_atom(itor->first, itor->second)); } + expr_ref premise(mk_and(items), m); expr_ref conclusion(ctx.mk_eq_atom(node, resultAst), m); assert_implication(premise, conclusion); } @@ -6374,11 +6367,18 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { // we want the Z3str2 eqc check here... expr * nn1_value = z3str2_get_eqc_value(lhs, nn1HasEqcValue); expr * nn2_value = z3str2_get_eqc_value(rhs, nn2HasEqcValue); - if (nn1HasEqcValue && !nn2HasEqcValue) { + + + // modification from z3str2: simplify whenever we see a string constant on either side, + // not only when it's on one side but not the other. + // this may work in cases where a concat is simplified to a string constant in group_terms_by_eqc() + // and we fail to simplify parents because we think a string constant is on both sides + + if (nn1HasEqcValue /* && !nn2HasEqcValue */ ) { simplify_parent(rhs, nn1_value); } - if (!nn1HasEqcValue && nn2HasEqcValue) { + if (/* !nn1HasEqcValue && */ nn2HasEqcValue) { simplify_parent(lhs, nn2_value); } From 55ae83f47ebedd3fea1c04e4c39962fa80b97819 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 16 Nov 2016 13:00:05 -0500 Subject: [PATCH 252/562] Revert "experimental modification to simplify_parent call in theory_str, WIP" This reverts commit 977142860042aaad55c80deda252b50d054ae524. --- src/smt/theory_str.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 3874f9f1d..21a9ace97 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2267,12 +2267,19 @@ expr * theory_str::simplify_concat(expr * node) { if (in_same_eqc(node, resultAst)) { TRACE("t_str_detail", tout << "SKIP: both concats are already in the same equivalence class" << std::endl;); } else { - expr_ref_vector items(m); + // TODO refactor + expr ** items = alloc_svect(expr*, resolvedMap.size()); + int pos = 0; std::map::iterator itor = resolvedMap.begin(); for (; itor != resolvedMap.end(); ++itor) { - items.push_back(ctx.mk_eq_atom(itor->first, itor->second)); + items[pos++] = ctx.mk_eq_atom(itor->first, itor->second); + } + expr_ref premise(m); + if (pos == 1) { + premise = items[0]; + } else { + premise = m.mk_and(pos, items); } - expr_ref premise(mk_and(items), m); expr_ref conclusion(ctx.mk_eq_atom(node, resultAst), m); assert_implication(premise, conclusion); } @@ -6367,18 +6374,11 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { // we want the Z3str2 eqc check here... expr * nn1_value = z3str2_get_eqc_value(lhs, nn1HasEqcValue); expr * nn2_value = z3str2_get_eqc_value(rhs, nn2HasEqcValue); - - - // modification from z3str2: simplify whenever we see a string constant on either side, - // not only when it's on one side but not the other. - // this may work in cases where a concat is simplified to a string constant in group_terms_by_eqc() - // and we fail to simplify parents because we think a string constant is on both sides - - if (nn1HasEqcValue /* && !nn2HasEqcValue */ ) { + if (nn1HasEqcValue && !nn2HasEqcValue) { simplify_parent(rhs, nn1_value); } - if (/* !nn1HasEqcValue && */ nn2HasEqcValue) { + if (!nn1HasEqcValue && nn2HasEqcValue) { simplify_parent(lhs, nn2_value); } From e2d05578d62251d9c7630ffa6c965f29a664d07a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 17 Nov 2016 15:25:39 -0500 Subject: [PATCH 253/562] add extra trace message in smt_context for theory_str results change --- src/smt/smt_context.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index c712135d3..8958eae5f 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -3105,6 +3105,7 @@ namespace smt { theory_str * str = (theory_str*)th; if (str->overlapping_variables_detected()) { TRACE("t_str", tout << "WARNING: overlapping variables detected, UNSAT changed to UNKNOWN!" << std::endl;); + TRACE("context", tout << "WARNING: overlapping variables detected in theory_str. UNSAT changed to UNKNOWN!" << std::endl;); r = l_undef; } break; From d260218e2be6f1ece2140cfeaea9aa2e8cc177fe Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 17 Nov 2016 15:28:17 -0500 Subject: [PATCH 254/562] tabs to spaces test --- src/smt/theory_str.cpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 21a9ace97..39b221961 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -35,13 +35,13 @@ theory_str::theory_str(ast_manager & m): opt_AggressiveUnrollTesting(true), opt_EagerStringConstantLengthAssertions(true), opt_VerifyFinalCheckProgress(true), - opt_LCMUnrollStep(2), - opt_NoQuickReturn_IntegerTheory(false), - opt_DisableIntegerTheoryIntegration(false), - opt_DeferEQCConsistencyCheck(false), - opt_CheckVariableScope(true), - opt_UseFastLengthTesterCache(true), - opt_UseFastValueTesterCache(true), + opt_LCMUnrollStep(2), + opt_NoQuickReturn_IntegerTheory(false), + opt_DisableIntegerTheoryIntegration(false), + opt_DeferEQCConsistencyCheck(false), + opt_CheckVariableScope(true), + opt_UseFastLengthTesterCache(true), + opt_UseFastValueTesterCache(true), /* Internal setup */ search_started(false), m_autil(m), @@ -51,17 +51,17 @@ theory_str::theory_str(ast_manager & m): m_trail(m), m_delayed_axiom_setup_terms(m), tmpStringVarCount(0), - tmpXorVarCount(0), - tmpLenTestVarCount(0), - tmpValTestVarCount(0), - avoidLoopCut(true), - loopDetected(false), - contains_map(m), - string_int_conversion_terms(m), - m_find(*this), - m_trail_stack(*this) + tmpXorVarCount(0), + tmpLenTestVarCount(0), + tmpValTestVarCount(0), + avoidLoopCut(true), + loopDetected(false), + contains_map(m), + string_int_conversion_terms(m), + m_find(*this), + m_trail_stack(*this) { - initialize_charset(); + initialize_charset(); } theory_str::~theory_str() { From 855037eed765cd4462011fa2562d471fc9cd4bc6 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 17 Nov 2016 16:25:53 -0500 Subject: [PATCH 255/562] refactor process_concat_eq_type2 in theory_str; fixes unsat/big/8558 --- src/smt/theory_str.cpp | 83 ++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 44 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 39b221961..f92939ac7 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3180,28 +3180,28 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { if (can_two_nodes_eq(y, temp1_strAst)) { if (!avoidLoopCut || !(has_self_cut(m, y))) { // break down option 2-1 - expr ** l_items = alloc_svect(expr*, 3); - l_items[0] = ctx.mk_eq_atom(concatAst1, concatAst2); + expr_ref_vector l_items(mgr); + l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); - expr ** r_items = alloc_svect(expr*, 3); + expr_ref_vector r_items(mgr); expr_ref x_temp1(mk_concat(x, temp1), mgr); - r_items[0] = ctx.mk_eq_atom(m, x_temp1); - r_items[1] = ctx.mk_eq_atom(y, temp1_strAst); + r_items.push_back(ctx.mk_eq_atom(m, x_temp1)); + r_items.push_back(ctx.mk_eq_atom(y, temp1_strAst)); if (x_len_exists && m_len_exists) { - l_items[1] = ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len)); - l_items[2] = ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len)); + l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); + l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); rational m_sub_x = (m_len - x_len); - r_items[2] = ctx.mk_eq_atom(mk_strlen(temp1), mk_int(m_sub_x)); + r_items.push_back(ctx.mk_eq_atom(mk_strlen(temp1), mk_int(m_sub_x))); } else { - l_items[1] = ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len)); - l_items[2] = ctx.mk_eq_atom(mk_strlen(strAst), mk_int(str_len)); + l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); + l_items.push_back(ctx.mk_eq_atom(mk_strlen(strAst), mk_int(str_len))); rational y_sub_str = (y_len - str_len); - r_items[2] = ctx.mk_eq_atom(mk_strlen(temp1), mk_int(y_sub_str)); + r_items.push_back(ctx.mk_eq_atom(mk_strlen(temp1), mk_int(y_sub_str))); } - expr_ref ax_l(mgr.mk_and(3, l_items), mgr); - expr_ref ax_r(mgr.mk_and(3, r_items), mgr); + expr_ref ax_l(mk_and(l_items), mgr); + expr_ref ax_r(mk_and(r_items), mgr); add_cut_info_merge(temp1, sLevel, y); add_cut_info_merge(temp1, sLevel, m); @@ -3228,16 +3228,16 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { // | x | y | // | m | str | rational lenDelta; - expr ** l_items = alloc_svect(expr*, 3); + expr_ref_vector l_items(mgr); int l_count = 0; - l_items[0] = ctx.mk_eq_atom(concatAst1, concatAst2); + l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); if (x_len_exists && m_len_exists) { - l_items[1] = ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len)); - l_items[2] = ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len)); + l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); + l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); l_count = 3; lenDelta = x_len - m_len; } else { - l_items[1] = ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len)); + l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); l_count = 2; lenDelta = str_len - y_len; } @@ -3255,35 +3255,32 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { ; ); - TRACE("t_str", tout << "*** MARKER 1 ***" << std::endl;); std::string part1Str = strValue.substr(0, lenDelta.get_unsigned()); - TRACE("t_str", tout << "*** MARKER 2 ***" << std::endl;); std::string part2Str = strValue.substr(lenDelta.get_unsigned(), strValue.length() - lenDelta.get_unsigned()); - TRACE("t_str", tout << "*** MARKER 3 ***" << std::endl;); expr_ref prefixStr(m_strutil.mk_string(part1Str), mgr); expr_ref x_concat(mk_concat(m, prefixStr), mgr); expr_ref cropStr(m_strutil.mk_string(part2Str), mgr); if (can_two_nodes_eq(x, x_concat) && can_two_nodes_eq(y, cropStr)) { - expr ** r_items = alloc_svect(expr*, 2); - r_items[0] = ctx.mk_eq_atom(x, x_concat); - r_items[1] = ctx.mk_eq_atom(y, cropStr); - expr_ref ax_l(mgr.mk_and(l_count, l_items), mgr); - expr_ref ax_r(mgr.mk_and(2, r_items), mgr); + expr_ref_vector r_items(mgr); + r_items.push_back(ctx.mk_eq_atom(x, x_concat)); + r_items.push_back(ctx.mk_eq_atom(y, cropStr)); + expr_ref ax_l(mk_and(l_items), mgr); + expr_ref ax_r(mk_and(r_items), mgr); assert_implication(ax_l, ax_r); } else { // negate! It's impossible to split str with these lengths TRACE("t_str", tout << "CONFLICT: Impossible to split str with these lengths." << std::endl;); - expr_ref ax_l(mgr.mk_and(l_count, l_items), mgr); + expr_ref ax_l(mk_and(l_items), mgr); assert_axiom(mgr.mk_not(ax_l)); } } else { // Split type -1: no idea about the length... int optionTotal = 2 + strValue.length(); - expr ** or_item = alloc_svect(expr*, optionTotal); - expr ** and_item = alloc_svect(expr*, (1 + 6 + 4 * (strValue.length() + 1))); + expr_ref_vector or_item(mgr); + expr_ref_vector and_item(mgr); int option = 0; int pos = 1; @@ -3293,13 +3290,14 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { if (can_two_nodes_eq(y, temp1_strAst)) { if (!avoidLoopCut || !has_self_cut(m, y)) { // break down option 2-1 - or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); + expr_ref current_or_item_option(ctx.mk_eq_atom(xorFlag, mk_int(option)), mgr); + or_item.push_back(current_or_item_option); expr_ref x_temp1(mk_concat(x, temp1), mgr); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(m, x_temp1)); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(y, temp1_strAst)); + and_item.push_back(ctx.mk_eq_atom(current_or_item_option, ctx.mk_eq_atom(m, x_temp1))); + and_item.push_back(ctx.mk_eq_atom(current_or_item_option, ctx.mk_eq_atom(y, temp1_strAst))); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(m), - m_autil.mk_add(mk_strlen(x), mk_strlen(temp1)))); + and_item.push_back(ctx.mk_eq_atom(current_or_item_option, ctx.mk_eq_atom(mk_strlen(m), + m_autil.mk_add(mk_strlen(x), mk_strlen(temp1))))); ++option; add_cut_info_merge(temp1, ctx.get_scope_level(), y); @@ -3319,21 +3317,18 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { expr_ref cropStr(m_strutil.mk_string(part2Str), mgr); if (can_two_nodes_eq(x, x_concat) && can_two_nodes_eq(y, cropStr)) { // break down option 2-2 - or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(x, x_concat)); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(y, cropStr)); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(y), mk_int(part2Str.length()))); + expr_ref current_or_item_option(ctx.mk_eq_atom(xorFlag, mk_int(option)), mgr); + or_item.push_back(current_or_item_option); + and_item.push_back(ctx.mk_eq_atom(current_or_item_option, ctx.mk_eq_atom(x, x_concat))); + and_item.push_back(ctx.mk_eq_atom(current_or_item_option, ctx.mk_eq_atom(y, cropStr))); + and_item.push_back(ctx.mk_eq_atom(current_or_item_option, ctx.mk_eq_atom(mk_strlen(y), mk_int(part2Str.length())))); ++option; } } if (option > 0) { - if (option == 1) { - and_item[0] = or_item[0]; - } else { - and_item[0] = mgr.mk_or(option, or_item); - } - expr_ref implyR(mgr.mk_and(pos, and_item), mgr); + and_item.push_back(mk_or(or_item)); + expr_ref implyR(mk_and(and_item), mgr); assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); } else { TRACE("t_str", tout << "STOP: Should not split two EQ concats." << std::endl;); From 5e37a218025aa5ed1dd7cbf00fbdd47376931f81 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 18 Nov 2016 16:07:20 -0500 Subject: [PATCH 256/562] fix expr_ref in theory_str splits WIP --- src/smt/theory_str.cpp | 120 +++++++++++++++++++++-------------------- 1 file changed, 61 insertions(+), 59 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index f92939ac7..a34a6b8c1 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -23,6 +23,8 @@ Revision History: #include #include #include + +#include "../ast/ast.h" #include"theory_arith.h" namespace smt { @@ -2834,31 +2836,31 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { // len(x) < len(m) || len(y) > len(n) //-------------------------------------- if (!has_self_cut(m, y)) { - expr ** ax_l_items = alloc_svect(expr*, 3); - expr ** ax_r_items = alloc_svect(expr*, 3); + expr_ref_vector ax_l_items(mgr); + expr_ref_vector ax_r_items(mgr); - ax_l_items[0] = ctx.mk_eq_atom(concatAst1, concatAst2); + ax_l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); expr_ref x_t1(mk_concat(x, t1), mgr); expr_ref t1_n(mk_concat(t1, n), mgr); - ax_r_items[0] = ctx.mk_eq_atom(m, x_t1); - ax_r_items[1] = ctx.mk_eq_atom(y, t1_n); + ax_r_items.push_back(ctx.mk_eq_atom(m, x_t1)); + ax_r_items.push_back(ctx.mk_eq_atom(y, t1_n)); if (m_len_exists && x_len_exists) { - ax_l_items[1] = ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len)); - ax_l_items[2] = ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len)); + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); rational m_sub_x = m_len - x_len; - ax_r_items[2] = ctx.mk_eq_atom(mk_strlen(t1), mk_int(m_sub_x)); + ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t1), mk_int(m_sub_x))); } else { - ax_l_items[1] = ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len)); - ax_l_items[2] = ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len)); + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len))); rational y_sub_n = y_len - n_len; - ax_r_items[2] = ctx.mk_eq_atom(mk_strlen(t1), mk_int(y_sub_n)); + ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t1), mk_int(y_sub_n))); } - expr_ref ax_l(mgr.mk_and(3, ax_l_items), mgr); - expr_ref ax_r(mgr.mk_and(3, ax_r_items), mgr); + expr_ref ax_l(mk_and(ax_l_items), mgr); + expr_ref ax_r(mk_and(ax_r_items), mgr); // Cut Info add_cut_info_merge(t1, sLevel, m); @@ -2885,27 +2887,27 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { expr_ref m_t2(mk_concat(m, t2), mgr); expr_ref t2_y(mk_concat(t2, y), mgr); - expr ** ax_l_items = alloc_svect(expr*, 3); - ax_l_items[0] = ctx.mk_eq_atom(concatAst1, concatAst2); + expr_ref_vector ax_l_items(mgr); + ax_l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); - expr ** ax_r_items = alloc_svect(expr*, 3); - ax_r_items[0] = ctx.mk_eq_atom(x, m_t2); - ax_r_items[1] = ctx.mk_eq_atom(t2_y, n); + expr_ref_vector ax_r_items(mgr); + ax_r_items.push_back(ctx.mk_eq_atom(x, m_t2)); + ax_r_items.push_back(ctx.mk_eq_atom(t2_y, n)); if (m_len_exists && x_len_exists) { - ax_l_items[1] = ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len)); - ax_l_items[2] = ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len)); + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); rational x_sub_m = x_len - m_len; - ax_r_items[2] = ctx.mk_eq_atom(mk_strlen(t2), mk_int(x_sub_m)); + ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t2), mk_int(x_sub_m))); } else { - ax_l_items[1] = ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len)); - ax_l_items[2] = ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len)); + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len))); rational n_sub_y = n_len - y_len; - ax_r_items[2] = ctx.mk_eq_atom(mk_strlen(t2), mk_int(n_sub_y)); + ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t2), mk_int(n_sub_y))); } - expr_ref ax_l(mgr.mk_and(3, ax_l_items), mgr); - expr_ref ax_r(mgr.mk_and(3, ax_r_items), mgr); + expr_ref ax_l(mk_and(ax_l_items), mgr); + expr_ref ax_r(mk_and(ax_r_items), mgr); // Cut Info add_cut_info_merge(t2, sLevel, x); @@ -2919,8 +2921,8 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } } else if (splitType == -1) { // Here we don't really have a choice. We have no length information at all... - expr ** or_item = alloc_svect(expr*, 3); - expr ** and_item = alloc_svect(expr*, 20); + expr_ref_vector or_item(mgr); + expr_ref_vector and_item(mgr); int option = 0; int pos = 1; @@ -2928,28 +2930,29 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { // len(x) < len(m) || len(y) > len(n) if (!avoidLoopCut || !has_self_cut(m, y)) { // break down option 1-1 - expr * x_t1 = mk_concat(x, t1); - expr * t1_n = mk_concat(t1, n); - or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(m, x_t1)); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(y, t1_n)); + expr_ref x_t1(mk_concat(x, t1), mgr); + expr_ref t1_n(mk_concat(t1, n), mgr); + expr_ref or_item_option(ctx.mk_eq_atom(xorFlag, mk_int(option)), mgr); + or_item.push_back(or_item_option); + and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(m, x_t1))); + and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(y, t1_n))); expr_ref x_plus_t1(m_autil.mk_add(mk_strlen(x), mk_strlen(t1)), mgr); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(m), x_plus_t1)); + and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(mk_strlen(m), x_plus_t1))); // These were crashing the solver because the integer theory // expects a constant on the right-hand side. // The things we want to assert here are len(m) > len(x) and len(y) > len(n). // We rewrite A > B as A-B > 0 and then as not(A-B <= 0), // and then, *because we aren't allowed to use subtraction*, // as not(A + -1*B <= 0) - and_item[pos++] = ctx.mk_eq_atom(or_item[option], + and_item.push_back(ctx.mk_eq_atom(or_item_option, mgr.mk_not(m_autil.mk_le( m_autil.mk_add(mk_strlen(m), m_autil.mk_mul(mk_int(-1), mk_strlen(x))), - mk_int(0))) ); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], + mk_int(0))) )); + and_item.push_back(ctx.mk_eq_atom(or_item_option, mgr.mk_not(m_autil.mk_le( m_autil.mk_add(mk_strlen(y),m_autil.mk_mul(mk_int(-1), mk_strlen(n))), - mk_int(0))) ); + mk_int(0))) )); option++; @@ -2965,25 +2968,26 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { // x = m || y = n if (!avoidLoopCut || !has_self_cut(x, n)) { // break down option 1-2 - expr * m_t2 = mk_concat(m, t2); - expr * t2_y = mk_concat(t2, y); - or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(x, m_t2)); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(n, t2_y)); + expr_ref m_t2(mk_concat(m, t2), mgr); + expr_ref t2_y(mk_concat(t2, y), mgr); + expr_ref or_item_option(ctx.mk_eq_atom(xorFlag, mk_int(option)), mgr); + or_item.push_back(or_item_option); + and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(x, m_t2))); + and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(n, t2_y))); expr_ref m_plus_t2(m_autil.mk_add(mk_strlen(m), mk_strlen(t2)), mgr); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(x), m_plus_t2)); + and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(mk_strlen(x), m_plus_t2))); // want len(x) > len(m) and len(n) > len(y) - and_item[pos++] = ctx.mk_eq_atom(or_item[option], + and_item.push_back(ctx.mk_eq_atom(or_item_option, mgr.mk_not(m_autil.mk_le( m_autil.mk_add(mk_strlen(x), m_autil.mk_mul(mk_int(-1), mk_strlen(m))), - mk_int(0))) ); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], + mk_int(0))) )); + and_item.push_back(ctx.mk_eq_atom(or_item_option, mgr.mk_not(m_autil.mk_le( m_autil.mk_add(mk_strlen(n), m_autil.mk_mul(mk_int(-1), mk_strlen(y))), - mk_int(0))) ); + mk_int(0))) )); option++; @@ -2997,22 +3001,20 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } if (can_two_nodes_eq(x, m) && can_two_nodes_eq(y, n)) { - or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(x, m)); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(y, n)); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(x), mk_strlen(m))); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(y), mk_strlen(n))); + expr_ref or_item_option(ctx.mk_eq_atom(xorFlag, mk_int(option)), mgr); + or_item.push_back(or_item_option); + and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(x, m))); + and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(y, n))); + and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(mk_strlen(x), mk_strlen(m)))); + and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(mk_strlen(y), mk_strlen(n)))); ++option; } if (option > 0) { - if (option == 1) { - and_item[0] = or_item[0]; - } else { - and_item[0] = mgr.mk_or(option, or_item); - } + and_item.push_back(mk_or(or_item)); + expr_ref premise(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); - expr_ref conclusion(mgr.mk_and(pos, and_item), mgr); + expr_ref conclusion(mk_and(and_item), mgr); assert_implication(premise, conclusion); } else { TRACE("t_str", tout << "STOP: no split option found for two EQ concats." << std::endl;); From 11d8ffc4d430a8bf553a2df84639455a416f7868 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 22 Nov 2016 18:21:40 -0500 Subject: [PATCH 257/562] escape characters in theory_str --- src/ast/str_decl_plugin.cpp | 64 +++++++++++++++++++++++++++++++++ src/ast/str_decl_plugin.h | 7 ++++ src/parsers/smt2/smt2parser.cpp | 2 +- 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 4f9dcb7aa..f17551b94 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -339,6 +339,70 @@ str_util::str_util(ast_manager &m) : m_fid = m_plugin->get_family_id(); } +/* + * Scan through the string 'val' and interpret each instance of "backslash followed by a character" + * as a possible escape sequence. Emit all other characters as-is. + * This exists because the SMT-LIB 2.5 standard does not recognize escape sequences other than "" -> " . + * The escape sequences recognized are as follows: + * \a \b \e \f \n \r \t \v : as specified by the C++ standard + * \ooo : produces the ASCII character corresponding to the octal value "ooo", where each "o" is a + * single octal digit and between 1 and 3 valid digits are given + * \xhh : produces the ASCII character corresponding to the hexadecimal value "hh", where each "h" is a + * single case-insensitive hex digit (0-9A-F) and exactly 2 digits are given + * \C, for any character C that does not start a legal escape sequence : the backslash is ignored and "C" is produced. + */ +app * str_util::mk_string_with_escape_characters(std::string & val) { + std::string parsedStr; + parsedStr.reserve(val.length()); + for (unsigned i = 0; i < val.length(); ++i) { + char nextChar = val.at(i); + + if (nextChar == '\\') { + // check escape sequence + i++; + if (i >= val.length()) { + // TODO illegal escape sequence + NOT_IMPLEMENTED_YET(); + } + char escapeChar1 = val.at(i); + if (escapeChar1 == 'a') { + parsedStr.push_back('\a'); + } else if (escapeChar1 == 'b') { + parsedStr.push_back('\b'); + } else if (escapeChar1 == 'e') { + parsedStr.push_back('\e'); + } else if (escapeChar1 == 'f') { + parsedStr.push_back('\f'); + } else if (escapeChar1 == 'n') { + parsedStr.push_back('\n'); + } else if (escapeChar1 == 'r') { + parsedStr.push_back('\r'); + } else if (escapeChar1 == 't') { + parsedStr.push_back('\t'); + } else if (escapeChar1 == 'v') { + parsedStr.push_back('\v'); + } else if (escapeChar1 == 'x') { + // TODO hex escape + NOT_IMPLEMENTED_YET(); + } else if (escapeChar1 == '0' || escapeChar1 == '1' || escapeChar1 == '2' || escapeChar1 == '3' || + escapeChar1 == '4' || escapeChar1 == '5' || escapeChar1 == '6' || escapeChar1 == '7') { + // TODO octal escape + NOT_IMPLEMENTED_YET(); + } else { + // unrecognized escape sequence -- just emit that character + parsedStr.push_back(escapeChar1); + } + } else { + parsedStr.push_back(nextChar); + } + + // i is incremented at the end of this loop. + // If it is modified, ensure that it points to the index before + // the next character. + } + return mk_string(parsedStr); +} + static std::string str2RegexStr(std::string str) { std::string res = ""; int len = str.size(); diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 8905d66bc..ff531e942 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -156,10 +156,17 @@ public: app * mk_string(std::string & val) { return m_plugin->mk_string(val); } + app * mk_fresh_string() { return m_plugin->mk_fresh_string(); } + app * mk_string_with_escape_characters(const char * val) { + std::string str(val); + return mk_string_with_escape_characters(str); + } + app * mk_string_with_escape_characters(std::string & val); + app * mk_re_Str2Reg(expr * s) { expr * es[1] = {s}; return m_manager.mk_app(get_fid(), OP_RE_STR2REGEX, 1, es); diff --git a/src/parsers/smt2/smt2parser.cpp b/src/parsers/smt2/smt2parser.cpp index c8e9a78b6..cdef41b72 100644 --- a/src/parsers/smt2/smt2parser.cpp +++ b/src/parsers/smt2/smt2parser.cpp @@ -1104,7 +1104,7 @@ namespace smt2 { strncpy(buf, original_token, bufsize); buf[bufsize] = '\0'; TRACE("parse_string", tout << "new string constant: " << buf << " length=" << bufsize << "\n";); - expr_stack().push_back(strutil().mk_string(buf)); + expr_stack().push_back(strutil().mk_string_with_escape_characters(buf)); next(); } From 8e962aa427e7a10ee315b9b3868fac7a8920a497 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 22 Nov 2016 18:32:03 -0500 Subject: [PATCH 258/562] escape chars in smt2 printing of string constants --- src/ast/ast_smt2_pp.cpp | 43 +++++++++++++++++++++++++++++++------ src/ast/str_decl_plugin.cpp | 4 +++- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/ast/ast_smt2_pp.cpp b/src/ast/ast_smt2_pp.cpp index ce7177ec9..eece67a32 100644 --- a/src/ast/ast_smt2_pp.cpp +++ b/src/ast/ast_smt2_pp.cpp @@ -305,14 +305,45 @@ format * smt2_pp_environment::mk_float(rational const & val) const { } format * smt2_pp_environment::pp_str_literal(app * t) { - TRACE("parse_string", tout << "pp_str_literal\n";); - str_util & u = get_strutil(); - SASSERT(u.is_string(t)); - const char * val; - u.is_string(t, &val); ast_manager & m = get_manager(); + str_util & u = get_strutil(); + TRACE("parse_string", tout << "pp_str_literal\n";); + + SASSERT(u.is_string(t)); + std::string strVal = u.get_string_constant_value(t); string_buffer<> buf; - buf << "\"" << val << "\""; + buf << "\""; + + // we want to scan strVal and escape every non-printable character + for (unsigned int i = 0; i < strVal.length(); ++i) { + char c = strVal.at(i); + if (isprint(c)) { + buf << c; + } else if (c == '\a') { + buf << "\\a"; + } else if (c == '\b') { + buf << "\\b"; + } else if (c == '\e') { + buf << "\\e"; + } else if (c == '\f') { + buf << "\\f"; + } else if (c == '\n') { + buf << "\\n"; + } else if (c == '\r') { + buf << "\\r"; + } else if (c == '\t') { + buf << "\\t"; + } else if (c == '\v') { + buf << "\\v"; + } else if (c == '\\') { + buf << "\\" << "\\"; + } else { + // TODO general hex escape + NOT_IMPLEMENTED_YET(); + } + } + + buf << "\""; return mk_string(m, buf.c_str()); } diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index f17551b94..8ac1f722f 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -344,7 +344,7 @@ str_util::str_util(ast_manager &m) : * as a possible escape sequence. Emit all other characters as-is. * This exists because the SMT-LIB 2.5 standard does not recognize escape sequences other than "" -> " . * The escape sequences recognized are as follows: - * \a \b \e \f \n \r \t \v : as specified by the C++ standard + * \a \b \e \f \n \r \t \v \\ : as specified by the C++ standard * \ooo : produces the ASCII character corresponding to the octal value "ooo", where each "o" is a * single octal digit and between 1 and 3 valid digits are given * \xhh : produces the ASCII character corresponding to the hexadecimal value "hh", where each "h" is a @@ -381,6 +381,8 @@ app * str_util::mk_string_with_escape_characters(std::string & val) { parsedStr.push_back('\t'); } else if (escapeChar1 == 'v') { parsedStr.push_back('\v'); + } else if (escapeChar1 == '\\') { + parsedStr.push_back('\\'); } else if (escapeChar1 == 'x') { // TODO hex escape NOT_IMPLEMENTED_YET(); From 889b6be2c3eb5d78613ceb46df98e290c12c2fde Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 23 Nov 2016 19:03:53 -0500 Subject: [PATCH 259/562] fix smt-lib 2.5 double quotes in pp --- src/ast/ast_smt2_pp.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ast/ast_smt2_pp.cpp b/src/ast/ast_smt2_pp.cpp index eece67a32..7e178b422 100644 --- a/src/ast/ast_smt2_pp.cpp +++ b/src/ast/ast_smt2_pp.cpp @@ -317,7 +317,10 @@ format * smt2_pp_environment::pp_str_literal(app * t) { // we want to scan strVal and escape every non-printable character for (unsigned int i = 0; i < strVal.length(); ++i) { char c = strVal.at(i); - if (isprint(c)) { + if (c == '"') { + // SMT-LIB 2.5 string escape + buf << "\"\""; + } else if (isprint(c)) { buf << c; } else if (c == '\a') { buf << "\\a"; From 1fa8129c8f063a5508a63f2450c5c0184429e7ca Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 25 Nov 2016 18:02:24 -0500 Subject: [PATCH 260/562] pretty-printing of general escape sequences for string literals --- src/ast/ast_smt2_pp.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/ast/ast_smt2_pp.cpp b/src/ast/ast_smt2_pp.cpp index 7e178b422..ed634069c 100644 --- a/src/ast/ast_smt2_pp.cpp +++ b/src/ast/ast_smt2_pp.cpp @@ -341,8 +341,14 @@ format * smt2_pp_environment::pp_str_literal(app * t) { } else if (c == '\\') { buf << "\\" << "\\"; } else { - // TODO general hex escape - NOT_IMPLEMENTED_YET(); + // general hex escape + buf << "\\x"; + unsigned int cVal = (unsigned int)c; + const char convtable[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + unsigned int highPart = cVal / 16; + unsigned int lowPart = cVal % 16; + SASSERT(highPart < 16); SASSERT(lowPart < 16); + buf << convtable[highPart] << convtable[lowPart]; } } From 8c33dfab39c380ab5162b712bbab024fecb61cf8 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 27 Nov 2016 20:51:34 -0500 Subject: [PATCH 261/562] fix escape character overflow print --- src/ast/ast_smt2_pp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ast/ast_smt2_pp.cpp b/src/ast/ast_smt2_pp.cpp index ed634069c..db2043320 100644 --- a/src/ast/ast_smt2_pp.cpp +++ b/src/ast/ast_smt2_pp.cpp @@ -343,7 +343,7 @@ format * smt2_pp_environment::pp_str_literal(app * t) { } else { // general hex escape buf << "\\x"; - unsigned int cVal = (unsigned int)c; + unsigned int cVal = ((unsigned int)c) & 0x000000FF; const char convtable[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; unsigned int highPart = cVal / 16; unsigned int lowPart = cVal % 16; From 1e65511a3f86084c8fdcaf1e8e6445091dcef43b Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 28 Nov 2016 16:21:26 -0500 Subject: [PATCH 262/562] save a few functions to trail in theory_str --- src/smt/theory_str.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index a34a6b8c1..ca27169ed 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -662,6 +662,7 @@ app * theory_str::mk_unroll(expr * n, expr * bound) { expr * args[2] = {n, bound}; app * unrollFunc = get_manager().mk_app(get_id(), OP_RE_UNROLL, 0, 0, 2, args); + m_trail.push_back(unrollFunc); expr_ref_vector items(m); items.push_back(ctx.mk_eq_atom(ctx.mk_eq_atom(bound, mk_int(0)), ctx.mk_eq_atom(unrollFunc, m_strutil.mk_string("")))); @@ -677,6 +678,7 @@ app * theory_str::mk_unroll(expr * n, expr * bound) { app * theory_str::mk_contains(expr * haystack, expr * needle) { expr * args[2] = {haystack, needle}; app * contains = get_manager().mk_app(get_id(), OP_STR_CONTAINS, 0, 0, 2, args); + m_trail.push_back(contains); // immediately force internalization so that axiom setup does not fail get_context().internalize(contains, false); set_up_axioms(contains); @@ -686,6 +688,7 @@ app * theory_str::mk_contains(expr * haystack, expr * needle) { app * theory_str::mk_indexof(expr * haystack, expr * needle) { expr * args[2] = {haystack, needle}; app * indexof = get_manager().mk_app(get_id(), OP_STR_INDEXOF, 0, 0, 2, args); + m_trail.push_back(indexof); // immediately force internalization so that axiom setup does not fail get_context().internalize(indexof, false); set_up_axioms(indexof); From b77f6666dc82cd5976ef4f9916e4e0aba8865955 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 28 Nov 2016 18:40:28 -0500 Subject: [PATCH 263/562] refactor process_concat_eq_type_6 to use expr_ref_vector --- src/smt/theory_str.cpp | 52 +++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ca27169ed..6c584fa2e 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2272,19 +2272,14 @@ expr * theory_str::simplify_concat(expr * node) { if (in_same_eqc(node, resultAst)) { TRACE("t_str_detail", tout << "SKIP: both concats are already in the same equivalence class" << std::endl;); } else { - // TODO refactor - expr ** items = alloc_svect(expr*, resolvedMap.size()); + expr_ref_vector items(m); int pos = 0; std::map::iterator itor = resolvedMap.begin(); for (; itor != resolvedMap.end(); ++itor) { - items[pos++] = ctx.mk_eq_atom(itor->first, itor->second); - } - expr_ref premise(m); - if (pos == 1) { - premise = items[0]; - } else { - premise = m.mk_and(pos, items); + items.push_back(ctx.mk_eq_atom(itor->first, itor->second)); + pos += 1; } + expr_ref premise(mk_and(items), m); expr_ref conclusion(ctx.mk_eq_atom(node, resultAst), m); assert_implication(premise, conclusion); } @@ -3961,22 +3956,26 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { refresh_theory_var(commonVar); } - expr ** or_item = alloc_svect(expr*, (overlapLen.size() + 1)); + expr_ref_vector or_item(mgr); int option = 0; - expr ** and_item = alloc_svect(expr*, (1 + 4 * (overlapLen.size() + 1))); + expr_ref_vector and_item(mgr); int pos = 1; if (!avoidLoopCut || !has_self_cut(m, y)) { - or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); + expr_ref or_item_option(ctx.mk_eq_atom(xorFlag, mk_int(option)), mgr); + or_item.push_back(or_item_option); expr_ref str1_commonVar(mk_concat(str1Ast, commonVar), mgr); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(m, str1_commonVar)); + and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(m, str1_commonVar))); + pos += 1; expr_ref commonVar_str2(mk_concat(commonVar, str2Ast), mgr); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(y, commonVar_str2)); + and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(y, commonVar_str2))); + pos += 1; - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(m), - m_autil.mk_add(mk_strlen(str1Ast), mk_strlen(commonVar)) )); + and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(mk_strlen(m), + m_autil.mk_add(mk_strlen(str1Ast), mk_strlen(commonVar)) ))); + pos += 1; // addItems[0] = mk_length(t, commonVar); // addItems[1] = mk_length(t, str2Ast); @@ -3993,29 +3992,34 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { int overLen = *itor; std::string prefix = str1Value.substr(0, str1Len - overLen); std::string suffix = str2Value.substr(overLen, str2Len - overLen); - or_item[option] = ctx.mk_eq_atom(xorFlag, mk_int(option)); + expr_ref or_item_option(ctx.mk_eq_atom(xorFlag, mk_int(option)), mgr); + or_item.push_back(or_item_option); expr_ref prefixAst(m_strutil.mk_string(prefix), mgr); expr_ref x_eq_prefix(ctx.mk_eq_atom(m, prefixAst), mgr); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], x_eq_prefix); + and_item.push_back(ctx.mk_eq_atom(or_item_option, x_eq_prefix)); + pos += 1; - and_item[pos++] = ctx.mk_eq_atom(or_item[option], - ctx.mk_eq_atom(mk_strlen(m), mk_strlen(prefixAst))); + and_item.push_back(ctx.mk_eq_atom(or_item_option, + ctx.mk_eq_atom(mk_strlen(m), mk_strlen(prefixAst)))); + pos += 1; // adding length constraint for _ = constStr seems slowing things down. expr_ref suffixAst(m_strutil.mk_string(suffix), mgr); expr_ref y_eq_suffix(ctx.mk_eq_atom(y, suffixAst), mgr); - and_item[pos++] = ctx.mk_eq_atom(or_item[option], y_eq_suffix); + and_item.push_back(ctx.mk_eq_atom(or_item_option, y_eq_suffix)); + pos += 1; - and_item[pos++] = ctx.mk_eq_atom(or_item[option], ctx.mk_eq_atom(mk_strlen(y), mk_strlen(suffixAst))); + and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(mk_strlen(y), mk_strlen(suffixAst)))); + pos += 1; option++; } // case 6: concat("str1", y) = concat(m, "str2") - and_item[0] = mgr.mk_or(option, or_item); - expr_ref implyR(mgr.mk_and(pos, and_item), mgr); + and_item.push_back(mk_or(or_item)); + expr_ref implyR(mk_and(and_item), mgr); assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); } From f968f79d1c5e4f6bc696a62a77ce140dd61f5a5b Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 28 Nov 2016 18:47:42 -0500 Subject: [PATCH 264/562] refactor solve_concat_eq_str to use expr_ref_vector --- src/smt/theory_str.cpp | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 6c584fa2e..ea1ae8677 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -5824,19 +5824,16 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { if (arg1 != a1 || arg2 != a2) { TRACE("t_str", tout << "resolved concat argument(s) to eqc string constants" << std::endl;); int iPos = 0; - app * item1[2]; + expr_ref_vector item1(m); if (a1 != arg1) { - item1[iPos++] = ctx.mk_eq_atom(a1, arg1); + item1.push_back(ctx.mk_eq_atom(a1, arg1)); + iPos += 1; } if (a2 != arg2) { - item1[iPos++] = ctx.mk_eq_atom(a2, arg2); - } - expr_ref implyL1(m); - if (iPos == 1) { - implyL1 = item1[0]; - } else { - implyL1 = m.mk_and(item1[0], item1[1]); + item1.push_back(ctx.mk_eq_atom(a2, arg2)); + iPos += 1; } + expr_ref implyL1(mk_and(item1), m); newConcat = mk_concat(arg1, arg2); if (newConcat != str) { expr_ref implyR1(ctx.mk_eq_atom(concat, newConcat), m); @@ -6091,8 +6088,8 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { int xor_pos = 0; int and_count = 1; - expr ** xor_items = alloc_svect(expr*, (concatStrLen+1)); - expr ** and_items = alloc_svect(expr*, (4 * (concatStrLen+1) + 1)); + expr_ref_vector xor_items(m); + expr_ref_vector and_items(m); for (int i = 0; i < concatStrLen + 1; ++i) { std::string prefixStr = const_str.substr(0, i); @@ -6105,15 +6102,18 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { continue; } expr_ref xorAst(ctx.mk_eq_atom(xorFlag, m_autil.mk_numeral(rational(xor_pos), true)), m); - xor_items[xor_pos++] = xorAst; + xor_items.push_back(xorAst); + xor_pos += 1; expr_ref prefixAst(m_strutil.mk_string(prefixStr), m); expr_ref arg1_eq (ctx.mk_eq_atom(arg1, prefixAst), m); - and_items[and_count++] = ctx.mk_eq_atom(xorAst, arg1_eq); + and_items.push_back(ctx.mk_eq_atom(xorAst, arg1_eq)); + and_count += 1; expr_ref suffixAst(m_strutil.mk_string(suffixStr), m); expr_ref arg2_eq (ctx.mk_eq_atom(arg2, suffixAst), m); - and_items[and_count++] = ctx.mk_eq_atom(xorAst, arg2_eq); + and_items.push_back(ctx.mk_eq_atom(xorAst, arg2_eq)); + and_count += 1; } expr_ref implyL(ctx.mk_eq_atom(concat, str), m); @@ -6124,13 +6124,8 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { expr_ref negate_ast(m.mk_not(concat_eq_str), m); assert_axiom(negate_ast); } else { - if (xor_pos == 1) { - and_items[0] = xor_items[0]; - implyR1 = m.mk_and(and_count, and_items); - } else { - and_items[0] = m.mk_or(xor_pos, xor_items); - implyR1 = m.mk_and(and_count, and_items); - } + and_items.push_back(mk_or(xor_items)); + implyR1 = mk_and(and_items); assert_implication(implyL, implyR1); } } /* (arg1Len != 1 || arg2Len != 1) */ From 361f02ef1dc8ea0c3eeb9c5d993cc4e15e0dbab6 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 28 Nov 2016 21:34:55 -0500 Subject: [PATCH 265/562] remove assignment refcount hack from theory_str::pop_scope_eh --- src/smt/theory_str.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ea1ae8677..37be73333 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -6686,8 +6686,9 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { // TODO: figure out what's going out of scope and why context & ctx = get_context(); ast_manager & m = get_manager(); - expr_ref_vector assignments(m); - ctx.get_assignments(assignments); + + // expr_ref_vector assignments(m); + // ctx.get_assignments(assignments); TRACE_CODE(if (is_trace_enabled("t_str_dump_assign_on_scope_change")) { dump_assignments(); }); @@ -8254,6 +8255,7 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * // ---------------------------------------------------------------------------------------- + // TODO refactor this and below to use expr_ref_vector instead of ptr_vector/svect ptr_vector orList; ptr_vector andList; From 947d4437266ae05a397f024ae622406631ffc090 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 29 Nov 2016 19:46:37 -0500 Subject: [PATCH 266/562] improved regex concat rewrite --- src/ast/rewriter/str_rewriter.cpp | 22 ++++++++++++++++++++++ src/ast/rewriter/str_rewriter.h | 1 + 2 files changed, 23 insertions(+) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index 875343655..e30e857b2 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -489,6 +489,25 @@ br_status str_rewriter::mk_re_RegexStar(expr * re, expr_ref & result) { } } +br_status str_rewriter::mk_re_RegexConcat(expr * r0, expr * r1, expr_ref & result) { + TRACE("t_str_rw", tout << "rewrite (RegexConcat " << mk_pp(r0, m()) << " " << mk_pp(r1, m()) << ")" << std::endl;); + // (RegexConcat (Str2Reg "A") (Str2Reg "B")) --> (Str2Reg "AB") + if (m_strutil.is_re_Str2Reg(r0) && m_strutil.is_re_Str2Reg(r1)) { + expr * r0str = to_app(r0)->get_arg(0); + expr * r1str = to_app(r1)->get_arg(0); + ENSURE(m_strutil.is_string(r0str)); + ENSURE(m_strutil.is_string(r1str)); + std::string r0val = m_strutil.get_string_constant_value(r0str); + std::string r1val = m_strutil.get_string_constant_value(r1str); + std::string simplifyVal = r0val + r1val; + TRACE("t_str_rw", tout << "RegexConcat fast path: both sides are Str2Reg, simplify to (Str2Reg \"" << simplifyVal << "\")" << std::endl;); + result = m_strutil.mk_re_Str2Reg(simplifyVal); + return BR_DONE; + } + + return BR_FAILED; +} + br_status str_rewriter::mk_re_RegexPlus(expr * re, expr_ref & result) { /* * Two optimizations are possible if we inspect 're'. @@ -596,6 +615,9 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con case OP_RE_REGEXSTAR: SASSERT(num_args == 1); return mk_re_RegexStar(args[0], result); + case OP_RE_REGEXCONCAT: + SASSERT(num_args == 2); + return mk_re_RegexConcat(args[0], args[1], result); case OP_RE_REGEXCHARRANGE: SASSERT(num_args == 2); return mk_re_RegexCharRange(args[0], args[1], result); diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index 822fb1ea8..145c0193e 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -60,6 +60,7 @@ public: br_status mk_re_RegexIn(expr * str, expr * re, expr_ref & result); br_status mk_re_RegexPlus(expr * re, expr_ref & result); br_status mk_re_RegexStar(expr * re, expr_ref & result); + br_status mk_re_RegexConcat(expr * r0, expr * r1, expr_ref & result); br_status mk_re_RegexCharRange(expr * start, expr * end, expr_ref & result); bool reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change); From edf151c9a0a3b0b69803f9944c6fc88bf1443a76 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 29 Nov 2016 21:46:00 -0500 Subject: [PATCH 267/562] testing term generation refactor in theory_str::check_length_const_string --- src/smt/theory_str.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 37be73333..76b605ff9 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -5453,6 +5453,7 @@ bool theory_str::can_two_nodes_eq(expr * n1, expr * n2) { // case 0: n1_curr is const string, n2_curr is const string if (is_string(n1_curr) && is_string(n2_curr)) { if (n1_curr != n2_curr) { + // TODO confirm whether it's okay to compare the pointers like this return false; } } @@ -5494,8 +5495,18 @@ bool theory_str::check_length_const_string(expr * n1, expr * constStr) { rational strLen((unsigned) (m_strutil.get_string_constant_value(constStr).length())); if (is_concat(to_app(n1))) { + /* + * This has been refactored from Z3str2. + * We avoid creating new subexpressions until we actually detect a conflict. + * This may avoid a bit of overhead incurred by creating these terms. + */ + ptr_vector args; - expr_ref_vector items(mgr); + + expr_ref_vector eq_args(mgr); + vector eq_lens; + // foreach (arg, len) in zip(eq_args, eq_lens): + // generate eq(mk_strlen(arg), mk_int(len)) get_nodes_in_concat(n1, args); @@ -5505,12 +5516,20 @@ bool theory_str::check_length_const_string(expr * n1, expr * constStr) { bool argLen_exists = get_len_value(args[i], argLen); if (argLen_exists) { if (!m_strutil.is_string(args[i])) { - items.push_back(ctx.mk_eq_atom(mk_strlen(args[i]), mk_int(argLen))); + // items.push_back(ctx.mk_eq_atom(mk_strlen(args[i]), mk_int(argLen))); + eq_args.push_back(args[i]); + eq_lens.push_back(rational(argLen)); } TRACE("t_str_detail", tout << "concat arg: " << mk_pp(args[i], mgr) << " has len = " << argLen.to_string() << std::endl;); sumLen += argLen; if (sumLen > strLen) { + expr_ref_vector items(mgr); items.push_back(ctx.mk_eq_atom(n1, constStr)); + for (unsigned int z = 0; z < eq_args.size(); ++z) { + expr * arg = eq_args.get(z); + rational len = eq_lens.get(z); + items.push_back(ctx.mk_eq_atom(mk_strlen(arg), mk_int(len))); + } expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); TRACE("t_str_detail", tout << "inconsistent length: concat (len = " << sumLen << ") <==> string constant (len = " << strLen << ")" << std::endl;); assert_axiom(toAssert); From 599cc1e75d616c4640c1813bf2d26076372e18eb Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 30 Nov 2016 13:08:42 -0500 Subject: [PATCH 268/562] ref_vector refactoring in theory_str::check_length_concat_concat --- src/smt/theory_str.cpp | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 76b605ff9..84adf819d 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -5569,7 +5569,13 @@ bool theory_str::check_length_concat_concat(expr * n1, expr * n2) { bool concat1LenFixed = true; bool concat2LenFixed = true; - expr_ref_vector items(mgr); + /* + * Refactored from the Z3str2 version. + * We delay creation of new terms until a conflict + * is actually detected. + */ + expr_ref_vector eq_args(mgr); + vector eq_lens; rational sum1(0), sum2(0); @@ -5580,7 +5586,9 @@ bool theory_str::check_length_concat_concat(expr * n1, expr * n2) { if (argLen_exists) { sum1 += argLen; if (!m_strutil.is_string(oneArg)) { - items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); + // items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); + eq_args.push_back(oneArg); + eq_lens.push_back(rational(argLen)); } } else { concat1LenFixed = false; @@ -5594,15 +5602,15 @@ bool theory_str::check_length_concat_concat(expr * n1, expr * n2) { if (argLen_exists) { sum2 += argLen; if (!m_strutil.is_string(oneArg)) { - items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); + // items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); + eq_args.push_back(oneArg); + eq_lens.push_back(rational(argLen)); } } else { concat2LenFixed = false; } } - items.push_back(ctx.mk_eq_atom(n1, n2)); - bool conflict = false; if (concat1LenFixed && concat2LenFixed) { @@ -5621,6 +5629,13 @@ bool theory_str::check_length_concat_concat(expr * n1, expr * n2) { if (conflict) { TRACE("t_str_detail", tout << "inconsistent length detected in concat <==> concat" << std::endl;); + expr_ref_vector items(mgr); + for (unsigned int z = 0; z < eq_args.size(); ++z) { + expr * arg = eq_args.get(z); + rational len = eq_lens.get(z); + items.push_back(ctx.mk_eq_atom(mk_strlen(arg), mk_int(len))); + } + items.push_back(ctx.mk_eq_atom(n1, n2)); expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); assert_axiom(toAssert); return false; From fd1bf65b6472b883203ea3f0fecb33ee028c66df Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 30 Nov 2016 15:52:58 -0500 Subject: [PATCH 269/562] experimental non-reuse of XOR vars in theory_str --- src/smt/theory_str.cpp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 84adf819d..ec0a432d1 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2815,13 +2815,12 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { if (entry1InScope) { t1 = varForBreakConcat[key1][0]; t2 = varForBreakConcat[key1][1]; - xorFlag = varForBreakConcat[key1][2]; + xorFlag = mk_internal_xor_var(); } else { t1 = varForBreakConcat[key2][0]; t2 = varForBreakConcat[key2][1]; - xorFlag = varForBreakConcat[key2][2]; + xorFlag = mk_internal_xor_var(); } - // TODO do I need to refresh the xorFlag, which is an integer var, and if so, how? refresh_theory_var(t1); refresh_theory_var(t2); } @@ -3141,12 +3140,11 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { } else { if (entry1InScope) { temp1 = varForBreakConcat[key1][0]; - xorFlag = varForBreakConcat[key1][1]; + xorFlag = mk_internal_xor_var(); } else if (entry2InScope) { temp1 = varForBreakConcat[key2][0]; - xorFlag = varForBreakConcat[key2][1]; + xorFlag = mk_internal_xor_var(); } - // TODO refresh xorFlag? refresh_theory_var(temp1); } @@ -3451,10 +3449,10 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { } else { if (entry1InScope) { temp1 = varForBreakConcat[key1][0]; - xorFlag = varForBreakConcat[key1][1]; + xorFlag = mk_internal_xor_var(); } else if (varForBreakConcat.find(key2) != varForBreakConcat.end()) { temp1 = varForBreakConcat[key2][0]; - xorFlag = varForBreakConcat[key2][1]; + xorFlag = mk_internal_xor_var(); } refresh_theory_var(temp1); } @@ -3948,10 +3946,10 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { } else { if (entry1InScope) { commonVar = (entry1->second)[0]; - xorFlag = (entry1->second)[1]; + xorFlag = mk_internal_xor_var(); } else { commonVar = (entry2->second)[0]; - xorFlag = (entry2->second)[1]; + xorFlag = mk_internal_xor_var(); } refresh_theory_var(commonVar); } @@ -6113,9 +6111,9 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { xorFlag = mk_internal_xor_var(); varForBreakConcat[key1][0] = xorFlag; } else if (entry1InScope) { - xorFlag = varForBreakConcat[key1][0]; + xorFlag = mk_internal_xor_var(); } else { // entry2InScope - xorFlag = varForBreakConcat[key2][0]; + xorFlag = mk_internal_xor_var(); } int concatStrLen = const_str.length(); From 170e2b4e2a56b1bbcafcf104ead8f16baa2fc054 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 30 Nov 2016 19:41:00 -0500 Subject: [PATCH 270/562] refactor theory_str::check_length_concat_var --- src/smt/theory_str.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ec0a432d1..bc73db405 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -5652,7 +5652,15 @@ bool theory_str::check_length_concat_var(expr * concat, expr * var) { } else { rational sumLen(0); ptr_vector args; - expr_ref_vector items(mgr); + + /* + * Refactor from the Z3str2 version. + * Only generate new terms if a conflict is actually detected. + */ + + expr_ref_vector eq_args(mgr); + vector eq_lens; + get_nodes_in_concat(concat, args); for (unsigned int i = 0; i < args.size(); ++i) { expr * oneArg = args[i]; @@ -5660,11 +5668,19 @@ bool theory_str::check_length_concat_var(expr * concat, expr * var) { bool argLen_exists = get_len_value(oneArg, argLen); if (argLen_exists) { if (!m_strutil.is_string(oneArg) && !argLen.is_zero()) { - items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); + // items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); + eq_args.push_back(oneArg); + eq_lens.push_back(rational(argLen)); } sumLen += argLen; if (sumLen > varLen) { TRACE("t_str_detail", tout << "inconsistent length detected in concat <==> var" << std::endl;); + expr_ref_vector items(mgr); + for (unsigned int z = 0; z < eq_args.size(); ++z) { + expr * arg = eq_args.get(z); + rational len = eq_lens.get(z); + items.push_back(ctx.mk_eq_atom(mk_strlen(arg), mk_int(len))); + } items.push_back(ctx.mk_eq_atom(mk_strlen(var), mk_int(varLen))); items.push_back(ctx.mk_eq_atom(concat, var)); expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); From 10c0d94cf2a7834efed103d01f2c3d498045040e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 1 Dec 2016 15:19:50 -0500 Subject: [PATCH 271/562] Revert "refactor theory_str::check_length_concat_var" This reverts commit 170e2b4e2a56b1bbcafcf104ead8f16baa2fc054. --- src/smt/theory_str.cpp | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index bc73db405..ec0a432d1 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -5652,15 +5652,7 @@ bool theory_str::check_length_concat_var(expr * concat, expr * var) { } else { rational sumLen(0); ptr_vector args; - - /* - * Refactor from the Z3str2 version. - * Only generate new terms if a conflict is actually detected. - */ - - expr_ref_vector eq_args(mgr); - vector eq_lens; - + expr_ref_vector items(mgr); get_nodes_in_concat(concat, args); for (unsigned int i = 0; i < args.size(); ++i) { expr * oneArg = args[i]; @@ -5668,19 +5660,11 @@ bool theory_str::check_length_concat_var(expr * concat, expr * var) { bool argLen_exists = get_len_value(oneArg, argLen); if (argLen_exists) { if (!m_strutil.is_string(oneArg) && !argLen.is_zero()) { - // items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); - eq_args.push_back(oneArg); - eq_lens.push_back(rational(argLen)); + items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); } sumLen += argLen; if (sumLen > varLen) { TRACE("t_str_detail", tout << "inconsistent length detected in concat <==> var" << std::endl;); - expr_ref_vector items(mgr); - for (unsigned int z = 0; z < eq_args.size(); ++z) { - expr * arg = eq_args.get(z); - rational len = eq_lens.get(z); - items.push_back(ctx.mk_eq_atom(mk_strlen(arg), mk_int(len))); - } items.push_back(ctx.mk_eq_atom(mk_strlen(var), mk_int(varLen))); items.push_back(ctx.mk_eq_atom(concat, var)); expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); From 548f635f7ed1094d8aa84ead6f6acd80951eb4d4 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 1 Dec 2016 15:19:50 -0500 Subject: [PATCH 272/562] Revert "experimental non-reuse of XOR vars in theory_str" This reverts commit fd1bf65b6472b883203ea3f0fecb33ee028c66df. --- src/smt/theory_str.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ec0a432d1..84adf819d 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2815,12 +2815,13 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { if (entry1InScope) { t1 = varForBreakConcat[key1][0]; t2 = varForBreakConcat[key1][1]; - xorFlag = mk_internal_xor_var(); + xorFlag = varForBreakConcat[key1][2]; } else { t1 = varForBreakConcat[key2][0]; t2 = varForBreakConcat[key2][1]; - xorFlag = mk_internal_xor_var(); + xorFlag = varForBreakConcat[key2][2]; } + // TODO do I need to refresh the xorFlag, which is an integer var, and if so, how? refresh_theory_var(t1); refresh_theory_var(t2); } @@ -3140,11 +3141,12 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { } else { if (entry1InScope) { temp1 = varForBreakConcat[key1][0]; - xorFlag = mk_internal_xor_var(); + xorFlag = varForBreakConcat[key1][1]; } else if (entry2InScope) { temp1 = varForBreakConcat[key2][0]; - xorFlag = mk_internal_xor_var(); + xorFlag = varForBreakConcat[key2][1]; } + // TODO refresh xorFlag? refresh_theory_var(temp1); } @@ -3449,10 +3451,10 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { } else { if (entry1InScope) { temp1 = varForBreakConcat[key1][0]; - xorFlag = mk_internal_xor_var(); + xorFlag = varForBreakConcat[key1][1]; } else if (varForBreakConcat.find(key2) != varForBreakConcat.end()) { temp1 = varForBreakConcat[key2][0]; - xorFlag = mk_internal_xor_var(); + xorFlag = varForBreakConcat[key2][1]; } refresh_theory_var(temp1); } @@ -3946,10 +3948,10 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { } else { if (entry1InScope) { commonVar = (entry1->second)[0]; - xorFlag = mk_internal_xor_var(); + xorFlag = (entry1->second)[1]; } else { commonVar = (entry2->second)[0]; - xorFlag = mk_internal_xor_var(); + xorFlag = (entry2->second)[1]; } refresh_theory_var(commonVar); } @@ -6111,9 +6113,9 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { xorFlag = mk_internal_xor_var(); varForBreakConcat[key1][0] = xorFlag; } else if (entry1InScope) { - xorFlag = mk_internal_xor_var(); + xorFlag = varForBreakConcat[key1][0]; } else { // entry2InScope - xorFlag = mk_internal_xor_var(); + xorFlag = varForBreakConcat[key2][0]; } int concatStrLen = const_str.length(); From b020c71f8a8ddbd01addb88269cb582459ecc204 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 1 Dec 2016 15:19:51 -0500 Subject: [PATCH 273/562] Revert "ref_vector refactoring in theory_str::check_length_concat_concat" This reverts commit 599cc1e75d616c4640c1813bf2d26076372e18eb. --- src/smt/theory_str.cpp | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 84adf819d..76b605ff9 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -5569,13 +5569,7 @@ bool theory_str::check_length_concat_concat(expr * n1, expr * n2) { bool concat1LenFixed = true; bool concat2LenFixed = true; - /* - * Refactored from the Z3str2 version. - * We delay creation of new terms until a conflict - * is actually detected. - */ - expr_ref_vector eq_args(mgr); - vector eq_lens; + expr_ref_vector items(mgr); rational sum1(0), sum2(0); @@ -5586,9 +5580,7 @@ bool theory_str::check_length_concat_concat(expr * n1, expr * n2) { if (argLen_exists) { sum1 += argLen; if (!m_strutil.is_string(oneArg)) { - // items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); - eq_args.push_back(oneArg); - eq_lens.push_back(rational(argLen)); + items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); } } else { concat1LenFixed = false; @@ -5602,15 +5594,15 @@ bool theory_str::check_length_concat_concat(expr * n1, expr * n2) { if (argLen_exists) { sum2 += argLen; if (!m_strutil.is_string(oneArg)) { - // items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); - eq_args.push_back(oneArg); - eq_lens.push_back(rational(argLen)); + items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); } } else { concat2LenFixed = false; } } + items.push_back(ctx.mk_eq_atom(n1, n2)); + bool conflict = false; if (concat1LenFixed && concat2LenFixed) { @@ -5629,13 +5621,6 @@ bool theory_str::check_length_concat_concat(expr * n1, expr * n2) { if (conflict) { TRACE("t_str_detail", tout << "inconsistent length detected in concat <==> concat" << std::endl;); - expr_ref_vector items(mgr); - for (unsigned int z = 0; z < eq_args.size(); ++z) { - expr * arg = eq_args.get(z); - rational len = eq_lens.get(z); - items.push_back(ctx.mk_eq_atom(mk_strlen(arg), mk_int(len))); - } - items.push_back(ctx.mk_eq_atom(n1, n2)); expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); assert_axiom(toAssert); return false; From 406b622f59c92caa59f7e2713f4d6f8d67d32ae4 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 1 Dec 2016 15:19:51 -0500 Subject: [PATCH 274/562] Revert "testing term generation refactor in theory_str::check_length_const_string" This reverts commit edf151c9a0a3b0b69803f9944c6fc88bf1443a76. --- src/smt/theory_str.cpp | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 76b605ff9..37be73333 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -5453,7 +5453,6 @@ bool theory_str::can_two_nodes_eq(expr * n1, expr * n2) { // case 0: n1_curr is const string, n2_curr is const string if (is_string(n1_curr) && is_string(n2_curr)) { if (n1_curr != n2_curr) { - // TODO confirm whether it's okay to compare the pointers like this return false; } } @@ -5495,18 +5494,8 @@ bool theory_str::check_length_const_string(expr * n1, expr * constStr) { rational strLen((unsigned) (m_strutil.get_string_constant_value(constStr).length())); if (is_concat(to_app(n1))) { - /* - * This has been refactored from Z3str2. - * We avoid creating new subexpressions until we actually detect a conflict. - * This may avoid a bit of overhead incurred by creating these terms. - */ - ptr_vector args; - - expr_ref_vector eq_args(mgr); - vector eq_lens; - // foreach (arg, len) in zip(eq_args, eq_lens): - // generate eq(mk_strlen(arg), mk_int(len)) + expr_ref_vector items(mgr); get_nodes_in_concat(n1, args); @@ -5516,20 +5505,12 @@ bool theory_str::check_length_const_string(expr * n1, expr * constStr) { bool argLen_exists = get_len_value(args[i], argLen); if (argLen_exists) { if (!m_strutil.is_string(args[i])) { - // items.push_back(ctx.mk_eq_atom(mk_strlen(args[i]), mk_int(argLen))); - eq_args.push_back(args[i]); - eq_lens.push_back(rational(argLen)); + items.push_back(ctx.mk_eq_atom(mk_strlen(args[i]), mk_int(argLen))); } TRACE("t_str_detail", tout << "concat arg: " << mk_pp(args[i], mgr) << " has len = " << argLen.to_string() << std::endl;); sumLen += argLen; if (sumLen > strLen) { - expr_ref_vector items(mgr); items.push_back(ctx.mk_eq_atom(n1, constStr)); - for (unsigned int z = 0; z < eq_args.size(); ++z) { - expr * arg = eq_args.get(z); - rational len = eq_lens.get(z); - items.push_back(ctx.mk_eq_atom(mk_strlen(arg), mk_int(len))); - } expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); TRACE("t_str_detail", tout << "inconsistent length: concat (len = " << sumLen << ") <==> string constant (len = " << strLen << ")" << std::endl;); assert_axiom(toAssert); From 35ad68d9b5726f60f75a790164ccd69786182277 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 5 Dec 2016 15:13:48 -0500 Subject: [PATCH 275/562] assert stronger arrangements theory_str --- src/smt/theory_str.cpp | 128 +++++++++++++++++++++++++++++++++++------ src/smt/theory_str.h | 9 +++ 2 files changed, 120 insertions(+), 17 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 37be73333..3c6ad60ca 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -44,6 +44,7 @@ theory_str::theory_str(ast_manager & m): opt_CheckVariableScope(true), opt_UseFastLengthTesterCache(true), opt_UseFastValueTesterCache(true), + opt_AssertStrongerArrangements(true), /* Internal setup */ search_started(false), m_autil(m), @@ -2864,7 +2865,12 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { add_cut_info_merge(t1, sLevel, m); add_cut_info_merge(t1, sLevel, y); - assert_implication(ax_l, ax_r); + if (opt_AssertStrongerArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ax_l, ax_r); + } } else { loopDetected = true; TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); @@ -2911,7 +2917,12 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { add_cut_info_merge(t2, sLevel, x); add_cut_info_merge(t2, sLevel, n); - assert_implication(ax_l, ax_r); + if (opt_AssertStrongerArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ax_l, ax_r); + } } else { loopDetected = true; TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); @@ -3013,7 +3024,12 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { expr_ref premise(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); expr_ref conclusion(mk_and(and_item), mgr); - assert_implication(premise, conclusion); + if (opt_AssertStrongerArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom(premise, conclusion), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(premise, conclusion); + } } else { TRACE("t_str", tout << "STOP: no split option found for two EQ concats." << std::endl;); } @@ -3206,7 +3222,12 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { add_cut_info_merge(temp1, sLevel, y); add_cut_info_merge(temp1, sLevel, m); - assert_implication(ax_l, ax_r); + if (opt_AssertStrongerArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ax_l, ax_r); + } } else { loopDetected = true; TRACE("t_str", tout << "AVOID LOOP: SKIP" << std::endl;); @@ -3269,7 +3290,12 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { expr_ref ax_l(mk_and(l_items), mgr); expr_ref ax_r(mk_and(r_items), mgr); - assert_implication(ax_l, ax_r); + if (opt_AssertStrongerArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ax_l, ax_r); + } } else { // negate! It's impossible to split str with these lengths TRACE("t_str", tout << "CONFLICT: Impossible to split str with these lengths." << std::endl;); @@ -3329,7 +3355,14 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { if (option > 0) { and_item.push_back(mk_or(or_item)); expr_ref implyR(mk_and(and_item), mgr); - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + + if (opt_AssertStrongerArrangements) { + expr_ref implyLHS(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); + expr_ref ax_strong(ctx.mk_eq_atom(implyLHS, implyR), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } } else { TRACE("t_str", tout << "STOP: Should not split two EQ concats." << std::endl;); } @@ -3508,7 +3541,13 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { expr_ref_vector r_items(mgr); r_items.push_back(ctx.mk_eq_atom(x, prefixAst)); r_items.push_back(ctx.mk_eq_atom(y, suf_n_concat)); - assert_implication(ax_l, mk_and(r_items)); + + if (opt_AssertStrongerArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom(ax_l, mk_and(r_items)), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ax_l, mk_and(r_items)); + } } else { // negate! It's impossible to split str with these lengths TRACE("t_str", tout << "CONFLICT: Impossible to split str with these lengths." << std::endl;); @@ -3522,7 +3561,13 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { ctx.mk_eq_atom(mk_strlen(y), mk_strlen(n))), mgr); expr_ref ax_l(mgr.mk_and(ax_l1, ax_l2), mgr); expr_ref ax_r(mgr.mk_and(ctx.mk_eq_atom(x, strAst), ctx.mk_eq_atom(y, n)), mgr); - assert_implication(ax_l, ax_r); + + if (opt_AssertStrongerArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ax_l, ax_r); + } } else if (splitType == 2) { // | x | y | @@ -3555,7 +3600,12 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { add_cut_info_merge(temp1, sLevel, x); add_cut_info_merge(temp1, sLevel, n); - assert_implication(ax_l, ax_r); + if (opt_AssertStrongerArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ax_l, ax_r); + } } else { loopDetected = true; TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); @@ -3633,7 +3683,14 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { and_item.push_back(mk_or(or_item)); } expr_ref implyR(mk_and(and_item), mgr); - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + + if (opt_AssertStrongerArrangements) { + expr_ref ax_lhs(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); + expr_ref ax_strong(ctx.mk_eq_atom(ax_lhs, implyR), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } } else { TRACE("t_str", tout << "STOP: should not split two eq. concats" << std::endl;); } @@ -3708,13 +3765,24 @@ void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { if (!in_same_eqc(tmpAst, n)) { // break down option 4-1 expr_ref implyR(ctx.mk_eq_atom(n, tmpAst), mgr); - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + if (opt_AssertStrongerArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } } } else if (str1Len == str2Len) { if (!in_same_eqc(n, y)) { //break down option 4-2 expr_ref implyR(ctx.mk_eq_atom(n, y), mgr); - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + + if (opt_AssertStrongerArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } } } else { std::string deltaStr = str2Value.substr(str1Len, str2Len - str1Len); @@ -3722,7 +3790,12 @@ void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { if (!in_same_eqc(y, tmpAst)) { //break down option 4-3 expr_ref implyR(ctx.mk_eq_atom(y, tmpAst), mgr); - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + if (opt_AssertStrongerArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } } } } @@ -3794,20 +3867,35 @@ void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { expr_ref x_deltaStr(mk_concat(x, m_strutil.mk_string(deltaStr)), mgr); if (!in_same_eqc(m, x_deltaStr)) { expr_ref implyR(ctx.mk_eq_atom(m, x_deltaStr), mgr); - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + if (opt_AssertStrongerArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } } } else if (str1Len == str2Len) { // test if (!in_same_eqc(x, m)) { expr_ref implyR(ctx.mk_eq_atom(x, m), mgr); - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + if (opt_AssertStrongerArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } } } else { std::string deltaStr = str2Value.substr(0, str2Len - str1Len); expr_ref m_deltaStr(mk_concat(m, m_strutil.mk_string(deltaStr)), mgr); if (!in_same_eqc(x, m_deltaStr)) { expr_ref implyR(ctx.mk_eq_atom(x, m_deltaStr), mgr); - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + if (opt_AssertStrongerArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } } } } @@ -4020,7 +4108,13 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { // case 6: concat("str1", y) = concat(m, "str2") and_item.push_back(mk_or(or_item)); expr_ref implyR(mk_and(and_item), mgr); - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + + if (opt_AssertStrongerArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } } void theory_str::process_unroll_eq_const_str(expr * unrollFunc, expr * constStr) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 5b8f644eb..8168d0632 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -189,6 +189,15 @@ namespace smt { */ bool opt_UseFastValueTesterCache; + /* + * If AssertStrongerArrangements is set to true, + * the implications that would normally be asserted during arrangement generation + * will instead be asserted as equivalences. + * This is a stronger version of the regular axiom. + * The default (Z3str2) behaviour is to set this to false. + */ + bool opt_AssertStrongerArrangements; + bool search_started; arith_util m_autil; str_util m_strutil; From be9cb8db82d56493401817ef5bac6a5ee4affd47 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 5 Dec 2016 20:17:43 -0500 Subject: [PATCH 276/562] regex tracing theory_str --- src/ast/rewriter/str_rewriter.cpp | 2 +- src/smt/theory_str.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index e30e857b2..bc64e7218 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -456,10 +456,10 @@ br_status str_rewriter::mk_re_RegexIn(expr * str, expr * re, expr_ref & result) // fast path: // (RegexIn E (Str2Reg S)) --> (= E S) if (m_strutil.is_re_Str2Reg(re)) { - TRACE("t_str_rw", tout << "RegexIn fast path: " << mk_pp(str, m()) << " in " << mk_pp(re, m()) << std::endl;); expr * regexStr = to_app(re)->get_arg(0); ENSURE(m_strutil.is_string(regexStr)); result = m().mk_eq(str, regexStr); + TRACE("t_str_rw", tout << "RegexIn fast path: " << mk_pp(str, m()) << " in " << mk_pp(re, m()) << " ==> " << mk_pp(result, m()) << std::endl;); return BR_REWRITE_FULL; } diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 37be73333..543adcc03 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1708,6 +1708,7 @@ void theory_str::instantiate_axiom_RegexIn(enode * e) { expr_ref finalAxiom(m.mk_iff(ex, rhs), m); SASSERT(finalAxiom); assert_axiom(finalAxiom); + TRACE("t_str", tout << "set up Str2Reg: (RegexIn " << mk_pp(str, m) << " " << mk_pp(regex, m) << ")" << std::endl;); } else if (is_RegexConcat(regex)) { expr_ref var1(mk_regex_rep_var(), m); expr_ref var2(mk_regex_rep_var(), m); From da61c99f9e880edf1e7b6540e4b3012c96f62ce5 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 6 Dec 2016 12:52:48 -0500 Subject: [PATCH 277/562] experimental boolean case split in theory_str process_concat_eq_type1 WIP --- src/smt/theory_str.cpp | 93 +++++++++++++++++++++++++++--------------- src/smt/theory_str.h | 2 + 2 files changed, 62 insertions(+), 33 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 84091e2da..3d22427d5 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2443,6 +2443,30 @@ void theory_str::infer_len_concat_equality(expr * nn1, expr * nn2) { */ } +expr_ref theory_str::generate_mutual_exclusion(expr_ref_vector & terms) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + expr_ref_vector result(m); + + // TODO this can probably be made more efficient + + for (unsigned int majorIndex = 0; majorIndex < terms.size(); ++majorIndex) { + for (unsigned int minorIndex = 0; minorIndex < terms.size(); ++minorIndex) { + if (majorIndex == minorIndex) { + continue; + } + // generate an expression of the form + // terms[majorIndex] --> NOT(terms[minorIndex]) + expr_ref ex(rewrite_implication(terms.get(majorIndex), m.mk_not(terms.get(minorIndex))), m); + result.push_back(ex); + } + } + + expr_ref final_result(mk_and(result), m); + return final_result; +} + /* * Handle two equivalent Concats. */ @@ -2931,40 +2955,42 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } } else if (splitType == -1) { // Here we don't really have a choice. We have no length information at all... - expr_ref_vector or_item(mgr); - expr_ref_vector and_item(mgr); + + // This vector will eventually contain one term for each possible arrangement we explore. + expr_ref_vector arrangement_disjunction(mgr); + int option = 0; int pos = 1; // break option 1: m cuts y // len(x) < len(m) || len(y) > len(n) if (!avoidLoopCut || !has_self_cut(m, y)) { + expr_ref_vector and_item(mgr); // break down option 1-1 expr_ref x_t1(mk_concat(x, t1), mgr); expr_ref t1_n(mk_concat(t1, n), mgr); - expr_ref or_item_option(ctx.mk_eq_atom(xorFlag, mk_int(option)), mgr); - or_item.push_back(or_item_option); - and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(m, x_t1))); - and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(y, t1_n))); + + and_item.push_back(ctx.mk_eq_atom(m, x_t1)); + and_item.push_back(ctx.mk_eq_atom(y, t1_n)); expr_ref x_plus_t1(m_autil.mk_add(mk_strlen(x), mk_strlen(t1)), mgr); - and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(mk_strlen(m), x_plus_t1))); + and_item.push_back(ctx.mk_eq_atom(mk_strlen(m), x_plus_t1)); // These were crashing the solver because the integer theory // expects a constant on the right-hand side. // The things we want to assert here are len(m) > len(x) and len(y) > len(n). // We rewrite A > B as A-B > 0 and then as not(A-B <= 0), // and then, *because we aren't allowed to use subtraction*, // as not(A + -1*B <= 0) - and_item.push_back(ctx.mk_eq_atom(or_item_option, + and_item.push_back( mgr.mk_not(m_autil.mk_le( m_autil.mk_add(mk_strlen(m), m_autil.mk_mul(mk_int(-1), mk_strlen(x))), - mk_int(0))) )); - and_item.push_back(ctx.mk_eq_atom(or_item_option, + mk_int(0))) ); + and_item.push_back( mgr.mk_not(m_autil.mk_le( m_autil.mk_add(mk_strlen(y),m_autil.mk_mul(mk_int(-1), mk_strlen(n))), - mk_int(0))) )); + mk_int(0))) ); - option++; + arrangement_disjunction.push_back(mk_and(and_item)); add_cut_info_merge(t1, ctx.get_scope_level(), m); add_cut_info_merge(t1, ctx.get_scope_level(), y); @@ -2977,30 +3003,30 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { // break option 2: // x = m || y = n if (!avoidLoopCut || !has_self_cut(x, n)) { + expr_ref_vector and_item(mgr); // break down option 1-2 expr_ref m_t2(mk_concat(m, t2), mgr); expr_ref t2_y(mk_concat(t2, y), mgr); - expr_ref or_item_option(ctx.mk_eq_atom(xorFlag, mk_int(option)), mgr); - or_item.push_back(or_item_option); - and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(x, m_t2))); - and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(n, t2_y))); + + and_item.push_back(ctx.mk_eq_atom(x, m_t2)); + and_item.push_back(ctx.mk_eq_atom(n, t2_y)); expr_ref m_plus_t2(m_autil.mk_add(mk_strlen(m), mk_strlen(t2)), mgr); - and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(mk_strlen(x), m_plus_t2))); + and_item.push_back(ctx.mk_eq_atom(mk_strlen(x), m_plus_t2)); // want len(x) > len(m) and len(n) > len(y) - and_item.push_back(ctx.mk_eq_atom(or_item_option, + and_item.push_back( mgr.mk_not(m_autil.mk_le( m_autil.mk_add(mk_strlen(x), m_autil.mk_mul(mk_int(-1), mk_strlen(m))), - mk_int(0))) )); - and_item.push_back(ctx.mk_eq_atom(or_item_option, + mk_int(0))) ); + and_item.push_back( mgr.mk_not(m_autil.mk_le( m_autil.mk_add(mk_strlen(n), m_autil.mk_mul(mk_int(-1), mk_strlen(y))), - mk_int(0))) )); + mk_int(0))) ); - option++; + arrangement_disjunction.push_back(mk_and(and_item)); add_cut_info_merge(t2, ctx.get_scope_level(), x); add_cut_info_merge(t2, ctx.get_scope_level(), n); @@ -3011,26 +3037,27 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } if (can_two_nodes_eq(x, m) && can_two_nodes_eq(y, n)) { - expr_ref or_item_option(ctx.mk_eq_atom(xorFlag, mk_int(option)), mgr); - or_item.push_back(or_item_option); - and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(x, m))); - and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(y, n))); - and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(mk_strlen(x), mk_strlen(m)))); - and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(mk_strlen(y), mk_strlen(n)))); - ++option; + expr_ref_vector and_item(mgr); + + and_item.push_back(ctx.mk_eq_atom(x, m)); + and_item.push_back(ctx.mk_eq_atom(y, n)); + and_item.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_strlen(m))); + and_item.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_strlen(n))); + + arrangement_disjunction.push_back(mk_and(and_item)); } - if (option > 0) { - and_item.push_back(mk_or(or_item)); - + if (!arrangement_disjunction.empty()) { expr_ref premise(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); - expr_ref conclusion(mk_and(and_item), mgr); + expr_ref conclusion(mk_or(arrangement_disjunction), mgr); if (opt_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(premise, conclusion), mgr); assert_axiom(ax_strong); } else { assert_implication(premise, conclusion); } + // assert mutual exclusion between each branch of the arrangement + assert_axiom(generate_mutual_exclusion(arrangement_disjunction)); } else { TRACE("t_str", tout << "STOP: no split option found for two EQ concats." << std::endl;); } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 8168d0632..29f5c2336 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -478,6 +478,8 @@ namespace smt { void process_concat_eq_type5(expr * concatAst1, expr * concatAst2); void process_concat_eq_type6(expr * concatAst1, expr * concatAst2); + expr_ref generate_mutual_exclusion(expr_ref_vector & exprs); + bool new_eq_check(expr * lhs, expr * rhs); void group_terms_by_eqc(expr * n, std::set & concats, std::set & vars, std::set & consts); From b57f04e2d2c74d124d72765964cae95475287f3b Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 6 Dec 2016 12:59:40 -0500 Subject: [PATCH 278/562] optimize generate_mutual_exclusion in theory_str to make only half as many subterms --- src/smt/theory_str.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 3d22427d5..4b38d02d3 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2449,13 +2449,8 @@ expr_ref theory_str::generate_mutual_exclusion(expr_ref_vector & terms) { expr_ref_vector result(m); - // TODO this can probably be made more efficient - for (unsigned int majorIndex = 0; majorIndex < terms.size(); ++majorIndex) { - for (unsigned int minorIndex = 0; minorIndex < terms.size(); ++minorIndex) { - if (majorIndex == minorIndex) { - continue; - } + for (unsigned int minorIndex = majorIndex + 1; minorIndex < terms.size(); ++minorIndex) { // generate an expression of the form // terms[majorIndex] --> NOT(terms[minorIndex]) expr_ref ex(rewrite_implication(terms.get(majorIndex), m.mk_not(terms.get(minorIndex))), m); From 225b527d5832848bc80bc3406b3fafde36d581a8 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 6 Dec 2016 16:09:38 -0500 Subject: [PATCH 279/562] boolean case split theory_str process_concat_eq_type2 --- src/smt/theory_str.cpp | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 4b38d02d3..ef86be313 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3328,8 +3328,8 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { } else { // Split type -1: no idea about the length... int optionTotal = 2 + strValue.length(); - expr_ref_vector or_item(mgr); - expr_ref_vector and_item(mgr); + expr_ref_vector arrangement_disjunction(mgr); + int option = 0; int pos = 1; @@ -3339,16 +3339,16 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { if (can_two_nodes_eq(y, temp1_strAst)) { if (!avoidLoopCut || !has_self_cut(m, y)) { // break down option 2-1 - expr_ref current_or_item_option(ctx.mk_eq_atom(xorFlag, mk_int(option)), mgr); - or_item.push_back(current_or_item_option); + expr_ref_vector and_item(mgr); + expr_ref x_temp1(mk_concat(x, temp1), mgr); - and_item.push_back(ctx.mk_eq_atom(current_or_item_option, ctx.mk_eq_atom(m, x_temp1))); - and_item.push_back(ctx.mk_eq_atom(current_or_item_option, ctx.mk_eq_atom(y, temp1_strAst))); + and_item.push_back(ctx.mk_eq_atom(m, x_temp1)); + and_item.push_back(ctx.mk_eq_atom(y, temp1_strAst)); - and_item.push_back(ctx.mk_eq_atom(current_or_item_option, ctx.mk_eq_atom(mk_strlen(m), - m_autil.mk_add(mk_strlen(x), mk_strlen(temp1))))); + and_item.push_back(ctx.mk_eq_atom(mk_strlen(m), + m_autil.mk_add(mk_strlen(x), mk_strlen(temp1)))); - ++option; + arrangement_disjunction.push_back(mk_and(and_item)); add_cut_info_merge(temp1, ctx.get_scope_level(), y); add_cut_info_merge(temp1, ctx.get_scope_level(), m); } else { @@ -3366,18 +3366,16 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { expr_ref cropStr(m_strutil.mk_string(part2Str), mgr); if (can_two_nodes_eq(x, x_concat) && can_two_nodes_eq(y, cropStr)) { // break down option 2-2 - expr_ref current_or_item_option(ctx.mk_eq_atom(xorFlag, mk_int(option)), mgr); - or_item.push_back(current_or_item_option); - and_item.push_back(ctx.mk_eq_atom(current_or_item_option, ctx.mk_eq_atom(x, x_concat))); - and_item.push_back(ctx.mk_eq_atom(current_or_item_option, ctx.mk_eq_atom(y, cropStr))); - and_item.push_back(ctx.mk_eq_atom(current_or_item_option, ctx.mk_eq_atom(mk_strlen(y), mk_int(part2Str.length())))); - ++option; + expr_ref_vector and_item(mgr); + and_item.push_back(ctx.mk_eq_atom(x, x_concat)); + and_item.push_back(ctx.mk_eq_atom(y, cropStr)); + and_item.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(part2Str.length()))); + arrangement_disjunction.push_back(mk_and(and_item)); } } - if (option > 0) { - and_item.push_back(mk_or(or_item)); - expr_ref implyR(mk_and(and_item), mgr); + if (!arrangement_disjunction.empty()) { + expr_ref implyR(mk_or(arrangement_disjunction), mgr); if (opt_AssertStrongerArrangements) { expr_ref implyLHS(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); @@ -3386,6 +3384,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { } else { assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); } + assert_axiom(generate_mutual_exclusion(arrangement_disjunction)); } else { TRACE("t_str", tout << "STOP: Should not split two EQ concats." << std::endl;); } From 7b0aaf874554704af8561810285c69c42a451d97 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 6 Dec 2016 16:22:42 -0500 Subject: [PATCH 280/562] boolean case split theory_str concat_eq remaining cases --- src/smt/theory_str.cpp | 77 ++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ef86be313..d524bffe7 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3643,9 +3643,9 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { else { // Split type -1. We know nothing about the length... - expr_ref_vector or_item(mgr); + expr_ref_vector arrangement_disjunction(mgr); unsigned option = 0; - expr_ref_vector and_item(mgr); + int pos = 1; for (int i = 0; i <= (int) strValue.size(); i++) { std::string part1Str = strValue.substr(0, i); @@ -3655,17 +3655,18 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { expr_ref y_concat(mk_concat(suffixStr, n), mgr); if (can_two_nodes_eq(x, cropStr) && can_two_nodes_eq(y, y_concat)) { + expr_ref_vector and_item(mgr); // break down option 3-1 expr_ref x_eq_str(ctx.mk_eq_atom(x, cropStr), mgr); - or_item.push_back(ctx.mk_eq_atom(xorFlag, mk_int(option))); - and_item.push_back(ctx.mk_eq_atom(or_item.get(option), x_eq_str)); ++pos; - and_item.push_back(ctx.mk_eq_atom(or_item.get(option), ctx.mk_eq_atom(y, y_concat))); - and_item.push_back(ctx.mk_eq_atom(or_item.get(option), ctx.mk_eq_atom(mk_strlen(x), mk_strlen(cropStr)))); ++pos; + and_item.push_back(x_eq_str); ++pos; + and_item.push_back(ctx.mk_eq_atom(y, y_concat)); + and_item.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_strlen(cropStr))); ++pos; + // and_item[pos++] = Z3_mk_eq(ctx, or_item[option], Z3_mk_eq(ctx, mk_length(t, y), mk_length(t, y_concat))); - // adding length constraint for _ = constStr seems slowing things down. - option++; + + arrangement_disjunction.push_back(mk_and(and_item)); } } @@ -3678,15 +3679,16 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { if (can_two_nodes_eq(x, strAst_temp1)) { if (!avoidLoopCut || !(has_self_cut(x, n))) { // break down option 3-2 - or_item.push_back(ctx.mk_eq_atom(xorFlag, mk_int(option))); + expr_ref_vector and_item(mgr); expr_ref temp1_y(mk_concat(temp1, y), mgr); - and_item.push_back(ctx.mk_eq_atom(or_item.get(option), ctx.mk_eq_atom(x, strAst_temp1))); ++pos; - and_item.push_back(ctx.mk_eq_atom(or_item.get(option), ctx.mk_eq_atom(n, temp1_y))); ++pos; + and_item.push_back(ctx.mk_eq_atom(x, strAst_temp1)); ++pos; + and_item.push_back(ctx.mk_eq_atom(n, temp1_y)); ++pos; - and_item.push_back(ctx.mk_eq_atom(or_item.get(option), ctx.mk_eq_atom(mk_strlen(x), - m_autil.mk_add(mk_strlen(strAst), mk_strlen(temp1)) )) ); ++pos; - option++; + and_item.push_back(ctx.mk_eq_atom(mk_strlen(x), + m_autil.mk_add(mk_strlen(strAst), mk_strlen(temp1)) ) ); ++pos; + + arrangement_disjunction.push_back(mk_and(and_item)); add_cut_info_merge(temp1, sLevel, x); add_cut_info_merge(temp1, sLevel, n); @@ -3698,13 +3700,8 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { } - if (option > 0) { - if (option == 1) { - and_item.push_back(or_item.get(0)); - } else { - and_item.push_back(mk_or(or_item)); - } - expr_ref implyR(mk_and(and_item), mgr); + if (!arrangement_disjunction.empty()) { + expr_ref implyR(mk_or(arrangement_disjunction), mgr); if (opt_AssertStrongerArrangements) { expr_ref ax_lhs(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); @@ -3713,6 +3710,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { } else { assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); } + assert_axiom(generate_mutual_exclusion(arrangement_disjunction)); } else { TRACE("t_str", tout << "STOP: should not split two eq. concats" << std::endl;); } @@ -4066,32 +4064,30 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { refresh_theory_var(commonVar); } - expr_ref_vector or_item(mgr); + expr_ref_vector arrangement_disjunction(mgr); int option = 0; - expr_ref_vector and_item(mgr); int pos = 1; if (!avoidLoopCut || !has_self_cut(m, y)) { - expr_ref or_item_option(ctx.mk_eq_atom(xorFlag, mk_int(option)), mgr); - or_item.push_back(or_item_option); + expr_ref_vector and_item(mgr); expr_ref str1_commonVar(mk_concat(str1Ast, commonVar), mgr); - and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(m, str1_commonVar))); + and_item.push_back(ctx.mk_eq_atom(m, str1_commonVar)); pos += 1; expr_ref commonVar_str2(mk_concat(commonVar, str2Ast), mgr); - and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(y, commonVar_str2))); + and_item.push_back(ctx.mk_eq_atom(y, commonVar_str2)); pos += 1; - and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(mk_strlen(m), - m_autil.mk_add(mk_strlen(str1Ast), mk_strlen(commonVar)) ))); + and_item.push_back(ctx.mk_eq_atom(mk_strlen(m), + m_autil.mk_add(mk_strlen(str1Ast), mk_strlen(commonVar)) )); pos += 1; // addItems[0] = mk_length(t, commonVar); // addItems[1] = mk_length(t, str2Ast); // and_item[pos++] = Z3_mk_eq(ctx, or_item[option], Z3_mk_eq(ctx, mk_length(t, y), Z3_mk_add(ctx, 2, addItems))); - option++; + arrangement_disjunction.push_back(mk_and(and_item)); } else { loopDetected = true; TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); @@ -4102,34 +4098,34 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { int overLen = *itor; std::string prefix = str1Value.substr(0, str1Len - overLen); std::string suffix = str2Value.substr(overLen, str2Len - overLen); - expr_ref or_item_option(ctx.mk_eq_atom(xorFlag, mk_int(option)), mgr); - or_item.push_back(or_item_option); + + expr_ref_vector and_item(mgr); expr_ref prefixAst(m_strutil.mk_string(prefix), mgr); expr_ref x_eq_prefix(ctx.mk_eq_atom(m, prefixAst), mgr); - and_item.push_back(ctx.mk_eq_atom(or_item_option, x_eq_prefix)); + and_item.push_back(x_eq_prefix); pos += 1; - and_item.push_back(ctx.mk_eq_atom(or_item_option, - ctx.mk_eq_atom(mk_strlen(m), mk_strlen(prefixAst)))); + and_item.push_back( + ctx.mk_eq_atom(mk_strlen(m), mk_strlen(prefixAst))); pos += 1; // adding length constraint for _ = constStr seems slowing things down. expr_ref suffixAst(m_strutil.mk_string(suffix), mgr); expr_ref y_eq_suffix(ctx.mk_eq_atom(y, suffixAst), mgr); - and_item.push_back(ctx.mk_eq_atom(or_item_option, y_eq_suffix)); + and_item.push_back(y_eq_suffix); pos += 1; - and_item.push_back(ctx.mk_eq_atom(or_item_option, ctx.mk_eq_atom(mk_strlen(y), mk_strlen(suffixAst)))); + and_item.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_strlen(suffixAst))); pos += 1; - option++; + arrangement_disjunction.push_back(mk_and(and_item)); } // case 6: concat("str1", y) = concat(m, "str2") - and_item.push_back(mk_or(or_item)); - expr_ref implyR(mk_and(and_item), mgr); + + expr_ref implyR(mk_or(arrangement_disjunction), mgr); if (opt_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); @@ -4137,6 +4133,7 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { } else { assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); } + assert_axiom(generate_mutual_exclusion(arrangement_disjunction)); } void theory_str::process_unroll_eq_const_str(expr * unrollFunc, expr * constStr) { From 515cd4a3f33cf2e4509cce349dc6cabb8260ee5c Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 8 Dec 2016 14:49:38 -0500 Subject: [PATCH 281/562] add boolean case split in theory_str::solve_concat_eq_str --- src/smt/theory_str.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index d524bffe7..0f434900e 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -6201,10 +6201,10 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { int xor_pos = 0; int and_count = 1; - expr_ref_vector xor_items(m); - expr_ref_vector and_items(m); + expr_ref_vector arrangement_disjunction(m); for (int i = 0; i < concatStrLen + 1; ++i) { + expr_ref_vector and_items(m); std::string prefixStr = const_str.substr(0, i); std::string suffixStr = const_str.substr(i, concatStrLen - i); // skip invalid options @@ -6214,32 +6214,36 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { if (is_concat(to_app(arg2)) && !can_concat_eq_str(arg2, suffixStr)) { continue; } - expr_ref xorAst(ctx.mk_eq_atom(xorFlag, m_autil.mk_numeral(rational(xor_pos), true)), m); - xor_items.push_back(xorAst); - xor_pos += 1; expr_ref prefixAst(m_strutil.mk_string(prefixStr), m); expr_ref arg1_eq (ctx.mk_eq_atom(arg1, prefixAst), m); - and_items.push_back(ctx.mk_eq_atom(xorAst, arg1_eq)); + and_items.push_back(arg1_eq); and_count += 1; expr_ref suffixAst(m_strutil.mk_string(suffixStr), m); expr_ref arg2_eq (ctx.mk_eq_atom(arg2, suffixAst), m); - and_items.push_back(ctx.mk_eq_atom(xorAst, arg2_eq)); + and_items.push_back(arg2_eq); and_count += 1; + + arrangement_disjunction.push_back(mk_and(and_items)); } expr_ref implyL(ctx.mk_eq_atom(concat, str), m); expr_ref implyR1(m); - if (xor_pos == 0) { + if (arrangement_disjunction.empty()) { // negate expr_ref concat_eq_str(ctx.mk_eq_atom(concat, str), m); expr_ref negate_ast(m.mk_not(concat_eq_str), m); assert_axiom(negate_ast); } else { - and_items.push_back(mk_or(xor_items)); - implyR1 = mk_and(and_items); - assert_implication(implyL, implyR1); + implyR1 = mk_or(arrangement_disjunction); + if (opt_AssertStrongerArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom(implyL, implyR1), m); + assert_axiom(ax_strong); + } else { + assert_implication(implyL, implyR1); + } + assert_axiom(generate_mutual_exclusion(arrangement_disjunction)); } } /* (arg1Len != 1 || arg2Len != 1) */ } /* if (Concat(arg1, arg2) == NULL) */ From 737565180fbe21c20e7395b32fe40b51d93aeba2 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 9 Dec 2016 16:55:34 -0500 Subject: [PATCH 282/562] disable stronger arrangements in theory_str for now --- src/smt/theory_str.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 0f434900e..92920c220 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -44,7 +44,7 @@ theory_str::theory_str(ast_manager & m): opt_CheckVariableScope(true), opt_UseFastLengthTesterCache(true), opt_UseFastValueTesterCache(true), - opt_AssertStrongerArrangements(true), + opt_AssertStrongerArrangements(false), /* Internal setup */ search_started(false), m_autil(m), From e9411e5b8c6d80de3f8866071ae89cc4f7df431c Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 9 Dec 2016 17:12:29 -0500 Subject: [PATCH 283/562] explicitly re-introduce string axioms on refreshed string theory vars this fixes at least one case (kaluza/unsat/big/9650.smt2) where a string could have a negative length value due to a constraint that went out of scope --- src/smt/theory_str.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 92920c220..a6d93e70b 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -320,6 +320,9 @@ void theory_str::refresh_theory_var(expr * e) { enode * en = ensure_enode(e); theory_var v = mk_var(en); TRACE("t_str_detail", tout << "refresh " << mk_pp(e, get_manager()) << ": v#" << v << std::endl;); + // TODO this is probably sub-optimal + // TODO case where the refreshed var must be non-empty? + m_basicstr_axiom_todo.push_back(en); } theory_var theory_str::mk_var(enode* n) { From 09053b831dd6bb4948045d6a43ed24b636e00382 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 9 Dec 2016 17:23:39 -0500 Subject: [PATCH 284/562] enforce nonempty string constraint on refreshed nonempty string vars --- src/smt/theory_str.cpp | 34 ++++++++++++++++++++++++++++++---- src/smt/theory_str.h | 1 + 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index a6d93e70b..b9e9e748f 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -321,7 +321,6 @@ void theory_str::refresh_theory_var(expr * e) { theory_var v = mk_var(en); TRACE("t_str_detail", tout << "refresh " << mk_pp(e, get_manager()) << ": v#" << v << std::endl;); // TODO this is probably sub-optimal - // TODO case where the refreshed var must be non-empty? m_basicstr_axiom_todo.push_back(en); } @@ -617,6 +616,28 @@ app * theory_str::mk_regex_rep_var() { return a; } +void theory_str::add_nonempty_constraint(expr * s) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + expr_ref ax1(m.mk_not(ctx.mk_eq_atom(s, m_strutil.mk_string(""))), m); + assert_axiom(ax1); + + { + // build LHS + expr_ref len_str(mk_strlen(s), m); + SASSERT(len_str); + // build RHS + expr_ref zero(m_autil.mk_numeral(rational(0), true), m); + SASSERT(zero); + // build LHS > RHS and assert + // we have to build !(LHS <= RHS) instead + expr_ref lhs_gt_rhs(m.mk_not(m_autil.mk_le(len_str, zero)), m); + SASSERT(lhs_gt_rhs); + assert_axiom(lhs_gt_rhs); + } +} + app * theory_str::mk_nonempty_str_var() { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -639,14 +660,14 @@ app * theory_str::mk_nonempty_str_var() { // assert a variation of the basic string axioms that ensures this string is nonempty { // build LHS - expr * len_str = mk_strlen(a); + expr_ref len_str(mk_strlen(a), m); SASSERT(len_str); // build RHS - expr * zero = m_autil.mk_numeral(rational(0), true); + expr_ref zero(m_autil.mk_numeral(rational(0), true), m); SASSERT(zero); // build LHS > RHS and assert // we have to build !(LHS <= RHS) instead - app * lhs_gt_rhs = m.mk_not(m_autil.mk_le(len_str, zero)); + expr_ref lhs_gt_rhs(m.mk_not(m_autil.mk_le(len_str, zero)), m); SASSERT(lhs_gt_rhs); assert_axiom(lhs_gt_rhs); } @@ -2847,7 +2868,9 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } // TODO do I need to refresh the xorFlag, which is an integer var, and if so, how? refresh_theory_var(t1); + add_nonempty_constraint(t1); refresh_theory_var(t2); + add_nonempty_constraint(t2); } // For split types 0 through 2, we can get away with providing @@ -3190,6 +3213,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { } // TODO refresh xorFlag? refresh_theory_var(temp1); + add_nonempty_constraint(temp1); } int splitType = -1; @@ -3515,6 +3539,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { xorFlag = varForBreakConcat[key2][1]; } refresh_theory_var(temp1); + add_nonempty_constraint(temp1); } @@ -4065,6 +4090,7 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { xorFlag = (entry2->second)[1]; } refresh_theory_var(commonVar); + add_nonempty_constraint(commonVar); } expr_ref_vector arrangement_disjunction(mgr); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 29f5c2336..b3667bdec 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -341,6 +341,7 @@ namespace smt { app * mk_regex_rep_var(); app * mk_unroll_bound_var(); app * mk_unroll_test_var(); + void add_nonempty_constraint(expr * s); bool is_concat(app const * a) const { return a->is_app_of(get_id(), OP_STRCAT); } bool is_concat(enode const * n) const { return is_concat(n->get_owner()); } From f5bc17b864a7989b339bbe921aae1bf18f3ecbf0 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 13 Dec 2016 16:12:57 -0500 Subject: [PATCH 285/562] theory_str params module, WIP --- src/smt/params/smt_params.h | 2 ++ src/smt/params/smt_params_helper.pyg | 3 +- src/smt/params/theory_str_params.cpp | 24 ++++++++++++++++ src/smt/params/theory_str_params.h | 42 ++++++++++++++++++++++++++++ src/smt/smt_setup.cpp | 4 +-- src/smt/theory_str.cpp | 40 +++++++++++++------------- src/smt/theory_str.h | 16 +++-------- 7 files changed, 96 insertions(+), 35 deletions(-) create mode 100644 src/smt/params/theory_str_params.cpp create mode 100644 src/smt/params/theory_str_params.h diff --git a/src/smt/params/smt_params.h b/src/smt/params/smt_params.h index 9c1eec649..27071bd9e 100644 --- a/src/smt/params/smt_params.h +++ b/src/smt/params/smt_params.h @@ -25,6 +25,7 @@ Revision History: #include"theory_arith_params.h" #include"theory_array_params.h" #include"theory_bv_params.h" +#include"theory_str_params.h" #include"theory_pb_params.h" #include"theory_datatype_params.h" #include"preprocessor_params.h" @@ -75,6 +76,7 @@ struct smt_params : public preprocessor_params, public theory_arith_params, public theory_array_params, public theory_bv_params, + public theory_str_params, public theory_pb_params, public theory_datatype_params { bool m_display_proof; diff --git a/src/smt/params/smt_params_helper.pyg b/src/smt/params/smt_params_helper.pyg index a9f6ccc18..49a786e69 100644 --- a/src/smt/params/smt_params_helper.pyg +++ b/src/smt/params/smt_params_helper.pyg @@ -61,5 +61,6 @@ def_module_params(module_name='smt', ('dack.gc', UINT, 2000, 'Dynamic ackermannization garbage collection frequency (per conflict)'), ('dack.gc_inv_decay', DOUBLE, 0.8, 'Dynamic ackermannization garbage collection decay'), ('dack.threshold', UINT, 10, ' number of times the congruence rule must be used before Leibniz\'s axiom is expanded'), - ('core.validate', BOOL, False, 'validate unsat core produced by SMT context') + ('core.validate', BOOL, False, 'validate unsat core produced by SMT context'), + ('str.strong_arrangements', BOOL, True, 'assert equivalences instead of implications when generating string arrangement axioms') )) diff --git a/src/smt/params/theory_str_params.cpp b/src/smt/params/theory_str_params.cpp new file mode 100644 index 000000000..c1fcb0412 --- /dev/null +++ b/src/smt/params/theory_str_params.cpp @@ -0,0 +1,24 @@ +/*++ +Module Name: + + theory_str_params.cpp + +Abstract: + + Parameters for string theory plugin + +Author: + + Murphy Berzish (mtrberzi) 2016-12-13 + +Revision History: + +--*/ + +#include"theory_str_params.h" +#include"smt_params_helper.hpp" + +void theory_str_params::updt_params(params_ref const & _p) { + smt_params_helper p(_p); + m_AssertStrongerArrangements = p.str_strong_arrangements(); +} diff --git a/src/smt/params/theory_str_params.h b/src/smt/params/theory_str_params.h new file mode 100644 index 000000000..480ad1479 --- /dev/null +++ b/src/smt/params/theory_str_params.h @@ -0,0 +1,42 @@ +/*++ +Module Name: + + theory_str_params.h + +Abstract: + + Parameters for string theory plugin + +Author: + + Murphy Berzish (mtrberzi) 2016-12-13 + +Revision History: + +--*/ + +#ifndef THEORY_STR_PARAMS_H +#define THEORY_STR_PARAMS_H + +#include"params.h" + +struct theory_str_params { + /* + * If AssertStrongerArrangements is set to true, + * the implications that would normally be asserted during arrangement generation + * will instead be asserted as equivalences. + * This is a stronger version of the standard axiom. + * The Z3str2 axioms can be simulated by setting this to false. + */ + bool m_AssertStrongerArrangements; + + theory_str_params(params_ref const & p = params_ref()): + m_AssertStrongerArrangements(true) + { + updt_params(p); + } + + void updt_params(params_ref const & p); +}; + +#endif /* THEORY_STR_PARAMS_H */ diff --git a/src/smt/smt_setup.cpp b/src/smt/smt_setup.cpp index 117b606fd..7cbfd0b2e 100644 --- a/src/smt/smt_setup.cpp +++ b/src/smt/smt_setup.cpp @@ -707,7 +707,7 @@ namespace smt { void setup::setup_QF_S() { m_context.register_plugin(alloc(smt::theory_mi_arith, m_manager, m_params)); - m_context.register_plugin(alloc(smt::theory_str, m_manager)); + m_context.register_plugin(alloc(smt::theory_str, m_manager, m_params)); } bool is_arith(static_features const & st) { @@ -839,7 +839,7 @@ namespace smt { void setup::setup_str() { setup_arith(); - m_context.register_plugin(alloc(theory_str, m_manager)); + m_context.register_plugin(alloc(theory_str, m_manager, m_params)); } void setup::setup_unknown() { diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index b9e9e748f..4eb15d6ad 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -29,8 +29,9 @@ Revision History: namespace smt { -theory_str::theory_str(ast_manager & m): +theory_str::theory_str(ast_manager & m, theory_str_params const & params): theory(m.mk_family_id("str")), + m_params(params), /* Options */ opt_AggressiveLengthTesting(false), opt_AggressiveValueTesting(false), @@ -44,7 +45,6 @@ theory_str::theory_str(ast_manager & m): opt_CheckVariableScope(true), opt_UseFastLengthTesterCache(true), opt_UseFastValueTesterCache(true), - opt_AssertStrongerArrangements(false), /* Internal setup */ search_started(false), m_autil(m), @@ -2911,7 +2911,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { add_cut_info_merge(t1, sLevel, m); add_cut_info_merge(t1, sLevel, y); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); assert_axiom(ax_strong); } else { @@ -2963,7 +2963,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { add_cut_info_merge(t2, sLevel, x); add_cut_info_merge(t2, sLevel, n); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); assert_axiom(ax_strong); } else { @@ -3071,7 +3071,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { if (!arrangement_disjunction.empty()) { expr_ref premise(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); expr_ref conclusion(mk_or(arrangement_disjunction), mgr); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(premise, conclusion), mgr); assert_axiom(ax_strong); } else { @@ -3272,7 +3272,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { add_cut_info_merge(temp1, sLevel, y); add_cut_info_merge(temp1, sLevel, m); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); assert_axiom(ax_strong); } else { @@ -3340,7 +3340,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { expr_ref ax_l(mk_and(l_items), mgr); expr_ref ax_r(mk_and(r_items), mgr); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); assert_axiom(ax_strong); } else { @@ -3404,7 +3404,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { if (!arrangement_disjunction.empty()) { expr_ref implyR(mk_or(arrangement_disjunction), mgr); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref implyLHS(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); expr_ref ax_strong(ctx.mk_eq_atom(implyLHS, implyR), mgr); assert_axiom(ax_strong); @@ -3592,7 +3592,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { r_items.push_back(ctx.mk_eq_atom(x, prefixAst)); r_items.push_back(ctx.mk_eq_atom(y, suf_n_concat)); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(ax_l, mk_and(r_items)), mgr); assert_axiom(ax_strong); } else { @@ -3612,7 +3612,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { expr_ref ax_l(mgr.mk_and(ax_l1, ax_l2), mgr); expr_ref ax_r(mgr.mk_and(ctx.mk_eq_atom(x, strAst), ctx.mk_eq_atom(y, n)), mgr); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); assert_axiom(ax_strong); } else { @@ -3650,7 +3650,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { add_cut_info_merge(temp1, sLevel, x); add_cut_info_merge(temp1, sLevel, n); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); assert_axiom(ax_strong); } else { @@ -3731,7 +3731,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { if (!arrangement_disjunction.empty()) { expr_ref implyR(mk_or(arrangement_disjunction), mgr); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref ax_lhs(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); expr_ref ax_strong(ctx.mk_eq_atom(ax_lhs, implyR), mgr); assert_axiom(ax_strong); @@ -3813,7 +3813,7 @@ void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { if (!in_same_eqc(tmpAst, n)) { // break down option 4-1 expr_ref implyR(ctx.mk_eq_atom(n, tmpAst), mgr); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); assert_axiom(ax_strong); } else { @@ -3825,7 +3825,7 @@ void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { //break down option 4-2 expr_ref implyR(ctx.mk_eq_atom(n, y), mgr); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); assert_axiom(ax_strong); } else { @@ -3838,7 +3838,7 @@ void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { if (!in_same_eqc(y, tmpAst)) { //break down option 4-3 expr_ref implyR(ctx.mk_eq_atom(y, tmpAst), mgr); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); assert_axiom(ax_strong); } else { @@ -3915,7 +3915,7 @@ void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { expr_ref x_deltaStr(mk_concat(x, m_strutil.mk_string(deltaStr)), mgr); if (!in_same_eqc(m, x_deltaStr)) { expr_ref implyR(ctx.mk_eq_atom(m, x_deltaStr), mgr); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); assert_axiom(ax_strong); } else { @@ -3926,7 +3926,7 @@ void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { // test if (!in_same_eqc(x, m)) { expr_ref implyR(ctx.mk_eq_atom(x, m), mgr); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); assert_axiom(ax_strong); } else { @@ -3938,7 +3938,7 @@ void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { expr_ref m_deltaStr(mk_concat(m, m_strutil.mk_string(deltaStr)), mgr); if (!in_same_eqc(x, m_deltaStr)) { expr_ref implyR(ctx.mk_eq_atom(x, m_deltaStr), mgr); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); assert_axiom(ax_strong); } else { @@ -4156,7 +4156,7 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { expr_ref implyR(mk_or(arrangement_disjunction), mgr); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); assert_axiom(ax_strong); } else { @@ -6266,7 +6266,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { assert_axiom(negate_ast); } else { implyR1 = mk_or(arrangement_disjunction); - if (opt_AssertStrongerArrangements) { + if (m_params.m_AssertStrongerArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(implyL, implyR1), m); assert_axiom(ax_strong); } else { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index b3667bdec..30bf0b080 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -18,6 +18,7 @@ Revision History: #define _THEORY_STR_H_ #include"smt_theory.h" +#include"theory_str_params.h" #include"trail.h" #include"th_rewriter.h" #include"value_factory.h" @@ -97,7 +98,7 @@ namespace smt { typedef map > string_map; protected: - // Some options that control how the solver operates. + theory_str_params const & m_params; /* * If AggressiveLengthTesting is true, we manipulate the phase of length tester equalities @@ -189,15 +190,6 @@ namespace smt { */ bool opt_UseFastValueTesterCache; - /* - * If AssertStrongerArrangements is set to true, - * the implications that would normally be asserted during arrangement generation - * will instead be asserted as equivalences. - * This is a stronger version of the regular axiom. - * The default (Z3str2) behaviour is to set this to false. - */ - bool opt_AssertStrongerArrangements; - bool search_started; arith_util m_autil; str_util m_strutil; @@ -548,7 +540,7 @@ namespace smt { void refresh_theory_var(expr * e); public: - theory_str(ast_manager & m); + theory_str(ast_manager & m, theory_str_params const & params); virtual ~theory_str(); virtual char const * get_name() const { return "strings"; } @@ -569,7 +561,7 @@ namespace smt { virtual void new_eq_eh(theory_var, theory_var); virtual void new_diseq_eh(theory_var, theory_var); - virtual theory* mk_fresh(context*) { return alloc(theory_str, get_manager()); } + virtual theory* mk_fresh(context*) { return alloc(theory_str, get_manager(), m_params); } virtual void init_search_eh(); virtual void relevant_eh(app * n); virtual void assign_eh(bool_var v, bool is_true); From bced5828f7c1dbcab586709a2a6f067a97fab1f7 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 13 Dec 2016 17:20:58 -0500 Subject: [PATCH 286/562] theory_str parameters --- src/smt/params/smt_params_helper.pyg | 4 +++- src/smt/params/theory_str_params.cpp | 2 ++ src/smt/params/theory_str_params.h | 16 +++++++++++++++- src/smt/theory_str.cpp | 14 ++++++-------- src/smt/theory_str.h | 12 ------------ 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/smt/params/smt_params_helper.pyg b/src/smt/params/smt_params_helper.pyg index 49a786e69..feec8b01c 100644 --- a/src/smt/params/smt_params_helper.pyg +++ b/src/smt/params/smt_params_helper.pyg @@ -62,5 +62,7 @@ def_module_params(module_name='smt', ('dack.gc_inv_decay', DOUBLE, 0.8, 'Dynamic ackermannization garbage collection decay'), ('dack.threshold', UINT, 10, ' number of times the congruence rule must be used before Leibniz\'s axiom is expanded'), ('core.validate', BOOL, False, 'validate unsat core produced by SMT context'), - ('str.strong_arrangements', BOOL, True, 'assert equivalences instead of implications when generating string arrangement axioms') + ('str.strong_arrangements', BOOL, True, 'assert equivalences instead of implications when generating string arrangement axioms'), + ('str.aggressive_length_testing', BOOL, False, 'prioritize testing concrete length values over generating more options'), + ('str.aggressive_value_testing', BOOL, False, 'prioritize testing concrete string constant values over generating more options') )) diff --git a/src/smt/params/theory_str_params.cpp b/src/smt/params/theory_str_params.cpp index c1fcb0412..f7a562842 100644 --- a/src/smt/params/theory_str_params.cpp +++ b/src/smt/params/theory_str_params.cpp @@ -21,4 +21,6 @@ Revision History: void theory_str_params::updt_params(params_ref const & _p) { smt_params_helper p(_p); m_AssertStrongerArrangements = p.str_strong_arrangements(); + m_AggressiveLengthTesting = p.str_aggressive_length_testing(); + m_AggressiveValueTesting = p.str_aggressive_value_testing(); } diff --git a/src/smt/params/theory_str_params.h b/src/smt/params/theory_str_params.h index 480ad1479..78c78089e 100644 --- a/src/smt/params/theory_str_params.h +++ b/src/smt/params/theory_str_params.h @@ -30,8 +30,22 @@ struct theory_str_params { */ bool m_AssertStrongerArrangements; + /* + * If AggressiveLengthTesting is true, we manipulate the phase of length tester equalities + * to prioritize trying concrete length options over choosing the "more" option. + */ + bool m_AggressiveLengthTesting; + + /* + * Similarly, if AggressiveValueTesting is true, we manipulate the phase of value tester equalities + * to prioritize trying concrete value options over choosing the "more" option. + */ + bool m_AggressiveValueTesting; + theory_str_params(params_ref const & p = params_ref()): - m_AssertStrongerArrangements(true) + m_AssertStrongerArrangements(true), + m_AggressiveLengthTesting(false), + m_AggressiveValueTesting(false) { updt_params(p); } diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 4eb15d6ad..fe89b4662 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -33,8 +33,6 @@ theory_str::theory_str(ast_manager & m, theory_str_params const & params): theory(m.mk_family_id("str")), m_params(params), /* Options */ - opt_AggressiveLengthTesting(false), - opt_AggressiveValueTesting(false), opt_AggressiveUnrollTesting(true), opt_EagerStringConstantLengthAssertions(true), opt_VerifyFinalCheckProgress(true), @@ -8364,7 +8362,7 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * << "val_indicator = " << mk_ismt2_pp(val_indicator, m) << std::endl << "lenstr = " << lenStr << std::endl << "tries = " << tries << std::endl; - if (opt_AggressiveValueTesting) { + if (m_params.m_AggressiveValueTesting) { tout << "note: aggressive value testing is enabled" << std::endl; } ); @@ -8408,7 +8406,7 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * for (long long i = l; i < h; i++) { // TODO can we share the val_indicator constants with the length tester cache? orList.push_back(m.mk_eq(val_indicator, m_strutil.mk_string(longlong_to_string(i).c_str()) )); - if (opt_AggressiveValueTesting) { + if (m_params.m_AggressiveValueTesting) { literal l = mk_eq(val_indicator, m_strutil.mk_string(longlong_to_string(i).c_str()), false); ctx.mark_as_relevant(l); ctx.force_phase(l); @@ -8429,7 +8427,7 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * } if (!coverAll) { orList.push_back(m.mk_eq(val_indicator, m_strutil.mk_string("more"))); - if (opt_AggressiveValueTesting) { + if (m_params.m_AggressiveValueTesting) { literal l = mk_eq(val_indicator, m_strutil.mk_string("more"), false); ctx.mark_as_relevant(l); ctx.force_phase(~l); @@ -8980,7 +8978,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr TRACE("t_str_detail", tout << "building andList and orList" << std::endl; - if (opt_AggressiveLengthTesting) { + if (m_params.m_AggressiveLengthTesting) { tout << "note: aggressive length testing is active" << std::endl; } ); @@ -9007,7 +9005,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); orList.push_back(or_expr); - if (opt_AggressiveLengthTesting) { + if (m_params.m_AggressiveLengthTesting) { literal l = mk_eq(indicator, str_indicator, false); ctx.mark_as_relevant(l); ctx.force_phase(l); @@ -9019,7 +9017,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr // TODO cache mk_string("more") orList.push_back(m.mk_eq(indicator, m_strutil.mk_string("more"))); - if (opt_AggressiveLengthTesting) { + if (m_params.m_AggressiveLengthTesting) { literal l = mk_eq(indicator, m_strutil.mk_string("more"), false); ctx.mark_as_relevant(l); ctx.force_phase(~l); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 30bf0b080..02b351167 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -100,18 +100,6 @@ namespace smt { protected: theory_str_params const & m_params; - /* - * If AggressiveLengthTesting is true, we manipulate the phase of length tester equalities - * to prioritize trying concrete length options over choosing the "more" option. - */ - bool opt_AggressiveLengthTesting; - - /* - * Similarly, if AggressiveValueTesting is true, we manipulate the phase of value tester equalities - * to prioritize trying concrete value options over choosing the "more" option. - */ - bool opt_AggressiveValueTesting; - /* * If AggressiveUnrollTesting is true, we manipulate the phase of regex unroll tester equalities * to prioritize trying concrete unroll counts over choosing the "more" option. From 27a2c20c1cf38e55cc4995749de4475864b5ef39 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 13 Dec 2016 19:38:40 -0500 Subject: [PATCH 287/562] add more parameters for theory_str --- src/smt/params/smt_params_helper.pyg | 6 +++++- src/smt/params/theory_str_params.cpp | 3 +++ src/smt/params/theory_str_params.h | 25 ++++++++++++++++++++++++- src/smt/theory_str.cpp | 13 +++++-------- src/smt/theory_str.h | 20 -------------------- 5 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/smt/params/smt_params_helper.pyg b/src/smt/params/smt_params_helper.pyg index feec8b01c..cf861a28a 100644 --- a/src/smt/params/smt_params_helper.pyg +++ b/src/smt/params/smt_params_helper.pyg @@ -64,5 +64,9 @@ def_module_params(module_name='smt', ('core.validate', BOOL, False, 'validate unsat core produced by SMT context'), ('str.strong_arrangements', BOOL, True, 'assert equivalences instead of implications when generating string arrangement axioms'), ('str.aggressive_length_testing', BOOL, False, 'prioritize testing concrete length values over generating more options'), - ('str.aggressive_value_testing', BOOL, False, 'prioritize testing concrete string constant values over generating more options') + ('str.aggressive_value_testing', BOOL, False, 'prioritize testing concrete string constant values over generating more options'), + ('str.aggressive_unroll_testing', BOOL, True, 'prioritize testing concrete regex unroll counts over generating more options'), + ('str.fast_length_tester_cache', BOOL, False, 'cache length tester constants instead of regenerating them'), + ('str.fast_value_tester_cache', BOOL, True, 'cache value tester constants instead of regenerating them') + )) diff --git a/src/smt/params/theory_str_params.cpp b/src/smt/params/theory_str_params.cpp index f7a562842..f952c6c87 100644 --- a/src/smt/params/theory_str_params.cpp +++ b/src/smt/params/theory_str_params.cpp @@ -23,4 +23,7 @@ void theory_str_params::updt_params(params_ref const & _p) { m_AssertStrongerArrangements = p.str_strong_arrangements(); m_AggressiveLengthTesting = p.str_aggressive_length_testing(); m_AggressiveValueTesting = p.str_aggressive_value_testing(); + m_AggressiveUnrollTesting = p.str_aggressive_unroll_testing(); + m_UseFastLengthTesterCache = p.str_fast_length_tester_cache(); + m_UseFastValueTesterCache = p.str_fast_value_tester_cache(); } diff --git a/src/smt/params/theory_str_params.h b/src/smt/params/theory_str_params.h index 78c78089e..f4e7ecf33 100644 --- a/src/smt/params/theory_str_params.h +++ b/src/smt/params/theory_str_params.h @@ -42,10 +42,33 @@ struct theory_str_params { */ bool m_AggressiveValueTesting; + /* + * If AggressiveUnrollTesting is true, we manipulate the phase of regex unroll tester equalities + * to prioritize trying concrete unroll counts over choosing the "more" option. + */ + bool m_AggressiveUnrollTesting; + + /* + * If UseFastLengthTesterCache is set to true, + * length tester terms will not be generated from scratch each time they are needed, + * but will be saved in a map and looked up. + */ + bool m_UseFastLengthTesterCache; + + /* + * If UseFastValueTesterCache is set to true, + * value tester terms will not be generated from scratch each time they are needed, + * but will be saved in a map and looked up. + */ + bool m_UseFastValueTesterCache; + theory_str_params(params_ref const & p = params_ref()): m_AssertStrongerArrangements(true), m_AggressiveLengthTesting(false), - m_AggressiveValueTesting(false) + m_AggressiveValueTesting(false), + m_AggressiveUnrollTesting(true), + m_UseFastLengthTesterCache(false), + m_UseFastValueTesterCache(true) { updt_params(p); } diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index fe89b4662..b18d51a98 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -33,7 +33,6 @@ theory_str::theory_str(ast_manager & m, theory_str_params const & params): theory(m.mk_family_id("str")), m_params(params), /* Options */ - opt_AggressiveUnrollTesting(true), opt_EagerStringConstantLengthAssertions(true), opt_VerifyFinalCheckProgress(true), opt_LCMUnrollStep(2), @@ -41,8 +40,6 @@ theory_str::theory_str(ast_manager & m, theory_str_params const & params): opt_DisableIntegerTheoryIntegration(false), opt_DeferEQCConsistencyCheck(false), opt_CheckVariableScope(true), - opt_UseFastLengthTesterCache(true), - opt_UseFastValueTesterCache(true), /* Internal setup */ search_started(false), m_autil(m), @@ -8414,7 +8411,7 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * std::string aStr = gen_val_string(len, options[i - l]); expr * strAst; - if (opt_UseFastValueTesterCache) { + if (m_params.m_UseFastValueTesterCache) { if (!valueTesterCache.find(aStr, strAst)) { strAst = m_strutil.mk_string(aStr); valueTesterCache.insert(aStr, strAst); @@ -8905,7 +8902,7 @@ expr * theory_str::gen_unroll_assign(expr * var, std::string lcmStr, expr * test TRACE("t_str_detail", tout << "entry: var = " << mk_pp(var, mgr) << ", lcmStr = " << lcmStr << ", l = " << l << ", h = " << h << std::endl;); - if (opt_AggressiveUnrollTesting) { + if (m_params.m_AggressiveUnrollTesting) { TRACE("t_str_detail", tout << "note: aggressive unroll testing is active" << std::endl;); } @@ -8916,7 +8913,7 @@ expr * theory_str::gen_unroll_assign(expr * var, std::string lcmStr, expr * test std::string iStr = int_to_string(i); expr_ref testerEqAst(ctx.mk_eq_atom(testerVar, m_strutil.mk_string(iStr)), mgr); TRACE("t_str_detail", tout << "testerEqAst = " << mk_pp(testerEqAst, mgr) << std::endl;); - if (opt_AggressiveUnrollTesting) { + if (m_params.m_AggressiveUnrollTesting) { literal l = mk_eq(testerVar, m_strutil.mk_string(iStr), false); ctx.mark_as_relevant(l); ctx.force_phase(l); @@ -8935,7 +8932,7 @@ expr * theory_str::gen_unroll_assign(expr * var, std::string lcmStr, expr * test } expr_ref testerEqMore(ctx.mk_eq_atom(testerVar, m_strutil.mk_string("more")), mgr); TRACE("t_str_detail", tout << "testerEqMore = " << mk_pp(testerEqMore, mgr) << std::endl;); - if (opt_AggressiveUnrollTesting) { + if (m_params.m_AggressiveUnrollTesting) { literal l = mk_eq(testerVar, m_strutil.mk_string("more"), false); ctx.mark_as_relevant(l); ctx.force_phase(~l); @@ -8985,7 +8982,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr for (int i = l; i < h; ++i) { expr_ref str_indicator(m); - if (opt_UseFastLengthTesterCache) { + if (m_params.m_UseFastLengthTesterCache) { rational ri(i); expr * lookup_val; if(lengthTesterCache.find(ri, lookup_val)) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 02b351167..2a9997517 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -100,12 +100,6 @@ namespace smt { protected: theory_str_params const & m_params; - /* - * If AggressiveUnrollTesting is true, we manipulate the phase of regex unroll tester equalities - * to prioritize trying concrete unroll counts over choosing the "more" option. - */ - bool opt_AggressiveUnrollTesting; - /* * Setting EagerStringConstantLengthAssertions to true allows some methods, * in particular internalize_term(), to add @@ -164,20 +158,6 @@ namespace smt { */ bool opt_CheckVariableScope; - /* - * If UseFastLengthTesterCache is set to true, - * length tester terms will not be generated from scratch each time they are needed, - * but will be saved in a map and looked up. - */ - bool opt_UseFastLengthTesterCache; - - /* - * If UseFastValueTesterCache is set to true, - * value tester terms will not be generated from scratch each time they are needed, - * but will be saved in a map and looked up. - */ - bool opt_UseFastValueTesterCache; - bool search_started; arith_util m_autil; str_util m_strutil; From 67e73077773b6fa136c8a4896f5f2e55cfd77e9b Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 14 Dec 2016 15:00:17 -0500 Subject: [PATCH 288/562] add cut var debug info, wip --- src/smt/theory_str.cpp | 39 ++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 2 ++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index b18d51a98..503485293 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2481,6 +2481,43 @@ expr_ref theory_str::generate_mutual_exclusion(expr_ref_vector & terms) { return final_result; } +void theory_str::print_cut_var(expr * node, std::ofstream & xout) { + ast_manager & m = get_manager(); + /* +#ifdef DEBUGLOG + __debugPrint(logFile, "\n>> CUT info of ["); + printZ3Node(t, node); + __debugPrint(logFile, "]\n"); + + if (cut_VARMap.find(node) != cut_VARMap.end()) { + if (!cut_VARMap[node].empty()) { + __debugPrint(logFile, "[%2d] {", cut_VARMap[node].top()->level); + std::map::iterator itor = cut_VARMap[node].top()->vars.begin(); + for (; itor != cut_VARMap[node].top()->vars.end(); itor++) { + printZ3Node(t, itor->first); + __debugPrint(logFile, ", "); + } + __debugPrint(logFile, "}\n"); + } else { + + } + } + __debugPrint(logFile, "------------------------\n\n"); +#endif +*/ + xout << "Cut info of " << mk_pp(node, m) << std::endl; + if (cut_var_map.contains(node)) { + if (!cut_var_map[node].empty()) { + xout << "[" << cut_var_map[node].top()->level << "] "; + std::map::iterator itor = cut_var_map[node].top()->vars.begin(); + for (; itor != cut_var_map[node].top()->vars.end(); ++itor) { + xout << mk_pp(itor->first, m) << ", "; + } + xout << std::endl; + } + } +} + /* * Handle two equivalent Concats. */ @@ -3013,7 +3050,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } else { loopDetected = true; TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - // TODO printCutVar(m, y); + TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); } // break option 2: diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 2a9997517..73f8d9dcc 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -439,6 +439,8 @@ namespace smt { void process_concat_eq_type5(expr * concatAst1, expr * concatAst2); void process_concat_eq_type6(expr * concatAst1, expr * concatAst2); + void print_cut_var(expr * node, std::ofstream & xout); + expr_ref generate_mutual_exclusion(expr_ref_vector & exprs); bool new_eq_check(expr * lhs, expr * rhs); From dd8cd8199ba06db25e34b7539aff8dc212e28881 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 16 Dec 2016 14:37:34 -0500 Subject: [PATCH 289/562] theory_str refcount debug messages and beginning theory case split --- src/ast/ast.cpp | 1 + src/smt/params/smt_params.cpp | 1 + src/smt/params/smt_params.h | 2 ++ src/smt/params/smt_params_helper.pyg | 4 ++-- src/smt/smt_context.cpp | 11 ++++++++++- src/smt/theory_str.cpp | 12 ++++++++++++ 6 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index a9a91ab2a..a822be37a 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -1767,6 +1767,7 @@ void ast_manager::delete_node(ast * n) { TRACE("ast", tout << "Deleting object " << n->m_id << " " << n << "\n";); CTRACE("del_quantifier", is_quantifier(n), tout << "deleting quantifier " << n->m_id << " " << n << "\n";); TRACE("mk_var_bug", tout << "del_ast: " << n->m_id << "\n";); + TRACE("t_str_refcount_hack", tout << "delete ast " << n->m_id << std::endl;); TRACE("ast_delete_node", tout << mk_bounded_pp(n, *this) << "\n";); SASSERT(m_ast_table.contains(n)); diff --git a/src/smt/params/smt_params.cpp b/src/smt/params/smt_params.cpp index 8222c3d60..a5b3e4867 100644 --- a/src/smt/params/smt_params.cpp +++ b/src/smt/params/smt_params.cpp @@ -31,6 +31,7 @@ void smt_params::updt_local_params(params_ref const & _p) { m_restart_strategy = static_cast(p.restart_strategy()); m_restart_factor = p.restart_factor(); m_case_split_strategy = static_cast(p.case_split()); + m_theory_case_split = p.theory_case_split(); m_delay_units = p.delay_units(); m_delay_units_threshold = p.delay_units_threshold(); m_preprocess = _p.get_bool("preprocess", true); // hidden parameter diff --git a/src/smt/params/smt_params.h b/src/smt/params/smt_params.h index 27071bd9e..55346d34f 100644 --- a/src/smt/params/smt_params.h +++ b/src/smt/params/smt_params.h @@ -111,6 +111,7 @@ struct smt_params : public preprocessor_params, case_split_strategy m_case_split_strategy; unsigned m_rel_case_split_order; bool m_lookahead_diseq; + bool m_theory_case_split; // ----------------------------------- // @@ -241,6 +242,7 @@ struct smt_params : public preprocessor_params, m_case_split_strategy(CS_ACTIVITY_DELAY_NEW), m_rel_case_split_order(0), m_lookahead_diseq(false), + m_theory_case_split(false), m_delay_units(false), m_delay_units_threshold(32), m_theory_resolve(false), diff --git a/src/smt/params/smt_params_helper.pyg b/src/smt/params/smt_params_helper.pyg index cf861a28a..3f2c6a54a 100644 --- a/src/smt/params/smt_params_helper.pyg +++ b/src/smt/params/smt_params_helper.pyg @@ -67,6 +67,6 @@ def_module_params(module_name='smt', ('str.aggressive_value_testing', BOOL, False, 'prioritize testing concrete string constant values over generating more options'), ('str.aggressive_unroll_testing', BOOL, True, 'prioritize testing concrete regex unroll counts over generating more options'), ('str.fast_length_tester_cache', BOOL, False, 'cache length tester constants instead of regenerating them'), - ('str.fast_value_tester_cache', BOOL, True, 'cache value tester constants instead of regenerating them') - + ('str.fast_value_tester_cache', BOOL, True, 'cache value tester constants instead of regenerating them'), + ('theory_case_split', BOOL, False, 'Allow the context to use heuristics involving theory case splits, which are a set of literals of which exactly one can be assigned True. If this option is false, the context will generate extra axioms to enforce this instead.') )) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 8958eae5f..741525dd2 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -2377,6 +2377,9 @@ namespace smt { */ unsigned context::pop_scope_core(unsigned num_scopes) { + TRACE("t_str_refcount_hack", tout << "begin pop_scope_core in smt_context" << std::endl;); + + if (m_manager.has_trace_stream()) m_manager.trace_stream() << "[pop] " << num_scopes << " " << m_scope_lvl << "\n"; @@ -2423,8 +2426,11 @@ namespace smt { ptr_vector::iterator it = m_theory_set.begin(); ptr_vector::iterator end = m_theory_set.end(); - for (; it != end; ++it) + for (; it != end; ++it) { + TRACE("t_str_refcount_hack", tout << "begin theory pop_scope_eh" << std::endl;); (*it)->pop_scope_eh(num_scopes); + TRACE("t_str_refcount_hack", tout << "end theory pop_scope_eh" << std::endl;); + } del_justifications(m_justifications, s.m_justifications_lim); @@ -2450,6 +2456,9 @@ namespace smt { reassert_units(units_to_reassert_lim); TRACE("pop_scope_detail", tout << "end of pop_scope: \n"; display(tout);); CASSERT("context", check_invariant()); + + TRACE("t_str_refcount_hack", tout << "end pop_scope_core in smt_context" << std::endl;); + return num_bool_vars; } diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 503485293..25a045ee8 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1805,6 +1805,7 @@ void theory_str::reset_eh() { * Then add an assertion: (y2 == (Concat ce m2)) AND ("str3" == (Concat abc x2)) -> (y2 != "str3") */ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { + TRACE("t_str_refcount_hack", tout << "begin new_eq_check in theory_str" << std::endl;); context & ctx = get_context(); ast_manager & m = get_manager(); @@ -1830,6 +1831,7 @@ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { expr_ref to_assert(m.mk_not(ctx.mk_eq_atom(eqc_nn1, eqc_nn2)), m); assert_axiom(to_assert); // this shouldn't use the integer theory at all, so we don't allow the option of quick-return + TRACE("t_str_refcount_hack", tout << "end new_eq_check in theory_str" << std::endl;); return false; } if (!check_length_consistency(eqc_nn1, eqc_nn2)) { @@ -1837,6 +1839,7 @@ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { if (opt_NoQuickReturn_IntegerTheory){ TRACE("t_str_detail", tout << "continuing in new_eq_check() due to opt_NoQuickReturn_IntegerTheory" << std::endl;); } else { + TRACE("t_str_refcount_hack", tout << "end new_eq_check in theory_str" << std::endl;); return false; } } @@ -1855,6 +1858,7 @@ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { } // okay, all checks here passed + TRACE("t_str_refcount_hack", tout << "end new_eq_check in theory_str" << std::endl;); return true; } @@ -6859,14 +6863,20 @@ void theory_str::check_variable_scope() { } void theory_str::pop_scope_eh(unsigned num_scopes) { + TRACE("t_str_refcount_hack", tout << "begin pop_scope_eh in theory_str" << std::endl;); + sLevel -= num_scopes; TRACE("t_str", tout << "pop " << num_scopes << " to " << sLevel << std::endl;); // TODO: figure out what's going out of scope and why context & ctx = get_context(); ast_manager & m = get_manager(); + // { // expr_ref_vector assignments(m); // ctx.get_assignments(assignments); + // TRACE("t_str_refcount_hack", tout << "assignment vector about to go out of scope" << std::endl;); + // } + // TRACE("t_str_refcount_hack", tout << "assignment vector has gone out of scope" << std::endl;); TRACE_CODE(if (is_trace_enabled("t_str_dump_assign_on_scope_change")) { dump_assignments(); }); @@ -6937,6 +6947,8 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { theory::pop_scope_eh(num_scopes); //check_variable_scope(); + + TRACE("t_str_refcount_hack", tout << "end pop_scope_eh in theory_str" << std::endl;); } void theory_str::dump_assignments() { From e85f9d33c4dceccded5436955a84f125a1e712d8 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 16 Dec 2016 15:50:03 -0500 Subject: [PATCH 290/562] add "legacy" support for theory case splits this replicates what was done in theory_str to add axioms excluding each pair of literals from being assigned True at the same time; no new heuristics are being used in smt_context (yet) --- src/smt/smt_context.cpp | 21 +++++++++++++++++++++ src/smt/smt_context.h | 8 ++++++++ src/smt/theory_str.cpp | 26 +++++++++++++++++++------- src/smt/theory_str.h | 2 +- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 741525dd2..907ea876b 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -2939,6 +2939,27 @@ namespace smt { assert_expr_core(e, pr); } + void context::mk_th_case_split(unsigned num_lits, literal * lits) { + TRACE("theory_case_split", display_literals_verbose(tout << "theory case split: ", num_lits, lits); tout << std::endl;); + // If we don't use the theory case split heuristic, + // for each pair of literals (l1, l2) we add the clause (~l1 OR ~l2) + // to enforce the condition that more than one literal can't be + // assigned 'true' simultaneously. + if (!m_fparams.m_theory_case_split) { + for (unsigned i = 0; i < num_lits; ++i) { + for (unsigned j = i+1; j < num_lits; ++j) { + literal l1 = lits[i]; + literal l2 = lits[j]; + literal excl[2] = {~l1, ~l2}; + justification * j_excl = 0; + mk_clause(2, excl, j_excl); + } + } + } else { + NOT_IMPLEMENTED_YET(); + } + } + bool context::reduce_assertions() { if (!m_asserted_formulas.inconsistent()) { SASSERT(at_base_level()); diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index 8b2453e31..5c52adc73 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -805,6 +805,14 @@ namespace smt { void mk_th_axiom(theory_id tid, literal l1, literal l2, literal l3, unsigned num_params = 0, parameter * params = 0); + /* + * Provide a hint to the core solver that the specified literals form a "theory case split". + * The core solver will enforce the condition that exactly one of these literals can be + * assigned 'true' at any time. + * We assume that the theory solver has already asserted the disjunction of these literals + * or some other axiom that means at least one of them must be assigned 'true'. + */ + void mk_th_case_split(unsigned num_lits, literal * lits); bool_var mk_bool_var(expr * n); diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 25a045ee8..89f31db5a 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2466,8 +2466,19 @@ void theory_str::infer_len_concat_equality(expr * nn1, expr * nn2) { */ } -expr_ref theory_str::generate_mutual_exclusion(expr_ref_vector & terms) { +void theory_str::generate_mutual_exclusion(expr_ref_vector & terms) { context & ctx = get_context(); + // pull each literal out of the arrangement disjunction + literal_vector ls; + for (unsigned i = 0; i < terms.size(); ++i) { + expr * e = terms.get(i); + literal l = ctx.get_literal(e); + ls.push_back(l); + } + ctx.mk_th_case_split(ls.size(), ls.c_ptr()); + + // old version, without special support in the context + /* ast_manager & m = get_manager(); expr_ref_vector result(m); @@ -2482,7 +2493,8 @@ expr_ref theory_str::generate_mutual_exclusion(expr_ref_vector & terms) { } expr_ref final_result(mk_and(result), m); - return final_result; + assert_axiom(final_result); + */ } void theory_str::print_cut_var(expr * node, std::ofstream & xout) { @@ -3114,7 +3126,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { assert_implication(premise, conclusion); } // assert mutual exclusion between each branch of the arrangement - assert_axiom(generate_mutual_exclusion(arrangement_disjunction)); + generate_mutual_exclusion(arrangement_disjunction); } else { TRACE("t_str", tout << "STOP: no split option found for two EQ concats." << std::endl;); } @@ -3447,7 +3459,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { } else { assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); } - assert_axiom(generate_mutual_exclusion(arrangement_disjunction)); + generate_mutual_exclusion(arrangement_disjunction); } else { TRACE("t_str", tout << "STOP: Should not split two EQ concats." << std::endl;); } @@ -3774,7 +3786,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { } else { assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); } - assert_axiom(generate_mutual_exclusion(arrangement_disjunction)); + generate_mutual_exclusion(arrangement_disjunction); } else { TRACE("t_str", tout << "STOP: should not split two eq. concats" << std::endl;); } @@ -4198,7 +4210,7 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { } else { assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); } - assert_axiom(generate_mutual_exclusion(arrangement_disjunction)); + generate_mutual_exclusion(arrangement_disjunction); } void theory_str::process_unroll_eq_const_str(expr * unrollFunc, expr * constStr) { @@ -6308,7 +6320,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } else { assert_implication(implyL, implyR1); } - assert_axiom(generate_mutual_exclusion(arrangement_disjunction)); + generate_mutual_exclusion(arrangement_disjunction); } } /* (arg1Len != 1 || arg2Len != 1) */ } /* if (Concat(arg1, arg2) == NULL) */ diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 73f8d9dcc..ffeea34e8 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -441,7 +441,7 @@ namespace smt { void print_cut_var(expr * node, std::ofstream & xout); - expr_ref generate_mutual_exclusion(expr_ref_vector & exprs); + void generate_mutual_exclusion(expr_ref_vector & exprs); bool new_eq_check(expr * lhs, expr * rhs); void group_terms_by_eqc(expr * n, std::set & concats, std::set & vars, std::set & consts); From e5d3e425f10aba1380018d68711154c3588face8 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 18 Dec 2016 15:23:05 -0500 Subject: [PATCH 291/562] theory_str caching of all string constants --- src/smt/theory_str.cpp | 155 ++++++++++++++++++++++++----------------- src/smt/theory_str.h | 8 +++ 2 files changed, 101 insertions(+), 62 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 89f31db5a..19e677acb 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -56,6 +56,9 @@ theory_str::theory_str(ast_manager & m, theory_str_params const & params): loopDetected(false), contains_map(m), string_int_conversion_terms(m), + totalCacheAccessCount(0), + cacheHitCount(0), + cacheMissCount(0), m_find(*this), m_trail_stack(*this) { @@ -66,6 +69,34 @@ theory_str::~theory_str() { m_trail_stack.reset(); } +expr * theory_str::mk_string(std::string str) { + ++totalCacheAccessCount; + expr * val; + if (stringConstantCache.find(str, val)) { + // cache hit + ++cacheHitCount; + TRACE("t_str_cache", tout << "cache hit: \"" << str << "\" (" + << cacheHitCount << " hits, " << cacheMissCount << " misses out of " + << totalCacheAccessCount << " accesses)" << std::endl;); + return val; + } else { + // cache miss + ++cacheMissCount; + TRACE("t_str_cache", tout << "cache miss: \"" << str << "\" (" + << cacheHitCount << " hits, " << cacheMissCount << " misses out of " + << totalCacheAccessCount << " accesses)" << std::endl;); + val = m_strutil.mk_string(str); + m_trail.push_back(val); + stringConstantCache.insert(str, val); + return val; + } +} + +expr * theory_str::mk_string(const char * str) { + std::string valStr(str); + return mk_string(valStr); +} + void theory_str::initialize_charset() { bool defaultCharset = true; if (defaultCharset) { @@ -615,7 +646,7 @@ void theory_str::add_nonempty_constraint(expr * s) { context & ctx = get_context(); ast_manager & m = get_manager(); - expr_ref ax1(m.mk_not(ctx.mk_eq_atom(s, m_strutil.mk_string(""))), m); + expr_ref ax1(m.mk_not(ctx.mk_eq_atom(s, mk_string(""))), m); assert_axiom(ax1); { @@ -685,7 +716,7 @@ app * theory_str::mk_unroll(expr * n, expr * bound) { m_trail.push_back(unrollFunc); expr_ref_vector items(m); - items.push_back(ctx.mk_eq_atom(ctx.mk_eq_atom(bound, mk_int(0)), ctx.mk_eq_atom(unrollFunc, m_strutil.mk_string("")))); + items.push_back(ctx.mk_eq_atom(ctx.mk_eq_atom(bound, mk_int(0)), ctx.mk_eq_atom(unrollFunc, mk_string("")))); items.push_back(m_autil.mk_ge(bound, mk_int(0))); items.push_back(m_autil.mk_ge(mk_strlen(unrollFunc), mk_int(0))); @@ -760,7 +791,7 @@ expr * theory_str::mk_concat_const_str(expr * n1, expr * n2) { m_strutil.is_string(v2, & n2_str_tmp); std::string n2_str(n2_str_tmp); std::string result = n1_str + n2_str; - return m_strutil.mk_string(result); + return mk_string(result); } else if (n1HasEqcValue && !n2HasEqcValue) { const char * n1_str_tmp; m_strutil.is_string(v1, & n1_str_tmp); @@ -1013,7 +1044,7 @@ void theory_str::try_eval_concat(enode * cat) { } if (constOK) { TRACE("t_str_detail", tout << "flattened to \"" << flattenedString << "\"" << std::endl;); - expr_ref constStr(m_strutil.mk_string(flattenedString), m); + expr_ref constStr(mk_string(flattenedString), m); expr_ref axiom(ctx.mk_eq_atom(a_cat, constStr), m); assert_axiom(axiom); } @@ -1132,7 +1163,7 @@ void theory_str::instantiate_basic_string_axioms(enode * str) { SASSERT(lhs); // build RHS of iff expr_ref empty_str(m); - empty_str = m_strutil.mk_string(""); + empty_str = mk_string(""); SASSERT(empty_str); expr_ref rhs(m); rhs = ctx.mk_eq_atom(a_str, empty_str); @@ -1203,7 +1234,7 @@ void theory_str::instantiate_axiom_CharAt(enode * e) { and_item.push_back(ctx.mk_eq_atom(mk_strlen(ts1), mk_int(1))); expr_ref thenBranch(m.mk_and(and_item.size(), and_item.c_ptr()), m); - expr_ref elseBranch(ctx.mk_eq_atom(ts1, m_strutil.mk_string("")), m); + expr_ref elseBranch(ctx.mk_eq_atom(ts1, mk_string("")), m); expr_ref axiom(m.mk_ite(cond, thenBranch, elseBranch), m); expr_ref reductionVar(ctx.mk_eq_atom(expr, ts1), m); @@ -1644,7 +1675,7 @@ void theory_str::instantiate_axiom_str_to_int(enode * e) { { expr_ref lhs(ctx.mk_eq_atom(ex, m_autil.mk_numeral(rational::zero(), true)), m); - expr_ref rhs(ctx.mk_eq_atom(S, m_strutil.mk_string("0")), m); + expr_ref rhs(ctx.mk_eq_atom(S, mk_string("0")), m); expr_ref axiom2(ctx.mk_eq_atom(lhs, rhs), m); SASSERT(axiom2); assert_axiom(axiom2); @@ -1656,7 +1687,7 @@ void theory_str::instantiate_axiom_str_to_int(enode * e) { expr_ref tl(mk_str_var("tl"), m); expr_ref conclusion1(ctx.mk_eq_atom(S, mk_concat(hd, tl)), m); expr_ref conclusion2(ctx.mk_eq_atom(mk_strlen(hd), m_autil.mk_numeral(rational::one(), true)), m); - expr_ref conclusion3(m.mk_not(ctx.mk_eq_atom(hd, m_strutil.mk_string("0"))), m); + expr_ref conclusion3(m.mk_not(ctx.mk_eq_atom(hd, mk_string("0"))), m); expr_ref conclusion(m.mk_and(conclusion1, conclusion2, conclusion3), m); SASSERT(premise); SASSERT(conclusion); @@ -1681,7 +1712,7 @@ void theory_str::instantiate_axiom_int_to_str(enode * e) { expr * N = ex->get_arg(0); { expr_ref axiom1_lhs(m.mk_not(m_autil.mk_ge(N, m_autil.mk_numeral(rational::zero(), true))), m); - expr_ref axiom1_rhs(ctx.mk_eq_atom(ex, m_strutil.mk_string("")), m); + expr_ref axiom1_rhs(ctx.mk_eq_atom(ex, mk_string("")), m); expr_ref axiom1(ctx.mk_eq_atom(axiom1_lhs, axiom1_rhs), m); SASSERT(axiom1); assert_axiom(axiom1); @@ -1766,7 +1797,7 @@ void theory_str::instantiate_axiom_RegexIn(enode * e) { expr_ref unrollFunc(mk_unroll(regex1, unrollCount), m); expr_ref_vector items(m); items.push_back(ctx.mk_eq_atom(ex, ctx.mk_eq_atom(str, unrollFunc))); - items.push_back(ctx.mk_eq_atom(ctx.mk_eq_atom(unrollCount, mk_int(0)), ctx.mk_eq_atom(unrollFunc, m_strutil.mk_string("")))); + items.push_back(ctx.mk_eq_atom(ctx.mk_eq_atom(unrollCount, mk_int(0)), ctx.mk_eq_atom(unrollFunc, mk_string("")))); expr_ref finalAxiom(mk_and(items), m); SASSERT(finalAxiom); assert_axiom(finalAxiom); @@ -1947,7 +1978,7 @@ expr * theory_str::eval_concat(expr * n1, expr * n2) { std::string n1_str = m_strutil.get_string_constant_value(v1); std::string n2_str = m_strutil.get_string_constant_value(v2); std::string result = n1_str + n2_str; - return m_strutil.mk_string(result); + return mk_string(result); } else if (n1HasEqcValue && !n2HasEqcValue) { if (m_strutil.get_string_constant_value(v1) == "") { return n2; @@ -2286,7 +2317,7 @@ expr * theory_str::simplify_concat(expr * node) { // no simplification possible return node; } else { - expr * resultAst = m_strutil.mk_string(""); + expr * resultAst = mk_string(""); for (unsigned i = 0; i < argVec.size(); ++i) { bool vArgHasEqcValue = false; expr * vArg = get_eqc_value(argVec[i], vArgHasEqcValue); @@ -3377,9 +3408,9 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { std::string part1Str = strValue.substr(0, lenDelta.get_unsigned()); std::string part2Str = strValue.substr(lenDelta.get_unsigned(), strValue.length() - lenDelta.get_unsigned()); - expr_ref prefixStr(m_strutil.mk_string(part1Str), mgr); + expr_ref prefixStr(mk_string(part1Str), mgr); expr_ref x_concat(mk_concat(m, prefixStr), mgr); - expr_ref cropStr(m_strutil.mk_string(part2Str), mgr); + expr_ref cropStr(mk_string(part2Str), mgr); if (can_two_nodes_eq(x, x_concat) && can_two_nodes_eq(y, cropStr)) { expr_ref_vector r_items(mgr); @@ -3436,9 +3467,9 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { for (int i = 0; i <= (int)strValue.size(); ++i) { std::string part1Str = strValue.substr(0, i); std::string part2Str = strValue.substr(i, strValue.size() - i); - expr_ref prefixStr(m_strutil.mk_string(part1Str), mgr); + expr_ref prefixStr(mk_string(part1Str), mgr); expr_ref x_concat(mk_concat(m, prefixStr), mgr); - expr_ref cropStr(m_strutil.mk_string(part2Str), mgr); + expr_ref cropStr(mk_string(part2Str), mgr); if (can_two_nodes_eq(x, x_concat) && can_two_nodes_eq(y, cropStr)) { // break down option 2-2 expr_ref_vector and_item(mgr); @@ -3630,8 +3661,8 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { std::string prefixStr = strValue.substr(0, prefixLen.get_unsigned()); rational str_sub_prefix = str_len - prefixLen; std::string suffixStr = strValue.substr(prefixLen.get_unsigned(), str_sub_prefix.get_unsigned()); - expr_ref prefixAst(m_strutil.mk_string(prefixStr), mgr); - expr_ref suffixAst(m_strutil.mk_string(suffixStr), mgr); + expr_ref prefixAst(mk_string(prefixStr), mgr); + expr_ref suffixAst(mk_string(suffixStr), mgr); expr_ref ax_l(mgr.mk_and(litems.size(), litems.c_ptr()), mgr); expr_ref suf_n_concat(mk_concat(suffixAst, n), mgr); @@ -3726,8 +3757,8 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { for (int i = 0; i <= (int) strValue.size(); i++) { std::string part1Str = strValue.substr(0, i); std::string part2Str = strValue.substr(i, strValue.size() - i); - expr_ref cropStr(m_strutil.mk_string(part1Str), mgr); - expr_ref suffixStr(m_strutil.mk_string(part2Str), mgr); + expr_ref cropStr(mk_string(part1Str), mgr); + expr_ref suffixStr(mk_string(part2Str), mgr); expr_ref y_concat(mk_concat(suffixStr, n), mgr); if (can_two_nodes_eq(x, cropStr) && can_two_nodes_eq(y, y_concat)) { @@ -3857,7 +3888,7 @@ void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { } else { if (str1Len > str2Len) { std::string deltaStr = str1Value.substr(str2Len, str1Len - str2Len); - expr_ref tmpAst(mk_concat(m_strutil.mk_string(deltaStr), y), mgr); + expr_ref tmpAst(mk_concat(mk_string(deltaStr), y), mgr); if (!in_same_eqc(tmpAst, n)) { // break down option 4-1 expr_ref implyR(ctx.mk_eq_atom(n, tmpAst), mgr); @@ -3882,7 +3913,7 @@ void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { } } else { std::string deltaStr = str2Value.substr(str1Len, str2Len - str1Len); - expr_ref tmpAst(mk_concat(m_strutil.mk_string(deltaStr), n), mgr); + expr_ref tmpAst(mk_concat(mk_string(deltaStr), n), mgr); if (!in_same_eqc(y, tmpAst)) { //break down option 4-3 expr_ref implyR(ctx.mk_eq_atom(y, tmpAst), mgr); @@ -3960,7 +3991,7 @@ void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { } else { if (str1Len > str2Len) { std::string deltaStr = str1Value.substr(0, str1Len - str2Len); - expr_ref x_deltaStr(mk_concat(x, m_strutil.mk_string(deltaStr)), mgr); + expr_ref x_deltaStr(mk_concat(x, mk_string(deltaStr)), mgr); if (!in_same_eqc(m, x_deltaStr)) { expr_ref implyR(ctx.mk_eq_atom(m, x_deltaStr), mgr); if (m_params.m_AssertStrongerArrangements) { @@ -3983,7 +4014,7 @@ void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { } } else { std::string deltaStr = str2Value.substr(0, str2Len - str1Len); - expr_ref m_deltaStr(mk_concat(m, m_strutil.mk_string(deltaStr)), mgr); + expr_ref m_deltaStr(mk_concat(m, mk_string(deltaStr)), mgr); if (!in_same_eqc(x, m_deltaStr)) { expr_ref implyR(ctx.mk_eq_atom(x, m_deltaStr), mgr); if (m_params.m_AssertStrongerArrangements) { @@ -4178,7 +4209,7 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { expr_ref_vector and_item(mgr); - expr_ref prefixAst(m_strutil.mk_string(prefix), mgr); + expr_ref prefixAst(mk_string(prefix), mgr); expr_ref x_eq_prefix(ctx.mk_eq_atom(m, prefixAst), mgr); and_item.push_back(x_eq_prefix); pos += 1; @@ -4189,7 +4220,7 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { // adding length constraint for _ = constStr seems slowing things down. - expr_ref suffixAst(m_strutil.mk_string(suffix), mgr); + expr_ref suffixAst(mk_string(suffix), mgr); expr_ref y_eq_suffix(ctx.mk_eq_atom(y, suffixAst), mgr); and_item.push_back(y_eq_suffix); pos += 1; @@ -4262,7 +4293,7 @@ void theory_str::process_concat_eq_unroll(expr * concat, expr * unroll) { expr_ref t2(mk_unroll_bound_var(), mgr); expr_ref t3(mk_unroll_bound_var(), mgr); - expr_ref emptyStr(m_strutil.mk_string(""), mgr); + expr_ref emptyStr(mk_string(""), mgr); expr_ref unroll1(mk_unroll(r1, t2), mgr); expr_ref unroll2(mk_unroll(r1, t3), mgr); @@ -6093,7 +6124,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { assert_axiom(diseq); return; } else { - expr_ref tmpStrConst(m_strutil.mk_string(firstPart), m); + expr_ref tmpStrConst(mk_string(firstPart), m); expr_ref premise(ctx.mk_eq_atom(newConcat, str), m); expr_ref conclusion(ctx.mk_eq_atom(arg1, tmpStrConst), m); assert_implication(premise, conclusion); @@ -6133,7 +6164,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { assert_axiom(diseq); return; } else { - expr_ref tmpStrConst(m_strutil.mk_string(secondPart), m); + expr_ref tmpStrConst(mk_string(secondPart), m); expr_ref premise(ctx.mk_eq_atom(newConcat, str), m); expr_ref conclusion(ctx.mk_eq_atom(arg2, tmpStrConst), m); assert_implication(premise, conclusion); @@ -6200,8 +6231,8 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { return; } expr_ref_vector r_items(m); - r_items.push_back(ctx.mk_eq_atom(arg1, m_strutil.mk_string(prefixStr))); - r_items.push_back(ctx.mk_eq_atom(arg2, m_strutil.mk_string(suffixStr))); + r_items.push_back(ctx.mk_eq_atom(arg1, mk_string(prefixStr))); + r_items.push_back(ctx.mk_eq_atom(arg2, mk_string(suffixStr))); if (!arg1Len_exists) { r_items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(prefixStr.size()))); } @@ -6292,12 +6323,12 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { continue; } - expr_ref prefixAst(m_strutil.mk_string(prefixStr), m); + expr_ref prefixAst(mk_string(prefixStr), m); expr_ref arg1_eq (ctx.mk_eq_atom(arg1, prefixAst), m); and_items.push_back(arg1_eq); and_count += 1; - expr_ref suffixAst(m_strutil.mk_string(suffixStr), m); + expr_ref suffixAst(mk_string(suffixStr), m); expr_ref arg2_eq (ctx.mk_eq_atom(arg2, suffixAst), m); and_items.push_back(arg2_eq); and_count += 1; @@ -6450,7 +6481,7 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { rational nn1Len, nn2Len; bool nn1Len_exists = get_len_value(lhs, nn1Len); bool nn2Len_exists = get_len_value(rhs, nn2Len); - expr * emptyStr = m_strutil.mk_string(""); + expr * emptyStr = mk_string(""); if (nn1Len_exists && nn1Len.is_zero()) { if (!in_same_eqc(lhs, emptyStr) && rhs != emptyStr) { @@ -7853,7 +7884,7 @@ bool theory_str::finalcheck_str2int(app * a) { if (!Ival.is_minus_one()) { std::string Ival_str = Ival.to_string(); expr_ref premise(ctx.mk_eq_atom(a, m_autil.mk_numeral(Ival, true)), m); - expr_ref conclusion(ctx.mk_eq_atom(S, m_strutil.mk_string(Ival_str)), m); + expr_ref conclusion(ctx.mk_eq_atom(S, mk_string(Ival_str)), m); expr_ref axiom(rewrite_implication(premise, conclusion), m); if (!string_int_axioms.contains(axiom)) { string_int_axioms.insert(axiom); @@ -7907,7 +7938,7 @@ bool theory_str::finalcheck_int2str(app * a) { } } if (conversionOK) { - expr_ref premise(ctx.mk_eq_atom(a, m_strutil.mk_string(Sval)), m); + expr_ref premise(ctx.mk_eq_atom(a, mk_string(Sval)), m); expr_ref conclusion(ctx.mk_eq_atom(N, m_autil.mk_numeral(convertedRepresentation, true)), m); expr_ref axiom(rewrite_implication(premise, conclusion), m); if (!string_int_axioms.contains(axiom)) { @@ -7917,7 +7948,7 @@ bool theory_str::finalcheck_int2str(app * a) { axiomAdd = true; } } else { - expr_ref axiom(m.mk_not(ctx.mk_eq_atom(a, m_strutil.mk_string(Sval))), m); + expr_ref axiom(m.mk_not(ctx.mk_eq_atom(a, mk_string(Sval))), m); // always assert this axiom because this is a conflict clause assert_axiom(axiom); axiomAdd = true; @@ -8036,7 +8067,7 @@ final_check_status theory_str::final_check_eh() { expr_ref lhs1(ctx.mk_eq_atom(concat_lhs, concat_lhs_str), m); expr_ref lhs2(ctx.mk_eq_atom(concat_rhs, concat_rhs_str), m); expr_ref lhs(m.mk_and(lhs1, lhs2), m); - expr_ref rhs(ctx.mk_eq_atom(concat, m_strutil.mk_string(concatString)), m); + expr_ref rhs(ctx.mk_eq_atom(concat, mk_string(concatString)), m); assert_implication(lhs, rhs); backpropagation_occurred = true; } @@ -8130,7 +8161,7 @@ final_check_status theory_str::final_check_eh() { TRACE("t_str", tout << "Assigning decoy values to free internal variables." << std::endl;); for (std::set::iterator it = unused_internal_variables.begin(); it != unused_internal_variables.end(); ++it) { expr * var = *it; - expr_ref assignment(m.mk_eq(var, m_strutil.mk_string("**unused**")), m); + expr_ref assignment(m.mk_eq(var, mk_string("**unused**")), m); assert_axiom(assignment); } return FC_CONTINUE; @@ -8463,9 +8494,9 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * for (long long i = l; i < h; i++) { // TODO can we share the val_indicator constants with the length tester cache? - orList.push_back(m.mk_eq(val_indicator, m_strutil.mk_string(longlong_to_string(i).c_str()) )); + orList.push_back(m.mk_eq(val_indicator, mk_string(longlong_to_string(i).c_str()) )); if (m_params.m_AggressiveValueTesting) { - literal l = mk_eq(val_indicator, m_strutil.mk_string(longlong_to_string(i).c_str()), false); + literal l = mk_eq(val_indicator, mk_string(longlong_to_string(i).c_str()), false); ctx.mark_as_relevant(l); ctx.force_phase(l); } @@ -8474,19 +8505,19 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * expr * strAst; if (m_params.m_UseFastValueTesterCache) { if (!valueTesterCache.find(aStr, strAst)) { - strAst = m_strutil.mk_string(aStr); + strAst = mk_string(aStr); valueTesterCache.insert(aStr, strAst); m_trail.push_back(strAst); } } else { - strAst = m_strutil.mk_string(aStr); + strAst = mk_string(aStr); } andList.push_back(m.mk_eq(orList[orList.size() - 1], m.mk_eq(freeVar, strAst))); } if (!coverAll) { - orList.push_back(m.mk_eq(val_indicator, m_strutil.mk_string("more"))); + orList.push_back(m.mk_eq(val_indicator, mk_string("more"))); if (m_params.m_AggressiveValueTesting) { - literal l = mk_eq(val_indicator, m_strutil.mk_string("more"), false); + literal l = mk_eq(val_indicator, mk_string("more"), false); ctx.mark_as_relevant(l); ctx.force_phase(~l); } @@ -8513,11 +8544,11 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * // Should add ($$_len_x_j = 16) /\ ($$_val_x_16_i = "more") // --------------------------------------- andList.reset(); - andList.push_back(m.mk_eq(len_indicator, m_strutil.mk_string(lenStr.c_str()))); + andList.push_back(m.mk_eq(len_indicator, mk_string(lenStr.c_str()))); for (int i = 0; i < tries; i++) { expr * vTester = fvar_valueTester_map[freeVar][len][i].second; if (vTester != val_indicator) - andList.push_back(m.mk_eq(vTester, m_strutil.mk_string("more"))); + andList.push_back(m.mk_eq(vTester, mk_string("more"))); } expr * assertL = NULL; if (andList.size() == 1) { @@ -8772,7 +8803,7 @@ void theory_str::gen_assign_unroll_reg(std::set & unrolls) { // option 0 expr_ref op0(ctx.mk_eq_atom(cntInUnr, mk_int(0)), mgr); - expr_ref ast1(ctx.mk_eq_atom(unrFunc, m_strutil.mk_string("")), mgr); + expr_ref ast1(ctx.mk_eq_atom(unrFunc, mk_string("")), mgr); expr_ref ast2(ctx.mk_eq_atom(mk_strlen(unrFunc), mk_int(0)), mgr); expr_ref and1(mgr.mk_and(ast1, ast2), mgr); @@ -8856,7 +8887,7 @@ expr * theory_str::gen_assign_unroll_Str2Reg(expr * n, std::set & unrolls return gen_unroll_conditional_options(n, unrolls, lcmStr); } else { expr_ref implyL(mk_and(litems), mgr); - expr_ref implyR(ctx.mk_eq_atom(n, m_strutil.mk_string("")), mgr); + expr_ref implyR(ctx.mk_eq_atom(n, mk_string("")), mgr); // want to return (implyL -> implyR) expr * final_axiom = rewrite_implication(implyL, implyR); return final_axiom; @@ -8869,7 +8900,7 @@ expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & int dist = opt_LCMUnrollStep; expr_ref_vector litems(mgr); - expr_ref moreAst(m_strutil.mk_string("more"), mgr); + expr_ref moreAst(mk_string("more"), mgr); for (std::set::iterator itor = unrolls.begin(); itor != unrolls.end(); itor++) { expr_ref item(ctx.mk_eq_atom(var, *itor), mgr); TRACE("t_str_detail", tout << "considering unroll " << mk_pp(item, mgr) << std::endl;); @@ -8972,10 +9003,10 @@ expr * theory_str::gen_unroll_assign(expr * var, std::string lcmStr, expr * test for (int i = l; i < h; i++) { std::string iStr = int_to_string(i); - expr_ref testerEqAst(ctx.mk_eq_atom(testerVar, m_strutil.mk_string(iStr)), mgr); + expr_ref testerEqAst(ctx.mk_eq_atom(testerVar, mk_string(iStr)), mgr); TRACE("t_str_detail", tout << "testerEqAst = " << mk_pp(testerEqAst, mgr) << std::endl;); if (m_params.m_AggressiveUnrollTesting) { - literal l = mk_eq(testerVar, m_strutil.mk_string(iStr), false); + literal l = mk_eq(testerVar, mk_string(iStr), false); ctx.mark_as_relevant(l); ctx.force_phase(l); } @@ -8983,7 +9014,7 @@ expr * theory_str::gen_unroll_assign(expr * var, std::string lcmStr, expr * test orItems.push_back(testerEqAst); std::string unrollStrInstance = get_unrolled_string(lcmStr, i); - expr_ref x1(ctx.mk_eq_atom(testerEqAst, ctx.mk_eq_atom(var, m_strutil.mk_string(unrollStrInstance))), mgr); + expr_ref x1(ctx.mk_eq_atom(testerEqAst, ctx.mk_eq_atom(var, mk_string(unrollStrInstance))), mgr); TRACE("t_str_detail", tout << "x1 = " << mk_pp(x1, mgr) << std::endl;); andItems.push_back(x1); @@ -8991,10 +9022,10 @@ expr * theory_str::gen_unroll_assign(expr * var, std::string lcmStr, expr * test TRACE("t_str_detail", tout << "x2 = " << mk_pp(x2, mgr) << std::endl;); andItems.push_back(x2); } - expr_ref testerEqMore(ctx.mk_eq_atom(testerVar, m_strutil.mk_string("more")), mgr); + expr_ref testerEqMore(ctx.mk_eq_atom(testerVar, mk_string("more")), mgr); TRACE("t_str_detail", tout << "testerEqMore = " << mk_pp(testerEqMore, mgr) << std::endl;); if (m_params.m_AggressiveUnrollTesting) { - literal l = mk_eq(testerVar, m_strutil.mk_string("more"), false); + literal l = mk_eq(testerVar, mk_string("more"), false); ctx.mark_as_relevant(l); ctx.force_phase(~l); } @@ -9051,14 +9082,14 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr } else { // no match; create and insert std::string i_str = int_to_string(i); - expr_ref new_val(m_strutil.mk_string(i_str), m); + expr_ref new_val(mk_string(i_str), m); lengthTesterCache.insert(ri, new_val); m_trail.push_back(new_val); str_indicator = expr_ref(new_val, m); } } else { std::string i_str = int_to_string(i); - str_indicator = expr_ref(m_strutil.mk_string(i_str), m); + str_indicator = expr_ref(mk_string(i_str), m); } expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); orList.push_back(or_expr); @@ -9074,9 +9105,9 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr } // TODO cache mk_string("more") - orList.push_back(m.mk_eq(indicator, m_strutil.mk_string("more"))); + orList.push_back(m.mk_eq(indicator, mk_string("more"))); if (m_params.m_AggressiveLengthTesting) { - literal l = mk_eq(indicator, m_strutil.mk_string("more"), false); + literal l = mk_eq(indicator, mk_string("more"), false); ctx.mark_as_relevant(l); ctx.force_phase(~l); } @@ -9104,7 +9135,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr int testerCount = tries - 1; if (testerCount > 0) { expr_ref_vector and_items_LHS(m); - expr_ref moreAst(m_strutil.mk_string("more"), m); + expr_ref moreAst(mk_string("more"), m); for (int i = 0; i < testerCount; ++i) { expr * indicator = fvar_lenTester_map[freeVar][i]; if (internal_variable_set.find(indicator) == internal_variable_set.end()) { @@ -9530,7 +9561,7 @@ app * theory_str::mk_value_helper(app * n) { std::string a0_s(a0_str); std::string a1_s(a1_str); std::string result = a0_s + a1_s; - return m_strutil.mk_string(result); + return to_app(mk_string(result)); } } // fallback path @@ -9562,7 +9593,7 @@ model_value_proc * theory_str::mk_value(enode * n, model_generator & mg) { TRACE("t_str", tout << "WARNING: failed to find a concrete value, falling back" << std::endl;); // TODO make absolutely sure the reason we can't find a concrete value is because of an unassigned temporary // e.g. for an expression like (Concat X $$_str0) - return alloc(expr_wrapper_proc, m_strutil.mk_string("**UNUSED**")); + return alloc(expr_wrapper_proc, to_app(mk_string("**UNUSED**"))); } } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index ffeea34e8..e77c955f2 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -263,6 +263,11 @@ namespace smt { // used when opt_FastValueTesterCache is true string_map valueTesterCache; + string_map stringConstantCache; + unsigned long totalCacheAccessCount; + unsigned long cacheHitCount; + unsigned long cacheMissCount; + // cache mapping each string S to Length(S) obj_map length_ast_map; @@ -277,6 +282,9 @@ namespace smt { void assert_implication(expr * premise, expr * conclusion); expr * rewrite_implication(expr * premise, expr * conclusion); + expr * mk_string(std::string str); + expr * mk_string(const char * str); + app * mk_strlen(expr * e); expr * mk_concat(expr * n1, expr * n2); expr * mk_concat_const_str(expr * n1, expr * n2); From 94762d276d7a6cac121e72fae0d39be046701ac9 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 18 Dec 2016 18:47:38 -0500 Subject: [PATCH 292/562] add string constant cache to theory_str and associated param --- src/smt/params/smt_params_helper.pyg | 1 + src/smt/params/theory_str_params.cpp | 1 + src/smt/params/theory_str_params.h | 9 ++++++- src/smt/theory_str.cpp | 40 +++++++++++++++------------- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/smt/params/smt_params_helper.pyg b/src/smt/params/smt_params_helper.pyg index 3f2c6a54a..3bcb867b4 100644 --- a/src/smt/params/smt_params_helper.pyg +++ b/src/smt/params/smt_params_helper.pyg @@ -68,5 +68,6 @@ def_module_params(module_name='smt', ('str.aggressive_unroll_testing', BOOL, True, 'prioritize testing concrete regex unroll counts over generating more options'), ('str.fast_length_tester_cache', BOOL, False, 'cache length tester constants instead of regenerating them'), ('str.fast_value_tester_cache', BOOL, True, 'cache value tester constants instead of regenerating them'), + ('str.string_constant_cache', BOOL, True, 'cache all generated string constants generated from anywhere in theory_str'), ('theory_case_split', BOOL, False, 'Allow the context to use heuristics involving theory case splits, which are a set of literals of which exactly one can be assigned True. If this option is false, the context will generate extra axioms to enforce this instead.') )) diff --git a/src/smt/params/theory_str_params.cpp b/src/smt/params/theory_str_params.cpp index f952c6c87..dae7765cc 100644 --- a/src/smt/params/theory_str_params.cpp +++ b/src/smt/params/theory_str_params.cpp @@ -26,4 +26,5 @@ void theory_str_params::updt_params(params_ref const & _p) { m_AggressiveUnrollTesting = p.str_aggressive_unroll_testing(); m_UseFastLengthTesterCache = p.str_fast_length_tester_cache(); m_UseFastValueTesterCache = p.str_fast_value_tester_cache(); + m_StringConstantCache = p.str_string_constant_cache(); } diff --git a/src/smt/params/theory_str_params.h b/src/smt/params/theory_str_params.h index f4e7ecf33..dc4e1aa89 100644 --- a/src/smt/params/theory_str_params.h +++ b/src/smt/params/theory_str_params.h @@ -62,13 +62,20 @@ struct theory_str_params { */ bool m_UseFastValueTesterCache; + /* + * If StringConstantCache is set to true, + * all string constants in theory_str generated from anywhere will be cached and saved. + */ + bool m_StringConstantCache; + theory_str_params(params_ref const & p = params_ref()): m_AssertStrongerArrangements(true), m_AggressiveLengthTesting(false), m_AggressiveValueTesting(false), m_AggressiveUnrollTesting(true), m_UseFastLengthTesterCache(false), - m_UseFastValueTesterCache(true) + m_UseFastValueTesterCache(true), + m_StringConstantCache(true) { updt_params(p); } diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 19e677acb..3a3d36c36 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -70,25 +70,29 @@ theory_str::~theory_str() { } expr * theory_str::mk_string(std::string str) { - ++totalCacheAccessCount; - expr * val; - if (stringConstantCache.find(str, val)) { - // cache hit - ++cacheHitCount; - TRACE("t_str_cache", tout << "cache hit: \"" << str << "\" (" - << cacheHitCount << " hits, " << cacheMissCount << " misses out of " - << totalCacheAccessCount << " accesses)" << std::endl;); - return val; + if (m_params.m_StringConstantCache) { + ++totalCacheAccessCount; + expr * val; + if (stringConstantCache.find(str, val)) { + // cache hit + ++cacheHitCount; + TRACE("t_str_cache", tout << "cache hit: \"" << str << "\" (" + << cacheHitCount << " hits, " << cacheMissCount << " misses out of " + << totalCacheAccessCount << " accesses)" << std::endl;); + return val; + } else { + // cache miss + ++cacheMissCount; + TRACE("t_str_cache", tout << "cache miss: \"" << str << "\" (" + << cacheHitCount << " hits, " << cacheMissCount << " misses out of " + << totalCacheAccessCount << " accesses)" << std::endl;); + val = m_strutil.mk_string(str); + m_trail.push_back(val); + stringConstantCache.insert(str, val); + return val; + } } else { - // cache miss - ++cacheMissCount; - TRACE("t_str_cache", tout << "cache miss: \"" << str << "\" (" - << cacheHitCount << " hits, " << cacheMissCount << " misses out of " - << totalCacheAccessCount << " accesses)" << std::endl;); - val = m_strutil.mk_string(str); - m_trail.push_back(val); - stringConstantCache.insert(str, val); - return val; + return m_strutil.mk_string(str); } } From a04bc9974b2d2847505a37f3e9e640a082f4ed84 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 20 Dec 2016 11:14:42 -0500 Subject: [PATCH 293/562] theory case split WIP --- src/smt/smt_context.cpp | 26 +++++++++++++++++++++++++- src/smt/smt_context.h | 12 ++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 907ea876b..45beebc15 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -1750,6 +1750,8 @@ namespace smt { if (inconsistent()) return false; unsigned qhead = m_qhead; + if (!propagate_th_case_split()) + return false; if (!bcp()) return false; if (get_cancel_flag()) @@ -2956,10 +2958,32 @@ namespace smt { } } } else { - NOT_IMPLEMENTED_YET(); + int_set new_case_split; // TODO is it okay to allocate this on the stack? + for (unsigned i = 0; i < num_lits; ++i) { + literal l = lits[i]; + // TODO do we need to enforce this invariant? can we make undo information work without it? + SASSERT(!m_all_th_case_split_literals.contains(l.index())); + m_all_th_case_split_literals.insert(l.index()); + // TODO add undo information for this insert + new_case_split.insert(l.index()); + } + m_th_case_split_sets.push_back(new_case_split); + push_trail(push_back_vector >(m_th_case_split_sets)); + for (unsigned i = 0; i < num_lits; ++i) { + literal l = lits[i]; + m_literal2casesplitsets[l.index()].push_back(new_case_split); + push_trail(push_back_vector >(m_literal2casesplitsets[l.index()])); + } } } + bool context::propagate_th_case_split() { + if (m_all_th_case_split_literals.empty()) + return true; + + NOT_IMPLEMENTED_YET(); return true; + } + bool context::reduce_assertions() { if (!m_asserted_formulas.inconsistent()) { SASSERT(at_base_level()); diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index 5c52adc73..cdc52dc67 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -212,6 +212,16 @@ namespace smt { literal2assumption m_literal2assumption; // maps an expression associated with a literal to the original assumption expr_ref_vector m_unsat_core; + // ----------------------------------- + // + // Theory case split + // + // ----------------------------------- + typedef int_hashtable > int_set; + int_set m_all_th_case_split_literals; + vector m_th_case_split_sets; + u_map< vector > m_literal2casesplitsets; // returns the case split literal sets that a literal participates in + // ----------------------------------- // // Accessors @@ -814,6 +824,8 @@ namespace smt { */ void mk_th_case_split(unsigned num_lits, literal * lits); + bool propagate_th_case_split(); + bool_var mk_bool_var(expr * n); enode * mk_enode(app * n, bool suppress_args, bool merge_tf, bool cgc_enabled); From ab0fcc42f9107931a414fff39c1769dfd7be14d1 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 20 Dec 2016 16:21:07 -0500 Subject: [PATCH 294/562] theory case split heuristic --- src/smt/smt_context.cpp | 93 +++++++++++++++++++++++++++++++++++++---- src/smt/smt_context.h | 8 +++- 2 files changed, 91 insertions(+), 10 deletions(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 45beebc15..93461584f 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -63,6 +63,7 @@ namespace smt { m_is_diseq_tmp(0), m_units_to_reassert(m_manager), m_qhead(0), + m_th_case_split_qhead(0), m_simp_qhead(0), m_simp_counter(0), m_bvar_inc(1.0), @@ -325,6 +326,7 @@ namespace smt { bool context::bcp() { SASSERT(!inconsistent()); + m_th_case_split_qhead = m_qhead; while (m_qhead < m_assigned_literals.size()) { if (get_cancel_flag()) { return true; @@ -1750,10 +1752,10 @@ namespace smt { if (inconsistent()) return false; unsigned qhead = m_qhead; - if (!propagate_th_case_split()) - return false; if (!bcp()) return false; + if (!propagate_th_case_split()) + return false; if (get_cancel_flag()) return true; SASSERT(!inconsistent()); @@ -2941,6 +2943,18 @@ namespace smt { assert_expr_core(e, pr); } + class case_split_insert_trail : public trail { + literal l; + public: + case_split_insert_trail(literal l): + l(l) { + + } + virtual void undo(context & ctx) { + ctx.undo_th_case_split(l); + } + }; + void context::mk_th_case_split(unsigned num_lits, literal * lits) { TRACE("theory_case_split", display_literals_verbose(tout << "theory case split: ", num_lits, lits); tout << std::endl;); // If we don't use the theory case split heuristic, @@ -2958,30 +2972,93 @@ namespace smt { } } } else { - int_set new_case_split; // TODO is it okay to allocate this on the stack? + literal_vector new_case_split; // TODO is it okay to allocate this on the stack? for (unsigned i = 0; i < num_lits; ++i) { literal l = lits[i]; // TODO do we need to enforce this invariant? can we make undo information work without it? SASSERT(!m_all_th_case_split_literals.contains(l.index())); m_all_th_case_split_literals.insert(l.index()); - // TODO add undo information for this insert - new_case_split.insert(l.index()); + push_trail(case_split_insert_trail(l)); + new_case_split.push_back(l); } m_th_case_split_sets.push_back(new_case_split); - push_trail(push_back_vector >(m_th_case_split_sets)); + push_trail(push_back_vector >(m_th_case_split_sets)); for (unsigned i = 0; i < num_lits; ++i) { literal l = lits[i]; + if (!m_literal2casesplitsets.contains(l.index())) { + m_literal2casesplitsets.insert(l.index(), vector()); + } m_literal2casesplitsets[l.index()].push_back(new_case_split); - push_trail(push_back_vector >(m_literal2casesplitsets[l.index()])); + push_trail(push_back_vector >(m_literal2casesplitsets[l.index()])); } + TRACE("theory_case_split", tout << "tracking case split literal set { "; + for (unsigned i = 0; i < num_lits; ++i) { + tout << lits[i].index() << " "; + } + tout << "}" << std::endl; + ); } } + void context::undo_th_case_split(literal l) { + m_all_th_case_split_literals.remove(l.index()); + } + bool context::propagate_th_case_split() { if (m_all_th_case_split_literals.empty()) return true; - NOT_IMPLEMENTED_YET(); return true; + // iterate over all literals assigned since the last time this method was called, + // not counting any literals that get assigned by this method + // this relies on bcp() to give us its old m_qhead and therefore + // bcp() should always be called before this method + unsigned assigned_literal_idx = m_th_case_split_qhead; + unsigned assigned_literal_end = m_assigned_literals.size(); + while(assigned_literal_idx < assigned_literal_end) { + literal l = m_assigned_literals[assigned_literal_idx]; + TRACE("theory_case_split", tout << "check literal " << l.index() << std::endl; display_literal_verbose(tout, l); tout << std::endl;); + ++assigned_literal_idx; + // check if this literal participates in any theory case split + if (m_all_th_case_split_literals.contains(l.index())) { + TRACE("theory_case_split", tout << "assigned literal " << l.index() << " is a theory case split literal" << std::endl;); + // now find the sets of literals which contain l + vector case_split_sets = m_literal2casesplitsets.get(l.index(), vector()); + for (vector::const_iterator it = case_split_sets.begin(); it != case_split_sets.end(); ++it) { + literal_vector case_split_set = *it; + TRACE("theory_case_split", tout << "found case split set { "; + for(literal_vector::iterator set_it = case_split_set.begin(); set_it != case_split_set.end(); ++set_it) { + tout << set_it->index() << " "; + } + tout << "}" << std::endl;); + for(literal_vector::iterator set_it = case_split_set.begin(); set_it != case_split_set.end(); ++set_it) { + literal l2 = *set_it; + if (l2 != l) { + b_justification js(l); + switch (get_assignment(l2)) { + case l_false: + TRACE("theory_case_split", tout << "case split literal " << l2.index() << " is already assigned False" << std::endl;); + break; + // TODO these next two cases can be combined. I'm doing this for debugging purposes + case l_undef: + TRACE("theory_case_split", tout << "case split literal " << l2.index() << " is not assigned" << std::endl;); + assign(~l2, js); + break; + case l_true: + TRACE("theory_case_split", tout << "case split literal " << l2.index() << " is already assigned True" << std::endl;); + assign(~l2, js); + break; + } + if (inconsistent()) { + TRACE("theory_case_split", tout << "conflict detected!" << std::endl;); + return false; + } + } + } + } + } + } + // if we get here without detecting a conflict, we're fine + return true; } bool context::reduce_assertions() { diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index cdc52dc67..8016eb587 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -219,8 +219,9 @@ namespace smt { // ----------------------------------- typedef int_hashtable > int_set; int_set m_all_th_case_split_literals; - vector m_th_case_split_sets; - u_map< vector > m_literal2casesplitsets; // returns the case split literal sets that a literal participates in + vector m_th_case_split_sets; + u_map< vector > m_literal2casesplitsets; // returns the case split literal sets that a literal participates in + unsigned m_th_case_split_qhead; // ----------------------------------- // @@ -824,6 +825,9 @@ namespace smt { */ void mk_th_case_split(unsigned num_lits, literal * lits); + // helper function for trail + void undo_th_case_split(literal l); + bool propagate_th_case_split(); bool_var mk_bool_var(expr * n); From df63b62763ef06c27fef3bc9c3d5d5fac17437ff Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 20 Dec 2016 17:32:51 -0500 Subject: [PATCH 295/562] fix vector manip bug in theory case split --- src/smt/smt_context.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 93461584f..6c0a89d4f 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -2989,7 +2989,6 @@ namespace smt { m_literal2casesplitsets.insert(l.index(), vector()); } m_literal2casesplitsets[l.index()].push_back(new_case_split); - push_trail(push_back_vector >(m_literal2casesplitsets[l.index()])); } TRACE("theory_case_split", tout << "tracking case split literal set { "; for (unsigned i = 0; i < num_lits; ++i) { @@ -3002,6 +3001,11 @@ namespace smt { void context::undo_th_case_split(literal l) { m_all_th_case_split_literals.remove(l.index()); + if (m_literal2casesplitsets.contains(l.index())) { + if (!m_literal2casesplitsets[l.index()].empty()) { + m_literal2casesplitsets[l.index()].pop_back(); + } + } } bool context::propagate_th_case_split() { From 2dc9b486d3d4962850e2899836b64cf2a07266f9 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 22 Dec 2016 19:17:42 -0500 Subject: [PATCH 296/562] theory_str binary search heuristic WIP --- src/smt/params/smt_params_helper.pyg | 2 + src/smt/params/theory_str_params.cpp | 2 + src/smt/params/theory_str_params.h | 7 +- src/smt/theory_str.cpp | 546 +++++++++++++++++++-------- src/smt/theory_str.h | 53 +++ 5 files changed, 442 insertions(+), 168 deletions(-) diff --git a/src/smt/params/smt_params_helper.pyg b/src/smt/params/smt_params_helper.pyg index 3bcb867b4..4e3bec57d 100644 --- a/src/smt/params/smt_params_helper.pyg +++ b/src/smt/params/smt_params_helper.pyg @@ -69,5 +69,7 @@ def_module_params(module_name='smt', ('str.fast_length_tester_cache', BOOL, False, 'cache length tester constants instead of regenerating them'), ('str.fast_value_tester_cache', BOOL, True, 'cache value tester constants instead of regenerating them'), ('str.string_constant_cache', BOOL, True, 'cache all generated string constants generated from anywhere in theory_str'), + ('str.use_binary_search', BOOL, False, 'use a binary search heuristic for finding concrete length values for free variables in theory_str (set to False to use linear search)'), + ('str.binary_search_start', UINT, 64, 'initial upper bound for theory_str binary search'), ('theory_case_split', BOOL, False, 'Allow the context to use heuristics involving theory case splits, which are a set of literals of which exactly one can be assigned True. If this option is false, the context will generate extra axioms to enforce this instead.') )) diff --git a/src/smt/params/theory_str_params.cpp b/src/smt/params/theory_str_params.cpp index dae7765cc..2e98a4394 100644 --- a/src/smt/params/theory_str_params.cpp +++ b/src/smt/params/theory_str_params.cpp @@ -27,4 +27,6 @@ void theory_str_params::updt_params(params_ref const & _p) { m_UseFastLengthTesterCache = p.str_fast_length_tester_cache(); m_UseFastValueTesterCache = p.str_fast_value_tester_cache(); m_StringConstantCache = p.str_string_constant_cache(); + m_UseBinarySearch = p.str_use_binary_search(); + m_BinarySearchInitialUpperBound = p.str_binary_search_start(); } diff --git a/src/smt/params/theory_str_params.h b/src/smt/params/theory_str_params.h index dc4e1aa89..39c553780 100644 --- a/src/smt/params/theory_str_params.h +++ b/src/smt/params/theory_str_params.h @@ -68,6 +68,9 @@ struct theory_str_params { */ bool m_StringConstantCache; + bool m_UseBinarySearch; + unsigned m_BinarySearchInitialUpperBound; + theory_str_params(params_ref const & p = params_ref()): m_AssertStrongerArrangements(true), m_AggressiveLengthTesting(false), @@ -75,7 +78,9 @@ struct theory_str_params { m_AggressiveUnrollTesting(true), m_UseFastLengthTesterCache(false), m_UseFastValueTesterCache(true), - m_StringConstantCache(true) + m_StringConstantCache(true), + m_UseBinarySearch(false), + m_BinarySearchInitialUpperBound(64) { updt_params(p); } diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 3a3d36c36..754d258bc 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -6379,27 +6379,53 @@ void theory_str::more_value_tests(expr * valTester, std::string valTesterValue) ast_manager & m = get_manager(); expr * fVar = valueTester_fvar_map[valTester]; - int lenTesterCount = fvar_lenTester_map[fVar].size(); - - expr * effectiveLenInd = NULL; - std::string effectiveLenIndiStr = ""; - for (int i = 0; i < lenTesterCount; ++i) { - expr * len_indicator_pre = fvar_lenTester_map[fVar][i]; - bool indicatorHasEqcValue = false; - expr * len_indicator_value = get_eqc_value(len_indicator_pre, indicatorHasEqcValue); - if (indicatorHasEqcValue) { - std::string len_pIndiStr = m_strutil.get_string_constant_value(len_indicator_value); - if (len_pIndiStr != "more") { - effectiveLenInd = len_indicator_pre; - effectiveLenIndiStr = len_pIndiStr; - break; + if (m_params.m_UseBinarySearch) { + if (!binary_search_len_tester_stack.contains(fVar) || binary_search_len_tester_stack[fVar].empty()) { + TRACE("t_str_binary_search", tout << "WARNING: no active length testers for " << mk_pp(fVar, m) << std::endl;); + // TODO handle this? + NOT_IMPLEMENTED_YET(); + } + expr * effectiveLenInd = binary_search_len_tester_stack[fVar].back(); + bool hasEqcValue; + expr * len_indicator_value = get_eqc_value(effectiveLenInd, hasEqcValue); + if (!hasEqcValue) { + TRACE("t_str_binary_search", tout << "WARNING: length tester " << mk_pp(effectiveLenInd, m) << " at top of stack for " << mk_pp(fVar, m) << " has no EQC value" << std::endl;); + } else { + // safety check + std::string effectiveLenIndiStr = m_strutil.get_string_constant_value(len_indicator_value); + if (effectiveLenIndiStr == "more" || effectiveLenIndiStr == "less") { + TRACE("t_str_binary_search", tout << "ERROR: illegal state -- requesting 'more value tests' but a length tester is not yet concrete!" << std::endl;); + UNREACHABLE(); + } + expr * valueAssert = gen_free_var_options(fVar, effectiveLenInd, effectiveLenIndiStr, valTester, valTesterValue); + TRACE("t_str_detail", tout << "asserting more value tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); + if (valueAssert != NULL) { + assert_axiom(valueAssert); } } - } - expr * valueAssert = gen_free_var_options(fVar, effectiveLenInd, effectiveLenIndiStr, valTester, valTesterValue); - TRACE("t_str_detail", tout << "asserting more value tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); - if (valueAssert != NULL) { - assert_axiom(valueAssert); + } else { + int lenTesterCount = fvar_lenTester_map[fVar].size(); + + expr * effectiveLenInd = NULL; + std::string effectiveLenIndiStr = ""; + for (int i = 0; i < lenTesterCount; ++i) { + expr * len_indicator_pre = fvar_lenTester_map[fVar][i]; + bool indicatorHasEqcValue = false; + expr * len_indicator_value = get_eqc_value(len_indicator_pre, indicatorHasEqcValue); + if (indicatorHasEqcValue) { + std::string len_pIndiStr = m_strutil.get_string_constant_value(len_indicator_value); + if (len_pIndiStr != "more") { + effectiveLenInd = len_indicator_pre; + effectiveLenIndiStr = len_pIndiStr; + break; + } + } + } + expr * valueAssert = gen_free_var_options(fVar, effectiveLenInd, effectiveLenIndiStr, valTester, valTesterValue); + TRACE("t_str_detail", tout << "asserting more value tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); + if (valueAssert != NULL) { + assert_axiom(valueAssert); + } } } @@ -9163,6 +9189,186 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr } } +// Return an expression of the form +// (tester = "less" | tester = "N" | tester = "more") & +// (tester = "less" iff len(freeVar) < N) & (tester = "more" iff len(freeVar) > N) & (tester = "N" iff len(freeVar) = N)) +expr_ref theory_str::binary_search_case_split(expr * freeVar, expr * tester, binary_search_info & bounds) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + rational N = bounds.midPoint; + rational N_minus_one = N - rational::one(); + rational N_plus_one = N + rational::one(); + expr_ref lenFreeVar(mk_strlen(freeVar), m); + + TRACE("t_str_binary_search", tout << "create case split for free var " << mk_pp(freeVar, m) + << " over " << mk_pp(tester, m) << " with midpoint " << N << std::endl;); + + expr_ref_vector combinedCaseSplit(m); + expr_ref_vector testerCases(m); + + expr_ref caseLess(ctx.mk_eq_atom(tester, mk_string("less")), m); + testerCases.push_back(caseLess); + combinedCaseSplit.push_back(ctx.mk_eq_atom(caseLess, m_autil.mk_le(lenFreeVar, m_autil.mk_numeral(N_minus_one, true) ))); + + expr_ref caseMore(ctx.mk_eq_atom(tester, mk_string("more")), m); + testerCases.push_back(caseMore); + combinedCaseSplit.push_back(ctx.mk_eq_atom(caseMore, m_autil.mk_ge(lenFreeVar, m_autil.mk_numeral(N_plus_one, true) ))); + + expr_ref caseEq(ctx.mk_eq_atom(tester, mk_string(N.to_string())), m); + testerCases.push_back(caseEq); + combinedCaseSplit.push_back(ctx.mk_eq_atom(caseEq, ctx.mk_eq_atom(lenFreeVar, m_autil.mk_numeral(N, true)))); + + combinedCaseSplit.push_back(mk_or(testerCases)); + + expr_ref final_term(mk_and(combinedCaseSplit), m); + SASSERT(final_term); + TRACE("t_str_binary_search", tout << "final term: " << mk_pp(final_term, m) << std::endl;); + return final_term; +} + +expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenTester, std::string previousLenTesterValue) { + ast_manager & m = get_manager(); + + if (binary_search_len_tester_stack.contains(freeVar)) { + TRACE("t_str_binary_search", tout << "checking existing length testers for " << mk_pp(freeVar, m) << std::endl; + for (ptr_vector::const_iterator it = binary_search_len_tester_stack[freeVar].begin(); + it != binary_search_len_tester_stack[freeVar].end(); ++it) { + expr * tester = *it; + tout << mk_pp(tester, m) << ": "; + if (binary_search_len_tester_info.contains(tester)) { + binary_search_info & bounds = binary_search_len_tester_info[tester]; + tout << "[" << bounds.lowerBound << " | " << bounds.midPoint << " | " << bounds.upperBound << "]!" << bounds.windowSize; + } else { + tout << "[WARNING: no bounds info available]"; + } + bool hasEqcValue; + expr * testerEqcValue = get_eqc_value(tester, hasEqcValue); + if (hasEqcValue) { + tout << " = " << mk_pp(testerEqcValue, m); + } else { + tout << " [no eqc value]"; + } + tout << std::endl; + } + ); + expr * lastTester = binary_search_len_tester_stack[freeVar].back(); + bool lastTesterHasEqcValue; + expr * lastTesterValue = get_eqc_value(lastTester, lastTesterHasEqcValue); + std::string lastTesterConstant; + if (!lastTesterHasEqcValue) { + TRACE("t_str_binary_search", tout << "length tester " << mk_pp(lastTester, m) << " at top of stack doesn't have an EQC value yet" << std::endl;); + // check previousLenTester + if (previousLenTester == lastTester) { + lastTesterConstant = previousLenTesterValue; + TRACE("t_str_binary_search", tout << "invoked with previousLenTester info matching top of stack" << std::endl;); + } else { + // this is a bit unexpected + TRACE("t_str_binary_search", tout << "WARNING: unexpected reordering of length testers!" << std::endl;); + // TODO resolve this case + NOT_IMPLEMENTED_YET(); return NULL; + } + } else { + lastTesterConstant = m_strutil.get_string_constant_value(lastTesterValue); + } + TRACE("t_str_binary_search", tout << "last length tester is assigned \"" << lastTesterConstant << "\"" << std::endl;); + if (lastTesterConstant == "more" || lastTesterConstant == "less") { + // use the previous bounds info to generate a new midpoint + binary_search_info lastBounds; + if (!binary_search_len_tester_info.find(lastTester, lastBounds)) { + // unexpected + TRACE("t_str_binary_search", tout << "WARNING: no bounds information available for last tester!" << std::endl;); + // TODO resolve this + NOT_IMPLEMENTED_YET(); + } + TRACE("t_str_binary_search", tout << "last bounds are [" << lastBounds.lowerBound << " | " << lastBounds.midPoint << " | " << lastBounds.upperBound << "]!" << lastBounds.windowSize << std::endl;); + binary_search_info newBounds; + expr * newTester; + if (lastTesterConstant == "more") { + // special case: if the midpoint, upper bound, and window size are all equal, + // we double the window size and adjust the bounds + if (lastBounds.midPoint == lastBounds.upperBound && lastBounds.upperBound == lastBounds.windowSize) { + TRACE("t_str_binary_search", tout << "search hit window size; expanding" << std::endl;); + // TODO is this correct? + newBounds.lowerBound = lastBounds.windowSize + rational::one(); + newBounds.windowSize = lastBounds.windowSize * rational(2); + newBounds.upperBound = newBounds.windowSize; + newBounds.calculate_midpoint(); + } else if (false) { + // TODO handle the case where the midpoint can't be increased further + // (e.g. a window like [50 | 50 | 50]!64 and we don't answer "50") + } else { + // general case + newBounds.lowerBound = lastBounds.midPoint + rational::one(); + newBounds.windowSize = lastBounds.windowSize; + newBounds.upperBound = lastBounds.upperBound; + newBounds.calculate_midpoint(); + } + if (!binary_search_next_var_high.find(lastTester, newTester)) { + newTester = mk_internal_lenTest_var(freeVar, newBounds.midPoint.get_int32()); + binary_search_next_var_high.insert(lastTester, newTester); + } + refresh_theory_var(newTester); + } else if (lastTesterConstant == "less") { + if (false) { + // TODO handle the case where the midpoint can't be decreased further + // (e.g. a window like [0 | 0 | 0]!64 and we don't answer "0" + } else { + // general case + newBounds.upperBound = lastBounds.midPoint - rational::one(); + newBounds.windowSize = lastBounds.windowSize; + newBounds.lowerBound = lastBounds.lowerBound; + newBounds.calculate_midpoint(); + } + if (!binary_search_next_var_low.find(lastTester, newTester)) { + newTester = mk_internal_lenTest_var(freeVar, newBounds.midPoint.get_int32()); + binary_search_next_var_low.insert(lastTester, newTester); + } + refresh_theory_var(newTester); + } + TRACE("t_str_binary_search", tout << "new bounds are [" << newBounds.lowerBound << " | " << newBounds.midPoint << " | " << newBounds.upperBound << "]!" << newBounds.windowSize << std::endl;); + binary_search_len_tester_stack[freeVar].push_back(newTester); + m_trail_stack.push(binary_search_trail(binary_search_len_tester_stack, freeVar)); + binary_search_len_tester_info.insert(newTester, newBounds); + m_trail_stack.push(insert_obj_map(binary_search_len_tester_info, newTester)); + + expr_ref next_case_split(binary_search_case_split(freeVar, newTester, newBounds)); + m_trail.push_back(next_case_split); + // TODO assert a precondition about all previous length testers that got us here + return next_case_split; + } else { // lastTesterConstant is a concrete value + TRACE("t_str", tout << "length is fixed; generating models for free var" << std::endl;); + // length is fixed + expr * valueAssert = gen_free_var_options(freeVar, lastTester, lastTesterConstant, NULL, ""); + return valueAssert; + } + } else { + // no length testers yet + TRACE("t_str_binary_search", tout << "no length testers for " << mk_pp(freeVar, m) << std::endl;); + binary_search_len_tester_stack.insert(freeVar, ptr_vector()); + + expr * firstTester; + rational lowerBound(0); + rational upperBound(m_params.m_BinarySearchInitialUpperBound); + rational windowSize(upperBound); + rational midPoint(floor(upperBound / rational(2))); + if (!binary_search_starting_len_tester.find(freeVar, firstTester)) { + firstTester = mk_internal_lenTest_var(freeVar, midPoint.get_int32()); + binary_search_starting_len_tester.insert(freeVar, firstTester); + } + refresh_theory_var(firstTester); + + binary_search_len_tester_stack[freeVar].push_back(firstTester); + m_trail_stack.push(binary_search_trail(binary_search_len_tester_stack, freeVar)); + binary_search_info new_info(lowerBound, midPoint, upperBound, windowSize); + binary_search_len_tester_info.insert(firstTester, new_info); + m_trail_stack.push(insert_obj_map(binary_search_len_tester_info, firstTester)); + + expr_ref initial_case_split(binary_search_case_split(freeVar, firstTester, new_info)); + m_trail.push_back(initial_case_split); + return initial_case_split; + } +} + // ----------------------------------------------------------------------------------------------------- // True branch will be taken in final_check: // - When we discover a variable is "free" for the first time @@ -9180,161 +9386,167 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe TRACE("t_str_detail", tout << "gen for free var " << mk_ismt2_pp(freeVar, m) << std::endl;); - bool map_effectively_empty = false; - if (fvar_len_count_map.find(freeVar) == fvar_len_count_map.end()) { - TRACE("t_str_detail", tout << "fvar_len_count_map is empty" << std::endl;); - map_effectively_empty = true; - } - - if (!map_effectively_empty) { - // check whether any entries correspond to variables that went out of scope; - // if every entry is out of scope then the map counts as being empty - // TODO: maybe remove them from the map instead? either here or in pop_scope_eh() - - // assume empty and find a counterexample - map_effectively_empty = true; - ptr_vector indicator_set = fvar_lenTester_map[freeVar]; - for (ptr_vector::iterator it = indicator_set.begin(); it != indicator_set.end(); ++it) { - expr * indicator = *it; - if (internal_variable_set.find(indicator) != internal_variable_set.end()) { - TRACE("t_str_detail", tout <<"found active internal variable " << mk_ismt2_pp(indicator, m) - << " in fvar_lenTester_map[freeVar]" << std::endl;); - map_effectively_empty = false; - break; - } - } - CTRACE("t_str_detail", map_effectively_empty, tout << "all variables in fvar_lenTester_map[freeVar] out of scope" << std::endl;); - } - - if (map_effectively_empty) { - // no length assertions for this free variable have ever been added. - TRACE("t_str_detail", tout << "no length assertions yet" << std::endl;); - - fvar_len_count_map[freeVar] = 1; - unsigned int testNum = fvar_len_count_map[freeVar]; - - expr_ref indicator(mk_internal_lenTest_var(freeVar, testNum), m); - SASSERT(indicator); - - // since the map is "effectively empty", we can remove those variables that have left scope... - fvar_lenTester_map[freeVar].shrink(0); - fvar_lenTester_map[freeVar].push_back(indicator); - lenTester_fvar_map[indicator] = freeVar; - - expr * lenTestAssert = gen_len_test_options(freeVar, indicator, testNum); - SASSERT(lenTestAssert != NULL); - return lenTestAssert; + if (m_params.m_UseBinarySearch) { + TRACE("t_str_detail", tout << "using binary search heuristic" << std::endl;); + return binary_search_length_test(freeVar, lenTesterInCbEq, lenTesterValue); } else { - TRACE("t_str_detail", tout << "found previous in-scope length assertions" << std::endl;); + bool map_effectively_empty = false; + if (fvar_len_count_map.find(freeVar) == fvar_len_count_map.end()) { + TRACE("t_str_detail", tout << "fvar_len_count_map is empty" << std::endl;); + map_effectively_empty = true; + } - expr * effectiveLenInd = NULL; - std::string effectiveLenIndiStr = ""; - int lenTesterCount = (int) fvar_lenTester_map[freeVar].size(); + if (!map_effectively_empty) { + // check whether any entries correspond to variables that went out of scope; + // if every entry is out of scope then the map counts as being empty + // TODO: maybe remove them from the map instead? either here or in pop_scope_eh() - TRACE("t_str_detail", - tout << lenTesterCount << " length testers in fvar_lenTester_map[" << mk_pp(freeVar, m) << "]:" << std::endl; - for (int i = 0; i < lenTesterCount; ++i) { - expr * len_indicator = fvar_lenTester_map[freeVar][i]; - tout << mk_pp(len_indicator, m) << ": "; - bool effectiveInScope = (internal_variable_set.find(len_indicator) != internal_variable_set.end()); - tout << (effectiveInScope ? "in scope" : "NOT in scope"); - tout << std::endl; - } - ); + // assume empty and find a counterexample + map_effectively_empty = true; + ptr_vector indicator_set = fvar_lenTester_map[freeVar]; + for (ptr_vector::iterator it = indicator_set.begin(); it != indicator_set.end(); ++it) { + expr * indicator = *it; + if (internal_variable_set.find(indicator) != internal_variable_set.end()) { + TRACE("t_str_detail", tout <<"found active internal variable " << mk_ismt2_pp(indicator, m) + << " in fvar_lenTester_map[freeVar]" << std::endl;); + map_effectively_empty = false; + break; + } + } + CTRACE("t_str_detail", map_effectively_empty, tout << "all variables in fvar_lenTester_map[freeVar] out of scope" << std::endl;); + } - int i = 0; - for (; i < lenTesterCount; ++i) { - expr * len_indicator_pre = fvar_lenTester_map[freeVar][i]; - // check whether this is in scope as well - if (internal_variable_set.find(len_indicator_pre) == internal_variable_set.end()) { - TRACE("t_str_detail", tout << "length indicator " << mk_pp(len_indicator_pre, m) << " not in scope" << std::endl;); - continue; - } + if (map_effectively_empty) { + // no length assertions for this free variable have ever been added. + TRACE("t_str_detail", tout << "no length assertions yet" << std::endl;); - bool indicatorHasEqcValue = false; - expr * len_indicator_value = get_eqc_value(len_indicator_pre, indicatorHasEqcValue); - TRACE("t_str_detail", tout << "length indicator " << mk_ismt2_pp(len_indicator_pre, m) << - " = " << mk_ismt2_pp(len_indicator_value, m) << std::endl;); - if (indicatorHasEqcValue) { - const char * val = 0; - m_strutil.is_string(len_indicator_value, & val); - std::string len_pIndiStr(val); - if (len_pIndiStr != "more") { - effectiveLenInd = len_indicator_pre; - effectiveLenIndiStr = len_pIndiStr; - break; - } - } else { - if (lenTesterInCbEq != len_indicator_pre) { - TRACE("t_str", tout << "WARNING: length indicator " << mk_ismt2_pp(len_indicator_pre, m) - << " does not have an equivalence class value." - << " i = " << i << ", lenTesterCount = " << lenTesterCount << std::endl;); - if (i > 0) { - effectiveLenInd = fvar_lenTester_map[freeVar][i - 1]; - bool effectiveHasEqcValue; - expr * effective_eqc_value = get_eqc_value(effectiveLenInd, effectiveHasEqcValue); - bool effectiveInScope = (internal_variable_set.find(effectiveLenInd) != internal_variable_set.end()); - TRACE("t_str_detail", tout << "checking effective length indicator " << mk_pp(effectiveLenInd, m) << ": " - << (effectiveInScope ? "in scope" : "NOT in scope") << ", "; - if (effectiveHasEqcValue) { - tout << "~= " << mk_pp(effective_eqc_value, m); - } else { - tout << "no eqc string constant"; - } - tout << std::endl;); - if (effectiveLenInd == lenTesterInCbEq) { - effectiveLenIndiStr = lenTesterValue; - } else { - if (effectiveHasEqcValue) { - effectiveLenIndiStr = m_strutil.get_string_constant_value(effective_eqc_value); - } else { - // TODO this should be unreachable, but can we really do anything here? - NOT_IMPLEMENTED_YET(); - } - } - } - break; - } - // lenTesterInCbEq == len_indicator_pre - else { - if (lenTesterValue != "more") { - effectiveLenInd = len_indicator_pre; - effectiveLenIndiStr = lenTesterValue; - break; - } - } - } // !indicatorHasEqcValue - } // for (i : [0..lenTesterCount-1]) - if (effectiveLenIndiStr == "more" || effectiveLenIndiStr == "") { - TRACE("t_str", tout << "length is not fixed; generating length tester options for free var" << std::endl;); - expr_ref indicator(m); - unsigned int testNum = 0; + fvar_len_count_map[freeVar] = 1; + unsigned int testNum = fvar_len_count_map[freeVar]; - TRACE("t_str", tout << "effectiveLenIndiStr = " << effectiveLenIndiStr - << ", i = " << i << ", lenTesterCount = " << lenTesterCount << std::endl;); + expr_ref indicator(mk_internal_lenTest_var(freeVar, testNum), m); + SASSERT(indicator); - if (i == lenTesterCount) { - fvar_len_count_map[freeVar] = fvar_len_count_map[freeVar] + 1; - testNum = fvar_len_count_map[freeVar]; - indicator = mk_internal_lenTest_var(freeVar, testNum); - fvar_lenTester_map[freeVar].push_back(indicator); - lenTester_fvar_map[indicator] = freeVar; - } else { - // TODO make absolutely sure this is safe to do if 'indicator' is technically out of scope - indicator = fvar_lenTester_map[freeVar][i]; - refresh_theory_var(indicator); - testNum = i + 1; - } - expr * lenTestAssert = gen_len_test_options(freeVar, indicator, testNum); - SASSERT(lenTestAssert != NULL); - return lenTestAssert; - } else { - TRACE("t_str", tout << "length is fixed; generating models for free var" << std::endl;); - // length is fixed - expr * valueAssert = gen_free_var_options(freeVar, effectiveLenInd, effectiveLenIndiStr, NULL, ""); - return valueAssert; - } - } // fVarLenCountMap.find(...) + // since the map is "effectively empty", we can remove those variables that have left scope... + fvar_lenTester_map[freeVar].shrink(0); + fvar_lenTester_map[freeVar].push_back(indicator); + lenTester_fvar_map[indicator] = freeVar; + + expr * lenTestAssert = gen_len_test_options(freeVar, indicator, testNum); + SASSERT(lenTestAssert != NULL); + return lenTestAssert; + } else { + TRACE("t_str_detail", tout << "found previous in-scope length assertions" << std::endl;); + + expr * effectiveLenInd = NULL; + std::string effectiveLenIndiStr = ""; + int lenTesterCount = (int) fvar_lenTester_map[freeVar].size(); + + TRACE("t_str_detail", + tout << lenTesterCount << " length testers in fvar_lenTester_map[" << mk_pp(freeVar, m) << "]:" << std::endl; + for (int i = 0; i < lenTesterCount; ++i) { + expr * len_indicator = fvar_lenTester_map[freeVar][i]; + tout << mk_pp(len_indicator, m) << ": "; + bool effectiveInScope = (internal_variable_set.find(len_indicator) != internal_variable_set.end()); + tout << (effectiveInScope ? "in scope" : "NOT in scope"); + tout << std::endl; + } + ); + + int i = 0; + for (; i < lenTesterCount; ++i) { + expr * len_indicator_pre = fvar_lenTester_map[freeVar][i]; + // check whether this is in scope as well + if (internal_variable_set.find(len_indicator_pre) == internal_variable_set.end()) { + TRACE("t_str_detail", tout << "length indicator " << mk_pp(len_indicator_pre, m) << " not in scope" << std::endl;); + continue; + } + + bool indicatorHasEqcValue = false; + expr * len_indicator_value = get_eqc_value(len_indicator_pre, indicatorHasEqcValue); + TRACE("t_str_detail", tout << "length indicator " << mk_ismt2_pp(len_indicator_pre, m) << + " = " << mk_ismt2_pp(len_indicator_value, m) << std::endl;); + if (indicatorHasEqcValue) { + const char * val = 0; + m_strutil.is_string(len_indicator_value, & val); + std::string len_pIndiStr(val); + if (len_pIndiStr != "more") { + effectiveLenInd = len_indicator_pre; + effectiveLenIndiStr = len_pIndiStr; + break; + } + } else { + if (lenTesterInCbEq != len_indicator_pre) { + TRACE("t_str", tout << "WARNING: length indicator " << mk_ismt2_pp(len_indicator_pre, m) + << " does not have an equivalence class value." + << " i = " << i << ", lenTesterCount = " << lenTesterCount << std::endl;); + if (i > 0) { + effectiveLenInd = fvar_lenTester_map[freeVar][i - 1]; + bool effectiveHasEqcValue; + expr * effective_eqc_value = get_eqc_value(effectiveLenInd, effectiveHasEqcValue); + bool effectiveInScope = (internal_variable_set.find(effectiveLenInd) != internal_variable_set.end()); + TRACE("t_str_detail", tout << "checking effective length indicator " << mk_pp(effectiveLenInd, m) << ": " + << (effectiveInScope ? "in scope" : "NOT in scope") << ", "; + if (effectiveHasEqcValue) { + tout << "~= " << mk_pp(effective_eqc_value, m); + } else { + tout << "no eqc string constant"; + } + tout << std::endl;); + if (effectiveLenInd == lenTesterInCbEq) { + effectiveLenIndiStr = lenTesterValue; + } else { + if (effectiveHasEqcValue) { + effectiveLenIndiStr = m_strutil.get_string_constant_value(effective_eqc_value); + } else { + // TODO this should be unreachable, but can we really do anything here? + NOT_IMPLEMENTED_YET(); + } + } + } + break; + } + // lenTesterInCbEq == len_indicator_pre + else { + if (lenTesterValue != "more") { + effectiveLenInd = len_indicator_pre; + effectiveLenIndiStr = lenTesterValue; + break; + } + } + } // !indicatorHasEqcValue + } // for (i : [0..lenTesterCount-1]) + if (effectiveLenIndiStr == "more" || effectiveLenIndiStr == "") { + TRACE("t_str", tout << "length is not fixed; generating length tester options for free var" << std::endl;); + expr_ref indicator(m); + unsigned int testNum = 0; + + TRACE("t_str", tout << "effectiveLenIndiStr = " << effectiveLenIndiStr + << ", i = " << i << ", lenTesterCount = " << lenTesterCount << std::endl;); + + if (i == lenTesterCount) { + fvar_len_count_map[freeVar] = fvar_len_count_map[freeVar] + 1; + testNum = fvar_len_count_map[freeVar]; + indicator = mk_internal_lenTest_var(freeVar, testNum); + fvar_lenTester_map[freeVar].push_back(indicator); + lenTester_fvar_map[indicator] = freeVar; + } else { + // TODO make absolutely sure this is safe to do if 'indicator' is technically out of scope + indicator = fvar_lenTester_map[freeVar][i]; + refresh_theory_var(indicator); + testNum = i + 1; + } + expr * lenTestAssert = gen_len_test_options(freeVar, indicator, testNum); + SASSERT(lenTestAssert != NULL); + return lenTestAssert; + } else { + TRACE("t_str", tout << "length is fixed; generating models for free var" << std::endl;); + // length is fixed + expr * valueAssert = gen_free_var_options(freeVar, effectiveLenInd, effectiveLenIndiStr, NULL, ""); + return valueAssert; + } + } // fVarLenCountMap.find(...) + + } // !UseBinarySearch } void theory_str::get_concats_in_eqc(expr * n, std::set & concats) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index e77c955f2..4ac054c52 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -75,6 +75,27 @@ namespace smt { } }; + template + class binary_search_trail : public trail { + obj_map > & target; + expr * entry; + public: + binary_search_trail(obj_map > & target, expr * entry) : + target(target), entry(entry) {} + virtual ~binary_search_trail() {} + virtual void undo(Ctx & ctx) { + if (target.contains(entry)) { + if (!target[entry].empty()) { + target[entry].pop_back(); + } else { + TRACE("t_str_binary_search", tout << "WARNING: attempt to remove length tester from an empty stack" << std::endl;); + } + } else { + TRACE("t_str_binary_search", tout << "WARNING: attempt to access length tester map via invalid key" << std::endl;); + } + } + }; + class theory_str : public theory { struct T_cut { @@ -277,6 +298,34 @@ namespace smt { expr * get_eqc_next(expr * n); app * get_ast(theory_var i); + // binary search heuristic data + struct binary_search_info { + rational lowerBound; + rational midPoint; + rational upperBound; + rational windowSize; + + binary_search_info() : lowerBound(rational::zero()), midPoint(rational::zero()), + upperBound(rational::zero()), windowSize(rational::zero()) {} + binary_search_info(rational lower, rational mid, rational upper, rational windowSize) : + lowerBound(lower), midPoint(mid), upperBound(upper), windowSize(windowSize) {} + + void calculate_midpoint() { + midPoint = floor(lowerBound + ((upperBound - lowerBound) / rational(2)) ); + } + }; + // maps a free string var to a stack of active length testers. + // can use binary_search_trail to record changes to this object + obj_map > binary_search_len_tester_stack; + // maps a length tester var to the *active* search window + obj_map binary_search_len_tester_info; + // maps a free string var to the first length tester to be (re)used + obj_map binary_search_starting_len_tester; + // maps a length tester to the next length tester to be (re)used if the split is "low" + obj_map binary_search_next_var_low; + // maps a length tester to the next length tester to be (re)used if the split is "high" + obj_map binary_search_next_var_high; + protected: void assert_axiom(expr * e); void assert_implication(expr * premise, expr * conclusion); @@ -482,6 +531,10 @@ namespace smt { bool get_next_val_encode(int_vector & base, int_vector & next); std::string gen_val_string(int len, int_vector & encoding); + // binary search heuristic + expr * binary_search_length_test(expr * freeVar, expr * previousLenTester, std::string previousLenTesterValue); + expr_ref binary_search_case_split(expr * freeVar, expr * tester, binary_search_info & bounds); + bool free_var_attempt(expr * nn1, expr * nn2); void more_len_tests(expr * lenTester, std::string lenTesterValue); void more_value_tests(expr * valTester, std::string valTesterValue); From 0a6c23148fa1723d6582513b22acc9c632c97e97 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 22 Dec 2016 19:33:38 -0500 Subject: [PATCH 297/562] fix empty vector edge case in binary search --- src/smt/theory_str.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 754d258bc..0edd2726d 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -9229,7 +9229,7 @@ expr_ref theory_str::binary_search_case_split(expr * freeVar, expr * tester, bin expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenTester, std::string previousLenTesterValue) { ast_manager & m = get_manager(); - if (binary_search_len_tester_stack.contains(freeVar)) { + if (binary_search_len_tester_stack.contains(freeVar) && !binary_search_len_tester_stack[freeVar].empty()) { TRACE("t_str_binary_search", tout << "checking existing length testers for " << mk_pp(freeVar, m) << std::endl; for (ptr_vector::const_iterator it = binary_search_len_tester_stack[freeVar].begin(); it != binary_search_len_tester_stack[freeVar].end(); ++it) { From f3e064cb077a417ba97f0198e18097ad3ec10caf Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 31 Dec 2016 13:28:32 -0500 Subject: [PATCH 298/562] theory_str binary search crash avoidance when a negative length is reached --- src/smt/theory_str.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 0edd2726d..d3d680717 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -8426,6 +8426,13 @@ std::string theory_str::gen_val_string(int len, int_vector & encoding) { bool theory_str::get_next_val_encode(int_vector & base, int_vector & next) { SASSERT(charSetSize > 0); + TRACE("t_str_value_test_bug", tout << "base vector: [ "; + for (unsigned i = 0; i < base.size(); ++i) { + tout << base[i] << " "; + } + tout << "]" << std::endl; + ); + int s = 0; int carry = 0; next.reset(); @@ -9228,6 +9235,7 @@ expr_ref theory_str::binary_search_case_split(expr * freeVar, expr * tester, bin expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenTester, std::string previousLenTesterValue) { ast_manager & m = get_manager(); + context & ctx = get_context(); if (binary_search_len_tester_stack.contains(freeVar) && !binary_search_len_tester_stack[freeVar].empty()) { TRACE("t_str_binary_search", tout << "checking existing length testers for " << mk_pp(freeVar, m) << std::endl; @@ -9337,6 +9345,19 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT return next_case_split; } else { // lastTesterConstant is a concrete value TRACE("t_str", tout << "length is fixed; generating models for free var" << std::endl;); + // defensive check that this length did not converge on a negative value. + binary_search_info lastBounds; + if (!binary_search_len_tester_info.find(lastTester, lastBounds)) { + // unexpected + TRACE("t_str_binary_search", tout << "WARNING: no bounds information available for last tester!" << std::endl;); + // TODO resolve this + NOT_IMPLEMENTED_YET(); + } + if (lastBounds.midPoint.is_neg()) { + TRACE("t_str_binary_search", tout << "WARNING: length search converged on a negative value. Negating this constraint." << std::endl;); + expr_ref axiom(m.mk_not(ctx.mk_eq_atom(mk_strlen(freeVar), m_autil.mk_numeral(lastBounds.midPoint, true))), m); + return axiom; + } // length is fixed expr * valueAssert = gen_free_var_options(freeVar, lastTester, lastTesterConstant, NULL, ""); return valueAssert; From f9d7981c1eb81aa7121c0ae5f637ca712864847a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 3 Jan 2017 15:45:04 -0500 Subject: [PATCH 299/562] add theory case split to theory_str binary search --- src/smt/theory_str.cpp | 22 ++++++++++++++++++---- src/smt/theory_str.h | 3 ++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index d3d680717..278f692f8 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2507,6 +2507,7 @@ void theory_str::generate_mutual_exclusion(expr_ref_vector & terms) { literal_vector ls; for (unsigned i = 0; i < terms.size(); ++i) { expr * e = terms.get(i); + // TODO make sure the terms are internalized, etc.? literal l = ctx.get_literal(e); ls.push_back(l); } @@ -9199,7 +9200,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr // Return an expression of the form // (tester = "less" | tester = "N" | tester = "more") & // (tester = "less" iff len(freeVar) < N) & (tester = "more" iff len(freeVar) > N) & (tester = "N" iff len(freeVar) = N)) -expr_ref theory_str::binary_search_case_split(expr * freeVar, expr * tester, binary_search_info & bounds) { +expr_ref theory_str::binary_search_case_split(expr * freeVar, expr * tester, binary_search_info & bounds, literal_vector & case_split) { context & ctx = get_context(); ast_manager & m = get_manager(); rational N = bounds.midPoint; @@ -9227,6 +9228,16 @@ expr_ref theory_str::binary_search_case_split(expr * freeVar, expr * tester, bin combinedCaseSplit.push_back(mk_or(testerCases)); + // force internalization on all terms in testerCases so we can extract literals + for (unsigned i = 0; i < testerCases.size(); ++i) { + expr * testerCase = testerCases.get(i); + if (!ctx.b_internalized(testerCase)) { + ctx.internalize(testerCase, false); + } + literal l = ctx.get_literal(testerCase); + case_split.push_back(l); + } + expr_ref final_term(mk_and(combinedCaseSplit), m); SASSERT(final_term); TRACE("t_str_binary_search", tout << "final term: " << mk_pp(final_term, m) << std::endl;); @@ -9339,9 +9350,10 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT binary_search_len_tester_info.insert(newTester, newBounds); m_trail_stack.push(insert_obj_map(binary_search_len_tester_info, newTester)); - expr_ref next_case_split(binary_search_case_split(freeVar, newTester, newBounds)); + literal_vector case_split_literals; + expr_ref next_case_split(binary_search_case_split(freeVar, newTester, newBounds, case_split_literals)); m_trail.push_back(next_case_split); - // TODO assert a precondition about all previous length testers that got us here + ctx.mk_th_case_split(case_split_literals.size(), case_split_literals.c_ptr()); return next_case_split; } else { // lastTesterConstant is a concrete value TRACE("t_str", tout << "length is fixed; generating models for free var" << std::endl;); @@ -9384,8 +9396,10 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT binary_search_len_tester_info.insert(firstTester, new_info); m_trail_stack.push(insert_obj_map(binary_search_len_tester_info, firstTester)); - expr_ref initial_case_split(binary_search_case_split(freeVar, firstTester, new_info)); + literal_vector case_split_literals; + expr_ref initial_case_split(binary_search_case_split(freeVar, firstTester, new_info, case_split_literals)); m_trail.push_back(initial_case_split); + ctx.mk_th_case_split(case_split_literals.size(), case_split_literals.c_ptr()); return initial_case_split; } } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 4ac054c52..fdd1a9c84 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -84,6 +84,7 @@ namespace smt { target(target), entry(entry) {} virtual ~binary_search_trail() {} virtual void undo(Ctx & ctx) { + TRACE("t_str_binary_search", tout << "in binary_search_trail::undo()" << std::endl;); if (target.contains(entry)) { if (!target[entry].empty()) { target[entry].pop_back(); @@ -533,7 +534,7 @@ namespace smt { // binary search heuristic expr * binary_search_length_test(expr * freeVar, expr * previousLenTester, std::string previousLenTesterValue); - expr_ref binary_search_case_split(expr * freeVar, expr * tester, binary_search_info & bounds); + expr_ref binary_search_case_split(expr * freeVar, expr * tester, binary_search_info & bounds, literal_vector & case_split_lits); bool free_var_attempt(expr * nn1, expr * nn2); void more_len_tests(expr * lenTester, std::string lenTesterValue); From c190d458596803fabc7db00d006c143e93b58e5d Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 4 Jan 2017 15:56:16 -0500 Subject: [PATCH 300/562] fix binary search string length axiom --- src/smt/theory_str.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 278f692f8..bfa439e03 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -9367,7 +9367,7 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT } if (lastBounds.midPoint.is_neg()) { TRACE("t_str_binary_search", tout << "WARNING: length search converged on a negative value. Negating this constraint." << std::endl;); - expr_ref axiom(m.mk_not(ctx.mk_eq_atom(mk_strlen(freeVar), m_autil.mk_numeral(lastBounds.midPoint, true))), m); + expr_ref axiom(m_autil.mk_ge(mk_strlen(freeVar), m_autil.mk_numeral(rational::zero(), true)), m); return axiom; } // length is fixed From 6f5c1942f0529afe2a40193cfbff9a625696ef60 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 8 Jan 2017 20:15:45 -0500 Subject: [PATCH 301/562] theory_str length propagation --- src/smt/theory_str.cpp | 169 ++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 7 +- 2 files changed, 174 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index bfa439e03..120bf426a 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -9,7 +9,7 @@ Abstract: Author: - Murphy Berzish (mtrberzi) 2015-09-03 + Murphy Berzish and Yunhui Zheng Revision History: @@ -7993,6 +7993,160 @@ bool theory_str::finalcheck_int2str(app * a) { return axiomAdd; } +void theory_str::collect_var_concat(expr * node, std::set & varSet, std::set & concatSet) { + if (variable_set.find(node) != variable_set.end()) { + if (internal_lenTest_vars.find(node) == internal_lenTest_vars.end()) { + varSet.insert(node); + } + } + else if (is_app(node)) { + app * aNode = to_app(node); + if (is_strlen(aNode)) { + // Length + return; + } + if (is_concat(aNode)) { + expr * arg0 = aNode->get_arg(0); + expr * arg1 = aNode->get_arg(1); + if (concatSet.find(node) == concatSet.end()) { + concatSet.insert(node); + } + } + // recursively visit all arguments + for (unsigned i = 0; i < aNode->get_num_args(); ++i) { + expr * arg = aNode->get_arg(i); + collect_var_concat(arg, varSet, concatSet); + } + } +} + +bool theory_str::propagate_length_within_eqc(expr * var) { + bool res = false; + ast_manager & m = get_manager(); + context & ctx = get_context(); + + TRACE("t_str_length", tout << "propagate_length_within_eqc: " << mk_ismt2_pp(var, m) << std::endl ;); + + enode * n_eq_enode = ctx.get_enode(var); + rational varLen; + if (! get_len_value(var, varLen)) { + bool hasLen = false; + expr * nodeWithLen= var; + do { + if (get_len_value(nodeWithLen, varLen)) { + hasLen = true; + break; + } + nodeWithLen = get_eqc_next(nodeWithLen); + } while (nodeWithLen != var); + + if (hasLen) { + // var = nodeWithLen --> |var| = |nodeWithLen| + expr_ref_vector l_items(m); + expr_ref varEqNode(ctx.mk_eq_atom(var, nodeWithLen), m); + l_items.push_back(varEqNode); + + expr_ref nodeWithLenExpr (mk_strlen(nodeWithLen), m); + expr_ref varLenExpr (mk_int(varLen), m); + expr_ref lenEqNum(ctx.mk_eq_atom(nodeWithLenExpr, varLenExpr), m); + l_items.push_back(lenEqNum); + + expr_ref axl(m.mk_and(l_items.size(), l_items.c_ptr()), m); + expr_ref varLen(mk_strlen(var), m); + expr_ref axr(ctx.mk_eq_atom(varLen, mk_int(varLen)), m); + assert_implication(axl, axr); + TRACE("t_str_length", tout << mk_ismt2_pp(axl, m) << std::endl << " ---> " << std::endl << mk_ismt2_pp(axr, m);); + res = true; + } + } + return res; +} + +bool theory_str::propagate_length(std::set & varSet, std::set & concatSet, std::map & exprLenMap) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + bool axiomAdded = false; + // collect all concats in context + for (expr_ref_vector::iterator it = assignments.begin(); it != assignments.end(); ++it) { + if (! ctx.is_relevant(*it)) { + continue; + } + if (m.is_eq(*it)) { + collect_var_concat(*it, varSet, concatSet); + } + } + // iterate each concat + // if a concat doesn't have length info, check if the length of all leaf nodes can be resolved + for (std::set::iterator it = concatSet.begin(); it != concatSet.end(); it++) { + expr * concat = *it; + rational lenValue; + expr_ref concatlenExpr (mk_strlen(concat), m) ; + bool allLeafResolved = true; + if (! get_value(concatlenExpr, lenValue)) { + // the length fo concat is unresolved yet + if (get_len_value(concat, lenValue)) { + // but all leaf nodes have length information + TRACE("t_str_length", tout << "* length pop-up: " << mk_ismt2_pp(concat, m) << "| = " << lenValue << std::endl;); + std::set leafNodes; + get_unique_non_concat_nodes(concat, leafNodes); + expr_ref_vector l_items(m); + for (std::set::iterator leafIt = leafNodes.begin(); leafIt != leafNodes.end(); ++leafIt) { + rational leafLenValue; + if (get_len_value(*leafIt, leafLenValue)) { + expr_ref leafItLenExpr (mk_strlen(*leafIt), m); + expr_ref leafLenValueExpr (mk_int(leafLenValue), m); + expr_ref lcExpr (ctx.mk_eq_atom(leafItLenExpr, leafLenValueExpr), m); + l_items.push_back(lcExpr); + } else { + allLeafResolved = false; + break; + } + } + if (allLeafResolved) { + expr_ref axl(m.mk_and(l_items.size(), l_items.c_ptr()), m); + expr_ref lenValueExpr (mk_int(lenValue), m); + expr_ref axr(ctx.mk_eq_atom(concatlenExpr, lenValueExpr), m); + assert_implication(axl, axr); + TRACE("t_str_length", tout << mk_ismt2_pp(axl, m) << std::endl << " ---> " << std::endl << mk_ismt2_pp(axr, m)<< std::endl;); + axiomAdded = true; + } + } + } + } + // if no concat length is propagated, check the length of variables. + if (! axiomAdded) { + for (std::set::iterator it = varSet.begin(); it != varSet.end(); it++) { + expr * var = *it; + rational lenValue; + expr_ref varlen (mk_strlen(var), m) ; + bool allLeafResolved = true; + if (! get_value(varlen, lenValue)) { + if (propagate_length_within_eqc(var)) { + axiomAdded = true; + } + } + } + + } + return axiomAdded; +} + +void theory_str::get_unique_non_concat_nodes(expr * node, std::set & argSet) { + app * a_node = to_app(node); + if (!is_concat(a_node)) { + argSet.insert(node); + return; + } else { + SASSERT(a_node->get_num_args() == 2); + expr * leftArg = a_node->get_arg(0); + expr * rightArg = a_node->get_arg(1); + get_unique_non_concat_nodes(leftArg, argSet); + get_unique_non_concat_nodes(rightArg, argSet); + } +} + final_check_status theory_str::final_check_eh() { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -8110,6 +8264,19 @@ final_check_status theory_str::final_check_eh() { return FC_CONTINUE; } + // enhancement: improved backpropagation of length information + { + std::set varSet; + std::set concatSet; + std::map exprLenMap; + + bool length_propagation_occurred = propagate_length(varSet, concatSet, exprLenMap); + if (length_propagation_occurred) { + TRACE("t_str", tout << "Resuming search due to axioms added by length propagation." << std::endl;); + return FC_CONTINUE; + } + } + bool needToAssignFreeVars = false; std::set free_variables; std::set unused_internal_variables; diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index fdd1a9c84..b7229a72e 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -9,7 +9,7 @@ Abstract: Author: - Murphy Berzish (mtrberzi) 2015-09-03 + Murphy Berzish and Yunhui Zheng Revision History: @@ -568,6 +568,11 @@ namespace smt { void check_variable_scope(); void recursive_check_variable_scope(expr * ex); + void collect_var_concat(expr * node, std::set & varSet, std::set & concatSet); + bool propagate_length(std::set & varSet, std::set & concatSet, std::map & exprLenMap); + void get_unique_non_concat_nodes(expr * node, std::set & argSet); + bool propagate_length_within_eqc(expr * var); + // TESTING void refresh_theory_var(expr * e); From 5f854c6689b4aa6250a79f28822fdde6c6ea5d48 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 9 Jan 2017 15:11:56 -0500 Subject: [PATCH 302/562] experimental linear search theory case split in theory_str --- src/smt/theory_str.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 120bf426a..9a71c05a9 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -9277,6 +9277,9 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr } ); + // experimental theory-aware case split support + literal_vector case_split_literals; + for (int i = l; i < h; ++i) { expr_ref str_indicator(m); if (m_params.m_UseFastLengthTesterCache) { @@ -9305,6 +9308,8 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr ctx.force_phase(l); } + case_split_literals.insert(mk_eq(freeVarLen, mk_int(i), false)); + expr_ref and_expr(ctx.mk_eq_atom(orList.get(orList.size() - 1), m.mk_eq(freeVarLen, mk_int(i))), m); andList.push_back(and_expr); } @@ -9319,6 +9324,13 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr andList.push_back(ctx.mk_eq_atom(orList.get(orList.size() - 1), m_autil.mk_ge(freeVarLen, mk_int(h)))); + { // more experimental theory case split support + expr_ref tmp(m_autil.mk_ge(freeVarLen, mk_int(h)), m); + ctx.internalize(m_autil.mk_ge(freeVarLen, mk_int(h)), false); + case_split_literals.push_back(ctx.get_literal(tmp)); + ctx.mk_th_case_split(case_split_literals.size(), case_split_literals.c_ptr()); + } + expr_ref_vector or_items(m); expr_ref_vector and_items(m); From 9004e1b23e9e662d6fde73e1db5cfeedc9399b1b Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 10 Jan 2017 12:34:44 -0500 Subject: [PATCH 303/562] disable length test/theory case split integration theory_str --- src/smt/theory_str.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 9a71c05a9..5a27dcebb 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -9324,12 +9324,14 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr andList.push_back(ctx.mk_eq_atom(orList.get(orList.size() - 1), m_autil.mk_ge(freeVarLen, mk_int(h)))); + /* { // more experimental theory case split support expr_ref tmp(m_autil.mk_ge(freeVarLen, mk_int(h)), m); ctx.internalize(m_autil.mk_ge(freeVarLen, mk_int(h)), false); case_split_literals.push_back(ctx.get_literal(tmp)); ctx.mk_th_case_split(case_split_literals.size(), case_split_literals.c_ptr()); } + */ expr_ref_vector or_items(m); expr_ref_vector and_items(m); @@ -9532,7 +9534,7 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT literal_vector case_split_literals; expr_ref next_case_split(binary_search_case_split(freeVar, newTester, newBounds, case_split_literals)); m_trail.push_back(next_case_split); - ctx.mk_th_case_split(case_split_literals.size(), case_split_literals.c_ptr()); + // ctx.mk_th_case_split(case_split_literals.size(), case_split_literals.c_ptr()); return next_case_split; } else { // lastTesterConstant is a concrete value TRACE("t_str", tout << "length is fixed; generating models for free var" << std::endl;); @@ -9578,7 +9580,7 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT literal_vector case_split_literals; expr_ref initial_case_split(binary_search_case_split(freeVar, firstTester, new_info, case_split_literals)); m_trail.push_back(initial_case_split); - ctx.mk_th_case_split(case_split_literals.size(), case_split_literals.c_ptr()); + // ctx.mk_th_case_split(case_split_literals.size(), case_split_literals.c_ptr()); return initial_case_split; } } From 3459c1993ebd8b21745e4b796cc4cbc2b45c4005 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 10 Jan 2017 15:38:33 -0500 Subject: [PATCH 304/562] experimental theory-aware branching code --- src/smt/params/smt_params.cpp | 1 + src/smt/params/smt_params.h | 2 + src/smt/params/smt_params_helper.pyg | 3 +- src/smt/smt_case_split_queue.cpp | 164 ++++++++++++++++++++++++++- src/smt/smt_case_split_queue.h | 3 + 5 files changed, 166 insertions(+), 7 deletions(-) diff --git a/src/smt/params/smt_params.cpp b/src/smt/params/smt_params.cpp index a5b3e4867..f295e260b 100644 --- a/src/smt/params/smt_params.cpp +++ b/src/smt/params/smt_params.cpp @@ -32,6 +32,7 @@ void smt_params::updt_local_params(params_ref const & _p) { m_restart_factor = p.restart_factor(); m_case_split_strategy = static_cast(p.case_split()); m_theory_case_split = p.theory_case_split(); + m_theory_aware_branching = p.theory_aware_branching(); m_delay_units = p.delay_units(); m_delay_units_threshold = p.delay_units_threshold(); m_preprocess = _p.get_bool("preprocess", true); // hidden parameter diff --git a/src/smt/params/smt_params.h b/src/smt/params/smt_params.h index 55346d34f..a0c90a525 100644 --- a/src/smt/params/smt_params.h +++ b/src/smt/params/smt_params.h @@ -112,6 +112,7 @@ struct smt_params : public preprocessor_params, unsigned m_rel_case_split_order; bool m_lookahead_diseq; bool m_theory_case_split; + bool m_theory_aware_branching; // ----------------------------------- // @@ -243,6 +244,7 @@ struct smt_params : public preprocessor_params, m_rel_case_split_order(0), m_lookahead_diseq(false), m_theory_case_split(false), + m_theory_aware_branching(false), m_delay_units(false), m_delay_units_threshold(32), m_theory_resolve(false), diff --git a/src/smt/params/smt_params_helper.pyg b/src/smt/params/smt_params_helper.pyg index 4e3bec57d..8e8e52987 100644 --- a/src/smt/params/smt_params_helper.pyg +++ b/src/smt/params/smt_params_helper.pyg @@ -71,5 +71,6 @@ def_module_params(module_name='smt', ('str.string_constant_cache', BOOL, True, 'cache all generated string constants generated from anywhere in theory_str'), ('str.use_binary_search', BOOL, False, 'use a binary search heuristic for finding concrete length values for free variables in theory_str (set to False to use linear search)'), ('str.binary_search_start', UINT, 64, 'initial upper bound for theory_str binary search'), - ('theory_case_split', BOOL, False, 'Allow the context to use heuristics involving theory case splits, which are a set of literals of which exactly one can be assigned True. If this option is false, the context will generate extra axioms to enforce this instead.') + ('theory_case_split', BOOL, False, 'Allow the context to use heuristics involving theory case splits, which are a set of literals of which exactly one can be assigned True. If this option is false, the context will generate extra axioms to enforce this instead.'), + ('theory_aware_branching', BOOL, False, 'Allow the context to use extra information from theory solvers regarding literal branching prioritization.') )) diff --git a/src/smt/smt_case_split_queue.cpp b/src/smt/smt_case_split_queue.cpp index 06004e3b8..8b02dd6a9 100644 --- a/src/smt/smt_case_split_queue.cpp +++ b/src/smt/smt_case_split_queue.cpp @@ -22,9 +22,13 @@ Revision History: #include"stopwatch.h" #include"for_each_expr.h" #include"ast_pp.h" +#include"map.h" +#include"hashtable.h" namespace smt { + typedef map > theory_var_priority_map; + struct bool_var_act_lt { svector const & m_activity; bool_var_act_lt(svector const & a):m_activity(a) {} @@ -35,6 +39,25 @@ namespace smt { typedef heap bool_var_act_queue; + struct theory_aware_act_lt { + // only take into account theory var priority for now + theory_var_priority_map const & m_theory_var_priority; + theory_aware_act_lt(theory_var_priority_map const & a):m_theory_var_priority(a) {} + bool operator()(bool_var v1, bool_var v2) const { + double p_v1, p_v2; + // safety -- use a large negative number if some var isn't in the map + if (!m_theory_var_priority.find(v1, p_v1)) { + p_v1 = -1000.0; + } + if (!m_theory_var_priority.find(v2, p_v2)) { + p_v2 = -1000.0; + } + return p_v1 > p_v2; + } + }; + + typedef heap theory_aware_act_queue; + /** \brief Case split queue based on activity and random splits. */ @@ -1087,6 +1110,118 @@ namespace smt { } }; + class theory_aware_branching_queue : public case_split_queue { + protected: + context & m_context; + smt_params & m_params; + + theory_var_priority_map m_theory_var_priority; + theory_aware_act_queue m_theory_queue; + case_split_queue * m_base_queue; + int_hashtable > m_theory_vars; + map > m_theory_var_phase; + public: + theory_aware_branching_queue(context & ctx, smt_params & p, case_split_queue * base_queue) : + m_context(ctx), + m_params(p), + m_theory_var_priority(), + m_theory_queue(1024, theory_aware_act_lt(m_theory_var_priority)), + m_base_queue(base_queue) { + } + + virtual void activity_increased_eh(bool_var v) { + if (m_theory_queue.contains(v)) { + m_theory_queue.decreased(v); + } + m_base_queue->activity_increased_eh(v); + } + + virtual void mk_var_eh(bool_var v) { + // do nothing. we only "react" if/when we learn this is an important theory literal + m_base_queue->mk_var_eh(v); + } + + virtual void del_var_eh(bool_var v) { + if (m_theory_queue.contains(v)) { + m_theory_queue.erase(v); + } + m_base_queue->del_var_eh(v); + } + + virtual void assign_lit_eh(literal l) { + m_base_queue->assign_lit_eh(l); + } + + virtual void unassign_var_eh(bool_var v) { + if (m_theory_vars.contains(v) && !m_theory_queue.contains(v)) { + m_theory_queue.insert(v); + } + m_base_queue->unassign_var_eh(v); + } + + virtual void relevant_eh(expr * n) { + m_base_queue->relevant_eh(n); + } + + virtual void init_search_eh() { + m_base_queue->init_search_eh(); + } + + virtual void end_search_eh() { + m_base_queue->end_search_eh(); + } + + virtual void internalize_instance_eh(expr * e, unsigned gen) { + m_base_queue->internalize_instance_eh(e, gen); + } + + virtual void reset() { + m_theory_queue.reset(); + m_theory_vars.reset(); + m_theory_var_phase.reset(); + m_theory_var_priority.reset(); + m_base_queue->reset(); + } + + virtual void push_scope() { + m_base_queue->push_scope(); + } + + virtual void pop_scope(unsigned num_scopes) { + m_base_queue->pop_scope(num_scopes); + } + + virtual void next_case_split(bool_var & next, lbool & phase) { + while (!m_theory_queue.empty()) { + next = m_theory_queue.erase_min(); + // if this literal is unassigned, it is the theory literal with the highest priority, + // so case split on this + if (m_context.get_assignment(next) == l_undef) { + TRACE("theory_aware_branching", tout << "Theory-aware branch on l#" << next << std::endl;); + if (!m_theory_var_phase.find(next, phase)) { + phase = l_undef; + } + return; + } + } + // if we reach this point, the theory literal queue is empty, + // so fall back to the base queue + m_base_queue->next_case_split(next, phase); + } + + virtual void add_theory_aware_branching_info(bool_var v, double priority, lbool phase) { + TRACE("theory_aware_branching", tout << "Add theory-aware branching information for l#" << v << ": priority=" << priority << std::endl;); + m_theory_vars.insert(v); + m_theory_var_phase.insert(v, phase); + m_theory_var_priority.insert(v, priority); + m_theory_queue.insert(v); + } + + virtual void display(std::ostream & out) { + // TODO + m_base_queue->display(out); + } + }; case_split_queue * mk_case_split_queue(context & ctx, smt_params & p) { if (p.m_relevancy_lvl < 2 && (p.m_case_split_strategy == CS_RELEVANCY || p.m_case_split_strategy == CS_RELEVANCY_ACTIVITY || @@ -1099,19 +1234,36 @@ namespace smt { warning_msg("auto configuration (option AUTO_CONFIG) must be disabled to use option CASE_SPLIT=3, 4 or 5"); p.m_case_split_strategy = CS_ACTIVITY; } + + case_split_queue * baseQueue; + switch (p.m_case_split_strategy) { case CS_ACTIVITY_DELAY_NEW: - return alloc(dact_case_split_queue, ctx, p); + baseQueue = alloc(dact_case_split_queue, ctx, p); + break; case CS_ACTIVITY_WITH_CACHE: - return alloc(cact_case_split_queue, ctx, p); + baseQueue = alloc(cact_case_split_queue, ctx, p); + break; case CS_RELEVANCY: - return alloc(rel_case_split_queue, ctx, p); + baseQueue = alloc(rel_case_split_queue, ctx, p); + break; case CS_RELEVANCY_ACTIVITY: - return alloc(rel_act_case_split_queue, ctx, p); + baseQueue = alloc(rel_act_case_split_queue, ctx, p); + break; case CS_RELEVANCY_GOAL: - return alloc(rel_goal_case_split_queue, ctx, p); + baseQueue = alloc(rel_goal_case_split_queue, ctx, p); + break; default: - return alloc(act_case_split_queue, ctx, p); + baseQueue = alloc(act_case_split_queue, ctx, p); + break; + } + + if (p.m_theory_aware_branching) { + TRACE("theory_aware_branching", tout << "Allocating and returning theory-aware branching queue." << std::endl;); + case_split_queue * theory_aware_queue = alloc(theory_aware_branching_queue, ctx, p, baseQueue); + return theory_aware_queue; + } else { + return baseQueue; } } diff --git a/src/smt/smt_case_split_queue.h b/src/smt/smt_case_split_queue.h index e6b217a22..9a3a93cc6 100644 --- a/src/smt/smt_case_split_queue.h +++ b/src/smt/smt_case_split_queue.h @@ -46,6 +46,9 @@ namespace smt { virtual void next_case_split(bool_var & next, lbool & phase) = 0; virtual void display(std::ostream & out) = 0; virtual ~case_split_queue() {} + + // theory-aware branching hint + virtual void add_theory_aware_branching_info(bool_var v, double priority, lbool phase) {} }; case_split_queue * mk_case_split_queue(context & ctx, smt_params & p); From 1363f50e4ffce014dc80a5e757529232c93f6154 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 10 Jan 2017 19:50:46 -0500 Subject: [PATCH 305/562] demonstration of theory-aware branching in theory_str, WIP --- src/smt/smt_context.cpp | 4 ++++ src/smt/smt_context.h | 7 +++++++ src/smt/theory_str.cpp | 40 +++++++++++++++++----------------------- src/smt/theory_str.h | 1 + 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 6c0a89d4f..2de610772 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -2999,6 +2999,10 @@ namespace smt { } } + void context::add_theory_aware_branching_info(bool_var v, double priority, lbool phase) { + m_case_split_queue->add_theory_aware_branching_info(v, priority, phase); + } + void context::undo_th_case_split(literal l) { m_all_th_case_split_literals.remove(l.index()); if (m_literal2casesplitsets.contains(l.index())) { diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index 8016eb587..2aae6c8a5 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -825,6 +825,13 @@ namespace smt { */ void mk_th_case_split(unsigned num_lits, literal * lits); + /* + * Provide a hint to the branching heuristic about the priority of a "theory-aware literal". + * Literals marked in this way will always be branched on before unmarked literals, + * starting with the literal having the highest priority. + */ + void add_theory_aware_branching_info(bool_var v, double priority, lbool phase); + // helper function for trail void undo_th_case_split(literal l); diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 5a27dcebb..f49b539dd 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2501,6 +2501,13 @@ void theory_str::infer_len_concat_equality(expr * nn1, expr * nn2) { */ } +void theory_str::add_theory_aware_branching_info(expr * term, double priority, lbool phase) { + context & ctx = get_context(); + ctx.internalize(term, false); + bool_var v = ctx.get_bool_var(term); + ctx.add_theory_aware_branching_info(v, priority, phase); +} + void theory_str::generate_mutual_exclusion(expr_ref_vector & terms) { context & ctx = get_context(); // pull each literal out of the arrangement disjunction @@ -2512,25 +2519,6 @@ void theory_str::generate_mutual_exclusion(expr_ref_vector & terms) { ls.push_back(l); } ctx.mk_th_case_split(ls.size(), ls.c_ptr()); - - // old version, without special support in the context - /* - ast_manager & m = get_manager(); - - expr_ref_vector result(m); - - for (unsigned int majorIndex = 0; majorIndex < terms.size(); ++majorIndex) { - for (unsigned int minorIndex = majorIndex + 1; minorIndex < terms.size(); ++minorIndex) { - // generate an expression of the form - // terms[majorIndex] --> NOT(terms[minorIndex]) - expr_ref ex(rewrite_implication(terms.get(majorIndex), m.mk_not(terms.get(minorIndex))), m); - result.push_back(ex); - } - } - - expr_ref final_result(mk_and(result), m); - assert_axiom(final_result); - */ } void theory_str::print_cut_var(expr * node, std::ofstream & xout) { @@ -3095,7 +3083,9 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { m_autil.mk_add(mk_strlen(y),m_autil.mk_mul(mk_int(-1), mk_strlen(n))), mk_int(0))) ); - arrangement_disjunction.push_back(mk_and(and_item)); + expr_ref option1(mk_and(and_item), mgr); + arrangement_disjunction.push_back(option1); + add_theory_aware_branching_info(option1, 0.0, l_true); add_cut_info_merge(t1, ctx.get_scope_level(), m); add_cut_info_merge(t1, ctx.get_scope_level(), y); @@ -3130,8 +3120,9 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { m_autil.mk_add(mk_strlen(n), m_autil.mk_mul(mk_int(-1), mk_strlen(y))), mk_int(0))) ); - - arrangement_disjunction.push_back(mk_and(and_item)); + expr_ref option2(mk_and(and_item), mgr); + arrangement_disjunction.push_back(option2); + add_theory_aware_branching_info(option2, 0.0, l_true); add_cut_info_merge(t2, ctx.get_scope_level(), x); add_cut_info_merge(t2, ctx.get_scope_level(), n); @@ -3149,7 +3140,10 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { and_item.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_strlen(m))); and_item.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_strlen(n))); - arrangement_disjunction.push_back(mk_and(and_item)); + expr_ref option3(mk_and(and_item), mgr); + arrangement_disjunction.push_back(option3); + // prioritize this case, it is easier + add_theory_aware_branching_info(option3, 2.0, l_true); } if (!arrangement_disjunction.empty()) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index b7229a72e..1f615cfc5 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -500,6 +500,7 @@ namespace smt { void print_cut_var(expr * node, std::ofstream & xout); void generate_mutual_exclusion(expr_ref_vector & exprs); + void add_theory_aware_branching_info(expr * term, double priority, lbool phase); bool new_eq_check(expr * lhs, expr * rhs); void group_terms_by_eqc(expr * n, std::set & concats, std::set & vars, std::set & consts); From bc5af5873463f9648a093e8e9c21c8a2d0ce487c Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 10 Jan 2017 20:08:35 -0500 Subject: [PATCH 306/562] additional theory-aware branches in theory_str --- src/smt/theory_str.cpp | 59 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index f49b539dd..0bc9e8dc8 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3453,7 +3453,9 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { and_item.push_back(ctx.mk_eq_atom(mk_strlen(m), m_autil.mk_add(mk_strlen(x), mk_strlen(temp1)))); - arrangement_disjunction.push_back(mk_and(and_item)); + expr_ref option1(mk_and(and_item), mgr); + arrangement_disjunction.push_back(option1); + add_theory_aware_branching_info(option1, 0.0, l_true); add_cut_info_merge(temp1, ctx.get_scope_level(), y); add_cut_info_merge(temp1, ctx.get_scope_level(), m); } else { @@ -3475,7 +3477,16 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { and_item.push_back(ctx.mk_eq_atom(x, x_concat)); and_item.push_back(ctx.mk_eq_atom(y, cropStr)); and_item.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(part2Str.length()))); - arrangement_disjunction.push_back(mk_and(and_item)); + expr_ref option2(mk_and(and_item), mgr); + arrangement_disjunction.push_back(option2); + double priority; + // prioritize the option where y is equal to the original string + if (i == 0) { + priority = 2.0; + } else { + priority = 0.0; + } + add_theory_aware_branching_info(option2, priority, l_true); } } @@ -3772,7 +3783,15 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { // and_item[pos++] = Z3_mk_eq(ctx, or_item[option], Z3_mk_eq(ctx, mk_length(t, y), mk_length(t, y_concat))); // adding length constraint for _ = constStr seems slowing things down. - arrangement_disjunction.push_back(mk_and(and_item)); + expr_ref option1(mk_and(and_item), mgr); + arrangement_disjunction.push_back(option1); + double priority; + if (i == (int)strValue.size()) { + priority = 1.0; + } else { + priority = 0.0; + } + add_theory_aware_branching_info(option1, priority, l_true); } } @@ -3794,7 +3813,9 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { and_item.push_back(ctx.mk_eq_atom(mk_strlen(x), m_autil.mk_add(mk_strlen(strAst), mk_strlen(temp1)) ) ); ++pos; - arrangement_disjunction.push_back(mk_and(and_item)); + expr_ref option2(mk_and(and_item), mgr); + arrangement_disjunction.push_back(option2); + add_theory_aware_branching_info(option2, 0.0, l_true); add_cut_info_merge(temp1, sLevel, x); add_cut_info_merge(temp1, sLevel, n); @@ -4194,7 +4215,9 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { // addItems[1] = mk_length(t, str2Ast); // and_item[pos++] = Z3_mk_eq(ctx, or_item[option], Z3_mk_eq(ctx, mk_length(t, y), Z3_mk_add(ctx, 2, addItems))); - arrangement_disjunction.push_back(mk_and(and_item)); + expr_ref option1(mk_and(and_item), mgr); + arrangement_disjunction.push_back(option1); + add_theory_aware_branching_info(option1, 0.0, l_true); } else { loopDetected = true; TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); @@ -4227,7 +4250,16 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { and_item.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_strlen(suffixAst))); pos += 1; - arrangement_disjunction.push_back(mk_and(and_item)); + expr_ref option2(mk_and(and_item), mgr); + arrangement_disjunction.push_back(option2); + double priority; + // prefer the option "str1" = x + if (prefix == str1Value) { + priority = 1.0; + } else { + priority = 0.0; + } + add_theory_aware_branching_info(option2, priority, l_true); } // case 6: concat("str1", y) = concat(m, "str2") @@ -9296,6 +9328,16 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); orList.push_back(or_expr); + double priority; + // give high priority to small lengths if this is available + if (i <= 5) { + priority = 3.0; + } else { + // prioritize over "more" + priority = 0.5; + } + add_theory_aware_branching_info(or_expr, priority, l_true); + if (m_params.m_AggressiveLengthTesting) { literal l = mk_eq(indicator, str_indicator, false); ctx.mark_as_relevant(l); @@ -9309,7 +9351,10 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr } // TODO cache mk_string("more") - orList.push_back(m.mk_eq(indicator, mk_string("more"))); + expr_ref more_option(ctx.mk_eq_atom(indicator, mk_string("more")), m); + orList.push_back(more_option); + // decrease priority of this option + add_theory_aware_branching_info(more_option, -1.0, l_true); if (m_params.m_AggressiveLengthTesting) { literal l = mk_eq(indicator, mk_string("more"), false); ctx.mark_as_relevant(l); From 20a8ad9b2101b558191aa650353c421ad7c28ca0 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 10 Jan 2017 22:15:46 -0500 Subject: [PATCH 307/562] correctly reserve entries in theory aware branching queue heap --- src/smt/smt_case_split_queue.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/smt/smt_case_split_queue.cpp b/src/smt/smt_case_split_queue.cpp index 8b02dd6a9..ebe9c2e4e 100644 --- a/src/smt/smt_case_split_queue.cpp +++ b/src/smt/smt_case_split_queue.cpp @@ -1130,9 +1130,6 @@ namespace smt { } virtual void activity_increased_eh(bool_var v) { - if (m_theory_queue.contains(v)) { - m_theory_queue.decreased(v); - } m_base_queue->activity_increased_eh(v); } @@ -1214,6 +1211,7 @@ namespace smt { m_theory_vars.insert(v); m_theory_var_phase.insert(v, phase); m_theory_var_priority.insert(v, priority); + m_theory_queue.reserve(v+1); m_theory_queue.insert(v); } From 6576dabd583c3d8789e519cec5b6aafbc5a5cac8 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 12 Jan 2017 00:20:34 -0500 Subject: [PATCH 308/562] add tracing info to theory_str cut var map --- src/smt/theory_str.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 0bc9e8dc8..44d13d666 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -416,14 +416,14 @@ void theory_str::add_cut_info_one_node(expr * baseNode, int slevel, expr * node) varInfo->vars[node] = 1; cut_var_map.insert(baseNode, std::stack()); cut_var_map[baseNode].push(varInfo); - TRACE("t_str_cut_var_map", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << std::endl;); + TRACE("t_str_cut_var_map", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); } else { if (cut_var_map[baseNode].empty()) { T_cut * varInfo = alloc(T_cut); varInfo->level = slevel; varInfo->vars[node] = 1; cut_var_map[baseNode].push(varInfo); - TRACE("t_str_cut_var_map", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << std::endl;); + TRACE("t_str_cut_var_map", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); } else { if (cut_var_map[baseNode].top()->level < slevel) { T_cut * varInfo = alloc(T_cut); @@ -431,10 +431,10 @@ void theory_str::add_cut_info_one_node(expr * baseNode, int slevel, expr * node) cut_vars_map_copy(varInfo->vars, cut_var_map[baseNode].top()->vars); varInfo->vars[node] = 1; cut_var_map[baseNode].push(varInfo); - TRACE("t_str_cut_var_map", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << std::endl;); + TRACE("t_str_cut_var_map", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); } else if (cut_var_map[baseNode].top()->level == slevel) { cut_var_map[baseNode].top()->vars[node] = 1; - TRACE("t_str_cut_var_map", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << std::endl;); + TRACE("t_str_cut_var_map", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); } else { get_manager().raise_exception("entered illegal state during add_cut_info_one_node()"); } @@ -460,7 +460,7 @@ void theory_str::add_cut_info_merge(expr * destNode, int slevel, expr * srcNode) cut_vars_map_copy(varInfo->vars, cut_var_map[srcNode].top()->vars); cut_var_map.insert(destNode, std::stack()); cut_var_map[destNode].push(varInfo); - TRACE("t_str_cut_var_map", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << std::endl;); + TRACE("t_str_cut_var_map", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << " [" << slevel << "]" << std::endl;); } else { if (cut_var_map[destNode].empty() || cut_var_map[destNode].top()->level < slevel) { T_cut * varInfo = alloc(T_cut); @@ -468,10 +468,10 @@ void theory_str::add_cut_info_merge(expr * destNode, int slevel, expr * srcNode) cut_vars_map_copy(varInfo->vars, cut_var_map[destNode].top()->vars); cut_vars_map_copy(varInfo->vars, cut_var_map[srcNode].top()->vars); cut_var_map[destNode].push(varInfo); - TRACE("t_str_cut_var_map", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << std::endl;); + TRACE("t_str_cut_var_map", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << " [" << slevel << "]" << std::endl;); } else if (cut_var_map[destNode].top()->level == slevel) { cut_vars_map_copy(cut_var_map[destNode].top()->vars, cut_var_map[srcNode].top()->vars); - TRACE("t_str_cut_var_map", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << std::endl;); + TRACE("t_str_cut_var_map", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << " [" << slevel << "]" << std::endl;); } else { get_manager().raise_exception("illegal state in add_cut_info_merge(): inconsistent slevels"); } @@ -4221,7 +4221,7 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { } else { loopDetected = true; TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); - // TODO printCutVAR(m, y) + TRACE("t_str", print_cut_var(m, tout); print_cut_var(y, tout);); } for (std::list::iterator itor = overlapLen.begin(); itor != overlapLen.end(); itor++) { @@ -6985,8 +6985,10 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { obj_map >::iterator varItor = cut_var_map.begin(); while (varItor != cut_var_map.end()) { + expr * e = varItor->m_key; std::stack & val = cut_var_map[varItor->m_key]; while ((val.size() > 0) && (val.top()->level != 0) && (val.top()->level >= sLevel)) { + TRACE("t_str_cut_var_map", tout << "remove cut info for " << mk_pp(e, m) << std::endl; print_cut_var(e, tout);); T_cut * aCut = val.top(); val.pop(); // dealloc(aCut); // TODO find a safer way to do this, it is causing a crash From 677fcdcb41e93eb450774c97ba497d4368d55066 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 12 Jan 2017 18:41:30 -0500 Subject: [PATCH 309/562] concat overlap avoid in theory_str --- src/smt/theory_str.cpp | 202 ++++++++++++++++++++++++++++++++++++++++- src/smt/theory_str.h | 11 +++ 2 files changed, 212 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 44d13d666..03b04d308 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -40,6 +40,7 @@ theory_str::theory_str(ast_manager & m, theory_str_params const & params): opt_DisableIntegerTheoryIntegration(false), opt_DeferEQCConsistencyCheck(false), opt_CheckVariableScope(true), + opt_ConcatOverlapAvoid(true), /* Internal setup */ search_started(false), m_autil(m), @@ -2801,6 +2802,179 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { } +/* + * Returns true if attempting to process a concat equality between lhs and rhs + * will result in overlapping variables (false otherwise). + */ +bool theory_str::will_result_in_overlap(expr * lhs, expr * rhs) { + ast_manager & m = get_manager(); + + expr_ref new_nn1(simplify_concat(lhs), m); + expr_ref new_nn2(simplify_concat(rhs), m); + app * a_new_nn1 = to_app(new_nn1); + app * a_new_nn2 = to_app(new_nn2); + + bool n1IsConcat = is_concat(a_new_nn1); + bool n2IsConcat = is_concat(a_new_nn2); + if (!n1IsConcat && !n2IsConcat) { + // we simplified both sides to non-concat expressions... + return false; + } + + expr * v1_arg0 = a_new_nn1->get_arg(0); + expr * v1_arg1 = a_new_nn1->get_arg(1); + expr * v2_arg0 = a_new_nn2->get_arg(0); + expr * v2_arg1 = a_new_nn2->get_arg(1); + + TRACE("t_str_detail", tout << "checking whether " << mk_pp(new_nn1, m) << " and " << mk_pp(new_nn1, m) << " might overlap." << std::endl;); + + check_and_init_cut_var(v1_arg0); + check_and_init_cut_var(v1_arg1); + check_and_init_cut_var(v2_arg0); + check_and_init_cut_var(v2_arg1); + + //************************************************************* + // case 1: concat(x, y) = concat(m, n) + //************************************************************* + if (is_concat_eq_type1(new_nn1, new_nn2)) { + TRACE("t_str_detail", tout << "Type 1 check." << std::endl;); + expr * x = to_app(new_nn1)->get_arg(0); + expr * y = to_app(new_nn1)->get_arg(1); + expr * m = to_app(new_nn2)->get_arg(0); + expr * n = to_app(new_nn2)->get_arg(1); + + // TODO is it too slow to perform length checks here to avoid false positives? + + if (has_self_cut(m, y)) { + TRACE("t_str_detail", tout << "Possible overlap found" << std::endl; print_cut_var(m, tout); print_cut_var(y, tout);); + return true; + } else if (has_self_cut(x, n)) { + TRACE("t_str_detail", tout << "Possible overlap found" << std::endl; print_cut_var(x, tout); print_cut_var(n, tout);); + return true; + } else { + return false; + } + } + + //************************************************************* + // case 2: concat(x, y) = concat(m, "str") + //************************************************************* + if (is_concat_eq_type2(new_nn1, new_nn2)) { + expr * x = NULL; + expr * y = NULL; + expr * strAst = NULL; + expr * m = NULL; + + expr * v1_arg0 = to_app(new_nn1)->get_arg(0); + expr * v1_arg1 = to_app(new_nn1)->get_arg(1); + expr * v2_arg0 = to_app(new_nn2)->get_arg(0); + expr * v2_arg1 = to_app(new_nn2)->get_arg(1); + + if (m_strutil.is_string(v1_arg1) && !m_strutil.is_string(v2_arg1)) { + m = v1_arg0; + strAst = v1_arg1; + x = v2_arg0; + y = v2_arg1; + } else { + m = v2_arg0; + strAst = v2_arg1; + x = v1_arg0; + y = v1_arg1; + } + + if (has_self_cut(m, y)) { + TRACE("t_str_detail", tout << "Possible overlap found" << std::endl; print_cut_var(m, tout); print_cut_var(y, tout);); + return true; + } else { + return false; + } + } + + //************************************************************* + // case 3: concat(x, y) = concat("str", n) + //************************************************************* + if (is_concat_eq_type3(new_nn1, new_nn2)) { + expr * v1_arg0 = to_app(new_nn1)->get_arg(0); + expr * v1_arg1 = to_app(new_nn1)->get_arg(1); + expr * v2_arg0 = to_app(new_nn2)->get_arg(0); + expr * v2_arg1 = to_app(new_nn2)->get_arg(1); + + expr * x = NULL; + expr * y = NULL; + expr * strAst = NULL; + expr * n = NULL; + + if (m_strutil.is_string(v1_arg0) && !m_strutil.is_string(v2_arg0)) { + strAst = v1_arg0; + n = v1_arg1; + x = v2_arg0; + y = v2_arg1; + } else { + strAst = v2_arg0; + n = v2_arg1; + x = v1_arg0; + y = v1_arg1; + } + if (has_self_cut(x, n)) { + TRACE("t_str_detail", tout << "Possible overlap found" << std::endl; print_cut_var(x, tout); print_cut_var(n, tout);); + return true; + } else { + return false; + } + } + + //************************************************************* + // case 4: concat("str1", y) = concat("str2", n) + //************************************************************* + if (is_concat_eq_type4(new_nn1, new_nn2)) { + // This case can never result in an overlap. + return false; + } + + //************************************************************* + // case 5: concat(x, "str1") = concat(m, "str2") + //************************************************************* + if (is_concat_eq_type5(new_nn1, new_nn2)) { + // This case can never result in an overlap. + return false; + } + //************************************************************* + // case 6: concat("str1", y) = concat(m, "str2") + //************************************************************* + if (is_concat_eq_type6(new_nn1, new_nn2)) { + expr * v1_arg0 = to_app(new_nn1)->get_arg(0); + expr * v1_arg1 = to_app(new_nn1)->get_arg(1); + expr * v2_arg0 = to_app(new_nn2)->get_arg(0); + expr * v2_arg1 = to_app(new_nn2)->get_arg(1); + + expr * str1Ast = NULL; + expr * y = NULL; + expr * m = NULL; + expr * str2Ast = NULL; + + if (m_strutil.is_string(v1_arg0)) { + str1Ast = v1_arg0; + y = v1_arg1; + m = v2_arg0; + str2Ast = v2_arg1; + } else { + str1Ast = v2_arg0; + y = v2_arg1; + m = v1_arg0; + str2Ast = v1_arg1; + } + if (has_self_cut(m, y)) { + TRACE("t_str_detail", tout << "Possible overlap found" << std::endl; print_cut_var(m, tout); print_cut_var(y, tout);); + return true; + } else { + return false; + } + } + + TRACE("t_str_detail", tout << "warning: unrecognized concat case" << std::endl;); + return false; +} + /************************************************************* * Type 1: concat(x, y) = concat(m, n) * x, y, m and n all variables @@ -6629,7 +6803,33 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { } } if (hasCommon == 0) { - simplify_concat_equality(*(eqc_concat_lhs.begin()), *(eqc_concat_rhs.begin())); + if (opt_ConcatOverlapAvoid) { + bool found = false; + // check each pair and take the first ones that won't immediately overlap + for (itor1 = eqc_concat_lhs.begin(); itor1 != eqc_concat_lhs.end() && !found; ++itor1) { + expr * concat_lhs = *itor1; + for (itor2 = eqc_concat_rhs.begin(); itor2 != eqc_concat_rhs.end() && !found; ++itor2) { + expr * concat_rhs = *itor2; + if (will_result_in_overlap(concat_lhs, concat_rhs)) { + TRACE("t_str_detail", tout << "Concats " << mk_pp(concat_lhs, m) << " and " + << mk_pp(concat_rhs, m) << " will result in overlap; skipping." << std::endl;); + } else { + TRACE("t_str_detail", tout << "Concats " << mk_pp(concat_lhs, m) << " and " + << mk_pp(concat_rhs, m) << " won't overlap. Simplifying here." << std::endl;); + simplify_concat_equality(concat_lhs, concat_rhs); + found = true; + break; + } + } + } + if (!found) { + TRACE("t_str_detail", tout << "All pairs of concats expected to overlap, falling back." << std::endl;); + simplify_concat_equality(*(eqc_concat_lhs.begin()), *(eqc_concat_rhs.begin())); + } + } else { + // default behaviour + simplify_concat_equality(*(eqc_concat_lhs.begin()), *(eqc_concat_rhs.begin())); + } } } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 1f615cfc5..7f1e1dd9c 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -180,6 +180,14 @@ namespace smt { */ bool opt_CheckVariableScope; + /* + * If ConcatOverlapAvoid is set to true, + * the check to simplify Concat = Concat in handle_equality() will + * avoid simplifying wrt. pairs of Concat terms that will immediately + * result in an overlap. (false = Z3str2 behaviour) + */ + bool opt_ConcatOverlapAvoid; + bool search_started; arith_util m_autil; str_util m_strutil; @@ -350,6 +358,9 @@ namespace smt { void add_cut_info_merge(expr * destNode, int slevel, expr * srcNode); bool has_self_cut(expr * n1, expr * n2); + // for ConcatOverlapAvoid + bool will_result_in_overlap(expr * lhs, expr * rhs); + void track_variable_scope(expr * var); app * mk_str_var(std::string name); app * mk_int_var(std::string name); From f033a77faed636ed932ed2b6a811e3255cbca189 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 13 Jan 2017 12:57:48 -0500 Subject: [PATCH 310/562] modify theory-aware branching to manipulate activity instead of giving absolute priority --- src/smt/smt_case_split_queue.cpp | 137 +++++++++++++++++++++++++++---- src/smt/theory_str.cpp | 30 +++---- 2 files changed, 138 insertions(+), 29 deletions(-) diff --git a/src/smt/smt_case_split_queue.cpp b/src/smt/smt_case_split_queue.cpp index ebe9c2e4e..fa012525b 100644 --- a/src/smt/smt_case_split_queue.cpp +++ b/src/smt/smt_case_split_queue.cpp @@ -40,18 +40,20 @@ namespace smt { typedef heap bool_var_act_queue; struct theory_aware_act_lt { - // only take into account theory var priority for now + svector const & m_activity; theory_var_priority_map const & m_theory_var_priority; - theory_aware_act_lt(theory_var_priority_map const & a):m_theory_var_priority(a) {} + theory_aware_act_lt(svector const & act, theory_var_priority_map const & a):m_activity(act),m_theory_var_priority(a) {} bool operator()(bool_var v1, bool_var v2) const { double p_v1, p_v2; - // safety -- use a large negative number if some var isn't in the map if (!m_theory_var_priority.find(v1, p_v1)) { - p_v1 = -1000.0; - } + p_v1 = 0.0; + } if (!m_theory_var_priority.find(v2, p_v2)) { - p_v2 = -1000.0; + p_v2 = 0.0; } + // add clause activity + p_v1 += m_activity[v1]; + p_v2 += m_activity[v2]; return p_v1 > p_v2; } }; @@ -1109,7 +1111,8 @@ namespace smt { m_params.m_qi_eager_threshold += start_gen; } }; - + + /* class theory_aware_branching_queue : public case_split_queue { protected: context & m_context; @@ -1220,7 +1223,114 @@ namespace smt { m_base_queue->display(out); } }; + */ + class theory_aware_branching_queue : public case_split_queue { + protected: + context & m_context; + smt_params & m_params; + theory_var_priority_map m_theory_var_priority; + theory_aware_act_queue m_queue; + public: + theory_aware_branching_queue(context & ctx, smt_params & p): + m_context(ctx), + m_params(p), + m_theory_var_priority(), + m_queue(1024, theory_aware_act_lt(ctx.get_activity_vector(), m_theory_var_priority)) { + } + + virtual void activity_increased_eh(bool_var v) { + if (m_queue.contains(v)) + m_queue.decreased(v); + } + + virtual void mk_var_eh(bool_var v) { + m_queue.reserve(v+1); + m_queue.insert(v); + } + + virtual void del_var_eh(bool_var v) { + if (m_queue.contains(v)) + m_queue.erase(v); + } + + virtual void unassign_var_eh(bool_var v) { + if (!m_queue.contains(v)) + m_queue.insert(v); + } + + virtual void relevant_eh(expr * n) {} + + virtual void init_search_eh() {} + + virtual void end_search_eh() {} + + virtual void reset() { + m_queue.reset(); + } + + virtual void push_scope() {} + + virtual void pop_scope(unsigned num_scopes) {} + + virtual void next_case_split(bool_var & next, lbool & phase) { + phase = l_undef; + + if (m_context.get_random_value() < static_cast(m_params.m_random_var_freq * random_gen::max_value())) { + next = m_context.get_random_value() % m_context.get_num_b_internalized(); + TRACE("random_split", tout << "next: " << next << " get_assignment(next): " << m_context.get_assignment(next) << "\n";); + if (m_context.get_assignment(next) == l_undef) + return; + } + + while (!m_queue.empty()) { + next = m_queue.erase_min(); + if (m_context.get_assignment(next) == l_undef) + return; + } + + next = null_bool_var; + } + + virtual void add_theory_aware_branching_info(bool_var v, double priority, lbool phase) { + TRACE("theory_aware_branching", tout << "Add theory-aware branching information for l#" << v << ": priority=" << priority << std::endl;); + // m_theory_vars.insert(v); + // m_theory_var_phase.insert(v, phase); + m_theory_var_priority.insert(v, priority); + if (m_queue.contains(v)) { + if (priority > 0.0) { + m_queue.decreased(v); + } else { + m_queue.increased(v); + } + } + // m_theory_queue.reserve(v+1); + // m_theory_queue.insert(v); + } + + virtual void display(std::ostream & out) { + bool first = true; + bool_var_act_queue::const_iterator it = m_queue.begin(); + bool_var_act_queue::const_iterator end = m_queue.end(); + for (; it != end ; ++it) { + unsigned v = *it; + if (m_context.get_assignment(v) == l_undef) { + if (first) { + out << "remaining case-splits:\n"; + first = false; + } + out << "#" << m_context.bool_var2expr(v)->get_id() << " "; + } + } + if (!first) + out << "\n"; + + } + + virtual ~theory_aware_branching_queue() {}; + }; + + case_split_queue * mk_case_split_queue(context & ctx, smt_params & p) { if (p.m_relevancy_lvl < 2 && (p.m_case_split_strategy == CS_RELEVANCY || p.m_case_split_strategy == CS_RELEVANCY_ACTIVITY || p.m_case_split_strategy == CS_RELEVANCY_GOAL)) { @@ -1235,6 +1345,10 @@ namespace smt { case_split_queue * baseQueue; + if (p.m_theory_aware_branching) { + // override + baseQueue = alloc(theory_aware_branching_queue, ctx, p); + } else { switch (p.m_case_split_strategy) { case CS_ACTIVITY_DELAY_NEW: baseQueue = alloc(dact_case_split_queue, ctx, p); @@ -1255,14 +1369,9 @@ namespace smt { baseQueue = alloc(act_case_split_queue, ctx, p); break; } + } - if (p.m_theory_aware_branching) { - TRACE("theory_aware_branching", tout << "Allocating and returning theory-aware branching queue." << std::endl;); - case_split_queue * theory_aware_queue = alloc(theory_aware_branching_queue, ctx, p, baseQueue); - return theory_aware_queue; - } else { - return baseQueue; - } + return baseQueue; } }; diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 44d13d666..2936baf13 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3085,7 +3085,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { expr_ref option1(mk_and(and_item), mgr); arrangement_disjunction.push_back(option1); - add_theory_aware_branching_info(option1, 0.0, l_true); + add_theory_aware_branching_info(option1, 0.1, l_true); add_cut_info_merge(t1, ctx.get_scope_level(), m); add_cut_info_merge(t1, ctx.get_scope_level(), y); @@ -3122,7 +3122,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { expr_ref option2(mk_and(and_item), mgr); arrangement_disjunction.push_back(option2); - add_theory_aware_branching_info(option2, 0.0, l_true); + add_theory_aware_branching_info(option2, 0.1, l_true); add_cut_info_merge(t2, ctx.get_scope_level(), x); add_cut_info_merge(t2, ctx.get_scope_level(), n); @@ -3143,7 +3143,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { expr_ref option3(mk_and(and_item), mgr); arrangement_disjunction.push_back(option3); // prioritize this case, it is easier - add_theory_aware_branching_info(option3, 2.0, l_true); + add_theory_aware_branching_info(option3, 0.5, l_true); } if (!arrangement_disjunction.empty()) { @@ -3455,7 +3455,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { expr_ref option1(mk_and(and_item), mgr); arrangement_disjunction.push_back(option1); - add_theory_aware_branching_info(option1, 0.0, l_true); + add_theory_aware_branching_info(option1, 0.1, l_true); add_cut_info_merge(temp1, ctx.get_scope_level(), y); add_cut_info_merge(temp1, ctx.get_scope_level(), m); } else { @@ -3482,9 +3482,9 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { double priority; // prioritize the option where y is equal to the original string if (i == 0) { - priority = 2.0; + priority = 0.5; } else { - priority = 0.0; + priority = 0.1; } add_theory_aware_branching_info(option2, priority, l_true); } @@ -3787,9 +3787,9 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { arrangement_disjunction.push_back(option1); double priority; if (i == (int)strValue.size()) { - priority = 1.0; + priority = 0.5; } else { - priority = 0.0; + priority = 0.1; } add_theory_aware_branching_info(option1, priority, l_true); } @@ -3815,7 +3815,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { expr_ref option2(mk_and(and_item), mgr); arrangement_disjunction.push_back(option2); - add_theory_aware_branching_info(option2, 0.0, l_true); + add_theory_aware_branching_info(option2, 0.1, l_true); add_cut_info_merge(temp1, sLevel, x); add_cut_info_merge(temp1, sLevel, n); @@ -4217,7 +4217,7 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { expr_ref option1(mk_and(and_item), mgr); arrangement_disjunction.push_back(option1); - add_theory_aware_branching_info(option1, 0.0, l_true); + add_theory_aware_branching_info(option1, 0.1, l_true); } else { loopDetected = true; TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); @@ -4255,9 +4255,9 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { double priority; // prefer the option "str1" = x if (prefix == str1Value) { - priority = 1.0; + priority = 0.5; } else { - priority = 0.0; + priority = 0.1; } add_theory_aware_branching_info(option2, priority, l_true); } @@ -9333,10 +9333,10 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr double priority; // give high priority to small lengths if this is available if (i <= 5) { - priority = 3.0; + priority = 0.3; } else { // prioritize over "more" - priority = 0.5; + priority = 0.2; } add_theory_aware_branching_info(or_expr, priority, l_true); @@ -9356,7 +9356,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr expr_ref more_option(ctx.mk_eq_atom(indicator, mk_string("more")), m); orList.push_back(more_option); // decrease priority of this option - add_theory_aware_branching_info(more_option, -1.0, l_true); + add_theory_aware_branching_info(more_option, -0.1, l_true); if (m_params.m_AggressiveLengthTesting) { literal l = mk_eq(indicator, mk_string("more"), false); ctx.mark_as_relevant(l); From a9ec8666f0c2e310bfe581169538b59e9cb1748d Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 14 Jan 2017 14:43:57 -0500 Subject: [PATCH 311/562] add phase selection to theory-aware branching queue --- src/smt/smt_case_split_queue.cpp | 44 +++++++++++++++++++------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/src/smt/smt_case_split_queue.cpp b/src/smt/smt_case_split_queue.cpp index fa012525b..c7ef655f2 100644 --- a/src/smt/smt_case_split_queue.cpp +++ b/src/smt/smt_case_split_queue.cpp @@ -1231,14 +1231,17 @@ namespace smt { smt_params & m_params; theory_var_priority_map m_theory_var_priority; theory_aware_act_queue m_queue; + + int_hashtable > m_theory_vars; + map > m_theory_var_phase; public: theory_aware_branching_queue(context & ctx, smt_params & p): m_context(ctx), m_params(p), - m_theory_var_priority(), + m_theory_var_priority(), m_queue(1024, theory_aware_act_lt(ctx.get_activity_vector(), m_theory_var_priority)) { } - + virtual void activity_increased_eh(bool_var v) { if (m_queue.contains(v)) m_queue.decreased(v); @@ -1275,39 +1278,44 @@ namespace smt { virtual void next_case_split(bool_var & next, lbool & phase) { phase = l_undef; - + if (m_context.get_random_value() < static_cast(m_params.m_random_var_freq * random_gen::max_value())) { next = m_context.get_random_value() % m_context.get_num_b_internalized(); TRACE("random_split", tout << "next: " << next << " get_assignment(next): " << m_context.get_assignment(next) << "\n";); if (m_context.get_assignment(next) == l_undef) return; } - + while (!m_queue.empty()) { next = m_queue.erase_min(); if (m_context.get_assignment(next) == l_undef) return; } - + next = null_bool_var; + if (m_theory_vars.contains(next)) { + if (!m_theory_var_phase.find(next, phase)) { + phase = l_undef; + } + } } virtual void add_theory_aware_branching_info(bool_var v, double priority, lbool phase) { TRACE("theory_aware_branching", tout << "Add theory-aware branching information for l#" << v << ": priority=" << priority << std::endl;); - // m_theory_vars.insert(v); - // m_theory_var_phase.insert(v, phase); + m_theory_vars.insert(v); + m_theory_var_phase.insert(v, phase); m_theory_var_priority.insert(v, priority); - if (m_queue.contains(v)) { - if (priority > 0.0) { - m_queue.decreased(v); - } else { - m_queue.increased(v); - } - } + if (m_queue.contains(v)) { + if (priority > 0.0) { + m_queue.decreased(v); + } else { + m_queue.increased(v); + } + } // m_theory_queue.reserve(v+1); // m_theory_queue.insert(v); } - + virtual void display(std::ostream & out) { bool first = true; bool_var_act_queue::const_iterator it = m_queue.begin(); @@ -1330,15 +1338,15 @@ namespace smt { virtual ~theory_aware_branching_queue() {}; }; - + case_split_queue * mk_case_split_queue(context & ctx, smt_params & p) { if (p.m_relevancy_lvl < 2 && (p.m_case_split_strategy == CS_RELEVANCY || p.m_case_split_strategy == CS_RELEVANCY_ACTIVITY || - p.m_case_split_strategy == CS_RELEVANCY_GOAL)) { + p.m_case_split_strategy == CS_RELEVANCY_GOAL)) { warning_msg("relevancy must be enabled to use option CASE_SPLIT=3, 4 or 5"); p.m_case_split_strategy = CS_ACTIVITY; } if (p.m_auto_config && (p.m_case_split_strategy == CS_RELEVANCY || p.m_case_split_strategy == CS_RELEVANCY_ACTIVITY || - p.m_case_split_strategy == CS_RELEVANCY_GOAL)) { + p.m_case_split_strategy == CS_RELEVANCY_GOAL)) { warning_msg("auto configuration (option AUTO_CONFIG) must be disabled to use option CASE_SPLIT=3, 4 or 5"); p.m_case_split_strategy = CS_ACTIVITY; } From aa8bf2668f9942af6ef819e1a9f9af87a227c14e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 14 Jan 2017 15:28:58 -0500 Subject: [PATCH 312/562] scale theory-aware priority by bvar_inc --- src/smt/smt_case_split_queue.cpp | 19 ++++++++++++------- src/smt/smt_context.h | 1 + 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/smt/smt_case_split_queue.cpp b/src/smt/smt_case_split_queue.cpp index c7ef655f2..2bc3e32df 100644 --- a/src/smt/smt_case_split_queue.cpp +++ b/src/smt/smt_case_split_queue.cpp @@ -42,18 +42,23 @@ namespace smt { struct theory_aware_act_lt { svector const & m_activity; theory_var_priority_map const & m_theory_var_priority; - theory_aware_act_lt(svector const & act, theory_var_priority_map const & a):m_activity(act),m_theory_var_priority(a) {} + double const & m_bvar_inc; + theory_aware_act_lt(svector const & act, + theory_var_priority_map const & a, + double const & bvar_inc):m_activity(act),m_theory_var_priority(a),m_bvar_inc(bvar_inc) {} bool operator()(bool_var v1, bool_var v2) const { double p_v1, p_v2; if (!m_theory_var_priority.find(v1, p_v1)) { - p_v1 = 0.0; - } + p_v1 = 0.0; + } + p_v1 *= m_bvar_inc; if (!m_theory_var_priority.find(v2, p_v2)) { p_v2 = 0.0; } - // add clause activity - p_v1 += m_activity[v1]; - p_v2 += m_activity[v2]; + p_v2 *= m_bvar_inc; + // add clause activity + p_v1 += m_activity[v1]; + p_v2 += m_activity[v2]; return p_v1 > p_v2; } }; @@ -1239,7 +1244,7 @@ namespace smt { m_context(ctx), m_params(p), m_theory_var_priority(), - m_queue(1024, theory_aware_act_lt(ctx.get_activity_vector(), m_theory_var_priority)) { + m_queue(1024, theory_aware_act_lt(ctx.get_activity_vector(), m_theory_var_priority, ctx.get_bvar_inc())) { } virtual void activity_increased_eh(bool_var v) { diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index 2aae6c8a5..9a8e01b93 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -824,6 +824,7 @@ namespace smt { * or some other axiom that means at least one of them must be assigned 'true'. */ void mk_th_case_split(unsigned num_lits, literal * lits); + double get_bvar_inc() const { return m_bvar_inc; } /* * Provide a hint to the branching heuristic about the priority of a "theory-aware literal". From 0dfaa30ae8f2b143bf90959dea1ba222e923d2ad Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 16 Jan 2017 14:46:04 -0500 Subject: [PATCH 313/562] experimental z3str2 search order --- src/smt/theory_str.cpp | 22 ++++++++++++++++++++-- src/smt/theory_str.h | 16 +++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index fd379fd2d..13f2732d8 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -41,6 +41,7 @@ theory_str::theory_str(ast_manager & m, theory_str_params const & params): opt_DeferEQCConsistencyCheck(false), opt_CheckVariableScope(true), opt_ConcatOverlapAvoid(true), + opt_DeferredSearchOrder(true), /* Internal setup */ search_started(false), m_autil(m), @@ -899,7 +900,8 @@ bool theory_str::can_propagate() { || !m_axiom_Contains_todo.empty() || !m_axiom_Indexof_todo.empty() || !m_axiom_Indexof2_todo.empty() || !m_axiom_LastIndexof_todo.empty() || !m_axiom_Substr_todo.empty() || !m_axiom_Replace_todo.empty() || !m_axiom_RegexIn_todo.empty() || !m_library_aware_axiom_todo.empty() - || !m_delayed_axiom_setup_terms.empty(); + || !m_delayed_axiom_setup_terms.empty() + || (opt_DeferredSearchOrder && !m_new_eqs.empty()) ; } @@ -1000,6 +1002,14 @@ void theory_str::propagate() { set_up_axioms(m_delayed_axiom_setup_terms[i].get()); } m_delayed_axiom_setup_terms.reset(); + + if (opt_DeferredSearchOrder) { + for (unsigned i = 0; i < m_new_eqs.size(); ++i) { + var_pair & p = m_new_eqs[i]; + cb_new_eq(p.first, p.second); + } + m_new_eqs.reset(); + } } } @@ -7062,7 +7072,15 @@ void theory_str::init_search_eh() { search_started = true; } -void theory_str::new_eq_eh(theory_var x, theory_var y) { +void theory_str::new_eq_eh(theory_var v1, theory_var v2) { + if (opt_DeferredSearchOrder) { + m_new_eqs.push_back(var_pair(v1,v2)); + } else { + cb_new_eq(v1, v2); + } +} + +void theory_str::cb_new_eq(theory_var x, theory_var y) { //TRACE("t_str_detail", tout << "new eq: v#" << x << " = v#" << y << std::endl;); TRACE("t_str", tout << "new eq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " = " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 7f1e1dd9c..598e9d8c9 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -97,6 +97,8 @@ namespace smt { } }; + typedef std::pair var_pair; + class theory_str : public theory { struct T_cut { @@ -188,6 +190,16 @@ namespace smt { */ bool opt_ConcatOverlapAvoid; + /* + * If DeferredSearchOrder is set to true, + * certain behaviours from user_smt_theory will be emulated in order to + * reproduce more faithfully the search order used by Z3str2. + * In particular, new equalities will be saved and processed during propagate(), + * and asserted axioms will be deferred until the end of each propagate() step. + */ + bool opt_DeferredSearchOrder; + svector m_new_eqs; + bool search_started; arith_util m_autil; str_util m_strutil; @@ -585,9 +597,11 @@ namespace smt { void get_unique_non_concat_nodes(expr * node, std::set & argSet); bool propagate_length_within_eqc(expr * var); - // TESTING void refresh_theory_var(expr * e); + // user_smt_theory search order emulation + void cb_new_eq(theory_var v1, theory_var v2); + public: theory_str(ast_manager & m, theory_str_params const & params); virtual ~theory_str(); From 4b6582b8f35f5c5c650cd855d6774a1c0a4463c3 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 16 Jan 2017 15:46:17 -0500 Subject: [PATCH 314/562] Revert "experimental z3str2 search order" This reverts commit 0dfaa30ae8f2b143bf90959dea1ba222e923d2ad. --- src/smt/theory_str.cpp | 22 ++-------------------- src/smt/theory_str.h | 16 +--------------- 2 files changed, 3 insertions(+), 35 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 13f2732d8..fd379fd2d 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -41,7 +41,6 @@ theory_str::theory_str(ast_manager & m, theory_str_params const & params): opt_DeferEQCConsistencyCheck(false), opt_CheckVariableScope(true), opt_ConcatOverlapAvoid(true), - opt_DeferredSearchOrder(true), /* Internal setup */ search_started(false), m_autil(m), @@ -900,8 +899,7 @@ bool theory_str::can_propagate() { || !m_axiom_Contains_todo.empty() || !m_axiom_Indexof_todo.empty() || !m_axiom_Indexof2_todo.empty() || !m_axiom_LastIndexof_todo.empty() || !m_axiom_Substr_todo.empty() || !m_axiom_Replace_todo.empty() || !m_axiom_RegexIn_todo.empty() || !m_library_aware_axiom_todo.empty() - || !m_delayed_axiom_setup_terms.empty() - || (opt_DeferredSearchOrder && !m_new_eqs.empty()) + || !m_delayed_axiom_setup_terms.empty(); ; } @@ -1002,14 +1000,6 @@ void theory_str::propagate() { set_up_axioms(m_delayed_axiom_setup_terms[i].get()); } m_delayed_axiom_setup_terms.reset(); - - if (opt_DeferredSearchOrder) { - for (unsigned i = 0; i < m_new_eqs.size(); ++i) { - var_pair & p = m_new_eqs[i]; - cb_new_eq(p.first, p.second); - } - m_new_eqs.reset(); - } } } @@ -7072,15 +7062,7 @@ void theory_str::init_search_eh() { search_started = true; } -void theory_str::new_eq_eh(theory_var v1, theory_var v2) { - if (opt_DeferredSearchOrder) { - m_new_eqs.push_back(var_pair(v1,v2)); - } else { - cb_new_eq(v1, v2); - } -} - -void theory_str::cb_new_eq(theory_var x, theory_var y) { +void theory_str::new_eq_eh(theory_var x, theory_var y) { //TRACE("t_str_detail", tout << "new eq: v#" << x << " = v#" << y << std::endl;); TRACE("t_str", tout << "new eq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " = " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 598e9d8c9..7f1e1dd9c 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -97,8 +97,6 @@ namespace smt { } }; - typedef std::pair var_pair; - class theory_str : public theory { struct T_cut { @@ -190,16 +188,6 @@ namespace smt { */ bool opt_ConcatOverlapAvoid; - /* - * If DeferredSearchOrder is set to true, - * certain behaviours from user_smt_theory will be emulated in order to - * reproduce more faithfully the search order used by Z3str2. - * In particular, new equalities will be saved and processed during propagate(), - * and asserted axioms will be deferred until the end of each propagate() step. - */ - bool opt_DeferredSearchOrder; - svector m_new_eqs; - bool search_started; arith_util m_autil; str_util m_strutil; @@ -597,11 +585,9 @@ namespace smt { void get_unique_non_concat_nodes(expr * node, std::set & argSet); bool propagate_length_within_eqc(expr * var); + // TESTING void refresh_theory_var(expr * e); - // user_smt_theory search order emulation - void cb_new_eq(theory_var v1, theory_var v2); - public: theory_str(ast_manager & m, theory_str_params const & params); virtual ~theory_str(); From 4e2847dea4e84f2ab4309b3de28309ca8eda41ce Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 16 Jan 2017 15:46:28 -0500 Subject: [PATCH 315/562] Revert "scale theory-aware priority by bvar_inc" This reverts commit aa8bf2668f9942af6ef819e1a9f9af87a227c14e. --- src/smt/smt_case_split_queue.cpp | 19 +++++++------------ src/smt/smt_context.h | 1 - 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/smt/smt_case_split_queue.cpp b/src/smt/smt_case_split_queue.cpp index 2bc3e32df..c7ef655f2 100644 --- a/src/smt/smt_case_split_queue.cpp +++ b/src/smt/smt_case_split_queue.cpp @@ -42,23 +42,18 @@ namespace smt { struct theory_aware_act_lt { svector const & m_activity; theory_var_priority_map const & m_theory_var_priority; - double const & m_bvar_inc; - theory_aware_act_lt(svector const & act, - theory_var_priority_map const & a, - double const & bvar_inc):m_activity(act),m_theory_var_priority(a),m_bvar_inc(bvar_inc) {} + theory_aware_act_lt(svector const & act, theory_var_priority_map const & a):m_activity(act),m_theory_var_priority(a) {} bool operator()(bool_var v1, bool_var v2) const { double p_v1, p_v2; if (!m_theory_var_priority.find(v1, p_v1)) { - p_v1 = 0.0; - } - p_v1 *= m_bvar_inc; + p_v1 = 0.0; + } if (!m_theory_var_priority.find(v2, p_v2)) { p_v2 = 0.0; } - p_v2 *= m_bvar_inc; - // add clause activity - p_v1 += m_activity[v1]; - p_v2 += m_activity[v2]; + // add clause activity + p_v1 += m_activity[v1]; + p_v2 += m_activity[v2]; return p_v1 > p_v2; } }; @@ -1244,7 +1239,7 @@ namespace smt { m_context(ctx), m_params(p), m_theory_var_priority(), - m_queue(1024, theory_aware_act_lt(ctx.get_activity_vector(), m_theory_var_priority, ctx.get_bvar_inc())) { + m_queue(1024, theory_aware_act_lt(ctx.get_activity_vector(), m_theory_var_priority)) { } virtual void activity_increased_eh(bool_var v) { diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index 9a8e01b93..2aae6c8a5 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -824,7 +824,6 @@ namespace smt { * or some other axiom that means at least one of them must be assigned 'true'. */ void mk_th_case_split(unsigned num_lits, literal * lits); - double get_bvar_inc() const { return m_bvar_inc; } /* * Provide a hint to the branching heuristic about the priority of a "theory-aware literal". From e459617c39b8ef171586f50748126de49dc53f85 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 16 Jan 2017 18:04:03 -0500 Subject: [PATCH 316/562] experimental finite model finding WIP, first successful run --- src/smt/params/smt_params_helper.pyg | 3 +- src/smt/params/theory_str_params.cpp | 1 + src/smt/params/theory_str_params.h | 8 ++ src/smt/theory_str.cpp | 160 ++++++++++++++++++++++++++- src/smt/theory_str.h | 6 + 5 files changed, 175 insertions(+), 3 deletions(-) diff --git a/src/smt/params/smt_params_helper.pyg b/src/smt/params/smt_params_helper.pyg index 8e8e52987..e23915ab4 100644 --- a/src/smt/params/smt_params_helper.pyg +++ b/src/smt/params/smt_params_helper.pyg @@ -72,5 +72,6 @@ def_module_params(module_name='smt', ('str.use_binary_search', BOOL, False, 'use a binary search heuristic for finding concrete length values for free variables in theory_str (set to False to use linear search)'), ('str.binary_search_start', UINT, 64, 'initial upper bound for theory_str binary search'), ('theory_case_split', BOOL, False, 'Allow the context to use heuristics involving theory case splits, which are a set of literals of which exactly one can be assigned True. If this option is false, the context will generate extra axioms to enforce this instead.'), - ('theory_aware_branching', BOOL, False, 'Allow the context to use extra information from theory solvers regarding literal branching prioritization.') + ('theory_aware_branching', BOOL, False, 'Allow the context to use extra information from theory solvers regarding literal branching prioritization.'), + ('str.finite_overlap_models', BOOL, False, 'attempt a finite model search for overlapping variables instead of completely giving up on the arrangement') )) diff --git a/src/smt/params/theory_str_params.cpp b/src/smt/params/theory_str_params.cpp index 2e98a4394..46302cf82 100644 --- a/src/smt/params/theory_str_params.cpp +++ b/src/smt/params/theory_str_params.cpp @@ -27,6 +27,7 @@ void theory_str_params::updt_params(params_ref const & _p) { m_UseFastLengthTesterCache = p.str_fast_length_tester_cache(); m_UseFastValueTesterCache = p.str_fast_value_tester_cache(); m_StringConstantCache = p.str_string_constant_cache(); + m_FiniteOverlapModels = p.str_finite_overlap_models(); m_UseBinarySearch = p.str_use_binary_search(); m_BinarySearchInitialUpperBound = p.str_binary_search_start(); } diff --git a/src/smt/params/theory_str_params.h b/src/smt/params/theory_str_params.h index 39c553780..4effb0897 100644 --- a/src/smt/params/theory_str_params.h +++ b/src/smt/params/theory_str_params.h @@ -68,6 +68,13 @@ struct theory_str_params { */ bool m_StringConstantCache; + /* + * If FiniteOverlapModels is set to true, + * arrangements that result in overlapping variables will generate a small number of models + * to test instead of completely giving up on the case. + */ + bool m_FiniteOverlapModels; + bool m_UseBinarySearch; unsigned m_BinarySearchInitialUpperBound; @@ -79,6 +86,7 @@ struct theory_str_params { m_UseFastLengthTesterCache(false), m_UseFastValueTesterCache(true), m_StringConstantCache(true), + m_FiniteOverlapModels(false), m_UseBinarySearch(false), m_BinarySearchInitialUpperBound(64) { diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index fd379fd2d..4ff80a613 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4394,8 +4394,47 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { add_theory_aware_branching_info(option1, 0.1, l_true); } else { loopDetected = true; - TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); - TRACE("t_str", print_cut_var(m, tout); print_cut_var(y, tout);); + + if (m_params.m_FiniteOverlapModels) { + // TODO refactor this entire segment into its own method. this is really just for experiment purposes + TRACE("t_str", tout << "activating finite model testing for overlapping concats " + << mk_pp(concatAst1, mgr) << " and " << mk_pp(concatAst2, mgr) << std::endl;); + std::map concatMap; + std::map unrollMap; + std::map varMap; + classify_ast_by_type(concatAst1, varMap, concatMap, unrollMap); + classify_ast_by_type(concatAst2, varMap, concatMap, unrollMap); + TRACE("t_str_detail", tout << "found vars:"; + for (std::map::iterator it = varMap.begin(); it != varMap.end(); ++it) { + tout << " " << mk_pp(it->first, mgr); + } + tout << std::endl; + ); + + expr_ref testvar(mk_str_var("finiteModelTest"), mgr); + m_trail.push_back(testvar); + ptr_vector varlist; + + for (std::map::iterator it = varMap.begin(); it != varMap.end(); ++it) { + expr * v = it->first; + varlist.push_back(v); + } + + // make things easy for the core wrt. testvar + expr_ref t1(ctx.mk_eq_atom(testvar, m_strutil.mk_string("")), mgr); + expr_ref t_yes(ctx.mk_eq_atom(testvar, m_strutil.mk_string("yes")), mgr); + expr_ref testvaraxiom(mgr.mk_or(t1, t_yes), mgr); + assert_axiom(testvaraxiom); + + finite_model_test_varlists.insert(testvar, varlist); + m_trail_stack.push(insert_obj_map >(finite_model_test_varlists, testvar) ); + + arrangement_disjunction.push_back(t_yes); + add_theory_aware_branching_info(t_yes, -0.1, l_true); + } else { + TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); + TRACE("t_str", print_cut_var(m, tout); print_cut_var(y, tout);); + } } for (std::list::iterator itor = overlapLen.begin(); itor != overlapLen.end(); itor++) { @@ -6564,6 +6603,114 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } } +void theory_str::finite_model_test(expr * testvar, expr * str) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + if (!m_strutil.is_string(str)) return; + std::string s = m_strutil.get_string_constant_value(str); + if (s == "yes") { + TRACE("t_str", tout << "start finite model test for " << mk_pp(testvar, m) << std::endl;); + ptr_vector & vars = finite_model_test_varlists[testvar]; + for (ptr_vector::iterator it = vars.begin(); it != vars.end(); ++it) { + expr * v = *it; + // check for any sort of existing length tester we might interfere with + if (m_params.m_UseBinarySearch) { + NOT_IMPLEMENTED_YET(); + } else { + bool map_effectively_empty = false; + if (fvar_len_count_map.find(v) == fvar_len_count_map.end()) { + map_effectively_empty = true; + } + + if (!map_effectively_empty) { + map_effectively_empty = true; + ptr_vector indicator_set = fvar_lenTester_map[v]; + for (ptr_vector::iterator it = indicator_set.begin(); it != indicator_set.end(); ++it) { + expr * indicator = *it; + if (internal_variable_set.find(indicator) != internal_variable_set.end()) { + map_effectively_empty = false; + break; + } + } + } + + if (map_effectively_empty) { + TRACE("t_str_detail", tout << "no existing length testers for " << mk_pp(v, m) << std::endl;); + rational v_len; + if (get_len_value(v, v_len)) { + TRACE("t_str_detail", tout << "length = " << v_len.to_string() << std::endl;); + } else { + expr_ref vLengthExpr(mk_strlen(v), m); + + rational v_lower_bound; + bool lower_bound_exists = lower_bound(vLengthExpr, v_lower_bound); + rational v_upper_bound; + bool upper_bound_exists = upper_bound(vLengthExpr, v_upper_bound); + TRACE("t_str_detail", tout << "bounds = [" << (lower_bound_exists?v_lower_bound.to_string():"?") + << ".." << (upper_bound_exists?v_upper_bound.to_string():"?") << "]" << std::endl;); + + // make sure the bounds are non-negative + if (lower_bound_exists && v_lower_bound.is_neg()) { + v_lower_bound = rational::zero(); + } + if (upper_bound_exists && v_upper_bound.is_neg()) { + v_upper_bound = rational::zero(); + } + + if (lower_bound_exists && upper_bound_exists) { + // easiest case. we will search within these bounds + } else if (upper_bound_exists && !lower_bound_exists) { + // search between 0 and the upper bound + v_lower_bound == rational::zero(); + } else if (lower_bound_exists && !upper_bound_exists) { + // check some finite portion of the search space + // TODO here and below, factor out the increment to a param + v_upper_bound = v_lower_bound + rational(10); + } else { + // no bounds information + v_lower_bound = rational::zero(); + v_upper_bound = v_lower_bound + rational(10); + } + // now create a fake length tester over this finite disjunction of lengths + + fvar_len_count_map[v] = 1; + unsigned int testNum = fvar_len_count_map[v]; + + expr_ref indicator(mk_internal_lenTest_var(v, testNum), m); + SASSERT(indicator); + m_trail.push_back(indicator); + + fvar_lenTester_map[v].shrink(0); + fvar_lenTester_map[v].push_back(indicator); + lenTester_fvar_map[indicator] = v; + + expr_ref_vector orList(m); + expr_ref_vector andList(m); + + for (rational l = v_lower_bound; l <= v_upper_bound; l += rational::one()) { + // TODO integrate with the enhancements in gen_len_test_options() + std::string lStr = l.to_string(); + expr_ref str_indicator(m_strutil.mk_string(lStr), m); + expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); + orList.push_back(or_expr); + expr_ref and_expr(ctx.mk_eq_atom(or_expr, ctx.mk_eq_atom(vLengthExpr, m_autil.mk_numeral(l, true))), m); + andList.push_back(and_expr); + } + andList.push_back(mk_or(orList)); + expr_ref implLhs(ctx.mk_eq_atom(testvar, str), m); + expr_ref implRhs(mk_and(andList), m); + assert_implication(implLhs, implRhs); + } + } else { + // TODO figure out this case + NOT_IMPLEMENTED_YET(); + } + } + } // foreach (v in vars) + } // (s == "yes") +} + void theory_str::more_len_tests(expr * lenTester, std::string lenTesterValue) { ast_manager & m = get_manager(); if (lenTester_fvar_map.find(lenTester) != lenTester_fvar_map.end()) { @@ -6666,6 +6813,15 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { return; } + if (m_params.m_FiniteOverlapModels && !finite_model_test_varlists.empty()) { + // TODO NEXT + if (finite_model_test_varlists.contains(lhs)) { + finite_model_test(lhs, rhs); return; + } else if (finite_model_test_varlists.contains(rhs)) { + finite_model_test(rhs, lhs); return; + } + } + if (free_var_attempt(lhs, rhs) || free_var_attempt(rhs, lhs)) { return; } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 7f1e1dd9c..050593691 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -335,6 +335,10 @@ namespace smt { // maps a length tester to the next length tester to be (re)used if the split is "high" obj_map binary_search_next_var_high; + // finite model finding data + // maps a finite model tester var to a list of variables that will be tested + obj_map > finite_model_test_varlists; + protected: void assert_axiom(expr * e); void assert_implication(expr * premise, expr * conclusion); @@ -588,6 +592,8 @@ namespace smt { // TESTING void refresh_theory_var(expr * e); + void finite_model_test(expr * v, expr * c); + public: theory_str(ast_manager & m, theory_str_params const & params); virtual ~theory_str(); From 0af834421faa801faa1010b20e085cde4fc4780e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 16 Jan 2017 18:24:47 -0500 Subject: [PATCH 317/562] finite model finding for other concat cases in theory_str --- src/smt/theory_str.cpp | 378 ++++++++++++++++++++++++----------------- src/smt/theory_str.h | 1 + 2 files changed, 220 insertions(+), 159 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 4ff80a613..5313b08ce 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3126,33 +3126,33 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { // Type 0: M cuts Y. // len(x) < len(m) || len(y) > len(n) //-------------------------------------- + expr_ref_vector ax_l_items(mgr); + expr_ref_vector ax_r_items(mgr); + + ax_l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); + + expr_ref x_t1(mk_concat(x, t1), mgr); + expr_ref t1_n(mk_concat(t1, n), mgr); + + ax_r_items.push_back(ctx.mk_eq_atom(m, x_t1)); + ax_r_items.push_back(ctx.mk_eq_atom(y, t1_n)); + + if (m_len_exists && x_len_exists) { + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); + rational m_sub_x = m_len - x_len; + ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t1), mk_int(m_sub_x))); + } else { + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len))); + rational y_sub_n = y_len - n_len; + ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t1), mk_int(y_sub_n))); + } + + expr_ref ax_l(mk_and(ax_l_items), mgr); + expr_ref ax_r(mk_and(ax_r_items), mgr); + if (!has_self_cut(m, y)) { - expr_ref_vector ax_l_items(mgr); - expr_ref_vector ax_r_items(mgr); - - ax_l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); - - expr_ref x_t1(mk_concat(x, t1), mgr); - expr_ref t1_n(mk_concat(t1, n), mgr); - - ax_r_items.push_back(ctx.mk_eq_atom(m, x_t1)); - ax_r_items.push_back(ctx.mk_eq_atom(y, t1_n)); - - if (m_len_exists && x_len_exists) { - ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); - ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); - rational m_sub_x = m_len - x_len; - ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t1), mk_int(m_sub_x))); - } else { - ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); - ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len))); - rational y_sub_n = y_len - n_len; - ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t1), mk_int(y_sub_n))); - } - - expr_ref ax_l(mk_and(ax_l_items), mgr); - expr_ref ax_r(mk_and(ax_r_items), mgr); - // Cut Info add_cut_info_merge(t1, sLevel, m); add_cut_info_merge(t1, sLevel, y); @@ -3165,8 +3165,14 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } } else { loopDetected = true; - TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - // TODO printCutVar(m, y); + if (m_params.m_FiniteOverlapModels) { + expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); + assert_implication(ax_l, tester); + add_theory_aware_branching_info(tester, -0.1, l_true); + } else { + TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + // TODO printCutVar(m, y); + } } } else if (splitType == 1) { // Type 1: @@ -3179,32 +3185,32 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } else if (splitType == 2) { // Type 2: X cuts N. // len(x) > len(m) || len(y) < len(n) + expr_ref m_t2(mk_concat(m, t2), mgr); + expr_ref t2_y(mk_concat(t2, y), mgr); + + expr_ref_vector ax_l_items(mgr); + ax_l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); + + expr_ref_vector ax_r_items(mgr); + ax_r_items.push_back(ctx.mk_eq_atom(x, m_t2)); + ax_r_items.push_back(ctx.mk_eq_atom(t2_y, n)); + + if (m_len_exists && x_len_exists) { + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); + rational x_sub_m = x_len - m_len; + ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t2), mk_int(x_sub_m))); + } else { + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len))); + rational n_sub_y = n_len - y_len; + ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t2), mk_int(n_sub_y))); + } + + expr_ref ax_l(mk_and(ax_l_items), mgr); + expr_ref ax_r(mk_and(ax_r_items), mgr); + if (!has_self_cut(x, n)) { - expr_ref m_t2(mk_concat(m, t2), mgr); - expr_ref t2_y(mk_concat(t2, y), mgr); - - expr_ref_vector ax_l_items(mgr); - ax_l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); - - expr_ref_vector ax_r_items(mgr); - ax_r_items.push_back(ctx.mk_eq_atom(x, m_t2)); - ax_r_items.push_back(ctx.mk_eq_atom(t2_y, n)); - - if (m_len_exists && x_len_exists) { - ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); - ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); - rational x_sub_m = x_len - m_len; - ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t2), mk_int(x_sub_m))); - } else { - ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); - ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len))); - rational n_sub_y = n_len - y_len; - ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t2), mk_int(n_sub_y))); - } - - expr_ref ax_l(mk_and(ax_l_items), mgr); - expr_ref ax_r(mk_and(ax_r_items), mgr); - // Cut Info add_cut_info_merge(t2, sLevel, x); add_cut_info_merge(t2, sLevel, n); @@ -3217,8 +3223,14 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } } else { loopDetected = true; - TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - // TODO printCutVar(m, y); + if (m_params.m_FiniteOverlapModels) { + expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); + assert_implication(ax_l, tester); + add_theory_aware_branching_info(tester, -0.1, l_true); + } else { + TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + // TODO printCutVar(m, y); + } } } else if (splitType == -1) { // Here we don't really have a choice. We have no length information at all... @@ -3265,12 +3277,19 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { add_cut_info_merge(t1, ctx.get_scope_level(), y); } else { loopDetected = true; - TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); + if (m_params.m_FiniteOverlapModels) { + expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); + arrangement_disjunction.push_back(tester); + add_theory_aware_branching_info(tester, -0.1, l_true); + } else { + TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); + } } // break option 2: - // x = m || y = n + // x = m . t2 + // n = t2 . y if (!avoidLoopCut || !has_self_cut(x, n)) { expr_ref_vector and_item(mgr); // break down option 1-2 @@ -3302,10 +3321,19 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { add_cut_info_merge(t2, ctx.get_scope_level(), n); } else { loopDetected = true; - TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - // TODO printCutVar(x, n); + if (m_params.m_FiniteOverlapModels) { + // TODO this might repeat the case above, we may wish to avoid doing this twice + expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); + arrangement_disjunction.push_back(tester); + add_theory_aware_branching_info(tester, -0.1, l_true); + } else { + TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + // TODO printCutVar(x, n); + } } + // option 3: + // x = m, y = n if (can_two_nodes_eq(x, m) && can_two_nodes_eq(y, n)) { expr_ref_vector and_item(mgr); @@ -3496,31 +3524,31 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { // | m | str | expr_ref temp1_strAst(mk_concat(temp1, strAst), mgr); if (can_two_nodes_eq(y, temp1_strAst)) { + expr_ref_vector l_items(mgr); + l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); + + expr_ref_vector r_items(mgr); + expr_ref x_temp1(mk_concat(x, temp1), mgr); + r_items.push_back(ctx.mk_eq_atom(m, x_temp1)); + r_items.push_back(ctx.mk_eq_atom(y, temp1_strAst)); + + if (x_len_exists && m_len_exists) { + l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); + l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); + rational m_sub_x = (m_len - x_len); + r_items.push_back(ctx.mk_eq_atom(mk_strlen(temp1), mk_int(m_sub_x))); + } else { + l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); + l_items.push_back(ctx.mk_eq_atom(mk_strlen(strAst), mk_int(str_len))); + rational y_sub_str = (y_len - str_len); + r_items.push_back(ctx.mk_eq_atom(mk_strlen(temp1), mk_int(y_sub_str))); + } + + expr_ref ax_l(mk_and(l_items), mgr); + expr_ref ax_r(mk_and(r_items), mgr); + if (!avoidLoopCut || !(has_self_cut(m, y))) { // break down option 2-1 - expr_ref_vector l_items(mgr); - l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); - - expr_ref_vector r_items(mgr); - expr_ref x_temp1(mk_concat(x, temp1), mgr); - r_items.push_back(ctx.mk_eq_atom(m, x_temp1)); - r_items.push_back(ctx.mk_eq_atom(y, temp1_strAst)); - - if (x_len_exists && m_len_exists) { - l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); - l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); - rational m_sub_x = (m_len - x_len); - r_items.push_back(ctx.mk_eq_atom(mk_strlen(temp1), mk_int(m_sub_x))); - } else { - l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); - l_items.push_back(ctx.mk_eq_atom(mk_strlen(strAst), mk_int(str_len))); - rational y_sub_str = (y_len - str_len); - r_items.push_back(ctx.mk_eq_atom(mk_strlen(temp1), mk_int(y_sub_str))); - } - - expr_ref ax_l(mk_and(l_items), mgr); - expr_ref ax_r(mk_and(r_items), mgr); - add_cut_info_merge(temp1, sLevel, y); add_cut_info_merge(temp1, sLevel, m); @@ -3532,8 +3560,15 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { } } else { loopDetected = true; - TRACE("t_str", tout << "AVOID LOOP: SKIP" << std::endl;); - // TODO printCutVar(m, y); + + if (m_params.m_FiniteOverlapModels) { + expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); + assert_implication(ax_l, tester); + add_theory_aware_branching_info(tester, -0.1, l_true); + } else { + TRACE("t_str", tout << "AVOID LOOP: SKIP" << std::endl;); + // TODO printCutVar(m, y); + } } } } else if (splitType == 1) { @@ -3634,8 +3669,14 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { add_cut_info_merge(temp1, ctx.get_scope_level(), m); } else { loopDetected = true; - TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - // TODO printCutVar(m, y) + if (m_params.m_FiniteOverlapModels) { + expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); + arrangement_disjunction.push_back(tester); + add_theory_aware_branching_info(tester, -0.1, l_true); + } else { + TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + // TODO printCutVar(m, y) + } } } @@ -3921,8 +3962,14 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { } } else { loopDetected = true; - TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - // TODO printCutVar(x, n); + if (m_params.m_FiniteOverlapModels) { + expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); + assert_implication(ax_l, tester); + add_theory_aware_branching_info(tester, -0.1, l_true); + } else { + TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + // TODO printCutVar(x, n); + } } } // else { @@ -3995,8 +4042,14 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { add_cut_info_merge(temp1, sLevel, n); } else { loopDetected = true; - TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); - // TODO printCutVAR(x, n) + if (m_params.m_FiniteOverlapModels) { + expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); + arrangement_disjunction.push_back(tester); + add_theory_aware_branching_info(tester, -0.1, l_true); + } else { + TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); + // TODO printCutVAR(x, n) + } } } @@ -4396,41 +4449,9 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { loopDetected = true; if (m_params.m_FiniteOverlapModels) { - // TODO refactor this entire segment into its own method. this is really just for experiment purposes - TRACE("t_str", tout << "activating finite model testing for overlapping concats " - << mk_pp(concatAst1, mgr) << " and " << mk_pp(concatAst2, mgr) << std::endl;); - std::map concatMap; - std::map unrollMap; - std::map varMap; - classify_ast_by_type(concatAst1, varMap, concatMap, unrollMap); - classify_ast_by_type(concatAst2, varMap, concatMap, unrollMap); - TRACE("t_str_detail", tout << "found vars:"; - for (std::map::iterator it = varMap.begin(); it != varMap.end(); ++it) { - tout << " " << mk_pp(it->first, mgr); - } - tout << std::endl; - ); - - expr_ref testvar(mk_str_var("finiteModelTest"), mgr); - m_trail.push_back(testvar); - ptr_vector varlist; - - for (std::map::iterator it = varMap.begin(); it != varMap.end(); ++it) { - expr * v = it->first; - varlist.push_back(v); - } - - // make things easy for the core wrt. testvar - expr_ref t1(ctx.mk_eq_atom(testvar, m_strutil.mk_string("")), mgr); - expr_ref t_yes(ctx.mk_eq_atom(testvar, m_strutil.mk_string("yes")), mgr); - expr_ref testvaraxiom(mgr.mk_or(t1, t_yes), mgr); - assert_axiom(testvaraxiom); - - finite_model_test_varlists.insert(testvar, varlist); - m_trail_stack.push(insert_obj_map >(finite_model_test_varlists, testvar) ); - - arrangement_disjunction.push_back(t_yes); - add_theory_aware_branching_info(t_yes, -0.1, l_true); + expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); + arrangement_disjunction.push_back(tester); + add_theory_aware_branching_info(tester, -0.1, l_true); } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); TRACE("t_str", print_cut_var(m, tout); print_cut_var(y, tout);); @@ -6603,6 +6624,44 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } } +expr_ref theory_str::set_up_finite_model_test(expr * lhs, expr * rhs) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + TRACE("t_str", tout << "activating finite model testing for overlapping concats " + << mk_pp(lhs, m) << " and " << mk_pp(rhs, m) << std::endl;); + std::map concatMap; + std::map unrollMap; + std::map varMap; + classify_ast_by_type(lhs, varMap, concatMap, unrollMap); + classify_ast_by_type(rhs, varMap, concatMap, unrollMap); + TRACE("t_str_detail", tout << "found vars:"; + for (std::map::iterator it = varMap.begin(); it != varMap.end(); ++it) { + tout << " " << mk_pp(it->first, m); + } + tout << std::endl; + ); + + expr_ref testvar(mk_str_var("finiteModelTest"), m); + m_trail.push_back(testvar); + ptr_vector varlist; + + for (std::map::iterator it = varMap.begin(); it != varMap.end(); ++it) { + expr * v = it->first; + varlist.push_back(v); + } + + // make things easy for the core wrt. testvar + expr_ref t1(ctx.mk_eq_atom(testvar, m_strutil.mk_string("")), m); + expr_ref t_yes(ctx.mk_eq_atom(testvar, m_strutil.mk_string("yes")), m); + expr_ref testvaraxiom(m.mk_or(t1, t_yes), m); + assert_axiom(testvaraxiom); + + finite_model_test_varlists.insert(testvar, varlist); + m_trail_stack.push(insert_obj_map >(finite_model_test_varlists, testvar) ); + return t_yes; +} + void theory_str::finite_model_test(expr * testvar, expr * str) { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -6638,14 +6697,15 @@ void theory_str::finite_model_test(expr * testvar, expr * str) { if (map_effectively_empty) { TRACE("t_str_detail", tout << "no existing length testers for " << mk_pp(v, m) << std::endl;); rational v_len; + rational v_lower_bound; + rational v_upper_bound; + expr_ref vLengthExpr(mk_strlen(v), m); if (get_len_value(v, v_len)) { TRACE("t_str_detail", tout << "length = " << v_len.to_string() << std::endl;); + v_lower_bound = v_len; + v_upper_bound = v_len; } else { - expr_ref vLengthExpr(mk_strlen(v), m); - - rational v_lower_bound; bool lower_bound_exists = lower_bound(vLengthExpr, v_lower_bound); - rational v_upper_bound; bool upper_bound_exists = upper_bound(vLengthExpr, v_upper_bound); TRACE("t_str_detail", tout << "bounds = [" << (lower_bound_exists?v_lower_bound.to_string():"?") << ".." << (upper_bound_exists?v_upper_bound.to_string():"?") << "]" << std::endl;); @@ -6672,36 +6732,36 @@ void theory_str::finite_model_test(expr * testvar, expr * str) { v_lower_bound = rational::zero(); v_upper_bound = v_lower_bound + rational(10); } - // now create a fake length tester over this finite disjunction of lengths - - fvar_len_count_map[v] = 1; - unsigned int testNum = fvar_len_count_map[v]; - - expr_ref indicator(mk_internal_lenTest_var(v, testNum), m); - SASSERT(indicator); - m_trail.push_back(indicator); - - fvar_lenTester_map[v].shrink(0); - fvar_lenTester_map[v].push_back(indicator); - lenTester_fvar_map[indicator] = v; - - expr_ref_vector orList(m); - expr_ref_vector andList(m); - - for (rational l = v_lower_bound; l <= v_upper_bound; l += rational::one()) { - // TODO integrate with the enhancements in gen_len_test_options() - std::string lStr = l.to_string(); - expr_ref str_indicator(m_strutil.mk_string(lStr), m); - expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); - orList.push_back(or_expr); - expr_ref and_expr(ctx.mk_eq_atom(or_expr, ctx.mk_eq_atom(vLengthExpr, m_autil.mk_numeral(l, true))), m); - andList.push_back(and_expr); - } - andList.push_back(mk_or(orList)); - expr_ref implLhs(ctx.mk_eq_atom(testvar, str), m); - expr_ref implRhs(mk_and(andList), m); - assert_implication(implLhs, implRhs); } + // now create a fake length tester over this finite disjunction of lengths + + fvar_len_count_map[v] = 1; + unsigned int testNum = fvar_len_count_map[v]; + + expr_ref indicator(mk_internal_lenTest_var(v, testNum), m); + SASSERT(indicator); + m_trail.push_back(indicator); + + fvar_lenTester_map[v].shrink(0); + fvar_lenTester_map[v].push_back(indicator); + lenTester_fvar_map[indicator] = v; + + expr_ref_vector orList(m); + expr_ref_vector andList(m); + + for (rational l = v_lower_bound; l <= v_upper_bound; l += rational::one()) { + // TODO integrate with the enhancements in gen_len_test_options() + std::string lStr = l.to_string(); + expr_ref str_indicator(m_strutil.mk_string(lStr), m); + expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); + orList.push_back(or_expr); + expr_ref and_expr(ctx.mk_eq_atom(or_expr, ctx.mk_eq_atom(vLengthExpr, m_autil.mk_numeral(l, true))), m); + andList.push_back(and_expr); + } + andList.push_back(mk_or(orList)); + expr_ref implLhs(ctx.mk_eq_atom(testvar, str), m); + expr_ref implRhs(mk_and(andList), m); + assert_implication(implLhs, implRhs); } else { // TODO figure out this case NOT_IMPLEMENTED_YET(); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 050593691..3bb093dcd 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -592,6 +592,7 @@ namespace smt { // TESTING void refresh_theory_var(expr * e); + expr_ref set_up_finite_model_test(expr * lhs, expr * rhs); void finite_model_test(expr * v, expr * c); public: From 794e210958df94b313fae287cbfa8951d1c712d7 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 16 Jan 2017 21:42:11 -0500 Subject: [PATCH 318/562] finite model fix --- src/smt/theory_str.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 5313b08ce..e91709962 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -6673,6 +6673,12 @@ void theory_str::finite_model_test(expr * testvar, expr * str) { ptr_vector & vars = finite_model_test_varlists[testvar]; for (ptr_vector::iterator it = vars.begin(); it != vars.end(); ++it) { expr * v = *it; + bool v_has_eqc = false; + get_eqc_value(v, v_has_eqc); + if (v_has_eqc) { + TRACE("t_str_detail", tout << "variable " << mk_pp(v,m) << " already equivalent to a string constant" << std::endl;); + continue; + } // check for any sort of existing length tester we might interfere with if (m_params.m_UseBinarySearch) { NOT_IMPLEMENTED_YET(); @@ -6763,8 +6769,8 @@ void theory_str::finite_model_test(expr * testvar, expr * str) { expr_ref implRhs(mk_and(andList), m); assert_implication(implLhs, implRhs); } else { - // TODO figure out this case - NOT_IMPLEMENTED_YET(); + TRACE("t_str_detail", tout << "already found existing length testers for " << mk_pp(v, m) << std::endl;); + continue; } } } // foreach (v in vars) From a570149b57e30137d27f647ef59c83ca9fd793fa Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 17 Jan 2017 14:49:57 -0500 Subject: [PATCH 319/562] finite overlap models with binary search --- src/smt/theory_str.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index e91709962..3153fa337 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -6681,7 +6681,15 @@ void theory_str::finite_model_test(expr * testvar, expr * str) { } // check for any sort of existing length tester we might interfere with if (m_params.m_UseBinarySearch) { - NOT_IMPLEMENTED_YET(); + if (binary_search_len_tester_stack.contains(v) && !binary_search_len_tester_stack[v].empty()) { + TRACE("t_str_detail", tout << "already found existing length testers for " << mk_pp(v, m) << std::endl;); + continue; + } else { + // start binary search as normal + expr_ref implLhs(ctx.mk_eq_atom(testvar, str), m); + expr_ref implRhs(binary_search_length_test(v, NULL, ""), m); + assert_implication(implLhs, implRhs); + } } else { bool map_effectively_empty = false; if (fvar_len_count_map.find(v) == fvar_len_count_map.end()) { From 50e2273dbdd67b8d7fa8940f3411d3b0a9d93d57 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 20 Jan 2017 17:39:32 -0500 Subject: [PATCH 320/562] substr bugfix --- src/ast/rewriter/str_rewriter.cpp | 45 ++++++++++++++--- src/smt/theory_str.cpp | 81 +++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 8 deletions(-) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index bc64e7218..2e3c82613 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -431,18 +431,47 @@ br_status str_rewriter::mk_str_from_int(expr * arg0, expr_ref & result) { br_status str_rewriter::mk_str_Substr(expr * base, expr * start, expr * len, expr_ref & result) { TRACE("t_str_rw", tout << "rewrite (Substr " << mk_pp(base, m()) << " " << mk_pp(start, m()) << " " << mk_pp(len, m()) << ")" << std::endl;); - rational startVal, lenVal; - if (m_strutil.is_string(base) && m_autil.is_numeral(start, startVal) && m_autil.is_numeral(len, lenVal)) { - std::string baseStr = m_strutil.get_string_constant_value(base); - // TODO handling for invalid start/len - if (startVal.is_nonneg() && lenVal.is_nonneg() && startVal.get_unsigned() <= baseStr.length()) { - TRACE("t_str_rw", tout << "rewriting constant Substr expression" << std::endl;); - std::string substr = baseStr.substr(startVal.get_unsigned(), lenVal.get_unsigned()); - result = m_strutil.mk_string(substr); + + bool constant_base = m_strutil.is_string(base); + std::string baseStr; + if (constant_base) { + baseStr = m_strutil.get_string_constant_value(base); + } + rational startVal; + bool constant_start = m_autil.is_numeral(start, startVal); + rational lenVal; + bool constant_len = m_autil.is_numeral(len, lenVal); + + // case 1: start < 0 or len < 0 + if ( (constant_start && startVal.is_neg()) || (constant_len && lenVal.is_neg()) ) { + TRACE("t_str_rw", tout << "start/len of substr is negative" << std::endl;); + result = m_strutil.mk_string(""); + return BR_DONE; + } + // case 1.1: start >= length(base) + if (constant_start && constant_base) { + rational baseStrlen((unsigned int)baseStr.length()); + if (startVal >= baseStrlen) { + TRACE("t_str_rw", tout << "start >= strlen for substr" << std::endl;); + result = m_strutil.mk_string(""); return BR_DONE; } } + if (constant_base && constant_start && constant_len) { + rational baseStrlen((unsigned int)baseStr.length()); + std::string retval; + if (startVal + lenVal >= baseStrlen) { + // case 2: pos+len goes past the end of the string + retval = baseStr.substr(startVal.get_unsigned(), std::string::npos); + } else { + // case 3: pos+len still within string + retval = baseStr.substr(startVal.get_unsigned(), lenVal.get_unsigned()); + } + result = m_strutil.mk_string(retval); + return BR_DONE; + } + return BR_FAILED; } diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 3153fa337..706f2cd73 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1579,6 +1579,86 @@ void theory_str::instantiate_axiom_Substr(enode * e) { TRACE("t_str_detail", tout << "instantiate Substr axiom for " << mk_pp(expr, m) << std::endl;); + expr_ref substrBase(expr->get_arg(0), m); + expr_ref substrPos(expr->get_arg(1), m); + expr_ref substrLen(expr->get_arg(2), m); + SASSERT(substrBase); + SASSERT(substrPos); + SASSERT(substrLen); + + expr_ref zero(m_autil.mk_numeral(rational::zero(), true), m); + expr_ref minusOne(m_autil.mk_numeral(rational::minus_one(), true), m); + SASSERT(zero); + SASSERT(minusOne); + + expr_ref_vector argumentsValid_terms(m); + // pos >= 0 + argumentsValid_terms.push_back(m_autil.mk_ge(substrPos, zero)); + // pos < strlen(base) + // --> pos + -1*strlen(base) < 0 + argumentsValid_terms.push_back(m.mk_not(m_autil.mk_ge( + m_autil.mk_add(substrPos, m_autil.mk_mul(minusOne, substrLen)), + zero))); + // len >= 0 + argumentsValid_terms.push_back(m_autil.mk_ge(substrLen, zero)); + + expr_ref argumentsValid(mk_and(argumentsValid_terms), m); + SASSERT(argumentsValid); + ctx.internalize(argumentsValid, false); + + // (pos+len) >= strlen(base) + // --> pos + len + -1*strlen(base) >= 0 + expr_ref lenOutOfBounds(m_autil.mk_ge( + m_autil.mk_add(substrPos, substrLen, m_autil.mk_mul(minusOne, mk_strlen(substrBase))), + zero), m); + SASSERT(lenOutOfBounds); + ctx.internalize(argumentsValid, false); + + // Case 1: pos < 0 or pos >= strlen(base) or len < 0 + // ==> (Substr ...) = "" + expr_ref case1_premise(m.mk_not(argumentsValid), m); + SASSERT(case1_premise); + ctx.internalize(case1_premise, false); + expr_ref case1_conclusion(ctx.mk_eq_atom(expr, mk_string("")), m); + SASSERT(case1_conclusion); + ctx.internalize(case1_conclusion, false); + expr_ref case1(rewrite_implication(case1_premise, case1_conclusion), m); + SASSERT(case1); + + // Case 2: (pos >= 0 and pos < strlen(base) and len >= 0) and (pos+len) >= strlen(base) + // ==> base = t0.t1 AND len(t0) = pos AND (Substr ...) = t1 + expr_ref t0(mk_str_var("t0"), m); + expr_ref t1(mk_str_var("t1"), m); + expr_ref case2_conclusion(m.mk_and( + ctx.mk_eq_atom(substrBase, mk_concat(t0,t1)), + ctx.mk_eq_atom(mk_strlen(t0), substrPos), + ctx.mk_eq_atom(expr, t1)), m); + expr_ref case2(rewrite_implication(m.mk_and(argumentsValid, lenOutOfBounds), case2_conclusion), m); + SASSERT(case2); + + // Case 3: (pos >= 0 and pos < strlen(base) and len >= 0) and (pos+len) < strlen(base) + // ==> base = t2.t3.t4 AND len(t2) = pos AND len(t3) = len AND (Substr ...) = t3 + expr_ref t2(mk_str_var("t2"), m); + expr_ref t3(mk_str_var("t3"), m); + expr_ref t4(mk_str_var("t4"), m); + expr_ref_vector case3_conclusion_terms(m); + case3_conclusion_terms.push_back(ctx.mk_eq_atom(substrBase, mk_concat(t2, mk_concat(t3, t4)))); + case3_conclusion_terms.push_back(ctx.mk_eq_atom(mk_strlen(t2), substrPos)); + case3_conclusion_terms.push_back(ctx.mk_eq_atom(mk_strlen(t3), substrLen)); + case3_conclusion_terms.push_back(ctx.mk_eq_atom(expr, t3)); + expr_ref case3_conclusion(mk_and(case3_conclusion_terms), m); + expr_ref case3(rewrite_implication(m.mk_and(argumentsValid, m.mk_not(lenOutOfBounds)), case3_conclusion), m); + SASSERT(case3); + + ctx.internalize(case1, false); + ctx.internalize(case2, false); + ctx.internalize(case3, false); + + expr_ref finalAxiom(m.mk_and(case1, case2, case3), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); + + /* expr_ref ts0(mk_str_var("ts0"), m); expr_ref ts1(mk_str_var("ts1"), m); expr_ref ts2(mk_str_var("ts2"), m); @@ -1601,6 +1681,7 @@ void theory_str::instantiate_axiom_Substr(enode * e) { expr_ref finalAxiom(m.mk_and(breakdownAssert, reduceToVar), m); SASSERT(finalAxiom); assert_axiom(finalAxiom); + */ } void theory_str::instantiate_axiom_Replace(enode * e) { From 09ac5645e4c947e134e62534b4e44417a99bf771 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 22 Jan 2017 23:21:20 -0500 Subject: [PATCH 321/562] parameterize theory-aware activity of overlap --- src/smt/params/smt_params_helper.pyg | 3 ++- src/smt/params/theory_str_params.cpp | 1 + src/smt/params/theory_str_params.h | 5 ++++- src/smt/theory_str.cpp | 18 +++++++++--------- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/smt/params/smt_params_helper.pyg b/src/smt/params/smt_params_helper.pyg index e23915ab4..75ee20ebd 100644 --- a/src/smt/params/smt_params_helper.pyg +++ b/src/smt/params/smt_params_helper.pyg @@ -73,5 +73,6 @@ def_module_params(module_name='smt', ('str.binary_search_start', UINT, 64, 'initial upper bound for theory_str binary search'), ('theory_case_split', BOOL, False, 'Allow the context to use heuristics involving theory case splits, which are a set of literals of which exactly one can be assigned True. If this option is false, the context will generate extra axioms to enforce this instead.'), ('theory_aware_branching', BOOL, False, 'Allow the context to use extra information from theory solvers regarding literal branching prioritization.'), - ('str.finite_overlap_models', BOOL, False, 'attempt a finite model search for overlapping variables instead of completely giving up on the arrangement') + ('str.finite_overlap_models', BOOL, False, 'attempt a finite model search for overlapping variables instead of completely giving up on the arrangement'), + ('str.overlap_priority', DOUBLE, -0.1, 'theory-aware priority for overlapping variable cases; use smt.theory_aware_branching=true') )) diff --git a/src/smt/params/theory_str_params.cpp b/src/smt/params/theory_str_params.cpp index 46302cf82..f86cd9379 100644 --- a/src/smt/params/theory_str_params.cpp +++ b/src/smt/params/theory_str_params.cpp @@ -30,4 +30,5 @@ void theory_str_params::updt_params(params_ref const & _p) { m_FiniteOverlapModels = p.str_finite_overlap_models(); m_UseBinarySearch = p.str_use_binary_search(); m_BinarySearchInitialUpperBound = p.str_binary_search_start(); + m_OverlapTheoryAwarePriority = p.str_overlap_priority(); } diff --git a/src/smt/params/theory_str_params.h b/src/smt/params/theory_str_params.h index 4effb0897..de0945395 100644 --- a/src/smt/params/theory_str_params.h +++ b/src/smt/params/theory_str_params.h @@ -78,6 +78,8 @@ struct theory_str_params { bool m_UseBinarySearch; unsigned m_BinarySearchInitialUpperBound; + double m_OverlapTheoryAwarePriority; + theory_str_params(params_ref const & p = params_ref()): m_AssertStrongerArrangements(true), m_AggressiveLengthTesting(false), @@ -88,7 +90,8 @@ struct theory_str_params { m_StringConstantCache(true), m_FiniteOverlapModels(false), m_UseBinarySearch(false), - m_BinarySearchInitialUpperBound(64) + m_BinarySearchInitialUpperBound(64), + m_OverlapTheoryAwarePriority(-0.1) { updt_params(p); } diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 706f2cd73..138b7db9f 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3249,7 +3249,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { if (m_params.m_FiniteOverlapModels) { expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); assert_implication(ax_l, tester); - add_theory_aware_branching_info(tester, -0.1, l_true); + add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); // TODO printCutVar(m, y); @@ -3307,7 +3307,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { if (m_params.m_FiniteOverlapModels) { expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); assert_implication(ax_l, tester); - add_theory_aware_branching_info(tester, -0.1, l_true); + add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); // TODO printCutVar(m, y); @@ -3361,7 +3361,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { if (m_params.m_FiniteOverlapModels) { expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); arrangement_disjunction.push_back(tester); - add_theory_aware_branching_info(tester, -0.1, l_true); + add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); @@ -3406,7 +3406,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { // TODO this might repeat the case above, we may wish to avoid doing this twice expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); arrangement_disjunction.push_back(tester); - add_theory_aware_branching_info(tester, -0.1, l_true); + add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); // TODO printCutVar(x, n); @@ -3645,7 +3645,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { if (m_params.m_FiniteOverlapModels) { expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); assert_implication(ax_l, tester); - add_theory_aware_branching_info(tester, -0.1, l_true); + add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { TRACE("t_str", tout << "AVOID LOOP: SKIP" << std::endl;); // TODO printCutVar(m, y); @@ -3753,7 +3753,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { if (m_params.m_FiniteOverlapModels) { expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); arrangement_disjunction.push_back(tester); - add_theory_aware_branching_info(tester, -0.1, l_true); + add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); // TODO printCutVar(m, y) @@ -4046,7 +4046,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { if (m_params.m_FiniteOverlapModels) { expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); assert_implication(ax_l, tester); - add_theory_aware_branching_info(tester, -0.1, l_true); + add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); // TODO printCutVar(x, n); @@ -4126,7 +4126,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { if (m_params.m_FiniteOverlapModels) { expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); arrangement_disjunction.push_back(tester); - add_theory_aware_branching_info(tester, -0.1, l_true); + add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); // TODO printCutVAR(x, n) @@ -4532,7 +4532,7 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { if (m_params.m_FiniteOverlapModels) { expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); arrangement_disjunction.push_back(tester); - add_theory_aware_branching_info(tester, -0.1, l_true); + add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); TRACE("t_str", print_cut_var(m, tout); print_cut_var(y, tout);); From a879b240114e75938ac2668ccd8f178a87c94ad4 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 27 Jan 2017 16:26:30 -0500 Subject: [PATCH 322/562] add str.prefixof, str.suffixof in theory_str --- src/ast/rewriter/str_rewriter.cpp | 18 ++++++++++++++++++ src/ast/rewriter/str_rewriter.h | 2 ++ src/ast/str_decl_plugin.cpp | 14 ++++++++++++++ src/ast/str_decl_plugin.h | 15 +++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index 2e3c82613..3926e66e1 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -375,6 +375,18 @@ br_status str_rewriter::mk_str_Replace(expr * base, expr * source, expr * target } } +br_status str_rewriter::mk_str_prefixof(expr * pre, expr * full, expr_ref & result) { + TRACE("t_str_rw", tout << "rewrite (str.prefixof " << mk_pp(pre, m()) << " " << mk_pp(full, m()) << ")" << std::endl;); + result = m_strutil.mk_str_StartsWith(full, pre); + return BR_REWRITE_FULL; +} + +br_status str_rewriter::mk_str_suffixof(expr * post, expr * full, expr_ref & result) { + TRACE("t_str_rw", tout << "rewrite (str.suffixof" << mk_pp(post, m()) << " " << mk_pp(full, m()) << ")" << std::endl;); + result = m_strutil.mk_str_EndsWith(full, post); + return BR_REWRITE_FULL; +} + br_status str_rewriter::mk_str_to_int(expr * arg0, expr_ref & result) { TRACE("t_str_rw", tout << "rewrite (str.to-int " << mk_pp(arg0, m()) << ")" << std::endl;); @@ -623,6 +635,12 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con case OP_STR_REPLACE: SASSERT(num_args == 3); return mk_str_Replace(args[0], args[1], args[2], result); + case OP_STR_PREFIXOF: + SASSERT(num_args == 2); + return mk_str_prefixof(args[0], args[1], result); + case OP_STR_SUFFIXOF: + SASSERT(num_args == 2); + return mk_str_suffixof(args[0], args[1], result); case OP_STR_STR2INT: SASSERT(num_args == 1); return mk_str_to_int(args[0], result); diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index 145c0193e..0494d4d1b 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -53,6 +53,8 @@ public: br_status mk_str_LastIndexof(expr * haystack, expr * needle, expr_ref & result); br_status mk_str_Replace(expr * base, expr * source, expr * target, expr_ref & result); br_status mk_str_Substr(expr * base, expr * start, expr * len, expr_ref & result); + br_status mk_str_prefixof(expr * pre, expr * full, expr_ref & result); + br_status mk_str_suffixof(expr * post, expr * full, expr_ref & result); br_status mk_str_to_int(expr * arg0, expr_ref & result); br_status mk_str_from_int(expr * arg0, expr_ref & result); diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 8ac1f722f..60f50b5c4 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -38,6 +38,8 @@ str_decl_plugin::str_decl_plugin(): m_replace_decl(0), m_str2int_decl(0), m_int2str_decl(0), + m_prefixof_decl(0), + m_suffixof_decl(0), m_re_str2regex_decl(0), m_re_regexin_decl(0), m_re_regexconcat_decl(0), @@ -69,6 +71,8 @@ void str_decl_plugin::finalize(void) { DEC_REF(m_lastindexof_decl); DEC_REF(m_substr_decl); DEC_REF(m_replace_decl); + DEC_REF(m_prefixof_decl); + DEC_REF(m_suffixof_decl); DEC_REF(m_str2int_decl); DEC_REF(m_int2str_decl); DEC_REF(m_re_str2regex_decl); @@ -149,6 +153,12 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_manager->inc_ref(m_replace_decl); } + m_prefixof_decl = m->mk_func_decl(symbol("str.prefixof"), s, s, boolT, func_decl_info(id, OP_STR_PREFIXOF)); + m_manager->inc_ref(m_prefixof_decl); + + m_suffixof_decl = m->mk_func_decl(symbol("str.suffixof"), s, s, boolT, func_decl_info(id, OP_STR_SUFFIXOF)); + m_manager->inc_ref(m_suffixof_decl); + m_str2int_decl = m->mk_func_decl(symbol("str.to-int"), s, i, func_decl_info(id, OP_STR_STR2INT)); m_manager->inc_ref(m_str2int_decl); @@ -206,6 +216,8 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { case OP_STR_LASTINDEXOF: return m_lastindexof_decl; case OP_STR_SUBSTR: return m_substr_decl; case OP_STR_REPLACE: return m_replace_decl; + case OP_STR_PREFIXOF: return m_prefixof_decl; + case OP_STR_SUFFIXOF: return m_suffixof_decl; case OP_STR_STR2INT: return m_str2int_decl; case OP_STR_INT2STR: return m_int2str_decl; case OP_RE_STR2REGEX: return m_re_str2regex_decl; @@ -281,6 +293,8 @@ void str_decl_plugin::get_op_names(svector & op_names, symbol cons op_names.push_back(builtin_name("LastIndexof", OP_STR_LASTINDEXOF)); op_names.push_back(builtin_name("Substring", OP_STR_SUBSTR)); op_names.push_back(builtin_name("Replace", OP_STR_REPLACE)); + op_names.push_back(builtin_name("str.prefixof", OP_STR_PREFIXOF)); + op_names.push_back(builtin_name("str.suffixof", OP_STR_SUFFIXOF)); op_names.push_back(builtin_name("str.to-int", OP_STR_STR2INT)); op_names.push_back(builtin_name("str.from-int", OP_STR_INT2STR)); op_names.push_back(builtin_name("Str2Reg", OP_RE_STR2REGEX)); diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index ff531e942..3ae034b45 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -41,6 +41,9 @@ enum str_op_kind { OP_STR_LASTINDEXOF, OP_STR_SUBSTR, OP_STR_REPLACE, + // SMT-LIB 2.5 standard operators -- these are rewritten to internal ones + OP_STR_PREFIXOF, + OP_STR_SUFFIXOF, // string-integer conversion OP_STR_STR2INT, OP_STR_INT2STR, OP_STR_PLACEHOLDER1, OP_STR_PLACEHOLDER2, @@ -78,6 +81,8 @@ protected: func_decl * m_replace_decl; func_decl * m_str2int_decl; func_decl * m_int2str_decl; + func_decl * m_prefixof_decl; + func_decl * m_suffixof_decl; func_decl * m_re_str2regex_decl; func_decl * m_re_regexin_decl; @@ -167,6 +172,16 @@ public: } app * mk_string_with_escape_characters(std::string & val); + app * mk_str_StartsWith(expr * haystack, expr * needle) { + expr * es[2] = {haystack, needle}; + return m_manager.mk_app(get_fid(), OP_STR_STARTSWITH, 2, es); + } + + app * mk_str_EndsWith(expr * haystack, expr * needle) { + expr * es[2] = {haystack, needle}; + return m_manager.mk_app(get_fid(), OP_STR_ENDSWITH, 2, es); + } + app * mk_re_Str2Reg(expr * s) { expr * es[1] = {s}; return m_manager.mk_app(get_fid(), OP_RE_STR2REGEX, 1, es); From fa1ec0b80f7c22ef1aef9a58b571d5dd9a18e5d7 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 27 Jan 2017 16:49:40 -0500 Subject: [PATCH 323/562] smtlib25 draft standard in theory_str --- src/ast/str_decl_plugin.cpp | 60 ++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 60f50b5c4..bd6d70ebe 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -112,12 +112,12 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { FIELD = m->mk_func_decl(symbol(NAME), SORT, SORT, SORT, func_decl_info(id, KIND)); \ m->inc_ref(FIELD) - MK_OP(m_concat_decl, "Concat", OP_STRCAT, s); + MK_OP(m_concat_decl, "str.++", OP_STRCAT, s); - m_length_decl = m->mk_func_decl(symbol("Length"), s, i, func_decl_info(id, OP_STRLEN)); + m_length_decl = m->mk_func_decl(symbol("str.len"), s, i, func_decl_info(id, OP_STRLEN)); m_manager->inc_ref(m_length_decl); - m_charat_decl = m->mk_func_decl(symbol("CharAt"), s, i, s, func_decl_info(id, OP_STR_CHARAT)); + m_charat_decl = m->mk_func_decl(symbol("str.at"), s, i, s, func_decl_info(id, OP_STR_CHARAT)); m_manager->inc_ref(m_charat_decl); m_startswith_decl = m->mk_func_decl(symbol("StartsWith"), s, s, boolT, func_decl_info(id, OP_STR_STARTSWITH)); @@ -126,10 +126,10 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_endswith_decl = m->mk_func_decl(symbol("EndsWith"), s, s, boolT, func_decl_info(id, OP_STR_ENDSWITH)); m_manager->inc_ref(m_endswith_decl); - m_contains_decl = m->mk_func_decl(symbol("Contains"), s, s, boolT, func_decl_info(id, OP_STR_CONTAINS)); + m_contains_decl = m->mk_func_decl(symbol("str.contains"), s, s, boolT, func_decl_info(id, OP_STR_CONTAINS)); m_manager->inc_ref(m_contains_decl); - m_indexof_decl = m->mk_func_decl(symbol("Indexof"), s, s, i, func_decl_info(id, OP_STR_INDEXOF)); + m_indexof_decl = m->mk_func_decl(symbol("str.indexof"), s, s, i, func_decl_info(id, OP_STR_INDEXOF)); m_manager->inc_ref(m_indexof_decl); { @@ -138,18 +138,18 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_manager->inc_ref(m_indexof2_decl); } - m_lastindexof_decl = m->mk_func_decl(symbol("LastIndexof"), s, s, i, func_decl_info(id, OP_STR_LASTINDEXOF)); + m_lastindexof_decl = m->mk_func_decl(symbol("str.lastindexof"), s, s, i, func_decl_info(id, OP_STR_LASTINDEXOF)); m_manager->inc_ref(m_lastindexof_decl); { sort * d[3] = {s, i, i }; - m_substr_decl = m->mk_func_decl(symbol("Substring"), 3, d, s, func_decl_info(id, OP_STR_SUBSTR)); + m_substr_decl = m->mk_func_decl(symbol("str.substr"), 3, d, s, func_decl_info(id, OP_STR_SUBSTR)); m_manager->inc_ref(m_substr_decl); } { sort * d[3] = {s, s, s}; - m_replace_decl = m->mk_func_decl(symbol("Replace"), 3, d, s, func_decl_info(id, OP_STR_REPLACE)); + m_replace_decl = m->mk_func_decl(symbol("str.replace"), 3, d, s, func_decl_info(id, OP_STR_REPLACE)); m_manager->inc_ref(m_replace_decl); } @@ -165,28 +165,28 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) { m_int2str_decl = m->mk_func_decl(symbol("str.from-int"), i, s, func_decl_info(id, OP_STR_INT2STR)); m_manager->inc_ref(m_int2str_decl); - m_re_str2regex_decl = m->mk_func_decl(symbol("Str2Reg"), s, re, func_decl_info(id, OP_RE_STR2REGEX)); + m_re_str2regex_decl = m->mk_func_decl(symbol("str.to.re"), s, re, func_decl_info(id, OP_RE_STR2REGEX)); m_manager->inc_ref(m_re_str2regex_decl); - m_re_regexin_decl = m->mk_func_decl(symbol("RegexIn"), s, re, boolT, func_decl_info(id, OP_RE_REGEXIN)); + m_re_regexin_decl = m->mk_func_decl(symbol("str.in.re"), s, re, boolT, func_decl_info(id, OP_RE_REGEXIN)); m_manager->inc_ref(m_re_regexin_decl); - m_re_regexconcat_decl = m->mk_func_decl(symbol("RegexConcat"), re, re, re, func_decl_info(id, OP_RE_REGEXCONCAT)); + m_re_regexconcat_decl = m->mk_func_decl(symbol("re.++"), re, re, re, func_decl_info(id, OP_RE_REGEXCONCAT)); m_manager->inc_ref(m_re_regexconcat_decl); - m_re_regexstar_decl = m->mk_func_decl(symbol("RegexStar"), re, re, func_decl_info(id, OP_RE_REGEXSTAR)); + m_re_regexstar_decl = m->mk_func_decl(symbol("re.*"), re, re, func_decl_info(id, OP_RE_REGEXSTAR)); m_manager->inc_ref(m_re_regexstar_decl); - m_re_regexplus_decl = m->mk_func_decl(symbol("RegexPlus"), re, re, func_decl_info(id, OP_RE_REGEXPLUS)); + m_re_regexplus_decl = m->mk_func_decl(symbol("re.+"), re, re, func_decl_info(id, OP_RE_REGEXPLUS)); m_manager->inc_ref(m_re_regexplus_decl); - m_re_regexunion_decl = m->mk_func_decl(symbol("RegexUnion"), re, re, re, func_decl_info(id, OP_RE_REGEXUNION)); + m_re_regexunion_decl = m->mk_func_decl(symbol("re.union"), re, re, re, func_decl_info(id, OP_RE_REGEXUNION)); m_manager->inc_ref(m_re_regexunion_decl); m_re_unroll_decl = m->mk_func_decl(symbol("Unroll"), re, i, s, func_decl_info(id, OP_RE_UNROLL)); m_manager->inc_ref(m_re_unroll_decl); - m_re_regexcharrange_decl = m->mk_func_decl(symbol("RegexCharRange"), s, s, re, func_decl_info(id, OP_RE_REGEXCHARRANGE)); + m_re_regexcharrange_decl = m->mk_func_decl(symbol("re.range"), s, s, re, func_decl_info(id, OP_RE_REGEXCHARRANGE)); m_manager->inc_ref(m_re_regexcharrange_decl); } @@ -282,29 +282,29 @@ app * str_decl_plugin::mk_fresh_string() { } void str_decl_plugin::get_op_names(svector & op_names, symbol const & logic) { - op_names.push_back(builtin_name("Concat", OP_STRCAT)); - op_names.push_back(builtin_name("Length", OP_STRLEN)); - op_names.push_back(builtin_name("CharAt", OP_STR_CHARAT)); + op_names.push_back(builtin_name("str.++", OP_STRCAT)); + op_names.push_back(builtin_name("str.len", OP_STRLEN)); + op_names.push_back(builtin_name("str.at", OP_STR_CHARAT)); op_names.push_back(builtin_name("StartsWith", OP_STR_STARTSWITH)); op_names.push_back(builtin_name("EndsWith", OP_STR_ENDSWITH)); - op_names.push_back(builtin_name("Contains", OP_STR_CONTAINS)); - op_names.push_back(builtin_name("Indexof", OP_STR_INDEXOF)); + op_names.push_back(builtin_name("str.contains", OP_STR_CONTAINS)); + op_names.push_back(builtin_name("str.indexof", OP_STR_INDEXOF)); op_names.push_back(builtin_name("Indexof2", OP_STR_INDEXOF2)); - op_names.push_back(builtin_name("LastIndexof", OP_STR_LASTINDEXOF)); - op_names.push_back(builtin_name("Substring", OP_STR_SUBSTR)); - op_names.push_back(builtin_name("Replace", OP_STR_REPLACE)); + op_names.push_back(builtin_name("str.lastindexof", OP_STR_LASTINDEXOF)); + op_names.push_back(builtin_name("str.substr", OP_STR_SUBSTR)); + op_names.push_back(builtin_name("str.replace", OP_STR_REPLACE)); op_names.push_back(builtin_name("str.prefixof", OP_STR_PREFIXOF)); op_names.push_back(builtin_name("str.suffixof", OP_STR_SUFFIXOF)); op_names.push_back(builtin_name("str.to-int", OP_STR_STR2INT)); op_names.push_back(builtin_name("str.from-int", OP_STR_INT2STR)); - op_names.push_back(builtin_name("Str2Reg", OP_RE_STR2REGEX)); - op_names.push_back(builtin_name("RegexIn", OP_RE_REGEXIN)); - op_names.push_back(builtin_name("RegexConcat", OP_RE_REGEXCONCAT)); - op_names.push_back(builtin_name("RegexStar", OP_RE_REGEXSTAR)); - op_names.push_back(builtin_name("RegexUnion", OP_RE_REGEXUNION)); - op_names.push_back(builtin_name("RegexPlus", OP_RE_REGEXPLUS)); + op_names.push_back(builtin_name("str.to.reg", OP_RE_STR2REGEX)); + op_names.push_back(builtin_name("str.in.reg", OP_RE_REGEXIN)); + op_names.push_back(builtin_name("re.++", OP_RE_REGEXCONCAT)); + op_names.push_back(builtin_name("re.*", OP_RE_REGEXSTAR)); + op_names.push_back(builtin_name("re.union", OP_RE_REGEXUNION)); + op_names.push_back(builtin_name("re.+", OP_RE_REGEXPLUS)); op_names.push_back(builtin_name("Unroll", OP_RE_UNROLL)); - op_names.push_back(builtin_name("RegexCharRange", OP_RE_REGEXCHARRANGE)); + op_names.push_back(builtin_name("re.range", OP_RE_REGEXCHARRANGE)); } void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { From ebcfa966c7e3c6e10371df9e2a4799bcc3421330 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 30 Jan 2017 16:07:32 -0500 Subject: [PATCH 324/562] data structure refactor in theory_str --- src/smt/theory_str.cpp | 23 ++--------------------- src/smt/theory_str.h | 6 +++--- 2 files changed, 5 insertions(+), 24 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 138b7db9f..a59bfb90b 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -7870,7 +7870,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map::iterator it = variable_set.begin(); it != variable_set.end(); ++it) { + for(obj_hashtable::iterator it = variable_set.begin(); it != variable_set.end(); ++it) { expr* var = *it; if (internal_variable_set.find(var) == internal_variable_set.end()) { TRACE("t_str_detail", tout << "new variable: " << mk_pp(var, m) << std::endl;); @@ -8819,7 +8819,7 @@ final_check_status theory_str::final_check_eh() { bool needToAssignFreeVars = false; std::set free_variables; std::set unused_internal_variables; - if (true) { // Z3str2 free variables check + { // Z3str2 free variables check std::map::iterator itor = varAppearInAssign.begin(); for (; itor != varAppearInAssign.end(); ++itor) { /* @@ -8845,25 +8845,6 @@ final_check_status theory_str::final_check_eh() { TRACE("t_str_detail", tout << "variable " << mk_pp(itor->first, m) << " = " << mk_pp(eqcString, m) << std::endl;); } } - } else { // new, possibly incorrect free variables check - // Check every variable to see if it's eq. to some string constant. - // If not, mark it as free. - TRACE("t_str_detail", tout << variable_set.size() << " variables in variable_set" << std::endl;); - for (std::set::iterator it = variable_set.begin(); it != variable_set.end(); ++it) { - TRACE("t_str_detail", tout << "checking eqc of variable " << mk_ismt2_pp(*it, m) << std::endl;); - bool has_eqc_value = false; - get_eqc_value(*it, has_eqc_value); - if (!has_eqc_value) { - // if this is an internal variable, it can be ignored...I think - if (internal_variable_set.find(*it) != internal_variable_set.end() || regex_variable_set.find(*it) != regex_variable_set.end()) { - TRACE("t_str_detail", tout << "WARNING: free internal variable " << mk_ismt2_pp(*it, m) << std::endl;); - //unused_internal_variables.insert(*it); - } else { - needToAssignFreeVars = true; - free_variables.insert(*it); - } - } - } } if (!needToAssignFreeVars) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 3bb093dcd..97f2b9fa4 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -240,9 +240,9 @@ namespace smt { bool loopDetected; obj_map > cut_var_map; - std::set variable_set; - std::set internal_variable_set; - std::set regex_variable_set; + obj_hashtable variable_set; + obj_hashtable internal_variable_set; + obj_hashtable regex_variable_set; std::map > internal_variable_scope_levels; obj_hashtable internal_lenTest_vars; From 19779f1a9b0a567f498a1665805fbe7b3d68f14a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 31 Jan 2017 11:49:10 -0500 Subject: [PATCH 325/562] fix string operators in theory_str, this breaks theory_seq temporarily --- src/ast/seq_decl_plugin.cpp | 4 ++-- src/ast/str_decl_plugin.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ast/seq_decl_plugin.cpp b/src/ast/seq_decl_plugin.cpp index 787648e19..779096038 100644 --- a/src/ast/seq_decl_plugin.cpp +++ b/src/ast/seq_decl_plugin.cpp @@ -477,8 +477,8 @@ void seq_decl_plugin::init() { m_sigs[_OP_STRING_CHARAT] = alloc(psig, m, "str.at", 0, 2, strTint2T, strT); m_sigs[_OP_STRING_PREFIX] = alloc(psig, m, "str.prefixof", 0, 2, str2T, boolT); m_sigs[_OP_STRING_SUFFIX] = alloc(psig, m, "str.suffixof", 0, 2, str2T, boolT); - m_sigs[_OP_STRING_IN_REGEXP] = alloc(psig, m, "str.in.re", 0, 2, strTreT, boolT); - m_sigs[_OP_STRING_TO_REGEXP] = alloc(psig, m, "str.to.re", 0, 1, &strT, reT); + m_sigs[_OP_STRING_IN_REGEXP] = alloc(psig, m, "seqstr.in.re", 0, 2, strTreT, boolT); + m_sigs[_OP_STRING_TO_REGEXP] = alloc(psig, m, "seqstr.to.re", 0, 1, &strT, reT); m_sigs[_OP_REGEXP_EMPTY] = alloc(psig, m, "re.nostr", 0, 0, 0, reT); m_sigs[_OP_REGEXP_FULL] = alloc(psig, m, "re.allchar", 0, 0, 0, reT); m_sigs[_OP_STRING_SUBSTR] = alloc(psig, m, "str.substr", 0, 3, strTint2T, strT); diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index bd6d70ebe..766fefdcf 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -297,8 +297,8 @@ void str_decl_plugin::get_op_names(svector & op_names, symbol cons op_names.push_back(builtin_name("str.suffixof", OP_STR_SUFFIXOF)); op_names.push_back(builtin_name("str.to-int", OP_STR_STR2INT)); op_names.push_back(builtin_name("str.from-int", OP_STR_INT2STR)); - op_names.push_back(builtin_name("str.to.reg", OP_RE_STR2REGEX)); - op_names.push_back(builtin_name("str.in.reg", OP_RE_REGEXIN)); + op_names.push_back(builtin_name("str.to.re", OP_RE_STR2REGEX)); + op_names.push_back(builtin_name("str.in.re", OP_RE_REGEXIN)); op_names.push_back(builtin_name("re.++", OP_RE_REGEXCONCAT)); op_names.push_back(builtin_name("re.*", OP_RE_REGEXSTAR)); op_names.push_back(builtin_name("re.union", OP_RE_REGEXUNION)); From 55cb440aae7c24ac1923a57b40e4aa3b5afc5ffe Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 7 Feb 2017 14:41:16 -0500 Subject: [PATCH 326/562] add cut var info for theory_str processtype2 --- src/smt/theory_str.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index a59bfb90b..097cfcb15 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3648,7 +3648,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { TRACE("t_str", tout << "AVOID LOOP: SKIP" << std::endl;); - // TODO printCutVar(m, y); + TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); } } } @@ -3756,7 +3756,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - // TODO printCutVar(m, y) + TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); } } } From c456795acdffcf5ada19c10303487fe0686cd2f1 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 7 Feb 2017 17:14:11 -0500 Subject: [PATCH 327/562] temporarily remove finite model finding from theory_str --- src/smt/theory_str.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 097cfcb15..b3f2bc478 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -6968,6 +6968,7 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { return; } + /* // temporarily disabled, we are borrowing these testers for something else if (m_params.m_FiniteOverlapModels && !finite_model_test_varlists.empty()) { // TODO NEXT if (finite_model_test_varlists.contains(lhs)) { @@ -6976,6 +6977,7 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { finite_model_test(rhs, lhs); return; } } + */ if (free_var_attempt(lhs, rhs) || free_var_attempt(rhs, lhs)) { return; From 3670fa64e69971727ec8ee7612e426e98a95ef7a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 11 Feb 2017 16:59:06 -0500 Subject: [PATCH 328/562] add hex escape support theory_str --- src/ast/str_decl_plugin.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 766fefdcf..ea539c0c6 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -375,8 +375,7 @@ app * str_util::mk_string_with_escape_characters(std::string & val) { // check escape sequence i++; if (i >= val.length()) { - // TODO illegal escape sequence - NOT_IMPLEMENTED_YET(); + get_manager().raise_exception("invalid escape sequence"); } char escapeChar1 = val.at(i); if (escapeChar1 == 'a') { @@ -398,8 +397,21 @@ app * str_util::mk_string_with_escape_characters(std::string & val) { } else if (escapeChar1 == '\\') { parsedStr.push_back('\\'); } else if (escapeChar1 == 'x') { - // TODO hex escape - NOT_IMPLEMENTED_YET(); + // hex escape: we expect 'x' to be followed by exactly two hex digits + // which means that i+2 must be a valid index + if (i+2 >= val.length()) { + get_manager().raise_exception("invalid hex escape: \\x must be followed by exactly two hex digits"); + } + char hexDigitHi = val.at(i+1); + char hexDigitLo = val.at(i+2); + i += 2; + if (!isxdigit((int)hexDigitHi) || !isxdigit((int)hexDigitLo)) { + get_manager().raise_exception("invalid hex escape: \\x must be followed by exactly two hex digits"); + } + char tmp[3] = {hexDigitHi, hexDigitLo, '\0'}; + long converted = strtol(tmp, NULL, 16); + unsigned char convChar = (unsigned char)converted; + parsedStr.push_back(convChar); } else if (escapeChar1 == '0' || escapeChar1 == '1' || escapeChar1 == '2' || escapeChar1 == '3' || escapeChar1 == '4' || escapeChar1 == '5' || escapeChar1 == '6' || escapeChar1 == '7') { // TODO octal escape From e699f25889c8ba6e0ac608b0e83c77a9f6f3399d Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 13 Feb 2017 16:24:32 -0500 Subject: [PATCH 329/562] theory_str cleanup --- src/smt/theory_str.cpp | 163 ++--------------------------------------- 1 file changed, 5 insertions(+), 158 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index b3f2bc478..158342cb1 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -273,68 +273,6 @@ bool theory_str::internalize_term(app * term) { TRACE("t_str_axiom_bug", tout << "add " << mk_pp(e->get_owner(), m) << " to m_basicstr_axiom_todo" << std::endl;); } return true; - - /* // what I had before - SASSERT(!ctx.e_internalized(term)); - - unsigned num_args = term->get_num_args(); - for (unsigned i = 0; i < num_args; i++) - ctx.internalize(term->get_arg(i), false); - - enode * e = (ctx.e_internalized(term)) ? ctx.get_enode(term) : - ctx.mk_enode(term, false, false, true); - - if (is_attached_to_var(e)) - return false; - - attach_new_th_var(e); - - //if (is_concat(term)) { - // instantiate_concat_axiom(e); - //} - */ - - // TODO do we still need to do instantiate_concat_axiom()? - - // partially from theory_seq::internalize_term() - /* - if (ctx.e_internalized(term)) { - enode* e = ctx.get_enode(term); - mk_var(e); - return true; - } - TRACE("t_str_detail", tout << "internalizing term: " << mk_ismt2_pp(term, get_manager()) << std::endl;); - unsigned num_args = term->get_num_args(); - expr* arg; - for (unsigned i = 0; i < num_args; i++) { - arg = term->get_arg(i); - mk_var(ensure_enode(arg)); - } - if (m.is_bool(term)) { - bool_var bv = ctx.mk_bool_var(term); - ctx.set_var_theory(bv, get_id()); - ctx.mark_as_relevant(bv); - } - - enode* e = 0; - if (ctx.e_internalized(term)) { - e = ctx.get_enode(term); - } - else { - e = ctx.mk_enode(term, false, m.is_bool(term), true); - } - - if (opt_EagerStringConstantLengthAssertions && m_strutil.is_string(term)) { - TRACE("t_str", tout << "eagerly asserting length of string term " << mk_pp(term, m) << std::endl;); - m_basicstr_axiom_todo.insert(e); - TRACE("t_str_axiom_bug", tout << "add " << mk_pp(e->get_owner(), m) << " to m_basicstr_axiom_todo" << std::endl;); - } - - theory_var v = mk_var(e); - TRACE("t_str_detail", tout << "term " << mk_ismt2_pp(term, get_manager()) << " = v#" << v << std::endl;); - - return true; - */ } enode* theory_str::ensure_enode(expr* e) { @@ -351,18 +289,11 @@ void theory_str::refresh_theory_var(expr * e) { enode * en = ensure_enode(e); theory_var v = mk_var(en); TRACE("t_str_detail", tout << "refresh " << mk_pp(e, get_manager()) << ": v#" << v << std::endl;); - // TODO this is probably sub-optimal m_basicstr_axiom_todo.push_back(en); } theory_var theory_str::mk_var(enode* n) { TRACE("t_str_detail", tout << "mk_var for " << mk_pp(n->get_owner(), get_manager()) << std::endl;); - /* - if (!m_strutil.is_string(n->get_owner())) { - return null_theory_var; - } - */ - // TODO this may require an overhaul of m_strutil.is_string() if things suddenly start working after the following change: ast_manager & m = get_manager(); if (!(is_sort_of(m.get_sort(n->get_owner()), m_strutil.get_fid(), STRING_SORT))) { return null_theory_var; @@ -503,9 +434,6 @@ app * theory_str::mk_int(rational & q) { return m_autil.mk_numeral(q, true); } - -// TODO refactor all of these so that they don't use variable counters, but use ast_manager::mk_fresh_const instead - expr * theory_str::mk_internal_lenTest_var(expr * node, int lTries) { ast_manager & m = get_manager(); @@ -539,23 +467,6 @@ void theory_str::track_variable_scope(expr * var) { } app * theory_str::mk_internal_xor_var() { - /* - ast_manager & m = get_manager(); - std::stringstream ss; - ss << tmpXorVarCount; - tmpXorVarCount++; - std::string name = "$$_xor_" + ss.str(); - // Z3_sort r = of_sort(mk_c(c)->m().mk_sort(mk_c(c)->get_arith_fid(), INT_SORT)); - sort * int_sort = m.mk_sort(m_autil.get_family_id(), INT_SORT); - - char * new_buffer = alloc_svect(char, name.length() + 1); - strcpy(new_buffer, name.c_str()); - symbol sym(new_buffer); - - app * a = m.mk_const(m.mk_const_decl(sym, int_sort)); - m_trail.push_back(a); - return a; - */ return mk_int_var("$$_xor"); } @@ -1069,7 +980,6 @@ void theory_str::instantiate_concat_axiom(enode * cat) { // build LHS expr_ref len_xy(m); - // TODO should we use str_util for these and other expressions? len_xy = mk_strlen(a_cat); SASSERT(len_xy); @@ -1106,15 +1016,12 @@ void theory_str::instantiate_concat_axiom(enode * cat) { * Length(x) == strlen(x) */ void theory_str::instantiate_basic_string_axioms(enode * str) { - // TODO keep track of which enodes we have added axioms for, so we don't add the same ones twice? - context & ctx = get_context(); ast_manager & m = get_manager(); TRACE("t_str_axiom_bug", tout << "set up basic string axioms on " << mk_pp(str->get_owner(), m) << std::endl;); // TESTING: attempt to avoid a crash here when a variable goes out of scope - // TODO this seems to work so we probably need to do this for other propagate checks, etc. if (str->get_iscope_lvl() > ctx.get_scope_level()) { TRACE("t_str_detail", tout << "WARNING: skipping axiom setup on out-of-scope string term" << std::endl;); return; @@ -2596,7 +2503,6 @@ void theory_str::generate_mutual_exclusion(expr_ref_vector & terms) { literal_vector ls; for (unsigned i = 0; i < terms.size(); ++i) { expr * e = terms.get(i); - // TODO make sure the terms are internalized, etc.? literal l = ctx.get_literal(e); ls.push_back(l); } @@ -2605,28 +2511,6 @@ void theory_str::generate_mutual_exclusion(expr_ref_vector & terms) { void theory_str::print_cut_var(expr * node, std::ofstream & xout) { ast_manager & m = get_manager(); - /* -#ifdef DEBUGLOG - __debugPrint(logFile, "\n>> CUT info of ["); - printZ3Node(t, node); - __debugPrint(logFile, "]\n"); - - if (cut_VARMap.find(node) != cut_VARMap.end()) { - if (!cut_VARMap[node].empty()) { - __debugPrint(logFile, "[%2d] {", cut_VARMap[node].top()->level); - std::map::iterator itor = cut_VARMap[node].top()->vars.begin(); - for (; itor != cut_VARMap[node].top()->vars.end(); itor++) { - printZ3Node(t, itor->first); - __debugPrint(logFile, ", "); - } - __debugPrint(logFile, "}\n"); - } else { - - } - } - __debugPrint(logFile, "------------------------\n\n"); -#endif -*/ xout << "Cut info of " << mk_pp(node, m) << std::endl; if (cut_var_map.contains(node)) { if (!cut_var_map[node].empty()) { @@ -2924,8 +2808,6 @@ bool theory_str::will_result_in_overlap(expr * lhs, expr * rhs) { expr * m = to_app(new_nn2)->get_arg(0); expr * n = to_app(new_nn2)->get_arg(1); - // TODO is it too slow to perform length checks here to avoid false positives? - if (has_self_cut(m, y)) { TRACE("t_str_detail", tout << "Possible overlap found" << std::endl; print_cut_var(m, tout); print_cut_var(y, tout);); return true; @@ -3193,7 +3075,6 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { t2 = varForBreakConcat[key2][1]; xorFlag = varForBreakConcat[key2][2]; } - // TODO do I need to refresh the xorFlag, which is an integer var, and if so, how? refresh_theory_var(t1); add_nonempty_constraint(t1); refresh_theory_var(t2); @@ -3252,7 +3133,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - // TODO printCutVar(m, y); + TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); } } } else if (splitType == 1) { @@ -3310,7 +3191,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - // TODO printCutVar(m, y); + TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); } } } else if (splitType == -1) { @@ -3409,7 +3290,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - // TODO printCutVar(x, n); + TRACE("t_str_detail", {print_cut_var(x, tout); print_cut_var(n, tout);}); } } @@ -3572,7 +3453,6 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { temp1 = varForBreakConcat[key2][0]; xorFlag = varForBreakConcat[key2][1]; } - // TODO refresh xorFlag? refresh_theory_var(temp1); add_nonempty_constraint(temp1); } @@ -4049,7 +3929,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - // TODO printCutVar(x, n); + TRACE("t_str_detail", {print_cut_var(x, tout); print_cut_var(n, tout);}); } } } @@ -4129,7 +4009,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); - // TODO printCutVAR(x, n) + TRACE("t_str_detail", {print_cut_var(x, tout); print_cut_var(n, tout);}); } } } @@ -4712,26 +4592,6 @@ void theory_str::unroll_str2reg_constStr(expr * unrollFunc, expr * eqConstStr) { * Return that constant if it is found, and set hasEqcValue to true. * Otherwise, return n, and set hasEqcValue to false. */ -/* -expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { - context & ctx = get_context(); - // I hope this works - ctx.internalize(n, false); - enode * nNode = ctx.get_enode(n); - enode * eqcNode = nNode; - do { - app * ast = eqcNode->get_owner(); - if (is_string(eqcNode)) { - hasEqcValue = true; - return ast; - } - eqcNode = eqcNode->get_next(); - } while (eqcNode != nNode); - // not found - hasEqcValue = false; - return n; -} -*/ expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { return z3str2_get_eqc_value(n, hasEqcValue); @@ -6596,8 +6456,6 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { // check the entries in this map to make sure they're still in scope // before we use them. - // TODO XOR variables will always show up as "not in scope" because of how we update internal_variable_set - std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); @@ -7409,14 +7267,6 @@ void theory_str::push_scope_eh() { theory::push_scope_eh(); m_trail_stack.push_scope(); - // TODO out-of-scope term debugging, see comment in pop_scope_eh() - /* - context & ctx = get_context(); - ast_manager & m = get_manager(); - expr_ref_vector assignments(m); - ctx.get_assignments(assignments); - */ - sLevel += 1; TRACE("t_str", tout << "push to " << sLevel << std::endl;); TRACE_CODE(if (is_trace_enabled("t_str_dump_assign_on_scope_change")) { dump_assignments(); }); @@ -9846,7 +9696,6 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr andList.push_back(and_expr); } - // TODO cache mk_string("more") expr_ref more_option(ctx.mk_eq_atom(indicator, mk_string("more")), m); orList.push_back(more_option); // decrease priority of this option @@ -10558,8 +10407,6 @@ model_value_proc * theory_str::mk_value(enode * n, model_generator & mg) { return alloc(expr_wrapper_proc, val); } else { TRACE("t_str", tout << "WARNING: failed to find a concrete value, falling back" << std::endl;); - // TODO make absolutely sure the reason we can't find a concrete value is because of an unassigned temporary - // e.g. for an expression like (Concat X $$_str0) return alloc(expr_wrapper_proc, to_app(mk_string("**UNUSED**"))); } } From 5ca4f2a1c86604aafb805189406ead9695729663 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 13 Feb 2017 17:15:13 -0500 Subject: [PATCH 330/562] theory_str cleanup --- src/smt/theory_str.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 158342cb1..a36a75868 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4576,7 +4576,8 @@ void theory_str::unroll_str2reg_constStr(expr * unrollFunc, expr * eqConstStr) { std::string regStrValue = m_strutil.get_string_constant_value(strInStr2RegFunc); int strLen = strValue.length(); int regStrLen = regStrValue.length(); - int cnt = strLen / regStrLen; // TODO prevent DIV/0 on regStrLen + SASSERT(regStrLen != 0); // this should never occur -- the case for empty string is handled elsewhere + int cnt = strLen / regStrLen; expr_ref implyL(ctx.mk_eq_atom(unrollFunc, eqConstStr), m); expr_ref implyR1(ctx.mk_eq_atom(oriCnt, mk_int(cnt)), m); @@ -7446,7 +7447,7 @@ void theory_str::classify_ast_by_type(expr * node, std::map & varMap varMap[node] = 1; } // check whether the node is a function that we want to inspect - else if (is_app(node)) { // TODO + else if (is_app(node)) { app * aNode = to_app(node); if (is_strlen(aNode)) { // Length From 52eaae9da0939c193e60bba43df17f47c26e6732 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 14 Feb 2017 15:19:03 -0500 Subject: [PATCH 331/562] theory_str refactor: check_contain_by_eqc_val uses contain_pair_idx_map --- src/smt/theory_str.cpp | 224 +++++++++++++++++++++-------------------- 1 file changed, 113 insertions(+), 111 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index a36a75868..504e0e2fe 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4842,130 +4842,134 @@ void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { expr_ref_vector litems(m); - // TODO refactor to use the new contain_pair_idx_map + if (contain_pair_idx_map.find(varNode) != contain_pair_idx_map.end()) { + std::set >::iterator itor1 = contain_pair_idx_map[varNode].begin(); + for (; itor1 != contain_pair_idx_map[varNode].end(); ++itor1) { + expr * strAst = itor1->first; + expr * substrAst = itor1->second; - expr_ref_vector::iterator itor1 = contains_map.begin(); - for (; itor1 != contains_map.end(); ++itor1) { - expr * boolVar = *itor1; - // boolVar is actually a Contains term - app * containsApp = to_app(boolVar); - expr * strAst = containsApp->get_arg(0); - expr * substrAst = containsApp->get_arg(1); - - // we only want to inspect the Contains terms where either of strAst or substrAst - // are equal to varNode. - - TRACE("t_str_detail", tout << "considering Contains with strAst = " << mk_pp(strAst, m) << ", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); - - if (varNode != strAst && varNode != substrAst) { - TRACE("t_str_detail", tout << "varNode not equal to strAst or substrAst, skip" << std::endl;); - continue; - } - TRACE("t_str_detail", tout << "varNode matched one of strAst or substrAst. Continuing" << std::endl;); - - // varEqcNode is str - if (strAst == varNode) { - expr_ref implyR(m); - litems.reset(); - - if (strAst != constNode) { - litems.push_back(ctx.mk_eq_atom(strAst, constNode)); + expr * boolVar; + if (!contain_pair_bool_map.find(strAst, substrAst, boolVar)) { + TRACE("t_str_detail", tout << "warning: no entry for boolVar in contain_pair_bool_map" << std::endl;); } - std::string strConst = m_strutil.get_string_constant_value(constNode); - bool subStrHasEqcValue = false; - expr * substrValue = get_eqc_value(substrAst, subStrHasEqcValue); - if (substrValue != substrAst) { - litems.push_back(ctx.mk_eq_atom(substrAst, substrValue)); + // boolVar is actually a Contains term + app * containsApp = to_app(boolVar); + + // we only want to inspect the Contains terms where either of strAst or substrAst + // are equal to varNode. + + TRACE("t_str_detail", tout << "considering Contains with strAst = " << mk_pp(strAst, m) << ", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); + + if (varNode != strAst && varNode != substrAst) { + TRACE("t_str_detail", tout << "varNode not equal to strAst or substrAst, skip" << std::endl;); + continue; } + TRACE("t_str_detail", tout << "varNode matched one of strAst or substrAst. Continuing" << std::endl;); - if (subStrHasEqcValue) { - // subStr has an eqc constant value - std::string subStrConst = m_strutil.get_string_constant_value(substrValue); + // varEqcNode is str + if (strAst == varNode) { + expr_ref implyR(m); + litems.reset(); - TRACE("t_str_detail", tout << "strConst = " << strConst << ", subStrConst = " << subStrConst << std::endl;); - - if (strConst.find(subStrConst) != std::string::npos) { - //implyR = ctx.mk_eq(ctx, boolVar, Z3_mk_true(ctx)); - implyR = boolVar; - } else { - //implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_false(ctx)); - implyR = m.mk_not(boolVar); + if (strAst != constNode) { + litems.push_back(ctx.mk_eq_atom(strAst, constNode)); } - } else { - // ------------------------------------------------------------------------------------------------ - // subStr doesn't have an eqc contant value - // however, subStr equals to some concat(arg_1, arg_2, ..., arg_n) - // if arg_j is a constant and is not a part of the strConst, it's sure that the contains is false - // ** This check is needed here because the "strConst" and "strAst" may not be in a same eqc yet - // ------------------------------------------------------------------------------------------------ - // collect eqc concat - std::set eqcConcats; - get_concats_in_eqc(substrAst, eqcConcats); - for (std::set::iterator concatItor = eqcConcats.begin(); - concatItor != eqcConcats.end(); concatItor++) { - expr_ref_vector constList(m); - bool counterEgFound = false; - // get constant strings in concat - expr * aConcat = *concatItor; - get_const_str_asts_in_node(aConcat, constList); - for (expr_ref_vector::iterator cstItor = constList.begin(); - cstItor != constList.end(); cstItor++) { - std::string pieceStr = m_strutil.get_string_constant_value(*cstItor); - if (strConst.find(pieceStr) == std::string::npos) { - counterEgFound = true; - if (aConcat != substrAst) { - litems.push_back(ctx.mk_eq_atom(substrAst, aConcat)); + std::string strConst = m_strutil.get_string_constant_value(constNode); + bool subStrHasEqcValue = false; + expr * substrValue = get_eqc_value(substrAst, subStrHasEqcValue); + if (substrValue != substrAst) { + litems.push_back(ctx.mk_eq_atom(substrAst, substrValue)); + } + + if (subStrHasEqcValue) { + // subStr has an eqc constant value + std::string subStrConst = m_strutil.get_string_constant_value(substrValue); + + TRACE("t_str_detail", tout << "strConst = " << strConst << ", subStrConst = " << subStrConst << std::endl;); + + if (strConst.find(subStrConst) != std::string::npos) { + //implyR = ctx.mk_eq(ctx, boolVar, Z3_mk_true(ctx)); + implyR = boolVar; + } else { + //implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_false(ctx)); + implyR = m.mk_not(boolVar); + } + } else { + // ------------------------------------------------------------------------------------------------ + // subStr doesn't have an eqc contant value + // however, subStr equals to some concat(arg_1, arg_2, ..., arg_n) + // if arg_j is a constant and is not a part of the strConst, it's sure that the contains is false + // ** This check is needed here because the "strConst" and "strAst" may not be in a same eqc yet + // ------------------------------------------------------------------------------------------------ + // collect eqc concat + std::set eqcConcats; + get_concats_in_eqc(substrAst, eqcConcats); + for (std::set::iterator concatItor = eqcConcats.begin(); + concatItor != eqcConcats.end(); concatItor++) { + expr_ref_vector constList(m); + bool counterEgFound = false; + // get constant strings in concat + expr * aConcat = *concatItor; + get_const_str_asts_in_node(aConcat, constList); + for (expr_ref_vector::iterator cstItor = constList.begin(); + cstItor != constList.end(); cstItor++) { + std::string pieceStr = m_strutil.get_string_constant_value(*cstItor); + if (strConst.find(pieceStr) == std::string::npos) { + counterEgFound = true; + if (aConcat != substrAst) { + litems.push_back(ctx.mk_eq_atom(substrAst, aConcat)); + } + //implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_false(ctx)); + implyR = m.mk_not(boolVar); + break; } - //implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_false(ctx)); - implyR = m.mk_not(boolVar); + } + if (counterEgFound) { + TRACE("t_str_detail", tout << "Inconsistency found!" << std::endl;); break; } } - if (counterEgFound) { - TRACE("t_str_detail", tout << "Inconsistency found!" << std::endl;); - break; + } + // add assertion + if (implyR) { + expr_ref implyLHS(mk_and(litems), m); + assert_implication(implyLHS, implyR); + } + } + // varEqcNode is subStr + else if (substrAst == varNode) { + expr_ref implyR(m); + litems.reset(); + + if (substrAst != constNode) { + litems.push_back(ctx.mk_eq_atom(substrAst, constNode)); + } + bool strHasEqcValue = false; + expr * strValue = get_eqc_value(strAst, strHasEqcValue); + if (strValue != strAst) { + litems.push_back(ctx.mk_eq_atom(strAst, strValue)); + } + + if (strHasEqcValue) { + std::string strConst = m_strutil.get_string_constant_value(strValue); + std::string subStrConst = m_strutil.get_string_constant_value(constNode); + if (strConst.find(subStrConst) != std::string::npos) { + //implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_true(ctx)); + implyR = boolVar; + } else { + // implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_false(ctx)); + implyR = m.mk_not(boolVar); } } - } - // add assertion - if (implyR) { - expr_ref implyLHS(mk_and(litems), m); - assert_implication(implyLHS, implyR); - } - } - // varEqcNode is subStr - else if (substrAst == varNode) { - expr_ref implyR(m); - litems.reset(); - if (substrAst != constNode) { - litems.push_back(ctx.mk_eq_atom(substrAst, constNode)); - } - bool strHasEqcValue = false; - expr * strValue = get_eqc_value(strAst, strHasEqcValue); - if (strValue != strAst) { - litems.push_back(ctx.mk_eq_atom(strAst, strValue)); - } - - if (strHasEqcValue) { - std::string strConst = m_strutil.get_string_constant_value(strValue); - std::string subStrConst = m_strutil.get_string_constant_value(constNode); - if (strConst.find(subStrConst) != std::string::npos) { - //implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_true(ctx)); - implyR = boolVar; - } else { - // implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_false(ctx)); - implyR = m.mk_not(boolVar); + // add assertion + if (implyR) { + expr_ref implyLHS(mk_and(litems), m); + assert_implication(implyLHS, implyR); } } - - // add assertion - if (implyR) { - expr_ref implyLHS(mk_and(litems), m); - assert_implication(implyLHS, implyR); - } - } - } // for (itor1 : contains_map) + } // for (itor1 : contains_map) + } // if varNode in contain_pair_idx_map } void theory_str::check_contain_by_substr(expr * varNode, expr_ref_vector & willEqClass) { @@ -5782,7 +5786,6 @@ void theory_str::compute_contains(std::map & varAliasMap, } bool theory_str::can_concat_eq_str(expr * concat, std::string str) { - // TODO this method could use some traces and debugging info int strLen = str.length(); if (is_concat(to_app(concat))) { ptr_vector args; @@ -5834,7 +5837,6 @@ bool theory_str::can_concat_eq_str(expr * concat, std::string str) { } bool theory_str::can_concat_eq_concat(expr * concat1, expr * concat2) { - // TODO this method could use some traces and debugging info if (is_concat(to_app(concat1)) && is_concat(to_app(concat2))) { { // Suppose concat1 = (Concat X Y) and concat2 = (Concat M N). From 3e714075c48588751a724df4d64176ede3d1d345 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 14 Feb 2017 16:09:45 -0500 Subject: [PATCH 332/562] theory_str refactor: check_contain_by_substr uses contain_pair_idx_map --- src/smt/theory_str.cpp | 102 +++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 49 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 504e0e2fe..37ebc0c93 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4977,67 +4977,71 @@ void theory_str::check_contain_by_substr(expr * varNode, expr_ref_vector & willE ast_manager & m = get_manager(); expr_ref_vector litems(m); - // TODO refactor to use the new contain_pair_idx_map + if (contain_pair_idx_map.find(varNode) != contain_pair_idx_map.end()) { + std::set >::iterator itor1 = contain_pair_idx_map[varNode].begin(); + for (; itor1 != contain_pair_idx_map[varNode].end(); ++itor1) { + expr * strAst = itor1->first; + expr * substrAst = itor1->second; - expr_ref_vector::iterator itor1 = contains_map.begin(); - for (; itor1 != contains_map.end(); ++itor1) { - expr * boolVar = *itor1; - // boolVar is actually a Contains term - app * containsApp = to_app(boolVar); - expr * strAst = containsApp->get_arg(0); - expr * substrAst = containsApp->get_arg(1); + expr * boolVar; + if (!contain_pair_bool_map.find(strAst, substrAst, boolVar)) { + TRACE("t_str_detail", tout << "warning: no entry for boolVar in contain_pair_bool_map" << std::endl;); + } + // boolVar is actually a Contains term + app * containsApp = to_app(boolVar); - // we only want to inspect the Contains terms where either of strAst or substrAst - // are equal to varNode. + // we only want to inspect the Contains terms where either of strAst or substrAst + // are equal to varNode. - TRACE("t_str_detail", tout << "considering Contains with strAst = " << mk_pp(strAst, m) << ", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); + TRACE("t_str_detail", tout << "considering Contains with strAst = " << mk_pp(strAst, m) << ", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); - if (varNode != strAst && varNode != substrAst) { - TRACE("t_str_detail", tout << "varNode not equal to strAst or substrAst, skip" << std::endl;); - continue; - } - TRACE("t_str_detail", tout << "varNode matched one of strAst or substrAst. Continuing" << std::endl;); + if (varNode != strAst && varNode != substrAst) { + TRACE("t_str_detail", tout << "varNode not equal to strAst or substrAst, skip" << std::endl;); + continue; + } + TRACE("t_str_detail", tout << "varNode matched one of strAst or substrAst. Continuing" << std::endl;); - if (substrAst == varNode) { - bool strAstHasVal = false; - expr * strValue = get_eqc_value(strAst, strAstHasVal); - if (strAstHasVal) { - TRACE("t_str_detail", tout << mk_pp(strAst, m) << " has constant eqc value " << mk_pp(strValue, m) << std::endl;); - if (strValue != strAst) { - litems.push_back(ctx.mk_eq_atom(strAst, strValue)); - } - std::string strConst = m_strutil.get_string_constant_value(strValue); - // iterate eqc (also eqc-to-be) of substr - for (expr_ref_vector::iterator itAst = willEqClass.begin(); itAst != willEqClass.end(); itAst++) { - bool counterEgFound = false; - if (is_concat(to_app(*itAst))) { - expr_ref_vector constList(m); - // get constant strings in concat - app * aConcat = to_app(*itAst); - get_const_str_asts_in_node(aConcat, constList); - for (expr_ref_vector::iterator cstItor = constList.begin(); - cstItor != constList.end(); cstItor++) { - std::string pieceStr = m_strutil.get_string_constant_value(*cstItor); - if (strConst.find(pieceStr) == std::string::npos) { - TRACE("t_str_detail", tout << "Inconsistency found!" << std::endl;); - counterEgFound = true; - if (aConcat != substrAst) { - litems.push_back(ctx.mk_eq_atom(substrAst, aConcat)); + if (substrAst == varNode) { + bool strAstHasVal = false; + expr * strValue = get_eqc_value(strAst, strAstHasVal); + if (strAstHasVal) { + TRACE("t_str_detail", tout << mk_pp(strAst, m) << " has constant eqc value " << mk_pp(strValue, m) << std::endl;); + if (strValue != strAst) { + litems.push_back(ctx.mk_eq_atom(strAst, strValue)); + } + std::string strConst = m_strutil.get_string_constant_value(strValue); + // iterate eqc (also eqc-to-be) of substr + for (expr_ref_vector::iterator itAst = willEqClass.begin(); itAst != willEqClass.end(); itAst++) { + bool counterEgFound = false; + if (is_concat(to_app(*itAst))) { + expr_ref_vector constList(m); + // get constant strings in concat + app * aConcat = to_app(*itAst); + get_const_str_asts_in_node(aConcat, constList); + for (expr_ref_vector::iterator cstItor = constList.begin(); + cstItor != constList.end(); cstItor++) { + std::string pieceStr = m_strutil.get_string_constant_value(*cstItor); + if (strConst.find(pieceStr) == std::string::npos) { + TRACE("t_str_detail", tout << "Inconsistency found!" << std::endl;); + counterEgFound = true; + if (aConcat != substrAst) { + litems.push_back(ctx.mk_eq_atom(substrAst, aConcat)); + } + expr_ref implyLHS(mk_and(litems), m); + expr_ref implyR(m.mk_not(boolVar), m); + assert_implication(implyLHS, implyR); + break; } - expr_ref implyLHS(mk_and(litems), m); - expr_ref implyR(m.mk_not(boolVar), m); - assert_implication(implyLHS, implyR); - break; } } - } - if (counterEgFound) { - break; + if (counterEgFound) { + break; + } } } } } - } + } // varNode in contain_pair_idx_map } bool theory_str::in_contain_idx_map(expr * n) { From d5b1e4b015772730f82e751f2212ce482b8bbf85 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 14 Feb 2017 18:44:40 -0500 Subject: [PATCH 333/562] refactor theory_str: all library-aware/high-level terms are in one worklist --- src/smt/theory_str.cpp | 104 ++++++++++++----------------------------- src/smt/theory_str.h | 4 +- 2 files changed, 31 insertions(+), 77 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 37ebc0c93..9fcf1f084 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -805,11 +805,9 @@ expr * theory_str::mk_concat(expr * n1, expr * n2) { } bool theory_str::can_propagate() { - return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty() || !m_concat_axiom_todo.empty() || !m_concat_eval_todo.empty() - || !m_axiom_CharAt_todo.empty() || !m_axiom_StartsWith_todo.empty() || !m_axiom_EndsWith_todo.empty() - || !m_axiom_Contains_todo.empty() || !m_axiom_Indexof_todo.empty() || !m_axiom_Indexof2_todo.empty() || !m_axiom_LastIndexof_todo.empty() - || !m_axiom_Substr_todo.empty() || !m_axiom_Replace_todo.empty() - || !m_axiom_RegexIn_todo.empty() || !m_library_aware_axiom_todo.empty() + return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty() + || !m_concat_axiom_todo.empty() || !m_concat_eval_todo.empty() + || !m_library_aware_axiom_todo.empty() || !m_delayed_axiom_setup_terms.empty(); ; } @@ -842,62 +840,32 @@ void theory_str::propagate() { } m_concat_eval_todo.reset(); - for (unsigned i = 0; i < m_axiom_CharAt_todo.size(); ++i) { - instantiate_axiom_CharAt(m_axiom_CharAt_todo[i]); - } - m_axiom_CharAt_todo.reset(); - - for (unsigned i = 0; i < m_axiom_StartsWith_todo.size(); ++i) { - instantiate_axiom_StartsWith(m_axiom_StartsWith_todo[i]); - } - m_axiom_StartsWith_todo.reset(); - - for (unsigned i = 0; i < m_axiom_EndsWith_todo.size(); ++i) { - instantiate_axiom_EndsWith(m_axiom_EndsWith_todo[i]); - } - m_axiom_EndsWith_todo.reset(); - - for (unsigned i = 0; i < m_axiom_Contains_todo.size(); ++i) { - instantiate_axiom_Contains(m_axiom_Contains_todo[i]); - } - m_axiom_Contains_todo.reset(); - - for (unsigned i = 0; i < m_axiom_Indexof_todo.size(); ++i) { - instantiate_axiom_Indexof(m_axiom_Indexof_todo[i]); - } - m_axiom_Indexof_todo.reset(); - - for (unsigned i = 0; i < m_axiom_Indexof2_todo.size(); ++i) { - instantiate_axiom_Indexof2(m_axiom_Indexof2_todo[i]); - } - m_axiom_Indexof2_todo.reset(); - - for (unsigned i = 0; i < m_axiom_LastIndexof_todo.size(); ++i) { - instantiate_axiom_LastIndexof(m_axiom_LastIndexof_todo[i]); - } - m_axiom_LastIndexof_todo.reset(); - - for (unsigned i = 0; i < m_axiom_Substr_todo.size(); ++i) { - instantiate_axiom_Substr(m_axiom_Substr_todo[i]); - } - m_axiom_Substr_todo.reset(); - - for (unsigned i = 0; i < m_axiom_Replace_todo.size(); ++i) { - instantiate_axiom_Replace(m_axiom_Replace_todo[i]); - } - m_axiom_Replace_todo.reset(); - - for (unsigned i = 0; i < m_axiom_RegexIn_todo.size(); ++i) { - instantiate_axiom_RegexIn(m_axiom_RegexIn_todo[i]); - } - m_axiom_RegexIn_todo.reset(); - for (unsigned i = 0; i < m_library_aware_axiom_todo.size(); ++i) { enode * e = m_library_aware_axiom_todo[i]; if (is_str_to_int(e)) { instantiate_axiom_str_to_int(e); } else if (is_int_to_str(e)) { instantiate_axiom_int_to_str(e); + } else if (is_CharAt(e)) { + instantiate_axiom_CharAt(e); + } else if (is_StartsWith(e)) { + instantiate_axiom_StartsWith(e); + } else if (is_EndsWith(e)) { + instantiate_axiom_EndsWith(e); + } else if (is_Contains(e)) { + instantiate_axiom_Contains(e); + } else if (is_Indexof(e)) { + instantiate_axiom_Indexof(e); + } else if (is_Indexof2(e)) { + instantiate_axiom_Indexof2(e); + } else if (is_LastIndexof(e)) { + instantiate_axiom_LastIndexof(e); + } else if (is_Substr(e)) { + instantiate_axiom_Substr(e); + } else if (is_Replace(e)) { + instantiate_axiom_Replace(e); + } else if (is_RegexIn(e)) { + instantiate_axiom_RegexIn(e); } else { TRACE("t_str", tout << "BUG: unhandled library-aware term " << mk_pp(e->get_owner(), get_manager()) << std::endl;); NOT_IMPLEMENTED_YET(); @@ -7099,12 +7067,8 @@ void theory_str::set_up_axioms(expr * ex) { if (aVar->get_num_args() == 0 && !is_string(aVar)) { input_var_in_len.insert(var); } - } else if (is_CharAt(ap)) { - m_axiom_CharAt_todo.push_back(n); - } else if (is_Substr(ap)) { - m_axiom_Substr_todo.push_back(n); - } else if (is_Replace(ap)) { - m_axiom_Replace_todo.push_back(n); + } else if (is_CharAt(ap) || is_Substr(ap) || is_Replace(ap)) { + m_library_aware_axiom_todo.push_back(n); } else if (ap->get_num_args() == 0 && !is_string(ap)) { // if ex is a variable, add it to our list of variables TRACE("t_str_detail", tout << "tracking variable " << mk_ismt2_pp(ap, get_manager()) << std::endl;); @@ -7127,14 +7091,8 @@ void theory_str::set_up_axioms(expr * ex) { if (is_app(ex)) { app * ap = to_app(ex); - if (is_StartsWith(ap)) { - m_axiom_StartsWith_todo.push_back(n); - } else if (is_EndsWith(ap)) { - m_axiom_EndsWith_todo.push_back(n); - } else if (is_Contains(ap)) { - m_axiom_Contains_todo.push_back(n); - } else if (is_RegexIn(ap)) { - m_axiom_RegexIn_todo.push_back(n); + if (is_StartsWith(ap) || is_EndsWith(ap) || is_Contains(ap) || is_RegexIn(ap)) { + m_library_aware_axiom_todo.push_back(n); } } } else { @@ -7152,12 +7110,8 @@ void theory_str::set_up_axioms(expr * ex) { if (is_app(ex)) { app * ap = to_app(ex); - if (is_Indexof(ap)) { - m_axiom_Indexof_todo.push_back(n); - } else if (is_Indexof2(ap)) { - m_axiom_Indexof2_todo.push_back(n); - } else if (is_LastIndexof(ap)) { - m_axiom_LastIndexof_todo.push_back(n); + if (is_Indexof(ap) || is_Indexof2(ap) || is_LastIndexof(ap)) { + m_library_aware_axiom_todo.push_back(n); } else if (is_str_to_int(ap) || is_int_to_str(ap)) { string_int_conversion_terms.push_back(ap); m_library_aware_axiom_todo.push_back(n); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 97f2b9fa4..1915763f1 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -210,7 +210,7 @@ namespace smt { ptr_vector m_concat_eval_todo; // enode lists for term-specific axioms - // TODO maybe refactor this into a generic "library_aware_axiom_todo" list + /* ptr_vector m_axiom_CharAt_todo; ptr_vector m_axiom_StartsWith_todo; ptr_vector m_axiom_EndsWith_todo; @@ -221,8 +221,8 @@ namespace smt { ptr_vector m_axiom_Substr_todo; ptr_vector m_axiom_Replace_todo; ptr_vector m_axiom_RegexIn_todo; + */ - // TODO refactor everything to use this worklist ptr_vector m_library_aware_axiom_todo; // hashtable of all exprs for which we've already set up term-specific axioms -- From f9b3c47bf513672f0dcf76c202e8cd7d6b509aa1 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 14 Feb 2017 18:45:09 -0500 Subject: [PATCH 334/562] remove commented-out old worklists --- src/smt/theory_str.h | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 1915763f1..47a8e8d0b 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -209,20 +209,7 @@ namespace smt { ptr_vector m_string_constant_length_todo; ptr_vector m_concat_eval_todo; - // enode lists for term-specific axioms - /* - ptr_vector m_axiom_CharAt_todo; - ptr_vector m_axiom_StartsWith_todo; - ptr_vector m_axiom_EndsWith_todo; - ptr_vector m_axiom_Contains_todo; - ptr_vector m_axiom_Indexof_todo; - ptr_vector m_axiom_Indexof2_todo; - ptr_vector m_axiom_LastIndexof_todo; - ptr_vector m_axiom_Substr_todo; - ptr_vector m_axiom_Replace_todo; - ptr_vector m_axiom_RegexIn_todo; - */ - + // enode lists for library-aware/high-level string terms (e.g. substr, contains) ptr_vector m_library_aware_axiom_todo; // hashtable of all exprs for which we've already set up term-specific axioms -- From d67f732c7cf4a62f3b0d7a992d5096b69e7f6bf6 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 15 Feb 2017 13:39:55 -0500 Subject: [PATCH 335/562] theory_str data structure refactoring --- src/smt/theory_str.cpp | 6 +++--- src/smt/theory_str.h | 7 +++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 9fcf1f084..da6f94afe 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -6606,7 +6606,7 @@ void theory_str::finite_model_test(expr * testvar, expr * str) { } } else { bool map_effectively_empty = false; - if (fvar_len_count_map.find(v) == fvar_len_count_map.end()) { + if (!fvar_len_count_map.contains(v)) { map_effectively_empty = true; } @@ -6701,7 +6701,7 @@ void theory_str::finite_model_test(expr * testvar, expr * str) { void theory_str::more_len_tests(expr * lenTester, std::string lenTesterValue) { ast_manager & m = get_manager(); - if (lenTester_fvar_map.find(lenTester) != lenTester_fvar_map.end()) { + if (lenTester_fvar_map.contains(lenTester)) { expr * fVar = lenTester_fvar_map[lenTester]; expr * toAssert = gen_len_val_options_for_free_var(fVar, lenTester, lenTesterValue); TRACE("t_str_detail", tout << "asserting more length tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); @@ -9952,7 +9952,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe return binary_search_length_test(freeVar, lenTesterInCbEq, lenTesterValue); } else { bool map_effectively_empty = false; - if (fvar_len_count_map.find(freeVar) == fvar_len_count_map.end()) { + if (!fvar_len_count_map.contains(freeVar)) { TRACE("t_str_detail", tout << "fvar_len_count_map is empty" << std::endl;); map_effectively_empty = true; } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 47a8e8d0b..f81b4ada7 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -195,7 +195,6 @@ namespace smt { bool finalCheckProgressIndicator; - // TODO make sure that all generated expressions are saved into the trail expr_ref_vector m_trail; // trail for generated terms str_value_factory * m_factory; @@ -236,11 +235,11 @@ namespace smt { obj_hashtable internal_valTest_vars; obj_hashtable internal_unrollTest_vars; - std::set input_var_in_len; + obj_hashtable input_var_in_len; - std::map fvar_len_count_map; + obj_map fvar_len_count_map; std::map > fvar_lenTester_map; - std::map lenTester_fvar_map; + obj_map lenTester_fvar_map; std::map > > > fvar_valueTester_map; std::map valueTester_fvar_map; From 2e27e1cd366746113ff1717fce8a19bdd9953bf4 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 15 Feb 2017 16:08:54 -0500 Subject: [PATCH 336/562] fix obj_map insertions theory_str --- src/smt/theory_str.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index da6f94afe..3673d3e79 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -9981,7 +9981,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe // no length assertions for this free variable have ever been added. TRACE("t_str_detail", tout << "no length assertions yet" << std::endl;); - fvar_len_count_map[freeVar] = 1; + fvar_len_count_map.insert(freeVar, 1); unsigned int testNum = fvar_len_count_map[freeVar]; expr_ref indicator(mk_internal_lenTest_var(freeVar, testNum), m); @@ -9990,7 +9990,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe // since the map is "effectively empty", we can remove those variables that have left scope... fvar_lenTester_map[freeVar].shrink(0); fvar_lenTester_map[freeVar].push_back(indicator); - lenTester_fvar_map[indicator] = freeVar; + lenTester_fvar_map.insert(indicator, freeVar); expr * lenTestAssert = gen_len_test_options(freeVar, indicator, testNum); SASSERT(lenTestAssert != NULL); @@ -10089,7 +10089,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe testNum = fvar_len_count_map[freeVar]; indicator = mk_internal_lenTest_var(freeVar, testNum); fvar_lenTester_map[freeVar].push_back(indicator); - lenTester_fvar_map[indicator] = freeVar; + lenTester_fvar_map.insert(indicator, freeVar); } else { // TODO make absolutely sure this is safe to do if 'indicator' is technically out of scope indicator = fvar_lenTester_map[freeVar][i]; From 90705cfd5f3ebba397e029735d2979f40366909b Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 17 Feb 2017 13:28:52 -0500 Subject: [PATCH 337/562] remove todo from str api --- src/api/api_str.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/api/api_str.cpp b/src/api/api_str.cpp index 1a1debb5b..eb56a839b 100644 --- a/src/api/api_str.cpp +++ b/src/api/api_str.cpp @@ -81,7 +81,6 @@ extern "C" { MK_UNARY(Z3_mk_str_length, mk_c(c)->get_str_fid(), OP_STRLEN, SKIP); MK_BINARY(Z3_mk_str_at, mk_c(c)->get_str_fid(), OP_STR_CHARAT, SKIP); // translate prefixof/suffixof to StartsWith/EndsWith - // TODO string standardization might just remove StartsWith/EndsWith in future Z3_ast Z3_API Z3_mk_str_prefixof(Z3_context c, Z3_ast pre, Z3_ast full) { LOG_Z3_mk_str_prefixof(c, pre, full); Z3_TRY; From fe1a976c21778c088f1e59c77cad87497055d663 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 18 Feb 2017 15:25:04 -0500 Subject: [PATCH 338/562] fix merge remnant --- src/cmd_context/cmd_context.cpp | 32 +------------------------------- 1 file changed, 1 insertion(+), 31 deletions(-) diff --git a/src/cmd_context/cmd_context.cpp b/src/cmd_context/cmd_context.cpp index 4f1458318..dc66f5da9 100644 --- a/src/cmd_context/cmd_context.cpp +++ b/src/cmd_context/cmd_context.cpp @@ -528,39 +528,13 @@ bool cmd_context::logic_has_pb() const { } bool cmd_context::logic_has_fpa() const { -<<<<<<< HEAD - return !has_logic() || logic_has_fpa_core(m_logic); + return !has_logic() || smt_logics::logic_has_fpa(m_logic); } bool cmd_context::logic_has_str() const { return !has_logic() || m_logic == "QF_S"; } -bool cmd_context::logic_has_array_core(symbol const & s) const { - return - s == "QF_AX" || - s == "QF_AUFLIA" || - s == "QF_ANIA" || - s == "QF_ALIA" || - s == "QF_AUFLIRA" || - s == "QF_AUFNIA" || - s == "QF_AUFNIRA" || - s == "ALIA" || - s == "AUFLIA" || - s == "AUFLIRA" || - s == "AUFNIA" || - s == "AUFNIRA" || - s == "AUFBV" || - s == "ABV" || - s == "QF_ABV" || - s == "QF_AUFBV" || - s == "HORN"; -======= - return !has_logic() || smt_logics::logic_has_fpa(m_logic); ->>>>>>> upstream-master -} - - bool cmd_context::logic_has_array() const { return !has_logic() || smt_logics::logic_has_array(m_logic); } @@ -601,12 +575,8 @@ void cmd_context::init_manager_core(bool new_manager) { load_plugin(symbol("datatype"), logic_has_datatype(), fids); load_plugin(symbol("seq"), logic_has_seq(), fids); load_plugin(symbol("fpa"), logic_has_fpa(), fids); -<<<<<<< HEAD load_plugin(symbol("str"), logic_has_str(), fids); -======= load_plugin(symbol("pb"), logic_has_pb(), fids); - ->>>>>>> upstream-master svector::iterator it = fids.begin(); svector::iterator end = fids.end(); for (; it != end; ++it) { From a081d819413449e73166941cb60758aa0ebd936c Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 20 Feb 2017 13:27:36 -0500 Subject: [PATCH 339/562] remove local dev files from gitignore --- .gitignore | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.gitignore b/.gitignore index a5c9c7e66..7cc289168 100644 --- a/.gitignore +++ b/.gitignore @@ -87,10 +87,3 @@ src/*/*/*/CMakeLists.txt src/api/dotnet/cmake_install_gac.cmake.in src/api/dotnet/cmake_uninstall_gac.cmake.in -# reference code for z3str2 -Z3-str -Z3-str/** -# test cases -tests -tests/** - From 15e3d3ec3ce8e3547b291b699963a26413b45a1b Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 21 Feb 2017 15:51:08 -0500 Subject: [PATCH 340/562] octal escape theory_str --- src/ast/str_decl_plugin.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index ea539c0c6..80493f3cf 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -414,8 +414,29 @@ app * str_util::mk_string_with_escape_characters(std::string & val) { parsedStr.push_back(convChar); } else if (escapeChar1 == '0' || escapeChar1 == '1' || escapeChar1 == '2' || escapeChar1 == '3' || escapeChar1 == '4' || escapeChar1 == '5' || escapeChar1 == '6' || escapeChar1 == '7') { - // TODO octal escape - NOT_IMPLEMENTED_YET(); + // octal escape: we expect exactly three octal digits + // which means that val[i], val[i+1], val[i+2] must all be octal digits + // and that i+2 must be a valid index + if (i+2 >= val.length()) { + get_manager().raise_exception("invalid octal escape: exactly three octal digits required"); + } + char c2 = escapeChar1; + char c1 = val.at(i+1); + char c0 = val.at(i+2); + i += 2; + + if (!isdigit(c2) || !isdigit(c1) || !isdigit(c0)) { + get_manager().raise_exception("invalid octal escape: exactly three octal digits required"); + } + + if (c2 == '8' || c2 == '9' || c1 == '8' || c1 == '9' || c0 == '8' || c0 == '9') { + get_manager().raise_exception("invalid octal escape: exactly three octal digits required"); + } + + char tmp[4] = {c2, c1, c0, '\0'}; + long converted = strtol(tmp, NULL, 8); + unsigned char convChar = (unsigned char)converted; + parsedStr.push_back(convChar); } else { // unrecognized escape sequence -- just emit that character parsedStr.push_back(escapeChar1); From 179b0f763095201bda085456bb05d2fa209b298a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 21 Feb 2017 19:52:27 -0500 Subject: [PATCH 341/562] clean up todos theory_str --- src/ast/rewriter/str_rewriter.cpp | 5 -- src/smt/theory_str.cpp | 109 +++++------------------------- src/smt/theory_str.h | 4 -- 3 files changed, 17 insertions(+), 101 deletions(-) diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index 3926e66e1..045d06b97 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -38,7 +38,6 @@ void nfa::convert_re(expr * e, unsigned & start, unsigned & end, str_util & m_st std::string str = m_strutil.get_string_constant_value(arg_str); TRACE("t_str_rw", tout << "build NFA for '" << str << "'" << std::endl;); - // TODO this assumes the string is not empty /* * For an n-character string, we make (n-1) intermediate states, * labelled i_(0) through i_(n-2). @@ -219,7 +218,6 @@ br_status str_rewriter::mk_str_CharAt(expr * arg0, expr * arg1, expr_ref & resul result = m_strutil.mk_string(resultStr); return BR_DONE; } else { - // TODO if we ever figure out how to assert axioms in here, add the axiom code from Z3str2's strAstReduce.cpp return BR_FAILED; } } @@ -399,7 +397,6 @@ br_status str_rewriter::mk_str_to_int(expr * arg0, expr_ref & result) { // interpret str as a natural number and rewrite to the corresponding integer. // if this is not valid, rewrite to -1 - // TODO leading zeroes? rational convertedRepresentation(0); rational ten(10); for (unsigned i = 0; i < str.length(); ++i) { @@ -692,13 +689,11 @@ br_status str_rewriter::mk_eq_core(expr * l, expr * r, expr_ref & result) { } bool str_rewriter::reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change) { - // TODO inspect seq_rewriter::reduce_eq() change = false; return true; } bool str_rewriter::reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change) { - // TODO inspect seq_rewriter::reduce_eq() change = false; return true; } diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 3673d3e79..84295940a 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -549,7 +549,6 @@ app * theory_str::mk_regex_rep_var() { TRACE("t_str_axiom_bug", tout << "add " << mk_pp(a, m) << " to m_basicstr_axiom_todo" << std::endl;); m_trail.push_back(a); - // TODO cross-check which variable sets we need variable_set.insert(a); //internal_variable_set.insert(a); regex_variable_set.insert(a); @@ -1215,7 +1214,6 @@ void theory_str::instantiate_axiom_Contains(enode * e) { // quick path, because this is necessary due to rewriter behaviour // (at minimum it should fix z3str/concat-006.smt2 - // TODO: see if it's necessary for other such terms if (m_strutil.is_string(ex->get_arg(0)) && m_strutil.is_string(ex->get_arg(1))) { TRACE("t_str_detail", tout << "eval constant Contains term " << mk_pp(ex, m) << std::endl;); std::string haystackStr = m_strutil.get_string_constant_value(ex->get_arg(0)); @@ -1541,7 +1539,6 @@ void theory_str::instantiate_axiom_Substr(enode * e) { expr_ref ts0_contains_ts1(mk_contains(expr->get_arg(0), ts1), m); expr_ref_vector and_item(m); - // TODO simulate this contains check; it causes problems with a few regressions but we might need it for performance //and_item.push_back(ts0_contains_ts1); and_item.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(ts0, mk_concat(ts1, ts2)))); and_item.push_back(ctx.mk_eq_atom(expr->get_arg(1), mk_strlen(ts0))); @@ -1782,7 +1779,6 @@ void theory_str::reset_eh() { m_basicstr_axiom_todo.reset(); m_str_eq_todo.reset(); m_concat_axiom_todo.reset(); - // TODO reset a loooooot more internal stuff pop_scope_eh(get_context().get_scope_level()); } @@ -2377,7 +2373,7 @@ void theory_str::infer_len_concat_arg(expr * n, rational len) { if (arg0Len.is_nonneg()) { axr = ctx.mk_eq_atom(mk_strlen(arg0), mk_int(arg0Len)); } else { - // TODO negate? + // could negate } } else if (arg0_len_exists && !arg1_len_exists) { //if (mk_length(t, arg0) != mk_int(ctx, arg0_len)) { @@ -2388,7 +2384,7 @@ void theory_str::infer_len_concat_arg(expr * n, rational len) { if (arg1Len.is_nonneg()) { axr = ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1Len)); } else { - // TODO negate? + // could negate } } else { @@ -3252,7 +3248,6 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } else { loopDetected = true; if (m_params.m_FiniteOverlapModels) { - // TODO this might repeat the case above, we may wish to avoid doing this twice expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); arrangement_disjunction.push_back(tester); add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); @@ -5556,7 +5551,7 @@ void theory_str::get_grounded_concats(expr* node, std::map & varAl else { std::vector concatNodes; concatNodes.push_back(node); - groundedMap[node][concatNodes]; // TODO ??? + groundedMap[node][concatNodes]; } } } @@ -6653,7 +6648,6 @@ void theory_str::finite_model_test(expr * testvar, expr * str) { v_lower_bound == rational::zero(); } else if (lower_bound_exists && !upper_bound_exists) { // check some finite portion of the search space - // TODO here and below, factor out the increment to a param v_upper_bound = v_lower_bound + rational(10); } else { // no bounds information @@ -6678,7 +6672,6 @@ void theory_str::finite_model_test(expr * testvar, expr * str) { expr_ref_vector andList(m); for (rational l = v_lower_bound; l <= v_upper_bound; l += rational::one()) { - // TODO integrate with the enhancements in gen_len_test_options() std::string lStr = l.to_string(); expr_ref str_indicator(m_strutil.mk_string(lStr), m); expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); @@ -6718,7 +6711,6 @@ void theory_str::more_value_tests(expr * valTester, std::string valTesterValue) if (m_params.m_UseBinarySearch) { if (!binary_search_len_tester_stack.contains(fVar) || binary_search_len_tester_stack[fVar].empty()) { TRACE("t_str_binary_search", tout << "WARNING: no active length testers for " << mk_pp(fVar, m) << std::endl;); - // TODO handle this? NOT_IMPLEMENTED_YET(); } expr * effectiveLenInd = binary_search_len_tester_stack[fVar].back(); @@ -6803,7 +6795,6 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { /* // temporarily disabled, we are borrowing these testers for something else if (m_params.m_FiniteOverlapModels && !finite_model_test_varlists.empty()) { - // TODO NEXT if (finite_model_test_varlists.contains(lhs)) { finite_model_test(lhs, rhs); return; } else if (finite_model_test_varlists.contains(rhs)) { @@ -6879,8 +6870,6 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { } } - // TODO some setup with haveEQLength() which I skip for now, not sure if necessary - instantiate_str_eq_length_axiom(ctx.get_enode(lhs), ctx.get_enode(rhs)); // group terms by equivalence class (groupNodeInEqc()) @@ -7291,7 +7280,6 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { sLevel -= num_scopes; TRACE("t_str", tout << "pop " << num_scopes << " to " << sLevel << std::endl;); - // TODO: figure out what's going out of scope and why context & ctx = get_context(); ast_manager & m = get_manager(); @@ -7315,7 +7303,7 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { TRACE("t_str_cut_var_map", tout << "remove cut info for " << mk_pp(e, m) << std::endl; print_cut_var(e, tout);); T_cut * aCut = val.top(); val.pop(); - // dealloc(aCut); // TODO find a safer way to do this, it is causing a crash + // dealloc(aCut); } if (val.size() == 0) { cutvarmap_removes.insert(varItor->m_key); @@ -7331,30 +7319,6 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { } } - /* - // see if any internal variables went out of scope - for (int check_level = sLevel + num_scopes ; check_level > sLevel; --check_level) { - TRACE("t_str_detail", tout << "cleaning up internal variables at scope level " << check_level << std::endl;); - std::map >::iterator it = internal_variable_scope_levels.find(check_level); - if (it != internal_variable_scope_levels.end()) { - unsigned count = 0; - std::set vars = it->second; - for (std::set::iterator var_it = vars.begin(); var_it != vars.end(); ++var_it) { - TRACE("t_str_detail", tout << "clean up variable " << mk_pp(*var_it, get_manager()) << std::endl;); - variable_set.erase(*var_it); - internal_variable_set.erase(*var_it); - regex_variable_set.erase(*var_it); - internal_unrollTest_vars.erase(*var_it); - count += 1; - } - TRACE("t_str_detail", tout << "cleaned up " << count << " variables" << std::endl;); - vars.clear(); - } - } - */ - - // TODO use the trail stack to do this for us! requires lots of refactoring - // TODO if this works, possibly remove axioms from other vectors as well ptr_vector new_m_basicstr; for (ptr_vector::iterator it = m_basicstr_axiom_todo.begin(); it != m_basicstr_axiom_todo.end(); ++it) { enode * e = *it; @@ -7732,7 +7696,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::mapfirst; do { - if (variable_set.find(curr) != variable_set.end()) { // TODO internal_variable_set? + if (variable_set.find(curr) != variable_set.end()) { if (aRoot == NULL) { aRoot = curr; } else { @@ -8280,7 +8244,6 @@ bool theory_str::finalcheck_str2int(app * a) { TRACE("t_str_detail", tout << "integer theory has no assignment for " << mk_pp(a, m) << std::endl;); NOT_IMPLEMENTED_YET(); } - // TODO also check assignment in string theory return axiomAdd; } @@ -8303,7 +8266,6 @@ bool theory_str::finalcheck_int2str(app * a) { // ignore this. we should already assert the axiom for what happens when the string is "" } else { // nonempty string --> convert to correct integer value, or disallow it - // TODO think about whether we need to persist the axiom in this case? rational convertedRepresentation(0); rational ten(10); bool conversionOK = true; @@ -8341,7 +8303,6 @@ bool theory_str::finalcheck_int2str(app * a) { TRACE("t_str_detail", tout << "string theory has no assignment for " << mk_pp(a, m) << std::endl;); NOT_IMPLEMENTED_YET(); } - // TODO also check assignment in integer theory return axiomAdd; } @@ -8503,7 +8464,6 @@ final_check_status theory_str::final_check_eh() { context & ctx = get_context(); ast_manager & m = get_manager(); - // TODO out-of-scope term debugging, see comment in pop_scope_eh() expr_ref_vector assignments(m); ctx.get_assignments(assignments); @@ -8811,7 +8771,6 @@ final_check_status theory_str::final_check_eh() { expr * freeVar = freeVarItor1->first; rational lenValue; bool lenValue_exists = get_len_value(freeVar, lenValue); - // TODO get_bound_strlen() tout << mk_pp(freeVar, m) << " [depCnt = " << freeVarItor1->second << ", length = " << (lenValue_exists ? lenValue.to_string() : "?") << "]" << std::endl; @@ -8842,7 +8801,6 @@ final_check_status theory_str::final_check_eh() { continue; } */ - // TODO if this variable represents a regular expression, continue expr * toAssert = gen_len_val_options_for_free_var(freeVar, NULL, ""); if (toAssert != NULL) { assert_axiom(toAssert); @@ -9026,12 +8984,10 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * // ---------------------------------------------------------------------------------------- - // TODO refactor this and below to use expr_ref_vector instead of ptr_vector/svect ptr_vector orList; ptr_vector andList; for (long long i = l; i < h; i++) { - // TODO can we share the val_indicator constants with the length tester cache? orList.push_back(m.mk_eq(val_indicator, mk_string(longlong_to_string(i).c_str()) )); if (m_params.m_AggressiveValueTesting) { literal l = mk_eq(val_indicator, mk_string(longlong_to_string(i).c_str()), false); @@ -9448,7 +9404,6 @@ expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & // handle out-of-scope entries in unroll_tries_map ptr_vector outOfScopeTesters; - // TODO refactor unroll_tries_map and internal_unrollTest_vars to use m_trail_stack for (ptr_vector::iterator it = unroll_tries_map[var][unrolls].begin(); it != unroll_tries_map[var][unrolls].end(); ++it) { @@ -9807,10 +9762,8 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT lastTesterConstant = previousLenTesterValue; TRACE("t_str_binary_search", tout << "invoked with previousLenTester info matching top of stack" << std::endl;); } else { - // this is a bit unexpected TRACE("t_str_binary_search", tout << "WARNING: unexpected reordering of length testers!" << std::endl;); - // TODO resolve this case - NOT_IMPLEMENTED_YET(); return NULL; + UNREACHABLE(); return NULL; } } else { lastTesterConstant = m_strutil.get_string_constant_value(lastTesterValue); @@ -9822,8 +9775,7 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT if (!binary_search_len_tester_info.find(lastTester, lastBounds)) { // unexpected TRACE("t_str_binary_search", tout << "WARNING: no bounds information available for last tester!" << std::endl;); - // TODO resolve this - NOT_IMPLEMENTED_YET(); + UNREACHABLE(); } TRACE("t_str_binary_search", tout << "last bounds are [" << lastBounds.lowerBound << " | " << lastBounds.midPoint << " | " << lastBounds.upperBound << "]!" << lastBounds.windowSize << std::endl;); binary_search_info newBounds; @@ -9833,13 +9785,12 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT // we double the window size and adjust the bounds if (lastBounds.midPoint == lastBounds.upperBound && lastBounds.upperBound == lastBounds.windowSize) { TRACE("t_str_binary_search", tout << "search hit window size; expanding" << std::endl;); - // TODO is this correct? newBounds.lowerBound = lastBounds.windowSize + rational::one(); newBounds.windowSize = lastBounds.windowSize * rational(2); newBounds.upperBound = newBounds.windowSize; newBounds.calculate_midpoint(); } else if (false) { - // TODO handle the case where the midpoint can't be increased further + // handle the case where the midpoint can't be increased further // (e.g. a window like [50 | 50 | 50]!64 and we don't answer "50") } else { // general case @@ -9855,7 +9806,7 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT refresh_theory_var(newTester); } else if (lastTesterConstant == "less") { if (false) { - // TODO handle the case where the midpoint can't be decreased further + // handle the case where the midpoint can't be decreased further // (e.g. a window like [0 | 0 | 0]!64 and we don't answer "0" } else { // general case @@ -9888,8 +9839,7 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT if (!binary_search_len_tester_info.find(lastTester, lastBounds)) { // unexpected TRACE("t_str_binary_search", tout << "WARNING: no bounds information available for last tester!" << std::endl;); - // TODO resolve this - NOT_IMPLEMENTED_YET(); + UNREACHABLE(); } if (lastBounds.midPoint.is_neg()) { TRACE("t_str_binary_search", tout << "WARNING: length search converged on a negative value. Negating this constraint." << std::endl;); @@ -9960,7 +9910,6 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe if (!map_effectively_empty) { // check whether any entries correspond to variables that went out of scope; // if every entry is out of scope then the map counts as being empty - // TODO: maybe remove them from the map instead? either here or in pop_scope_eh() // assume empty and find a counterexample map_effectively_empty = true; @@ -10059,7 +10008,6 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe if (effectiveHasEqcValue) { effectiveLenIndiStr = m_strutil.get_string_constant_value(effective_eqc_value); } else { - // TODO this should be unreachable, but can we really do anything here? NOT_IMPLEMENTED_YET(); } } @@ -10091,7 +10039,6 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe fvar_lenTester_map[freeVar].push_back(indicator); lenTester_fvar_map.insert(indicator, freeVar); } else { - // TODO make absolutely sure this is safe to do if 'indicator' is technically out of scope indicator = fvar_lenTester_map[freeVar][i]; refresh_theory_var(indicator); testNum = i + 1; @@ -10211,35 +10158,13 @@ void theory_str::process_free_var(std::map & freeVar_map) { } } - // TODO here's a great place for debugging info - - // testing: iterate over leafVarSet deterministically - if (false) { - // *** TESTING CODE - std::vector sortedLeafVarSet; - for (std::set::iterator itor1 = leafVarSet.begin(); itor1 != leafVarSet.end(); ++itor1) { - sortedLeafVarSet.push_back(*itor1); - } - std::sort(sortedLeafVarSet.begin(), sortedLeafVarSet.end(), cmpvarnames); - for(std::vector::iterator itor1 = sortedLeafVarSet.begin(); - itor1 != sortedLeafVarSet.end(); ++itor1) { - expr * toAssert = gen_len_val_options_for_free_var(*itor1, NULL, ""); - // gen_len_val_options_for_free_var() can legally return NULL, - // as methods that it calls may assert their own axioms instead. - if (toAssert != NULL) { - assert_axiom(toAssert); - } - } - } else { - // *** CODE FROM BEFORE - for(std::set::iterator itor1 = leafVarSet.begin(); - itor1 != leafVarSet.end(); ++itor1) { - expr * toAssert = gen_len_val_options_for_free_var(*itor1, NULL, ""); - // gen_len_val_options_for_free_var() can legally return NULL, - // as methods that it calls may assert their own axioms instead. - if (toAssert != NULL) { - assert_axiom(toAssert); - } + for(std::set::iterator itor1 = leafVarSet.begin(); + itor1 != leafVarSet.end(); ++itor1) { + expr * toAssert = gen_len_val_options_for_free_var(*itor1, NULL, ""); + // gen_len_val_options_for_free_var() can legally return NULL, + // as methods that it calls may assert their own axioms instead. + if (toAssert != NULL) { + assert_axiom(toAssert); } } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index f81b4ada7..6b1ce9023 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -30,8 +30,6 @@ Revision History: #include"str_rewriter.h" #include"union_find.h" -// TODO refactor: anything that returns an expr* instead returns an expr_ref - namespace smt { class str_value_factory : public value_factory { @@ -256,7 +254,6 @@ namespace smt { theory_str_contain_pair_bool_map_t contain_pair_bool_map; //obj_map > contain_pair_idx_map; - // TODO Find a better data structure, this is 100% a hack right now std::map > > contain_pair_idx_map; std::map, expr*> regex_in_bool_map; @@ -458,7 +455,6 @@ namespace smt { void check_contain_by_substr(expr * varNode, expr_ref_vector & willEqClass); void check_contain_by_eq_nodes(expr * n1, expr * n2); bool in_contain_idx_map(expr * n); - // TODO refactor these methods to use expr_ref_vector instead of std::vector void compute_contains(std::map & varAliasMap, std::map & concatAliasMap, std::map & varConstMap, std::map & concatConstMap, std::map > & varEqConcatMap); From cff7c450c381067402334cb8f10482f9f78cbaba Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 23 Feb 2017 14:57:48 -0500 Subject: [PATCH 342/562] refactor: uint_set --- src/smt/smt_context.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index edc122bd6..e23ecaf43 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -230,8 +230,7 @@ namespace smt { // Theory case split // // ----------------------------------- - typedef int_hashtable > int_set; - int_set m_all_th_case_split_literals; + uint_set m_all_th_case_split_literals; vector m_th_case_split_sets; u_map< vector > m_literal2casesplitsets; // returns the case split literal sets that a literal participates in unsigned m_th_case_split_qhead; From 5107e5cafc2dd74d2627de7f9986ba1c88702532 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 23 Feb 2017 15:01:55 -0500 Subject: [PATCH 343/562] refactor: remove t_str_refcount_hack traces --- src/ast/ast.cpp | 1 - src/smt/smt_context.cpp | 8 -------- src/smt/theory_str.cpp | 15 --------------- 3 files changed, 24 deletions(-) diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 37ea297c2..7271048b1 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -1829,7 +1829,6 @@ void ast_manager::delete_node(ast * n) { TRACE("ast", tout << "Deleting object " << n->m_id << " " << n << "\n";); CTRACE("del_quantifier", is_quantifier(n), tout << "deleting quantifier " << n->m_id << " " << n << "\n";); TRACE("mk_var_bug", tout << "del_ast: " << n->m_id << "\n";); - TRACE("t_str_refcount_hack", tout << "delete ast " << n->m_id << std::endl;); TRACE("ast_delete_node", tout << mk_bounded_pp(n, *this) << "\n";); SASSERT(m_ast_table.contains(n)); diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 7532bf3f8..29321ecf7 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -2403,9 +2403,6 @@ namespace smt { */ unsigned context::pop_scope_core(unsigned num_scopes) { - TRACE("t_str_refcount_hack", tout << "begin pop_scope_core in smt_context" << std::endl;); - - if (m_manager.has_trace_stream()) m_manager.trace_stream() << "[pop] " << num_scopes << " " << m_scope_lvl << "\n"; @@ -2453,9 +2450,7 @@ namespace smt { ptr_vector::iterator it = m_theory_set.begin(); ptr_vector::iterator end = m_theory_set.end(); for (; it != end; ++it) { - TRACE("t_str_refcount_hack", tout << "begin theory pop_scope_eh" << std::endl;); (*it)->pop_scope_eh(num_scopes); - TRACE("t_str_refcount_hack", tout << "end theory pop_scope_eh" << std::endl;); } del_justifications(m_justifications, s.m_justifications_lim); @@ -2482,9 +2477,6 @@ namespace smt { reassert_units(units_to_reassert_lim); TRACE("pop_scope_detail", tout << "end of pop_scope: \n"; display(tout);); CASSERT("context", check_invariant()); - - TRACE("t_str_refcount_hack", tout << "end pop_scope_core in smt_context" << std::endl;); - return num_bool_vars; } diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 84295940a..b78cbbe59 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1793,7 +1793,6 @@ void theory_str::reset_eh() { * Then add an assertion: (y2 == (Concat ce m2)) AND ("str3" == (Concat abc x2)) -> (y2 != "str3") */ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { - TRACE("t_str_refcount_hack", tout << "begin new_eq_check in theory_str" << std::endl;); context & ctx = get_context(); ast_manager & m = get_manager(); @@ -1819,7 +1818,6 @@ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { expr_ref to_assert(m.mk_not(ctx.mk_eq_atom(eqc_nn1, eqc_nn2)), m); assert_axiom(to_assert); // this shouldn't use the integer theory at all, so we don't allow the option of quick-return - TRACE("t_str_refcount_hack", tout << "end new_eq_check in theory_str" << std::endl;); return false; } if (!check_length_consistency(eqc_nn1, eqc_nn2)) { @@ -1827,7 +1825,6 @@ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { if (opt_NoQuickReturn_IntegerTheory){ TRACE("t_str_detail", tout << "continuing in new_eq_check() due to opt_NoQuickReturn_IntegerTheory" << std::endl;); } else { - TRACE("t_str_refcount_hack", tout << "end new_eq_check in theory_str" << std::endl;); return false; } } @@ -1846,7 +1843,6 @@ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { } // okay, all checks here passed - TRACE("t_str_refcount_hack", tout << "end new_eq_check in theory_str" << std::endl;); return true; } @@ -7276,20 +7272,11 @@ void theory_str::check_variable_scope() { } void theory_str::pop_scope_eh(unsigned num_scopes) { - TRACE("t_str_refcount_hack", tout << "begin pop_scope_eh in theory_str" << std::endl;); - sLevel -= num_scopes; TRACE("t_str", tout << "pop " << num_scopes << " to " << sLevel << std::endl;); context & ctx = get_context(); ast_manager & m = get_manager(); - // { - // expr_ref_vector assignments(m); - // ctx.get_assignments(assignments); - // TRACE("t_str_refcount_hack", tout << "assignment vector about to go out of scope" << std::endl;); - // } - // TRACE("t_str_refcount_hack", tout << "assignment vector has gone out of scope" << std::endl;); - TRACE_CODE(if (is_trace_enabled("t_str_dump_assign_on_scope_change")) { dump_assignments(); }); // list of expr* to remove from cut_var_map @@ -7337,8 +7324,6 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { theory::pop_scope_eh(num_scopes); //check_variable_scope(); - - TRACE("t_str_refcount_hack", tout << "end pop_scope_eh in theory_str" << std::endl;); } void theory_str::dump_assignments() { From 858c754b15ab57b4050e328173193492bcf8a0f7 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 23 Feb 2017 15:05:43 -0500 Subject: [PATCH 344/562] refactor: remove unused variable in smt_case_split_queue --- src/smt/smt_case_split_queue.cpp | 46 +++++++++++++------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/src/smt/smt_case_split_queue.cpp b/src/smt/smt_case_split_queue.cpp index c7ef655f2..67f370da0 100644 --- a/src/smt/smt_case_split_queue.cpp +++ b/src/smt/smt_case_split_queue.cpp @@ -1351,35 +1351,25 @@ namespace smt { p.m_case_split_strategy = CS_ACTIVITY; } - case_split_queue * baseQueue; - - if (p.m_theory_aware_branching) { - // override - baseQueue = alloc(theory_aware_branching_queue, ctx, p); - } else { - switch (p.m_case_split_strategy) { - case CS_ACTIVITY_DELAY_NEW: - baseQueue = alloc(dact_case_split_queue, ctx, p); - break; - case CS_ACTIVITY_WITH_CACHE: - baseQueue = alloc(cact_case_split_queue, ctx, p); - break; - case CS_RELEVANCY: - baseQueue = alloc(rel_case_split_queue, ctx, p); - break; - case CS_RELEVANCY_ACTIVITY: - baseQueue = alloc(rel_act_case_split_queue, ctx, p); - break; - case CS_RELEVANCY_GOAL: - baseQueue = alloc(rel_goal_case_split_queue, ctx, p); - break; - default: - baseQueue = alloc(act_case_split_queue, ctx, p); - break; + if (p.m_theory_aware_branching) { + // override + return alloc(theory_aware_branching_queue, ctx, p); + } else { + switch (p.m_case_split_strategy) { + case CS_ACTIVITY_DELAY_NEW: + return alloc(dact_case_split_queue, ctx, p); + case CS_ACTIVITY_WITH_CACHE: + return alloc(cact_case_split_queue, ctx, p); + case CS_RELEVANCY: + return alloc(rel_case_split_queue, ctx, p); + case CS_RELEVANCY_ACTIVITY: + return alloc(rel_act_case_split_queue, ctx, p); + case CS_RELEVANCY_GOAL: + return alloc(rel_goal_case_split_queue, ctx, p); + default: + return alloc(act_case_split_queue, ctx, p); + } } - } - - return baseQueue; } }; From 6387d59f5c37bb7cbc981dd9513457b33fc7a37f Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 23 Feb 2017 15:08:05 -0500 Subject: [PATCH 345/562] refactor: remove commented-out code --- src/smt/smt_case_split_queue.cpp | 113 ------------------------------- 1 file changed, 113 deletions(-) diff --git a/src/smt/smt_case_split_queue.cpp b/src/smt/smt_case_split_queue.cpp index 67f370da0..6cdfed7ea 100644 --- a/src/smt/smt_case_split_queue.cpp +++ b/src/smt/smt_case_split_queue.cpp @@ -1112,119 +1112,6 @@ namespace smt { } }; - /* - class theory_aware_branching_queue : public case_split_queue { - protected: - context & m_context; - smt_params & m_params; - - theory_var_priority_map m_theory_var_priority; - theory_aware_act_queue m_theory_queue; - case_split_queue * m_base_queue; - int_hashtable > m_theory_vars; - map > m_theory_var_phase; - public: - theory_aware_branching_queue(context & ctx, smt_params & p, case_split_queue * base_queue) : - m_context(ctx), - m_params(p), - m_theory_var_priority(), - m_theory_queue(1024, theory_aware_act_lt(m_theory_var_priority)), - m_base_queue(base_queue) { - } - - virtual void activity_increased_eh(bool_var v) { - m_base_queue->activity_increased_eh(v); - } - - virtual void mk_var_eh(bool_var v) { - // do nothing. we only "react" if/when we learn this is an important theory literal - m_base_queue->mk_var_eh(v); - } - - virtual void del_var_eh(bool_var v) { - if (m_theory_queue.contains(v)) { - m_theory_queue.erase(v); - } - m_base_queue->del_var_eh(v); - } - - virtual void assign_lit_eh(literal l) { - m_base_queue->assign_lit_eh(l); - } - - virtual void unassign_var_eh(bool_var v) { - if (m_theory_vars.contains(v) && !m_theory_queue.contains(v)) { - m_theory_queue.insert(v); - } - m_base_queue->unassign_var_eh(v); - } - - virtual void relevant_eh(expr * n) { - m_base_queue->relevant_eh(n); - } - - virtual void init_search_eh() { - m_base_queue->init_search_eh(); - } - - virtual void end_search_eh() { - m_base_queue->end_search_eh(); - } - - virtual void internalize_instance_eh(expr * e, unsigned gen) { - m_base_queue->internalize_instance_eh(e, gen); - } - - virtual void reset() { - m_theory_queue.reset(); - m_theory_vars.reset(); - m_theory_var_phase.reset(); - m_theory_var_priority.reset(); - m_base_queue->reset(); - } - - virtual void push_scope() { - m_base_queue->push_scope(); - } - - virtual void pop_scope(unsigned num_scopes) { - m_base_queue->pop_scope(num_scopes); - } - - virtual void next_case_split(bool_var & next, lbool & phase) { - while (!m_theory_queue.empty()) { - next = m_theory_queue.erase_min(); - // if this literal is unassigned, it is the theory literal with the highest priority, - // so case split on this - if (m_context.get_assignment(next) == l_undef) { - TRACE("theory_aware_branching", tout << "Theory-aware branch on l#" << next << std::endl;); - if (!m_theory_var_phase.find(next, phase)) { - phase = l_undef; - } - return; - } - } - // if we reach this point, the theory literal queue is empty, - // so fall back to the base queue - m_base_queue->next_case_split(next, phase); - } - - virtual void add_theory_aware_branching_info(bool_var v, double priority, lbool phase) { - TRACE("theory_aware_branching", tout << "Add theory-aware branching information for l#" << v << ": priority=" << priority << std::endl;); - m_theory_vars.insert(v); - m_theory_var_phase.insert(v, phase); - m_theory_var_priority.insert(v, priority); - m_theory_queue.reserve(v+1); - m_theory_queue.insert(v); - } - - virtual void display(std::ostream & out) { - // TODO - m_base_queue->display(out); - } - }; - */ - class theory_aware_branching_queue : public case_split_queue { protected: context & m_context; From 3816779ba12c24fdfd05d9a244101aca1f712b35 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 23 Feb 2017 15:25:20 -0500 Subject: [PATCH 346/562] fix indent --- src/smt/smt_case_split_queue.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/smt/smt_case_split_queue.cpp b/src/smt/smt_case_split_queue.cpp index 6cdfed7ea..35cdcb6fe 100644 --- a/src/smt/smt_case_split_queue.cpp +++ b/src/smt/smt_case_split_queue.cpp @@ -46,8 +46,8 @@ namespace smt { bool operator()(bool_var v1, bool_var v2) const { double p_v1, p_v2; if (!m_theory_var_priority.find(v1, p_v1)) { - p_v1 = 0.0; - } + p_v1 = 0.0; + } if (!m_theory_var_priority.find(v2, p_v2)) { p_v2 = 0.0; } From a7b21dc5d51c3256ecadccc499b8b310af2ab31e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 23 Feb 2017 16:00:05 -0500 Subject: [PATCH 347/562] refactor: aligned external/internal names for str.strong_arrangements option --- src/smt/params/theory_str_params.cpp | 2 +- src/smt/params/theory_str_params.h | 4 ++-- src/smt/theory_str.cpp | 36 ++++++++++++++-------------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/smt/params/theory_str_params.cpp b/src/smt/params/theory_str_params.cpp index f86cd9379..6090086b8 100644 --- a/src/smt/params/theory_str_params.cpp +++ b/src/smt/params/theory_str_params.cpp @@ -20,7 +20,7 @@ Revision History: void theory_str_params::updt_params(params_ref const & _p) { smt_params_helper p(_p); - m_AssertStrongerArrangements = p.str_strong_arrangements(); + m_StrongArrangements = p.str_strong_arrangements(); m_AggressiveLengthTesting = p.str_aggressive_length_testing(); m_AggressiveValueTesting = p.str_aggressive_value_testing(); m_AggressiveUnrollTesting = p.str_aggressive_unroll_testing(); diff --git a/src/smt/params/theory_str_params.h b/src/smt/params/theory_str_params.h index de0945395..207b635d7 100644 --- a/src/smt/params/theory_str_params.h +++ b/src/smt/params/theory_str_params.h @@ -28,7 +28,7 @@ struct theory_str_params { * This is a stronger version of the standard axiom. * The Z3str2 axioms can be simulated by setting this to false. */ - bool m_AssertStrongerArrangements; + bool m_StrongArrangements; /* * If AggressiveLengthTesting is true, we manipulate the phase of length tester equalities @@ -81,7 +81,7 @@ struct theory_str_params { double m_OverlapTheoryAwarePriority; theory_str_params(params_ref const & p = params_ref()): - m_AssertStrongerArrangements(true), + m_StrongArrangements(true), m_AggressiveLengthTesting(false), m_AggressiveValueTesting(false), m_AggressiveUnrollTesting(true), diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index b78cbbe59..80781d6aa 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -3079,7 +3079,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { add_cut_info_merge(t1, sLevel, m); add_cut_info_merge(t1, sLevel, y); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); assert_axiom(ax_strong); } else { @@ -3137,7 +3137,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { add_cut_info_merge(t2, sLevel, x); add_cut_info_merge(t2, sLevel, n); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); assert_axiom(ax_strong); } else { @@ -3272,7 +3272,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { if (!arrangement_disjunction.empty()) { expr_ref premise(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); expr_ref conclusion(mk_or(arrangement_disjunction), mgr); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(premise, conclusion), mgr); assert_axiom(ax_strong); } else { @@ -3472,7 +3472,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { add_cut_info_merge(temp1, sLevel, y); add_cut_info_merge(temp1, sLevel, m); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); assert_axiom(ax_strong); } else { @@ -3547,7 +3547,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { expr_ref ax_l(mk_and(l_items), mgr); expr_ref ax_r(mk_and(r_items), mgr); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); assert_axiom(ax_strong); } else { @@ -3628,7 +3628,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { if (!arrangement_disjunction.empty()) { expr_ref implyR(mk_or(arrangement_disjunction), mgr); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref implyLHS(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); expr_ref ax_strong(ctx.mk_eq_atom(implyLHS, implyR), mgr); assert_axiom(ax_strong); @@ -3816,7 +3816,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { r_items.push_back(ctx.mk_eq_atom(x, prefixAst)); r_items.push_back(ctx.mk_eq_atom(y, suf_n_concat)); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(ax_l, mk_and(r_items)), mgr); assert_axiom(ax_strong); } else { @@ -3836,7 +3836,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { expr_ref ax_l(mgr.mk_and(ax_l1, ax_l2), mgr); expr_ref ax_r(mgr.mk_and(ctx.mk_eq_atom(x, strAst), ctx.mk_eq_atom(y, n)), mgr); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); assert_axiom(ax_strong); } else { @@ -3874,7 +3874,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { add_cut_info_merge(temp1, sLevel, x); add_cut_info_merge(temp1, sLevel, n); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); assert_axiom(ax_strong); } else { @@ -3977,7 +3977,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { if (!arrangement_disjunction.empty()) { expr_ref implyR(mk_or(arrangement_disjunction), mgr); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref ax_lhs(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); expr_ref ax_strong(ctx.mk_eq_atom(ax_lhs, implyR), mgr); assert_axiom(ax_strong); @@ -4059,7 +4059,7 @@ void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { if (!in_same_eqc(tmpAst, n)) { // break down option 4-1 expr_ref implyR(ctx.mk_eq_atom(n, tmpAst), mgr); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); assert_axiom(ax_strong); } else { @@ -4071,7 +4071,7 @@ void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { //break down option 4-2 expr_ref implyR(ctx.mk_eq_atom(n, y), mgr); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); assert_axiom(ax_strong); } else { @@ -4084,7 +4084,7 @@ void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { if (!in_same_eqc(y, tmpAst)) { //break down option 4-3 expr_ref implyR(ctx.mk_eq_atom(y, tmpAst), mgr); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); assert_axiom(ax_strong); } else { @@ -4161,7 +4161,7 @@ void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { expr_ref x_deltaStr(mk_concat(x, mk_string(deltaStr)), mgr); if (!in_same_eqc(m, x_deltaStr)) { expr_ref implyR(ctx.mk_eq_atom(m, x_deltaStr), mgr); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); assert_axiom(ax_strong); } else { @@ -4172,7 +4172,7 @@ void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { // test if (!in_same_eqc(x, m)) { expr_ref implyR(ctx.mk_eq_atom(x, m), mgr); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); assert_axiom(ax_strong); } else { @@ -4184,7 +4184,7 @@ void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { expr_ref m_deltaStr(mk_concat(m, mk_string(deltaStr)), mgr); if (!in_same_eqc(x, m_deltaStr)) { expr_ref implyR(ctx.mk_eq_atom(x, m_deltaStr), mgr); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); assert_axiom(ax_strong); } else { @@ -4420,7 +4420,7 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { expr_ref implyR(mk_or(arrangement_disjunction), mgr); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); assert_axiom(ax_strong); } else { @@ -6515,7 +6515,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { assert_axiom(negate_ast); } else { implyR1 = mk_or(arrangement_disjunction); - if (m_params.m_AssertStrongerArrangements) { + if (m_params.m_StrongArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(implyL, implyR1), m); assert_axiom(ax_strong); } else { From 725352234d8f28e66845304aef8eaeb1ad353621 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 27 Feb 2017 13:22:56 -0500 Subject: [PATCH 348/562] refactoring theory_str --- src/api/api_ast.cpp | 16 --- src/api/api_context.cpp | 2 - src/api/api_context.h | 4 - src/api/api_str.cpp | 160 ------------------------------ src/api/z3_api.h | 2 - src/ast/ast_smt2_pp.h | 5 +- src/ast/ast_smt_pp.cpp | 5 - src/ast/reg_decl_plugins.cpp | 4 - src/ast/rewriter/str_rewriter.cpp | 3 + src/ast/rewriter/str_rewriter.h | 4 + src/ast/str_decl_plugin.cpp | 5 + src/ast/str_decl_plugin.h | 5 + src/cmd_context/check_logic.cpp | 7 +- src/cmd_context/cmd_context.cpp | 8 +- src/parsers/smt2/smt2parser.cpp | 27 ----- src/smt/theory_str.cpp | 137 ++++++++++--------------- src/smt/theory_str.h | 83 ++-------------- 17 files changed, 87 insertions(+), 390 deletions(-) delete mode 100644 src/api/api_str.cpp diff --git a/src/api/api_ast.cpp b/src/api/api_ast.cpp index f3b3b7edf..c9cdc6ab3 100644 --- a/src/api/api_ast.cpp +++ b/src/api/api_ast.cpp @@ -647,12 +647,6 @@ extern "C" { else if (fid == mk_c(c)->get_seq_fid() && k == RE_SORT) { return Z3_RE_SORT; } - else if (fid == mk_c(c)->get_str_fid() && k == STRING_SORT) { - return Z3_STRING_SORT; - } - else if (fid == mk_c(c)->get_str_fid() && k == REGEX_SORT) { - return Z3_REGEX_SORT; - } else { return Z3_UNKNOWN_SORT; } @@ -1147,16 +1141,6 @@ extern "C" { } } - if (mk_c(c)->get_str_fid() == _d->get_family_id()) { - switch (_d->get_decl_kind()) { - // TODO(z3str2) add others - case OP_STRCAT: return Z3_OP_STR_CONCAT; - case OP_STRLEN: return Z3_OP_STR_LENGTH; - default: - return Z3_OP_UNINTERPRETED; - } - } - if (mk_c(c)->get_fpa_fid() == _d->get_family_id()) { switch (_d->get_decl_kind()) { case OP_FPA_RM_NEAREST_TIES_TO_EVEN: return Z3_OP_FPA_RM_NEAREST_TIES_TO_EVEN; diff --git a/src/api/api_context.cpp b/src/api/api_context.cpp index fd3d16bd0..bcd3c60f2 100644 --- a/src/api/api_context.cpp +++ b/src/api/api_context.cpp @@ -81,7 +81,6 @@ namespace api { m_fpa_util(m()), m_dtutil(m()), m_sutil(m()), - m_strutil(m()), m_last_result(m()), m_ast_trail(m()), m_pmanager(m_limit) { @@ -105,7 +104,6 @@ namespace api { m_datalog_fid = m().mk_family_id("datalog_relation"); m_fpa_fid = m().mk_family_id("fpa"); m_seq_fid = m().mk_family_id("seq"); - m_str_fid = m().mk_family_id("str"); m_dt_plugin = static_cast(m().get_plugin(m_dt_fid)); install_tactics(*this); diff --git a/src/api/api_context.h b/src/api/api_context.h index 7459bd102..6e34f6d6e 100644 --- a/src/api/api_context.h +++ b/src/api/api_context.h @@ -26,7 +26,6 @@ Revision History: #include"arith_decl_plugin.h" #include"bv_decl_plugin.h" #include"seq_decl_plugin.h" -#include"str_decl_plugin.h" #include"datatype_decl_plugin.h" #include"dl_decl_plugin.h" #include"fpa_decl_plugin.h" @@ -63,8 +62,6 @@ namespace api { datatype_util m_dtutil; seq_util m_sutil; - str_util m_strutil; - // Support for old solver API smt_params m_fparams; // ------------------------------- @@ -130,7 +127,6 @@ namespace api { fpa_util & fpautil() { return m_fpa_util; } datatype_util& dtutil() { return m_dtutil; } seq_util& sutil() { return m_sutil; } - str_util& strutil() { return m_strutil; } family_id get_basic_fid() const { return m_basic_fid; } family_id get_array_fid() const { return m_array_fid; } family_id get_arith_fid() const { return m_arith_fid; } diff --git a/src/api/api_str.cpp b/src/api/api_str.cpp deleted file mode 100644 index eb56a839b..000000000 --- a/src/api/api_str.cpp +++ /dev/null @@ -1,160 +0,0 @@ -/*++ -Copyright (c) 2016 Microsoft Corporation - -Module Name: - - api_str.cpp - -Abstract: - - API for strings and regular expressions (Z3str2 implementation). - -Author: - - Murphy Berzish (mtrberzi) 2016-10-03. - -Revision History: - ---*/ -#include -#include"z3.h" -#include"api_log_macros.h" -#include"api_context.h" -#include"api_util.h" -#include"ast_pp.h" - -extern "C" { - - Z3_sort Z3_API Z3_mk_str_sort(Z3_context c) { - Z3_TRY; - LOG_Z3_mk_str_sort(c); - RESET_ERROR_CODE(); - sort * ty = mk_c(c)->strutil().mk_string_sort(); - mk_c(c)->save_ast_trail(ty); - RETURN_Z3(of_sort(ty)); - Z3_CATCH_RETURN(0); - } - - Z3_bool Z3_API Z3_is_str_sort(Z3_context c, Z3_sort s) { - Z3_TRY; - LOG_Z3_is_str_sort(c, s); - RESET_ERROR_CODE(); - bool result = mk_c(c)->strutil().is_str_sort(to_sort(s)); - return result?Z3_TRUE:Z3_FALSE; - Z3_CATCH_RETURN(Z3_FALSE); - } - - Z3_bool Z3_API Z3_is_str(Z3_context c, Z3_ast s) { - Z3_TRY; - LOG_Z3_is_str(c, s); - RESET_ERROR_CODE(); - bool result = mk_c(c)->strutil().is_string(to_expr(s)); - return result ? Z3_TRUE : Z3_FALSE; - Z3_CATCH_RETURN(Z3_FALSE); - } - - Z3_string Z3_API Z3_get_str(Z3_context c, Z3_ast s) { - Z3_TRY; - LOG_Z3_get_str(c, s); - RESET_ERROR_CODE(); - if (!mk_c(c)->strutil().is_string(to_expr(s))) { - SET_ERROR_CODE(Z3_INVALID_ARG); - return ""; - } - std::string result = mk_c(c)->strutil().get_string_constant_value(to_expr(s)); - return mk_c(c)->mk_external_string(result); - Z3_CATCH_RETURN(""); - } - - Z3_ast Z3_API Z3_mk_str(Z3_context c, Z3_string str) { - Z3_TRY; - LOG_Z3_mk_str(c, str); - RESET_ERROR_CODE(); - std::string s(str); - app * a = mk_c(c)->strutil().mk_string(str); - mk_c(c)->save_ast_trail(a); - RETURN_Z3(of_ast(a)); - Z3_CATCH_RETURN(0); - } - - MK_BINARY(Z3_mk_str_concat, mk_c(c)->get_str_fid(), OP_STRCAT, SKIP); - MK_UNARY(Z3_mk_str_length, mk_c(c)->get_str_fid(), OP_STRLEN, SKIP); - MK_BINARY(Z3_mk_str_at, mk_c(c)->get_str_fid(), OP_STR_CHARAT, SKIP); - // translate prefixof/suffixof to StartsWith/EndsWith - Z3_ast Z3_API Z3_mk_str_prefixof(Z3_context c, Z3_ast pre, Z3_ast full) { - LOG_Z3_mk_str_prefixof(c, pre, full); - Z3_TRY; - RESET_ERROR_CODE(); - expr * args[2] = { to_expr(full), to_expr(pre) }; // reverse args - ast * a = mk_c(c)->m().mk_app(mk_c(c)->get_str_fid(), OP_STR_STARTSWITH, 0, 0, 2, args); - mk_c(c)->save_ast_trail(a); - check_sorts(c, a); - RETURN_Z3(of_ast(a)); - Z3_CATCH_RETURN(0); - } - Z3_ast Z3_API Z3_mk_str_suffixof(Z3_context c, Z3_ast suf, Z3_ast full) { - LOG_Z3_mk_str_suffixof(c, suf, full); - Z3_TRY; - RESET_ERROR_CODE(); - expr * args[2] = { to_expr(full), to_expr(suf) }; // reverse args - ast * a = mk_c(c)->m().mk_app(mk_c(c)->get_str_fid(), OP_STR_ENDSWITH, 0, 0, 2, args); - mk_c(c)->save_ast_trail(a); - check_sorts(c, a); - RETURN_Z3(of_ast(a)); - Z3_CATCH_RETURN(0); - } - - MK_BINARY(Z3_mk_str_contains, mk_c(c)->get_str_fid(), OP_STR_CONTAINS, SKIP); - MK_TERNARY(Z3_mk_str_indexof, mk_c(c)->get_str_fid(), OP_STR_INDEXOF, SKIP); - MK_TERNARY(Z3_mk_str_substr, mk_c(c)->get_str_fid(), OP_STR_SUBSTR, SKIP); - MK_TERNARY(Z3_mk_str_replace, mk_c(c)->get_str_fid(), OP_STR_REPLACE, SKIP); - - Z3_ast Z3_API Z3_mk_str_to_regex(Z3_context c, Z3_string str) { - LOG_Z3_mk_str_to_regex(c, str); - Z3_TRY; - RESET_ERROR_CODE(); - std::string s(str); - app * a = mk_c(c)->strutil().mk_string(str); - mk_c(c)->save_ast_trail(a); - - expr * args[1] = { to_expr(a) }; - ast * re = mk_c(c)->m().mk_app(mk_c(c)->get_str_fid(), OP_RE_STR2REGEX, 0, 0, 1, args); - mk_c(c)->save_ast_trail(re); - check_sorts(c, re); - RETURN_Z3(of_ast(re)); - Z3_CATCH_RETURN(0); - } - - MK_BINARY(Z3_mk_str_in_regex, mk_c(c)->get_str_fid(), OP_RE_REGEXIN, SKIP); - MK_BINARY(Z3_mk_regex_concat, mk_c(c)->get_str_fid(), OP_RE_REGEXCONCAT, SKIP); - MK_BINARY(Z3_mk_regex_union, mk_c(c)->get_str_fid(), OP_RE_REGEXUNION, SKIP); - MK_UNARY(Z3_mk_regex_star, mk_c(c)->get_str_fid(), OP_RE_REGEXSTAR, SKIP); - MK_UNARY(Z3_mk_regex_plus, mk_c(c)->get_str_fid(), OP_RE_REGEXPLUS, SKIP); - - Z3_ast Z3_API Z3_mk_regex_range(Z3_context c, Z3_string start, Z3_string end) { - LOG_Z3_mk_regex_range(c, start, end); - Z3_TRY; - RESET_ERROR_CODE(); - - std::string cStart(start); - std::string cEnd(end); - if(cStart.length() != 1 || cEnd.length() != 1) { - SET_ERROR_CODE(Z3_INVALID_ARG); - return 0; - } - - app * a1 = mk_c(c)->strutil().mk_string(cStart); - mk_c(c)->save_ast_trail(a1); - app * a2 = mk_c(c)->strutil().mk_string(cEnd); - mk_c(c)->save_ast_trail(a2); - - expr * args[2] = { to_expr(a1), to_expr(a2) }; - ast * range = mk_c(c)->m().mk_app(mk_c(c)->get_str_fid(), OP_RE_REGEXCHARRANGE, 0, 0, 2, args); - mk_c(c)->save_ast_trail(range); - check_sorts(c, range); - RETURN_Z3(of_ast(range)); - - Z3_CATCH_RETURN(0); - } - -}; diff --git a/src/api/z3_api.h b/src/api/z3_api.h index aa4701f13..0b8351190 100644 --- a/src/api/z3_api.h +++ b/src/api/z3_api.h @@ -165,8 +165,6 @@ typedef enum Z3_ROUNDING_MODE_SORT, Z3_SEQ_SORT, Z3_RE_SORT, - Z3_STRING_SORT, - Z3_REGEX_SORT, Z3_UNKNOWN_SORT = 1000 } Z3_sort_kind; diff --git a/src/ast/ast_smt2_pp.h b/src/ast/ast_smt2_pp.h index 0bff579bc..b1bdf52bd 100644 --- a/src/ast/ast_smt2_pp.h +++ b/src/ast/ast_smt2_pp.h @@ -50,7 +50,6 @@ public: virtual array_util & get_arutil() = 0; virtual fpa_util & get_futil() = 0; virtual seq_util & get_sutil() = 0; - virtual str_util & get_strutil() = 0; virtual datalog::dl_decl_util& get_dlutil() = 0; virtual bool uses(symbol const & s) const = 0; virtual format_ns::format * pp_fdecl(func_decl * f, unsigned & len); @@ -77,17 +76,15 @@ class smt2_pp_environment_dbg : public smt2_pp_environment { array_util m_arutil; fpa_util m_futil; seq_util m_sutil; - str_util m_strutil; datalog::dl_decl_util m_dlutil; public: - smt2_pp_environment_dbg(ast_manager & m):m_manager(m), m_autil(m), m_bvutil(m), m_arutil(m), m_futil(m), m_sutil(m), m_strutil(m), m_dlutil(m) {} + smt2_pp_environment_dbg(ast_manager & m):m_manager(m), m_autil(m), m_bvutil(m), m_arutil(m), m_futil(m), m_sutil(m), m_dlutil(m) {} virtual ast_manager & get_manager() const { return m_manager; } virtual arith_util & get_autil() { return m_autil; } virtual bv_util & get_bvutil() { return m_bvutil; } virtual seq_util & get_sutil() { return m_sutil; } virtual array_util & get_arutil() { return m_arutil; } virtual fpa_util & get_futil() { return m_futil; } - virtual str_util & get_strutil() { return m_strutil; } virtual datalog::dl_decl_util& get_dlutil() { return m_dlutil; } virtual bool uses(symbol const & s) const { return false; } }; diff --git a/src/ast/ast_smt_pp.cpp b/src/ast/ast_smt_pp.cpp index 75e6a46f1..de6ae6cc3 100644 --- a/src/ast/ast_smt_pp.cpp +++ b/src/ast/ast_smt_pp.cpp @@ -165,7 +165,6 @@ class smt_printer { bv_util m_bvutil; seq_util m_sutil; fpa_util m_futil; - str_util m_strutil; family_id m_basic_fid; family_id m_bv_fid; family_id m_str_fid; @@ -473,9 +472,6 @@ class smt_printer { m_out << ") bv1[1])"; } } - else if (m_strutil.is_string(n, &str)) { - m_out << "\"" << str << "\""; - } else if (m_manager.is_label(n, pos, names) && names.size() >= 1) { if (m_is_smt2) { m_out << "(! "; @@ -839,7 +835,6 @@ public: m_bvutil(m), m_sutil(m), m_futil(m), - m_strutil(m), m_logic(logic), m_AUFLIRA("AUFLIRA"), // It's much easier to read those testcases with that. diff --git a/src/ast/reg_decl_plugins.cpp b/src/ast/reg_decl_plugins.cpp index 886e3f495..b4ff63ede 100644 --- a/src/ast/reg_decl_plugins.cpp +++ b/src/ast/reg_decl_plugins.cpp @@ -26,7 +26,6 @@ Revision History: #include"seq_decl_plugin.h" #include"pb_decl_plugin.h" #include"fpa_decl_plugin.h" -#include"str_decl_plugin.h" void reg_decl_plugins(ast_manager & m) { if (!m.get_plugin(m.mk_family_id(symbol("arith")))) { @@ -53,7 +52,4 @@ void reg_decl_plugins(ast_manager & m) { if (!m.get_plugin(m.mk_family_id(symbol("pb")))) { m.register_plugin(symbol("pb"), alloc(pb_decl_plugin)); } - if (!m.get_plugin(m.mk_family_id(symbol("str")))) { - m.register_plugin(symbol("str"), alloc(str_decl_plugin)); - } } diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp index 045d06b97..3933e7fdb 100644 --- a/src/ast/rewriter/str_rewriter.cpp +++ b/src/ast/rewriter/str_rewriter.cpp @@ -17,6 +17,8 @@ Notes: --*/ +#if 0 + #include"str_rewriter.h" #include"arith_decl_plugin.h" #include"ast_pp.h" @@ -698,3 +700,4 @@ bool str_rewriter::reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_ return true; } +#endif /* disable */ diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h index 0494d4d1b..8d6041a51 100644 --- a/src/ast/rewriter/str_rewriter.h +++ b/src/ast/rewriter/str_rewriter.h @@ -17,6 +17,8 @@ Notes: --*/ +#if 0 + #include"str_decl_plugin.h" #include"arith_decl_plugin.h" #include"rewriter_types.h" @@ -114,3 +116,5 @@ public: bool matches(std::string input); }; + +#endif /* disable */ diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp index 80493f3cf..067420f04 100644 --- a/src/ast/str_decl_plugin.cpp +++ b/src/ast/str_decl_plugin.cpp @@ -14,6 +14,9 @@ Author: Revision History: --*/ + +#if 0 + #include #include"str_decl_plugin.h" #include"string_buffer.h" @@ -494,3 +497,5 @@ std::string str_util::get_std_regex_str(expr * regex) { UNREACHABLE(); return ""; } } + +#endif /* disable */ diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h index 3ae034b45..28ecd1e43 100644 --- a/src/ast/str_decl_plugin.h +++ b/src/ast/str_decl_plugin.h @@ -14,6 +14,9 @@ Author: Revision History: --*/ + +#if 0 + #ifndef _STR_DECL_PLUGIN_H_ #define _STR_DECL_PLUGIN_H_ @@ -211,3 +214,5 @@ public: }; #endif /* _STR_DECL_PLUGIN_H_ */ + +#endif /* disable */ diff --git a/src/cmd_context/check_logic.cpp b/src/cmd_context/check_logic.cpp index 02f66fc4d..c75c12689 100644 --- a/src/cmd_context/check_logic.cpp +++ b/src/cmd_context/check_logic.cpp @@ -21,7 +21,6 @@ Revision History: #include"array_decl_plugin.h" #include"bv_decl_plugin.h" #include"seq_decl_plugin.h" -#include"str_decl_plugin.h" #include"pb_decl_plugin.h" #include"datatype_decl_plugin.h" #include"ast_pp.h" @@ -35,7 +34,6 @@ struct check_logic::imp { bv_util m_bv_util; array_util m_ar_util; seq_util m_seq_util; - str_util m_str_util; datatype_util m_dt_util; pb_util m_pb_util; bool m_uf; // true if the logic supports uninterpreted functions @@ -49,7 +47,7 @@ struct check_logic::imp { bool m_quantifiers; // true if the logic supports quantifiers bool m_unknown_logic; - imp(ast_manager & _m):m(_m), m_a_util(m), m_bv_util(m), m_ar_util(m), m_seq_util(m), m_str_util(m), m_dt_util(m), m_pb_util(m) { + imp(ast_manager & _m):m(_m), m_a_util(m), m_bv_util(m), m_ar_util(m), m_seq_util(m), m_dt_util(m), m_pb_util(m) { reset(); } @@ -444,9 +442,6 @@ struct check_logic::imp { else if (fid == m_seq_util.get_family_id()) { // nothing to check } - else if (fid == m_str_util.get_family_id()) { - // nothing to check - } else if (fid == m_dt_util.get_family_id() && m_logic == "QF_FD") { // nothing to check } diff --git a/src/cmd_context/cmd_context.cpp b/src/cmd_context/cmd_context.cpp index dc66f5da9..b387e8810 100644 --- a/src/cmd_context/cmd_context.cpp +++ b/src/cmd_context/cmd_context.cpp @@ -26,7 +26,6 @@ Notes: #include"seq_decl_plugin.h" #include"pb_decl_plugin.h" #include"fpa_decl_plugin.h" -#include"str_decl_plugin.h" #include"ast_pp.h" #include"var_subst.h" #include"pp.h" @@ -250,7 +249,6 @@ protected: array_util m_arutil; fpa_util m_futil; seq_util m_sutil; - str_util m_strutil; datalog::dl_decl_util m_dlutil; @@ -272,7 +270,7 @@ protected: } public: - pp_env(cmd_context & o):m_owner(o), m_autil(o.m()), m_bvutil(o.m()), m_arutil(o.m()), m_futil(o.m()), m_sutil(o.m()), m_strutil(o.m()), m_dlutil(o.m()) {} + pp_env(cmd_context & o):m_owner(o), m_autil(o.m()), m_bvutil(o.m()), m_arutil(o.m()), m_futil(o.m()), m_sutil(o.m()), m_dlutil(o.m()) {} virtual ~pp_env() {} virtual ast_manager & get_manager() const { return m_owner.m(); } virtual arith_util & get_autil() { return m_autil; } @@ -280,7 +278,7 @@ public: virtual array_util & get_arutil() { return m_arutil; } virtual fpa_util & get_futil() { return m_futil; } virtual seq_util & get_sutil() { return m_sutil; } - virtual str_util & get_strutil() { return m_strutil; } + virtual datalog::dl_decl_util& get_dlutil() { return m_dlutil; } virtual bool uses(symbol const & s) const { return @@ -561,7 +559,6 @@ void cmd_context::init_manager_core(bool new_manager) { register_plugin(symbol("pb"), alloc(pb_decl_plugin), logic_has_pb()); register_plugin(symbol("fpa"), alloc(fpa_decl_plugin), logic_has_fpa()); register_plugin(symbol("datalog_relation"), alloc(datalog::dl_decl_plugin), !has_logic()); - register_plugin(symbol("str"), alloc(str_decl_plugin), logic_has_str()); } else { // the manager was created by an external module @@ -575,7 +572,6 @@ void cmd_context::init_manager_core(bool new_manager) { load_plugin(symbol("datatype"), logic_has_datatype(), fids); load_plugin(symbol("seq"), logic_has_seq(), fids); load_plugin(symbol("fpa"), logic_has_fpa(), fids); - load_plugin(symbol("str"), logic_has_str(), fids); load_plugin(symbol("pb"), logic_has_pb(), fids); svector::iterator it = fids.begin(); svector::iterator end = fids.end(); diff --git a/src/parsers/smt2/smt2parser.cpp b/src/parsers/smt2/smt2parser.cpp index fc28fa6e7..cbfcbf1fe 100644 --- a/src/parsers/smt2/smt2parser.cpp +++ b/src/parsers/smt2/smt2parser.cpp @@ -23,7 +23,6 @@ Revision History: #include"bv_decl_plugin.h" #include"arith_decl_plugin.h" #include"seq_decl_plugin.h" -#include"str_decl_plugin.h" #include"ast_pp.h" #include"well_sorted.h" #include"pattern_validation.h" @@ -68,7 +67,6 @@ namespace smt2 { scoped_ptr m_bv_util; scoped_ptr m_arith_util; scoped_ptr m_seq_util; - scoped_ptr m_str_util; scoped_ptr m_pattern_validator; scoped_ptr m_var_shifter; @@ -286,12 +284,6 @@ namespace smt2 { return *(m_bv_util.get()); } - str_util & strutil() { - if (m_str_util.get() == 0) - m_str_util = alloc(str_util, m()); - return *(m_str_util.get()); - } - pattern_validator & pat_validator() { if (m_pattern_validator.get() == 0) { m_pattern_validator = alloc(pattern_validator, m()); @@ -1086,29 +1078,10 @@ namespace smt2 { next(); } - // sorry, breaking theory_seq for a bit - /* void parse_string_const() { SASSERT(curr() == scanner::STRING_TOKEN); expr_stack().push_back(sutil().str.mk_string(symbol(m_scanner.get_string()))); TRACE("smt2parser", tout << "new string: " << mk_pp(expr_stack().back(), m()) << "\n";); - next(); - } - */ - - void parse_string_const() { - parse_string(); - } - - void parse_string() { - SASSERT(curr() == scanner::STRING_TOKEN); - char const *original_token = m_scanner.get_string(); - size_t bufsize = strlen(original_token); - char * buf = alloc_svect(char, bufsize + 1); - strncpy(buf, original_token, bufsize); - buf[bufsize] = '\0'; - TRACE("parse_string", tout << "new string constant: " << buf << " length=" << bufsize << "\n";); - expr_stack().push_back(strutil().mk_string_with_escape_characters(buf)); next(); } diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 80781d6aa..1d56c43a4 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -44,7 +44,7 @@ theory_str::theory_str(ast_manager & m, theory_str_params const & params): /* Internal setup */ search_started(false), m_autil(m), - m_strutil(m), + u(m), sLevel(0), finalCheckProgressIndicator(false), m_trail(m), @@ -70,36 +70,26 @@ theory_str::~theory_str() { m_trail_stack.reset(); } -expr * theory_str::mk_string(std::string str) { +expr * theory_str::mk_string(zstring const& str) { if (m_params.m_StringConstantCache) { ++totalCacheAccessCount; expr * val; if (stringConstantCache.find(str, val)) { - // cache hit - ++cacheHitCount; - TRACE("t_str_cache", tout << "cache hit: \"" << str << "\" (" - << cacheHitCount << " hits, " << cacheMissCount << " misses out of " - << totalCacheAccessCount << " accesses)" << std::endl;); return val; } else { - // cache miss - ++cacheMissCount; - TRACE("t_str_cache", tout << "cache miss: \"" << str << "\" (" - << cacheHitCount << " hits, " << cacheMissCount << " misses out of " - << totalCacheAccessCount << " accesses)" << std::endl;); - val = m_strutil.mk_string(str); + val = u.str.mk_string(str); m_trail.push_back(val); stringConstantCache.insert(str, val); return val; } } else { - return m_strutil.mk_string(str); + return u.str.mk_string(str); } } expr * theory_str::mk_string(const char * str) { - std::string valStr(str); - return mk_string(valStr); + symbol sym(str); + return u.str.mk_string(sym); } void theory_str::initialize_charset() { @@ -210,25 +200,6 @@ void theory_str::assert_implication(expr * premise, expr * conclusion) { } bool theory_str::internalize_atom(app * atom, bool gate_ctx) { - /* - TRACE("t_str", tout << "internalizing atom: " << mk_ismt2_pp(atom, get_manager()) << std::endl;); - SASSERT(atom->get_family_id() == get_family_id()); - - context & ctx = get_context(); - - if (ctx.b_internalized(atom)) - return true; - - unsigned num_args = atom->get_num_args(); - for (unsigned i = 0; i < num_args; i++) - ctx.internalize(atom->get_arg(i), false); - - literal l(ctx.mk_bool_var(atom)); - - ctx.set_var_theory(l.var(), get_id()); - - return true; - */ return internalize_term(atom); } @@ -267,10 +238,9 @@ bool theory_str::internalize_term(app * term) { theory_var v = mk_var(e); TRACE("t_str_detail", tout << "term has theory var #" << v << std::endl;); - if (opt_EagerStringConstantLengthAssertions && m_strutil.is_string(term)) { + if (opt_EagerStringConstantLengthAssertions && u.str.is_string(term)) { TRACE("t_str", tout << "eagerly asserting length of string term " << mk_pp(term, m) << std::endl;); m_basicstr_axiom_todo.insert(e); - TRACE("t_str_axiom_bug", tout << "add " << mk_pp(e->get_owner(), m) << " to m_basicstr_axiom_todo" << std::endl;); } return true; } @@ -295,7 +265,7 @@ void theory_str::refresh_theory_var(expr * e) { theory_var theory_str::mk_var(enode* n) { TRACE("t_str_detail", tout << "mk_var for " << mk_pp(n->get_owner(), get_manager()) << std::endl;); ast_manager & m = get_manager(); - if (!(is_sort_of(m.get_sort(n->get_owner()), m_strutil.get_fid(), STRING_SORT))) { + if (!(is_sort_of(m.get_sort(n->get_owner()), u.get_family_id(), _STRING_SORT))) { return null_theory_var; } if (is_attached_to_var(n)) { @@ -413,7 +383,7 @@ void theory_str::add_cut_info_merge(expr * destNode, int slevel, expr * srcNode) void theory_str::check_and_init_cut_var(expr * node) { if (cut_var_map.contains(node)) { return; - } else if (!m_strutil.is_string(node)) { + } else if (!u.str.is_string(node)) { add_cut_info_one_node(node, -1, node); } } @@ -511,7 +481,7 @@ app * theory_str::mk_str_var(std::string name) { TRACE("t_str_detail", tout << "creating string variable " << name << " at scope level " << sLevel << std::endl;); - sort * string_sort = m.mk_sort(get_family_id(), STRING_SORT); + sort * string_sort = u.str.mk_string_sort(); app * a = m.mk_fresh_const(name.c_str(), string_sort); TRACE("t_str_detail", tout << "a->get_family_id() = " << a->get_family_id() << std::endl @@ -538,7 +508,7 @@ app * theory_str::mk_regex_rep_var() { context & ctx = get_context(); ast_manager & m = get_manager(); - sort * string_sort = m.mk_sort(get_family_id(), STRING_SORT); + sort * string_sort = u.str.mk_string_sort(); app * a = m.mk_fresh_const("regex", string_sort); ctx.internalize(a, false); @@ -590,7 +560,7 @@ app * theory_str::mk_nonempty_str_var() { TRACE("t_str_detail", tout << "creating nonempty string variable " << name << " at scope level " << sLevel << std::endl;); - sort * string_sort = m.mk_sort(get_family_id(), STRING_SORT); + sort * string_sort = u.str.mk_string_sort(); app * a = m.mk_fresh_const(name.c_str(), string_sort); ctx.internalize(a, false); @@ -642,8 +612,7 @@ app * theory_str::mk_unroll(expr * n, expr * bound) { } app * theory_str::mk_contains(expr * haystack, expr * needle) { - expr * args[2] = {haystack, needle}; - app * contains = get_manager().mk_app(get_id(), OP_STR_CONTAINS, 0, 0, 2, args); + app * contains = u.str.mk_contains(haystack, needle); // TODO double-check semantics/argument order m_trail.push_back(contains); // immediately force internalization so that axiom setup does not fail get_context().internalize(contains, false); @@ -652,8 +621,8 @@ app * theory_str::mk_contains(expr * haystack, expr * needle) { } app * theory_str::mk_indexof(expr * haystack, expr * needle) { - expr * args[2] = {haystack, needle}; - app * indexof = get_manager().mk_app(get_id(), OP_STR_INDEXOF, 0, 0, 2, args); + // TODO check meaning of the third argument here + app * indexof = u.str.mk_index(haystack, needle, mk_int(0)); m_trail.push_back(indexof); // immediately force internalization so that axiom setup does not fail get_context().internalize(indexof, false); @@ -663,25 +632,23 @@ app * theory_str::mk_indexof(expr * haystack, expr * needle) { app * theory_str::mk_strlen(expr * e) { /*if (m_strutil.is_string(e)) {*/ if (false) { - const char * strval = 0; - m_strutil.is_string(e, &strval); - int len = strlen(strval); + zstring strval; + u.str.is_string(e, strval); + unsigned int len = strval.length(); return m_autil.mk_numeral(rational(len), true); } else { if (false) { // use cache app * lenTerm = NULL; if (!length_ast_map.find(e, lenTerm)) { - expr * args[1] = {e}; - lenTerm = get_manager().mk_app(get_id(), OP_STRLEN, 0, 0, 1, args); + lenTerm = u.str.mk_length(e); length_ast_map.insert(e, lenTerm); m_trail.push_back(lenTerm); } return lenTerm; } else { // always regen - expr * args[1] = {e}; - return get_manager().mk_app(get_id(), OP_STRLEN, 0, 0, 1, args); + return u.str.mk_length(e); } } } @@ -699,24 +666,22 @@ expr * theory_str::mk_concat_const_str(expr * n1, expr * n2) { expr * v1 = get_eqc_value(n1, n1HasEqcValue); expr * v2 = get_eqc_value(n2, n2HasEqcValue); if (n1HasEqcValue && n2HasEqcValue) { - const char * n1_str_tmp; - m_strutil.is_string(v1, & n1_str_tmp); - std::string n1_str(n1_str_tmp); - const char * n2_str_tmp; - m_strutil.is_string(v2, & n2_str_tmp); - std::string n2_str(n2_str_tmp); - std::string result = n1_str + n2_str; + zstring n1_str; + u.str.is_string(v1, n1_str); + zstring n2_str; + u.str.is_string(v2, n2_str); + zstring result = n1_str + n2_str; return mk_string(result); } else if (n1HasEqcValue && !n2HasEqcValue) { - const char * n1_str_tmp; - m_strutil.is_string(v1, & n1_str_tmp); - if (strcmp(n1_str_tmp, "") == 0) { + zstring n1_str; + u.str.is_string(v1, n1_str); + if (n1_str.empty()) { return n2; } } else if (!n1HasEqcValue && n2HasEqcValue) { - const char * n2_str_tmp; - m_strutil.is_string(v2, & n2_str_tmp); - if (strcmp(n2_str_tmp, "") == 0) { + zstring n2_str; + u.str.is_string(v2, n2_str); + if (n2_str.empty()) { return n1; } } @@ -735,38 +700,42 @@ expr * theory_str::mk_concat(expr * n1, expr * n2) { if (n1HasEqcValue && n2HasEqcValue) { return mk_concat_const_str(n1, n2); } else if (n1HasEqcValue && !n2HasEqcValue) { - bool n2_isConcatFunc = is_concat(to_app(n2)); - if (m_strutil.get_string_constant_value(n1) == "") { + bool n2_isConcatFunc = u.str.is_concat(to_app(n2)); + zstring n1_str; + u.str.is_string(n1, n1_str); + if (n1_str.empty()) { return n2; } if (n2_isConcatFunc) { expr * n2_arg0 = to_app(n2)->get_arg(0); expr * n2_arg1 = to_app(n2)->get_arg(1); - if (m_strutil.is_string(n2_arg0)) { + if (u.str.is_string(n2_arg0)) { n1 = mk_concat_const_str(n1, n2_arg0); // n1 will be a constant n2 = n2_arg1; } } } else if (!n1HasEqcValue && n2HasEqcValue) { - if (m_strutil.get_string_constant_value(n2) == "") { + zstring n2_str; + u.str.is_string(n2, n2_str); + if (n2_str.empty()) { return n1; } - if (is_concat(to_app(n1))) { + if (u.str.is_concat(to_app(n1))) { expr * n1_arg0 = to_app(n1)->get_arg(0); expr * n1_arg1 = to_app(n1)->get_arg(1); - if (m_strutil.is_string(n1_arg1)) { + if (u.str.is_string(n1_arg1)) { n1 = n1_arg0; n2 = mk_concat_const_str(n1_arg1, n2); // n2 will be a constant } } } else { - if (is_concat(to_app(n1)) && is_concat(to_app(n2))) { + if (u.str.is_concat(to_app(n1)) && u.str.is_concat(to_app(n2))) { expr * n1_arg0 = to_app(n1)->get_arg(0); expr * n1_arg1 = to_app(n1)->get_arg(1); expr * n2_arg0 = to_app(n2)->get_arg(0); expr * n2_arg1 = to_app(n2)->get_arg(1); - if (m_strutil.is_string(n1_arg1) && m_strutil.is_string(n2_arg0)) { + if (u.str.is_string(n1_arg1) && u.str.is_string(n2_arg0)) { expr * tmpN1 = n1_arg0; expr * tmpN2 = mk_concat_const_str(n1_arg1, n2_arg0); n1 = mk_concat(tmpN1, tmpN2); @@ -784,8 +753,7 @@ expr * theory_str::mk_concat(expr * n1, expr * n2) { expr * concatAst = NULL; if (!concat_astNode_map.find(n1, n2, concatAst)) { - expr * args[2] = {n1, n2}; - concatAst = m.mk_app(get_id(), OP_STRCAT, 0, 0, 2, args); + concatAst = u.str.mk_concat(n1, n2); m_trail.push_back(concatAst); concat_astNode_map.insert(n1, n2, concatAst); @@ -841,25 +809,30 @@ void theory_str::propagate() { for (unsigned i = 0; i < m_library_aware_axiom_todo.size(); ++i) { enode * e = m_library_aware_axiom_todo[i]; - if (is_str_to_int(e)) { + app * a = e->get_owner(); + if (u.str.is_stoi(a)) { instantiate_axiom_str_to_int(e); - } else if (is_int_to_str(e)) { + } else if (u.str.is_itos(a)) { instantiate_axiom_int_to_str(e); - } else if (is_CharAt(e)) { + } else if (u.str.is_at(a)) { instantiate_axiom_CharAt(e); + /* TODO NEXT: StartsWith/EndsWith -> prefixof/suffixof } else if (is_StartsWith(e)) { instantiate_axiom_StartsWith(e); } else if (is_EndsWith(e)) { instantiate_axiom_EndsWith(e); - } else if (is_Contains(e)) { + */ + } else if (u.str.is_contains(a)) { instantiate_axiom_Contains(e); - } else if (is_Indexof(e)) { + } else if (u.str.is_index(a)) { instantiate_axiom_Indexof(e); + /* TODO NEXT: Indexof2/Lastindexof rewrite? } else if (is_Indexof2(e)) { instantiate_axiom_Indexof2(e); } else if (is_LastIndexof(e)) { instantiate_axiom_LastIndexof(e); - } else if (is_Substr(e)) { + */ + } else if (u.str.is_substr(a)) { instantiate_axiom_Substr(e); } else if (is_Replace(e)) { instantiate_axiom_Replace(e); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 6b1ce9023..63f5d3cfc 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -27,32 +27,13 @@ Revision History: #include #include #include -#include"str_rewriter.h" +#include +#include"seq_decl_plugin.h" #include"union_find.h" +#include"theory_seq_empty.h" namespace smt { - class str_value_factory : public value_factory { - str_util m_util; - public: - str_value_factory(ast_manager & m, family_id fid) : - value_factory(m, fid), - m_util(m) {} - virtual ~str_value_factory() {} - virtual expr * get_some_value(sort * s) { - return m_util.mk_string("some value"); - } - virtual bool get_some_values(sort * s, expr_ref & v1, expr_ref & v2) { - v1 = m_util.mk_string("value 1"); - v2 = m_util.mk_string("value 2"); - return true; - } - virtual expr * get_fresh_value(sort * s) { - return m_util.mk_fresh_string(); - } - virtual void register_value(expr * n) { /* Ignore */ } - }; - // rather than modify obj_pair_map I inherit from it and add my own helper methods class theory_str_contain_pair_bool_map_t : public obj_pair_map { public: @@ -110,12 +91,12 @@ namespace smt { typedef union_find th_union_find; typedef map, default_eq > rational_map; - struct str_hash_proc { - unsigned operator()(std::string const & s) const { - return string_hash(s.c_str(), static_cast(s.length()), 17); + struct zstring_hash_proc { + unsigned operator()(zstring const & s) const { + return string_hash(s.encode().c_str(), static_cast(s.length()), 17); } }; - typedef map > string_map; + typedef map > string_map; protected: theory_str_params const & m_params; @@ -188,14 +169,14 @@ namespace smt { bool search_started; arith_util m_autil; - str_util m_strutil; + seq_util u; int sLevel; bool finalCheckProgressIndicator; expr_ref_vector m_trail; // trail for generated terms - str_value_factory * m_factory; + seq_factory * m_factory; // terms we couldn't go through set_up_axioms() with because they weren't internalized expr_ref_vector m_delayed_axiom_setup_terms; @@ -259,7 +240,7 @@ namespace smt { std::map, expr*> regex_in_bool_map; std::map > regex_in_var_reg_str_map; - std::map regex_nfa_cache; // Regex term --> NFA + // std::map regex_nfa_cache; // Regex term --> NFA char * char_set; std::map charSetLookupTable; @@ -327,7 +308,7 @@ namespace smt { void assert_implication(expr * premise, expr * conclusion); expr * rewrite_implication(expr * premise, expr * conclusion); - expr * mk_string(std::string str); + expr * mk_string(zstring const& str); expr * mk_string(const char * str); app * mk_strlen(expr * e); @@ -359,48 +340,6 @@ namespace smt { app * mk_unroll_test_var(); void add_nonempty_constraint(expr * s); - bool is_concat(app const * a) const { return a->is_app_of(get_id(), OP_STRCAT); } - bool is_concat(enode const * n) const { return is_concat(n->get_owner()); } - bool is_string(app const * a) const { return a->is_app_of(get_id(), OP_STR); } - bool is_string(enode const * n) const { return is_string(n->get_owner()); } - bool is_strlen(app const * a) const { return a->is_app_of(get_id(), OP_STRLEN); } - bool is_strlen(enode const * n) const { return is_strlen(n->get_owner()); } - bool is_CharAt(app const * a) const { return a->is_app_of(get_id(), OP_STR_CHARAT); } - bool is_CharAt(enode const * n) const { return is_CharAt(n->get_owner()); } - bool is_StartsWith(app const * a) const { return a->is_app_of(get_id(), OP_STR_STARTSWITH); } - bool is_StartsWith(enode const * n) const { return is_StartsWith(n->get_owner()); } - bool is_EndsWith(app const * a) const { return a->is_app_of(get_id(), OP_STR_ENDSWITH); } - bool is_EndsWith(enode const * n) const { return is_EndsWith(n->get_owner()); } - bool is_Contains(app const * a) const { return a->is_app_of(get_id(), OP_STR_CONTAINS); } - bool is_Contains(enode const * n) const { return is_Contains(n->get_owner()); } - bool is_Indexof(app const * a) const { return a->is_app_of(get_id(), OP_STR_INDEXOF); } - bool is_Indexof(enode const * n) const { return is_Indexof(n->get_owner()); } - bool is_Indexof2(app const * a) const { return a->is_app_of(get_id(), OP_STR_INDEXOF2); } - bool is_Indexof2(enode const * n) const { return is_Indexof2(n->get_owner()); } - bool is_LastIndexof(app const * a) const { return a->is_app_of(get_id(), OP_STR_LASTINDEXOF); } - bool is_LastIndexof(enode const * n) const { return is_LastIndexof(n->get_owner()); } - bool is_Substr(app const * a) const { return a->is_app_of(get_id(), OP_STR_SUBSTR); } - bool is_Substr(enode const * n) const { return is_Substr(n->get_owner()); } - bool is_Replace(app const * a) const { return a->is_app_of(get_id(), OP_STR_REPLACE); } - bool is_Replace(enode const * n) const { return is_Replace(n->get_owner()); } - bool is_str_to_int(app const * a) const { return a->is_app_of(get_id(), OP_STR_STR2INT); } - bool is_str_to_int(enode const * n) const { return is_str_to_int(n->get_owner()); } - bool is_int_to_str(app const * a) const { return a->is_app_of(get_id(), OP_STR_INT2STR); } - bool is_int_to_str(enode const * n) const { return is_int_to_str(n->get_owner()); } - - bool is_RegexIn(app const * a) const { return a->is_app_of(get_id(), OP_RE_REGEXIN); } - bool is_RegexIn(enode const * n) const { return is_RegexIn(n->get_owner()); } - bool is_RegexConcat(app const * a) const { return a->is_app_of(get_id(), OP_RE_REGEXCONCAT); } - bool is_RegexConcat(enode const * n) const { return is_RegexConcat(n->get_owner()); } - bool is_RegexStar(app const * a) const { return a->is_app_of(get_id(), OP_RE_REGEXSTAR); } - bool is_RegexStar(enode const * n) const { return is_RegexStar(n->get_owner()); } - bool is_RegexUnion(app const * a) const { return a->is_app_of(get_id(), OP_RE_REGEXUNION); } - bool is_RegexUnion(enode const * n) const { return is_RegexUnion(n->get_owner()); } - bool is_Str2Reg(app const * a) const { return a->is_app_of(get_id(), OP_RE_STR2REGEX); } - bool is_Str2Reg(enode const * n) const { return is_Str2Reg(n->get_owner()); } - bool is_Unroll(app const * a) const { return a->is_app_of(get_id(), OP_RE_UNROLL); } - bool is_Unroll(enode const * n) const { return is_Unroll(n->get_owner()); } - void instantiate_concat_axiom(enode * cat); void try_eval_concat(enode * cat); void instantiate_basic_string_axioms(enode * str); From 3f1ceedcb14b0ecef4f9f73bb24fec296857c9c8 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 27 Feb 2017 20:48:55 -0500 Subject: [PATCH 349/562] theory_str refactor pass 2 --- src/smt/theory_str.cpp | 932 +++++++++++++++++++++-------------------- src/smt/theory_str.h | 57 ++- 2 files changed, 529 insertions(+), 460 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 1d56c43a4..6585bd7f2 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -23,6 +23,7 @@ Revision History: #include #include #include +#include"theory_seq_empty.h" #include "../ast/ast.h" #include"theory_arith.h" @@ -832,11 +833,12 @@ void theory_str::propagate() { } else if (is_LastIndexof(e)) { instantiate_axiom_LastIndexof(e); */ - } else if (u.str.is_substr(a)) { + } else if (u.str.is_extract(a)) { + // TODO check semantics of substr vs. extract instantiate_axiom_Substr(e); - } else if (is_Replace(e)) { + } else if (u.str.is_replace(a)) { instantiate_axiom_Replace(e); - } else if (is_RegexIn(e)) { + } else if (u.str.is_in_re(a)) { instantiate_axiom_RegexIn(e); } else { TRACE("t_str", tout << "BUG: unhandled library-aware term " << mk_pp(e->get_owner(), get_manager()) << std::endl;); @@ -861,8 +863,8 @@ void theory_str::propagate() { */ void theory_str::try_eval_concat(enode * cat) { - SASSERT(is_concat(cat)); app * a_cat = cat->get_owner(); + SASSERT(u.str.is_concat(a_cat)); context & ctx = get_context(); ast_manager & m = get_manager(); @@ -870,7 +872,7 @@ void theory_str::try_eval_concat(enode * cat) { TRACE("t_str_detail", tout << "attempting to flatten " << mk_pp(a_cat, m) << std::endl;); std::stack worklist; - std::string flattenedString(""); + zstring flattenedString(""); bool constOK = true; { @@ -883,10 +885,10 @@ void theory_str::try_eval_concat(enode * cat) { while (constOK && !worklist.empty()) { app * evalArg = worklist.top(); worklist.pop(); - if (m_strutil.is_string(evalArg)) { - std::string nextStr = m_strutil.get_string_constant_value(evalArg); - flattenedString.append(nextStr); - } else if (is_concat(evalArg)) { + zstring nextStr; + if (u.str.is_string(evalArg, nextStr)) { + flattenedString += nextStr; + } else if (u.str.is_concat(evalArg)) { app * arg0 = to_app(evalArg->get_arg(0)); app * arg1 = to_app(evalArg->get_arg(1)); @@ -899,7 +901,7 @@ void theory_str::try_eval_concat(enode * cat) { } } if (constOK) { - TRACE("t_str_detail", tout << "flattened to \"" << flattenedString << "\"" << std::endl;); + TRACE("t_str_detail", tout << "flattened to \"" << flattenedString.encode().c_str() << "\"" << std::endl;); expr_ref constStr(mk_string(flattenedString), m); expr_ref axiom(ctx.mk_eq_atom(a_cat, constStr), m); assert_axiom(axiom); @@ -911,8 +913,8 @@ void theory_str::try_eval_concat(enode * cat) { * Length(Concat(x, y)) = Length(x) + Length(y) */ void theory_str::instantiate_concat_axiom(enode * cat) { - SASSERT(is_concat(cat)); app * a_cat = cat->get_owner(); + SASSERT(u.str.is_concat(a_cat)); ast_manager & m = get_manager(); @@ -969,15 +971,15 @@ void theory_str::instantiate_basic_string_axioms(enode * str) { // generate a stronger axiom for constant strings app * a_str = str->get_owner(); - if (m_strutil.is_string(str->get_owner())) { + if (u.str.is_string(a_str)) { expr_ref len_str(m); len_str = mk_strlen(a_str); SASSERT(len_str); - const char * strconst = 0; - m_strutil.is_string(str->get_owner(), & strconst); - TRACE("t_str_detail", tout << "instantiating constant string axioms for \"" << strconst << "\"" << std::endl;); - int l = strlen(strconst); + zstring strconst; + u.str.is_string(str->get_owner(), strconst); + TRACE("t_str_detail", tout << "instantiating constant string axioms for \"" << strconst.encode().c_str() << "\"" << std::endl;); + unsigned int l = strconst.length(); expr_ref len(m_autil.mk_numeral(rational(l), true), m); literal lit(mk_eq(len_str, len, false)); @@ -1186,12 +1188,11 @@ void theory_str::instantiate_axiom_Contains(enode * e) { axiomatized_terms.insert(ex); // quick path, because this is necessary due to rewriter behaviour - // (at minimum it should fix z3str/concat-006.smt2 - if (m_strutil.is_string(ex->get_arg(0)) && m_strutil.is_string(ex->get_arg(1))) { + // at minimum it should fix z3str/concat-006.smt2 + zstring haystackStr, needleStr; + if (u.str.is_string(ex->get_arg(0), haystackStr) && u.str.is_string(ex->get_arg(1), needleStr)) { TRACE("t_str_detail", tout << "eval constant Contains term " << mk_pp(ex, m) << std::endl;); - std::string haystackStr = m_strutil.get_string_constant_value(ex->get_arg(0)); - std::string needleStr = m_strutil.get_string_constant_value(ex->get_arg(1)); - if (haystackStr.find(needleStr) != std::string::npos) { + if (haystackStr.contains(needleStr)) { assert_axiom(ex); } else { assert_axiom(m.mk_not(ex)); @@ -1378,8 +1379,8 @@ void theory_str::instantiate_axiom_LastIndexof(enode * e) { thenItems.push_back(ctx.mk_eq_atom(indexAst, mk_strlen(x1))); bool canSkip = false; - if (m_strutil.is_string(expr->get_arg(1))) { - std::string arg1Str = m_strutil.get_string_constant_value(expr->get_arg(1)); + zstring arg1Str; + if (u.str.is_string(expr->get_arg(1), arg1Str)) { if (arg1Str.length() == 1) { canSkip = true; } @@ -1503,30 +1504,6 @@ void theory_str::instantiate_axiom_Substr(enode * e) { expr_ref finalAxiom(m.mk_and(case1, case2, case3), m); SASSERT(finalAxiom); assert_axiom(finalAxiom); - - /* - expr_ref ts0(mk_str_var("ts0"), m); - expr_ref ts1(mk_str_var("ts1"), m); - expr_ref ts2(mk_str_var("ts2"), m); - - expr_ref ts0_contains_ts1(mk_contains(expr->get_arg(0), ts1), m); - - expr_ref_vector and_item(m); - //and_item.push_back(ts0_contains_ts1); - and_item.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(ts0, mk_concat(ts1, ts2)))); - and_item.push_back(ctx.mk_eq_atom(expr->get_arg(1), mk_strlen(ts0))); - and_item.push_back(ctx.mk_eq_atom(expr->get_arg(2), mk_strlen(ts1))); - - expr_ref breakdownAssert(m.mk_and(and_item.size(), and_item.c_ptr()), m); - SASSERT(breakdownAssert); - - expr_ref reduceToVar(ctx.mk_eq_atom(expr, ts1), m); - SASSERT(reduceToVar); - - expr_ref finalAxiom(m.mk_and(breakdownAssert, reduceToVar), m); - SASSERT(finalAxiom); - assert_axiom(finalAxiom); - */ } void theory_str::instantiate_axiom_Replace(enode * e) { @@ -1651,14 +1628,58 @@ void theory_str::instantiate_axiom_int_to_str(enode * e) { } expr * theory_str::mk_RegexIn(expr * str, expr * regexp) { - expr * args[2] = {str, regexp}; - app * regexIn = get_manager().mk_app(get_id(), OP_RE_REGEXIN, 0, 0, 2, args); + app * regexIn = u.re.mk_in_re(str, regexp); // immediately force internalization so that axiom setup does not fail get_context().internalize(regexIn, false); set_up_axioms(regexIn); return regexIn; } +static zstring str2RegexStr(zstring str) { + zstring res(""); + int len = str.length(); + for (int i = 0; i < len; i++) { + char nc = str[i]; + // 12 special chars + if (nc == '\\' || nc == '^' || nc == '$' || nc == '.' || nc == '|' || nc == '?' + || nc == '*' || nc == '+' || nc == '(' || nc == ')' || nc == '[' || nc == '{') { + res += zstring("\\"); + } + res += zstring(1, (unsigned)str[i]); + } + return res; +} + +zstring theory_str::get_std_regex_str(expr * regex) { + app * a_regex = to_app(regex); + if (u.re.is_to_re(a_regex)) { + expr * regAst = a_regex->get_arg(0); + zstring regAstVal; + u.str.is_string(regAst, regAstVal); + zstring regStr = str2RegexStr(regAstVal); + return regStr; + } else if (u.re.is_concat(a_regex)) { + expr * reg1Ast = a_regex->get_arg(0); + expr * reg2Ast = a_regex->get_arg(1); + zstring reg1Str = get_std_regex_str(reg1Ast); + zstring reg2Str = get_std_regex_str(reg2Ast); + return zstring("(") + reg1Str + zstring(")(") + reg2Str + zstring(")"); + } else if (u.re.is_union(a_regex)) { + expr * reg1Ast = a_regex->get_arg(0); + expr * reg2Ast = a_regex->get_arg(1); + zstring reg1Str = get_std_regex_str(reg1Ast); + zstring reg2Str = get_std_regex_str(reg2Ast); + return zstring("(") + reg1Str + zstring(")|(") + reg2Str + zstring(")"); + } else if (u.re.is_star(a_regex)) { + expr * reg1Ast = a_regex->get_arg(0); + zstring reg1Str = get_std_regex_str(reg1Ast); + return zstring("(") + reg1Str + zstring(")*"); + } else { + TRACE("t_str", tout << "BUG: unrecognized regex term " << mk_pp(regex, get_manager()) << std::endl;); + UNREACHABLE(); return zstring(""); + } +} + void theory_str::instantiate_axiom_RegexIn(enode * e) { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -1673,8 +1694,8 @@ void theory_str::instantiate_axiom_RegexIn(enode * e) { TRACE("t_str_detail", tout << "instantiate RegexIn axiom for " << mk_pp(ex, m) << std::endl;); { - std::string regexStr = m_strutil.get_std_regex_str(ex->get_arg(1)); - std::pair key1(ex->get_arg(0), regexStr); + zstring regexStr = get_std_regex_str(ex->get_arg(1)); + std::pair key1(ex->get_arg(0), regexStr); // skip Z3str's map check, because we already check if we set up axioms on this term regex_in_bool_map[key1] = ex; regex_in_var_reg_str_map[ex->get_arg(0)].insert(regexStr); @@ -1683,7 +1704,7 @@ void theory_str::instantiate_axiom_RegexIn(enode * e) { expr_ref str(ex->get_arg(0), m); app * regex = to_app(ex->get_arg(1)); - if (is_Str2Reg(regex)) { + if (u.re.is_to_re(regex)) { expr_ref rxStr(regex->get_arg(0), m); // want to assert 'expr IFF (str == rxStr)' expr_ref rhs(ctx.mk_eq_atom(str, rxStr), m); @@ -1691,7 +1712,7 @@ void theory_str::instantiate_axiom_RegexIn(enode * e) { SASSERT(finalAxiom); assert_axiom(finalAxiom); TRACE("t_str", tout << "set up Str2Reg: (RegexIn " << mk_pp(str, m) << " " << mk_pp(regex, m) << ")" << std::endl;); - } else if (is_RegexConcat(regex)) { + } else if (u.re.is_concat(regex)) { expr_ref var1(mk_regex_rep_var(), m); expr_ref var2(mk_regex_rep_var(), m); expr_ref rhs(mk_concat(var1, var2), m); @@ -1708,7 +1729,7 @@ void theory_str::instantiate_axiom_RegexIn(enode * e) { expr_ref finalAxiom(mk_and(items), m); SASSERT(finalAxiom); assert_axiom(finalAxiom); - } else if (is_RegexUnion(regex)) { + } else if (u.re.is_union(regex)) { expr_ref var1(mk_regex_rep_var(), m); expr_ref var2(mk_regex_rep_var(), m); expr_ref orVar(m.mk_or(ctx.mk_eq_atom(str, var1), ctx.mk_eq_atom(str, var2)), m); @@ -1721,7 +1742,7 @@ void theory_str::instantiate_axiom_RegexIn(enode * e) { items.push_back(var2InRegex2); items.push_back(ctx.mk_eq_atom(ex, orVar)); assert_axiom(mk_and(items)); - } else if (is_RegexStar(regex)) { + } else if (u.re.is_star(regex)) { // slightly more complex due to the unrolling step. expr_ref regex1(regex->get_arg(0), m); expr_ref unrollCount(mk_unroll_bound_var(), m); @@ -1852,13 +1873,13 @@ void theory_str::group_terms_by_eqc(expr * n, std::set & concats, std::se expr * eqcNode = n; do { app * ast = to_app(eqcNode); - if (is_concat(ast)) { + if (u.str.is_concat(ast)) { expr * simConcat = simplify_concat(ast); if (simConcat != ast) { - if (is_concat(to_app(simConcat))) { + if (u.str.is_concat(to_app(simConcat))) { concats.insert(simConcat); } else { - if (m_strutil.is_string(simConcat)) { + if (u.str.is_string(simConcat)) { consts.insert(simConcat); } else { vars.insert(simConcat); @@ -1867,7 +1888,7 @@ void theory_str::group_terms_by_eqc(expr * n, std::set & concats, std::se } else { concats.insert(simConcat); } - } else if (is_string(ast)) { + } else if (u.str.is_string(ast)) { consts.insert(ast); } else { vars.insert(ast); @@ -1878,7 +1899,7 @@ void theory_str::group_terms_by_eqc(expr * n, std::set & concats, std::se void theory_str::get_nodes_in_concat(expr * node, ptr_vector & nodeList) { app * a_node = to_app(node); - if (!is_concat(a_node)) { + if (!u.str.is_concat(a_node)) { nodeList.push_back(node); return; } else { @@ -1901,16 +1922,21 @@ expr * theory_str::eval_concat(expr * n1, expr * n2) { expr * v1 = get_eqc_value(n1, n1HasEqcValue); expr * v2 = get_eqc_value(n2, n2HasEqcValue); if (n1HasEqcValue && n2HasEqcValue) { - std::string n1_str = m_strutil.get_string_constant_value(v1); - std::string n2_str = m_strutil.get_string_constant_value(v2); - std::string result = n1_str + n2_str; + zstring n1_str, n2_str; + u.str.is_string(v1, n1_str); + u.str.is_string(v2, n2_str); + zstring result = n1_str + n2_str; return mk_string(result); } else if (n1HasEqcValue && !n2HasEqcValue) { - if (m_strutil.get_string_constant_value(v1) == "") { + zstring v1_str; + u.str.is_string(v1, v1_str); + if (v1_str.empty()) { return n2; } } else if (n2HasEqcValue && !n1HasEqcValue) { - if (m_strutil.get_string_constant_value(v2) == "") { + zstring v2_str; + u.str.is_string(v2, v2_str); + if (v2_str.empty()) { return n1; } } @@ -1943,7 +1969,8 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { ctx.internalize(nn, false); - std::string eq_strValue = m_strutil.get_string_constant_value(eq_str); + zstring eq_strValue; + u.str.is_string(eq_str, eq_strValue); expr * n_eqNode = nn; do { enode * n_eq_enode = ctx.get_enode(n_eqNode); @@ -1966,7 +1993,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { app * a_parent = e_parent->get_owner(); TRACE("t_str_detail", tout << "considering parent " << mk_ismt2_pp(a_parent, m) << std::endl;); - if (is_concat(a_parent)) { + if (u.str.is_concat(a_parent)) { expr * arg0 = a_parent->get_arg(0); expr * arg1 = a_parent->get_arg(1); @@ -2028,7 +2055,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { assert_implication(implyL, implyR); } - } else if (is_concat(to_app(n_eqNode))) { + } else if (u.str.is_concat(to_app(n_eqNode))) { expr_ref simpleConcat(m); simpleConcat = mk_concat(eq_str, arg1); if (!in_same_eqc(a_parent, simpleConcat)) { @@ -2097,7 +2124,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { assert_implication(implyL, implyR); } - } else if (is_concat(to_app(n_eqNode))) { + } else if (u.str.is_concat(to_app(n_eqNode))) { expr_ref simpleConcat(m); simpleConcat = mk_concat(arg0, eq_str); if (!in_same_eqc(a_parent, simpleConcat)) { @@ -2116,11 +2143,11 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { //--------------------------------------------------------- // Case (2-1) begin: (Concat n_eqNode (Concat str var)) - if (arg0 == n_eqNode && is_concat(to_app(arg1))) { + if (arg0 == n_eqNode && u.str.is_concat(to_app(arg1))) { app * a_arg1 = to_app(arg1); TRACE("t_str_detail", tout << "simplify_parent #3" << std::endl;); expr * r_concat_arg0 = a_arg1->get_arg(0); - if (m_strutil.is_string(r_concat_arg0)) { + if (u.str.is_string(r_concat_arg0)) { expr * combined_str = eval_concat(eq_str, r_concat_arg0); SASSERT(combined_str); expr * r_concat_arg1 = a_arg1->get_arg(1); @@ -2140,11 +2167,11 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { //--------------------------------------------------------- // Case (2-2) begin: (Concat (Concat var str) n_eqNode) - if (is_concat(to_app(arg0)) && arg1 == n_eqNode) { + if (u.str.is_concat(to_app(arg0)) && arg1 == n_eqNode) { app * a_arg0 = to_app(arg0); TRACE("t_str_detail", tout << "simplify_parent #4" << std::endl;); expr * l_concat_arg1 = a_arg0->get_arg(1); - if (m_strutil.is_string(l_concat_arg1)) { + if (u.str.is_string(l_concat_arg1)) { expr * combined_str = eval_concat(l_concat_arg1, eq_str); SASSERT(combined_str); expr * l_concat_arg0 = a_arg0->get_arg(0); @@ -2169,10 +2196,10 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { concat_parent_it != e_parent->end_parents(); concat_parent_it++) { enode * e_concat_parent = *concat_parent_it; app * concat_parent = e_concat_parent->get_owner(); - if (is_concat(concat_parent)) { + if (u.str.is_concat(concat_parent)) { expr * concat_parent_arg0 = concat_parent->get_arg(0); expr * concat_parent_arg1 = concat_parent->get_arg(1); - if (concat_parent_arg0 == a_parent && m_strutil.is_string(concat_parent_arg1)) { + if (concat_parent_arg0 == a_parent && u.str.is_string(concat_parent_arg1)) { TRACE("t_str_detail", tout << "simplify_parent #5" << std::endl;); expr * combinedStr = eval_concat(eq_str, concat_parent_arg1); SASSERT(combinedStr); @@ -2195,10 +2222,10 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { concat_parent_it != e_parent->end_parents(); concat_parent_it++) { enode * e_concat_parent = *concat_parent_it; app * concat_parent = e_concat_parent->get_owner(); - if (is_concat(concat_parent)) { + if (u.str.is_concat(concat_parent)) { expr * concat_parent_arg0 = concat_parent->get_arg(0); expr * concat_parent_arg1 = concat_parent->get_arg(1); - if (concat_parent_arg1 == a_parent && m_strutil.is_string(concat_parent_arg0)) { + if (concat_parent_arg1 == a_parent && u.str.is_string(concat_parent_arg0)) { TRACE("t_str_detail", tout << "simplify_parent #6" << std::endl;); expr * combinedStr = eval_concat(concat_parent_arg0, eq_str); SASSERT(combinedStr); @@ -2376,7 +2403,7 @@ void theory_str::infer_len_concat_equality(expr * nn1, expr * nn2) { // Known: a1_arg0 and a1_arg1 // Unknown: nn1 - if (is_concat(to_app(nn1))) { + if (u.str.is_concat(to_app(nn1))) { rational nn1ConcatLen; bool nn1ConcatLen_exists = infer_len_concat(nn1, nn1ConcatLen); if (nnLen_exists && nn1ConcatLen_exists) { @@ -2388,7 +2415,7 @@ void theory_str::infer_len_concat_equality(expr * nn1, expr * nn2) { // Known: a1_arg0 and a1_arg1 // Unknown: nn1 - if (is_concat(to_app(nn2))) { + if (u.str.is_concat(to_app(nn2))) { rational nn2ConcatLen; bool nn2ConcatLen_exists = infer_len_concat(nn2, nn2ConcatLen); if (nnLen_exists && nn2ConcatLen_exists) { @@ -2397,10 +2424,10 @@ void theory_str::infer_len_concat_equality(expr * nn1, expr * nn2) { } if (nnLen_exists) { - if (is_concat(to_app(nn1))) { + if (u.str.is_concat(to_app(nn1))) { infer_len_concat_arg(nn1, nnLen); } - if (is_concat(to_app(nn2))) { + if (u.str.is_concat(to_app(nn2))) { infer_len_concat_arg(nn2, nnLen); } } @@ -2604,17 +2631,17 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { // check whether new_nn1 and new_nn2 are still concats - bool n1IsConcat = is_concat(a_new_nn1); - bool n2IsConcat = is_concat(a_new_nn2); + bool n1IsConcat = u.str.is_concat(a_new_nn1); + bool n2IsConcat = u.str.is_concat(a_new_nn2); if (!n1IsConcat && n2IsConcat) { TRACE("t_str_detail", tout << "nn1_new is not a concat" << std::endl;); - if (is_string(a_new_nn1)) { + if (u.str.is_string(a_new_nn1)) { simplify_parent(new_nn2, new_nn1); } return; } else if (n1IsConcat && !n2IsConcat) { TRACE("t_str_detail", tout << "nn2_new is not a concat" << std::endl;); - if (is_string(a_new_nn2)) { + if (u.str.is_string(a_new_nn2)) { simplify_parent(new_nn1, new_nn2); } return; @@ -2712,8 +2739,8 @@ bool theory_str::will_result_in_overlap(expr * lhs, expr * rhs) { app * a_new_nn1 = to_app(new_nn1); app * a_new_nn2 = to_app(new_nn2); - bool n1IsConcat = is_concat(a_new_nn1); - bool n2IsConcat = is_concat(a_new_nn2); + bool n1IsConcat = u.str.is_concat(a_new_nn1); + bool n2IsConcat = u.str.is_concat(a_new_nn2); if (!n1IsConcat && !n2IsConcat) { // we simplified both sides to non-concat expressions... return false; @@ -2766,7 +2793,7 @@ bool theory_str::will_result_in_overlap(expr * lhs, expr * rhs) { expr * v2_arg0 = to_app(new_nn2)->get_arg(0); expr * v2_arg1 = to_app(new_nn2)->get_arg(1); - if (m_strutil.is_string(v1_arg1) && !m_strutil.is_string(v2_arg1)) { + if (u.str.is_string(v1_arg1) && !u.str.is_string(v2_arg1)) { m = v1_arg0; strAst = v1_arg1; x = v2_arg0; @@ -2800,7 +2827,7 @@ bool theory_str::will_result_in_overlap(expr * lhs, expr * rhs) { expr * strAst = NULL; expr * n = NULL; - if (m_strutil.is_string(v1_arg0) && !m_strutil.is_string(v2_arg0)) { + if (u.str.is_string(v1_arg0) && !u.str.is_string(v2_arg0)) { strAst = v1_arg0; n = v1_arg1; x = v2_arg0; @@ -2848,7 +2875,7 @@ bool theory_str::will_result_in_overlap(expr * lhs, expr * rhs) { expr * m = NULL; expr * str2Ast = NULL; - if (m_strutil.is_string(v1_arg0)) { + if (u.str.is_string(v1_arg0)) { str1Ast = v1_arg0; y = v1_arg1; m = v2_arg0; @@ -2881,7 +2908,7 @@ bool theory_str::is_concat_eq_type1(expr * concatAst1, expr * concatAst2) { expr * m = to_app(concatAst2)->get_arg(0); expr * n = to_app(concatAst2)->get_arg(1); - if (!m_strutil.is_string(x) && !m_strutil.is_string(y) && !m_strutil.is_string(m) && !m_strutil.is_string(n)) { + if (!u.str.is_string(x) && !u.str.is_string(y) && !u.str.is_string(m) && !u.str.is_string(n)) { return true; } else { return false; @@ -2896,11 +2923,11 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; ); - if (!is_concat(to_app(concatAst1))) { + if (!u.str.is_concat(to_app(concatAst1))) { TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); return; } - if (!is_concat(to_app(concatAst2))) { + if (!u.str.is_concat(to_app(concatAst2))) { TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); return; } @@ -3268,11 +3295,11 @@ bool theory_str::is_concat_eq_type2(expr * concatAst1, expr * concatAst2) { expr * v2_arg0 = to_app(concatAst2)->get_arg(0); expr * v2_arg1 = to_app(concatAst2)->get_arg(1); - if ((!m_strutil.is_string(v1_arg0)) && m_strutil.is_string(v1_arg1) - && (!m_strutil.is_string(v2_arg0)) && (!m_strutil.is_string(v2_arg1))) { + if ((!u.str.is_string(v1_arg0)) && u.str.is_string(v1_arg1) + && (!u.str.is_string(v2_arg0)) && (!u.str.is_string(v2_arg1))) { return true; - } else if ((!m_strutil.is_string(v2_arg0)) && m_strutil.is_string(v2_arg1) - && (!m_strutil.is_string(v1_arg0)) && (!m_strutil.is_string(v1_arg1))) { + } else if ((!u.str.is_string(v2_arg0)) && u.str.is_string(v2_arg1) + && (!u.str.is_string(v1_arg0)) && (!u.str.is_string(v1_arg1))) { return true; } else { return false; @@ -3287,11 +3314,11 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; ); - if (!is_concat(to_app(concatAst1))) { + if (!u.str.is_concat(to_app(concatAst1))) { TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); return; } - if (!is_concat(to_app(concatAst2))) { + if (!u.str.is_concat(to_app(concatAst2))) { TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); return; } @@ -3306,7 +3333,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { expr * v2_arg0 = to_app(concatAst2)->get_arg(0); expr * v2_arg1 = to_app(concatAst2)->get_arg(1); - if (m_strutil.is_string(v1_arg1) && !m_strutil.is_string(v2_arg1)) { + if (u.str.is_string(v1_arg1) && !u.str.is_string(v2_arg1)) { m = v1_arg0; strAst = v1_arg1; x = v2_arg0; @@ -3318,14 +3345,15 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { y = v1_arg1; } - std::string strValue = m_strutil.get_string_constant_value(strAst); + zstring strValue; + u.str.is_string(strAst, strValue); rational x_len, y_len, m_len, str_len; bool x_len_exists = get_len_value(x, x_len); bool y_len_exists = get_len_value(y, y_len); bool m_len_exists = get_len_value(m, m_len); bool str_len_exists = true; - str_len = rational((unsigned)(strValue.length())); + str_len = rational(strValue.length()); // setup @@ -3502,12 +3530,12 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { << "mLen = " << m_len.to_string() << std::endl << "strLen = " << str_len.to_string() << std::endl << "lenDelta = " << lenDelta.to_string() << std::endl - << "strValue = \"" << strValue << "\" (len=" << strValue.length() << ")" << std::endl + << "strValue = \"" << strValue << "\" (len=" << strValue.length() << ")" << "\n" ; ); - std::string part1Str = strValue.substr(0, lenDelta.get_unsigned()); - std::string part2Str = strValue.substr(lenDelta.get_unsigned(), strValue.length() - lenDelta.get_unsigned()); + zstring part1Str = strValue.extract(0, lenDelta.get_unsigned()); + zstring part2Str = strValue.extract(lenDelta.get_unsigned(), strValue.length() - lenDelta.get_unsigned()); expr_ref prefixStr(mk_string(part1Str), mgr); expr_ref x_concat(mk_concat(m, prefixStr), mgr); @@ -3573,9 +3601,9 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { } } - for (int i = 0; i <= (int)strValue.size(); ++i) { - std::string part1Str = strValue.substr(0, i); - std::string part2Str = strValue.substr(i, strValue.size() - i); + for (unsigned int i = 0; i <= strValue.length(); ++i) { + zstring part1Str = strValue.extract(0, i); + zstring part2Str = strValue.extract(i, strValue.length() - i); expr_ref prefixStr(mk_string(part1Str), mgr); expr_ref x_concat(mk_concat(m, prefixStr), mgr); expr_ref cropStr(mk_string(part2Str), mgr); @@ -3624,11 +3652,11 @@ bool theory_str::is_concat_eq_type3(expr * concatAst1, expr * concatAst2) { expr * v2_arg0 = to_app(concatAst2)->get_arg(0); expr * v2_arg1 = to_app(concatAst2)->get_arg(1); - if (m_strutil.is_string(v1_arg0) && (!m_strutil.is_string(v1_arg1)) - && (!m_strutil.is_string(v2_arg0)) && (!m_strutil.is_string(v2_arg1))) { + if (u.str.is_string(v1_arg0) && (!u.str.is_string(v1_arg1)) + && (!u.str.is_string(v2_arg0)) && (!u.str.is_string(v2_arg1))) { return true; - } else if (m_strutil.is_string(v2_arg0) && (!m_strutil.is_string(v2_arg1)) - && (!m_strutil.is_string(v1_arg0)) && (!m_strutil.is_string(v1_arg1))) { + } else if (u.str.is_string(v2_arg0) && (!u.str.is_string(v2_arg1)) + && (!u.str.is_string(v1_arg0)) && (!u.str.is_string(v1_arg1))) { return true; } else { return false; @@ -3643,11 +3671,11 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; ); - if (!is_concat(to_app(concatAst1))) { + if (!u.str.is_concat(to_app(concatAst1))) { TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); return; } - if (!is_concat(to_app(concatAst2))) { + if (!u.str.is_concat(to_app(concatAst2))) { TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); return; } @@ -3662,7 +3690,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { expr * strAst = NULL; expr * n = NULL; - if (m_strutil.is_string(v1_arg0) && !m_strutil.is_string(v2_arg0)) { + if (u.str.is_string(v1_arg0) && !u.str.is_string(v2_arg0)) { strAst = v1_arg0; n = v1_arg1; x = v2_arg0; @@ -3674,7 +3702,8 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { y = v1_arg1; } - std::string strValue = m_strutil.get_string_constant_value(strAst); + zstring strValue; + u.str.is_string(strAst, strValue); rational x_len, y_len, str_len, n_len; bool x_len_exists = get_len_value(x, x_len); @@ -3776,9 +3805,9 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { prefixLen = x_len; litems.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); } - std::string prefixStr = strValue.substr(0, prefixLen.get_unsigned()); + zstring prefixStr = strValue.extract(0, prefixLen.get_unsigned()); rational str_sub_prefix = str_len - prefixLen; - std::string suffixStr = strValue.substr(prefixLen.get_unsigned(), str_sub_prefix.get_unsigned()); + zstring suffixStr = strValue.extract(prefixLen.get_unsigned(), str_sub_prefix.get_unsigned()); expr_ref prefixAst(mk_string(prefixStr), mgr); expr_ref suffixAst(mk_string(suffixStr), mgr); expr_ref ax_l(mgr.mk_and(litems.size(), litems.c_ptr()), mgr); @@ -3878,9 +3907,9 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { unsigned option = 0; int pos = 1; - for (int i = 0; i <= (int) strValue.size(); i++) { - std::string part1Str = strValue.substr(0, i); - std::string part2Str = strValue.substr(i, strValue.size() - i); + for (unsigned int i = 0; i <= strValue.length(); i++) { + zstring part1Str = strValue.extract(0, i); + zstring part2Str = strValue.extract(i, strValue.length() - i); expr_ref cropStr(mk_string(part1Str), mgr); expr_ref suffixStr(mk_string(part2Str), mgr); expr_ref y_concat(mk_concat(suffixStr, n), mgr); @@ -3900,7 +3929,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { expr_ref option1(mk_and(and_item), mgr); arrangement_disjunction.push_back(option1); double priority; - if (i == (int)strValue.size()) { + if (i == strValue.length()) { priority = 0.5; } else { priority = 0.1; @@ -3974,8 +4003,8 @@ bool theory_str::is_concat_eq_type4(expr * concatAst1, expr * concatAst2) { expr * v2_arg0 = to_app(concatAst2)->get_arg(0); expr * v2_arg1 = to_app(concatAst2)->get_arg(1); - if (m_strutil.is_string(v1_arg0) && (!m_strutil.is_string(v1_arg1)) - && m_strutil.is_string(v2_arg0) && (!m_strutil.is_string(v2_arg1))) { + if (u.str.is_string(v1_arg0) && (!u.str.is_string(v1_arg1)) + && u.str.is_string(v2_arg0) && (!u.str.is_string(v2_arg1))) { return true; } else { return false; @@ -3990,11 +4019,11 @@ void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; ); - if (!is_concat(to_app(concatAst1))) { + if (!u.str.is_concat(to_app(concatAst1))) { TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); return; } - if (!is_concat(to_app(concatAst2))) { + if (!u.str.is_concat(to_app(concatAst2))) { TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); return; } @@ -4009,17 +4038,15 @@ void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { expr * str2Ast = v2_arg0; expr * n = v2_arg1; - const char *tmp = 0; - m_strutil.is_string(str1Ast, &tmp); - std::string str1Value(tmp); - m_strutil.is_string(str2Ast, &tmp); - std::string str2Value(tmp); + zstring str1Value, str2Value; + u.str.is_string(str1Ast, str1Value); + u.str.is_string(str2Ast, str2Value); - int str1Len = str1Value.length(); - int str2Len = str2Value.length(); + unsigned int str1Len = str1Value.length(); + unsigned int str2Len = str2Value.length(); int commonLen = (str1Len > str2Len) ? str2Len : str1Len; - if (str1Value.substr(0, commonLen) != str2Value.substr(0, commonLen)) { + if (str1Value.extract(0, commonLen) != str2Value.extract(0, commonLen)) { TRACE("t_str_detail", tout << "Conflict: " << mk_ismt2_pp(concatAst1, mgr) << " has no common prefix with " << mk_ismt2_pp(concatAst2, mgr) << std::endl;); expr_ref toNegate(mgr.mk_not(ctx.mk_eq_atom(concatAst1, concatAst2)), mgr); @@ -4027,7 +4054,7 @@ void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { return; } else { if (str1Len > str2Len) { - std::string deltaStr = str1Value.substr(str2Len, str1Len - str2Len); + zstring deltaStr = str1Value.extract(str2Len, str1Len - str2Len); expr_ref tmpAst(mk_concat(mk_string(deltaStr), y), mgr); if (!in_same_eqc(tmpAst, n)) { // break down option 4-1 @@ -4052,7 +4079,7 @@ void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { } } } else { - std::string deltaStr = str2Value.substr(str1Len, str2Len - str1Len); + zstring deltaStr = str2Value.extract(str1Len, str2Len - str1Len); expr_ref tmpAst(mk_concat(mk_string(deltaStr), n), mgr); if (!in_same_eqc(y, tmpAst)) { //break down option 4-3 @@ -4077,8 +4104,8 @@ bool theory_str::is_concat_eq_type5(expr * concatAst1, expr * concatAst2) { expr * v2_arg0 = to_app(concatAst2)->get_arg(0); expr * v2_arg1 = to_app(concatAst2)->get_arg(1); - if ((!m_strutil.is_string(v1_arg0)) && m_strutil.is_string(v1_arg1) - && (!m_strutil.is_string(v2_arg0)) && m_strutil.is_string(v2_arg1)) { + if ((!u.str.is_string(v1_arg0)) && u.str.is_string(v1_arg1) + && (!u.str.is_string(v2_arg0)) && u.str.is_string(v2_arg1)) { return true; } else { return false; @@ -4093,11 +4120,11 @@ void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; ); - if (!is_concat(to_app(concatAst1))) { + if (!u.str.is_concat(to_app(concatAst1))) { TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); return; } - if (!is_concat(to_app(concatAst2))) { + if (!u.str.is_concat(to_app(concatAst2))) { TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); return; } @@ -4112,17 +4139,15 @@ void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { expr * m = v2_arg0; expr * str2Ast = v2_arg1; - const char *tmp = 0; - m_strutil.is_string(str1Ast, &tmp); - std::string str1Value(tmp); - m_strutil.is_string(str2Ast, &tmp); - std::string str2Value(tmp); + zstring str1Value, str2Value; + u.str.is_string(str1Ast, str1Value); + u.str.is_string(str2Ast, str2Value); - int str1Len = str1Value.length(); - int str2Len = str2Value.length(); + unsigned int str1Len = str1Value.length(); + unsigned int str2Len = str2Value.length(); int cLen = (str1Len > str2Len) ? str2Len : str1Len; - if (str1Value.substr(str1Len - cLen, cLen) != str2Value.substr(str2Len - cLen, cLen)) { + if (str1Value.extract(str1Len - cLen, cLen) != str2Value.extract(str2Len - cLen, cLen)) { TRACE("t_str_detail", tout << "Conflict: " << mk_ismt2_pp(concatAst1, mgr) << " has no common suffix with " << mk_ismt2_pp(concatAst2, mgr) << std::endl;); expr_ref toNegate(mgr.mk_not(ctx.mk_eq_atom(concatAst1, concatAst2)), mgr); @@ -4130,7 +4155,7 @@ void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { return; } else { if (str1Len > str2Len) { - std::string deltaStr = str1Value.substr(0, str1Len - str2Len); + zstring deltaStr = str1Value.extract(0, str1Len - str2Len); expr_ref x_deltaStr(mk_concat(x, mk_string(deltaStr)), mgr); if (!in_same_eqc(m, x_deltaStr)) { expr_ref implyR(ctx.mk_eq_atom(m, x_deltaStr), mgr); @@ -4153,7 +4178,7 @@ void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { } } } else { - std::string deltaStr = str2Value.substr(0, str2Len - str1Len); + zstring deltaStr = str2Value.extract(0, str2Len - str1Len); expr_ref m_deltaStr(mk_concat(m, mk_string(deltaStr)), mgr); if (!in_same_eqc(x, m_deltaStr)) { expr_ref implyR(ctx.mk_eq_atom(x, m_deltaStr), mgr); @@ -4177,11 +4202,11 @@ bool theory_str::is_concat_eq_type6(expr * concatAst1, expr * concatAst2) { expr * v2_arg0 = to_app(concatAst2)->get_arg(0); expr * v2_arg1 = to_app(concatAst2)->get_arg(1); - if (m_strutil.is_string(v1_arg0) && (!m_strutil.is_string(v1_arg1)) - && (!m_strutil.is_string(v2_arg0)) && m_strutil.is_string(v2_arg1)) { + if (u.str.is_string(v1_arg0) && (!u.str.is_string(v1_arg1)) + && (!u.str.is_string(v2_arg0)) && u.str.is_string(v2_arg1)) { return true; - } else if (m_strutil.is_string(v2_arg0) && (!m_strutil.is_string(v2_arg1)) - && (!m_strutil.is_string(v1_arg0)) && m_strutil.is_string(v1_arg1)) { + } else if (u.str.is_string(v2_arg0) && (!u.str.is_string(v2_arg1)) + && (!u.str.is_string(v1_arg0)) && u.str.is_string(v1_arg1)) { return true; } else { return false; @@ -4196,11 +4221,11 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; ); - if (!is_concat(to_app(concatAst1))) { + if (!u.str.is_concat(to_app(concatAst1))) { TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); return; } - if (!is_concat(to_app(concatAst2))) { + if (!u.str.is_concat(to_app(concatAst2))) { TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); return; } @@ -4216,7 +4241,7 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { expr * m = NULL; expr * str2Ast = NULL; - if (m_strutil.is_string(v1_arg0)) { + if (u.str.is_string(v1_arg0)) { str1Ast = v1_arg0; y = v1_arg1; m = v2_arg0; @@ -4228,14 +4253,12 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { str2Ast = v1_arg1; } - const char *tmp = 0; - m_strutil.is_string(str1Ast, &tmp); - std::string str1Value(tmp); - m_strutil.is_string(str2Ast, &tmp); - std::string str2Value(tmp); + zstring str1Value, str2Value; + u.str.is_string(str1Ast, str1Value); + u.str.is_string(str2Ast, str2Value); - int str1Len = str1Value.length(); - int str2Len = str2Value.length(); + unsigned int str1Len = str1Value.length(); + unsigned int str2Len = str2Value.length(); //---------------------------------------- //(a) |---str1---|----y----| @@ -4248,11 +4271,11 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { // |------m------|-str2-| //---------------------------------------- - std::list overlapLen; + std::list overlapLen; overlapLen.push_back(0); - for (int i = 1; i <= str1Len && i <= str2Len; i++) { - if (str1Value.substr(str1Len - i, i) == str2Value.substr(0, i)) + for (unsigned int i = 1; i <= str1Len && i <= str2Len; i++) { + if (str1Value.extract(str1Len - i, i) == str2Value.extract(0, i)) overlapLen.push_back(i); } @@ -4351,10 +4374,10 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { } } - for (std::list::iterator itor = overlapLen.begin(); itor != overlapLen.end(); itor++) { - int overLen = *itor; - std::string prefix = str1Value.substr(0, str1Len - overLen); - std::string suffix = str2Value.substr(overLen, str2Len - overLen); + for (std::list::iterator itor = overlapLen.begin(); itor != overlapLen.end(); itor++) { + unsigned int overLen = *itor; + zstring prefix = str1Value.extract(0, str1Len - overLen); + zstring suffix = str2Value.extract(overLen, str2Len - overLen); expr_ref_vector and_item(mgr); @@ -4408,12 +4431,13 @@ void theory_str::process_unroll_eq_const_str(expr * unrollFunc, expr * constStr) if (!is_Unroll(to_app(unrollFunc))) { return; } - if (!m_strutil.is_string(constStr)) { + if (!u.str.is_string(constStr)) { return; } expr * funcInUnroll = to_app(unrollFunc)->get_arg(0); - std::string strValue = m_strutil.get_string_constant_value(constStr); + zstring strValue; + u.str.is_string(constStr, strValue); TRACE("t_str_detail", tout << "unrollFunc: " << mk_pp(unrollFunc, m) << std::endl << "constStr: " << mk_pp(constStr, m) << std::endl;); @@ -4422,7 +4446,7 @@ void theory_str::process_unroll_eq_const_str(expr * unrollFunc, expr * constStr) return; } - if (is_Str2Reg(to_app(funcInUnroll))) { + if (u.re.is_to_re(to_app(funcInUnroll))) { unroll_str2reg_constStr(unrollFunc, constStr); return; } @@ -4504,12 +4528,14 @@ void theory_str::unroll_str2reg_constStr(expr * unrollFunc, expr * eqConstStr) { expr * strInStr2RegFunc = to_app(str2RegFunc)->get_arg(0); expr * oriCnt = to_app(unrollFunc)->get_arg(1); - std::string strValue = m_strutil.get_string_constant_value(eqConstStr); - std::string regStrValue = m_strutil.get_string_constant_value(strInStr2RegFunc); - int strLen = strValue.length(); - int regStrLen = regStrValue.length(); + zstring strValue; + u.str.is_string(eqConstStr, strValue); + zstring regStrValue; + u.str.is_string(strInStr2RegFunc, regStrValue); + unsigned int strLen = strValue.length(); + unsigned int regStrLen = regStrValue.length(); SASSERT(regStrLen != 0); // this should never occur -- the case for empty string is handled elsewhere - int cnt = strLen / regStrLen; + unsigned int cnt = strLen / regStrLen; expr_ref implyL(ctx.mk_eq_atom(unrollFunc, eqConstStr), m); expr_ref implyR1(ctx.mk_eq_atom(oriCnt, mk_int(cnt)), m); @@ -4537,7 +4563,7 @@ expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { expr * theory_str::z3str2_get_eqc_value(expr * n , bool & hasEqcValue) { expr * curr = n; do { - if (m_strutil.is_string(curr)) { + if (u.str.is_string(curr)) { hasEqcValue = true; return curr; } @@ -4649,14 +4675,16 @@ bool theory_str::get_len_value(expr* e, rational& val) { while (!todo.empty()) { expr* c = todo.back(); todo.pop_back(); - if (is_concat(to_app(c))) { + if (u.str.is_concat(to_app(c))) { e1 = to_app(c)->get_arg(0); e2 = to_app(c)->get_arg(1); todo.push_back(e1); todo.push_back(e2); } - else if (is_string(to_app(c))) { - int sl = m_strutil.get_string_constant_value(c).length(); + else if (u.str.is_string(to_app(c))) { + zstring tmp; + u.str.is_string(to_app(c), tmp); + unsigned int sl = tmp.length(); val += rational(sl); } else { @@ -4738,7 +4766,7 @@ expr * theory_str::collect_eq_nodes(expr * n, expr_ref_vector & eqcSet) { expr * ex = n; do { - if (m_strutil.is_string(to_app(ex))) { + if (u.str.is_string(to_app(ex))) { constStrNode = ex; } eqcSet.push_back(ex); @@ -4753,7 +4781,7 @@ expr * theory_str::collect_eq_nodes(expr * n, expr_ref_vector & eqcSet) { */ void theory_str::get_const_str_asts_in_node(expr * node, expr_ref_vector & astList) { ast_manager & m = get_manager(); - if (m_strutil.is_string(node)) { + if (u.str.is_string(node)) { astList.push_back(node); //} else if (getNodeType(t, node) == my_Z3_Func) { } else if (is_app(node)) { @@ -4806,7 +4834,8 @@ void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { if (strAst != constNode) { litems.push_back(ctx.mk_eq_atom(strAst, constNode)); } - std::string strConst = m_strutil.get_string_constant_value(constNode); + zstring strConst; + u.str.is_string(constNode, strConst); bool subStrHasEqcValue = false; expr * substrValue = get_eqc_value(substrAst, subStrHasEqcValue); if (substrValue != substrAst) { @@ -4815,11 +4844,12 @@ void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { if (subStrHasEqcValue) { // subStr has an eqc constant value - std::string subStrConst = m_strutil.get_string_constant_value(substrValue); + zstring subStrConst; + u.str.is_string(substrValue, subStrConst); - TRACE("t_str_detail", tout << "strConst = " << strConst << ", subStrConst = " << subStrConst << std::endl;); + TRACE("t_str_detail", tout << "strConst = " << strConst << ", subStrConst = " << subStrConst << "\n";); - if (strConst.find(subStrConst) != std::string::npos) { + if (strConst.contains(subStrConst)) { //implyR = ctx.mk_eq(ctx, boolVar, Z3_mk_true(ctx)); implyR = boolVar; } else { @@ -4845,8 +4875,9 @@ void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { get_const_str_asts_in_node(aConcat, constList); for (expr_ref_vector::iterator cstItor = constList.begin(); cstItor != constList.end(); cstItor++) { - std::string pieceStr = m_strutil.get_string_constant_value(*cstItor); - if (strConst.find(pieceStr) == std::string::npos) { + zstring pieceStr; + u.str.is_string(*cstItor, pieceStr); + if (strConst.contains(pieceStr)) { counterEgFound = true; if (aConcat != substrAst) { litems.push_back(ctx.mk_eq_atom(substrAst, aConcat)); @@ -4883,9 +4914,10 @@ void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { } if (strHasEqcValue) { - std::string strConst = m_strutil.get_string_constant_value(strValue); - std::string subStrConst = m_strutil.get_string_constant_value(constNode); - if (strConst.find(subStrConst) != std::string::npos) { + zstring strConst, subStrConst; + u.str.is_string(strValue, strConst); + u.str.is_string(constNode, subStrConst); + if (strConst.contains(subStrConst)) { //implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_true(ctx)); implyR = boolVar; } else { @@ -4941,19 +4973,21 @@ void theory_str::check_contain_by_substr(expr * varNode, expr_ref_vector & willE if (strValue != strAst) { litems.push_back(ctx.mk_eq_atom(strAst, strValue)); } - std::string strConst = m_strutil.get_string_constant_value(strValue); + zstring strConst; + u.str.is_string(strValue, strConst); // iterate eqc (also eqc-to-be) of substr for (expr_ref_vector::iterator itAst = willEqClass.begin(); itAst != willEqClass.end(); itAst++) { bool counterEgFound = false; - if (is_concat(to_app(*itAst))) { + if (u.str.is_concat(to_app(*itAst))) { expr_ref_vector constList(m); // get constant strings in concat app * aConcat = to_app(*itAst); get_const_str_asts_in_node(aConcat, constList); for (expr_ref_vector::iterator cstItor = constList.begin(); cstItor != constList.end(); cstItor++) { - std::string pieceStr = m_strutil.get_string_constant_value(*cstItor); - if (strConst.find(pieceStr) == std::string::npos) { + zstring pieceStr; + u.str.is_string(*cstItor, pieceStr); + if (!strConst.contains(pieceStr)) { TRACE("t_str_detail", tout << "Inconsistency found!" << std::endl;); counterEgFound = true; if (aConcat != substrAst) { @@ -5045,18 +5079,19 @@ void theory_str::check_contain_by_eq_nodes(expr * n1, expr * n2) { litems1.push_back(ctx.mk_eq_atom(subAst2, subValue2)); } - std::string subConst1 = m_strutil.get_string_constant_value(subValue1); - std::string subConst2 = m_strutil.get_string_constant_value(subValue2); + zstring subConst1, subConst2; + u.str.is_string(subValue1, subConst1); + u.str.is_string(subValue2, subConst2); expr_ref implyR(m); if (subConst1 == subConst2) { // key1.first = key2.first /\ key1.second = key2.second // ==> (containPairBoolMap[key1] = containPairBoolMap[key2]) implyR = ctx.mk_eq_atom(contain_pair_bool_map[key1], contain_pair_bool_map[key2]); - } else if (subConst1.find(subConst2) != std::string::npos) { + } else if (subConst1.contains(subConst2)) { // key1.first = key2.first /\ Contains(key1.second, key2.second) // ==> (containPairBoolMap[key1] --> containPairBoolMap[key2]) implyR = rewrite_implication(contain_pair_bool_map[key1], contain_pair_bool_map[key2]); - } else if (subConst2.find(subConst1) != std::string::npos) { + } else if (subConst2.contains(subConst1)) { // key1.first = key2.first /\ Contains(key2.second, key1.second) // ==> (containPairBoolMap[key2] --> containPairBoolMap[key1]) implyR = rewrite_implication(contain_pair_bool_map[key2], contain_pair_bool_map[key1]); @@ -5191,19 +5226,20 @@ void theory_str::check_contain_by_eq_nodes(expr * n1, expr * n2) { litems1.push_back(ctx.mk_eq_atom(str2, strVal2)); } - std::string const1 = m_strutil.get_string_constant_value(strVal1); - std::string const2 = m_strutil.get_string_constant_value(strVal2); + zstring const1, const2; + u.str.is_string(strVal1, const1); + u.str.is_string(strVal2, const2); expr_ref implyR(m); if (const1 == const2) { // key1.second = key2.second /\ key1.first = key2.first // ==> (containPairBoolMap[key1] = containPairBoolMap[key2]) implyR = ctx.mk_eq_atom(contain_pair_bool_map[key1], contain_pair_bool_map[key2]); - } else if (const1.find(const2) != std::string::npos) { + } else if (const1.contains(const2)) { // key1.second = key2.second /\ Contains(key1.first, key2.first) // ==> (containPairBoolMap[key2] --> containPairBoolMap[key1]) implyR = rewrite_implication(contain_pair_bool_map[key2], contain_pair_bool_map[key1]); - } else if (const2.find(const1) != std::string::npos) { + } else if (const2.contains(const1)) { // key1.first = key2.first /\ Contains(key2.first, key1.first) // ==> (containPairBoolMap[key1] --> containPairBoolMap[key2]) implyR = rewrite_implication(contain_pair_bool_map[key1], contain_pair_bool_map[key2]); @@ -5398,7 +5434,7 @@ void theory_str::check_contain_in_new_eq(expr * n1, expr * n2) { expr * theory_str::dealias_node(expr * node, std::map & varAliasMap, std::map & concatAliasMap) { if (variable_set.find(node) != variable_set.end()) { return get_alias_index_ast(varAliasMap, node); - } else if (is_concat(to_app(node))) { + } else if (u.str.is_concat(to_app(node))) { return get_alias_index_ast(concatAliasMap, node); } return node; @@ -5427,13 +5463,13 @@ void theory_str::get_grounded_concats(expr* node, std::map & varAl ast_manager & m = get_manager(); // const strings: node is de-aliased - if (m_strutil.is_string(node)) { + if (u.str.is_string(node)) { std::vector concatNodes; concatNodes.push_back(node); groundedMap[node][concatNodes].clear(); // no condition } // Concat functions - else if (is_concat(to_app(node))) { + else if (u.str.is_concat(to_app(node))) { // if "node" equals to a constant string, thenjust push the constant into the concat vector // Again "node" has been de-aliased at the very beginning if (concatConstMap.find(node) != concatConstMap.end()) { @@ -5461,7 +5497,7 @@ void theory_str::get_grounded_concats(expr* node, std::map & varAl ndVec.insert(ndVec.end(), arg0_grdItor->first.begin(), arg0_grdItor->first.end()); int arg0VecSize = arg0_grdItor->first.size(); int arg1VecSize = arg1_grdItor->first.size(); - if (arg0VecSize > 0 && arg1VecSize > 0 && m_strutil.is_string(arg0_grdItor->first[arg0VecSize - 1]) && m_strutil.is_string(arg1_grdItor->first[0])) { + if (arg0VecSize > 0 && arg1VecSize > 0 && u.str.is_string(arg0_grdItor->first[arg0VecSize - 1]) && u.str.is_string(arg1_grdItor->first[0])) { ndVec.pop_back(); ndVec.push_back(mk_concat(arg0_grdItor->first[arg0VecSize - 1], arg1_grdItor->first[0])); for (int i = 1; i < arg1VecSize; i++) { @@ -5565,11 +5601,11 @@ bool theory_str::is_partial_in_grounded_concat(const std::vector & strVec } if (subStrCnt == 1) { - if (m_strutil.is_string(subStrVec[0])) { - std::string subStrVal = m_strutil.get_string_constant_value(subStrVec[0]); + zstring subStrVal; + if (u.str.is_string(subStrVec[0]), subStrVal) { for (int i = 0; i < strCnt; i++) { - if (m_strutil.is_string(strVec[i])) { - std::string strVal = m_strutil.get_string_constant_value(strVec[i]); + zstring strVal; + if (u.str.is_string(strVec[i], strVal)) { if (strVal.find(subStrVal) != std::string::npos) { return true; } @@ -5589,12 +5625,12 @@ bool theory_str::is_partial_in_grounded_concat(const std::vector & strVec // * constant: a suffix of a note in strVec[i] // * variable: bool firstNodesOK = true; - if (m_strutil.is_string(subStrVec[0])) { - std::string subStrHeadVal = m_strutil.get_string_constant_value(subStrVec[0]); - if (m_strutil.is_string(strVec[i])) { - std::string strHeadVal = m_strutil.get_string_constant_value(strVec[i]); - if (strHeadVal.size() >= subStrHeadVal.size()) { - std::string suffix = strHeadVal.substr(strHeadVal.size() - subStrHeadVal.size(), subStrHeadVal.size()); + zstring subStrHeadVal; + if (u.str.is_string(subStrVec[0], subStrHeadVal)) { + zstring strHeadVal; + if (u.str.is_string(strVec[i], strHeadVal)) { + if (strHeadVal.length() >= subStrHeadVal.length()) { + std::string suffix = strHeadVal.extract(strHeadVal.length() - subStrHeadVal.length(), subStrHeadVal.length()); if (suffix != subStrHeadVal) { firstNodesOK = false; } @@ -5625,12 +5661,12 @@ bool theory_str::is_partial_in_grounded_concat(const std::vector & strVec // tail nodes int tailIdx = i + subStrCnt - 1; - if (m_strutil.is_string(subStrVec[subStrCnt - 1])) { - std::string subStrTailVal = m_strutil.get_string_constant_value(subStrVec[subStrCnt - 1]); - if (m_strutil.is_string(strVec[tailIdx])) { - std::string strTailVal = m_strutil.get_string_constant_value(strVec[tailIdx]); - if (strTailVal.size() >= subStrTailVal.size()) { - std::string prefix = strTailVal.substr(0, subStrTailVal.size()); + zstring subStrTailVal; + if (u.str.is_string(subStrVec[subStrCnt - 1], subStrTailVal)) { + zstring strTailVal; + if (u.str.is_string(strVec[tailIdx], strTailVal)) { + if (strTailVal.length() >= subStrTailVal.length()) { + zstring prefix = strTailVal.extract(0, subStrTailVal.length()); if (prefix == subStrTailVal) { return true; } else { @@ -5721,44 +5757,44 @@ void theory_str::compute_contains(std::map & varAliasMap, } } -bool theory_str::can_concat_eq_str(expr * concat, std::string str) { +bool theory_str::can_concat_eq_str(expr * concat, zstring& str) { int strLen = str.length(); - if (is_concat(to_app(concat))) { + if (u.str.is_concat(to_app(concat))) { ptr_vector args; get_nodes_in_concat(concat, args); expr * ml_node = args[0]; expr * mr_node = args[args.size() - 1]; - if (m_strutil.is_string(ml_node)) { - std::string ml_str = m_strutil.get_string_constant_value(ml_node); - int ml_len = ml_str.length(); + zstring ml_str; + if (u.str.is_string(ml_node, ml_str)) { + unsigned int ml_len = ml_str.length(); if (ml_len > strLen) { return false; } - int cLen = ml_len; - if (ml_str != str.substr(0, cLen)) { + unsigned int cLen = ml_len; + if (ml_str != str.extract(0, cLen)) { return false; } } - if (m_strutil.is_string(mr_node)) { - std::string mr_str = m_strutil.get_string_constant_value(mr_node); - int mr_len = mr_str.length(); + zstring mr_str; + if (u.str.is_string(mr_node, mr_str)) { + unsigned int mr_len = mr_str.length(); if (mr_len > strLen) { return false; } - int cLen = mr_len; - if (mr_str != str.substr(strLen - cLen, cLen)) { + unsigned int cLen = mr_len; + if (mr_str != str.extract(strLen - cLen, cLen)) { return false; } } - int sumLen = 0; + unsigned int sumLen = 0; for (unsigned int i = 0 ; i < args.size() ; i++) { expr * oneArg = args[i]; - if (m_strutil.is_string(oneArg)) { - std::string arg_str = m_strutil.get_string_constant_value(oneArg); - if (str.find(arg_str) == std::string::npos) { + zstring arg_str; + if (u.str.is_string(oneArg, arg_str)) { + if (str.contains(arg_str)) { return false; } sumLen += arg_str.length(); @@ -5773,17 +5809,16 @@ bool theory_str::can_concat_eq_str(expr * concat, std::string str) { } bool theory_str::can_concat_eq_concat(expr * concat1, expr * concat2) { - if (is_concat(to_app(concat1)) && is_concat(to_app(concat2))) { + if (u.str.is_concat(to_app(concat1)) && u.str.is_concat(to_app(concat2))) { { // Suppose concat1 = (Concat X Y) and concat2 = (Concat M N). expr * concat1_mostL = getMostLeftNodeInConcat(concat1); expr * concat2_mostL = getMostLeftNodeInConcat(concat2); // if both X and M are constant strings, check whether they have the same prefix - if (m_strutil.is_string(concat1_mostL) && m_strutil.is_string(concat2_mostL)) { - std::string concat1_mostL_str = m_strutil.get_string_constant_value(concat1_mostL); - std::string concat2_mostL_str = m_strutil.get_string_constant_value(concat2_mostL); - int cLen = std::min(concat1_mostL_str.length(), concat2_mostL_str.length()); - if (concat1_mostL_str.substr(0, cLen) != concat2_mostL_str.substr(0, cLen)) { + zstring concat1_mostL_str, concat2_mostL_str; + if (u.str.is_string(concat1_mostL, concat1_mostL_str) && u.str.is_string(concat2_mostL, concat2_mostL_str)) { + unsigned int cLen = std::min(concat1_mostL_str.length(), concat2_mostL_str.length()); + if (concat1_mostL_str.extract(0, cLen) != concat2_mostL_str.extract(0, cLen)) { return false; } } @@ -5793,12 +5828,11 @@ bool theory_str::can_concat_eq_concat(expr * concat1, expr * concat2) { // Similarly, if both Y and N are constant strings, check whether they have the same suffix expr * concat1_mostR = getMostRightNodeInConcat(concat1); expr * concat2_mostR = getMostRightNodeInConcat(concat2); - if (m_strutil.is_string(concat1_mostR) && m_strutil.is_string(concat2_mostR)) { - std::string concat1_mostR_str = m_strutil.get_string_constant_value(concat1_mostR); - std::string concat2_mostR_str = m_strutil.get_string_constant_value(concat2_mostR); - int cLen = std::min(concat1_mostR_str.length(), concat2_mostR_str.length()); - if (concat1_mostR_str.substr(concat1_mostR_str.length() - cLen, cLen) != - concat2_mostR_str.substr(concat2_mostR_str.length() - cLen, cLen)) { + zstring concat1_mostR_str, concat2_mostR_str; + if (u.str.is_string(concat1_mostR, concat1_mostR_str) && u.str.is_string(concat2_mostR, concat2_mostR_str)) { + unsigned int cLen = std::min(concat1_mostR_str.length(), concat2_mostR_str.length()); + if (concat1_mostR_str.extract(concat1_mostR_str.length() - cLen, cLen) != + concat2_mostR_str.extract(concat2_mostR_str.length() - cLen, cLen)) { return false; } } @@ -5817,31 +5851,29 @@ bool theory_str::can_two_nodes_eq(expr * n1, expr * n2) { app * n2_curr = to_app(n2); // case 0: n1_curr is const string, n2_curr is const string - if (is_string(n1_curr) && is_string(n2_curr)) { + if (u.str.is_string(n1_curr) && u.str.is_string(n2_curr)) { if (n1_curr != n2_curr) { return false; } } // case 1: n1_curr is concat, n2_curr is const string - else if (is_concat(n1_curr) && is_string(n2_curr)) { - const char * tmp = 0; - m_strutil.is_string(n2_curr, & tmp); - std::string n2_curr_str(tmp); + else if (u.str.is_concat(n1_curr) && u.str.is_string(n2_curr)) { + zstring n2_curr_str; + u.str.is_string(n2_curr, n2_curr_str); if (!can_concat_eq_str(n1_curr, n2_curr_str)) { return false; } } // case 2: n2_curr is concat, n1_curr is const string - else if (is_concat(n2_curr) && is_string(n1_curr)) { - const char * tmp = 0; - m_strutil.is_string(n1_curr, & tmp); - std::string n1_curr_str(tmp); + else if (u.str.is_concat(n2_curr) && u.str.is_string(n1_curr)) { + zstring n1_curr_str; + u.str.is_string(n1_curr, n1_curr_str); if (!can_concat_eq_str(n2_curr, n1_curr_str)) { return false; } } // case 3: both are concats - else if (is_concat(n1_curr) && is_concat(n2_curr)) { + else if (u.str.is_concat(n1_curr) && u.str.is_concat(n2_curr)) { if (!can_concat_eq_concat(n1_curr, n2_curr)) { return false; } @@ -5857,9 +5889,11 @@ bool theory_str::check_length_const_string(expr * n1, expr * constStr) { ast_manager & mgr = get_manager(); context & ctx = get_context(); - rational strLen((unsigned) (m_strutil.get_string_constant_value(constStr).length())); + zstring tmp; + u.str.is_string(constStr, tmp); + rational strLen(tmp.length()); - if (is_concat(to_app(n1))) { + if (u.str.is_concat(to_app(n1))) { ptr_vector args; expr_ref_vector items(mgr); @@ -5870,7 +5904,7 @@ bool theory_str::check_length_const_string(expr * n1, expr * constStr) { rational argLen; bool argLen_exists = get_len_value(args[i], argLen); if (argLen_exists) { - if (!m_strutil.is_string(args[i])) { + if (!u.str.is_string(args[i])) { items.push_back(ctx.mk_eq_atom(mk_strlen(args[i]), mk_int(argLen))); } TRACE("t_str_detail", tout << "concat arg: " << mk_pp(args[i], mgr) << " has len = " << argLen.to_string() << std::endl;); @@ -5926,7 +5960,7 @@ bool theory_str::check_length_concat_concat(expr * n1, expr * n2) { bool argLen_exists = get_len_value(oneArg, argLen); if (argLen_exists) { sum1 += argLen; - if (!m_strutil.is_string(oneArg)) { + if (!u.str.is_string(oneArg)) { items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); } } else { @@ -5940,7 +5974,7 @@ bool theory_str::check_length_concat_concat(expr * n1, expr * n2) { bool argLen_exists = get_len_value(oneArg, argLen); if (argLen_exists) { sum2 += argLen; - if (!m_strutil.is_string(oneArg)) { + if (!u.str.is_string(oneArg)) { items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); } } else { @@ -5993,7 +6027,7 @@ bool theory_str::check_length_concat_var(expr * concat, expr * var) { rational argLen; bool argLen_exists = get_len_value(oneArg, argLen); if (argLen_exists) { - if (!m_strutil.is_string(oneArg) && !argLen.is_zero()) { + if (!u.str.is_string(oneArg) && !argLen.is_zero()) { items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); } sumLen += argLen; @@ -6036,8 +6070,8 @@ bool theory_str::check_length_var_var(expr * var1, expr * var2) { // - note that these are different from the semantics in Z3str2 bool theory_str::check_length_eq_var_concat(expr * n1, expr * n2) { // n1 and n2 are not const string: either variable or concat - bool n1Concat = is_concat(to_app(n1)); - bool n2Concat = is_concat(to_app(n2)); + bool n1Concat = u.str.is_concat(to_app(n1)); + bool n2Concat = u.str.is_concat(to_app(n2)); if (n1Concat && n2Concat) { return check_length_concat_concat(n1, n2); } @@ -6059,12 +6093,12 @@ bool theory_str::check_length_eq_var_concat(expr * n1, expr * n2) { // returns false if an inconsistency is detected, or true if no inconsistencies were found // - note that these are different from the semantics of checkLengConsistency() in Z3str2 bool theory_str::check_length_consistency(expr * n1, expr * n2) { - if (m_strutil.is_string(n1) && m_strutil.is_string(n2)) { + if (u.str.is_string(n1) && u.str.is_string(n2)) { // consistency has already been checked in can_two_nodes_eq(). return true; - } else if (m_strutil.is_string(n1) && (!m_strutil.is_string(n2))) { + } else if (u.str.is_string(n1) && (!u.str.is_string(n2))) { return check_length_const_string(n2, n1); - } else if (m_strutil.is_string(n2) && (!m_strutil.is_string(n1))) { + } else if (u.str.is_string(n2) && (!u.str.is_string(n1))) { return check_length_const_string(n1, n2); } else { // n1 and n2 are vars or concats @@ -6082,7 +6116,7 @@ bool theory_str::check_concat_len_in_eqc(expr * concat) { expr * eqc_n = concat; do { - if (is_concat(to_app(eqc_n))) { + if (u.str.is_concat(to_app(eqc_n))) { rational unused; bool status = infer_len_concat(eqc_n, unused); if (status) { @@ -6114,13 +6148,15 @@ void theory_str::check_regex_in(expr * nn1, expr * nn2) { std::set::iterator strItor = regex_in_var_reg_str_map[*itor].begin(); for (; strItor != regex_in_var_reg_str_map[*itor].end(); strItor++) { std::string regStr = *strItor; - std::string constStrValue = m_strutil.get_string_constant_value(constStr); + zstring constStrValue; + u.str.is_string(constStr, constStrValue); std::pair key1 = std::make_pair(*itor, regStr); if (regex_in_bool_map.find(key1) != regex_in_bool_map.end()) { expr * boolVar = regex_in_bool_map[key1]; // actually the RegexIn term app * a_regexIn = to_app(boolVar); expr * regexTerm = a_regexIn->get_arg(1); + // TODO figure out regex NFA stuff if (regex_nfa_cache.find(regexTerm) == regex_nfa_cache.end()) { TRACE("t_str_detail", tout << "regex_nfa_cache: cache miss" << std::endl;); regex_nfa_cache[regexTerm] = nfa(m_strutil, regexTerm); @@ -6159,16 +6195,14 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { TRACE("t_str_detail", tout << mk_ismt2_pp(concat, m) << " == " << mk_ismt2_pp(str, m) << std::endl;); - if (is_concat(to_app(concat)) && is_string(to_app(str))) { - const char * tmp = 0; - m_strutil.is_string(str, & tmp); - std::string const_str(tmp); + zstring const_str; + if (u.str.is_concat(to_app(concat)) && u.str.is_string(to_app(str), const_str)) { app * a_concat = to_app(concat); SASSERT(a_concat->get_num_args() == 2); expr * a1 = a_concat->get_arg(0); expr * a2 = a_concat->get_arg(1); - if (const_str == "") { + if (const_str.empty()) { TRACE("t_str", tout << "quick path: concat == \"\"" << std::endl;); // assert the following axiom: // ( (Concat a1 a2) == "" ) -> ( (a1 == "") AND (a2 == "") ) @@ -6211,26 +6245,22 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { if (newConcat == str) { return; } - if (!is_concat(to_app(newConcat))) { + if (!u.str.is_concat(to_app(newConcat))) { return; } if (arg1_has_eqc_value && arg2_has_eqc_value) { // Case 1: Concat(const, const) == const TRACE("t_str", tout << "Case 1: Concat(const, const) == const" << std::endl;); - const char * str1; - m_strutil.is_string(arg1, & str1); - std::string arg1_str(str1); + zstring arg1_str, arg2_str; + u.str.is_string(arg1, arg1_str); + u.str.is_string(arg2, arg2_str); - const char * str2; - m_strutil.is_string(arg2, & str2); - std::string arg2_str(str2); - - std::string result_str = arg1_str + arg2_str; + zstring result_str = arg1_str + arg2_str; if (result_str != const_str) { // Inconsistency TRACE("t_str", tout << "inconsistency detected: \"" << arg1_str << "\" + \"" << arg2_str << - "\" != \"" << const_str << "\"" << std::endl;); + "\" != \"" << const_str << "\"\n"); expr_ref equality(ctx.mk_eq_atom(concat, str), m); expr_ref diseq(m.mk_not(equality), m); assert_axiom(diseq); @@ -6239,31 +6269,30 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } else if (!arg1_has_eqc_value && arg2_has_eqc_value) { // Case 2: Concat(var, const) == const TRACE("t_str", tout << "Case 2: Concat(var, const) == const" << std::endl;); - const char * str2; - m_strutil.is_string(arg2, & str2); - std::string arg2_str(str2); - int resultStrLen = const_str.length(); - int arg2StrLen = arg2_str.length(); + zstring arg2_str; + u.str.is_string(arg2, arg2_str); + unsigned int resultStrLen = const_str.length(); + unsigned int arg2StrLen = arg2_str.length(); if (resultStrLen < arg2StrLen) { // Inconsistency TRACE("t_str", tout << "inconsistency detected: \"" << arg2_str << "\" is longer than \"" << const_str << "\"," - << " so cannot be concatenated with anything to form it" << std::endl;); + << " so cannot be concatenated with anything to form it\n"); expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); expr_ref diseq(m.mk_not(equality), m); assert_axiom(diseq); return; } else { int varStrLen = resultStrLen - arg2StrLen; - std::string firstPart = const_str.substr(0, varStrLen); - std::string secondPart = const_str.substr(varStrLen, arg2StrLen); + zstring firstPart = const_str.extract(0, varStrLen); + zstring secondPart = const_str.extract(varStrLen, arg2StrLen); if (arg2_str != secondPart) { // Inconsistency TRACE("t_str", tout << "inconsistency detected: " << "suffix of concatenation result expected \"" << secondPart << "\", " << "actually \"" << arg2_str << "\"" - << std::endl;); + << "\n"); expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); expr_ref diseq(m.mk_not(equality), m); assert_axiom(diseq); @@ -6279,31 +6308,30 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } else if (arg1_has_eqc_value && !arg2_has_eqc_value) { // Case 3: Concat(const, var) == const TRACE("t_str", tout << "Case 3: Concat(const, var) == const" << std::endl;); - const char * str1; - m_strutil.is_string(arg1, & str1); - std::string arg1_str(str1); - int resultStrLen = const_str.length(); - int arg1StrLen = arg1_str.length(); + zstring arg1_str; + u.str.is_string(arg1, arg1_str); + unsigned int resultStrLen = const_str.length(); + unsigned int arg1StrLen = arg1_str.length(); if (resultStrLen < arg1StrLen) { // Inconsistency TRACE("t_str", tout << "inconsistency detected: \"" << arg1_str << "\" is longer than \"" << const_str << "\"," - << " so cannot be concatenated with anything to form it" << std::endl;); + << " so cannot be concatenated with anything to form it" << "\n";); expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); expr_ref diseq(m.mk_not(equality), m); assert_axiom(diseq); return; } else { int varStrLen = resultStrLen - arg1StrLen; - std::string firstPart = const_str.substr(0, arg1StrLen); - std::string secondPart = const_str.substr(arg1StrLen, varStrLen); + zstring firstPart = const_str.extract(0, arg1StrLen); + zstring secondPart = const_str.extract(arg1StrLen, varStrLen); if (arg1_str != firstPart) { // Inconsistency TRACE("t_str", tout << "inconsistency detected: " << "prefix of concatenation result expected \"" << secondPart << "\", " << "actually \"" << arg1_str << "\"" - << std::endl;); + << "\n";); expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); expr_ref diseq(m.mk_not(equality), m); assert_axiom(diseq); @@ -6327,7 +6355,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { if (arg1Len_exists || arg2Len_exists) { expr_ref ax_l1(ctx.mk_eq_atom(concat, str), m); expr_ref ax_l2(m); - std::string prefixStr, suffixStr; + zstring prefixStr, suffixStr; if (arg1Len_exists) { if (arg1Len.is_neg()) { TRACE("t_str_detail", tout << "length conflict: arg1Len = " << arg1Len << ", concatStrLen = " << concatStrLen << std::endl;); @@ -6341,9 +6369,9 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { return; } - prefixStr = const_str.substr(0, arg1Len.get_unsigned()); + prefixStr = const_str.extract(0, arg1Len.get_unsigned()); rational concat_minus_arg1 = concatStrLen - arg1Len; - suffixStr = const_str.substr(arg1Len.get_unsigned(), concat_minus_arg1.get_unsigned()); + suffixStr = const_str.extract(arg1Len.get_unsigned(), concat_minus_arg1.get_unsigned()); ax_l2 = ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1Len)); } else { // arg2's length is available @@ -6360,17 +6388,17 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } rational concat_minus_arg2 = concatStrLen - arg2Len; - prefixStr = const_str.substr(0, concat_minus_arg2.get_unsigned()); - suffixStr = const_str.substr(concat_minus_arg2.get_unsigned(), arg2Len.get_unsigned()); + prefixStr = const_str.extract(0, concat_minus_arg2.get_unsigned()); + suffixStr = const_str.extract(concat_minus_arg2.get_unsigned(), arg2Len.get_unsigned()); ax_l2 = ctx.mk_eq_atom(mk_strlen(arg2), mk_int(arg2Len)); } // consistency check - if (is_concat(to_app(arg1)) && !can_concat_eq_str(arg1, prefixStr)) { + if (u.str.is_concat(to_app(arg1)) && !can_concat_eq_str(arg1, prefixStr)) { expr_ref ax_r(m.mk_not(ax_l2), m); assert_implication(ax_l1, ax_r); return; } - if (is_concat(to_app(arg2)) && !can_concat_eq_str(arg2, suffixStr)) { + if (u.str.is_concat(to_app(arg2)) && !can_concat_eq_str(arg2, suffixStr)) { expr_ref ax_r(m.mk_not(ax_l2), m); assert_implication(ax_l1, ax_r); return; @@ -6379,10 +6407,10 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { r_items.push_back(ctx.mk_eq_atom(arg1, mk_string(prefixStr))); r_items.push_back(ctx.mk_eq_atom(arg2, mk_string(suffixStr))); if (!arg1Len_exists) { - r_items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(prefixStr.size()))); + r_items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(prefixStr.length()))); } if (!arg2Len_exists) { - r_items.push_back(ctx.mk_eq_atom(mk_strlen(arg2), mk_int(suffixStr.size()))); + r_items.push_back(ctx.mk_eq_atom(mk_strlen(arg2), mk_int(suffixStr.length()))); } expr_ref lhs(m.mk_and(ax_l1, ax_l2), m); expr_ref rhs(mk_and(r_items), m); @@ -6456,13 +6484,13 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { for (int i = 0; i < concatStrLen + 1; ++i) { expr_ref_vector and_items(m); - std::string prefixStr = const_str.substr(0, i); - std::string suffixStr = const_str.substr(i, concatStrLen - i); + zstring prefixStr = const_str.extract(0, i); + zstring suffixStr = const_str.extract(i, concatStrLen - i); // skip invalid options - if (is_concat(to_app(arg1)) && !can_concat_eq_str(arg1, prefixStr)) { + if (u.str.is_concat(to_app(arg1)) && !can_concat_eq_str(arg1, prefixStr)) { continue; } - if (is_concat(to_app(arg2)) && !can_concat_eq_str(arg2, suffixStr)) { + if (u.str.is_concat(to_app(arg2)) && !can_concat_eq_str(arg2, suffixStr)) { continue; } @@ -6530,8 +6558,8 @@ expr_ref theory_str::set_up_finite_model_test(expr * lhs, expr * rhs) { } // make things easy for the core wrt. testvar - expr_ref t1(ctx.mk_eq_atom(testvar, m_strutil.mk_string("")), m); - expr_ref t_yes(ctx.mk_eq_atom(testvar, m_strutil.mk_string("yes")), m); + expr_ref t1(ctx.mk_eq_atom(testvar, u.str.mk_string("")), m); + expr_ref t_yes(ctx.mk_eq_atom(testvar, u.str.mk_string("yes")), m); expr_ref testvaraxiom(m.mk_or(t1, t_yes), m); assert_axiom(testvaraxiom); @@ -6544,8 +6572,8 @@ void theory_str::finite_model_test(expr * testvar, expr * str) { context & ctx = get_context(); ast_manager & m = get_manager(); - if (!m_strutil.is_string(str)) return; - std::string s = m_strutil.get_string_constant_value(str); + zstring s; + if (!u.str.is_string(str, s)) return; if (s == "yes") { TRACE("t_str", tout << "start finite model test for " << mk_pp(testvar, m) << std::endl;); ptr_vector & vars = finite_model_test_varlists[testvar]; @@ -6642,7 +6670,7 @@ void theory_str::finite_model_test(expr * testvar, expr * str) { for (rational l = v_lower_bound; l <= v_upper_bound; l += rational::one()) { std::string lStr = l.to_string(); - expr_ref str_indicator(m_strutil.mk_string(lStr), m); + expr_ref str_indicator(u.str.mk_string(lStr), m); expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); orList.push_back(or_expr); expr_ref and_expr(ctx.mk_eq_atom(or_expr, ctx.mk_eq_atom(vLengthExpr, m_autil.mk_numeral(l, true))), m); @@ -6661,7 +6689,7 @@ void theory_str::finite_model_test(expr * testvar, expr * str) { } // (s == "yes") } -void theory_str::more_len_tests(expr * lenTester, std::string lenTesterValue) { +void theory_str::more_len_tests(expr * lenTester, zstring lenTesterValue) { ast_manager & m = get_manager(); if (lenTester_fvar_map.contains(lenTester)) { expr * fVar = lenTester_fvar_map[lenTester]; @@ -6673,7 +6701,7 @@ void theory_str::more_len_tests(expr * lenTester, std::string lenTesterValue) { } } -void theory_str::more_value_tests(expr * valTester, std::string valTesterValue) { +void theory_str::more_value_tests(expr * valTester, zstring valTesterValue) { ast_manager & m = get_manager(); expr * fVar = valueTester_fvar_map[valTester]; @@ -6689,7 +6717,8 @@ void theory_str::more_value_tests(expr * valTester, std::string valTesterValue) TRACE("t_str_binary_search", tout << "WARNING: length tester " << mk_pp(effectiveLenInd, m) << " at top of stack for " << mk_pp(fVar, m) << " has no EQC value" << std::endl;); } else { // safety check - std::string effectiveLenIndiStr = m_strutil.get_string_constant_value(len_indicator_value); + zstring effectiveLenIndiStr; + u.str.is_string(len_indicator_value, effectiveLenIndiStr); if (effectiveLenIndiStr == "more" || effectiveLenIndiStr == "less") { TRACE("t_str_binary_search", tout << "ERROR: illegal state -- requesting 'more value tests' but a length tester is not yet concrete!" << std::endl;); UNREACHABLE(); @@ -6704,13 +6733,14 @@ void theory_str::more_value_tests(expr * valTester, std::string valTesterValue) int lenTesterCount = fvar_lenTester_map[fVar].size(); expr * effectiveLenInd = NULL; - std::string effectiveLenIndiStr = ""; + zstring effectiveLenIndiStr = ""; for (int i = 0; i < lenTesterCount; ++i) { expr * len_indicator_pre = fvar_lenTester_map[fVar][i]; bool indicatorHasEqcValue = false; expr * len_indicator_value = get_eqc_value(len_indicator_pre, indicatorHasEqcValue); if (indicatorHasEqcValue) { - std::string len_pIndiStr = m_strutil.get_string_constant_value(len_indicator_value); + zstring len_pIndiStr; + u.str.is_string(len_indicator_value, len_pIndiStr); if (len_pIndiStr != "more") { effectiveLenInd = len_indicator_pre; effectiveLenIndiStr = len_pIndiStr; @@ -6728,14 +6758,13 @@ void theory_str::more_value_tests(expr * valTester, std::string valTesterValue) bool theory_str::free_var_attempt(expr * nn1, expr * nn2) { ast_manager & m = get_manager(); - - if (internal_lenTest_vars.contains(nn1) && m_strutil.is_string(nn2)) { + zstring nn2_str; + if (internal_lenTest_vars.contains(nn1) && u.str.is_string(nn2, nn2_str)) { TRACE("t_str", tout << "acting on equivalence between length tester var " << mk_ismt2_pp(nn1, m) << " and constant " << mk_ismt2_pp(nn2, m) << std::endl;); - more_len_tests(nn1, m_strutil.get_string_constant_value(nn2)); + more_len_tests(nn1, nn2_str); return true; - } else if (internal_valTest_vars.contains(nn1) && m_strutil.is_string(nn2)) { - std::string nn2_str = m_strutil.get_string_constant_value(nn2); + } else if (internal_valTest_vars.contains(nn1) && u.str.is_string(nn2, nn2_str)) { if (nn2_str == "more") { TRACE("t_str", tout << "acting on equivalence between value var " << mk_ismt2_pp(nn1, m) << " and constant " << mk_ismt2_pp(nn2, m) << std::endl;); @@ -6755,7 +6784,7 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { // both terms must be of sort String sort * lhs_sort = m.get_sort(lhs); sort * rhs_sort = m.get_sort(rhs); - sort * str_sort = m.mk_sort(get_family_id(), STRING_SORT); + sort * str_sort = u.str.mk_string_sort(); if (lhs_sort != str_sort || rhs_sort != str_sort) { TRACE("t_str_detail", tout << "skip equality: not String sort" << std::endl;); @@ -6776,7 +6805,7 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { return; } - if (is_concat(to_app(lhs)) && is_concat(to_app(rhs))) { + if (u.str.is_concat(to_app(lhs)) && u.str.is_concat(to_app(rhs))) { bool nn1HasEqcValue = false; bool nn2HasEqcValue = false; expr * nn1_value = get_eqc_value(lhs, nn1HasEqcValue); @@ -6993,7 +7022,7 @@ void theory_str::set_up_axioms(expr * ex) { context & ctx = get_context(); sort * ex_sort = m.get_sort(ex); - sort * str_sort = m.mk_sort(get_family_id(), STRING_SORT); + sort * str_sort = u.str.mk_string_sort(); sort * bool_sort = m.mk_bool_sort(); family_id m_arith_fid = m.mk_family_id("arith"); @@ -7011,23 +7040,23 @@ void theory_str::set_up_axioms(expr * ex) { if (is_app(ex)) { app * ap = to_app(ex); - if (is_concat(ap)) { + if (u.str.is_concat(ap)) { // if ex is a concat, set up concat axioms later m_concat_axiom_todo.push_back(n); // we also want to check whether we can eval this concat, // in case the rewriter did not totally finish with this term m_concat_eval_todo.push_back(n); - } else if (is_strlen(ap)) { + } else if (u.str.is_length(ap)) { // if the argument is a variable, // keep track of this for later, we'll need it during model gen expr * var = ap->get_arg(0); app * aVar = to_app(var); - if (aVar->get_num_args() == 0 && !is_string(aVar)) { + if (aVar->get_num_args() == 0 && !u.str.is_string(aVar)) { input_var_in_len.insert(var); } - } else if (is_CharAt(ap) || is_Substr(ap) || is_Replace(ap)) { + } else if (u.str.is_at(ap) || u.str.is_extract(ap) || u.str.is_replace(ap)) { m_library_aware_axiom_todo.push_back(n); - } else if (ap->get_num_args() == 0 && !is_string(ap)) { + } else if (ap->get_num_args() == 0 && !u.str.is_string(ap)) { // if ex is a variable, add it to our list of variables TRACE("t_str_detail", tout << "tracking variable " << mk_ismt2_pp(ap, get_manager()) << std::endl;); variable_set.insert(ex); @@ -7049,7 +7078,7 @@ void theory_str::set_up_axioms(expr * ex) { if (is_app(ex)) { app * ap = to_app(ex); - if (is_StartsWith(ap) || is_EndsWith(ap) || is_Contains(ap) || is_RegexIn(ap)) { + if (u.str.is_prefix(ap) || u.str.is_suffix(ap) || u.str.is_contains(ap) || u.str.is_in_re(ap)) { m_library_aware_axiom_todo.push_back(n); } } @@ -7068,9 +7097,10 @@ void theory_str::set_up_axioms(expr * ex) { if (is_app(ex)) { app * ap = to_app(ex); - if (is_Indexof(ap) || is_Indexof2(ap) || is_LastIndexof(ap)) { + // TODO indexof2/lastindexof + if (u.str.is_index(ap) /* || is_Indexof2(ap) || is_LastIndexof(ap) */) { m_library_aware_axiom_todo.push_back(n); - } else if (is_str_to_int(ap) || is_int_to_str(ap)) { + } else if (u.str.is_stoi(ap) || u.str.is_itos(ap)) { string_int_conversion_terms.push_back(ap); m_library_aware_axiom_todo.push_back(n); } @@ -7200,12 +7230,12 @@ void theory_str::recursive_check_variable_scope(expr * ex) { if (a->get_num_args() == 0) { // we only care about string variables sort * s = m.get_sort(ex); - sort * string_sort = m.mk_sort(get_family_id(), STRING_SORT); + sort * string_sort = u.str.mk_string_sort(); if (s != string_sort) { return; } // base case: string constant / var - if (m_strutil.is_string(a)) { + if (u.str.is_string(a)) { return; } else { // assume var @@ -7331,10 +7361,10 @@ void theory_str::classify_ast_by_type(expr * node, std::map & varMap // check whether the node is a function that we want to inspect else if (is_app(node)) { app * aNode = to_app(node); - if (is_strlen(aNode)) { + if (u.str.is_length(aNode)) { // Length return; - } else if (is_concat(aNode)) { + } else if (u.str.is_concat(aNode)) { expr * arg0 = aNode->get_arg(0); expr * arg1 = aNode->get_arg(1); bool arg0HasEq = false; @@ -7343,10 +7373,13 @@ void theory_str::classify_ast_by_type(expr * node, std::map & varMap expr * arg1Val = get_eqc_value(arg1, arg1HasEq); int canskip = 0; - if (arg0HasEq && m_strutil.get_string_constant_value(arg0Val).empty()) { + zstring tmp; + u.str.is_string(arg0Val, tmp); + if (arg0HasEq && tmp.empty()) { canskip = 1; } - if (canskip == 0 && arg1HasEq && m_strutil.get_string_constant_value(arg1Val).empty()) { + u.str.is_string(arg1Val, tmp); + if (canskip == 0 && arg1HasEq && tmp.empty()) { canskip = 1; } if (canskip == 0 && concatMap.find(node) == concatMap.end()) { @@ -7402,7 +7435,7 @@ inline expr * theory_str::get_alias_index_ast(std::map & aliasInde inline expr * theory_str::getMostLeftNodeInConcat(expr * node) { app * aNode = to_app(node); - if (!is_concat(aNode)) { + if (!u.str.is_concat(aNode)) { return node; } else { expr * concatArgL = aNode->get_arg(0); @@ -7412,7 +7445,7 @@ inline expr * theory_str::getMostLeftNodeInConcat(expr * node) { inline expr * theory_str::getMostRightNodeInConcat(expr * node) { app * aNode = to_app(node); - if (!is_concat(aNode)) { + if (!u.str.is_concat(aNode)) { return node; } else { expr * concatArgR = aNode->get_arg(1); @@ -7556,7 +7589,7 @@ void theory_str::trace_ctx_dep(std::ofstream & tout, enode * e_curr_end = e_curr; do { app * curr = e_curr->get_owner(); - if (is_concat(curr)) { + if (u.str.is_concat(curr)) { tout << " >>> " << mk_pp(curr, mgr) << std::endl; } e_curr = e_curr->get_next(); @@ -7691,7 +7724,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::mapget_arg(0); expr * arg1 = aCurr->get_arg(1); bool arg0HasEqcValue = false; @@ -7701,18 +7734,18 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map & strVarMap, std::mapfirst; do { - if (is_concat(to_app(curr))) { + if (u.str.is_concat(to_app(curr))) { if (aRoot == NULL) { aRoot = curr; } else { @@ -7780,7 +7813,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map & strVarMap, std::map::iterator itor1 = itor->second.begin(); itor1 != itor->second.end(); itor1++) { expr * concatNode = itor1->first; expr * mLNode = getMostLeftNodeInConcat(concatNode); - const char * strval; - if (m_strutil.is_string(to_app(mLNode), & strval)) { - if (mLConst == NULL && strcmp(strval, "") != 0) { + zstring strval; + if (u.str.is_string(to_app(mLNode), strval)) { + if (mLConst == NULL && strval.empty()) { mLConst = mLNode; } } else { @@ -7913,8 +7946,8 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map integer value < 0 if (Sval.empty()) { // ignore this. we should already assert the axiom for what happens when the string is "" @@ -8228,7 +8262,7 @@ bool theory_str::finalcheck_int2str(app * a) { rational ten(10); bool conversionOK = true; for (unsigned i = 0; i < Sval.length(); ++i) { - char digit = Sval.at(i); + char digit = (int)Sval[i]; if (isdigit((int)digit)) { std::string sDigit(1, digit); int val = atoi(sDigit.c_str()); @@ -8272,11 +8306,11 @@ void theory_str::collect_var_concat(expr * node, std::set & varSet, std:: } else if (is_app(node)) { app * aNode = to_app(node); - if (is_strlen(aNode)) { + if (u.str.is_length(aNode)) { // Length return; } - if (is_concat(aNode)) { + if (u.str.is_concat(aNode)) { expr * arg0 = aNode->get_arg(0); expr * arg1 = aNode->get_arg(1); if (concatSet.find(node) == concatSet.end()) { @@ -8406,7 +8440,7 @@ bool theory_str::propagate_length(std::set & varSet, std::set & co void theory_str::get_unique_non_concat_nodes(expr * node, std::set & argSet) { app * a_node = to_app(node); - if (!is_concat(a_node)) { + if (!u.str.is_concat(a_node)) { argSet.insert(node); return; } else { @@ -8447,7 +8481,7 @@ final_check_status theory_str::final_check_eh() { for (std::set::iterator it = eqc_roots.begin(); it != eqc_roots.end(); ++it) { enode * e = *it; app * a = e->get_owner(); - if (!(is_sort_of(m.get_sort(a), m_strutil.get_fid(), STRING_SORT))) { + if (!(m.get_sort(a) == u.str.mk_string_sort())) { TRACE("t_str_detail", tout << "EQC root " << mk_pp(a, m) << " not a string term; skipping" << std::endl;); } else { TRACE("t_str_detail", tout << "EQC root " << mk_pp(a, m) << " is a string term. Checking this EQC" << std::endl;); @@ -8516,9 +8550,10 @@ final_check_status theory_str::final_check_eh() { if (concat_lhs_haseqc && concat_rhs_haseqc && !var_haseqc) { TRACE("t_str_detail", tout << "backpropagate into " << mk_pp(var, m) << " = " << mk_pp(concat, m) << std::endl << "LHS ~= " << mk_pp(concat_lhs_str, m) << " RHS ~= " << mk_pp(concat_rhs_str, m) << std::endl;); - std::string lhsString = m_strutil.get_string_constant_value(concat_lhs_str); - std::string rhsString = m_strutil.get_string_constant_value(concat_rhs_str); - std::string concatString = lhsString + rhsString; + zstring lhsString, rhsString; + u.str.is_string(concat_lhs_str, lhsString); + u.str.is_string(concat_rhs_str, rhsString); + zstring concatString = lhsString + rhsString; expr_ref lhs1(ctx.mk_eq_atom(concat_lhs, concat_lhs_str), m); expr_ref lhs2(ctx.mk_eq_atom(concat_rhs, concat_rhs_str), m); expr_ref lhs(m.mk_and(lhs1, lhs2), m); @@ -8584,12 +8619,12 @@ final_check_status theory_str::final_check_eh() { bool addedStrIntAxioms = false; for (unsigned i = 0; i < string_int_conversion_terms.size(); ++i) { app * ex = to_app(string_int_conversion_terms[i].get()); - if (is_str_to_int(ex)) { + if (u.str.is_stoi(ex)) { bool axiomAdd = finalcheck_str2int(ex); if (axiomAdd) { addedStrIntAxioms = true; } - } else if (is_int_to_str(ex)) { + } else if (u.str.is_itos(ex)) { bool axiomAdd = finalcheck_int2str(ex); if (axiomAdd) { addedStrIntAxioms = true; @@ -8664,7 +8699,7 @@ final_check_status theory_str::final_check_eh() { expr * unroll = urItor->first; expr * curr = unroll; do { - if (is_concat(to_app(curr))) { + if (u.str.is_concat(to_app(curr))) { concatEqUnrollsMap[curr].insert(unroll); concatEqUnrollsMap[curr].insert(unrollGroup_map[unroll].begin(), unrollGroup_map[unroll].end()); } @@ -8690,7 +8725,7 @@ final_check_status theory_str::final_check_eh() { } else { fvUnrollSet.insert(concatArg1); } - } else if (is_concat(to_app(concatArg1))) { + } else if (u.str.is_concat(to_app(concatArg1))) { if (concatEqUnrollsMap.find(concatArg1) == concatEqUnrollsMap.end()) { arg1Bounded = true; } @@ -8702,7 +8737,7 @@ final_check_status theory_str::final_check_eh() { } else { fvUnrollSet.insert(concatArg2); } - } else if (is_concat(to_app(concatArg2))) { + } else if (u.str.is_concat(to_app(concatArg2))) { if (concatEqUnrollsMap.find(concatArg2) == concatEqUnrollsMap.end()) { arg2Bounded = true; } @@ -8794,10 +8829,11 @@ final_check_status theory_str::final_check_eh() { return FC_CONTINUE; // since by this point we've added axioms } -inline std::string int_to_string(int i) { +inline zstring int_to_string(int i) { std::stringstream ss; ss << i; - return ss.str(); + std::string str = ss.str(); + return zstring(str.c_str()); } inline std::string longlong_to_string(long long i) { @@ -8823,11 +8859,11 @@ void theory_str::print_value_tester_list(svector > & teste ); } -std::string theory_str::gen_val_string(int len, int_vector & encoding) { +zstring theory_str::gen_val_string(int len, int_vector & encoding) { SASSERT(charSetSize > 0); SASSERT(char_set != NULL); - std::string re = std::string(len, char_set[0]); + zstring re(len, (int) char_set[0]); for (int i = 0; i < (int) encoding.size() - 1; i++) { int idx = encoding[i]; re[len - 1 - i] = char_set[idx]; @@ -8876,7 +8912,7 @@ bool theory_str::get_next_val_encode(int_vector & base, int_vector & next) { } expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * val_indicator, - std::string lenStr, int tries) { + zstring lenStr, int tries) { ast_manager & m = get_manager(); context & ctx = get_context(); @@ -8894,7 +8930,7 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * // {0, 0, 1} // the last item "1" shows this is not a valid encoding, and we have covered all space // ---------------------------------------------------------------------------------------- - int len = atoi(lenStr.c_str()); + int len = atoi(lenStr.encode().c_str()); bool coverAll = false; svector options; int_vector base; @@ -8903,8 +8939,8 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * << "freeVar = " << mk_ismt2_pp(freeVar, m) << std::endl << "len_indicator = " << mk_ismt2_pp(len_indicator, m) << std::endl << "val_indicator = " << mk_ismt2_pp(val_indicator, m) << std::endl - << "lenstr = " << lenStr << std::endl - << "tries = " << tries << std::endl; + << "lenstr = " << lenStr << "\n" + << "tries = " << tries << "\n"; if (m_params.m_AggressiveValueTesting) { tout << "note: aggressive value testing is enabled" << std::endl; } @@ -8953,7 +8989,7 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * ctx.force_phase(l); } - std::string aStr = gen_val_string(len, options[i - l]); + zstring aStr = gen_val_string(len, options[i - l]); expr * strAst; if (m_params.m_UseFastValueTesterCache) { if (!valueTesterCache.find(aStr, strAst)) { @@ -8996,7 +9032,7 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * // Should add ($$_len_x_j = 16) /\ ($$_val_x_16_i = "more") // --------------------------------------- andList.reset(); - andList.push_back(m.mk_eq(len_indicator, mk_string(lenStr.c_str()))); + andList.push_back(m.mk_eq(len_indicator, mk_string(lenStr))); for (int i = 0; i < tries; i++) { expr * vTester = fvar_valueTester_map[freeVar][len][i].second; if (vTester != val_indicator) @@ -9019,10 +9055,10 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * } expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, - std::string len_valueStr, expr * valTesterInCbEq, std::string valTesterValueStr) { + zstring len_valueStr, expr * valTesterInCbEq, zstring valTesterValueStr) { ast_manager & m = get_manager(); - int len = atoi(len_valueStr.c_str()); + int len = atoi(len_valueStr.encode().c_str()); // check whether any value tester is actually in scope TRACE("t_str_detail", tout << "checking scope of previous value testers" << std::endl;); @@ -9117,7 +9153,7 @@ void theory_str::reduce_virtual_regex_in(expr * var, expr * regex, expr_ref_vect TRACE("t_str_detail", tout << "reduce regex " << mk_pp(regex, mgr) << " with respect to variable " << mk_pp(var, mgr) << std::endl;); app * regexFuncDecl = to_app(regex); - if (is_Str2Reg(regexFuncDecl)) { + if (u.re.is_to_re(regexFuncDecl)) { // --------------------------------------------------------- // var \in Str2Reg(s1) // ==> @@ -9129,7 +9165,7 @@ void theory_str::reduce_virtual_regex_in(expr * var, expr * regex, expr_ref_vect return; } // RegexUnion - else if (is_RegexUnion(regexFuncDecl)) { + else if (u.re.is_union(regexFuncDecl)) { // --------------------------------------------------------- // var \in RegexUnion(r1, r2) // ==> @@ -9156,7 +9192,7 @@ void theory_str::reduce_virtual_regex_in(expr * var, expr * regex, expr_ref_vect return; } // RegexConcat - else if (is_RegexConcat(regexFuncDecl)) { + else if (u.re.is_concat(regexFuncDecl)) { // --------------------------------------------------------- // var \in RegexConcat(r1, r2) // ==> @@ -9177,7 +9213,7 @@ void theory_str::reduce_virtual_regex_in(expr * var, expr * regex, expr_ref_vect return; } // Unroll - else if (is_RegexStar(regexFuncDecl)) { + else if (u.re.is_star(regexFuncDecl)) { // --------------------------------------------------------- // var \in Star(r1) // ==> @@ -9190,6 +9226,7 @@ void theory_str::reduce_virtual_regex_in(expr * var, expr * regex, expr_ref_vect items.push_back(ctx.mk_eq_atom(mk_strlen(var), mk_strlen(unrollFunc))); return; } else { + get_manager().raise_exception("unrecognized regex operator"); UNREACHABLE(); } } @@ -9291,8 +9328,8 @@ static int computeLCM(int a, int b) { return temp ? (a / temp * b) : 0; } -static std::string get_unrolled_string(std::string core, int count) { - std::string res = ""; +static zstring get_unrolled_string(zstring core, int count) { + zstring res(""); for (int i = 0; i < count; i++) { res += core; } @@ -9306,11 +9343,12 @@ expr * theory_str::gen_assign_unroll_Str2Reg(expr * n, std::set & unrolls int lcm = 1; int coreValueCount = 0; expr * oneUnroll = NULL; - std::string oneCoreStr = ""; + zstring oneCoreStr(""); for (std::set::iterator itor = unrolls.begin(); itor != unrolls.end(); itor++) { expr * str2RegFunc = to_app(*itor)->get_arg(0); expr * coreVal = to_app(str2RegFunc)->get_arg(0); - std::string coreStr = m_strutil.get_string_constant_value(coreVal); + zstring coreStr; + u.str.is_string(coreVal, coreStr); if (oneUnroll == NULL) { oneUnroll = *itor; oneCoreStr = coreStr; @@ -9322,13 +9360,14 @@ expr * theory_str::gen_assign_unroll_Str2Reg(expr * n, std::set & unrolls // bool canHaveNonEmptyAssign = true; expr_ref_vector litems(mgr); - std::string lcmStr = get_unrolled_string(oneCoreStr, (lcm / oneCoreStr.length())); + zstring lcmStr = get_unrolled_string(oneCoreStr, (lcm / oneCoreStr.length())); for (std::set::iterator itor = unrolls.begin(); itor != unrolls.end(); itor++) { expr * str2RegFunc = to_app(*itor)->get_arg(0); expr * coreVal = to_app(str2RegFunc)->get_arg(0); - std::string coreStr = m_strutil.get_string_constant_value(coreVal); - int core1Len = coreStr.length(); - std::string uStr = get_unrolled_string(coreStr, (lcm / core1Len)); + zstring coreStr; + u.str.is_string(coreVal, coreStr); + unsigned int core1Len = coreStr.length(); + zstring uStr = get_unrolled_string(coreStr, (lcm / core1Len)); if (uStr != lcmStr) { canHaveNonEmptyAssign = false; } @@ -9346,7 +9385,7 @@ expr * theory_str::gen_assign_unroll_Str2Reg(expr * n, std::set & unrolls } } -expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & unrolls, std::string lcmStr) { +expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & unrolls, zstring lcmStr) { context & ctx = get_context(); ast_manager & mgr = get_manager(); @@ -9416,8 +9455,9 @@ expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & // insert [tester = "more"] to litems so that the implyL for next tester is correct litems.push_back(ctx.mk_eq_atom(tester, moreAst)); } else { - std::string testerStr = m_strutil.get_string_constant_value(testerVal); - TRACE("t_str_detail", tout << "Tester [" << mk_pp(tester, mgr) << "] = " << testerStr << std::endl;); + zstring testerStr; + u.str.is_string(testerVal, testerStr); + TRACE("t_str_detail", tout << "Tester [" << mk_pp(tester, mgr) << "] = " << testerStr << "\n";); if (testerStr == "more") { litems.push_back(ctx.mk_eq_atom(tester, moreAst)); } @@ -9438,12 +9478,12 @@ expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & return toAssert; } -expr * theory_str::gen_unroll_assign(expr * var, std::string lcmStr, expr * testerVar, int l, int h) { +expr * theory_str::gen_unroll_assign(expr * var, zstring lcmStr, expr * testerVar, int l, int h) { context & ctx = get_context(); ast_manager & mgr = get_manager(); TRACE("t_str_detail", tout << "entry: var = " << mk_pp(var, mgr) << ", lcmStr = " << lcmStr - << ", l = " << l << ", h = " << h << std::endl;); + << ", l = " << l << ", h = " << h << "\n";); if (m_params.m_AggressiveUnrollTesting) { TRACE("t_str_detail", tout << "note: aggressive unroll testing is active" << std::endl;); @@ -9453,7 +9493,7 @@ expr * theory_str::gen_unroll_assign(expr * var, std::string lcmStr, expr * test expr_ref_vector andItems(mgr); for (int i = l; i < h; i++) { - std::string iStr = int_to_string(i); + zstring iStr = int_to_string(i); expr_ref testerEqAst(ctx.mk_eq_atom(testerVar, mk_string(iStr)), mgr); TRACE("t_str_detail", tout << "testerEqAst = " << mk_pp(testerEqAst, mgr) << std::endl;); if (m_params.m_AggressiveUnrollTesting) { @@ -9463,7 +9503,7 @@ expr * theory_str::gen_unroll_assign(expr * var, std::string lcmStr, expr * test } orItems.push_back(testerEqAst); - std::string unrollStrInstance = get_unrolled_string(lcmStr, i); + zstring unrollStrInstance = get_unrolled_string(lcmStr, i); expr_ref x1(ctx.mk_eq_atom(testerEqAst, ctx.mk_eq_atom(var, mk_string(unrollStrInstance))), mgr); TRACE("t_str_detail", tout << "x1 = " << mk_pp(x1, mgr) << std::endl;); @@ -9535,14 +9575,14 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr str_indicator = expr_ref(lookup_val, m); } else { // no match; create and insert - std::string i_str = int_to_string(i); + zstring i_str = int_to_string(i); expr_ref new_val(mk_string(i_str), m); lengthTesterCache.insert(ri, new_val); m_trail.push_back(new_val); str_indicator = expr_ref(new_val, m); } } else { - std::string i_str = int_to_string(i); + zstring i_str = int_to_string(i); str_indicator = expr_ref(mk_string(i_str), m); } expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); @@ -9661,7 +9701,7 @@ expr_ref theory_str::binary_search_case_split(expr * freeVar, expr * tester, bin testerCases.push_back(caseMore); combinedCaseSplit.push_back(ctx.mk_eq_atom(caseMore, m_autil.mk_ge(lenFreeVar, m_autil.mk_numeral(N_plus_one, true) ))); - expr_ref caseEq(ctx.mk_eq_atom(tester, mk_string(N.to_string())), m); + expr_ref caseEq(ctx.mk_eq_atom(tester, mk_string(N.to_string().c_str())), m); testerCases.push_back(caseEq); combinedCaseSplit.push_back(ctx.mk_eq_atom(caseEq, ctx.mk_eq_atom(lenFreeVar, m_autil.mk_numeral(N, true)))); @@ -9712,7 +9752,7 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT expr * lastTester = binary_search_len_tester_stack[freeVar].back(); bool lastTesterHasEqcValue; expr * lastTesterValue = get_eqc_value(lastTester, lastTesterHasEqcValue); - std::string lastTesterConstant; + zstring lastTesterConstant; if (!lastTesterHasEqcValue) { TRACE("t_str_binary_search", tout << "length tester " << mk_pp(lastTester, m) << " at top of stack doesn't have an EQC value yet" << std::endl;); // check previousLenTester @@ -9724,9 +9764,9 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT UNREACHABLE(); return NULL; } } else { - lastTesterConstant = m_strutil.get_string_constant_value(lastTesterValue); + u.str.is_string(lastTesterValue, lastTesterConstant); } - TRACE("t_str_binary_search", tout << "last length tester is assigned \"" << lastTesterConstant << "\"" << std::endl;); + TRACE("t_str_binary_search", tout << "last length tester is assigned \"" << lastTesterConstant << "\"" << "\n";); if (lastTesterConstant == "more" || lastTesterConstant == "less") { // use the previous bounds info to generate a new midpoint binary_search_info lastBounds; @@ -9805,7 +9845,7 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT return axiom; } // length is fixed - expr * valueAssert = gen_free_var_options(freeVar, lastTester, lastTesterConstant, NULL, ""); + expr * valueAssert = gen_free_var_options(freeVar, lastTester, lastTesterConstant, NULL, zstring("")); return valueAssert; } } else { @@ -9906,7 +9946,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe TRACE("t_str_detail", tout << "found previous in-scope length assertions" << std::endl;); expr * effectiveLenInd = NULL; - std::string effectiveLenIndiStr = ""; + zstring effectiveLenIndiStr(""); int lenTesterCount = (int) fvar_lenTester_map[freeVar].size(); TRACE("t_str_detail", @@ -9934,9 +9974,8 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe TRACE("t_str_detail", tout << "length indicator " << mk_ismt2_pp(len_indicator_pre, m) << " = " << mk_ismt2_pp(len_indicator_value, m) << std::endl;); if (indicatorHasEqcValue) { - const char * val = 0; - m_strutil.is_string(len_indicator_value, & val); - std::string len_pIndiStr(val); + zstring len_pIndiStr; + u.str.is_string(len_indicator_value, len_pIndiStr); if (len_pIndiStr != "more") { effectiveLenInd = len_indicator_pre; effectiveLenIndiStr = len_pIndiStr; @@ -9964,7 +10003,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe effectiveLenIndiStr = lenTesterValue; } else { if (effectiveHasEqcValue) { - effectiveLenIndiStr = m_strutil.get_string_constant_value(effective_eqc_value); + u.str.is_string(effective_eqc_value, effectiveLenIndiStr); } else { NOT_IMPLEMENTED_YET(); } @@ -9988,7 +10027,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe unsigned int testNum = 0; TRACE("t_str", tout << "effectiveLenIndiStr = " << effectiveLenIndiStr - << ", i = " << i << ", lenTesterCount = " << lenTesterCount << std::endl;); + << ", i = " << i << ", lenTesterCount = " << lenTesterCount << "\n";); if (i == lenTesterCount) { fvar_len_count_map[freeVar] = fvar_len_count_map[freeVar] + 1; @@ -10007,7 +10046,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe } else { TRACE("t_str", tout << "length is fixed; generating models for free var" << std::endl;); // length is fixed - expr * valueAssert = gen_free_var_options(freeVar, effectiveLenInd, effectiveLenIndiStr, NULL, ""); + expr * valueAssert = gen_free_var_options(freeVar, effectiveLenInd, effectiveLenIndiStr, NULL, zstring("")); return valueAssert; } } // fVarLenCountMap.find(...) @@ -10020,7 +10059,7 @@ void theory_str::get_concats_in_eqc(expr * n, std::set & concats) { expr * eqcNode = n; do { - if (is_concat(to_app(eqcNode))) { + if (u.str.is_concat(to_app(eqcNode))) { concats.insert(eqcNode); } eqcNode = get_eqc_next(eqcNode); @@ -10096,7 +10135,7 @@ void theory_str::process_free_var(std::map & freeVar_map) { enode_vector::iterator it = e_freeVar->begin_parents(); for (; it != e_freeVar->end_parents(); ++it) { expr * parentAst = (*it)->get_owner(); - if (is_concat(to_app(parentAst))) { + if (u.str.is_concat(to_app(parentAst))) { standAlone = false; break; } @@ -10150,7 +10189,7 @@ void theory_str::get_eqc_allUnroll(expr * n, expr * &constStr, std::set & expr * curr = n; do { - if (is_string(to_app(curr))) { + if (u.str.is_string(to_app(curr))) { constStr = curr; } else if (is_Unroll(to_app(curr))) { if (unrollFuncSet.find(curr) == unrollFuncSet.end()) { @@ -10169,11 +10208,11 @@ void theory_str::get_eqc_simpleUnroll(expr * n, expr * &constStr, std::setget_arg(0); - if (is_Str2Reg(to_app(core))) { + if (u.re.is_to_re(to_app(core))) { if (unrollFuncSet.find(curr) == unrollFuncSet.end()) { unrollFuncSet.insert(curr); } @@ -10200,9 +10239,9 @@ void theory_str::init_model(model_generator & mg) { * or else returns NULL if no concrete value was derived. */ app * theory_str::mk_value_helper(app * n) { - if (m_strutil.is_string(n)) { + if (u.str.is_string(n)) { return n; - } else if (is_concat(n)) { + } else if (u.str.is_concat(n)) { // recursively call this function on each argument SASSERT(n->get_num_args() == 2); expr * a0 = n->get_arg(0); @@ -10212,15 +10251,10 @@ app * theory_str::mk_value_helper(app * n) { app * a1_conststr = mk_value_helper(to_app(a1)); if (a0_conststr != NULL && a1_conststr != NULL) { - const char * a0_str = 0; - m_strutil.is_string(a0_conststr, &a0_str); - - const char * a1_str = 0; - m_strutil.is_string(a1_conststr, &a1_str); - - std::string a0_s(a0_str); - std::string a1_s(a1_str); - std::string result = a0_s + a1_s; + zstring a0_s, a1_s; + u.str.is_string(a0_conststr, a0_s); + u.str.is_string(a1_conststr, a1_s); + zstring result = a0_s + a1_s; return to_app(mk_string(result)); } } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 63f5d3cfc..3be852cf4 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -30,10 +30,44 @@ Revision History: #include #include"seq_decl_plugin.h" #include"union_find.h" -#include"theory_seq_empty.h" namespace smt { + class str_value_factory : public value_factory { + seq_util u; + symbol_set m_strings; + std::string delim; + unsigned m_next; + public: + str_value_factory(ast_manager & m, family_id fid) : + value_factory(m, fid), + u(m), delim("!"), m_next(0) {} + virtual ~str_value_factory() {} + virtual expr * get_some_value(sort * s) { + return u.str.mk_string("some value"); + } + virtual bool get_some_values(sort * s, expr_ref & v1, expr_ref & v2) { + v1 = u.str.mk_string("value 1"); + v2 = u.str.mk_string("value 2"); + return true; + } + virtual expr * get_fresh_value(sort * s) { + if (u.is_string(s)) { + while (true) { + std::ostringstream strm; + strm << delim << std::hex << (m_next++) << std::dec << delim; + symbol sym(strm.str().c_str()); + if (m_strings.contains(sym)) continue; + m_strings.insert(sym); + return u.str.mk_string(sym); + } + } else { + UNREACHABLE(); return NULL; + } + } + virtual void register_value(expr * n) { /* Ignore */ } + }; + // rather than modify obj_pair_map I inherit from it and add my own helper methods class theory_str_contain_pair_bool_map_t : public obj_pair_map { public: @@ -237,8 +271,8 @@ namespace smt { //obj_map > contain_pair_idx_map; std::map > > contain_pair_idx_map; - std::map, expr*> regex_in_bool_map; - std::map > regex_in_var_reg_str_map; + std::map, expr*> regex_in_bool_map; + std::map > regex_in_var_reg_str_map; // std::map regex_nfa_cache; // Regex term --> NFA @@ -380,7 +414,7 @@ namespace smt { bool upper_bound(expr* _e, rational& hi); bool can_two_nodes_eq(expr * n1, expr * n2); - bool can_concat_eq_str(expr * concat, std::string str); + bool can_concat_eq_str(expr * concat, zstring& str); bool can_concat_eq_concat(expr * concat1, expr * concat2); bool check_concat_len_in_eqc(expr * concat); bool check_length_consistency(expr * n1, expr * n2); @@ -462,20 +496,20 @@ namespace smt { void process_free_var(std::map & freeVar_map); expr * gen_len_test_options(expr * freeVar, expr * indicator, int tries); expr * gen_free_var_options(expr * freeVar, expr * len_indicator, - std::string len_valueStr, expr * valTesterInCbEq, std::string valTesterValueStr); + zstring len_valueStr, expr * valTesterInCbEq, zstring valTesterValueStr); expr * gen_val_options(expr * freeVar, expr * len_indicator, expr * val_indicator, - std::string lenStr, int tries); + zstring lenStr, int tries); void print_value_tester_list(svector > & testerList); bool get_next_val_encode(int_vector & base, int_vector & next); - std::string gen_val_string(int len, int_vector & encoding); + zstring gen_val_string(int len, int_vector & encoding); // binary search heuristic expr * binary_search_length_test(expr * freeVar, expr * previousLenTester, std::string previousLenTesterValue); expr_ref binary_search_case_split(expr * freeVar, expr * tester, binary_search_info & bounds, literal_vector & case_split_lits); bool free_var_attempt(expr * nn1, expr * nn2); - void more_len_tests(expr * lenTester, std::string lenTesterValue); - void more_value_tests(expr * valTester, std::string valTesterValue); + void more_len_tests(expr * lenTester, zstring lenTesterValue); + void more_value_tests(expr * valTester, zstring valTesterValue); expr * get_alias_index_ast(std::map & aliasIndexMap, expr * node); expr * getMostLeftNodeInConcat(expr * node); @@ -494,10 +528,11 @@ namespace smt { void get_eqc_simpleUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet); void gen_assign_unroll_reg(std::set & unrolls); expr * gen_assign_unroll_Str2Reg(expr * n, std::set & unrolls); - expr * gen_unroll_conditional_options(expr * var, std::set & unrolls, std::string lcmStr); - expr * gen_unroll_assign(expr * var, std::string lcmStr, expr * testerVar, int l, int h); + expr * gen_unroll_conditional_options(expr * var, std::set & unrolls, zstring lcmStr); + expr * gen_unroll_assign(expr * var, zstring lcmStr, expr * testerVar, int l, int h); void reduce_virtual_regex_in(expr * var, expr * regex, expr_ref_vector & items); void check_regex_in(expr * nn1, expr * nn2); + zstring get_std_regex_str(expr * r); void dump_assignments(); void initialize_charset(); From c62b55f9b1edeeb1ffa7497a76577b99cb27aa9e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 27 Feb 2017 20:51:30 -0500 Subject: [PATCH 350/562] fix npos semantics --- src/smt/theory_str.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 6585bd7f2..21564c327 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4877,7 +4877,7 @@ void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { cstItor != constList.end(); cstItor++) { zstring pieceStr; u.str.is_string(*cstItor, pieceStr); - if (strConst.contains(pieceStr)) { + if (!strConst.contains(pieceStr)) { counterEgFound = true; if (aConcat != substrAst) { litems.push_back(ctx.mk_eq_atom(substrAst, aConcat)); @@ -5606,7 +5606,7 @@ bool theory_str::is_partial_in_grounded_concat(const std::vector & strVec for (int i = 0; i < strCnt; i++) { zstring strVal; if (u.str.is_string(strVec[i], strVal)) { - if (strVal.find(subStrVal) != std::string::npos) { + if (strVal.contains(subStrVal)) { return true; } } @@ -5794,7 +5794,7 @@ bool theory_str::can_concat_eq_str(expr * concat, zstring& str) { expr * oneArg = args[i]; zstring arg_str; if (u.str.is_string(oneArg, arg_str)) { - if (str.contains(arg_str)) { + if (!str.contains(arg_str)) { return false; } sumLen += arg_str.length(); From 11000efbfeb8470cdac95f2da497d03290c22ca8 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 27 Feb 2017 21:16:15 -0500 Subject: [PATCH 351/562] fix zstring --- src/smt/theory_str.cpp | 4 ++-- src/smt/theory_str.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 21564c327..4440d6462 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -9723,7 +9723,7 @@ expr_ref theory_str::binary_search_case_split(expr * freeVar, expr * tester, bin return final_term; } -expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenTester, std::string previousLenTesterValue) { +expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenTester, zstring previousLenTesterValue) { ast_manager & m = get_manager(); context & ctx = get_context(); @@ -9889,7 +9889,7 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT // lenTesterInCbEq != NULL, and its value will be passed by lenTesterValue // The difference is that in new_eq_eh(), lenTesterInCbEq and its value have NOT been put into a same eqc // ----------------------------------------------------------------------------------------------------- -expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTesterInCbEq, std::string lenTesterValue) { +expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTesterInCbEq, zstring lenTesterValue) { ast_manager & m = get_manager(); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 3be852cf4..233b3b7f5 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -492,7 +492,7 @@ namespace smt { std::map & concatMap, std::map & unrollMap); expr * mk_internal_lenTest_var(expr * node, int lTries); - expr * gen_len_val_options_for_free_var(expr * freeVar, expr * lenTesterInCbEq, std::string lenTesterValue); + expr * gen_len_val_options_for_free_var(expr * freeVar, expr * lenTesterInCbEq, zstring lenTesterValue); void process_free_var(std::map & freeVar_map); expr * gen_len_test_options(expr * freeVar, expr * indicator, int tries); expr * gen_free_var_options(expr * freeVar, expr * len_indicator, @@ -504,7 +504,7 @@ namespace smt { zstring gen_val_string(int len, int_vector & encoding); // binary search heuristic - expr * binary_search_length_test(expr * freeVar, expr * previousLenTester, std::string previousLenTesterValue); + expr * binary_search_length_test(expr * freeVar, expr * previousLenTester, zstring previousLenTesterValue); expr_ref binary_search_case_split(expr * freeVar, expr * tester, binary_search_info & bounds, literal_vector & case_split_lits); bool free_var_attempt(expr * nn1, expr * nn2); From 8b077ebbe799f0be78679f537167103e34904c92 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 28 Feb 2017 14:06:13 -0500 Subject: [PATCH 352/562] re-add regex NFA --- src/smt/theory_str.cpp | 161 ++++++++++++++++++++++++++++++++++++++--- src/smt/theory_str.h | 48 +++++++++++- 2 files changed, 199 insertions(+), 10 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 4440d6462..d81bb9471 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -598,7 +598,7 @@ app * theory_str::mk_unroll(expr * n, expr * bound) { ast_manager & m = get_manager(); expr * args[2] = {n, bound}; - app * unrollFunc = get_manager().mk_app(get_id(), OP_RE_UNROLL, 0, 0, 2, args); + app * unrollFunc = get_manager().mk_app(get_id(), _OP_RE_UNROLL, 0, 0, 2, args); m_trail.push_back(unrollFunc); expr_ref_vector items(m); @@ -4428,7 +4428,7 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { void theory_str::process_unroll_eq_const_str(expr * unrollFunc, expr * constStr) { ast_manager & m = get_manager(); - if (!is_Unroll(to_app(unrollFunc))) { + if (!u.re.is_unroll(to_app(unrollFunc))) { return; } if (!u.str.is_string(constStr)) { @@ -5444,7 +5444,7 @@ void theory_str::get_grounded_concats(expr* node, std::map & varAl std::map & concatAliasMap, std::map & varConstMap, std::map & concatConstMap, std::map > & varEqConcatMap, std::map, std::set > > & groundedMap) { - if (is_Unroll(to_app(node))) { + if (u.re.is_unroll(to_app(node))) { return; } // ************************************************** @@ -6129,6 +6129,149 @@ bool theory_str::check_concat_len_in_eqc(expr * concat) { return no_assertions; } +// Convert a regular expression to an e-NFA using Thompson's construction +void nfa::convert_re(expr * e, unsigned & start, unsigned & end, seq_util & u) { + start = next_id(); + end = next_id(); + if (u.re.is_to_re(e)) { + app * a = to_app(e); + expr * arg_str = a->get_arg(0); + zstring str; + if (u.str.is_string(arg_str, str)) { + TRACE("t_str_rw", tout << "build NFA for '" << str << "'" << "\n";); + + /* + * For an n-character string, we make (n-1) intermediate states, + * labelled i_(0) through i_(n-2). + * Then we construct the following transitions: + * start --str[0]--> i_(0) --str[1]--> i_(1) --...--> i_(n-2) --str[n-1]--> final + */ + unsigned last = start; + for (int i = 0; i <= ((int)str.length()) - 2; ++i) { + unsigned i_state = next_id(); + make_transition(last, str[i], i_state); + TRACE("t_str_rw", tout << "string transition " << last << "--" << str[i] << "--> " << i_state << "\n";); + last = i_state; + } + make_transition(last, str[(str.length() - 1)], end); + TRACE("t_str_rw", tout << "string transition " << last << "--" << str[(str.length() - 1)] << "--> " << end << "\n";); + TRACE("t_str_rw", tout << "string NFA: start = " << start << ", end = " << end << std::endl;); + } else { + TRACE("t_str_rw", tout << "invalid string constant in Str2Reg" << std::endl;); + m_valid = false; + return; + } + } else if (u.re.is_concat(e)){ + app * a = to_app(e); + expr * re1 = a->get_arg(0); + expr * re2 = a->get_arg(1); + unsigned start1, end1; + convert_re(re1, start1, end1, u); + unsigned start2, end2; + convert_re(re2, start2, end2, u); + // start --e--> start1 --...--> end1 --e--> start2 --...--> end2 --e--> end + make_epsilon_move(start, start1); + make_epsilon_move(end1, start2); + make_epsilon_move(end2, end); + TRACE("t_str_rw", tout << "concat NFA: start = " << start << ", end = " << end << std::endl;); + } else if (u.re.is_union(e)) { + app * a = to_app(e); + expr * re1 = a->get_arg(0); + expr * re2 = a->get_arg(1); + unsigned start1, end1; + convert_re(re1, start1, end1, u); + unsigned start2, end2; + convert_re(re2, start2, end2, u); + + // start --e--> start1 ; start --e--> start2 + // end1 --e--> end ; end2 --e--> end + make_epsilon_move(start, start1); + make_epsilon_move(start, start2); + make_epsilon_move(end1, end); + make_epsilon_move(end2, end); + TRACE("t_str_rw", tout << "union NFA: start = " << start << ", end = " << end << std::endl;); + } else if (u.re.is_star(e)) { + app * a = to_app(e); + expr * subex = a->get_arg(0); + unsigned start_subex, end_subex; + convert_re(subex, start_subex, end_subex, u); + // start --e--> start_subex, start --e--> end + // end_subex --e--> start_subex, end_subex --e--> end + make_epsilon_move(start, start_subex); + make_epsilon_move(start, end); + make_epsilon_move(end_subex, start_subex); + make_epsilon_move(end_subex, end); + TRACE("t_str_rw", tout << "star NFA: start = " << start << ", end = " << end << std::endl;); + } else { + TRACE("t_str_rw", tout << "invalid regular expression" << std::endl;); + m_valid = false; + return; + } +} + +void nfa::epsilon_closure(unsigned start, std::set & closure) { + std::deque worklist; + closure.insert(start); + worklist.push_back(start); + + while(!worklist.empty()) { + unsigned state = worklist.front(); + worklist.pop_front(); + if (epsilon_map.find(state) != epsilon_map.end()) { + for (std::set::iterator it = epsilon_map[state].begin(); + it != epsilon_map[state].end(); ++it) { + unsigned new_state = *it; + if (closure.find(new_state) == closure.end()) { + closure.insert(new_state); + worklist.push_back(new_state); + } + } + } + } +} + +bool nfa::matches(zstring input) { + /* + * Keep a set of all states the NFA can currently be in. + * Initially this is the e-closure of m_start_state + * For each character A in the input string, + * the set of next states contains + * all states in transition_map[S][A] for each S in current_states, + * and all states in epsilon_map[S] for each S in current_states. + * After consuming the entire input string, + * the match is successful iff current_states contains m_end_state. + */ + std::set current_states; + epsilon_closure(m_start_state, current_states); + for (unsigned i = 0; i < input.length(); ++i) { + char A = input.at(i); + std::set next_states; + for (std::set::iterator it = current_states.begin(); + it != current_states.end(); ++it) { + unsigned S = *it; + // check transition_map + if (transition_map[S].find(A) != transition_map[S].end()) { + next_states.insert(transition_map[S][A]); + } + } + + // take e-closure over next_states to compute the actual next_states + std::set epsilon_next_states; + for (std::set::iterator it = next_states.begin(); it != next_states.end(); ++it) { + unsigned S = *it; + std::set closure; + epsilon_closure(S, closure); + epsilon_next_states.insert(closure.begin(), closure.end()); + } + current_states = epsilon_next_states; + } + if (current_states.find(m_end_state) != current_states.end()) { + return true; + } else { + return false; + } +} + void theory_str::check_regex_in(expr * nn1, expr * nn2) { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -6159,7 +6302,7 @@ void theory_str::check_regex_in(expr * nn1, expr * nn2) { // TODO figure out regex NFA stuff if (regex_nfa_cache.find(regexTerm) == regex_nfa_cache.end()) { TRACE("t_str_detail", tout << "regex_nfa_cache: cache miss" << std::endl;); - regex_nfa_cache[regexTerm] = nfa(m_strutil, regexTerm); + regex_nfa_cache[regexTerm] = nfa(u, regexTerm); } else { TRACE("t_str_detail", tout << "regex_nfa_cache: cache hit" << std::endl;); } @@ -7385,7 +7528,7 @@ void theory_str::classify_ast_by_type(expr * node, std::map & varMap if (canskip == 0 && concatMap.find(node) == concatMap.end()) { concatMap[node] = 1; } - } else if (is_Unroll(aNode)) { + } else if (u.re.is_unroll(aNode)) { // Unroll if (unrollMap.find(node) == unrollMap.end()) { unrollMap[node] = 1; @@ -7658,7 +7801,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::mapget_owner(); - if (is_Unroll(curr)) { + if (u.re.is_unroll(curr)) { if (aRoot == NULL) { aRoot = curr; } @@ -7753,7 +7896,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map & do { if (u.str.is_string(to_app(curr))) { constStr = curr; - } else if (is_Unroll(to_app(curr))) { + } else if (u.re.is_unroll(to_app(curr))) { if (unrollFuncSet.find(curr) == unrollFuncSet.end()) { unrollFuncSet.insert(curr); } @@ -10210,7 +10353,7 @@ void theory_str::get_eqc_simpleUnroll(expr * n, expr * &constStr, std::setget_arg(0); if (u.re.is_to_re(to_app(core))) { if (unrollFuncSet.find(curr) == unrollFuncSet.end()) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 233b3b7f5..499bb23f8 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -110,6 +110,52 @@ namespace smt { } }; + + class nfa { + protected: + bool m_valid; + unsigned m_next_id; + + unsigned next_id() { + unsigned retval = m_next_id; + ++m_next_id; + return retval; + } + + unsigned m_start_state; + unsigned m_end_state; + + std::map > transition_map; + std::map > epsilon_map; + + void make_transition(unsigned start, char symbol, unsigned end) { + transition_map[start][symbol] = end; + } + + void make_epsilon_move(unsigned start, unsigned end) { + epsilon_map[start].insert(end); + } + + // Convert a regular expression to an e-NFA using Thompson's construction + void convert_re(expr * e, unsigned & start, unsigned & end, seq_util & u); + + public: + nfa(seq_util & u, expr * e) + : m_valid(true), m_next_id(0), m_start_state(0), m_end_state(0) { + convert_re(e, m_start_state, m_end_state, u); + } + + nfa() : m_valid(false), m_next_id(0), m_start_state(0), m_end_state(0) {} + + bool is_valid() const { + return m_valid; + } + + void epsilon_closure(unsigned start, std::set & closure); + + bool matches(zstring input); + }; + class theory_str : public theory { struct T_cut { @@ -274,7 +320,7 @@ namespace smt { std::map, expr*> regex_in_bool_map; std::map > regex_in_var_reg_str_map; - // std::map regex_nfa_cache; // Regex term --> NFA + std::map regex_nfa_cache; // Regex term --> NFA char * char_set; std::map charSetLookupTable; From ab71dea82d574aba85811d858a674f97820c5f0c Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 28 Feb 2017 17:47:55 -0500 Subject: [PATCH 353/562] theory_str refactoring --- src/api/z3_api.h | 148 ------------------------------- src/ast/ast_smt2_pp.cpp | 55 ------------ src/ast/ast_smt2_pp.h | 3 +- src/ast/rewriter/th_rewriter.cpp | 8 -- src/ast/seq_decl_plugin.cpp | 50 ++++++++++- src/ast/seq_decl_plugin.h | 6 +- src/model/model_evaluator.cpp | 7 -- src/smt/theory_str.cpp | 76 +++++----------- src/smt/theory_str.h | 10 ++- 9 files changed, 83 insertions(+), 280 deletions(-) diff --git a/src/api/z3_api.h b/src/api/z3_api.h index 0b8351190..87c48f3d2 100644 --- a/src/api/z3_api.h +++ b/src/api/z3_api.h @@ -3173,154 +3173,6 @@ extern "C" { /*@}*/ - /** @name Strings and regular expressions (Z3str2 implementation) */ - /*@{*/ - - /** - \brief Create a string sort for 8-bit ASCII strings. - - This function creates a sort for ASCII strings. - Each character is 8 bits. - - def_API('Z3_mk_str_sort', SORT, (_in(CONTEXT), )) - */ - Z3_sort Z3_API Z3_mk_str_sort(Z3_context c); - - /** - \brief Check if \c s is a string sort. - - def_API('Z3_is_str_sort', BOOL, (_in(CONTEXT), _in(SORT))) - */ - - Z3_bool Z3_API Z3_is_str_sort(Z3_context c, Z3_sort s); - - /** - \brief Determine if \c s is a string constant. - - def_API('Z3_is_str', BOOL, (_in(CONTEXT), _in(AST))) - */ - - Z3_bool Z3_API Z3_is_str(Z3_context c, Z3_ast s); - - /** - \brief Retrieve the string constant stored in \c s. - - \pre Z3_is_str(c, s) - - def_API('Z3_get_str', STRING, (_in(CONTEXT), _in(AST))) - */ - - Z3_string Z3_API Z3_get_str(Z3_context c, Z3_ast s); - - /** - \brief Create a string constant. - - \param c logical context. - \param str The ASCII representation of the string constant. - - def_API('Z3_mk_str', AST, (_in(CONTEXT), _in(STRING))) - */ - Z3_ast Z3_API Z3_mk_str(Z3_context c, Z3_string str); - - /** - \brief Create a string concatenation term. - def_API('Z3_mk_str_concat', AST, (_in(CONTEXT), _in(AST), _in(AST))) - */ - Z3_ast Z3_API Z3_mk_str_concat(Z3_context c, Z3_ast s1, Z3_ast s2); - - /** - \brief Create a string length term. (Integer representation) - def_API('Z3_mk_str_length', AST, (_in(CONTEXT), _in(AST))) - */ - Z3_ast Z3_API Z3_mk_str_length(Z3_context c, Z3_ast s); - - /** - \brief Create 'character at index' term. - def_API('Z3_mk_str_at', AST, (_in(CONTEXT), _in(AST), _in(AST))) - */ - Z3_ast Z3_API Z3_mk_str_at(Z3_context c, Z3_ast s, Z3_ast idx); - - /** - \brief Create 'str.prefixof' term. - def_API('Z3_mk_str_prefixof', AST, (_in(CONTEXT), _in(AST), _in(AST))) - */ - Z3_ast Z3_API Z3_mk_str_prefixof(Z3_context c, Z3_ast pre, Z3_ast full); - - /** - \brief Create 'str.suffixof' term. - def_API('Z3_mk_str_suffixof', AST, (_in(CONTEXT), _in(AST), _in(AST))) - */ - Z3_ast Z3_API Z3_mk_str_suffixof(Z3_context c, Z3_ast suf, Z3_ast full); - - /** - \brief Create 'str.contains' term. - def_API('Z3_mk_str_contains', AST, (_in(CONTEXT), _in(AST), _in(AST))) - */ - Z3_ast Z3_API Z3_mk_str_contains(Z3_context c, Z3_ast needle, Z3_ast haystack); - - /** - \brief Create 'str.indexof' term. - def_API('Z3_mk_str_indexof', AST, (_in(CONTEXT), _in(AST), _in(AST), _in(AST))) - */ - Z3_ast Z3_API Z3_mk_str_indexof(Z3_context c, Z3_ast haystack, Z3_ast needle, Z3_ast start); - - /** - \brief Create 'str.substr' term. - def_API('Z3_mk_str_substr', AST, (_in(CONTEXT), _in(AST), _in(AST), _in(AST))) - */ - Z3_ast Z3_API Z3_mk_str_substr(Z3_context c, Z3_ast s, Z3_ast start, Z3_ast count); - - /** - \brief Create 'str.replace' term. - def_API('Z3_mk_str_replace', AST, (_in(CONTEXT), _in(AST), _in(AST), _in(AST))) - */ - Z3_ast Z3_API Z3_mk_str_replace(Z3_context c, Z3_ast base, Z3_ast target, Z3_ast replacement); - - - /** - \brief Create a regular expression that matches the given string constant. - def_API('Z3_mk_str_to_regex', AST, (_in(CONTEXT), _in(STRING))) - */ - Z3_ast Z3_API Z3_mk_str_to_regex(Z3_context c, Z3_string str); - - /** - \brief Create a regular expression membership predicate. - def_API('Z3_mk_str_in_regex', AST, (_in(CONTEXT), _in(AST), _in(AST))) - */ - Z3_ast Z3_API Z3_mk_str_in_regex(Z3_context c, Z3_ast str, Z3_ast regex); - - /** - \brief Create a regex concatenation term. - def_API('Z3_mk_regex_concat', AST, (_in(CONTEXT), _in(AST), _in(AST))) - */ - Z3_ast Z3_API Z3_mk_regex_concat(Z3_context c, Z3_ast r1, Z3_ast r2); - - /** - \brief Create a regex union term. - def_API('Z3_mk_regex_union', AST, (_in(CONTEXT), _in(AST), _in(AST))) - */ - Z3_ast Z3_API Z3_mk_regex_union(Z3_context c, Z3_ast r1, Z3_ast r2); - - /** - \brief Create a regex Kleene star term. - def_API('Z3_mk_regex_star', AST, (_in(CONTEXT), _in(AST))) - */ - Z3_ast Z3_API Z3_mk_regex_star(Z3_context c, Z3_ast r); - - /** - \brief Create a regex plus term. - def_API('Z3_mk_regex_plus', AST, (_in(CONTEXT), _in(AST))) - */ - Z3_ast Z3_API Z3_mk_regex_plus(Z3_context c, Z3_ast r); - - /** - \brief Create a regex character range term. - def_API('Z3_mk_regex_range', AST, (_in(CONTEXT), _in(STRING), _in(STRING))) - */ - Z3_ast Z3_API Z3_mk_regex_range(Z3_context c, Z3_string start, Z3_string end); - - /*@}*/ - /** @name Sequences and regular expressions */ /*@{*/ diff --git a/src/ast/ast_smt2_pp.cpp b/src/ast/ast_smt2_pp.cpp index 023773f62..98c3b7962 100644 --- a/src/ast/ast_smt2_pp.cpp +++ b/src/ast/ast_smt2_pp.cpp @@ -304,58 +304,6 @@ format * smt2_pp_environment::mk_float(rational const & val) const { return mk_string(get_manager(), s.c_str()); } -format * smt2_pp_environment::pp_str_literal(app * t) { - ast_manager & m = get_manager(); - str_util & u = get_strutil(); - TRACE("parse_string", tout << "pp_str_literal\n";); - - SASSERT(u.is_string(t)); - std::string strVal = u.get_string_constant_value(t); - string_buffer<> buf; - buf << "\""; - - // we want to scan strVal and escape every non-printable character - for (unsigned int i = 0; i < strVal.length(); ++i) { - char c = strVal.at(i); - if (c == '"') { - // SMT-LIB 2.5 string escape - buf << "\"\""; - } else if (isprint(c)) { - buf << c; - } else if (c == '\a') { - buf << "\\a"; - } else if (c == '\b') { - buf << "\\b"; - } else if (c == '\e') { - buf << "\\e"; - } else if (c == '\f') { - buf << "\\f"; - } else if (c == '\n') { - buf << "\\n"; - } else if (c == '\r') { - buf << "\\r"; - } else if (c == '\t') { - buf << "\\t"; - } else if (c == '\v') { - buf << "\\v"; - } else if (c == '\\') { - buf << "\\" << "\\"; - } else { - // general hex escape - buf << "\\x"; - unsigned int cVal = ((unsigned int)c) & 0x000000FF; - const char convtable[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; - unsigned int highPart = cVal / 16; - unsigned int lowPart = cVal % 16; - SASSERT(highPart < 16); SASSERT(lowPart < 16); - buf << convtable[highPart] << convtable[lowPart]; - } - } - - buf << "\""; - return mk_string(m, buf.c_str()); -} - format * smt2_pp_environment::pp_arith_literal(app * t, bool decimal, unsigned decimal_prec) { arith_util & u = get_autil(); SASSERT(u.is_numeral(t) || u.is_irrational_algebraic_numeral(t)); @@ -666,9 +614,6 @@ class smt2_printer { else if (m_env.get_dlutil().is_numeral(c)) { f = m_env.pp_datalog_literal(c); } - else if (m_env.get_strutil().is_string(c)) { - f = m_env.pp_str_literal(c); - } else { buffer names; if (m().is_label_lit(c, names)) { diff --git a/src/ast/ast_smt2_pp.h b/src/ast/ast_smt2_pp.h index b1bdf52bd..2f79ebaec 100644 --- a/src/ast/ast_smt2_pp.h +++ b/src/ast/ast_smt2_pp.h @@ -56,9 +56,8 @@ public: virtual format_ns::format * pp_bv_literal(app * t, bool use_bv_lits, bool bv_neg); virtual format_ns::format * pp_arith_literal(app * t, bool decimal, unsigned prec); virtual format_ns::format * pp_float_literal(app * t, bool use_bv_lits, bool use_float_real_lits); - virtual format_ns::format * pp_str_literal(app * t); - virtual format_ns::format * pp_datalog_literal(app * t); virtual format_ns::format * pp_string_literal(app * t); + virtual format_ns::format * pp_datalog_literal(app * t); virtual format_ns::format * pp_sort(sort * s); virtual format_ns::format * pp_fdecl_ref(func_decl * f); format_ns::format * pp_fdecl_name(symbol const & fname, unsigned & len) const; diff --git a/src/ast/rewriter/th_rewriter.cpp b/src/ast/rewriter/th_rewriter.cpp index 86772cdb4..0c57ea609 100644 --- a/src/ast/rewriter/th_rewriter.cpp +++ b/src/ast/rewriter/th_rewriter.cpp @@ -27,7 +27,6 @@ Notes: #include"dl_rewriter.h" #include"pb_rewriter.h" #include"seq_rewriter.h" -#include"str_rewriter.h" #include"rewriter_def.h" #include"expr_substitution.h" #include"ast_smt2_pp.h" @@ -46,7 +45,6 @@ struct th_rewriter_cfg : public default_rewriter_cfg { dl_rewriter m_dl_rw; pb_rewriter m_pb_rw; seq_rewriter m_seq_rw; - str_rewriter m_str_rw; arith_util m_a_util; bv_util m_bv_util; unsigned long long m_max_memory; // in bytes @@ -81,7 +79,6 @@ struct th_rewriter_cfg : public default_rewriter_cfg { m_ar_rw.updt_params(p); m_f_rw.updt_params(p); m_seq_rw.updt_params(p); - m_str_rw.updt_params(p); updt_local_params(p); } @@ -182,8 +179,6 @@ struct th_rewriter_cfg : public default_rewriter_cfg { st = m_ar_rw.mk_eq_core(args[0], args[1], result); else if (s_fid == m_seq_rw.get_fid()) st = m_seq_rw.mk_eq_core(args[0], args[1], result); - else if (s_fid == m_str_rw.get_fid()) - st = m_str_rw.mk_eq_core(args[0], args[1], result); if (st != BR_FAILED) return st; @@ -220,8 +215,6 @@ struct th_rewriter_cfg : public default_rewriter_cfg { return m_pb_rw.mk_app_core(f, num, args, result); if (fid == m_seq_rw.get_fid()) return m_seq_rw.mk_app_core(f, num, args, result); - if (fid == m_str_rw.get_fid()) - return m_str_rw.mk_app_core(f, num, args, result); return BR_FAILED; } @@ -680,7 +673,6 @@ struct th_rewriter_cfg : public default_rewriter_cfg { m_dl_rw(m), m_pb_rw(m), m_seq_rw(m), - m_str_rw(m), m_a_util(m), m_bv_util(m), m_used_dependencies(m), diff --git a/src/ast/seq_decl_plugin.cpp b/src/ast/seq_decl_plugin.cpp index 5022c57a6..059fe9674 100644 --- a/src/ast/seq_decl_plugin.cpp +++ b/src/ast/seq_decl_plugin.cpp @@ -284,8 +284,54 @@ zstring zstring::operator+(zstring const& other) const { return result; } -std::ostream& zstring::operator<<(std::ostream& out) const { - return out << encode(); +bool zstring::operator==(const zstring& other) const { + // two strings are equal iff they have the same length and characters + if (length() != other.length()) { + return false; + } + for (unsigned i = 0; i < length(); ++i) { + unsigned Xi = m_buffer[i]; + unsigned Yi = other[i]; + if (Xi != Yi) { + return false; + } + } + + return true; +} + +bool zstring::operator!=(const zstring& other) const { + return !(*this == other); +} + +std::ostream& operator<<(std::ostream &os, const zstring &str) { + return os << str.encode(); +} + +bool operator<(const zstring& lhs, const zstring& rhs) { + // This has the same semantics as strcmp() + unsigned len = lhs.length(); + if (rhs.length() < len) { + len = rhs.length(); + } + for (unsigned i = 0; i < len; ++i) { + unsigned Li = lhs[i]; + unsigned Ri = rhs[i]; + if (Li < Ri) { + return true; + } else if (Li > Ri) { + return false; + } else { + continue; + } + } + // at this point, all compared characters are equal, + // so decide based on the relative lengths + if (lhs.length() < rhs.length()) { + return true; + } else { + return false; + } } diff --git a/src/ast/seq_decl_plugin.h b/src/ast/seq_decl_plugin.h index b07e4d307..a7e534bbb 100644 --- a/src/ast/seq_decl_plugin.h +++ b/src/ast/seq_decl_plugin.h @@ -114,7 +114,11 @@ public: int indexof(zstring const& other, int offset) const; zstring extract(int lo, int hi) const; zstring operator+(zstring const& other) const; - std::ostream& operator<<(std::ostream& out) const; + bool operator==(const zstring& other) const; + bool operator!=(const zstring& other) const; + + friend std::ostream& operator<<(std::ostream &os, const zstring &str); + friend bool operator<(const zstring& lhs, const zstring& rhs); }; class seq_decl_plugin : public decl_plugin { diff --git a/src/model/model_evaluator.cpp b/src/model/model_evaluator.cpp index 06bbceb43..af2253801 100644 --- a/src/model/model_evaluator.cpp +++ b/src/model/model_evaluator.cpp @@ -28,7 +28,6 @@ Revision History: #include"datatype_rewriter.h" #include"array_rewriter.h" #include"fpa_rewriter.h" -#include"str_rewriter.h" #include"rewriter_def.h" #include"cooperate.h" #include"ast_pp.h" @@ -46,7 +45,6 @@ struct evaluator_cfg : public default_rewriter_cfg { pb_rewriter m_pb_rw; fpa_rewriter m_f_rw; seq_rewriter m_seq_rw; - str_rewriter m_str_rw; array_util m_ar; unsigned long long m_max_memory; unsigned m_max_steps; @@ -66,7 +64,6 @@ struct evaluator_cfg : public default_rewriter_cfg { m_pb_rw(m), m_f_rw(m), m_seq_rw(m), - m_str_rw(m), m_ar(m) { bool flat = true; m_b_rw.set_flat(flat); @@ -158,8 +155,6 @@ struct evaluator_cfg : public default_rewriter_cfg { st = m_f_rw.mk_eq_core(args[0], args[1], result); else if (s_fid == m_seq_rw.get_fid()) st = m_seq_rw.mk_eq_core(args[0], args[1], result); - else if (s_fid == m_str_rw.get_fid()) - st = m_str_rw.mk_eq_core(args[0], args[1], result); else if (s_fid == m_ar_rw.get_fid()) st = mk_array_eq(args[0], args[1], result); if (st != BR_FAILED) @@ -182,8 +177,6 @@ struct evaluator_cfg : public default_rewriter_cfg { st = m_f_rw.mk_app_core(f, num, args, result); else if (fid == m_seq_rw.get_fid()) st = m_seq_rw.mk_app_core(f, num, args, result); - else if (fid == m_str_rw.get_fid()) - st = m_str_rw.mk_app_core(f, num, args, result); else if (fid == m().get_label_family_id() && num == 1) { result = args[0]; st = BR_DONE; diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index d81bb9471..79b6efb8b 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -887,7 +887,7 @@ void theory_str::try_eval_concat(enode * cat) { app * evalArg = worklist.top(); worklist.pop(); zstring nextStr; if (u.str.is_string(evalArg, nextStr)) { - flattenedString += nextStr; + flattenedString = flattenedString + nextStr; } else if (u.str.is_concat(evalArg)) { app * arg0 = to_app(evalArg->get_arg(0)); app * arg1 = to_app(evalArg->get_arg(1)); @@ -1643,9 +1643,10 @@ static zstring str2RegexStr(zstring str) { // 12 special chars if (nc == '\\' || nc == '^' || nc == '$' || nc == '.' || nc == '|' || nc == '?' || nc == '*' || nc == '+' || nc == '(' || nc == ')' || nc == '[' || nc == '{') { - res += zstring("\\"); + res = res + zstring("\\"); } - res += zstring(1, (unsigned)str[i]); + char tmp[1] = {(char)str[i]}; + res = res + zstring(tmp); } return res; } @@ -2783,11 +2784,9 @@ bool theory_str::will_result_in_overlap(expr * lhs, expr * rhs) { // case 2: concat(x, y) = concat(m, "str") //************************************************************* if (is_concat_eq_type2(new_nn1, new_nn2)) { - expr * x = NULL; - expr * y = NULL; - expr * strAst = NULL; - expr * m = NULL; + expr * y = NULL; + expr * m = NULL; expr * v1_arg0 = to_app(new_nn1)->get_arg(0); expr * v1_arg1 = to_app(new_nn1)->get_arg(1); expr * v2_arg0 = to_app(new_nn2)->get_arg(0); @@ -2795,13 +2794,9 @@ bool theory_str::will_result_in_overlap(expr * lhs, expr * rhs) { if (u.str.is_string(v1_arg1) && !u.str.is_string(v2_arg1)) { m = v1_arg0; - strAst = v1_arg1; - x = v2_arg0; y = v2_arg1; } else { m = v2_arg0; - strAst = v2_arg1; - x = v1_arg0; y = v1_arg1; } @@ -2823,20 +2818,14 @@ bool theory_str::will_result_in_overlap(expr * lhs, expr * rhs) { expr * v2_arg1 = to_app(new_nn2)->get_arg(1); expr * x = NULL; - expr * y = NULL; - expr * strAst = NULL; expr * n = NULL; if (u.str.is_string(v1_arg0) && !u.str.is_string(v2_arg0)) { - strAst = v1_arg0; n = v1_arg1; x = v2_arg0; - y = v2_arg1; } else { - strAst = v2_arg0; n = v2_arg1; x = v1_arg0; - y = v1_arg1; } if (has_self_cut(x, n)) { TRACE("t_str_detail", tout << "Possible overlap found" << std::endl; print_cut_var(x, tout); print_cut_var(n, tout);); @@ -2870,21 +2859,15 @@ bool theory_str::will_result_in_overlap(expr * lhs, expr * rhs) { expr * v2_arg0 = to_app(new_nn2)->get_arg(0); expr * v2_arg1 = to_app(new_nn2)->get_arg(1); - expr * str1Ast = NULL; expr * y = NULL; expr * m = NULL; - expr * str2Ast = NULL; if (u.str.is_string(v1_arg0)) { - str1Ast = v1_arg0; y = v1_arg1; m = v2_arg0; - str2Ast = v2_arg1; } else { - str1Ast = v2_arg0; y = v2_arg1; m = v1_arg0; - str2Ast = v1_arg1; } if (has_self_cut(m, y)) { TRACE("t_str_detail", tout << "Possible overlap found" << std::endl; print_cut_var(m, tout); print_cut_var(y, tout);); @@ -3160,9 +3143,6 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { // This vector will eventually contain one term for each possible arrangement we explore. expr_ref_vector arrangement_disjunction(mgr); - int option = 0; - int pos = 1; - // break option 1: m cuts y // len(x) < len(m) || len(y) > len(n) if (!avoidLoopCut || !has_self_cut(m, y)) { @@ -3508,16 +3488,13 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { // | m | str | rational lenDelta; expr_ref_vector l_items(mgr); - int l_count = 0; l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); if (x_len_exists && m_len_exists) { l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); - l_count = 3; lenDelta = x_len - m_len; } else { l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); - l_count = 2; lenDelta = str_len - y_len; } TRACE("t_str", @@ -3562,12 +3539,8 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { } } else { // Split type -1: no idea about the length... - int optionTotal = 2 + strValue.length(); expr_ref_vector arrangement_disjunction(mgr); - int option = 0; - int pos = 1; - expr_ref temp1_strAst(mk_concat(temp1, strAst), mgr); // m cuts y @@ -3904,7 +3877,6 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { // Split type -1. We know nothing about the length... expr_ref_vector arrangement_disjunction(mgr); - unsigned option = 0; int pos = 1; for (unsigned int i = 0; i <= strValue.length(); i++) { @@ -4336,7 +4308,6 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { } expr_ref_vector arrangement_disjunction(mgr); - int option = 0; int pos = 1; if (!avoidLoopCut || !has_self_cut(m, y)) { @@ -5602,7 +5573,7 @@ bool theory_str::is_partial_in_grounded_concat(const std::vector & strVec if (subStrCnt == 1) { zstring subStrVal; - if (u.str.is_string(subStrVec[0]), subStrVal) { + if (u.str.is_string(subStrVec[0], subStrVal)) { for (int i = 0; i < strCnt; i++) { zstring strVal; if (u.str.is_string(strVec[i], strVal)) { @@ -5630,7 +5601,7 @@ bool theory_str::is_partial_in_grounded_concat(const std::vector & strVec zstring strHeadVal; if (u.str.is_string(strVec[i], strHeadVal)) { if (strHeadVal.length() >= subStrHeadVal.length()) { - std::string suffix = strHeadVal.extract(strHeadVal.length() - subStrHeadVal.length(), subStrHeadVal.length()); + zstring suffix = strHeadVal.extract(strHeadVal.length() - subStrHeadVal.length(), subStrHeadVal.length()); if (suffix != subStrHeadVal) { firstNodesOK = false; } @@ -5758,7 +5729,7 @@ void theory_str::compute_contains(std::map & varAliasMap, } bool theory_str::can_concat_eq_str(expr * concat, zstring& str) { - int strLen = str.length(); + unsigned int strLen = str.length(); if (u.str.is_concat(to_app(concat))) { ptr_vector args; get_nodes_in_concat(concat, args); @@ -6244,7 +6215,7 @@ bool nfa::matches(zstring input) { std::set current_states; epsilon_closure(m_start_state, current_states); for (unsigned i = 0; i < input.length(); ++i) { - char A = input.at(i); + char A = (char)input[i]; std::set next_states; for (std::set::iterator it = current_states.begin(); it != current_states.end(); ++it) { @@ -6288,12 +6259,12 @@ void theory_str::check_regex_in(expr * nn1, expr * nn2) { expr_ref_vector::iterator itor = eqNodeSet.begin(); for (; itor != eqNodeSet.end(); itor++) { if (regex_in_var_reg_str_map.find(*itor) != regex_in_var_reg_str_map.end()) { - std::set::iterator strItor = regex_in_var_reg_str_map[*itor].begin(); + std::set::iterator strItor = regex_in_var_reg_str_map[*itor].begin(); for (; strItor != regex_in_var_reg_str_map[*itor].end(); strItor++) { - std::string regStr = *strItor; + zstring regStr = *strItor; zstring constStrValue; u.str.is_string(constStr, constStrValue); - std::pair key1 = std::make_pair(*itor, regStr); + std::pair key1 = std::make_pair(*itor, regStr); if (regex_in_bool_map.find(key1) != regex_in_bool_map.end()) { expr * boolVar = regex_in_bool_map[key1]; // actually the RegexIn term app * a_regexIn = to_app(boolVar); @@ -6403,7 +6374,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { // Inconsistency TRACE("t_str", tout << "inconsistency detected: \"" << arg1_str << "\" + \"" << arg2_str << - "\" != \"" << const_str << "\"\n"); + "\" != \"" << const_str << "\"" << "\n";); expr_ref equality(ctx.mk_eq_atom(concat, str), m); expr_ref diseq(m.mk_not(equality), m); assert_axiom(diseq); @@ -6421,7 +6392,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { TRACE("t_str", tout << "inconsistency detected: \"" << arg2_str << "\" is longer than \"" << const_str << "\"," - << " so cannot be concatenated with anything to form it\n"); + << " so cannot be concatenated with anything to form it" << "\n";); expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); expr_ref diseq(m.mk_not(equality), m); assert_axiom(diseq); @@ -6435,7 +6406,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { TRACE("t_str", tout << "inconsistency detected: " << "suffix of concatenation result expected \"" << secondPart << "\", " << "actually \"" << arg2_str << "\"" - << "\n"); + << "\n";); expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); expr_ref diseq(m.mk_not(equality), m); assert_axiom(diseq); @@ -6620,7 +6591,6 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } int concatStrLen = const_str.length(); - int xor_pos = 0; int and_count = 1; expr_ref_vector arrangement_disjunction(m); @@ -6701,8 +6671,8 @@ expr_ref theory_str::set_up_finite_model_test(expr * lhs, expr * rhs) { } // make things easy for the core wrt. testvar - expr_ref t1(ctx.mk_eq_atom(testvar, u.str.mk_string("")), m); - expr_ref t_yes(ctx.mk_eq_atom(testvar, u.str.mk_string("yes")), m); + expr_ref t1(ctx.mk_eq_atom(testvar, mk_string("")), m); + expr_ref t_yes(ctx.mk_eq_atom(testvar, mk_string("yes")), m); expr_ref testvaraxiom(m.mk_or(t1, t_yes), m); assert_axiom(testvaraxiom); @@ -6812,8 +6782,8 @@ void theory_str::finite_model_test(expr * testvar, expr * str) { expr_ref_vector andList(m); for (rational l = v_lower_bound; l <= v_upper_bound; l += rational::one()) { - std::string lStr = l.to_string(); - expr_ref str_indicator(u.str.mk_string(lStr), m); + zstring lStr = zstring(l.to_string().c_str()); + expr_ref str_indicator(mk_string(lStr), m); expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); orList.push_back(or_expr); expr_ref and_expr(ctx.mk_eq_atom(or_expr, ctx.mk_eq_atom(vLengthExpr, m_autil.mk_numeral(l, true))), m); @@ -9006,12 +8976,12 @@ zstring theory_str::gen_val_string(int len, int_vector & encoding) { SASSERT(charSetSize > 0); SASSERT(char_set != NULL); - zstring re(len, (int) char_set[0]); + std::string re(len, char_set[0]); for (int i = 0; i < (int) encoding.size() - 1; i++) { int idx = encoding[i]; re[len - 1 - i] = char_set[idx]; } - return re; + return zstring(re.c_str()); } /* @@ -9474,7 +9444,7 @@ static int computeLCM(int a, int b) { static zstring get_unrolled_string(zstring core, int count) { zstring res(""); for (int i = 0; i < count; i++) { - res += core; + res = res + core; } return res; } diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 499bb23f8..fc238acbd 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -33,6 +33,8 @@ Revision History: namespace smt { + typedef hashtable symbol_set; + class str_value_factory : public value_factory { seq_util u; symbol_set m_strings; @@ -44,11 +46,11 @@ namespace smt { u(m), delim("!"), m_next(0) {} virtual ~str_value_factory() {} virtual expr * get_some_value(sort * s) { - return u.str.mk_string("some value"); + return u.str.mk_string(symbol("some value")); } virtual bool get_some_values(sort * s, expr_ref & v1, expr_ref & v2) { - v1 = u.str.mk_string("value 1"); - v2 = u.str.mk_string("value 2"); + v1 = u.str.mk_string(symbol("value 1")); + v2 = u.str.mk_string(symbol("value 2")); return true; } virtual expr * get_fresh_value(sort * s) { @@ -256,7 +258,7 @@ namespace smt { expr_ref_vector m_trail; // trail for generated terms - seq_factory * m_factory; + str_value_factory * m_factory; // terms we couldn't go through set_up_axioms() with because they weren't internalized expr_ref_vector m_delayed_axiom_setup_terms; From d00723e7ea4252c0a2d7e6cf66668f54f41d830d Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 1 Mar 2017 18:23:48 -0500 Subject: [PATCH 354/562] add String name for string sort, SMTLIB2.5 compat --- src/ast/seq_decl_plugin.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ast/seq_decl_plugin.cpp b/src/ast/seq_decl_plugin.cpp index 059fe9674..90fd3fb22 100644 --- a/src/ast/seq_decl_plugin.cpp +++ b/src/ast/seq_decl_plugin.cpp @@ -830,6 +830,8 @@ void seq_decl_plugin::get_sort_names(svector & sort_names, symbol init(); sort_names.push_back(builtin_name("Seq", SEQ_SORT)); sort_names.push_back(builtin_name("RegEx", RE_SORT)); + // SMT-LIB 2.5 compatibility + sort_names.push_back(builtin_name("String", _STRING_SORT)); sort_names.push_back(builtin_name("StringSequence", _STRING_SORT)); } From 9f79015ee6b2e61fcec3d363b96610948242739d Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 1 Mar 2017 22:18:18 -0500 Subject: [PATCH 355/562] patches to theory_str for theory_seq compat --- src/ast/seq_decl_plugin.cpp | 2 +- src/smt/smt_setup.cpp | 4 +++- src/smt/theory_str.cpp | 4 ++-- src/smt/theory_str.h | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/ast/seq_decl_plugin.cpp b/src/ast/seq_decl_plugin.cpp index 90fd3fb22..2483d2370 100644 --- a/src/ast/seq_decl_plugin.cpp +++ b/src/ast/seq_decl_plugin.cpp @@ -336,7 +336,7 @@ bool operator<(const zstring& lhs, const zstring& rhs) { seq_decl_plugin::seq_decl_plugin(): m_init(false), - m_stringc_sym("StringSequence"), + m_stringc_sym("String"), m_charc_sym("Char"), m_string(0), m_char(0), diff --git a/src/smt/smt_setup.cpp b/src/smt/smt_setup.cpp index 3a4f7f981..78a295e27 100644 --- a/src/smt/smt_setup.cpp +++ b/src/smt/smt_setup.cpp @@ -825,7 +825,9 @@ namespace smt { } void setup::setup_seq() { - m_context.register_plugin(alloc(theory_seq, m_manager)); + // TODO proper negotiation of theory_str vs. theory_seq + //m_context.register_plugin(alloc(theory_seq, m_manager)); + setup_str(); } void setup::setup_card() { diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 79b6efb8b..1f276125c 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -31,7 +31,7 @@ Revision History: namespace smt { theory_str::theory_str(ast_manager & m, theory_str_params const & params): - theory(m.mk_family_id("str")), + theory(m.mk_family_id("seq")), m_params(params), /* Options */ opt_EagerStringConstantLengthAssertions(true), @@ -266,7 +266,7 @@ void theory_str::refresh_theory_var(expr * e) { theory_var theory_str::mk_var(enode* n) { TRACE("t_str_detail", tout << "mk_var for " << mk_pp(n->get_owner(), get_manager()) << std::endl;); ast_manager & m = get_manager(); - if (!(is_sort_of(m.get_sort(n->get_owner()), u.get_family_id(), _STRING_SORT))) { + if (!(m.get_sort(n->get_owner()) == u.str.mk_string_sort())) { return null_theory_var; } if (is_attached_to_var(n)) { diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index fc238acbd..5a67f72f1 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -603,7 +603,7 @@ namespace smt { theory_str(ast_manager & m, theory_str_params const & params); virtual ~theory_str(); - virtual char const * get_name() const { return "strings"; } + virtual char const * get_name() const { return "seq"; } virtual void display(std::ostream & out) const; bool overlapping_variables_detected() const { return loopDetected; } From 82b1a61b250810c5bd904856f4651bbafcabb730 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 4 Mar 2017 16:30:36 -0500 Subject: [PATCH 356/562] fix string operator names --- src/ast/seq_decl_plugin.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ast/seq_decl_plugin.cpp b/src/ast/seq_decl_plugin.cpp index 2483d2370..465c6f675 100644 --- a/src/ast/seq_decl_plugin.cpp +++ b/src/ast/seq_decl_plugin.cpp @@ -558,8 +558,8 @@ void seq_decl_plugin::init() { m_sigs[_OP_STRING_CHARAT] = alloc(psig, m, "str.at", 0, 2, strTint2T, strT); m_sigs[_OP_STRING_PREFIX] = alloc(psig, m, "str.prefixof", 0, 2, str2T, boolT); m_sigs[_OP_STRING_SUFFIX] = alloc(psig, m, "str.suffixof", 0, 2, str2T, boolT); - m_sigs[_OP_STRING_IN_REGEXP] = alloc(psig, m, "seqstr.in.re", 0, 2, strTreT, boolT); - m_sigs[_OP_STRING_TO_REGEXP] = alloc(psig, m, "seqstr.to.re", 0, 1, &strT, reT); + m_sigs[_OP_STRING_IN_REGEXP] = alloc(psig, m, "str.in.re", 0, 2, strTreT, boolT); + m_sigs[_OP_STRING_TO_REGEXP] = alloc(psig, m, "str.to.re", 0, 1, &strT, reT); m_sigs[_OP_REGEXP_EMPTY] = alloc(psig, m, "re.nostr", 0, 0, 0, reT); m_sigs[_OP_REGEXP_FULL] = alloc(psig, m, "re.allchar", 0, 0, 0, reT); m_sigs[_OP_STRING_SUBSTR] = alloc(psig, m, "str.substr", 0, 3, strTint2T, strT); From 577cb19745f8be7f29988d4dd9fec44e24fd210a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 6 Mar 2017 13:58:03 -0500 Subject: [PATCH 357/562] experimental rewrite of bitvector unit sequences to string constants --- src/ast/rewriter/seq_rewriter.cpp | 35 ++++++++++++++++++++++++++++++- src/ast/rewriter/seq_rewriter.h | 1 + 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/ast/rewriter/seq_rewriter.cpp b/src/ast/rewriter/seq_rewriter.cpp index 26c3e23e4..0c77dfcf2 100644 --- a/src/ast/rewriter/seq_rewriter.cpp +++ b/src/ast/rewriter/seq_rewriter.cpp @@ -322,7 +322,13 @@ br_status seq_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con switch(f->get_decl_kind()) { case OP_SEQ_UNIT: - return BR_FAILED; + // TODO configuration param + if (true) { + SASSERT(num_args == 1); + return mk_seq_unit(args[0], result); + } else { + return BR_FAILED; + } case OP_SEQ_EMPTY: return BR_FAILED; case OP_RE_PLUS: @@ -427,6 +433,33 @@ br_status seq_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con return BR_FAILED; } +/* + * (seq.unit (_ BitVector 8)) ==> String constant + */ +br_status seq_rewriter::mk_seq_unit(expr* e, expr_ref& result) { + sort * s = m().get_sort(e); + bv_util bvu(m()); + + if (is_sort_of(s, bvu.get_family_id(), BV_SORT)) { + // specifically we want (_ BitVector 8) + rational n_val; + unsigned int n_size; + if (bvu.is_numeral(e, n_val, n_size)) { + if (n_size == 8) { + // convert to string constant + char ch = (char)n_val.get_int32(); + TRACE("seq", tout << "rewrite seq.unit of 8-bit value " << n_val.to_string() << " to string constant \"" << ch << "\"" << std::endl;); + char s_tmp[2] = {ch, '\0'}; + symbol s(s_tmp); + result = m_util.str.mk_string(s); + return BR_DONE; + } + } + } + + return BR_FAILED; +} + /* string + string = string a + (b + c) = (a + b) + c diff --git a/src/ast/rewriter/seq_rewriter.h b/src/ast/rewriter/seq_rewriter.h index 2b434f475..eed08e376 100644 --- a/src/ast/rewriter/seq_rewriter.h +++ b/src/ast/rewriter/seq_rewriter.h @@ -98,6 +98,7 @@ class seq_rewriter { re2automaton m_re2aut; expr_ref_vector m_es, m_lhs, m_rhs; + br_status mk_seq_unit(expr* e, expr_ref& result); br_status mk_seq_concat(expr* a, expr* b, expr_ref& result); br_status mk_seq_length(expr* a, expr_ref& result); br_status mk_seq_extract(expr* a, expr* b, expr* c, expr_ref& result); From 4d5c1dcfb696a687c0934259b810305669cb4971 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 6 Mar 2017 17:04:07 -0500 Subject: [PATCH 358/562] fix model gen for regex terms in theory_str --- src/smt/theory_str.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 5a67f72f1..54fdc6538 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -63,9 +63,14 @@ namespace smt { m_strings.insert(sym); return u.str.mk_string(sym); } - } else { - UNREACHABLE(); return NULL; } + sort* seq = 0; + if (u.is_re(s, seq)) { + expr* v0 = get_fresh_value(seq); + return u.re.mk_to_re(v0); + } + TRACE("t_str", tout << "unexpected sort in get_fresh_value(): " << mk_pp(s, m_manager) << std::endl;); + UNREACHABLE(); return NULL; } virtual void register_value(expr * n) { /* Ignore */ } }; From c198bc58638bda1da1621a4a1f373ae4c0c47714 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 10 Mar 2017 13:13:45 -0500 Subject: [PATCH 359/562] fix re.range rewrite for theory_str --- src/ast/rewriter/seq_rewriter.cpp | 42 ++++++++++++++++++++++++++++--- src/ast/rewriter/seq_rewriter.h | 1 + 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/ast/rewriter/seq_rewriter.cpp b/src/ast/rewriter/seq_rewriter.cpp index 0c77dfcf2..4efb4b9d7 100644 --- a/src/ast/rewriter/seq_rewriter.cpp +++ b/src/ast/rewriter/seq_rewriter.cpp @@ -350,7 +350,8 @@ br_status seq_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con SASSERT(num_args == 2); return mk_re_union(args[0], args[1], result); case OP_RE_RANGE: - return BR_FAILED; + SASSERT(num_args == 2); + return mk_re_range(args[0], args[1], result); case OP_RE_INTERSECT: SASSERT(num_args == 2); return mk_re_inter(args[0], args[1], result); @@ -1313,6 +1314,39 @@ br_status seq_rewriter::mk_re_star(expr* a, expr_ref& result) { return BR_FAILED; } +/* + * (re.range c_1 c_n) = (re.union (str.to.re c1) (str.to.re c2) ... (str.to.re cn)) + */ +br_status seq_rewriter::mk_re_range(expr* lo, expr* hi, expr_ref& result) { + TRACE("seq", tout << "rewrite re.range [" << mk_pp(lo, m()) << " " << mk_pp(hi, m()) << "]\n";); + zstring str_lo, str_hi; + if (m_util.str.is_string(lo, str_lo) && m_util.str.is_string(hi, str_hi)) { + if (str_lo.length() == 1 && str_hi.length() == 1) { + unsigned int c1 = str_lo[0]; + unsigned int c2 = str_hi[0]; + if (c1 > c2) { + // exchange c1 and c2 + unsigned int tmp = c1; + c2 = c1; + c1 = tmp; + } + zstring s(c1); + expr_ref acc(m_util.re.mk_to_re(m_util.str.mk_string(s)), m()); + for (unsigned int ch = c1 + 1; ch <= c2; ++ch) { + zstring s_ch(ch); + expr_ref acc2(m_util.re.mk_to_re(m_util.str.mk_string(s_ch)), m()); + acc = m_util.re.mk_union(acc, acc2); + } + result = acc; + return BR_REWRITE2; + } else { + m().raise_exception("string constants in re.range must have length 1"); + } + } + + return BR_FAILED; +} + /* emp+ = emp all+ = all @@ -1342,9 +1376,9 @@ br_status seq_rewriter::mk_re_plus(expr* a, expr_ref& result) { return BR_DONE; } - return BR_FAILED; -// result = m_util.re.mk_concat(a, m_util.re.mk_star(a)); -// return BR_REWRITE2; + //return BR_FAILED; + result = m_util.re.mk_concat(a, m_util.re.mk_star(a)); + return BR_REWRITE2; } br_status seq_rewriter::mk_re_opt(expr* a, expr_ref& result) { diff --git a/src/ast/rewriter/seq_rewriter.h b/src/ast/rewriter/seq_rewriter.h index eed08e376..210b2d72c 100644 --- a/src/ast/rewriter/seq_rewriter.h +++ b/src/ast/rewriter/seq_rewriter.h @@ -120,6 +120,7 @@ class seq_rewriter { br_status mk_re_plus(expr* a, expr_ref& result); br_status mk_re_opt(expr* a, expr_ref& result); br_status mk_re_loop(unsigned num_args, expr* const* args, expr_ref& result); + br_status mk_re_range(expr* lo, expr* hi, expr_ref& result); bool set_empty(unsigned sz, expr* const* es, bool all, expr_ref_vector& lhs, expr_ref_vector& rhs); bool is_subsequence(unsigned n, expr* const* l, unsigned m, expr* const* r, From b459d17624c13fdb40d0b802ebc7a4981570ce87 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 10 Mar 2017 13:53:55 -0500 Subject: [PATCH 360/562] fix int-to-str terms in theory_str not being picked up --- src/smt/theory_str.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 1f276125c..ccfdaf8aa 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -7169,6 +7169,10 @@ void theory_str::set_up_axioms(expr * ex) { } } else if (u.str.is_at(ap) || u.str.is_extract(ap) || u.str.is_replace(ap)) { m_library_aware_axiom_todo.push_back(n); + } else if (u.str.is_itos(ap)) { + TRACE("t_str_detail", tout << "found string-integer conversion term: " << mk_pp(ex, get_manager()) << std::endl;); + string_int_conversion_terms.push_back(ap); + m_library_aware_axiom_todo.push_back(n); } else if (ap->get_num_args() == 0 && !u.str.is_string(ap)) { // if ex is a variable, add it to our list of variables TRACE("t_str_detail", tout << "tracking variable " << mk_ismt2_pp(ap, get_manager()) << std::endl;); @@ -7213,7 +7217,8 @@ void theory_str::set_up_axioms(expr * ex) { // TODO indexof2/lastindexof if (u.str.is_index(ap) /* || is_Indexof2(ap) || is_LastIndexof(ap) */) { m_library_aware_axiom_todo.push_back(n); - } else if (u.str.is_stoi(ap) || u.str.is_itos(ap)) { + } else if (u.str.is_stoi(ap)) { + TRACE("t_str_detail", tout << "found string-integer conversion term: " << mk_pp(ex, get_manager()) << std::endl;); string_int_conversion_terms.push_back(ap); m_library_aware_axiom_todo.push_back(n); } From 338193548b62cb8168dff0de89e2abc60b876c2f Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Fri, 10 Mar 2017 22:52:55 +0100 Subject: [PATCH 361/562] fixing build break, adding fixedpoint object to C++ API Signed-off-by: Nikolaj Bjorner --- src/ast/rewriter/rewriter.h | 2 ++ src/ast/rewriter/rewriter_def.h | 44 ++++++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/ast/rewriter/rewriter.h b/src/ast/rewriter/rewriter.h index fc596cabd..2b4f4b14e 100644 --- a/src/ast/rewriter/rewriter.h +++ b/src/ast/rewriter/rewriter.h @@ -315,6 +315,8 @@ protected: template void process_app(app * t, frame & fr); + bool constant_fold(app* t, frame& fr); + template void process_quantifier(quantifier * q, frame & fr); diff --git a/src/ast/rewriter/rewriter_def.h b/src/ast/rewriter/rewriter_def.h index dddb02dfd..aecc1c93a 100644 --- a/src/ast/rewriter/rewriter_def.h +++ b/src/ast/rewriter/rewriter_def.h @@ -174,6 +174,38 @@ bool rewriter_tpl::visit(expr * t, unsigned max_depth) { } } +template +bool rewriter_tpl::constant_fold(app * t, frame & fr) { + if (fr.m_i == 1 && m().is_ite(t)) { + expr * cond = result_stack()[fr.m_spos].get(); + expr* arg = 0; + if (m().is_true(cond)) { + arg = t->get_arg(1); + } + else if (m().is_false(cond)) { + arg = t->get_arg(2); + } + if (arg) { + result_stack().shrink(fr.m_spos); + result_stack().push_back(arg); + fr.m_state = REWRITE_BUILTIN; + unsigned max_depth = fr.m_max_depth; + if (visit(arg, fr.m_max_depth)) { + m_r = result_stack().back(); + result_stack().pop_back(); + result_stack().pop_back(); + result_stack().push_back(m_r); + cache_result(t, m_r, m_pr, fr.m_cache_result); + frame_stack().pop_back(); + set_new_child_flag(t); + } + m_r = 0; + return true; + } + } + return false; +} + template template void rewriter_tpl::process_app(app * t, frame & fr) { @@ -183,16 +215,10 @@ void rewriter_tpl::process_app(app * t, frame & fr) { case PROCESS_CHILDREN: { unsigned num_args = t->get_num_args(); while (fr.m_i < num_args) { - expr * arg = t->get_arg(fr.m_i); - if (fr.m_i >= 1 && m().is_ite(t) && !ProofGen) { - expr * cond = result_stack()[fr.m_spos].get(); - if (m().is_true(cond)) { - arg = t->get_arg(1); - } - else if (m().is_false(cond)) { - arg = t->get_arg(2); - } + if (!ProofGen && constant_fold(t, fr)) { + return; } + expr * arg = t->get_arg(fr.m_i); fr.m_i++; if (!visit(arg, fr.m_max_depth)) return; From 509f7409badc0e7834968511ca3c6b6f97fdaed6 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Fri, 10 Mar 2017 23:01:43 +0100 Subject: [PATCH 362/562] adding fixedpoint object to C++ API Signed-off-by: Nikolaj Bjorner --- src/api/c++/z3++.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/api/c++/z3++.h b/src/api/c++/z3++.h index 21a3fedf4..bfd4eb2c4 100644 --- a/src/api/c++/z3++.h +++ b/src/api/c++/z3++.h @@ -2175,6 +2175,50 @@ namespace z3 { }; inline std::ostream & operator<<(std::ostream & out, optimize const & s) { out << Z3_optimize_to_string(s.ctx(), s.m_opt); return out; } + class fixedpoint : public object { + Z3_fixedpoint m_fp; + public: + fixedpoint(context& c):object(c) { m_fp = Z3_mk_fixedpoint(c); Z3_fixedpoint_inc_ref(c, m_fp); } + ~fixedpoint() { Z3_fixedpoint_dec_ref(ctx(), m_fp); } + operator Z3_fixedpoint() const { return m_fp; } + void from_string(char const* s) { Z3_fixedpoint_from_string(ctx(), m_fp, s); check_error(); } + void from_file(char const* s) { Z3_fixedpoint_from_file(ctx(), m_fp, s); check_error(); } + void add_rule(expr& rule, symbol const& name) { Z3_fixedpoint_add_rule(ctx(), m_fp, rule, name); check_error(); } + void add_fact(func_decl& f, unsigned * args) { Z3_fixedpoint_add_fact(ctx(), m_fp, f, f.arity(), args); check_error(); } + check_result query(expr& q) { Z3_lbool r = Z3_fixedpoint_query(ctx(), m_fp, q); check_error(); to_check_result(r); } + check_result query(func_decl_vector& relations) { + array rs(relations); + Z3_lbool r = Z3_fixedpoint_query_relations(ctx(), m_fp, rs.size(), rs.ptr()); + check_error(); + return to_check_result(r); + } + expr get_answer() { Z3_ast r = Z3_fixedpoint_get_answer(ctx(), m_fp); check_error(); return expr(ctx(), r); } + std::string reason_unknown() { return Z3_fixedpoint_get_reason_unknown(ctx(), m_fp); } + void update_rule(expr& rule, symbol const& name) { Z3_fixedpoint_update_rule(ctx(), m_fp, rule, name); check_error(); } + unsigned get_num_levels(func_decl& p) { unsigned r = Z3_fixedpoint_get_num_levels(ctx(), m_fp, p); check_error(); return r; } + expr get_cover_delta(int level, func_decl& p) { + Z3_ast r = Z3_fixedpoint_get_cover_delta(ctx(), m_fp, level, p); + check_error(); + return expr(ctx(), r); + } + void add_cover(int level, func_decl& p, expr& property) { Z3_fixedpoint_add_cover(ctx(), m_fp, level, p, property); check_error(); } + stats statistics() const { Z3_stats r = Z3_fixedpoint_get_statistics(ctx(), m_fp); check_error(); return stats(ctx(), r); } + void register_relation(func_decl& p) { Z3_fixedpoint_register_relation(ctx(), m_fp, p); } + expr_vector assertions() const { Z3_ast_vector r = Z3_fixedpoint_get_assertions(ctx(), m_fp); check_error(); return expr_vector(ctx(), r); } + expr_vector rules() const { Z3_ast_vector r = Z3_fixedpoint_get_rules(ctx(), m_fp); check_error(); return expr_vector(ctx(), r); } + void set(params const & p) { Z3_fixedpoint_set_params(ctx(), m_fp, p); check_error(); } + std::string help() const { return Z3_fixedpoint_get_help(ctx(), m_fp); } + param_descrs get_param_descrs() { return param_descrs(ctx(), Z3_fixedpoint_get_param_descrs(ctx(), m_fp)); } + std::string to_string() { return Z3_fixedpoint_to_string(ctx(), m_fp, 0, 0); } + std::string to_string(expr_vector const& queries) { + array qs(queries); + return Z3_fixedpoint_to_string(ctx(), m_fp, qs.size(), qs.ptr()); + } + void push() { Z3_fixedpoint_push(ctx(), m_fp); check_error(); } + void pop() { Z3_fixedpoint_pop(ctx(), m_fp); check_error(); } + }; + inline std::ostream & operator<<(std::ostream & out, fixedpoint const & f) { return out << Z3_fixedpoint_to_string(f.ctx(), f, 0, 0); } + inline tactic fail_if(probe const & p) { Z3_tactic r = Z3_tactic_fail_if(p.ctx(), p); p.check_error(); From 228111511c8293c7a4b35743b141693d4a3b6415 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sat, 11 Mar 2017 18:41:36 +0100 Subject: [PATCH 363/562] fixing build break, addressing #935 Signed-off-by: Nikolaj Bjorner --- src/cmd_context/cmd_context.cpp | 1 + src/smt/mam.cpp | 2 +- src/smt/smt_context.cpp | 40 +++++++++++++++++++++++++++++---- src/smt/smt_context.h | 8 +++++++ src/smt/smt_quantifier.cpp | 26 ++++++++++++++------- 5 files changed, 64 insertions(+), 13 deletions(-) diff --git a/src/cmd_context/cmd_context.cpp b/src/cmd_context/cmd_context.cpp index 67b1df50c..2551f0aa0 100644 --- a/src/cmd_context/cmd_context.cpp +++ b/src/cmd_context/cmd_context.cpp @@ -1551,6 +1551,7 @@ void cmd_context::validate_model() { p.set_uint("sort_store", true); p.set_bool("completion", true); model_evaluator evaluator(*(md.get()), p); + evaluator.set_expand_array_equalities(false); contains_array_op_proc contains_array(m()); { scoped_rlimit _rlimit(m().limit(), 0); diff --git a/src/smt/mam.cpp b/src/smt/mam.cpp index 5d03a3563..ba9f970a2 100644 --- a/src/smt/mam.cpp +++ b/src/smt/mam.cpp @@ -3942,7 +3942,7 @@ namespace smt { #endif virtual void on_match(quantifier * qa, app * pat, unsigned num_bindings, enode * const * bindings, unsigned max_generation, ptr_vector & used_enodes) { - TRACE("trigger_bug", tout << "found match\n";); + TRACE("trigger_bug", tout << "found match " << mk_pp(qa, m_ast_manager) << "\n";); #ifdef Z3DEBUG if (m_check_missing_instances) { if (!m_context.slow_contains_instance(qa, num_bindings, bindings)) { diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index e816f5c73..3fca7d04b 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -304,7 +304,6 @@ namespace smt { TRACE("assign_core", tout << (decision?"decision: ":"propagating: ") << l << " "; display_literal_verbose(tout, l); tout << " level: " << m_scope_lvl << "\n"; display(tout, j);); - SASSERT(l.var() < static_cast(m_b_internalized_stack.size())); m_assigned_literals.push_back(l); m_assignment[l.index()] = l_true; m_assignment[(~l).index()] = l_false; @@ -319,14 +318,23 @@ namespace smt { d.m_phase_available = true; d.m_phase = !l.sign(); TRACE("phase_selection", tout << "saving phase, is_pos: " << d.m_phase << " l: " << l << "\n";); + TRACE("relevancy", - tout << "is_atom: " << d.is_atom() << " is relevant: " << is_relevant_core(bool_var2expr(l.var())) << "\n";); - if (d.is_atom() && (m_fparams.m_relevancy_lvl == 0 || (m_fparams.m_relevancy_lvl == 1 && !d.is_quantifier()) || is_relevant_core(bool_var2expr(l.var())))) + tout << "is_atom: " << d.is_atom() << " is relevant: " << is_relevant_core(l) << "\n";); + if (d.is_atom() && (m_fparams.m_relevancy_lvl == 0 || (m_fparams.m_relevancy_lvl == 1 && !d.is_quantifier()) || is_relevant_core(l))) m_atom_propagation_queue.push_back(l); if (m_manager.has_trace_stream()) trace_assign(l, j, decision); m_case_split_queue->assign_lit_eh(l); + + // a unit is asserted at search level. Mark it as relevant. + // this addresses bug... where a literal becomes fixed to true (false) + // as a conflict gets assigned misses relevancy (and quantifier instantiation). + // + if (false && !decision && relevancy() && at_search_level() && !is_relevant_core(l)) { + mark_as_relevant(l); + } } bool context::bcp() { @@ -1634,7 +1642,7 @@ namespace smt { m_atom_propagation_queue.push_back(literal(v, val == l_false)); } } - TRACE("propagate_relevancy", tout << "marking as relevant:\n" << mk_bounded_pp(n, m_manager) << "\n";); + TRACE("propagate_relevancy", tout << "marking as relevant:\n" << mk_bounded_pp(n, m_manager) << " " << m_scope_lvl << "\n";); #ifndef SMTCOMP m_case_split_queue->relevant_eh(n); #endif @@ -3737,7 +3745,9 @@ namespace smt { // I invoke pop_scope_core instead of pop_scope because I don't want // to reset cached generations... I need them to rebuild the literals // of the new conflict clause. + if (relevancy()) record_relevancy(num_lits, lits); unsigned num_bool_vars = pop_scope_core(m_scope_lvl - new_lvl); + if (relevancy()) restore_relevancy(num_lits, lits); SASSERT(m_scope_lvl == new_lvl); // the logical context may still be in conflict after // clauses are reinitialized in pop_scope. @@ -3850,6 +3860,28 @@ namespace smt { } return false; } + + /* + \brief we record and restore relevancy information for literals in conflict clauses. + A literal may have been marked relevant within the scope that gets popped during + conflict resolution. In this case, the literal is no longer marked as relevant after + the pop. This can cause quantifier instantiation to miss relevant triggers and thereby + cause incmpleteness. + */ + void context::record_relevancy(unsigned n, literal const* lits) { + m_relevant_conflict_literals.reset(); + for (unsigned i = 0; i < n; ++i) { + m_relevant_conflict_literals.push_back(is_relevant(lits[i])); + } + } + + void context::restore_relevancy(unsigned n, literal const* lits) { + for (unsigned i = 0; i < n; ++i) { + if (m_relevant_conflict_literals[i] && !is_relevant(lits[i])) { + mark_as_relevant(lits[i]); + } + } + } void context::get_relevant_labels(expr* cnstr, buffer & result) { if (m_fparams.m_check_at_labels) { diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index c1c684fec..2a555e6b5 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -1103,6 +1103,10 @@ namespace smt { bool is_relevant_core(expr * n) const { return m_relevancy_propagator->is_relevant(n); } + svector m_relevant_conflict_literals; + void record_relevancy(unsigned n, literal const* lits); + void restore_relevancy(unsigned n, literal const* lits); + public: // event handler for relevancy_propagator class void relevant_eh(expr * n); @@ -1124,6 +1128,10 @@ namespace smt { return is_relevant(l.var()); } + bool is_relevant_core(literal l) const { + return is_relevant_core(bool_var2expr(l.var())); + } + void mark_as_relevant(expr * n) { m_relevancy_propagator->mark_as_relevant(n); m_relevancy_propagator->propagate(); } void mark_as_relevant(enode * n) { mark_as_relevant(n->get_owner()); } diff --git a/src/smt/smt_quantifier.cpp b/src/smt/smt_quantifier.cpp index 2a56168f2..bad788f5d 100644 --- a/src/smt/smt_quantifier.cpp +++ b/src/smt/smt_quantifier.cpp @@ -52,8 +52,9 @@ namespace smt { m_qi_queue.setup(); } - bool has_trace_stream() const { return m_context.get_manager().has_trace_stream(); } - std::ostream & trace_stream() { return m_context.get_manager().trace_stream(); } + ast_manager& m() const { return m_context.get_manager(); } + bool has_trace_stream() const { return m().has_trace_stream(); } + std::ostream & trace_stream() { return m().trace_stream(); } quantifier_stat * get_stat(quantifier * q) const { return m_quantifier_stat.find(q); @@ -110,8 +111,9 @@ namespace smt { unsigned max_top_generation, ptr_vector & used_enodes) { max_generation = std::max(max_generation, get_generation(q)); - if (m_num_instances > m_params.m_qi_max_instances) + if (m_num_instances > m_params.m_qi_max_instances) { return false; + } get_stat(q)->update_max_generation(max_generation); fingerprint * f = m_context.add_fingerprint(q, q->get_id(), num_bindings, bindings); if (f) { @@ -132,9 +134,17 @@ namespace smt { } m_qi_queue.insert(f, pat, max_generation, min_top_generation, max_top_generation); // TODO m_num_instances++; - return true; } - return false; + TRACE("quantifier", + tout << mk_pp(q, m()) << " "; + for (unsigned i = 0; i < num_bindings; ++i) { + tout << mk_pp(bindings[i]->get_owner(), m()) << " "; + } + tout << "\n"; + tout << "inserted: " << (f != 0) << "\n"; + ); + + return f != 0; } void init_search_eh() { @@ -186,7 +196,7 @@ namespace smt { } bool check_quantifier(quantifier* q) { - return m_context.is_relevant(q) && m_context.get_assignment(q) == l_true; // && !m_context.get_manager().is_rec_fun_def(q); + return m_context.is_relevant(q) && m_context.get_assignment(q) == l_true; // && !m().is_rec_fun_def(q); } bool quick_check_quantifiers() { @@ -501,13 +511,13 @@ namespace smt { SASSERT(m_context->get_manager().is_pattern(mp)); bool unary = (mp->get_num_args() == 1); if (!unary && j >= num_eager_multi_patterns) { - TRACE("assign_quantifier", tout << "delaying (too many multipatterns):\n" << mk_ismt2_pp(mp, m_context->get_manager()) << "\n" + TRACE("quantifier", tout << "delaying (too many multipatterns):\n" << mk_ismt2_pp(mp, m_context->get_manager()) << "\n" << "j: " << j << " unary: " << unary << " m_params.m_qi_max_eager_multipatterns: " << m_fparams->m_qi_max_eager_multipatterns << " num_eager_multi_patterns: " << num_eager_multi_patterns << "\n";); m_lazy_mam->add_pattern(q, mp); } else { - TRACE("assign_quantifier", tout << "adding:\n" << mk_ismt2_pp(mp, m_context->get_manager()) << "\n";); + TRACE("quantifier", tout << "adding:\n" << mk_ismt2_pp(mp, m_context->get_manager()) << "\n";); m_mam->add_pattern(q, mp); } if (!unary) From 559c5e5ae63e13a0fbb18b7b6589d8a0b111debe Mon Sep 17 00:00:00 2001 From: James Bornholt Date: Sat, 11 Mar 2017 16:03:09 -0800 Subject: [PATCH 364/562] z3py: With tactical should not try to use context as a parameter --- src/api/python/z3/z3.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/python/z3/z3.py b/src/api/python/z3/z3.py index a0aba95d6..16d7fbb5f 100644 --- a/src/api/python/z3/z3.py +++ b/src/api/python/z3/z3.py @@ -7204,7 +7204,7 @@ def With(t, *args, **keys): >>> t((x + 1)*(y + 2) == 0) [[2*x + y + x*y == -2]] """ - ctx = keys.get('ctx', None) + ctx = keys.pop('ctx', None) t = _to_tactic(t, ctx) p = args2params(args, keys, t.ctx) return Tactic(Z3_tactic_using_params(t.ctx.ref(), t.tactic, p.params), t.ctx) From 8bec1e25a83e8e3f8c9b48f1641b68b6df841f08 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sun, 12 Mar 2017 08:32:06 +0100 Subject: [PATCH 365/562] move restore relevancy until after literals have been replayed Signed-off-by: Nikolaj Bjorner --- src/smt/smt_context.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 3fca7d04b..9336322f7 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -3747,7 +3747,6 @@ namespace smt { // of the new conflict clause. if (relevancy()) record_relevancy(num_lits, lits); unsigned num_bool_vars = pop_scope_core(m_scope_lvl - new_lvl); - if (relevancy()) restore_relevancy(num_lits, lits); SASSERT(m_scope_lvl == new_lvl); // the logical context may still be in conflict after // clauses are reinitialized in pop_scope. @@ -3778,6 +3777,7 @@ namespace smt { } } } + if (relevancy()) restore_relevancy(num_lits, lits); // Resetting the cache manually because I did not invoke pop_scope, but pop_scope_core reset_cache_generation(); TRACE("resolve_conflict_bug", From 2cb4223979cc94e2ebc4e49a9e83adbdcd2b6979 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Sun, 12 Mar 2017 12:29:26 +0100 Subject: [PATCH 366/562] [CMake] Support including Git hash and description into the build. CMake will automatically pick up changes in git's HEAD so that the necessary code is rebuilt when the build system is invoked. Two new options `INCLUDE_GIT_HASH` and `INCLUDE_GIT_DESCRIBE` have been added that enable/disable including the git hash and the output of `git describe` respectively. By default if the source tree is a git repository both options are on, otherwise they are false by default. To support the `Z3GITHASH` macro a different implementation is used from the old build system. In that build system the define is passed on the command line. This would not work well for CMake because CMake conservatively (and correctly) rebuilds *everything* if the flags given to the compiler change. This would result in the entire project being rebuilt everytime git's `HEAD` changed. Instead in this implementation a CMake specific version of `version.h.in` (named `version.h.cmake.in`) is added that uses the `#cmakedefine` feature of CMake's `configure_file()` command to define `Z3GITHASH` if it is available and not define it otherwise. This way only object files that depend on `version.h` get re-built rather than the whole project. It is unfortunate that the build systems now have different `version.h` file templates. However they are very simple and I don't want to modify how templates are handled in the python/Makefile build system. --- CMakeLists.txt | 60 ++++++++- README-CMake.md | 2 + contrib/cmake/cmake/git_utils.cmake | 173 ++++++++++++++++++++++++++ contrib/cmake/src/util/CMakeLists.txt | 4 +- src/util/version.h.cmake.in | 8 ++ 5 files changed, 245 insertions(+), 2 deletions(-) create mode 100644 contrib/cmake/cmake/git_utils.cmake create mode 100644 src/util/version.h.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 87bd07f31..8788cdaf4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,8 +35,8 @@ set(Z3_VERSION_MAJOR 4) set(Z3_VERSION_MINOR 5) set(Z3_VERSION_PATCH 1) set(Z3_VERSION_TWEAK 0) -set(Z3_FULL_VERSION 0) set(Z3_VERSION "${Z3_VERSION_MAJOR}.${Z3_VERSION_MINOR}.${Z3_VERSION_PATCH}.${Z3_VERSION_TWEAK}") +set(Z3_FULL_VERSION_STR "${Z3_VERSION}") # Note this might be modified message(STATUS "Z3 version ${Z3_VERSION}") ################################################################################ @@ -75,6 +75,64 @@ endif() ################################################################################ list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules") +################################################################################ +# Handle git hash and description +################################################################################ +include(${CMAKE_SOURCE_DIR}/cmake/git_utils.cmake) +macro(disable_git_describe) + message(WARNING "Disabling INCLUDE_GIT_DESCRIBE") + set(INCLUDE_GIT_DESCRIBE OFF CACHE BOOL "Include git describe output in version output" FORCE) +endmacro() +macro(disable_git_hash) + message(WARNING "Disabling INCLUDE_GIT_HASH") + set(INCLUDE_GIT_HASH OFF CACHE BOOL "Include git hash in version output" FORCE) + unset(Z3GITHASH) # Used in configure_file() +endmacro() +option(INCLUDE_GIT_HASH "Include git hash in version output" ON) +option(INCLUDE_GIT_DESCRIBE "Include git describe output in version output" ON) + +set(GIT_DIR "${CMAKE_SOURCE_DIR}/.git") +if (EXISTS "${GIT_DIR}") + # Try to make CMake configure depend on the current git HEAD so that + # a re-configure is triggered when the HEAD changes. + add_git_dir_dependency("${GIT_DIR}" ADD_GIT_DEP_SUCCESS) + if (ADD_GIT_DEP_SUCCESS) + if (INCLUDE_GIT_HASH) + get_git_head_hash("${GIT_DIR}" Z3GITHASH) + if (NOT Z3GITHASH) + message(WARNING "Failed to get Git hash") + disable_git_hash() + endif() + message(STATUS "Using Git hash in version output: ${Z3GITHASH}") + # This mimics the behaviour of the old build system. + string(APPEND Z3_FULL_VERSION_STR " ${Z3GITHASH}") + else() + message(STATUS "Not using Git hash in version output") + unset(Z3GITHASH) # Used in configure_file() + endif() + if (INCLUDE_GIT_DESCRIBE) + get_git_head_describe("${GIT_DIR}" Z3_GIT_DESCRIPTION) + if (NOT Z3_GIT_DESCRIPTION) + message(WARNING "Failed to get Git description") + disable_git_describe() + endif() + message(STATUS "Using Git description in version output: ${Z3_GIT_DESCRIPTION}") + # This mimics the behaviour of the old build system. + string(APPEND Z3_FULL_VERSION_STR " ${Z3_GIT_DESCRIPTION}") + else() + message(STATUS "Not including git descrption in version") + endif() + else() + message(WARNING "Failed to add git dependency.") + disable_git_describe() + disable_git_hash() + endif() +else() + message(STATUS "Failed to find git directory.") + disable_git_describe() + disable_git_hash() +endif() + ################################################################################ # Useful CMake functions/Macros ################################################################################ diff --git a/README-CMake.md b/README-CMake.md index 0943e7a7d..2a8317890 100644 --- a/README-CMake.md +++ b/README-CMake.md @@ -283,6 +283,8 @@ The following useful options can be passed to CMake whilst configuring. * ``INSTALL_JAVA_BINDINGS`` - BOOL. If set to ``TRUE`` and ``BUILD_JAVA_BINDINGS`` is ``TRUE`` then running the ``install`` target will install Z3's Java bindings. * ``Z3_JAVA_JAR_INSTALLDIR`` - STRING. The path to directory to install the Z3 Java ``.jar`` file. This path should be relative to ``CMAKE_INSTALL_PREFIX``. * ``Z3_JAVA_JNI_LIB_INSTALLDIRR`` - STRING. The path to directory to install the Z3 Java JNI bridge library. This path should be relative to ``CMAKE_INSTALL_PREFIX``. +* ``INCLUDE_GIT_DESCRIBE`` - BOOL. If set to ``TRUE`` and the source tree of Z3 is a git repository then the output of ``git describe`` will be included in the build. +* ``INCLUDE_GIT_HASH`` - BOOL. If set to ``TRUE`` and the source tree of Z3 is a git repository then the git hash will be included in the build. On the command line these can be passed to ``cmake`` using the ``-D`` option. In ``ccmake`` and ``cmake-gui`` these can be set in the user interface. diff --git a/contrib/cmake/cmake/git_utils.cmake b/contrib/cmake/cmake/git_utils.cmake new file mode 100644 index 000000000..aa7f38825 --- /dev/null +++ b/contrib/cmake/cmake/git_utils.cmake @@ -0,0 +1,173 @@ +# add_git_dir_dependency(GIT_DIR SUCCESS_VAR) +# +# Adds a configure time dependency on the git directory such that if the HEAD +# of the git directory changes CMake will be forced to re-run. This useful +# for fetching the current git hash and including it in the build. +# +# `GIT_DIR` is the path to the git directory (i.e. the `.git` directory) +# `SUCCESS_VAR` is the name of the variable to set. It will be set to TRUE +# if the dependency was successfully added and FALSE otherwise. +function(add_git_dir_dependency GIT_DIR SUCCESS_VAR) + if (NOT "${ARGC}" EQUAL 2) + message(FATAL_ERROR "Invalid number (${ARGC}) of arguments") + endif() + + if (NOT IS_ABSOLUTE "${GIT_DIR}") + message(FATAL_ERROR "GIT_DIR (\"${GIT_DIR}\") is not an absolute path") + endif() + + if (NOT IS_DIRECTORY "${GIT_DIR}") + message(FATAL_ERROR "GIT_DIR (\"${GIT_DIR}\") is not a directory") + endif() + + set(GIT_HEAD_FILE "${GIT_DIR}/HEAD") + if (NOT EXISTS "${GIT_HEAD_FILE}") + message(AUTHOR_WARNING "Git head file \"${GIT_HEAD_FILE}\" cannot be found") + set(${SUCCESS_VAR} FALSE PARENT_SCOPE) + return() + endif() + + # List of files in the git tree that CMake configuration should depend on + set(GIT_FILE_DEPS "${GIT_HEAD_FILE}") + + # Examine the HEAD and workout what additional dependencies there are. + file(READ "${GIT_HEAD_FILE}" GIT_HEAD_DATA LIMIT 128) + string(STRIP "${GIT_HEAD_DATA}" GIT_HEAD_DATA_STRIPPED) + + if ("${GIT_HEAD_DATA_STRIPPED}" MATCHES "^ref:[ ]*(.+)$") + # HEAD points at a reference. + set(GIT_REF "${CMAKE_MATCH_1}") + if (EXISTS "${GIT_DIR}/${GIT_REF}") + # Unpacked reference. The file contains the commit hash + # so add a dependency on this file so that if we stay on this + # reference (i.e. branch) but change commit CMake will be forced + # to reconfigure. + list(APPEND GIT_FILE_DEPS "${GIT_DIR}/${GIT_REF}") + elseif(EXISTS "${GIT_DIR}/packed-refs") + # The ref must be packed (see `man git-pack-refs`). + list(APPEND GIT_FILE_DEPS "${GIT_DIR}/packed-refs") + else() + # Fail + message(AUTHOR_WARNING "Unhandled git reference") + set(${SUCCESS_VAR} FALSE PARENT_SCOPE) + return() + endif() + else() + # Detached HEAD. + # No other dependencies needed + endif() + + # FIXME: + # This is the directory we will copy (via `configure_file()`) git files + # into. This is a hack. It would be better to use the + # `CMAKE_CONFIGURE_DEPENDS` directory property but that feature is not + # available in CMake 2.8.12. So we use `configure_file()` to effectively + # do the same thing. When the source file to `configure_file()` changes + # it will trigger a re-run of CMake. + set(GIT_CMAKE_FILES_DIR "${CMAKE_CURRENT_BINARY_DIR}/git_cmake_files") + file(MAKE_DIRECTORY "${GIT_CMAKE_FILES_DIR}") + + foreach (git_dependency ${GIT_FILE_DEPS}) + message(STATUS "Adding git dependency \"${git_dependency}\"") + configure_file( + "${git_dependency}" + "${GIT_CMAKE_FILES_DIR}" + COPYONLY + ) + endforeach() + + set(${SUCCESS_VAR} TRUE PARENT_SCOPE) +endfunction() + +# get_git_head_hash(GIT_DIR OUTPUT_VAR) +# +# Retrieve the current commit hash for a git working directory where `GIT_DIR` +# is the `.git` directory in the root of the git working directory. +# +# `OUTPUT_VAR` should be the name of the variable to put the result in. If this +# function fails then either a fatal error will be raised or `OUTPUT_VAR` will +# contain a string with the suffix `NOTFOUND` which can be used in CMake `if()` +# commands. +function(get_git_head_hash GIT_DIR OUTPUT_VAR) + if (NOT "${ARGC}" EQUAL 2) + message(FATAL_ERROR "Invalid number of arguments") + endif() + if (NOT IS_DIRECTORY "${GIT_DIR}") + message(FATAL_ERROR "\"${GIT_DIR}\" is not a directory") + endif() + if (NOT IS_ABSOLUTE "${GIT_DIR}") + message(FATAL_ERROR \""${GIT_DIR}\" is not an absolute path") + endif() + find_package(Git) + if (NOT Git_FOUND) + set(${OUTPUT_VAR} "GIT-NOTFOUND" PARENT_SCOPE) + return() + endif() + get_filename_component(GIT_WORKING_DIR "${GIT_DIR}" DIRECTORY) + execute_process( + COMMAND + "${GIT_EXECUTABLE}" + "rev-parse" + "-q" # Quiet + "HEAD" + WORKING_DIRECTORY + "${GIT_WORKING_DIR}" + RESULT_VARIABLE + GIT_EXIT_CODE + OUTPUT_VARIABLE + Z3_GIT_HASH + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + if (NOT "${GIT_EXIT_CODE}" EQUAL 0) + message(WARNING "Failed to execute git") + set(${OUTPUT_VAR} NOTFOUND PARENT_SCOPE) + return() + endif() + set(${OUTPUT_VAR} "${Z3_GIT_HASH}" PARENT_SCOPE) +endfunction() + +# get_git_head_describe(GIT_DIR OUTPUT_VAR) +# +# Retrieve the output of `git describe` for a git working directory where +# `GIT_DIR` is the `.git` directory in the root of the git working directory. +# +# `OUTPUT_VAR` should be the name of the variable to put the result in. If this +# function fails then either a fatal error will be raised or `OUTPUT_VAR` will +# contain a string with the suffix `NOTFOUND` which can be used in CMake `if()` +# commands. +function(get_git_head_describe GIT_DIR OUTPUT_VAR) + if (NOT "${ARGC}" EQUAL 2) + message(FATAL_ERROR "Invalid number of arguments") + endif() + if (NOT IS_DIRECTORY "${GIT_DIR}") + message(FATAL_ERROR "\"${GIT_DIR}\" is not a directory") + endif() + if (NOT IS_ABSOLUTE "${GIT_DIR}") + message(FATAL_ERROR \""${GIT_DIR}\" is not an absolute path") + endif() + find_package(Git) + if (NOT Git_FOUND) + set(${OUTPUT_VAR} "GIT-NOTFOUND" PARENT_SCOPE) + return() + endif() + get_filename_component(GIT_WORKING_DIR "${GIT_DIR}" DIRECTORY) + execute_process( + COMMAND + "${GIT_EXECUTABLE}" + "describe" + "--long" + WORKING_DIRECTORY + "${GIT_WORKING_DIR}" + RESULT_VARIABLE + GIT_EXIT_CODE + OUTPUT_VARIABLE + Z3_GIT_DESCRIPTION + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + if (NOT "${GIT_EXIT_CODE}" EQUAL 0) + message(WARNING "Failed to execute git") + set(${OUTPUT_VAR} NOTFOUND PARENT_SCOPE) + return() + endif() + set(${OUTPUT_VAR} "${Z3_GIT_DESCRIPTION}" PARENT_SCOPE) +endfunction() diff --git a/contrib/cmake/src/util/CMakeLists.txt b/contrib/cmake/src/util/CMakeLists.txt index c85076531..b76f909d0 100644 --- a/contrib/cmake/src/util/CMakeLists.txt +++ b/contrib/cmake/src/util/CMakeLists.txt @@ -3,7 +3,9 @@ if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/version.h") ${z3_polluted_tree_msg} ) endif() -configure_file(version.h.in ${CMAKE_CURRENT_BINARY_DIR}/version.h @ONLY) + +set(Z3_FULL_VERSION "\"${Z3_FULL_VERSION_STR}\"") +configure_file(version.h.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/version.h) z3_add_component(util SOURCES diff --git a/src/util/version.h.cmake.in b/src/util/version.h.cmake.in new file mode 100644 index 000000000..af3a652a6 --- /dev/null +++ b/src/util/version.h.cmake.in @@ -0,0 +1,8 @@ +// automatically generated file. +#define Z3_MAJOR_VERSION @Z3_VERSION_MAJOR@ +#define Z3_MINOR_VERSION @Z3_VERSION_MINOR@ +#define Z3_BUILD_NUMBER @Z3_VERSION_PATCH@ +#define Z3_REVISION_NUMBER @Z3_VERSION_TWEAK@ + +#define Z3_FULL_VERSION @Z3_FULL_VERSION@ +#cmakedefine Z3GITHASH @Z3GITHASH@ From 73614abf37feab15d4558cf65d0ab682ecd2e7f7 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Fri, 3 Mar 2017 19:39:53 +0000 Subject: [PATCH 367/562] [CMake] Implement generation of `Z3Config.cmake` and `Z3Target.cmake` file for the build and install tree. These files allow users of CMake to use Z3 via a CMake config package. Clients can do `find_package(Z3 CONFIG)` to get use the package from their projects. When generating the files for the install tree we try to generate the files so that they are relocatable so that it shouldn't matter if the installed files aren't in the CMAKE_INSTALL_PREFIX when a user consumes them. As long as the relative locations of the files aren't changed things should still work. A new CMake cache variable `CMAKE_INSTALL_Z3_CMAKE_PACKAGE_DIR` has been added so that the install location of the Z3 CMake package files can be controlled. This addresses #915 . --- CMakeLists.txt | 78 +++++++++++++++++++++++++++ README-CMake.md | 1 + contrib/cmake/cmake/Z3Config.cmake.in | 30 +++++++++++ contrib/cmake/src/CMakeLists.txt | 1 + 4 files changed, 110 insertions(+) create mode 100644 contrib/cmake/cmake/Z3Config.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 8788cdaf4..72ea1143d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -398,10 +398,18 @@ set(CMAKE_INSTALL_PKGCONFIGDIR PATH "Directory to install pkgconfig files" ) + +set(CMAKE_INSTALL_Z3_CMAKE_PACKAGE_DIR + "${CMAKE_INSTALL_LIBDIR}/cmake/z3" + CACHE + PATH + "Directory to install Z3 CMake package files" +) message(STATUS "CMAKE_INSTALL_LIBDIR: \"${CMAKE_INSTALL_LIBDIR}\"") message(STATUS "CMAKE_INSTALL_BINDIR: \"${CMAKE_INSTALL_BINDIR}\"") message(STATUS "CMAKE_INSTALL_INCLUDEDIR: \"${CMAKE_INSTALL_INCLUDEDIR}\"") message(STATUS "CMAKE_INSTALL_PKGCONFIGDIR: \"${CMAKE_INSTALL_PKGCONFIGDIR}\"") +message(STATUS "CMAKE_INSTALL_Z3_CMAKE_PACKAGE_DIR: \"${CMAKE_INSTALL_Z3_CMAKE_PACKAGE_DIR}\"") ################################################################################ # Uninstall rule @@ -449,6 +457,76 @@ include(${CMAKE_SOURCE_DIR}/cmake/z3_add_component.cmake) include(${CMAKE_SOURCE_DIR}/cmake/z3_append_linker_flag_list_to_target.cmake) add_subdirectory(src) +################################################################################ +# Create `Z3Config.cmake` and related files for the build tree so clients can +# use Z3 via CMake. +################################################################################ +include(CMakePackageConfigHelpers) +export(EXPORT Z3_EXPORTED_TARGETS + NAMESPACE z3:: + FILE "${CMAKE_BINARY_DIR}/Z3Targets.cmake" +) +set(Z3_FIRST_PACKAGE_INCLUDE_DIR "${CMAKE_BINARY_DIR}/src/api") +set(Z3_SECOND_PACKAGE_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/src/api") +set(Z3_CXX_PACKAGE_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/src/api/c++") +set(AUTO_GEN_MSG "Automatically generated. DO NOT EDIT") +set(CONFIG_FILE_TYPE "build tree") +configure_package_config_file("${CMAKE_SOURCE_DIR}/cmake/Z3Config.cmake.in" + "Z3Config.cmake" + INSTALL_DESTINATION "${CMAKE_BINARY_DIR}" + PATH_VARS + Z3_FIRST_PACKAGE_INCLUDE_DIR + Z3_SECOND_PACKAGE_INCLUDE_DIR + Z3_CXX_PACKAGE_INCLUDE_DIR + INSTALL_PREFIX "${CMAKE_BINARY_DIR}" +) +unset(Z3_FIRST_PACKAGE_INCLUDE_DIR) +unset(Z3_SECOND_PACKAGE_INCLUDE_DIR) +unset(Z3_CXX_PACKAGE_INCLUDE_DIR) +unset(AUTO_GEN_MSG) +unset(CONFIG_FILE_TYPE) +# TODO: Provide a `Z3Version.cmake` file so that clients can specify the version +# of Z3 they want. + +################################################################################ +# Create `Z3Config.cmake` and related files for install tree so clients can use +# Z3 via CMake. +################################################################################ +install(EXPORT + Z3_EXPORTED_TARGETS + FILE "Z3Targets.cmake" + NAMESPACE z3:: + DESTINATION "${CMAKE_INSTALL_Z3_CMAKE_PACKAGE_DIR}" +) +set(Z3_INSTALL_TREE_CMAKE_CONFIG_FILE "${CMAKE_BINARY_DIR}/cmake/Z3Config.cmake") +set(Z3_FIRST_PACKAGE_INCLUDE_DIR "${CMAKE_INSTALL_INCLUDEDIR}") +set(Z3_SECOND_INCLUDE_DIR "") +set(Z3_CXX_PACKAGE_INCLUDE_DIR "") +set(AUTO_GEN_MSG "Automatically generated. DO NOT EDIT") +set(CONFIG_FILE_TYPE "install tree") +# We use `configure_package_config_file()` to try and create CMake files +# that are re-locatable so that it doesn't matter if the files aren't placed +# in the original install prefix. +configure_package_config_file("${CMAKE_SOURCE_DIR}/cmake/Z3Config.cmake.in" + "${Z3_INSTALL_TREE_CMAKE_CONFIG_FILE}" + INSTALL_DESTINATION "${CMAKE_INSTALL_Z3_CMAKE_PACKAGE_DIR}" + PATH_VARS Z3_FIRST_PACKAGE_INCLUDE_DIR +) +unset(Z3_FIRST_PACKAGE_INCLUDE_DIR) +unset(Z3_SECOND_PACKAGE_INCLUDE_DIR) +unset(Z3_CXX_PACKAGE_INCLUDE_DIR) +unset(AUTO_GEN_MSG) +unset(CONFIG_FILE_TYPE) + +# Add install rule to install ${Z3_INSTALL_TREE_CMAKE_CONFIG_FILE} +install( + FILES "${Z3_INSTALL_TREE_CMAKE_CONFIG_FILE}" + DESTINATION "${CMAKE_INSTALL_Z3_CMAKE_PACKAGE_DIR}" +) + +# TODO: Provide a `Z3Version.cmake` file so that clients can specify the version +# of Z3 they want. + ################################################################################ # Examples ################################################################################ diff --git a/README-CMake.md b/README-CMake.md index 2a8317890..6a78b5d4c 100644 --- a/README-CMake.md +++ b/README-CMake.md @@ -267,6 +267,7 @@ The following useful options can be passed to CMake whilst configuring. * ``CMAKE_INSTALL_PREFIX`` - STRING. The install prefix to use (e.g. ``/usr/local/``). * ``CMAKE_INSTALL_PKGCONFIGDIR`` - STRING. The path to install pkgconfig files. * ``CMAKE_INSTALL_PYTHON_PKG_DIR`` - STRING. The path to install the z3 python bindings. This can be relative (to ``CMAKE_INSTALL_PREFIX``) or absolute. +* ``CMAKE_INSTALL_Z3_CMAKE_PACKAGE_DIR`` - STRING. The path to install CMake package files (e.g. ``/usr/lib/cmake/z3``). * ``ENABLE_TRACING_FOR_NON_DEBUG`` - BOOL. If set to ``TRUE`` enable tracing in non-debug builds, if set to ``FALSE`` disable tracing in non-debug builds. Note in debug builds tracing is always enabled. * ``BUILD_LIBZ3_SHARED`` - BOOL. If set to ``TRUE`` build libz3 as a shared library otherwise build as a static library. * ``ENABLE_EXAMPLE_TARGETS`` - BOOL. If set to ``TRUE`` add the build targets for building the API examples. diff --git a/contrib/cmake/cmake/Z3Config.cmake.in b/contrib/cmake/cmake/Z3Config.cmake.in new file mode 100644 index 000000000..e7f604591 --- /dev/null +++ b/contrib/cmake/cmake/Z3Config.cmake.in @@ -0,0 +1,30 @@ +################################################################################ +# @AUTO_GEN_MSG@ +# +# This file is intended to be consumed by clients who wish to use Z3 from CMake. +# It can be use by doing `find_package(Z3 config)` from within a +# `CMakeLists.txt` file. If CMake doesn't find this package automatically you +# can give it a hint by passing `-DZ3_DIR=` to the CMake invocation where +# `` is the path to the directory containing this file. +# +# This file was built for the @CONFIG_FILE_TYPE@. +################################################################################ + +# Exported targets +include("${CMAKE_CURRENT_LIST_DIR}/Z3Targets.cmake") + +@PACKAGE_INIT@ + +# Version information +set(Z3_VERSION_MAJOR @Z3_VERSION_MAJOR@) +set(Z3_VERSION_MINOR @Z3_VERSION_MINOR@) +set(Z3_VERSION_PATCH @Z3_VERSION_PATCH@) +set(Z3_VERSION_TWEAK @Z3_VERSION_TWEAK@) +set(Z3_VERSION_STRING "${Z3_VERSION_MAJOR}.${Z3_VERSION_MINOR}.${Z3_VERSION_PATCH}.${Z3_VERSION_TWEAK}") + +# NOTE: We can't use `set_and_check()` here because this a list of paths. +# List of include directories +set(Z3_C_INCLUDE_DIRS @PACKAGE_Z3_FIRST_PACKAGE_INCLUDE_DIR@ @PACKAGE_Z3_SECOND_PACKAGE_INCLUDE_DIR@) +set(Z3_CXX_INCLUDE_DIRS @PACKAGE_Z3_CXX_PACKAGE_INCLUDE_DIR@ ${Z3_C_INCLUDE_DIRS}) +# List of libraries to link against +set(Z3_LIBRARIES "z3::libz3") diff --git a/contrib/cmake/src/CMakeLists.txt b/contrib/cmake/src/CMakeLists.txt index 65eef8094..548b59053 100644 --- a/contrib/cmake/src/CMakeLists.txt +++ b/contrib/cmake/src/CMakeLists.txt @@ -168,6 +168,7 @@ foreach (header ${libz3_public_headers}) endforeach() install(TARGETS libz3 + EXPORT Z3_EXPORTED_TARGETS LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" # On Windows this installs ``libz3.lib`` which CMake calls the "corresponding import library". Do we want this installed? RUNTIME DESTINATION "${CMAKE_INSTALL_LIBDIR}" # For Windows. DLLs are runtime targets for CMake From d9617841e085f8d62bafb5a6994db96807aacdcd Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Sat, 4 Mar 2017 13:07:36 +0000 Subject: [PATCH 368/562] [CMake] Python examples should only be copied over if python bindings are being built. --- contrib/cmake/examples/CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/contrib/cmake/examples/CMakeLists.txt b/contrib/cmake/examples/CMakeLists.txt index e596ed3dd..04df6e6b4 100644 --- a/contrib/cmake/examples/CMakeLists.txt +++ b/contrib/cmake/examples/CMakeLists.txt @@ -1,4 +1,6 @@ add_subdirectory(c) add_subdirectory(c++) add_subdirectory(tptp) -add_subdirectory(python) +if (BUILD_PYTHON_BINDINGS) + add_subdirectory(python) +endif() From db5520c71d1dca4354ad82bbed4a7afad240dca1 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Sat, 4 Mar 2017 16:45:37 +0000 Subject: [PATCH 369/562] [CMake] Build `c_example`, `cpp_example` and `z3_tptp5` as external projects. This works by giving each example it's own CMake build system and then consuming Z3 via the Z3 CMake config package from the build tree. --- contrib/cmake/examples/CMakeLists.txt | 64 +++++++++++++++++++++- contrib/cmake/examples/c++/CMakeLists.txt | 37 ++++++++++--- contrib/cmake/examples/c/CMakeLists.txt | 35 +++++++++--- contrib/cmake/examples/tptp/CMakeLists.txt | 32 +++++++++-- 4 files changed, 144 insertions(+), 24 deletions(-) diff --git a/contrib/cmake/examples/CMakeLists.txt b/contrib/cmake/examples/CMakeLists.txt index 04df6e6b4..3fa49f9e0 100644 --- a/contrib/cmake/examples/CMakeLists.txt +++ b/contrib/cmake/examples/CMakeLists.txt @@ -1,6 +1,64 @@ -add_subdirectory(c) -add_subdirectory(c++) -add_subdirectory(tptp) +include(ExternalProject) +# Unfortunately `BUILD_ALWAYS` only seems to be supported with the version of ExternalProject +# that shipped with CMake >= 3.1. +if (("${CMAKE_VERSION}" VERSION_EQUAL "3.1") OR ("${CMAKE_VERSION}" VERSION_GREATER "3.1")) + set(EXTERNAL_PROJECT_BUILD_ALWAYS_ARG BUILD_ALWAYS 1) +else() + set(EXTERNAL_PROJECT_BUILD_ALWAYS_ARG "") +endif() + +################################################################################ +# Build example project using libz3's C API as an external project +################################################################################ +ExternalProject_Add(c_example + DEPENDS libz3 + # Configure step + SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/c" + CMAKE_ARGS "-DZ3_DIR=${CMAKE_BINARY_DIR}" + # Build step + ${EXTERNAL_PROJECT_BUILD_ALWAYS_ARG} + BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/c_example_build_dir" + # Install Step + INSTALL_COMMAND "${CMAKE_COMMAND}" -E echo "" # Dummy command +) +set_target_properties(c_example PROPERTIES EXCLUDE_FROM_ALL TRUE) + + +################################################################################ +# Build example project using libz3's C++ API as an external project +################################################################################ +ExternalProject_Add(cpp_example + DEPENDS libz3 + # Configure step + SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/c++" + CMAKE_ARGS "-DZ3_DIR=${CMAKE_BINARY_DIR}" + # Build step + ${EXTERNAL_PROJECT_BUILD_ALWAYS_ARG} + BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/cpp_example_build_dir" + # Install Step + INSTALL_COMMAND "${CMAKE_COMMAND}" -E echo "" # Dummy command +) +set_target_properties(cpp_example PROPERTIES EXCLUDE_FROM_ALL TRUE) + +################################################################################ +# Build example tptp5 project using libz3's C++ API as an external project +################################################################################ +ExternalProject_Add(z3_tptp5 + DEPENDS libz3 + # Configure step + SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/tptp" + CMAKE_ARGS "-DZ3_DIR=${CMAKE_BINARY_DIR}" + # Build step + ${EXTERNAL_PROJECT_BUILD_ALWAYS_ARG} + BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/tptp_build_dir" + # Install Step + INSTALL_COMMAND "${CMAKE_COMMAND}" -E echo "" # Dummy command +) +set_target_properties(z3_tptp5 PROPERTIES EXCLUDE_FROM_ALL TRUE) + +################################################################################ +# Build Python examples +################################################################################ if (BUILD_PYTHON_BINDINGS) add_subdirectory(python) endif() diff --git a/contrib/cmake/examples/c++/CMakeLists.txt b/contrib/cmake/examples/c++/CMakeLists.txt index fdc5cf387..b25bea533 100644 --- a/contrib/cmake/examples/c++/CMakeLists.txt +++ b/contrib/cmake/examples/c++/CMakeLists.txt @@ -1,9 +1,28 @@ -# FIXME: We should build this as an external project and consume -# Z3 as `find_package(z3 CONFIG)`. -add_executable(cpp_example EXCLUDE_FROM_ALL example.cpp) -target_link_libraries(cpp_example PRIVATE libz3) -target_include_directories(cpp_example PRIVATE "${CMAKE_SOURCE_DIR}/src/api") -target_include_directories(cpp_example PRIVATE "${CMAKE_SOURCE_DIR}/src/api/c++") -if (NOT BUILD_LIBZ3_SHARED) - z3_append_linker_flag_list_to_target(cpp_example ${Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS}) -endif() +################################################################################ +# Example C++ project +################################################################################ +project(Z3_C_EXAMPLE CXX) +cmake_minimum_required(VERSION 2.8.12) +find_package(Z3 + REQUIRED + CONFIG + # `NO_DEFAULT_PATH` is set so that -DZ3_DIR has to be passed to find Z3. + # This should prevent us from accidently picking up an installed + # copy of Z3. This is here to benefit Z3's build sytem when building + # this project. When making your own project you probably shouldn't + # use this option. + NO_DEFAULT_PATH +) +message(STATUS "Z3_FOUND: ${Z3_FOUND}") +message(STATUS "Found Z3 ${Z3_VERSION_STRING}") +message(STATUS "Z3_DIR: ${Z3_DIR}") + +add_executable(cpp_example example.cpp) +target_include_directories(cpp_example PRIVATE ${Z3_CXX_INCLUDE_DIRS}) +target_link_libraries(cpp_example PRIVATE ${Z3_LIBRARIES}) + +# FIXME: The Z3 package does not export information on the link flags +# This is needed for when libz3 is built as a static library +#if (NOT BUILD_LIBZ3_SHARED) +# z3_append_linker_flag_list_to_target(cpp_example ${Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS}) +#endif() diff --git a/contrib/cmake/examples/c/CMakeLists.txt b/contrib/cmake/examples/c/CMakeLists.txt index fc6eaee18..d51503e29 100644 --- a/contrib/cmake/examples/c/CMakeLists.txt +++ b/contrib/cmake/examples/c/CMakeLists.txt @@ -1,9 +1,28 @@ -# FIXME: We should build this as an external project and consume -# Z3 as `find_package(z3 CONFIG)`. -add_executable(c_example EXCLUDE_FROM_ALL test_capi.c) -target_link_libraries(c_example PRIVATE libz3) -target_include_directories(c_example PRIVATE "${CMAKE_SOURCE_DIR}/src/api") +################################################################################ +# Example C project +################################################################################ +project(Z3_C_EXAMPLE C) +cmake_minimum_required(VERSION 2.8.12) +find_package(Z3 + REQUIRED + CONFIG + # `NO_DEFAULT_PATH` is set so that -DZ3_DIR has to be passed to find Z3. + # This should prevent us from accidently picking up an installed + # copy of Z3. This is here to benefit Z3's build sytem when building + # this project. When making your own project you probably shouldn't + # use this option. + NO_DEFAULT_PATH +) +message(STATUS "Z3_FOUND: ${Z3_FOUND}") +message(STATUS "Found Z3 ${Z3_VERSION_STRING}") +message(STATUS "Z3_DIR: ${Z3_DIR}") + +add_executable(c_example test_capi.c) +target_include_directories(c_example PRIVATE ${Z3_C_INCLUDE_DIRS}) +target_link_libraries(c_example PRIVATE ${Z3_LIBRARIES}) + +# FIXME: The Z3 package does not export information on the link flags # This is needed for when libz3 is built as a static library -if (NOT BUILD_LIBZ3_SHARED) - z3_append_linker_flag_list_to_target(c_example ${Z3_DEPENDENT_EXTRA_C_LINK_FLAGS}) -endif() +#if (NOT BUILD_LIBZ3_SHARED) +# z3_append_linker_flag_list_to_target(c_example ${Z3_DEPENDENT_EXTRA_C_LINK_FLAGS}) +#endif() diff --git a/contrib/cmake/examples/tptp/CMakeLists.txt b/contrib/cmake/examples/tptp/CMakeLists.txt index 41fa9cc65..6a2858a9b 100644 --- a/contrib/cmake/examples/tptp/CMakeLists.txt +++ b/contrib/cmake/examples/tptp/CMakeLists.txt @@ -1,4 +1,28 @@ -add_executable(z3_tptp5 EXCLUDE_FROM_ALL tptp5.cpp tptp5.lex.cpp) -target_link_libraries(z3_tptp5 PRIVATE libz3) -target_include_directories(z3_tptp5 PRIVATE "${CMAKE_SOURCE_DIR}/src/api") -target_include_directories(z3_tptp5 PRIVATE "${CMAKE_SOURCE_DIR}/src/api/c++") +################################################################################ +# TPTP example +################################################################################ +project(Z3_TPTP5 CXX) +cmake_minimum_required(VERSION 2.8.12) +find_package(Z3 + REQUIRED + CONFIG + # `NO_DEFAULT_PATH` is set so that -DZ3_DIR has to be passed to find Z3. + # This should prevent us from accidently picking up an installed + # copy of Z3. This is here to benefit Z3's build sytem when building + # this project. When making your own project you probably shouldn't + # use this option. + NO_DEFAULT_PATH +) +message(STATUS "Z3_FOUND: ${Z3_FOUND}") +message(STATUS "Found Z3 ${Z3_VERSION_STRING}") +message(STATUS "Z3_DIR: ${Z3_DIR}") + +add_executable(z3_tptp5 tptp5.cpp tptp5.lex.cpp) +target_include_directories(z3_tptp5 PRIVATE ${Z3_CXX_INCLUDE_DIRS}) +target_link_libraries(z3_tptp5 PRIVATE ${Z3_LIBRARIES}) + +# FIXME: The Z3 package does not export information on the link flags +# This is needed for when libz3 is built as a static library +#if (NOT BUILD_LIBZ3_SHARED) +# z3_append_linker_flag_list_to_target(z3_tptp5 ${Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS}) +#endif() From b20bf5169a5f9d7c6e235199b081993f210084c6 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Sat, 4 Mar 2017 16:49:19 +0000 Subject: [PATCH 370/562] [CMake] Fix typo handling OpenMP flags. --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 72ea1143d..6281f951b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -294,11 +294,11 @@ if (OPENMP_FOUND) # flag by MSVC and breaks the build if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")) - list(APPEND Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS ${OpenMP_C_FLAGS}) + list(APPEND Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS ${OpenMP_CXX_FLAGS}) endif() if (("${CMAKE_C_COMPILER_ID}" MATCHES "Clang") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU")) - list(APPEND Z3_DEPENDENT_EXTRA_C_LINK_FLAGS ${OpenMP_CXX_FLAGS}) + list(APPEND Z3_DEPENDENT_EXTRA_C_LINK_FLAGS ${OpenMP_C_FLAGS}) endif() unset(CMAKE_REQUIRED_FLAGS) message(STATUS "Using OpenMP") From ac85c68ccb437590c2e6669d892eef146ba87d65 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Sat, 4 Mar 2017 19:01:23 +0000 Subject: [PATCH 371/562] [CMake] Fix examples linking against libz3 when it is built as a static library on Linux. --- CMakeLists.txt | 6 ------ contrib/cmake/examples/c++/CMakeLists.txt | 6 ------ contrib/cmake/examples/c/CMakeLists.txt | 12 +++++------- contrib/cmake/examples/tptp/CMakeLists.txt | 6 ------ contrib/cmake/src/CMakeLists.txt | 8 +++++++- contrib/cmake/src/api/java/CMakeLists.txt | 1 - 6 files changed, 12 insertions(+), 27 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6281f951b..cf46e7012 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -147,7 +147,6 @@ set(Z3_COMPONENT_CXX_FLAGS "") set(Z3_COMPONENT_EXTRA_INCLUDE_DIRS "") set(Z3_DEPENDENT_LIBS "") set(Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS "") -set(Z3_DEPENDENT_EXTRA_C_LINK_FLAGS "") ################################################################################ # Build type @@ -296,10 +295,6 @@ if (OPENMP_FOUND) ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")) list(APPEND Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS ${OpenMP_CXX_FLAGS}) endif() - if (("${CMAKE_C_COMPILER_ID}" MATCHES "Clang") OR - ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU")) - list(APPEND Z3_DEPENDENT_EXTRA_C_LINK_FLAGS ${OpenMP_C_FLAGS}) - endif() unset(CMAKE_REQUIRED_FLAGS) message(STATUS "Using OpenMP") else() @@ -386,7 +381,6 @@ message(STATUS "Z3_COMPONENT_CXX_FLAGS: ${Z3_COMPONENT_CXX_FLAGS}") message(STATUS "Z3_DEPENDENT_LIBS: ${Z3_DEPENDENT_LIBS}") message(STATUS "Z3_COMPONENT_EXTRA_INCLUDE_DIRS: ${Z3_COMPONENT_EXTRA_INCLUDE_DIRS}") message(STATUS "Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS: ${Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS}") -message(STATUS "Z3_DEPENDENT_EXTRA_C_LINK_FLAGS: ${Z3_DEPENDENT_EXTRA_C_LINK_FLAGS}") ################################################################################ # Z3 installation locations diff --git a/contrib/cmake/examples/c++/CMakeLists.txt b/contrib/cmake/examples/c++/CMakeLists.txt index b25bea533..c37aa475d 100644 --- a/contrib/cmake/examples/c++/CMakeLists.txt +++ b/contrib/cmake/examples/c++/CMakeLists.txt @@ -20,9 +20,3 @@ message(STATUS "Z3_DIR: ${Z3_DIR}") add_executable(cpp_example example.cpp) target_include_directories(cpp_example PRIVATE ${Z3_CXX_INCLUDE_DIRS}) target_link_libraries(cpp_example PRIVATE ${Z3_LIBRARIES}) - -# FIXME: The Z3 package does not export information on the link flags -# This is needed for when libz3 is built as a static library -#if (NOT BUILD_LIBZ3_SHARED) -# z3_append_linker_flag_list_to_target(cpp_example ${Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS}) -#endif() diff --git a/contrib/cmake/examples/c/CMakeLists.txt b/contrib/cmake/examples/c/CMakeLists.txt index d51503e29..5ca985845 100644 --- a/contrib/cmake/examples/c/CMakeLists.txt +++ b/contrib/cmake/examples/c/CMakeLists.txt @@ -1,7 +1,11 @@ ################################################################################ # Example C project ################################################################################ -project(Z3_C_EXAMPLE C) +# NOTE: Even though this is a C project, libz3 uses C++. When using libz3 +# as a static library if we don't configure this project to also support +# C++ we will use the C linker rather than the C++ linker and will not link +# the C++ standard library in resulting in a link failure. +project(Z3_C_EXAMPLE C CXX) cmake_minimum_required(VERSION 2.8.12) find_package(Z3 REQUIRED @@ -20,9 +24,3 @@ message(STATUS "Z3_DIR: ${Z3_DIR}") add_executable(c_example test_capi.c) target_include_directories(c_example PRIVATE ${Z3_C_INCLUDE_DIRS}) target_link_libraries(c_example PRIVATE ${Z3_LIBRARIES}) - -# FIXME: The Z3 package does not export information on the link flags -# This is needed for when libz3 is built as a static library -#if (NOT BUILD_LIBZ3_SHARED) -# z3_append_linker_flag_list_to_target(c_example ${Z3_DEPENDENT_EXTRA_C_LINK_FLAGS}) -#endif() diff --git a/contrib/cmake/examples/tptp/CMakeLists.txt b/contrib/cmake/examples/tptp/CMakeLists.txt index 6a2858a9b..6cd814487 100644 --- a/contrib/cmake/examples/tptp/CMakeLists.txt +++ b/contrib/cmake/examples/tptp/CMakeLists.txt @@ -20,9 +20,3 @@ message(STATUS "Z3_DIR: ${Z3_DIR}") add_executable(z3_tptp5 tptp5.cpp tptp5.lex.cpp) target_include_directories(z3_tptp5 PRIVATE ${Z3_CXX_INCLUDE_DIRS}) target_link_libraries(z3_tptp5 PRIVATE ${Z3_LIBRARIES}) - -# FIXME: The Z3 package does not export information on the link flags -# This is needed for when libz3 is built as a static library -#if (NOT BUILD_LIBZ3_SHARED) -# z3_append_linker_flag_list_to_target(z3_tptp5 ${Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS}) -#endif() diff --git a/contrib/cmake/src/CMakeLists.txt b/contrib/cmake/src/CMakeLists.txt index 548b59053..66e34790a 100644 --- a/contrib/cmake/src/CMakeLists.txt +++ b/contrib/cmake/src/CMakeLists.txt @@ -143,7 +143,13 @@ endif() # so that if those are also shared libraries they are referenced by `libz3.so`. target_link_libraries(libz3 PRIVATE ${Z3_DEPENDENT_LIBS}) -z3_append_linker_flag_list_to_target(libz3 ${Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS}) +# This is currently only for the OpenMP flags. It needs to be set +# via `target_link_libraries()` rather than `z3_append_linker_flag_list_to_target()` +# because when building the `libz3` as a static library when the target is exported +# the link dependencies need to be exported too. +foreach (flag_name ${Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS}) + target_link_libraries(libz3 PRIVATE ${flag_name}) +endforeach() # Declare which header file are the public header files of libz3 # these will automatically installed when the libz3 target is installed diff --git a/contrib/cmake/src/api/java/CMakeLists.txt b/contrib/cmake/src/api/java/CMakeLists.txt index b34277266..dce2bc4ea 100644 --- a/contrib/cmake/src/api/java/CMakeLists.txt +++ b/contrib/cmake/src/api/java/CMakeLists.txt @@ -44,7 +44,6 @@ target_link_libraries(z3java PRIVATE libz3) # Z3's components to build ``Native.cpp`` lets do the same for now. target_compile_options(z3java PRIVATE ${Z3_COMPONENT_CXX_FLAGS}) target_compile_definitions(z3java PRIVATE ${Z3_COMPONENT_CXX_DEFINES}) -z3_append_linker_flag_list_to_target(z3java ${Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS}) target_include_directories(z3java PRIVATE "${CMAKE_SOURCE_DIR}/src/api" "${CMAKE_BINARY_DIR}/src/api" From 28493622c25e033a2db48bb99a4fba7d12e2fb7e Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Mon, 13 Mar 2017 12:37:29 +0000 Subject: [PATCH 372/562] [CMake] On Windows when building the examples copy the Z3 library into the directory of the example executable so that it works "out of the box". --- contrib/cmake/examples/c++/CMakeLists.txt | 16 ++++++++++++++++ contrib/cmake/examples/c/CMakeLists.txt | 16 ++++++++++++++++ contrib/cmake/examples/tptp/CMakeLists.txt | 17 +++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/contrib/cmake/examples/c++/CMakeLists.txt b/contrib/cmake/examples/c++/CMakeLists.txt index c37aa475d..d60604924 100644 --- a/contrib/cmake/examples/c++/CMakeLists.txt +++ b/contrib/cmake/examples/c++/CMakeLists.txt @@ -20,3 +20,19 @@ message(STATUS "Z3_DIR: ${Z3_DIR}") add_executable(cpp_example example.cpp) target_include_directories(cpp_example PRIVATE ${Z3_CXX_INCLUDE_DIRS}) target_link_libraries(cpp_example PRIVATE ${Z3_LIBRARIES}) + +if ("${CMAKE_SYSTEM_NAME}" MATCHES "[Ww]indows") + # On Windows we need to copy the Z3 libraries + # into the same directory as the executable + # so that they can be found. + foreach (z3_lib ${Z3_LIBRARIES}) + message(STATUS "Adding copy rule for ${z3_lib}") + add_custom_command(TARGET cpp_example + POST_BUILD + COMMAND + ${CMAKE_COMMAND} -E copy_if_different + $ + $ + ) + endforeach() +endif() diff --git a/contrib/cmake/examples/c/CMakeLists.txt b/contrib/cmake/examples/c/CMakeLists.txt index 5ca985845..dd8fa6328 100644 --- a/contrib/cmake/examples/c/CMakeLists.txt +++ b/contrib/cmake/examples/c/CMakeLists.txt @@ -24,3 +24,19 @@ message(STATUS "Z3_DIR: ${Z3_DIR}") add_executable(c_example test_capi.c) target_include_directories(c_example PRIVATE ${Z3_C_INCLUDE_DIRS}) target_link_libraries(c_example PRIVATE ${Z3_LIBRARIES}) + +if ("${CMAKE_SYSTEM_NAME}" MATCHES "[Ww]indows") + # On Windows we need to copy the Z3 libraries + # into the same directory as the executable + # so that they can be found. + foreach (z3_lib ${Z3_LIBRARIES}) + message(STATUS "Adding copy rule for ${z3_lib}") + add_custom_command(TARGET c_example + POST_BUILD + COMMAND + ${CMAKE_COMMAND} -E copy_if_different + $ + $ + ) + endforeach() +endif() diff --git a/contrib/cmake/examples/tptp/CMakeLists.txt b/contrib/cmake/examples/tptp/CMakeLists.txt index 6cd814487..8e8dfb8ea 100644 --- a/contrib/cmake/examples/tptp/CMakeLists.txt +++ b/contrib/cmake/examples/tptp/CMakeLists.txt @@ -20,3 +20,20 @@ message(STATUS "Z3_DIR: ${Z3_DIR}") add_executable(z3_tptp5 tptp5.cpp tptp5.lex.cpp) target_include_directories(z3_tptp5 PRIVATE ${Z3_CXX_INCLUDE_DIRS}) target_link_libraries(z3_tptp5 PRIVATE ${Z3_LIBRARIES}) + +if ("${CMAKE_SYSTEM_NAME}" MATCHES "[Ww]indows") + # On Windows we need to copy the Z3 libraries + # into the same directory as the executable + # so that they can be found. + foreach (z3_lib ${Z3_LIBRARIES}) + message(STATUS "Adding copy rule for ${z3_lib}") + add_custom_command(TARGET z3_tptp5 + POST_BUILD + COMMAND + ${CMAKE_COMMAND} -E copy_if_different + $ + $ + ) + endforeach() +endif() + From 5c9d7538a03c0e18b1e99df863058e5218cf0022 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 13 Mar 2017 14:39:12 -0400 Subject: [PATCH 373/562] add alternate str.at semantics check in seq_rewriter this rewrites to empty string if the index is negative or beyond the length of the string, which is consistent with CVC4's semantics for this term --- src/ast/rewriter/seq_rewriter.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/ast/rewriter/seq_rewriter.cpp b/src/ast/rewriter/seq_rewriter.cpp index 26c3e23e4..adb70c30c 100644 --- a/src/ast/rewriter/seq_rewriter.cpp +++ b/src/ast/rewriter/seq_rewriter.cpp @@ -598,14 +598,25 @@ br_status seq_rewriter::mk_seq_contains(expr* a, expr* b, expr_ref& result) { return BR_FAILED; } +/* + * (str.at s i), constants s/i, i < 0 or i >= |s| ==> (str.at s i) = "" + */ br_status seq_rewriter::mk_seq_at(expr* a, expr* b, expr_ref& result) { zstring c; rational r; - if (m_util.str.is_string(a, c) && m_autil.is_numeral(b, r) && r.is_unsigned()) { - unsigned j = r.get_unsigned(); - if (j < c.length()) { - result = m_util.str.mk_string(c.extract(j, 1)); + if (m_util.str.is_string(a, c) && m_autil.is_numeral(b, r)) { + if (r.is_neg()) { + result = m_util.str.mk_string(symbol("")); return BR_DONE; + } else if (r.is_unsigned()) { + unsigned j = r.get_unsigned(); + if (j < c.length()) { + result = m_util.str.mk_string(c.extract(j, 1)); + return BR_DONE; + } else { + result = m_util.str.mk_string(symbol("")); + return BR_DONE; + } } } return BR_FAILED; From 24df976f956fc728af51d0a7f84e050576ff99e8 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 13 Mar 2017 17:03:36 -0400 Subject: [PATCH 374/562] fixup startswith/endswith to prefixof/suffixof --- src/smt/theory_str.cpp | 103 ++++++++++++++++++++--------------------- src/smt/theory_str.h | 4 +- 2 files changed, 52 insertions(+), 55 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ccfdaf8aa..0554ae2c2 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -817,12 +817,10 @@ void theory_str::propagate() { instantiate_axiom_int_to_str(e); } else if (u.str.is_at(a)) { instantiate_axiom_CharAt(e); - /* TODO NEXT: StartsWith/EndsWith -> prefixof/suffixof - } else if (is_StartsWith(e)) { - instantiate_axiom_StartsWith(e); - } else if (is_EndsWith(e)) { - instantiate_axiom_EndsWith(e); - */ + } else if (u.str.is_prefix(a)) { + instantiate_axiom_prefixof(e); + } else if (u.str.is_suffix(a)) { + instantiate_axiom_suffixof(e); } else if (u.str.is_contains(a)) { instantiate_axiom_Contains(e); } else if (u.str.is_index(a)) { @@ -1101,64 +1099,26 @@ void theory_str::instantiate_axiom_CharAt(enode * e) { assert_axiom(finalAxiom); } -void theory_str::instantiate_axiom_StartsWith(enode * e) { +void theory_str::instantiate_axiom_prefixof(enode * e) { context & ctx = get_context(); ast_manager & m = get_manager(); app * expr = e->get_owner(); if (axiomatized_terms.contains(expr)) { - TRACE("t_str_detail", tout << "already set up StartsWith axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("t_str_detail", tout << "already set up prefixof axiom for " << mk_pp(expr, m) << std::endl;); return; } axiomatized_terms.insert(expr); - TRACE("t_str_detail", tout << "instantiate StartsWith axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("t_str_detail", tout << "instantiate prefixof axiom for " << mk_pp(expr, m) << std::endl;); expr_ref ts0(mk_str_var("ts0"), m); expr_ref ts1(mk_str_var("ts1"), m); expr_ref_vector innerItems(m); - innerItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(ts0, ts1))); - innerItems.push_back(ctx.mk_eq_atom(mk_strlen(ts0), mk_strlen(expr->get_arg(1)))); - innerItems.push_back(m.mk_ite(ctx.mk_eq_atom(ts0, expr->get_arg(1)), expr, m.mk_not(expr))); - expr_ref then1(m.mk_and(innerItems.size(), innerItems.c_ptr()), m); - SASSERT(then1); - - // the top-level condition is Length(arg0) >= Length(arg1). - // of course, the integer theory is not so accommodating - expr_ref topLevelCond( - m_autil.mk_ge( - m_autil.mk_add( - mk_strlen(expr->get_arg(0)), m_autil.mk_mul(mk_int(-1), mk_strlen(expr->get_arg(1)))), - mk_int(0)) - , m); - SASSERT(topLevelCond); - - expr_ref finalAxiom(m.mk_ite(topLevelCond, then1, m.mk_not(expr)), m); - SASSERT(finalAxiom); - assert_axiom(finalAxiom); -} - -void theory_str::instantiate_axiom_EndsWith(enode * e) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - app * expr = e->get_owner(); - if (axiomatized_terms.contains(expr)) { - TRACE("t_str_detail", tout << "already set up EndsWith axiom for " << mk_pp(expr, m) << std::endl;); - return; - } - axiomatized_terms.insert(expr); - - TRACE("t_str_detail", tout << "instantiate EndsWith axiom for " << mk_pp(expr, m) << std::endl;); - - expr_ref ts0(mk_str_var("ts0"), m); - expr_ref ts1(mk_str_var("ts1"), m); - - expr_ref_vector innerItems(m); - innerItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(ts0, ts1))); - innerItems.push_back(ctx.mk_eq_atom(mk_strlen(ts1), mk_strlen(expr->get_arg(1)))); - innerItems.push_back(m.mk_ite(ctx.mk_eq_atom(ts1, expr->get_arg(1)), expr, m.mk_not(expr))); + innerItems.push_back(ctx.mk_eq_atom(expr->get_arg(1), mk_concat(ts0, ts1))); + innerItems.push_back(ctx.mk_eq_atom(mk_strlen(ts0), mk_strlen(expr->get_arg(0)))); + innerItems.push_back(m.mk_ite(ctx.mk_eq_atom(ts0, expr->get_arg(0)), expr, m.mk_not(expr))); expr_ref then1(m.mk_and(innerItems.size(), innerItems.c_ptr()), m); SASSERT(then1); @@ -1166,9 +1126,46 @@ void theory_str::instantiate_axiom_EndsWith(enode * e) { expr_ref topLevelCond( m_autil.mk_ge( m_autil.mk_add( - mk_strlen(expr->get_arg(0)), m_autil.mk_mul(mk_int(-1), mk_strlen(expr->get_arg(1)))), - mk_int(0)) - , m); + mk_strlen(expr->get_arg(1)), m_autil.mk_mul(mk_int(-1), mk_strlen(expr->get_arg(0)))), + mk_int(0)) + , m); + SASSERT(topLevelCond); + + expr_ref finalAxiom(m.mk_ite(topLevelCond, then1, m.mk_not(expr)), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); +} + +void theory_str::instantiate_axiom_suffixof(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("t_str_detail", tout << "already set up suffixof axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); + + TRACE("t_str_detail", tout << "instantiate suffixof axiom for " << mk_pp(expr, m) << std::endl;); + + expr_ref ts0(mk_str_var("ts0"), m); + expr_ref ts1(mk_str_var("ts1"), m); + + expr_ref_vector innerItems(m); + innerItems.push_back(ctx.mk_eq_atom(expr->get_arg(1), mk_concat(ts0, ts1))); + innerItems.push_back(ctx.mk_eq_atom(mk_strlen(ts1), mk_strlen(expr->get_arg(0)))); + innerItems.push_back(m.mk_ite(ctx.mk_eq_atom(ts1, expr->get_arg(0)), expr, m.mk_not(expr))); + expr_ref then1(m.mk_and(innerItems.size(), innerItems.c_ptr()), m); + SASSERT(then1); + + // the top-level condition is Length(arg0) >= Length(arg1) + expr_ref topLevelCond( + m_autil.mk_ge( + m_autil.mk_add( + mk_strlen(expr->get_arg(1)), m_autil.mk_mul(mk_int(-1), mk_strlen(expr->get_arg(0)))), + mk_int(0)) + , m); SASSERT(topLevelCond); expr_ref finalAxiom(m.mk_ite(topLevelCond, then1, m.mk_not(expr)), m); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 54fdc6538..3ea4db7d4 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -433,8 +433,8 @@ namespace smt { void instantiate_str_eq_length_axiom(enode * lhs, enode * rhs); void instantiate_axiom_CharAt(enode * e); - void instantiate_axiom_StartsWith(enode * e); - void instantiate_axiom_EndsWith(enode * e); + void instantiate_axiom_prefixof(enode * e); + void instantiate_axiom_suffixof(enode * e); void instantiate_axiom_Contains(enode * e); void instantiate_axiom_Indexof(enode * e); void instantiate_axiom_Indexof2(enode * e); From 0b1d5645097d41eec4c43946407e08d57b41ad64 Mon Sep 17 00:00:00 2001 From: hume Date: Tue, 14 Mar 2017 18:11:00 +0800 Subject: [PATCH 375/562] added no exception support to z3++.h --- src/api/c++/z3++.h | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/api/c++/z3++.h b/src/api/c++/z3++.h index bfd4eb2c4..ac0e2c84a 100644 --- a/src/api/c++/z3++.h +++ b/src/api/c++/z3++.h @@ -86,7 +86,13 @@ namespace z3 { }; inline std::ostream & operator<<(std::ostream & out, exception const & e) { out << e.msg(); return out; } - +#if !defined(Z3_THROW) +#if __cpp_exceptions || _CPPUNWIND +#define Z3_THROW(x) throw x +#else +#define Z3_THROW(x) {} +#endif +#endif // !defined(Z3_THROW) /** \brief Z3 global configuration object. @@ -165,7 +171,7 @@ namespace z3 { Z3_error_code check_error() const { Z3_error_code e = Z3_get_error_code(m_ctx); if (e != Z3_OK && enable_exceptions()) - throw exception(Z3_get_error_msg(m_ctx, e)); + Z3_THROW(exception(Z3_get_error_msg(m_ctx, e))); return e; } @@ -701,7 +707,7 @@ namespace z3 { if (!is_numeral_i(result)) { assert(ctx().enable_exceptions()); if (!ctx().enable_exceptions()) return 0; - throw exception("numeral does not fit in machine int"); + Z3_THROW(exception("numeral does not fit in machine int")); } return result; } @@ -721,7 +727,7 @@ namespace z3 { if (!is_numeral_u(result)) { assert(ctx().enable_exceptions()); if (!ctx().enable_exceptions()) return 0; - throw exception("numeral does not fit in machine uint"); + Z3_THROW(exception("numeral does not fit in machine uint")); } return result; } @@ -738,7 +744,7 @@ namespace z3 { if (!is_numeral_i64(result)) { assert(ctx().enable_exceptions()); if (!ctx().enable_exceptions()) return 0; - throw exception("numeral does not fit in machine __int64"); + Z3_THROW(exception("numeral does not fit in machine __int64")); } return result; } @@ -755,7 +761,7 @@ namespace z3 { if (!is_numeral_u64(result)) { assert(ctx().enable_exceptions()); if (!ctx().enable_exceptions()) return 0; - throw exception("numeral does not fit in machine __uint64"); + Z3_THROW(exception("numeral does not fit in machine __uint64")); } return result; } @@ -1699,7 +1705,7 @@ namespace z3 { Z3_bool status = Z3_model_eval(ctx(), m_model, n, model_completion, &r); check_error(); if (status == Z3_FALSE && ctx().enable_exceptions()) - throw exception("failed to evaluate expression"); + Z3_THROW(exception("failed to evaluate expression")); return expr(ctx(), r); } @@ -2023,7 +2029,7 @@ namespace z3 { } inline tactic par_or(unsigned n, tactic const* tactics) { if (n == 0) { - throw exception("a non-zero number of tactics need to be passed to par_or"); + Z3_THROW(exception("a non-zero number of tactics need to be passed to par_or")); } array buffer(n); for (unsigned i = 0; i < n; ++i) buffer[i] = tactics[i]; From 1dd2de61ecbe17ac9b66bd1f9df6a9dc4bb1b7c0 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Tue, 14 Mar 2017 07:43:26 -0700 Subject: [PATCH 376/562] add sum shorthand to C++. Issue #694 Signed-off-by: Nikolaj Bjorner --- src/api/c++/z3++.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/api/c++/z3++.h b/src/api/c++/z3++.h index bfd4eb2c4..8b523d91c 100644 --- a/src/api/c++/z3++.h +++ b/src/api/c++/z3++.h @@ -890,6 +890,7 @@ namespace z3 { friend expr operator+(expr const & a, expr const & b); friend expr operator+(expr const & a, int b); friend expr operator+(int a, expr const & b); + friend expr sum(expr_vector const& args); friend expr operator*(expr const & a, expr const & b); friend expr operator*(expr const & a, int b); @@ -1561,6 +1562,15 @@ namespace z3 { } + inline expr sum(expr_vector const& args) { + assert(args.size() > 0); + context& ctx = args[0].ctx(); + array _args(args); + Z3_ast r = Z3_mk_add(ctx, _args.size(), _args.ptr()); + ctx.check_error(); + return expr(ctx, r); + } + inline expr distinct(expr_vector const& args) { assert(args.size() > 0); context& ctx = args[0].ctx(); From 0668ba5f6c916e85b2efc48d4e473680f11bdf3d Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Tue, 14 Mar 2017 07:58:39 -0700 Subject: [PATCH 377/562] add pb shorthands to C++. Issue #694 Signed-off-by: Nikolaj Bjorner --- src/api/api_pb.cpp | 6 +++--- src/api/c++/z3++.h | 49 +++++++++++++++++++++++++++++++++++++++++++--- src/api/z3_api.h | 6 +++--- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/api/api_pb.cpp b/src/api/api_pb.cpp index f19fd8661..bb4a40c9a 100644 --- a/src/api/api_pb.cpp +++ b/src/api/api_pb.cpp @@ -52,7 +52,7 @@ extern "C" { } Z3_ast Z3_API Z3_mk_pble(Z3_context c, unsigned num_args, - Z3_ast const args[], int _coeffs[], + Z3_ast const args[], int const _coeffs[], int k) { Z3_TRY; LOG_Z3_mk_pble(c, num_args, args, _coeffs, k); @@ -70,7 +70,7 @@ extern "C" { } Z3_ast Z3_API Z3_mk_pbge(Z3_context c, unsigned num_args, - Z3_ast const args[], int _coeffs[], + Z3_ast const args[], int const _coeffs[], int k) { Z3_TRY; LOG_Z3_mk_pble(c, num_args, args, _coeffs, k); @@ -88,7 +88,7 @@ extern "C" { } Z3_ast Z3_API Z3_mk_pbeq(Z3_context c, unsigned num_args, - Z3_ast const args[], int _coeffs[], + Z3_ast const args[], int const _coeffs[], int k) { Z3_TRY; LOG_Z3_mk_pble(c, num_args, args, _coeffs, k); diff --git a/src/api/c++/z3++.h b/src/api/c++/z3++.h index 6250f324b..b6157f3ff 100644 --- a/src/api/c++/z3++.h +++ b/src/api/c++/z3++.h @@ -935,7 +935,6 @@ namespace z3 { friend expr operator>=(expr const & a, expr const & b); - friend expr wasoperator(expr const & a, expr const & b); friend expr operator>=(expr const & a, int b); friend expr operator>=(int a, expr const & b); @@ -947,6 +946,12 @@ namespace z3 { friend expr operator>(expr const & a, int b); friend expr operator>(int a, expr const & b); + friend expr pble(expr_vector const& es, int const * coeffs, int bound); + friend expr pbge(expr_vector const& es, int const * coeffs, int bound); + friend expr pbeq(expr_vector const& es, int const * coeffs, int bound); + friend expr atmost(expr_vector const& es, unsigned bound); + friend expr atleast(expr_vector const& es, unsigned bound); + friend expr operator&(expr const & a, expr const & b); friend expr operator&(expr const & a, int b); friend expr operator&(int a, expr const & b); @@ -1566,8 +1571,46 @@ namespace z3 { array vars(xs); Z3_ast r = Z3_mk_exists_const(b.ctx(), 0, vars.size(), vars.ptr(), 0, 0, b); b.check_error(); return expr(b.ctx(), r); } - - + inline expr pble(expr_vector const& es, int const* coeffs, int bound) { + assert(es.size() > 0); + context& ctx = es[0].ctx(); + array _es(es); + Z3_ast r = Z3_mk_pble(ctx, _es.size(), _es.ptr(), coeffs, bound); + ctx.check_error(); + return expr(ctx, r); + } + inline expr pbge(expr_vector const& es, int const* coeffs, int bound) { + assert(es.size() > 0); + context& ctx = es[0].ctx(); + array _es(es); + Z3_ast r = Z3_mk_pbge(ctx, _es.size(), _es.ptr(), coeffs, bound); + ctx.check_error(); + return expr(ctx, r); + } + inline expr pbeq(expr_vector const& es, int const* coeffs, int bound) { + assert(es.size() > 0); + context& ctx = es[0].ctx(); + array _es(es); + Z3_ast r = Z3_mk_pbeq(ctx, _es.size(), _es.ptr(), coeffs, bound); + ctx.check_error(); + return expr(ctx, r); + } + inline expr atmost(expr_vector const& es, unsigned bound) { + assert(es.size() > 0); + context& ctx = es[0].ctx(); + array _es(es); + Z3_ast r = Z3_mk_atmost(ctx, _es.size(), _es.ptr(), bound); + ctx.check_error(); + return expr(ctx, r); + } + inline expr atleast(expr_vector const& es, unsigned bound) { + assert(es.size() > 0); + context& ctx = es[0].ctx(); + array _es(es); + Z3_ast r = Z3_mk_atleast(ctx, _es.size(), _es.ptr(), bound); + ctx.check_error(); + return expr(ctx, r); + } inline expr sum(expr_vector const& args) { assert(args.size() > 0); context& ctx = args[0].ctx(); diff --git a/src/api/z3_api.h b/src/api/z3_api.h index ee35c002e..272c94dda 100644 --- a/src/api/z3_api.h +++ b/src/api/z3_api.h @@ -4006,7 +4006,7 @@ extern "C" { def_API('Z3_mk_pble', AST, (_in(CONTEXT), _in(UINT), _in_array(1,AST), _in_array(1,INT), _in(INT))) */ Z3_ast Z3_API Z3_mk_pble(Z3_context c, unsigned num_args, - Z3_ast const args[], int coeffs[], + Z3_ast const args[], int const coeffs[], int k); /** @@ -4017,7 +4017,7 @@ extern "C" { def_API('Z3_mk_pbge', AST, (_in(CONTEXT), _in(UINT), _in_array(1,AST), _in_array(1,INT), _in(INT))) */ Z3_ast Z3_API Z3_mk_pbge(Z3_context c, unsigned num_args, - Z3_ast const args[], int coeffs[], + Z3_ast const args[], int const coeffs[], int k); /** @@ -4028,7 +4028,7 @@ extern "C" { def_API('Z3_mk_pbeq', AST, (_in(CONTEXT), _in(UINT), _in_array(1,AST), _in_array(1,INT), _in(INT))) */ Z3_ast Z3_API Z3_mk_pbeq(Z3_context c, unsigned num_args, - Z3_ast const args[], int coeffs[], + Z3_ast const args[], int const coeffs[], int k); /** From 34717a7b6e3048b1c0b6730361ac35594d603aaa Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 14 Mar 2017 14:14:46 -0400 Subject: [PATCH 378/562] str.extract semantics fix --- src/ast/rewriter/seq_rewriter.cpp | 35 ++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/ast/rewriter/seq_rewriter.cpp b/src/ast/rewriter/seq_rewriter.cpp index 26c3e23e4..4f99c6ae6 100644 --- a/src/ast/rewriter/seq_rewriter.cpp +++ b/src/ast/rewriter/seq_rewriter.cpp @@ -509,15 +509,40 @@ br_status seq_rewriter::mk_seq_length(expr* a, expr_ref& result) { br_status seq_rewriter::mk_seq_extract(expr* a, expr* b, expr* c, expr_ref& result) { zstring s; rational pos, len; - if (m_util.str.is_string(a, s) && m_autil.is_numeral(b, pos) && m_autil.is_numeral(c, len) && - pos.is_unsigned() && len.is_unsigned() && pos.get_unsigned() + len.get_unsigned() <= s.length()) { - unsigned _pos = pos.get_unsigned(); - unsigned _len = len.get_unsigned(); - result = m_util.str.mk_string(s.extract(_pos, _len)); + + bool constantBase = m_util.str.is_string(a, s); + bool constantPos = m_autil.is_numeral(b, pos); + bool constantLen = m_autil.is_numeral(c, len); + + // case 1: pos<0 or len<0 + // rewrite to "" + if ( (constantPos && pos.is_neg()) || (constantLen && len.is_neg()) ) { + result = m_util.str.mk_string(symbol("")); return BR_DONE; } + // case 1.1: pos >= length(base) + // rewrite to "" + if (constantBase && constantPos && pos.get_unsigned() >= s.length()) { + result = m_util.str.mk_string(symbol("")); + return BR_DONE; + } + + if (constantBase && constantPos && constantLen) { + if (pos.get_unsigned() + len.get_unsigned() >= s.length()) { + // case 2: pos+len goes past the end of the string + unsigned _len = s.length() - pos.get_unsigned() + 1; + result = m_util.str.mk_string(s.extract(pos.get_unsigned(), _len)); + return BR_DONE; + } else { + // case 3: pos+len still within string + result = m_util.str.mk_string(s.extract(pos.get_unsigned(), len.get_unsigned())); + return BR_DONE; + } + } + return BR_FAILED; } + br_status seq_rewriter::mk_seq_contains(expr* a, expr* b, expr_ref& result) { zstring c, d; if (m_util.str.is_string(a, c) && m_util.str.is_string(b, d)) { From 05c267b8d8ad4cf3c00e104453f2049ecd665a72 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Tue, 14 Mar 2017 15:37:47 -0700 Subject: [PATCH 379/562] make seq.at operations generic Signed-off-by: Nikolaj Bjorner --- src/ast/rewriter/seq_rewriter.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/ast/rewriter/seq_rewriter.cpp b/src/ast/rewriter/seq_rewriter.cpp index adb70c30c..26918261e 100644 --- a/src/ast/rewriter/seq_rewriter.cpp +++ b/src/ast/rewriter/seq_rewriter.cpp @@ -604,20 +604,21 @@ br_status seq_rewriter::mk_seq_contains(expr* a, expr* b, expr_ref& result) { br_status seq_rewriter::mk_seq_at(expr* a, expr* b, expr_ref& result) { zstring c; rational r; - if (m_util.str.is_string(a, c) && m_autil.is_numeral(b, r)) { + if (m_autil.is_numeral(b, r)) { if (r.is_neg()) { - result = m_util.str.mk_string(symbol("")); + result = m_util.str.mk_empty(m().get_sort(a)); + return BR_DONE; + } + unsigned len = 0; + bool bounded = min_length(1, &a, len); + if (bounded && r >= rational(len)) { + result = m_util.str.mk_empty(m().get_sort(a)); return BR_DONE; - } else if (r.is_unsigned()) { - unsigned j = r.get_unsigned(); - if (j < c.length()) { - result = m_util.str.mk_string(c.extract(j, 1)); - return BR_DONE; - } else { - result = m_util.str.mk_string(symbol("")); - return BR_DONE; - } } + if (m_util.str.is_string(a, c) && r.is_unsigned() && r < rational(c.length())) { + result = m_util.str.mk_string(c.extract(r.get_unsigned(), 1)); + return BR_DONE; + } } return BR_FAILED; } From 72651e2e98b38a8eec25acffa41f1b5470a76fa8 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Tue, 14 Mar 2017 19:35:11 -0700 Subject: [PATCH 380/562] fixing sources for double frees of clauses. #940 Signed-off-by: Nikolaj Bjorner --- src/sat/sat_asymm_branch.cpp | 2 +- src/sat/sat_simplifier.cpp | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/sat/sat_asymm_branch.cpp b/src/sat/sat_asymm_branch.cpp index 8a761ea3a..782713d5c 100644 --- a/src/sat/sat_asymm_branch.cpp +++ b/src/sat/sat_asymm_branch.cpp @@ -188,8 +188,8 @@ namespace sat { case 1: TRACE("asymm_branch", tout << "produced unit clause: " << c[0] << "\n";); s.assign(c[0], justification()); - s.del_clause(c); s.propagate_core(false); + s.del_clause(c); SASSERT(s.inconsistent() || s.m_qhead == s.m_trail.size()); return false; // check_missed_propagation() may fail, since m_clauses is not in a consistent state. case 2: diff --git a/src/sat/sat_simplifier.cpp b/src/sat/sat_simplifier.cpp index bd975115b..e744bc007 100644 --- a/src/sat/sat_simplifier.cpp +++ b/src/sat/sat_simplifier.cpp @@ -279,7 +279,10 @@ namespace sat { unsigned sz = c.size(); if (sz == 0) { s.set_conflict(justification()); - return; + for (; it != end; ++it, ++it2) { + *it2 = *it; + } + break; } if (sz == 1) { s.assign(c[0], justification()); From 8021d63539b697476f510dbd79abeaeca06b9a7e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 15 Mar 2017 15:25:48 -0400 Subject: [PATCH 381/562] remove legacy str_decl_plugin and str_rewriter classes; these have been unified with sequence-compatible equivalents --- src/ast/ast_smt2_pp.h | 1 - src/ast/ast_smt_pp.cpp | 1 - src/ast/rewriter/str_rewriter.cpp | 703 ------------------------------ src/ast/rewriter/str_rewriter.h | 120 ----- src/ast/str_decl_plugin.cpp | 501 --------------------- src/ast/str_decl_plugin.h | 218 --------- 6 files changed, 1544 deletions(-) delete mode 100644 src/ast/rewriter/str_rewriter.cpp delete mode 100644 src/ast/rewriter/str_rewriter.h delete mode 100644 src/ast/str_decl_plugin.cpp delete mode 100644 src/ast/str_decl_plugin.h diff --git a/src/ast/ast_smt2_pp.h b/src/ast/ast_smt2_pp.h index 2f79ebaec..244594461 100644 --- a/src/ast/ast_smt2_pp.h +++ b/src/ast/ast_smt2_pp.h @@ -30,7 +30,6 @@ Revision History: #include"fpa_decl_plugin.h" #include"dl_decl_plugin.h" #include"seq_decl_plugin.h" -#include"str_decl_plugin.h" #include"smt2_util.h" class smt2_pp_environment { diff --git a/src/ast/ast_smt_pp.cpp b/src/ast/ast_smt_pp.cpp index de6ae6cc3..c3f1523b1 100644 --- a/src/ast/ast_smt_pp.cpp +++ b/src/ast/ast_smt_pp.cpp @@ -24,7 +24,6 @@ Revision History: #include"ast_smt_pp.h" #include"arith_decl_plugin.h" #include"bv_decl_plugin.h" -#include"str_decl_plugin.h" #include"array_decl_plugin.h" #include"datatype_decl_plugin.h" #include"fpa_decl_plugin.h" diff --git a/src/ast/rewriter/str_rewriter.cpp b/src/ast/rewriter/str_rewriter.cpp deleted file mode 100644 index 3933e7fdb..000000000 --- a/src/ast/rewriter/str_rewriter.cpp +++ /dev/null @@ -1,703 +0,0 @@ -/*++ -Copyright (c) 2016 Microsoft Corporation - -Module Name: - - str_rewriter.cpp - -Abstract: - - AST rewriting rules for string terms. - -Author: - - Murphy Berzish - -Notes: - ---*/ - -#if 0 - -#include"str_rewriter.h" -#include"arith_decl_plugin.h" -#include"ast_pp.h" -#include"ast_util.h" -#include"well_sorted.h" -#include -#include -#include -#include - -// Convert a regular expression to an e-NFA using Thompson's construction -void nfa::convert_re(expr * e, unsigned & start, unsigned & end, str_util & m_strutil) { - start = next_id(); - end = next_id(); - if (m_strutil.is_re_Str2Reg(e)) { - app * a = to_app(e); - expr * arg_str = a->get_arg(0); - if (m_strutil.is_string(arg_str)) { - std::string str = m_strutil.get_string_constant_value(arg_str); - TRACE("t_str_rw", tout << "build NFA for '" << str << "'" << std::endl;); - - /* - * For an n-character string, we make (n-1) intermediate states, - * labelled i_(0) through i_(n-2). - * Then we construct the following transitions: - * start --str[0]--> i_(0) --str[1]--> i_(1) --...--> i_(n-2) --str[n-1]--> final - */ - unsigned last = start; - for (int i = 0; i <= ((int)str.length()) - 2; ++i) { - unsigned i_state = next_id(); - make_transition(last, str.at(i), i_state); - TRACE("t_str_rw", tout << "string transition " << last << "--" << str.at(i) << "--> " << i_state << std::endl;); - last = i_state; - } - make_transition(last, str.at(str.length() - 1), end); - TRACE("t_str_rw", tout << "string transition " << last << "--" << str.at(str.length() - 1) << "--> " << end << std::endl;); - TRACE("t_str_rw", tout << "string NFA: start = " << start << ", end = " << end << std::endl;); - } else { - TRACE("t_str_rw", tout << "invalid string constant in Str2Reg" << std::endl;); - m_valid = false; - return; - } - } else if (m_strutil.is_re_RegexConcat(e)){ - app * a = to_app(e); - expr * re1 = a->get_arg(0); - expr * re2 = a->get_arg(1); - unsigned start1, end1; - convert_re(re1, start1, end1, m_strutil); - unsigned start2, end2; - convert_re(re2, start2, end2, m_strutil); - // start --e--> start1 --...--> end1 --e--> start2 --...--> end2 --e--> end - make_epsilon_move(start, start1); - make_epsilon_move(end1, start2); - make_epsilon_move(end2, end); - TRACE("t_str_rw", tout << "concat NFA: start = " << start << ", end = " << end << std::endl;); - } else if (m_strutil.is_re_RegexUnion(e)) { - app * a = to_app(e); - expr * re1 = a->get_arg(0); - expr * re2 = a->get_arg(1); - unsigned start1, end1; - convert_re(re1, start1, end1, m_strutil); - unsigned start2, end2; - convert_re(re2, start2, end2, m_strutil); - - // start --e--> start1 ; start --e--> start2 - // end1 --e--> end ; end2 --e--> end - make_epsilon_move(start, start1); - make_epsilon_move(start, start2); - make_epsilon_move(end1, end); - make_epsilon_move(end2, end); - TRACE("t_str_rw", tout << "union NFA: start = " << start << ", end = " << end << std::endl;); - } else if (m_strutil.is_re_RegexStar(e)) { - app * a = to_app(e); - expr * subex = a->get_arg(0); - unsigned start_subex, end_subex; - convert_re(subex, start_subex, end_subex, m_strutil); - // start --e--> start_subex, start --e--> end - // end_subex --e--> start_subex, end_subex --e--> end - make_epsilon_move(start, start_subex); - make_epsilon_move(start, end); - make_epsilon_move(end_subex, start_subex); - make_epsilon_move(end_subex, end); - TRACE("t_str_rw", tout << "star NFA: start = " << start << ", end = " << end << std::endl;); - } else { - TRACE("t_str_rw", tout << "invalid regular expression" << std::endl;); - m_valid = false; - return; - } -} - -void nfa::epsilon_closure(unsigned start, std::set & closure) { - std::deque worklist; - closure.insert(start); - worklist.push_back(start); - - while(!worklist.empty()) { - unsigned state = worklist.front(); - worklist.pop_front(); - if (epsilon_map.find(state) != epsilon_map.end()) { - for (std::set::iterator it = epsilon_map[state].begin(); - it != epsilon_map[state].end(); ++it) { - unsigned new_state = *it; - if (closure.find(new_state) == closure.end()) { - closure.insert(new_state); - worklist.push_back(new_state); - } - } - } - } -} - -bool nfa::matches(std::string input) { - /* - * Keep a set of all states the NFA can currently be in. - * Initially this is the e-closure of m_start_state - * For each character A in the input string, - * the set of next states contains - * all states in transition_map[S][A] for each S in current_states, - * and all states in epsilon_map[S] for each S in current_states. - * After consuming the entire input string, - * the match is successful iff current_states contains m_end_state. - */ - std::set current_states; - epsilon_closure(m_start_state, current_states); - for (unsigned i = 0; i < input.length(); ++i) { - char A = input.at(i); - std::set next_states; - for (std::set::iterator it = current_states.begin(); - it != current_states.end(); ++it) { - unsigned S = *it; - // check transition_map - if (transition_map[S].find(A) != transition_map[S].end()) { - next_states.insert(transition_map[S][A]); - } - } - - // take e-closure over next_states to compute the actual next_states - std::set epsilon_next_states; - for (std::set::iterator it = next_states.begin(); it != next_states.end(); ++it) { - unsigned S = *it; - std::set closure; - epsilon_closure(S, closure); - epsilon_next_states.insert(closure.begin(), closure.end()); - } - current_states = epsilon_next_states; - } - if (current_states.find(m_end_state) != current_states.end()) { - return true; - } else { - return false; - } -} - - -br_status str_rewriter::mk_str_Concat(expr * arg0, expr * arg1, expr_ref & result) { - TRACE("t_str_rw", tout << "rewrite (Concat " << mk_pp(arg0, m()) << " " << mk_pp(arg1, m()) << ")" << std::endl;); - if(m_strutil.is_string(arg0) && m_strutil.is_string(arg1)) { - TRACE("t_str_rw", tout << "evaluating concat of two constant strings" << std::endl;); - std::string arg0Str = m_strutil.get_string_constant_value(arg0); - std::string arg1Str = m_strutil.get_string_constant_value(arg1); - std::string resultStr = arg0Str + arg1Str; - result = m_strutil.mk_string(resultStr); - return BR_DONE; - } else { - return BR_FAILED; - } -} - -br_status str_rewriter::mk_str_Length(expr * arg0, expr_ref & result) { - TRACE("t_str_rw", tout << "rewrite (Length " << mk_pp(arg0, m()) << ")" << std::endl;); - if (m_strutil.is_string(arg0)) { - TRACE("t_str_rw", tout << "evaluating length of constant string" << std::endl;); - std::string arg0Str = m_strutil.get_string_constant_value(arg0); - rational arg0Len((unsigned)arg0Str.length()); - result = m_autil.mk_numeral(arg0Len, true); - TRACE("t_str_rw", tout << "result is " << mk_pp(result, m()) << std::endl;); - return BR_DONE; - } else { - return BR_FAILED; - } -} - -br_status str_rewriter::mk_str_CharAt(expr * arg0, expr * arg1, expr_ref & result) { - TRACE("t_str_rw", tout << "rewrite (CharAt " << mk_pp(arg0, m()) << " " << mk_pp(arg1, m()) << ")" << std::endl;); - // if arg0 is a string constant and arg1 is an integer constant, - // we can rewrite this by evaluating the expression - rational arg1Int; - if (m_strutil.is_string(arg0) && m_autil.is_numeral(arg1, arg1Int)) { - TRACE("t_str_rw", tout << "evaluating constant CharAt expression" << std::endl;); - std::string arg0Str = m_strutil.get_string_constant_value(arg0); - std::string resultStr; - if (arg1Int >= rational(0) && arg1Int <= rational((unsigned)arg0Str.length())) { - resultStr = arg0Str.at(arg1Int.get_unsigned()); - TRACE("t_str_rw", tout << "result is '" << resultStr << "'" << std::endl;); - } else { - resultStr = ""; - TRACE("t_str_rw", tout << "bogus length argument, result is empty string" << std::endl;); - } - result = m_strutil.mk_string(resultStr); - return BR_DONE; - } else { - return BR_FAILED; - } -} - -br_status str_rewriter::mk_str_StartsWith(expr * haystack, expr * needle, expr_ref & result) { - TRACE("t_str_rw", tout << "rewrite (StartsWith " << mk_pp(haystack, m()) << " " << mk_pp(needle, m()) << ")" << std::endl;); - if (m_strutil.is_string(haystack) && m_strutil.is_string(needle)) { - TRACE("t_str_rw", tout << "evaluating constant StartsWith predicate" << std::endl;); - std::string haystackStr = m_strutil.get_string_constant_value(haystack); - std::string needleStr = m_strutil.get_string_constant_value(needle); - if (haystackStr.length() < needleStr.length()) { - result = m().mk_false(); - return BR_DONE; - } else { - if (haystackStr.substr(0, needleStr.length()) == needleStr) { - result = m().mk_true(); - } else { - result = m().mk_false(); - } - return BR_DONE; - } - } else { - return BR_FAILED; - } -} - -br_status str_rewriter::mk_str_EndsWith(expr * haystack, expr * needle, expr_ref & result) { - TRACE("t_str_rw", tout << "rewrite (EndsWith " << mk_pp(haystack, m()) << " " << mk_pp(needle, m()) << ")" << std::endl;); - if (m_strutil.is_string(haystack) && m_strutil.is_string(needle)) { - TRACE("t_str_rw", tout << "evaluating constant EndsWith predicate" << std::endl;); - std::string haystackStr = m_strutil.get_string_constant_value(haystack); - std::string needleStr = m_strutil.get_string_constant_value(needle); - if (haystackStr.length() < needleStr.length()) { - result = m().mk_false(); - return BR_DONE; - } else { - if (haystackStr.substr(haystackStr.length() - needleStr.length(), needleStr.length()) == needleStr) { - result = m().mk_true(); - } else { - result = m().mk_false(); - } - return BR_DONE; - } - } else { - return BR_FAILED; - } -} - -br_status str_rewriter::mk_str_Contains(expr * haystack, expr * needle, expr_ref & result) { - TRACE("t_str_rw", tout << "rewrite (Contains " << mk_pp(haystack, m()) << " " << mk_pp(needle, m()) << ")" << std::endl;); - if (haystack == needle) { - TRACE("t_str_rw", tout << "eliminate (Contains) over identical terms" << std::endl;); - result = m().mk_true(); - return BR_DONE; - } else if (m_strutil.is_string(haystack) && m_strutil.is_string(needle)) { - TRACE("t_str_rw", tout << "evaluating constant Contains predicate" << std::endl;); - std::string haystackStr = m_strutil.get_string_constant_value(haystack); - std::string needleStr = m_strutil.get_string_constant_value(needle); - if (haystackStr.find(needleStr) != std::string::npos) { - result = m().mk_true(); - } else { - result = m().mk_false(); - } - return BR_DONE; - } else { - return BR_FAILED; - } -} - -br_status str_rewriter::mk_str_Indexof(expr * haystack, expr * needle, expr_ref & result) { - TRACE("t_str_rw", tout << "rewrite (Indexof " << mk_pp(haystack, m()) << " " << mk_pp(needle, m()) << ")" << std::endl;); - if (m_strutil.is_string(haystack) && m_strutil.is_string(needle)) { - TRACE("t_str_rw", tout << "evaluating constant Indexof expression" << std::endl;); - std::string haystackStr = m_strutil.get_string_constant_value(haystack); - std::string needleStr = m_strutil.get_string_constant_value(needle); - if (haystackStr.find(needleStr) != std::string::npos) { - int index = haystackStr.find(needleStr); - result = m_autil.mk_numeral(rational(index), true); - } else { - result = m_autil.mk_numeral(rational(-1), true); - } - return BR_DONE; - } else { - return BR_FAILED; - } -} - -br_status str_rewriter::mk_str_Indexof2(expr * arg0, expr * arg1, expr * arg2, expr_ref & result) { - TRACE("t_str_rw", tout << "rewrite (Indexof2 " << mk_pp(arg0, m()) << " " << mk_pp(arg1, m()) << " " << mk_pp(arg2, m()) << ")" << std::endl;); - //if (getNodeType(t, args[0]) == my_Z3_ConstStr && getNodeType(t, args[1]) == my_Z3_ConstStr && getNodeType(t, args[2]) == my_Z3_Num) { - rational arg2Int; - if (m_strutil.is_string(arg0) && m_strutil.is_string(arg1) && m_autil.is_numeral(arg2, arg2Int)) { - TRACE("t_str_rw", tout << "evaluating constant Indexof2 expression" << std::endl;); - std::string arg0str = m_strutil.get_string_constant_value(arg0); - std::string arg1str = m_strutil.get_string_constant_value(arg1); - if (arg2Int >= rational((unsigned)arg0str.length())) { - result = m_autil.mk_numeral(rational(-1), true); - } else if (arg2Int < rational(0)) { - int index = arg0str.find(arg1str); - result = m_autil.mk_numeral(rational(index), true); - } else { - std::string suffixStr = arg0str.substr(arg2Int.get_unsigned(), arg0str.length() - arg2Int.get_unsigned()); - if (suffixStr.find(arg1str) != std::string::npos) { - int index = suffixStr.find(arg1str) + arg2Int.get_unsigned(); - result = m_autil.mk_numeral(rational(index), true); - } else { - result = m_autil.mk_numeral(rational(-1), true); - } - } - return BR_DONE; - } else { - return BR_FAILED; - } -} - -br_status str_rewriter::mk_str_LastIndexof(expr * haystack, expr * needle, expr_ref & result) { - TRACE("t_str_rw", tout << "rewrite (LastIndexof " << mk_pp(haystack, m()) << " " << mk_pp(needle, m()) << ")" << std::endl;); - if (m_strutil.is_string(haystack) && m_strutil.is_string(needle)) { - TRACE("t_str_rw", tout << "evaluating constant LastIndexof expression" << std::endl;); - std::string arg0Str = m_strutil.get_string_constant_value(haystack); - std::string arg1Str = m_strutil.get_string_constant_value(needle); - if (arg0Str.rfind(arg1Str) != std::string::npos) { - int index = arg0Str.rfind(arg1Str); - result = m_autil.mk_numeral(rational(index), true); - } else { - result = m_autil.mk_numeral(rational(-1), true); - } - return BR_DONE; - } else { - return BR_FAILED; - } -} - -br_status str_rewriter::mk_str_Replace(expr * base, expr * source, expr * target, expr_ref & result) { - TRACE("t_str_rw", tout << "rewrite (Replace " << mk_pp(base, m()) << " " << mk_pp(source, m()) << " " << mk_pp(target, m()) << ")" << std::endl;); - if (m_strutil.is_string(base) && m_strutil.is_string(source) && m_strutil.is_string(target)) { - std::string arg0Str = m_strutil.get_string_constant_value(base); - std::string arg1Str = m_strutil.get_string_constant_value(source); - std::string arg2Str = m_strutil.get_string_constant_value(target); - if (arg0Str.find(arg1Str) != std::string::npos) { - int index1 = arg0Str.find(arg1Str); - int index2 = index1 + arg1Str.length(); - std::string substr0 = arg0Str.substr(0, index1); - std::string substr2 = arg0Str.substr(index2); - std::string replaced = substr0 + arg2Str + substr2; - result = m_strutil.mk_string(replaced); - } else { - result = base; - } - return BR_DONE; - } else { - return BR_FAILED; - } -} - -br_status str_rewriter::mk_str_prefixof(expr * pre, expr * full, expr_ref & result) { - TRACE("t_str_rw", tout << "rewrite (str.prefixof " << mk_pp(pre, m()) << " " << mk_pp(full, m()) << ")" << std::endl;); - result = m_strutil.mk_str_StartsWith(full, pre); - return BR_REWRITE_FULL; -} - -br_status str_rewriter::mk_str_suffixof(expr * post, expr * full, expr_ref & result) { - TRACE("t_str_rw", tout << "rewrite (str.suffixof" << mk_pp(post, m()) << " " << mk_pp(full, m()) << ")" << std::endl;); - result = m_strutil.mk_str_EndsWith(full, post); - return BR_REWRITE_FULL; -} - -br_status str_rewriter::mk_str_to_int(expr * arg0, expr_ref & result) { - TRACE("t_str_rw", tout << "rewrite (str.to-int " << mk_pp(arg0, m()) << ")" << std::endl;); - - if (m_strutil.is_string(arg0)) { - std::string str = m_strutil.get_string_constant_value(arg0); - if (str.length() == 0) { - result = m_autil.mk_numeral(rational::zero(), true); - return BR_DONE; - } - - // interpret str as a natural number and rewrite to the corresponding integer. - // if this is not valid, rewrite to -1 - rational convertedRepresentation(0); - rational ten(10); - for (unsigned i = 0; i < str.length(); ++i) { - char digit = str.at(i); - if (isdigit((int)digit)) { - std::string sDigit(1, digit); - int val = atoi(sDigit.c_str()); - convertedRepresentation = (ten * convertedRepresentation) + rational(val); - } else { - // not a digit, invalid - TRACE("t_str_rw", tout << "str.to-int argument contains non-digit character '" << digit << "'" << std::endl;); - convertedRepresentation = rational::minus_one(); - break; - } - } - result = m_autil.mk_numeral(convertedRepresentation, true); - return BR_DONE; - } - return BR_FAILED; - -} - -br_status str_rewriter::mk_str_from_int(expr * arg0, expr_ref & result) { - TRACE("t_str_rw", tout << "rewrite (str.from-int " << mk_pp(arg0, m()) << ")" << std::endl;); - rational arg0Int; - if (m_autil.is_numeral(arg0, arg0Int)) { - // (str.from-int N) with N non-negative is the corresponding string in decimal notation. - // otherwise it is the empty string - if (arg0Int.is_nonneg()) { - std::string str = arg0Int.to_string(); - result = m_strutil.mk_string(str); - TRACE("t_str_rw", tout << "convert non-negative integer constant to " << str << std::endl;); - } else { - result = m_strutil.mk_string(""); - TRACE("t_str_rw", tout << "convert invalid integer constant to empty string" << std::endl;); - } - return BR_DONE; - } - return BR_FAILED; -} - -br_status str_rewriter::mk_str_Substr(expr * base, expr * start, expr * len, expr_ref & result) { - TRACE("t_str_rw", tout << "rewrite (Substr " << mk_pp(base, m()) << " " << mk_pp(start, m()) << " " << mk_pp(len, m()) << ")" << std::endl;); - - bool constant_base = m_strutil.is_string(base); - std::string baseStr; - if (constant_base) { - baseStr = m_strutil.get_string_constant_value(base); - } - rational startVal; - bool constant_start = m_autil.is_numeral(start, startVal); - rational lenVal; - bool constant_len = m_autil.is_numeral(len, lenVal); - - // case 1: start < 0 or len < 0 - if ( (constant_start && startVal.is_neg()) || (constant_len && lenVal.is_neg()) ) { - TRACE("t_str_rw", tout << "start/len of substr is negative" << std::endl;); - result = m_strutil.mk_string(""); - return BR_DONE; - } - // case 1.1: start >= length(base) - if (constant_start && constant_base) { - rational baseStrlen((unsigned int)baseStr.length()); - if (startVal >= baseStrlen) { - TRACE("t_str_rw", tout << "start >= strlen for substr" << std::endl;); - result = m_strutil.mk_string(""); - return BR_DONE; - } - } - - if (constant_base && constant_start && constant_len) { - rational baseStrlen((unsigned int)baseStr.length()); - std::string retval; - if (startVal + lenVal >= baseStrlen) { - // case 2: pos+len goes past the end of the string - retval = baseStr.substr(startVal.get_unsigned(), std::string::npos); - } else { - // case 3: pos+len still within string - retval = baseStr.substr(startVal.get_unsigned(), lenVal.get_unsigned()); - } - result = m_strutil.mk_string(retval); - return BR_DONE; - } - - return BR_FAILED; -} - -br_status str_rewriter::mk_re_Str2Reg(expr * str, expr_ref & result) { - // the argument to Str2Reg *must* be a string constant - ENSURE(m_strutil.is_string(str)); - return BR_FAILED; -} - -br_status str_rewriter::mk_re_RegexIn(expr * str, expr * re, expr_ref & result) { - // fast path: - // (RegexIn E (Str2Reg S)) --> (= E S) - if (m_strutil.is_re_Str2Reg(re)) { - expr * regexStr = to_app(re)->get_arg(0); - ENSURE(m_strutil.is_string(regexStr)); - result = m().mk_eq(str, regexStr); - TRACE("t_str_rw", tout << "RegexIn fast path: " << mk_pp(str, m()) << " in " << mk_pp(re, m()) << " ==> " << mk_pp(result, m()) << std::endl;); - return BR_REWRITE_FULL; - } - - // necessary for model validation - if (m_strutil.is_string(str)) { - TRACE("t_str_rw", tout << "RegexIn with constant string argument" << std::endl;); - nfa regex_nfa(m_strutil, re); - ENSURE(regex_nfa.is_valid()); - std::string input = m_strutil.get_string_constant_value(str); - if (regex_nfa.matches(input)) { - result = m().mk_true(); - } else { - result = m().mk_false(); - } - return BR_DONE; - } - - return BR_FAILED; -} - -br_status str_rewriter::mk_re_RegexStar(expr * re, expr_ref & result) { - if (m_strutil.is_re_RegexStar(re)) { - result = re; - return BR_REWRITE_FULL; - } else { - return BR_FAILED; - } -} - -br_status str_rewriter::mk_re_RegexConcat(expr * r0, expr * r1, expr_ref & result) { - TRACE("t_str_rw", tout << "rewrite (RegexConcat " << mk_pp(r0, m()) << " " << mk_pp(r1, m()) << ")" << std::endl;); - // (RegexConcat (Str2Reg "A") (Str2Reg "B")) --> (Str2Reg "AB") - if (m_strutil.is_re_Str2Reg(r0) && m_strutil.is_re_Str2Reg(r1)) { - expr * r0str = to_app(r0)->get_arg(0); - expr * r1str = to_app(r1)->get_arg(0); - ENSURE(m_strutil.is_string(r0str)); - ENSURE(m_strutil.is_string(r1str)); - std::string r0val = m_strutil.get_string_constant_value(r0str); - std::string r1val = m_strutil.get_string_constant_value(r1str); - std::string simplifyVal = r0val + r1val; - TRACE("t_str_rw", tout << "RegexConcat fast path: both sides are Str2Reg, simplify to (Str2Reg \"" << simplifyVal << "\")" << std::endl;); - result = m_strutil.mk_re_Str2Reg(simplifyVal); - return BR_DONE; - } - - return BR_FAILED; -} - -br_status str_rewriter::mk_re_RegexPlus(expr * re, expr_ref & result) { - /* - * Two optimizations are possible if we inspect 're'. - * If 're' is (RegexPlus X), then reduce to 're'. - * If 're' is (RegexStar X), then reduce to 're'. - * Otherwise, reduce to (RegexConcat re (RegexStar re)). - */ - - if (m_strutil.is_re_RegexPlus(re)) { - result = re; - return BR_REWRITE_FULL; - } else if (m_strutil.is_re_RegexStar(re)) { - // Z3str2 re-created the AST under 're' here, but I don't think we need to do that - result = re; - return BR_REWRITE_FULL; - } else { - result = m_strutil.mk_re_RegexConcat(re, m_strutil.mk_re_RegexStar(re)); - return BR_REWRITE_FULL; - } -} - -br_status str_rewriter::mk_re_RegexCharRange(expr * start, expr * end, expr_ref & result) { - TRACE("t_str_rw", tout << "rewrite (RegexCharRange " << mk_pp(start, m()) << " " << mk_pp(end, m()) << ")" << std::endl;); - // both 'start' and 'end' must be string constants - ENSURE(m_strutil.is_string(start) && m_strutil.is_string(end)); - std::string arg0Value = m_strutil.get_string_constant_value(start); - std::string arg1Value = m_strutil.get_string_constant_value(end); - ENSURE(arg0Value.length() == 1 && arg1Value.length() == 1); - char low = arg0Value[0]; - char high = arg1Value[0]; - if (low > high) { - char t = low; - low = high; - high = t; - } - - char c = low; - std::string cStr; - cStr.push_back(c); - expr * res = m_strutil.mk_re_Str2Reg(cStr); - c++; - for (; c <= high; c++) { - cStr.clear(); - cStr.push_back(c); - res = m_strutil.mk_re_RegexUnion(res, m_strutil.mk_re_Str2Reg(cStr)); - } - result = res; - return BR_DONE; -} - -br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) { - SASSERT(f->get_family_id() == get_fid()); - - TRACE("t_str_rw", tout << "rewrite app: " << f->get_name() << std::endl;); - - switch(f->get_decl_kind()) { - case OP_STRCAT: - SASSERT(num_args == 2); - return mk_str_Concat(args[0], args[1], result); - case OP_STRLEN: - SASSERT(num_args == 1); - return mk_str_Length(args[0], result); - case OP_STR_CHARAT: - SASSERT(num_args == 2); - return mk_str_CharAt(args[0], args[1], result); - case OP_STR_STARTSWITH: - SASSERT(num_args == 2); - return mk_str_StartsWith(args[0], args[1], result); - case OP_STR_ENDSWITH: - SASSERT(num_args == 2); - return mk_str_EndsWith(args[0], args[1], result); - case OP_STR_CONTAINS: - SASSERT(num_args == 2); - return mk_str_Contains(args[0], args[1], result); - case OP_STR_INDEXOF: - SASSERT(num_args == 2); - return mk_str_Indexof(args[0], args[1], result); - case OP_STR_INDEXOF2: - SASSERT(num_args == 3); - return mk_str_Indexof2(args[0], args[1], args[2], result); - case OP_STR_LASTINDEXOF: - SASSERT(num_args == 2); - return mk_str_LastIndexof(args[0], args[1], result); - case OP_STR_REPLACE: - SASSERT(num_args == 3); - return mk_str_Replace(args[0], args[1], args[2], result); - case OP_STR_PREFIXOF: - SASSERT(num_args == 2); - return mk_str_prefixof(args[0], args[1], result); - case OP_STR_SUFFIXOF: - SASSERT(num_args == 2); - return mk_str_suffixof(args[0], args[1], result); - case OP_STR_STR2INT: - SASSERT(num_args == 1); - return mk_str_to_int(args[0], result); - case OP_STR_INT2STR: - SASSERT(num_args == 1); - return mk_str_from_int(args[0], result); - case OP_STR_SUBSTR: - SASSERT(num_args == 3); - return mk_str_Substr(args[0], args[1], args[2], result); - case OP_RE_STR2REGEX: - SASSERT(num_args == 1); - return mk_re_Str2Reg(args[0], result); - case OP_RE_REGEXIN: - SASSERT(num_args == 2); - return mk_re_RegexIn(args[0], args[1], result); - case OP_RE_REGEXPLUS: - SASSERT(num_args == 1); - return mk_re_RegexPlus(args[0], result); - case OP_RE_REGEXSTAR: - SASSERT(num_args == 1); - return mk_re_RegexStar(args[0], result); - case OP_RE_REGEXCONCAT: - SASSERT(num_args == 2); - return mk_re_RegexConcat(args[0], args[1], result); - case OP_RE_REGEXCHARRANGE: - SASSERT(num_args == 2); - return mk_re_RegexCharRange(args[0], args[1], result); - default: - return BR_FAILED; - } -} - -br_status str_rewriter::mk_eq_core(expr * l, expr * r, expr_ref & result) { - // from seq_rewriter - expr_ref_vector lhs(m()), rhs(m()), res(m()); - bool changed = false; - if (!reduce_eq(l, r, lhs, rhs, changed)) { - result = m().mk_false(); - return BR_DONE; - } - if (!changed) { - return BR_FAILED; - } - for (unsigned i = 0; i < lhs.size(); ++i) { - res.push_back(m().mk_eq(lhs[i].get(), rhs[i].get())); - } - result = mk_and(res); - return BR_REWRITE3; -} - -bool str_rewriter::reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change) { - change = false; - return true; -} - -bool str_rewriter::reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change) { - change = false; - return true; -} - -#endif /* disable */ diff --git a/src/ast/rewriter/str_rewriter.h b/src/ast/rewriter/str_rewriter.h deleted file mode 100644 index 8d6041a51..000000000 --- a/src/ast/rewriter/str_rewriter.h +++ /dev/null @@ -1,120 +0,0 @@ -/*++ -Copyright (c) 2016 Microsoft Corporation - -Module Name: - - str_rewriter.h - -Abstract: - - AST rewriting rules for string terms. - -Author: - - Murphy Berzish - -Notes: - ---*/ - -#if 0 - -#include"str_decl_plugin.h" -#include"arith_decl_plugin.h" -#include"rewriter_types.h" -#include"params.h" -#include -#include - -class str_rewriter { - str_util m_strutil; - arith_util m_autil; - -public: - str_rewriter(ast_manager & m, params_ref const & p = params_ref()) : - m_strutil(m), m_autil(m) { - } - - ast_manager & m() const { return m_strutil.get_manager(); } - family_id get_fid() const { return m_strutil.get_family_id(); } - - void updt_params(params_ref const & p) {} - static void get_param_descrs(param_descrs & r) {} - - br_status mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result); - br_status mk_eq_core(expr * lhs, expr * rhs, expr_ref & result); - - br_status mk_str_Concat(expr * arg0, expr * arg1, expr_ref & result); - br_status mk_str_Length(expr * arg0, expr_ref & result); - br_status mk_str_CharAt(expr * arg0, expr * arg1, expr_ref & result); - br_status mk_str_StartsWith(expr * haystack, expr * needle, expr_ref & result); - br_status mk_str_EndsWith(expr * haystack, expr * needle, expr_ref & result); - br_status mk_str_Contains(expr * haystack, expr * needle, expr_ref & result); - br_status mk_str_Indexof(expr * haystack, expr * needle, expr_ref & result); - br_status mk_str_Indexof2(expr * arg0, expr * arg1, expr * arg2, expr_ref & result); - br_status mk_str_LastIndexof(expr * haystack, expr * needle, expr_ref & result); - br_status mk_str_Replace(expr * base, expr * source, expr * target, expr_ref & result); - br_status mk_str_Substr(expr * base, expr * start, expr * len, expr_ref & result); - br_status mk_str_prefixof(expr * pre, expr * full, expr_ref & result); - br_status mk_str_suffixof(expr * post, expr * full, expr_ref & result); - br_status mk_str_to_int(expr * arg0, expr_ref & result); - br_status mk_str_from_int(expr * arg0, expr_ref & result); - - br_status mk_re_Str2Reg(expr * str, expr_ref & result); - br_status mk_re_RegexIn(expr * str, expr * re, expr_ref & result); - br_status mk_re_RegexPlus(expr * re, expr_ref & result); - br_status mk_re_RegexStar(expr * re, expr_ref & result); - br_status mk_re_RegexConcat(expr * r0, expr * r1, expr_ref & result); - br_status mk_re_RegexCharRange(expr * start, expr * end, expr_ref & result); - - bool reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change); - bool reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change); - -}; - -class nfa { -protected: - bool m_valid; - unsigned m_next_id; - - unsigned next_id() { - unsigned retval = m_next_id; - ++m_next_id; - return retval; - } - - unsigned m_start_state; - unsigned m_end_state; - - std::map > transition_map; - std::map > epsilon_map; - - void make_transition(unsigned start, char symbol, unsigned end) { - transition_map[start][symbol] = end; - } - - void make_epsilon_move(unsigned start, unsigned end) { - epsilon_map[start].insert(end); - } - - // Convert a regular expression to an e-NFA using Thompson's construction - void convert_re(expr * e, unsigned & start, unsigned & end, str_util & m_strutil); - -public: - nfa(str_util & m_strutil, expr * e) -: m_valid(true), m_next_id(0), m_start_state(0), m_end_state(0) { - convert_re(e, m_start_state, m_end_state, m_strutil); - } - - nfa() : m_valid(false), m_next_id(0), m_start_state(0), m_end_state(0) {} - - bool is_valid() const { - return m_valid; - } - - void epsilon_closure(unsigned start, std::set & closure); - - bool matches(std::string input); -}; - -#endif /* disable */ diff --git a/src/ast/str_decl_plugin.cpp b/src/ast/str_decl_plugin.cpp deleted file mode 100644 index 067420f04..000000000 --- a/src/ast/str_decl_plugin.cpp +++ /dev/null @@ -1,501 +0,0 @@ -/*++ -Module Name: - - str_decl_plugin.h - -Abstract: - - - -Author: - - Murphy Berzish (mtrberzi) 2015-09-02. - -Revision History: - ---*/ - -#if 0 - -#include -#include"str_decl_plugin.h" -#include"string_buffer.h" -#include"warning.h" -#include"ast_pp.h" -#include"ast_smt2_pp.h" - -str_decl_plugin::str_decl_plugin(): - m_strv_sym("String"), - m_str_decl(0), - m_regex_decl(0), - m_concat_decl(0), - m_length_decl(0), - m_charat_decl(0), - m_startswith_decl(0), - m_endswith_decl(0), - m_contains_decl(0), - m_indexof_decl(0), - m_indexof2_decl(0), - m_lastindexof_decl(0), - m_substr_decl(0), - m_replace_decl(0), - m_str2int_decl(0), - m_int2str_decl(0), - m_prefixof_decl(0), - m_suffixof_decl(0), - m_re_str2regex_decl(0), - m_re_regexin_decl(0), - m_re_regexconcat_decl(0), - m_re_regexstar_decl(0), - m_re_regexunion_decl(0), - m_re_unroll_decl(0), - m_re_regexplus_decl(0), - m_re_regexcharrange_decl(0), - m_arith_plugin(0), - m_arith_fid(0), - m_int_sort(0){ -} - -str_decl_plugin::~str_decl_plugin(){ -} - -void str_decl_plugin::finalize(void) { - #define DEC_REF(decl) if (decl) { m_manager->dec_ref(decl); } ((void) 0) - DEC_REF(m_str_decl); - DEC_REF(m_regex_decl); - DEC_REF(m_concat_decl); - DEC_REF(m_length_decl); - DEC_REF(m_charat_decl); - DEC_REF(m_startswith_decl); - DEC_REF(m_endswith_decl); - DEC_REF(m_contains_decl); - DEC_REF(m_indexof_decl); - DEC_REF(m_indexof2_decl); - DEC_REF(m_lastindexof_decl); - DEC_REF(m_substr_decl); - DEC_REF(m_replace_decl); - DEC_REF(m_prefixof_decl); - DEC_REF(m_suffixof_decl); - DEC_REF(m_str2int_decl); - DEC_REF(m_int2str_decl); - DEC_REF(m_re_str2regex_decl); - DEC_REF(m_re_regexin_decl); - DEC_REF(m_re_regexconcat_decl); - DEC_REF(m_re_regexstar_decl); - DEC_REF(m_re_regexunion_decl); - DEC_REF(m_re_regexplus_decl); - DEC_REF(m_re_regexcharrange_decl); - DEC_REF(m_re_unroll_decl); - DEC_REF(m_int_sort); -} - -void str_decl_plugin::set_manager(ast_manager * m, family_id id) { - decl_plugin::set_manager(m, id); - m_str_decl = m->mk_sort(symbol("String"), sort_info(id, STRING_SORT)); - m->inc_ref(m_str_decl); - sort * s = m_str_decl; - - m_regex_decl = m->mk_sort(symbol("Regex"), sort_info(id, REGEX_SORT)); - m->inc_ref(m_regex_decl); - sort * re = m_regex_decl; - - SASSERT(m_manager->has_plugin(symbol("arith"))); - m_arith_fid = m_manager->mk_family_id("arith"); - m_arith_plugin = static_cast(m_manager->get_plugin(m_arith_fid)); - SASSERT(m_arith_plugin); - - m_int_sort = m_manager->mk_sort(m_arith_fid, INT_SORT); - SASSERT(m_int_sort != 0); // arith_decl_plugin must be installed before str_decl_plugin. - m_manager->inc_ref(m_int_sort); - sort * i = m_int_sort; - - sort* boolT = m_manager->mk_bool_sort(); - -#define MK_OP(FIELD, NAME, KIND, SORT) \ - FIELD = m->mk_func_decl(symbol(NAME), SORT, SORT, SORT, func_decl_info(id, KIND)); \ - m->inc_ref(FIELD) - - MK_OP(m_concat_decl, "str.++", OP_STRCAT, s); - - m_length_decl = m->mk_func_decl(symbol("str.len"), s, i, func_decl_info(id, OP_STRLEN)); - m_manager->inc_ref(m_length_decl); - - m_charat_decl = m->mk_func_decl(symbol("str.at"), s, i, s, func_decl_info(id, OP_STR_CHARAT)); - m_manager->inc_ref(m_charat_decl); - - m_startswith_decl = m->mk_func_decl(symbol("StartsWith"), s, s, boolT, func_decl_info(id, OP_STR_STARTSWITH)); - m_manager->inc_ref(m_startswith_decl); - - m_endswith_decl = m->mk_func_decl(symbol("EndsWith"), s, s, boolT, func_decl_info(id, OP_STR_ENDSWITH)); - m_manager->inc_ref(m_endswith_decl); - - m_contains_decl = m->mk_func_decl(symbol("str.contains"), s, s, boolT, func_decl_info(id, OP_STR_CONTAINS)); - m_manager->inc_ref(m_contains_decl); - - m_indexof_decl = m->mk_func_decl(symbol("str.indexof"), s, s, i, func_decl_info(id, OP_STR_INDEXOF)); - m_manager->inc_ref(m_indexof_decl); - - { - sort * d[3] = { s, s, i }; - m_indexof2_decl = m->mk_func_decl(symbol("Indexof2"), 3, d, i, func_decl_info(id, OP_STR_INDEXOF2)); - m_manager->inc_ref(m_indexof2_decl); - } - - m_lastindexof_decl = m->mk_func_decl(symbol("str.lastindexof"), s, s, i, func_decl_info(id, OP_STR_LASTINDEXOF)); - m_manager->inc_ref(m_lastindexof_decl); - - { - sort * d[3] = {s, i, i }; - m_substr_decl = m->mk_func_decl(symbol("str.substr"), 3, d, s, func_decl_info(id, OP_STR_SUBSTR)); - m_manager->inc_ref(m_substr_decl); - } - - { - sort * d[3] = {s, s, s}; - m_replace_decl = m->mk_func_decl(symbol("str.replace"), 3, d, s, func_decl_info(id, OP_STR_REPLACE)); - m_manager->inc_ref(m_replace_decl); - } - - m_prefixof_decl = m->mk_func_decl(symbol("str.prefixof"), s, s, boolT, func_decl_info(id, OP_STR_PREFIXOF)); - m_manager->inc_ref(m_prefixof_decl); - - m_suffixof_decl = m->mk_func_decl(symbol("str.suffixof"), s, s, boolT, func_decl_info(id, OP_STR_SUFFIXOF)); - m_manager->inc_ref(m_suffixof_decl); - - m_str2int_decl = m->mk_func_decl(symbol("str.to-int"), s, i, func_decl_info(id, OP_STR_STR2INT)); - m_manager->inc_ref(m_str2int_decl); - - m_int2str_decl = m->mk_func_decl(symbol("str.from-int"), i, s, func_decl_info(id, OP_STR_INT2STR)); - m_manager->inc_ref(m_int2str_decl); - - m_re_str2regex_decl = m->mk_func_decl(symbol("str.to.re"), s, re, func_decl_info(id, OP_RE_STR2REGEX)); - m_manager->inc_ref(m_re_str2regex_decl); - - m_re_regexin_decl = m->mk_func_decl(symbol("str.in.re"), s, re, boolT, func_decl_info(id, OP_RE_REGEXIN)); - m_manager->inc_ref(m_re_regexin_decl); - - m_re_regexconcat_decl = m->mk_func_decl(symbol("re.++"), re, re, re, func_decl_info(id, OP_RE_REGEXCONCAT)); - m_manager->inc_ref(m_re_regexconcat_decl); - - m_re_regexstar_decl = m->mk_func_decl(symbol("re.*"), re, re, func_decl_info(id, OP_RE_REGEXSTAR)); - m_manager->inc_ref(m_re_regexstar_decl); - - m_re_regexplus_decl = m->mk_func_decl(symbol("re.+"), re, re, func_decl_info(id, OP_RE_REGEXPLUS)); - m_manager->inc_ref(m_re_regexplus_decl); - - m_re_regexunion_decl = m->mk_func_decl(symbol("re.union"), re, re, re, func_decl_info(id, OP_RE_REGEXUNION)); - m_manager->inc_ref(m_re_regexunion_decl); - - m_re_unroll_decl = m->mk_func_decl(symbol("Unroll"), re, i, s, func_decl_info(id, OP_RE_UNROLL)); - m_manager->inc_ref(m_re_unroll_decl); - - m_re_regexcharrange_decl = m->mk_func_decl(symbol("re.range"), s, s, re, func_decl_info(id, OP_RE_REGEXCHARRANGE)); - m_manager->inc_ref(m_re_regexcharrange_decl); - -} - -decl_plugin * str_decl_plugin::mk_fresh() { - return alloc(str_decl_plugin); -} - -sort * str_decl_plugin::mk_sort(decl_kind k, unsigned num_parameters, parameter const * parameters) { - switch (k) { - case STRING_SORT: return m_str_decl; - case REGEX_SORT: return m_regex_decl; - default: return 0; - } -} - -func_decl * str_decl_plugin::mk_func_decl(decl_kind k) { - switch(k) { - case OP_STRCAT: return m_concat_decl; - case OP_STRLEN: return m_length_decl; - case OP_STR_CHARAT: return m_charat_decl; - case OP_STR_STARTSWITH: return m_startswith_decl; - case OP_STR_ENDSWITH: return m_endswith_decl; - case OP_STR_CONTAINS: return m_contains_decl; - case OP_STR_INDEXOF: return m_indexof_decl; - case OP_STR_INDEXOF2: return m_indexof2_decl; - case OP_STR_LASTINDEXOF: return m_lastindexof_decl; - case OP_STR_SUBSTR: return m_substr_decl; - case OP_STR_REPLACE: return m_replace_decl; - case OP_STR_PREFIXOF: return m_prefixof_decl; - case OP_STR_SUFFIXOF: return m_suffixof_decl; - case OP_STR_STR2INT: return m_str2int_decl; - case OP_STR_INT2STR: return m_int2str_decl; - case OP_RE_STR2REGEX: return m_re_str2regex_decl; - case OP_RE_REGEXIN: return m_re_regexin_decl; - case OP_RE_REGEXCONCAT: return m_re_regexconcat_decl; - case OP_RE_REGEXSTAR: return m_re_regexstar_decl; - case OP_RE_REGEXPLUS: return m_re_regexplus_decl; - case OP_RE_REGEXUNION: return m_re_regexunion_decl; - case OP_RE_UNROLL: return m_re_unroll_decl; - case OP_RE_REGEXCHARRANGE: return m_re_regexcharrange_decl; - default: return 0; - } -} - -func_decl * str_decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters, parameter const * parameters, - unsigned arity, sort * const * domain, sort * range) { - if (k == OP_STR) { - m_manager->raise_exception("OP_STR not yet implemented in mk_func_decl!"); - return 0; - } - if (arity == 0) { - m_manager->raise_exception("no arguments supplied to string operator"); - return 0; - } - return mk_func_decl(k); -} - -app * str_decl_plugin::mk_string(std::string & val) { - std::map::iterator it = string_cache.find(val); - //if (it == string_cache.end()) { - if (true) { - char * new_buffer = alloc_svect(char, (val.length() + 1)); - strcpy(new_buffer, val.c_str()); - parameter p[1] = {parameter(new_buffer)}; - func_decl * d; - d = m_manager->mk_const_decl(m_strv_sym, m_str_decl, func_decl_info(m_family_id, OP_STR, 1, p)); - app * str = m_manager->mk_const(d); - string_cache[val] = str; - return str; - } else { - return it->second; - } -} - -app * str_decl_plugin::mk_string(const char * val) { - std::string key(val); - return mk_string(key); -} - -app * str_decl_plugin::mk_fresh_string() { - // cheating. - // take the longest string in the cache, append the letter "A", and call it fresh. - std::string longestString = ""; - std::map::iterator it = string_cache.begin(); - for (; it != string_cache.end(); ++it) { - if (it->first.length() > longestString.length()) { - longestString = it->first; - } - } - longestString += "A"; - return mk_string(longestString); -} - -void str_decl_plugin::get_op_names(svector & op_names, symbol const & logic) { - op_names.push_back(builtin_name("str.++", OP_STRCAT)); - op_names.push_back(builtin_name("str.len", OP_STRLEN)); - op_names.push_back(builtin_name("str.at", OP_STR_CHARAT)); - op_names.push_back(builtin_name("StartsWith", OP_STR_STARTSWITH)); - op_names.push_back(builtin_name("EndsWith", OP_STR_ENDSWITH)); - op_names.push_back(builtin_name("str.contains", OP_STR_CONTAINS)); - op_names.push_back(builtin_name("str.indexof", OP_STR_INDEXOF)); - op_names.push_back(builtin_name("Indexof2", OP_STR_INDEXOF2)); - op_names.push_back(builtin_name("str.lastindexof", OP_STR_LASTINDEXOF)); - op_names.push_back(builtin_name("str.substr", OP_STR_SUBSTR)); - op_names.push_back(builtin_name("str.replace", OP_STR_REPLACE)); - op_names.push_back(builtin_name("str.prefixof", OP_STR_PREFIXOF)); - op_names.push_back(builtin_name("str.suffixof", OP_STR_SUFFIXOF)); - op_names.push_back(builtin_name("str.to-int", OP_STR_STR2INT)); - op_names.push_back(builtin_name("str.from-int", OP_STR_INT2STR)); - op_names.push_back(builtin_name("str.to.re", OP_RE_STR2REGEX)); - op_names.push_back(builtin_name("str.in.re", OP_RE_REGEXIN)); - op_names.push_back(builtin_name("re.++", OP_RE_REGEXCONCAT)); - op_names.push_back(builtin_name("re.*", OP_RE_REGEXSTAR)); - op_names.push_back(builtin_name("re.union", OP_RE_REGEXUNION)); - op_names.push_back(builtin_name("re.+", OP_RE_REGEXPLUS)); - op_names.push_back(builtin_name("Unroll", OP_RE_UNROLL)); - op_names.push_back(builtin_name("re.range", OP_RE_REGEXCHARRANGE)); -} - -void str_decl_plugin::get_sort_names(svector & sort_names, symbol const & logic) { - sort_names.push_back(builtin_name("String", STRING_SORT)); - sort_names.push_back(builtin_name("Regex", REGEX_SORT)); -} - -bool str_decl_plugin::is_value(app * e) const { - if (e->get_family_id() != m_family_id) { - return false; - } - switch (e->get_decl_kind()) { - case OP_STR: - return true; - default: - return false; - } -} - -bool str_recognizers::is_string(expr const * n, const char ** val) const { - if (!is_app_of(n, m_afid, OP_STR)) - return false; - func_decl * decl = to_app(n)->get_decl(); - *val = decl->get_parameter(0).get_string(); - return true; -} - -bool str_recognizers::is_string(expr const * n) const { - const char * tmp = 0; - return is_string(n, & tmp); -} - -std::string str_recognizers::get_string_constant_value(expr const *n) const { - const char * cstr = 0; - bool isString = is_string(n, & cstr); - SASSERT(isString); - std::string strval(cstr); - return strval; -} - -str_util::str_util(ast_manager &m) : - str_recognizers(m.mk_family_id(symbol("str"))), - m_manager(m) { - SASSERT(m.has_plugin(symbol("str"))); - m_plugin = static_cast(m.get_plugin(m.mk_family_id(symbol("str")))); - m_fid = m_plugin->get_family_id(); -} - -/* - * Scan through the string 'val' and interpret each instance of "backslash followed by a character" - * as a possible escape sequence. Emit all other characters as-is. - * This exists because the SMT-LIB 2.5 standard does not recognize escape sequences other than "" -> " . - * The escape sequences recognized are as follows: - * \a \b \e \f \n \r \t \v \\ : as specified by the C++ standard - * \ooo : produces the ASCII character corresponding to the octal value "ooo", where each "o" is a - * single octal digit and between 1 and 3 valid digits are given - * \xhh : produces the ASCII character corresponding to the hexadecimal value "hh", where each "h" is a - * single case-insensitive hex digit (0-9A-F) and exactly 2 digits are given - * \C, for any character C that does not start a legal escape sequence : the backslash is ignored and "C" is produced. - */ -app * str_util::mk_string_with_escape_characters(std::string & val) { - std::string parsedStr; - parsedStr.reserve(val.length()); - for (unsigned i = 0; i < val.length(); ++i) { - char nextChar = val.at(i); - - if (nextChar == '\\') { - // check escape sequence - i++; - if (i >= val.length()) { - get_manager().raise_exception("invalid escape sequence"); - } - char escapeChar1 = val.at(i); - if (escapeChar1 == 'a') { - parsedStr.push_back('\a'); - } else if (escapeChar1 == 'b') { - parsedStr.push_back('\b'); - } else if (escapeChar1 == 'e') { - parsedStr.push_back('\e'); - } else if (escapeChar1 == 'f') { - parsedStr.push_back('\f'); - } else if (escapeChar1 == 'n') { - parsedStr.push_back('\n'); - } else if (escapeChar1 == 'r') { - parsedStr.push_back('\r'); - } else if (escapeChar1 == 't') { - parsedStr.push_back('\t'); - } else if (escapeChar1 == 'v') { - parsedStr.push_back('\v'); - } else if (escapeChar1 == '\\') { - parsedStr.push_back('\\'); - } else if (escapeChar1 == 'x') { - // hex escape: we expect 'x' to be followed by exactly two hex digits - // which means that i+2 must be a valid index - if (i+2 >= val.length()) { - get_manager().raise_exception("invalid hex escape: \\x must be followed by exactly two hex digits"); - } - char hexDigitHi = val.at(i+1); - char hexDigitLo = val.at(i+2); - i += 2; - if (!isxdigit((int)hexDigitHi) || !isxdigit((int)hexDigitLo)) { - get_manager().raise_exception("invalid hex escape: \\x must be followed by exactly two hex digits"); - } - char tmp[3] = {hexDigitHi, hexDigitLo, '\0'}; - long converted = strtol(tmp, NULL, 16); - unsigned char convChar = (unsigned char)converted; - parsedStr.push_back(convChar); - } else if (escapeChar1 == '0' || escapeChar1 == '1' || escapeChar1 == '2' || escapeChar1 == '3' || - escapeChar1 == '4' || escapeChar1 == '5' || escapeChar1 == '6' || escapeChar1 == '7') { - // octal escape: we expect exactly three octal digits - // which means that val[i], val[i+1], val[i+2] must all be octal digits - // and that i+2 must be a valid index - if (i+2 >= val.length()) { - get_manager().raise_exception("invalid octal escape: exactly three octal digits required"); - } - char c2 = escapeChar1; - char c1 = val.at(i+1); - char c0 = val.at(i+2); - i += 2; - - if (!isdigit(c2) || !isdigit(c1) || !isdigit(c0)) { - get_manager().raise_exception("invalid octal escape: exactly three octal digits required"); - } - - if (c2 == '8' || c2 == '9' || c1 == '8' || c1 == '9' || c0 == '8' || c0 == '9') { - get_manager().raise_exception("invalid octal escape: exactly three octal digits required"); - } - - char tmp[4] = {c2, c1, c0, '\0'}; - long converted = strtol(tmp, NULL, 8); - unsigned char convChar = (unsigned char)converted; - parsedStr.push_back(convChar); - } else { - // unrecognized escape sequence -- just emit that character - parsedStr.push_back(escapeChar1); - } - } else { - parsedStr.push_back(nextChar); - } - - // i is incremented at the end of this loop. - // If it is modified, ensure that it points to the index before - // the next character. - } - return mk_string(parsedStr); -} - -static std::string str2RegexStr(std::string str) { - std::string res = ""; - int len = str.size(); - for (int i = 0; i < len; i++) { - char nc = str[i]; - // 12 special chars - if (nc == '\\' || nc == '^' || nc == '$' || nc == '.' || nc == '|' || nc == '?' - || nc == '*' || nc == '+' || nc == '(' || nc == ')' || nc == '[' || nc == '{') { - res.append(1, '\\'); - } - res.append(1, str[i]); - } - return res; -} - -std::string str_util::get_std_regex_str(expr * regex) { - app * a_regex = to_app(regex); - if (is_re_Str2Reg(a_regex)) { - expr * regAst = a_regex->get_arg(0); - std::string regStr = str2RegexStr(get_string_constant_value(regAst)); - return regStr; - } else if (is_re_RegexConcat(a_regex)) { - expr * reg1Ast = a_regex->get_arg(0); - expr * reg2Ast = a_regex->get_arg(1); - std::string reg1Str = get_std_regex_str(reg1Ast); - std::string reg2Str = get_std_regex_str(reg2Ast); - return "(" + reg1Str + ")(" + reg2Str + ")"; - } else if (is_re_RegexUnion(a_regex)) { - expr * reg1Ast = a_regex->get_arg(0); - expr * reg2Ast = a_regex->get_arg(1); - std::string reg1Str = get_std_regex_str(reg1Ast); - std::string reg2Str = get_std_regex_str(reg2Ast); - return "(" + reg1Str + ")|(" + reg2Str + ")"; - } else if (is_re_RegexStar(a_regex)) { - expr * reg1Ast = a_regex->get_arg(0); - std::string reg1Str = get_std_regex_str(reg1Ast); - return "(" + reg1Str + ")*"; - } else { - TRACE("t_str", tout << "BUG: unrecognized regex term " << mk_pp(regex, get_manager()) << std::endl;); - UNREACHABLE(); return ""; - } -} - -#endif /* disable */ diff --git a/src/ast/str_decl_plugin.h b/src/ast/str_decl_plugin.h deleted file mode 100644 index 28ecd1e43..000000000 --- a/src/ast/str_decl_plugin.h +++ /dev/null @@ -1,218 +0,0 @@ -/*++ -Module Name: - - str_decl_plugin.h - -Abstract: - - - -Author: - - Murphy Berzish (mtrberzi) 2015-09-02. - -Revision History: - ---*/ - -#if 0 - -#ifndef _STR_DECL_PLUGIN_H_ -#define _STR_DECL_PLUGIN_H_ - -#include"ast.h" -#include"arith_decl_plugin.h" -#include - -enum str_sort_kind { - STRING_SORT, - REGEX_SORT, -}; - -enum str_op_kind { - OP_STR, /* string constants */ - // basic string operators - OP_STRCAT, - OP_STRLEN, - // higher-level string functions -- these are reduced to basic operations - OP_STR_CHARAT, - OP_STR_STARTSWITH, - OP_STR_ENDSWITH, - OP_STR_CONTAINS, - OP_STR_INDEXOF, - OP_STR_INDEXOF2, - OP_STR_LASTINDEXOF, - OP_STR_SUBSTR, - OP_STR_REPLACE, - // SMT-LIB 2.5 standard operators -- these are rewritten to internal ones - OP_STR_PREFIXOF, - OP_STR_SUFFIXOF, - // string-integer conversion - OP_STR_STR2INT, - OP_STR_INT2STR, OP_STR_PLACEHOLDER1, OP_STR_PLACEHOLDER2, - // regular expression operators - OP_RE_STR2REGEX, - OP_RE_REGEXIN, - OP_RE_REGEXCONCAT, - OP_RE_REGEXSTAR, - OP_RE_REGEXUNION, - OP_RE_UNROLL, - // higher-level regex operators - OP_RE_REGEXPLUS, - OP_RE_REGEXCHARRANGE, - // end - LAST_STR_OP -}; - -class str_decl_plugin : public decl_plugin { -protected: - symbol m_strv_sym; - sort * m_str_decl; - sort * m_regex_decl; - - func_decl * m_concat_decl; - func_decl * m_length_decl; - - func_decl * m_charat_decl; - func_decl * m_startswith_decl; - func_decl * m_endswith_decl; - func_decl * m_contains_decl; - func_decl * m_indexof_decl; - func_decl * m_indexof2_decl; - func_decl * m_lastindexof_decl; - func_decl * m_substr_decl; - func_decl * m_replace_decl; - func_decl * m_str2int_decl; - func_decl * m_int2str_decl; - func_decl * m_prefixof_decl; - func_decl * m_suffixof_decl; - - func_decl * m_re_str2regex_decl; - func_decl * m_re_regexin_decl; - func_decl * m_re_regexconcat_decl; - func_decl * m_re_regexstar_decl; - func_decl * m_re_regexunion_decl; - func_decl * m_re_unroll_decl; - func_decl * m_re_regexplus_decl; - func_decl * m_re_regexcharrange_decl; - - arith_decl_plugin * m_arith_plugin; - family_id m_arith_fid; - sort * m_int_sort; - - std::map string_cache; - - virtual void set_manager(ast_manager * m, family_id id); - - func_decl * mk_func_decl(decl_kind k); -public: - str_decl_plugin(); - virtual ~str_decl_plugin(); - virtual void finalize(); - - virtual decl_plugin * mk_fresh(); - virtual sort * mk_sort(decl_kind k, unsigned num_parameters, parameter const * parameters); - virtual func_decl * mk_func_decl(decl_kind k, unsigned num_parameters, parameter const * parameters, - unsigned arity, sort * const * domain, sort * range); - - app * mk_string(const char * val); - app * mk_string(std::string & val); - app * mk_fresh_string(); - - virtual void get_op_names(svector & op_names, symbol const & logic); - virtual void get_sort_names(svector & sort_names, symbol const & logic); - - virtual bool is_value(app * e) const; - virtual bool is_unique_value(app * e) const { return is_value(e); } -}; - -class str_recognizers { - family_id m_afid; -public: - str_recognizers(family_id fid):m_afid(fid) {} - family_id get_fid() const { return m_afid; } - family_id get_family_id() const { return get_fid(); } - - bool is_str_sort(sort* s) const { return is_sort_of(s, m_afid, STRING_SORT); } - - bool is_string(expr const * n, const char ** val) const; - bool is_string(expr const * n) const; - - bool is_re_Str2Reg(expr const * n) const { return is_app_of(n, get_fid(), OP_RE_STR2REGEX); } - bool is_re_RegexConcat(expr const * n) const { return is_app_of(n, get_fid(), OP_RE_REGEXCONCAT); } - bool is_re_RegexUnion(expr const * n) const { return is_app_of(n, get_fid(), OP_RE_REGEXUNION); } - bool is_re_RegexStar(expr const * n) const { return is_app_of(n, get_fid(), OP_RE_REGEXSTAR); } - bool is_re_RegexPlus(expr const * n) const { return is_app_of(n, get_fid(), OP_RE_REGEXPLUS); } - - std::string get_string_constant_value(expr const *n) const; -}; - -class str_util : public str_recognizers { - ast_manager & m_manager; - str_decl_plugin * m_plugin; - family_id m_fid; -public: - str_util(ast_manager & m); - ast_manager & get_manager() const { return m_manager; } - str_decl_plugin & plugin() { return *m_plugin; } - - sort* mk_string_sort() const { return get_manager().mk_sort(m_fid, STRING_SORT, 0, 0); } - - app * mk_string(const char * val) { - return m_plugin->mk_string(val); - } - app * mk_string(std::string & val) { - return m_plugin->mk_string(val); - } - - app * mk_fresh_string() { - return m_plugin->mk_fresh_string(); - } - - app * mk_string_with_escape_characters(const char * val) { - std::string str(val); - return mk_string_with_escape_characters(str); - } - app * mk_string_with_escape_characters(std::string & val); - - app * mk_str_StartsWith(expr * haystack, expr * needle) { - expr * es[2] = {haystack, needle}; - return m_manager.mk_app(get_fid(), OP_STR_STARTSWITH, 2, es); - } - - app * mk_str_EndsWith(expr * haystack, expr * needle) { - expr * es[2] = {haystack, needle}; - return m_manager.mk_app(get_fid(), OP_STR_ENDSWITH, 2, es); - } - - app * mk_re_Str2Reg(expr * s) { - expr * es[1] = {s}; - return m_manager.mk_app(get_fid(), OP_RE_STR2REGEX, 1, es); - } - - app * mk_re_Str2Reg(std::string s) { - return mk_re_Str2Reg(mk_string(s)); - } - - app * mk_re_RegexUnion(expr * e1, expr * e2) { - expr * es[2] = {e1, e2}; - return m_manager.mk_app(get_fid(), OP_RE_REGEXUNION, 2, es); - } - - app * mk_re_RegexConcat(expr * e1, expr * e2) { - expr * es[2] = {e1, e2}; - return m_manager.mk_app(get_fid(), OP_RE_REGEXCONCAT, 2, es); - } - - app * mk_re_RegexStar(expr * r) { - expr * es[1] = {r}; - return m_manager.mk_app(get_fid(), OP_RE_REGEXSTAR, 1, es); - } - - std::string get_std_regex_str(expr * regex); - -}; - -#endif /* _STR_DECL_PLUGIN_H_ */ - -#endif /* disable */ From a0237ed2a6e2c08483e39ae64daa0727e165e54c Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Thu, 16 Mar 2017 18:56:43 -0700 Subject: [PATCH 382/562] fix crash reported in #946 Signed-off-by: Nikolaj Bjorner --- src/opt/opt_solver.cpp | 1 + src/smt/theory_dense_diff_logic_def.h | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/opt/opt_solver.cpp b/src/opt/opt_solver.cpp index 351141a3f..8ae4e467f 100644 --- a/src/opt/opt_solver.cpp +++ b/src/opt/opt_solver.cpp @@ -50,6 +50,7 @@ namespace opt { if (m_params.m_case_split_strategy == CS_ACTIVITY_DELAY_NEW) { m_params.m_relevancy_lvl = 0; } + // m_params.m_auto_config = false; } unsigned opt_solver::m_dump_count = 0; diff --git a/src/smt/theory_dense_diff_logic_def.h b/src/smt/theory_dense_diff_logic_def.h index 877d4f659..628eeea83 100644 --- a/src/smt/theory_dense_diff_logic_def.h +++ b/src/smt/theory_dense_diff_logic_def.h @@ -901,7 +901,7 @@ namespace smt { objective_term const& objective = m_objectives[v]; has_shared = false; - IF_VERBOSE(1, + IF_VERBOSE(4, for (unsigned i = 0; i < objective.size(); ++i) { verbose_stream() << objective[i].second << " * v" << objective[i].first << " "; @@ -991,9 +991,12 @@ namespace smt { if (num_nodes <= v && v < num_nodes + num_edges) { unsigned edge_id = v - num_nodes; literal lit = m_edges[edge_id].m_justification; - get_context().literal2expr(lit, tmp); - core.push_back(tmp); + if (lit != null_literal) { + get_context().literal2expr(lit, tmp); + core.push_back(tmp); + } } + TRACE("opt", tout << core << "\n";); } for (unsigned i = 0; i < num_nodes; ++i) { mpq_inf const& val = S.get_value(i); From d754aa2dc4221c56bcb2a240a5084c4b394e0e5b Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Fri, 17 Mar 2017 10:12:32 -0700 Subject: [PATCH 383/562] disable ackerman reduction when head contains a non-constant/non-variable. #947 Signed-off-by: Nikolaj Bjorner --- src/muz/transforms/dl_mk_array_blast.cpp | 7 +++++++ src/muz/transforms/dl_mk_interp_tail_simplifier.cpp | 4 ---- src/smt/theory_arith_int.h | 6 ++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/muz/transforms/dl_mk_array_blast.cpp b/src/muz/transforms/dl_mk_array_blast.cpp index 82d351113..031c5098e 100644 --- a/src/muz/transforms/dl_mk_array_blast.cpp +++ b/src/muz/transforms/dl_mk_array_blast.cpp @@ -126,6 +126,12 @@ namespace datalog { app* s; var* v; + // disable Ackerman reduction if head contains a non-variable or non-constant argument. + for (unsigned i = 0; i < to_app(head)->get_num_args(); ++i) { + expr* arg = to_app(head)->get_arg(i); + if (!is_var(arg) && !m.is_value(arg)) return false; + } + for (unsigned i = 0; i < conjs.size(); ++i) { expr* e = conjs[i].get(); if (is_select_eq_var(e, s, v)) { @@ -281,6 +287,7 @@ namespace datalog { m_rewriter(body); sub(head); m_rewriter(head); + TRACE("dl", tout << body << " => " << head << "\n";); change = ackermanize(r, body, head); if (!change) { rules.add_rule(&r); diff --git a/src/muz/transforms/dl_mk_interp_tail_simplifier.cpp b/src/muz/transforms/dl_mk_interp_tail_simplifier.cpp index ea0e6c887..455b06d3d 100644 --- a/src/muz/transforms/dl_mk_interp_tail_simplifier.cpp +++ b/src/muz/transforms/dl_mk_interp_tail_simplifier.cpp @@ -394,10 +394,6 @@ namespace datalog { m_simp(a, simp1_res); (*m_rw)(simp1_res.get(), res); - /*if (simp1_res.get()!=res.get()) { - std::cout<<"pre norm:\n"<get_owner(), m_util.mk_numeral(_k, true)); + expr* e = get_enode(v)->get_owner(); + bound = m_util.mk_ge(e, m_util.mk_numeral(_k, m_util.is_int(e))); TRACE("arith_int", tout << mk_bounded_pp(bound, get_manager()) << "\n";); context & ctx = get_context(); ctx.internalize(bound, true); @@ -371,7 +372,7 @@ namespace smt { ctx.mk_th_axiom(get_id(), l1, l2); - TRACE("theory_arith_int", + TRACE("arith_int", tout << "cut: (or " << mk_pp(p1, get_manager()) << " " << mk_pp(p2, get_manager()) << ")\n"; ); @@ -1407,6 +1408,7 @@ namespace smt { if (m_params.m_arith_int_eq_branching && branch_infeasible_int_equality()) { return FC_CONTINUE; } + theory_var int_var = find_infeasible_int_base_var(); if (int_var != null_theory_var) { TRACE("arith_int", tout << "v" << int_var << " does not have an integer assignment: " << get_value(int_var) << "\n";); From 43f9a0a2bdf3d9be5a5deae97d35b590e010726c Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 17 Mar 2017 13:48:30 -0400 Subject: [PATCH 384/562] fix unterminated char* --- src/smt/theory_str.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 0554ae2c2..ff32e6f38 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1642,7 +1642,7 @@ static zstring str2RegexStr(zstring str) { || nc == '*' || nc == '+' || nc == '(' || nc == ')' || nc == '[' || nc == '{') { res = res + zstring("\\"); } - char tmp[1] = {(char)str[i]}; + char tmp[2] = {(char)str[i], '\0'}; res = res + zstring(tmp); } return res; From d58018841e8bca4a85bbd8972fccb5d6cca645a7 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Fri, 17 Mar 2017 10:52:16 -0700 Subject: [PATCH 385/562] remove code that causes infinite loop. Stackoverflow question from Dominik Wojtaszek Signed-off-by: Nikolaj Bjorner --- src/ast/ast_smt2_pp.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/ast/ast_smt2_pp.cpp b/src/ast/ast_smt2_pp.cpp index 98c3b7962..89af8bd3e 100644 --- a/src/ast/ast_smt2_pp.cpp +++ b/src/ast/ast_smt2_pp.cpp @@ -1002,14 +1002,6 @@ class smt2_printer { reset_stacks(); SASSERT(&(r.get_manager()) == &(fm())); m_soccs(n); - TRACE("smt2_pp_shared", - tout << "shared terms for:\n" << mk_pp(n, m()) << "\n"; - tout << "------>\n"; - shared_occs::iterator it = m_soccs.begin_shared(); - shared_occs::iterator end = m_soccs.end_shared(); - for (; it != end; ++it) { - tout << mk_pp(*it, m()) << "\n"; - }); m_root = n; push_frame(n, true); while (!m_frame_stack.empty()) { From 8ac060c549441e0968c8db5e0166ca61ef996f18 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Mon, 20 Mar 2017 09:12:41 +0000 Subject: [PATCH 386/562] fix build with VS 2017 --- src/util/hwf.cpp | 1 + src/util/hwf.h | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/util/hwf.cpp b/src/util/hwf.cpp index bd8d4958d..f84f7fe40 100644 --- a/src/util/hwf.cpp +++ b/src/util/hwf.cpp @@ -52,6 +52,7 @@ Revision History: #ifdef USE_INTRINSICS #include +#include #endif hwf_manager::hwf_manager() : diff --git a/src/util/hwf.h b/src/util/hwf.h index cf0c9b7ea..8816e5b37 100644 --- a/src/util/hwf.h +++ b/src/util/hwf.h @@ -88,9 +88,6 @@ public: bool is_pzero(hwf const & x); bool is_one(hwf const & x); - - // structural eq - bool eq_core(hwf const & x, hwf const & y); bool eq(hwf const & x, hwf const & y); bool lt(hwf const & x, hwf const & y); From ca4ae171ea95d39fd836972789db09cdbbb7c82f Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Tue, 21 Mar 2017 07:40:35 -0600 Subject: [PATCH 387/562] remove unsound simplification in prefix #949 Signed-off-by: Nikolaj Bjorner --- src/ast/rewriter/seq_rewriter.cpp | 17 +++++++++++------ src/smt/theory_seq.cpp | 7 +++++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/ast/rewriter/seq_rewriter.cpp b/src/ast/rewriter/seq_rewriter.cpp index 26918261e..b7f99298c 100644 --- a/src/ast/rewriter/seq_rewriter.cpp +++ b/src/ast/rewriter/seq_rewriter.cpp @@ -676,6 +676,7 @@ br_status seq_rewriter::mk_seq_prefix(expr* a, expr* b, expr_ref& result) { bool isc2 = m_util.str.is_string(b, s2); if (isc1 && isc2) { result = m().mk_bool_val(s1.prefixof(s2)); + TRACE("seq", tout << result << "\n";); return BR_DONE; } if (m_util.str.is_empty(a)) { @@ -689,6 +690,7 @@ br_status seq_rewriter::mk_seq_prefix(expr* a, expr* b, expr_ref& result) { expr_ref_vector as(m()), bs(m()); if (a1 != b1 && isc1 && isc2) { + TRACE("seq", tout << s1 << " " << s2 << "\n";); if (s1.length() <= s2.length()) { if (s1.prefixof(s2)) { if (a == a1) { @@ -733,26 +735,27 @@ br_status seq_rewriter::mk_seq_prefix(expr* a, expr* b, expr_ref& result) { m_util.str.get_concat(a, as); m_util.str.get_concat(b, bs); unsigned i = 0; - bool all_values = true; expr_ref_vector eqs(m()); for (; i < as.size() && i < bs.size(); ++i) { expr* a = as[i].get(), *b = bs[i].get(); if (a == b) { continue; } - all_values &= m().is_value(a) && m().is_value(b); - if (all_values) { - result = m().mk_false(); - return BR_DONE; - } if (m_util.str.is_unit(a) && m_util.str.is_unit(b)) { eqs.push_back(m().mk_eq(a, b)); continue; } + if (m().is_value(a) && m().is_value(b) && m_util.str.is_string(a) && m_util.str.is_string(b)) { + TRACE("seq", tout << mk_pp(a, m()) << " != " << mk_pp(b, m()) << "\n";); + result = m().mk_false(); + return BR_DONE; + } + break; } if (i == as.size()) { result = mk_and(eqs); + TRACE("seq", tout << result << "\n";); if (m().is_true(result)) { return BR_DONE; } @@ -764,6 +767,7 @@ br_status seq_rewriter::mk_seq_prefix(expr* a, expr* b, expr_ref& result) { eqs.push_back(m().mk_eq(m_util.str.mk_empty(m().get_sort(a)), as[j].get())); } result = mk_and(eqs); + TRACE("seq", tout << result << "\n";); return BR_REWRITE3; } if (i > 0) { @@ -771,6 +775,7 @@ br_status seq_rewriter::mk_seq_prefix(expr* a, expr* b, expr_ref& result) { a = m_util.str.mk_concat(as.size() - i, as.c_ptr() + i); b = m_util.str.mk_concat(bs.size() - i, bs.c_ptr() + i); result = m_util.str.mk_prefix(a, b); + TRACE("seq", tout << result << "\n";); return BR_DONE; } else { diff --git a/src/smt/theory_seq.cpp b/src/smt/theory_seq.cpp index d5251c56b..daf5e3702 100644 --- a/src/smt/theory_seq.cpp +++ b/src/smt/theory_seq.cpp @@ -2719,7 +2719,9 @@ bool theory_seq::can_propagate() { expr_ref theory_seq::canonize(expr* e, dependency*& eqs) { expr_ref result = expand(e, eqs); + TRACE("seq", tout << mk_pp(e, m) << " expands to " << result << "\n";); m_rewrite(result); + TRACE("seq", tout << mk_pp(e, m) << " rewrites to " << result << "\n";); return result; } @@ -4469,10 +4471,11 @@ bool theory_seq::canonizes(bool sign, expr* e) { context& ctx = get_context(); dependency* deps = 0; expr_ref cont = canonize(e, deps); - TRACE("seq", tout << mk_pp(e, m) << " -> " << cont << "\n";); + TRACE("seq", tout << mk_pp(e, m) << " -> " << cont << "\n"; + if (deps) display_deps(tout, deps);); if ((m.is_true(cont) && !sign) || (m.is_false(cont) && sign)) { - TRACE("seq", display(tout);); + TRACE("seq", display(tout); tout << ctx.get_assignment(ctx.get_literal(e)) << "\n";); propagate_lit(deps, 0, 0, ctx.get_literal(e)); return true; } From 6804c88b66fbdbdc8bba55487dbd6366352361fc Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 21 Mar 2017 12:54:06 -0400 Subject: [PATCH 388/562] make seq.extract rewrite type-generic --- src/ast/rewriter/seq_rewriter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ast/rewriter/seq_rewriter.cpp b/src/ast/rewriter/seq_rewriter.cpp index 4f99c6ae6..526d715dc 100644 --- a/src/ast/rewriter/seq_rewriter.cpp +++ b/src/ast/rewriter/seq_rewriter.cpp @@ -517,13 +517,13 @@ br_status seq_rewriter::mk_seq_extract(expr* a, expr* b, expr* c, expr_ref& resu // case 1: pos<0 or len<0 // rewrite to "" if ( (constantPos && pos.is_neg()) || (constantLen && len.is_neg()) ) { - result = m_util.str.mk_string(symbol("")); + result = m_util.str.mk_empty(m().get_sort(a)); return BR_DONE; } // case 1.1: pos >= length(base) // rewrite to "" if (constantBase && constantPos && pos.get_unsigned() >= s.length()) { - result = m_util.str.mk_string(symbol("")); + result = m_util.str.mk_empty(m().get_sort(a)); return BR_DONE; } From 25d839ed10b67353ed0ae065b87935f2b4d2d83c Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Wed, 22 Mar 2017 10:55:55 -0700 Subject: [PATCH 389/562] fix bug in simplifier of bv2int over concatentations exposed by #948 Signed-off-by: Nikolaj Bjorner --- src/ast/simplifier/bv_simplifier_plugin.cpp | 25 +++++++++++++++------ src/smt/theory_arith_core.h | 7 +++--- src/smt/theory_bv.cpp | 5 +++-- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/ast/simplifier/bv_simplifier_plugin.cpp b/src/ast/simplifier/bv_simplifier_plugin.cpp index 11ed1b9e0..a72e7e117 100644 --- a/src/ast/simplifier/bv_simplifier_plugin.cpp +++ b/src/ast/simplifier/bv_simplifier_plugin.cpp @@ -1517,13 +1517,24 @@ void bv_simplifier_plugin::mk_bv2int(expr * arg, sort* range, expr_ref & result) result = m_arith.mk_add(tmp1, tmp2); } // commented out to reproduce bug in reduction of int2bv/bv2int - else if (m_util.is_concat(arg)) { - expr_ref tmp1(m_manager), tmp2(m_manager); - unsigned sz2 = get_bv_size(to_app(arg)->get_arg(1)); - mk_bv2int(to_app(arg)->get_arg(0), range, tmp1); - mk_bv2int(to_app(arg)->get_arg(1), range, tmp2); - tmp1 = m_arith.mk_mul(m_arith.mk_numeral(power(numeral(2), sz2), true), tmp1); - result = m_arith.mk_add(tmp1, tmp2); + else if (m_util.is_concat(arg) && to_app(arg)->get_num_args() > 0) { + expr_ref_vector args(m_manager); + unsigned num_args = to_app(arg)->get_num_args(); + for (unsigned i = 0; i < num_args; ++i) { + expr_ref tmp(m_manager); + mk_bv2int(to_app(arg)->get_arg(i), range, tmp); + args.push_back(tmp); + } + unsigned sz = get_bv_size(to_app(arg)->get_arg(num_args-1)); + for (unsigned i = num_args - 1; i > 0; ) { + expr_ref tmp(m_manager); + --i; + tmp = args[i].get(); + tmp = m_arith.mk_mul(m_arith.mk_numeral(power(numeral(2), sz), true), tmp); + args[i] = tmp; + sz += get_bv_size(to_app(arg)->get_arg(i)); + } + result = m_arith.mk_add(args.size(), args.c_ptr()); } else { parameter parameter(range); diff --git a/src/smt/theory_arith_core.h b/src/smt/theory_arith_core.h index 513cf36a4..785e0120f 100644 --- a/src/smt/theory_arith_core.h +++ b/src/smt/theory_arith_core.h @@ -40,7 +40,7 @@ namespace smt { template void theory_arith::found_underspecified_op(app * n) { if (!m_found_underspecified_op) { - TRACE("arith", tout << "found non underspecificed expression:\n" << mk_pp(n, get_manager()) << "\n";); + TRACE("arith", tout << "found underspecificed expression:\n" << mk_pp(n, get_manager()) << "\n";); get_context().push_trail(value_trail(m_found_underspecified_op)); m_found_underspecified_op = true; } @@ -395,6 +395,7 @@ namespace smt { template theory_var theory_arith::internalize_div(app * n) { + if (!m_util.is_numeral(n->get_arg(1))) found_underspecified_op(n); found_underspecified_op(n); theory_var s = mk_binary_op(n); context & ctx = get_context(); @@ -418,7 +419,7 @@ namespace smt { template theory_var theory_arith::internalize_mod(app * n) { TRACE("arith_mod", tout << "internalizing...\n" << mk_pp(n, get_manager()) << "\n";); - found_underspecified_op(n); + if (!m_util.is_numeral(n->get_arg(1))) found_underspecified_op(n); theory_var s = mk_binary_op(n); context & ctx = get_context(); if (!ctx.relevancy()) @@ -428,7 +429,7 @@ namespace smt { template theory_var theory_arith::internalize_rem(app * n) { - found_underspecified_op(n); + if (!m_util.is_numeral(n->get_arg(1))) found_underspecified_op(n); theory_var s = mk_binary_op(n); context & ctx = get_context(); if (!ctx.relevancy()) { diff --git a/src/smt/theory_bv.cpp b/src/smt/theory_bv.cpp index a886c8a1e..ae2aa95e2 100644 --- a/src/smt/theory_bv.cpp +++ b/src/smt/theory_bv.cpp @@ -607,12 +607,13 @@ namespace smt { } expr_ref sum(m); arith_simp().mk_add(sz, args.c_ptr(), sum); + literal l(mk_eq(n, sum, false)); TRACE("bv", tout << mk_pp(n, m) << "\n"; tout << mk_pp(sum, m) << "\n"; + ctx.display_literal_verbose(tout, l); + tout << "\n"; ); - - literal l(mk_eq(n, sum, false)); ctx.mark_as_relevant(l); ctx.mk_th_axiom(get_id(), 1, &l); From e47e8c67c093f2832fd862a7b7a80716dd9d7ee7 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Wed, 22 Mar 2017 14:12:47 -0700 Subject: [PATCH 390/562] introducing scoped detacth/attach of clauses to enforce basic sat solver invariants. Part of investigating #939: Signed-off-by: Nikolaj Bjorner --- src/sat/sat_asymm_branch.cpp | 11 ++++------- src/sat/sat_elim_eqs.cpp | 2 +- src/sat/sat_solver.cpp | 24 ++++++++++++------------ src/sat/sat_solver.h | 27 +++++++++++++++++++++++---- src/util/small_object_allocator.cpp | 2 ++ 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/sat/sat_asymm_branch.cpp b/src/sat/sat_asymm_branch.cpp index 782713d5c..cca0c5c1b 100644 --- a/src/sat/sat_asymm_branch.cpp +++ b/src/sat/sat_asymm_branch.cpp @@ -96,7 +96,6 @@ namespace sat { if (!process(c)) continue; // clause was removed *it2 = *it; - // throw exception to test bug fix: if (it2 != it) throw solver_exception("trigger bug"); ++it2; } s.m_clauses.set_end(it2); @@ -129,14 +128,14 @@ namespace sat { // check if the clause is already satisfied for (i = 0; i < sz; i++) { if (s.value(c[i]) == l_true) { - s.dettach_clause(c); + s.detach_clause(c); s.del_clause(c); return false; } } // try asymmetric branching // clause must not be used for propagation - s.dettach_clause(c); + solver::scoped_detach scoped_d(s, c); s.push(); for (i = 0; i < sz - 1; i++) { literal l = c[i]; @@ -154,7 +153,6 @@ namespace sat { SASSERT(s.m_qhead == s.m_trail.size()); if (i == sz - 1) { // clause size can't be reduced. - s.attach_clause(c); return true; } // clause can be reduced @@ -189,18 +187,17 @@ namespace sat { TRACE("asymm_branch", tout << "produced unit clause: " << c[0] << "\n";); s.assign(c[0], justification()); s.propagate_core(false); - s.del_clause(c); + scoped_d.del_clause(); SASSERT(s.inconsistent() || s.m_qhead == s.m_trail.size()); return false; // check_missed_propagation() may fail, since m_clauses is not in a consistent state. case 2: SASSERT(s.value(c[0]) == l_undef && s.value(c[1]) == l_undef); s.mk_bin_clause(c[0], c[1], false); - s.del_clause(c); + scoped_d.del_clause(); SASSERT(s.m_qhead == s.m_trail.size()); return false; default: c.shrink(new_sz); - s.attach_clause(c); SASSERT(s.m_qhead == s.m_trail.size()); return true; } diff --git a/src/sat/sat_elim_eqs.cpp b/src/sat/sat_elim_eqs.cpp index 6a7ca6280..b7f83df6c 100644 --- a/src/sat/sat_elim_eqs.cpp +++ b/src/sat/sat_elim_eqs.cpp @@ -94,7 +94,7 @@ namespace sat { continue; } if (!c.frozen()) - m_solver.dettach_clause(c); + m_solver.detach_clause(c); // apply substitution for (i = 0; i < sz; i++) { SASSERT(!m_solver.was_eliminated(c[i].var())); diff --git a/src/sat/sat_solver.cpp b/src/sat/sat_solver.cpp index 57cdc2fb4..a66f82486 100644 --- a/src/sat/sat_solver.cpp +++ b/src/sat/sat_solver.cpp @@ -462,25 +462,25 @@ namespace sat { return simplify_clause_core(num_lits, lits); } - void solver::dettach_bin_clause(literal l1, literal l2, bool learned) { + void solver::detach_bin_clause(literal l1, literal l2, bool learned) { get_wlist(~l1).erase(watched(l2, learned)); get_wlist(~l2).erase(watched(l1, learned)); } - void solver::dettach_clause(clause & c) { + void solver::detach_clause(clause & c) { if (c.size() == 3) - dettach_ter_clause(c); + detach_ter_clause(c); else - dettach_nary_clause(c); + detach_nary_clause(c); } - void solver::dettach_nary_clause(clause & c) { + void solver::detach_nary_clause(clause & c) { clause_offset cls_off = get_offset(c); erase_clause_watch(get_wlist(~c[0]), cls_off); erase_clause_watch(get_wlist(~c[1]), cls_off); } - void solver::dettach_ter_clause(clause & c) { + void solver::detach_ter_clause(clause & c) { erase_ternary_watch(get_wlist(~c[0]), c[1], c[2]); erase_ternary_watch(get_wlist(~c[1]), c[0], c[2]); erase_ternary_watch(get_wlist(~c[2]), c[0], c[1]); @@ -1493,7 +1493,7 @@ namespace sat { for (unsigned i = new_sz; i < sz; i++) { clause & c = *(m_learned[i]); if (can_delete(c)) { - dettach_clause(c); + detach_clause(c); del_clause(c); } else { @@ -1551,7 +1551,7 @@ namespace sat { else { c.inc_inact_rounds(); if (c.inact_rounds() > m_config.m_gc_k) { - dettach_clause(c); + detach_clause(c); del_clause(c); m_stats.m_gc_clause++; deleted++; @@ -1562,7 +1562,7 @@ namespace sat { if (psm(c) > static_cast(c.size() * m_min_d_tk)) { // move to frozen; TRACE("sat_frozen", tout << "freezing size: " << c.size() << " psm: " << psm(c) << " " << c << "\n";); - dettach_clause(c); + detach_clause(c); c.reset_inact_rounds(); c.freeze(); m_num_frozen++; @@ -2595,7 +2595,7 @@ namespace sat { } else { clause & c = *(cw.get_clause()); - dettach_clause(c); + detach_clause(c); attach_clause(c, reinit); if (scope_lvl() > 0 && reinit) { // clause propagated literal, must keep it in the reinit stack. @@ -2628,7 +2628,7 @@ namespace sat { for (unsigned i = 0; i < clauses.size(); ++i) { clause & c = *(clauses[i]); if (c.contains(lit)) { - dettach_clause(c); + detach_clause(c); del_clause(c); } else { @@ -2646,7 +2646,7 @@ namespace sat { literal l1 = m_user_bin_clauses[i].first; literal l2 = m_user_bin_clauses[i].second; if (nlit == l1 || nlit == l2) { - dettach_bin_clause(l1, l2, learned); + detach_bin_clause(l1, l2, learned); } } } diff --git a/src/sat/sat_solver.h b/src/sat/sat_solver.h index f910e374f..6c91565aa 100644 --- a/src/sat/sat_solver.h +++ b/src/sat/sat_solver.h @@ -195,15 +195,34 @@ namespace sat { bool attach_nary_clause(clause & c); void attach_clause(clause & c, bool & reinit); void attach_clause(clause & c) { bool reinit; attach_clause(c, reinit); } + class scoped_detach { + solver& s; + clause& c; + bool m_deleted; + public: + scoped_detach(solver& s, clause& c): s(s), c(c), m_deleted(false) { + s.detach_clause(c); + } + ~scoped_detach() { + if (!m_deleted) s.attach_clause(c); + } + + void del_clause() { + if (!m_deleted) { + s.del_clause(c); + m_deleted = true; + } + } + }; unsigned select_watch_lit(clause const & cls, unsigned starting_at) const; unsigned select_learned_watch_lit(clause const & cls) const; bool simplify_clause(unsigned & num_lits, literal * lits) const; template bool simplify_clause_core(unsigned & num_lits, literal * lits) const; - void dettach_bin_clause(literal l1, literal l2, bool learned); - void dettach_clause(clause & c); - void dettach_nary_clause(clause & c); - void dettach_ter_clause(clause & c); + void detach_bin_clause(literal l1, literal l2, bool learned); + void detach_clause(clause & c); + void detach_nary_clause(clause & c); + void detach_ter_clause(clause & c); void push_reinit_stack(clause & c); // ----------------------- diff --git a/src/util/small_object_allocator.cpp b/src/util/small_object_allocator.cpp index aee84c1f0..60c85b660 100644 --- a/src/util/small_object_allocator.cpp +++ b/src/util/small_object_allocator.cpp @@ -70,6 +70,7 @@ void small_object_allocator::reset() { void small_object_allocator::deallocate(size_t size, void * p) { if (size == 0) return; + #if defined(Z3DEBUG) && !defined(_WINDOWS) // Valgrind friendly memory::deallocate(p); @@ -93,6 +94,7 @@ void small_object_allocator::deallocate(size_t size, void * p) { void * small_object_allocator::allocate(size_t size) { if (size == 0) return 0; + #if defined(Z3DEBUG) && !defined(_WINDOWS) // Valgrind friendly return memory::allocate(size); From 26ae3a5abb92a39ace36f3d28fd661283ec8e34f Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Wed, 22 Mar 2017 19:06:59 -0700 Subject: [PATCH 391/562] making simplifier code exception friendlier. Towards getting a handle on #939 Signed-off-by: Nikolaj Bjorner --- src/sat/sat_simplifier.cpp | 17 ++++++++++------- src/sat/sat_simplifier.h | 11 +++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/sat/sat_simplifier.cpp b/src/sat/sat_simplifier.cpp index e744bc007..007751220 100644 --- a/src/sat/sat_simplifier.cpp +++ b/src/sat/sat_simplifier.cpp @@ -168,14 +168,13 @@ namespace sat { m_need_cleanup = false; m_use_list.init(s.num_vars()); init_visited(); - bool learned_in_use_lists = false; + m_learned_in_use_lists = false; if (learned) { register_clauses(s.m_learned); - learned_in_use_lists = true; + m_learned_in_use_lists = true; } register_clauses(s.m_clauses); - if (!learned && (m_elim_blocked_clauses || m_elim_blocked_clauses_at == m_num_calls)) elim_blocked_clauses(); @@ -184,7 +183,9 @@ namespace sat { m_sub_counter = m_subsumption_limit; m_elim_counter = m_res_limit; - unsigned old_num_elim_vars = m_num_elim_vars; + m_old_num_elim_vars = m_num_elim_vars; + + scoped_finalize _scoped_finalize(*this); do { if (m_subsumption) @@ -199,20 +200,22 @@ namespace sat { break; } while (!m_sub_todo.empty()); + } - bool vars_eliminated = m_num_elim_vars > old_num_elim_vars; + void simplifier::scoped_finalize_fn() { + bool vars_eliminated = m_num_elim_vars > m_old_num_elim_vars; if (m_need_cleanup) { TRACE("after_simplifier", tout << "cleanning watches...\n";); cleanup_watches(); - cleanup_clauses(s.m_learned, true, vars_eliminated, learned_in_use_lists); + cleanup_clauses(s.m_learned, true, vars_eliminated, m_learned_in_use_lists); cleanup_clauses(s.m_clauses, false, vars_eliminated, true); } else { TRACE("after_simplifier", tout << "skipping cleanup...\n";); if (vars_eliminated) { // must remove learned clauses with eliminated variables - cleanup_clauses(s.m_learned, true, true, learned_in_use_lists); + cleanup_clauses(s.m_learned, true, true, m_learned_in_use_lists); } } CASSERT("sat_solver", s.check_invariant()); diff --git a/src/sat/sat_simplifier.h b/src/sat/sat_simplifier.h index 9ee239083..d26d0041f 100644 --- a/src/sat/sat_simplifier.h +++ b/src/sat/sat_simplifier.h @@ -91,6 +91,9 @@ namespace sat { unsigned m_num_sub_res; unsigned m_num_elim_lits; + bool m_learned_in_use_lists; + unsigned m_old_num_elim_vars; + struct size_lt { bool operator()(clause const * c1, clause const * c2) const { return c1->size() > c2->size(); } }; @@ -170,6 +173,14 @@ namespace sat { struct subsumption_report; struct elim_var_report; + class scoped_finalize { + simplifier& s; + public: + scoped_finalize(simplifier& s) : s(s) {} + ~scoped_finalize() { s.scoped_finalize_fn(); } + }; + void scoped_finalize_fn(); + public: simplifier(solver & s, params_ref const & p); ~simplifier(); From 1ab7ab9d744d5cddec94ae539d4d52233fedcb16 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Thu, 23 Mar 2017 11:09:36 -0700 Subject: [PATCH 392/562] fix double ownership of enode marking causing crash during tracing. Issue #952 Signed-off-by: Nikolaj Bjorner --- src/api/c++/z3++.h | 2 +- src/smt/smt_conflict_resolution.cpp | 6 +++--- src/smt/smt_context.cpp | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/api/c++/z3++.h b/src/api/c++/z3++.h index b6157f3ff..104f3ae6c 100644 --- a/src/api/c++/z3++.h +++ b/src/api/c++/z3++.h @@ -140,7 +140,7 @@ namespace z3 { class context { bool m_enable_exceptions; Z3_context m_ctx; - static void error_handler(Z3_context /*c*/, Z3_error_code /*e*/) { /* do nothing */ } + static void error_handler(Z3_context c, Z3_error_code e) { std::cout << "ex\n"; Z3_THROW(exception(Z3_get_error_msg(c, e))); std::cout << "unreach\n"; } void init(config & c) { m_ctx = Z3_mk_context_rc(c); m_enable_exceptions = true; diff --git a/src/smt/smt_conflict_resolution.cpp b/src/smt/smt_conflict_resolution.cpp index 7dd9144fe..8d90f9583 100644 --- a/src/smt/smt_conflict_resolution.cpp +++ b/src/smt/smt_conflict_resolution.cpp @@ -59,9 +59,9 @@ namespace smt { SASSERT(n->trans_reaches(n->get_root())); while (n) { if (Set) - n->set_mark(); + n->set_mark2(); else - n->unset_mark(); + n->unset_mark2(); n = n->m_trans.m_target; } } @@ -84,7 +84,7 @@ namespace smt { mark_enodes_in_trans(n1); while (true) { SASSERT(n2); - if (n2->is_marked()) { + if (n2->is_marked2()) { mark_enodes_in_trans(n1); return n2; } diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 9336322f7..6bc5cc6ab 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -4214,6 +4214,7 @@ namespace smt { for (unsigned i = 0; i < m_asserted_formulas.get_num_formulas(); ++i) { expr* e = m_asserted_formulas.get_formula(i); if (is_quantifier(e)) { + TRACE("context", tout << mk_pp(e, m) << "\n";); quantifier* q = to_quantifier(e); if (!m.is_rec_fun_def(q)) continue; SASSERT(q->get_num_patterns() == 1); From 62e87d647474e539ef9274e244869dac8615fa8c Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Thu, 23 Mar 2017 11:10:19 -0700 Subject: [PATCH 393/562] fix double ownership of enode marking causing crash during tracing. Issue #952 Signed-off-by: Nikolaj Bjorner --- src/api/c++/z3++.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/c++/z3++.h b/src/api/c++/z3++.h index 104f3ae6c..b6157f3ff 100644 --- a/src/api/c++/z3++.h +++ b/src/api/c++/z3++.h @@ -140,7 +140,7 @@ namespace z3 { class context { bool m_enable_exceptions; Z3_context m_ctx; - static void error_handler(Z3_context c, Z3_error_code e) { std::cout << "ex\n"; Z3_THROW(exception(Z3_get_error_msg(c, e))); std::cout << "unreach\n"; } + static void error_handler(Z3_context /*c*/, Z3_error_code /*e*/) { /* do nothing */ } void init(config & c) { m_ctx = Z3_mk_context_rc(c); m_enable_exceptions = true; From 37167a8dd607d27fb666222b63bfeef188de5789 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Thu, 23 Mar 2017 19:53:23 +0000 Subject: [PATCH 394/562] Fixed excessive trace output --- src/smt/smt_context_pp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/smt_context_pp.cpp b/src/smt/smt_context_pp.cpp index ff45c5089..74c759510 100644 --- a/src/smt/smt_context_pp.cpp +++ b/src/smt/smt_context_pp.cpp @@ -606,7 +606,7 @@ namespace smt { case b_justification::CLAUSE: { clause * cls = j.get_clause(); out << "clause "; - if (cls) display_literals_verbose(out, cls->get_num_literals(), cls->begin_literals()); + if (cls) display_literals(out, cls->get_num_literals(), cls->begin_literals()); break; } case b_justification::JUSTIFICATION: { From c56c7fd649a41e5b99a1ae150d059d895cd3cf69 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Fri, 24 Mar 2017 01:31:00 -0700 Subject: [PATCH 395/562] add handlers for dense difference logic Signed-off-by: Nikolaj Bjorner --- src/opt/opt_solver.cpp | 27 ++++++++++++++++++++++++++- src/smt/theory_dense_diff_logic_def.h | 2 ++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/opt/opt_solver.cpp b/src/opt/opt_solver.cpp index 8ae4e467f..bc6462a18 100644 --- a/src/opt/opt_solver.cpp +++ b/src/opt/opt_solver.cpp @@ -358,6 +358,7 @@ namespace opt { } smt::theory_opt& opt = get_optimizer(); smt::theory_var v = m_objective_vars[var]; + TRACE("opt", tout << "v" << var << " " << val << "\n";); if (typeid(smt::theory_inf_arith) == typeid(opt)) { smt::theory_inf_arith& th = dynamic_cast(opt); @@ -387,8 +388,32 @@ namespace opt { smt::theory_rdl& th = dynamic_cast(opt); return th.mk_ge(m_fm, v, val); } + + if (typeid(smt::theory_dense_i) == typeid(opt) && + val.get_infinitesimal().is_zero()) { + smt::theory_dense_i& th = dynamic_cast(opt); + return th.mk_ge(m_fm, v, val); + } - // difference logic? + if (typeid(smt::theory_dense_mi) == typeid(opt) && + val.get_infinitesimal().is_zero()) { + smt::theory_dense_mi& th = dynamic_cast(opt); + return th.mk_ge(m_fm, v, val); + } + + if (typeid(smt::theory_dense_si) == typeid(opt) && + val.get_infinitesimal().is_zero()) { + smt::theory_dense_si& th = dynamic_cast(opt); + return th.mk_ge(m_fm, v, val); + } + + if (typeid(smt::theory_dense_smi) == typeid(opt) && + val.get_infinitesimal().is_zero()) { + smt::theory_dense_smi& th = dynamic_cast(opt); + return th.mk_ge(m_fm, v, val); + } + + IF_VERBOSE(0, verbose_stream() << "WARNING: unhandled theory " << typeid(opt).name() << "\n";); return expr_ref(m.mk_true(), m); } diff --git a/src/smt/theory_dense_diff_logic_def.h b/src/smt/theory_dense_diff_logic_def.h index 628eeea83..ed94ee62c 100644 --- a/src/smt/theory_dense_diff_logic_def.h +++ b/src/smt/theory_dense_diff_logic_def.h @@ -1019,6 +1019,7 @@ namespace smt { template theory_var theory_dense_diff_logic::add_objective(app* term) { + TRACE("opt", tout << mk_pp(term, get_manager()) << "\n";); objective_term objective; theory_var result = m_objectives.size(); rational q(1), r(0); @@ -1053,6 +1054,7 @@ namespace smt { ast_manager& m = get_manager(); objective_term const& t = m_objectives[v]; expr_ref e(m), f(m), f2(m); + TRACE("opt", tout << "mk_ineq " << v << " " << val << "\n";); if (t.size() == 1 && t[0].second.is_one()) { f = get_enode(t[0].first)->get_owner(); } From ec4770622690fc29b2eb0557b7d644651a92b90c Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Fri, 24 Mar 2017 02:23:50 -0700 Subject: [PATCH 396/562] fix constant offset and handling of ite in difference logic optimizer code-path. Issue #946 Signed-off-by: Nikolaj Bjorner --- src/opt/opt_context.cpp | 7 ++++++- src/smt/theory_dense_diff_logic_def.h | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/opt/opt_context.cpp b/src/opt/opt_context.cpp index cd40944b2..d1b7a489e 100644 --- a/src/opt/opt_context.cpp +++ b/src/opt/opt_context.cpp @@ -273,7 +273,8 @@ namespace opt { display_benchmark(); IF_VERBOSE(1, verbose_stream() << "(optimize:check-sat)\n";); lbool is_sat = s.check_sat(0,0); - TRACE("opt", tout << "initial search result: " << is_sat << "\n";); + TRACE("opt", tout << "initial search result: " << is_sat << "\n"; + s.display(tout);); if (is_sat != l_false) { s.get_model(m_model); s.get_labels(m_labels); @@ -1037,6 +1038,10 @@ namespace opt { TRACE("opt", tout << "Purifying " << term << "\n";); term = purify(fm, term); } + else if (m.is_ite(term)) { + TRACE("opt", tout << "Purifying " << term << "\n";); + term = purify(fm, term); + } if (fm) { m_model_converter = concat(m_model_converter.get(), fm.get()); } diff --git a/src/smt/theory_dense_diff_logic_def.h b/src/smt/theory_dense_diff_logic_def.h index ed94ee62c..addb5d92b 100644 --- a/src/smt/theory_dense_diff_logic_def.h +++ b/src/smt/theory_dense_diff_logic_def.h @@ -868,7 +868,8 @@ namespace smt { e = ctx.get_enode(to_app(n)); } else { - e = ctx.mk_enode(to_app(n), false, false, true); + ctx.internalize(n, false); + e = ctx.get_enode(n); } v = e->get_th_var(get_id()); if (v == null_theory_var) { @@ -1008,7 +1009,8 @@ namespace smt { inf_eps result(rational(0), r); blocker = mk_gt(v, result); IF_VERBOSE(10, verbose_stream() << blocker << "\n";); - return result; + r += m_objective_consts[v]; + return inf_eps(rational(0), r); } default: TRACE("opt", tout << "unbounded\n"; ); From 866035d786ca69860359d7833a6c15108f7c0740 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Fri, 24 Mar 2017 09:40:18 +0000 Subject: [PATCH 397/562] Disabled debug output --- src/smt/smt_context_pp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/smt_context_pp.cpp b/src/smt/smt_context_pp.cpp index 74c759510..73d822fb4 100644 --- a/src/smt/smt_context_pp.cpp +++ b/src/smt/smt_context_pp.cpp @@ -613,7 +613,7 @@ namespace smt { out << "justification "; literal_vector lits; const_cast(*m_conflict_resolution).justification2literals(j.get_justification(), lits); - display_literals_verbose(out, lits.size(), lits.c_ptr()); + display_literals(out, lits.size(), lits.c_ptr()); break; } default: From e9cd4d10570c7e035489841ebadc9ce08df8a1a9 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Fri, 24 Mar 2017 11:51:36 +0000 Subject: [PATCH 398/562] Build fix for systems that don't come with SSE4.1 support by default --- src/util/hwf.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/util/hwf.cpp b/src/util/hwf.cpp index f84f7fe40..ac39db71a 100644 --- a/src/util/hwf.cpp +++ b/src/util/hwf.cpp @@ -52,8 +52,10 @@ Revision History: #ifdef USE_INTRINSICS #include +#ifdef __SSE4_1__ #include #endif +#endif hwf_manager::hwf_manager() : m_mpz_manager(m_mpq_manager) From 7f9c37e19d4bab0eaeaed586513c812f271603f5 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Fri, 24 Mar 2017 14:23:39 +0000 Subject: [PATCH 399/562] VS2017 SSE4 intrinsics build fix --- src/util/hwf.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/util/hwf.cpp b/src/util/hwf.cpp index ac39db71a..f8e4ff69b 100644 --- a/src/util/hwf.cpp +++ b/src/util/hwf.cpp @@ -52,10 +52,8 @@ Revision History: #ifdef USE_INTRINSICS #include -#ifdef __SSE4_1__ #include #endif -#endif hwf_manager::hwf_manager() : m_mpz_manager(m_mpq_manager) @@ -306,7 +304,9 @@ void hwf_manager::round_to_integral(mpf_rounding_mode rm, hwf const & x, hwf & o // According to the Intel Architecture manual, the x87-instrunction FRNDINT is the // same in 32-bit and 64-bit mode. The _mm_round_* intrinsics are SSE4 extensions. #ifdef _WINDOWS -#ifdef USE_INTRINSICS +#if defined(USE_INTRINSICS) && \ + (defined(_WINDOWS) && defined(__AVX__)) || \ + (!defined(_WINDOWS) && defined(__SSE4_1__) ) switch (rm) { case 0: _mm_store_sd(&o.value, _mm_round_pd(_mm_set_sd(x.value), _MM_FROUND_TO_NEAREST_INT)); break; case 2: _mm_store_sd(&o.value, _mm_round_pd(_mm_set_sd(x.value), _MM_FROUND_TO_POS_INF)); break; From d10dec2218657023d8adcfc0a1b5e6c4a61aba97 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Fri, 24 Mar 2017 14:31:06 +0000 Subject: [PATCH 400/562] Removed unused variable --- src/ast/rewriter/rewriter_def.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ast/rewriter/rewriter_def.h b/src/ast/rewriter/rewriter_def.h index aecc1c93a..76f149df7 100644 --- a/src/ast/rewriter/rewriter_def.h +++ b/src/ast/rewriter/rewriter_def.h @@ -189,10 +189,9 @@ bool rewriter_tpl::constant_fold(app * t, frame & fr) { result_stack().shrink(fr.m_spos); result_stack().push_back(arg); fr.m_state = REWRITE_BUILTIN; - unsigned max_depth = fr.m_max_depth; if (visit(arg, fr.m_max_depth)) { m_r = result_stack().back(); - result_stack().pop_back(); + result_stack().pop_back(); result_stack().pop_back(); result_stack().push_back(m_r); cache_result(t, m_r, m_pr, fr.m_cache_result); From 0399e5e2d339505a9b34a66358133ef872549077 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Fri, 24 Mar 2017 14:49:24 +0000 Subject: [PATCH 401/562] Fixed variable initialization warning --- src/sat/sat_solver.cpp | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/sat/sat_solver.cpp b/src/sat/sat_solver.cpp index a66f82486..9c858a29a 100644 --- a/src/sat/sat_solver.cpp +++ b/src/sat/sat_solver.cpp @@ -101,7 +101,7 @@ namespace sat { if (!it->is_binary_non_learned_clause()) continue; literal l2 = it->get_literal(); - if (l.index() > l2.index()) + if (l.index() > l2.index()) continue; mk_clause_core(l, l2); } @@ -223,7 +223,7 @@ namespace sat { if (propagate_bin_clause(l1, l2)) { if (scope_lvl() == 0) return; - if (!learned) + if (!learned) m_clauses_to_reinit.push_back(clause_wrapper(l1, l2)); } m_stats.m_mk_bin_clause++; @@ -248,7 +248,7 @@ namespace sat { void solver::push_reinit_stack(clause & c) { TRACE("sat_reinit", tout << "adding to reinit stack: " << c << "\n";); m_clauses_to_reinit.push_back(clause_wrapper(c)); - c.set_reinit_stack(true); + c.set_reinit_stack(true); } @@ -257,7 +257,7 @@ namespace sat { clause * r = m_cls_allocator.mk_clause(3, lits, learned); bool reinit = attach_ter_clause(*r); if (reinit && !learned) push_reinit_stack(*r); - + if (learned) m_learned.push_back(r); else @@ -806,22 +806,22 @@ namespace sat { m_params.set_uint("random_seed", m_rand()); if (i == 1 + num_threads/2) { m_params.set_sym("phase", symbol("random")); - } + } solvers[i] = alloc(sat::solver, m_params, rlims[i], 0); solvers[i]->copy(*this); solvers[i]->set_par(&par); - scoped_rlimit.push_child(&solvers[i]->rlimit()); + scoped_rlimit.push_child(&solvers[i]->rlimit()); } set_par(&par); m_params.set_sym("phase", saved_phase); int finished_id = -1; std::string ex_msg; - par_exception_kind ex_kind; + par_exception_kind ex_kind = DEFAULT_EX; unsigned error_code = 0; lbool result = l_undef; #pragma omp parallel for for (int i = 0; i < num_threads; ++i) { - try { + try { lbool r = l_undef; if (i < num_extra_solvers) { r = solvers[i]->check(num_lits, lits); @@ -851,7 +851,7 @@ namespace sat { rlims[j].cancel(); } } - } + } } catch (z3_error & err) { if (i == 0) { @@ -871,7 +871,7 @@ namespace sat { m_stats = solvers[finished_id]->m_stats; } - for (int i = 0; i < num_extra_solvers; ++i) { + for (int i = 0; i < num_extra_solvers; ++i) { dealloc(solvers[i]); } if (finished_id == -1) { @@ -1140,7 +1140,7 @@ namespace sat { for (unsigned i = 0; !inconsistent() && i < m_assumptions.size(); ++i) { assign(m_assumptions[i], justification()); } - TRACE("sat", + TRACE("sat", for (unsigned i = 0; i < m_assumptions.size(); ++i) { index_set s; if (m_antecedents.find(m_assumptions[i].var(), s)) { @@ -2037,7 +2037,7 @@ namespace sat { } } - literal consequent = m_not_l; + literal consequent = m_not_l; justification js = m_conflict; @@ -3115,7 +3115,7 @@ namespace sat { literal_pair p(l1, l2); if (!seen_bc.contains(p)) { seen_bc.insert(p); - mc.add_edge(l1.index(), l2.index()); + mc.add_edge(l1.index(), l2.index()); } } vector _mutexes; @@ -3168,7 +3168,7 @@ namespace sat { } void solver::fixup_consequence_core() { - index_set s; + index_set s; TRACE("sat", tout << m_core << "\n";); for (unsigned i = 0; i < m_core.size(); ++i) { TRACE("sat", tout << m_core[i] << ": "; display_index_set(tout, m_antecedents.find(m_core[i].var())) << "\n";); @@ -3218,20 +3218,20 @@ namespace sat { while (true) { ++num_iterations; SASSERT(!inconsistent()); - + lbool r = bounded_search(); if (r != l_undef) { fixup_consequence_core(); return r; } - + extract_fixed_consequences(num_units, asms, unfixed_vars, conseq); if (m_conflicts > m_config.m_max_conflicts) { IF_VERBOSE(SAT_VB_LVL, verbose_stream() << "(sat \"abort: max-conflicts = " << m_conflicts << "\")\n";); return l_undef; } - + restart(); simplify_problem(); if (check_inconsistent()) { @@ -3239,11 +3239,11 @@ namespace sat { return l_false; } gc(); - + if (m_config.m_restart_max <= num_iterations) { IF_VERBOSE(SAT_VB_LVL, verbose_stream() << "(sat \"abort: max-restarts\")\n";); return l_undef; - } + } } } From fb105afac2ed8ec35e6f5c9706d8c596b95a06d7 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Fri, 24 Mar 2017 15:22:33 +0000 Subject: [PATCH 402/562] Windows build fix --- src/util/hwf.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/hwf.cpp b/src/util/hwf.cpp index f8e4ff69b..5572ee252 100644 --- a/src/util/hwf.cpp +++ b/src/util/hwf.cpp @@ -305,8 +305,8 @@ void hwf_manager::round_to_integral(mpf_rounding_mode rm, hwf const & x, hwf & o // same in 32-bit and 64-bit mode. The _mm_round_* intrinsics are SSE4 extensions. #ifdef _WINDOWS #if defined(USE_INTRINSICS) && \ - (defined(_WINDOWS) && defined(__AVX__)) || \ - (!defined(_WINDOWS) && defined(__SSE4_1__) ) + (defined(_WINDOWS) && (defined(__AVX__) || defined(_M_X64))) || \ + (!defined(_WINDOWS) && defined(__SSE4_1__)) switch (rm) { case 0: _mm_store_sd(&o.value, _mm_round_pd(_mm_set_sd(x.value), _MM_FROUND_TO_NEAREST_INT)); break; case 2: _mm_store_sd(&o.value, _mm_round_pd(_mm_set_sd(x.value), _MM_FROUND_TO_POS_INF)); break; From f8d022a18060ac6abe6429c7e6e79ecdaf15fc02 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Fri, 24 Mar 2017 15:25:18 +0000 Subject: [PATCH 403/562] Non-windows build fix --- src/util/hwf.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/util/hwf.cpp b/src/util/hwf.cpp index 5572ee252..e577e15df 100644 --- a/src/util/hwf.cpp +++ b/src/util/hwf.cpp @@ -52,8 +52,10 @@ Revision History: #ifdef USE_INTRINSICS #include +#if defined(_MSC_VER) || defined(__SSE4_1__) #include #endif +#endif hwf_manager::hwf_manager() : m_mpz_manager(m_mpq_manager) From 3bbe5eceeb0e0a51c6d11d7a2ba2d54fb6d52dbc Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Fri, 24 Mar 2017 15:53:46 +0000 Subject: [PATCH 404/562] fix for --get-describe --- scripts/mk_util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/mk_util.py b/scripts/mk_util.py index 17ab8dea0..f05250ae7 100644 --- a/scripts/mk_util.py +++ b/scripts/mk_util.py @@ -2685,8 +2685,8 @@ def get_full_version_string(major, minor, build, revision): if GIT_HASH: res += " " + GIT_HASH if GIT_DESCRIBE: - branch = check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD', '--long']) - res += " master " + check_output(['git', 'describe']) + branch = check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']) + res += " " + branch + " " + check_output(['git', 'describe']) return '"' + res + '"' # Update files with the version number From e05cee757ba715aa3631a32ea4b8ee92cede2d7c Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Fri, 24 Mar 2017 10:10:42 -0700 Subject: [PATCH 405/562] properly handle recursive function definitions #898 Signed-off-by: Nikolaj Bjorner --- src/cmd_context/cmd_context.cpp | 7 +++++-- src/sat/sat_simplifier.cpp | 1 + src/smt/smt_context.cpp | 32 +++++--------------------------- src/smt/smt_context.h | 2 -- src/smt/smt_model_checker.cpp | 2 +- 5 files changed, 12 insertions(+), 32 deletions(-) diff --git a/src/cmd_context/cmd_context.cpp b/src/cmd_context/cmd_context.cpp index 2551f0aa0..7060d79ad 100644 --- a/src/cmd_context/cmd_context.cpp +++ b/src/cmd_context/cmd_context.cpp @@ -739,8 +739,11 @@ void cmd_context::insert_rec_fun(func_decl* f, expr_ref_vector const& binding, s lhs = m().mk_app(f, binding.size(), binding.c_ptr()); eq = m().mk_eq(lhs, e); if (!ids.empty()) { - expr* pat = m().mk_pattern(lhs); - eq = m().mk_forall(ids.size(), f->get_domain(), ids.c_ptr(), eq, 0, m().rec_fun_qid(), symbol::null, 1, &pat); + if (!is_app(e)) { + throw cmd_exception("Z3 only supports recursive definitions that are proper terms (not binders or variables)"); + } + expr* pats[2] = { m().mk_pattern(lhs), m().mk_pattern(to_app(e)) }; + eq = m().mk_forall(ids.size(), f->get_domain(), ids.c_ptr(), eq, 0, m().rec_fun_qid(), symbol::null, 2, pats); } // diff --git a/src/sat/sat_simplifier.cpp b/src/sat/sat_simplifier.cpp index 007751220..8b753fb67 100644 --- a/src/sat/sat_simplifier.cpp +++ b/src/sat/sat_simplifier.cpp @@ -149,6 +149,7 @@ namespace sat { } void simplifier::operator()(bool learned) { + std::cout << s.rlimit().count() << "\n"; if (s.inconsistent()) return; if (!m_subsumption && !m_elim_blocked_clauses && !m_resolution) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 6bc5cc6ab..f1b043556 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -4217,40 +4217,18 @@ namespace smt { TRACE("context", tout << mk_pp(e, m) << "\n";); quantifier* q = to_quantifier(e); if (!m.is_rec_fun_def(q)) continue; - SASSERT(q->get_num_patterns() == 1); + SASSERT(q->get_num_patterns() == 2); expr* fn = to_app(q->get_pattern(0))->get_arg(0); + expr* body = to_app(q->get_pattern(1))->get_arg(0); SASSERT(is_app(fn)); func_decl* f = to_app(fn)->get_decl(); - expr* eq = q->get_expr(); - expr_ref body(m); - if (is_fun_def(fn, q->get_expr(), body)) { - func_interp* fi = alloc(func_interp, m, f->get_arity()); - fi->set_else(body); - m_model->register_decl(f, fi); - } + func_interp* fi = alloc(func_interp, m, f->get_arity()); + fi->set_else(body); + m_model->register_decl(f, fi); } } } - bool context::is_fun_def(expr* f, expr* body, expr_ref& result) { - expr* t1, *t2, *t3; - if (m_manager.is_eq(body, t1, t2) || m_manager.is_iff(body, t1, t2)) { - if (t1 == f) return result = t2, true; - if (t2 == f) return result = t1, true; - return false; - } - if (m_manager.is_ite(body, t1, t2, t3)) { - expr_ref body1(m_manager), body2(m_manager); - if (is_fun_def(f, t2, body1) && is_fun_def(f, t3, body2)) { - // f is not free in t1 - result = m_manager.mk_ite(t1, body1, body2); - return true; - } - } - return false; - } - - }; diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index 2a555e6b5..1f57a7550 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -1167,8 +1167,6 @@ namespace smt { void add_rec_funs_to_model(); - bool is_fun_def(expr* f, expr* q, expr_ref& body); - public: bool can_propagate() const; diff --git a/src/smt/smt_model_checker.cpp b/src/smt/smt_model_checker.cpp index 093d215b6..dfdb035c5 100644 --- a/src/smt/smt_model_checker.cpp +++ b/src/smt/smt_model_checker.cpp @@ -318,7 +318,7 @@ namespace smt { bool model_checker::check_rec_fun(quantifier* q) { TRACE("model_checker", tout << mk_pp(q, m) << "\n";); - SASSERT(q->get_num_patterns() == 1); + SASSERT(q->get_num_patterns() == 2); // first pattern is the function, second is the body. expr* fn = to_app(q->get_pattern(0))->get_arg(0); SASSERT(is_app(fn)); func_decl* f = to_app(fn)->get_decl(); From 723b507a887499517c69182947a2c611f7487edb Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Fri, 24 Mar 2017 10:11:39 -0700 Subject: [PATCH 406/562] properly handle recursive function definitions #898 Signed-off-by: Nikolaj Bjorner --- src/sat/sat_simplifier.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/sat/sat_simplifier.cpp b/src/sat/sat_simplifier.cpp index 8b753fb67..007751220 100644 --- a/src/sat/sat_simplifier.cpp +++ b/src/sat/sat_simplifier.cpp @@ -149,7 +149,6 @@ namespace sat { } void simplifier::operator()(bool learned) { - std::cout << s.rlimit().count() << "\n"; if (s.inconsistent()) return; if (!m_subsumption && !m_elim_blocked_clauses && !m_resolution) From 3a9857940e21e6978ba7a75548b6cf26c4707386 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sat, 25 Mar 2017 19:31:01 +0100 Subject: [PATCH 407/562] add missing axioms for str.at. Issue #953 Signed-off-by: Nikolaj Bjorner --- src/ast/bv_decl_plugin.h | 10 +++++----- src/ast/macros/macro_util.cpp | 36 ++++++++++++++--------------------- src/ast/macros/macro_util.h | 3 +-- src/smt/theory_seq.cpp | 15 +++++++++++++++ 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/ast/bv_decl_plugin.h b/src/ast/bv_decl_plugin.h index ac0ff7f79..33cf094b9 100644 --- a/src/ast/bv_decl_plugin.h +++ b/src/ast/bv_decl_plugin.h @@ -406,11 +406,11 @@ public: app * mk_bv_not(expr * arg) { return m_manager.mk_app(get_fid(), OP_BNOT, arg); } app * mk_bv_xor(unsigned num, expr * const * args) { return m_manager.mk_app(get_fid(), OP_BXOR, num, args); } app * mk_bv_neg(expr * arg) { return m_manager.mk_app(get_fid(), OP_BNEG, arg); } - app * mk_bv_urem(expr * arg1, expr * arg2) { return m_manager.mk_app(get_fid(), OP_BUREM, arg1, arg2); } - app * mk_bv_srem(expr * arg1, expr * arg2) { return m_manager.mk_app(get_fid(), OP_BSREM, arg1, arg2); } - app * mk_bv_add(expr * arg1, expr * arg2) { return m_manager.mk_app(get_fid(), OP_BADD, arg1, arg2); } - app * mk_bv_sub(expr * arg1, expr * arg2) { return m_manager.mk_app(get_fid(), OP_BSUB, arg1, arg2); } - app * mk_bv_mul(expr * arg1, expr * arg2) { return m_manager.mk_app(get_fid(), OP_BMUL, arg1, arg2); } + app * mk_bv_urem(expr * arg1, expr * arg2) const { return m_manager.mk_app(get_fid(), OP_BUREM, arg1, arg2); } + app * mk_bv_srem(expr * arg1, expr * arg2) const { return m_manager.mk_app(get_fid(), OP_BSREM, arg1, arg2); } + app * mk_bv_add(expr * arg1, expr * arg2) const { return m_manager.mk_app(get_fid(), OP_BADD, arg1, arg2); } + app * mk_bv_sub(expr * arg1, expr * arg2) const { return m_manager.mk_app(get_fid(), OP_BSUB, arg1, arg2); } + app * mk_bv_mul(expr * arg1, expr * arg2) const { return m_manager.mk_app(get_fid(), OP_BMUL, arg1, arg2); } app * mk_zero_extend(unsigned n, expr* e) { parameter p(n); return m_manager.mk_app(get_fid(), OP_ZERO_EXT, 1, &p, 1, &e); diff --git a/src/ast/macros/macro_util.cpp b/src/ast/macros/macro_util.cpp index 99732871c..fce6f1b28 100644 --- a/src/ast/macros/macro_util.cpp +++ b/src/ast/macros/macro_util.cpp @@ -19,22 +19,22 @@ Revision History: --*/ #include"macro_util.h" #include"occurs.h" +#include"ast_util.h" #include"arith_simplifier_plugin.h" -#include"basic_simplifier_plugin.h" #include"bv_simplifier_plugin.h" #include"var_subst.h" #include"ast_pp.h" #include"ast_ll_pp.h" -#include"ast_util.h" #include"for_each_expr.h" #include"well_sorted.h" +#include"bool_rewriter.h" macro_util::macro_util(ast_manager & m, simplifier & s): m_manager(m), + m_bv(m), m_simplifier(s), m_arith_simp(0), m_bv_simp(0), - m_basic_simp(0), m_forbidden_set(0), m_curr_clause(0) { } @@ -55,24 +55,17 @@ bv_simplifier_plugin * macro_util::get_bv_simp() const { return m_bv_simp; } -basic_simplifier_plugin * macro_util::get_basic_simp() const { - if (m_basic_simp == 0) { - const_cast(this)->m_basic_simp = static_cast(m_simplifier.get_plugin(m_manager.get_basic_family_id())); - } - SASSERT(m_basic_simp != 0); - return m_basic_simp; -} bool macro_util::is_bv(expr * n) const { - return get_bv_simp()->is_bv(n); + return m_bv.is_bv(n); } bool macro_util::is_bv_sort(sort * s) const { - return get_bv_simp()->is_bv_sort(s); + return m_bv.is_bv_sort(s); } bool macro_util::is_add(expr * n) const { - return get_arith_simp()->is_add(n) || get_bv_simp()->is_add(n); + return get_arith_simp()->is_add(n) || m_bv.is_bv_add(n); } bool macro_util::is_times_minus_one(expr * n, expr * & arg) const { @@ -80,11 +73,11 @@ bool macro_util::is_times_minus_one(expr * n, expr * & arg) const { } bool macro_util::is_le(expr * n) const { - return get_arith_simp()->is_le(n) || get_bv_simp()->is_le(n); + return get_arith_simp()->is_le(n) || m_bv.is_bv_ule(n) || m_bv.is_bv_sle(n); } bool macro_util::is_le_ge(expr * n) const { - return get_arith_simp()->is_le_ge(n) || get_bv_simp()->is_le_ge(n); + return get_arith_simp()->is_le_ge(n) || m_bv.is_bv_ule(n) || m_bv.is_bv_sle(n); } poly_simplifier_plugin * macro_util::get_poly_simp_for(sort * s) const { @@ -102,7 +95,7 @@ app * macro_util::mk_zero(sort * s) const { void macro_util::mk_sub(expr * t1, expr * t2, expr_ref & r) const { if (is_bv(t1)) { - get_bv_simp()->mk_sub(t1, t2, r); + r = m_bv.mk_bv_sub(t1, t2); } else { get_arith_simp()->mk_sub(t1, t2, r); @@ -111,7 +104,7 @@ void macro_util::mk_sub(expr * t1, expr * t2, expr_ref & r) const { void macro_util::mk_add(expr * t1, expr * t2, expr_ref & r) const { if (is_bv(t1)) { - get_bv_simp()->mk_add(t1, t2, r); + r = m_bv.mk_bv_add(t1, t2); } else { get_arith_simp()->mk_add(t1, t2, r); @@ -429,7 +422,7 @@ void macro_util::quasi_macro_head_to_macro_head(app * qhead, unsigned & num_decl new_args.push_back(new_var); new_conds.push_back(new_cond); } - get_basic_simp()->mk_and(new_conds.size(), new_conds.c_ptr(), cond); + bool_rewriter(m_manager).mk_and(new_conds.size(), new_conds.c_ptr(), cond); head = m_manager.mk_app(qhead->get_decl(), new_args.size(), new_args.c_ptr()); num_decls = next_var_idx; } @@ -687,7 +680,7 @@ void macro_util::insert_quasi_macro(app * head, unsigned num_decls, expr * def, if (cond == 0) new_cond = extra_cond; else - get_basic_simp()->mk_and(cond, extra_cond, new_cond); + bool_rewriter(m_manager).mk_and(cond, extra_cond, new_cond); } else { hint_to_macro_head(m_manager, head, num_decls, new_head); @@ -719,20 +712,19 @@ void macro_util::get_rest_clause_as_cond(expr * except_lit, expr_ref & extra_con if (m_curr_clause == 0) return; SASSERT(is_clause(m_manager, m_curr_clause)); - basic_simplifier_plugin * bs = get_basic_simp(); expr_ref_buffer neg_other_lits(m_manager); unsigned num_lits = get_clause_num_literals(m_manager, m_curr_clause); for (unsigned i = 0; i < num_lits; i++) { expr * l = get_clause_literal(m_manager, m_curr_clause, i); if (l != except_lit) { expr_ref neg_l(m_manager); - bs->mk_not(l, neg_l); + bool_rewriter(m_manager).mk_not(l, neg_l); neg_other_lits.push_back(neg_l); } } if (neg_other_lits.empty()) return; - get_basic_simp()->mk_and(neg_other_lits.size(), neg_other_lits.c_ptr(), extra_cond); + bool_rewriter(m_manager).mk_and(neg_other_lits.size(), neg_other_lits.c_ptr(), extra_cond); } void macro_util::collect_poly_args(expr * n, expr * exception, ptr_buffer & args) { diff --git a/src/ast/macros/macro_util.h b/src/ast/macros/macro_util.h index 033f6ecb4..8aa8e550e 100644 --- a/src/ast/macros/macro_util.h +++ b/src/ast/macros/macro_util.h @@ -62,10 +62,10 @@ public: private: ast_manager & m_manager; + bv_util m_bv; simplifier & m_simplifier; arith_simplifier_plugin * m_arith_simp; bv_simplifier_plugin * m_bv_simp; - basic_simplifier_plugin * m_basic_simp; obj_hashtable * m_forbidden_set; bool is_forbidden(func_decl * f) const { return m_forbidden_set != 0 && m_forbidden_set->contains(f); } @@ -99,7 +99,6 @@ public: arith_simplifier_plugin * get_arith_simp() const; bv_simplifier_plugin * get_bv_simp() const; - basic_simplifier_plugin * get_basic_simp() const; bool is_macro_head(expr * n, unsigned num_decls) const; bool is_left_simple_macro(expr * n, unsigned num_decls, app_ref & head, expr_ref & def) const; diff --git a/src/smt/theory_seq.cpp b/src/smt/theory_seq.cpp index daf5e3702..663d4cbe1 100644 --- a/src/smt/theory_seq.cpp +++ b/src/smt/theory_seq.cpp @@ -1981,6 +1981,7 @@ bool theory_seq::solve_nc(unsigned idx) { } if (c != n.contains()) { m_ncs.push_back(nc(c, deps)); + m_new_propagation = true; return true; } return false; @@ -2403,6 +2404,14 @@ void theory_seq::display(std::ostream & out) const { } } + if (!m_ncs.empty()) { + out << "Non contains:\n"; + for (unsigned i = 0; i < m_ncs.size(); ++i) { + out << "not " << mk_pp(m_ncs[i].contains(), m) << "\n"; + display_deps(out << " <- ", m_ncs[i].deps()); out << "\n"; + } + } + } void theory_seq::display_equations(std::ostream& out) const { @@ -3496,6 +3505,7 @@ void theory_seq::add_extract_suffix_axiom(expr* e, expr* s, expr* i) { let e = at(s, i) 0 <= i < len(s) -> s = xey & len(x) = i & len(e) = 1 + i < 0 \/ i >= len(s) -> e = empty */ void theory_seq::add_at_axiom(expr* e) { @@ -3509,13 +3519,18 @@ void theory_seq::add_at_axiom(expr* e) { expr_ref y = mk_skolem(m_post, s, mk_sub(mk_sub(len_s, i), one)); expr_ref xey = mk_concat(x, e, y); expr_ref len_x(m_util.str.mk_length(x), m); + expr_ref emp(m_util.str.mk_empty(m.get_sort(e)), m); literal i_ge_0 = mk_literal(m_autil.mk_ge(i, zero)); literal i_ge_len_s = mk_literal(m_autil.mk_ge(mk_sub(i, m_util.str.mk_length(s)), zero)); + add_axiom(~i_ge_0, i_ge_len_s, mk_seq_eq(s, xey)); add_axiom(~i_ge_0, i_ge_len_s, mk_eq(one, len_e, false)); add_axiom(~i_ge_0, i_ge_len_s, mk_eq(i, len_x, false)); + + add_axiom(i_ge_0, mk_eq(s, emp, false)); + add_axiom(~i_ge_len_s, mk_eq(s, emp, false)); } /** From 041520f72753f7c4283ccc9bc133df0da9f0d080 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Tue, 28 Mar 2017 18:17:22 +0100 Subject: [PATCH 408/562] SMT2 compliancy fix; NRA includes conversion of Int numerals --- src/ast/arith_decl_plugin.cpp | 59 ++++++++++++++++++++--------------- src/ast/arith_decl_plugin.h | 2 ++ 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/ast/arith_decl_plugin.cpp b/src/ast/arith_decl_plugin.cpp index 546f037de..1c01496cf 100644 --- a/src/ast/arith_decl_plugin.cpp +++ b/src/ast/arith_decl_plugin.cpp @@ -35,7 +35,7 @@ struct arith_decl_plugin::algebraic_numbers_wrapper { ~algebraic_numbers_wrapper() { } - + unsigned mk_id(algebraic_numbers::anum const & val) { SASSERT(!m_amanager.is_rational(val)); unsigned new_id = m_id_gen.mk(); @@ -121,7 +121,7 @@ void arith_decl_plugin::set_manager(ast_manager * m, family_id id) { m_int_decl = m->mk_sort(symbol("Int"), sort_info(id, INT_SORT)); m->inc_ref(m_int_decl); sort * i = m_int_decl; - + sort * b = m->mk_bool_sort(); #define MK_PRED(FIELD, NAME, KIND, SORT) { \ @@ -140,7 +140,7 @@ void arith_decl_plugin::set_manager(ast_manager * m, family_id id) { MK_PRED(m_i_ge_decl, ">=", OP_GE, i); MK_PRED(m_i_lt_decl, "<", OP_LT, i); MK_PRED(m_i_gt_decl, ">", OP_GT, i); - + #define MK_AC_OP(FIELD, NAME, KIND, SORT) { \ func_decl_info info(id, KIND); \ info.set_associative(); \ @@ -205,7 +205,7 @@ void arith_decl_plugin::set_manager(ast_manager * m, family_id id) { MK_UNARY(m_asinh_decl, "asinh", OP_ASINH, r); MK_UNARY(m_acosh_decl, "acosh", OP_ACOSH, r); MK_UNARY(m_atanh_decl, "atanh", OP_ATANH, r); - + func_decl * pi_decl = m->mk_const_decl(symbol("pi"), r, func_decl_info(id, OP_PI)); m_pi = m->mk_const(pi_decl); m->inc_ref(m_pi); @@ -213,7 +213,7 @@ void arith_decl_plugin::set_manager(ast_manager * m, family_id id) { func_decl * e_decl = m->mk_const_decl(symbol("euler"), r, func_decl_info(id, OP_E)); m_e = m->mk_const(e_decl); m->inc_ref(m_e); - + func_decl * z_pw_z_int = m->mk_const_decl(symbol("0^0-int"), i, func_decl_info(id, OP_0_PW_0_INT)); m_0_pw_0_int = m->mk_const(z_pw_z_int); m->inc_ref(m_0_pw_0_int); @@ -221,7 +221,7 @@ void arith_decl_plugin::set_manager(ast_manager * m, family_id id) { func_decl * z_pw_z_real = m->mk_const_decl(symbol("0^0-real"), r, func_decl_info(id, OP_0_PW_0_REAL)); m_0_pw_0_real = m->mk_const(z_pw_z_real); m->inc_ref(m_0_pw_0_real); - + MK_OP(m_neg_root_decl, "neg-root", OP_NEG_ROOT, r); MK_UNARY(m_div_0_decl, "/0", OP_DIV_0, r); MK_UNARY(m_idiv_0_decl, "div0", OP_IDIV_0, i); @@ -285,7 +285,8 @@ arith_decl_plugin::arith_decl_plugin(): m_idiv_0_decl(0), m_mod_0_decl(0), m_u_asin_decl(0), - m_u_acos_decl(0) { + m_u_acos_decl(0), + m_convert_int_numerals_to_real(false) { } arith_decl_plugin::~arith_decl_plugin() { @@ -418,7 +419,7 @@ app * arith_decl_plugin::mk_numeral(rational const & val, bool is_int) { if (val.is_unsigned()) { unsigned u_val = val.get_unsigned(); if (u_val < MAX_SMALL_NUM_TO_CACHE) { - if (is_int) { + if (is_int && !m_convert_int_numerals_to_real) { app * r = m_small_ints.get(u_val, 0); if (r == 0) { parameter p[2] = { parameter(val), parameter(1) }; @@ -442,7 +443,7 @@ app * arith_decl_plugin::mk_numeral(rational const & val, bool is_int) { } parameter p[2] = { parameter(val), parameter(static_cast(is_int)) }; func_decl * decl; - if (is_int) + if (is_int && !m_convert_int_numerals_to_real) decl = m_manager->mk_const_decl(m_intv_sym, m_int_decl, func_decl_info(m_family_id, OP_NUM, 2, p)); else decl = m_manager->mk_const_decl(m_realv_sym, m_real_decl, func_decl_info(m_family_id, OP_NUM, 2, p)); @@ -479,14 +480,14 @@ static bool has_real_arg(ast_manager * m, unsigned num_args, expr * const * args } static bool is_const_op(decl_kind k) { - return - k == OP_PI || + return + k == OP_PI || k == OP_E || k == OP_0_PW_0_INT || k == OP_0_PW_0_REAL; } - -func_decl * arith_decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters, parameter const * parameters, + +func_decl * arith_decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters, parameter const * parameters, unsigned arity, sort * const * domain, sort * range) { if (k == OP_NUM) return mk_num_decl(num_parameters, parameters, arity); @@ -503,7 +504,7 @@ func_decl * arith_decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters } } -func_decl * arith_decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters, parameter const * parameters, +func_decl * arith_decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters, parameter const * parameters, unsigned num_args, expr * const * args, sort * range) { if (k == OP_NUM) return mk_num_decl(num_parameters, parameters, num_args); @@ -521,9 +522,17 @@ func_decl * arith_decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters } void arith_decl_plugin::get_sort_names(svector& sort_names, symbol const & logic) { - // TODO: only define Int and Real in the right logics - sort_names.push_back(builtin_name("Int", INT_SORT)); - sort_names.push_back(builtin_name("Real", REAL_SORT)); + if (logic == "NRA" || + logic == "QF_NRA" || + logic == "QF_UFNRA") { + m_convert_int_numerals_to_real = true; + sort_names.push_back(builtin_name("Real", REAL_SORT)); + } + else { + // TODO: only define Int and Real in the right logics + sort_names.push_back(builtin_name("Int", INT_SORT)); + sort_names.push_back(builtin_name("Real", REAL_SORT)); + } } void arith_decl_plugin::get_op_names(svector& op_names, symbol const & logic) { @@ -563,16 +572,16 @@ void arith_decl_plugin::get_op_names(svector& op_names, symbol con } bool arith_decl_plugin::is_value(app * e) const { - return - is_app_of(e, m_family_id, OP_NUM) || + return + is_app_of(e, m_family_id, OP_NUM) || is_app_of(e, m_family_id, OP_IRRATIONAL_ALGEBRAIC_NUM) || is_app_of(e, m_family_id, OP_PI) || is_app_of(e, m_family_id, OP_E); } bool arith_decl_plugin::is_unique_value(app * e) const { - return - is_app_of(e, m_family_id, OP_NUM) || + return + is_app_of(e, m_family_id, OP_NUM) || is_app_of(e, m_family_id, OP_PI) || is_app_of(e, m_family_id, OP_E); } @@ -671,7 +680,7 @@ expr_ref arith_util::mk_mul_simplify(expr_ref_vector const& args) { } expr_ref arith_util::mk_mul_simplify(unsigned sz, expr* const* args) { expr_ref result(m_manager); - + switch (sz) { case 0: result = mk_numeral(rational(1), true); @@ -681,7 +690,7 @@ expr_ref arith_util::mk_mul_simplify(unsigned sz, expr* const* args) { break; default: result = mk_mul(sz, args); - break; + break; } return result; } @@ -692,7 +701,7 @@ expr_ref arith_util::mk_add_simplify(expr_ref_vector const& args) { } expr_ref arith_util::mk_add_simplify(unsigned sz, expr* const* args) { expr_ref result(m_manager); - + switch (sz) { case 0: result = mk_numeral(rational(0), true); @@ -702,7 +711,7 @@ expr_ref arith_util::mk_add_simplify(unsigned sz, expr* const* args) { break; default: result = mk_add(sz, args); - break; + break; } return result; } diff --git a/src/ast/arith_decl_plugin.h b/src/ast/arith_decl_plugin.h index 410c50852..668eebcc9 100644 --- a/src/ast/arith_decl_plugin.h +++ b/src/ast/arith_decl_plugin.h @@ -152,6 +152,8 @@ protected: ptr_vector m_small_ints; ptr_vector m_small_reals; + bool m_convert_int_numerals_to_real; + func_decl * mk_func_decl(decl_kind k, bool is_real); virtual void set_manager(ast_manager * m, family_id id); decl_kind fix_kind(decl_kind k, unsigned arity); From ef3d340c85e4dbe7bdfb74303bae3468cfe35d59 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Fri, 31 Mar 2017 12:04:46 +0100 Subject: [PATCH 409/562] Improved decl_collector for uninterpreted sorts in :print_benchmark output --- src/ast/decl_collector.cpp | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/ast/decl_collector.cpp b/src/ast/decl_collector.cpp index b663a9df3..773ebefc7 100644 --- a/src/ast/decl_collector.cpp +++ b/src/ast/decl_collector.cpp @@ -23,8 +23,21 @@ void decl_collector::visit_sort(sort * n) { family_id fid = n->get_family_id(); if (m().is_uninterp(n)) m_sorts.push_back(n); - if (fid == m_dt_fid) + if (fid == m_dt_fid) { m_sorts.push_back(n); + + unsigned num_cnstr = m_dt_util.get_datatype_num_constructors(n); + for (unsigned i = 0; i < num_cnstr; i++) { + func_decl * cnstr = m_dt_util.get_datatype_constructors(n)->get(i); + m_decls.push_back(cnstr); + ptr_vector const & cnstr_acc = *m_dt_util.get_constructor_accessors(cnstr); + unsigned num_cas = cnstr_acc.size(); + for (unsigned j = 0; j < num_cas; j++) { + func_decl * accsr = cnstr_acc.get(j); + m_decls.push_back(accsr); + } + } + } } bool decl_collector::is_bool(sort * s) { @@ -38,14 +51,15 @@ void decl_collector::visit_func(func_decl * n) { m_preds.push_back(n); else m_decls.push_back(n); - } + } } decl_collector::decl_collector(ast_manager & m, bool preds): m_manager(m), - m_sep_preds(preds) { + m_sep_preds(preds), + m_dt_util(m) { m_basic_fid = m_manager.get_basic_family_id(); - m_dt_fid = m_manager.mk_family_id("datatype"); + m_dt_fid = m_dt_util.get_family_id(); } void decl_collector::visit(ast* n) { @@ -55,7 +69,7 @@ void decl_collector::visit(ast* n) { n = todo.back(); todo.pop_back(); if (!m_visited.is_marked(n)) { - m_visited.mark(n, true); + m_visited.mark(n, true); switch(n->get_kind()) { case AST_APP: { app * a = to_app(n); @@ -64,7 +78,7 @@ void decl_collector::visit(ast* n) { } todo.push_back(a->get_decl()); break; - } + } case AST_QUANTIFIER: { quantifier * q = to_quantifier(n); unsigned num_decls = q->get_num_decls(); @@ -77,7 +91,7 @@ void decl_collector::visit(ast* n) { } break; } - case AST_SORT: + case AST_SORT: visit_sort(to_sort(n)); break; case AST_FUNC_DECL: { From 0fb31611135d0d3fc305414ff449e522d92f63ec Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Fri, 31 Mar 2017 12:10:51 +0100 Subject: [PATCH 410/562] Updated declarations in decl_collector --- src/ast/decl_collector.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ast/decl_collector.h b/src/ast/decl_collector.h index 678f3805d..0067d18eb 100644 --- a/src/ast/decl_collector.h +++ b/src/ast/decl_collector.h @@ -21,6 +21,7 @@ Revision History: #define SMT_DECL_COLLECTOR_H_ #include"ast.h" +#include"datatype_decl_plugin.h" class decl_collector { ast_manager & m_manager; @@ -28,9 +29,10 @@ class decl_collector { ptr_vector m_sorts; ptr_vector m_decls; ptr_vector m_preds; - ast_mark m_visited; - family_id m_basic_fid; - family_id m_dt_fid; + ast_mark m_visited; + family_id m_basic_fid; + family_id m_dt_fid; + datatype_util m_dt_util; void visit_sort(sort* n); bool is_bool(sort* s); From c99205fa7e9931cea4a267d789cbd8c83744c320 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Fri, 31 Mar 2017 08:12:53 -0700 Subject: [PATCH 411/562] return box model based on index. Issue #955 Signed-off-by: Nikolaj Bjorner --- src/cmd_context/basic_cmds.cpp | 35 +++++++++++++++++++++++++++------- src/cmd_context/cmd_context.h | 1 + src/opt/opt_context.cpp | 8 ++++++++ src/opt/opt_context.h | 1 + 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/cmd_context/basic_cmds.cpp b/src/cmd_context/basic_cmds.cpp index d951f7710..f09d35158 100644 --- a/src/cmd_context/basic_cmds.cpp +++ b/src/cmd_context/basic_cmds.cpp @@ -100,13 +100,34 @@ public: ATOMIC_CMD(exit_cmd, "exit", "exit.", ctx.print_success(); throw stop_parser_exception();); -ATOMIC_CMD(get_model_cmd, "get-model", "retrieve model for the last check-sat command", { - if (!ctx.is_model_available() || ctx.get_check_sat_result() == 0) - throw cmd_exception("model is not available"); - model_ref m; - ctx.get_check_sat_result()->get_model(m); - ctx.display_model(m); -}); +class get_model_cmd : public cmd { + unsigned m_index; +public: + get_model_cmd(): cmd("get-model"), m_index(0) {} + virtual char const * get_usage() const { return "[]"; } + virtual char const * get_descr(cmd_context & ctx) const { + return "retrieve model for the last check-sat command.\nSupply optional index if retrieving a model corresponding to a box optimization objective"; + } + virtual unsigned get_arity() const { return VAR_ARITY; } + virtual cmd_arg_kind next_arg_kind(cmd_context & ctx) const { return CPK_UINT; } + virtual void set_next_arg(cmd_context & ctx, unsigned index) { m_index = index; } + virtual void execute(cmd_context & ctx) { + if (!ctx.is_model_available() || ctx.get_check_sat_result() == 0) + throw cmd_exception("model is not available"); + model_ref m; + if (m_index > 0 && ctx.get_opt()) { + ctx.get_opt()->get_box_model(m, m_index); + } + else { + ctx.get_check_sat_result()->get_model(m); + } + ctx.display_model(m); + } + virtual void reset(cmd_context& ctx) { + m_index = 0; + } +}; + ATOMIC_CMD(get_assignment_cmd, "get-assignment", "retrieve assignment", { if (!ctx.is_model_available() || ctx.get_check_sat_result() == 0) diff --git a/src/cmd_context/cmd_context.h b/src/cmd_context/cmd_context.h index 8eee632dc..92943c71c 100644 --- a/src/cmd_context/cmd_context.h +++ b/src/cmd_context/cmd_context.h @@ -124,6 +124,7 @@ public: virtual bool is_pareto() = 0; virtual void set_logic(symbol const& s) = 0; virtual bool print_model() const = 0; + virtual void get_box_model(model_ref& mdl, unsigned index) = 0; virtual void updt_params(params_ref const& p) = 0; }; diff --git a/src/opt/opt_context.cpp b/src/opt/opt_context.cpp index d1b7a489e..af3c57baa 100644 --- a/src/opt/opt_context.cpp +++ b/src/opt/opt_context.cpp @@ -342,6 +342,14 @@ namespace opt { fix_model(mdl); } + void context::get_box_model(model_ref& mdl, unsigned index) { + if (index >= m_box_models.size()) { + throw default_exception("index into models is out of bounds"); + } + mdl = m_box_models[index]; + fix_model(mdl); + } + lbool context::execute_min_max(unsigned index, bool committed, bool scoped, bool is_max) { if (scoped) get_solver().push(); lbool result = m_optsmt.lex(index, is_max); diff --git a/src/opt/opt_context.h b/src/opt/opt_context.h index f51f75830..53bfc19c5 100644 --- a/src/opt/opt_context.h +++ b/src/opt/opt_context.h @@ -186,6 +186,7 @@ namespace opt { virtual bool print_model() const; virtual void set_model(model_ref& _m) { m_model = _m; } virtual void get_model(model_ref& _m); + virtual void get_box_model(model_ref& _m, unsigned index); virtual void fix_model(model_ref& _m); virtual void collect_statistics(statistics& stats) const; virtual proof* get_proof() { return 0; } From 8f798fef1a10706f9871f7baff12b564326d6fc7 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Fri, 31 Mar 2017 08:24:12 -0700 Subject: [PATCH 412/562] fix python interface for string extract to take symbolic indices per bug report from Kun Wei Signed-off-by: Nikolaj Bjorner --- src/api/python/z3/z3.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/api/python/z3/z3.py b/src/api/python/z3/z3.py index 16d7fbb5f..568ffe9a2 100644 --- a/src/api/python/z3/z3.py +++ b/src/api/python/z3/z3.py @@ -3701,12 +3701,8 @@ def Extract(high, low, a): high = StringVal(high) if is_seq(high): s = high - offset = _py2expr(low, high.ctx) - length = _py2expr(a, high.ctx) - - if __debug__: - _z3_assert(is_int(offset) and is_int(length), "Second and third arguments must be integers") - return SeqRef(Z3_mk_seq_extract(s.ctx_ref(), s.as_ast(), offset.as_ast(), length.as_ast()), s.ctx) + offset, length = _coerce_exprs(low, a, s.ctx) + return SeqRef(Z3_mk_seq_extract(s.ctx_ref(), s.as_ast(), offset.as_ast(), length.as_ast()), s.ctx) if __debug__: _z3_assert(low <= high, "First argument must be greater than or equal to second argument") _z3_assert(_is_int(high) and high >= 0 and _is_int(low) and low >= 0, "First and second arguments must be non negative integers") From 582880346e8c8ad814f59391d304a04bdc3b5cef Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Fri, 31 Mar 2017 09:22:56 -0700 Subject: [PATCH 413/562] add index option to 'eval' command for box objectives. Issue #955 Signed-off-by: Nikolaj Bjorner --- src/ast/rewriter/seq_rewriter.cpp | 66 +++++++++++++++++++++++++++---- src/cmd_context/eval_cmd.cpp | 9 ++++- 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/ast/rewriter/seq_rewriter.cpp b/src/ast/rewriter/seq_rewriter.cpp index a62eea6ea..3d7da43a7 100644 --- a/src/ast/rewriter/seq_rewriter.cpp +++ b/src/ast/rewriter/seq_rewriter.cpp @@ -12,6 +12,7 @@ Abstract: Author: Nikolaj Bjorner (nbjorner) 2015-12-5 + Murphy Berzish 2017-02-21 Notes: @@ -514,30 +515,56 @@ br_status seq_rewriter::mk_seq_extract(expr* a, expr* b, expr* c, expr_ref& resu bool constantPos = m_autil.is_numeral(b, pos); bool constantLen = m_autil.is_numeral(c, len); - // case 1: pos<0 or len<0 + // case 1: pos<0 or len<=0 // rewrite to "" - if ( (constantPos && pos.is_neg()) || (constantLen && len.is_neg()) ) { + if ( (constantPos && pos.is_neg()) || (constantLen && !len.is_pos()) ) { result = m_util.str.mk_empty(m().get_sort(a)); return BR_DONE; } // case 1.1: pos >= length(base) // rewrite to "" - if (constantBase && constantPos && pos.get_unsigned() >= s.length()) { + if (constantBase && constantPos && pos >= rational(s.length())) { result = m_util.str.mk_empty(m().get_sort(a)); return BR_DONE; } + constantPos &= pos.is_unsigned(); + constantLen &= len.is_unsigned(); + if (constantBase && constantPos && constantLen) { if (pos.get_unsigned() + len.get_unsigned() >= s.length()) { // case 2: pos+len goes past the end of the string unsigned _len = s.length() - pos.get_unsigned() + 1; result = m_util.str.mk_string(s.extract(pos.get_unsigned(), _len)); - return BR_DONE; } else { // case 3: pos+len still within string result = m_util.str.mk_string(s.extract(pos.get_unsigned(), len.get_unsigned())); - return BR_DONE; } + return BR_DONE; + } + + if (constantPos && constantLen) { + unsigned _pos = pos.get_unsigned(); + unsigned _len = len.get_unsigned(); + SASSERT(_len > 0); + expr_ref_vector as(m()), bs(m()); + m_util.str.get_concat(a, as); + for (unsigned i = 0; i < as.size() && _len > 0; ++i) { + if (m_util.str.is_unit(as[i].get())) { + if (_pos == 0) { + bs.push_back(as[i].get()); + --_len; + } + else { + --_pos; + } + } + else { + return BR_FAILED; + } + } + result = m_util.str.mk_concat(bs); + return BR_DONE; } return BR_FAILED; @@ -640,10 +667,33 @@ br_status seq_rewriter::mk_seq_at(expr* a, expr* b, expr_ref& result) { result = m_util.str.mk_empty(m().get_sort(a)); return BR_DONE; } - if (m_util.str.is_string(a, c) && r.is_unsigned() && r < rational(c.length())) { - result = m_util.str.mk_string(c.extract(r.get_unsigned(), 1)); + if (m_util.str.is_string(a, c)) { + if (r.is_unsigned() && r < rational(c.length())) { + result = m_util.str.mk_string(c.extract(r.get_unsigned(), 1)); + } + else { + result = m_util.str.mk_empty(m().get_sort(a)); + } return BR_DONE; - } + } + if (r.is_unsigned()) { + len = r.get_unsigned(); + expr_ref_vector as(m()); + m_util.str.get_concat(a, as); + for (unsigned i = 0; i < as.size(); ++i) { + if (m_util.str.is_unit(as[i].get())) { + if (len == 0) { + result = as[i].get(); + return BR_DONE; + } + --len; + } + else { + return BR_FAILED; + } + } + } + } return BR_FAILED; } diff --git a/src/cmd_context/eval_cmd.cpp b/src/cmd_context/eval_cmd.cpp index 94583001b..86078a13c 100644 --- a/src/cmd_context/eval_cmd.cpp +++ b/src/cmd_context/eval_cmd.cpp @@ -38,6 +38,7 @@ public: virtual void init_pdescrs(cmd_context & ctx, param_descrs & p) { model_evaluator::get_param_descrs(p); insert_timeout(p); + p.insert("model_index", CPK_UINT, "(default: 0) index of model from box optimization objective"); } virtual void prepare(cmd_context & ctx) { @@ -58,9 +59,15 @@ public: if (!ctx.is_model_available()) throw cmd_exception("model is not available"); model_ref md; + unsigned index = m_params.get_uint("model_index", 0); check_sat_result * last_result = ctx.get_check_sat_result(); SASSERT(last_result); - last_result->get_model(md); + if (index == 0 || !ctx.get_opt()) { + last_result->get_model(md); + } + else { + ctx.get_opt()->get_box_model(md, index); + } expr_ref r(ctx.m()); unsigned timeout = m_params.get_uint("timeout", UINT_MAX); unsigned rlimit = m_params.get_uint("rlimit", 0); From c4b26cd691ddfa303b36a6b5a37ac1948352e5be Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Fri, 31 Mar 2017 16:38:15 -0700 Subject: [PATCH 414/562] add bypass to allow recursive functions from API Signed-off-by: Nikolaj Bjorner --- src/api/api_quant.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/api/api_quant.cpp b/src/api/api_quant.cpp index bf64aa571..e87b9446f 100644 --- a/src/api/api_quant.cpp +++ b/src/api/api_quant.cpp @@ -69,14 +69,17 @@ extern "C" { } expr * const* ps = reinterpret_cast(patterns); expr * const* no_ps = reinterpret_cast(no_patterns); - pattern_validator v(mk_c(c)->m()); - for (unsigned i = 0; i < num_patterns; i++) { - if (!v(num_decls, ps[i], 0, 0)) { - SET_ERROR_CODE(Z3_INVALID_PATTERN); - return 0; + symbol qid = to_symbol(quantifier_id); + bool is_rec = mk_c(c)->m().rec_fun_qid() == qid; + if (!is_rec) { + pattern_validator v(mk_c(c)->m()); + for (unsigned i = 0; i < num_patterns; i++) { + if (!v(num_decls, ps[i], 0, 0)) { + SET_ERROR_CODE(Z3_INVALID_PATTERN); + return 0; + } } } - sort* const* ts = reinterpret_cast(sorts); svector names; for (unsigned i = 0; i < num_decls; ++i) { @@ -88,7 +91,7 @@ extern "C" { (0 != is_forall), names.size(), ts, names.c_ptr(), to_expr(body), weight, - to_symbol(quantifier_id), + qid, to_symbol(skolem_id), num_patterns, ps, num_no_patterns, no_ps From 19de682b58901a17c5be8e13fcbee508ff667ae2 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 4 Apr 2017 17:22:55 -0400 Subject: [PATCH 415/562] remove references to m_str_fid in api --- src/api/api_context.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/api/api_context.h b/src/api/api_context.h index 6e34f6d6e..4685fd04e 100644 --- a/src/api/api_context.h +++ b/src/api/api_context.h @@ -82,7 +82,6 @@ namespace api { family_id m_pb_fid; family_id m_fpa_fid; family_id m_seq_fid; - family_id m_str_fid; datatype_decl_plugin * m_dt_plugin; std::string m_string_buffer; // temporary buffer used to cache strings sent to the "external" world. @@ -136,7 +135,6 @@ namespace api { family_id get_pb_fid() const { return m_pb_fid; } family_id get_fpa_fid() const { return m_fpa_fid; } family_id get_seq_fid() const { return m_seq_fid; } - family_id get_str_fid() const { return m_str_fid; } datatype_decl_plugin * get_dt_plugin() const { return m_dt_plugin; } Z3_error_code get_error_code() const { return m_error_code; } From f881e854702972d99ca611ea74bd2255222880dd Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 4 Apr 2017 17:54:18 -0400 Subject: [PATCH 416/562] remove old theory_str enums from api --- src/api/z3_api.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/api/z3_api.h b/src/api/z3_api.h index 23d04f0be..272c94dda 100644 --- a/src/api/z3_api.h +++ b/src/api/z3_api.h @@ -1169,10 +1169,6 @@ typedef enum { Z3_OP_RE_FULL_SET, Z3_OP_RE_COMPLEMENT, - // theory_str - Z3_OP_STR_CONCAT, - Z3_OP_STR_LENGTH, - // Auxiliary Z3_OP_LABEL = 0x700, Z3_OP_LABEL_LIT, From eef2bbadad9dd51a7c9ea0ded1a986dd7e0ba04a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 4 Apr 2017 20:29:48 -0400 Subject: [PATCH 417/562] remove obsolete PARAM_STRING from ast --- src/ast/ast.cpp | 4 ---- src/ast/ast.h | 9 ++------- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 7271048b1..5f2de5170 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -59,7 +59,6 @@ parameter& parameter::operator=(parameter const& other) { case PARAM_SYMBOL: new (m_symbol) symbol(other.get_symbol()); break; case PARAM_RATIONAL: new (m_rational) rational(other.get_rational()); break; case PARAM_DOUBLE: m_dval = other.m_dval; break; - case PARAM_STRING: m_string = other.m_string; break; case PARAM_EXTERNAL: m_ext_id = other.m_ext_id; break; default: UNREACHABLE(); @@ -95,7 +94,6 @@ bool parameter::operator==(parameter const & p) const { case PARAM_SYMBOL: return get_symbol() == p.get_symbol(); case PARAM_RATIONAL: return get_rational() == p.get_rational(); case PARAM_DOUBLE: return m_dval == p.m_dval; - case PARAM_STRING: return (m_string == NULL && p.m_string == NULL) || strcmp(m_string, p.m_string)==0; case PARAM_EXTERNAL: return m_ext_id == p.m_ext_id; default: UNREACHABLE(); return false; } @@ -109,7 +107,6 @@ unsigned parameter::hash() const { case PARAM_SYMBOL: b = get_symbol().hash(); break; case PARAM_RATIONAL: b = get_rational().hash(); break; case PARAM_DOUBLE: b = static_cast(m_dval); break; - case PARAM_STRING: /* TODO */ b = 42; break; case PARAM_EXTERNAL: b = m_ext_id; break; } return (b << 2) | m_kind; @@ -122,7 +119,6 @@ std::ostream& parameter::display(std::ostream& out) const { case PARAM_RATIONAL: return out << get_rational(); case PARAM_AST: return out << "#" << get_ast()->get_id(); case PARAM_DOUBLE: return out << m_dval; - case PARAM_STRING: return out << m_string; case PARAM_EXTERNAL: return out << "@" << m_ext_id; default: UNREACHABLE(); diff --git a/src/ast/ast.h b/src/ast/ast.h index 066265bb8..6bb3b01c9 100644 --- a/src/ast/ast.h +++ b/src/ast/ast.h @@ -87,7 +87,6 @@ public: PARAM_SYMBOL, PARAM_RATIONAL, PARAM_DOUBLE, - PARAM_STRING, // PARAM_EXTERNAL is used for handling decl_plugin specific parameters. // For example, it is used for handling mpf numbers in float_decl_plugin, // and irrational algebraic numbers in arith_decl_plugin. @@ -106,7 +105,6 @@ private: char m_symbol[sizeof(symbol)]; // for PARAM_SYMBOL char m_rational[sizeof(rational)]; // for PARAM_RATIONAL double m_dval; // for PARAM_DOUBLE (remark: this is not used in float_decl_plugin) - const char* m_string; // for PARAM_STRING unsigned m_ext_id; // for PARAM_EXTERNAL }; @@ -119,8 +117,8 @@ public: explicit parameter(symbol const & s): m_kind(PARAM_SYMBOL) { new (m_symbol) symbol(s); } explicit parameter(rational const & r): m_kind(PARAM_RATIONAL) { new (m_rational) rational(r); } explicit parameter(double d):m_kind(PARAM_DOUBLE), m_dval(d) {} - explicit parameter(const char *s):m_kind(PARAM_STRING), m_string(s) { - TRACE("parse_string", tout << "parameter(const char *): " << s << "\n";); + explicit parameter(const char *s):m_kind(PARAM_SYMBOL) { + new (m_symbol) symbol(s); } explicit parameter(unsigned ext_id, bool):m_kind(PARAM_EXTERNAL), m_ext_id(ext_id) {} parameter(parameter const&); @@ -135,7 +133,6 @@ public: bool is_symbol() const { return m_kind == PARAM_SYMBOL; } bool is_rational() const { return m_kind == PARAM_RATIONAL; } bool is_double() const { return m_kind == PARAM_DOUBLE; } - bool is_string() const { return m_kind == PARAM_STRING; } bool is_external() const { return m_kind == PARAM_EXTERNAL; } bool is_int(int & i) const { return is_int() && (i = get_int(), true); } @@ -143,7 +140,6 @@ public: bool is_symbol(symbol & s) const { return is_symbol() && (s = get_symbol(), true); } bool is_rational(rational & r) const { return is_rational() && (r = get_rational(), true); } bool is_double(double & d) const { return is_double() && (d = get_double(), true); } - // TODO is_string(char*) bool is_external(unsigned & id) const { return is_external() && (id = get_ext_id(), true); } /** @@ -163,7 +159,6 @@ public: symbol const & get_symbol() const { SASSERT(is_symbol()); return *(reinterpret_cast(m_symbol)); } rational const & get_rational() const { SASSERT(is_rational()); return *(reinterpret_cast(m_rational)); } double get_double() const { SASSERT(is_double()); return m_dval; } - const char * get_string() const { SASSERT(is_string()); return m_string; } unsigned get_ext_id() const { SASSERT(is_external()); return m_ext_id; } bool operator==(parameter const & p) const; From 7d35fcb17eb035ebd1b63ff3d62c7a4101fe7435 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Wed, 5 Apr 2017 19:42:02 +0100 Subject: [PATCH 418/562] Avoid null pointer warnings in justifications. --- src/smt/smt_justification.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/smt/smt_justification.cpp b/src/smt/smt_justification.cpp index c091f6973..7a9938cd0 100644 --- a/src/smt/smt_justification.cpp +++ b/src/smt/smt_justification.cpp @@ -246,13 +246,15 @@ namespace smt { simple_justification::simple_justification(region & r, unsigned num_lits, literal const * lits): m_num_literals(num_lits) { - m_literals = new (r) literal[num_lits]; - memcpy(m_literals, lits, sizeof(literal) * num_lits); + if (num_lits != 0) { + m_literals = new (r) literal[num_lits]; + memcpy(m_literals, lits, sizeof(literal) * num_lits); #ifdef Z3DEBUG - for (unsigned i = 0; i < num_lits; i++) { - SASSERT(lits[i] != null_literal); - } + for (unsigned i = 0; i < num_lits; i++) { + SASSERT(lits[i] != null_literal); + } #endif + } } void simple_justification::get_antecedents(conflict_resolution & cr) { From d3908857576b25f0ae781bdac41a2e6bdd42eb7c Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Thu, 6 Apr 2017 18:37:29 +0100 Subject: [PATCH 419/562] Added utility to compare quantifier instantiation profiles generated via smt.qi.profile=true --- contrib/qprofdiff/Makefile | 7 + contrib/qprofdiff/main.cpp | 238 ++++++++++++++++++++ contrib/qprofdiff/qprofdiff.vcxproj | 137 +++++++++++ contrib/qprofdiff/qprofdiff.vcxproj.filters | 22 ++ 4 files changed, 404 insertions(+) create mode 100644 contrib/qprofdiff/Makefile create mode 100644 contrib/qprofdiff/main.cpp create mode 100644 contrib/qprofdiff/qprofdiff.vcxproj create mode 100644 contrib/qprofdiff/qprofdiff.vcxproj.filters diff --git a/contrib/qprofdiff/Makefile b/contrib/qprofdiff/Makefile new file mode 100644 index 000000000..6b90bed51 --- /dev/null +++ b/contrib/qprofdiff/Makefile @@ -0,0 +1,7 @@ +qprofdiff: main.cpp + $(CXX) $(CXXFLAGS) main.cpp -o qprofdiff + +all: qprofdiff + +clean: + rm -f qprofdiff diff --git a/contrib/qprofdiff/main.cpp b/contrib/qprofdiff/main.cpp new file mode 100644 index 000000000..44c76b17d --- /dev/null +++ b/contrib/qprofdiff/main.cpp @@ -0,0 +1,238 @@ +/*++ +Copyright (c) 2017 Microsoft Corporation + +Module Name: + + main.cpp + +Abstract: + + Main file for qprofdiff. + +Author: + + Christoph M. Wintersteiger (cwinter) + +Revision History: +--*/ +#include + +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +set options; + +// Profile format: +// [quantifier_instances] qname : num_instances : max_generation : max_cost_s +const string prefix = "[quantifier_instances]"; +unsigned prefix_len = prefix.length(); +typedef struct { unsigned num_instances, max_generation, max_cost; } map_entry; + +string trim(string str) { + size_t linx = str.find_first_not_of(' '); + size_t rinx = str.find_last_not_of(' '); + return str.substr(linx, rinx-linx+1); +} + +int parse(string const & filename, map & data) { + ifstream fs(filename.c_str()); + + if (!fs.is_open()) { + cout << "Can't open file '" << filename << "'" << endl; + return ENOENT; + } + + string qid; + string tokens[4]; + unsigned cur_token = 0; + + while (!fs.eof()) { + string line; + getline(fs, line); + + if (line.substr(0, prefix_len) == prefix) { + line = trim(line.substr(prefix_len)); + size_t from = 0, ti = 0; + for (size_t inx = line.find(':', from); + inx != string::npos; + inx = line.find(':', from)) { + tokens[ti] = trim(line.substr(from, inx-from)); + from = inx+1; + ti++; + } + if (from != line.length() && ti < 4) + tokens[ti] = trim(line.substr(from)); + + qid = tokens[0]; + + if (data.find(qid) == data.end()) { + map_entry & entry = data[qid]; + entry.num_instances = entry.max_generation = entry.max_cost = 0; + } + + map_entry & entry = data[qid]; + entry.num_instances += atoi(tokens[1].c_str()); + entry.max_generation = std::max(entry.max_generation, (unsigned)atoi(tokens[2].c_str())); + entry.max_cost = max(entry.max_cost, (unsigned)atoi(tokens[3].c_str())); + } + } + + fs.close(); + + return 0; +} + +void display_data(map & data) { + for (map::iterator it = data.begin(); + it != data.end(); + it++) + cout << it->first << ": " << it->second.num_instances << + ", " << it->second.max_generation << + ", " << it->second.max_cost << endl; +} + + +typedef struct { int d_num_instances, d_max_generation, d_max_cost; } diff_entry; +typedef struct { string qid; diff_entry e; } diff_item; + +bool diff_item_lt_inst(diff_item const & l, diff_item const & r) { + return l.e.d_num_instances < r.e.d_num_instances; +} + +bool diff_item_lt_gen(diff_item const & l, diff_item const & r) { + return l.e.d_max_generation< r.e.d_max_generation; +} + +bool diff_item_lt_cost(diff_item const & l, diff_item const & r) { + return l.e.d_max_cost < r.e.d_max_cost; +} + +void display_indicator(int const & delta, bool suppress_unchanged) { + if (delta < 0) + cout << "+ "; + else if (delta > 0) + cout << "- "; + else if (delta == 0 && !suppress_unchanged) + cout << "= "; +} + +void diff(map & left, map & right) { + map diff_data; + + for (map::const_iterator lit = left.begin(); + lit != left.end(); + lit++) { + string const & qid = lit->first; + map_entry const & lentry = lit->second; + + map::const_iterator rit = right.find(qid); + if (rit != right.end()) { + map_entry const & rentry = rit->second; + + diff_entry & de = diff_data[qid]; + de.d_num_instances = lentry.num_instances - rentry.num_instances; + de.d_max_generation = lentry.max_generation - rentry.max_generation; + de.d_max_cost = lentry.max_cost - rentry.max_cost; + } + } + + vector flat_data; + for (map::const_iterator it = diff_data.begin(); + it != diff_data.end(); + it++) { + flat_data.push_back(diff_item()); + flat_data.back().qid = it->first; + flat_data.back().e = it->second; + } + + stable_sort(flat_data.begin(), flat_data.end(), + options.find("-si") != options.end() ? diff_item_lt_inst : + options.find("-sg") != options.end() ? diff_item_lt_gen : + options.find("-sc") != options.end() ? diff_item_lt_cost : + diff_item_lt_inst); + + bool suppress_unchanged = options.find("-n") != options.end(); + + for (vector::const_iterator it = flat_data.begin(); + it != flat_data.end(); + it++) { + diff_item const & d = *it; + string const & qid = d.qid; + diff_entry const & e = d.e; + + int const & delta = + (options.find("-si") != options.end()) ? e.d_num_instances : + (options.find("-sg") != options.end()) ? e.d_max_generation : + (options.find("-sc") != options.end()) ? e.d_max_cost : + e.d_num_instances; + + display_indicator(delta, suppress_unchanged); + + if (delta != 0 || !suppress_unchanged) + cout << qid << " (" << + (e.d_num_instances > 0 ? "" : "+") << -e.d_num_instances << " inst., " << + (e.d_max_generation > 0 ? "" : "+") << -e.d_max_generation << " max. gen., " << + (e.d_max_cost > 0 ? "" : "+") << -e.d_max_cost << " max. cost)" << + endl; + } +} + +void display_usage() { + cout << "Usage: qprofdiff [options] " << endl; + cout << "Options:" << endl; + cout << " -n Suppress unchanged items" << endl; + cout << " -si Sort by difference in number of instances" << endl; + cout << " -sg Sort by difference in max. generation" << endl; + cout << " -sc Sort by difference in max. cost" << endl; +} + +int main(int argc, char ** argv) { + char * filename1 = 0; + char * filename2 = 0; + + for (int i = 1; i < argc; i++) { + int len = string(argv[i]).length(); + if (len > 1 && argv[i][0] == '-') { + options.insert(string(argv[i])); + } + else if (filename1 == 0) + filename1 = argv[i]; + else if (filename2 == 0) + filename2 = argv[i]; + else { + cout << "Invalid argument: " << argv[i] << endl << endl; + display_usage(); + return EINVAL; + } + } + + if (filename1 == 0 || filename2 == 0) { + cout << "Two filenames required." << endl << endl; + display_usage(); + return EINVAL; + } + + + cout << "Comparing " << filename1 << " to " << filename2 << endl; + + map data1, data2; + + int r = parse(filename1, data1); + if (r != 0) return r; + r = parse(filename2, data2); + if (r != 0) return r; + + // display_data(data1); + // display_data(data2); + + diff(data1, data2); + + return 0; +} \ No newline at end of file diff --git a/contrib/qprofdiff/qprofdiff.vcxproj b/contrib/qprofdiff/qprofdiff.vcxproj new file mode 100644 index 000000000..b6584e126 --- /dev/null +++ b/contrib/qprofdiff/qprofdiff.vcxproj @@ -0,0 +1,137 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {96E7E3EF-4162-474D-BD32-C702632AAF2B} + qprofdiff + 8.1 + + + + Application + true + v141 + NotSet + + + Application + false + v141 + true + MultiByte + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + $(IncludePath) + $(LibraryPath) + + + $(IncludePath) + $(LibraryPath) + + + + Level3 + Disabled + true + MultiThreadedDebugDLL + ..\..\src\util;%(AdditionalIncludeDirectories) + + + ProgramDatabase + + + $(LibraryPath);%(AdditionalLibraryDirectories) + + + + + Level3 + Disabled + true + ..\..\src\util;%(AdditionalIncludeDirectories) + + + + + Level3 + MaxSpeed + true + true + true + ..\..\src\util;%(AdditionalIncludeDirectories) + + + true + true + + + + + Level3 + MaxSpeed + true + true + true + ..\..\src\util;%(AdditionalIncludeDirectories) + + + true + true + + + + + + + + + \ No newline at end of file diff --git a/contrib/qprofdiff/qprofdiff.vcxproj.filters b/contrib/qprofdiff/qprofdiff.vcxproj.filters new file mode 100644 index 000000000..0d8d9e457 --- /dev/null +++ b/contrib/qprofdiff/qprofdiff.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file From f3c990d356f477cecf65dfc795041e6f3e0ab126 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Fri, 7 Apr 2017 17:54:28 +0100 Subject: [PATCH 420/562] Fixes for qprofdiff --- contrib/qprofdiff/main.cpp | 111 +++++++++++++++++++++++++------------ 1 file changed, 77 insertions(+), 34 deletions(-) diff --git a/contrib/qprofdiff/main.cpp b/contrib/qprofdiff/main.cpp index 44c76b17d..d6a403cf2 100644 --- a/contrib/qprofdiff/main.cpp +++ b/contrib/qprofdiff/main.cpp @@ -78,9 +78,9 @@ int parse(string const & filename, map & data) { } map_entry & entry = data[qid]; - entry.num_instances += atoi(tokens[1].c_str()); - entry.max_generation = std::max(entry.max_generation, (unsigned)atoi(tokens[2].c_str())); - entry.max_cost = max(entry.max_cost, (unsigned)atoi(tokens[3].c_str())); + entry.num_instances = atoi(tokens[1].c_str()); + entry.max_generation = (unsigned)atoi(tokens[2].c_str()); + entry.max_cost = (unsigned)atoi(tokens[3].c_str()); } } @@ -94,33 +94,56 @@ void display_data(map & data) { it != data.end(); it++) cout << it->first << ": " << it->second.num_instances << - ", " << it->second.max_generation << - ", " << it->second.max_cost << endl; + ", " << it->second.max_generation << + ", " << it->second.max_cost << endl; } -typedef struct { int d_num_instances, d_max_generation, d_max_cost; } diff_entry; +typedef struct { + int d_num_instances, d_max_generation, d_max_cost; + bool left_only, right_only; +} diff_entry; + typedef struct { string qid; diff_entry e; } diff_item; -bool diff_item_lt_inst(diff_item const & l, diff_item const & r) { - return l.e.d_num_instances < r.e.d_num_instances; +#define DIFF_LT(X) bool diff_item_lt_ ## X (diff_item const & l, diff_item const & r) { \ + return \ + l.e.left_only ? (r.e.left_only ? l.qid < r.qid : false) : \ + l.e.right_only ? (r.e.right_only ? l.qid < r.qid : true) : \ + r.e.right_only ? false : \ + r.e.left_only ? true : \ + l.e.d_ ## X < r.e.d_ ## X ; \ } -bool diff_item_lt_gen(diff_item const & l, diff_item const & r) { - return l.e.d_max_generation< r.e.d_max_generation; -} +DIFF_LT(num_instances) +DIFF_LT(max_generation) +DIFF_LT(max_cost) -bool diff_item_lt_cost(diff_item const & l, diff_item const & r) { - return l.e.d_max_cost < r.e.d_max_cost; -} +int indicate(diff_entry const & e, bool suppress_unchanged) { + if (e.left_only) { + cout << "< "; + return INT_MIN; + } + else if (e.right_only) { + cout << "> "; + return INT_MAX; + } + else { + int const & delta = + (options.find("-si") != options.end()) ? e.d_num_instances : + (options.find("-sg") != options.end()) ? e.d_max_generation : + (options.find("-sc") != options.end()) ? e.d_max_cost : + e.d_num_instances; -void display_indicator(int const & delta, bool suppress_unchanged) { - if (delta < 0) - cout << "+ "; - else if (delta > 0) - cout << "- "; - else if (delta == 0 && !suppress_unchanged) - cout << "= "; + if (delta < 0) + cout << "+ "; + else if (delta > 0) + cout << "- "; + else if (delta == 0 && !suppress_unchanged) + cout << "= "; + + return delta; + } } void diff(map & left, map & right) { @@ -135,12 +158,38 @@ void diff(map & left, map & right) { map::const_iterator rit = right.find(qid); if (rit != right.end()) { map_entry const & rentry = rit->second; - diff_entry & de = diff_data[qid]; + + de.left_only = de.right_only = false; de.d_num_instances = lentry.num_instances - rentry.num_instances; de.d_max_generation = lentry.max_generation - rentry.max_generation; de.d_max_cost = lentry.max_cost - rentry.max_cost; } + else { + diff_entry & de = diff_data[qid]; + de.left_only = true; + de.right_only = false; + de.d_num_instances = lentry.num_instances; + de.d_max_generation = lentry.max_generation; + de.d_max_cost = lentry.max_cost; + } + } + + for (map::const_iterator rit = right.begin(); + rit != right.end(); + rit++) { + string const & qid = rit->first; + map_entry const & rentry = rit->second; + + map::const_iterator lit = left.find(qid); + if (lit == left.end()) { + diff_entry & de = diff_data[qid]; + de.left_only = false; + de.right_only = true; + de.d_num_instances = -(int)rentry.num_instances; + de.d_max_generation = -(int)rentry.max_generation; + de.d_max_cost = -(int)rentry.max_cost; + } } vector flat_data; @@ -153,10 +202,10 @@ void diff(map & left, map & right) { } stable_sort(flat_data.begin(), flat_data.end(), - options.find("-si") != options.end() ? diff_item_lt_inst : - options.find("-sg") != options.end() ? diff_item_lt_gen : - options.find("-sc") != options.end() ? diff_item_lt_cost : - diff_item_lt_inst); + options.find("-si") != options.end() ? diff_item_lt_num_instances: + options.find("-sg") != options.end() ? diff_item_lt_max_generation : + options.find("-sc") != options.end() ? diff_item_lt_max_cost : + diff_item_lt_num_instances); bool suppress_unchanged = options.find("-n") != options.end(); @@ -167,15 +216,9 @@ void diff(map & left, map & right) { string const & qid = d.qid; diff_entry const & e = d.e; - int const & delta = - (options.find("-si") != options.end()) ? e.d_num_instances : - (options.find("-sg") != options.end()) ? e.d_max_generation : - (options.find("-sc") != options.end()) ? e.d_max_cost : - e.d_num_instances; + int delta = indicate(e, suppress_unchanged); - display_indicator(delta, suppress_unchanged); - - if (delta != 0 || !suppress_unchanged) + if (!(delta == 0 && suppress_unchanged)) cout << qid << " (" << (e.d_num_instances > 0 ? "" : "+") << -e.d_num_instances << " inst., " << (e.d_max_generation > 0 ? "" : "+") << -e.d_max_generation << " max. gen., " << From 23f4a0c332d1b88708c317a116d1912b029bc0b6 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Fri, 7 Apr 2017 17:57:49 +0100 Subject: [PATCH 421/562] Build fix for qprofdiff --- contrib/qprofdiff/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contrib/qprofdiff/main.cpp b/contrib/qprofdiff/main.cpp index d6a403cf2..661e976ec 100644 --- a/contrib/qprofdiff/main.cpp +++ b/contrib/qprofdiff/main.cpp @@ -16,6 +16,7 @@ Author: Revision History: --*/ #include +#include #include #include @@ -278,4 +279,4 @@ int main(int argc, char ** argv) { diff(data1, data2); return 0; -} \ No newline at end of file +} From 9a757ffffe242d62c2f5802955b72f95963c4302 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Fri, 7 Apr 2017 18:09:35 +0100 Subject: [PATCH 422/562] Result ordering fix for qprofdiff --- contrib/qprofdiff/main.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/contrib/qprofdiff/main.cpp b/contrib/qprofdiff/main.cpp index 661e976ec..58d21b77d 100644 --- a/contrib/qprofdiff/main.cpp +++ b/contrib/qprofdiff/main.cpp @@ -108,12 +108,14 @@ typedef struct { typedef struct { string qid; diff_entry e; } diff_item; #define DIFF_LT(X) bool diff_item_lt_ ## X (diff_item const & l, diff_item const & r) { \ + int l_lt_r = l.e.d_ ## X < r.e.d_ ## X; \ + int l_eq_r = l.e.d_ ## X == r.e.d_ ## X; \ return \ - l.e.left_only ? (r.e.left_only ? l.qid < r.qid : false) : \ - l.e.right_only ? (r.e.right_only ? l.qid < r.qid : true) : \ + l.e.left_only ? (r.e.left_only ? ((l_eq_r) ? l.qid < r.qid : l_lt_r) : false) : \ + l.e.right_only ? (r.e.right_only ? ((l_eq_r) ? l.qid < r.qid : l_lt_r) : true) : \ r.e.right_only ? false : \ r.e.left_only ? true : \ - l.e.d_ ## X < r.e.d_ ## X ; \ + l_lt_r; \ } DIFF_LT(num_instances) From 27a17588575d82774e2f812bbe2735084b43e855 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Fri, 7 Apr 2017 21:19:20 +0100 Subject: [PATCH 423/562] Added rewriter.ignore_patterns_on_ground_qbody option to disable simplification of quantifiers that have their universals appear only in patterns, but otherwise have a ground body. --- src/ast/normal_forms/defined_names.cpp | 36 +- src/ast/rewriter/der.cpp | 62 +- src/ast/rewriter/rewriter_params.pyg | 3 +- src/ast/rewriter/th_rewriter.cpp | 72 +-- src/ast/rewriter/var_subst.cpp | 14 +- src/ast/rewriter/var_subst.h | 21 +- src/ast/simplifier/distribute_forall.cpp | 22 +- src/ast/simplifier/elim_bounds.cpp | 14 +- src/ast/simplifier/simplifier.cpp | 94 +-- src/cmd_context/extra_cmds/dbg_cmds.cpp | 43 +- src/duality/duality_wrapper.cpp | 82 +-- src/muz/base/dl_rule.cpp | 82 +-- src/muz/pdr/pdr_context.cpp | 4 +- src/muz/tab/tab_context.cpp | 206 +++---- src/qe/qe_lite.cpp | 575 ++++++++++--------- src/qe/qe_lite.h | 10 +- src/tactic/bv/elim_small_bv_tactic.cpp | 9 +- src/tactic/core/distribute_forall_tactic.cpp | 32 +- src/tactic/ufbv/ufbv_rewriter.cpp | 190 +++--- 19 files changed, 795 insertions(+), 776 deletions(-) diff --git a/src/ast/normal_forms/defined_names.cpp b/src/ast/normal_forms/defined_names.cpp index c1d9b36a5..1ac2049ac 100644 --- a/src/ast/normal_forms/defined_names.cpp +++ b/src/ast/normal_forms/defined_names.cpp @@ -28,7 +28,7 @@ struct defined_names::impl { typedef obj_map expr2proof; ast_manager & m_manager; symbol m_z3name; - + /** \brief Mapping from expressions to their names. A name is an application. If the expression does not have free variables, then the name is just a constant. @@ -38,25 +38,25 @@ struct defined_names::impl { \brief Mapping from expressions to the apply-def proof. That is, for each expression e, m_expr2proof[e] is the proof e and m_expr2name[2] are observ. equivalent. - + This mapping is not used if proof production is disabled. */ expr2proof m_expr2proof; - + /** \brief Domain of m_expr2name. It is used to keep the expressions alive and for backtracking */ - expr_ref_vector m_exprs; + expr_ref_vector m_exprs; expr_ref_vector m_names; //!< Range of m_expr2name. It is used to keep the names alive. proof_ref_vector m_apply_proofs; //!< Range of m_expr2proof. It is used to keep the def-intro proofs alive. - - + + unsigned_vector m_lims; //!< Backtracking support. - + impl(ast_manager & m, char const * prefix); virtual ~impl(); - + app * gen_name(expr * e, sort_ref_buffer & var_sorts, buffer & var_names); void cache_new_name(expr * e, app * name); void cache_new_name_intro_proof(expr * e, proof * pr); @@ -106,7 +106,7 @@ app * defined_names::impl::gen_name(expr * e, sort_ref_buffer & var_sorts, buffe for (unsigned i = 0; i < num_vars; i++) { sort * s = uv.get(i); if (s) { - domain.push_back(s); + domain.push_back(s); new_args.push_back(m_manager.mk_var(i, s)); var_sorts.push_back(s); } @@ -162,7 +162,7 @@ void defined_names::impl::bound_vars(sort_ref_buffer const & sorts, buffer var_names; - + n = gen_name(e, var_sorts, var_names); cache_new_name(e, n); - + TRACE("mk_definition_bug", tout << "name: " << mk_ismt2_pp(n, m_manager) << "\n";); // variables are in reverse order in quantifiers std::reverse(var_sorts.c_ptr(), var_sorts.c_ptr() + var_sorts.size()); std::reverse(var_names.c_ptr(), var_names.c_ptr() + var_names.size()); - + mk_definition(e, n, var_sorts, var_names, new_def); - + TRACE("mk_definition_bug", tout << "new_def:\n" << mk_ismt2_pp(new_def, m_manager) << "\n";); - + if (m_manager.proofs_enabled()) { new_def_pr = m_manager.mk_def_intro(new_def); pr = m_manager.mk_apply_def(e, n, new_def_pr); @@ -311,11 +311,11 @@ void defined_names::reset() { m_pos_impl->reset(); } -unsigned defined_names::get_num_names() const { +unsigned defined_names::get_num_names() const { return m_impl->get_num_names() + m_pos_impl->get_num_names(); } -func_decl * defined_names::get_name_decl(unsigned i) const { +func_decl * defined_names::get_name_decl(unsigned i) const { SASSERT(i < get_num_names()); unsigned n1 = m_impl->get_num_names(); return i < n1 ? m_impl->get_name_decl(i) : m_pos_impl->get_name_decl(i - n1); diff --git a/src/ast/rewriter/der.cpp b/src/ast/rewriter/der.cpp index 83ed94ece..aef5d8ddd 100644 --- a/src/ast/rewriter/der.cpp +++ b/src/ast/rewriter/der.cpp @@ -36,7 +36,7 @@ static bool is_neg_var(ast_manager & m, expr * e, unsigned num_decls) { /** \brief Return true if \c e is of the form (not (= VAR t)) or (not (iff VAR t)) or (iff VAR t) or (iff (not VAR) t) or (VAR IDX) or (not (VAR IDX)). - The last case can be viewed + The last case can be viewed */ bool der::is_var_diseq(expr * e, unsigned num_decls, var * & v, expr_ref & t) { // (not (= VAR t)) and (not (iff VAR t)) cases @@ -49,7 +49,7 @@ bool der::is_var_diseq(expr * e, unsigned num_decls, var * & v, expr_ref & t) { return false; if (!is_var(lhs, num_decls)) std::swap(lhs, rhs); - SASSERT(is_var(lhs, num_decls)); + SASSERT(is_var(lhs, num_decls)); // Remark: Occurs check is not necessary here... the top-sort procedure will check for cycles... // if (occurs(lhs, rhs)) { // return false; @@ -67,7 +67,7 @@ bool der::is_var_diseq(expr * e, unsigned num_decls, var * & v, expr_ref & t) { if (is_var(lhs, num_decls) || is_var(rhs, num_decls)) { if (!is_var(lhs, num_decls)) std::swap(lhs, rhs); - SASSERT(is_var(lhs, num_decls)); + SASSERT(is_var(lhs, num_decls)); // Remark: Occurs check is not necessary here... the top-sort procedure will check for cycles... // if (occurs(lhs, rhs)) { // return false; @@ -83,11 +83,11 @@ bool der::is_var_diseq(expr * e, unsigned num_decls, var * & v, expr_ref & t) { if (!is_neg_var(m_manager, lhs, num_decls)) std::swap(lhs, rhs); SASSERT(is_neg_var(m_manager, lhs, num_decls)); - expr * lhs_var = to_app(lhs)->get_arg(0); + expr * lhs_var = to_app(lhs)->get_arg(0); // Remark: Occurs check is not necessary here... the top-sort procedure will check for cycles... // if (occurs(lhs_var, rhs)) { // return false; - // } + // } v = to_var(lhs_var); t = rhs; TRACE("der", tout << mk_pp(e, m_manager) << "\n";); @@ -134,11 +134,11 @@ void der::operator()(quantifier * q, expr_ref & r, proof_ref & pr) { pr = m_manager.mk_transitivity(pr, curr_pr); } } while (q != r && is_quantifier(r)); - + // Eliminate variables that have become unused if (reduced && is_forall(r)) { quantifier * q = to_quantifier(r); - elim_unused_vars(m_manager, q, r); + elim_unused_vars(m_manager, q, params_ref(), r); if (m_manager.proofs_enabled()) { proof * p1 = m_manager.mk_elim_unused_vars(q, r); pr = m_manager.mk_transitivity(pr, p1); @@ -153,24 +153,24 @@ void der::reduce1(quantifier * q, expr_ref & r, proof_ref & pr) { r = q; return; } - + expr * e = q->get_expr(); unsigned num_decls = q->get_num_decls(); var * v = 0; - expr_ref t(m_manager); + expr_ref t(m_manager); if (m_manager.is_or(e)) { unsigned num_args = to_app(e)->get_num_args(); unsigned i = 0; unsigned diseq_count = 0; unsigned largest_vinx = 0; - + m_map.reset(); m_pos2var.reset(); m_inx2var.reset(); - + m_pos2var.reserve(num_args, -1); - + // Find all disequalities for (; i < num_args; i++) { if (is_var_diseq(to_app(e)->get_arg(i), num_decls, v, t)) { @@ -192,7 +192,7 @@ void der::reduce1(quantifier * q, expr_ref & r, proof_ref & pr) { get_elimination_order(); SASSERT(m_order.size() <= diseq_count); // some might be missing because of cycles - if (!m_order.empty()) { + if (!m_order.empty()) { create_substitution(largest_vinx + 1); apply_substitution(q, r); } @@ -202,22 +202,22 @@ void der::reduce1(quantifier * q, expr_ref & r, proof_ref & pr) { r = q; } } - // Remark: get_elimination_order/top-sort checks for cycles, but it is not invoked for unit clauses. + // Remark: get_elimination_order/top-sort checks for cycles, but it is not invoked for unit clauses. // So, we must perform a occurs check here. else if (is_var_diseq(e, num_decls, v, t) && !occurs(v, t)) { r = m_manager.mk_false(); } - else + else r = q; - + if (m_manager.proofs_enabled()) { pr = r == q ? 0 : m_manager.mk_der(q, r); - } + } } void der_sort_vars(ptr_vector & vars, ptr_vector & definitions, unsigned_vector & order) { order.reset(); - + // eliminate self loops, and definitions containing quantifiers. bool found = false; for (unsigned i = 0; i < definitions.size(); i++) { @@ -228,7 +228,7 @@ void der_sort_vars(ptr_vector & vars, ptr_vector & definitions, unsig else found = true; // found at least one candidate } - + if (!found) return; @@ -329,14 +329,14 @@ void der::get_elimination_order() { // der::top_sort ts(m_manager); der_sort_vars(m_inx2var, m_map, m_order); - TRACE("der", + TRACE("der", tout << "Elimination m_order:" << std::endl; for(unsigned i=0; iget_expr(); - unsigned num_args=to_app(e)->get_num_args(); - + unsigned num_args=to_app(e)->get_num_args(); + // get a new expression m_new_args.reset(); for(unsigned i = 0; i < num_args; i++) { int x = m_pos2var[i]; - if (x != -1 && m_map[x] != 0) + if (x != -1 && m_map[x] != 0) continue; // this is a disequality with definition (vanishes) - + m_new_args.push_back(to_app(e)->get_arg(i)); } unsigned sz = m_new_args.size(); expr_ref t(m_manager); t = (sz == 1) ? m_new_args[0] : m_manager.mk_or(sz, m_new_args.c_ptr()); - expr_ref new_e(m_manager); + expr_ref new_e(m_manager); m_subst(t, m_subst_map.size(), m_subst_map.c_ptr(), new_e); - + // don't forget to update the quantifier patterns expr_ref_buffer new_patterns(m_manager); expr_ref_buffer new_no_patterns(m_manager); @@ -392,7 +392,7 @@ void der::apply_substitution(quantifier * q, expr_ref & r) { new_no_patterns.push_back(new_nopat); } - r = m_manager.update_quantifier(q, new_patterns.size(), new_patterns.c_ptr(), + r = m_manager.update_quantifier(q, new_patterns.size(), new_patterns.c_ptr(), new_no_patterns.size(), new_no_patterns.c_ptr(), new_e); } @@ -404,9 +404,9 @@ struct der_rewriter_cfg : public default_rewriter_cfg { ast_manager & m() const { return m_der.m(); } - bool reduce_quantifier(quantifier * old_q, - expr * new_body, - expr * const * new_patterns, + bool reduce_quantifier(quantifier * old_q, + expr * new_body, + expr * const * new_patterns, expr * const * new_no_patterns, expr_ref & result, proof_ref & result_pr) { diff --git a/src/ast/rewriter/rewriter_params.pyg b/src/ast/rewriter/rewriter_params.pyg index 5bd17f556..06500086a 100644 --- a/src/ast/rewriter/rewriter_params.pyg +++ b/src/ast/rewriter/rewriter_params.pyg @@ -8,5 +8,6 @@ def_module_params('rewriter', ("push_ite_bv", BOOL, False, "push if-then-else over bit-vector terms."), ("pull_cheap_ite", BOOL, False, "pull if-then-else terms when cheap."), ("bv_ineq_consistency_test_max", UINT, 0, "max size of conjunctions on which to perform consistency test based on inequalities on bitvectors."), - ("cache_all", BOOL, False, "cache all intermediate results."))) + ("cache_all", BOOL, False, "cache all intermediate results."), + ("ignore_patterns_on_ground_qbody", BOOL, True, "ignores patterns on quantifiers that don't mention their bound variables."))) diff --git a/src/ast/rewriter/th_rewriter.cpp b/src/ast/rewriter/th_rewriter.cpp index 0c57ea609..b561e02fc 100644 --- a/src/ast/rewriter/th_rewriter.cpp +++ b/src/ast/rewriter/th_rewriter.cpp @@ -54,6 +54,7 @@ struct th_rewriter_cfg : public default_rewriter_cfg { bool m_cache_all; bool m_push_ite_arith; bool m_push_ite_bv; + bool m_ignore_patterns_on_ground_qbody; // substitution support expr_dependency_ref m_used_dependencies; // set of dependencies of used substitutions @@ -70,8 +71,9 @@ struct th_rewriter_cfg : public default_rewriter_cfg { m_cache_all = p.cache_all(); m_push_ite_arith = p.push_ite_arith(); m_push_ite_bv = p.push_ite_bv(); + m_ignore_patterns_on_ground_qbody = p.ignore_patterns_on_ground_qbody(); } - + void updt_params(params_ref const & p) { m_b_rw.updt_params(p); m_a_rw.updt_params(p); @@ -82,7 +84,7 @@ struct th_rewriter_cfg : public default_rewriter_cfg { updt_local_params(p); } - bool flat_assoc(func_decl * f) const { + bool flat_assoc(func_decl * f) const { if (!m_flat) return false; family_id fid = f->get_family_id(); if (fid == null_family_id) @@ -98,10 +100,10 @@ struct th_rewriter_cfg : public default_rewriter_cfg { } bool rewrite_patterns() const { return false; } - + bool cache_all_results() const { return m_cache_all; } - bool max_steps_exceeded(unsigned num_steps) const { + bool max_steps_exceeded(unsigned num_steps) const { cooperate("simplifier"); if (memory::get_allocation_size() > m_max_memory) throw rewriter_exception(Z3_MAX_MEMORY_MSG); @@ -179,13 +181,13 @@ struct th_rewriter_cfg : public default_rewriter_cfg { st = m_ar_rw.mk_eq_core(args[0], args[1], result); else if (s_fid == m_seq_rw.get_fid()) st = m_seq_rw.mk_eq_core(args[0], args[1], result); - + if (st != BR_FAILED) return st; } if (k == OP_EQ || k == OP_IFF) { SASSERT(num == 2); - st = apply_tamagotchi(args[0], args[1], result); + st = apply_tamagotchi(args[0], args[1], result); if (st != BR_FAILED) return st; } @@ -239,13 +241,13 @@ struct th_rewriter_cfg : public default_rewriter_cfg { } else { if (SWAP) { - result = m().mk_ite(ite->get_arg(0), + result = m().mk_ite(ite->get_arg(0), m().mk_app(p, value, ite->get_arg(1)), m().mk_app(p, value, ite->get_arg(2))); return BR_REWRITE2; } else { - result = m().mk_ite(ite->get_arg(0), + result = m().mk_ite(ite->get_arg(0), m().mk_app(p, ite->get_arg(1), value), m().mk_app(p, ite->get_arg(2), value)); return BR_REWRITE2; @@ -257,7 +259,7 @@ struct th_rewriter_cfg : public default_rewriter_cfg { // ite-value-tree := (ite c ) // subtree := value // | (ite c ) - // + // bool is_ite_value_tree(expr * t) { if (!m().is_ite(t)) return false; @@ -281,7 +283,7 @@ struct th_rewriter_cfg : public default_rewriter_cfg { } return true; } - + br_status pull_ite(func_decl * f, unsigned num, expr * const * args, expr_ref & result) { if (num == 2 && m().is_bool(f->get_range()) && !m().is_bool(args[0])) { if (m().is_ite(args[0])) { @@ -325,7 +327,7 @@ struct th_rewriter_cfg : public default_rewriter_cfg { if (!is_app(t)) return false; family_id fid = to_app(t)->get_family_id(); - return ((fid == m_a_rw.get_fid() && m_push_ite_arith) || + return ((fid == m_a_rw.get_fid() && m_push_ite_arith) || (fid == m_bv_rw.get_fid() && m_push_ite_bv)); } @@ -349,7 +351,7 @@ struct th_rewriter_cfg : public default_rewriter_cfg { } return false; } - + /** \brief Try to "unify" t1 and t2 Examples @@ -463,7 +465,7 @@ struct th_rewriter_cfg : public default_rewriter_cfg { } // terms matched... bool is_int = m_a_util.is_int(t1); - if (!new_t1) + if (!new_t1) new_t1 = m_a_util.mk_numeral(rational(0), is_int); if (!new_t2) new_t2 = m_a_util.mk_numeral(rational(0), is_int); @@ -476,7 +478,7 @@ struct th_rewriter_cfg : public default_rewriter_cfg { args.push_back(arg); } SASSERT(!args.empty()); - if (args.size() == 1) + if (args.size() == 1) c = args[0]; else c = m_a_util.mk_add(args.size(), args.c_ptr()); @@ -518,7 +520,7 @@ struct th_rewriter_cfg : public default_rewriter_cfg { // Apply transformations of the form // - // (ite c (+ k1 a) (+ k2 a)) --> (+ (ite c k1 k2) a) + // (ite c (+ k1 a) (+ k2 a)) --> (+ (ite c k1 k2) a) // (ite c (* k1 a) (* k2 a)) --> (* (ite c k1 k2) a) // // These transformations are useful for bit-vector problems, since @@ -536,7 +538,7 @@ struct th_rewriter_cfg : public default_rewriter_cfg { if (unify(t, e, f_prime, new_t, new_e, common, first)) { if (first) result = m().mk_app(f_prime, common, m().mk_ite(c, new_t, new_e)); - else + else result = m().mk_app(f_prime, m().mk_ite(c, new_t, new_e), common); return BR_DONE; } @@ -558,7 +560,7 @@ struct th_rewriter_cfg : public default_rewriter_cfg { result_pr = 0; br_status st = reduce_app_core(f, num, args, result); if (st != BR_DONE && st != BR_FAILED) { - CTRACE("th_rewriter_step", st != BR_FAILED, + CTRACE("th_rewriter_step", st != BR_FAILED, tout << f->get_name() << "\n"; for (unsigned i = 0; i < num; i++) tout << mk_ismt2_pp(args[i], m()) << "\n"; tout << "---------->\n" << mk_ismt2_pp(result, m()) << "\n";); @@ -576,7 +578,7 @@ struct th_rewriter_cfg : public default_rewriter_cfg { else st = pull_ite(result); } - CTRACE("th_rewriter_step", st != BR_FAILED, + CTRACE("th_rewriter_step", st != BR_FAILED, tout << f->get_name() << "\n"; for (unsigned i = 0; i < num; i++) tout << mk_ismt2_pp(args[i], m()) << "\n"; tout << "---------->\n" << mk_ismt2_pp(result, m()) << "\n";); @@ -593,28 +595,28 @@ struct th_rewriter_cfg : public default_rewriter_cfg { } - bool reduce_quantifier(quantifier * old_q, - expr * new_body, - expr * const * new_patterns, + bool reduce_quantifier(quantifier * old_q, + expr * new_body, + expr * const * new_patterns, expr * const * new_no_patterns, expr_ref & result, proof_ref & result_pr) { quantifier_ref q1(m()); proof * p1 = 0; - if (is_quantifier(new_body) && + if (is_quantifier(new_body) && to_quantifier(new_body)->is_forall() == old_q->is_forall() && !old_q->has_patterns() && !to_quantifier(new_body)->has_patterns()) { - + quantifier * nested_q = to_quantifier(new_body); - + ptr_buffer sorts; - buffer names; + buffer names; sorts.append(old_q->get_num_decls(), old_q->get_decl_sorts()); names.append(old_q->get_num_decls(), old_q->get_decl_names()); sorts.append(nested_q->get_num_decls(), nested_q->get_decl_sorts()); names.append(nested_q->get_num_decls(), nested_q->get_decl_names()); - + q1 = m().mk_quantifier(old_q->is_forall(), sorts.size(), sorts.c_ptr(), @@ -624,9 +626,9 @@ struct th_rewriter_cfg : public default_rewriter_cfg { old_q->get_qid(), old_q->get_skid(), 0, 0, 0, 0); - + SASSERT(is_well_sorted(m(), q1)); - + if (m().proofs_enabled()) { SASSERT(old_q->get_expr() == new_body); p1 = m().mk_pull_quant(old_q, q1); @@ -635,24 +637,24 @@ struct th_rewriter_cfg : public default_rewriter_cfg { else { ptr_buffer new_patterns_buf; ptr_buffer new_no_patterns_buf; - + new_patterns_buf.append(old_q->get_num_patterns(), new_patterns); new_no_patterns_buf.append(old_q->get_num_no_patterns(), new_no_patterns); remove_duplicates(new_patterns_buf); remove_duplicates(new_no_patterns_buf); - - q1 = m().update_quantifier(old_q, + + q1 = m().update_quantifier(old_q, new_patterns_buf.size(), new_patterns_buf.c_ptr(), new_no_patterns_buf.size(), new_no_patterns_buf.c_ptr(), new_body); TRACE("reduce_quantifier", tout << mk_ismt2_pp(old_q, m()) << "\n----->\n" << mk_ismt2_pp(q1, m()) << "\n";); SASSERT(is_well_sorted(m(), q1)); } - - elim_unused_vars(m(), q1, result); + + elim_unused_vars(m(), q1, params_ref(), result); TRACE("reduce_quantifier", tout << "after elim_unused_vars:\n" << mk_ismt2_pp(result, m()) << "\n";); - + result_pr = 0; if (m().proofs_enabled()) { proof * p2 = 0; @@ -758,7 +760,7 @@ unsigned th_rewriter::get_num_steps() const { void th_rewriter::cleanup() { ast_manager & m = m_imp->m(); dealloc(m_imp); - m_imp = alloc(imp, m, m_params); + m_imp = alloc(imp, m, m_params); } void th_rewriter::reset() { diff --git a/src/ast/rewriter/var_subst.cpp b/src/ast/rewriter/var_subst.cpp index 37b335a9d..fd290c8fe 100644 --- a/src/ast/rewriter/var_subst.cpp +++ b/src/ast/rewriter/var_subst.cpp @@ -39,10 +39,16 @@ void var_subst::operator()(expr * n, unsigned num_args, expr * const * args, exp tout << mk_ismt2_pp(result, m_reducer.m()) << "\n";); } +unused_vars_eliminator::unused_vars_eliminator(ast_manager & m, params_ref const & params) : + m(m), m_subst(m), m_params(params) +{ + m_ignore_patterns_on_ground_qbody = m_params.get_bool("ignore_patterns_on_ground_qbody", true); +} + void unused_vars_eliminator::operator()(quantifier* q, expr_ref & result) { SASSERT(is_well_sorted(m, q)); - if (is_ground(q->get_expr())) { - // ignore patterns if the body is a ground formula. + if (m_ignore_patterns_on_ground_qbody && is_ground(q->get_expr())) { + // Ignore patterns if the body is a ground formula. result = q->get_expr(); return; } @@ -146,8 +152,8 @@ void unused_vars_eliminator::operator()(quantifier* q, expr_ref & result) { SASSERT(is_well_sorted(m, result)); } -void elim_unused_vars(ast_manager & m, quantifier * q, expr_ref & result) { - unused_vars_eliminator el(m); +void elim_unused_vars(ast_manager & m, quantifier * q, params_ref const & params, expr_ref & result) { + unused_vars_eliminator el(m, params); el(q, result); } diff --git a/src/ast/rewriter/var_subst.h b/src/ast/rewriter/var_subst.h index 9d04cebe3..21aa58399 100644 --- a/src/ast/rewriter/var_subst.h +++ b/src/ast/rewriter/var_subst.h @@ -21,6 +21,7 @@ Notes: #include"rewriter.h" #include"used_vars.h" +#include"params.h" /** \brief Alias for var_shifter class. @@ -31,7 +32,7 @@ typedef var_shifter shift_vars; \brief Variable substitution functor. It substitutes variables by expressions. The expressions may contain variables. */ -class var_subst { +class var_subst { beta_reducer m_reducer; bool m_std_order; public: @@ -39,7 +40,7 @@ public: bool std_order() const { return m_std_order; } /** - When std_order() == true, + When std_order() == true, I'm using the same standard used in quantifier instantiation. (VAR 0) is stored in the last position of the array. ... @@ -55,15 +56,17 @@ public: \brief Eliminate the unused variables from \c q. Store the result in \c r. */ class unused_vars_eliminator { - ast_manager& m; - var_subst m_subst; - used_vars m_used; + ast_manager & m; + var_subst m_subst; + used_vars m_used; + params_ref m_params; + bool m_ignore_patterns_on_ground_qbody; public: - unused_vars_eliminator(ast_manager& m): m(m), m_subst(m) {} + unused_vars_eliminator(ast_manager & m, params_ref const & params); void operator()(quantifier* q, expr_ref& r); }; -void elim_unused_vars(ast_manager & m, quantifier * q, expr_ref & r); +void elim_unused_vars(ast_manager & m, quantifier * q, params_ref const & params, expr_ref & r); /** \brief Instantiate quantifier q using the given exprs. @@ -86,7 +89,7 @@ class expr_free_vars { expr_sparse_mark m_mark; ptr_vector m_sorts; ptr_vector m_todo; -public: +public: void reset(); void operator()(expr* e); void accumulate(expr* e); @@ -96,7 +99,7 @@ public: bool contains(unsigned idx) const { return idx < m_sorts.size() && m_sorts[idx] != 0; } void set_default_sort(sort* s); void reverse() { m_sorts.reverse(); } - sort*const* c_ptr() const { return m_sorts.c_ptr(); } + sort*const* c_ptr() const { return m_sorts.c_ptr(); } }; #endif diff --git a/src/ast/simplifier/distribute_forall.cpp b/src/ast/simplifier/distribute_forall.cpp index bd2af5675..78e5d5ded 100644 --- a/src/ast/simplifier/distribute_forall.cpp +++ b/src/ast/simplifier/distribute_forall.cpp @@ -14,7 +14,7 @@ Author: Leonardo de Moura (leonardo) 2010-04-02. Revision History: - + Christoph Wintersteiger 2010-04-06: Added implementation. --*/ @@ -40,7 +40,7 @@ bool distribute_forall::visit_children(expr * n) { bool visited = true; unsigned j; switch(n->get_kind()) { - case AST_VAR: + case AST_VAR: break; case AST_APP: j = to_app(n)->get_num_args(); @@ -86,15 +86,15 @@ void distribute_forall::reduce1_app(app * a) { SASSERT(is_cached(a->get_arg(j))); expr * c = get_cached(a->get_arg(j)); SASSERT(c!=0); - if (c != a->get_arg(j)) + if (c != a->get_arg(j)) reduced = true; m_new_args[j] = c; - } + } if (reduced) { na = m_manager.mk_app(a->get_decl(), num_args, m_new_args.c_ptr()); } - + cache_result(a, na); } @@ -126,11 +126,11 @@ void distribute_forall::reduce1_quantifier(quantifier * q) { quantifier_ref tmp_q(m_manager); tmp_q = m_manager.update_quantifier(q, not_arg); expr_ref new_q(m_manager); - elim_unused_vars(m_manager, tmp_q, new_q); + elim_unused_vars(m_manager, tmp_q, params_ref(), new_q); new_args.push_back(new_q); } expr_ref result(m_manager); - // m_bsimp.mk_and actually constructs a (not (or ...)) formula, + // m_bsimp.mk_and actually constructs a (not (or ...)) formula, // it will also apply basic simplifications. m_bsimp.mk_and(new_args.size(), new_args.c_ptr(), result); cache_result(q, result); @@ -148,15 +148,15 @@ void distribute_forall::operator()(expr * f, expr_ref & result) { while (!m_todo.empty()) { expr * e = m_todo.back(); - if (visit_children(e)) { + if (visit_children(e)) { m_todo.pop_back(); reduce1(e); - } + } } result = get_cached(f); SASSERT(result!=0); - TRACE("distribute_forall", tout << mk_ll_pp(f, m_manager) << "======>\n" + TRACE("distribute_forall", tout << mk_ll_pp(f, m_manager) << "======>\n" << mk_ll_pp(result, m_manager);); } @@ -166,5 +166,5 @@ expr * distribute_forall::get_cached(expr * n) const { void distribute_forall::cache_result(expr * n, expr * r) { SASSERT(r != 0); - m_cache.insert(n, r); + m_cache.insert(n, r); } diff --git a/src/ast/simplifier/elim_bounds.cpp b/src/ast/simplifier/elim_bounds.cpp index a4e145e0a..7a40b8602 100644 --- a/src/ast/simplifier/elim_bounds.cpp +++ b/src/ast/simplifier/elim_bounds.cpp @@ -32,7 +32,7 @@ elim_bounds::elim_bounds(ast_manager & m): (<= x k) (<= (+ x (* -1 y)) k) - (<= (+ x (* -1 t)) k) + (<= (+ x (* -1 t)) k) (<= (+ t (* -1 x)) k) x and y are a bound variables, t is a ground term and k is a numeral @@ -65,14 +65,14 @@ bool elim_bounds::is_bound(expr * n, var * & lower, var * & upper) { if (neg) le = !le; - + if (is_var(n)) { upper = to_var(n); } else if (m_util.is_add(n) && to_app(n)->get_num_args() == 2) { expr * arg1 = to_app(n)->get_arg(0); expr * arg2 = to_app(n)->get_arg(1); - if (is_var(arg1)) + if (is_var(arg1)) upper = to_var(arg1); else if (!is_ground(arg1)) return false; @@ -95,7 +95,7 @@ bool elim_bounds::is_bound(expr * n, var * & lower, var * & upper) { if (!le) std::swap(upper, lower); - + return true; } @@ -188,7 +188,7 @@ void elim_bounds::operator()(quantifier * q, expr_ref & r) { } quantifier_ref new_q(m_manager); new_q = m_manager.update_quantifier(q, new_body); - elim_unused_vars(m_manager, new_q, r); + elim_unused_vars(m_manager, new_q, params_ref(), r); TRACE("elim_bounds", tout << mk_pp(q, m_manager) << "\n" << mk_pp(r, m_manager) << "\n";); } @@ -199,10 +199,10 @@ bool elim_bounds_star::visit_quantifier(quantifier * q) { visit(q->get_expr(), visited); return visited; } - + void elim_bounds_star::reduce1_quantifier(quantifier * q) { if (!q->is_forall() || q->get_num_patterns() != 0) { - cache_result(q, q, 0); + cache_result(q, q, 0); return; } quantifier_ref new_q(m); diff --git a/src/ast/simplifier/simplifier.cpp b/src/ast/simplifier/simplifier.cpp index 6f7e62fd4..498244919 100644 --- a/src/ast/simplifier/simplifier.cpp +++ b/src/ast/simplifier/simplifier.cpp @@ -33,8 +33,8 @@ simplifier::simplifier(ast_manager & m): m_ac_support(true) { } -void simplifier::register_plugin(plugin * p) { - m_plugins.register_plugin(p); +void simplifier::register_plugin(plugin * p) { + m_plugins.register_plugin(p); } simplifier::~simplifier() { @@ -46,13 +46,13 @@ void simplifier::enable_ac_support(bool flag) { ptr_vector::const_iterator it = m_plugins.begin(); ptr_vector::const_iterator end = m_plugins.end(); for (; it != end; ++it) { - if (*it != 0) + if (*it != 0) (*it)->enable_ac_support(flag); } } /** - \brief External interface for the simplifier. + \brief External interface for the simplifier. A client will invoke operator()(s, r, p) to simplify s. The result is stored in r. When proof generation is enabled, a proof for the equivalence (or equisatisfiability) @@ -69,14 +69,14 @@ void simplifier::operator()(expr * s, expr_ref & r, proof_ref & p) { proof * result_proof; switch (m.proof_mode()) { case PGM_DISABLED: // proof generation is disabled. - reduce_core(s); + reduce_core(s); // after executing reduce_core, the result of the simplification is in the cache get_cached(s, result, result_proof); r = result; p = m.mk_undef_proof(); break; case PGM_COARSE: // coarse proofs... in this case, we do not produce a step by step (fine grain) proof to show the equivalence (or equisatisfiability) of s an r. - m_subst_proofs.reset(); // m_subst_proofs is an auxiliary vector that is used to justify substitutions. See comment on method get_subst. + m_subst_proofs.reset(); // m_subst_proofs is an auxiliary vector that is used to justify substitutions. See comment on method get_subst. reduce_core(s); get_cached(s, result, result_proof); r = result; @@ -163,7 +163,7 @@ bool simplifier::visit_children(expr * n) { // The method ast_manager::mk_app is used to create the flat version of an AC operator. // In Z3 1.x, we used multi-ary operators. This creates problems for the superposition engine. // So, starting at Z3 2.x, only boolean operators can be multi-ary. - // Example: + // Example: // (and (and a b) (and c d)) --> (and a b c d) // (+ (+ a b) (+ c d)) --> (+ a (+ b (+ c d))) // Remark: The flattening is only applied if m_ac_support is true. @@ -178,7 +178,7 @@ bool simplifier::visit_children(expr * n) { } return visited; } - case AST_QUANTIFIER: + case AST_QUANTIFIER: return visit_quantifier(to_quantifier(n)); default: UNREACHABLE(); @@ -188,7 +188,7 @@ bool simplifier::visit_children(expr * n) { /** \brief Visit the children of n assuming it is an AC (associative-commutative) operator. - + For example, if n is of the form (+ (+ a b) (+ c d)), this method will return true if the nodes a, b, c and d have been already simplified. The nodes (+ a b) and (+ c d) are not really checked. @@ -216,7 +216,7 @@ bool simplifier::visit_ac(app * n) { expr * arg = n->get_arg(i); if (is_app_of(arg, decl)) todo.push_back(to_app(arg)); - else + else visit(arg, visited); } } @@ -319,7 +319,7 @@ void simplifier::reduce1_app_core(app * n) { proof * p; if (n == r) p = 0; - else if (r != s) + else if (r != s) // we use a "theory rewrite generic proof" to justify the step // s = (decl arg_0' ... arg_{n-1}') --> r p = m.mk_transitivity(p1, m.mk_rewrite(s, r)); @@ -368,7 +368,7 @@ void simplifier::reduce1_ac_app_core(app * n) { proof_ref p1(m); mk_ac_congruent_term(n, n_c, p1); TRACE("ac", tout << "expr:\n" << mk_pp(n, m) << "\ncongruent term:\n" << mk_pp(n_c, m) << "\n";); - expr_ref r(m); + expr_ref r(m); func_decl * decl = n->get_decl(); family_id fid = decl->get_family_id(); plugin * p = get_plugin(fid); @@ -415,7 +415,7 @@ void simplifier::reduce1_ac_app_core(app * n) { proof * p; if (n == r.get()) p = 0; - else if (r.get() != n_c.get()) + else if (r.get() != n_c.get()) p = m.mk_transitivity(p1, m.mk_rewrite(n_c, r)); else p = p1; @@ -434,7 +434,7 @@ void simplifier::dump_rewrite_lemma(func_decl * decl, unsigned num_args, expr * sprintf_s(buffer, ARRAYSIZE(buffer), "lemma_%d.smt", g_rewrite_lemma_id); #else sprintf(buffer, "rewrite_lemma_%d.smt", g_rewrite_lemma_id); -#endif +#endif ast_smt_pp pp(m); pp.set_benchmark_name("rewrite_lemma"); pp.set_status("unsat"); @@ -450,7 +450,7 @@ void simplifier::dump_rewrite_lemma(func_decl * decl, unsigned num_args, expr * /** \brief Return in \c result an expression \c e equivalent to (f args[0] ... args[num_args - 1]), and store in \c pr a proof for (= (f args[0] ... args[num_args - 1]) e) - + If e is identical to (f args[0] ... args[num_args - 1]), then pr is set to 0. */ void simplifier::mk_app(func_decl * decl, unsigned num_args, expr * const * args, expr_ref & result) { @@ -474,7 +474,7 @@ void simplifier::mk_app(func_decl * decl, unsigned num_args, expr * const * args //dump_rewrite_lemma(decl, num_args, args, result.get()); return; } - + result = m.mk_app(decl, num_args, args); } @@ -494,17 +494,17 @@ void simplifier::mk_congruent_term(app * n, app_ref & r, proof_ref & p) { proof * arg_proof; get_cached(arg, new_arg, arg_proof); - CTRACE("simplifier_bug", (arg != new_arg) != (arg_proof != 0), + CTRACE("simplifier_bug", (arg != new_arg) != (arg_proof != 0), tout << mk_ll_pp(arg, m) << "\n---->\n" << mk_ll_pp(new_arg, m) << "\n"; tout << "#" << arg->get_id() << " #" << new_arg->get_id() << "\n"; tout << arg << " " << new_arg << "\n";); - - + + if (arg != new_arg) { has_new_args = true; proofs.push_back(arg_proof); SASSERT(arg_proof); - } + } else { SASSERT(arg_proof == 0); } @@ -526,10 +526,10 @@ void simplifier::mk_congruent_term(app * n, app_ref & r, proof_ref & p) { /** \brief Store the new arguments of \c n in result. Store in p a proof for (= n (f result[0] ... result[num_args - 1])), where f is the function symbol of n. - + If there are no new arguments or fine grain proofs are disabled, then p is set to 0. - Return true there are new arguments. + Return true there are new arguments. */ bool simplifier::get_args(app * n, ptr_vector & result, proof_ref & p) { bool has_new_args = false; @@ -565,10 +565,10 @@ bool simplifier::get_args(app * n, ptr_vector & result, proof_ref & p) { void simplifier::mk_ac_congruent_term(app * n, app_ref & r, proof_ref & p) { SASSERT(m_ac_support); func_decl * f = n->get_decl(); - + m_ac_cache.reset(); m_ac_pr_cache.reset(); - + ptr_buffer todo; ptr_buffer new_args; ptr_buffer new_arg_prs; @@ -621,7 +621,7 @@ void simplifier::mk_ac_congruent_term(app * n, app_ref & r, proof_ref & p) { todo.pop_back(); if (!has_new_arg) { m_ac_cache.insert(curr, curr); - if (m.fine_grain_proofs()) + if (m.fine_grain_proofs()) m_ac_pr_cache.insert(curr, 0); } else { @@ -634,7 +634,7 @@ void simplifier::mk_ac_congruent_term(app * n, app_ref & r, proof_ref & p) { } } } - + SASSERT(m_ac_cache.contains(n)); app * new_n = 0; m_ac_cache.find(n, new_n); @@ -646,7 +646,7 @@ void simplifier::mk_ac_congruent_term(app * n, app_ref & r, proof_ref & p) { } } -#define White 0 +#define White 0 #define Grey 1 #define Black 2 @@ -688,7 +688,7 @@ void simplifier::ac_top_sort(app * n, ptr_buffer & result) { while (!todo.empty()) { expr * curr = todo.back(); int color; - obj_map::obj_map_entry * entry = colors.insert_if_not_there2(curr, White); + obj_map::obj_map_entry * entry = colors.insert_if_not_there2(curr, White); SASSERT(entry); color = entry->get_data().m_value; switch (color) { @@ -731,7 +731,7 @@ void simplifier::get_ac_args(app * n, ptr_vector & args, vector ac_top_sort(n, sorted_exprs); SASSERT(!sorted_exprs.empty()); SASSERT(sorted_exprs[sorted_exprs.size()-1] == n); - + TRACE("ac", tout << mk_ll_pp(n, m, true, false) << "#" << n->get_id() << "\nsorted expressions...\n"; for (unsigned i = 0; i < sorted_exprs.size(); i++) { tout << "#" << sorted_exprs[i]->get_id() << " "; @@ -747,7 +747,7 @@ void simplifier::get_ac_args(app * n, ptr_vector & args, vector expr * curr = sorted_exprs[j]; rational mult; m_ac_mults.find(curr, mult); - SASSERT(!mult.is_zero()); + SASSERT(!mult.is_zero()); if (is_app_of(curr, decl)) { unsigned num_args = to_app(curr)->get_num_args(); for (unsigned i = 0; i < num_args; i++) { @@ -772,16 +772,16 @@ void simplifier::reduce1_quantifier(quantifier * q) { quantifier_ref q1(m); proof * p1 = 0; - - if (is_quantifier(new_body) && + + if (is_quantifier(new_body) && to_quantifier(new_body)->is_forall() == q->is_forall() && !to_quantifier(q)->has_patterns() && !to_quantifier(new_body)->has_patterns()) { - + quantifier * nested_q = to_quantifier(new_body); ptr_buffer sorts; - buffer names; + buffer names; sorts.append(q->get_num_decls(), q->get_decl_sorts()); names.append(q->get_num_decls(), q->get_decl_names()); sorts.append(nested_q->get_num_decls(), nested_q->get_decl_sorts()); @@ -797,7 +797,7 @@ void simplifier::reduce1_quantifier(quantifier * q) { q->get_skid(), 0, 0, 0, 0); SASSERT(is_well_sorted(m, q1)); - + if (m.fine_grain_proofs()) { quantifier * q0 = m.update_quantifier(q, new_body); proof * p0 = q == q0 ? 0 : m.mk_quant_intro(q, q0, new_body_pr); @@ -817,13 +817,13 @@ void simplifier::reduce1_quantifier(quantifier * q) { get_cached(q->get_pattern(i), new_pattern, new_pattern_pr); if (m.is_pattern(new_pattern)) { new_patterns.push_back(new_pattern); - } + } } num = q->get_num_no_patterns(); for (unsigned i = 0; i < num; i++) { get_cached(q->get_no_pattern(i), new_pattern, new_pattern_pr); new_no_patterns.push_back(new_pattern); - } + } remove_duplicates(new_patterns); remove_duplicates(new_no_patterns); @@ -833,7 +833,7 @@ void simplifier::reduce1_quantifier(quantifier * q) { q->get_decl_sorts(), q->get_decl_names(), new_body, - q->get_weight(), + q->get_weight(), q->get_qid(), q->get_skid(), new_patterns.size(), @@ -850,10 +850,10 @@ void simplifier::reduce1_quantifier(quantifier * q) { p1 = q == q1 ? 0 : m.mk_quant_intro(q, q1, new_body_pr); } } - + expr_ref r(m); - elim_unused_vars(m, q1, r); - + elim_unused_vars(m, q1, params_ref(), r); + proof * pr = 0; if (m.fine_grain_proofs()) { proof * p2 = 0; @@ -871,7 +871,7 @@ void simplifier::reduce1_quantifier(quantifier * q) { void simplifier::borrow_plugins(simplifier const & s) { ptr_vector::const_iterator it = s.begin_plugins(); ptr_vector::const_iterator end = s.end_plugins(); - for (; it != end; ++it) + for (; it != end; ++it) register_plugin(*it); } @@ -882,7 +882,7 @@ void simplifier::enable_presimp() { enable_ac_support(false); ptr_vector::const_iterator it = begin_plugins(); ptr_vector::const_iterator end = end_plugins(); - for (; it != end; ++it) + for (; it != end; ++it) (*it)->enable_presimp(true); } @@ -905,7 +905,7 @@ bool subst_simplifier::get_subst(expr * n, expr_ref & r, proof_ref & p) { m_subst_map->get(n, _r, _p); r = _r; p = _p; - if (m.coarse_grain_proofs()) + if (m.coarse_grain_proofs()) m_subst_proofs.push_back(p); return true; } @@ -917,7 +917,7 @@ static void push_core(ast_manager & m, expr * e, proof * pr, expr_ref_vector & r TRACE("preprocessor", tout << mk_pp(e, m) << "\n"; if (pr) tout << mk_ll_pp(pr, m) << "\n\n";); - if (m.is_true(e)) + if (m.is_true(e)) return; result.push_back(e); if (m.proofs_enabled()) @@ -952,9 +952,9 @@ void push_assertion(ast_manager & m, expr * e, proof * pr, expr_ref_vector & res CTRACE("push_assertion", !(pr == 0 || m.is_undef_proof(pr) || m.get_fact(pr) == e), tout << mk_pp(e, m) << "\n" << mk_pp(m.get_fact(pr), m) << "\n";); SASSERT(pr == 0 || m.is_undef_proof(pr) || m.get_fact(pr) == e); - if (m.is_and(e)) + if (m.is_and(e)) push_and(m, to_app(e), pr, result, result_prs); - else if (m.is_not(e) && m.is_or(to_app(e)->get_arg(0))) + else if (m.is_not(e) && m.is_or(to_app(e)->get_arg(0))) push_not_or(m, to_app(to_app(e)->get_arg(0)), pr, result, result_prs); else push_core(m, e, pr, result, result_prs); diff --git a/src/cmd_context/extra_cmds/dbg_cmds.cpp b/src/cmd_context/extra_cmds/dbg_cmds.cpp index 509b5ff2e..7ee1c0aeb 100644 --- a/src/cmd_context/extra_cmds/dbg_cmds.cpp +++ b/src/cmd_context/extra_cmds/dbg_cmds.cpp @@ -29,11 +29,12 @@ Notes: #include"bound_manager.h" #include"used_vars.h" #include"var_subst.h" +#include"gparams.h" #ifndef _EXTERNAL_RELEASE -BINARY_SYM_CMD(get_quantifier_body_cmd, - "dbg-get-qbody", +BINARY_SYM_CMD(get_quantifier_body_cmd, + "dbg-get-qbody", " ", "store the body of the quantifier in the global variable ", CPK_EXPR, @@ -43,8 +44,8 @@ BINARY_SYM_CMD(get_quantifier_body_cmd, store_expr_ref(ctx, m_sym, to_quantifier(arg)->get_expr()); }); -BINARY_SYM_CMD(set_cmd, - "dbg-set", +BINARY_SYM_CMD(set_cmd, + "dbg-set", " ", "store in the global variable ", CPK_EXPR, @@ -57,7 +58,7 @@ UNARY_CMD(pp_var_cmd, "dbg-pp-var", "", "pretty print a global variable expr * t = get_expr_ref(ctx, arg); SASSERT(t != 0); ctx.display(ctx.regular_stream(), t); - ctx.regular_stream() << std::endl; + ctx.regular_stream() << std::endl; }); BINARY_SYM_CMD(shift_vars_cmd, @@ -71,7 +72,7 @@ BINARY_SYM_CMD(shift_vars_cmd, var_shifter s(ctx.m()); s(t, arg, r); store_expr_ref(ctx, m_sym, r.get()); -}); +}); UNARY_CMD(pp_shared_cmd, "dbg-pp-shared", "", "display shared subterms of the given term", CPK_EXPR, expr *, { shared_occs s(ctx.m()); @@ -81,7 +82,7 @@ UNARY_CMD(pp_shared_cmd, "dbg-pp-shared", "", "display shared subterms of shared_occs::iterator end = s.end_shared(); for (; it != end; ++it) { expr * curr = *it; - ctx.regular_stream() << std::endl << " "; + ctx.regular_stream() << std::endl << " "; ctx.display(ctx.regular_stream(), curr, 2); } ctx.regular_stream() << ")" << std::endl; @@ -112,7 +113,7 @@ public: if (m_idx == 1) return CPK_SYMBOL_LIST; return CPK_SYMBOL; } - virtual void set_next_arg(cmd_context & ctx, symbol const & s) { + virtual void set_next_arg(cmd_context & ctx, symbol const & s) { if (m_idx == 0) { m_source = get_expr_ref(ctx, s); } @@ -146,24 +147,24 @@ UNARY_CMD(bool_rewriter_cmd, "dbg-bool-rewriter", "", "apply the Boolean r bool_rewriter_star r(ctx.m(), p); r(arg, t); ctx.display(ctx.regular_stream(), t); - ctx.regular_stream() << std::endl; + ctx.regular_stream() << std::endl; }); UNARY_CMD(bool_frewriter_cmd, "dbg-bool-flat-rewriter", "", "apply the Boolean (flattening) rewriter to the given term", CPK_EXPR, expr *, { expr_ref t(ctx.m()); - { + { params_ref p; p.set_bool("flat", true); bool_rewriter_star r(ctx.m(), p); r(arg, t); } ctx.display(ctx.regular_stream(), t); - ctx.regular_stream() << std::endl; + ctx.regular_stream() << std::endl; }); UNARY_CMD(elim_and_cmd, "dbg-elim-and", "", "apply the Boolean rewriter (eliminating AND operator and flattening) to the given term", CPK_EXPR, expr *, { expr_ref t(ctx.m()); - { + { params_ref p; p.set_bool("flat", true); p.set_bool("elim_and", true); @@ -171,7 +172,7 @@ UNARY_CMD(elim_and_cmd, "dbg-elim-and", "", "apply the Boolean rewriter (e r(arg, t); } ctx.display(ctx.regular_stream(), t); - ctx.regular_stream() << std::endl; + ctx.regular_stream() << std::endl; }); class lt_cmd : public cmd { @@ -192,7 +193,7 @@ public: } virtual void execute(cmd_context & ctx) { bool r = lt(m_t1, m_t2); - ctx.regular_stream() << (r ? "true" : "false") << std::endl; + ctx.regular_stream() << (r ? "true" : "false") << std::endl; } }; @@ -249,7 +250,7 @@ UNARY_CMD(set_next_id, "dbg-set-next-id", "", "set the next expression UNARY_CMD(used_vars_cmd, "dbg-used-vars", "", "test used_vars functor", CPK_EXPR, expr *, { used_vars proc; - if (is_quantifier(arg)) + if (is_quantifier(arg)) arg = to_quantifier(arg)->get_expr(); proc(arg); ctx.regular_stream() << "(vars"; @@ -258,7 +259,7 @@ UNARY_CMD(used_vars_cmd, "dbg-used-vars", "", "test used_vars functor", CP ctx.regular_stream() << "\n (" << std::left << std::setw(6) << i << " "; if (s != 0) ctx.display(ctx.regular_stream(), s, 10); - else + else ctx.regular_stream() << ""; ctx.regular_stream() << ")"; } @@ -271,7 +272,7 @@ UNARY_CMD(elim_unused_vars_cmd, "dbg-elim-unused-vars", "", "eliminate unu return; } expr_ref r(ctx.m()); - elim_unused_vars(ctx.m(), to_quantifier(arg), r); + elim_unused_vars(ctx.m(), to_quantifier(arg), gparams::get(), r); SASSERT(!is_quantifier(r) || !to_quantifier(r)->may_have_unused_vars()); ctx.display(ctx.regular_stream(), r); ctx.regular_stream() << std::endl; @@ -287,18 +288,18 @@ public: virtual char const * get_descr() const { return "instantiate the quantifier using the given expressions."; } virtual unsigned get_arity() const { return 2; } virtual void prepare(cmd_context & ctx) { m_q = 0; m_args.reset(); } - + virtual cmd_arg_kind next_arg_kind(cmd_context & ctx) const { if (m_q == 0) return CPK_EXPR; else return CPK_EXPR_LIST; } - + virtual void set_next_arg(cmd_context & ctx, expr * s) { if (!is_quantifier(s)) throw cmd_exception("invalid command, quantifier expected."); m_q = to_quantifier(s); } - + virtual void set_next_arg(cmd_context & ctx, unsigned num, expr * const * ts) { if (num != m_q->get_num_decls()) throw cmd_exception("invalid command, mismatch between the number of quantified variables and the number of arguments."); @@ -331,7 +332,7 @@ public: class instantiate_nested_cmd : public instantiate_cmd_core { public: instantiate_nested_cmd():instantiate_cmd_core("dbg-instantiate-nested") {} - + virtual char const * get_descr() const { return "instantiate the quantifier nested in the outermost quantifier, this command is used to test the instantiation procedure with quantifiers that contain free variables."; } virtual void set_next_arg(cmd_context & ctx, expr * s) { diff --git a/src/duality/duality_wrapper.cpp b/src/duality/duality_wrapper.cpp index 7ee76d4d6..35033f739 100755 --- a/src/duality/duality_wrapper.cpp +++ b/src/duality/duality_wrapper.cpp @@ -41,8 +41,8 @@ namespace Duality { params_ref p; p.set_bool("proof", true); // this is currently useless if(models) - p.set_bool("model", true); - p.set_bool("unsat_core", true); + p.set_bool("model", true); + p.set_bool("unsat_core", true); bool mbqi = c.get_config().get().get_bool("mbqi",true); p.set_bool("mbqi",mbqi); // just to test p.set_str("mbqi.id","itp"); // use mbqi for quantifiers in interpolants @@ -57,7 +57,7 @@ namespace Duality { m_mode = m().proof_mode(); } - expr context::constant(const std::string &name, const sort &ty){ + expr context::constant(const std::string &name, const sort &ty){ symbol s = str_symbol(name.c_str()); return cook(m().mk_const(m().mk_const_decl(s, ty))); } @@ -111,7 +111,7 @@ namespace Duality { } expr context::mki(family_id fid, ::decl_kind dk, int n, ::expr **args){ - return cook(m().mk_app(fid, dk, 0, 0, n, (::expr **)args)); + return cook(m().mk_app(fid, dk, 0, 0, n, (::expr **)args)); } expr context::make(decl_kind op, const std::vector &args){ @@ -168,9 +168,9 @@ namespace Duality { expr_abstract(m(), 0, num_bound, VEC2PTR(bound_asts), to_expr(body.raw()), abs_body); expr_ref result(m()); result = m().mk_quantifier( - op == Forall, - names.size(), VEC2PTR(types), VEC2PTR(names), abs_body.get(), - 0, + op == Forall, + names.size(), VEC2PTR(types), VEC2PTR(names), abs_body.get(), + 0, ::symbol(), ::symbol(), 0, 0, @@ -194,9 +194,9 @@ namespace Duality { } expr_ref result(m()); result = m().mk_quantifier( - op == Forall, - names.size(), VEC2PTR(types), VEC2PTR(names), to_expr(body.raw()), - 0, + op == Forall, + names.size(), VEC2PTR(types), VEC2PTR(names), to_expr(body.raw()), + 0, ::symbol(), ::symbol(), 0, 0, @@ -273,7 +273,7 @@ namespace Duality { return OtherArray; } } - + return Other; } @@ -340,7 +340,7 @@ namespace Duality { params p; return simplify(p); } - + expr context::make_var(int idx, const sort &s){ ::sort * a = to_sort(s.raw()); return cook(m().mk_var(idx,a)); @@ -348,7 +348,7 @@ namespace Duality { expr expr::qe_lite() const { - ::qe_lite qe(m()); + ::qe_lite qe(m(), params_ref()); expr_ref result(to_expr(raw()),m()); proof_ref pf(m()); qe(result,pf); @@ -356,7 +356,7 @@ namespace Duality { } expr expr::qe_lite(const std::set &idxs, bool index_of_bound) const { - ::qe_lite qe(m()); + ::qe_lite qe(m(), params_ref()); expr_ref result(to_expr(raw()),m()); proof_ref pf(m()); uint_set uis; @@ -412,16 +412,16 @@ namespace Duality { std::vector < ::sort * > _domain(domain.size()); for(unsigned i = 0; i < domain.size(); i++) _domain[i] = to_sort(domain[i].raw()); - ::func_decl* d = m().mk_fresh_func_decl(prefix, - _domain.size(), + ::func_decl* d = m().mk_fresh_func_decl(prefix, + _domain.size(), VEC2PTR(_domain), to_sort(range.raw())); return func_decl(*this,d); } func_decl context::fresh_func_decl(char const * prefix, sort const & range){ - ::func_decl* d = m().mk_fresh_func_decl(prefix, - 0, + ::func_decl* d = m().mk_fresh_func_decl(prefix, + 0, 0, to_sort(range.raw())); return func_decl(*this,d); @@ -462,30 +462,30 @@ namespace Duality { incremental, _theory.size(), VEC2PTR(_theory)); - + if(lb == Z3_L_FALSE){ interpolants.resize(_interpolants.size()); for (unsigned i = 0; i < _interpolants.size(); ++i) { interpolants[i] = expr(ctx(),_interpolants[i]); } - } - + } + if (_model) { model = iz3wrapper::model(ctx(), _model); } - + if(_labels){ labels = _labels; } - + return lb; } #endif - + static int linearize_assumptions(int num, TermTree *assumptions, - std::vector > &linear_assumptions, + std::vector > &linear_assumptions, std::vector &parents){ for(unsigned i = 0; i < assumptions->getChildren().size(); i++) num = linearize_assumptions(num, assumptions->getChildren()[i], linear_assumptions, parents); @@ -501,7 +501,7 @@ namespace Duality { } static int unlinearize_interpolants(int num, - TermTree* assumptions, + TermTree* assumptions, const std::vector &interpolant, TermTree * &tree_interpolant) { @@ -522,7 +522,7 @@ namespace Duality { literals &labels, bool incremental ) - + { int size = assumptions->number(0); std::vector > linear_assumptions(size); @@ -540,36 +540,36 @@ namespace Duality { ptr_vector< ::ast> _theory(theory.size()); for(unsigned i = 0; i < theory.size(); i++) _theory[i] = theory[i]; - - + + if(!incremental){ push(); for(unsigned i = 0; i < linear_assumptions.size(); i++) for(unsigned j = 0; j < linear_assumptions[i].size(); j++) add(linear_assumptions[i][j]); } - + check_result res = unsat; if(!m_solver->get_proof()) res = check(); - + if(res == unsat){ interpolation_options_struct opts; if(weak_mode) - opts.set("weak","1"); - + opts.set("weak","1"); + ::ast *proof = m_solver->get_proof(); try { iz3interpolate(m(),proof,_assumptions,_parents,_interpolants,_theory,&opts); } // If there's an interpolation bug, throw a char * - // exception so duality can catch it and restart. + // exception so duality can catch it and restart. catch (const interpolation_failure &f) { throw f.msg(); } - + std::vector linearized_interpolants(_interpolants.size()); for(unsigned i = 0; i < _interpolants.size(); i++) linearized_interpolants[i] = expr(ctx(),_interpolants[i]); @@ -585,13 +585,13 @@ namespace Duality { model_ref _m; m_solver->get_model(_m); model = Duality::model(ctx(),_m.get()); - + #if 0 if(_labels){ labels = _labels; } #endif - + if(!incremental) pop(); @@ -603,7 +603,7 @@ namespace Duality { void interpolating_solver::SetWeakInterpolants(bool weak){ weak_mode = weak; } - + void interpolating_solver::SetPrintToFile(const std::string &filename){ print_filename = filename; @@ -618,14 +618,14 @@ namespace Duality { void interpolating_solver::RemoveInterpolationAxiom(const expr & t){ // theory.remove(t); } - + const char *interpolating_solver::profile(){ // return Z3_interpolation_profile(ctx()); return ""; } - + static void get_assumptions_rec(stl_ext::hash_set &memo, const proof &pf, std::vector &assumps){ if(memo.find(pf) != memo.end())return; memo.insert(pf); @@ -657,7 +657,7 @@ namespace Duality { model_smt2_pp(std::cout, m(), *m_model, 0); std::cout << std::endl; } - + void model::show_hash() const { std::ostringstream ss; model_smt2_pp(ss, m(), *m_model, 0); diff --git a/src/muz/base/dl_rule.cpp b/src/muz/base/dl_rule.cpp index 56cc6e154..ffb964f47 100644 --- a/src/muz/base/dl_rule.cpp +++ b/src/muz/base/dl_rule.cpp @@ -47,14 +47,14 @@ Revision History: namespace datalog { - rule_manager::rule_manager(context& ctx) + rule_manager::rule_manager(context& ctx) : m(ctx.get_manager()), m_ctx(ctx), m_body(m), m_head(m), m_args(m), m_hnf(m), - m_qe(m), + m_qe(m, params_ref()), m_rwr(m), m_ufproc(m) {} @@ -98,7 +98,7 @@ namespace datalog { var_idx_set& rule_manager::finalize_collect_vars() { unsigned sz = m_free_vars.size(); for (unsigned i = 0; i < sz; ++i) { - if (m_free_vars[i]) m_var_idx.insert(i); + if (m_free_vars[i]) m_var_idx.insert(i); } return m_var_idx; } @@ -139,7 +139,7 @@ namespace datalog { } - void rule_manager::mk_rule(expr* fml, proof* p, rule_set& rules, symbol const& name) { + void rule_manager::mk_rule(expr* fml, proof* p, rule_set& rules, symbol const& name) { scoped_proof_mode _sc(m, m_ctx.generate_proof_trace()?PGM_FINE:PGM_DISABLED); proof_ref pr(p, m); expr_ref fml1(m); @@ -147,7 +147,7 @@ namespace datalog { if (fml1 != fml && pr) { pr = m.mk_asserted(fml1); } - remove_labels(fml1, pr); + remove_labels(fml1, pr); mk_rule_core(fml1, pr, rules, name); } @@ -162,7 +162,7 @@ namespace datalog { else { is_negated.push_back(false); } - } + } } void rule_manager::mk_rule_core(expr* fml, proof* p, rule_set& rules, symbol const& name) { @@ -170,7 +170,7 @@ namespace datalog { proof_ref_vector prs(m); m_hnf.reset(); m_hnf.set_name(name); - + m_hnf(fml, p, fmls, prs); for (unsigned i = 0; i < m_hnf.get_fresh_predicates().size(); ++i) { m_ctx.register_predicate(m_hnf.get_fresh_predicates()[i], false); @@ -181,7 +181,7 @@ namespace datalog { } void rule_manager::mk_horn_rule(expr* fml, proof* p, rule_set& rules, symbol const& name) { - + m_body.reset(); m_neg.reset(); unsigned index = extract_horn(fml, m_body, m_head); @@ -208,13 +208,13 @@ namespace datalog { } else if (is_quantifier(fml1)) { p = m.mk_modus_ponens(p, m.mk_symmetry(m.mk_der(to_quantifier(fml1), fml))); - } + } else { p = m.mk_modus_ponens(p, m.mk_rewrite(fml, fml1)); } } - if (m_ctx.fix_unbound_vars()) { + if (m_ctx.fix_unbound_vars()) { fix_unbound_vars(r, true); } @@ -242,10 +242,10 @@ namespace datalog { for (unsigned i = 0; i < m_args.size(); ++i) { body.push_back(ensure_app(m_args[i].get())); } - } + } else { head = ensure_app(fml); - } + } return index; } @@ -262,12 +262,12 @@ namespace datalog { func_decl* rule_manager::mk_query(expr* query, rule_set& rules) { TRACE("dl", tout << mk_pp(query, m) << "\n";); - + ptr_vector vars; svector names; app_ref_vector body(m); expr_ref q(m); - + // Add implicit variables. // Remove existential prefix. bind_variables(query, false, q); @@ -278,7 +278,7 @@ namespace datalog { m_free_vars(q); vars.append(m_free_vars.size(), m_free_vars.c_ptr()); if (vars.contains(static_cast(0))) { - var_subst sub(m, false); + var_subst sub(m, false); expr_ref_vector args(m); // [s0, 0, s2, ..] // [0 -> 0, 1 -> x, 2 -> 1, ..] @@ -313,7 +313,7 @@ namespace datalog { } } - // we want outermost declared variable first to + // we want outermost declared variable first to // follow order of quantified variables so we reverse vars. while (vars.size() > names.size()) { names.push_back(symbol(names.size())); @@ -321,9 +321,9 @@ namespace datalog { vars.reverse(); names.reverse(); func_decl* qpred = m_ctx.mk_fresh_head_predicate(symbol("query"), symbol(), vars.size(), vars.c_ptr(), body_pred); - m_ctx.register_predicate(qpred, false); + m_ctx.register_predicate(qpred, false); rules.set_output_predicate(qpred); - + if (m_ctx.get_model_converter()) { filter_model_converter* mc = alloc(filter_model_converter, m); mc->insert(qpred); @@ -366,7 +366,7 @@ namespace datalog { for (unsigned i = 0; i < r.size(); ++i) { body.push_back(ensure_app(r[i].get())); } - } + } void rule_manager::hoist_compound(unsigned& num_bound, app_ref& fml, app_ref_vector& body) { @@ -440,7 +440,7 @@ namespace datalog { if (is_quantifier(e)) { q = to_quantifier(e); return q->is_forall(); - } + } return false; } @@ -454,7 +454,7 @@ namespace datalog { return app_ref(m.mk_eq(e, m.mk_true()), m); } } - + void rule_manager::check_app(expr* e) { if (!is_app(e)) { std::ostringstream out; @@ -481,7 +481,7 @@ namespace datalog { bool has_neg = false; for (unsigned i = 0; i < n; i++) { - bool is_neg = (is_negated != 0 && is_negated[i]); + bool is_neg = (is_negated != 0 && is_negated[i]); app * curr = tail[i]; if (is_neg && !m_ctx.is_predicate(curr)) { @@ -571,7 +571,7 @@ namespace datalog { case 1: fml = m.mk_implies(body[0].get(), fml); break; default: fml = m.mk_implies(m.mk_and(body.size(), body.c_ptr()), fml); break; } - + m_free_vars(fml); if (m_free_vars.empty()) { return; @@ -579,7 +579,7 @@ namespace datalog { svector names; used_symbols<> us; m_free_vars.set_default_sort(m.mk_bool_sort()); - + us(fml); m_free_vars.reverse(); for (unsigned j = 0, i = 0; i < m_free_vars.size(); ++j) { @@ -594,8 +594,8 @@ namespace datalog { ++i; } } - } - fml = m.mk_forall(m_free_vars.size(), m_free_vars.c_ptr(), names.c_ptr(), fml); + } + fml = m.mk_forall(m_free_vars.size(), m_free_vars.c_ptr(), names.c_ptr(), fml); } std::ostream& rule_manager::display_smt2(rule const& r, std::ostream & out) { @@ -749,7 +749,7 @@ namespace datalog { quant_tail = m.mk_exists(q_var_cnt, qsorts.c_ptr(), qnames.c_ptr(), unbound_tail_pre_quant); if (try_quantifier_elimination) { - TRACE("dl_rule_unbound_fix_pre_qe", + TRACE("dl_rule_unbound_fix_pre_qe", tout<<"rule: "; r->display(m_ctx, tout); tout<<"tail with unbound vars: "<display(m_ctx, tout); tout<<"tail with unbound vars: "<name(), false); - // keep old variable indices around so we can compose with substitutions. + // keep old variable indices around so we can compose with substitutions. // r->norm_vars(*this); } @@ -835,7 +835,7 @@ namespace datalog { void rule_manager::check_valid_head(expr * head) const { SASSERT(head); - + if (!m_ctx.is_predicate(head)) { std::ostringstream out; out << "Illegal head. The head predicate needs to be uninterpreted and registered (as recursive) " << mk_pp(head, m); @@ -874,14 +874,14 @@ namespace datalog { m.get_allocator().deallocate(get_obj_size(n), this); } - void rule::set_proof(ast_manager& m, proof* p) { + void rule::set_proof(ast_manager& m, proof* p) { if (p) { - m.inc_ref(p); + m.inc_ref(p); } if (m_proof) { - m.dec_ref(m_proof); + m.dec_ref(m_proof); } - m_proof = p; + m_proof = p; } bool rule::is_in_tail(const func_decl * p, bool only_positive) const { @@ -896,7 +896,7 @@ namespace datalog { // - // non-predicates may appear only in the interpreted tail, it is therefore + // non-predicates may appear only in the interpreted tail, it is therefore // sufficient only to check the tail. // bool rule_manager::has_uninterpreted_non_predicates(rule const& r, func_decl*& f) const { @@ -911,7 +911,7 @@ namespace datalog { // - // Quantifiers may appear only in the interpreted tail, it is therefore + // Quantifiers may appear only in the interpreted tail, it is therefore // sufficient only to check the interpreted tail. // void rule_manager::has_quantifiers(rule const& r, bool& existential, bool& universal) const { @@ -945,7 +945,7 @@ namespace datalog { unsigned sz = get_tail_size(); for (unsigned i = 0; i < sz; ++i) { used.process(get_tail(i)); - } + } } void rule::get_vars(ast_manager& m, ptr_vector& sorts) const { @@ -994,13 +994,13 @@ namespace datalog { app * old_tail = get_tail(i); expr_ref new_tail_e(m); vs(old_tail, subst_vals.size(), subst_vals.c_ptr(), new_tail_e); - bool sign = is_neg_tail(i); + bool sign = is_neg_tail(i); m.inc_ref(new_tail_e); m.dec_ref(old_tail); m_tail[i] = TAG(app *, to_app(new_tail_e), sign); } } - + void rule::display(context & ctx, std::ostream & out) const { ast_manager & m = ctx.get_manager(); //out << mk_pp(m_head, m); @@ -1068,7 +1068,7 @@ namespace datalog { } - + }; diff --git a/src/muz/pdr/pdr_context.cpp b/src/muz/pdr/pdr_context.cpp index 587488fc9..7484a77fa 100644 --- a/src/muz/pdr/pdr_context.cpp +++ b/src/muz/pdr/pdr_context.cpp @@ -1007,7 +1007,7 @@ namespace pdr { return m_cache[l]; } - void model_search::erase_children(model_node& n, bool backtrack) { + void model_search::erase_children(model_node& n, bool backtrack) { ptr_vector todo, nodes; todo.append(n.children()); remove_goal(n); @@ -2241,7 +2241,7 @@ namespace pdr { vars.append(aux_vars.size(), aux_vars.c_ptr()); scoped_ptr rep; - qe_lite qe(m); + qe_lite qe(m, m_params.p); expr_ref phi1 = m_pm.mk_and(Phi); qe(vars, phi1); TRACE("pdr", tout << "Eliminated\n" << mk_pp(phi1, m) << "\n";); diff --git a/src/muz/tab/tab_context.cpp b/src/muz/tab/tab_context.cpp index 0a6c4c294..35eb5d936 100644 --- a/src/muz/tab/tab_context.cpp +++ b/src/muz/tab/tab_context.cpp @@ -53,10 +53,10 @@ namespace tb { app* t = to_app(_t); if (m.is_value(s) && m.is_value(t)) { - IF_VERBOSE(2, verbose_stream() << "different:" << mk_pp(s, m) << " " << mk_pp(t, m) << "\n";); + IF_VERBOSE(2, verbose_stream() << "different:" << mk_pp(s, m) << " " << mk_pp(t, m) << "\n";); return l_false; } - + if (m_dt.is_constructor(s) && m_dt.is_constructor(t)) { if (s->get_decl() == t->get_decl()) { lbool state = l_true; @@ -75,7 +75,7 @@ namespace tb { return state; } else { - IF_VERBOSE(2, verbose_stream() << "different constructors:" << mk_pp(s, m) << " " << mk_pp(t, m) << "\n";); + IF_VERBOSE(2, verbose_stream() << "different constructors:" << mk_pp(s, m) << " " << mk_pp(t, m) << "\n";); return l_false; } } @@ -109,7 +109,7 @@ namespace tb { case l_false: return false; default: - conds.push_back(m.mk_eq(p, t)); + conds.push_back(m.mk_eq(p, t)); return true; } } @@ -117,7 +117,7 @@ namespace tb { public: matcher(ast_manager& m): m(m), m_dt(m) {} - + bool operator()(app* pat, app* term, substitution& s, expr_ref_vector& conds) { // top-most term to match is a predicate. The predicates should be the same. if (pat->get_decl() != term->get_decl() || @@ -149,7 +149,7 @@ namespace tb { } } return true; - } + } }; class clause { @@ -165,22 +165,22 @@ namespace tb { unsigned m_next_rule; // next rule to expand goal on unsigned m_ref; // reference count - public: - + public: + clause(ast_manager& m): m_head(m), m_predicates(m), m_constraint(m), m_seqno(0), - m_index(0), + m_index(0), m_num_vars(0), - m_predicate_index(0), + m_predicate_index(0), m_parent_rule(0), m_parent_index(0), m_next_rule(static_cast(-1)), m_ref(0) { } - + void set_seqno(unsigned seqno) { m_seqno = seqno; } unsigned get_seqno() const { return m_seqno; } unsigned get_next_rule() const { return m_next_rule; } @@ -198,10 +198,10 @@ namespace tb { void set_head(app* h) { m_head = h; } unsigned get_parent_index() const { return m_parent_index; } unsigned get_parent_rule() const { return m_parent_rule; } - void set_parent(ref& parent) { + void set_parent(ref& parent) { m_parent_index = parent->get_index(); m_parent_rule = parent->get_next_rule(); - } + } expr_ref get_body() const { ast_manager& m = get_manager(); @@ -247,7 +247,7 @@ namespace tb { } if (!vars.empty()) { body = m.mk_forall(vars.size(), vars.c_ptr(), names.c_ptr(), body); - } + } return body; } @@ -273,18 +273,18 @@ namespace tb { reduce_equalities(); // IF_VERBOSE(1, display(verbose_stream());); } - + void inc_ref() { m_ref++; } - + void dec_ref() { --m_ref; if (m_ref == 0) { dealloc(this); } } - + void display(std::ostream& out) const { ast_manager& m = m_head.get_manager(); expr_ref_vector fmls(m); @@ -304,7 +304,7 @@ namespace tb { } out << mk_pp(fml, m) << "\n"; } - + private: ast_manager& get_manager() const { return m_head.get_manager(); } @@ -314,7 +314,7 @@ namespace tb { // - m_head - head predicate // - m_predicates - auxiliary predicates in body. // - m_constraint - side constraint - // + // void init_from_rule(datalog::rule_ref const& r) { ast_manager& m = get_manager(); expr_ref_vector fmls(m); @@ -328,7 +328,7 @@ namespace tb { m_predicates.reset(); for (unsigned i = 0; i < utsz; ++i) { m_predicates.push_back(r->get_tail(i)); - } + } bool_rewriter(m).mk_and(fmls.size(), fmls.c_ptr(), m_constraint); } @@ -348,13 +348,13 @@ namespace tb { if (get_subst(rw, subst, i, fmls)) { fmls[i] = m.mk_true(); } - } + } subst.apply(1, delta, expr_offset(m_head, 0), tmp); m_head = to_app(tmp); for (unsigned i = 0; i < m_predicates.size(); ++i) { subst.apply(1, delta, expr_offset(m_predicates[i].get(), 0), tmp); m_predicates[i] = to_app(tmp); - } + } bool_rewriter(m).mk_and(fmls.size(), fmls.c_ptr(), m_constraint); subst.apply(1, delta, expr_offset(m_constraint, 0), m_constraint); rw(m_constraint); @@ -404,7 +404,7 @@ namespace tb { throw non_constructor(); } } - void operator()(var* v) { } + void operator()(var* v) { } void operator()(quantifier* ) { throw non_constructor(); } @@ -421,7 +421,7 @@ namespace tb { return true; } - }; + }; // rules class rules { @@ -456,7 +456,7 @@ namespace tb { func_decl* f = g->get_decl(); map::obj_map_entry* e = m_index.insert_if_not_there2(f, unsigned_vector()); SASSERT(e); - e->get_data().m_value.push_back(idx); + e->get_data().m_value.push_back(idx); } unsigned get_num_rules(func_decl* p) const { @@ -475,14 +475,14 @@ namespace tb { for (; it != end; ++it) { decls.push_back(it->m_key); } - } + } ref get_rule(func_decl* p, unsigned idx) const { map::obj_map_entry* e = m_index.find_core(p); SASSERT(p); unsigned rule_id = e->get_data().get_value()[idx]; return m_rules[rule_id]; - } + } private: void reset() { m_rules.reset(); @@ -509,7 +509,7 @@ namespace tb { bool_rewriter m_rw; smt_params m_fparams; smt::kernel m_solver; - + public: index(ast_manager& m): m(m), @@ -520,7 +520,7 @@ namespace tb { m_matcher(m), m_refs(m), m_subst(m), - m_qe(m), + m_qe(m, params_ref()), m_rw(m), m_solver(m, m_fparams) {} @@ -544,7 +544,7 @@ namespace tb { } private: - + void setup(clause const& g) { m_preds.reset(); m_refs.reset(); @@ -569,8 +569,8 @@ namespace tb { } vs(g.get_constraint(), vars.size(), vars.c_ptr(), fml); fmls.push_back(fml); - m_precond = m.mk_and(fmls.size(), fmls.c_ptr()); - IF_VERBOSE(2, + m_precond = m.mk_and(fmls.size(), fmls.c_ptr()); + IF_VERBOSE(2, verbose_stream() << "setup-match: "; for (unsigned i = 0; i < m_preds.size(); ++i) { verbose_stream() << mk_pp(m_preds[i].get(), m) << " "; @@ -587,18 +587,18 @@ namespace tb { return true; } } - return false; + return false; } // // check that each predicate in r is matched by some predicate in premise. // for now: skip multiple matches within the same rule (incomplete). // bool match_rule(unsigned rule_index) { - clause const& g = *m_index[rule_index]; + clause const& g = *m_index[rule_index]; m_sideconds.reset(); m_subst.reset(); m_subst.reserve(2, g.get_num_vars()); - + IF_VERBOSE(2, g.display(verbose_stream() << "try-match\n");); return match_head(g); @@ -628,9 +628,9 @@ namespace tb { } verbose_stream() << mk_pp(q, m) << " = " << mk_pp(p, m) << "\n"; ); - - if (q->get_decl() == p->get_decl() && + + if (q->get_decl() == p->get_decl() && m_matcher(q, p, m_subst, m_sideconds) && match_predicates(predicate_index + 1, g)) { return true; @@ -646,7 +646,7 @@ namespace tb { expr_ref q(m), postcond(m); expr_ref_vector fmls(m_sideconds); m_subst.reset_cache(); - + for (unsigned i = 0; !m.canceled() && i < fmls.size(); ++i) { m_subst.apply(2, deltas, expr_offset(fmls[i].get(), 0), q); fmls[i] = q; @@ -680,7 +680,7 @@ namespace tb { verbose_stream() << "check: " << mk_pp(postcond, m, 7 + g.get_num_predicates()) << "\n";); if (!is_ground(postcond)) { - IF_VERBOSE(1, verbose_stream() << "TBD: non-ground\n" + IF_VERBOSE(1, verbose_stream() << "TBD: non-ground\n" << mk_pp(postcond, m) << "\n"; m_clause->display(verbose_stream()); verbose_stream() << "\n=>\n"; @@ -743,7 +743,7 @@ namespace tb { double m_weight_multiply; unsigned m_update_frequency; unsigned m_next_update; - + public: selection(datalog::context& ctx): @@ -766,7 +766,7 @@ namespace tb { scores.reset(); basic_score_predicate(p, scores); insert_score(p->get_decl(), scores); - } + } normalize_scores(rs); } @@ -783,7 +783,7 @@ namespace tb { default: return weight_select(g); - } + } } void reset() { @@ -867,8 +867,8 @@ namespace tb { } } IF_VERBOSE(1, verbose_stream() << "select:" << result << "\n";); - - return result; + + return result; } unsigned basic_weight_select(clause const& g) { @@ -957,7 +957,7 @@ namespace tb { } } - + double score_predicate(app* p) { double score = 1; if (find_score(p, score)) { @@ -1031,7 +1031,7 @@ namespace tb { } else { m_score_map.insert(f, scores); - } + } } }; @@ -1044,15 +1044,15 @@ namespace tb { expr_ref_vector m_sub1; expr_ref_vector m_sub2; public: - unifier(ast_manager& m): - m(m), + unifier(ast_manager& m): + m(m), m_unifier(m), m_S1(m), m_S2(m, false), m_rename(m), - m_sub1(m), + m_sub1(m), m_sub2(m) {} - + bool operator()(ref& tgt, unsigned idx, ref& src, bool compute_subst, ref& result) { return unify(*tgt, idx, *src, compute_subst, result); } @@ -1066,12 +1066,12 @@ namespace tb { } } - bool unify(clause const& tgt, unsigned idx, clause const& src, bool compute_subst, ref& result) { - qe_lite qe(m); + bool unify(clause const& tgt, unsigned idx, clause const& src, bool compute_subst, ref& result) { + qe_lite qe(m, params_ref()); reset(); SASSERT(tgt.get_predicate(idx)->get_decl() == src.get_decl()); unsigned var_cnt = std::max(tgt.get_num_vars(), src.get_num_vars()); - m_S1.reserve(2, var_cnt); + m_S1.reserve(2, var_cnt); if (!m_unifier(tgt.get_predicate(idx), src.get_head(), m_S1)) { return false; } @@ -1080,7 +1080,7 @@ namespace tb { app_ref head(m); result = alloc(clause, m); unsigned delta[2] = { 0, var_cnt }; - m_S1.apply(2, delta, expr_offset(tgt.get_head(), 0), tmp); + m_S1.apply(2, delta, expr_offset(tgt.get_head(), 0), tmp); head = to_app(tmp); for (unsigned i = 0; i < tgt.get_num_predicates(); ++i) { if (i != idx) { @@ -1096,7 +1096,7 @@ namespace tb { } m_S1.apply(2, delta, expr_offset(tgt.get_constraint(), 0), tmp); m_S1.apply(2, delta, expr_offset(src.get_constraint(), 1), tmp2); - constraint = m.mk_and(tmp, tmp2); + constraint = m.mk_and(tmp, tmp2); // perform trival quantifier-elimination: uint_set index_set; @@ -1114,7 +1114,7 @@ namespace tb { if (m.is_false(constraint)) { return false; } - + // initialize rule. result->init(head, predicates, constraint); ptr_vector vars; @@ -1147,10 +1147,10 @@ namespace tb { extract_subst(delta, src, 1); } // init result using head, predicates, constraint - return true; + return true; } - - + + private: void reset() { m_S1.reset(); @@ -1175,9 +1175,9 @@ namespace tb { else { insert_subst(offset, m.mk_true()); } - } + } } - + void insert_subst(unsigned offset, expr* e) { if (offset == 0) { m_sub1.push_back(e); @@ -1201,7 +1201,7 @@ namespace tb { // - // Given a clause + // Given a clause // P(s) :- P(t), Phi(x). // Compute the clauses: // acc: P(s) :- Delta(z,t), P(z), Phi(x). @@ -1237,7 +1237,7 @@ namespace tb { head = m.mk_app(delta, zszs.size(), zszs.c_ptr()); for (unsigned i = 0; i < zs.size(); ++i) { zszs[i+zs.size()] = q->get_arg(i); - } + } pred = m.mk_app(delta, zszs.size(), zszs.c_ptr()); preds.push_back(pred); for (unsigned i = 1; i < g.get_num_predicates(); ++i) { @@ -1247,28 +1247,28 @@ namespace tb { preds.push_back(m.mk_app(q->get_decl(), zs.size(), zs.c_ptr())); acc->init(p, preds, g.get_constraint()); - IF_VERBOSE(1, + IF_VERBOSE(1, delta1->display(verbose_stream() << "delta1:\n"); delta2->display(verbose_stream() << "delta2:\n"); acc->display(verbose_stream() << "acc:\n");); } - // + // // Given a sequence of clauses and inference rules // compute a super-predicate and auxiliary clauses. - // + // // P1(x) :- P2(y), R(z) // P2(y) :- P3(z), T(u) // P3(z) :- P1(x), U(v) // => // P1(x) :- P1(x), R(z), T(u), U(v) - // + // ref resolve_rules(unsigned num_clauses, clause*const* clauses, unsigned const* positions) { ref result = clauses[0]; ref tmp; unsigned offset = 0; - for (unsigned i = 0; i + 1 < num_clauses; ++i) { + for (unsigned i = 0; i + 1 < num_clauses; ++i) { clause const& cl = *clauses[i+1]; offset += positions[i]; VERIFY (m_unifier.unify(*result, offset, cl, false, tmp)); @@ -1276,7 +1276,7 @@ namespace tb { } return result; } - + private: @@ -1286,7 +1286,7 @@ namespace tb { unsigned num_vars = g.get_num_vars(); for (unsigned i = 0; i < p->get_num_args(); ++i) { result.push_back(m.mk_var(num_vars+i, m.get_sort(p->get_arg(i)))); - } + } return result; } }; @@ -1341,7 +1341,7 @@ namespace datalog { uint_set m_displayed_rules; public: imp(context& ctx): - m_ctx(ctx), + m_ctx(ctx), m(ctx.get_manager()), rm(ctx.get_rule_manager()), m_index(m), @@ -1358,7 +1358,7 @@ namespace datalog { m_fparams.m_timeout = 1000; } - ~imp() {} + ~imp() {} lbool query(expr* query) { m_ctx.ensure_opened(); @@ -1378,7 +1378,7 @@ namespace datalog { IF_VERBOSE(1, display_clause(*get_clause(), verbose_stream() << "g" << get_clause()->get_seqno() << " ");); return run(); } - + void cleanup() { m_clauses.reset(); } @@ -1400,7 +1400,7 @@ namespace datalog { expr_ref get_answer() const { switch(m_status) { - case l_undef: + case l_undef: UNREACHABLE(); return expr_ref(m.mk_false(), m); case l_true: { @@ -1415,7 +1415,7 @@ namespace datalog { return expr_ref(m.mk_true(), m); } private: - + void select_predicate() { tb::clause & g = *get_clause(); unsigned num_predicates = g.get_num_predicates(); @@ -1430,17 +1430,17 @@ namespace datalog { IF_VERBOSE(2, verbose_stream() << mk_pp(g.get_predicate(pi), m) << "\n";); } } - + void apply_rule(ref& r) { ref clause = get_clause(); - ref next_clause; + ref next_clause; if (m_unifier(clause, clause->get_predicate_index(), r, false, next_clause) && !query_is_tautology(*next_clause)) { init_clause(next_clause); unsigned subsumer = 0; - IF_VERBOSE(1, + IF_VERBOSE(1, display_rule(*clause, verbose_stream()); - display_premise(*clause, + display_premise(*clause, verbose_stream() << "g" << next_clause->get_seqno() << " "); display_clause(*next_clause, verbose_stream()); ); @@ -1462,8 +1462,8 @@ namespace datalog { m_instruction = tb::SELECT_RULE; } } - - void select_rule() { + + void select_rule() { tb::clause& g = *get_clause(); g.inc_next_rule(); unsigned pi = g.get_predicate_index(); @@ -1481,7 +1481,7 @@ namespace datalog { void backtrack() { SASSERT(!m_clauses.empty()); - m_clauses.pop_back(); + m_clauses.pop_back(); if (m_clauses.empty()) { m_instruction = tb::SATISFIABLE; } @@ -1500,16 +1500,16 @@ namespace datalog { return l_undef; } switch(m_instruction) { - case tb::SELECT_PREDICATE: - select_predicate(); + case tb::SELECT_PREDICATE: + select_predicate(); break; - case tb::SELECT_RULE: - select_rule(); + case tb::SELECT_RULE: + select_rule(); break; case tb::BACKTRACK: backtrack(); break; - case tb::SATISFIABLE: + case tb::SATISFIABLE: m_status = l_false; return l_false; case tb::UNSATISFIABLE: @@ -1522,18 +1522,18 @@ namespace datalog { return l_undef; } } - } + } bool query_is_tautology(tb::clause const& g) { expr_ref fml = g.to_formula(); fml = m.mk_not(fml); m_solver.push(); m_solver.assert_expr(fml); - lbool is_sat = m_solver.check(); + lbool is_sat = m_solver.check(); m_solver.pop(1); TRACE("dl", tout << is_sat << ":\n" << mk_pp(fml, m) << "\n";); - + return l_false == is_sat; } @@ -1560,7 +1560,7 @@ namespace datalog { void display_premise(tb::clause& p, std::ostream& out) { func_decl* f = p.get_predicate(p.get_predicate_index())->get_decl(); - out << "{g" << p.get_seqno() << " " << f->get_name() << " pos: " + out << "{g" << p.get_seqno() << " " << f->get_name() << " pos: " << p.get_predicate_index() << " rule: " << p.get_next_rule() << "}\n"; } @@ -1576,21 +1576,21 @@ namespace datalog { ref replayed_clause; replace_proof_converter pc(m); - // clause is a empty clause. + // clause is a empty clause. // Pretend it is asserted. // It gets replaced by premises. - SASSERT(clause->get_num_predicates() == 0); + SASSERT(clause->get_num_predicates() == 0); expr_ref root = clause->to_formula(); vector substs; - while (0 != clause->get_index()) { - SASSERT(clause->get_parent_index() < clause->get_index()); + while (0 != clause->get_index()) { + SASSERT(clause->get_parent_index() < clause->get_index()); unsigned p_index = clause->get_parent_index(); unsigned p_rule = clause->get_parent_rule(); ref parent = m_clauses[p_index]; unsigned pi = parent->get_predicate_index(); func_decl* pred = parent->get_predicate(pi)->get_decl(); - ref rl = m_rules.get_rule(pred, p_rule); + ref rl = m_rules.get_rule(pred, p_rule); VERIFY(m_unifier(parent, parent->get_predicate_index(), rl, true, replayed_clause)); expr_ref_vector s1(m_unifier.get_rule_subst(true)); expr_ref_vector s2(m_unifier.get_rule_subst(false)); @@ -1614,36 +1614,36 @@ namespace datalog { } expr_ref body = clause.get_body(); var_subst vs(m, false); - vs(body, subst.size(), subst.c_ptr(), body); + vs(body, subst.size(), subst.c_ptr(), body); out << mk_pp(body, m) << "\n"; } - void resolve_rule(replace_proof_converter& pc, tb::clause const& r1, tb::clause const& r2, + void resolve_rule(replace_proof_converter& pc, tb::clause const& r1, tb::clause const& r2, expr_ref_vector const& s1, expr_ref_vector const& s2, tb::clause const& res) const { unsigned idx = r1.get_predicate_index(); expr_ref fml = res.to_formula(); vector substs; svector > positions; substs.push_back(s1); - substs.push_back(s2); + substs.push_back(s2); scoped_proof _sc(m); proof_ref pr(m); proof_ref_vector premises(m); premises.push_back(m.mk_asserted(r1.to_formula())); premises.push_back(m.mk_asserted(r2.to_formula())); - positions.push_back(std::make_pair(idx+1, 0)); + positions.push_back(std::make_pair(idx+1, 0)); pr = m.mk_hyper_resolve(2, premises.c_ptr(), fml, positions, substs); pc.insert(pr); - } + } }; tab::tab(context& ctx): datalog::engine_base(ctx.get_manager(),"tabulation"), - m_imp(alloc(imp, ctx)) { + m_imp(alloc(imp, ctx)) { } tab::~tab() { dealloc(m_imp); - } + } lbool tab::query(expr* query) { return m_imp->query(query); } diff --git a/src/qe/qe_lite.cpp b/src/qe/qe_lite.cpp index 2b73381a9..eccc2d0c7 100644 --- a/src/qe/qe_lite.cpp +++ b/src/qe/qe_lite.cpp @@ -48,8 +48,8 @@ class is_variable_test : public is_variable_proc { is_var_kind m_var_kind; public: is_variable_test(uint_set const& vars, bool index_of_bound) : - m_var_set(vars), - m_num_decls(0), + m_var_set(vars), + m_num_decls(0), m_var_kind(index_of_bound?BY_VAR_SET:BY_VAR_SET_COMPLEMENT) {} is_variable_test(unsigned num_decls) : @@ -83,7 +83,7 @@ namespace eq { is_variable_proc* m_is_variable; var_subst m_subst; expr_ref_vector m_new_exprs; - + ptr_vector m_map; int_vector m_pos2var; ptr_vector m_inx2var; @@ -91,10 +91,11 @@ namespace eq { expr_ref_vector m_subst_map; expr_ref_buffer m_new_args; th_rewriter m_rewriter; - + params_ref m_params; + void der_sort_vars(ptr_vector & vars, ptr_vector & definitions, unsigned_vector & order) { order.reset(); - + // eliminate self loops, and definitions containing quantifiers. bool found = false; for (unsigned i = 0; i < definitions.size(); i++) { @@ -105,18 +106,18 @@ namespace eq { else found = true; // found at least one candidate } - + if (!found) return; - + typedef std::pair frame; svector todo; - + expr_fast_mark1 visiting; expr_fast_mark2 done; - + unsigned vidx, num; - + for (unsigned i = 0; i < definitions.size(); i++) { if (definitions[i] == 0) continue; @@ -193,11 +194,11 @@ namespace eq { } } } - + bool is_variable(expr * e) const { return (*m_is_variable)(e); } - + bool is_neg_var(ast_manager & m, expr * e, var*& v) { expr* e1; if (m.is_not(e, e1) && is_variable(e1)) { @@ -208,13 +209,13 @@ namespace eq { return false; } } - - + + /** - \brief Return true if e can be viewed as a variable disequality. + \brief Return true if e can be viewed as a variable disequality. Store the variable id in v and the definition in t. For example: - + if e is (not (= (VAR 1) T)), then v assigned to 1, and t to T. if e is (iff (VAR 2) T), then v is assigned to 2, and t to (not T). (not T) is used because this formula is equivalent to (not (iff (VAR 2) (not T))), @@ -225,7 +226,7 @@ namespace eq { if (m.is_not(e, e1)) { return is_var_eq(e, vs, ts); } - else if (is_var_eq(e, vs, ts) && vs.size() == 1 && m.is_bool(vs[0])) { + else if (is_var_eq(e, vs, ts) && vs.size() == 1 && m.is_bool(vs[0])) { expr_ref tmp(m); bool_rewriter(m).mk_not(ts[0].get(), tmp); ts[0] = tmp; @@ -305,7 +306,7 @@ namespace eq { todo.pop_back(); if (a.is_add(e)) { for (unsigned i = 0; i < to_app(e)->get_num_args(); ++i) { - todo.push_back(std::make_pair(sign, to_app(e)->get_arg(i))); + todo.push_back(std::make_pair(sign, to_app(e)->get_arg(i))); } } else if (is_invertible_mul(is_int, e, a_val)) { @@ -322,7 +323,7 @@ namespace eq { } return false; } - + bool arith_solve(expr * lhs, expr * rhs, expr * eq, ptr_vector& vs, expr_ref_vector& ts) { return solve_arith(lhs, rhs, vs, ts); } @@ -339,7 +340,7 @@ namespace eq { TRACE("qe_lite", tout << mk_pp(eq, m) << "\n";); return true; } - + bool same_vars(ptr_vector const& vs1, ptr_vector const& vs2) const { if (vs1.size() != vs2.size()) { @@ -356,12 +357,12 @@ namespace eq { /** \brief Return true if e can be viewed as a variable equality. */ - + bool is_var_eq(expr * e, ptr_vector& vs, expr_ref_vector & ts) { expr* lhs, *rhs; var* v; - - // (= VAR t), (iff VAR t), (iff (not VAR) t), (iff t (not VAR)) cases + + // (= VAR t), (iff VAR t), (iff (not VAR) t), (iff t (not VAR)) cases if (m.is_eq(e, lhs, rhs) || m.is_iff(e, lhs, rhs)) { // (iff (not VAR) t) (iff t (not VAR)) cases if (!is_variable(lhs) && !is_variable(rhs) && m.is_bool(lhs)) { @@ -384,7 +385,7 @@ namespace eq { } return false; } - + // (ite cond (= VAR t) (= VAR t2)) case expr* cond, *e2, *e3; if (m.is_ite(e, cond, e2, e3)) { @@ -400,7 +401,7 @@ namespace eq { } return false; } - + // VAR = true case if (is_variable(e)) { ts.push_back(m.mk_true()); @@ -408,7 +409,7 @@ namespace eq { TRACE("qe_lite", tout << mk_pp(e, m) << "\n";); return true; } - + // VAR = false case if (is_neg_var(m, e, v)) { ts.push_back(m.mk_false()); @@ -416,56 +417,56 @@ namespace eq { TRACE("qe_lite", tout << mk_pp(e, m) << "\n";); return true; } - + return false; } - - + + bool is_var_def(bool check_eq, expr* e, ptr_vector& vs, expr_ref_vector& ts) { if (check_eq) { return is_var_eq(e, vs, ts); } else { return is_var_diseq(e, vs, ts); - } + } } - + void get_elimination_order() { m_order.reset(); - + TRACE("top_sort", tout << "DEFINITIONS: " << std::endl; for(unsigned i = 0; i < m_map.size(); i++) if(m_map[i]) tout << "VAR " << i << " = " << mk_pp(m_map[i], m) << std::endl; ); - + der_sort_vars(m_inx2var, m_map, m_order); - - TRACE("qe_lite", + + TRACE("qe_lite", tout << "Elimination m_order:" << std::endl; for(unsigned i=0; iget_expr(); if ((q->is_forall() && m.is_or(e)) || @@ -474,15 +475,15 @@ namespace eq { args = to_app(e)->get_args(); } } - + void apply_substitution(quantifier * q, expr_ref & r) { - + expr * e = q->get_expr(); unsigned num_args = 1; expr* const* args = &e; flatten_args(q, num_args, args); bool_rewriter rw(m); - + // get a new expression m_new_args.reset(); for(unsigned i = 0; i < num_args; i++) { @@ -495,7 +496,7 @@ namespace eq { r = q; return; } - + expr_ref t(m); if (q->is_forall()) { rw.mk_or(m_new_args.size(), m_new_args.c_ptr(), t); @@ -503,9 +504,9 @@ namespace eq { else { rw.mk_and(m_new_args.size(), m_new_args.c_ptr(), t); } - expr_ref new_e(m); + expr_ref new_e(m); m_subst(t, m_subst_map.size(), m_subst_map.c_ptr(), new_e); - + // don't forget to update the quantifier patterns expr_ref_buffer new_patterns(m); expr_ref_buffer new_no_patterns(m); @@ -514,17 +515,17 @@ namespace eq { m_subst(q->get_pattern(j), m_subst_map.size(), m_subst_map.c_ptr(), new_pat); new_patterns.push_back(new_pat); } - + for (unsigned j = 0; j < q->get_num_no_patterns(); j++) { expr_ref new_nopat(m); m_subst(q->get_no_pattern(j), m_subst_map.size(), m_subst_map.c_ptr(), new_nopat); new_no_patterns.push_back(new_nopat); } - - r = m.update_quantifier(q, new_patterns.size(), new_patterns.c_ptr(), + + r = m.update_quantifier(q, new_patterns.size(), new_patterns.c_ptr(), new_no_patterns.size(), new_no_patterns.c_ptr(), new_e); } - + void reduce_quantifier1(quantifier * q, expr_ref & r, proof_ref & pr) { expr * e = q->get_expr(); is_variable_test is_v(q->get_num_decls()); @@ -532,17 +533,17 @@ namespace eq { unsigned num_args = 1; expr* const* args = &e; flatten_args(q, num_args, args); - + unsigned def_count = 0; unsigned largest_vinx = 0; - + find_definitions(num_args, args, q->is_exists(), def_count, largest_vinx); - + if (def_count > 0) { get_elimination_order(); SASSERT(m_order.size() <= def_count); // some might be missing because of cycles - - if (!m_order.empty()) { + + if (!m_order.empty()) { create_substitution(largest_vinx + 1); apply_substitution(q, r); } @@ -554,31 +555,32 @@ namespace eq { TRACE("der_bug", tout << "Did not find any diseq\n" << mk_pp(q, m) << "\n";); r = q; } - + if (m.proofs_enabled()) { pr = r == q ? 0 : m.mk_der(q, r); - } - } - + } + } + void elim_unused_vars(expr_ref& r, proof_ref &pr) { if (is_quantifier(r)) { quantifier * q = to_quantifier(r); - ::elim_unused_vars(m, q, r); + + ::elim_unused_vars(m, q, m_params, r); if (m.proofs_enabled()) { proof * p1 = m.mk_elim_unused_vars(q, r); pr = m.mk_transitivity(pr, p1); } } } - + void find_definitions(unsigned num_args, expr* const* args, bool is_exists, unsigned& def_count, unsigned& largest_vinx) { def_count = 0; largest_vinx = 0; m_map.reset(); m_pos2var.reset(); - m_inx2var.reset(); + m_inx2var.reset(); m_pos2var.reserve(num_args, -1); - + // Find all definitions for (unsigned i = 0; i < num_args; i++) { checkpoint(); @@ -591,12 +593,12 @@ namespace eq { unsigned idx = v->get_idx(); if (m_map.get(idx, 0) == 0) { m_map.reserve(idx + 1, 0); - m_inx2var.reserve(idx + 1, 0); + m_inx2var.reserve(idx + 1, 0); m_map[idx] = t; m_inx2var[idx] = v; m_pos2var[i] = idx; def_count++; - largest_vinx = std::max(idx, largest_vinx); + largest_vinx = std::max(idx, largest_vinx); m_new_exprs.push_back(t); } } @@ -646,10 +648,10 @@ namespace eq { tmp = m.mk_and(conjs.size(), conjs.c_ptr()); tout << "after flatten\n" << mk_pp(tmp, m) << "\n";); } - + void flatten_constructor(app* c, app* r, expr_ref_vector& conjs) { SASSERT(dt.is_constructor(c)); - + func_decl* d = c->get_decl(); if (dt.is_constructor(r->get_decl())) { @@ -661,7 +663,7 @@ namespace eq { } else { conjs.push_back(m.mk_false()); - } + } } else { func_decl* rec = dt.get_constructor_recognizer(d); @@ -683,7 +685,7 @@ namespace eq { bool remove_unconstrained(expr_ref_vector& conjs) { bool reduced = false, change = true; - expr* r, *l, *ne; + expr* r, *l, *ne; while (change) { change = false; for (unsigned i = 0; i < conjs.size(); ++i) { @@ -704,21 +706,21 @@ namespace eq { } return reduced; } - + bool reduce_var_set(expr_ref_vector& conjs) { unsigned def_count = 0; unsigned largest_vinx = 0; bool reduced = false; flatten_definitions(conjs); - + find_definitions(conjs.size(), conjs.c_ptr(), true, def_count, largest_vinx); - + if (def_count > 0) { get_elimination_order(); SASSERT(m_order.size() <= def_count); // some might be missing because of cycles - - if (!m_order.empty()) { + + if (!m_order.empty()) { expr_ref r(m), new_r(m); r = m.mk_and(conjs.size(), conjs.c_ptr()); create_substitution(largest_vinx + 1); @@ -739,35 +741,36 @@ namespace eq { void checkpoint() { cooperate("der"); - if (m.canceled()) + if (m.canceled()) throw tactic_exception(m.limit().get_cancel_msg()); } public: - der(ast_manager & m): - m(m), + der(ast_manager & m, params_ref const & p): + m(m), a(m), dt(m), - m_is_variable(0), - m_subst(m), - m_new_exprs(m), - m_subst_map(m), - m_new_args(m), - m_rewriter(m) {} - + m_is_variable(0), + m_subst(m), + m_new_exprs(m), + m_subst_map(m), + m_new_args(m), + m_rewriter(m), + m_params(p) {} + void set_is_variable_proc(is_variable_proc& proc) { m_is_variable = &proc;} - + void operator()(quantifier * q, expr_ref & r, proof_ref & pr) { - TRACE("qe_lite", tout << mk_pp(q, m) << "\n";); + TRACE("qe_lite", tout << mk_pp(q, m) << "\n";); pr = 0; r = q; - reduce_quantifier(q, r, pr); + reduce_quantifier(q, r, pr); if (r != q) { elim_unused_vars(r, pr); } } - - void reduce_quantifier(quantifier * q, expr_ref & r, proof_ref & pr) { + + void reduce_quantifier(quantifier * q, expr_ref & r, proof_ref & pr) { r = q; // Keep applying reduce_quantifier1 until r doesn't change anymore do { @@ -779,15 +782,15 @@ namespace eq { pr = m.mk_transitivity(pr, curr_pr); } } while (q != r && is_quantifier(r)); - + m_new_exprs.reset(); } - + void operator()(expr_ref_vector& r) { while (reduce_var_set(r)) ; m_new_exprs.reset(); } - + ast_manager& get_manager() const { return m; } @@ -804,7 +807,7 @@ namespace ar { is_variable_proc* m_is_variable; ptr_vector m_todo; expr_mark m_visited; - + bool is_variable(expr * e) const { return (*m_is_variable)(e); } @@ -827,7 +830,7 @@ namespace ar { Ex A. Phi[store(A,x,t)] Perhaps also: - Ex A. store(A,y,z)[x] = t & Phi where x \not\in A, t, y, z, A \not\in y z, t + Ex A. store(A,y,z)[x] = t & Phi where x \not\in A, t, y, z, A \not\in y z, t => Ex A, v . (x = y => z = t) & Phi[store(store(A,x,t),y,v)] @@ -873,7 +876,7 @@ namespace ar { bool solve_select(expr_ref_vector& conjs, unsigned i, expr* e) { expr* e1, *e2; - return + return m.is_eq(e, e1, e2) && (solve_select(conjs, i, e1, e2) || solve_select(conjs, i, e2, e1)); @@ -887,8 +890,8 @@ namespace ar { bool solve_neq_select(expr_ref_vector& conjs, unsigned i, expr* e) { expr* e1, *a1, *a2; if (m.is_not(e, e1) && m.is_eq(e1, a1, a2)) { - if (a.is_select(a1) && - a.is_select(a2) && + if (a.is_select(a1) && + a.is_select(a2) && to_app(a1)->get_num_args() == to_app(a2)->get_num_args()) { expr* e1 = to_app(a1)->get_arg(0); expr* e2 = to_app(a2)->get_arg(0); @@ -937,7 +940,7 @@ namespace ar { void operator()(expr* e) {} void set_is_variable_proc(is_variable_proc& proc) { m_is_variable = &proc;} - + }; }; // namespace ar @@ -976,27 +979,27 @@ namespace fm { for (; it != end; ++it) it->~rational(); } - + unsigned hash() const { return hash_u(m_id); } }; - + typedef ptr_vector constraints; - + class constraint_set { - unsigned_vector m_id2pos; + unsigned_vector m_id2pos; constraints m_set; public: typedef constraints::const_iterator iterator; - - bool contains(constraint const & c) const { - if (c.m_id >= m_id2pos.size()) - return false; - return m_id2pos[c.m_id] != UINT_MAX; + + bool contains(constraint const & c) const { + if (c.m_id >= m_id2pos.size()) + return false; + return m_id2pos[c.m_id] != UINT_MAX; } - + bool empty() const { return m_set.empty(); } unsigned size() const { return m_set.size(); } - + void insert(constraint & c) { unsigned id = c.m_id; m_id2pos.reserve(id+1, UINT_MAX); @@ -1006,7 +1009,7 @@ namespace fm { m_id2pos[id] = pos; m_set.push_back(&c); } - + void erase(constraint & c) { unsigned id = c.m_id; if (id >= m_id2pos.size()) @@ -1018,27 +1021,27 @@ namespace fm { unsigned last_pos = m_set.size() - 1; if (pos != last_pos) { constraint * last_c = m_set[last_pos]; - m_set[pos] = last_c; + m_set[pos] = last_c; m_id2pos[last_c->m_id] = pos; } m_set.pop_back(); } - + constraint & erase() { SASSERT(!empty()); - constraint & c = *m_set.back(); + constraint & c = *m_set.back(); m_id2pos[c.m_id] = UINT_MAX; m_set.pop_back(); return c; } - + void reset() { m_id2pos.reset(); m_set.reset(); } void finalize() { m_id2pos.finalize(); m_set.finalize(); } - + iterator begin() const { return m_set.begin(); } iterator end() const { return m_set.end(); } }; - + class fm { ast_manager & m; is_variable_proc* m_is_variable; @@ -1068,24 +1071,24 @@ namespace fm { bool m_inconsistent; expr_dependency_ref m_inconsistent_core; constraint_set m_sub_todo; - + // --------------------------- // // OCC clause recognizer // // --------------------------- - + bool is_literal(expr * t) const { expr * atom; return is_uninterp_const(t) || (m.is_not(t, atom) && is_uninterp_const(atom)); } - + bool is_constraint(expr * t) const { return !is_literal(t); } - + bool is_var(expr * t, expr * & x) const { - + if ((*m_is_variable)(t)) { x = t; return true; @@ -1096,24 +1099,24 @@ namespace fm { } return false; } - + bool is_var(expr * t) const { expr * x; return is_var(t, x); } - + bool is_linear_mon_core(expr * t, expr * & x) const { expr * c; if (m_util.is_mul(t, c, x) && m_util.is_numeral(c) && is_var(x, x)) return true; return is_var(t, x); } - + bool is_linear_mon(expr * t) const { expr * x; return is_linear_mon_core(t, x); } - + bool is_linear_pol(expr * t) const { unsigned num_mons; expr * const * mons; @@ -1125,7 +1128,7 @@ namespace fm { num_mons = 1; mons = &t; } - + expr_fast_mark2 visited; bool all_forbidden = true; for (unsigned i = 0; i < num_mons; i++) { @@ -1141,7 +1144,7 @@ namespace fm { } return !all_forbidden; } - + bool is_linear_ineq(expr * t) const { bool result = false; m.is_not(t, t); @@ -1153,7 +1156,7 @@ namespace fm { return result; } - + bool is_occ(expr * t) { if (m_fm_occ && m.is_or(t)) { unsigned num = to_app(t)->get_num_args(); @@ -1176,7 +1179,7 @@ namespace fm { } return is_linear_ineq(t); } - + // --------------------------- // // Memory mng @@ -1195,12 +1198,12 @@ namespace fm { for (unsigned i = 0; i < sz; i++) del_constraint(cs[i]); } - + void reset_constraints() { del_constraints(m_constraints.size(), m_constraints.c_ptr()); m_constraints.reset(); } - + constraint * mk_constraint(unsigned num_lits, literal * lits, unsigned num_vars, var * xs, rational * as, rational & c, bool strict, expr_dependency * dep) { unsigned sz = constraint::get_obj_size(num_lits, num_vars); @@ -1236,15 +1239,15 @@ namespace fm { m.inc_ref(dep); return cnstr; } - + // --------------------------- // // Util // // --------------------------- - + unsigned num_vars() const { return m_is_int.size(); } - + // multiply as and c, by the lcm of their denominators void mk_int(unsigned num, rational * as, rational & c) { rational l = denominator(c); @@ -1259,7 +1262,7 @@ namespace fm { SASSERT(as[i].is_int()); } } - + void normalize_coeffs(constraint & c) { if (c.m_num_vars == 0) return; @@ -1281,7 +1284,7 @@ namespace fm { for (unsigned i = 0; i < c.m_num_vars; i++) c.m_as[i] /= g; } - + void display(std::ostream & out, constraint const & c) const { for (unsigned i = 0; i < c.m_num_lits; i++) { literal l = c.m_lits[i]; @@ -1308,10 +1311,10 @@ namespace fm { out << c.m_c; out << ")"; } - + /** \brief Return true if c1 subsumes c2 - + c1 subsumes c2 If 1) All literals of c1 are literals of c2 2) polynomial of c1 == polynomial of c2 @@ -1329,13 +1332,13 @@ namespace fm { return false; if (!c1.m_strict && c2.m_strict && c1.m_c == c2.m_c) return false; - + m_counter += c1.m_num_lits + c2.m_num_lits; - + for (unsigned i = 0; i < c1.m_num_vars; i++) { m_var2pos[c1.m_xs[i]] = i; } - + bool failed = false; for (unsigned i = 0; i < c2.m_num_vars; i++) { unsigned pos1 = m_var2pos[c2.m_xs[i]]; @@ -1344,21 +1347,21 @@ namespace fm { break; } } - + for (unsigned i = 0; i < c1.m_num_vars; i++) { m_var2pos[c1.m_xs[i]] = UINT_MAX; } - + if (failed) return false; - + for (unsigned i = 0; i < c2.m_num_lits; i++) { literal l = c2.m_lits[i]; bvar b = lit2bvar(l); SASSERT(m_bvar2sign[b] == 0); m_bvar2sign[b] = sign(l) ? -1 : 1; } - + for (unsigned i = 0; i < c1.m_num_lits; i++) { literal l = c1.m_lits[i]; bvar b = lit2bvar(l); @@ -1368,19 +1371,19 @@ namespace fm { break; } } - + for (unsigned i = 0; i < c2.m_num_lits; i++) { literal l = c2.m_lits[i]; bvar b = lit2bvar(l); m_bvar2sign[b] = 0; } - + if (failed) return false; - + return true; } - + void backward_subsumption(constraint const & c) { if (c.m_num_vars == 0) return; @@ -1422,7 +1425,7 @@ namespace fm { } cs.set_end(it2); } - + void subsume() { while (!m_sub_todo.empty()) { constraint & c = m_sub_todo.erase(); @@ -1433,13 +1436,13 @@ namespace fm { } public: - + // --------------------------- // // Initialization // // --------------------------- - + fm(ast_manager & _m): m(_m), m_is_variable(0), @@ -1453,11 +1456,11 @@ namespace fm { m_counter = 0; m_inconsistent = false; } - + ~fm() { reset_constraints(); } - + void updt_params() { m_fm_real_only = false; m_fm_limit = 5000000; @@ -1466,9 +1469,9 @@ namespace fm { m_fm_extra = 0; m_fm_occ = true; } - + private: - + struct forbidden_proc { fm & m_owner; forbidden_proc(fm & o):m_owner(o) {} @@ -1480,7 +1483,7 @@ namespace fm { void operator()(app * n) { } void operator()(quantifier * n) {} }; - + void init_forbidden_set(expr_ref_vector const & g) { m_forbidden_set.reset(); expr_fast_mark1 visited; @@ -1494,7 +1497,7 @@ namespace fm { quick_for_each_expr(proc, visited, f); } } - + void init(expr_ref_vector const & g) { m_sub_todo.reset(); m_id_gen.reset(); @@ -1517,24 +1520,24 @@ namespace fm { m_inconsistent_core = 0; init_forbidden_set(g); } - + // --------------------------- // // Internal data-structures // // --------------------------- - + static bool sign(literal l) { return l < 0; } static bvar lit2bvar(literal l) { return l < 0 ? -l : l; } - - bool is_int(var x) const { + + bool is_int(var x) const { return m_is_int[x] != 0; } - + bool is_forbidden(var x) const { return m_forbidden[x] != 0; } - + bool all_int(constraint const & c) const { for (unsigned i = 0; i < c.m_num_vars; i++) { if (!is_int(c.m_xs[i])) @@ -1542,7 +1545,7 @@ namespace fm { } return true; } - + app * to_expr(constraint const & c) { expr * ineq; if (c.m_num_vars == 0) { @@ -1577,20 +1580,20 @@ namespace fm { ineq = m_util.mk_le(lhs, rhs); } } - + if (c.m_num_lits == 0) { if (ineq) return to_app(ineq); else return m.mk_false(); } - + ptr_buffer lits; for (unsigned i = 0; i < c.m_num_lits; i++) { literal l = c.m_lits[i]; if (sign(l)) lits.push_back(m.mk_not(m_bvar2expr.get(lit2bvar(l)))); - else + else lits.push_back(m_bvar2expr.get(lit2bvar(l))); } if (ineq) @@ -1600,7 +1603,7 @@ namespace fm { else return m.mk_or(lits.size(), lits.c_ptr()); } - + var mk_var(expr * t) { SASSERT(::is_var(t)); SASSERT(m_util.is_int(t) || m_util.is_real(t)); @@ -1617,12 +1620,12 @@ namespace fm { SASSERT(m_var2expr.size() == m_is_int.size()); SASSERT(m_lowers.size() == m_is_int.size()); SASSERT(m_uppers.size() == m_is_int.size()); - SASSERT(m_forbidden.size() == m_is_int.size()); + SASSERT(m_forbidden.size() == m_is_int.size()); SASSERT(m_var2pos.size() == m_is_int.size()); TRACE("qe_lite", tout << mk_pp(t,m) << " |-> " << x << " forbidden: " << forbidden << "\n";); return x; } - + bvar mk_bvar(expr * t) { SASSERT(is_uninterp_const(t)); SASSERT(m.is_bool(t)); @@ -1634,7 +1637,7 @@ namespace fm { SASSERT(p > 0); return p; } - + var to_var(expr * t) { var x; if (!m_expr2var.find(t, x)) @@ -1644,22 +1647,22 @@ namespace fm { TRACE("qe_lite", tout << mk_ismt2_pp(t, m) << " --> " << x << "\n";); return x; } - + bvar to_bvar(expr * t) { bvar p; if (m_expr2bvar.find(t, p)) return p; return mk_bvar(t); } - + literal to_literal(expr * t) { if (m.is_not(t, t)) - return -to_bvar(t); + return -to_bvar(t); else return to_bvar(t); } - - + + void add_constraint(expr * f, expr_dependency * dep) { TRACE("qe_lite", tout << mk_pp(f, m) << "\n";); SASSERT(!m.is_or(f) || m_fm_occ); @@ -1711,7 +1714,7 @@ namespace fm { num_mons = 1; mons = &lhs; } - + bool all_int = true; for (unsigned j = 0; j < num_mons; j++) { expr * monomial = mons[j]; @@ -1740,9 +1743,9 @@ namespace fm { } } } - + TRACE("qe_lite", tout << "before mk_constraint: "; for (unsigned i = 0; i < xs.size(); i++) tout << " " << xs[i]; tout << "\n";); - + constraint * new_c = mk_constraint(lits.size(), lits.c_ptr(), xs.size(), @@ -1751,15 +1754,15 @@ namespace fm { c, strict, dep); - + TRACE("qe_lite", tout << "add_constraint: "; display(tout, *new_c); tout << "\n";); VERIFY(register_constraint(new_c)); } - + bool is_false(constraint const & c) const { return c.m_num_lits == 0 && c.m_num_vars == 0 && (c.m_c.is_neg() || (c.m_strict && c.m_c.is_zero())); } - + bool register_constraint(constraint * c) { normalize_coeffs(*c); if (is_false(*c)) { @@ -1768,20 +1771,20 @@ namespace fm { TRACE("qe_lite", tout << "is false "; display(tout, *c); tout << "\n";); return false; } - + bool r = false; - + for (unsigned i = 0; i < c->m_num_vars; i++) { var x = c->m_xs[i]; if (!is_forbidden(x)) { r = true; - if (c->m_as[i].is_neg()) + if (c->m_as[i].is_neg()) m_lowers[x].push_back(c); else m_uppers[x].push_back(c); } } - + if (r) { m_sub_todo.insert(*c); m_constraints.push_back(c); @@ -1794,7 +1797,7 @@ namespace fm { return false; } } - + void init_use_list(expr_ref_vector const & g) { unsigned sz = g.size(); for (unsigned i = 0; !m_inconsistent && i < sz; i++) { @@ -1812,13 +1815,13 @@ namespace fm { return UINT_MAX; return static_cast(r); } - + typedef std::pair x_cost; - + struct x_cost_lt { char_vector const m_is_int; x_cost_lt(char_vector & is_int):m_is_int(is_int) {} - bool operator()(x_cost const & p1, x_cost const & p2) const { + bool operator()(x_cost const & p1, x_cost const & p2) const { // Integer variables with cost 0 can be eliminated even if they depend on real variables. // Cost 0 == no lower or no upper bound. if (p1.second == 0) { @@ -1828,7 +1831,7 @@ namespace fm { if (p2.second == 0) return false; bool int1 = m_is_int[p1.first] != 0; bool int2 = m_is_int[p2.first] != 0; - return (!int1 && int2) || (int1 == int2 && p1.second < p2.second); + return (!int1 && int2) || (int1 == int2 && p1.second < p2.second); } }; @@ -1842,7 +1845,7 @@ namespace fm { } // x_cost_lt is not a total order on variables std::stable_sort(x_cost_vector.begin(), x_cost_vector.end(), x_cost_lt(m_is_int)); - TRACE("qe_lite", + TRACE("qe_lite", svector::iterator it2 = x_cost_vector.begin(); svector::iterator end2 = x_cost_vector.end(); for (; it2 != end2; ++it2) { @@ -1855,7 +1858,7 @@ namespace fm { xs.push_back(it2->first); } } - + void cleanup_constraints(constraints & cs) { unsigned j = 0; unsigned sz = cs.size(); @@ -1868,7 +1871,7 @@ namespace fm { } cs.shrink(j); } - + // Set all_int = true if all variables in c are int. // Set unit_coeff = true if the coefficient of x in c is 1 or -1. // If all_int = false, then unit_coeff may not be set. @@ -1900,8 +1903,8 @@ namespace fm { unit_coeff = false; } } - - // An integer variable x may be eliminated, if + + // An integer variable x may be eliminated, if // 1- All variables in the contraints it occur are integer. // 2- The coefficient of x in all lower bounds (or all upper bounds) is unit. bool can_eliminate(var x) const { @@ -1915,7 +1918,7 @@ namespace fm { analyze(m_uppers[x], x, all_int, u_unit); return all_int && (l_unit || u_unit); } - + void copy_constraints(constraints const & s, clauses & t) { constraints::const_iterator it = s.begin(); constraints::const_iterator end = s.end(); @@ -1924,23 +1927,23 @@ namespace fm { t.push_back(c); } } - + clauses tmp_clauses; void save_constraints(var x) { } - + void mark_constraints_dead(constraints const & cs) { constraints::const_iterator it = cs.begin(); constraints::const_iterator end = cs.end(); for (; it != end; ++it) (*it)->m_dead = true; } - + void mark_constraints_dead(var x) { save_constraints(x); mark_constraints_dead(m_lowers[x]); mark_constraints_dead(m_uppers[x]); } - + void get_coeff(constraint const & c, var x, rational & a) { for (unsigned i = 0; i < c.m_num_vars; i++) { if (c.m_xs[i] == x) { @@ -1950,11 +1953,11 @@ namespace fm { } UNREACHABLE(); } - + var_vector new_xs; vector new_as; svector new_lits; - + constraint * resolve(constraint const & l, constraint const & u, var x) { m_counter += l.m_num_vars + u.m_num_vars + l.m_num_lits + u.m_num_lits; rational a, b; @@ -1963,14 +1966,14 @@ namespace fm { SASSERT(a.is_neg()); SASSERT(b.is_pos()); a.neg(); - + SASSERT(!is_int(x) || a.is_one() || b.is_one()); - + new_xs.reset(); new_as.reset(); rational new_c = l.m_c*b + u.m_c*a; bool new_strict = l.m_strict || u.m_strict; - + for (unsigned i = 0; i < l.m_num_vars; i++) { var xi = l.m_xs[i]; if (xi == x) @@ -1983,7 +1986,7 @@ namespace fm { SASSERT(new_xs[m_var2pos[xi]] == xi); SASSERT(new_xs.size() == new_as.size()); } - + for (unsigned i = 0; i < u.m_num_vars; i++) { var xi = u.m_xs[i]; if (xi == x) @@ -1997,7 +2000,7 @@ namespace fm { new_as[pos] += u.m_as[i] * a; } } - + // remove zeros and check whether all variables are int bool all_int = true; unsigned sz = new_xs.size(); @@ -2015,17 +2018,17 @@ namespace fm { } new_xs.shrink(j); new_as.shrink(j); - + if (all_int && new_strict) { new_strict = false; new_c --; } - + // reset m_var2pos for (unsigned i = 0; i < l.m_num_vars; i++) { m_var2pos[l.m_xs[i]] = UINT_MAX; } - + if (new_xs.empty() && (new_c.is_pos() || (!new_strict && new_c.is_zero()))) { // literal is true TRACE("qe_lite", tout << "resolution " << x << " consequent literal is always true: \n"; @@ -2034,7 +2037,7 @@ namespace fm { display(tout, u); tout << "\n";); return 0; // no constraint needs to be created. } - + new_lits.reset(); for (unsigned i = 0; i < l.m_num_lits; i++) { literal lit = l.m_lits[i]; @@ -2042,7 +2045,7 @@ namespace fm { m_bvar2sign[p] = sign(lit) ? -1 : 1; new_lits.push_back(lit); } - + bool tautology = false; for (unsigned i = 0; i < u.m_num_lits && !tautology; i++) { literal lit = u.m_lits[i]; @@ -2063,14 +2066,14 @@ namespace fm { UNREACHABLE(); } } - + // reset m_bvar2sign for (unsigned i = 0; i < l.m_num_lits; i++) { literal lit = l.m_lits[i]; bvar p = lit2bvar(lit); m_bvar2sign[p] = 0; } - + if (tautology) { TRACE("qe_lite", tout << "resolution " << x << " tautology: \n"; display(tout, l); @@ -2080,7 +2083,7 @@ namespace fm { } expr_dependency * new_dep = m.mk_join(l.m_dep, u.m_dep); - + if (new_lits.empty() && new_xs.empty() && (new_c.is_neg() || (new_strict && new_c.is_zero()))) { TRACE("qe_lite", tout << "resolution " << x << " inconsistent: \n"; display(tout, l); @@ -2090,7 +2093,7 @@ namespace fm { m_inconsistent_core = new_dep; return 0; } - + constraint * new_cnstr = mk_constraint(new_lits.size(), new_lits.c_ptr(), new_xs.size(), @@ -2105,45 +2108,45 @@ namespace fm { tout << "\n"; display(tout, u); tout << "\n---->\n"; - display(tout, *new_cnstr); + display(tout, *new_cnstr); tout << "\n"; tout << "new_dep: " << new_dep << "\n";); - + return new_cnstr; } - + ptr_vector new_constraints; - + bool try_eliminate(var x) { constraints & l = m_lowers[x]; constraints & u = m_uppers[x]; cleanup_constraints(l); cleanup_constraints(u); - + if (l.empty() || u.empty()) { // easy case mark_constraints_dead(x); TRACE("qe_lite", tout << "variable was eliminated (trivial case)\n";); return true; } - + unsigned num_lowers = l.size(); unsigned num_uppers = u.size(); - + if (num_lowers > m_fm_cutoff1 && num_uppers > m_fm_cutoff1) return false; - + if (num_lowers * num_uppers > m_fm_cutoff2) return false; - + if (!can_eliminate(x)) return false; - + m_counter += num_lowers * num_uppers; - + TRACE("qe_lite", tout << "eliminating " << mk_ismt2_pp(m_var2expr.get(x), m) << "\nlowers:\n"; display_constraints(tout, l); tout << "uppers:\n"; display_constraints(tout, u);); - + unsigned num_old_cnstrs = num_uppers + num_lowers; unsigned limit = num_old_cnstrs + m_fm_extra; unsigned num_new_cnstrs = 0; @@ -2164,13 +2167,13 @@ namespace fm { } } } - + mark_constraints_dead(x); - + unsigned sz = new_constraints.size(); - + m_counter += sz; - + for (unsigned i = 0; i < sz; i++) { constraint * c = new_constraints[i]; backward_subsumption(*c); @@ -2179,7 +2182,7 @@ namespace fm { TRACE("qe_lite", tout << "variables was eliminated old: " << num_old_cnstrs << " new_constraints: " << sz << "\n";); return true; } - + void copy_remaining(vector & v2cs) { vector::iterator it = v2cs.begin(); vector::iterator end = v2cs.end(); @@ -2199,13 +2202,13 @@ namespace fm { } v2cs.finalize(); } - + // Copy remaining clauses to m_new_fmls void copy_remaining() { copy_remaining(m_uppers); copy_remaining(m_lowers); } - + void checkpoint() { cooperate("fm"); if (m.canceled()) @@ -2224,12 +2227,12 @@ namespace fm { } else { TRACE("qe_lite", display(tout);); - + subsume(); var_vector candidates; - sort_candidates(candidates); - unsigned eliminated = 0; - + sort_candidates(candidates); + unsigned eliminated = 0; + unsigned num = candidates.size(); for (unsigned i = 0; i < num; i++) { checkpoint(); @@ -2251,8 +2254,8 @@ namespace fm { reset_constraints(); fmls.reset(); fmls.append(m_new_fmls); - } - + } + void display_constraints(std::ostream & out, constraints const & cs) const { constraints::const_iterator it = cs.begin(); constraints::const_iterator end = cs.end(); @@ -2262,7 +2265,7 @@ namespace fm { out << "\n"; } } - + void display(std::ostream & out) const { unsigned num = num_vars(); for (var x = 0; x < num; x++) { @@ -2284,10 +2287,10 @@ public: ast_manager& m; public: elim_cfg(impl& i): m_imp(i), m(i.m) {} - - bool reduce_quantifier(quantifier * q, - expr * new_body, - expr * const * new_patterns, + + bool reduce_quantifier(quantifier * q, + expr * new_body, + expr * const * new_patterns, expr * const * new_no_patterns, expr_ref & result, proof_ref & result_pr) { @@ -2299,13 +2302,13 @@ public: for (unsigned i = 0; i < q->get_num_decls(); ++i) { indices.insert(i); } - m_imp(indices, true, result); + m_imp(indices, true, result); if (is_forall(q)) { result = push_not(result); } result = m.update_quantifier( - q, - q->get_num_patterns(), new_patterns, + q, + q->get_num_patterns(), new_patterns, q->get_num_no_patterns(), new_no_patterns, result); m_imp.m_rewriter(result); return true; @@ -2315,7 +2318,7 @@ public: class elim_star : public rewriter_tpl { elim_cfg m_cfg; public: - elim_star(impl& i): + elim_star(impl& i): rewriter_tpl(i.m, false, m_cfg), m_cfg(i) {} @@ -2346,21 +2349,21 @@ private: } public: - impl(ast_manager& m): - m(m), - m_der(m), - m_fm(m), - m_array_der(m), - m_elim_star(*this), + impl(ast_manager & m, params_ref const & p): + m(m), + m_der(m, p), + m_fm(m), + m_array_der(m), + m_elim_star(*this), m_rewriter(m) {} - + void operator()(app_ref_vector& vars, expr_ref& fml) { if (vars.empty()) { return; } expr_ref tmp(fml); quantifier_ref q(m); - proof_ref pr(m); + proof_ref pr(m); symbol qe_lite("QE"); expr_abstract(m, 0, vars.size(), (expr*const*)vars.c_ptr(), fml, tmp); ptr_vector sorts; @@ -2386,12 +2389,12 @@ public: ++j; } } - vars.resize(j); - } + vars.resize(j); + } else { fml = tmp; } - } + } void operator()(expr_ref& fml, proof_ref& pr) { expr_ref tmp(m); @@ -2438,8 +2441,8 @@ public: }; -qe_lite::qe_lite(ast_manager& m) { - m_impl = alloc(impl, m); +qe_lite::qe_lite(ast_manager & m, params_ref const & p) { + m_impl = alloc(impl, m, p); } qe_lite::~qe_lite() { @@ -2464,14 +2467,14 @@ void qe_lite::operator()(uint_set const& index_set, bool index_of_bound, expr_re } class qe_lite_tactic : public tactic { - + struct imp { ast_manager& m; qe_lite m_qe; - imp(ast_manager& m, params_ref const& p): + imp(ast_manager& m, params_ref const & p): m(m), - m_qe(m) + m_qe(m, p) {} void checkpoint() { @@ -2479,7 +2482,7 @@ class qe_lite_tactic : public tactic { throw tactic_exception(m.limit().get_cancel_msg()); cooperate("qe-lite"); } - + void debug_diff(expr* a, expr* b) { ptr_vector as, bs; as.push_back(a); @@ -2515,9 +2518,9 @@ class qe_lite_tactic : public tactic { } } - void operator()(goal_ref const & g, - goal_ref_buffer & result, - model_converter_ref & mc, + void operator()(goal_ref const & g, + goal_ref_buffer & result, + model_converter_ref & mc, proof_converter_ref & pc, expr_dependency_ref & core) { SASSERT(g->is_well_sorted()); @@ -2540,7 +2543,7 @@ class qe_lite_tactic : public tactic { if (produce_proofs) { expr* fact = m.get_fact(new_pr); if (to_app(fact)->get_arg(0) != to_app(fact)->get_arg(1)) { - new_pr = m.mk_modus_ponens(g->pr(i), new_pr); + new_pr = m.mk_modus_ponens(g->pr(i), new_pr); } else { new_pr = g->pr(i); @@ -2548,7 +2551,7 @@ class qe_lite_tactic : public tactic { } if (f != new_f) { TRACE("qe", tout << mk_pp(f, m) << "\n" << new_f << "\n";); - g->update(i, new_f, new_pr, g->dep(i)); + g->update(i, new_f, new_pr, g->dep(i)); } } g->inc_depth(); @@ -2558,7 +2561,7 @@ class qe_lite_tactic : public tactic { } }; - + params_ref m_params; imp * m_imp; @@ -2567,7 +2570,7 @@ public: m_params(p) { m_imp = alloc(imp, m, p); } - + virtual ~qe_lite_tactic() { dealloc(m_imp); } @@ -2581,20 +2584,20 @@ public: // m_imp->updt_params(p); } - + virtual void collect_param_descrs(param_descrs & r) { // m_imp->collect_param_descrs(r); } - - virtual void operator()(goal_ref const & in, - goal_ref_buffer & result, - model_converter_ref & mc, + + virtual void operator()(goal_ref const & in, + goal_ref_buffer & result, + model_converter_ref & mc, proof_converter_ref & pc, expr_dependency_ref & core) { (*m_imp)(in, result, mc, pc, core); } - + virtual void collect_statistics(statistics & st) const { // m_imp->collect_statistics(st); } @@ -2603,13 +2606,13 @@ public: // m_imp->reset_statistics(); } - + virtual void cleanup() { ast_manager & m = m_imp->m; dealloc(m_imp); m_imp = alloc(imp, m, m_params); } - + }; tactic * mk_qe_lite_tactic(ast_manager & m, params_ref const & p) { diff --git a/src/qe/qe_lite.h b/src/qe/qe_lite.h index 48874f5cf..cff547f36 100644 --- a/src/qe/qe_lite.h +++ b/src/qe/qe_lite.h @@ -7,7 +7,7 @@ Module Name: Abstract: - Light weight partial quantifier-elimination procedures + Light weight partial quantifier-elimination procedures Author: @@ -31,14 +31,14 @@ class qe_lite { class impl; impl * m_impl; public: - qe_lite(ast_manager& m); + qe_lite(ast_manager & m, params_ref const & p); ~qe_lite(); /** \brief - Apply light-weight quantifier elimination - on constants provided as vector of variables. + Apply light-weight quantifier elimination + on constants provided as vector of variables. Return the updated formula and updated set of variables that were not eliminated. */ @@ -66,4 +66,4 @@ tactic * mk_qe_lite_tactic(ast_manager & m, params_ref const & p = params_ref()) ADD_TACTIC("qe-light", "apply light-weight quantifier elimination.", "mk_qe_lite_tactic(m, p)") */ -#endif +#endif diff --git a/src/tactic/bv/elim_small_bv_tactic.cpp b/src/tactic/bv/elim_small_bv_tactic.cpp index e395433f5..8cfc27950 100644 --- a/src/tactic/bv/elim_small_bv_tactic.cpp +++ b/src/tactic/bv/elim_small_bv_tactic.cpp @@ -34,6 +34,7 @@ class elim_small_bv_tactic : public tactic { struct rw_cfg : public default_rewriter_cfg { ast_manager & m; + params_ref m_params; bv_util m_util; simplifier m_simp; ref m_mc; @@ -47,6 +48,7 @@ class elim_small_bv_tactic : public tactic { rw_cfg(ast_manager & _m, params_ref const & p) : m(_m), + m_params(p), m_util(_m), m_simp(_m), m_bindings(_m), @@ -119,7 +121,7 @@ class elim_small_bv_tactic : public tactic { return res; } - br_status reduce_app(func_decl * f, unsigned num, expr * const * args, expr_ref & result, proof_ref & result_pr) { + br_status reduce_app(func_decl * f, unsigned num, expr * const * args, expr_ref & result, proof_ref & result_pr) { TRACE("elim_small_bv_app", expr_ref tmp(m.mk_app(f, num, args), m); tout << "reduce " << tmp << std::endl; ); return BR_FAILED; } @@ -178,7 +180,7 @@ class elim_small_bv_tactic : public tactic { quantifier_ref new_q(m); new_q = m.update_quantifier(q, body); - unused_vars_eliminator el(m); + unused_vars_eliminator el(m, m_params); el(new_q, result); TRACE("elim_small_bv", tout << "elimination result: " << mk_ismt2_pp(result, m) << std::endl; ); @@ -203,6 +205,7 @@ class elim_small_bv_tactic : public tactic { } void updt_params(params_ref const & p) { + m_params = p; m_max_memory = megabytes_to_bytes(p.get_uint("max_memory", UINT_MAX)); m_max_steps = p.get_uint("max_steps", UINT_MAX); m_max_bits = p.get_uint("max_bits", 4); @@ -305,7 +308,7 @@ public: virtual void cleanup() { ast_manager & m = m_imp->m; imp * d = alloc(imp, m, m_params); - std::swap(d, m_imp); + std::swap(d, m_imp); dealloc(d); } diff --git a/src/tactic/core/distribute_forall_tactic.cpp b/src/tactic/core/distribute_forall_tactic.cpp index 769f415f2..5d525a836 100644 --- a/src/tactic/core/distribute_forall_tactic.cpp +++ b/src/tactic/core/distribute_forall_tactic.cpp @@ -24,9 +24,9 @@ class distribute_forall_tactic : public tactic { ast_manager & m; rw_cfg(ast_manager & _m):m(_m) {} - bool reduce_quantifier(quantifier * old_q, - expr * new_body, - expr * const * new_patterns, + bool reduce_quantifier(quantifier * old_q, + expr * new_body, + expr * const * new_patterns, expr * const * new_no_patterns, expr_ref & result, proof_ref & result_pr) { @@ -34,7 +34,7 @@ class distribute_forall_tactic : public tactic { if (!old_q->is_forall()) { return false; } - + if (m.is_not(new_body) && m.is_or(to_app(new_body)->get_arg(0))) { // (forall X (not (or F1 ... Fn))) // --> @@ -50,13 +50,13 @@ class distribute_forall_tactic : public tactic { quantifier_ref tmp_q(m); tmp_q = m.update_quantifier(old_q, not_arg); expr_ref new_q(m); - elim_unused_vars(m, tmp_q, new_q); + elim_unused_vars(m, tmp_q, params_ref(), new_q); new_args.push_back(new_q); } result = m.mk_and(new_args.size(), new_args.c_ptr()); return true; } - + if (m.is_and(new_body)) { // (forall X (and F1 ... Fn)) // --> @@ -70,20 +70,20 @@ class distribute_forall_tactic : public tactic { quantifier_ref tmp_q(m); tmp_q = m.update_quantifier(old_q, arg); expr_ref new_q(m); - elim_unused_vars(m, tmp_q, new_q); + elim_unused_vars(m, tmp_q, params_ref(), new_q); new_args.push_back(new_q); } result = m.mk_and(new_args.size(), new_args.c_ptr()); return true; } - + return false; } }; struct rw : public rewriter_tpl { rw_cfg m_cfg; - + rw(ast_manager & m, bool proofs_enabled): rewriter_tpl(m, proofs_enabled, m_cfg), m_cfg(m) { @@ -99,19 +99,19 @@ public: return alloc(distribute_forall_tactic); } - virtual void operator()(goal_ref const & g, - goal_ref_buffer & result, - model_converter_ref & mc, + virtual void operator()(goal_ref const & g, + goal_ref_buffer & result, + model_converter_ref & mc, proof_converter_ref & pc, expr_dependency_ref & core) { SASSERT(g->is_well_sorted()); ast_manager & m = g->m(); bool produce_proofs = g->proofs_enabled(); rw r(m, produce_proofs); - m_rw = &r; + m_rw = &r; mc = 0; pc = 0; core = 0; result.reset(); tactic_report report("distribute-forall", *g); - + expr_ref new_curr(m); proof_ref new_pr(m); unsigned size = g->size(); @@ -126,12 +126,12 @@ public: } g->update(idx, new_curr, new_pr, g->dep(idx)); } - + g->inc_depth(); result.push_back(g.get()); TRACE("distribute-forall", g->display(tout);); SASSERT(g->is_well_sorted()); - m_rw = 0; + m_rw = 0; } virtual void cleanup() {} diff --git a/src/tactic/ufbv/ufbv_rewriter.cpp b/src/tactic/ufbv/ufbv_rewriter.cpp index 40fdf5e3e..f5f18d234 100644 --- a/src/tactic/ufbv/ufbv_rewriter.cpp +++ b/src/tactic/ufbv/ufbv_rewriter.cpp @@ -49,7 +49,7 @@ ufbv_rewriter::~ufbv_rewriter() { bool ufbv_rewriter::is_demodulator(expr * e, expr_ref & large, expr_ref & small) const { if (e->get_kind() == AST_QUANTIFIER) { quantifier * q = to_quantifier(e); - if (q->is_forall()) { + if (q->is_forall()) { expr * qe = q->get_expr(); if ((m_manager.is_eq(qe) || m_manager.is_iff(qe))) { app * eq = to_app(q->get_expr()); @@ -61,7 +61,7 @@ bool ufbv_rewriter::is_demodulator(expr * e, expr_ref & large, expr_ref & small) << mk_pp(lhs, m_manager) << "\n" << mk_pp(rhs, m_manager) << "\n" << "subset: " << subset << ", smaller: " << smaller << "\n";); - // We only track uninterpreted functions, everything else is likely too expensive. + // We only track uninterpreted functions, everything else is likely too expensive. if ((subset == +1 || subset == +2) && smaller == +1) { if (is_uninterp(rhs)) { large = rhs; @@ -78,7 +78,7 @@ bool ufbv_rewriter::is_demodulator(expr * e, expr_ref & large, expr_ref & small) } #endif } - + if ((subset == -1 || subset == +2) && smaller == -1) { if (is_uninterp(lhs)) { large = lhs; @@ -113,13 +113,13 @@ bool ufbv_rewriter::is_demodulator(expr * e, expr_ref & large, expr_ref & small) return false; } -class var_set_proc { +class var_set_proc { uint_set & m_set; public: var_set_proc(uint_set &s):m_set(s) {} void operator()(var * n) { m_set.insert(n->get_idx()); } void operator()(quantifier * n) {} - void operator()(app * n) {} + void operator()(app * n) {} }; int ufbv_rewriter::is_subset(expr * e1, expr * e2) const { @@ -132,10 +132,10 @@ int ufbv_rewriter::is_subset(expr * e1, expr * e2) const { for_each_expr(proc1, e1); var_set_proc proc2(ev2); for_each_expr(proc2, e2); - - return (ev1==ev2 ) ? +2 : // We return +2 if the sets are equal. - (ev1.subset_of(ev2)) ? +1 : - (ev2.subset_of(ev1)) ? -1 : + + return (ev1==ev2 ) ? +2 : // We return +2 if the sets are equal. + (ev1.subset_of(ev2)) ? +1 : + (ev2.subset_of(ev1)) ? -1 : 0 ; } @@ -154,8 +154,8 @@ int ufbv_rewriter::is_smaller(expr * e1, expr * e2) const { else if (is_uninterp(e1) && !is_uninterp(e2)) return -1; - // two uninterpreted functions are ordered first by the number of - // arguments, then by their id. + // two uninterpreted functions are ordered first by the number of + // arguments, then by their id. if (is_uninterp(e1) && is_uninterp(e2)) { if (to_app(e1)->get_num_args() < to_app(e2)->get_num_args()) return +1; @@ -163,10 +163,10 @@ int ufbv_rewriter::is_smaller(expr * e1, expr * e2) const { return -1; else { unsigned a = to_app(e1)->get_decl()->get_id(); - unsigned b = to_app(e2)->get_decl()->get_id(); - if (a < b) + unsigned b = to_app(e2)->get_decl()->get_id(); + if (a < b) return +1; - else if (a > b) + else if (a > b) return -1; } } @@ -185,8 +185,8 @@ int ufbv_rewriter::is_smaller(expr * e1, expr * e2) const { default: UNREACHABLE(); } - return (sz1 == sz2) ? 0 : - (sz1 < sz2) ? +1 : + return (sz1 == sz2) ? 0 : + (sz1 < sz2) ? +1 : -1 ; } @@ -194,9 +194,9 @@ class max_var_id_proc { unsigned m_max_var_id; public: max_var_id_proc(void):m_max_var_id(0) {} - void operator()(var * n) { - if(n->get_idx() > m_max_var_id) - m_max_var_id = n->get_idx(); + void operator()(var * n) { + if(n->get_idx() > m_max_var_id) + m_max_var_id = n->get_idx(); } void operator()(quantifier * n) {} void operator()(app * n) {} @@ -206,7 +206,7 @@ public: unsigned ufbv_rewriter::max_var_id(expr * e) { max_var_id_proc proc; - for_each_expr(proc, e); + for_each_expr(proc, e); return proc.get_max(); } @@ -219,14 +219,14 @@ void ufbv_rewriter::insert_fwd_idx(expr * large, expr * small, quantifier * demo func_decl * fd = to_app(large)->get_decl(); fwd_idx_map::iterator it = m_fwd_idx.find_iterator(fd); - if (it == m_fwd_idx.end()) { - quantifier_set * qs = alloc(quantifier_set, 1); + if (it == m_fwd_idx.end()) { + quantifier_set * qs = alloc(quantifier_set, 1); m_fwd_idx.insert(fd, qs); it = m_fwd_idx.find_iterator(fd); } SASSERT(it->m_value); - it->m_value->insert(demodulator); + it->m_value->insert(demodulator); m_manager.inc_ref(demodulator); m_manager.inc_ref(large); @@ -238,13 +238,13 @@ void ufbv_rewriter::remove_fwd_idx(func_decl * f, quantifier * demodulator) { TRACE("demodulator_fwd", tout << "REMOVE: " << std::hex << (size_t)demodulator << std::endl; ); fwd_idx_map::iterator it = m_fwd_idx.find_iterator(f); - if (it != m_fwd_idx.end()) { + if (it != m_fwd_idx.end()) { demodulator2lhs_rhs::iterator fit = m_demodulator2lhs_rhs.find_iterator(demodulator); m_manager.dec_ref(fit->m_value.first); m_manager.dec_ref(fit->m_value.second); m_manager.dec_ref(demodulator); m_demodulator2lhs_rhs.erase(demodulator); - it->m_value->erase(demodulator); + it->m_value->erase(demodulator); } else { SASSERT(m_demodulator2lhs_rhs.contains(demodulator)); } @@ -281,13 +281,13 @@ void ufbv_rewriter::show_fwd_idx(std::ostream & out) { } } -bool ufbv_rewriter::rewrite1(func_decl * f, ptr_vector & m_new_args, expr_ref & np) { +bool ufbv_rewriter::rewrite1(func_decl * f, ptr_vector & m_new_args, expr_ref & np) { fwd_idx_map::iterator it = m_fwd_idx.find_iterator(f); if (it != m_fwd_idx.end()) { TRACE("demodulator_bug", tout << "trying to rewrite: " << f->get_name() << " args:\n"; for (unsigned i = 0; i < m_new_args.size(); i++) { tout << mk_pp(m_new_args[i], m_manager) << "\n"; }); quantifier_set::iterator dit = it->m_value->begin(); - quantifier_set::iterator dend = it->m_value->end(); + quantifier_set::iterator dend = it->m_value->end(); for ( ; dit != dend ; dit++ ) { quantifier * d = *dit; @@ -302,7 +302,7 @@ bool ufbv_rewriter::rewrite1(func_decl * f, ptr_vector & m_new_args, expr_ TRACE("demodulator_bug", tout << "Matching with demodulator: " << mk_pp(d, m_manager) << std::endl; ); SASSERT(large->get_decl() == f); - + if (m_match_subst(large, l_s.second, m_new_args.c_ptr(), np)) { TRACE("demodulator_bug", tout << "succeeded...\n" << mk_pp(l_s.second, m_manager) << "\n===>\n" << mk_pp(np, m_manager) << "\n";); return true; @@ -331,22 +331,22 @@ void ufbv_rewriter::rewrite_cache(expr * e, expr * new_e, bool done) { } expr * ufbv_rewriter::rewrite(expr * n) { - if (m_fwd_idx.empty()) + if (m_fwd_idx.empty()) return n; TRACE("demodulator", tout << "rewrite: " << mk_pp(n, m_manager) << std::endl; ); app * a; - + SASSERT(m_rewrite_todo.empty()); m_rewrite_cache.reset(); - + m_rewrite_todo.push_back(n); while (!m_rewrite_todo.empty()) { TRACE("demodulator_stack", tout << "STACK: " << std::endl; - for ( unsigned i = 0; iget_decl(); m_new_args.reset(); @@ -389,12 +389,12 @@ expr * ufbv_rewriter::rewrite(expr * n) { // No pop. } else { if(all_untouched) { - rewrite_cache(e, actual, true); - } + rewrite_cache(e, actual, true); + } else { expr_ref na(m_manager); if (f->get_family_id() != m_manager.get_basic_family_id()) - na = m_manager.mk_app(f, m_new_args.size(), m_new_args.c_ptr()); + na = m_manager.mk_app(f, m_new_args.size(), m_new_args.c_ptr()); else m_bsimp.reduce(f, m_new_args.size(), m_new_args.c_ptr(), na); TRACE("demodulator_bug", tout << "e:\n" << mk_pp(e, m_manager) << "\nnew_args: \n"; @@ -405,9 +405,9 @@ expr * ufbv_rewriter::rewrite(expr * n) { } m_rewrite_todo.pop_back(); } - } + } break; - case AST_QUANTIFIER: { + case AST_QUANTIFIER: { expr * body = to_quantifier(actual)->get_expr(); if (m_rewrite_cache.contains(body)) { const expr_bool_pair ebp = m_rewrite_cache.get(body); @@ -417,13 +417,13 @@ expr * ufbv_rewriter::rewrite(expr * n) { q = m_manager.update_quantifier(to_quantifier(actual), new_body); m_new_exprs.push_back(q); expr_ref new_q(m_manager); - elim_unused_vars(m_manager, q, new_q); + elim_unused_vars(m_manager, q, params_ref(), new_q); m_new_exprs.push_back(new_q); - rewrite_cache(e, new_q, true); + rewrite_cache(e, new_q, true); m_rewrite_todo.pop_back(); } else { m_rewrite_todo.push_back(body); - } + } break; } default: @@ -437,7 +437,7 @@ expr * ufbv_rewriter::rewrite(expr * n) { expr * r = ebp.first; TRACE("demodulator", tout << "rewrite result: " << mk_pp(r, m_manager) << std::endl; ); - + return r; } @@ -448,7 +448,7 @@ public: add_back_idx_proc(back_idx_map & bi, expr * e):m_back_idx(bi),m_expr(e) {} void operator()(var * n) {} void operator()(quantifier * n) {} - void operator()(app * n) { + void operator()(app * n) { // We track only uninterpreted and constant functions. if (n->get_num_args()==0) return; SASSERT(m_expr && m_expr != (expr*) 0x00000003); @@ -464,7 +464,7 @@ public: m_back_idx.insert(d, e); } } - } + } }; class ufbv_rewriter::remove_back_idx_proc { @@ -473,15 +473,15 @@ class ufbv_rewriter::remove_back_idx_proc { public: remove_back_idx_proc(back_idx_map & bi, expr * e):m_back_idx(bi),m_expr(e) {} void operator()(var * n) {} - void operator()(quantifier * n) {} - void operator()(app * n) { + void operator()(quantifier * n) {} + void operator()(app * n) { // We track only uninterpreted and constant functions. if (n->get_num_args()==0) return; func_decl * d=n->get_decl(); - if (d->get_family_id() == null_family_id) { + if (d->get_family_id() == null_family_id) { back_idx_map::iterator it = m_back_idx.find_iterator(d); if (it != m_back_idx.end()) { - SASSERT(it->m_value); + SASSERT(it->m_value); it->m_value->remove(m_expr); } } @@ -489,12 +489,12 @@ public: }; void ufbv_rewriter::reschedule_processed(func_decl * f) { - //use m_back_idx to find all formulas p in m_processed that contains f { + //use m_back_idx to find all formulas p in m_processed that contains f { back_idx_map::iterator it = m_back_idx.find_iterator(f); if (it != m_back_idx.end()) { SASSERT(it->m_value); expr_set temp; - + expr_set::iterator sit = it->m_value->begin(); expr_set::iterator send = it->m_value->end(); for ( ; sit != send ; sit++ ) { @@ -502,7 +502,7 @@ void ufbv_rewriter::reschedule_processed(func_decl * f) { if (m_processed.contains(p)) temp.insert(p); } - + sit = temp.begin(); send = temp.end(); for ( ; sit != send; sit++) { @@ -511,7 +511,7 @@ void ufbv_rewriter::reschedule_processed(func_decl * f) { m_processed.remove(p); remove_back_idx_proc proc(m_back_idx, p); // this could change it->m_value, thus we need the `temp' set. for_each_expr(proc, p); - // insert p into m_todo + // insert p into m_todo m_todo.push_back(p); } } @@ -529,40 +529,40 @@ bool ufbv_rewriter::can_rewrite(expr * n, expr * lhs) { while (!stack.empty()) { curr = stack.back(); - + if (visited.is_marked(curr)) { stack.pop_back(); continue; } switch(curr->get_kind()) { - case AST_VAR: + case AST_VAR: visited.mark(curr, true); stack.pop_back(); break; case AST_APP: - if (for_each_expr_args(stack, visited, to_app(curr)->get_num_args(), to_app(curr)->get_args())) { + if (for_each_expr_args(stack, visited, to_app(curr)->get_num_args(), to_app(curr)->get_args())) { if (m_match_subst(lhs, curr)) return true; visited.mark(curr, true); stack.pop_back(); } break; - + case AST_QUANTIFIER: - if (!for_each_expr_args(stack, visited, to_quantifier(curr)->get_num_patterns(), + if (!for_each_expr_args(stack, visited, to_quantifier(curr)->get_num_patterns(), to_quantifier(curr)->get_patterns())) { break; } - if (!for_each_expr_args(stack, visited, to_quantifier(curr)->get_num_no_patterns(), + if (!for_each_expr_args(stack, visited, to_quantifier(curr)->get_num_no_patterns(), to_quantifier(curr)->get_no_patterns())) { break; } if (!visited.is_marked(to_quantifier(curr)->get_expr())) { stack.push_back(to_quantifier(curr)->get_expr()); break; - } + } stack.pop_back(); break; @@ -597,7 +597,7 @@ void ufbv_rewriter::reschedule_demodulators(func_decl * f, expr * lhs) { expr * occ = *esit; if (!is_quantifier(occ)) - continue; + continue; // Use the fwd idx to find out whether this is a demodulator. demodulator2lhs_rhs::iterator d2lr_it = m_demodulator2lhs_rhs.find_iterator(to_quantifier(occ)); @@ -605,22 +605,22 @@ void ufbv_rewriter::reschedule_demodulators(func_decl * f, expr * lhs) { l = d2lr_it->m_value.first; quantifier_ref d(m_manager); func_decl_ref df(m_manager); - d = to_quantifier(occ); + d = to_quantifier(occ); df = to_app(l)->get_decl(); // Now we know there is an occurrence of f in d - // if n' can rewrite d { + // if n' can rewrite d { if (can_rewrite(d, lhs)) { TRACE("demodulator", tout << "Rescheduling: " << std::endl << mk_pp(d, m_manager) << std::endl; ); // remove d from m_fwd_idx remove_fwd_idx(df, d); - // remove d from m_back_idx + // remove d from m_back_idx // just remember it here, because otherwise it and/or esit might become invalid? - // to_remove.insert(d); + // to_remove.insert(d); remove_back_idx_proc proc(m_back_idx, d); for_each_expr(proc, d); // insert d into m_todo - m_todo.push_back(d); + m_todo.push_back(d); } } } @@ -629,10 +629,10 @@ void ufbv_rewriter::reschedule_demodulators(func_decl * f, expr * lhs) { //for (ptr_vector::iterator it = to_remove.begin(); it != to_remove.end(); it++) { // expr * d = *it; // remove_back_idx_proc proc(m_manager, m_back_idx, d); - // for_each_expr(proc, d); + // for_each_expr(proc, d); //} } - + void ufbv_rewriter::operator()(unsigned n, expr * const * exprs, proof * const * prs, expr_ref_vector & new_exprs, proof_ref_vector & new_prs) { if (m_manager.proofs_enabled()) { // Let us not waste time with proof production @@ -655,7 +655,7 @@ void ufbv_rewriter::operator()(unsigned n, expr * const * exprs, proof * const * m_match_subst.reserve(max_vid); - while (!m_todo.empty()) { + while (!m_todo.empty()) { // let n be the next formula in m_todo. expr_ref cur(m_manager); cur = m_todo.back(); @@ -670,21 +670,21 @@ void ufbv_rewriter::operator()(unsigned n, expr * const * exprs, proof * const * expr_ref large(m_manager), small(m_manager); if (!is_demodulator(np, large, small)) { // insert n' into m_processed - m_processed.insert(np); - // update m_back_idx (traverse n' and for each uninterpreted function declaration f in n' add the entry f->n' to m_back_idx) + m_processed.insert(np); + // update m_back_idx (traverse n' and for each uninterpreted function declaration f in n' add the entry f->n' to m_back_idx) add_back_idx_proc proc(m_back_idx, np); for_each_expr(proc, np); - } else { + } else { // np is a demodulator that allows us to replace 'large' with 'small'. TRACE("demodulator", tout << "Found demodulator: " << std::endl; - tout << mk_pp(large.get(), m_manager) << std::endl << " ---> " << + tout << mk_pp(large.get(), m_manager) << std::endl << " ---> " << std::endl << mk_pp(small.get(), m_manager) << std::endl; ); TRACE("demodulator_s", tout << "Found demodulator: " << std::endl; - tout << to_app(large)->get_decl()->get_name() << + tout << to_app(large)->get_decl()->get_name() << "[" << to_app(large)->get_depth() << "]" << " ---> "; if (is_app(small)) - tout << to_app(small)->get_decl()->get_name() << + tout << to_app(small)->get_decl()->get_name() << "[" << to_app(small)->get_depth() << "]" << std::endl; else tout << mk_pp(small.get(), m_manager) << std::endl; ); @@ -695,14 +695,14 @@ void ufbv_rewriter::operator()(unsigned n, expr * const * exprs, proof * const * reschedule_processed(f); reschedule_demodulators(f, large); - + // insert n' into m_fwd_idx insert_fwd_idx(large, small, to_quantifier(np)); // update m_back_idx add_back_idx_proc proc(m_back_idx, np); for_each_expr(proc, np); - } + } } // the result is the contents of m_processed + all demodulators in m_fwd_idx. @@ -743,10 +743,10 @@ ufbv_rewriter::match_subst::match_subst(ast_manager & m): */ struct match_args_aux_proc { substitution & m_subst; - struct no_match {}; - + struct no_match {}; + match_args_aux_proc(substitution & s):m_subst(s) {} - + void operator()(var * n) { expr_offset r; if (m_subst.find(n, 0, r)) { @@ -766,7 +766,7 @@ struct match_args_aux_proc { bool ufbv_rewriter::match_subst::match_args(app * lhs, expr * const * args) { m_cache.reset(); m_todo.reset(); - + // fill todo-list, and perform quick success/failure tests m_all_args_eq = true; unsigned num_args = lhs->get_num_args(); @@ -777,21 +777,21 @@ bool ufbv_rewriter::match_subst::match_args(app * lhs, expr * const * args) { m_all_args_eq = false; if (is_app(t_arg) && is_app(i_arg) && to_app(t_arg)->get_decl() != to_app(i_arg)->get_decl()) { // quick failure... - return false; + return false; } m_todo.push_back(expr_pair(t_arg, i_arg)); } - - if (m_all_args_eq) { + + if (m_all_args_eq) { // quick success worked... return true; } m_subst.reset(); - + while (!m_todo.empty()) { expr_pair const & p = m_todo.back(); - + if (is_var(p.first)) { expr_offset r; if (m_subst.find(to_var(p.first), 0, r)) { @@ -814,7 +814,7 @@ bool ufbv_rewriter::match_subst::match_args(app * lhs, expr * const * args) { SASSERT(is_app(p.first) && is_app(p.second)); - if (to_app(p.first)->is_ground() && !to_app(p.second)->is_ground()) + if (to_app(p.first)->is_ground() && !to_app(p.second)->is_ground()) return false; if (p.first == p.second && to_app(p.first)->is_ground()) { @@ -827,7 +827,7 @@ bool ufbv_rewriter::match_subst::match_args(app * lhs, expr * const * args) { m_todo.pop_back(); continue; } - + if (p.first == p.second) { // p.first and p.second is not ground... @@ -855,10 +855,10 @@ bool ufbv_rewriter::match_subst::match_args(app * lhs, expr * const * args) { app * n1 = to_app(p.first); app * n2 = to_app(p.second); - + if (n1->get_decl() != n2->get_decl()) return false; - + unsigned num_args1 = n1->get_num_args(); if (num_args1 != n2->get_num_args()) return false; @@ -867,7 +867,7 @@ bool ufbv_rewriter::match_subst::match_args(app * lhs, expr * const * args) { if (num_args1 == 0) continue; - + m_cache.insert(p); unsigned j = num_args1; while (j > 0) { @@ -886,7 +886,7 @@ bool ufbv_rewriter::match_subst::operator()(app * lhs, expr * rhs, expr * const new_rhs = rhs; return true; } - unsigned deltas[2] = { 0, 0 }; + unsigned deltas[2] = { 0, 0 }; m_subst.apply(2, deltas, expr_offset(rhs, 0), new_rhs); return true; } From ec29a03c8ff033fc8a38e98d439411012538b185 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Fri, 7 Apr 2017 21:22:38 -0700 Subject: [PATCH 424/562] add facility to dispense with cancellation (not activated at this point). Address #961 by expanding recurisve function definitions that are not tautologies if the current model does not validate Signed-off-by: Nikolaj Bjorner --- src/sat/sat_simplifier.cpp | 3 +- src/sat/sat_solver.cpp | 1 + src/sat/sat_solver.h | 12 +++++++ src/smt/smt_context_inv.cpp | 3 ++ src/smt/smt_model_checker.cpp | 62 ++++++++++++++++++++--------------- src/smt/smt_model_checker.h | 3 +- src/smt/smt_quantifier.cpp | 4 +++ src/smt/smt_quantifier.h | 2 ++ 8 files changed, 62 insertions(+), 28 deletions(-) diff --git a/src/sat/sat_simplifier.cpp b/src/sat/sat_simplifier.cpp index 007751220..923f5ae49 100644 --- a/src/sat/sat_simplifier.cpp +++ b/src/sat/sat_simplifier.cpp @@ -154,6 +154,8 @@ namespace sat { if (!m_subsumption && !m_elim_blocked_clauses && !m_resolution) return; + // solver::scoped_disable_checkpoint _scoped_disable_checkpoint(s); + initialize(); CASSERT("sat_solver", s.check_invariant()); @@ -167,7 +169,6 @@ namespace sat { CASSERT("sat_solver", s.check_invariant()); m_need_cleanup = false; m_use_list.init(s.num_vars()); - init_visited(); m_learned_in_use_lists = false; if (learned) { register_clauses(s.m_learned); diff --git a/src/sat/sat_solver.cpp b/src/sat/sat_solver.cpp index 9c858a29a..08c70fba5 100644 --- a/src/sat/sat_solver.cpp +++ b/src/sat/sat_solver.cpp @@ -33,6 +33,7 @@ namespace sat { solver::solver(params_ref const & p, reslimit& l, extension * ext): m_rlimit(l), + m_checkpoint_enabled(true), m_config(p), m_ext(ext), m_par(0), diff --git a/src/sat/sat_solver.h b/src/sat/sat_solver.h index 6c91565aa..42291609d 100644 --- a/src/sat/sat_solver.h +++ b/src/sat/sat_solver.h @@ -72,6 +72,7 @@ namespace sat { struct abort_solver {}; protected: reslimit& m_rlimit; + bool m_checkpoint_enabled; config m_config; stats m_stats; extension * m_ext; @@ -214,6 +215,16 @@ namespace sat { } } }; + class scoped_disable_checkpoint { + solver& s; + public: + scoped_disable_checkpoint(solver& s): s(s) { + s.m_checkpoint_enabled = false; + } + ~scoped_disable_checkpoint() { + s.m_checkpoint_enabled = true; + } + }; unsigned select_watch_lit(clause const & cls, unsigned starting_at) const; unsigned select_learned_watch_lit(clause const & cls) const; bool simplify_clause(unsigned & num_lits, literal * lits) const; @@ -257,6 +268,7 @@ namespace sat { lbool status(clause const & c) const; clause_offset get_offset(clause const & c) const { return m_cls_allocator.get_offset(&c); } void checkpoint() { + if (!m_checkpoint_enabled) return; if (!m_rlimit.inc()) { m_mc.reset(); m_model_is_current = false; diff --git a/src/smt/smt_context_inv.cpp b/src/smt/smt_context_inv.cpp index 7d009a037..f63e07b57 100644 --- a/src/smt/smt_context_inv.cpp +++ b/src/smt/smt_context_inv.cpp @@ -433,6 +433,9 @@ namespace smt { if (!is_ground(n)) { continue; } + if (is_quantifier(n) && m.is_rec_fun_def(to_quantifier(n))) { + continue; + } switch (get_assignment(*it)) { case l_undef: break; diff --git a/src/smt/smt_model_checker.cpp b/src/smt/smt_model_checker.cpp index dfdb035c5..279ea20cf 100644 --- a/src/smt/smt_model_checker.cpp +++ b/src/smt/smt_model_checker.cpp @@ -316,7 +316,7 @@ namespace smt { return false; } - bool model_checker::check_rec_fun(quantifier* q) { + bool model_checker::check_rec_fun(quantifier* q, bool strict_rec_fun) { TRACE("model_checker", tout << mk_pp(q, m) << "\n";); SASSERT(q->get_num_patterns() == 2); // first pattern is the function, second is the body. expr* fn = to_app(q->get_pattern(0))->get_arg(0); @@ -340,7 +340,7 @@ namespace smt { } sub(q->get_expr(), num_decls, args.c_ptr(), tmp); m_curr_model->eval(tmp, result, true); - if (m.is_false(result)) { + if (strict_rec_fun ? !m.is_true(result) : m.is_false(result)) { add_instance(q, args, 0); return false; } @@ -365,10 +365,10 @@ namespace smt { bool model_checker::check(proto_model * md, obj_map const & root2value) { SASSERT(md != 0); + m_root2value = &root2value; - ptr_vector::const_iterator it = m_qm->begin_quantifiers(); - ptr_vector::const_iterator end = m_qm->end_quantifiers(); - if (it == end) + + if (m_qm->num_quantifiers() == 0) return true; if (m_iteration_idx >= m_params.m_mbqi_max_iterations) { @@ -393,6 +393,36 @@ namespace smt { bool found_relevant = false; unsigned num_failures = 0; + check_quantifiers(false, found_relevant, num_failures); + + + if (found_relevant) + m_iteration_idx++; + + TRACE("model_checker", tout << "model after check:\n"; model_pp(tout, *md);); + TRACE("model_checker", tout << "model checker result: " << (num_failures == 0) << "\n";); + m_max_cexs += m_params.m_mbqi_max_cexs; + + if (num_failures == 0 && !m_context->validate_model()) { + num_failures = 1; + // this time force expanding recursive function definitions + // that are not forced true in the current model. + check_quantifiers(true, found_relevant, num_failures); + } + if (num_failures == 0) + m_curr_model->cleanup(); + if (m_params.m_mbqi_trace) { + if (num_failures == 0) + verbose_stream() << "(smt.mbqi :succeeded true)\n"; + else + verbose_stream() << "(smt.mbqi :num-failures " << num_failures << ")\n"; + } + return num_failures == 0; + } + + void model_checker::check_quantifiers(bool strict_rec_fun, bool& found_relevant, unsigned& num_failures) { + ptr_vector::const_iterator it = m_qm->begin_quantifiers(); + ptr_vector::const_iterator end = m_qm->end_quantifiers(); for (; it != end; ++it) { quantifier * q = *it; if(!m_qm->mbqi_enabled(q)) continue; @@ -406,7 +436,7 @@ namespace smt { } found_relevant = true; if (m.is_rec_fun_def(q)) { - if (!check_rec_fun(q)) { + if (!check_rec_fun(q, strict_rec_fun)) { TRACE("model_checker", tout << "checking recursive function failed\n";); num_failures++; } @@ -420,26 +450,6 @@ namespace smt { } } } - - if (found_relevant) - m_iteration_idx++; - - TRACE("model_checker", tout << "model after check:\n"; model_pp(tout, *md);); - TRACE("model_checker", tout << "model checker result: " << (num_failures == 0) << "\n";); - m_max_cexs += m_params.m_mbqi_max_cexs; - - if (num_failures == 0 && !m_context->validate_model()) { - num_failures = 1; - } - if (num_failures == 0) - m_curr_model->cleanup(); - if (m_params.m_mbqi_trace) { - if (num_failures == 0) - verbose_stream() << "(smt.mbqi :succeeded true)\n"; - else - verbose_stream() << "(smt.mbqi :num-failures " << num_failures << ")\n"; - } - return num_failures == 0; } void model_checker::init_search_eh() { diff --git a/src/smt/smt_model_checker.h b/src/smt/smt_model_checker.h index b94ddb6bb..1b7713d59 100644 --- a/src/smt/smt_model_checker.h +++ b/src/smt/smt_model_checker.h @@ -59,7 +59,8 @@ namespace smt { void assert_neg_q_m(quantifier * q, expr_ref_vector & sks); bool add_blocking_clause(model * cex, expr_ref_vector & sks); bool check(quantifier * q); - bool check_rec_fun(quantifier* q); + bool check_rec_fun(quantifier* q, bool strict_rec_fun); + void check_quantifiers(bool strict_rec_fun, bool& found_relevant, unsigned& num_failures); struct instance { quantifier * m_q; diff --git a/src/smt/smt_quantifier.cpp b/src/smt/smt_quantifier.cpp index bad788f5d..10e2df988 100644 --- a/src/smt/smt_quantifier.cpp +++ b/src/smt/smt_quantifier.cpp @@ -397,6 +397,10 @@ namespace smt { return m_imp->m_quantifiers.end(); } + unsigned quantifier_manager::num_quantifiers() const { + return m_imp->m_quantifiers.size(); + } + // The default plugin uses E-matching, MBQI and quick-checker class default_qm_plugin : public quantifier_manager_plugin { quantifier_manager * m_qm; diff --git a/src/smt/smt_quantifier.h b/src/smt/smt_quantifier.h index bc249ed1a..6dcf20583 100644 --- a/src/smt/smt_quantifier.h +++ b/src/smt/smt_quantifier.h @@ -91,6 +91,8 @@ namespace smt { ptr_vector::const_iterator begin_quantifiers() const; ptr_vector::const_iterator end_quantifiers() const; + unsigned num_quantifiers() const; + }; class quantifier_manager_plugin { From 95cf1447eace96a71c7582c9c39f4d3a82cdc118 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Mon, 10 Apr 2017 13:18:45 +0100 Subject: [PATCH 425/562] Added maintainers.txt for qprofdiff --- contrib/qprofdiff/maintainers.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 contrib/qprofdiff/maintainers.txt diff --git a/contrib/qprofdiff/maintainers.txt b/contrib/qprofdiff/maintainers.txt new file mode 100644 index 000000000..01167f6d3 --- /dev/null +++ b/contrib/qprofdiff/maintainers.txt @@ -0,0 +1,3 @@ +# Maintainers + +- Christoph M. Wintersteiger (@wintersteiger, cwinter@microsoft.com) From b67c1c550127b8bd05d490eec876083fded6afe3 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Mon, 10 Apr 2017 16:28:41 +0100 Subject: [PATCH 426/562] Fixed valgrind warning. Fixes #972 --- src/ast/rewriter/arith_rewriter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ast/rewriter/arith_rewriter.cpp b/src/ast/rewriter/arith_rewriter.cpp index 81385c2af..2b2087e3b 100644 --- a/src/ast/rewriter/arith_rewriter.cpp +++ b/src/ast/rewriter/arith_rewriter.cpp @@ -194,7 +194,7 @@ bool arith_rewriter::is_bound(expr * arg1, expr * arg2, op_kind kind, expr_ref & } } expr* t1, *t2; - bool is_int; + bool is_int = false; if (m_util.is_mod(arg2)) { std::swap(arg1, arg2); switch (kind) { From 67513a2cf574bbac681b6c7ae7f05677d390bc0e Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Tue, 11 Apr 2017 07:40:09 +0800 Subject: [PATCH 427/562] fix detection of bounds under conjunctions. Issue #971 Signed-off-by: Nikolaj Bjorner --- .../portfolio/bounded_int2bv_solver.cpp | 74 +++++++++++++------ src/tactic/portfolio/enum2bv_solver.cpp | 8 ++ src/tactic/portfolio/pb2bv_solver.cpp | 19 ++++- 3 files changed, 73 insertions(+), 28 deletions(-) diff --git a/src/tactic/portfolio/bounded_int2bv_solver.cpp b/src/tactic/portfolio/bounded_int2bv_solver.cpp index 53c20b253..83693abba 100644 --- a/src/tactic/portfolio/bounded_int2bv_solver.cpp +++ b/src/tactic/portfolio/bounded_int2bv_solver.cpp @@ -34,19 +34,19 @@ Notes: class bounded_int2bv_solver : public solver_na2as { ast_manager& m; params_ref m_params; - bv_util m_bv; - arith_util m_arith; - expr_ref_vector m_assertions; + mutable bv_util m_bv; + mutable arith_util m_arith; + mutable expr_ref_vector m_assertions; ref m_solver; - ptr_vector m_bounds; - func_decl_ref_vector m_bv_fns; - func_decl_ref_vector m_int_fns; + mutable ptr_vector m_bounds; + mutable func_decl_ref_vector m_bv_fns; + mutable func_decl_ref_vector m_int_fns; unsigned_vector m_bv_fns_lim; - obj_map m_int2bv; - obj_map m_bv2int; - obj_map m_bv2offset; - bv2int_rewriter_ctx m_rewriter_ctx; - bv2int_rewriter_star m_rewriter; + mutable obj_map m_int2bv; + mutable obj_map m_bv2int; + mutable obj_map m_bv2offset; + mutable bv2int_rewriter_ctx m_rewriter_ctx; + mutable bv2int_rewriter_star m_rewriter; public: @@ -78,7 +78,19 @@ public: } virtual void assert_expr(expr * t) { + unsigned i = m_assertions.size(); m_assertions.push_back(t); + while (i < m_assertions.size()) { + t = m_assertions[i].get(); + if (m.is_and(t)) { + m_assertions.append(to_app(t)->get_num_args(), to_app(t)->get_args()); + m_assertions[i] = m_assertions.back(); + m_assertions.pop_back(); + } + else { + ++i; + } + } } virtual void push_core() { @@ -184,7 +196,7 @@ private: } filter_model_converter filter(m); for (unsigned i = 0; i < m_bv_fns.size(); ++i) { - filter.insert(m_bv_fns[i]); + filter.insert(m_bv_fns[i].get()); } filter(mdl, 0); } @@ -205,13 +217,13 @@ private: ext(mdl, 0); } - void accumulate_sub(expr_safe_replace& sub) { + void accumulate_sub(expr_safe_replace& sub) const { for (unsigned i = 0; i < m_bounds.size(); ++i) { accumulate_sub(sub, *m_bounds[i]); } } - void accumulate_sub(expr_safe_replace& sub, bound_manager& bm) { + void accumulate_sub(expr_safe_replace& sub, bound_manager& bm) const { bound_manager::iterator it = bm.begin(), end = bm.end(); for (; it != end; ++it) { expr* e = *it; @@ -252,19 +264,20 @@ private: sub.insert(e, t); } else { - IF_VERBOSE(1, - verbose_stream() << "unprocessed entry: " << mk_pp(e, m) << "\n"; - if (bm.has_lower(e, lo, s1)) { - verbose_stream() << "lower: " << lo << " " << s1 << "\n"; - } - if (bm.has_upper(e, hi, s2)) { - verbose_stream() << "upper: " << hi << " " << s2 << "\n"; - }); + TRACE("pb", + tout << "unprocessed entry: " << mk_pp(e, m) << "\n"; + if (bm.has_lower(e, lo, s1)) { + tout << "lower: " << lo << " " << s1 << "\n"; + } + if (bm.has_upper(e, hi, s2)) { + tout << "upper: " << hi << " " << s2 << "\n"; + }); } } } - unsigned get_num_bits(rational const& k) { + + unsigned get_num_bits(rational const& k) const { SASSERT(!k.is_neg()); SASSERT(k.is_int()); rational two(2); @@ -277,11 +290,13 @@ private: return num_bits; } - void flush_assertions() { + void flush_assertions() const { + if (m_assertions.empty()) return; bound_manager& bm = *m_bounds.back(); for (unsigned i = 0; i < m_assertions.size(); ++i) { bm(m_assertions[i].get()); } + TRACE("int2bv", bm.display(tout);); expr_safe_replace sub(m); accumulate_sub(sub); proof_ref proof(m); @@ -304,6 +319,17 @@ private: m_assertions.reset(); m_rewriter.reset(); } + + virtual unsigned get_num_assertions() const { + flush_assertions(); + return m_solver->get_num_assertions(); + } + + virtual expr * get_assertion(unsigned idx) const { + flush_assertions(); + return m_solver->get_assertion(idx); + } + }; solver * mk_bounded_int2bv_solver(ast_manager & m, params_ref const & p, solver* s) { diff --git a/src/tactic/portfolio/enum2bv_solver.cpp b/src/tactic/portfolio/enum2bv_solver.cpp index f3288d8d6..9afd97de5 100644 --- a/src/tactic/portfolio/enum2bv_solver.cpp +++ b/src/tactic/portfolio/enum2bv_solver.cpp @@ -163,6 +163,14 @@ public: ext(mdl, 0); } + virtual unsigned get_num_assertions() const { + return m_solver->get_num_assertions(); + } + + virtual expr * get_assertion(unsigned idx) const { + return m_solver->get_assertion(idx); + } + }; solver * mk_enum2bv_solver(ast_manager & m, params_ref const & p, solver* s) { diff --git a/src/tactic/portfolio/pb2bv_solver.cpp b/src/tactic/portfolio/pb2bv_solver.cpp index bfd533e8a..090ea9f76 100644 --- a/src/tactic/portfolio/pb2bv_solver.cpp +++ b/src/tactic/portfolio/pb2bv_solver.cpp @@ -27,9 +27,9 @@ Notes: class pb2bv_solver : public solver_na2as { ast_manager& m; params_ref m_params; - expr_ref_vector m_assertions; - ref m_solver; - pb2bv_rewriter m_rewriter; + mutable expr_ref_vector m_assertions; + mutable ref m_solver; + mutable pb2bv_rewriter m_rewriter; public: @@ -107,8 +107,19 @@ public: filter(mdl, 0); } + virtual unsigned get_num_assertions() const { + flush_assertions(); + return m_solver->get_num_assertions(); + } + + virtual expr * get_assertion(unsigned idx) const { + flush_assertions(); + return m_solver->get_assertion(idx); + } + + private: - void flush_assertions() { + void flush_assertions() const { proof_ref proof(m); expr_ref fml(m); expr_ref_vector fmls(m); From 4140afa4cb276377f15aeb10dc945b5af450a3c7 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Tue, 11 Apr 2017 10:49:42 +0800 Subject: [PATCH 428/562] add regular expression membership for range of int.to.str functions. Issue #957 Signed-off-by: Nikolaj Bjorner --- src/ast/seq_decl_plugin.h | 1 + src/smt/theory_seq.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/ast/seq_decl_plugin.h b/src/ast/seq_decl_plugin.h index a7e534bbb..2882e905d 100644 --- a/src/ast/seq_decl_plugin.h +++ b/src/ast/seq_decl_plugin.h @@ -304,6 +304,7 @@ public: app* mk_to_re(expr* s) { return m.mk_app(m_fid, OP_SEQ_TO_RE, 1, &s); } app* mk_in_re(expr* s, expr* r) { return m.mk_app(m_fid, OP_SEQ_IN_RE, s, r); } + app* mk_range(expr* s1, expr* s2) { return m.mk_app(m_fid, OP_RE_RANGE, s1, s2); } app* mk_concat(expr* r1, expr* r2) { return m.mk_app(m_fid, OP_RE_CONCAT, r1, r2); } app* mk_union(expr* r1, expr* r2) { return m.mk_app(m_fid, OP_RE_UNION, r1, r2); } app* mk_inter(expr* r1, expr* r2) { return m.mk_app(m_fid, OP_RE_INTERSECT, r1, r2); } diff --git a/src/smt/theory_seq.cpp b/src/smt/theory_seq.cpp index 663d4cbe1..672d3c440 100644 --- a/src/smt/theory_seq.cpp +++ b/src/smt/theory_seq.cpp @@ -2345,6 +2345,17 @@ bool theory_seq::add_itos_axiom(expr* e) { return false; } add_axiom(mk_eq(e2, n, false)); + +#if 1 + expr_ref num_re(m), opt_re(m); + num_re = m_util.re.mk_range(m_util.str.mk_string(symbol("0")), m_util.str.mk_string(symbol("9"))); + num_re = m_util.re.mk_plus(num_re); + opt_re = m_util.re.mk_opt(m_util.re.mk_to_re(m_util.str.mk_string(symbol("-")))); + num_re = m_util.re.mk_concat(opt_re, num_re); + app_ref in_re(m_util.re.mk_in_re(e, num_re), m); + internalize_term(in_re); + propagate_in_re(in_re, true); +#endif m_trail_stack.push(push_replay(alloc(replay_axiom, m, e))); return true; } From 7207cabc9710d7b13b4b91ce8e2f4f4bbbcc6767 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 12 Apr 2017 17:09:35 -0400 Subject: [PATCH 429/562] experimental new unsat core based overlap detection --- src/smt/smt_context.cpp | 61 ++++++++++++++++++++++++++++++++--------- src/smt/smt_context.h | 20 ++++++++++++++ src/smt/smt_setup.cpp | 2 ++ src/smt/theory_str.cpp | 12 ++++++++ src/smt/theory_str.h | 1 - 5 files changed, 82 insertions(+), 14 deletions(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 4fd027031..dfe396f2b 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -37,7 +37,7 @@ Revision History: #include"model_pp.h" #include"ast_smt2_pp.h" #include"ast_translation.h" -#include"theory_str.h" +#include"theory_seq.h" namespace smt { @@ -76,6 +76,8 @@ namespace smt { m_unsat_proof(m), m_unknown("unknown"), m_unsat_core(m), + m_use_theory_str_overlap_assumption(false), + m_theoryStrOverlapAssumption_term(m_manager), #ifdef Z3DEBUG m_trail_enabled(true), #endif @@ -3269,21 +3271,38 @@ namespace smt { // PATCH for theory_str: // UNSAT + overlapping variables => UNKNOWN - if (r == l_false) { - ptr_vector::iterator it = m_theory_set.begin(); - ptr_vector::iterator end = m_theory_set.end(); - for (; it != end; ++it) { - theory * th = *it; - if (strcmp(th->get_name(), "strings") == 0) { - theory_str * str = (theory_str*)th; - if (str->overlapping_variables_detected()) { - TRACE("t_str", tout << "WARNING: overlapping variables detected, UNSAT changed to UNKNOWN!" << std::endl;); - TRACE("context", tout << "WARNING: overlapping variables detected in theory_str. UNSAT changed to UNKNOWN!" << std::endl;); - r = l_undef; - } + if (r == l_false && use_theory_str_overlap_assumption()) { + // check the unsat core for an assumption from theory_str relating to overlaps. + // if we find this assumption, we have to answer UNKNOWN + // otherwise, we can pass through UNSAT + TRACE("t_str", tout << "unsat core:\n"; + unsigned sz = m_unsat_core.size(); + for (unsigned i = 0; i < sz; i++) { + tout << mk_pp(m_unsat_core.get(i), m_manager) << "\n"; + }); + + bool assumptionFound = false; + unsigned sz = m_unsat_core.size(); + app * target_term = to_app(m_manager.mk_not(m_theoryStrOverlapAssumption_term)); + internalize_term(target_term); + for (unsigned i = 0; i < sz; ++i) { + app * core_term = to_app(m_unsat_core.get(i)); + // not sure if this is the correct way to compare exprs in this context + enode * e1; + enode * e2; + e1 = get_enode(target_term); + e2 = get_enode(core_term); + if (e1 == e2) { + // found match + TRACE("t_str", tout << "overlap detected in unsat core; changing UNSAT to UNKNOWN" << std::endl;); + assumptionFound = true; + r = l_undef; break; } } + if (!assumptionFound) { + TRACE("t_str", tout << "no overlaps detected in unsat core, answering UNSAT" << std::endl;); + } } return r; @@ -3302,6 +3321,22 @@ namespace smt { SASSERT(m_scope_lvl == 0); SASSERT(!m_setup.already_configured()); setup_context(m_fparams.m_auto_config); + + // theory_str requires the context to be set up with a special assumption. + // we need to wait until after setup_context() to know whether this is the case + if (m_use_theory_str_overlap_assumption) { + TRACE("t_str", tout << "enabling theory_str overlap assumption" << std::endl;); + // TODO maybe refactor this a bit + symbol strOverlap("!!TheoryStrOverlapAssumption!!"); + expr_ref_vector assumption(get_manager()); + seq_util m_sequtil(m_manager); + sort * s = m_manager.mk_bool_sort(); + m_theoryStrOverlapAssumption_term = expr_ref(m_manager.mk_const(strOverlap, s), m_manager); + assumption.push_back(m_manager.mk_not(m_theoryStrOverlapAssumption_term)); + // this might work, even though we already did a bit of setup + return check(assumption.size(), assumption.c_ptr(), reset_cancel); + } + internalize_assertions(); lbool r = l_undef; if (m_asserted_formulas.inconsistent()) { diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index 47ed5d671..0cf3f8d68 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -226,6 +226,9 @@ namespace smt { literal2assumption m_literal2assumption; // maps an expression associated with a literal to the original assumption expr_ref_vector m_unsat_core; + // Unsat core assumption hint for theory_str + bool m_use_theory_str_overlap_assumption; + // ----------------------------------- // // Theory case split @@ -846,6 +849,23 @@ namespace smt { */ void add_theory_aware_branching_info(bool_var v, double priority, lbool phase); + // unsat core assumption hint for theory_str + void set_use_theory_str_overlap_assumption(bool f) { + m_use_theory_str_overlap_assumption = f; + } + + bool use_theory_str_overlap_assumption() const { + return m_use_theory_str_overlap_assumption; + } + + expr_ref get_theory_str_overlap_assumption_term() { + return m_theoryStrOverlapAssumption_term; + } + + protected: + expr_ref m_theoryStrOverlapAssumption_term; + public: + // helper function for trail void undo_th_case_split(literal l); diff --git a/src/smt/smt_setup.cpp b/src/smt/smt_setup.cpp index 78a295e27..fdcf33c0e 100644 --- a/src/smt/smt_setup.cpp +++ b/src/smt/smt_setup.cpp @@ -706,6 +706,7 @@ namespace smt { } void setup::setup_QF_S() { + m_context.set_use_theory_str_overlap_assumption(true); m_context.register_plugin(alloc(smt::theory_mi_arith, m_manager, m_params)); m_context.register_plugin(alloc(smt::theory_str, m_manager, m_params)); } @@ -841,6 +842,7 @@ namespace smt { void setup::setup_str() { setup_arith(); + m_context.set_use_theory_str_overlap_assumption(true); m_context.register_plugin(alloc(theory_str, m_manager, m_params)); } diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index ff32e6f38..b69ebda4c 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4304,6 +4304,8 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { add_nonempty_constraint(commonVar); } + bool overlapAssumptionUsed = false; + expr_ref_vector arrangement_disjunction(mgr); int pos = 1; @@ -4339,6 +4341,12 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); TRACE("t_str", print_cut_var(m, tout); print_cut_var(y, tout);); + + // only add the overlap assumption one time + if (!overlapAssumptionUsed) { + arrangement_disjunction.push_back(ctx.get_theory_str_overlap_assumption_term()); + overlapAssumptionUsed = true; + } } } @@ -7239,6 +7247,9 @@ void theory_str::init_search_eh() { ast_manager & m = get_manager(); context & ctx = get_context(); + // safety + SASSERT(ctx.use_theory_str_overlap_assumption()); + TRACE("t_str_detail", tout << "dumping all asserted formulas:" << std::endl; unsigned nFormulas = ctx.get_num_asserted_formulas(); @@ -7301,6 +7312,7 @@ void theory_str::new_eq_eh(theory_var x, theory_var y) { //TRACE("t_str_detail", tout << "new eq: v#" << x << " = v#" << y << std::endl;); TRACE("t_str", tout << "new eq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " = " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); + /* if (m_find.find(x) == m_find.find(y)) { return; diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 3ea4db7d4..a8857de24 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -389,7 +389,6 @@ namespace smt { // finite model finding data // maps a finite model tester var to a list of variables that will be tested obj_map > finite_model_test_varlists; - protected: void assert_axiom(expr * e); void assert_implication(expr * premise, expr * conclusion); From a7f72bf4ef1a8c2aeb459accd2779c3f465f915f Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 13 Apr 2017 13:46:23 -0400 Subject: [PATCH 430/562] add overlap assumption to other cases in theory_str --- src/smt/theory_str.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index b69ebda4c..9d3fef6d7 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -2898,6 +2898,9 @@ bool theory_str::is_concat_eq_type1(expr * concatAst1, expr * concatAst2) { void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { ast_manager & mgr = get_manager(); context & ctx = get_context(); + + bool overlapAssumptionUsed = false; + TRACE("t_str_detail", tout << "process_concat_eq TYPE 1" << std::endl << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; @@ -3074,6 +3077,11 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); + + if (!overlapAssumptionUsed) { + overlapAssumptionUsed = true; + assert_implication(ax_l, ctx.get_theory_str_overlap_assumption_term()); + } } } } else if (splitType == 1) { @@ -3132,6 +3140,11 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); + + if (!overlapAssumptionUsed) { + overlapAssumptionUsed = true; + assert_implication(ax_l, ctx.get_theory_str_overlap_assumption_term()); + } } } } else if (splitType == -1) { @@ -3183,6 +3196,11 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); + + if (!overlapAssumptionUsed) { + overlapAssumptionUsed = true; + arrangement_disjunction.push_back(ctx.get_theory_str_overlap_assumption_term()); + } } } @@ -3227,6 +3245,11 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); TRACE("t_str_detail", {print_cut_var(x, tout); print_cut_var(n, tout);}); + + if (!overlapAssumptionUsed) { + overlapAssumptionUsed = true; + arrangement_disjunction.push_back(ctx.get_theory_str_overlap_assumption_term()); + } } } @@ -3286,6 +3309,9 @@ bool theory_str::is_concat_eq_type2(expr * concatAst1, expr * concatAst2) { void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { ast_manager & mgr = get_manager(); context & ctx = get_context(); + + bool overlapAssumptionUsed = false; + TRACE("t_str_detail", tout << "process_concat_eq TYPE 2" << std::endl << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; @@ -3466,6 +3492,11 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { } else { TRACE("t_str", tout << "AVOID LOOP: SKIP" << std::endl;); TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); + + if (!overlapAssumptionUsed) { + overlapAssumptionUsed = true; + assert_implication(ax_l, ctx.get_theory_str_overlap_assumption_term()); + } } } } @@ -3567,6 +3598,11 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); + + if (!overlapAssumptionUsed) { + overlapAssumptionUsed = true; + arrangement_disjunction.push_back(ctx.get_theory_str_overlap_assumption_term()); + } } } } @@ -3636,6 +3672,9 @@ bool theory_str::is_concat_eq_type3(expr * concatAst1, expr * concatAst2) { void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { ast_manager & mgr = get_manager(); context & ctx = get_context(); + + bool overlapAssumptionUsed = false; + TRACE("t_str_detail", tout << "process_concat_eq TYPE 3" << std::endl << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; @@ -3861,6 +3900,11 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); TRACE("t_str_detail", {print_cut_var(x, tout); print_cut_var(n, tout);}); + + if (!overlapAssumptionUsed) { + overlapAssumptionUsed = true; + assert_implication(ax_l, ctx.get_theory_str_overlap_assumption_term()); + } } } } @@ -3940,6 +3984,11 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { } else { TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); TRACE("t_str_detail", {print_cut_var(x, tout); print_cut_var(n, tout);}); + + if (!overlapAssumptionUsed) { + overlapAssumptionUsed = true; + arrangement_disjunction.push_back(ctx.get_theory_str_overlap_assumption_term()); + } } } } From 7bb5e72e073d47b123ddc15eb49c997577214e38 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sat, 15 Apr 2017 09:09:30 +0700 Subject: [PATCH 431/562] add missing string/re operations #977 and adding Pseudo-Boolean operations to Java API Signed-off-by: Nikolaj Bjorner --- src/api/java/Context.java | 139 ++++++++++++++++++++++++++++---------- 1 file changed, 102 insertions(+), 37 deletions(-) diff --git a/src/api/java/Context.java b/src/api/java/Context.java index d50968a32..549694de0 100644 --- a/src/api/java/Context.java +++ b/src/api/java/Context.java @@ -1890,43 +1890,43 @@ public class Context implements AutoCloseable { /** * Create the empty sequence. */ - public SeqExpr MkEmptySeq(Sort s) + public SeqExpr mkEmptySeq(Sort s) { checkContextMatch(s); - return new SeqExpr(this, Native.mkSeqEmpty(nCtx(), s.getNativeObject())); + return (SeqExpr) Expr.create(this, Native.mkSeqEmpty(nCtx(), s.getNativeObject())); } /** * Create the singleton sequence. */ - public SeqExpr MkUnit(Expr elem) + public SeqExpr mkUnit(Expr elem) { checkContextMatch(elem); - return new SeqExpr(this, Native.mkSeqUnit(nCtx(), elem.getNativeObject())); + return (SeqExpr) Expr.create(this, Native.mkSeqUnit(nCtx(), elem.getNativeObject())); } /** * Create a string constant. */ - public SeqExpr MkString(String s) + public SeqExpr mkString(String s) { - return new SeqExpr(this, Native.mkString(nCtx(), s)); + return (SeqExpr) Expr.create(this, Native.mkString(nCtx(), s)); } /** * Concatentate sequences. */ - public SeqExpr MkConcat(SeqExpr... t) + public SeqExpr mkConcat(SeqExpr... t) { checkContextMatch(t); - return new SeqExpr(this, Native.mkSeqConcat(nCtx(), t.length, AST.arrayToNative(t))); + return (SeqExpr) Expr.create(this, Native.mkSeqConcat(nCtx(), t.length, AST.arrayToNative(t))); } /** * Retrieve the length of a given sequence. */ - public IntExpr MkLength(SeqExpr s) + public IntExpr mkLength(SeqExpr s) { checkContextMatch(s); return (IntExpr) Expr.create(this, Native.mkSeqLength(nCtx(), s.getNativeObject())); @@ -1935,130 +1935,195 @@ public class Context implements AutoCloseable { /** * Check for sequence prefix. */ - public BoolExpr MkPrefixOf(SeqExpr s1, SeqExpr s2) + public BoolExpr mkPrefixOf(SeqExpr s1, SeqExpr s2) { checkContextMatch(s1, s2); - return new BoolExpr(this, Native.mkSeqPrefix(nCtx(), s1.getNativeObject(), s2.getNativeObject())); + return (BoolExpr) Expr.create(this, Native.mkSeqPrefix(nCtx(), s1.getNativeObject(), s2.getNativeObject())); } /** * Check for sequence suffix. */ - public BoolExpr MkSuffixOf(SeqExpr s1, SeqExpr s2) + public BoolExpr mkSuffixOf(SeqExpr s1, SeqExpr s2) { checkContextMatch(s1, s2); - return new BoolExpr(this, Native.mkSeqSuffix(nCtx(), s1.getNativeObject(), s2.getNativeObject())); + return (BoolExpr)Expr.create(this, Native.mkSeqSuffix(nCtx(), s1.getNativeObject(), s2.getNativeObject())); } /** * Check for sequence containment of s2 in s1. */ - public BoolExpr MkContains(SeqExpr s1, SeqExpr s2) + public BoolExpr mkContains(SeqExpr s1, SeqExpr s2) { checkContextMatch(s1, s2); - return new BoolExpr(this, Native.mkSeqContains(nCtx(), s1.getNativeObject(), s2.getNativeObject())); + return (BoolExpr) Expr.create(this, Native.mkSeqContains(nCtx(), s1.getNativeObject(), s2.getNativeObject())); } /** * Retrieve sequence of length one at index. */ - public SeqExpr MkAt(SeqExpr s, IntExpr index) + public SeqExpr mkAt(SeqExpr s, IntExpr index) { checkContextMatch(s, index); - return new SeqExpr(this, Native.mkSeqAt(nCtx(), s.getNativeObject(), index.getNativeObject())); + return (SeqExpr) Expr.create(this, Native.mkSeqAt(nCtx(), s.getNativeObject(), index.getNativeObject())); } /** * Extract subsequence. */ - public SeqExpr MkExtract(SeqExpr s, IntExpr offset, IntExpr length) + public SeqExpr mkExtract(SeqExpr s, IntExpr offset, IntExpr length) { checkContextMatch(s, offset, length); - return new SeqExpr(this, Native.mkSeqExtract(nCtx(), s.getNativeObject(), offset.getNativeObject(), length.getNativeObject())); + return (SeqExpr) Expr.create(this, Native.mkSeqExtract(nCtx(), s.getNativeObject(), offset.getNativeObject(), length.getNativeObject())); } /** * Extract index of sub-string starting at offset. */ - public IntExpr MkIndexOf(SeqExpr s, SeqExpr substr, ArithExpr offset) + public IntExpr mkIndexOf(SeqExpr s, SeqExpr substr, ArithExpr offset) { checkContextMatch(s, substr, offset); - return new IntExpr(this, Native.mkSeqIndex(nCtx(), s.getNativeObject(), substr.getNativeObject(), offset.getNativeObject())); + return (IntExpr)Expr.create(this, Native.mkSeqIndex(nCtx(), s.getNativeObject(), substr.getNativeObject(), offset.getNativeObject())); } /** * Replace the first occurrence of src by dst in s. */ - public SeqExpr MkReplace(SeqExpr s, SeqExpr src, SeqExpr dst) + public SeqExpr mkReplace(SeqExpr s, SeqExpr src, SeqExpr dst) { checkContextMatch(s, src, dst); - return new SeqExpr(this, Native.mkSeqReplace(nCtx(), s.getNativeObject(), src.getNativeObject(), dst.getNativeObject())); + return (SeqExpr) Expr.create(this, Native.mkSeqReplace(nCtx(), s.getNativeObject(), src.getNativeObject(), dst.getNativeObject())); } /** * Convert a regular expression that accepts sequence s. */ - public ReExpr MkToRe(SeqExpr s) + public ReExpr mkToRe(SeqExpr s) { checkContextMatch(s); - return new ReExpr(this, Native.mkSeqToRe(nCtx(), s.getNativeObject())); + return (ReExpr) Expr.create(this, Native.mkSeqToRe(nCtx(), s.getNativeObject())); } /** * Check for regular expression membership. */ - public BoolExpr MkInRe(SeqExpr s, ReExpr re) + public BoolExpr mkInRe(SeqExpr s, ReExpr re) { checkContextMatch(s, re); - return new BoolExpr(this, Native.mkSeqInRe(nCtx(), s.getNativeObject(), re.getNativeObject())); + return (BoolExpr) Expr.create(this, Native.mkSeqInRe(nCtx(), s.getNativeObject(), re.getNativeObject())); } /** * Take the Kleene star of a regular expression. */ - public ReExpr MkStar(ReExpr re) + public ReExpr mkStar(ReExpr re) { checkContextMatch(re); - return new ReExpr(this, Native.mkReStar(nCtx(), re.getNativeObject())); + return (ReExpr) Expr.create(this, Native.mkReStar(nCtx(), re.getNativeObject())); } + + /** + * Take the bounded Kleene star of a regular expression. + */ + public ReExpr mkLoop(ReExpr re, uint lo, uint hi = 0) + { + return (ReExpr) Expr.create(this, Native.mkReLoop(nCtx(), re.getNativeObject(), lo, hi)); + } + /** * Take the Kleene plus of a regular expression. */ - public ReExpr MPlus(ReExpr re) + public ReExpr mkPlus(ReExpr re) { checkContextMatch(re); - return new ReExpr(this, Native.mkRePlus(nCtx(), re.getNativeObject())); + return (ReExpr) Expr.create(this, Native.mkRePlus(nCtx(), re.getNativeObject())); } /** * Create the optional regular expression. */ - public ReExpr MOption(ReExpr re) + public ReExpr mkOption(ReExpr re) { checkContextMatch(re); - return new ReExpr(this, Native.mkReOption(nCtx(), re.getNativeObject())); + return (ReExpr) Expr.create(this, Native.mkReOption(nCtx(), re.getNativeObject())); } /** * Create the concatenation of regular languages. */ - public ReExpr MkConcat(ReExpr... t) + public ReExpr mkConcat(ReExpr... t) { checkContextMatch(t); - return new ReExpr(this, Native.mkReConcat(nCtx(), t.length, AST.arrayToNative(t))); + return (ReExpr) Expr.create(this, Native.mkReConcat(nCtx(), t.length, AST.arrayToNative(t))); } /** * Create the union of regular languages. */ - public ReExpr MkUnion(ReExpr... t) + public ReExpr mkUnion(ReExpr... t) { checkContextMatch(t); - return new ReExpr(this, Native.mkReUnion(nCtx(), t.length, AST.arrayToNative(t))); + return (ReExpr) Expr.create(this, Native.mkReUnion(nCtx(), t.length, AST.arrayToNative(t))); } + + /** + * Create a range expression. + */ + public ReExpr MkRange(SeqExpr lo, SeqExpr hi) + { + checkContextMatch(lo, hi); + return (ReExpr) Expr.create(this, Native.mkReRange(nCtx(), lo.getNativeObject(), hi.getNativeObject())); + } + + + /** + * Create an at-most-k constraint. + */ + public BoolExpr mkAtMost(BoolExpr[] args, loong k) + { + checkContextMatch(args); + return (BoolExpr) Expr.create(this, Native.mkAtmost(nCtx(), args.length, AST.arrayToNative(args), k)); + } + + /** + * Create an at-least-k constraint. + */ + public BoolExpr mkAtLeast(BoolExpr[] args, long k) + { + checkContextMatch(args); + return (BoolExpr) Expr.create(this, Native.mkAtleast(nCtx(), args.length, AST.arrayToNative(args), k)); + } + + /** + * Create a pseudo-Boolean less-or-equal constraint. + */ + public BoolExpr mkPBLe(int[] coeffs, BoolExpr[] args, int k) + { + checkContextMatch(args); + return (BoolExpr) Expr.create(this, Native.mkPble(nCtx(), args.length, AST.arrayToNative(args), coeffs, k)); + } + + /** + * Create a pseudo-Boolean greater-or-equal constraint. + */ + public BoolExpr mkPBGe(int[] coeffs, BoolExpr[] args, int k) + { + checkContextMatch(args); + return (BoolExpr) Expr.create(this, Native.mkPbge(nCtx(), args.length, AST.arrayToNative(args), coeffs, k)); + } + + /** + * Create a pseudo-Boolean equal constraint. + */ + public BoolExpr mkPBEq(int[] coeffs, BoolExpr[] args, int k) + { + checkContextMatch(args); + return (BoolExpr) Expr.create(this, Native.mkPbeq(nCtx(), args.length, AST.arrayToNative(args), coeffs, k)); + } + /** * Create a Term of a given sort. From 48638c6f1e08bd6bf9223a783b6cdd70a79c314e Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sat, 15 Apr 2017 09:34:13 +0700 Subject: [PATCH 432/562] fix for #975, add mask to ensure character encoding is unique within range of bits used for encoding Signed-off-by: Nikolaj Bjorner --- src/ast/rewriter/seq_rewriter.cpp | 3 +++ src/ast/seq_decl_plugin.cpp | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ast/rewriter/seq_rewriter.cpp b/src/ast/rewriter/seq_rewriter.cpp index 3d7da43a7..b4dbfa6df 100644 --- a/src/ast/rewriter/seq_rewriter.cpp +++ b/src/ast/rewriter/seq_rewriter.cpp @@ -1435,6 +1435,7 @@ bool seq_rewriter::reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_ zstring s; bool lchange = false; SASSERT(lhs.empty()); + TRACE("seq", tout << ls << "\n"; tout << rs << "\n";); // solve from back while (true) { while (!rs.empty() && m_util.str.is_empty(rs.back())) { @@ -1552,9 +1553,11 @@ bool seq_rewriter::reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_ head2 < rs.size() && m_util.str.is_string(ls[head1].get(), s1) && m_util.str.is_string(rs[head2].get(), s2)) { + TRACE("seq", tout << s1 << " - " << s2 << " " << s1.length() << " " << s2.length() << "\n";); unsigned l = std::min(s1.length(), s2.length()); for (unsigned i = 0; i < l; ++i) { if (s1[i] != s2[i]) { + TRACE("seq", tout << "different at position " << i << " " << s1[i] << " " << s2[i] << "\n";); return false; } } diff --git a/src/ast/seq_decl_plugin.cpp b/src/ast/seq_decl_plugin.cpp index 9af4687d2..f282043e6 100644 --- a/src/ast/seq_decl_plugin.cpp +++ b/src/ast/seq_decl_plugin.cpp @@ -126,13 +126,14 @@ static bool is_escape_char(char const *& s, unsigned& result) { zstring::zstring(encoding enc): m_encoding(enc) {} zstring::zstring(char const* s, encoding enc): m_encoding(enc) { + unsigned mask = 0xFF; // TBD for UTF while (*s) { unsigned ch; if (is_escape_char(s, ch)) { - m_buffer.push_back(ch); + m_buffer.push_back(ch & mask); } else { - m_buffer.push_back(*s); + m_buffer.push_back(*s & mask); ++s; } } From e4b9080165dce794561f26d4008fd9c9a0eb9bde Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sat, 15 Apr 2017 15:04:13 +0800 Subject: [PATCH 433/562] include timeout/rlimit parameters in optmize Signed-off-by: Nikolaj Bjorner --- src/api/api_opt.cpp | 2 +- src/api/java/Context.java | 12 ++++++++++-- src/opt/opt_params.pyg | 2 ++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/api/api_opt.cpp b/src/api/api_opt.cpp index a2299aec3..0d1b4ef8d 100644 --- a/src/api/api_opt.cpp +++ b/src/api/api_opt.cpp @@ -128,7 +128,7 @@ extern "C" { lbool r = l_undef; cancel_eh eh(mk_c(c)->m().limit()); unsigned timeout = to_optimize_ptr(o)->get_params().get_uint("timeout", mk_c(c)->get_timeout()); - unsigned rlimit = mk_c(c)->get_rlimit(); + unsigned rlimit = to_optimize_ptr(o)->get_params().get_uint("rlimit", mk_c(c)->get_rlimit()); api::context::set_interruptable si(*(mk_c(c)), eh); { scoped_timer timer(timeout, &eh); diff --git a/src/api/java/Context.java b/src/api/java/Context.java index 549694de0..9fe4e0c38 100644 --- a/src/api/java/Context.java +++ b/src/api/java/Context.java @@ -2024,13 +2024,21 @@ public class Context implements AutoCloseable { } /** - * Take the bounded Kleene star of a regular expression. + * Take the lower and upper-bounded Kleene star of a regular expression. */ - public ReExpr mkLoop(ReExpr re, uint lo, uint hi = 0) + public ReExpr mkLoop(ReExpr re, uint lo, uint hi) { return (ReExpr) Expr.create(this, Native.mkReLoop(nCtx(), re.getNativeObject(), lo, hi)); } + /** + * Take the lower-bounded Kleene star of a regular expression. + */ + public ReExpr mkLoop(ReExpr re, uint lo) + { + return (ReExpr) Expr.create(this, Native.mkReLoop(nCtx(), re.getNativeObject(), lo, 0)); + } + /** * Take the Kleene plus of a regular expression. diff --git a/src/opt/opt_params.pyg b/src/opt/opt_params.pyg index a7c9e0011..51f58396c 100644 --- a/src/opt/opt_params.pyg +++ b/src/opt/opt_params.pyg @@ -5,6 +5,8 @@ def_module_params('opt', ('maxsat_engine', SYMBOL, 'maxres', "select engine for maxsat: 'core_maxsat', 'wmax', 'maxres', 'pd-maxres'"), ('priority', SYMBOL, 'lex', "select how to priortize objectives: 'lex' (lexicographic), 'pareto', or 'box'"), ('dump_benchmarks', BOOL, False, 'dump benchmarks for profiling'), + ('timeout', UINT, UINT_MAX, 'timeout (in milliseconds) (UINT_MAX and 0 mean no timeout)'), + ('rlimit', UINT, 0, 'resource limit (0 means no limit)'), ('print_model', BOOL, False, 'display model for satisfiable constraints'), ('enable_sls', BOOL, False, 'enable SLS tuning during weighted maxsast'), ('enable_sat', BOOL, True, 'enable the new SAT core for propositional constraints'), From 87c3b5ee5140b758e3dff863b39b0e9128250b02 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sat, 15 Apr 2017 15:29:02 +0800 Subject: [PATCH 434/562] replace uint by long Signed-off-by: Nikolaj Bjorner --- src/api/java/Context.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/java/Context.java b/src/api/java/Context.java index 9fe4e0c38..4a443f171 100644 --- a/src/api/java/Context.java +++ b/src/api/java/Context.java @@ -2026,7 +2026,7 @@ public class Context implements AutoCloseable { /** * Take the lower and upper-bounded Kleene star of a regular expression. */ - public ReExpr mkLoop(ReExpr re, uint lo, uint hi) + public ReExpr mkLoop(ReExpr re, long lo, long hi) { return (ReExpr) Expr.create(this, Native.mkReLoop(nCtx(), re.getNativeObject(), lo, hi)); } @@ -2034,7 +2034,7 @@ public class Context implements AutoCloseable { /** * Take the lower-bounded Kleene star of a regular expression. */ - public ReExpr mkLoop(ReExpr re, uint lo) + public ReExpr mkLoop(ReExpr re, long lo) { return (ReExpr) Expr.create(this, Native.mkReLoop(nCtx(), re.getNativeObject(), lo, 0)); } From ab6abe901fcf96c0509eb17251cd6b8fd2d9d6bd Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sat, 15 Apr 2017 15:57:30 +0800 Subject: [PATCH 435/562] replace long by int Signed-off-by: Nikolaj Bjorner --- src/api/java/Context.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/api/java/Context.java b/src/api/java/Context.java index 4a443f171..da7c692b8 100644 --- a/src/api/java/Context.java +++ b/src/api/java/Context.java @@ -2026,7 +2026,7 @@ public class Context implements AutoCloseable { /** * Take the lower and upper-bounded Kleene star of a regular expression. */ - public ReExpr mkLoop(ReExpr re, long lo, long hi) + public ReExpr mkLoop(ReExpr re, int lo, int hi) { return (ReExpr) Expr.create(this, Native.mkReLoop(nCtx(), re.getNativeObject(), lo, hi)); } @@ -2034,7 +2034,7 @@ public class Context implements AutoCloseable { /** * Take the lower-bounded Kleene star of a regular expression. */ - public ReExpr mkLoop(ReExpr re, long lo) + public ReExpr mkLoop(ReExpr re, int lo) { return (ReExpr) Expr.create(this, Native.mkReLoop(nCtx(), re.getNativeObject(), lo, 0)); } @@ -2090,7 +2090,7 @@ public class Context implements AutoCloseable { /** * Create an at-most-k constraint. */ - public BoolExpr mkAtMost(BoolExpr[] args, loong k) + public BoolExpr mkAtMost(BoolExpr[] args, long k) { checkContextMatch(args); return (BoolExpr) Expr.create(this, Native.mkAtmost(nCtx(), args.length, AST.arrayToNative(args), k)); From 9933c360507ed795eeaff816e99d0d14417956f8 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sat, 15 Apr 2017 17:06:03 +0800 Subject: [PATCH 436/562] replace long by int Signed-off-by: Nikolaj Bjorner --- src/api/java/Context.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/java/Context.java b/src/api/java/Context.java index da7c692b8..790ffdca7 100644 --- a/src/api/java/Context.java +++ b/src/api/java/Context.java @@ -2090,7 +2090,7 @@ public class Context implements AutoCloseable { /** * Create an at-most-k constraint. */ - public BoolExpr mkAtMost(BoolExpr[] args, long k) + public BoolExpr mkAtMost(BoolExpr[] args, int k) { checkContextMatch(args); return (BoolExpr) Expr.create(this, Native.mkAtmost(nCtx(), args.length, AST.arrayToNative(args), k)); @@ -2099,7 +2099,7 @@ public class Context implements AutoCloseable { /** * Create an at-least-k constraint. */ - public BoolExpr mkAtLeast(BoolExpr[] args, long k) + public BoolExpr mkAtLeast(BoolExpr[] args, int k) { checkContextMatch(args); return (BoolExpr) Expr.create(this, Native.mkAtleast(nCtx(), args.length, AST.arrayToNative(args), k)); From 8b5627e361e2e19ff26dac18d920e01906590ad4 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sun, 16 Apr 2017 12:13:30 +0900 Subject: [PATCH 437/562] additional API per #977 Signed-off-by: Nikolaj Bjorner --- src/api/java/Context.java | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/api/java/Context.java b/src/api/java/Context.java index 790ffdca7..db7a08711 100644 --- a/src/api/java/Context.java +++ b/src/api/java/Context.java @@ -2057,7 +2057,17 @@ public class Context implements AutoCloseable { checkContextMatch(re); return (ReExpr) Expr.create(this, Native.mkReOption(nCtx(), re.getNativeObject())); } + + /** + * Create the complement regular expression. + */ + public ReExpr mkComplement(ReExpr re) + { + checkContextMatch(re); + return (ReExpr) Expr.create(this, Native.mkReComplement(nCtx(), re.getNativeObject())); + } + /** * Create the concatenation of regular languages. */ @@ -2075,7 +2085,15 @@ public class Context implements AutoCloseable { checkContextMatch(t); return (ReExpr) Expr.create(this, Native.mkReUnion(nCtx(), t.length, AST.arrayToNative(t))); } - + + /** + * Create the intersection of regular languages. + */ + public ReExpr mkIntersect(ReExpr... t) + { + checkContextMatch(t); + return (ReExpr) Expr.create(this, Native.mkReIntersect(nCtx(), t.length, AST.arrayToNative(t))); + } /** * Create a range expression. From 66e61b8a31a04a858af570e0665fcdd61fc0c2d2 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Mon, 17 Apr 2017 03:03:59 -0700 Subject: [PATCH 438/562] issues #963 #912 --- src/ast/rewriter/seq_rewriter.cpp | 31 ++++++++++++++++++++++- src/smt/theory_seq.cpp | 41 +++++++++++++++++++++++++------ src/smt/theory_seq.h | 1 + 3 files changed, 64 insertions(+), 9 deletions(-) diff --git a/src/ast/rewriter/seq_rewriter.cpp b/src/ast/rewriter/seq_rewriter.cpp index b4dbfa6df..3737f4651 100644 --- a/src/ast/rewriter/seq_rewriter.cpp +++ b/src/ast/rewriter/seq_rewriter.cpp @@ -60,7 +60,12 @@ expr_ref sym_expr::accept(expr* e) { } std::ostream& sym_expr::display(std::ostream& out) const { - return out << m_t; + switch (m_ty) { + case t_char: return out << m_t; + case t_range: return out << m_t << ":" << m_s; + case t_pred: return out << m_t; + } + return out << "expression type not recognized"; } struct display_expr1 { @@ -237,6 +242,7 @@ eautomaton* re2automaton::re2aut(expr* e) { unsigned nb = s1.num_bits(); expr_ref _start(bv.mk_numeral(start, nb), m); expr_ref _stop(bv.mk_numeral(stop, nb), m); + TRACE("seq", tout << "Range: " << start << " " << stop << "\n";); a = alloc(eautomaton, sm, sym_expr::mk_range(_start, _stop)); return a.detach(); } @@ -646,6 +652,29 @@ br_status seq_rewriter::mk_seq_contains(expr* a, expr* b, expr_ref& result) { result = m().mk_true(); return BR_DONE; } + bool all_units = true; + for (unsigned i = 0; i < bs.size(); ++i) { + all_units = m_util.str.is_unit(bs[i].get()); + } + for (unsigned i = 0; i < as.size(); ++i) { + all_units = m_util.str.is_unit(as[i].get()); + } + if (all_units) { + if (as.size() < bs.size()) { + result = m().mk_false(); + return BR_DONE; + } + expr_ref_vector ors(m()); + for (unsigned i = 0; i < as.size() - bs.size() + 1; ++i) { + expr_ref_vector ands(m()); + for (unsigned j = 0; j < bs.size(); ++j) { + ands.push_back(m().mk_eq(as[i + j].get(), bs[j].get())); + } + ors.push_back(::mk_and(ands)); + } + result = ::mk_or(ors); + return BR_REWRITE_FULL; + } return BR_FAILED; } diff --git a/src/smt/theory_seq.cpp b/src/smt/theory_seq.cpp index 672d3c440..9dd082de0 100644 --- a/src/smt/theory_seq.cpp +++ b/src/smt/theory_seq.cpp @@ -255,6 +255,11 @@ final_check_status theory_seq::final_check_eh() { TRACE("seq", tout << ">>solve_eqs\n";); return FC_CONTINUE; } + if (check_contains()) { + ++m_stats.m_propagate_contains; + TRACE("seq", tout << ">>propagate_contains\n";); + return FC_CONTINUE; + } if (solve_nqs(0)) { ++m_stats.m_solve_nqs; TRACE("seq", tout << ">>solve_nqs\n";); @@ -290,11 +295,6 @@ final_check_status theory_seq::final_check_eh() { TRACE("seq", tout << ">>propagate_automata\n";); return FC_CONTINUE; } - if (check_contains()) { - ++m_stats.m_propagate_contains; - TRACE("seq", tout << ">>propagate_contains\n";); - return FC_CONTINUE; - } if (is_solved()) { TRACE("seq", tout << ">>is_solved\n";); return FC_DONE; @@ -1159,7 +1159,7 @@ bool theory_seq::check_extensionality() { } /* - \brief check negated contains constriants. + \brief check negated contains constraints. */ bool theory_seq::check_contains() { context & ctx = get_context(); @@ -1199,6 +1199,11 @@ bool theory_seq::is_solved() { IF_VERBOSE(10, display_disequation(verbose_stream() << "(seq.giveup ", m_nqs[0]); verbose_stream() << " is unsolved)\n";); return false; } + if (!m_ncs.empty()) { + TRACE("seq", display_nc(tout << "(seq.giveup ", m_ncs[0]); tout << " is unsolved)\n";); + IF_VERBOSE(10, display_nc(verbose_stream() << "(seq.giveup ", m_ncs[0]); verbose_stream() << " is unsolved)\n";); + return false; + } return true; } @@ -1984,6 +1989,22 @@ bool theory_seq::solve_nc(unsigned idx) { m_new_propagation = true; return true; } + + expr* e1, *e2; + if (m.is_eq(c, e1, e2)) { + literal eq = mk_eq(e1, e2, false); + propagate_lit(deps, 0, 0, ~eq); + return true; + } + + if (m.is_or(c)) { + for (unsigned i = 0; i < to_app(c)->get_num_args(); ++i) { + expr_ref ci(to_app(c)->get_arg(i), m); + m_ncs.push_back(nc(ci, deps)); + } + m_new_propagation = true; + return true; + } return false; } @@ -2418,13 +2439,17 @@ void theory_seq::display(std::ostream & out) const { if (!m_ncs.empty()) { out << "Non contains:\n"; for (unsigned i = 0; i < m_ncs.size(); ++i) { - out << "not " << mk_pp(m_ncs[i].contains(), m) << "\n"; - display_deps(out << " <- ", m_ncs[i].deps()); out << "\n"; + display_nc(out, m_ncs[i]); } } } +void theory_seq::display_nc(std::ostream& out, nc const& nc) const { + out << "not " << mk_pp(nc.contains(), m) << "\n"; + display_deps(out << " <- ", nc.deps()); out << "\n"; +} + void theory_seq::display_equations(std::ostream& out) const { for (unsigned i = 0; i < m_eqs.size(); ++i) { display_equation(out, m_eqs[i]); diff --git a/src/smt/theory_seq.h b/src/smt/theory_seq.h index aa7ddec1b..2b8fb2fd7 100644 --- a/src/smt/theory_seq.h +++ b/src/smt/theory_seq.h @@ -570,6 +570,7 @@ namespace smt { void display_disequation(std::ostream& out, ne const& e) const; void display_deps(std::ostream& out, dependency* deps) const; void display_deps(std::ostream& out, literal_vector const& lits, enode_pair_vector const& eqs) const; + void display_nc(std::ostream& out, nc const& nc) const; public: theory_seq(ast_manager& m); virtual ~theory_seq(); From 352f8b6cb974bb3c6c2edb24ac8dd2883d3e682f Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Mon, 17 Apr 2017 13:04:57 -0700 Subject: [PATCH 439/562] fixing local search Signed-off-by: Nikolaj Bjorner --- src/sat/sat_local_search.cpp | 30 ++++++++++++++++++------------ src/sat/sat_local_search.h | 8 ++++---- src/sat/sat_parallel.cpp | 12 +++++------- src/sat/sat_parallel.h | 2 ++ src/sat/sat_solver.cpp | 3 +++ 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/sat/sat_local_search.cpp b/src/sat/sat_local_search.cpp index 6b0205cc2..c5e7fcf54 100644 --- a/src/sat/sat_local_search.cpp +++ b/src/sat/sat_local_search.cpp @@ -70,9 +70,10 @@ namespace sat { void local_search::init_cur_solution() { for (unsigned v = 0; v < num_vars(); ++v) { - // use bias half the time. - if (m_rand() % 100 < 10) { - m_vars[v].m_value = ((unsigned)(m_rand() % 100) < m_vars[v].m_bias); + // use bias with a small probability + if (m_rand() % 100 < 3) { + //m_vars[v].m_value = ((unsigned)(m_rand() % 100) < m_vars[v].m_bias); + m_vars[v].m_value = (50 < m_vars[v].m_bias); } } } @@ -140,14 +141,17 @@ namespace sat { // the following methods does NOT converge for pseudo-boolean // can try other way to define "worse" and "better" // the current best noise is below 1000 - if (best_unsat_rate >= last_best_unsat_rate) { +#if 0 + if (m_best_unsat_rate > m_last_best_unsat_rate) { // worse m_noise -= m_noise * 2 * m_noise_delta; + m_best_unsat_rate *= 1000.0; } else { // better m_noise += (10000 - m_noise) * m_noise_delta; } +#endif for (unsigned i = 0; i < m_constraints.size(); ++i) { constraint& c = m_constraints[i]; c.m_slack = c.m_k; @@ -178,6 +182,8 @@ namespace sat { init_slack(); init_scores(); init_goodvars(); + + m_best_unsat = m_unsat_stack.size(); } void local_search::calculate_and_update_ob() { @@ -382,13 +388,13 @@ namespace sat { IF_VERBOSE(1, verbose_stream() << "(sat-local-search" \ << " :flips " << flips \ << " :noise " << m_noise \ - << " :unsat " << /*m_unsat_stack.size()*/ best_unsat \ + << " :unsat " << /*m_unsat_stack.size()*/ m_best_unsat \ << " :time " << (timer.get_seconds() < 0.001 ? 0.0 : timer.get_seconds()) << ")\n";); \ } void local_search::walksat() { - best_unsat_rate = 1; - last_best_unsat_rate = 1; + m_best_unsat_rate = 1; + m_last_best_unsat_rate = 1; reinit(); @@ -398,10 +404,10 @@ namespace sat { PROGRESS(tries, total_flips); for (tries = 1; !m_unsat_stack.empty() && m_limit.inc(); ++tries) { - if (m_unsat_stack.size() < best_unsat) { - best_unsat = m_unsat_stack.size(); - last_best_unsat_rate = best_unsat_rate; - best_unsat_rate = (double)m_unsat_stack.size() / num_constraints(); + if (m_unsat_stack.size() < m_best_unsat) { + m_best_unsat = m_unsat_stack.size(); + m_last_best_unsat_rate = m_best_unsat_rate; + m_best_unsat_rate = (double)m_unsat_stack.size() / num_constraints(); } for (step = 0; step < m_max_steps && !m_unsat_stack.empty(); ++step) { pick_flip_walksat(); @@ -506,7 +512,7 @@ namespace sat { SASSERT(c.m_k < constraint_value(c)); // TBD: dynamic noise strategy //if (m_rand() % 100 < 98) { - if (m_rand() % 10000 >= m_noise) { + if (m_rand() % 10000 <= m_noise) { // take this branch with 98% probability. // find the first one, to fast break the rest unsigned best_bsb = 0; diff --git a/src/sat/sat_local_search.h b/src/sat/sat_local_search.h index 678eee60a..918f5328d 100644 --- a/src/sat/sat_local_search.h +++ b/src/sat/sat_local_search.h @@ -164,9 +164,9 @@ namespace sat { // information about solution - unsigned best_unsat; - double best_unsat_rate; - double last_best_unsat_rate; + unsigned m_best_unsat; + double m_best_unsat_rate; + double m_last_best_unsat_rate; int m_objective_value; // the objective function value corresponds to the current solution bool_vector m_best_solution; // !var: the best solution so far int m_best_objective_value = -1; // the objective value corresponds to the best solution so far @@ -176,7 +176,7 @@ namespace sat { unsigned m_max_steps = (1 << 30); // dynamic noise - double m_noise = 400; // normalized by 10000 + double m_noise = 9800; // normalized by 10000 double m_noise_delta = 0.05; // for tuning diff --git a/src/sat/sat_parallel.cpp b/src/sat/sat_parallel.cpp index 67d446a29..0b9a5bcb3 100644 --- a/src/sat/sat_parallel.cpp +++ b/src/sat/sat_parallel.cpp @@ -115,12 +115,16 @@ namespace sat { m_solvers[i] = alloc(sat::solver, s.m_params, m_limits[i]); m_solvers[i]->copy(s); m_solvers[i]->set_par(this, i); - m_scoped_rlimit.push_child(&m_solvers[i]->rlimit()); + push_child(m_solvers[i]->rlimit()); } s.set_par(this, num_extra_solvers); s.m_params.set_sym("phase", saved_phase); } + void parallel::push_child(reslimit& rl) { + m_scoped_rlimit.push_child(&rl); + } + void parallel::exchange(solver& s, literal_vector const& in, unsigned& limit, literal_vector& out) { if (s.m_par_syncing_clauses) return; @@ -268,12 +272,6 @@ namespace sat { { if (m_solver_copy && s.num_non_binary_clauses() > m_solver_copy->m_clauses.size()) { s.import(*m_solver_copy.get(), true); - m_num_clauses = s.num_non_binary_clauses(); - SASSERT(s.num_non_binary_clauses() == m_solver_copy->m_clauses.size()); - m_solver_copy = 0; - } - if (m_num_clauses < s.num_non_binary_clauses()) { - m_num_clauses = s.num_non_binary_clauses(); } for (unsigned i = 0; i < m_phase.size(); ++i) { s.set_phase(i, m_phase[i]); diff --git a/src/sat/sat_parallel.h b/src/sat/sat_parallel.h index b93384bd6..ffdfddf55 100644 --- a/src/sat/sat_parallel.h +++ b/src/sat/sat_parallel.h @@ -78,6 +78,8 @@ namespace sat { void init_solvers(solver& s, unsigned num_extra_solvers); + void push_child(reslimit& rl); + // reserve space void reserve(unsigned num_owners, unsigned sz) { m_pool.reserve(num_owners, sz); } diff --git a/src/sat/sat_solver.cpp b/src/sat/sat_solver.cpp index 66ebb189f..d25c8bdee 100644 --- a/src/sat/sat_solver.cpp +++ b/src/sat/sat_solver.cpp @@ -875,6 +875,9 @@ namespace sat { sat::parallel par(*this); par.reserve(num_threads, 1 << 12); par.init_solvers(*this, num_extra_solvers); + for (unsigned i = 0; i < ls.size(); ++i) { + par.push_child(ls[i]->rlimit()); + } int finished_id = -1; std::string ex_msg; par_exception_kind ex_kind; From 71da36f85c221765dcc8f8cf5b3ecd356a50ee72 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Tue, 18 Apr 2017 15:13:11 +0100 Subject: [PATCH 440/562] Added core.extend_nonlocal_patterns parameter to improve unsat cores. --- src/smt/params/smt_params_helper.pyg | 3 +- src/smt/smt_solver.cpp | 62 ++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/src/smt/params/smt_params_helper.pyg b/src/smt/params/smt_params_helper.pyg index a3f163ed4..6ac4aab04 100644 --- a/src/smt/params/smt_params_helper.pyg +++ b/src/smt/params/smt_params_helper.pyg @@ -64,5 +64,6 @@ def_module_params(module_name='smt', ('core.validate', BOOL, False, 'validate unsat core produced by SMT context'), ('core.minimize', BOOL, False, 'minimize unsat core produced by SMT context'), ('core.extend_patterns', BOOL, False, 'extend unsat core with literals that trigger (potential) quantifier instances'), - ('core.extend_patterns.max_distance', UINT, UINT_MAX, 'limits the distance of a pattern-extended unsat core') + ('core.extend_patterns.max_distance', UINT, UINT_MAX, 'limits the distance of a pattern-extended unsat core'), + ('core.extend_nonlocal_patterns', BOOL, False, 'extend unsat cores with literals that have quantifiers with patterns that contain symbols which are not in the quantifier\'s body') )) diff --git a/src/smt/smt_solver.cpp b/src/smt/smt_solver.cpp index 59a5ddf8f..cd912b72e 100644 --- a/src/smt/smt_solver.cpp +++ b/src/smt/smt_solver.cpp @@ -38,6 +38,7 @@ namespace smt { bool m_minimizing_core; bool m_core_extend_patterns; unsigned m_core_extend_patterns_max_distance; + bool m_core_extend_nonlocal_patterns; obj_map m_name2assertion; public: @@ -48,13 +49,15 @@ namespace smt { m_context(m, m_smt_params), m_minimizing_core(false), m_core_extend_patterns(false), - m_core_extend_patterns_max_distance(UINT_MAX) { + m_core_extend_patterns_max_distance(UINT_MAX), + m_core_extend_nonlocal_patterns(false) { m_logic = l; if (m_logic != symbol::null) m_context.set_logic(m_logic); smt_params_helper smth(p); m_core_extend_patterns = smth.core_extend_patterns(); m_core_extend_patterns_max_distance = smth.core_extend_patterns_max_distance(); + m_core_extend_nonlocal_patterns = smth.core_extend_nonlocal_patterns(); } virtual solver * translate(ast_manager & m, params_ref const & p) { @@ -81,6 +84,8 @@ namespace smt { m_context.updt_params(p); smt_params_helper smth(p); m_core_extend_patterns = smth.core_extend_patterns(); + m_core_extend_patterns_max_distance = smth.core_extend_patterns_max_distance(); + m_core_extend_nonlocal_patterns = smth.core_extend_nonlocal_patterns(); } virtual void collect_param_descrs(param_descrs & r) { @@ -172,6 +177,8 @@ namespace smt { if (m_core_extend_patterns) add_pattern_literals_to_core(r); + if (m_core_extend_nonlocal_patterns) + add_nonlocal_pattern_literals_to_core(r); } virtual void get_model(model_ref & m) { @@ -250,7 +257,7 @@ namespace smt { } }; - void collect_pattern_func_decls(expr_ref & e, func_decl_set & fds) { + void collect_pattern_fds(expr_ref & e, func_decl_set & fds) { collect_pattern_fds_proc p(get_manager(), fds); expr_mark visited; for_each_expr(p, visited, e); @@ -295,7 +302,7 @@ namespace smt { expr_ref name(core[i], m); SASSERT(m_name2assertion.contains(name)); expr_ref assrtn(m_name2assertion.find(name), m); - collect_pattern_func_decls(assrtn, pattern_fds); + collect_pattern_fds(assrtn, pattern_fds); } if (!pattern_fds.empty()) { @@ -317,6 +324,55 @@ namespace smt { break; } } + + struct collect_body_fds_proc { + ast_manager & m; + func_decl_set & m_fds; + collect_body_fds_proc(ast_manager & m, func_decl_set & fds) : + m(m), m_fds(fds) { + } + void operator()(var * n) {} + void operator()(app * n) {} + void operator()(quantifier * n) { + collect_fds_proc p(m, m_fds); + expr_fast_mark1 visited; + quick_for_each_expr(p, visited, n->get_expr()); + } + }; + + void collect_body_func_decls(expr_ref & e, func_decl_set & fds) { + ast_manager & m = get_manager(); + collect_body_fds_proc p(m, fds); + expr_mark visited; + for_each_expr(p, visited, e); + } + + void add_nonlocal_pattern_literals_to_core(ptr_vector & core) { + ast_manager & m = get_manager(); + + obj_map::iterator it = m_name2assertion.begin(); + obj_map::iterator end = m_name2assertion.end(); + for (unsigned i = 0; it != end; it++, i++) { + expr_ref name(it->m_key, m); + expr_ref assrtn(it->m_value, m); + + if (!core.contains(name)) { + func_decl_set pattern_fds, body_fds; + collect_pattern_fds(assrtn, pattern_fds); + collect_body_func_decls(assrtn, body_fds); + + func_decl_set::iterator pit = pattern_fds.begin(); + func_decl_set::iterator pend= pattern_fds.end(); + for (; pit != pend; pit++) { + func_decl * fd = *pit; + if (!body_fds.contains(fd)) { + core.insert(name); + break; + } + } + } + } + } }; }; From bef64961ae985969ca4a02e9d8e11f0aecb49a26 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 18 Apr 2017 13:12:03 -0400 Subject: [PATCH 441/562] add pre-init assumptions for smt theories --- src/smt/smt_context.cpp | 17 ++++++++++++++++- src/smt/smt_context.h | 15 --------------- src/smt/smt_setup.cpp | 2 -- src/smt/smt_theory.h | 7 +++++++ src/smt/theory_str.cpp | 31 +++++++++++++++++++------------ src/smt/theory_str.h | 2 ++ 6 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index dfe396f2b..db09552ef 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -77,7 +77,6 @@ namespace smt { m_unknown("unknown"), m_unsat_core(m), m_use_theory_str_overlap_assumption(false), - m_theoryStrOverlapAssumption_term(m_manager), #ifdef Z3DEBUG m_trail_enabled(true), #endif @@ -3269,6 +3268,7 @@ namespace smt { r = l_undef; } + /* // PATCH for theory_str: // UNSAT + overlapping variables => UNKNOWN if (r == l_false && use_theory_str_overlap_assumption()) { @@ -3304,6 +3304,7 @@ namespace smt { TRACE("t_str", tout << "no overlaps detected in unsat core, answering UNSAT" << std::endl;); } } + */ return r; } @@ -3322,6 +3323,7 @@ namespace smt { SASSERT(!m_setup.already_configured()); setup_context(m_fparams.m_auto_config); + /* // theory_str requires the context to be set up with a special assumption. // we need to wait until after setup_context() to know whether this is the case if (m_use_theory_str_overlap_assumption) { @@ -3336,6 +3338,19 @@ namespace smt { // this might work, even though we already did a bit of setup return check(assumption.size(), assumption.c_ptr(), reset_cancel); } + */ + + expr_ref_vector theory_assumptions(m_manager); + ptr_vector::iterator it = m_theory_set.begin(); + ptr_vector::iterator end = m_theory_set.end(); + for (; it != end; ++it) { + (*it)->add_theory_assumptions(theory_assumptions); + } + if (!theory_assumptions.empty()) { + TRACE("search", tout << "Adding theory assumptions to context" << std::endl;); + // this works even though we already did part of setup + return check(theory_assumptions.size(), theory_assumptions.c_ptr(), reset_cancel); + } internalize_assertions(); lbool r = l_undef; diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index 0cf3f8d68..0667f622e 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -849,21 +849,6 @@ namespace smt { */ void add_theory_aware_branching_info(bool_var v, double priority, lbool phase); - // unsat core assumption hint for theory_str - void set_use_theory_str_overlap_assumption(bool f) { - m_use_theory_str_overlap_assumption = f; - } - - bool use_theory_str_overlap_assumption() const { - return m_use_theory_str_overlap_assumption; - } - - expr_ref get_theory_str_overlap_assumption_term() { - return m_theoryStrOverlapAssumption_term; - } - - protected: - expr_ref m_theoryStrOverlapAssumption_term; public: // helper function for trail diff --git a/src/smt/smt_setup.cpp b/src/smt/smt_setup.cpp index fdcf33c0e..78a295e27 100644 --- a/src/smt/smt_setup.cpp +++ b/src/smt/smt_setup.cpp @@ -706,7 +706,6 @@ namespace smt { } void setup::setup_QF_S() { - m_context.set_use_theory_str_overlap_assumption(true); m_context.register_plugin(alloc(smt::theory_mi_arith, m_manager, m_params)); m_context.register_plugin(alloc(smt::theory_str, m_manager, m_params)); } @@ -842,7 +841,6 @@ namespace smt { void setup::setup_str() { setup_arith(); - m_context.set_use_theory_str_overlap_assumption(true); m_context.register_plugin(alloc(theory_str, m_manager, m_params)); } diff --git a/src/smt/smt_theory.h b/src/smt/smt_theory.h index cee36535f..e412f2f1b 100644 --- a/src/smt/smt_theory.h +++ b/src/smt/smt_theory.h @@ -177,6 +177,13 @@ namespace smt { virtual void restart_eh() { } + /** + \brief This method is called by smt_context before the search starts to get any + extra assumptions the theory wants to use. (see theory_str for an example) + */ + virtual void add_theory_assumptions(expr_ref_vector & assumptions) { + } + /** \brief This method is invoked before the search starts. */ diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 9d3fef6d7..354589318 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -56,6 +56,7 @@ theory_str::theory_str(ast_manager & m, theory_str_params const & params): tmpValTestVarCount(0), avoidLoopCut(true), loopDetected(false), + m_theoryStrOverlapAssumption_term(m), contains_map(m), string_int_conversion_terms(m), totalCacheAccessCount(0), @@ -3080,7 +3081,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { if (!overlapAssumptionUsed) { overlapAssumptionUsed = true; - assert_implication(ax_l, ctx.get_theory_str_overlap_assumption_term()); + assert_implication(ax_l, m_theoryStrOverlapAssumption_term); } } } @@ -3143,7 +3144,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { if (!overlapAssumptionUsed) { overlapAssumptionUsed = true; - assert_implication(ax_l, ctx.get_theory_str_overlap_assumption_term()); + assert_implication(ax_l, m_theoryStrOverlapAssumption_term); } } } @@ -3199,7 +3200,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { if (!overlapAssumptionUsed) { overlapAssumptionUsed = true; - arrangement_disjunction.push_back(ctx.get_theory_str_overlap_assumption_term()); + arrangement_disjunction.push_back(m_theoryStrOverlapAssumption_term); } } } @@ -3248,7 +3249,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { if (!overlapAssumptionUsed) { overlapAssumptionUsed = true; - arrangement_disjunction.push_back(ctx.get_theory_str_overlap_assumption_term()); + arrangement_disjunction.push_back(m_theoryStrOverlapAssumption_term); } } } @@ -3495,7 +3496,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { if (!overlapAssumptionUsed) { overlapAssumptionUsed = true; - assert_implication(ax_l, ctx.get_theory_str_overlap_assumption_term()); + assert_implication(ax_l, m_theoryStrOverlapAssumption_term); } } } @@ -3601,7 +3602,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { if (!overlapAssumptionUsed) { overlapAssumptionUsed = true; - arrangement_disjunction.push_back(ctx.get_theory_str_overlap_assumption_term()); + arrangement_disjunction.push_back(m_theoryStrOverlapAssumption_term); } } } @@ -3903,7 +3904,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { if (!overlapAssumptionUsed) { overlapAssumptionUsed = true; - assert_implication(ax_l, ctx.get_theory_str_overlap_assumption_term()); + assert_implication(ax_l, m_theoryStrOverlapAssumption_term); } } } @@ -3987,7 +3988,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { if (!overlapAssumptionUsed) { overlapAssumptionUsed = true; - arrangement_disjunction.push_back(ctx.get_theory_str_overlap_assumption_term()); + arrangement_disjunction.push_back(m_theoryStrOverlapAssumption_term); } } } @@ -4393,7 +4394,7 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { // only add the overlap assumption one time if (!overlapAssumptionUsed) { - arrangement_disjunction.push_back(ctx.get_theory_str_overlap_assumption_term()); + arrangement_disjunction.push_back(m_theoryStrOverlapAssumption_term); overlapAssumptionUsed = true; } } @@ -7292,13 +7293,19 @@ void theory_str::set_up_axioms(expr * ex) { } } +void theory_str::add_theory_assumptions(expr_ref_vector & assumptions) { + TRACE("t_str", tout << "add overlap assumption for theory_str" << std::endl;); + symbol strOverlap("!!TheoryStrOverlapAssumption!!"); + seq_util m_sequtil(get_manager()); + sort * s = get_manager().mk_bool_sort(); + m_theoryStrOverlapAssumption_term = expr_ref(get_manager().mk_const(strOverlap, s), get_manager()); + assumptions.push_back(get_manager().mk_not(m_theoryStrOverlapAssumption_term)); +} + void theory_str::init_search_eh() { ast_manager & m = get_manager(); context & ctx = get_context(); - // safety - SASSERT(ctx.use_theory_str_overlap_assumption()); - TRACE("t_str_detail", tout << "dumping all asserted formulas:" << std::endl; unsigned nFormulas = ctx.get_num_asserted_formulas(); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index a8857de24..3c273d4e2 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -291,6 +291,7 @@ namespace smt { bool avoidLoopCut; bool loopDetected; obj_map > cut_var_map; + expr_ref m_theoryStrOverlapAssumption_term; obj_hashtable variable_set; obj_hashtable internal_variable_set; @@ -627,6 +628,7 @@ namespace smt { virtual theory* mk_fresh(context*) { return alloc(theory_str, get_manager(), m_params); } virtual void init_search_eh(); + virtual void add_theory_assumptions(expr_ref_vector & assumptions); virtual void relevant_eh(app * n); virtual void assign_eh(bool_var v, bool is_true); virtual void push_scope_eh(); From a3f4d58b000f8ef91c5db61f74adf05329686ba3 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Tue, 18 Apr 2017 16:58:56 -0700 Subject: [PATCH 442/562] use lookahead for simplification Signed-off-by: Nikolaj Bjorner --- src/sat/sat_config.cpp | 4 +- src/sat/sat_config.h | 1 + src/sat/sat_local_search.cpp | 42 ++++++----- src/sat/sat_local_search.h | 1 + src/sat/sat_lookahead.h | 135 +++++++++++++++++++++++++++++------ src/sat/sat_params.pyg | 1 + src/sat/sat_scc.cpp | 4 +- src/sat/sat_simplifier.cpp | 8 +++ src/sat/sat_simplifier.h | 6 +- src/sat/sat_solver.cpp | 23 ++++++ src/sat/sat_solver.h | 3 + 11 files changed, 183 insertions(+), 45 deletions(-) diff --git a/src/sat/sat_config.cpp b/src/sat/sat_config.cpp index 4b019c2b7..42c185ee1 100644 --- a/src/sat/sat_config.cpp +++ b/src/sat/sat_config.cpp @@ -37,6 +37,7 @@ namespace sat { m_psm_glue("psm_glue") { m_num_threads = 1; m_local_search = 0; + m_lookahead_search = false; updt_params(p); } @@ -81,7 +82,8 @@ namespace sat { m_max_conflicts = p.max_conflicts(); m_num_threads = p.threads(); m_local_search = p.local_search(); - + m_lookahead_search = p.lookahead_search(); + // These parameters are not exposed m_simplify_mult1 = _p.get_uint("simplify_mult1", 300); m_simplify_mult2 = _p.get_double("simplify_mult2", 1.5); diff --git a/src/sat/sat_config.h b/src/sat/sat_config.h index 313b4ec49..8c10983d2 100644 --- a/src/sat/sat_config.h +++ b/src/sat/sat_config.h @@ -59,6 +59,7 @@ namespace sat { unsigned m_max_conflicts; unsigned m_num_threads; unsigned m_local_search; + bool m_lookahead_search; unsigned m_simplify_mult1; double m_simplify_mult2; diff --git a/src/sat/sat_local_search.cpp b/src/sat/sat_local_search.cpp index c5e7fcf54..5372011ed 100644 --- a/src/sat/sat_local_search.cpp +++ b/src/sat/sat_local_search.cpp @@ -71,9 +71,8 @@ namespace sat { void local_search::init_cur_solution() { for (unsigned v = 0; v < num_vars(); ++v) { // use bias with a small probability - if (m_rand() % 100 < 3) { - //m_vars[v].m_value = ((unsigned)(m_rand() % 100) < m_vars[v].m_bias); - m_vars[v].m_value = (50 < m_vars[v].m_bias); + if (m_rand() % 100 < 2) { + m_vars[v].m_value = ((unsigned)(m_rand() % 100) < m_vars[v].m_bias); } } } @@ -138,20 +137,24 @@ namespace sat { } void local_search::reinit() { - // the following methods does NOT converge for pseudo-boolean - // can try other way to define "worse" and "better" - // the current best noise is below 1000 -#if 0 - if (m_best_unsat_rate > m_last_best_unsat_rate) { - // worse - m_noise -= m_noise * 2 * m_noise_delta; - m_best_unsat_rate *= 1000.0; + + if (!m_is_pb) { + // + // the following methods does NOT converge for pseudo-boolean + // can try other way to define "worse" and "better" + // the current best noise is below 1000 + // + if (m_best_unsat_rate > m_last_best_unsat_rate) { + // worse + m_noise -= m_noise * 2 * m_noise_delta; + m_best_unsat_rate *= 1000.0; + } + else { + // better + m_noise += (10000 - m_noise) * m_noise_delta; + } } - else { - // better - m_noise += (10000 - m_noise) * m_noise_delta; - } -#endif + for (unsigned i = 0; i < m_constraints.size(); ++i) { constraint& c = m_constraints[i]; c.m_slack = c.m_k; @@ -264,7 +267,7 @@ namespace sat { unsigned id = m_constraints.size(); m_constraints.push_back(constraint(k)); for (unsigned i = 0; i < sz; ++i) { - m_vars.reserve(c[i].var() + 1); + m_vars.reserve(c[i].var() + 1); literal t(~c[i]); m_vars[t.var()].m_watch[is_pos(t)].push_back(pbcoeff(id, coeffs[i])); m_constraints.back().push(t); // add coefficient to constraint? @@ -279,6 +282,7 @@ namespace sat { } void local_search::import(solver& s, bool _init) { + m_is_pb = false; m_vars.reset(); m_constraints.reset(); @@ -349,6 +353,7 @@ namespace sat { // = ~c.lit() or (~c.lits() <= n - k) // = k*c.lit() + ~c.lits() <= n // + m_is_pb = true; lits.reset(); coeffs.reset(); for (unsigned j = 0; j < n; ++j) lits.push_back(c[j]), coeffs.push_back(1); @@ -616,8 +621,7 @@ namespace sat { // verify_unsat_stack(); } - void local_search::flip_gsat(bool_var flipvar) - { + void local_search::flip_gsat(bool_var flipvar) { // already changed truth value!!!! m_vars[flipvar].m_value = !cur_solution(flipvar); diff --git a/src/sat/sat_local_search.h b/src/sat/sat_local_search.h index 918f5328d..edce6cc9c 100644 --- a/src/sat/sat_local_search.h +++ b/src/sat/sat_local_search.h @@ -144,6 +144,7 @@ namespace sat { literal_vector m_assumptions; unsigned m_num_non_binary_clauses; + bool m_is_pb; inline bool is_pos(literal t) const { return !t.sign(); } inline bool is_true(bool_var v) const { return cur_solution(v); } diff --git a/src/sat/sat_lookahead.h b/src/sat/sat_lookahead.h index c4f6a4bba..6bc528220 100644 --- a/src/sat/sat_lookahead.h +++ b/src/sat/sat_lookahead.h @@ -20,6 +20,8 @@ Notes: #ifndef _SAT_LOOKAHEAD_H_ #define _SAT_LOOKAHEAD_H_ +#include "sat_elim_eqs.h" + namespace sat { struct pp_prefix { @@ -34,6 +36,9 @@ namespace sat { for (unsigned i = 0; i <= d; ++i) { if (0 != (p.m_prefix & (1ull << i))) out << "1"; else out << "0"; } + if (d < p.m_depth) { + out << " d:" << p.m_depth; + } return out; } @@ -309,6 +314,7 @@ namespace sat { assign(u); return false; } + IF_VERBOSE(3, verbose_stream() << "tc1: " << u << " " << w << "\n";); add_binary(u, w); } } @@ -367,7 +373,8 @@ namespace sat { bool select(unsigned level) { init_pre_selection(level); - unsigned max_num_cand = level == 0 ? m_freevars.size() : m_config.m_level_cand / level; + unsigned level_cand = std::max(m_config.m_level_cand, m_freevars.size() / 50); + unsigned max_num_cand = level == 0 ? m_freevars.size() : level_cand / level; max_num_cand = std::max(m_config.m_min_cutoff, max_num_cand); float sum = 0; @@ -1010,7 +1017,8 @@ namespace sat { m_lits.push_back(lit_info()); m_rating.push_back(0); m_vprefix.push_back(prefix()); - m_freevars.insert(v); + if (!s.was_eliminated(v)) + m_freevars.insert(v); } void init() { @@ -1040,11 +1048,31 @@ namespace sat { } } + copy_clauses(s.m_clauses); + copy_clauses(s.m_learned); + + // copy units + unsigned trail_sz = s.init_trail_size(); + for (unsigned i = 0; i < trail_sz; ++i) { + literal l = s.m_trail[i]; + if (!s.was_eliminated(l.var())) + { + if (s.m_config.m_drat) m_drat.add(l, false); + assign(l); + } + } + propagate(); + m_qhead = m_trail.size(); + TRACE("sat", s.display(tout); display(tout);); + } + + void copy_clauses(clause_vector const& clauses) { // copy clauses - clause_vector::const_iterator it = s.m_clauses.begin(); - clause_vector::const_iterator end = s.m_clauses.end(); - for (; it != end; ++it) { + clause_vector::const_iterator it = clauses.begin(); + clause_vector::const_iterator end = clauses.end(); + for (; it != end; ++it) { clause& c = *(*it); + if (c.was_removed()) continue; clause* c1 = m_cls_allocator.mk_clause(c.size(), c.begin(), false); m_clauses.push_back(c1); attach_clause(*c1); @@ -1053,17 +1081,6 @@ namespace sat { } if (s.m_config.m_drat) m_drat.add(c, false); } - - // copy units - unsigned trail_sz = s.init_trail_size(); - for (unsigned i = 0; i < trail_sz; ++i) { - literal l = s.m_trail[i]; - if (s.m_config.m_drat) m_drat.add(l, false); - assign(l); - } - propagate(); - m_qhead = m_trail.size(); - TRACE("sat", s.display(tout); display(tout);); } // ------------------------------------ @@ -1393,19 +1410,24 @@ namespace sat { TRACE("sat", display_lookahead(tout); ); unsigned base = 2; bool change = true; + bool first = true; while (change && !inconsistent()) { change = false; for (unsigned i = 0; !inconsistent() && i < m_lookahead.size(); ++i) { + s.checkpoint(); literal lit = m_lookahead[i].m_lit; if (is_fixed_at(lit, c_fixed_truth)) continue; unsigned level = base + m_lookahead[i].m_offset; if (m_stamp[lit.var()] >= level) { continue; } + if (scope_lvl() == 1) { + IF_VERBOSE(3, verbose_stream() << scope_lvl() << " " << lit << " binary: " << m_binary_trail.size() << " trail: " << m_trail_lim.back() << "\n";); + } TRACE("sat", tout << "lookahead: " << lit << " @ " << m_lookahead[i].m_offset << "\n";); reset_wnb(lit); push_lookahead1(lit, level); - do_double(lit, base); + if (!first) do_double(lit, base); bool unsat = inconsistent(); pop_lookahead1(lit); if (unsat) { @@ -1424,7 +1446,13 @@ namespace sat { if (c_fixed_truth - 2 * m_lookahead.size() < base) { break; } - base += 2 * m_lookahead.size(); + if (first && !change) { + first = false; + change = true; + } + reset_wnb(); + init_wnb(); + // base += 2 * m_lookahead.size(); } reset_wnb(); TRACE("sat", display_lookahead(tout); ); @@ -1487,6 +1515,7 @@ namespace sat { } bool check_autarky(literal l, unsigned level) { + return false; // no propagations are allowed to reduce clauses. clause_vector::const_iterator it = m_full_watches[l.index()].begin(); clause_vector::const_iterator end = m_full_watches[l.index()].end(); @@ -1568,7 +1597,7 @@ namespace sat { } void do_double(literal l, unsigned& base) { - if (!inconsistent() && scope_lvl() > 0 && dl_enabled(l)) { + if (!inconsistent() && scope_lvl() > 1 && dl_enabled(l)) { if (get_wnb(l) > m_delta_trigger) { if (dl_no_overflow(base)) { ++m_stats.m_double_lookahead_rounds; @@ -1588,6 +1617,7 @@ namespace sat { SASSERT(dl_no_overflow(base)); unsigned dl_truth = base + 2 * m_lookahead.size() * (m_config.m_dl_max_iterations + 1); scoped_level _sl(*this, dl_truth); + IF_VERBOSE(2, verbose_stream() << "double: " << l << "\n";); init_wnb(); assign(l); propagate(); @@ -1769,9 +1799,6 @@ namespace sat { m_drat(s), m_level(2), m_prefix(0) { - m_search_mode = lookahead_mode::searching; - scoped_level _sl(*this, c_fixed_truth); - init(); } ~lookahead() { @@ -1779,9 +1806,73 @@ namespace sat { } lbool check() { + { + m_search_mode = lookahead_mode::searching; + scoped_level _sl(*this, c_fixed_truth); + init(); + } return search(); } + void simplify() { + SASSERT(m_prefix == 0); + SASSERT(m_watches.empty()); + m_search_mode = lookahead_mode::searching; + scoped_level _sl(*this, c_fixed_truth); + init(); + if (inconsistent()) return; + inc_istamp(); + literal l = choose(); + if (inconsistent()) return; + SASSERT(m_trail_lim.empty()); + unsigned num_units = 0; + for (unsigned i = 0; i < m_trail.size(); ++i) { + literal lit = m_trail[i]; + if (s.value(lit) == l_undef && !s.was_eliminated(lit.var())) { + s.m_simplifier.propagate_unit(lit); + ++num_units; + } + } + IF_VERBOSE(1, verbose_stream() << "units found: " << num_units << "\n";); + + s.m_simplifier.subsume(); + m_lookahead.reset(); + } + + void scc() { + SASSERT(m_prefix == 0); + SASSERT(m_watches.empty()); + m_search_mode = lookahead_mode::searching; + scoped_level _sl(*this, c_fixed_truth); + init(); + if (inconsistent()) return; + inc_istamp(); + m_lookahead.reset(); + if (select(0)) { + // extract equivalences + get_scc(); + if (inconsistent()) return; + literal_vector roots; + bool_var_vector to_elim; + for (unsigned i = 0; i < s.num_vars(); ++i) { + roots.push_back(literal(i, false)); + } + for (unsigned i = 0; i < m_candidates.size(); ++i) { + bool_var v = m_candidates[i].m_var; + literal lit = literal(v, false); + literal p = get_parent(lit); + if (p != null_literal && p.var() != v && !s.is_external(v) && !s.was_eliminated(v) && !s.was_eliminated(p.var())) { + to_elim.push_back(v); + roots[v] = p; + } + } + IF_VERBOSE(1, verbose_stream() << "eliminate " << to_elim.size() << " variables\n";); + elim_eqs elim(s); + elim(roots, to_elim); + } + m_lookahead.reset(); + } + std::ostream& display(std::ostream& out) const { out << "Prefix: " << pp_prefix(m_prefix, m_trail_lim.size()) << "\n"; out << "Level: " << m_level << "\n"; diff --git a/src/sat/sat_params.pyg b/src/sat/sat_params.pyg index 045fd803a..ffc699d02 100644 --- a/src/sat/sat_params.pyg +++ b/src/sat/sat_params.pyg @@ -29,4 +29,5 @@ def_module_params('sat', ('cardinality.solver', BOOL, False, 'use cardinality solver'), ('xor.solver', BOOL, False, 'use xor solver'), ('local_search', UINT, 0, 'number of local search threads to find satisfiable solution'), + ('lookahead_search', BOOL, False, 'use lookahead solver') )) diff --git a/src/sat/sat_scc.cpp b/src/sat/sat_scc.cpp index ffbdb31c6..3dfc42f6a 100644 --- a/src/sat/sat_scc.cpp +++ b/src/sat/sat_scc.cpp @@ -76,7 +76,9 @@ namespace sat { lowlink.resize(num_lits, UINT_MAX); in_s.resize(num_lits, false); literal_vector roots; - roots.resize(m_solver.num_vars(), null_literal); + for (unsigned i = 0; i < m_solver.num_vars(); ++i) { + roots.push_back(literal(i, false)); + } unsigned next_index = 0; svector frames; bool_var_vector to_elim; diff --git a/src/sat/sat_simplifier.cpp b/src/sat/sat_simplifier.cpp index fe019427f..8cbedb86b 100644 --- a/src/sat/sat_simplifier.cpp +++ b/src/sat/sat_simplifier.cpp @@ -21,6 +21,7 @@ Revision History: #include"sat_simplifier.h" #include"sat_simplifier_params.hpp" #include"sat_solver.h" +#include"sat_lookahead.h" #include"stopwatch.h" #include"trace.h" @@ -204,6 +205,11 @@ namespace sat { } while (!m_sub_todo.empty()); + if (!learned) { + // perform lookahead simplification + lookahead(s).simplify(); + } + bool vars_eliminated = m_num_elim_vars > old_num_elim_vars; if (m_need_cleanup) { @@ -219,9 +225,11 @@ namespace sat { cleanup_clauses(s.m_learned, true, true, learned_in_use_lists); } } + CASSERT("sat_solver", s.check_invariant()); TRACE("after_simplifier", s.display(tout); tout << "model_converter:\n"; s.m_mc.display(tout);); finalize(); + } /** diff --git a/src/sat/sat_simplifier.h b/src/sat/sat_simplifier.h index 9ee239083..47648cc10 100644 --- a/src/sat/sat_simplifier.h +++ b/src/sat/sat_simplifier.h @@ -130,13 +130,11 @@ namespace sat { bool cleanup_clause(clause & c, bool in_use_list); bool cleanup_clause(literal_vector & c); - void propagate_unit(literal l); void elim_lit(clause & c, literal l); void elim_dup_bins(); bool subsume_with_binaries(); void mark_as_not_learned_core(watch_list & wlist, literal l2); void mark_as_not_learned(literal l1, literal l2); - void subsume(); void cleanup_watches(); void cleanup_clauses(clause_vector & cs, bool learned, bool vars_eliminated, bool in_use_lists); @@ -191,6 +189,10 @@ namespace sat { void collect_statistics(statistics & st) const; void reset_statistics(); + + void propagate_unit(literal l); + void subsume(); + }; }; diff --git a/src/sat/sat_solver.cpp b/src/sat/sat_solver.cpp index d25c8bdee..5ebd661ee 100644 --- a/src/sat/sat_solver.cpp +++ b/src/sat/sat_solver.cpp @@ -22,6 +22,7 @@ Revision History: #include"trace.h" #include"max_cliques.h" #include"scoped_ptr_vector.h" +#include"sat_lookahead.h" // define to update glue during propagation #define UPDATE_GLUE @@ -783,6 +784,9 @@ namespace sat { pop_to_base_level(); IF_VERBOSE(2, verbose_stream() << "(sat.sat-solver)\n";); SASSERT(at_base_lvl()); + if (m_config.m_lookahead_search && num_lits == 0) { + return lookahead_search(); + } if ((m_config.m_num_threads > 1 || m_config.m_local_search > 0) && !m_par) { return check_par(num_lits, lits); } @@ -855,6 +859,20 @@ namespace sat { ERROR_EX }; + lbool solver::lookahead_search() { + lookahead lh(*this); + lbool r = l_undef; + try { + r = lh.check(); + m_model = lh.get_model(); + } + catch (z3_exception&) { + lh.collect_statistics(m_lookahead_stats); + throw; + } + lh.collect_statistics(m_lookahead_stats); + return r; + } lbool solver::check_par(unsigned num_lits, literal const* lits) { scoped_ptr_vector ls; @@ -1295,6 +1313,8 @@ namespace sat { CASSERT("sat_simplify_bug", check_invariant()); } + lookahead(*this).scc(); + sort_watch_lits(); CASSERT("sat_simplify_bug", check_invariant()); @@ -2762,6 +2782,7 @@ namespace sat { m_asymm_branch.collect_statistics(st); m_probing.collect_statistics(st); if (m_ext) m_ext->collect_statistics(st); + st.copy(m_lookahead_stats); } void solver::reset_statistics() { @@ -2770,6 +2791,7 @@ namespace sat { m_simplifier.reset_statistics(); m_asymm_branch.reset_statistics(); m_probing.reset_statistics(); + m_lookahead_stats.reset(); } // ----------------------- @@ -3605,6 +3627,7 @@ namespace sat { if (m_solver.m_num_frozen > 0) out << " :frozen " << m_solver.m_num_frozen; } + out << " :units " << m_solver.init_trail_size(); out << " :gc-clause " << m_solver.m_stats.m_gc_clause; out << mem_stat(); } diff --git a/src/sat/sat_solver.h b/src/sat/sat_solver.h index c35a0296c..1bf393696 100644 --- a/src/sat/sat_solver.h +++ b/src/sat/sat_solver.h @@ -141,6 +141,8 @@ namespace sat { unsigned m_par_num_vars; bool m_par_syncing_clauses; + statistics m_lookahead_stats; + void del_clauses(clause * const * begin, clause * const * end); friend class integrity_checker; @@ -346,6 +348,7 @@ namespace sat { void sort_watch_lits(); void exchange_par(); lbool check_par(unsigned num_lits, literal const* lits); + lbool lookahead_search(); // ----------------------- // From a02a7f44430475e0ec2f09ac64b94598a0c6448e Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Wed, 19 Apr 2017 13:04:04 +0100 Subject: [PATCH 443/562] Whitespace --- src/smt/old_interval.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/smt/old_interval.cpp b/src/smt/old_interval.cpp index 616b74ed6..da03bc03e 100644 --- a/src/smt/old_interval.cpp +++ b/src/smt/old_interval.cpp @@ -81,7 +81,7 @@ ext_numeral & ext_numeral::operator*=(ext_numeral const & other) { m_value.reset(); return *this; } - + if (is_infinite() || other.is_infinite()) { if (sign() == other.sign()) m_kind = PLUS_INFINITY; @@ -203,7 +203,7 @@ interval::interval(v_dependency_manager & m, rational const & val, v_dependency m_lower_dep(l_dep), m_upper_dep(u_dep) { } - + /** \brief Create intervals (-oo, val], (-oo, val), [val, oo), (val, oo) */ @@ -271,8 +271,8 @@ interval & interval::operator-=(interval const & other) { return operator+=(tmp); } -v_dependency * interval::join(v_dependency * d1, v_dependency * d2, v_dependency * d3, v_dependency * d4) { - return m_manager.mk_join(m_manager.mk_join(d1, d2), m_manager.mk_join(d3,d4)); +v_dependency * interval::join(v_dependency * d1, v_dependency * d2, v_dependency * d3, v_dependency * d4) { + return m_manager.mk_join(m_manager.mk_join(d1, d2), m_manager.mk_join(d3,d4)); } /** @@ -318,7 +318,7 @@ interval & interval::operator*=(interval const & other) { v_dependency * d_d = other.m_upper_dep; TRACE("interval_bug", tout << "operator*= " << *this << " " << other << "\n";); - + if (is_N()) { if (other.is_N()) { // x <= b <= 0, y <= d <= 0 --> b*d <= x*y @@ -452,7 +452,7 @@ interval & interval::operator*=(interval const & other) { m_upper_dep = m_upper.is_infinite() ? 0 : join(b_d, d_d, a_d); } else { - // 0 <= a <= x, 0 <= c <= y --> a*c <= x*y + // 0 <= a <= x, 0 <= c <= y --> a*c <= x*y // x <= b, y <= d --> x*y <= b*d (uses the fact that x is pos (a is not negative) or y is pos (c is not negative)) TRACE("interval_bug", tout << "(P, P)\n";); SASSERT(other.is_P()); @@ -467,7 +467,7 @@ interval & interval::operator*=(interval const & other) { } } TRACE("interval_bug", tout << "operator*= result: " << *this << "\n";); - CTRACE("interval", !(!(contains_zero1 || contains_zero2) || contains_zero()), + CTRACE("interval", !(!(contains_zero1 || contains_zero2) || contains_zero()), tout << "contains_zero1: " << contains_zero1 << ", contains_zero2: " << contains_zero2 << ", contains_zero(): " << contains_zero() << "\n";); SASSERT(!(contains_zero1 || contains_zero2) || contains_zero()); return *this; @@ -482,7 +482,7 @@ bool interval::contains_zero() const { tout << "m_upper.is_zero: " << m_upper.is_zero() << "\n"; tout << "m_upper_open: " << m_upper_open << "\n"; tout << "result: " << ((m_lower.is_neg() || (m_lower.is_zero() && !m_lower_open)) && (m_upper.is_pos() || (m_upper.is_zero() && !m_upper_open))) << "\n";); - return + return (m_lower.is_neg() || (m_lower.is_zero() && !m_lower_open)) && (m_upper.is_pos() || (m_upper.is_zero() && !m_upper_open)); } @@ -510,7 +510,7 @@ interval & interval::inv() { ext_numeral new_upper; if (m_lower.is_zero()) { SASSERT(m_lower_open); - ext_numeral plus_infinity(true); + ext_numeral plus_infinity(true); new_upper = plus_infinity; } else { @@ -595,7 +595,7 @@ void interval::expt(unsigned n) { else if (m_upper.is_neg()) { // [l, u]^n = [u^n, l^n] if u < 0 // a <= x <= b < 0 --> x^n <= a^n (use lower and upper bound -- need the fact that x is negative) - // x <= b < 0 --> b^n <= x^n + // x <= b < 0 --> b^n <= x^n std::swap(m_lower, m_upper); std::swap(m_lower_open, m_upper_open); std::swap(m_lower_dep, m_upper_dep); @@ -614,7 +614,7 @@ void interval::expt(unsigned n) { m_upper = m_lower; m_upper_open = m_lower_open; } - m_upper_dep = m_upper.is_infinite() ? 0 : m_manager.mk_join(m_lower_dep, m_upper_dep); + m_upper_dep = m_upper.is_infinite() ? 0 : m_manager.mk_join(m_lower_dep, m_upper_dep); m_lower = ext_numeral(0); m_lower_open = false; m_lower_dep = 0; From 0a0b17540f307e041a791145c2c94ab57a7b6907 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Wed, 19 Apr 2017 13:07:04 +0100 Subject: [PATCH 444/562] Added rlimit.inc() for expensive interval exponentiation in the non-linear arithmetic theory. --- src/smt/theory_arith_nl.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/smt/theory_arith_nl.h b/src/smt/theory_arith_nl.h index 52a117cd5..c8729ea36 100644 --- a/src/smt/theory_arith_nl.h +++ b/src/smt/theory_arith_nl.h @@ -339,8 +339,13 @@ namespace smt { tout << mk_pp(var, get_manager()) << "\n"; tout << "power " << power << ": " << expt(i, power) << "\n"; display_interval(tout << "target before: ", target); tout << "\n";); + i.expt(power); target *= i; + + get_manager().limit().inc((target.is_lower_open() || target.minus_infinity()) ? 1 : target.get_lower_value().bitsize()); + get_manager().limit().inc((target.is_upper_open() || target.plus_infinity()) ? 1 : target.get_upper_value().bitsize()); + TRACE("non_linear", display_interval(tout << "target after: ", target); tout << "\n";); } From e65f106a83177bb8e2becfb1d2bb41bd6936cb44 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Wed, 19 Apr 2017 08:59:49 -0700 Subject: [PATCH 445/562] ccc Signed-off-by: Nikolaj Bjorner --- contrib/cmake/src/sat/CMakeLists.txt | 1 + src/sat/card_extension.cpp | 90 ++++++++- src/sat/card_extension.h | 45 ++++- src/sat/sat_ccc.cpp | 271 +++++++++++++++++++++++++++ src/sat/sat_ccc.h | 66 +++++++ src/sat/sat_config.cpp | 1 + src/sat/sat_config.h | 3 +- src/sat/sat_lookahead.h | 38 ++-- src/sat/sat_params.pyg | 3 +- src/sat/sat_solver.cpp | 21 ++- src/sat/sat_solver.h | 4 +- src/util/memory_manager.cpp | 1 + src/util/queue.h | 61 ++++++ 13 files changed, 573 insertions(+), 32 deletions(-) create mode 100644 src/sat/sat_ccc.cpp create mode 100644 src/sat/sat_ccc.h create mode 100644 src/util/queue.h diff --git a/contrib/cmake/src/sat/CMakeLists.txt b/contrib/cmake/src/sat/CMakeLists.txt index 5494049d7..fcfe53e02 100644 --- a/contrib/cmake/src/sat/CMakeLists.txt +++ b/contrib/cmake/src/sat/CMakeLists.txt @@ -3,6 +3,7 @@ z3_add_component(sat card_extension.cpp dimacs.cpp sat_asymm_branch.cpp + sat_ccc.cpp sat_clause.cpp sat_clause_set.cpp sat_clause_use_list.cpp diff --git a/src/sat/card_extension.cpp b/src/sat/card_extension.cpp index cb05e5d61..7d1c27abb 100644 --- a/src/sat/card_extension.cpp +++ b/src/sat/card_extension.cpp @@ -26,8 +26,7 @@ namespace sat { m_index(index), m_lit(lit), m_k(k), - m_size(lits.size()) - { + m_size(lits.size()) { for (unsigned i = 0; i < lits.size(); ++i) { m_lits[i] = lits[i]; } @@ -42,6 +41,27 @@ namespace sat { SASSERT(m_size >= m_k && m_k > 0); } + card_extension::pb::pb(unsigned index, literal lit, svector const& wlits, unsigned k): + m_index(index), + m_lit(lit), + m_k(k), + m_size(wlits.size()) { + for (unsigned i = 0; i < wlits.size(); ++i) { + m_wlits[i] = wlits[i]; + } + } + + void card_extension::pb::negate() { + m_lit.neg(); + unsigned w = 0; + for (unsigned i = 0; i < m_size; ++i) { + m_wlits[i].second.neg(); + w += m_wlits[i].first; + } + m_k = w - m_k + 1; + SASSERT(w >= m_k && m_k > 0); + } + card_extension::xor::xor(unsigned index, literal lit, literal_vector const& lits): m_index(index), m_lit(lit), @@ -191,6 +211,15 @@ namespace sat { SASSERT(s().inconsistent()); } + // pb: + void card_extension::init_watch(pb& p, bool is_true) { + NOT_IMPLEMENTED_YET(); + } + + + + + // xor: void card_extension::clear_watch(xor& x) { unwatch_literal(x[0], &x); unwatch_literal(x[1], &x); @@ -510,7 +539,7 @@ namespace sat { process_card(c, offset); ++m_stats.m_num_card_resolves; } - else { + else if (is_xor_index(index)) { // jus.push_back(js); m_lemma.reset(); m_bound += offset; @@ -521,6 +550,12 @@ namespace sat { } ++m_stats.m_num_xor_resolves; } + else if (is_pb_index(index)) { + NOT_IMPLEMENTED_YET(); + } + else { + UNREACHABLE(); + } break; } default: @@ -758,7 +793,7 @@ namespace sat { } void card_extension::add_at_least(bool_var v, literal_vector const& lits, unsigned k) { - unsigned index = 2*m_cards.size(); + unsigned index = 4*m_cards.size(); literal lit = v == null_bool_var ? null_literal : literal(v, false); card* c = new (memory::allocate(card::get_obj_size(lits.size()))) card(index, lit, lits, k); m_cards.push_back(c); @@ -774,9 +809,26 @@ namespace sat { } } + void card_extension::add_pb_ge(bool_var v, svector const& wlits, unsigned k) { + unsigned index = 4*m_pb.size() + 0x11; + literal lit = v == null_bool_var ? null_literal : literal(v, false); + pb* p = new (memory::allocate(pb::get_obj_size(wlits.size()))) pb(index, lit, wlits, k); + m_pb.push_back(p); + if (v == null_bool_var) { + init_watch(*p, true); + m_pb_axioms.push_back(p); + } + else { + init_watch(v); + m_var_infos[v].m_pb = p; + m_var_trail.push_back(v); + } + } + + void card_extension::add_xor(bool_var v, literal_vector const& lits) { m_has_xor = true; - unsigned index = 2*m_xors.size()+1; + unsigned index = 4*m_xors.size() + 0x01; xor* x = new (memory::allocate(xor::get_obj_size(lits.size()))) xor(index, literal(v, false), lits); m_xors.push_back(x); init_watch(v); @@ -819,7 +871,7 @@ namespace sat { unsigned level = lvl(l); bool_var v = l.var(); SASSERT(js.get_kind() == justification::EXT_JUSTIFICATION); - SASSERT(!is_card_index(js.get_ext_justification_idx())); + SASSERT(is_xor_index(js.get_ext_justification_idx())); TRACE("sat", tout << l << ": " << js << "\n"; tout << s().m_trail << "\n";); unsigned num_marks = 0; @@ -828,7 +880,7 @@ namespace sat { ++count; if (js.get_kind() == justification::EXT_JUSTIFICATION) { unsigned idx = js.get_ext_justification_idx(); - if (is_card_index(idx)) { + if (!is_xor_index(idx)) { r.push_back(l); } else { @@ -912,7 +964,7 @@ namespace sat { r.push_back(~c[i]); } } - else { + else if (is_xor_index(idx)) { xor& x = index2xor(idx); if (x.lit() != null_literal) r.push_back(x.lit()); TRACE("sat", display(tout << l << " ", x, true);); @@ -931,6 +983,12 @@ namespace sat { r.push_back(value(x[i]) == l_true ? x[i] : ~x[i]); } } + else if (is_pb_index(idx)) { + NOT_IMPLEMENTED_YET(); + } + else { + UNREACHABLE(); + } } @@ -1281,13 +1339,19 @@ namespace sat { } out << ">= " << c.k(); } - else { + else if (is_xor_index(idx)) { xor& x = index2xor(idx); out << "xor " << x.lit() << ": "; for (unsigned i = 0; i < x.size(); ++i) { out << x[i] << " "; } } + else if (is_pb_index(idx)) { + NOT_IMPLEMENTED_YET(); + } + else { + UNREACHABLE(); + } return out; } @@ -1382,7 +1446,7 @@ namespace sat { } if (c.lit() != null_literal) p.push(~c.lit(), offset*c.k()); } - else { + else if (is_xor_index(index)) { literal_vector ls; get_antecedents(lit, index, ls); p.reset(offset); @@ -1392,6 +1456,12 @@ namespace sat { literal lxor = index2xor(index).lit(); if (lxor != null_literal) p.push(~lxor, offset); } + else if (is_pb_index(index)) { + NOT_IMPLEMENTED_YET(); + } + else { + UNREACHABLE(); + } break; } default: diff --git a/src/sat/card_extension.h b/src/sat/card_extension.h index 0d649a97c..6f8b6d120 100644 --- a/src/sat/card_extension.h +++ b/src/sat/card_extension.h @@ -58,6 +58,26 @@ namespace sat { void swap(unsigned i, unsigned j) { std::swap(m_lits[i], m_lits[j]); } void negate(); }; + + typedef std::pair wliteral; + + class pb { + unsigned m_index; + literal m_lit; + unsigned m_k; + unsigned m_size; + wliteral m_wlits[0]; + public: + static size_t get_obj_size(unsigned num_lits) { return sizeof(card) + num_lits * sizeof(wliteral); } + pb(unsigned index, literal lit, svector const& wlits, unsigned k); + unsigned index() const { return m_index; } + literal lit() const { return m_lit; } + wliteral operator[](unsigned i) const { return m_wlits[i]; } + unsigned k() const { return m_k; } + unsigned size() const { return m_size; } + void swap(unsigned i, unsigned j) { std::swap(m_wlits[i], m_wlits[j]); } + void negate(); + }; class xor { unsigned m_index; @@ -85,20 +105,28 @@ namespace sat { typedef ptr_vector card_watch; typedef ptr_vector xor_watch; + typedef ptr_vector pb_watch; struct var_info { card_watch* m_card_watch[2]; + pb_watch* m_pb_watch[2]; xor_watch* m_xor_watch; card* m_card; + pb* m_pb; xor* m_xor; - var_info(): m_xor_watch(0), m_card(0), m_xor(0) { + var_info(): m_xor_watch(0), m_card(0), m_xor(0), m_pb(0) { m_card_watch[0] = 0; m_card_watch[1] = 0; + m_pb_watch[0] = 0; + m_pb_watch[1] = 0; } void reset() { dealloc(m_card); dealloc(m_xor); + dealloc(m_pb); dealloc(card_extension::set_tag_non_empty(m_card_watch[0])); dealloc(card_extension::set_tag_non_empty(m_card_watch[1])); + dealloc(card_extension::set_tag_non_empty(m_pb_watch[0])); + dealloc(card_extension::set_tag_non_empty(m_pb_watch[1])); dealloc(card_extension::set_tag_non_empty(m_xor_watch)); } }; @@ -125,8 +153,10 @@ namespace sat { ptr_vector m_cards; ptr_vector m_xors; + ptr_vector m_pb; scoped_ptr_vector m_card_axioms; + scoped_ptr_vector m_pb_axioms; // watch literals svector m_var_infos; @@ -173,11 +203,17 @@ namespace sat { lbool add_assign(xor& x, literal alit); void asserted_xor(literal l, ptr_vector* xors, xor* x); - bool is_card_index(unsigned idx) const { return 0 == (idx & 0x1); } - card& index2card(unsigned idx) const { SASSERT(is_card_index(idx)); return *m_cards[idx >> 1]; } - xor& index2xor(unsigned idx) const { SASSERT(!is_card_index(idx)); return *m_xors[idx >> 1]; } + bool is_card_index(unsigned idx) const { return 0x00 == (idx & 0x11); } + bool is_xor_index(unsigned idx) const { return 0x01 == (idx & 0x11); } + bool is_pb_index(unsigned idx) const { return 0x11 == (idx & 0x11); } + card& index2card(unsigned idx) const { SASSERT(is_card_index(idx)); return *m_cards[idx >> 2]; } + xor& index2xor(unsigned idx) const { SASSERT(!is_card_index(idx)); return *m_xors[idx >> 2]; } + pb& index2pb(unsigned idx) const { SASSERT(is_pb_index(idx)); return *m_pb[idx >> 2]; } + void get_xor_antecedents(literal l, unsigned inddex, justification js, literal_vector& r); + void init_watch(pb& p, bool is_true); + template bool remove(ptr_vector& ts, T* t) { @@ -233,6 +269,7 @@ namespace sat { virtual ~card_extension(); virtual void set_solver(solver* s) { m_solver = s; } void add_at_least(bool_var v, literal_vector const& lits, unsigned k); + void add_pb_ge(bool_var v, svector const& wlits, unsigned k); void add_xor(bool_var v, literal_vector const& lits); virtual void propagate(literal l, ext_constraint_idx idx, bool & keep); virtual bool resolve_conflict(); diff --git a/src/sat/sat_ccc.cpp b/src/sat/sat_ccc.cpp new file mode 100644 index 000000000..70defb17d --- /dev/null +++ b/src/sat/sat_ccc.cpp @@ -0,0 +1,271 @@ +/*++ +Copyright (c) 2017 Microsoft Corporation + +Module Name: + + sat_ccc.cpp + +Abstract: + + A variant of Concurrent Cube and Conquer + +Author: + + Nikolaj Bjorner (nbjorner) 2017-4-17 + +Notes: + +--*/ + +#include "sat_solver.h" +#include "sat_lookahead.h" +#include "sat_ccc.h" + +using namespace sat; + +lbool ccc::cube() { + unsigned branch_id = 0; + unsigned_vector id_trail; + + lookahead lh(s); + lh.init_search(); + lh.m_model.reset(); + + lookahead::scoped_level _sl(lh, lh.c_fixed_truth); + literal_vector trail; + lh.m_search_mode = lookahead_mode::searching; + while (!m_cancel) { + // remove old branch ids from id_trail. + while (id_trail.size() > trail.size()) { + id_trail.pop_back(); + } + TRACE("sat", lh.display(tout);); + lh.inc_istamp(); + s.checkpoint(); + if (lh.inconsistent()) { + if (!lh.backtrack(trail)) return l_false; + continue; + } + + // check if CDCL solver got ahead. + bool repeat = false; + #pragma omp critical (ccc_solved) + { + if (!m_solved.empty()) { + unsigned solved_id = m_solved.top(); + if (id_trail.contains(solved_id)) { + lh.set_conflict(); + } + else { + m_solved.pop(); + } + repeat = true; + } + } + if (repeat) continue; + + ++branch_id; + if (!trail.empty()) { + #pragma omp critical (ccc_decisions) + { + m_decisions.push(decision(branch_id, trail.size()-1, trail.back())); + } + } + + literal l = lh.choose(); + if (lh.inconsistent()) { + if (!lh.backtrack(trail)) return l_false; + continue; + } + if (l == null_literal) { + m_model = lh.get_model(); + return l_true; + } + + // update trail and set of ids + id_trail.push_back(branch_id); + trail.push_back(l); + SASSERT(id_trail.size() == trail.size()); + + TRACE("sat", tout << "choose: " << l << " " << trail << "\n";); + ++lh.m_stats.m_decisions; + IF_VERBOSE(1, verbose_stream() << "select " << pp_prefix(lh.m_prefix, lh.m_trail_lim.size()) << ": " << l << " " << lh.m_trail.size() << "\n";); + lh.push(l, lh.c_fixed_truth); + SASSERT(lh.inconsistent() || !lh.is_unsat()); + } + return l_undef; +} + +lbool ccc::conquer(solver& s) { + try { + if (s.inconsistent()) return l_false; + s.init_search(); + s.propagate(false); + if (s.inconsistent()) return l_false; + s.init_assumptions(0, 0); + s.propagate(false); + if (s.check_inconsistent()) return l_false; + s.cleanup(); + s.simplify_problem(); + if (s.check_inconsistent()) return l_false; + + unsigned_vector ids; + + while (true) { + SASSERT(!s.inconsistent()); + + lbool r = bounded_search(s, ids); + if (r != l_undef) + return r; + + if (s.m_conflicts > s.m_config.m_max_conflicts) { + IF_VERBOSE(SAT_VB_LVL, verbose_stream() << "(sat \"abort: max-conflicts = " << s.m_conflicts << "\")\n";); + return l_undef; + } + + s.restart(); + s.simplify_problem(); + if (s.check_inconsistent()) return l_false; + s.gc(); + } + } + catch (solver::abort_solver) { + return l_undef; + } +} + +lbool ccc::bounded_search(solver& s, unsigned_vector& ids) { + decision d; + + while (true) { + s.checkpoint(); + bool done = false; + while (!done) { + lbool is_sat = s.propagate_and_backjump_step(done); + if (is_sat != l_true) return is_sat; + } + + if (s.m_scope_lvl < ids.size()) { + while (ids.size() > s.m_scope_lvl + 1) ids.pop_back(); + unsigned id = ids.back(); + ids.pop_back(); + #pragma omp critical (ccc_solved) + { + m_solved.push(id); + } + } + + s.gc(); + + bool cube_decision = false; + #pragma omp critical (ccc_decisions) + { + if (!m_decisions.empty()) { + d = m_decisions.pop(); + cube_decision = true; + } + } + if (cube_decision) { + if (d.m_depth > ids.size()) continue; + ids.push_back(d.m_id); + s.pop_reinit(s.m_scope_lvl - d.m_depth); // TBD: check alignment of scopes + s.push(); + s.assign(d.m_last, justification()); + } + else if (!s.decide()) { + lbool is_sat = s.final_check(); + if (is_sat != l_undef) { + return is_sat; + } + } + } +} + + +lbool ccc::search() { + enum par_exception_kind { + DEFAULT_EX, + ERROR_EX + }; + + m_cancel = false; + + scoped_limits scoped_rlimit(s.rlimit()); + vector limits; + ptr_vector solvers; + int finished_id = -1; + std::string ex_msg; + par_exception_kind ex_kind; + unsigned error_code = 0; + lbool result = l_undef; + bool canceled = false; + + int num_threads = s.m_config.m_num_threads + 1; + for (int i = 1; i < num_threads; ++i) { + limits.push_back(reslimit()); + } + + for (int i = 1; i < num_threads; ++i) { + s.m_params.set_uint("random_seed", s.m_rand()); + solvers[i] = alloc(sat::solver, s.m_params, limits[i]); + solvers[i]->copy(s); + scoped_rlimit.push_child(&solvers[i]->rlimit()); + } + + #pragma omp parallel for + for (int i = 0; i < num_threads; ++i) { + try { + lbool r = l_undef; + if (i == 0) { + r = cube(); + } + else { + r = conquer(*solvers[i-1]); + } + bool first = false; + #pragma omp critical (par_solver) + { + if (finished_id == -1) { + finished_id = i; + first = true; + result = r; + } + } + if (first) { + for (unsigned j = 0; j < solvers.size(); ++j) { + solvers[j]->rlimit().cancel(); + } + // cancel lookahead solver: + m_cancel = true; + } + } + catch (z3_error & err) { + error_code = err.error_code(); + ex_kind = ERROR_EX; + } + catch (z3_exception & ex) { + ex_msg = ex.msg(); + ex_kind = DEFAULT_EX; + } + } + + if (finished_id > 0 && result == l_true) { + // set model from auxiliary solver + m_model = solvers[finished_id - 1]->get_model(); + } + + for (unsigned i = 0; i < solvers.size(); ++i) { + dealloc(solvers[i]); + } + + if (finished_id == -1) { + switch (ex_kind) { + case ERROR_EX: throw z3_error(error_code); + default: throw default_exception(ex_msg.c_str()); + } + } + + + return result; +} + diff --git a/src/sat/sat_ccc.h b/src/sat/sat_ccc.h new file mode 100644 index 000000000..d5357090a --- /dev/null +++ b/src/sat/sat_ccc.h @@ -0,0 +1,66 @@ +/*++ +Copyright (c) 2017 Microsoft Corporation + +Module Name: + + sat_ccc.h + +Abstract: + + A variant of Concurrent Cube and Conquer + +Author: + + Nikolaj Bjorner (nbjorner) 2017-4-17 + +Notes: + +--*/ +#ifndef _SAT_CCC_H_ +#define _SAT_CCC_H_ + +#include "queue.h" + +namespace sat { + + class ccc { + struct decision { + unsigned m_id; + unsigned m_depth; + literal m_last; + decision(unsigned id, unsigned d, literal last): + m_id(id), m_depth(d), m_last(last) {} + decision(): m_id(0), m_depth(0), m_last(null_literal) {} + }; + + solver& s; + queue m_solved; + queue m_decisions; + model m_model; + volatile bool m_cancel; + + struct config { + config() { + } + }; + + struct stats { + stats() { reset(); } + void reset() { memset(this, 0, sizeof(*this)); } + }; + + lbool conquer(solver& s); + lbool bounded_search(solver& s, unsigned_vector& ids); + + lbool cube(); + + public: + ccc(solver& s): s(s) {} + + lbool search(); + + }; +} + +#endif + diff --git a/src/sat/sat_config.cpp b/src/sat/sat_config.cpp index 42c185ee1..175c34690 100644 --- a/src/sat/sat_config.cpp +++ b/src/sat/sat_config.cpp @@ -82,6 +82,7 @@ namespace sat { m_max_conflicts = p.max_conflicts(); m_num_threads = p.threads(); m_local_search = p.local_search(); + m_local_search_threads = p.local_search_threads(); m_lookahead_search = p.lookahead_search(); // These parameters are not exposed diff --git a/src/sat/sat_config.h b/src/sat/sat_config.h index 8c10983d2..fb125c529 100644 --- a/src/sat/sat_config.h +++ b/src/sat/sat_config.h @@ -58,7 +58,8 @@ namespace sat { unsigned m_burst_search; unsigned m_max_conflicts; unsigned m_num_threads; - unsigned m_local_search; + unsigned m_local_search_threads; + bool m_local_search; bool m_lookahead_search; unsigned m_simplify_mult1; diff --git a/src/sat/sat_lookahead.h b/src/sat/sat_lookahead.h index 6bc528220..a801cdad7 100644 --- a/src/sat/sat_lookahead.h +++ b/src/sat/sat_lookahead.h @@ -61,6 +61,8 @@ namespace sat { class lookahead { solver& s; + friend class ccc; + struct config { double m_dl_success; float m_alpha; @@ -70,6 +72,7 @@ namespace sat { unsigned m_level_cand; float m_delta_rho; unsigned m_dl_max_iterations; + unsigned m_tc1_limit; config() { m_max_hlevel = 50; @@ -79,6 +82,7 @@ namespace sat { m_level_cand = 600; m_delta_rho = (float)0.9995; m_dl_max_iterations = 32; + m_tc1_limit = 10000000; } }; @@ -126,6 +130,8 @@ namespace sat { vector m_binary; // literal: binary clauses unsigned_vector m_binary_trail; // trail of added binary clauses unsigned_vector m_binary_trail_lim; + unsigned m_num_tc1; + unsigned_vector m_num_tc1_lim; unsigned m_qhead; // propagation queue head unsigned_vector m_qhead_lim; clause_vector m_clauses; // non-binary clauses @@ -314,8 +320,11 @@ namespace sat { assign(u); return false; } - IF_VERBOSE(3, verbose_stream() << "tc1: " << u << " " << w << "\n";); - add_binary(u, w); + if (m_num_tc1 < m_config.m_tc1_limit) { + ++m_num_tc1; + IF_VERBOSE(3, verbose_stream() << "tc1: " << u << " " << w << "\n";); + add_binary(u, w); + } } } return true; @@ -1055,11 +1064,10 @@ namespace sat { unsigned trail_sz = s.init_trail_size(); for (unsigned i = 0; i < trail_sz; ++i) { literal l = s.m_trail[i]; - if (!s.was_eliminated(l.var())) - { - if (s.m_config.m_drat) m_drat.add(l, false); - assign(l); - } + if (!s.was_eliminated(l.var())) { + if (s.m_config.m_drat) m_drat.add(l, false); + assign(l); + } } propagate(); m_qhead = m_trail.size(); @@ -1090,6 +1098,7 @@ namespace sat { SASSERT(m_search_mode == lookahead_mode::searching); m_binary_trail_lim.push_back(m_binary_trail.size()); m_trail_lim.push_back(m_trail.size()); + m_num_tc1_lim.push_back(m_num_tc1); m_retired_clause_lim.push_back(m_retired_clauses.size()); m_retired_ternary_lim.push_back(m_retired_ternary.size()); m_qhead_lim.push_back(m_qhead); @@ -1116,6 +1125,9 @@ namespace sat { } m_trail.shrink(old_sz); // reset assignment. m_trail_lim.pop_back(); + + m_num_tc1 = m_num_tc1_lim.back(); + m_num_tc1_lim.pop_back(); // unretire clauses old_sz = m_retired_clause_lim.back(); @@ -1792,11 +1804,17 @@ namespace sat { return out; } + void init_search() { + m_search_mode = lookahead_mode::searching; + scoped_level _sl(*this, c_fixed_truth); + init(); + } public: lookahead(solver& s) : s(s), m_drat(s), + m_num_tc1(0), m_level(2), m_prefix(0) { } @@ -1806,11 +1824,7 @@ namespace sat { } lbool check() { - { - m_search_mode = lookahead_mode::searching; - scoped_level _sl(*this, c_fixed_truth); - init(); - } + init_search(); return search(); } diff --git a/src/sat/sat_params.pyg b/src/sat/sat_params.pyg index ffc699d02..a69de0772 100644 --- a/src/sat/sat_params.pyg +++ b/src/sat/sat_params.pyg @@ -28,6 +28,7 @@ def_module_params('sat', ('drat.check', BOOL, False, 'build up internal proof and check'), ('cardinality.solver', BOOL, False, 'use cardinality solver'), ('xor.solver', BOOL, False, 'use xor solver'), - ('local_search', UINT, 0, 'number of local search threads to find satisfiable solution'), + ('local_search_threads', UINT, 0, 'number of local search threads to find satisfiable solution'), + ('local_search', BOOL, False, 'use local search instead of CDCL'), ('lookahead_search', BOOL, False, 'use lookahead solver') )) diff --git a/src/sat/sat_solver.cpp b/src/sat/sat_solver.cpp index 5ebd661ee..8eaba2734 100644 --- a/src/sat/sat_solver.cpp +++ b/src/sat/sat_solver.cpp @@ -787,7 +787,10 @@ namespace sat { if (m_config.m_lookahead_search && num_lits == 0) { return lookahead_search(); } - if ((m_config.m_num_threads > 1 || m_config.m_local_search > 0) && !m_par) { + if (m_config.m_local_search) { + return do_local_search(num_lits, lits); + } + if ((m_config.m_num_threads > 1 || m_config.m_local_search_threads > 0) && !m_par) { return check_par(num_lits, lits); } flet _searching(m_searching, true); @@ -859,6 +862,18 @@ namespace sat { ERROR_EX }; + lbool solver::do_local_search(unsigned num_lits, literal const* lits) { + scoped_limits scoped_rl(rlimit()); + local_search srch; + srch.config().set_seed(m_config.m_random_seed); + srch.import(*this, false); + scoped_rl.push_child(&srch.rlimit()); + lbool r = srch.check(num_lits, lits, 0); + m_model = srch.get_model(); + // srch.collect_statistics(m_lookahead_stats); + return r; + } + lbool solver::lookahead_search() { lookahead lh(*this); lbool r = l_undef; @@ -876,9 +891,9 @@ namespace sat { lbool solver::check_par(unsigned num_lits, literal const* lits) { scoped_ptr_vector ls; - int num_threads = static_cast(m_config.m_num_threads + m_config.m_local_search); + int num_threads = static_cast(m_config.m_num_threads + m_config.m_local_search_threads); int num_extra_solvers = m_config.m_num_threads - 1; - int num_local_search = static_cast(m_config.m_local_search); + int num_local_search = static_cast(m_config.m_local_search_threads); for (int i = 0; i < num_local_search; ++i) { local_search* l = alloc(local_search); l->config().set_seed(m_config.m_random_seed + i); diff --git a/src/sat/sat_solver.h b/src/sat/sat_solver.h index 1bf393696..b4b7f82fe 100644 --- a/src/sat/sat_solver.h +++ b/src/sat/sat_solver.h @@ -160,6 +160,7 @@ namespace sat { friend class lookahead; friend class local_search; friend struct mk_stat; + friend class ccc; public: solver(params_ref const & p, reslimit& l); ~solver(); @@ -349,6 +350,7 @@ namespace sat { void exchange_par(); lbool check_par(unsigned num_lits, literal const* lits); lbool lookahead_search(); + lbool do_local_search(unsigned num_lits, literal const* lits); // ----------------------- // @@ -465,7 +467,7 @@ namespace sat { lbool get_consequences(literal_vector const& assms, bool_var_vector const& vars, vector& conseq); // initialize and retrieve local search. - local_search& init_local_search(); + // local_search& init_local_search(); private: diff --git a/src/util/memory_manager.cpp b/src/util/memory_manager.cpp index 6bd4ec64f..c727a8af3 100644 --- a/src/util/memory_manager.cpp +++ b/src/util/memory_manager.cpp @@ -57,6 +57,7 @@ static void throw_out_of_memory() { { g_memory_out_of_memory = true; } + if (g_exit_when_out_of_memory) { std::cerr << g_out_of_memory_msg << "\n"; exit(ERR_MEMOUT); diff --git a/src/util/queue.h b/src/util/queue.h new file mode 100644 index 000000000..4b85f53f0 --- /dev/null +++ b/src/util/queue.h @@ -0,0 +1,61 @@ +/*++ +Copyright (c) 2017 Microsoft Corporation + +Module Name: + + queue.h + +Abstract: + + Generic queue. + +Author: + + Nikolaj Bjorner (nbjorner) 2017-4-17 + +Notes: + +--*/ +#ifndef _QUEUE_H_ +#define _QUEUE_H_ + +#include "vector.h" + +template +class queue { + vector m_elems; + unsigned m_head; + unsigned m_capacity; + +public: + + queue(): m_head(0), m_capacity(0) {} + + void push(T const& t) { m_elems.push_back(t); } + + bool empty() const { + return m_head == m_elems.size(); + } + + T top() const { + return m_elems[m_head]; + } + + T pop() { + SASSERT(!empty()); + m_capacity = std::max(m_capacity, m_elems.size()); + SASSERT(m_head < m_elems.size()); + if (2 * m_head > m_capacity && m_capacity > 10) { + for (unsigned i = 0; i < m_elems.size() - m_head; ++i) { + m_elems[i] = m_elems[i + m_head]; + } + m_elems.shrink(m_elems.size() - m_head); + m_head = 0; + } + return m_elems[m_head++]; + } + +}; + +#endif + From 4b0f7bc222184c1ae649e23b88cfc4698d1c6da0 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Thu, 20 Apr 2017 17:22:05 +0100 Subject: [PATCH 446/562] Fix typo noted in #979. `g++` is the default compiler rather than the `gcc` binary. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f4c6dd012..b9d0d6fe1 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ make sudo make install ``` -Note by default ``gcc`` is used as the C++ compiler if it is available. If you +Note by default ``g++`` is used as the C++ compiler if it is available. If you would prefer to use Clang change the ``mk_make.py`` line to ```bash From 2badef9d0bdacbe6a7645aad0ed7f5896c5d47f4 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Thu, 20 Apr 2017 17:25:00 +0100 Subject: [PATCH 447/562] Be more explicit about using Clang as the compiler as noted in #979. Referring to the ``mk_make.py`` line might lead someone to think they need to modify the ``mk_make.py`` file rather than change the command line invocation. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b9d0d6fe1..f92a5389a 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ sudo make install ``` Note by default ``g++`` is used as the C++ compiler if it is available. If you -would prefer to use Clang change the ``mk_make.py`` line to +would prefer to use Clang change the ``mk_make.py`` invocation to: ```bash CXX=clang++ CC=clang python scripts/mk_make.py From 86a54dfec8ddc3e558a558319ae3887ca13a5a78 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Fri, 21 Apr 2017 08:18:25 -0700 Subject: [PATCH 448/562] debugging ccc Signed-off-by: Nikolaj Bjorner --- src/sat/sat_ccc.cpp | 719 ++++++++++++++++++++++++++++++++++++++--- src/sat/sat_ccc.h | 28 +- src/sat/sat_config.cpp | 2 + src/sat/sat_config.h | 1 + src/sat/sat_params.pyg | 3 +- src/sat/sat_scc.cpp | 247 +++++++------- src/sat/sat_solver.cpp | 11 + src/sat/sat_solver.h | 1 + 8 files changed, 832 insertions(+), 180 deletions(-) diff --git a/src/sat/sat_ccc.cpp b/src/sat/sat_ccc.cpp index 70defb17d..bb60a99e3 100644 --- a/src/sat/sat_ccc.cpp +++ b/src/sat/sat_ccc.cpp @@ -23,6 +23,18 @@ Notes: using namespace sat; + +std::ostream& ccc::decision::pp(std::ostream& out) const { + return out << "(" << m_id << " " << m_last << " d:" << m_depth << ") "; +} + +std::ostream& ccc::pp(std::ostream& out, svector const& v) { + for (unsigned i = 0; i < v.size(); ++i) { + v[i].pp(out); + } + return out; +} + lbool ccc::cube() { unsigned branch_id = 0; unsigned_vector id_trail; @@ -33,20 +45,30 @@ lbool ccc::cube() { lookahead::scoped_level _sl(lh, lh.c_fixed_truth); literal_vector trail; + svector decisions; lh.m_search_mode = lookahead_mode::searching; while (!m_cancel) { - // remove old branch ids from id_trail. - while (id_trail.size() > trail.size()) { + + s.checkpoint(); + + SASSERT(trail.size() <= decisions.size()); + while (trail.size() < decisions.size()) { + check_non_model("lh inconsistent ", decisions); + decisions.pop_back(); id_trail.pop_back(); } + SASSERT(id_trail.size() == trail.size()); + SASSERT(id_trail.size() == decisions.size()); + TRACE("sat", lh.display(tout);); - lh.inc_istamp(); - s.checkpoint(); + if (lh.inconsistent()) { if (!lh.backtrack(trail)) return l_false; continue; } + lh.inc_istamp(); + // check if CDCL solver got ahead. bool repeat = false; #pragma omp critical (ccc_solved) @@ -64,13 +86,6 @@ lbool ccc::cube() { } if (repeat) continue; - ++branch_id; - if (!trail.empty()) { - #pragma omp critical (ccc_decisions) - { - m_decisions.push(decision(branch_id, trail.size()-1, trail.back())); - } - } literal l = lh.choose(); if (lh.inconsistent()) { @@ -83,13 +98,27 @@ lbool ccc::cube() { } // update trail and set of ids + + ++branch_id; + ++lh.m_stats.m_decisions; + unsigned parent_id = id_trail.empty() ? 0 : id_trail.back(); + decision d(branch_id, trail.size() + 1, l, parent_id); id_trail.push_back(branch_id); trail.push_back(l); + decisions.push_back(d); SASSERT(id_trail.size() == trail.size()); - + + #pragma omp critical (ccc_log) + { + IF_VERBOSE(1, verbose_stream() << "select " << pp_prefix(lh.m_prefix, lh.m_trail_lim.size()) << ": " << l << " " << lh.m_trail.size() << " " << trail << "\n"; + pp(verbose_stream(), decisions) << "\n"; + ); + } + #pragma omp critical (ccc_decisions) + { + m_decisions.push(d); + } TRACE("sat", tout << "choose: " << l << " " << trail << "\n";); - ++lh.m_stats.m_decisions; - IF_VERBOSE(1, verbose_stream() << "select " << pp_prefix(lh.m_prefix, lh.m_trail_lim.size()) << ": " << l << " " << lh.m_trail.size() << "\n";); lh.push(l, lh.c_fixed_truth); SASSERT(lh.inconsistent() || !lh.is_unsat()); } @@ -102,31 +131,24 @@ lbool ccc::conquer(solver& s) { s.init_search(); s.propagate(false); if (s.inconsistent()) return l_false; - s.init_assumptions(0, 0); - s.propagate(false); - if (s.check_inconsistent()) return l_false; s.cleanup(); s.simplify_problem(); - if (s.check_inconsistent()) return l_false; + if (s.inconsistent()) return l_false; - unsigned_vector ids; + svector decisions; while (true) { SASSERT(!s.inconsistent()); - lbool r = bounded_search(s, ids); + lbool r = bounded_search(s, decisions); if (r != l_undef) return r; - - if (s.m_conflicts > s.m_config.m_max_conflicts) { - IF_VERBOSE(SAT_VB_LVL, verbose_stream() << "(sat \"abort: max-conflicts = " << s.m_conflicts << "\")\n";); - return l_undef; - } - - s.restart(); + + s.restart(); s.simplify_problem(); if (s.check_inconsistent()) return l_false; - s.gc(); + s.gc(); + } } catch (solver::abort_solver) { @@ -134,29 +156,54 @@ lbool ccc::conquer(solver& s) { } } -lbool ccc::bounded_search(solver& s, unsigned_vector& ids) { - decision d; +void ccc::replay_decisions(solver& s, svector& decisions) { + // replay decisions + bool shortcut = false; + s.propagate(true); + for (unsigned i = s.scope_lvl(); !shortcut && !s.inconsistent() && i < decisions.size(); ++i) { + decision d = decisions[i]; + literal lit = d.m_last; + lbool val = s.value(lit); + #pragma omp critical (ccc_log) + { + IF_VERBOSE(1, verbose_stream() << "replay " << lit << " " << val << "\n";); + } + switch (val) { + case l_false: + #pragma omp critical (ccc_solved) + { + m_solved.push(d.m_id); + } + check_non_model("replay", decisions); + decisions.resize(i); + shortcut = true; + break; + case l_undef: + s.push(); + s.assign(lit, justification()); + s.propagate(false); + break; + case l_true: + s.push(); + break; + } + } +} +lbool ccc::bounded_search(solver& s, svector& decisions) { + while (true) { s.checkpoint(); bool done = false; while (!done) { + replay_decisions(s, decisions); lbool is_sat = s.propagate_and_backjump_step(done); if (is_sat != l_true) return is_sat; } - if (s.m_scope_lvl < ids.size()) { - while (ids.size() > s.m_scope_lvl + 1) ids.pop_back(); - unsigned id = ids.back(); - ids.pop_back(); - #pragma omp critical (ccc_solved) - { - m_solved.push(id); - } - } - s.gc(); + decision d; bool cube_decision = false; #pragma omp critical (ccc_decisions) { @@ -165,12 +212,38 @@ lbool ccc::bounded_search(solver& s, unsigned_vector& ids) { cube_decision = true; } } + if (cube_decision) { - if (d.m_depth > ids.size()) continue; - ids.push_back(d.m_id); - s.pop_reinit(s.m_scope_lvl - d.m_depth); // TBD: check alignment of scopes - s.push(); - s.assign(d.m_last, justification()); + if (d.m_depth > 1 + decisions.size()) continue; + while (!decisions.empty() && decisions.back().m_depth >= d.m_depth) { + SASSERT(decisions.back().m_depth == decisions.size()); + check_non_model("cube decision", decisions); + decisions.pop_back(); + } + SASSERT(decisions.empty() || decisions.back().m_depth + 1 == d.m_depth); + SASSERT(decisions.empty() || decisions.back().m_id == d.m_parent); + decisions.push_back(d); + s.pop_reinit(s.m_scope_lvl + 1 - d.m_depth); // TBD: check alignment of scopes + literal lit = d.m_last; + #pragma omp critical (ccc_log) + { + IF_VERBOSE(1, pp(verbose_stream() << "push ", decisions) << " @ " << s.scope_lvl() << " " << s.value(lit) << "\n"; + if (s.value(lit) == l_false) verbose_stream() << "level: " << s.lvl(lit) << "\n";); + } + switch (s.value(lit)) { + case l_false: + decisions.pop_back(); + #pragma omp critical (ccc_solved) + { + m_solved.push(d.m_id); + } + break; + case l_true: + case l_undef: + s.push(); + s.assign(lit, justification()); + break; + } } else if (!s.decide()) { lbool is_sat = s.final_check(); @@ -181,6 +254,541 @@ lbool ccc::bounded_search(solver& s, unsigned_vector& ids) { } } +void ccc::set_model() { + push_model(1, false); + push_model(2, false); + push_model(3, false); + push_model(4, false); + push_model(5, false); + push_model(6, false); + push_model(7, false); + push_model(8, false); + push_model(9, true); + push_model(10, true); + push_model(11, true); + push_model(12, true); + push_model(13, true); + push_model(14, true); + push_model(15, true); + push_model(16, true); + push_model(17, true); + push_model(18, true); + push_model(19, true); + push_model(20, true); + push_model(21, true); + push_model(22, true); + push_model(23, true); +push_model(24, true); +push_model(25, true); +push_model(26, true); +push_model(27, true); +push_model(28, true); +push_model(29, true); +push_model(30, true); +push_model(31, true); +push_model(32, true); +push_model(33, true); +push_model(34, true); +push_model(35, true); +push_model(36, true); +push_model(37, true); +push_model(38, true); +push_model(39, true); +push_model(40, true); +push_model(41, false); +push_model(42, true); +push_model(43, true); +push_model(44, true); +push_model(45, true); +push_model(46, true); +push_model(47, true); +push_model(48, false); +push_model(49, true); +push_model(50, true); +push_model(51, true); +push_model(52, true); +push_model(53, true); +push_model(54, false); +push_model(55, true); +push_model(56, true); +push_model(57, true); +push_model(58, true); +push_model(59, true); +push_model(60, true); +push_model(61, true); +push_model(62, true); +push_model(63, true); +push_model(64, true); +push_model(65, true); +push_model(66, true); +push_model(67, true); +push_model(68, false); +push_model(69, true); +push_model(70, true); +push_model(71, false); +push_model(72, true); +push_model(73, true); +push_model(74, true); +push_model(75, true); +push_model(76, true); +push_model(77, true); +push_model(78, true); +push_model(79, true); +push_model(80, true); +push_model(81, true); +push_model(82, false); +push_model(83, true); +push_model(84, true); +push_model(85, true); +push_model(86, true); +push_model(87, true); +push_model(88, true); +push_model(89, true); +push_model(90, true); +push_model(91, false); +push_model(92, true); +push_model(93, true); +push_model(94, true); +push_model(95, true); +push_model(96, true); +push_model(97, true); +push_model(98, true); +push_model(99, false); +push_model(100, true); +push_model(101, true); +push_model(102, true); +push_model(103, true); +push_model(104, true); +push_model(105, true); +push_model(106, true); +push_model(107, true); +push_model(108, true); +push_model(109, true); +push_model(110, true); +push_model(111, true); +push_model(112, true); +push_model(113, false); +push_model(114, true); +push_model(115, true); +push_model(116, true); +push_model(117, true); +push_model(118, true); +push_model(119, true); +push_model(120, false); +push_model(121, true); +push_model(122, true); +push_model(123, true); +push_model(124, true); +push_model(125, true); +push_model(126, false); +push_model(127, true); +push_model(128, true); +push_model(129, true); +push_model(130, true); +push_model(131, true); +push_model(132, true); +push_model(133, true); +push_model(134, true); +push_model(135, true); +push_model(136, true); +push_model(137, true); +push_model(138, true); +push_model(139, false); +push_model(140, true); +push_model(141, true); +push_model(142, true); +push_model(143, false); +push_model(144, true); +push_model(145, true); +push_model(146, true); +push_model(147, true); +push_model(148, false); +push_model(149, true); +push_model(150, true); +push_model(151, true); +push_model(152, true); +push_model(153, true); +push_model(154, true); +push_model(155, true); +push_model(156, true); +push_model(157, true); +push_model(158, true); +push_model(159, true); +push_model(160, false); +push_model(161, true); +push_model(162, true); +push_model(163, true); +push_model(164, false); +push_model(165, true); +push_model(166, true); +push_model(167, true); +push_model(168, true); +push_model(169, true); +push_model(170, true); +push_model(171, true); +push_model(172, true); +push_model(173, true); +push_model(174, true); +push_model(175, true); +push_model(176, true); +push_model(177, true); +push_model(178, true); +push_model(179, true); +push_model(180, true); +push_model(181, true); +push_model(182, true); +push_model(183, true); +push_model(184, true); +push_model(185, false); +push_model(186, true); +push_model(187, true); +push_model(188, true); +push_model(189, true); +push_model(190, true); +push_model(191, true); +push_model(192, false); +push_model(193, true); +push_model(194, true); +push_model(195, true); +push_model(196, true); +push_model(197, true); +push_model(198, false); +push_model(199, true); +push_model(200, true); +push_model(201, true); +push_model(202, true); +push_model(203, true); +push_model(204, true); +push_model(205, true); +push_model(206, true); +push_model(207, true); +push_model(208, true); +push_model(209, true); +push_model(210, false); +push_model(211, false); +push_model(212, true); +push_model(213, true); +push_model(214, true); +push_model(215, true); +push_model(216, true); +push_model(217, true); +push_model(218, true); +push_model(219, true); +push_model(220, true); +push_model(221, true); +push_model(222, true); +push_model(223, true); +push_model(224, true); +push_model(225, false); +push_model(226, true); +push_model(227, true); +push_model(228, true); +push_model(229, true); +push_model(230, true); +push_model(231, false); +push_model(232, true); +push_model(233, true); +push_model(234, true); +push_model(235, false); +push_model(236, true); +push_model(237, true); +push_model(238, true); +push_model(239, true); +push_model(240, true); +push_model(241, true); +push_model(242, true); +push_model(243, true); +push_model(244, true); +push_model(245, true); +push_model(246, true); +push_model(247, true); +push_model(248, true); +push_model(249, false); +push_model(250, true); +push_model(251, true); +push_model(252, true); +push_model(253, true); +push_model(254, true); +push_model(255, true); +push_model(256, true); +push_model(257, true); +push_model(258, true); +push_model(259, true); +push_model(260, true); +push_model(261, true); +push_model(262, true); +push_model(263, false); +push_model(264, true); +push_model(265, true); +push_model(266, true); +push_model(267, true); +push_model(268, true); +push_model(269, false); +push_model(270, true); +push_model(271, true); +push_model(272, true); +push_model(273, false); +push_model(274, true); +push_model(275, true); +push_model(276, true); +push_model(277, true); +push_model(278, true); +push_model(279, true); +push_model(280, true); +push_model(281, true); +push_model(282, true); +push_model(283, true); +push_model(284, false); +push_model(285, true); +push_model(286, true); +push_model(287, true); +push_model(288, true); +push_model(289, true); +push_model(290, true); +push_model(291, true); +push_model(292, true); +push_model(293, true); +push_model(294, false); +push_model(295, true); +push_model(296, true); +push_model(297, true); +push_model(298, true); +push_model(299, true); +push_model(300, true); +push_model(301, false); +push_model(302, true); +push_model(303, true); +push_model(304, true); +push_model(305, false); +push_model(306, true); +push_model(307, true); +push_model(308, true); +push_model(309, true); +push_model(310, true); +push_model(311, true); +push_model(312, true); +push_model(313, true); +push_model(314, true); +push_model(315, true); +push_model(316, true); +push_model(317, true); +push_model(318, true); +push_model(319, false); +push_model(320, true); +push_model(321, true); +push_model(322, true); +push_model(323, true); +push_model(324, true); +push_model(325, true); +push_model(326, false); +push_model(327, true); +push_model(328, true); +push_model(329, true); +push_model(330, true); +push_model(331, true); +push_model(332, true); +push_model(333, true); +push_model(334, false); +push_model(335, true); +push_model(336, true); +push_model(337, true); +push_model(338, true); +push_model(339, true); +push_model(340, false); +push_model(341, true); +push_model(342, true); +push_model(343, true); +push_model(344, true); +push_model(345, true); +push_model(346, true); +push_model(347, true); +push_model(348, true); +push_model(349, true); +push_model(350, true); +push_model(351, true); +push_model(352, true); +push_model(353, false); +push_model(354, true); +push_model(355, true); +push_model(356, true); +push_model(357, true); +push_model(358, true); +push_model(359, true); +push_model(360, true); +push_model(361, true); +push_model(362, false); +push_model(363, false); +push_model(364, true); +push_model(365, true); +push_model(366, true); +push_model(367, true); +push_model(368, true); +push_model(369, true); +push_model(370, true); +push_model(371, true); +push_model(372, true); +push_model(373, true); +push_model(374, false); +push_model(375, true); +push_model(376, true); +push_model(377, true); +push_model(378, true); +push_model(379, true); +push_model(380, true); +push_model(381, true); +push_model(382, true); +push_model(383, true); +push_model(384, true); +push_model(385, true); +push_model(386, true); +push_model(387, true); +push_model(388, false); +push_model(389, true); +push_model(390, true); +push_model(391, true); +push_model(392, true); +push_model(393, true); +push_model(394, false); +push_model(395, true); +push_model(396, true); +push_model(397, true); +push_model(398, true); +push_model(399, true); +push_model(400, true); +push_model(401, false); +push_model(402, true); +push_model(403, true); +push_model(404, true); +push_model(405, true); +push_model(406, true); +push_model(407, true); +push_model(408, false); +push_model(409, true); +push_model(410, true); +push_model(411, true); +push_model(412, true); +push_model(413, true); +push_model(414, false); +push_model(415, true); +push_model(416, true); +push_model(417, true); +push_model(418, true); +push_model(419, true); +push_model(420, true); +push_model(421, true); +push_model(422, true); +push_model(423, true); +push_model(424, true); +push_model(425, true); +push_model(426, true); +push_model(427, true); +push_model(428, true); +push_model(429, false); +push_model(430, true); +push_model(431, false); +push_model(432, true); +push_model(433, true); +push_model(434, true); +push_model(435, true); +push_model(436, true); +push_model(437, true); +push_model(438, true); +push_model(439, true); +push_model(440, true); +push_model(441, true); +push_model(442, false); +push_model(443, true); +push_model(444, true); +push_model(445, true); +push_model(446, true); +push_model(447, true); +push_model(448, true); +push_model(449, true); +push_model(450, true); +push_model(451, true); +push_model(452, true); +push_model(453, false); +push_model(454, true); +push_model(455, true); +push_model(456, true); +push_model(457, true); +push_model(458, false); +push_model(459, true); +push_model(460, true); +push_model(461, true); +push_model(462, true); +push_model(463, true); +push_model(464, true); +push_model(465, true); +push_model(466, true); +push_model(467, false); +push_model(468, true); +push_model(469, true); +push_model(470, true); +push_model(471, true); +push_model(472, true); +push_model(473, true); +push_model(474, true); +push_model(475, true); +push_model(476, false); +push_model(477, true); +push_model(478, true); +push_model(479, true); +push_model(480, true); +push_model(481, true); +push_model(482, true); +push_model(483, true); +push_model(484, true); +push_model(485, false); +push_model(486, true); +push_model(487, true); +push_model(488, true); +push_model(489, true); +push_model(490, true); +push_model(491, true); +push_model(492, true); +push_model(493, true); +push_model(494, false); +push_model(495, true); +push_model(496, false); +push_model(497, true); +push_model(498, true); +push_model(499, true); +push_model(500, true); +push_model(501, true); +push_model(502, true); +push_model(503, true); +push_model(504, true); +push_model(505, false); +push_model(506, true); +push_model(507, true); +push_model(508, true); +push_model(509, true); +push_model(510, true); +push_model(511, true); +push_model(512, true); + +} + +void ccc::push_model(unsigned v, bool sign) { + if (m_values.size() <= v) { + m_values.resize(v + 1); + } + m_values[v] = sign; +} + +void ccc::check_non_model(char const* fn, svector const& decisions) { + for (unsigned i = 0; i < decisions.size(); ++i) { + decision d = decisions[i]; + literal lit = d.m_last; + if (m_values[lit.var()] != lit.sign()) return; + } + + #pragma omp critical (ccc_log) + { + pp(verbose_stream() << "backtracking from model " << fn << " ", decisions) << "\n"; + } +} lbool ccc::search() { enum par_exception_kind { @@ -188,6 +796,8 @@ lbool ccc::search() { ERROR_EX }; + set_model(); + m_cancel = false; scoped_limits scoped_rlimit(s.rlimit()); @@ -200,16 +810,17 @@ lbool ccc::search() { lbool result = l_undef; bool canceled = false; - int num_threads = s.m_config.m_num_threads + 1; + int num_threads = 2; // for ccc-infinity only two threads. s.m_config.m_num_threads + 1; for (int i = 1; i < num_threads; ++i) { limits.push_back(reslimit()); } for (int i = 1; i < num_threads; ++i) { s.m_params.set_uint("random_seed", s.m_rand()); - solvers[i] = alloc(sat::solver, s.m_params, limits[i]); - solvers[i]->copy(s); - scoped_rlimit.push_child(&solvers[i]->rlimit()); + solver* s1 = alloc(sat::solver, s.m_params, limits[i-1]); + solvers.push_back(s1); + s1->copy(s); + scoped_rlimit.push_child(&s1->rlimit()); } #pragma omp parallel for @@ -261,10 +872,18 @@ lbool ccc::search() { if (finished_id == -1) { switch (ex_kind) { case ERROR_EX: throw z3_error(error_code); - default: throw default_exception(ex_msg.c_str()); + default: throw default_exception(ex_msg.c_str()); } } +#if 0 + if (result == l_true) { + for (unsigned i = 1; i < m_model.size(); ++i) { + std::cout << "push_model(" << i << ", " << (m_model[i] > 0 ? "false" : "true") << ");\n"; + } + } +#endif + return result; } diff --git a/src/sat/sat_ccc.h b/src/sat/sat_ccc.h index d5357090a..da546bca1 100644 --- a/src/sat/sat_ccc.h +++ b/src/sat/sat_ccc.h @@ -23,14 +23,18 @@ Notes: namespace sat { + class ccc { struct decision { unsigned m_id; unsigned m_depth; literal m_last; - decision(unsigned id, unsigned d, literal last): - m_id(id), m_depth(d), m_last(last) {} - decision(): m_id(0), m_depth(0), m_last(null_literal) {} + unsigned m_parent; + decision(unsigned id, unsigned d, literal last, unsigned parent_id): + m_id(id), m_depth(d), m_last(last), m_parent(parent_id) {} + decision(): m_id(0), m_depth(0), m_last(null_literal), m_parent(0) {} + + std::ostream& pp(std::ostream& out) const; }; solver& s; @@ -39,6 +43,8 @@ namespace sat { model m_model; volatile bool m_cancel; + svector m_values; + struct config { config() { } @@ -50,14 +56,26 @@ namespace sat { }; lbool conquer(solver& s); - lbool bounded_search(solver& s, unsigned_vector& ids); - + lbool bounded_search(solver& s, svector& decisions); lbool cube(); + void replay_decisions(solver& s, svector& decisions); + + static std::ostream& pp(std::ostream& out, svector const& v); + + void push_model(unsigned v, bool sign); + void set_model(); + bool trail_in_model(literal_vector const& trail) const; + + void check_non_model(char const* fn, svector const& decisions); + public: + ccc(solver& s): s(s) {} lbool search(); + + model const& get_model() const { return m_model; } }; } diff --git a/src/sat/sat_config.cpp b/src/sat/sat_config.cpp index 175c34690..45ce213b0 100644 --- a/src/sat/sat_config.cpp +++ b/src/sat/sat_config.cpp @@ -38,6 +38,7 @@ namespace sat { m_num_threads = 1; m_local_search = 0; m_lookahead_search = false; + m_ccc = false; updt_params(p); } @@ -84,6 +85,7 @@ namespace sat { m_local_search = p.local_search(); m_local_search_threads = p.local_search_threads(); m_lookahead_search = p.lookahead_search(); + m_ccc = p.ccc(); // These parameters are not exposed m_simplify_mult1 = _p.get_uint("simplify_mult1", 300); diff --git a/src/sat/sat_config.h b/src/sat/sat_config.h index fb125c529..2e3d4ec86 100644 --- a/src/sat/sat_config.h +++ b/src/sat/sat_config.h @@ -61,6 +61,7 @@ namespace sat { unsigned m_local_search_threads; bool m_local_search; bool m_lookahead_search; + bool m_ccc; unsigned m_simplify_mult1; double m_simplify_mult2; diff --git a/src/sat/sat_params.pyg b/src/sat/sat_params.pyg index a69de0772..a13a8e8b5 100644 --- a/src/sat/sat_params.pyg +++ b/src/sat/sat_params.pyg @@ -30,5 +30,6 @@ def_module_params('sat', ('xor.solver', BOOL, False, 'use xor solver'), ('local_search_threads', UINT, 0, 'number of local search threads to find satisfiable solution'), ('local_search', BOOL, False, 'use local search instead of CDCL'), - ('lookahead_search', BOOL, False, 'use lookahead solver') + ('lookahead_search', BOOL, False, 'use lookahead solver'), + ('ccc', BOOL, False, 'use Concurrent Cube and Conquer solver') )) diff --git a/src/sat/sat_scc.cpp b/src/sat/sat_scc.cpp index 3dfc42f6a..aa35363b6 100644 --- a/src/sat/sat_scc.cpp +++ b/src/sat/sat_scc.cpp @@ -76,20 +76,18 @@ namespace sat { lowlink.resize(num_lits, UINT_MAX); in_s.resize(num_lits, false); literal_vector roots; - for (unsigned i = 0; i < m_solver.num_vars(); ++i) { - roots.push_back(literal(i, false)); - } + roots.resize(m_solver.num_vars(), null_literal); unsigned next_index = 0; svector frames; bool_var_vector to_elim; - for (unsigned l_idx = 0; l_idx < num_lits; l_idx++) { - if (index[l_idx] != UINT_MAX) - continue; - if (m_solver.was_eliminated(to_literal(l_idx).var())) - continue; - - m_solver.checkpoint(); + for (unsigned l_idx = 0; l_idx < num_lits; l_idx++) { + if (index[l_idx] != UINT_MAX) + continue; + if (m_solver.was_eliminated(to_literal(l_idx).var())) + continue; + + m_solver.checkpoint(); #define NEW_NODE(LIDX) { \ index[LIDX] = next_index; \ @@ -100,121 +98,122 @@ namespace sat { watch_list & wlist = m_solver.get_wlist(LIDX); \ frames.push_back(frame(LIDX, wlist.begin(), wlist.end())); \ } - - NEW_NODE(l_idx); - - while (!frames.empty()) { - loop: - frame & fr = frames.back(); - unsigned l_idx = fr.m_lidx; - if (!fr.m_first) { - // after visiting child - literal l2 = fr.m_it->get_literal(); - unsigned l2_idx = l2.index(); - SASSERT(index[l2_idx] != UINT_MAX); - if (lowlink[l2_idx] < lowlink[l_idx]) - lowlink[l_idx] = lowlink[l2_idx]; - fr.m_it++; - } - fr.m_first = false; - while (fr.m_it != fr.m_end) { - if (!fr.m_it->is_binary_clause()) { - fr.m_it++; - continue; - } - literal l2 = fr.m_it->get_literal(); - unsigned l2_idx = l2.index(); - if (index[l2_idx] == UINT_MAX) { - NEW_NODE(l2_idx); - goto loop; - } - else if (in_s[l2_idx]) { - if (index[l2_idx] < lowlink[l_idx]) - lowlink[l_idx] = index[l2_idx]; - } - fr.m_it++; - } - // visited all successors - if (lowlink[l_idx] == index[l_idx]) { - // found new SCC - CTRACE("scc_cycle", s.back() != l_idx, { - tout << "cycle: "; - unsigned j = s.size() - 1; - unsigned l2_idx; - do { - l2_idx = s[j]; - j--; - tout << to_literal(l2_idx) << " "; - } - while (l2_idx != l_idx); - tout << "\n"; - }); - - SASSERT(!s.empty()); - literal l = to_literal(l_idx); - bool_var v = l.var(); - if (roots[v] != null_literal) { - // variable was already assigned... just consume stack - TRACE("scc_detail", tout << "consuming stack...\n";); - unsigned l2_idx; - do { - l2_idx = s.back(); - s.pop_back(); - in_s[l2_idx] = false; - SASSERT(roots[to_literal(l2_idx).var()].var() == roots[v].var()); - } - while (l2_idx != l_idx); - } - else { - // check if the SCC has an external variable, and check for conflicts - TRACE("scc_detail", tout << "assigning roots...\n";); - literal r = null_literal; - unsigned j = s.size() - 1; - unsigned l2_idx; - do { - l2_idx = s[j]; - j--; - if (to_literal(l2_idx) == ~l) { - m_solver.set_conflict(justification()); - return 0; - } - if (m_solver.is_external(to_literal(l2_idx).var())) { - r = to_literal(l2_idx); - break; - } - } - while (l2_idx != l_idx); - - if (r == null_literal) { - // SCC does not contain external variable - r = to_literal(l_idx); - } - - TRACE("scc_detail", tout << "r: " << r << "\n";); - do { - l2_idx = s.back(); - s.pop_back(); - in_s[l2_idx] = false; - literal l2 = to_literal(l2_idx); - bool_var v2 = l2.var(); - if (roots[v2] == null_literal) { - if (l2.sign()) { - roots[v2] = ~r; - } - else { - roots[v2] = r; - } - if (v2 != r.var()) - to_elim.push_back(v2); - } - } - while (l2_idx != l_idx); - } - } - frames.pop_back(); - } - } + NEW_NODE(l_idx); + + while (!frames.empty()) { + loop: + frame & fr = frames.back(); + unsigned l_idx = fr.m_lidx; + if (!fr.m_first) { + // after visiting child + literal l2 = fr.m_it->get_literal(); + unsigned l2_idx = l2.index(); + SASSERT(index[l2_idx] != UINT_MAX); + if (lowlink[l2_idx] < lowlink[l_idx]) + lowlink[l_idx] = lowlink[l2_idx]; + fr.m_it++; + } + fr.m_first = false; + while (fr.m_it != fr.m_end) { + if (!fr.m_it->is_binary_clause()) { + fr.m_it++; + continue; + } + literal l2 = fr.m_it->get_literal(); + unsigned l2_idx = l2.index(); + if (index[l2_idx] == UINT_MAX) { + NEW_NODE(l2_idx); + goto loop; + } + else if (in_s[l2_idx]) { + if (index[l2_idx] < lowlink[l_idx]) + lowlink[l_idx] = index[l2_idx]; + } + fr.m_it++; + } + // visited all successors + if (lowlink[l_idx] == index[l_idx]) { + // found new SCC + CTRACE("scc_cycle", s.back() != l_idx, { + tout << "cycle: "; + unsigned j = s.size() - 1; + unsigned l2_idx; + do { + l2_idx = s[j]; + j--; + tout << to_literal(l2_idx) << " "; + } while (l2_idx != l_idx); + tout << "\n"; + }); + + SASSERT(!s.empty()); + literal l = to_literal(l_idx); + bool_var v = l.var(); + if (roots[v] != null_literal) { + // variable was already assigned... just consume stack + TRACE("scc_detail", tout << "consuming stack...\n";); + unsigned l2_idx; + do { + l2_idx = s.back(); + s.pop_back(); + in_s[l2_idx] = false; + SASSERT(roots[to_literal(l2_idx).var()].var() == roots[v].var()); + } while (l2_idx != l_idx); + } + else { + // check if the SCC has an external variable, and check for conflicts + TRACE("scc_detail", tout << "assigning roots...\n";); + literal r = null_literal; + unsigned j = s.size() - 1; + unsigned l2_idx; + do { + l2_idx = s[j]; + j--; + if (to_literal(l2_idx) == ~l) { + m_solver.set_conflict(justification()); + return 0; + } + if (m_solver.is_external(to_literal(l2_idx).var())) { + r = to_literal(l2_idx); + break; + } + } while (l2_idx != l_idx); + + if (r == null_literal) { + // SCC does not contain external variable + r = to_literal(l_idx); + } + + TRACE("scc_detail", tout << "r: " << r << "\n";); + + do { + l2_idx = s.back(); + s.pop_back(); + in_s[l2_idx] = false; + literal l2 = to_literal(l2_idx); + bool_var v2 = l2.var(); + if (roots[v2] == null_literal) { + if (l2.sign()) { + roots[v2] = ~r; + } + else { + roots[v2] = r; + } + if (v2 != r.var()) + to_elim.push_back(v2); + } + } while (l2_idx != l_idx); + } + } + frames.pop_back(); + } + } + for (unsigned i = 0; i < m_solver.num_vars(); ++i) { + if (roots[i] == null_literal) { + roots[i] = literal(i, false); + } + } TRACE("scc", for (unsigned i = 0; i < roots.size(); i++) { tout << i << " -> " << roots[i] << "\n"; } tout << "to_elim: "; for (unsigned i = 0; i < to_elim.size(); i++) tout << to_elim[i] << " "; tout << "\n";); m_num_elim += to_elim.size(); diff --git a/src/sat/sat_solver.cpp b/src/sat/sat_solver.cpp index 8eaba2734..4647ddb36 100644 --- a/src/sat/sat_solver.cpp +++ b/src/sat/sat_solver.cpp @@ -23,6 +23,7 @@ Revision History: #include"max_cliques.h" #include"scoped_ptr_vector.h" #include"sat_lookahead.h" +#include"sat_ccc.h" // define to update glue during propagation #define UPDATE_GLUE @@ -790,6 +791,9 @@ namespace sat { if (m_config.m_local_search) { return do_local_search(num_lits, lits); } + if (m_config.m_ccc && num_lits == 0) { + return do_ccc(); + } if ((m_config.m_num_threads > 1 || m_config.m_local_search_threads > 0) && !m_par) { return check_par(num_lits, lits); } @@ -874,6 +878,13 @@ namespace sat { return r; } + lbool solver::do_ccc() { + ccc c(*this); + lbool r = c.search(); + m_model = c.get_model(); + return r; + } + lbool solver::lookahead_search() { lookahead lh(*this); lbool r = l_undef; diff --git a/src/sat/sat_solver.h b/src/sat/sat_solver.h index b4b7f82fe..fda8362ca 100644 --- a/src/sat/sat_solver.h +++ b/src/sat/sat_solver.h @@ -351,6 +351,7 @@ namespace sat { lbool check_par(unsigned num_lits, literal const* lits); lbool lookahead_search(); lbool do_local_search(unsigned num_lits, literal const* lits); + lbool do_ccc(); // ----------------------- // From 5cfe5e15aca60262ed9fcc1764ef065914fc46fa Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 21 Apr 2017 17:51:14 -0400 Subject: [PATCH 449/562] unsat core validation for smt theories --- src/smt/smt_context.cpp | 100 +++++++++++++++------------------------- src/smt/smt_context.h | 2 +- src/smt/smt_theory.h | 8 ++++ src/smt/theory_str.cpp | 22 +++++++++ src/smt/theory_str.h | 1 + 5 files changed, 68 insertions(+), 65 deletions(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index db09552ef..412e7b13d 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -3211,11 +3211,11 @@ namespace smt { m_assumptions.reset(); } - void context::mk_unsat_core() { + lbool context::mk_unsat_core() { SASSERT(inconsistent()); if (!tracking_assumptions()) { SASSERT(m_assumptions.empty()); - return; + return l_false; } uint_set already_found_assumptions; literal_vector::const_iterator it = m_conflict_resolution->begin_unsat_core(); @@ -3240,7 +3240,19 @@ namespace smt { for (unsigned i = 0; i < sz; i++) { tout << mk_pp(m_unsat_core.get(i), m_manager) << "\n"; }); - validate_unsat_core(); + validate_unsat_core(); + // theory validation of unsat core + + ptr_vector::iterator th_it = m_theory_set.begin(); + ptr_vector::iterator th_end = m_theory_set.end(); + for (; th_it != th_end; ++th_it) { + lbool theory_result = (*th_it)->validate_unsat_core(m_unsat_core); + if (theory_result == l_undef) { + return l_undef; + } + } + + return l_false; } /** @@ -3267,45 +3279,6 @@ namespace smt { if (r == l_true && get_cancel_flag()) { r = l_undef; } - - /* - // PATCH for theory_str: - // UNSAT + overlapping variables => UNKNOWN - if (r == l_false && use_theory_str_overlap_assumption()) { - // check the unsat core for an assumption from theory_str relating to overlaps. - // if we find this assumption, we have to answer UNKNOWN - // otherwise, we can pass through UNSAT - TRACE("t_str", tout << "unsat core:\n"; - unsigned sz = m_unsat_core.size(); - for (unsigned i = 0; i < sz; i++) { - tout << mk_pp(m_unsat_core.get(i), m_manager) << "\n"; - }); - - bool assumptionFound = false; - unsigned sz = m_unsat_core.size(); - app * target_term = to_app(m_manager.mk_not(m_theoryStrOverlapAssumption_term)); - internalize_term(target_term); - for (unsigned i = 0; i < sz; ++i) { - app * core_term = to_app(m_unsat_core.get(i)); - // not sure if this is the correct way to compare exprs in this context - enode * e1; - enode * e2; - e1 = get_enode(target_term); - e2 = get_enode(core_term); - if (e1 == e2) { - // found match - TRACE("t_str", tout << "overlap detected in unsat core; changing UNSAT to UNKNOWN" << std::endl;); - assumptionFound = true; - r = l_undef; - break; - } - } - if (!assumptionFound) { - TRACE("t_str", tout << "no overlaps detected in unsat core, answering UNSAT" << std::endl;); - } - } - */ - return r; } @@ -3323,23 +3296,6 @@ namespace smt { SASSERT(!m_setup.already_configured()); setup_context(m_fparams.m_auto_config); - /* - // theory_str requires the context to be set up with a special assumption. - // we need to wait until after setup_context() to know whether this is the case - if (m_use_theory_str_overlap_assumption) { - TRACE("t_str", tout << "enabling theory_str overlap assumption" << std::endl;); - // TODO maybe refactor this a bit - symbol strOverlap("!!TheoryStrOverlapAssumption!!"); - expr_ref_vector assumption(get_manager()); - seq_util m_sequtil(m_manager); - sort * s = m_manager.mk_bool_sort(); - m_theoryStrOverlapAssumption_term = expr_ref(m_manager.mk_const(strOverlap, s), m_manager); - assumption.push_back(m_manager.mk_not(m_theoryStrOverlapAssumption_term)); - // this might work, even though we already did a bit of setup - return check(assumption.size(), assumption.c_ptr(), reset_cancel); - } - */ - expr_ref_vector theory_assumptions(m_manager); ptr_vector::iterator it = m_theory_set.begin(); ptr_vector::iterator end = m_theory_set.end(); @@ -3413,7 +3369,7 @@ namespace smt { (*it)->setup(); } - lbool context::check(unsigned num_assumptions, expr * const * assumptions, bool reset_cancel) { + lbool context::check(unsigned ext_num_assumptions, expr * const * ext_assumptions, bool reset_cancel) { m_stats.m_num_checks++; TRACE("check_bug", tout << "STARTING check(num_assumptions, assumptions)\n"; tout << "inconsistent: " << inconsistent() << ", m_unsat_core.empty(): " << m_unsat_core.empty() << "\n"; @@ -3424,6 +3380,22 @@ namespace smt { m_unsat_core.reset(); if (!check_preamble(reset_cancel)) return l_undef; + + expr_ref_vector theory_assumptions(m_manager); + for (unsigned i = 0; i < ext_num_assumptions; ++i) { + theory_assumptions.push_back(ext_assumptions[i]); + } + ptr_vector::iterator it = m_theory_set.begin(); + ptr_vector::iterator end = m_theory_set.end(); + for (; it != end; ++it) { + (*it)->add_theory_assumptions(theory_assumptions); + } + if (!theory_assumptions.empty()) { + TRACE("search", tout << "Adding theory assumptions to context" << std::endl;); + } + unsigned num_assumptions = theory_assumptions.size(); + expr * const * assumptions = theory_assumptions.c_ptr(); + if (!validate_assumptions(num_assumptions, assumptions)) return l_undef; TRACE("check_bug", tout << "inconsistent: " << inconsistent() << ", m_unsat_core.empty(): " << m_unsat_core.empty() << "\n";); @@ -3447,13 +3419,13 @@ namespace smt { TRACE("after_internalization", display(tout);); if (inconsistent()) { VERIFY(!resolve_conflict()); // build the proof - mk_unsat_core(); - r = l_false; + r = mk_unsat_core(); } else { r = search(); - if (r == l_false) - mk_unsat_core(); + if (r == l_false) { + r = mk_unsat_core(); // validation may change an l_false to l_undef here + } } } } diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index 0667f622e..0943662e8 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -1094,7 +1094,7 @@ namespace smt { void reset_assumptions(); - void mk_unsat_core(); + lbool mk_unsat_core(); void validate_unsat_core(); diff --git a/src/smt/smt_theory.h b/src/smt/smt_theory.h index e412f2f1b..ff29c7413 100644 --- a/src/smt/smt_theory.h +++ b/src/smt/smt_theory.h @@ -199,6 +199,14 @@ namespace smt { return FC_DONE; } + /** + \brief This method is called from the smt_context when an unsat core is generated. + The theory may change the answer to UNKNOWN by returning l_undef from this method. + */ + virtual lbool validate_unsat_core(expr_ref_vector & unsat_core) { + return l_false; + } + /** \brief Parametric theories (e.g. Arrays) should implement this method. See example in context::is_shared diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 354589318..bddd0b78e 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -7302,6 +7302,28 @@ void theory_str::add_theory_assumptions(expr_ref_vector & assumptions) { assumptions.push_back(get_manager().mk_not(m_theoryStrOverlapAssumption_term)); } +lbool theory_str::validate_unsat_core(expr_ref_vector & unsat_core) { + bool assumptionFound = false; + + app * target_term = to_app(get_manager().mk_not(m_theoryStrOverlapAssumption_term)); + internalize_term(target_term); + for (unsigned i = 0; i < unsat_core.size(); ++i) { + app * core_term = to_app(unsat_core.get(i)); + // not sure if this is the correct way to compare terms in this context + enode * e1; + enode * e2; + e1 = get_context().get_enode(target_term); + e2 = get_context().get_enode(core_term); + if (e1 == e2) { + TRACE("t_str", tout << "overlap detected in unsat core, changing UNSAT to UNKNOWN" << std::endl;); + assumptionFound = true; + return l_undef; + } + } + + return l_false; +} + void theory_str::init_search_eh() { ast_manager & m = get_manager(); context & ctx = get_context(); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 3c273d4e2..7c2df9e12 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -629,6 +629,7 @@ namespace smt { virtual theory* mk_fresh(context*) { return alloc(theory_str, get_manager(), m_params); } virtual void init_search_eh(); virtual void add_theory_assumptions(expr_ref_vector & assumptions); + virtual lbool validate_unsat_core(expr_ref_vector & unsat_core); virtual void relevant_eh(app * n); virtual void assign_eh(bool_var v, bool is_true); virtual void push_scope_eh(); From a1bb1f2a13557856ea8112b5e856fc8731a040af Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 22 Apr 2017 13:15:00 -0400 Subject: [PATCH 450/562] pre-init assumptions and unsat core validation for smt theories --- src/smt/smt_context.cpp | 50 +++++++++++++++++++++++++++++++++++++---- src/smt/smt_context.h | 6 +++-- src/smt/smt_theory.h | 16 +++++++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index f1b043556..225a0d58d 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -3072,11 +3072,11 @@ namespace smt { m_assumptions.reset(); } - void context::mk_unsat_core() { + lbool context::mk_unsat_core() { SASSERT(inconsistent()); if (!tracking_assumptions()) { SASSERT(m_assumptions.empty()); - return; + return l_false; } uint_set already_found_assumptions; literal_vector::const_iterator it = m_conflict_resolution->begin_unsat_core(); @@ -3101,7 +3101,17 @@ namespace smt { for (unsigned i = 0; i < sz; i++) { tout << mk_pp(m_unsat_core.get(i), m_manager) << "\n"; }); - validate_unsat_core(); + validate_unsat_core(); + // theory validation of unsat core + ptr_vector::iterator th_it = m_theory_set.begin(); + ptr_vector::iterator th_end = m_theory_set.end(); + for (; th_it != th_end; ++th_it) { + lbool theory_result = (*th_it)->validate_unsat_core(m_unsat_core); + if (theory_result == l_undef) { + return l_undef; + } + } + return l_false; } /** @@ -3144,6 +3154,14 @@ namespace smt { SASSERT(m_scope_lvl == 0); SASSERT(!m_setup.already_configured()); setup_context(m_fparams.m_auto_config); + + expr_ref_vector theory_assumptions(m_manager); + get_theory_assumptions(theory_assumptions); + if (!theory_assumptions.empty()) { + TRACE("search", tout << "Adding theory assumptions to context" << std::endl;); + return check(theory_assumptions.size(), theory_assumptions.c_ptr(), reset_cancel, true); + } + internalize_assertions(); lbool r = l_undef; if (m_asserted_formulas.inconsistent()) { @@ -3205,7 +3223,15 @@ namespace smt { (*it)->setup(); } - lbool context::check(unsigned num_assumptions, expr * const * assumptions, bool reset_cancel) { + void context::get_theory_assumptions(expr_ref_vector & theory_assumptions) { + ptr_vector::iterator it = m_theory_set.begin(); + ptr_vector::iterator end = m_theory_set.end(); + for (; it != end; ++it) { + (*it)->add_theory_assumptions(theory_assumptions); + } + } + + lbool context::check(unsigned ext_num_assumptions, expr * const * ext_assumptions, bool reset_cancel, bool already_did_theory_assumptions) { m_stats.m_num_checks++; TRACE("check_bug", tout << "STARTING check(num_assumptions, assumptions)\n"; tout << "inconsistent: " << inconsistent() << ", m_unsat_core.empty(): " << m_unsat_core.empty() << "\n"; @@ -3216,6 +3242,22 @@ namespace smt { m_unsat_core.reset(); if (!check_preamble(reset_cancel)) return l_undef; + + expr_ref_vector all_assumptions(m_manager); + for (unsigned i = 0; i < ext_num_assumptions; ++i) { + all_assumptions.push_back(ext_assumptions[i]); + } + if (!already_did_theory_assumptions) { + ptr_vector::iterator it = m_theory_set.begin(); + ptr_vector::iterator end = m_theory_set.end(); + for (; it != end; ++it) { + (*it)->add_theory_assumptions(all_assumptions); + } + } + + unsigned num_assumptions = all_assumptions.size(); + expr * const * assumptions = all_assumptions.c_ptr(); + if (!validate_assumptions(num_assumptions, assumptions)) return l_undef; TRACE("check_bug", tout << "inconsistent: " << inconsistent() << ", m_unsat_core.empty(): " << m_unsat_core.empty() << "\n";); diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index 1f57a7550..4f0c14f5a 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -1059,7 +1059,9 @@ namespace smt { void reset_assumptions(); - void mk_unsat_core(); + void get_theory_assumptions(expr_ref_vector & theory_assumptions); + + lbool mk_unsat_core(); void validate_unsat_core(); @@ -1441,7 +1443,7 @@ namespace smt { void pop(unsigned num_scopes); - lbool check(unsigned num_assumptions = 0, expr * const * assumptions = 0, bool reset_cancel = true); + lbool check(unsigned num_assumptions = 0, expr * const * assumptions = 0, bool reset_cancel = true, bool already_did_theory_assumptions = false); lbool get_consequences(expr_ref_vector const& assumptions, expr_ref_vector const& vars, expr_ref_vector& conseq, expr_ref_vector& unfixed); diff --git a/src/smt/smt_theory.h b/src/smt/smt_theory.h index cee36535f..2745a6efd 100644 --- a/src/smt/smt_theory.h +++ b/src/smt/smt_theory.h @@ -177,6 +177,22 @@ namespace smt { virtual void restart_eh() { } + /** + \brief This method is called by smt_context before the search starts + to get any extra assumptions the theory wants to use. + (See theory_str for an example) + */ + virtual void add_theory_assumptions(expr_ref_vector & assumptions) { + } + + /** + \brief This method is called from smt_context when an unsat core is generated. + The theory may change the answer to UNKNOWN by returning l_undef from this method. + */ + virtual lbool validate_unsat_core(expr_ref_vector & unsat_core) { + return l_false; + } + /** \brief This method is invoked before the search starts. */ From 367cc4b77f4ae283790556b848be31390a8e22c6 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sat, 22 Apr 2017 13:36:09 -0400 Subject: [PATCH 451/562] check result of unsat core validation --- src/smt/smt_context.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 225a0d58d..32fb492ad 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -3281,13 +3281,21 @@ namespace smt { TRACE("after_internalization", display(tout);); if (inconsistent()) { VERIFY(!resolve_conflict()); // build the proof - mk_unsat_core(); - r = l_false; + lbool result = mk_unsat_core(); + if (result == l_undef) { + r = l_undef; + } else { + r = l_false; + } } else { r = search(); - if (r == l_false) - mk_unsat_core(); + if (r == l_false) { + lbool result = mk_unsat_core(); + if (result == l_undef) { + r = l_undef; + } + } } } } From 5068d2083dc0609801f572a0e3d14df753d36a03 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sat, 22 Apr 2017 11:36:03 -0700 Subject: [PATCH 452/562] tidy Signed-off-by: Nikolaj Bjorner --- src/smt/smt_context.cpp | 15 ++++----------- src/smt/smt_context.h | 2 +- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 32fb492ad..6a3c036ca 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -3156,7 +3156,7 @@ namespace smt { setup_context(m_fparams.m_auto_config); expr_ref_vector theory_assumptions(m_manager); - get_theory_assumptions(theory_assumptions); + add_theory_assumptions(theory_assumptions); if (!theory_assumptions.empty()) { TRACE("search", tout << "Adding theory assumptions to context" << std::endl;); return check(theory_assumptions.size(), theory_assumptions.c_ptr(), reset_cancel, true); @@ -3223,7 +3223,7 @@ namespace smt { (*it)->setup(); } - void context::get_theory_assumptions(expr_ref_vector & theory_assumptions) { + void context::add_theory_assumptions(expr_ref_vector & theory_assumptions) { ptr_vector::iterator it = m_theory_set.begin(); ptr_vector::iterator end = m_theory_set.end(); for (; it != end; ++it) { @@ -3243,16 +3243,9 @@ namespace smt { if (!check_preamble(reset_cancel)) return l_undef; - expr_ref_vector all_assumptions(m_manager); - for (unsigned i = 0; i < ext_num_assumptions; ++i) { - all_assumptions.push_back(ext_assumptions[i]); - } + expr_ref_vector all_assumptions(m_manager, ext_num_assumptions, ext_assumptions); if (!already_did_theory_assumptions) { - ptr_vector::iterator it = m_theory_set.begin(); - ptr_vector::iterator end = m_theory_set.end(); - for (; it != end; ++it) { - (*it)->add_theory_assumptions(all_assumptions); - } + add_theory_assumptions(all_assumptions); } unsigned num_assumptions = all_assumptions.size(); diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index 4f0c14f5a..abdba86d1 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -1059,7 +1059,7 @@ namespace smt { void reset_assumptions(); - void get_theory_assumptions(expr_ref_vector & theory_assumptions); + void add_theory_assumptions(expr_ref_vector & theory_assumptions); lbool mk_unsat_core(); From 07fe45e92330a7d30e3d25def3e696d31f1e377f Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sat, 22 Apr 2017 11:40:47 -0700 Subject: [PATCH 453/562] ccc Signed-off-by: Nikolaj Bjorner --- src/sat/sat_ccc.cpp | 801 ++++++++++------------------------------ src/sat/sat_ccc.h | 17 +- src/sat/sat_lookahead.h | 4 +- src/sat/sat_scc.cpp | 238 ++++++------ 4 files changed, 340 insertions(+), 720 deletions(-) diff --git a/src/sat/sat_ccc.cpp b/src/sat/sat_ccc.cpp index bb60a99e3..141869f11 100644 --- a/src/sat/sat_ccc.cpp +++ b/src/sat/sat_ccc.cpp @@ -35,11 +35,11 @@ std::ostream& ccc::pp(std::ostream& out, svector const& v) { return out; } -lbool ccc::cube() { +lbool ccc::cube2() { unsigned branch_id = 0; unsigned_vector id_trail; - lookahead lh(s); + lookahead lh(m_s); lh.init_search(); lh.m_model.reset(); @@ -47,13 +47,115 @@ lbool ccc::cube() { literal_vector trail; svector decisions; lh.m_search_mode = lookahead_mode::searching; + lh.m_blocked_literal = null_literal; + lbool r = cube2(branch_id, decisions, lh); + if (r == l_true) { + m_model = lh.get_model(); + } + return r; +} + +lbool ccc::cube2(unsigned& branch_id, svector& decisions, lookahead& lh) { + m_s.checkpoint(); + + if (lh.inconsistent()) { + return l_false; + } + + lh.inc_istamp(); + + // check if CDCL solver got ahead. + bool repeat = false; + #pragma omp critical (ccc_solved) + { + while (!m_solved.empty()) { + unsigned solved_id = m_solved.top(); + if (contains_branch(decisions, solved_id)) { + IF_VERBOSE(1, verbose_stream() << "conquer " << decisions.size() << "\n";); + repeat = true; + break; + } + else { + m_solved.pop(); + } + } + } + if (repeat) return l_false; + + literal l = lh.choose(); + if (lh.inconsistent()) { + return l_false; + } + + if (l == null_literal) { + return l_true; + } + + if (!decisions.empty()) { + #pragma omp critical (ccc_decisions) + { + m_decisions.push(decisions.back()); + } + } + + // update trail and set of ids + + ++branch_id; + ++lh.m_stats.m_decisions; + unsigned parent_id = decisions.empty() ? 0 : decisions.back().m_id; + decision d(branch_id, decisions.size() + 1, l, null_literal, parent_id); + decisions.push_back(d); + + #pragma omp critical (ccc_log) + { + IF_VERBOSE(1, verbose_stream() << "select " << pp_prefix(lh.m_prefix, lh.m_trail_lim.size()) << ": " << l << " " << lh.m_trail.size() << "\n";); + IF_VERBOSE(2, pp(verbose_stream(), decisions) << "\n"; ); + } + TRACE("sat", tout << "choose: " << l << " " << trail << "\n";); + lh.push(l, lh.c_fixed_truth); + lbool r = cube2(branch_id, decisions, lh); + if (r == l_false) { + lh.pop(); + lh.flip_prefix(); + lh.push(~l, lh.c_fixed_truth); + decisions.back().m_last = ~l; + r = cube2(branch_id, decisions, lh); + if (r == l_false) { + lh.pop(); + decisions.pop_back(); + } + } + return r; +} + +bool ccc::contains_branch(svector const& decisions, unsigned branch_id) const { + for (unsigned i = 0; i < decisions.size(); ++i) { + if (branch_id == decisions[i].m_id) return true; + } + return false; +} + + +lbool ccc::cube() { + unsigned branch_id = 0; + unsigned_vector id_trail; + + lookahead lh(m_s); + lh.init_search(); + lh.m_model.reset(); + + lookahead::scoped_level _sl(lh, lh.c_fixed_truth); + literal_vector trail; + svector decisions; + lh.m_search_mode = lookahead_mode::searching; + lh.m_blocked_literal = null_literal; while (!m_cancel) { - s.checkpoint(); + m_s.checkpoint(); SASSERT(trail.size() <= decisions.size()); while (trail.size() < decisions.size()) { - check_non_model("lh inconsistent ", decisions); + //check_non_model("lh inconsistent ", decisions); decisions.pop_back(); id_trail.pop_back(); } @@ -76,6 +178,7 @@ lbool ccc::cube() { if (!m_solved.empty()) { unsigned solved_id = m_solved.top(); if (id_trail.contains(solved_id)) { + IF_VERBOSE(1, verbose_stream() << "cconquer " << decisions.size() << "\n";); lh.set_conflict(); } else { @@ -86,7 +189,6 @@ lbool ccc::cube() { } if (repeat) continue; - literal l = lh.choose(); if (lh.inconsistent()) { if (!lh.backtrack(trail)) return l_false; @@ -102,17 +204,17 @@ lbool ccc::cube() { ++branch_id; ++lh.m_stats.m_decisions; unsigned parent_id = id_trail.empty() ? 0 : id_trail.back(); - decision d(branch_id, trail.size() + 1, l, parent_id); + decision d(branch_id, trail.size() + 1, l, lh.m_blocked_literal, parent_id); id_trail.push_back(branch_id); trail.push_back(l); decisions.push_back(d); SASSERT(id_trail.size() == trail.size()); + lh.m_blocked_literal = null_literal; #pragma omp critical (ccc_log) { - IF_VERBOSE(1, verbose_stream() << "select " << pp_prefix(lh.m_prefix, lh.m_trail_lim.size()) << ": " << l << " " << lh.m_trail.size() << " " << trail << "\n"; - pp(verbose_stream(), decisions) << "\n"; - ); + IF_VERBOSE(1, verbose_stream() << "select " << pp_prefix(lh.m_prefix, lh.m_trail_lim.size()) << ": " << l << " " << lh.m_trail.size() << "\n";); + IF_VERBOSE(2, verbose_stream() << " " << trail << "\n"; pp(verbose_stream(), decisions) << "\n"; ); } #pragma omp critical (ccc_decisions) { @@ -148,7 +250,6 @@ lbool ccc::conquer(solver& s) { s.simplify_problem(); if (s.check_inconsistent()) return l_false; s.gc(); - } } catch (solver::abort_solver) { @@ -157,41 +258,110 @@ lbool ccc::conquer(solver& s) { } void ccc::replay_decisions(solver& s, svector& decisions) { - // replay decisions - bool shortcut = false; s.propagate(true); - for (unsigned i = s.scope_lvl(); !shortcut && !s.inconsistent() && i < decisions.size(); ++i) { + for (unsigned i = s.scope_lvl(); !s.inconsistent() && i < decisions.size(); ++i) { decision d = decisions[i]; literal lit = d.m_last; lbool val = s.value(lit); #pragma omp critical (ccc_log) { - IF_VERBOSE(1, verbose_stream() << "replay " << lit << " " << val << "\n";); + IF_VERBOSE(2, verbose_stream() << "replay " << lit << " " << val << "\n";); } - switch (val) { + if (!push_decision(s, d)) { + // negation of decision is implied. + // check_non_model("replay", decisions); + decisions.resize(i); + return; + } + } +} + +bool ccc::push_decision(solver& s, decision const& d) { + literal lit = d.m_last; + switch (s.value(lit)) { + case l_false: + #pragma omp critical (ccc_solved) + { + m_solved.push(d.m_id); + } + //TBD: + s.m_restart_threshold = s.m_config.m_restart_initial; + //s.m_conflicts_since_last_restart = 0; + return false; + case l_true: + s.push(); + break; + case l_undef: + s.push(); + s.assign(lit, justification()); + s.propagate(true); + break; + } + literal blocked = d.m_blocked; + if (false && blocked != null_literal) { + switch (s.value(blocked)) { case l_false: #pragma omp critical (ccc_solved) { m_solved.push(d.m_id); } - check_non_model("replay", decisions); - decisions.resize(i); - shortcut = true; + return false; + case l_true: break; case l_undef: - s.push(); - s.assign(lit, justification()); - s.propagate(false); + //s.assign(blocked, justification()); + //s.propagate(true); break; - case l_true: - s.push(); - break; } - } + } + return true; } -lbool ccc::bounded_search(solver& s, svector& decisions) { +bool ccc::cube_decision(solver& s, svector& decisions) { + decision d; + bool use_cube_decision = false; + SASSERT(s.m_qhead == s.m_trail.size()); + get_cube: + #pragma omp critical (ccc_decisions) + { + if (!m_decisions.empty()) { + d = m_decisions.pop(); + use_cube_decision = true; + } + } + + if (!use_cube_decision) { + return false; + } + if (!decisions.empty() && decisions.back().m_depth + 1 < d.m_depth) { + goto get_cube; + } + + while (!decisions.empty() && decisions.back().m_depth >= d.m_depth) { + // check_non_model("cube decision", decisions); + decisions.pop_back(); + } + SASSERT(decisions.empty() || decisions.back().m_depth + 1 == d.m_depth); + SASSERT(decisions.empty() || decisions.back().m_id == d.m_parent); + s.pop_reinit(s.scope_lvl() - decisions.size()); + SASSERT(s.m_qhead == s.m_trail.size()); + SASSERT(s.scope_lvl() == decisions.size()); + #pragma omp critical (ccc_log) + { + literal lit = d.m_last; + IF_VERBOSE(1, verbose_stream() << "cube " << decisions.size() << "\n";); + IF_VERBOSE(2, pp(verbose_stream() << "push ", decisions) << " @ " << s.scope_lvl() << " " << s.value(lit) << "\n"; + if (s.value(lit) == l_false) verbose_stream() << "level: " << s.lvl(lit) << "\n";); + } + if (push_decision(s, d)) { + decisions.push_back(d); + } + + return true; +} + +lbool ccc::bounded_search(solver& s, svector& decisions) { while (true) { s.checkpoint(); bool done = false; @@ -202,50 +372,8 @@ lbool ccc::bounded_search(solver& s, svector& decisions) { } s.gc(); - - decision d; - bool cube_decision = false; - #pragma omp critical (ccc_decisions) - { - if (!m_decisions.empty()) { - d = m_decisions.pop(); - cube_decision = true; - } - } - - if (cube_decision) { - if (d.m_depth > 1 + decisions.size()) continue; - while (!decisions.empty() && decisions.back().m_depth >= d.m_depth) { - SASSERT(decisions.back().m_depth == decisions.size()); - check_non_model("cube decision", decisions); - decisions.pop_back(); - } - SASSERT(decisions.empty() || decisions.back().m_depth + 1 == d.m_depth); - SASSERT(decisions.empty() || decisions.back().m_id == d.m_parent); - decisions.push_back(d); - s.pop_reinit(s.m_scope_lvl + 1 - d.m_depth); // TBD: check alignment of scopes - literal lit = d.m_last; - #pragma omp critical (ccc_log) - { - IF_VERBOSE(1, pp(verbose_stream() << "push ", decisions) << " @ " << s.scope_lvl() << " " << s.value(lit) << "\n"; - if (s.value(lit) == l_false) verbose_stream() << "level: " << s.lvl(lit) << "\n";); - } - switch (s.value(lit)) { - case l_false: - decisions.pop_back(); - #pragma omp critical (ccc_solved) - { - m_solved.push(d.m_id); - } - break; - case l_true: - case l_undef: - s.push(); - s.assign(lit, justification()); - break; - } - } - else if (!s.decide()) { + + if (!cube_decision(s, decisions) && !s.decide()) { lbool is_sat = s.final_check(); if (is_sat != l_undef) { return is_sat; @@ -254,521 +382,6 @@ lbool ccc::bounded_search(solver& s, svector& decisions) { } } -void ccc::set_model() { - push_model(1, false); - push_model(2, false); - push_model(3, false); - push_model(4, false); - push_model(5, false); - push_model(6, false); - push_model(7, false); - push_model(8, false); - push_model(9, true); - push_model(10, true); - push_model(11, true); - push_model(12, true); - push_model(13, true); - push_model(14, true); - push_model(15, true); - push_model(16, true); - push_model(17, true); - push_model(18, true); - push_model(19, true); - push_model(20, true); - push_model(21, true); - push_model(22, true); - push_model(23, true); -push_model(24, true); -push_model(25, true); -push_model(26, true); -push_model(27, true); -push_model(28, true); -push_model(29, true); -push_model(30, true); -push_model(31, true); -push_model(32, true); -push_model(33, true); -push_model(34, true); -push_model(35, true); -push_model(36, true); -push_model(37, true); -push_model(38, true); -push_model(39, true); -push_model(40, true); -push_model(41, false); -push_model(42, true); -push_model(43, true); -push_model(44, true); -push_model(45, true); -push_model(46, true); -push_model(47, true); -push_model(48, false); -push_model(49, true); -push_model(50, true); -push_model(51, true); -push_model(52, true); -push_model(53, true); -push_model(54, false); -push_model(55, true); -push_model(56, true); -push_model(57, true); -push_model(58, true); -push_model(59, true); -push_model(60, true); -push_model(61, true); -push_model(62, true); -push_model(63, true); -push_model(64, true); -push_model(65, true); -push_model(66, true); -push_model(67, true); -push_model(68, false); -push_model(69, true); -push_model(70, true); -push_model(71, false); -push_model(72, true); -push_model(73, true); -push_model(74, true); -push_model(75, true); -push_model(76, true); -push_model(77, true); -push_model(78, true); -push_model(79, true); -push_model(80, true); -push_model(81, true); -push_model(82, false); -push_model(83, true); -push_model(84, true); -push_model(85, true); -push_model(86, true); -push_model(87, true); -push_model(88, true); -push_model(89, true); -push_model(90, true); -push_model(91, false); -push_model(92, true); -push_model(93, true); -push_model(94, true); -push_model(95, true); -push_model(96, true); -push_model(97, true); -push_model(98, true); -push_model(99, false); -push_model(100, true); -push_model(101, true); -push_model(102, true); -push_model(103, true); -push_model(104, true); -push_model(105, true); -push_model(106, true); -push_model(107, true); -push_model(108, true); -push_model(109, true); -push_model(110, true); -push_model(111, true); -push_model(112, true); -push_model(113, false); -push_model(114, true); -push_model(115, true); -push_model(116, true); -push_model(117, true); -push_model(118, true); -push_model(119, true); -push_model(120, false); -push_model(121, true); -push_model(122, true); -push_model(123, true); -push_model(124, true); -push_model(125, true); -push_model(126, false); -push_model(127, true); -push_model(128, true); -push_model(129, true); -push_model(130, true); -push_model(131, true); -push_model(132, true); -push_model(133, true); -push_model(134, true); -push_model(135, true); -push_model(136, true); -push_model(137, true); -push_model(138, true); -push_model(139, false); -push_model(140, true); -push_model(141, true); -push_model(142, true); -push_model(143, false); -push_model(144, true); -push_model(145, true); -push_model(146, true); -push_model(147, true); -push_model(148, false); -push_model(149, true); -push_model(150, true); -push_model(151, true); -push_model(152, true); -push_model(153, true); -push_model(154, true); -push_model(155, true); -push_model(156, true); -push_model(157, true); -push_model(158, true); -push_model(159, true); -push_model(160, false); -push_model(161, true); -push_model(162, true); -push_model(163, true); -push_model(164, false); -push_model(165, true); -push_model(166, true); -push_model(167, true); -push_model(168, true); -push_model(169, true); -push_model(170, true); -push_model(171, true); -push_model(172, true); -push_model(173, true); -push_model(174, true); -push_model(175, true); -push_model(176, true); -push_model(177, true); -push_model(178, true); -push_model(179, true); -push_model(180, true); -push_model(181, true); -push_model(182, true); -push_model(183, true); -push_model(184, true); -push_model(185, false); -push_model(186, true); -push_model(187, true); -push_model(188, true); -push_model(189, true); -push_model(190, true); -push_model(191, true); -push_model(192, false); -push_model(193, true); -push_model(194, true); -push_model(195, true); -push_model(196, true); -push_model(197, true); -push_model(198, false); -push_model(199, true); -push_model(200, true); -push_model(201, true); -push_model(202, true); -push_model(203, true); -push_model(204, true); -push_model(205, true); -push_model(206, true); -push_model(207, true); -push_model(208, true); -push_model(209, true); -push_model(210, false); -push_model(211, false); -push_model(212, true); -push_model(213, true); -push_model(214, true); -push_model(215, true); -push_model(216, true); -push_model(217, true); -push_model(218, true); -push_model(219, true); -push_model(220, true); -push_model(221, true); -push_model(222, true); -push_model(223, true); -push_model(224, true); -push_model(225, false); -push_model(226, true); -push_model(227, true); -push_model(228, true); -push_model(229, true); -push_model(230, true); -push_model(231, false); -push_model(232, true); -push_model(233, true); -push_model(234, true); -push_model(235, false); -push_model(236, true); -push_model(237, true); -push_model(238, true); -push_model(239, true); -push_model(240, true); -push_model(241, true); -push_model(242, true); -push_model(243, true); -push_model(244, true); -push_model(245, true); -push_model(246, true); -push_model(247, true); -push_model(248, true); -push_model(249, false); -push_model(250, true); -push_model(251, true); -push_model(252, true); -push_model(253, true); -push_model(254, true); -push_model(255, true); -push_model(256, true); -push_model(257, true); -push_model(258, true); -push_model(259, true); -push_model(260, true); -push_model(261, true); -push_model(262, true); -push_model(263, false); -push_model(264, true); -push_model(265, true); -push_model(266, true); -push_model(267, true); -push_model(268, true); -push_model(269, false); -push_model(270, true); -push_model(271, true); -push_model(272, true); -push_model(273, false); -push_model(274, true); -push_model(275, true); -push_model(276, true); -push_model(277, true); -push_model(278, true); -push_model(279, true); -push_model(280, true); -push_model(281, true); -push_model(282, true); -push_model(283, true); -push_model(284, false); -push_model(285, true); -push_model(286, true); -push_model(287, true); -push_model(288, true); -push_model(289, true); -push_model(290, true); -push_model(291, true); -push_model(292, true); -push_model(293, true); -push_model(294, false); -push_model(295, true); -push_model(296, true); -push_model(297, true); -push_model(298, true); -push_model(299, true); -push_model(300, true); -push_model(301, false); -push_model(302, true); -push_model(303, true); -push_model(304, true); -push_model(305, false); -push_model(306, true); -push_model(307, true); -push_model(308, true); -push_model(309, true); -push_model(310, true); -push_model(311, true); -push_model(312, true); -push_model(313, true); -push_model(314, true); -push_model(315, true); -push_model(316, true); -push_model(317, true); -push_model(318, true); -push_model(319, false); -push_model(320, true); -push_model(321, true); -push_model(322, true); -push_model(323, true); -push_model(324, true); -push_model(325, true); -push_model(326, false); -push_model(327, true); -push_model(328, true); -push_model(329, true); -push_model(330, true); -push_model(331, true); -push_model(332, true); -push_model(333, true); -push_model(334, false); -push_model(335, true); -push_model(336, true); -push_model(337, true); -push_model(338, true); -push_model(339, true); -push_model(340, false); -push_model(341, true); -push_model(342, true); -push_model(343, true); -push_model(344, true); -push_model(345, true); -push_model(346, true); -push_model(347, true); -push_model(348, true); -push_model(349, true); -push_model(350, true); -push_model(351, true); -push_model(352, true); -push_model(353, false); -push_model(354, true); -push_model(355, true); -push_model(356, true); -push_model(357, true); -push_model(358, true); -push_model(359, true); -push_model(360, true); -push_model(361, true); -push_model(362, false); -push_model(363, false); -push_model(364, true); -push_model(365, true); -push_model(366, true); -push_model(367, true); -push_model(368, true); -push_model(369, true); -push_model(370, true); -push_model(371, true); -push_model(372, true); -push_model(373, true); -push_model(374, false); -push_model(375, true); -push_model(376, true); -push_model(377, true); -push_model(378, true); -push_model(379, true); -push_model(380, true); -push_model(381, true); -push_model(382, true); -push_model(383, true); -push_model(384, true); -push_model(385, true); -push_model(386, true); -push_model(387, true); -push_model(388, false); -push_model(389, true); -push_model(390, true); -push_model(391, true); -push_model(392, true); -push_model(393, true); -push_model(394, false); -push_model(395, true); -push_model(396, true); -push_model(397, true); -push_model(398, true); -push_model(399, true); -push_model(400, true); -push_model(401, false); -push_model(402, true); -push_model(403, true); -push_model(404, true); -push_model(405, true); -push_model(406, true); -push_model(407, true); -push_model(408, false); -push_model(409, true); -push_model(410, true); -push_model(411, true); -push_model(412, true); -push_model(413, true); -push_model(414, false); -push_model(415, true); -push_model(416, true); -push_model(417, true); -push_model(418, true); -push_model(419, true); -push_model(420, true); -push_model(421, true); -push_model(422, true); -push_model(423, true); -push_model(424, true); -push_model(425, true); -push_model(426, true); -push_model(427, true); -push_model(428, true); -push_model(429, false); -push_model(430, true); -push_model(431, false); -push_model(432, true); -push_model(433, true); -push_model(434, true); -push_model(435, true); -push_model(436, true); -push_model(437, true); -push_model(438, true); -push_model(439, true); -push_model(440, true); -push_model(441, true); -push_model(442, false); -push_model(443, true); -push_model(444, true); -push_model(445, true); -push_model(446, true); -push_model(447, true); -push_model(448, true); -push_model(449, true); -push_model(450, true); -push_model(451, true); -push_model(452, true); -push_model(453, false); -push_model(454, true); -push_model(455, true); -push_model(456, true); -push_model(457, true); -push_model(458, false); -push_model(459, true); -push_model(460, true); -push_model(461, true); -push_model(462, true); -push_model(463, true); -push_model(464, true); -push_model(465, true); -push_model(466, true); -push_model(467, false); -push_model(468, true); -push_model(469, true); -push_model(470, true); -push_model(471, true); -push_model(472, true); -push_model(473, true); -push_model(474, true); -push_model(475, true); -push_model(476, false); -push_model(477, true); -push_model(478, true); -push_model(479, true); -push_model(480, true); -push_model(481, true); -push_model(482, true); -push_model(483, true); -push_model(484, true); -push_model(485, false); -push_model(486, true); -push_model(487, true); -push_model(488, true); -push_model(489, true); -push_model(490, true); -push_model(491, true); -push_model(492, true); -push_model(493, true); -push_model(494, false); -push_model(495, true); -push_model(496, false); -push_model(497, true); -push_model(498, true); -push_model(499, true); -push_model(500, true); -push_model(501, true); -push_model(502, true); -push_model(503, true); -push_model(504, true); -push_model(505, false); -push_model(506, true); -push_model(507, true); -push_model(508, true); -push_model(509, true); -push_model(510, true); -push_model(511, true); -push_model(512, true); - -} void ccc::push_model(unsigned v, bool sign) { if (m_values.size() <= v) { @@ -796,11 +409,11 @@ lbool ccc::search() { ERROR_EX }; - set_model(); + // set_model(); m_cancel = false; - scoped_limits scoped_rlimit(s.rlimit()); + scoped_limits scoped_rlimit(m_s.rlimit()); vector limits; ptr_vector solvers; int finished_id = -1; @@ -808,18 +421,15 @@ lbool ccc::search() { par_exception_kind ex_kind; unsigned error_code = 0; lbool result = l_undef; - bool canceled = false; int num_threads = 2; // for ccc-infinity only two threads. s.m_config.m_num_threads + 1; - for (int i = 1; i < num_threads; ++i) { - limits.push_back(reslimit()); - } for (int i = 1; i < num_threads; ++i) { - s.m_params.set_uint("random_seed", s.m_rand()); - solver* s1 = alloc(sat::solver, s.m_params, limits[i-1]); + limits.push_back(reslimit()); + m_s.m_params.set_uint("random_seed", m_s.m_rand()); + solver* s1 = alloc(sat::solver, m_s.m_params, limits.back()); solvers.push_back(s1); - s1->copy(s); + s1->copy(m_s); scoped_rlimit.push_child(&s1->rlimit()); } @@ -828,7 +438,7 @@ lbool ccc::search() { try { lbool r = l_undef; if (i == 0) { - r = cube(); + r = cube2(); } else { r = conquer(*solvers[i-1]); @@ -884,7 +494,6 @@ lbool ccc::search() { } #endif - return result; } diff --git a/src/sat/sat_ccc.h b/src/sat/sat_ccc.h index da546bca1..7d6000c93 100644 --- a/src/sat/sat_ccc.h +++ b/src/sat/sat_ccc.h @@ -29,15 +29,16 @@ namespace sat { unsigned m_id; unsigned m_depth; literal m_last; + literal m_blocked; unsigned m_parent; - decision(unsigned id, unsigned d, literal last, unsigned parent_id): - m_id(id), m_depth(d), m_last(last), m_parent(parent_id) {} + decision(unsigned id, unsigned d, literal last, literal blocked, unsigned parent_id): + m_id(id), m_depth(d), m_last(last), m_blocked(blocked), m_parent(parent_id) {} decision(): m_id(0), m_depth(0), m_last(null_literal), m_parent(0) {} std::ostream& pp(std::ostream& out) const; }; - solver& s; + solver& m_s; queue m_solved; queue m_decisions; model m_model; @@ -56,8 +57,14 @@ namespace sat { }; lbool conquer(solver& s); + bool cube_decision(solver& s, svector& decisions); + lbool bounded_search(solver& s, svector& decisions); lbool cube(); + bool push_decision(solver& s, decision const& d); + + lbool cube2(); + lbool cube2(unsigned& branch_id, svector& decisions, lookahead& lh); void replay_decisions(solver& s, svector& decisions); @@ -69,9 +76,11 @@ namespace sat { void check_non_model(char const* fn, svector const& decisions); + bool contains_branch(svector const& decisions, unsigned branch_id) const; + public: - ccc(solver& s): s(s) {} + ccc(solver& s): m_s(s) {} lbool search(); diff --git a/src/sat/sat_lookahead.h b/src/sat/sat_lookahead.h index a801cdad7..a0b82cf45 100644 --- a/src/sat/sat_lookahead.h +++ b/src/sat/sat_lookahead.h @@ -161,6 +161,7 @@ namespace sat { lookahead_mode m_search_mode; // mode of search stats m_stats; model m_model; + literal m_blocked_literal; // --------------------------------------- // truth values @@ -1712,7 +1713,8 @@ namespace sat { if (trail.empty()) return false; pop(); flip_prefix(); - assign(~trail.back()); + m_blocked_literal = trail.back(); + assign(~m_blocked_literal); trail.pop_back(); propagate(); } diff --git a/src/sat/sat_scc.cpp b/src/sat/sat_scc.cpp index aa35363b6..ec77ebfbd 100644 --- a/src/sat/sat_scc.cpp +++ b/src/sat/sat_scc.cpp @@ -76,18 +76,18 @@ namespace sat { lowlink.resize(num_lits, UINT_MAX); in_s.resize(num_lits, false); literal_vector roots; - roots.resize(m_solver.num_vars(), null_literal); + roots.resize(m_solver.num_vars(), null_literal); unsigned next_index = 0; svector frames; bool_var_vector to_elim; - for (unsigned l_idx = 0; l_idx < num_lits; l_idx++) { - if (index[l_idx] != UINT_MAX) - continue; - if (m_solver.was_eliminated(to_literal(l_idx).var())) - continue; - - m_solver.checkpoint(); + for (unsigned l_idx = 0; l_idx < num_lits; l_idx++) { + if (index[l_idx] != UINT_MAX) + continue; + if (m_solver.was_eliminated(to_literal(l_idx).var())) + continue; + + m_solver.checkpoint(); #define NEW_NODE(LIDX) { \ index[LIDX] = next_index; \ @@ -99,121 +99,121 @@ namespace sat { frames.push_back(frame(LIDX, wlist.begin(), wlist.end())); \ } - NEW_NODE(l_idx); + NEW_NODE(l_idx); - while (!frames.empty()) { - loop: - frame & fr = frames.back(); - unsigned l_idx = fr.m_lidx; - if (!fr.m_first) { - // after visiting child - literal l2 = fr.m_it->get_literal(); - unsigned l2_idx = l2.index(); - SASSERT(index[l2_idx] != UINT_MAX); - if (lowlink[l2_idx] < lowlink[l_idx]) - lowlink[l_idx] = lowlink[l2_idx]; - fr.m_it++; - } - fr.m_first = false; - while (fr.m_it != fr.m_end) { - if (!fr.m_it->is_binary_clause()) { - fr.m_it++; - continue; - } - literal l2 = fr.m_it->get_literal(); - unsigned l2_idx = l2.index(); - if (index[l2_idx] == UINT_MAX) { - NEW_NODE(l2_idx); - goto loop; - } - else if (in_s[l2_idx]) { - if (index[l2_idx] < lowlink[l_idx]) - lowlink[l_idx] = index[l2_idx]; - } - fr.m_it++; - } - // visited all successors - if (lowlink[l_idx] == index[l_idx]) { - // found new SCC - CTRACE("scc_cycle", s.back() != l_idx, { - tout << "cycle: "; - unsigned j = s.size() - 1; - unsigned l2_idx; - do { - l2_idx = s[j]; - j--; - tout << to_literal(l2_idx) << " "; - } while (l2_idx != l_idx); - tout << "\n"; - }); + while (!frames.empty()) { + loop: + frame & fr = frames.back(); + unsigned l_idx = fr.m_lidx; + if (!fr.m_first) { + // after visiting child + literal l2 = fr.m_it->get_literal(); + unsigned l2_idx = l2.index(); + SASSERT(index[l2_idx] != UINT_MAX); + if (lowlink[l2_idx] < lowlink[l_idx]) + lowlink[l_idx] = lowlink[l2_idx]; + fr.m_it++; + } + fr.m_first = false; + while (fr.m_it != fr.m_end) { + if (!fr.m_it->is_binary_clause()) { + fr.m_it++; + continue; + } + literal l2 = fr.m_it->get_literal(); + unsigned l2_idx = l2.index(); + if (index[l2_idx] == UINT_MAX) { + NEW_NODE(l2_idx); + goto loop; + } + else if (in_s[l2_idx]) { + if (index[l2_idx] < lowlink[l_idx]) + lowlink[l_idx] = index[l2_idx]; + } + fr.m_it++; + } + // visited all successors + if (lowlink[l_idx] == index[l_idx]) { + // found new SCC + CTRACE("scc_cycle", s.back() != l_idx, { + tout << "cycle: "; + unsigned j = s.size() - 1; + unsigned l2_idx; + do { + l2_idx = s[j]; + j--; + tout << to_literal(l2_idx) << " "; + } while (l2_idx != l_idx); + tout << "\n"; + }); + + SASSERT(!s.empty()); + literal l = to_literal(l_idx); + bool_var v = l.var(); + if (roots[v] != null_literal) { + // variable was already assigned... just consume stack + TRACE("scc_detail", tout << "consuming stack...\n";); + unsigned l2_idx; + do { + l2_idx = s.back(); + s.pop_back(); + in_s[l2_idx] = false; + SASSERT(roots[to_literal(l2_idx).var()].var() == roots[v].var()); + } while (l2_idx != l_idx); + } + else { + // check if the SCC has an external variable, and check for conflicts + TRACE("scc_detail", tout << "assigning roots...\n";); + literal r = null_literal; + unsigned j = s.size() - 1; + unsigned l2_idx; + do { + l2_idx = s[j]; + j--; + if (to_literal(l2_idx) == ~l) { + m_solver.set_conflict(justification()); + return 0; + } + if (m_solver.is_external(to_literal(l2_idx).var())) { + r = to_literal(l2_idx); + break; + } + } while (l2_idx != l_idx); - SASSERT(!s.empty()); - literal l = to_literal(l_idx); - bool_var v = l.var(); - if (roots[v] != null_literal) { - // variable was already assigned... just consume stack - TRACE("scc_detail", tout << "consuming stack...\n";); - unsigned l2_idx; - do { - l2_idx = s.back(); - s.pop_back(); - in_s[l2_idx] = false; - SASSERT(roots[to_literal(l2_idx).var()].var() == roots[v].var()); - } while (l2_idx != l_idx); - } - else { - // check if the SCC has an external variable, and check for conflicts - TRACE("scc_detail", tout << "assigning roots...\n";); - literal r = null_literal; - unsigned j = s.size() - 1; - unsigned l2_idx; - do { - l2_idx = s[j]; - j--; - if (to_literal(l2_idx) == ~l) { - m_solver.set_conflict(justification()); - return 0; - } - if (m_solver.is_external(to_literal(l2_idx).var())) { - r = to_literal(l2_idx); - break; - } - } while (l2_idx != l_idx); + if (r == null_literal) { + // SCC does not contain external variable + r = to_literal(l_idx); + } - if (r == null_literal) { - // SCC does not contain external variable - r = to_literal(l_idx); - } + TRACE("scc_detail", tout << "r: " << r << "\n";); - TRACE("scc_detail", tout << "r: " << r << "\n";); - - do { - l2_idx = s.back(); - s.pop_back(); - in_s[l2_idx] = false; - literal l2 = to_literal(l2_idx); - bool_var v2 = l2.var(); - if (roots[v2] == null_literal) { - if (l2.sign()) { - roots[v2] = ~r; - } - else { - roots[v2] = r; - } - if (v2 != r.var()) - to_elim.push_back(v2); - } - } while (l2_idx != l_idx); - } - } - frames.pop_back(); - } - } - for (unsigned i = 0; i < m_solver.num_vars(); ++i) { - if (roots[i] == null_literal) { - roots[i] = literal(i, false); - } - } + do { + l2_idx = s.back(); + s.pop_back(); + in_s[l2_idx] = false; + literal l2 = to_literal(l2_idx); + bool_var v2 = l2.var(); + if (roots[v2] == null_literal) { + if (l2.sign()) { + roots[v2] = ~r; + } + else { + roots[v2] = r; + } + if (v2 != r.var()) + to_elim.push_back(v2); + } + } while (l2_idx != l_idx); + } + } + frames.pop_back(); + } + } + for (unsigned i = 0; i < m_solver.num_vars(); ++i) { + if (roots[i] == null_literal) { + roots[i] = literal(i, false); + } + } TRACE("scc", for (unsigned i = 0; i < roots.size(); i++) { tout << i << " -> " << roots[i] << "\n"; } tout << "to_elim: "; for (unsigned i = 0; i < to_elim.size(); i++) tout << to_elim[i] << " "; tout << "\n";); m_num_elim += to_elim.size(); From d052155f6eb817fbd1e915da001653bcf230b946 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sun, 23 Apr 2017 14:46:46 -0700 Subject: [PATCH 454/562] parallelizing ccc Signed-off-by: Nikolaj Bjorner --- src/sat/sat_ccc.cpp | 397 ++++++++++++++++------------------ src/sat/sat_ccc.h | 74 ++++--- src/sat/sat_lookahead.h | 6 +- src/sat/sat_solver.cpp | 11 +- src/sat/sat_solver.h | 2 +- src/smt/asserted_formulas.cpp | 32 +-- src/util/util.cpp | 16 ++ src/util/util.h | 27 ++- 8 files changed, 284 insertions(+), 281 deletions(-) diff --git a/src/sat/sat_ccc.cpp b/src/sat/sat_ccc.cpp index 141869f11..a282397be 100644 --- a/src/sat/sat_ccc.cpp +++ b/src/sat/sat_ccc.cpp @@ -15,6 +15,16 @@ Author: Notes: + The cube process spawns conquer threads to search parts of the + state space. + The conquer threads have two modes: + - emulation mode - where they try to outpace the cuber on the same search tree + - complement mode - where they solve a sub-branch not yet explored by the cuber. + When the conquer thread returns a solved cube it is processed in the following ways: + - ignore if solved_id \not\in decisions + - mark d as closed if d \in decisions, such that d is marked by solved id + - backjump otherwise, conquer thread has solved a branch attempted by the cuber + --*/ #include "sat_solver.h" @@ -23,9 +33,16 @@ Notes: using namespace sat; - std::ostream& ccc::decision::pp(std::ostream& out) const { - return out << "(" << m_id << " " << m_last << " d:" << m_depth << ") "; + out << "(" + << " id:" << m_id + << " l:" << m_literal + << " d:" << m_depth; + if (m_spawn_id != 0) { + out << " s:" << m_spawn_id; + } + out << ") "; + return out; } std::ostream& ccc::pp(std::ostream& out, svector const& v) { @@ -35,9 +52,9 @@ std::ostream& ccc::pp(std::ostream& out, svector const& v) { return out; } -lbool ccc::cube2() { - unsigned branch_id = 0; - unsigned_vector id_trail; +lbool ccc::cube() { + m_branch_id = 0; + m_last_closure_level = UINT_MAX; lookahead lh(m_s); lh.init_search(); @@ -47,41 +64,26 @@ lbool ccc::cube2() { literal_vector trail; svector decisions; lh.m_search_mode = lookahead_mode::searching; - lh.m_blocked_literal = null_literal; - lbool r = cube2(branch_id, decisions, lh); + lbool r = cube(decisions, lh); if (r == l_true) { m_model = lh.get_model(); } + lh.collect_statistics(m_stats); return r; } -lbool ccc::cube2(unsigned& branch_id, svector& decisions, lookahead& lh) { +lbool ccc::cube(svector& decisions, lookahead& lh) { m_s.checkpoint(); if (lh.inconsistent()) { return l_false; } - lh.inc_istamp(); - - // check if CDCL solver got ahead. - bool repeat = false; - #pragma omp critical (ccc_solved) - { - while (!m_solved.empty()) { - unsigned solved_id = m_solved.top(); - if (contains_branch(decisions, solved_id)) { - IF_VERBOSE(1, verbose_stream() << "conquer " << decisions.size() << "\n";); - repeat = true; - break; - } - else { - m_solved.pop(); - } - } + if (get_solved(decisions)) { + return l_false; } - if (repeat) return l_false; - + + lh.inc_istamp(); literal l = lh.choose(); if (lh.inconsistent()) { return l_false; @@ -92,34 +94,38 @@ lbool ccc::cube2(unsigned& branch_id, svector& decisions, lookahead& l } if (!decisions.empty()) { - #pragma omp critical (ccc_decisions) - { - m_decisions.push(decisions.back()); - } + put_decision(decisions.back()); } - // update trail and set of ids + // update trail and decisions - ++branch_id; ++lh.m_stats.m_decisions; unsigned parent_id = decisions.empty() ? 0 : decisions.back().m_id; - decision d(branch_id, decisions.size() + 1, l, null_literal, parent_id); + unsigned spawn_id = spawn_conquer(decisions); + unsigned branch_id = ++m_branch_id; + decision d(branch_id, decisions.size() + 1, l, parent_id, spawn_id); decisions.push_back(d); - #pragma omp critical (ccc_log) - { - IF_VERBOSE(1, verbose_stream() << "select " << pp_prefix(lh.m_prefix, lh.m_trail_lim.size()) << ": " << l << " " << lh.m_trail.size() << "\n";); - IF_VERBOSE(2, pp(verbose_stream(), decisions) << "\n"; ); - } - TRACE("sat", tout << "choose: " << l << " " << trail << "\n";); + IF_VERBOSE(1, verbose_stream() << "select " << pp_prefix(lh.m_prefix, lh.m_trail_lim.size()) << ": " << l << " " << lh.m_trail.size() << "\n";); + IF_VERBOSE(2, pp(verbose_stream(), decisions) << "\n"; ); + + TRACE("sat", tout << "choose: " << l << "\n";); lh.push(l, lh.c_fixed_truth); - lbool r = cube2(branch_id, decisions, lh); + lbool r = cube(decisions, lh); if (r == l_false) { lh.pop(); - lh.flip_prefix(); - lh.push(~l, lh.c_fixed_truth); - decisions.back().m_last = ~l; - r = cube2(branch_id, decisions, lh); + if (decisions.back().is_closed()) { + // branch was solved by a spawned conquer process + IF_VERBOSE(1, verbose_stream() << "closed " << decisions.back().m_id << "\n";); + + r = l_false; + } + else { + lh.flip_prefix(); + lh.push(~l, lh.c_fixed_truth); + decisions.back().negate(); + r = cube(decisions, lh); + } if (r == l_false) { lh.pop(); decisions.pop_back(); @@ -128,106 +134,26 @@ lbool ccc::cube2(unsigned& branch_id, svector& decisions, lookahead& l return r; } -bool ccc::contains_branch(svector const& decisions, unsigned branch_id) const { - for (unsigned i = 0; i < decisions.size(); ++i) { - if (branch_id == decisions[i].m_id) return true; +unsigned ccc::spawn_conquer(svector const& decisions) { + unsigned result = 0; + // + // decisions must have been solved at a higher level by a conquer thread + // + if (!m_free_threads.empty() && m_last_closure_level <= 1 + decisions.size() + m_free_threads.size()) { + result = m_free_threads.back(); + m_free_threads.pop_back(); + IF_VERBOSE(1, verbose_stream() << "spawn " << result << "\n";); } - return false; + return result; +} + +void ccc::free_conquer(unsigned thread_id) { + m_free_threads.push_back(thread_id); } -lbool ccc::cube() { - unsigned branch_id = 0; - unsigned_vector id_trail; - - lookahead lh(m_s); - lh.init_search(); - lh.m_model.reset(); - - lookahead::scoped_level _sl(lh, lh.c_fixed_truth); - literal_vector trail; - svector decisions; - lh.m_search_mode = lookahead_mode::searching; - lh.m_blocked_literal = null_literal; - while (!m_cancel) { - - m_s.checkpoint(); - - SASSERT(trail.size() <= decisions.size()); - while (trail.size() < decisions.size()) { - //check_non_model("lh inconsistent ", decisions); - decisions.pop_back(); - id_trail.pop_back(); - } - SASSERT(id_trail.size() == trail.size()); - SASSERT(id_trail.size() == decisions.size()); - - TRACE("sat", lh.display(tout);); - - if (lh.inconsistent()) { - if (!lh.backtrack(trail)) return l_false; - continue; - } - - lh.inc_istamp(); - - // check if CDCL solver got ahead. - bool repeat = false; - #pragma omp critical (ccc_solved) - { - if (!m_solved.empty()) { - unsigned solved_id = m_solved.top(); - if (id_trail.contains(solved_id)) { - IF_VERBOSE(1, verbose_stream() << "cconquer " << decisions.size() << "\n";); - lh.set_conflict(); - } - else { - m_solved.pop(); - } - repeat = true; - } - } - if (repeat) continue; - - literal l = lh.choose(); - if (lh.inconsistent()) { - if (!lh.backtrack(trail)) return l_false; - continue; - } - if (l == null_literal) { - m_model = lh.get_model(); - return l_true; - } - - // update trail and set of ids - - ++branch_id; - ++lh.m_stats.m_decisions; - unsigned parent_id = id_trail.empty() ? 0 : id_trail.back(); - decision d(branch_id, trail.size() + 1, l, lh.m_blocked_literal, parent_id); - id_trail.push_back(branch_id); - trail.push_back(l); - decisions.push_back(d); - SASSERT(id_trail.size() == trail.size()); - lh.m_blocked_literal = null_literal; - - #pragma omp critical (ccc_log) - { - IF_VERBOSE(1, verbose_stream() << "select " << pp_prefix(lh.m_prefix, lh.m_trail_lim.size()) << ": " << l << " " << lh.m_trail.size() << "\n";); - IF_VERBOSE(2, verbose_stream() << " " << trail << "\n"; pp(verbose_stream(), decisions) << "\n"; ); - } - #pragma omp critical (ccc_decisions) - { - m_decisions.push(d); - } - TRACE("sat", tout << "choose: " << l << " " << trail << "\n";); - lh.push(l, lh.c_fixed_truth); - SASSERT(lh.inconsistent() || !lh.is_unsat()); - } - return l_undef; -} - -lbool ccc::conquer(solver& s) { +lbool ccc::conquer(solver& s, unsigned thread_id) { + SASSERT(thread_id > 0); try { if (s.inconsistent()) return l_false; s.init_search(); @@ -242,7 +168,7 @@ lbool ccc::conquer(solver& s) { while (true) { SASSERT(!s.inconsistent()); - lbool r = bounded_search(s, decisions); + lbool r = bounded_search(s, decisions, thread_id); if (r != l_undef) return r; @@ -257,17 +183,13 @@ lbool ccc::conquer(solver& s) { } } -void ccc::replay_decisions(solver& s, svector& decisions) { +void ccc::replay_decisions(solver& s, svector& decisions, unsigned thread_id) { s.propagate(true); for (unsigned i = s.scope_lvl(); !s.inconsistent() && i < decisions.size(); ++i) { - decision d = decisions[i]; - literal lit = d.m_last; - lbool val = s.value(lit); - #pragma omp critical (ccc_log) - { - IF_VERBOSE(2, verbose_stream() << "replay " << lit << " " << val << "\n";); - } - if (!push_decision(s, d)) { + decision const& d = decisions[i]; + IF_VERBOSE(2, verbose_stream() << "replay " << d.get_literal(thread_id) << " " << s.value(d.get_literal(thread_id)) << "\n";); + + if (!push_decision(s, d, thread_id)) { // negation of decision is implied. // check_non_model("replay", decisions); decisions.resize(i); @@ -276,13 +198,73 @@ void ccc::replay_decisions(solver& s, svector& decisions) { } } -bool ccc::push_decision(solver& s, decision const& d) { - literal lit = d.m_last; +bool ccc::get_solved(svector& decisions) { + // check if CDCL solver got ahead. + bool found = false; + #pragma omp critical (ccc_solved) + { + while (!m_solved.empty()) { + solution const& sol = m_solved.top(); + unsigned branch_id = sol.m_branch_id; + unsigned thread_id = sol.m_thread_id; + SASSERT(thread_id > 0); + for (unsigned i = decisions.size(); i > 0; ) { + --i; + decision& d = decisions[i]; + if (branch_id == d.m_id) { + if (d.m_spawn_id == thread_id) { + SASSERT(d.m_spawn_id > 0); + free_conquer(thread_id); + IF_VERBOSE(1, verbose_stream() << "close " << i << "\n";); + d.close(); + } + else { + // IF_VERBOSE(1, verbose_stream() << "conquer " << branch_id << " " << i << " " << d.get_literal(thread_id) << "\n";); + found = true; + } + m_last_closure_level = d.m_depth; + break; + } + } + if (found) { + break; + } + // IF_VERBOSE(1, verbose_stream() << "not found: " << branch_id << " " << decisions.size() << "\n";); + m_solved.pop(); + } + } + return found; +} + +void ccc::put_decision(decision const& d) { + #pragma omp critical (ccc_decisions) + { + for (unsigned i = 0; i < m_num_conquer; ++i) { + m_decisions[i].push(d); + } + } +} + +bool ccc::get_decision(unsigned thread_id, decision& d) { + SASSERT(0 < thread_id && thread_id <= m_decisions.size()); + bool result = false; + #pragma omp critical (ccc_decisions) + { + if (!m_decisions[thread_id - 1].empty()) { + d = m_decisions[thread_id - 1].pop(); + result = true; + } + } + return result; +} + +bool ccc::push_decision(solver& s, decision const& d, unsigned thread_id) { + literal lit = d.get_literal(thread_id); switch (s.value(lit)) { case l_false: #pragma omp critical (ccc_solved) { - m_solved.push(d.m_id); + m_solved.push(solution(thread_id, d.m_id)); } //TBD: s.m_restart_threshold = s.m_config.m_restart_initial; @@ -297,40 +279,16 @@ bool ccc::push_decision(solver& s, decision const& d) { s.propagate(true); break; } - literal blocked = d.m_blocked; - if (false && blocked != null_literal) { - switch (s.value(blocked)) { - case l_false: - #pragma omp critical (ccc_solved) - { - m_solved.push(d.m_id); - } - return false; - case l_true: - break; - case l_undef: - //s.assign(blocked, justification()); - //s.propagate(true); - break; - } - } return true; } -bool ccc::cube_decision(solver& s, svector& decisions) { +bool ccc::cube_decision(solver& s, svector& decisions, unsigned thread_id) { decision d; bool use_cube_decision = false; SASSERT(s.m_qhead == s.m_trail.size()); - get_cube: - #pragma omp critical (ccc_decisions) - { - if (!m_decisions.empty()) { - d = m_decisions.pop(); - use_cube_decision = true; - } - } - if (!use_cube_decision) { + get_cube: + if (!get_decision(thread_id, d)) { return false; } @@ -338,42 +296,45 @@ bool ccc::cube_decision(solver& s, svector& decisions) { goto get_cube; } + if (!decisions.empty() && decisions.back().m_spawn_id == thread_id && decisions.back().m_depth < d.m_depth) { + goto get_cube; + } + while (!decisions.empty() && decisions.back().m_depth >= d.m_depth) { // check_non_model("cube decision", decisions); decisions.pop_back(); } + SASSERT(decisions.empty() || decisions.back().m_depth + 1 == d.m_depth); SASSERT(decisions.empty() || decisions.back().m_id == d.m_parent); s.pop_reinit(s.scope_lvl() - decisions.size()); SASSERT(s.m_qhead == s.m_trail.size()); SASSERT(s.scope_lvl() == decisions.size()); - #pragma omp critical (ccc_log) - { - literal lit = d.m_last; - IF_VERBOSE(1, verbose_stream() << "cube " << decisions.size() << "\n";); - IF_VERBOSE(2, pp(verbose_stream() << "push ", decisions) << " @ " << s.scope_lvl() << " " << s.value(lit) << "\n"; - if (s.value(lit) == l_false) verbose_stream() << "level: " << s.lvl(lit) << "\n";); - } - if (push_decision(s, d)) { + literal lit = d.get_literal(thread_id); + IF_VERBOSE(1, verbose_stream() << "cube " << decisions.size() << " " << d.get_literal(thread_id) << "\n";); + IF_VERBOSE(2, pp(verbose_stream() << "push ", decisions) << " @ " << s.scope_lvl() << " " << s.value(lit) << "\n"; + if (s.value(lit) == l_false) verbose_stream() << "level: " << s.lvl(lit) << "\n";); + + if (push_decision(s, d, thread_id)) { decisions.push_back(d); } return true; } -lbool ccc::bounded_search(solver& s, svector& decisions) { +lbool ccc::bounded_search(solver& s, svector& decisions, unsigned thread_id) { while (true) { s.checkpoint(); bool done = false; while (!done) { - replay_decisions(s, decisions); + replay_decisions(s, decisions, thread_id); lbool is_sat = s.propagate_and_backjump_step(done); if (is_sat != l_true) return is_sat; } s.gc(); - if (!cube_decision(s, decisions) && !s.decide()) { + if (!cube_decision(s, decisions, thread_id) && !s.decide()) { lbool is_sat = s.final_check(); if (is_sat != l_undef) { return is_sat; @@ -383,26 +344,6 @@ lbool ccc::bounded_search(solver& s, svector& decisions) { } -void ccc::push_model(unsigned v, bool sign) { - if (m_values.size() <= v) { - m_values.resize(v + 1); - } - m_values[v] = sign; -} - -void ccc::check_non_model(char const* fn, svector const& decisions) { - for (unsigned i = 0; i < decisions.size(); ++i) { - decision d = decisions[i]; - literal lit = d.m_last; - if (m_values[lit.var()] != lit.sign()) return; - } - - #pragma omp critical (ccc_log) - { - pp(verbose_stream() << "backtracking from model " << fn << " ", decisions) << "\n"; - } -} - lbool ccc::search() { enum par_exception_kind { DEFAULT_EX, @@ -421,8 +362,10 @@ lbool ccc::search() { par_exception_kind ex_kind; unsigned error_code = 0; lbool result = l_undef; + m_decisions.reset(); - int num_threads = 2; // for ccc-infinity only two threads. s.m_config.m_num_threads + 1; + m_num_conquer = m_s.m_config.m_num_threads; + int num_threads = 1 + m_num_conquer; // for ccc-infinity only two threads. for (int i = 1; i < num_threads; ++i) { limits.push_back(reslimit()); @@ -431,6 +374,10 @@ lbool ccc::search() { solvers.push_back(s1); s1->copy(m_s); scoped_rlimit.push_child(&s1->rlimit()); + m_decisions.push_back(queue()); + } + for (unsigned i = 1; i < m_num_conquer; ++i) { + m_free_threads.push_back(i); } #pragma omp parallel for @@ -438,10 +385,10 @@ lbool ccc::search() { try { lbool r = l_undef; if (i == 0) { - r = cube2(); + r = cube(); } else { - r = conquer(*solvers[i-1]); + r = conquer(*solvers[i-1], i); } bool first = false; #pragma omp critical (par_solver) @@ -476,6 +423,7 @@ lbool ccc::search() { } for (unsigned i = 0; i < solvers.size(); ++i) { + solvers[i]->collect_statistics(m_stats); dealloc(solvers[i]); } @@ -497,3 +445,22 @@ lbool ccc::search() { return result; } + +#if 0 +void ccc::push_model(unsigned v, bool sign) { + if (m_values.size() <= v) { + m_values.resize(v + 1); + } + m_values[v] = sign; +} + +void ccc::check_non_model(char const* fn, svector const& decisions) { + for (unsigned i = 0; i < decisions.size(); ++i) { + decision d = decisions[i]; + literal lit = d.m_literal; + if (m_values[lit.var()] != lit.sign()) return; + } + + IF_VERBOSE(1, pp(verbose_stream() << "backtracking from model " << fn << " ", decisions) << "\n";); +} +#endif diff --git a/src/sat/sat_ccc.h b/src/sat/sat_ccc.h index 7d6000c93..21291c1aa 100644 --- a/src/sat/sat_ccc.h +++ b/src/sat/sat_ccc.h @@ -26,47 +26,55 @@ namespace sat { class ccc { struct decision { - unsigned m_id; - unsigned m_depth; - literal m_last; - literal m_blocked; - unsigned m_parent; - decision(unsigned id, unsigned d, literal last, literal blocked, unsigned parent_id): - m_id(id), m_depth(d), m_last(last), m_blocked(blocked), m_parent(parent_id) {} - decision(): m_id(0), m_depth(0), m_last(null_literal), m_parent(0) {} - + unsigned m_id; // unique identifier for decision + unsigned m_depth; // depth of decision + literal m_literal; // decision literal + unsigned m_parent; // id of parent + int m_spawn_id; // thread id of conquer thread processing complented branch. + // = 0 if not spawned. + // > 0 if active spawn is in progress + // < 0 if thread has closed the branch + decision(unsigned id, unsigned d, literal last, unsigned parent_id, unsigned spawn): + m_id(id), m_depth(d), m_literal(last), m_parent(parent_id), m_spawn_id(spawn) {} + decision(): + m_id(0), m_depth(0), m_literal(null_literal), m_parent(0), m_spawn_id(0) {} + + void close() { SASSERT(m_spawn_id > 0); m_spawn_id = -m_spawn_id; } + bool is_closed() const { return m_spawn_id < 0; } + void negate() { m_literal.neg(); m_spawn_id = 0; } + literal get_literal(unsigned thread_id) const { return thread_id == m_spawn_id ? ~m_literal : m_literal; } std::ostream& pp(std::ostream& out) const; }; + struct solution { + unsigned m_thread_id; + unsigned m_branch_id; + solution(unsigned t, unsigned s): m_thread_id(t), m_branch_id(s) {} + }; + solver& m_s; - queue m_solved; - queue m_decisions; + queue m_solved; + vector > m_decisions; + unsigned m_num_conquer; model m_model; volatile bool m_cancel; + unsigned m_branch_id; + unsigned_vector m_free_threads; + unsigned m_last_closure_level; + ::statistics m_stats; - svector m_values; + lbool conquer(solver& s, unsigned thread_id); + bool cube_decision(solver& s, svector& decisions, unsigned thread_id); - struct config { - config() { - } - }; - - struct stats { - stats() { reset(); } - void reset() { memset(this, 0, sizeof(*this)); } - }; - - lbool conquer(solver& s); - bool cube_decision(solver& s, svector& decisions); - - lbool bounded_search(solver& s, svector& decisions); + lbool bounded_search(solver& s, svector& decisions, unsigned thread_id); + bool push_decision(solver& s, decision const& d, unsigned thread_id); lbool cube(); - bool push_decision(solver& s, decision const& d); + lbool cube(svector& decisions, lookahead& lh); + void put_decision(decision const& d); + bool get_decision(unsigned thread_id, decision& d); + bool get_solved(svector& decisions); - lbool cube2(); - lbool cube2(unsigned& branch_id, svector& decisions, lookahead& lh); - - void replay_decisions(solver& s, svector& decisions); + void replay_decisions(solver& s, svector& decisions, unsigned thread_id); static std::ostream& pp(std::ostream& out, svector const& v); @@ -76,7 +84,8 @@ namespace sat { void check_non_model(char const* fn, svector const& decisions); - bool contains_branch(svector const& decisions, unsigned branch_id) const; + unsigned spawn_conquer(svector const& decisions); + void free_conquer(unsigned thread_id); public: @@ -86,6 +95,7 @@ namespace sat { model const& get_model() const { return m_model; } + void collect_statistics(::statistics& st) { st.copy(m_stats); } }; } diff --git a/src/sat/sat_lookahead.h b/src/sat/sat_lookahead.h index a0b82cf45..0cc4678dd 100644 --- a/src/sat/sat_lookahead.h +++ b/src/sat/sat_lookahead.h @@ -161,8 +161,7 @@ namespace sat { lookahead_mode m_search_mode; // mode of search stats m_stats; model m_model; - literal m_blocked_literal; - + // --------------------------------------- // truth values @@ -1713,8 +1712,7 @@ namespace sat { if (trail.empty()) return false; pop(); flip_prefix(); - m_blocked_literal = trail.back(); - assign(~m_blocked_literal); + assign(~trail.back()); trail.pop_back(); propagate(); } diff --git a/src/sat/sat_solver.cpp b/src/sat/sat_solver.cpp index 4647ddb36..fa8663db8 100644 --- a/src/sat/sat_solver.cpp +++ b/src/sat/sat_solver.cpp @@ -874,7 +874,7 @@ namespace sat { scoped_rl.push_child(&srch.rlimit()); lbool r = srch.check(num_lits, lits, 0); m_model = srch.get_model(); - // srch.collect_statistics(m_lookahead_stats); + // srch.collect_statistics(m_aux_stats); return r; } @@ -882,6 +882,7 @@ namespace sat { ccc c(*this); lbool r = c.search(); m_model = c.get_model(); + c.collect_statistics(m_aux_stats); return r; } @@ -893,10 +894,10 @@ namespace sat { m_model = lh.get_model(); } catch (z3_exception&) { - lh.collect_statistics(m_lookahead_stats); + lh.collect_statistics(m_aux_stats); throw; } - lh.collect_statistics(m_lookahead_stats); + lh.collect_statistics(m_aux_stats); return r; } @@ -2808,7 +2809,7 @@ namespace sat { m_asymm_branch.collect_statistics(st); m_probing.collect_statistics(st); if (m_ext) m_ext->collect_statistics(st); - st.copy(m_lookahead_stats); + st.copy(m_aux_stats); } void solver::reset_statistics() { @@ -2817,7 +2818,7 @@ namespace sat { m_simplifier.reset_statistics(); m_asymm_branch.reset_statistics(); m_probing.reset_statistics(); - m_lookahead_stats.reset(); + m_aux_stats.reset(); } // ----------------------- diff --git a/src/sat/sat_solver.h b/src/sat/sat_solver.h index fda8362ca..dec32f11a 100644 --- a/src/sat/sat_solver.h +++ b/src/sat/sat_solver.h @@ -141,7 +141,7 @@ namespace sat { unsigned m_par_num_vars; bool m_par_syncing_clauses; - statistics m_lookahead_stats; + statistics m_aux_stats; void del_clauses(clause * const * begin, clause * const * end); diff --git a/src/smt/asserted_formulas.cpp b/src/smt/asserted_formulas.cpp index 26395f9ab..6598c3a05 100644 --- a/src/smt/asserted_formulas.cpp +++ b/src/smt/asserted_formulas.cpp @@ -288,7 +288,7 @@ void asserted_formulas::reduce() { } void asserted_formulas::eliminate_and() { - IF_IVERBOSE(10, verbose_stream() << "(smt.eliminating-and)\n";); + IF_VERBOSE(10, verbose_stream() << "(smt.eliminating-and)\n";); set_eliminate_and(true); reduce_asserted_formulas(); TRACE("after_elim_and", display(tout);); @@ -393,19 +393,19 @@ void asserted_formulas::find_macros_core() { } void asserted_formulas::find_macros() { - IF_IVERBOSE(10, verbose_stream() << "(smt.find-macros)\n";); + IF_VERBOSE(10, verbose_stream() << "(smt.find-macros)\n";); TRACE("before_find_macros", display(tout);); find_macros_core(); TRACE("after_find_macros", display(tout);); } void asserted_formulas::expand_macros() { - IF_IVERBOSE(10, verbose_stream() << "(smt.expand-macros)\n";); + IF_VERBOSE(10, verbose_stream() << "(smt.expand-macros)\n";); find_macros_core(); } void asserted_formulas::apply_quasi_macros() { - IF_IVERBOSE(10, verbose_stream() << "(smt.find-quasi-macros)\n";); + IF_VERBOSE(10, verbose_stream() << "(smt.find-quasi-macros)\n";); TRACE("before_quasi_macros", display(tout);); expr_ref_vector new_exprs(m_manager); proof_ref_vector new_prs(m_manager); @@ -423,7 +423,7 @@ void asserted_formulas::apply_quasi_macros() { } void asserted_formulas::nnf_cnf() { - IF_IVERBOSE(10, verbose_stream() << "(smt.nnf)\n";); + IF_VERBOSE(10, verbose_stream() << "(smt.nnf)\n";); nnf apply_nnf(m_manager, m_defined_names); expr_ref_vector new_exprs(m_manager); proof_ref_vector new_prs(m_manager); @@ -473,7 +473,7 @@ void asserted_formulas::nnf_cnf() { #define MK_SIMPLE_SIMPLIFIER(NAME, FUNCTOR_DEF, LABEL, MSG) \ void asserted_formulas::NAME() { \ - IF_IVERBOSE(10, verbose_stream() << "(smt." << MSG << ")\n";); \ + IF_VERBOSE(10, verbose_stream() << "(smt." << MSG << ")\n";); \ TRACE(LABEL, tout << "before:\n"; display(tout);); \ FUNCTOR_DEF; \ expr_ref_vector new_exprs(m_manager); \ @@ -508,13 +508,13 @@ void asserted_formulas::NAME() { MK_SIMPLE_SIMPLIFIER(apply_distribute_forall, distribute_forall functor(m_manager, *m_bsimp), "distribute_forall", "distribute-forall"); void asserted_formulas::reduce_and_solve() { - IF_IVERBOSE(10, verbose_stream() << "(smt.reducing)\n";); + IF_VERBOSE(10, verbose_stream() << "(smt.reducing)\n";); flush_cache(); // collect garbage reduce_asserted_formulas(); } void asserted_formulas::infer_patterns() { - IF_IVERBOSE(10, verbose_stream() << "(smt.pattern-inference)\n";); + IF_VERBOSE(10, verbose_stream() << "(smt.pattern-inference)\n";); TRACE("before_pattern_inference", display(tout);); pattern_inference infer(m_manager, m_params); expr_ref_vector new_exprs(m_manager); @@ -552,7 +552,7 @@ void asserted_formulas::commit(unsigned new_qhead) { } void asserted_formulas::eliminate_term_ite() { - IF_IVERBOSE(10, verbose_stream() << "(smt.eliminating-ite-term)\n";); + IF_VERBOSE(10, verbose_stream() << "(smt.eliminating-ite-term)\n";); TRACE("before_elim_term_ite", display(tout);); elim_term_ite elim(m_manager, m_defined_names); expr_ref_vector new_exprs(m_manager); @@ -589,7 +589,7 @@ void asserted_formulas::eliminate_term_ite() { } void asserted_formulas::propagate_values() { - IF_IVERBOSE(10, verbose_stream() << "(smt.constant-propagation)\n";); + IF_VERBOSE(10, verbose_stream() << "(smt.constant-propagation)\n";); TRACE("propagate_values", tout << "before:\n"; display(tout);); flush_cache(); bool found = false; @@ -673,7 +673,7 @@ void asserted_formulas::propagate_booleans() { flush_cache(); while (cont) { TRACE("propagate_booleans", tout << "before:\n"; display(tout);); - IF_IVERBOSE(10, verbose_stream() << "(smt.propagate-booleans)\n";); + IF_VERBOSE(10, verbose_stream() << "(smt.propagate-booleans)\n";); cont = false; unsigned i = m_asserted_qhead; unsigned sz = m_asserted_formulas.size(); @@ -716,7 +716,7 @@ void asserted_formulas::propagate_booleans() { #define MK_SIMPLIFIER(NAME, FUNCTOR, TAG, MSG, REDUCE) \ bool asserted_formulas::NAME() { \ - IF_IVERBOSE(10, verbose_stream() << "(smt." << MSG << ")\n";); \ + IF_VERBOSE(10, verbose_stream() << "(smt." << MSG << ")\n";); \ TRACE(TAG, ast_mark visited; display_ll(tout, visited);); \ FUNCTOR; \ bool changed = false; \ @@ -773,7 +773,7 @@ proof * asserted_formulas::get_inconsistency_proof() const { } void asserted_formulas::refine_inj_axiom() { - IF_IVERBOSE(10, verbose_stream() << "(smt.refine-injectivity)\n";); + IF_VERBOSE(10, verbose_stream() << "(smt.refine-injectivity)\n";); TRACE("inj_axiom", display(tout);); unsigned i = m_asserted_qhead; unsigned sz = m_asserted_formulas.size(); @@ -805,7 +805,7 @@ MK_SIMPLIFIER(elim_bvs_from_quantifiers, bv_elim_star functor(m_manager), "bv_el #define LIFT_ITE(NAME, FUNCTOR, MSG) \ void asserted_formulas::NAME() { \ - IF_IVERBOSE(10, verbose_stream() << "(smt." << MSG << ")\n";); \ + IF_VERBOSE(10, verbose_stream() << "(smt." << MSG << ")\n";); \ TRACE("lift_ite", display(tout);); \ FUNCTOR; \ unsigned i = m_asserted_qhead; \ @@ -817,7 +817,7 @@ void asserted_formulas::NAME() { proof_ref new_pr(m_manager); \ functor(n, new_n, new_pr); \ TRACE("lift_ite_step", tout << mk_pp(n, m_manager) << "\n";); \ - IF_IVERBOSE(10000, verbose_stream() << "lift before: " << get_num_exprs(n) << ", after: " << get_num_exprs(new_n) << "\n";); \ + IF_VERBOSE(10000, verbose_stream() << "lift before: " << get_num_exprs(n) << ", after: " << get_num_exprs(new_n) << "\n";); \ m_asserted_formulas.set(i, new_n); \ if (m_manager.proofs_enabled()) { \ new_pr = m_manager.mk_modus_ponens(pr, new_pr); \ @@ -841,7 +841,7 @@ unsigned asserted_formulas::get_total_size() const { } void asserted_formulas::max_bv_sharing() { - IF_IVERBOSE(10, verbose_stream() << "(smt.maximizing-bv-sharing)\n";); + IF_VERBOSE(10, verbose_stream() << "(smt.maximizing-bv-sharing)\n";); TRACE("bv_sharing", display(tout);); unsigned i = m_asserted_qhead; unsigned sz = m_asserted_formulas.size(); diff --git a/src/util/util.cpp b/src/util/util.cpp index bfd4923a8..50d93913a 100644 --- a/src/util/util.cpp +++ b/src/util/util.cpp @@ -17,6 +17,9 @@ Revision History: --*/ +#ifdef _WINDOWS +#include "windows.h" +#endif #include"util.h" static unsigned g_verbosity_level = 0; @@ -35,6 +38,19 @@ void set_verbose_stream(std::ostream& str) { g_verbose_stream = &str; } +static int g_thread_id = 0; +static bool g_is_threaded = false; + +bool is_threaded() { + if (g_is_threaded) return true; +#ifdef _WINDOWS + int thid = GetCurrentThreadId(); + g_is_threaded = g_thread_id != thid && g_thread_id != 0; + g_thread_id = thid; +#endif + return g_is_threaded; +} + std::ostream& verbose_stream() { return *g_verbose_stream; } diff --git a/src/util/util.h b/src/util/util.h index a040a79ae..e62f24e44 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -24,6 +24,7 @@ Revision History: #include #include #include +#include"z3_omp.h" #ifndef SIZE_MAX #define SIZE_MAX std::numeric_limits::max() @@ -182,16 +183,26 @@ void set_verbosity_level(unsigned lvl); unsigned get_verbosity_level(); std::ostream& verbose_stream(); void set_verbose_stream(std::ostream& str); +bool is_threaded(); -#define IF_VERBOSE(LVL, CODE) { if (get_verbosity_level() >= LVL) { CODE } } ((void) 0) - -#ifdef _EXTERNAL_RELEASE -#define IF_IVERBOSE(LVL, CODE) ((void) 0) -#else -#define IF_IVERBOSE(LVL, CODE) { if (get_verbosity_level() >= LVL) { CODE } } ((void) 0) -#endif - + +#define IF_VERBOSE(LVL, CODE) { \ + if (get_verbosity_level() >= LVL) { \ + if (is_threaded()) { \ + LOCK_CODE(CODE); \ + } \ + else { \ + CODE; \ + } \ + } } ((void) 0) +#define LOCK_CODE(CODE) \ + { \ + __pragma(omp critical (verbose_lock)) \ + { \ + CODE; \ + } \ + } template struct default_eq { From 3aaea6b920de7aa2a1c446b04c6aac1d2559be6d Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sun, 23 Apr 2017 23:10:23 -0700 Subject: [PATCH 455/562] parallelizing ccc Signed-off-by: Nikolaj Bjorner --- src/sat/sat_ccc.cpp | 24 +++++++---- src/sat/sat_ccc.h | 2 +- src/sat/sat_lookahead.h | 88 ++++++++++++++++++++++++++--------------- 3 files changed, 73 insertions(+), 41 deletions(-) diff --git a/src/sat/sat_ccc.cpp b/src/sat/sat_ccc.cpp index a282397be..b0657fd64 100644 --- a/src/sat/sat_ccc.cpp +++ b/src/sat/sat_ccc.cpp @@ -35,7 +35,7 @@ using namespace sat; std::ostream& ccc::decision::pp(std::ostream& out) const { out << "(" - << " id:" << m_id + << "id:" << m_id << " l:" << m_literal << " d:" << m_depth; if (m_spawn_id != 0) { @@ -187,9 +187,9 @@ void ccc::replay_decisions(solver& s, svector& decisions, unsigned thr s.propagate(true); for (unsigned i = s.scope_lvl(); !s.inconsistent() && i < decisions.size(); ++i) { decision const& d = decisions[i]; - IF_VERBOSE(2, verbose_stream() << "replay " << d.get_literal(thread_id) << " " << s.value(d.get_literal(thread_id)) << "\n";); + IF_VERBOSE(2, verbose_stream() << thread_id << ": replay " << d.get_literal(thread_id) << " " << s.value(d.get_literal(thread_id)) << "\n";); - if (!push_decision(s, d, thread_id)) { + if (!push_decision(s, decisions, d, thread_id)) { // negation of decision is implied. // check_non_model("replay", decisions); decisions.resize(i); @@ -214,7 +214,7 @@ bool ccc::get_solved(svector& decisions) { if (branch_id == d.m_id) { if (d.m_spawn_id == thread_id) { SASSERT(d.m_spawn_id > 0); - free_conquer(thread_id); + free_conquer(thread_id); IF_VERBOSE(1, verbose_stream() << "close " << i << "\n";); d.close(); } @@ -258,10 +258,18 @@ bool ccc::get_decision(unsigned thread_id, decision& d) { return result; } -bool ccc::push_decision(solver& s, decision const& d, unsigned thread_id) { +bool ccc::push_decision(solver& s, svector const& decisions, decision const& d, unsigned thread_id) { literal lit = d.get_literal(thread_id); switch (s.value(lit)) { case l_false: + // TBD: we leak conquer threads if they backjump below spawn point. + if (decisions.empty() && decisions.back().m_spawn_id == thread_id && decisions.back().m_id != d.m_id) { + IF_VERBOSE(0, verbose_stream() << "LEAK avoided\n";); + #pragma omp critical (ccc_solved) + { + m_solved.push(solution(thread_id, decisions.back().m_id)); + } + } #pragma omp critical (ccc_solved) { m_solved.push(solution(thread_id, d.m_id)); @@ -311,11 +319,11 @@ bool ccc::cube_decision(solver& s, svector& decisions, unsigned thread SASSERT(s.m_qhead == s.m_trail.size()); SASSERT(s.scope_lvl() == decisions.size()); literal lit = d.get_literal(thread_id); - IF_VERBOSE(1, verbose_stream() << "cube " << decisions.size() << " " << d.get_literal(thread_id) << "\n";); - IF_VERBOSE(2, pp(verbose_stream() << "push ", decisions) << " @ " << s.scope_lvl() << " " << s.value(lit) << "\n"; + IF_VERBOSE(1, verbose_stream() << thread_id << ": cube " << decisions.size() << " " << d.get_literal(thread_id) << "\n";); + IF_VERBOSE(2, pp(verbose_stream() << thread_id << ": push ", decisions) << " @ " << s.scope_lvl() << " " << s.value(lit) << "\n"; if (s.value(lit) == l_false) verbose_stream() << "level: " << s.lvl(lit) << "\n";); - if (push_decision(s, d, thread_id)) { + if (push_decision(s, decisions, d, thread_id)) { decisions.push_back(d); } diff --git a/src/sat/sat_ccc.h b/src/sat/sat_ccc.h index 21291c1aa..b4d497fa0 100644 --- a/src/sat/sat_ccc.h +++ b/src/sat/sat_ccc.h @@ -67,7 +67,7 @@ namespace sat { bool cube_decision(solver& s, svector& decisions, unsigned thread_id); lbool bounded_search(solver& s, svector& decisions, unsigned thread_id); - bool push_decision(solver& s, decision const& d, unsigned thread_id); + bool push_decision(solver& s, svector const& decisions, decision const& d, unsigned thread_id); lbool cube(); lbool cube(svector& decisions, lookahead& lh); void put_decision(decision const& d); diff --git a/src/sat/sat_lookahead.h b/src/sat/sat_lookahead.h index 0cc4678dd..c2763b776 100644 --- a/src/sat/sat_lookahead.h +++ b/src/sat/sat_lookahead.h @@ -59,7 +59,9 @@ namespace sat { } class lookahead { - solver& s; + solver& m_s; + unsigned m_num_vars; + reslimit m_rlimit; friend class ccc; @@ -243,14 +245,20 @@ namespace sat { m_binary[(~l2).index()].push_back(l1); m_binary_trail.push_back((~l1).index()); ++m_stats.m_add_binary; - if (s.m_config.m_drat) validate_binary(l1, l2); + if (m_s.m_config.m_drat) validate_binary(l1, l2); } void del_binary(unsigned idx) { // TRACE("sat", display(tout << "Delete " << to_literal(idx) << "\n");); literal_vector & lits = m_binary[idx]; - literal l = lits.back(); + if (lits.empty()) IF_VERBOSE(0, verbose_stream() << "empty literals\n";); + literal l = lits.back(); lits.pop_back(); + if (m_binary[(~l).index()].back() != ~to_literal(idx)) { + IF_VERBOSE(0, verbose_stream() << "pop bad literal: " << idx << " " << (~l).index() << "\n";); + } + if (m_binary[(~l).index()].empty()) + IF_VERBOSE(0, verbose_stream() << "empty binary\n";); m_binary[(~l).index()].pop_back(); ++m_stats.m_del_binary; } @@ -547,7 +555,7 @@ namespace sat { void ensure_H(unsigned level) { while (m_H.size() <= level) { m_H.push_back(svector()); - m_H.back().resize(s.num_vars() * 2, 0); + m_H.back().resize(m_num_vars * 2, 0); } } @@ -574,6 +582,9 @@ namespace sat { float sum = 0, tsum = 0; literal_vector::iterator it = m_binary[l.index()].begin(), end = m_binary[l.index()].end(); for (; it != end; ++it) { + bool_var v = it->var(); + if (it->index() >= h.size()) + IF_VERBOSE(0, verbose_stream() << l << " " << *it << " " << h.size() << "\n";); if (is_undef(*it)) sum += h[it->index()]; // if (m_freevars.contains(it->var())) sum += h[it->index()]; } @@ -593,7 +604,7 @@ namespace sat { } case watched::CLAUSE: { clause_offset cls_off = wit->get_clause_offset(); - clause & c = *(s.m_cls_allocator.get_clause(cls_off)); + clause & c = *(m_cls_allocator.get_clause(cls_off)); // approximation compared to ternary clause case: // we pick two other literals from the clause. if (c[0] == ~l) { @@ -1026,26 +1037,26 @@ namespace sat { m_lits.push_back(lit_info()); m_rating.push_back(0); m_vprefix.push_back(prefix()); - if (!s.was_eliminated(v)) + if (!m_s.was_eliminated(v)) m_freevars.insert(v); } void init() { - m_delta_trigger = s.num_vars()/10; + m_delta_trigger = m_num_vars/10; m_config.m_dl_success = 0.8; m_inconsistent = false; m_qhead = 0; m_bstamp_id = 0; - for (unsigned i = 0; i < s.num_vars(); ++i) { + for (unsigned i = 0; i < m_num_vars; ++i) { init_var(i); } // copy binary clauses - unsigned sz = s.m_watches.size(); + unsigned sz = m_s.m_watches.size(); for (unsigned l_idx = 0; l_idx < sz; ++l_idx) { literal l = ~to_literal(l_idx); - watch_list const & wlist = s.m_watches[l_idx]; + watch_list const & wlist = m_s.m_watches[l_idx]; watch_list::const_iterator it = wlist.begin(); watch_list::const_iterator end = wlist.end(); for (; it != end; ++it) { @@ -1057,21 +1068,21 @@ namespace sat { } } - copy_clauses(s.m_clauses); - copy_clauses(s.m_learned); + copy_clauses(m_s.m_clauses); + copy_clauses(m_s.m_learned); // copy units - unsigned trail_sz = s.init_trail_size(); + unsigned trail_sz = m_s.init_trail_size(); for (unsigned i = 0; i < trail_sz; ++i) { - literal l = s.m_trail[i]; - if (!s.was_eliminated(l.var())) { - if (s.m_config.m_drat) m_drat.add(l, false); + literal l = m_s.m_trail[i]; + if (!m_s.was_eliminated(l.var())) { + if (m_s.m_config.m_drat) m_drat.add(l, false); assign(l); } } propagate(); m_qhead = m_trail.size(); - TRACE("sat", s.display(tout); display(tout);); + TRACE("sat", m_s.display(tout); display(tout);); } void copy_clauses(clause_vector const& clauses) { @@ -1087,7 +1098,7 @@ namespace sat { for (unsigned i = 0; i < c.size(); ++i) { m_full_watches[(~c[i]).index()].push_back(c1); } - if (s.m_config.m_drat) m_drat.add(c, false); + if (m_s.m_config.m_drat) m_drat.add(c, false); } } @@ -1207,7 +1218,7 @@ namespace sat { clause const& get_clause(watch_list::iterator it) const { clause_offset cls_off = it->get_clause_offset(); - return *(s.m_cls_allocator.get_clause(cls_off)); + return *(m_cls_allocator.get_clause(cls_off)); } bool is_nary_propagation(clause const& c, literal l) const { @@ -1288,7 +1299,7 @@ namespace sat { break; } clause_offset cls_off = it->get_clause_offset(); - clause & c = *(s.m_cls_allocator.get_clause(cls_off)); + clause & c = *(m_cls_allocator.get_clause(cls_off)); if (c[0] == ~l) std::swap(c[0], c[1]); if (is_true(c[0])) { @@ -1426,7 +1437,7 @@ namespace sat { while (change && !inconsistent()) { change = false; for (unsigned i = 0; !inconsistent() && i < m_lookahead.size(); ++i) { - s.checkpoint(); + checkpoint(); literal lit = m_lookahead[i].m_lit; if (is_fixed_at(lit, c_fixed_truth)) continue; unsigned level = base + m_lookahead[i].m_offset; @@ -1501,7 +1512,7 @@ namespace sat { float mixd = mix_diff(diff1, diff2); if (mixd == h) ++count; - if (mixd > h || (mixd == h && s.m_rand(count) == 0)) { + if (mixd > h || (mixd == h && m_s.m_rand(count) == 0)) { CTRACE("sat", l != null_literal, tout << lit << " mix diff: " << mixd << "\n";); if (mixd > h) count = 1; h = mixd; @@ -1666,7 +1677,7 @@ namespace sat { unsigned scope_lvl() const { return m_trail_lim.size(); } void validate_assign(literal l) { - if (s.m_config.m_drat && m_search_mode == lookahead_mode::searching) { + if (m_s.m_config.m_drat && m_search_mode == lookahead_mode::searching) { m_assumptions.push_back(l); m_drat.add(m_assumptions); m_assumptions.pop_back(); @@ -1727,7 +1738,7 @@ namespace sat { while (true) { TRACE("sat", display(tout);); inc_istamp(); - s.checkpoint(); + checkpoint(); if (inconsistent()) { if (!backtrack(trail)) return l_false; continue; @@ -1751,7 +1762,7 @@ namespace sat { void init_model() { m_model.reset(); - for (unsigned i = 0; i < s.num_vars(); ++i) { + for (unsigned i = 0; i < m_num_vars; ++i) { lbool val; literal lit(i, false); if (is_undef(lit)) { @@ -1810,17 +1821,30 @@ namespace sat { init(); } + void checkpoint() { + if (!m_rlimit.inc()) { + throw solver_exception(Z3_CANCELED_MSG); + } + if (memory::get_allocation_size() > m_s.m_config.m_max_memory) { + throw solver_exception(Z3_MAX_MEMORY_MSG); + } + } + + public: lookahead(solver& s) : - s(s), + m_s(s), + m_num_vars(s.num_vars()), m_drat(s), m_num_tc1(0), m_level(2), m_prefix(0) { + m_s.rlimit().push_child(&m_rlimit); } ~lookahead() { del_clauses(); + m_s.rlimit().pop_child(); } lbool check() { @@ -1842,14 +1866,14 @@ namespace sat { unsigned num_units = 0; for (unsigned i = 0; i < m_trail.size(); ++i) { literal lit = m_trail[i]; - if (s.value(lit) == l_undef && !s.was_eliminated(lit.var())) { - s.m_simplifier.propagate_unit(lit); + if (m_s.value(lit) == l_undef && !m_s.was_eliminated(lit.var())) { + m_s.m_simplifier.propagate_unit(lit); ++num_units; } } IF_VERBOSE(1, verbose_stream() << "units found: " << num_units << "\n";); - s.m_simplifier.subsume(); + m_s.m_simplifier.subsume(); m_lookahead.reset(); } @@ -1868,20 +1892,20 @@ namespace sat { if (inconsistent()) return; literal_vector roots; bool_var_vector to_elim; - for (unsigned i = 0; i < s.num_vars(); ++i) { + for (unsigned i = 0; i < m_num_vars; ++i) { roots.push_back(literal(i, false)); } for (unsigned i = 0; i < m_candidates.size(); ++i) { bool_var v = m_candidates[i].m_var; literal lit = literal(v, false); literal p = get_parent(lit); - if (p != null_literal && p.var() != v && !s.is_external(v) && !s.was_eliminated(v) && !s.was_eliminated(p.var())) { + if (p != null_literal && p.var() != v && !m_s.is_external(v) && !m_s.was_eliminated(v) && !m_s.was_eliminated(p.var())) { to_elim.push_back(v); roots[v] = p; } } IF_VERBOSE(1, verbose_stream() << "eliminate " << to_elim.size() << " variables\n";); - elim_eqs elim(s); + elim_eqs elim(m_s); elim(roots, to_elim); } m_lookahead.reset(); From ce67c8277c62b10cbb295266c0c6cc939dc86910 Mon Sep 17 00:00:00 2001 From: Bruce Collie Date: Mon, 24 Apr 2017 12:59:44 +0000 Subject: [PATCH 456/562] Return check result in fixedpoint object This is a small change to fix a missing return statement. --- src/api/c++/z3++.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/c++/z3++.h b/src/api/c++/z3++.h index b6157f3ff..9d9982523 100644 --- a/src/api/c++/z3++.h +++ b/src/api/c++/z3++.h @@ -2244,7 +2244,7 @@ namespace z3 { void from_file(char const* s) { Z3_fixedpoint_from_file(ctx(), m_fp, s); check_error(); } void add_rule(expr& rule, symbol const& name) { Z3_fixedpoint_add_rule(ctx(), m_fp, rule, name); check_error(); } void add_fact(func_decl& f, unsigned * args) { Z3_fixedpoint_add_fact(ctx(), m_fp, f, f.arity(), args); check_error(); } - check_result query(expr& q) { Z3_lbool r = Z3_fixedpoint_query(ctx(), m_fp, q); check_error(); to_check_result(r); } + check_result query(expr& q) { Z3_lbool r = Z3_fixedpoint_query(ctx(), m_fp, q); check_error(); return to_check_result(r); } check_result query(func_decl_vector& relations) { array rs(relations); Z3_lbool r = Z3_fixedpoint_query_relations(ctx(), m_fp, rs.size(), rs.ptr()); From 81ba729aab8d2181244de354f0bdb1ea8415691a Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Mon, 24 Apr 2017 15:25:45 +0100 Subject: [PATCH 457/562] [Doxygen] Fix script `--help` functionality. --- doc/mk_api_doc.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/mk_api_doc.py b/doc/mk_api_doc.py index edabcbd1b..bb5a19e5f 100644 --- a/doc/mk_api_doc.py +++ b/doc/mk_api_doc.py @@ -13,11 +13,13 @@ ML_ENABLED=False BUILD_DIR='../build' def display_help(exit_code): + assert isinstance(exit_code, int) print("mk_api_doc.py: Z3 documentation generator\n") print("\nOptions:") print(" -h, --help display this message.") print(" -b , --build= subdirectory where Z3 is built (default: ../build).") print(" --ml include ML/OCaml API documentation.") + sys.exit(exit_code) def parse_options(): global ML_ENABLED, BUILD_DIR @@ -34,8 +36,7 @@ def parse_options(): if opt in ('-b', '--build'): BUILD_DIR = mk_util.norm_path(arg) elif opt in ('h', '--help'): - display_help() - exit(1) + display_help(0) elif opt in ('--ml'): ML_ENABLED=True else: @@ -128,7 +129,7 @@ try: print("Generated ML/OCaml documentation.") print("Documentation was successfully generated at subdirectory './api/html'.") -except: +except Exception: exctype, value = sys.exc_info()[:2] print("ERROR: failed to generate documentation: %s" % value) exit(1) From ca678c3675a7aa1bd5b396b15660b7ab1163c193 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Mon, 24 Apr 2017 15:45:57 +0100 Subject: [PATCH 458/562] [Doxygen] Fix bug where `def_Type` directives in `z3.h` would appear in generated doxygen documentation. --- doc/mk_api_doc.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/mk_api_doc.py b/doc/mk_api_doc.py index bb5a19e5f..e86205a7a 100644 --- a/doc/mk_api_doc.py +++ b/doc/mk_api_doc.py @@ -50,15 +50,16 @@ def mk_dir(d): if not os.path.exists(d): os.makedirs(d) -# Eliminate def_API and extra_API directives from file 'inf'. +# Eliminate def_API, extra_API, and def_Type directives from file 'inf'. # The result is stored in 'outf'. def cleanup_API(inf, outf): pat1 = re.compile(".*def_API.*") pat2 = re.compile(".*extra_API.*") + pat3 = re.compile(r".*def_Type\(.*") _inf = open(inf, 'r') _outf = open(outf, 'w') for line in _inf: - if not pat1.match(line) and not pat2.match(line): + if not pat1.match(line) and not pat2.match(line) and not pat3.match(line): _outf.write(line) try: From 07ef79d66458b758114f0b2777b3e0a0bd134bb2 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Mon, 24 Apr 2017 08:36:33 -0700 Subject: [PATCH 459/562] parallelizing ccc Signed-off-by: Nikolaj Bjorner --- src/sat/sat_ccc.cpp | 71 +++++++++++++++++++++++++---------------- src/sat/sat_ccc.h | 22 +++++++++++-- src/sat/sat_lookahead.h | 4 +++ 3 files changed, 67 insertions(+), 30 deletions(-) diff --git a/src/sat/sat_ccc.cpp b/src/sat/sat_ccc.cpp index b0657fd64..eec7e313e 100644 --- a/src/sat/sat_ccc.cpp +++ b/src/sat/sat_ccc.cpp @@ -54,7 +54,7 @@ std::ostream& ccc::pp(std::ostream& out, svector const& v) { lbool ccc::cube() { m_branch_id = 0; - m_last_closure_level = UINT_MAX; + m_last_closure_level = 1000; lookahead lh(m_s); lh.init_search(); @@ -68,7 +68,7 @@ lbool ccc::cube() { if (r == l_true) { m_model = lh.get_model(); } - lh.collect_statistics(m_stats); + lh.collect_statistics(m_lh_stats); return r; } @@ -102,8 +102,9 @@ lbool ccc::cube(svector& decisions, lookahead& lh) { ++lh.m_stats.m_decisions; unsigned parent_id = decisions.empty() ? 0 : decisions.back().m_id; unsigned spawn_id = spawn_conquer(decisions); - unsigned branch_id = ++m_branch_id; - decision d(branch_id, decisions.size() + 1, l, parent_id, spawn_id); + unsigned branch1 = m_branch_id++; + unsigned branch2 = m_branch_id++; + decision d(branch1, decisions.size() + 1, l, parent_id, spawn_id); decisions.push_back(d); IF_VERBOSE(1, verbose_stream() << "select " << pp_prefix(lh.m_prefix, lh.m_trail_lim.size()) << ": " << l << " " << lh.m_trail.size() << "\n";); @@ -116,39 +117,50 @@ lbool ccc::cube(svector& decisions, lookahead& lh) { lh.pop(); if (decisions.back().is_closed()) { // branch was solved by a spawned conquer process - IF_VERBOSE(1, verbose_stream() << "closed " << decisions.back().m_id << "\n";); - + IF_VERBOSE(0, verbose_stream() << "closed " << decisions.back().m_id << "\n";); r = l_false; + decisions.pop_back(); } else { + lh.inc_istamp(); lh.flip_prefix(); lh.push(~l, lh.c_fixed_truth); decisions.back().negate(); + decisions.back().m_id = branch2; r = cube(decisions, lh); - } - if (r == l_false) { - lh.pop(); - decisions.pop_back(); + if (r == l_false) { + lh.pop(); + decisions.pop_back(); + } } } return r; } +void ccc::update_closure_level(decision const& d, int offset) { + m_last_closure_level = (d.m_depth + 3*m_last_closure_level) / 4 + offset; +} + unsigned ccc::spawn_conquer(svector const& decisions) { unsigned result = 0; // // decisions must have been solved at a higher level by a conquer thread // - if (!m_free_threads.empty() && m_last_closure_level <= 1 + decisions.size() + m_free_threads.size()) { + if (m_ccc_stats.m_cdcl_closed < 10) { + return 0; + } + if (!m_free_threads.empty() && m_last_closure_level >= decisions.size()) { result = m_free_threads.back(); m_free_threads.pop_back(); - IF_VERBOSE(1, verbose_stream() << "spawn " << result << "\n";); + IF_VERBOSE(0, verbose_stream() << "spawn " << decisions.size() << " " << result << "\n";); } return result; } void ccc::free_conquer(unsigned thread_id) { - m_free_threads.push_back(thread_id); + if (thread_id != 0) { + m_free_threads.push_back(thread_id); + } } @@ -208,21 +220,32 @@ bool ccc::get_solved(svector& decisions) { unsigned branch_id = sol.m_branch_id; unsigned thread_id = sol.m_thread_id; SASSERT(thread_id > 0); + free_conquer(thread_id); for (unsigned i = decisions.size(); i > 0; ) { --i; decision& d = decisions[i]; if (branch_id == d.m_id) { - if (d.m_spawn_id == thread_id) { + if (d.m_spawn_id == thread_id && thread_id != 0) { SASSERT(d.m_spawn_id > 0); - free_conquer(thread_id); - IF_VERBOSE(1, verbose_stream() << "close " << i << "\n";); + IF_VERBOSE(0, verbose_stream() << "spawn close " << branch_id << " " << thread_id << " " << d.m_depth << "\n";); + ++m_ccc_stats.m_spawn_closed; d.close(); + update_closure_level(d, -1); } else { - // IF_VERBOSE(1, verbose_stream() << "conquer " << branch_id << " " << i << " " << d.get_literal(thread_id) << "\n";); + IF_VERBOSE(0, verbose_stream() << "conquer " << branch_id << " " << thread_id << " " << d.m_depth << " " << d.get_literal(thread_id) << "\n";); found = true; + ++m_ccc_stats.m_cdcl_closed; + update_closure_level(d, 1); } - m_last_closure_level = d.m_depth; + break; + } + // branch is even, d has moved to the next branch + if (branch_id == (d.m_id & ~0x1) && d.m_spawn_id == thread_id && thread_id != 0) { + IF_VERBOSE(0, verbose_stream() << "spawn conquer " << branch_id << " " << thread_id << " " << d.m_depth << "\n";); + found = true; + ++m_ccc_stats.m_cdcl_closed; + update_closure_level(d, 1); break; } } @@ -233,6 +256,7 @@ bool ccc::get_solved(svector& decisions) { m_solved.pop(); } } + return found; } @@ -262,14 +286,7 @@ bool ccc::push_decision(solver& s, svector const& decisions, decision literal lit = d.get_literal(thread_id); switch (s.value(lit)) { case l_false: - // TBD: we leak conquer threads if they backjump below spawn point. - if (decisions.empty() && decisions.back().m_spawn_id == thread_id && decisions.back().m_id != d.m_id) { - IF_VERBOSE(0, verbose_stream() << "LEAK avoided\n";); - #pragma omp critical (ccc_solved) - { - m_solved.push(solution(thread_id, decisions.back().m_id)); - } - } + thread_id = (d.m_spawn_id == thread_id || (!decisions.empty() && decisions.back().m_spawn_id == thread_id)) ? thread_id : 0; #pragma omp critical (ccc_solved) { m_solved.push(solution(thread_id, d.m_id)); @@ -431,7 +448,7 @@ lbool ccc::search() { } for (unsigned i = 0; i < solvers.size(); ++i) { - solvers[i]->collect_statistics(m_stats); + solvers[i]->collect_statistics(m_lh_stats); dealloc(solvers[i]); } diff --git a/src/sat/sat_ccc.h b/src/sat/sat_ccc.h index b4d497fa0..29bf3c18d 100644 --- a/src/sat/sat_ccc.h +++ b/src/sat/sat_ccc.h @@ -41,7 +41,7 @@ namespace sat { void close() { SASSERT(m_spawn_id > 0); m_spawn_id = -m_spawn_id; } bool is_closed() const { return m_spawn_id < 0; } - void negate() { m_literal.neg(); m_spawn_id = 0; } + void negate() { m_literal.neg(); } literal get_literal(unsigned thread_id) const { return thread_id == m_spawn_id ? ~m_literal : m_literal; } std::ostream& pp(std::ostream& out) const; }; @@ -52,6 +52,15 @@ namespace sat { solution(unsigned t, unsigned s): m_thread_id(t), m_branch_id(s) {} }; + struct stats { + unsigned m_spawn_closed; + unsigned m_cdcl_closed; + stats() { reset(); } + void reset() { + memset(this, 0, sizeof(*this)); + } + }; + solver& m_s; queue m_solved; vector > m_decisions; @@ -61,7 +70,8 @@ namespace sat { unsigned m_branch_id; unsigned_vector m_free_threads; unsigned m_last_closure_level; - ::statistics m_stats; + ::statistics m_lh_stats; + stats m_ccc_stats; lbool conquer(solver& s, unsigned thread_id); bool cube_decision(solver& s, svector& decisions, unsigned thread_id); @@ -74,6 +84,8 @@ namespace sat { bool get_decision(unsigned thread_id, decision& d); bool get_solved(svector& decisions); + void update_closure_level(decision const& d, int offset); + void replay_decisions(solver& s, svector& decisions, unsigned thread_id); static std::ostream& pp(std::ostream& out, svector const& v); @@ -95,7 +107,11 @@ namespace sat { model const& get_model() const { return m_model; } - void collect_statistics(::statistics& st) { st.copy(m_stats); } + void collect_statistics(::statistics& st) { + st.copy(m_lh_stats); + st.update("ccc-spawn-closed", m_ccc_stats.m_spawn_closed); + st.update("ccc-cdcl-closed", m_ccc_stats.m_cdcl_closed); + } }; } diff --git a/src/sat/sat_lookahead.h b/src/sat/sat_lookahead.h index c2763b776..72e64ae1d 100644 --- a/src/sat/sat_lookahead.h +++ b/src/sat/sat_lookahead.h @@ -344,6 +344,9 @@ namespace sat { void try_add_binary(literal u, literal v) { SASSERT(m_search_mode == lookahead_mode::searching); SASSERT(u.var() != v.var()); + if (!is_undef(u) || !is_undef(v)) { + IF_VERBOSE(0, verbose_stream() << "adding assigned binary " << v << " " << u << "\n";); + } set_bstamps(~u); if (is_stamped(~v)) { TRACE("sat", tout << "try_add_binary: " << u << "\n";); @@ -1120,6 +1123,7 @@ namespace sat { } void pop() { + if (m_assumptions.empty()) IF_VERBOSE(0, verbose_stream() << "empty pop\n";); m_assumptions.pop_back(); m_inconsistent = false; SASSERT(m_search_mode == lookahead_mode::searching); From c46f95a629c07ae2e983ce68ab25f255a0197137 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 24 Apr 2017 12:39:55 -0400 Subject: [PATCH 460/562] remove unused parameter from smt_context --- src/smt/smt_context.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 3d0652093..f003dfa37 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -76,7 +76,6 @@ namespace smt { m_unsat_proof(m), m_unknown("unknown"), m_unsat_core(m), - m_use_theory_str_overlap_assumption(false), #ifdef Z3DEBUG m_trail_enabled(true), #endif From 8ce93b4ee528776bba150a7fa88d10bce790b777 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 24 Apr 2017 15:39:25 -0400 Subject: [PATCH 461/562] unify tracing in theory_str to 'str' tag --- src/smt/theory_str.cpp | 832 ++++++++++++++++++++--------------------- 1 file changed, 416 insertions(+), 416 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index bddd0b78e..01123a22c 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -174,7 +174,7 @@ void theory_str::assert_axiom(expr * e) { } if (get_manager().is_true(e)) return; - TRACE("t_str_detail", tout << "asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); + TRACE("str", tout << "asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); context & ctx = get_context(); if (!ctx.b_internalized(e)) { ctx.internalize(e, false); @@ -186,7 +186,7 @@ void theory_str::assert_axiom(expr * e) { // crash/error avoidance: add all axioms to the trail m_trail.push_back(e); - //TRACE("t_str_detail", tout << "done asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); + //TRACE("str", tout << "done asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); } expr * theory_str::rewrite_implication(expr * premise, expr * conclusion) { @@ -196,7 +196,7 @@ expr * theory_str::rewrite_implication(expr * premise, expr * conclusion) { void theory_str::assert_implication(expr * premise, expr * conclusion) { ast_manager & m = get_manager(); - TRACE("t_str_detail", tout << "asserting implication " << mk_ismt2_pp(premise, m) << " -> " << mk_ismt2_pp(conclusion, m) << std::endl;); + TRACE("str", tout << "asserting implication " << mk_ismt2_pp(premise, m) << " -> " << mk_ismt2_pp(conclusion, m) << std::endl;); expr_ref axiom(m.mk_or(m.mk_not(premise), conclusion), m); assert_axiom(axiom); } @@ -210,7 +210,7 @@ bool theory_str::internalize_term(app * term) { ast_manager & m = get_manager(); SASSERT(term->get_family_id() == get_family_id()); - TRACE("t_str_detail", tout << "internalizing term: " << mk_ismt2_pp(term, get_manager()) << std::endl;); + TRACE("str", tout << "internalizing term: " << mk_ismt2_pp(term, get_manager()) << std::endl;); // emulation of user_smt_theory::internalize_term() @@ -234,14 +234,14 @@ bool theory_str::internalize_term(app * term) { for (unsigned i = 0; i < num_args; ++i) { enode * arg = e->get_arg(i); theory_var v_arg = mk_var(arg); - TRACE("t_str_detail", tout << "arg has theory var #" << v_arg << std::endl;); + TRACE("str", tout << "arg has theory var #" << v_arg << std::endl;); } theory_var v = mk_var(e); - TRACE("t_str_detail", tout << "term has theory var #" << v << std::endl;); + TRACE("str", tout << "term has theory var #" << v << std::endl;); if (opt_EagerStringConstantLengthAssertions && u.str.is_string(term)) { - TRACE("t_str", tout << "eagerly asserting length of string term " << mk_pp(term, m) << std::endl;); + TRACE("str", tout << "eagerly asserting length of string term " << mk_pp(term, m) << std::endl;); m_basicstr_axiom_todo.insert(e); } return true; @@ -260,23 +260,23 @@ enode* theory_str::ensure_enode(expr* e) { void theory_str::refresh_theory_var(expr * e) { enode * en = ensure_enode(e); theory_var v = mk_var(en); - TRACE("t_str_detail", tout << "refresh " << mk_pp(e, get_manager()) << ": v#" << v << std::endl;); + TRACE("str", tout << "refresh " << mk_pp(e, get_manager()) << ": v#" << v << std::endl;); m_basicstr_axiom_todo.push_back(en); } theory_var theory_str::mk_var(enode* n) { - TRACE("t_str_detail", tout << "mk_var for " << mk_pp(n->get_owner(), get_manager()) << std::endl;); + TRACE("str", tout << "mk_var for " << mk_pp(n->get_owner(), get_manager()) << std::endl;); ast_manager & m = get_manager(); if (!(m.get_sort(n->get_owner()) == u.str.mk_string_sort())) { return null_theory_var; } if (is_attached_to_var(n)) { - TRACE("t_str_detail", tout << "already attached to theory var" << std::endl;); + TRACE("str", tout << "already attached to theory var" << std::endl;); return n->get_th_var(get_id()); } else { theory_var v = theory::mk_var(n); m_find.mk_var(); - TRACE("t_str_detail", tout << "new theory var v#" << v << std::endl;); + TRACE("str", tout << "new theory var v#" << v << std::endl;); get_context().attach_th_var(n, this, v); get_context().mark_as_relevant(n); return v; @@ -320,14 +320,14 @@ void theory_str::add_cut_info_one_node(expr * baseNode, int slevel, expr * node) varInfo->vars[node] = 1; cut_var_map.insert(baseNode, std::stack()); cut_var_map[baseNode].push(varInfo); - TRACE("t_str_cut_var_map", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); + TRACE("str", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); } else { if (cut_var_map[baseNode].empty()) { T_cut * varInfo = alloc(T_cut); varInfo->level = slevel; varInfo->vars[node] = 1; cut_var_map[baseNode].push(varInfo); - TRACE("t_str_cut_var_map", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); + TRACE("str", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); } else { if (cut_var_map[baseNode].top()->level < slevel) { T_cut * varInfo = alloc(T_cut); @@ -335,10 +335,10 @@ void theory_str::add_cut_info_one_node(expr * baseNode, int slevel, expr * node) cut_vars_map_copy(varInfo->vars, cut_var_map[baseNode].top()->vars); varInfo->vars[node] = 1; cut_var_map[baseNode].push(varInfo); - TRACE("t_str_cut_var_map", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); + TRACE("str", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); } else if (cut_var_map[baseNode].top()->level == slevel) { cut_var_map[baseNode].top()->vars[node] = 1; - TRACE("t_str_cut_var_map", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); + TRACE("str", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); } else { get_manager().raise_exception("entered illegal state during add_cut_info_one_node()"); } @@ -364,7 +364,7 @@ void theory_str::add_cut_info_merge(expr * destNode, int slevel, expr * srcNode) cut_vars_map_copy(varInfo->vars, cut_var_map[srcNode].top()->vars); cut_var_map.insert(destNode, std::stack()); cut_var_map[destNode].push(varInfo); - TRACE("t_str_cut_var_map", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << " [" << slevel << "]" << std::endl;); + TRACE("str", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << " [" << slevel << "]" << std::endl;); } else { if (cut_var_map[destNode].empty() || cut_var_map[destNode].top()->level < slevel) { T_cut * varInfo = alloc(T_cut); @@ -372,10 +372,10 @@ void theory_str::add_cut_info_merge(expr * destNode, int slevel, expr * srcNode) cut_vars_map_copy(varInfo->vars, cut_var_map[destNode].top()->vars); cut_vars_map_copy(varInfo->vars, cut_var_map[srcNode].top()->vars); cut_var_map[destNode].push(varInfo); - TRACE("t_str_cut_var_map", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << " [" << slevel << "]" << std::endl;); + TRACE("str", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << " [" << slevel << "]" << std::endl;); } else if (cut_var_map[destNode].top()->level == slevel) { cut_vars_map_copy(cut_var_map[destNode].top()->vars, cut_var_map[srcNode].top()->vars); - TRACE("t_str_cut_var_map", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << " [" << slevel << "]" << std::endl;); + TRACE("str", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << " [" << slevel << "]" << std::endl;); } else { get_manager().raise_exception("illegal state in add_cut_info_merge(): inconsistent slevels"); } @@ -446,7 +446,7 @@ app * theory_str::mk_int_var(std::string name) { context & ctx = get_context(); ast_manager & m = get_manager(); - TRACE("t_str_detail", tout << "creating integer variable " << name << " at scope level " << sLevel << std::endl;); + TRACE("str", tout << "creating integer variable " << name << " at scope level " << sLevel << std::endl;); sort * int_sort = m.mk_sort(m_autil.get_family_id(), INT_SORT); app * a = m.mk_fresh_const(name.c_str(), int_sort); @@ -481,12 +481,12 @@ app * theory_str::mk_str_var(std::string name) { context & ctx = get_context(); ast_manager & m = get_manager(); - TRACE("t_str_detail", tout << "creating string variable " << name << " at scope level " << sLevel << std::endl;); + TRACE("str", tout << "creating string variable " << name << " at scope level " << sLevel << std::endl;); sort * string_sort = u.str.mk_string_sort(); app * a = m.mk_fresh_const(name.c_str(), string_sort); - TRACE("t_str_detail", tout << "a->get_family_id() = " << a->get_family_id() << std::endl + TRACE("str", tout << "a->get_family_id() = " << a->get_family_id() << std::endl << "this->get_family_id() = " << this->get_family_id() << std::endl;); // I have a hunch that this may not get internalized for free... @@ -496,7 +496,7 @@ app * theory_str::mk_str_var(std::string name) { // this might help?? mk_var(ctx.get_enode(a)); m_basicstr_axiom_todo.push_back(ctx.get_enode(a)); - TRACE("t_str_axiom_bug", tout << "add " << mk_pp(a, m) << " to m_basicstr_axiom_todo" << std::endl;); + TRACE("str", tout << "add " << mk_pp(a, m) << " to m_basicstr_axiom_todo" << std::endl;); m_trail.push_back(a); variable_set.insert(a); @@ -518,7 +518,7 @@ app * theory_str::mk_regex_rep_var() { SASSERT(ctx.e_internalized(a)); mk_var(ctx.get_enode(a)); m_basicstr_axiom_todo.push_back(ctx.get_enode(a)); - TRACE("t_str_axiom_bug", tout << "add " << mk_pp(a, m) << " to m_basicstr_axiom_todo" << std::endl;); + TRACE("str", tout << "add " << mk_pp(a, m) << " to m_basicstr_axiom_todo" << std::endl;); m_trail.push_back(a); variable_set.insert(a); @@ -560,7 +560,7 @@ app * theory_str::mk_nonempty_str_var() { tmpStringVarCount++; std::string name = "$$_str" + ss.str(); - TRACE("t_str_detail", tout << "creating nonempty string variable " << name << " at scope level " << sLevel << std::endl;); + TRACE("str", tout << "creating nonempty string variable " << name << " at scope level " << sLevel << std::endl;); sort * string_sort = u.str.mk_string_sort(); app * a = m.mk_fresh_const(name.c_str(), string_sort); @@ -784,12 +784,12 @@ bool theory_str::can_propagate() { void theory_str::propagate() { context & ctx = get_context(); while (can_propagate()) { - TRACE("t_str_detail", tout << "propagating..." << std::endl;); + TRACE("str", tout << "propagating..." << std::endl;); for (unsigned i = 0; i < m_basicstr_axiom_todo.size(); ++i) { instantiate_basic_string_axioms(m_basicstr_axiom_todo[i]); } m_basicstr_axiom_todo.reset(); - TRACE("t_str_axiom_bug", tout << "reset m_basicstr_axiom_todo" << std::endl;); + TRACE("str", tout << "reset m_basicstr_axiom_todo" << std::endl;); for (unsigned i = 0; i < m_str_eq_todo.size(); ++i) { std::pair pair = m_str_eq_todo[i]; @@ -840,7 +840,7 @@ void theory_str::propagate() { } else if (u.str.is_in_re(a)) { instantiate_axiom_RegexIn(e); } else { - TRACE("t_str", tout << "BUG: unhandled library-aware term " << mk_pp(e->get_owner(), get_manager()) << std::endl;); + TRACE("str", tout << "BUG: unhandled library-aware term " << mk_pp(e->get_owner(), get_manager()) << std::endl;); NOT_IMPLEMENTED_YET(); } } @@ -868,7 +868,7 @@ void theory_str::try_eval_concat(enode * cat) { context & ctx = get_context(); ast_manager & m = get_manager(); - TRACE("t_str_detail", tout << "attempting to flatten " << mk_pp(a_cat, m) << std::endl;); + TRACE("str", tout << "attempting to flatten " << mk_pp(a_cat, m) << std::endl;); std::stack worklist; zstring flattenedString(""); @@ -894,13 +894,13 @@ void theory_str::try_eval_concat(enode * cat) { worklist.push(arg1); worklist.push(arg0); } else { - TRACE("t_str_detail", tout << "non-constant term in concat -- giving up." << std::endl;); + TRACE("str", tout << "non-constant term in concat -- giving up." << std::endl;); constOK = false; break; } } if (constOK) { - TRACE("t_str_detail", tout << "flattened to \"" << flattenedString.encode().c_str() << "\"" << std::endl;); + TRACE("str", tout << "flattened to \"" << flattenedString.encode().c_str() << "\"" << std::endl;); expr_ref constStr(mk_string(flattenedString), m); expr_ref axiom(ctx.mk_eq_atom(a_cat, constStr), m); assert_axiom(axiom); @@ -917,7 +917,7 @@ void theory_str::instantiate_concat_axiom(enode * cat) { ast_manager & m = get_manager(); - TRACE("t_str_detail", tout << "instantiating concat axiom for " << mk_ismt2_pp(a_cat, m) << std::endl;); + TRACE("str", tout << "instantiating concat axiom for " << mk_ismt2_pp(a_cat, m) << std::endl;); // build LHS expr_ref len_xy(m); @@ -960,11 +960,11 @@ void theory_str::instantiate_basic_string_axioms(enode * str) { context & ctx = get_context(); ast_manager & m = get_manager(); - TRACE("t_str_axiom_bug", tout << "set up basic string axioms on " << mk_pp(str->get_owner(), m) << std::endl;); + TRACE("str", tout << "set up basic string axioms on " << mk_pp(str->get_owner(), m) << std::endl;); // TESTING: attempt to avoid a crash here when a variable goes out of scope if (str->get_iscope_lvl() > ctx.get_scope_level()) { - TRACE("t_str_detail", tout << "WARNING: skipping axiom setup on out-of-scope string term" << std::endl;); + TRACE("str", tout << "WARNING: skipping axiom setup on out-of-scope string term" << std::endl;); return; } @@ -977,7 +977,7 @@ void theory_str::instantiate_basic_string_axioms(enode * str) { zstring strconst; u.str.is_string(str->get_owner(), strconst); - TRACE("t_str_detail", tout << "instantiating constant string axioms for \"" << strconst.encode().c_str() << "\"" << std::endl;); + TRACE("str", tout << "instantiating constant string axioms for \"" << strconst.encode().c_str() << "\"" << std::endl;); unsigned int l = strconst.length(); expr_ref len(m_autil.mk_numeral(rational(l), true), m); @@ -998,7 +998,7 @@ void theory_str::instantiate_basic_string_axioms(enode * str) { // build LHS >= RHS and assert app * lhs_ge_rhs = m_autil.mk_ge(len_str, zero); SASSERT(lhs_ge_rhs); - TRACE("t_str_detail", tout << "string axiom 1: " << mk_ismt2_pp(lhs_ge_rhs, m) << std::endl;); + TRACE("str", tout << "string axiom 1: " << mk_ismt2_pp(lhs_ge_rhs, m) << std::endl;); assert_axiom(lhs_ge_rhs); } @@ -1022,7 +1022,7 @@ void theory_str::instantiate_basic_string_axioms(enode * str) { rhs = ctx.mk_eq_atom(a_str, empty_str); SASSERT(rhs); // build LHS <=> RHS and assert - TRACE("t_str_detail", tout << "string axiom 2: " << mk_ismt2_pp(lhs, m) << " <=> " << mk_ismt2_pp(rhs, m) << std::endl;); + TRACE("str", tout << "string axiom 2: " << mk_ismt2_pp(lhs, m) << " <=> " << mk_ismt2_pp(rhs, m) << std::endl;); literal l(mk_eq(lhs, rhs, true)); ctx.mark_as_relevant(l); ctx.mk_th_axiom(get_id(), 1, &l); @@ -1052,7 +1052,7 @@ void theory_str::instantiate_str_eq_length_axiom(enode * lhs, enode * rhs) { SASSERT(len_rhs); expr_ref conclusion(ctx.mk_eq_atom(len_lhs, len_rhs), m); - TRACE("t_str_detail", tout << "string-eq length-eq axiom: " + TRACE("str", tout << "string-eq length-eq axiom: " << mk_ismt2_pp(premise, m) << " -> " << mk_ismt2_pp(conclusion, m) << std::endl;); assert_implication(premise, conclusion); } @@ -1063,12 +1063,12 @@ void theory_str::instantiate_axiom_CharAt(enode * e) { app * expr = e->get_owner(); if (axiomatized_terms.contains(expr)) { - TRACE("t_str_detail", tout << "already set up CharAt axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("str", tout << "already set up CharAt axiom for " << mk_pp(expr, m) << std::endl;); return; } axiomatized_terms.insert(expr); - TRACE("t_str_detail", tout << "instantiate CharAt axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("str", tout << "instantiate CharAt axiom for " << mk_pp(expr, m) << std::endl;); expr_ref ts0(mk_str_var("ts0"), m); expr_ref ts1(mk_str_var("ts1"), m); @@ -1106,12 +1106,12 @@ void theory_str::instantiate_axiom_prefixof(enode * e) { app * expr = e->get_owner(); if (axiomatized_terms.contains(expr)) { - TRACE("t_str_detail", tout << "already set up prefixof axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("str", tout << "already set up prefixof axiom for " << mk_pp(expr, m) << std::endl;); return; } axiomatized_terms.insert(expr); - TRACE("t_str_detail", tout << "instantiate prefixof axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("str", tout << "instantiate prefixof axiom for " << mk_pp(expr, m) << std::endl;); expr_ref ts0(mk_str_var("ts0"), m); expr_ref ts1(mk_str_var("ts1"), m); @@ -1143,12 +1143,12 @@ void theory_str::instantiate_axiom_suffixof(enode * e) { app * expr = e->get_owner(); if (axiomatized_terms.contains(expr)) { - TRACE("t_str_detail", tout << "already set up suffixof axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("str", tout << "already set up suffixof axiom for " << mk_pp(expr, m) << std::endl;); return; } axiomatized_terms.insert(expr); - TRACE("t_str_detail", tout << "instantiate suffixof axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("str", tout << "instantiate suffixof axiom for " << mk_pp(expr, m) << std::endl;); expr_ref ts0(mk_str_var("ts0"), m); expr_ref ts1(mk_str_var("ts1"), m); @@ -1180,7 +1180,7 @@ void theory_str::instantiate_axiom_Contains(enode * e) { app * ex = e->get_owner(); if (axiomatized_terms.contains(ex)) { - TRACE("t_str_detail", tout << "already set up Contains axiom for " << mk_pp(ex, m) << std::endl;); + TRACE("str", tout << "already set up Contains axiom for " << mk_pp(ex, m) << std::endl;); return; } axiomatized_terms.insert(ex); @@ -1189,7 +1189,7 @@ void theory_str::instantiate_axiom_Contains(enode * e) { // at minimum it should fix z3str/concat-006.smt2 zstring haystackStr, needleStr; if (u.str.is_string(ex->get_arg(0), haystackStr) && u.str.is_string(ex->get_arg(1), needleStr)) { - TRACE("t_str_detail", tout << "eval constant Contains term " << mk_pp(ex, m) << std::endl;); + TRACE("str", tout << "eval constant Contains term " << mk_pp(ex, m) << std::endl;); if (haystackStr.contains(needleStr)) { assert_axiom(ex); } else { @@ -1208,7 +1208,7 @@ void theory_str::instantiate_axiom_Contains(enode * e) { contain_pair_idx_map[substr].insert(key); } - TRACE("t_str_detail", tout << "instantiate Contains axiom for " << mk_pp(ex, m) << std::endl;); + TRACE("str", tout << "instantiate Contains axiom for " << mk_pp(ex, m) << std::endl;); expr_ref ts0(mk_str_var("ts0"), m); expr_ref ts1(mk_str_var("ts1"), m); @@ -1224,12 +1224,12 @@ void theory_str::instantiate_axiom_Indexof(enode * e) { app * expr = e->get_owner(); if (axiomatized_terms.contains(expr)) { - TRACE("t_str_detail", tout << "already set up Indexof axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("str", tout << "already set up Indexof axiom for " << mk_pp(expr, m) << std::endl;); return; } axiomatized_terms.insert(expr); - TRACE("t_str_detail", tout << "instantiate Indexof axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("str", tout << "instantiate Indexof axiom for " << mk_pp(expr, m) << std::endl;); expr_ref x1(mk_str_var("x1"), m); expr_ref x2(mk_str_var("x2"), m); @@ -1280,12 +1280,12 @@ void theory_str::instantiate_axiom_Indexof2(enode * e) { app * expr = e->get_owner(); if (axiomatized_terms.contains(expr)) { - TRACE("t_str_detail", tout << "already set up Indexof2 axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("str", tout << "already set up Indexof2 axiom for " << mk_pp(expr, m) << std::endl;); return; } axiomatized_terms.insert(expr); - TRACE("t_str_detail", tout << "instantiate Indexof2 axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("str", tout << "instantiate Indexof2 axiom for " << mk_pp(expr, m) << std::endl;); // ------------------------------------------------------------------------------- // if (arg[2] >= length(arg[0])) // ite2 @@ -1348,12 +1348,12 @@ void theory_str::instantiate_axiom_LastIndexof(enode * e) { app * expr = e->get_owner(); if (axiomatized_terms.contains(expr)) { - TRACE("t_str_detail", tout << "already set up LastIndexof axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("str", tout << "already set up LastIndexof axiom for " << mk_pp(expr, m) << std::endl;); return; } axiomatized_terms.insert(expr); - TRACE("t_str_detail", tout << "instantiate LastIndexof axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("str", tout << "instantiate LastIndexof axiom for " << mk_pp(expr, m) << std::endl;); expr_ref x1(mk_str_var("x1"), m); expr_ref x2(mk_str_var("x2"), m); @@ -1417,12 +1417,12 @@ void theory_str::instantiate_axiom_Substr(enode * e) { app * expr = e->get_owner(); if (axiomatized_terms.contains(expr)) { - TRACE("t_str_detail", tout << "already set up Substr axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("str", tout << "already set up Substr axiom for " << mk_pp(expr, m) << std::endl;); return; } axiomatized_terms.insert(expr); - TRACE("t_str_detail", tout << "instantiate Substr axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("str", tout << "instantiate Substr axiom for " << mk_pp(expr, m) << std::endl;); expr_ref substrBase(expr->get_arg(0), m); expr_ref substrPos(expr->get_arg(1), m); @@ -1510,12 +1510,12 @@ void theory_str::instantiate_axiom_Replace(enode * e) { app * expr = e->get_owner(); if (axiomatized_terms.contains(expr)) { - TRACE("t_str_detail", tout << "already set up Replace axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("str", tout << "already set up Replace axiom for " << mk_pp(expr, m) << std::endl;); return; } axiomatized_terms.insert(expr); - TRACE("t_str_detail", tout << "instantiate Replace axiom for " << mk_pp(expr, m) << std::endl;); + TRACE("str", tout << "instantiate Replace axiom for " << mk_pp(expr, m) << std::endl;); expr_ref x1(mk_str_var("x1"), m); expr_ref x2(mk_str_var("x2"), m); @@ -1560,12 +1560,12 @@ void theory_str::instantiate_axiom_str_to_int(enode * e) { app * ex = e->get_owner(); if (axiomatized_terms.contains(ex)) { - TRACE("t_str_detail", tout << "already set up str.to-int axiom for " << mk_pp(ex, m) << std::endl;); + TRACE("str", tout << "already set up str.to-int axiom for " << mk_pp(ex, m) << std::endl;); return; } axiomatized_terms.insert(ex); - TRACE("t_str_detail", tout << "instantiate str.to-int axiom for " << mk_pp(ex, m) << std::endl;); + TRACE("str", tout << "instantiate str.to-int axiom for " << mk_pp(ex, m) << std::endl;); // let expr = (str.to-int S) // axiom 1: expr >= -1 @@ -1607,12 +1607,12 @@ void theory_str::instantiate_axiom_int_to_str(enode * e) { app * ex = e->get_owner(); if (axiomatized_terms.contains(ex)) { - TRACE("t_str_detail", tout << "already set up str.from-int axiom for " << mk_pp(ex, m) << std::endl;); + TRACE("str", tout << "already set up str.from-int axiom for " << mk_pp(ex, m) << std::endl;); return; } axiomatized_terms.insert(ex); - TRACE("t_str_detail", tout << "instantiate str.from-int axiom for " << mk_pp(ex, m) << std::endl;); + TRACE("str", tout << "instantiate str.from-int axiom for " << mk_pp(ex, m) << std::endl;); // axiom 1: N < 0 <==> (str.from-int N) = "" expr * N = ex->get_arg(0); @@ -1674,7 +1674,7 @@ zstring theory_str::get_std_regex_str(expr * regex) { zstring reg1Str = get_std_regex_str(reg1Ast); return zstring("(") + reg1Str + zstring(")*"); } else { - TRACE("t_str", tout << "BUG: unrecognized regex term " << mk_pp(regex, get_manager()) << std::endl;); + TRACE("str", tout << "BUG: unrecognized regex term " << mk_pp(regex, get_manager()) << std::endl;); UNREACHABLE(); return zstring(""); } } @@ -1685,12 +1685,12 @@ void theory_str::instantiate_axiom_RegexIn(enode * e) { app * ex = e->get_owner(); if (axiomatized_terms.contains(ex)) { - TRACE("t_str_detail", tout << "already set up RegexIn axiom for " << mk_pp(ex, m) << std::endl;); + TRACE("str", tout << "already set up RegexIn axiom for " << mk_pp(ex, m) << std::endl;); return; } axiomatized_terms.insert(ex); - TRACE("t_str_detail", tout << "instantiate RegexIn axiom for " << mk_pp(ex, m) << std::endl;); + TRACE("str", tout << "instantiate RegexIn axiom for " << mk_pp(ex, m) << std::endl;); { zstring regexStr = get_std_regex_str(ex->get_arg(1)); @@ -1710,7 +1710,7 @@ void theory_str::instantiate_axiom_RegexIn(enode * e) { expr_ref finalAxiom(m.mk_iff(ex, rhs), m); SASSERT(finalAxiom); assert_axiom(finalAxiom); - TRACE("t_str", tout << "set up Str2Reg: (RegexIn " << mk_pp(str, m) << " " << mk_pp(regex, m) << ")" << std::endl;); + TRACE("str", tout << "set up Str2Reg: (RegexIn " << mk_pp(str, m) << " " << mk_pp(regex, m) << ")" << std::endl;); } else if (u.re.is_concat(regex)) { expr_ref var1(mk_regex_rep_var(), m); expr_ref var2(mk_regex_rep_var(), m); @@ -1753,7 +1753,7 @@ void theory_str::instantiate_axiom_RegexIn(enode * e) { SASSERT(finalAxiom); assert_axiom(finalAxiom); } else { - TRACE("t_str_detail", tout << "ERROR: unknown regex expression " << mk_pp(regex, m) << "!" << std::endl;); + TRACE("str", tout << "ERROR: unknown regex expression " << mk_pp(regex, m) << "!" << std::endl;); NOT_IMPLEMENTED_YET(); } } @@ -1762,11 +1762,11 @@ void theory_str::attach_new_th_var(enode * n) { context & ctx = get_context(); theory_var v = mk_var(n); ctx.attach_th_var(n, this, v); - TRACE("t_str_detail", tout << "new theory var: " << mk_ismt2_pp(n->get_owner(), get_manager()) << " := v#" << v << std::endl;); + TRACE("str", tout << "new theory var: " << mk_ismt2_pp(n->get_owner(), get_manager()) << " := v#" << v << std::endl;); } void theory_str::reset_eh() { - TRACE("t_str", tout << "resetting" << std::endl;); + TRACE("str", tout << "resetting" << std::endl;); m_trail_stack.reset(); m_basicstr_axiom_todo.reset(); @@ -1804,19 +1804,19 @@ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { do { expr * eqc_nn2 = rhs; do { - TRACE("t_str_detail", tout << "checking whether " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " can be equal" << std::endl;); + TRACE("str", tout << "checking whether " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " can be equal" << std::endl;); // inconsistency check: value if (!can_two_nodes_eq(eqc_nn1, eqc_nn2)) { - TRACE("t_str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " cannot be equal to " << mk_pp(eqc_nn2, m) << std::endl;); + TRACE("str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " cannot be equal to " << mk_pp(eqc_nn2, m) << std::endl;); expr_ref to_assert(m.mk_not(ctx.mk_eq_atom(eqc_nn1, eqc_nn2)), m); assert_axiom(to_assert); // this shouldn't use the integer theory at all, so we don't allow the option of quick-return return false; } if (!check_length_consistency(eqc_nn1, eqc_nn2)) { - TRACE("t_str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " have inconsistent lengths" << std::endl;); + TRACE("str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " have inconsistent lengths" << std::endl;); if (opt_NoQuickReturn_IntegerTheory){ - TRACE("t_str_detail", tout << "continuing in new_eq_check() due to opt_NoQuickReturn_IntegerTheory" << std::endl;); + TRACE("str", tout << "continuing in new_eq_check() due to opt_NoQuickReturn_IntegerTheory" << std::endl;); } else { return false; } @@ -1831,7 +1831,7 @@ bool theory_str::new_eq_check(expr * lhs, expr * rhs) { } if (!regex_in_bool_map.empty()) { - TRACE("t_str", tout << "checking regex consistency" << std::endl;); + TRACE("str", tout << "checking regex consistency" << std::endl;); check_regex_in(lhs, rhs); } @@ -1963,7 +1963,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { ast_manager & m = get_manager(); context & ctx = get_context(); - TRACE("t_str", tout << "simplifying parents of " << mk_ismt2_pp(nn, m) + TRACE("str", tout << "simplifying parents of " << mk_ismt2_pp(nn, m) << " with respect to " << mk_ismt2_pp(eq_str, m) << std::endl;); ctx.internalize(nn, false); @@ -1973,7 +1973,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { expr * n_eqNode = nn; do { enode * n_eq_enode = ctx.get_enode(n_eqNode); - TRACE("t_str_detail", tout << "considering all parents of " << mk_ismt2_pp(n_eqNode, m) << std::endl + TRACE("str", tout << "considering all parents of " << mk_ismt2_pp(n_eqNode, m) << std::endl << "associated n_eq_enode has " << n_eq_enode->get_num_parents() << " parents" << std::endl;); // the goal of this next bit is to avoid dereferencing a bogus e_parent in the following loop. @@ -1990,7 +1990,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { SASSERT(e_parent != NULL); app * a_parent = e_parent->get_owner(); - TRACE("t_str_detail", tout << "considering parent " << mk_ismt2_pp(a_parent, m) << std::endl;); + TRACE("str", tout << "considering parent " << mk_ismt2_pp(a_parent, m) << std::endl;); if (u.str.is_concat(a_parent)) { expr * arg0 = a_parent->get_arg(0); @@ -2004,7 +2004,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { bool arg0Len_exists = get_len_value(eq_str, arg0Len); bool arg1Len_exists = get_len_value(arg1, arg1Len); - TRACE("t_str_detail", + TRACE("str", tout << "simplify_parent #1:" << std::endl << "* parent = " << mk_ismt2_pp(a_parent, m) << std::endl << "* |parent| = " << rational_to_string_if_exists(parentLen, parentLen_exists) << std::endl @@ -2013,7 +2013,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { ); if (parentLen_exists && !arg1Len_exists) { - TRACE("t_str_detail", tout << "make up len for arg1" << std::endl;); + TRACE("str", tout << "make up len for arg1" << std::endl;); expr_ref implyL11(m.mk_and(ctx.mk_eq_atom(mk_strlen(a_parent), mk_int(parentLen)), ctx.mk_eq_atom(mk_strlen(arg0), mk_int(arg0Len))), m); rational makeUpLenArg1 = parentLen - arg0Len; @@ -2075,7 +2075,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { bool arg0Len_exists = get_len_value(arg0, arg0Len); bool arg1Len_exists = get_len_value(eq_str, arg1Len); - TRACE("t_str_detail", + TRACE("str", tout << "simplify_parent #2:" << std::endl << "* parent = " << mk_ismt2_pp(a_parent, m) << std::endl << "* |parent| = " << rational_to_string_if_exists(parentLen, parentLen_exists) << std::endl @@ -2083,7 +2083,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { << "* |arg1| = " << rational_to_string_if_exists(arg1Len, arg1Len_exists) << std::endl; ); if (parentLen_exists && !arg0Len_exists) { - TRACE("t_str_detail", tout << "make up len for arg0" << std::endl;); + TRACE("str", tout << "make up len for arg0" << std::endl;); expr_ref implyL11(m.mk_and(ctx.mk_eq_atom(mk_strlen(a_parent), mk_int(parentLen)), ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1Len))), m); rational makeUpLenArg0 = parentLen - arg1Len; @@ -2144,7 +2144,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { // Case (2-1) begin: (Concat n_eqNode (Concat str var)) if (arg0 == n_eqNode && u.str.is_concat(to_app(arg1))) { app * a_arg1 = to_app(arg1); - TRACE("t_str_detail", tout << "simplify_parent #3" << std::endl;); + TRACE("str", tout << "simplify_parent #3" << std::endl;); expr * r_concat_arg0 = a_arg1->get_arg(0); if (u.str.is_string(r_concat_arg0)) { expr * combined_str = eval_concat(eq_str, r_concat_arg0); @@ -2168,7 +2168,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { // Case (2-2) begin: (Concat (Concat var str) n_eqNode) if (u.str.is_concat(to_app(arg0)) && arg1 == n_eqNode) { app * a_arg0 = to_app(arg0); - TRACE("t_str_detail", tout << "simplify_parent #4" << std::endl;); + TRACE("str", tout << "simplify_parent #4" << std::endl;); expr * l_concat_arg1 = a_arg0->get_arg(1); if (u.str.is_string(l_concat_arg1)) { expr * combined_str = eval_concat(l_concat_arg1, eq_str); @@ -2199,7 +2199,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { expr * concat_parent_arg0 = concat_parent->get_arg(0); expr * concat_parent_arg1 = concat_parent->get_arg(1); if (concat_parent_arg0 == a_parent && u.str.is_string(concat_parent_arg1)) { - TRACE("t_str_detail", tout << "simplify_parent #5" << std::endl;); + TRACE("str", tout << "simplify_parent #5" << std::endl;); expr * combinedStr = eval_concat(eq_str, concat_parent_arg1); SASSERT(combinedStr); expr_ref implyL(m); @@ -2225,7 +2225,7 @@ void theory_str::simplify_parent(expr * nn, expr * eq_str) { expr * concat_parent_arg0 = concat_parent->get_arg(0); expr * concat_parent_arg1 = concat_parent->get_arg(1); if (concat_parent_arg1 == a_parent && u.str.is_string(concat_parent_arg0)) { - TRACE("t_str_detail", tout << "simplify_parent #6" << std::endl;); + TRACE("str", tout << "simplify_parent #6" << std::endl;); expr * combinedStr = eval_concat(concat_parent_arg0, eq_str); SASSERT(combinedStr); expr_ref implyL(m); @@ -2275,10 +2275,10 @@ expr * theory_str::simplify_concat(expr * node) { expr * vArg = get_eqc_value(argVec[i], vArgHasEqcValue); resultAst = mk_concat(resultAst, vArg); } - TRACE("t_str_detail", tout << mk_ismt2_pp(node, m) << " is simplified to " << mk_ismt2_pp(resultAst, m) << std::endl;); + TRACE("str", tout << mk_ismt2_pp(node, m) << " is simplified to " << mk_ismt2_pp(resultAst, m) << std::endl;); if (in_same_eqc(node, resultAst)) { - TRACE("t_str_detail", tout << "SKIP: both concats are already in the same equivalence class" << std::endl;); + TRACE("str", tout << "SKIP: both concats are already in the same equivalence class" << std::endl;); } else { expr_ref_vector items(m); int pos = 0; @@ -2327,7 +2327,7 @@ bool theory_str::infer_len_concat(expr * n, rational & nLen) { expr_ref axl(m.mk_and(l_items.size(), l_items.c_ptr()), m); rational nnLen = arg0_len + arg1_len; expr_ref axr(ctx.mk_eq_atom(mk_strlen(n), mk_int(nnLen)), m); - TRACE("t_str_detail", tout << "inferred (Length " << mk_pp(n, m) << ") = " << nnLen << std::endl;); + TRACE("str", tout << "inferred (Length " << mk_pp(n, m) << ") = " << nnLen << std::endl;); assert_implication(axl, axr); nLen = nnLen; return true; @@ -2507,10 +2507,10 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { bool a2_arg0_len_exists = get_len_value(a2_arg0, a2_arg0_len); bool a2_arg1_len_exists = get_len_value(a2_arg1, a2_arg1_len); - TRACE("t_str", tout << "nn1 = " << mk_ismt2_pp(nn1, m) << std::endl + TRACE("str", tout << "nn1 = " << mk_ismt2_pp(nn1, m) << std::endl << "nn2 = " << mk_ismt2_pp(nn2, m) << std::endl;); - TRACE("t_str_detail", tout + TRACE("str", tout << "len(" << mk_pp(a1_arg0, m) << ") = " << (a1_arg0_len_exists ? a1_arg0_len.to_string() : "?") << std::endl << "len(" << mk_pp(a1_arg1, m) << ") = " << (a1_arg1_len_exists ? a1_arg1_len.to_string() : "?") << std::endl << "len(" << mk_pp(a2_arg0, m) << ") = " << (a2_arg0_len_exists ? a2_arg0_len.to_string() : "?") << std::endl @@ -2527,7 +2527,7 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { expr_ref conclusion(m.mk_and(eq1, eq2), m); assert_implication(premise, conclusion); } - TRACE("t_str_detail", tout << "SKIP: a1_arg0 == a2_arg0" << std::endl;); + TRACE("str", tout << "SKIP: a1_arg0 == a2_arg0" << std::endl;); return; } @@ -2539,7 +2539,7 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { expr_ref conclusion(m.mk_and(eq1, eq2), m); assert_implication(premise, conclusion); } - TRACE("t_str_detail", tout << "SKIP: a1_arg1 == a2_arg1" << std::endl;); + TRACE("str", tout << "SKIP: a1_arg1 == a2_arg1" << std::endl;); return; } @@ -2547,10 +2547,10 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { if (in_same_eqc(a1_arg0, a2_arg0)) { if (in_same_eqc(a1_arg1, a2_arg1)) { - TRACE("t_str_detail", tout << "SKIP: a1_arg0 =~ a2_arg0 and a1_arg1 =~ a2_arg1" << std::endl;); + TRACE("str", tout << "SKIP: a1_arg0 =~ a2_arg0 and a1_arg1 =~ a2_arg1" << std::endl;); return; } else { - TRACE("t_str_detail", tout << "quick path 1-1: a1_arg0 =~ a2_arg0" << std::endl;); + TRACE("str", tout << "quick path 1-1: a1_arg0 =~ a2_arg0" << std::endl;); expr_ref premise(m.mk_and(ctx.mk_eq_atom(nn1, nn2), ctx.mk_eq_atom(a1_arg0, a2_arg0)), m); expr_ref conclusion(m.mk_and(ctx.mk_eq_atom(a1_arg1, a2_arg1), ctx.mk_eq_atom(mk_strlen(a1_arg1), mk_strlen(a2_arg1))), m); assert_implication(premise, conclusion); @@ -2558,7 +2558,7 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { } } else { if (in_same_eqc(a1_arg1, a2_arg1)) { - TRACE("t_str_detail", tout << "quick path 1-2: a1_arg1 =~ a2_arg1" << std::endl;); + TRACE("str", tout << "quick path 1-2: a1_arg1 =~ a2_arg1" << std::endl;); expr_ref premise(m.mk_and(ctx.mk_eq_atom(nn1, nn2), ctx.mk_eq_atom(a1_arg1, a2_arg1)), m); expr_ref conclusion(m.mk_and(ctx.mk_eq_atom(a1_arg0, a2_arg0), ctx.mk_eq_atom(mk_strlen(a1_arg0), mk_strlen(a2_arg0))), m); assert_implication(premise, conclusion); @@ -2569,7 +2569,7 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { // quick path 2-1 if (a1_arg0_len_exists && a2_arg0_len_exists && a1_arg0_len == a2_arg0_len) { if (!in_same_eqc(a1_arg0, a2_arg0)) { - TRACE("t_str_detail", tout << "quick path 2-1: len(nn1.arg0) == len(nn2.arg0)" << std::endl;); + TRACE("str", tout << "quick path 2-1: len(nn1.arg0) == len(nn2.arg0)" << std::endl;); expr_ref ax_l1(ctx.mk_eq_atom(nn1, nn2), m); expr_ref ax_l2(ctx.mk_eq_atom(mk_strlen(a1_arg0), mk_strlen(a2_arg0)), m); expr_ref ax_r1(ctx.mk_eq_atom(a1_arg0, a2_arg0), m); @@ -2581,7 +2581,7 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { assert_implication(premise, conclusion); if (opt_NoQuickReturn_IntegerTheory) { - TRACE("t_str_detail", tout << "bypassing quick return from the end of this case" << std::endl;); + TRACE("str", tout << "bypassing quick return from the end of this case" << std::endl;); } else { return; } @@ -2590,7 +2590,7 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { if (a1_arg1_len_exists && a2_arg1_len_exists && a1_arg1_len == a2_arg1_len) { if (!in_same_eqc(a1_arg1, a2_arg1)) { - TRACE("t_str_detail", tout << "quick path 2-2: len(nn1.arg1) == len(nn2.arg1)" << std::endl;); + TRACE("str", tout << "quick path 2-2: len(nn1.arg1) == len(nn2.arg1)" << std::endl;); expr_ref ax_l1(ctx.mk_eq_atom(nn1, nn2), m); expr_ref ax_l2(ctx.mk_eq_atom(mk_strlen(a1_arg1), mk_strlen(a2_arg1)), m); expr_ref ax_r1(ctx.mk_eq_atom(a1_arg0, a2_arg0), m); @@ -2601,7 +2601,7 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { assert_implication(premise, conclusion); if (opt_NoQuickReturn_IntegerTheory) { - TRACE("t_str_detail", tout << "bypassing quick return from the end of this case" << std::endl;); + TRACE("str", tout << "bypassing quick return from the end of this case" << std::endl;); } else { return; } @@ -2613,17 +2613,17 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { app * a_new_nn1 = to_app(new_nn1); app * a_new_nn2 = to_app(new_nn2); - TRACE("t_str_detail", tout << "new_nn1 = " << mk_ismt2_pp(new_nn1, m) << std::endl + TRACE("str", tout << "new_nn1 = " << mk_ismt2_pp(new_nn1, m) << std::endl << "new_nn2 = " << mk_ismt2_pp(new_nn2, m) << std::endl;); if (new_nn1 == new_nn2) { - TRACE("t_str_detail", tout << "equal concats, return" << std::endl;); + TRACE("str", tout << "equal concats, return" << std::endl;); return; } if (!can_two_nodes_eq(new_nn1, new_nn2)) { expr_ref detected(m.mk_not(ctx.mk_eq_atom(new_nn1, new_nn2)), m); - TRACE("t_str_detail", tout << "inconsistency detected: " << mk_ismt2_pp(detected, m) << std::endl;); + TRACE("str", tout << "inconsistency detected: " << mk_ismt2_pp(detected, m) << std::endl;); assert_axiom(detected); return; } @@ -2633,13 +2633,13 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { bool n1IsConcat = u.str.is_concat(a_new_nn1); bool n2IsConcat = u.str.is_concat(a_new_nn2); if (!n1IsConcat && n2IsConcat) { - TRACE("t_str_detail", tout << "nn1_new is not a concat" << std::endl;); + TRACE("str", tout << "nn1_new is not a concat" << std::endl;); if (u.str.is_string(a_new_nn1)) { simplify_parent(new_nn2, new_nn1); } return; } else if (n1IsConcat && !n2IsConcat) { - TRACE("t_str_detail", tout << "nn2_new is not a concat" << std::endl;); + TRACE("str", tout << "nn2_new is not a concat" << std::endl;); if (u.str.is_string(a_new_nn2)) { simplify_parent(new_nn1, new_nn2); } @@ -2647,7 +2647,7 @@ void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { } else if (!n1IsConcat && !n2IsConcat) { // normally this should never happen, because group_terms_by_eqc() should have pre-simplified // as much as possible. however, we make a defensive check here just in case - TRACE("t_str_detail", tout << "WARNING: nn1_new and nn2_new both simplify to non-concat terms" << std::endl;); + TRACE("str", tout << "WARNING: nn1_new and nn2_new both simplify to non-concat terms" << std::endl;); return; } @@ -2750,7 +2750,7 @@ bool theory_str::will_result_in_overlap(expr * lhs, expr * rhs) { expr * v2_arg0 = a_new_nn2->get_arg(0); expr * v2_arg1 = a_new_nn2->get_arg(1); - TRACE("t_str_detail", tout << "checking whether " << mk_pp(new_nn1, m) << " and " << mk_pp(new_nn1, m) << " might overlap." << std::endl;); + TRACE("str", tout << "checking whether " << mk_pp(new_nn1, m) << " and " << mk_pp(new_nn1, m) << " might overlap." << std::endl;); check_and_init_cut_var(v1_arg0); check_and_init_cut_var(v1_arg1); @@ -2761,17 +2761,17 @@ bool theory_str::will_result_in_overlap(expr * lhs, expr * rhs) { // case 1: concat(x, y) = concat(m, n) //************************************************************* if (is_concat_eq_type1(new_nn1, new_nn2)) { - TRACE("t_str_detail", tout << "Type 1 check." << std::endl;); + TRACE("str", tout << "Type 1 check." << std::endl;); expr * x = to_app(new_nn1)->get_arg(0); expr * y = to_app(new_nn1)->get_arg(1); expr * m = to_app(new_nn2)->get_arg(0); expr * n = to_app(new_nn2)->get_arg(1); if (has_self_cut(m, y)) { - TRACE("t_str_detail", tout << "Possible overlap found" << std::endl; print_cut_var(m, tout); print_cut_var(y, tout);); + TRACE("str", tout << "Possible overlap found" << std::endl; print_cut_var(m, tout); print_cut_var(y, tout);); return true; } else if (has_self_cut(x, n)) { - TRACE("t_str_detail", tout << "Possible overlap found" << std::endl; print_cut_var(x, tout); print_cut_var(n, tout);); + TRACE("str", tout << "Possible overlap found" << std::endl; print_cut_var(x, tout); print_cut_var(n, tout);); return true; } else { return false; @@ -2799,7 +2799,7 @@ bool theory_str::will_result_in_overlap(expr * lhs, expr * rhs) { } if (has_self_cut(m, y)) { - TRACE("t_str_detail", tout << "Possible overlap found" << std::endl; print_cut_var(m, tout); print_cut_var(y, tout);); + TRACE("str", tout << "Possible overlap found" << std::endl; print_cut_var(m, tout); print_cut_var(y, tout);); return true; } else { return false; @@ -2826,7 +2826,7 @@ bool theory_str::will_result_in_overlap(expr * lhs, expr * rhs) { x = v1_arg0; } if (has_self_cut(x, n)) { - TRACE("t_str_detail", tout << "Possible overlap found" << std::endl; print_cut_var(x, tout); print_cut_var(n, tout);); + TRACE("str", tout << "Possible overlap found" << std::endl; print_cut_var(x, tout); print_cut_var(n, tout);); return true; } else { return false; @@ -2868,14 +2868,14 @@ bool theory_str::will_result_in_overlap(expr * lhs, expr * rhs) { m = v1_arg0; } if (has_self_cut(m, y)) { - TRACE("t_str_detail", tout << "Possible overlap found" << std::endl; print_cut_var(m, tout); print_cut_var(y, tout);); + TRACE("str", tout << "Possible overlap found" << std::endl; print_cut_var(m, tout); print_cut_var(y, tout);); return true; } else { return false; } } - TRACE("t_str_detail", tout << "warning: unrecognized concat case" << std::endl;); + TRACE("str", tout << "warning: unrecognized concat case" << std::endl;); return false; } @@ -2902,17 +2902,17 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { bool overlapAssumptionUsed = false; - TRACE("t_str_detail", tout << "process_concat_eq TYPE 1" << std::endl + TRACE("str", tout << "process_concat_eq TYPE 1" << std::endl << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; ); if (!u.str.is_concat(to_app(concatAst1))) { - TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); + TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); return; } if (!u.str.is_concat(to_app(concatAst2))) { - TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); + TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); return; } expr * x = to_app(concatAst1)->get_arg(0); @@ -2928,7 +2928,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { int splitType = -1; if (x_len_exists && m_len_exists) { - TRACE("t_str_int", tout << "length values found: x/m" << std::endl;); + TRACE("str", tout << "length values found: x/m" << std::endl;); if (x_len < m_len) { splitType = 0; } else if (x_len == m_len) { @@ -2939,7 +2939,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } if (splitType == -1 && y_len_exists && n_len_exists) { - TRACE("t_str_int", tout << "length values found: y/n" << std::endl;); + TRACE("str", tout << "length values found: y/n" << std::endl;); if (y_len > n_len) { splitType = 0; } else if (y_len == n_len) { @@ -2949,7 +2949,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } } - TRACE("t_str_detail", tout + TRACE("str", tout << "len(x) = " << (x_len_exists ? x_len.to_string() : "?") << std::endl << "len(y) = " << (y_len_exists ? y_len.to_string() : "?") << std::endl << "len(m) = " << (m_len_exists ? m_len.to_string() : "?") << std::endl @@ -2996,7 +2996,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { } } - TRACE("t_str_detail", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl + TRACE("str", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); if (!entry1InScope && !entry2InScope) { @@ -3076,8 +3076,8 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { assert_implication(ax_l, tester); add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { - TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); + TRACE("str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + TRACE("str", {print_cut_var(m, tout); print_cut_var(y, tout);}); if (!overlapAssumptionUsed) { overlapAssumptionUsed = true; @@ -3139,8 +3139,8 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { assert_implication(ax_l, tester); add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { - TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); + TRACE("str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + TRACE("str", {print_cut_var(m, tout); print_cut_var(y, tout);}); if (!overlapAssumptionUsed) { overlapAssumptionUsed = true; @@ -3195,8 +3195,8 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { arrangement_disjunction.push_back(tester); add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { - TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); + TRACE("str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + TRACE("str", {print_cut_var(m, tout); print_cut_var(y, tout);}); if (!overlapAssumptionUsed) { overlapAssumptionUsed = true; @@ -3244,8 +3244,8 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { arrangement_disjunction.push_back(tester); add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { - TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - TRACE("t_str_detail", {print_cut_var(x, tout); print_cut_var(n, tout);}); + TRACE("str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + TRACE("str", {print_cut_var(x, tout); print_cut_var(n, tout);}); if (!overlapAssumptionUsed) { overlapAssumptionUsed = true; @@ -3282,7 +3282,7 @@ void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { // assert mutual exclusion between each branch of the arrangement generate_mutual_exclusion(arrangement_disjunction); } else { - TRACE("t_str", tout << "STOP: no split option found for two EQ concats." << std::endl;); + TRACE("str", tout << "STOP: no split option found for two EQ concats." << std::endl;); } } // (splitType == -1) } @@ -3313,17 +3313,17 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { bool overlapAssumptionUsed = false; - TRACE("t_str_detail", tout << "process_concat_eq TYPE 2" << std::endl + TRACE("str", tout << "process_concat_eq TYPE 2" << std::endl << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; ); if (!u.str.is_concat(to_app(concatAst1))) { - TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); + TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); return; } if (!u.str.is_concat(to_app(concatAst2))) { - TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); + TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); return; } @@ -3400,7 +3400,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { } } - TRACE("t_str_detail", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl + TRACE("str", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); @@ -3439,7 +3439,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { splitType = 2; } - TRACE("t_str_detail", tout << "Split type " << splitType << std::endl;); + TRACE("str", tout << "Split type " << splitType << std::endl;); // Provide fewer split options when length information is available. @@ -3491,8 +3491,8 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { assert_implication(ax_l, tester); add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { - TRACE("t_str", tout << "AVOID LOOP: SKIP" << std::endl;); - TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); + TRACE("str", tout << "AVOID LOOP: SKIP" << std::endl;); + TRACE("str", {print_cut_var(m, tout); print_cut_var(y, tout);}); if (!overlapAssumptionUsed) { overlapAssumptionUsed = true; @@ -3526,7 +3526,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); lenDelta = str_len - y_len; } - TRACE("t_str", + TRACE("str", tout << "xLen? " << (x_len_exists ? "yes" : "no") << std::endl << "mLen? " << (m_len_exists ? "yes" : "no") << std::endl @@ -3562,7 +3562,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { } } else { // negate! It's impossible to split str with these lengths - TRACE("t_str", tout << "CONFLICT: Impossible to split str with these lengths." << std::endl;); + TRACE("str", tout << "CONFLICT: Impossible to split str with these lengths." << std::endl;); expr_ref ax_l(mk_and(l_items), mgr); assert_axiom(mgr.mk_not(ax_l)); } @@ -3597,8 +3597,8 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { arrangement_disjunction.push_back(tester); add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { - TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - TRACE("t_str_detail", {print_cut_var(m, tout); print_cut_var(y, tout);}); + TRACE("str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + TRACE("str", {print_cut_var(m, tout); print_cut_var(y, tout);}); if (!overlapAssumptionUsed) { overlapAssumptionUsed = true; @@ -3645,7 +3645,7 @@ void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { } generate_mutual_exclusion(arrangement_disjunction); } else { - TRACE("t_str", tout << "STOP: Should not split two EQ concats." << std::endl;); + TRACE("str", tout << "STOP: Should not split two EQ concats." << std::endl;); } } // (splitType == -1) } @@ -3676,17 +3676,17 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { bool overlapAssumptionUsed = false; - TRACE("t_str_detail", tout << "process_concat_eq TYPE 3" << std::endl + TRACE("str", tout << "process_concat_eq TYPE 3" << std::endl << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; ); if (!u.str.is_concat(to_app(concatAst1))) { - TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); + TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); return; } if (!u.str.is_concat(to_app(concatAst2))) { - TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); + TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); return; } @@ -3756,7 +3756,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { } } - TRACE("t_str_detail", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl + TRACE("str", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); @@ -3798,7 +3798,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { splitType = 2; } - TRACE("t_str_detail", tout << "Split type " << splitType << std::endl;); + TRACE("str", tout << "Split type " << splitType << std::endl;); // Provide fewer split options when length information is available. if (splitType == 0) { @@ -3836,7 +3836,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { } } else { // negate! It's impossible to split str with these lengths - TRACE("t_str", tout << "CONFLICT: Impossible to split str with these lengths." << std::endl;); + TRACE("str", tout << "CONFLICT: Impossible to split str with these lengths." << std::endl;); assert_axiom(mgr.mk_not(ax_l)); } } @@ -3899,8 +3899,8 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { assert_implication(ax_l, tester); add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { - TRACE("t_str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - TRACE("t_str_detail", {print_cut_var(x, tout); print_cut_var(n, tout);}); + TRACE("str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + TRACE("str", {print_cut_var(x, tout); print_cut_var(n, tout);}); if (!overlapAssumptionUsed) { overlapAssumptionUsed = true; @@ -3983,8 +3983,8 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { arrangement_disjunction.push_back(tester); add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { - TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); - TRACE("t_str_detail", {print_cut_var(x, tout); print_cut_var(n, tout);}); + TRACE("str", tout << "AVOID LOOP: SKIPPED." << std::endl;); + TRACE("str", {print_cut_var(x, tout); print_cut_var(n, tout);}); if (!overlapAssumptionUsed) { overlapAssumptionUsed = true; @@ -4007,7 +4007,7 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { } generate_mutual_exclusion(arrangement_disjunction); } else { - TRACE("t_str", tout << "STOP: should not split two eq. concats" << std::endl;); + TRACE("str", tout << "STOP: should not split two eq. concats" << std::endl;); } } @@ -4033,17 +4033,17 @@ bool theory_str::is_concat_eq_type4(expr * concatAst1, expr * concatAst2) { void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { ast_manager & mgr = get_manager(); context & ctx = get_context(); - TRACE("t_str_detail", tout << "process_concat_eq TYPE 4" << std::endl + TRACE("str", tout << "process_concat_eq TYPE 4" << std::endl << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; ); if (!u.str.is_concat(to_app(concatAst1))) { - TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); + TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); return; } if (!u.str.is_concat(to_app(concatAst2))) { - TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); + TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); return; } @@ -4066,7 +4066,7 @@ void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { int commonLen = (str1Len > str2Len) ? str2Len : str1Len; if (str1Value.extract(0, commonLen) != str2Value.extract(0, commonLen)) { - TRACE("t_str_detail", tout << "Conflict: " << mk_ismt2_pp(concatAst1, mgr) + TRACE("str", tout << "Conflict: " << mk_ismt2_pp(concatAst1, mgr) << " has no common prefix with " << mk_ismt2_pp(concatAst2, mgr) << std::endl;); expr_ref toNegate(mgr.mk_not(ctx.mk_eq_atom(concatAst1, concatAst2)), mgr); assert_axiom(toNegate); @@ -4134,17 +4134,17 @@ bool theory_str::is_concat_eq_type5(expr * concatAst1, expr * concatAst2) { void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { ast_manager & mgr = get_manager(); context & ctx = get_context(); - TRACE("t_str_detail", tout << "process_concat_eq TYPE 5" << std::endl + TRACE("str", tout << "process_concat_eq TYPE 5" << std::endl << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; ); if (!u.str.is_concat(to_app(concatAst1))) { - TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); + TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); return; } if (!u.str.is_concat(to_app(concatAst2))) { - TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); + TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); return; } @@ -4167,7 +4167,7 @@ void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { int cLen = (str1Len > str2Len) ? str2Len : str1Len; if (str1Value.extract(str1Len - cLen, cLen) != str2Value.extract(str2Len - cLen, cLen)) { - TRACE("t_str_detail", tout << "Conflict: " << mk_ismt2_pp(concatAst1, mgr) + TRACE("str", tout << "Conflict: " << mk_ismt2_pp(concatAst1, mgr) << " has no common suffix with " << mk_ismt2_pp(concatAst2, mgr) << std::endl;); expr_ref toNegate(mgr.mk_not(ctx.mk_eq_atom(concatAst1, concatAst2)), mgr); assert_axiom(toNegate); @@ -4235,17 +4235,17 @@ bool theory_str::is_concat_eq_type6(expr * concatAst1, expr * concatAst2) { void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { ast_manager & mgr = get_manager(); context & ctx = get_context(); - TRACE("t_str_detail", tout << "process_concat_eq TYPE 6" << std::endl + TRACE("str", tout << "process_concat_eq TYPE 6" << std::endl << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; ); if (!u.str.is_concat(to_app(concatAst1))) { - TRACE("t_str_detail", tout << "concatAst1 is not a concat function" << std::endl;); + TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); return; } if (!u.str.is_concat(to_app(concatAst2))) { - TRACE("t_str_detail", tout << "concatAst2 is not a concat function" << std::endl;); + TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); return; } @@ -4334,7 +4334,7 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { } } - TRACE("t_str_detail", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl + TRACE("str", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); if (!entry1InScope && !entry2InScope) { @@ -4389,8 +4389,8 @@ void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { arrangement_disjunction.push_back(tester); add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { - TRACE("t_str", tout << "AVOID LOOP: SKIPPED." << std::endl;); - TRACE("t_str", print_cut_var(m, tout); print_cut_var(y, tout);); + TRACE("str", tout << "AVOID LOOP: SKIPPED." << std::endl;); + TRACE("str", print_cut_var(m, tout); print_cut_var(y, tout);); // only add the overlap assumption one time if (!overlapAssumptionUsed) { @@ -4465,7 +4465,7 @@ void theory_str::process_unroll_eq_const_str(expr * unrollFunc, expr * constStr) zstring strValue; u.str.is_string(constStr, strValue); - TRACE("t_str_detail", tout << "unrollFunc: " << mk_pp(unrollFunc, m) << std::endl + TRACE("str", tout << "unrollFunc: " << mk_pp(unrollFunc, m) << std::endl << "constStr: " << mk_pp(constStr, m) << std::endl;); if (strValue == "") { @@ -4482,7 +4482,7 @@ void theory_str::process_concat_eq_unroll(expr * concat, expr * unroll) { context & ctx = get_context(); ast_manager & mgr = get_manager(); - TRACE("t_str_detail", tout << "concat = " << mk_pp(concat, mgr) << ", unroll = " << mk_pp(unroll, mgr) << std::endl;); + TRACE("str", tout << "concat = " << mk_pp(concat, mgr) << ", unroll = " << mk_pp(unroll, mgr) << std::endl;); std::pair key = std::make_pair(concat, unroll); expr_ref toAssert(mgr); @@ -4613,7 +4613,7 @@ static theory_mi_arith* get_th_arith(context& ctx, theory_id afid, expr* e) { bool theory_str::get_value(expr* e, rational& val) const { if (opt_DisableIntegerTheoryIntegration) { - TRACE("t_str_detail", tout << "WARNING: integer theory integration disabled" << std::endl;); + TRACE("str", tout << "WARNING: integer theory integration disabled" << std::endl;); return false; } @@ -4623,28 +4623,28 @@ bool theory_str::get_value(expr* e, rational& val) const { if (!tha) { return false; } - TRACE("t_str_int", tout << "checking eqc of " << mk_pp(e, m) << " for arithmetic value" << std::endl;); + TRACE("str", tout << "checking eqc of " << mk_pp(e, m) << " for arithmetic value" << std::endl;); expr_ref _val(m); enode * en_e = ctx.get_enode(e); enode * it = en_e; do { if (m_autil.is_numeral(it->get_owner(), val) && val.is_int()) { // found an arithmetic term - TRACE("t_str_int", tout << mk_pp(it->get_owner(), m) << " is an integer ( ~= " << val << " )" + TRACE("str", tout << mk_pp(it->get_owner(), m) << " is an integer ( ~= " << val << " )" << std::endl;); return true; } else { - TRACE("t_str_int", tout << mk_pp(it->get_owner(), m) << " not a numeral" << std::endl;); + TRACE("str", tout << mk_pp(it->get_owner(), m) << " not a numeral" << std::endl;); } it = it->get_next(); } while (it != en_e); - TRACE("t_str_int", tout << "no arithmetic values found in eqc" << std::endl;); + TRACE("str", tout << "no arithmetic values found in eqc" << std::endl;); return false; } bool theory_str::lower_bound(expr* _e, rational& lo) { if (opt_DisableIntegerTheoryIntegration) { - TRACE("t_str_detail", tout << "WARNING: integer theory integration disabled" << std::endl;); + TRACE("str", tout << "WARNING: integer theory integration disabled" << std::endl;); return false; } @@ -4658,7 +4658,7 @@ bool theory_str::lower_bound(expr* _e, rational& lo) { bool theory_str::upper_bound(expr* _e, rational& hi) { if (opt_DisableIntegerTheoryIntegration) { - TRACE("t_str_detail", tout << "WARNING: integer theory integration disabled" << std::endl;); + TRACE("str", tout << "WARNING: integer theory integration disabled" << std::endl;); return false; } @@ -4672,7 +4672,7 @@ bool theory_str::upper_bound(expr* _e, rational& hi) { bool theory_str::get_len_value(expr* e, rational& val) { if (opt_DisableIntegerTheoryIntegration) { - TRACE("t_str_detail", tout << "WARNING: integer theory integration disabled" << std::endl;); + TRACE("str", tout << "WARNING: integer theory integration disabled" << std::endl;); return false; } @@ -4681,16 +4681,16 @@ bool theory_str::get_len_value(expr* e, rational& val) { theory* th = ctx.get_theory(m_autil.get_family_id()); if (!th) { - TRACE("t_str_int", tout << "oops, can't get m_autil's theory" << std::endl;); + TRACE("str", tout << "oops, can't get m_autil's theory" << std::endl;); return false; } theory_mi_arith* tha = dynamic_cast(th); if (!tha) { - TRACE("t_str_int", tout << "oops, can't cast to theory_mi_arith" << std::endl;); + TRACE("str", tout << "oops, can't cast to theory_mi_arith" << std::endl;); return false; } - TRACE("t_str_int", tout << "checking len value of " << mk_ismt2_pp(e, m) << std::endl;); + TRACE("str", tout << "checking len value of " << mk_ismt2_pp(e, m) << std::endl;); rational val1; expr_ref len(m), len_val(m); @@ -4717,7 +4717,7 @@ bool theory_str::get_len_value(expr* e, rational& val) { len = mk_strlen(c); // debugging - TRACE("t_str_int", { + TRACE("str", { tout << mk_pp(len, m) << ":" << std::endl << (ctx.is_relevant(len.get()) ? "relevant" : "not relevant") << std::endl << (ctx.e_internalized(len) ? "internalized" : "not internalized") << std::endl @@ -4742,16 +4742,16 @@ bool theory_str::get_len_value(expr* e, rational& val) { if (ctx.e_internalized(len) && get_value(len, val1)) { val += val1; - TRACE("t_str_int", tout << "integer theory: subexpression " << mk_ismt2_pp(len, m) << " has length " << val1 << std::endl;); + TRACE("str", tout << "integer theory: subexpression " << mk_ismt2_pp(len, m) << " has length " << val1 << std::endl;); } else { - TRACE("t_str_int", tout << "integer theory: subexpression " << mk_ismt2_pp(len, m) << " has no length assignment; bailing out" << std::endl;); + TRACE("str", tout << "integer theory: subexpression " << mk_ismt2_pp(len, m) << " has no length assignment; bailing out" << std::endl;); return false; } } } - TRACE("t_str_int", tout << "length of " << mk_ismt2_pp(e, m) << " is " << val << std::endl;); + TRACE("str", tout << "length of " << mk_ismt2_pp(e, m) << " is " << val << std::endl;); return val.is_int(); } @@ -4769,11 +4769,11 @@ bool theory_str::in_same_eqc(expr * n1, expr * n2) { // that we've set this up properly for the context if (!ctx.e_internalized(n1)) { - TRACE("t_str_detail", tout << "WARNING: expression " << mk_ismt2_pp(n1, m) << " was not internalized" << std::endl;); + TRACE("str", tout << "WARNING: expression " << mk_ismt2_pp(n1, m) << " was not internalized" << std::endl;); ctx.internalize(n1, false); } if (!ctx.e_internalized(n2)) { - TRACE("t_str_detail", tout << "WARNING: expression " << mk_ismt2_pp(n2, m) << " was not internalized" << std::endl;); + TRACE("str", tout << "WARNING: expression " << mk_ismt2_pp(n2, m) << " was not internalized" << std::endl;); ctx.internalize(n2, false); } @@ -4824,7 +4824,7 @@ void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { context & ctx = get_context(); ast_manager & m = get_manager(); - TRACE("t_str_detail", tout << "varNode = " << mk_pp(varNode, m) << ", constNode = " << mk_pp(constNode, m) << std::endl;); + TRACE("str", tout << "varNode = " << mk_pp(varNode, m) << ", constNode = " << mk_pp(constNode, m) << std::endl;); expr_ref_vector litems(m); @@ -4836,7 +4836,7 @@ void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { expr * boolVar; if (!contain_pair_bool_map.find(strAst, substrAst, boolVar)) { - TRACE("t_str_detail", tout << "warning: no entry for boolVar in contain_pair_bool_map" << std::endl;); + TRACE("str", tout << "warning: no entry for boolVar in contain_pair_bool_map" << std::endl;); } // boolVar is actually a Contains term app * containsApp = to_app(boolVar); @@ -4844,13 +4844,13 @@ void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { // we only want to inspect the Contains terms where either of strAst or substrAst // are equal to varNode. - TRACE("t_str_detail", tout << "considering Contains with strAst = " << mk_pp(strAst, m) << ", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); + TRACE("t_str_detail", tout << "considering Contains with strAst = "str", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); if (varNode != strAst && varNode != substrAst) { - TRACE("t_str_detail", tout << "varNode not equal to strAst or substrAst, skip" << std::endl;); + TRACE("str", tout << "varNode not equal to strAst or substrAst, skip" << std::endl;); continue; } - TRACE("t_str_detail", tout << "varNode matched one of strAst or substrAst. Continuing" << std::endl;); + TRACE("str", tout << "varNode matched one of strAst or substrAst. Continuing" << std::endl;); // varEqcNode is str if (strAst == varNode) { @@ -4873,7 +4873,7 @@ void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { zstring subStrConst; u.str.is_string(substrValue, subStrConst); - TRACE("t_str_detail", tout << "strConst = " << strConst << ", subStrConst = " << subStrConst << "\n";); + TRACE("t_str_detail", tout << "strConst = "str", subStrConst = " << subStrConst << "\n";); if (strConst.contains(subStrConst)) { //implyR = ctx.mk_eq(ctx, boolVar, Z3_mk_true(ctx)); @@ -4914,7 +4914,7 @@ void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { } } if (counterEgFound) { - TRACE("t_str_detail", tout << "Inconsistency found!" << std::endl;); + TRACE("str", tout << "Inconsistency found!" << std::endl;); break; } } @@ -4975,7 +4975,7 @@ void theory_str::check_contain_by_substr(expr * varNode, expr_ref_vector & willE expr * boolVar; if (!contain_pair_bool_map.find(strAst, substrAst, boolVar)) { - TRACE("t_str_detail", tout << "warning: no entry for boolVar in contain_pair_bool_map" << std::endl;); + TRACE("str", tout << "warning: no entry for boolVar in contain_pair_bool_map" << std::endl;); } // boolVar is actually a Contains term app * containsApp = to_app(boolVar); @@ -4983,19 +4983,19 @@ void theory_str::check_contain_by_substr(expr * varNode, expr_ref_vector & willE // we only want to inspect the Contains terms where either of strAst or substrAst // are equal to varNode. - TRACE("t_str_detail", tout << "considering Contains with strAst = " << mk_pp(strAst, m) << ", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); + TRACE("t_str_detail", tout << "considering Contains with strAst = "str", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); if (varNode != strAst && varNode != substrAst) { - TRACE("t_str_detail", tout << "varNode not equal to strAst or substrAst, skip" << std::endl;); + TRACE("str", tout << "varNode not equal to strAst or substrAst, skip" << std::endl;); continue; } - TRACE("t_str_detail", tout << "varNode matched one of strAst or substrAst. Continuing" << std::endl;); + TRACE("str", tout << "varNode matched one of strAst or substrAst. Continuing" << std::endl;); if (substrAst == varNode) { bool strAstHasVal = false; expr * strValue = get_eqc_value(strAst, strAstHasVal); if (strAstHasVal) { - TRACE("t_str_detail", tout << mk_pp(strAst, m) << " has constant eqc value " << mk_pp(strValue, m) << std::endl;); + TRACE("str", tout << mk_pp(strAst, m) << " has constant eqc value " << mk_pp(strValue, m) << std::endl;); if (strValue != strAst) { litems.push_back(ctx.mk_eq_atom(strAst, strValue)); } @@ -5014,7 +5014,7 @@ void theory_str::check_contain_by_substr(expr * varNode, expr_ref_vector & willE zstring pieceStr; u.str.is_string(*cstItor, pieceStr); if (!strConst.contains(pieceStr)) { - TRACE("t_str_detail", tout << "Inconsistency found!" << std::endl;); + TRACE("str", tout << "Inconsistency found!" << std::endl;); counterEgFound = true; if (aConcat != substrAst) { litems.push_back(ctx.mk_eq_atom(substrAst, aConcat)); @@ -5082,7 +5082,7 @@ void theory_str::check_contain_by_eq_nodes(expr * n1, expr * n2) { expr * subValue1 = get_eqc_value(subAst1, subAst1HasValue); expr * subValue2 = get_eqc_value(subAst2, subAst2HasValue); - TRACE("t_str_detail", + TRACE("str", tout << "(Contains " << mk_pp(n1, m) << " " << mk_pp(subAst1, m) << ")" << std::endl; tout << "(Contains " << mk_pp(n2, m) << " " << mk_pp(subAst2, m) << ")" << std::endl; if (subAst1 != subValue1) { @@ -5182,7 +5182,7 @@ void theory_str::check_contain_by_eq_nodes(expr * n1, expr * n2) { } std::pair tryKey1 = std::make_pair(eqSubVar1, eqSubVar2); if (contain_pair_bool_map.contains(tryKey1)) { - TRACE("t_str_detail", tout << "(Contains " << mk_pp(eqSubVar1, m) << " " << mk_pp(eqSubVar2, m) << ")" << std::endl;); + TRACE("str", tout << "(Contains " << mk_pp(eqSubVar1, m) << " " << mk_pp(eqSubVar2, m) << ")" << std::endl;); litems3.push_back(contain_pair_bool_map[tryKey1]); expr_ref implR(rewrite_implication(contain_pair_bool_map[key1], contain_pair_bool_map[key2]), m); assert_implication(mk_and(litems3), implR); @@ -5207,7 +5207,7 @@ void theory_str::check_contain_by_eq_nodes(expr * n1, expr * n2) { } std::pair tryKey2 = std::make_pair(eqSubVar2, eqSubVar1); if (contain_pair_bool_map.contains(tryKey2)) { - TRACE("t_str_detail", tout << "(Contains " << mk_pp(eqSubVar2, m) << " " << mk_pp(eqSubVar1, m) << ")" << std::endl;); + TRACE("str", tout << "(Contains " << mk_pp(eqSubVar2, m) << " " << mk_pp(eqSubVar1, m) << ")" << std::endl;); litems4.push_back(contain_pair_bool_map[tryKey2]); expr_ref implR(rewrite_implication(contain_pair_bool_map[key2], contain_pair_bool_map[key1]), m); assert_implication(mk_and(litems4), implR); @@ -5229,7 +5229,7 @@ void theory_str::check_contain_by_eq_nodes(expr * n1, expr * n2) { expr * strVal1 = get_eqc_value(str1, str1HasValue); expr * strVal2 = get_eqc_value(str2, str2HasValue); - TRACE("t_str_detail", + TRACE("str", tout << "(Contains " << mk_pp(str1, m) << " " << mk_pp(n1, m) << ")" << std::endl; tout << "(Contains " << mk_pp(str2, m) << " " << mk_pp(n2, m) << ")" << std::endl; if (str1 != strVal1) { @@ -5328,7 +5328,7 @@ void theory_str::check_contain_by_eq_nodes(expr * n1, expr * n2) { } std::pair tryKey1 = std::make_pair(eqStrVar1, eqStrVar2); if (contain_pair_bool_map.contains(tryKey1)) { - TRACE("t_str_detail", tout << "(Contains " << mk_pp(eqStrVar1, m) << " " << mk_pp(eqStrVar2, m) << ")" << std::endl;); + TRACE("str", tout << "(Contains " << mk_pp(eqStrVar1, m) << " " << mk_pp(eqStrVar2, m) << ")" << std::endl;); litems3.push_back(contain_pair_bool_map[tryKey1]); // ------------ @@ -5356,7 +5356,7 @@ void theory_str::check_contain_by_eq_nodes(expr * n1, expr * n2) { std::pair tryKey2 = std::make_pair(eqStrVar2, eqStrVar1); if (contain_pair_bool_map.contains(tryKey2)) { - TRACE("t_str_detail", tout << "(Contains " << mk_pp(eqStrVar2, m) << " " << mk_pp(eqStrVar1, m) << ")" << std::endl;); + TRACE("str", tout << "(Contains " << mk_pp(eqStrVar2, m) << " " << mk_pp(eqStrVar1, m) << ")" << std::endl;); litems4.push_back(contain_pair_bool_map[tryKey2]); // ------------ // key1.first = key2.first /\ containPairBoolMap[] @@ -5388,14 +5388,14 @@ void theory_str::check_contain_in_new_eq(expr * n1, expr * n2) { context & ctx = get_context(); ast_manager & m = get_manager(); - TRACE("t_str_detail", tout << "consistency check for contains wrt. " << mk_pp(n1, m) << " and " << mk_pp(n2, m) << std::endl;); + TRACE("str", tout << "consistency check for contains wrt. " << mk_pp(n1, m) << " and " << mk_pp(n2, m) << std::endl;); expr_ref_vector willEqClass(m); expr * constStrAst_1 = collect_eq_nodes(n1, willEqClass); expr * constStrAst_2 = collect_eq_nodes(n2, willEqClass); expr * constStrAst = (constStrAst_1 != NULL) ? constStrAst_1 : constStrAst_2; - TRACE("t_str_detail", tout << "eqc of n1 is {"; + TRACE("str", tout << "eqc of n1 is {"; for (expr_ref_vector::iterator it = willEqClass.begin(); it != willEqClass.end(); ++it) { expr * el = *it; tout << " " << mk_pp(el, m); @@ -5589,11 +5589,11 @@ void theory_str::get_grounded_concats(expr* node, std::map & varAl void theory_str::print_grounded_concat(expr * node, std::map, std::set > > & groundedMap) { ast_manager & m = get_manager(); - TRACE("t_str_detail", tout << mk_pp(node, m) << std::endl;); + TRACE("str", tout << mk_pp(node, m) << std::endl;); if (groundedMap.find(node) != groundedMap.end()) { std::map, std::set >::iterator itor = groundedMap[node].begin(); for (; itor != groundedMap[node].end(); ++itor) { - TRACE("t_str_detail", + TRACE("str", tout << "\t[grounded] "; std::vector::const_iterator vIt = itor->first.begin(); for (; vIt != itor->first.end(); ++vIt) { @@ -5609,7 +5609,7 @@ void theory_str::print_grounded_concat(expr * node, std::map string constant (len = " << strLen << ")" << std::endl;); + TRACE("str", tout << "inconsistent length: concat (len = " << sumLen << ") <==> string constant (len = " << strLen << ")" << std::endl;); assert_axiom(toAssert); return false; } @@ -5948,7 +5948,7 @@ bool theory_str::check_length_const_string(expr * n1, expr * constStr) { rational oLen; bool oLen_exists = get_len_value(n1, oLen); if (oLen_exists && oLen != strLen) { - TRACE("t_str_detail", tout << "inconsistent length: var (len = " << oLen << ") <==> string constant (len = " << strLen << ")" << std::endl;); + TRACE("str", tout << "inconsistent length: var (len = " << oLen << ") <==> string constant (len = " << strLen << ")" << std::endl;); expr_ref l(ctx.mk_eq_atom(n1, constStr), mgr); expr_ref r(ctx.mk_eq_atom(mk_strlen(n1), mk_strlen(constStr)), mgr); assert_implication(l, r); @@ -6027,7 +6027,7 @@ bool theory_str::check_length_concat_concat(expr * n1, expr * n2) { } if (conflict) { - TRACE("t_str_detail", tout << "inconsistent length detected in concat <==> concat" << std::endl;); + TRACE("str", tout << "inconsistent length detected in concat <==> concat" << std::endl;); expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); assert_axiom(toAssert); return false; @@ -6058,7 +6058,7 @@ bool theory_str::check_length_concat_var(expr * concat, expr * var) { } sumLen += argLen; if (sumLen > varLen) { - TRACE("t_str_detail", tout << "inconsistent length detected in concat <==> var" << std::endl;); + TRACE("str", tout << "inconsistent length detected in concat <==> var" << std::endl;); items.push_back(ctx.mk_eq_atom(mk_strlen(var), mk_int(varLen))); items.push_back(ctx.mk_eq_atom(concat, var)); expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); @@ -6080,7 +6080,7 @@ bool theory_str::check_length_var_var(expr * var1, expr * var2) { bool var2Len_exists = get_len_value(var2, var2Len); if (var1Len_exists && var2Len_exists && var1Len != var2Len) { - TRACE("t_str_detail", tout << "inconsistent length detected in var <==> var" << std::endl;); + TRACE("str", tout << "inconsistent length detected in var <==> var" << std::endl;); expr_ref_vector items(mgr); items.push_back(ctx.mk_eq_atom(mk_strlen(var1), mk_int(var1Len))); items.push_back(ctx.mk_eq_atom(mk_strlen(var2), mk_int(var2Len))); @@ -6164,7 +6164,7 @@ void nfa::convert_re(expr * e, unsigned & start, unsigned & end, seq_util & u) { expr * arg_str = a->get_arg(0); zstring str; if (u.str.is_string(arg_str, str)) { - TRACE("t_str_rw", tout << "build NFA for '" << str << "'" << "\n";); + TRACE("str", tout << "build NFA for '" << str << "'" << "\n";); /* * For an n-character string, we make (n-1) intermediate states, @@ -6176,14 +6176,14 @@ void nfa::convert_re(expr * e, unsigned & start, unsigned & end, seq_util & u) { for (int i = 0; i <= ((int)str.length()) - 2; ++i) { unsigned i_state = next_id(); make_transition(last, str[i], i_state); - TRACE("t_str_rw", tout << "string transition " << last << "--" << str[i] << "--> " << i_state << "\n";); + TRACE("str", tout << "string transition " << last << "--" << str[i] << "--> " << i_state << "\n";); last = i_state; } make_transition(last, str[(str.length() - 1)], end); - TRACE("t_str_rw", tout << "string transition " << last << "--" << str[(str.length() - 1)] << "--> " << end << "\n";); - TRACE("t_str_rw", tout << "string NFA: start = " << start << ", end = " << end << std::endl;); + TRACE("str", tout << "string transition " << last << "--" << str[(str.length() - 1)] << "--> " << end << "\n";); + TRACE("t_str_rw", tout << "str", end = " << end << std::endl;); } else { - TRACE("t_str_rw", tout << "invalid string constant in Str2Reg" << std::endl;); + TRACE("str", tout << "invalid string constant in Str2Reg" << std::endl;); m_valid = false; return; } @@ -6199,7 +6199,7 @@ void nfa::convert_re(expr * e, unsigned & start, unsigned & end, seq_util & u) { make_epsilon_move(start, start1); make_epsilon_move(end1, start2); make_epsilon_move(end2, end); - TRACE("t_str_rw", tout << "concat NFA: start = " << start << ", end = " << end << std::endl;); + TRACE("str", tout << "concat NFA: start = " << start << ", end = " << end << std::endl;); } else if (u.re.is_union(e)) { app * a = to_app(e); expr * re1 = a->get_arg(0); @@ -6215,7 +6215,7 @@ void nfa::convert_re(expr * e, unsigned & start, unsigned & end, seq_util & u) { make_epsilon_move(start, start2); make_epsilon_move(end1, end); make_epsilon_move(end2, end); - TRACE("t_str_rw", tout << "union NFA: start = " << start << ", end = " << end << std::endl;); + TRACE("str", tout << "union NFA: start = " << start << ", end = " << end << std::endl;); } else if (u.re.is_star(e)) { app * a = to_app(e); expr * subex = a->get_arg(0); @@ -6227,9 +6227,9 @@ void nfa::convert_re(expr * e, unsigned & start, unsigned & end, seq_util & u) { make_epsilon_move(start, end); make_epsilon_move(end_subex, start_subex); make_epsilon_move(end_subex, end); - TRACE("t_str_rw", tout << "star NFA: start = " << start << ", end = " << end << std::endl;); + TRACE("str", tout << "star NFA: start = " << start << ", end = " << end << std::endl;); } else { - TRACE("t_str_rw", tout << "invalid regular expression" << std::endl;); + TRACE("str", tout << "invalid regular expression" << std::endl;); m_valid = false; return; } @@ -6327,17 +6327,17 @@ void theory_str::check_regex_in(expr * nn1, expr * nn2) { // TODO figure out regex NFA stuff if (regex_nfa_cache.find(regexTerm) == regex_nfa_cache.end()) { - TRACE("t_str_detail", tout << "regex_nfa_cache: cache miss" << std::endl;); + TRACE("str", tout << "regex_nfa_cache: cache miss" << std::endl;); regex_nfa_cache[regexTerm] = nfa(u, regexTerm); } else { - TRACE("t_str_detail", tout << "regex_nfa_cache: cache hit" << std::endl;); + TRACE("str", tout << "regex_nfa_cache: cache hit" << std::endl;); } nfa regexNFA = regex_nfa_cache[regexTerm]; ENSURE(regexNFA.is_valid()); bool matchRes = regexNFA.matches(constStrValue); - TRACE("t_str_detail", tout << mk_pp(*itor, m) << " in " << regStr << " : " << (matchRes ? "yes" : "no") << std::endl;); + TRACE("str", tout << mk_pp(*itor, m) << " in " << regStr << " : " << (matchRes ? "yes" : "no") << std::endl;); expr_ref implyL(ctx.mk_eq_atom(*itor, constStr), m); if (matchRes) { @@ -6362,7 +6362,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { ast_manager & m = get_manager(); context & ctx = get_context(); - TRACE("t_str_detail", tout << mk_ismt2_pp(concat, m) << " == " << mk_ismt2_pp(str, m) << std::endl;); + TRACE("str", tout << mk_ismt2_pp(concat, m) << " == " << mk_ismt2_pp(str, m) << std::endl;); zstring const_str; if (u.str.is_concat(to_app(concat)) && u.str.is_string(to_app(str), const_str)) { @@ -6372,7 +6372,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { expr * a2 = a_concat->get_arg(1); if (const_str.empty()) { - TRACE("t_str", tout << "quick path: concat == \"\"" << std::endl;); + TRACE("str", tout << "quick path: concat == \"\"" << std::endl;); // assert the following axiom: // ( (Concat a1 a2) == "" ) -> ( (a1 == "") AND (a2 == "") ) @@ -6391,7 +6391,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { expr * arg2 = get_eqc_value(a2, arg2_has_eqc_value); expr_ref newConcat(m); if (arg1 != a1 || arg2 != a2) { - TRACE("t_str", tout << "resolved concat argument(s) to eqc string constants" << std::endl;); + TRACE("str", tout << "resolved concat argument(s) to eqc string constants" << std::endl;); int iPos = 0; expr_ref_vector item1(m); if (a1 != arg1) { @@ -6419,7 +6419,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } if (arg1_has_eqc_value && arg2_has_eqc_value) { // Case 1: Concat(const, const) == const - TRACE("t_str", tout << "Case 1: Concat(const, const) == const" << std::endl;); + TRACE("str", tout << "Case 1: Concat(const, const) == const" << std::endl;); zstring arg1_str, arg2_str; u.str.is_string(arg1, arg1_str); u.str.is_string(arg2, arg2_str); @@ -6427,7 +6427,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { zstring result_str = arg1_str + arg2_str; if (result_str != const_str) { // Inconsistency - TRACE("t_str", tout << "inconsistency detected: \"" + TRACE("str", tout << "inconsistency detected: \"" << arg1_str << "\" + \"" << arg2_str << "\" != \"" << const_str << "\"" << "\n";); expr_ref equality(ctx.mk_eq_atom(concat, str), m); @@ -6437,14 +6437,14 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } } else if (!arg1_has_eqc_value && arg2_has_eqc_value) { // Case 2: Concat(var, const) == const - TRACE("t_str", tout << "Case 2: Concat(var, const) == const" << std::endl;); + TRACE("str", tout << "Case 2: Concat(var, const) == const" << std::endl;); zstring arg2_str; u.str.is_string(arg2, arg2_str); unsigned int resultStrLen = const_str.length(); unsigned int arg2StrLen = arg2_str.length(); if (resultStrLen < arg2StrLen) { // Inconsistency - TRACE("t_str", tout << "inconsistency detected: \"" + TRACE("str", tout << "inconsistency detected: \"" << arg2_str << "\" is longer than \"" << const_str << "\"," << " so cannot be concatenated with anything to form it" << "\n";); @@ -6458,7 +6458,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { zstring secondPart = const_str.extract(varStrLen, arg2StrLen); if (arg2_str != secondPart) { // Inconsistency - TRACE("t_str", tout << "inconsistency detected: " + TRACE("str", tout << "inconsistency detected: " << "suffix of concatenation result expected \"" << secondPart << "\", " << "actually \"" << arg2_str << "\"" << "\n";); @@ -6476,14 +6476,14 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } } else if (arg1_has_eqc_value && !arg2_has_eqc_value) { // Case 3: Concat(const, var) == const - TRACE("t_str", tout << "Case 3: Concat(const, var) == const" << std::endl;); + TRACE("str", tout << "Case 3: Concat(const, var) == const" << std::endl;); zstring arg1_str; u.str.is_string(arg1, arg1_str); unsigned int resultStrLen = const_str.length(); unsigned int arg1StrLen = arg1_str.length(); if (resultStrLen < arg1StrLen) { // Inconsistency - TRACE("t_str", tout << "inconsistency detected: \"" + TRACE("str", tout << "inconsistency detected: \"" << arg1_str << "\" is longer than \"" << const_str << "\"," << " so cannot be concatenated with anything to form it" << "\n";); @@ -6497,7 +6497,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { zstring secondPart = const_str.extract(arg1StrLen, varStrLen); if (arg1_str != firstPart) { // Inconsistency - TRACE("t_str", tout << "inconsistency detected: " + TRACE("str", tout << "inconsistency detected: " << "prefix of concatenation result expected \"" << secondPart << "\", " << "actually \"" << arg1_str << "\"" << "\n";); @@ -6515,7 +6515,7 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } } else { // Case 4: Concat(var, var) == const - TRACE("t_str", tout << "Case 4: Concat(var, var) == const" << std::endl;); + TRACE("str", tout << "Case 4: Concat(var, var) == const" << std::endl;); if (eval_concat(arg1, arg2) == NULL) { rational arg1Len, arg2Len; bool arg1Len_exists = get_len_value(arg1, arg1Len); @@ -6527,12 +6527,12 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { zstring prefixStr, suffixStr; if (arg1Len_exists) { if (arg1Len.is_neg()) { - TRACE("t_str_detail", tout << "length conflict: arg1Len = " << arg1Len << ", concatStrLen = " << concatStrLen << std::endl;); + TRACE("str", tout << "length conflict: arg1Len = " << arg1Len << ", concatStrLen = " << concatStrLen << std::endl;); expr_ref toAssert(m_autil.mk_ge(mk_strlen(arg1), mk_int(0)), m); assert_axiom(toAssert); return; } else if (arg1Len > concatStrLen) { - TRACE("t_str_detail", tout << "length conflict: arg1Len = " << arg1Len << ", concatStrLen = " << concatStrLen << std::endl;); + TRACE("str", tout << "length conflict: arg1Len = " << arg1Len << ", concatStrLen = " << concatStrLen << std::endl;); expr_ref ax_r1(m_autil.mk_le(mk_strlen(arg1), mk_int(concatStrLen)), m); assert_implication(ax_l1, ax_r1); return; @@ -6545,12 +6545,12 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { } else { // arg2's length is available if (arg2Len.is_neg()) { - TRACE("t_str_detail", tout << "length conflict: arg2Len = " << arg2Len << ", concatStrLen = " << concatStrLen << std::endl;); + TRACE("str", tout << "length conflict: arg2Len = " << arg2Len << ", concatStrLen = " << concatStrLen << std::endl;); expr_ref toAssert(m_autil.mk_ge(mk_strlen(arg2), mk_int(0)), m); assert_axiom(toAssert); return; } else if (arg2Len > concatStrLen) { - TRACE("t_str_detail", tout << "length conflict: arg2Len = " << arg2Len << ", concatStrLen = " << concatStrLen << std::endl;); + TRACE("str", tout << "length conflict: arg2Len = " << arg2Len << ", concatStrLen = " << concatStrLen << std::endl;); expr_ref ax_r1(m_autil.mk_le(mk_strlen(arg2), mk_int(concatStrLen)), m); assert_implication(ax_l1, ax_r1); return; @@ -6597,18 +6597,18 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { bool entry1InScope; if (entry1 == varForBreakConcat.end()) { - TRACE("t_str_detail", tout << "key1 no entry" << std::endl;); + TRACE("str", tout << "key1 no entry" << std::endl;); entry1InScope = false; } else { // OVERRIDE. entry1InScope = true; - TRACE("t_str_detail", tout << "key1 entry" << std::endl;); + TRACE("str", tout << "key1 entry" << std::endl;); /* if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end()) { - TRACE("t_str_detail", tout << "key1 entry not in scope" << std::endl;); + TRACE("str", tout << "key1 entry not in scope" << std::endl;); entry1InScope = false; } else { - TRACE("t_str_detail", tout << "key1 entry in scope" << std::endl;); + TRACE("str", tout << "key1 entry in scope" << std::endl;); entry1InScope = true; } */ @@ -6616,24 +6616,24 @@ void theory_str::solve_concat_eq_str(expr * concat, expr * str) { bool entry2InScope; if (entry2 == varForBreakConcat.end()) { - TRACE("t_str_detail", tout << "key2 no entry" << std::endl;); + TRACE("str", tout << "key2 no entry" << std::endl;); entry2InScope = false; } else { // OVERRIDE. entry2InScope = true; - TRACE("t_str_detail", tout << "key2 entry" << std::endl;); + TRACE("str", tout << "key2 entry" << std::endl;); /* if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end()) { - TRACE("t_str_detail", tout << "key2 entry not in scope" << std::endl;); + TRACE("str", tout << "key2 entry not in scope" << std::endl;); entry2InScope = false; } else { - TRACE("t_str_detail", tout << "key2 entry in scope" << std::endl;); + TRACE("str", tout << "key2 entry in scope" << std::endl;); entry2InScope = true; } */ } - TRACE("t_str_detail", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl + TRACE("str", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); if (!entry1InScope && !entry2InScope) { @@ -6702,14 +6702,14 @@ expr_ref theory_str::set_up_finite_model_test(expr * lhs, expr * rhs) { context & ctx = get_context(); ast_manager & m = get_manager(); - TRACE("t_str", tout << "activating finite model testing for overlapping concats " + TRACE("str", tout << "activating finite model testing for overlapping concats " << mk_pp(lhs, m) << " and " << mk_pp(rhs, m) << std::endl;); std::map concatMap; std::map unrollMap; std::map varMap; classify_ast_by_type(lhs, varMap, concatMap, unrollMap); classify_ast_by_type(rhs, varMap, concatMap, unrollMap); - TRACE("t_str_detail", tout << "found vars:"; + TRACE("str", tout << "found vars:"; for (std::map::iterator it = varMap.begin(); it != varMap.end(); ++it) { tout << " " << mk_pp(it->first, m); } @@ -6743,20 +6743,20 @@ void theory_str::finite_model_test(expr * testvar, expr * str) { zstring s; if (!u.str.is_string(str, s)) return; if (s == "yes") { - TRACE("t_str", tout << "start finite model test for " << mk_pp(testvar, m) << std::endl;); + TRACE("str", tout << "start finite model test for " << mk_pp(testvar, m) << std::endl;); ptr_vector & vars = finite_model_test_varlists[testvar]; for (ptr_vector::iterator it = vars.begin(); it != vars.end(); ++it) { expr * v = *it; bool v_has_eqc = false; get_eqc_value(v, v_has_eqc); if (v_has_eqc) { - TRACE("t_str_detail", tout << "variable " << mk_pp(v,m) << " already equivalent to a string constant" << std::endl;); + TRACE("str", tout << "variable " << mk_pp(v,m) << " already equivalent to a string constant" << std::endl;); continue; } // check for any sort of existing length tester we might interfere with if (m_params.m_UseBinarySearch) { if (binary_search_len_tester_stack.contains(v) && !binary_search_len_tester_stack[v].empty()) { - TRACE("t_str_detail", tout << "already found existing length testers for " << mk_pp(v, m) << std::endl;); + TRACE("str", tout << "already found existing length testers for " << mk_pp(v, m) << std::endl;); continue; } else { // start binary search as normal @@ -6783,19 +6783,19 @@ void theory_str::finite_model_test(expr * testvar, expr * str) { } if (map_effectively_empty) { - TRACE("t_str_detail", tout << "no existing length testers for " << mk_pp(v, m) << std::endl;); + TRACE("str", tout << "no existing length testers for " << mk_pp(v, m) << std::endl;); rational v_len; rational v_lower_bound; rational v_upper_bound; expr_ref vLengthExpr(mk_strlen(v), m); if (get_len_value(v, v_len)) { - TRACE("t_str_detail", tout << "length = " << v_len.to_string() << std::endl;); + TRACE("str", tout << "length = " << v_len.to_string() << std::endl;); v_lower_bound = v_len; v_upper_bound = v_len; } else { bool lower_bound_exists = lower_bound(vLengthExpr, v_lower_bound); bool upper_bound_exists = upper_bound(vLengthExpr, v_upper_bound); - TRACE("t_str_detail", tout << "bounds = [" << (lower_bound_exists?v_lower_bound.to_string():"?") + TRACE("str", tout << "bounds = [" << (lower_bound_exists?v_lower_bound.to_string():"?") << ".." << (upper_bound_exists?v_upper_bound.to_string():"?") << "]" << std::endl;); // make sure the bounds are non-negative @@ -6849,7 +6849,7 @@ void theory_str::finite_model_test(expr * testvar, expr * str) { expr_ref implRhs(mk_and(andList), m); assert_implication(implLhs, implRhs); } else { - TRACE("t_str_detail", tout << "already found existing length testers for " << mk_pp(v, m) << std::endl;); + TRACE("str", tout << "already found existing length testers for " << mk_pp(v, m) << std::endl;); continue; } } @@ -6862,7 +6862,7 @@ void theory_str::more_len_tests(expr * lenTester, zstring lenTesterValue) { if (lenTester_fvar_map.contains(lenTester)) { expr * fVar = lenTester_fvar_map[lenTester]; expr * toAssert = gen_len_val_options_for_free_var(fVar, lenTester, lenTesterValue); - TRACE("t_str_detail", tout << "asserting more length tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); + TRACE("str", tout << "asserting more length tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); if (toAssert != NULL) { assert_axiom(toAssert); } @@ -6875,24 +6875,24 @@ void theory_str::more_value_tests(expr * valTester, zstring valTesterValue) { expr * fVar = valueTester_fvar_map[valTester]; if (m_params.m_UseBinarySearch) { if (!binary_search_len_tester_stack.contains(fVar) || binary_search_len_tester_stack[fVar].empty()) { - TRACE("t_str_binary_search", tout << "WARNING: no active length testers for " << mk_pp(fVar, m) << std::endl;); + TRACE("str", tout << "WARNING: no active length testers for " << mk_pp(fVar, m) << std::endl;); NOT_IMPLEMENTED_YET(); } expr * effectiveLenInd = binary_search_len_tester_stack[fVar].back(); bool hasEqcValue; expr * len_indicator_value = get_eqc_value(effectiveLenInd, hasEqcValue); if (!hasEqcValue) { - TRACE("t_str_binary_search", tout << "WARNING: length tester " << mk_pp(effectiveLenInd, m) << " at top of stack for " << mk_pp(fVar, m) << " has no EQC value" << std::endl;); + TRACE("str", tout << "WARNING: length tester " << mk_pp(effectiveLenInd, m) << " at top of stack for " << mk_pp(fVar, m) << " has no EQC value" << std::endl;); } else { // safety check zstring effectiveLenIndiStr; u.str.is_string(len_indicator_value, effectiveLenIndiStr); if (effectiveLenIndiStr == "more" || effectiveLenIndiStr == "less") { - TRACE("t_str_binary_search", tout << "ERROR: illegal state -- requesting 'more value tests' but a length tester is not yet concrete!" << std::endl;); + TRACE("str", tout << "ERROR: illegal state -- requesting 'more value tests' but a length tester is not yet concrete!" << std::endl;); UNREACHABLE(); } expr * valueAssert = gen_free_var_options(fVar, effectiveLenInd, effectiveLenIndiStr, valTester, valTesterValue); - TRACE("t_str_detail", tout << "asserting more value tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); + TRACE("str", tout << "asserting more value tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); if (valueAssert != NULL) { assert_axiom(valueAssert); } @@ -6917,7 +6917,7 @@ void theory_str::more_value_tests(expr * valTester, zstring valTesterValue) { } } expr * valueAssert = gen_free_var_options(fVar, effectiveLenInd, effectiveLenIndiStr, valTester, valTesterValue); - TRACE("t_str_detail", tout << "asserting more value tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); + TRACE("str", tout << "asserting more value tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); if (valueAssert != NULL) { assert_axiom(valueAssert); } @@ -6928,13 +6928,13 @@ bool theory_str::free_var_attempt(expr * nn1, expr * nn2) { ast_manager & m = get_manager(); zstring nn2_str; if (internal_lenTest_vars.contains(nn1) && u.str.is_string(nn2, nn2_str)) { - TRACE("t_str", tout << "acting on equivalence between length tester var " << mk_ismt2_pp(nn1, m) + TRACE("str", tout << "acting on equivalence between length tester var " << mk_ismt2_pp(nn1, m) << " and constant " << mk_ismt2_pp(nn2, m) << std::endl;); more_len_tests(nn1, nn2_str); return true; } else if (internal_valTest_vars.contains(nn1) && u.str.is_string(nn2, nn2_str)) { if (nn2_str == "more") { - TRACE("t_str", tout << "acting on equivalence between value var " << mk_ismt2_pp(nn1, m) + TRACE("str", tout << "acting on equivalence between value var " << mk_ismt2_pp(nn1, m) << " and constant " << mk_ismt2_pp(nn2, m) << std::endl;); more_value_tests(nn1, nn2_str); } @@ -6955,7 +6955,7 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { sort * str_sort = u.str.mk_string_sort(); if (lhs_sort != str_sort || rhs_sort != str_sort) { - TRACE("t_str_detail", tout << "skip equality: not String sort" << std::endl;); + TRACE("str", tout << "skip equality: not String sort" << std::endl;); return; } @@ -6990,18 +6990,18 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { expr * nn2_arg0 = to_app(rhs)->get_arg(0); expr * nn2_arg1 = to_app(rhs)->get_arg(1); if (nn1_arg0 == nn2_arg0 && in_same_eqc(nn1_arg1, nn2_arg1)) { - TRACE("t_str_detail", tout << "skip: lhs arg0 == rhs arg0" << std::endl;); + TRACE("str", tout << "skip: lhs arg0 == rhs arg0" << std::endl;); return; } if (nn1_arg1 == nn2_arg1 && in_same_eqc(nn1_arg0, nn2_arg0)) { - TRACE("t_str_detail", tout << "skip: lhs arg1 == rhs arg1" << std::endl;); + TRACE("str", tout << "skip: lhs arg1 == rhs arg1" << std::endl;); return; } } if (opt_DeferEQCConsistencyCheck) { - TRACE("t_str_detail", tout << "opt_DeferEQCConsistencyCheck is set; deferring new_eq_check call" << std::endl;); + TRACE("str", tout << "opt_DeferEQCConsistencyCheck is set; deferring new_eq_check call" << std::endl;); } else { // newEqCheck() -- check consistency wrt. existing equivalence classes if (!new_eq_check(lhs, rhs)) { @@ -7050,7 +7050,7 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { std::set eqc_const_rhs; group_terms_by_eqc(rhs, eqc_concat_rhs, eqc_var_rhs, eqc_const_rhs); - TRACE("t_str_detail", + TRACE("str", tout << "lhs eqc:" << std::endl; tout << "Concats:" << std::endl; for (std::set::iterator it = eqc_concat_lhs.begin(); it != eqc_concat_lhs.end(); ++it) { @@ -7112,10 +7112,10 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { for (itor2 = eqc_concat_rhs.begin(); itor2 != eqc_concat_rhs.end() && !found; ++itor2) { expr * concat_rhs = *itor2; if (will_result_in_overlap(concat_lhs, concat_rhs)) { - TRACE("t_str_detail", tout << "Concats " << mk_pp(concat_lhs, m) << " and " + TRACE("str", tout << "Concats " << mk_pp(concat_lhs, m) << " and " << mk_pp(concat_rhs, m) << " will result in overlap; skipping." << std::endl;); } else { - TRACE("t_str_detail", tout << "Concats " << mk_pp(concat_lhs, m) << " and " + TRACE("str", tout << "Concats " << mk_pp(concat_lhs, m) << " and " << mk_pp(concat_rhs, m) << " won't overlap. Simplifying here." << std::endl;); simplify_concat_equality(concat_lhs, concat_rhs); found = true; @@ -7124,7 +7124,7 @@ void theory_str::handle_equality(expr * lhs, expr * rhs) { } } if (!found) { - TRACE("t_str_detail", tout << "All pairs of concats expected to overlap, falling back." << std::endl;); + TRACE("str", tout << "All pairs of concats expected to overlap, falling back." << std::endl;); simplify_concat_equality(*(eqc_concat_lhs.begin()), *(eqc_concat_rhs.begin())); } } else { @@ -7197,13 +7197,13 @@ void theory_str::set_up_axioms(expr * ex) { sort * int_sort = m.mk_sort(m_arith_fid, INT_SORT); if (ex_sort == str_sort) { - TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << + TRACE("str", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << ": expr is of sort String" << std::endl;); // set up basic string axioms enode * n = ctx.get_enode(ex); SASSERT(n); m_basicstr_axiom_todo.push_back(n); - TRACE("t_str_axiom_bug", tout << "add " << mk_pp(ex, m) << " to m_basicstr_axiom_todo" << std::endl;); + TRACE("str", tout << "add " << mk_pp(ex, m) << " to m_basicstr_axiom_todo" << std::endl;); if (is_app(ex)) { @@ -7225,21 +7225,21 @@ void theory_str::set_up_axioms(expr * ex) { } else if (u.str.is_at(ap) || u.str.is_extract(ap) || u.str.is_replace(ap)) { m_library_aware_axiom_todo.push_back(n); } else if (u.str.is_itos(ap)) { - TRACE("t_str_detail", tout << "found string-integer conversion term: " << mk_pp(ex, get_manager()) << std::endl;); + TRACE("str", tout << "found string-integer conversion term: " << mk_pp(ex, get_manager()) << std::endl;); string_int_conversion_terms.push_back(ap); m_library_aware_axiom_todo.push_back(n); } else if (ap->get_num_args() == 0 && !u.str.is_string(ap)) { // if ex is a variable, add it to our list of variables - TRACE("t_str_detail", tout << "tracking variable " << mk_ismt2_pp(ap, get_manager()) << std::endl;); + TRACE("str", tout << "tracking variable " << mk_ismt2_pp(ap, get_manager()) << std::endl;); variable_set.insert(ex); ctx.mark_as_relevant(ex); // this might help?? theory_var v = mk_var(n); - TRACE("t_str_detail", tout << "variable " << mk_ismt2_pp(ap, get_manager()) << " is #" << v << std::endl;); + TRACE("str", tout << "variable " << mk_ismt2_pp(ap, get_manager()) << " is #" << v << std::endl;); } } } else if (ex_sort == bool_sort) { - TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << + TRACE("str", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << ": expr is of sort Bool" << std::endl;); // set up axioms for boolean terms @@ -7255,13 +7255,13 @@ void theory_str::set_up_axioms(expr * ex) { } } } else { - TRACE("t_str_detail", tout << "WARNING: Bool term " << mk_ismt2_pp(ex, get_manager()) << " not internalized. Delaying axiom setup to prevent a crash." << std::endl;); + TRACE("str", tout << "WARNING: Bool term " << mk_ismt2_pp(ex, get_manager()) << " not internalized. Delaying axiom setup to prevent a crash." << std::endl;); ENSURE(!search_started); // infinite loop prevention m_delayed_axiom_setup_terms.push_back(ex); return; } } else if (ex_sort == int_sort) { - TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << + TRACE("str", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << ": expr is of sort Int" << std::endl;); // set up axioms for integer terms enode * n = ensure_enode(ex); @@ -7273,13 +7273,13 @@ void theory_str::set_up_axioms(expr * ex) { if (u.str.is_index(ap) /* || is_Indexof2(ap) || is_LastIndexof(ap) */) { m_library_aware_axiom_todo.push_back(n); } else if (u.str.is_stoi(ap)) { - TRACE("t_str_detail", tout << "found string-integer conversion term: " << mk_pp(ex, get_manager()) << std::endl;); + TRACE("str", tout << "found string-integer conversion term: " << mk_pp(ex, get_manager()) << std::endl;); string_int_conversion_terms.push_back(ap); m_library_aware_axiom_todo.push_back(n); } } } else { - TRACE("t_str_detail", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << + TRACE("str", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << ": expr is of wrong sort, ignoring" << std::endl;); } @@ -7294,7 +7294,7 @@ void theory_str::set_up_axioms(expr * ex) { } void theory_str::add_theory_assumptions(expr_ref_vector & assumptions) { - TRACE("t_str", tout << "add overlap assumption for theory_str" << std::endl;); + TRACE("str", tout << "add overlap assumption for theory_str" << std::endl;); symbol strOverlap("!!TheoryStrOverlapAssumption!!"); seq_util m_sequtil(get_manager()); sort * s = get_manager().mk_bool_sort(); @@ -7315,7 +7315,7 @@ lbool theory_str::validate_unsat_core(expr_ref_vector & unsat_core) { e1 = get_context().get_enode(target_term); e2 = get_context().get_enode(core_term); if (e1 == e2) { - TRACE("t_str", tout << "overlap detected in unsat core, changing UNSAT to UNKNOWN" << std::endl;); + TRACE("str", tout << "overlap detected in unsat core, changing UNSAT to UNKNOWN" << std::endl;); assumptionFound = true; return l_undef; } @@ -7328,7 +7328,7 @@ void theory_str::init_search_eh() { ast_manager & m = get_manager(); context & ctx = get_context(); - TRACE("t_str_detail", + TRACE("str", tout << "dumping all asserted formulas:" << std::endl; unsigned nFormulas = ctx.get_num_asserted_formulas(); for (unsigned i = 0; i < nFormulas; ++i) { @@ -7360,7 +7360,7 @@ void theory_str::init_search_eh() { for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { expr * ex = *i; if (m.is_eq(ex)) { - TRACE("t_str_detail", tout << "processing assignment " << mk_ismt2_pp(ex, m) << + TRACE("str", tout << "processing assignment " << mk_ismt2_pp(ex, m) << ": expr is equality" << std::endl;); app * eq = (app*)ex; SASSERT(eq->get_num_args() == 2); @@ -7372,7 +7372,7 @@ void theory_str::init_search_eh() { std::pair eq_pair(e_lhs, e_rhs); m_str_eq_todo.push_back(eq_pair); } else { - TRACE("t_str_detail", tout << "processing assignment " << mk_ismt2_pp(ex, m) + TRACE("str", tout << "processing assignment " << mk_ismt2_pp(ex, m) << ": expr ignored" << std::endl;); } } @@ -7382,13 +7382,13 @@ void theory_str::init_search_eh() { // before the first call to new_eq_eh() propagate(); - TRACE("t_str", tout << "search started" << std::endl;); + TRACE("str", tout << "search started" << std::endl;); search_started = true; } void theory_str::new_eq_eh(theory_var x, theory_var y) { - //TRACE("t_str_detail", tout << "new eq: v#" << x << " = v#" << y << std::endl;); - TRACE("t_str", tout << "new eq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " = " << + //TRACE("str", tout << "new eq: v#" << x << " = v#" << y << std::endl;); + TRACE("str", tout << "new eq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " = " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); /* @@ -7403,18 +7403,18 @@ void theory_str::new_eq_eh(theory_var x, theory_var y) { } void theory_str::new_diseq_eh(theory_var x, theory_var y) { - //TRACE("t_str_detail", tout << "new diseq: v#" << x << " != v#" << y << std::endl;); - TRACE("t_str", tout << "new diseq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " != " << + //TRACE("str", tout << "new diseq: v#" << x << " != v#" << y << std::endl;); + TRACE("str", tout << "new diseq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " != " << mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); } void theory_str::relevant_eh(app * n) { - TRACE("t_str", tout << "relevant: " << mk_ismt2_pp(n, get_manager()) << std::endl;); + TRACE("str", tout << "relevant: " << mk_ismt2_pp(n, get_manager()) << std::endl;); } void theory_str::assign_eh(bool_var v, bool is_true) { context & ctx = get_context(); - TRACE("t_str", tout << "assert: v" << v << " #" << ctx.bool_var2expr(v)->get_id() << " is_true: " << is_true << std::endl;); + TRACE("str", tout << "assert: v" << v << " #" << ctx.bool_var2expr(v)->get_id() << " is_true: " << is_true << std::endl;); } void theory_str::push_scope_eh() { @@ -7422,7 +7422,7 @@ void theory_str::push_scope_eh() { m_trail_stack.push_scope(); sLevel += 1; - TRACE("t_str", tout << "push to " << sLevel << std::endl;); + TRACE("str", tout << "push to " << sLevel << std::endl;); TRACE_CODE(if (is_trace_enabled("t_str_dump_assign_on_scope_change")) { dump_assignments(); }); } @@ -7446,7 +7446,7 @@ void theory_str::recursive_check_variable_scope(expr * ex) { // assume var if (variable_set.find(ex) == variable_set.end() && internal_variable_set.find(ex) == internal_variable_set.end()) { - TRACE("t_str_detail", tout << "WARNING: possible reference to out-of-scope variable " << mk_pp(ex, m) << std::endl;); + TRACE("str", tout << "WARNING: possible reference to out-of-scope variable " << mk_pp(ex, m) << std::endl;); } } } else { @@ -7466,7 +7466,7 @@ void theory_str::check_variable_scope() { return; } - TRACE("t_str_detail", tout << "checking scopes of variables in the current assignment" << std::endl;); + TRACE("str", tout << "checking scopes of variables in the current assignment" << std::endl;); context & ctx = get_context(); ast_manager & m = get_manager(); @@ -7481,7 +7481,7 @@ void theory_str::check_variable_scope() { void theory_str::pop_scope_eh(unsigned num_scopes) { sLevel -= num_scopes; - TRACE("t_str", tout << "pop " << num_scopes << " to " << sLevel << std::endl;); + TRACE("str", tout << "pop " << num_scopes << " to " << sLevel << std::endl;); context & ctx = get_context(); ast_manager & m = get_manager(); @@ -7495,7 +7495,7 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { expr * e = varItor->m_key; std::stack & val = cut_var_map[varItor->m_key]; while ((val.size() > 0) && (val.top()->level != 0) && (val.top()->level >= sLevel)) { - TRACE("t_str_cut_var_map", tout << "remove cut info for " << mk_pp(e, m) << std::endl; print_cut_var(e, tout);); + TRACE("str", tout << "remove cut info for " << mk_pp(e, m) << std::endl; print_cut_var(e, tout);); T_cut * aCut = val.top(); val.pop(); // dealloc(aCut); @@ -7518,7 +7518,7 @@ void theory_str::pop_scope_eh(unsigned num_scopes) { for (ptr_vector::iterator it = m_basicstr_axiom_todo.begin(); it != m_basicstr_axiom_todo.end(); ++it) { enode * e = *it; app * a = e->get_owner(); - TRACE("t_str_axiom_bug", tout << "consider deleting " << mk_pp(a, get_manager()) + TRACE("str", tout << "consider deleting " << mk_pp(a, get_manager()) << ", enode scope level is " << e->get_iscope_lvl() << std::endl;); if (e->get_iscope_lvl() <= (unsigned)sLevel) { @@ -7559,7 +7559,7 @@ void theory_str::classify_ast_by_type(expr * node, std::map & varMap && internal_valTest_vars.find(node) == internal_valTest_vars.end() && internal_unrollTest_vars.find(node) == internal_unrollTest_vars.end()) { if (varMap[node] != 1) { - TRACE("t_str_detail", tout << "new variable: " << mk_pp(node, get_manager()) << std::endl;); + TRACE("str", tout << "new variable: " << mk_pp(node, get_manager()) << std::endl;); } varMap[node] = 1; } @@ -7622,7 +7622,7 @@ void theory_str::classify_ast_by_type_in_positive_context(std::map & // so we bypass a huge amount of work by doing the following... if (m.is_eq(argAst)) { - TRACE("t_str_detail", tout + TRACE("str", tout << "eq ast " << mk_pp(argAst, m) << " is between args of sort " << m.get_sort(to_app(argAst)->get_arg(0))->get_name() << std::endl;); @@ -7846,7 +7846,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map::iterator it = variable_set.begin(); it != variable_set.end(); ++it) { expr* var = *it; if (internal_variable_set.find(var) == internal_variable_set.end()) { - TRACE("t_str_detail", tout << "new variable: " << mk_pp(var, m) << std::endl;); + TRACE("str", tout << "new variable: " << mk_pp(var, m) << std::endl;); strVarMap[*it] = 1; } } @@ -8030,7 +8030,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map & strVarMap, std::map::iterator itor2 = inVarMap.begin(); itor2 != inVarMap.end(); itor2++) { expr * varInFunc = get_alias_index_ast(aliasIndexMap, itor2->first); - TRACE("t_str_detail", tout << "var in unroll = " << + TRACE("str", tout << "var in unroll = " << mk_ismt2_pp(itor2->first, m) << std::endl << "dealiased var = " << mk_ismt2_pp(varInFunc, m) << std::endl;); @@ -8255,7 +8255,7 @@ int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map >::iterator itor = depMap.begin(); itor != depMap.end(); itor++) { tout << mk_pp(itor->first, m); @@ -8422,7 +8422,7 @@ bool theory_str::finalcheck_str2int(app * a) { rational Ival; bool Ival_exists = get_value(a, Ival); if (Ival_exists) { - TRACE("t_str_detail", tout << "integer theory assigns " << mk_pp(a, m) << " = " << Ival.to_string() << std::endl;); + TRACE("str", tout << "integer theory assigns " << mk_pp(a, m) << " = " << Ival.to_string() << std::endl;); // if that value is not -1, we can assert (str.to-int S) = Ival --> S = "Ival" if (!Ival.is_minus_one()) { zstring Ival_str(Ival.to_string().c_str()); @@ -8437,7 +8437,7 @@ bool theory_str::finalcheck_str2int(app * a) { } } } else { - TRACE("t_str_detail", tout << "integer theory has no assignment for " << mk_pp(a, m) << std::endl;); + TRACE("str", tout << "integer theory has no assignment for " << mk_pp(a, m) << std::endl;); NOT_IMPLEMENTED_YET(); } @@ -8457,7 +8457,7 @@ bool theory_str::finalcheck_int2str(app * a) { if (Sval_expr_exists) { zstring Sval; u.str.is_string(Sval_expr, Sval); - TRACE("t_str_detail", tout << "string theory assigns \"" << mk_pp(a, m) << " = " << Sval << "\n";); + TRACE("str", tout << "string theory assigns \"" << mk_pp(a, m) << " = " << Sval << "\n";); // empty string --> integer value < 0 if (Sval.empty()) { // ignore this. we should already assert the axiom for what happens when the string is "" @@ -8474,7 +8474,7 @@ bool theory_str::finalcheck_int2str(app * a) { convertedRepresentation = (ten * convertedRepresentation) + rational(val); } else { // not a digit, invalid - TRACE("t_str_rw", tout << "str.to-int argument contains non-digit character '" << digit << "'" << std::endl;); + TRACE("str", tout << "str.to-int argument contains non-digit character '" << digit << "'" << std::endl;); conversionOK = false; break; } @@ -8497,7 +8497,7 @@ bool theory_str::finalcheck_int2str(app * a) { } } } else { - TRACE("t_str_detail", tout << "string theory has no assignment for " << mk_pp(a, m) << std::endl;); + TRACE("str", tout << "string theory has no assignment for " << mk_pp(a, m) << std::endl;); NOT_IMPLEMENTED_YET(); } return axiomAdd; @@ -8535,7 +8535,7 @@ bool theory_str::propagate_length_within_eqc(expr * var) { ast_manager & m = get_manager(); context & ctx = get_context(); - TRACE("t_str_length", tout << "propagate_length_within_eqc: " << mk_ismt2_pp(var, m) << std::endl ;); + TRACE("str", tout << "propagate_length_within_eqc: " << mk_ismt2_pp(var, m) << std::endl ;); enode * n_eq_enode = ctx.get_enode(var); rational varLen; @@ -8565,7 +8565,7 @@ bool theory_str::propagate_length_within_eqc(expr * var) { expr_ref varLen(mk_strlen(var), m); expr_ref axr(ctx.mk_eq_atom(varLen, mk_int(varLen)), m); assert_implication(axl, axr); - TRACE("t_str_length", tout << mk_ismt2_pp(axl, m) << std::endl << " ---> " << std::endl << mk_ismt2_pp(axr, m);); + TRACE("str", tout << mk_ismt2_pp(axl, m) << std::endl << " ---> " << std::endl << mk_ismt2_pp(axr, m);); res = true; } } @@ -8598,7 +8598,7 @@ bool theory_str::propagate_length(std::set & varSet, std::set & co // the length fo concat is unresolved yet if (get_len_value(concat, lenValue)) { // but all leaf nodes have length information - TRACE("t_str_length", tout << "* length pop-up: " << mk_ismt2_pp(concat, m) << "| = " << lenValue << std::endl;); + TRACE("str", tout << "* length pop-up: " << mk_ismt2_pp(concat, m) << "| = " << lenValue << std::endl;); std::set leafNodes; get_unique_non_concat_nodes(concat, leafNodes); expr_ref_vector l_items(m); @@ -8619,7 +8619,7 @@ bool theory_str::propagate_length(std::set & varSet, std::set & co expr_ref lenValueExpr (mk_int(lenValue), m); expr_ref axr(ctx.mk_eq_atom(concatlenExpr, lenValueExpr), m); assert_implication(axl, axr); - TRACE("t_str_length", tout << mk_ismt2_pp(axl, m) << std::endl << " ---> " << std::endl << mk_ismt2_pp(axr, m)<< std::endl;); + TRACE("str", tout << mk_ismt2_pp(axl, m) << std::endl << " ---> " << std::endl << mk_ismt2_pp(axr, m)<< std::endl;); axiomAdded = true; } } @@ -8668,12 +8668,12 @@ final_check_status theory_str::final_check_eh() { finalCheckProgressIndicator = false; } - TRACE("t_str", tout << "final check" << std::endl;); + TRACE("str", tout << "final check" << std::endl;); TRACE_CODE(if (is_trace_enabled("t_str_dump_assign")) { dump_assignments(); }); check_variable_scope(); if (opt_DeferEQCConsistencyCheck) { - TRACE("t_str_detail", tout << "performing deferred EQC consistency check" << std::endl;); + TRACE("str", tout << "performing deferred EQC consistency check" << std::endl;); std::set eqc_roots; for (ptr_vector::const_iterator it = ctx.begin_enodes(); it != ctx.end_enodes(); ++it) { enode * e = *it; @@ -8687,16 +8687,16 @@ final_check_status theory_str::final_check_eh() { enode * e = *it; app * a = e->get_owner(); if (!(m.get_sort(a) == u.str.mk_string_sort())) { - TRACE("t_str_detail", tout << "EQC root " << mk_pp(a, m) << " not a string term; skipping" << std::endl;); + TRACE("str", tout << "EQC root " << mk_pp(a, m) << " not a string term; skipping" << std::endl;); } else { - TRACE("t_str_detail", tout << "EQC root " << mk_pp(a, m) << " is a string term. Checking this EQC" << std::endl;); + TRACE("str", tout << "EQC root " << mk_pp(a, m) << " is a string term. Checking this EQC" << std::endl;); // first call check_concat_len_in_eqc() on each member of the eqc enode * e_it = e; enode * e_root = e_it; do { bool status = check_concat_len_in_eqc(e_it->get_owner()); if (!status) { - TRACE("t_str_detail", tout << "concat-len check asserted an axiom on " << mk_pp(e_it->get_owner(), m) << std::endl;); + TRACE("str", tout << "concat-len check asserted an axiom on " << mk_pp(e_it->get_owner(), m) << std::endl;); found_inconsistency = true; } e_it = e_it->get_next(); @@ -8706,10 +8706,10 @@ final_check_status theory_str::final_check_eh() { enode * e1 = e; enode * e2 = e1->get_next(); if (e1 != e2) { - TRACE("t_str_detail", tout << "deferred new_eq_check() over EQC of " << mk_pp(e1->get_owner(), m) << " and " << mk_pp(e2->get_owner(), m) << std::endl;); + TRACE("str", tout << "deferred new_eq_check() over EQC of " << mk_pp(e1->get_owner(), m) << " and " << mk_pp(e2->get_owner(), m) << std::endl;); bool result = new_eq_check(e1->get_owner(), e2->get_owner()); if (!result) { - TRACE("t_str_detail", tout << "new_eq_check found inconsistencies" << std::endl;); + TRACE("str", tout << "new_eq_check found inconsistencies" << std::endl;); found_inconsistency = true; } } @@ -8717,10 +8717,10 @@ final_check_status theory_str::final_check_eh() { } if (found_inconsistency) { - TRACE("t_str", tout << "Found inconsistency in final check! Returning to search." << std::endl;); + TRACE("str", tout << "Found inconsistency in final check! Returning to search." << std::endl;); return FC_CONTINUE; } else { - TRACE("t_str", tout << "Deferred consistency check passed. Continuing in final check." << std::endl;); + TRACE("str", tout << "Deferred consistency check passed. Continuing in final check." << std::endl;); } } @@ -8753,7 +8753,7 @@ final_check_status theory_str::final_check_eh() { expr * concat_rhs_str = get_eqc_value(concat_rhs, concat_rhs_haseqc); expr * var_str = get_eqc_value(var, var_haseqc); if (concat_lhs_haseqc && concat_rhs_haseqc && !var_haseqc) { - TRACE("t_str_detail", tout << "backpropagate into " << mk_pp(var, m) << " = " << mk_pp(concat, m) << std::endl + TRACE("str", tout << "backpropagate into " << mk_pp(var, m) << " = " << mk_pp(concat, m) << std::endl << "LHS ~= " << mk_pp(concat_lhs_str, m) << " RHS ~= " << mk_pp(concat_rhs_str, m) << std::endl;); zstring lhsString, rhsString; u.str.is_string(concat_lhs_str, lhsString); @@ -8770,7 +8770,7 @@ final_check_status theory_str::final_check_eh() { } if (backpropagation_occurred) { - TRACE("t_str", tout << "Resuming search due to axioms added by backpropagation." << std::endl;); + TRACE("str", tout << "Resuming search due to axioms added by backpropagation." << std::endl;); return FC_CONTINUE; } @@ -8782,7 +8782,7 @@ final_check_status theory_str::final_check_eh() { bool length_propagation_occurred = propagate_length(varSet, concatSet, exprLenMap); if (length_propagation_occurred) { - TRACE("t_str", tout << "Resuming search due to axioms added by length propagation." << std::endl;); + TRACE("str", tout << "Resuming search due to axioms added by length propagation." << std::endl;); return FC_CONTINUE; } } @@ -8801,19 +8801,19 @@ final_check_status theory_str::final_check_eh() { if (internal_variable_set.find(itor->first) != internal_variable_set.end() || regex_variable_set.find(itor->first) != regex_variable_set.end()) { // this can be ignored, I think - TRACE("t_str_detail", tout << "free internal variable " << mk_pp(itor->first, m) << " ignored" << std::endl;); + TRACE("str", tout << "free internal variable " << mk_pp(itor->first, m) << " ignored" << std::endl;); continue; } bool hasEqcValue = false; expr * eqcString = get_eqc_value(itor->first, hasEqcValue); if (!hasEqcValue) { - TRACE("t_str_detail", tout << "found free variable " << mk_pp(itor->first, m) << std::endl;); + TRACE("str", tout << "found free variable " << mk_pp(itor->first, m) << std::endl;); needToAssignFreeVars = true; free_variables.insert(itor->first); // break; } else { // debug - TRACE("t_str_detail", tout << "variable " << mk_pp(itor->first, m) << " = " << mk_pp(eqcString, m) << std::endl;); + TRACE("str", tout << "variable " << mk_pp(itor->first, m) << " = " << mk_pp(eqcString, m) << std::endl;); } } } @@ -8839,15 +8839,15 @@ final_check_status theory_str::final_check_eh() { } } if (addedStrIntAxioms) { - TRACE("t_str", tout << "Resuming search due to addition of string-integer conversion axioms." << std::endl;); + TRACE("str", tout << "Resuming search due to addition of string-integer conversion axioms." << std::endl;); return FC_CONTINUE; } if (unused_internal_variables.empty()) { - TRACE("t_str", tout << "All variables are assigned. Done!" << std::endl;); + TRACE("str", tout << "All variables are assigned. Done!" << std::endl;); return FC_DONE; } else { - TRACE("t_str", tout << "Assigning decoy values to free internal variables." << std::endl;); + TRACE("str", tout << "Assigning decoy values to free internal variables." << std::endl;); for (std::set::iterator it = unused_internal_variables.begin(); it != unused_internal_variables.end(); ++it) { expr * var = *it; expr_ref assignment(m.mk_eq(var, mk_string("**unused**")), m); @@ -8857,7 +8857,7 @@ final_check_status theory_str::final_check_eh() { } } - CTRACE("t_str", needToAssignFreeVars, + CTRACE("str", needToAssignFreeVars, tout << "Need to assign values to the following free variables:" << std::endl; for (std::set::iterator itx = free_variables.begin(); itx != free_variables.end(); ++itx) { tout << mk_ismt2_pp(*itx, m) << std::endl; @@ -8890,7 +8890,7 @@ final_check_status theory_str::final_check_eh() { for (std::map >::iterator fvIt3 = fv_unrolls_map.begin(); fvIt3 != fv_unrolls_map.end(); fvIt3++) { expr * var = fvIt3->first; - TRACE("t_str_detail", tout << "erase free variable " << mk_pp(var, m) << " from freeVar_map, it is bounded by an Unroll" << std::endl;); + TRACE("str", tout << "erase free variable " << mk_pp(var, m) << " from freeVar_map, it is bounded by an Unroll" << std::endl;); freeVar_map.erase(var); } @@ -8954,7 +8954,7 @@ final_check_status theory_str::final_check_eh() { } } for (std::set::iterator vItor = fvUnrollSet.begin(); vItor != fvUnrollSet.end(); vItor++) { - TRACE("t_str_detail", tout << "remove " << mk_pp(*vItor, m) << " from freeVar_map" << std::endl;); + TRACE("str", tout << "remove " << mk_pp(*vItor, m) << " from freeVar_map" << std::endl;); freeVar_map.erase(*vItor); } @@ -8964,7 +8964,7 @@ final_check_status theory_str::final_check_eh() { constValue = NULL; { - TRACE("t_str_detail", tout << "free var map (#" << freeVar_map.size() << "):" << std::endl; + TRACE("str", tout << "free var map (#" << freeVar_map.size() << "):" << std::endl; for (std::map::iterator freeVarItor1 = freeVar_map.begin(); freeVarItor1 != freeVar_map.end(); freeVarItor1++) { expr * freeVar = freeVarItor1->first; rational lenValue; @@ -9010,7 +9010,7 @@ final_check_status theory_str::final_check_eh() { // experimental free variable assignment - end // now deal with removed free variables that are bounded by an unroll - TRACE("t_str", tout << "fv_unrolls_map (#" << fv_unrolls_map.size() << "):" << std::endl;); + TRACE("str", tout << "fv_unrolls_map (#" << fv_unrolls_map.size() << "):" << std::endl;); for (std::map >::iterator fvIt1 = fv_unrolls_map.begin(); fvIt1 != fv_unrolls_map.end(); fvIt1++) { expr * var = fvIt1->first; @@ -9027,7 +9027,7 @@ final_check_status theory_str::final_check_eh() { } if (opt_VerifyFinalCheckProgress && !finalCheckProgressIndicator) { - TRACE("t_str", tout << "BUG: no progress in final check, giving up!!" << std::endl;); + TRACE("str", tout << "BUG: no progress in final check, giving up!!" << std::endl;); m.raise_exception("no progress in theory_str final check"); } @@ -9049,7 +9049,7 @@ inline std::string longlong_to_string(long long i) { void theory_str::print_value_tester_list(svector > & testerList) { ast_manager & m = get_manager(); - TRACE("t_str_detail", + TRACE("str", int ss = testerList.size(); tout << "valueTesterList = {"; for (int i = 0; i < ss; ++i) { @@ -9084,7 +9084,7 @@ zstring theory_str::gen_val_string(int len, int_vector & encoding) { bool theory_str::get_next_val_encode(int_vector & base, int_vector & next) { SASSERT(charSetSize > 0); - TRACE("t_str_value_test_bug", tout << "base vector: [ "; + TRACE("str", tout << "base vector: [ "; for (unsigned i = 0; i < base.size(); ++i) { tout << base[i] << " "; } @@ -9140,7 +9140,7 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * svector options; int_vector base; - TRACE("t_str_detail", tout + TRACE("str", tout << "freeVar = " << mk_ismt2_pp(freeVar, m) << std::endl << "len_indicator = " << mk_ismt2_pp(len_indicator, m) << std::endl << "val_indicator = " << mk_ismt2_pp(val_indicator, m) << std::endl @@ -9156,7 +9156,7 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * coverAll = false; } else { expr * lastestValIndi = fvar_valueTester_map[freeVar][len][tries - 1].second; - TRACE("t_str_detail", tout << "last value tester = " << mk_ismt2_pp(lastestValIndi, m) << std::endl;); + TRACE("str", tout << "last value tester = " << mk_ismt2_pp(lastestValIndi, m) << std::endl;); coverAll = get_next_val_encode(val_range_map[lastestValIndi], base); } @@ -9171,7 +9171,7 @@ expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * } val_range_map[val_indicator] = options[options.size() - 1]; - TRACE("t_str_detail", + TRACE("str", tout << "value tester encoding " << "{" << std::endl; int_vector vec = val_range_map[val_indicator]; @@ -9266,7 +9266,7 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, int len = atoi(len_valueStr.encode().c_str()); // check whether any value tester is actually in scope - TRACE("t_str_detail", tout << "checking scope of previous value testers" << std::endl;); + TRACE("str", tout << "checking scope of previous value testers" << std::endl;); bool map_effectively_empty = true; if (fvar_valueTester_map[freeVar].find(len) != fvar_valueTester_map[freeVar].end()) { // there's *something* in the map, but check its scope @@ -9275,9 +9275,9 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, std::pair entry = *it; expr * aTester = entry.second; if (internal_variable_set.find(aTester) == internal_variable_set.end()) { - TRACE("t_str_detail", tout << mk_pp(aTester, m) << " out of scope" << std::endl;); + TRACE("str", tout << mk_pp(aTester, m) << " out of scope" << std::endl;); } else { - TRACE("t_str_detail", tout << mk_pp(aTester, m) << " in scope" << std::endl;); + TRACE("str", tout << mk_pp(aTester, m) << " in scope" << std::endl;); map_effectively_empty = false; break; } @@ -9285,7 +9285,7 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, } if (map_effectively_empty) { - TRACE("t_str_detail", tout << "no previous value testers, or none of them were in scope" << std::endl;); + TRACE("str", tout << "no previous value testers, or none of them were in scope" << std::endl;); int tries = 0; expr * val_indicator = mk_internal_valTest_var(freeVar, len, tries); valueTester_fvar_map[val_indicator] = freeVar; @@ -9293,7 +9293,7 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, print_value_tester_list(fvar_valueTester_map[freeVar][len]); return gen_val_options(freeVar, len_indicator, val_indicator, len_valueStr, tries); } else { - TRACE("t_str_detail", tout << "checking previous value testers" << std::endl;); + TRACE("str", tout << "checking previous value testers" << std::endl;); print_value_tester_list(fvar_valueTester_map[freeVar][len]); // go through all previous value testers @@ -9305,7 +9305,7 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, // it's probably worth checking scope here, actually if (internal_variable_set.find(aTester) == internal_variable_set.end()) { - TRACE("t_str_detail", tout << "value tester " << mk_pp(aTester, m) << " out of scope, skipping" << std::endl;); + TRACE("str", tout << "value tester " << mk_pp(aTester, m) << " out of scope, skipping" << std::endl;); continue; } @@ -9317,17 +9317,17 @@ expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, // Z3_ast anEqc = get_eqc_value(t, aTester, anEqcHasValue); expr * aTester_eqc_value = get_eqc_value(aTester, anEqcHasValue); if (!anEqcHasValue) { - TRACE("t_str_detail", tout << "value tester " << mk_ismt2_pp(aTester, m) + TRACE("str", tout << "value tester " << mk_ismt2_pp(aTester, m) << " doesn't have an equivalence class value." << std::endl;); refresh_theory_var(aTester); expr * makeupAssert = gen_val_options(freeVar, len_indicator, aTester, len_valueStr, i); - TRACE("t_str_detail", tout << "var: " << mk_ismt2_pp(freeVar, m) << std::endl + TRACE("str", tout << "var: " << mk_ismt2_pp(freeVar, m) << std::endl << mk_ismt2_pp(makeupAssert, m) << std::endl;); assert_axiom(makeupAssert); } else { - TRACE("t_str_detail", tout << "value tester " << mk_ismt2_pp(aTester, m) + TRACE("str", tout << "value tester " << mk_ismt2_pp(aTester, m) << " == " << mk_ismt2_pp(aTester_eqc_value, m) << std::endl;); } } @@ -9355,7 +9355,7 @@ void theory_str::reduce_virtual_regex_in(expr * var, expr * regex, expr_ref_vect context & ctx = get_context(); ast_manager & mgr = get_manager(); - TRACE("t_str_detail", tout << "reduce regex " << mk_pp(regex, mgr) << " with respect to variable " << mk_pp(var, mgr) << std::endl;); + TRACE("str", tout << "reduce regex " << mk_pp(regex, mgr) << " with respect to variable " << mk_pp(var, mgr) << std::endl;); app * regexFuncDecl = to_app(regex); if (u.re.is_to_re(regexFuncDecl)) { @@ -9443,7 +9443,7 @@ void theory_str::gen_assign_unroll_reg(std::set & unrolls) { expr_ref_vector items(mgr); for (std::set::iterator itor = unrolls.begin(); itor != unrolls.end(); itor++) { expr * unrFunc = *itor; - TRACE("t_str_detail", tout << "generating assignment for unroll " << mk_pp(unrFunc, mgr) << std::endl;); + TRACE("str", tout << "generating assignment for unroll " << mk_pp(unrFunc, mgr) << std::endl;); expr * regexInUnr = to_app(unrFunc)->get_arg(0); expr * cntInUnr = to_app(unrFunc)->get_arg(1); @@ -9453,7 +9453,7 @@ void theory_str::gen_assign_unroll_reg(std::set & unrolls) { bool low_exists = lower_bound(cntInUnr, low); bool high_exists = upper_bound(cntInUnr, high); - TRACE("t_str_detail", + TRACE("str", tout << "unroll " << mk_pp(unrFunc, mgr) << std::endl; rational unrLenValue; bool unrLenValue_exists = get_len_value(unrFunc, unrLenValue); @@ -9599,7 +9599,7 @@ expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & expr_ref moreAst(mk_string("more"), mgr); for (std::set::iterator itor = unrolls.begin(); itor != unrolls.end(); itor++) { expr_ref item(ctx.mk_eq_atom(var, *itor), mgr); - TRACE("t_str_detail", tout << "considering unroll " << mk_pp(item, mgr) << std::endl;); + TRACE("str", tout << "considering unroll " << mk_pp(item, mgr) << std::endl;); litems.push_back(item); } @@ -9611,7 +9611,7 @@ expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & it != unroll_tries_map[var][unrolls].end(); ++it) { expr * tester = *it; bool inScope = (internal_unrollTest_vars.find(tester) != internal_unrollTest_vars.end()); - TRACE("t_str_detail", tout << "unroll test var " << mk_pp(tester, mgr) + TRACE("str", tout << "unroll test var " << mk_pp(tester, mgr) << (inScope ? " in scope" : " out of scope") << std::endl;); if (!inScope) { @@ -9644,13 +9644,13 @@ expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & expr_ref rImp(gen_unroll_assign(var, lcmStr, tester, l, h), mgr); SASSERT(lImp); - TRACE("t_str_detail", tout << "lImp = " << mk_pp(lImp, mgr) << std::endl;); + TRACE("str", tout << "lImp = " << mk_pp(lImp, mgr) << std::endl;); SASSERT(rImp); - TRACE("t_str_detail", tout << "rImp = " << mk_pp(rImp, mgr) << std::endl;); + TRACE("str", tout << "rImp = " << mk_pp(rImp, mgr) << std::endl;); expr_ref toAssert(mgr.mk_or(mgr.mk_not(lImp), rImp), mgr); SASSERT(toAssert); - TRACE("t_str_detail", tout << "Making up assignments for variable which is equal to unbounded Unroll" << std::endl;); + TRACE("str", tout << "Making up assignments for variable which is equal to unbounded Unroll" << std::endl;); m_trail.push_back(toAssert); return toAssert; @@ -9662,7 +9662,7 @@ expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & } else { zstring testerStr; u.str.is_string(testerVal, testerStr); - TRACE("t_str_detail", tout << "Tester [" << mk_pp(tester, mgr) << "] = " << testerStr << "\n";); + TRACE("str", tout << "Tester [" << mk_pp(tester, mgr) << "] = " << testerStr << "\n";); if (testerStr == "more") { litems.push_back(ctx.mk_eq_atom(tester, moreAst)); } @@ -9678,7 +9678,7 @@ expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & SASSERT(rImp); expr_ref toAssert(mgr.mk_or(mgr.mk_not(lImp), rImp), mgr); SASSERT(toAssert); - TRACE("t_str_detail", tout << "Generating assignment for variable which is equal to unbounded Unroll" << std::endl;); + TRACE("str", tout << "Generating assignment for variable which is equal to unbounded Unroll" << std::endl;); m_trail.push_back(toAssert); return toAssert; } @@ -9687,11 +9687,11 @@ expr * theory_str::gen_unroll_assign(expr * var, zstring lcmStr, expr * testerVa context & ctx = get_context(); ast_manager & mgr = get_manager(); - TRACE("t_str_detail", tout << "entry: var = " << mk_pp(var, mgr) << ", lcmStr = " << lcmStr + TRACE("str", tout << "entry: var = " << mk_pp(var, mgr) << ", lcmStr = " << lcmStr << ", l = " << l << ", h = " << h << "\n";); if (m_params.m_AggressiveUnrollTesting) { - TRACE("t_str_detail", tout << "note: aggressive unroll testing is active" << std::endl;); + TRACE("str", tout << "note: aggressive unroll testing is active" << std::endl;); } expr_ref_vector orItems(mgr); @@ -9700,7 +9700,7 @@ expr * theory_str::gen_unroll_assign(expr * var, zstring lcmStr, expr * testerVa for (int i = l; i < h; i++) { zstring iStr = int_to_string(i); expr_ref testerEqAst(ctx.mk_eq_atom(testerVar, mk_string(iStr)), mgr); - TRACE("t_str_detail", tout << "testerEqAst = " << mk_pp(testerEqAst, mgr) << std::endl;); + TRACE("str", tout << "testerEqAst = " << mk_pp(testerEqAst, mgr) << std::endl;); if (m_params.m_AggressiveUnrollTesting) { literal l = mk_eq(testerVar, mk_string(iStr), false); ctx.mark_as_relevant(l); @@ -9711,15 +9711,15 @@ expr * theory_str::gen_unroll_assign(expr * var, zstring lcmStr, expr * testerVa zstring unrollStrInstance = get_unrolled_string(lcmStr, i); expr_ref x1(ctx.mk_eq_atom(testerEqAst, ctx.mk_eq_atom(var, mk_string(unrollStrInstance))), mgr); - TRACE("t_str_detail", tout << "x1 = " << mk_pp(x1, mgr) << std::endl;); + TRACE("str", tout << "x1 = " << mk_pp(x1, mgr) << std::endl;); andItems.push_back(x1); expr_ref x2(ctx.mk_eq_atom(testerEqAst, ctx.mk_eq_atom(mk_strlen(var), mk_int(i * lcmStr.length()))), mgr); - TRACE("t_str_detail", tout << "x2 = " << mk_pp(x2, mgr) << std::endl;); + TRACE("str", tout << "x2 = " << mk_pp(x2, mgr) << std::endl;); andItems.push_back(x2); } expr_ref testerEqMore(ctx.mk_eq_atom(testerVar, mk_string("more")), mgr); - TRACE("t_str_detail", tout << "testerEqMore = " << mk_pp(testerEqMore, mgr) << std::endl;); + TRACE("str", tout << "testerEqMore = " << mk_pp(testerEqMore, mgr) << std::endl;); if (m_params.m_AggressiveUnrollTesting) { literal l = mk_eq(testerVar, mk_string("more"), false); ctx.mark_as_relevant(l); @@ -9732,15 +9732,15 @@ expr * theory_str::gen_unroll_assign(expr * var, zstring lcmStr, expr * testerVa //Z3_mk_ge(mk_length(t, var), mk_int(ctx, nextLowerLenBound)) m_autil.mk_ge(m_autil.mk_add(mk_strlen(var), mk_int(-1 * nextLowerLenBound)), mk_int(0)) ), mgr); - TRACE("t_str_detail", tout << "more2 = " << mk_pp(more2, mgr) << std::endl;); + TRACE("str", tout << "more2 = " << mk_pp(more2, mgr) << std::endl;); andItems.push_back(more2); expr_ref finalOR(mgr.mk_or(orItems.size(), orItems.c_ptr()), mgr); - TRACE("t_str_detail", tout << "finalOR = " << mk_pp(finalOR, mgr) << std::endl;); + TRACE("str", tout << "finalOR = " << mk_pp(finalOR, mgr) << std::endl;); andItems.push_back(mk_or(orItems)); expr_ref finalAND(mgr.mk_and(andItems.size(), andItems.c_ptr()), mgr); - TRACE("t_str_detail", tout << "finalAND = " << mk_pp(finalAND, mgr) << std::endl;); + TRACE("str", tout << "finalAND = " << mk_pp(finalAND, mgr) << std::endl;); // doing the following avoids a segmentation fault m_trail.push_back(finalAND); @@ -9761,7 +9761,7 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr int l = (tries - 1) * distance; int h = tries * distance; - TRACE("t_str_detail", + TRACE("str", tout << "building andList and orList" << std::endl; if (m_params.m_AggressiveLengthTesting) { tout << "note: aggressive length testing is active" << std::endl; @@ -9848,11 +9848,11 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr and_items.push_back(andList.get(i)); } - TRACE("t_str_detail", tout << "check: " << mk_pp(mk_and(and_items), m) << std::endl;); + TRACE("str", tout << "check: " << mk_pp(mk_and(and_items), m) << std::endl;); expr_ref lenTestAssert = mk_and(and_items); SASSERT(lenTestAssert); - TRACE("t_str_detail", tout << "crash avoidance lenTestAssert: " << mk_pp(lenTestAssert, m) << std::endl;); + TRACE("str", tout << "crash avoidance lenTestAssert: " << mk_pp(lenTestAssert, m) << std::endl;); int testerCount = tries - 1; if (testerCount > 0) { @@ -9861,10 +9861,10 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr for (int i = 0; i < testerCount; ++i) { expr * indicator = fvar_lenTester_map[freeVar][i]; if (internal_variable_set.find(indicator) == internal_variable_set.end()) { - TRACE("t_str_detail", tout << "indicator " << mk_pp(indicator, m) << " out of scope; continuing" << std::endl;); + TRACE("str", tout << "indicator " << mk_pp(indicator, m) << " out of scope; continuing" << std::endl;); continue; } else { - TRACE("t_str_detail", tout << "indicator " << mk_pp(indicator, m) << " in scope" << std::endl;); + TRACE("str", tout << "indicator " << mk_pp(indicator, m) << " in scope" << std::endl;); and_items_LHS.push_back(ctx.mk_eq_atom(indicator, moreAst)); } } @@ -9872,10 +9872,10 @@ expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tr SASSERT(assertL); expr * finalAxiom = m.mk_or(m.mk_not(assertL), lenTestAssert.get()); SASSERT(finalAxiom != NULL); - TRACE("t_str_detail", tout << "crash avoidance finalAxiom: " << mk_pp(finalAxiom, m) << std::endl;); + TRACE("str", tout << "crash avoidance finalAxiom: " << mk_pp(finalAxiom, m) << std::endl;); return finalAxiom; } else { - TRACE("t_str_detail", tout << "crash avoidance lenTestAssert.get(): " << mk_pp(lenTestAssert.get(), m) << std::endl;); + TRACE("str", tout << "crash avoidance lenTestAssert.get(): " << mk_pp(lenTestAssert.get(), m) << std::endl;); m_trail.push_back(lenTestAssert.get()); return lenTestAssert.get(); } @@ -9892,7 +9892,7 @@ expr_ref theory_str::binary_search_case_split(expr * freeVar, expr * tester, bin rational N_plus_one = N + rational::one(); expr_ref lenFreeVar(mk_strlen(freeVar), m); - TRACE("t_str_binary_search", tout << "create case split for free var " << mk_pp(freeVar, m) + TRACE("str", tout << "create case split for free var " << mk_pp(freeVar, m) << " over " << mk_pp(tester, m) << " with midpoint " << N << std::endl;); expr_ref_vector combinedCaseSplit(m); @@ -9924,7 +9924,7 @@ expr_ref theory_str::binary_search_case_split(expr * freeVar, expr * tester, bin expr_ref final_term(mk_and(combinedCaseSplit), m); SASSERT(final_term); - TRACE("t_str_binary_search", tout << "final term: " << mk_pp(final_term, m) << std::endl;); + TRACE("str", tout << "final term: " << mk_pp(final_term, m) << std::endl;); return final_term; } @@ -9933,7 +9933,7 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT context & ctx = get_context(); if (binary_search_len_tester_stack.contains(freeVar) && !binary_search_len_tester_stack[freeVar].empty()) { - TRACE("t_str_binary_search", tout << "checking existing length testers for " << mk_pp(freeVar, m) << std::endl; + TRACE("str", tout << "checking existing length testers for " << mk_pp(freeVar, m) << std::endl; for (ptr_vector::const_iterator it = binary_search_len_tester_stack[freeVar].begin(); it != binary_search_len_tester_stack[freeVar].end(); ++it) { expr * tester = *it; @@ -9959,35 +9959,35 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT expr * lastTesterValue = get_eqc_value(lastTester, lastTesterHasEqcValue); zstring lastTesterConstant; if (!lastTesterHasEqcValue) { - TRACE("t_str_binary_search", tout << "length tester " << mk_pp(lastTester, m) << " at top of stack doesn't have an EQC value yet" << std::endl;); + TRACE("str", tout << "length tester " << mk_pp(lastTester, m) << " at top of stack doesn't have an EQC value yet" << std::endl;); // check previousLenTester if (previousLenTester == lastTester) { lastTesterConstant = previousLenTesterValue; - TRACE("t_str_binary_search", tout << "invoked with previousLenTester info matching top of stack" << std::endl;); + TRACE("str", tout << "invoked with previousLenTester info matching top of stack" << std::endl;); } else { - TRACE("t_str_binary_search", tout << "WARNING: unexpected reordering of length testers!" << std::endl;); + TRACE("str", tout << "WARNING: unexpected reordering of length testers!" << std::endl;); UNREACHABLE(); return NULL; } } else { u.str.is_string(lastTesterValue, lastTesterConstant); } - TRACE("t_str_binary_search", tout << "last length tester is assigned \"" << lastTesterConstant << "\"" << "\n";); + TRACE("str", tout << "last length tester is assigned \"" << lastTesterConstant << "\"" << "\n";); if (lastTesterConstant == "more" || lastTesterConstant == "less") { // use the previous bounds info to generate a new midpoint binary_search_info lastBounds; if (!binary_search_len_tester_info.find(lastTester, lastBounds)) { // unexpected - TRACE("t_str_binary_search", tout << "WARNING: no bounds information available for last tester!" << std::endl;); + TRACE("str", tout << "WARNING: no bounds information available for last tester!" << std::endl;); UNREACHABLE(); } - TRACE("t_str_binary_search", tout << "last bounds are [" << lastBounds.lowerBound << " | " << lastBounds.midPoint << " | " << lastBounds.upperBound << "]!" << lastBounds.windowSize << std::endl;); + TRACE("str", tout << "last bounds are [" << lastBounds.lowerBound << " | " << lastBounds.midPoint << " | " << lastBounds.upperBound << "]!" << lastBounds.windowSize << std::endl;); binary_search_info newBounds; expr * newTester; if (lastTesterConstant == "more") { // special case: if the midpoint, upper bound, and window size are all equal, // we double the window size and adjust the bounds if (lastBounds.midPoint == lastBounds.upperBound && lastBounds.upperBound == lastBounds.windowSize) { - TRACE("t_str_binary_search", tout << "search hit window size; expanding" << std::endl;); + TRACE("str", tout << "search hit window size; expanding" << std::endl;); newBounds.lowerBound = lastBounds.windowSize + rational::one(); newBounds.windowSize = lastBounds.windowSize * rational(2); newBounds.upperBound = newBounds.windowSize; @@ -10024,7 +10024,7 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT } refresh_theory_var(newTester); } - TRACE("t_str_binary_search", tout << "new bounds are [" << newBounds.lowerBound << " | " << newBounds.midPoint << " | " << newBounds.upperBound << "]!" << newBounds.windowSize << std::endl;); + TRACE("str", tout << "new bounds are [" << newBounds.lowerBound << " | " << newBounds.midPoint << " | " << newBounds.upperBound << "]!" << newBounds.windowSize << std::endl;); binary_search_len_tester_stack[freeVar].push_back(newTester); m_trail_stack.push(binary_search_trail(binary_search_len_tester_stack, freeVar)); binary_search_len_tester_info.insert(newTester, newBounds); @@ -10036,16 +10036,16 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT // ctx.mk_th_case_split(case_split_literals.size(), case_split_literals.c_ptr()); return next_case_split; } else { // lastTesterConstant is a concrete value - TRACE("t_str", tout << "length is fixed; generating models for free var" << std::endl;); + TRACE("str", tout << "length is fixed; generating models for free var" << std::endl;); // defensive check that this length did not converge on a negative value. binary_search_info lastBounds; if (!binary_search_len_tester_info.find(lastTester, lastBounds)) { // unexpected - TRACE("t_str_binary_search", tout << "WARNING: no bounds information available for last tester!" << std::endl;); + TRACE("str", tout << "WARNING: no bounds information available for last tester!" << std::endl;); UNREACHABLE(); } if (lastBounds.midPoint.is_neg()) { - TRACE("t_str_binary_search", tout << "WARNING: length search converged on a negative value. Negating this constraint." << std::endl;); + TRACE("str", tout << "WARNING: length search converged on a negative value. Negating this constraint." << std::endl;); expr_ref axiom(m_autil.mk_ge(mk_strlen(freeVar), m_autil.mk_numeral(rational::zero(), true)), m); return axiom; } @@ -10055,7 +10055,7 @@ expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenT } } else { // no length testers yet - TRACE("t_str_binary_search", tout << "no length testers for " << mk_pp(freeVar, m) << std::endl;); + TRACE("str", tout << "no length testers for " << mk_pp(freeVar, m) << std::endl;); binary_search_len_tester_stack.insert(freeVar, ptr_vector()); expr * firstTester; @@ -10098,15 +10098,15 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe ast_manager & m = get_manager(); - TRACE("t_str_detail", tout << "gen for free var " << mk_ismt2_pp(freeVar, m) << std::endl;); + TRACE("str", tout << "gen for free var " << mk_ismt2_pp(freeVar, m) << std::endl;); if (m_params.m_UseBinarySearch) { - TRACE("t_str_detail", tout << "using binary search heuristic" << std::endl;); + TRACE("str", tout << "using binary search heuristic" << std::endl;); return binary_search_length_test(freeVar, lenTesterInCbEq, lenTesterValue); } else { bool map_effectively_empty = false; if (!fvar_len_count_map.contains(freeVar)) { - TRACE("t_str_detail", tout << "fvar_len_count_map is empty" << std::endl;); + TRACE("str", tout << "fvar_len_count_map is empty" << std::endl;); map_effectively_empty = true; } @@ -10120,18 +10120,18 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe for (ptr_vector::iterator it = indicator_set.begin(); it != indicator_set.end(); ++it) { expr * indicator = *it; if (internal_variable_set.find(indicator) != internal_variable_set.end()) { - TRACE("t_str_detail", tout <<"found active internal variable " << mk_ismt2_pp(indicator, m) + TRACE("str", tout <<"found active internal variable " << mk_ismt2_pp(indicator, m) << " in fvar_lenTester_map[freeVar]" << std::endl;); map_effectively_empty = false; break; } } - CTRACE("t_str_detail", map_effectively_empty, tout << "all variables in fvar_lenTester_map[freeVar] out of scope" << std::endl;); + CTRACE("str", map_effectively_empty, tout << "all variables in fvar_lenTester_map[freeVar] out of scope" << std::endl;); } if (map_effectively_empty) { // no length assertions for this free variable have ever been added. - TRACE("t_str_detail", tout << "no length assertions yet" << std::endl;); + TRACE("str", tout << "no length assertions yet" << std::endl;); fvar_len_count_map.insert(freeVar, 1); unsigned int testNum = fvar_len_count_map[freeVar]; @@ -10148,13 +10148,13 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe SASSERT(lenTestAssert != NULL); return lenTestAssert; } else { - TRACE("t_str_detail", tout << "found previous in-scope length assertions" << std::endl;); + TRACE("str", tout << "found previous in-scope length assertions" << std::endl;); expr * effectiveLenInd = NULL; zstring effectiveLenIndiStr(""); int lenTesterCount = (int) fvar_lenTester_map[freeVar].size(); - TRACE("t_str_detail", + TRACE("str", tout << lenTesterCount << " length testers in fvar_lenTester_map[" << mk_pp(freeVar, m) << "]:" << std::endl; for (int i = 0; i < lenTesterCount; ++i) { expr * len_indicator = fvar_lenTester_map[freeVar][i]; @@ -10170,13 +10170,13 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe expr * len_indicator_pre = fvar_lenTester_map[freeVar][i]; // check whether this is in scope as well if (internal_variable_set.find(len_indicator_pre) == internal_variable_set.end()) { - TRACE("t_str_detail", tout << "length indicator " << mk_pp(len_indicator_pre, m) << " not in scope" << std::endl;); + TRACE("str", tout << "length indicator " << mk_pp(len_indicator_pre, m) << " not in scope" << std::endl;); continue; } bool indicatorHasEqcValue = false; expr * len_indicator_value = get_eqc_value(len_indicator_pre, indicatorHasEqcValue); - TRACE("t_str_detail", tout << "length indicator " << mk_ismt2_pp(len_indicator_pre, m) << + TRACE("str", tout << "length indicator " << mk_ismt2_pp(len_indicator_pre, m) << " = " << mk_ismt2_pp(len_indicator_value, m) << std::endl;); if (indicatorHasEqcValue) { zstring len_pIndiStr; @@ -10188,7 +10188,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe } } else { if (lenTesterInCbEq != len_indicator_pre) { - TRACE("t_str", tout << "WARNING: length indicator " << mk_ismt2_pp(len_indicator_pre, m) + TRACE("str", tout << "WARNING: length indicator " << mk_ismt2_pp(len_indicator_pre, m) << " does not have an equivalence class value." << " i = " << i << ", lenTesterCount = " << lenTesterCount << std::endl;); if (i > 0) { @@ -10196,7 +10196,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe bool effectiveHasEqcValue; expr * effective_eqc_value = get_eqc_value(effectiveLenInd, effectiveHasEqcValue); bool effectiveInScope = (internal_variable_set.find(effectiveLenInd) != internal_variable_set.end()); - TRACE("t_str_detail", tout << "checking effective length indicator " << mk_pp(effectiveLenInd, m) << ": " + TRACE("str", tout << "checking effective length indicator " << mk_pp(effectiveLenInd, m) << ": " << (effectiveInScope ? "in scope" : "NOT in scope") << ", "; if (effectiveHasEqcValue) { tout << "~= " << mk_pp(effective_eqc_value, m); @@ -10227,11 +10227,11 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe } // !indicatorHasEqcValue } // for (i : [0..lenTesterCount-1]) if (effectiveLenIndiStr == "more" || effectiveLenIndiStr == "") { - TRACE("t_str", tout << "length is not fixed; generating length tester options for free var" << std::endl;); + TRACE("str", tout << "length is not fixed; generating length tester options for free var" << std::endl;); expr_ref indicator(m); unsigned int testNum = 0; - TRACE("t_str", tout << "effectiveLenIndiStr = " << effectiveLenIndiStr + TRACE("str", tout << "effectiveLenIndiStr = " << effectiveLenIndiStr << ", i = " << i << ", lenTesterCount = " << lenTesterCount << "\n";); if (i == lenTesterCount) { @@ -10249,7 +10249,7 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe SASSERT(lenTestAssert != NULL); return lenTestAssert; } else { - TRACE("t_str", tout << "length is fixed; generating models for free var" << std::endl;); + TRACE("str", tout << "length is fixed; generating models for free var" << std::endl;); // length is fixed expr * valueAssert = gen_free_var_options(freeVar, effectiveLenInd, effectiveLenIndiStr, NULL, zstring("")); return valueAssert; @@ -10318,7 +10318,7 @@ void theory_str::process_free_var(std::map & freeVar_map) { } } if (duplicated && dupVar != NULL) { - TRACE("t_str_detail", tout << "Duplicated free variable found:" << mk_ismt2_pp(freeVar, m) + TRACE("str", tout << "Duplicated free variable found:" << mk_ismt2_pp(freeVar, m) << " = " << mk_ismt2_pp(dupVar, m) << " (SKIP)" << std::endl;); continue; } else { @@ -10428,7 +10428,7 @@ void theory_str::get_eqc_simpleUnroll(expr * n, expr * &constStr, std::setget_owner(), get_manager()) << + TRACE("str", tout << "mk_value for: " << mk_ismt2_pp(n->get_owner(), get_manager()) << " (sort " << mk_ismt2_pp(get_manager().get_sort(n->get_owner()), get_manager()) << ")" << std::endl;); ast_manager & m = get_manager(); context & ctx = get_context(); @@ -10489,7 +10489,7 @@ model_value_proc * theory_str::mk_value(enode * n, model_generator & mg) { if (val != NULL) { return alloc(expr_wrapper_proc, val); } else { - TRACE("t_str", tout << "WARNING: failed to find a concrete value, falling back" << std::endl;); + TRACE("str", tout << "WARNING: failed to find a concrete value, falling back" << std::endl;); return alloc(expr_wrapper_proc, to_app(mk_string("**UNUSED**"))); } } From 34acaa8f564824f1ff2fddd5e7d27fc4b75a0f33 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Mon, 24 Apr 2017 13:34:10 -0700 Subject: [PATCH 462/562] update license for space/quotes per #982 Signed-off-by: Nikolaj Bjorner --- LICENSE.txt | 7 +++++-- src/interp/iz3proof_itp.cpp | 7 ++++++- src/sat/sat_solver.cpp | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/LICENSE.txt b/LICENSE.txt index 91c8070d0..cc90bed74 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -2,6 +2,9 @@ Z3 Copyright (c) Microsoft Corporation All rights reserved. MIT License -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/src/interp/iz3proof_itp.cpp b/src/interp/iz3proof_itp.cpp index 11cb2f6a2..26ef7386c 100755 --- a/src/interp/iz3proof_itp.cpp +++ b/src/interp/iz3proof_itp.cpp @@ -541,6 +541,7 @@ class iz3proof_itp_impl : public iz3proof_itp { placeholder_arg |= is_placeholder(args[i]); } try { + TRACE("duality", print_expr(tout, e); tout << "\n";); opr f = op(e); if(f == Equal && args[0] == args[1]) res = mk_true(); else if(f == And) res = my_and(args); @@ -853,6 +854,7 @@ class iz3proof_itp_impl : public iz3proof_itp { ast simplify_rotate_eq2leq(const ast &pl, const ast &neg_equality, const ast &pf){ if(pl == arg(pf,1)){ + TRACE("duality", print_expr(tout, pl); print_expr(tout << "\n", neg_equality); print_expr(tout << "\n", pf); tout << "\n";); ast cond = mk_true(); ast equa = sep_cond(arg(pf,0),cond); if(is_equivrel_chain(equa)){ @@ -1870,10 +1872,13 @@ class iz3proof_itp_impl : public iz3proof_itp { ast chain_ineqs(opr comp_op, LitType t, const ast &chain, const ast &lhs, const ast &rhs){ if(is_true(chain)){ - if(lhs != rhs) + if (lhs != rhs) { + TRACE("duality", print_expr(tout, lhs); tout << " "; print_expr(tout, rhs); tout << "\n";); throw bad_ineq_inference(); + } return make(Leq,make_int(rational(0)),make_int(rational(0))); } + TRACE("duality", print_expr(tout, chain); print_expr(tout << "\n", lhs); tout << " "; print_expr(tout, rhs); tout << "\n";); ast last = chain_last(chain); ast rest = chain_rest(chain); ast mid = subst_in_pos(rhs,rewrite_pos(last),rewrite_lhs(last)); diff --git a/src/sat/sat_solver.cpp b/src/sat/sat_solver.cpp index 9c858a29a..e382d1d00 100644 --- a/src/sat/sat_solver.cpp +++ b/src/sat/sat_solver.cpp @@ -3324,7 +3324,7 @@ namespace sat { if (is_sat == l_true) { delete_unfixed(unfixed_lits, unfixed_vars); } - extract_fixed_consequences(num_units, assumptions, unfixed_vars, conseq); + extract_fixed_consequences(num_units, assumptions, unfixed_vars, conseq); update_unfixed_literals(unfixed_lits, unfixed_vars); IF_VERBOSE(1, verbose_stream() << "(sat.get-consequences" << " iterations: " << num_iterations From 2cdb45605dcc3f17d0101f9669fb6ad8a4923e83 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Mon, 24 Apr 2017 16:24:15 +0100 Subject: [PATCH 463/562] [Doxygen] Switch to using `argparse` to parse command line arguments in `mk_api_doc.py`. Given that we need to add a bunch of new command line options it makes sense to use a less clumsy and concise API. --- doc/mk_api_doc.py | 50 ++++++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/doc/mk_api_doc.py b/doc/mk_api_doc.py index e86205a7a..d27351015 100644 --- a/doc/mk_api_doc.py +++ b/doc/mk_api_doc.py @@ -1,5 +1,9 @@ # Copyright (c) Microsoft Corporation 2015 +""" +Z3 API documentation generator script +""" +import argparse import os import shutil import re @@ -12,39 +16,23 @@ import shutil ML_ENABLED=False BUILD_DIR='../build' -def display_help(exit_code): - assert isinstance(exit_code, int) - print("mk_api_doc.py: Z3 documentation generator\n") - print("\nOptions:") - print(" -h, --help display this message.") - print(" -b , --build= subdirectory where Z3 is built (default: ../build).") - print(" --ml include ML/OCaml API documentation.") - sys.exit(exit_code) - def parse_options(): global ML_ENABLED, BUILD_DIR - - try: - options, remainder = getopt.gnu_getopt(sys.argv[1:], - 'b:h', - ['build=', 'help', 'ml']) - except: - print("ERROR: Invalid command line option") - display_help(1) - - for opt, arg in options: - if opt in ('-b', '--build'): - BUILD_DIR = mk_util.norm_path(arg) - elif opt in ('h', '--help'): - display_help(0) - elif opt in ('--ml'): - ML_ENABLED=True - else: - print("ERROR: Invalid command line option: %s" % opt) - display_help(1) - - - + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('-b', + '--build', + default=BUILD_DIR, + help='Directory where Z3 is built (default: %(default)s)', + ) + parser.add_argument('--ml', + action='store_true', + default=False, + help='Include ML/OCaml API documentation' + ) + pargs = parser.parse_args() + ML_ENABLED = pargs.ml + BUILD_DIR = pargs.build + return def mk_dir(d): if not os.path.exists(d): From 8a1df3df6293b5f8ac9be6d42d8ba6f23d708864 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Mon, 24 Apr 2017 21:52:59 +0100 Subject: [PATCH 464/562] [Doxygen] Add `--doxygen-executable` command line option to `mk_api_doc.py`. This allows a custom path to Doxygen to be specified. --- doc/mk_api_doc.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/doc/mk_api_doc.py b/doc/mk_api_doc.py index d27351015..62130d73c 100644 --- a/doc/mk_api_doc.py +++ b/doc/mk_api_doc.py @@ -15,9 +15,10 @@ import shutil ML_ENABLED=False BUILD_DIR='../build' +DOXYGEN_EXE='doxygen' def parse_options(): - global ML_ENABLED, BUILD_DIR + global ML_ENABLED, BUILD_DIR, DOXYGEN_EXE parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('-b', '--build', @@ -29,9 +30,15 @@ def parse_options(): default=False, help='Include ML/OCaml API documentation' ) + parser.add_argument('--doxygen-executable', + dest='doxygen_executable', + default=DOXYGEN_EXE, + help='Doxygen executable to use (default: %(default)s)', + ) pargs = parser.parse_args() ML_ENABLED = pargs.ml BUILD_DIR = pargs.build + DOXYGEN_EXE = pargs.doxygen_executable return def mk_dir(d): @@ -81,7 +88,7 @@ try: print("Removed annotations from z3_api.h.") try: - if subprocess.call(['doxygen', 'z3api.dox']) != 0: + if subprocess.call([DOXYGEN_EXE, 'z3api.dox']) != 0: print("ERROR: doxygen returned nonzero return code") exit(1) except: From 3fe49137d0f678b09340d0c20beded6521812c64 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 24 Apr 2017 19:25:35 -0400 Subject: [PATCH 465/562] fix trace typos --- src/ast/seq_decl_plugin.h | 4 ++++ src/ast/static_features.cpp | 7 +++++++ src/ast/static_features.h | 4 ++++ src/smt/params/smt_params.cpp | 3 ++- src/smt/params/smt_params.h | 10 +++++++++- src/smt/params/smt_params_helper.pyg | 1 + src/smt/smt_setup.cpp | 27 +++++++++++++++++++-------- src/smt/smt_setup.h | 2 +- src/smt/theory_str.cpp | 7 +++---- 9 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/ast/seq_decl_plugin.h b/src/ast/seq_decl_plugin.h index 2882e905d..030b244e5 100644 --- a/src/ast/seq_decl_plugin.h +++ b/src/ast/seq_decl_plugin.h @@ -273,6 +273,10 @@ public: bool is_in_re(expr const* n) const { return is_app_of(n, m_fid, OP_SEQ_IN_RE); } bool is_unit(expr const* n) const { return is_app_of(n, m_fid, OP_SEQ_UNIT); } + bool is_string_term(expr const * n) const { + sort * s = get_sort(n); + return is_sort_of(s, m_fid, _STRING_SORT); + } MATCH_BINARY(is_concat); MATCH_UNARY(is_length); diff --git a/src/ast/static_features.cpp b/src/ast/static_features.cpp index 328128794..9958b3d50 100644 --- a/src/ast/static_features.cpp +++ b/src/ast/static_features.cpp @@ -25,6 +25,7 @@ static_features::static_features(ast_manager & m): m_bvutil(m), m_arrayutil(m), m_fpautil(m), + m_sequtil(m), m_bfid(m.get_basic_family_id()), m_afid(m.mk_family_id("arith")), m_lfid(m.mk_family_id("label")), @@ -77,6 +78,8 @@ void static_features::reset() { m_has_real = false; m_has_bv = false; m_has_fpa = false; + m_has_str = false; + m_has_seq_non_str = false; m_has_arrays = false; m_arith_k_sum .reset(); m_num_arith_terms = 0; @@ -279,6 +282,10 @@ void static_features::update_core(expr * e) { m_has_fpa = true; if (!m_has_arrays && m_arrayutil.is_array(e)) m_has_arrays = true; + if (!m_has_str && m_sequtil.str.is_string_term(e)) + m_has_str = true; + if (!m_has_seq_non_str && m_sequtil.is_seq(e)) + m_has_seq_non_str = true; if (is_app(e)) { family_id fid = to_app(e)->get_family_id(); mark_theory(fid); diff --git a/src/ast/static_features.h b/src/ast/static_features.h index 8b20c5463..e7f69e041 100644 --- a/src/ast/static_features.h +++ b/src/ast/static_features.h @@ -24,6 +24,7 @@ Revision History: #include"bv_decl_plugin.h" #include"array_decl_plugin.h" #include"fpa_decl_plugin.h" +#include"seq_decl_plugin.h" #include"map.h" struct static_features { @@ -32,6 +33,7 @@ struct static_features { bv_util m_bvutil; array_util m_arrayutil; fpa_util m_fpautil; + seq_util m_sequtil; family_id m_bfid; family_id m_afid; family_id m_lfid; @@ -77,6 +79,8 @@ struct static_features { bool m_has_real; // bool m_has_bv; // bool m_has_fpa; // + bool m_has_str; // has String-typed terms + bool m_has_seq_non_str; // has non-String-typed Sequence terms bool m_has_arrays; // rational m_arith_k_sum; // sum of the numerals in arith atoms. unsigned m_num_arith_terms; diff --git a/src/smt/params/smt_params.cpp b/src/smt/params/smt_params.cpp index dcf396531..b8d5fe7b5 100644 --- a/src/smt/params/smt_params.cpp +++ b/src/smt/params/smt_params.cpp @@ -41,6 +41,7 @@ void smt_params::updt_local_params(params_ref const & _p) { m_max_conflicts = p.max_conflicts(); m_core_validate = p.core_validate(); m_logic = _p.get_sym("logic", m_logic); + m_string_solver = _p.get_sym("string_solver", m_string_solver); model_params mp(_p); m_model_compact = mp.compact(); if (_p.get_bool("arith.greatest_error_pivot", false)) @@ -157,4 +158,4 @@ void smt_params::display(std::ostream & out) const { DISPLAY_PARAM(m_check_at_labels); DISPLAY_PARAM(m_dump_goal_as_smt); DISPLAY_PARAM(m_auto_config); -} \ No newline at end of file +} diff --git a/src/smt/params/smt_params.h b/src/smt/params/smt_params.h index a86123a33..295e141cc 100644 --- a/src/smt/params/smt_params.h +++ b/src/smt/params/smt_params.h @@ -216,6 +216,13 @@ struct smt_params : public preprocessor_params, bool m_dump_goal_as_smt; bool m_auto_config; + // ----------------------------------- + // + // Solver selection + // + // ----------------------------------- + symbol m_string_solver; + smt_params(params_ref const & p = params_ref()): m_display_proof(false), m_display_dot_proof(false), @@ -286,7 +293,8 @@ struct smt_params : public preprocessor_params, m_at_labels_cex(false), m_check_at_labels(false), m_dump_goal_as_smt(false), - m_auto_config(true) { + m_auto_config(true), + m_string_solver(symbol("auto")){ updt_local_params(p); } diff --git a/src/smt/params/smt_params_helper.pyg b/src/smt/params/smt_params_helper.pyg index 133d1d527..f99c2df16 100644 --- a/src/smt/params/smt_params_helper.pyg +++ b/src/smt/params/smt_params_helper.pyg @@ -62,6 +62,7 @@ def_module_params(module_name='smt', ('dack.gc_inv_decay', DOUBLE, 0.8, 'Dynamic ackermannization garbage collection decay'), ('dack.threshold', UINT, 10, ' number of times the congruence rule must be used before Leibniz\'s axiom is expanded'), ('core.validate', BOOL, False, 'validate unsat core produced by SMT context'), + ('string_solver', SYMBOL, 'auto', 'solver for string/sequence theories. options are: \'z3str3\' (specialized string solver), \'seq\' (sequence solver), \'auto\' (use static features to choose best solver)'), ('str.strong_arrangements', BOOL, True, 'assert equivalences instead of implications when generating string arrangement axioms'), ('str.aggressive_length_testing', BOOL, False, 'prioritize testing concrete length values over generating more options'), ('str.aggressive_value_testing', BOOL, False, 'prioritize testing concrete string constant values over generating more options'), diff --git a/src/smt/smt_setup.cpp b/src/smt/smt_setup.cpp index 78a295e27..c295801ad 100644 --- a/src/smt/smt_setup.cpp +++ b/src/smt/smt_setup.cpp @@ -206,7 +206,7 @@ namespace smt { void setup::setup_QF_BVRE() { setup_QF_BV(); setup_QF_LIA(); - setup_seq(); + m_context.register_plugin(alloc(theory_seq, m_manager)); } void setup::setup_QF_UF(static_features const & st) { @@ -824,10 +824,21 @@ namespace smt { m_context.register_plugin(mk_theory_dl(m_manager)); } - void setup::setup_seq() { - // TODO proper negotiation of theory_str vs. theory_seq - //m_context.register_plugin(alloc(theory_seq, m_manager)); - setup_str(); + void setup::setup_seq(static_features const & st) { + // check params for what to do here when it's ambiguous + if (m_params.m_string_solver == "z3str3") { + setup_str(); + } else if (m_params.m_string_solver == "seq") { + m_context.register_plugin(alloc(theory_seq, m_manager)); + } else if (m_params.m_string_solver == "auto") { + if (st.m_has_seq_non_str) { + m_context.register_plugin(alloc(theory_seq, m_manager)); + } else { + setup_str(); + } + } else { + throw default_exception("invalid parameter for smt.string_solver, valid options are 'z3str3', 'seq', 'auto'"); + } } void setup::setup_card() { @@ -850,10 +861,10 @@ namespace smt { setup_bv(); setup_datatypes(); setup_dl(); - setup_seq(); + // setup_seq() + m_context.register_plugin(alloc(theory_seq, m_manager)); setup_card(); setup_fpa(); - setup_str(); } void setup::setup_unknown(static_features & st) { @@ -866,7 +877,7 @@ namespace smt { setup_datatypes(); setup_bv(); setup_dl(); - setup_seq(); + setup_seq(st); setup_card(); setup_fpa(); return; diff --git a/src/smt/smt_setup.h b/src/smt/smt_setup.h index 031c65c1f..d30c896e5 100644 --- a/src/smt/smt_setup.h +++ b/src/smt/smt_setup.h @@ -94,7 +94,7 @@ namespace smt { void setup_bv(); void setup_arith(); void setup_dl(); - void setup_seq(); + void setup_seq(static_features const & st); void setup_card(); void setup_i_arith(); void setup_mi_arith(); diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 01123a22c..be268ec5c 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4844,7 +4844,7 @@ void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { // we only want to inspect the Contains terms where either of strAst or substrAst // are equal to varNode. - TRACE("t_str_detail", tout << "considering Contains with strAst = "str", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); + TRACE("t_str_detail", tout << "considering Contains with strAst = " << mk_pp(strAst, m) << ", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); if (varNode != strAst && varNode != substrAst) { TRACE("str", tout << "varNode not equal to strAst or substrAst, skip" << std::endl;); @@ -4873,7 +4873,7 @@ void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { zstring subStrConst; u.str.is_string(substrValue, subStrConst); - TRACE("t_str_detail", tout << "strConst = "str", subStrConst = " << subStrConst << "\n";); + TRACE("t_str_detail", tout << "strConst = " << strConst << ", subStrConst = " << subStrConst << "\n";); if (strConst.contains(subStrConst)) { //implyR = ctx.mk_eq(ctx, boolVar, Z3_mk_true(ctx)); @@ -4983,7 +4983,7 @@ void theory_str::check_contain_by_substr(expr * varNode, expr_ref_vector & willE // we only want to inspect the Contains terms where either of strAst or substrAst // are equal to varNode. - TRACE("t_str_detail", tout << "considering Contains with strAst = "str", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); + TRACE("t_str_detail", tout << "considering Contains with strAst = " << mk_pp(strAst, m) << ", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); if (varNode != strAst && varNode != substrAst) { TRACE("str", tout << "varNode not equal to strAst or substrAst, skip" << std::endl;); @@ -6181,7 +6181,6 @@ void nfa::convert_re(expr * e, unsigned & start, unsigned & end, seq_util & u) { } make_transition(last, str[(str.length() - 1)], end); TRACE("str", tout << "string transition " << last << "--" << str[(str.length() - 1)] << "--> " << end << "\n";); - TRACE("t_str_rw", tout << "str", end = " << end << std::endl;); } else { TRACE("str", tout << "invalid string constant in Str2Reg" << std::endl;); m_valid = false; From 54e28a4fe73fd807f1c2aebd12e354dcb46c02b3 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 24 Apr 2017 21:02:22 -0400 Subject: [PATCH 466/562] string/sequence static features test --- src/ast/seq_decl_plugin.h | 11 +++++++++++ src/ast/static_features.cpp | 3 ++- src/smt/smt_setup.cpp | 16 +++++++++++++--- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/ast/seq_decl_plugin.h b/src/ast/seq_decl_plugin.h index 030b244e5..52abb2c45 100644 --- a/src/ast/seq_decl_plugin.h +++ b/src/ast/seq_decl_plugin.h @@ -278,6 +278,17 @@ public: return is_sort_of(s, m_fid, _STRING_SORT); } + bool is_non_string_sequence(expr const * n) const { + if (is_string_term(n)) + return false; + + sort * s = get_sort(n); + if (u.is_seq(s) && !u.is_string(s)) { + return true; + } + return false; + } + MATCH_BINARY(is_concat); MATCH_UNARY(is_length); MATCH_TERNARY(is_extract); diff --git a/src/ast/static_features.cpp b/src/ast/static_features.cpp index 9958b3d50..daf20e095 100644 --- a/src/ast/static_features.cpp +++ b/src/ast/static_features.cpp @@ -284,8 +284,9 @@ void static_features::update_core(expr * e) { m_has_arrays = true; if (!m_has_str && m_sequtil.str.is_string_term(e)) m_has_str = true; - if (!m_has_seq_non_str && m_sequtil.is_seq(e)) + if (!m_has_seq_non_str && m_sequtil.str.is_non_string_sequence(e)) { m_has_seq_non_str = true; + } if (is_app(e)) { family_id fid = to_app(e)->get_family_id(); mark_theory(fid); diff --git a/src/smt/smt_setup.cpp b/src/smt/smt_setup.cpp index c295801ad..dd94d9473 100644 --- a/src/smt/smt_setup.cpp +++ b/src/smt/smt_setup.cpp @@ -832,6 +832,7 @@ namespace smt { m_context.register_plugin(alloc(theory_seq, m_manager)); } else if (m_params.m_string_solver == "auto") { if (st.m_has_seq_non_str) { + NOT_IMPLEMENTED_YET(); m_context.register_plugin(alloc(theory_seq, m_manager)); } else { setup_str(); @@ -856,13 +857,15 @@ namespace smt { } void setup::setup_unknown() { + static_features st(m_manager); + st.collect(m_context.get_num_asserted_formulas(), m_context.get_asserted_formulas()); + setup_arith(); setup_arrays(); setup_bv(); setup_datatypes(); setup_dl(); - // setup_seq() - m_context.register_plugin(alloc(theory_seq, m_manager)); + setup_seq(st); setup_card(); setup_fpa(); } @@ -966,7 +969,14 @@ namespace smt { return; } - // TODO setup_str() by features + if (st.num_theories() == 2 && st.m_has_str && !st.m_has_seq_non_str) { + setup_QF_S(); + return; + } + + if (st.num_theories() == 2 && st.m_has_seq_non_str) { + m_context.register_plugin(alloc(theory_seq, m_manager)); + } setup_unknown(); } From 48b62d34b7563c8267e1c8f480d254e85154a88e Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Mon, 24 Apr 2017 18:08:52 -0700 Subject: [PATCH 467/562] make sure consequence generation works with interpreted atoms/terms Signed-off-by: Nikolaj Bjorner --- src/smt/smt_consequences.cpp | 83 +++++++++++++++++++++++++++--------- src/smt/smt_context.h | 6 +-- 2 files changed, 66 insertions(+), 23 deletions(-) diff --git a/src/smt/smt_consequences.cpp b/src/smt/smt_consequences.cpp index 9558f6a3b..88f4308cf 100644 --- a/src/smt/smt_consequences.cpp +++ b/src/smt/smt_consequences.cpp @@ -44,13 +44,14 @@ namespace smt { // - e is an equality between a variable and value that is to be fixed. // - e is a data-type recognizer of a variable that is to be fixed. // - void context::extract_fixed_consequences(literal lit, obj_map& vars, index_set const& assumptions, expr_ref_vector& conseq) { + void context::extract_fixed_consequences(literal lit, obj_map& vars, obj_map const& var2orig, index_set const& assumptions, expr_ref_vector& conseq) { ast_manager& m = m_manager; datatype_util dt(m); expr* e1, *e2; expr_ref fml(m); if (lit == true_literal) return; expr* e = bool_var2expr(lit.var()); + TRACE("context", display(tout << mk_pp(e, m) << "\n");); index_set s; if (assumptions.contains(lit.var())) { s.insert(lit.var()); @@ -67,17 +68,23 @@ namespace smt { bool found = false; if (vars.contains(e)) { found = true; - fml = lit.sign() ? m.mk_not(e) : e; vars.erase(e); + e = var2orig.find(e); + fml = lit.sign() ? m.mk_not(e) : e; } else if (!lit.sign() && m.is_eq(e, e1, e2)) { - if (vars.contains(e2)) { - std::swap(e1, e2); - } - if (vars.contains(e1) && m.is_value(e2)) { + if (vars.contains(e2) && m.is_value(e1)) { + found = true; + vars.erase(e2); + e2 = var2orig.find(e2); + std::swap(e1, e2); + fml = m.mk_eq(e1, e2); + } + else if (vars.contains(e1) && m.is_value(e2)) { found = true; - fml = e; vars.erase(e1); + e1 = var2orig.find(e1); + fml = m.mk_eq(e1, e2); } } else if (!lit.sign() && is_app(e) && dt.is_recognizer(to_app(e)->get_decl())) { @@ -94,6 +101,7 @@ namespace smt { } void context::justify(literal lit, index_set& s) { + ast_manager& m = m_manager; b_justification js = get_justification(lit.var()); switch (js.get_kind()) { case b_justification::CLAUSE: { @@ -119,6 +127,9 @@ namespace smt { literal_vector literals; m_conflict_resolution->justification2literals(js.get_justification(), literals); for (unsigned j = 0; j < literals.size(); ++j) { + if (!m_antecedents.contains(literals[j].var())) { + TRACE("context", tout << literals[j] << " " << mk_pp(bool_var2expr(literals[j].var()), m) << "\n";); + } s |= m_antecedents.find(literals[j].var()); } break; @@ -126,13 +137,13 @@ namespace smt { } } - void context::extract_fixed_consequences(unsigned& start, obj_map& vars, index_set const& assumptions, expr_ref_vector& conseq) { + void context::extract_fixed_consequences(unsigned& start, obj_map& vars, obj_map const& var2orig, index_set const& assumptions, expr_ref_vector& conseq) { pop_to_search_lvl(); SASSERT(!inconsistent()); literal_vector const& lits = assigned_literals(); unsigned sz = lits.size(); for (unsigned i = start; i < sz; ++i) { - extract_fixed_consequences(lits[i], vars, assumptions, conseq); + extract_fixed_consequences(lits[i], vars, var2orig, assumptions, conseq); } start = sz; SASSERT(!inconsistent()); @@ -202,7 +213,7 @@ namespace smt { // Add a clause to short-circuit the congruence justifications for // next rounds. // - unsigned context::extract_fixed_eqs(obj_map& var2val, expr_ref_vector& conseq) { + unsigned context::extract_fixed_eqs(obj_map& var2val, obj_map const& var2orig, expr_ref_vector& conseq) { TRACE("context", tout << "extract fixed consequences\n";); ast_manager& m = m_manager; ptr_vector to_delete; @@ -220,7 +231,7 @@ namespace smt { s |= m_antecedents.find(literals[i].var()); } - fml = m.mk_eq(k, v); + fml = m.mk_eq(var2orig.find(k), v); fml = m.mk_implies(antecedent2fml(s), fml); conseq.push_back(fml); to_delete.push_back(k); @@ -242,9 +253,13 @@ namespace smt { literal context::mk_diseq(expr* e, expr* val) { ast_manager& m = m_manager; - if (m.is_bool(e)) { + if (m.is_bool(e) && b_internalized(e)) { return literal(get_bool_var(e), m.is_true(val)); } + else if (m.is_bool(e)) { + internalize_formula(e, false); + return literal(get_bool_var(e), !m.is_true(val)); + } else { expr_ref eq(mk_eq_atom(e, val), m); internalize_formula(eq, false); @@ -253,15 +268,39 @@ namespace smt { } lbool context::get_consequences(expr_ref_vector const& assumptions, - expr_ref_vector const& vars, + expr_ref_vector const& vars1, expr_ref_vector& conseq, expr_ref_vector& unfixed) { m_antecedents.reset(); + m_antecedents.insert(true_literal.var(), index_set()); pop_to_base_lvl(); + ast_manager& m = m_manager; + expr_ref_vector vars(m); + obj_map var2orig; + bool pushed = false; + for (unsigned i = 0; i < vars1.size(); ++i) { + expr* v = vars1[i]; + if (is_uninterp_const(v)) { + vars.push_back(v); + var2orig.insert(v, v); + } + else { + if (!pushed) { + pushed = true; + push(); + } + expr_ref c(m.mk_fresh_const("v", m.get_sort(v)), m); + expr_ref eq(m.mk_eq(c, v), m); + assert_expr(eq); + vars.push_back(c); + var2orig.insert(c, v); + } + } lbool is_sat = check(assumptions.size(), assumptions.c_ptr()); if (is_sat != l_true) { TRACE("context", tout << is_sat << "\n";); + if (pushed) pop(1); return is_sat; } @@ -272,23 +311,22 @@ namespace smt { } model_ref mdl; get_model(mdl); - ast_manager& m = m_manager; expr_ref_vector trail(m); model_evaluator eval(*mdl.get()); expr_ref val(m); TRACE("context", model_pp(tout, *mdl);); for (unsigned i = 0; i < vars.size(); ++i) { - eval(vars[i], val); + eval(vars[i].get(), val); if (m.is_value(val)) { trail.push_back(val); - var2val.insert(vars[i], val); + var2val.insert(vars[i].get(), val); } else { - unfixed.push_back(vars[i]); + unfixed.push_back(vars[i].get()); } } unsigned num_units = 0; - extract_fixed_consequences(num_units, var2val, _assumptions, conseq); + extract_fixed_consequences(num_units, var2val, var2orig, _assumptions, conseq); app_ref eq(m); TRACE("context", tout << "vars: " << vars.size() << "\n"; @@ -303,6 +341,7 @@ namespace smt { unsigned num_vars = 0; for (; it != end && num_vars < chunk_size; ++it) { if (get_cancel_flag()) { + if (pushed) pop(1); return l_undef; } expr* e = it->m_key; @@ -332,6 +371,7 @@ namespace smt { while (true) { is_sat = bounded_search(); if (is_sat != l_true && m_last_search_failure != OK) { + if (pushed) pop(1); return is_sat; } if (is_sat == l_undef) { @@ -349,8 +389,8 @@ namespace smt { if (is_sat == l_true) { delete_unfixed(var2val, unfixed); } - extract_fixed_consequences(num_units, var2val, _assumptions, conseq); - num_fixed_eqs += extract_fixed_eqs(var2val, conseq); + extract_fixed_consequences(num_units, var2val, var2orig, _assumptions, conseq); + num_fixed_eqs += extract_fixed_eqs(var2val, var2orig, conseq); IF_VERBOSE(1, display_consequence_progress(verbose_stream(), num_iterations, var2val.size(), conseq.size(), unfixed.size(), num_fixed_eqs);); TRACE("context", display_consequence_progress(tout, num_iterations, var2val.size(), conseq.size(), @@ -359,6 +399,9 @@ namespace smt { end_search(); DEBUG_CODE(validate_consequences(assumptions, vars, conseq, unfixed);); + if (pushed) { + pop(1); + } return l_true; } diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index 1f57a7550..8fd958fb0 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -1377,14 +1377,14 @@ namespace smt { typedef hashtable index_set; //typedef uint_set index_set; u_map m_antecedents; - void extract_fixed_consequences(literal lit, obj_map& var2val, index_set const& assumptions, expr_ref_vector& conseq); - void extract_fixed_consequences(unsigned& idx, obj_map& var2val, index_set const& assumptions, expr_ref_vector& conseq); + void extract_fixed_consequences(literal lit, obj_map& var2val, obj_map const& var2orig, index_set const& assumptions, expr_ref_vector& conseq); + void extract_fixed_consequences(unsigned& idx, obj_map& var2val, obj_map const& var2orig, index_set const& assumptions, expr_ref_vector& conseq); void display_consequence_progress(std::ostream& out, unsigned it, unsigned nv, unsigned fixed, unsigned unfixed, unsigned eq); unsigned delete_unfixed(obj_map& var2val, expr_ref_vector& unfixed); - unsigned extract_fixed_eqs(obj_map& var2val, expr_ref_vector& conseq); + unsigned extract_fixed_eqs(obj_map& var2val, obj_map const& var2orig, expr_ref_vector& conseq); expr_ref antecedent2fml(index_set const& ante); From 6fececaad99d8972e2b0780ef32a7c19ad9620fc Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 24 Apr 2017 21:47:31 -0400 Subject: [PATCH 468/562] fix str/seq parameter config --- src/smt/params/smt_params.cpp | 2 +- src/smt/smt_setup.cpp | 10 ---------- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/src/smt/params/smt_params.cpp b/src/smt/params/smt_params.cpp index b8d5fe7b5..92ff1de90 100644 --- a/src/smt/params/smt_params.cpp +++ b/src/smt/params/smt_params.cpp @@ -41,7 +41,7 @@ void smt_params::updt_local_params(params_ref const & _p) { m_max_conflicts = p.max_conflicts(); m_core_validate = p.core_validate(); m_logic = _p.get_sym("logic", m_logic); - m_string_solver = _p.get_sym("string_solver", m_string_solver); + m_string_solver = p.string_solver(); model_params mp(_p); m_model_compact = mp.compact(); if (_p.get_bool("arith.greatest_error_pivot", false)) diff --git a/src/smt/smt_setup.cpp b/src/smt/smt_setup.cpp index dd94d9473..4d02218bf 100644 --- a/src/smt/smt_setup.cpp +++ b/src/smt/smt_setup.cpp @@ -832,7 +832,6 @@ namespace smt { m_context.register_plugin(alloc(theory_seq, m_manager)); } else if (m_params.m_string_solver == "auto") { if (st.m_has_seq_non_str) { - NOT_IMPLEMENTED_YET(); m_context.register_plugin(alloc(theory_seq, m_manager)); } else { setup_str(); @@ -969,15 +968,6 @@ namespace smt { return; } - if (st.num_theories() == 2 && st.m_has_str && !st.m_has_seq_non_str) { - setup_QF_S(); - return; - } - - if (st.num_theories() == 2 && st.m_has_seq_non_str) { - m_context.register_plugin(alloc(theory_seq, m_manager)); - } - setup_unknown(); } From bd8b0186d6e76fb589b3f16c51d5b9afebbb0e22 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Tue, 25 Apr 2017 10:30:10 -0700 Subject: [PATCH 469/562] make SMT consequence finding work with compound terms and formulas Signed-off-by: Nikolaj Bjorner --- src/smt/smt_consequences.cpp | 100 +++++++++++++++++++++-------------- src/smt/smt_context.h | 11 ++-- 2 files changed, 67 insertions(+), 44 deletions(-) diff --git a/src/smt/smt_consequences.cpp b/src/smt/smt_consequences.cpp index 88f4308cf..65272207e 100644 --- a/src/smt/smt_consequences.cpp +++ b/src/smt/smt_consequences.cpp @@ -30,6 +30,7 @@ namespace smt { index_set::iterator it = vars.begin(), end = vars.end(); for (; it != end; ++it) { expr* e = bool_var2expr(*it); + e = m_assumption2orig.find(e); premises.push_back(get_assignment(*it) != l_false ? e : m_manager.mk_not(e)); } return mk_and(premises); @@ -44,7 +45,7 @@ namespace smt { // - e is an equality between a variable and value that is to be fixed. // - e is a data-type recognizer of a variable that is to be fixed. // - void context::extract_fixed_consequences(literal lit, obj_map& vars, obj_map const& var2orig, index_set const& assumptions, expr_ref_vector& conseq) { + void context::extract_fixed_consequences(literal lit, index_set const& assumptions, expr_ref_vector& conseq) { ast_manager& m = m_manager; datatype_util dt(m); expr* e1, *e2; @@ -66,32 +67,32 @@ namespace smt { } tout << "\n";); bool found = false; - if (vars.contains(e)) { + if (m_var2val.contains(e)) { found = true; - vars.erase(e); - e = var2orig.find(e); + m_var2val.erase(e); + e = m_var2orig.find(e); fml = lit.sign() ? m.mk_not(e) : e; } else if (!lit.sign() && m.is_eq(e, e1, e2)) { - if (vars.contains(e2) && m.is_value(e1)) { + if (m_var2val.contains(e2) && m.is_value(e1)) { found = true; - vars.erase(e2); - e2 = var2orig.find(e2); + m_var2val.erase(e2); + e2 = m_var2orig.find(e2); std::swap(e1, e2); fml = m.mk_eq(e1, e2); } - else if (vars.contains(e1) && m.is_value(e2)) { + else if (m_var2val.contains(e1) && m.is_value(e2)) { found = true; - vars.erase(e1); - e1 = var2orig.find(e1); + m_var2val.erase(e1); + e1 = m_var2orig.find(e1); fml = m.mk_eq(e1, e2); } } else if (!lit.sign() && is_app(e) && dt.is_recognizer(to_app(e)->get_decl())) { - if (vars.contains(to_app(e)->get_arg(0))) { + if (m_var2val.contains(to_app(e)->get_arg(0))) { found = true; fml = m.mk_eq(to_app(e)->get_arg(0), m.mk_const(dt.get_recognizer_constructor(to_app(e)->get_decl()))); - vars.erase(to_app(e)->get_arg(0)); + m_var2val.erase(to_app(e)->get_arg(0)); } } if (found) { @@ -137,13 +138,13 @@ namespace smt { } } - void context::extract_fixed_consequences(unsigned& start, obj_map& vars, obj_map const& var2orig, index_set const& assumptions, expr_ref_vector& conseq) { + void context::extract_fixed_consequences(unsigned& start, index_set const& assumptions, expr_ref_vector& conseq) { pop_to_search_lvl(); SASSERT(!inconsistent()); literal_vector const& lits = assigned_literals(); unsigned sz = lits.size(); for (unsigned i = start; i < sz; ++i) { - extract_fixed_consequences(lits[i], vars, var2orig, assumptions, conseq); + extract_fixed_consequences(lits[i], assumptions, conseq); } start = sz; SASSERT(!inconsistent()); @@ -161,10 +162,10 @@ namespace smt { // rules out as many non-fixed variables as possible. // - unsigned context::delete_unfixed(obj_map& var2val, expr_ref_vector& unfixed) { + unsigned context::delete_unfixed(expr_ref_vector& unfixed) { ast_manager& m = m_manager; ptr_vector to_delete; - obj_map::iterator it = var2val.begin(), end = var2val.end(); + obj_map::iterator it = m_var2val.begin(), end = m_var2val.end(); for (; it != end; ++it) { expr* k = it->m_key; expr* v = it->m_value; @@ -200,7 +201,7 @@ namespace smt { } } for (unsigned i = 0; i < to_delete.size(); ++i) { - var2val.remove(to_delete[i]); + m_var2val.remove(to_delete[i]); unfixed.push_back(to_delete[i]); } return to_delete.size(); @@ -213,12 +214,12 @@ namespace smt { // Add a clause to short-circuit the congruence justifications for // next rounds. // - unsigned context::extract_fixed_eqs(obj_map& var2val, obj_map const& var2orig, expr_ref_vector& conseq) { + unsigned context::extract_fixed_eqs(expr_ref_vector& conseq) { TRACE("context", tout << "extract fixed consequences\n";); ast_manager& m = m_manager; ptr_vector to_delete; expr_ref fml(m), eq(m); - obj_map::iterator it = var2val.begin(), end = var2val.end(); + obj_map::iterator it = m_var2val.begin(), end = m_var2val.end(); for (; it != end; ++it) { expr* k = it->m_key; expr* v = it->m_value; @@ -231,7 +232,7 @@ namespace smt { s |= m_antecedents.find(literals[i].var()); } - fml = m.mk_eq(var2orig.find(k), v); + fml = m.mk_eq(m_var2orig.find(k), v); fml = m.mk_implies(antecedent2fml(s), fml); conseq.push_back(fml); to_delete.push_back(k); @@ -246,7 +247,7 @@ namespace smt { } } for (unsigned i = 0; i < to_delete.size(); ++i) { - var2val.remove(to_delete[i]); + m_var2val.remove(to_delete[i]); } return to_delete.size(); } @@ -267,8 +268,8 @@ namespace smt { } } - lbool context::get_consequences(expr_ref_vector const& assumptions, - expr_ref_vector const& vars1, + lbool context::get_consequences(expr_ref_vector const& assumptions0, + expr_ref_vector const& vars0, expr_ref_vector& conseq, expr_ref_vector& unfixed) { @@ -276,14 +277,16 @@ namespace smt { m_antecedents.insert(true_literal.var(), index_set()); pop_to_base_lvl(); ast_manager& m = m_manager; - expr_ref_vector vars(m); - obj_map var2orig; + expr_ref_vector vars(m), assumptions(m); + m_var2val.reset(); + m_var2orig.reset(); + m_assumption2orig.reset(); bool pushed = false; - for (unsigned i = 0; i < vars1.size(); ++i) { - expr* v = vars1[i]; + for (unsigned i = 0; i < vars0.size(); ++i) { + expr* v = vars0[i]; if (is_uninterp_const(v)) { vars.push_back(v); - var2orig.insert(v, v); + m_var2orig.insert(v, v); } else { if (!pushed) { @@ -294,7 +297,25 @@ namespace smt { expr_ref eq(m.mk_eq(c, v), m); assert_expr(eq); vars.push_back(c); - var2orig.insert(c, v); + m_var2orig.insert(c, v); + } + } + for (unsigned i = 0; i < assumptions0.size(); ++i) { + expr* a = assumptions0[i]; + if (is_uninterp_const(a)) { + assumptions.push_back(a); + m_assumption2orig.insert(a, a); + } + else { + if (!pushed) { + pushed = true; + push(); + } + expr_ref c(m.mk_fresh_const("a", m.get_sort(a)), m); + expr_ref eq(m.mk_eq(c, a), m); + assert_expr(eq); + assumptions.push_back(c); + m_assumption2orig.insert(c, a); } } lbool is_sat = check(assumptions.size(), assumptions.c_ptr()); @@ -304,10 +325,9 @@ namespace smt { return is_sat; } - obj_map var2val; index_set _assumptions; for (unsigned i = 0; i < assumptions.size(); ++i) { - _assumptions.insert(get_literal(assumptions[i]).var()); + _assumptions.insert(get_literal(assumptions[i].get()).var()); } model_ref mdl; get_model(mdl); @@ -319,14 +339,14 @@ namespace smt { eval(vars[i].get(), val); if (m.is_value(val)) { trail.push_back(val); - var2val.insert(vars[i].get(), val); + m_var2val.insert(vars[i].get(), val); } else { unfixed.push_back(vars[i].get()); } } unsigned num_units = 0; - extract_fixed_consequences(num_units, var2val, var2orig, _assumptions, conseq); + extract_fixed_consequences(num_units, _assumptions, conseq); app_ref eq(m); TRACE("context", tout << "vars: " << vars.size() << "\n"; @@ -336,8 +356,8 @@ namespace smt { unsigned num_fixed_eqs = 0; unsigned chunk_size = 100; - while (!var2val.empty()) { - obj_map::iterator it = var2val.begin(), end = var2val.end(); + while (!m_var2val.empty()) { + obj_map::iterator it = m_var2val.begin(), end = m_var2val.end(); unsigned num_vars = 0; for (; it != end && num_vars < chunk_size; ++it) { if (get_cancel_flag()) { @@ -387,13 +407,13 @@ namespace smt { m_not_l = null_literal; } if (is_sat == l_true) { - delete_unfixed(var2val, unfixed); + delete_unfixed(unfixed); } - extract_fixed_consequences(num_units, var2val, var2orig, _assumptions, conseq); - num_fixed_eqs += extract_fixed_eqs(var2val, var2orig, conseq); - IF_VERBOSE(1, display_consequence_progress(verbose_stream(), num_iterations, var2val.size(), conseq.size(), + extract_fixed_consequences(num_units, _assumptions, conseq); + num_fixed_eqs += extract_fixed_eqs(conseq); + IF_VERBOSE(1, display_consequence_progress(verbose_stream(), num_iterations, m_var2val.size(), conseq.size(), unfixed.size(), num_fixed_eqs);); - TRACE("context", display_consequence_progress(tout, num_iterations, var2val.size(), conseq.size(), + TRACE("context", display_consequence_progress(tout, num_iterations, m_var2val.size(), conseq.size(), unfixed.size(), num_fixed_eqs);); } diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index 8fd958fb0..4980d32e5 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -1377,14 +1377,17 @@ namespace smt { typedef hashtable index_set; //typedef uint_set index_set; u_map m_antecedents; - void extract_fixed_consequences(literal lit, obj_map& var2val, obj_map const& var2orig, index_set const& assumptions, expr_ref_vector& conseq); - void extract_fixed_consequences(unsigned& idx, obj_map& var2val, obj_map const& var2orig, index_set const& assumptions, expr_ref_vector& conseq); + obj_map m_var2orig; + obj_map m_assumption2orig; + obj_map m_var2val; + void extract_fixed_consequences(literal lit, index_set const& assumptions, expr_ref_vector& conseq); + void extract_fixed_consequences(unsigned& idx, index_set const& assumptions, expr_ref_vector& conseq); void display_consequence_progress(std::ostream& out, unsigned it, unsigned nv, unsigned fixed, unsigned unfixed, unsigned eq); - unsigned delete_unfixed(obj_map& var2val, expr_ref_vector& unfixed); + unsigned delete_unfixed(expr_ref_vector& unfixed); - unsigned extract_fixed_eqs(obj_map& var2val, obj_map const& var2orig, expr_ref_vector& conseq); + unsigned extract_fixed_eqs(expr_ref_vector& conseq); expr_ref antecedent2fml(index_set const& ante); From c637240c4030359c5f16f34e5ff61b435bc080da Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Tue, 25 Apr 2017 16:56:39 -0700 Subject: [PATCH 470/562] parallel verison of ccc Signed-off-by: Nikolaj Bjorner --- src/sat/sat_ccc.cpp | 272 ++++++++++++++++++++++++++------------------ src/sat/sat_ccc.h | 36 ++++-- src/util/queue.h | 14 ++- 3 files changed, 204 insertions(+), 118 deletions(-) diff --git a/src/sat/sat_ccc.cpp b/src/sat/sat_ccc.cpp index eec7e313e..67b7f5e80 100644 --- a/src/sat/sat_ccc.cpp +++ b/src/sat/sat_ccc.cpp @@ -127,6 +127,7 @@ lbool ccc::cube(svector& decisions, lookahead& lh) { lh.push(~l, lh.c_fixed_truth); decisions.back().negate(); decisions.back().m_id = branch2; + decisions.back().m_spawn_id = 0; r = cube(decisions, lh); if (r == l_false) { lh.pop(); @@ -141,6 +142,7 @@ void ccc::update_closure_level(decision const& d, int offset) { m_last_closure_level = (d.m_depth + 3*m_last_closure_level) / 4 + offset; } + unsigned ccc::spawn_conquer(svector const& decisions) { unsigned result = 0; // @@ -152,20 +154,89 @@ unsigned ccc::spawn_conquer(svector const& decisions) { if (!m_free_threads.empty() && m_last_closure_level >= decisions.size()) { result = m_free_threads.back(); m_free_threads.pop_back(); - IF_VERBOSE(0, verbose_stream() << "spawn " << decisions.size() << " " << result << "\n";); + IF_VERBOSE(0, verbose_stream() << "spawn " << result << " with " << decisions.size() << " decisions\n";); } return result; } void ccc::free_conquer(unsigned thread_id) { if (thread_id != 0) { + IF_VERBOSE(0, verbose_stream() << "free conquer " << thread_id << "\n";); m_free_threads.push_back(thread_id); } } +bool ccc::get_solved(svector& decisions) { + // check if CDCL solver got ahead. + bool do_pop = false; + while (true) { + solution sol; + bool is_empty = true; + #pragma omp critical (ccc_solved) + { + if (do_pop) m_solved.pop_front(); + if (!m_solved.empty()) { + sol = m_solved.top(); + is_empty = false; + } + } + if (is_empty) { + return false; + } + do_pop = true; + unsigned branch_id = sol.m_branch_id; + unsigned thread_id = sol.m_thread_id; + free_conquer(thread_id); + for (unsigned i = decisions.size(); i > 0; ) { + --i; + decision& d = decisions[i]; + if (branch_id == d.m_id) { + if (d.m_spawn_id == thread_id && thread_id != 0) { + SASSERT(d.m_spawn_id > 0); + IF_VERBOSE(0, verbose_stream() << thread_id << ": spawn close " << branch_id << " " << " " << d.m_depth << "\n";); + ++m_ccc_stats.m_spawn_closed; + d.close(); + update_closure_level(d, -1); + break; + } + else { + IF_VERBOSE(0, verbose_stream() << thread_id << ": conquer " << branch_id << " " << d.m_depth << " " << decisions.size() << " " << d.get_literal(thread_id) << "\n";); + ++m_ccc_stats.m_cdcl_closed; + update_closure_level(d, 1); + return true; + } + } + // branch is even, d has moved to the next branch + if (branch_id == (d.m_id & ~0x1) && d.m_spawn_id == thread_id && thread_id != 0) { + IF_VERBOSE(0, verbose_stream() << thread_id << ": spawn conquer " << branch_id << " " << " " << d.m_depth << "\n";); + ++m_ccc_stats.m_cdcl_closed; + update_closure_level(d, 1); + return true; + } + } + } +} -lbool ccc::conquer(solver& s, unsigned thread_id) { - SASSERT(thread_id > 0); +void ccc::put_decision(decision const& d) { + for (unsigned i = 0; i < m_num_conquer; ++i) { + #pragma omp critical (ccc_decisions) + { + while (!m_decisions[i].empty()) { + decision d = m_decisions[i].back(); + if (d.m_depth < d.m_depth || d.m_spawn_id != 0) { + break; + } + m_decisions[i].pop_back(); + } + m_decisions[i].push(d); + } + } +} + +// --------------------- +// conquer state machine + +lbool ccc::conquer::search() { try { if (s.inconsistent()) return l_false; s.init_search(); @@ -174,13 +245,11 @@ lbool ccc::conquer(solver& s, unsigned thread_id) { s.cleanup(); s.simplify_problem(); if (s.inconsistent()) return l_false; - - svector decisions; while (true) { SASSERT(!s.inconsistent()); - lbool r = bounded_search(s, decisions, thread_id); + lbool r = bounded_search(); if (r != l_undef) return r; @@ -195,102 +264,47 @@ lbool ccc::conquer(solver& s, unsigned thread_id) { } } -void ccc::replay_decisions(solver& s, svector& decisions, unsigned thread_id) { +void ccc::conquer::replay_decisions() { s.propagate(true); for (unsigned i = s.scope_lvl(); !s.inconsistent() && i < decisions.size(); ++i) { decision const& d = decisions[i]; IF_VERBOSE(2, verbose_stream() << thread_id << ": replay " << d.get_literal(thread_id) << " " << s.value(d.get_literal(thread_id)) << "\n";); - if (!push_decision(s, decisions, d, thread_id)) { + if (!push_decision(d)) { // negation of decision is implied. - // check_non_model("replay", decisions); - decisions.resize(i); - return; + IF_VERBOSE(0, verbose_stream() << thread_id << ": backjump to level " << i << " of " << decisions.size() << "\n";); + while (decisions.size() > i) { + pop_decision(decisions.back()); + decisions.pop_back(); + } + break; + } + + if (d.m_spawn_id == thread_id && d.is_left()) { + // we pushed the right branch on this thread. + IF_VERBOSE(0, verbose_stream() << thread_id << ": skip left branch on level " << i + 1 << " of " << decisions.size() << "\n";); + break; } } } -bool ccc::get_solved(svector& decisions) { - // check if CDCL solver got ahead. - bool found = false; +void ccc::conquer::pop_decision(decision const& d) { + unsigned tid = 0; + if (d.is_spawned(thread_id)) { + tid = thread_id; + m_spawned = false; + IF_VERBOSE(0, verbose_stream() << thread_id << " retire spawn\n";); + } #pragma omp critical (ccc_solved) { - while (!m_solved.empty()) { - solution const& sol = m_solved.top(); - unsigned branch_id = sol.m_branch_id; - unsigned thread_id = sol.m_thread_id; - SASSERT(thread_id > 0); - free_conquer(thread_id); - for (unsigned i = decisions.size(); i > 0; ) { - --i; - decision& d = decisions[i]; - if (branch_id == d.m_id) { - if (d.m_spawn_id == thread_id && thread_id != 0) { - SASSERT(d.m_spawn_id > 0); - IF_VERBOSE(0, verbose_stream() << "spawn close " << branch_id << " " << thread_id << " " << d.m_depth << "\n";); - ++m_ccc_stats.m_spawn_closed; - d.close(); - update_closure_level(d, -1); - } - else { - IF_VERBOSE(0, verbose_stream() << "conquer " << branch_id << " " << thread_id << " " << d.m_depth << " " << d.get_literal(thread_id) << "\n";); - found = true; - ++m_ccc_stats.m_cdcl_closed; - update_closure_level(d, 1); - } - break; - } - // branch is even, d has moved to the next branch - if (branch_id == (d.m_id & ~0x1) && d.m_spawn_id == thread_id && thread_id != 0) { - IF_VERBOSE(0, verbose_stream() << "spawn conquer " << branch_id << " " << thread_id << " " << d.m_depth << "\n";); - found = true; - ++m_ccc_stats.m_cdcl_closed; - update_closure_level(d, 1); - break; - } - } - if (found) { - break; - } - // IF_VERBOSE(1, verbose_stream() << "not found: " << branch_id << " " << decisions.size() << "\n";); - m_solved.pop(); - } - } - - return found; -} - -void ccc::put_decision(decision const& d) { - #pragma omp critical (ccc_decisions) - { - for (unsigned i = 0; i < m_num_conquer; ++i) { - m_decisions[i].push(d); - } + super.m_solved.push(solution(tid, d.m_id)); } } -bool ccc::get_decision(unsigned thread_id, decision& d) { - SASSERT(0 < thread_id && thread_id <= m_decisions.size()); - bool result = false; - #pragma omp critical (ccc_decisions) - { - if (!m_decisions[thread_id - 1].empty()) { - d = m_decisions[thread_id - 1].pop(); - result = true; - } - } - return result; -} - -bool ccc::push_decision(solver& s, svector const& decisions, decision const& d, unsigned thread_id) { +bool ccc::conquer::push_decision(decision const& d) { literal lit = d.get_literal(thread_id); switch (s.value(lit)) { case l_false: - thread_id = (d.m_spawn_id == thread_id || (!decisions.empty() && decisions.back().m_spawn_id == thread_id)) ? thread_id : 0; - #pragma omp critical (ccc_solved) - { - m_solved.push(solution(thread_id, d.m_id)); - } //TBD: s.m_restart_threshold = s.m_config.m_restart_initial; //s.m_conflicts_since_last_restart = 0; @@ -304,62 +318,90 @@ bool ccc::push_decision(solver& s, svector const& decisions, decision s.propagate(true); break; } + m_spawned |= d.is_spawned(thread_id); return true; } -bool ccc::cube_decision(solver& s, svector& decisions, unsigned thread_id) { +bool ccc::conquer::cube_decision() { decision d; bool use_cube_decision = false; SASSERT(s.m_qhead == s.m_trail.size()); - get_cube: - if (!get_decision(thread_id, d)) { - return false; - } - - if (!decisions.empty() && decisions.back().m_depth + 1 < d.m_depth) { - goto get_cube; - } + while (true) { + if (!super.get_decision(thread_id, d)) { + return false; + } - if (!decisions.empty() && decisions.back().m_spawn_id == thread_id && decisions.back().m_depth < d.m_depth) { - goto get_cube; + if (d.is_spawned(thread_id)) IF_VERBOSE(0, verbose_stream() << thread_id << ": spawned d:" << d.m_depth << " decisions: " << decisions.size() << "\n";); + + if (!decisions.empty() && decisions.back().m_depth + 1 < d.m_depth) { + if (d.is_spawned(thread_id)) { + pop_decision(d); + } + } + else { + break; + } + } + SASSERT(decisions.empty() || decisions.back().m_depth + 1 >= d.m_depth); + + if (!decisions.empty() && decisions.back().is_spawned(thread_id) && decisions.back().m_depth == d.m_depth) { + SASSERT(d.m_spawn_id == 0); + SASSERT(decisions.back().is_left()); + SASSERT(!d.is_left()); + IF_VERBOSE(0, verbose_stream() << thread_id << " inherit spawn\n";); + d.m_spawn_id = thread_id; + decisions.back().m_spawn_id = 0; + m_spawned = false; } + SASSERT(decisions.empty() || decisions.back().m_depth + 1 >= d.m_depth); while (!decisions.empty() && decisions.back().m_depth >= d.m_depth) { // check_non_model("cube decision", decisions); + if (decisions.back().is_spawned(thread_id)) { + pop_decision(decisions.back()); + } decisions.pop_back(); } SASSERT(decisions.empty() || decisions.back().m_depth + 1 == d.m_depth); SASSERT(decisions.empty() || decisions.back().m_id == d.m_parent); + if (m_spawned) { + decisions.push_back(d); + return true; + } + s.pop_reinit(s.scope_lvl() - decisions.size()); SASSERT(s.m_qhead == s.m_trail.size()); SASSERT(s.scope_lvl() == decisions.size()); literal lit = d.get_literal(thread_id); - IF_VERBOSE(1, verbose_stream() << thread_id << ": cube " << decisions.size() << " " << d.get_literal(thread_id) << "\n";); + IF_VERBOSE(0, verbose_stream() << thread_id << ": cube " << decisions.size() << " " << d.get_literal(thread_id) << "\n";); IF_VERBOSE(2, pp(verbose_stream() << thread_id << ": push ", decisions) << " @ " << s.scope_lvl() << " " << s.value(lit) << "\n"; if (s.value(lit) == l_false) verbose_stream() << "level: " << s.lvl(lit) << "\n";); - if (push_decision(s, decisions, d, thread_id)) { + if (push_decision(d)) { decisions.push_back(d); } + else { + pop_decision(d); + } return true; } -lbool ccc::bounded_search(solver& s, svector& decisions, unsigned thread_id) { +lbool ccc::conquer::bounded_search() { while (true) { s.checkpoint(); bool done = false; while (!done) { - replay_decisions(s, decisions, thread_id); + replay_decisions(); lbool is_sat = s.propagate_and_backjump_step(done); if (is_sat != l_true) return is_sat; } s.gc(); - if (!cube_decision(s, decisions, thread_id) && !s.decide()) { + if (!cube_decision() && !s.decide()) { lbool is_sat = s.final_check(); if (is_sat != l_undef) { return is_sat; @@ -369,6 +411,20 @@ lbool ccc::bounded_search(solver& s, svector& decisions, unsigned thre } +bool ccc::get_decision(unsigned thread_id, decision& d) { + SASSERT(0 < thread_id && thread_id <= m_decisions.size()); + bool result = false; + #pragma omp critical (ccc_decisions) + { + if (!m_decisions[thread_id - 1].empty()) { + d = m_decisions[thread_id - 1].pop_front(); + result = true; + } + } + return result; +} + + lbool ccc::search() { enum par_exception_kind { DEFAULT_EX, @@ -381,7 +437,7 @@ lbool ccc::search() { scoped_limits scoped_rlimit(m_s.rlimit()); vector limits; - ptr_vector solvers; + ptr_vector solvers; int finished_id = -1; std::string ex_msg; par_exception_kind ex_kind; @@ -395,10 +451,10 @@ lbool ccc::search() { for (int i = 1; i < num_threads; ++i) { limits.push_back(reslimit()); m_s.m_params.set_uint("random_seed", m_s.m_rand()); - solver* s1 = alloc(sat::solver, m_s.m_params, limits.back()); + conquer* s1 = alloc(conquer, *this, m_s.m_params, limits.back(), i); solvers.push_back(s1); - s1->copy(m_s); - scoped_rlimit.push_child(&s1->rlimit()); + s1->s.copy(m_s); + scoped_rlimit.push_child(&(s1->s.rlimit())); m_decisions.push_back(queue()); } for (unsigned i = 1; i < m_num_conquer; ++i) { @@ -413,7 +469,7 @@ lbool ccc::search() { r = cube(); } else { - r = conquer(*solvers[i-1], i); + r = solvers[i-1]->search(); } bool first = false; #pragma omp critical (par_solver) @@ -426,7 +482,7 @@ lbool ccc::search() { } if (first) { for (unsigned j = 0; j < solvers.size(); ++j) { - solvers[j]->rlimit().cancel(); + solvers[j]->s.rlimit().cancel(); } // cancel lookahead solver: m_cancel = true; @@ -444,11 +500,11 @@ lbool ccc::search() { if (finished_id > 0 && result == l_true) { // set model from auxiliary solver - m_model = solvers[finished_id - 1]->get_model(); + m_model = solvers[finished_id - 1]->s.get_model(); } for (unsigned i = 0; i < solvers.size(); ++i) { - solvers[i]->collect_statistics(m_lh_stats); + solvers[i]->s.collect_statistics(m_lh_stats); dealloc(solvers[i]); } diff --git a/src/sat/sat_ccc.h b/src/sat/sat_ccc.h index 29bf3c18d..905a64d93 100644 --- a/src/sat/sat_ccc.h +++ b/src/sat/sat_ccc.h @@ -42,7 +42,14 @@ namespace sat { void close() { SASSERT(m_spawn_id > 0); m_spawn_id = -m_spawn_id; } bool is_closed() const { return m_spawn_id < 0; } void negate() { m_literal.neg(); } - literal get_literal(unsigned thread_id) const { return thread_id == m_spawn_id ? ~m_literal : m_literal; } + bool is_left() const { return 0 == (m_id & 0x1); } + bool is_spawned(unsigned thread_id) const { return m_spawn_id == thread_id; } + + // the left branch has an even branch_id. + // the branch is spawned if it is even and the spawn_id is the same as the thread_id, and furthermore it is exploring the left branch. + // it may explore the right branch, but is then not in a spawned mode. + // we retain the spawn id so that the spawned thread can be re-spun. + literal get_literal(unsigned thread_id) const { return ((m_id & 0x1) == 0 && thread_id == m_spawn_id) ? ~m_literal : m_literal; } std::ostream& pp(std::ostream& out) const; }; @@ -50,6 +57,7 @@ namespace sat { unsigned m_thread_id; unsigned m_branch_id; solution(unsigned t, unsigned s): m_thread_id(t), m_branch_id(s) {} + solution(): m_thread_id(0), m_branch_id(0) {} }; struct stats { @@ -61,6 +69,22 @@ namespace sat { } }; + struct conquer { + ccc& super; + solver s; + svector decisions; + unsigned thread_id; + bool m_spawned; + conquer(ccc& super, params_ref const& p, reslimit& l, unsigned tid): super(super), s(p, l), thread_id(tid), m_spawned(false) {} + + lbool search(); + bool cube_decision(); + lbool bounded_search(); + bool push_decision(decision const& d); + void pop_decision(decision const& d); + void replay_decisions(); + }; + solver& m_s; queue m_solved; vector > m_decisions; @@ -72,21 +96,15 @@ namespace sat { unsigned m_last_closure_level; ::statistics m_lh_stats; stats m_ccc_stats; - - lbool conquer(solver& s, unsigned thread_id); - bool cube_decision(solver& s, svector& decisions, unsigned thread_id); - - lbool bounded_search(solver& s, svector& decisions, unsigned thread_id); - bool push_decision(solver& s, svector const& decisions, decision const& d, unsigned thread_id); + lbool cube(); lbool cube(svector& decisions, lookahead& lh); void put_decision(decision const& d); - bool get_decision(unsigned thread_id, decision& d); bool get_solved(svector& decisions); + bool get_decision(unsigned thread_id, decision& d); void update_closure_level(decision const& d, int offset); - void replay_decisions(solver& s, svector& decisions, unsigned thread_id); static std::ostream& pp(std::ostream& out, svector const& v); diff --git a/src/util/queue.h b/src/util/queue.h index 4b85f53f0..a517efc24 100644 --- a/src/util/queue.h +++ b/src/util/queue.h @@ -41,7 +41,7 @@ public: return m_elems[m_head]; } - T pop() { + T pop_front() { SASSERT(!empty()); m_capacity = std::max(m_capacity, m_elems.size()); SASSERT(m_head < m_elems.size()); @@ -55,6 +55,18 @@ public: return m_elems[m_head++]; } + + T back() const { + return m_elems[m_elems.size() - 1]; + } + + T pop_back() { + SASSERT(!empty()); + SASSERT(m_head < m_elems.size()); + T result = back(); + m_elems.shrink(m_elems.size() - 1); + return result; + } }; #endif From 4575b2820d25f6add9d8a159fee2c9dbe9d1ced8 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Wed, 26 Apr 2017 00:22:59 -0700 Subject: [PATCH 471/562] parallelizing lh Signed-off-by: Nikolaj Bjorner --- src/sat/sat_ccc.cpp | 198 ++++++++++++++++++++++++-------------------- src/sat/sat_ccc.h | 35 +++++--- 2 files changed, 128 insertions(+), 105 deletions(-) diff --git a/src/sat/sat_ccc.cpp b/src/sat/sat_ccc.cpp index 67b7f5e80..025bbafd4 100644 --- a/src/sat/sat_ccc.cpp +++ b/src/sat/sat_ccc.cpp @@ -33,53 +33,36 @@ Notes: using namespace sat; -std::ostream& ccc::decision::pp(std::ostream& out) const { - out << "(" - << "id:" << m_id - << " l:" << m_literal - << " d:" << m_depth; - if (m_spawn_id != 0) { - out << " s:" << m_spawn_id; - } - out << ") "; - return out; -} +// ------------ +// cuber -std::ostream& ccc::pp(std::ostream& out, svector const& v) { - for (unsigned i = 0; i < v.size(); ++i) { - v[i].pp(out); - } - return out; -} +ccc::cuber::cuber(ccc& c): m_ccc(c), lh(c.m_s), m_branch_id(0) {} -lbool ccc::cube() { +lbool ccc::cuber::search() { m_branch_id = 0; m_last_closure_level = 1000; - lookahead lh(m_s); lh.init_search(); lh.m_model.reset(); lookahead::scoped_level _sl(lh, lh.c_fixed_truth); - literal_vector trail; - svector decisions; lh.m_search_mode = lookahead_mode::searching; - lbool r = cube(decisions, lh); + lbool r = research(); if (r == l_true) { - m_model = lh.get_model(); + m_ccc.m_model = lh.get_model(); } - lh.collect_statistics(m_lh_stats); + lh.collect_statistics(m_ccc.m_lh_stats); return r; } -lbool ccc::cube(svector& decisions, lookahead& lh) { - m_s.checkpoint(); +lbool ccc::cuber::research() { + m_ccc.m_s.checkpoint(); if (lh.inconsistent()) { return l_false; } - if (get_solved(decisions)) { + if (get_solved()) { return l_false; } @@ -94,41 +77,47 @@ lbool ccc::cube(svector& decisions, lookahead& lh) { } if (!decisions.empty()) { - put_decision(decisions.back()); + m_ccc.put_decision(decisions.back()); } // update trail and decisions ++lh.m_stats.m_decisions; unsigned parent_id = decisions.empty() ? 0 : decisions.back().m_id; - unsigned spawn_id = spawn_conquer(decisions); + unsigned spawn_id = spawn_conquer(); unsigned branch1 = m_branch_id++; unsigned branch2 = m_branch_id++; decision d(branch1, decisions.size() + 1, l, parent_id, spawn_id); decisions.push_back(d); + IF_VERBOSE(1, d.pp(verbose_stream() << "select " << m_last_closure_level << " ") << "\n";); IF_VERBOSE(1, verbose_stream() << "select " << pp_prefix(lh.m_prefix, lh.m_trail_lim.size()) << ": " << l << " " << lh.m_trail.size() << "\n";); IF_VERBOSE(2, pp(verbose_stream(), decisions) << "\n"; ); TRACE("sat", tout << "choose: " << l << "\n";); lh.push(l, lh.c_fixed_truth); - lbool r = cube(decisions, lh); + lbool r = research(); if (r == l_false) { lh.pop(); if (decisions.back().is_closed()) { // branch was solved by a spawned conquer process - IF_VERBOSE(0, verbose_stream() << "closed " << decisions.back().m_id << "\n";); + IF_VERBOSE(1, decisions.back().pp(verbose_stream() << "closed ") << "\n";); r = l_false; decisions.pop_back(); } else { + if (spawn_id > 0) { + free_conquer(spawn_id); + m_last_closure_level *= 3; + m_last_closure_level /= 4; + } lh.inc_istamp(); lh.flip_prefix(); lh.push(~l, lh.c_fixed_truth); decisions.back().negate(); decisions.back().m_id = branch2; decisions.back().m_spawn_id = 0; - r = cube(decisions, lh); + r = research(); if (r == l_false) { lh.pop(); decisions.pop_back(); @@ -138,45 +127,50 @@ lbool ccc::cube(svector& decisions, lookahead& lh) { return r; } -void ccc::update_closure_level(decision const& d, int offset) { - m_last_closure_level = (d.m_depth + 3*m_last_closure_level) / 4 + offset; +void ccc::cuber::update_closure_level(decision const& d, int offset) { + m_last_closure_level = (d.m_depth + 3*m_last_closure_level) / 4; + if (m_last_closure_level >= static_cast(abs(offset))) { + m_last_closure_level += offset; + } } - -unsigned ccc::spawn_conquer(svector const& decisions) { +unsigned ccc::cuber::spawn_conquer() { unsigned result = 0; // // decisions must have been solved at a higher level by a conquer thread // - if (m_ccc_stats.m_cdcl_closed < 10) { - return 0; - } - if (!m_free_threads.empty() && m_last_closure_level >= decisions.size()) { - result = m_free_threads.back(); - m_free_threads.pop_back(); - IF_VERBOSE(0, verbose_stream() << "spawn " << result << " with " << decisions.size() << " decisions\n";); + if (!m_free_threads.empty()) { + if (m_last_closure_level <= decisions.size()) { + result = m_free_threads.back(); + ++m_ccc.m_ccc_stats.m_spawn_opened; + m_free_threads.pop_back(); + } + else { + IF_VERBOSE(1, verbose_stream() << m_last_closure_level << " " << decisions.size() << "\n";); + } } return result; } -void ccc::free_conquer(unsigned thread_id) { - if (thread_id != 0) { - IF_VERBOSE(0, verbose_stream() << "free conquer " << thread_id << "\n";); - m_free_threads.push_back(thread_id); + +void ccc::cuber::free_conquer(unsigned id) { + if (id != 0) { + m_free_threads.push_back(id); } } -bool ccc::get_solved(svector& decisions) { + +bool ccc::cuber::get_solved() { // check if CDCL solver got ahead. bool do_pop = false; + solution sol; while (true) { - solution sol; bool is_empty = true; #pragma omp critical (ccc_solved) { - if (do_pop) m_solved.pop_front(); - if (!m_solved.empty()) { - sol = m_solved.top(); + if (do_pop) m_ccc.m_solved.pop_front(); + if (!m_ccc.m_solved.empty()) { + sol = m_ccc.m_solved.top(); is_empty = false; } } @@ -186,30 +180,31 @@ bool ccc::get_solved(svector& decisions) { do_pop = true; unsigned branch_id = sol.m_branch_id; unsigned thread_id = sol.m_thread_id; - free_conquer(thread_id); + bool found = false; for (unsigned i = decisions.size(); i > 0; ) { --i; decision& d = decisions[i]; if (branch_id == d.m_id) { if (d.m_spawn_id == thread_id && thread_id != 0) { SASSERT(d.m_spawn_id > 0); - IF_VERBOSE(0, verbose_stream() << thread_id << ": spawn close " << branch_id << " " << " " << d.m_depth << "\n";); - ++m_ccc_stats.m_spawn_closed; + IF_VERBOSE(1, d.pp(verbose_stream() << thread_id << ": spawn close ") << "\n";); + ++m_ccc.m_ccc_stats.m_spawn_closed; d.close(); + free_conquer(thread_id); update_closure_level(d, -1); break; } else { - IF_VERBOSE(0, verbose_stream() << thread_id << ": conquer " << branch_id << " " << d.m_depth << " " << decisions.size() << " " << d.get_literal(thread_id) << "\n";); - ++m_ccc_stats.m_cdcl_closed; + IF_VERBOSE(1, d.pp(verbose_stream() << thread_id << ": conquer ") << "\n";); + ++m_ccc.m_ccc_stats.m_cdcl_closed; update_closure_level(d, 1); return true; - } + } } // branch is even, d has moved to the next branch if (branch_id == (d.m_id & ~0x1) && d.m_spawn_id == thread_id && thread_id != 0) { - IF_VERBOSE(0, verbose_stream() << thread_id << ": spawn conquer " << branch_id << " " << " " << d.m_depth << "\n";); - ++m_ccc_stats.m_cdcl_closed; + IF_VERBOSE(1, d.pp(verbose_stream() << thread_id << ": spawn conquer ") << "\n";); + ++m_ccc.m_ccc_stats.m_cdcl_closed; update_closure_level(d, 1); return true; } @@ -217,22 +212,6 @@ bool ccc::get_solved(svector& decisions) { } } -void ccc::put_decision(decision const& d) { - for (unsigned i = 0; i < m_num_conquer; ++i) { - #pragma omp critical (ccc_decisions) - { - while (!m_decisions[i].empty()) { - decision d = m_decisions[i].back(); - if (d.m_depth < d.m_depth || d.m_spawn_id != 0) { - break; - } - m_decisions[i].pop_back(); - } - m_decisions[i].push(d); - } - } -} - // --------------------- // conquer state machine @@ -272,7 +251,7 @@ void ccc::conquer::replay_decisions() { if (!push_decision(d)) { // negation of decision is implied. - IF_VERBOSE(0, verbose_stream() << thread_id << ": backjump to level " << i << " of " << decisions.size() << "\n";); + IF_VERBOSE(1, d.pp(verbose_stream() << thread_id << ": backjump to level " << i << " ") << "\n";); while (decisions.size() > i) { pop_decision(decisions.back()); decisions.pop_back(); @@ -282,7 +261,7 @@ void ccc::conquer::replay_decisions() { if (d.m_spawn_id == thread_id && d.is_left()) { // we pushed the right branch on this thread. - IF_VERBOSE(0, verbose_stream() << thread_id << ": skip left branch on level " << i + 1 << " of " << decisions.size() << "\n";); + IF_VERBOSE(1, d.pp(verbose_stream() << thread_id << ": skip left branch on level " << i + 1 << " ") << "\n";); break; } } @@ -293,11 +272,11 @@ void ccc::conquer::pop_decision(decision const& d) { if (d.is_spawned(thread_id)) { tid = thread_id; m_spawned = false; - IF_VERBOSE(0, verbose_stream() << thread_id << " retire spawn\n";); + IF_VERBOSE(1, d.pp(verbose_stream() << thread_id << ": retire spawn ") << "\n";); } #pragma omp critical (ccc_solved) { - super.m_solved.push(solution(tid, d.m_id)); + m_ccc.m_solved.push(solution(tid, d.m_id)); } } @@ -328,11 +307,11 @@ bool ccc::conquer::cube_decision() { SASSERT(s.m_qhead == s.m_trail.size()); while (true) { - if (!super.get_decision(thread_id, d)) { + if (!m_ccc.get_decision(thread_id, d)) { return false; } - if (d.is_spawned(thread_id)) IF_VERBOSE(0, verbose_stream() << thread_id << ": spawned d:" << d.m_depth << " decisions: " << decisions.size() << "\n";); + if (d.is_spawned(thread_id)) IF_VERBOSE(1, d.pp(verbose_stream() << thread_id << " ") << "\n";); if (!decisions.empty() && decisions.back().m_depth + 1 < d.m_depth) { if (d.is_spawned(thread_id)) { @@ -349,8 +328,7 @@ bool ccc::conquer::cube_decision() { SASSERT(d.m_spawn_id == 0); SASSERT(decisions.back().is_left()); SASSERT(!d.is_left()); - IF_VERBOSE(0, verbose_stream() << thread_id << " inherit spawn\n";); - d.m_spawn_id = thread_id; + IF_VERBOSE(1, verbose_stream() << thread_id << " inherit spawn\n";); decisions.back().m_spawn_id = 0; m_spawned = false; } @@ -375,7 +353,7 @@ bool ccc::conquer::cube_decision() { SASSERT(s.m_qhead == s.m_trail.size()); SASSERT(s.scope_lvl() == decisions.size()); literal lit = d.get_literal(thread_id); - IF_VERBOSE(0, verbose_stream() << thread_id << ": cube " << decisions.size() << " " << d.get_literal(thread_id) << "\n";); + IF_VERBOSE(1, d.pp(verbose_stream() << thread_id << ": cube ") << "\n";); IF_VERBOSE(2, pp(verbose_stream() << thread_id << ": push ", decisions) << " @ " << s.scope_lvl() << " " << s.value(lit) << "\n"; if (s.value(lit) == l_false) verbose_stream() << "level: " << s.lvl(lit) << "\n";); @@ -410,6 +388,27 @@ lbool ccc::conquer::bounded_search() { } } +// -------------- +// shared state + +std::ostream& ccc::decision::pp(std::ostream& out) const { + out << "(" + << "id:" << m_id + << " l:" << m_literal + << " d:" << m_depth; + if (m_spawn_id != 0) { + out << " s:" << m_spawn_id; + } + out << ") "; + return out; +} + +std::ostream& ccc::pp(std::ostream& out, svector const& v) { + for (unsigned i = 0; i < v.size(); ++i) { + v[i].pp(out); + } + return out; +} bool ccc::get_decision(unsigned thread_id, decision& d) { SASSERT(0 < thread_id && thread_id <= m_decisions.size()); @@ -424,6 +423,21 @@ bool ccc::get_decision(unsigned thread_id, decision& d) { return result; } +void ccc::put_decision(decision const& d) { + for (unsigned i = 0; i < m_num_conquer; ++i) { + #pragma omp critical (ccc_decisions) + { + while (false && !m_decisions[i].empty()) { // introduces contention. + decision d = m_decisions[i].back(); + if (d.m_depth < d.m_depth || d.m_spawn_id != 0) { + break; + } + m_decisions[i].pop_back(); + } + m_decisions[i].push(d); + } + } +} lbool ccc::search() { enum par_exception_kind { @@ -436,7 +450,6 @@ lbool ccc::search() { m_cancel = false; scoped_limits scoped_rlimit(m_s.rlimit()); - vector limits; ptr_vector solvers; int finished_id = -1; std::string ex_msg; @@ -445,20 +458,21 @@ lbool ccc::search() { lbool result = l_undef; m_decisions.reset(); + cuber cuber(*this); + m_num_conquer = m_s.m_config.m_num_threads; int num_threads = 1 + m_num_conquer; // for ccc-infinity only two threads. for (int i = 1; i < num_threads; ++i) { - limits.push_back(reslimit()); m_s.m_params.set_uint("random_seed", m_s.m_rand()); - conquer* s1 = alloc(conquer, *this, m_s.m_params, limits.back(), i); + conquer* s1 = alloc(conquer, *this, m_s.m_params, i); solvers.push_back(s1); s1->s.copy(m_s); - scoped_rlimit.push_child(&(s1->s.rlimit())); + scoped_rlimit.push_child(&(s1->m_limit)); m_decisions.push_back(queue()); } for (unsigned i = 1; i < m_num_conquer; ++i) { - m_free_threads.push_back(i); + cuber.m_free_threads.push_back(i); } #pragma omp parallel for @@ -466,7 +480,7 @@ lbool ccc::search() { try { lbool r = l_undef; if (i == 0) { - r = cube(); + r = cuber.search(); } else { r = solvers[i-1]->search(); @@ -482,7 +496,7 @@ lbool ccc::search() { } if (first) { for (unsigned j = 0; j < solvers.size(); ++j) { - solvers[j]->s.rlimit().cancel(); + solvers[j]->m_limit.cancel(); } // cancel lookahead solver: m_cancel = true; diff --git a/src/sat/sat_ccc.h b/src/sat/sat_ccc.h index 905a64d93..30c8a3229 100644 --- a/src/sat/sat_ccc.h +++ b/src/sat/sat_ccc.h @@ -62,6 +62,7 @@ namespace sat { struct stats { unsigned m_spawn_closed; + unsigned m_spawn_opened; unsigned m_cdcl_closed; stats() { reset(); } void reset() { @@ -70,12 +71,13 @@ namespace sat { }; struct conquer { - ccc& super; + reslimit m_limit; + ccc& m_ccc; solver s; svector decisions; unsigned thread_id; bool m_spawned; - conquer(ccc& super, params_ref const& p, reslimit& l, unsigned tid): super(super), s(p, l), thread_id(tid), m_spawned(false) {} + conquer(ccc& super, params_ref const& p, unsigned tid): m_ccc(super), s(p, m_limit), thread_id(tid), m_spawned(false) {} lbool search(); bool cube_decision(); @@ -85,27 +87,35 @@ namespace sat { void replay_decisions(); }; + struct cuber { + ccc& m_ccc; + lookahead lh; + unsigned m_branch_id; + unsigned m_last_closure_level; + unsigned_vector m_free_threads; + svector decisions; + + cuber(ccc& c); + lbool search(); + lbool research(); + bool get_solved(); + void update_closure_level(decision const& d, int offset); + unsigned spawn_conquer(); + void free_conquer(unsigned thread_id); + }; + solver& m_s; queue m_solved; vector > m_decisions; unsigned m_num_conquer; model m_model; volatile bool m_cancel; - unsigned m_branch_id; - unsigned_vector m_free_threads; - unsigned m_last_closure_level; ::statistics m_lh_stats; stats m_ccc_stats; - lbool cube(); - lbool cube(svector& decisions, lookahead& lh); void put_decision(decision const& d); - bool get_solved(svector& decisions); bool get_decision(unsigned thread_id, decision& d); - void update_closure_level(decision const& d, int offset); - - static std::ostream& pp(std::ostream& out, svector const& v); void push_model(unsigned v, bool sign); @@ -114,8 +124,6 @@ namespace sat { void check_non_model(char const* fn, svector const& decisions); - unsigned spawn_conquer(svector const& decisions); - void free_conquer(unsigned thread_id); public: @@ -129,6 +137,7 @@ namespace sat { st.copy(m_lh_stats); st.update("ccc-spawn-closed", m_ccc_stats.m_spawn_closed); st.update("ccc-cdcl-closed", m_ccc_stats.m_cdcl_closed); + st.update("ccc-spawn-opened", m_ccc_stats.m_spawn_opened); } }; } From 5f7ae920c6758f1b359fc1827b8492b750243e4b Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Mon, 24 Apr 2017 22:25:51 +0100 Subject: [PATCH 472/562] [Doxygen] Teach `mk_api_doc.py` a new command line option (`--temp-dir`) which allows the location of the temporary directory to be controlled. While I'm here also write `website.dox` into the temporary directory where it belongs instead of in the source tree and simplify the logic that deletes the temporary directory and its contents. --- doc/mk_api_doc.py | 61 ++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/doc/mk_api_doc.py b/doc/mk_api_doc.py index 62130d73c..a8ee1e220 100644 --- a/doc/mk_api_doc.py +++ b/doc/mk_api_doc.py @@ -16,9 +16,10 @@ import shutil ML_ENABLED=False BUILD_DIR='../build' DOXYGEN_EXE='doxygen' +TEMP_DIR=os.path.join(os.getcwd(), 'tmp') def parse_options(): - global ML_ENABLED, BUILD_DIR, DOXYGEN_EXE + global ML_ENABLED, BUILD_DIR, DOXYGEN_EXE, TEMP_DIR parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('-b', '--build', @@ -35,10 +36,17 @@ def parse_options(): default=DOXYGEN_EXE, help='Doxygen executable to use (default: %(default)s)', ) + parser.add_argument('--temp-dir', + dest='temp_dir', + default=TEMP_DIR, + help='Path to directory to use as temporary directory. ' + '(default: %(default)s)', + ) pargs = parser.parse_args() ML_ENABLED = pargs.ml BUILD_DIR = pargs.build DOXYGEN_EXE = pargs.doxygen_executable + TEMP_DIR = pargs.temp_dir return def mk_dir(d): @@ -60,8 +68,14 @@ def cleanup_API(inf, outf): try: parse_options() + print("Creating temporary directory \"{}\"".format(TEMP_DIR)) + mk_dir(TEMP_DIR) + # Short-hand for path to temporary file + def temp_path(path): + return os.path.join(TEMP_DIR, path) + fi = open('website.dox', 'r') - fo = open('website-adj.dox', 'w') + fo = open(temp_path('website.dox'), 'w') for line in fi: if (line != '[ML]\n'): @@ -71,20 +85,18 @@ try: fi.close() fo.close() + mk_dir('api/html') - mk_dir('tmp') - shutil.copyfile('website-adj.dox', 'tmp/website.dox') - os.remove('website-adj.dox') - shutil.copyfile('../src/api/python/z3/z3.py', 'tmp/z3py.py') - cleanup_API('../src/api/z3_api.h', 'tmp/z3_api.h') - cleanup_API('../src/api/z3_ast_containers.h', 'tmp/z3_ast_containers.h') - cleanup_API('../src/api/z3_algebraic.h', 'tmp/z3_algebraic.h') - cleanup_API('../src/api/z3_polynomial.h', 'tmp/z3_polynomial.h') - cleanup_API('../src/api/z3_rcf.h', 'tmp/z3_rcf.h') - cleanup_API('../src/api/z3_fixedpoint.h', 'tmp/z3_fixedpoint.h') - cleanup_API('../src/api/z3_optimization.h', 'tmp/z3_optimization.h') - cleanup_API('../src/api/z3_interp.h', 'tmp/z3_interp.h') - cleanup_API('../src/api/z3_fpa.h', 'tmp/z3_fpa.h') + shutil.copyfile('../src/api/python/z3/z3.py', temp_path('z3py.py')) + cleanup_API('../src/api/z3_api.h', temp_path('z3_api.h')) + cleanup_API('../src/api/z3_ast_containers.h', temp_path('z3_ast_containers.h')) + cleanup_API('../src/api/z3_algebraic.h', temp_path('z3_algebraic.h')) + cleanup_API('../src/api/z3_polynomial.h', temp_path('z3_polynomial.h')) + cleanup_API('../src/api/z3_rcf.h', temp_path('z3_rcf.h')) + cleanup_API('../src/api/z3_fixedpoint.h', temp_path('z3_fixedpoint.h')) + cleanup_API('../src/api/z3_optimization.h', temp_path('z3_optimization.h')) + cleanup_API('../src/api/z3_interp.h', temp_path('z3_interp.h')) + cleanup_API('../src/api/z3_fpa.h', temp_path('z3_fpa.h')) print("Removed annotations from z3_api.h.") try: @@ -95,23 +107,8 @@ try: print("ERROR: failed to execute 'doxygen', make sure doxygen (http://www.doxygen.org) is available in your system.") exit(1) print("Generated C and .NET API documentation.") - os.remove('tmp/z3_api.h') - os.remove('tmp/z3_ast_containers.h') - os.remove('tmp/z3_algebraic.h') - os.remove('tmp/z3_polynomial.h') - os.remove('tmp/z3_rcf.h') - os.remove('tmp/z3_fixedpoint.h') - os.remove('tmp/z3_optimization.h') - os.remove('tmp/z3_interp.h') - os.remove('tmp/z3_fpa.h') - print("Removed temporary file header files.") - - os.remove('tmp/website.dox') - print("Removed temporary file website.dox") - os.remove('tmp/z3py.py') - print("Removed temporary file z3py.py") - os.removedirs('tmp') - print("Removed temporary directory tmp.") + shutil.rmtree(os.path.realpath(TEMP_DIR)) + print("Removed temporary directory \"{}\"".format(TEMP_DIR)) sys.path.append('../src/api/python/z3') pydoc.writedoc('z3') shutil.move('z3.html', 'api/html/z3.html') From b4f8b001cecb9a341ddacb4e590b1dcaa01889fd Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Mon, 24 Apr 2017 22:55:41 +0100 Subject: [PATCH 473/562] [Doxygen] Teach `mk_api_doc.py` a new command line option (`--output-dir`) to control where output files are emitted. This is implemented by making `z3api.dox` a template file (renamed `z3api.cfg.in`) and populating the template at build time with the required settings. --- doc/mk_api_doc.py | 64 +++++++++++++++++++++++++++++---- doc/{z3api.dox => z3api.cfg.in} | 2 +- 2 files changed, 58 insertions(+), 8 deletions(-) rename doc/{z3api.dox => z3api.cfg.in} (99%) diff --git a/doc/mk_api_doc.py b/doc/mk_api_doc.py index a8ee1e220..b4a58f392 100644 --- a/doc/mk_api_doc.py +++ b/doc/mk_api_doc.py @@ -17,9 +17,10 @@ ML_ENABLED=False BUILD_DIR='../build' DOXYGEN_EXE='doxygen' TEMP_DIR=os.path.join(os.getcwd(), 'tmp') +OUTPUT_DIRECTORY=os.path.join(os.getcwd(), 'api') def parse_options(): - global ML_ENABLED, BUILD_DIR, DOXYGEN_EXE, TEMP_DIR + global ML_ENABLED, BUILD_DIR, DOXYGEN_EXE, TEMP_DIR, OUTPUT_DIRECTORY parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('-b', '--build', @@ -42,11 +43,17 @@ def parse_options(): help='Path to directory to use as temporary directory. ' '(default: %(default)s)', ) + parser.add_argument('--output-dir', + dest='output_dir', + default=OUTPUT_DIRECTORY, + help='Path to output directory (default: %(default)s)', + ) pargs = parser.parse_args() ML_ENABLED = pargs.ml BUILD_DIR = pargs.build DOXYGEN_EXE = pargs.doxygen_executable TEMP_DIR = pargs.temp_dir + OUTPUT_DIRECTORY = pargs.output_dir return def mk_dir(d): @@ -65,6 +72,41 @@ def cleanup_API(inf, outf): if not pat1.match(line) and not pat2.match(line) and not pat3.match(line): _outf.write(line) +def configure_file(template_file_path, output_file_path, substitutions): + """ + Read a template file ``template_file_path``, perform substitutions + found in the ``substitutions`` dictionary and write the result to + the output file ``output_file_path``. + + The template file should contain zero or more template strings of the + form ``@NAME@``. + + The substitutions dictionary maps old strings (without the ``@`` + symbols) to their replacements. + """ + assert isinstance(template_file_path, str) + assert isinstance(output_file_path, str) + assert isinstance(substitutions, dict) + assert len(template_file_path) > 0 + assert len(output_file_path) > 0 + print("Generating {} from {}".format(output_file_path, template_file_path)) + + if not os.path.exists(template_file_path): + raise Exception('Could not find template file "{}"'.format(template_file_path)) + + # Read whole template file into string + template_string = None + with open(template_file_path, 'r') as f: + template_string = f.read() + + # Do replacements + for (old_string, replacement) in substitutions.items(): + template_string = template_string.replace('@{}@'.format(old_string), replacement) + + # Write the string to the file + with open(output_file_path, 'w') as f: + f.write(template_string) + try: parse_options() @@ -74,6 +116,13 @@ try: def temp_path(path): return os.path.join(TEMP_DIR, path) + # Create configuration file from template + doxygen_config_substitutions = { + 'OUTPUT_DIRECTORY': OUTPUT_DIRECTORY, + } + doxygen_config_file = temp_path('z3api.cfg') + configure_file('z3api.cfg.in', doxygen_config_file, doxygen_config_substitutions) + fi = open('website.dox', 'r') fo = open(temp_path('website.dox'), 'w') @@ -86,7 +135,7 @@ try: fo.close() - mk_dir('api/html') + mk_dir(os.path.join(OUTPUT_DIRECTORY, 'html')) shutil.copyfile('../src/api/python/z3/z3.py', temp_path('z3py.py')) cleanup_API('../src/api/z3_api.h', temp_path('z3_api.h')) cleanup_API('../src/api/z3_ast_containers.h', temp_path('z3_ast_containers.h')) @@ -100,7 +149,7 @@ try: print("Removed annotations from z3_api.h.") try: - if subprocess.call([DOXYGEN_EXE, 'z3api.dox']) != 0: + if subprocess.call([DOXYGEN_EXE, doxygen_config_file]) != 0: print("ERROR: doxygen returned nonzero return code") exit(1) except: @@ -111,17 +160,18 @@ try: print("Removed temporary directory \"{}\"".format(TEMP_DIR)) sys.path.append('../src/api/python/z3') pydoc.writedoc('z3') - shutil.move('z3.html', 'api/html/z3.html') + shutil.move('z3.html', os.path.join(OUTPUT_DIRECTORY, 'html', 'z3.html')) print("Generated Python documentation.") if ML_ENABLED: - mk_dir('api/html/ml') - if subprocess.call(['ocamldoc', '-html', '-d', 'api\html\ml', '-sort', '-hide', 'Z3', '-I', '%s/api/ml' % BUILD_DIR, '../src/api/ml/z3enums.mli', '../src/api/ml/z3.mli']) != 0: + ml_output_dir = os.path.join(OUTPUT_DIRECTORY, 'html', 'ml') + mk_dir(ml_output_dir) + if subprocess.call(['ocamldoc', '-html', '-d', ml_output_dir, '-sort', '-hide', 'Z3', '-I', '%s/api/ml' % BUILD_DIR, '../src/api/ml/z3enums.mli', '../src/api/ml/z3.mli']) != 0: print("ERROR: ocamldoc failed.") exit(1) print("Generated ML/OCaml documentation.") - print("Documentation was successfully generated at subdirectory './api/html'.") + print("Documentation was successfully generated at subdirectory '{}'.".format(OUTPUT_DIRECTORY)) except Exception: exctype, value = sys.exc_info()[:2] print("ERROR: failed to generate documentation: %s" % value) diff --git a/doc/z3api.dox b/doc/z3api.cfg.in similarity index 99% rename from doc/z3api.dox rename to doc/z3api.cfg.in index c96a7be73..7e6c81e4f 100644 --- a/doc/z3api.dox +++ b/doc/z3api.cfg.in @@ -52,7 +52,7 @@ PROJECT_LOGO = # If a relative path is entered, it will be relative to the location # where doxygen was started. If left blank the current directory will be used. -OUTPUT_DIRECTORY = api +OUTPUT_DIRECTORY = @OUTPUT_DIRECTORY@ # If the CREATE_SUBDIRS tag is set to YES, then doxygen will create # 4096 sub-directories (in 2 levels) under the output directory of each output From 5a66f053848c05947a48e2aec953610291b39e28 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Mon, 24 Apr 2017 23:38:40 +0100 Subject: [PATCH 474/562] [Doxygen] Teach `mk_api_doc.py` to use `@` style substitutions to control whether OCaml documentation link is emitted. --- doc/mk_api_doc.py | 17 ++++++----------- doc/{website.dox => website.dox.in} | 3 +-- 2 files changed, 7 insertions(+), 13 deletions(-) rename doc/{website.dox => website.dox.in} (92%) diff --git a/doc/mk_api_doc.py b/doc/mk_api_doc.py index b4a58f392..0a7efacfa 100644 --- a/doc/mk_api_doc.py +++ b/doc/mk_api_doc.py @@ -123,17 +123,12 @@ try: doxygen_config_file = temp_path('z3api.cfg') configure_file('z3api.cfg.in', doxygen_config_file, doxygen_config_substitutions) - fi = open('website.dox', 'r') - fo = open(temp_path('website.dox'), 'w') - - for line in fi: - if (line != '[ML]\n'): - fo.write(line) - elif (ML_ENABLED): - fo.write(' - ML/OCaml API\n') - fi.close() - fo.close() - + website_dox_substitutions = {} + if ML_ENABLED: + website_dox_substitutions['OCAML_API'] = '\n - ML/OCaml API\n' + else: + website_dox_substitutions['OCAML_API'] = '' + configure_file('website.dox.in', temp_path('website.dox'), website_dox_substitutions) mk_dir(os.path.join(OUTPUT_DIRECTORY, 'html')) shutil.copyfile('../src/api/python/z3/z3.py', temp_path('z3py.py')) diff --git a/doc/website.dox b/doc/website.dox.in similarity index 92% rename from doc/website.dox rename to doc/website.dox.in index 799949752..b00874c97 100644 --- a/doc/website.dox +++ b/doc/website.dox.in @@ -14,7 +14,6 @@ - \ref cppapi - .NET API - Java API - - Python API (also available in pydoc format) -[ML] + - Python API (also available in pydoc format)@OCAML_API@ - Try Z3 online at RiSE4Fun. */ From c78bf66df34ad6ea9aa104ec13f37b01de30769e Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Mon, 24 Apr 2017 23:49:44 +0100 Subject: [PATCH 475/562] [Doxygen] Fix bug in `mk_api_doc.py` where the generated doxygen configuration would not point at the correct path to the temporary directory. --- doc/mk_api_doc.py | 1 + doc/z3api.cfg.in | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/mk_api_doc.py b/doc/mk_api_doc.py index 0a7efacfa..cc59ffa69 100644 --- a/doc/mk_api_doc.py +++ b/doc/mk_api_doc.py @@ -119,6 +119,7 @@ try: # Create configuration file from template doxygen_config_substitutions = { 'OUTPUT_DIRECTORY': OUTPUT_DIRECTORY, + 'TEMP_DIR': TEMP_DIR } doxygen_config_file = temp_path('z3api.cfg') configure_file('z3api.cfg.in', doxygen_config_file, doxygen_config_substitutions) diff --git a/doc/z3api.cfg.in b/doc/z3api.cfg.in index 7e6c81e4f..bcc7113cc 100644 --- a/doc/z3api.cfg.in +++ b/doc/z3api.cfg.in @@ -684,7 +684,7 @@ WARN_LOGFILE = INPUT = ../src/api/dotnet \ ../src/api/java \ ../src/api/c++ \ - ./tmp + @TEMP_DIR@ # This tag can be used to specify the character encoding of the source files # that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is From 33af478ce24b33393b23952833064638fead3e8a Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Mon, 24 Apr 2017 23:55:51 +0100 Subject: [PATCH 476/562] [Doxygen] Fix some indentation in doxygen configuration file template. --- doc/z3api.cfg.in | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/doc/z3api.cfg.in b/doc/z3api.cfg.in index bcc7113cc..cb07045b3 100644 --- a/doc/z3api.cfg.in +++ b/doc/z3api.cfg.in @@ -703,15 +703,15 @@ INPUT_ENCODING = UTF-8 # *.f90 *.f *.for *.vhd *.vhdl FILE_PATTERNS = website.dox \ - z3_api.h \ - z3_algebraic.h \ - z3_polynomial.h \ - z3_rcf.h \ - z3_interp.h \ - z3_fpa.h \ + z3_api.h \ + z3_algebraic.h \ + z3_polynomial.h \ + z3_rcf.h \ + z3_interp.h \ + z3_fpa.h \ z3++.h \ z3py.py \ - *.cs \ + *.cs \ *.java # The RECURSIVE tag can be used to turn specify whether or not subdirectories From eb1c985a944b59a1cbbb55912a6fcfb030c5670e Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Tue, 25 Apr 2017 00:24:46 +0100 Subject: [PATCH 477/562] [Doxygen] Fixed malformed code blocks in `z3_api.h`. These malformed `\code` blocks caused broken documentation to be generated. --- src/api/z3_api.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/z3_api.h b/src/api/z3_api.h index 272c94dda..557667b03 100644 --- a/src/api/z3_api.h +++ b/src/api/z3_api.h @@ -3091,8 +3091,8 @@ extern "C" { \brief Create a numeral of a given sort. \param c logical context. - \param numeral A string representing the numeral value in decimal notation. The string may be of the form \code{[num]*[.[num]*][E[+|-][num]+]}. - If the given sort is a real, then the numeral can be a rational, that is, a string of the form \ccode{[num]* / [num]*}. + \param numeral A string representing the numeral value in decimal notation. The string may be of the form `[num]*[.[num]*][E[+|-][num]+]`. + If the given sort is a real, then the numeral can be a rational, that is, a string of the form `[num]* / [num]*` . \param ty The sort of the numeral. In the current implementation, the given sort can be an int, real, finite-domain, or bit-vectors of arbitrary size. \sa Z3_mk_int From 7242a77a3f377308dbfdd4b62a4dca7d80cb05d2 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Tue, 25 Apr 2017 00:31:05 +0100 Subject: [PATCH 478/562] [Doxygen] Fix typo found with Doxygen warning ``` warning: Found unknown command `\s' ``` --- src/api/z3_api.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/z3_api.h b/src/api/z3_api.h index 557667b03..04f84fa2a 100644 --- a/src/api/z3_api.h +++ b/src/api/z3_api.h @@ -3306,7 +3306,7 @@ extern "C" { Z3_ast Z3_API Z3_mk_seq_replace(Z3_context c, Z3_ast s, Z3_ast src, Z3_ast dst); /** - \brief Retrieve from \s the unit sequence positioned at position \c index. + \brief Retrieve from \c s the unit sequence positioned at position \c index. def_API('Z3_mk_seq_at' ,AST ,(_in(CONTEXT), _in(AST), _in(AST))) */ From fe702d7782db990cb90ff2bea390b100fdd65872 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Tue, 25 Apr 2017 00:36:53 +0100 Subject: [PATCH 479/562] [Doxygen] Fix warning about non-existent functions. `Z3_push` and `Z3_pop` should be `Z3_solver_push` and `Z3_solver_pop` respectively. --- src/api/z3_api.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/z3_api.h b/src/api/z3_api.h index 04f84fa2a..45065f856 100644 --- a/src/api/z3_api.h +++ b/src/api/z3_api.h @@ -1500,7 +1500,7 @@ extern "C" { All main interaction with Z3 happens in the context of a \c Z3_context. In contrast to #Z3_mk_context_rc, the life time of Z3_ast objects - are determined by the scope level of #Z3_push and #Z3_pop. + are determined by the scope level of #Z3_solver_push and #Z3_solver_pop. In other words, a Z3_ast object remains valid until there is a call to Z3_pop that takes the current scope below the level where the object was created. From e309174ec968d378f36415611dee1a09be52ccdd Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Tue, 25 Apr 2017 11:32:26 +0100 Subject: [PATCH 480/562] [Doxygen] Add `--z3py-package-path` command line option to `mk_api_doc.py` so that the location of the z3py package can be specified. This is needed by the CMake build system because the complete Z3py package is not emitted in the source tree. Also fix a bug in the path added to the module/package search path. The directory containing the `z3` package needs to be added not the `z3` package directory itself. --- doc/mk_api_doc.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/doc/mk_api_doc.py b/doc/mk_api_doc.py index cc59ffa69..3a1f8b3f8 100644 --- a/doc/mk_api_doc.py +++ b/doc/mk_api_doc.py @@ -18,9 +18,10 @@ BUILD_DIR='../build' DOXYGEN_EXE='doxygen' TEMP_DIR=os.path.join(os.getcwd(), 'tmp') OUTPUT_DIRECTORY=os.path.join(os.getcwd(), 'api') +Z3PY_PACKAGE_PATH='../src/api/python/z3' def parse_options(): - global ML_ENABLED, BUILD_DIR, DOXYGEN_EXE, TEMP_DIR, OUTPUT_DIRECTORY + global ML_ENABLED, BUILD_DIR, DOXYGEN_EXE, TEMP_DIR, OUTPUT_DIRECTORY, Z3PY_PACKAGE_PATH parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('-b', '--build', @@ -48,12 +49,22 @@ def parse_options(): default=OUTPUT_DIRECTORY, help='Path to output directory (default: %(default)s)', ) + parser.add_argument('--z3py-package-path', + dest='z3py_package_path', + default=Z3PY_PACKAGE_PATH, + help='Path to directory containing Z3py package (default: %(default)s)', + ) pargs = parser.parse_args() ML_ENABLED = pargs.ml BUILD_DIR = pargs.build DOXYGEN_EXE = pargs.doxygen_executable TEMP_DIR = pargs.temp_dir OUTPUT_DIRECTORY = pargs.output_dir + Z3PY_PACKAGE_PATH = pargs.z3py_package_path + if not os.path.exists(Z3PY_PACKAGE_PATH): + raise Exception('"{}" does not exist'.format(Z3PY_PACKAGE_PATH)) + if not os.path.basename(Z3PY_PACKAGE_PATH) == 'z3': + raise Exception('"{}" does not end with "z3"'.format(Z3PY_PACKAGE_PATH)) return def mk_dir(d): @@ -154,7 +165,7 @@ try: print("Generated C and .NET API documentation.") shutil.rmtree(os.path.realpath(TEMP_DIR)) print("Removed temporary directory \"{}\"".format(TEMP_DIR)) - sys.path.append('../src/api/python/z3') + sys.path.append(os.path.dirname(Z3PY_PACKAGE_PATH)) pydoc.writedoc('z3') shutil.move('z3.html', os.path.join(OUTPUT_DIRECTORY, 'html', 'z3.html')) print("Generated Python documentation.") From cb6baa8bcb5f987cf6577307e1f7f0809386d981 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Tue, 25 Apr 2017 11:47:50 +0100 Subject: [PATCH 481/562] [Doxygen] Put the path to the directory containing the Z3py package at the beginning of the search path so it is picked up first. This is to try to avoid picking an installed copy of Z3py. --- doc/mk_api_doc.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/mk_api_doc.py b/doc/mk_api_doc.py index 3a1f8b3f8..10b2fcf55 100644 --- a/doc/mk_api_doc.py +++ b/doc/mk_api_doc.py @@ -165,7 +165,9 @@ try: print("Generated C and .NET API documentation.") shutil.rmtree(os.path.realpath(TEMP_DIR)) print("Removed temporary directory \"{}\"".format(TEMP_DIR)) - sys.path.append(os.path.dirname(Z3PY_PACKAGE_PATH)) + # Put z3py at the beginning of the search path to try to avoid picking up + # an installed copy of Z3py. + sys.path.insert(0, os.path.dirname(Z3PY_PACKAGE_PATH)) pydoc.writedoc('z3') shutil.move('z3.html', os.path.join(OUTPUT_DIRECTORY, 'html', 'z3.html')) print("Generated Python documentation.") From fa8f6f20a5fa59f3e4e180cbfbb0c575d7b4e1ed Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Tue, 25 Apr 2017 13:07:30 +0100 Subject: [PATCH 482/562] [Doxygen] Teach `mk_api_doc.py` to prevent ".NET", "Z3py" and "Java" bindings from appearing in the generated documentation. This can be enabled with `--no-dotnet`, `--no-z3py`, and `--no-java` respectively. This fine-grained control is being added for the CMake build system which will need this control. --- doc/mk_api_doc.py | 102 ++++++++++++++++++++++++++++++++++++++++----- doc/website.dox.in | 7 +--- doc/z3api.cfg.in | 9 ++-- 3 files changed, 97 insertions(+), 21 deletions(-) diff --git a/doc/mk_api_doc.py b/doc/mk_api_doc.py index 10b2fcf55..b61036d44 100644 --- a/doc/mk_api_doc.py +++ b/doc/mk_api_doc.py @@ -19,9 +19,13 @@ DOXYGEN_EXE='doxygen' TEMP_DIR=os.path.join(os.getcwd(), 'tmp') OUTPUT_DIRECTORY=os.path.join(os.getcwd(), 'api') Z3PY_PACKAGE_PATH='../src/api/python/z3' +Z3PY_ENABLED=True +DOTNET_ENABLED=True +JAVA_ENABLED=True def parse_options(): - global ML_ENABLED, BUILD_DIR, DOXYGEN_EXE, TEMP_DIR, OUTPUT_DIRECTORY, Z3PY_PACKAGE_PATH + global ML_ENABLED, BUILD_DIR, DOXYGEN_EXE, TEMP_DIR, OUTPUT_DIRECTORY + global Z3PY_PACKAGE_PATH, Z3PY_ENABLED, DOTNET_ENABLED, JAVA_ENABLED parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('-b', '--build', @@ -54,6 +58,28 @@ def parse_options(): default=Z3PY_PACKAGE_PATH, help='Path to directory containing Z3py package (default: %(default)s)', ) + # FIXME: I would prefer not to have negative options (i.e. `--z3py` + # instead of `--no-z3py`) but historically these bindings have been on by + # default so we have options to disable generating documentation for these + # bindings rather than enable them. + parser.add_argument('--no-z3py', + dest='no_z3py', + action='store_true', + default=False, + help='Do not generate documentation for Python bindings', + ) + parser.add_argument('--no-dotnet', + dest='no_dotnet', + action='store_true', + default=False, + help='Do not generate documentation for .NET bindings', + ) + parser.add_argument('--no-java', + dest='no_java', + action='store_true', + default=False, + help='Do not generate documentation for Java bindings', + ) pargs = parser.parse_args() ML_ENABLED = pargs.ml BUILD_DIR = pargs.build @@ -65,6 +91,9 @@ def parse_options(): raise Exception('"{}" does not exist'.format(Z3PY_PACKAGE_PATH)) if not os.path.basename(Z3PY_PACKAGE_PATH) == 'z3': raise Exception('"{}" does not end with "z3"'.format(Z3PY_PACKAGE_PATH)) + Z3PY_ENABLED = not pargs.no_z3py + DOTNET_ENABLED = not pargs.no_dotnet + JAVA_ENABLED = not pargs.no_java return def mk_dir(d): @@ -132,18 +161,70 @@ try: 'OUTPUT_DIRECTORY': OUTPUT_DIRECTORY, 'TEMP_DIR': TEMP_DIR } + + if Z3PY_ENABLED: + print("Z3Py documentation enabled") + doxygen_config_substitutions['PYTHON_API_FILES'] = 'z3.py' + else: + print("Z3Py documentation disabled") + doxygen_config_substitutions['PYTHON_API_FILES'] = '' + if DOTNET_ENABLED: + print(".NET documentation enabled") + doxygen_config_substitutions['DOTNET_API_FILES'] = '*.cs' + doxygen_config_substitutions['DOTNET_API_SEARCH_PATHS'] = '../src/api/dotnet' + else: + print(".NET documentation disabled") + doxygen_config_substitutions['DOTNET_API_FILES'] = '' + doxygen_config_substitutions['DOTNET_API_SEARCH_PATHS'] = '' + if JAVA_ENABLED: + print("Java documentation enabled") + doxygen_config_substitutions['JAVA_API_FILES'] = '*.java' + doxygen_config_substitutions['JAVA_API_SEARCH_PATHS'] = '../src/api/java' + else: + print("Java documentation disabled") + doxygen_config_substitutions['JAVA_API_FILES'] = '' + doxygen_config_substitutions['JAVA_API_SEARCH_PATHS'] = '' + doxygen_config_file = temp_path('z3api.cfg') configure_file('z3api.cfg.in', doxygen_config_file, doxygen_config_substitutions) website_dox_substitutions = {} + bullet_point_prefix='\n - ' + if Z3PY_ENABLED: + website_dox_substitutions['PYTHON_API'] = ( + '{prefix}Python API ' + '(also available in pydoc format)' + ).format( + prefix=bullet_point_prefix) + else: + website_dox_substitutions['PYTHON_API'] = '' + if DOTNET_ENABLED: + website_dox_substitutions['DOTNET_API'] = ( + '{prefix}' + '' + '.NET API').format( + prefix=bullet_point_prefix) + else: + website_dox_substitutions['DOTNET_API'] = '' + if JAVA_ENABLED: + website_dox_substitutions['JAVA_API'] = ( + '{prefix}' + 'Java API').format( + prefix=bullet_point_prefix) + else: + website_dox_substitutions['JAVA_API'] = '' if ML_ENABLED: - website_dox_substitutions['OCAML_API'] = '\n - ML/OCaml API\n' + website_dox_substitutions['OCAML_API'] = ( + 'ML/OCaml API' + ).format( + prefix=bullet_point_prefix) else: website_dox_substitutions['OCAML_API'] = '' configure_file('website.dox.in', temp_path('website.dox'), website_dox_substitutions) mk_dir(os.path.join(OUTPUT_DIRECTORY, 'html')) - shutil.copyfile('../src/api/python/z3/z3.py', temp_path('z3py.py')) + if Z3PY_ENABLED: + shutil.copyfile('../src/api/python/z3/z3.py', temp_path('z3py.py')) cleanup_API('../src/api/z3_api.h', temp_path('z3_api.h')) cleanup_API('../src/api/z3_ast_containers.h', temp_path('z3_ast_containers.h')) cleanup_API('../src/api/z3_algebraic.h', temp_path('z3_algebraic.h')) @@ -162,15 +243,16 @@ try: except: print("ERROR: failed to execute 'doxygen', make sure doxygen (http://www.doxygen.org) is available in your system.") exit(1) - print("Generated C and .NET API documentation.") + print("Generated Doxygen based documentation") shutil.rmtree(os.path.realpath(TEMP_DIR)) print("Removed temporary directory \"{}\"".format(TEMP_DIR)) - # Put z3py at the beginning of the search path to try to avoid picking up - # an installed copy of Z3py. - sys.path.insert(0, os.path.dirname(Z3PY_PACKAGE_PATH)) - pydoc.writedoc('z3') - shutil.move('z3.html', os.path.join(OUTPUT_DIRECTORY, 'html', 'z3.html')) - print("Generated Python documentation.") + if Z3PY_ENABLED: + # Put z3py at the beginning of the search path to try to avoid picking up + # an installed copy of Z3py. + sys.path.insert(0, os.path.dirname(Z3PY_PACKAGE_PATH)) + pydoc.writedoc('z3') + shutil.move('z3.html', os.path.join(OUTPUT_DIRECTORY, 'html', 'z3.html')) + print("Generated pydoc Z3Py documentation.") if ML_ENABLED: ml_output_dir = os.path.join(OUTPUT_DIRECTORY, 'html', 'ml') diff --git a/doc/website.dox.in b/doc/website.dox.in index b00874c97..17a8552d1 100644 --- a/doc/website.dox.in +++ b/doc/website.dox.in @@ -10,10 +10,7 @@ This website hosts the automatically generated documentation for the Z3 APIs. - - \ref capi - - \ref cppapi - - .NET API - - Java API - - Python API (also available in pydoc format)@OCAML_API@ + - \ref capi + - \ref cppapi @DOTNET_API@ @JAVA_API@ @PYTHON_API@ @OCAML_API@ - Try Z3 online at RiSE4Fun. */ diff --git a/doc/z3api.cfg.in b/doc/z3api.cfg.in index cb07045b3..408e981d2 100644 --- a/doc/z3api.cfg.in +++ b/doc/z3api.cfg.in @@ -681,10 +681,9 @@ WARN_LOGFILE = # directories like "/usr/src/myproject". Separate the files or directories # with spaces. -INPUT = ../src/api/dotnet \ - ../src/api/java \ +INPUT = @TEMP_DIR@ \ ../src/api/c++ \ - @TEMP_DIR@ + @DOTNET_API_SEARCH_PATHS@ @JAVA_API_SEARCH_PATHS@ # This tag can be used to specify the character encoding of the source files # that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is @@ -710,9 +709,7 @@ FILE_PATTERNS = website.dox \ z3_interp.h \ z3_fpa.h \ z3++.h \ - z3py.py \ - *.cs \ - *.java + @PYTHON_API_FILES@ @DOTNET_API_FILES@ @JAVA_API_FILES@ # The RECURSIVE tag can be used to turn specify whether or not subdirectories # should be searched for input files as well. Possible values are YES and NO. From 09d7ebf1adcc3a4dfea3125f126f7a27a059797e Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Tue, 25 Apr 2017 13:17:21 +0100 Subject: [PATCH 483/562] [Doxygen] Fix bug where temporary directory and output directory paths were not handled properly if paths contained spaces. --- doc/z3api.cfg.in | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/z3api.cfg.in b/doc/z3api.cfg.in index 408e981d2..7ccad3fbb 100644 --- a/doc/z3api.cfg.in +++ b/doc/z3api.cfg.in @@ -52,7 +52,7 @@ PROJECT_LOGO = # If a relative path is entered, it will be relative to the location # where doxygen was started. If left blank the current directory will be used. -OUTPUT_DIRECTORY = @OUTPUT_DIRECTORY@ +OUTPUT_DIRECTORY = "@OUTPUT_DIRECTORY@" # If the CREATE_SUBDIRS tag is set to YES, then doxygen will create # 4096 sub-directories (in 2 levels) under the output directory of each output @@ -681,7 +681,7 @@ WARN_LOGFILE = # directories like "/usr/src/myproject". Separate the files or directories # with spaces. -INPUT = @TEMP_DIR@ \ +INPUT = "@TEMP_DIR@" \ ../src/api/c++ \ @DOTNET_API_SEARCH_PATHS@ @JAVA_API_SEARCH_PATHS@ From e4bec1572aa404298abbacf7693e3f37e7453d04 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Tue, 25 Apr 2017 13:36:48 +0100 Subject: [PATCH 484/562] [Doxygen] Teach `mk_api_doc.py` to allow multiple search paths for the ".NET" and "Java" bindings. The CMake build system needs this because the generated files exist in a different directory to the source files. Multiple paths can be specified using the `--dot-search-paths` and `--java-search-paths` options. --- doc/mk_api_doc.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/doc/mk_api_doc.py b/doc/mk_api_doc.py index b61036d44..e331c60b9 100644 --- a/doc/mk_api_doc.py +++ b/doc/mk_api_doc.py @@ -22,10 +22,13 @@ Z3PY_PACKAGE_PATH='../src/api/python/z3' Z3PY_ENABLED=True DOTNET_ENABLED=True JAVA_ENABLED=True +DOTNET_API_SEARCH_PATHS=['../src/api/dotnet'] +JAVA_API_SEARCH_PATHS=['../src/api/java'] def parse_options(): global ML_ENABLED, BUILD_DIR, DOXYGEN_EXE, TEMP_DIR, OUTPUT_DIRECTORY global Z3PY_PACKAGE_PATH, Z3PY_ENABLED, DOTNET_ENABLED, JAVA_ENABLED + global DOTNET_API_SEARCH_PATHS, JAVA_API_SEARCH_PATHS parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('-b', '--build', @@ -80,6 +83,18 @@ def parse_options(): default=False, help='Do not generate documentation for Java bindings', ) + parser.add_argument('--dotnet-search-paths', + dest='dotnet_search_paths', + nargs='+', + default=DOTNET_API_SEARCH_PATHS, + help='Specify one or more path to look for .NET files (default: %(default)s).', + ) + parser.add_argument('--java-search-paths', + dest='java_search_paths', + nargs='+', + default=JAVA_API_SEARCH_PATHS, + help='Specify one or more paths to look for Java files (default: %(default)s).', + ) pargs = parser.parse_args() ML_ENABLED = pargs.ml BUILD_DIR = pargs.build @@ -94,6 +109,8 @@ def parse_options(): Z3PY_ENABLED = not pargs.no_z3py DOTNET_ENABLED = not pargs.no_dotnet JAVA_ENABLED = not pargs.no_java + DOTNET_API_SEARCH_PATHS = pargs.dotnet_search_paths + JAVA_API_SEARCH_PATHS = pargs.java_search_paths return def mk_dir(d): @@ -171,7 +188,11 @@ try: if DOTNET_ENABLED: print(".NET documentation enabled") doxygen_config_substitutions['DOTNET_API_FILES'] = '*.cs' - doxygen_config_substitutions['DOTNET_API_SEARCH_PATHS'] = '../src/api/dotnet' + dotnet_api_search_path_str = "" + for p in DOTNET_API_SEARCH_PATHS: + # Quote path so that paths with spaces are handled correctly + dotnet_api_search_path_str += "\"{}\" ".format(p) + doxygen_config_substitutions['DOTNET_API_SEARCH_PATHS'] = dotnet_api_search_path_str else: print(".NET documentation disabled") doxygen_config_substitutions['DOTNET_API_FILES'] = '' @@ -179,7 +200,11 @@ try: if JAVA_ENABLED: print("Java documentation enabled") doxygen_config_substitutions['JAVA_API_FILES'] = '*.java' - doxygen_config_substitutions['JAVA_API_SEARCH_PATHS'] = '../src/api/java' + java_api_search_path_str = "" + for p in JAVA_API_SEARCH_PATHS: + # Quote path so that paths with spaces are handled correctly + java_api_search_path_str += "\"{}\" ".format(p) + doxygen_config_substitutions['JAVA_API_SEARCH_PATHS'] = java_api_search_path_str else: print("Java documentation disabled") doxygen_config_substitutions['JAVA_API_FILES'] = '' From 121fd06cc2f3dec7fd2412815b9340e8dbfb90c0 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Tue, 25 Apr 2017 14:36:02 +0100 Subject: [PATCH 485/562] [Doxygen] Fix `mk_api_doc.py` so it is not required that the current working directory be the `doc` directory in the source tree. --- doc/mk_api_doc.py | 39 +++++++++++++++++++++++++-------------- doc/z3api.cfg.in | 2 +- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/doc/mk_api_doc.py b/doc/mk_api_doc.py index e331c60b9..6007b77c4 100644 --- a/doc/mk_api_doc.py +++ b/doc/mk_api_doc.py @@ -24,6 +24,7 @@ DOTNET_ENABLED=True JAVA_ENABLED=True DOTNET_API_SEARCH_PATHS=['../src/api/dotnet'] JAVA_API_SEARCH_PATHS=['../src/api/java'] +SCRIPT_DIR=os.path.abspath(os.path.dirname(__file__)) def parse_options(): global ML_ENABLED, BUILD_DIR, DOXYGEN_EXE, TEMP_DIR, OUTPUT_DIRECTORY @@ -172,11 +173,15 @@ try: # Short-hand for path to temporary file def temp_path(path): return os.path.join(TEMP_DIR, path) + # Short-hand for path to file in `doc` directory + def doc_path(path): + return os.path.join(SCRIPT_DIR, path) # Create configuration file from template doxygen_config_substitutions = { 'OUTPUT_DIRECTORY': OUTPUT_DIRECTORY, - 'TEMP_DIR': TEMP_DIR + 'TEMP_DIR': TEMP_DIR, + 'CXX_API_SEARCH_PATHS': doc_path('../src/api/c++'), } if Z3PY_ENABLED: @@ -211,7 +216,10 @@ try: doxygen_config_substitutions['JAVA_API_SEARCH_PATHS'] = '' doxygen_config_file = temp_path('z3api.cfg') - configure_file('z3api.cfg.in', doxygen_config_file, doxygen_config_substitutions) + configure_file( + doc_path('z3api.cfg.in'), + doxygen_config_file, + doxygen_config_substitutions) website_dox_substitutions = {} bullet_point_prefix='\n - ' @@ -245,20 +253,23 @@ try: prefix=bullet_point_prefix) else: website_dox_substitutions['OCAML_API'] = '' - configure_file('website.dox.in', temp_path('website.dox'), website_dox_substitutions) + configure_file( + doc_path('website.dox.in'), + temp_path('website.dox'), + website_dox_substitutions) mk_dir(os.path.join(OUTPUT_DIRECTORY, 'html')) if Z3PY_ENABLED: - shutil.copyfile('../src/api/python/z3/z3.py', temp_path('z3py.py')) - cleanup_API('../src/api/z3_api.h', temp_path('z3_api.h')) - cleanup_API('../src/api/z3_ast_containers.h', temp_path('z3_ast_containers.h')) - cleanup_API('../src/api/z3_algebraic.h', temp_path('z3_algebraic.h')) - cleanup_API('../src/api/z3_polynomial.h', temp_path('z3_polynomial.h')) - cleanup_API('../src/api/z3_rcf.h', temp_path('z3_rcf.h')) - cleanup_API('../src/api/z3_fixedpoint.h', temp_path('z3_fixedpoint.h')) - cleanup_API('../src/api/z3_optimization.h', temp_path('z3_optimization.h')) - cleanup_API('../src/api/z3_interp.h', temp_path('z3_interp.h')) - cleanup_API('../src/api/z3_fpa.h', temp_path('z3_fpa.h')) + shutil.copyfile(doc_path('../src/api/python/z3/z3.py'), temp_path('z3py.py')) + cleanup_API(doc_path('../src/api/z3_api.h'), temp_path('z3_api.h')) + cleanup_API(doc_path('../src/api/z3_ast_containers.h'), temp_path('z3_ast_containers.h')) + cleanup_API(doc_path('../src/api/z3_algebraic.h'), temp_path('z3_algebraic.h')) + cleanup_API(doc_path('../src/api/z3_polynomial.h'), temp_path('z3_polynomial.h')) + cleanup_API(doc_path('../src/api/z3_rcf.h'), temp_path('z3_rcf.h')) + cleanup_API(doc_path('../src/api/z3_fixedpoint.h'), temp_path('z3_fixedpoint.h')) + cleanup_API(doc_path('../src/api/z3_optimization.h'), temp_path('z3_optimization.h')) + cleanup_API(doc_path('../src/api/z3_interp.h'), temp_path('z3_interp.h')) + cleanup_API(doc_path('../src/api/z3_fpa.h'), temp_path('z3_fpa.h')) print("Removed annotations from z3_api.h.") try: @@ -282,7 +293,7 @@ try: if ML_ENABLED: ml_output_dir = os.path.join(OUTPUT_DIRECTORY, 'html', 'ml') mk_dir(ml_output_dir) - if subprocess.call(['ocamldoc', '-html', '-d', ml_output_dir, '-sort', '-hide', 'Z3', '-I', '%s/api/ml' % BUILD_DIR, '../src/api/ml/z3enums.mli', '../src/api/ml/z3.mli']) != 0: + if subprocess.call(['ocamldoc', '-html', '-d', ml_output_dir, '-sort', '-hide', 'Z3', '-I', '%s/api/ml' % BUILD_DIR, doc_path('../src/api/ml/z3enums.mli'), doc_path('../src/api/ml/z3.mli')]) != 0: print("ERROR: ocamldoc failed.") exit(1) print("Generated ML/OCaml documentation.") diff --git a/doc/z3api.cfg.in b/doc/z3api.cfg.in index 7ccad3fbb..9e946aa7f 100644 --- a/doc/z3api.cfg.in +++ b/doc/z3api.cfg.in @@ -682,7 +682,7 @@ WARN_LOGFILE = # with spaces. INPUT = "@TEMP_DIR@" \ - ../src/api/c++ \ + "@CXX_API_SEARCH_PATHS@" \ @DOTNET_API_SEARCH_PATHS@ @JAVA_API_SEARCH_PATHS@ # This tag can be used to specify the character encoding of the source files From a6a6a9c29f96c63eec7d5efef6860553d366be21 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Tue, 25 Apr 2017 15:43:03 +0100 Subject: [PATCH 486/562] [Doxygen] Fix link to ".NET" documentation it should point to the "Microsoft.Z3" namespace, not the "Microsoft.Z3.Context" class. This mirrors the link provided for the Java API. --- doc/mk_api_doc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/mk_api_doc.py b/doc/mk_api_doc.py index 6007b77c4..014a152b4 100644 --- a/doc/mk_api_doc.py +++ b/doc/mk_api_doc.py @@ -234,7 +234,7 @@ try: if DOTNET_ENABLED: website_dox_substitutions['DOTNET_API'] = ( '{prefix}' - '' + '' '.NET API').format( prefix=bullet_point_prefix) else: From d4b7b489d072daaf268ee15f3f1517c4388cf8b3 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Tue, 25 Apr 2017 14:16:14 +0100 Subject: [PATCH 487/562] [CMake] Teach CMake to build the documentation for the API bindings and install them. The target for building the documentation is `api_docs`. This is off by default but can be enabled with the `BUILD_DOCUMENTATION` option. The C and C++ API documentation is always built but the Python, ".NET", and Java documentation are only built if they are enabled in the build system. The rationale for this is that it would be confusing to install documentation for API bindings that are not installed. By default `ALWAYS_BUILD_DOCS` is on which will slow down builds significantly but will ensure that when the `install` target is invoked the documentation is up-to-date. Unfortunately I couldn't find a better way to do this. `ALWAYS_BUILD_DOCS` can be disabled to get faster builds and still have the `api_docs` target available. --- CMakeLists.txt | 16 +++++- README-CMake.md | 7 +++ contrib/cmake/doc/CMakeLists.txt | 93 ++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 contrib/cmake/doc/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index cf46e7012..47a081e75 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,10 +43,13 @@ message(STATUS "Z3 version ${Z3_VERSION}") # Set various useful variables depending on CMake version ################################################################################ if (("${CMAKE_VERSION}" VERSION_EQUAL "3.2") OR ("${CMAKE_VERSION}" VERSION_GREATER "3.2")) - # In CMake >= 3.2 add_custom_command() supports a ``USES_TERMINAL`` argument + # In CMake >= 3.2 add_custom_command() and add_custom_target() + # supports a ``USES_TERMINAL`` argument set(ADD_CUSTOM_COMMAND_USES_TERMINAL_ARG "USES_TERMINAL") + set(ADD_CUSTOM_TARGET_USES_TERMINAL_ARG "USES_TERMINAL") else() set(ADD_CUSTOM_COMMAND_USES_TERMINAL_ARG "") + set(ADD_CUSTOM_TARGET_USES_TERMINAL_ARG "") endif() ################################################################################ @@ -528,3 +531,14 @@ option(ENABLE_EXAMPLE_TARGETS "Build Z3 api examples" ON) if (ENABLE_EXAMPLE_TARGETS) add_subdirectory(examples) endif() + +################################################################################ +# Documentation +################################################################################ +option(BUILD_DOCUMENTATION "Build API documentation" OFF) +if (BUILD_DOCUMENTATION) + message(STATUS "Building documentation enabled") + add_subdirectory(doc) +else() + message(STATUS "Building documentation disabled") +endif() diff --git a/README-CMake.md b/README-CMake.md index 6a78b5d4c..4bbbd36a8 100644 --- a/README-CMake.md +++ b/README-CMake.md @@ -268,6 +268,7 @@ The following useful options can be passed to CMake whilst configuring. * ``CMAKE_INSTALL_PKGCONFIGDIR`` - STRING. The path to install pkgconfig files. * ``CMAKE_INSTALL_PYTHON_PKG_DIR`` - STRING. The path to install the z3 python bindings. This can be relative (to ``CMAKE_INSTALL_PREFIX``) or absolute. * ``CMAKE_INSTALL_Z3_CMAKE_PACKAGE_DIR`` - STRING. The path to install CMake package files (e.g. ``/usr/lib/cmake/z3``). +* ``CMAKE_INSTALL_API_BINDINGS_DOC`` - STRING. The path to install documentation for API bindings. * ``ENABLE_TRACING_FOR_NON_DEBUG`` - BOOL. If set to ``TRUE`` enable tracing in non-debug builds, if set to ``FALSE`` disable tracing in non-debug builds. Note in debug builds tracing is always enabled. * ``BUILD_LIBZ3_SHARED`` - BOOL. If set to ``TRUE`` build libz3 as a shared library otherwise build as a static library. * ``ENABLE_EXAMPLE_TARGETS`` - BOOL. If set to ``TRUE`` add the build targets for building the API examples. @@ -286,6 +287,11 @@ The following useful options can be passed to CMake whilst configuring. * ``Z3_JAVA_JNI_LIB_INSTALLDIRR`` - STRING. The path to directory to install the Z3 Java JNI bridge library. This path should be relative to ``CMAKE_INSTALL_PREFIX``. * ``INCLUDE_GIT_DESCRIBE`` - BOOL. If set to ``TRUE`` and the source tree of Z3 is a git repository then the output of ``git describe`` will be included in the build. * ``INCLUDE_GIT_HASH`` - BOOL. If set to ``TRUE`` and the source tree of Z3 is a git repository then the git hash will be included in the build. +* ``BUILD_DOCUMENTATION`` - BOOL. If set to ``TRUE`` then documentation for the API bindings can be built by invoking the ``api_docs`` target. +* ``INSTALL_API_BINDINGS_DOCUMENTATION`` - BOOL. If set to ``TRUE`` and ``BUILD_DOCUMENTATION` is ``TRUE`` then documentation for API bindings will be installed + when running the ``install`` target. +* ``ALWAYS_BUILD_DOCS`` - BOOL. If set to ``TRUE`` and ``BUILD_DOCUMENTATION`` is ``TRUE`` then documentation for API bindings will always be built. + Disabling this is useful for faster incremental builds. The documentation can be manually built by invoking the ``api_docs`` target. On the command line these can be passed to ``cmake`` using the ``-D`` option. In ``ccmake`` and ``cmake-gui`` these can be set in the user interface. @@ -417,6 +423,7 @@ There are a few special targets: * ``clean`` all the built targets in the current directory and below * ``edit_cache`` will invoke one of the CMake tools (depending on which is available) to let you change configuration options. * ``rebuild_cache`` will reinvoke ``cmake`` for the project. +* ``api_docs`` will build the documentation for the API bindings. ### Setting build type specific flags diff --git a/contrib/cmake/doc/CMakeLists.txt b/contrib/cmake/doc/CMakeLists.txt new file mode 100644 index 000000000..86e208ab1 --- /dev/null +++ b/contrib/cmake/doc/CMakeLists.txt @@ -0,0 +1,93 @@ +find_package(Doxygen REQUIRED) +message(STATUS "DOXYGEN_EXECUTABLE: \"${DOXYGEN_EXECUTABLE}\"") +message(STATUS "DOXYGEN_VERSION: \"${DOXYGEN_VERSION}\"") + +set(DOC_DEST_DIR "${CMAKE_CURRENT_BINARY_DIR}/api") +set(DOC_TEMP_DIR "${CMAKE_CURRENT_BINARY_DIR}/temp") +set(MK_API_DOC_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/mk_api_doc.py") + +set(PYTHON_API_OPTIONS "") +set(DOTNET_API_OPTIONS "") +set(JAVA_API_OPTIONS "") +SET(DOC_EXTRA_DEPENDS "") + +if (BUILD_PYTHON_BINDINGS) + # FIXME: Don't hard code this path + list(APPEND PYTHON_API_OPTIONS "--z3py-package-path" "${CMAKE_BINARY_DIR}/python/z3") + list(APPEND DOC_EXTRA_DEPENDS "build_z3_python_bindings") +else() + list(APPEND PYTHON_API_OPTIONS "--no-z3py") +endif() + +if (BUILD_DOTNET_BINDINGS) + # FIXME: Don't hard code these paths + list(APPEND DOTNET_API_OPTIONS "--dotnet-search-paths" + "${CMAKE_SOURCE_DIR}/src/api/dotnet" + "${CMAKE_BINARY_DIR}/src/api/dotnet" + ) + list(APPEND DOC_EXTRA_DEPENDS "build_z3_dotnet_bindings") +else() + list(APPEND DOTNET_API_OPTIONS "--no-dotnet") +endif() + +if (BUILD_JAVA_BINDINGS) + # FIXME: Don't hard code these paths + list(APPEND JAVA_API_OPTIONS "--java-search-paths" + "${CMAKE_SOURCE_DIR}/src/api/java" + "${CMAKE_BINARY_DIR}/src/api/java" + ) + list(APPEND DOC_EXTRA_DEPENDS "build_z3_java_bindings") +else() + list(APPEND JAVA_API_OPTIONS "--no-java") +endif() + +option(ALWAYS_BUILD_DOCS "Always build documentation for API bindings" ON) +if (ALWAYS_BUILD_DOCS) + set(ALWAYS_BUILD_DOCS_ARG "ALL") +else() + set(ALWAYS_BUILD_DOCS_ARG "") + # FIXME: This sucks but there doesn't seem to be a way to make the top level + # install target depend on the `api_docs` target. + message(WARNING "Building documentation for API bindings is not part of the" + " all target. This may result in stale files being installed when running" + " the install target. You should run the api_docs target before running" + " the install target. Alternatively Set ALWAYS_BUILD_DOCS to ON to" + " automatically build documentation when running the install target." + ) +endif() + +add_custom_target(api_docs ${ALWAYS_BUILD_DOCS_ARG} + COMMAND + "${PYTHON_EXECUTABLE}" "${MK_API_DOC_SCRIPT}" + --doxygen-executable "${DOXYGEN_EXECUTABLE}" + --output-dir "${DOC_DEST_DIR}" + --temp-dir "${DOC_TEMP_DIR}" + ${PYTHON_API_OPTIONS} + ${DOTNET_API_OPTIONS} + ${JAVA_API_OPTIONS} + DEPENDS + ${DOC_EXTRA_DEPENDS} + BYPRODUCTS "${DOC_DEST_DIR}" + COMMENT "Generating documentation" + ${ADD_CUSTOM_TARGET_USES_TERMINAL_ARG} +) + +# Remove generated documentation when running `clean` target. +set_property(DIRECTORY APPEND PROPERTY + ADDITIONAL_MAKE_CLEAN_FILES + "${DOC_DEST_DIR}" +) + +option(INSTALL_API_BINDINGS_DOCUMENTATION "Install documentation for API bindings" ON) +set(CMAKE_INSTALL_API_BINDINGS_DOC + "${CMAKE_INSTALL_DOCDIR}" + CACHE + PATH + "Path to install documentation for API bindings" +) +if (INSTALL_API_BINDINGS_DOCUMENTATION) + install( + DIRECTORY "${DOC_DEST_DIR}" + DESTINATION "${CMAKE_INSTALL_API_BINDINGS_DOC}" + ) +endif() From 8032217fd1f190c06ff8e4f243cf5b7b566bc3ca Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Wed, 26 Apr 2017 13:53:37 -0700 Subject: [PATCH 488/562] tuning and fixing consequence finding, adding dimacs evaluation Signed-off-by: Nikolaj Bjorner --- contrib/cmake/src/test/CMakeLists.txt | 1 + src/sat/sat_solver.cpp | 18 ++++- src/sat/sat_solver.h | 1 + src/test/cnf_backbones.cpp | 106 ++++++++++++++++++++++++++ src/test/ddnf.cpp | 1 - src/test/main.cpp | 1 + 6 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 src/test/cnf_backbones.cpp diff --git a/contrib/cmake/src/test/CMakeLists.txt b/contrib/cmake/src/test/CMakeLists.txt index 6ea07e84c..46781b2cc 100644 --- a/contrib/cmake/src/test/CMakeLists.txt +++ b/contrib/cmake/src/test/CMakeLists.txt @@ -23,6 +23,7 @@ add_executable(test-z3 bv_simplifier_plugin.cpp chashtable.cpp check_assumptions.cpp + cnf_backbones.cpp datalog_parser.cpp ddnf.cpp diff_logic.cpp diff --git a/src/sat/sat_solver.cpp b/src/sat/sat_solver.cpp index 9031ad6b6..739a591e7 100644 --- a/src/sat/sat_solver.cpp +++ b/src/sat/sat_solver.cpp @@ -3284,13 +3284,17 @@ namespace sat { checkpoint(); literal_set::iterator it = unfixed_lits.begin(), end = unfixed_lits.end(); unsigned num_resolves = 0; + unsigned num_fixed = 0; + unsigned num_assigned = 0; lbool is_sat = l_true; for (; it != end; ++it) { - literal lit = *it; + literal lit = *it; if (value(lit) != l_undef) { + ++num_fixed; continue; } push(); + ++num_assigned; assign(~lit, justification()); propagate(false); while (inconsistent()) { @@ -3307,8 +3311,18 @@ namespace sat { break; } } + if (scope_lvl() == 1) { + it = unfixed_lits.begin(); + for (; it != end; ++it) { + literal lit = *it; + if (value(lit) == l_true) { + VERIFY(extract_fixed_consequences(lit, assumptions, unfixed_vars, conseq)); + } + } + } if (is_sat == l_true) { if (scope_lvl() == 1 && num_resolves > 0) { + IF_VERBOSE(1, verbose_stream() << "(sat.get-consequences backjump)\n";); is_sat = l_undef; } else { @@ -3331,6 +3345,8 @@ namespace sat { << " iterations: " << num_iterations << " variables: " << unfixed_lits.size() << " fixed: " << conseq.size() + << " status: " << is_sat + << " pre-assigned: " << num_fixed << " unfixed: " << lits.size() - conseq.size() - unfixed_lits.size() << ")\n";); diff --git a/src/sat/sat_solver.h b/src/sat/sat_solver.h index 42291609d..c7f472a60 100644 --- a/src/sat/sat_solver.h +++ b/src/sat/sat_solver.h @@ -247,6 +247,7 @@ namespace sat { unsigned num_clauses() const; unsigned num_restarts() const { return m_restarts; } bool is_external(bool_var v) const { return m_external[v] != 0; } + void set_external(bool_var v) { m_external[v] = true; } bool was_eliminated(bool_var v) const { return m_eliminated[v] != 0; } unsigned scope_lvl() const { return m_scope_lvl; } lbool value(literal l) const { return static_cast(m_assignment[l.index()]); } diff --git a/src/test/cnf_backbones.cpp b/src/test/cnf_backbones.cpp new file mode 100644 index 000000000..c562cfe58 --- /dev/null +++ b/src/test/cnf_backbones.cpp @@ -0,0 +1,106 @@ +/*++ +Copyright (c) 2017 Microsoft Corporation + +--*/ +#include +#include +#include +#include"timeout.h" +#include"rlimit.h" +#include"dimacs.h" +#include"sat_solver.h" +#include"gparams.h" + +static sat::solver * g_solver = 0; +static clock_t g_start_time; + +static void display_statistics() { + clock_t end_time = clock(); + if (g_solver) { + std::cout.flush(); + std::cerr.flush(); + + statistics st; + g_solver->collect_statistics(st); + st.update("total time", ((static_cast(end_time) - static_cast(g_start_time)) / CLOCKS_PER_SEC)); + st.display_smt2(std::cout); + } +} + +static void on_timeout() { + display_statistics(); + exit(0); +} + +static void STD_CALL on_ctrl_c(int) { + signal (SIGINT, SIG_DFL); + display_statistics(); + raise(SIGINT); +} + +static void display_model(sat::solver const & s) { + sat::model const & m = s.get_model(); + for (unsigned i = 1; i < m.size(); i++) { + switch (m[i]) { + case l_false: std::cout << "-" << i << " "; break; + case l_undef: break; + case l_true: std::cout << i << " "; break; + } + } + std::cout << "\n"; +} + + +static void cnf_backbones(char const* file_name) { + g_start_time = clock(); + register_on_timeout_proc(on_timeout); + signal(SIGINT, on_ctrl_c); + params_ref p = gparams::get_module("sat"); + p.set_bool("produce_models", true); + reslimit limit; + sat::solver solver(p, limit, 0); + g_solver = &solver; + + if (file_name) { + std::ifstream in(file_name); + if (in.bad() || in.fail()) { + std::cerr << "(error \"failed to open file '" << file_name << "'\")" << std::endl; + exit(ERR_OPEN_FILE); + } + parse_dimacs(in, solver); + } + else { + parse_dimacs(std::cin, solver); + } + IF_VERBOSE(20, solver.display_status(verbose_stream());); + + vector conseq; + sat::bool_var_vector vars; + sat::literal_vector assumptions; + for (unsigned i = 1; i < solver.num_vars(); ++i) { + vars.push_back(i); + solver.set_external(i); + } + lbool r = solver.get_consequences(assumptions, vars, conseq); + + switch (r) { + case l_true: + std::cout << "sat\n"; + std::cout << vars.size() << " " << conseq.size() << "\n"; + break; + case l_undef: + std::cout << "unknown\n"; + break; + case l_false: + std::cout << "unsat\n"; + break; + } + display_statistics(); +} + +void tst_cnf_backbones(char ** argv, int argc, int& i) { + if (i + 1 < argc) { + cnf_backbones(argv[i + 1]); + ++i; + } +} diff --git a/src/test/ddnf.cpp b/src/test/ddnf.cpp index d2ca92557..31503842d 100644 --- a/src/test/ddnf.cpp +++ b/src/test/ddnf.cpp @@ -1,4 +1,3 @@ - /*++ Copyright (c) 2015 Microsoft Corporation diff --git a/src/test/main.cpp b/src/test/main.cpp index 9239d0119..4a54797ac 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -229,6 +229,7 @@ int main(int argc, char ** argv) { TST(model_evaluator); TST(get_consequences); TST(pb2bv); + TST_ARGV(cnf_backbones); //TST_ARGV(hs); } From a048d74bae705b38c9155cf9758b606704f00942 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Wed, 26 Apr 2017 14:05:33 -0700 Subject: [PATCH 489/562] adding interval designator to output of non-optimal objectives, fix python doc test Signed-off-by: Nikolaj Bjorner --- src/api/python/z3/z3.py | 2 +- src/opt/opt_context.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/python/z3/z3.py b/src/api/python/z3/z3.py index 568ffe9a2..b4cdf834b 100644 --- a/src/api/python/z3/z3.py +++ b/src/api/python/z3/z3.py @@ -6194,7 +6194,7 @@ class Solver(Z3PPObject): >>> s.consequences([a],[b,c,d]) (sat, [Implies(a, b), Implies(a, c)]) >>> s.consequences([Not(c),d],[a,b,c,d]) - (sat, [Implies(Not(c), Not(c)), Implies(d, d), Implies(Not(c), Not(b)), Implies(Not(c), Not(a))]) + (sat, [Implies(d, d), Implies(Not(c), Not(c)), Implies(Not(c), Not(b)), Implies(Not(c), Not(a))]) """ if isinstance(assumptions, list): _asms = AstVector(None, self.ctx) diff --git a/src/opt/opt_context.cpp b/src/opt/opt_context.cpp index af3c57baa..1310727aa 100644 --- a/src/opt/opt_context.cpp +++ b/src/opt/opt_context.cpp @@ -1235,7 +1235,7 @@ namespace opt { out << " ("; display_objective(out, obj); if (get_lower_as_num(i) != get_upper_as_num(i)) { - out << " (" << get_lower(i) << " " << get_upper(i) << ")"; + out << " (interval " << get_lower(i) << " " << get_upper(i) << ")"; } else { out << " " << get_lower(i); From 46ac718790b5cfddc4d5ed215a301618285018de Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 26 Apr 2017 17:24:05 -0400 Subject: [PATCH 490/562] theory_str frontend changes --- src/ast/ast.cpp | 1 + src/ast/ast.h | 3 ++ src/ast/rewriter/seq_rewriter.cpp | 72 ++++++++++++++++++++++++++++--- src/ast/rewriter/seq_rewriter.h | 2 + src/ast/seq_decl_plugin.cpp | 4 +- 5 files changed, 76 insertions(+), 6 deletions(-) diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 7be7300a2..5f2de5170 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -17,6 +17,7 @@ Revision History: --*/ #include +#include #include"ast.h" #include"ast_pp.h" #include"ast_ll_pp.h" diff --git a/src/ast/ast.h b/src/ast/ast.h index 9259d5431..6bb3b01c9 100644 --- a/src/ast/ast.h +++ b/src/ast/ast.h @@ -117,6 +117,9 @@ public: explicit parameter(symbol const & s): m_kind(PARAM_SYMBOL) { new (m_symbol) symbol(s); } explicit parameter(rational const & r): m_kind(PARAM_RATIONAL) { new (m_rational) rational(r); } explicit parameter(double d):m_kind(PARAM_DOUBLE), m_dval(d) {} + explicit parameter(const char *s):m_kind(PARAM_SYMBOL) { + new (m_symbol) symbol(s); + } explicit parameter(unsigned ext_id, bool):m_kind(PARAM_EXTERNAL), m_ext_id(ext_id) {} parameter(parameter const&); diff --git a/src/ast/rewriter/seq_rewriter.cpp b/src/ast/rewriter/seq_rewriter.cpp index 3737f4651..85d2ba749 100644 --- a/src/ast/rewriter/seq_rewriter.cpp +++ b/src/ast/rewriter/seq_rewriter.cpp @@ -329,7 +329,8 @@ br_status seq_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con switch(f->get_decl_kind()) { case OP_SEQ_UNIT: - return BR_FAILED; + SASSERT(num_args == 1); + return mk_seq_unit(args[0], result); case OP_SEQ_EMPTY: return BR_FAILED; case OP_RE_PLUS: @@ -351,7 +352,8 @@ br_status seq_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con SASSERT(num_args == 2); return mk_re_union(args[0], args[1], result); case OP_RE_RANGE: - return BR_FAILED; + SASSERT(num_args == 2); + return mk_re_range(args[0], args[1], result); case OP_RE_INTERSECT: SASSERT(num_args == 2); return mk_re_inter(args[0], args[1], result); @@ -434,6 +436,33 @@ br_status seq_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con return BR_FAILED; } +/* + * (seq.unit (_ BitVector 8)) ==> String constant + */ +br_status seq_rewriter::mk_seq_unit(expr* e, expr_ref& result) { + sort * s = m().get_sort(e); + bv_util bvu(m()); + + if (is_sort_of(s, bvu.get_family_id(), BV_SORT)) { + // specifically we want (_ BitVector 8) + rational n_val; + unsigned int n_size; + if (bvu.is_numeral(e, n_val, n_size)) { + if (n_size == 8) { + // convert to string constant + char ch = (char)n_val.get_int32(); + TRACE("seq", tout << "rewrite seq.unit of 8-bit value " << n_val.to_string() << " to string constant \"" << ch << "\"" << std::endl;); + char s_tmp[2] = {ch, '\0'}; + symbol s(s_tmp); + result = m_util.str.mk_string(s); + return BR_DONE; + } + } + } + + return BR_FAILED; +} + /* string + string = string a + (b + c) = (a + b) + c @@ -1401,6 +1430,39 @@ br_status seq_rewriter::mk_re_star(expr* a, expr_ref& result) { return BR_FAILED; } +/* + * (re.range c_1 c_n) = (re.union (str.to.re c1) (str.to.re c2) ... (str.to.re cn)) + */ +br_status seq_rewriter::mk_re_range(expr* lo, expr* hi, expr_ref& result) { + TRACE("seq", tout << "rewrite re.range [" << mk_pp(lo, m()) << " " << mk_pp(hi, m()) << "]\n";); + zstring str_lo, str_hi; + if (m_util.str.is_string(lo, str_lo) && m_util.str.is_string(hi, str_hi)) { + if (str_lo.length() == 1 && str_hi.length() == 1) { + unsigned int c1 = str_lo[0]; + unsigned int c2 = str_hi[0]; + if (c1 > c2) { + // exchange c1 and c2 + unsigned int tmp = c1; + c2 = c1; + c1 = tmp; + } + zstring s(c1); + expr_ref acc(m_util.re.mk_to_re(m_util.str.mk_string(s)), m()); + for (unsigned int ch = c1 + 1; ch <= c2; ++ch) { + zstring s_ch(ch); + expr_ref acc2(m_util.re.mk_to_re(m_util.str.mk_string(s_ch)), m()); + acc = m_util.re.mk_union(acc, acc2); + } + result = acc; + return BR_REWRITE2; + } else { + m().raise_exception("string constants in re.range must have length 1"); + } + } + + return BR_FAILED; +} + /* emp+ = emp all+ = all @@ -1430,9 +1492,9 @@ br_status seq_rewriter::mk_re_plus(expr* a, expr_ref& result) { return BR_DONE; } - return BR_FAILED; -// result = m_util.re.mk_concat(a, m_util.re.mk_star(a)); -// return BR_REWRITE2; + //return BR_FAILED; + result = m_util.re.mk_concat(a, m_util.re.mk_star(a)); + return BR_REWRITE2; } br_status seq_rewriter::mk_re_opt(expr* a, expr_ref& result) { diff --git a/src/ast/rewriter/seq_rewriter.h b/src/ast/rewriter/seq_rewriter.h index 2b434f475..210b2d72c 100644 --- a/src/ast/rewriter/seq_rewriter.h +++ b/src/ast/rewriter/seq_rewriter.h @@ -98,6 +98,7 @@ class seq_rewriter { re2automaton m_re2aut; expr_ref_vector m_es, m_lhs, m_rhs; + br_status mk_seq_unit(expr* e, expr_ref& result); br_status mk_seq_concat(expr* a, expr* b, expr_ref& result); br_status mk_seq_length(expr* a, expr_ref& result); br_status mk_seq_extract(expr* a, expr* b, expr* c, expr_ref& result); @@ -119,6 +120,7 @@ class seq_rewriter { br_status mk_re_plus(expr* a, expr_ref& result); br_status mk_re_opt(expr* a, expr_ref& result); br_status mk_re_loop(unsigned num_args, expr* const* args, expr_ref& result); + br_status mk_re_range(expr* lo, expr* hi, expr_ref& result); bool set_empty(unsigned sz, expr* const* es, bool all, expr_ref_vector& lhs, expr_ref_vector& rhs); bool is_subsequence(unsigned n, expr* const* l, unsigned m, expr* const* r, diff --git a/src/ast/seq_decl_plugin.cpp b/src/ast/seq_decl_plugin.cpp index f282043e6..353fb975f 100644 --- a/src/ast/seq_decl_plugin.cpp +++ b/src/ast/seq_decl_plugin.cpp @@ -573,7 +573,7 @@ void seq_decl_plugin::set_manager(ast_manager* m, family_id id) { m_char = bv.mk_sort(8); m->inc_ref(m_char); parameter param(m_char); - m_string = m->mk_sort(symbol("String"), sort_info(m_family_id, SEQ_SORT, 1, ¶m)); + m_string = m->mk_sort(symbol("StringSequence"), sort_info(m_family_id, SEQ_SORT, 1, ¶m)); m->inc_ref(m_string); parameter paramS(m_string); m_re = m->mk_sort(m_family_id, RE_SORT, 1, ¶mS); @@ -831,7 +831,9 @@ void seq_decl_plugin::get_sort_names(svector & sort_names, symbol init(); sort_names.push_back(builtin_name("Seq", SEQ_SORT)); sort_names.push_back(builtin_name("RegEx", RE_SORT)); + // SMT-LIB 2.5 compatibility sort_names.push_back(builtin_name("String", _STRING_SORT)); + sort_names.push_back(builtin_name("StringSequence", _STRING_SORT)); } app* seq_decl_plugin::mk_string(symbol const& s) { From d3b30968fa3eba7b50ff27d538cea48d9777659f Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Wed, 26 Apr 2017 16:55:56 -0700 Subject: [PATCH 491/562] added chunk based backbone utility Signed-off-by: Nikolaj Bjorner --- src/test/cnf_backbones.cpp | 142 +++++++++++++++++++++++++++++++++---- 1 file changed, 127 insertions(+), 15 deletions(-) diff --git a/src/test/cnf_backbones.cpp b/src/test/cnf_backbones.cpp index c562cfe58..a4eaa4222 100644 --- a/src/test/cnf_backbones.cpp +++ b/src/test/cnf_backbones.cpp @@ -50,8 +50,123 @@ static void display_model(sat::solver const & s) { std::cout << "\n"; } +static void display_status(lbool r) { + switch (r) { + case l_true: + std::cout << "sat\n"; + break; + case l_undef: + std::cout << "unknown\n"; + break; + case l_false: + std::cout << "unsat\n"; + break; + } +} -static void cnf_backbones(char const* file_name) { +static void prune_unfixed(sat::literal_vector& lambda, sat::model const& m) { + for (unsigned i = 0; i < lambda.size(); ++i) { + if ((m[lambda[i].var()] == l_false) != lambda[i].sign()) { + lambda[i] = lambda.back(); + lambda.pop_back(); + --i; + } + } +} + +// Algorithm 7: Corebased Algorithm with Chunking + +static void back_remove(sat::literal_vector& lits, sat::literal l) { + for (unsigned i = lits.size(); i > 0; ) { + --i; + if (lits[i] == l) { + lits[i] = lits.back(); + lits.pop_back(); + return; + } + } + std::cout << "UNREACHABLE\n"; +} + +static void brute_force_consequences(sat::solver& s, sat::literal_vector const& gamma, sat::literal_vector& backbones) { + for (unsigned i = 0; i < gamma.size(); ++i) { + sat::literal nlit = ~gamma[i]; + lbool r = s.check(1, &nlit); + if (r == l_false) { + backbones.push_back(gamma[i]); + } + } +} + +static lbool core_chunking(sat::solver& s, sat::bool_var_vector& vars, vector& conseq, unsigned K) { + lbool r = s.check(); + display_status(r); + if (r != l_true) { + return r; + } + sat::model const & m = s.get_model(); + sat::literal_vector lambda, backbones; + for (unsigned i = 1; i < m.size(); i++) { + lambda.push_back(sat::literal(i, m[i] == l_false)); + } + while (!lambda.empty()) { + IF_VERBOSE(1, verbose_stream() << "(sat-backbone-core " << lambda.size() << " " << backbones.size() << ")\n";); + unsigned k = std::min(K, lambda.size()); + sat::literal_vector gamma, omegaN; + for (unsigned i = 0; i < k; ++i) { + sat::literal l = lambda[lambda.size() - i - 1]; + gamma.push_back(l); + omegaN.push_back(~l); + } + while (true) { + r = s.check(omegaN.size(), omegaN.c_ptr()); + if (r == l_true) { + IF_VERBOSE(1, verbose_stream() << "(sat) " << omegaN << "\n";); + prune_unfixed(lambda, s.get_model()); + break; + } + sat::literal_vector const& core = s.get_core(); + sat::literal_vector occurs; + IF_VERBOSE(1, verbose_stream() << "(core " << core.size() << ")\n";); + for (unsigned i = 0; i < omegaN.size(); ++i) { + if (core.contains(omegaN[i])) { + occurs.push_back(omegaN[i]); + } + } + if (occurs.size() == 1) { + sat::literal lit = occurs.back(); + sat::literal nlit = ~lit; + backbones.push_back(~lit); + back_remove(lambda, ~lit); + back_remove(gamma, ~lit); + s.mk_clause(1, &nlit); + } + for (unsigned i = 0; i < omegaN.size(); ++i) { + if (occurs.contains(omegaN[i])) { + omegaN[i] = omegaN.back(); + omegaN.pop_back(); + --i; + } + } + if (omegaN.empty() && occurs.size() > 1) { + brute_force_consequences(s, gamma, backbones); + for (unsigned i = 0; i < gamma.size(); ++i) { + back_remove(lambda, gamma[i]); + } + break; + } + } + } + for (unsigned i = 0; i < backbones.size(); ++i) { + sat::literal_vector cons; + cons.push_back(backbones[i]); + conseq.push_back(cons); + } + return l_true; +} + + +static void cnf_backbones(bool use_chunk, char const* file_name) { g_start_time = clock(); register_on_timeout_proc(on_timeout); signal(SIGINT, on_ctrl_c); @@ -81,26 +196,23 @@ static void cnf_backbones(char const* file_name) { vars.push_back(i); solver.set_external(i); } - lbool r = solver.get_consequences(assumptions, vars, conseq); - - switch (r) { - case l_true: - std::cout << "sat\n"; - std::cout << vars.size() << " " << conseq.size() << "\n"; - break; - case l_undef: - std::cout << "unknown\n"; - break; - case l_false: - std::cout << "unsat\n"; - break; + lbool r; + if (use_chunk) { + r = core_chunking(solver, vars, conseq, 100); } + else { + r = solver.get_consequences(assumptions, vars, conseq); + } + std::cout << vars.size() << " " << conseq.size() << "\n"; + display_status(r); display_statistics(); } void tst_cnf_backbones(char ** argv, int argc, int& i) { if (i + 1 < argc) { - cnf_backbones(argv[i + 1]); + bool use_chunk = (i + 2 < argc && argv[i + 2] == std::string("chunk")); + cnf_backbones(use_chunk, argv[i + 1]); ++i; + if (use_chunk) ++i; } } From 334677a7eb4f5a2395cb621dfe24843561e3fca7 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 27 Apr 2017 13:58:36 -0400 Subject: [PATCH 492/562] fix is_string_term() --- src/ast/seq_decl_plugin.h | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/ast/seq_decl_plugin.h b/src/ast/seq_decl_plugin.h index 52abb2c45..833455ff4 100644 --- a/src/ast/seq_decl_plugin.h +++ b/src/ast/seq_decl_plugin.h @@ -275,18 +275,12 @@ public: bool is_string_term(expr const * n) const { sort * s = get_sort(n); - return is_sort_of(s, m_fid, _STRING_SORT); + return (u.is_seq(s) && u.is_string(s)); } bool is_non_string_sequence(expr const * n) const { - if (is_string_term(n)) - return false; - sort * s = get_sort(n); - if (u.is_seq(s) && !u.is_string(s)) { - return true; - } - return false; + return (u.is_seq(s) && !u.is_string(s)); } MATCH_BINARY(is_concat); From 7811a91bad1d1f4586f82d221049de956567368a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 27 Apr 2017 13:59:02 -0400 Subject: [PATCH 493/562] fix is_string_term() --- src/ast/seq_decl_plugin.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/ast/seq_decl_plugin.h b/src/ast/seq_decl_plugin.h index 2882e905d..833455ff4 100644 --- a/src/ast/seq_decl_plugin.h +++ b/src/ast/seq_decl_plugin.h @@ -273,6 +273,15 @@ public: bool is_in_re(expr const* n) const { return is_app_of(n, m_fid, OP_SEQ_IN_RE); } bool is_unit(expr const* n) const { return is_app_of(n, m_fid, OP_SEQ_UNIT); } + bool is_string_term(expr const * n) const { + sort * s = get_sort(n); + return (u.is_seq(s) && u.is_string(s)); + } + + bool is_non_string_sequence(expr const * n) const { + sort * s = get_sort(n); + return (u.is_seq(s) && !u.is_string(s)); + } MATCH_BINARY(is_concat); MATCH_UNARY(is_length); From aedabfff7af810e427118e75f3e73dafaef5d014 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Thu, 27 Apr 2017 11:24:30 -0700 Subject: [PATCH 494/562] disable newer pb encoding Signed-off-by: Nikolaj Bjorner --- src/ast/rewriter/pb2bv_rewriter.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ast/rewriter/pb2bv_rewriter.cpp b/src/ast/rewriter/pb2bv_rewriter.cpp index d85bdec41..3a134a13c 100644 --- a/src/ast/rewriter/pb2bv_rewriter.cpp +++ b/src/ast/rewriter/pb2bv_rewriter.cpp @@ -113,12 +113,15 @@ struct pb2bv_rewriter::imp { return expr_ref((is_le == l_false)?m.mk_true():m.mk_false(), m); } +#if 0 expr_ref result(m); switch (is_le) { case l_true: if (mk_le_tot(sz, args, k, result)) return result; else break; case l_false: if (mk_ge_tot(sz, args, k, result)) return result; else break; case l_undef: break; } +#endif + #if 0 expr_ref result(m); switch (is_le) { From 12dd6d786b238d35ddcebb412d51516aec7a56b1 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 27 Apr 2017 21:24:40 -0400 Subject: [PATCH 495/562] remove redundant is_seq() check --- src/ast/seq_decl_plugin.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ast/seq_decl_plugin.h b/src/ast/seq_decl_plugin.h index 833455ff4..76b5ebe31 100644 --- a/src/ast/seq_decl_plugin.h +++ b/src/ast/seq_decl_plugin.h @@ -275,7 +275,7 @@ public: bool is_string_term(expr const * n) const { sort * s = get_sort(n); - return (u.is_seq(s) && u.is_string(s)); + return u.is_string(s); } bool is_non_string_sequence(expr const * n) const { From 05958193ab3dc8f1780f1c9e2ae9c1ad7ecc4855 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Thu, 27 Apr 2017 22:30:02 -0400 Subject: [PATCH 496/562] revert change to String sort declaration --- src/ast/seq_decl_plugin.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ast/seq_decl_plugin.cpp b/src/ast/seq_decl_plugin.cpp index 353fb975f..bf238d8c5 100644 --- a/src/ast/seq_decl_plugin.cpp +++ b/src/ast/seq_decl_plugin.cpp @@ -573,7 +573,7 @@ void seq_decl_plugin::set_manager(ast_manager* m, family_id id) { m_char = bv.mk_sort(8); m->inc_ref(m_char); parameter param(m_char); - m_string = m->mk_sort(symbol("StringSequence"), sort_info(m_family_id, SEQ_SORT, 1, ¶m)); + m_string = m->mk_sort(symbol("String"), sort_info(m_family_id, SEQ_SORT, 1, ¶m)); m->inc_ref(m_string); parameter paramS(m_string); m_re = m->mk_sort(m_family_id, RE_SORT, 1, ¶mS); From f1cee803e83bdac80f350e28693ae6801ec0674a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 28 Apr 2017 13:44:48 -0400 Subject: [PATCH 497/562] fixup --- src/ast/seq_decl_plugin.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ast/seq_decl_plugin.cpp b/src/ast/seq_decl_plugin.cpp index 353fb975f..bf238d8c5 100644 --- a/src/ast/seq_decl_plugin.cpp +++ b/src/ast/seq_decl_plugin.cpp @@ -573,7 +573,7 @@ void seq_decl_plugin::set_manager(ast_manager* m, family_id id) { m_char = bv.mk_sort(8); m->inc_ref(m_char); parameter param(m_char); - m_string = m->mk_sort(symbol("StringSequence"), sort_info(m_family_id, SEQ_SORT, 1, ¶m)); + m_string = m->mk_sort(symbol("String"), sort_info(m_family_id, SEQ_SORT, 1, ¶m)); m->inc_ref(m_string); parameter paramS(m_string); m_re = m->mk_sort(m_family_id, RE_SORT, 1, ¶mS); From d51ebac10a19520917d3725279a3a421e3e2279d Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 28 Apr 2017 14:01:44 -0400 Subject: [PATCH 498/562] remove references to str_fid --- src/ast/ast_smt_pp.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ast/ast_smt_pp.cpp b/src/ast/ast_smt_pp.cpp index c3f1523b1..f41350dc5 100644 --- a/src/ast/ast_smt_pp.cpp +++ b/src/ast/ast_smt_pp.cpp @@ -166,7 +166,6 @@ class smt_printer { fpa_util m_futil; family_id m_basic_fid; family_id m_bv_fid; - family_id m_str_fid; family_id m_arith_fid; family_id m_array_fid; family_id m_dt_fid; @@ -846,7 +845,6 @@ public: m_bv_fid = m.mk_family_id("bv"); m_arith_fid = m.mk_family_id("arith"); m_array_fid = m.mk_family_id("array"); - m_str_fid = m.mk_family_id("str"); m_dt_fid = m.mk_family_id("datatype"); m_fpa_fid = m.mk_family_id("fpa"); } From 88147f7047f85b64c26660717871e0d6be8c3eeb Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 28 Apr 2017 14:14:28 -0400 Subject: [PATCH 499/562] theory_str static features and cmd_context --- src/ast/static_features.cpp | 8 ++++++++ src/ast/static_features.h | 4 ++++ src/cmd_context/cmd_context.cpp | 7 +++++-- src/cmd_context/cmd_context.h | 1 + 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/ast/static_features.cpp b/src/ast/static_features.cpp index 328128794..daf20e095 100644 --- a/src/ast/static_features.cpp +++ b/src/ast/static_features.cpp @@ -25,6 +25,7 @@ static_features::static_features(ast_manager & m): m_bvutil(m), m_arrayutil(m), m_fpautil(m), + m_sequtil(m), m_bfid(m.get_basic_family_id()), m_afid(m.mk_family_id("arith")), m_lfid(m.mk_family_id("label")), @@ -77,6 +78,8 @@ void static_features::reset() { m_has_real = false; m_has_bv = false; m_has_fpa = false; + m_has_str = false; + m_has_seq_non_str = false; m_has_arrays = false; m_arith_k_sum .reset(); m_num_arith_terms = 0; @@ -279,6 +282,11 @@ void static_features::update_core(expr * e) { m_has_fpa = true; if (!m_has_arrays && m_arrayutil.is_array(e)) m_has_arrays = true; + if (!m_has_str && m_sequtil.str.is_string_term(e)) + m_has_str = true; + if (!m_has_seq_non_str && m_sequtil.str.is_non_string_sequence(e)) { + m_has_seq_non_str = true; + } if (is_app(e)) { family_id fid = to_app(e)->get_family_id(); mark_theory(fid); diff --git a/src/ast/static_features.h b/src/ast/static_features.h index 8b20c5463..e7f69e041 100644 --- a/src/ast/static_features.h +++ b/src/ast/static_features.h @@ -24,6 +24,7 @@ Revision History: #include"bv_decl_plugin.h" #include"array_decl_plugin.h" #include"fpa_decl_plugin.h" +#include"seq_decl_plugin.h" #include"map.h" struct static_features { @@ -32,6 +33,7 @@ struct static_features { bv_util m_bvutil; array_util m_arrayutil; fpa_util m_fpautil; + seq_util m_sequtil; family_id m_bfid; family_id m_afid; family_id m_lfid; @@ -77,6 +79,8 @@ struct static_features { bool m_has_real; // bool m_has_bv; // bool m_has_fpa; // + bool m_has_str; // has String-typed terms + bool m_has_seq_non_str; // has non-String-typed Sequence terms bool m_has_arrays; // rational m_arith_k_sum; // sum of the numerals in arith atoms. unsigned m_num_arith_terms; diff --git a/src/cmd_context/cmd_context.cpp b/src/cmd_context/cmd_context.cpp index 7060d79ad..f590725a7 100644 --- a/src/cmd_context/cmd_context.cpp +++ b/src/cmd_context/cmd_context.cpp @@ -249,6 +249,7 @@ protected: array_util m_arutil; fpa_util m_futil; seq_util m_sutil; + datalog::dl_decl_util m_dlutil; format_ns::format * pp_fdecl_name(symbol const & s, func_decls const & fs, func_decl * f, unsigned & len) { @@ -277,6 +278,7 @@ public: virtual array_util & get_arutil() { return m_arutil; } virtual fpa_util & get_futil() { return m_futil; } virtual seq_util & get_sutil() { return m_sutil; } + virtual datalog::dl_decl_util& get_dlutil() { return m_dlutil; } virtual bool uses(symbol const & s) const { return @@ -527,6 +529,9 @@ bool cmd_context::logic_has_fpa() const { return !has_logic() || smt_logics::logic_has_fpa(m_logic); } +bool cmd_context::logic_has_str() const { + return !has_logic() || m_logic == "QF_S"; +} bool cmd_context::logic_has_array() const { return !has_logic() || smt_logics::logic_has_array(m_logic); @@ -568,7 +573,6 @@ void cmd_context::init_manager_core(bool new_manager) { load_plugin(symbol("seq"), logic_has_seq(), fids); load_plugin(symbol("fpa"), logic_has_fpa(), fids); load_plugin(symbol("pb"), logic_has_pb(), fids); - svector::iterator it = fids.begin(); svector::iterator end = fids.end(); for (; it != end; ++it) { @@ -616,7 +620,6 @@ void cmd_context::init_external_manager() { init_manager_core(false); } - bool cmd_context::set_logic(symbol const & s) { if (has_logic()) throw cmd_exception("the logic has already been set"); diff --git a/src/cmd_context/cmd_context.h b/src/cmd_context/cmd_context.h index 92943c71c..8885bc5d6 100644 --- a/src/cmd_context/cmd_context.h +++ b/src/cmd_context/cmd_context.h @@ -257,6 +257,7 @@ protected: bool logic_has_array() const; bool logic_has_datatype() const; bool logic_has_fpa() const; + bool logic_has_str() const; void print_unsupported_msg() { regular_stream() << "unsupported" << std::endl; } void print_unsupported_info(symbol const& s, int line, int pos) { if (s != symbol::null) diagnostic_stream() << "; " << s << " line: " << line << " position: " << pos << std::endl;} From 4cc2b292c0cc8759da7a525e63dbfefdb06d6a01 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Fri, 28 Apr 2017 17:59:04 +0100 Subject: [PATCH 500/562] [CMake] Remove compiler flag overrides and support for C language. The setting of overrides was broken (the CXX flags were not set but the C flags were) and we aren't even using the C compiler any more. The C compiler is used by the example C project but that is built as an external project now so we don't need C support anymore. The setting of defaults was also very fragile. CMake has quite complicated support here (e.g. MSVC with a clang based tool chain) which would likely not work properly with the override approach as it existed. This means we loose some of the custom linker flags we were setting for MSVC but we were never doing a great job of replicating the exact set of flags used in the old build system anyway. Subsequent commits will gradually fix this. --- CMakeLists.txt | 4 +- .../cmake/cmake/compiler_flags_override.cmake | 43 ------------------- 2 files changed, 1 insertion(+), 46 deletions(-) delete mode 100644 contrib/cmake/cmake/compiler_flags_override.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 47a081e75..ab3976aea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,9 +24,7 @@ if (NOT (EXISTS "${CMAKE_SOURCE_DIR}/src/CMakeLists.txt")) "``python contrib/cmake/bootstrap.py create``") endif() -# This overrides the default flags for the different CMAKE_BUILD_TYPEs -set(CMAKE_USER_MAKE_RULES_OVERRIDE "${CMAKE_CURRENT_SOURCE_DIR}/cmake/compiler_flags_override.cmake") -project(Z3 C CXX) +project(Z3 CXX) ################################################################################ # Project version diff --git a/contrib/cmake/cmake/compiler_flags_override.cmake b/contrib/cmake/cmake/compiler_flags_override.cmake deleted file mode 100644 index c6005396b..000000000 --- a/contrib/cmake/cmake/compiler_flags_override.cmake +++ /dev/null @@ -1,43 +0,0 @@ -# This file overrides the default compiler flags for CMake's built-in -# configurations (CMAKE_BUILD_TYPE). Most compiler flags should not be set here. -# The main purpose is to have very fine grained control of the compiler flags. -if (CMAKE_C_COMPILER_ID) - set(_lang C) -elseif(CMAKE_CXX_COMPILER_ID) - set(_lang CXX) -else() - message(FATAL_ERROR "Unknown language") -endif() - -# TODO: The value of doing this is debatable. The flags set here are pretty -# much the CMake defaults now (they didn't use to be) and makes extra work for -# us when supporting different compilers. Perhaps we should move the remaining -# code that sets non-default flags out into the CMakeLists.txt files and remove -# any overrides here? -if (("${CMAKE_${_lang}_COMPILER_ID}" MATCHES "Clang") OR ("${CMAKE_${_lang}_COMPILER_ID}" MATCHES "GNU")) - # Taken from Modules/Compiler/GNU.cmake - set(CMAKE_${_lang}_FLAGS_INIT "") - set(CMAKE_${_lang}_FLAGS_DEBUG_INIT "-g -O0") - set(CMAKE_${_lang}_FLAGS_MINSIZEREL_INIT "-Os -DNDEBUG") - set(CMAKE_${_lang}_FLAGS_RELEASE_INIT "-O3 -DNDEBUG") - set(CMAKE_${_lang}_FLAGS_RELWITHDEBINFO_INIT "-O2 -g -DNDEBUG") - # FIXME: Remove "x.." when CMP0054 is set to NEW -elseif ("x${CMAKE_${_lang}_COMPILER_ID}" STREQUAL "xMSVC") - # FIXME: Perhaps we should be using /MD instead? - set(CMAKE_${_lang}_FLAGS_DEBUG_INIT "/MTd /Zi /Ob0 /Od /RTC1") - set(CMAKE_${_lang}_FLAGS_MINSIZEREL_INIT "/MT /O1 /Ob1 /D NDEBUG") - set(CMAKE_${_lang}_FLAGS_RELEASE_INIT "/MT /O2 /Ob2 /D NDEBUG") - set(CMAKE_${_lang}_FLAGS_RELWITHDEBINFO_INIT "/MT /Zi /O2 /Ob1 /D NDEBUG") - # Override linker flags (see Windows-MSVC.cmake for CMake's defaults) - # The stack size comes from the Python build system. - set(_msvc_stack_size "8388608") - set(CMAKE_EXE_LINKER_FLAGS_DEBUG_INIT "/debug /INCREMENTAL:NO /STACK:${_msvc_stack_size}") - set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO_INIT "/debug /INCREMENTAL:NO /STACK:${_msvc_stack_size}") - set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL_INIT "/INCREMENTAL:NO /STACK:${_msvc_stack_size}") - set(CMAKE_EXE_LINKER_FLAGS_RELEASE_INIT "/INCREMENTAL:NO /STACK:${_msvc_stack_size}") - unset(_msvc_stack_size) -else() - message(FATAL_ERROR "Overrides not set for ${_lang} compiler \"${CMAKE_${_lang}_COMPILER_ID}\"") -endif() - -unset(_lang) From f568b2478ff743b735f5e1ab03325040ea320cc9 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Fri, 28 Apr 2017 21:30:36 +0100 Subject: [PATCH 501/562] [CMake] Report the various values of CMAKE_CXX_FLAGS, CMAKE_CXX_FLAGS_, CMAKE__LINKER_FLAGS, and CMAKE__LINKER_FLAGS_. This is useful for debugging where some flags come from. Now that we don't explicitly set the defaults it useful to see which default values we are getting from CMake. --- CMakeLists.txt | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ab3976aea..795a23320 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -153,13 +153,13 @@ set(Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS "") # Build type ################################################################################ message(STATUS "CMake generator: ${CMAKE_GENERATOR}") +set(available_build_types Debug Release RelWithDebInfo MinSizeRel) if (DEFINED CMAKE_CONFIGURATION_TYPES) # Multi-configuration build (e.g. Visual Studio and Xcode). Here # CMAKE_BUILD_TYPE doesn't matter message(STATUS "Available configurations: ${CMAKE_CONFIGURATION_TYPES}") else() # Single configuration generator (e.g. Unix Makefiles, Ninja) - set(available_build_types Debug Release RelWithDebInfo MinSizeRel) if(NOT CMAKE_BUILD_TYPE) message(STATUS "CMAKE_BUILD_TYPE is not set. Setting default") message(STATUS "The available build types are: ${available_build_types}") @@ -374,6 +374,28 @@ if (BUILD_LIBZ3_SHARED) endif() endif() +################################################################################ +# Report default CMake flags +################################################################################ +# This is mainly for debugging. +message(STATUS "CMAKE_CXX_FLAGS: \"${CMAKE_CXX_FLAGS}\"") +message(STATUS "CMAKE_EXE_LINKER_FLAGS: \"${CMAKE_EXE_LINKER_FLAGS}\"") +message(STATUS "CMAKE_STATIC_LINKER_FLAGS: \"${CMAKE_STATIC_LINKER_FLAGS}\"") +message(STATUS "CMAKE_SHARED_LINKER_FLAGS: \"${CMAKE_SHARED_LINKER_FLAGS}\"") +if (DEFINED CMAKE_CONFIGURATION_TYPES) + # Multi configuration generator + string(TOUPPER "${available_build_types}" build_types_to_report) +else() + # Single configuration generator + string(TOUPPER "${CMAKE_BUILD_TYPE}" build_types_to_report) +endif() +foreach (_build_type ${build_types_to_report}) + message(STATUS "CMAKE_CXX_FLAGS_${_build_type}: \"${CMAKE_CXX_FLAGS_${_build_type}}\"") + message(STATUS "CMAKE_EXE_LINKER_FLAGS_${_build_type}: \"${CMAKE_EXE_LINKER_FLAGS_${_build_type}}\"") + message(STATUS "CMAKE_SHARED_LINKER_FLAGS_${_build_type}: \"${CMAKE_SHARED_LINKER_FLAGS_${_build_type}}\"") + message(STATUS "CMAKE_STATIC_LINKER_FLAGS_${_build_type}: \"${CMAKE_STATIC_LINKER_FLAGS_${_build_type}}\"") +endforeach() + ################################################################################ # Report Z3_COMPONENT flags ################################################################################ From fe1af4bcdb7c4f4d85874e449fae3db8cf0a49ad Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Fri, 28 Apr 2017 23:28:04 +0100 Subject: [PATCH 502/562] [CMake] Teach build system to pass `/fp:precise` to compiler when using MSVC. This is set by the old build system but we weren't setting it. This actually MSVC's default but in an effort to try to behave more like the old build system we will set it anyway. --- CMakeLists.txt | 9 +++++++++ contrib/cmake/cmake/z3_add_cxx_flag.cmake | 1 + 2 files changed, 10 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 795a23320..6e858a421 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -324,6 +324,15 @@ if (("${TARGET_ARCHITECTURE}" STREQUAL "x86_64") OR ("${TARGET_ARCHITECTURE}" ST unset(SSE_FLAGS) endif() +# FIXME: Remove "x.." when CMP0054 is set to NEW +if ("x${CMAKE_CXX_COMPILER_ID}" STREQUAL "xMSVC") + # This is the default for MSVC already but to replicate the + # python/Makefile build system behaviour this flag is set + # explicitly. + z3_add_cxx_flag("/fp:precise" REQUIRED) +endif() +# There doesn't seem to be an equivalent for clang/gcc + ################################################################################ # Threading support ################################################################################ diff --git a/contrib/cmake/cmake/z3_add_cxx_flag.cmake b/contrib/cmake/cmake/z3_add_cxx_flag.cmake index 0c56bb0f6..8bffd7de3 100644 --- a/contrib/cmake/cmake/z3_add_cxx_flag.cmake +++ b/contrib/cmake/cmake/z3_add_cxx_flag.cmake @@ -7,6 +7,7 @@ function(z3_add_cxx_flag flag) string(REPLACE "/" "_" SANITIZED_FLAG_NAME "${SANITIZED_FLAG_NAME}") string(REPLACE "=" "_" SANITIZED_FLAG_NAME "${SANITIZED_FLAG_NAME}") string(REPLACE " " "_" SANITIZED_FLAG_NAME "${SANITIZED_FLAG_NAME}") + string(REPLACE ":" "_" SANITIZED_FLAG_NAME "${SANITIZED_FLAG_NAME}") unset(HAS_${SANITIZED_FLAG_NAME}) CHECK_CXX_COMPILER_FLAG("${flag}" HAS_${SANITIZED_FLAG_NAME}) if (z3_add_flag_REQUIRED AND NOT HAS_${SANITIZED_FLAG_NAME}) From fb403229bd7a2bc422afe574c98dd858035c42aa Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Sat, 29 Apr 2017 00:29:26 +0100 Subject: [PATCH 503/562] [CMake] CMake's default value for CMAKE_CXX_FLAGS includes `/W3` remove this so we can have fine grained control of warnings. --- contrib/cmake/cmake/compiler_warnings.cmake | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/contrib/cmake/cmake/compiler_warnings.cmake b/contrib/cmake/cmake/compiler_warnings.cmake index c214e4464..e49e43947 100644 --- a/contrib/cmake/cmake/compiler_warnings.cmake +++ b/contrib/cmake/cmake/compiler_warnings.cmake @@ -15,6 +15,13 @@ elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") # FIXME: Remove "x.." when CMP0054 is set to NEW elseif ("x${CMAKE_CXX_COMPILER_ID}" STREQUAL "xMSVC") list(APPEND WARNING_FLAGS_TO_CHECK ${MSVC_WARNINGS}) + + # CMake's default flags include /W3 already so remove them if + # they already exist. + if ("${CMAKE_CXX_FLAGS}" MATCHES "/W3") + string(REPLACE "/W3" "" _cmake_cxx_flags_remove_w3 "${CMAKE_CXX_FLAGS}") + set(CMAKE_CXX_FLAGS "${_cmake_cxx_flags_remove_w3}" CACHE STRING "" FORCE) + endif() else() message(AUTHOR_WARNING "Unknown compiler") endif() From 0e1343e78db9d396240151af436a1a2028cc40fa Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Sat, 29 Apr 2017 01:21:49 +0100 Subject: [PATCH 504/562] [CMake] Add support for link time optimization (LTO). This analogous to the `--optimize` flag in the Python/Makefile build system except that we now support doing LTO with Clang/GCC as well. However it is probably best to avoid doing LTO with Clang or GCC for now because I see a bunch of warnings about ODR violations when building with LTO. LTO can be enabled with the new `LINK_TIME_OPTIMIZATION` option which is off by default. --- CMakeLists.txt | 5 +++ README-CMake.md | 1 + contrib/cmake/cmake/compiler_lto.cmake | 52 ++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 contrib/cmake/cmake/compiler_lto.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 6e858a421..8afc6339b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -383,6 +383,11 @@ if (BUILD_LIBZ3_SHARED) endif() endif() +################################################################################ +# Link time optimization +################################################################################ +include(${CMAKE_SOURCE_DIR}/cmake/compiler_lto.cmake) + ################################################################################ # Report default CMake flags ################################################################################ diff --git a/README-CMake.md b/README-CMake.md index 4bbbd36a8..9289d40f1 100644 --- a/README-CMake.md +++ b/README-CMake.md @@ -292,6 +292,7 @@ The following useful options can be passed to CMake whilst configuring. when running the ``install`` target. * ``ALWAYS_BUILD_DOCS`` - BOOL. If set to ``TRUE`` and ``BUILD_DOCUMENTATION`` is ``TRUE`` then documentation for API bindings will always be built. Disabling this is useful for faster incremental builds. The documentation can be manually built by invoking the ``api_docs`` target. +* ``LINK_TIME_OPTIMIZATION`` - BOOL. If set to ``TRUE`` link time optimization will be enabled. On the command line these can be passed to ``cmake`` using the ``-D`` option. In ``ccmake`` and ``cmake-gui`` these can be set in the user interface. diff --git a/contrib/cmake/cmake/compiler_lto.cmake b/contrib/cmake/cmake/compiler_lto.cmake new file mode 100644 index 000000000..b90890f59 --- /dev/null +++ b/contrib/cmake/cmake/compiler_lto.cmake @@ -0,0 +1,52 @@ +option(LINK_TIME_OPTIMIZATION "Use link time optimiziation" OFF) + +if (LINK_TIME_OPTIMIZATION) + message(STATUS "LTO enabled") + set(build_types_with_lto "RELEASE" "RELWITHDEBINFO") + if (DEFINED CMAKE_CONFIGURATION_TYPES) + # Multi configuration generator + message(STATUS "Note LTO is only enabled for the following configurations: ${build_types_with_lto}") + else() + # Single configuration generator + string(TOUPPER "${CMAKE_BUILD_TYPE}" _build_type_upper) + list(FIND build_types_with_lto "${_build_type_upper}" _index) + if ("${_index}" EQUAL -1) + message(FATAL_ERROR "Configuration ${CMAKE_BUILD_TYPE} does not support LTO." + "You should set LINK_TIME_OPTIMIZATION to OFF.") + endif() + endif() + + set(_lto_compiler_flag "") + set(_lto_linker_flag "") + if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") OR + ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")) + set(_lto_compiler_flag "-flto") + set(_lto_linker_flag "-flto") + # FIXME: Remove "x.." when CMP0054 is set to NEW + elseif ("x${CMAKE_CXX_COMPILER_ID}" STREQUAL "xMSVC") + set(_lto_compiler_flag "/GL") + set(_lto_linker_flag "/LTCG") + else() + message(FATAL_ERROR "Can't enable LTO for compiler \"${CMAKE_CXX_COMPILER_ID}\"." + "You should set LINK_TIME_OPTIMIZATION to OFF.") + endif() + CHECK_CXX_COMPILER_FLAG("${_lto_compiler_flag}" HAS_LTO) + if (NOT HAS_LTO) + message(FATAL_ERROR "Compiler does not support LTO") + endif() + + foreach (_config ${build_types_with_lto}) + # Set flags compiler and linker flags globally rather than using + # `Z3_COMPONENT_CXX_FLAGS` and `Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS` + # respectively. We need per configuration compiler and linker flags. The + # `LINK_FLAGS` property (which we populate with + # `Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS`) doesn't seem to support generator + # expressions so we can't do `$<$:${_lto_linker_flag}>`. + set(CMAKE_CXX_FLAGS_${_config} "${CMAKE_CXX_FLAGS_${_config}} ${_lto_compiler_flag}") + set(CMAKE_EXE_LINKER_FLAGS_${_config} "${CMAKE_EXE_LINKER_FLAGS_${_config}} ${_lto_linker_flag}") + set(CMAKE_SHARED_LINKER_FLAGS_${_config} "${CMAKE_SHARED_LINKER_FLAGS_${_config}} ${_lto_linker_flag}") + set(CMAKE_STATIC_LINKER_FLAGS_${_config} "${CMAKE_STATIC_LINKER_FLAGS_${_config}} ${_lto_linker_flag}") + endforeach() +else() + message(STATUS "LTO disabled") +endif() From 870be706e958f37d6bc7a0ca0347704b56a9c08d Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Sat, 29 Apr 2017 11:00:27 +0100 Subject: [PATCH 505/562] [CMake] Try to do a better job of matching the old build system's compiler defines and flags when using MSVC. There are lots of defines and flags that I'm unsure about so in some cases I've changed the behaviour slightly (if I'm confident the behaviour in the old build system is wrong) or not added the flag/define at all but just left comments noting what the old build system did and why I disagree with the old build system's choices. --- CMakeLists.txt | 8 ++ contrib/cmake/cmake/msvc_legacy_quirks.cmake | 112 +++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 contrib/cmake/cmake/msvc_legacy_quirks.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 8afc6339b..aa347ae66 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -388,6 +388,14 @@ endif() ################################################################################ include(${CMAKE_SOURCE_DIR}/cmake/compiler_lto.cmake) +################################################################################ +# MSVC specific flags inherited from old build system +################################################################################ +# FIXME: Remove "x.." when CMP0054 is set to NEW +if ("x${CMAKE_CXX_COMPILER_ID}" STREQUAL "xMSVC") + include(${CMAKE_SOURCE_DIR}/cmake/msvc_legacy_quirks.cmake) +endif() + ################################################################################ # Report default CMake flags ################################################################################ diff --git a/contrib/cmake/cmake/msvc_legacy_quirks.cmake b/contrib/cmake/cmake/msvc_legacy_quirks.cmake new file mode 100644 index 000000000..050d34553 --- /dev/null +++ b/contrib/cmake/cmake/msvc_legacy_quirks.cmake @@ -0,0 +1,112 @@ +# This file ether sets or notes various compiler and linker flags for MSVC that +# were defined by the old python/Makefile based build system but +# don't obviously belong in the other sections in the CMake build system. + +################################################################################ +# Compiler definitions +################################################################################ +# FIXME: All the commented out defines should be removed once +# we are confident it is correct to not set them. +set(Z3_MSVC_LEGACY_DEFINES + # Don't set `_DEBUG`. The old build sytem sets this but this + # is wrong. MSVC will set this depending on which runtime is being used. + # See https://msdn.microsoft.com/en-us/library/b0084kay.aspx + # _DEBUG + + # The old build system only set `UNICODE` and `_UNICODE` for x86_64 release. + # That seems completly wrong so set it for all configurations. + # According to https://blogs.msdn.microsoft.com/oldnewthing/20040212-00/?p=40643/ + # `UNICODE` affects Windows headers and `_UNICODE` affects C runtime header files. + # There is some discussion of this define at https://msdn.microsoft.com/en-us/library/dybsewaf.aspx + UNICODE + _UNICODE +) + +if ("${TARGET_ARCHITECTURE}" STREQUAL "x86_64") + list(APPEND Z3_MSVC_LEGACY_DEFINES "" + # Don't set `_LIB`. The old build system sets this for x86_64 release + # build. This flag doesn't seem to be documented but a stackoverflow + # post hints that this is usually set when building a static library. + # See http://stackoverflow.com/questions/35034683/how-to-tell-if-current-project-is-dll-or-static-lib + # This seems wrong give that the old build system set this regardless + # whether or not libz3 was static or shared so its probably best + # to not set for now. + #$<$:_LIB> + #$<$:_LIB> + + # Don't set `_CONSOLE`. The old build system sets for all configurations + # except x86_64 release. It seems ( https://codeyarns.com/2010/12/02/visual-c-windows-and-console-subsystems/ ) + # that `_CONSOLE` used to be defined by older Visual C++ environments. + # Setting this undocumented option seems like a bad idea so let's not do it. + #$<$ + #$<$ + + # Don't set `ASYNC_COMMANDS`. The old build system sets this for x86_64 + # release but this macro does not appear to be used anywhere and is not + # documented so don't set it for now. + #$<$:ASYNC_COMMANDS> + #$<$:ASYNC_COMMANDS> + ) +else() + list(APPEND Z3_MSVC_LEGACY_DEFINES "" + # Don't set `_CONSOLE`. See reasoning above. + #_CONSOLE + ) +endif() + +# Note we don't set WIN32 or _WINDOWS because +# CMake provides that for us. As a sanity check make sure the option +# is present. +if (NOT "${CMAKE_CXX_FLAGS}" MATCHES "/D[ ]*WIN32") + message(FATAL_ERROR "\"/D WIN32\" is missing") +endif() +if (NOT "${CMAKE_CXX_FLAGS}" MATCHES "/D[ ]*_WINDOWS") + message(FATAL_ERROR "\"/D _WINDOWS\" is missing") +endif() + +list(APPEND Z3_COMPONENT_CXX_DEFINES ${Z3_MSVC_LEGACY_DEFINES}) + +################################################################################ +# Compiler flags +################################################################################ +# By default in MSVC this is on but the old build system set this explicitly so +# for completeness set it too. +# See https://msdn.microsoft.com/en-us/library/dh8che7s.aspx +z3_add_cxx_flag("/Zc:wchar_t" REQUIRED) +# By default in MSVC this on but the old build system set this explicitly so +# for completeness set it too. +z3_add_cxx_flag("/Zc:forScope" REQUIRED) + +# FIXME: We might want to move this out somewhere else if we decide +# we want to set `-fno-omit-frame-pointer` for gcc/clang. +# No omit frame pointer +set(NO_OMIT_FRAME_POINTER_MSVC_FLAG "/Oy-") +CHECK_CXX_COMPILER_FLAG(${NO_OMIT_FRAME_POINTER_MSVC_FLAG} HAS_MSVC_NO_OMIT_FRAME_POINTER) +if (NOT HAS_MSVC_NO_OMIT_FRAME_POINTER) + message(FATAL_ERROR "${NO_OMIT_FRAME_POINTER_MSVC_FLAG} flag not supported") +endif() + +# FIXME: This doesn't make a huge amount of sense but the old +# build system kept the frame pointer for all configurations +# except x86_64 release (I don't know why the frame pointer +# is kept for i686 release). +if ("${TARGET_ARCHITECTURE}" STREQUAL "x86_64") + list(APPEND Z3_COMPONENT_CXX_FLAGS + $<$:${NO_OMIT_FRAME_POINTER_MSVC_FLAG}> + $<$:${NO_OMIT_FRAME_POINTER_MSVC_FLAG}> + ) +else() + list(APPEND Z3_COMPONENT_CXX_FLAGS ${NO_OMIT_FRAME_POINTER_MSVC_FLAG}) +endif() + +if (("${TARGET_ARCHITECTURE}" STREQUAL "x86_64") OR ("${TARGET_ARCHITECTURE}" STREQUAL "i686")) + # Use __cdecl calling convention. Apparently this is MSVC's default + # but the old build system set it so for completeness set it too. + # See https://msdn.microsoft.com/en-us/library/46t77ak2.aspx + z3_add_cxx_flag("/Gd" REQUIRED) +endif() + +# FIXME: The old build system explicitly disables code analysis. +# I don't know why. Duplicate this behaviour for now. +# See https://msdn.microsoft.com/en-us/library/ms173498.aspx +z3_add_cxx_flag("/analyze-" REQUIRED) From 5893aea60250c0e55475fd53326a0929a8028c8a Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Sat, 29 Apr 2017 12:12:54 +0100 Subject: [PATCH 506/562] [CMake] When building with MSVC and without `WARNINGS_AS_ERRORS` pass `/WX-` to MSVC. Although this is not necessary this duplicates the behaviour of the old build system. --- contrib/cmake/cmake/compiler_warnings.cmake | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/contrib/cmake/cmake/compiler_warnings.cmake b/contrib/cmake/cmake/compiler_warnings.cmake index e49e43947..e02b28b2c 100644 --- a/contrib/cmake/cmake/compiler_warnings.cmake +++ b/contrib/cmake/cmake/compiler_warnings.cmake @@ -44,4 +44,11 @@ if (WARNINGS_AS_ERRORS) message(STATUS "Treating compiler warnings as errors") else() message(STATUS "Not treating compiler warnings as errors") + # FIXME: Remove "x.." when CMP0054 is set to NEW + if ("x${CMAKE_CXX_COMPILER_ID}" STREQUAL "xMSVC") + # Warnings as errors is off by default for MSVC so setting this + # is not necessary but this duplicates the behaviour of the old + # build system. + list(APPEND Z3_COMPONENT_CXX_FLAGS "/WX-") + endif() endif() From c9aac0ba7787b98b3f2dd4268d252f434d3e2c1a Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Sat, 29 Apr 2017 12:48:49 +0100 Subject: [PATCH 507/562] [CMake] When building with MSVC try to disable incremental linking for all builds. --- contrib/cmake/cmake/msvc_legacy_quirks.cmake | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/contrib/cmake/cmake/msvc_legacy_quirks.cmake b/contrib/cmake/cmake/msvc_legacy_quirks.cmake index 050d34553..9411d112b 100644 --- a/contrib/cmake/cmake/msvc_legacy_quirks.cmake +++ b/contrib/cmake/cmake/msvc_legacy_quirks.cmake @@ -110,3 +110,34 @@ endif() # I don't know why. Duplicate this behaviour for now. # See https://msdn.microsoft.com/en-us/library/ms173498.aspx z3_add_cxx_flag("/analyze-" REQUIRED) + +################################################################################ +# Linker flags +################################################################################ + +# By default CMake enables incremental linking for Debug and RelWithDebInfo +# builds. The old build sytem disables it for all builds so try to do the same +# by changing all configurations if necessary + +string(TOUPPER "${available_build_types}" _build_types_as_upper) +foreach (_build_type ${_build_types_as_upper}) + foreach (t EXE SHARED STATIC) + set(_replacement "/INCREMENTAL:NO") + # Remove any existing incremental flags + string(REGEX REPLACE + "/INCREMENTAL:YES" + "${_replacement}" + _replaced_linker_flags + "${CMAKE_${t}_LINKER_FLAGS_${_build_type}}") + string(REGEX REPLACE + "(/INCREMENTAL$)|(/INCREMENTAL )" + "${_replacement} " + _replaced_linker_flags + "${_replaced_linker_flags}") + if (NOT "${_replaced_linker_flags}" MATCHES "${_replacement}") + # Flag not present. Add it + string(APPEND _replaced_linker_flags " ${_replacement}") + endif() + set(CMAKE_${t}_LINKER_FLAGS_${_build_type} "${_replaced_linker_flags}") + endforeach() +endforeach() From 364bcde6c1daa9efa4db30cad5fc179cc304d287 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Sat, 29 Apr 2017 13:28:44 +0100 Subject: [PATCH 508/562] [CMake] When building with MSVC pass the `/STACK:` argument to the linker like the old build system does. --- contrib/cmake/cmake/msvc_legacy_quirks.cmake | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/contrib/cmake/cmake/msvc_legacy_quirks.cmake b/contrib/cmake/cmake/msvc_legacy_quirks.cmake index 9411d112b..d0ca00c83 100644 --- a/contrib/cmake/cmake/msvc_legacy_quirks.cmake +++ b/contrib/cmake/cmake/msvc_legacy_quirks.cmake @@ -118,7 +118,6 @@ z3_add_cxx_flag("/analyze-" REQUIRED) # By default CMake enables incremental linking for Debug and RelWithDebInfo # builds. The old build sytem disables it for all builds so try to do the same # by changing all configurations if necessary - string(TOUPPER "${available_build_types}" _build_types_as_upper) foreach (_build_type ${_build_types_as_upper}) foreach (t EXE SHARED STATIC) @@ -141,3 +140,12 @@ foreach (_build_type ${_build_types_as_upper}) set(CMAKE_${t}_LINKER_FLAGS_${_build_type} "${_replaced_linker_flags}") endforeach() endforeach() + +# The original build system passes `/STACK:` to the linker. +# This size comes from the original build system. +# FIXME: What is the rationale behind this? +set(STACK_SIZE_MSVC_LINKER 8388608) +# MSVC documentation (https://msdn.microsoft.com/en-us/library/35yc2tc3.aspx) +# says this only matters for executables which is why this is not being +# set for CMAKE_SHARED_LINKER_FLAGS or CMAKE_STATIC_LINKER_FLAGS. +string(APPEND CMAKE_EXE_LINKER_FLAGS " /STACK:${STACK_SIZE_MSVC_LINKER}") From d032dbcbb238e9be365d9b8057190d9f3f375def Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Sat, 29 Apr 2017 13:49:53 +0100 Subject: [PATCH 509/562] [CMake] When using MSVC set the `/SUBSYSTEM:` argument given to the linker. This mimics the behaviour of the old build system. --- contrib/cmake/cmake/msvc_legacy_quirks.cmake | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/contrib/cmake/cmake/msvc_legacy_quirks.cmake b/contrib/cmake/cmake/msvc_legacy_quirks.cmake index d0ca00c83..f36a5bfb0 100644 --- a/contrib/cmake/cmake/msvc_legacy_quirks.cmake +++ b/contrib/cmake/cmake/msvc_legacy_quirks.cmake @@ -149,3 +149,13 @@ set(STACK_SIZE_MSVC_LINKER 8388608) # says this only matters for executables which is why this is not being # set for CMAKE_SHARED_LINKER_FLAGS or CMAKE_STATIC_LINKER_FLAGS. string(APPEND CMAKE_EXE_LINKER_FLAGS " /STACK:${STACK_SIZE_MSVC_LINKER}") + +# The original build system passes `/SUBSYSTEM:` to the linker where `` +# depends on what is being linked. Where `` is `CONSOLE` for executables +# and `WINDOWS` for shard libraries. +# We don't need to pass `/SUBSYSTEM:CONSOLE` because CMake will do this for +# us when building executables because we don't pass the `WIN32` argument to +# `add_executable()`. +# FIXME: We probably don't need this. https://msdn.microsoft.com/en-us/library/fcc1zstk.aspx +# suggests that `/SUBSYSTEM:` only matters for executables. +string(APPEND CMAKE_SHARED_LINKER_FLAGS " /SUBSYSTEM:WINDOWS") From 2a919cf16e7d6807a76fa2806d37486c7e7e04e5 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Sat, 29 Apr 2017 15:02:01 +0100 Subject: [PATCH 510/562] [CMake] Duplicate the remaining linker flags from the old build system. --- contrib/cmake/cmake/msvc_legacy_quirks.cmake | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/contrib/cmake/cmake/msvc_legacy_quirks.cmake b/contrib/cmake/cmake/msvc_legacy_quirks.cmake index f36a5bfb0..2ca20277c 100644 --- a/contrib/cmake/cmake/msvc_legacy_quirks.cmake +++ b/contrib/cmake/cmake/msvc_legacy_quirks.cmake @@ -159,3 +159,36 @@ string(APPEND CMAKE_EXE_LINKER_FLAGS " /STACK:${STACK_SIZE_MSVC_LINKER}") # FIXME: We probably don't need this. https://msdn.microsoft.com/en-us/library/fcc1zstk.aspx # suggests that `/SUBSYSTEM:` only matters for executables. string(APPEND CMAKE_SHARED_LINKER_FLAGS " /SUBSYSTEM:WINDOWS") + +# FIXME: The following linker flags are weird. They are set in all configurations +# in the old build system except release x86_64. We try to emulate this here but +# this is likely the wrong thing to do. +foreach (_build_type ${_build_types_as_upper}) + if ("${TARGET_ARCHITECTURE}" STREQUAL "x86_64" AND + ("${_build_type}" STREQUAL "RELEASE" OR + "${_build_type}" STREQUAL "RELWITHDEBINFO") + ) + message(AUTHOR_WARNING "Skipping legacy linker MSVC options for x86_64 ${_build_type}") + else() + # Linker optimizations. + # See https://msdn.microsoft.com/en-us/library/bxwfs976.aspx + string(APPEND CMAKE_EXE_LINKER_FLAGS_${_build_type} " /OPT:REF /OPT:ICF") + string(APPEND CMAKE_SHARED_LINKER_FLAGS_${_build_type} " /OPT:REF /OPT:ICF") + + # FIXME: This is not necessary. This is MSVC's default. + # See https://msdn.microsoft.com/en-us/library/b1kw34cb.aspx + string(APPEND CMAKE_EXE_LINKER_FLAGS_${_build_type} " /TLBID:1") + string(APPEND CMAKE_SHARED_LINKER_FLAGS_${_build_type} " /TLBID:1") + + # FIXME: This is not necessary. This is MSVC's default. + # Address space layout randomization + # See https://msdn.microsoft.com/en-us/library/bb384887.aspx + string(APPEND CMAKE_EXE_LINKER_FLAGS_${_build_type} " /DYNAMICBASE") + string(APPEND CMAKE_SHARED_LINKER_FLAGS_${_build_type} " /DYNAMICBASE:NO") + + # FIXME: This is not necessary. This is MSVC's default. + # Indicate that the executable is compatible with DEP + # See https://msdn.microsoft.com/en-us/library/ms235442.aspx + string(APPEND CMAKE_EXE_LINKER_FLAGS_${_build_type} " /NXCOMPAT") + endif() +endforeach() From 6e07d6dd2d11c7c779640f7cdb9c234875454e88 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Sat, 29 Apr 2017 17:45:02 +0100 Subject: [PATCH 511/562] [CMake] Override CMake's default flags for GCC/Clang as we were doing before 4cc2b292c0cc8759da7a525e63dbfefdb06d6a01. It's useful to be able to control the defaults and CMake's internal logic for GCC/Clang is simple enough that doing this makes sense. It would be nice to do the same for MSVC but CMake's internal logic is more complicated so for now it's better that we just use CMake's default. --- CMakeLists.txt | 1 + .../cmake/cmake/cxx_compiler_flags_overrides.cmake | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 contrib/cmake/cmake/cxx_compiler_flags_overrides.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index aa347ae66..19c56a413 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ if (NOT (EXISTS "${CMAKE_SOURCE_DIR}/src/CMakeLists.txt")) "``python contrib/cmake/bootstrap.py create``") endif() +set(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cxx_compiler_flags_overrides.cmake") project(Z3 CXX) ################################################################################ diff --git a/contrib/cmake/cmake/cxx_compiler_flags_overrides.cmake b/contrib/cmake/cmake/cxx_compiler_flags_overrides.cmake new file mode 100644 index 000000000..59966f424 --- /dev/null +++ b/contrib/cmake/cmake/cxx_compiler_flags_overrides.cmake @@ -0,0 +1,14 @@ +# This file overrides the default compiler flags for CMake's built-in +# configurations (CMAKE_BUILD_TYPE). Most compiler flags should not be set here. +# The main purpose is to have very fine grained control of the compiler flags. + +# We only override the defaults for Clang and GCC right now. +# CMake's MSVC logic is complicated so for now it's better to just inherit CMake's defaults. +if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")) + # Taken from Modules/Compiler/GNU.cmake + set(CMAKE_CXX_FLAGS_INIT "") + set(CMAKE_CXX_FLAGS_DEBUG_INIT "-g -O0") + set(CMAKE_CXX_FLAGS_MINSIZEREL_INIT "-Os -DNDEBUG") + set(CMAKE_CXX_FLAGS_RELEASE_INIT "-O3 -DNDEBUG") + set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "-O2 -g -DNDEBUG") +endif() From fa868e058efa853cceff50e4ae925a8381a91cfc Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sat, 29 Apr 2017 17:39:02 -0700 Subject: [PATCH 512/562] fix bound bug #991 Signed-off-by: Nikolaj Bjorner --- src/sat/sat_solver.cpp | 100 ++++++++++++++++++++++++++++++++++++- src/smt/theory_arith.h | 1 + src/smt/theory_arith_nl.h | 38 +++++++++++--- src/test/cnf_backbones.cpp | 81 ++++++++++++++++++++++++++---- src/test/main.cpp | 14 +++++- 5 files changed, 215 insertions(+), 19 deletions(-) diff --git a/src/sat/sat_solver.cpp b/src/sat/sat_solver.cpp index 739a591e7..bc28221a9 100644 --- a/src/sat/sat_solver.cpp +++ b/src/sat/sat_solver.cpp @@ -3141,6 +3141,101 @@ namespace sat { // // ----------------------- +static void prune_unfixed(sat::literal_vector& lambda, sat::model const& m) { + for (unsigned i = 0; i < lambda.size(); ++i) { + if ((m[lambda[i].var()] == l_false) != lambda[i].sign()) { + lambda[i] = lambda.back(); + lambda.pop_back(); + --i; + } + } +} + +// Algorithm 7: Corebased Algorithm with Chunking + +static void back_remove(sat::literal_vector& lits, sat::literal l) { + for (unsigned i = lits.size(); i > 0; ) { + --i; + if (lits[i] == l) { + lits[i] = lits.back(); + lits.pop_back(); + return; + } + } + std::cout << "UNREACHABLE\n"; +} + + static void brute_force_consequences(sat::solver& s, sat::literal_vector const& asms, sat::literal_vector const& gamma, vector& conseq) { + for (unsigned i = 0; i < gamma.size(); ++i) { + sat::literal nlit = ~gamma[i]; + sat::literal_vector asms1(asms); + asms1.push_back(nlit); + lbool r = s.check(asms1.size(), asms1.c_ptr()); + if (r == l_false) { + conseq.push_back(s.get_core()); + } + } +} + + static lbool core_chunking(sat::solver& s, model const& m, sat::bool_var_vector const& vars, sat::literal_vector const& asms, vector& conseq, unsigned K) { + sat::literal_vector lambda; + for (unsigned i = 0; i < vars.size(); i++) { + lambda.push_back(sat::literal(i, m[vars[i]] == l_false)); + } + while (!lambda.empty()) { + IF_VERBOSE(1, verbose_stream() << "(sat-backbone-core " << lambda.size() << " " << conseq.size() << ")\n";); + unsigned k = std::min(K, lambda.size()); + sat::literal_vector gamma, omegaN; + for (unsigned i = 0; i < k; ++i) { + sat::literal l = lambda[lambda.size() - i - 1]; + gamma.push_back(l); + omegaN.push_back(~l); + } + while (true) { + sat::literal_vector asms1(asms); + asms1.append(omegaN); + lbool r = s.check(asms1.size(), asms1.c_ptr()); + if (r == l_true) { + IF_VERBOSE(1, verbose_stream() << "(sat) " << omegaN << "\n";); + prune_unfixed(lambda, s.get_model()); + break; + } + sat::literal_vector const& core = s.get_core(); + sat::literal_vector occurs; + IF_VERBOSE(1, verbose_stream() << "(core " << core.size() << ")\n";); + for (unsigned i = 0; i < omegaN.size(); ++i) { + if (core.contains(omegaN[i])) { + occurs.push_back(omegaN[i]); + } + } + if (occurs.size() == 1) { + sat::literal lit = occurs.back(); + sat::literal nlit = ~lit; + conseq.push_back(core); + back_remove(lambda, ~lit); + back_remove(gamma, ~lit); + s.mk_clause(1, &nlit); + } + for (unsigned i = 0; i < omegaN.size(); ++i) { + if (occurs.contains(omegaN[i])) { + omegaN[i] = omegaN.back(); + omegaN.pop_back(); + --i; + } + } + if (omegaN.empty() && occurs.size() > 1) { + brute_force_consequences(s, asms, gamma, conseq); + for (unsigned i = 0; i < gamma.size(); ++i) { + back_remove(lambda, gamma[i]); + } + break; + } + } + } + return l_true; + } + + lbool solver::get_consequences(literal_vector const& asms, bool_var_vector const& vars, vector& conseq) { literal_vector lits; lbool is_sat = l_true; @@ -3163,6 +3258,9 @@ namespace sat { default: break; } } + + // is_sat = core_chunking(*this, mdl, vars, asms, conseq, 100); + is_sat = get_consequences(asms, lits, conseq); set_model(mdl); return is_sat; @@ -3307,7 +3405,7 @@ namespace sat { propagate(false); ++num_resolves; } - if (scope_lvl() == 1) { + if (false && scope_lvl() == 1) { break; } } diff --git a/src/smt/theory_arith.h b/src/smt/theory_arith.h index 77882f186..439adbdff 100644 --- a/src/smt/theory_arith.h +++ b/src/smt/theory_arith.h @@ -552,6 +552,7 @@ namespace smt { bool is_int(theory_var v) const { return m_data[v].m_is_int; } bool is_int_src(theory_var v) const { return m_util.is_int(var2expr(v)); } bool is_real(theory_var v) const { return !is_int(v); } + bool is_real_src(theory_var v) const { return !is_int_src(v); } bool get_implied_old_value(theory_var v, inf_numeral & r) const; inf_numeral const & get_implied_value(theory_var v) const; inf_numeral const & get_quasi_base_value(theory_var v) const { return get_implied_value(v); } diff --git a/src/smt/theory_arith_nl.h b/src/smt/theory_arith_nl.h index c8729ea36..8a632cf48 100644 --- a/src/smt/theory_arith_nl.h +++ b/src/smt/theory_arith_nl.h @@ -444,7 +444,7 @@ namespace smt { m_asserted_bounds.push_back(new_bound); // copy justification to new bound dependency2new_bound(dep, *new_bound); - TRACE("buggy_bound", new_bound->display(*this, tout); tout << "\n";); + TRACE("non_linear", new_bound->display(*this, tout); tout << "\n";); } /** @@ -457,8 +457,19 @@ namespace smt { bool r = false; if (!i.minus_infinity()) { inf_numeral new_lower(i.get_lower_value()); - if (i.is_lower_open()) - new_lower += get_epsilon(v); + if (i.is_lower_open()) { + if (is_int(v)) { + if (new_lower.is_int()) { + new_lower += rational::one(); + } + else { + new_lower = ceil(new_lower.get_rational()); + } + } + else { + new_lower += get_epsilon(v); + } + } bound * old_lower = lower(v); if (old_lower == 0 || new_lower > old_lower->get_value()) { TRACE("non_linear", tout << "NEW lower bound for v" << v << " " << new_lower << "\n"; @@ -469,8 +480,19 @@ namespace smt { } if (!i.plus_infinity()) { inf_numeral new_upper(i.get_upper_value()); - if (i.is_upper_open()) - new_upper -= get_epsilon(v); + if (i.is_upper_open()) { + if (is_int(v)) { + if (new_upper.is_int()) { + new_upper -= rational::one(); + } + else { + new_upper = floor(new_upper.get_rational()); + } + } + else { + new_upper -= get_epsilon(v); + } + } bound * old_upper = upper(v); if (old_upper == 0 || new_upper < old_upper->get_value()) { TRACE("non_linear", tout << "NEW upper bound for v" << v << " " << new_upper << "\n"; @@ -819,6 +841,7 @@ namespace smt { if (is_fixed(_var)) r *= lower_bound(_var).get_rational(); } + TRACE("arith", tout << mk_pp(m, get_manager()) << " " << r << "\n";); return r; } @@ -896,7 +919,7 @@ namespace smt { // Assert the equality // (= (* x_1 ... x_n) k) - TRACE("non_linear", tout << "all variables are fixed.\n";); + TRACE("non_linear", tout << "all variables are fixed, and bound is: " << k << "\n";); new_lower = alloc(derived_bound, v, inf_numeral(k), B_LOWER); new_upper = alloc(derived_bound, v, inf_numeral(k), B_UPPER); } @@ -953,7 +976,8 @@ namespace smt { new_upper->m_eqs.append(new_lower->m_eqs); TRACE("non_linear", - tout << "lower: " << new_lower << " upper: " << new_upper << "\n"; + new_lower->display(*this, tout << "lower: "); tout << "\n"; + new_upper->display(*this, tout << "upper: "); tout << "\n"; for (unsigned j = 0; j < new_upper->m_lits.size(); ++j) { ctx.display_detailed_literal(tout, new_upper->m_lits[j]); tout << " "; diff --git a/src/test/cnf_backbones.cpp b/src/test/cnf_backbones.cpp index a4eaa4222..3387e1a8e 100644 --- a/src/test/cnf_backbones.cpp +++ b/src/test/cnf_backbones.cpp @@ -64,6 +64,56 @@ static void display_status(lbool r) { } } +static void track_clause(sat::solver& dst, + sat::literal_vector& lits, + sat::literal_vector& assumptions, + vector& tracking_clauses) { + sat::literal lit = sat::literal(dst.mk_var(true, false), false); + tracking_clauses.set(lit.var(), lits); + lits.push_back(~lit); + dst.mk_clause(lits.size(), lits.c_ptr()); + assumptions.push_back(lit); +} + + +static void track_clauses(sat::solver const& src, + sat::solver& dst, + sat::literal_vector& assumptions, + vector& tracking_clauses) { + for (sat::bool_var v = 0; v < src.num_vars(); ++v) { + dst.mk_var(false, true); + } + sat::literal_vector lits; + sat::literal lit; + sat::clause * const * it = src.begin_clauses(); + sat::clause * const * end = src.end_clauses(); + svector bin_clauses; + src.collect_bin_clauses(bin_clauses, false); + tracking_clauses.reserve(2*src.num_vars() + static_cast(end - it) + bin_clauses.size()); + + for (sat::bool_var v = 1; v < src.num_vars(); ++v) { + if (src.value(v) != l_undef) { + bool sign = src.value(v) == l_false; + lits.reset(); + lits.push_back(sat::literal(v, sign)); + track_clause(dst, lits, assumptions, tracking_clauses); + } + } + for (; it != end; ++it) { + lits.reset(); + sat::clause& cls = *(*it); + lits.append(static_cast(cls.end()-cls.begin()), cls.begin()); + track_clause(dst, lits, assumptions, tracking_clauses); + } + for (unsigned i = 0; i < bin_clauses.size(); ++i) { + lits.reset(); + lits.push_back(bin_clauses[i].first); + lits.push_back(bin_clauses[i].second); + track_clause(dst, lits, assumptions, tracking_clauses); + } +} + + static void prune_unfixed(sat::literal_vector& lambda, sat::model const& m) { for (unsigned i = 0; i < lambda.size(); ++i) { if ((m[lambda[i].var()] == l_false) != lambda[i].sign()) { @@ -88,18 +138,20 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { std::cout << "UNREACHABLE\n"; } -static void brute_force_consequences(sat::solver& s, sat::literal_vector const& gamma, sat::literal_vector& backbones) { +static void brute_force_consequences(sat::solver& s, sat::literal_vector const& asms, sat::literal_vector const& gamma, sat::literal_vector& backbones) { for (unsigned i = 0; i < gamma.size(); ++i) { sat::literal nlit = ~gamma[i]; - lbool r = s.check(1, &nlit); + sat::literal_vector asms1(asms); + asms1.push_back(nlit); + lbool r = s.check(asms1.size(), asms1.c_ptr()); if (r == l_false) { backbones.push_back(gamma[i]); } } } -static lbool core_chunking(sat::solver& s, sat::bool_var_vector& vars, vector& conseq, unsigned K) { - lbool r = s.check(); +static lbool core_chunking(sat::solver& s, sat::bool_var_vector& vars, sat::literal_vector const& asms, vector& conseq, unsigned K) { + lbool r = s.check(asms.size(), asms.c_ptr()); display_status(r); if (r != l_true) { return r; @@ -119,7 +171,9 @@ static lbool core_chunking(sat::solver& s, sat::bool_var_vector& vars, vector 1) { - brute_force_consequences(s, gamma, backbones); + brute_force_consequences(s, asms, gamma, backbones); for (unsigned i = 0; i < gamma.size(); ++i) { back_remove(lambda, gamma[i]); } @@ -174,6 +228,7 @@ static void cnf_backbones(bool use_chunk, char const* file_name) { p.set_bool("produce_models", true); reslimit limit; sat::solver solver(p, limit, 0); + sat::solver solver2(p, limit, 0); g_solver = &solver; if (file_name) { @@ -192,16 +247,22 @@ static void cnf_backbones(bool use_chunk, char const* file_name) { vector conseq; sat::bool_var_vector vars; sat::literal_vector assumptions; - for (unsigned i = 1; i < solver.num_vars(); ++i) { + if (p.get_bool("dimacs.core", false)) { + g_solver = &solver2; + vector tracking_clauses; + track_clauses(solver, solver2, assumptions, tracking_clauses); + } + + for (unsigned i = 1; i < g_solver->num_vars(); ++i) { vars.push_back(i); - solver.set_external(i); + g_solver->set_external(i); } lbool r; if (use_chunk) { - r = core_chunking(solver, vars, conseq, 100); + r = core_chunking(*g_solver, vars, assumptions, conseq, 100); } else { - r = solver.get_consequences(assumptions, vars, conseq); + r = g_solver->get_consequences(assumptions, vars, conseq); } std::cout << vars.size() << " " << conseq.size() << "\n"; display_status(r); diff --git a/src/test/main.cpp b/src/test/main.cpp index 4a54797ac..15f92b2f7 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -8,6 +8,7 @@ #include"timeit.h" #include"warning.h" #include "memory_manager.h" +#include"gparams.h" // // Unit tests fail by asserting. @@ -75,7 +76,7 @@ void display_usage() { void parse_cmd_line_args(int argc, char ** argv, bool& do_display_usage, bool& test_all) { int i = 1; while (i < argc) { - char * arg = argv[i]; + char * arg = argv[i], *eq_pos = 0; if (arg[0] == '-' || arg[0] == '/') { char * opt_name = arg + 1; @@ -118,6 +119,17 @@ void parse_cmd_line_args(int argc, char ** argv, bool& do_display_usage, bool& t } #endif } + else if (arg[0] != '"' && (eq_pos = strchr(arg, '='))) { + char * key = arg; + *eq_pos = 0; + char * value = eq_pos+1; + try { + gparams::set(key, value); + } + catch (z3_exception& ex) { + std::cerr << ex.msg() << "\n"; + } + } i++; } } From 3152833893ece622174d1b8e6fab443da1e409cf Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sat, 29 Apr 2017 18:55:47 -0700 Subject: [PATCH 513/562] fix unused variables Signed-off-by: Nikolaj Bjorner --- src/smt/theory_seq.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/smt/theory_seq.cpp b/src/smt/theory_seq.cpp index 9dd082de0..7a9369fd3 100644 --- a/src/smt/theory_seq.cpp +++ b/src/smt/theory_seq.cpp @@ -3874,8 +3874,8 @@ void theory_seq::new_eq_eh(dependency* deps, enode* n1, enode* n2) { } else if (n1 != n2 && m_util.is_re(n1->get_owner())) { warning_msg("equality between regular expressions is not yet supported"); - eautomaton* a1 = get_automaton(n1->get_owner()); - eautomaton* a2 = get_automaton(n2->get_owner()); + // eautomaton* a1 = get_automaton(n1->get_owner()); + // eautomaton* a2 = get_automaton(n2->get_owner()); // eautomaton* b1 = mk_difference(*a1, *a2); // eautomaton* b2 = mk_difference(*a2, *a1); // eautomaton* c = mk_union(*b1, *b2); From b3f720c2bf0ba2326f5c1513c27c3e142683d068 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sat, 29 Apr 2017 18:58:34 -0700 Subject: [PATCH 514/562] fix unused variables Signed-off-by: Nikolaj Bjorner --- src/smt/theory_arith_core.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/theory_arith_core.h b/src/smt/theory_arith_core.h index 785e0120f..5c652414a 100644 --- a/src/smt/theory_arith_core.h +++ b/src/smt/theory_arith_core.h @@ -465,7 +465,7 @@ namespace smt { TRACE("arith_axiom", tout << mk_pp(ante, m) << "\n" << mk_pp(conseq, m) << "\n"; tout << s_ante << "\n" << s_conseq << "\n";); - literal lits[2] = {l_ante, l_conseq}; + // literal lits[2] = {l_ante, l_conseq}; mk_clause(l_ante, l_conseq, 0, 0); if (ctx.relevancy()) { if (l_ante == false_literal) { From 4468816d3223ff8efa604388c5967e34cf7fe354 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sat, 29 Apr 2017 19:00:15 -0700 Subject: [PATCH 515/562] fix unused variables Signed-off-by: Nikolaj Bjorner --- src/smt/smt_context_inv.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/smt/smt_context_inv.cpp b/src/smt/smt_context_inv.cpp index f63e07b57..e6551b8da 100644 --- a/src/smt/smt_context_inv.cpp +++ b/src/smt/smt_context_inv.cpp @@ -405,7 +405,6 @@ namespace smt { bool context::validate_justification(bool_var v, bool_var_data const& d, b_justification const& j) { if (j.get_kind() == b_justification::CLAUSE && v != true_bool_var) { clause* cls = j.get_clause(); - unsigned num_lits = cls->get_num_literals(); literal l = cls->get_literal(0); if (l.var() != v) { l = cls->get_literal(1); From 2c208e1d1002c25593ad2e1a637fdfe96628fda3 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sun, 30 Apr 2017 10:23:00 -0700 Subject: [PATCH 516/562] Sat update Signed-off-by: Nikolaj Bjorner --- src/sat/sat_solver.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/sat/sat_solver.cpp b/src/sat/sat_solver.cpp index 739a591e7..cd13ac7a4 100644 --- a/src/sat/sat_solver.cpp +++ b/src/sat/sat_solver.cpp @@ -3291,6 +3291,9 @@ namespace sat { literal lit = *it; if (value(lit) != l_undef) { ++num_fixed; + if (value(lit) == l_true && lvl(lit) == 1) { + VERIFY(extract_fixed_consequences(lit, assumptions, unfixed_vars, conseq)); + } continue; } push(); From bd1b930d7a1735da6de575a4c57784f720690c8b Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sun, 30 Apr 2017 11:00:03 -0700 Subject: [PATCH 517/562] swap argument order of chunk with file Signed-off-by: Nikolaj Bjorner --- src/test/cnf_backbones.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/cnf_backbones.cpp b/src/test/cnf_backbones.cpp index 3387e1a8e..c34e109d3 100644 --- a/src/test/cnf_backbones.cpp +++ b/src/test/cnf_backbones.cpp @@ -271,9 +271,9 @@ static void cnf_backbones(bool use_chunk, char const* file_name) { void tst_cnf_backbones(char ** argv, int argc, int& i) { if (i + 1 < argc) { - bool use_chunk = (i + 2 < argc && argv[i + 2] == std::string("chunk")); + bool use_chunk = (i + 2 < argc && argv[i + 1] == std::string("chunk")); + if (use_chunk) ++i; cnf_backbones(use_chunk, argv[i + 1]); ++i; - if (use_chunk) ++i; } } From aff02ca9056f299b0b460d408e1425e4dec050f8 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sun, 30 Apr 2017 11:26:54 -0700 Subject: [PATCH 518/562] include 'stopwatch.h' to avoid ODR warnings, #994 --- src/muz/base/dl_util.h | 1 + src/muz/rel/dl_instruction.h | 1 - src/sat/sat_solver.cpp | 18 ++++++++++++++++-- src/sat/sat_solver.h | 1 + 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/muz/base/dl_util.h b/src/muz/base/dl_util.h index b1719577b..a4e9f192e 100644 --- a/src/muz/base/dl_util.h +++ b/src/muz/base/dl_util.h @@ -29,6 +29,7 @@ Revision History: #include"substitution.h" #include"ast_counter.h" #include"statistics.h" +#include"stopwatch.h" #include"lbool.h" namespace datalog { diff --git a/src/muz/rel/dl_instruction.h b/src/muz/rel/dl_instruction.h index 160eb7b94..196f5268c 100644 --- a/src/muz/rel/dl_instruction.h +++ b/src/muz/rel/dl_instruction.h @@ -30,7 +30,6 @@ Revision History: namespace datalog { - class execution_context; class instruction_block; class rel_context; diff --git a/src/sat/sat_solver.cpp b/src/sat/sat_solver.cpp index bc28221a9..c80b75ba6 100644 --- a/src/sat/sat_solver.cpp +++ b/src/sat/sat_solver.cpp @@ -3500,7 +3500,13 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { } bool solver::check_domain(literal lit, literal lit2) { - return m_antecedents.contains(lit2.var()); + if (!m_antecedents.contains(lit2.var())) { + m_todo_antecedents.push_back(lit2); + return false; + } + else { + return true; + } } bool solver::extract_assumptions(literal lit, index_set& s) { @@ -3565,8 +3571,16 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { s.insert(lit.index()); } else { + SASSERT(m_todo_antecedents.empty()); if (!extract_assumptions(lit, s)) { - return false; + SASSERT(!m_todo_antecedents.empty()); + while (!m_todo_antecedents.empty()) { + index_set s1; + if (extract_assumptions(m_todo_antecedents.back(), s1)) { + m_todo_antecedents.pop_back(); + } + } + VERIFY (extract_assumptions(lit, s)); } add_assumption(lit); } diff --git a/src/sat/sat_solver.h b/src/sat/sat_solver.h index c7f472a60..091162f23 100644 --- a/src/sat/sat_solver.h +++ b/src/sat/sat_solver.h @@ -481,6 +481,7 @@ namespace sat { typedef hashtable index_set; u_map m_antecedents; + literal_vector m_todo_antecedents; vector m_binary_clause_graph; bool extract_assumptions(literal lit, index_set& s); From 86f3526110de9ca8c4d92696744c07d9dbad4e72 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sun, 30 Apr 2017 11:48:06 -0700 Subject: [PATCH 519/562] update get-value to also respect parameter model_index, #955 Signed-off-by: Nikolaj Bjorner --- src/parsers/smt2/smt2parser.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/parsers/smt2/smt2parser.cpp b/src/parsers/smt2/smt2parser.cpp index d2b55af1d..b7f33d333 100644 --- a/src/parsers/smt2/smt2parser.cpp +++ b/src/parsers/smt2/smt2parser.cpp @@ -2218,11 +2218,27 @@ namespace smt2 { if (m_cached_strings.empty()) throw cmd_exception("invalid get-value command, empty list of terms"); next(); + unsigned index = 0; + if (curr_is_keyword() && curr_id() == ":model_index") { + next(); + check_int("integer index expected to indexed model evaluation"); + if (!curr_numeral().is_unsigned()) + throw parser_exception("expected unsigned integer index to model evaluation"); + index = curr_numeral().get_unsigned(); + next(); + } + check_rparen("invalid get-value command, ')' expected"); if (!m_ctx.is_model_available() || m_ctx.get_check_sat_result() == 0) throw cmd_exception("model is not available"); model_ref md; - m_ctx.get_check_sat_result()->get_model(md); + if (index == 0) { + m_ctx.get_check_sat_result()->get_model(md); + } + else { + m_ctx.get_opt()->get_box_model(md, index); + + } m_ctx.regular_stream() << "("; expr ** expr_it = expr_stack().c_ptr() + spos; expr ** expr_end = expr_it + m_cached_strings.size(); From 0693a413b6dc9facb8ae9ee484dc7d85d4fe89ec Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sun, 30 Apr 2017 12:50:56 -0700 Subject: [PATCH 520/562] augment #955 to handle hyphen Signed-off-by: Nikolaj Bjorner --- src/parsers/smt2/smt2parser.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parsers/smt2/smt2parser.cpp b/src/parsers/smt2/smt2parser.cpp index b7f33d333..8e72e50db 100644 --- a/src/parsers/smt2/smt2parser.cpp +++ b/src/parsers/smt2/smt2parser.cpp @@ -2219,7 +2219,7 @@ namespace smt2 { throw cmd_exception("invalid get-value command, empty list of terms"); next(); unsigned index = 0; - if (curr_is_keyword() && curr_id() == ":model_index") { + if (curr_is_keyword() && (curr_id() == ":model-index" || curr_id() == ":model_index")) { next(); check_int("integer index expected to indexed model evaluation"); if (!curr_numeral().is_unsigned()) From aceee3fac8912629e0d1e3568bb0788f184138d6 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sun, 30 Apr 2017 12:54:29 -0700 Subject: [PATCH 521/562] renmae to opt_stream_buffer to avoid clash with dimacs stream buffer. #994 Signed-off-by: Nikolaj Bjorner --- src/shell/opt_frontend.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/shell/opt_frontend.cpp b/src/shell/opt_frontend.cpp index 760fdcf54..278dad287 100644 --- a/src/shell/opt_frontend.cpp +++ b/src/shell/opt_frontend.cpp @@ -20,12 +20,12 @@ static opt::context* g_opt = 0; static double g_start_time = 0; static unsigned_vector g_handles; -class stream_buffer { +class opt_stream_buffer { std::istream & m_stream; int m_val; unsigned m_line; public: - stream_buffer(std::istream & s): + opt_stream_buffer(std::istream & s): m_stream(s), m_line(0) { m_val = m_stream.get(); @@ -111,7 +111,7 @@ public: class wcnf { opt::context& opt; ast_manager& m; - stream_buffer& in; + opt_stream_buffer& in; app_ref read_clause(unsigned& weight) { int parsed_lit; @@ -141,7 +141,7 @@ class wcnf { public: - wcnf(opt::context& opt, stream_buffer& in): opt(opt), m(opt.get_manager()), in(in) { + wcnf(opt::context& opt, opt_stream_buffer& in): opt(opt), m(opt.get_manager()), in(in) { opt.set_clausal(true); } @@ -180,7 +180,7 @@ public: class opb { opt::context& opt; ast_manager& m; - stream_buffer& in; + opt_stream_buffer& in; arith_util arith; app_ref parse_id() { @@ -254,7 +254,7 @@ class opb { opt.add_hard_constraint(t); } public: - opb(opt::context& opt, stream_buffer& in): + opb(opt::context& opt, opt_stream_buffer& in): opt(opt), m(opt.get_manager()), in(in), arith(m) {} @@ -335,7 +335,7 @@ static unsigned parse_opt(std::istream& in, bool is_wcnf) { g_opt = &opt; params_ref p = gparams::get_module("opt"); opt.updt_params(p); - stream_buffer _in(in); + opt_stream_buffer _in(in); if (is_wcnf) { wcnf wcnf(opt, _in); wcnf.parse(); From f655e1976eca91d1aaa06d1551367910e63ec03c Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 1 May 2017 10:18:38 -0400 Subject: [PATCH 522/562] add params for theory case split --- src/smt/params/smt_params.cpp | 3 ++- src/smt/params/smt_params.h | 1 + src/smt/params/smt_params_helper.pyg | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/smt/params/smt_params.cpp b/src/smt/params/smt_params.cpp index 8a9188e2b..4b7920596 100644 --- a/src/smt/params/smt_params.cpp +++ b/src/smt/params/smt_params.cpp @@ -31,6 +31,7 @@ void smt_params::updt_local_params(params_ref const & _p) { m_restart_strategy = static_cast(p.restart_strategy()); m_restart_factor = p.restart_factor(); m_case_split_strategy = static_cast(p.case_split()); + m_theory_case_split = p.theory_case_split(); m_delay_units = p.delay_units(); m_delay_units_threshold = p.delay_units_threshold(); m_preprocess = _p.get_bool("preprocess", true); // hidden parameter @@ -155,4 +156,4 @@ void smt_params::display(std::ostream & out) const { DISPLAY_PARAM(m_check_at_labels); DISPLAY_PARAM(m_dump_goal_as_smt); DISPLAY_PARAM(m_auto_config); -} \ No newline at end of file +} diff --git a/src/smt/params/smt_params.h b/src/smt/params/smt_params.h index 366570365..c03eaeaef 100644 --- a/src/smt/params/smt_params.h +++ b/src/smt/params/smt_params.h @@ -109,6 +109,7 @@ struct smt_params : public preprocessor_params, case_split_strategy m_case_split_strategy; unsigned m_rel_case_split_order; bool m_lookahead_diseq; + bool m_theory_case_split; // ----------------------------------- // diff --git a/src/smt/params/smt_params_helper.pyg b/src/smt/params/smt_params_helper.pyg index 6ac4aab04..faa48400d 100644 --- a/src/smt/params/smt_params_helper.pyg +++ b/src/smt/params/smt_params_helper.pyg @@ -61,6 +61,7 @@ def_module_params(module_name='smt', ('dack.gc', UINT, 2000, 'Dynamic ackermannization garbage collection frequency (per conflict)'), ('dack.gc_inv_decay', DOUBLE, 0.8, 'Dynamic ackermannization garbage collection decay'), ('dack.threshold', UINT, 10, ' number of times the congruence rule must be used before Leibniz\'s axiom is expanded'), + ('theory_case_split', BOOL, False, 'Allow the context to use heuristics involving theory case splits, which are a set of literals of which exactly one can be assigned True. If this option is false, the context will generate extra axioms to enforce this instead.'), ('core.validate', BOOL, False, 'validate unsat core produced by SMT context'), ('core.minimize', BOOL, False, 'minimize unsat core produced by SMT context'), ('core.extend_patterns', BOOL, False, 'extend unsat core with literals that trigger (potential) quantifier instances'), From 2f56d128b0531829d8354c52d1059e368747c33e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 1 May 2017 10:34:43 -0400 Subject: [PATCH 523/562] add theory case split support to smt_context --- src/smt/smt_context.cpp | 123 ++++++++++++++++++++++++++++++++++++++++ src/smt/smt_context.h | 25 ++++++++ 2 files changed, 148 insertions(+) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 6a3c036ca..af1e94e62 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -62,6 +62,7 @@ namespace smt { m_is_diseq_tmp(0), m_units_to_reassert(m_manager), m_qhead(0), + m_th_case_split_qhead(0), m_simp_qhead(0), m_simp_counter(0), m_bvar_inc(1.0), @@ -339,6 +340,7 @@ namespace smt { bool context::bcp() { SASSERT(!inconsistent()); + m_th_case_split_qhead = m_qhead; while (m_qhead < m_assigned_literals.size()) { if (get_cancel_flag()) { return true; @@ -1768,6 +1770,8 @@ namespace smt { unsigned qhead = m_qhead; if (!bcp()) return false; + if (!propagate_th_case_split()) + return false; if (get_cancel_flag()) { m_qhead = qhead; return true; @@ -2960,6 +2964,125 @@ namespace smt { assert_expr_core(e, pr); } + class case_split_insert_trail : public trail { + literal l; + public: + case_split_insert_trail(literal l): + l(l) { + } + virtual void undo(context & ctx) { + ctx.undo_th_case_split(l); + } + }; + + void context::mk_th_case_split(unsigned num_lits, literal * lits) { + TRACE("theory_case_split", display_literals_verbose(tout << "theory case split: ", num_lits, lits); tout << std::endl;); + // If we don't use the theory case split heuristic, + // for each pair of literals (l1, l2) we add the clause (~l1 OR ~l2) + // to enforce the condition that more than one literal can't be + // assigned 'true' simultaneously. + if (!m_fparams.m_theory_case_split) { + for (unsigned i = 0; i < num_lits; ++i) { + for (unsigned j = i+1; j < num_lits; ++j) { + literal l1 = lits[i]; + literal l2 = lits[j]; + literal excl[2] = {~l1, ~l2}; + justification * j_excl = 0; + mk_clause(2, excl, j_excl); + } + } + } else { + literal_vector new_case_split; + for (unsigned i = 0; i < num_lits; ++i) { + literal l = lits[i]; + SASSERT(!m_all_th_case_split_literals.contains(l.index())); + m_all_th_case_split_literals.insert(l.index()); + push_trail(case_split_insert_trail(l)); + new_case_split.push_back(l); + } + m_th_case_split_sets.push_back(new_case_split); + push_trail(push_back_vector >(m_th_case_split_sets)); + for (unsigned i = 0; i < num_lits; ++i) { + literal l = lits[i]; + if (!m_literal2casesplitsets.contains(l.index())) { + m_literal2casesplitsets.insert(l.index(), vector()); + } + m_literal2casesplitsets[l.index()].push_back(new_case_split); + } + TRACE("theory_case_split", tout << "tracking case split literal set { "; + for (unsigned i = 0; i < num_lits; ++i) { + tout << lits[i].index() << " "; + } + tout << "}" << std::endl; + ); + } + } + + void context::undo_th_case_split(literal l) { + m_all_th_case_split_literals.remove(l.index()); + if (m_literal2casesplitsets.contains(l.index())) { + if (!m_literal2casesplitsets[l.index()].empty()) { + m_literal2casesplitsets[l.index()].pop_back(); + } + } + } + + bool context::propagate_th_case_split() { + if (m_all_th_case_split_literals.empty()) + return true; + + // iterate over all literals assigned since the last time this method was called, + // not counting any literals that get assigned by this method + // this relies on bcp() to give us its old m_qhead and therefore + // bcp() should always be called before this method + unsigned assigned_literal_idx = m_th_case_split_qhead; + unsigned assigned_literal_end = m_assigned_literals.size(); + while(assigned_literal_idx < assigned_literal_end) { + literal l = m_assigned_literals[assigned_literal_idx]; + TRACE("theory_case_split", tout << "check literal " << l.index() << std::endl; display_literal_verbose(tout, l); tout << std::endl;); + ++assigned_literal_idx; + // check if this literal participates in any theory case split + if (m_all_th_case_split_literals.contains(l.index())) { + TRACE("theory_case_split", tout << "assigned literal " << l.index() << " is a theory case split literal" << std::endl;); + // now find the sets of literals which contain l + vector case_split_sets = m_literal2casesplitsets.get(l.index(), vector()); + for (vector::const_iterator it = case_split_sets.begin(); it != case_split_sets.end(); ++it) { + literal_vector case_split_set = *it; + TRACE("theory_case_split", tout << "found case split set { "; + for(literal_vector::iterator set_it = case_split_set.begin(); set_it != case_split_set.end(); ++set_it) { + tout << set_it->index() << " "; + } + tout << "}" << std::endl;); + for(literal_vector::iterator set_it = case_split_set.begin(); set_it != case_split_set.end(); ++set_it) { + literal l2 = *set_it; + if (l2 != l) { + b_justification js(l); + switch (get_assignment(l2)) { + case l_false: + TRACE("theory_case_split", tout << "case split literal " << l2.index() << " is already assigned False" << std::endl;); + break; + case l_undef: + TRACE("theory_case_split", tout << "case split literal " << l2.index() << " is not assigned" << std::endl;); + assign(~l2, js); + break; + case l_true: + TRACE("theory_case_split", tout << "case split literal " << l2.index() << " is already assigned True" << std::endl;); + assign(~l2, js); + break; + } + if (inconsistent()) { + TRACE("theory_case_split", tout << "conflict detected!" << std::endl;); + return false; + } + } + } + } + } + } + // if we get here without detecting a conflict, we're fine + return true; + } + bool context::reduce_assertions() { if (!m_asserted_formulas.inconsistent()) { SASSERT(at_base_level()); diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index b3cfb4639..d9817236e 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -226,6 +226,16 @@ namespace smt { literal2assumption m_literal2assumption; // maps an expression associated with a literal to the original assumption expr_ref_vector m_unsat_core; + // ----------------------------------- + // + // Theory case split + // + // ----------------------------------- + uint_set m_all_th_case_split_literals; + vector m_th_case_split_sets; + u_map< vector > m_literal2casesplitsets; // returns the case split literal sets that a literal participates in + unsigned m_th_case_split_qhead; + // ----------------------------------- // // Accessors @@ -820,6 +830,21 @@ namespace smt { void mk_th_axiom(theory_id tid, literal l1, literal l2, literal l3, unsigned num_params = 0, parameter * params = 0); + /* + * Provide a hint to the core solver that the specified literals form a "theory case split". + * The core solver will enforce the condition that exactly one of these literals can be + * assigned 'true' at any time. + * We assume that the theory solver has already asserted the disjunction of these literals + * or some other axiom that means at least one of them must be assigned 'true'. + */ + void mk_th_case_split(unsigned num_lits, literal * lits); + + public: + + // helper function for trail + void undo_th_case_split(literal l); + + bool propagate_th_case_split(); bool_var mk_bool_var(expr * n); From 3bce61e0d432261ae173ac77659c773aa148940e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 1 May 2017 10:43:33 -0400 Subject: [PATCH 524/562] fix warning --- src/smt/smt_context.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index af1e94e62..9a841ea26 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -62,7 +62,6 @@ namespace smt { m_is_diseq_tmp(0), m_units_to_reassert(m_manager), m_qhead(0), - m_th_case_split_qhead(0), m_simp_qhead(0), m_simp_counter(0), m_bvar_inc(1.0), @@ -75,6 +74,7 @@ namespace smt { m_unsat_proof(m), m_unknown("unknown"), m_unsat_core(m), + m_th_case_split_qhead(0), #ifdef Z3DEBUG m_trail_enabled(true), #endif From d14f2af5ae5844d62be2a4408ffb22ef948e02d1 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Mon, 1 May 2017 15:22:06 -0700 Subject: [PATCH 525/562] deal with subtraction that manages to sneak in. Issue #996 Signed-off-by: Nikolaj Bjorner --- src/ast/simplifier/poly_simplifier_plugin.h | 2 +- src/cmd_context/check_logic.cpp | 1 + src/sat/sat_solver.cpp | 81 ++++++++++++--------- src/sat/sat_solver.h | 6 +- src/smt/theory_bv.cpp | 16 ++++ src/smt/theory_bv.h | 1 + src/tactic/portfolio/enum2bv_solver.cpp | 6 +- src/test/cnf_backbones.cpp | 18 +++-- 8 files changed, 84 insertions(+), 47 deletions(-) diff --git a/src/ast/simplifier/poly_simplifier_plugin.h b/src/ast/simplifier/poly_simplifier_plugin.h index 4e6ae9c15..ed6d506c5 100644 --- a/src/ast/simplifier/poly_simplifier_plugin.h +++ b/src/ast/simplifier/poly_simplifier_plugin.h @@ -37,7 +37,7 @@ protected: expr * mk_add(expr * arg1, expr * arg2) { expr * args[2] = { arg1, arg2 }; return mk_add(2, args); } expr * mk_mul(unsigned num_args, expr * const * args); expr * mk_mul(expr * arg1, expr * arg2) { expr * args[2] = { arg1, arg2 }; return mk_mul(2, args); } - expr * mk_sub(unsigned num_args, expr * const * args) { return m_manager.mk_app(m_fid, m_SUB, num_args, args); } + // expr * mk_sub(unsigned num_args, expr * const * args) { return m_manager.mk_app(m_fid, m_SUB, num_args, args); } expr * mk_uminus(expr * arg) { return m_manager.mk_app(m_fid, m_UMINUS, arg); } void process_monomial(unsigned num_args, expr * const * args, numeral & k, ptr_buffer & result); diff --git a/src/cmd_context/check_logic.cpp b/src/cmd_context/check_logic.cpp index c75c12689..0faddce73 100644 --- a/src/cmd_context/check_logic.cpp +++ b/src/cmd_context/check_logic.cpp @@ -187,6 +187,7 @@ struct check_logic::imp { m_bvs = true; m_uf = true; m_ints = true; + m_nonlinear = true; // non-linear 0-1 variables may get eliminated } else { m_unknown_logic = true; diff --git a/src/sat/sat_solver.cpp b/src/sat/sat_solver.cpp index d6b39c394..22418ed28 100644 --- a/src/sat/sat_solver.cpp +++ b/src/sat/sat_solver.cpp @@ -3180,7 +3180,7 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { static lbool core_chunking(sat::solver& s, model const& m, sat::bool_var_vector const& vars, sat::literal_vector const& asms, vector& conseq, unsigned K) { sat::literal_vector lambda; for (unsigned i = 0; i < vars.size(); i++) { - lambda.push_back(sat::literal(i, m[vars[i]] == l_false)); + lambda.push_back(sat::literal(vars[i], m[vars[i]] == l_false)); } while (!lambda.empty()) { IF_VERBOSE(1, verbose_stream() << "(sat-backbone-core " << lambda.size() << " " << conseq.size() << ")\n";); @@ -3259,9 +3259,9 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { } } - // is_sat = core_chunking(*this, mdl, vars, asms, conseq, 100); + is_sat = core_chunking(*this, mdl, vars, asms, conseq, 100); - is_sat = get_consequences(asms, lits, conseq); + // is_sat = get_consequences(asms, lits, conseq); set_model(mdl); return is_sat; } @@ -3371,13 +3371,14 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { propagate(false); if (check_inconsistent()) return l_false; - unsigned num_units = 0, num_iterations = 0; - extract_fixed_consequences(num_units, assumptions, unfixed_vars, conseq); + unsigned num_iterations = 0; + extract_fixed_consequences(unfixed_lits, assumptions, unfixed_vars, conseq); update_unfixed_literals(unfixed_lits, unfixed_vars); while (!unfixed_lits.empty()) { if (scope_lvl() > 1) { pop(scope_lvl() - 1); } + propagate(false); ++num_iterations; checkpoint(); literal_set::iterator it = unfixed_lits.begin(), end = unfixed_lits.end(); @@ -3389,8 +3390,9 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { literal lit = *it; if (value(lit) != l_undef) { ++num_fixed; - if (value(lit) == l_true && lvl(lit) == 1) { - VERIFY(extract_fixed_consequences(lit, assumptions, unfixed_vars, conseq)); + if (lvl(lit) <= 1) { + SASSERT(value(lit) == l_true); + extract_fixed_consequences(lit, assumptions, unfixed_vars, conseq); } continue; } @@ -3409,18 +3411,13 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { ++num_resolves; } if (false && scope_lvl() == 1) { + is_sat = l_undef; break; } } - if (scope_lvl() == 1) { - it = unfixed_lits.begin(); - for (; it != end; ++it) { - literal lit = *it; - if (value(lit) == l_true) { - VERIFY(extract_fixed_consequences(lit, assumptions, unfixed_vars, conseq)); - } - } - } + + extract_fixed_consequences(unfixed_lits, assumptions, unfixed_vars, conseq); + if (is_sat == l_true) { if (scope_lvl() == 1 && num_resolves > 0) { IF_VERBOSE(1, verbose_stream() << "(sat.get-consequences backjump)\n";); @@ -3431,6 +3428,7 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { if (is_sat == l_undef) { restart(); } + extract_fixed_consequences(unfixed_lits, assumptions, unfixed_vars, conseq); } } if (is_sat == l_false) { @@ -3440,7 +3438,6 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { if (is_sat == l_true) { delete_unfixed(unfixed_lits, unfixed_vars); } - extract_fixed_consequences(num_units, assumptions, unfixed_vars, conseq); update_unfixed_literals(unfixed_lits, unfixed_vars); IF_VERBOSE(1, verbose_stream() << "(sat.get-consequences" << " iterations: " << num_iterations @@ -3492,19 +3489,27 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { SASSERT(!inconsistent()); unsigned sz = m_trail.size(); for (unsigned i = start; i < sz && lvl(m_trail[i]) <= 1; ++i) { - if (!extract_fixed_consequences(m_trail[i], assumptions, unfixed, conseq)) { - for (i = 0; i < sz && lvl(m_trail[i]) <= 1; ++i) { - VERIFY(extract_fixed_consequences(m_trail[i], assumptions, unfixed, conseq)); - } - break; - } + extract_fixed_consequences(m_trail[i], assumptions, unfixed, conseq); } start = sz; } + void solver::extract_fixed_consequences(literal_set const& unfixed_lits, literal_set const& assumptions, bool_var_set& unfixed_vars, vector& conseq) { + literal_set::iterator it = unfixed_lits.begin(), end = unfixed_lits.end(); + for (; it != end; ++it) { + literal lit = *it; + if (lvl(lit) <= 1) { + SASSERT(value(lit) == l_true); + extract_fixed_consequences(lit, assumptions, unfixed_vars, conseq); + } + } + } + bool solver::check_domain(literal lit, literal lit2) { if (!m_antecedents.contains(lit2.var())) { + SASSERT(value(lit2) == l_true); m_todo_antecedents.push_back(lit2); + TRACE("sat", tout << "todo: " << lit2 << " " << value(lit2) << "\n";); return false; } else { @@ -3514,16 +3519,17 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { bool solver::extract_assumptions(literal lit, index_set& s) { justification js = m_justification[lit.var()]; + TRACE("sat", tout << lit << " " << js << "\n";); switch (js.get_kind()) { case justification::NONE: break; case justification::BINARY: - if (!check_domain(lit, js.get_literal())) return false; + if (!check_domain(lit, ~js.get_literal())) return false; s |= m_antecedents.find(js.get_literal().var()); break; case justification::TERNARY: - if (!check_domain(lit, js.get_literal1())) return false; - if (!check_domain(lit, js.get_literal2())) return false; + if (!check_domain(lit, ~js.get_literal1()) || + !check_domain(lit, ~js.get_literal2())) return false; s |= m_antecedents.find(js.get_literal1().var()); s |= m_antecedents.find(js.get_literal2().var()); break; @@ -3531,7 +3537,7 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { clause & c = *(m_cls_allocator.get_clause(js.get_clause_offset())); for (unsigned i = 0; i < c.size(); ++i) { if (c[i] != lit) { - if (!check_domain(lit, c[i])) return false; + if (!check_domain(lit, ~c[i])) return false; s |= m_antecedents.find(c[i].var()); } } @@ -3565,7 +3571,7 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { } - bool solver::extract_fixed_consequences(literal lit, literal_set const& assumptions, bool_var_set& unfixed, vector& conseq) { + bool solver::extract_fixed_consequences1(literal lit, literal_set const& assumptions, bool_var_set& unfixed, vector& conseq) { index_set s; if (m_antecedents.contains(lit.var())) { return true; @@ -3574,16 +3580,9 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { s.insert(lit.index()); } else { - SASSERT(m_todo_antecedents.empty()); if (!extract_assumptions(lit, s)) { SASSERT(!m_todo_antecedents.empty()); - while (!m_todo_antecedents.empty()) { - index_set s1; - if (extract_assumptions(m_todo_antecedents.back(), s1)) { - m_todo_antecedents.pop_back(); - } - } - VERIFY (extract_assumptions(lit, s)); + return false; } add_assumption(lit); } @@ -3601,6 +3600,16 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { return true; } + void solver::extract_fixed_consequences(literal lit, literal_set const& assumptions, bool_var_set& unfixed, vector& conseq) { + SASSERT(m_todo_antecedents.empty()); + m_todo_antecedents.push_back(lit); + while (!m_todo_antecedents.empty()) { + if (extract_fixed_consequences1(m_todo_antecedents.back(), assumptions, unfixed, conseq)) { + m_todo_antecedents.pop_back(); + } + } + } + void solver::asymmetric_branching() { if (scope_lvl() > 0 || inconsistent()) return; diff --git a/src/sat/sat_solver.h b/src/sat/sat_solver.h index 091162f23..a26abe1e0 100644 --- a/src/sat/sat_solver.h +++ b/src/sat/sat_solver.h @@ -498,7 +498,11 @@ namespace sat { void extract_fixed_consequences(unsigned& start, literal_set const& assumptions, bool_var_set& unfixed, vector& conseq); - bool extract_fixed_consequences(literal lit, literal_set const& assumptions, bool_var_set& unfixed, vector& conseq); + void extract_fixed_consequences(literal_set const& unfixed_lits, literal_set const& assumptions, bool_var_set& unfixed, vector& conseq); + + void extract_fixed_consequences(literal lit, literal_set const& assumptions, bool_var_set& unfixed, vector& conseq); + + bool extract_fixed_consequences1(literal lit, literal_set const& assumptions, bool_var_set& unfixed, vector& conseq); void update_unfixed_literals(literal_set& unfixed_lits, bool_var_set& unfixed_vars); diff --git a/src/smt/theory_bv.cpp b/src/smt/theory_bv.cpp index ae2aa95e2..255e2044f 100644 --- a/src/smt/theory_bv.cpp +++ b/src/smt/theory_bv.cpp @@ -762,6 +762,21 @@ namespace smt { TRACE("bv", tout << mk_pp(cond, get_manager()) << "\n"; tout << l << "\n";); \ } + void theory_bv::internalize_sub(app *n) { + SASSERT(!get_context().e_internalized(n)); + SASSERT(n->get_num_args() == 2); + process_args(n); + ast_manager & m = get_manager(); + enode * e = mk_enode(n); + expr_ref_vector arg1_bits(m), arg2_bits(m), bits(m); + get_arg_bits(e, 0, arg1_bits); + get_arg_bits(e, 1, arg2_bits); + SASSERT(arg1_bits.size() == arg2_bits.size()); + expr_ref carry(m); + m_bb.mk_subtracter(arg1_bits.size(), arg1_bits.c_ptr(), arg2_bits.c_ptr(), bits, carry); + init_bits(e, bits); + } + MK_UNARY(internalize_not, mk_not); MK_UNARY(internalize_redand, mk_redand); MK_UNARY(internalize_redor, mk_redor); @@ -848,6 +863,7 @@ namespace smt { switch (term->get_decl_kind()) { case OP_BV_NUM: internalize_num(term); return true; case OP_BADD: internalize_add(term); return true; + case OP_BSUB: internalize_sub(term); return true; case OP_BMUL: internalize_mul(term); return true; case OP_BSDIV_I: internalize_sdiv(term); return true; case OP_BUDIV_I: internalize_udiv(term); return true; diff --git a/src/smt/theory_bv.h b/src/smt/theory_bv.h index 17d03b412..50e4f9c30 100644 --- a/src/smt/theory_bv.h +++ b/src/smt/theory_bv.h @@ -172,6 +172,7 @@ namespace smt { bool get_fixed_value(theory_var v, numeral & result) const; void internalize_num(app * n); void internalize_add(app * n); + void internalize_sub(app * n); void internalize_mul(app * n); void internalize_udiv(app * n); void internalize_sdiv(app * n); diff --git a/src/tactic/portfolio/enum2bv_solver.cpp b/src/tactic/portfolio/enum2bv_solver.cpp index 9afd97de5..35601f374 100644 --- a/src/tactic/portfolio/enum2bv_solver.cpp +++ b/src/tactic/portfolio/enum2bv_solver.cpp @@ -137,8 +137,10 @@ public: SASSERT(num.is_unsigned()); expr_ref head(m); ptr_vector const& enums = *dt.get_datatype_constructors(f->get_range()); - head = m.mk_eq(m.mk_const(f), m.mk_const(enums[num.get_unsigned()])); - consequences[i] = m.mk_implies(a, head); + if (enums.size() > num.get_unsigned()) { + head = m.mk_eq(m.mk_const(f), m.mk_const(enums[num.get_unsigned()])); + consequences[i] = m.mk_implies(a, head); + } } } return r; diff --git a/src/test/cnf_backbones.cpp b/src/test/cnf_backbones.cpp index c34e109d3..b95977a46 100644 --- a/src/test/cnf_backbones.cpp +++ b/src/test/cnf_backbones.cpp @@ -152,14 +152,13 @@ static void brute_force_consequences(sat::solver& s, sat::literal_vector const& static lbool core_chunking(sat::solver& s, sat::bool_var_vector& vars, sat::literal_vector const& asms, vector& conseq, unsigned K) { lbool r = s.check(asms.size(), asms.c_ptr()); - display_status(r); if (r != l_true) { return r; } sat::model const & m = s.get_model(); sat::literal_vector lambda, backbones; - for (unsigned i = 1; i < m.size(); i++) { - lambda.push_back(sat::literal(i, m[i] == l_false)); + for (unsigned i = 0; i < vars.size(); i++) { + lambda.push_back(sat::literal(vars[i], m[vars[i]] == l_false)); } while (!lambda.empty()) { IF_VERBOSE(1, verbose_stream() << "(sat-backbone-core " << lambda.size() << " " << backbones.size() << ")\n";); @@ -270,10 +269,15 @@ static void cnf_backbones(bool use_chunk, char const* file_name) { } void tst_cnf_backbones(char ** argv, int argc, int& i) { + bool use_chunk = i + 1 < argc && argv[i + 1] == std::string("chunk"); + if (use_chunk) ++i; + char const* file = ""; if (i + 1 < argc) { - bool use_chunk = (i + 2 < argc && argv[i + 1] == std::string("chunk")); - if (use_chunk) ++i; - cnf_backbones(use_chunk, argv[i + 1]); - ++i; + file = argv[i + 1]; } + else { + file = argv[1]; + } + cnf_backbones(use_chunk, file); + ++i; } From b86d472eaf5767cc2d870a08d0c22b70b2262f47 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 1 May 2017 18:22:49 -0400 Subject: [PATCH 526/562] simplify theory case split handling --- src/smt/smt_context.cpp | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 9a841ea26..af45f0fcc 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -3057,19 +3057,8 @@ namespace smt { literal l2 = *set_it; if (l2 != l) { b_justification js(l); - switch (get_assignment(l2)) { - case l_false: - TRACE("theory_case_split", tout << "case split literal " << l2.index() << " is already assigned False" << std::endl;); - break; - case l_undef: - TRACE("theory_case_split", tout << "case split literal " << l2.index() << " is not assigned" << std::endl;); - assign(~l2, js); - break; - case l_true: - TRACE("theory_case_split", tout << "case split literal " << l2.index() << " is already assigned True" << std::endl;); - assign(~l2, js); - break; - } + TRACE("theory_case_split", tout << "case split literal "; l2.display(tout, m_manager, m_bool_var2expr.c_ptr());); + assign(~l2, js); if (inconsistent()) { TRACE("theory_case_split", tout << "conflict detected!" << std::endl;); return false; From f9105edb14c6d6aca605bc61ce206c57f95a28f4 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Mon, 1 May 2017 15:22:52 -0700 Subject: [PATCH 527/562] revert to native chunker Signed-off-by: Nikolaj Bjorner --- src/sat/sat_solver.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sat/sat_solver.cpp b/src/sat/sat_solver.cpp index 22418ed28..dc5aa2964 100644 --- a/src/sat/sat_solver.cpp +++ b/src/sat/sat_solver.cpp @@ -3259,9 +3259,9 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { } } - is_sat = core_chunking(*this, mdl, vars, asms, conseq, 100); + // is_sat = core_chunking(*this, mdl, vars, asms, conseq, 100); - // is_sat = get_consequences(asms, lits, conseq); + is_sat = get_consequences(asms, lits, conseq); set_model(mdl); return is_sat; } From 16a5e944d78aae384f2acb17e1bd60c6499f965e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Mon, 1 May 2017 18:25:54 -0400 Subject: [PATCH 528/562] use reference for case split sets --- src/smt/smt_context.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index af45f0fcc..602ff5160 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -3045,7 +3045,7 @@ namespace smt { if (m_all_th_case_split_literals.contains(l.index())) { TRACE("theory_case_split", tout << "assigned literal " << l.index() << " is a theory case split literal" << std::endl;); // now find the sets of literals which contain l - vector case_split_sets = m_literal2casesplitsets.get(l.index(), vector()); + vector & case_split_sets = m_literal2casesplitsets.get(l.index(), vector()); for (vector::const_iterator it = case_split_sets.begin(); it != case_split_sets.end(); ++it) { literal_vector case_split_set = *it; TRACE("theory_case_split", tout << "found case split set { "; From 8ba78081ecc8057eb03d8447e9778157d9d20bba Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Mon, 1 May 2017 16:41:17 -0700 Subject: [PATCH 529/562] fix build break Signed-off-by: Nikolaj Bjorner --- src/smt/smt_context.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 602ff5160..03eb38043 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -3045,7 +3045,7 @@ namespace smt { if (m_all_th_case_split_literals.contains(l.index())) { TRACE("theory_case_split", tout << "assigned literal " << l.index() << " is a theory case split literal" << std::endl;); // now find the sets of literals which contain l - vector & case_split_sets = m_literal2casesplitsets.get(l.index(), vector()); + vector const& case_split_sets = m_literal2casesplitsets[l.index()]; for (vector::const_iterator it = case_split_sets.begin(); it != case_split_sets.end(); ++it) { literal_vector case_split_set = *it; TRACE("theory_case_split", tout << "found case split set { "; From 48e37b0e160313a58fe946d84baa7445bb721a06 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Mon, 1 May 2017 16:54:22 -0700 Subject: [PATCH 530/562] pass qhead Signed-off-by: Nikolaj Bjorner --- src/smt/smt_context.cpp | 73 +++++++++++++++++++---------------------- src/smt/smt_context.h | 3 +- 2 files changed, 35 insertions(+), 41 deletions(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 03eb38043..abac9ef82 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -74,7 +74,6 @@ namespace smt { m_unsat_proof(m), m_unknown("unknown"), m_unsat_core(m), - m_th_case_split_qhead(0), #ifdef Z3DEBUG m_trail_enabled(true), #endif @@ -340,7 +339,6 @@ namespace smt { bool context::bcp() { SASSERT(!inconsistent()); - m_th_case_split_qhead = m_qhead; while (m_qhead < m_assigned_literals.size()) { if (get_cancel_flag()) { return true; @@ -1770,7 +1768,7 @@ namespace smt { unsigned qhead = m_qhead; if (!bcp()) return false; - if (!propagate_th_case_split()) + if (!propagate_th_case_split(qhead)) return false; if (get_cancel_flag()) { m_qhead = qhead; @@ -2979,16 +2977,13 @@ namespace smt { TRACE("theory_case_split", display_literals_verbose(tout << "theory case split: ", num_lits, lits); tout << std::endl;); // If we don't use the theory case split heuristic, // for each pair of literals (l1, l2) we add the clause (~l1 OR ~l2) - // to enforce the condition that more than one literal can't be - // assigned 'true' simultaneously. + // to enforce the condition that at most one literal can be assigned 'true'. if (!m_fparams.m_theory_case_split) { for (unsigned i = 0; i < num_lits; ++i) { for (unsigned j = i+1; j < num_lits; ++j) { literal l1 = lits[i]; literal l2 = lits[j]; - literal excl[2] = {~l1, ~l2}; - justification * j_excl = 0; - mk_clause(2, excl, j_excl); + mk_clause(~l1, ~l2, (justification*) 0); } } } else { @@ -3010,11 +3005,11 @@ namespace smt { m_literal2casesplitsets[l.index()].push_back(new_case_split); } TRACE("theory_case_split", tout << "tracking case split literal set { "; - for (unsigned i = 0; i < num_lits; ++i) { - tout << lits[i].index() << " "; - } - tout << "}" << std::endl; - ); + for (unsigned i = 0; i < num_lits; ++i) { + tout << lits[i].index() << " "; + } + tout << "}" << std::endl; + ); } } @@ -3027,7 +3022,7 @@ namespace smt { } } - bool context::propagate_th_case_split() { + bool context::propagate_th_case_split(unsigned qhead) { if (m_all_th_case_split_literals.empty()) return true; @@ -3035,34 +3030,34 @@ namespace smt { // not counting any literals that get assigned by this method // this relies on bcp() to give us its old m_qhead and therefore // bcp() should always be called before this method - unsigned assigned_literal_idx = m_th_case_split_qhead; + unsigned assigned_literal_end = m_assigned_literals.size(); - while(assigned_literal_idx < assigned_literal_end) { - literal l = m_assigned_literals[assigned_literal_idx]; + for (; qhead < assigned_literal_end; ++qhead) { + literal l = m_assigned_literals[qhead]; TRACE("theory_case_split", tout << "check literal " << l.index() << std::endl; display_literal_verbose(tout, l); tout << std::endl;); - ++assigned_literal_idx; // check if this literal participates in any theory case split - if (m_all_th_case_split_literals.contains(l.index())) { - TRACE("theory_case_split", tout << "assigned literal " << l.index() << " is a theory case split literal" << std::endl;); - // now find the sets of literals which contain l - vector const& case_split_sets = m_literal2casesplitsets[l.index()]; - for (vector::const_iterator it = case_split_sets.begin(); it != case_split_sets.end(); ++it) { - literal_vector case_split_set = *it; - TRACE("theory_case_split", tout << "found case split set { "; - for(literal_vector::iterator set_it = case_split_set.begin(); set_it != case_split_set.end(); ++set_it) { - tout << set_it->index() << " "; - } - tout << "}" << std::endl;); - for(literal_vector::iterator set_it = case_split_set.begin(); set_it != case_split_set.end(); ++set_it) { - literal l2 = *set_it; - if (l2 != l) { - b_justification js(l); - TRACE("theory_case_split", tout << "case split literal "; l2.display(tout, m_manager, m_bool_var2expr.c_ptr());); - assign(~l2, js); - if (inconsistent()) { - TRACE("theory_case_split", tout << "conflict detected!" << std::endl;); - return false; - } + if (!m_all_th_case_split_literals.contains(l.index())) { + continue; + } + TRACE("theory_case_split", tout << "assigned literal " << l.index() << " is a theory case split literal" << std::endl;); + // now find the sets of literals which contain l + vector const& case_split_sets = m_literal2casesplitsets[l.index()]; + for (vector::const_iterator it = case_split_sets.begin(); it != case_split_sets.end(); ++it) { + literal_vector case_split_set = *it; + TRACE("theory_case_split", tout << "found case split set { "; + for(literal_vector::iterator set_it = case_split_set.begin(); set_it != case_split_set.end(); ++set_it) { + tout << set_it->index() << " "; + } + tout << "}" << std::endl;); + for(literal_vector::iterator set_it = case_split_set.begin(); set_it != case_split_set.end(); ++set_it) { + literal l2 = *set_it; + if (l2 != l) { + b_justification js(l); + TRACE("theory_case_split", tout << "case split literal "; l2.display(tout, m_manager, m_bool_var2expr.c_ptr());); + assign(~l2, js); + if (inconsistent()) { + TRACE("theory_case_split", tout << "conflict detected!" << std::endl;); + return false; } } } diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index d9817236e..ca2429be8 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -234,7 +234,6 @@ namespace smt { uint_set m_all_th_case_split_literals; vector m_th_case_split_sets; u_map< vector > m_literal2casesplitsets; // returns the case split literal sets that a literal participates in - unsigned m_th_case_split_qhead; // ----------------------------------- // @@ -844,7 +843,7 @@ namespace smt { // helper function for trail void undo_th_case_split(literal l); - bool propagate_th_case_split(); + bool propagate_th_case_split(unsigned qhead); bool_var mk_bool_var(expr * n); From 6cd1f877b84a9d488d37035bff800e69e02221a7 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 2 May 2017 10:39:32 -0400 Subject: [PATCH 531/562] params for theory aware branching --- src/smt/params/smt_params.cpp | 1 + src/smt/params/smt_params.h | 3 +++ src/smt/params/smt_params_helper.pyg | 1 + 3 files changed, 5 insertions(+) diff --git a/src/smt/params/smt_params.cpp b/src/smt/params/smt_params.cpp index 4b7920596..d4eb0b394 100644 --- a/src/smt/params/smt_params.cpp +++ b/src/smt/params/smt_params.cpp @@ -32,6 +32,7 @@ void smt_params::updt_local_params(params_ref const & _p) { m_restart_factor = p.restart_factor(); m_case_split_strategy = static_cast(p.case_split()); m_theory_case_split = p.theory_case_split(); + m_theory_aware_branching = p.theory_aware_branching(); m_delay_units = p.delay_units(); m_delay_units_threshold = p.delay_units_threshold(); m_preprocess = _p.get_bool("preprocess", true); // hidden parameter diff --git a/src/smt/params/smt_params.h b/src/smt/params/smt_params.h index c03eaeaef..eacee95ab 100644 --- a/src/smt/params/smt_params.h +++ b/src/smt/params/smt_params.h @@ -110,6 +110,7 @@ struct smt_params : public preprocessor_params, unsigned m_rel_case_split_order; bool m_lookahead_diseq; bool m_theory_case_split; + bool m_theory_aware_branching; // ----------------------------------- // @@ -240,6 +241,8 @@ struct smt_params : public preprocessor_params, m_case_split_strategy(CS_ACTIVITY_DELAY_NEW), m_rel_case_split_order(0), m_lookahead_diseq(false), + m_theory_case_split(false), + m_theory_aware_branching(false), m_delay_units(false), m_delay_units_threshold(32), m_theory_resolve(false), diff --git a/src/smt/params/smt_params_helper.pyg b/src/smt/params/smt_params_helper.pyg index faa48400d..4c9bbc677 100644 --- a/src/smt/params/smt_params_helper.pyg +++ b/src/smt/params/smt_params_helper.pyg @@ -62,6 +62,7 @@ def_module_params(module_name='smt', ('dack.gc_inv_decay', DOUBLE, 0.8, 'Dynamic ackermannization garbage collection decay'), ('dack.threshold', UINT, 10, ' number of times the congruence rule must be used before Leibniz\'s axiom is expanded'), ('theory_case_split', BOOL, False, 'Allow the context to use heuristics involving theory case splits, which are a set of literals of which exactly one can be assigned True. If this option is false, the context will generate extra axioms to enforce this instead.'), + ('theory_aware_branching', BOOL, False, 'Allow the context to use extra information from theory solvers regarding literal branching prioritization.'), ('core.validate', BOOL, False, 'validate unsat core produced by SMT context'), ('core.minimize', BOOL, False, 'minimize unsat core produced by SMT context'), ('core.extend_patterns', BOOL, False, 'extend unsat core with literals that trigger (potential) quantifier instances'), From 5b4792955dbc954a5d80ce1e5e9210ea82c33e2b Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 2 May 2017 10:43:40 -0400 Subject: [PATCH 532/562] theory-aware branching heuristic --- src/smt/smt_case_split_queue.cpp | 176 ++++++++++++++++++++++++++++--- src/smt/smt_case_split_queue.h | 3 + src/smt/smt_context.cpp | 4 + src/smt/smt_context.h | 10 +- 4 files changed, 176 insertions(+), 17 deletions(-) diff --git a/src/smt/smt_case_split_queue.cpp b/src/smt/smt_case_split_queue.cpp index 06004e3b8..35cdcb6fe 100644 --- a/src/smt/smt_case_split_queue.cpp +++ b/src/smt/smt_case_split_queue.cpp @@ -22,9 +22,13 @@ Revision History: #include"stopwatch.h" #include"for_each_expr.h" #include"ast_pp.h" +#include"map.h" +#include"hashtable.h" namespace smt { + typedef map > theory_var_priority_map; + struct bool_var_act_lt { svector const & m_activity; bool_var_act_lt(svector const & a):m_activity(a) {} @@ -35,6 +39,27 @@ namespace smt { typedef heap bool_var_act_queue; + struct theory_aware_act_lt { + svector const & m_activity; + theory_var_priority_map const & m_theory_var_priority; + theory_aware_act_lt(svector const & act, theory_var_priority_map const & a):m_activity(act),m_theory_var_priority(a) {} + bool operator()(bool_var v1, bool_var v2) const { + double p_v1, p_v2; + if (!m_theory_var_priority.find(v1, p_v1)) { + p_v1 = 0.0; + } + if (!m_theory_var_priority.find(v2, p_v2)) { + p_v2 = 0.0; + } + // add clause activity + p_v1 += m_activity[v1]; + p_v2 += m_activity[v2]; + return p_v1 > p_v2; + } + }; + + typedef heap theory_aware_act_queue; + /** \brief Case split queue based on activity and random splits. */ @@ -1086,32 +1111,151 @@ namespace smt { m_params.m_qi_eager_threshold += start_gen; } }; - + + class theory_aware_branching_queue : public case_split_queue { + protected: + context & m_context; + smt_params & m_params; + theory_var_priority_map m_theory_var_priority; + theory_aware_act_queue m_queue; + + int_hashtable > m_theory_vars; + map > m_theory_var_phase; + public: + theory_aware_branching_queue(context & ctx, smt_params & p): + m_context(ctx), + m_params(p), + m_theory_var_priority(), + m_queue(1024, theory_aware_act_lt(ctx.get_activity_vector(), m_theory_var_priority)) { + } + + virtual void activity_increased_eh(bool_var v) { + if (m_queue.contains(v)) + m_queue.decreased(v); + } + + virtual void mk_var_eh(bool_var v) { + m_queue.reserve(v+1); + m_queue.insert(v); + } + + virtual void del_var_eh(bool_var v) { + if (m_queue.contains(v)) + m_queue.erase(v); + } + + virtual void unassign_var_eh(bool_var v) { + if (!m_queue.contains(v)) + m_queue.insert(v); + } + + virtual void relevant_eh(expr * n) {} + + virtual void init_search_eh() {} + + virtual void end_search_eh() {} + + virtual void reset() { + m_queue.reset(); + } + + virtual void push_scope() {} + + virtual void pop_scope(unsigned num_scopes) {} + + virtual void next_case_split(bool_var & next, lbool & phase) { + phase = l_undef; + + if (m_context.get_random_value() < static_cast(m_params.m_random_var_freq * random_gen::max_value())) { + next = m_context.get_random_value() % m_context.get_num_b_internalized(); + TRACE("random_split", tout << "next: " << next << " get_assignment(next): " << m_context.get_assignment(next) << "\n";); + if (m_context.get_assignment(next) == l_undef) + return; + } + + while (!m_queue.empty()) { + next = m_queue.erase_min(); + if (m_context.get_assignment(next) == l_undef) + return; + } + + next = null_bool_var; + if (m_theory_vars.contains(next)) { + if (!m_theory_var_phase.find(next, phase)) { + phase = l_undef; + } + } + } + + virtual void add_theory_aware_branching_info(bool_var v, double priority, lbool phase) { + TRACE("theory_aware_branching", tout << "Add theory-aware branching information for l#" << v << ": priority=" << priority << std::endl;); + m_theory_vars.insert(v); + m_theory_var_phase.insert(v, phase); + m_theory_var_priority.insert(v, priority); + if (m_queue.contains(v)) { + if (priority > 0.0) { + m_queue.decreased(v); + } else { + m_queue.increased(v); + } + } + // m_theory_queue.reserve(v+1); + // m_theory_queue.insert(v); + } + + virtual void display(std::ostream & out) { + bool first = true; + bool_var_act_queue::const_iterator it = m_queue.begin(); + bool_var_act_queue::const_iterator end = m_queue.end(); + for (; it != end ; ++it) { + unsigned v = *it; + if (m_context.get_assignment(v) == l_undef) { + if (first) { + out << "remaining case-splits:\n"; + first = false; + } + out << "#" << m_context.bool_var2expr(v)->get_id() << " "; + } + } + if (!first) + out << "\n"; + + } + + virtual ~theory_aware_branching_queue() {}; + }; + case_split_queue * mk_case_split_queue(context & ctx, smt_params & p) { if (p.m_relevancy_lvl < 2 && (p.m_case_split_strategy == CS_RELEVANCY || p.m_case_split_strategy == CS_RELEVANCY_ACTIVITY || - p.m_case_split_strategy == CS_RELEVANCY_GOAL)) { + p.m_case_split_strategy == CS_RELEVANCY_GOAL)) { warning_msg("relevancy must be enabled to use option CASE_SPLIT=3, 4 or 5"); p.m_case_split_strategy = CS_ACTIVITY; } if (p.m_auto_config && (p.m_case_split_strategy == CS_RELEVANCY || p.m_case_split_strategy == CS_RELEVANCY_ACTIVITY || - p.m_case_split_strategy == CS_RELEVANCY_GOAL)) { + p.m_case_split_strategy == CS_RELEVANCY_GOAL)) { warning_msg("auto configuration (option AUTO_CONFIG) must be disabled to use option CASE_SPLIT=3, 4 or 5"); p.m_case_split_strategy = CS_ACTIVITY; } - switch (p.m_case_split_strategy) { - case CS_ACTIVITY_DELAY_NEW: - return alloc(dact_case_split_queue, ctx, p); - case CS_ACTIVITY_WITH_CACHE: - return alloc(cact_case_split_queue, ctx, p); - case CS_RELEVANCY: - return alloc(rel_case_split_queue, ctx, p); - case CS_RELEVANCY_ACTIVITY: - return alloc(rel_act_case_split_queue, ctx, p); - case CS_RELEVANCY_GOAL: - return alloc(rel_goal_case_split_queue, ctx, p); - default: - return alloc(act_case_split_queue, ctx, p); + + if (p.m_theory_aware_branching) { + // override + return alloc(theory_aware_branching_queue, ctx, p); + } else { + switch (p.m_case_split_strategy) { + case CS_ACTIVITY_DELAY_NEW: + return alloc(dact_case_split_queue, ctx, p); + case CS_ACTIVITY_WITH_CACHE: + return alloc(cact_case_split_queue, ctx, p); + case CS_RELEVANCY: + return alloc(rel_case_split_queue, ctx, p); + case CS_RELEVANCY_ACTIVITY: + return alloc(rel_act_case_split_queue, ctx, p); + case CS_RELEVANCY_GOAL: + return alloc(rel_goal_case_split_queue, ctx, p); + default: + return alloc(act_case_split_queue, ctx, p); + } } } diff --git a/src/smt/smt_case_split_queue.h b/src/smt/smt_case_split_queue.h index e6b217a22..9a3a93cc6 100644 --- a/src/smt/smt_case_split_queue.h +++ b/src/smt/smt_case_split_queue.h @@ -46,6 +46,9 @@ namespace smt { virtual void next_case_split(bool_var & next, lbool & phase) = 0; virtual void display(std::ostream & out) = 0; virtual ~case_split_queue() {} + + // theory-aware branching hint + virtual void add_theory_aware_branching_info(bool_var v, double priority, lbool phase) {} }; case_split_queue * mk_case_split_queue(context & ctx, smt_params & p); diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index abac9ef82..a6cef548f 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -3022,6 +3022,10 @@ namespace smt { } } + void context::add_theory_aware_branching_info(bool_var v, double priority, lbool phase) { + m_case_split_queue->add_theory_aware_branching_info(v, priority, phase); + } + bool context::propagate_th_case_split(unsigned qhead) { if (m_all_th_case_split_literals.empty()) return true; diff --git a/src/smt/smt_context.h b/src/smt/smt_context.h index ca2429be8..9c70f5999 100644 --- a/src/smt/smt_context.h +++ b/src/smt/smt_context.h @@ -838,12 +838,20 @@ namespace smt { */ void mk_th_case_split(unsigned num_lits, literal * lits); + + /* + * Provide a hint to the branching heuristic about the priority of a "theory-aware literal". + * Literals marked in this way will always be branched on before unmarked literals, + * starting with the literal having the highest priority. + */ + void add_theory_aware_branching_info(bool_var v, double priority, lbool phase); + public: // helper function for trail void undo_th_case_split(literal l); - bool propagate_th_case_split(unsigned qhead); + bool propagate_th_case_split(unsigned qhead); bool_var mk_bool_var(expr * n); From a8d069ba460d2d839986ceceec1bf6fa7051af7e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 2 May 2017 13:06:08 -0400 Subject: [PATCH 533/562] refactor: add asserts, use case split strategy param --- src/smt/params/smt_params.cpp | 1 - src/smt/params/smt_params.h | 5 ++-- src/smt/params/smt_params_helper.pyg | 3 +-- src/smt/smt_case_split_queue.cpp | 39 +++++++++++++--------------- 4 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/smt/params/smt_params.cpp b/src/smt/params/smt_params.cpp index d4eb0b394..4b7920596 100644 --- a/src/smt/params/smt_params.cpp +++ b/src/smt/params/smt_params.cpp @@ -32,7 +32,6 @@ void smt_params::updt_local_params(params_ref const & _p) { m_restart_factor = p.restart_factor(); m_case_split_strategy = static_cast(p.case_split()); m_theory_case_split = p.theory_case_split(); - m_theory_aware_branching = p.theory_aware_branching(); m_delay_units = p.delay_units(); m_delay_units_threshold = p.delay_units_threshold(); m_preprocess = _p.get_bool("preprocess", true); // hidden parameter diff --git a/src/smt/params/smt_params.h b/src/smt/params/smt_params.h index eacee95ab..07ae99242 100644 --- a/src/smt/params/smt_params.h +++ b/src/smt/params/smt_params.h @@ -66,7 +66,8 @@ enum case_split_strategy { CS_ACTIVITY_WITH_CACHE, // case split based on activity and cache the activity CS_RELEVANCY, // case split based on relevancy CS_RELEVANCY_ACTIVITY, // case split based on relevancy and activity - CS_RELEVANCY_GOAL // based on relevancy and the current goal + CS_RELEVANCY_GOAL, // based on relevancy and the current goal + CS_ACTIVITY_THEORY_AWARE_BRANCHING // activity-based case split, but theory solvers can manipulate activity }; struct smt_params : public preprocessor_params, @@ -110,7 +111,6 @@ struct smt_params : public preprocessor_params, unsigned m_rel_case_split_order; bool m_lookahead_diseq; bool m_theory_case_split; - bool m_theory_aware_branching; // ----------------------------------- // @@ -242,7 +242,6 @@ struct smt_params : public preprocessor_params, m_rel_case_split_order(0), m_lookahead_diseq(false), m_theory_case_split(false), - m_theory_aware_branching(false), m_delay_units(false), m_delay_units_threshold(32), m_theory_resolve(false), diff --git a/src/smt/params/smt_params_helper.pyg b/src/smt/params/smt_params_helper.pyg index 4c9bbc677..450d2eff3 100644 --- a/src/smt/params/smt_params_helper.pyg +++ b/src/smt/params/smt_params_helper.pyg @@ -11,7 +11,7 @@ def_module_params(module_name='smt', ('phase_selection', UINT, 3, 'phase selection heuristic: 0 - always false, 1 - always true, 2 - phase caching, 3 - phase caching conservative, 4 - phase caching conservative 2, 5 - random, 6 - number of occurrences'), ('restart_strategy', UINT, 1, '0 - geometric, 1 - inner-outer-geometric, 2 - luby, 3 - fixed, 4 - arithmetic'), ('restart_factor', DOUBLE, 1.1, 'when using geometric (or inner-outer-geometric) progression of restarts, it specifies the constant used to multiply the currect restart threshold'), - ('case_split', UINT, 1, '0 - case split based on variable activity, 1 - similar to 0, but delay case splits created during the search, 2 - similar to 0, but cache the relevancy, 3 - case split based on relevancy (structural splitting), 4 - case split on relevancy and activity, 5 - case split on relevancy and current goal'), + ('case_split', UINT, 1, '0 - case split based on variable activity, 1 - similar to 0, but delay case splits created during the search, 2 - similar to 0, but cache the relevancy, 3 - case split based on relevancy (structural splitting), 4 - case split on relevancy and activity, 5 - case split on relevancy and current goal, 6 - activity-based case split with theory-aware branching activity'), ('delay_units', BOOL, False, 'if true then z3 will not restart when a unit clause is learned'), ('delay_units_threshold', UINT, 32, 'maximum number of learned unit clauses before restarting, ignored if delay_units is false'), ('pull_nested_quantifiers', BOOL, False, 'pull nested quantifiers'), @@ -62,7 +62,6 @@ def_module_params(module_name='smt', ('dack.gc_inv_decay', DOUBLE, 0.8, 'Dynamic ackermannization garbage collection decay'), ('dack.threshold', UINT, 10, ' number of times the congruence rule must be used before Leibniz\'s axiom is expanded'), ('theory_case_split', BOOL, False, 'Allow the context to use heuristics involving theory case splits, which are a set of literals of which exactly one can be assigned True. If this option is false, the context will generate extra axioms to enforce this instead.'), - ('theory_aware_branching', BOOL, False, 'Allow the context to use extra information from theory solvers regarding literal branching prioritization.'), ('core.validate', BOOL, False, 'validate unsat core produced by SMT context'), ('core.minimize', BOOL, False, 'minimize unsat core produced by SMT context'), ('core.extend_patterns', BOOL, False, 'extend unsat core with literals that trigger (potential) quantifier instances'), diff --git a/src/smt/smt_case_split_queue.cpp b/src/smt/smt_case_split_queue.cpp index 35cdcb6fe..129d77c85 100644 --- a/src/smt/smt_case_split_queue.cpp +++ b/src/smt/smt_case_split_queue.cpp @@ -1164,9 +1164,10 @@ namespace smt { virtual void pop_scope(unsigned num_scopes) {} virtual void next_case_split(bool_var & next, lbool & phase) { - phase = l_undef; - - if (m_context.get_random_value() < static_cast(m_params.m_random_var_freq * random_gen::max_value())) { + int threshold = static_cast(m_params.m_random_var_freq * random_gen::max_value()); + SASSERT(threshold >= 0); + if (m_context.get_random_value() < threshold) { + SASSERT(m_context.get_num_b_internalized() > 0); next = m_context.get_random_value() % m_context.get_num_b_internalized(); TRACE("random_split", tout << "next: " << next << " get_assignment(next): " << m_context.get_assignment(next) << "\n";); if (m_context.get_assignment(next) == l_undef) @@ -1237,25 +1238,21 @@ namespace smt { warning_msg("auto configuration (option AUTO_CONFIG) must be disabled to use option CASE_SPLIT=3, 4 or 5"); p.m_case_split_strategy = CS_ACTIVITY; } - - if (p.m_theory_aware_branching) { - // override + switch (p.m_case_split_strategy) { + case CS_ACTIVITY_DELAY_NEW: + return alloc(dact_case_split_queue, ctx, p); + case CS_ACTIVITY_WITH_CACHE: + return alloc(cact_case_split_queue, ctx, p); + case CS_RELEVANCY: + return alloc(rel_case_split_queue, ctx, p); + case CS_RELEVANCY_ACTIVITY: + return alloc(rel_act_case_split_queue, ctx, p); + case CS_RELEVANCY_GOAL: + return alloc(rel_goal_case_split_queue, ctx, p); + case CS_ACTIVITY_THEORY_AWARE_BRANCHING: return alloc(theory_aware_branching_queue, ctx, p); - } else { - switch (p.m_case_split_strategy) { - case CS_ACTIVITY_DELAY_NEW: - return alloc(dact_case_split_queue, ctx, p); - case CS_ACTIVITY_WITH_CACHE: - return alloc(cact_case_split_queue, ctx, p); - case CS_RELEVANCY: - return alloc(rel_case_split_queue, ctx, p); - case CS_RELEVANCY_ACTIVITY: - return alloc(rel_act_case_split_queue, ctx, p); - case CS_RELEVANCY_GOAL: - return alloc(rel_goal_case_split_queue, ctx, p); - default: - return alloc(act_case_split_queue, ctx, p); - } + default: + return alloc(act_case_split_queue, ctx, p); } } From 15cb2d7dbad86ce0411c902278ba8c57bb66474e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 2 May 2017 14:08:48 -0400 Subject: [PATCH 534/562] cleanup --- src/smt/params/smt_params.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/smt/params/smt_params.h b/src/smt/params/smt_params.h index 07ae99242..49487c4dd 100644 --- a/src/smt/params/smt_params.h +++ b/src/smt/params/smt_params.h @@ -241,7 +241,6 @@ struct smt_params : public preprocessor_params, m_case_split_strategy(CS_ACTIVITY_DELAY_NEW), m_rel_case_split_order(0), m_lookahead_diseq(false), - m_theory_case_split(false), m_delay_units(false), m_delay_units_threshold(32), m_theory_resolve(false), From e6d527c5d576333ce8be7d9ace47aa52603956aa Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 2 May 2017 15:39:15 -0400 Subject: [PATCH 535/562] remove trace code from theory_arith --- src/smt/theory_arith.h | 14 +------------ src/smt/theory_arith_core.h | 41 ++----------------------------------- 2 files changed, 3 insertions(+), 52 deletions(-) diff --git a/src/smt/theory_arith.h b/src/smt/theory_arith.h index eb36a92fb..439adbdff 100644 --- a/src/smt/theory_arith.h +++ b/src/smt/theory_arith.h @@ -577,19 +577,7 @@ namespace smt { return is_free(get_context().get_enode(n)->get_th_var(get_id())); } bool is_fixed(theory_var v) const; - void set_bound_core(theory_var v, bound * new_bound, bool upper) { - TRACE("t_str_int", - tout << "setting " << (upper ? "upper" : "lower") << " bound "; - if (new_bound) { - tout << new_bound->get_value(); - } else { - tout << "(NULL)"; - } - tout << " for theory var v#" << v; - tout << std::endl; - ); - m_bounds[static_cast(upper)][v] = new_bound; - } + void set_bound_core(theory_var v, bound * new_bound, bool upper) { m_bounds[static_cast(upper)][v] = new_bound; } void restore_bound(theory_var v, bound * new_bound, bool upper) { set_bound_core(v, new_bound, upper); } void restore_nl_propagated_flag(unsigned old_trail_size); void set_bound(bound * new_bound, bool upper); diff --git a/src/smt/theory_arith_core.h b/src/smt/theory_arith_core.h index 7fed094da..dd1924e44 100644 --- a/src/smt/theory_arith_core.h +++ b/src/smt/theory_arith_core.h @@ -3263,50 +3263,13 @@ namespace smt { bool theory_arith::get_value(enode * n, expr_ref & r) { theory_var v = n->get_th_var(get_id()); inf_numeral val; - // rewrites for tracing purposes - if (v == null_theory_var) { - TRACE("t_str_int", tout << "WARNING: enode " << mk_pp(n->get_owner(), get_manager()) - << " attached to null theory var" << std::endl; - ); - return false; - } else { - val = get_value(v); - TRACE("t_str_int", tout << "enode " << mk_pp(n->get_owner(), get_manager()) - << " attached to theory var v#" << v - << ", has val = " << val - << std::endl; - ); - if (!is_int(v) || val.is_int()) { - return to_expr(val, is_int(v), r); - } else { - return false; - } - } - // return v != null_theory_var && (val = get_value(v), (!is_int(v) || val.is_int())) && to_expr(val, is_int(v), r); + return v != null_theory_var && (val = get_value(v), (!is_int(v) || val.is_int())) && to_expr(val, is_int(v), r); } template bool theory_arith::get_lower(enode * n, expr_ref & r) { theory_var v = n->get_th_var(get_id()); - bound * b; - if (v == null_theory_var) { - TRACE("t_str_int", tout << "WARNING: enode " << mk_pp(n->get_owner(), get_manager()) - << " attached to null theory var" << std::endl; - ); - b = 0; - } else { - b = lower(v); - TRACE("t_str_int", - tout << "enode " << mk_pp(n->get_owner(), get_manager()) - << " attached to theory var v#" << v - << std::endl; - if (b) { - tout << "lower bound = " << b->get_value() << std::endl; - } else { - tout << "WARNING: b = NULL" << std::endl; - } - ); - } + bound * b = (v == null_theory_var) ? 0 : lower(v); return b && to_expr(b->get_value(), is_int(v), r); } From a418f0c30b8ea589e08cb8aa1c187ebb1f7003bd Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 2 May 2017 15:52:35 -0400 Subject: [PATCH 536/562] fix spacing --- src/smt/theory_arith_core.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/theory_arith_core.h b/src/smt/theory_arith_core.h index dd1924e44..5c652414a 100644 --- a/src/smt/theory_arith_core.h +++ b/src/smt/theory_arith_core.h @@ -3269,7 +3269,7 @@ namespace smt { template bool theory_arith::get_lower(enode * n, expr_ref & r) { theory_var v = n->get_th_var(get_id()); - bound * b = (v == null_theory_var) ? 0 : lower(v); + bound* b = (v == null_theory_var) ? 0 : lower(v); return b && to_expr(b->get_value(), is_int(v), r); } From ed0b2be618121b407bc3384e21a446538408c4c8 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Tue, 2 May 2017 14:10:07 -0700 Subject: [PATCH 537/562] fix bug in tracking levels of variables: levels are not cleared, only truth assignment Signed-off-by: Nikolaj Bjorner --- src/opt/wmax.cpp | 4 +-- src/sat/sat_solver.cpp | 77 ++++++++++++++++++++++-------------------- 2 files changed, 41 insertions(+), 40 deletions(-) diff --git a/src/opt/wmax.cpp b/src/opt/wmax.cpp index 9708bdc8f..58d319a63 100644 --- a/src/opt/wmax.cpp +++ b/src/opt/wmax.cpp @@ -61,13 +61,12 @@ namespace opt { return is_sat; } m_upper = m_lower; - bool was_sat = false; expr_ref_vector asms(m); vector cores; obj_map::iterator it = soft.begin(), end = soft.end(); for (; it != end; ++it) { - expr* c = assert_weighted(wth(), it->m_key, it->m_value); + assert_weighted(wth(), it->m_key, it->m_value); if (!is_true(it->m_key)) { m_upper += it->m_value; } @@ -97,7 +96,6 @@ namespace opt { expr_ref fml = wth().mk_block(); //DEBUG_CODE(verify_cores(cores);); s().assert_expr(fml); - was_sat = true; } else { //DEBUG_CODE(verify_cores(cores);); diff --git a/src/sat/sat_solver.cpp b/src/sat/sat_solver.cpp index dc5aa2964..b5bd46168 100644 --- a/src/sat/sat_solver.cpp +++ b/src/sat/sat_solver.cpp @@ -3141,42 +3141,42 @@ namespace sat { // // ----------------------- -static void prune_unfixed(sat::literal_vector& lambda, sat::model const& m) { - for (unsigned i = 0; i < lambda.size(); ++i) { - if ((m[lambda[i].var()] == l_false) != lambda[i].sign()) { - lambda[i] = lambda.back(); - lambda.pop_back(); + static void prune_unfixed(sat::literal_vector& lambda, sat::model const& m) { + for (unsigned i = 0; i < lambda.size(); ++i) { + if ((m[lambda[i].var()] == l_false) != lambda[i].sign()) { + lambda[i] = lambda.back(); + lambda.pop_back(); + --i; + } + } + } + + // Algorithm 7: Corebased Algorithm with Chunking + + static void back_remove(sat::literal_vector& lits, sat::literal l) { + for (unsigned i = lits.size(); i > 0; ) { --i; + if (lits[i] == l) { + lits[i] = lits.back(); + lits.pop_back(); + return; + } } + UNREACHABLE(); } -} - -// Algorithm 7: Corebased Algorithm with Chunking - -static void back_remove(sat::literal_vector& lits, sat::literal l) { - for (unsigned i = lits.size(); i > 0; ) { - --i; - if (lits[i] == l) { - lits[i] = lits.back(); - lits.pop_back(); - return; - } - } - std::cout << "UNREACHABLE\n"; -} static void brute_force_consequences(sat::solver& s, sat::literal_vector const& asms, sat::literal_vector const& gamma, vector& conseq) { - for (unsigned i = 0; i < gamma.size(); ++i) { - sat::literal nlit = ~gamma[i]; - sat::literal_vector asms1(asms); - asms1.push_back(nlit); - lbool r = s.check(asms1.size(), asms1.c_ptr()); - if (r == l_false) { - conseq.push_back(s.get_core()); + for (unsigned i = 0; i < gamma.size(); ++i) { + sat::literal nlit = ~gamma[i]; + sat::literal_vector asms1(asms); + asms1.push_back(nlit); + lbool r = s.check(asms1.size(), asms1.c_ptr()); + if (r == l_false) { + conseq.push_back(s.get_core()); + } } } -} - + static lbool core_chunking(sat::solver& s, model const& m, sat::bool_var_vector const& vars, sat::literal_vector const& asms, vector& conseq, unsigned K) { sat::literal_vector lambda; for (unsigned i = 0; i < vars.size(); i++) { @@ -3259,9 +3259,12 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { } } - // is_sat = core_chunking(*this, mdl, vars, asms, conseq, 100); - - is_sat = get_consequences(asms, lits, conseq); + if (false && asms.empty()) { + is_sat = core_chunking(*this, mdl, vars, asms, conseq, 100); + } + else { + is_sat = get_consequences(asms, lits, conseq); + } set_model(mdl); return is_sat; } @@ -3390,8 +3393,7 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { literal lit = *it; if (value(lit) != l_undef) { ++num_fixed; - if (lvl(lit) <= 1) { - SASSERT(value(lit) == l_true); + if (lvl(lit) <= 1 && value(lit) == l_true) { extract_fixed_consequences(lit, assumptions, unfixed_vars, conseq); } continue; @@ -3498,8 +3500,9 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { literal_set::iterator it = unfixed_lits.begin(), end = unfixed_lits.end(); for (; it != end; ++it) { literal lit = *it; - if (lvl(lit) <= 1) { - SASSERT(value(lit) == l_true); + TRACE("sat", tout << "extract: " << lit << " " << value(lit) << " " << lvl(lit) << "\n";); + + if (lvl(lit) <= 1 && value(lit) == l_true) { extract_fixed_consequences(lit, assumptions, unfixed_vars, conseq); } } @@ -3508,8 +3511,8 @@ static void back_remove(sat::literal_vector& lits, sat::literal l) { bool solver::check_domain(literal lit, literal lit2) { if (!m_antecedents.contains(lit2.var())) { SASSERT(value(lit2) == l_true); + SASSERT(m_todo_antecedents.empty() || m_todo_antecedents.back() != lit2); m_todo_antecedents.push_back(lit2); - TRACE("sat", tout << "todo: " << lit2 << " " << value(lit2) << "\n";); return false; } else { From 92755b0185e32a31d0ed1e2d700b9971f1523087 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Tue, 2 May 2017 17:16:35 -0400 Subject: [PATCH 538/562] smt_setup framework, all hooks to theory_str are redirected to theory_seq --- src/smt/params/smt_params.cpp | 1 + src/smt/params/smt_params.h | 10 +++++++- src/smt/params/smt_params_helper.pyg | 1 + src/smt/smt_setup.cpp | 38 ++++++++++++++++++++++++---- src/smt/smt_setup.h | 4 ++- src/solver/smt_logics.cpp | 6 ++++- src/solver/smt_logics.h | 1 + 7 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/smt/params/smt_params.cpp b/src/smt/params/smt_params.cpp index 4b7920596..3d0b59c88 100644 --- a/src/smt/params/smt_params.cpp +++ b/src/smt/params/smt_params.cpp @@ -40,6 +40,7 @@ void smt_params::updt_local_params(params_ref const & _p) { m_max_conflicts = p.max_conflicts(); m_core_validate = p.core_validate(); m_logic = _p.get_sym("logic", m_logic); + m_string_solver = p.string_solver(); model_params mp(_p); m_model_compact = mp.compact(); if (_p.get_bool("arith.greatest_error_pivot", false)) diff --git a/src/smt/params/smt_params.h b/src/smt/params/smt_params.h index c03eaeaef..b44e782fd 100644 --- a/src/smt/params/smt_params.h +++ b/src/smt/params/smt_params.h @@ -213,6 +213,13 @@ struct smt_params : public preprocessor_params, bool m_dump_goal_as_smt; bool m_auto_config; + // ----------------------------------- + // + // Solver selection + // + // ----------------------------------- + symbol m_string_solver; + smt_params(params_ref const & p = params_ref()): m_display_proof(false), m_display_dot_proof(false), @@ -281,7 +288,8 @@ struct smt_params : public preprocessor_params, m_at_labels_cex(false), m_check_at_labels(false), m_dump_goal_as_smt(false), - m_auto_config(true) { + m_auto_config(true), + m_string_solver(symbol("seq")){ updt_local_params(p); } diff --git a/src/smt/params/smt_params_helper.pyg b/src/smt/params/smt_params_helper.pyg index faa48400d..a06a37f2a 100644 --- a/src/smt/params/smt_params_helper.pyg +++ b/src/smt/params/smt_params_helper.pyg @@ -62,6 +62,7 @@ def_module_params(module_name='smt', ('dack.gc_inv_decay', DOUBLE, 0.8, 'Dynamic ackermannization garbage collection decay'), ('dack.threshold', UINT, 10, ' number of times the congruence rule must be used before Leibniz\'s axiom is expanded'), ('theory_case_split', BOOL, False, 'Allow the context to use heuristics involving theory case splits, which are a set of literals of which exactly one can be assigned True. If this option is false, the context will generate extra axioms to enforce this instead.'), + ('string_solver', SYMBOL, 'seq', 'solver for string/sequence theories. options are: \'z3str3\' (specialized string solver), \'seq\' (sequence solver), \'auto\' (use static features to choose best solver)'), ('core.validate', BOOL, False, 'validate unsat core produced by SMT context'), ('core.minimize', BOOL, False, 'minimize unsat core produced by SMT context'), ('core.extend_patterns', BOOL, False, 'extend unsat core with literals that trigger (potential) quantifier instances'), diff --git a/src/smt/smt_setup.cpp b/src/smt/smt_setup.cpp index ee92c4f61..84e3dee32 100644 --- a/src/smt/smt_setup.cpp +++ b/src/smt/smt_setup.cpp @@ -120,6 +120,8 @@ namespace smt { setup_QF_FP(); else if (m_logic == "QF_FPBV" || m_logic == "QF_BVFP") setup_QF_FPBV(); + else if (m_logic == "QF_S") + setup_QF_S(); else setup_unknown(); } @@ -161,6 +163,8 @@ namespace smt { setup_QF_BVRE(); else if (m_logic == "QF_AUFLIA") setup_QF_AUFLIA(st); + else if (m_logic == "QF_S") + setup_QF_S(); else if (m_logic == "AUFLIA") setup_AUFLIA(st); else if (m_logic == "AUFLIRA") @@ -201,7 +205,7 @@ namespace smt { void setup::setup_QF_BVRE() { setup_QF_BV(); setup_QF_LIA(); - setup_seq(); + m_context.register_plugin(alloc(theory_seq, m_manager)); } void setup::setup_QF_UF(static_features const & st) { @@ -700,6 +704,10 @@ namespace smt { m_context.register_plugin(alloc(smt::theory_fpa, m_manager)); } + void setup::setup_QF_S() { + m_context.register_plugin(alloc(smt::theory_seq, m_manager)); + } + bool is_arith(static_features const & st) { return st.m_num_arith_ineqs > 0 || st.m_num_arith_terms > 0 || st.m_num_arith_eqs > 0; } @@ -814,8 +822,21 @@ namespace smt { m_context.register_plugin(mk_theory_dl(m_manager)); } - void setup::setup_seq() { - m_context.register_plugin(alloc(theory_seq, m_manager)); + void setup::setup_seq(static_features const & st) { + // check params for what to do here when it's ambiguous + if (m_params.m_string_solver == "z3str3") { + setup_str(); + } else if (m_params.m_string_solver == "seq") { + m_context.register_plugin(alloc(smt::theory_seq, m_manager)); + } else if (m_params.m_string_solver == "auto") { + if (st.m_has_seq_non_str) { + m_context.register_plugin(alloc(smt::theory_seq, m_manager)); + } else { + setup_str(); + } + } else { + throw default_exception("invalid parameter for smt.string_solver, valid options are 'z3str3', 'seq', 'auto'"); + } } void setup::setup_card() { @@ -827,13 +848,20 @@ namespace smt { m_context.register_plugin(alloc(theory_fpa, m_manager)); } + void setup::setup_str() { + m_context.register_plugin(alloc(smt::theory_seq, m_manager)); + } + void setup::setup_unknown() { + static_features st(m_manager); + st.collect(m_context.get_num_asserted_formulas(), m_context.get_asserted_formulas()); + setup_arith(); setup_arrays(); setup_bv(); setup_datatypes(); setup_dl(); - setup_seq(); + setup_seq(st); setup_card(); setup_fpa(); } @@ -848,7 +876,7 @@ namespace smt { setup_datatypes(); setup_bv(); setup_dl(); - setup_seq(); + setup_seq(st); setup_card(); setup_fpa(); return; diff --git a/src/smt/smt_setup.h b/src/smt/smt_setup.h index 68cd5703c..d30c896e5 100644 --- a/src/smt/smt_setup.h +++ b/src/smt/smt_setup.h @@ -77,6 +77,7 @@ namespace smt { void setup_QF_AUFLIA(static_features const & st); void setup_QF_FP(); void setup_QF_FPBV(); + void setup_QF_S(); void setup_LRA(); void setup_AUFLIA(bool simple_array = true); void setup_AUFLIA(static_features const & st); @@ -93,11 +94,12 @@ namespace smt { void setup_bv(); void setup_arith(); void setup_dl(); - void setup_seq(); + void setup_seq(static_features const & st); void setup_card(); void setup_i_arith(); void setup_mi_arith(); void setup_fpa(); + void setup_str(); public: setup(context & c, smt_params & params); diff --git a/src/solver/smt_logics.cpp b/src/solver/smt_logics.cpp index 210a09f96..c4ead74df 100644 --- a/src/solver/smt_logics.cpp +++ b/src/solver/smt_logics.cpp @@ -24,7 +24,7 @@ Revision History: bool smt_logics::supported_logic(symbol const & s) { return logic_has_uf(s) || logic_is_all(s) || logic_has_fd(s) || logic_has_arith(s) || logic_has_bv(s) || - logic_has_array(s) || logic_has_seq(s) || + logic_has_array(s) || logic_has_seq(s) || logic_has_str(s) || logic_has_horn(s) || logic_has_fpa(s); } @@ -132,6 +132,10 @@ bool smt_logics::logic_has_seq(symbol const & s) { return s == "QF_BVRE" || s == "QF_S" || s == "ALL"; } +bool smt_logics::logic_has_str(symbol const & s) { + return s == "QF_S" || s == "ALL"; +} + bool smt_logics::logic_has_fpa(symbol const & s) { return s == "QF_FP" || s == "QF_FPBV" || s == "QF_BVFP" || s == "ALL"; } diff --git a/src/solver/smt_logics.h b/src/solver/smt_logics.h index 72c3b8764..702431cdd 100644 --- a/src/solver/smt_logics.h +++ b/src/solver/smt_logics.h @@ -30,6 +30,7 @@ public: static bool logic_has_bv(symbol const & s); static bool logic_has_array(symbol const & s); static bool logic_has_seq(symbol const & s); + static bool logic_has_str(symbol const & s); static bool logic_has_fpa(symbol const & s); static bool logic_has_horn(symbol const& s); static bool logic_has_pb(symbol const& s); From 21cda27f5e175cecdc8ed0bd25bb1bfc0d8c1d17 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Tue, 2 May 2017 15:57:31 -0700 Subject: [PATCH 539/562] fix quadratic behavior of extract_assumptions Signed-off-by: Nikolaj Bjorner --- src/sat/sat_solver.cpp | 19 ++++++++++++++----- src/test/cnf_backbones.cpp | 3 ++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/sat/sat_solver.cpp b/src/sat/sat_solver.cpp index b5bd46168..7f2b75830 100644 --- a/src/sat/sat_solver.cpp +++ b/src/sat/sat_solver.cpp @@ -3523,6 +3523,7 @@ namespace sat { bool solver::extract_assumptions(literal lit, index_set& s) { justification js = m_justification[lit.var()]; TRACE("sat", tout << lit << " " << js << "\n";); + bool all_found = true; switch (js.get_kind()) { case justification::NONE: break; @@ -3540,8 +3541,12 @@ namespace sat { clause & c = *(m_cls_allocator.get_clause(js.get_clause_offset())); for (unsigned i = 0; i < c.size(); ++i) { if (c[i] != lit) { - if (!check_domain(lit, ~c[i])) return false; - s |= m_antecedents.find(c[i].var()); + if (check_domain(lit, ~c[i]) && all_found) { + s |= m_antecedents.find(c[i].var()); + } + else { + all_found = false; + } } } break; @@ -3551,8 +3556,12 @@ namespace sat { literal_vector::iterator it = m_ext_antecedents.begin(); literal_vector::iterator end = m_ext_antecedents.end(); for (; it != end; ++it) { - if (!check_domain(lit, *it)) return false; - s |= m_antecedents.find(it->var()); + if (check_domain(lit, *it) && all_found) { + s |= m_antecedents.find(it->var()); + } + else { + all_found = false; + } } break; } @@ -3561,7 +3570,7 @@ namespace sat { break; } TRACE("sat", display_index_set(tout << lit << ": " , s) << "\n";); - return true; + return all_found; } std::ostream& solver::display_index_set(std::ostream& out, index_set const& s) const { diff --git a/src/test/cnf_backbones.cpp b/src/test/cnf_backbones.cpp index b95977a46..eab91e557 100644 --- a/src/test/cnf_backbones.cpp +++ b/src/test/cnf_backbones.cpp @@ -246,13 +246,14 @@ static void cnf_backbones(bool use_chunk, char const* file_name) { vector conseq; sat::bool_var_vector vars; sat::literal_vector assumptions; + unsigned num_vars = solver.num_vars(); if (p.get_bool("dimacs.core", false)) { g_solver = &solver2; vector tracking_clauses; track_clauses(solver, solver2, assumptions, tracking_clauses); } - for (unsigned i = 1; i < g_solver->num_vars(); ++i) { + for (unsigned i = 1; i < num_vars; ++i) { vars.push_back(i); g_solver->set_external(i); } From 561a4331a83438a3483504e272722bd1f291ba32 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Tue, 2 May 2017 16:36:05 -0700 Subject: [PATCH 540/562] add back use of all variables for tracking Signed-off-by: Nikolaj Bjorner --- src/test/cnf_backbones.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/cnf_backbones.cpp b/src/test/cnf_backbones.cpp index eab91e557..07edabece 100644 --- a/src/test/cnf_backbones.cpp +++ b/src/test/cnf_backbones.cpp @@ -257,6 +257,7 @@ static void cnf_backbones(bool use_chunk, char const* file_name) { vars.push_back(i); g_solver->set_external(i); } + num_vars = g_solver->num_vars(); lbool r; if (use_chunk) { r = core_chunking(*g_solver, vars, assumptions, conseq, 100); From eeb79e1c3c9343f3dcf7a5ec09c2e503abdeec6a Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Tue, 2 May 2017 19:30:54 -0700 Subject: [PATCH 541/562] update to retain original behavior Signed-off-by: Nikolaj Bjorner --- src/opt/opt_context.cpp | 2 +- src/test/cnf_backbones.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/opt/opt_context.cpp b/src/opt/opt_context.cpp index 1310727aa..d486dfd11 100644 --- a/src/opt/opt_context.cpp +++ b/src/opt/opt_context.cpp @@ -406,7 +406,7 @@ namespace opt { if (r == l_true && !get_lower_as_num(i).is_finite()) { return r; } - if (r == l_true && i + 1 < m_objectives.size()) { + if (r == l_true && i + 1 < m_objectives.size() && get_lower_as_num(i).is_finite()) { update_lower(); } } diff --git a/src/test/cnf_backbones.cpp b/src/test/cnf_backbones.cpp index 07edabece..14fc594b6 100644 --- a/src/test/cnf_backbones.cpp +++ b/src/test/cnf_backbones.cpp @@ -252,12 +252,12 @@ static void cnf_backbones(bool use_chunk, char const* file_name) { vector tracking_clauses; track_clauses(solver, solver2, assumptions, tracking_clauses); } - + // remove this line to limit variables to exclude assumptions + num_vars = g_solver->num_vars(); for (unsigned i = 1; i < num_vars; ++i) { vars.push_back(i); g_solver->set_external(i); } - num_vars = g_solver->num_vars(); lbool r; if (use_chunk) { r = core_chunking(*g_solver, vars, assumptions, conseq, 100); From cc7a176c8936162be94235a6277c93bf7d96fd2f Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Tue, 2 May 2017 19:32:03 -0700 Subject: [PATCH 542/562] update to retain original behavior Signed-off-by: Nikolaj Bjorner --- src/opt/opt_context.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/opt/opt_context.cpp b/src/opt/opt_context.cpp index d486dfd11..1310727aa 100644 --- a/src/opt/opt_context.cpp +++ b/src/opt/opt_context.cpp @@ -406,7 +406,7 @@ namespace opt { if (r == l_true && !get_lower_as_num(i).is_finite()) { return r; } - if (r == l_true && i + 1 < m_objectives.size() && get_lower_as_num(i).is_finite()) { + if (r == l_true && i + 1 < m_objectives.size()) { update_lower(); } } From 1177be63913b2813f667d7202f09fe31f4a9688e Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Tue, 2 May 2017 20:52:39 -0700 Subject: [PATCH 543/562] add common utility to set up seq Signed-off-by: Nikolaj Bjorner --- src/smt/smt_setup.cpp | 30 +++++++++++++++++++----------- src/smt/smt_setup.h | 3 ++- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/smt/smt_setup.cpp b/src/smt/smt_setup.cpp index 84e3dee32..820159d18 100644 --- a/src/smt/smt_setup.cpp +++ b/src/smt/smt_setup.cpp @@ -205,7 +205,7 @@ namespace smt { void setup::setup_QF_BVRE() { setup_QF_BV(); setup_QF_LIA(); - m_context.register_plugin(alloc(theory_seq, m_manager)); + setup_seq(); } void setup::setup_QF_UF(static_features const & st) { @@ -705,7 +705,7 @@ namespace smt { } void setup::setup_QF_S() { - m_context.register_plugin(alloc(smt::theory_seq, m_manager)); + setup_seq(); } bool is_arith(static_features const & st) { @@ -822,19 +822,23 @@ namespace smt { m_context.register_plugin(mk_theory_dl(m_manager)); } - void setup::setup_seq(static_features const & st) { + void setup::setup_seq_str(static_features const & st) { // check params for what to do here when it's ambiguous if (m_params.m_string_solver == "z3str3") { setup_str(); - } else if (m_params.m_string_solver == "seq") { - m_context.register_plugin(alloc(smt::theory_seq, m_manager)); - } else if (m_params.m_string_solver == "auto") { + } + else if (m_params.m_string_solver == "seq") { + setup_seq(); + } + else if (m_params.m_string_solver == "auto") { if (st.m_has_seq_non_str) { - m_context.register_plugin(alloc(smt::theory_seq, m_manager)); - } else { + setup_seq(); + } + else { setup_str(); } - } else { + } + else { throw default_exception("invalid parameter for smt.string_solver, valid options are 'z3str3', 'seq', 'auto'"); } } @@ -852,6 +856,10 @@ namespace smt { m_context.register_plugin(alloc(smt::theory_seq, m_manager)); } + void setup::setup_seq() { + m_context.register_plugin(alloc(smt::theory_seq, m_manager)); + } + void setup::setup_unknown() { static_features st(m_manager); st.collect(m_context.get_num_asserted_formulas(), m_context.get_asserted_formulas()); @@ -861,7 +869,7 @@ namespace smt { setup_bv(); setup_datatypes(); setup_dl(); - setup_seq(st); + setup_seq_str(st); setup_card(); setup_fpa(); } @@ -876,7 +884,7 @@ namespace smt { setup_datatypes(); setup_bv(); setup_dl(); - setup_seq(st); + setup_seq_str(st); setup_card(); setup_fpa(); return; diff --git a/src/smt/smt_setup.h b/src/smt/smt_setup.h index d30c896e5..80d5d7d1b 100644 --- a/src/smt/smt_setup.h +++ b/src/smt/smt_setup.h @@ -94,7 +94,8 @@ namespace smt { void setup_bv(); void setup_arith(); void setup_dl(); - void setup_seq(static_features const & st); + void setup_seq_str(static_features const & st); + void setup_seq(); void setup_card(); void setup_i_arith(); void setup_mi_arith(); From ede6d7bb2b95e895afc9b6f596941776cd4b1906 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 3 May 2017 14:55:22 -0400 Subject: [PATCH 544/562] add iterator accessors to obj_pair_set --- src/util/obj_pair_set.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/util/obj_pair_set.h b/src/util/obj_pair_set.h index 29139a51d..c4212977c 100644 --- a/src/util/obj_pair_set.h +++ b/src/util/obj_pair_set.h @@ -46,6 +46,11 @@ public: bool contains(obj_pair const & p) const { return m_set.contains(p); } void reset() { m_set.reset(); } bool empty() const { return m_set.empty(); } + + typedef typename chashtable::iterator iterator; + + iterator begin() { return m_set.begin(); } + iterator end() { return m_set.end(); } }; #endif From ab4fbe40b67b630203ed688269e98b6427b58b89 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 3 May 2017 17:45:56 -0400 Subject: [PATCH 545/562] cleanup --- .gitignore | 1 - src/ast/ast_smt2_pp.h | 2 +- src/ast/ast_smt_pp.cpp | 1 - src/ast/rewriter/rewriter.txt | 1 - src/parsers/smt2/smt2parser.cpp | 3 +-- src/smt/smt_context.cpp | 7 +------ src/smt/smt_theory.h | 16 ++++++++-------- 7 files changed, 11 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 7cc289168..cc1c2a754 100644 --- a/.gitignore +++ b/.gitignore @@ -86,4 +86,3 @@ src/*/*/CMakeLists.txt src/*/*/*/CMakeLists.txt src/api/dotnet/cmake_install_gac.cmake.in src/api/dotnet/cmake_uninstall_gac.cmake.in - diff --git a/src/ast/ast_smt2_pp.h b/src/ast/ast_smt2_pp.h index 244594461..f2d177041 100644 --- a/src/ast/ast_smt2_pp.h +++ b/src/ast/ast_smt2_pp.h @@ -55,8 +55,8 @@ public: virtual format_ns::format * pp_bv_literal(app * t, bool use_bv_lits, bool bv_neg); virtual format_ns::format * pp_arith_literal(app * t, bool decimal, unsigned prec); virtual format_ns::format * pp_float_literal(app * t, bool use_bv_lits, bool use_float_real_lits); - virtual format_ns::format * pp_string_literal(app * t); virtual format_ns::format * pp_datalog_literal(app * t); + virtual format_ns::format * pp_string_literal(app * t); virtual format_ns::format * pp_sort(sort * s); virtual format_ns::format * pp_fdecl_ref(func_decl * f); format_ns::format * pp_fdecl_name(symbol const & fname, unsigned & len) const; diff --git a/src/ast/ast_smt_pp.cpp b/src/ast/ast_smt_pp.cpp index f41350dc5..706f65ac4 100644 --- a/src/ast/ast_smt_pp.cpp +++ b/src/ast/ast_smt_pp.cpp @@ -406,7 +406,6 @@ class smt_printer { void visit_app(app* n) { rational val; - const char *str; bool is_int, pos; buffer names; unsigned bv_size; diff --git a/src/ast/rewriter/rewriter.txt b/src/ast/rewriter/rewriter.txt index a7a9e5eff..9eb016af2 100644 --- a/src/ast/rewriter/rewriter.txt +++ b/src/ast/rewriter/rewriter.txt @@ -8,7 +8,6 @@ The following classes implement theory specific rewriting rules: - datatype_rewriter - fpa_rewriter - seq_rewriter - - str_rewriter Each of them provide the method br_status mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) diff --git a/src/parsers/smt2/smt2parser.cpp b/src/parsers/smt2/smt2parser.cpp index 491aca8ba..1486f6e6c 100644 --- a/src/parsers/smt2/smt2parser.cpp +++ b/src/parsers/smt2/smt2parser.cpp @@ -66,8 +66,7 @@ namespace smt2 { scoped_ptr m_bv_util; scoped_ptr m_arith_util; - scoped_ptr m_seq_util; - + scoped_ptr m_seq_util; scoped_ptr m_pattern_validator; scoped_ptr m_var_shifter; diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 68a9f980d..535ae3b1e 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -1714,12 +1714,6 @@ namespace smt { for (unsigned i = 0; i < m_th_eq_propagation_queue.size() && !inconsistent(); i++) { new_th_eq curr = m_th_eq_propagation_queue[i]; theory * th = get_theory(curr.m_th_id); - TRACE("t_str_eq_bug", tout - << "th->name = " << th->get_name() << std::endl - << "m_th_id = " << curr.m_th_id << std::endl - << "m_lhs = " << curr.m_lhs << std::endl - << "m_rhs = " << curr.m_rhs << std::endl - << std::endl;); SASSERT(th); th->new_eq_eh(curr.m_lhs, curr.m_rhs); #ifdef Z3DEBUG @@ -3042,6 +3036,7 @@ namespace smt { // not counting any literals that get assigned by this method // this relies on bcp() to give us its old m_qhead and therefore // bcp() should always be called before this method + unsigned assigned_literal_end = m_assigned_literals.size(); for (; qhead < assigned_literal_end; ++qhead) { literal l = m_assigned_literals[qhead]; diff --git a/src/smt/smt_theory.h b/src/smt/smt_theory.h index 7dd2819e4..67091c601 100644 --- a/src/smt/smt_theory.h +++ b/src/smt/smt_theory.h @@ -185,6 +185,14 @@ namespace smt { virtual void add_theory_assumptions(expr_ref_vector & assumptions) { } + /** + \brief This method is called from the smt_context when an unsat core is generated. + The theory may change the answer to UNKNOWN by returning l_undef from this method. + */ + virtual lbool validate_unsat_core(expr_ref_vector & unsat_core) { + return l_false; + } + /** \brief This method is invoked before the search starts. */ @@ -200,14 +208,6 @@ namespace smt { return FC_DONE; } - /** - \brief This method is called from the smt_context when an unsat core is generated. - The theory may change the answer to UNKNOWN by returning l_undef from this method. - */ - virtual lbool validate_unsat_core(expr_ref_vector & unsat_core) { - return l_false; - } - /** \brief Parametric theories (e.g. Arrays) should implement this method. See example in context::is_shared From c2b5e8cfdafec22eaf7614bfc688566b1c09b42e Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Wed, 3 May 2017 17:46:06 -0400 Subject: [PATCH 546/562] fix overlap detection internalization --- src/smt/theory_str.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index be268ec5c..a26cb2ee2 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -7305,7 +7305,7 @@ lbool theory_str::validate_unsat_core(expr_ref_vector & unsat_core) { bool assumptionFound = false; app * target_term = to_app(get_manager().mk_not(m_theoryStrOverlapAssumption_term)); - internalize_term(target_term); + get_context().internalize(target_term, false); for (unsigned i = 0; i < unsat_core.size(); ++i) { app * core_term = to_app(unsat_core.get(i)); // not sure if this is the correct way to compare terms in this context From 6261a5c27bac07275178889052ae983e828a0811 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Thu, 4 May 2017 15:28:20 +0100 Subject: [PATCH 547/562] Fix bug in `mk_api_doc.py` where the Z3 python package path would be checked when building the Z3 python package documentation was disabled. --- doc/mk_api_doc.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/doc/mk_api_doc.py b/doc/mk_api_doc.py index 014a152b4..234dd670c 100644 --- a/doc/mk_api_doc.py +++ b/doc/mk_api_doc.py @@ -103,15 +103,17 @@ def parse_options(): TEMP_DIR = pargs.temp_dir OUTPUT_DIRECTORY = pargs.output_dir Z3PY_PACKAGE_PATH = pargs.z3py_package_path - if not os.path.exists(Z3PY_PACKAGE_PATH): - raise Exception('"{}" does not exist'.format(Z3PY_PACKAGE_PATH)) - if not os.path.basename(Z3PY_PACKAGE_PATH) == 'z3': - raise Exception('"{}" does not end with "z3"'.format(Z3PY_PACKAGE_PATH)) Z3PY_ENABLED = not pargs.no_z3py DOTNET_ENABLED = not pargs.no_dotnet JAVA_ENABLED = not pargs.no_java DOTNET_API_SEARCH_PATHS = pargs.dotnet_search_paths JAVA_API_SEARCH_PATHS = pargs.java_search_paths + + if Z3PY_ENABLED: + if not os.path.exists(Z3PY_PACKAGE_PATH): + raise Exception('"{}" does not exist'.format(Z3PY_PACKAGE_PATH)) + if not os.path.basename(Z3PY_PACKAGE_PATH) == 'z3': + raise Exception('"{}" does not end with "z3"'.format(Z3PY_PACKAGE_PATH)) return def mk_dir(d): From 1db07f1189d6560316537ec4822d41e84f0a7105 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Thu, 4 May 2017 15:29:47 +0100 Subject: [PATCH 548/562] [CMake] Remove `BYPRODUCTS` declaration for `api_docs` target. This breaks the `clean` rule when using Ninja as the CMake generator. Unfortunately this means `clean` doesn't try to remove the generated documentation anymore when using Ninja. --- contrib/cmake/doc/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/contrib/cmake/doc/CMakeLists.txt b/contrib/cmake/doc/CMakeLists.txt index 86e208ab1..2f8ee0dc5 100644 --- a/contrib/cmake/doc/CMakeLists.txt +++ b/contrib/cmake/doc/CMakeLists.txt @@ -67,7 +67,6 @@ add_custom_target(api_docs ${ALWAYS_BUILD_DOCS_ARG} ${JAVA_API_OPTIONS} DEPENDS ${DOC_EXTRA_DEPENDS} - BYPRODUCTS "${DOC_DEST_DIR}" COMMENT "Generating documentation" ${ADD_CUSTOM_TARGET_USES_TERMINAL_ARG} ) From 0ebce66c57e124ebcb4c3bb3d34fe9d35779341b Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Fri, 5 May 2017 14:22:40 +0100 Subject: [PATCH 549/562] Fixed bug with .NET keyfile path containing spaces. Fixes #1003. --- scripts/mk_util.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/mk_util.py b/scripts/mk_util.py index f05250ae7..b0ac92a8d 100644 --- a/scripts/mk_util.py +++ b/scripts/mk_util.py @@ -1633,6 +1633,8 @@ class DotNetDLLComponent(Component): if not self.key_file is None: print("%s.dll will be signed using key '%s'." % (self.dll_name, self.key_file)) + if (self.key_file.find(' ') != -1): + self.key_file = '"' + self.key_file + '"' cscCmdLine.append('/keyfile:{}'.format(self.key_file)) cscCmdLine.extend( ['/unsafe+', From 79dcf03a42a79f4f6fe09b60ff809be39dce38d8 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Fri, 5 May 2017 15:01:10 +0100 Subject: [PATCH 550/562] Enabled C++11 in GCC and Clang --- scripts/mk_util.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/mk_util.py b/scripts/mk_util.py index b0ac92a8d..d1bbd6ca4 100644 --- a/scripts/mk_util.py +++ b/scripts/mk_util.py @@ -2421,6 +2421,7 @@ def mk_config(): FOCI2 = False if GIT_HASH: CPPFLAGS = '%s -DZ3GITHASH=%s' % (CPPFLAGS, GIT_HASH) + CXXFLAGS = '%s -std=c++11' % CXXFLAGS CXXFLAGS = '%s -fvisibility=hidden -c' % CXXFLAGS FPMATH = test_fpmath(CXX) CXXFLAGS = '%s %s' % (CXXFLAGS, FPMATH_FLAGS) @@ -2445,8 +2446,8 @@ def mk_config(): CXXFLAGS = '%s -Wno-unknown-pragmas -Wno-overloaded-virtual -Wno-unused-value' % CXXFLAGS sysname, _, _, _, machine = os.uname() if sysname == 'Darwin': - SO_EXT = '.dylib' - SLIBFLAGS = '-dynamiclib' + SO_EXT = '.dylib' + SLIBFLAGS = '-dynamiclib' elif sysname == 'Linux': CXXFLAGS = '%s -fno-strict-aliasing -D_LINUX_' % CXXFLAGS OS_DEFINES = '-D_LINUX_' From 7e1fae418a766532f99cea06a1c6021268661864 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Fri, 5 May 2017 10:59:47 -0400 Subject: [PATCH 551/562] fix #1005, disable expansion of regular expression range to union as it degrades performance significantly Signed-off-by: Nikolaj Bjorner --- src/ast/rewriter/seq_rewriter.cpp | 1 + src/smt/theory_arith.h | 2 +- src/smt/theory_arith_aux.h | 11 +++++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/ast/rewriter/seq_rewriter.cpp b/src/ast/rewriter/seq_rewriter.cpp index 85d2ba749..7aa9329d4 100644 --- a/src/ast/rewriter/seq_rewriter.cpp +++ b/src/ast/rewriter/seq_rewriter.cpp @@ -1434,6 +1434,7 @@ br_status seq_rewriter::mk_re_star(expr* a, expr_ref& result) { * (re.range c_1 c_n) = (re.union (str.to.re c1) (str.to.re c2) ... (str.to.re cn)) */ br_status seq_rewriter::mk_re_range(expr* lo, expr* hi, expr_ref& result) { + return BR_FAILED; TRACE("seq", tout << "rewrite re.range [" << mk_pp(lo, m()) << " " << mk_pp(hi, m()) << "]\n";); zstring str_lo, str_hi; if (m_util.str.is_string(lo, str_lo) && m_util.str.is_string(hi, str_hi)) { diff --git a/src/smt/theory_arith.h b/src/smt/theory_arith.h index 439adbdff..cdc1a3933 100644 --- a/src/smt/theory_arith.h +++ b/src/smt/theory_arith.h @@ -505,7 +505,7 @@ namespace smt { struct var_value_eq { theory_arith & m_th; var_value_eq(theory_arith & th):m_th(th) {} - bool operator()(theory_var v1, theory_var v2) const { return m_th.get_value(v1) == m_th.get_value(v2) && m_th.is_int(v1) == m_th.is_int(v2); } + bool operator()(theory_var v1, theory_var v2) const { return m_th.get_value(v1) == m_th.get_value(v2) && m_th.is_int_src(v1) == m_th.is_int_src(v2); } }; typedef int_hashtable var_value_table; diff --git a/src/smt/theory_arith_aux.h b/src/smt/theory_arith_aux.h index de357c8d3..54b617152 100644 --- a/src/smt/theory_arith_aux.h +++ b/src/smt/theory_arith_aux.h @@ -2201,16 +2201,19 @@ namespace smt { int num = get_num_vars(); for (theory_var v = 0; v < num; v++) { enode * n = get_enode(v); - TRACE("func_interp_bug", tout << "#" << n->get_owner_id() << " -> " << m_value[v] << "\n";); - if (!is_relevant_and_shared(n)) + TRACE("func_interp_bug", tout << mk_pp(n->get_owner(), get_manager()) << " -> " << m_value[v] << " root #" << n->get_root()->get_owner_id() << " " << is_relevant_and_shared(n) << "\n";); + if (!is_relevant_and_shared(n)) { continue; + } theory_var other = null_theory_var; other = m_var_value_table.insert_if_not_there(v); - if (other == v) + if (other == v) { continue; + } enode * n2 = get_enode(other); - if (n->get_root() == n2->get_root()) + if (n->get_root() == n2->get_root()) { continue; + } TRACE("func_interp_bug", tout << "adding to assume_eq queue #" << n->get_owner_id() << " #" << n2->get_owner_id() << "\n";); m_assume_eq_candidates.push_back(std::make_pair(other, v)); result = true; From 75ba4d5a4d537f60f935d47a0f0edf84177de528 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 5 May 2017 14:54:36 -0400 Subject: [PATCH 552/562] remove unneeded include --- src/smt/smt_context.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 535ae3b1e..37a6d32b7 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -37,7 +37,6 @@ Revision History: #include"model_pp.h" #include"ast_smt2_pp.h" #include"ast_translation.h" -#include"theory_seq.h" namespace smt { From 7ddd43e16df6275488cec300cc050f87419b7450 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 5 May 2017 15:29:58 -0400 Subject: [PATCH 553/562] first-class re.range support in theory_str --- src/smt/theory_str.cpp | 98 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index a26cb2ee2..e7c99da69 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1673,6 +1673,13 @@ zstring theory_str::get_std_regex_str(expr * regex) { expr * reg1Ast = a_regex->get_arg(0); zstring reg1Str = get_std_regex_str(reg1Ast); return zstring("(") + reg1Str + zstring(")*"); + } else if (u.re.is_range(a_regex)) { + expr * range1 = a_regex->get_arg(0); + expr * range2 = a_regex->get_arg(1); + zstring range1val, range2val; + u.str.is_string(range1, range1val); + u.str.is_string(range2, range2val); + return zstring("[") + range1val + zstring("-") + range2val + zstring("]"); } else { TRACE("str", tout << "BUG: unrecognized regex term " << mk_pp(regex, get_manager()) << std::endl;); UNREACHABLE(); return zstring(""); @@ -1752,6 +1759,36 @@ void theory_str::instantiate_axiom_RegexIn(enode * e) { expr_ref finalAxiom(mk_and(items), m); SASSERT(finalAxiom); assert_axiom(finalAxiom); + } else if (u.re.is_range(regex)) { + // (re.range "A" "Z") unfolds to (re.union "A" "B" ... "Z"); + // we rewrite to expr IFF (str = "A" or str = "B" or ... or str = "Z") + expr_ref lo(regex->get_arg(0), m); + expr_ref hi(regex->get_arg(1), m); + zstring str_lo, str_hi; + SASSERT(u.str.is_string(lo)); + SASSERT(u.str.is_string(hi)); + u.str.is_string(lo, str_lo); + u.str.is_string(hi, str_hi); + SASSERT(str_lo.length() == 1); + SASSERT(str_hi.length() == 1); + unsigned int c1 = str_lo[0]; + unsigned int c2 = str_hi[0]; + if (c1 > c2) { + // exchange + unsigned int tmp = c1; + c1 = c2; + c2 = tmp; + } + expr_ref_vector range_cases(m); + for (unsigned int ch = c1; ch <= c2; ++ch) { + zstring s_ch(ch); + expr_ref rhs(ctx.mk_eq_atom(str, u.str.mk_string(s_ch)), m); + range_cases.push_back(rhs); + } + expr_ref rhs(mk_or(range_cases), m); + expr_ref finalAxiom(m.mk_iff(ex, rhs), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); } else { TRACE("str", tout << "ERROR: unknown regex expression " << mk_pp(regex, m) << "!" << std::endl;); NOT_IMPLEMENTED_YET(); @@ -6165,7 +6202,6 @@ void nfa::convert_re(expr * e, unsigned & start, unsigned & end, seq_util & u) { zstring str; if (u.str.is_string(arg_str, str)) { TRACE("str", tout << "build NFA for '" << str << "'" << "\n";); - /* * For an n-character string, we make (n-1) intermediate states, * labelled i_(0) through i_(n-2). @@ -6227,6 +6263,33 @@ void nfa::convert_re(expr * e, unsigned & start, unsigned & end, seq_util & u) { make_epsilon_move(end_subex, start_subex); make_epsilon_move(end_subex, end); TRACE("str", tout << "star NFA: start = " << start << ", end = " << end << std::endl;); + } else if (u.re.is_range(e)) { + // range('a', 'z') + // start --'a'--> end + // start --'b'--> end + // ... + // start --'z'--> end + app * a = to_app(e); + expr * c1 = a->get_arg(0); + expr * c2 = a->get_arg(1); + zstring s_c1, s_c2; + u.str.is_string(c1, s_c1); + u.str.is_string(c2, s_c2); + + unsigned int id1 = s_c1[0]; + unsigned int id2 = s_c2[0]; + if (id1 > id2) { + unsigned int tmp = id1; + id1 = id2; + id2 = tmp; + } + + for (unsigned int i = id1; i <= id2; ++i) { + char ch = (char)i; + make_transition(start, ch, end); + } + + TRACE("str", tout << "range NFA: start = " << start << ", end = " << end << std::endl;); } else { TRACE("str", tout << "invalid regular expression" << std::endl;); m_valid = false; @@ -9429,6 +9492,39 @@ void theory_str::reduce_virtual_regex_in(expr * var, expr * regex, expr_ref_vect items.push_back(ctx.mk_eq_atom(var, unrollFunc)); items.push_back(ctx.mk_eq_atom(mk_strlen(var), mk_strlen(unrollFunc))); return; + } + // re.range + else if (u.re.is_range(regexFuncDecl)) { + // var in range("a", "z") + // ==> + // (var = "a" or var = "b" or ... or var = "z") + expr_ref lo(regexFuncDecl->get_arg(0), mgr); + expr_ref hi(regexFuncDecl->get_arg(1), mgr); + zstring str_lo, str_hi; + SASSERT(u.str.is_string(lo)); + SASSERT(u.str.is_string(hi)); + u.str.is_string(lo, str_lo); + u.str.is_string(hi, str_hi); + SASSERT(str_lo.length() == 1); + SASSERT(str_hi.length() == 1); + unsigned int c1 = str_lo[0]; + unsigned int c2 = str_hi[0]; + if (c1 > c2) { + // exchange + unsigned int tmp = c1; + c1 = c2; + c2 = tmp; + } + expr_ref_vector range_cases(mgr); + for (unsigned int ch = c1; ch <= c2; ++ch) { + zstring s_ch(ch); + expr_ref rhs(ctx.mk_eq_atom(var, u.str.mk_string(s_ch)), mgr); + range_cases.push_back(rhs); + } + expr_ref rhs(mk_or(range_cases), mgr); + SASSERT(rhs); + assert_axiom(rhs); + return; } else { get_manager().raise_exception("unrecognized regex operator"); UNREACHABLE(); From 8eb26e25c298ed17ae9a16ab8ed47983eb9316bb Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Fri, 5 May 2017 17:03:03 -0400 Subject: [PATCH 554/562] add new files to cmakelist.txt files Signed-off-by: Nikolaj Bjorner --- contrib/cmake/src/smt/CMakeLists.txt | 1 + contrib/cmake/src/smt/params/CMakeLists.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/contrib/cmake/src/smt/CMakeLists.txt b/contrib/cmake/src/smt/CMakeLists.txt index bd8ad3f31..c344e936f 100644 --- a/contrib/cmake/src/smt/CMakeLists.txt +++ b/contrib/cmake/src/smt/CMakeLists.txt @@ -58,6 +58,7 @@ z3_add_component(smt theory_opt.cpp theory_pb.cpp theory_seq.cpp + theory_str.cpp theory_utvpi.cpp theory_wmaxsat.cpp uses_theory.cpp diff --git a/contrib/cmake/src/smt/params/CMakeLists.txt b/contrib/cmake/src/smt/params/CMakeLists.txt index 67224a287..500423dcc 100644 --- a/contrib/cmake/src/smt/params/CMakeLists.txt +++ b/contrib/cmake/src/smt/params/CMakeLists.txt @@ -8,6 +8,7 @@ z3_add_component(smt_params theory_array_params.cpp theory_bv_params.cpp theory_pb_params.cpp + theory_str_params.cpp COMPONENT_DEPENDENCIES ast bit_blaster From 21c8f4aae01880dba291cc94accd741a394cb3c6 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 5 May 2017 19:26:15 -0400 Subject: [PATCH 555/562] formatting theory_str.cpp --- src/smt/theory_str.cpp | 20032 +++++++++++++++++++-------------------- 1 file changed, 10015 insertions(+), 10017 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index e7c99da69..4a6a6da5b 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1,19 +1,19 @@ /*++ -Module Name: + Module Name: - theory_str.cpp + theory_str.cpp -Abstract: + Abstract: - String Theory Plugin + String Theory Plugin -Author: + Author: - Murphy Berzish and Yunhui Zheng + Murphy Berzish and Yunhui Zheng -Revision History: + Revision History: ---*/ + --*/ #include"ast_smt2_pp.h" #include"smt_context.h" #include"theory_str.h" @@ -24,13 +24,11 @@ Revision History: #include #include #include"theory_seq_empty.h" - -#include "../ast/ast.h" #include"theory_arith.h" namespace smt { - -theory_str::theory_str(ast_manager & m, theory_str_params const & params): + + theory_str::theory_str(ast_manager & m, theory_str_params const & params): theory(m.mk_family_id("seq")), m_params(params), /* Options */ @@ -64,3864 +62,3041 @@ theory_str::theory_str(ast_manager & m, theory_str_params const & params): cacheMissCount(0), m_find(*this), m_trail_stack(*this) -{ - initialize_charset(); -} + { + initialize_charset(); + } -theory_str::~theory_str() { - m_trail_stack.reset(); -} + theory_str::~theory_str() { + m_trail_stack.reset(); + } -expr * theory_str::mk_string(zstring const& str) { - if (m_params.m_StringConstantCache) { - ++totalCacheAccessCount; - expr * val; - if (stringConstantCache.find(str, val)) { - return val; + expr * theory_str::mk_string(zstring const& str) { + if (m_params.m_StringConstantCache) { + ++totalCacheAccessCount; + expr * val; + if (stringConstantCache.find(str, val)) { + return val; + } else { + val = u.str.mk_string(str); + m_trail.push_back(val); + stringConstantCache.insert(str, val); + return val; + } } else { - val = u.str.mk_string(str); - m_trail.push_back(val); - stringConstantCache.insert(str, val); - return val; + return u.str.mk_string(str); } - } else { - return u.str.mk_string(str); - } -} - -expr * theory_str::mk_string(const char * str) { - symbol sym(str); - return u.str.mk_string(sym); -} - -void theory_str::initialize_charset() { - bool defaultCharset = true; - if (defaultCharset) { - // valid C strings can't contain the null byte ('\0') - charSetSize = 255; - char_set = alloc_svect(char, charSetSize); - int idx = 0; - // small letters - for (int i = 97; i < 123; i++) { - char_set[idx] = (char) i; - charSetLookupTable[char_set[idx]] = idx; - idx++; - } - // caps - for (int i = 65; i < 91; i++) { - char_set[idx] = (char) i; - charSetLookupTable[char_set[idx]] = idx; - idx++; - } - // numbers - for (int i = 48; i < 58; i++) { - char_set[idx] = (char) i; - charSetLookupTable[char_set[idx]] = idx; - idx++; - } - // printable marks - 1 - for (int i = 32; i < 48; i++) { - char_set[idx] = (char) i; - charSetLookupTable[char_set[idx]] = idx; - idx++; - } - // printable marks - 2 - for (int i = 58; i < 65; i++) { - char_set[idx] = (char) i; - charSetLookupTable[char_set[idx]] = idx; - idx++; - } - // printable marks - 3 - for (int i = 91; i < 97; i++) { - char_set[idx] = (char) i; - charSetLookupTable[char_set[idx]] = idx; - idx++; - } - // printable marks - 4 - for (int i = 123; i < 127; i++) { - char_set[idx] = (char) i; - charSetLookupTable[char_set[idx]] = idx; - idx++; - } - // non-printable - 1 - for (int i = 1; i < 32; i++) { - char_set[idx] = (char) i; - charSetLookupTable[char_set[idx]] = idx; - idx++; - } - // non-printable - 2 - for (int i = 127; i < 256; i++) { - char_set[idx] = (char) i; - charSetLookupTable[char_set[idx]] = idx; - idx++; - } - } else { - const char setset[] = { 'a', 'b', 'c' }; - int fSize = sizeof(setset) / sizeof(char); - - char_set = alloc_svect(char, fSize); - charSetSize = fSize; - for (int i = 0; i < charSetSize; i++) { - char_set[i] = setset[i]; - charSetLookupTable[setset[i]] = i; - } - } -} - -void theory_str::assert_axiom(expr * e) { - if (opt_VerifyFinalCheckProgress) { - finalCheckProgressIndicator = true; } - if (get_manager().is_true(e)) return; - TRACE("str", tout << "asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); - context & ctx = get_context(); - if (!ctx.b_internalized(e)) { - ctx.internalize(e, false); - } - literal lit(ctx.get_literal(e)); - ctx.mark_as_relevant(lit); - ctx.mk_th_axiom(get_id(), 1, &lit); - - // crash/error avoidance: add all axioms to the trail - m_trail.push_back(e); - - //TRACE("str", tout << "done asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); -} - -expr * theory_str::rewrite_implication(expr * premise, expr * conclusion) { - ast_manager & m = get_manager(); - return m.mk_or(m.mk_not(premise), conclusion); -} - -void theory_str::assert_implication(expr * premise, expr * conclusion) { - ast_manager & m = get_manager(); - TRACE("str", tout << "asserting implication " << mk_ismt2_pp(premise, m) << " -> " << mk_ismt2_pp(conclusion, m) << std::endl;); - expr_ref axiom(m.mk_or(m.mk_not(premise), conclusion), m); - assert_axiom(axiom); -} - -bool theory_str::internalize_atom(app * atom, bool gate_ctx) { - return internalize_term(atom); -} - -bool theory_str::internalize_term(app * term) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - SASSERT(term->get_family_id() == get_family_id()); - - TRACE("str", tout << "internalizing term: " << mk_ismt2_pp(term, get_manager()) << std::endl;); - - // emulation of user_smt_theory::internalize_term() - - unsigned num_args = term->get_num_args(); - for (unsigned i = 0; i < num_args; ++i) { - ctx.internalize(term->get_arg(i), false); - } - if (ctx.e_internalized(term)) { - enode * e = ctx.get_enode(term); - mk_var(e); - return true; - } - // m_parents.push_back(term); - enode * e = ctx.mk_enode(term, false, m.is_bool(term), true); - if (m.is_bool(term)) { - bool_var bv = ctx.mk_bool_var(term); - ctx.set_var_theory(bv, get_id()); - ctx.set_enode_flag(bv, true); - } - // make sure every argument is attached to a theory variable - for (unsigned i = 0; i < num_args; ++i) { - enode * arg = e->get_arg(i); - theory_var v_arg = mk_var(arg); - TRACE("str", tout << "arg has theory var #" << v_arg << std::endl;); + expr * theory_str::mk_string(const char * str) { + symbol sym(str); + return u.str.mk_string(sym); } - theory_var v = mk_var(e); - TRACE("str", tout << "term has theory var #" << v << std::endl;); + void theory_str::initialize_charset() { + bool defaultCharset = true; + if (defaultCharset) { + // valid C strings can't contain the null byte ('\0') + charSetSize = 255; + char_set = alloc_svect(char, charSetSize); + int idx = 0; + // small letters + for (int i = 97; i < 123; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + // caps + for (int i = 65; i < 91; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + // numbers + for (int i = 48; i < 58; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + // printable marks - 1 + for (int i = 32; i < 48; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + // printable marks - 2 + for (int i = 58; i < 65; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + // printable marks - 3 + for (int i = 91; i < 97; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + // printable marks - 4 + for (int i = 123; i < 127; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + // non-printable - 1 + for (int i = 1; i < 32; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + // non-printable - 2 + for (int i = 127; i < 256; i++) { + char_set[idx] = (char) i; + charSetLookupTable[char_set[idx]] = idx; + idx++; + } + } else { + const char setset[] = { 'a', 'b', 'c' }; + int fSize = sizeof(setset) / sizeof(char); - if (opt_EagerStringConstantLengthAssertions && u.str.is_string(term)) { - TRACE("str", tout << "eagerly asserting length of string term " << mk_pp(term, m) << std::endl;); - m_basicstr_axiom_todo.insert(e); - } - return true; -} - -enode* theory_str::ensure_enode(expr* e) { - context& ctx = get_context(); - if (!ctx.e_internalized(e)) { - ctx.internalize(e, false); - } - enode* n = ctx.get_enode(e); - ctx.mark_as_relevant(n); - return n; -} - -void theory_str::refresh_theory_var(expr * e) { - enode * en = ensure_enode(e); - theory_var v = mk_var(en); - TRACE("str", tout << "refresh " << mk_pp(e, get_manager()) << ": v#" << v << std::endl;); - m_basicstr_axiom_todo.push_back(en); -} - -theory_var theory_str::mk_var(enode* n) { - TRACE("str", tout << "mk_var for " << mk_pp(n->get_owner(), get_manager()) << std::endl;); - ast_manager & m = get_manager(); - if (!(m.get_sort(n->get_owner()) == u.str.mk_string_sort())) { - return null_theory_var; - } - if (is_attached_to_var(n)) { - TRACE("str", tout << "already attached to theory var" << std::endl;); - return n->get_th_var(get_id()); - } else { - theory_var v = theory::mk_var(n); - m_find.mk_var(); - TRACE("str", tout << "new theory var v#" << v << std::endl;); - get_context().attach_th_var(n, this, v); - get_context().mark_as_relevant(n); - return v; - } -} - -static void cut_vars_map_copy(std::map & dest, std::map & src) { - std::map::iterator itor = src.begin(); - for (; itor != src.end(); itor++) { - dest[itor->first] = 1; - } -} - -bool theory_str::has_self_cut(expr * n1, expr * n2) { - if (!cut_var_map.contains(n1)) { - return false; - } - if (!cut_var_map.contains(n2)) { - return false; - } - if (cut_var_map[n1].empty() || cut_var_map[n2].empty()) { - return false; + char_set = alloc_svect(char, fSize); + charSetSize = fSize; + for (int i = 0; i < charSetSize; i++) { + char_set[i] = setset[i]; + charSetLookupTable[setset[i]] = i; + } + } } - std::map::iterator itor = cut_var_map[n1].top()->vars.begin(); - for (; itor != cut_var_map[n1].top()->vars.end(); ++itor) { - if (cut_var_map[n2].top()->vars.find(itor->first) != cut_var_map[n2].top()->vars.end()) { + void theory_str::assert_axiom(expr * e) { + if (opt_VerifyFinalCheckProgress) { + finalCheckProgressIndicator = true; + } + + if (get_manager().is_true(e)) return; + TRACE("str", tout << "asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); + context & ctx = get_context(); + if (!ctx.b_internalized(e)) { + ctx.internalize(e, false); + } + literal lit(ctx.get_literal(e)); + ctx.mark_as_relevant(lit); + ctx.mk_th_axiom(get_id(), 1, &lit); + + // crash/error avoidance: add all axioms to the trail + m_trail.push_back(e); + + //TRACE("str", tout << "done asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;); + } + + expr * theory_str::rewrite_implication(expr * premise, expr * conclusion) { + ast_manager & m = get_manager(); + return m.mk_or(m.mk_not(premise), conclusion); + } + + void theory_str::assert_implication(expr * premise, expr * conclusion) { + ast_manager & m = get_manager(); + TRACE("str", tout << "asserting implication " << mk_ismt2_pp(premise, m) << " -> " << mk_ismt2_pp(conclusion, m) << std::endl;); + expr_ref axiom(m.mk_or(m.mk_not(premise), conclusion), m); + assert_axiom(axiom); + } + + bool theory_str::internalize_atom(app * atom, bool gate_ctx) { + return internalize_term(atom); + } + + bool theory_str::internalize_term(app * term) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + SASSERT(term->get_family_id() == get_family_id()); + + TRACE("str", tout << "internalizing term: " << mk_ismt2_pp(term, get_manager()) << std::endl;); + + // emulation of user_smt_theory::internalize_term() + + unsigned num_args = term->get_num_args(); + for (unsigned i = 0; i < num_args; ++i) { + ctx.internalize(term->get_arg(i), false); + } + if (ctx.e_internalized(term)) { + enode * e = ctx.get_enode(term); + mk_var(e); return true; } - } - return false; -} + // m_parents.push_back(term); + enode * e = ctx.mk_enode(term, false, m.is_bool(term), true); + if (m.is_bool(term)) { + bool_var bv = ctx.mk_bool_var(term); + ctx.set_var_theory(bv, get_id()); + ctx.set_enode_flag(bv, true); + } + // make sure every argument is attached to a theory variable + for (unsigned i = 0; i < num_args; ++i) { + enode * arg = e->get_arg(i); + theory_var v_arg = mk_var(arg); + TRACE("str", tout << "arg has theory var #" << v_arg << std::endl;); + } -void theory_str::add_cut_info_one_node(expr * baseNode, int slevel, expr * node) { - // crash avoidance? - m_trail.push_back(baseNode); - m_trail.push_back(node); - if (!cut_var_map.contains(baseNode)) { - T_cut * varInfo = alloc(T_cut); - varInfo->level = slevel; - varInfo->vars[node] = 1; - cut_var_map.insert(baseNode, std::stack()); - cut_var_map[baseNode].push(varInfo); - TRACE("str", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); - } else { - if (cut_var_map[baseNode].empty()) { + theory_var v = mk_var(e); + TRACE("str", tout << "term has theory var #" << v << std::endl;); + + if (opt_EagerStringConstantLengthAssertions && u.str.is_string(term)) { + TRACE("str", tout << "eagerly asserting length of string term " << mk_pp(term, m) << std::endl;); + m_basicstr_axiom_todo.insert(e); + } + return true; + } + + enode* theory_str::ensure_enode(expr* e) { + context& ctx = get_context(); + if (!ctx.e_internalized(e)) { + ctx.internalize(e, false); + } + enode* n = ctx.get_enode(e); + ctx.mark_as_relevant(n); + return n; + } + + void theory_str::refresh_theory_var(expr * e) { + enode * en = ensure_enode(e); + theory_var v = mk_var(en); + TRACE("str", tout << "refresh " << mk_pp(e, get_manager()) << ": v#" << v << std::endl;); + m_basicstr_axiom_todo.push_back(en); + } + + theory_var theory_str::mk_var(enode* n) { + TRACE("str", tout << "mk_var for " << mk_pp(n->get_owner(), get_manager()) << std::endl;); + ast_manager & m = get_manager(); + if (!(m.get_sort(n->get_owner()) == u.str.mk_string_sort())) { + return null_theory_var; + } + if (is_attached_to_var(n)) { + TRACE("str", tout << "already attached to theory var" << std::endl;); + return n->get_th_var(get_id()); + } else { + theory_var v = theory::mk_var(n); + m_find.mk_var(); + TRACE("str", tout << "new theory var v#" << v << std::endl;); + get_context().attach_th_var(n, this, v); + get_context().mark_as_relevant(n); + return v; + } + } + + static void cut_vars_map_copy(std::map & dest, std::map & src) { + std::map::iterator itor = src.begin(); + for (; itor != src.end(); itor++) { + dest[itor->first] = 1; + } + } + + bool theory_str::has_self_cut(expr * n1, expr * n2) { + if (!cut_var_map.contains(n1)) { + return false; + } + if (!cut_var_map.contains(n2)) { + return false; + } + if (cut_var_map[n1].empty() || cut_var_map[n2].empty()) { + return false; + } + + std::map::iterator itor = cut_var_map[n1].top()->vars.begin(); + for (; itor != cut_var_map[n1].top()->vars.end(); ++itor) { + if (cut_var_map[n2].top()->vars.find(itor->first) != cut_var_map[n2].top()->vars.end()) { + return true; + } + } + return false; + } + + void theory_str::add_cut_info_one_node(expr * baseNode, int slevel, expr * node) { + // crash avoidance? + m_trail.push_back(baseNode); + m_trail.push_back(node); + if (!cut_var_map.contains(baseNode)) { T_cut * varInfo = alloc(T_cut); varInfo->level = slevel; varInfo->vars[node] = 1; + cut_var_map.insert(baseNode, std::stack()); cut_var_map[baseNode].push(varInfo); TRACE("str", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); } else { - if (cut_var_map[baseNode].top()->level < slevel) { + if (cut_var_map[baseNode].empty()) { T_cut * varInfo = alloc(T_cut); varInfo->level = slevel; - cut_vars_map_copy(varInfo->vars, cut_var_map[baseNode].top()->vars); varInfo->vars[node] = 1; cut_var_map[baseNode].push(varInfo); TRACE("str", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); - } else if (cut_var_map[baseNode].top()->level == slevel) { - cut_var_map[baseNode].top()->vars[node] = 1; - TRACE("str", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); } else { - get_manager().raise_exception("entered illegal state during add_cut_info_one_node()"); + if (cut_var_map[baseNode].top()->level < slevel) { + T_cut * varInfo = alloc(T_cut); + varInfo->level = slevel; + cut_vars_map_copy(varInfo->vars, cut_var_map[baseNode].top()->vars); + varInfo->vars[node] = 1; + cut_var_map[baseNode].push(varInfo); + TRACE("str", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); + } else if (cut_var_map[baseNode].top()->level == slevel) { + cut_var_map[baseNode].top()->vars[node] = 1; + TRACE("str", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;); + } else { + get_manager().raise_exception("entered illegal state during add_cut_info_one_node()"); + } } } } -} -void theory_str::add_cut_info_merge(expr * destNode, int slevel, expr * srcNode) { - // crash avoidance? - m_trail.push_back(destNode); - m_trail.push_back(srcNode); - if (!cut_var_map.contains(srcNode)) { - get_manager().raise_exception("illegal state in add_cut_info_merge(): cut_var_map doesn't contain srcNode"); - } + void theory_str::add_cut_info_merge(expr * destNode, int slevel, expr * srcNode) { + // crash avoidance? + m_trail.push_back(destNode); + m_trail.push_back(srcNode); + if (!cut_var_map.contains(srcNode)) { + get_manager().raise_exception("illegal state in add_cut_info_merge(): cut_var_map doesn't contain srcNode"); + } - if (cut_var_map[srcNode].empty()) { - get_manager().raise_exception("illegal state in add_cut_info_merge(): cut_var_map[srcNode] is empty"); - } + if (cut_var_map[srcNode].empty()) { + get_manager().raise_exception("illegal state in add_cut_info_merge(): cut_var_map[srcNode] is empty"); + } - if (!cut_var_map.contains(destNode)) { - T_cut * varInfo = alloc(T_cut); - varInfo->level = slevel; - cut_vars_map_copy(varInfo->vars, cut_var_map[srcNode].top()->vars); - cut_var_map.insert(destNode, std::stack()); - cut_var_map[destNode].push(varInfo); - TRACE("str", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << " [" << slevel << "]" << std::endl;); - } else { - if (cut_var_map[destNode].empty() || cut_var_map[destNode].top()->level < slevel) { + if (!cut_var_map.contains(destNode)) { T_cut * varInfo = alloc(T_cut); varInfo->level = slevel; - cut_vars_map_copy(varInfo->vars, cut_var_map[destNode].top()->vars); cut_vars_map_copy(varInfo->vars, cut_var_map[srcNode].top()->vars); + cut_var_map.insert(destNode, std::stack()); cut_var_map[destNode].push(varInfo); TRACE("str", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << " [" << slevel << "]" << std::endl;); - } else if (cut_var_map[destNode].top()->level == slevel) { - cut_vars_map_copy(cut_var_map[destNode].top()->vars, cut_var_map[srcNode].top()->vars); - TRACE("str", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << " [" << slevel << "]" << std::endl;); } else { - get_manager().raise_exception("illegal state in add_cut_info_merge(): inconsistent slevels"); - } - } -} - -void theory_str::check_and_init_cut_var(expr * node) { - if (cut_var_map.contains(node)) { - return; - } else if (!u.str.is_string(node)) { - add_cut_info_one_node(node, -1, node); - } -} - -literal theory_str::mk_literal(expr* _e) { - ast_manager & m = get_manager(); - expr_ref e(_e, m); - context& ctx = get_context(); - ensure_enode(e); - return ctx.get_literal(e); -} - -app * theory_str::mk_int(int n) { - return m_autil.mk_numeral(rational(n), true); -} - -app * theory_str::mk_int(rational & q) { - return m_autil.mk_numeral(q, true); -} - -expr * theory_str::mk_internal_lenTest_var(expr * node, int lTries) { - ast_manager & m = get_manager(); - - std::stringstream ss; - ss << "$$_len_" << mk_ismt2_pp(node, m) << "_" << lTries << "_" << tmpLenTestVarCount; - tmpLenTestVarCount += 1; - std::string name = ss.str(); - app * var = mk_str_var(name); - internal_lenTest_vars.insert(var); - m_trail.push_back(var); - return var; -} - -expr * theory_str::mk_internal_valTest_var(expr * node, int len, int vTries) { - ast_manager & m = get_manager(); - std::stringstream ss; - ss << "$$_val_" << mk_ismt2_pp(node, m) << "_" << len << "_" << vTries << "_" << tmpValTestVarCount; - tmpValTestVarCount += 1; - std::string name = ss.str(); - app * var = mk_str_var(name); - internal_valTest_vars.insert(var); - m_trail.push_back(var); - return var; -} - -void theory_str::track_variable_scope(expr * var) { - if (internal_variable_scope_levels.find(sLevel) == internal_variable_scope_levels.end()) { - internal_variable_scope_levels[sLevel] = std::set(); - } - internal_variable_scope_levels[sLevel].insert(var); -} - -app * theory_str::mk_internal_xor_var() { - return mk_int_var("$$_xor"); -} - -app * theory_str::mk_int_var(std::string name) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - TRACE("str", tout << "creating integer variable " << name << " at scope level " << sLevel << std::endl;); - - sort * int_sort = m.mk_sort(m_autil.get_family_id(), INT_SORT); - app * a = m.mk_fresh_const(name.c_str(), int_sort); - - ctx.internalize(a, false); - SASSERT(ctx.get_enode(a) != NULL); - SASSERT(ctx.e_internalized(a)); - ctx.mark_as_relevant(a); - // I'm assuming that this combination will do the correct thing in the integer theory. - - //mk_var(ctx.get_enode(a)); - m_trail.push_back(a); - //variable_set.insert(a); - //internal_variable_set.insert(a); - //track_variable_scope(a); - - return a; -} - -app * theory_str::mk_unroll_bound_var() { - return mk_int_var("unroll"); -} - -app * theory_str::mk_unroll_test_var() { - app * v = mk_str_var("unrollTest"); // was uRt - internal_unrollTest_vars.insert(v); - track_variable_scope(v); - return v; -} - -app * theory_str::mk_str_var(std::string name) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - TRACE("str", tout << "creating string variable " << name << " at scope level " << sLevel << std::endl;); - - sort * string_sort = u.str.mk_string_sort(); - app * a = m.mk_fresh_const(name.c_str(), string_sort); - - TRACE("str", tout << "a->get_family_id() = " << a->get_family_id() << std::endl - << "this->get_family_id() = " << this->get_family_id() << std::endl;); - - // I have a hunch that this may not get internalized for free... - ctx.internalize(a, false); - SASSERT(ctx.get_enode(a) != NULL); - SASSERT(ctx.e_internalized(a)); - // this might help?? - mk_var(ctx.get_enode(a)); - m_basicstr_axiom_todo.push_back(ctx.get_enode(a)); - TRACE("str", tout << "add " << mk_pp(a, m) << " to m_basicstr_axiom_todo" << std::endl;); - - m_trail.push_back(a); - variable_set.insert(a); - internal_variable_set.insert(a); - track_variable_scope(a); - - return a; -} - -app * theory_str::mk_regex_rep_var() { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - sort * string_sort = u.str.mk_string_sort(); - app * a = m.mk_fresh_const("regex", string_sort); - - ctx.internalize(a, false); - SASSERT(ctx.get_enode(a) != NULL); - SASSERT(ctx.e_internalized(a)); - mk_var(ctx.get_enode(a)); - m_basicstr_axiom_todo.push_back(ctx.get_enode(a)); - TRACE("str", tout << "add " << mk_pp(a, m) << " to m_basicstr_axiom_todo" << std::endl;); - - m_trail.push_back(a); - variable_set.insert(a); - //internal_variable_set.insert(a); - regex_variable_set.insert(a); - track_variable_scope(a); - - return a; -} - -void theory_str::add_nonempty_constraint(expr * s) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - expr_ref ax1(m.mk_not(ctx.mk_eq_atom(s, mk_string(""))), m); - assert_axiom(ax1); - - { - // build LHS - expr_ref len_str(mk_strlen(s), m); - SASSERT(len_str); - // build RHS - expr_ref zero(m_autil.mk_numeral(rational(0), true), m); - SASSERT(zero); - // build LHS > RHS and assert - // we have to build !(LHS <= RHS) instead - expr_ref lhs_gt_rhs(m.mk_not(m_autil.mk_le(len_str, zero)), m); - SASSERT(lhs_gt_rhs); - assert_axiom(lhs_gt_rhs); - } -} - -app * theory_str::mk_nonempty_str_var() { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - std::stringstream ss; - ss << tmpStringVarCount; - tmpStringVarCount++; - std::string name = "$$_str" + ss.str(); - - TRACE("str", tout << "creating nonempty string variable " << name << " at scope level " << sLevel << std::endl;); - - sort * string_sort = u.str.mk_string_sort(); - app * a = m.mk_fresh_const(name.c_str(), string_sort); - - ctx.internalize(a, false); - SASSERT(ctx.get_enode(a) != NULL); - // this might help?? - mk_var(ctx.get_enode(a)); - - // assert a variation of the basic string axioms that ensures this string is nonempty - { - // build LHS - expr_ref len_str(mk_strlen(a), m); - SASSERT(len_str); - // build RHS - expr_ref zero(m_autil.mk_numeral(rational(0), true), m); - SASSERT(zero); - // build LHS > RHS and assert - // we have to build !(LHS <= RHS) instead - expr_ref lhs_gt_rhs(m.mk_not(m_autil.mk_le(len_str, zero)), m); - SASSERT(lhs_gt_rhs); - assert_axiom(lhs_gt_rhs); - } - - // add 'a' to variable sets, so we can keep track of it - m_trail.push_back(a); - variable_set.insert(a); - internal_variable_set.insert(a); - track_variable_scope(a); - - return a; -} - -app * theory_str::mk_unroll(expr * n, expr * bound) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - expr * args[2] = {n, bound}; - app * unrollFunc = get_manager().mk_app(get_id(), _OP_RE_UNROLL, 0, 0, 2, args); - m_trail.push_back(unrollFunc); - - expr_ref_vector items(m); - items.push_back(ctx.mk_eq_atom(ctx.mk_eq_atom(bound, mk_int(0)), ctx.mk_eq_atom(unrollFunc, mk_string("")))); - items.push_back(m_autil.mk_ge(bound, mk_int(0))); - items.push_back(m_autil.mk_ge(mk_strlen(unrollFunc), mk_int(0))); - - expr_ref finalAxiom(mk_and(items), m); - SASSERT(finalAxiom); - assert_axiom(finalAxiom); - return unrollFunc; -} - -app * theory_str::mk_contains(expr * haystack, expr * needle) { - app * contains = u.str.mk_contains(haystack, needle); // TODO double-check semantics/argument order - m_trail.push_back(contains); - // immediately force internalization so that axiom setup does not fail - get_context().internalize(contains, false); - set_up_axioms(contains); - return contains; -} - -app * theory_str::mk_indexof(expr * haystack, expr * needle) { - // TODO check meaning of the third argument here - app * indexof = u.str.mk_index(haystack, needle, mk_int(0)); - m_trail.push_back(indexof); - // immediately force internalization so that axiom setup does not fail - get_context().internalize(indexof, false); - set_up_axioms(indexof); - return indexof; -} - -app * theory_str::mk_strlen(expr * e) { - /*if (m_strutil.is_string(e)) {*/ if (false) { - zstring strval; - u.str.is_string(e, strval); - unsigned int len = strval.length(); - return m_autil.mk_numeral(rational(len), true); - } else { - if (false) { - // use cache - app * lenTerm = NULL; - if (!length_ast_map.find(e, lenTerm)) { - lenTerm = u.str.mk_length(e); - length_ast_map.insert(e, lenTerm); - m_trail.push_back(lenTerm); + if (cut_var_map[destNode].empty() || cut_var_map[destNode].top()->level < slevel) { + T_cut * varInfo = alloc(T_cut); + varInfo->level = slevel; + cut_vars_map_copy(varInfo->vars, cut_var_map[destNode].top()->vars); + cut_vars_map_copy(varInfo->vars, cut_var_map[srcNode].top()->vars); + cut_var_map[destNode].push(varInfo); + TRACE("str", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << " [" << slevel << "]" << std::endl;); + } else if (cut_var_map[destNode].top()->level == slevel) { + cut_vars_map_copy(cut_var_map[destNode].top()->vars, cut_var_map[srcNode].top()->vars); + TRACE("str", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << " [" << slevel << "]" << std::endl;); + } else { + get_manager().raise_exception("illegal state in add_cut_info_merge(): inconsistent slevels"); } - return lenTerm; - } else { - // always regen - return u.str.mk_length(e); } } -} -/* - * Returns the simplified concatenation of two expressions, - * where either both expressions are constant strings - * or one expression is the empty string. - * If this precondition does not hold, the function returns NULL. - * (note: this function was strTheory::Concat()) - */ -expr * theory_str::mk_concat_const_str(expr * n1, expr * n2) { - bool n1HasEqcValue = false; - bool n2HasEqcValue = false; - expr * v1 = get_eqc_value(n1, n1HasEqcValue); - expr * v2 = get_eqc_value(n2, n2HasEqcValue); - if (n1HasEqcValue && n2HasEqcValue) { - zstring n1_str; - u.str.is_string(v1, n1_str); - zstring n2_str; - u.str.is_string(v2, n2_str); - zstring result = n1_str + n2_str; - return mk_string(result); - } else if (n1HasEqcValue && !n2HasEqcValue) { - zstring n1_str; - u.str.is_string(v1, n1_str); - if (n1_str.empty()) { - return n2; - } - } else if (!n1HasEqcValue && n2HasEqcValue) { - zstring n2_str; - u.str.is_string(v2, n2_str); - if (n2_str.empty()) { - return n1; + void theory_str::check_and_init_cut_var(expr * node) { + if (cut_var_map.contains(node)) { + return; + } else if (!u.str.is_string(node)) { + add_cut_info_one_node(node, -1, node); } } - return NULL; -} -expr * theory_str::mk_concat(expr * n1, expr * n2) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - ENSURE(n1 != NULL); - ENSURE(n2 != NULL); - bool n1HasEqcValue = false; - bool n2HasEqcValue = false; - n1 = get_eqc_value(n1, n1HasEqcValue); - n2 = get_eqc_value(n2, n2HasEqcValue); - if (n1HasEqcValue && n2HasEqcValue) { - return mk_concat_const_str(n1, n2); - } else if (n1HasEqcValue && !n2HasEqcValue) { - bool n2_isConcatFunc = u.str.is_concat(to_app(n2)); - zstring n1_str; - u.str.is_string(n1, n1_str); - if (n1_str.empty()) { - return n2; - } - if (n2_isConcatFunc) { - expr * n2_arg0 = to_app(n2)->get_arg(0); - expr * n2_arg1 = to_app(n2)->get_arg(1); - if (u.str.is_string(n2_arg0)) { - n1 = mk_concat_const_str(n1, n2_arg0); // n1 will be a constant - n2 = n2_arg1; - } - } - } else if (!n1HasEqcValue && n2HasEqcValue) { - zstring n2_str; - u.str.is_string(n2, n2_str); - if (n2_str.empty()) { - return n1; - } - - if (u.str.is_concat(to_app(n1))) { - expr * n1_arg0 = to_app(n1)->get_arg(0); - expr * n1_arg1 = to_app(n1)->get_arg(1); - if (u.str.is_string(n1_arg1)) { - n1 = n1_arg0; - n2 = mk_concat_const_str(n1_arg1, n2); // n2 will be a constant - } - } - } else { - if (u.str.is_concat(to_app(n1)) && u.str.is_concat(to_app(n2))) { - expr * n1_arg0 = to_app(n1)->get_arg(0); - expr * n1_arg1 = to_app(n1)->get_arg(1); - expr * n2_arg0 = to_app(n2)->get_arg(0); - expr * n2_arg1 = to_app(n2)->get_arg(1); - if (u.str.is_string(n1_arg1) && u.str.is_string(n2_arg0)) { - expr * tmpN1 = n1_arg0; - expr * tmpN2 = mk_concat_const_str(n1_arg1, n2_arg0); - n1 = mk_concat(tmpN1, tmpN2); - n2 = n2_arg1; - } - } - } - - //------------------------------------------------------ - // * expr * ast1 = mk_2_arg_app(ctx, td->Concat, n1, n2); - // * expr * ast2 = mk_2_arg_app(ctx, td->Concat, n1, n2); - // Z3 treats (ast1) and (ast2) as two different nodes. - //------------------------------------------------------- - - expr * concatAst = NULL; - - if (!concat_astNode_map.find(n1, n2, concatAst)) { - concatAst = u.str.mk_concat(n1, n2); - m_trail.push_back(concatAst); - concat_astNode_map.insert(n1, n2, concatAst); - - expr_ref concat_length(mk_strlen(concatAst), m); - - ptr_vector childrenVector; - get_nodes_in_concat(concatAst, childrenVector); - expr_ref_vector items(m); - for (unsigned int i = 0; i < childrenVector.size(); i++) { - items.push_back(mk_strlen(childrenVector.get(i))); - } - expr_ref lenAssert(ctx.mk_eq_atom(concat_length, m_autil.mk_add(items.size(), items.c_ptr())), m); - assert_axiom(lenAssert); - } - return concatAst; -} - -bool theory_str::can_propagate() { - return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty() - || !m_concat_axiom_todo.empty() || !m_concat_eval_todo.empty() - || !m_library_aware_axiom_todo.empty() - || !m_delayed_axiom_setup_terms.empty(); - ; -} - -void theory_str::propagate() { - context & ctx = get_context(); - while (can_propagate()) { - TRACE("str", tout << "propagating..." << std::endl;); - for (unsigned i = 0; i < m_basicstr_axiom_todo.size(); ++i) { - instantiate_basic_string_axioms(m_basicstr_axiom_todo[i]); - } - m_basicstr_axiom_todo.reset(); - TRACE("str", tout << "reset m_basicstr_axiom_todo" << std::endl;); - - for (unsigned i = 0; i < m_str_eq_todo.size(); ++i) { - std::pair pair = m_str_eq_todo[i]; - enode * lhs = pair.first; - enode * rhs = pair.second; - handle_equality(lhs->get_owner(), rhs->get_owner()); - } - m_str_eq_todo.reset(); - - for (unsigned i = 0; i < m_concat_axiom_todo.size(); ++i) { - instantiate_concat_axiom(m_concat_axiom_todo[i]); - } - m_concat_axiom_todo.reset(); - - for (unsigned i = 0; i < m_concat_eval_todo.size(); ++i) { - try_eval_concat(m_concat_eval_todo[i]); - } - m_concat_eval_todo.reset(); - - for (unsigned i = 0; i < m_library_aware_axiom_todo.size(); ++i) { - enode * e = m_library_aware_axiom_todo[i]; - app * a = e->get_owner(); - if (u.str.is_stoi(a)) { - instantiate_axiom_str_to_int(e); - } else if (u.str.is_itos(a)) { - instantiate_axiom_int_to_str(e); - } else if (u.str.is_at(a)) { - instantiate_axiom_CharAt(e); - } else if (u.str.is_prefix(a)) { - instantiate_axiom_prefixof(e); - } else if (u.str.is_suffix(a)) { - instantiate_axiom_suffixof(e); - } else if (u.str.is_contains(a)) { - instantiate_axiom_Contains(e); - } else if (u.str.is_index(a)) { - instantiate_axiom_Indexof(e); - /* TODO NEXT: Indexof2/Lastindexof rewrite? - } else if (is_Indexof2(e)) { - instantiate_axiom_Indexof2(e); - } else if (is_LastIndexof(e)) { - instantiate_axiom_LastIndexof(e); - */ - } else if (u.str.is_extract(a)) { - // TODO check semantics of substr vs. extract - instantiate_axiom_Substr(e); - } else if (u.str.is_replace(a)) { - instantiate_axiom_Replace(e); - } else if (u.str.is_in_re(a)) { - instantiate_axiom_RegexIn(e); - } else { - TRACE("str", tout << "BUG: unhandled library-aware term " << mk_pp(e->get_owner(), get_manager()) << std::endl;); - NOT_IMPLEMENTED_YET(); - } - } - m_library_aware_axiom_todo.reset(); - - for (unsigned i = 0; i < m_delayed_axiom_setup_terms.size(); ++i) { - // I think this is okay - ctx.internalize(m_delayed_axiom_setup_terms[i].get(), false); - set_up_axioms(m_delayed_axiom_setup_terms[i].get()); - } - m_delayed_axiom_setup_terms.reset(); - } -} - -/* - * Attempt to evaluate a concat over constant strings, - * and if this is possible, assert equality between the - * flattened string and the original term. - */ - -void theory_str::try_eval_concat(enode * cat) { - app * a_cat = cat->get_owner(); - SASSERT(u.str.is_concat(a_cat)); - - context & ctx = get_context(); - ast_manager & m = get_manager(); - - TRACE("str", tout << "attempting to flatten " << mk_pp(a_cat, m) << std::endl;); - - std::stack worklist; - zstring flattenedString(""); - bool constOK = true; - - { - app * arg0 = to_app(a_cat->get_arg(0)); - app * arg1 = to_app(a_cat->get_arg(1)); - - worklist.push(arg1); - worklist.push(arg0); + literal theory_str::mk_literal(expr* _e) { + ast_manager & m = get_manager(); + expr_ref e(_e, m); + context& ctx = get_context(); + ensure_enode(e); + return ctx.get_literal(e); } - while (constOK && !worklist.empty()) { - app * evalArg = worklist.top(); worklist.pop(); - zstring nextStr; - if (u.str.is_string(evalArg, nextStr)) { - flattenedString = flattenedString + nextStr; - } else if (u.str.is_concat(evalArg)) { - app * arg0 = to_app(evalArg->get_arg(0)); - app * arg1 = to_app(evalArg->get_arg(1)); + app * theory_str::mk_int(int n) { + return m_autil.mk_numeral(rational(n), true); + } - worklist.push(arg1); - worklist.push(arg0); - } else { - TRACE("str", tout << "non-constant term in concat -- giving up." << std::endl;); - constOK = false; - break; + app * theory_str::mk_int(rational & q) { + return m_autil.mk_numeral(q, true); + } + + expr * theory_str::mk_internal_lenTest_var(expr * node, int lTries) { + ast_manager & m = get_manager(); + + std::stringstream ss; + ss << "$$_len_" << mk_ismt2_pp(node, m) << "_" << lTries << "_" << tmpLenTestVarCount; + tmpLenTestVarCount += 1; + std::string name = ss.str(); + app * var = mk_str_var(name); + internal_lenTest_vars.insert(var); + m_trail.push_back(var); + return var; + } + + expr * theory_str::mk_internal_valTest_var(expr * node, int len, int vTries) { + ast_manager & m = get_manager(); + std::stringstream ss; + ss << "$$_val_" << mk_ismt2_pp(node, m) << "_" << len << "_" << vTries << "_" << tmpValTestVarCount; + tmpValTestVarCount += 1; + std::string name = ss.str(); + app * var = mk_str_var(name); + internal_valTest_vars.insert(var); + m_trail.push_back(var); + return var; + } + + void theory_str::track_variable_scope(expr * var) { + if (internal_variable_scope_levels.find(sLevel) == internal_variable_scope_levels.end()) { + internal_variable_scope_levels[sLevel] = std::set(); } - } - if (constOK) { - TRACE("str", tout << "flattened to \"" << flattenedString.encode().c_str() << "\"" << std::endl;); - expr_ref constStr(mk_string(flattenedString), m); - expr_ref axiom(ctx.mk_eq_atom(a_cat, constStr), m); - assert_axiom(axiom); - } -} - -/* - * Instantiate an axiom of the following form: - * Length(Concat(x, y)) = Length(x) + Length(y) - */ -void theory_str::instantiate_concat_axiom(enode * cat) { - app * a_cat = cat->get_owner(); - SASSERT(u.str.is_concat(a_cat)); - - ast_manager & m = get_manager(); - - TRACE("str", tout << "instantiating concat axiom for " << mk_ismt2_pp(a_cat, m) << std::endl;); - - // build LHS - expr_ref len_xy(m); - len_xy = mk_strlen(a_cat); - SASSERT(len_xy); - - // build RHS: start by extracting x and y from Concat(x, y) - unsigned nArgs = a_cat->get_num_args(); - SASSERT(nArgs == 2); - app * a_x = to_app(a_cat->get_arg(0)); - app * a_y = to_app(a_cat->get_arg(1)); - - expr_ref len_x(m); - len_x = mk_strlen(a_x); - SASSERT(len_x); - - expr_ref len_y(m); - len_y = mk_strlen(a_y); - SASSERT(len_y); - - // now build len_x + len_y - expr_ref len_x_plus_len_y(m); - len_x_plus_len_y = m_autil.mk_add(len_x, len_y); - SASSERT(len_x_plus_len_y); - - // finally assert equality between the two subexpressions - app * eq = m.mk_eq(len_xy, len_x_plus_len_y); - SASSERT(eq); - assert_axiom(eq); -} - -/* - * Add axioms that are true for any string variable: - * 1. Length(x) >= 0 - * 2. Length(x) == 0 <=> x == "" - * If the term is a string constant, we can assert something stronger: - * Length(x) == strlen(x) - */ -void theory_str::instantiate_basic_string_axioms(enode * str) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - TRACE("str", tout << "set up basic string axioms on " << mk_pp(str->get_owner(), m) << std::endl;); - - // TESTING: attempt to avoid a crash here when a variable goes out of scope - if (str->get_iscope_lvl() > ctx.get_scope_level()) { - TRACE("str", tout << "WARNING: skipping axiom setup on out-of-scope string term" << std::endl;); - return; + internal_variable_scope_levels[sLevel].insert(var); } - // generate a stronger axiom for constant strings - app * a_str = str->get_owner(); - if (u.str.is_string(a_str)) { - expr_ref len_str(m); - len_str = mk_strlen(a_str); - SASSERT(len_str); + app * theory_str::mk_internal_xor_var() { + return mk_int_var("$$_xor"); + } - zstring strconst; - u.str.is_string(str->get_owner(), strconst); - TRACE("str", tout << "instantiating constant string axioms for \"" << strconst.encode().c_str() << "\"" << std::endl;); - unsigned int l = strconst.length(); - expr_ref len(m_autil.mk_numeral(rational(l), true), m); + app * theory_str::mk_int_var(std::string name) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + TRACE("str", tout << "creating integer variable " << name << " at scope level " << sLevel << std::endl;); + + sort * int_sort = m.mk_sort(m_autil.get_family_id(), INT_SORT); + app * a = m.mk_fresh_const(name.c_str(), int_sort); + + ctx.internalize(a, false); + SASSERT(ctx.get_enode(a) != NULL); + SASSERT(ctx.e_internalized(a)); + ctx.mark_as_relevant(a); + // I'm assuming that this combination will do the correct thing in the integer theory. + + //mk_var(ctx.get_enode(a)); + m_trail.push_back(a); + //variable_set.insert(a); + //internal_variable_set.insert(a); + //track_variable_scope(a); + + return a; + } + + app * theory_str::mk_unroll_bound_var() { + return mk_int_var("unroll"); + } + + app * theory_str::mk_unroll_test_var() { + app * v = mk_str_var("unrollTest"); // was uRt + internal_unrollTest_vars.insert(v); + track_variable_scope(v); + return v; + } + + app * theory_str::mk_str_var(std::string name) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + TRACE("str", tout << "creating string variable " << name << " at scope level " << sLevel << std::endl;); + + sort * string_sort = u.str.mk_string_sort(); + app * a = m.mk_fresh_const(name.c_str(), string_sort); + + TRACE("str", tout << "a->get_family_id() = " << a->get_family_id() << std::endl + << "this->get_family_id() = " << this->get_family_id() << std::endl;); + + // I have a hunch that this may not get internalized for free... + ctx.internalize(a, false); + SASSERT(ctx.get_enode(a) != NULL); + SASSERT(ctx.e_internalized(a)); + // this might help?? + mk_var(ctx.get_enode(a)); + m_basicstr_axiom_todo.push_back(ctx.get_enode(a)); + TRACE("str", tout << "add " << mk_pp(a, m) << " to m_basicstr_axiom_todo" << std::endl;); + + m_trail.push_back(a); + variable_set.insert(a); + internal_variable_set.insert(a); + track_variable_scope(a); + + return a; + } + + app * theory_str::mk_regex_rep_var() { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + sort * string_sort = u.str.mk_string_sort(); + app * a = m.mk_fresh_const("regex", string_sort); + + ctx.internalize(a, false); + SASSERT(ctx.get_enode(a) != NULL); + SASSERT(ctx.e_internalized(a)); + mk_var(ctx.get_enode(a)); + m_basicstr_axiom_todo.push_back(ctx.get_enode(a)); + TRACE("str", tout << "add " << mk_pp(a, m) << " to m_basicstr_axiom_todo" << std::endl;); + + m_trail.push_back(a); + variable_set.insert(a); + //internal_variable_set.insert(a); + regex_variable_set.insert(a); + track_variable_scope(a); + + return a; + } + + void theory_str::add_nonempty_constraint(expr * s) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + expr_ref ax1(m.mk_not(ctx.mk_eq_atom(s, mk_string(""))), m); + assert_axiom(ax1); - literal lit(mk_eq(len_str, len, false)); - ctx.mark_as_relevant(lit); - ctx.mk_th_axiom(get_id(), 1, &lit); - } else { - // build axiom 1: Length(a_str) >= 0 { // build LHS - expr_ref len_str(m); - len_str = mk_strlen(a_str); + expr_ref len_str(mk_strlen(s), m); SASSERT(len_str); // build RHS - expr_ref zero(m); - zero = m_autil.mk_numeral(rational(0), true); + expr_ref zero(m_autil.mk_numeral(rational(0), true), m); SASSERT(zero); - // build LHS >= RHS and assert - app * lhs_ge_rhs = m_autil.mk_ge(len_str, zero); - SASSERT(lhs_ge_rhs); - TRACE("str", tout << "string axiom 1: " << mk_ismt2_pp(lhs_ge_rhs, m) << std::endl;); - assert_axiom(lhs_ge_rhs); + // build LHS > RHS and assert + // we have to build !(LHS <= RHS) instead + expr_ref lhs_gt_rhs(m.mk_not(m_autil.mk_le(len_str, zero)), m); + SASSERT(lhs_gt_rhs); + assert_axiom(lhs_gt_rhs); } + } - // build axiom 2: Length(a_str) == 0 <=> a_str == "" + app * theory_str::mk_nonempty_str_var() { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + std::stringstream ss; + ss << tmpStringVarCount; + tmpStringVarCount++; + std::string name = "$$_str" + ss.str(); + + TRACE("str", tout << "creating nonempty string variable " << name << " at scope level " << sLevel << std::endl;); + + sort * string_sort = u.str.mk_string_sort(); + app * a = m.mk_fresh_const(name.c_str(), string_sort); + + ctx.internalize(a, false); + SASSERT(ctx.get_enode(a) != NULL); + // this might help?? + mk_var(ctx.get_enode(a)); + + // assert a variation of the basic string axioms that ensures this string is nonempty { - // build LHS of iff - expr_ref len_str(m); - len_str = mk_strlen(a_str); + // build LHS + expr_ref len_str(mk_strlen(a), m); SASSERT(len_str); - expr_ref zero(m); - zero = m_autil.mk_numeral(rational(0), true); + // build RHS + expr_ref zero(m_autil.mk_numeral(rational(0), true), m); SASSERT(zero); - expr_ref lhs(m); - lhs = ctx.mk_eq_atom(len_str, zero); - SASSERT(lhs); - // build RHS of iff - expr_ref empty_str(m); - empty_str = mk_string(""); - SASSERT(empty_str); - expr_ref rhs(m); - rhs = ctx.mk_eq_atom(a_str, empty_str); - SASSERT(rhs); - // build LHS <=> RHS and assert - TRACE("str", tout << "string axiom 2: " << mk_ismt2_pp(lhs, m) << " <=> " << mk_ismt2_pp(rhs, m) << std::endl;); - literal l(mk_eq(lhs, rhs, true)); - ctx.mark_as_relevant(l); - ctx.mk_th_axiom(get_id(), 1, &l); + // build LHS > RHS and assert + // we have to build !(LHS <= RHS) instead + expr_ref lhs_gt_rhs(m.mk_not(m_autil.mk_le(len_str, zero)), m); + SASSERT(lhs_gt_rhs); + assert_axiom(lhs_gt_rhs); } - } -} + // add 'a' to variable sets, so we can keep track of it + m_trail.push_back(a); + variable_set.insert(a); + internal_variable_set.insert(a); + track_variable_scope(a); -/* - * Add an axiom of the form: - * (lhs == rhs) -> ( Length(lhs) == Length(rhs) ) - */ -void theory_str::instantiate_str_eq_length_axiom(enode * lhs, enode * rhs) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - app * a_lhs = lhs->get_owner(); - app * a_rhs = rhs->get_owner(); - - // build premise: (lhs == rhs) - expr_ref premise(ctx.mk_eq_atom(a_lhs, a_rhs), m); - - // build conclusion: ( Length(lhs) == Length(rhs) ) - expr_ref len_lhs(mk_strlen(a_lhs), m); - SASSERT(len_lhs); - expr_ref len_rhs(mk_strlen(a_rhs), m); - SASSERT(len_rhs); - expr_ref conclusion(ctx.mk_eq_atom(len_lhs, len_rhs), m); - - TRACE("str", tout << "string-eq length-eq axiom: " - << mk_ismt2_pp(premise, m) << " -> " << mk_ismt2_pp(conclusion, m) << std::endl;); - assert_implication(premise, conclusion); -} - -void theory_str::instantiate_axiom_CharAt(enode * e) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - app * expr = e->get_owner(); - if (axiomatized_terms.contains(expr)) { - TRACE("str", tout << "already set up CharAt axiom for " << mk_pp(expr, m) << std::endl;); - return; - } - axiomatized_terms.insert(expr); - - TRACE("str", tout << "instantiate CharAt axiom for " << mk_pp(expr, m) << std::endl;); - - expr_ref ts0(mk_str_var("ts0"), m); - expr_ref ts1(mk_str_var("ts1"), m); - expr_ref ts2(mk_str_var("ts2"), m); - - expr_ref cond(m.mk_and( - m_autil.mk_ge(expr->get_arg(1), mk_int(0)), - // REWRITE for arithmetic theory: - // m_autil.mk_lt(expr->get_arg(1), mk_strlen(expr->get_arg(0))) - m.mk_not(m_autil.mk_ge(m_autil.mk_add(expr->get_arg(1), m_autil.mk_mul(mk_int(-1), mk_strlen(expr->get_arg(0)))), mk_int(0))) - ), m); - - expr_ref_vector and_item(m); - and_item.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(ts0, mk_concat(ts1, ts2)))); - and_item.push_back(ctx.mk_eq_atom(expr->get_arg(1), mk_strlen(ts0))); - and_item.push_back(ctx.mk_eq_atom(mk_strlen(ts1), mk_int(1))); - - expr_ref thenBranch(m.mk_and(and_item.size(), and_item.c_ptr()), m); - expr_ref elseBranch(ctx.mk_eq_atom(ts1, mk_string("")), m); - - expr_ref axiom(m.mk_ite(cond, thenBranch, elseBranch), m); - expr_ref reductionVar(ctx.mk_eq_atom(expr, ts1), m); - - SASSERT(axiom); - SASSERT(reductionVar); - - expr_ref finalAxiom(m.mk_and(axiom, reductionVar), m); - SASSERT(finalAxiom); - assert_axiom(finalAxiom); -} - -void theory_str::instantiate_axiom_prefixof(enode * e) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - app * expr = e->get_owner(); - if (axiomatized_terms.contains(expr)) { - TRACE("str", tout << "already set up prefixof axiom for " << mk_pp(expr, m) << std::endl;); - return; - } - axiomatized_terms.insert(expr); - - TRACE("str", tout << "instantiate prefixof axiom for " << mk_pp(expr, m) << std::endl;); - - expr_ref ts0(mk_str_var("ts0"), m); - expr_ref ts1(mk_str_var("ts1"), m); - - expr_ref_vector innerItems(m); - innerItems.push_back(ctx.mk_eq_atom(expr->get_arg(1), mk_concat(ts0, ts1))); - innerItems.push_back(ctx.mk_eq_atom(mk_strlen(ts0), mk_strlen(expr->get_arg(0)))); - innerItems.push_back(m.mk_ite(ctx.mk_eq_atom(ts0, expr->get_arg(0)), expr, m.mk_not(expr))); - expr_ref then1(m.mk_and(innerItems.size(), innerItems.c_ptr()), m); - SASSERT(then1); - - // the top-level condition is Length(arg0) >= Length(arg1) - expr_ref topLevelCond( - m_autil.mk_ge( - m_autil.mk_add( - mk_strlen(expr->get_arg(1)), m_autil.mk_mul(mk_int(-1), mk_strlen(expr->get_arg(0)))), - mk_int(0)) - , m); - SASSERT(topLevelCond); - - expr_ref finalAxiom(m.mk_ite(topLevelCond, then1, m.mk_not(expr)), m); - SASSERT(finalAxiom); - assert_axiom(finalAxiom); -} - -void theory_str::instantiate_axiom_suffixof(enode * e) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - app * expr = e->get_owner(); - if (axiomatized_terms.contains(expr)) { - TRACE("str", tout << "already set up suffixof axiom for " << mk_pp(expr, m) << std::endl;); - return; - } - axiomatized_terms.insert(expr); - - TRACE("str", tout << "instantiate suffixof axiom for " << mk_pp(expr, m) << std::endl;); - - expr_ref ts0(mk_str_var("ts0"), m); - expr_ref ts1(mk_str_var("ts1"), m); - - expr_ref_vector innerItems(m); - innerItems.push_back(ctx.mk_eq_atom(expr->get_arg(1), mk_concat(ts0, ts1))); - innerItems.push_back(ctx.mk_eq_atom(mk_strlen(ts1), mk_strlen(expr->get_arg(0)))); - innerItems.push_back(m.mk_ite(ctx.mk_eq_atom(ts1, expr->get_arg(0)), expr, m.mk_not(expr))); - expr_ref then1(m.mk_and(innerItems.size(), innerItems.c_ptr()), m); - SASSERT(then1); - - // the top-level condition is Length(arg0) >= Length(arg1) - expr_ref topLevelCond( - m_autil.mk_ge( - m_autil.mk_add( - mk_strlen(expr->get_arg(1)), m_autil.mk_mul(mk_int(-1), mk_strlen(expr->get_arg(0)))), - mk_int(0)) - , m); - SASSERT(topLevelCond); - - expr_ref finalAxiom(m.mk_ite(topLevelCond, then1, m.mk_not(expr)), m); - SASSERT(finalAxiom); - assert_axiom(finalAxiom); -} - -void theory_str::instantiate_axiom_Contains(enode * e) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - app * ex = e->get_owner(); - if (axiomatized_terms.contains(ex)) { - TRACE("str", tout << "already set up Contains axiom for " << mk_pp(ex, m) << std::endl;); - return; - } - axiomatized_terms.insert(ex); - - // quick path, because this is necessary due to rewriter behaviour - // at minimum it should fix z3str/concat-006.smt2 - zstring haystackStr, needleStr; - if (u.str.is_string(ex->get_arg(0), haystackStr) && u.str.is_string(ex->get_arg(1), needleStr)) { - TRACE("str", tout << "eval constant Contains term " << mk_pp(ex, m) << std::endl;); - if (haystackStr.contains(needleStr)) { - assert_axiom(ex); - } else { - assert_axiom(m.mk_not(ex)); - } - return; + return a; } - { // register Contains() - expr * str = ex->get_arg(0); - expr * substr = ex->get_arg(1); - contains_map.push_back(ex); - std::pair key = std::pair(str, substr); - contain_pair_bool_map.insert(str, substr, ex); - contain_pair_idx_map[str].insert(key); - contain_pair_idx_map[substr].insert(key); - } + app * theory_str::mk_unroll(expr * n, expr * bound) { + context & ctx = get_context(); + ast_manager & m = get_manager(); - TRACE("str", tout << "instantiate Contains axiom for " << mk_pp(ex, m) << std::endl;); + expr * args[2] = {n, bound}; + app * unrollFunc = get_manager().mk_app(get_id(), _OP_RE_UNROLL, 0, 0, 2, args); + m_trail.push_back(unrollFunc); - expr_ref ts0(mk_str_var("ts0"), m); - expr_ref ts1(mk_str_var("ts1"), m); + expr_ref_vector items(m); + items.push_back(ctx.mk_eq_atom(ctx.mk_eq_atom(bound, mk_int(0)), ctx.mk_eq_atom(unrollFunc, mk_string("")))); + items.push_back(m_autil.mk_ge(bound, mk_int(0))); + items.push_back(m_autil.mk_ge(mk_strlen(unrollFunc), mk_int(0))); - expr_ref breakdownAssert(ctx.mk_eq_atom(ex, ctx.mk_eq_atom(ex->get_arg(0), mk_concat(ts0, mk_concat(ex->get_arg(1), ts1)))), m); - SASSERT(breakdownAssert); - assert_axiom(breakdownAssert); -} - -void theory_str::instantiate_axiom_Indexof(enode * e) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - app * expr = e->get_owner(); - if (axiomatized_terms.contains(expr)) { - TRACE("str", tout << "already set up Indexof axiom for " << mk_pp(expr, m) << std::endl;); - return; - } - axiomatized_terms.insert(expr); - - TRACE("str", tout << "instantiate Indexof axiom for " << mk_pp(expr, m) << std::endl;); - - expr_ref x1(mk_str_var("x1"), m); - expr_ref x2(mk_str_var("x2"), m); - expr_ref indexAst(mk_int_var("index"), m); - - expr_ref condAst(mk_contains(expr->get_arg(0), expr->get_arg(1)), m); - SASSERT(condAst); - - // ----------------------- - // true branch - expr_ref_vector thenItems(m); - // args[0] = x1 . args[1] . x2 - thenItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x1, mk_concat(expr->get_arg(1), x2)))); - // indexAst = |x1| - thenItems.push_back(ctx.mk_eq_atom(indexAst, mk_strlen(x1))); - // args[0] = x3 . x4 - // /\ |x3| = |x1| + |args[1]| - 1 - // /\ ! contains(x3, args[1]) - expr_ref x3(mk_str_var("x3"), m); - expr_ref x4(mk_str_var("x4"), m); - expr_ref tmpLen(m_autil.mk_add(indexAst, mk_strlen(expr->get_arg(1)), mk_int(-1)), m); - SASSERT(tmpLen); - thenItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x3, x4))); - thenItems.push_back(ctx.mk_eq_atom(mk_strlen(x3), tmpLen)); - thenItems.push_back(m.mk_not(mk_contains(x3, expr->get_arg(1)))); - expr_ref thenBranch(m.mk_and(thenItems.size(), thenItems.c_ptr()), m); - SASSERT(thenBranch); - - // ----------------------- - // false branch - expr_ref elseBranch(ctx.mk_eq_atom(indexAst, mk_int(-1)), m); - SASSERT(elseBranch); - - expr_ref breakdownAssert(m.mk_ite(condAst, thenBranch, elseBranch), m); - SASSERT(breakdownAssert); - - expr_ref reduceToIndex(ctx.mk_eq_atom(expr, indexAst), m); - SASSERT(reduceToIndex); - - expr_ref finalAxiom(m.mk_and(breakdownAssert, reduceToIndex), m); - SASSERT(finalAxiom); - assert_axiom(finalAxiom); -} - -void theory_str::instantiate_axiom_Indexof2(enode * e) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - app * expr = e->get_owner(); - if (axiomatized_terms.contains(expr)) { - TRACE("str", tout << "already set up Indexof2 axiom for " << mk_pp(expr, m) << std::endl;); - return; - } - axiomatized_terms.insert(expr); - - TRACE("str", tout << "instantiate Indexof2 axiom for " << mk_pp(expr, m) << std::endl;); - - // ------------------------------------------------------------------------------- - // if (arg[2] >= length(arg[0])) // ite2 - // resAst = -1 - // else - // args[0] = prefix . suffix - // /\ indexAst = indexof(suffix, arg[1]) - // /\ args[2] = len(prefix) - // /\ if (indexAst == -1) resAst = indexAst // ite3 - // else resAst = args[2] + indexAst - // ------------------------------------------------------------------------------- - - expr_ref resAst(mk_int_var("res"), m); - expr_ref indexAst(mk_int_var("index"), m); - expr_ref prefix(mk_str_var("prefix"), m); - expr_ref suffix(mk_str_var("suffix"), m); - expr_ref prefixLen(mk_strlen(prefix), m); - expr_ref zeroAst(mk_int(0), m); - expr_ref negOneAst(mk_int(-1), m); - - expr_ref ite3(m.mk_ite( - ctx.mk_eq_atom(indexAst, negOneAst), - ctx.mk_eq_atom(resAst, negOneAst), - ctx.mk_eq_atom(resAst, m_autil.mk_add(expr->get_arg(2), indexAst)) - ),m); - - expr_ref_vector ite2ElseItems(m); - ite2ElseItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(prefix, suffix))); - ite2ElseItems.push_back(ctx.mk_eq_atom(indexAst, mk_indexof(suffix, expr->get_arg(1)))); - ite2ElseItems.push_back(ctx.mk_eq_atom(expr->get_arg(2), prefixLen)); - ite2ElseItems.push_back(ite3); - expr_ref ite2Else(m.mk_and(ite2ElseItems.size(), ite2ElseItems.c_ptr()), m); - SASSERT(ite2Else); - - expr_ref ite2(m.mk_ite( - //m_autil.mk_ge(expr->get_arg(2), mk_strlen(expr->get_arg(0))), - m_autil.mk_ge(m_autil.mk_add(expr->get_arg(2), m_autil.mk_mul(mk_int(-1), mk_strlen(expr->get_arg(0)))), zeroAst), - ctx.mk_eq_atom(resAst, negOneAst), - ite2Else - ), m); - SASSERT(ite2); - - expr_ref ite1(m.mk_ite( - //m_autil.mk_lt(expr->get_arg(2), zeroAst), - m.mk_not(m_autil.mk_ge(expr->get_arg(2), zeroAst)), - ctx.mk_eq_atom(resAst, mk_indexof(expr->get_arg(0), expr->get_arg(1))), - ite2 - ), m); - SASSERT(ite1); - assert_axiom(ite1); - - expr_ref reduceTerm(ctx.mk_eq_atom(expr, resAst), m); - SASSERT(reduceTerm); - assert_axiom(reduceTerm); -} - -void theory_str::instantiate_axiom_LastIndexof(enode * e) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - app * expr = e->get_owner(); - if (axiomatized_terms.contains(expr)) { - TRACE("str", tout << "already set up LastIndexof axiom for " << mk_pp(expr, m) << std::endl;); - return; - } - axiomatized_terms.insert(expr); - - TRACE("str", tout << "instantiate LastIndexof axiom for " << mk_pp(expr, m) << std::endl;); - - expr_ref x1(mk_str_var("x1"), m); - expr_ref x2(mk_str_var("x2"), m); - expr_ref indexAst(mk_int_var("index"), m); - expr_ref_vector items(m); - - // args[0] = x1 . args[1] . x2 - expr_ref eq1(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x1, mk_concat(expr->get_arg(1), x2))), m); - expr_ref arg0HasArg1(mk_contains(expr->get_arg(0), expr->get_arg(1)), m); // arg0HasArg1 = Contains(args[0], args[1]) - items.push_back(ctx.mk_eq_atom(arg0HasArg1, eq1)); - - - expr_ref condAst(arg0HasArg1, m); - //---------------------------- - // true branch - expr_ref_vector thenItems(m); - thenItems.push_back(m_autil.mk_ge(indexAst, mk_int(0))); - // args[0] = x1 . args[1] . x2 - // x1 doesn't contain args[1] - thenItems.push_back(m.mk_not(mk_contains(x2, expr->get_arg(1)))); - thenItems.push_back(ctx.mk_eq_atom(indexAst, mk_strlen(x1))); - - bool canSkip = false; - zstring arg1Str; - if (u.str.is_string(expr->get_arg(1), arg1Str)) { - if (arg1Str.length() == 1) { - canSkip = true; - } - } - - if (!canSkip) { - // args[0] = x3 . x4 /\ |x3| = |x1| + 1 /\ ! contains(x4, args[1]) - expr_ref x3(mk_str_var("x3"), m); - expr_ref x4(mk_str_var("x4"), m); - expr_ref tmpLen(m_autil.mk_add(indexAst, mk_int(1)), m); - thenItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x3, x4))); - thenItems.push_back(ctx.mk_eq_atom(mk_strlen(x3), tmpLen)); - thenItems.push_back(m.mk_not(mk_contains(x4, expr->get_arg(1)))); - } - //---------------------------- - // else branch - expr_ref_vector elseItems(m); - elseItems.push_back(ctx.mk_eq_atom(indexAst, mk_int(-1))); - - items.push_back(m.mk_ite(condAst, m.mk_and(thenItems.size(), thenItems.c_ptr()), m.mk_and(elseItems.size(), elseItems.c_ptr()))); - - expr_ref breakdownAssert(m.mk_and(items.size(), items.c_ptr()), m); - SASSERT(breakdownAssert); - - expr_ref reduceToIndex(ctx.mk_eq_atom(expr, indexAst), m); - SASSERT(reduceToIndex); - - expr_ref finalAxiom(m.mk_and(breakdownAssert, reduceToIndex), m); - SASSERT(finalAxiom); - assert_axiom(finalAxiom); -} - -void theory_str::instantiate_axiom_Substr(enode * e) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - app * expr = e->get_owner(); - if (axiomatized_terms.contains(expr)) { - TRACE("str", tout << "already set up Substr axiom for " << mk_pp(expr, m) << std::endl;); - return; - } - axiomatized_terms.insert(expr); - - TRACE("str", tout << "instantiate Substr axiom for " << mk_pp(expr, m) << std::endl;); - - expr_ref substrBase(expr->get_arg(0), m); - expr_ref substrPos(expr->get_arg(1), m); - expr_ref substrLen(expr->get_arg(2), m); - SASSERT(substrBase); - SASSERT(substrPos); - SASSERT(substrLen); - - expr_ref zero(m_autil.mk_numeral(rational::zero(), true), m); - expr_ref minusOne(m_autil.mk_numeral(rational::minus_one(), true), m); - SASSERT(zero); - SASSERT(minusOne); - - expr_ref_vector argumentsValid_terms(m); - // pos >= 0 - argumentsValid_terms.push_back(m_autil.mk_ge(substrPos, zero)); - // pos < strlen(base) - // --> pos + -1*strlen(base) < 0 - argumentsValid_terms.push_back(m.mk_not(m_autil.mk_ge( - m_autil.mk_add(substrPos, m_autil.mk_mul(minusOne, substrLen)), - zero))); - // len >= 0 - argumentsValid_terms.push_back(m_autil.mk_ge(substrLen, zero)); - - expr_ref argumentsValid(mk_and(argumentsValid_terms), m); - SASSERT(argumentsValid); - ctx.internalize(argumentsValid, false); - - // (pos+len) >= strlen(base) - // --> pos + len + -1*strlen(base) >= 0 - expr_ref lenOutOfBounds(m_autil.mk_ge( - m_autil.mk_add(substrPos, substrLen, m_autil.mk_mul(minusOne, mk_strlen(substrBase))), - zero), m); - SASSERT(lenOutOfBounds); - ctx.internalize(argumentsValid, false); - - // Case 1: pos < 0 or pos >= strlen(base) or len < 0 - // ==> (Substr ...) = "" - expr_ref case1_premise(m.mk_not(argumentsValid), m); - SASSERT(case1_premise); - ctx.internalize(case1_premise, false); - expr_ref case1_conclusion(ctx.mk_eq_atom(expr, mk_string("")), m); - SASSERT(case1_conclusion); - ctx.internalize(case1_conclusion, false); - expr_ref case1(rewrite_implication(case1_premise, case1_conclusion), m); - SASSERT(case1); - - // Case 2: (pos >= 0 and pos < strlen(base) and len >= 0) and (pos+len) >= strlen(base) - // ==> base = t0.t1 AND len(t0) = pos AND (Substr ...) = t1 - expr_ref t0(mk_str_var("t0"), m); - expr_ref t1(mk_str_var("t1"), m); - expr_ref case2_conclusion(m.mk_and( - ctx.mk_eq_atom(substrBase, mk_concat(t0,t1)), - ctx.mk_eq_atom(mk_strlen(t0), substrPos), - ctx.mk_eq_atom(expr, t1)), m); - expr_ref case2(rewrite_implication(m.mk_and(argumentsValid, lenOutOfBounds), case2_conclusion), m); - SASSERT(case2); - - // Case 3: (pos >= 0 and pos < strlen(base) and len >= 0) and (pos+len) < strlen(base) - // ==> base = t2.t3.t4 AND len(t2) = pos AND len(t3) = len AND (Substr ...) = t3 - expr_ref t2(mk_str_var("t2"), m); - expr_ref t3(mk_str_var("t3"), m); - expr_ref t4(mk_str_var("t4"), m); - expr_ref_vector case3_conclusion_terms(m); - case3_conclusion_terms.push_back(ctx.mk_eq_atom(substrBase, mk_concat(t2, mk_concat(t3, t4)))); - case3_conclusion_terms.push_back(ctx.mk_eq_atom(mk_strlen(t2), substrPos)); - case3_conclusion_terms.push_back(ctx.mk_eq_atom(mk_strlen(t3), substrLen)); - case3_conclusion_terms.push_back(ctx.mk_eq_atom(expr, t3)); - expr_ref case3_conclusion(mk_and(case3_conclusion_terms), m); - expr_ref case3(rewrite_implication(m.mk_and(argumentsValid, m.mk_not(lenOutOfBounds)), case3_conclusion), m); - SASSERT(case3); - - ctx.internalize(case1, false); - ctx.internalize(case2, false); - ctx.internalize(case3, false); - - expr_ref finalAxiom(m.mk_and(case1, case2, case3), m); - SASSERT(finalAxiom); - assert_axiom(finalAxiom); -} - -void theory_str::instantiate_axiom_Replace(enode * e) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - app * expr = e->get_owner(); - if (axiomatized_terms.contains(expr)) { - TRACE("str", tout << "already set up Replace axiom for " << mk_pp(expr, m) << std::endl;); - return; - } - axiomatized_terms.insert(expr); - - TRACE("str", tout << "instantiate Replace axiom for " << mk_pp(expr, m) << std::endl;); - - expr_ref x1(mk_str_var("x1"), m); - expr_ref x2(mk_str_var("x2"), m); - expr_ref i1(mk_int_var("i1"), m); - expr_ref result(mk_str_var("result"), m); - - // condAst = Contains(args[0], args[1]) - expr_ref condAst(mk_contains(expr->get_arg(0), expr->get_arg(1)), m); - // ----------------------- - // true branch - expr_ref_vector thenItems(m); - // args[0] = x1 . args[1] . x2 - thenItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x1, mk_concat(expr->get_arg(1), x2)))); - // i1 = |x1| - thenItems.push_back(ctx.mk_eq_atom(i1, mk_strlen(x1))); - // args[0] = x3 . x4 /\ |x3| = |x1| + |args[1]| - 1 /\ ! contains(x3, args[1]) - expr_ref x3(mk_str_var("x3"), m); - expr_ref x4(mk_str_var("x4"), m); - expr_ref tmpLen(m_autil.mk_add(i1, mk_strlen(expr->get_arg(1)), mk_int(-1)), m); - thenItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x3, x4))); - thenItems.push_back(ctx.mk_eq_atom(mk_strlen(x3), tmpLen)); - thenItems.push_back(m.mk_not(mk_contains(x3, expr->get_arg(1)))); - thenItems.push_back(ctx.mk_eq_atom(result, mk_concat(x1, mk_concat(expr->get_arg(2), x2)))); - // ----------------------- - // false branch - expr_ref elseBranch(ctx.mk_eq_atom(result, expr->get_arg(0)), m); - - expr_ref breakdownAssert(m.mk_ite(condAst, m.mk_and(thenItems.size(), thenItems.c_ptr()), elseBranch), m); - SASSERT(breakdownAssert); - - expr_ref reduceToResult(ctx.mk_eq_atom(expr, result), m); - SASSERT(reduceToResult); - - expr_ref finalAxiom(m.mk_and(breakdownAssert, reduceToResult), m); - SASSERT(finalAxiom); - assert_axiom(finalAxiom); -} - -void theory_str::instantiate_axiom_str_to_int(enode * e) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - app * ex = e->get_owner(); - if (axiomatized_terms.contains(ex)) { - TRACE("str", tout << "already set up str.to-int axiom for " << mk_pp(ex, m) << std::endl;); - return; - } - axiomatized_terms.insert(ex); - - TRACE("str", tout << "instantiate str.to-int axiom for " << mk_pp(ex, m) << std::endl;); - - // let expr = (str.to-int S) - // axiom 1: expr >= -1 - // axiom 2: expr = 0 <==> S = "0" - // axiom 3: expr >= 1 ==> len(S) > 0 AND S[0] != "0" - - expr * S = ex->get_arg(0); - { - expr_ref axiom1(m_autil.mk_ge(ex, m_autil.mk_numeral(rational::minus_one(), true)), m); - SASSERT(axiom1); - assert_axiom(axiom1); - } - - { - expr_ref lhs(ctx.mk_eq_atom(ex, m_autil.mk_numeral(rational::zero(), true)), m); - expr_ref rhs(ctx.mk_eq_atom(S, mk_string("0")), m); - expr_ref axiom2(ctx.mk_eq_atom(lhs, rhs), m); - SASSERT(axiom2); - assert_axiom(axiom2); - } - - { - expr_ref premise(m_autil.mk_ge(ex, m_autil.mk_numeral(rational::one(), true)), m); - expr_ref hd(mk_str_var("hd"), m); - expr_ref tl(mk_str_var("tl"), m); - expr_ref conclusion1(ctx.mk_eq_atom(S, mk_concat(hd, tl)), m); - expr_ref conclusion2(ctx.mk_eq_atom(mk_strlen(hd), m_autil.mk_numeral(rational::one(), true)), m); - expr_ref conclusion3(m.mk_not(ctx.mk_eq_atom(hd, mk_string("0"))), m); - expr_ref conclusion(m.mk_and(conclusion1, conclusion2, conclusion3), m); - SASSERT(premise); - SASSERT(conclusion); - assert_implication(premise, conclusion); - } -} - -void theory_str::instantiate_axiom_int_to_str(enode * e) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - app * ex = e->get_owner(); - if (axiomatized_terms.contains(ex)) { - TRACE("str", tout << "already set up str.from-int axiom for " << mk_pp(ex, m) << std::endl;); - return; - } - axiomatized_terms.insert(ex); - - TRACE("str", tout << "instantiate str.from-int axiom for " << mk_pp(ex, m) << std::endl;); - - // axiom 1: N < 0 <==> (str.from-int N) = "" - expr * N = ex->get_arg(0); - { - expr_ref axiom1_lhs(m.mk_not(m_autil.mk_ge(N, m_autil.mk_numeral(rational::zero(), true))), m); - expr_ref axiom1_rhs(ctx.mk_eq_atom(ex, mk_string("")), m); - expr_ref axiom1(ctx.mk_eq_atom(axiom1_lhs, axiom1_rhs), m); - SASSERT(axiom1); - assert_axiom(axiom1); - } -} - -expr * theory_str::mk_RegexIn(expr * str, expr * regexp) { - app * regexIn = u.re.mk_in_re(str, regexp); - // immediately force internalization so that axiom setup does not fail - get_context().internalize(regexIn, false); - set_up_axioms(regexIn); - return regexIn; -} - -static zstring str2RegexStr(zstring str) { - zstring res(""); - int len = str.length(); - for (int i = 0; i < len; i++) { - char nc = str[i]; - // 12 special chars - if (nc == '\\' || nc == '^' || nc == '$' || nc == '.' || nc == '|' || nc == '?' - || nc == '*' || nc == '+' || nc == '(' || nc == ')' || nc == '[' || nc == '{') { - res = res + zstring("\\"); - } - char tmp[2] = {(char)str[i], '\0'}; - res = res + zstring(tmp); - } - return res; -} - -zstring theory_str::get_std_regex_str(expr * regex) { - app * a_regex = to_app(regex); - if (u.re.is_to_re(a_regex)) { - expr * regAst = a_regex->get_arg(0); - zstring regAstVal; - u.str.is_string(regAst, regAstVal); - zstring regStr = str2RegexStr(regAstVal); - return regStr; - } else if (u.re.is_concat(a_regex)) { - expr * reg1Ast = a_regex->get_arg(0); - expr * reg2Ast = a_regex->get_arg(1); - zstring reg1Str = get_std_regex_str(reg1Ast); - zstring reg2Str = get_std_regex_str(reg2Ast); - return zstring("(") + reg1Str + zstring(")(") + reg2Str + zstring(")"); - } else if (u.re.is_union(a_regex)) { - expr * reg1Ast = a_regex->get_arg(0); - expr * reg2Ast = a_regex->get_arg(1); - zstring reg1Str = get_std_regex_str(reg1Ast); - zstring reg2Str = get_std_regex_str(reg2Ast); - return zstring("(") + reg1Str + zstring(")|(") + reg2Str + zstring(")"); - } else if (u.re.is_star(a_regex)) { - expr * reg1Ast = a_regex->get_arg(0); - zstring reg1Str = get_std_regex_str(reg1Ast); - return zstring("(") + reg1Str + zstring(")*"); - } else if (u.re.is_range(a_regex)) { - expr * range1 = a_regex->get_arg(0); - expr * range2 = a_regex->get_arg(1); - zstring range1val, range2val; - u.str.is_string(range1, range1val); - u.str.is_string(range2, range2val); - return zstring("[") + range1val + zstring("-") + range2val + zstring("]"); - } else { - TRACE("str", tout << "BUG: unrecognized regex term " << mk_pp(regex, get_manager()) << std::endl;); - UNREACHABLE(); return zstring(""); - } -} - -void theory_str::instantiate_axiom_RegexIn(enode * e) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - app * ex = e->get_owner(); - if (axiomatized_terms.contains(ex)) { - TRACE("str", tout << "already set up RegexIn axiom for " << mk_pp(ex, m) << std::endl;); - return; - } - axiomatized_terms.insert(ex); - - TRACE("str", tout << "instantiate RegexIn axiom for " << mk_pp(ex, m) << std::endl;); - - { - zstring regexStr = get_std_regex_str(ex->get_arg(1)); - std::pair key1(ex->get_arg(0), regexStr); - // skip Z3str's map check, because we already check if we set up axioms on this term - regex_in_bool_map[key1] = ex; - regex_in_var_reg_str_map[ex->get_arg(0)].insert(regexStr); - } - - expr_ref str(ex->get_arg(0), m); - app * regex = to_app(ex->get_arg(1)); - - if (u.re.is_to_re(regex)) { - expr_ref rxStr(regex->get_arg(0), m); - // want to assert 'expr IFF (str == rxStr)' - expr_ref rhs(ctx.mk_eq_atom(str, rxStr), m); - expr_ref finalAxiom(m.mk_iff(ex, rhs), m); - SASSERT(finalAxiom); - assert_axiom(finalAxiom); - TRACE("str", tout << "set up Str2Reg: (RegexIn " << mk_pp(str, m) << " " << mk_pp(regex, m) << ")" << std::endl;); - } else if (u.re.is_concat(regex)) { - expr_ref var1(mk_regex_rep_var(), m); - expr_ref var2(mk_regex_rep_var(), m); - expr_ref rhs(mk_concat(var1, var2), m); - expr_ref rx1(regex->get_arg(0), m); - expr_ref rx2(regex->get_arg(1), m); - expr_ref var1InRegex1(mk_RegexIn(var1, rx1), m); - expr_ref var2InRegex2(mk_RegexIn(var2, rx2), m); - - expr_ref_vector items(m); - items.push_back(var1InRegex1); - items.push_back(var2InRegex2); - items.push_back(ctx.mk_eq_atom(ex, ctx.mk_eq_atom(str, rhs))); - - expr_ref finalAxiom(mk_and(items), m); - SASSERT(finalAxiom); - assert_axiom(finalAxiom); - } else if (u.re.is_union(regex)) { - expr_ref var1(mk_regex_rep_var(), m); - expr_ref var2(mk_regex_rep_var(), m); - expr_ref orVar(m.mk_or(ctx.mk_eq_atom(str, var1), ctx.mk_eq_atom(str, var2)), m); - expr_ref regex1(regex->get_arg(0), m); - expr_ref regex2(regex->get_arg(1), m); - expr_ref var1InRegex1(mk_RegexIn(var1, regex1), m); - expr_ref var2InRegex2(mk_RegexIn(var2, regex2), m); - expr_ref_vector items(m); - items.push_back(var1InRegex1); - items.push_back(var2InRegex2); - items.push_back(ctx.mk_eq_atom(ex, orVar)); - assert_axiom(mk_and(items)); - } else if (u.re.is_star(regex)) { - // slightly more complex due to the unrolling step. - expr_ref regex1(regex->get_arg(0), m); - expr_ref unrollCount(mk_unroll_bound_var(), m); - expr_ref unrollFunc(mk_unroll(regex1, unrollCount), m); - expr_ref_vector items(m); - items.push_back(ctx.mk_eq_atom(ex, ctx.mk_eq_atom(str, unrollFunc))); - items.push_back(ctx.mk_eq_atom(ctx.mk_eq_atom(unrollCount, mk_int(0)), ctx.mk_eq_atom(unrollFunc, mk_string("")))); - expr_ref finalAxiom(mk_and(items), m); - SASSERT(finalAxiom); - assert_axiom(finalAxiom); - } else if (u.re.is_range(regex)) { - // (re.range "A" "Z") unfolds to (re.union "A" "B" ... "Z"); - // we rewrite to expr IFF (str = "A" or str = "B" or ... or str = "Z") - expr_ref lo(regex->get_arg(0), m); - expr_ref hi(regex->get_arg(1), m); - zstring str_lo, str_hi; - SASSERT(u.str.is_string(lo)); - SASSERT(u.str.is_string(hi)); - u.str.is_string(lo, str_lo); - u.str.is_string(hi, str_hi); - SASSERT(str_lo.length() == 1); - SASSERT(str_hi.length() == 1); - unsigned int c1 = str_lo[0]; - unsigned int c2 = str_hi[0]; - if (c1 > c2) { - // exchange - unsigned int tmp = c1; - c1 = c2; - c2 = tmp; - } - expr_ref_vector range_cases(m); - for (unsigned int ch = c1; ch <= c2; ++ch) { - zstring s_ch(ch); - expr_ref rhs(ctx.mk_eq_atom(str, u.str.mk_string(s_ch)), m); - range_cases.push_back(rhs); - } - expr_ref rhs(mk_or(range_cases), m); - expr_ref finalAxiom(m.mk_iff(ex, rhs), m); + expr_ref finalAxiom(mk_and(items), m); SASSERT(finalAxiom); assert_axiom(finalAxiom); - } else { - TRACE("str", tout << "ERROR: unknown regex expression " << mk_pp(regex, m) << "!" << std::endl;); - NOT_IMPLEMENTED_YET(); - } -} - -void theory_str::attach_new_th_var(enode * n) { - context & ctx = get_context(); - theory_var v = mk_var(n); - ctx.attach_th_var(n, this, v); - TRACE("str", tout << "new theory var: " << mk_ismt2_pp(n->get_owner(), get_manager()) << " := v#" << v << std::endl;); -} - -void theory_str::reset_eh() { - TRACE("str", tout << "resetting" << std::endl;); - m_trail_stack.reset(); - - m_basicstr_axiom_todo.reset(); - m_str_eq_todo.reset(); - m_concat_axiom_todo.reset(); - pop_scope_eh(get_context().get_scope_level()); -} - -/* - * Check equality among equivalence class members of LHS and RHS - * to discover an incorrect LHS == RHS. - * For example, if we have y2 == "str3" - * and the equivalence classes are - * { y2, (Concat ce m2) } - * { "str3", (Concat abc x2) } - * then y2 can't be equal to "str3". - * Then add an assertion: (y2 == (Concat ce m2)) AND ("str3" == (Concat abc x2)) -> (y2 != "str3") - */ -bool theory_str::new_eq_check(expr * lhs, expr * rhs) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - // skip this check if we defer consistency checking, as we can do it for every EQC in final check - if (!opt_DeferEQCConsistencyCheck) { - check_concat_len_in_eqc(lhs); - check_concat_len_in_eqc(rhs); + return unrollFunc; } - // Now we iterate over all pairs of terms across both EQCs - // and check whether we can show that any pair of distinct terms - // cannot possibly be equal. - // If that's the case, we assert an axiom to that effect and stop. + app * theory_str::mk_contains(expr * haystack, expr * needle) { + app * contains = u.str.mk_contains(haystack, needle); // TODO double-check semantics/argument order + m_trail.push_back(contains); + // immediately force internalization so that axiom setup does not fail + get_context().internalize(contains, false); + set_up_axioms(contains); + return contains; + } - expr * eqc_nn1 = lhs; - do { - expr * eqc_nn2 = rhs; - do { - TRACE("str", tout << "checking whether " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " can be equal" << std::endl;); - // inconsistency check: value - if (!can_two_nodes_eq(eqc_nn1, eqc_nn2)) { - TRACE("str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " cannot be equal to " << mk_pp(eqc_nn2, m) << std::endl;); - expr_ref to_assert(m.mk_not(ctx.mk_eq_atom(eqc_nn1, eqc_nn2)), m); - assert_axiom(to_assert); - // this shouldn't use the integer theory at all, so we don't allow the option of quick-return - return false; - } - if (!check_length_consistency(eqc_nn1, eqc_nn2)) { - TRACE("str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " have inconsistent lengths" << std::endl;); - if (opt_NoQuickReturn_IntegerTheory){ - TRACE("str", tout << "continuing in new_eq_check() due to opt_NoQuickReturn_IntegerTheory" << std::endl;); - } else { - return false; - } - } - eqc_nn2 = get_eqc_next(eqc_nn2); - } while (eqc_nn2 != rhs); - eqc_nn1 = get_eqc_next(eqc_nn1); - } while (eqc_nn1 != lhs); - - if (!contains_map.empty()) { - check_contain_in_new_eq(lhs, rhs); - } - - if (!regex_in_bool_map.empty()) { - TRACE("str", tout << "checking regex consistency" << std::endl;); - check_regex_in(lhs, rhs); - } - - // okay, all checks here passed - return true; -} - -// support for user_smt_theory-style EQC handling - -app * theory_str::get_ast(theory_var i) { - return get_enode(i)->get_owner(); -} - -theory_var theory_str::get_var(expr * n) const { - if (!is_app(n)) { - return null_theory_var; - } - context & ctx = get_context(); - if (ctx.e_internalized(to_app(n))) { - enode * e = ctx.get_enode(to_app(n)); - return e->get_th_var(get_id()); - } - return null_theory_var; -} - -// simulate Z3_theory_get_eqc_next() -expr * theory_str::get_eqc_next(expr * n) { - theory_var v = get_var(n); - if (v != null_theory_var) { - theory_var r = m_find.next(v); - return get_ast(r); - } - return n; -} - -void theory_str::group_terms_by_eqc(expr * n, std::set & concats, std::set & vars, std::set & consts) { - context & ctx = get_context(); - expr * eqcNode = n; - do { - app * ast = to_app(eqcNode); - if (u.str.is_concat(ast)) { - expr * simConcat = simplify_concat(ast); - if (simConcat != ast) { - if (u.str.is_concat(to_app(simConcat))) { - concats.insert(simConcat); - } else { - if (u.str.is_string(simConcat)) { - consts.insert(simConcat); - } else { - vars.insert(simConcat); - } + app * theory_str::mk_indexof(expr * haystack, expr * needle) { + // TODO check meaning of the third argument here + app * indexof = u.str.mk_index(haystack, needle, mk_int(0)); + m_trail.push_back(indexof); + // immediately force internalization so that axiom setup does not fail + get_context().internalize(indexof, false); + set_up_axioms(indexof); + return indexof; + } + + app * theory_str::mk_strlen(expr * e) { + /*if (m_strutil.is_string(e)) {*/ if (false) { + zstring strval; + u.str.is_string(e, strval); + unsigned int len = strval.length(); + return m_autil.mk_numeral(rational(len), true); + } else { + if (false) { + // use cache + app * lenTerm = NULL; + if (!length_ast_map.find(e, lenTerm)) { + lenTerm = u.str.mk_length(e); + length_ast_map.insert(e, lenTerm); + m_trail.push_back(lenTerm); } + return lenTerm; } else { - concats.insert(simConcat); + // always regen + return u.str.mk_length(e); } - } else if (u.str.is_string(ast)) { - consts.insert(ast); - } else { - vars.insert(ast); - } - eqcNode = get_eqc_next(eqcNode); - } while (eqcNode != n); -} - -void theory_str::get_nodes_in_concat(expr * node, ptr_vector & nodeList) { - app * a_node = to_app(node); - if (!u.str.is_concat(a_node)) { - nodeList.push_back(node); - return; - } else { - SASSERT(a_node->get_num_args() == 2); - expr * leftArg = a_node->get_arg(0); - expr * rightArg = a_node->get_arg(1); - get_nodes_in_concat(leftArg, nodeList); - get_nodes_in_concat(rightArg, nodeList); - } -} - -// previously Concat() in strTheory.cpp -// Evaluates the concatenation (n1 . n2) with respect to -// the current equivalence classes of n1 and n2. -// Returns a constant string expression representing this concatenation -// if one can be determined, or NULL if this is not possible. -expr * theory_str::eval_concat(expr * n1, expr * n2) { - bool n1HasEqcValue = false; - bool n2HasEqcValue = false; - expr * v1 = get_eqc_value(n1, n1HasEqcValue); - expr * v2 = get_eqc_value(n2, n2HasEqcValue); - if (n1HasEqcValue && n2HasEqcValue) { - zstring n1_str, n2_str; - u.str.is_string(v1, n1_str); - u.str.is_string(v2, n2_str); - zstring result = n1_str + n2_str; - return mk_string(result); - } else if (n1HasEqcValue && !n2HasEqcValue) { - zstring v1_str; - u.str.is_string(v1, v1_str); - if (v1_str.empty()) { - return n2; - } - } else if (n2HasEqcValue && !n1HasEqcValue) { - zstring v2_str; - u.str.is_string(v2, v2_str); - if (v2_str.empty()) { - return n1; - } - } - // give up - return NULL; -} - -static inline std::string rational_to_string_if_exists(const rational & x, bool x_exists) { - if (x_exists) { - return x.to_string(); - } else { - return "?"; - } -} - -/* - * The inputs: - * ~ nn: non const node - * ~ eq_str: the equivalent constant string of nn - * Iterate the parent of all eqc nodes of nn, looking for: - * ~ concat node - * to see whether some concat nodes can be simplified. - */ -void theory_str::simplify_parent(expr * nn, expr * eq_str) { - ast_manager & m = get_manager(); - context & ctx = get_context(); - - TRACE("str", tout << "simplifying parents of " << mk_ismt2_pp(nn, m) - << " with respect to " << mk_ismt2_pp(eq_str, m) << std::endl;); - - ctx.internalize(nn, false); - - zstring eq_strValue; - u.str.is_string(eq_str, eq_strValue); - expr * n_eqNode = nn; - do { - enode * n_eq_enode = ctx.get_enode(n_eqNode); - TRACE("str", tout << "considering all parents of " << mk_ismt2_pp(n_eqNode, m) << std::endl - << "associated n_eq_enode has " << n_eq_enode->get_num_parents() << " parents" << std::endl;); - - // the goal of this next bit is to avoid dereferencing a bogus e_parent in the following loop. - // what I imagine is causing this bug is that, for example, we examine some parent, we add an axiom that involves it, - // and the parent_it iterator becomes invalidated, because we indirectly modified the container that we're iterating over. - - enode_vector current_parents; - for (enode_vector::const_iterator parent_it = n_eq_enode->begin_parents(); parent_it != n_eq_enode->end_parents(); parent_it++) { - current_parents.insert(*parent_it); - } - - for (enode_vector::iterator parent_it = current_parents.begin(); parent_it != current_parents.end(); ++parent_it) { - enode * e_parent = *parent_it; - SASSERT(e_parent != NULL); - - app * a_parent = e_parent->get_owner(); - TRACE("str", tout << "considering parent " << mk_ismt2_pp(a_parent, m) << std::endl;); - - if (u.str.is_concat(a_parent)) { - expr * arg0 = a_parent->get_arg(0); - expr * arg1 = a_parent->get_arg(1); - - rational parentLen; - bool parentLen_exists = get_len_value(a_parent, parentLen); - - if (arg0 == n_eq_enode->get_owner()) { - rational arg0Len, arg1Len; - bool arg0Len_exists = get_len_value(eq_str, arg0Len); - bool arg1Len_exists = get_len_value(arg1, arg1Len); - - TRACE("str", - tout << "simplify_parent #1:" << std::endl - << "* parent = " << mk_ismt2_pp(a_parent, m) << std::endl - << "* |parent| = " << rational_to_string_if_exists(parentLen, parentLen_exists) << std::endl - << "* |arg0| = " << rational_to_string_if_exists(arg0Len, arg0Len_exists) << std::endl - << "* |arg1| = " << rational_to_string_if_exists(arg1Len, arg1Len_exists) << std::endl; - ); - - if (parentLen_exists && !arg1Len_exists) { - TRACE("str", tout << "make up len for arg1" << std::endl;); - expr_ref implyL11(m.mk_and(ctx.mk_eq_atom(mk_strlen(a_parent), mk_int(parentLen)), - ctx.mk_eq_atom(mk_strlen(arg0), mk_int(arg0Len))), m); - rational makeUpLenArg1 = parentLen - arg0Len; - if (makeUpLenArg1.is_nonneg()) { - expr_ref implyR11(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(makeUpLenArg1)), m); - assert_implication(implyL11, implyR11); - } else { - expr_ref neg(m.mk_not(implyL11), m); - assert_axiom(neg); - } - } - - // (Concat n_eqNode arg1) /\ arg1 has eq const - - expr * concatResult = eval_concat(eq_str, arg1); - if (concatResult != NULL) { - bool arg1HasEqcValue = false; - expr * arg1Value = get_eqc_value(arg1, arg1HasEqcValue); - expr_ref implyL(m); - if (arg1 != arg1Value) { - expr_ref eq_ast1(m); - eq_ast1 = ctx.mk_eq_atom(n_eqNode, eq_str); - SASSERT(eq_ast1); - - expr_ref eq_ast2(m); - eq_ast2 = ctx.mk_eq_atom(arg1, arg1Value); - SASSERT(eq_ast2); - implyL = m.mk_and(eq_ast1, eq_ast2); - } else { - implyL = ctx.mk_eq_atom(n_eqNode, eq_str); - } - - - if (!in_same_eqc(a_parent, concatResult)) { - expr_ref implyR(m); - implyR = ctx.mk_eq_atom(a_parent, concatResult); - SASSERT(implyR); - - assert_implication(implyL, implyR); - } - } else if (u.str.is_concat(to_app(n_eqNode))) { - expr_ref simpleConcat(m); - simpleConcat = mk_concat(eq_str, arg1); - if (!in_same_eqc(a_parent, simpleConcat)) { - expr_ref implyL(m); - implyL = ctx.mk_eq_atom(n_eqNode, eq_str); - SASSERT(implyL); - - expr_ref implyR(m); - implyR = ctx.mk_eq_atom(a_parent, simpleConcat); - SASSERT(implyR); - assert_implication(implyL, implyR); - } - } - } // if (arg0 == n_eq_enode->get_owner()) - - if (arg1 == n_eq_enode->get_owner()) { - rational arg0Len, arg1Len; - bool arg0Len_exists = get_len_value(arg0, arg0Len); - bool arg1Len_exists = get_len_value(eq_str, arg1Len); - - TRACE("str", - tout << "simplify_parent #2:" << std::endl - << "* parent = " << mk_ismt2_pp(a_parent, m) << std::endl - << "* |parent| = " << rational_to_string_if_exists(parentLen, parentLen_exists) << std::endl - << "* |arg0| = " << rational_to_string_if_exists(arg0Len, arg0Len_exists) << std::endl - << "* |arg1| = " << rational_to_string_if_exists(arg1Len, arg1Len_exists) << std::endl; - ); - if (parentLen_exists && !arg0Len_exists) { - TRACE("str", tout << "make up len for arg0" << std::endl;); - expr_ref implyL11(m.mk_and(ctx.mk_eq_atom(mk_strlen(a_parent), mk_int(parentLen)), - ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1Len))), m); - rational makeUpLenArg0 = parentLen - arg1Len; - if (makeUpLenArg0.is_nonneg()) { - expr_ref implyR11(ctx.mk_eq_atom(mk_strlen(arg0), mk_int(makeUpLenArg0)), m); - assert_implication(implyL11, implyR11); - } else { - expr_ref neg(m.mk_not(implyL11), m); - assert_axiom(neg); - } - } - - // (Concat arg0 n_eqNode) /\ arg0 has eq const - - expr * concatResult = eval_concat(arg0, eq_str); - if (concatResult != NULL) { - bool arg0HasEqcValue = false; - expr * arg0Value = get_eqc_value(arg0, arg0HasEqcValue); - expr_ref implyL(m); - if (arg0 != arg0Value) { - expr_ref eq_ast1(m); - eq_ast1 = ctx.mk_eq_atom(n_eqNode, eq_str); - SASSERT(eq_ast1); - expr_ref eq_ast2(m); - eq_ast2 = ctx.mk_eq_atom(arg0, arg0Value); - SASSERT(eq_ast2); - - implyL = m.mk_and(eq_ast1, eq_ast2); - } else { - implyL = ctx.mk_eq_atom(n_eqNode, eq_str); - } - - if (!in_same_eqc(a_parent, concatResult)) { - expr_ref implyR(m); - implyR = ctx.mk_eq_atom(a_parent, concatResult); - SASSERT(implyR); - - assert_implication(implyL, implyR); - } - } else if (u.str.is_concat(to_app(n_eqNode))) { - expr_ref simpleConcat(m); - simpleConcat = mk_concat(arg0, eq_str); - if (!in_same_eqc(a_parent, simpleConcat)) { - expr_ref implyL(m); - implyL = ctx.mk_eq_atom(n_eqNode, eq_str); - SASSERT(implyL); - - expr_ref implyR(m); - implyR = ctx.mk_eq_atom(a_parent, simpleConcat); - SASSERT(implyR); - assert_implication(implyL, implyR); - } - } - } // if (arg1 == n_eq_enode->get_owner - - - //--------------------------------------------------------- - // Case (2-1) begin: (Concat n_eqNode (Concat str var)) - if (arg0 == n_eqNode && u.str.is_concat(to_app(arg1))) { - app * a_arg1 = to_app(arg1); - TRACE("str", tout << "simplify_parent #3" << std::endl;); - expr * r_concat_arg0 = a_arg1->get_arg(0); - if (u.str.is_string(r_concat_arg0)) { - expr * combined_str = eval_concat(eq_str, r_concat_arg0); - SASSERT(combined_str); - expr * r_concat_arg1 = a_arg1->get_arg(1); - expr_ref implyL(m); - implyL = ctx.mk_eq_atom(n_eqNode, eq_str); - expr * simplifiedAst = mk_concat(combined_str, r_concat_arg1); - if (!in_same_eqc(a_parent, simplifiedAst)) { - expr_ref implyR(m); - implyR = ctx.mk_eq_atom(a_parent, simplifiedAst); - assert_implication(implyL, implyR); - } - } - } - // Case (2-1) end: (Concat n_eqNode (Concat str var)) - //--------------------------------------------------------- - - - //--------------------------------------------------------- - // Case (2-2) begin: (Concat (Concat var str) n_eqNode) - if (u.str.is_concat(to_app(arg0)) && arg1 == n_eqNode) { - app * a_arg0 = to_app(arg0); - TRACE("str", tout << "simplify_parent #4" << std::endl;); - expr * l_concat_arg1 = a_arg0->get_arg(1); - if (u.str.is_string(l_concat_arg1)) { - expr * combined_str = eval_concat(l_concat_arg1, eq_str); - SASSERT(combined_str); - expr * l_concat_arg0 = a_arg0->get_arg(0); - expr_ref implyL(m); - implyL = ctx.mk_eq_atom(n_eqNode, eq_str); - expr * simplifiedAst = mk_concat(l_concat_arg0, combined_str); - if (!in_same_eqc(a_parent, simplifiedAst)) { - expr_ref implyR(m); - implyR = ctx.mk_eq_atom(a_parent, simplifiedAst); - assert_implication(implyL, implyR); - } - } - } - // Case (2-2) end: (Concat (Concat var str) n_eqNode) - //--------------------------------------------------------- - - // Have to look up one more layer: if the parent of the concat is another concat - //------------------------------------------------- - // Case (3-1) begin: (Concat (Concat var n_eqNode) str ) - if (arg1 == n_eqNode) { - for (enode_vector::iterator concat_parent_it = e_parent->begin_parents(); - concat_parent_it != e_parent->end_parents(); concat_parent_it++) { - enode * e_concat_parent = *concat_parent_it; - app * concat_parent = e_concat_parent->get_owner(); - if (u.str.is_concat(concat_parent)) { - expr * concat_parent_arg0 = concat_parent->get_arg(0); - expr * concat_parent_arg1 = concat_parent->get_arg(1); - if (concat_parent_arg0 == a_parent && u.str.is_string(concat_parent_arg1)) { - TRACE("str", tout << "simplify_parent #5" << std::endl;); - expr * combinedStr = eval_concat(eq_str, concat_parent_arg1); - SASSERT(combinedStr); - expr_ref implyL(m); - implyL = ctx.mk_eq_atom(n_eqNode, eq_str); - expr * simplifiedAst = mk_concat(arg0, combinedStr); - if (!in_same_eqc(concat_parent, simplifiedAst)) { - expr_ref implyR(m); - implyR = ctx.mk_eq_atom(concat_parent, simplifiedAst); - assert_implication(implyL, implyR); - } - } - } - } - } - // Case (3-1) end: (Concat (Concat var n_eqNode) str ) - // Case (3-2) begin: (Concat str (Concat n_eqNode var) ) - if (arg0 == n_eqNode) { - for (enode_vector::iterator concat_parent_it = e_parent->begin_parents(); - concat_parent_it != e_parent->end_parents(); concat_parent_it++) { - enode * e_concat_parent = *concat_parent_it; - app * concat_parent = e_concat_parent->get_owner(); - if (u.str.is_concat(concat_parent)) { - expr * concat_parent_arg0 = concat_parent->get_arg(0); - expr * concat_parent_arg1 = concat_parent->get_arg(1); - if (concat_parent_arg1 == a_parent && u.str.is_string(concat_parent_arg0)) { - TRACE("str", tout << "simplify_parent #6" << std::endl;); - expr * combinedStr = eval_concat(concat_parent_arg0, eq_str); - SASSERT(combinedStr); - expr_ref implyL(m); - implyL = ctx.mk_eq_atom(n_eqNode, eq_str); - expr * simplifiedAst = mk_concat(combinedStr, arg1); - if (!in_same_eqc(concat_parent, simplifiedAst)) { - expr_ref implyR(m); - implyR = ctx.mk_eq_atom(concat_parent, simplifiedAst); - assert_implication(implyL, implyR); - } - } - } - } - } - // Case (3-2) end: (Concat str (Concat n_eqNode var) ) - } // if is_concat(a_parent) - } // for parent_it : n_eq_enode->begin_parents() - - - // check next EQC member - n_eqNode = get_eqc_next(n_eqNode); - } while (n_eqNode != nn); -} - -expr * theory_str::simplify_concat(expr * node) { - ast_manager & m = get_manager(); - context & ctx = get_context(); - std::map resolvedMap; - ptr_vector argVec; - get_nodes_in_concat(node, argVec); - - for (unsigned i = 0; i < argVec.size(); ++i) { - bool vArgHasEqcValue = false; - expr * vArg = get_eqc_value(argVec[i], vArgHasEqcValue); - if (vArg != argVec[i]) { - resolvedMap[argVec[i]] = vArg; - } - } - - if (resolvedMap.size() == 0) { - // no simplification possible - return node; - } else { - expr * resultAst = mk_string(""); - for (unsigned i = 0; i < argVec.size(); ++i) { - bool vArgHasEqcValue = false; - expr * vArg = get_eqc_value(argVec[i], vArgHasEqcValue); - resultAst = mk_concat(resultAst, vArg); - } - TRACE("str", tout << mk_ismt2_pp(node, m) << " is simplified to " << mk_ismt2_pp(resultAst, m) << std::endl;); - - if (in_same_eqc(node, resultAst)) { - TRACE("str", tout << "SKIP: both concats are already in the same equivalence class" << std::endl;); - } else { - expr_ref_vector items(m); - int pos = 0; - std::map::iterator itor = resolvedMap.begin(); - for (; itor != resolvedMap.end(); ++itor) { - items.push_back(ctx.mk_eq_atom(itor->first, itor->second)); - pos += 1; - } - expr_ref premise(mk_and(items), m); - expr_ref conclusion(ctx.mk_eq_atom(node, resultAst), m); - assert_implication(premise, conclusion); - } - return resultAst; - } - -} - -// Modified signature of Z3str2's inferLenConcat(). -// Returns true iff nLen can be inferred by this method -// (i.e. the equivalent of a len_exists flag in get_len_value()). - -bool theory_str::infer_len_concat(expr * n, rational & nLen) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - expr * arg0 = to_app(n)->get_arg(0); - expr * arg1 = to_app(n)->get_arg(1); - - rational arg0_len, arg1_len; - bool arg0_len_exists = get_len_value(arg0, arg0_len); - bool arg1_len_exists = get_len_value(arg1, arg1_len); - rational tmp_len; - bool nLen_exists = get_len_value(n, tmp_len); - - if (arg0_len_exists && arg1_len_exists && !nLen_exists) { - expr_ref_vector l_items(m); - // if (mk_strlen(arg0) != mk_int(arg0_len)) { - { - l_items.push_back(ctx.mk_eq_atom(mk_strlen(arg0), mk_int(arg0_len))); - } - - // if (mk_strlen(arg1) != mk_int(arg1_len)) { - { - l_items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1_len))); - } - - expr_ref axl(m.mk_and(l_items.size(), l_items.c_ptr()), m); - rational nnLen = arg0_len + arg1_len; - expr_ref axr(ctx.mk_eq_atom(mk_strlen(n), mk_int(nnLen)), m); - TRACE("str", tout << "inferred (Length " << mk_pp(n, m) << ") = " << nnLen << std::endl;); - assert_implication(axl, axr); - nLen = nnLen; - return true; - } else { - return false; - } -} - -void theory_str::infer_len_concat_arg(expr * n, rational len) { - if (len.is_neg()) { - return; - } - - context & ctx = get_context(); - ast_manager & m = get_manager(); - - expr * arg0 = to_app(n)->get_arg(0); - expr * arg1 = to_app(n)->get_arg(1); - rational arg0_len, arg1_len; - bool arg0_len_exists = get_len_value(arg0, arg0_len); - bool arg1_len_exists = get_len_value(arg1, arg1_len); - - expr_ref_vector l_items(m); - expr_ref axr(m); - axr.reset(); - - // if (mk_length(t, n) != mk_int(ctx, len)) { - { - l_items.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(len))); - } - - if (!arg0_len_exists && arg1_len_exists) { - //if (mk_length(t, arg1) != mk_int(ctx, arg1_len)) { - { - l_items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1_len))); - } - rational arg0Len = len - arg1_len; - if (arg0Len.is_nonneg()) { - axr = ctx.mk_eq_atom(mk_strlen(arg0), mk_int(arg0Len)); - } else { - // could negate - } - } else if (arg0_len_exists && !arg1_len_exists) { - //if (mk_length(t, arg0) != mk_int(ctx, arg0_len)) { - { - l_items.push_back(ctx.mk_eq_atom(mk_strlen(arg0), mk_int(arg0_len))); - } - rational arg1Len = len - arg0_len; - if (arg1Len.is_nonneg()) { - axr = ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1Len)); - } else { - // could negate - } - } else { - - } - - if (axr) { - expr_ref axl(m.mk_and(l_items.size(), l_items.c_ptr()), m); - assert_implication(axl, axr); - } -} - -void theory_str::infer_len_concat_equality(expr * nn1, expr * nn2) { - rational nnLen; - bool nnLen_exists = get_len_value(nn1, nnLen); - if (!nnLen_exists) { - nnLen_exists = get_len_value(nn2, nnLen); - } - - // case 1: - // Known: a1_arg0 and a1_arg1 - // Unknown: nn1 - - if (u.str.is_concat(to_app(nn1))) { - rational nn1ConcatLen; - bool nn1ConcatLen_exists = infer_len_concat(nn1, nn1ConcatLen); - if (nnLen_exists && nn1ConcatLen_exists) { - nnLen = nn1ConcatLen; - } - } - - // case 2: - // Known: a1_arg0 and a1_arg1 - // Unknown: nn1 - - if (u.str.is_concat(to_app(nn2))) { - rational nn2ConcatLen; - bool nn2ConcatLen_exists = infer_len_concat(nn2, nn2ConcatLen); - if (nnLen_exists && nn2ConcatLen_exists) { - nnLen = nn2ConcatLen; - } - } - - if (nnLen_exists) { - if (u.str.is_concat(to_app(nn1))) { - infer_len_concat_arg(nn1, nnLen); - } - if (u.str.is_concat(to_app(nn2))) { - infer_len_concat_arg(nn2, nnLen); } } /* - if (isConcatFunc(t, nn2)) { - int nn2ConcatLen = inferLenConcat(t, nn2); - if (nnLen == -1 && nn2ConcatLen != -1) - nnLen = nn2ConcatLen; - } - - if (nnLen != -1) { - if (isConcatFunc(t, nn1)) { - inferLenConcatArg(t, nn1, nnLen); - } - if (isConcatFunc(t, nn2)) { - inferLenConcatArg(t, nn2, nnLen); - } - } - */ -} - -void theory_str::add_theory_aware_branching_info(expr * term, double priority, lbool phase) { - context & ctx = get_context(); - ctx.internalize(term, false); - bool_var v = ctx.get_bool_var(term); - ctx.add_theory_aware_branching_info(v, priority, phase); -} - -void theory_str::generate_mutual_exclusion(expr_ref_vector & terms) { - context & ctx = get_context(); - // pull each literal out of the arrangement disjunction - literal_vector ls; - for (unsigned i = 0; i < terms.size(); ++i) { - expr * e = terms.get(i); - literal l = ctx.get_literal(e); - ls.push_back(l); - } - ctx.mk_th_case_split(ls.size(), ls.c_ptr()); -} - -void theory_str::print_cut_var(expr * node, std::ofstream & xout) { - ast_manager & m = get_manager(); - xout << "Cut info of " << mk_pp(node, m) << std::endl; - if (cut_var_map.contains(node)) { - if (!cut_var_map[node].empty()) { - xout << "[" << cut_var_map[node].top()->level << "] "; - std::map::iterator itor = cut_var_map[node].top()->vars.begin(); - for (; itor != cut_var_map[node].top()->vars.end(); ++itor) { - xout << mk_pp(itor->first, m) << ", "; + * Returns the simplified concatenation of two expressions, + * where either both expressions are constant strings + * or one expression is the empty string. + * If this precondition does not hold, the function returns NULL. + * (note: this function was strTheory::Concat()) + */ + expr * theory_str::mk_concat_const_str(expr * n1, expr * n2) { + bool n1HasEqcValue = false; + bool n2HasEqcValue = false; + expr * v1 = get_eqc_value(n1, n1HasEqcValue); + expr * v2 = get_eqc_value(n2, n2HasEqcValue); + if (n1HasEqcValue && n2HasEqcValue) { + zstring n1_str; + u.str.is_string(v1, n1_str); + zstring n2_str; + u.str.is_string(v2, n2_str); + zstring result = n1_str + n2_str; + return mk_string(result); + } else if (n1HasEqcValue && !n2HasEqcValue) { + zstring n1_str; + u.str.is_string(v1, n1_str); + if (n1_str.empty()) { + return n2; + } + } else if (!n1HasEqcValue && n2HasEqcValue) { + zstring n2_str; + u.str.is_string(v2, n2_str); + if (n2_str.empty()) { + return n1; } - xout << std::endl; } - } -} - -/* - * Handle two equivalent Concats. - */ -void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { - ast_manager & m = get_manager(); - context & ctx = get_context(); - - app * a_nn1 = to_app(nn1); - SASSERT(a_nn1->get_num_args() == 2); - app * a_nn2 = to_app(nn2); - SASSERT(a_nn2->get_num_args() == 2); - - expr * a1_arg0 = a_nn1->get_arg(0); - expr * a1_arg1 = a_nn1->get_arg(1); - expr * a2_arg0 = a_nn2->get_arg(0); - expr * a2_arg1 = a_nn2->get_arg(1); - - rational a1_arg0_len, a1_arg1_len, a2_arg0_len, a2_arg1_len; - - bool a1_arg0_len_exists = get_len_value(a1_arg0, a1_arg0_len); - bool a1_arg1_len_exists = get_len_value(a1_arg1, a1_arg1_len); - bool a2_arg0_len_exists = get_len_value(a2_arg0, a2_arg0_len); - bool a2_arg1_len_exists = get_len_value(a2_arg1, a2_arg1_len); - - TRACE("str", tout << "nn1 = " << mk_ismt2_pp(nn1, m) << std::endl - << "nn2 = " << mk_ismt2_pp(nn2, m) << std::endl;); - - TRACE("str", tout - << "len(" << mk_pp(a1_arg0, m) << ") = " << (a1_arg0_len_exists ? a1_arg0_len.to_string() : "?") << std::endl - << "len(" << mk_pp(a1_arg1, m) << ") = " << (a1_arg1_len_exists ? a1_arg1_len.to_string() : "?") << std::endl - << "len(" << mk_pp(a2_arg0, m) << ") = " << (a2_arg0_len_exists ? a2_arg0_len.to_string() : "?") << std::endl - << "len(" << mk_pp(a2_arg1, m) << ") = " << (a2_arg1_len_exists ? a2_arg1_len.to_string() : "?") << std::endl - << std::endl;); - - infer_len_concat_equality(nn1, nn2); - - if (a1_arg0 == a2_arg0) { - if (!in_same_eqc(a1_arg1, a2_arg1)) { - expr_ref premise(ctx.mk_eq_atom(nn1, nn2), m); - expr_ref eq1(ctx.mk_eq_atom(a1_arg1, a2_arg1), m); - expr_ref eq2(ctx.mk_eq_atom(mk_strlen(a1_arg1), mk_strlen(a2_arg1)), m); - expr_ref conclusion(m.mk_and(eq1, eq2), m); - assert_implication(premise, conclusion); - } - TRACE("str", tout << "SKIP: a1_arg0 == a2_arg0" << std::endl;); - return; + return NULL; } - if (a1_arg1 == a2_arg1) { - if (!in_same_eqc(a1_arg0, a2_arg0)) { - expr_ref premise(ctx.mk_eq_atom(nn1, nn2), m); - expr_ref eq1(ctx.mk_eq_atom(a1_arg0, a2_arg0), m); - expr_ref eq2(ctx.mk_eq_atom(mk_strlen(a1_arg0), mk_strlen(a2_arg0)), m); - expr_ref conclusion(m.mk_and(eq1, eq2), m); - assert_implication(premise, conclusion); - } - TRACE("str", tout << "SKIP: a1_arg1 == a2_arg1" << std::endl;); - return; - } + expr * theory_str::mk_concat(expr * n1, expr * n2) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + ENSURE(n1 != NULL); + ENSURE(n2 != NULL); + bool n1HasEqcValue = false; + bool n2HasEqcValue = false; + n1 = get_eqc_value(n1, n1HasEqcValue); + n2 = get_eqc_value(n2, n2HasEqcValue); + if (n1HasEqcValue && n2HasEqcValue) { + return mk_concat_const_str(n1, n2); + } else if (n1HasEqcValue && !n2HasEqcValue) { + bool n2_isConcatFunc = u.str.is_concat(to_app(n2)); + zstring n1_str; + u.str.is_string(n1, n1_str); + if (n1_str.empty()) { + return n2; + } + if (n2_isConcatFunc) { + expr * n2_arg0 = to_app(n2)->get_arg(0); + expr * n2_arg1 = to_app(n2)->get_arg(1); + if (u.str.is_string(n2_arg0)) { + n1 = mk_concat_const_str(n1, n2_arg0); // n1 will be a constant + n2 = n2_arg1; + } + } + } else if (!n1HasEqcValue && n2HasEqcValue) { + zstring n2_str; + u.str.is_string(n2, n2_str); + if (n2_str.empty()) { + return n1; + } - // quick path - - if (in_same_eqc(a1_arg0, a2_arg0)) { - if (in_same_eqc(a1_arg1, a2_arg1)) { - TRACE("str", tout << "SKIP: a1_arg0 =~ a2_arg0 and a1_arg1 =~ a2_arg1" << std::endl;); - return; + if (u.str.is_concat(to_app(n1))) { + expr * n1_arg0 = to_app(n1)->get_arg(0); + expr * n1_arg1 = to_app(n1)->get_arg(1); + if (u.str.is_string(n1_arg1)) { + n1 = n1_arg0; + n2 = mk_concat_const_str(n1_arg1, n2); // n2 will be a constant + } + } } else { - TRACE("str", tout << "quick path 1-1: a1_arg0 =~ a2_arg0" << std::endl;); - expr_ref premise(m.mk_and(ctx.mk_eq_atom(nn1, nn2), ctx.mk_eq_atom(a1_arg0, a2_arg0)), m); - expr_ref conclusion(m.mk_and(ctx.mk_eq_atom(a1_arg1, a2_arg1), ctx.mk_eq_atom(mk_strlen(a1_arg1), mk_strlen(a2_arg1))), m); - assert_implication(premise, conclusion); - return; - } - } else { - if (in_same_eqc(a1_arg1, a2_arg1)) { - TRACE("str", tout << "quick path 1-2: a1_arg1 =~ a2_arg1" << std::endl;); - expr_ref premise(m.mk_and(ctx.mk_eq_atom(nn1, nn2), ctx.mk_eq_atom(a1_arg1, a2_arg1)), m); - expr_ref conclusion(m.mk_and(ctx.mk_eq_atom(a1_arg0, a2_arg0), ctx.mk_eq_atom(mk_strlen(a1_arg0), mk_strlen(a2_arg0))), m); - assert_implication(premise, conclusion); - return; - } - } - - // quick path 2-1 - if (a1_arg0_len_exists && a2_arg0_len_exists && a1_arg0_len == a2_arg0_len) { - if (!in_same_eqc(a1_arg0, a2_arg0)) { - TRACE("str", tout << "quick path 2-1: len(nn1.arg0) == len(nn2.arg0)" << std::endl;); - expr_ref ax_l1(ctx.mk_eq_atom(nn1, nn2), m); - expr_ref ax_l2(ctx.mk_eq_atom(mk_strlen(a1_arg0), mk_strlen(a2_arg0)), m); - expr_ref ax_r1(ctx.mk_eq_atom(a1_arg0, a2_arg0), m); - expr_ref ax_r2(ctx.mk_eq_atom(a1_arg1, a2_arg1), m); - - expr_ref premise(m.mk_and(ax_l1, ax_l2), m); - expr_ref conclusion(m.mk_and(ax_r1, ax_r2), m); - - assert_implication(premise, conclusion); - - if (opt_NoQuickReturn_IntegerTheory) { - TRACE("str", tout << "bypassing quick return from the end of this case" << std::endl;); - } else { - return; + if (u.str.is_concat(to_app(n1)) && u.str.is_concat(to_app(n2))) { + expr * n1_arg0 = to_app(n1)->get_arg(0); + expr * n1_arg1 = to_app(n1)->get_arg(1); + expr * n2_arg0 = to_app(n2)->get_arg(0); + expr * n2_arg1 = to_app(n2)->get_arg(1); + if (u.str.is_string(n1_arg1) && u.str.is_string(n2_arg0)) { + expr * tmpN1 = n1_arg0; + expr * tmpN2 = mk_concat_const_str(n1_arg1, n2_arg0); + n1 = mk_concat(tmpN1, tmpN2); + n2 = n2_arg1; + } } } + + //------------------------------------------------------ + // * expr * ast1 = mk_2_arg_app(ctx, td->Concat, n1, n2); + // * expr * ast2 = mk_2_arg_app(ctx, td->Concat, n1, n2); + // Z3 treats (ast1) and (ast2) as two different nodes. + //------------------------------------------------------- + + expr * concatAst = NULL; + + if (!concat_astNode_map.find(n1, n2, concatAst)) { + concatAst = u.str.mk_concat(n1, n2); + m_trail.push_back(concatAst); + concat_astNode_map.insert(n1, n2, concatAst); + + expr_ref concat_length(mk_strlen(concatAst), m); + + ptr_vector childrenVector; + get_nodes_in_concat(concatAst, childrenVector); + expr_ref_vector items(m); + for (unsigned int i = 0; i < childrenVector.size(); i++) { + items.push_back(mk_strlen(childrenVector.get(i))); + } + expr_ref lenAssert(ctx.mk_eq_atom(concat_length, m_autil.mk_add(items.size(), items.c_ptr())), m); + assert_axiom(lenAssert); + } + return concatAst; } - if (a1_arg1_len_exists && a2_arg1_len_exists && a1_arg1_len == a2_arg1_len) { - if (!in_same_eqc(a1_arg1, a2_arg1)) { - TRACE("str", tout << "quick path 2-2: len(nn1.arg1) == len(nn2.arg1)" << std::endl;); - expr_ref ax_l1(ctx.mk_eq_atom(nn1, nn2), m); - expr_ref ax_l2(ctx.mk_eq_atom(mk_strlen(a1_arg1), mk_strlen(a2_arg1)), m); - expr_ref ax_r1(ctx.mk_eq_atom(a1_arg0, a2_arg0), m); - expr_ref ax_r2(ctx.mk_eq_atom(a1_arg1, a2_arg1), m); + bool theory_str::can_propagate() { + return !m_basicstr_axiom_todo.empty() || !m_str_eq_todo.empty() + || !m_concat_axiom_todo.empty() || !m_concat_eval_todo.empty() + || !m_library_aware_axiom_todo.empty() + || !m_delayed_axiom_setup_terms.empty(); + ; + } - expr_ref premise(m.mk_and(ax_l1, ax_l2), m); - expr_ref conclusion(m.mk_and(ax_r1, ax_r2), m); + void theory_str::propagate() { + context & ctx = get_context(); + while (can_propagate()) { + TRACE("str", tout << "propagating..." << std::endl;); + for (unsigned i = 0; i < m_basicstr_axiom_todo.size(); ++i) { + instantiate_basic_string_axioms(m_basicstr_axiom_todo[i]); + } + m_basicstr_axiom_todo.reset(); + TRACE("str", tout << "reset m_basicstr_axiom_todo" << std::endl;); - assert_implication(premise, conclusion); - if (opt_NoQuickReturn_IntegerTheory) { - TRACE("str", tout << "bypassing quick return from the end of this case" << std::endl;); + for (unsigned i = 0; i < m_str_eq_todo.size(); ++i) { + std::pair pair = m_str_eq_todo[i]; + enode * lhs = pair.first; + enode * rhs = pair.second; + handle_equality(lhs->get_owner(), rhs->get_owner()); + } + m_str_eq_todo.reset(); + + for (unsigned i = 0; i < m_concat_axiom_todo.size(); ++i) { + instantiate_concat_axiom(m_concat_axiom_todo[i]); + } + m_concat_axiom_todo.reset(); + + for (unsigned i = 0; i < m_concat_eval_todo.size(); ++i) { + try_eval_concat(m_concat_eval_todo[i]); + } + m_concat_eval_todo.reset(); + + for (unsigned i = 0; i < m_library_aware_axiom_todo.size(); ++i) { + enode * e = m_library_aware_axiom_todo[i]; + app * a = e->get_owner(); + if (u.str.is_stoi(a)) { + instantiate_axiom_str_to_int(e); + } else if (u.str.is_itos(a)) { + instantiate_axiom_int_to_str(e); + } else if (u.str.is_at(a)) { + instantiate_axiom_CharAt(e); + } else if (u.str.is_prefix(a)) { + instantiate_axiom_prefixof(e); + } else if (u.str.is_suffix(a)) { + instantiate_axiom_suffixof(e); + } else if (u.str.is_contains(a)) { + instantiate_axiom_Contains(e); + } else if (u.str.is_index(a)) { + instantiate_axiom_Indexof(e); + /* TODO NEXT: Indexof2/Lastindexof rewrite? + } else if (is_Indexof2(e)) { + instantiate_axiom_Indexof2(e); + } else if (is_LastIndexof(e)) { + instantiate_axiom_LastIndexof(e); + */ + } else if (u.str.is_extract(a)) { + // TODO check semantics of substr vs. extract + instantiate_axiom_Substr(e); + } else if (u.str.is_replace(a)) { + instantiate_axiom_Replace(e); + } else if (u.str.is_in_re(a)) { + instantiate_axiom_RegexIn(e); + } else { + TRACE("str", tout << "BUG: unhandled library-aware term " << mk_pp(e->get_owner(), get_manager()) << std::endl;); + NOT_IMPLEMENTED_YET(); + } + } + m_library_aware_axiom_todo.reset(); + + for (unsigned i = 0; i < m_delayed_axiom_setup_terms.size(); ++i) { + // I think this is okay + ctx.internalize(m_delayed_axiom_setup_terms[i].get(), false); + set_up_axioms(m_delayed_axiom_setup_terms[i].get()); + } + m_delayed_axiom_setup_terms.reset(); + } + } + + /* + * Attempt to evaluate a concat over constant strings, + * and if this is possible, assert equality between the + * flattened string and the original term. + */ + + void theory_str::try_eval_concat(enode * cat) { + app * a_cat = cat->get_owner(); + SASSERT(u.str.is_concat(a_cat)); + + context & ctx = get_context(); + ast_manager & m = get_manager(); + + TRACE("str", tout << "attempting to flatten " << mk_pp(a_cat, m) << std::endl;); + + std::stack worklist; + zstring flattenedString(""); + bool constOK = true; + + { + app * arg0 = to_app(a_cat->get_arg(0)); + app * arg1 = to_app(a_cat->get_arg(1)); + + worklist.push(arg1); + worklist.push(arg0); + } + + while (constOK && !worklist.empty()) { + app * evalArg = worklist.top(); worklist.pop(); + zstring nextStr; + if (u.str.is_string(evalArg, nextStr)) { + flattenedString = flattenedString + nextStr; + } else if (u.str.is_concat(evalArg)) { + app * arg0 = to_app(evalArg->get_arg(0)); + app * arg1 = to_app(evalArg->get_arg(1)); + + worklist.push(arg1); + worklist.push(arg0); } else { - return; + TRACE("str", tout << "non-constant term in concat -- giving up." << std::endl;); + constOK = false; + break; } } + if (constOK) { + TRACE("str", tout << "flattened to \"" << flattenedString.encode().c_str() << "\"" << std::endl;); + expr_ref constStr(mk_string(flattenedString), m); + expr_ref axiom(ctx.mk_eq_atom(a_cat, constStr), m); + assert_axiom(axiom); + } } - expr_ref new_nn1(simplify_concat(nn1), m); - expr_ref new_nn2(simplify_concat(nn2), m); - app * a_new_nn1 = to_app(new_nn1); - app * a_new_nn2 = to_app(new_nn2); + /* + * Instantiate an axiom of the following form: + * Length(Concat(x, y)) = Length(x) + Length(y) + */ + void theory_str::instantiate_concat_axiom(enode * cat) { + app * a_cat = cat->get_owner(); + SASSERT(u.str.is_concat(a_cat)); - TRACE("str", tout << "new_nn1 = " << mk_ismt2_pp(new_nn1, m) << std::endl - << "new_nn2 = " << mk_ismt2_pp(new_nn2, m) << std::endl;); + ast_manager & m = get_manager(); - if (new_nn1 == new_nn2) { - TRACE("str", tout << "equal concats, return" << std::endl;); - return; + TRACE("str", tout << "instantiating concat axiom for " << mk_ismt2_pp(a_cat, m) << std::endl;); + + // build LHS + expr_ref len_xy(m); + len_xy = mk_strlen(a_cat); + SASSERT(len_xy); + + // build RHS: start by extracting x and y from Concat(x, y) + unsigned nArgs = a_cat->get_num_args(); + SASSERT(nArgs == 2); + app * a_x = to_app(a_cat->get_arg(0)); + app * a_y = to_app(a_cat->get_arg(1)); + + expr_ref len_x(m); + len_x = mk_strlen(a_x); + SASSERT(len_x); + + expr_ref len_y(m); + len_y = mk_strlen(a_y); + SASSERT(len_y); + + // now build len_x + len_y + expr_ref len_x_plus_len_y(m); + len_x_plus_len_y = m_autil.mk_add(len_x, len_y); + SASSERT(len_x_plus_len_y); + + // finally assert equality between the two subexpressions + app * eq = m.mk_eq(len_xy, len_x_plus_len_y); + SASSERT(eq); + assert_axiom(eq); } - if (!can_two_nodes_eq(new_nn1, new_nn2)) { - expr_ref detected(m.mk_not(ctx.mk_eq_atom(new_nn1, new_nn2)), m); - TRACE("str", tout << "inconsistency detected: " << mk_ismt2_pp(detected, m) << std::endl;); - assert_axiom(detected); - return; + /* + * Add axioms that are true for any string variable: + * 1. Length(x) >= 0 + * 2. Length(x) == 0 <=> x == "" + * If the term is a string constant, we can assert something stronger: + * Length(x) == strlen(x) + */ + void theory_str::instantiate_basic_string_axioms(enode * str) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + TRACE("str", tout << "set up basic string axioms on " << mk_pp(str->get_owner(), m) << std::endl;); + + // TESTING: attempt to avoid a crash here when a variable goes out of scope + if (str->get_iscope_lvl() > ctx.get_scope_level()) { + TRACE("str", tout << "WARNING: skipping axiom setup on out-of-scope string term" << std::endl;); + return; + } + + // generate a stronger axiom for constant strings + app * a_str = str->get_owner(); + if (u.str.is_string(a_str)) { + expr_ref len_str(m); + len_str = mk_strlen(a_str); + SASSERT(len_str); + + zstring strconst; + u.str.is_string(str->get_owner(), strconst); + TRACE("str", tout << "instantiating constant string axioms for \"" << strconst.encode().c_str() << "\"" << std::endl;); + unsigned int l = strconst.length(); + expr_ref len(m_autil.mk_numeral(rational(l), true), m); + + literal lit(mk_eq(len_str, len, false)); + ctx.mark_as_relevant(lit); + ctx.mk_th_axiom(get_id(), 1, &lit); + } else { + // build axiom 1: Length(a_str) >= 0 + { + // build LHS + expr_ref len_str(m); + len_str = mk_strlen(a_str); + SASSERT(len_str); + // build RHS + expr_ref zero(m); + zero = m_autil.mk_numeral(rational(0), true); + SASSERT(zero); + // build LHS >= RHS and assert + app * lhs_ge_rhs = m_autil.mk_ge(len_str, zero); + SASSERT(lhs_ge_rhs); + TRACE("str", tout << "string axiom 1: " << mk_ismt2_pp(lhs_ge_rhs, m) << std::endl;); + assert_axiom(lhs_ge_rhs); + } + + // build axiom 2: Length(a_str) == 0 <=> a_str == "" + { + // build LHS of iff + expr_ref len_str(m); + len_str = mk_strlen(a_str); + SASSERT(len_str); + expr_ref zero(m); + zero = m_autil.mk_numeral(rational(0), true); + SASSERT(zero); + expr_ref lhs(m); + lhs = ctx.mk_eq_atom(len_str, zero); + SASSERT(lhs); + // build RHS of iff + expr_ref empty_str(m); + empty_str = mk_string(""); + SASSERT(empty_str); + expr_ref rhs(m); + rhs = ctx.mk_eq_atom(a_str, empty_str); + SASSERT(rhs); + // build LHS <=> RHS and assert + TRACE("str", tout << "string axiom 2: " << mk_ismt2_pp(lhs, m) << " <=> " << mk_ismt2_pp(rhs, m) << std::endl;); + literal l(mk_eq(lhs, rhs, true)); + ctx.mark_as_relevant(l); + ctx.mk_th_axiom(get_id(), 1, &l); + } + + } } - // check whether new_nn1 and new_nn2 are still concats + /* + * Add an axiom of the form: + * (lhs == rhs) -> ( Length(lhs) == Length(rhs) ) + */ + void theory_str::instantiate_str_eq_length_axiom(enode * lhs, enode * rhs) { + context & ctx = get_context(); + ast_manager & m = get_manager(); - bool n1IsConcat = u.str.is_concat(a_new_nn1); - bool n2IsConcat = u.str.is_concat(a_new_nn2); - if (!n1IsConcat && n2IsConcat) { - TRACE("str", tout << "nn1_new is not a concat" << std::endl;); - if (u.str.is_string(a_new_nn1)) { - simplify_parent(new_nn2, new_nn1); - } - return; - } else if (n1IsConcat && !n2IsConcat) { - TRACE("str", tout << "nn2_new is not a concat" << std::endl;); - if (u.str.is_string(a_new_nn2)) { - simplify_parent(new_nn1, new_nn2); - } - return; - } else if (!n1IsConcat && !n2IsConcat) { - // normally this should never happen, because group_terms_by_eqc() should have pre-simplified - // as much as possible. however, we make a defensive check here just in case - TRACE("str", tout << "WARNING: nn1_new and nn2_new both simplify to non-concat terms" << std::endl;); - return; - } + app * a_lhs = lhs->get_owner(); + app * a_rhs = rhs->get_owner(); - expr * v1_arg0 = a_new_nn1->get_arg(0); - expr * v1_arg1 = a_new_nn1->get_arg(1); - expr * v2_arg0 = a_new_nn2->get_arg(0); - expr * v2_arg1 = a_new_nn2->get_arg(1); + // build premise: (lhs == rhs) + expr_ref premise(ctx.mk_eq_atom(a_lhs, a_rhs), m); - if (!in_same_eqc(new_nn1, new_nn2) && (nn1 != new_nn1 || nn2 != new_nn2)) { - int ii4 = 0; - expr* item[3]; - if (nn1 != new_nn1) { - item[ii4++] = ctx.mk_eq_atom(nn1, new_nn1); - } - if (nn2 != new_nn2) { - item[ii4++] = ctx.mk_eq_atom(nn2, new_nn2); - } - item[ii4++] = ctx.mk_eq_atom(nn1, nn2); - expr_ref premise(m.mk_and(ii4, item), m); - expr_ref conclusion(ctx.mk_eq_atom(new_nn1, new_nn2), m); + // build conclusion: ( Length(lhs) == Length(rhs) ) + expr_ref len_lhs(mk_strlen(a_lhs), m); + SASSERT(len_lhs); + expr_ref len_rhs(mk_strlen(a_rhs), m); + SASSERT(len_rhs); + expr_ref conclusion(ctx.mk_eq_atom(len_lhs, len_rhs), m); + + TRACE("str", tout << "string-eq length-eq axiom: " + << mk_ismt2_pp(premise, m) << " -> " << mk_ismt2_pp(conclusion, m) << std::endl;); assert_implication(premise, conclusion); } - // start to split both concats - check_and_init_cut_var(v1_arg0); - check_and_init_cut_var(v1_arg1); - check_and_init_cut_var(v2_arg0); - check_and_init_cut_var(v2_arg1); + void theory_str::instantiate_axiom_CharAt(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); - //************************************************************* - // case 1: concat(x, y) = concat(m, n) - //************************************************************* - if (is_concat_eq_type1(new_nn1, new_nn2)) { - process_concat_eq_type1(new_nn1, new_nn2); - return; + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("str", tout << "already set up CharAt axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); + + TRACE("str", tout << "instantiate CharAt axiom for " << mk_pp(expr, m) << std::endl;); + + expr_ref ts0(mk_str_var("ts0"), m); + expr_ref ts1(mk_str_var("ts1"), m); + expr_ref ts2(mk_str_var("ts2"), m); + + expr_ref cond(m.mk_and( + m_autil.mk_ge(expr->get_arg(1), mk_int(0)), + // REWRITE for arithmetic theory: + // m_autil.mk_lt(expr->get_arg(1), mk_strlen(expr->get_arg(0))) + m.mk_not(m_autil.mk_ge(m_autil.mk_add(expr->get_arg(1), m_autil.mk_mul(mk_int(-1), mk_strlen(expr->get_arg(0)))), mk_int(0))) + ), m); + + expr_ref_vector and_item(m); + and_item.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(ts0, mk_concat(ts1, ts2)))); + and_item.push_back(ctx.mk_eq_atom(expr->get_arg(1), mk_strlen(ts0))); + and_item.push_back(ctx.mk_eq_atom(mk_strlen(ts1), mk_int(1))); + + expr_ref thenBranch(m.mk_and(and_item.size(), and_item.c_ptr()), m); + expr_ref elseBranch(ctx.mk_eq_atom(ts1, mk_string("")), m); + + expr_ref axiom(m.mk_ite(cond, thenBranch, elseBranch), m); + expr_ref reductionVar(ctx.mk_eq_atom(expr, ts1), m); + + SASSERT(axiom); + SASSERT(reductionVar); + + expr_ref finalAxiom(m.mk_and(axiom, reductionVar), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); } - //************************************************************* - // case 2: concat(x, y) = concat(m, "str") - //************************************************************* - if (is_concat_eq_type2(new_nn1, new_nn2)) { - process_concat_eq_type2(new_nn1, new_nn2); - return; + void theory_str::instantiate_axiom_prefixof(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("str", tout << "already set up prefixof axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); + + TRACE("str", tout << "instantiate prefixof axiom for " << mk_pp(expr, m) << std::endl;); + + expr_ref ts0(mk_str_var("ts0"), m); + expr_ref ts1(mk_str_var("ts1"), m); + + expr_ref_vector innerItems(m); + innerItems.push_back(ctx.mk_eq_atom(expr->get_arg(1), mk_concat(ts0, ts1))); + innerItems.push_back(ctx.mk_eq_atom(mk_strlen(ts0), mk_strlen(expr->get_arg(0)))); + innerItems.push_back(m.mk_ite(ctx.mk_eq_atom(ts0, expr->get_arg(0)), expr, m.mk_not(expr))); + expr_ref then1(m.mk_and(innerItems.size(), innerItems.c_ptr()), m); + SASSERT(then1); + + // the top-level condition is Length(arg0) >= Length(arg1) + expr_ref topLevelCond( + m_autil.mk_ge( + m_autil.mk_add( + mk_strlen(expr->get_arg(1)), m_autil.mk_mul(mk_int(-1), mk_strlen(expr->get_arg(0)))), + mk_int(0)) + , m); + SASSERT(topLevelCond); + + expr_ref finalAxiom(m.mk_ite(topLevelCond, then1, m.mk_not(expr)), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); } - //************************************************************* - // case 3: concat(x, y) = concat("str", n) - //************************************************************* - if (is_concat_eq_type3(new_nn1, new_nn2)) { - process_concat_eq_type3(new_nn1, new_nn2); - return; + void theory_str::instantiate_axiom_suffixof(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("str", tout << "already set up suffixof axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); + + TRACE("str", tout << "instantiate suffixof axiom for " << mk_pp(expr, m) << std::endl;); + + expr_ref ts0(mk_str_var("ts0"), m); + expr_ref ts1(mk_str_var("ts1"), m); + + expr_ref_vector innerItems(m); + innerItems.push_back(ctx.mk_eq_atom(expr->get_arg(1), mk_concat(ts0, ts1))); + innerItems.push_back(ctx.mk_eq_atom(mk_strlen(ts1), mk_strlen(expr->get_arg(0)))); + innerItems.push_back(m.mk_ite(ctx.mk_eq_atom(ts1, expr->get_arg(0)), expr, m.mk_not(expr))); + expr_ref then1(m.mk_and(innerItems.size(), innerItems.c_ptr()), m); + SASSERT(then1); + + // the top-level condition is Length(arg0) >= Length(arg1) + expr_ref topLevelCond( + m_autil.mk_ge( + m_autil.mk_add( + mk_strlen(expr->get_arg(1)), m_autil.mk_mul(mk_int(-1), mk_strlen(expr->get_arg(0)))), + mk_int(0)) + , m); + SASSERT(topLevelCond); + + expr_ref finalAxiom(m.mk_ite(topLevelCond, then1, m.mk_not(expr)), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); } - //************************************************************* - // case 4: concat("str1", y) = concat("str2", n) - //************************************************************* - if (is_concat_eq_type4(new_nn1, new_nn2)) { - process_concat_eq_type4(new_nn1, new_nn2); - return; + void theory_str::instantiate_axiom_Contains(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * ex = e->get_owner(); + if (axiomatized_terms.contains(ex)) { + TRACE("str", tout << "already set up Contains axiom for " << mk_pp(ex, m) << std::endl;); + return; + } + axiomatized_terms.insert(ex); + + // quick path, because this is necessary due to rewriter behaviour + // at minimum it should fix z3str/concat-006.smt2 + zstring haystackStr, needleStr; + if (u.str.is_string(ex->get_arg(0), haystackStr) && u.str.is_string(ex->get_arg(1), needleStr)) { + TRACE("str", tout << "eval constant Contains term " << mk_pp(ex, m) << std::endl;); + if (haystackStr.contains(needleStr)) { + assert_axiom(ex); + } else { + assert_axiom(m.mk_not(ex)); + } + return; + } + + { // register Contains() + expr * str = ex->get_arg(0); + expr * substr = ex->get_arg(1); + contains_map.push_back(ex); + std::pair key = std::pair(str, substr); + contain_pair_bool_map.insert(str, substr, ex); + contain_pair_idx_map[str].insert(key); + contain_pair_idx_map[substr].insert(key); + } + + TRACE("str", tout << "instantiate Contains axiom for " << mk_pp(ex, m) << std::endl;); + + expr_ref ts0(mk_str_var("ts0"), m); + expr_ref ts1(mk_str_var("ts1"), m); + + expr_ref breakdownAssert(ctx.mk_eq_atom(ex, ctx.mk_eq_atom(ex->get_arg(0), mk_concat(ts0, mk_concat(ex->get_arg(1), ts1)))), m); + SASSERT(breakdownAssert); + assert_axiom(breakdownAssert); } - //************************************************************* - // case 5: concat(x, "str1") = concat(m, "str2") - //************************************************************* - if (is_concat_eq_type5(new_nn1, new_nn2)) { - process_concat_eq_type5(new_nn1, new_nn2); - return; - } - //************************************************************* - // case 6: concat("str1", y) = concat(m, "str2") - //************************************************************* - if (is_concat_eq_type6(new_nn1, new_nn2)) { - process_concat_eq_type6(new_nn1, new_nn2); - return; + void theory_str::instantiate_axiom_Indexof(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("str", tout << "already set up Indexof axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); + + TRACE("str", tout << "instantiate Indexof axiom for " << mk_pp(expr, m) << std::endl;); + + expr_ref x1(mk_str_var("x1"), m); + expr_ref x2(mk_str_var("x2"), m); + expr_ref indexAst(mk_int_var("index"), m); + + expr_ref condAst(mk_contains(expr->get_arg(0), expr->get_arg(1)), m); + SASSERT(condAst); + + // ----------------------- + // true branch + expr_ref_vector thenItems(m); + // args[0] = x1 . args[1] . x2 + thenItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x1, mk_concat(expr->get_arg(1), x2)))); + // indexAst = |x1| + thenItems.push_back(ctx.mk_eq_atom(indexAst, mk_strlen(x1))); + // args[0] = x3 . x4 + // /\ |x3| = |x1| + |args[1]| - 1 + // /\ ! contains(x3, args[1]) + expr_ref x3(mk_str_var("x3"), m); + expr_ref x4(mk_str_var("x4"), m); + expr_ref tmpLen(m_autil.mk_add(indexAst, mk_strlen(expr->get_arg(1)), mk_int(-1)), m); + SASSERT(tmpLen); + thenItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x3, x4))); + thenItems.push_back(ctx.mk_eq_atom(mk_strlen(x3), tmpLen)); + thenItems.push_back(m.mk_not(mk_contains(x3, expr->get_arg(1)))); + expr_ref thenBranch(m.mk_and(thenItems.size(), thenItems.c_ptr()), m); + SASSERT(thenBranch); + + // ----------------------- + // false branch + expr_ref elseBranch(ctx.mk_eq_atom(indexAst, mk_int(-1)), m); + SASSERT(elseBranch); + + expr_ref breakdownAssert(m.mk_ite(condAst, thenBranch, elseBranch), m); + SASSERT(breakdownAssert); + + expr_ref reduceToIndex(ctx.mk_eq_atom(expr, indexAst), m); + SASSERT(reduceToIndex); + + expr_ref finalAxiom(m.mk_and(breakdownAssert, reduceToIndex), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); } -} + void theory_str::instantiate_axiom_Indexof2(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); -/* - * Returns true if attempting to process a concat equality between lhs and rhs - * will result in overlapping variables (false otherwise). - */ -bool theory_str::will_result_in_overlap(expr * lhs, expr * rhs) { - ast_manager & m = get_manager(); + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("str", tout << "already set up Indexof2 axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); - expr_ref new_nn1(simplify_concat(lhs), m); - expr_ref new_nn2(simplify_concat(rhs), m); - app * a_new_nn1 = to_app(new_nn1); - app * a_new_nn2 = to_app(new_nn2); + TRACE("str", tout << "instantiate Indexof2 axiom for " << mk_pp(expr, m) << std::endl;); - bool n1IsConcat = u.str.is_concat(a_new_nn1); - bool n2IsConcat = u.str.is_concat(a_new_nn2); - if (!n1IsConcat && !n2IsConcat) { - // we simplified both sides to non-concat expressions... - return false; + // ------------------------------------------------------------------------------- + // if (arg[2] >= length(arg[0])) // ite2 + // resAst = -1 + // else + // args[0] = prefix . suffix + // /\ indexAst = indexof(suffix, arg[1]) + // /\ args[2] = len(prefix) + // /\ if (indexAst == -1) resAst = indexAst // ite3 + // else resAst = args[2] + indexAst + // ------------------------------------------------------------------------------- + + expr_ref resAst(mk_int_var("res"), m); + expr_ref indexAst(mk_int_var("index"), m); + expr_ref prefix(mk_str_var("prefix"), m); + expr_ref suffix(mk_str_var("suffix"), m); + expr_ref prefixLen(mk_strlen(prefix), m); + expr_ref zeroAst(mk_int(0), m); + expr_ref negOneAst(mk_int(-1), m); + + expr_ref ite3(m.mk_ite( + ctx.mk_eq_atom(indexAst, negOneAst), + ctx.mk_eq_atom(resAst, negOneAst), + ctx.mk_eq_atom(resAst, m_autil.mk_add(expr->get_arg(2), indexAst)) + ),m); + + expr_ref_vector ite2ElseItems(m); + ite2ElseItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(prefix, suffix))); + ite2ElseItems.push_back(ctx.mk_eq_atom(indexAst, mk_indexof(suffix, expr->get_arg(1)))); + ite2ElseItems.push_back(ctx.mk_eq_atom(expr->get_arg(2), prefixLen)); + ite2ElseItems.push_back(ite3); + expr_ref ite2Else(m.mk_and(ite2ElseItems.size(), ite2ElseItems.c_ptr()), m); + SASSERT(ite2Else); + + expr_ref ite2(m.mk_ite( + //m_autil.mk_ge(expr->get_arg(2), mk_strlen(expr->get_arg(0))), + m_autil.mk_ge(m_autil.mk_add(expr->get_arg(2), m_autil.mk_mul(mk_int(-1), mk_strlen(expr->get_arg(0)))), zeroAst), + ctx.mk_eq_atom(resAst, negOneAst), + ite2Else + ), m); + SASSERT(ite2); + + expr_ref ite1(m.mk_ite( + //m_autil.mk_lt(expr->get_arg(2), zeroAst), + m.mk_not(m_autil.mk_ge(expr->get_arg(2), zeroAst)), + ctx.mk_eq_atom(resAst, mk_indexof(expr->get_arg(0), expr->get_arg(1))), + ite2 + ), m); + SASSERT(ite1); + assert_axiom(ite1); + + expr_ref reduceTerm(ctx.mk_eq_atom(expr, resAst), m); + SASSERT(reduceTerm); + assert_axiom(reduceTerm); } - expr * v1_arg0 = a_new_nn1->get_arg(0); - expr * v1_arg1 = a_new_nn1->get_arg(1); - expr * v2_arg0 = a_new_nn2->get_arg(0); - expr * v2_arg1 = a_new_nn2->get_arg(1); + void theory_str::instantiate_axiom_LastIndexof(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); - TRACE("str", tout << "checking whether " << mk_pp(new_nn1, m) << " and " << mk_pp(new_nn1, m) << " might overlap." << std::endl;); + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("str", tout << "already set up LastIndexof axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); - check_and_init_cut_var(v1_arg0); - check_and_init_cut_var(v1_arg1); - check_and_init_cut_var(v2_arg0); - check_and_init_cut_var(v2_arg1); + TRACE("str", tout << "instantiate LastIndexof axiom for " << mk_pp(expr, m) << std::endl;); - //************************************************************* - // case 1: concat(x, y) = concat(m, n) - //************************************************************* - if (is_concat_eq_type1(new_nn1, new_nn2)) { - TRACE("str", tout << "Type 1 check." << std::endl;); - expr * x = to_app(new_nn1)->get_arg(0); - expr * y = to_app(new_nn1)->get_arg(1); - expr * m = to_app(new_nn2)->get_arg(0); - expr * n = to_app(new_nn2)->get_arg(1); + expr_ref x1(mk_str_var("x1"), m); + expr_ref x2(mk_str_var("x2"), m); + expr_ref indexAst(mk_int_var("index"), m); + expr_ref_vector items(m); - if (has_self_cut(m, y)) { - TRACE("str", tout << "Possible overlap found" << std::endl; print_cut_var(m, tout); print_cut_var(y, tout);); - return true; - } else if (has_self_cut(x, n)) { - TRACE("str", tout << "Possible overlap found" << std::endl; print_cut_var(x, tout); print_cut_var(n, tout);); - return true; - } else { - return false; + // args[0] = x1 . args[1] . x2 + expr_ref eq1(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x1, mk_concat(expr->get_arg(1), x2))), m); + expr_ref arg0HasArg1(mk_contains(expr->get_arg(0), expr->get_arg(1)), m); // arg0HasArg1 = Contains(args[0], args[1]) + items.push_back(ctx.mk_eq_atom(arg0HasArg1, eq1)); + + + expr_ref condAst(arg0HasArg1, m); + //---------------------------- + // true branch + expr_ref_vector thenItems(m); + thenItems.push_back(m_autil.mk_ge(indexAst, mk_int(0))); + // args[0] = x1 . args[1] . x2 + // x1 doesn't contain args[1] + thenItems.push_back(m.mk_not(mk_contains(x2, expr->get_arg(1)))); + thenItems.push_back(ctx.mk_eq_atom(indexAst, mk_strlen(x1))); + + bool canSkip = false; + zstring arg1Str; + if (u.str.is_string(expr->get_arg(1), arg1Str)) { + if (arg1Str.length() == 1) { + canSkip = true; + } + } + + if (!canSkip) { + // args[0] = x3 . x4 /\ |x3| = |x1| + 1 /\ ! contains(x4, args[1]) + expr_ref x3(mk_str_var("x3"), m); + expr_ref x4(mk_str_var("x4"), m); + expr_ref tmpLen(m_autil.mk_add(indexAst, mk_int(1)), m); + thenItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x3, x4))); + thenItems.push_back(ctx.mk_eq_atom(mk_strlen(x3), tmpLen)); + thenItems.push_back(m.mk_not(mk_contains(x4, expr->get_arg(1)))); + } + //---------------------------- + // else branch + expr_ref_vector elseItems(m); + elseItems.push_back(ctx.mk_eq_atom(indexAst, mk_int(-1))); + + items.push_back(m.mk_ite(condAst, m.mk_and(thenItems.size(), thenItems.c_ptr()), m.mk_and(elseItems.size(), elseItems.c_ptr()))); + + expr_ref breakdownAssert(m.mk_and(items.size(), items.c_ptr()), m); + SASSERT(breakdownAssert); + + expr_ref reduceToIndex(ctx.mk_eq_atom(expr, indexAst), m); + SASSERT(reduceToIndex); + + expr_ref finalAxiom(m.mk_and(breakdownAssert, reduceToIndex), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); + } + + void theory_str::instantiate_axiom_Substr(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("str", tout << "already set up Substr axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); + + TRACE("str", tout << "instantiate Substr axiom for " << mk_pp(expr, m) << std::endl;); + + expr_ref substrBase(expr->get_arg(0), m); + expr_ref substrPos(expr->get_arg(1), m); + expr_ref substrLen(expr->get_arg(2), m); + SASSERT(substrBase); + SASSERT(substrPos); + SASSERT(substrLen); + + expr_ref zero(m_autil.mk_numeral(rational::zero(), true), m); + expr_ref minusOne(m_autil.mk_numeral(rational::minus_one(), true), m); + SASSERT(zero); + SASSERT(minusOne); + + expr_ref_vector argumentsValid_terms(m); + // pos >= 0 + argumentsValid_terms.push_back(m_autil.mk_ge(substrPos, zero)); + // pos < strlen(base) + // --> pos + -1*strlen(base) < 0 + argumentsValid_terms.push_back(m.mk_not(m_autil.mk_ge( + m_autil.mk_add(substrPos, m_autil.mk_mul(minusOne, substrLen)), + zero))); + // len >= 0 + argumentsValid_terms.push_back(m_autil.mk_ge(substrLen, zero)); + + expr_ref argumentsValid(mk_and(argumentsValid_terms), m); + SASSERT(argumentsValid); + ctx.internalize(argumentsValid, false); + + // (pos+len) >= strlen(base) + // --> pos + len + -1*strlen(base) >= 0 + expr_ref lenOutOfBounds(m_autil.mk_ge( + m_autil.mk_add(substrPos, substrLen, m_autil.mk_mul(minusOne, mk_strlen(substrBase))), + zero), m); + SASSERT(lenOutOfBounds); + ctx.internalize(argumentsValid, false); + + // Case 1: pos < 0 or pos >= strlen(base) or len < 0 + // ==> (Substr ...) = "" + expr_ref case1_premise(m.mk_not(argumentsValid), m); + SASSERT(case1_premise); + ctx.internalize(case1_premise, false); + expr_ref case1_conclusion(ctx.mk_eq_atom(expr, mk_string("")), m); + SASSERT(case1_conclusion); + ctx.internalize(case1_conclusion, false); + expr_ref case1(rewrite_implication(case1_premise, case1_conclusion), m); + SASSERT(case1); + + // Case 2: (pos >= 0 and pos < strlen(base) and len >= 0) and (pos+len) >= strlen(base) + // ==> base = t0.t1 AND len(t0) = pos AND (Substr ...) = t1 + expr_ref t0(mk_str_var("t0"), m); + expr_ref t1(mk_str_var("t1"), m); + expr_ref case2_conclusion(m.mk_and( + ctx.mk_eq_atom(substrBase, mk_concat(t0,t1)), + ctx.mk_eq_atom(mk_strlen(t0), substrPos), + ctx.mk_eq_atom(expr, t1)), m); + expr_ref case2(rewrite_implication(m.mk_and(argumentsValid, lenOutOfBounds), case2_conclusion), m); + SASSERT(case2); + + // Case 3: (pos >= 0 and pos < strlen(base) and len >= 0) and (pos+len) < strlen(base) + // ==> base = t2.t3.t4 AND len(t2) = pos AND len(t3) = len AND (Substr ...) = t3 + expr_ref t2(mk_str_var("t2"), m); + expr_ref t3(mk_str_var("t3"), m); + expr_ref t4(mk_str_var("t4"), m); + expr_ref_vector case3_conclusion_terms(m); + case3_conclusion_terms.push_back(ctx.mk_eq_atom(substrBase, mk_concat(t2, mk_concat(t3, t4)))); + case3_conclusion_terms.push_back(ctx.mk_eq_atom(mk_strlen(t2), substrPos)); + case3_conclusion_terms.push_back(ctx.mk_eq_atom(mk_strlen(t3), substrLen)); + case3_conclusion_terms.push_back(ctx.mk_eq_atom(expr, t3)); + expr_ref case3_conclusion(mk_and(case3_conclusion_terms), m); + expr_ref case3(rewrite_implication(m.mk_and(argumentsValid, m.mk_not(lenOutOfBounds)), case3_conclusion), m); + SASSERT(case3); + + ctx.internalize(case1, false); + ctx.internalize(case2, false); + ctx.internalize(case3, false); + + expr_ref finalAxiom(m.mk_and(case1, case2, case3), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); + } + + void theory_str::instantiate_axiom_Replace(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * expr = e->get_owner(); + if (axiomatized_terms.contains(expr)) { + TRACE("str", tout << "already set up Replace axiom for " << mk_pp(expr, m) << std::endl;); + return; + } + axiomatized_terms.insert(expr); + + TRACE("str", tout << "instantiate Replace axiom for " << mk_pp(expr, m) << std::endl;); + + expr_ref x1(mk_str_var("x1"), m); + expr_ref x2(mk_str_var("x2"), m); + expr_ref i1(mk_int_var("i1"), m); + expr_ref result(mk_str_var("result"), m); + + // condAst = Contains(args[0], args[1]) + expr_ref condAst(mk_contains(expr->get_arg(0), expr->get_arg(1)), m); + // ----------------------- + // true branch + expr_ref_vector thenItems(m); + // args[0] = x1 . args[1] . x2 + thenItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x1, mk_concat(expr->get_arg(1), x2)))); + // i1 = |x1| + thenItems.push_back(ctx.mk_eq_atom(i1, mk_strlen(x1))); + // args[0] = x3 . x4 /\ |x3| = |x1| + |args[1]| - 1 /\ ! contains(x3, args[1]) + expr_ref x3(mk_str_var("x3"), m); + expr_ref x4(mk_str_var("x4"), m); + expr_ref tmpLen(m_autil.mk_add(i1, mk_strlen(expr->get_arg(1)), mk_int(-1)), m); + thenItems.push_back(ctx.mk_eq_atom(expr->get_arg(0), mk_concat(x3, x4))); + thenItems.push_back(ctx.mk_eq_atom(mk_strlen(x3), tmpLen)); + thenItems.push_back(m.mk_not(mk_contains(x3, expr->get_arg(1)))); + thenItems.push_back(ctx.mk_eq_atom(result, mk_concat(x1, mk_concat(expr->get_arg(2), x2)))); + // ----------------------- + // false branch + expr_ref elseBranch(ctx.mk_eq_atom(result, expr->get_arg(0)), m); + + expr_ref breakdownAssert(m.mk_ite(condAst, m.mk_and(thenItems.size(), thenItems.c_ptr()), elseBranch), m); + SASSERT(breakdownAssert); + + expr_ref reduceToResult(ctx.mk_eq_atom(expr, result), m); + SASSERT(reduceToResult); + + expr_ref finalAxiom(m.mk_and(breakdownAssert, reduceToResult), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); + } + + void theory_str::instantiate_axiom_str_to_int(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + app * ex = e->get_owner(); + if (axiomatized_terms.contains(ex)) { + TRACE("str", tout << "already set up str.to-int axiom for " << mk_pp(ex, m) << std::endl;); + return; + } + axiomatized_terms.insert(ex); + + TRACE("str", tout << "instantiate str.to-int axiom for " << mk_pp(ex, m) << std::endl;); + + // let expr = (str.to-int S) + // axiom 1: expr >= -1 + // axiom 2: expr = 0 <==> S = "0" + // axiom 3: expr >= 1 ==> len(S) > 0 AND S[0] != "0" + + expr * S = ex->get_arg(0); + { + expr_ref axiom1(m_autil.mk_ge(ex, m_autil.mk_numeral(rational::minus_one(), true)), m); + SASSERT(axiom1); + assert_axiom(axiom1); + } + + { + expr_ref lhs(ctx.mk_eq_atom(ex, m_autil.mk_numeral(rational::zero(), true)), m); + expr_ref rhs(ctx.mk_eq_atom(S, mk_string("0")), m); + expr_ref axiom2(ctx.mk_eq_atom(lhs, rhs), m); + SASSERT(axiom2); + assert_axiom(axiom2); + } + + { + expr_ref premise(m_autil.mk_ge(ex, m_autil.mk_numeral(rational::one(), true)), m); + expr_ref hd(mk_str_var("hd"), m); + expr_ref tl(mk_str_var("tl"), m); + expr_ref conclusion1(ctx.mk_eq_atom(S, mk_concat(hd, tl)), m); + expr_ref conclusion2(ctx.mk_eq_atom(mk_strlen(hd), m_autil.mk_numeral(rational::one(), true)), m); + expr_ref conclusion3(m.mk_not(ctx.mk_eq_atom(hd, mk_string("0"))), m); + expr_ref conclusion(m.mk_and(conclusion1, conclusion2, conclusion3), m); + SASSERT(premise); + SASSERT(conclusion); + assert_implication(premise, conclusion); } } - //************************************************************* - // case 2: concat(x, y) = concat(m, "str") - //************************************************************* - if (is_concat_eq_type2(new_nn1, new_nn2)) { + void theory_str::instantiate_axiom_int_to_str(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); - expr * y = NULL; - expr * m = NULL; - expr * v1_arg0 = to_app(new_nn1)->get_arg(0); - expr * v1_arg1 = to_app(new_nn1)->get_arg(1); - expr * v2_arg0 = to_app(new_nn2)->get_arg(0); - expr * v2_arg1 = to_app(new_nn2)->get_arg(1); - - if (u.str.is_string(v1_arg1) && !u.str.is_string(v2_arg1)) { - m = v1_arg0; - y = v2_arg1; - } else { - m = v2_arg0; - y = v1_arg1; + app * ex = e->get_owner(); + if (axiomatized_terms.contains(ex)) { + TRACE("str", tout << "already set up str.from-int axiom for " << mk_pp(ex, m) << std::endl;); + return; } + axiomatized_terms.insert(ex); - if (has_self_cut(m, y)) { - TRACE("str", tout << "Possible overlap found" << std::endl; print_cut_var(m, tout); print_cut_var(y, tout);); - return true; - } else { - return false; + TRACE("str", tout << "instantiate str.from-int axiom for " << mk_pp(ex, m) << std::endl;); + + // axiom 1: N < 0 <==> (str.from-int N) = "" + expr * N = ex->get_arg(0); + { + expr_ref axiom1_lhs(m.mk_not(m_autil.mk_ge(N, m_autil.mk_numeral(rational::zero(), true))), m); + expr_ref axiom1_rhs(ctx.mk_eq_atom(ex, mk_string("")), m); + expr_ref axiom1(ctx.mk_eq_atom(axiom1_lhs, axiom1_rhs), m); + SASSERT(axiom1); + assert_axiom(axiom1); } } - //************************************************************* - // case 3: concat(x, y) = concat("str", n) - //************************************************************* - if (is_concat_eq_type3(new_nn1, new_nn2)) { - expr * v1_arg0 = to_app(new_nn1)->get_arg(0); - expr * v1_arg1 = to_app(new_nn1)->get_arg(1); - expr * v2_arg0 = to_app(new_nn2)->get_arg(0); - expr * v2_arg1 = to_app(new_nn2)->get_arg(1); + expr * theory_str::mk_RegexIn(expr * str, expr * regexp) { + app * regexIn = u.re.mk_in_re(str, regexp); + // immediately force internalization so that axiom setup does not fail + get_context().internalize(regexIn, false); + set_up_axioms(regexIn); + return regexIn; + } - expr * x = NULL; - expr * n = NULL; - - if (u.str.is_string(v1_arg0) && !u.str.is_string(v2_arg0)) { - n = v1_arg1; - x = v2_arg0; - } else { - n = v2_arg1; - x = v1_arg0; + static zstring str2RegexStr(zstring str) { + zstring res(""); + int len = str.length(); + for (int i = 0; i < len; i++) { + char nc = str[i]; + // 12 special chars + if (nc == '\\' || nc == '^' || nc == '$' || nc == '.' || nc == '|' || nc == '?' + || nc == '*' || nc == '+' || nc == '(' || nc == ')' || nc == '[' || nc == '{') { + res = res + zstring("\\"); + } + char tmp[2] = {(char)str[i], '\0'}; + res = res + zstring(tmp); } - if (has_self_cut(x, n)) { - TRACE("str", tout << "Possible overlap found" << std::endl; print_cut_var(x, tout); print_cut_var(n, tout);); - return true; + return res; + } + + zstring theory_str::get_std_regex_str(expr * regex) { + app * a_regex = to_app(regex); + if (u.re.is_to_re(a_regex)) { + expr * regAst = a_regex->get_arg(0); + zstring regAstVal; + u.str.is_string(regAst, regAstVal); + zstring regStr = str2RegexStr(regAstVal); + return regStr; + } else if (u.re.is_concat(a_regex)) { + expr * reg1Ast = a_regex->get_arg(0); + expr * reg2Ast = a_regex->get_arg(1); + zstring reg1Str = get_std_regex_str(reg1Ast); + zstring reg2Str = get_std_regex_str(reg2Ast); + return zstring("(") + reg1Str + zstring(")(") + reg2Str + zstring(")"); + } else if (u.re.is_union(a_regex)) { + expr * reg1Ast = a_regex->get_arg(0); + expr * reg2Ast = a_regex->get_arg(1); + zstring reg1Str = get_std_regex_str(reg1Ast); + zstring reg2Str = get_std_regex_str(reg2Ast); + return zstring("(") + reg1Str + zstring(")|(") + reg2Str + zstring(")"); + } else if (u.re.is_star(a_regex)) { + expr * reg1Ast = a_regex->get_arg(0); + zstring reg1Str = get_std_regex_str(reg1Ast); + return zstring("(") + reg1Str + zstring(")*"); + } else if (u.re.is_range(a_regex)) { + expr * range1 = a_regex->get_arg(0); + expr * range2 = a_regex->get_arg(1); + zstring range1val, range2val; + u.str.is_string(range1, range1val); + u.str.is_string(range2, range2val); + return zstring("[") + range1val + zstring("-") + range2val + zstring("]"); } else { - return false; + TRACE("str", tout << "BUG: unrecognized regex term " << mk_pp(regex, get_manager()) << std::endl;); + UNREACHABLE(); return zstring(""); } } - //************************************************************* - // case 4: concat("str1", y) = concat("str2", n) - //************************************************************* - if (is_concat_eq_type4(new_nn1, new_nn2)) { - // This case can never result in an overlap. - return false; - } + void theory_str::instantiate_axiom_RegexIn(enode * e) { + context & ctx = get_context(); + ast_manager & m = get_manager(); - //************************************************************* - // case 5: concat(x, "str1") = concat(m, "str2") - //************************************************************* - if (is_concat_eq_type5(new_nn1, new_nn2)) { - // This case can never result in an overlap. - return false; - } - //************************************************************* - // case 6: concat("str1", y) = concat(m, "str2") - //************************************************************* - if (is_concat_eq_type6(new_nn1, new_nn2)) { - expr * v1_arg0 = to_app(new_nn1)->get_arg(0); - expr * v1_arg1 = to_app(new_nn1)->get_arg(1); - expr * v2_arg0 = to_app(new_nn2)->get_arg(0); - expr * v2_arg1 = to_app(new_nn2)->get_arg(1); - - expr * y = NULL; - expr * m = NULL; - - if (u.str.is_string(v1_arg0)) { - y = v1_arg1; - m = v2_arg0; - } else { - y = v2_arg1; - m = v1_arg0; + app * ex = e->get_owner(); + if (axiomatized_terms.contains(ex)) { + TRACE("str", tout << "already set up RegexIn axiom for " << mk_pp(ex, m) << std::endl;); + return; } - if (has_self_cut(m, y)) { - TRACE("str", tout << "Possible overlap found" << std::endl; print_cut_var(m, tout); print_cut_var(y, tout);); - return true; + axiomatized_terms.insert(ex); + + TRACE("str", tout << "instantiate RegexIn axiom for " << mk_pp(ex, m) << std::endl;); + + { + zstring regexStr = get_std_regex_str(ex->get_arg(1)); + std::pair key1(ex->get_arg(0), regexStr); + // skip Z3str's map check, because we already check if we set up axioms on this term + regex_in_bool_map[key1] = ex; + regex_in_var_reg_str_map[ex->get_arg(0)].insert(regexStr); + } + + expr_ref str(ex->get_arg(0), m); + app * regex = to_app(ex->get_arg(1)); + + if (u.re.is_to_re(regex)) { + expr_ref rxStr(regex->get_arg(0), m); + // want to assert 'expr IFF (str == rxStr)' + expr_ref rhs(ctx.mk_eq_atom(str, rxStr), m); + expr_ref finalAxiom(m.mk_iff(ex, rhs), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); + TRACE("str", tout << "set up Str2Reg: (RegexIn " << mk_pp(str, m) << " " << mk_pp(regex, m) << ")" << std::endl;); + } else if (u.re.is_concat(regex)) { + expr_ref var1(mk_regex_rep_var(), m); + expr_ref var2(mk_regex_rep_var(), m); + expr_ref rhs(mk_concat(var1, var2), m); + expr_ref rx1(regex->get_arg(0), m); + expr_ref rx2(regex->get_arg(1), m); + expr_ref var1InRegex1(mk_RegexIn(var1, rx1), m); + expr_ref var2InRegex2(mk_RegexIn(var2, rx2), m); + + expr_ref_vector items(m); + items.push_back(var1InRegex1); + items.push_back(var2InRegex2); + items.push_back(ctx.mk_eq_atom(ex, ctx.mk_eq_atom(str, rhs))); + + expr_ref finalAxiom(mk_and(items), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); + } else if (u.re.is_union(regex)) { + expr_ref var1(mk_regex_rep_var(), m); + expr_ref var2(mk_regex_rep_var(), m); + expr_ref orVar(m.mk_or(ctx.mk_eq_atom(str, var1), ctx.mk_eq_atom(str, var2)), m); + expr_ref regex1(regex->get_arg(0), m); + expr_ref regex2(regex->get_arg(1), m); + expr_ref var1InRegex1(mk_RegexIn(var1, regex1), m); + expr_ref var2InRegex2(mk_RegexIn(var2, regex2), m); + expr_ref_vector items(m); + items.push_back(var1InRegex1); + items.push_back(var2InRegex2); + items.push_back(ctx.mk_eq_atom(ex, orVar)); + assert_axiom(mk_and(items)); + } else if (u.re.is_star(regex)) { + // slightly more complex due to the unrolling step. + expr_ref regex1(regex->get_arg(0), m); + expr_ref unrollCount(mk_unroll_bound_var(), m); + expr_ref unrollFunc(mk_unroll(regex1, unrollCount), m); + expr_ref_vector items(m); + items.push_back(ctx.mk_eq_atom(ex, ctx.mk_eq_atom(str, unrollFunc))); + items.push_back(ctx.mk_eq_atom(ctx.mk_eq_atom(unrollCount, mk_int(0)), ctx.mk_eq_atom(unrollFunc, mk_string("")))); + expr_ref finalAxiom(mk_and(items), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); + } else if (u.re.is_range(regex)) { + // (re.range "A" "Z") unfolds to (re.union "A" "B" ... "Z"); + // we rewrite to expr IFF (str = "A" or str = "B" or ... or str = "Z") + expr_ref lo(regex->get_arg(0), m); + expr_ref hi(regex->get_arg(1), m); + zstring str_lo, str_hi; + SASSERT(u.str.is_string(lo)); + SASSERT(u.str.is_string(hi)); + u.str.is_string(lo, str_lo); + u.str.is_string(hi, str_hi); + SASSERT(str_lo.length() == 1); + SASSERT(str_hi.length() == 1); + unsigned int c1 = str_lo[0]; + unsigned int c2 = str_hi[0]; + if (c1 > c2) { + // exchange + unsigned int tmp = c1; + c1 = c2; + c2 = tmp; + } + expr_ref_vector range_cases(m); + for (unsigned int ch = c1; ch <= c2; ++ch) { + zstring s_ch(ch); + expr_ref rhs(ctx.mk_eq_atom(str, u.str.mk_string(s_ch)), m); + range_cases.push_back(rhs); + } + expr_ref rhs(mk_or(range_cases), m); + expr_ref finalAxiom(m.mk_iff(ex, rhs), m); + SASSERT(finalAxiom); + assert_axiom(finalAxiom); } else { - return false; + TRACE("str", tout << "ERROR: unknown regex expression " << mk_pp(regex, m) << "!" << std::endl;); + NOT_IMPLEMENTED_YET(); } } - TRACE("str", tout << "warning: unrecognized concat case" << std::endl;); - return false; -} + void theory_str::attach_new_th_var(enode * n) { + context & ctx = get_context(); + theory_var v = mk_var(n); + ctx.attach_th_var(n, this, v); + TRACE("str", tout << "new theory var: " << mk_ismt2_pp(n->get_owner(), get_manager()) << " := v#" << v << std::endl;); + } -/************************************************************* - * Type 1: concat(x, y) = concat(m, n) - * x, y, m and n all variables - *************************************************************/ -bool theory_str::is_concat_eq_type1(expr * concatAst1, expr * concatAst2) { - expr * x = to_app(concatAst1)->get_arg(0); - expr * y = to_app(concatAst1)->get_arg(1); - expr * m = to_app(concatAst2)->get_arg(0); - expr * n = to_app(concatAst2)->get_arg(1); + void theory_str::reset_eh() { + TRACE("str", tout << "resetting" << std::endl;); + m_trail_stack.reset(); - if (!u.str.is_string(x) && !u.str.is_string(y) && !u.str.is_string(m) && !u.str.is_string(n)) { + m_basicstr_axiom_todo.reset(); + m_str_eq_todo.reset(); + m_concat_axiom_todo.reset(); + pop_scope_eh(get_context().get_scope_level()); + } + + /* + * Check equality among equivalence class members of LHS and RHS + * to discover an incorrect LHS == RHS. + * For example, if we have y2 == "str3" + * and the equivalence classes are + * { y2, (Concat ce m2) } + * { "str3", (Concat abc x2) } + * then y2 can't be equal to "str3". + * Then add an assertion: (y2 == (Concat ce m2)) AND ("str3" == (Concat abc x2)) -> (y2 != "str3") + */ + bool theory_str::new_eq_check(expr * lhs, expr * rhs) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + // skip this check if we defer consistency checking, as we can do it for every EQC in final check + if (!opt_DeferEQCConsistencyCheck) { + check_concat_len_in_eqc(lhs); + check_concat_len_in_eqc(rhs); + } + + // Now we iterate over all pairs of terms across both EQCs + // and check whether we can show that any pair of distinct terms + // cannot possibly be equal. + // If that's the case, we assert an axiom to that effect and stop. + + expr * eqc_nn1 = lhs; + do { + expr * eqc_nn2 = rhs; + do { + TRACE("str", tout << "checking whether " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " can be equal" << std::endl;); + // inconsistency check: value + if (!can_two_nodes_eq(eqc_nn1, eqc_nn2)) { + TRACE("str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " cannot be equal to " << mk_pp(eqc_nn2, m) << std::endl;); + expr_ref to_assert(m.mk_not(ctx.mk_eq_atom(eqc_nn1, eqc_nn2)), m); + assert_axiom(to_assert); + // this shouldn't use the integer theory at all, so we don't allow the option of quick-return + return false; + } + if (!check_length_consistency(eqc_nn1, eqc_nn2)) { + TRACE("str", tout << "inconsistency detected: " << mk_pp(eqc_nn1, m) << " and " << mk_pp(eqc_nn2, m) << " have inconsistent lengths" << std::endl;); + if (opt_NoQuickReturn_IntegerTheory){ + TRACE("str", tout << "continuing in new_eq_check() due to opt_NoQuickReturn_IntegerTheory" << std::endl;); + } else { + return false; + } + } + eqc_nn2 = get_eqc_next(eqc_nn2); + } while (eqc_nn2 != rhs); + eqc_nn1 = get_eqc_next(eqc_nn1); + } while (eqc_nn1 != lhs); + + if (!contains_map.empty()) { + check_contain_in_new_eq(lhs, rhs); + } + + if (!regex_in_bool_map.empty()) { + TRACE("str", tout << "checking regex consistency" << std::endl;); + check_regex_in(lhs, rhs); + } + + // okay, all checks here passed return true; - } else { + } + + // support for user_smt_theory-style EQC handling + + app * theory_str::get_ast(theory_var i) { + return get_enode(i)->get_owner(); + } + + theory_var theory_str::get_var(expr * n) const { + if (!is_app(n)) { + return null_theory_var; + } + context & ctx = get_context(); + if (ctx.e_internalized(to_app(n))) { + enode * e = ctx.get_enode(to_app(n)); + return e->get_th_var(get_id()); + } + return null_theory_var; + } + + // simulate Z3_theory_get_eqc_next() + expr * theory_str::get_eqc_next(expr * n) { + theory_var v = get_var(n); + if (v != null_theory_var) { + theory_var r = m_find.next(v); + return get_ast(r); + } + return n; + } + + void theory_str::group_terms_by_eqc(expr * n, std::set & concats, std::set & vars, std::set & consts) { + context & ctx = get_context(); + expr * eqcNode = n; + do { + app * ast = to_app(eqcNode); + if (u.str.is_concat(ast)) { + expr * simConcat = simplify_concat(ast); + if (simConcat != ast) { + if (u.str.is_concat(to_app(simConcat))) { + concats.insert(simConcat); + } else { + if (u.str.is_string(simConcat)) { + consts.insert(simConcat); + } else { + vars.insert(simConcat); + } + } + } else { + concats.insert(simConcat); + } + } else if (u.str.is_string(ast)) { + consts.insert(ast); + } else { + vars.insert(ast); + } + eqcNode = get_eqc_next(eqcNode); + } while (eqcNode != n); + } + + void theory_str::get_nodes_in_concat(expr * node, ptr_vector & nodeList) { + app * a_node = to_app(node); + if (!u.str.is_concat(a_node)) { + nodeList.push_back(node); + return; + } else { + SASSERT(a_node->get_num_args() == 2); + expr * leftArg = a_node->get_arg(0); + expr * rightArg = a_node->get_arg(1); + get_nodes_in_concat(leftArg, nodeList); + get_nodes_in_concat(rightArg, nodeList); + } + } + + // previously Concat() in strTheory.cpp + // Evaluates the concatenation (n1 . n2) with respect to + // the current equivalence classes of n1 and n2. + // Returns a constant string expression representing this concatenation + // if one can be determined, or NULL if this is not possible. + expr * theory_str::eval_concat(expr * n1, expr * n2) { + bool n1HasEqcValue = false; + bool n2HasEqcValue = false; + expr * v1 = get_eqc_value(n1, n1HasEqcValue); + expr * v2 = get_eqc_value(n2, n2HasEqcValue); + if (n1HasEqcValue && n2HasEqcValue) { + zstring n1_str, n2_str; + u.str.is_string(v1, n1_str); + u.str.is_string(v2, n2_str); + zstring result = n1_str + n2_str; + return mk_string(result); + } else if (n1HasEqcValue && !n2HasEqcValue) { + zstring v1_str; + u.str.is_string(v1, v1_str); + if (v1_str.empty()) { + return n2; + } + } else if (n2HasEqcValue && !n1HasEqcValue) { + zstring v2_str; + u.str.is_string(v2, v2_str); + if (v2_str.empty()) { + return n1; + } + } + // give up + return NULL; + } + + static inline std::string rational_to_string_if_exists(const rational & x, bool x_exists) { + if (x_exists) { + return x.to_string(); + } else { + return "?"; + } + } + + /* + * The inputs: + * ~ nn: non const node + * ~ eq_str: the equivalent constant string of nn + * Iterate the parent of all eqc nodes of nn, looking for: + * ~ concat node + * to see whether some concat nodes can be simplified. + */ + void theory_str::simplify_parent(expr * nn, expr * eq_str) { + ast_manager & m = get_manager(); + context & ctx = get_context(); + + TRACE("str", tout << "simplifying parents of " << mk_ismt2_pp(nn, m) + << " with respect to " << mk_ismt2_pp(eq_str, m) << std::endl;); + + ctx.internalize(nn, false); + + zstring eq_strValue; + u.str.is_string(eq_str, eq_strValue); + expr * n_eqNode = nn; + do { + enode * n_eq_enode = ctx.get_enode(n_eqNode); + TRACE("str", tout << "considering all parents of " << mk_ismt2_pp(n_eqNode, m) << std::endl + << "associated n_eq_enode has " << n_eq_enode->get_num_parents() << " parents" << std::endl;); + + // the goal of this next bit is to avoid dereferencing a bogus e_parent in the following loop. + // what I imagine is causing this bug is that, for example, we examine some parent, we add an axiom that involves it, + // and the parent_it iterator becomes invalidated, because we indirectly modified the container that we're iterating over. + + enode_vector current_parents; + for (enode_vector::const_iterator parent_it = n_eq_enode->begin_parents(); parent_it != n_eq_enode->end_parents(); parent_it++) { + current_parents.insert(*parent_it); + } + + for (enode_vector::iterator parent_it = current_parents.begin(); parent_it != current_parents.end(); ++parent_it) { + enode * e_parent = *parent_it; + SASSERT(e_parent != NULL); + + app * a_parent = e_parent->get_owner(); + TRACE("str", tout << "considering parent " << mk_ismt2_pp(a_parent, m) << std::endl;); + + if (u.str.is_concat(a_parent)) { + expr * arg0 = a_parent->get_arg(0); + expr * arg1 = a_parent->get_arg(1); + + rational parentLen; + bool parentLen_exists = get_len_value(a_parent, parentLen); + + if (arg0 == n_eq_enode->get_owner()) { + rational arg0Len, arg1Len; + bool arg0Len_exists = get_len_value(eq_str, arg0Len); + bool arg1Len_exists = get_len_value(arg1, arg1Len); + + TRACE("str", + tout << "simplify_parent #1:" << std::endl + << "* parent = " << mk_ismt2_pp(a_parent, m) << std::endl + << "* |parent| = " << rational_to_string_if_exists(parentLen, parentLen_exists) << std::endl + << "* |arg0| = " << rational_to_string_if_exists(arg0Len, arg0Len_exists) << std::endl + << "* |arg1| = " << rational_to_string_if_exists(arg1Len, arg1Len_exists) << std::endl; + ); + + if (parentLen_exists && !arg1Len_exists) { + TRACE("str", tout << "make up len for arg1" << std::endl;); + expr_ref implyL11(m.mk_and(ctx.mk_eq_atom(mk_strlen(a_parent), mk_int(parentLen)), + ctx.mk_eq_atom(mk_strlen(arg0), mk_int(arg0Len))), m); + rational makeUpLenArg1 = parentLen - arg0Len; + if (makeUpLenArg1.is_nonneg()) { + expr_ref implyR11(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(makeUpLenArg1)), m); + assert_implication(implyL11, implyR11); + } else { + expr_ref neg(m.mk_not(implyL11), m); + assert_axiom(neg); + } + } + + // (Concat n_eqNode arg1) /\ arg1 has eq const + + expr * concatResult = eval_concat(eq_str, arg1); + if (concatResult != NULL) { + bool arg1HasEqcValue = false; + expr * arg1Value = get_eqc_value(arg1, arg1HasEqcValue); + expr_ref implyL(m); + if (arg1 != arg1Value) { + expr_ref eq_ast1(m); + eq_ast1 = ctx.mk_eq_atom(n_eqNode, eq_str); + SASSERT(eq_ast1); + + expr_ref eq_ast2(m); + eq_ast2 = ctx.mk_eq_atom(arg1, arg1Value); + SASSERT(eq_ast2); + implyL = m.mk_and(eq_ast1, eq_ast2); + } else { + implyL = ctx.mk_eq_atom(n_eqNode, eq_str); + } + + + if (!in_same_eqc(a_parent, concatResult)) { + expr_ref implyR(m); + implyR = ctx.mk_eq_atom(a_parent, concatResult); + SASSERT(implyR); + + assert_implication(implyL, implyR); + } + } else if (u.str.is_concat(to_app(n_eqNode))) { + expr_ref simpleConcat(m); + simpleConcat = mk_concat(eq_str, arg1); + if (!in_same_eqc(a_parent, simpleConcat)) { + expr_ref implyL(m); + implyL = ctx.mk_eq_atom(n_eqNode, eq_str); + SASSERT(implyL); + + expr_ref implyR(m); + implyR = ctx.mk_eq_atom(a_parent, simpleConcat); + SASSERT(implyR); + assert_implication(implyL, implyR); + } + } + } // if (arg0 == n_eq_enode->get_owner()) + + if (arg1 == n_eq_enode->get_owner()) { + rational arg0Len, arg1Len; + bool arg0Len_exists = get_len_value(arg0, arg0Len); + bool arg1Len_exists = get_len_value(eq_str, arg1Len); + + TRACE("str", + tout << "simplify_parent #2:" << std::endl + << "* parent = " << mk_ismt2_pp(a_parent, m) << std::endl + << "* |parent| = " << rational_to_string_if_exists(parentLen, parentLen_exists) << std::endl + << "* |arg0| = " << rational_to_string_if_exists(arg0Len, arg0Len_exists) << std::endl + << "* |arg1| = " << rational_to_string_if_exists(arg1Len, arg1Len_exists) << std::endl; + ); + if (parentLen_exists && !arg0Len_exists) { + TRACE("str", tout << "make up len for arg0" << std::endl;); + expr_ref implyL11(m.mk_and(ctx.mk_eq_atom(mk_strlen(a_parent), mk_int(parentLen)), + ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1Len))), m); + rational makeUpLenArg0 = parentLen - arg1Len; + if (makeUpLenArg0.is_nonneg()) { + expr_ref implyR11(ctx.mk_eq_atom(mk_strlen(arg0), mk_int(makeUpLenArg0)), m); + assert_implication(implyL11, implyR11); + } else { + expr_ref neg(m.mk_not(implyL11), m); + assert_axiom(neg); + } + } + + // (Concat arg0 n_eqNode) /\ arg0 has eq const + + expr * concatResult = eval_concat(arg0, eq_str); + if (concatResult != NULL) { + bool arg0HasEqcValue = false; + expr * arg0Value = get_eqc_value(arg0, arg0HasEqcValue); + expr_ref implyL(m); + if (arg0 != arg0Value) { + expr_ref eq_ast1(m); + eq_ast1 = ctx.mk_eq_atom(n_eqNode, eq_str); + SASSERT(eq_ast1); + expr_ref eq_ast2(m); + eq_ast2 = ctx.mk_eq_atom(arg0, arg0Value); + SASSERT(eq_ast2); + + implyL = m.mk_and(eq_ast1, eq_ast2); + } else { + implyL = ctx.mk_eq_atom(n_eqNode, eq_str); + } + + if (!in_same_eqc(a_parent, concatResult)) { + expr_ref implyR(m); + implyR = ctx.mk_eq_atom(a_parent, concatResult); + SASSERT(implyR); + + assert_implication(implyL, implyR); + } + } else if (u.str.is_concat(to_app(n_eqNode))) { + expr_ref simpleConcat(m); + simpleConcat = mk_concat(arg0, eq_str); + if (!in_same_eqc(a_parent, simpleConcat)) { + expr_ref implyL(m); + implyL = ctx.mk_eq_atom(n_eqNode, eq_str); + SASSERT(implyL); + + expr_ref implyR(m); + implyR = ctx.mk_eq_atom(a_parent, simpleConcat); + SASSERT(implyR); + assert_implication(implyL, implyR); + } + } + } // if (arg1 == n_eq_enode->get_owner + + + //--------------------------------------------------------- + // Case (2-1) begin: (Concat n_eqNode (Concat str var)) + if (arg0 == n_eqNode && u.str.is_concat(to_app(arg1))) { + app * a_arg1 = to_app(arg1); + TRACE("str", tout << "simplify_parent #3" << std::endl;); + expr * r_concat_arg0 = a_arg1->get_arg(0); + if (u.str.is_string(r_concat_arg0)) { + expr * combined_str = eval_concat(eq_str, r_concat_arg0); + SASSERT(combined_str); + expr * r_concat_arg1 = a_arg1->get_arg(1); + expr_ref implyL(m); + implyL = ctx.mk_eq_atom(n_eqNode, eq_str); + expr * simplifiedAst = mk_concat(combined_str, r_concat_arg1); + if (!in_same_eqc(a_parent, simplifiedAst)) { + expr_ref implyR(m); + implyR = ctx.mk_eq_atom(a_parent, simplifiedAst); + assert_implication(implyL, implyR); + } + } + } + // Case (2-1) end: (Concat n_eqNode (Concat str var)) + //--------------------------------------------------------- + + + //--------------------------------------------------------- + // Case (2-2) begin: (Concat (Concat var str) n_eqNode) + if (u.str.is_concat(to_app(arg0)) && arg1 == n_eqNode) { + app * a_arg0 = to_app(arg0); + TRACE("str", tout << "simplify_parent #4" << std::endl;); + expr * l_concat_arg1 = a_arg0->get_arg(1); + if (u.str.is_string(l_concat_arg1)) { + expr * combined_str = eval_concat(l_concat_arg1, eq_str); + SASSERT(combined_str); + expr * l_concat_arg0 = a_arg0->get_arg(0); + expr_ref implyL(m); + implyL = ctx.mk_eq_atom(n_eqNode, eq_str); + expr * simplifiedAst = mk_concat(l_concat_arg0, combined_str); + if (!in_same_eqc(a_parent, simplifiedAst)) { + expr_ref implyR(m); + implyR = ctx.mk_eq_atom(a_parent, simplifiedAst); + assert_implication(implyL, implyR); + } + } + } + // Case (2-2) end: (Concat (Concat var str) n_eqNode) + //--------------------------------------------------------- + + // Have to look up one more layer: if the parent of the concat is another concat + //------------------------------------------------- + // Case (3-1) begin: (Concat (Concat var n_eqNode) str ) + if (arg1 == n_eqNode) { + for (enode_vector::iterator concat_parent_it = e_parent->begin_parents(); + concat_parent_it != e_parent->end_parents(); concat_parent_it++) { + enode * e_concat_parent = *concat_parent_it; + app * concat_parent = e_concat_parent->get_owner(); + if (u.str.is_concat(concat_parent)) { + expr * concat_parent_arg0 = concat_parent->get_arg(0); + expr * concat_parent_arg1 = concat_parent->get_arg(1); + if (concat_parent_arg0 == a_parent && u.str.is_string(concat_parent_arg1)) { + TRACE("str", tout << "simplify_parent #5" << std::endl;); + expr * combinedStr = eval_concat(eq_str, concat_parent_arg1); + SASSERT(combinedStr); + expr_ref implyL(m); + implyL = ctx.mk_eq_atom(n_eqNode, eq_str); + expr * simplifiedAst = mk_concat(arg0, combinedStr); + if (!in_same_eqc(concat_parent, simplifiedAst)) { + expr_ref implyR(m); + implyR = ctx.mk_eq_atom(concat_parent, simplifiedAst); + assert_implication(implyL, implyR); + } + } + } + } + } + // Case (3-1) end: (Concat (Concat var n_eqNode) str ) + // Case (3-2) begin: (Concat str (Concat n_eqNode var) ) + if (arg0 == n_eqNode) { + for (enode_vector::iterator concat_parent_it = e_parent->begin_parents(); + concat_parent_it != e_parent->end_parents(); concat_parent_it++) { + enode * e_concat_parent = *concat_parent_it; + app * concat_parent = e_concat_parent->get_owner(); + if (u.str.is_concat(concat_parent)) { + expr * concat_parent_arg0 = concat_parent->get_arg(0); + expr * concat_parent_arg1 = concat_parent->get_arg(1); + if (concat_parent_arg1 == a_parent && u.str.is_string(concat_parent_arg0)) { + TRACE("str", tout << "simplify_parent #6" << std::endl;); + expr * combinedStr = eval_concat(concat_parent_arg0, eq_str); + SASSERT(combinedStr); + expr_ref implyL(m); + implyL = ctx.mk_eq_atom(n_eqNode, eq_str); + expr * simplifiedAst = mk_concat(combinedStr, arg1); + if (!in_same_eqc(concat_parent, simplifiedAst)) { + expr_ref implyR(m); + implyR = ctx.mk_eq_atom(concat_parent, simplifiedAst); + assert_implication(implyL, implyR); + } + } + } + } + } + // Case (3-2) end: (Concat str (Concat n_eqNode var) ) + } // if is_concat(a_parent) + } // for parent_it : n_eq_enode->begin_parents() + + + // check next EQC member + n_eqNode = get_eqc_next(n_eqNode); + } while (n_eqNode != nn); + } + + expr * theory_str::simplify_concat(expr * node) { + ast_manager & m = get_manager(); + context & ctx = get_context(); + std::map resolvedMap; + ptr_vector argVec; + get_nodes_in_concat(node, argVec); + + for (unsigned i = 0; i < argVec.size(); ++i) { + bool vArgHasEqcValue = false; + expr * vArg = get_eqc_value(argVec[i], vArgHasEqcValue); + if (vArg != argVec[i]) { + resolvedMap[argVec[i]] = vArg; + } + } + + if (resolvedMap.size() == 0) { + // no simplification possible + return node; + } else { + expr * resultAst = mk_string(""); + for (unsigned i = 0; i < argVec.size(); ++i) { + bool vArgHasEqcValue = false; + expr * vArg = get_eqc_value(argVec[i], vArgHasEqcValue); + resultAst = mk_concat(resultAst, vArg); + } + TRACE("str", tout << mk_ismt2_pp(node, m) << " is simplified to " << mk_ismt2_pp(resultAst, m) << std::endl;); + + if (in_same_eqc(node, resultAst)) { + TRACE("str", tout << "SKIP: both concats are already in the same equivalence class" << std::endl;); + } else { + expr_ref_vector items(m); + int pos = 0; + std::map::iterator itor = resolvedMap.begin(); + for (; itor != resolvedMap.end(); ++itor) { + items.push_back(ctx.mk_eq_atom(itor->first, itor->second)); + pos += 1; + } + expr_ref premise(mk_and(items), m); + expr_ref conclusion(ctx.mk_eq_atom(node, resultAst), m); + assert_implication(premise, conclusion); + } + return resultAst; + } + + } + + // Modified signature of Z3str2's inferLenConcat(). + // Returns true iff nLen can be inferred by this method + // (i.e. the equivalent of a len_exists flag in get_len_value()). + + bool theory_str::infer_len_concat(expr * n, rational & nLen) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + expr * arg0 = to_app(n)->get_arg(0); + expr * arg1 = to_app(n)->get_arg(1); + + rational arg0_len, arg1_len; + bool arg0_len_exists = get_len_value(arg0, arg0_len); + bool arg1_len_exists = get_len_value(arg1, arg1_len); + rational tmp_len; + bool nLen_exists = get_len_value(n, tmp_len); + + if (arg0_len_exists && arg1_len_exists && !nLen_exists) { + expr_ref_vector l_items(m); + // if (mk_strlen(arg0) != mk_int(arg0_len)) { + { + l_items.push_back(ctx.mk_eq_atom(mk_strlen(arg0), mk_int(arg0_len))); + } + + // if (mk_strlen(arg1) != mk_int(arg1_len)) { + { + l_items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1_len))); + } + + expr_ref axl(m.mk_and(l_items.size(), l_items.c_ptr()), m); + rational nnLen = arg0_len + arg1_len; + expr_ref axr(ctx.mk_eq_atom(mk_strlen(n), mk_int(nnLen)), m); + TRACE("str", tout << "inferred (Length " << mk_pp(n, m) << ") = " << nnLen << std::endl;); + assert_implication(axl, axr); + nLen = nnLen; + return true; + } else { + return false; + } + } + + void theory_str::infer_len_concat_arg(expr * n, rational len) { + if (len.is_neg()) { + return; + } + + context & ctx = get_context(); + ast_manager & m = get_manager(); + + expr * arg0 = to_app(n)->get_arg(0); + expr * arg1 = to_app(n)->get_arg(1); + rational arg0_len, arg1_len; + bool arg0_len_exists = get_len_value(arg0, arg0_len); + bool arg1_len_exists = get_len_value(arg1, arg1_len); + + expr_ref_vector l_items(m); + expr_ref axr(m); + axr.reset(); + + // if (mk_length(t, n) != mk_int(ctx, len)) { + { + l_items.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(len))); + } + + if (!arg0_len_exists && arg1_len_exists) { + //if (mk_length(t, arg1) != mk_int(ctx, arg1_len)) { + { + l_items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1_len))); + } + rational arg0Len = len - arg1_len; + if (arg0Len.is_nonneg()) { + axr = ctx.mk_eq_atom(mk_strlen(arg0), mk_int(arg0Len)); + } else { + // could negate + } + } else if (arg0_len_exists && !arg1_len_exists) { + //if (mk_length(t, arg0) != mk_int(ctx, arg0_len)) { + { + l_items.push_back(ctx.mk_eq_atom(mk_strlen(arg0), mk_int(arg0_len))); + } + rational arg1Len = len - arg0_len; + if (arg1Len.is_nonneg()) { + axr = ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1Len)); + } else { + // could negate + } + } else { + + } + + if (axr) { + expr_ref axl(m.mk_and(l_items.size(), l_items.c_ptr()), m); + assert_implication(axl, axr); + } + } + + void theory_str::infer_len_concat_equality(expr * nn1, expr * nn2) { + rational nnLen; + bool nnLen_exists = get_len_value(nn1, nnLen); + if (!nnLen_exists) { + nnLen_exists = get_len_value(nn2, nnLen); + } + + // case 1: + // Known: a1_arg0 and a1_arg1 + // Unknown: nn1 + + if (u.str.is_concat(to_app(nn1))) { + rational nn1ConcatLen; + bool nn1ConcatLen_exists = infer_len_concat(nn1, nn1ConcatLen); + if (nnLen_exists && nn1ConcatLen_exists) { + nnLen = nn1ConcatLen; + } + } + + // case 2: + // Known: a1_arg0 and a1_arg1 + // Unknown: nn1 + + if (u.str.is_concat(to_app(nn2))) { + rational nn2ConcatLen; + bool nn2ConcatLen_exists = infer_len_concat(nn2, nn2ConcatLen); + if (nnLen_exists && nn2ConcatLen_exists) { + nnLen = nn2ConcatLen; + } + } + + if (nnLen_exists) { + if (u.str.is_concat(to_app(nn1))) { + infer_len_concat_arg(nn1, nnLen); + } + if (u.str.is_concat(to_app(nn2))) { + infer_len_concat_arg(nn2, nnLen); + } + } + + /* + if (isConcatFunc(t, nn2)) { + int nn2ConcatLen = inferLenConcat(t, nn2); + if (nnLen == -1 && nn2ConcatLen != -1) + nnLen = nn2ConcatLen; + } + + if (nnLen != -1) { + if (isConcatFunc(t, nn1)) { + inferLenConcatArg(t, nn1, nnLen); + } + if (isConcatFunc(t, nn2)) { + inferLenConcatArg(t, nn2, nnLen); + } + } + */ + } + + void theory_str::add_theory_aware_branching_info(expr * term, double priority, lbool phase) { + context & ctx = get_context(); + ctx.internalize(term, false); + bool_var v = ctx.get_bool_var(term); + ctx.add_theory_aware_branching_info(v, priority, phase); + } + + void theory_str::generate_mutual_exclusion(expr_ref_vector & terms) { + context & ctx = get_context(); + // pull each literal out of the arrangement disjunction + literal_vector ls; + for (unsigned i = 0; i < terms.size(); ++i) { + expr * e = terms.get(i); + literal l = ctx.get_literal(e); + ls.push_back(l); + } + ctx.mk_th_case_split(ls.size(), ls.c_ptr()); + } + + void theory_str::print_cut_var(expr * node, std::ofstream & xout) { + ast_manager & m = get_manager(); + xout << "Cut info of " << mk_pp(node, m) << std::endl; + if (cut_var_map.contains(node)) { + if (!cut_var_map[node].empty()) { + xout << "[" << cut_var_map[node].top()->level << "] "; + std::map::iterator itor = cut_var_map[node].top()->vars.begin(); + for (; itor != cut_var_map[node].top()->vars.end(); ++itor) { + xout << mk_pp(itor->first, m) << ", "; + } + xout << std::endl; + } + } + } + + /* + * Handle two equivalent Concats. + */ + void theory_str::simplify_concat_equality(expr * nn1, expr * nn2) { + ast_manager & m = get_manager(); + context & ctx = get_context(); + + app * a_nn1 = to_app(nn1); + SASSERT(a_nn1->get_num_args() == 2); + app * a_nn2 = to_app(nn2); + SASSERT(a_nn2->get_num_args() == 2); + + expr * a1_arg0 = a_nn1->get_arg(0); + expr * a1_arg1 = a_nn1->get_arg(1); + expr * a2_arg0 = a_nn2->get_arg(0); + expr * a2_arg1 = a_nn2->get_arg(1); + + rational a1_arg0_len, a1_arg1_len, a2_arg0_len, a2_arg1_len; + + bool a1_arg0_len_exists = get_len_value(a1_arg0, a1_arg0_len); + bool a1_arg1_len_exists = get_len_value(a1_arg1, a1_arg1_len); + bool a2_arg0_len_exists = get_len_value(a2_arg0, a2_arg0_len); + bool a2_arg1_len_exists = get_len_value(a2_arg1, a2_arg1_len); + + TRACE("str", tout << "nn1 = " << mk_ismt2_pp(nn1, m) << std::endl + << "nn2 = " << mk_ismt2_pp(nn2, m) << std::endl;); + + TRACE("str", tout + << "len(" << mk_pp(a1_arg0, m) << ") = " << (a1_arg0_len_exists ? a1_arg0_len.to_string() : "?") << std::endl + << "len(" << mk_pp(a1_arg1, m) << ") = " << (a1_arg1_len_exists ? a1_arg1_len.to_string() : "?") << std::endl + << "len(" << mk_pp(a2_arg0, m) << ") = " << (a2_arg0_len_exists ? a2_arg0_len.to_string() : "?") << std::endl + << "len(" << mk_pp(a2_arg1, m) << ") = " << (a2_arg1_len_exists ? a2_arg1_len.to_string() : "?") << std::endl + << std::endl;); + + infer_len_concat_equality(nn1, nn2); + + if (a1_arg0 == a2_arg0) { + if (!in_same_eqc(a1_arg1, a2_arg1)) { + expr_ref premise(ctx.mk_eq_atom(nn1, nn2), m); + expr_ref eq1(ctx.mk_eq_atom(a1_arg1, a2_arg1), m); + expr_ref eq2(ctx.mk_eq_atom(mk_strlen(a1_arg1), mk_strlen(a2_arg1)), m); + expr_ref conclusion(m.mk_and(eq1, eq2), m); + assert_implication(premise, conclusion); + } + TRACE("str", tout << "SKIP: a1_arg0 == a2_arg0" << std::endl;); + return; + } + + if (a1_arg1 == a2_arg1) { + if (!in_same_eqc(a1_arg0, a2_arg0)) { + expr_ref premise(ctx.mk_eq_atom(nn1, nn2), m); + expr_ref eq1(ctx.mk_eq_atom(a1_arg0, a2_arg0), m); + expr_ref eq2(ctx.mk_eq_atom(mk_strlen(a1_arg0), mk_strlen(a2_arg0)), m); + expr_ref conclusion(m.mk_and(eq1, eq2), m); + assert_implication(premise, conclusion); + } + TRACE("str", tout << "SKIP: a1_arg1 == a2_arg1" << std::endl;); + return; + } + + // quick path + + if (in_same_eqc(a1_arg0, a2_arg0)) { + if (in_same_eqc(a1_arg1, a2_arg1)) { + TRACE("str", tout << "SKIP: a1_arg0 =~ a2_arg0 and a1_arg1 =~ a2_arg1" << std::endl;); + return; + } else { + TRACE("str", tout << "quick path 1-1: a1_arg0 =~ a2_arg0" << std::endl;); + expr_ref premise(m.mk_and(ctx.mk_eq_atom(nn1, nn2), ctx.mk_eq_atom(a1_arg0, a2_arg0)), m); + expr_ref conclusion(m.mk_and(ctx.mk_eq_atom(a1_arg1, a2_arg1), ctx.mk_eq_atom(mk_strlen(a1_arg1), mk_strlen(a2_arg1))), m); + assert_implication(premise, conclusion); + return; + } + } else { + if (in_same_eqc(a1_arg1, a2_arg1)) { + TRACE("str", tout << "quick path 1-2: a1_arg1 =~ a2_arg1" << std::endl;); + expr_ref premise(m.mk_and(ctx.mk_eq_atom(nn1, nn2), ctx.mk_eq_atom(a1_arg1, a2_arg1)), m); + expr_ref conclusion(m.mk_and(ctx.mk_eq_atom(a1_arg0, a2_arg0), ctx.mk_eq_atom(mk_strlen(a1_arg0), mk_strlen(a2_arg0))), m); + assert_implication(premise, conclusion); + return; + } + } + + // quick path 2-1 + if (a1_arg0_len_exists && a2_arg0_len_exists && a1_arg0_len == a2_arg0_len) { + if (!in_same_eqc(a1_arg0, a2_arg0)) { + TRACE("str", tout << "quick path 2-1: len(nn1.arg0) == len(nn2.arg0)" << std::endl;); + expr_ref ax_l1(ctx.mk_eq_atom(nn1, nn2), m); + expr_ref ax_l2(ctx.mk_eq_atom(mk_strlen(a1_arg0), mk_strlen(a2_arg0)), m); + expr_ref ax_r1(ctx.mk_eq_atom(a1_arg0, a2_arg0), m); + expr_ref ax_r2(ctx.mk_eq_atom(a1_arg1, a2_arg1), m); + + expr_ref premise(m.mk_and(ax_l1, ax_l2), m); + expr_ref conclusion(m.mk_and(ax_r1, ax_r2), m); + + assert_implication(premise, conclusion); + + if (opt_NoQuickReturn_IntegerTheory) { + TRACE("str", tout << "bypassing quick return from the end of this case" << std::endl;); + } else { + return; + } + } + } + + if (a1_arg1_len_exists && a2_arg1_len_exists && a1_arg1_len == a2_arg1_len) { + if (!in_same_eqc(a1_arg1, a2_arg1)) { + TRACE("str", tout << "quick path 2-2: len(nn1.arg1) == len(nn2.arg1)" << std::endl;); + expr_ref ax_l1(ctx.mk_eq_atom(nn1, nn2), m); + expr_ref ax_l2(ctx.mk_eq_atom(mk_strlen(a1_arg1), mk_strlen(a2_arg1)), m); + expr_ref ax_r1(ctx.mk_eq_atom(a1_arg0, a2_arg0), m); + expr_ref ax_r2(ctx.mk_eq_atom(a1_arg1, a2_arg1), m); + + expr_ref premise(m.mk_and(ax_l1, ax_l2), m); + expr_ref conclusion(m.mk_and(ax_r1, ax_r2), m); + + assert_implication(premise, conclusion); + if (opt_NoQuickReturn_IntegerTheory) { + TRACE("str", tout << "bypassing quick return from the end of this case" << std::endl;); + } else { + return; + } + } + } + + expr_ref new_nn1(simplify_concat(nn1), m); + expr_ref new_nn2(simplify_concat(nn2), m); + app * a_new_nn1 = to_app(new_nn1); + app * a_new_nn2 = to_app(new_nn2); + + TRACE("str", tout << "new_nn1 = " << mk_ismt2_pp(new_nn1, m) << std::endl + << "new_nn2 = " << mk_ismt2_pp(new_nn2, m) << std::endl;); + + if (new_nn1 == new_nn2) { + TRACE("str", tout << "equal concats, return" << std::endl;); + return; + } + + if (!can_two_nodes_eq(new_nn1, new_nn2)) { + expr_ref detected(m.mk_not(ctx.mk_eq_atom(new_nn1, new_nn2)), m); + TRACE("str", tout << "inconsistency detected: " << mk_ismt2_pp(detected, m) << std::endl;); + assert_axiom(detected); + return; + } + + // check whether new_nn1 and new_nn2 are still concats + + bool n1IsConcat = u.str.is_concat(a_new_nn1); + bool n2IsConcat = u.str.is_concat(a_new_nn2); + if (!n1IsConcat && n2IsConcat) { + TRACE("str", tout << "nn1_new is not a concat" << std::endl;); + if (u.str.is_string(a_new_nn1)) { + simplify_parent(new_nn2, new_nn1); + } + return; + } else if (n1IsConcat && !n2IsConcat) { + TRACE("str", tout << "nn2_new is not a concat" << std::endl;); + if (u.str.is_string(a_new_nn2)) { + simplify_parent(new_nn1, new_nn2); + } + return; + } else if (!n1IsConcat && !n2IsConcat) { + // normally this should never happen, because group_terms_by_eqc() should have pre-simplified + // as much as possible. however, we make a defensive check here just in case + TRACE("str", tout << "WARNING: nn1_new and nn2_new both simplify to non-concat terms" << std::endl;); + return; + } + + expr * v1_arg0 = a_new_nn1->get_arg(0); + expr * v1_arg1 = a_new_nn1->get_arg(1); + expr * v2_arg0 = a_new_nn2->get_arg(0); + expr * v2_arg1 = a_new_nn2->get_arg(1); + + if (!in_same_eqc(new_nn1, new_nn2) && (nn1 != new_nn1 || nn2 != new_nn2)) { + int ii4 = 0; + expr* item[3]; + if (nn1 != new_nn1) { + item[ii4++] = ctx.mk_eq_atom(nn1, new_nn1); + } + if (nn2 != new_nn2) { + item[ii4++] = ctx.mk_eq_atom(nn2, new_nn2); + } + item[ii4++] = ctx.mk_eq_atom(nn1, nn2); + expr_ref premise(m.mk_and(ii4, item), m); + expr_ref conclusion(ctx.mk_eq_atom(new_nn1, new_nn2), m); + assert_implication(premise, conclusion); + } + + // start to split both concats + check_and_init_cut_var(v1_arg0); + check_and_init_cut_var(v1_arg1); + check_and_init_cut_var(v2_arg0); + check_and_init_cut_var(v2_arg1); + + //************************************************************* + // case 1: concat(x, y) = concat(m, n) + //************************************************************* + if (is_concat_eq_type1(new_nn1, new_nn2)) { + process_concat_eq_type1(new_nn1, new_nn2); + return; + } + + //************************************************************* + // case 2: concat(x, y) = concat(m, "str") + //************************************************************* + if (is_concat_eq_type2(new_nn1, new_nn2)) { + process_concat_eq_type2(new_nn1, new_nn2); + return; + } + + //************************************************************* + // case 3: concat(x, y) = concat("str", n) + //************************************************************* + if (is_concat_eq_type3(new_nn1, new_nn2)) { + process_concat_eq_type3(new_nn1, new_nn2); + return; + } + + //************************************************************* + // case 4: concat("str1", y) = concat("str2", n) + //************************************************************* + if (is_concat_eq_type4(new_nn1, new_nn2)) { + process_concat_eq_type4(new_nn1, new_nn2); + return; + } + + //************************************************************* + // case 5: concat(x, "str1") = concat(m, "str2") + //************************************************************* + if (is_concat_eq_type5(new_nn1, new_nn2)) { + process_concat_eq_type5(new_nn1, new_nn2); + return; + } + //************************************************************* + // case 6: concat("str1", y) = concat(m, "str2") + //************************************************************* + if (is_concat_eq_type6(new_nn1, new_nn2)) { + process_concat_eq_type6(new_nn1, new_nn2); + return; + } + + } + + /* + * Returns true if attempting to process a concat equality between lhs and rhs + * will result in overlapping variables (false otherwise). + */ + bool theory_str::will_result_in_overlap(expr * lhs, expr * rhs) { + ast_manager & m = get_manager(); + + expr_ref new_nn1(simplify_concat(lhs), m); + expr_ref new_nn2(simplify_concat(rhs), m); + app * a_new_nn1 = to_app(new_nn1); + app * a_new_nn2 = to_app(new_nn2); + + bool n1IsConcat = u.str.is_concat(a_new_nn1); + bool n2IsConcat = u.str.is_concat(a_new_nn2); + if (!n1IsConcat && !n2IsConcat) { + // we simplified both sides to non-concat expressions... + return false; + } + + expr * v1_arg0 = a_new_nn1->get_arg(0); + expr * v1_arg1 = a_new_nn1->get_arg(1); + expr * v2_arg0 = a_new_nn2->get_arg(0); + expr * v2_arg1 = a_new_nn2->get_arg(1); + + TRACE("str", tout << "checking whether " << mk_pp(new_nn1, m) << " and " << mk_pp(new_nn1, m) << " might overlap." << std::endl;); + + check_and_init_cut_var(v1_arg0); + check_and_init_cut_var(v1_arg1); + check_and_init_cut_var(v2_arg0); + check_and_init_cut_var(v2_arg1); + + //************************************************************* + // case 1: concat(x, y) = concat(m, n) + //************************************************************* + if (is_concat_eq_type1(new_nn1, new_nn2)) { + TRACE("str", tout << "Type 1 check." << std::endl;); + expr * x = to_app(new_nn1)->get_arg(0); + expr * y = to_app(new_nn1)->get_arg(1); + expr * m = to_app(new_nn2)->get_arg(0); + expr * n = to_app(new_nn2)->get_arg(1); + + if (has_self_cut(m, y)) { + TRACE("str", tout << "Possible overlap found" << std::endl; print_cut_var(m, tout); print_cut_var(y, tout);); + return true; + } else if (has_self_cut(x, n)) { + TRACE("str", tout << "Possible overlap found" << std::endl; print_cut_var(x, tout); print_cut_var(n, tout);); + return true; + } else { + return false; + } + } + + //************************************************************* + // case 2: concat(x, y) = concat(m, "str") + //************************************************************* + if (is_concat_eq_type2(new_nn1, new_nn2)) { + + expr * y = NULL; + expr * m = NULL; + expr * v1_arg0 = to_app(new_nn1)->get_arg(0); + expr * v1_arg1 = to_app(new_nn1)->get_arg(1); + expr * v2_arg0 = to_app(new_nn2)->get_arg(0); + expr * v2_arg1 = to_app(new_nn2)->get_arg(1); + + if (u.str.is_string(v1_arg1) && !u.str.is_string(v2_arg1)) { + m = v1_arg0; + y = v2_arg1; + } else { + m = v2_arg0; + y = v1_arg1; + } + + if (has_self_cut(m, y)) { + TRACE("str", tout << "Possible overlap found" << std::endl; print_cut_var(m, tout); print_cut_var(y, tout);); + return true; + } else { + return false; + } + } + + //************************************************************* + // case 3: concat(x, y) = concat("str", n) + //************************************************************* + if (is_concat_eq_type3(new_nn1, new_nn2)) { + expr * v1_arg0 = to_app(new_nn1)->get_arg(0); + expr * v1_arg1 = to_app(new_nn1)->get_arg(1); + expr * v2_arg0 = to_app(new_nn2)->get_arg(0); + expr * v2_arg1 = to_app(new_nn2)->get_arg(1); + + expr * x = NULL; + expr * n = NULL; + + if (u.str.is_string(v1_arg0) && !u.str.is_string(v2_arg0)) { + n = v1_arg1; + x = v2_arg0; + } else { + n = v2_arg1; + x = v1_arg0; + } + if (has_self_cut(x, n)) { + TRACE("str", tout << "Possible overlap found" << std::endl; print_cut_var(x, tout); print_cut_var(n, tout);); + return true; + } else { + return false; + } + } + + //************************************************************* + // case 4: concat("str1", y) = concat("str2", n) + //************************************************************* + if (is_concat_eq_type4(new_nn1, new_nn2)) { + // This case can never result in an overlap. + return false; + } + + //************************************************************* + // case 5: concat(x, "str1") = concat(m, "str2") + //************************************************************* + if (is_concat_eq_type5(new_nn1, new_nn2)) { + // This case can never result in an overlap. + return false; + } + //************************************************************* + // case 6: concat("str1", y) = concat(m, "str2") + //************************************************************* + if (is_concat_eq_type6(new_nn1, new_nn2)) { + expr * v1_arg0 = to_app(new_nn1)->get_arg(0); + expr * v1_arg1 = to_app(new_nn1)->get_arg(1); + expr * v2_arg0 = to_app(new_nn2)->get_arg(0); + expr * v2_arg1 = to_app(new_nn2)->get_arg(1); + + expr * y = NULL; + expr * m = NULL; + + if (u.str.is_string(v1_arg0)) { + y = v1_arg1; + m = v2_arg0; + } else { + y = v2_arg1; + m = v1_arg0; + } + if (has_self_cut(m, y)) { + TRACE("str", tout << "Possible overlap found" << std::endl; print_cut_var(m, tout); print_cut_var(y, tout);); + return true; + } else { + return false; + } + } + + TRACE("str", tout << "warning: unrecognized concat case" << std::endl;); return false; } -} -void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { - ast_manager & mgr = get_manager(); - context & ctx = get_context(); + /************************************************************* + * Type 1: concat(x, y) = concat(m, n) + * x, y, m and n all variables + *************************************************************/ + bool theory_str::is_concat_eq_type1(expr * concatAst1, expr * concatAst2) { + expr * x = to_app(concatAst1)->get_arg(0); + expr * y = to_app(concatAst1)->get_arg(1); + expr * m = to_app(concatAst2)->get_arg(0); + expr * n = to_app(concatAst2)->get_arg(1); - bool overlapAssumptionUsed = false; - - TRACE("str", tout << "process_concat_eq TYPE 1" << std::endl - << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl - << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; - ); - - if (!u.str.is_concat(to_app(concatAst1))) { - TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); - return; - } - if (!u.str.is_concat(to_app(concatAst2))) { - TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); - return; - } - expr * x = to_app(concatAst1)->get_arg(0); - expr * y = to_app(concatAst1)->get_arg(1); - expr * m = to_app(concatAst2)->get_arg(0); - expr * n = to_app(concatAst2)->get_arg(1); - - rational x_len, y_len, m_len, n_len; - bool x_len_exists = get_len_value(x, x_len); - bool y_len_exists = get_len_value(y, y_len); - bool m_len_exists = get_len_value(m, m_len); - bool n_len_exists = get_len_value(n, n_len); - - int splitType = -1; - if (x_len_exists && m_len_exists) { - TRACE("str", tout << "length values found: x/m" << std::endl;); - if (x_len < m_len) { - splitType = 0; - } else if (x_len == m_len) { - splitType = 1; + if (!u.str.is_string(x) && !u.str.is_string(y) && !u.str.is_string(m) && !u.str.is_string(n)) { + return true; } else { - splitType = 2; + return false; } } - if (splitType == -1 && y_len_exists && n_len_exists) { - TRACE("str", tout << "length values found: y/n" << std::endl;); - if (y_len > n_len) { - splitType = 0; - } else if (y_len == n_len) { - splitType = 1; - } else { - splitType = 2; + void theory_str::process_concat_eq_type1(expr * concatAst1, expr * concatAst2) { + ast_manager & mgr = get_manager(); + context & ctx = get_context(); + + bool overlapAssumptionUsed = false; + + TRACE("str", tout << "process_concat_eq TYPE 1" << std::endl + << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl + << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; + ); + + if (!u.str.is_concat(to_app(concatAst1))) { + TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); + return; } - } + if (!u.str.is_concat(to_app(concatAst2))) { + TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); + return; + } + expr * x = to_app(concatAst1)->get_arg(0); + expr * y = to_app(concatAst1)->get_arg(1); + expr * m = to_app(concatAst2)->get_arg(0); + expr * n = to_app(concatAst2)->get_arg(1); - TRACE("str", tout - << "len(x) = " << (x_len_exists ? x_len.to_string() : "?") << std::endl - << "len(y) = " << (y_len_exists ? y_len.to_string() : "?") << std::endl - << "len(m) = " << (m_len_exists ? m_len.to_string() : "?") << std::endl - << "len(n) = " << (n_len_exists ? n_len.to_string() : "?") << std::endl - << "split type " << splitType << std::endl; - ); + rational x_len, y_len, m_len, n_len; + bool x_len_exists = get_len_value(x, x_len); + bool y_len_exists = get_len_value(y, y_len); + bool m_len_exists = get_len_value(m, m_len); + bool n_len_exists = get_len_value(n, n_len); - expr * t1 = NULL; - expr * t2 = NULL; - expr * xorFlag = NULL; + int splitType = -1; + if (x_len_exists && m_len_exists) { + TRACE("str", tout << "length values found: x/m" << std::endl;); + if (x_len < m_len) { + splitType = 0; + } else if (x_len == m_len) { + splitType = 1; + } else { + splitType = 2; + } + } - std::pair key1(concatAst1, concatAst2); - std::pair key2(concatAst2, concatAst1); + if (splitType == -1 && y_len_exists && n_len_exists) { + TRACE("str", tout << "length values found: y/n" << std::endl;); + if (y_len > n_len) { + splitType = 0; + } else if (y_len == n_len) { + splitType = 1; + } else { + splitType = 2; + } + } - // check the entries in this map to make sure they're still in scope - // before we use them. + TRACE("str", tout + << "len(x) = " << (x_len_exists ? x_len.to_string() : "?") << std::endl + << "len(y) = " << (y_len_exists ? y_len.to_string() : "?") << std::endl + << "len(m) = " << (m_len_exists ? m_len.to_string() : "?") << std::endl + << "len(n) = " << (n_len_exists ? n_len.to_string() : "?") << std::endl + << "split type " << splitType << std::endl; + ); - std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); - std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); + expr * t1 = NULL; + expr * t2 = NULL; + expr * xorFlag = NULL; - bool entry1InScope; - if (entry1 == varForBreakConcat.end()) { - entry1InScope = false; - } else { - if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end() - || internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end() - /*|| internal_variable_set.find((entry1->second)[2]) == internal_variable_set.end() */) { + std::pair key1(concatAst1, concatAst2); + std::pair key2(concatAst2, concatAst1); + + // check the entries in this map to make sure they're still in scope + // before we use them. + + std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); + std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); + + bool entry1InScope; + if (entry1 == varForBreakConcat.end()) { entry1InScope = false; } else { - entry1InScope = true; + if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end() + || internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end() + /*|| internal_variable_set.find((entry1->second)[2]) == internal_variable_set.end() */) { + entry1InScope = false; + } else { + entry1InScope = true; + } } - } - bool entry2InScope; - if (entry2 == varForBreakConcat.end()) { - entry2InScope = false; - } else { - if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end() - || internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end() - /* || internal_variable_set.find((entry2->second)[2]) == internal_variable_set.end() */) { + bool entry2InScope; + if (entry2 == varForBreakConcat.end()) { entry2InScope = false; } else { - entry2InScope = true; - } - } - - TRACE("str", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl - << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); - - if (!entry1InScope && !entry2InScope) { - t1 = mk_nonempty_str_var(); - t2 = mk_nonempty_str_var(); - xorFlag = mk_internal_xor_var(); - check_and_init_cut_var(t1); - check_and_init_cut_var(t2); - varForBreakConcat[key1][0] = t1; - varForBreakConcat[key1][1] = t2; - varForBreakConcat[key1][2] = xorFlag; - } else { - // match found - if (entry1InScope) { - t1 = varForBreakConcat[key1][0]; - t2 = varForBreakConcat[key1][1]; - xorFlag = varForBreakConcat[key1][2]; - } else { - t1 = varForBreakConcat[key2][0]; - t2 = varForBreakConcat[key2][1]; - xorFlag = varForBreakConcat[key2][2]; - } - refresh_theory_var(t1); - add_nonempty_constraint(t1); - refresh_theory_var(t2); - add_nonempty_constraint(t2); - } - - // For split types 0 through 2, we can get away with providing - // fewer split options since more length information is available. - if (splitType == 0) { - //-------------------------------------- - // Type 0: M cuts Y. - // len(x) < len(m) || len(y) > len(n) - //-------------------------------------- - expr_ref_vector ax_l_items(mgr); - expr_ref_vector ax_r_items(mgr); - - ax_l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); - - expr_ref x_t1(mk_concat(x, t1), mgr); - expr_ref t1_n(mk_concat(t1, n), mgr); - - ax_r_items.push_back(ctx.mk_eq_atom(m, x_t1)); - ax_r_items.push_back(ctx.mk_eq_atom(y, t1_n)); - - if (m_len_exists && x_len_exists) { - ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); - ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); - rational m_sub_x = m_len - x_len; - ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t1), mk_int(m_sub_x))); - } else { - ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); - ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len))); - rational y_sub_n = y_len - n_len; - ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t1), mk_int(y_sub_n))); - } - - expr_ref ax_l(mk_and(ax_l_items), mgr); - expr_ref ax_r(mk_and(ax_r_items), mgr); - - if (!has_self_cut(m, y)) { - // Cut Info - add_cut_info_merge(t1, sLevel, m); - add_cut_info_merge(t1, sLevel, y); - - if (m_params.m_StrongArrangements) { - expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); - assert_axiom(ax_strong); + if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end() + || internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end() + /* || internal_variable_set.find((entry2->second)[2]) == internal_variable_set.end() */) { + entry2InScope = false; } else { - assert_implication(ax_l, ax_r); - } - } else { - loopDetected = true; - if (m_params.m_FiniteOverlapModels) { - expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); - assert_implication(ax_l, tester); - add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); - } else { - TRACE("str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - TRACE("str", {print_cut_var(m, tout); print_cut_var(y, tout);}); - - if (!overlapAssumptionUsed) { - overlapAssumptionUsed = true; - assert_implication(ax_l, m_theoryStrOverlapAssumption_term); - } + entry2InScope = true; } } - } else if (splitType == 1) { - // Type 1: - // len(x) = len(m) || len(y) = len(n) - expr_ref ax_l1(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); - expr_ref ax_l2(mgr.mk_or(ctx.mk_eq_atom(mk_strlen(x), mk_strlen(m)), ctx.mk_eq_atom(mk_strlen(y), mk_strlen(n))), mgr); - expr_ref ax_l(mgr.mk_and(ax_l1, ax_l2), mgr); - expr_ref ax_r(mgr.mk_and(ctx.mk_eq_atom(x,m), ctx.mk_eq_atom(y,n)), mgr); - assert_implication(ax_l, ax_r); - } else if (splitType == 2) { - // Type 2: X cuts N. - // len(x) > len(m) || len(y) < len(n) - expr_ref m_t2(mk_concat(m, t2), mgr); - expr_ref t2_y(mk_concat(t2, y), mgr); - expr_ref_vector ax_l_items(mgr); - ax_l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); + TRACE("str", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl + << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); - expr_ref_vector ax_r_items(mgr); - ax_r_items.push_back(ctx.mk_eq_atom(x, m_t2)); - ax_r_items.push_back(ctx.mk_eq_atom(t2_y, n)); - - if (m_len_exists && x_len_exists) { - ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); - ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); - rational x_sub_m = x_len - m_len; - ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t2), mk_int(x_sub_m))); + if (!entry1InScope && !entry2InScope) { + t1 = mk_nonempty_str_var(); + t2 = mk_nonempty_str_var(); + xorFlag = mk_internal_xor_var(); + check_and_init_cut_var(t1); + check_and_init_cut_var(t2); + varForBreakConcat[key1][0] = t1; + varForBreakConcat[key1][1] = t2; + varForBreakConcat[key1][2] = xorFlag; } else { - ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); - ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len))); - rational n_sub_y = n_len - y_len; - ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t2), mk_int(n_sub_y))); + // match found + if (entry1InScope) { + t1 = varForBreakConcat[key1][0]; + t2 = varForBreakConcat[key1][1]; + xorFlag = varForBreakConcat[key1][2]; + } else { + t1 = varForBreakConcat[key2][0]; + t2 = varForBreakConcat[key2][1]; + xorFlag = varForBreakConcat[key2][2]; + } + refresh_theory_var(t1); + add_nonempty_constraint(t1); + refresh_theory_var(t2); + add_nonempty_constraint(t2); } - expr_ref ax_l(mk_and(ax_l_items), mgr); - expr_ref ax_r(mk_and(ax_r_items), mgr); + // For split types 0 through 2, we can get away with providing + // fewer split options since more length information is available. + if (splitType == 0) { + //-------------------------------------- + // Type 0: M cuts Y. + // len(x) < len(m) || len(y) > len(n) + //-------------------------------------- + expr_ref_vector ax_l_items(mgr); + expr_ref_vector ax_r_items(mgr); - if (!has_self_cut(x, n)) { - // Cut Info - add_cut_info_merge(t2, sLevel, x); - add_cut_info_merge(t2, sLevel, n); + ax_l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); - if (m_params.m_StrongArrangements) { - expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); - assert_axiom(ax_strong); - } else { - assert_implication(ax_l, ax_r); - } - } else { - loopDetected = true; - if (m_params.m_FiniteOverlapModels) { - expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); - assert_implication(ax_l, tester); - add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); - } else { - TRACE("str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - TRACE("str", {print_cut_var(m, tout); print_cut_var(y, tout);}); - - if (!overlapAssumptionUsed) { - overlapAssumptionUsed = true; - assert_implication(ax_l, m_theoryStrOverlapAssumption_term); - } - } - } - } else if (splitType == -1) { - // Here we don't really have a choice. We have no length information at all... - - // This vector will eventually contain one term for each possible arrangement we explore. - expr_ref_vector arrangement_disjunction(mgr); - - // break option 1: m cuts y - // len(x) < len(m) || len(y) > len(n) - if (!avoidLoopCut || !has_self_cut(m, y)) { - expr_ref_vector and_item(mgr); - // break down option 1-1 expr_ref x_t1(mk_concat(x, t1), mgr); expr_ref t1_n(mk_concat(t1, n), mgr); - and_item.push_back(ctx.mk_eq_atom(m, x_t1)); - and_item.push_back(ctx.mk_eq_atom(y, t1_n)); + ax_r_items.push_back(ctx.mk_eq_atom(m, x_t1)); + ax_r_items.push_back(ctx.mk_eq_atom(y, t1_n)); - expr_ref x_plus_t1(m_autil.mk_add(mk_strlen(x), mk_strlen(t1)), mgr); - and_item.push_back(ctx.mk_eq_atom(mk_strlen(m), x_plus_t1)); - // These were crashing the solver because the integer theory - // expects a constant on the right-hand side. - // The things we want to assert here are len(m) > len(x) and len(y) > len(n). - // We rewrite A > B as A-B > 0 and then as not(A-B <= 0), - // and then, *because we aren't allowed to use subtraction*, - // as not(A + -1*B <= 0) - and_item.push_back( - mgr.mk_not(m_autil.mk_le( - m_autil.mk_add(mk_strlen(m), m_autil.mk_mul(mk_int(-1), mk_strlen(x))), - mk_int(0))) ); - and_item.push_back( - mgr.mk_not(m_autil.mk_le( - m_autil.mk_add(mk_strlen(y),m_autil.mk_mul(mk_int(-1), mk_strlen(n))), - mk_int(0))) ); - - expr_ref option1(mk_and(and_item), mgr); - arrangement_disjunction.push_back(option1); - add_theory_aware_branching_info(option1, 0.1, l_true); - - add_cut_info_merge(t1, ctx.get_scope_level(), m); - add_cut_info_merge(t1, ctx.get_scope_level(), y); - } else { - loopDetected = true; - if (m_params.m_FiniteOverlapModels) { - expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); - arrangement_disjunction.push_back(tester); - add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); + if (m_len_exists && x_len_exists) { + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); + rational m_sub_x = m_len - x_len; + ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t1), mk_int(m_sub_x))); } else { - TRACE("str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - TRACE("str", {print_cut_var(m, tout); print_cut_var(y, tout);}); - - if (!overlapAssumptionUsed) { - overlapAssumptionUsed = true; - arrangement_disjunction.push_back(m_theoryStrOverlapAssumption_term); - } - } - } - - // break option 2: - // x = m . t2 - // n = t2 . y - if (!avoidLoopCut || !has_self_cut(x, n)) { - expr_ref_vector and_item(mgr); - // break down option 1-2 - expr_ref m_t2(mk_concat(m, t2), mgr); - expr_ref t2_y(mk_concat(t2, y), mgr); - - and_item.push_back(ctx.mk_eq_atom(x, m_t2)); - and_item.push_back(ctx.mk_eq_atom(n, t2_y)); - - - expr_ref m_plus_t2(m_autil.mk_add(mk_strlen(m), mk_strlen(t2)), mgr); - - and_item.push_back(ctx.mk_eq_atom(mk_strlen(x), m_plus_t2)); - // want len(x) > len(m) and len(n) > len(y) - and_item.push_back( - mgr.mk_not(m_autil.mk_le( - m_autil.mk_add(mk_strlen(x), m_autil.mk_mul(mk_int(-1), mk_strlen(m))), - mk_int(0))) ); - and_item.push_back( - mgr.mk_not(m_autil.mk_le( - m_autil.mk_add(mk_strlen(n), m_autil.mk_mul(mk_int(-1), mk_strlen(y))), - mk_int(0))) ); - - expr_ref option2(mk_and(and_item), mgr); - arrangement_disjunction.push_back(option2); - add_theory_aware_branching_info(option2, 0.1, l_true); - - add_cut_info_merge(t2, ctx.get_scope_level(), x); - add_cut_info_merge(t2, ctx.get_scope_level(), n); - } else { - loopDetected = true; - if (m_params.m_FiniteOverlapModels) { - expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); - arrangement_disjunction.push_back(tester); - add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); - } else { - TRACE("str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - TRACE("str", {print_cut_var(x, tout); print_cut_var(n, tout);}); - - if (!overlapAssumptionUsed) { - overlapAssumptionUsed = true; - arrangement_disjunction.push_back(m_theoryStrOverlapAssumption_term); - } - } - } - - // option 3: - // x = m, y = n - if (can_two_nodes_eq(x, m) && can_two_nodes_eq(y, n)) { - expr_ref_vector and_item(mgr); - - and_item.push_back(ctx.mk_eq_atom(x, m)); - and_item.push_back(ctx.mk_eq_atom(y, n)); - and_item.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_strlen(m))); - and_item.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_strlen(n))); - - expr_ref option3(mk_and(and_item), mgr); - arrangement_disjunction.push_back(option3); - // prioritize this case, it is easier - add_theory_aware_branching_info(option3, 0.5, l_true); - } - - if (!arrangement_disjunction.empty()) { - expr_ref premise(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); - expr_ref conclusion(mk_or(arrangement_disjunction), mgr); - if (m_params.m_StrongArrangements) { - expr_ref ax_strong(ctx.mk_eq_atom(premise, conclusion), mgr); - assert_axiom(ax_strong); - } else { - assert_implication(premise, conclusion); - } - // assert mutual exclusion between each branch of the arrangement - generate_mutual_exclusion(arrangement_disjunction); - } else { - TRACE("str", tout << "STOP: no split option found for two EQ concats." << std::endl;); - } - } // (splitType == -1) -} - -/************************************************************* - * Type 2: concat(x, y) = concat(m, "str") - *************************************************************/ -bool theory_str::is_concat_eq_type2(expr * concatAst1, expr * concatAst2) { - expr * v1_arg0 = to_app(concatAst1)->get_arg(0); - expr * v1_arg1 = to_app(concatAst1)->get_arg(1); - expr * v2_arg0 = to_app(concatAst2)->get_arg(0); - expr * v2_arg1 = to_app(concatAst2)->get_arg(1); - - if ((!u.str.is_string(v1_arg0)) && u.str.is_string(v1_arg1) - && (!u.str.is_string(v2_arg0)) && (!u.str.is_string(v2_arg1))) { - return true; - } else if ((!u.str.is_string(v2_arg0)) && u.str.is_string(v2_arg1) - && (!u.str.is_string(v1_arg0)) && (!u.str.is_string(v1_arg1))) { - return true; - } else { - return false; - } -} - -void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { - ast_manager & mgr = get_manager(); - context & ctx = get_context(); - - bool overlapAssumptionUsed = false; - - TRACE("str", tout << "process_concat_eq TYPE 2" << std::endl - << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl - << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; - ); - - if (!u.str.is_concat(to_app(concatAst1))) { - TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); - return; - } - if (!u.str.is_concat(to_app(concatAst2))) { - TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); - return; - } - - expr * x = NULL; - expr * y = NULL; - expr * strAst = NULL; - expr * m = NULL; - - expr * v1_arg0 = to_app(concatAst1)->get_arg(0); - expr * v1_arg1 = to_app(concatAst1)->get_arg(1); - expr * v2_arg0 = to_app(concatAst2)->get_arg(0); - expr * v2_arg1 = to_app(concatAst2)->get_arg(1); - - if (u.str.is_string(v1_arg1) && !u.str.is_string(v2_arg1)) { - m = v1_arg0; - strAst = v1_arg1; - x = v2_arg0; - y = v2_arg1; - } else { - m = v2_arg0; - strAst = v2_arg1; - x = v1_arg0; - y = v1_arg1; - } - - zstring strValue; - u.str.is_string(strAst, strValue); - - rational x_len, y_len, m_len, str_len; - bool x_len_exists = get_len_value(x, x_len); - bool y_len_exists = get_len_value(y, y_len); - bool m_len_exists = get_len_value(m, m_len); - bool str_len_exists = true; - str_len = rational(strValue.length()); - - // setup - - expr * xorFlag = NULL; - expr * temp1 = NULL; - std::pair key1(concatAst1, concatAst2); - std::pair key2(concatAst2, concatAst1); - - // check the entries in this map to make sure they're still in scope - // before we use them. - - std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); - std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); - - // prevent checking scope for the XOR term, as it's always in the same scope as the split var - - bool entry1InScope; - if (entry1 == varForBreakConcat.end()) { - entry1InScope = false; - } else { - if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end() - /*|| internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end()*/ - ) { - entry1InScope = false; - } else { - entry1InScope = true; - } - } - - bool entry2InScope; - if (entry2 == varForBreakConcat.end()) { - entry2InScope = false; - } else { - if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end() - /*|| internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end()*/ - ) { - entry2InScope = false; - } else { - entry2InScope = true; - } - } - - TRACE("str", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl - << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); - - - if (!entry1InScope && !entry2InScope) { - temp1 = mk_nonempty_str_var(); - xorFlag = mk_internal_xor_var(); - varForBreakConcat[key1][0] = temp1; - varForBreakConcat[key1][1] = xorFlag; - } else { - if (entry1InScope) { - temp1 = varForBreakConcat[key1][0]; - xorFlag = varForBreakConcat[key1][1]; - } else if (entry2InScope) { - temp1 = varForBreakConcat[key2][0]; - xorFlag = varForBreakConcat[key2][1]; - } - refresh_theory_var(temp1); - add_nonempty_constraint(temp1); - } - - int splitType = -1; - if (x_len_exists && m_len_exists) { - if (x_len < m_len) - splitType = 0; - else if (x_len == m_len) - splitType = 1; - else - splitType = 2; - } - if (splitType == -1 && y_len_exists && str_len_exists) { - if (y_len > str_len) - splitType = 0; - else if (y_len == str_len) - splitType = 1; - else - splitType = 2; - } - - TRACE("str", tout << "Split type " << splitType << std::endl;); - - // Provide fewer split options when length information is available. - - if (splitType == 0) { - // M cuts Y - // | x | y | - // | m | str | - expr_ref temp1_strAst(mk_concat(temp1, strAst), mgr); - if (can_two_nodes_eq(y, temp1_strAst)) { - expr_ref_vector l_items(mgr); - l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); - - expr_ref_vector r_items(mgr); - expr_ref x_temp1(mk_concat(x, temp1), mgr); - r_items.push_back(ctx.mk_eq_atom(m, x_temp1)); - r_items.push_back(ctx.mk_eq_atom(y, temp1_strAst)); - - if (x_len_exists && m_len_exists) { - l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); - l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); - rational m_sub_x = (m_len - x_len); - r_items.push_back(ctx.mk_eq_atom(mk_strlen(temp1), mk_int(m_sub_x))); - } else { - l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); - l_items.push_back(ctx.mk_eq_atom(mk_strlen(strAst), mk_int(str_len))); - rational y_sub_str = (y_len - str_len); - r_items.push_back(ctx.mk_eq_atom(mk_strlen(temp1), mk_int(y_sub_str))); + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len))); + rational y_sub_n = y_len - n_len; + ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t1), mk_int(y_sub_n))); } - expr_ref ax_l(mk_and(l_items), mgr); - expr_ref ax_r(mk_and(r_items), mgr); + expr_ref ax_l(mk_and(ax_l_items), mgr); + expr_ref ax_r(mk_and(ax_r_items), mgr); - if (!avoidLoopCut || !(has_self_cut(m, y))) { - // break down option 2-1 - add_cut_info_merge(temp1, sLevel, y); - add_cut_info_merge(temp1, sLevel, m); - - if (m_params.m_StrongArrangements) { - expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); - assert_axiom(ax_strong); - } else { - assert_implication(ax_l, ax_r); - } - } else { - loopDetected = true; - - if (m_params.m_FiniteOverlapModels) { - expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); - assert_implication(ax_l, tester); - add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); - } else { - TRACE("str", tout << "AVOID LOOP: SKIP" << std::endl;); - TRACE("str", {print_cut_var(m, tout); print_cut_var(y, tout);}); - - if (!overlapAssumptionUsed) { - overlapAssumptionUsed = true; - assert_implication(ax_l, m_theoryStrOverlapAssumption_term); - } - } - } - } - } else if (splitType == 1) { - // | x | y | - // | m | str | - expr_ref ax_l1(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); - expr_ref ax_l2(mgr.mk_or( - ctx.mk_eq_atom(mk_strlen(x), mk_strlen(m)), - ctx.mk_eq_atom(mk_strlen(y), mk_strlen(strAst))), mgr); - expr_ref ax_l(mgr.mk_and(ax_l1, ax_l2), mgr); - expr_ref ax_r(mgr.mk_and(ctx.mk_eq_atom(x, m), ctx.mk_eq_atom(y, strAst)), mgr); - assert_implication(ax_l, ax_r); - } else if (splitType == 2) { - // m cut y, - // | x | y | - // | m | str | - rational lenDelta; - expr_ref_vector l_items(mgr); - l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); - if (x_len_exists && m_len_exists) { - l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); - l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); - lenDelta = x_len - m_len; - } else { - l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); - lenDelta = str_len - y_len; - } - TRACE("str", - tout - << "xLen? " << (x_len_exists ? "yes" : "no") << std::endl - << "mLen? " << (m_len_exists ? "yes" : "no") << std::endl - << "yLen? " << (y_len_exists ? "yes" : "no") << std::endl - << "xLen = " << x_len.to_string() << std::endl - << "yLen = " << y_len.to_string() << std::endl - << "mLen = " << m_len.to_string() << std::endl - << "strLen = " << str_len.to_string() << std::endl - << "lenDelta = " << lenDelta.to_string() << std::endl - << "strValue = \"" << strValue << "\" (len=" << strValue.length() << ")" << "\n" - ; - ); - - zstring part1Str = strValue.extract(0, lenDelta.get_unsigned()); - zstring part2Str = strValue.extract(lenDelta.get_unsigned(), strValue.length() - lenDelta.get_unsigned()); - - expr_ref prefixStr(mk_string(part1Str), mgr); - expr_ref x_concat(mk_concat(m, prefixStr), mgr); - expr_ref cropStr(mk_string(part2Str), mgr); - - if (can_two_nodes_eq(x, x_concat) && can_two_nodes_eq(y, cropStr)) { - expr_ref_vector r_items(mgr); - r_items.push_back(ctx.mk_eq_atom(x, x_concat)); - r_items.push_back(ctx.mk_eq_atom(y, cropStr)); - expr_ref ax_l(mk_and(l_items), mgr); - expr_ref ax_r(mk_and(r_items), mgr); - - if (m_params.m_StrongArrangements) { - expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); - assert_axiom(ax_strong); - } else { - assert_implication(ax_l, ax_r); - } - } else { - // negate! It's impossible to split str with these lengths - TRACE("str", tout << "CONFLICT: Impossible to split str with these lengths." << std::endl;); - expr_ref ax_l(mk_and(l_items), mgr); - assert_axiom(mgr.mk_not(ax_l)); - } - } else { - // Split type -1: no idea about the length... - expr_ref_vector arrangement_disjunction(mgr); - - expr_ref temp1_strAst(mk_concat(temp1, strAst), mgr); - - // m cuts y - if (can_two_nodes_eq(y, temp1_strAst)) { - if (!avoidLoopCut || !has_self_cut(m, y)) { - // break down option 2-1 - expr_ref_vector and_item(mgr); - - expr_ref x_temp1(mk_concat(x, temp1), mgr); - and_item.push_back(ctx.mk_eq_atom(m, x_temp1)); - and_item.push_back(ctx.mk_eq_atom(y, temp1_strAst)); - - and_item.push_back(ctx.mk_eq_atom(mk_strlen(m), - m_autil.mk_add(mk_strlen(x), mk_strlen(temp1)))); - - expr_ref option1(mk_and(and_item), mgr); - arrangement_disjunction.push_back(option1); - add_theory_aware_branching_info(option1, 0.1, l_true); - add_cut_info_merge(temp1, ctx.get_scope_level(), y); - add_cut_info_merge(temp1, ctx.get_scope_level(), m); - } else { - loopDetected = true; - if (m_params.m_FiniteOverlapModels) { - expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); - arrangement_disjunction.push_back(tester); - add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); - } else { - TRACE("str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - TRACE("str", {print_cut_var(m, tout); print_cut_var(y, tout);}); - - if (!overlapAssumptionUsed) { - overlapAssumptionUsed = true; - arrangement_disjunction.push_back(m_theoryStrOverlapAssumption_term); - } - } - } - } - - for (unsigned int i = 0; i <= strValue.length(); ++i) { - zstring part1Str = strValue.extract(0, i); - zstring part2Str = strValue.extract(i, strValue.length() - i); - expr_ref prefixStr(mk_string(part1Str), mgr); - expr_ref x_concat(mk_concat(m, prefixStr), mgr); - expr_ref cropStr(mk_string(part2Str), mgr); - if (can_two_nodes_eq(x, x_concat) && can_two_nodes_eq(y, cropStr)) { - // break down option 2-2 - expr_ref_vector and_item(mgr); - and_item.push_back(ctx.mk_eq_atom(x, x_concat)); - and_item.push_back(ctx.mk_eq_atom(y, cropStr)); - and_item.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(part2Str.length()))); - expr_ref option2(mk_and(and_item), mgr); - arrangement_disjunction.push_back(option2); - double priority; - // prioritize the option where y is equal to the original string - if (i == 0) { - priority = 0.5; - } else { - priority = 0.1; - } - add_theory_aware_branching_info(option2, priority, l_true); - } - } - - if (!arrangement_disjunction.empty()) { - expr_ref implyR(mk_or(arrangement_disjunction), mgr); - - if (m_params.m_StrongArrangements) { - expr_ref implyLHS(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); - expr_ref ax_strong(ctx.mk_eq_atom(implyLHS, implyR), mgr); - assert_axiom(ax_strong); - } else { - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); - } - generate_mutual_exclusion(arrangement_disjunction); - } else { - TRACE("str", tout << "STOP: Should not split two EQ concats." << std::endl;); - } - } // (splitType == -1) -} - -/************************************************************* - * Type 3: concat(x, y) = concat("str", n) - *************************************************************/ -bool theory_str::is_concat_eq_type3(expr * concatAst1, expr * concatAst2) { - expr * v1_arg0 = to_app(concatAst1)->get_arg(0); - expr * v1_arg1 = to_app(concatAst1)->get_arg(1); - expr * v2_arg0 = to_app(concatAst2)->get_arg(0); - expr * v2_arg1 = to_app(concatAst2)->get_arg(1); - - if (u.str.is_string(v1_arg0) && (!u.str.is_string(v1_arg1)) - && (!u.str.is_string(v2_arg0)) && (!u.str.is_string(v2_arg1))) { - return true; - } else if (u.str.is_string(v2_arg0) && (!u.str.is_string(v2_arg1)) - && (!u.str.is_string(v1_arg0)) && (!u.str.is_string(v1_arg1))) { - return true; - } else { - return false; - } -} - -void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { - ast_manager & mgr = get_manager(); - context & ctx = get_context(); - - bool overlapAssumptionUsed = false; - - TRACE("str", tout << "process_concat_eq TYPE 3" << std::endl - << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl - << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; - ); - - if (!u.str.is_concat(to_app(concatAst1))) { - TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); - return; - } - if (!u.str.is_concat(to_app(concatAst2))) { - TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); - return; - } - - expr * v1_arg0 = to_app(concatAst1)->get_arg(0); - expr * v1_arg1 = to_app(concatAst1)->get_arg(1); - expr * v2_arg0 = to_app(concatAst2)->get_arg(0); - expr * v2_arg1 = to_app(concatAst2)->get_arg(1); - - expr * x = NULL; - expr * y = NULL; - expr * strAst = NULL; - expr * n = NULL; - - if (u.str.is_string(v1_arg0) && !u.str.is_string(v2_arg0)) { - strAst = v1_arg0; - n = v1_arg1; - x = v2_arg0; - y = v2_arg1; - } else { - strAst = v2_arg0; - n = v2_arg1; - x = v1_arg0; - y = v1_arg1; - } - - zstring strValue; - u.str.is_string(strAst, strValue); - - rational x_len, y_len, str_len, n_len; - bool x_len_exists = get_len_value(x, x_len); - bool y_len_exists = get_len_value(y, y_len); - str_len = rational((unsigned)(strValue.length())); - bool n_len_exists = get_len_value(n, n_len); - - expr_ref xorFlag(mgr); - expr_ref temp1(mgr); - std::pair key1(concatAst1, concatAst2); - std::pair key2(concatAst2, concatAst1); - - // check the entries in this map to make sure they're still in scope - // before we use them. - - std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); - std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); - - bool entry1InScope; - if (entry1 == varForBreakConcat.end()) { - entry1InScope = false; - } else { - if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end() - /* || internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end() */) { - entry1InScope = false; - } else { - entry1InScope = true; - } - } - - bool entry2InScope; - if (entry2 == varForBreakConcat.end()) { - entry2InScope = false; - } else { - if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end() - /* || internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end() */) { - entry2InScope = false; - } else { - entry2InScope = true; - } - } - - TRACE("str", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl - << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); - - - if (!entry1InScope && !entry2InScope) { - temp1 = mk_nonempty_str_var(); - xorFlag = mk_internal_xor_var(); - - varForBreakConcat[key1][0] = temp1; - varForBreakConcat[key1][1] = xorFlag; - } else { - if (entry1InScope) { - temp1 = varForBreakConcat[key1][0]; - xorFlag = varForBreakConcat[key1][1]; - } else if (varForBreakConcat.find(key2) != varForBreakConcat.end()) { - temp1 = varForBreakConcat[key2][0]; - xorFlag = varForBreakConcat[key2][1]; - } - refresh_theory_var(temp1); - add_nonempty_constraint(temp1); - } - - - - int splitType = -1; - if (x_len_exists) { - if (x_len < str_len) - splitType = 0; - else if (x_len == str_len) - splitType = 1; - else - splitType = 2; - } - if (splitType == -1 && y_len_exists && n_len_exists) { - if (y_len > n_len) - splitType = 0; - else if (y_len == n_len) - splitType = 1; - else - splitType = 2; - } - - TRACE("str", tout << "Split type " << splitType << std::endl;); - - // Provide fewer split options when length information is available. - if (splitType == 0) { - // | x | y | - // | str | n | - expr_ref_vector litems(mgr); - litems.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); - rational prefixLen; - if (!x_len_exists) { - prefixLen = str_len - (y_len - n_len); - litems.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); - litems.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len))); - } else { - prefixLen = x_len; - litems.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); - } - zstring prefixStr = strValue.extract(0, prefixLen.get_unsigned()); - rational str_sub_prefix = str_len - prefixLen; - zstring suffixStr = strValue.extract(prefixLen.get_unsigned(), str_sub_prefix.get_unsigned()); - expr_ref prefixAst(mk_string(prefixStr), mgr); - expr_ref suffixAst(mk_string(suffixStr), mgr); - expr_ref ax_l(mgr.mk_and(litems.size(), litems.c_ptr()), mgr); - - expr_ref suf_n_concat(mk_concat(suffixAst, n), mgr); - if (can_two_nodes_eq(x, prefixAst) && can_two_nodes_eq(y, suf_n_concat)) { - expr_ref_vector r_items(mgr); - r_items.push_back(ctx.mk_eq_atom(x, prefixAst)); - r_items.push_back(ctx.mk_eq_atom(y, suf_n_concat)); - - if (m_params.m_StrongArrangements) { - expr_ref ax_strong(ctx.mk_eq_atom(ax_l, mk_and(r_items)), mgr); - assert_axiom(ax_strong); - } else { - assert_implication(ax_l, mk_and(r_items)); - } - } else { - // negate! It's impossible to split str with these lengths - TRACE("str", tout << "CONFLICT: Impossible to split str with these lengths." << std::endl;); - assert_axiom(mgr.mk_not(ax_l)); - } - } - else if (splitType == 1) { - expr_ref ax_l1(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); - expr_ref ax_l2(mgr.mk_or( - ctx.mk_eq_atom(mk_strlen(x), mk_strlen(strAst)), - ctx.mk_eq_atom(mk_strlen(y), mk_strlen(n))), mgr); - expr_ref ax_l(mgr.mk_and(ax_l1, ax_l2), mgr); - expr_ref ax_r(mgr.mk_and(ctx.mk_eq_atom(x, strAst), ctx.mk_eq_atom(y, n)), mgr); - - if (m_params.m_StrongArrangements) { - expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); - assert_axiom(ax_strong); - } else { - assert_implication(ax_l, ax_r); - } - } - else if (splitType == 2) { - // | x | y | - // | str | n | - expr_ref_vector litems(mgr); - litems.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); - rational tmpLen; - if (!x_len_exists) { - tmpLen = n_len - y_len; - litems.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); - litems.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len))); - } else { - tmpLen = x_len - str_len; - litems.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); - } - expr_ref ax_l(mgr.mk_and(litems.size(), litems.c_ptr()), mgr); - - expr_ref str_temp1(mk_concat(strAst, temp1), mgr); - expr_ref temp1_y(mk_concat(temp1, y), mgr); - - if (can_two_nodes_eq(x, str_temp1)) { - if (!avoidLoopCut || !(has_self_cut(x, n))) { - expr_ref_vector r_items(mgr); - r_items.push_back(ctx.mk_eq_atom(x, str_temp1)); - r_items.push_back(ctx.mk_eq_atom(n, temp1_y)); - r_items.push_back(ctx.mk_eq_atom(mk_strlen(temp1), mk_int(tmpLen))); - expr_ref ax_r(mk_and(r_items), mgr); - - //Cut Info - add_cut_info_merge(temp1, sLevel, x); - add_cut_info_merge(temp1, sLevel, n); + if (!has_self_cut(m, y)) { + // Cut Info + add_cut_info_merge(t1, sLevel, m); + add_cut_info_merge(t1, sLevel, y); if (m_params.m_StrongArrangements) { expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); @@ -3937,82 +3112,117 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { TRACE("str", tout << "AVOID LOOP: SKIPPED" << std::endl;); - TRACE("str", {print_cut_var(x, tout); print_cut_var(n, tout);}); + TRACE("str", {print_cut_var(m, tout); print_cut_var(y, tout);}); if (!overlapAssumptionUsed) { - overlapAssumptionUsed = true; - assert_implication(ax_l, m_theoryStrOverlapAssumption_term); + overlapAssumptionUsed = true; + assert_implication(ax_l, m_theoryStrOverlapAssumption_term); } } } - } - // else { - // // negate! It's impossible to split str with these lengths - // __debugPrint(logFile, "[Conflict] Negate! It's impossible to split str with these lengths @ %d.\n", __LINE__); - // addAxiom(t, Z3_mk_not(ctx, ax_l), __LINE__); - // } - } - else { - // Split type -1. We know nothing about the length... + } else if (splitType == 1) { + // Type 1: + // len(x) = len(m) || len(y) = len(n) + expr_ref ax_l1(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); + expr_ref ax_l2(mgr.mk_or(ctx.mk_eq_atom(mk_strlen(x), mk_strlen(m)), ctx.mk_eq_atom(mk_strlen(y), mk_strlen(n))), mgr); + expr_ref ax_l(mgr.mk_and(ax_l1, ax_l2), mgr); + expr_ref ax_r(mgr.mk_and(ctx.mk_eq_atom(x,m), ctx.mk_eq_atom(y,n)), mgr); + assert_implication(ax_l, ax_r); + } else if (splitType == 2) { + // Type 2: X cuts N. + // len(x) > len(m) || len(y) < len(n) + expr_ref m_t2(mk_concat(m, t2), mgr); + expr_ref t2_y(mk_concat(t2, y), mgr); - expr_ref_vector arrangement_disjunction(mgr); + expr_ref_vector ax_l_items(mgr); + ax_l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); - int pos = 1; - for (unsigned int i = 0; i <= strValue.length(); i++) { - zstring part1Str = strValue.extract(0, i); - zstring part2Str = strValue.extract(i, strValue.length() - i); - expr_ref cropStr(mk_string(part1Str), mgr); - expr_ref suffixStr(mk_string(part2Str), mgr); - expr_ref y_concat(mk_concat(suffixStr, n), mgr); + expr_ref_vector ax_r_items(mgr); + ax_r_items.push_back(ctx.mk_eq_atom(x, m_t2)); + ax_r_items.push_back(ctx.mk_eq_atom(t2_y, n)); - if (can_two_nodes_eq(x, cropStr) && can_two_nodes_eq(y, y_concat)) { + if (m_len_exists && x_len_exists) { + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); + rational x_sub_m = x_len - m_len; + ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t2), mk_int(x_sub_m))); + } else { + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); + ax_l_items.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len))); + rational n_sub_y = n_len - y_len; + ax_r_items.push_back(ctx.mk_eq_atom(mk_strlen(t2), mk_int(n_sub_y))); + } + + expr_ref ax_l(mk_and(ax_l_items), mgr); + expr_ref ax_r(mk_and(ax_r_items), mgr); + + if (!has_self_cut(x, n)) { + // Cut Info + add_cut_info_merge(t2, sLevel, x); + add_cut_info_merge(t2, sLevel, n); + + if (m_params.m_StrongArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ax_l, ax_r); + } + } else { + loopDetected = true; + if (m_params.m_FiniteOverlapModels) { + expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); + assert_implication(ax_l, tester); + add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); + } else { + TRACE("str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + TRACE("str", {print_cut_var(m, tout); print_cut_var(y, tout);}); + + if (!overlapAssumptionUsed) { + overlapAssumptionUsed = true; + assert_implication(ax_l, m_theoryStrOverlapAssumption_term); + } + } + } + } else if (splitType == -1) { + // Here we don't really have a choice. We have no length information at all... + + // This vector will eventually contain one term for each possible arrangement we explore. + expr_ref_vector arrangement_disjunction(mgr); + + // break option 1: m cuts y + // len(x) < len(m) || len(y) > len(n) + if (!avoidLoopCut || !has_self_cut(m, y)) { expr_ref_vector and_item(mgr); - // break down option 3-1 - expr_ref x_eq_str(ctx.mk_eq_atom(x, cropStr), mgr); + // break down option 1-1 + expr_ref x_t1(mk_concat(x, t1), mgr); + expr_ref t1_n(mk_concat(t1, n), mgr); - and_item.push_back(x_eq_str); ++pos; - and_item.push_back(ctx.mk_eq_atom(y, y_concat)); - and_item.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_strlen(cropStr))); ++pos; + and_item.push_back(ctx.mk_eq_atom(m, x_t1)); + and_item.push_back(ctx.mk_eq_atom(y, t1_n)); - // and_item[pos++] = Z3_mk_eq(ctx, or_item[option], Z3_mk_eq(ctx, mk_length(t, y), mk_length(t, y_concat))); - // adding length constraint for _ = constStr seems slowing things down. + expr_ref x_plus_t1(m_autil.mk_add(mk_strlen(x), mk_strlen(t1)), mgr); + and_item.push_back(ctx.mk_eq_atom(mk_strlen(m), x_plus_t1)); + // These were crashing the solver because the integer theory + // expects a constant on the right-hand side. + // The things we want to assert here are len(m) > len(x) and len(y) > len(n). + // We rewrite A > B as A-B > 0 and then as not(A-B <= 0), + // and then, *because we aren't allowed to use subtraction*, + // as not(A + -1*B <= 0) + and_item.push_back( + mgr.mk_not(m_autil.mk_le( + m_autil.mk_add(mk_strlen(m), m_autil.mk_mul(mk_int(-1), mk_strlen(x))), + mk_int(0))) ); + and_item.push_back( + mgr.mk_not(m_autil.mk_le( + m_autil.mk_add(mk_strlen(y),m_autil.mk_mul(mk_int(-1), mk_strlen(n))), + mk_int(0))) ); expr_ref option1(mk_and(and_item), mgr); arrangement_disjunction.push_back(option1); - double priority; - if (i == strValue.length()) { - priority = 0.5; - } else { - priority = 0.1; - } - add_theory_aware_branching_info(option1, priority, l_true); - } - } + add_theory_aware_branching_info(option1, 0.1, l_true); - expr_ref strAst_temp1(mk_concat(strAst, temp1), mgr); - - - //-------------------------------------------------------- - // x cut n - //-------------------------------------------------------- - if (can_two_nodes_eq(x, strAst_temp1)) { - if (!avoidLoopCut || !(has_self_cut(x, n))) { - // break down option 3-2 - expr_ref_vector and_item(mgr); - - expr_ref temp1_y(mk_concat(temp1, y), mgr); - and_item.push_back(ctx.mk_eq_atom(x, strAst_temp1)); ++pos; - and_item.push_back(ctx.mk_eq_atom(n, temp1_y)); ++pos; - - and_item.push_back(ctx.mk_eq_atom(mk_strlen(x), - m_autil.mk_add(mk_strlen(strAst), mk_strlen(temp1)) ) ); ++pos; - - expr_ref option2(mk_and(and_item), mgr); - arrangement_disjunction.push_back(option2); - add_theory_aware_branching_info(option2, 0.1, l_true); - - add_cut_info_merge(temp1, sLevel, x); - add_cut_info_merge(temp1, sLevel, n); + add_cut_info_merge(t1, ctx.get_scope_level(), m); + add_cut_info_merge(t1, ctx.get_scope_level(), y); } else { loopDetected = true; if (m_params.m_FiniteOverlapModels) { @@ -4020,6259 +3230,3742 @@ void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { arrangement_disjunction.push_back(tester); add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); } else { - TRACE("str", tout << "AVOID LOOP: SKIPPED." << std::endl;); + TRACE("str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + TRACE("str", {print_cut_var(m, tout); print_cut_var(y, tout);}); + + if (!overlapAssumptionUsed) { + overlapAssumptionUsed = true; + arrangement_disjunction.push_back(m_theoryStrOverlapAssumption_term); + } + } + } + + // break option 2: + // x = m . t2 + // n = t2 . y + if (!avoidLoopCut || !has_self_cut(x, n)) { + expr_ref_vector and_item(mgr); + // break down option 1-2 + expr_ref m_t2(mk_concat(m, t2), mgr); + expr_ref t2_y(mk_concat(t2, y), mgr); + + and_item.push_back(ctx.mk_eq_atom(x, m_t2)); + and_item.push_back(ctx.mk_eq_atom(n, t2_y)); + + + expr_ref m_plus_t2(m_autil.mk_add(mk_strlen(m), mk_strlen(t2)), mgr); + + and_item.push_back(ctx.mk_eq_atom(mk_strlen(x), m_plus_t2)); + // want len(x) > len(m) and len(n) > len(y) + and_item.push_back( + mgr.mk_not(m_autil.mk_le( + m_autil.mk_add(mk_strlen(x), m_autil.mk_mul(mk_int(-1), mk_strlen(m))), + mk_int(0))) ); + and_item.push_back( + mgr.mk_not(m_autil.mk_le( + m_autil.mk_add(mk_strlen(n), m_autil.mk_mul(mk_int(-1), mk_strlen(y))), + mk_int(0))) ); + + expr_ref option2(mk_and(and_item), mgr); + arrangement_disjunction.push_back(option2); + add_theory_aware_branching_info(option2, 0.1, l_true); + + add_cut_info_merge(t2, ctx.get_scope_level(), x); + add_cut_info_merge(t2, ctx.get_scope_level(), n); + } else { + loopDetected = true; + if (m_params.m_FiniteOverlapModels) { + expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); + arrangement_disjunction.push_back(tester); + add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); + } else { + TRACE("str", tout << "AVOID LOOP: SKIPPED" << std::endl;); TRACE("str", {print_cut_var(x, tout); print_cut_var(n, tout);}); if (!overlapAssumptionUsed) { - overlapAssumptionUsed = true; - arrangement_disjunction.push_back(m_theoryStrOverlapAssumption_term); + overlapAssumptionUsed = true; + arrangement_disjunction.push_back(m_theoryStrOverlapAssumption_term); } } } - } + // option 3: + // x = m, y = n + if (can_two_nodes_eq(x, m) && can_two_nodes_eq(y, n)) { + expr_ref_vector and_item(mgr); - if (!arrangement_disjunction.empty()) { - expr_ref implyR(mk_or(arrangement_disjunction), mgr); + and_item.push_back(ctx.mk_eq_atom(x, m)); + and_item.push_back(ctx.mk_eq_atom(y, n)); + and_item.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_strlen(m))); + and_item.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_strlen(n))); - if (m_params.m_StrongArrangements) { - expr_ref ax_lhs(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); - expr_ref ax_strong(ctx.mk_eq_atom(ax_lhs, implyR), mgr); - assert_axiom(ax_strong); + expr_ref option3(mk_and(and_item), mgr); + arrangement_disjunction.push_back(option3); + // prioritize this case, it is easier + add_theory_aware_branching_info(option3, 0.5, l_true); + } + + if (!arrangement_disjunction.empty()) { + expr_ref premise(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); + expr_ref conclusion(mk_or(arrangement_disjunction), mgr); + if (m_params.m_StrongArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom(premise, conclusion), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(premise, conclusion); + } + // assert mutual exclusion between each branch of the arrangement + generate_mutual_exclusion(arrangement_disjunction); } else { - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + TRACE("str", tout << "STOP: no split option found for two EQ concats." << std::endl;); } - generate_mutual_exclusion(arrangement_disjunction); + } // (splitType == -1) + } + + /************************************************************* + * Type 2: concat(x, y) = concat(m, "str") + *************************************************************/ + bool theory_str::is_concat_eq_type2(expr * concatAst1, expr * concatAst2) { + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + if ((!u.str.is_string(v1_arg0)) && u.str.is_string(v1_arg1) + && (!u.str.is_string(v2_arg0)) && (!u.str.is_string(v2_arg1))) { + return true; + } else if ((!u.str.is_string(v2_arg0)) && u.str.is_string(v2_arg1) + && (!u.str.is_string(v1_arg0)) && (!u.str.is_string(v1_arg1))) { + return true; } else { - TRACE("str", tout << "STOP: should not split two eq. concats" << std::endl;); + return false; } } -} + void theory_str::process_concat_eq_type2(expr * concatAst1, expr * concatAst2) { + ast_manager & mgr = get_manager(); + context & ctx = get_context(); -/************************************************************* - * Type 4: concat("str1", y) = concat("str2", n) - *************************************************************/ -bool theory_str::is_concat_eq_type4(expr * concatAst1, expr * concatAst2) { - expr * v1_arg0 = to_app(concatAst1)->get_arg(0); - expr * v1_arg1 = to_app(concatAst1)->get_arg(1); - expr * v2_arg0 = to_app(concatAst2)->get_arg(0); - expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + bool overlapAssumptionUsed = false; - if (u.str.is_string(v1_arg0) && (!u.str.is_string(v1_arg1)) - && u.str.is_string(v2_arg0) && (!u.str.is_string(v2_arg1))) { - return true; - } else { - return false; - } -} + TRACE("str", tout << "process_concat_eq TYPE 2" << std::endl + << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl + << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; + ); -void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { - ast_manager & mgr = get_manager(); - context & ctx = get_context(); - TRACE("str", tout << "process_concat_eq TYPE 4" << std::endl - << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl - << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; - ); - - if (!u.str.is_concat(to_app(concatAst1))) { - TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); - return; - } - if (!u.str.is_concat(to_app(concatAst2))) { - TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); - return; - } - - expr * v1_arg0 = to_app(concatAst1)->get_arg(0); - expr * v1_arg1 = to_app(concatAst1)->get_arg(1); - expr * v2_arg0 = to_app(concatAst2)->get_arg(0); - expr * v2_arg1 = to_app(concatAst2)->get_arg(1); - - expr * str1Ast = v1_arg0; - expr * y = v1_arg1; - expr * str2Ast = v2_arg0; - expr * n = v2_arg1; - - zstring str1Value, str2Value; - u.str.is_string(str1Ast, str1Value); - u.str.is_string(str2Ast, str2Value); - - unsigned int str1Len = str1Value.length(); - unsigned int str2Len = str2Value.length(); - - int commonLen = (str1Len > str2Len) ? str2Len : str1Len; - if (str1Value.extract(0, commonLen) != str2Value.extract(0, commonLen)) { - TRACE("str", tout << "Conflict: " << mk_ismt2_pp(concatAst1, mgr) - << " has no common prefix with " << mk_ismt2_pp(concatAst2, mgr) << std::endl;); - expr_ref toNegate(mgr.mk_not(ctx.mk_eq_atom(concatAst1, concatAst2)), mgr); - assert_axiom(toNegate); - return; - } else { - if (str1Len > str2Len) { - zstring deltaStr = str1Value.extract(str2Len, str1Len - str2Len); - expr_ref tmpAst(mk_concat(mk_string(deltaStr), y), mgr); - if (!in_same_eqc(tmpAst, n)) { - // break down option 4-1 - expr_ref implyR(ctx.mk_eq_atom(n, tmpAst), mgr); - if (m_params.m_StrongArrangements) { - expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); - assert_axiom(ax_strong); - } else { - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); - } - } - } else if (str1Len == str2Len) { - if (!in_same_eqc(n, y)) { - //break down option 4-2 - expr_ref implyR(ctx.mk_eq_atom(n, y), mgr); - - if (m_params.m_StrongArrangements) { - expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); - assert_axiom(ax_strong); - } else { - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); - } - } - } else { - zstring deltaStr = str2Value.extract(str1Len, str2Len - str1Len); - expr_ref tmpAst(mk_concat(mk_string(deltaStr), n), mgr); - if (!in_same_eqc(y, tmpAst)) { - //break down option 4-3 - expr_ref implyR(ctx.mk_eq_atom(y, tmpAst), mgr); - if (m_params.m_StrongArrangements) { - expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); - assert_axiom(ax_strong); - } else { - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); - } - } + if (!u.str.is_concat(to_app(concatAst1))) { + TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); + return; } - } -} - -/************************************************************* - * case 5: concat(x, "str1") = concat(m, "str2") - *************************************************************/ -bool theory_str::is_concat_eq_type5(expr * concatAst1, expr * concatAst2) { - expr * v1_arg0 = to_app(concatAst1)->get_arg(0); - expr * v1_arg1 = to_app(concatAst1)->get_arg(1); - expr * v2_arg0 = to_app(concatAst2)->get_arg(0); - expr * v2_arg1 = to_app(concatAst2)->get_arg(1); - - if ((!u.str.is_string(v1_arg0)) && u.str.is_string(v1_arg1) - && (!u.str.is_string(v2_arg0)) && u.str.is_string(v2_arg1)) { - return true; - } else { - return false; - } -} - -void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { - ast_manager & mgr = get_manager(); - context & ctx = get_context(); - TRACE("str", tout << "process_concat_eq TYPE 5" << std::endl - << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl - << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; - ); - - if (!u.str.is_concat(to_app(concatAst1))) { - TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); - return; - } - if (!u.str.is_concat(to_app(concatAst2))) { - TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); - return; - } - - expr * v1_arg0 = to_app(concatAst1)->get_arg(0); - expr * v1_arg1 = to_app(concatAst1)->get_arg(1); - expr * v2_arg0 = to_app(concatAst2)->get_arg(0); - expr * v2_arg1 = to_app(concatAst2)->get_arg(1); - - expr * x = v1_arg0; - expr * str1Ast = v1_arg1; - expr * m = v2_arg0; - expr * str2Ast = v2_arg1; - - zstring str1Value, str2Value; - u.str.is_string(str1Ast, str1Value); - u.str.is_string(str2Ast, str2Value); - - unsigned int str1Len = str1Value.length(); - unsigned int str2Len = str2Value.length(); - - int cLen = (str1Len > str2Len) ? str2Len : str1Len; - if (str1Value.extract(str1Len - cLen, cLen) != str2Value.extract(str2Len - cLen, cLen)) { - TRACE("str", tout << "Conflict: " << mk_ismt2_pp(concatAst1, mgr) - << " has no common suffix with " << mk_ismt2_pp(concatAst2, mgr) << std::endl;); - expr_ref toNegate(mgr.mk_not(ctx.mk_eq_atom(concatAst1, concatAst2)), mgr); - assert_axiom(toNegate); - return; - } else { - if (str1Len > str2Len) { - zstring deltaStr = str1Value.extract(0, str1Len - str2Len); - expr_ref x_deltaStr(mk_concat(x, mk_string(deltaStr)), mgr); - if (!in_same_eqc(m, x_deltaStr)) { - expr_ref implyR(ctx.mk_eq_atom(m, x_deltaStr), mgr); - if (m_params.m_StrongArrangements) { - expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); - assert_axiom(ax_strong); - } else { - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); - } - } - } else if (str1Len == str2Len) { - // test - if (!in_same_eqc(x, m)) { - expr_ref implyR(ctx.mk_eq_atom(x, m), mgr); - if (m_params.m_StrongArrangements) { - expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); - assert_axiom(ax_strong); - } else { - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); - } - } - } else { - zstring deltaStr = str2Value.extract(0, str2Len - str1Len); - expr_ref m_deltaStr(mk_concat(m, mk_string(deltaStr)), mgr); - if (!in_same_eqc(x, m_deltaStr)) { - expr_ref implyR(ctx.mk_eq_atom(x, m_deltaStr), mgr); - if (m_params.m_StrongArrangements) { - expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); - assert_axiom(ax_strong); - } else { - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); - } - } + if (!u.str.is_concat(to_app(concatAst2))) { + TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); + return; } - } -} -/************************************************************* - * case 6: concat("str1", y) = concat(m, "str2") - *************************************************************/ -bool theory_str::is_concat_eq_type6(expr * concatAst1, expr * concatAst2) { - expr * v1_arg0 = to_app(concatAst1)->get_arg(0); - expr * v1_arg1 = to_app(concatAst1)->get_arg(1); - expr * v2_arg0 = to_app(concatAst2)->get_arg(0); - expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + expr * x = NULL; + expr * y = NULL; + expr * strAst = NULL; + expr * m = NULL; - if (u.str.is_string(v1_arg0) && (!u.str.is_string(v1_arg1)) - && (!u.str.is_string(v2_arg0)) && u.str.is_string(v2_arg1)) { - return true; - } else if (u.str.is_string(v2_arg0) && (!u.str.is_string(v2_arg1)) - && (!u.str.is_string(v1_arg0)) && u.str.is_string(v1_arg1)) { - return true; - } else { - return false; - } -} + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); -void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { - ast_manager & mgr = get_manager(); - context & ctx = get_context(); - TRACE("str", tout << "process_concat_eq TYPE 6" << std::endl - << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl - << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; - ); + if (u.str.is_string(v1_arg1) && !u.str.is_string(v2_arg1)) { + m = v1_arg0; + strAst = v1_arg1; + x = v2_arg0; + y = v2_arg1; + } else { + m = v2_arg0; + strAst = v2_arg1; + x = v1_arg0; + y = v1_arg1; + } - if (!u.str.is_concat(to_app(concatAst1))) { - TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); - return; - } - if (!u.str.is_concat(to_app(concatAst2))) { - TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); - return; - } + zstring strValue; + u.str.is_string(strAst, strValue); - expr * v1_arg0 = to_app(concatAst1)->get_arg(0); - expr * v1_arg1 = to_app(concatAst1)->get_arg(1); - expr * v2_arg0 = to_app(concatAst2)->get_arg(0); - expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + rational x_len, y_len, m_len, str_len; + bool x_len_exists = get_len_value(x, x_len); + bool y_len_exists = get_len_value(y, y_len); + bool m_len_exists = get_len_value(m, m_len); + bool str_len_exists = true; + str_len = rational(strValue.length()); + // setup - expr * str1Ast = NULL; - expr * y = NULL; - expr * m = NULL; - expr * str2Ast = NULL; + expr * xorFlag = NULL; + expr * temp1 = NULL; + std::pair key1(concatAst1, concatAst2); + std::pair key2(concatAst2, concatAst1); - if (u.str.is_string(v1_arg0)) { - str1Ast = v1_arg0; - y = v1_arg1; - m = v2_arg0; - str2Ast = v2_arg1; - } else { - str1Ast = v2_arg0; - y = v2_arg1; - m = v1_arg0; - str2Ast = v1_arg1; - } + // check the entries in this map to make sure they're still in scope + // before we use them. - zstring str1Value, str2Value; - u.str.is_string(str1Ast, str1Value); - u.str.is_string(str2Ast, str2Value); + std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); + std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); - unsigned int str1Len = str1Value.length(); - unsigned int str2Len = str2Value.length(); + // prevent checking scope for the XOR term, as it's always in the same scope as the split var - //---------------------------------------- - //(a) |---str1---|----y----| - // |--m--|-----str2-----| - // - //(b) |---str1---|----y----| - // |-----m----|--str2---| - // - //(c) |---str1---|----y----| - // |------m------|-str2-| - //---------------------------------------- - - std::list overlapLen; - overlapLen.push_back(0); - - for (unsigned int i = 1; i <= str1Len && i <= str2Len; i++) { - if (str1Value.extract(str1Len - i, i) == str2Value.extract(0, i)) - overlapLen.push_back(i); - } - - //---------------------------------------------------------------- - expr * commonVar = NULL; - expr * xorFlag = NULL; - std::pair key1(concatAst1, concatAst2); - std::pair key2(concatAst2, concatAst1); - - // check the entries in this map to make sure they're still in scope - // before we use them. - - std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); - std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); - - bool entry1InScope; - if (entry1 == varForBreakConcat.end()) { - entry1InScope = false; - } else { - if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end() - /* || internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end() */) { + bool entry1InScope; + if (entry1 == varForBreakConcat.end()) { entry1InScope = false; } else { - entry1InScope = true; + if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end() + /*|| internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end()*/ + ) { + entry1InScope = false; + } else { + entry1InScope = true; + } } - } - bool entry2InScope; - if (entry2 == varForBreakConcat.end()) { - entry2InScope = false; - } else { - if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end() - /* || internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end() */) { + bool entry2InScope; + if (entry2 == varForBreakConcat.end()) { entry2InScope = false; } else { - entry2InScope = true; + if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end() + /*|| internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end()*/ + ) { + entry2InScope = false; + } else { + entry2InScope = true; + } } - } - TRACE("str", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl - << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); + TRACE("str", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl + << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); - if (!entry1InScope && !entry2InScope) { - commonVar = mk_nonempty_str_var(); - xorFlag = mk_internal_xor_var(); - varForBreakConcat[key1][0] = commonVar; - varForBreakConcat[key1][1] = xorFlag; - } else { - if (entry1InScope) { - commonVar = (entry1->second)[0]; - xorFlag = (entry1->second)[1]; + + if (!entry1InScope && !entry2InScope) { + temp1 = mk_nonempty_str_var(); + xorFlag = mk_internal_xor_var(); + varForBreakConcat[key1][0] = temp1; + varForBreakConcat[key1][1] = xorFlag; } else { - commonVar = (entry2->second)[0]; - xorFlag = (entry2->second)[1]; - } - refresh_theory_var(commonVar); - add_nonempty_constraint(commonVar); - } - - bool overlapAssumptionUsed = false; - - expr_ref_vector arrangement_disjunction(mgr); - int pos = 1; - - if (!avoidLoopCut || !has_self_cut(m, y)) { - expr_ref_vector and_item(mgr); - - expr_ref str1_commonVar(mk_concat(str1Ast, commonVar), mgr); - and_item.push_back(ctx.mk_eq_atom(m, str1_commonVar)); - pos += 1; - - expr_ref commonVar_str2(mk_concat(commonVar, str2Ast), mgr); - and_item.push_back(ctx.mk_eq_atom(y, commonVar_str2)); - pos += 1; - - and_item.push_back(ctx.mk_eq_atom(mk_strlen(m), - m_autil.mk_add(mk_strlen(str1Ast), mk_strlen(commonVar)) )); - pos += 1; - - // addItems[0] = mk_length(t, commonVar); - // addItems[1] = mk_length(t, str2Ast); - // and_item[pos++] = Z3_mk_eq(ctx, or_item[option], Z3_mk_eq(ctx, mk_length(t, y), Z3_mk_add(ctx, 2, addItems))); - - expr_ref option1(mk_and(and_item), mgr); - arrangement_disjunction.push_back(option1); - add_theory_aware_branching_info(option1, 0.1, l_true); - } else { - loopDetected = true; - - if (m_params.m_FiniteOverlapModels) { - expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); - arrangement_disjunction.push_back(tester); - add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); - } else { - TRACE("str", tout << "AVOID LOOP: SKIPPED." << std::endl;); - TRACE("str", print_cut_var(m, tout); print_cut_var(y, tout);); - - // only add the overlap assumption one time - if (!overlapAssumptionUsed) { - arrangement_disjunction.push_back(m_theoryStrOverlapAssumption_term); - overlapAssumptionUsed = true; + if (entry1InScope) { + temp1 = varForBreakConcat[key1][0]; + xorFlag = varForBreakConcat[key1][1]; + } else if (entry2InScope) { + temp1 = varForBreakConcat[key2][0]; + xorFlag = varForBreakConcat[key2][1]; } + refresh_theory_var(temp1); + add_nonempty_constraint(temp1); } - } - for (std::list::iterator itor = overlapLen.begin(); itor != overlapLen.end(); itor++) { - unsigned int overLen = *itor; - zstring prefix = str1Value.extract(0, str1Len - overLen); - zstring suffix = str2Value.extract(overLen, str2Len - overLen); - - expr_ref_vector and_item(mgr); - - expr_ref prefixAst(mk_string(prefix), mgr); - expr_ref x_eq_prefix(ctx.mk_eq_atom(m, prefixAst), mgr); - and_item.push_back(x_eq_prefix); - pos += 1; - - and_item.push_back( - ctx.mk_eq_atom(mk_strlen(m), mk_strlen(prefixAst))); - pos += 1; - - // adding length constraint for _ = constStr seems slowing things down. - - expr_ref suffixAst(mk_string(suffix), mgr); - expr_ref y_eq_suffix(ctx.mk_eq_atom(y, suffixAst), mgr); - and_item.push_back(y_eq_suffix); - pos += 1; - - and_item.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_strlen(suffixAst))); - pos += 1; - - expr_ref option2(mk_and(and_item), mgr); - arrangement_disjunction.push_back(option2); - double priority; - // prefer the option "str1" = x - if (prefix == str1Value) { - priority = 0.5; - } else { - priority = 0.1; + int splitType = -1; + if (x_len_exists && m_len_exists) { + if (x_len < m_len) + splitType = 0; + else if (x_len == m_len) + splitType = 1; + else + splitType = 2; } - add_theory_aware_branching_info(option2, priority, l_true); - } - - // case 6: concat("str1", y) = concat(m, "str2") - - expr_ref implyR(mk_or(arrangement_disjunction), mgr); - - if (m_params.m_StrongArrangements) { - expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); - assert_axiom(ax_strong); - } else { - assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); - } - generate_mutual_exclusion(arrangement_disjunction); -} - -void theory_str::process_unroll_eq_const_str(expr * unrollFunc, expr * constStr) { - ast_manager & m = get_manager(); - - if (!u.re.is_unroll(to_app(unrollFunc))) { - return; - } - if (!u.str.is_string(constStr)) { - return; - } - - expr * funcInUnroll = to_app(unrollFunc)->get_arg(0); - zstring strValue; - u.str.is_string(constStr, strValue); - - TRACE("str", tout << "unrollFunc: " << mk_pp(unrollFunc, m) << std::endl - << "constStr: " << mk_pp(constStr, m) << std::endl;); - - if (strValue == "") { - return; - } - - if (u.re.is_to_re(to_app(funcInUnroll))) { - unroll_str2reg_constStr(unrollFunc, constStr); - return; - } -} - -void theory_str::process_concat_eq_unroll(expr * concat, expr * unroll) { - context & ctx = get_context(); - ast_manager & mgr = get_manager(); - - TRACE("str", tout << "concat = " << mk_pp(concat, mgr) << ", unroll = " << mk_pp(unroll, mgr) << std::endl;); - - std::pair key = std::make_pair(concat, unroll); - expr_ref toAssert(mgr); - - if (concat_eq_unroll_ast_map.find(key) == concat_eq_unroll_ast_map.end()) { - expr_ref arg1(to_app(concat)->get_arg(0), mgr); - expr_ref arg2(to_app(concat)->get_arg(1), mgr); - expr_ref r1(to_app(unroll)->get_arg(0), mgr); - expr_ref t1(to_app(unroll)->get_arg(1), mgr); - - expr_ref v1(mk_regex_rep_var(), mgr); - expr_ref v2(mk_regex_rep_var(), mgr); - expr_ref v3(mk_regex_rep_var(), mgr); - expr_ref v4(mk_regex_rep_var(), mgr); - expr_ref v5(mk_regex_rep_var(), mgr); - - expr_ref t2(mk_unroll_bound_var(), mgr); - expr_ref t3(mk_unroll_bound_var(), mgr); - expr_ref emptyStr(mk_string(""), mgr); - - expr_ref unroll1(mk_unroll(r1, t2), mgr); - expr_ref unroll2(mk_unroll(r1, t3), mgr); - - expr_ref op0(ctx.mk_eq_atom(t1, mk_int(0)), mgr); - expr_ref op1(m_autil.mk_ge(t1, mk_int(1)), mgr); - - expr_ref_vector op1Items(mgr); - expr_ref_vector op2Items(mgr); - - op1Items.push_back(ctx.mk_eq_atom(arg1, emptyStr)); - op1Items.push_back(ctx.mk_eq_atom(arg2, emptyStr)); - op1Items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(0))); - op1Items.push_back(ctx.mk_eq_atom(mk_strlen(arg2), mk_int(0))); - expr_ref opAnd1(ctx.mk_eq_atom(op0, mk_and(op1Items)), mgr); - - expr_ref v1v2(mk_concat(v1, v2), mgr); - op2Items.push_back(ctx.mk_eq_atom(arg1, v1v2)); - op2Items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), m_autil.mk_add(mk_strlen(v1), mk_strlen(v2)))); - expr_ref v3v4(mk_concat(v3, v4), mgr); - op2Items.push_back(ctx.mk_eq_atom(arg2, v3v4)); - op2Items.push_back(ctx.mk_eq_atom(mk_strlen(arg2), m_autil.mk_add(mk_strlen(v3), mk_strlen(v4)))); - - op2Items.push_back(ctx.mk_eq_atom(v1, unroll1)); - op2Items.push_back(ctx.mk_eq_atom(mk_strlen(v1), mk_strlen(unroll1))); - op2Items.push_back(ctx.mk_eq_atom(v4, unroll2)); - op2Items.push_back(ctx.mk_eq_atom(mk_strlen(v4), mk_strlen(unroll2))); - expr_ref v2v3(mk_concat(v2, v3), mgr); - op2Items.push_back(ctx.mk_eq_atom(v5, v2v3)); - reduce_virtual_regex_in(v5, r1, op2Items); - op2Items.push_back(ctx.mk_eq_atom(mk_strlen(v5), m_autil.mk_add(mk_strlen(v2), mk_strlen(v3)))); - op2Items.push_back(ctx.mk_eq_atom(m_autil.mk_add(t2, t3), m_autil.mk_add(t1, mk_int(-1)))); - expr_ref opAnd2(ctx.mk_eq_atom(op1, mk_and(op2Items)), mgr); - - toAssert = mgr.mk_and(opAnd1, opAnd2); - m_trail.push_back(toAssert); - concat_eq_unroll_ast_map[key] = toAssert; - } else { - toAssert = concat_eq_unroll_ast_map[key]; - } - - assert_axiom(toAssert); -} - -void theory_str::unroll_str2reg_constStr(expr * unrollFunc, expr * eqConstStr) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - expr * str2RegFunc = to_app(unrollFunc)->get_arg(0); - expr * strInStr2RegFunc = to_app(str2RegFunc)->get_arg(0); - expr * oriCnt = to_app(unrollFunc)->get_arg(1); - - zstring strValue; - u.str.is_string(eqConstStr, strValue); - zstring regStrValue; - u.str.is_string(strInStr2RegFunc, regStrValue); - unsigned int strLen = strValue.length(); - unsigned int regStrLen = regStrValue.length(); - SASSERT(regStrLen != 0); // this should never occur -- the case for empty string is handled elsewhere - unsigned int cnt = strLen / regStrLen; - - expr_ref implyL(ctx.mk_eq_atom(unrollFunc, eqConstStr), m); - expr_ref implyR1(ctx.mk_eq_atom(oriCnt, mk_int(cnt)), m); - expr_ref implyR2(ctx.mk_eq_atom(mk_strlen(unrollFunc), mk_int(strLen)), m); - expr_ref axiomRHS(m.mk_and(implyR1, implyR2), m); - SASSERT(implyL); - SASSERT(axiomRHS); - assert_implication(implyL, axiomRHS); -} - -/* - * Look through the equivalence class of n to find a string constant. - * Return that constant if it is found, and set hasEqcValue to true. - * Otherwise, return n, and set hasEqcValue to false. - */ - -expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { - return z3str2_get_eqc_value(n, hasEqcValue); -} - - -// Simulate the behaviour of get_eqc_value() from Z3str2. -// We only check m_find for a string constant. - -expr * theory_str::z3str2_get_eqc_value(expr * n , bool & hasEqcValue) { - expr * curr = n; - do { - if (u.str.is_string(curr)) { - hasEqcValue = true; - return curr; + if (splitType == -1 && y_len_exists && str_len_exists) { + if (y_len > str_len) + splitType = 0; + else if (y_len == str_len) + splitType = 1; + else + splitType = 2; } - curr = get_eqc_next(curr); - } while (curr != n); - hasEqcValue = false; - return n; -} -// from Z3: theory_seq.cpp + TRACE("str", tout << "Split type " << splitType << std::endl;); -static theory_mi_arith* get_th_arith(context& ctx, theory_id afid, expr* e) { - theory* th = ctx.get_theory(afid); - if (th && ctx.e_internalized(e)) { - return dynamic_cast(th); - } - else { - return 0; - } -} + // Provide fewer split options when length information is available. -bool theory_str::get_value(expr* e, rational& val) const { - if (opt_DisableIntegerTheoryIntegration) { - TRACE("str", tout << "WARNING: integer theory integration disabled" << std::endl;); - return false; - } + if (splitType == 0) { + // M cuts Y + // | x | y | + // | m | str | + expr_ref temp1_strAst(mk_concat(temp1, strAst), mgr); + if (can_two_nodes_eq(y, temp1_strAst)) { + expr_ref_vector l_items(mgr); + l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); - context& ctx = get_context(); - ast_manager & m = get_manager(); - theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), e); - if (!tha) { - return false; - } - TRACE("str", tout << "checking eqc of " << mk_pp(e, m) << " for arithmetic value" << std::endl;); - expr_ref _val(m); - enode * en_e = ctx.get_enode(e); - enode * it = en_e; - do { - if (m_autil.is_numeral(it->get_owner(), val) && val.is_int()) { - // found an arithmetic term - TRACE("str", tout << mk_pp(it->get_owner(), m) << " is an integer ( ~= " << val << " )" - << std::endl;); - return true; - } else { - TRACE("str", tout << mk_pp(it->get_owner(), m) << " not a numeral" << std::endl;); - } - it = it->get_next(); - } while (it != en_e); - TRACE("str", tout << "no arithmetic values found in eqc" << std::endl;); - return false; -} + expr_ref_vector r_items(mgr); + expr_ref x_temp1(mk_concat(x, temp1), mgr); + r_items.push_back(ctx.mk_eq_atom(m, x_temp1)); + r_items.push_back(ctx.mk_eq_atom(y, temp1_strAst)); -bool theory_str::lower_bound(expr* _e, rational& lo) { - if (opt_DisableIntegerTheoryIntegration) { - TRACE("str", tout << "WARNING: integer theory integration disabled" << std::endl;); - return false; - } - - context& ctx = get_context(); - ast_manager & m = get_manager(); - theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), _e); - expr_ref _lo(m); - if (!tha || !tha->get_lower(ctx.get_enode(_e), _lo)) return false; - return m_autil.is_numeral(_lo, lo) && lo.is_int(); -} - -bool theory_str::upper_bound(expr* _e, rational& hi) { - if (opt_DisableIntegerTheoryIntegration) { - TRACE("str", tout << "WARNING: integer theory integration disabled" << std::endl;); - return false; - } - - context& ctx = get_context(); - ast_manager & m = get_manager(); - theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), _e); - expr_ref _hi(m); - if (!tha || !tha->get_upper(ctx.get_enode(_e), _hi)) return false; - return m_autil.is_numeral(_hi, hi) && hi.is_int(); -} - -bool theory_str::get_len_value(expr* e, rational& val) { - if (opt_DisableIntegerTheoryIntegration) { - TRACE("str", tout << "WARNING: integer theory integration disabled" << std::endl;); - return false; - } - - context& ctx = get_context(); - ast_manager & m = get_manager(); - - theory* th = ctx.get_theory(m_autil.get_family_id()); - if (!th) { - TRACE("str", tout << "oops, can't get m_autil's theory" << std::endl;); - return false; - } - theory_mi_arith* tha = dynamic_cast(th); - if (!tha) { - TRACE("str", tout << "oops, can't cast to theory_mi_arith" << std::endl;); - return false; - } - - TRACE("str", tout << "checking len value of " << mk_ismt2_pp(e, m) << std::endl;); - - rational val1; - expr_ref len(m), len_val(m); - expr* e1, *e2; - ptr_vector todo; - todo.push_back(e); - val.reset(); - while (!todo.empty()) { - expr* c = todo.back(); - todo.pop_back(); - if (u.str.is_concat(to_app(c))) { - e1 = to_app(c)->get_arg(0); - e2 = to_app(c)->get_arg(1); - todo.push_back(e1); - todo.push_back(e2); - } - else if (u.str.is_string(to_app(c))) { - zstring tmp; - u.str.is_string(to_app(c), tmp); - unsigned int sl = tmp.length(); - val += rational(sl); - } - else { - len = mk_strlen(c); - - // debugging - TRACE("str", { - tout << mk_pp(len, m) << ":" << std::endl - << (ctx.is_relevant(len.get()) ? "relevant" : "not relevant") << std::endl - << (ctx.e_internalized(len) ? "internalized" : "not internalized") << std::endl - ; - if (ctx.e_internalized(len)) { - enode * e_len = ctx.get_enode(len); - tout << "has " << e_len->get_num_th_vars() << " theory vars" << std::endl; - - // eqc debugging - { - tout << "dump equivalence class of " << mk_pp(len, get_manager()) << std::endl; - enode * nNode = ctx.get_enode(len); - enode * eqcNode = nNode; - do { - app * ast = eqcNode->get_owner(); - tout << mk_pp(ast, get_manager()) << std::endl; - eqcNode = eqcNode->get_next(); - } while (eqcNode != nNode); - } - } - }); - - if (ctx.e_internalized(len) && get_value(len, val1)) { - val += val1; - TRACE("str", tout << "integer theory: subexpression " << mk_ismt2_pp(len, m) << " has length " << val1 << std::endl;); - } - else { - TRACE("str", tout << "integer theory: subexpression " << mk_ismt2_pp(len, m) << " has no length assignment; bailing out" << std::endl;); - return false; - } - } - } - - TRACE("str", tout << "length of " << mk_ismt2_pp(e, m) << " is " << val << std::endl;); - return val.is_int(); -} - -/* - * Decide whether n1 and n2 are already in the same equivalence class. - * This only checks whether the core considers them to be equal; - * they may not actually be equal. - */ -bool theory_str::in_same_eqc(expr * n1, expr * n2) { - if (n1 == n2) return true; - context & ctx = get_context(); - ast_manager & m = get_manager(); - - // similar to get_eqc_value(), make absolutely sure - // that we've set this up properly for the context - - if (!ctx.e_internalized(n1)) { - TRACE("str", tout << "WARNING: expression " << mk_ismt2_pp(n1, m) << " was not internalized" << std::endl;); - ctx.internalize(n1, false); - } - if (!ctx.e_internalized(n2)) { - TRACE("str", tout << "WARNING: expression " << mk_ismt2_pp(n2, m) << " was not internalized" << std::endl;); - ctx.internalize(n2, false); - } - - expr * curr = get_eqc_next(n1); - while (curr != n1) { - if (curr == n2) - return true; - curr = get_eqc_next(curr); - } - return false; -} - -expr * theory_str::collect_eq_nodes(expr * n, expr_ref_vector & eqcSet) { - context & ctx = get_context(); - expr * constStrNode = NULL; - - expr * ex = n; - do { - if (u.str.is_string(to_app(ex))) { - constStrNode = ex; - } - eqcSet.push_back(ex); - - ex = get_eqc_next(ex); - } while (ex != n); - return constStrNode; -} - -/* - * Collect constant strings (from left to right) in an AST node. - */ -void theory_str::get_const_str_asts_in_node(expr * node, expr_ref_vector & astList) { - ast_manager & m = get_manager(); - if (u.str.is_string(node)) { - astList.push_back(node); - //} else if (getNodeType(t, node) == my_Z3_Func) { - } else if (is_app(node)) { - app * func_app = to_app(node); - unsigned int argCount = func_app->get_num_args(); - for (unsigned int i = 0; i < argCount; i++) { - expr * argAst = func_app->get_arg(i); - get_const_str_asts_in_node(argAst, astList); - } - } -} - -void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - TRACE("str", tout << "varNode = " << mk_pp(varNode, m) << ", constNode = " << mk_pp(constNode, m) << std::endl;); - - expr_ref_vector litems(m); - - if (contain_pair_idx_map.find(varNode) != contain_pair_idx_map.end()) { - std::set >::iterator itor1 = contain_pair_idx_map[varNode].begin(); - for (; itor1 != contain_pair_idx_map[varNode].end(); ++itor1) { - expr * strAst = itor1->first; - expr * substrAst = itor1->second; - - expr * boolVar; - if (!contain_pair_bool_map.find(strAst, substrAst, boolVar)) { - TRACE("str", tout << "warning: no entry for boolVar in contain_pair_bool_map" << std::endl;); - } - // boolVar is actually a Contains term - app * containsApp = to_app(boolVar); - - // we only want to inspect the Contains terms where either of strAst or substrAst - // are equal to varNode. - - TRACE("t_str_detail", tout << "considering Contains with strAst = " << mk_pp(strAst, m) << ", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); - - if (varNode != strAst && varNode != substrAst) { - TRACE("str", tout << "varNode not equal to strAst or substrAst, skip" << std::endl;); - continue; - } - TRACE("str", tout << "varNode matched one of strAst or substrAst. Continuing" << std::endl;); - - // varEqcNode is str - if (strAst == varNode) { - expr_ref implyR(m); - litems.reset(); - - if (strAst != constNode) { - litems.push_back(ctx.mk_eq_atom(strAst, constNode)); - } - zstring strConst; - u.str.is_string(constNode, strConst); - bool subStrHasEqcValue = false; - expr * substrValue = get_eqc_value(substrAst, subStrHasEqcValue); - if (substrValue != substrAst) { - litems.push_back(ctx.mk_eq_atom(substrAst, substrValue)); + if (x_len_exists && m_len_exists) { + l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); + l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); + rational m_sub_x = (m_len - x_len); + r_items.push_back(ctx.mk_eq_atom(mk_strlen(temp1), mk_int(m_sub_x))); + } else { + l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); + l_items.push_back(ctx.mk_eq_atom(mk_strlen(strAst), mk_int(str_len))); + rational y_sub_str = (y_len - str_len); + r_items.push_back(ctx.mk_eq_atom(mk_strlen(temp1), mk_int(y_sub_str))); } - if (subStrHasEqcValue) { - // subStr has an eqc constant value - zstring subStrConst; - u.str.is_string(substrValue, subStrConst); + expr_ref ax_l(mk_and(l_items), mgr); + expr_ref ax_r(mk_and(r_items), mgr); - TRACE("t_str_detail", tout << "strConst = " << strConst << ", subStrConst = " << subStrConst << "\n";); + if (!avoidLoopCut || !(has_self_cut(m, y))) { + // break down option 2-1 + add_cut_info_merge(temp1, sLevel, y); + add_cut_info_merge(temp1, sLevel, m); - if (strConst.contains(subStrConst)) { - //implyR = ctx.mk_eq(ctx, boolVar, Z3_mk_true(ctx)); - implyR = boolVar; + if (m_params.m_StrongArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); + assert_axiom(ax_strong); } else { - //implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_false(ctx)); - implyR = m.mk_not(boolVar); + assert_implication(ax_l, ax_r); } } else { - // ------------------------------------------------------------------------------------------------ - // subStr doesn't have an eqc contant value - // however, subStr equals to some concat(arg_1, arg_2, ..., arg_n) - // if arg_j is a constant and is not a part of the strConst, it's sure that the contains is false - // ** This check is needed here because the "strConst" and "strAst" may not be in a same eqc yet - // ------------------------------------------------------------------------------------------------ - // collect eqc concat - std::set eqcConcats; - get_concats_in_eqc(substrAst, eqcConcats); - for (std::set::iterator concatItor = eqcConcats.begin(); - concatItor != eqcConcats.end(); concatItor++) { - expr_ref_vector constList(m); - bool counterEgFound = false; - // get constant strings in concat - expr * aConcat = *concatItor; - get_const_str_asts_in_node(aConcat, constList); - for (expr_ref_vector::iterator cstItor = constList.begin(); - cstItor != constList.end(); cstItor++) { - zstring pieceStr; - u.str.is_string(*cstItor, pieceStr); - if (!strConst.contains(pieceStr)) { - counterEgFound = true; - if (aConcat != substrAst) { - litems.push_back(ctx.mk_eq_atom(substrAst, aConcat)); - } - //implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_false(ctx)); - implyR = m.mk_not(boolVar); - break; + loopDetected = true; + + if (m_params.m_FiniteOverlapModels) { + expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); + assert_implication(ax_l, tester); + add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); + } else { + TRACE("str", tout << "AVOID LOOP: SKIP" << std::endl;); + TRACE("str", {print_cut_var(m, tout); print_cut_var(y, tout);}); + + if (!overlapAssumptionUsed) { + overlapAssumptionUsed = true; + assert_implication(ax_l, m_theoryStrOverlapAssumption_term); + } + } + } + } + } else if (splitType == 1) { + // | x | y | + // | m | str | + expr_ref ax_l1(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); + expr_ref ax_l2(mgr.mk_or( + ctx.mk_eq_atom(mk_strlen(x), mk_strlen(m)), + ctx.mk_eq_atom(mk_strlen(y), mk_strlen(strAst))), mgr); + expr_ref ax_l(mgr.mk_and(ax_l1, ax_l2), mgr); + expr_ref ax_r(mgr.mk_and(ctx.mk_eq_atom(x, m), ctx.mk_eq_atom(y, strAst)), mgr); + assert_implication(ax_l, ax_r); + } else if (splitType == 2) { + // m cut y, + // | x | y | + // | m | str | + rational lenDelta; + expr_ref_vector l_items(mgr); + l_items.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); + if (x_len_exists && m_len_exists) { + l_items.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); + l_items.push_back(ctx.mk_eq_atom(mk_strlen(m), mk_int(m_len))); + lenDelta = x_len - m_len; + } else { + l_items.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); + lenDelta = str_len - y_len; + } + TRACE("str", + tout + << "xLen? " << (x_len_exists ? "yes" : "no") << std::endl + << "mLen? " << (m_len_exists ? "yes" : "no") << std::endl + << "yLen? " << (y_len_exists ? "yes" : "no") << std::endl + << "xLen = " << x_len.to_string() << std::endl + << "yLen = " << y_len.to_string() << std::endl + << "mLen = " << m_len.to_string() << std::endl + << "strLen = " << str_len.to_string() << std::endl + << "lenDelta = " << lenDelta.to_string() << std::endl + << "strValue = \"" << strValue << "\" (len=" << strValue.length() << ")" << "\n" + ; + ); + + zstring part1Str = strValue.extract(0, lenDelta.get_unsigned()); + zstring part2Str = strValue.extract(lenDelta.get_unsigned(), strValue.length() - lenDelta.get_unsigned()); + + expr_ref prefixStr(mk_string(part1Str), mgr); + expr_ref x_concat(mk_concat(m, prefixStr), mgr); + expr_ref cropStr(mk_string(part2Str), mgr); + + if (can_two_nodes_eq(x, x_concat) && can_two_nodes_eq(y, cropStr)) { + expr_ref_vector r_items(mgr); + r_items.push_back(ctx.mk_eq_atom(x, x_concat)); + r_items.push_back(ctx.mk_eq_atom(y, cropStr)); + expr_ref ax_l(mk_and(l_items), mgr); + expr_ref ax_r(mk_and(r_items), mgr); + + if (m_params.m_StrongArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ax_l, ax_r); + } + } else { + // negate! It's impossible to split str with these lengths + TRACE("str", tout << "CONFLICT: Impossible to split str with these lengths." << std::endl;); + expr_ref ax_l(mk_and(l_items), mgr); + assert_axiom(mgr.mk_not(ax_l)); + } + } else { + // Split type -1: no idea about the length... + expr_ref_vector arrangement_disjunction(mgr); + + expr_ref temp1_strAst(mk_concat(temp1, strAst), mgr); + + // m cuts y + if (can_two_nodes_eq(y, temp1_strAst)) { + if (!avoidLoopCut || !has_self_cut(m, y)) { + // break down option 2-1 + expr_ref_vector and_item(mgr); + + expr_ref x_temp1(mk_concat(x, temp1), mgr); + and_item.push_back(ctx.mk_eq_atom(m, x_temp1)); + and_item.push_back(ctx.mk_eq_atom(y, temp1_strAst)); + + and_item.push_back(ctx.mk_eq_atom(mk_strlen(m), + m_autil.mk_add(mk_strlen(x), mk_strlen(temp1)))); + + expr_ref option1(mk_and(and_item), mgr); + arrangement_disjunction.push_back(option1); + add_theory_aware_branching_info(option1, 0.1, l_true); + add_cut_info_merge(temp1, ctx.get_scope_level(), y); + add_cut_info_merge(temp1, ctx.get_scope_level(), m); + } else { + loopDetected = true; + if (m_params.m_FiniteOverlapModels) { + expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); + arrangement_disjunction.push_back(tester); + add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); + } else { + TRACE("str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + TRACE("str", {print_cut_var(m, tout); print_cut_var(y, tout);}); + + if (!overlapAssumptionUsed) { + overlapAssumptionUsed = true; + arrangement_disjunction.push_back(m_theoryStrOverlapAssumption_term); + } + } + } + } + + for (unsigned int i = 0; i <= strValue.length(); ++i) { + zstring part1Str = strValue.extract(0, i); + zstring part2Str = strValue.extract(i, strValue.length() - i); + expr_ref prefixStr(mk_string(part1Str), mgr); + expr_ref x_concat(mk_concat(m, prefixStr), mgr); + expr_ref cropStr(mk_string(part2Str), mgr); + if (can_two_nodes_eq(x, x_concat) && can_two_nodes_eq(y, cropStr)) { + // break down option 2-2 + expr_ref_vector and_item(mgr); + and_item.push_back(ctx.mk_eq_atom(x, x_concat)); + and_item.push_back(ctx.mk_eq_atom(y, cropStr)); + and_item.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(part2Str.length()))); + expr_ref option2(mk_and(and_item), mgr); + arrangement_disjunction.push_back(option2); + double priority; + // prioritize the option where y is equal to the original string + if (i == 0) { + priority = 0.5; + } else { + priority = 0.1; + } + add_theory_aware_branching_info(option2, priority, l_true); + } + } + + if (!arrangement_disjunction.empty()) { + expr_ref implyR(mk_or(arrangement_disjunction), mgr); + + if (m_params.m_StrongArrangements) { + expr_ref implyLHS(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); + expr_ref ax_strong(ctx.mk_eq_atom(implyLHS, implyR), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } + generate_mutual_exclusion(arrangement_disjunction); + } else { + TRACE("str", tout << "STOP: Should not split two EQ concats." << std::endl;); + } + } // (splitType == -1) + } + + /************************************************************* + * Type 3: concat(x, y) = concat("str", n) + *************************************************************/ + bool theory_str::is_concat_eq_type3(expr * concatAst1, expr * concatAst2) { + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + if (u.str.is_string(v1_arg0) && (!u.str.is_string(v1_arg1)) + && (!u.str.is_string(v2_arg0)) && (!u.str.is_string(v2_arg1))) { + return true; + } else if (u.str.is_string(v2_arg0) && (!u.str.is_string(v2_arg1)) + && (!u.str.is_string(v1_arg0)) && (!u.str.is_string(v1_arg1))) { + return true; + } else { + return false; + } + } + + void theory_str::process_concat_eq_type3(expr * concatAst1, expr * concatAst2) { + ast_manager & mgr = get_manager(); + context & ctx = get_context(); + + bool overlapAssumptionUsed = false; + + TRACE("str", tout << "process_concat_eq TYPE 3" << std::endl + << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl + << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; + ); + + if (!u.str.is_concat(to_app(concatAst1))) { + TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); + return; + } + if (!u.str.is_concat(to_app(concatAst2))) { + TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); + return; + } + + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + expr * x = NULL; + expr * y = NULL; + expr * strAst = NULL; + expr * n = NULL; + + if (u.str.is_string(v1_arg0) && !u.str.is_string(v2_arg0)) { + strAst = v1_arg0; + n = v1_arg1; + x = v2_arg0; + y = v2_arg1; + } else { + strAst = v2_arg0; + n = v2_arg1; + x = v1_arg0; + y = v1_arg1; + } + + zstring strValue; + u.str.is_string(strAst, strValue); + + rational x_len, y_len, str_len, n_len; + bool x_len_exists = get_len_value(x, x_len); + bool y_len_exists = get_len_value(y, y_len); + str_len = rational((unsigned)(strValue.length())); + bool n_len_exists = get_len_value(n, n_len); + + expr_ref xorFlag(mgr); + expr_ref temp1(mgr); + std::pair key1(concatAst1, concatAst2); + std::pair key2(concatAst2, concatAst1); + + // check the entries in this map to make sure they're still in scope + // before we use them. + + std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); + std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); + + bool entry1InScope; + if (entry1 == varForBreakConcat.end()) { + entry1InScope = false; + } else { + if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end() + /* || internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end() */) { + entry1InScope = false; + } else { + entry1InScope = true; + } + } + + bool entry2InScope; + if (entry2 == varForBreakConcat.end()) { + entry2InScope = false; + } else { + if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end() + /* || internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end() */) { + entry2InScope = false; + } else { + entry2InScope = true; + } + } + + TRACE("str", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl + << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); + + + if (!entry1InScope && !entry2InScope) { + temp1 = mk_nonempty_str_var(); + xorFlag = mk_internal_xor_var(); + + varForBreakConcat[key1][0] = temp1; + varForBreakConcat[key1][1] = xorFlag; + } else { + if (entry1InScope) { + temp1 = varForBreakConcat[key1][0]; + xorFlag = varForBreakConcat[key1][1]; + } else if (varForBreakConcat.find(key2) != varForBreakConcat.end()) { + temp1 = varForBreakConcat[key2][0]; + xorFlag = varForBreakConcat[key2][1]; + } + refresh_theory_var(temp1); + add_nonempty_constraint(temp1); + } + + + + int splitType = -1; + if (x_len_exists) { + if (x_len < str_len) + splitType = 0; + else if (x_len == str_len) + splitType = 1; + else + splitType = 2; + } + if (splitType == -1 && y_len_exists && n_len_exists) { + if (y_len > n_len) + splitType = 0; + else if (y_len == n_len) + splitType = 1; + else + splitType = 2; + } + + TRACE("str", tout << "Split type " << splitType << std::endl;); + + // Provide fewer split options when length information is available. + if (splitType == 0) { + // | x | y | + // | str | n | + expr_ref_vector litems(mgr); + litems.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); + rational prefixLen; + if (!x_len_exists) { + prefixLen = str_len - (y_len - n_len); + litems.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); + litems.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len))); + } else { + prefixLen = x_len; + litems.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); + } + zstring prefixStr = strValue.extract(0, prefixLen.get_unsigned()); + rational str_sub_prefix = str_len - prefixLen; + zstring suffixStr = strValue.extract(prefixLen.get_unsigned(), str_sub_prefix.get_unsigned()); + expr_ref prefixAst(mk_string(prefixStr), mgr); + expr_ref suffixAst(mk_string(suffixStr), mgr); + expr_ref ax_l(mgr.mk_and(litems.size(), litems.c_ptr()), mgr); + + expr_ref suf_n_concat(mk_concat(suffixAst, n), mgr); + if (can_two_nodes_eq(x, prefixAst) && can_two_nodes_eq(y, suf_n_concat)) { + expr_ref_vector r_items(mgr); + r_items.push_back(ctx.mk_eq_atom(x, prefixAst)); + r_items.push_back(ctx.mk_eq_atom(y, suf_n_concat)); + + if (m_params.m_StrongArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom(ax_l, mk_and(r_items)), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ax_l, mk_and(r_items)); + } + } else { + // negate! It's impossible to split str with these lengths + TRACE("str", tout << "CONFLICT: Impossible to split str with these lengths." << std::endl;); + assert_axiom(mgr.mk_not(ax_l)); + } + } + else if (splitType == 1) { + expr_ref ax_l1(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); + expr_ref ax_l2(mgr.mk_or( + ctx.mk_eq_atom(mk_strlen(x), mk_strlen(strAst)), + ctx.mk_eq_atom(mk_strlen(y), mk_strlen(n))), mgr); + expr_ref ax_l(mgr.mk_and(ax_l1, ax_l2), mgr); + expr_ref ax_r(mgr.mk_and(ctx.mk_eq_atom(x, strAst), ctx.mk_eq_atom(y, n)), mgr); + + if (m_params.m_StrongArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ax_l, ax_r); + } + } + else if (splitType == 2) { + // | x | y | + // | str | n | + expr_ref_vector litems(mgr); + litems.push_back(ctx.mk_eq_atom(concatAst1, concatAst2)); + rational tmpLen; + if (!x_len_exists) { + tmpLen = n_len - y_len; + litems.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_int(y_len))); + litems.push_back(ctx.mk_eq_atom(mk_strlen(n), mk_int(n_len))); + } else { + tmpLen = x_len - str_len; + litems.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_int(x_len))); + } + expr_ref ax_l(mgr.mk_and(litems.size(), litems.c_ptr()), mgr); + + expr_ref str_temp1(mk_concat(strAst, temp1), mgr); + expr_ref temp1_y(mk_concat(temp1, y), mgr); + + if (can_two_nodes_eq(x, str_temp1)) { + if (!avoidLoopCut || !(has_self_cut(x, n))) { + expr_ref_vector r_items(mgr); + r_items.push_back(ctx.mk_eq_atom(x, str_temp1)); + r_items.push_back(ctx.mk_eq_atom(n, temp1_y)); + r_items.push_back(ctx.mk_eq_atom(mk_strlen(temp1), mk_int(tmpLen))); + expr_ref ax_r(mk_and(r_items), mgr); + + //Cut Info + add_cut_info_merge(temp1, sLevel, x); + add_cut_info_merge(temp1, sLevel, n); + + if (m_params.m_StrongArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom(ax_l, ax_r), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ax_l, ax_r); + } + } else { + loopDetected = true; + if (m_params.m_FiniteOverlapModels) { + expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); + assert_implication(ax_l, tester); + add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); + } else { + TRACE("str", tout << "AVOID LOOP: SKIPPED" << std::endl;); + TRACE("str", {print_cut_var(x, tout); print_cut_var(n, tout);}); + + if (!overlapAssumptionUsed) { + overlapAssumptionUsed = true; + assert_implication(ax_l, m_theoryStrOverlapAssumption_term); + } + } + } + } + // else { + // // negate! It's impossible to split str with these lengths + // __debugPrint(logFile, "[Conflict] Negate! It's impossible to split str with these lengths @ %d.\n", __LINE__); + // addAxiom(t, Z3_mk_not(ctx, ax_l), __LINE__); + // } + } + else { + // Split type -1. We know nothing about the length... + + expr_ref_vector arrangement_disjunction(mgr); + + int pos = 1; + for (unsigned int i = 0; i <= strValue.length(); i++) { + zstring part1Str = strValue.extract(0, i); + zstring part2Str = strValue.extract(i, strValue.length() - i); + expr_ref cropStr(mk_string(part1Str), mgr); + expr_ref suffixStr(mk_string(part2Str), mgr); + expr_ref y_concat(mk_concat(suffixStr, n), mgr); + + if (can_two_nodes_eq(x, cropStr) && can_two_nodes_eq(y, y_concat)) { + expr_ref_vector and_item(mgr); + // break down option 3-1 + expr_ref x_eq_str(ctx.mk_eq_atom(x, cropStr), mgr); + + and_item.push_back(x_eq_str); ++pos; + and_item.push_back(ctx.mk_eq_atom(y, y_concat)); + and_item.push_back(ctx.mk_eq_atom(mk_strlen(x), mk_strlen(cropStr))); ++pos; + + // and_item[pos++] = Z3_mk_eq(ctx, or_item[option], Z3_mk_eq(ctx, mk_length(t, y), mk_length(t, y_concat))); + // adding length constraint for _ = constStr seems slowing things down. + + expr_ref option1(mk_and(and_item), mgr); + arrangement_disjunction.push_back(option1); + double priority; + if (i == strValue.length()) { + priority = 0.5; + } else { + priority = 0.1; + } + add_theory_aware_branching_info(option1, priority, l_true); + } + } + + expr_ref strAst_temp1(mk_concat(strAst, temp1), mgr); + + + //-------------------------------------------------------- + // x cut n + //-------------------------------------------------------- + if (can_two_nodes_eq(x, strAst_temp1)) { + if (!avoidLoopCut || !(has_self_cut(x, n))) { + // break down option 3-2 + expr_ref_vector and_item(mgr); + + expr_ref temp1_y(mk_concat(temp1, y), mgr); + and_item.push_back(ctx.mk_eq_atom(x, strAst_temp1)); ++pos; + and_item.push_back(ctx.mk_eq_atom(n, temp1_y)); ++pos; + + and_item.push_back(ctx.mk_eq_atom(mk_strlen(x), + m_autil.mk_add(mk_strlen(strAst), mk_strlen(temp1)) ) ); ++pos; + + expr_ref option2(mk_and(and_item), mgr); + arrangement_disjunction.push_back(option2); + add_theory_aware_branching_info(option2, 0.1, l_true); + + add_cut_info_merge(temp1, sLevel, x); + add_cut_info_merge(temp1, sLevel, n); + } else { + loopDetected = true; + if (m_params.m_FiniteOverlapModels) { + expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); + arrangement_disjunction.push_back(tester); + add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); + } else { + TRACE("str", tout << "AVOID LOOP: SKIPPED." << std::endl;); + TRACE("str", {print_cut_var(x, tout); print_cut_var(n, tout);}); + + if (!overlapAssumptionUsed) { + overlapAssumptionUsed = true; + arrangement_disjunction.push_back(m_theoryStrOverlapAssumption_term); + } + } + } + } + + + if (!arrangement_disjunction.empty()) { + expr_ref implyR(mk_or(arrangement_disjunction), mgr); + + if (m_params.m_StrongArrangements) { + expr_ref ax_lhs(ctx.mk_eq_atom(concatAst1, concatAst2), mgr); + expr_ref ax_strong(ctx.mk_eq_atom(ax_lhs, implyR), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } + generate_mutual_exclusion(arrangement_disjunction); + } else { + TRACE("str", tout << "STOP: should not split two eq. concats" << std::endl;); + } + } + + } + + /************************************************************* + * Type 4: concat("str1", y) = concat("str2", n) + *************************************************************/ + bool theory_str::is_concat_eq_type4(expr * concatAst1, expr * concatAst2) { + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + if (u.str.is_string(v1_arg0) && (!u.str.is_string(v1_arg1)) + && u.str.is_string(v2_arg0) && (!u.str.is_string(v2_arg1))) { + return true; + } else { + return false; + } + } + + void theory_str::process_concat_eq_type4(expr * concatAst1, expr * concatAst2) { + ast_manager & mgr = get_manager(); + context & ctx = get_context(); + TRACE("str", tout << "process_concat_eq TYPE 4" << std::endl + << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl + << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; + ); + + if (!u.str.is_concat(to_app(concatAst1))) { + TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); + return; + } + if (!u.str.is_concat(to_app(concatAst2))) { + TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); + return; + } + + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + expr * str1Ast = v1_arg0; + expr * y = v1_arg1; + expr * str2Ast = v2_arg0; + expr * n = v2_arg1; + + zstring str1Value, str2Value; + u.str.is_string(str1Ast, str1Value); + u.str.is_string(str2Ast, str2Value); + + unsigned int str1Len = str1Value.length(); + unsigned int str2Len = str2Value.length(); + + int commonLen = (str1Len > str2Len) ? str2Len : str1Len; + if (str1Value.extract(0, commonLen) != str2Value.extract(0, commonLen)) { + TRACE("str", tout << "Conflict: " << mk_ismt2_pp(concatAst1, mgr) + << " has no common prefix with " << mk_ismt2_pp(concatAst2, mgr) << std::endl;); + expr_ref toNegate(mgr.mk_not(ctx.mk_eq_atom(concatAst1, concatAst2)), mgr); + assert_axiom(toNegate); + return; + } else { + if (str1Len > str2Len) { + zstring deltaStr = str1Value.extract(str2Len, str1Len - str2Len); + expr_ref tmpAst(mk_concat(mk_string(deltaStr), y), mgr); + if (!in_same_eqc(tmpAst, n)) { + // break down option 4-1 + expr_ref implyR(ctx.mk_eq_atom(n, tmpAst), mgr); + if (m_params.m_StrongArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } + } + } else if (str1Len == str2Len) { + if (!in_same_eqc(n, y)) { + //break down option 4-2 + expr_ref implyR(ctx.mk_eq_atom(n, y), mgr); + + if (m_params.m_StrongArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } + } + } else { + zstring deltaStr = str2Value.extract(str1Len, str2Len - str1Len); + expr_ref tmpAst(mk_concat(mk_string(deltaStr), n), mgr); + if (!in_same_eqc(y, tmpAst)) { + //break down option 4-3 + expr_ref implyR(ctx.mk_eq_atom(y, tmpAst), mgr); + if (m_params.m_StrongArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } + } + } + } + } + + /************************************************************* + * case 5: concat(x, "str1") = concat(m, "str2") + *************************************************************/ + bool theory_str::is_concat_eq_type5(expr * concatAst1, expr * concatAst2) { + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + if ((!u.str.is_string(v1_arg0)) && u.str.is_string(v1_arg1) + && (!u.str.is_string(v2_arg0)) && u.str.is_string(v2_arg1)) { + return true; + } else { + return false; + } + } + + void theory_str::process_concat_eq_type5(expr * concatAst1, expr * concatAst2) { + ast_manager & mgr = get_manager(); + context & ctx = get_context(); + TRACE("str", tout << "process_concat_eq TYPE 5" << std::endl + << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl + << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; + ); + + if (!u.str.is_concat(to_app(concatAst1))) { + TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); + return; + } + if (!u.str.is_concat(to_app(concatAst2))) { + TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); + return; + } + + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + expr * x = v1_arg0; + expr * str1Ast = v1_arg1; + expr * m = v2_arg0; + expr * str2Ast = v2_arg1; + + zstring str1Value, str2Value; + u.str.is_string(str1Ast, str1Value); + u.str.is_string(str2Ast, str2Value); + + unsigned int str1Len = str1Value.length(); + unsigned int str2Len = str2Value.length(); + + int cLen = (str1Len > str2Len) ? str2Len : str1Len; + if (str1Value.extract(str1Len - cLen, cLen) != str2Value.extract(str2Len - cLen, cLen)) { + TRACE("str", tout << "Conflict: " << mk_ismt2_pp(concatAst1, mgr) + << " has no common suffix with " << mk_ismt2_pp(concatAst2, mgr) << std::endl;); + expr_ref toNegate(mgr.mk_not(ctx.mk_eq_atom(concatAst1, concatAst2)), mgr); + assert_axiom(toNegate); + return; + } else { + if (str1Len > str2Len) { + zstring deltaStr = str1Value.extract(0, str1Len - str2Len); + expr_ref x_deltaStr(mk_concat(x, mk_string(deltaStr)), mgr); + if (!in_same_eqc(m, x_deltaStr)) { + expr_ref implyR(ctx.mk_eq_atom(m, x_deltaStr), mgr); + if (m_params.m_StrongArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } + } + } else if (str1Len == str2Len) { + // test + if (!in_same_eqc(x, m)) { + expr_ref implyR(ctx.mk_eq_atom(x, m), mgr); + if (m_params.m_StrongArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } + } + } else { + zstring deltaStr = str2Value.extract(0, str2Len - str1Len); + expr_ref m_deltaStr(mk_concat(m, mk_string(deltaStr)), mgr); + if (!in_same_eqc(x, m_deltaStr)) { + expr_ref implyR(ctx.mk_eq_atom(x, m_deltaStr), mgr); + if (m_params.m_StrongArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } + } + } + } + } + + /************************************************************* + * case 6: concat("str1", y) = concat(m, "str2") + *************************************************************/ + bool theory_str::is_concat_eq_type6(expr * concatAst1, expr * concatAst2) { + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + if (u.str.is_string(v1_arg0) && (!u.str.is_string(v1_arg1)) + && (!u.str.is_string(v2_arg0)) && u.str.is_string(v2_arg1)) { + return true; + } else if (u.str.is_string(v2_arg0) && (!u.str.is_string(v2_arg1)) + && (!u.str.is_string(v1_arg0)) && u.str.is_string(v1_arg1)) { + return true; + } else { + return false; + } + } + + void theory_str::process_concat_eq_type6(expr * concatAst1, expr * concatAst2) { + ast_manager & mgr = get_manager(); + context & ctx = get_context(); + TRACE("str", tout << "process_concat_eq TYPE 6" << std::endl + << "concatAst1 = " << mk_ismt2_pp(concatAst1, mgr) << std::endl + << "concatAst2 = " << mk_ismt2_pp(concatAst2, mgr) << std::endl; + ); + + if (!u.str.is_concat(to_app(concatAst1))) { + TRACE("str", tout << "concatAst1 is not a concat function" << std::endl;); + return; + } + if (!u.str.is_concat(to_app(concatAst2))) { + TRACE("str", tout << "concatAst2 is not a concat function" << std::endl;); + return; + } + + expr * v1_arg0 = to_app(concatAst1)->get_arg(0); + expr * v1_arg1 = to_app(concatAst1)->get_arg(1); + expr * v2_arg0 = to_app(concatAst2)->get_arg(0); + expr * v2_arg1 = to_app(concatAst2)->get_arg(1); + + + expr * str1Ast = NULL; + expr * y = NULL; + expr * m = NULL; + expr * str2Ast = NULL; + + if (u.str.is_string(v1_arg0)) { + str1Ast = v1_arg0; + y = v1_arg1; + m = v2_arg0; + str2Ast = v2_arg1; + } else { + str1Ast = v2_arg0; + y = v2_arg1; + m = v1_arg0; + str2Ast = v1_arg1; + } + + zstring str1Value, str2Value; + u.str.is_string(str1Ast, str1Value); + u.str.is_string(str2Ast, str2Value); + + unsigned int str1Len = str1Value.length(); + unsigned int str2Len = str2Value.length(); + + //---------------------------------------- + //(a) |---str1---|----y----| + // |--m--|-----str2-----| + // + //(b) |---str1---|----y----| + // |-----m----|--str2---| + // + //(c) |---str1---|----y----| + // |------m------|-str2-| + //---------------------------------------- + + std::list overlapLen; + overlapLen.push_back(0); + + for (unsigned int i = 1; i <= str1Len && i <= str2Len; i++) { + if (str1Value.extract(str1Len - i, i) == str2Value.extract(0, i)) + overlapLen.push_back(i); + } + + //---------------------------------------------------------------- + expr * commonVar = NULL; + expr * xorFlag = NULL; + std::pair key1(concatAst1, concatAst2); + std::pair key2(concatAst2, concatAst1); + + // check the entries in this map to make sure they're still in scope + // before we use them. + + std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); + std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); + + bool entry1InScope; + if (entry1 == varForBreakConcat.end()) { + entry1InScope = false; + } else { + if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end() + /* || internal_variable_set.find((entry1->second)[1]) == internal_variable_set.end() */) { + entry1InScope = false; + } else { + entry1InScope = true; + } + } + + bool entry2InScope; + if (entry2 == varForBreakConcat.end()) { + entry2InScope = false; + } else { + if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end() + /* || internal_variable_set.find((entry2->second)[1]) == internal_variable_set.end() */) { + entry2InScope = false; + } else { + entry2InScope = true; + } + } + + TRACE("str", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl + << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); + + if (!entry1InScope && !entry2InScope) { + commonVar = mk_nonempty_str_var(); + xorFlag = mk_internal_xor_var(); + varForBreakConcat[key1][0] = commonVar; + varForBreakConcat[key1][1] = xorFlag; + } else { + if (entry1InScope) { + commonVar = (entry1->second)[0]; + xorFlag = (entry1->second)[1]; + } else { + commonVar = (entry2->second)[0]; + xorFlag = (entry2->second)[1]; + } + refresh_theory_var(commonVar); + add_nonempty_constraint(commonVar); + } + + bool overlapAssumptionUsed = false; + + expr_ref_vector arrangement_disjunction(mgr); + int pos = 1; + + if (!avoidLoopCut || !has_self_cut(m, y)) { + expr_ref_vector and_item(mgr); + + expr_ref str1_commonVar(mk_concat(str1Ast, commonVar), mgr); + and_item.push_back(ctx.mk_eq_atom(m, str1_commonVar)); + pos += 1; + + expr_ref commonVar_str2(mk_concat(commonVar, str2Ast), mgr); + and_item.push_back(ctx.mk_eq_atom(y, commonVar_str2)); + pos += 1; + + and_item.push_back(ctx.mk_eq_atom(mk_strlen(m), + m_autil.mk_add(mk_strlen(str1Ast), mk_strlen(commonVar)) )); + pos += 1; + + // addItems[0] = mk_length(t, commonVar); + // addItems[1] = mk_length(t, str2Ast); + // and_item[pos++] = Z3_mk_eq(ctx, or_item[option], Z3_mk_eq(ctx, mk_length(t, y), Z3_mk_add(ctx, 2, addItems))); + + expr_ref option1(mk_and(and_item), mgr); + arrangement_disjunction.push_back(option1); + add_theory_aware_branching_info(option1, 0.1, l_true); + } else { + loopDetected = true; + + if (m_params.m_FiniteOverlapModels) { + expr_ref tester = set_up_finite_model_test(concatAst1, concatAst2); + arrangement_disjunction.push_back(tester); + add_theory_aware_branching_info(tester, m_params.m_OverlapTheoryAwarePriority, l_true); + } else { + TRACE("str", tout << "AVOID LOOP: SKIPPED." << std::endl;); + TRACE("str", print_cut_var(m, tout); print_cut_var(y, tout);); + + // only add the overlap assumption one time + if (!overlapAssumptionUsed) { + arrangement_disjunction.push_back(m_theoryStrOverlapAssumption_term); + overlapAssumptionUsed = true; + } + } + } + + for (std::list::iterator itor = overlapLen.begin(); itor != overlapLen.end(); itor++) { + unsigned int overLen = *itor; + zstring prefix = str1Value.extract(0, str1Len - overLen); + zstring suffix = str2Value.extract(overLen, str2Len - overLen); + + expr_ref_vector and_item(mgr); + + expr_ref prefixAst(mk_string(prefix), mgr); + expr_ref x_eq_prefix(ctx.mk_eq_atom(m, prefixAst), mgr); + and_item.push_back(x_eq_prefix); + pos += 1; + + and_item.push_back( + ctx.mk_eq_atom(mk_strlen(m), mk_strlen(prefixAst))); + pos += 1; + + // adding length constraint for _ = constStr seems slowing things down. + + expr_ref suffixAst(mk_string(suffix), mgr); + expr_ref y_eq_suffix(ctx.mk_eq_atom(y, suffixAst), mgr); + and_item.push_back(y_eq_suffix); + pos += 1; + + and_item.push_back(ctx.mk_eq_atom(mk_strlen(y), mk_strlen(suffixAst))); + pos += 1; + + expr_ref option2(mk_and(and_item), mgr); + arrangement_disjunction.push_back(option2); + double priority; + // prefer the option "str1" = x + if (prefix == str1Value) { + priority = 0.5; + } else { + priority = 0.1; + } + add_theory_aware_branching_info(option2, priority, l_true); + } + + // case 6: concat("str1", y) = concat(m, "str2") + + expr_ref implyR(mk_or(arrangement_disjunction), mgr); + + if (m_params.m_StrongArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom( ctx.mk_eq_atom(concatAst1, concatAst2), implyR ), mgr); + assert_axiom(ax_strong); + } else { + assert_implication(ctx.mk_eq_atom(concatAst1, concatAst2), implyR); + } + generate_mutual_exclusion(arrangement_disjunction); + } + + void theory_str::process_unroll_eq_const_str(expr * unrollFunc, expr * constStr) { + ast_manager & m = get_manager(); + + if (!u.re.is_unroll(to_app(unrollFunc))) { + return; + } + if (!u.str.is_string(constStr)) { + return; + } + + expr * funcInUnroll = to_app(unrollFunc)->get_arg(0); + zstring strValue; + u.str.is_string(constStr, strValue); + + TRACE("str", tout << "unrollFunc: " << mk_pp(unrollFunc, m) << std::endl + << "constStr: " << mk_pp(constStr, m) << std::endl;); + + if (strValue == "") { + return; + } + + if (u.re.is_to_re(to_app(funcInUnroll))) { + unroll_str2reg_constStr(unrollFunc, constStr); + return; + } + } + + void theory_str::process_concat_eq_unroll(expr * concat, expr * unroll) { + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + + TRACE("str", tout << "concat = " << mk_pp(concat, mgr) << ", unroll = " << mk_pp(unroll, mgr) << std::endl;); + + std::pair key = std::make_pair(concat, unroll); + expr_ref toAssert(mgr); + + if (concat_eq_unroll_ast_map.find(key) == concat_eq_unroll_ast_map.end()) { + expr_ref arg1(to_app(concat)->get_arg(0), mgr); + expr_ref arg2(to_app(concat)->get_arg(1), mgr); + expr_ref r1(to_app(unroll)->get_arg(0), mgr); + expr_ref t1(to_app(unroll)->get_arg(1), mgr); + + expr_ref v1(mk_regex_rep_var(), mgr); + expr_ref v2(mk_regex_rep_var(), mgr); + expr_ref v3(mk_regex_rep_var(), mgr); + expr_ref v4(mk_regex_rep_var(), mgr); + expr_ref v5(mk_regex_rep_var(), mgr); + + expr_ref t2(mk_unroll_bound_var(), mgr); + expr_ref t3(mk_unroll_bound_var(), mgr); + expr_ref emptyStr(mk_string(""), mgr); + + expr_ref unroll1(mk_unroll(r1, t2), mgr); + expr_ref unroll2(mk_unroll(r1, t3), mgr); + + expr_ref op0(ctx.mk_eq_atom(t1, mk_int(0)), mgr); + expr_ref op1(m_autil.mk_ge(t1, mk_int(1)), mgr); + + expr_ref_vector op1Items(mgr); + expr_ref_vector op2Items(mgr); + + op1Items.push_back(ctx.mk_eq_atom(arg1, emptyStr)); + op1Items.push_back(ctx.mk_eq_atom(arg2, emptyStr)); + op1Items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(0))); + op1Items.push_back(ctx.mk_eq_atom(mk_strlen(arg2), mk_int(0))); + expr_ref opAnd1(ctx.mk_eq_atom(op0, mk_and(op1Items)), mgr); + + expr_ref v1v2(mk_concat(v1, v2), mgr); + op2Items.push_back(ctx.mk_eq_atom(arg1, v1v2)); + op2Items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), m_autil.mk_add(mk_strlen(v1), mk_strlen(v2)))); + expr_ref v3v4(mk_concat(v3, v4), mgr); + op2Items.push_back(ctx.mk_eq_atom(arg2, v3v4)); + op2Items.push_back(ctx.mk_eq_atom(mk_strlen(arg2), m_autil.mk_add(mk_strlen(v3), mk_strlen(v4)))); + + op2Items.push_back(ctx.mk_eq_atom(v1, unroll1)); + op2Items.push_back(ctx.mk_eq_atom(mk_strlen(v1), mk_strlen(unroll1))); + op2Items.push_back(ctx.mk_eq_atom(v4, unroll2)); + op2Items.push_back(ctx.mk_eq_atom(mk_strlen(v4), mk_strlen(unroll2))); + expr_ref v2v3(mk_concat(v2, v3), mgr); + op2Items.push_back(ctx.mk_eq_atom(v5, v2v3)); + reduce_virtual_regex_in(v5, r1, op2Items); + op2Items.push_back(ctx.mk_eq_atom(mk_strlen(v5), m_autil.mk_add(mk_strlen(v2), mk_strlen(v3)))); + op2Items.push_back(ctx.mk_eq_atom(m_autil.mk_add(t2, t3), m_autil.mk_add(t1, mk_int(-1)))); + expr_ref opAnd2(ctx.mk_eq_atom(op1, mk_and(op2Items)), mgr); + + toAssert = mgr.mk_and(opAnd1, opAnd2); + m_trail.push_back(toAssert); + concat_eq_unroll_ast_map[key] = toAssert; + } else { + toAssert = concat_eq_unroll_ast_map[key]; + } + + assert_axiom(toAssert); + } + + void theory_str::unroll_str2reg_constStr(expr * unrollFunc, expr * eqConstStr) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + expr * str2RegFunc = to_app(unrollFunc)->get_arg(0); + expr * strInStr2RegFunc = to_app(str2RegFunc)->get_arg(0); + expr * oriCnt = to_app(unrollFunc)->get_arg(1); + + zstring strValue; + u.str.is_string(eqConstStr, strValue); + zstring regStrValue; + u.str.is_string(strInStr2RegFunc, regStrValue); + unsigned int strLen = strValue.length(); + unsigned int regStrLen = regStrValue.length(); + SASSERT(regStrLen != 0); // this should never occur -- the case for empty string is handled elsewhere + unsigned int cnt = strLen / regStrLen; + + expr_ref implyL(ctx.mk_eq_atom(unrollFunc, eqConstStr), m); + expr_ref implyR1(ctx.mk_eq_atom(oriCnt, mk_int(cnt)), m); + expr_ref implyR2(ctx.mk_eq_atom(mk_strlen(unrollFunc), mk_int(strLen)), m); + expr_ref axiomRHS(m.mk_and(implyR1, implyR2), m); + SASSERT(implyL); + SASSERT(axiomRHS); + assert_implication(implyL, axiomRHS); + } + + /* + * Look through the equivalence class of n to find a string constant. + * Return that constant if it is found, and set hasEqcValue to true. + * Otherwise, return n, and set hasEqcValue to false. + */ + + expr * theory_str::get_eqc_value(expr * n, bool & hasEqcValue) { + return z3str2_get_eqc_value(n, hasEqcValue); + } + + + // Simulate the behaviour of get_eqc_value() from Z3str2. + // We only check m_find for a string constant. + + expr * theory_str::z3str2_get_eqc_value(expr * n , bool & hasEqcValue) { + expr * curr = n; + do { + if (u.str.is_string(curr)) { + hasEqcValue = true; + return curr; + } + curr = get_eqc_next(curr); + } while (curr != n); + hasEqcValue = false; + return n; + } + + // from Z3: theory_seq.cpp + + static theory_mi_arith* get_th_arith(context& ctx, theory_id afid, expr* e) { + theory* th = ctx.get_theory(afid); + if (th && ctx.e_internalized(e)) { + return dynamic_cast(th); + } + else { + return 0; + } + } + + bool theory_str::get_value(expr* e, rational& val) const { + if (opt_DisableIntegerTheoryIntegration) { + TRACE("str", tout << "WARNING: integer theory integration disabled" << std::endl;); + return false; + } + + context& ctx = get_context(); + ast_manager & m = get_manager(); + theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), e); + if (!tha) { + return false; + } + TRACE("str", tout << "checking eqc of " << mk_pp(e, m) << " for arithmetic value" << std::endl;); + expr_ref _val(m); + enode * en_e = ctx.get_enode(e); + enode * it = en_e; + do { + if (m_autil.is_numeral(it->get_owner(), val) && val.is_int()) { + // found an arithmetic term + TRACE("str", tout << mk_pp(it->get_owner(), m) << " is an integer ( ~= " << val << " )" + << std::endl;); + return true; + } else { + TRACE("str", tout << mk_pp(it->get_owner(), m) << " not a numeral" << std::endl;); + } + it = it->get_next(); + } while (it != en_e); + TRACE("str", tout << "no arithmetic values found in eqc" << std::endl;); + return false; + } + + bool theory_str::lower_bound(expr* _e, rational& lo) { + if (opt_DisableIntegerTheoryIntegration) { + TRACE("str", tout << "WARNING: integer theory integration disabled" << std::endl;); + return false; + } + + context& ctx = get_context(); + ast_manager & m = get_manager(); + theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), _e); + expr_ref _lo(m); + if (!tha || !tha->get_lower(ctx.get_enode(_e), _lo)) return false; + return m_autil.is_numeral(_lo, lo) && lo.is_int(); + } + + bool theory_str::upper_bound(expr* _e, rational& hi) { + if (opt_DisableIntegerTheoryIntegration) { + TRACE("str", tout << "WARNING: integer theory integration disabled" << std::endl;); + return false; + } + + context& ctx = get_context(); + ast_manager & m = get_manager(); + theory_mi_arith* tha = get_th_arith(ctx, m_autil.get_family_id(), _e); + expr_ref _hi(m); + if (!tha || !tha->get_upper(ctx.get_enode(_e), _hi)) return false; + return m_autil.is_numeral(_hi, hi) && hi.is_int(); + } + + bool theory_str::get_len_value(expr* e, rational& val) { + if (opt_DisableIntegerTheoryIntegration) { + TRACE("str", tout << "WARNING: integer theory integration disabled" << std::endl;); + return false; + } + + context& ctx = get_context(); + ast_manager & m = get_manager(); + + theory* th = ctx.get_theory(m_autil.get_family_id()); + if (!th) { + TRACE("str", tout << "oops, can't get m_autil's theory" << std::endl;); + return false; + } + theory_mi_arith* tha = dynamic_cast(th); + if (!tha) { + TRACE("str", tout << "oops, can't cast to theory_mi_arith" << std::endl;); + return false; + } + + TRACE("str", tout << "checking len value of " << mk_ismt2_pp(e, m) << std::endl;); + + rational val1; + expr_ref len(m), len_val(m); + expr* e1, *e2; + ptr_vector todo; + todo.push_back(e); + val.reset(); + while (!todo.empty()) { + expr* c = todo.back(); + todo.pop_back(); + if (u.str.is_concat(to_app(c))) { + e1 = to_app(c)->get_arg(0); + e2 = to_app(c)->get_arg(1); + todo.push_back(e1); + todo.push_back(e2); + } + else if (u.str.is_string(to_app(c))) { + zstring tmp; + u.str.is_string(to_app(c), tmp); + unsigned int sl = tmp.length(); + val += rational(sl); + } + else { + len = mk_strlen(c); + + // debugging + TRACE("str", { + tout << mk_pp(len, m) << ":" << std::endl + << (ctx.is_relevant(len.get()) ? "relevant" : "not relevant") << std::endl + << (ctx.e_internalized(len) ? "internalized" : "not internalized") << std::endl + ; + if (ctx.e_internalized(len)) { + enode * e_len = ctx.get_enode(len); + tout << "has " << e_len->get_num_th_vars() << " theory vars" << std::endl; + + // eqc debugging + { + tout << "dump equivalence class of " << mk_pp(len, get_manager()) << std::endl; + enode * nNode = ctx.get_enode(len); + enode * eqcNode = nNode; + do { + app * ast = eqcNode->get_owner(); + tout << mk_pp(ast, get_manager()) << std::endl; + eqcNode = eqcNode->get_next(); + } while (eqcNode != nNode); } } - if (counterEgFound) { - TRACE("str", tout << "Inconsistency found!" << std::endl;); - break; - } - } + }); + + if (ctx.e_internalized(len) && get_value(len, val1)) { + val += val1; + TRACE("str", tout << "integer theory: subexpression " << mk_ismt2_pp(len, m) << " has length " << val1 << std::endl;); } - // add assertion - if (implyR) { - expr_ref implyLHS(mk_and(litems), m); - assert_implication(implyLHS, implyR); + else { + TRACE("str", tout << "integer theory: subexpression " << mk_ismt2_pp(len, m) << " has no length assignment; bailing out" << std::endl;); + return false; } } - // varEqcNode is subStr - else if (substrAst == varNode) { - expr_ref implyR(m); - litems.reset(); + } - if (substrAst != constNode) { - litems.push_back(ctx.mk_eq_atom(substrAst, constNode)); - } - bool strHasEqcValue = false; - expr * strValue = get_eqc_value(strAst, strHasEqcValue); - if (strValue != strAst) { - litems.push_back(ctx.mk_eq_atom(strAst, strValue)); - } + TRACE("str", tout << "length of " << mk_ismt2_pp(e, m) << " is " << val << std::endl;); + return val.is_int(); + } - if (strHasEqcValue) { - zstring strConst, subStrConst; - u.str.is_string(strValue, strConst); - u.str.is_string(constNode, subStrConst); - if (strConst.contains(subStrConst)) { - //implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_true(ctx)); - implyR = boolVar; - } else { - // implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_false(ctx)); - implyR = m.mk_not(boolVar); - } - } + /* + * Decide whether n1 and n2 are already in the same equivalence class. + * This only checks whether the core considers them to be equal; + * they may not actually be equal. + */ + bool theory_str::in_same_eqc(expr * n1, expr * n2) { + if (n1 == n2) return true; + context & ctx = get_context(); + ast_manager & m = get_manager(); - // add assertion - if (implyR) { - expr_ref implyLHS(mk_and(litems), m); - assert_implication(implyLHS, implyR); - } + // similar to get_eqc_value(), make absolutely sure + // that we've set this up properly for the context + + if (!ctx.e_internalized(n1)) { + TRACE("str", tout << "WARNING: expression " << mk_ismt2_pp(n1, m) << " was not internalized" << std::endl;); + ctx.internalize(n1, false); + } + if (!ctx.e_internalized(n2)) { + TRACE("str", tout << "WARNING: expression " << mk_ismt2_pp(n2, m) << " was not internalized" << std::endl;); + ctx.internalize(n2, false); + } + + expr * curr = get_eqc_next(n1); + while (curr != n1) { + if (curr == n2) + return true; + curr = get_eqc_next(curr); + } + return false; + } + + expr * theory_str::collect_eq_nodes(expr * n, expr_ref_vector & eqcSet) { + context & ctx = get_context(); + expr * constStrNode = NULL; + + expr * ex = n; + do { + if (u.str.is_string(to_app(ex))) { + constStrNode = ex; } - } // for (itor1 : contains_map) - } // if varNode in contain_pair_idx_map -} + eqcSet.push_back(ex); -void theory_str::check_contain_by_substr(expr * varNode, expr_ref_vector & willEqClass) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - expr_ref_vector litems(m); + ex = get_eqc_next(ex); + } while (ex != n); + return constStrNode; + } - if (contain_pair_idx_map.find(varNode) != contain_pair_idx_map.end()) { - std::set >::iterator itor1 = contain_pair_idx_map[varNode].begin(); - for (; itor1 != contain_pair_idx_map[varNode].end(); ++itor1) { - expr * strAst = itor1->first; - expr * substrAst = itor1->second; - - expr * boolVar; - if (!contain_pair_bool_map.find(strAst, substrAst, boolVar)) { - TRACE("str", tout << "warning: no entry for boolVar in contain_pair_bool_map" << std::endl;); + /* + * Collect constant strings (from left to right) in an AST node. + */ + void theory_str::get_const_str_asts_in_node(expr * node, expr_ref_vector & astList) { + ast_manager & m = get_manager(); + if (u.str.is_string(node)) { + astList.push_back(node); + //} else if (getNodeType(t, node) == my_Z3_Func) { + } else if (is_app(node)) { + app * func_app = to_app(node); + unsigned int argCount = func_app->get_num_args(); + for (unsigned int i = 0; i < argCount; i++) { + expr * argAst = func_app->get_arg(i); + get_const_str_asts_in_node(argAst, astList); } - // boolVar is actually a Contains term - app * containsApp = to_app(boolVar); + } + } - // we only want to inspect the Contains terms where either of strAst or substrAst - // are equal to varNode. + void theory_str::check_contain_by_eqc_val(expr * varNode, expr * constNode) { + context & ctx = get_context(); + ast_manager & m = get_manager(); - TRACE("t_str_detail", tout << "considering Contains with strAst = " << mk_pp(strAst, m) << ", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); + TRACE("str", tout << "varNode = " << mk_pp(varNode, m) << ", constNode = " << mk_pp(constNode, m) << std::endl;); - if (varNode != strAst && varNode != substrAst) { - TRACE("str", tout << "varNode not equal to strAst or substrAst, skip" << std::endl;); - continue; - } - TRACE("str", tout << "varNode matched one of strAst or substrAst. Continuing" << std::endl;); + expr_ref_vector litems(m); - if (substrAst == varNode) { - bool strAstHasVal = false; - expr * strValue = get_eqc_value(strAst, strAstHasVal); - if (strAstHasVal) { - TRACE("str", tout << mk_pp(strAst, m) << " has constant eqc value " << mk_pp(strValue, m) << std::endl;); - if (strValue != strAst) { - litems.push_back(ctx.mk_eq_atom(strAst, strValue)); + if (contain_pair_idx_map.find(varNode) != contain_pair_idx_map.end()) { + std::set >::iterator itor1 = contain_pair_idx_map[varNode].begin(); + for (; itor1 != contain_pair_idx_map[varNode].end(); ++itor1) { + expr * strAst = itor1->first; + expr * substrAst = itor1->second; + + expr * boolVar; + if (!contain_pair_bool_map.find(strAst, substrAst, boolVar)) { + TRACE("str", tout << "warning: no entry for boolVar in contain_pair_bool_map" << std::endl;); + } + // boolVar is actually a Contains term + app * containsApp = to_app(boolVar); + + // we only want to inspect the Contains terms where either of strAst or substrAst + // are equal to varNode. + + TRACE("t_str_detail", tout << "considering Contains with strAst = " << mk_pp(strAst, m) << ", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); + + if (varNode != strAst && varNode != substrAst) { + TRACE("str", tout << "varNode not equal to strAst or substrAst, skip" << std::endl;); + continue; + } + TRACE("str", tout << "varNode matched one of strAst or substrAst. Continuing" << std::endl;); + + // varEqcNode is str + if (strAst == varNode) { + expr_ref implyR(m); + litems.reset(); + + if (strAst != constNode) { + litems.push_back(ctx.mk_eq_atom(strAst, constNode)); } zstring strConst; - u.str.is_string(strValue, strConst); - // iterate eqc (also eqc-to-be) of substr - for (expr_ref_vector::iterator itAst = willEqClass.begin(); itAst != willEqClass.end(); itAst++) { - bool counterEgFound = false; - if (u.str.is_concat(to_app(*itAst))) { + u.str.is_string(constNode, strConst); + bool subStrHasEqcValue = false; + expr * substrValue = get_eqc_value(substrAst, subStrHasEqcValue); + if (substrValue != substrAst) { + litems.push_back(ctx.mk_eq_atom(substrAst, substrValue)); + } + + if (subStrHasEqcValue) { + // subStr has an eqc constant value + zstring subStrConst; + u.str.is_string(substrValue, subStrConst); + + TRACE("t_str_detail", tout << "strConst = " << strConst << ", subStrConst = " << subStrConst << "\n";); + + if (strConst.contains(subStrConst)) { + //implyR = ctx.mk_eq(ctx, boolVar, Z3_mk_true(ctx)); + implyR = boolVar; + } else { + //implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_false(ctx)); + implyR = m.mk_not(boolVar); + } + } else { + // ------------------------------------------------------------------------------------------------ + // subStr doesn't have an eqc contant value + // however, subStr equals to some concat(arg_1, arg_2, ..., arg_n) + // if arg_j is a constant and is not a part of the strConst, it's sure that the contains is false + // ** This check is needed here because the "strConst" and "strAst" may not be in a same eqc yet + // ------------------------------------------------------------------------------------------------ + // collect eqc concat + std::set eqcConcats; + get_concats_in_eqc(substrAst, eqcConcats); + for (std::set::iterator concatItor = eqcConcats.begin(); + concatItor != eqcConcats.end(); concatItor++) { expr_ref_vector constList(m); + bool counterEgFound = false; // get constant strings in concat - app * aConcat = to_app(*itAst); + expr * aConcat = *concatItor; get_const_str_asts_in_node(aConcat, constList); for (expr_ref_vector::iterator cstItor = constList.begin(); - cstItor != constList.end(); cstItor++) { + cstItor != constList.end(); cstItor++) { zstring pieceStr; u.str.is_string(*cstItor, pieceStr); if (!strConst.contains(pieceStr)) { - TRACE("str", tout << "Inconsistency found!" << std::endl;); counterEgFound = true; if (aConcat != substrAst) { litems.push_back(ctx.mk_eq_atom(substrAst, aConcat)); } - expr_ref implyLHS(mk_and(litems), m); - expr_ref implyR(m.mk_not(boolVar), m); - assert_implication(implyLHS, implyR); + //implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_false(ctx)); + implyR = m.mk_not(boolVar); break; } } - } - if (counterEgFound) { - break; + if (counterEgFound) { + TRACE("str", tout << "Inconsistency found!" << std::endl;); + break; + } } } + // add assertion + if (implyR) { + expr_ref implyLHS(mk_and(litems), m); + assert_implication(implyLHS, implyR); + } } - } - } - } // varNode in contain_pair_idx_map -} + // varEqcNode is subStr + else if (substrAst == varNode) { + expr_ref implyR(m); + litems.reset(); -bool theory_str::in_contain_idx_map(expr * n) { - return contain_pair_idx_map.find(n) != contain_pair_idx_map.end(); -} + if (substrAst != constNode) { + litems.push_back(ctx.mk_eq_atom(substrAst, constNode)); + } + bool strHasEqcValue = false; + expr * strValue = get_eqc_value(strAst, strHasEqcValue); + if (strValue != strAst) { + litems.push_back(ctx.mk_eq_atom(strAst, strValue)); + } -void theory_str::check_contain_by_eq_nodes(expr * n1, expr * n2) { - context & ctx = get_context(); - ast_manager & m = get_manager(); + if (strHasEqcValue) { + zstring strConst, subStrConst; + u.str.is_string(strValue, strConst); + u.str.is_string(constNode, subStrConst); + if (strConst.contains(subStrConst)) { + //implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_true(ctx)); + implyR = boolVar; + } else { + // implyR = Z3_mk_eq(ctx, boolVar, Z3_mk_false(ctx)); + implyR = m.mk_not(boolVar); + } + } - if (in_contain_idx_map(n1) && in_contain_idx_map(n2)) { - std::set >::iterator keysItor1 = contain_pair_idx_map[n1].begin(); - std::set >::iterator keysItor2; - - for (; keysItor1 != contain_pair_idx_map[n1].end(); keysItor1++) { - // keysItor1 is on set {<.., n1>, ..., , ...} - std::pair key1 = *keysItor1; - if (key1.first == n1 && key1.second == n2) { - expr_ref implyL(m); - expr_ref implyR(contain_pair_bool_map[key1], m); - if (n1 != n2) { - implyL = ctx.mk_eq_atom(n1, n2); - assert_implication(implyL, implyR); - } else { - assert_axiom(implyR); + // add assertion + if (implyR) { + expr_ref implyLHS(mk_and(litems), m); + assert_implication(implyLHS, implyR); + } } - } + } // for (itor1 : contains_map) + } // if varNode in contain_pair_idx_map + } - for (keysItor2 = contain_pair_idx_map[n2].begin(); - keysItor2 != contain_pair_idx_map[n2].end(); keysItor2++) { - // keysItor2 is on set {<.., n2>, ..., , ...} - std::pair key2 = *keysItor2; - // skip if the pair is eq - if (key1 == key2) { + void theory_str::check_contain_by_substr(expr * varNode, expr_ref_vector & willEqClass) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + expr_ref_vector litems(m); + + if (contain_pair_idx_map.find(varNode) != contain_pair_idx_map.end()) { + std::set >::iterator itor1 = contain_pair_idx_map[varNode].begin(); + for (; itor1 != contain_pair_idx_map[varNode].end(); ++itor1) { + expr * strAst = itor1->first; + expr * substrAst = itor1->second; + + expr * boolVar; + if (!contain_pair_bool_map.find(strAst, substrAst, boolVar)) { + TRACE("str", tout << "warning: no entry for boolVar in contain_pair_bool_map" << std::endl;); + } + // boolVar is actually a Contains term + app * containsApp = to_app(boolVar); + + // we only want to inspect the Contains terms where either of strAst or substrAst + // are equal to varNode. + + TRACE("t_str_detail", tout << "considering Contains with strAst = " << mk_pp(strAst, m) << ", substrAst = " << mk_pp(substrAst, m) << "..." << std::endl;); + + if (varNode != strAst && varNode != substrAst) { + TRACE("str", tout << "varNode not equal to strAst or substrAst, skip" << std::endl;); continue; } + TRACE("str", tout << "varNode matched one of strAst or substrAst. Continuing" << std::endl;); - // *************************** - // Case 1: Contains(m, ...) /\ Contains(n, ) /\ m = n - // *************************** - if (key1.first == n1 && key2.first == n2) { - expr * subAst1 = key1.second; - expr * subAst2 = key2.second; - bool subAst1HasValue = false; - bool subAst2HasValue = false; - expr * subValue1 = get_eqc_value(subAst1, subAst1HasValue); - expr * subValue2 = get_eqc_value(subAst2, subAst2HasValue); - - TRACE("str", - tout << "(Contains " << mk_pp(n1, m) << " " << mk_pp(subAst1, m) << ")" << std::endl; - tout << "(Contains " << mk_pp(n2, m) << " " << mk_pp(subAst2, m) << ")" << std::endl; - if (subAst1 != subValue1) { - tout << mk_pp(subAst1, m) << " = " << mk_pp(subValue1, m) << std::endl; + if (substrAst == varNode) { + bool strAstHasVal = false; + expr * strValue = get_eqc_value(strAst, strAstHasVal); + if (strAstHasVal) { + TRACE("str", tout << mk_pp(strAst, m) << " has constant eqc value " << mk_pp(strValue, m) << std::endl;); + if (strValue != strAst) { + litems.push_back(ctx.mk_eq_atom(strAst, strValue)); + } + zstring strConst; + u.str.is_string(strValue, strConst); + // iterate eqc (also eqc-to-be) of substr + for (expr_ref_vector::iterator itAst = willEqClass.begin(); itAst != willEqClass.end(); itAst++) { + bool counterEgFound = false; + if (u.str.is_concat(to_app(*itAst))) { + expr_ref_vector constList(m); + // get constant strings in concat + app * aConcat = to_app(*itAst); + get_const_str_asts_in_node(aConcat, constList); + for (expr_ref_vector::iterator cstItor = constList.begin(); + cstItor != constList.end(); cstItor++) { + zstring pieceStr; + u.str.is_string(*cstItor, pieceStr); + if (!strConst.contains(pieceStr)) { + TRACE("str", tout << "Inconsistency found!" << std::endl;); + counterEgFound = true; + if (aConcat != substrAst) { + litems.push_back(ctx.mk_eq_atom(substrAst, aConcat)); + } + expr_ref implyLHS(mk_and(litems), m); + expr_ref implyR(m.mk_not(boolVar), m); + assert_implication(implyLHS, implyR); + break; + } + } } - if (subAst2 != subValue2) { - tout << mk_pp(subAst2, m) << " = " << mk_pp(subValue2, m) << std::endl; - } - ); - - if (subAst1HasValue && subAst2HasValue) { - expr_ref_vector litems1(m); - if (n1 != n2) { - litems1.push_back(ctx.mk_eq_atom(n1, n2)); - } - if (subValue1 != subAst1) { - litems1.push_back(ctx.mk_eq_atom(subAst1, subValue1)); - } - if (subValue2 != subAst2) { - litems1.push_back(ctx.mk_eq_atom(subAst2, subValue2)); - } - - zstring subConst1, subConst2; - u.str.is_string(subValue1, subConst1); - u.str.is_string(subValue2, subConst2); - expr_ref implyR(m); - if (subConst1 == subConst2) { - // key1.first = key2.first /\ key1.second = key2.second - // ==> (containPairBoolMap[key1] = containPairBoolMap[key2]) - implyR = ctx.mk_eq_atom(contain_pair_bool_map[key1], contain_pair_bool_map[key2]); - } else if (subConst1.contains(subConst2)) { - // key1.first = key2.first /\ Contains(key1.second, key2.second) - // ==> (containPairBoolMap[key1] --> containPairBoolMap[key2]) - implyR = rewrite_implication(contain_pair_bool_map[key1], contain_pair_bool_map[key2]); - } else if (subConst2.contains(subConst1)) { - // key1.first = key2.first /\ Contains(key2.second, key1.second) - // ==> (containPairBoolMap[key2] --> containPairBoolMap[key1]) - implyR = rewrite_implication(contain_pair_bool_map[key2], contain_pair_bool_map[key1]); - } - - if (implyR) { - if (litems1.empty()) { - assert_axiom(implyR); - } else { - assert_implication(mk_and(litems1), implyR); + if (counterEgFound) { + break; } } + } + } + } + } // varNode in contain_pair_idx_map + } + + bool theory_str::in_contain_idx_map(expr * n) { + return contain_pair_idx_map.find(n) != contain_pair_idx_map.end(); + } + + void theory_str::check_contain_by_eq_nodes(expr * n1, expr * n2) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + if (in_contain_idx_map(n1) && in_contain_idx_map(n2)) { + std::set >::iterator keysItor1 = contain_pair_idx_map[n1].begin(); + std::set >::iterator keysItor2; + + for (; keysItor1 != contain_pair_idx_map[n1].end(); keysItor1++) { + // keysItor1 is on set {<.., n1>, ..., , ...} + std::pair key1 = *keysItor1; + if (key1.first == n1 && key1.second == n2) { + expr_ref implyL(m); + expr_ref implyR(contain_pair_bool_map[key1], m); + if (n1 != n2) { + implyL = ctx.mk_eq_atom(n1, n2); + assert_implication(implyL, implyR); } else { - expr_ref_vector subAst1Eqc(m); - expr_ref_vector subAst2Eqc(m); - collect_eq_nodes(subAst1, subAst1Eqc); - collect_eq_nodes(subAst2, subAst2Eqc); + assert_axiom(implyR); + } + } - if (subAst1Eqc.contains(subAst2)) { - // ----------------------------------------------------------- - // * key1.first = key2.first /\ key1.second = key2.second - // --> containPairBoolMap[key1] = containPairBoolMap[key2] - // ----------------------------------------------------------- - expr_ref_vector litems2(m); + for (keysItor2 = contain_pair_idx_map[n2].begin(); + keysItor2 != contain_pair_idx_map[n2].end(); keysItor2++) { + // keysItor2 is on set {<.., n2>, ..., , ...} + std::pair key2 = *keysItor2; + // skip if the pair is eq + if (key1 == key2) { + continue; + } + + // *************************** + // Case 1: Contains(m, ...) /\ Contains(n, ) /\ m = n + // *************************** + if (key1.first == n1 && key2.first == n2) { + expr * subAst1 = key1.second; + expr * subAst2 = key2.second; + bool subAst1HasValue = false; + bool subAst2HasValue = false; + expr * subValue1 = get_eqc_value(subAst1, subAst1HasValue); + expr * subValue2 = get_eqc_value(subAst2, subAst2HasValue); + + TRACE("str", + tout << "(Contains " << mk_pp(n1, m) << " " << mk_pp(subAst1, m) << ")" << std::endl; + tout << "(Contains " << mk_pp(n2, m) << " " << mk_pp(subAst2, m) << ")" << std::endl; + if (subAst1 != subValue1) { + tout << mk_pp(subAst1, m) << " = " << mk_pp(subValue1, m) << std::endl; + } + if (subAst2 != subValue2) { + tout << mk_pp(subAst2, m) << " = " << mk_pp(subValue2, m) << std::endl; + } + ); + + if (subAst1HasValue && subAst2HasValue) { + expr_ref_vector litems1(m); if (n1 != n2) { - litems2.push_back(ctx.mk_eq_atom(n1, n2)); + litems1.push_back(ctx.mk_eq_atom(n1, n2)); } - if (subAst1 != subAst2) { - litems2.push_back(ctx.mk_eq_atom(subAst1, subAst2)); + if (subValue1 != subAst1) { + litems1.push_back(ctx.mk_eq_atom(subAst1, subValue1)); } - expr_ref implyR(ctx.mk_eq_atom(contain_pair_bool_map[key1], contain_pair_bool_map[key2]), m); - if (litems2.empty()) { - assert_axiom(implyR); - } else { - assert_implication(mk_and(litems2), implyR); + if (subValue2 != subAst2) { + litems1.push_back(ctx.mk_eq_atom(subAst2, subValue2)); + } + + zstring subConst1, subConst2; + u.str.is_string(subValue1, subConst1); + u.str.is_string(subValue2, subConst2); + expr_ref implyR(m); + if (subConst1 == subConst2) { + // key1.first = key2.first /\ key1.second = key2.second + // ==> (containPairBoolMap[key1] = containPairBoolMap[key2]) + implyR = ctx.mk_eq_atom(contain_pair_bool_map[key1], contain_pair_bool_map[key2]); + } else if (subConst1.contains(subConst2)) { + // key1.first = key2.first /\ Contains(key1.second, key2.second) + // ==> (containPairBoolMap[key1] --> containPairBoolMap[key2]) + implyR = rewrite_implication(contain_pair_bool_map[key1], contain_pair_bool_map[key2]); + } else if (subConst2.contains(subConst1)) { + // key1.first = key2.first /\ Contains(key2.second, key1.second) + // ==> (containPairBoolMap[key2] --> containPairBoolMap[key1]) + implyR = rewrite_implication(contain_pair_bool_map[key2], contain_pair_bool_map[key1]); + } + + if (implyR) { + if (litems1.empty()) { + assert_axiom(implyR); + } else { + assert_implication(mk_and(litems1), implyR); + } } } else { - // ----------------------------------------------------------- - // * key1.first = key2.first - // check eqc(key1.second) and eqc(key2.second) - // ----------------------------------------------------------- - expr_ref_vector::iterator eqItorSub1 = subAst1Eqc.begin(); - for (; eqItorSub1 != subAst1Eqc.end(); eqItorSub1++) { - expr_ref_vector::iterator eqItorSub2 = subAst2Eqc.begin(); - for (; eqItorSub2 != subAst2Eqc.end(); eqItorSub2++) { - // ------------ - // key1.first = key2.first /\ containPairBoolMap[] - // ==> (containPairBoolMap[key1] --> containPairBoolMap[key2]) - // ------------ - { - expr_ref_vector litems3(m); - if (n1 != n2) { - litems3.push_back(ctx.mk_eq_atom(n1, n2)); + expr_ref_vector subAst1Eqc(m); + expr_ref_vector subAst2Eqc(m); + collect_eq_nodes(subAst1, subAst1Eqc); + collect_eq_nodes(subAst2, subAst2Eqc); + + if (subAst1Eqc.contains(subAst2)) { + // ----------------------------------------------------------- + // * key1.first = key2.first /\ key1.second = key2.second + // --> containPairBoolMap[key1] = containPairBoolMap[key2] + // ----------------------------------------------------------- + expr_ref_vector litems2(m); + if (n1 != n2) { + litems2.push_back(ctx.mk_eq_atom(n1, n2)); + } + if (subAst1 != subAst2) { + litems2.push_back(ctx.mk_eq_atom(subAst1, subAst2)); + } + expr_ref implyR(ctx.mk_eq_atom(contain_pair_bool_map[key1], contain_pair_bool_map[key2]), m); + if (litems2.empty()) { + assert_axiom(implyR); + } else { + assert_implication(mk_and(litems2), implyR); + } + } else { + // ----------------------------------------------------------- + // * key1.first = key2.first + // check eqc(key1.second) and eqc(key2.second) + // ----------------------------------------------------------- + expr_ref_vector::iterator eqItorSub1 = subAst1Eqc.begin(); + for (; eqItorSub1 != subAst1Eqc.end(); eqItorSub1++) { + expr_ref_vector::iterator eqItorSub2 = subAst2Eqc.begin(); + for (; eqItorSub2 != subAst2Eqc.end(); eqItorSub2++) { + // ------------ + // key1.first = key2.first /\ containPairBoolMap[] + // ==> (containPairBoolMap[key1] --> containPairBoolMap[key2]) + // ------------ + { + expr_ref_vector litems3(m); + if (n1 != n2) { + litems3.push_back(ctx.mk_eq_atom(n1, n2)); + } + expr * eqSubVar1 = *eqItorSub1; + if (eqSubVar1 != subAst1) { + litems3.push_back(ctx.mk_eq_atom(subAst1, eqSubVar1)); + } + expr * eqSubVar2 = *eqItorSub2; + if (eqSubVar2 != subAst2) { + litems3.push_back(ctx.mk_eq_atom(subAst2, eqSubVar2)); + } + std::pair tryKey1 = std::make_pair(eqSubVar1, eqSubVar2); + if (contain_pair_bool_map.contains(tryKey1)) { + TRACE("str", tout << "(Contains " << mk_pp(eqSubVar1, m) << " " << mk_pp(eqSubVar2, m) << ")" << std::endl;); + litems3.push_back(contain_pair_bool_map[tryKey1]); + expr_ref implR(rewrite_implication(contain_pair_bool_map[key1], contain_pair_bool_map[key2]), m); + assert_implication(mk_and(litems3), implR); + } } - expr * eqSubVar1 = *eqItorSub1; - if (eqSubVar1 != subAst1) { - litems3.push_back(ctx.mk_eq_atom(subAst1, eqSubVar1)); - } - expr * eqSubVar2 = *eqItorSub2; - if (eqSubVar2 != subAst2) { - litems3.push_back(ctx.mk_eq_atom(subAst2, eqSubVar2)); - } - std::pair tryKey1 = std::make_pair(eqSubVar1, eqSubVar2); - if (contain_pair_bool_map.contains(tryKey1)) { - TRACE("str", tout << "(Contains " << mk_pp(eqSubVar1, m) << " " << mk_pp(eqSubVar2, m) << ")" << std::endl;); - litems3.push_back(contain_pair_bool_map[tryKey1]); - expr_ref implR(rewrite_implication(contain_pair_bool_map[key1], contain_pair_bool_map[key2]), m); - assert_implication(mk_and(litems3), implR); - } - } - // ------------ - // key1.first = key2.first /\ containPairBoolMap[] - // ==> (containPairBoolMap[key2] --> containPairBoolMap[key1]) - // ------------ - { - expr_ref_vector litems4(m); - if (n1 != n2) { - litems4.push_back(ctx.mk_eq_atom(n1, n2)); - } - expr * eqSubVar1 = *eqItorSub1; - if (eqSubVar1 != subAst1) { - litems4.push_back(ctx.mk_eq_atom(subAst1, eqSubVar1)); - } - expr * eqSubVar2 = *eqItorSub2; - if (eqSubVar2 != subAst2) { - litems4.push_back(ctx.mk_eq_atom(subAst2, eqSubVar2)); - } - std::pair tryKey2 = std::make_pair(eqSubVar2, eqSubVar1); - if (contain_pair_bool_map.contains(tryKey2)) { - TRACE("str", tout << "(Contains " << mk_pp(eqSubVar2, m) << " " << mk_pp(eqSubVar1, m) << ")" << std::endl;); - litems4.push_back(contain_pair_bool_map[tryKey2]); - expr_ref implR(rewrite_implication(contain_pair_bool_map[key2], contain_pair_bool_map[key1]), m); - assert_implication(mk_and(litems4), implR); + // ------------ + // key1.first = key2.first /\ containPairBoolMap[] + // ==> (containPairBoolMap[key2] --> containPairBoolMap[key1]) + // ------------ + { + expr_ref_vector litems4(m); + if (n1 != n2) { + litems4.push_back(ctx.mk_eq_atom(n1, n2)); + } + expr * eqSubVar1 = *eqItorSub1; + if (eqSubVar1 != subAst1) { + litems4.push_back(ctx.mk_eq_atom(subAst1, eqSubVar1)); + } + expr * eqSubVar2 = *eqItorSub2; + if (eqSubVar2 != subAst2) { + litems4.push_back(ctx.mk_eq_atom(subAst2, eqSubVar2)); + } + std::pair tryKey2 = std::make_pair(eqSubVar2, eqSubVar1); + if (contain_pair_bool_map.contains(tryKey2)) { + TRACE("str", tout << "(Contains " << mk_pp(eqSubVar2, m) << " " << mk_pp(eqSubVar1, m) << ")" << std::endl;); + litems4.push_back(contain_pair_bool_map[tryKey2]); + expr_ref implR(rewrite_implication(contain_pair_bool_map[key2], contain_pair_bool_map[key1]), m); + assert_implication(mk_and(litems4), implR); + } } } } } } } - } - // *************************** - // Case 2: Contains(..., m) /\ Contains(... , n) /\ m = n - // *************************** - else if (key1.second == n1 && key2.second == n2) { - expr * str1 = key1.first; - expr * str2 = key2.first; - bool str1HasValue = false; - bool str2HasValue = false; - expr * strVal1 = get_eqc_value(str1, str1HasValue); - expr * strVal2 = get_eqc_value(str2, str2HasValue); + // *************************** + // Case 2: Contains(..., m) /\ Contains(... , n) /\ m = n + // *************************** + else if (key1.second == n1 && key2.second == n2) { + expr * str1 = key1.first; + expr * str2 = key2.first; + bool str1HasValue = false; + bool str2HasValue = false; + expr * strVal1 = get_eqc_value(str1, str1HasValue); + expr * strVal2 = get_eqc_value(str2, str2HasValue); - TRACE("str", - tout << "(Contains " << mk_pp(str1, m) << " " << mk_pp(n1, m) << ")" << std::endl; - tout << "(Contains " << mk_pp(str2, m) << " " << mk_pp(n2, m) << ")" << std::endl; - if (str1 != strVal1) { - tout << mk_pp(str1, m) << " = " << mk_pp(strVal1, m) << std::endl; - } - if (str2 != strVal2) { - tout << mk_pp(str2, m) << " = " << mk_pp(strVal2, m) << std::endl; - } - ); + TRACE("str", + tout << "(Contains " << mk_pp(str1, m) << " " << mk_pp(n1, m) << ")" << std::endl; + tout << "(Contains " << mk_pp(str2, m) << " " << mk_pp(n2, m) << ")" << std::endl; + if (str1 != strVal1) { + tout << mk_pp(str1, m) << " = " << mk_pp(strVal1, m) << std::endl; + } + if (str2 != strVal2) { + tout << mk_pp(str2, m) << " = " << mk_pp(strVal2, m) << std::endl; + } + ); - if (str1HasValue && str2HasValue) { - expr_ref_vector litems1(m); - if (n1 != n2) { - litems1.push_back(ctx.mk_eq_atom(n1, n2)); - } - if (strVal1 != str1) { - litems1.push_back(ctx.mk_eq_atom(str1, strVal1)); - } - if (strVal2 != str2) { - litems1.push_back(ctx.mk_eq_atom(str2, strVal2)); - } - - zstring const1, const2; - u.str.is_string(strVal1, const1); - u.str.is_string(strVal2, const2); - expr_ref implyR(m); - - if (const1 == const2) { - // key1.second = key2.second /\ key1.first = key2.first - // ==> (containPairBoolMap[key1] = containPairBoolMap[key2]) - implyR = ctx.mk_eq_atom(contain_pair_bool_map[key1], contain_pair_bool_map[key2]); - } else if (const1.contains(const2)) { - // key1.second = key2.second /\ Contains(key1.first, key2.first) - // ==> (containPairBoolMap[key2] --> containPairBoolMap[key1]) - implyR = rewrite_implication(contain_pair_bool_map[key2], contain_pair_bool_map[key1]); - } else if (const2.contains(const1)) { - // key1.first = key2.first /\ Contains(key2.first, key1.first) - // ==> (containPairBoolMap[key1] --> containPairBoolMap[key2]) - implyR = rewrite_implication(contain_pair_bool_map[key1], contain_pair_bool_map[key2]); - } - - if (implyR) { - if (litems1.size() == 0) { - assert_axiom(implyR); - } else { - assert_implication(mk_and(litems1), implyR); - } - } - } - - else { - expr_ref_vector str1Eqc(m); - expr_ref_vector str2Eqc(m); - collect_eq_nodes(str1, str1Eqc); - collect_eq_nodes(str2, str2Eqc); - - if (str1Eqc.contains(str2)) { - // ----------------------------------------------------------- - // * key1.first = key2.first /\ key1.second = key2.second - // --> containPairBoolMap[key1] = containPairBoolMap[key2] - // ----------------------------------------------------------- - expr_ref_vector litems2(m); + if (str1HasValue && str2HasValue) { + expr_ref_vector litems1(m); if (n1 != n2) { - litems2.push_back(ctx.mk_eq_atom(n1, n2)); + litems1.push_back(ctx.mk_eq_atom(n1, n2)); } - if (str1 != str2) { - litems2.push_back(ctx.mk_eq_atom(str1, str2)); + if (strVal1 != str1) { + litems1.push_back(ctx.mk_eq_atom(str1, strVal1)); } - expr_ref implyR(ctx.mk_eq_atom(contain_pair_bool_map[key1], contain_pair_bool_map[key2]), m); - if (litems2.empty()) { - assert_axiom(implyR); + if (strVal2 != str2) { + litems1.push_back(ctx.mk_eq_atom(str2, strVal2)); + } + + zstring const1, const2; + u.str.is_string(strVal1, const1); + u.str.is_string(strVal2, const2); + expr_ref implyR(m); + + if (const1 == const2) { + // key1.second = key2.second /\ key1.first = key2.first + // ==> (containPairBoolMap[key1] = containPairBoolMap[key2]) + implyR = ctx.mk_eq_atom(contain_pair_bool_map[key1], contain_pair_bool_map[key2]); + } else if (const1.contains(const2)) { + // key1.second = key2.second /\ Contains(key1.first, key2.first) + // ==> (containPairBoolMap[key2] --> containPairBoolMap[key1]) + implyR = rewrite_implication(contain_pair_bool_map[key2], contain_pair_bool_map[key1]); + } else if (const2.contains(const1)) { + // key1.first = key2.first /\ Contains(key2.first, key1.first) + // ==> (containPairBoolMap[key1] --> containPairBoolMap[key2]) + implyR = rewrite_implication(contain_pair_bool_map[key1], contain_pair_bool_map[key2]); + } + + if (implyR) { + if (litems1.size() == 0) { + assert_axiom(implyR); + } else { + assert_implication(mk_and(litems1), implyR); + } + } + } + + else { + expr_ref_vector str1Eqc(m); + expr_ref_vector str2Eqc(m); + collect_eq_nodes(str1, str1Eqc); + collect_eq_nodes(str2, str2Eqc); + + if (str1Eqc.contains(str2)) { + // ----------------------------------------------------------- + // * key1.first = key2.first /\ key1.second = key2.second + // --> containPairBoolMap[key1] = containPairBoolMap[key2] + // ----------------------------------------------------------- + expr_ref_vector litems2(m); + if (n1 != n2) { + litems2.push_back(ctx.mk_eq_atom(n1, n2)); + } + if (str1 != str2) { + litems2.push_back(ctx.mk_eq_atom(str1, str2)); + } + expr_ref implyR(ctx.mk_eq_atom(contain_pair_bool_map[key1], contain_pair_bool_map[key2]), m); + if (litems2.empty()) { + assert_axiom(implyR); + } else { + assert_implication(mk_and(litems2), implyR); + } } else { - assert_implication(mk_and(litems2), implyR); - } - } else { - // ----------------------------------------------------------- - // * key1.second = key2.second - // check eqc(key1.first) and eqc(key2.first) - // ----------------------------------------------------------- - expr_ref_vector::iterator eqItorStr1 = str1Eqc.begin(); - for (; eqItorStr1 != str1Eqc.end(); eqItorStr1++) { - expr_ref_vector::iterator eqItorStr2 = str2Eqc.begin(); - for (; eqItorStr2 != str2Eqc.end(); eqItorStr2++) { - { - expr_ref_vector litems3(m); - if (n1 != n2) { - litems3.push_back(ctx.mk_eq_atom(n1, n2)); - } - expr * eqStrVar1 = *eqItorStr1; - if (eqStrVar1 != str1) { - litems3.push_back(ctx.mk_eq_atom(str1, eqStrVar1)); - } - expr * eqStrVar2 = *eqItorStr2; - if (eqStrVar2 != str2) { - litems3.push_back(ctx.mk_eq_atom(str2, eqStrVar2)); - } - std::pair tryKey1 = std::make_pair(eqStrVar1, eqStrVar2); - if (contain_pair_bool_map.contains(tryKey1)) { - TRACE("str", tout << "(Contains " << mk_pp(eqStrVar1, m) << " " << mk_pp(eqStrVar2, m) << ")" << std::endl;); - litems3.push_back(contain_pair_bool_map[tryKey1]); + // ----------------------------------------------------------- + // * key1.second = key2.second + // check eqc(key1.first) and eqc(key2.first) + // ----------------------------------------------------------- + expr_ref_vector::iterator eqItorStr1 = str1Eqc.begin(); + for (; eqItorStr1 != str1Eqc.end(); eqItorStr1++) { + expr_ref_vector::iterator eqItorStr2 = str2Eqc.begin(); + for (; eqItorStr2 != str2Eqc.end(); eqItorStr2++) { + { + expr_ref_vector litems3(m); + if (n1 != n2) { + litems3.push_back(ctx.mk_eq_atom(n1, n2)); + } + expr * eqStrVar1 = *eqItorStr1; + if (eqStrVar1 != str1) { + litems3.push_back(ctx.mk_eq_atom(str1, eqStrVar1)); + } + expr * eqStrVar2 = *eqItorStr2; + if (eqStrVar2 != str2) { + litems3.push_back(ctx.mk_eq_atom(str2, eqStrVar2)); + } + std::pair tryKey1 = std::make_pair(eqStrVar1, eqStrVar2); + if (contain_pair_bool_map.contains(tryKey1)) { + TRACE("str", tout << "(Contains " << mk_pp(eqStrVar1, m) << " " << mk_pp(eqStrVar2, m) << ")" << std::endl;); + litems3.push_back(contain_pair_bool_map[tryKey1]); - // ------------ - // key1.second = key2.second /\ containPairBoolMap[] - // ==> (containPairBoolMap[key2] --> containPairBoolMap[key1]) - // ------------ - expr_ref implR(rewrite_implication(contain_pair_bool_map[key2], contain_pair_bool_map[key1]), m); - assert_implication(mk_and(litems3), implR); + // ------------ + // key1.second = key2.second /\ containPairBoolMap[] + // ==> (containPairBoolMap[key2] --> containPairBoolMap[key1]) + // ------------ + expr_ref implR(rewrite_implication(contain_pair_bool_map[key2], contain_pair_bool_map[key1]), m); + assert_implication(mk_and(litems3), implR); + } } - } - { - expr_ref_vector litems4(m); - if (n1 != n2) { - litems4.push_back(ctx.mk_eq_atom(n1, n2)); - } - expr * eqStrVar1 = *eqItorStr1; - if (eqStrVar1 != str1) { - litems4.push_back(ctx.mk_eq_atom(str1, eqStrVar1)); - } - expr *eqStrVar2 = *eqItorStr2; - if (eqStrVar2 != str2) { - litems4.push_back(ctx.mk_eq_atom(str2, eqStrVar2)); - } - std::pair tryKey2 = std::make_pair(eqStrVar2, eqStrVar1); + { + expr_ref_vector litems4(m); + if (n1 != n2) { + litems4.push_back(ctx.mk_eq_atom(n1, n2)); + } + expr * eqStrVar1 = *eqItorStr1; + if (eqStrVar1 != str1) { + litems4.push_back(ctx.mk_eq_atom(str1, eqStrVar1)); + } + expr *eqStrVar2 = *eqItorStr2; + if (eqStrVar2 != str2) { + litems4.push_back(ctx.mk_eq_atom(str2, eqStrVar2)); + } + std::pair tryKey2 = std::make_pair(eqStrVar2, eqStrVar1); - if (contain_pair_bool_map.contains(tryKey2)) { - TRACE("str", tout << "(Contains " << mk_pp(eqStrVar2, m) << " " << mk_pp(eqStrVar1, m) << ")" << std::endl;); - litems4.push_back(contain_pair_bool_map[tryKey2]); - // ------------ - // key1.first = key2.first /\ containPairBoolMap[] - // ==> (containPairBoolMap[key1] --> containPairBoolMap[key2]) - // ------------ - expr_ref implR(rewrite_implication(contain_pair_bool_map[key1], contain_pair_bool_map[key2]), m); - assert_implication(mk_and(litems4), implR); + if (contain_pair_bool_map.contains(tryKey2)) { + TRACE("str", tout << "(Contains " << mk_pp(eqStrVar2, m) << " " << mk_pp(eqStrVar1, m) << ")" << std::endl;); + litems4.push_back(contain_pair_bool_map[tryKey2]); + // ------------ + // key1.first = key2.first /\ containPairBoolMap[] + // ==> (containPairBoolMap[key1] --> containPairBoolMap[key2]) + // ------------ + expr_ref implR(rewrite_implication(contain_pair_bool_map[key1], contain_pair_bool_map[key2]), m); + assert_implication(mk_and(litems4), implR); + } } } } } } - } - } - } - - if (n1 == n2) { - break; - } - } - } // (in_contain_idx_map(n1) && in_contain_idx_map(n2)) -} - -void theory_str::check_contain_in_new_eq(expr * n1, expr * n2) { - if (contains_map.empty()) { - return; - } - - context & ctx = get_context(); - ast_manager & m = get_manager(); - TRACE("str", tout << "consistency check for contains wrt. " << mk_pp(n1, m) << " and " << mk_pp(n2, m) << std::endl;); - - expr_ref_vector willEqClass(m); - expr * constStrAst_1 = collect_eq_nodes(n1, willEqClass); - expr * constStrAst_2 = collect_eq_nodes(n2, willEqClass); - expr * constStrAst = (constStrAst_1 != NULL) ? constStrAst_1 : constStrAst_2; - - TRACE("str", tout << "eqc of n1 is {"; - for (expr_ref_vector::iterator it = willEqClass.begin(); it != willEqClass.end(); ++it) { - expr * el = *it; - tout << " " << mk_pp(el, m); - } - tout << std::endl; - if (constStrAst == NULL) { - tout << "constStrAst = NULL" << std::endl; - } else { - tout << "constStrAst = " << mk_pp(constStrAst, m) << std::endl; - } - ); - - // step 1: we may have constant values for Contains checks now - if (constStrAst != NULL) { - expr_ref_vector::iterator itAst = willEqClass.begin(); - for (; itAst != willEqClass.end(); itAst++) { - if (*itAst == constStrAst) { - continue; - } - check_contain_by_eqc_val(*itAst, constStrAst); - } - } else { - // no concrete value to be put in eqc, solely based on context - // Check here is used to detected the facts as follows: - // * known: contains(Z, Y) /\ Z = "abcdefg" /\ Y = M - // * new fact: M = concat(..., "jio", ...) - // Note that in this branch, either M or concat(..., "jio", ...) has a constant value - // So, only need to check - // * "EQC(M) U EQC(concat(..., "jio", ...))" as substr and - // * If strAst registered has an eqc constant in the context - // ------------------------------------------------------------- - expr_ref_vector::iterator itAst = willEqClass.begin(); - for (; itAst != willEqClass.end(); ++itAst) { - check_contain_by_substr(*itAst, willEqClass); - } - } - - // ------------------------------------------ - // step 2: check for b1 = contains(x, m), b2 = contains(y, n) - // (1) x = y /\ m = n ==> b1 = b2 - // (2) x = y /\ Contains(const(m), const(n)) ==> (b1 -> b2) - // (3) x = y /\ Contains(const(n), const(m)) ==> (b2 -> b1) - // (4) x = y /\ containPairBoolMap[] ==> (b1 -> b2) - // (5) x = y /\ containPairBoolMap[] ==> (b2 -> b1) - // (6) Contains(const(x), const(y)) /\ m = n ==> (b2 -> b1) - // (7) Contains(const(y), const(x)) /\ m = n ==> (b1 -> b2) - // (8) containPairBoolMap[] /\ m = n ==> (b2 -> b1) - // (9) containPairBoolMap[] /\ m = n ==> (b1 -> b2) - // ------------------------------------------ - - expr_ref_vector::iterator varItor1 = willEqClass.begin(); - for (; varItor1 != willEqClass.end(); ++varItor1) { - expr * varAst1 = *varItor1; - expr_ref_vector::iterator varItor2 = varItor1; - for (; varItor2 != willEqClass.end(); ++varItor2) { - expr * varAst2 = *varItor2; - check_contain_by_eq_nodes(varAst1, varAst2); - } - } -} - -expr * theory_str::dealias_node(expr * node, std::map & varAliasMap, std::map & concatAliasMap) { - if (variable_set.find(node) != variable_set.end()) { - return get_alias_index_ast(varAliasMap, node); - } else if (u.str.is_concat(to_app(node))) { - return get_alias_index_ast(concatAliasMap, node); - } - return node; -} - -void theory_str::get_grounded_concats(expr* node, std::map & varAliasMap, - std::map & concatAliasMap, std::map & varConstMap, - std::map & concatConstMap, std::map > & varEqConcatMap, - std::map, std::set > > & groundedMap) { - if (u.re.is_unroll(to_app(node))) { - return; - } - // ************************************************** - // first deAlias the node if it is a var or concat - // ************************************************** - node = dealias_node(node, varAliasMap, concatAliasMap); - - if (groundedMap.find(node) != groundedMap.end()) { - return; - } - - // haven't computed grounded concats for "node" (de-aliased) - // --------------------------------------------------------- - - context & ctx = get_context(); - ast_manager & m = get_manager(); - - // const strings: node is de-aliased - if (u.str.is_string(node)) { - std::vector concatNodes; - concatNodes.push_back(node); - groundedMap[node][concatNodes].clear(); // no condition - } - // Concat functions - else if (u.str.is_concat(to_app(node))) { - // if "node" equals to a constant string, thenjust push the constant into the concat vector - // Again "node" has been de-aliased at the very beginning - if (concatConstMap.find(node) != concatConstMap.end()) { - std::vector concatNodes; - concatNodes.push_back(concatConstMap[node]); - groundedMap[node][concatNodes].clear(); - groundedMap[node][concatNodes].insert(ctx.mk_eq_atom(node, concatConstMap[node])); - } - // node doesn't have eq constant value. Process its children. - else { - // merge arg0 and arg1 - expr * arg0 = to_app(node)->get_arg(0); - expr * arg1 = to_app(node)->get_arg(1); - expr * arg0DeAlias = dealias_node(arg0, varAliasMap, concatAliasMap); - expr * arg1DeAlias = dealias_node(arg1, varAliasMap, concatAliasMap); - get_grounded_concats(arg0DeAlias, varAliasMap, concatAliasMap, varConstMap, concatConstMap, varEqConcatMap, groundedMap); - get_grounded_concats(arg1DeAlias, varAliasMap, concatAliasMap, varConstMap, concatConstMap, varEqConcatMap, groundedMap); - - std::map, std::set >::iterator arg0_grdItor = groundedMap[arg0DeAlias].begin(); - std::map, std::set >::iterator arg1_grdItor; - for (; arg0_grdItor != groundedMap[arg0DeAlias].end(); arg0_grdItor++) { - arg1_grdItor = groundedMap[arg1DeAlias].begin(); - for (; arg1_grdItor != groundedMap[arg1DeAlias].end(); arg1_grdItor++) { - std::vector ndVec; - ndVec.insert(ndVec.end(), arg0_grdItor->first.begin(), arg0_grdItor->first.end()); - int arg0VecSize = arg0_grdItor->first.size(); - int arg1VecSize = arg1_grdItor->first.size(); - if (arg0VecSize > 0 && arg1VecSize > 0 && u.str.is_string(arg0_grdItor->first[arg0VecSize - 1]) && u.str.is_string(arg1_grdItor->first[0])) { - ndVec.pop_back(); - ndVec.push_back(mk_concat(arg0_grdItor->first[arg0VecSize - 1], arg1_grdItor->first[0])); - for (int i = 1; i < arg1VecSize; i++) { - ndVec.push_back(arg1_grdItor->first[i]); - } - } else { - ndVec.insert(ndVec.end(), arg1_grdItor->first.begin(), arg1_grdItor->first.end()); - } - // only insert if we don't know "node = concat(ndVec)" since one set of condition leads to this is enough - if (groundedMap[node].find(ndVec) == groundedMap[node].end()) { - groundedMap[node][ndVec]; - if (arg0 != arg0DeAlias) { - groundedMap[node][ndVec].insert(ctx.mk_eq_atom(arg0, arg0DeAlias)); - } - groundedMap[node][ndVec].insert(arg0_grdItor->second.begin(), arg0_grdItor->second.end()); - - if (arg1 != arg1DeAlias) { - groundedMap[node][ndVec].insert(ctx.mk_eq_atom(arg1, arg1DeAlias)); - } - groundedMap[node][ndVec].insert(arg1_grdItor->second.begin(), arg1_grdItor->second.end()); } } + + if (n1 == n2) { + break; + } + } + } // (in_contain_idx_map(n1) && in_contain_idx_map(n2)) + } + + void theory_str::check_contain_in_new_eq(expr * n1, expr * n2) { + if (contains_map.empty()) { + return; + } + + context & ctx = get_context(); + ast_manager & m = get_manager(); + TRACE("str", tout << "consistency check for contains wrt. " << mk_pp(n1, m) << " and " << mk_pp(n2, m) << std::endl;); + + expr_ref_vector willEqClass(m); + expr * constStrAst_1 = collect_eq_nodes(n1, willEqClass); + expr * constStrAst_2 = collect_eq_nodes(n2, willEqClass); + expr * constStrAst = (constStrAst_1 != NULL) ? constStrAst_1 : constStrAst_2; + + TRACE("str", tout << "eqc of n1 is {"; + for (expr_ref_vector::iterator it = willEqClass.begin(); it != willEqClass.end(); ++it) { + expr * el = *it; + tout << " " << mk_pp(el, m); + } + tout << std::endl; + if (constStrAst == NULL) { + tout << "constStrAst = NULL" << std::endl; + } else { + tout << "constStrAst = " << mk_pp(constStrAst, m) << std::endl; + } + ); + + // step 1: we may have constant values for Contains checks now + if (constStrAst != NULL) { + expr_ref_vector::iterator itAst = willEqClass.begin(); + for (; itAst != willEqClass.end(); itAst++) { + if (*itAst == constStrAst) { + continue; + } + check_contain_by_eqc_val(*itAst, constStrAst); + } + } else { + // no concrete value to be put in eqc, solely based on context + // Check here is used to detected the facts as follows: + // * known: contains(Z, Y) /\ Z = "abcdefg" /\ Y = M + // * new fact: M = concat(..., "jio", ...) + // Note that in this branch, either M or concat(..., "jio", ...) has a constant value + // So, only need to check + // * "EQC(M) U EQC(concat(..., "jio", ...))" as substr and + // * If strAst registered has an eqc constant in the context + // ------------------------------------------------------------- + expr_ref_vector::iterator itAst = willEqClass.begin(); + for (; itAst != willEqClass.end(); ++itAst) { + check_contain_by_substr(*itAst, willEqClass); + } + } + + // ------------------------------------------ + // step 2: check for b1 = contains(x, m), b2 = contains(y, n) + // (1) x = y /\ m = n ==> b1 = b2 + // (2) x = y /\ Contains(const(m), const(n)) ==> (b1 -> b2) + // (3) x = y /\ Contains(const(n), const(m)) ==> (b2 -> b1) + // (4) x = y /\ containPairBoolMap[] ==> (b1 -> b2) + // (5) x = y /\ containPairBoolMap[] ==> (b2 -> b1) + // (6) Contains(const(x), const(y)) /\ m = n ==> (b2 -> b1) + // (7) Contains(const(y), const(x)) /\ m = n ==> (b1 -> b2) + // (8) containPairBoolMap[] /\ m = n ==> (b2 -> b1) + // (9) containPairBoolMap[] /\ m = n ==> (b1 -> b2) + // ------------------------------------------ + + expr_ref_vector::iterator varItor1 = willEqClass.begin(); + for (; varItor1 != willEqClass.end(); ++varItor1) { + expr * varAst1 = *varItor1; + expr_ref_vector::iterator varItor2 = varItor1; + for (; varItor2 != willEqClass.end(); ++varItor2) { + expr * varAst2 = *varItor2; + check_contain_by_eq_nodes(varAst1, varAst2); } } } - // string variables - else if (variable_set.find(node) != variable_set.end()) { - // deAliasedVar = Constant - if (varConstMap.find(node) != varConstMap.end()) { - std::vector concatNodes; - concatNodes.push_back(varConstMap[node]); - groundedMap[node][concatNodes].clear(); - groundedMap[node][concatNodes].insert(ctx.mk_eq_atom(node, varConstMap[node])); - } - // deAliasedVar = someConcat - else if (varEqConcatMap.find(node) != varEqConcatMap.end()) { - expr * eqConcat = varEqConcatMap[node].begin()->first; - expr * deAliasedEqConcat = dealias_node(eqConcat, varAliasMap, concatAliasMap); - get_grounded_concats(deAliasedEqConcat, varAliasMap, concatAliasMap, varConstMap, concatConstMap, varEqConcatMap, groundedMap); - std::map, std::set >::iterator grdItor = groundedMap[deAliasedEqConcat].begin(); - for (; grdItor != groundedMap[deAliasedEqConcat].end(); grdItor++) { - std::vector ndVec; - ndVec.insert(ndVec.end(), grdItor->first.begin(), grdItor->first.end()); - // only insert if we don't know "node = concat(ndVec)" since one set of condition leads to this is enough - if (groundedMap[node].find(ndVec) == groundedMap[node].end()) { - // condition: node = deAliasedEqConcat - groundedMap[node][ndVec].insert(ctx.mk_eq_atom(node, deAliasedEqConcat)); - // appending conditions for "deAliasedEqConcat = CONCAT(ndVec)" - groundedMap[node][ndVec].insert(grdItor->second.begin(), grdItor->second.end()); - } - } + expr * theory_str::dealias_node(expr * node, std::map & varAliasMap, std::map & concatAliasMap) { + if (variable_set.find(node) != variable_set.end()) { + return get_alias_index_ast(varAliasMap, node); + } else if (u.str.is_concat(to_app(node))) { + return get_alias_index_ast(concatAliasMap, node); } - // node (has been de-aliased) != constant && node (has been de-aliased) != any concat - // just push in the deAliasedVar - else { + return node; + } + + void theory_str::get_grounded_concats(expr* node, std::map & varAliasMap, + std::map & concatAliasMap, std::map & varConstMap, + std::map & concatConstMap, std::map > & varEqConcatMap, + std::map, std::set > > & groundedMap) { + if (u.re.is_unroll(to_app(node))) { + return; + } + // ************************************************** + // first deAlias the node if it is a var or concat + // ************************************************** + node = dealias_node(node, varAliasMap, concatAliasMap); + + if (groundedMap.find(node) != groundedMap.end()) { + return; + } + + // haven't computed grounded concats for "node" (de-aliased) + // --------------------------------------------------------- + + context & ctx = get_context(); + ast_manager & m = get_manager(); + + // const strings: node is de-aliased + if (u.str.is_string(node)) { std::vector concatNodes; concatNodes.push_back(node); - groundedMap[node][concatNodes]; + groundedMap[node][concatNodes].clear(); // no condition + } + // Concat functions + else if (u.str.is_concat(to_app(node))) { + // if "node" equals to a constant string, thenjust push the constant into the concat vector + // Again "node" has been de-aliased at the very beginning + if (concatConstMap.find(node) != concatConstMap.end()) { + std::vector concatNodes; + concatNodes.push_back(concatConstMap[node]); + groundedMap[node][concatNodes].clear(); + groundedMap[node][concatNodes].insert(ctx.mk_eq_atom(node, concatConstMap[node])); + } + // node doesn't have eq constant value. Process its children. + else { + // merge arg0 and arg1 + expr * arg0 = to_app(node)->get_arg(0); + expr * arg1 = to_app(node)->get_arg(1); + expr * arg0DeAlias = dealias_node(arg0, varAliasMap, concatAliasMap); + expr * arg1DeAlias = dealias_node(arg1, varAliasMap, concatAliasMap); + get_grounded_concats(arg0DeAlias, varAliasMap, concatAliasMap, varConstMap, concatConstMap, varEqConcatMap, groundedMap); + get_grounded_concats(arg1DeAlias, varAliasMap, concatAliasMap, varConstMap, concatConstMap, varEqConcatMap, groundedMap); + + std::map, std::set >::iterator arg0_grdItor = groundedMap[arg0DeAlias].begin(); + std::map, std::set >::iterator arg1_grdItor; + for (; arg0_grdItor != groundedMap[arg0DeAlias].end(); arg0_grdItor++) { + arg1_grdItor = groundedMap[arg1DeAlias].begin(); + for (; arg1_grdItor != groundedMap[arg1DeAlias].end(); arg1_grdItor++) { + std::vector ndVec; + ndVec.insert(ndVec.end(), arg0_grdItor->first.begin(), arg0_grdItor->first.end()); + int arg0VecSize = arg0_grdItor->first.size(); + int arg1VecSize = arg1_grdItor->first.size(); + if (arg0VecSize > 0 && arg1VecSize > 0 && u.str.is_string(arg0_grdItor->first[arg0VecSize - 1]) && u.str.is_string(arg1_grdItor->first[0])) { + ndVec.pop_back(); + ndVec.push_back(mk_concat(arg0_grdItor->first[arg0VecSize - 1], arg1_grdItor->first[0])); + for (int i = 1; i < arg1VecSize; i++) { + ndVec.push_back(arg1_grdItor->first[i]); + } + } else { + ndVec.insert(ndVec.end(), arg1_grdItor->first.begin(), arg1_grdItor->first.end()); + } + // only insert if we don't know "node = concat(ndVec)" since one set of condition leads to this is enough + if (groundedMap[node].find(ndVec) == groundedMap[node].end()) { + groundedMap[node][ndVec]; + if (arg0 != arg0DeAlias) { + groundedMap[node][ndVec].insert(ctx.mk_eq_atom(arg0, arg0DeAlias)); + } + groundedMap[node][ndVec].insert(arg0_grdItor->second.begin(), arg0_grdItor->second.end()); + + if (arg1 != arg1DeAlias) { + groundedMap[node][ndVec].insert(ctx.mk_eq_atom(arg1, arg1DeAlias)); + } + groundedMap[node][ndVec].insert(arg1_grdItor->second.begin(), arg1_grdItor->second.end()); + } + } + } + } + } + // string variables + else if (variable_set.find(node) != variable_set.end()) { + // deAliasedVar = Constant + if (varConstMap.find(node) != varConstMap.end()) { + std::vector concatNodes; + concatNodes.push_back(varConstMap[node]); + groundedMap[node][concatNodes].clear(); + groundedMap[node][concatNodes].insert(ctx.mk_eq_atom(node, varConstMap[node])); + } + // deAliasedVar = someConcat + else if (varEqConcatMap.find(node) != varEqConcatMap.end()) { + expr * eqConcat = varEqConcatMap[node].begin()->first; + expr * deAliasedEqConcat = dealias_node(eqConcat, varAliasMap, concatAliasMap); + get_grounded_concats(deAliasedEqConcat, varAliasMap, concatAliasMap, varConstMap, concatConstMap, varEqConcatMap, groundedMap); + + std::map, std::set >::iterator grdItor = groundedMap[deAliasedEqConcat].begin(); + for (; grdItor != groundedMap[deAliasedEqConcat].end(); grdItor++) { + std::vector ndVec; + ndVec.insert(ndVec.end(), grdItor->first.begin(), grdItor->first.end()); + // only insert if we don't know "node = concat(ndVec)" since one set of condition leads to this is enough + if (groundedMap[node].find(ndVec) == groundedMap[node].end()) { + // condition: node = deAliasedEqConcat + groundedMap[node][ndVec].insert(ctx.mk_eq_atom(node, deAliasedEqConcat)); + // appending conditions for "deAliasedEqConcat = CONCAT(ndVec)" + groundedMap[node][ndVec].insert(grdItor->second.begin(), grdItor->second.end()); + } + } + } + // node (has been de-aliased) != constant && node (has been de-aliased) != any concat + // just push in the deAliasedVar + else { + std::vector concatNodes; + concatNodes.push_back(node); + groundedMap[node][concatNodes]; + } } } -} -void theory_str::print_grounded_concat(expr * node, std::map, std::set > > & groundedMap) { - ast_manager & m = get_manager(); - TRACE("str", tout << mk_pp(node, m) << std::endl;); - if (groundedMap.find(node) != groundedMap.end()) { - std::map, std::set >::iterator itor = groundedMap[node].begin(); - for (; itor != groundedMap[node].end(); ++itor) { - TRACE("str", - tout << "\t[grounded] "; - std::vector::const_iterator vIt = itor->first.begin(); - for (; vIt != itor->first.end(); ++vIt) { - tout << mk_pp(*vIt, m) << ", "; - } - tout << std::endl; - tout << "\t[condition] "; - std::set::iterator sIt = itor->second.begin(); - for (; sIt != itor->second.end(); sIt++) { - tout << mk_pp(*sIt, m) << ", "; - } - tout << std::endl; - ); + void theory_str::print_grounded_concat(expr * node, std::map, std::set > > & groundedMap) { + ast_manager & m = get_manager(); + TRACE("str", tout << mk_pp(node, m) << std::endl;); + if (groundedMap.find(node) != groundedMap.end()) { + std::map, std::set >::iterator itor = groundedMap[node].begin(); + for (; itor != groundedMap[node].end(); ++itor) { + TRACE("str", + tout << "\t[grounded] "; + std::vector::const_iterator vIt = itor->first.begin(); + for (; vIt != itor->first.end(); ++vIt) { + tout << mk_pp(*vIt, m) << ", "; + } + tout << std::endl; + tout << "\t[condition] "; + std::set::iterator sIt = itor->second.begin(); + for (; sIt != itor->second.end(); sIt++) { + tout << mk_pp(*sIt, m) << ", "; + } + tout << std::endl; + ); + } + } else { + TRACE("str", tout << "not found" << std::endl;); } - } else { - TRACE("str", tout << "not found" << std::endl;); - } -} - -bool theory_str::is_partial_in_grounded_concat(const std::vector & strVec, const std::vector & subStrVec) { - int strCnt = strVec.size(); - int subStrCnt = subStrVec.size(); - - if (strCnt == 0 || subStrCnt == 0) { - return false; } - // The assumption is that all consecutive constant strings are merged into one node - if (strCnt < subStrCnt) { - return false; - } + bool theory_str::is_partial_in_grounded_concat(const std::vector & strVec, const std::vector & subStrVec) { + int strCnt = strVec.size(); + int subStrCnt = subStrVec.size(); - if (subStrCnt == 1) { - zstring subStrVal; - if (u.str.is_string(subStrVec[0], subStrVal)) { - for (int i = 0; i < strCnt; i++) { - zstring strVal; - if (u.str.is_string(strVec[i], strVal)) { - if (strVal.contains(subStrVal)) { + if (strCnt == 0 || subStrCnt == 0) { + return false; + } + + // The assumption is that all consecutive constant strings are merged into one node + if (strCnt < subStrCnt) { + return false; + } + + if (subStrCnt == 1) { + zstring subStrVal; + if (u.str.is_string(subStrVec[0], subStrVal)) { + for (int i = 0; i < strCnt; i++) { + zstring strVal; + if (u.str.is_string(strVec[i], strVal)) { + if (strVal.contains(subStrVal)) { + return true; + } + } + } + } else { + for (int i = 0; i < strCnt; i++) { + if (strVec[i] == subStrVec[0]) { return true; } } } + return false; } else { - for (int i = 0; i < strCnt; i++) { - if (strVec[i] == subStrVec[0]) { - return true; - } - } - } - return false; - } else { - for (int i = 0; i <= (strCnt - subStrCnt); i++) { - // The first node in subStrVect should be - // * constant: a suffix of a note in strVec[i] - // * variable: - bool firstNodesOK = true; - zstring subStrHeadVal; - if (u.str.is_string(subStrVec[0], subStrHeadVal)) { - zstring strHeadVal; - if (u.str.is_string(strVec[i], strHeadVal)) { - if (strHeadVal.length() >= subStrHeadVal.length()) { - zstring suffix = strHeadVal.extract(strHeadVal.length() - subStrHeadVal.length(), subStrHeadVal.length()); - if (suffix != subStrHeadVal) { + for (int i = 0; i <= (strCnt - subStrCnt); i++) { + // The first node in subStrVect should be + // * constant: a suffix of a note in strVec[i] + // * variable: + bool firstNodesOK = true; + zstring subStrHeadVal; + if (u.str.is_string(subStrVec[0], subStrHeadVal)) { + zstring strHeadVal; + if (u.str.is_string(strVec[i], strHeadVal)) { + if (strHeadVal.length() >= subStrHeadVal.length()) { + zstring suffix = strHeadVal.extract(strHeadVal.length() - subStrHeadVal.length(), subStrHeadVal.length()); + if (suffix != subStrHeadVal) { + firstNodesOK = false; + } + } else { firstNodesOK = false; } } else { - firstNodesOK = false; - } - } else { - if (subStrVec[0] != strVec[i]) { - firstNodesOK = false; + if (subStrVec[0] != strVec[i]) { + firstNodesOK = false; + } } } - } - if (!firstNodesOK) { - continue; - } - - // middle nodes - bool midNodesOK = true; - for (int j = 1; j < subStrCnt - 1; j++) { - if (subStrVec[j] != strVec[i + j]) { - midNodesOK = false; - break; + if (!firstNodesOK) { + continue; } - } - if (!midNodesOK) { - continue; - } - // tail nodes - int tailIdx = i + subStrCnt - 1; - zstring subStrTailVal; - if (u.str.is_string(subStrVec[subStrCnt - 1], subStrTailVal)) { - zstring strTailVal; - if (u.str.is_string(strVec[tailIdx], strTailVal)) { - if (strTailVal.length() >= subStrTailVal.length()) { - zstring prefix = strTailVal.extract(0, subStrTailVal.length()); - if (prefix == subStrTailVal) { - return true; + // middle nodes + bool midNodesOK = true; + for (int j = 1; j < subStrCnt - 1; j++) { + if (subStrVec[j] != strVec[i + j]) { + midNodesOK = false; + break; + } + } + if (!midNodesOK) { + continue; + } + + // tail nodes + int tailIdx = i + subStrCnt - 1; + zstring subStrTailVal; + if (u.str.is_string(subStrVec[subStrCnt - 1], subStrTailVal)) { + zstring strTailVal; + if (u.str.is_string(strVec[tailIdx], strTailVal)) { + if (strTailVal.length() >= subStrTailVal.length()) { + zstring prefix = strTailVal.extract(0, subStrTailVal.length()); + if (prefix == subStrTailVal) { + return true; + } else { + continue; + } } else { continue; } + } + } else { + if (subStrVec[subStrCnt - 1] == strVec[tailIdx]) { + return true; } else { continue; } } - } else { - if (subStrVec[subStrCnt - 1] == strVec[tailIdx]) { - return true; - } else { - continue; - } } - } - return false; - } -} - -void theory_str::check_subsequence(expr* str, expr* strDeAlias, expr* subStr, expr* subStrDeAlias, expr* boolVar, - std::map, std::set > > & groundedMap) { - - context & ctx = get_context(); - ast_manager & m = get_manager(); - std::map, std::set >::iterator itorStr = groundedMap[strDeAlias].begin(); - std::map, std::set >::iterator itorSubStr; - for (; itorStr != groundedMap[strDeAlias].end(); itorStr++) { - itorSubStr = groundedMap[subStrDeAlias].begin(); - for (; itorSubStr != groundedMap[subStrDeAlias].end(); itorSubStr++) { - bool contain = is_partial_in_grounded_concat(itorStr->first, itorSubStr->first); - if (contain) { - expr_ref_vector litems(m); - if (str != strDeAlias) { - litems.push_back(ctx.mk_eq_atom(str, strDeAlias)); - } - if (subStr != subStrDeAlias) { - litems.push_back(ctx.mk_eq_atom(subStr, subStrDeAlias)); - } - - //litems.insert(itorStr->second.begin(), itorStr->second.end()); - //litems.insert(itorSubStr->second.begin(), itorSubStr->second.end()); - for (std::set::const_iterator i1 = itorStr->second.begin(); - i1 != itorStr->second.end(); ++i1) { - litems.push_back(*i1); - } - for (std::set::const_iterator i1 = itorSubStr->second.begin(); - i1 != itorSubStr->second.end(); ++i1) { - litems.push_back(*i1); - } - - expr_ref implyR(boolVar, m); - - if (litems.empty()) { - assert_axiom(implyR); - } else { - expr_ref implyL(mk_and(litems), m); - assert_implication(implyL, implyR); - } - - } - } - } -} - -void theory_str::compute_contains(std::map & varAliasMap, - std::map & concatAliasMap, std::map & varConstMap, - std::map & concatConstMap, std::map > & varEqConcatMap) { - std::map, std::set > > groundedMap; - theory_str_contain_pair_bool_map_t::iterator containItor = contain_pair_bool_map.begin(); - for (; containItor != contain_pair_bool_map.end(); containItor++) { - expr* containBoolVar = containItor->get_value(); - expr* str = containItor->get_key1(); - expr* subStr = containItor->get_key2(); - - expr* strDeAlias = dealias_node(str, varAliasMap, concatAliasMap); - expr* subStrDeAlias = dealias_node(subStr, varAliasMap, concatAliasMap); - - get_grounded_concats(strDeAlias, varAliasMap, concatAliasMap, varConstMap, concatConstMap, varEqConcatMap, groundedMap); - get_grounded_concats(subStrDeAlias, varAliasMap, concatAliasMap, varConstMap, concatConstMap, varEqConcatMap, groundedMap); - - // debugging - print_grounded_concat(strDeAlias, groundedMap); - print_grounded_concat(subStrDeAlias, groundedMap); - - check_subsequence(str, strDeAlias, subStr, subStrDeAlias, containBoolVar, groundedMap); - } -} - -bool theory_str::can_concat_eq_str(expr * concat, zstring& str) { - unsigned int strLen = str.length(); - if (u.str.is_concat(to_app(concat))) { - ptr_vector args; - get_nodes_in_concat(concat, args); - expr * ml_node = args[0]; - expr * mr_node = args[args.size() - 1]; - - zstring ml_str; - if (u.str.is_string(ml_node, ml_str)) { - unsigned int ml_len = ml_str.length(); - if (ml_len > strLen) { - return false; - } - unsigned int cLen = ml_len; - if (ml_str != str.extract(0, cLen)) { - return false; - } - } - - zstring mr_str; - if (u.str.is_string(mr_node, mr_str)) { - unsigned int mr_len = mr_str.length(); - if (mr_len > strLen) { - return false; - } - unsigned int cLen = mr_len; - if (mr_str != str.extract(strLen - cLen, cLen)) { - return false; - } - } - - unsigned int sumLen = 0; - for (unsigned int i = 0 ; i < args.size() ; i++) { - expr * oneArg = args[i]; - zstring arg_str; - if (u.str.is_string(oneArg, arg_str)) { - if (!str.contains(arg_str)) { - return false; - } - sumLen += arg_str.length(); - } - } - - if (sumLen > strLen) { - return false; - } - } - return true; -} - -bool theory_str::can_concat_eq_concat(expr * concat1, expr * concat2) { - if (u.str.is_concat(to_app(concat1)) && u.str.is_concat(to_app(concat2))) { - { - // Suppose concat1 = (Concat X Y) and concat2 = (Concat M N). - expr * concat1_mostL = getMostLeftNodeInConcat(concat1); - expr * concat2_mostL = getMostLeftNodeInConcat(concat2); - // if both X and M are constant strings, check whether they have the same prefix - zstring concat1_mostL_str, concat2_mostL_str; - if (u.str.is_string(concat1_mostL, concat1_mostL_str) && u.str.is_string(concat2_mostL, concat2_mostL_str)) { - unsigned int cLen = std::min(concat1_mostL_str.length(), concat2_mostL_str.length()); - if (concat1_mostL_str.extract(0, cLen) != concat2_mostL_str.extract(0, cLen)) { - return false; - } - } - } - - { - // Similarly, if both Y and N are constant strings, check whether they have the same suffix - expr * concat1_mostR = getMostRightNodeInConcat(concat1); - expr * concat2_mostR = getMostRightNodeInConcat(concat2); - zstring concat1_mostR_str, concat2_mostR_str; - if (u.str.is_string(concat1_mostR, concat1_mostR_str) && u.str.is_string(concat2_mostR, concat2_mostR_str)) { - unsigned int cLen = std::min(concat1_mostR_str.length(), concat2_mostR_str.length()); - if (concat1_mostR_str.extract(concat1_mostR_str.length() - cLen, cLen) != - concat2_mostR_str.extract(concat2_mostR_str.length() - cLen, cLen)) { - return false; - } - } - } - } - return true; -} - -/* - * Check whether n1 and n2 could be equal. - * Returns true if n1 could equal n2 (maybe), - * and false if n1 is definitely not equal to n2 (no). - */ -bool theory_str::can_two_nodes_eq(expr * n1, expr * n2) { - app * n1_curr = to_app(n1); - app * n2_curr = to_app(n2); - - // case 0: n1_curr is const string, n2_curr is const string - if (u.str.is_string(n1_curr) && u.str.is_string(n2_curr)) { - if (n1_curr != n2_curr) { - return false; - } - } - // case 1: n1_curr is concat, n2_curr is const string - else if (u.str.is_concat(n1_curr) && u.str.is_string(n2_curr)) { - zstring n2_curr_str; - u.str.is_string(n2_curr, n2_curr_str); - if (!can_concat_eq_str(n1_curr, n2_curr_str)) { return false; } } - // case 2: n2_curr is concat, n1_curr is const string - else if (u.str.is_concat(n2_curr) && u.str.is_string(n1_curr)) { - zstring n1_curr_str; - u.str.is_string(n1_curr, n1_curr_str); - if (!can_concat_eq_str(n2_curr, n1_curr_str)) { - return false; + + void theory_str::check_subsequence(expr* str, expr* strDeAlias, expr* subStr, expr* subStrDeAlias, expr* boolVar, + std::map, std::set > > & groundedMap) { + + context & ctx = get_context(); + ast_manager & m = get_manager(); + std::map, std::set >::iterator itorStr = groundedMap[strDeAlias].begin(); + std::map, std::set >::iterator itorSubStr; + for (; itorStr != groundedMap[strDeAlias].end(); itorStr++) { + itorSubStr = groundedMap[subStrDeAlias].begin(); + for (; itorSubStr != groundedMap[subStrDeAlias].end(); itorSubStr++) { + bool contain = is_partial_in_grounded_concat(itorStr->first, itorSubStr->first); + if (contain) { + expr_ref_vector litems(m); + if (str != strDeAlias) { + litems.push_back(ctx.mk_eq_atom(str, strDeAlias)); + } + if (subStr != subStrDeAlias) { + litems.push_back(ctx.mk_eq_atom(subStr, subStrDeAlias)); + } + + //litems.insert(itorStr->second.begin(), itorStr->second.end()); + //litems.insert(itorSubStr->second.begin(), itorSubStr->second.end()); + for (std::set::const_iterator i1 = itorStr->second.begin(); + i1 != itorStr->second.end(); ++i1) { + litems.push_back(*i1); + } + for (std::set::const_iterator i1 = itorSubStr->second.begin(); + i1 != itorSubStr->second.end(); ++i1) { + litems.push_back(*i1); + } + + expr_ref implyR(boolVar, m); + + if (litems.empty()) { + assert_axiom(implyR); + } else { + expr_ref implyL(mk_and(litems), m); + assert_implication(implyL, implyR); + } + + } + } } } - // case 3: both are concats - else if (u.str.is_concat(n1_curr) && u.str.is_concat(n2_curr)) { - if (!can_concat_eq_concat(n1_curr, n2_curr)) { - return false; - } + + void theory_str::compute_contains(std::map & varAliasMap, + std::map & concatAliasMap, std::map & varConstMap, + std::map & concatConstMap, std::map > & varEqConcatMap) { + std::map, std::set > > groundedMap; + theory_str_contain_pair_bool_map_t::iterator containItor = contain_pair_bool_map.begin(); + for (; containItor != contain_pair_bool_map.end(); containItor++) { + expr* containBoolVar = containItor->get_value(); + expr* str = containItor->get_key1(); + expr* subStr = containItor->get_key2(); + + expr* strDeAlias = dealias_node(str, varAliasMap, concatAliasMap); + expr* subStrDeAlias = dealias_node(subStr, varAliasMap, concatAliasMap); + + get_grounded_concats(strDeAlias, varAliasMap, concatAliasMap, varConstMap, concatConstMap, varEqConcatMap, groundedMap); + get_grounded_concats(subStrDeAlias, varAliasMap, concatAliasMap, varConstMap, concatConstMap, varEqConcatMap, groundedMap); + + // debugging + print_grounded_concat(strDeAlias, groundedMap); + print_grounded_concat(subStrDeAlias, groundedMap); + + check_subsequence(str, strDeAlias, subStr, subStrDeAlias, containBoolVar, groundedMap); + } } - return true; -} + bool theory_str::can_concat_eq_str(expr * concat, zstring& str) { + unsigned int strLen = str.length(); + if (u.str.is_concat(to_app(concat))) { + ptr_vector args; + get_nodes_in_concat(concat, args); + expr * ml_node = args[0]; + expr * mr_node = args[args.size() - 1]; -// was checkLength2ConstStr() in Z3str2 -// returns true if everything is OK, or false if inconsistency detected -// - note that these are different from the semantics in Z3str2 -bool theory_str::check_length_const_string(expr * n1, expr * constStr) { - ast_manager & mgr = get_manager(); - context & ctx = get_context(); - - zstring tmp; - u.str.is_string(constStr, tmp); - rational strLen(tmp.length()); - - if (u.str.is_concat(to_app(n1))) { - ptr_vector args; - expr_ref_vector items(mgr); - - get_nodes_in_concat(n1, args); - - rational sumLen(0); - for (unsigned int i = 0; i < args.size(); ++i) { - rational argLen; - bool argLen_exists = get_len_value(args[i], argLen); - if (argLen_exists) { - if (!u.str.is_string(args[i])) { - items.push_back(ctx.mk_eq_atom(mk_strlen(args[i]), mk_int(argLen))); + zstring ml_str; + if (u.str.is_string(ml_node, ml_str)) { + unsigned int ml_len = ml_str.length(); + if (ml_len > strLen) { + return false; } - TRACE("str", tout << "concat arg: " << mk_pp(args[i], mgr) << " has len = " << argLen.to_string() << std::endl;); - sumLen += argLen; - if (sumLen > strLen) { - items.push_back(ctx.mk_eq_atom(n1, constStr)); - expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); - TRACE("str", tout << "inconsistent length: concat (len = " << sumLen << ") <==> string constant (len = " << strLen << ")" << std::endl;); - assert_axiom(toAssert); + unsigned int cLen = ml_len; + if (ml_str != str.extract(0, cLen)) { return false; } } + + zstring mr_str; + if (u.str.is_string(mr_node, mr_str)) { + unsigned int mr_len = mr_str.length(); + if (mr_len > strLen) { + return false; + } + unsigned int cLen = mr_len; + if (mr_str != str.extract(strLen - cLen, cLen)) { + return false; + } + } + + unsigned int sumLen = 0; + for (unsigned int i = 0 ; i < args.size() ; i++) { + expr * oneArg = args[i]; + zstring arg_str; + if (u.str.is_string(oneArg, arg_str)) { + if (!str.contains(arg_str)) { + return false; + } + sumLen += arg_str.length(); + } + } + + if (sumLen > strLen) { + return false; + } } - } else { // !is_concat(n1) - rational oLen; - bool oLen_exists = get_len_value(n1, oLen); - if (oLen_exists && oLen != strLen) { - TRACE("str", tout << "inconsistent length: var (len = " << oLen << ") <==> string constant (len = " << strLen << ")" << std::endl;); + return true; + } + + bool theory_str::can_concat_eq_concat(expr * concat1, expr * concat2) { + if (u.str.is_concat(to_app(concat1)) && u.str.is_concat(to_app(concat2))) { + { + // Suppose concat1 = (Concat X Y) and concat2 = (Concat M N). + expr * concat1_mostL = getMostLeftNodeInConcat(concat1); + expr * concat2_mostL = getMostLeftNodeInConcat(concat2); + // if both X and M are constant strings, check whether they have the same prefix + zstring concat1_mostL_str, concat2_mostL_str; + if (u.str.is_string(concat1_mostL, concat1_mostL_str) && u.str.is_string(concat2_mostL, concat2_mostL_str)) { + unsigned int cLen = std::min(concat1_mostL_str.length(), concat2_mostL_str.length()); + if (concat1_mostL_str.extract(0, cLen) != concat2_mostL_str.extract(0, cLen)) { + return false; + } + } + } + + { + // Similarly, if both Y and N are constant strings, check whether they have the same suffix + expr * concat1_mostR = getMostRightNodeInConcat(concat1); + expr * concat2_mostR = getMostRightNodeInConcat(concat2); + zstring concat1_mostR_str, concat2_mostR_str; + if (u.str.is_string(concat1_mostR, concat1_mostR_str) && u.str.is_string(concat2_mostR, concat2_mostR_str)) { + unsigned int cLen = std::min(concat1_mostR_str.length(), concat2_mostR_str.length()); + if (concat1_mostR_str.extract(concat1_mostR_str.length() - cLen, cLen) != + concat2_mostR_str.extract(concat2_mostR_str.length() - cLen, cLen)) { + return false; + } + } + } + } + return true; + } + + /* + * Check whether n1 and n2 could be equal. + * Returns true if n1 could equal n2 (maybe), + * and false if n1 is definitely not equal to n2 (no). + */ + bool theory_str::can_two_nodes_eq(expr * n1, expr * n2) { + app * n1_curr = to_app(n1); + app * n2_curr = to_app(n2); + + // case 0: n1_curr is const string, n2_curr is const string + if (u.str.is_string(n1_curr) && u.str.is_string(n2_curr)) { + if (n1_curr != n2_curr) { + return false; + } + } + // case 1: n1_curr is concat, n2_curr is const string + else if (u.str.is_concat(n1_curr) && u.str.is_string(n2_curr)) { + zstring n2_curr_str; + u.str.is_string(n2_curr, n2_curr_str); + if (!can_concat_eq_str(n1_curr, n2_curr_str)) { + return false; + } + } + // case 2: n2_curr is concat, n1_curr is const string + else if (u.str.is_concat(n2_curr) && u.str.is_string(n1_curr)) { + zstring n1_curr_str; + u.str.is_string(n1_curr, n1_curr_str); + if (!can_concat_eq_str(n2_curr, n1_curr_str)) { + return false; + } + } + // case 3: both are concats + else if (u.str.is_concat(n1_curr) && u.str.is_concat(n2_curr)) { + if (!can_concat_eq_concat(n1_curr, n2_curr)) { + return false; + } + } + + return true; + } + + // was checkLength2ConstStr() in Z3str2 + // returns true if everything is OK, or false if inconsistency detected + // - note that these are different from the semantics in Z3str2 + bool theory_str::check_length_const_string(expr * n1, expr * constStr) { + ast_manager & mgr = get_manager(); + context & ctx = get_context(); + + zstring tmp; + u.str.is_string(constStr, tmp); + rational strLen(tmp.length()); + + if (u.str.is_concat(to_app(n1))) { + ptr_vector args; + expr_ref_vector items(mgr); + + get_nodes_in_concat(n1, args); + + rational sumLen(0); + for (unsigned int i = 0; i < args.size(); ++i) { + rational argLen; + bool argLen_exists = get_len_value(args[i], argLen); + if (argLen_exists) { + if (!u.str.is_string(args[i])) { + items.push_back(ctx.mk_eq_atom(mk_strlen(args[i]), mk_int(argLen))); + } + TRACE("str", tout << "concat arg: " << mk_pp(args[i], mgr) << " has len = " << argLen.to_string() << std::endl;); + sumLen += argLen; + if (sumLen > strLen) { + items.push_back(ctx.mk_eq_atom(n1, constStr)); + expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); + TRACE("str", tout << "inconsistent length: concat (len = " << sumLen << ") <==> string constant (len = " << strLen << ")" << std::endl;); + assert_axiom(toAssert); + return false; + } + } + } + } else { // !is_concat(n1) + rational oLen; + bool oLen_exists = get_len_value(n1, oLen); + if (oLen_exists && oLen != strLen) { + TRACE("str", tout << "inconsistent length: var (len = " << oLen << ") <==> string constant (len = " << strLen << ")" << std::endl;); + expr_ref l(ctx.mk_eq_atom(n1, constStr), mgr); + expr_ref r(ctx.mk_eq_atom(mk_strlen(n1), mk_strlen(constStr)), mgr); + assert_implication(l, r); + return false; + } + } + rational unused; + if (get_len_value(n1, unused) == false) { expr_ref l(ctx.mk_eq_atom(n1, constStr), mgr); expr_ref r(ctx.mk_eq_atom(mk_strlen(n1), mk_strlen(constStr)), mgr); assert_implication(l, r); - return false; } - } - rational unused; - if (get_len_value(n1, unused) == false) { - expr_ref l(ctx.mk_eq_atom(n1, constStr), mgr); - expr_ref r(ctx.mk_eq_atom(mk_strlen(n1), mk_strlen(constStr)), mgr); - assert_implication(l, r); - } - return true; -} - -bool theory_str::check_length_concat_concat(expr * n1, expr * n2) { - context & ctx = get_context(); - ast_manager & mgr = get_manager(); - - ptr_vector concat1Args; - ptr_vector concat2Args; - get_nodes_in_concat(n1, concat1Args); - get_nodes_in_concat(n2, concat2Args); - - bool concat1LenFixed = true; - bool concat2LenFixed = true; - - expr_ref_vector items(mgr); - - rational sum1(0), sum2(0); - - for (unsigned int i = 0; i < concat1Args.size(); ++i) { - expr * oneArg = concat1Args[i]; - rational argLen; - bool argLen_exists = get_len_value(oneArg, argLen); - if (argLen_exists) { - sum1 += argLen; - if (!u.str.is_string(oneArg)) { - items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); - } - } else { - concat1LenFixed = false; - } - } - - for (unsigned int i = 0; i < concat2Args.size(); ++i) { - expr * oneArg = concat2Args[i]; - rational argLen; - bool argLen_exists = get_len_value(oneArg, argLen); - if (argLen_exists) { - sum2 += argLen; - if (!u.str.is_string(oneArg)) { - items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); - } - } else { - concat2LenFixed = false; - } - } - - items.push_back(ctx.mk_eq_atom(n1, n2)); - - bool conflict = false; - - if (concat1LenFixed && concat2LenFixed) { - if (sum1 != sum2) { - conflict = true; - } - } else if (!concat1LenFixed && concat2LenFixed) { - if (sum1 > sum2) { - conflict = true; - } - } else if (concat1LenFixed && !concat2LenFixed) { - if (sum1 < sum2) { - conflict = true; - } - } - - if (conflict) { - TRACE("str", tout << "inconsistent length detected in concat <==> concat" << std::endl;); - expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); - assert_axiom(toAssert); - return false; - } - return true; -} - -bool theory_str::check_length_concat_var(expr * concat, expr * var) { - context & ctx = get_context(); - ast_manager & mgr = get_manager(); - - rational varLen; - bool varLen_exists = get_len_value(var, varLen); - if (!varLen_exists) { return true; - } else { - rational sumLen(0); - ptr_vector args; + } + + bool theory_str::check_length_concat_concat(expr * n1, expr * n2) { + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + + ptr_vector concat1Args; + ptr_vector concat2Args; + get_nodes_in_concat(n1, concat1Args); + get_nodes_in_concat(n2, concat2Args); + + bool concat1LenFixed = true; + bool concat2LenFixed = true; + expr_ref_vector items(mgr); - get_nodes_in_concat(concat, args); - for (unsigned int i = 0; i < args.size(); ++i) { - expr * oneArg = args[i]; + + rational sum1(0), sum2(0); + + for (unsigned int i = 0; i < concat1Args.size(); ++i) { + expr * oneArg = concat1Args[i]; rational argLen; bool argLen_exists = get_len_value(oneArg, argLen); if (argLen_exists) { - if (!u.str.is_string(oneArg) && !argLen.is_zero()) { + sum1 += argLen; + if (!u.str.is_string(oneArg)) { items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); } - sumLen += argLen; - if (sumLen > varLen) { - TRACE("str", tout << "inconsistent length detected in concat <==> var" << std::endl;); - items.push_back(ctx.mk_eq_atom(mk_strlen(var), mk_int(varLen))); - items.push_back(ctx.mk_eq_atom(concat, var)); - expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); - assert_axiom(toAssert); - return false; - } + } else { + concat1LenFixed = false; } } + + for (unsigned int i = 0; i < concat2Args.size(); ++i) { + expr * oneArg = concat2Args[i]; + rational argLen; + bool argLen_exists = get_len_value(oneArg, argLen); + if (argLen_exists) { + sum2 += argLen; + if (!u.str.is_string(oneArg)) { + items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); + } + } else { + concat2LenFixed = false; + } + } + + items.push_back(ctx.mk_eq_atom(n1, n2)); + + bool conflict = false; + + if (concat1LenFixed && concat2LenFixed) { + if (sum1 != sum2) { + conflict = true; + } + } else if (!concat1LenFixed && concat2LenFixed) { + if (sum1 > sum2) { + conflict = true; + } + } else if (concat1LenFixed && !concat2LenFixed) { + if (sum1 < sum2) { + conflict = true; + } + } + + if (conflict) { + TRACE("str", tout << "inconsistent length detected in concat <==> concat" << std::endl;); + expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); + assert_axiom(toAssert); + return false; + } return true; } -} -bool theory_str::check_length_var_var(expr * var1, expr * var2) { - context & ctx = get_context(); - ast_manager & mgr = get_manager(); + bool theory_str::check_length_concat_var(expr * concat, expr * var) { + context & ctx = get_context(); + ast_manager & mgr = get_manager(); - rational var1Len, var2Len; - bool var1Len_exists = get_len_value(var1, var1Len); - bool var2Len_exists = get_len_value(var2, var2Len); - - if (var1Len_exists && var2Len_exists && var1Len != var2Len) { - TRACE("str", tout << "inconsistent length detected in var <==> var" << std::endl;); - expr_ref_vector items(mgr); - items.push_back(ctx.mk_eq_atom(mk_strlen(var1), mk_int(var1Len))); - items.push_back(ctx.mk_eq_atom(mk_strlen(var2), mk_int(var2Len))); - items.push_back(ctx.mk_eq_atom(var1, var2)); - expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); - assert_axiom(toAssert); - return false; - } - return true; -} - -// returns true if everything is OK, or false if inconsistency detected -// - note that these are different from the semantics in Z3str2 -bool theory_str::check_length_eq_var_concat(expr * n1, expr * n2) { - // n1 and n2 are not const string: either variable or concat - bool n1Concat = u.str.is_concat(to_app(n1)); - bool n2Concat = u.str.is_concat(to_app(n2)); - if (n1Concat && n2Concat) { - return check_length_concat_concat(n1, n2); - } - // n1 is concat, n2 is variable - else if (n1Concat && (!n2Concat)) { - return check_length_concat_var(n1, n2); - } - // n1 is variable, n2 is concat - else if ((!n1Concat) && n2Concat) { - return check_length_concat_var(n2, n1); - } - // n1 and n2 are both variables - else { - return check_length_var_var(n1, n2); - } - return true; -} - -// returns false if an inconsistency is detected, or true if no inconsistencies were found -// - note that these are different from the semantics of checkLengConsistency() in Z3str2 -bool theory_str::check_length_consistency(expr * n1, expr * n2) { - if (u.str.is_string(n1) && u.str.is_string(n2)) { - // consistency has already been checked in can_two_nodes_eq(). - return true; - } else if (u.str.is_string(n1) && (!u.str.is_string(n2))) { - return check_length_const_string(n2, n1); - } else if (u.str.is_string(n2) && (!u.str.is_string(n1))) { - return check_length_const_string(n1, n2); - } else { - // n1 and n2 are vars or concats - return check_length_eq_var_concat(n1, n2); - } - return true; -} - -// Modified signature: returns true if nothing was learned, or false if at least one axiom was asserted. -// (This is used for deferred consistency checking) -bool theory_str::check_concat_len_in_eqc(expr * concat) { - context & ctx = get_context(); - - bool no_assertions = true; - - expr * eqc_n = concat; - do { - if (u.str.is_concat(to_app(eqc_n))) { - rational unused; - bool status = infer_len_concat(eqc_n, unused); - if (status) { - no_assertions = false; - } - } - eqc_n = get_eqc_next(eqc_n); - } while (eqc_n != concat); - - return no_assertions; -} - -// Convert a regular expression to an e-NFA using Thompson's construction -void nfa::convert_re(expr * e, unsigned & start, unsigned & end, seq_util & u) { - start = next_id(); - end = next_id(); - if (u.re.is_to_re(e)) { - app * a = to_app(e); - expr * arg_str = a->get_arg(0); - zstring str; - if (u.str.is_string(arg_str, str)) { - TRACE("str", tout << "build NFA for '" << str << "'" << "\n";); - /* - * For an n-character string, we make (n-1) intermediate states, - * labelled i_(0) through i_(n-2). - * Then we construct the following transitions: - * start --str[0]--> i_(0) --str[1]--> i_(1) --...--> i_(n-2) --str[n-1]--> final - */ - unsigned last = start; - for (int i = 0; i <= ((int)str.length()) - 2; ++i) { - unsigned i_state = next_id(); - make_transition(last, str[i], i_state); - TRACE("str", tout << "string transition " << last << "--" << str[i] << "--> " << i_state << "\n";); - last = i_state; - } - make_transition(last, str[(str.length() - 1)], end); - TRACE("str", tout << "string transition " << last << "--" << str[(str.length() - 1)] << "--> " << end << "\n";); + rational varLen; + bool varLen_exists = get_len_value(var, varLen); + if (!varLen_exists) { + return true; } else { - TRACE("str", tout << "invalid string constant in Str2Reg" << std::endl;); + rational sumLen(0); + ptr_vector args; + expr_ref_vector items(mgr); + get_nodes_in_concat(concat, args); + for (unsigned int i = 0; i < args.size(); ++i) { + expr * oneArg = args[i]; + rational argLen; + bool argLen_exists = get_len_value(oneArg, argLen); + if (argLen_exists) { + if (!u.str.is_string(oneArg) && !argLen.is_zero()) { + items.push_back(ctx.mk_eq_atom(mk_strlen(oneArg), mk_int(argLen))); + } + sumLen += argLen; + if (sumLen > varLen) { + TRACE("str", tout << "inconsistent length detected in concat <==> var" << std::endl;); + items.push_back(ctx.mk_eq_atom(mk_strlen(var), mk_int(varLen))); + items.push_back(ctx.mk_eq_atom(concat, var)); + expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); + assert_axiom(toAssert); + return false; + } + } + } + return true; + } + } + + bool theory_str::check_length_var_var(expr * var1, expr * var2) { + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + + rational var1Len, var2Len; + bool var1Len_exists = get_len_value(var1, var1Len); + bool var2Len_exists = get_len_value(var2, var2Len); + + if (var1Len_exists && var2Len_exists && var1Len != var2Len) { + TRACE("str", tout << "inconsistent length detected in var <==> var" << std::endl;); + expr_ref_vector items(mgr); + items.push_back(ctx.mk_eq_atom(mk_strlen(var1), mk_int(var1Len))); + items.push_back(ctx.mk_eq_atom(mk_strlen(var2), mk_int(var2Len))); + items.push_back(ctx.mk_eq_atom(var1, var2)); + expr_ref toAssert(mgr.mk_not(mk_and(items)), mgr); + assert_axiom(toAssert); + return false; + } + return true; + } + + // returns true if everything is OK, or false if inconsistency detected + // - note that these are different from the semantics in Z3str2 + bool theory_str::check_length_eq_var_concat(expr * n1, expr * n2) { + // n1 and n2 are not const string: either variable or concat + bool n1Concat = u.str.is_concat(to_app(n1)); + bool n2Concat = u.str.is_concat(to_app(n2)); + if (n1Concat && n2Concat) { + return check_length_concat_concat(n1, n2); + } + // n1 is concat, n2 is variable + else if (n1Concat && (!n2Concat)) { + return check_length_concat_var(n1, n2); + } + // n1 is variable, n2 is concat + else if ((!n1Concat) && n2Concat) { + return check_length_concat_var(n2, n1); + } + // n1 and n2 are both variables + else { + return check_length_var_var(n1, n2); + } + return true; + } + + // returns false if an inconsistency is detected, or true if no inconsistencies were found + // - note that these are different from the semantics of checkLengConsistency() in Z3str2 + bool theory_str::check_length_consistency(expr * n1, expr * n2) { + if (u.str.is_string(n1) && u.str.is_string(n2)) { + // consistency has already been checked in can_two_nodes_eq(). + return true; + } else if (u.str.is_string(n1) && (!u.str.is_string(n2))) { + return check_length_const_string(n2, n1); + } else if (u.str.is_string(n2) && (!u.str.is_string(n1))) { + return check_length_const_string(n1, n2); + } else { + // n1 and n2 are vars or concats + return check_length_eq_var_concat(n1, n2); + } + return true; + } + + // Modified signature: returns true if nothing was learned, or false if at least one axiom was asserted. + // (This is used for deferred consistency checking) + bool theory_str::check_concat_len_in_eqc(expr * concat) { + context & ctx = get_context(); + + bool no_assertions = true; + + expr * eqc_n = concat; + do { + if (u.str.is_concat(to_app(eqc_n))) { + rational unused; + bool status = infer_len_concat(eqc_n, unused); + if (status) { + no_assertions = false; + } + } + eqc_n = get_eqc_next(eqc_n); + } while (eqc_n != concat); + + return no_assertions; + } + + // Convert a regular expression to an e-NFA using Thompson's construction + void nfa::convert_re(expr * e, unsigned & start, unsigned & end, seq_util & u) { + start = next_id(); + end = next_id(); + if (u.re.is_to_re(e)) { + app * a = to_app(e); + expr * arg_str = a->get_arg(0); + zstring str; + if (u.str.is_string(arg_str, str)) { + TRACE("str", tout << "build NFA for '" << str << "'" << "\n";); + /* + * For an n-character string, we make (n-1) intermediate states, + * labelled i_(0) through i_(n-2). + * Then we construct the following transitions: + * start --str[0]--> i_(0) --str[1]--> i_(1) --...--> i_(n-2) --str[n-1]--> final + */ + unsigned last = start; + for (int i = 0; i <= ((int)str.length()) - 2; ++i) { + unsigned i_state = next_id(); + make_transition(last, str[i], i_state); + TRACE("str", tout << "string transition " << last << "--" << str[i] << "--> " << i_state << "\n";); + last = i_state; + } + make_transition(last, str[(str.length() - 1)], end); + TRACE("str", tout << "string transition " << last << "--" << str[(str.length() - 1)] << "--> " << end << "\n";); + } else { + TRACE("str", tout << "invalid string constant in Str2Reg" << std::endl;); + m_valid = false; + return; + } + } else if (u.re.is_concat(e)){ + app * a = to_app(e); + expr * re1 = a->get_arg(0); + expr * re2 = a->get_arg(1); + unsigned start1, end1; + convert_re(re1, start1, end1, u); + unsigned start2, end2; + convert_re(re2, start2, end2, u); + // start --e--> start1 --...--> end1 --e--> start2 --...--> end2 --e--> end + make_epsilon_move(start, start1); + make_epsilon_move(end1, start2); + make_epsilon_move(end2, end); + TRACE("str", tout << "concat NFA: start = " << start << ", end = " << end << std::endl;); + } else if (u.re.is_union(e)) { + app * a = to_app(e); + expr * re1 = a->get_arg(0); + expr * re2 = a->get_arg(1); + unsigned start1, end1; + convert_re(re1, start1, end1, u); + unsigned start2, end2; + convert_re(re2, start2, end2, u); + + // start --e--> start1 ; start --e--> start2 + // end1 --e--> end ; end2 --e--> end + make_epsilon_move(start, start1); + make_epsilon_move(start, start2); + make_epsilon_move(end1, end); + make_epsilon_move(end2, end); + TRACE("str", tout << "union NFA: start = " << start << ", end = " << end << std::endl;); + } else if (u.re.is_star(e)) { + app * a = to_app(e); + expr * subex = a->get_arg(0); + unsigned start_subex, end_subex; + convert_re(subex, start_subex, end_subex, u); + // start --e--> start_subex, start --e--> end + // end_subex --e--> start_subex, end_subex --e--> end + make_epsilon_move(start, start_subex); + make_epsilon_move(start, end); + make_epsilon_move(end_subex, start_subex); + make_epsilon_move(end_subex, end); + TRACE("str", tout << "star NFA: start = " << start << ", end = " << end << std::endl;); + } else if (u.re.is_range(e)) { + // range('a', 'z') + // start --'a'--> end + // start --'b'--> end + // ... + // start --'z'--> end + app * a = to_app(e); + expr * c1 = a->get_arg(0); + expr * c2 = a->get_arg(1); + zstring s_c1, s_c2; + u.str.is_string(c1, s_c1); + u.str.is_string(c2, s_c2); + + unsigned int id1 = s_c1[0]; + unsigned int id2 = s_c2[0]; + if (id1 > id2) { + unsigned int tmp = id1; + id1 = id2; + id2 = tmp; + } + + for (unsigned int i = id1; i <= id2; ++i) { + char ch = (char)i; + make_transition(start, ch, end); + } + + TRACE("str", tout << "range NFA: start = " << start << ", end = " << end << std::endl;); + } else { + TRACE("str", tout << "invalid regular expression" << std::endl;); m_valid = false; return; } - } else if (u.re.is_concat(e)){ - app * a = to_app(e); - expr * re1 = a->get_arg(0); - expr * re2 = a->get_arg(1); - unsigned start1, end1; - convert_re(re1, start1, end1, u); - unsigned start2, end2; - convert_re(re2, start2, end2, u); - // start --e--> start1 --...--> end1 --e--> start2 --...--> end2 --e--> end - make_epsilon_move(start, start1); - make_epsilon_move(end1, start2); - make_epsilon_move(end2, end); - TRACE("str", tout << "concat NFA: start = " << start << ", end = " << end << std::endl;); - } else if (u.re.is_union(e)) { - app * a = to_app(e); - expr * re1 = a->get_arg(0); - expr * re2 = a->get_arg(1); - unsigned start1, end1; - convert_re(re1, start1, end1, u); - unsigned start2, end2; - convert_re(re2, start2, end2, u); - - // start --e--> start1 ; start --e--> start2 - // end1 --e--> end ; end2 --e--> end - make_epsilon_move(start, start1); - make_epsilon_move(start, start2); - make_epsilon_move(end1, end); - make_epsilon_move(end2, end); - TRACE("str", tout << "union NFA: start = " << start << ", end = " << end << std::endl;); - } else if (u.re.is_star(e)) { - app * a = to_app(e); - expr * subex = a->get_arg(0); - unsigned start_subex, end_subex; - convert_re(subex, start_subex, end_subex, u); - // start --e--> start_subex, start --e--> end - // end_subex --e--> start_subex, end_subex --e--> end - make_epsilon_move(start, start_subex); - make_epsilon_move(start, end); - make_epsilon_move(end_subex, start_subex); - make_epsilon_move(end_subex, end); - TRACE("str", tout << "star NFA: start = " << start << ", end = " << end << std::endl;); - } else if (u.re.is_range(e)) { - // range('a', 'z') - // start --'a'--> end - // start --'b'--> end - // ... - // start --'z'--> end - app * a = to_app(e); - expr * c1 = a->get_arg(0); - expr * c2 = a->get_arg(1); - zstring s_c1, s_c2; - u.str.is_string(c1, s_c1); - u.str.is_string(c2, s_c2); - - unsigned int id1 = s_c1[0]; - unsigned int id2 = s_c2[0]; - if (id1 > id2) { - unsigned int tmp = id1; - id1 = id2; - id2 = tmp; - } - - for (unsigned int i = id1; i <= id2; ++i) { - char ch = (char)i; - make_transition(start, ch, end); - } - - TRACE("str", tout << "range NFA: start = " << start << ", end = " << end << std::endl;); - } else { - TRACE("str", tout << "invalid regular expression" << std::endl;); - m_valid = false; - return; } -} -void nfa::epsilon_closure(unsigned start, std::set & closure) { - std::deque worklist; - closure.insert(start); - worklist.push_back(start); + void nfa::epsilon_closure(unsigned start, std::set & closure) { + std::deque worklist; + closure.insert(start); + worklist.push_back(start); - while(!worklist.empty()) { - unsigned state = worklist.front(); - worklist.pop_front(); - if (epsilon_map.find(state) != epsilon_map.end()) { - for (std::set::iterator it = epsilon_map[state].begin(); - it != epsilon_map[state].end(); ++it) { - unsigned new_state = *it; - if (closure.find(new_state) == closure.end()) { - closure.insert(new_state); - worklist.push_back(new_state); + while(!worklist.empty()) { + unsigned state = worklist.front(); + worklist.pop_front(); + if (epsilon_map.find(state) != epsilon_map.end()) { + for (std::set::iterator it = epsilon_map[state].begin(); + it != epsilon_map[state].end(); ++it) { + unsigned new_state = *it; + if (closure.find(new_state) == closure.end()) { + closure.insert(new_state); + worklist.push_back(new_state); + } + } + } + } + } + + bool nfa::matches(zstring input) { + /* + * Keep a set of all states the NFA can currently be in. + * Initially this is the e-closure of m_start_state + * For each character A in the input string, + * the set of next states contains + * all states in transition_map[S][A] for each S in current_states, + * and all states in epsilon_map[S] for each S in current_states. + * After consuming the entire input string, + * the match is successful iff current_states contains m_end_state. + */ + std::set current_states; + epsilon_closure(m_start_state, current_states); + for (unsigned i = 0; i < input.length(); ++i) { + char A = (char)input[i]; + std::set next_states; + for (std::set::iterator it = current_states.begin(); + it != current_states.end(); ++it) { + unsigned S = *it; + // check transition_map + if (transition_map[S].find(A) != transition_map[S].end()) { + next_states.insert(transition_map[S][A]); + } + } + + // take e-closure over next_states to compute the actual next_states + std::set epsilon_next_states; + for (std::set::iterator it = next_states.begin(); it != next_states.end(); ++it) { + unsigned S = *it; + std::set closure; + epsilon_closure(S, closure); + epsilon_next_states.insert(closure.begin(), closure.end()); + } + current_states = epsilon_next_states; + } + if (current_states.find(m_end_state) != current_states.end()) { + return true; + } else { + return false; + } + } + + void theory_str::check_regex_in(expr * nn1, expr * nn2) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + expr_ref_vector eqNodeSet(m); + + expr * constStr_1 = collect_eq_nodes(nn1, eqNodeSet); + expr * constStr_2 = collect_eq_nodes(nn2, eqNodeSet); + expr * constStr = (constStr_1 != NULL) ? constStr_1 : constStr_2; + + if (constStr == NULL) { + return; + } else { + expr_ref_vector::iterator itor = eqNodeSet.begin(); + for (; itor != eqNodeSet.end(); itor++) { + if (regex_in_var_reg_str_map.find(*itor) != regex_in_var_reg_str_map.end()) { + std::set::iterator strItor = regex_in_var_reg_str_map[*itor].begin(); + for (; strItor != regex_in_var_reg_str_map[*itor].end(); strItor++) { + zstring regStr = *strItor; + zstring constStrValue; + u.str.is_string(constStr, constStrValue); + std::pair key1 = std::make_pair(*itor, regStr); + if (regex_in_bool_map.find(key1) != regex_in_bool_map.end()) { + expr * boolVar = regex_in_bool_map[key1]; // actually the RegexIn term + app * a_regexIn = to_app(boolVar); + expr * regexTerm = a_regexIn->get_arg(1); + + // TODO figure out regex NFA stuff + if (regex_nfa_cache.find(regexTerm) == regex_nfa_cache.end()) { + TRACE("str", tout << "regex_nfa_cache: cache miss" << std::endl;); + regex_nfa_cache[regexTerm] = nfa(u, regexTerm); + } else { + TRACE("str", tout << "regex_nfa_cache: cache hit" << std::endl;); + } + + nfa regexNFA = regex_nfa_cache[regexTerm]; + ENSURE(regexNFA.is_valid()); + bool matchRes = regexNFA.matches(constStrValue); + + TRACE("str", tout << mk_pp(*itor, m) << " in " << regStr << " : " << (matchRes ? "yes" : "no") << std::endl;); + + expr_ref implyL(ctx.mk_eq_atom(*itor, constStr), m); + if (matchRes) { + assert_implication(implyL, boolVar); + } else { + assert_implication(implyL, m.mk_not(boolVar)); + } + } + } } } } } -} -bool nfa::matches(zstring input) { /* - * Keep a set of all states the NFA can currently be in. - * Initially this is the e-closure of m_start_state - * For each character A in the input string, - * the set of next states contains - * all states in transition_map[S][A] for each S in current_states, - * and all states in epsilon_map[S] for each S in current_states. - * After consuming the entire input string, - * the match is successful iff current_states contains m_end_state. + * strArgmt::solve_concat_eq_str() + * Solve concatenations of the form: + * const == Concat(const, X) + * const == Concat(X, const) */ - std::set current_states; - epsilon_closure(m_start_state, current_states); - for (unsigned i = 0; i < input.length(); ++i) { - char A = (char)input[i]; - std::set next_states; - for (std::set::iterator it = current_states.begin(); - it != current_states.end(); ++it) { - unsigned S = *it; - // check transition_map - if (transition_map[S].find(A) != transition_map[S].end()) { - next_states.insert(transition_map[S][A]); - } - } - - // take e-closure over next_states to compute the actual next_states - std::set epsilon_next_states; - for (std::set::iterator it = next_states.begin(); it != next_states.end(); ++it) { - unsigned S = *it; - std::set closure; - epsilon_closure(S, closure); - epsilon_next_states.insert(closure.begin(), closure.end()); - } - current_states = epsilon_next_states; - } - if (current_states.find(m_end_state) != current_states.end()) { - return true; - } else { - return false; - } -} - -void theory_str::check_regex_in(expr * nn1, expr * nn2) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - expr_ref_vector eqNodeSet(m); - - expr * constStr_1 = collect_eq_nodes(nn1, eqNodeSet); - expr * constStr_2 = collect_eq_nodes(nn2, eqNodeSet); - expr * constStr = (constStr_1 != NULL) ? constStr_1 : constStr_2; - - if (constStr == NULL) { - return; - } else { - expr_ref_vector::iterator itor = eqNodeSet.begin(); - for (; itor != eqNodeSet.end(); itor++) { - if (regex_in_var_reg_str_map.find(*itor) != regex_in_var_reg_str_map.end()) { - std::set::iterator strItor = regex_in_var_reg_str_map[*itor].begin(); - for (; strItor != regex_in_var_reg_str_map[*itor].end(); strItor++) { - zstring regStr = *strItor; - zstring constStrValue; - u.str.is_string(constStr, constStrValue); - std::pair key1 = std::make_pair(*itor, regStr); - if (regex_in_bool_map.find(key1) != regex_in_bool_map.end()) { - expr * boolVar = regex_in_bool_map[key1]; // actually the RegexIn term - app * a_regexIn = to_app(boolVar); - expr * regexTerm = a_regexIn->get_arg(1); - - // TODO figure out regex NFA stuff - if (regex_nfa_cache.find(regexTerm) == regex_nfa_cache.end()) { - TRACE("str", tout << "regex_nfa_cache: cache miss" << std::endl;); - regex_nfa_cache[regexTerm] = nfa(u, regexTerm); - } else { - TRACE("str", tout << "regex_nfa_cache: cache hit" << std::endl;); - } - - nfa regexNFA = regex_nfa_cache[regexTerm]; - ENSURE(regexNFA.is_valid()); - bool matchRes = regexNFA.matches(constStrValue); - - TRACE("str", tout << mk_pp(*itor, m) << " in " << regStr << " : " << (matchRes ? "yes" : "no") << std::endl;); - - expr_ref implyL(ctx.mk_eq_atom(*itor, constStr), m); - if (matchRes) { - assert_implication(implyL, boolVar); - } else { - assert_implication(implyL, m.mk_not(boolVar)); - } - } - } - } - } - } -} - -/* - * strArgmt::solve_concat_eq_str() - * Solve concatenations of the form: - * const == Concat(const, X) - * const == Concat(X, const) - */ -void theory_str::solve_concat_eq_str(expr * concat, expr * str) { - ast_manager & m = get_manager(); - context & ctx = get_context(); - - TRACE("str", tout << mk_ismt2_pp(concat, m) << " == " << mk_ismt2_pp(str, m) << std::endl;); - - zstring const_str; - if (u.str.is_concat(to_app(concat)) && u.str.is_string(to_app(str), const_str)) { - app * a_concat = to_app(concat); - SASSERT(a_concat->get_num_args() == 2); - expr * a1 = a_concat->get_arg(0); - expr * a2 = a_concat->get_arg(1); - - if (const_str.empty()) { - TRACE("str", tout << "quick path: concat == \"\"" << std::endl;); - // assert the following axiom: - // ( (Concat a1 a2) == "" ) -> ( (a1 == "") AND (a2 == "") ) - - - expr_ref premise(ctx.mk_eq_atom(concat, str), m); - expr_ref c1(ctx.mk_eq_atom(a1, str), m); - expr_ref c2(ctx.mk_eq_atom(a2, str), m); - expr_ref conclusion(m.mk_and(c1, c2), m); - assert_implication(premise, conclusion); - - return; - } - bool arg1_has_eqc_value = false; - bool arg2_has_eqc_value = false; - expr * arg1 = get_eqc_value(a1, arg1_has_eqc_value); - expr * arg2 = get_eqc_value(a2, arg2_has_eqc_value); - expr_ref newConcat(m); - if (arg1 != a1 || arg2 != a2) { - TRACE("str", tout << "resolved concat argument(s) to eqc string constants" << std::endl;); - int iPos = 0; - expr_ref_vector item1(m); - if (a1 != arg1) { - item1.push_back(ctx.mk_eq_atom(a1, arg1)); - iPos += 1; - } - if (a2 != arg2) { - item1.push_back(ctx.mk_eq_atom(a2, arg2)); - iPos += 1; - } - expr_ref implyL1(mk_and(item1), m); - newConcat = mk_concat(arg1, arg2); - if (newConcat != str) { - expr_ref implyR1(ctx.mk_eq_atom(concat, newConcat), m); - assert_implication(implyL1, implyR1); - } - } else { - newConcat = concat; - } - if (newConcat == str) { - return; - } - if (!u.str.is_concat(to_app(newConcat))) { - return; - } - if (arg1_has_eqc_value && arg2_has_eqc_value) { - // Case 1: Concat(const, const) == const - TRACE("str", tout << "Case 1: Concat(const, const) == const" << std::endl;); - zstring arg1_str, arg2_str; - u.str.is_string(arg1, arg1_str); - u.str.is_string(arg2, arg2_str); - - zstring result_str = arg1_str + arg2_str; - if (result_str != const_str) { - // Inconsistency - TRACE("str", tout << "inconsistency detected: \"" - << arg1_str << "\" + \"" << arg2_str << - "\" != \"" << const_str << "\"" << "\n";); - expr_ref equality(ctx.mk_eq_atom(concat, str), m); - expr_ref diseq(m.mk_not(equality), m); - assert_axiom(diseq); - return; - } - } else if (!arg1_has_eqc_value && arg2_has_eqc_value) { - // Case 2: Concat(var, const) == const - TRACE("str", tout << "Case 2: Concat(var, const) == const" << std::endl;); - zstring arg2_str; - u.str.is_string(arg2, arg2_str); - unsigned int resultStrLen = const_str.length(); - unsigned int arg2StrLen = arg2_str.length(); - if (resultStrLen < arg2StrLen) { - // Inconsistency - TRACE("str", tout << "inconsistency detected: \"" - << arg2_str << - "\" is longer than \"" << const_str << "\"," - << " so cannot be concatenated with anything to form it" << "\n";); - expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); - expr_ref diseq(m.mk_not(equality), m); - assert_axiom(diseq); - return; - } else { - int varStrLen = resultStrLen - arg2StrLen; - zstring firstPart = const_str.extract(0, varStrLen); - zstring secondPart = const_str.extract(varStrLen, arg2StrLen); - if (arg2_str != secondPart) { - // Inconsistency - TRACE("str", tout << "inconsistency detected: " - << "suffix of concatenation result expected \"" << secondPart << "\", " - << "actually \"" << arg2_str << "\"" - << "\n";); - expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); - expr_ref diseq(m.mk_not(equality), m); - assert_axiom(diseq); - return; - } else { - expr_ref tmpStrConst(mk_string(firstPart), m); - expr_ref premise(ctx.mk_eq_atom(newConcat, str), m); - expr_ref conclusion(ctx.mk_eq_atom(arg1, tmpStrConst), m); - assert_implication(premise, conclusion); - return; - } - } - } else if (arg1_has_eqc_value && !arg2_has_eqc_value) { - // Case 3: Concat(const, var) == const - TRACE("str", tout << "Case 3: Concat(const, var) == const" << std::endl;); - zstring arg1_str; - u.str.is_string(arg1, arg1_str); - unsigned int resultStrLen = const_str.length(); - unsigned int arg1StrLen = arg1_str.length(); - if (resultStrLen < arg1StrLen) { - // Inconsistency - TRACE("str", tout << "inconsistency detected: \"" - << arg1_str << - "\" is longer than \"" << const_str << "\"," - << " so cannot be concatenated with anything to form it" << "\n";); - expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); - expr_ref diseq(m.mk_not(equality), m); - assert_axiom(diseq); - return; - } else { - int varStrLen = resultStrLen - arg1StrLen; - zstring firstPart = const_str.extract(0, arg1StrLen); - zstring secondPart = const_str.extract(arg1StrLen, varStrLen); - if (arg1_str != firstPart) { - // Inconsistency - TRACE("str", tout << "inconsistency detected: " - << "prefix of concatenation result expected \"" << secondPart << "\", " - << "actually \"" << arg1_str << "\"" - << "\n";); - expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); - expr_ref diseq(m.mk_not(equality), m); - assert_axiom(diseq); - return; - } else { - expr_ref tmpStrConst(mk_string(secondPart), m); - expr_ref premise(ctx.mk_eq_atom(newConcat, str), m); - expr_ref conclusion(ctx.mk_eq_atom(arg2, tmpStrConst), m); - assert_implication(premise, conclusion); - return; - } - } - } else { - // Case 4: Concat(var, var) == const - TRACE("str", tout << "Case 4: Concat(var, var) == const" << std::endl;); - if (eval_concat(arg1, arg2) == NULL) { - rational arg1Len, arg2Len; - bool arg1Len_exists = get_len_value(arg1, arg1Len); - bool arg2Len_exists = get_len_value(arg2, arg2Len); - rational concatStrLen((unsigned)const_str.length()); - if (arg1Len_exists || arg2Len_exists) { - expr_ref ax_l1(ctx.mk_eq_atom(concat, str), m); - expr_ref ax_l2(m); - zstring prefixStr, suffixStr; - if (arg1Len_exists) { - if (arg1Len.is_neg()) { - TRACE("str", tout << "length conflict: arg1Len = " << arg1Len << ", concatStrLen = " << concatStrLen << std::endl;); - expr_ref toAssert(m_autil.mk_ge(mk_strlen(arg1), mk_int(0)), m); - assert_axiom(toAssert); - return; - } else if (arg1Len > concatStrLen) { - TRACE("str", tout << "length conflict: arg1Len = " << arg1Len << ", concatStrLen = " << concatStrLen << std::endl;); - expr_ref ax_r1(m_autil.mk_le(mk_strlen(arg1), mk_int(concatStrLen)), m); - assert_implication(ax_l1, ax_r1); - return; - } - - prefixStr = const_str.extract(0, arg1Len.get_unsigned()); - rational concat_minus_arg1 = concatStrLen - arg1Len; - suffixStr = const_str.extract(arg1Len.get_unsigned(), concat_minus_arg1.get_unsigned()); - ax_l2 = ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1Len)); - } else { - // arg2's length is available - if (arg2Len.is_neg()) { - TRACE("str", tout << "length conflict: arg2Len = " << arg2Len << ", concatStrLen = " << concatStrLen << std::endl;); - expr_ref toAssert(m_autil.mk_ge(mk_strlen(arg2), mk_int(0)), m); - assert_axiom(toAssert); - return; - } else if (arg2Len > concatStrLen) { - TRACE("str", tout << "length conflict: arg2Len = " << arg2Len << ", concatStrLen = " << concatStrLen << std::endl;); - expr_ref ax_r1(m_autil.mk_le(mk_strlen(arg2), mk_int(concatStrLen)), m); - assert_implication(ax_l1, ax_r1); - return; - } - - rational concat_minus_arg2 = concatStrLen - arg2Len; - prefixStr = const_str.extract(0, concat_minus_arg2.get_unsigned()); - suffixStr = const_str.extract(concat_minus_arg2.get_unsigned(), arg2Len.get_unsigned()); - ax_l2 = ctx.mk_eq_atom(mk_strlen(arg2), mk_int(arg2Len)); - } - // consistency check - if (u.str.is_concat(to_app(arg1)) && !can_concat_eq_str(arg1, prefixStr)) { - expr_ref ax_r(m.mk_not(ax_l2), m); - assert_implication(ax_l1, ax_r); - return; - } - if (u.str.is_concat(to_app(arg2)) && !can_concat_eq_str(arg2, suffixStr)) { - expr_ref ax_r(m.mk_not(ax_l2), m); - assert_implication(ax_l1, ax_r); - return; - } - expr_ref_vector r_items(m); - r_items.push_back(ctx.mk_eq_atom(arg1, mk_string(prefixStr))); - r_items.push_back(ctx.mk_eq_atom(arg2, mk_string(suffixStr))); - if (!arg1Len_exists) { - r_items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(prefixStr.length()))); - } - if (!arg2Len_exists) { - r_items.push_back(ctx.mk_eq_atom(mk_strlen(arg2), mk_int(suffixStr.length()))); - } - expr_ref lhs(m.mk_and(ax_l1, ax_l2), m); - expr_ref rhs(mk_and(r_items), m); - assert_implication(lhs, rhs); - } else { /* ! (arg1Len != 1 || arg2Len != 1) */ - expr_ref xorFlag(m); - std::pair key1(arg1, arg2); - std::pair key2(arg2, arg1); - - // check the entries in this map to make sure they're still in scope - // before we use them. - - std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); - std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); - - bool entry1InScope; - if (entry1 == varForBreakConcat.end()) { - TRACE("str", tout << "key1 no entry" << std::endl;); - entry1InScope = false; - } else { - // OVERRIDE. - entry1InScope = true; - TRACE("str", tout << "key1 entry" << std::endl;); - /* - if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end()) { - TRACE("str", tout << "key1 entry not in scope" << std::endl;); - entry1InScope = false; - } else { - TRACE("str", tout << "key1 entry in scope" << std::endl;); - entry1InScope = true; - } - */ - } - - bool entry2InScope; - if (entry2 == varForBreakConcat.end()) { - TRACE("str", tout << "key2 no entry" << std::endl;); - entry2InScope = false; - } else { - // OVERRIDE. - entry2InScope = true; - TRACE("str", tout << "key2 entry" << std::endl;); - /* - if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end()) { - TRACE("str", tout << "key2 entry not in scope" << std::endl;); - entry2InScope = false; - } else { - TRACE("str", tout << "key2 entry in scope" << std::endl;); - entry2InScope = true; - } - */ - } - - TRACE("str", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl - << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); - - if (!entry1InScope && !entry2InScope) { - xorFlag = mk_internal_xor_var(); - varForBreakConcat[key1][0] = xorFlag; - } else if (entry1InScope) { - xorFlag = varForBreakConcat[key1][0]; - } else { // entry2InScope - xorFlag = varForBreakConcat[key2][0]; - } - - int concatStrLen = const_str.length(); - int and_count = 1; - - expr_ref_vector arrangement_disjunction(m); - - for (int i = 0; i < concatStrLen + 1; ++i) { - expr_ref_vector and_items(m); - zstring prefixStr = const_str.extract(0, i); - zstring suffixStr = const_str.extract(i, concatStrLen - i); - // skip invalid options - if (u.str.is_concat(to_app(arg1)) && !can_concat_eq_str(arg1, prefixStr)) { - continue; - } - if (u.str.is_concat(to_app(arg2)) && !can_concat_eq_str(arg2, suffixStr)) { - continue; - } - - expr_ref prefixAst(mk_string(prefixStr), m); - expr_ref arg1_eq (ctx.mk_eq_atom(arg1, prefixAst), m); - and_items.push_back(arg1_eq); - and_count += 1; - - expr_ref suffixAst(mk_string(suffixStr), m); - expr_ref arg2_eq (ctx.mk_eq_atom(arg2, suffixAst), m); - and_items.push_back(arg2_eq); - and_count += 1; - - arrangement_disjunction.push_back(mk_and(and_items)); - } - - expr_ref implyL(ctx.mk_eq_atom(concat, str), m); - expr_ref implyR1(m); - if (arrangement_disjunction.empty()) { - // negate - expr_ref concat_eq_str(ctx.mk_eq_atom(concat, str), m); - expr_ref negate_ast(m.mk_not(concat_eq_str), m); - assert_axiom(negate_ast); - } else { - implyR1 = mk_or(arrangement_disjunction); - if (m_params.m_StrongArrangements) { - expr_ref ax_strong(ctx.mk_eq_atom(implyL, implyR1), m); - assert_axiom(ax_strong); - } else { - assert_implication(implyL, implyR1); - } - generate_mutual_exclusion(arrangement_disjunction); - } - } /* (arg1Len != 1 || arg2Len != 1) */ - } /* if (Concat(arg1, arg2) == NULL) */ - } - } -} - -expr_ref theory_str::set_up_finite_model_test(expr * lhs, expr * rhs) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - TRACE("str", tout << "activating finite model testing for overlapping concats " - << mk_pp(lhs, m) << " and " << mk_pp(rhs, m) << std::endl;); - std::map concatMap; - std::map unrollMap; - std::map varMap; - classify_ast_by_type(lhs, varMap, concatMap, unrollMap); - classify_ast_by_type(rhs, varMap, concatMap, unrollMap); - TRACE("str", tout << "found vars:"; - for (std::map::iterator it = varMap.begin(); it != varMap.end(); ++it) { - tout << " " << mk_pp(it->first, m); - } - tout << std::endl; - ); - - expr_ref testvar(mk_str_var("finiteModelTest"), m); - m_trail.push_back(testvar); - ptr_vector varlist; - - for (std::map::iterator it = varMap.begin(); it != varMap.end(); ++it) { - expr * v = it->first; - varlist.push_back(v); - } - - // make things easy for the core wrt. testvar - expr_ref t1(ctx.mk_eq_atom(testvar, mk_string("")), m); - expr_ref t_yes(ctx.mk_eq_atom(testvar, mk_string("yes")), m); - expr_ref testvaraxiom(m.mk_or(t1, t_yes), m); - assert_axiom(testvaraxiom); - - finite_model_test_varlists.insert(testvar, varlist); - m_trail_stack.push(insert_obj_map >(finite_model_test_varlists, testvar) ); - return t_yes; -} - -void theory_str::finite_model_test(expr * testvar, expr * str) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - zstring s; - if (!u.str.is_string(str, s)) return; - if (s == "yes") { - TRACE("str", tout << "start finite model test for " << mk_pp(testvar, m) << std::endl;); - ptr_vector & vars = finite_model_test_varlists[testvar]; - for (ptr_vector::iterator it = vars.begin(); it != vars.end(); ++it) { - expr * v = *it; - bool v_has_eqc = false; - get_eqc_value(v, v_has_eqc); - if (v_has_eqc) { - TRACE("str", tout << "variable " << mk_pp(v,m) << " already equivalent to a string constant" << std::endl;); - continue; - } - // check for any sort of existing length tester we might interfere with - if (m_params.m_UseBinarySearch) { - if (binary_search_len_tester_stack.contains(v) && !binary_search_len_tester_stack[v].empty()) { - TRACE("str", tout << "already found existing length testers for " << mk_pp(v, m) << std::endl;); - continue; - } else { - // start binary search as normal - expr_ref implLhs(ctx.mk_eq_atom(testvar, str), m); - expr_ref implRhs(binary_search_length_test(v, NULL, ""), m); - assert_implication(implLhs, implRhs); - } - } else { - bool map_effectively_empty = false; - if (!fvar_len_count_map.contains(v)) { - map_effectively_empty = true; - } - - if (!map_effectively_empty) { - map_effectively_empty = true; - ptr_vector indicator_set = fvar_lenTester_map[v]; - for (ptr_vector::iterator it = indicator_set.begin(); it != indicator_set.end(); ++it) { - expr * indicator = *it; - if (internal_variable_set.find(indicator) != internal_variable_set.end()) { - map_effectively_empty = false; - break; - } - } - } - - if (map_effectively_empty) { - TRACE("str", tout << "no existing length testers for " << mk_pp(v, m) << std::endl;); - rational v_len; - rational v_lower_bound; - rational v_upper_bound; - expr_ref vLengthExpr(mk_strlen(v), m); - if (get_len_value(v, v_len)) { - TRACE("str", tout << "length = " << v_len.to_string() << std::endl;); - v_lower_bound = v_len; - v_upper_bound = v_len; - } else { - bool lower_bound_exists = lower_bound(vLengthExpr, v_lower_bound); - bool upper_bound_exists = upper_bound(vLengthExpr, v_upper_bound); - TRACE("str", tout << "bounds = [" << (lower_bound_exists?v_lower_bound.to_string():"?") - << ".." << (upper_bound_exists?v_upper_bound.to_string():"?") << "]" << std::endl;); - - // make sure the bounds are non-negative - if (lower_bound_exists && v_lower_bound.is_neg()) { - v_lower_bound = rational::zero(); - } - if (upper_bound_exists && v_upper_bound.is_neg()) { - v_upper_bound = rational::zero(); - } - - if (lower_bound_exists && upper_bound_exists) { - // easiest case. we will search within these bounds - } else if (upper_bound_exists && !lower_bound_exists) { - // search between 0 and the upper bound - v_lower_bound == rational::zero(); - } else if (lower_bound_exists && !upper_bound_exists) { - // check some finite portion of the search space - v_upper_bound = v_lower_bound + rational(10); - } else { - // no bounds information - v_lower_bound = rational::zero(); - v_upper_bound = v_lower_bound + rational(10); - } - } - // now create a fake length tester over this finite disjunction of lengths - - fvar_len_count_map[v] = 1; - unsigned int testNum = fvar_len_count_map[v]; - - expr_ref indicator(mk_internal_lenTest_var(v, testNum), m); - SASSERT(indicator); - m_trail.push_back(indicator); - - fvar_lenTester_map[v].shrink(0); - fvar_lenTester_map[v].push_back(indicator); - lenTester_fvar_map[indicator] = v; - - expr_ref_vector orList(m); - expr_ref_vector andList(m); - - for (rational l = v_lower_bound; l <= v_upper_bound; l += rational::one()) { - zstring lStr = zstring(l.to_string().c_str()); - expr_ref str_indicator(mk_string(lStr), m); - expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); - orList.push_back(or_expr); - expr_ref and_expr(ctx.mk_eq_atom(or_expr, ctx.mk_eq_atom(vLengthExpr, m_autil.mk_numeral(l, true))), m); - andList.push_back(and_expr); - } - andList.push_back(mk_or(orList)); - expr_ref implLhs(ctx.mk_eq_atom(testvar, str), m); - expr_ref implRhs(mk_and(andList), m); - assert_implication(implLhs, implRhs); - } else { - TRACE("str", tout << "already found existing length testers for " << mk_pp(v, m) << std::endl;); - continue; - } - } - } // foreach (v in vars) - } // (s == "yes") -} - -void theory_str::more_len_tests(expr * lenTester, zstring lenTesterValue) { - ast_manager & m = get_manager(); - if (lenTester_fvar_map.contains(lenTester)) { - expr * fVar = lenTester_fvar_map[lenTester]; - expr * toAssert = gen_len_val_options_for_free_var(fVar, lenTester, lenTesterValue); - TRACE("str", tout << "asserting more length tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); - if (toAssert != NULL) { - assert_axiom(toAssert); - } - } -} - -void theory_str::more_value_tests(expr * valTester, zstring valTesterValue) { - ast_manager & m = get_manager(); - - expr * fVar = valueTester_fvar_map[valTester]; - if (m_params.m_UseBinarySearch) { - if (!binary_search_len_tester_stack.contains(fVar) || binary_search_len_tester_stack[fVar].empty()) { - TRACE("str", tout << "WARNING: no active length testers for " << mk_pp(fVar, m) << std::endl;); - NOT_IMPLEMENTED_YET(); - } - expr * effectiveLenInd = binary_search_len_tester_stack[fVar].back(); - bool hasEqcValue; - expr * len_indicator_value = get_eqc_value(effectiveLenInd, hasEqcValue); - if (!hasEqcValue) { - TRACE("str", tout << "WARNING: length tester " << mk_pp(effectiveLenInd, m) << " at top of stack for " << mk_pp(fVar, m) << " has no EQC value" << std::endl;); - } else { - // safety check - zstring effectiveLenIndiStr; - u.str.is_string(len_indicator_value, effectiveLenIndiStr); - if (effectiveLenIndiStr == "more" || effectiveLenIndiStr == "less") { - TRACE("str", tout << "ERROR: illegal state -- requesting 'more value tests' but a length tester is not yet concrete!" << std::endl;); - UNREACHABLE(); - } - expr * valueAssert = gen_free_var_options(fVar, effectiveLenInd, effectiveLenIndiStr, valTester, valTesterValue); - TRACE("str", tout << "asserting more value tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); - if (valueAssert != NULL) { - assert_axiom(valueAssert); - } - } - } else { - int lenTesterCount = fvar_lenTester_map[fVar].size(); - - expr * effectiveLenInd = NULL; - zstring effectiveLenIndiStr = ""; - for (int i = 0; i < lenTesterCount; ++i) { - expr * len_indicator_pre = fvar_lenTester_map[fVar][i]; - bool indicatorHasEqcValue = false; - expr * len_indicator_value = get_eqc_value(len_indicator_pre, indicatorHasEqcValue); - if (indicatorHasEqcValue) { - zstring len_pIndiStr; - u.str.is_string(len_indicator_value, len_pIndiStr); - if (len_pIndiStr != "more") { - effectiveLenInd = len_indicator_pre; - effectiveLenIndiStr = len_pIndiStr; - break; - } - } - } - expr * valueAssert = gen_free_var_options(fVar, effectiveLenInd, effectiveLenIndiStr, valTester, valTesterValue); - TRACE("str", tout << "asserting more value tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); - if (valueAssert != NULL) { - assert_axiom(valueAssert); - } - } -} - -bool theory_str::free_var_attempt(expr * nn1, expr * nn2) { - ast_manager & m = get_manager(); - zstring nn2_str; - if (internal_lenTest_vars.contains(nn1) && u.str.is_string(nn2, nn2_str)) { - TRACE("str", tout << "acting on equivalence between length tester var " << mk_ismt2_pp(nn1, m) - << " and constant " << mk_ismt2_pp(nn2, m) << std::endl;); - more_len_tests(nn1, nn2_str); - return true; - } else if (internal_valTest_vars.contains(nn1) && u.str.is_string(nn2, nn2_str)) { - if (nn2_str == "more") { - TRACE("str", tout << "acting on equivalence between value var " << mk_ismt2_pp(nn1, m) - << " and constant " << mk_ismt2_pp(nn2, m) << std::endl;); - more_value_tests(nn1, nn2_str); - } - return true; - } else if (internal_unrollTest_vars.contains(nn1)) { - return true; - } else { - return false; - } -} - -void theory_str::handle_equality(expr * lhs, expr * rhs) { - ast_manager & m = get_manager(); - context & ctx = get_context(); - // both terms must be of sort String - sort * lhs_sort = m.get_sort(lhs); - sort * rhs_sort = m.get_sort(rhs); - sort * str_sort = u.str.mk_string_sort(); - - if (lhs_sort != str_sort || rhs_sort != str_sort) { - TRACE("str", tout << "skip equality: not String sort" << std::endl;); - return; - } - - /* // temporarily disabled, we are borrowing these testers for something else - if (m_params.m_FiniteOverlapModels && !finite_model_test_varlists.empty()) { - if (finite_model_test_varlists.contains(lhs)) { - finite_model_test(lhs, rhs); return; - } else if (finite_model_test_varlists.contains(rhs)) { - finite_model_test(rhs, lhs); return; - } - } - */ - - if (free_var_attempt(lhs, rhs) || free_var_attempt(rhs, lhs)) { - return; - } - - if (u.str.is_concat(to_app(lhs)) && u.str.is_concat(to_app(rhs))) { - bool nn1HasEqcValue = false; - bool nn2HasEqcValue = false; - expr * nn1_value = get_eqc_value(lhs, nn1HasEqcValue); - expr * nn2_value = get_eqc_value(rhs, nn2HasEqcValue); - if (nn1HasEqcValue && !nn2HasEqcValue) { - simplify_parent(rhs, nn1_value); - } - if (!nn1HasEqcValue && nn2HasEqcValue) { - simplify_parent(lhs, nn2_value); - } - - expr * nn1_arg0 = to_app(lhs)->get_arg(0); - expr * nn1_arg1 = to_app(lhs)->get_arg(1); - expr * nn2_arg0 = to_app(rhs)->get_arg(0); - expr * nn2_arg1 = to_app(rhs)->get_arg(1); - if (nn1_arg0 == nn2_arg0 && in_same_eqc(nn1_arg1, nn2_arg1)) { - TRACE("str", tout << "skip: lhs arg0 == rhs arg0" << std::endl;); - return; - } - - if (nn1_arg1 == nn2_arg1 && in_same_eqc(nn1_arg0, nn2_arg0)) { - TRACE("str", tout << "skip: lhs arg1 == rhs arg1" << std::endl;); - return; - } - } - - if (opt_DeferEQCConsistencyCheck) { - TRACE("str", tout << "opt_DeferEQCConsistencyCheck is set; deferring new_eq_check call" << std::endl;); - } else { - // newEqCheck() -- check consistency wrt. existing equivalence classes - if (!new_eq_check(lhs, rhs)) { - return; - } - } - - // BEGIN new_eq_handler() in strTheory - - { - rational nn1Len, nn2Len; - bool nn1Len_exists = get_len_value(lhs, nn1Len); - bool nn2Len_exists = get_len_value(rhs, nn2Len); - expr * emptyStr = mk_string(""); - - if (nn1Len_exists && nn1Len.is_zero()) { - if (!in_same_eqc(lhs, emptyStr) && rhs != emptyStr) { - expr_ref eql(ctx.mk_eq_atom(mk_strlen(lhs), mk_int(0)), m); - expr_ref eqr(ctx.mk_eq_atom(lhs, emptyStr), m); - expr_ref toAssert(ctx.mk_eq_atom(eql, eqr), m); - assert_axiom(toAssert); - } - } - - if (nn2Len_exists && nn2Len.is_zero()) { - if (!in_same_eqc(rhs, emptyStr) && lhs != emptyStr) { - expr_ref eql(ctx.mk_eq_atom(mk_strlen(rhs), mk_int(0)), m); - expr_ref eqr(ctx.mk_eq_atom(rhs, emptyStr), m); - expr_ref toAssert(ctx.mk_eq_atom(eql, eqr), m); - assert_axiom(toAssert); - } - } - } - - instantiate_str_eq_length_axiom(ctx.get_enode(lhs), ctx.get_enode(rhs)); - - // group terms by equivalence class (groupNodeInEqc()) - - std::set eqc_concat_lhs; - std::set eqc_var_lhs; - std::set eqc_const_lhs; - group_terms_by_eqc(lhs, eqc_concat_lhs, eqc_var_lhs, eqc_const_lhs); - - std::set eqc_concat_rhs; - std::set eqc_var_rhs; - std::set eqc_const_rhs; - group_terms_by_eqc(rhs, eqc_concat_rhs, eqc_var_rhs, eqc_const_rhs); - - TRACE("str", - tout << "lhs eqc:" << std::endl; - tout << "Concats:" << std::endl; - for (std::set::iterator it = eqc_concat_lhs.begin(); it != eqc_concat_lhs.end(); ++it) { - expr * ex = *it; - tout << mk_ismt2_pp(ex, get_manager()) << std::endl; - } - tout << "Variables:" << std::endl; - for (std::set::iterator it = eqc_var_lhs.begin(); it != eqc_var_lhs.end(); ++it) { - expr * ex = *it; - tout << mk_ismt2_pp(ex, get_manager()) << std::endl; - } - tout << "Constants:" << std::endl; - for (std::set::iterator it = eqc_const_lhs.begin(); it != eqc_const_lhs.end(); ++it) { - expr * ex = *it; - tout << mk_ismt2_pp(ex, get_manager()) << std::endl; - } - - tout << "rhs eqc:" << std::endl; - tout << "Concats:" << std::endl; - for (std::set::iterator it = eqc_concat_rhs.begin(); it != eqc_concat_rhs.end(); ++it) { - expr * ex = *it; - tout << mk_ismt2_pp(ex, get_manager()) << std::endl; - } - tout << "Variables:" << std::endl; - for (std::set::iterator it = eqc_var_rhs.begin(); it != eqc_var_rhs.end(); ++it) { - expr * ex = *it; - tout << mk_ismt2_pp(ex, get_manager()) << std::endl; - } - tout << "Constants:" << std::endl; - for (std::set::iterator it = eqc_const_rhs.begin(); it != eqc_const_rhs.end(); ++it) { - expr * ex = *it; - tout << mk_ismt2_pp(ex, get_manager()) << std::endl; - } - ); - - // step 1: Concat == Concat - int hasCommon = 0; - if (eqc_concat_lhs.size() != 0 && eqc_concat_rhs.size() != 0) { - std::set::iterator itor1 = eqc_concat_lhs.begin(); - std::set::iterator itor2 = eqc_concat_rhs.begin(); - for (; itor1 != eqc_concat_lhs.end(); itor1++) { - if (eqc_concat_rhs.find(*itor1) != eqc_concat_rhs.end()) { - hasCommon = 1; - break; - } - } - for (; itor2 != eqc_concat_rhs.end(); itor2++) { - if (eqc_concat_lhs.find(*itor2) != eqc_concat_lhs.end()) { - hasCommon = 1; - break; - } - } - if (hasCommon == 0) { - if (opt_ConcatOverlapAvoid) { - bool found = false; - // check each pair and take the first ones that won't immediately overlap - for (itor1 = eqc_concat_lhs.begin(); itor1 != eqc_concat_lhs.end() && !found; ++itor1) { - expr * concat_lhs = *itor1; - for (itor2 = eqc_concat_rhs.begin(); itor2 != eqc_concat_rhs.end() && !found; ++itor2) { - expr * concat_rhs = *itor2; - if (will_result_in_overlap(concat_lhs, concat_rhs)) { - TRACE("str", tout << "Concats " << mk_pp(concat_lhs, m) << " and " - << mk_pp(concat_rhs, m) << " will result in overlap; skipping." << std::endl;); - } else { - TRACE("str", tout << "Concats " << mk_pp(concat_lhs, m) << " and " - << mk_pp(concat_rhs, m) << " won't overlap. Simplifying here." << std::endl;); - simplify_concat_equality(concat_lhs, concat_rhs); - found = true; - break; - } - } - } - if (!found) { - TRACE("str", tout << "All pairs of concats expected to overlap, falling back." << std::endl;); - simplify_concat_equality(*(eqc_concat_lhs.begin()), *(eqc_concat_rhs.begin())); - } - } else { - // default behaviour - simplify_concat_equality(*(eqc_concat_lhs.begin()), *(eqc_concat_rhs.begin())); - } - } - } - - // step 2: Concat == Constant - - if (eqc_const_lhs.size() != 0) { - expr * conStr = *(eqc_const_lhs.begin()); - std::set::iterator itor2 = eqc_concat_rhs.begin(); - for (; itor2 != eqc_concat_rhs.end(); itor2++) { - solve_concat_eq_str(*itor2, conStr); - } - } else if (eqc_const_rhs.size() != 0) { - expr* conStr = *(eqc_const_rhs.begin()); - std::set::iterator itor1 = eqc_concat_lhs.begin(); - for (; itor1 != eqc_concat_lhs.end(); itor1++) { - solve_concat_eq_str(*itor1, conStr); - } - } - - // simplify parents wrt. the equivalence class of both sides - bool nn1HasEqcValue = false; - bool nn2HasEqcValue = false; - // we want the Z3str2 eqc check here... - expr * nn1_value = z3str2_get_eqc_value(lhs, nn1HasEqcValue); - expr * nn2_value = z3str2_get_eqc_value(rhs, nn2HasEqcValue); - if (nn1HasEqcValue && !nn2HasEqcValue) { - simplify_parent(rhs, nn1_value); - } - - if (!nn1HasEqcValue && nn2HasEqcValue) { - simplify_parent(lhs, nn2_value); - } - - expr * nn1EqConst = NULL; - std::set nn1EqUnrollFuncs; - get_eqc_allUnroll(lhs, nn1EqConst, nn1EqUnrollFuncs); - expr * nn2EqConst = NULL; - std::set nn2EqUnrollFuncs; - get_eqc_allUnroll(rhs, nn2EqConst, nn2EqUnrollFuncs); - - if (nn2EqConst != NULL) { - for (std::set::iterator itor1 = nn1EqUnrollFuncs.begin(); itor1 != nn1EqUnrollFuncs.end(); itor1++) { - process_unroll_eq_const_str(*itor1, nn2EqConst); - } - } - - if (nn1EqConst != NULL) { - for (std::set::iterator itor2 = nn2EqUnrollFuncs.begin(); itor2 != nn2EqUnrollFuncs.end(); itor2++) { - process_unroll_eq_const_str(*itor2, nn1EqConst); - } - } - -} - -void theory_str::set_up_axioms(expr * ex) { - ast_manager & m = get_manager(); - context & ctx = get_context(); - - sort * ex_sort = m.get_sort(ex); - sort * str_sort = u.str.mk_string_sort(); - sort * bool_sort = m.mk_bool_sort(); - - family_id m_arith_fid = m.mk_family_id("arith"); - sort * int_sort = m.mk_sort(m_arith_fid, INT_SORT); - - if (ex_sort == str_sort) { - TRACE("str", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << - ": expr is of sort String" << std::endl;); - // set up basic string axioms - enode * n = ctx.get_enode(ex); - SASSERT(n); - m_basicstr_axiom_todo.push_back(n); - TRACE("str", tout << "add " << mk_pp(ex, m) << " to m_basicstr_axiom_todo" << std::endl;); - - - if (is_app(ex)) { - app * ap = to_app(ex); - if (u.str.is_concat(ap)) { - // if ex is a concat, set up concat axioms later - m_concat_axiom_todo.push_back(n); - // we also want to check whether we can eval this concat, - // in case the rewriter did not totally finish with this term - m_concat_eval_todo.push_back(n); - } else if (u.str.is_length(ap)) { - // if the argument is a variable, - // keep track of this for later, we'll need it during model gen - expr * var = ap->get_arg(0); - app * aVar = to_app(var); - if (aVar->get_num_args() == 0 && !u.str.is_string(aVar)) { - input_var_in_len.insert(var); - } - } else if (u.str.is_at(ap) || u.str.is_extract(ap) || u.str.is_replace(ap)) { - m_library_aware_axiom_todo.push_back(n); - } else if (u.str.is_itos(ap)) { - TRACE("str", tout << "found string-integer conversion term: " << mk_pp(ex, get_manager()) << std::endl;); - string_int_conversion_terms.push_back(ap); - m_library_aware_axiom_todo.push_back(n); - } else if (ap->get_num_args() == 0 && !u.str.is_string(ap)) { - // if ex is a variable, add it to our list of variables - TRACE("str", tout << "tracking variable " << mk_ismt2_pp(ap, get_manager()) << std::endl;); - variable_set.insert(ex); - ctx.mark_as_relevant(ex); - // this might help?? - theory_var v = mk_var(n); - TRACE("str", tout << "variable " << mk_ismt2_pp(ap, get_manager()) << " is #" << v << std::endl;); - } - } - } else if (ex_sort == bool_sort) { - TRACE("str", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << - ": expr is of sort Bool" << std::endl;); - // set up axioms for boolean terms - - ensure_enode(ex); - if (ctx.e_internalized(ex)) { - enode * n = ctx.get_enode(ex); - SASSERT(n); - - if (is_app(ex)) { - app * ap = to_app(ex); - if (u.str.is_prefix(ap) || u.str.is_suffix(ap) || u.str.is_contains(ap) || u.str.is_in_re(ap)) { - m_library_aware_axiom_todo.push_back(n); - } - } - } else { - TRACE("str", tout << "WARNING: Bool term " << mk_ismt2_pp(ex, get_manager()) << " not internalized. Delaying axiom setup to prevent a crash." << std::endl;); - ENSURE(!search_started); // infinite loop prevention - m_delayed_axiom_setup_terms.push_back(ex); - return; - } - } else if (ex_sort == int_sort) { - TRACE("str", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << - ": expr is of sort Int" << std::endl;); - // set up axioms for integer terms - enode * n = ensure_enode(ex); - SASSERT(n); - - if (is_app(ex)) { - app * ap = to_app(ex); - // TODO indexof2/lastindexof - if (u.str.is_index(ap) /* || is_Indexof2(ap) || is_LastIndexof(ap) */) { - m_library_aware_axiom_todo.push_back(n); - } else if (u.str.is_stoi(ap)) { - TRACE("str", tout << "found string-integer conversion term: " << mk_pp(ex, get_manager()) << std::endl;); - string_int_conversion_terms.push_back(ap); - m_library_aware_axiom_todo.push_back(n); - } - } - } else { - TRACE("str", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << - ": expr is of wrong sort, ignoring" << std::endl;); - } - - // if expr is an application, recursively inspect all arguments - if (is_app(ex)) { - app * term = (app*)ex; - unsigned num_args = term->get_num_args(); - for (unsigned i = 0; i < num_args; i++) { - set_up_axioms(term->get_arg(i)); - } - } -} - -void theory_str::add_theory_assumptions(expr_ref_vector & assumptions) { - TRACE("str", tout << "add overlap assumption for theory_str" << std::endl;); - symbol strOverlap("!!TheoryStrOverlapAssumption!!"); - seq_util m_sequtil(get_manager()); - sort * s = get_manager().mk_bool_sort(); - m_theoryStrOverlapAssumption_term = expr_ref(get_manager().mk_const(strOverlap, s), get_manager()); - assumptions.push_back(get_manager().mk_not(m_theoryStrOverlapAssumption_term)); -} - -lbool theory_str::validate_unsat_core(expr_ref_vector & unsat_core) { - bool assumptionFound = false; - - app * target_term = to_app(get_manager().mk_not(m_theoryStrOverlapAssumption_term)); - get_context().internalize(target_term, false); - for (unsigned i = 0; i < unsat_core.size(); ++i) { - app * core_term = to_app(unsat_core.get(i)); - // not sure if this is the correct way to compare terms in this context - enode * e1; - enode * e2; - e1 = get_context().get_enode(target_term); - e2 = get_context().get_enode(core_term); - if (e1 == e2) { - TRACE("str", tout << "overlap detected in unsat core, changing UNSAT to UNKNOWN" << std::endl;); - assumptionFound = true; - return l_undef; - } - } - - return l_false; -} - -void theory_str::init_search_eh() { - ast_manager & m = get_manager(); - context & ctx = get_context(); - - TRACE("str", - tout << "dumping all asserted formulas:" << std::endl; - unsigned nFormulas = ctx.get_num_asserted_formulas(); - for (unsigned i = 0; i < nFormulas; ++i) { - expr * ex = ctx.get_asserted_formula(i); - tout << mk_ismt2_pp(ex, m) << (ctx.is_relevant(ex) ? " (rel)" : " (NOT REL)") << std::endl; - } - ); - /* - * Recursive descent through all asserted formulas to set up axioms. - * Note that this is just the input structure and not necessarily things - * that we know to be true or false. We're just doing this to see - * which terms are explicitly mentioned. - */ - unsigned nFormulas = ctx.get_num_asserted_formulas(); - for (unsigned i = 0; i < nFormulas; ++i) { - expr * ex = ctx.get_asserted_formula(i); - set_up_axioms(ex); - } - - /* - * Similar recursive descent, except over all initially assigned terms. - * This is done to find equalities between terms, etc. that we otherwise - * might not get a chance to see. - */ - - /* - expr_ref_vector assignments(m); - ctx.get_assignments(assignments); - for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { - expr * ex = *i; - if (m.is_eq(ex)) { - TRACE("str", tout << "processing assignment " << mk_ismt2_pp(ex, m) << - ": expr is equality" << std::endl;); - app * eq = (app*)ex; - SASSERT(eq->get_num_args() == 2); - expr * lhs = eq->get_arg(0); - expr * rhs = eq->get_arg(1); - - enode * e_lhs = ctx.get_enode(lhs); - enode * e_rhs = ctx.get_enode(rhs); - std::pair eq_pair(e_lhs, e_rhs); - m_str_eq_todo.push_back(eq_pair); - } else { - TRACE("str", tout << "processing assignment " << mk_ismt2_pp(ex, m) - << ": expr ignored" << std::endl;); - } - } - */ - - // this might be cheating but we need to make sure that certain maps are populated - // before the first call to new_eq_eh() - propagate(); - - TRACE("str", tout << "search started" << std::endl;); - search_started = true; -} - -void theory_str::new_eq_eh(theory_var x, theory_var y) { - //TRACE("str", tout << "new eq: v#" << x << " = v#" << y << std::endl;); - TRACE("str", tout << "new eq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " = " << - mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); - - /* - if (m_find.find(x) == m_find.find(y)) { - return; - } - */ - handle_equality(get_enode(x)->get_owner(), get_enode(y)->get_owner()); - - // replicate Z3str2 behaviour: merge eqc **AFTER** handle_equality - m_find.merge(x, y); -} - -void theory_str::new_diseq_eh(theory_var x, theory_var y) { - //TRACE("str", tout << "new diseq: v#" << x << " != v#" << y << std::endl;); - TRACE("str", tout << "new diseq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " != " << - mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); -} - -void theory_str::relevant_eh(app * n) { - TRACE("str", tout << "relevant: " << mk_ismt2_pp(n, get_manager()) << std::endl;); -} - -void theory_str::assign_eh(bool_var v, bool is_true) { - context & ctx = get_context(); - TRACE("str", tout << "assert: v" << v << " #" << ctx.bool_var2expr(v)->get_id() << " is_true: " << is_true << std::endl;); -} - -void theory_str::push_scope_eh() { - theory::push_scope_eh(); - m_trail_stack.push_scope(); - - sLevel += 1; - TRACE("str", tout << "push to " << sLevel << std::endl;); - TRACE_CODE(if (is_trace_enabled("t_str_dump_assign_on_scope_change")) { dump_assignments(); }); -} - -void theory_str::recursive_check_variable_scope(expr * ex) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - if (is_app(ex)) { - app * a = to_app(ex); - if (a->get_num_args() == 0) { - // we only care about string variables - sort * s = m.get_sort(ex); - sort * string_sort = u.str.mk_string_sort(); - if (s != string_sort) { - return; - } - // base case: string constant / var - if (u.str.is_string(a)) { - return; - } else { - // assume var - if (variable_set.find(ex) == variable_set.end() - && internal_variable_set.find(ex) == internal_variable_set.end()) { - TRACE("str", tout << "WARNING: possible reference to out-of-scope variable " << mk_pp(ex, m) << std::endl;); - } - } - } else { - for (unsigned i = 0; i < a->get_num_args(); ++i) { - recursive_check_variable_scope(a->get_arg(i)); - } - } - } -} - -void theory_str::check_variable_scope() { - if (!opt_CheckVariableScope) { - return; - } - - if (!is_trace_enabled("t_str_detail")) { - return; - } - - TRACE("str", tout << "checking scopes of variables in the current assignment" << std::endl;); - - context & ctx = get_context(); - ast_manager & m = get_manager(); - - expr_ref_vector assignments(m); - ctx.get_assignments(assignments); - for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { - expr * ex = *i; - recursive_check_variable_scope(ex); - } -} - -void theory_str::pop_scope_eh(unsigned num_scopes) { - sLevel -= num_scopes; - TRACE("str", tout << "pop " << num_scopes << " to " << sLevel << std::endl;); - context & ctx = get_context(); - ast_manager & m = get_manager(); - - TRACE_CODE(if (is_trace_enabled("t_str_dump_assign_on_scope_change")) { dump_assignments(); }); - - // list of expr* to remove from cut_var_map - ptr_vector cutvarmap_removes; - - obj_map >::iterator varItor = cut_var_map.begin(); - while (varItor != cut_var_map.end()) { - expr * e = varItor->m_key; - std::stack & val = cut_var_map[varItor->m_key]; - while ((val.size() > 0) && (val.top()->level != 0) && (val.top()->level >= sLevel)) { - TRACE("str", tout << "remove cut info for " << mk_pp(e, m) << std::endl; print_cut_var(e, tout);); - T_cut * aCut = val.top(); - val.pop(); - // dealloc(aCut); - } - if (val.size() == 0) { - cutvarmap_removes.insert(varItor->m_key); - } - varItor++; - } - - if (!cutvarmap_removes.empty()) { - ptr_vector::iterator it = cutvarmap_removes.begin(); - for (; it != cutvarmap_removes.end(); ++it) { - expr * ex = *it; - cut_var_map.remove(ex); - } - } - - ptr_vector new_m_basicstr; - for (ptr_vector::iterator it = m_basicstr_axiom_todo.begin(); it != m_basicstr_axiom_todo.end(); ++it) { - enode * e = *it; - app * a = e->get_owner(); - TRACE("str", tout << "consider deleting " << mk_pp(a, get_manager()) - << ", enode scope level is " << e->get_iscope_lvl() - << std::endl;); - if (e->get_iscope_lvl() <= (unsigned)sLevel) { - new_m_basicstr.push_back(e); - } - } - m_basicstr_axiom_todo.reset(); - m_basicstr_axiom_todo = new_m_basicstr; - - m_trail_stack.pop_scope(num_scopes); - theory::pop_scope_eh(num_scopes); - - //check_variable_scope(); -} - -void theory_str::dump_assignments() { - TRACE_CODE( + void theory_str::solve_concat_eq_str(expr * concat, expr * str) { ast_manager & m = get_manager(); context & ctx = get_context(); - tout << "dumping all assignments:" << std::endl; - expr_ref_vector assignments(m); - ctx.get_assignments(assignments); - for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { - expr * ex = *i; - tout << mk_ismt2_pp(ex, m) << (ctx.is_relevant(ex) ? "" : " (NOT REL)") << std::endl; - } - ); -} -void theory_str::classify_ast_by_type(expr * node, std::map & varMap, - std::map & concatMap, std::map & unrollMap) { + TRACE("str", tout << mk_ismt2_pp(concat, m) << " == " << mk_ismt2_pp(str, m) << std::endl;); - // check whether the node is a string variable; - // testing set membership here bypasses several expensive checks. - // note that internal variables don't count if they're only length tester / value tester vars. - if (variable_set.find(node) != variable_set.end() - && internal_lenTest_vars.find(node) == internal_lenTest_vars.end() - && internal_valTest_vars.find(node) == internal_valTest_vars.end() - && internal_unrollTest_vars.find(node) == internal_unrollTest_vars.end()) { - if (varMap[node] != 1) { - TRACE("str", tout << "new variable: " << mk_pp(node, get_manager()) << std::endl;); - } - varMap[node] = 1; - } - // check whether the node is a function that we want to inspect - else if (is_app(node)) { - app * aNode = to_app(node); - if (u.str.is_length(aNode)) { - // Length - return; - } else if (u.str.is_concat(aNode)) { - expr * arg0 = aNode->get_arg(0); - expr * arg1 = aNode->get_arg(1); - bool arg0HasEq = false; - bool arg1HasEq = false; - expr * arg0Val = get_eqc_value(arg0, arg0HasEq); - expr * arg1Val = get_eqc_value(arg1, arg1HasEq); + zstring const_str; + if (u.str.is_concat(to_app(concat)) && u.str.is_string(to_app(str), const_str)) { + app * a_concat = to_app(concat); + SASSERT(a_concat->get_num_args() == 2); + expr * a1 = a_concat->get_arg(0); + expr * a2 = a_concat->get_arg(1); - int canskip = 0; - zstring tmp; - u.str.is_string(arg0Val, tmp); - if (arg0HasEq && tmp.empty()) { - canskip = 1; - } - u.str.is_string(arg1Val, tmp); - if (canskip == 0 && arg1HasEq && tmp.empty()) { - canskip = 1; - } - if (canskip == 0 && concatMap.find(node) == concatMap.end()) { - concatMap[node] = 1; - } - } else if (u.re.is_unroll(aNode)) { - // Unroll - if (unrollMap.find(node) == unrollMap.end()) { - unrollMap[node] = 1; - } - } - // recursively visit all arguments - for (unsigned i = 0; i < aNode->get_num_args(); ++i) { - expr * arg = aNode->get_arg(i); - classify_ast_by_type(arg, varMap, concatMap, unrollMap); - } - } -} + if (const_str.empty()) { + TRACE("str", tout << "quick path: concat == \"\"" << std::endl;); + // assert the following axiom: + // ( (Concat a1 a2) == "" ) -> ( (a1 == "") AND (a2 == "") ) -// NOTE: this function used to take an argument `Z3_ast node`; -// it was not used and so was removed from the signature -void theory_str::classify_ast_by_type_in_positive_context(std::map & varMap, - std::map & concatMap, std::map & unrollMap) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - expr_ref_vector assignments(m); - ctx.get_assignments(assignments); + expr_ref premise(ctx.mk_eq_atom(concat, str), m); + expr_ref c1(ctx.mk_eq_atom(a1, str), m); + expr_ref c2(ctx.mk_eq_atom(a2, str), m); + expr_ref conclusion(m.mk_and(c1, c2), m); + assert_implication(premise, conclusion); - for (expr_ref_vector::iterator it = assignments.begin(); it != assignments.end(); ++it) { - expr * argAst = *it; - // the original code jumped through some hoops to check whether the AST node - // is a function, then checked whether that function is "interesting". - // however, the only thing that's considered "interesting" is an equality predicate. - // so we bypass a huge amount of work by doing the following... - - if (m.is_eq(argAst)) { - TRACE("str", tout - << "eq ast " << mk_pp(argAst, m) << " is between args of sort " - << m.get_sort(to_app(argAst)->get_arg(0))->get_name() - << std::endl;); - classify_ast_by_type(argAst, varMap, concatMap, unrollMap); - } - } -} - -inline expr * theory_str::get_alias_index_ast(std::map & aliasIndexMap, expr * node) { - if (aliasIndexMap.find(node) != aliasIndexMap.end()) - return aliasIndexMap[node]; - else - return node; -} - -inline expr * theory_str::getMostLeftNodeInConcat(expr * node) { - app * aNode = to_app(node); - if (!u.str.is_concat(aNode)) { - return node; - } else { - expr * concatArgL = aNode->get_arg(0); - return getMostLeftNodeInConcat(concatArgL); - } -} - -inline expr * theory_str::getMostRightNodeInConcat(expr * node) { - app * aNode = to_app(node); - if (!u.str.is_concat(aNode)) { - return node; - } else { - expr * concatArgR = aNode->get_arg(1); - return getMostRightNodeInConcat(concatArgR); - } -} - -void theory_str::trace_ctx_dep(std::ofstream & tout, - std::map & aliasIndexMap, - std::map & var_eq_constStr_map, - std::map > & var_eq_concat_map, - std::map > & var_eq_unroll_map, - std::map & concat_eq_constStr_map, - std::map > & concat_eq_concat_map, - std::map > & unrollGroupMap) { -#ifdef _TRACE - context & ctx = get_context(); - ast_manager & mgr = get_manager(); - { - tout << "(0) alias: variables" << std::endl; - std::map > aliasSumMap; - std::map::iterator itor0 = aliasIndexMap.begin(); - for (; itor0 != aliasIndexMap.end(); itor0++) { - aliasSumMap[itor0->second][itor0->first] = 1; - } - std::map >::iterator keyItor = aliasSumMap.begin(); - for (; keyItor != aliasSumMap.end(); keyItor++) { - tout << " * "; - tout << mk_pp(keyItor->first, mgr); - tout << " : "; - std::map::iterator innerItor = keyItor->second.begin(); - for (; innerItor != keyItor->second.end(); innerItor++) { - tout << mk_pp(innerItor->first, mgr); - tout << ", "; + return; } - tout << std::endl; - } - tout << std::endl; - } - - { - tout << "(1) var = constStr:" << std::endl; - std::map::iterator itor1 = var_eq_constStr_map.begin(); - for (; itor1 != var_eq_constStr_map.end(); itor1++) { - tout << " * "; - tout << mk_pp(itor1->first, mgr); - tout << " = "; - tout << mk_pp(itor1->second, mgr); - if (!in_same_eqc(itor1->first, itor1->second)) { - tout << " (not true in ctx)"; - } - tout << std::endl; - } - tout << std::endl; - } - - { - tout << "(2) var = concat:" << std::endl; - std::map >::iterator itor2 = var_eq_concat_map.begin(); - for (; itor2 != var_eq_concat_map.end(); itor2++) { - tout << " * "; - tout << mk_pp(itor2->first, mgr); - tout << " = { "; - std::map::iterator i_itor = itor2->second.begin(); - for (; i_itor != itor2->second.end(); i_itor++) { - tout << mk_pp(i_itor->first, mgr); - tout << ", "; - } - tout << std::endl; - } - tout << std::endl; - } - - { - tout << "(3) var = unrollFunc:" << std::endl; - std::map >::iterator itor2 = var_eq_unroll_map.begin(); - for (; itor2 != var_eq_unroll_map.end(); itor2++) { - tout << " * " << mk_pp(itor2->first, mgr) << " = { "; - std::map::iterator i_itor = itor2->second.begin(); - for (; i_itor != itor2->second.end(); i_itor++) { - tout << mk_pp(i_itor->first, mgr) << ", "; - } - tout << " }" << std::endl; - } - tout << std::endl; - } - - { - tout << "(4) concat = constStr:" << std::endl; - std::map::iterator itor3 = concat_eq_constStr_map.begin(); - for (; itor3 != concat_eq_constStr_map.end(); itor3++) { - tout << " * "; - tout << mk_pp(itor3->first, mgr); - tout << " = "; - tout << mk_pp(itor3->second, mgr); - tout << std::endl; - - } - tout << std::endl; - } - - { - tout << "(5) eq concats:" << std::endl; - std::map >::iterator itor4 = concat_eq_concat_map.begin(); - for (; itor4 != concat_eq_concat_map.end(); itor4++) { - if (itor4->second.size() > 1) { - std::map::iterator i_itor = itor4->second.begin(); - tout << " * "; - for (; i_itor != itor4->second.end(); i_itor++) { - tout << mk_pp(i_itor->first, mgr); - tout << " , "; + bool arg1_has_eqc_value = false; + bool arg2_has_eqc_value = false; + expr * arg1 = get_eqc_value(a1, arg1_has_eqc_value); + expr * arg2 = get_eqc_value(a2, arg2_has_eqc_value); + expr_ref newConcat(m); + if (arg1 != a1 || arg2 != a2) { + TRACE("str", tout << "resolved concat argument(s) to eqc string constants" << std::endl;); + int iPos = 0; + expr_ref_vector item1(m); + if (a1 != arg1) { + item1.push_back(ctx.mk_eq_atom(a1, arg1)); + iPos += 1; } - tout << std::endl; - } - } - tout << std::endl; - } - - { - tout << "(6) eq unrolls:" << std::endl; - std::map >::iterator itor5 = unrollGroupMap.begin(); - for (; itor5 != unrollGroupMap.end(); itor5++) { - tout << " * "; - std::set::iterator i_itor = itor5->second.begin(); - for (; i_itor != itor5->second.end(); i_itor++) { - tout << mk_pp(*i_itor, mgr) << ", "; - } - tout << std::endl; - } - tout << std::endl; - } - - { - tout << "(7) unroll = concats:" << std::endl; - std::map >::iterator itor5 = unrollGroupMap.begin(); - for (; itor5 != unrollGroupMap.end(); itor5++) { - tout << " * "; - expr * unroll = itor5->first; - tout << mk_pp(unroll, mgr) << std::endl; - enode * e_curr = ctx.get_enode(unroll); - enode * e_curr_end = e_curr; - do { - app * curr = e_curr->get_owner(); - if (u.str.is_concat(curr)) { - tout << " >>> " << mk_pp(curr, mgr) << std::endl; + if (a2 != arg2) { + item1.push_back(ctx.mk_eq_atom(a2, arg2)); + iPos += 1; } - e_curr = e_curr->get_next(); - } while (e_curr != e_curr_end); - tout << std::endl; - } - tout << std::endl; - } -#else - return; -#endif // _TRACE -} - - -/* - * Dependence analysis from current context assignment - * - "freeVarMap" contains a set of variables that doesn't constrained by Concats. - * But it's possible that it's bounded by unrolls - * For the case of - * (1) var1 = unroll(r1, t1) - * var1 is in the freeVarMap - * > should unroll r1 for var1 - * (2) var1 = unroll(r1, t1) /\ var1 = Concat(var2, var3) - * var2, var3 are all in freeVar - * > should split the unroll function so that var2 and var3 are bounded by new unrolls - */ -int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map & freeVarMap, - std::map > & unrollGroupMap, std::map > & var_eq_concat_map) { - std::map concatMap; - std::map unrollMap; - std::map aliasIndexMap; - std::map var_eq_constStr_map; - std::map concat_eq_constStr_map; - std::map > var_eq_unroll_map; - std::map > concat_eq_concat_map; - std::map > depMap; - - context & ctx = get_context(); - ast_manager & m = get_manager(); - - // note that the old API concatenated these assignments into - // a massive conjunction; we may have the opportunity to avoid that here - expr_ref_vector assignments(m); - ctx.get_assignments(assignments); - - // Step 1: get variables / concat AST appearing in the context - // the thing we iterate over should just be variable_set - internal_variable_set - // so we avoid computing the set difference (but this might be slower) - for(obj_hashtable::iterator it = variable_set.begin(); it != variable_set.end(); ++it) { - expr* var = *it; - if (internal_variable_set.find(var) == internal_variable_set.end()) { - TRACE("str", tout << "new variable: " << mk_pp(var, m) << std::endl;); - strVarMap[*it] = 1; - } - } - classify_ast_by_type_in_positive_context(strVarMap, concatMap, unrollMap); - - std::map aliasUnrollSet; - std::map::iterator unrollItor = unrollMap.begin(); - for (; unrollItor != unrollMap.end(); ++unrollItor) { - if (aliasUnrollSet.find(unrollItor->first) != aliasUnrollSet.end()) { - continue; - } - expr * aRoot = NULL; - enode * e_currEqc = ctx.get_enode(unrollItor->first); - enode * e_curr = e_currEqc; - do { - app * curr = e_currEqc->get_owner(); - if (u.re.is_unroll(curr)) { - if (aRoot == NULL) { - aRoot = curr; - } - aliasUnrollSet[curr] = aRoot; - } - e_currEqc = e_currEqc->get_next(); - } while (e_currEqc != e_curr); - } - - for (unrollItor = unrollMap.begin(); unrollItor != unrollMap.end(); unrollItor++) { - expr * unrFunc = unrollItor->first; - expr * urKey = aliasUnrollSet[unrFunc]; - unrollGroupMap[urKey].insert(unrFunc); - } - - // Step 2: collect alias relation - // e.g. suppose we have the equivalence class {x, y, z}; - // then we set aliasIndexMap[y] = x - // and aliasIndexMap[z] = x - - std::map::iterator varItor = strVarMap.begin(); - for (; varItor != strVarMap.end(); ++varItor) { - if (aliasIndexMap.find(varItor->first) != aliasIndexMap.end()) { - continue; - } - expr * aRoot = NULL; - expr * curr = varItor->first; - do { - if (variable_set.find(curr) != variable_set.end()) { - if (aRoot == NULL) { - aRoot = curr; - } else { - aliasIndexMap[curr] = aRoot; - } - } - curr = get_eqc_next(curr); - } while (curr != varItor->first); - } - - // Step 3: Collect interested cases - - varItor = strVarMap.begin(); - for (; varItor != strVarMap.end(); ++varItor) { - expr * deAliasNode = get_alias_index_ast(aliasIndexMap, varItor->first); - // Case 1: variable = string constant - // e.g. z = "str1" ::= var_eq_constStr_map[z] = "str1" - - if (var_eq_constStr_map.find(deAliasNode) == var_eq_constStr_map.end()) { - bool nodeHasEqcValue = false; - expr * nodeValue = get_eqc_value(deAliasNode, nodeHasEqcValue); - if (nodeHasEqcValue) { - var_eq_constStr_map[deAliasNode] = nodeValue; - } - } - - // Case 2: var_eq_concat - // e.g. z = concat("str1", b) ::= var_eq_concat[z][concat(c, "str2")] = 1 - // var_eq_unroll - // e.g. z = unroll(...) ::= var_eq_unroll[z][unroll(...)] = 1 - - if (var_eq_concat_map.find(deAliasNode) == var_eq_concat_map.end()) { - expr * curr = get_eqc_next(deAliasNode); - while (curr != deAliasNode) { - app * aCurr = to_app(curr); - // collect concat - if (u.str.is_concat(aCurr)) { - expr * arg0 = aCurr->get_arg(0); - expr * arg1 = aCurr->get_arg(1); - bool arg0HasEqcValue = false; - bool arg1HasEqcValue = false; - expr * arg0_value = get_eqc_value(arg0, arg0HasEqcValue); - expr * arg1_value = get_eqc_value(arg1, arg1HasEqcValue); - - bool is_arg0_emptyStr = false; - if (arg0HasEqcValue) { - zstring strval; - u.str.is_string(arg0_value, strval); - if (strval.empty()) { - is_arg0_emptyStr = true; - } - } - - bool is_arg1_emptyStr = false; - if (arg1HasEqcValue) { - zstring strval; - u.str.is_string(arg1_value, strval); - if (strval.empty()) { - is_arg1_emptyStr = true; - } - } - - if (!is_arg0_emptyStr && !is_arg1_emptyStr) { - var_eq_concat_map[deAliasNode][curr] = 1; - } - } else if (u.re.is_unroll(to_app(curr))) { - var_eq_unroll_map[deAliasNode][curr] = 1; - } - - curr = get_eqc_next(curr); - } - } - - } // for(varItor in strVarMap) - - // -------------------------------------------------- - // * collect aliasing relation among eq concats - // e.g EQC={concat1, concat2, concat3} - // concats_eq_Index_map[concat2] = concat1 - // concats_eq_Index_map[concat3] = concat1 - // -------------------------------------------------- - - std::map concats_eq_index_map; - std::map::iterator concatItor = concatMap.begin(); - for(; concatItor != concatMap.end(); ++concatItor) { - if (concats_eq_index_map.find(concatItor->first) != concats_eq_index_map.end()) { - continue; - } - expr * aRoot = NULL; - expr * curr = concatItor->first; - do { - if (u.str.is_concat(to_app(curr))) { - if (aRoot == NULL) { - aRoot = curr; - } else { - concats_eq_index_map[curr] = aRoot; - } - } - curr = get_eqc_next(curr); - } while (curr != concatItor->first); - } - - concatItor = concatMap.begin(); - for(; concatItor != concatMap.end(); ++concatItor) { - expr * deAliasConcat = NULL; - if (concats_eq_index_map.find(concatItor->first) != concats_eq_index_map.end()) { - deAliasConcat = concats_eq_index_map[concatItor->first]; - } else { - deAliasConcat = concatItor->first; - } - - // (3) concat_eq_conststr, e.g. concat(a,b) = "str1" - if (concat_eq_constStr_map.find(deAliasConcat) == concat_eq_constStr_map.end()) { - bool nodeHasEqcValue = false; - expr * nodeValue = get_eqc_value(deAliasConcat, nodeHasEqcValue); - if (nodeHasEqcValue) { - concat_eq_constStr_map[deAliasConcat] = nodeValue; - } - } - - // (4) concat_eq_concat, e.g. - // concat(a,b) = concat("str1", c) AND z = concat(a,b) AND z = concat(e,f) - if (concat_eq_concat_map.find(deAliasConcat) == concat_eq_concat_map.end()) { - expr * curr = deAliasConcat; - do { - if (u.str.is_concat(to_app(curr))) { - // curr cannot be reduced - if (concatMap.find(curr) != concatMap.end()) { - concat_eq_concat_map[deAliasConcat][curr] = 1; - } - } - curr = get_eqc_next(curr); - } while (curr != deAliasConcat); - } - } - - // print some debugging info - TRACE("str", trace_ctx_dep(tout, aliasIndexMap, var_eq_constStr_map, - var_eq_concat_map, var_eq_unroll_map, - concat_eq_constStr_map, concat_eq_concat_map, unrollGroupMap);); - - if (!contain_pair_bool_map.empty()) { - compute_contains(aliasIndexMap, concats_eq_index_map, var_eq_constStr_map, concat_eq_constStr_map, var_eq_concat_map); - } - - // step 4: dependence analysis - - // (1) var = string constant - for (std::map::iterator itor = var_eq_constStr_map.begin(); - itor != var_eq_constStr_map.end(); ++itor) { - expr * var = get_alias_index_ast(aliasIndexMap, itor->first); - expr * strAst = itor->second; - depMap[var][strAst] = 1; - } - - // (2) var = concat - for (std::map >::iterator itor = var_eq_concat_map.begin(); - itor != var_eq_concat_map.end(); ++itor) { - expr * var = get_alias_index_ast(aliasIndexMap, itor->first); - for (std::map::iterator itor1 = itor->second.begin(); itor1 != itor->second.end(); ++itor1) { - expr * concat = itor1->first; - std::map inVarMap; - std::map inConcatMap; - std::map inUnrollMap; - classify_ast_by_type(concat, inVarMap, inConcatMap, inUnrollMap); - for (std::map::iterator itor2 = inVarMap.begin(); itor2 != inVarMap.end(); ++itor2) { - expr * varInConcat = get_alias_index_ast(aliasIndexMap, itor2->first); - if (!(depMap[var].find(varInConcat) != depMap[var].end() && depMap[var][varInConcat] == 1)) { - depMap[var][varInConcat] = 2; - } - } - } - } - - for (std::map >::iterator itor = var_eq_unroll_map.begin(); - itor != var_eq_unroll_map.end(); itor++) { - expr * var = get_alias_index_ast(aliasIndexMap, itor->first); - for (std::map::iterator itor1 = itor->second.begin(); itor1 != itor->second.end(); itor1++) { - expr * unrollFunc = itor1->first; - std::map inVarMap; - std::map inConcatMap; - std::map inUnrollMap; - classify_ast_by_type(unrollFunc, inVarMap, inConcatMap, inUnrollMap); - for (std::map::iterator itor2 = inVarMap.begin(); itor2 != inVarMap.end(); itor2++) { - expr * varInFunc = get_alias_index_ast(aliasIndexMap, itor2->first); - - TRACE("str", tout << "var in unroll = " << - mk_ismt2_pp(itor2->first, m) << std::endl - << "dealiased var = " << mk_ismt2_pp(varInFunc, m) << std::endl;); - - // it's possible that we have both (Unroll $$_regVar_0 $$_unr_0) /\ (Unroll abcd $$_unr_0), - // while $$_regVar_0 = "abcd" - // have to exclude such cases - bool varHasValue = false; - get_eqc_value(varInFunc, varHasValue); - if (varHasValue) - continue; - - if (depMap[var].find(varInFunc) == depMap[var].end()) { - depMap[var][varInFunc] = 6; - } - } - } - } - - // (3) concat = string constant - for (std::map::iterator itor = concat_eq_constStr_map.begin(); - itor != concat_eq_constStr_map.end(); itor++) { - expr * concatAst = itor->first; - expr * constStr = itor->second; - std::map inVarMap; - std::map inConcatMap; - std::map inUnrollMap; - classify_ast_by_type(concatAst, inVarMap, inConcatMap, inUnrollMap); - for (std::map::iterator itor2 = inVarMap.begin(); itor2 != inVarMap.end(); itor2++) { - expr * varInConcat = get_alias_index_ast(aliasIndexMap, itor2->first); - if (!(depMap[varInConcat].find(constStr) != depMap[varInConcat].end() && depMap[varInConcat][constStr] == 1)) - depMap[varInConcat][constStr] = 3; - } - } - - // (4) equivalent concats - // - possibility 1 : concat("str", v1) = concat(concat(v2, v3), v4) = concat(v5, v6) - // ==> v2, v5 are constrained by "str" - // - possibility 2 : concat(v1, "str") = concat(v2, v3) = concat(v4, v5) - // ==> v2, v4 are constrained by "str" - //-------------------------------------------------------------- - - std::map mostLeftNodes; - std::map mostRightNodes; - - std::map mLIdxMap; - std::map > mLMap; - std::map mRIdxMap; - std::map > mRMap; - std::set nSet; - - for (std::map >::iterator itor = concat_eq_concat_map.begin(); - itor != concat_eq_concat_map.end(); itor++) { - mostLeftNodes.clear(); - mostRightNodes.clear(); - - expr * mLConst = NULL; - expr * mRConst = NULL; - - for (std::map::iterator itor1 = itor->second.begin(); itor1 != itor->second.end(); itor1++) { - expr * concatNode = itor1->first; - expr * mLNode = getMostLeftNodeInConcat(concatNode); - zstring strval; - if (u.str.is_string(to_app(mLNode), strval)) { - if (mLConst == NULL && strval.empty()) { - mLConst = mLNode; - } - } else { - mostLeftNodes[mLNode] = concatNode; - } - - expr * mRNode = getMostRightNodeInConcat(concatNode); - if (u.str.is_string(to_app(mRNode), strval)) { - if (mRConst == NULL && strval.empty()) { - mRConst = mRNode; - } - } else { - mostRightNodes[mRNode] = concatNode; - } - } - - if (mLConst != NULL) { - // ------------------------------------------------------------------------------------- - // The left most variable in a concat is constrained by a constant string in eqc concat - // ------------------------------------------------------------------------------------- - // e.g. Concat(x, ...) = Concat("abc", ...) - // ------------------------------------------------------------------------------------- - for (std::map::iterator itor1 = mostLeftNodes.begin(); - itor1 != mostLeftNodes.end(); itor1++) { - expr * deVar = get_alias_index_ast(aliasIndexMap, itor1->first); - if (depMap[deVar].find(mLConst) == depMap[deVar].end() || depMap[deVar][mLConst] != 1) { - depMap[deVar][mLConst] = 4; - } - } - } - - { - // ------------------------------------------------------------------------------------- - // The left most variables in eqc concats are constrained by each other - // ------------------------------------------------------------------------------------- - // e.g. concat(x, ...) = concat(u, ...) = ... - // x and u are constrained by each other - // ------------------------------------------------------------------------------------- - nSet.clear(); - std::map::iterator itl = mostLeftNodes.begin(); - for (; itl != mostLeftNodes.end(); itl++) { - bool lfHasEqcValue = false; - get_eqc_value(itl->first, lfHasEqcValue); - if (lfHasEqcValue) - continue; - expr * deVar = get_alias_index_ast(aliasIndexMap, itl->first); - nSet.insert(deVar); - } - - if (nSet.size() > 1) { - int lId = -1; - for (std::set::iterator itor2 = nSet.begin(); itor2 != nSet.end(); itor2++) { - if (mLIdxMap.find(*itor2) != mLIdxMap.end()) { - lId = mLIdxMap[*itor2]; - break; - } - } - if (lId == -1) - lId = mLMap.size(); - for (std::set::iterator itor2 = nSet.begin(); itor2 != nSet.end(); itor2++) { - bool itorHasEqcValue = false; - get_eqc_value(*itor2, itorHasEqcValue); - if (itorHasEqcValue) - continue; - mLIdxMap[*itor2] = lId; - mLMap[lId].insert(*itor2); - } - } - } - - if (mRConst != NULL) { - for (std::map::iterator itor1 = mostRightNodes.begin(); - itor1 != mostRightNodes.end(); itor1++) { - expr * deVar = get_alias_index_ast(aliasIndexMap, itor1->first); - if (depMap[deVar].find(mRConst) == depMap[deVar].end() || depMap[deVar][mRConst] != 1) { - depMap[deVar][mRConst] = 5; - } - } - } - - { - nSet.clear(); - std::map::iterator itr = mostRightNodes.begin(); - for (; itr != mostRightNodes.end(); itr++) { - expr * deVar = get_alias_index_ast(aliasIndexMap, itr->first); - nSet.insert(deVar); - } - if (nSet.size() > 1) { - int rId = -1; - std::set::iterator itor2 = nSet.begin(); - for (; itor2 != nSet.end(); itor2++) { - if (mRIdxMap.find(*itor2) != mRIdxMap.end()) { - rId = mRIdxMap[*itor2]; - break; - } - } - if (rId == -1) - rId = mRMap.size(); - for (itor2 = nSet.begin(); itor2 != nSet.end(); itor2++) { - bool rHasEqcValue = false; - get_eqc_value(*itor2, rHasEqcValue); - if (rHasEqcValue) - continue; - mRIdxMap[*itor2] = rId; - mRMap[rId].insert(*itor2); - } - } - } - } - - // print the dependence map - TRACE("str", - tout << "Dependence Map" << std::endl; - for(std::map >::iterator itor = depMap.begin(); itor != depMap.end(); itor++) { - tout << mk_pp(itor->first, m); - rational nnLen; - bool nnLen_exists = get_len_value(itor->first, nnLen); - tout << " [len = " << (nnLen_exists ? nnLen.to_string() : "?") << "] \t-->\t"; - for (std::map::iterator itor1 = itor->second.begin(); itor1 != itor->second.end(); itor1++) { - tout << mk_pp(itor1->first, m) << "(" << itor1->second << "), "; - } - tout << std::endl; - } - ); - - // step, errr, 5: compute free variables based on the dependence map - - // the case dependence map is empty, every var in VarMap is free - //--------------------------------------------------------------- - // remove L/R most var in eq concat since they are constrained with each other - std::map > lrConstrainedMap; - for (std::map >::iterator itor = mLMap.begin(); itor != mLMap.end(); itor++) { - for (std::set::iterator it1 = itor->second.begin(); it1 != itor->second.end(); it1++) { - std::set::iterator it2 = it1; - it2++; - for (; it2 != itor->second.end(); it2++) { - expr * n1 = *it1; - expr * n2 = *it2; - lrConstrainedMap[n1][n2] = 1; - lrConstrainedMap[n2][n1] = 1; - } - } - } - for (std::map >::iterator itor = mRMap.begin(); itor != mRMap.end(); itor++) { - for (std::set::iterator it1 = itor->second.begin(); it1 != itor->second.end(); it1++) { - std::set::iterator it2 = it1; - it2++; - for (; it2 != itor->second.end(); it2++) { - expr * n1 = *it1; - expr * n2 = *it2; - lrConstrainedMap[n1][n2] = 1; - lrConstrainedMap[n2][n1] = 1; - } - } - } - - if (depMap.size() == 0) { - std::map::iterator itor = strVarMap.begin(); - for (; itor != strVarMap.end(); itor++) { - expr * var = get_alias_index_ast(aliasIndexMap, itor->first); - if (lrConstrainedMap.find(var) == lrConstrainedMap.end()) { - freeVarMap[var] = 1; - } else { - int lrConstainted = 0; - std::map::iterator lrit = freeVarMap.begin(); - for (; lrit != freeVarMap.end(); lrit++) { - if (lrConstrainedMap[var].find(lrit->first) != lrConstrainedMap[var].end()) { - lrConstainted = 1; - break; - } - } - if (lrConstainted == 0) { - freeVarMap[var] = 1; - } - } - } - } else { - // if the keys in aliasIndexMap are not contained in keys in depMap, they are free - // e.g., x= y /\ x = z /\ t = "abc" - // aliasIndexMap[y]= x, aliasIndexMap[z] = x - // depMap t ~ "abc"(1) - // x should be free - std::map::iterator itor2 = strVarMap.begin(); - for (; itor2 != strVarMap.end(); itor2++) { - if (aliasIndexMap.find(itor2->first) != aliasIndexMap.end()) { - expr * var = aliasIndexMap[itor2->first]; - if (depMap.find(var) == depMap.end()) { - if (lrConstrainedMap.find(var) == lrConstrainedMap.end()) { - freeVarMap[var] = 1; - } else { - int lrConstainted = 0; - std::map::iterator lrit = freeVarMap.begin(); - for (; lrit != freeVarMap.end(); lrit++) { - if (lrConstrainedMap[var].find(lrit->first) != lrConstrainedMap[var].end()) { - lrConstainted = 1; - break; - } - } - if (lrConstainted == 0) { - freeVarMap[var] = 1; - } - } - } - } else if (aliasIndexMap.find(itor2->first) == aliasIndexMap.end()) { - // if a variable is not in aliasIndexMap and not in depMap, it's free - if (depMap.find(itor2->first) == depMap.end()) { - expr * var = itor2->first; - if (lrConstrainedMap.find(var) == lrConstrainedMap.end()) { - freeVarMap[var] = 1; - } else { - int lrConstainted = 0; - std::map::iterator lrit = freeVarMap.begin(); - for (; lrit != freeVarMap.end(); lrit++) { - if (lrConstrainedMap[var].find(lrit->first) != lrConstrainedMap[var].end()) { - lrConstainted = 1; - break; - } - } - if (lrConstainted == 0) { - freeVarMap[var] = 1; - } - } - } - } - } - - std::map >::iterator itor = depMap.begin(); - for (; itor != depMap.end(); itor++) { - for (std::map::iterator itor1 = itor->second.begin(); itor1 != itor->second.end(); itor1++) { - if (variable_set.find(itor1->first) != variable_set.end()) { // expr type = var - expr * var = get_alias_index_ast(aliasIndexMap, itor1->first); - // if a var is dep on itself and all dependence are type 2, it's a free variable - // e.g {y --> x(2), y(2), m --> m(2), n(2)} y,m are free - { - if (depMap.find(var) == depMap.end()) { - if (freeVarMap.find(var) == freeVarMap.end()) { - if (lrConstrainedMap.find(var) == lrConstrainedMap.end()) { - freeVarMap[var] = 1; - } else { - int lrConstainted = 0; - std::map::iterator lrit = freeVarMap.begin(); - for (; lrit != freeVarMap.end(); lrit++) { - if (lrConstrainedMap[var].find(lrit->first) != lrConstrainedMap[var].end()) { - lrConstainted = 1; - break; - } - } - if (lrConstainted == 0) { - freeVarMap[var] = 1; - } - } - - } else { - freeVarMap[var] = freeVarMap[var] + 1; - } - } - } - } - } - } - } - - return 0; -} - -// Check agreement between integer and string theories for the term a = (str.to-int S). -// Returns true if axioms were added, and false otherwise. -bool theory_str::finalcheck_str2int(app * a) { - bool axiomAdd = false; - context & ctx = get_context(); - ast_manager & m = get_manager(); - - expr * S = a->get_arg(0); - - // check integer theory - rational Ival; - bool Ival_exists = get_value(a, Ival); - if (Ival_exists) { - TRACE("str", tout << "integer theory assigns " << mk_pp(a, m) << " = " << Ival.to_string() << std::endl;); - // if that value is not -1, we can assert (str.to-int S) = Ival --> S = "Ival" - if (!Ival.is_minus_one()) { - zstring Ival_str(Ival.to_string().c_str()); - expr_ref premise(ctx.mk_eq_atom(a, m_autil.mk_numeral(Ival, true)), m); - expr_ref conclusion(ctx.mk_eq_atom(S, mk_string(Ival_str)), m); - expr_ref axiom(rewrite_implication(premise, conclusion), m); - if (!string_int_axioms.contains(axiom)) { - string_int_axioms.insert(axiom); - assert_axiom(axiom); - m_trail_stack.push(insert_obj_trail(string_int_axioms, axiom)); - axiomAdd = true; - } - } - } else { - TRACE("str", tout << "integer theory has no assignment for " << mk_pp(a, m) << std::endl;); - NOT_IMPLEMENTED_YET(); - } - - return axiomAdd; -} - -bool theory_str::finalcheck_int2str(app * a) { - bool axiomAdd = false; - context & ctx = get_context(); - ast_manager & m = get_manager(); - - expr * N = a->get_arg(0); - - // check string theory - bool Sval_expr_exists; - expr * Sval_expr = get_eqc_value(a, Sval_expr_exists); - if (Sval_expr_exists) { - zstring Sval; - u.str.is_string(Sval_expr, Sval); - TRACE("str", tout << "string theory assigns \"" << mk_pp(a, m) << " = " << Sval << "\n";); - // empty string --> integer value < 0 - if (Sval.empty()) { - // ignore this. we should already assert the axiom for what happens when the string is "" - } else { - // nonempty string --> convert to correct integer value, or disallow it - rational convertedRepresentation(0); - rational ten(10); - bool conversionOK = true; - for (unsigned i = 0; i < Sval.length(); ++i) { - char digit = (int)Sval[i]; - if (isdigit((int)digit)) { - std::string sDigit(1, digit); - int val = atoi(sDigit.c_str()); - convertedRepresentation = (ten * convertedRepresentation) + rational(val); - } else { - // not a digit, invalid - TRACE("str", tout << "str.to-int argument contains non-digit character '" << digit << "'" << std::endl;); - conversionOK = false; - break; - } - } - if (conversionOK) { - expr_ref premise(ctx.mk_eq_atom(a, mk_string(Sval)), m); - expr_ref conclusion(ctx.mk_eq_atom(N, m_autil.mk_numeral(convertedRepresentation, true)), m); - expr_ref axiom(rewrite_implication(premise, conclusion), m); - if (!string_int_axioms.contains(axiom)) { - string_int_axioms.insert(axiom); - assert_axiom(axiom); - m_trail_stack.push(insert_obj_trail(string_int_axioms, axiom)); - axiomAdd = true; + expr_ref implyL1(mk_and(item1), m); + newConcat = mk_concat(arg1, arg2); + if (newConcat != str) { + expr_ref implyR1(ctx.mk_eq_atom(concat, newConcat), m); + assert_implication(implyL1, implyR1); } } else { - expr_ref axiom(m.mk_not(ctx.mk_eq_atom(a, mk_string(Sval))), m); - // always assert this axiom because this is a conflict clause - assert_axiom(axiom); - axiomAdd = true; + newConcat = concat; } - } - } else { - TRACE("str", tout << "string theory has no assignment for " << mk_pp(a, m) << std::endl;); - NOT_IMPLEMENTED_YET(); - } - return axiomAdd; -} - -void theory_str::collect_var_concat(expr * node, std::set & varSet, std::set & concatSet) { - if (variable_set.find(node) != variable_set.end()) { - if (internal_lenTest_vars.find(node) == internal_lenTest_vars.end()) { - varSet.insert(node); - } - } - else if (is_app(node)) { - app * aNode = to_app(node); - if (u.str.is_length(aNode)) { - // Length - return; - } - if (u.str.is_concat(aNode)) { - expr * arg0 = aNode->get_arg(0); - expr * arg1 = aNode->get_arg(1); - if (concatSet.find(node) == concatSet.end()) { - concatSet.insert(node); + if (newConcat == str) { + return; } - } - // recursively visit all arguments - for (unsigned i = 0; i < aNode->get_num_args(); ++i) { - expr * arg = aNode->get_arg(i); - collect_var_concat(arg, varSet, concatSet); - } - } -} - -bool theory_str::propagate_length_within_eqc(expr * var) { - bool res = false; - ast_manager & m = get_manager(); - context & ctx = get_context(); - - TRACE("str", tout << "propagate_length_within_eqc: " << mk_ismt2_pp(var, m) << std::endl ;); - - enode * n_eq_enode = ctx.get_enode(var); - rational varLen; - if (! get_len_value(var, varLen)) { - bool hasLen = false; - expr * nodeWithLen= var; - do { - if (get_len_value(nodeWithLen, varLen)) { - hasLen = true; - break; + if (!u.str.is_concat(to_app(newConcat))) { + return; } - nodeWithLen = get_eqc_next(nodeWithLen); - } while (nodeWithLen != var); + if (arg1_has_eqc_value && arg2_has_eqc_value) { + // Case 1: Concat(const, const) == const + TRACE("str", tout << "Case 1: Concat(const, const) == const" << std::endl;); + zstring arg1_str, arg2_str; + u.str.is_string(arg1, arg1_str); + u.str.is_string(arg2, arg2_str); - if (hasLen) { - // var = nodeWithLen --> |var| = |nodeWithLen| - expr_ref_vector l_items(m); - expr_ref varEqNode(ctx.mk_eq_atom(var, nodeWithLen), m); - l_items.push_back(varEqNode); - - expr_ref nodeWithLenExpr (mk_strlen(nodeWithLen), m); - expr_ref varLenExpr (mk_int(varLen), m); - expr_ref lenEqNum(ctx.mk_eq_atom(nodeWithLenExpr, varLenExpr), m); - l_items.push_back(lenEqNum); - - expr_ref axl(m.mk_and(l_items.size(), l_items.c_ptr()), m); - expr_ref varLen(mk_strlen(var), m); - expr_ref axr(ctx.mk_eq_atom(varLen, mk_int(varLen)), m); - assert_implication(axl, axr); - TRACE("str", tout << mk_ismt2_pp(axl, m) << std::endl << " ---> " << std::endl << mk_ismt2_pp(axr, m);); - res = true; - } - } - return res; -} - -bool theory_str::propagate_length(std::set & varSet, std::set & concatSet, std::map & exprLenMap) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - expr_ref_vector assignments(m); - ctx.get_assignments(assignments); - bool axiomAdded = false; - // collect all concats in context - for (expr_ref_vector::iterator it = assignments.begin(); it != assignments.end(); ++it) { - if (! ctx.is_relevant(*it)) { - continue; - } - if (m.is_eq(*it)) { - collect_var_concat(*it, varSet, concatSet); - } - } - // iterate each concat - // if a concat doesn't have length info, check if the length of all leaf nodes can be resolved - for (std::set::iterator it = concatSet.begin(); it != concatSet.end(); it++) { - expr * concat = *it; - rational lenValue; - expr_ref concatlenExpr (mk_strlen(concat), m) ; - bool allLeafResolved = true; - if (! get_value(concatlenExpr, lenValue)) { - // the length fo concat is unresolved yet - if (get_len_value(concat, lenValue)) { - // but all leaf nodes have length information - TRACE("str", tout << "* length pop-up: " << mk_ismt2_pp(concat, m) << "| = " << lenValue << std::endl;); - std::set leafNodes; - get_unique_non_concat_nodes(concat, leafNodes); - expr_ref_vector l_items(m); - for (std::set::iterator leafIt = leafNodes.begin(); leafIt != leafNodes.end(); ++leafIt) { - rational leafLenValue; - if (get_len_value(*leafIt, leafLenValue)) { - expr_ref leafItLenExpr (mk_strlen(*leafIt), m); - expr_ref leafLenValueExpr (mk_int(leafLenValue), m); - expr_ref lcExpr (ctx.mk_eq_atom(leafItLenExpr, leafLenValueExpr), m); - l_items.push_back(lcExpr); + zstring result_str = arg1_str + arg2_str; + if (result_str != const_str) { + // Inconsistency + TRACE("str", tout << "inconsistency detected: \"" + << arg1_str << "\" + \"" << arg2_str << + "\" != \"" << const_str << "\"" << "\n";); + expr_ref equality(ctx.mk_eq_atom(concat, str), m); + expr_ref diseq(m.mk_not(equality), m); + assert_axiom(diseq); + return; + } + } else if (!arg1_has_eqc_value && arg2_has_eqc_value) { + // Case 2: Concat(var, const) == const + TRACE("str", tout << "Case 2: Concat(var, const) == const" << std::endl;); + zstring arg2_str; + u.str.is_string(arg2, arg2_str); + unsigned int resultStrLen = const_str.length(); + unsigned int arg2StrLen = arg2_str.length(); + if (resultStrLen < arg2StrLen) { + // Inconsistency + TRACE("str", tout << "inconsistency detected: \"" + << arg2_str << + "\" is longer than \"" << const_str << "\"," + << " so cannot be concatenated with anything to form it" << "\n";); + expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); + expr_ref diseq(m.mk_not(equality), m); + assert_axiom(diseq); + return; + } else { + int varStrLen = resultStrLen - arg2StrLen; + zstring firstPart = const_str.extract(0, varStrLen); + zstring secondPart = const_str.extract(varStrLen, arg2StrLen); + if (arg2_str != secondPart) { + // Inconsistency + TRACE("str", tout << "inconsistency detected: " + << "suffix of concatenation result expected \"" << secondPart << "\", " + << "actually \"" << arg2_str << "\"" + << "\n";); + expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); + expr_ref diseq(m.mk_not(equality), m); + assert_axiom(diseq); + return; } else { - allLeafResolved = false; - break; + expr_ref tmpStrConst(mk_string(firstPart), m); + expr_ref premise(ctx.mk_eq_atom(newConcat, str), m); + expr_ref conclusion(ctx.mk_eq_atom(arg1, tmpStrConst), m); + assert_implication(premise, conclusion); + return; } } - if (allLeafResolved) { - expr_ref axl(m.mk_and(l_items.size(), l_items.c_ptr()), m); - expr_ref lenValueExpr (mk_int(lenValue), m); - expr_ref axr(ctx.mk_eq_atom(concatlenExpr, lenValueExpr), m); - assert_implication(axl, axr); - TRACE("str", tout << mk_ismt2_pp(axl, m) << std::endl << " ---> " << std::endl << mk_ismt2_pp(axr, m)<< std::endl;); - axiomAdded = true; + } else if (arg1_has_eqc_value && !arg2_has_eqc_value) { + // Case 3: Concat(const, var) == const + TRACE("str", tout << "Case 3: Concat(const, var) == const" << std::endl;); + zstring arg1_str; + u.str.is_string(arg1, arg1_str); + unsigned int resultStrLen = const_str.length(); + unsigned int arg1StrLen = arg1_str.length(); + if (resultStrLen < arg1StrLen) { + // Inconsistency + TRACE("str", tout << "inconsistency detected: \"" + << arg1_str << + "\" is longer than \"" << const_str << "\"," + << " so cannot be concatenated with anything to form it" << "\n";); + expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); + expr_ref diseq(m.mk_not(equality), m); + assert_axiom(diseq); + return; + } else { + int varStrLen = resultStrLen - arg1StrLen; + zstring firstPart = const_str.extract(0, arg1StrLen); + zstring secondPart = const_str.extract(arg1StrLen, varStrLen); + if (arg1_str != firstPart) { + // Inconsistency + TRACE("str", tout << "inconsistency detected: " + << "prefix of concatenation result expected \"" << secondPart << "\", " + << "actually \"" << arg1_str << "\"" + << "\n";); + expr_ref equality(ctx.mk_eq_atom(newConcat, str), m); + expr_ref diseq(m.mk_not(equality), m); + assert_axiom(diseq); + return; + } else { + expr_ref tmpStrConst(mk_string(secondPart), m); + expr_ref premise(ctx.mk_eq_atom(newConcat, str), m); + expr_ref conclusion(ctx.mk_eq_atom(arg2, tmpStrConst), m); + assert_implication(premise, conclusion); + return; + } } - } - } - } - // if no concat length is propagated, check the length of variables. - if (! axiomAdded) { - for (std::set::iterator it = varSet.begin(); it != varSet.end(); it++) { - expr * var = *it; - rational lenValue; - expr_ref varlen (mk_strlen(var), m) ; - bool allLeafResolved = true; - if (! get_value(varlen, lenValue)) { - if (propagate_length_within_eqc(var)) { - axiomAdded = true; - } - } - } - - } - return axiomAdded; -} - -void theory_str::get_unique_non_concat_nodes(expr * node, std::set & argSet) { - app * a_node = to_app(node); - if (!u.str.is_concat(a_node)) { - argSet.insert(node); - return; - } else { - SASSERT(a_node->get_num_args() == 2); - expr * leftArg = a_node->get_arg(0); - expr * rightArg = a_node->get_arg(1); - get_unique_non_concat_nodes(leftArg, argSet); - get_unique_non_concat_nodes(rightArg, argSet); - } -} - -final_check_status theory_str::final_check_eh() { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - expr_ref_vector assignments(m); - ctx.get_assignments(assignments); - - if (opt_VerifyFinalCheckProgress) { - finalCheckProgressIndicator = false; - } - - TRACE("str", tout << "final check" << std::endl;); - TRACE_CODE(if (is_trace_enabled("t_str_dump_assign")) { dump_assignments(); }); - check_variable_scope(); - - if (opt_DeferEQCConsistencyCheck) { - TRACE("str", tout << "performing deferred EQC consistency check" << std::endl;); - std::set eqc_roots; - for (ptr_vector::const_iterator it = ctx.begin_enodes(); it != ctx.end_enodes(); ++it) { - enode * e = *it; - enode * root = e->get_root(); - eqc_roots.insert(root); - } - - bool found_inconsistency = false; - - for (std::set::iterator it = eqc_roots.begin(); it != eqc_roots.end(); ++it) { - enode * e = *it; - app * a = e->get_owner(); - if (!(m.get_sort(a) == u.str.mk_string_sort())) { - TRACE("str", tout << "EQC root " << mk_pp(a, m) << " not a string term; skipping" << std::endl;); } else { - TRACE("str", tout << "EQC root " << mk_pp(a, m) << " is a string term. Checking this EQC" << std::endl;); - // first call check_concat_len_in_eqc() on each member of the eqc - enode * e_it = e; - enode * e_root = e_it; - do { - bool status = check_concat_len_in_eqc(e_it->get_owner()); - if (!status) { - TRACE("str", tout << "concat-len check asserted an axiom on " << mk_pp(e_it->get_owner(), m) << std::endl;); - found_inconsistency = true; - } - e_it = e_it->get_next(); - } while (e_it != e_root); + // Case 4: Concat(var, var) == const + TRACE("str", tout << "Case 4: Concat(var, var) == const" << std::endl;); + if (eval_concat(arg1, arg2) == NULL) { + rational arg1Len, arg2Len; + bool arg1Len_exists = get_len_value(arg1, arg1Len); + bool arg2Len_exists = get_len_value(arg2, arg2Len); + rational concatStrLen((unsigned)const_str.length()); + if (arg1Len_exists || arg2Len_exists) { + expr_ref ax_l1(ctx.mk_eq_atom(concat, str), m); + expr_ref ax_l2(m); + zstring prefixStr, suffixStr; + if (arg1Len_exists) { + if (arg1Len.is_neg()) { + TRACE("str", tout << "length conflict: arg1Len = " << arg1Len << ", concatStrLen = " << concatStrLen << std::endl;); + expr_ref toAssert(m_autil.mk_ge(mk_strlen(arg1), mk_int(0)), m); + assert_axiom(toAssert); + return; + } else if (arg1Len > concatStrLen) { + TRACE("str", tout << "length conflict: arg1Len = " << arg1Len << ", concatStrLen = " << concatStrLen << std::endl;); + expr_ref ax_r1(m_autil.mk_le(mk_strlen(arg1), mk_int(concatStrLen)), m); + assert_implication(ax_l1, ax_r1); + return; + } - // now grab any two distinct elements from the EQC and call new_eq_check() on them - enode * e1 = e; - enode * e2 = e1->get_next(); - if (e1 != e2) { - TRACE("str", tout << "deferred new_eq_check() over EQC of " << mk_pp(e1->get_owner(), m) << " and " << mk_pp(e2->get_owner(), m) << std::endl;); - bool result = new_eq_check(e1->get_owner(), e2->get_owner()); - if (!result) { - TRACE("str", tout << "new_eq_check found inconsistencies" << std::endl;); - found_inconsistency = true; - } - } + prefixStr = const_str.extract(0, arg1Len.get_unsigned()); + rational concat_minus_arg1 = concatStrLen - arg1Len; + suffixStr = const_str.extract(arg1Len.get_unsigned(), concat_minus_arg1.get_unsigned()); + ax_l2 = ctx.mk_eq_atom(mk_strlen(arg1), mk_int(arg1Len)); + } else { + // arg2's length is available + if (arg2Len.is_neg()) { + TRACE("str", tout << "length conflict: arg2Len = " << arg2Len << ", concatStrLen = " << concatStrLen << std::endl;); + expr_ref toAssert(m_autil.mk_ge(mk_strlen(arg2), mk_int(0)), m); + assert_axiom(toAssert); + return; + } else if (arg2Len > concatStrLen) { + TRACE("str", tout << "length conflict: arg2Len = " << arg2Len << ", concatStrLen = " << concatStrLen << std::endl;); + expr_ref ax_r1(m_autil.mk_le(mk_strlen(arg2), mk_int(concatStrLen)), m); + assert_implication(ax_l1, ax_r1); + return; + } + + rational concat_minus_arg2 = concatStrLen - arg2Len; + prefixStr = const_str.extract(0, concat_minus_arg2.get_unsigned()); + suffixStr = const_str.extract(concat_minus_arg2.get_unsigned(), arg2Len.get_unsigned()); + ax_l2 = ctx.mk_eq_atom(mk_strlen(arg2), mk_int(arg2Len)); + } + // consistency check + if (u.str.is_concat(to_app(arg1)) && !can_concat_eq_str(arg1, prefixStr)) { + expr_ref ax_r(m.mk_not(ax_l2), m); + assert_implication(ax_l1, ax_r); + return; + } + if (u.str.is_concat(to_app(arg2)) && !can_concat_eq_str(arg2, suffixStr)) { + expr_ref ax_r(m.mk_not(ax_l2), m); + assert_implication(ax_l1, ax_r); + return; + } + expr_ref_vector r_items(m); + r_items.push_back(ctx.mk_eq_atom(arg1, mk_string(prefixStr))); + r_items.push_back(ctx.mk_eq_atom(arg2, mk_string(suffixStr))); + if (!arg1Len_exists) { + r_items.push_back(ctx.mk_eq_atom(mk_strlen(arg1), mk_int(prefixStr.length()))); + } + if (!arg2Len_exists) { + r_items.push_back(ctx.mk_eq_atom(mk_strlen(arg2), mk_int(suffixStr.length()))); + } + expr_ref lhs(m.mk_and(ax_l1, ax_l2), m); + expr_ref rhs(mk_and(r_items), m); + assert_implication(lhs, rhs); + } else { /* ! (arg1Len != 1 || arg2Len != 1) */ + expr_ref xorFlag(m); + std::pair key1(arg1, arg2); + std::pair key2(arg2, arg1); + + // check the entries in this map to make sure they're still in scope + // before we use them. + + std::map, std::map >::iterator entry1 = varForBreakConcat.find(key1); + std::map, std::map >::iterator entry2 = varForBreakConcat.find(key2); + + bool entry1InScope; + if (entry1 == varForBreakConcat.end()) { + TRACE("str", tout << "key1 no entry" << std::endl;); + entry1InScope = false; + } else { + // OVERRIDE. + entry1InScope = true; + TRACE("str", tout << "key1 entry" << std::endl;); + /* + if (internal_variable_set.find((entry1->second)[0]) == internal_variable_set.end()) { + TRACE("str", tout << "key1 entry not in scope" << std::endl;); + entry1InScope = false; + } else { + TRACE("str", tout << "key1 entry in scope" << std::endl;); + entry1InScope = true; + } + */ + } + + bool entry2InScope; + if (entry2 == varForBreakConcat.end()) { + TRACE("str", tout << "key2 no entry" << std::endl;); + entry2InScope = false; + } else { + // OVERRIDE. + entry2InScope = true; + TRACE("str", tout << "key2 entry" << std::endl;); + /* + if (internal_variable_set.find((entry2->second)[0]) == internal_variable_set.end()) { + TRACE("str", tout << "key2 entry not in scope" << std::endl;); + entry2InScope = false; + } else { + TRACE("str", tout << "key2 entry in scope" << std::endl;); + entry2InScope = true; + } + */ + } + + TRACE("str", tout << "entry 1 " << (entry1InScope ? "in scope" : "not in scope") << std::endl + << "entry 2 " << (entry2InScope ? "in scope" : "not in scope") << std::endl;); + + if (!entry1InScope && !entry2InScope) { + xorFlag = mk_internal_xor_var(); + varForBreakConcat[key1][0] = xorFlag; + } else if (entry1InScope) { + xorFlag = varForBreakConcat[key1][0]; + } else { // entry2InScope + xorFlag = varForBreakConcat[key2][0]; + } + + int concatStrLen = const_str.length(); + int and_count = 1; + + expr_ref_vector arrangement_disjunction(m); + + for (int i = 0; i < concatStrLen + 1; ++i) { + expr_ref_vector and_items(m); + zstring prefixStr = const_str.extract(0, i); + zstring suffixStr = const_str.extract(i, concatStrLen - i); + // skip invalid options + if (u.str.is_concat(to_app(arg1)) && !can_concat_eq_str(arg1, prefixStr)) { + continue; + } + if (u.str.is_concat(to_app(arg2)) && !can_concat_eq_str(arg2, suffixStr)) { + continue; + } + + expr_ref prefixAst(mk_string(prefixStr), m); + expr_ref arg1_eq (ctx.mk_eq_atom(arg1, prefixAst), m); + and_items.push_back(arg1_eq); + and_count += 1; + + expr_ref suffixAst(mk_string(suffixStr), m); + expr_ref arg2_eq (ctx.mk_eq_atom(arg2, suffixAst), m); + and_items.push_back(arg2_eq); + and_count += 1; + + arrangement_disjunction.push_back(mk_and(and_items)); + } + + expr_ref implyL(ctx.mk_eq_atom(concat, str), m); + expr_ref implyR1(m); + if (arrangement_disjunction.empty()) { + // negate + expr_ref concat_eq_str(ctx.mk_eq_atom(concat, str), m); + expr_ref negate_ast(m.mk_not(concat_eq_str), m); + assert_axiom(negate_ast); + } else { + implyR1 = mk_or(arrangement_disjunction); + if (m_params.m_StrongArrangements) { + expr_ref ax_strong(ctx.mk_eq_atom(implyL, implyR1), m); + assert_axiom(ax_strong); + } else { + assert_implication(implyL, implyR1); + } + generate_mutual_exclusion(arrangement_disjunction); + } + } /* (arg1Len != 1 || arg2Len != 1) */ + } /* if (Concat(arg1, arg2) == NULL) */ } } - - if (found_inconsistency) { - TRACE("str", tout << "Found inconsistency in final check! Returning to search." << std::endl;); - return FC_CONTINUE; - } else { - TRACE("str", tout << "Deferred consistency check passed. Continuing in final check." << std::endl;); - } } - // run dependence analysis to find free string variables - std::map varAppearInAssign; - std::map freeVar_map; - std::map > unrollGroup_map; - std::map > var_eq_concat_map; - int conflictInDep = ctx_dep_analysis(varAppearInAssign, freeVar_map, unrollGroup_map, var_eq_concat_map); - if (conflictInDep == -1) { - // return Z3_TRUE; - return FC_DONE; - } - - // enhancement: improved backpropagation of string constants into var=concat terms - bool backpropagation_occurred = false; - for (std::map >::iterator veqc_map_it = var_eq_concat_map.begin(); - veqc_map_it != var_eq_concat_map.end(); ++veqc_map_it) { - expr * var = veqc_map_it->first; - for (std::map::iterator concat_map_it = veqc_map_it->second.begin(); - concat_map_it != veqc_map_it->second.end(); ++concat_map_it) { - app * concat = to_app(concat_map_it->first); - expr * concat_lhs = concat->get_arg(0); - expr * concat_rhs = concat->get_arg(1); - // If the concat LHS and RHS both have a string constant in their EQC, - // but the var does not, then we assert an axiom of the form - // (lhs = "lhs" AND rhs = "rhs") --> (Concat lhs rhs) = "lhsrhs" - bool concat_lhs_haseqc, concat_rhs_haseqc, var_haseqc; - expr * concat_lhs_str = get_eqc_value(concat_lhs, concat_lhs_haseqc); - expr * concat_rhs_str = get_eqc_value(concat_rhs, concat_rhs_haseqc); - expr * var_str = get_eqc_value(var, var_haseqc); - if (concat_lhs_haseqc && concat_rhs_haseqc && !var_haseqc) { - TRACE("str", tout << "backpropagate into " << mk_pp(var, m) << " = " << mk_pp(concat, m) << std::endl - << "LHS ~= " << mk_pp(concat_lhs_str, m) << " RHS ~= " << mk_pp(concat_rhs_str, m) << std::endl;); - zstring lhsString, rhsString; - u.str.is_string(concat_lhs_str, lhsString); - u.str.is_string(concat_rhs_str, rhsString); - zstring concatString = lhsString + rhsString; - expr_ref lhs1(ctx.mk_eq_atom(concat_lhs, concat_lhs_str), m); - expr_ref lhs2(ctx.mk_eq_atom(concat_rhs, concat_rhs_str), m); - expr_ref lhs(m.mk_and(lhs1, lhs2), m); - expr_ref rhs(ctx.mk_eq_atom(concat, mk_string(concatString)), m); - assert_implication(lhs, rhs); - backpropagation_occurred = true; - } - } - } - - if (backpropagation_occurred) { - TRACE("str", tout << "Resuming search due to axioms added by backpropagation." << std::endl;); - return FC_CONTINUE; - } - - // enhancement: improved backpropagation of length information - { - std::set varSet; - std::set concatSet; - std::map exprLenMap; - - bool length_propagation_occurred = propagate_length(varSet, concatSet, exprLenMap); - if (length_propagation_occurred) { - TRACE("str", tout << "Resuming search due to axioms added by length propagation." << std::endl;); - return FC_CONTINUE; - } - } - - bool needToAssignFreeVars = false; - std::set free_variables; - std::set unused_internal_variables; - { // Z3str2 free variables check - std::map::iterator itor = varAppearInAssign.begin(); - for (; itor != varAppearInAssign.end(); ++itor) { - /* - std::string vName = std::string(Z3_ast_to_string(ctx, itor->first)); - if (vName.length() >= 3 && vName.substr(0, 3) == "$$_") - continue; - */ - if (internal_variable_set.find(itor->first) != internal_variable_set.end() - || regex_variable_set.find(itor->first) != regex_variable_set.end()) { - // this can be ignored, I think - TRACE("str", tout << "free internal variable " << mk_pp(itor->first, m) << " ignored" << std::endl;); - continue; - } - bool hasEqcValue = false; - expr * eqcString = get_eqc_value(itor->first, hasEqcValue); - if (!hasEqcValue) { - TRACE("str", tout << "found free variable " << mk_pp(itor->first, m) << std::endl;); - needToAssignFreeVars = true; - free_variables.insert(itor->first); - // break; - } else { - // debug - TRACE("str", tout << "variable " << mk_pp(itor->first, m) << " = " << mk_pp(eqcString, m) << std::endl;); - } - } - } - - if (!needToAssignFreeVars) { - - // check string-int terms - bool addedStrIntAxioms = false; - for (unsigned i = 0; i < string_int_conversion_terms.size(); ++i) { - app * ex = to_app(string_int_conversion_terms[i].get()); - if (u.str.is_stoi(ex)) { - bool axiomAdd = finalcheck_str2int(ex); - if (axiomAdd) { - addedStrIntAxioms = true; - } - } else if (u.str.is_itos(ex)) { - bool axiomAdd = finalcheck_int2str(ex); - if (axiomAdd) { - addedStrIntAxioms = true; - } - } else { - UNREACHABLE(); - } - } - if (addedStrIntAxioms) { - TRACE("str", tout << "Resuming search due to addition of string-integer conversion axioms." << std::endl;); - return FC_CONTINUE; - } - - if (unused_internal_variables.empty()) { - TRACE("str", tout << "All variables are assigned. Done!" << std::endl;); - return FC_DONE; - } else { - TRACE("str", tout << "Assigning decoy values to free internal variables." << std::endl;); - for (std::set::iterator it = unused_internal_variables.begin(); it != unused_internal_variables.end(); ++it) { - expr * var = *it; - expr_ref assignment(m.mk_eq(var, mk_string("**unused**")), m); - assert_axiom(assignment); - } - return FC_CONTINUE; - } - } - - CTRACE("str", needToAssignFreeVars, - tout << "Need to assign values to the following free variables:" << std::endl; - for (std::set::iterator itx = free_variables.begin(); itx != free_variables.end(); ++itx) { - tout << mk_ismt2_pp(*itx, m) << std::endl; - } - tout << "freeVar_map has the following entries:" << std::endl; - for (std::map::iterator fvIt = freeVar_map.begin(); fvIt != freeVar_map.end(); fvIt++) { - expr * var = fvIt->first; - tout << mk_ismt2_pp(var, m) << std::endl; - } - ); - - // ----------------------------------------------------------- - // variables in freeVar are those not bounded by Concats - // classify variables in freeVarMap: - // (1) freeVar = unroll(r1, t1) - // (2) vars are not bounded by either concat or unroll - // ----------------------------------------------------------- - std::map > fv_unrolls_map; - std::set tmpSet; - expr * constValue = NULL; - for (std::map::iterator fvIt2 = freeVar_map.begin(); fvIt2 != freeVar_map.end(); fvIt2++) { - expr * var = fvIt2->first; - tmpSet.clear(); - get_eqc_allUnroll(var, constValue, tmpSet); - if (tmpSet.size() > 0) { - fv_unrolls_map[var] = tmpSet; - } - } - // erase var bounded by an unroll function from freeVar_map - for (std::map >::iterator fvIt3 = fv_unrolls_map.begin(); - fvIt3 != fv_unrolls_map.end(); fvIt3++) { - expr * var = fvIt3->first; - TRACE("str", tout << "erase free variable " << mk_pp(var, m) << " from freeVar_map, it is bounded by an Unroll" << std::endl;); - freeVar_map.erase(var); - } - - // collect the case: - // * Concat(X, Y) = unroll(r1, t1) /\ Concat(X, Y) = unroll(r2, t2) - // concatEqUnrollsMap[Concat(X, Y)] = {unroll(r1, t1), unroll(r2, t2)} - - std::map > concatEqUnrollsMap; - for (std::map >::iterator urItor = unrollGroup_map.begin(); - urItor != unrollGroup_map.end(); urItor++) { - expr * unroll = urItor->first; - expr * curr = unroll; - do { - if (u.str.is_concat(to_app(curr))) { - concatEqUnrollsMap[curr].insert(unroll); - concatEqUnrollsMap[curr].insert(unrollGroup_map[unroll].begin(), unrollGroup_map[unroll].end()); - } - enode * e_curr = ctx.get_enode(curr); - curr = e_curr->get_next()->get_owner(); - // curr = get_eqc_next(curr); - } while (curr != unroll); - } - - std::map > concatFreeArgsEqUnrollsMap; - std::set fvUnrollSet; - for (std::map >::iterator concatItor = concatEqUnrollsMap.begin(); - concatItor != concatEqUnrollsMap.end(); concatItor++) { - expr * concat = concatItor->first; - expr * concatArg1 = to_app(concat)->get_arg(0); - expr * concatArg2 = to_app(concat)->get_arg(1); - bool arg1Bounded = false; - bool arg2Bounded = false; - // arg1 - if (variable_set.find(concatArg1) != variable_set.end()) { - if (freeVar_map.find(concatArg1) == freeVar_map.end()) { - arg1Bounded = true; - } else { - fvUnrollSet.insert(concatArg1); - } - } else if (u.str.is_concat(to_app(concatArg1))) { - if (concatEqUnrollsMap.find(concatArg1) == concatEqUnrollsMap.end()) { - arg1Bounded = true; - } - } - // arg2 - if (variable_set.find(concatArg2) != variable_set.end()) { - if (freeVar_map.find(concatArg2) == freeVar_map.end()) { - arg2Bounded = true; - } else { - fvUnrollSet.insert(concatArg2); - } - } else if (u.str.is_concat(to_app(concatArg2))) { - if (concatEqUnrollsMap.find(concatArg2) == concatEqUnrollsMap.end()) { - arg2Bounded = true; - } - } - if (!arg1Bounded && !arg2Bounded) { - concatFreeArgsEqUnrollsMap[concat].insert( - concatEqUnrollsMap[concat].begin(), - concatEqUnrollsMap[concat].end()); - } - } - for (std::set::iterator vItor = fvUnrollSet.begin(); vItor != fvUnrollSet.end(); vItor++) { - TRACE("str", tout << "remove " << mk_pp(*vItor, m) << " from freeVar_map" << std::endl;); - freeVar_map.erase(*vItor); - } - - // Assign free variables - std::set fSimpUnroll; - - constValue = NULL; - - { - TRACE("str", tout << "free var map (#" << freeVar_map.size() << "):" << std::endl; - for (std::map::iterator freeVarItor1 = freeVar_map.begin(); freeVarItor1 != freeVar_map.end(); freeVarItor1++) { - expr * freeVar = freeVarItor1->first; - rational lenValue; - bool lenValue_exists = get_len_value(freeVar, lenValue); - tout << mk_pp(freeVar, m) << " [depCnt = " << freeVarItor1->second << ", length = " - << (lenValue_exists ? lenValue.to_string() : "?") - << "]" << std::endl; - } - ); - } - - for (std::map >::iterator fvIt2 = concatFreeArgsEqUnrollsMap.begin(); - fvIt2 != concatFreeArgsEqUnrollsMap.end(); fvIt2++) { - expr * concat = fvIt2->first; - for (std::set::iterator urItor = fvIt2->second.begin(); urItor != fvIt2->second.end(); urItor++) { - expr * unroll = *urItor; - process_concat_eq_unroll(concat, unroll); - } - } - - // -------- - // experimental free variable assignment - begin - // * special handling for variables that are not used in concat - // -------- - bool testAssign = true; - if (!testAssign) { - for (std::map::iterator fvIt = freeVar_map.begin(); fvIt != freeVar_map.end(); fvIt++) { - expr * freeVar = fvIt->first; - /* - std::string vName = std::string(Z3_ast_to_string(ctx, freeVar)); - if (vName.length() >= 9 && vName.substr(0, 9) == "$$_regVar") { - continue; - } - */ - expr * toAssert = gen_len_val_options_for_free_var(freeVar, NULL, ""); - if (toAssert != NULL) { - assert_axiom(toAssert); - } - } - } else { - process_free_var(freeVar_map); - } - // experimental free variable assignment - end - - // now deal with removed free variables that are bounded by an unroll - TRACE("str", tout << "fv_unrolls_map (#" << fv_unrolls_map.size() << "):" << std::endl;); - for (std::map >::iterator fvIt1 = fv_unrolls_map.begin(); - fvIt1 != fv_unrolls_map.end(); fvIt1++) { - expr * var = fvIt1->first; - fSimpUnroll.clear(); - get_eqc_simpleUnroll(var, constValue, fSimpUnroll); - if (fSimpUnroll.size() == 0) { - gen_assign_unroll_reg(fv_unrolls_map[var]); - } else { - expr * toAssert = gen_assign_unroll_Str2Reg(var, fSimpUnroll); - if (toAssert != NULL) { - assert_axiom(toAssert); - } - } - } - - if (opt_VerifyFinalCheckProgress && !finalCheckProgressIndicator) { - TRACE("str", tout << "BUG: no progress in final check, giving up!!" << std::endl;); - m.raise_exception("no progress in theory_str final check"); - } - - return FC_CONTINUE; // since by this point we've added axioms -} - -inline zstring int_to_string(int i) { - std::stringstream ss; - ss << i; - std::string str = ss.str(); - return zstring(str.c_str()); -} - -inline std::string longlong_to_string(long long i) { - std::stringstream ss; - ss << i; - return ss.str(); -} - -void theory_str::print_value_tester_list(svector > & testerList) { - ast_manager & m = get_manager(); - TRACE("str", - int ss = testerList.size(); - tout << "valueTesterList = {"; - for (int i = 0; i < ss; ++i) { - if (i % 4 == 0) { - tout << std::endl; - } - tout << "(" << testerList[i].first << ", "; - tout << mk_ismt2_pp(testerList[i].second, m); - tout << "), "; - } - tout << std::endl << "}" << std::endl; - ); -} - -zstring theory_str::gen_val_string(int len, int_vector & encoding) { - SASSERT(charSetSize > 0); - SASSERT(char_set != NULL); - - std::string re(len, char_set[0]); - for (int i = 0; i < (int) encoding.size() - 1; i++) { - int idx = encoding[i]; - re[len - 1 - i] = char_set[idx]; - } - return zstring(re.c_str()); -} - -/* - * The return value indicates whether we covered the search space. - * - If the next encoding is valid, return false - * - Otherwise, return true - */ -bool theory_str::get_next_val_encode(int_vector & base, int_vector & next) { - SASSERT(charSetSize > 0); - - TRACE("str", tout << "base vector: [ "; - for (unsigned i = 0; i < base.size(); ++i) { - tout << base[i] << " "; - } - tout << "]" << std::endl; - ); - - int s = 0; - int carry = 0; - next.reset(); - - for (int i = 0; i < (int) base.size(); i++) { - if (i == 0) { - s = base[i] + 1; - carry = s / charSetSize; - s = s % charSetSize; - next.push_back(s); - } else { - s = base[i] + carry; - carry = s / charSetSize; - s = s % charSetSize; - next.push_back(s); - } - } - if (next[next.size() - 1] > 0) { - next.reset(); - return true; - } else { - return false; - } -} - -expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * val_indicator, - zstring lenStr, int tries) { - ast_manager & m = get_manager(); - context & ctx = get_context(); - - int distance = 32; - - // ---------------------------------------------------------------------------------------- - // generate value options encoding - // encoding is a vector of size (len + 1) - // e.g, len = 2, - // encoding {1, 2, 0} means the value option is "charSet[2]"."charSet[1]" - // the last item in the encoding indicates whether the whole space is covered - // for example, if the charSet = {a, b}. All valid encodings are - // {0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {1, 1, 0} - // if add 1 to the last one, we get - // {0, 0, 1} - // the last item "1" shows this is not a valid encoding, and we have covered all space - // ---------------------------------------------------------------------------------------- - int len = atoi(lenStr.encode().c_str()); - bool coverAll = false; - svector options; - int_vector base; - - TRACE("str", tout - << "freeVar = " << mk_ismt2_pp(freeVar, m) << std::endl - << "len_indicator = " << mk_ismt2_pp(len_indicator, m) << std::endl - << "val_indicator = " << mk_ismt2_pp(val_indicator, m) << std::endl - << "lenstr = " << lenStr << "\n" - << "tries = " << tries << "\n"; - if (m_params.m_AggressiveValueTesting) { - tout << "note: aggressive value testing is enabled" << std::endl; - } - ); - - if (tries == 0) { - base = int_vector(len + 1, 0); - coverAll = false; - } else { - expr * lastestValIndi = fvar_valueTester_map[freeVar][len][tries - 1].second; - TRACE("str", tout << "last value tester = " << mk_ismt2_pp(lastestValIndi, m) << std::endl;); - coverAll = get_next_val_encode(val_range_map[lastestValIndi], base); - } - - long long l = (tries) * distance; - long long h = l; - for (int i = 0; i < distance; i++) { - if (coverAll) - break; - options.push_back(base); - h++; - coverAll = get_next_val_encode(options[options.size() - 1], base); - } - val_range_map[val_indicator] = options[options.size() - 1]; - - TRACE("str", - tout << "value tester encoding " << "{" << std::endl; - int_vector vec = val_range_map[val_indicator]; - - for (int_vector::iterator it = vec.begin(); it != vec.end(); ++it) { - tout << *it << std::endl; - } - tout << "}" << std::endl; - ); - - // ---------------------------------------------------------------------------------------- - - ptr_vector orList; - ptr_vector andList; - - for (long long i = l; i < h; i++) { - orList.push_back(m.mk_eq(val_indicator, mk_string(longlong_to_string(i).c_str()) )); - if (m_params.m_AggressiveValueTesting) { - literal l = mk_eq(val_indicator, mk_string(longlong_to_string(i).c_str()), false); - ctx.mark_as_relevant(l); - ctx.force_phase(l); - } - - zstring aStr = gen_val_string(len, options[i - l]); - expr * strAst; - if (m_params.m_UseFastValueTesterCache) { - if (!valueTesterCache.find(aStr, strAst)) { - strAst = mk_string(aStr); - valueTesterCache.insert(aStr, strAst); - m_trail.push_back(strAst); - } - } else { - strAst = mk_string(aStr); - } - andList.push_back(m.mk_eq(orList[orList.size() - 1], m.mk_eq(freeVar, strAst))); - } - if (!coverAll) { - orList.push_back(m.mk_eq(val_indicator, mk_string("more"))); - if (m_params.m_AggressiveValueTesting) { - literal l = mk_eq(val_indicator, mk_string("more"), false); - ctx.mark_as_relevant(l); - ctx.force_phase(~l); - } - } - - expr ** or_items = alloc_svect(expr*, orList.size()); - expr ** and_items = alloc_svect(expr*, andList.size() + 1); - - for (int i = 0; i < (int) orList.size(); i++) { - or_items[i] = orList[i]; - } - if (orList.size() > 1) - and_items[0] = m.mk_or(orList.size(), or_items); - else - and_items[0] = or_items[0]; - - for (int i = 0; i < (int) andList.size(); i++) { - and_items[i + 1] = andList[i]; - } - expr * valTestAssert = m.mk_and(andList.size() + 1, and_items); - - // --------------------------------------- - // If the new value tester is $$_val_x_16_i - // Should add ($$_len_x_j = 16) /\ ($$_val_x_16_i = "more") - // --------------------------------------- - andList.reset(); - andList.push_back(m.mk_eq(len_indicator, mk_string(lenStr))); - for (int i = 0; i < tries; i++) { - expr * vTester = fvar_valueTester_map[freeVar][len][i].second; - if (vTester != val_indicator) - andList.push_back(m.mk_eq(vTester, mk_string("more"))); - } - expr * assertL = NULL; - if (andList.size() == 1) { - assertL = andList[0]; - } else { - expr ** and_items = alloc_svect(expr*, andList.size()); - for (int i = 0; i < (int) andList.size(); i++) { - and_items[i] = andList[i]; - } - assertL = m.mk_and(andList.size(), and_items); - } - - // (assertL => valTestAssert) <=> (!assertL OR valTestAssert) - valTestAssert = m.mk_or(m.mk_not(assertL), valTestAssert); - return valTestAssert; -} - -expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, - zstring len_valueStr, expr * valTesterInCbEq, zstring valTesterValueStr) { - ast_manager & m = get_manager(); - - int len = atoi(len_valueStr.encode().c_str()); - - // check whether any value tester is actually in scope - TRACE("str", tout << "checking scope of previous value testers" << std::endl;); - bool map_effectively_empty = true; - if (fvar_valueTester_map[freeVar].find(len) != fvar_valueTester_map[freeVar].end()) { - // there's *something* in the map, but check its scope - svector > entries = fvar_valueTester_map[freeVar][len]; - for (svector >::iterator it = entries.begin(); it != entries.end(); ++it) { - std::pair entry = *it; - expr * aTester = entry.second; - if (internal_variable_set.find(aTester) == internal_variable_set.end()) { - TRACE("str", tout << mk_pp(aTester, m) << " out of scope" << std::endl;); - } else { - TRACE("str", tout << mk_pp(aTester, m) << " in scope" << std::endl;); - map_effectively_empty = false; - break; - } - } - } - - if (map_effectively_empty) { - TRACE("str", tout << "no previous value testers, or none of them were in scope" << std::endl;); - int tries = 0; - expr * val_indicator = mk_internal_valTest_var(freeVar, len, tries); - valueTester_fvar_map[val_indicator] = freeVar; - fvar_valueTester_map[freeVar][len].push_back(std::make_pair(sLevel, val_indicator)); - print_value_tester_list(fvar_valueTester_map[freeVar][len]); - return gen_val_options(freeVar, len_indicator, val_indicator, len_valueStr, tries); - } else { - TRACE("str", tout << "checking previous value testers" << std::endl;); - print_value_tester_list(fvar_valueTester_map[freeVar][len]); - - // go through all previous value testers - // If some doesn't have an eqc value, add its assertion again. - int testerTotal = fvar_valueTester_map[freeVar][len].size(); - int i = 0; - for (; i < testerTotal; i++) { - expr * aTester = fvar_valueTester_map[freeVar][len][i].second; - - // it's probably worth checking scope here, actually - if (internal_variable_set.find(aTester) == internal_variable_set.end()) { - TRACE("str", tout << "value tester " << mk_pp(aTester, m) << " out of scope, skipping" << std::endl;); - continue; - } - - if (aTester == valTesterInCbEq) { - break; - } - - bool anEqcHasValue = false; - // Z3_ast anEqc = get_eqc_value(t, aTester, anEqcHasValue); - expr * aTester_eqc_value = get_eqc_value(aTester, anEqcHasValue); - if (!anEqcHasValue) { - TRACE("str", tout << "value tester " << mk_ismt2_pp(aTester, m) - << " doesn't have an equivalence class value." << std::endl;); - refresh_theory_var(aTester); - - expr * makeupAssert = gen_val_options(freeVar, len_indicator, aTester, len_valueStr, i); - - TRACE("str", tout << "var: " << mk_ismt2_pp(freeVar, m) << std::endl - << mk_ismt2_pp(makeupAssert, m) << std::endl;); - assert_axiom(makeupAssert); - } else { - TRACE("str", tout << "value tester " << mk_ismt2_pp(aTester, m) - << " == " << mk_ismt2_pp(aTester_eqc_value, m) << std::endl;); - } - } - - if (valTesterValueStr == "more") { - expr * valTester = NULL; - if (i + 1 < testerTotal) { - valTester = fvar_valueTester_map[freeVar][len][i + 1].second; - refresh_theory_var(valTester); - } else { - valTester = mk_internal_valTest_var(freeVar, len, i + 1); - valueTester_fvar_map[valTester] = freeVar; - fvar_valueTester_map[freeVar][len].push_back(std::make_pair(sLevel, valTester)); - print_value_tester_list(fvar_valueTester_map[freeVar][len]); - } - expr * nextAssert = gen_val_options(freeVar, len_indicator, valTester, len_valueStr, i + 1); - return nextAssert; - } - - return NULL; - } -} - -void theory_str::reduce_virtual_regex_in(expr * var, expr * regex, expr_ref_vector & items) { - context & ctx = get_context(); - ast_manager & mgr = get_manager(); - - TRACE("str", tout << "reduce regex " << mk_pp(regex, mgr) << " with respect to variable " << mk_pp(var, mgr) << std::endl;); - - app * regexFuncDecl = to_app(regex); - if (u.re.is_to_re(regexFuncDecl)) { - // --------------------------------------------------------- - // var \in Str2Reg(s1) - // ==> - // var = s1 /\ length(var) = length(s1) - // --------------------------------------------------------- - expr * strInside = to_app(regex)->get_arg(0); - items.push_back(ctx.mk_eq_atom(var, strInside)); - items.push_back(ctx.mk_eq_atom(mk_strlen(var), mk_strlen(strInside))); - return; - } - // RegexUnion - else if (u.re.is_union(regexFuncDecl)) { - // --------------------------------------------------------- - // var \in RegexUnion(r1, r2) - // ==> - // (var = newVar1 \/ var = newVar2) - // (var = newVar1 --> length(var) = length(newVar1)) /\ (var = newVar2 --> length(var) = length(newVar2)) - // /\ (newVar1 \in r1) /\ (newVar2 \in r2) - // --------------------------------------------------------- - expr_ref newVar1(mk_regex_rep_var(), mgr); - expr_ref newVar2(mk_regex_rep_var(), mgr); - items.push_back(mgr.mk_or(ctx.mk_eq_atom(var, newVar1), ctx.mk_eq_atom(var, newVar2))); - items.push_back(mgr.mk_or( - mgr.mk_not(ctx.mk_eq_atom(var, newVar1)), - ctx.mk_eq_atom(mk_strlen(var), mk_strlen(newVar1)))); - items.push_back(mgr.mk_or( - mgr.mk_not(ctx.mk_eq_atom(var, newVar2)), - ctx.mk_eq_atom(mk_strlen(var), mk_strlen(newVar2)))); - - expr * regArg1 = to_app(regex)->get_arg(0); - reduce_virtual_regex_in(newVar1, regArg1, items); - - expr * regArg2 = to_app(regex)->get_arg(1); - reduce_virtual_regex_in(newVar2, regArg2, items); - - return; - } - // RegexConcat - else if (u.re.is_concat(regexFuncDecl)) { - // --------------------------------------------------------- - // var \in RegexConcat(r1, r2) - // ==> - // (var = newVar1 . newVar2) /\ (length(var) = length(vewVar1 . newVar2) ) - // /\ (newVar1 \in r1) /\ (newVar2 \in r2) - // --------------------------------------------------------- - expr_ref newVar1(mk_regex_rep_var(), mgr); - expr_ref newVar2(mk_regex_rep_var(), mgr); - expr_ref concatAst(mk_concat(newVar1, newVar2), mgr); - items.push_back(ctx.mk_eq_atom(var, concatAst)); - items.push_back(ctx.mk_eq_atom(mk_strlen(var), - m_autil.mk_add(mk_strlen(newVar1), mk_strlen(newVar2)))); - - expr * regArg1 = to_app(regex)->get_arg(0); - reduce_virtual_regex_in(newVar1, regArg1, items); - expr * regArg2 = to_app(regex)->get_arg(1); - reduce_virtual_regex_in(newVar2, regArg2, items); - return; - } - // Unroll - else if (u.re.is_star(regexFuncDecl)) { - // --------------------------------------------------------- - // var \in Star(r1) - // ==> - // var = unroll(r1, t1) /\ |var| = |unroll(r1, t1)| - // --------------------------------------------------------- - expr * regArg = to_app(regex)->get_arg(0); - expr_ref unrollCnt(mk_unroll_bound_var(), mgr); - expr_ref unrollFunc(mk_unroll(regArg, unrollCnt), mgr); - items.push_back(ctx.mk_eq_atom(var, unrollFunc)); - items.push_back(ctx.mk_eq_atom(mk_strlen(var), mk_strlen(unrollFunc))); - return; - } - // re.range - else if (u.re.is_range(regexFuncDecl)) { - // var in range("a", "z") - // ==> - // (var = "a" or var = "b" or ... or var = "z") - expr_ref lo(regexFuncDecl->get_arg(0), mgr); - expr_ref hi(regexFuncDecl->get_arg(1), mgr); - zstring str_lo, str_hi; - SASSERT(u.str.is_string(lo)); - SASSERT(u.str.is_string(hi)); - u.str.is_string(lo, str_lo); - u.str.is_string(hi, str_hi); - SASSERT(str_lo.length() == 1); - SASSERT(str_hi.length() == 1); - unsigned int c1 = str_lo[0]; - unsigned int c2 = str_hi[0]; - if (c1 > c2) { - // exchange - unsigned int tmp = c1; - c1 = c2; - c2 = tmp; - } - expr_ref_vector range_cases(mgr); - for (unsigned int ch = c1; ch <= c2; ++ch) { - zstring s_ch(ch); - expr_ref rhs(ctx.mk_eq_atom(var, u.str.mk_string(s_ch)), mgr); - range_cases.push_back(rhs); - } - expr_ref rhs(mk_or(range_cases), mgr); - SASSERT(rhs); - assert_axiom(rhs); - return; - } else { - get_manager().raise_exception("unrecognized regex operator"); - UNREACHABLE(); - } -} - -void theory_str::gen_assign_unroll_reg(std::set & unrolls) { - context & ctx = get_context(); - ast_manager & mgr = get_manager(); - - expr_ref_vector items(mgr); - for (std::set::iterator itor = unrolls.begin(); itor != unrolls.end(); itor++) { - expr * unrFunc = *itor; - TRACE("str", tout << "generating assignment for unroll " << mk_pp(unrFunc, mgr) << std::endl;); - - expr * regexInUnr = to_app(unrFunc)->get_arg(0); - expr * cntInUnr = to_app(unrFunc)->get_arg(1); - items.reset(); - - rational low, high; - bool low_exists = lower_bound(cntInUnr, low); - bool high_exists = upper_bound(cntInUnr, high); - - TRACE("str", - tout << "unroll " << mk_pp(unrFunc, mgr) << std::endl; - rational unrLenValue; - bool unrLenValue_exists = get_len_value(unrFunc, unrLenValue); - tout << "unroll length: " << (unrLenValue_exists ? unrLenValue.to_string() : "?") << std::endl; - rational cntInUnrValue; - bool cntHasValue = get_value(cntInUnr, cntInUnrValue); - tout << "unroll count: " << (cntHasValue ? cntInUnrValue.to_string() : "?") - << " low = " - << (low_exists ? low.to_string() : "?") - << " high = " - << (high_exists ? high.to_string() : "?") - << std::endl; - ); - - expr_ref toAssert(mgr); - if (low.is_neg()) { - toAssert = m_autil.mk_ge(cntInUnr, mk_int(0)); - } else { - if (unroll_var_map.find(unrFunc) == unroll_var_map.end()) { - - expr_ref newVar1(mk_regex_rep_var(), mgr); - expr_ref newVar2(mk_regex_rep_var(), mgr); - expr_ref concatAst(mk_concat(newVar1, newVar2), mgr); - expr_ref newCnt(mk_unroll_bound_var(), mgr); - expr_ref newUnrollFunc(mk_unroll(regexInUnr, newCnt), mgr); - - // unroll(r1, t1) = newVar1 . newVar2 - items.push_back(ctx.mk_eq_atom(unrFunc, concatAst)); - items.push_back(ctx.mk_eq_atom(mk_strlen(unrFunc), m_autil.mk_add(mk_strlen(newVar1), mk_strlen(newVar2)))); - // mk_strlen(unrFunc) >= mk_strlen(newVar{1,2}) - items.push_back(m_autil.mk_ge(m_autil.mk_add(mk_strlen(unrFunc), m_autil.mk_mul(mk_int(-1), mk_strlen(newVar1))), mk_int(0))); - items.push_back(m_autil.mk_ge(m_autil.mk_add(mk_strlen(unrFunc), m_autil.mk_mul(mk_int(-1), mk_strlen(newVar2))), mk_int(0))); - // newVar1 \in r1 - reduce_virtual_regex_in(newVar1, regexInUnr, items); - items.push_back(ctx.mk_eq_atom(cntInUnr, m_autil.mk_add(newCnt, mk_int(1)))); - items.push_back(ctx.mk_eq_atom(newVar2, newUnrollFunc)); - items.push_back(ctx.mk_eq_atom(mk_strlen(newVar2), mk_strlen(newUnrollFunc))); - toAssert = ctx.mk_eq_atom( - m_autil.mk_ge(cntInUnr, mk_int(1)), - mk_and(items)); - - // option 0 - expr_ref op0(ctx.mk_eq_atom(cntInUnr, mk_int(0)), mgr); - expr_ref ast1(ctx.mk_eq_atom(unrFunc, mk_string("")), mgr); - expr_ref ast2(ctx.mk_eq_atom(mk_strlen(unrFunc), mk_int(0)), mgr); - expr_ref and1(mgr.mk_and(ast1, ast2), mgr); - - // put together - toAssert = mgr.mk_and(ctx.mk_eq_atom(op0, and1), toAssert); - - unroll_var_map[unrFunc] = toAssert; - } else { - toAssert = unroll_var_map[unrFunc]; - } - } - m_trail.push_back(toAssert); - assert_axiom(toAssert); - } -} - -static int computeGCD(int x, int y) { - if (x == 0) { - return y; - } - while (y != 0) { - if (x > y) { - x = x - y; - } else { - y = y - x; - } - } - return x; -} - -static int computeLCM(int a, int b) { - int temp = computeGCD(a, b); - return temp ? (a / temp * b) : 0; -} - -static zstring get_unrolled_string(zstring core, int count) { - zstring res(""); - for (int i = 0; i < count; i++) { - res = res + core; - } - return res; -} - -expr * theory_str::gen_assign_unroll_Str2Reg(expr * n, std::set & unrolls) { - context & ctx = get_context(); - ast_manager & mgr = get_manager(); - - int lcm = 1; - int coreValueCount = 0; - expr * oneUnroll = NULL; - zstring oneCoreStr(""); - for (std::set::iterator itor = unrolls.begin(); itor != unrolls.end(); itor++) { - expr * str2RegFunc = to_app(*itor)->get_arg(0); - expr * coreVal = to_app(str2RegFunc)->get_arg(0); - zstring coreStr; - u.str.is_string(coreVal, coreStr); - if (oneUnroll == NULL) { - oneUnroll = *itor; - oneCoreStr = coreStr; - } - coreValueCount++; - int core1Len = coreStr.length(); - lcm = computeLCM(lcm, core1Len); - } - // - bool canHaveNonEmptyAssign = true; - expr_ref_vector litems(mgr); - zstring lcmStr = get_unrolled_string(oneCoreStr, (lcm / oneCoreStr.length())); - for (std::set::iterator itor = unrolls.begin(); itor != unrolls.end(); itor++) { - expr * str2RegFunc = to_app(*itor)->get_arg(0); - expr * coreVal = to_app(str2RegFunc)->get_arg(0); - zstring coreStr; - u.str.is_string(coreVal, coreStr); - unsigned int core1Len = coreStr.length(); - zstring uStr = get_unrolled_string(coreStr, (lcm / core1Len)); - if (uStr != lcmStr) { - canHaveNonEmptyAssign = false; - } - litems.push_back(ctx.mk_eq_atom(n, *itor)); - } - - if (canHaveNonEmptyAssign) { - return gen_unroll_conditional_options(n, unrolls, lcmStr); - } else { - expr_ref implyL(mk_and(litems), mgr); - expr_ref implyR(ctx.mk_eq_atom(n, mk_string("")), mgr); - // want to return (implyL -> implyR) - expr * final_axiom = rewrite_implication(implyL, implyR); - return final_axiom; - } -} - -expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & unrolls, zstring lcmStr) { - context & ctx = get_context(); - ast_manager & mgr = get_manager(); - - int dist = opt_LCMUnrollStep; - expr_ref_vector litems(mgr); - expr_ref moreAst(mk_string("more"), mgr); - for (std::set::iterator itor = unrolls.begin(); itor != unrolls.end(); itor++) { - expr_ref item(ctx.mk_eq_atom(var, *itor), mgr); - TRACE("str", tout << "considering unroll " << mk_pp(item, mgr) << std::endl;); - litems.push_back(item); - } - - // handle out-of-scope entries in unroll_tries_map - - ptr_vector outOfScopeTesters; - - for (ptr_vector::iterator it = unroll_tries_map[var][unrolls].begin(); - it != unroll_tries_map[var][unrolls].end(); ++it) { - expr * tester = *it; - bool inScope = (internal_unrollTest_vars.find(tester) != internal_unrollTest_vars.end()); - TRACE("str", tout << "unroll test var " << mk_pp(tester, mgr) - << (inScope ? " in scope" : " out of scope") - << std::endl;); - if (!inScope) { - outOfScopeTesters.push_back(tester); - } - } - - for (ptr_vector::iterator it = outOfScopeTesters.begin(); - it != outOfScopeTesters.end(); ++it) { - unroll_tries_map[var][unrolls].erase(*it); - } - - - if (unroll_tries_map[var][unrolls].size() == 0) { - unroll_tries_map[var][unrolls].push_back(mk_unroll_test_var()); - } - - int tries = unroll_tries_map[var][unrolls].size(); - for (int i = 0; i < tries; i++) { - expr * tester = unroll_tries_map[var][unrolls][i]; - // TESTING - refresh_theory_var(tester); - bool testerHasValue = false; - expr * testerVal = get_eqc_value(tester, testerHasValue); - if (!testerHasValue) { - // generate make-up assertion - int l = i * dist; - int h = (i + 1) * dist; - expr_ref lImp(mk_and(litems), mgr); - expr_ref rImp(gen_unroll_assign(var, lcmStr, tester, l, h), mgr); - - SASSERT(lImp); - TRACE("str", tout << "lImp = " << mk_pp(lImp, mgr) << std::endl;); - SASSERT(rImp); - TRACE("str", tout << "rImp = " << mk_pp(rImp, mgr) << std::endl;); - - expr_ref toAssert(mgr.mk_or(mgr.mk_not(lImp), rImp), mgr); - SASSERT(toAssert); - TRACE("str", tout << "Making up assignments for variable which is equal to unbounded Unroll" << std::endl;); - m_trail.push_back(toAssert); - return toAssert; - - // note: this is how the code looks in Z3str2's strRegex.cpp:genUnrollConditionalOptions. - // the return is in the same place - - // insert [tester = "more"] to litems so that the implyL for next tester is correct - litems.push_back(ctx.mk_eq_atom(tester, moreAst)); - } else { - zstring testerStr; - u.str.is_string(testerVal, testerStr); - TRACE("str", tout << "Tester [" << mk_pp(tester, mgr) << "] = " << testerStr << "\n";); - if (testerStr == "more") { - litems.push_back(ctx.mk_eq_atom(tester, moreAst)); - } - } - } - expr * tester = mk_unroll_test_var(); - unroll_tries_map[var][unrolls].push_back(tester); - int l = tries * dist; - int h = (tries + 1) * dist; - expr_ref lImp(mk_and(litems), mgr); - expr_ref rImp(gen_unroll_assign(var, lcmStr, tester, l, h), mgr); - SASSERT(lImp); - SASSERT(rImp); - expr_ref toAssert(mgr.mk_or(mgr.mk_not(lImp), rImp), mgr); - SASSERT(toAssert); - TRACE("str", tout << "Generating assignment for variable which is equal to unbounded Unroll" << std::endl;); - m_trail.push_back(toAssert); - return toAssert; -} - -expr * theory_str::gen_unroll_assign(expr * var, zstring lcmStr, expr * testerVar, int l, int h) { - context & ctx = get_context(); - ast_manager & mgr = get_manager(); - - TRACE("str", tout << "entry: var = " << mk_pp(var, mgr) << ", lcmStr = " << lcmStr - << ", l = " << l << ", h = " << h << "\n";); - - if (m_params.m_AggressiveUnrollTesting) { - TRACE("str", tout << "note: aggressive unroll testing is active" << std::endl;); - } - - expr_ref_vector orItems(mgr); - expr_ref_vector andItems(mgr); - - for (int i = l; i < h; i++) { - zstring iStr = int_to_string(i); - expr_ref testerEqAst(ctx.mk_eq_atom(testerVar, mk_string(iStr)), mgr); - TRACE("str", tout << "testerEqAst = " << mk_pp(testerEqAst, mgr) << std::endl;); - if (m_params.m_AggressiveUnrollTesting) { - literal l = mk_eq(testerVar, mk_string(iStr), false); - ctx.mark_as_relevant(l); - ctx.force_phase(l); - } - - orItems.push_back(testerEqAst); - zstring unrollStrInstance = get_unrolled_string(lcmStr, i); - - expr_ref x1(ctx.mk_eq_atom(testerEqAst, ctx.mk_eq_atom(var, mk_string(unrollStrInstance))), mgr); - TRACE("str", tout << "x1 = " << mk_pp(x1, mgr) << std::endl;); - andItems.push_back(x1); - - expr_ref x2(ctx.mk_eq_atom(testerEqAst, ctx.mk_eq_atom(mk_strlen(var), mk_int(i * lcmStr.length()))), mgr); - TRACE("str", tout << "x2 = " << mk_pp(x2, mgr) << std::endl;); - andItems.push_back(x2); - } - expr_ref testerEqMore(ctx.mk_eq_atom(testerVar, mk_string("more")), mgr); - TRACE("str", tout << "testerEqMore = " << mk_pp(testerEqMore, mgr) << std::endl;); - if (m_params.m_AggressiveUnrollTesting) { - literal l = mk_eq(testerVar, mk_string("more"), false); - ctx.mark_as_relevant(l); - ctx.force_phase(~l); - } - - orItems.push_back(testerEqMore); - int nextLowerLenBound = h * lcmStr.length(); - expr_ref more2(ctx.mk_eq_atom(testerEqMore, - //Z3_mk_ge(mk_length(t, var), mk_int(ctx, nextLowerLenBound)) - m_autil.mk_ge(m_autil.mk_add(mk_strlen(var), mk_int(-1 * nextLowerLenBound)), mk_int(0)) - ), mgr); - TRACE("str", tout << "more2 = " << mk_pp(more2, mgr) << std::endl;); - andItems.push_back(more2); - - expr_ref finalOR(mgr.mk_or(orItems.size(), orItems.c_ptr()), mgr); - TRACE("str", tout << "finalOR = " << mk_pp(finalOR, mgr) << std::endl;); - andItems.push_back(mk_or(orItems)); - - expr_ref finalAND(mgr.mk_and(andItems.size(), andItems.c_ptr()), mgr); - TRACE("str", tout << "finalAND = " << mk_pp(finalAND, mgr) << std::endl;); - - // doing the following avoids a segmentation fault - m_trail.push_back(finalAND); - return finalAND; -} - -expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tries) { - ast_manager & m = get_manager(); - context & ctx = get_context(); - - expr_ref freeVarLen(mk_strlen(freeVar), m); - SASSERT(freeVarLen); - - expr_ref_vector orList(m); - expr_ref_vector andList(m); - - int distance = 3; - int l = (tries - 1) * distance; - int h = tries * distance; - - TRACE("str", - tout << "building andList and orList" << std::endl; - if (m_params.m_AggressiveLengthTesting) { - tout << "note: aggressive length testing is active" << std::endl; - } - ); - - // experimental theory-aware case split support - literal_vector case_split_literals; - - for (int i = l; i < h; ++i) { - expr_ref str_indicator(m); - if (m_params.m_UseFastLengthTesterCache) { - rational ri(i); - expr * lookup_val; - if(lengthTesterCache.find(ri, lookup_val)) { - str_indicator = expr_ref(lookup_val, m); - } else { - // no match; create and insert - zstring i_str = int_to_string(i); - expr_ref new_val(mk_string(i_str), m); - lengthTesterCache.insert(ri, new_val); - m_trail.push_back(new_val); - str_indicator = expr_ref(new_val, m); - } - } else { - zstring i_str = int_to_string(i); - str_indicator = expr_ref(mk_string(i_str), m); - } - expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); - orList.push_back(or_expr); - - double priority; - // give high priority to small lengths if this is available - if (i <= 5) { - priority = 0.3; - } else { - // prioritize over "more" - priority = 0.2; - } - add_theory_aware_branching_info(or_expr, priority, l_true); - - if (m_params.m_AggressiveLengthTesting) { - literal l = mk_eq(indicator, str_indicator, false); - ctx.mark_as_relevant(l); - ctx.force_phase(l); - } - - case_split_literals.insert(mk_eq(freeVarLen, mk_int(i), false)); - - expr_ref and_expr(ctx.mk_eq_atom(orList.get(orList.size() - 1), m.mk_eq(freeVarLen, mk_int(i))), m); - andList.push_back(and_expr); - } - - expr_ref more_option(ctx.mk_eq_atom(indicator, mk_string("more")), m); - orList.push_back(more_option); - // decrease priority of this option - add_theory_aware_branching_info(more_option, -0.1, l_true); - if (m_params.m_AggressiveLengthTesting) { - literal l = mk_eq(indicator, mk_string("more"), false); - ctx.mark_as_relevant(l); - ctx.force_phase(~l); - } - - andList.push_back(ctx.mk_eq_atom(orList.get(orList.size() - 1), m_autil.mk_ge(freeVarLen, mk_int(h)))); - - /* - { // more experimental theory case split support - expr_ref tmp(m_autil.mk_ge(freeVarLen, mk_int(h)), m); - ctx.internalize(m_autil.mk_ge(freeVarLen, mk_int(h)), false); - case_split_literals.push_back(ctx.get_literal(tmp)); - ctx.mk_th_case_split(case_split_literals.size(), case_split_literals.c_ptr()); - } - */ - - expr_ref_vector or_items(m); - expr_ref_vector and_items(m); - - for (unsigned i = 0; i < orList.size(); ++i) { - or_items.push_back(orList.get(i)); - } - - and_items.push_back(mk_or(or_items)); - for(unsigned i = 0; i < andList.size(); ++i) { - and_items.push_back(andList.get(i)); - } - - TRACE("str", tout << "check: " << mk_pp(mk_and(and_items), m) << std::endl;); - - expr_ref lenTestAssert = mk_and(and_items); - SASSERT(lenTestAssert); - TRACE("str", tout << "crash avoidance lenTestAssert: " << mk_pp(lenTestAssert, m) << std::endl;); - - int testerCount = tries - 1; - if (testerCount > 0) { - expr_ref_vector and_items_LHS(m); - expr_ref moreAst(mk_string("more"), m); - for (int i = 0; i < testerCount; ++i) { - expr * indicator = fvar_lenTester_map[freeVar][i]; - if (internal_variable_set.find(indicator) == internal_variable_set.end()) { - TRACE("str", tout << "indicator " << mk_pp(indicator, m) << " out of scope; continuing" << std::endl;); - continue; - } else { - TRACE("str", tout << "indicator " << mk_pp(indicator, m) << " in scope" << std::endl;); - and_items_LHS.push_back(ctx.mk_eq_atom(indicator, moreAst)); - } - } - expr_ref assertL(mk_and(and_items_LHS), m); - SASSERT(assertL); - expr * finalAxiom = m.mk_or(m.mk_not(assertL), lenTestAssert.get()); - SASSERT(finalAxiom != NULL); - TRACE("str", tout << "crash avoidance finalAxiom: " << mk_pp(finalAxiom, m) << std::endl;); - return finalAxiom; - } else { - TRACE("str", tout << "crash avoidance lenTestAssert.get(): " << mk_pp(lenTestAssert.get(), m) << std::endl;); - m_trail.push_back(lenTestAssert.get()); - return lenTestAssert.get(); - } -} - -// Return an expression of the form -// (tester = "less" | tester = "N" | tester = "more") & -// (tester = "less" iff len(freeVar) < N) & (tester = "more" iff len(freeVar) > N) & (tester = "N" iff len(freeVar) = N)) -expr_ref theory_str::binary_search_case_split(expr * freeVar, expr * tester, binary_search_info & bounds, literal_vector & case_split) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - rational N = bounds.midPoint; - rational N_minus_one = N - rational::one(); - rational N_plus_one = N + rational::one(); - expr_ref lenFreeVar(mk_strlen(freeVar), m); - - TRACE("str", tout << "create case split for free var " << mk_pp(freeVar, m) - << " over " << mk_pp(tester, m) << " with midpoint " << N << std::endl;); - - expr_ref_vector combinedCaseSplit(m); - expr_ref_vector testerCases(m); - - expr_ref caseLess(ctx.mk_eq_atom(tester, mk_string("less")), m); - testerCases.push_back(caseLess); - combinedCaseSplit.push_back(ctx.mk_eq_atom(caseLess, m_autil.mk_le(lenFreeVar, m_autil.mk_numeral(N_minus_one, true) ))); - - expr_ref caseMore(ctx.mk_eq_atom(tester, mk_string("more")), m); - testerCases.push_back(caseMore); - combinedCaseSplit.push_back(ctx.mk_eq_atom(caseMore, m_autil.mk_ge(lenFreeVar, m_autil.mk_numeral(N_plus_one, true) ))); - - expr_ref caseEq(ctx.mk_eq_atom(tester, mk_string(N.to_string().c_str())), m); - testerCases.push_back(caseEq); - combinedCaseSplit.push_back(ctx.mk_eq_atom(caseEq, ctx.mk_eq_atom(lenFreeVar, m_autil.mk_numeral(N, true)))); - - combinedCaseSplit.push_back(mk_or(testerCases)); - - // force internalization on all terms in testerCases so we can extract literals - for (unsigned i = 0; i < testerCases.size(); ++i) { - expr * testerCase = testerCases.get(i); - if (!ctx.b_internalized(testerCase)) { - ctx.internalize(testerCase, false); - } - literal l = ctx.get_literal(testerCase); - case_split.push_back(l); - } - - expr_ref final_term(mk_and(combinedCaseSplit), m); - SASSERT(final_term); - TRACE("str", tout << "final term: " << mk_pp(final_term, m) << std::endl;); - return final_term; -} - -expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenTester, zstring previousLenTesterValue) { - ast_manager & m = get_manager(); - context & ctx = get_context(); - - if (binary_search_len_tester_stack.contains(freeVar) && !binary_search_len_tester_stack[freeVar].empty()) { - TRACE("str", tout << "checking existing length testers for " << mk_pp(freeVar, m) << std::endl; - for (ptr_vector::const_iterator it = binary_search_len_tester_stack[freeVar].begin(); - it != binary_search_len_tester_stack[freeVar].end(); ++it) { - expr * tester = *it; - tout << mk_pp(tester, m) << ": "; - if (binary_search_len_tester_info.contains(tester)) { - binary_search_info & bounds = binary_search_len_tester_info[tester]; - tout << "[" << bounds.lowerBound << " | " << bounds.midPoint << " | " << bounds.upperBound << "]!" << bounds.windowSize; - } else { - tout << "[WARNING: no bounds info available]"; - } - bool hasEqcValue; - expr * testerEqcValue = get_eqc_value(tester, hasEqcValue); - if (hasEqcValue) { - tout << " = " << mk_pp(testerEqcValue, m); - } else { - tout << " [no eqc value]"; - } - tout << std::endl; - } - ); - expr * lastTester = binary_search_len_tester_stack[freeVar].back(); - bool lastTesterHasEqcValue; - expr * lastTesterValue = get_eqc_value(lastTester, lastTesterHasEqcValue); - zstring lastTesterConstant; - if (!lastTesterHasEqcValue) { - TRACE("str", tout << "length tester " << mk_pp(lastTester, m) << " at top of stack doesn't have an EQC value yet" << std::endl;); - // check previousLenTester - if (previousLenTester == lastTester) { - lastTesterConstant = previousLenTesterValue; - TRACE("str", tout << "invoked with previousLenTester info matching top of stack" << std::endl;); - } else { - TRACE("str", tout << "WARNING: unexpected reordering of length testers!" << std::endl;); - UNREACHABLE(); return NULL; - } - } else { - u.str.is_string(lastTesterValue, lastTesterConstant); - } - TRACE("str", tout << "last length tester is assigned \"" << lastTesterConstant << "\"" << "\n";); - if (lastTesterConstant == "more" || lastTesterConstant == "less") { - // use the previous bounds info to generate a new midpoint - binary_search_info lastBounds; - if (!binary_search_len_tester_info.find(lastTester, lastBounds)) { - // unexpected - TRACE("str", tout << "WARNING: no bounds information available for last tester!" << std::endl;); - UNREACHABLE(); - } - TRACE("str", tout << "last bounds are [" << lastBounds.lowerBound << " | " << lastBounds.midPoint << " | " << lastBounds.upperBound << "]!" << lastBounds.windowSize << std::endl;); - binary_search_info newBounds; - expr * newTester; - if (lastTesterConstant == "more") { - // special case: if the midpoint, upper bound, and window size are all equal, - // we double the window size and adjust the bounds - if (lastBounds.midPoint == lastBounds.upperBound && lastBounds.upperBound == lastBounds.windowSize) { - TRACE("str", tout << "search hit window size; expanding" << std::endl;); - newBounds.lowerBound = lastBounds.windowSize + rational::one(); - newBounds.windowSize = lastBounds.windowSize * rational(2); - newBounds.upperBound = newBounds.windowSize; - newBounds.calculate_midpoint(); - } else if (false) { - // handle the case where the midpoint can't be increased further - // (e.g. a window like [50 | 50 | 50]!64 and we don't answer "50") - } else { - // general case - newBounds.lowerBound = lastBounds.midPoint + rational::one(); - newBounds.windowSize = lastBounds.windowSize; - newBounds.upperBound = lastBounds.upperBound; - newBounds.calculate_midpoint(); - } - if (!binary_search_next_var_high.find(lastTester, newTester)) { - newTester = mk_internal_lenTest_var(freeVar, newBounds.midPoint.get_int32()); - binary_search_next_var_high.insert(lastTester, newTester); - } - refresh_theory_var(newTester); - } else if (lastTesterConstant == "less") { - if (false) { - // handle the case where the midpoint can't be decreased further - // (e.g. a window like [0 | 0 | 0]!64 and we don't answer "0" - } else { - // general case - newBounds.upperBound = lastBounds.midPoint - rational::one(); - newBounds.windowSize = lastBounds.windowSize; - newBounds.lowerBound = lastBounds.lowerBound; - newBounds.calculate_midpoint(); - } - if (!binary_search_next_var_low.find(lastTester, newTester)) { - newTester = mk_internal_lenTest_var(freeVar, newBounds.midPoint.get_int32()); - binary_search_next_var_low.insert(lastTester, newTester); - } - refresh_theory_var(newTester); - } - TRACE("str", tout << "new bounds are [" << newBounds.lowerBound << " | " << newBounds.midPoint << " | " << newBounds.upperBound << "]!" << newBounds.windowSize << std::endl;); - binary_search_len_tester_stack[freeVar].push_back(newTester); - m_trail_stack.push(binary_search_trail(binary_search_len_tester_stack, freeVar)); - binary_search_len_tester_info.insert(newTester, newBounds); - m_trail_stack.push(insert_obj_map(binary_search_len_tester_info, newTester)); - - literal_vector case_split_literals; - expr_ref next_case_split(binary_search_case_split(freeVar, newTester, newBounds, case_split_literals)); - m_trail.push_back(next_case_split); - // ctx.mk_th_case_split(case_split_literals.size(), case_split_literals.c_ptr()); - return next_case_split; - } else { // lastTesterConstant is a concrete value - TRACE("str", tout << "length is fixed; generating models for free var" << std::endl;); - // defensive check that this length did not converge on a negative value. - binary_search_info lastBounds; - if (!binary_search_len_tester_info.find(lastTester, lastBounds)) { - // unexpected - TRACE("str", tout << "WARNING: no bounds information available for last tester!" << std::endl;); - UNREACHABLE(); - } - if (lastBounds.midPoint.is_neg()) { - TRACE("str", tout << "WARNING: length search converged on a negative value. Negating this constraint." << std::endl;); - expr_ref axiom(m_autil.mk_ge(mk_strlen(freeVar), m_autil.mk_numeral(rational::zero(), true)), m); - return axiom; - } - // length is fixed - expr * valueAssert = gen_free_var_options(freeVar, lastTester, lastTesterConstant, NULL, zstring("")); - return valueAssert; - } - } else { - // no length testers yet - TRACE("str", tout << "no length testers for " << mk_pp(freeVar, m) << std::endl;); - binary_search_len_tester_stack.insert(freeVar, ptr_vector()); - - expr * firstTester; - rational lowerBound(0); - rational upperBound(m_params.m_BinarySearchInitialUpperBound); - rational windowSize(upperBound); - rational midPoint(floor(upperBound / rational(2))); - if (!binary_search_starting_len_tester.find(freeVar, firstTester)) { - firstTester = mk_internal_lenTest_var(freeVar, midPoint.get_int32()); - binary_search_starting_len_tester.insert(freeVar, firstTester); - } - refresh_theory_var(firstTester); - - binary_search_len_tester_stack[freeVar].push_back(firstTester); - m_trail_stack.push(binary_search_trail(binary_search_len_tester_stack, freeVar)); - binary_search_info new_info(lowerBound, midPoint, upperBound, windowSize); - binary_search_len_tester_info.insert(firstTester, new_info); - m_trail_stack.push(insert_obj_map(binary_search_len_tester_info, firstTester)); - - literal_vector case_split_literals; - expr_ref initial_case_split(binary_search_case_split(freeVar, firstTester, new_info, case_split_literals)); - m_trail.push_back(initial_case_split); - // ctx.mk_th_case_split(case_split_literals.size(), case_split_literals.c_ptr()); - return initial_case_split; - } -} - -// ----------------------------------------------------------------------------------------------------- -// True branch will be taken in final_check: -// - When we discover a variable is "free" for the first time -// lenTesterInCbEq = NULL -// lenTesterValue = "" -// False branch will be taken when invoked by new_eq_eh(). -// - After we set up length tester for a "free" var in final_check, -// when the tester is assigned to some value (e.g. "more" or "4"), -// lenTesterInCbEq != NULL, and its value will be passed by lenTesterValue -// The difference is that in new_eq_eh(), lenTesterInCbEq and its value have NOT been put into a same eqc -// ----------------------------------------------------------------------------------------------------- -expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTesterInCbEq, zstring lenTesterValue) { - - ast_manager & m = get_manager(); - - TRACE("str", tout << "gen for free var " << mk_ismt2_pp(freeVar, m) << std::endl;); - - if (m_params.m_UseBinarySearch) { - TRACE("str", tout << "using binary search heuristic" << std::endl;); - return binary_search_length_test(freeVar, lenTesterInCbEq, lenTesterValue); - } else { - bool map_effectively_empty = false; - if (!fvar_len_count_map.contains(freeVar)) { - TRACE("str", tout << "fvar_len_count_map is empty" << std::endl;); - map_effectively_empty = true; + expr_ref theory_str::set_up_finite_model_test(expr * lhs, expr * rhs) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + TRACE("str", tout << "activating finite model testing for overlapping concats " + << mk_pp(lhs, m) << " and " << mk_pp(rhs, m) << std::endl;); + std::map concatMap; + std::map unrollMap; + std::map varMap; + classify_ast_by_type(lhs, varMap, concatMap, unrollMap); + classify_ast_by_type(rhs, varMap, concatMap, unrollMap); + TRACE("str", tout << "found vars:"; + for (std::map::iterator it = varMap.begin(); it != varMap.end(); ++it) { + tout << " " << mk_pp(it->first, m); + } + tout << std::endl; + ); + + expr_ref testvar(mk_str_var("finiteModelTest"), m); + m_trail.push_back(testvar); + ptr_vector varlist; + + for (std::map::iterator it = varMap.begin(); it != varMap.end(); ++it) { + expr * v = it->first; + varlist.push_back(v); } - if (!map_effectively_empty) { - // check whether any entries correspond to variables that went out of scope; - // if every entry is out of scope then the map counts as being empty + // make things easy for the core wrt. testvar + expr_ref t1(ctx.mk_eq_atom(testvar, mk_string("")), m); + expr_ref t_yes(ctx.mk_eq_atom(testvar, mk_string("yes")), m); + expr_ref testvaraxiom(m.mk_or(t1, t_yes), m); + assert_axiom(testvaraxiom); - // assume empty and find a counterexample - map_effectively_empty = true; - ptr_vector indicator_set = fvar_lenTester_map[freeVar]; - for (ptr_vector::iterator it = indicator_set.begin(); it != indicator_set.end(); ++it) { - expr * indicator = *it; - if (internal_variable_set.find(indicator) != internal_variable_set.end()) { - TRACE("str", tout <<"found active internal variable " << mk_ismt2_pp(indicator, m) - << " in fvar_lenTester_map[freeVar]" << std::endl;); - map_effectively_empty = false; - break; - } - } - CTRACE("str", map_effectively_empty, tout << "all variables in fvar_lenTester_map[freeVar] out of scope" << std::endl;); - } + finite_model_test_varlists.insert(testvar, varlist); + m_trail_stack.push(insert_obj_map >(finite_model_test_varlists, testvar) ); + return t_yes; + } - if (map_effectively_empty) { - // no length assertions for this free variable have ever been added. - TRACE("str", tout << "no length assertions yet" << std::endl;); + void theory_str::finite_model_test(expr * testvar, expr * str) { + context & ctx = get_context(); + ast_manager & m = get_manager(); - fvar_len_count_map.insert(freeVar, 1); - unsigned int testNum = fvar_len_count_map[freeVar]; - - expr_ref indicator(mk_internal_lenTest_var(freeVar, testNum), m); - SASSERT(indicator); - - // since the map is "effectively empty", we can remove those variables that have left scope... - fvar_lenTester_map[freeVar].shrink(0); - fvar_lenTester_map[freeVar].push_back(indicator); - lenTester_fvar_map.insert(indicator, freeVar); - - expr * lenTestAssert = gen_len_test_options(freeVar, indicator, testNum); - SASSERT(lenTestAssert != NULL); - return lenTestAssert; - } else { - TRACE("str", tout << "found previous in-scope length assertions" << std::endl;); - - expr * effectiveLenInd = NULL; - zstring effectiveLenIndiStr(""); - int lenTesterCount = (int) fvar_lenTester_map[freeVar].size(); - - TRACE("str", - tout << lenTesterCount << " length testers in fvar_lenTester_map[" << mk_pp(freeVar, m) << "]:" << std::endl; - for (int i = 0; i < lenTesterCount; ++i) { - expr * len_indicator = fvar_lenTester_map[freeVar][i]; - tout << mk_pp(len_indicator, m) << ": "; - bool effectiveInScope = (internal_variable_set.find(len_indicator) != internal_variable_set.end()); - tout << (effectiveInScope ? "in scope" : "NOT in scope"); - tout << std::endl; - } - ); - - int i = 0; - for (; i < lenTesterCount; ++i) { - expr * len_indicator_pre = fvar_lenTester_map[freeVar][i]; - // check whether this is in scope as well - if (internal_variable_set.find(len_indicator_pre) == internal_variable_set.end()) { - TRACE("str", tout << "length indicator " << mk_pp(len_indicator_pre, m) << " not in scope" << std::endl;); + zstring s; + if (!u.str.is_string(str, s)) return; + if (s == "yes") { + TRACE("str", tout << "start finite model test for " << mk_pp(testvar, m) << std::endl;); + ptr_vector & vars = finite_model_test_varlists[testvar]; + for (ptr_vector::iterator it = vars.begin(); it != vars.end(); ++it) { + expr * v = *it; + bool v_has_eqc = false; + get_eqc_value(v, v_has_eqc); + if (v_has_eqc) { + TRACE("str", tout << "variable " << mk_pp(v,m) << " already equivalent to a string constant" << std::endl;); continue; } + // check for any sort of existing length tester we might interfere with + if (m_params.m_UseBinarySearch) { + if (binary_search_len_tester_stack.contains(v) && !binary_search_len_tester_stack[v].empty()) { + TRACE("str", tout << "already found existing length testers for " << mk_pp(v, m) << std::endl;); + continue; + } else { + // start binary search as normal + expr_ref implLhs(ctx.mk_eq_atom(testvar, str), m); + expr_ref implRhs(binary_search_length_test(v, NULL, ""), m); + assert_implication(implLhs, implRhs); + } + } else { + bool map_effectively_empty = false; + if (!fvar_len_count_map.contains(v)) { + map_effectively_empty = true; + } + if (!map_effectively_empty) { + map_effectively_empty = true; + ptr_vector indicator_set = fvar_lenTester_map[v]; + for (ptr_vector::iterator it = indicator_set.begin(); it != indicator_set.end(); ++it) { + expr * indicator = *it; + if (internal_variable_set.find(indicator) != internal_variable_set.end()) { + map_effectively_empty = false; + break; + } + } + } + + if (map_effectively_empty) { + TRACE("str", tout << "no existing length testers for " << mk_pp(v, m) << std::endl;); + rational v_len; + rational v_lower_bound; + rational v_upper_bound; + expr_ref vLengthExpr(mk_strlen(v), m); + if (get_len_value(v, v_len)) { + TRACE("str", tout << "length = " << v_len.to_string() << std::endl;); + v_lower_bound = v_len; + v_upper_bound = v_len; + } else { + bool lower_bound_exists = lower_bound(vLengthExpr, v_lower_bound); + bool upper_bound_exists = upper_bound(vLengthExpr, v_upper_bound); + TRACE("str", tout << "bounds = [" << (lower_bound_exists?v_lower_bound.to_string():"?") + << ".." << (upper_bound_exists?v_upper_bound.to_string():"?") << "]" << std::endl;); + + // make sure the bounds are non-negative + if (lower_bound_exists && v_lower_bound.is_neg()) { + v_lower_bound = rational::zero(); + } + if (upper_bound_exists && v_upper_bound.is_neg()) { + v_upper_bound = rational::zero(); + } + + if (lower_bound_exists && upper_bound_exists) { + // easiest case. we will search within these bounds + } else if (upper_bound_exists && !lower_bound_exists) { + // search between 0 and the upper bound + v_lower_bound == rational::zero(); + } else if (lower_bound_exists && !upper_bound_exists) { + // check some finite portion of the search space + v_upper_bound = v_lower_bound + rational(10); + } else { + // no bounds information + v_lower_bound = rational::zero(); + v_upper_bound = v_lower_bound + rational(10); + } + } + // now create a fake length tester over this finite disjunction of lengths + + fvar_len_count_map[v] = 1; + unsigned int testNum = fvar_len_count_map[v]; + + expr_ref indicator(mk_internal_lenTest_var(v, testNum), m); + SASSERT(indicator); + m_trail.push_back(indicator); + + fvar_lenTester_map[v].shrink(0); + fvar_lenTester_map[v].push_back(indicator); + lenTester_fvar_map[indicator] = v; + + expr_ref_vector orList(m); + expr_ref_vector andList(m); + + for (rational l = v_lower_bound; l <= v_upper_bound; l += rational::one()) { + zstring lStr = zstring(l.to_string().c_str()); + expr_ref str_indicator(mk_string(lStr), m); + expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); + orList.push_back(or_expr); + expr_ref and_expr(ctx.mk_eq_atom(or_expr, ctx.mk_eq_atom(vLengthExpr, m_autil.mk_numeral(l, true))), m); + andList.push_back(and_expr); + } + andList.push_back(mk_or(orList)); + expr_ref implLhs(ctx.mk_eq_atom(testvar, str), m); + expr_ref implRhs(mk_and(andList), m); + assert_implication(implLhs, implRhs); + } else { + TRACE("str", tout << "already found existing length testers for " << mk_pp(v, m) << std::endl;); + continue; + } + } + } // foreach (v in vars) + } // (s == "yes") + } + + void theory_str::more_len_tests(expr * lenTester, zstring lenTesterValue) { + ast_manager & m = get_manager(); + if (lenTester_fvar_map.contains(lenTester)) { + expr * fVar = lenTester_fvar_map[lenTester]; + expr * toAssert = gen_len_val_options_for_free_var(fVar, lenTester, lenTesterValue); + TRACE("str", tout << "asserting more length tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); + if (toAssert != NULL) { + assert_axiom(toAssert); + } + } + } + + void theory_str::more_value_tests(expr * valTester, zstring valTesterValue) { + ast_manager & m = get_manager(); + + expr * fVar = valueTester_fvar_map[valTester]; + if (m_params.m_UseBinarySearch) { + if (!binary_search_len_tester_stack.contains(fVar) || binary_search_len_tester_stack[fVar].empty()) { + TRACE("str", tout << "WARNING: no active length testers for " << mk_pp(fVar, m) << std::endl;); + NOT_IMPLEMENTED_YET(); + } + expr * effectiveLenInd = binary_search_len_tester_stack[fVar].back(); + bool hasEqcValue; + expr * len_indicator_value = get_eqc_value(effectiveLenInd, hasEqcValue); + if (!hasEqcValue) { + TRACE("str", tout << "WARNING: length tester " << mk_pp(effectiveLenInd, m) << " at top of stack for " << mk_pp(fVar, m) << " has no EQC value" << std::endl;); + } else { + // safety check + zstring effectiveLenIndiStr; + u.str.is_string(len_indicator_value, effectiveLenIndiStr); + if (effectiveLenIndiStr == "more" || effectiveLenIndiStr == "less") { + TRACE("str", tout << "ERROR: illegal state -- requesting 'more value tests' but a length tester is not yet concrete!" << std::endl;); + UNREACHABLE(); + } + expr * valueAssert = gen_free_var_options(fVar, effectiveLenInd, effectiveLenIndiStr, valTester, valTesterValue); + TRACE("str", tout << "asserting more value tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); + if (valueAssert != NULL) { + assert_axiom(valueAssert); + } + } + } else { + int lenTesterCount = fvar_lenTester_map[fVar].size(); + + expr * effectiveLenInd = NULL; + zstring effectiveLenIndiStr = ""; + for (int i = 0; i < lenTesterCount; ++i) { + expr * len_indicator_pre = fvar_lenTester_map[fVar][i]; bool indicatorHasEqcValue = false; expr * len_indicator_value = get_eqc_value(len_indicator_pre, indicatorHasEqcValue); - TRACE("str", tout << "length indicator " << mk_ismt2_pp(len_indicator_pre, m) << - " = " << mk_ismt2_pp(len_indicator_value, m) << std::endl;); if (indicatorHasEqcValue) { zstring len_pIndiStr; u.str.is_string(len_indicator_value, len_pIndiStr); @@ -10281,318 +6974,3623 @@ expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTe effectiveLenIndiStr = len_pIndiStr; break; } - } else { - if (lenTesterInCbEq != len_indicator_pre) { - TRACE("str", tout << "WARNING: length indicator " << mk_ismt2_pp(len_indicator_pre, m) - << " does not have an equivalence class value." - << " i = " << i << ", lenTesterCount = " << lenTesterCount << std::endl;); - if (i > 0) { - effectiveLenInd = fvar_lenTester_map[freeVar][i - 1]; - bool effectiveHasEqcValue; - expr * effective_eqc_value = get_eqc_value(effectiveLenInd, effectiveHasEqcValue); - bool effectiveInScope = (internal_variable_set.find(effectiveLenInd) != internal_variable_set.end()); - TRACE("str", tout << "checking effective length indicator " << mk_pp(effectiveLenInd, m) << ": " - << (effectiveInScope ? "in scope" : "NOT in scope") << ", "; - if (effectiveHasEqcValue) { - tout << "~= " << mk_pp(effective_eqc_value, m); - } else { - tout << "no eqc string constant"; - } - tout << std::endl;); - if (effectiveLenInd == lenTesterInCbEq) { - effectiveLenIndiStr = lenTesterValue; + } + } + expr * valueAssert = gen_free_var_options(fVar, effectiveLenInd, effectiveLenIndiStr, valTester, valTesterValue); + TRACE("str", tout << "asserting more value tests for free variable " << mk_ismt2_pp(fVar, m) << std::endl;); + if (valueAssert != NULL) { + assert_axiom(valueAssert); + } + } + } + + bool theory_str::free_var_attempt(expr * nn1, expr * nn2) { + ast_manager & m = get_manager(); + zstring nn2_str; + if (internal_lenTest_vars.contains(nn1) && u.str.is_string(nn2, nn2_str)) { + TRACE("str", tout << "acting on equivalence between length tester var " << mk_ismt2_pp(nn1, m) + << " and constant " << mk_ismt2_pp(nn2, m) << std::endl;); + more_len_tests(nn1, nn2_str); + return true; + } else if (internal_valTest_vars.contains(nn1) && u.str.is_string(nn2, nn2_str)) { + if (nn2_str == "more") { + TRACE("str", tout << "acting on equivalence between value var " << mk_ismt2_pp(nn1, m) + << " and constant " << mk_ismt2_pp(nn2, m) << std::endl;); + more_value_tests(nn1, nn2_str); + } + return true; + } else if (internal_unrollTest_vars.contains(nn1)) { + return true; + } else { + return false; + } + } + + void theory_str::handle_equality(expr * lhs, expr * rhs) { + ast_manager & m = get_manager(); + context & ctx = get_context(); + // both terms must be of sort String + sort * lhs_sort = m.get_sort(lhs); + sort * rhs_sort = m.get_sort(rhs); + sort * str_sort = u.str.mk_string_sort(); + + if (lhs_sort != str_sort || rhs_sort != str_sort) { + TRACE("str", tout << "skip equality: not String sort" << std::endl;); + return; + } + + /* // temporarily disabled, we are borrowing these testers for something else + if (m_params.m_FiniteOverlapModels && !finite_model_test_varlists.empty()) { + if (finite_model_test_varlists.contains(lhs)) { + finite_model_test(lhs, rhs); return; + } else if (finite_model_test_varlists.contains(rhs)) { + finite_model_test(rhs, lhs); return; + } + } + */ + + if (free_var_attempt(lhs, rhs) || free_var_attempt(rhs, lhs)) { + return; + } + + if (u.str.is_concat(to_app(lhs)) && u.str.is_concat(to_app(rhs))) { + bool nn1HasEqcValue = false; + bool nn2HasEqcValue = false; + expr * nn1_value = get_eqc_value(lhs, nn1HasEqcValue); + expr * nn2_value = get_eqc_value(rhs, nn2HasEqcValue); + if (nn1HasEqcValue && !nn2HasEqcValue) { + simplify_parent(rhs, nn1_value); + } + if (!nn1HasEqcValue && nn2HasEqcValue) { + simplify_parent(lhs, nn2_value); + } + + expr * nn1_arg0 = to_app(lhs)->get_arg(0); + expr * nn1_arg1 = to_app(lhs)->get_arg(1); + expr * nn2_arg0 = to_app(rhs)->get_arg(0); + expr * nn2_arg1 = to_app(rhs)->get_arg(1); + if (nn1_arg0 == nn2_arg0 && in_same_eqc(nn1_arg1, nn2_arg1)) { + TRACE("str", tout << "skip: lhs arg0 == rhs arg0" << std::endl;); + return; + } + + if (nn1_arg1 == nn2_arg1 && in_same_eqc(nn1_arg0, nn2_arg0)) { + TRACE("str", tout << "skip: lhs arg1 == rhs arg1" << std::endl;); + return; + } + } + + if (opt_DeferEQCConsistencyCheck) { + TRACE("str", tout << "opt_DeferEQCConsistencyCheck is set; deferring new_eq_check call" << std::endl;); + } else { + // newEqCheck() -- check consistency wrt. existing equivalence classes + if (!new_eq_check(lhs, rhs)) { + return; + } + } + + // BEGIN new_eq_handler() in strTheory + + { + rational nn1Len, nn2Len; + bool nn1Len_exists = get_len_value(lhs, nn1Len); + bool nn2Len_exists = get_len_value(rhs, nn2Len); + expr * emptyStr = mk_string(""); + + if (nn1Len_exists && nn1Len.is_zero()) { + if (!in_same_eqc(lhs, emptyStr) && rhs != emptyStr) { + expr_ref eql(ctx.mk_eq_atom(mk_strlen(lhs), mk_int(0)), m); + expr_ref eqr(ctx.mk_eq_atom(lhs, emptyStr), m); + expr_ref toAssert(ctx.mk_eq_atom(eql, eqr), m); + assert_axiom(toAssert); + } + } + + if (nn2Len_exists && nn2Len.is_zero()) { + if (!in_same_eqc(rhs, emptyStr) && lhs != emptyStr) { + expr_ref eql(ctx.mk_eq_atom(mk_strlen(rhs), mk_int(0)), m); + expr_ref eqr(ctx.mk_eq_atom(rhs, emptyStr), m); + expr_ref toAssert(ctx.mk_eq_atom(eql, eqr), m); + assert_axiom(toAssert); + } + } + } + + instantiate_str_eq_length_axiom(ctx.get_enode(lhs), ctx.get_enode(rhs)); + + // group terms by equivalence class (groupNodeInEqc()) + + std::set eqc_concat_lhs; + std::set eqc_var_lhs; + std::set eqc_const_lhs; + group_terms_by_eqc(lhs, eqc_concat_lhs, eqc_var_lhs, eqc_const_lhs); + + std::set eqc_concat_rhs; + std::set eqc_var_rhs; + std::set eqc_const_rhs; + group_terms_by_eqc(rhs, eqc_concat_rhs, eqc_var_rhs, eqc_const_rhs); + + TRACE("str", + tout << "lhs eqc:" << std::endl; + tout << "Concats:" << std::endl; + for (std::set::iterator it = eqc_concat_lhs.begin(); it != eqc_concat_lhs.end(); ++it) { + expr * ex = *it; + tout << mk_ismt2_pp(ex, get_manager()) << std::endl; + } + tout << "Variables:" << std::endl; + for (std::set::iterator it = eqc_var_lhs.begin(); it != eqc_var_lhs.end(); ++it) { + expr * ex = *it; + tout << mk_ismt2_pp(ex, get_manager()) << std::endl; + } + tout << "Constants:" << std::endl; + for (std::set::iterator it = eqc_const_lhs.begin(); it != eqc_const_lhs.end(); ++it) { + expr * ex = *it; + tout << mk_ismt2_pp(ex, get_manager()) << std::endl; + } + + tout << "rhs eqc:" << std::endl; + tout << "Concats:" << std::endl; + for (std::set::iterator it = eqc_concat_rhs.begin(); it != eqc_concat_rhs.end(); ++it) { + expr * ex = *it; + tout << mk_ismt2_pp(ex, get_manager()) << std::endl; + } + tout << "Variables:" << std::endl; + for (std::set::iterator it = eqc_var_rhs.begin(); it != eqc_var_rhs.end(); ++it) { + expr * ex = *it; + tout << mk_ismt2_pp(ex, get_manager()) << std::endl; + } + tout << "Constants:" << std::endl; + for (std::set::iterator it = eqc_const_rhs.begin(); it != eqc_const_rhs.end(); ++it) { + expr * ex = *it; + tout << mk_ismt2_pp(ex, get_manager()) << std::endl; + } + ); + + // step 1: Concat == Concat + int hasCommon = 0; + if (eqc_concat_lhs.size() != 0 && eqc_concat_rhs.size() != 0) { + std::set::iterator itor1 = eqc_concat_lhs.begin(); + std::set::iterator itor2 = eqc_concat_rhs.begin(); + for (; itor1 != eqc_concat_lhs.end(); itor1++) { + if (eqc_concat_rhs.find(*itor1) != eqc_concat_rhs.end()) { + hasCommon = 1; + break; + } + } + for (; itor2 != eqc_concat_rhs.end(); itor2++) { + if (eqc_concat_lhs.find(*itor2) != eqc_concat_lhs.end()) { + hasCommon = 1; + break; + } + } + if (hasCommon == 0) { + if (opt_ConcatOverlapAvoid) { + bool found = false; + // check each pair and take the first ones that won't immediately overlap + for (itor1 = eqc_concat_lhs.begin(); itor1 != eqc_concat_lhs.end() && !found; ++itor1) { + expr * concat_lhs = *itor1; + for (itor2 = eqc_concat_rhs.begin(); itor2 != eqc_concat_rhs.end() && !found; ++itor2) { + expr * concat_rhs = *itor2; + if (will_result_in_overlap(concat_lhs, concat_rhs)) { + TRACE("str", tout << "Concats " << mk_pp(concat_lhs, m) << " and " + << mk_pp(concat_rhs, m) << " will result in overlap; skipping." << std::endl;); } else { - if (effectiveHasEqcValue) { - u.str.is_string(effective_eqc_value, effectiveLenIndiStr); - } else { - NOT_IMPLEMENTED_YET(); - } + TRACE("str", tout << "Concats " << mk_pp(concat_lhs, m) << " and " + << mk_pp(concat_rhs, m) << " won't overlap. Simplifying here." << std::endl;); + simplify_concat_equality(concat_lhs, concat_rhs); + found = true; + break; } } - break; } - // lenTesterInCbEq == len_indicator_pre - else { - if (lenTesterValue != "more") { - effectiveLenInd = len_indicator_pre; - effectiveLenIndiStr = lenTesterValue; + if (!found) { + TRACE("str", tout << "All pairs of concats expected to overlap, falling back." << std::endl;); + simplify_concat_equality(*(eqc_concat_lhs.begin()), *(eqc_concat_rhs.begin())); + } + } else { + // default behaviour + simplify_concat_equality(*(eqc_concat_lhs.begin()), *(eqc_concat_rhs.begin())); + } + } + } + + // step 2: Concat == Constant + + if (eqc_const_lhs.size() != 0) { + expr * conStr = *(eqc_const_lhs.begin()); + std::set::iterator itor2 = eqc_concat_rhs.begin(); + for (; itor2 != eqc_concat_rhs.end(); itor2++) { + solve_concat_eq_str(*itor2, conStr); + } + } else if (eqc_const_rhs.size() != 0) { + expr* conStr = *(eqc_const_rhs.begin()); + std::set::iterator itor1 = eqc_concat_lhs.begin(); + for (; itor1 != eqc_concat_lhs.end(); itor1++) { + solve_concat_eq_str(*itor1, conStr); + } + } + + // simplify parents wrt. the equivalence class of both sides + bool nn1HasEqcValue = false; + bool nn2HasEqcValue = false; + // we want the Z3str2 eqc check here... + expr * nn1_value = z3str2_get_eqc_value(lhs, nn1HasEqcValue); + expr * nn2_value = z3str2_get_eqc_value(rhs, nn2HasEqcValue); + if (nn1HasEqcValue && !nn2HasEqcValue) { + simplify_parent(rhs, nn1_value); + } + + if (!nn1HasEqcValue && nn2HasEqcValue) { + simplify_parent(lhs, nn2_value); + } + + expr * nn1EqConst = NULL; + std::set nn1EqUnrollFuncs; + get_eqc_allUnroll(lhs, nn1EqConst, nn1EqUnrollFuncs); + expr * nn2EqConst = NULL; + std::set nn2EqUnrollFuncs; + get_eqc_allUnroll(rhs, nn2EqConst, nn2EqUnrollFuncs); + + if (nn2EqConst != NULL) { + for (std::set::iterator itor1 = nn1EqUnrollFuncs.begin(); itor1 != nn1EqUnrollFuncs.end(); itor1++) { + process_unroll_eq_const_str(*itor1, nn2EqConst); + } + } + + if (nn1EqConst != NULL) { + for (std::set::iterator itor2 = nn2EqUnrollFuncs.begin(); itor2 != nn2EqUnrollFuncs.end(); itor2++) { + process_unroll_eq_const_str(*itor2, nn1EqConst); + } + } + + } + + void theory_str::set_up_axioms(expr * ex) { + ast_manager & m = get_manager(); + context & ctx = get_context(); + + sort * ex_sort = m.get_sort(ex); + sort * str_sort = u.str.mk_string_sort(); + sort * bool_sort = m.mk_bool_sort(); + + family_id m_arith_fid = m.mk_family_id("arith"); + sort * int_sort = m.mk_sort(m_arith_fid, INT_SORT); + + if (ex_sort == str_sort) { + TRACE("str", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << + ": expr is of sort String" << std::endl;); + // set up basic string axioms + enode * n = ctx.get_enode(ex); + SASSERT(n); + m_basicstr_axiom_todo.push_back(n); + TRACE("str", tout << "add " << mk_pp(ex, m) << " to m_basicstr_axiom_todo" << std::endl;); + + + if (is_app(ex)) { + app * ap = to_app(ex); + if (u.str.is_concat(ap)) { + // if ex is a concat, set up concat axioms later + m_concat_axiom_todo.push_back(n); + // we also want to check whether we can eval this concat, + // in case the rewriter did not totally finish with this term + m_concat_eval_todo.push_back(n); + } else if (u.str.is_length(ap)) { + // if the argument is a variable, + // keep track of this for later, we'll need it during model gen + expr * var = ap->get_arg(0); + app * aVar = to_app(var); + if (aVar->get_num_args() == 0 && !u.str.is_string(aVar)) { + input_var_in_len.insert(var); + } + } else if (u.str.is_at(ap) || u.str.is_extract(ap) || u.str.is_replace(ap)) { + m_library_aware_axiom_todo.push_back(n); + } else if (u.str.is_itos(ap)) { + TRACE("str", tout << "found string-integer conversion term: " << mk_pp(ex, get_manager()) << std::endl;); + string_int_conversion_terms.push_back(ap); + m_library_aware_axiom_todo.push_back(n); + } else if (ap->get_num_args() == 0 && !u.str.is_string(ap)) { + // if ex is a variable, add it to our list of variables + TRACE("str", tout << "tracking variable " << mk_ismt2_pp(ap, get_manager()) << std::endl;); + variable_set.insert(ex); + ctx.mark_as_relevant(ex); + // this might help?? + theory_var v = mk_var(n); + TRACE("str", tout << "variable " << mk_ismt2_pp(ap, get_manager()) << " is #" << v << std::endl;); + } + } + } else if (ex_sort == bool_sort) { + TRACE("str", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << + ": expr is of sort Bool" << std::endl;); + // set up axioms for boolean terms + + ensure_enode(ex); + if (ctx.e_internalized(ex)) { + enode * n = ctx.get_enode(ex); + SASSERT(n); + + if (is_app(ex)) { + app * ap = to_app(ex); + if (u.str.is_prefix(ap) || u.str.is_suffix(ap) || u.str.is_contains(ap) || u.str.is_in_re(ap)) { + m_library_aware_axiom_todo.push_back(n); + } + } + } else { + TRACE("str", tout << "WARNING: Bool term " << mk_ismt2_pp(ex, get_manager()) << " not internalized. Delaying axiom setup to prevent a crash." << std::endl;); + ENSURE(!search_started); // infinite loop prevention + m_delayed_axiom_setup_terms.push_back(ex); + return; + } + } else if (ex_sort == int_sort) { + TRACE("str", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << + ": expr is of sort Int" << std::endl;); + // set up axioms for integer terms + enode * n = ensure_enode(ex); + SASSERT(n); + + if (is_app(ex)) { + app * ap = to_app(ex); + // TODO indexof2/lastindexof + if (u.str.is_index(ap) /* || is_Indexof2(ap) || is_LastIndexof(ap) */) { + m_library_aware_axiom_todo.push_back(n); + } else if (u.str.is_stoi(ap)) { + TRACE("str", tout << "found string-integer conversion term: " << mk_pp(ex, get_manager()) << std::endl;); + string_int_conversion_terms.push_back(ap); + m_library_aware_axiom_todo.push_back(n); + } + } + } else { + TRACE("str", tout << "setting up axioms for " << mk_ismt2_pp(ex, get_manager()) << + ": expr is of wrong sort, ignoring" << std::endl;); + } + + // if expr is an application, recursively inspect all arguments + if (is_app(ex)) { + app * term = (app*)ex; + unsigned num_args = term->get_num_args(); + for (unsigned i = 0; i < num_args; i++) { + set_up_axioms(term->get_arg(i)); + } + } + } + + void theory_str::add_theory_assumptions(expr_ref_vector & assumptions) { + TRACE("str", tout << "add overlap assumption for theory_str" << std::endl;); + symbol strOverlap("!!TheoryStrOverlapAssumption!!"); + seq_util m_sequtil(get_manager()); + sort * s = get_manager().mk_bool_sort(); + m_theoryStrOverlapAssumption_term = expr_ref(get_manager().mk_const(strOverlap, s), get_manager()); + assumptions.push_back(get_manager().mk_not(m_theoryStrOverlapAssumption_term)); + } + + lbool theory_str::validate_unsat_core(expr_ref_vector & unsat_core) { + bool assumptionFound = false; + + app * target_term = to_app(get_manager().mk_not(m_theoryStrOverlapAssumption_term)); + get_context().internalize(target_term, false); + for (unsigned i = 0; i < unsat_core.size(); ++i) { + app * core_term = to_app(unsat_core.get(i)); + // not sure if this is the correct way to compare terms in this context + enode * e1; + enode * e2; + e1 = get_context().get_enode(target_term); + e2 = get_context().get_enode(core_term); + if (e1 == e2) { + TRACE("str", tout << "overlap detected in unsat core, changing UNSAT to UNKNOWN" << std::endl;); + assumptionFound = true; + return l_undef; + } + } + + return l_false; + } + + void theory_str::init_search_eh() { + ast_manager & m = get_manager(); + context & ctx = get_context(); + + TRACE("str", + tout << "dumping all asserted formulas:" << std::endl; + unsigned nFormulas = ctx.get_num_asserted_formulas(); + for (unsigned i = 0; i < nFormulas; ++i) { + expr * ex = ctx.get_asserted_formula(i); + tout << mk_ismt2_pp(ex, m) << (ctx.is_relevant(ex) ? " (rel)" : " (NOT REL)") << std::endl; + } + ); + /* + * Recursive descent through all asserted formulas to set up axioms. + * Note that this is just the input structure and not necessarily things + * that we know to be true or false. We're just doing this to see + * which terms are explicitly mentioned. + */ + unsigned nFormulas = ctx.get_num_asserted_formulas(); + for (unsigned i = 0; i < nFormulas; ++i) { + expr * ex = ctx.get_asserted_formula(i); + set_up_axioms(ex); + } + + /* + * Similar recursive descent, except over all initially assigned terms. + * This is done to find equalities between terms, etc. that we otherwise + * might not get a chance to see. + */ + + /* + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { + expr * ex = *i; + if (m.is_eq(ex)) { + TRACE("str", tout << "processing assignment " << mk_ismt2_pp(ex, m) << + ": expr is equality" << std::endl;); + app * eq = (app*)ex; + SASSERT(eq->get_num_args() == 2); + expr * lhs = eq->get_arg(0); + expr * rhs = eq->get_arg(1); + + enode * e_lhs = ctx.get_enode(lhs); + enode * e_rhs = ctx.get_enode(rhs); + std::pair eq_pair(e_lhs, e_rhs); + m_str_eq_todo.push_back(eq_pair); + } else { + TRACE("str", tout << "processing assignment " << mk_ismt2_pp(ex, m) + << ": expr ignored" << std::endl;); + } + } + */ + + // this might be cheating but we need to make sure that certain maps are populated + // before the first call to new_eq_eh() + propagate(); + + TRACE("str", tout << "search started" << std::endl;); + search_started = true; + } + + void theory_str::new_eq_eh(theory_var x, theory_var y) { + //TRACE("str", tout << "new eq: v#" << x << " = v#" << y << std::endl;); + TRACE("str", tout << "new eq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " = " << + mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); + + /* + if (m_find.find(x) == m_find.find(y)) { + return; + } + */ + handle_equality(get_enode(x)->get_owner(), get_enode(y)->get_owner()); + + // replicate Z3str2 behaviour: merge eqc **AFTER** handle_equality + m_find.merge(x, y); + } + + void theory_str::new_diseq_eh(theory_var x, theory_var y) { + //TRACE("str", tout << "new diseq: v#" << x << " != v#" << y << std::endl;); + TRACE("str", tout << "new diseq: " << mk_ismt2_pp(get_enode(x)->get_owner(), get_manager()) << " != " << + mk_ismt2_pp(get_enode(y)->get_owner(), get_manager()) << std::endl;); + } + + void theory_str::relevant_eh(app * n) { + TRACE("str", tout << "relevant: " << mk_ismt2_pp(n, get_manager()) << std::endl;); + } + + void theory_str::assign_eh(bool_var v, bool is_true) { + context & ctx = get_context(); + TRACE("str", tout << "assert: v" << v << " #" << ctx.bool_var2expr(v)->get_id() << " is_true: " << is_true << std::endl;); + } + + void theory_str::push_scope_eh() { + theory::push_scope_eh(); + m_trail_stack.push_scope(); + + sLevel += 1; + TRACE("str", tout << "push to " << sLevel << std::endl;); + TRACE_CODE(if (is_trace_enabled("t_str_dump_assign_on_scope_change")) { dump_assignments(); }); + } + + void theory_str::recursive_check_variable_scope(expr * ex) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + if (is_app(ex)) { + app * a = to_app(ex); + if (a->get_num_args() == 0) { + // we only care about string variables + sort * s = m.get_sort(ex); + sort * string_sort = u.str.mk_string_sort(); + if (s != string_sort) { + return; + } + // base case: string constant / var + if (u.str.is_string(a)) { + return; + } else { + // assume var + if (variable_set.find(ex) == variable_set.end() + && internal_variable_set.find(ex) == internal_variable_set.end()) { + TRACE("str", tout << "WARNING: possible reference to out-of-scope variable " << mk_pp(ex, m) << std::endl;); + } + } + } else { + for (unsigned i = 0; i < a->get_num_args(); ++i) { + recursive_check_variable_scope(a->get_arg(i)); + } + } + } + } + + void theory_str::check_variable_scope() { + if (!opt_CheckVariableScope) { + return; + } + + if (!is_trace_enabled("t_str_detail")) { + return; + } + + TRACE("str", tout << "checking scopes of variables in the current assignment" << std::endl;); + + context & ctx = get_context(); + ast_manager & m = get_manager(); + + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { + expr * ex = *i; + recursive_check_variable_scope(ex); + } + } + + void theory_str::pop_scope_eh(unsigned num_scopes) { + sLevel -= num_scopes; + TRACE("str", tout << "pop " << num_scopes << " to " << sLevel << std::endl;); + context & ctx = get_context(); + ast_manager & m = get_manager(); + + TRACE_CODE(if (is_trace_enabled("t_str_dump_assign_on_scope_change")) { dump_assignments(); }); + + // list of expr* to remove from cut_var_map + ptr_vector cutvarmap_removes; + + obj_map >::iterator varItor = cut_var_map.begin(); + while (varItor != cut_var_map.end()) { + expr * e = varItor->m_key; + std::stack & val = cut_var_map[varItor->m_key]; + while ((val.size() > 0) && (val.top()->level != 0) && (val.top()->level >= sLevel)) { + TRACE("str", tout << "remove cut info for " << mk_pp(e, m) << std::endl; print_cut_var(e, tout);); + T_cut * aCut = val.top(); + val.pop(); + // dealloc(aCut); + } + if (val.size() == 0) { + cutvarmap_removes.insert(varItor->m_key); + } + varItor++; + } + + if (!cutvarmap_removes.empty()) { + ptr_vector::iterator it = cutvarmap_removes.begin(); + for (; it != cutvarmap_removes.end(); ++it) { + expr * ex = *it; + cut_var_map.remove(ex); + } + } + + ptr_vector new_m_basicstr; + for (ptr_vector::iterator it = m_basicstr_axiom_todo.begin(); it != m_basicstr_axiom_todo.end(); ++it) { + enode * e = *it; + app * a = e->get_owner(); + TRACE("str", tout << "consider deleting " << mk_pp(a, get_manager()) + << ", enode scope level is " << e->get_iscope_lvl() + << std::endl;); + if (e->get_iscope_lvl() <= (unsigned)sLevel) { + new_m_basicstr.push_back(e); + } + } + m_basicstr_axiom_todo.reset(); + m_basicstr_axiom_todo = new_m_basicstr; + + m_trail_stack.pop_scope(num_scopes); + theory::pop_scope_eh(num_scopes); + + //check_variable_scope(); + } + + void theory_str::dump_assignments() { + TRACE_CODE( + ast_manager & m = get_manager(); + context & ctx = get_context(); + tout << "dumping all assignments:" << std::endl; + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + for (expr_ref_vector::iterator i = assignments.begin(); i != assignments.end(); ++i) { + expr * ex = *i; + tout << mk_ismt2_pp(ex, m) << (ctx.is_relevant(ex) ? "" : " (NOT REL)") << std::endl; + } + ); + } + + void theory_str::classify_ast_by_type(expr * node, std::map & varMap, + std::map & concatMap, std::map & unrollMap) { + + // check whether the node is a string variable; + // testing set membership here bypasses several expensive checks. + // note that internal variables don't count if they're only length tester / value tester vars. + if (variable_set.find(node) != variable_set.end() + && internal_lenTest_vars.find(node) == internal_lenTest_vars.end() + && internal_valTest_vars.find(node) == internal_valTest_vars.end() + && internal_unrollTest_vars.find(node) == internal_unrollTest_vars.end()) { + if (varMap[node] != 1) { + TRACE("str", tout << "new variable: " << mk_pp(node, get_manager()) << std::endl;); + } + varMap[node] = 1; + } + // check whether the node is a function that we want to inspect + else if (is_app(node)) { + app * aNode = to_app(node); + if (u.str.is_length(aNode)) { + // Length + return; + } else if (u.str.is_concat(aNode)) { + expr * arg0 = aNode->get_arg(0); + expr * arg1 = aNode->get_arg(1); + bool arg0HasEq = false; + bool arg1HasEq = false; + expr * arg0Val = get_eqc_value(arg0, arg0HasEq); + expr * arg1Val = get_eqc_value(arg1, arg1HasEq); + + int canskip = 0; + zstring tmp; + u.str.is_string(arg0Val, tmp); + if (arg0HasEq && tmp.empty()) { + canskip = 1; + } + u.str.is_string(arg1Val, tmp); + if (canskip == 0 && arg1HasEq && tmp.empty()) { + canskip = 1; + } + if (canskip == 0 && concatMap.find(node) == concatMap.end()) { + concatMap[node] = 1; + } + } else if (u.re.is_unroll(aNode)) { + // Unroll + if (unrollMap.find(node) == unrollMap.end()) { + unrollMap[node] = 1; + } + } + // recursively visit all arguments + for (unsigned i = 0; i < aNode->get_num_args(); ++i) { + expr * arg = aNode->get_arg(i); + classify_ast_by_type(arg, varMap, concatMap, unrollMap); + } + } + } + + // NOTE: this function used to take an argument `Z3_ast node`; + // it was not used and so was removed from the signature + void theory_str::classify_ast_by_type_in_positive_context(std::map & varMap, + std::map & concatMap, std::map & unrollMap) { + + context & ctx = get_context(); + ast_manager & m = get_manager(); + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + + for (expr_ref_vector::iterator it = assignments.begin(); it != assignments.end(); ++it) { + expr * argAst = *it; + // the original code jumped through some hoops to check whether the AST node + // is a function, then checked whether that function is "interesting". + // however, the only thing that's considered "interesting" is an equality predicate. + // so we bypass a huge amount of work by doing the following... + + if (m.is_eq(argAst)) { + TRACE("str", tout + << "eq ast " << mk_pp(argAst, m) << " is between args of sort " + << m.get_sort(to_app(argAst)->get_arg(0))->get_name() + << std::endl;); + classify_ast_by_type(argAst, varMap, concatMap, unrollMap); + } + } + } + + inline expr * theory_str::get_alias_index_ast(std::map & aliasIndexMap, expr * node) { + if (aliasIndexMap.find(node) != aliasIndexMap.end()) + return aliasIndexMap[node]; + else + return node; + } + + inline expr * theory_str::getMostLeftNodeInConcat(expr * node) { + app * aNode = to_app(node); + if (!u.str.is_concat(aNode)) { + return node; + } else { + expr * concatArgL = aNode->get_arg(0); + return getMostLeftNodeInConcat(concatArgL); + } + } + + inline expr * theory_str::getMostRightNodeInConcat(expr * node) { + app * aNode = to_app(node); + if (!u.str.is_concat(aNode)) { + return node; + } else { + expr * concatArgR = aNode->get_arg(1); + return getMostRightNodeInConcat(concatArgR); + } + } + + void theory_str::trace_ctx_dep(std::ofstream & tout, + std::map & aliasIndexMap, + std::map & var_eq_constStr_map, + std::map > & var_eq_concat_map, + std::map > & var_eq_unroll_map, + std::map & concat_eq_constStr_map, + std::map > & concat_eq_concat_map, + std::map > & unrollGroupMap) { +#ifdef _TRACE + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + { + tout << "(0) alias: variables" << std::endl; + std::map > aliasSumMap; + std::map::iterator itor0 = aliasIndexMap.begin(); + for (; itor0 != aliasIndexMap.end(); itor0++) { + aliasSumMap[itor0->second][itor0->first] = 1; + } + std::map >::iterator keyItor = aliasSumMap.begin(); + for (; keyItor != aliasSumMap.end(); keyItor++) { + tout << " * "; + tout << mk_pp(keyItor->first, mgr); + tout << " : "; + std::map::iterator innerItor = keyItor->second.begin(); + for (; innerItor != keyItor->second.end(); innerItor++) { + tout << mk_pp(innerItor->first, mgr); + tout << ", "; + } + tout << std::endl; + } + tout << std::endl; + } + + { + tout << "(1) var = constStr:" << std::endl; + std::map::iterator itor1 = var_eq_constStr_map.begin(); + for (; itor1 != var_eq_constStr_map.end(); itor1++) { + tout << " * "; + tout << mk_pp(itor1->first, mgr); + tout << " = "; + tout << mk_pp(itor1->second, mgr); + if (!in_same_eqc(itor1->first, itor1->second)) { + tout << " (not true in ctx)"; + } + tout << std::endl; + } + tout << std::endl; + } + + { + tout << "(2) var = concat:" << std::endl; + std::map >::iterator itor2 = var_eq_concat_map.begin(); + for (; itor2 != var_eq_concat_map.end(); itor2++) { + tout << " * "; + tout << mk_pp(itor2->first, mgr); + tout << " = { "; + std::map::iterator i_itor = itor2->second.begin(); + for (; i_itor != itor2->second.end(); i_itor++) { + tout << mk_pp(i_itor->first, mgr); + tout << ", "; + } + tout << std::endl; + } + tout << std::endl; + } + + { + tout << "(3) var = unrollFunc:" << std::endl; + std::map >::iterator itor2 = var_eq_unroll_map.begin(); + for (; itor2 != var_eq_unroll_map.end(); itor2++) { + tout << " * " << mk_pp(itor2->first, mgr) << " = { "; + std::map::iterator i_itor = itor2->second.begin(); + for (; i_itor != itor2->second.end(); i_itor++) { + tout << mk_pp(i_itor->first, mgr) << ", "; + } + tout << " }" << std::endl; + } + tout << std::endl; + } + + { + tout << "(4) concat = constStr:" << std::endl; + std::map::iterator itor3 = concat_eq_constStr_map.begin(); + for (; itor3 != concat_eq_constStr_map.end(); itor3++) { + tout << " * "; + tout << mk_pp(itor3->first, mgr); + tout << " = "; + tout << mk_pp(itor3->second, mgr); + tout << std::endl; + + } + tout << std::endl; + } + + { + tout << "(5) eq concats:" << std::endl; + std::map >::iterator itor4 = concat_eq_concat_map.begin(); + for (; itor4 != concat_eq_concat_map.end(); itor4++) { + if (itor4->second.size() > 1) { + std::map::iterator i_itor = itor4->second.begin(); + tout << " * "; + for (; i_itor != itor4->second.end(); i_itor++) { + tout << mk_pp(i_itor->first, mgr); + tout << " , "; + } + tout << std::endl; + } + } + tout << std::endl; + } + + { + tout << "(6) eq unrolls:" << std::endl; + std::map >::iterator itor5 = unrollGroupMap.begin(); + for (; itor5 != unrollGroupMap.end(); itor5++) { + tout << " * "; + std::set::iterator i_itor = itor5->second.begin(); + for (; i_itor != itor5->second.end(); i_itor++) { + tout << mk_pp(*i_itor, mgr) << ", "; + } + tout << std::endl; + } + tout << std::endl; + } + + { + tout << "(7) unroll = concats:" << std::endl; + std::map >::iterator itor5 = unrollGroupMap.begin(); + for (; itor5 != unrollGroupMap.end(); itor5++) { + tout << " * "; + expr * unroll = itor5->first; + tout << mk_pp(unroll, mgr) << std::endl; + enode * e_curr = ctx.get_enode(unroll); + enode * e_curr_end = e_curr; + do { + app * curr = e_curr->get_owner(); + if (u.str.is_concat(curr)) { + tout << " >>> " << mk_pp(curr, mgr) << std::endl; + } + e_curr = e_curr->get_next(); + } while (e_curr != e_curr_end); + tout << std::endl; + } + tout << std::endl; + } +#else + return; +#endif // _TRACE + } + + + /* + * Dependence analysis from current context assignment + * - "freeVarMap" contains a set of variables that doesn't constrained by Concats. + * But it's possible that it's bounded by unrolls + * For the case of + * (1) var1 = unroll(r1, t1) + * var1 is in the freeVarMap + * > should unroll r1 for var1 + * (2) var1 = unroll(r1, t1) /\ var1 = Concat(var2, var3) + * var2, var3 are all in freeVar + * > should split the unroll function so that var2 and var3 are bounded by new unrolls + */ + int theory_str::ctx_dep_analysis(std::map & strVarMap, std::map & freeVarMap, + std::map > & unrollGroupMap, std::map > & var_eq_concat_map) { + std::map concatMap; + std::map unrollMap; + std::map aliasIndexMap; + std::map var_eq_constStr_map; + std::map concat_eq_constStr_map; + std::map > var_eq_unroll_map; + std::map > concat_eq_concat_map; + std::map > depMap; + + context & ctx = get_context(); + ast_manager & m = get_manager(); + + // note that the old API concatenated these assignments into + // a massive conjunction; we may have the opportunity to avoid that here + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + + // Step 1: get variables / concat AST appearing in the context + // the thing we iterate over should just be variable_set - internal_variable_set + // so we avoid computing the set difference (but this might be slower) + for(obj_hashtable::iterator it = variable_set.begin(); it != variable_set.end(); ++it) { + expr* var = *it; + if (internal_variable_set.find(var) == internal_variable_set.end()) { + TRACE("str", tout << "new variable: " << mk_pp(var, m) << std::endl;); + strVarMap[*it] = 1; + } + } + classify_ast_by_type_in_positive_context(strVarMap, concatMap, unrollMap); + + std::map aliasUnrollSet; + std::map::iterator unrollItor = unrollMap.begin(); + for (; unrollItor != unrollMap.end(); ++unrollItor) { + if (aliasUnrollSet.find(unrollItor->first) != aliasUnrollSet.end()) { + continue; + } + expr * aRoot = NULL; + enode * e_currEqc = ctx.get_enode(unrollItor->first); + enode * e_curr = e_currEqc; + do { + app * curr = e_currEqc->get_owner(); + if (u.re.is_unroll(curr)) { + if (aRoot == NULL) { + aRoot = curr; + } + aliasUnrollSet[curr] = aRoot; + } + e_currEqc = e_currEqc->get_next(); + } while (e_currEqc != e_curr); + } + + for (unrollItor = unrollMap.begin(); unrollItor != unrollMap.end(); unrollItor++) { + expr * unrFunc = unrollItor->first; + expr * urKey = aliasUnrollSet[unrFunc]; + unrollGroupMap[urKey].insert(unrFunc); + } + + // Step 2: collect alias relation + // e.g. suppose we have the equivalence class {x, y, z}; + // then we set aliasIndexMap[y] = x + // and aliasIndexMap[z] = x + + std::map::iterator varItor = strVarMap.begin(); + for (; varItor != strVarMap.end(); ++varItor) { + if (aliasIndexMap.find(varItor->first) != aliasIndexMap.end()) { + continue; + } + expr * aRoot = NULL; + expr * curr = varItor->first; + do { + if (variable_set.find(curr) != variable_set.end()) { + if (aRoot == NULL) { + aRoot = curr; + } else { + aliasIndexMap[curr] = aRoot; + } + } + curr = get_eqc_next(curr); + } while (curr != varItor->first); + } + + // Step 3: Collect interested cases + + varItor = strVarMap.begin(); + for (; varItor != strVarMap.end(); ++varItor) { + expr * deAliasNode = get_alias_index_ast(aliasIndexMap, varItor->first); + // Case 1: variable = string constant + // e.g. z = "str1" ::= var_eq_constStr_map[z] = "str1" + + if (var_eq_constStr_map.find(deAliasNode) == var_eq_constStr_map.end()) { + bool nodeHasEqcValue = false; + expr * nodeValue = get_eqc_value(deAliasNode, nodeHasEqcValue); + if (nodeHasEqcValue) { + var_eq_constStr_map[deAliasNode] = nodeValue; + } + } + + // Case 2: var_eq_concat + // e.g. z = concat("str1", b) ::= var_eq_concat[z][concat(c, "str2")] = 1 + // var_eq_unroll + // e.g. z = unroll(...) ::= var_eq_unroll[z][unroll(...)] = 1 + + if (var_eq_concat_map.find(deAliasNode) == var_eq_concat_map.end()) { + expr * curr = get_eqc_next(deAliasNode); + while (curr != deAliasNode) { + app * aCurr = to_app(curr); + // collect concat + if (u.str.is_concat(aCurr)) { + expr * arg0 = aCurr->get_arg(0); + expr * arg1 = aCurr->get_arg(1); + bool arg0HasEqcValue = false; + bool arg1HasEqcValue = false; + expr * arg0_value = get_eqc_value(arg0, arg0HasEqcValue); + expr * arg1_value = get_eqc_value(arg1, arg1HasEqcValue); + + bool is_arg0_emptyStr = false; + if (arg0HasEqcValue) { + zstring strval; + u.str.is_string(arg0_value, strval); + if (strval.empty()) { + is_arg0_emptyStr = true; + } + } + + bool is_arg1_emptyStr = false; + if (arg1HasEqcValue) { + zstring strval; + u.str.is_string(arg1_value, strval); + if (strval.empty()) { + is_arg1_emptyStr = true; + } + } + + if (!is_arg0_emptyStr && !is_arg1_emptyStr) { + var_eq_concat_map[deAliasNode][curr] = 1; + } + } else if (u.re.is_unroll(to_app(curr))) { + var_eq_unroll_map[deAliasNode][curr] = 1; + } + + curr = get_eqc_next(curr); + } + } + + } // for(varItor in strVarMap) + + // -------------------------------------------------- + // * collect aliasing relation among eq concats + // e.g EQC={concat1, concat2, concat3} + // concats_eq_Index_map[concat2] = concat1 + // concats_eq_Index_map[concat3] = concat1 + // -------------------------------------------------- + + std::map concats_eq_index_map; + std::map::iterator concatItor = concatMap.begin(); + for(; concatItor != concatMap.end(); ++concatItor) { + if (concats_eq_index_map.find(concatItor->first) != concats_eq_index_map.end()) { + continue; + } + expr * aRoot = NULL; + expr * curr = concatItor->first; + do { + if (u.str.is_concat(to_app(curr))) { + if (aRoot == NULL) { + aRoot = curr; + } else { + concats_eq_index_map[curr] = aRoot; + } + } + curr = get_eqc_next(curr); + } while (curr != concatItor->first); + } + + concatItor = concatMap.begin(); + for(; concatItor != concatMap.end(); ++concatItor) { + expr * deAliasConcat = NULL; + if (concats_eq_index_map.find(concatItor->first) != concats_eq_index_map.end()) { + deAliasConcat = concats_eq_index_map[concatItor->first]; + } else { + deAliasConcat = concatItor->first; + } + + // (3) concat_eq_conststr, e.g. concat(a,b) = "str1" + if (concat_eq_constStr_map.find(deAliasConcat) == concat_eq_constStr_map.end()) { + bool nodeHasEqcValue = false; + expr * nodeValue = get_eqc_value(deAliasConcat, nodeHasEqcValue); + if (nodeHasEqcValue) { + concat_eq_constStr_map[deAliasConcat] = nodeValue; + } + } + + // (4) concat_eq_concat, e.g. + // concat(a,b) = concat("str1", c) AND z = concat(a,b) AND z = concat(e,f) + if (concat_eq_concat_map.find(deAliasConcat) == concat_eq_concat_map.end()) { + expr * curr = deAliasConcat; + do { + if (u.str.is_concat(to_app(curr))) { + // curr cannot be reduced + if (concatMap.find(curr) != concatMap.end()) { + concat_eq_concat_map[deAliasConcat][curr] = 1; + } + } + curr = get_eqc_next(curr); + } while (curr != deAliasConcat); + } + } + + // print some debugging info + TRACE("str", trace_ctx_dep(tout, aliasIndexMap, var_eq_constStr_map, + var_eq_concat_map, var_eq_unroll_map, + concat_eq_constStr_map, concat_eq_concat_map, unrollGroupMap);); + + if (!contain_pair_bool_map.empty()) { + compute_contains(aliasIndexMap, concats_eq_index_map, var_eq_constStr_map, concat_eq_constStr_map, var_eq_concat_map); + } + + // step 4: dependence analysis + + // (1) var = string constant + for (std::map::iterator itor = var_eq_constStr_map.begin(); + itor != var_eq_constStr_map.end(); ++itor) { + expr * var = get_alias_index_ast(aliasIndexMap, itor->first); + expr * strAst = itor->second; + depMap[var][strAst] = 1; + } + + // (2) var = concat + for (std::map >::iterator itor = var_eq_concat_map.begin(); + itor != var_eq_concat_map.end(); ++itor) { + expr * var = get_alias_index_ast(aliasIndexMap, itor->first); + for (std::map::iterator itor1 = itor->second.begin(); itor1 != itor->second.end(); ++itor1) { + expr * concat = itor1->first; + std::map inVarMap; + std::map inConcatMap; + std::map inUnrollMap; + classify_ast_by_type(concat, inVarMap, inConcatMap, inUnrollMap); + for (std::map::iterator itor2 = inVarMap.begin(); itor2 != inVarMap.end(); ++itor2) { + expr * varInConcat = get_alias_index_ast(aliasIndexMap, itor2->first); + if (!(depMap[var].find(varInConcat) != depMap[var].end() && depMap[var][varInConcat] == 1)) { + depMap[var][varInConcat] = 2; + } + } + } + } + + for (std::map >::iterator itor = var_eq_unroll_map.begin(); + itor != var_eq_unroll_map.end(); itor++) { + expr * var = get_alias_index_ast(aliasIndexMap, itor->first); + for (std::map::iterator itor1 = itor->second.begin(); itor1 != itor->second.end(); itor1++) { + expr * unrollFunc = itor1->first; + std::map inVarMap; + std::map inConcatMap; + std::map inUnrollMap; + classify_ast_by_type(unrollFunc, inVarMap, inConcatMap, inUnrollMap); + for (std::map::iterator itor2 = inVarMap.begin(); itor2 != inVarMap.end(); itor2++) { + expr * varInFunc = get_alias_index_ast(aliasIndexMap, itor2->first); + + TRACE("str", tout << "var in unroll = " << + mk_ismt2_pp(itor2->first, m) << std::endl + << "dealiased var = " << mk_ismt2_pp(varInFunc, m) << std::endl;); + + // it's possible that we have both (Unroll $$_regVar_0 $$_unr_0) /\ (Unroll abcd $$_unr_0), + // while $$_regVar_0 = "abcd" + // have to exclude such cases + bool varHasValue = false; + get_eqc_value(varInFunc, varHasValue); + if (varHasValue) + continue; + + if (depMap[var].find(varInFunc) == depMap[var].end()) { + depMap[var][varInFunc] = 6; + } + } + } + } + + // (3) concat = string constant + for (std::map::iterator itor = concat_eq_constStr_map.begin(); + itor != concat_eq_constStr_map.end(); itor++) { + expr * concatAst = itor->first; + expr * constStr = itor->second; + std::map inVarMap; + std::map inConcatMap; + std::map inUnrollMap; + classify_ast_by_type(concatAst, inVarMap, inConcatMap, inUnrollMap); + for (std::map::iterator itor2 = inVarMap.begin(); itor2 != inVarMap.end(); itor2++) { + expr * varInConcat = get_alias_index_ast(aliasIndexMap, itor2->first); + if (!(depMap[varInConcat].find(constStr) != depMap[varInConcat].end() && depMap[varInConcat][constStr] == 1)) + depMap[varInConcat][constStr] = 3; + } + } + + // (4) equivalent concats + // - possibility 1 : concat("str", v1) = concat(concat(v2, v3), v4) = concat(v5, v6) + // ==> v2, v5 are constrained by "str" + // - possibility 2 : concat(v1, "str") = concat(v2, v3) = concat(v4, v5) + // ==> v2, v4 are constrained by "str" + //-------------------------------------------------------------- + + std::map mostLeftNodes; + std::map mostRightNodes; + + std::map mLIdxMap; + std::map > mLMap; + std::map mRIdxMap; + std::map > mRMap; + std::set nSet; + + for (std::map >::iterator itor = concat_eq_concat_map.begin(); + itor != concat_eq_concat_map.end(); itor++) { + mostLeftNodes.clear(); + mostRightNodes.clear(); + + expr * mLConst = NULL; + expr * mRConst = NULL; + + for (std::map::iterator itor1 = itor->second.begin(); itor1 != itor->second.end(); itor1++) { + expr * concatNode = itor1->first; + expr * mLNode = getMostLeftNodeInConcat(concatNode); + zstring strval; + if (u.str.is_string(to_app(mLNode), strval)) { + if (mLConst == NULL && strval.empty()) { + mLConst = mLNode; + } + } else { + mostLeftNodes[mLNode] = concatNode; + } + + expr * mRNode = getMostRightNodeInConcat(concatNode); + if (u.str.is_string(to_app(mRNode), strval)) { + if (mRConst == NULL && strval.empty()) { + mRConst = mRNode; + } + } else { + mostRightNodes[mRNode] = concatNode; + } + } + + if (mLConst != NULL) { + // ------------------------------------------------------------------------------------- + // The left most variable in a concat is constrained by a constant string in eqc concat + // ------------------------------------------------------------------------------------- + // e.g. Concat(x, ...) = Concat("abc", ...) + // ------------------------------------------------------------------------------------- + for (std::map::iterator itor1 = mostLeftNodes.begin(); + itor1 != mostLeftNodes.end(); itor1++) { + expr * deVar = get_alias_index_ast(aliasIndexMap, itor1->first); + if (depMap[deVar].find(mLConst) == depMap[deVar].end() || depMap[deVar][mLConst] != 1) { + depMap[deVar][mLConst] = 4; + } + } + } + + { + // ------------------------------------------------------------------------------------- + // The left most variables in eqc concats are constrained by each other + // ------------------------------------------------------------------------------------- + // e.g. concat(x, ...) = concat(u, ...) = ... + // x and u are constrained by each other + // ------------------------------------------------------------------------------------- + nSet.clear(); + std::map::iterator itl = mostLeftNodes.begin(); + for (; itl != mostLeftNodes.end(); itl++) { + bool lfHasEqcValue = false; + get_eqc_value(itl->first, lfHasEqcValue); + if (lfHasEqcValue) + continue; + expr * deVar = get_alias_index_ast(aliasIndexMap, itl->first); + nSet.insert(deVar); + } + + if (nSet.size() > 1) { + int lId = -1; + for (std::set::iterator itor2 = nSet.begin(); itor2 != nSet.end(); itor2++) { + if (mLIdxMap.find(*itor2) != mLIdxMap.end()) { + lId = mLIdxMap[*itor2]; break; } } - } // !indicatorHasEqcValue - } // for (i : [0..lenTesterCount-1]) - if (effectiveLenIndiStr == "more" || effectiveLenIndiStr == "") { - TRACE("str", tout << "length is not fixed; generating length tester options for free var" << std::endl;); - expr_ref indicator(m); - unsigned int testNum = 0; - - TRACE("str", tout << "effectiveLenIndiStr = " << effectiveLenIndiStr - << ", i = " << i << ", lenTesterCount = " << lenTesterCount << "\n";); - - if (i == lenTesterCount) { - fvar_len_count_map[freeVar] = fvar_len_count_map[freeVar] + 1; - testNum = fvar_len_count_map[freeVar]; - indicator = mk_internal_lenTest_var(freeVar, testNum); - fvar_lenTester_map[freeVar].push_back(indicator); - lenTester_fvar_map.insert(indicator, freeVar); - } else { - indicator = fvar_lenTester_map[freeVar][i]; - refresh_theory_var(indicator); - testNum = i + 1; + if (lId == -1) + lId = mLMap.size(); + for (std::set::iterator itor2 = nSet.begin(); itor2 != nSet.end(); itor2++) { + bool itorHasEqcValue = false; + get_eqc_value(*itor2, itorHasEqcValue); + if (itorHasEqcValue) + continue; + mLIdxMap[*itor2] = lId; + mLMap[lId].insert(*itor2); + } } + } + + if (mRConst != NULL) { + for (std::map::iterator itor1 = mostRightNodes.begin(); + itor1 != mostRightNodes.end(); itor1++) { + expr * deVar = get_alias_index_ast(aliasIndexMap, itor1->first); + if (depMap[deVar].find(mRConst) == depMap[deVar].end() || depMap[deVar][mRConst] != 1) { + depMap[deVar][mRConst] = 5; + } + } + } + + { + nSet.clear(); + std::map::iterator itr = mostRightNodes.begin(); + for (; itr != mostRightNodes.end(); itr++) { + expr * deVar = get_alias_index_ast(aliasIndexMap, itr->first); + nSet.insert(deVar); + } + if (nSet.size() > 1) { + int rId = -1; + std::set::iterator itor2 = nSet.begin(); + for (; itor2 != nSet.end(); itor2++) { + if (mRIdxMap.find(*itor2) != mRIdxMap.end()) { + rId = mRIdxMap[*itor2]; + break; + } + } + if (rId == -1) + rId = mRMap.size(); + for (itor2 = nSet.begin(); itor2 != nSet.end(); itor2++) { + bool rHasEqcValue = false; + get_eqc_value(*itor2, rHasEqcValue); + if (rHasEqcValue) + continue; + mRIdxMap[*itor2] = rId; + mRMap[rId].insert(*itor2); + } + } + } + } + + // print the dependence map + TRACE("str", + tout << "Dependence Map" << std::endl; + for(std::map >::iterator itor = depMap.begin(); itor != depMap.end(); itor++) { + tout << mk_pp(itor->first, m); + rational nnLen; + bool nnLen_exists = get_len_value(itor->first, nnLen); + tout << " [len = " << (nnLen_exists ? nnLen.to_string() : "?") << "] \t-->\t"; + for (std::map::iterator itor1 = itor->second.begin(); itor1 != itor->second.end(); itor1++) { + tout << mk_pp(itor1->first, m) << "(" << itor1->second << "), "; + } + tout << std::endl; + } + ); + + // step, errr, 5: compute free variables based on the dependence map + + // the case dependence map is empty, every var in VarMap is free + //--------------------------------------------------------------- + // remove L/R most var in eq concat since they are constrained with each other + std::map > lrConstrainedMap; + for (std::map >::iterator itor = mLMap.begin(); itor != mLMap.end(); itor++) { + for (std::set::iterator it1 = itor->second.begin(); it1 != itor->second.end(); it1++) { + std::set::iterator it2 = it1; + it2++; + for (; it2 != itor->second.end(); it2++) { + expr * n1 = *it1; + expr * n2 = *it2; + lrConstrainedMap[n1][n2] = 1; + lrConstrainedMap[n2][n1] = 1; + } + } + } + for (std::map >::iterator itor = mRMap.begin(); itor != mRMap.end(); itor++) { + for (std::set::iterator it1 = itor->second.begin(); it1 != itor->second.end(); it1++) { + std::set::iterator it2 = it1; + it2++; + for (; it2 != itor->second.end(); it2++) { + expr * n1 = *it1; + expr * n2 = *it2; + lrConstrainedMap[n1][n2] = 1; + lrConstrainedMap[n2][n1] = 1; + } + } + } + + if (depMap.size() == 0) { + std::map::iterator itor = strVarMap.begin(); + for (; itor != strVarMap.end(); itor++) { + expr * var = get_alias_index_ast(aliasIndexMap, itor->first); + if (lrConstrainedMap.find(var) == lrConstrainedMap.end()) { + freeVarMap[var] = 1; + } else { + int lrConstainted = 0; + std::map::iterator lrit = freeVarMap.begin(); + for (; lrit != freeVarMap.end(); lrit++) { + if (lrConstrainedMap[var].find(lrit->first) != lrConstrainedMap[var].end()) { + lrConstainted = 1; + break; + } + } + if (lrConstainted == 0) { + freeVarMap[var] = 1; + } + } + } + } else { + // if the keys in aliasIndexMap are not contained in keys in depMap, they are free + // e.g., x= y /\ x = z /\ t = "abc" + // aliasIndexMap[y]= x, aliasIndexMap[z] = x + // depMap t ~ "abc"(1) + // x should be free + std::map::iterator itor2 = strVarMap.begin(); + for (; itor2 != strVarMap.end(); itor2++) { + if (aliasIndexMap.find(itor2->first) != aliasIndexMap.end()) { + expr * var = aliasIndexMap[itor2->first]; + if (depMap.find(var) == depMap.end()) { + if (lrConstrainedMap.find(var) == lrConstrainedMap.end()) { + freeVarMap[var] = 1; + } else { + int lrConstainted = 0; + std::map::iterator lrit = freeVarMap.begin(); + for (; lrit != freeVarMap.end(); lrit++) { + if (lrConstrainedMap[var].find(lrit->first) != lrConstrainedMap[var].end()) { + lrConstainted = 1; + break; + } + } + if (lrConstainted == 0) { + freeVarMap[var] = 1; + } + } + } + } else if (aliasIndexMap.find(itor2->first) == aliasIndexMap.end()) { + // if a variable is not in aliasIndexMap and not in depMap, it's free + if (depMap.find(itor2->first) == depMap.end()) { + expr * var = itor2->first; + if (lrConstrainedMap.find(var) == lrConstrainedMap.end()) { + freeVarMap[var] = 1; + } else { + int lrConstainted = 0; + std::map::iterator lrit = freeVarMap.begin(); + for (; lrit != freeVarMap.end(); lrit++) { + if (lrConstrainedMap[var].find(lrit->first) != lrConstrainedMap[var].end()) { + lrConstainted = 1; + break; + } + } + if (lrConstainted == 0) { + freeVarMap[var] = 1; + } + } + } + } + } + + std::map >::iterator itor = depMap.begin(); + for (; itor != depMap.end(); itor++) { + for (std::map::iterator itor1 = itor->second.begin(); itor1 != itor->second.end(); itor1++) { + if (variable_set.find(itor1->first) != variable_set.end()) { // expr type = var + expr * var = get_alias_index_ast(aliasIndexMap, itor1->first); + // if a var is dep on itself and all dependence are type 2, it's a free variable + // e.g {y --> x(2), y(2), m --> m(2), n(2)} y,m are free + { + if (depMap.find(var) == depMap.end()) { + if (freeVarMap.find(var) == freeVarMap.end()) { + if (lrConstrainedMap.find(var) == lrConstrainedMap.end()) { + freeVarMap[var] = 1; + } else { + int lrConstainted = 0; + std::map::iterator lrit = freeVarMap.begin(); + for (; lrit != freeVarMap.end(); lrit++) { + if (lrConstrainedMap[var].find(lrit->first) != lrConstrainedMap[var].end()) { + lrConstainted = 1; + break; + } + } + if (lrConstainted == 0) { + freeVarMap[var] = 1; + } + } + + } else { + freeVarMap[var] = freeVarMap[var] + 1; + } + } + } + } + } + } + } + + return 0; + } + + // Check agreement between integer and string theories for the term a = (str.to-int S). + // Returns true if axioms were added, and false otherwise. + bool theory_str::finalcheck_str2int(app * a) { + bool axiomAdd = false; + context & ctx = get_context(); + ast_manager & m = get_manager(); + + expr * S = a->get_arg(0); + + // check integer theory + rational Ival; + bool Ival_exists = get_value(a, Ival); + if (Ival_exists) { + TRACE("str", tout << "integer theory assigns " << mk_pp(a, m) << " = " << Ival.to_string() << std::endl;); + // if that value is not -1, we can assert (str.to-int S) = Ival --> S = "Ival" + if (!Ival.is_minus_one()) { + zstring Ival_str(Ival.to_string().c_str()); + expr_ref premise(ctx.mk_eq_atom(a, m_autil.mk_numeral(Ival, true)), m); + expr_ref conclusion(ctx.mk_eq_atom(S, mk_string(Ival_str)), m); + expr_ref axiom(rewrite_implication(premise, conclusion), m); + if (!string_int_axioms.contains(axiom)) { + string_int_axioms.insert(axiom); + assert_axiom(axiom); + m_trail_stack.push(insert_obj_trail(string_int_axioms, axiom)); + axiomAdd = true; + } + } + } else { + TRACE("str", tout << "integer theory has no assignment for " << mk_pp(a, m) << std::endl;); + NOT_IMPLEMENTED_YET(); + } + + return axiomAdd; + } + + bool theory_str::finalcheck_int2str(app * a) { + bool axiomAdd = false; + context & ctx = get_context(); + ast_manager & m = get_manager(); + + expr * N = a->get_arg(0); + + // check string theory + bool Sval_expr_exists; + expr * Sval_expr = get_eqc_value(a, Sval_expr_exists); + if (Sval_expr_exists) { + zstring Sval; + u.str.is_string(Sval_expr, Sval); + TRACE("str", tout << "string theory assigns \"" << mk_pp(a, m) << " = " << Sval << "\n";); + // empty string --> integer value < 0 + if (Sval.empty()) { + // ignore this. we should already assert the axiom for what happens when the string is "" + } else { + // nonempty string --> convert to correct integer value, or disallow it + rational convertedRepresentation(0); + rational ten(10); + bool conversionOK = true; + for (unsigned i = 0; i < Sval.length(); ++i) { + char digit = (int)Sval[i]; + if (isdigit((int)digit)) { + std::string sDigit(1, digit); + int val = atoi(sDigit.c_str()); + convertedRepresentation = (ten * convertedRepresentation) + rational(val); + } else { + // not a digit, invalid + TRACE("str", tout << "str.to-int argument contains non-digit character '" << digit << "'" << std::endl;); + conversionOK = false; + break; + } + } + if (conversionOK) { + expr_ref premise(ctx.mk_eq_atom(a, mk_string(Sval)), m); + expr_ref conclusion(ctx.mk_eq_atom(N, m_autil.mk_numeral(convertedRepresentation, true)), m); + expr_ref axiom(rewrite_implication(premise, conclusion), m); + if (!string_int_axioms.contains(axiom)) { + string_int_axioms.insert(axiom); + assert_axiom(axiom); + m_trail_stack.push(insert_obj_trail(string_int_axioms, axiom)); + axiomAdd = true; + } + } else { + expr_ref axiom(m.mk_not(ctx.mk_eq_atom(a, mk_string(Sval))), m); + // always assert this axiom because this is a conflict clause + assert_axiom(axiom); + axiomAdd = true; + } + } + } else { + TRACE("str", tout << "string theory has no assignment for " << mk_pp(a, m) << std::endl;); + NOT_IMPLEMENTED_YET(); + } + return axiomAdd; + } + + void theory_str::collect_var_concat(expr * node, std::set & varSet, std::set & concatSet) { + if (variable_set.find(node) != variable_set.end()) { + if (internal_lenTest_vars.find(node) == internal_lenTest_vars.end()) { + varSet.insert(node); + } + } + else if (is_app(node)) { + app * aNode = to_app(node); + if (u.str.is_length(aNode)) { + // Length + return; + } + if (u.str.is_concat(aNode)) { + expr * arg0 = aNode->get_arg(0); + expr * arg1 = aNode->get_arg(1); + if (concatSet.find(node) == concatSet.end()) { + concatSet.insert(node); + } + } + // recursively visit all arguments + for (unsigned i = 0; i < aNode->get_num_args(); ++i) { + expr * arg = aNode->get_arg(i); + collect_var_concat(arg, varSet, concatSet); + } + } + } + + bool theory_str::propagate_length_within_eqc(expr * var) { + bool res = false; + ast_manager & m = get_manager(); + context & ctx = get_context(); + + TRACE("str", tout << "propagate_length_within_eqc: " << mk_ismt2_pp(var, m) << std::endl ;); + + enode * n_eq_enode = ctx.get_enode(var); + rational varLen; + if (! get_len_value(var, varLen)) { + bool hasLen = false; + expr * nodeWithLen= var; + do { + if (get_len_value(nodeWithLen, varLen)) { + hasLen = true; + break; + } + nodeWithLen = get_eqc_next(nodeWithLen); + } while (nodeWithLen != var); + + if (hasLen) { + // var = nodeWithLen --> |var| = |nodeWithLen| + expr_ref_vector l_items(m); + expr_ref varEqNode(ctx.mk_eq_atom(var, nodeWithLen), m); + l_items.push_back(varEqNode); + + expr_ref nodeWithLenExpr (mk_strlen(nodeWithLen), m); + expr_ref varLenExpr (mk_int(varLen), m); + expr_ref lenEqNum(ctx.mk_eq_atom(nodeWithLenExpr, varLenExpr), m); + l_items.push_back(lenEqNum); + + expr_ref axl(m.mk_and(l_items.size(), l_items.c_ptr()), m); + expr_ref varLen(mk_strlen(var), m); + expr_ref axr(ctx.mk_eq_atom(varLen, mk_int(varLen)), m); + assert_implication(axl, axr); + TRACE("str", tout << mk_ismt2_pp(axl, m) << std::endl << " ---> " << std::endl << mk_ismt2_pp(axr, m);); + res = true; + } + } + return res; + } + + bool theory_str::propagate_length(std::set & varSet, std::set & concatSet, std::map & exprLenMap) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + bool axiomAdded = false; + // collect all concats in context + for (expr_ref_vector::iterator it = assignments.begin(); it != assignments.end(); ++it) { + if (! ctx.is_relevant(*it)) { + continue; + } + if (m.is_eq(*it)) { + collect_var_concat(*it, varSet, concatSet); + } + } + // iterate each concat + // if a concat doesn't have length info, check if the length of all leaf nodes can be resolved + for (std::set::iterator it = concatSet.begin(); it != concatSet.end(); it++) { + expr * concat = *it; + rational lenValue; + expr_ref concatlenExpr (mk_strlen(concat), m) ; + bool allLeafResolved = true; + if (! get_value(concatlenExpr, lenValue)) { + // the length fo concat is unresolved yet + if (get_len_value(concat, lenValue)) { + // but all leaf nodes have length information + TRACE("str", tout << "* length pop-up: " << mk_ismt2_pp(concat, m) << "| = " << lenValue << std::endl;); + std::set leafNodes; + get_unique_non_concat_nodes(concat, leafNodes); + expr_ref_vector l_items(m); + for (std::set::iterator leafIt = leafNodes.begin(); leafIt != leafNodes.end(); ++leafIt) { + rational leafLenValue; + if (get_len_value(*leafIt, leafLenValue)) { + expr_ref leafItLenExpr (mk_strlen(*leafIt), m); + expr_ref leafLenValueExpr (mk_int(leafLenValue), m); + expr_ref lcExpr (ctx.mk_eq_atom(leafItLenExpr, leafLenValueExpr), m); + l_items.push_back(lcExpr); + } else { + allLeafResolved = false; + break; + } + } + if (allLeafResolved) { + expr_ref axl(m.mk_and(l_items.size(), l_items.c_ptr()), m); + expr_ref lenValueExpr (mk_int(lenValue), m); + expr_ref axr(ctx.mk_eq_atom(concatlenExpr, lenValueExpr), m); + assert_implication(axl, axr); + TRACE("str", tout << mk_ismt2_pp(axl, m) << std::endl << " ---> " << std::endl << mk_ismt2_pp(axr, m)<< std::endl;); + axiomAdded = true; + } + } + } + } + // if no concat length is propagated, check the length of variables. + if (! axiomAdded) { + for (std::set::iterator it = varSet.begin(); it != varSet.end(); it++) { + expr * var = *it; + rational lenValue; + expr_ref varlen (mk_strlen(var), m) ; + bool allLeafResolved = true; + if (! get_value(varlen, lenValue)) { + if (propagate_length_within_eqc(var)) { + axiomAdded = true; + } + } + } + + } + return axiomAdded; + } + + void theory_str::get_unique_non_concat_nodes(expr * node, std::set & argSet) { + app * a_node = to_app(node); + if (!u.str.is_concat(a_node)) { + argSet.insert(node); + return; + } else { + SASSERT(a_node->get_num_args() == 2); + expr * leftArg = a_node->get_arg(0); + expr * rightArg = a_node->get_arg(1); + get_unique_non_concat_nodes(leftArg, argSet); + get_unique_non_concat_nodes(rightArg, argSet); + } + } + + final_check_status theory_str::final_check_eh() { + context & ctx = get_context(); + ast_manager & m = get_manager(); + + expr_ref_vector assignments(m); + ctx.get_assignments(assignments); + + if (opt_VerifyFinalCheckProgress) { + finalCheckProgressIndicator = false; + } + + TRACE("str", tout << "final check" << std::endl;); + TRACE_CODE(if (is_trace_enabled("t_str_dump_assign")) { dump_assignments(); }); + check_variable_scope(); + + if (opt_DeferEQCConsistencyCheck) { + TRACE("str", tout << "performing deferred EQC consistency check" << std::endl;); + std::set eqc_roots; + for (ptr_vector::const_iterator it = ctx.begin_enodes(); it != ctx.end_enodes(); ++it) { + enode * e = *it; + enode * root = e->get_root(); + eqc_roots.insert(root); + } + + bool found_inconsistency = false; + + for (std::set::iterator it = eqc_roots.begin(); it != eqc_roots.end(); ++it) { + enode * e = *it; + app * a = e->get_owner(); + if (!(m.get_sort(a) == u.str.mk_string_sort())) { + TRACE("str", tout << "EQC root " << mk_pp(a, m) << " not a string term; skipping" << std::endl;); + } else { + TRACE("str", tout << "EQC root " << mk_pp(a, m) << " is a string term. Checking this EQC" << std::endl;); + // first call check_concat_len_in_eqc() on each member of the eqc + enode * e_it = e; + enode * e_root = e_it; + do { + bool status = check_concat_len_in_eqc(e_it->get_owner()); + if (!status) { + TRACE("str", tout << "concat-len check asserted an axiom on " << mk_pp(e_it->get_owner(), m) << std::endl;); + found_inconsistency = true; + } + e_it = e_it->get_next(); + } while (e_it != e_root); + + // now grab any two distinct elements from the EQC and call new_eq_check() on them + enode * e1 = e; + enode * e2 = e1->get_next(); + if (e1 != e2) { + TRACE("str", tout << "deferred new_eq_check() over EQC of " << mk_pp(e1->get_owner(), m) << " and " << mk_pp(e2->get_owner(), m) << std::endl;); + bool result = new_eq_check(e1->get_owner(), e2->get_owner()); + if (!result) { + TRACE("str", tout << "new_eq_check found inconsistencies" << std::endl;); + found_inconsistency = true; + } + } + } + } + + if (found_inconsistency) { + TRACE("str", tout << "Found inconsistency in final check! Returning to search." << std::endl;); + return FC_CONTINUE; + } else { + TRACE("str", tout << "Deferred consistency check passed. Continuing in final check." << std::endl;); + } + } + + // run dependence analysis to find free string variables + std::map varAppearInAssign; + std::map freeVar_map; + std::map > unrollGroup_map; + std::map > var_eq_concat_map; + int conflictInDep = ctx_dep_analysis(varAppearInAssign, freeVar_map, unrollGroup_map, var_eq_concat_map); + if (conflictInDep == -1) { + // return Z3_TRUE; + return FC_DONE; + } + + // enhancement: improved backpropagation of string constants into var=concat terms + bool backpropagation_occurred = false; + for (std::map >::iterator veqc_map_it = var_eq_concat_map.begin(); + veqc_map_it != var_eq_concat_map.end(); ++veqc_map_it) { + expr * var = veqc_map_it->first; + for (std::map::iterator concat_map_it = veqc_map_it->second.begin(); + concat_map_it != veqc_map_it->second.end(); ++concat_map_it) { + app * concat = to_app(concat_map_it->first); + expr * concat_lhs = concat->get_arg(0); + expr * concat_rhs = concat->get_arg(1); + // If the concat LHS and RHS both have a string constant in their EQC, + // but the var does not, then we assert an axiom of the form + // (lhs = "lhs" AND rhs = "rhs") --> (Concat lhs rhs) = "lhsrhs" + bool concat_lhs_haseqc, concat_rhs_haseqc, var_haseqc; + expr * concat_lhs_str = get_eqc_value(concat_lhs, concat_lhs_haseqc); + expr * concat_rhs_str = get_eqc_value(concat_rhs, concat_rhs_haseqc); + expr * var_str = get_eqc_value(var, var_haseqc); + if (concat_lhs_haseqc && concat_rhs_haseqc && !var_haseqc) { + TRACE("str", tout << "backpropagate into " << mk_pp(var, m) << " = " << mk_pp(concat, m) << std::endl + << "LHS ~= " << mk_pp(concat_lhs_str, m) << " RHS ~= " << mk_pp(concat_rhs_str, m) << std::endl;); + zstring lhsString, rhsString; + u.str.is_string(concat_lhs_str, lhsString); + u.str.is_string(concat_rhs_str, rhsString); + zstring concatString = lhsString + rhsString; + expr_ref lhs1(ctx.mk_eq_atom(concat_lhs, concat_lhs_str), m); + expr_ref lhs2(ctx.mk_eq_atom(concat_rhs, concat_rhs_str), m); + expr_ref lhs(m.mk_and(lhs1, lhs2), m); + expr_ref rhs(ctx.mk_eq_atom(concat, mk_string(concatString)), m); + assert_implication(lhs, rhs); + backpropagation_occurred = true; + } + } + } + + if (backpropagation_occurred) { + TRACE("str", tout << "Resuming search due to axioms added by backpropagation." << std::endl;); + return FC_CONTINUE; + } + + // enhancement: improved backpropagation of length information + { + std::set varSet; + std::set concatSet; + std::map exprLenMap; + + bool length_propagation_occurred = propagate_length(varSet, concatSet, exprLenMap); + if (length_propagation_occurred) { + TRACE("str", tout << "Resuming search due to axioms added by length propagation." << std::endl;); + return FC_CONTINUE; + } + } + + bool needToAssignFreeVars = false; + std::set free_variables; + std::set unused_internal_variables; + { // Z3str2 free variables check + std::map::iterator itor = varAppearInAssign.begin(); + for (; itor != varAppearInAssign.end(); ++itor) { + /* + std::string vName = std::string(Z3_ast_to_string(ctx, itor->first)); + if (vName.length() >= 3 && vName.substr(0, 3) == "$$_") + continue; + */ + if (internal_variable_set.find(itor->first) != internal_variable_set.end() + || regex_variable_set.find(itor->first) != regex_variable_set.end()) { + // this can be ignored, I think + TRACE("str", tout << "free internal variable " << mk_pp(itor->first, m) << " ignored" << std::endl;); + continue; + } + bool hasEqcValue = false; + expr * eqcString = get_eqc_value(itor->first, hasEqcValue); + if (!hasEqcValue) { + TRACE("str", tout << "found free variable " << mk_pp(itor->first, m) << std::endl;); + needToAssignFreeVars = true; + free_variables.insert(itor->first); + // break; + } else { + // debug + TRACE("str", tout << "variable " << mk_pp(itor->first, m) << " = " << mk_pp(eqcString, m) << std::endl;); + } + } + } + + if (!needToAssignFreeVars) { + + // check string-int terms + bool addedStrIntAxioms = false; + for (unsigned i = 0; i < string_int_conversion_terms.size(); ++i) { + app * ex = to_app(string_int_conversion_terms[i].get()); + if (u.str.is_stoi(ex)) { + bool axiomAdd = finalcheck_str2int(ex); + if (axiomAdd) { + addedStrIntAxioms = true; + } + } else if (u.str.is_itos(ex)) { + bool axiomAdd = finalcheck_int2str(ex); + if (axiomAdd) { + addedStrIntAxioms = true; + } + } else { + UNREACHABLE(); + } + } + if (addedStrIntAxioms) { + TRACE("str", tout << "Resuming search due to addition of string-integer conversion axioms." << std::endl;); + return FC_CONTINUE; + } + + if (unused_internal_variables.empty()) { + TRACE("str", tout << "All variables are assigned. Done!" << std::endl;); + return FC_DONE; + } else { + TRACE("str", tout << "Assigning decoy values to free internal variables." << std::endl;); + for (std::set::iterator it = unused_internal_variables.begin(); it != unused_internal_variables.end(); ++it) { + expr * var = *it; + expr_ref assignment(m.mk_eq(var, mk_string("**unused**")), m); + assert_axiom(assignment); + } + return FC_CONTINUE; + } + } + + CTRACE("str", needToAssignFreeVars, + tout << "Need to assign values to the following free variables:" << std::endl; + for (std::set::iterator itx = free_variables.begin(); itx != free_variables.end(); ++itx) { + tout << mk_ismt2_pp(*itx, m) << std::endl; + } + tout << "freeVar_map has the following entries:" << std::endl; + for (std::map::iterator fvIt = freeVar_map.begin(); fvIt != freeVar_map.end(); fvIt++) { + expr * var = fvIt->first; + tout << mk_ismt2_pp(var, m) << std::endl; + } + ); + + // ----------------------------------------------------------- + // variables in freeVar are those not bounded by Concats + // classify variables in freeVarMap: + // (1) freeVar = unroll(r1, t1) + // (2) vars are not bounded by either concat or unroll + // ----------------------------------------------------------- + std::map > fv_unrolls_map; + std::set tmpSet; + expr * constValue = NULL; + for (std::map::iterator fvIt2 = freeVar_map.begin(); fvIt2 != freeVar_map.end(); fvIt2++) { + expr * var = fvIt2->first; + tmpSet.clear(); + get_eqc_allUnroll(var, constValue, tmpSet); + if (tmpSet.size() > 0) { + fv_unrolls_map[var] = tmpSet; + } + } + // erase var bounded by an unroll function from freeVar_map + for (std::map >::iterator fvIt3 = fv_unrolls_map.begin(); + fvIt3 != fv_unrolls_map.end(); fvIt3++) { + expr * var = fvIt3->first; + TRACE("str", tout << "erase free variable " << mk_pp(var, m) << " from freeVar_map, it is bounded by an Unroll" << std::endl;); + freeVar_map.erase(var); + } + + // collect the case: + // * Concat(X, Y) = unroll(r1, t1) /\ Concat(X, Y) = unroll(r2, t2) + // concatEqUnrollsMap[Concat(X, Y)] = {unroll(r1, t1), unroll(r2, t2)} + + std::map > concatEqUnrollsMap; + for (std::map >::iterator urItor = unrollGroup_map.begin(); + urItor != unrollGroup_map.end(); urItor++) { + expr * unroll = urItor->first; + expr * curr = unroll; + do { + if (u.str.is_concat(to_app(curr))) { + concatEqUnrollsMap[curr].insert(unroll); + concatEqUnrollsMap[curr].insert(unrollGroup_map[unroll].begin(), unrollGroup_map[unroll].end()); + } + enode * e_curr = ctx.get_enode(curr); + curr = e_curr->get_next()->get_owner(); + // curr = get_eqc_next(curr); + } while (curr != unroll); + } + + std::map > concatFreeArgsEqUnrollsMap; + std::set fvUnrollSet; + for (std::map >::iterator concatItor = concatEqUnrollsMap.begin(); + concatItor != concatEqUnrollsMap.end(); concatItor++) { + expr * concat = concatItor->first; + expr * concatArg1 = to_app(concat)->get_arg(0); + expr * concatArg2 = to_app(concat)->get_arg(1); + bool arg1Bounded = false; + bool arg2Bounded = false; + // arg1 + if (variable_set.find(concatArg1) != variable_set.end()) { + if (freeVar_map.find(concatArg1) == freeVar_map.end()) { + arg1Bounded = true; + } else { + fvUnrollSet.insert(concatArg1); + } + } else if (u.str.is_concat(to_app(concatArg1))) { + if (concatEqUnrollsMap.find(concatArg1) == concatEqUnrollsMap.end()) { + arg1Bounded = true; + } + } + // arg2 + if (variable_set.find(concatArg2) != variable_set.end()) { + if (freeVar_map.find(concatArg2) == freeVar_map.end()) { + arg2Bounded = true; + } else { + fvUnrollSet.insert(concatArg2); + } + } else if (u.str.is_concat(to_app(concatArg2))) { + if (concatEqUnrollsMap.find(concatArg2) == concatEqUnrollsMap.end()) { + arg2Bounded = true; + } + } + if (!arg1Bounded && !arg2Bounded) { + concatFreeArgsEqUnrollsMap[concat].insert( + concatEqUnrollsMap[concat].begin(), + concatEqUnrollsMap[concat].end()); + } + } + for (std::set::iterator vItor = fvUnrollSet.begin(); vItor != fvUnrollSet.end(); vItor++) { + TRACE("str", tout << "remove " << mk_pp(*vItor, m) << " from freeVar_map" << std::endl;); + freeVar_map.erase(*vItor); + } + + // Assign free variables + std::set fSimpUnroll; + + constValue = NULL; + + { + TRACE("str", tout << "free var map (#" << freeVar_map.size() << "):" << std::endl; + for (std::map::iterator freeVarItor1 = freeVar_map.begin(); freeVarItor1 != freeVar_map.end(); freeVarItor1++) { + expr * freeVar = freeVarItor1->first; + rational lenValue; + bool lenValue_exists = get_len_value(freeVar, lenValue); + tout << mk_pp(freeVar, m) << " [depCnt = " << freeVarItor1->second << ", length = " + << (lenValue_exists ? lenValue.to_string() : "?") + << "]" << std::endl; + } + ); + } + + for (std::map >::iterator fvIt2 = concatFreeArgsEqUnrollsMap.begin(); + fvIt2 != concatFreeArgsEqUnrollsMap.end(); fvIt2++) { + expr * concat = fvIt2->first; + for (std::set::iterator urItor = fvIt2->second.begin(); urItor != fvIt2->second.end(); urItor++) { + expr * unroll = *urItor; + process_concat_eq_unroll(concat, unroll); + } + } + + // -------- + // experimental free variable assignment - begin + // * special handling for variables that are not used in concat + // -------- + bool testAssign = true; + if (!testAssign) { + for (std::map::iterator fvIt = freeVar_map.begin(); fvIt != freeVar_map.end(); fvIt++) { + expr * freeVar = fvIt->first; + /* + std::string vName = std::string(Z3_ast_to_string(ctx, freeVar)); + if (vName.length() >= 9 && vName.substr(0, 9) == "$$_regVar") { + continue; + } + */ + expr * toAssert = gen_len_val_options_for_free_var(freeVar, NULL, ""); + if (toAssert != NULL) { + assert_axiom(toAssert); + } + } + } else { + process_free_var(freeVar_map); + } + // experimental free variable assignment - end + + // now deal with removed free variables that are bounded by an unroll + TRACE("str", tout << "fv_unrolls_map (#" << fv_unrolls_map.size() << "):" << std::endl;); + for (std::map >::iterator fvIt1 = fv_unrolls_map.begin(); + fvIt1 != fv_unrolls_map.end(); fvIt1++) { + expr * var = fvIt1->first; + fSimpUnroll.clear(); + get_eqc_simpleUnroll(var, constValue, fSimpUnroll); + if (fSimpUnroll.size() == 0) { + gen_assign_unroll_reg(fv_unrolls_map[var]); + } else { + expr * toAssert = gen_assign_unroll_Str2Reg(var, fSimpUnroll); + if (toAssert != NULL) { + assert_axiom(toAssert); + } + } + } + + if (opt_VerifyFinalCheckProgress && !finalCheckProgressIndicator) { + TRACE("str", tout << "BUG: no progress in final check, giving up!!" << std::endl;); + m.raise_exception("no progress in theory_str final check"); + } + + return FC_CONTINUE; // since by this point we've added axioms + } + + inline zstring int_to_string(int i) { + std::stringstream ss; + ss << i; + std::string str = ss.str(); + return zstring(str.c_str()); + } + + inline std::string longlong_to_string(long long i) { + std::stringstream ss; + ss << i; + return ss.str(); + } + + void theory_str::print_value_tester_list(svector > & testerList) { + ast_manager & m = get_manager(); + TRACE("str", + int ss = testerList.size(); + tout << "valueTesterList = {"; + for (int i = 0; i < ss; ++i) { + if (i % 4 == 0) { + tout << std::endl; + } + tout << "(" << testerList[i].first << ", "; + tout << mk_ismt2_pp(testerList[i].second, m); + tout << "), "; + } + tout << std::endl << "}" << std::endl; + ); + } + + zstring theory_str::gen_val_string(int len, int_vector & encoding) { + SASSERT(charSetSize > 0); + SASSERT(char_set != NULL); + + std::string re(len, char_set[0]); + for (int i = 0; i < (int) encoding.size() - 1; i++) { + int idx = encoding[i]; + re[len - 1 - i] = char_set[idx]; + } + return zstring(re.c_str()); + } + + /* + * The return value indicates whether we covered the search space. + * - If the next encoding is valid, return false + * - Otherwise, return true + */ + bool theory_str::get_next_val_encode(int_vector & base, int_vector & next) { + SASSERT(charSetSize > 0); + + TRACE("str", tout << "base vector: [ "; + for (unsigned i = 0; i < base.size(); ++i) { + tout << base[i] << " "; + } + tout << "]" << std::endl; + ); + + int s = 0; + int carry = 0; + next.reset(); + + for (int i = 0; i < (int) base.size(); i++) { + if (i == 0) { + s = base[i] + 1; + carry = s / charSetSize; + s = s % charSetSize; + next.push_back(s); + } else { + s = base[i] + carry; + carry = s / charSetSize; + s = s % charSetSize; + next.push_back(s); + } + } + if (next[next.size() - 1] > 0) { + next.reset(); + return true; + } else { + return false; + } + } + + expr * theory_str::gen_val_options(expr * freeVar, expr * len_indicator, expr * val_indicator, + zstring lenStr, int tries) { + ast_manager & m = get_manager(); + context & ctx = get_context(); + + int distance = 32; + + // ---------------------------------------------------------------------------------------- + // generate value options encoding + // encoding is a vector of size (len + 1) + // e.g, len = 2, + // encoding {1, 2, 0} means the value option is "charSet[2]"."charSet[1]" + // the last item in the encoding indicates whether the whole space is covered + // for example, if the charSet = {a, b}. All valid encodings are + // {0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {1, 1, 0} + // if add 1 to the last one, we get + // {0, 0, 1} + // the last item "1" shows this is not a valid encoding, and we have covered all space + // ---------------------------------------------------------------------------------------- + int len = atoi(lenStr.encode().c_str()); + bool coverAll = false; + svector options; + int_vector base; + + TRACE("str", tout + << "freeVar = " << mk_ismt2_pp(freeVar, m) << std::endl + << "len_indicator = " << mk_ismt2_pp(len_indicator, m) << std::endl + << "val_indicator = " << mk_ismt2_pp(val_indicator, m) << std::endl + << "lenstr = " << lenStr << "\n" + << "tries = " << tries << "\n"; + if (m_params.m_AggressiveValueTesting) { + tout << "note: aggressive value testing is enabled" << std::endl; + } + ); + + if (tries == 0) { + base = int_vector(len + 1, 0); + coverAll = false; + } else { + expr * lastestValIndi = fvar_valueTester_map[freeVar][len][tries - 1].second; + TRACE("str", tout << "last value tester = " << mk_ismt2_pp(lastestValIndi, m) << std::endl;); + coverAll = get_next_val_encode(val_range_map[lastestValIndi], base); + } + + long long l = (tries) * distance; + long long h = l; + for (int i = 0; i < distance; i++) { + if (coverAll) + break; + options.push_back(base); + h++; + coverAll = get_next_val_encode(options[options.size() - 1], base); + } + val_range_map[val_indicator] = options[options.size() - 1]; + + TRACE("str", + tout << "value tester encoding " << "{" << std::endl; + int_vector vec = val_range_map[val_indicator]; + + for (int_vector::iterator it = vec.begin(); it != vec.end(); ++it) { + tout << *it << std::endl; + } + tout << "}" << std::endl; + ); + + // ---------------------------------------------------------------------------------------- + + ptr_vector orList; + ptr_vector andList; + + for (long long i = l; i < h; i++) { + orList.push_back(m.mk_eq(val_indicator, mk_string(longlong_to_string(i).c_str()) )); + if (m_params.m_AggressiveValueTesting) { + literal l = mk_eq(val_indicator, mk_string(longlong_to_string(i).c_str()), false); + ctx.mark_as_relevant(l); + ctx.force_phase(l); + } + + zstring aStr = gen_val_string(len, options[i - l]); + expr * strAst; + if (m_params.m_UseFastValueTesterCache) { + if (!valueTesterCache.find(aStr, strAst)) { + strAst = mk_string(aStr); + valueTesterCache.insert(aStr, strAst); + m_trail.push_back(strAst); + } + } else { + strAst = mk_string(aStr); + } + andList.push_back(m.mk_eq(orList[orList.size() - 1], m.mk_eq(freeVar, strAst))); + } + if (!coverAll) { + orList.push_back(m.mk_eq(val_indicator, mk_string("more"))); + if (m_params.m_AggressiveValueTesting) { + literal l = mk_eq(val_indicator, mk_string("more"), false); + ctx.mark_as_relevant(l); + ctx.force_phase(~l); + } + } + + expr ** or_items = alloc_svect(expr*, orList.size()); + expr ** and_items = alloc_svect(expr*, andList.size() + 1); + + for (int i = 0; i < (int) orList.size(); i++) { + or_items[i] = orList[i]; + } + if (orList.size() > 1) + and_items[0] = m.mk_or(orList.size(), or_items); + else + and_items[0] = or_items[0]; + + for (int i = 0; i < (int) andList.size(); i++) { + and_items[i + 1] = andList[i]; + } + expr * valTestAssert = m.mk_and(andList.size() + 1, and_items); + + // --------------------------------------- + // If the new value tester is $$_val_x_16_i + // Should add ($$_len_x_j = 16) /\ ($$_val_x_16_i = "more") + // --------------------------------------- + andList.reset(); + andList.push_back(m.mk_eq(len_indicator, mk_string(lenStr))); + for (int i = 0; i < tries; i++) { + expr * vTester = fvar_valueTester_map[freeVar][len][i].second; + if (vTester != val_indicator) + andList.push_back(m.mk_eq(vTester, mk_string("more"))); + } + expr * assertL = NULL; + if (andList.size() == 1) { + assertL = andList[0]; + } else { + expr ** and_items = alloc_svect(expr*, andList.size()); + for (int i = 0; i < (int) andList.size(); i++) { + and_items[i] = andList[i]; + } + assertL = m.mk_and(andList.size(), and_items); + } + + // (assertL => valTestAssert) <=> (!assertL OR valTestAssert) + valTestAssert = m.mk_or(m.mk_not(assertL), valTestAssert); + return valTestAssert; + } + + expr * theory_str::gen_free_var_options(expr * freeVar, expr * len_indicator, + zstring len_valueStr, expr * valTesterInCbEq, zstring valTesterValueStr) { + ast_manager & m = get_manager(); + + int len = atoi(len_valueStr.encode().c_str()); + + // check whether any value tester is actually in scope + TRACE("str", tout << "checking scope of previous value testers" << std::endl;); + bool map_effectively_empty = true; + if (fvar_valueTester_map[freeVar].find(len) != fvar_valueTester_map[freeVar].end()) { + // there's *something* in the map, but check its scope + svector > entries = fvar_valueTester_map[freeVar][len]; + for (svector >::iterator it = entries.begin(); it != entries.end(); ++it) { + std::pair entry = *it; + expr * aTester = entry.second; + if (internal_variable_set.find(aTester) == internal_variable_set.end()) { + TRACE("str", tout << mk_pp(aTester, m) << " out of scope" << std::endl;); + } else { + TRACE("str", tout << mk_pp(aTester, m) << " in scope" << std::endl;); + map_effectively_empty = false; + break; + } + } + } + + if (map_effectively_empty) { + TRACE("str", tout << "no previous value testers, or none of them were in scope" << std::endl;); + int tries = 0; + expr * val_indicator = mk_internal_valTest_var(freeVar, len, tries); + valueTester_fvar_map[val_indicator] = freeVar; + fvar_valueTester_map[freeVar][len].push_back(std::make_pair(sLevel, val_indicator)); + print_value_tester_list(fvar_valueTester_map[freeVar][len]); + return gen_val_options(freeVar, len_indicator, val_indicator, len_valueStr, tries); + } else { + TRACE("str", tout << "checking previous value testers" << std::endl;); + print_value_tester_list(fvar_valueTester_map[freeVar][len]); + + // go through all previous value testers + // If some doesn't have an eqc value, add its assertion again. + int testerTotal = fvar_valueTester_map[freeVar][len].size(); + int i = 0; + for (; i < testerTotal; i++) { + expr * aTester = fvar_valueTester_map[freeVar][len][i].second; + + // it's probably worth checking scope here, actually + if (internal_variable_set.find(aTester) == internal_variable_set.end()) { + TRACE("str", tout << "value tester " << mk_pp(aTester, m) << " out of scope, skipping" << std::endl;); + continue; + } + + if (aTester == valTesterInCbEq) { + break; + } + + bool anEqcHasValue = false; + // Z3_ast anEqc = get_eqc_value(t, aTester, anEqcHasValue); + expr * aTester_eqc_value = get_eqc_value(aTester, anEqcHasValue); + if (!anEqcHasValue) { + TRACE("str", tout << "value tester " << mk_ismt2_pp(aTester, m) + << " doesn't have an equivalence class value." << std::endl;); + refresh_theory_var(aTester); + + expr * makeupAssert = gen_val_options(freeVar, len_indicator, aTester, len_valueStr, i); + + TRACE("str", tout << "var: " << mk_ismt2_pp(freeVar, m) << std::endl + << mk_ismt2_pp(makeupAssert, m) << std::endl;); + assert_axiom(makeupAssert); + } else { + TRACE("str", tout << "value tester " << mk_ismt2_pp(aTester, m) + << " == " << mk_ismt2_pp(aTester_eqc_value, m) << std::endl;); + } + } + + if (valTesterValueStr == "more") { + expr * valTester = NULL; + if (i + 1 < testerTotal) { + valTester = fvar_valueTester_map[freeVar][len][i + 1].second; + refresh_theory_var(valTester); + } else { + valTester = mk_internal_valTest_var(freeVar, len, i + 1); + valueTester_fvar_map[valTester] = freeVar; + fvar_valueTester_map[freeVar][len].push_back(std::make_pair(sLevel, valTester)); + print_value_tester_list(fvar_valueTester_map[freeVar][len]); + } + expr * nextAssert = gen_val_options(freeVar, len_indicator, valTester, len_valueStr, i + 1); + return nextAssert; + } + + return NULL; + } + } + + void theory_str::reduce_virtual_regex_in(expr * var, expr * regex, expr_ref_vector & items) { + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + + TRACE("str", tout << "reduce regex " << mk_pp(regex, mgr) << " with respect to variable " << mk_pp(var, mgr) << std::endl;); + + app * regexFuncDecl = to_app(regex); + if (u.re.is_to_re(regexFuncDecl)) { + // --------------------------------------------------------- + // var \in Str2Reg(s1) + // ==> + // var = s1 /\ length(var) = length(s1) + // --------------------------------------------------------- + expr * strInside = to_app(regex)->get_arg(0); + items.push_back(ctx.mk_eq_atom(var, strInside)); + items.push_back(ctx.mk_eq_atom(mk_strlen(var), mk_strlen(strInside))); + return; + } + // RegexUnion + else if (u.re.is_union(regexFuncDecl)) { + // --------------------------------------------------------- + // var \in RegexUnion(r1, r2) + // ==> + // (var = newVar1 \/ var = newVar2) + // (var = newVar1 --> length(var) = length(newVar1)) /\ (var = newVar2 --> length(var) = length(newVar2)) + // /\ (newVar1 \in r1) /\ (newVar2 \in r2) + // --------------------------------------------------------- + expr_ref newVar1(mk_regex_rep_var(), mgr); + expr_ref newVar2(mk_regex_rep_var(), mgr); + items.push_back(mgr.mk_or(ctx.mk_eq_atom(var, newVar1), ctx.mk_eq_atom(var, newVar2))); + items.push_back(mgr.mk_or( + mgr.mk_not(ctx.mk_eq_atom(var, newVar1)), + ctx.mk_eq_atom(mk_strlen(var), mk_strlen(newVar1)))); + items.push_back(mgr.mk_or( + mgr.mk_not(ctx.mk_eq_atom(var, newVar2)), + ctx.mk_eq_atom(mk_strlen(var), mk_strlen(newVar2)))); + + expr * regArg1 = to_app(regex)->get_arg(0); + reduce_virtual_regex_in(newVar1, regArg1, items); + + expr * regArg2 = to_app(regex)->get_arg(1); + reduce_virtual_regex_in(newVar2, regArg2, items); + + return; + } + // RegexConcat + else if (u.re.is_concat(regexFuncDecl)) { + // --------------------------------------------------------- + // var \in RegexConcat(r1, r2) + // ==> + // (var = newVar1 . newVar2) /\ (length(var) = length(vewVar1 . newVar2) ) + // /\ (newVar1 \in r1) /\ (newVar2 \in r2) + // --------------------------------------------------------- + expr_ref newVar1(mk_regex_rep_var(), mgr); + expr_ref newVar2(mk_regex_rep_var(), mgr); + expr_ref concatAst(mk_concat(newVar1, newVar2), mgr); + items.push_back(ctx.mk_eq_atom(var, concatAst)); + items.push_back(ctx.mk_eq_atom(mk_strlen(var), + m_autil.mk_add(mk_strlen(newVar1), mk_strlen(newVar2)))); + + expr * regArg1 = to_app(regex)->get_arg(0); + reduce_virtual_regex_in(newVar1, regArg1, items); + expr * regArg2 = to_app(regex)->get_arg(1); + reduce_virtual_regex_in(newVar2, regArg2, items); + return; + } + // Unroll + else if (u.re.is_star(regexFuncDecl)) { + // --------------------------------------------------------- + // var \in Star(r1) + // ==> + // var = unroll(r1, t1) /\ |var| = |unroll(r1, t1)| + // --------------------------------------------------------- + expr * regArg = to_app(regex)->get_arg(0); + expr_ref unrollCnt(mk_unroll_bound_var(), mgr); + expr_ref unrollFunc(mk_unroll(regArg, unrollCnt), mgr); + items.push_back(ctx.mk_eq_atom(var, unrollFunc)); + items.push_back(ctx.mk_eq_atom(mk_strlen(var), mk_strlen(unrollFunc))); + return; + } + // re.range + else if (u.re.is_range(regexFuncDecl)) { + // var in range("a", "z") + // ==> + // (var = "a" or var = "b" or ... or var = "z") + expr_ref lo(regexFuncDecl->get_arg(0), mgr); + expr_ref hi(regexFuncDecl->get_arg(1), mgr); + zstring str_lo, str_hi; + SASSERT(u.str.is_string(lo)); + SASSERT(u.str.is_string(hi)); + u.str.is_string(lo, str_lo); + u.str.is_string(hi, str_hi); + SASSERT(str_lo.length() == 1); + SASSERT(str_hi.length() == 1); + unsigned int c1 = str_lo[0]; + unsigned int c2 = str_hi[0]; + if (c1 > c2) { + // exchange + unsigned int tmp = c1; + c1 = c2; + c2 = tmp; + } + expr_ref_vector range_cases(mgr); + for (unsigned int ch = c1; ch <= c2; ++ch) { + zstring s_ch(ch); + expr_ref rhs(ctx.mk_eq_atom(var, u.str.mk_string(s_ch)), mgr); + range_cases.push_back(rhs); + } + expr_ref rhs(mk_or(range_cases), mgr); + SASSERT(rhs); + assert_axiom(rhs); + return; + } else { + get_manager().raise_exception("unrecognized regex operator"); + UNREACHABLE(); + } + } + + void theory_str::gen_assign_unroll_reg(std::set & unrolls) { + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + + expr_ref_vector items(mgr); + for (std::set::iterator itor = unrolls.begin(); itor != unrolls.end(); itor++) { + expr * unrFunc = *itor; + TRACE("str", tout << "generating assignment for unroll " << mk_pp(unrFunc, mgr) << std::endl;); + + expr * regexInUnr = to_app(unrFunc)->get_arg(0); + expr * cntInUnr = to_app(unrFunc)->get_arg(1); + items.reset(); + + rational low, high; + bool low_exists = lower_bound(cntInUnr, low); + bool high_exists = upper_bound(cntInUnr, high); + + TRACE("str", + tout << "unroll " << mk_pp(unrFunc, mgr) << std::endl; + rational unrLenValue; + bool unrLenValue_exists = get_len_value(unrFunc, unrLenValue); + tout << "unroll length: " << (unrLenValue_exists ? unrLenValue.to_string() : "?") << std::endl; + rational cntInUnrValue; + bool cntHasValue = get_value(cntInUnr, cntInUnrValue); + tout << "unroll count: " << (cntHasValue ? cntInUnrValue.to_string() : "?") + << " low = " + << (low_exists ? low.to_string() : "?") + << " high = " + << (high_exists ? high.to_string() : "?") + << std::endl; + ); + + expr_ref toAssert(mgr); + if (low.is_neg()) { + toAssert = m_autil.mk_ge(cntInUnr, mk_int(0)); + } else { + if (unroll_var_map.find(unrFunc) == unroll_var_map.end()) { + + expr_ref newVar1(mk_regex_rep_var(), mgr); + expr_ref newVar2(mk_regex_rep_var(), mgr); + expr_ref concatAst(mk_concat(newVar1, newVar2), mgr); + expr_ref newCnt(mk_unroll_bound_var(), mgr); + expr_ref newUnrollFunc(mk_unroll(regexInUnr, newCnt), mgr); + + // unroll(r1, t1) = newVar1 . newVar2 + items.push_back(ctx.mk_eq_atom(unrFunc, concatAst)); + items.push_back(ctx.mk_eq_atom(mk_strlen(unrFunc), m_autil.mk_add(mk_strlen(newVar1), mk_strlen(newVar2)))); + // mk_strlen(unrFunc) >= mk_strlen(newVar{1,2}) + items.push_back(m_autil.mk_ge(m_autil.mk_add(mk_strlen(unrFunc), m_autil.mk_mul(mk_int(-1), mk_strlen(newVar1))), mk_int(0))); + items.push_back(m_autil.mk_ge(m_autil.mk_add(mk_strlen(unrFunc), m_autil.mk_mul(mk_int(-1), mk_strlen(newVar2))), mk_int(0))); + // newVar1 \in r1 + reduce_virtual_regex_in(newVar1, regexInUnr, items); + items.push_back(ctx.mk_eq_atom(cntInUnr, m_autil.mk_add(newCnt, mk_int(1)))); + items.push_back(ctx.mk_eq_atom(newVar2, newUnrollFunc)); + items.push_back(ctx.mk_eq_atom(mk_strlen(newVar2), mk_strlen(newUnrollFunc))); + toAssert = ctx.mk_eq_atom( + m_autil.mk_ge(cntInUnr, mk_int(1)), + mk_and(items)); + + // option 0 + expr_ref op0(ctx.mk_eq_atom(cntInUnr, mk_int(0)), mgr); + expr_ref ast1(ctx.mk_eq_atom(unrFunc, mk_string("")), mgr); + expr_ref ast2(ctx.mk_eq_atom(mk_strlen(unrFunc), mk_int(0)), mgr); + expr_ref and1(mgr.mk_and(ast1, ast2), mgr); + + // put together + toAssert = mgr.mk_and(ctx.mk_eq_atom(op0, and1), toAssert); + + unroll_var_map[unrFunc] = toAssert; + } else { + toAssert = unroll_var_map[unrFunc]; + } + } + m_trail.push_back(toAssert); + assert_axiom(toAssert); + } + } + + static int computeGCD(int x, int y) { + if (x == 0) { + return y; + } + while (y != 0) { + if (x > y) { + x = x - y; + } else { + y = y - x; + } + } + return x; + } + + static int computeLCM(int a, int b) { + int temp = computeGCD(a, b); + return temp ? (a / temp * b) : 0; + } + + static zstring get_unrolled_string(zstring core, int count) { + zstring res(""); + for (int i = 0; i < count; i++) { + res = res + core; + } + return res; + } + + expr * theory_str::gen_assign_unroll_Str2Reg(expr * n, std::set & unrolls) { + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + + int lcm = 1; + int coreValueCount = 0; + expr * oneUnroll = NULL; + zstring oneCoreStr(""); + for (std::set::iterator itor = unrolls.begin(); itor != unrolls.end(); itor++) { + expr * str2RegFunc = to_app(*itor)->get_arg(0); + expr * coreVal = to_app(str2RegFunc)->get_arg(0); + zstring coreStr; + u.str.is_string(coreVal, coreStr); + if (oneUnroll == NULL) { + oneUnroll = *itor; + oneCoreStr = coreStr; + } + coreValueCount++; + int core1Len = coreStr.length(); + lcm = computeLCM(lcm, core1Len); + } + // + bool canHaveNonEmptyAssign = true; + expr_ref_vector litems(mgr); + zstring lcmStr = get_unrolled_string(oneCoreStr, (lcm / oneCoreStr.length())); + for (std::set::iterator itor = unrolls.begin(); itor != unrolls.end(); itor++) { + expr * str2RegFunc = to_app(*itor)->get_arg(0); + expr * coreVal = to_app(str2RegFunc)->get_arg(0); + zstring coreStr; + u.str.is_string(coreVal, coreStr); + unsigned int core1Len = coreStr.length(); + zstring uStr = get_unrolled_string(coreStr, (lcm / core1Len)); + if (uStr != lcmStr) { + canHaveNonEmptyAssign = false; + } + litems.push_back(ctx.mk_eq_atom(n, *itor)); + } + + if (canHaveNonEmptyAssign) { + return gen_unroll_conditional_options(n, unrolls, lcmStr); + } else { + expr_ref implyL(mk_and(litems), mgr); + expr_ref implyR(ctx.mk_eq_atom(n, mk_string("")), mgr); + // want to return (implyL -> implyR) + expr * final_axiom = rewrite_implication(implyL, implyR); + return final_axiom; + } + } + + expr * theory_str::gen_unroll_conditional_options(expr * var, std::set & unrolls, zstring lcmStr) { + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + + int dist = opt_LCMUnrollStep; + expr_ref_vector litems(mgr); + expr_ref moreAst(mk_string("more"), mgr); + for (std::set::iterator itor = unrolls.begin(); itor != unrolls.end(); itor++) { + expr_ref item(ctx.mk_eq_atom(var, *itor), mgr); + TRACE("str", tout << "considering unroll " << mk_pp(item, mgr) << std::endl;); + litems.push_back(item); + } + + // handle out-of-scope entries in unroll_tries_map + + ptr_vector outOfScopeTesters; + + for (ptr_vector::iterator it = unroll_tries_map[var][unrolls].begin(); + it != unroll_tries_map[var][unrolls].end(); ++it) { + expr * tester = *it; + bool inScope = (internal_unrollTest_vars.find(tester) != internal_unrollTest_vars.end()); + TRACE("str", tout << "unroll test var " << mk_pp(tester, mgr) + << (inScope ? " in scope" : " out of scope") + << std::endl;); + if (!inScope) { + outOfScopeTesters.push_back(tester); + } + } + + for (ptr_vector::iterator it = outOfScopeTesters.begin(); + it != outOfScopeTesters.end(); ++it) { + unroll_tries_map[var][unrolls].erase(*it); + } + + + if (unroll_tries_map[var][unrolls].size() == 0) { + unroll_tries_map[var][unrolls].push_back(mk_unroll_test_var()); + } + + int tries = unroll_tries_map[var][unrolls].size(); + for (int i = 0; i < tries; i++) { + expr * tester = unroll_tries_map[var][unrolls][i]; + // TESTING + refresh_theory_var(tester); + bool testerHasValue = false; + expr * testerVal = get_eqc_value(tester, testerHasValue); + if (!testerHasValue) { + // generate make-up assertion + int l = i * dist; + int h = (i + 1) * dist; + expr_ref lImp(mk_and(litems), mgr); + expr_ref rImp(gen_unroll_assign(var, lcmStr, tester, l, h), mgr); + + SASSERT(lImp); + TRACE("str", tout << "lImp = " << mk_pp(lImp, mgr) << std::endl;); + SASSERT(rImp); + TRACE("str", tout << "rImp = " << mk_pp(rImp, mgr) << std::endl;); + + expr_ref toAssert(mgr.mk_or(mgr.mk_not(lImp), rImp), mgr); + SASSERT(toAssert); + TRACE("str", tout << "Making up assignments for variable which is equal to unbounded Unroll" << std::endl;); + m_trail.push_back(toAssert); + return toAssert; + + // note: this is how the code looks in Z3str2's strRegex.cpp:genUnrollConditionalOptions. + // the return is in the same place + + // insert [tester = "more"] to litems so that the implyL for next tester is correct + litems.push_back(ctx.mk_eq_atom(tester, moreAst)); + } else { + zstring testerStr; + u.str.is_string(testerVal, testerStr); + TRACE("str", tout << "Tester [" << mk_pp(tester, mgr) << "] = " << testerStr << "\n";); + if (testerStr == "more") { + litems.push_back(ctx.mk_eq_atom(tester, moreAst)); + } + } + } + expr * tester = mk_unroll_test_var(); + unroll_tries_map[var][unrolls].push_back(tester); + int l = tries * dist; + int h = (tries + 1) * dist; + expr_ref lImp(mk_and(litems), mgr); + expr_ref rImp(gen_unroll_assign(var, lcmStr, tester, l, h), mgr); + SASSERT(lImp); + SASSERT(rImp); + expr_ref toAssert(mgr.mk_or(mgr.mk_not(lImp), rImp), mgr); + SASSERT(toAssert); + TRACE("str", tout << "Generating assignment for variable which is equal to unbounded Unroll" << std::endl;); + m_trail.push_back(toAssert); + return toAssert; + } + + expr * theory_str::gen_unroll_assign(expr * var, zstring lcmStr, expr * testerVar, int l, int h) { + context & ctx = get_context(); + ast_manager & mgr = get_manager(); + + TRACE("str", tout << "entry: var = " << mk_pp(var, mgr) << ", lcmStr = " << lcmStr + << ", l = " << l << ", h = " << h << "\n";); + + if (m_params.m_AggressiveUnrollTesting) { + TRACE("str", tout << "note: aggressive unroll testing is active" << std::endl;); + } + + expr_ref_vector orItems(mgr); + expr_ref_vector andItems(mgr); + + for (int i = l; i < h; i++) { + zstring iStr = int_to_string(i); + expr_ref testerEqAst(ctx.mk_eq_atom(testerVar, mk_string(iStr)), mgr); + TRACE("str", tout << "testerEqAst = " << mk_pp(testerEqAst, mgr) << std::endl;); + if (m_params.m_AggressiveUnrollTesting) { + literal l = mk_eq(testerVar, mk_string(iStr), false); + ctx.mark_as_relevant(l); + ctx.force_phase(l); + } + + orItems.push_back(testerEqAst); + zstring unrollStrInstance = get_unrolled_string(lcmStr, i); + + expr_ref x1(ctx.mk_eq_atom(testerEqAst, ctx.mk_eq_atom(var, mk_string(unrollStrInstance))), mgr); + TRACE("str", tout << "x1 = " << mk_pp(x1, mgr) << std::endl;); + andItems.push_back(x1); + + expr_ref x2(ctx.mk_eq_atom(testerEqAst, ctx.mk_eq_atom(mk_strlen(var), mk_int(i * lcmStr.length()))), mgr); + TRACE("str", tout << "x2 = " << mk_pp(x2, mgr) << std::endl;); + andItems.push_back(x2); + } + expr_ref testerEqMore(ctx.mk_eq_atom(testerVar, mk_string("more")), mgr); + TRACE("str", tout << "testerEqMore = " << mk_pp(testerEqMore, mgr) << std::endl;); + if (m_params.m_AggressiveUnrollTesting) { + literal l = mk_eq(testerVar, mk_string("more"), false); + ctx.mark_as_relevant(l); + ctx.force_phase(~l); + } + + orItems.push_back(testerEqMore); + int nextLowerLenBound = h * lcmStr.length(); + expr_ref more2(ctx.mk_eq_atom(testerEqMore, + //Z3_mk_ge(mk_length(t, var), mk_int(ctx, nextLowerLenBound)) + m_autil.mk_ge(m_autil.mk_add(mk_strlen(var), mk_int(-1 * nextLowerLenBound)), mk_int(0)) + ), mgr); + TRACE("str", tout << "more2 = " << mk_pp(more2, mgr) << std::endl;); + andItems.push_back(more2); + + expr_ref finalOR(mgr.mk_or(orItems.size(), orItems.c_ptr()), mgr); + TRACE("str", tout << "finalOR = " << mk_pp(finalOR, mgr) << std::endl;); + andItems.push_back(mk_or(orItems)); + + expr_ref finalAND(mgr.mk_and(andItems.size(), andItems.c_ptr()), mgr); + TRACE("str", tout << "finalAND = " << mk_pp(finalAND, mgr) << std::endl;); + + // doing the following avoids a segmentation fault + m_trail.push_back(finalAND); + return finalAND; + } + + expr * theory_str::gen_len_test_options(expr * freeVar, expr * indicator, int tries) { + ast_manager & m = get_manager(); + context & ctx = get_context(); + + expr_ref freeVarLen(mk_strlen(freeVar), m); + SASSERT(freeVarLen); + + expr_ref_vector orList(m); + expr_ref_vector andList(m); + + int distance = 3; + int l = (tries - 1) * distance; + int h = tries * distance; + + TRACE("str", + tout << "building andList and orList" << std::endl; + if (m_params.m_AggressiveLengthTesting) { + tout << "note: aggressive length testing is active" << std::endl; + } + ); + + // experimental theory-aware case split support + literal_vector case_split_literals; + + for (int i = l; i < h; ++i) { + expr_ref str_indicator(m); + if (m_params.m_UseFastLengthTesterCache) { + rational ri(i); + expr * lookup_val; + if(lengthTesterCache.find(ri, lookup_val)) { + str_indicator = expr_ref(lookup_val, m); + } else { + // no match; create and insert + zstring i_str = int_to_string(i); + expr_ref new_val(mk_string(i_str), m); + lengthTesterCache.insert(ri, new_val); + m_trail.push_back(new_val); + str_indicator = expr_ref(new_val, m); + } + } else { + zstring i_str = int_to_string(i); + str_indicator = expr_ref(mk_string(i_str), m); + } + expr_ref or_expr(ctx.mk_eq_atom(indicator, str_indicator), m); + orList.push_back(or_expr); + + double priority; + // give high priority to small lengths if this is available + if (i <= 5) { + priority = 0.3; + } else { + // prioritize over "more" + priority = 0.2; + } + add_theory_aware_branching_info(or_expr, priority, l_true); + + if (m_params.m_AggressiveLengthTesting) { + literal l = mk_eq(indicator, str_indicator, false); + ctx.mark_as_relevant(l); + ctx.force_phase(l); + } + + case_split_literals.insert(mk_eq(freeVarLen, mk_int(i), false)); + + expr_ref and_expr(ctx.mk_eq_atom(orList.get(orList.size() - 1), m.mk_eq(freeVarLen, mk_int(i))), m); + andList.push_back(and_expr); + } + + expr_ref more_option(ctx.mk_eq_atom(indicator, mk_string("more")), m); + orList.push_back(more_option); + // decrease priority of this option + add_theory_aware_branching_info(more_option, -0.1, l_true); + if (m_params.m_AggressiveLengthTesting) { + literal l = mk_eq(indicator, mk_string("more"), false); + ctx.mark_as_relevant(l); + ctx.force_phase(~l); + } + + andList.push_back(ctx.mk_eq_atom(orList.get(orList.size() - 1), m_autil.mk_ge(freeVarLen, mk_int(h)))); + + /* + { // more experimental theory case split support + expr_ref tmp(m_autil.mk_ge(freeVarLen, mk_int(h)), m); + ctx.internalize(m_autil.mk_ge(freeVarLen, mk_int(h)), false); + case_split_literals.push_back(ctx.get_literal(tmp)); + ctx.mk_th_case_split(case_split_literals.size(), case_split_literals.c_ptr()); + } + */ + + expr_ref_vector or_items(m); + expr_ref_vector and_items(m); + + for (unsigned i = 0; i < orList.size(); ++i) { + or_items.push_back(orList.get(i)); + } + + and_items.push_back(mk_or(or_items)); + for(unsigned i = 0; i < andList.size(); ++i) { + and_items.push_back(andList.get(i)); + } + + TRACE("str", tout << "check: " << mk_pp(mk_and(and_items), m) << std::endl;); + + expr_ref lenTestAssert = mk_and(and_items); + SASSERT(lenTestAssert); + TRACE("str", tout << "crash avoidance lenTestAssert: " << mk_pp(lenTestAssert, m) << std::endl;); + + int testerCount = tries - 1; + if (testerCount > 0) { + expr_ref_vector and_items_LHS(m); + expr_ref moreAst(mk_string("more"), m); + for (int i = 0; i < testerCount; ++i) { + expr * indicator = fvar_lenTester_map[freeVar][i]; + if (internal_variable_set.find(indicator) == internal_variable_set.end()) { + TRACE("str", tout << "indicator " << mk_pp(indicator, m) << " out of scope; continuing" << std::endl;); + continue; + } else { + TRACE("str", tout << "indicator " << mk_pp(indicator, m) << " in scope" << std::endl;); + and_items_LHS.push_back(ctx.mk_eq_atom(indicator, moreAst)); + } + } + expr_ref assertL(mk_and(and_items_LHS), m); + SASSERT(assertL); + expr * finalAxiom = m.mk_or(m.mk_not(assertL), lenTestAssert.get()); + SASSERT(finalAxiom != NULL); + TRACE("str", tout << "crash avoidance finalAxiom: " << mk_pp(finalAxiom, m) << std::endl;); + return finalAxiom; + } else { + TRACE("str", tout << "crash avoidance lenTestAssert.get(): " << mk_pp(lenTestAssert.get(), m) << std::endl;); + m_trail.push_back(lenTestAssert.get()); + return lenTestAssert.get(); + } + } + + // Return an expression of the form + // (tester = "less" | tester = "N" | tester = "more") & + // (tester = "less" iff len(freeVar) < N) & (tester = "more" iff len(freeVar) > N) & (tester = "N" iff len(freeVar) = N)) + expr_ref theory_str::binary_search_case_split(expr * freeVar, expr * tester, binary_search_info & bounds, literal_vector & case_split) { + context & ctx = get_context(); + ast_manager & m = get_manager(); + rational N = bounds.midPoint; + rational N_minus_one = N - rational::one(); + rational N_plus_one = N + rational::one(); + expr_ref lenFreeVar(mk_strlen(freeVar), m); + + TRACE("str", tout << "create case split for free var " << mk_pp(freeVar, m) + << " over " << mk_pp(tester, m) << " with midpoint " << N << std::endl;); + + expr_ref_vector combinedCaseSplit(m); + expr_ref_vector testerCases(m); + + expr_ref caseLess(ctx.mk_eq_atom(tester, mk_string("less")), m); + testerCases.push_back(caseLess); + combinedCaseSplit.push_back(ctx.mk_eq_atom(caseLess, m_autil.mk_le(lenFreeVar, m_autil.mk_numeral(N_minus_one, true) ))); + + expr_ref caseMore(ctx.mk_eq_atom(tester, mk_string("more")), m); + testerCases.push_back(caseMore); + combinedCaseSplit.push_back(ctx.mk_eq_atom(caseMore, m_autil.mk_ge(lenFreeVar, m_autil.mk_numeral(N_plus_one, true) ))); + + expr_ref caseEq(ctx.mk_eq_atom(tester, mk_string(N.to_string().c_str())), m); + testerCases.push_back(caseEq); + combinedCaseSplit.push_back(ctx.mk_eq_atom(caseEq, ctx.mk_eq_atom(lenFreeVar, m_autil.mk_numeral(N, true)))); + + combinedCaseSplit.push_back(mk_or(testerCases)); + + // force internalization on all terms in testerCases so we can extract literals + for (unsigned i = 0; i < testerCases.size(); ++i) { + expr * testerCase = testerCases.get(i); + if (!ctx.b_internalized(testerCase)) { + ctx.internalize(testerCase, false); + } + literal l = ctx.get_literal(testerCase); + case_split.push_back(l); + } + + expr_ref final_term(mk_and(combinedCaseSplit), m); + SASSERT(final_term); + TRACE("str", tout << "final term: " << mk_pp(final_term, m) << std::endl;); + return final_term; + } + + expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenTester, zstring previousLenTesterValue) { + ast_manager & m = get_manager(); + context & ctx = get_context(); + + if (binary_search_len_tester_stack.contains(freeVar) && !binary_search_len_tester_stack[freeVar].empty()) { + TRACE("str", tout << "checking existing length testers for " << mk_pp(freeVar, m) << std::endl; + for (ptr_vector::const_iterator it = binary_search_len_tester_stack[freeVar].begin(); + it != binary_search_len_tester_stack[freeVar].end(); ++it) { + expr * tester = *it; + tout << mk_pp(tester, m) << ": "; + if (binary_search_len_tester_info.contains(tester)) { + binary_search_info & bounds = binary_search_len_tester_info[tester]; + tout << "[" << bounds.lowerBound << " | " << bounds.midPoint << " | " << bounds.upperBound << "]!" << bounds.windowSize; + } else { + tout << "[WARNING: no bounds info available]"; + } + bool hasEqcValue; + expr * testerEqcValue = get_eqc_value(tester, hasEqcValue); + if (hasEqcValue) { + tout << " = " << mk_pp(testerEqcValue, m); + } else { + tout << " [no eqc value]"; + } + tout << std::endl; + } + ); + expr * lastTester = binary_search_len_tester_stack[freeVar].back(); + bool lastTesterHasEqcValue; + expr * lastTesterValue = get_eqc_value(lastTester, lastTesterHasEqcValue); + zstring lastTesterConstant; + if (!lastTesterHasEqcValue) { + TRACE("str", tout << "length tester " << mk_pp(lastTester, m) << " at top of stack doesn't have an EQC value yet" << std::endl;); + // check previousLenTester + if (previousLenTester == lastTester) { + lastTesterConstant = previousLenTesterValue; + TRACE("str", tout << "invoked with previousLenTester info matching top of stack" << std::endl;); + } else { + TRACE("str", tout << "WARNING: unexpected reordering of length testers!" << std::endl;); + UNREACHABLE(); return NULL; + } + } else { + u.str.is_string(lastTesterValue, lastTesterConstant); + } + TRACE("str", tout << "last length tester is assigned \"" << lastTesterConstant << "\"" << "\n";); + if (lastTesterConstant == "more" || lastTesterConstant == "less") { + // use the previous bounds info to generate a new midpoint + binary_search_info lastBounds; + if (!binary_search_len_tester_info.find(lastTester, lastBounds)) { + // unexpected + TRACE("str", tout << "WARNING: no bounds information available for last tester!" << std::endl;); + UNREACHABLE(); + } + TRACE("str", tout << "last bounds are [" << lastBounds.lowerBound << " | " << lastBounds.midPoint << " | " << lastBounds.upperBound << "]!" << lastBounds.windowSize << std::endl;); + binary_search_info newBounds; + expr * newTester; + if (lastTesterConstant == "more") { + // special case: if the midpoint, upper bound, and window size are all equal, + // we double the window size and adjust the bounds + if (lastBounds.midPoint == lastBounds.upperBound && lastBounds.upperBound == lastBounds.windowSize) { + TRACE("str", tout << "search hit window size; expanding" << std::endl;); + newBounds.lowerBound = lastBounds.windowSize + rational::one(); + newBounds.windowSize = lastBounds.windowSize * rational(2); + newBounds.upperBound = newBounds.windowSize; + newBounds.calculate_midpoint(); + } else if (false) { + // handle the case where the midpoint can't be increased further + // (e.g. a window like [50 | 50 | 50]!64 and we don't answer "50") + } else { + // general case + newBounds.lowerBound = lastBounds.midPoint + rational::one(); + newBounds.windowSize = lastBounds.windowSize; + newBounds.upperBound = lastBounds.upperBound; + newBounds.calculate_midpoint(); + } + if (!binary_search_next_var_high.find(lastTester, newTester)) { + newTester = mk_internal_lenTest_var(freeVar, newBounds.midPoint.get_int32()); + binary_search_next_var_high.insert(lastTester, newTester); + } + refresh_theory_var(newTester); + } else if (lastTesterConstant == "less") { + if (false) { + // handle the case where the midpoint can't be decreased further + // (e.g. a window like [0 | 0 | 0]!64 and we don't answer "0" + } else { + // general case + newBounds.upperBound = lastBounds.midPoint - rational::one(); + newBounds.windowSize = lastBounds.windowSize; + newBounds.lowerBound = lastBounds.lowerBound; + newBounds.calculate_midpoint(); + } + if (!binary_search_next_var_low.find(lastTester, newTester)) { + newTester = mk_internal_lenTest_var(freeVar, newBounds.midPoint.get_int32()); + binary_search_next_var_low.insert(lastTester, newTester); + } + refresh_theory_var(newTester); + } + TRACE("str", tout << "new bounds are [" << newBounds.lowerBound << " | " << newBounds.midPoint << " | " << newBounds.upperBound << "]!" << newBounds.windowSize << std::endl;); + binary_search_len_tester_stack[freeVar].push_back(newTester); + m_trail_stack.push(binary_search_trail(binary_search_len_tester_stack, freeVar)); + binary_search_len_tester_info.insert(newTester, newBounds); + m_trail_stack.push(insert_obj_map(binary_search_len_tester_info, newTester)); + + literal_vector case_split_literals; + expr_ref next_case_split(binary_search_case_split(freeVar, newTester, newBounds, case_split_literals)); + m_trail.push_back(next_case_split); + // ctx.mk_th_case_split(case_split_literals.size(), case_split_literals.c_ptr()); + return next_case_split; + } else { // lastTesterConstant is a concrete value + TRACE("str", tout << "length is fixed; generating models for free var" << std::endl;); + // defensive check that this length did not converge on a negative value. + binary_search_info lastBounds; + if (!binary_search_len_tester_info.find(lastTester, lastBounds)) { + // unexpected + TRACE("str", tout << "WARNING: no bounds information available for last tester!" << std::endl;); + UNREACHABLE(); + } + if (lastBounds.midPoint.is_neg()) { + TRACE("str", tout << "WARNING: length search converged on a negative value. Negating this constraint." << std::endl;); + expr_ref axiom(m_autil.mk_ge(mk_strlen(freeVar), m_autil.mk_numeral(rational::zero(), true)), m); + return axiom; + } + // length is fixed + expr * valueAssert = gen_free_var_options(freeVar, lastTester, lastTesterConstant, NULL, zstring("")); + return valueAssert; + } + } else { + // no length testers yet + TRACE("str", tout << "no length testers for " << mk_pp(freeVar, m) << std::endl;); + binary_search_len_tester_stack.insert(freeVar, ptr_vector()); + + expr * firstTester; + rational lowerBound(0); + rational upperBound(m_params.m_BinarySearchInitialUpperBound); + rational windowSize(upperBound); + rational midPoint(floor(upperBound / rational(2))); + if (!binary_search_starting_len_tester.find(freeVar, firstTester)) { + firstTester = mk_internal_lenTest_var(freeVar, midPoint.get_int32()); + binary_search_starting_len_tester.insert(freeVar, firstTester); + } + refresh_theory_var(firstTester); + + binary_search_len_tester_stack[freeVar].push_back(firstTester); + m_trail_stack.push(binary_search_trail(binary_search_len_tester_stack, freeVar)); + binary_search_info new_info(lowerBound, midPoint, upperBound, windowSize); + binary_search_len_tester_info.insert(firstTester, new_info); + m_trail_stack.push(insert_obj_map(binary_search_len_tester_info, firstTester)); + + literal_vector case_split_literals; + expr_ref initial_case_split(binary_search_case_split(freeVar, firstTester, new_info, case_split_literals)); + m_trail.push_back(initial_case_split); + // ctx.mk_th_case_split(case_split_literals.size(), case_split_literals.c_ptr()); + return initial_case_split; + } + } + + // ----------------------------------------------------------------------------------------------------- + // True branch will be taken in final_check: + // - When we discover a variable is "free" for the first time + // lenTesterInCbEq = NULL + // lenTesterValue = "" + // False branch will be taken when invoked by new_eq_eh(). + // - After we set up length tester for a "free" var in final_check, + // when the tester is assigned to some value (e.g. "more" or "4"), + // lenTesterInCbEq != NULL, and its value will be passed by lenTesterValue + // The difference is that in new_eq_eh(), lenTesterInCbEq and its value have NOT been put into a same eqc + // ----------------------------------------------------------------------------------------------------- + expr * theory_str::gen_len_val_options_for_free_var(expr * freeVar, expr * lenTesterInCbEq, zstring lenTesterValue) { + + ast_manager & m = get_manager(); + + TRACE("str", tout << "gen for free var " << mk_ismt2_pp(freeVar, m) << std::endl;); + + if (m_params.m_UseBinarySearch) { + TRACE("str", tout << "using binary search heuristic" << std::endl;); + return binary_search_length_test(freeVar, lenTesterInCbEq, lenTesterValue); + } else { + bool map_effectively_empty = false; + if (!fvar_len_count_map.contains(freeVar)) { + TRACE("str", tout << "fvar_len_count_map is empty" << std::endl;); + map_effectively_empty = true; + } + + if (!map_effectively_empty) { + // check whether any entries correspond to variables that went out of scope; + // if every entry is out of scope then the map counts as being empty + + // assume empty and find a counterexample + map_effectively_empty = true; + ptr_vector indicator_set = fvar_lenTester_map[freeVar]; + for (ptr_vector::iterator it = indicator_set.begin(); it != indicator_set.end(); ++it) { + expr * indicator = *it; + if (internal_variable_set.find(indicator) != internal_variable_set.end()) { + TRACE("str", tout <<"found active internal variable " << mk_ismt2_pp(indicator, m) + << " in fvar_lenTester_map[freeVar]" << std::endl;); + map_effectively_empty = false; + break; + } + } + CTRACE("str", map_effectively_empty, tout << "all variables in fvar_lenTester_map[freeVar] out of scope" << std::endl;); + } + + if (map_effectively_empty) { + // no length assertions for this free variable have ever been added. + TRACE("str", tout << "no length assertions yet" << std::endl;); + + fvar_len_count_map.insert(freeVar, 1); + unsigned int testNum = fvar_len_count_map[freeVar]; + + expr_ref indicator(mk_internal_lenTest_var(freeVar, testNum), m); + SASSERT(indicator); + + // since the map is "effectively empty", we can remove those variables that have left scope... + fvar_lenTester_map[freeVar].shrink(0); + fvar_lenTester_map[freeVar].push_back(indicator); + lenTester_fvar_map.insert(indicator, freeVar); + expr * lenTestAssert = gen_len_test_options(freeVar, indicator, testNum); SASSERT(lenTestAssert != NULL); return lenTestAssert; } else { - TRACE("str", tout << "length is fixed; generating models for free var" << std::endl;); - // length is fixed - expr * valueAssert = gen_free_var_options(freeVar, effectiveLenInd, effectiveLenIndiStr, NULL, zstring("")); - return valueAssert; + TRACE("str", tout << "found previous in-scope length assertions" << std::endl;); + + expr * effectiveLenInd = NULL; + zstring effectiveLenIndiStr(""); + int lenTesterCount = (int) fvar_lenTester_map[freeVar].size(); + + TRACE("str", + tout << lenTesterCount << " length testers in fvar_lenTester_map[" << mk_pp(freeVar, m) << "]:" << std::endl; + for (int i = 0; i < lenTesterCount; ++i) { + expr * len_indicator = fvar_lenTester_map[freeVar][i]; + tout << mk_pp(len_indicator, m) << ": "; + bool effectiveInScope = (internal_variable_set.find(len_indicator) != internal_variable_set.end()); + tout << (effectiveInScope ? "in scope" : "NOT in scope"); + tout << std::endl; + } + ); + + int i = 0; + for (; i < lenTesterCount; ++i) { + expr * len_indicator_pre = fvar_lenTester_map[freeVar][i]; + // check whether this is in scope as well + if (internal_variable_set.find(len_indicator_pre) == internal_variable_set.end()) { + TRACE("str", tout << "length indicator " << mk_pp(len_indicator_pre, m) << " not in scope" << std::endl;); + continue; + } + + bool indicatorHasEqcValue = false; + expr * len_indicator_value = get_eqc_value(len_indicator_pre, indicatorHasEqcValue); + TRACE("str", tout << "length indicator " << mk_ismt2_pp(len_indicator_pre, m) << + " = " << mk_ismt2_pp(len_indicator_value, m) << std::endl;); + if (indicatorHasEqcValue) { + zstring len_pIndiStr; + u.str.is_string(len_indicator_value, len_pIndiStr); + if (len_pIndiStr != "more") { + effectiveLenInd = len_indicator_pre; + effectiveLenIndiStr = len_pIndiStr; + break; + } + } else { + if (lenTesterInCbEq != len_indicator_pre) { + TRACE("str", tout << "WARNING: length indicator " << mk_ismt2_pp(len_indicator_pre, m) + << " does not have an equivalence class value." + << " i = " << i << ", lenTesterCount = " << lenTesterCount << std::endl;); + if (i > 0) { + effectiveLenInd = fvar_lenTester_map[freeVar][i - 1]; + bool effectiveHasEqcValue; + expr * effective_eqc_value = get_eqc_value(effectiveLenInd, effectiveHasEqcValue); + bool effectiveInScope = (internal_variable_set.find(effectiveLenInd) != internal_variable_set.end()); + TRACE("str", tout << "checking effective length indicator " << mk_pp(effectiveLenInd, m) << ": " + << (effectiveInScope ? "in scope" : "NOT in scope") << ", "; + if (effectiveHasEqcValue) { + tout << "~= " << mk_pp(effective_eqc_value, m); + } else { + tout << "no eqc string constant"; + } + tout << std::endl;); + if (effectiveLenInd == lenTesterInCbEq) { + effectiveLenIndiStr = lenTesterValue; + } else { + if (effectiveHasEqcValue) { + u.str.is_string(effective_eqc_value, effectiveLenIndiStr); + } else { + NOT_IMPLEMENTED_YET(); + } + } + } + break; + } + // lenTesterInCbEq == len_indicator_pre + else { + if (lenTesterValue != "more") { + effectiveLenInd = len_indicator_pre; + effectiveLenIndiStr = lenTesterValue; + break; + } + } + } // !indicatorHasEqcValue + } // for (i : [0..lenTesterCount-1]) + if (effectiveLenIndiStr == "more" || effectiveLenIndiStr == "") { + TRACE("str", tout << "length is not fixed; generating length tester options for free var" << std::endl;); + expr_ref indicator(m); + unsigned int testNum = 0; + + TRACE("str", tout << "effectiveLenIndiStr = " << effectiveLenIndiStr + << ", i = " << i << ", lenTesterCount = " << lenTesterCount << "\n";); + + if (i == lenTesterCount) { + fvar_len_count_map[freeVar] = fvar_len_count_map[freeVar] + 1; + testNum = fvar_len_count_map[freeVar]; + indicator = mk_internal_lenTest_var(freeVar, testNum); + fvar_lenTester_map[freeVar].push_back(indicator); + lenTester_fvar_map.insert(indicator, freeVar); + } else { + indicator = fvar_lenTester_map[freeVar][i]; + refresh_theory_var(indicator); + testNum = i + 1; + } + expr * lenTestAssert = gen_len_test_options(freeVar, indicator, testNum); + SASSERT(lenTestAssert != NULL); + return lenTestAssert; + } else { + TRACE("str", tout << "length is fixed; generating models for free var" << std::endl;); + // length is fixed + expr * valueAssert = gen_free_var_options(freeVar, effectiveLenInd, effectiveLenIndiStr, NULL, zstring("")); + return valueAssert; + } + } // fVarLenCountMap.find(...) + + } // !UseBinarySearch + } + + void theory_str::get_concats_in_eqc(expr * n, std::set & concats) { + context & ctx = get_context(); + + expr * eqcNode = n; + do { + if (u.str.is_concat(to_app(eqcNode))) { + concats.insert(eqcNode); } - } // fVarLenCountMap.find(...) + eqcNode = get_eqc_next(eqcNode); + } while (eqcNode != n); + } - } // !UseBinarySearch -} + void theory_str::get_var_in_eqc(expr * n, std::set & varSet) { + context & ctx = get_context(); -void theory_str::get_concats_in_eqc(expr * n, std::set & concats) { - context & ctx = get_context(); + expr * eqcNode = n; + do { + if (variable_set.find(eqcNode) != variable_set.end()) { + varSet.insert(eqcNode); + } + eqcNode = get_eqc_next(eqcNode); + } while (eqcNode != n); + } - expr * eqcNode = n; - do { - if (u.str.is_concat(to_app(eqcNode))) { - concats.insert(eqcNode); - } - eqcNode = get_eqc_next(eqcNode); - } while (eqcNode != n); -} + bool cmpvarnames(expr * lhs, expr * rhs) { + symbol lhs_name = to_app(lhs)->get_decl()->get_name(); + symbol rhs_name = to_app(rhs)->get_decl()->get_name(); + return lhs_name.str() < rhs_name.str(); + } -void theory_str::get_var_in_eqc(expr * n, std::set & varSet) { - context & ctx = get_context(); + void theory_str::process_free_var(std::map & freeVar_map) { + context & ctx = get_context(); + ast_manager & m = get_manager(); - expr * eqcNode = n; - do { - if (variable_set.find(eqcNode) != variable_set.end()) { - varSet.insert(eqcNode); - } - eqcNode = get_eqc_next(eqcNode); - } while (eqcNode != n); -} + std::set eqcRepSet; + std::set leafVarSet; + std::map > aloneVars; -bool cmpvarnames(expr * lhs, expr * rhs) { - symbol lhs_name = to_app(lhs)->get_decl()->get_name(); - symbol rhs_name = to_app(rhs)->get_decl()->get_name(); - return lhs_name.str() < rhs_name.str(); -} + for (std::map::iterator fvIt = freeVar_map.begin(); fvIt != freeVar_map.end(); fvIt++) { + expr * freeVar = fvIt->first; + // skip all regular expression vars + if (regex_variable_set.find(freeVar) != regex_variable_set.end()) { + continue; + } -void theory_str::process_free_var(std::map & freeVar_map) { - context & ctx = get_context(); - ast_manager & m = get_manager(); - - std::set eqcRepSet; - std::set leafVarSet; - std::map > aloneVars; - - for (std::map::iterator fvIt = freeVar_map.begin(); fvIt != freeVar_map.end(); fvIt++) { - expr * freeVar = fvIt->first; - // skip all regular expression vars - if (regex_variable_set.find(freeVar) != regex_variable_set.end()) { - continue; - } - - // Iterate the EQC of freeVar, its eqc variable should not be in the eqcRepSet. - // If found, have to filter it out - std::set eqVarSet; - get_var_in_eqc(freeVar, eqVarSet); - bool duplicated = false; - expr * dupVar = NULL; - for (std::set::iterator itorEqv = eqVarSet.begin(); itorEqv != eqVarSet.end(); itorEqv++) { - if (eqcRepSet.find(*itorEqv) != eqcRepSet.end()) { - duplicated = true; - dupVar = *itorEqv; - break; - } - } - if (duplicated && dupVar != NULL) { - TRACE("str", tout << "Duplicated free variable found:" << mk_ismt2_pp(freeVar, m) - << " = " << mk_ismt2_pp(dupVar, m) << " (SKIP)" << std::endl;); - continue; - } else { - eqcRepSet.insert(freeVar); - } - } - - for (std::set::iterator fvIt = eqcRepSet.begin(); fvIt != eqcRepSet.end(); fvIt++) { - bool standAlone = true; - expr * freeVar = *fvIt; - // has length constraint initially - if (input_var_in_len.find(freeVar) != input_var_in_len.end()) { - standAlone = false; - } - // iterate parents - if (standAlone) { - // I hope this works! - enode * e_freeVar = ctx.get_enode(freeVar); - enode_vector::iterator it = e_freeVar->begin_parents(); - for (; it != e_freeVar->end_parents(); ++it) { - expr * parentAst = (*it)->get_owner(); - if (u.str.is_concat(to_app(parentAst))) { - standAlone = false; - break; - } - } - } - - if (standAlone) { - rational len_value; - bool len_value_exists = get_len_value(freeVar, len_value); - if (len_value_exists) { - leafVarSet.insert(freeVar); - } else { - aloneVars[-1].insert(freeVar); - } - } else { - leafVarSet.insert(freeVar); - } - } - - for(std::set::iterator itor1 = leafVarSet.begin(); - itor1 != leafVarSet.end(); ++itor1) { - expr * toAssert = gen_len_val_options_for_free_var(*itor1, NULL, ""); - // gen_len_val_options_for_free_var() can legally return NULL, - // as methods that it calls may assert their own axioms instead. - if (toAssert != NULL) { - assert_axiom(toAssert); - } - } - - for (std::map >::iterator mItor = aloneVars.begin(); - mItor != aloneVars.end(); ++mItor) { - std::set::iterator itor2 = mItor->second.begin(); - for(; itor2 != mItor->second.end(); ++itor2) { - expr * toAssert = gen_len_val_options_for_free_var(*itor2, NULL, ""); - // same deal with returning a NULL axiom here - if(toAssert != NULL) { - assert_axiom(toAssert); - } - } - } -} - -/* - * Collect all unroll functions - * and constant string in eqc of node n - */ -void theory_str::get_eqc_allUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet) { - constStr = NULL; - unrollFuncSet.clear(); - context & ctx = get_context(); - - expr * curr = n; - do { - if (u.str.is_string(to_app(curr))) { - constStr = curr; - } else if (u.re.is_unroll(to_app(curr))) { - if (unrollFuncSet.find(curr) == unrollFuncSet.end()) { - unrollFuncSet.insert(curr); + // Iterate the EQC of freeVar, its eqc variable should not be in the eqcRepSet. + // If found, have to filter it out + std::set eqVarSet; + get_var_in_eqc(freeVar, eqVarSet); + bool duplicated = false; + expr * dupVar = NULL; + for (std::set::iterator itorEqv = eqVarSet.begin(); itorEqv != eqVarSet.end(); itorEqv++) { + if (eqcRepSet.find(*itorEqv) != eqcRepSet.end()) { + duplicated = true; + dupVar = *itorEqv; + break; + } + } + if (duplicated && dupVar != NULL) { + TRACE("str", tout << "Duplicated free variable found:" << mk_ismt2_pp(freeVar, m) + << " = " << mk_ismt2_pp(dupVar, m) << " (SKIP)" << std::endl;); + continue; + } else { + eqcRepSet.insert(freeVar); } } - curr = get_eqc_next(curr); - } while (curr != n); -} -// Collect simple Unroll functions (whose core is Str2Reg) and constant strings in the EQC of n. -void theory_str::get_eqc_simpleUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet) { - constStr = NULL; - unrollFuncSet.clear(); - context & ctx = get_context(); + for (std::set::iterator fvIt = eqcRepSet.begin(); fvIt != eqcRepSet.end(); fvIt++) { + bool standAlone = true; + expr * freeVar = *fvIt; + // has length constraint initially + if (input_var_in_len.find(freeVar) != input_var_in_len.end()) { + standAlone = false; + } + // iterate parents + if (standAlone) { + // I hope this works! + enode * e_freeVar = ctx.get_enode(freeVar); + enode_vector::iterator it = e_freeVar->begin_parents(); + for (; it != e_freeVar->end_parents(); ++it) { + expr * parentAst = (*it)->get_owner(); + if (u.str.is_concat(to_app(parentAst))) { + standAlone = false; + break; + } + } + } - expr * curr = n; - do { - if (u.str.is_string(to_app(curr))) { - constStr = curr; - } else if (u.re.is_unroll(to_app(curr))) { - expr * core = to_app(curr)->get_arg(0); - if (u.re.is_to_re(to_app(core))) { - if (unrollFuncSet.find(curr) == unrollFuncSet.end()) { - unrollFuncSet.insert(curr); - } - } - } - curr = get_eqc_next(curr); - } while (curr != n); -} + if (standAlone) { + rational len_value; + bool len_value_exists = get_len_value(freeVar, len_value); + if (len_value_exists) { + leafVarSet.insert(freeVar); + } else { + aloneVars[-1].insert(freeVar); + } + } else { + leafVarSet.insert(freeVar); + } + } -void theory_str::init_model(model_generator & mg) { - //TRACE("str", tout << "initializing model" << std::endl; display(tout);); - m_factory = alloc(str_value_factory, get_manager(), get_family_id()); - mg.register_factory(m_factory); -} + for(std::set::iterator itor1 = leafVarSet.begin(); + itor1 != leafVarSet.end(); ++itor1) { + expr * toAssert = gen_len_val_options_for_free_var(*itor1, NULL, ""); + // gen_len_val_options_for_free_var() can legally return NULL, + // as methods that it calls may assert their own axioms instead. + if (toAssert != NULL) { + assert_axiom(toAssert); + } + } -/* - * Helper function for mk_value(). - * Attempts to resolve the expression 'n' to a string constant. - * Stronger than get_eqc_value() in that it will perform recursive descent - * through every subexpression and attempt to resolve those to concrete values as well. - * Returns the concrete value obtained from this process, - * guaranteed to satisfy m_strutil.is_string(), - * if one could be obtained, - * or else returns NULL if no concrete value was derived. - */ -app * theory_str::mk_value_helper(app * n) { - if (u.str.is_string(n)) { - return n; - } else if (u.str.is_concat(n)) { - // recursively call this function on each argument - SASSERT(n->get_num_args() == 2); - expr * a0 = n->get_arg(0); - expr * a1 = n->get_arg(1); - - app * a0_conststr = mk_value_helper(to_app(a0)); - app * a1_conststr = mk_value_helper(to_app(a1)); - - if (a0_conststr != NULL && a1_conststr != NULL) { - zstring a0_s, a1_s; - u.str.is_string(a0_conststr, a0_s); - u.str.is_string(a1_conststr, a1_s); - zstring result = a0_s + a1_s; - return to_app(mk_string(result)); + for (std::map >::iterator mItor = aloneVars.begin(); + mItor != aloneVars.end(); ++mItor) { + std::set::iterator itor2 = mItor->second.begin(); + for(; itor2 != mItor->second.end(); ++itor2) { + expr * toAssert = gen_len_val_options_for_free_var(*itor2, NULL, ""); + // same deal with returning a NULL axiom here + if(toAssert != NULL) { + assert_axiom(toAssert); + } + } } } - // fallback path - // try to find some constant string, anything, in the equivalence class of n - bool hasEqc = false; - expr * n_eqc = get_eqc_value(n, hasEqc); - if (hasEqc) { - return to_app(n_eqc); - } else { - return NULL; + + /* + * Collect all unroll functions + * and constant string in eqc of node n + */ + void theory_str::get_eqc_allUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet) { + constStr = NULL; + unrollFuncSet.clear(); + context & ctx = get_context(); + + expr * curr = n; + do { + if (u.str.is_string(to_app(curr))) { + constStr = curr; + } else if (u.re.is_unroll(to_app(curr))) { + if (unrollFuncSet.find(curr) == unrollFuncSet.end()) { + unrollFuncSet.insert(curr); + } + } + curr = get_eqc_next(curr); + } while (curr != n); } -} -model_value_proc * theory_str::mk_value(enode * n, model_generator & mg) { - TRACE("str", tout << "mk_value for: " << mk_ismt2_pp(n->get_owner(), get_manager()) << - " (sort " << mk_ismt2_pp(get_manager().get_sort(n->get_owner()), get_manager()) << ")" << std::endl;); - ast_manager & m = get_manager(); - context & ctx = get_context(); - app_ref owner(m); - owner = n->get_owner(); + // Collect simple Unroll functions (whose core is Str2Reg) and constant strings in the EQC of n. + void theory_str::get_eqc_simpleUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet) { + constStr = NULL; + unrollFuncSet.clear(); + context & ctx = get_context(); - // If the owner is not internalized, it doesn't have an enode associated. - SASSERT(ctx.e_internalized(owner)); - - app * val = mk_value_helper(owner); - if (val != NULL) { - return alloc(expr_wrapper_proc, val); - } else { - TRACE("str", tout << "WARNING: failed to find a concrete value, falling back" << std::endl;); - return alloc(expr_wrapper_proc, to_app(mk_string("**UNUSED**"))); + expr * curr = n; + do { + if (u.str.is_string(to_app(curr))) { + constStr = curr; + } else if (u.re.is_unroll(to_app(curr))) { + expr * core = to_app(curr)->get_arg(0); + if (u.re.is_to_re(to_app(core))) { + if (unrollFuncSet.find(curr) == unrollFuncSet.end()) { + unrollFuncSet.insert(curr); + } + } + } + curr = get_eqc_next(curr); + } while (curr != n); } -} -void theory_str::finalize_model(model_generator & mg) {} + void theory_str::init_model(model_generator & mg) { + //TRACE("str", tout << "initializing model" << std::endl; display(tout);); + m_factory = alloc(str_value_factory, get_manager(), get_family_id()); + mg.register_factory(m_factory); + } -void theory_str::display(std::ostream & out) const { - out << "TODO: theory_str display" << std::endl; -} + /* + * Helper function for mk_value(). + * Attempts to resolve the expression 'n' to a string constant. + * Stronger than get_eqc_value() in that it will perform recursive descent + * through every subexpression and attempt to resolve those to concrete values as well. + * Returns the concrete value obtained from this process, + * guaranteed to satisfy m_strutil.is_string(), + * if one could be obtained, + * or else returns NULL if no concrete value was derived. + */ + app * theory_str::mk_value_helper(app * n) { + if (u.str.is_string(n)) { + return n; + } else if (u.str.is_concat(n)) { + // recursively call this function on each argument + SASSERT(n->get_num_args() == 2); + expr * a0 = n->get_arg(0); + expr * a1 = n->get_arg(1); + + app * a0_conststr = mk_value_helper(to_app(a0)); + app * a1_conststr = mk_value_helper(to_app(a1)); + + if (a0_conststr != NULL && a1_conststr != NULL) { + zstring a0_s, a1_s; + u.str.is_string(a0_conststr, a0_s); + u.str.is_string(a1_conststr, a1_s); + zstring result = a0_s + a1_s; + return to_app(mk_string(result)); + } + } + // fallback path + // try to find some constant string, anything, in the equivalence class of n + bool hasEqc = false; + expr * n_eqc = get_eqc_value(n, hasEqc); + if (hasEqc) { + return to_app(n_eqc); + } else { + return NULL; + } + } + + model_value_proc * theory_str::mk_value(enode * n, model_generator & mg) { + TRACE("str", tout << "mk_value for: " << mk_ismt2_pp(n->get_owner(), get_manager()) << + " (sort " << mk_ismt2_pp(get_manager().get_sort(n->get_owner()), get_manager()) << ")" << std::endl;); + ast_manager & m = get_manager(); + context & ctx = get_context(); + app_ref owner(m); + owner = n->get_owner(); + + // If the owner is not internalized, it doesn't have an enode associated. + SASSERT(ctx.e_internalized(owner)); + + app * val = mk_value_helper(owner); + if (val != NULL) { + return alloc(expr_wrapper_proc, val); + } else { + TRACE("str", tout << "WARNING: failed to find a concrete value, falling back" << std::endl;); + return alloc(expr_wrapper_proc, to_app(mk_string("**UNUSED**"))); + } + } + + void theory_str::finalize_model(model_generator & mg) {} + + void theory_str::display(std::ostream & out) const { + out << "TODO: theory_str display" << std::endl; + } }; /* namespace smt */ From f904b033ad8f6db0eefe37ebfe40f1bd60b310e5 Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Fri, 5 May 2017 19:29:53 -0400 Subject: [PATCH 556/562] formatting theory_str.h --- src/smt/theory_str.h | 1110 +++++++++++++++++++++--------------------- 1 file changed, 555 insertions(+), 555 deletions(-) diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 7c2df9e12..2e6d96fa7 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -1,19 +1,19 @@ /*++ -Module Name: + Module Name: - theory_str.h + theory_str.h -Abstract: + Abstract: - String Theory Plugin + String Theory Plugin -Author: + Author: - Murphy Berzish and Yunhui Zheng + Murphy Berzish and Yunhui Zheng -Revision History: + Revision History: ---*/ + --*/ #ifndef _THEORY_STR_H_ #define _THEORY_STR_H_ @@ -33,619 +33,619 @@ Revision History: namespace smt { - typedef hashtable symbol_set; +typedef hashtable symbol_set; - class str_value_factory : public value_factory { - seq_util u; - symbol_set m_strings; - std::string delim; - unsigned m_next; - public: - str_value_factory(ast_manager & m, family_id fid) : - value_factory(m, fid), - u(m), delim("!"), m_next(0) {} - virtual ~str_value_factory() {} - virtual expr * get_some_value(sort * s) { - return u.str.mk_string(symbol("some value")); - } - virtual bool get_some_values(sort * s, expr_ref & v1, expr_ref & v2) { - v1 = u.str.mk_string(symbol("value 1")); - v2 = u.str.mk_string(symbol("value 2")); - return true; - } - virtual expr * get_fresh_value(sort * s) { - if (u.is_string(s)) { - while (true) { - std::ostringstream strm; - strm << delim << std::hex << (m_next++) << std::dec << delim; - symbol sym(strm.str().c_str()); - if (m_strings.contains(sym)) continue; - m_strings.insert(sym); - return u.str.mk_string(sym); - } +class str_value_factory : public value_factory { + seq_util u; + symbol_set m_strings; + std::string delim; + unsigned m_next; +public: + str_value_factory(ast_manager & m, family_id fid) : + value_factory(m, fid), + u(m), delim("!"), m_next(0) {} + virtual ~str_value_factory() {} + virtual expr * get_some_value(sort * s) { + return u.str.mk_string(symbol("some value")); + } + virtual bool get_some_values(sort * s, expr_ref & v1, expr_ref & v2) { + v1 = u.str.mk_string(symbol("value 1")); + v2 = u.str.mk_string(symbol("value 2")); + return true; + } + virtual expr * get_fresh_value(sort * s) { + if (u.is_string(s)) { + while (true) { + std::ostringstream strm; + strm << delim << std::hex << (m_next++) << std::dec << delim; + symbol sym(strm.str().c_str()); + if (m_strings.contains(sym)) continue; + m_strings.insert(sym); + return u.str.mk_string(sym); } - sort* seq = 0; - if (u.is_re(s, seq)) { - expr* v0 = get_fresh_value(seq); - return u.re.mk_to_re(v0); - } - TRACE("t_str", tout << "unexpected sort in get_fresh_value(): " << mk_pp(s, m_manager) << std::endl;); - UNREACHABLE(); return NULL; } - virtual void register_value(expr * n) { /* Ignore */ } - }; + sort* seq = 0; + if (u.is_re(s, seq)) { + expr* v0 = get_fresh_value(seq); + return u.re.mk_to_re(v0); + } + TRACE("t_str", tout << "unexpected sort in get_fresh_value(): " << mk_pp(s, m_manager) << std::endl;); + UNREACHABLE(); return NULL; + } + virtual void register_value(expr * n) { /* Ignore */ } +}; - // rather than modify obj_pair_map I inherit from it and add my own helper methods - class theory_str_contain_pair_bool_map_t : public obj_pair_map { - public: - expr * operator[](std::pair key) const { - expr * value; - bool found = this->find(key.first, key.second, value); - if (found) { - return value; +// rather than modify obj_pair_map I inherit from it and add my own helper methods +class theory_str_contain_pair_bool_map_t : public obj_pair_map { +public: + expr * operator[](std::pair key) const { + expr * value; + bool found = this->find(key.first, key.second, value); + if (found) { + return value; + } else { + TRACE("t_str", tout << "WARNING: lookup miss in contain_pair_bool_map!" << std::endl;); + return NULL; + } + } + + bool contains(std::pair key) const { + expr * unused; + return this->find(key.first, key.second, unused); + } +}; + +template +class binary_search_trail : public trail { + obj_map > & target; + expr * entry; +public: + binary_search_trail(obj_map > & target, expr * entry) : + target(target), entry(entry) {} + virtual ~binary_search_trail() {} + virtual void undo(Ctx & ctx) { + TRACE("t_str_binary_search", tout << "in binary_search_trail::undo()" << std::endl;); + if (target.contains(entry)) { + if (!target[entry].empty()) { + target[entry].pop_back(); } else { - TRACE("t_str", tout << "WARNING: lookup miss in contain_pair_bool_map!" << std::endl;); - return NULL; + TRACE("t_str_binary_search", tout << "WARNING: attempt to remove length tester from an empty stack" << std::endl;); } + } else { + TRACE("t_str_binary_search", tout << "WARNING: attempt to access length tester map via invalid key" << std::endl;); } + } +}; - bool contains(std::pair key) const { - expr * unused; - return this->find(key.first, key.second, unused); + +class nfa { +protected: + bool m_valid; + unsigned m_next_id; + + unsigned next_id() { + unsigned retval = m_next_id; + ++m_next_id; + return retval; + } + + unsigned m_start_state; + unsigned m_end_state; + + std::map > transition_map; + std::map > epsilon_map; + + void make_transition(unsigned start, char symbol, unsigned end) { + transition_map[start][symbol] = end; + } + + void make_epsilon_move(unsigned start, unsigned end) { + epsilon_map[start].insert(end); + } + + // Convert a regular expression to an e-NFA using Thompson's construction + void convert_re(expr * e, unsigned & start, unsigned & end, seq_util & u); + +public: + nfa(seq_util & u, expr * e) +: m_valid(true), m_next_id(0), m_start_state(0), m_end_state(0) { + convert_re(e, m_start_state, m_end_state, u); + } + + nfa() : m_valid(false), m_next_id(0), m_start_state(0), m_end_state(0) {} + + bool is_valid() const { + return m_valid; + } + + void epsilon_closure(unsigned start, std::set & closure); + + bool matches(zstring input); +}; + +class theory_str : public theory { + struct T_cut + { + int level; + std::map vars; + + T_cut() { + level = -100; } }; - template - class binary_search_trail : public trail { - obj_map > & target; - expr * entry; - public: - binary_search_trail(obj_map > & target, expr * entry) : - target(target), entry(entry) {} - virtual ~binary_search_trail() {} - virtual void undo(Ctx & ctx) { - TRACE("t_str_binary_search", tout << "in binary_search_trail::undo()" << std::endl;); - if (target.contains(entry)) { - if (!target[entry].empty()) { - target[entry].pop_back(); - } else { - TRACE("t_str_binary_search", tout << "WARNING: attempt to remove length tester from an empty stack" << std::endl;); - } - } else { - TRACE("t_str_binary_search", tout << "WARNING: attempt to access length tester map via invalid key" << std::endl;); - } + typedef trail_stack th_trail_stack; + typedef union_find th_union_find; + + typedef map, default_eq > rational_map; + struct zstring_hash_proc { + unsigned operator()(zstring const & s) const { + return string_hash(s.encode().c_str(), static_cast(s.length()), 17); } }; + typedef map > string_map; +protected: + theory_str_params const & m_params; - class nfa { - protected: - bool m_valid; - unsigned m_next_id; + /* + * Setting EagerStringConstantLengthAssertions to true allows some methods, + * in particular internalize_term(), to add + * length assertions about relevant string constants. + * Note that currently this should always be set to 'true', or else *no* length assertions + * will be made about string constants. + */ + bool opt_EagerStringConstantLengthAssertions; - unsigned next_id() { - unsigned retval = m_next_id; - ++m_next_id; - return retval; + /* + * If VerifyFinalCheckProgress is set to true, continuing after final check is invoked + * without asserting any new axioms is considered a bug and will throw an exception. + */ + bool opt_VerifyFinalCheckProgress; + + /* + * This constant controls how eagerly we expand unrolls in unbounded regex membership tests. + */ + int opt_LCMUnrollStep; + + /* + * If NoQuickReturn_IntegerTheory is set to true, + * integer theory integration checks that assert axioms + * will not return from the function after asserting their axioms. + * The default behaviour of Z3str2 is to set this to 'false'. This may be incorrect. + */ + bool opt_NoQuickReturn_IntegerTheory; + + /* + * If DisableIntegerTheoryIntegration is set to true, + * ALL calls to the integer theory integration methods + * (get_value, get_len_value, lower_bound, upper_bound) + * will ignore what the arithmetic solver believes about length terms, + * and will return no information. + * + * This reduces performance significantly, but can be useful to enable + * if it is suspected that string-integer integration, or the arithmetic solver itself, + * might have a bug. + * + * The default behaviour of Z3str2 is to set this to 'false'. + */ + bool opt_DisableIntegerTheoryIntegration; + + /* + * If DeferEQCConsistencyCheck is set to true, + * expensive calls to new_eq_check() will be deferred until final check, + * at which time the consistency of *all* string equivalence classes will be validated. + */ + bool opt_DeferEQCConsistencyCheck; + + /* + * If CheckVariableScope is set to true, + * pop_scope_eh() and final_check_eh() will run extra checks + * to determine whether the current assignment + * contains references to any internal variables that are no longer in scope. + */ + bool opt_CheckVariableScope; + + /* + * If ConcatOverlapAvoid is set to true, + * the check to simplify Concat = Concat in handle_equality() will + * avoid simplifying wrt. pairs of Concat terms that will immediately + * result in an overlap. (false = Z3str2 behaviour) + */ + bool opt_ConcatOverlapAvoid; + + bool search_started; + arith_util m_autil; + seq_util u; + int sLevel; + + bool finalCheckProgressIndicator; + + expr_ref_vector m_trail; // trail for generated terms + + str_value_factory * m_factory; + + // terms we couldn't go through set_up_axioms() with because they weren't internalized + expr_ref_vector m_delayed_axiom_setup_terms; + + ptr_vector m_basicstr_axiom_todo; + svector > m_str_eq_todo; + ptr_vector m_concat_axiom_todo; + ptr_vector m_string_constant_length_todo; + ptr_vector m_concat_eval_todo; + + // enode lists for library-aware/high-level string terms (e.g. substr, contains) + ptr_vector m_library_aware_axiom_todo; + + // hashtable of all exprs for which we've already set up term-specific axioms -- + // this prevents infinite recursive descent with respect to axioms that + // include an occurrence of the term for which axioms are being generated + obj_hashtable axiomatized_terms; + + int tmpStringVarCount; + int tmpXorVarCount; + int tmpLenTestVarCount; + int tmpValTestVarCount; + std::map, std::map > varForBreakConcat; + + bool avoidLoopCut; + bool loopDetected; + obj_map > cut_var_map; + expr_ref m_theoryStrOverlapAssumption_term; + + obj_hashtable variable_set; + obj_hashtable internal_variable_set; + obj_hashtable regex_variable_set; + std::map > internal_variable_scope_levels; + + obj_hashtable internal_lenTest_vars; + obj_hashtable internal_valTest_vars; + obj_hashtable internal_unrollTest_vars; + + obj_hashtable input_var_in_len; + + obj_map fvar_len_count_map; + std::map > fvar_lenTester_map; + obj_map lenTester_fvar_map; + + std::map > > > fvar_valueTester_map; + std::map valueTester_fvar_map; + + std::map val_range_map; + + // This can't be an expr_ref_vector because the constructor is wrong, + // we would need to modify the allocator so we pass in ast_manager + std::map, ptr_vector > > unroll_tries_map; + std::map unroll_var_map; + std::map, expr*> concat_eq_unroll_ast_map; + + expr_ref_vector contains_map; + + theory_str_contain_pair_bool_map_t contain_pair_bool_map; + //obj_map > contain_pair_idx_map; + std::map > > contain_pair_idx_map; + + std::map, expr*> regex_in_bool_map; + std::map > regex_in_var_reg_str_map; + + std::map regex_nfa_cache; // Regex term --> NFA + + char * char_set; + std::map charSetLookupTable; + int charSetSize; + + obj_pair_map concat_astNode_map; + + // all (str.to-int) and (int.to-str) terms + expr_ref_vector string_int_conversion_terms; + obj_hashtable string_int_axioms; + + // used when opt_FastLengthTesterCache is true + rational_map lengthTesterCache; + // used when opt_FastValueTesterCache is true + string_map valueTesterCache; + + string_map stringConstantCache; + unsigned long totalCacheAccessCount; + unsigned long cacheHitCount; + unsigned long cacheMissCount; + + // cache mapping each string S to Length(S) + obj_map length_ast_map; + + th_union_find m_find; + th_trail_stack m_trail_stack; + theory_var get_var(expr * n) const; + expr * get_eqc_next(expr * n); + app * get_ast(theory_var i); + + // binary search heuristic data + struct binary_search_info { + rational lowerBound; + rational midPoint; + rational upperBound; + rational windowSize; + + binary_search_info() : lowerBound(rational::zero()), midPoint(rational::zero()), + upperBound(rational::zero()), windowSize(rational::zero()) {} + binary_search_info(rational lower, rational mid, rational upper, rational windowSize) : + lowerBound(lower), midPoint(mid), upperBound(upper), windowSize(windowSize) {} + + void calculate_midpoint() { + midPoint = floor(lowerBound + ((upperBound - lowerBound) / rational(2)) ); } - - unsigned m_start_state; - unsigned m_end_state; - - std::map > transition_map; - std::map > epsilon_map; - - void make_transition(unsigned start, char symbol, unsigned end) { - transition_map[start][symbol] = end; - } - - void make_epsilon_move(unsigned start, unsigned end) { - epsilon_map[start].insert(end); - } - - // Convert a regular expression to an e-NFA using Thompson's construction - void convert_re(expr * e, unsigned & start, unsigned & end, seq_util & u); - - public: - nfa(seq_util & u, expr * e) - : m_valid(true), m_next_id(0), m_start_state(0), m_end_state(0) { - convert_re(e, m_start_state, m_end_state, u); - } - - nfa() : m_valid(false), m_next_id(0), m_start_state(0), m_end_state(0) {} - - bool is_valid() const { - return m_valid; - } - - void epsilon_closure(unsigned start, std::set & closure); - - bool matches(zstring input); }; + // maps a free string var to a stack of active length testers. + // can use binary_search_trail to record changes to this object + obj_map > binary_search_len_tester_stack; + // maps a length tester var to the *active* search window + obj_map binary_search_len_tester_info; + // maps a free string var to the first length tester to be (re)used + obj_map binary_search_starting_len_tester; + // maps a length tester to the next length tester to be (re)used if the split is "low" + obj_map binary_search_next_var_low; + // maps a length tester to the next length tester to be (re)used if the split is "high" + obj_map binary_search_next_var_high; - class theory_str : public theory { - struct T_cut - { - int level; - std::map vars; + // finite model finding data + // maps a finite model tester var to a list of variables that will be tested + obj_map > finite_model_test_varlists; +protected: + void assert_axiom(expr * e); + void assert_implication(expr * premise, expr * conclusion); + expr * rewrite_implication(expr * premise, expr * conclusion); - T_cut() { - level = -100; - } - }; + expr * mk_string(zstring const& str); + expr * mk_string(const char * str); - typedef trail_stack th_trail_stack; - typedef union_find th_union_find; + app * mk_strlen(expr * e); + expr * mk_concat(expr * n1, expr * n2); + expr * mk_concat_const_str(expr * n1, expr * n2); + app * mk_contains(expr * haystack, expr * needle); + app * mk_indexof(expr * haystack, expr * needle); - typedef map, default_eq > rational_map; - struct zstring_hash_proc { - unsigned operator()(zstring const & s) const { - return string_hash(s.encode().c_str(), static_cast(s.length()), 17); - } - }; - typedef map > string_map; + literal mk_literal(expr* _e); + app * mk_int(int n); + app * mk_int(rational & q); - protected: - theory_str_params const & m_params; + void check_and_init_cut_var(expr * node); + void add_cut_info_one_node(expr * baseNode, int slevel, expr * node); + void add_cut_info_merge(expr * destNode, int slevel, expr * srcNode); + bool has_self_cut(expr * n1, expr * n2); - /* - * Setting EagerStringConstantLengthAssertions to true allows some methods, - * in particular internalize_term(), to add - * length assertions about relevant string constants. - * Note that currently this should always be set to 'true', or else *no* length assertions - * will be made about string constants. - */ - bool opt_EagerStringConstantLengthAssertions; + // for ConcatOverlapAvoid + bool will_result_in_overlap(expr * lhs, expr * rhs); - /* - * If VerifyFinalCheckProgress is set to true, continuing after final check is invoked - * without asserting any new axioms is considered a bug and will throw an exception. - */ - bool opt_VerifyFinalCheckProgress; + void track_variable_scope(expr * var); + app * mk_str_var(std::string name); + app * mk_int_var(std::string name); + app * mk_nonempty_str_var(); + app * mk_internal_xor_var(); + expr * mk_internal_valTest_var(expr * node, int len, int vTries); + app * mk_regex_rep_var(); + app * mk_unroll_bound_var(); + app * mk_unroll_test_var(); + void add_nonempty_constraint(expr * s); - /* - * This constant controls how eagerly we expand unrolls in unbounded regex membership tests. - */ - int opt_LCMUnrollStep; + void instantiate_concat_axiom(enode * cat); + void try_eval_concat(enode * cat); + void instantiate_basic_string_axioms(enode * str); + void instantiate_str_eq_length_axiom(enode * lhs, enode * rhs); - /* - * If NoQuickReturn_IntegerTheory is set to true, - * integer theory integration checks that assert axioms - * will not return from the function after asserting their axioms. - * The default behaviour of Z3str2 is to set this to 'false'. This may be incorrect. - */ - bool opt_NoQuickReturn_IntegerTheory; + void instantiate_axiom_CharAt(enode * e); + void instantiate_axiom_prefixof(enode * e); + void instantiate_axiom_suffixof(enode * e); + void instantiate_axiom_Contains(enode * e); + void instantiate_axiom_Indexof(enode * e); + void instantiate_axiom_Indexof2(enode * e); + void instantiate_axiom_LastIndexof(enode * e); + void instantiate_axiom_Substr(enode * e); + void instantiate_axiom_Replace(enode * e); + void instantiate_axiom_str_to_int(enode * e); + void instantiate_axiom_int_to_str(enode * e); - /* - * If DisableIntegerTheoryIntegration is set to true, - * ALL calls to the integer theory integration methods - * (get_value, get_len_value, lower_bound, upper_bound) - * will ignore what the arithmetic solver believes about length terms, - * and will return no information. - * - * This reduces performance significantly, but can be useful to enable - * if it is suspected that string-integer integration, or the arithmetic solver itself, - * might have a bug. - * - * The default behaviour of Z3str2 is to set this to 'false'. - */ - bool opt_DisableIntegerTheoryIntegration; + expr * mk_RegexIn(expr * str, expr * regexp); + void instantiate_axiom_RegexIn(enode * e); + app * mk_unroll(expr * n, expr * bound); - /* - * If DeferEQCConsistencyCheck is set to true, - * expensive calls to new_eq_check() will be deferred until final check, - * at which time the consistency of *all* string equivalence classes will be validated. - */ - bool opt_DeferEQCConsistencyCheck; + void process_unroll_eq_const_str(expr * unrollFunc, expr * constStr); + void unroll_str2reg_constStr(expr * unrollFunc, expr * eqConstStr); + void process_concat_eq_unroll(expr * concat, expr * unroll); - /* - * If CheckVariableScope is set to true, - * pop_scope_eh() and final_check_eh() will run extra checks - * to determine whether the current assignment - * contains references to any internal variables that are no longer in scope. - */ - bool opt_CheckVariableScope; + void set_up_axioms(expr * ex); + void handle_equality(expr * lhs, expr * rhs); - /* - * If ConcatOverlapAvoid is set to true, - * the check to simplify Concat = Concat in handle_equality() will - * avoid simplifying wrt. pairs of Concat terms that will immediately - * result in an overlap. (false = Z3str2 behaviour) - */ - bool opt_ConcatOverlapAvoid; + app * mk_value_helper(app * n); + expr * get_eqc_value(expr * n, bool & hasEqcValue); + expr * z3str2_get_eqc_value(expr * n , bool & hasEqcValue); + bool in_same_eqc(expr * n1, expr * n2); + expr * collect_eq_nodes(expr * n, expr_ref_vector & eqcSet); - bool search_started; - arith_util m_autil; - seq_util u; - int sLevel; + bool get_value(expr* e, rational& val) const; + bool get_len_value(expr* e, rational& val); + bool lower_bound(expr* _e, rational& lo); + bool upper_bound(expr* _e, rational& hi); - bool finalCheckProgressIndicator; - - expr_ref_vector m_trail; // trail for generated terms - - str_value_factory * m_factory; - - // terms we couldn't go through set_up_axioms() with because they weren't internalized - expr_ref_vector m_delayed_axiom_setup_terms; - - ptr_vector m_basicstr_axiom_todo; - svector > m_str_eq_todo; - ptr_vector m_concat_axiom_todo; - ptr_vector m_string_constant_length_todo; - ptr_vector m_concat_eval_todo; - - // enode lists for library-aware/high-level string terms (e.g. substr, contains) - ptr_vector m_library_aware_axiom_todo; - - // hashtable of all exprs for which we've already set up term-specific axioms -- - // this prevents infinite recursive descent with respect to axioms that - // include an occurrence of the term for which axioms are being generated - obj_hashtable axiomatized_terms; - - int tmpStringVarCount; - int tmpXorVarCount; - int tmpLenTestVarCount; - int tmpValTestVarCount; - std::map, std::map > varForBreakConcat; - - bool avoidLoopCut; - bool loopDetected; - obj_map > cut_var_map; - expr_ref m_theoryStrOverlapAssumption_term; - - obj_hashtable variable_set; - obj_hashtable internal_variable_set; - obj_hashtable regex_variable_set; - std::map > internal_variable_scope_levels; - - obj_hashtable internal_lenTest_vars; - obj_hashtable internal_valTest_vars; - obj_hashtable internal_unrollTest_vars; - - obj_hashtable input_var_in_len; - - obj_map fvar_len_count_map; - std::map > fvar_lenTester_map; - obj_map lenTester_fvar_map; - - std::map > > > fvar_valueTester_map; - std::map valueTester_fvar_map; - - std::map val_range_map; - - // This can't be an expr_ref_vector because the constructor is wrong, - // we would need to modify the allocator so we pass in ast_manager - std::map, ptr_vector > > unroll_tries_map; - std::map unroll_var_map; - std::map, expr*> concat_eq_unroll_ast_map; - - expr_ref_vector contains_map; - - theory_str_contain_pair_bool_map_t contain_pair_bool_map; - //obj_map > contain_pair_idx_map; - std::map > > contain_pair_idx_map; - - std::map, expr*> regex_in_bool_map; - std::map > regex_in_var_reg_str_map; - - std::map regex_nfa_cache; // Regex term --> NFA - - char * char_set; - std::map charSetLookupTable; - int charSetSize; - - obj_pair_map concat_astNode_map; - - // all (str.to-int) and (int.to-str) terms - expr_ref_vector string_int_conversion_terms; - obj_hashtable string_int_axioms; - - // used when opt_FastLengthTesterCache is true - rational_map lengthTesterCache; - // used when opt_FastValueTesterCache is true - string_map valueTesterCache; - - string_map stringConstantCache; - unsigned long totalCacheAccessCount; - unsigned long cacheHitCount; - unsigned long cacheMissCount; - - // cache mapping each string S to Length(S) - obj_map length_ast_map; - - th_union_find m_find; - th_trail_stack m_trail_stack; - theory_var get_var(expr * n) const; - expr * get_eqc_next(expr * n); - app * get_ast(theory_var i); - - // binary search heuristic data - struct binary_search_info { - rational lowerBound; - rational midPoint; - rational upperBound; - rational windowSize; - - binary_search_info() : lowerBound(rational::zero()), midPoint(rational::zero()), - upperBound(rational::zero()), windowSize(rational::zero()) {} - binary_search_info(rational lower, rational mid, rational upper, rational windowSize) : - lowerBound(lower), midPoint(mid), upperBound(upper), windowSize(windowSize) {} - - void calculate_midpoint() { - midPoint = floor(lowerBound + ((upperBound - lowerBound) / rational(2)) ); - } - }; - // maps a free string var to a stack of active length testers. - // can use binary_search_trail to record changes to this object - obj_map > binary_search_len_tester_stack; - // maps a length tester var to the *active* search window - obj_map binary_search_len_tester_info; - // maps a free string var to the first length tester to be (re)used - obj_map binary_search_starting_len_tester; - // maps a length tester to the next length tester to be (re)used if the split is "low" - obj_map binary_search_next_var_low; - // maps a length tester to the next length tester to be (re)used if the split is "high" - obj_map binary_search_next_var_high; - - // finite model finding data - // maps a finite model tester var to a list of variables that will be tested - obj_map > finite_model_test_varlists; - protected: - void assert_axiom(expr * e); - void assert_implication(expr * premise, expr * conclusion); - expr * rewrite_implication(expr * premise, expr * conclusion); - - expr * mk_string(zstring const& str); - expr * mk_string(const char * str); - - app * mk_strlen(expr * e); - expr * mk_concat(expr * n1, expr * n2); - expr * mk_concat_const_str(expr * n1, expr * n2); - app * mk_contains(expr * haystack, expr * needle); - app * mk_indexof(expr * haystack, expr * needle); - - literal mk_literal(expr* _e); - app * mk_int(int n); - app * mk_int(rational & q); - - void check_and_init_cut_var(expr * node); - void add_cut_info_one_node(expr * baseNode, int slevel, expr * node); - void add_cut_info_merge(expr * destNode, int slevel, expr * srcNode); - bool has_self_cut(expr * n1, expr * n2); - - // for ConcatOverlapAvoid - bool will_result_in_overlap(expr * lhs, expr * rhs); - - void track_variable_scope(expr * var); - app * mk_str_var(std::string name); - app * mk_int_var(std::string name); - app * mk_nonempty_str_var(); - app * mk_internal_xor_var(); - expr * mk_internal_valTest_var(expr * node, int len, int vTries); - app * mk_regex_rep_var(); - app * mk_unroll_bound_var(); - app * mk_unroll_test_var(); - void add_nonempty_constraint(expr * s); - - void instantiate_concat_axiom(enode * cat); - void try_eval_concat(enode * cat); - void instantiate_basic_string_axioms(enode * str); - void instantiate_str_eq_length_axiom(enode * lhs, enode * rhs); - - void instantiate_axiom_CharAt(enode * e); - void instantiate_axiom_prefixof(enode * e); - void instantiate_axiom_suffixof(enode * e); - void instantiate_axiom_Contains(enode * e); - void instantiate_axiom_Indexof(enode * e); - void instantiate_axiom_Indexof2(enode * e); - void instantiate_axiom_LastIndexof(enode * e); - void instantiate_axiom_Substr(enode * e); - void instantiate_axiom_Replace(enode * e); - void instantiate_axiom_str_to_int(enode * e); - void instantiate_axiom_int_to_str(enode * e); - - expr * mk_RegexIn(expr * str, expr * regexp); - void instantiate_axiom_RegexIn(enode * e); - app * mk_unroll(expr * n, expr * bound); - - void process_unroll_eq_const_str(expr * unrollFunc, expr * constStr); - void unroll_str2reg_constStr(expr * unrollFunc, expr * eqConstStr); - void process_concat_eq_unroll(expr * concat, expr * unroll); - - void set_up_axioms(expr * ex); - void handle_equality(expr * lhs, expr * rhs); - - app * mk_value_helper(app * n); - expr * get_eqc_value(expr * n, bool & hasEqcValue); - expr * z3str2_get_eqc_value(expr * n , bool & hasEqcValue); - bool in_same_eqc(expr * n1, expr * n2); - expr * collect_eq_nodes(expr * n, expr_ref_vector & eqcSet); - - bool get_value(expr* e, rational& val) const; - bool get_len_value(expr* e, rational& val); - bool lower_bound(expr* _e, rational& lo); - bool upper_bound(expr* _e, rational& hi); - - bool can_two_nodes_eq(expr * n1, expr * n2); - bool can_concat_eq_str(expr * concat, zstring& str); - bool can_concat_eq_concat(expr * concat1, expr * concat2); - bool check_concat_len_in_eqc(expr * concat); - bool check_length_consistency(expr * n1, expr * n2); - bool check_length_const_string(expr * n1, expr * constStr); - bool check_length_eq_var_concat(expr * n1, expr * n2); - bool check_length_concat_concat(expr * n1, expr * n2); - bool check_length_concat_var(expr * concat, expr * var); - bool check_length_var_var(expr * var1, expr * var2); - void check_contain_in_new_eq(expr * n1, expr * n2); - void check_contain_by_eqc_val(expr * varNode, expr * constNode); - void check_contain_by_substr(expr * varNode, expr_ref_vector & willEqClass); - void check_contain_by_eq_nodes(expr * n1, expr * n2); - bool in_contain_idx_map(expr * n); - void compute_contains(std::map & varAliasMap, - std::map & concatAliasMap, std::map & varConstMap, - std::map & concatConstMap, std::map > & varEqConcatMap); - expr * dealias_node(expr * node, std::map & varAliasMap, std::map & concatAliasMap); - void get_grounded_concats(expr* node, std::map & varAliasMap, - std::map & concatAliasMap, std::map & varConstMap, - std::map & concatConstMap, std::map > & varEqConcatMap, - std::map, std::set > > & groundedMap); - void print_grounded_concat(expr * node, std::map, std::set > > & groundedMap); - void check_subsequence(expr* str, expr* strDeAlias, expr* subStr, expr* subStrDeAlias, expr* boolVar, + bool can_two_nodes_eq(expr * n1, expr * n2); + bool can_concat_eq_str(expr * concat, zstring& str); + bool can_concat_eq_concat(expr * concat1, expr * concat2); + bool check_concat_len_in_eqc(expr * concat); + bool check_length_consistency(expr * n1, expr * n2); + bool check_length_const_string(expr * n1, expr * constStr); + bool check_length_eq_var_concat(expr * n1, expr * n2); + bool check_length_concat_concat(expr * n1, expr * n2); + bool check_length_concat_var(expr * concat, expr * var); + bool check_length_var_var(expr * var1, expr * var2); + void check_contain_in_new_eq(expr * n1, expr * n2); + void check_contain_by_eqc_val(expr * varNode, expr * constNode); + void check_contain_by_substr(expr * varNode, expr_ref_vector & willEqClass); + void check_contain_by_eq_nodes(expr * n1, expr * n2); + bool in_contain_idx_map(expr * n); + void compute_contains(std::map & varAliasMap, + std::map & concatAliasMap, std::map & varConstMap, + std::map & concatConstMap, std::map > & varEqConcatMap); + expr * dealias_node(expr * node, std::map & varAliasMap, std::map & concatAliasMap); + void get_grounded_concats(expr* node, std::map & varAliasMap, + std::map & concatAliasMap, std::map & varConstMap, + std::map & concatConstMap, std::map > & varEqConcatMap, std::map, std::set > > & groundedMap); - bool is_partial_in_grounded_concat(const std::vector & strVec, const std::vector & subStrVec); + void print_grounded_concat(expr * node, std::map, std::set > > & groundedMap); + void check_subsequence(expr* str, expr* strDeAlias, expr* subStr, expr* subStrDeAlias, expr* boolVar, + std::map, std::set > > & groundedMap); + bool is_partial_in_grounded_concat(const std::vector & strVec, const std::vector & subStrVec); - void get_nodes_in_concat(expr * node, ptr_vector & nodeList); - expr * simplify_concat(expr * node); + void get_nodes_in_concat(expr * node, ptr_vector & nodeList); + expr * simplify_concat(expr * node); - void simplify_parent(expr * nn, expr * eq_str); + void simplify_parent(expr * nn, expr * eq_str); - void simplify_concat_equality(expr * lhs, expr * rhs); - void solve_concat_eq_str(expr * concat, expr * str); + void simplify_concat_equality(expr * lhs, expr * rhs); + void solve_concat_eq_str(expr * concat, expr * str); - void infer_len_concat_equality(expr * nn1, expr * nn2); - bool infer_len_concat(expr * n, rational & nLen); - void infer_len_concat_arg(expr * n, rational len); + void infer_len_concat_equality(expr * nn1, expr * nn2); + bool infer_len_concat(expr * n, rational & nLen); + void infer_len_concat_arg(expr * n, rational len); - bool is_concat_eq_type1(expr * concatAst1, expr * concatAst2); - bool is_concat_eq_type2(expr * concatAst1, expr * concatAst2); - bool is_concat_eq_type3(expr * concatAst1, expr * concatAst2); - bool is_concat_eq_type4(expr * concatAst1, expr * concatAst2); - bool is_concat_eq_type5(expr * concatAst1, expr * concatAst2); - bool is_concat_eq_type6(expr * concatAst1, expr * concatAst2); + bool is_concat_eq_type1(expr * concatAst1, expr * concatAst2); + bool is_concat_eq_type2(expr * concatAst1, expr * concatAst2); + bool is_concat_eq_type3(expr * concatAst1, expr * concatAst2); + bool is_concat_eq_type4(expr * concatAst1, expr * concatAst2); + bool is_concat_eq_type5(expr * concatAst1, expr * concatAst2); + bool is_concat_eq_type6(expr * concatAst1, expr * concatAst2); - void process_concat_eq_type1(expr * concatAst1, expr * concatAst2); - void process_concat_eq_type2(expr * concatAst1, expr * concatAst2); - void process_concat_eq_type3(expr * concatAst1, expr * concatAst2); - void process_concat_eq_type4(expr * concatAst1, expr * concatAst2); - void process_concat_eq_type5(expr * concatAst1, expr * concatAst2); - void process_concat_eq_type6(expr * concatAst1, expr * concatAst2); + void process_concat_eq_type1(expr * concatAst1, expr * concatAst2); + void process_concat_eq_type2(expr * concatAst1, expr * concatAst2); + void process_concat_eq_type3(expr * concatAst1, expr * concatAst2); + void process_concat_eq_type4(expr * concatAst1, expr * concatAst2); + void process_concat_eq_type5(expr * concatAst1, expr * concatAst2); + void process_concat_eq_type6(expr * concatAst1, expr * concatAst2); - void print_cut_var(expr * node, std::ofstream & xout); + void print_cut_var(expr * node, std::ofstream & xout); - void generate_mutual_exclusion(expr_ref_vector & exprs); - void add_theory_aware_branching_info(expr * term, double priority, lbool phase); + void generate_mutual_exclusion(expr_ref_vector & exprs); + void add_theory_aware_branching_info(expr * term, double priority, lbool phase); - bool new_eq_check(expr * lhs, expr * rhs); - void group_terms_by_eqc(expr * n, std::set & concats, std::set & vars, std::set & consts); + bool new_eq_check(expr * lhs, expr * rhs); + void group_terms_by_eqc(expr * n, std::set & concats, std::set & vars, std::set & consts); - int ctx_dep_analysis(std::map & strVarMap, std::map & freeVarMap, - std::map > & unrollGroupMap, std::map > & var_eq_concat_map); - void trace_ctx_dep(std::ofstream & tout, - std::map & aliasIndexMap, - std::map & var_eq_constStr_map, - std::map > & var_eq_concat_map, - std::map > & var_eq_unroll_map, - std::map & concat_eq_constStr_map, - std::map > & concat_eq_concat_map, - std::map > & unrollGroupMap); + int ctx_dep_analysis(std::map & strVarMap, std::map & freeVarMap, + std::map > & unrollGroupMap, std::map > & var_eq_concat_map); + void trace_ctx_dep(std::ofstream & tout, + std::map & aliasIndexMap, + std::map & var_eq_constStr_map, + std::map > & var_eq_concat_map, + std::map > & var_eq_unroll_map, + std::map & concat_eq_constStr_map, + std::map > & concat_eq_concat_map, + std::map > & unrollGroupMap); - void classify_ast_by_type(expr * node, std::map & varMap, - std::map & concatMap, std::map & unrollMap); - void classify_ast_by_type_in_positive_context(std::map & varMap, - std::map & concatMap, std::map & unrollMap); + void classify_ast_by_type(expr * node, std::map & varMap, + std::map & concatMap, std::map & unrollMap); + void classify_ast_by_type_in_positive_context(std::map & varMap, + std::map & concatMap, std::map & unrollMap); - expr * mk_internal_lenTest_var(expr * node, int lTries); - expr * gen_len_val_options_for_free_var(expr * freeVar, expr * lenTesterInCbEq, zstring lenTesterValue); - void process_free_var(std::map & freeVar_map); - expr * gen_len_test_options(expr * freeVar, expr * indicator, int tries); - expr * gen_free_var_options(expr * freeVar, expr * len_indicator, - zstring len_valueStr, expr * valTesterInCbEq, zstring valTesterValueStr); - expr * gen_val_options(expr * freeVar, expr * len_indicator, expr * val_indicator, - zstring lenStr, int tries); - void print_value_tester_list(svector > & testerList); - bool get_next_val_encode(int_vector & base, int_vector & next); - zstring gen_val_string(int len, int_vector & encoding); + expr * mk_internal_lenTest_var(expr * node, int lTries); + expr * gen_len_val_options_for_free_var(expr * freeVar, expr * lenTesterInCbEq, zstring lenTesterValue); + void process_free_var(std::map & freeVar_map); + expr * gen_len_test_options(expr * freeVar, expr * indicator, int tries); + expr * gen_free_var_options(expr * freeVar, expr * len_indicator, + zstring len_valueStr, expr * valTesterInCbEq, zstring valTesterValueStr); + expr * gen_val_options(expr * freeVar, expr * len_indicator, expr * val_indicator, + zstring lenStr, int tries); + void print_value_tester_list(svector > & testerList); + bool get_next_val_encode(int_vector & base, int_vector & next); + zstring gen_val_string(int len, int_vector & encoding); - // binary search heuristic - expr * binary_search_length_test(expr * freeVar, expr * previousLenTester, zstring previousLenTesterValue); - expr_ref binary_search_case_split(expr * freeVar, expr * tester, binary_search_info & bounds, literal_vector & case_split_lits); + // binary search heuristic + expr * binary_search_length_test(expr * freeVar, expr * previousLenTester, zstring previousLenTesterValue); + expr_ref binary_search_case_split(expr * freeVar, expr * tester, binary_search_info & bounds, literal_vector & case_split_lits); - bool free_var_attempt(expr * nn1, expr * nn2); - void more_len_tests(expr * lenTester, zstring lenTesterValue); - void more_value_tests(expr * valTester, zstring valTesterValue); + bool free_var_attempt(expr * nn1, expr * nn2); + void more_len_tests(expr * lenTester, zstring lenTesterValue); + void more_value_tests(expr * valTester, zstring valTesterValue); - expr * get_alias_index_ast(std::map & aliasIndexMap, expr * node); - expr * getMostLeftNodeInConcat(expr * node); - expr * getMostRightNodeInConcat(expr * node); - void get_var_in_eqc(expr * n, std::set & varSet); - void get_concats_in_eqc(expr * n, std::set & concats); - void get_const_str_asts_in_node(expr * node, expr_ref_vector & constList); - expr * eval_concat(expr * n1, expr * n2); + expr * get_alias_index_ast(std::map & aliasIndexMap, expr * node); + expr * getMostLeftNodeInConcat(expr * node); + expr * getMostRightNodeInConcat(expr * node); + void get_var_in_eqc(expr * n, std::set & varSet); + void get_concats_in_eqc(expr * n, std::set & concats); + void get_const_str_asts_in_node(expr * node, expr_ref_vector & constList); + expr * eval_concat(expr * n1, expr * n2); - bool finalcheck_str2int(app * a); - bool finalcheck_int2str(app * a); + bool finalcheck_str2int(app * a); + bool finalcheck_int2str(app * a); - // strRegex + // strRegex - void get_eqc_allUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet); - void get_eqc_simpleUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet); - void gen_assign_unroll_reg(std::set & unrolls); - expr * gen_assign_unroll_Str2Reg(expr * n, std::set & unrolls); - expr * gen_unroll_conditional_options(expr * var, std::set & unrolls, zstring lcmStr); - expr * gen_unroll_assign(expr * var, zstring lcmStr, expr * testerVar, int l, int h); - void reduce_virtual_regex_in(expr * var, expr * regex, expr_ref_vector & items); - void check_regex_in(expr * nn1, expr * nn2); - zstring get_std_regex_str(expr * r); + void get_eqc_allUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet); + void get_eqc_simpleUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet); + void gen_assign_unroll_reg(std::set & unrolls); + expr * gen_assign_unroll_Str2Reg(expr * n, std::set & unrolls); + expr * gen_unroll_conditional_options(expr * var, std::set & unrolls, zstring lcmStr); + expr * gen_unroll_assign(expr * var, zstring lcmStr, expr * testerVar, int l, int h); + void reduce_virtual_regex_in(expr * var, expr * regex, expr_ref_vector & items); + void check_regex_in(expr * nn1, expr * nn2); + zstring get_std_regex_str(expr * r); - void dump_assignments(); - void initialize_charset(); + void dump_assignments(); + void initialize_charset(); - void check_variable_scope(); - void recursive_check_variable_scope(expr * ex); + void check_variable_scope(); + void recursive_check_variable_scope(expr * ex); - void collect_var_concat(expr * node, std::set & varSet, std::set & concatSet); - bool propagate_length(std::set & varSet, std::set & concatSet, std::map & exprLenMap); - void get_unique_non_concat_nodes(expr * node, std::set & argSet); - bool propagate_length_within_eqc(expr * var); + void collect_var_concat(expr * node, std::set & varSet, std::set & concatSet); + bool propagate_length(std::set & varSet, std::set & concatSet, std::map & exprLenMap); + void get_unique_non_concat_nodes(expr * node, std::set & argSet); + bool propagate_length_within_eqc(expr * var); - // TESTING - void refresh_theory_var(expr * e); + // TESTING + void refresh_theory_var(expr * e); - expr_ref set_up_finite_model_test(expr * lhs, expr * rhs); - void finite_model_test(expr * v, expr * c); + expr_ref set_up_finite_model_test(expr * lhs, expr * rhs); + void finite_model_test(expr * v, expr * c); - public: - theory_str(ast_manager & m, theory_str_params const & params); - virtual ~theory_str(); +public: + theory_str(ast_manager & m, theory_str_params const & params); + virtual ~theory_str(); - virtual char const * get_name() const { return "seq"; } - virtual void display(std::ostream & out) const; + virtual char const * get_name() const { return "seq"; } + virtual void display(std::ostream & out) const; - bool overlapping_variables_detected() const { return loopDetected; } + bool overlapping_variables_detected() const { return loopDetected; } - th_trail_stack& get_trail_stack() { return m_trail_stack; } - void merge_eh(theory_var, theory_var, theory_var v1, theory_var v2) {} - void after_merge_eh(theory_var r1, theory_var r2, theory_var v1, theory_var v2) { } - void unmerge_eh(theory_var v1, theory_var v2) {} - protected: - virtual bool internalize_atom(app * atom, bool gate_ctx); - virtual bool internalize_term(app * term); - virtual enode* ensure_enode(expr* e); - virtual theory_var mk_var(enode * n); + th_trail_stack& get_trail_stack() { return m_trail_stack; } + void merge_eh(theory_var, theory_var, theory_var v1, theory_var v2) {} + void after_merge_eh(theory_var r1, theory_var r2, theory_var v1, theory_var v2) { } + void unmerge_eh(theory_var v1, theory_var v2) {} +protected: + virtual bool internalize_atom(app * atom, bool gate_ctx); + virtual bool internalize_term(app * term); + virtual enode* ensure_enode(expr* e); + virtual theory_var mk_var(enode * n); - virtual void new_eq_eh(theory_var, theory_var); - virtual void new_diseq_eh(theory_var, theory_var); + virtual void new_eq_eh(theory_var, theory_var); + virtual void new_diseq_eh(theory_var, theory_var); - virtual theory* mk_fresh(context*) { return alloc(theory_str, get_manager(), m_params); } - virtual void init_search_eh(); - virtual void add_theory_assumptions(expr_ref_vector & assumptions); - virtual lbool validate_unsat_core(expr_ref_vector & unsat_core); - virtual void relevant_eh(app * n); - virtual void assign_eh(bool_var v, bool is_true); - virtual void push_scope_eh(); - virtual void pop_scope_eh(unsigned num_scopes); - virtual void reset_eh(); + virtual theory* mk_fresh(context*) { return alloc(theory_str, get_manager(), m_params); } + virtual void init_search_eh(); + virtual void add_theory_assumptions(expr_ref_vector & assumptions); + virtual lbool validate_unsat_core(expr_ref_vector & unsat_core); + virtual void relevant_eh(app * n); + virtual void assign_eh(bool_var v, bool is_true); + virtual void push_scope_eh(); + virtual void pop_scope_eh(unsigned num_scopes); + virtual void reset_eh(); - virtual bool can_propagate(); - virtual void propagate(); + virtual bool can_propagate(); + virtual void propagate(); - virtual final_check_status final_check_eh(); - virtual void attach_new_th_var(enode * n); + virtual final_check_status final_check_eh(); + virtual void attach_new_th_var(enode * n); - virtual void init_model(model_generator & m); - virtual model_value_proc * mk_value(enode * n, model_generator & mg); - virtual void finalize_model(model_generator & mg); - }; + virtual void init_model(model_generator & m); + virtual model_value_proc * mk_value(enode * n, model_generator & mg); + virtual void finalize_model(model_generator & mg); +}; }; From 82bdd26817b611829d6417094f86cf3b964c15b0 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sat, 6 May 2017 13:40:53 -0400 Subject: [PATCH 557/562] clean up some warnings Signed-off-by: Nikolaj Bjorner --- src/smt/theory_str.cpp | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 4a6a6da5b..d156005fd 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -1903,7 +1903,6 @@ namespace smt { } void theory_str::group_terms_by_eqc(expr * n, std::set & concats, std::set & vars, std::set & consts) { - context & ctx = get_context(); expr * eqcNode = n; do { app * ast = to_app(eqcNode); @@ -4822,7 +4821,6 @@ namespace smt { } expr * theory_str::collect_eq_nodes(expr * n, expr_ref_vector & eqcSet) { - context & ctx = get_context(); expr * constStrNode = NULL; expr * ex = n; @@ -4873,8 +4871,6 @@ namespace smt { if (!contain_pair_bool_map.find(strAst, substrAst, boolVar)) { TRACE("str", tout << "warning: no entry for boolVar in contain_pair_bool_map" << std::endl;); } - // boolVar is actually a Contains term - app * containsApp = to_app(boolVar); // we only want to inspect the Contains terms where either of strAst or substrAst // are equal to varNode. @@ -5012,8 +5008,6 @@ namespace smt { if (!contain_pair_bool_map.find(strAst, substrAst, boolVar)) { TRACE("str", tout << "warning: no entry for boolVar in contain_pair_bool_map" << std::endl;); } - // boolVar is actually a Contains term - app * containsApp = to_app(boolVar); // we only want to inspect the Contains terms where either of strAst or substrAst // are equal to varNode. @@ -5421,7 +5415,6 @@ namespace smt { return; } - context & ctx = get_context(); ast_manager & m = get_manager(); TRACE("str", tout << "consistency check for contains wrt. " << mk_pp(n1, m) << " and " << mk_pp(n2, m) << std::endl;); @@ -6171,8 +6164,6 @@ namespace smt { // Modified signature: returns true if nothing was learned, or false if at least one axiom was asserted. // (This is used for deferred consistency checking) bool theory_str::check_concat_len_in_eqc(expr * concat) { - context & ctx = get_context(); - bool no_assertions = true; expr * eqc_n = concat; @@ -7542,7 +7533,6 @@ namespace smt { void theory_str::pop_scope_eh(unsigned num_scopes) { sLevel -= num_scopes; TRACE("str", tout << "pop " << num_scopes << " to " << sLevel << std::endl;); - context & ctx = get_context(); ast_manager & m = get_manager(); TRACE_CODE(if (is_trace_enabled("t_str_dump_assign_on_scope_change")) { dump_assignments(); }); @@ -10023,7 +10013,6 @@ namespace smt { expr * theory_str::binary_search_length_test(expr * freeVar, expr * previousLenTester, zstring previousLenTesterValue) { ast_manager & m = get_manager(); - context & ctx = get_context(); if (binary_search_len_tester_stack.contains(freeVar) && !binary_search_len_tester_stack[freeVar].empty()) { TRACE("str", tout << "checking existing length testers for " << mk_pp(freeVar, m) << std::endl; @@ -10353,7 +10342,6 @@ namespace smt { } void theory_str::get_concats_in_eqc(expr * n, std::set & concats) { - context & ctx = get_context(); expr * eqcNode = n; do { @@ -10502,7 +10490,6 @@ namespace smt { void theory_str::get_eqc_simpleUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet) { constStr = NULL; unrollFuncSet.clear(); - context & ctx = get_context(); expr * curr = n; do { @@ -10571,12 +10558,11 @@ namespace smt { TRACE("str", tout << "mk_value for: " << mk_ismt2_pp(n->get_owner(), get_manager()) << " (sort " << mk_ismt2_pp(get_manager().get_sort(n->get_owner()), get_manager()) << ")" << std::endl;); ast_manager & m = get_manager(); - context & ctx = get_context(); app_ref owner(m); owner = n->get_owner(); // If the owner is not internalized, it doesn't have an enode associated. - SASSERT(ctx.e_internalized(owner)); + SASSERT(get_context().e_internalized(owner)); app * val = mk_value_helper(owner); if (val != NULL) { From e02392c0e3e8a52b2999990ec70466ce20a23384 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sun, 7 May 2017 14:03:24 -0700 Subject: [PATCH 558/562] use skolem function to avoid exposing temporary variables in models Signed-off-by: Nikolaj Bjorner --- src/smt/theory_str.cpp | 13 +++++++++---- src/smt/theory_str.h | 1 + 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index d156005fd..835f3b553 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -440,6 +440,11 @@ namespace smt { return mk_int_var("$$_xor"); } + app * theory_str::mk_fresh_const(char const* name, sort* s) { + return u.mk_skolem(symbol(name), 0, 0, s); + } + + app * theory_str::mk_int_var(std::string name) { context & ctx = get_context(); ast_manager & m = get_manager(); @@ -447,7 +452,7 @@ namespace smt { TRACE("str", tout << "creating integer variable " << name << " at scope level " << sLevel << std::endl;); sort * int_sort = m.mk_sort(m_autil.get_family_id(), INT_SORT); - app * a = m.mk_fresh_const(name.c_str(), int_sort); + app * a = mk_fresh_const(name.c_str(), int_sort); ctx.internalize(a, false); SASSERT(ctx.get_enode(a) != NULL); @@ -482,7 +487,7 @@ namespace smt { TRACE("str", tout << "creating string variable " << name << " at scope level " << sLevel << std::endl;); sort * string_sort = u.str.mk_string_sort(); - app * a = m.mk_fresh_const(name.c_str(), string_sort); + app * a = mk_fresh_const(name.c_str(), string_sort); TRACE("str", tout << "a->get_family_id() = " << a->get_family_id() << std::endl << "this->get_family_id() = " << this->get_family_id() << std::endl;); @@ -509,7 +514,7 @@ namespace smt { ast_manager & m = get_manager(); sort * string_sort = u.str.mk_string_sort(); - app * a = m.mk_fresh_const("regex", string_sort); + app * a = mk_fresh_const("regex", string_sort); ctx.internalize(a, false); SASSERT(ctx.get_enode(a) != NULL); @@ -561,7 +566,7 @@ namespace smt { TRACE("str", tout << "creating nonempty string variable " << name << " at scope level " << sLevel << std::endl;); sort * string_sort = u.str.mk_string_sort(); - app * a = m.mk_fresh_const(name.c_str(), string_sort); + app * a = mk_fresh_const(name.c_str(), string_sort); ctx.internalize(a, false); SASSERT(ctx.get_enode(a) != NULL); diff --git a/src/smt/theory_str.h b/src/smt/theory_str.h index 2e6d96fa7..0403b0623 100644 --- a/src/smt/theory_str.h +++ b/src/smt/theory_str.h @@ -403,6 +403,7 @@ protected: expr * mk_concat_const_str(expr * n1, expr * n2); app * mk_contains(expr * haystack, expr * needle); app * mk_indexof(expr * haystack, expr * needle); + app * mk_fresh_const(char const* name, sort* s); literal mk_literal(expr* _e); app * mk_int(int n); From 3ae722025f44b2c6df646264743a06fe57bf6d04 Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sun, 7 May 2017 14:54:47 -0700 Subject: [PATCH 559/562] relaxing condition for assumptions, add theory-assumption to skolem functions Signed-off-by: Nikolaj Bjorner --- src/smt/smt_context.cpp | 9 ++++++++- src/smt/theory_str.cpp | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/smt/smt_context.cpp b/src/smt/smt_context.cpp index 37a6d32b7..50b957331 100644 --- a/src/smt/smt_context.cpp +++ b/src/smt/smt_context.cpp @@ -3115,11 +3115,18 @@ namespace smt { } bool is_valid_assumption(ast_manager & m, expr * assumption) { + expr* arg; if (!m.is_bool(assumption)) return false; if (is_uninterp_const(assumption)) return true; - if (m.is_not(assumption) && is_uninterp_const(to_app(assumption)->get_arg(0))) + if (m.is_not(assumption, arg) && is_uninterp_const(arg)) + return true; + if (!is_app(assumption)) + return false; + if (to_app(assumption)->get_num_args() == 0) + return true; + if (m.is_not(assumption, arg) && is_app(arg) && to_app(arg)->get_num_args() == 0) return true; return false; } diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 835f3b553..128f93b11 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -7351,10 +7351,10 @@ namespace smt { void theory_str::add_theory_assumptions(expr_ref_vector & assumptions) { TRACE("str", tout << "add overlap assumption for theory_str" << std::endl;); - symbol strOverlap("!!TheoryStrOverlapAssumption!!"); + char* strOverlap = "!!TheoryStrOverlapAssumption!!"; seq_util m_sequtil(get_manager()); sort * s = get_manager().mk_bool_sort(); - m_theoryStrOverlapAssumption_term = expr_ref(get_manager().mk_const(strOverlap, s), get_manager()); + m_theoryStrOverlapAssumption_term = expr_ref(mk_fresh_const(strOverlap, s), get_manager()); assumptions.push_back(get_manager().mk_not(m_theoryStrOverlapAssumption_term)); } From 6b2a800c7fb14a504ece5fb0eae574c8cd7f1f4a Mon Sep 17 00:00:00 2001 From: Murphy Berzish Date: Sun, 7 May 2017 18:23:47 -0400 Subject: [PATCH 560/562] fix warnings: unused variables, string constants --- src/smt/theory_str.cpp | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/smt/theory_str.cpp b/src/smt/theory_str.cpp index 128f93b11..818aca29a 100644 --- a/src/smt/theory_str.cpp +++ b/src/smt/theory_str.cpp @@ -4844,7 +4844,6 @@ namespace smt { * Collect constant strings (from left to right) in an AST node. */ void theory_str::get_const_str_asts_in_node(expr * node, expr_ref_vector & astList) { - ast_manager & m = get_manager(); if (u.str.is_string(node)) { astList.push_back(node); //} else if (getNodeType(t, node) == my_Z3_Func) { @@ -5519,7 +5518,6 @@ namespace smt { // --------------------------------------------------------- context & ctx = get_context(); - ast_manager & m = get_manager(); // const strings: node is de-aliased if (u.str.is_string(node)) { @@ -7351,7 +7349,7 @@ namespace smt { void theory_str::add_theory_assumptions(expr_ref_vector & assumptions) { TRACE("str", tout << "add overlap assumption for theory_str" << std::endl;); - char* strOverlap = "!!TheoryStrOverlapAssumption!!"; + const char* strOverlap = "!!TheoryStrOverlapAssumption!!"; seq_util m_sequtil(get_manager()); sort * s = get_manager().mk_bool_sort(); m_theoryStrOverlapAssumption_term = expr_ref(mk_fresh_const(strOverlap, s), get_manager()); @@ -7359,8 +7357,6 @@ namespace smt { } lbool theory_str::validate_unsat_core(expr_ref_vector & unsat_core) { - bool assumptionFound = false; - app * target_term = to_app(get_manager().mk_not(m_theoryStrOverlapAssumption_term)); get_context().internalize(target_term, false); for (unsigned i = 0; i < unsat_core.size(); ++i) { @@ -7372,7 +7368,6 @@ namespace smt { e2 = get_context().get_enode(core_term); if (e1 == e2) { TRACE("str", tout << "overlap detected in unsat core, changing UNSAT to UNKNOWN" << std::endl;); - assumptionFound = true; return l_undef; } } @@ -7483,7 +7478,6 @@ namespace smt { } void theory_str::recursive_check_variable_scope(expr * ex) { - context & ctx = get_context(); ast_manager & m = get_manager(); if (is_app(ex)) { @@ -7551,7 +7545,7 @@ namespace smt { std::stack & val = cut_var_map[varItor->m_key]; while ((val.size() > 0) && (val.top()->level != 0) && (val.top()->level >= sLevel)) { TRACE("str", tout << "remove cut info for " << mk_pp(e, m) << std::endl; print_cut_var(e, tout);); - T_cut * aCut = val.top(); + // T_cut * aCut = val.top(); val.pop(); // dealloc(aCut); } @@ -8571,8 +8565,6 @@ namespace smt { return; } if (u.str.is_concat(aNode)) { - expr * arg0 = aNode->get_arg(0); - expr * arg1 = aNode->get_arg(1); if (concatSet.find(node) == concatSet.end()) { concatSet.insert(node); } @@ -8592,7 +8584,6 @@ namespace smt { TRACE("str", tout << "propagate_length_within_eqc: " << mk_ismt2_pp(var, m) << std::endl ;); - enode * n_eq_enode = ctx.get_enode(var); rational varLen; if (! get_len_value(var, varLen)) { bool hasLen = false; @@ -8686,7 +8677,6 @@ namespace smt { expr * var = *it; rational lenValue; expr_ref varlen (mk_strlen(var), m) ; - bool allLeafResolved = true; if (! get_value(varlen, lenValue)) { if (propagate_length_within_eqc(var)) { axiomAdded = true; @@ -8806,7 +8796,7 @@ namespace smt { bool concat_lhs_haseqc, concat_rhs_haseqc, var_haseqc; expr * concat_lhs_str = get_eqc_value(concat_lhs, concat_lhs_haseqc); expr * concat_rhs_str = get_eqc_value(concat_rhs, concat_rhs_haseqc); - expr * var_str = get_eqc_value(var, var_haseqc); + get_eqc_value(var, var_haseqc); if (concat_lhs_haseqc && concat_rhs_haseqc && !var_haseqc) { TRACE("str", tout << "backpropagate into " << mk_pp(var, m) << " = " << mk_pp(concat, m) << std::endl << "LHS ~= " << mk_pp(concat_lhs_str, m) << " RHS ~= " << mk_pp(concat_rhs_str, m) << std::endl;); @@ -10358,8 +10348,6 @@ namespace smt { } void theory_str::get_var_in_eqc(expr * n, std::set & varSet) { - context & ctx = get_context(); - expr * eqcNode = n; do { if (variable_set.find(eqcNode) != variable_set.end()) { @@ -10476,7 +10464,6 @@ namespace smt { void theory_str::get_eqc_allUnroll(expr * n, expr * &constStr, std::set & unrollFuncSet) { constStr = NULL; unrollFuncSet.clear(); - context & ctx = get_context(); expr * curr = n; do { From 0ba7c9c39bae4e53ab8b2029be0db88214300b8f Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sun, 7 May 2017 16:53:25 -0700 Subject: [PATCH 561/562] adding pb Signed-off-by: Nikolaj Bjorner --- src/sat/card_extension.cpp | 521 ++++++++++++++++++++++++++++++++----- src/sat/card_extension.h | 33 ++- src/sat/sat_simplifier.cpp | 2 +- 3 files changed, 487 insertions(+), 69 deletions(-) diff --git a/src/sat/card_extension.cpp b/src/sat/card_extension.cpp index 7d1c27abb..30206a441 100644 --- a/src/sat/card_extension.cpp +++ b/src/sat/card_extension.cpp @@ -45,9 +45,11 @@ namespace sat { m_index(index), m_lit(lit), m_k(k), - m_size(wlits.size()) { + m_size(wlits.size()), + m_max_sum(0) { for (unsigned i = 0; i < wlits.size(); ++i) { m_wlits[i] = wlits[i]; + m_max_sum += wlits[i].first; } } @@ -212,14 +214,355 @@ namespace sat { } // pb: - void card_extension::init_watch(pb& p, bool is_true) { - NOT_IMPLEMENTED_YET(); + + void card_extension::copy_pb(card_extension& result) { + for (unsigned i = 0; i < m_pbs.size(); ++i) { + svector wlits; + pb& p = *m_pbs[i]; + for (unsigned i = 0; i < p.size(); ++i) { + wlits.push_back(p[i]); + } + bool_var v = p.lit() == null_literal ? null_bool_var : p.lit().var(); + result.add_pb_ge(v, wlits, p.k()); + } } + // watch a prefix of literals, such that the slack of these is >= k + void card_extension::init_watch(pb& p, bool is_true) { + clear_watch(p); + if (p.lit() != null_literal && p.lit().sign() == is_true) { + p.negate(); + } + + TRACE("sat", display(tout << "init watch: ", p, true);); + SASSERT(p.lit() == null_literal || value(p.lit()) == l_true); + unsigned sz = p.size(), bound = p.k(); + // put the non-false literals into the head. + unsigned slack = 0, num_watch = 0, j = 0; + for (unsigned i = 0; i < sz; ++i) { + if (value(p[i].second) != l_false) { + if (j != i) { + p.swap(i, j); + } + if (slack < bound) { + slack += p[i].first; + ++num_watch; + } + ++j; + } + } + DEBUG_CODE( + bool is_false = false; + for (unsigned k = 0; k < sz; ++k) { + SASSERT(!is_false || value(p[k].second) == l_false); + SASSERT(k < j == (value(p[k].second) != l_false)); + is_false = value(p[k].second) == l_false; + }); + if (slack < bound) { + literal lit = p[j].second; + SASSERT(value(p[j].second) == l_false); + for (unsigned i = j + 1; j < sz; ++i) { + if (lvl(lit) < lvl(p[i].second)) { + lit = p[i].second; + } + } + set_conflict(p, lit); + } + else { + for (unsigned i = 0; i < num_watch; ++i) { + watch_literal(p, p[i]); + } + p.set_slack(slack); + p.set_num_watch(num_watch); + } + } + + /* + Chai Kuhlmann: + Lw - set of watched literals + Lu - set of unwatched literals that are not false + + Lw = Lw \ { alit } + Sw -= value + a_max = max { a | l in Lw u Lu, l = undef } + while (Sw < k + a_max & Lu != 0) { + a_s = max { a | l in Lu } + Sw += a_s + Lw = Lw u {l_s} + Lu = Lu \ {l_s} + } + if (Sw < bound) conflict + while (Sw < k + a_max) { + assign (l_max) + a_max = max { ai | li in Lw, li = undef } + } + ASSERT(Sw >= bound) + return no-conflict + + a_max index: index of non-false literal with maximal weight. + + + */ + lbool card_extension::add_assign(pb& p, literal alit) { + unsigned sz = p.size(); + unsigned bound = p.k(); + unsigned num_watch = p.num_watch(); + unsigned slack = p.slack(); + SASSERT(value(alit) == l_false); + SASSERT(p.lit() == null_literal || value(p.lit()) == l_true); + SASSERT(num_watch <= sz); + unsigned index = 0; + unsigned a_max = 0; + unsigned max_index = 0; + m_pb_undef.reset(); + for (; index < num_watch; ++index) { + literal lit = p[index].second; + if (lit == alit) { + break; + } + if (value(lit) == l_undef) { + m_pb_undef.push_back(index); + if (p[index].first > a_max) { + a_max = p[index].first; + max_index = index; + } + } + } + + for (unsigned j = index + 1; a_max == 0 && j < num_watch; ++j) { + literal lit = p[j].second; + if (value(lit) == l_undef) { + m_pb_undef.push_back(j); + a_max = p[j].first; + max_index = j; + } + } + for (unsigned j = num_watch; a_max == 0 && j < sz; ++j) { + literal lit = p[j].second; + if (value(lit) == l_undef) { + p.swap(j, num_watch); + m_pb_undef.push_back(num_watch); + a_max = p[num_watch].first; + max_index = num_watch; + } + } + + unsigned val = p[index].first; + SASSERT(num_watch > 0); + SASSERT(index < num_watch); + SASSERT(value(p[index].second) == l_false); + SASSERT(val <= slack); + slack -= val; + // find literals to swap with: + for (unsigned j = num_watch; j < sz && slack < bound + a_max; ++j) { + if (value(p[j].second) != l_false) { + slack += p[j].first; + watch_literal(p, p[j]); + p.swap(num_watch, j); + if (value(p[num_watch].second) == l_undef && a_max < p[num_watch].first) { + m_pb_undef.push_back(num_watch); + a_max = p[num_watch].first; + max_index = num_watch; + } + ++num_watch; + } + } + + if (slack < bound) { + // maintain watching the literal + slack += val; + p.set_slack(slack); + p.set_num_watch(num_watch); + SASSERT(bound <= slack); + TRACE("sat", tout << "conflict " << alit << "\n";); + set_conflict(p, alit); + return l_false; + } + + // swap out the watched literal. + p.set_slack(slack); + --num_watch; + SASSERT(num_watch > 0); + p.set_num_watch(num_watch); + p.swap(num_watch, index); + if (num_watch == max_index) { + max_index = index; + } + + SASSERT(max_index < sz); + while (slack < bound + a_max && !s().inconsistent()) { + // variable at max-index must be assigned to true. + assign(p, p[max_index].second); + + a_max = 0; + // find the next a_max among m_pb_undef + while (!m_pb_undef.empty() && l_undef != value(p[m_pb_undef.back()].second)) { + m_pb_undef.pop_back(); + } + if (m_pb_undef.empty()) { + break; + } + max_index = m_pb_undef.back(); + a_max = p[max_index].first; + m_pb_undef.pop_back(); + } + + return s().inconsistent() ? l_false : l_true; + } + + void card_extension::watch_literal(pb& p, wliteral l) { + literal lit = l.second; + init_watch(lit.var()); + ptr_vector* pbs = m_var_infos[lit.var()].m_pb_watch[lit.sign()]; + if (pbs == 0) { + pbs = alloc(ptr_vector); + m_var_infos[lit.var()].m_pb_watch[lit.sign()] = pbs; + } + else if (is_tag_empty(pbs)) { + pbs = set_tag_non_empty(pbs); + m_var_infos[lit.var()].m_pb_watch[lit.sign()] = pbs; + } + TRACE("sat_verbose", tout << "insert: " << lit.var() << " " << lit.sign() << "\n";); + pbs->push_back(&p); + } + + void card_extension::clear_watch(pb& p) { + unsigned sz = p.size(); + for (unsigned i = 0; i < sz; ++i) { + unwatch_literal(p[i].second, &p); + } + } + + void card_extension::unwatch_literal(literal lit, pb* p) { + if (m_var_infos.size() <= static_cast(lit.var())) { + return; + } + pb_watch*& pbs = m_var_infos[lit.var()].m_pb_watch[lit.sign()]; + if (!is_tag_empty(pbs)) { + if (remove(*pbs, p)) { + pbs = set_tag_empty(pbs); + } + } + } + + void card_extension::set_conflict(pb& p, literal lit) { + m_stats.m_num_pb_conflicts++; + TRACE("sat", display(tout, p, true); ); + // SASSERT(validate_conflict(p)); + SASSERT(value(lit) == l_false); + s().set_conflict(justification::mk_ext_justification(p.index()), ~lit); + SASSERT(s().inconsistent()); + } + + void card_extension::assign(pb& p, literal lit) { + switch (value(lit)) { + case l_true: + break; + case l_false: + set_conflict(p, lit); + break; + default: + m_stats.m_num_pb_propagations++; + m_num_propagations_since_pop++; + if (s().m_config.m_drat) { + svector ps; + literal_vector lits; + get_pb_antecedents(lit, p, lits); + lits.push_back(lit); + ps.push_back(drat::premise(drat::s_ext(), p.lit())); + s().m_drat.add(lits, ps); + } + s().assign(lit, justification::mk_ext_justification(p.index())); + break; + } + } + + void card_extension::display(std::ostream& out, pb& p, bool values) const { + out << p.lit() << "[" << p.size() << "]"; + if (p.lit() != null_literal && values) { + out << "@(" << value(p.lit()); + if (value(p.lit()) != l_undef) { + out << ":" << lvl(p.lit()); + } + out << "): "; + } + else { + out << ": "; + } + for (unsigned i = 0; i < p.size(); ++i) { + literal l = p[i].second; + unsigned w = p[i].first; + if (w > 1) out << w << " * "; + out << l; + if (values) { + out << "@(" << value(l); + if (value(l) != l_undef) { + out << ":" << lvl(l); + } + out << ") "; + } + else { + out << " "; + } + } + out << ">= " << p.k() << "\n"; + } + + void card_extension::asserted_pb(literal l, ptr_vector* pbs, pb* p0) { + TRACE("sat", tout << l << " " << !is_tag_empty(pbs) << " " << (p0 != 0) << "\n";); + if (!is_tag_empty(pbs)) { + ptr_vector::iterator begin = pbs->begin(); + ptr_vector::iterator it = begin, it2 = it, end = pbs->end(); + for (; it != end; ++it) { + pb& p = *(*it); + if (p.lit() != null_literal && value(p.lit()) != l_true) { + continue; + } + switch (add_assign(p, ~l)) { + case l_false: // conflict + for (; it != end; ++it, ++it2) { + *it2 = *it; + } + SASSERT(s().inconsistent()); + pbs->set_end(it2); + return; + case l_true: // unit propagation, keep watching the literal + if (it2 != it) { + *it2 = *it; + } + ++it2; + break; + case l_undef: // watch literal was swapped + break; + } + } + pbs->set_end(it2); + if (pbs->empty()) { + m_var_infos[l.var()].m_pb_watch[!l.sign()] = set_tag_empty(pbs); + } + } + + if (p0 != 0 && !s().inconsistent()) { + init_watch(*p0, !l.sign()); + } + } // xor: + + void card_extension::copy_xor(card_extension& result) { + for (unsigned i = 0; i < m_xors.size(); ++i) { + literal_vector lits; + xor& x = *m_xors[i]; + for (unsigned i = 0; i < x.size(); ++i) { + lits.push_back(x[i]); + } + bool_var v = x.lit() == null_literal ? null_bool_var : x.lit().var(); + result.add_xor(v, lits); + } + } + void card_extension::clear_watch(xor& x) { unwatch_literal(x[0], &x); unwatch_literal(x[1], &x); @@ -229,7 +572,7 @@ namespace sat { if (m_var_infos.size() <= static_cast(lit.var())) { return; } - xor_watch* xors = m_var_infos[lit.var()].m_xor_watch; + xor_watch*& xors = m_var_infos[lit.var()].m_xor_watch; if (!is_tag_empty(xors)) { if (remove(*xors, c)) { xors = set_tag_empty(xors); @@ -384,6 +727,45 @@ namespace sat { return s().inconsistent() ? l_false : l_true; } + void card_extension::asserted_xor(literal l, ptr_vector* xors, xor* x) { + TRACE("sat", tout << l << " " << !is_tag_empty(xors) << " " << (x != 0) << "\n";); + if (!is_tag_empty(xors)) { + ptr_vector::iterator begin = xors->begin(); + ptr_vector::iterator it = begin, it2 = it, end = xors->end(); + for (; it != end; ++it) { + xor& c = *(*it); + if (c.lit() != null_literal && value(c.lit()) != l_true) { + continue; + } + switch (add_assign(c, ~l)) { + case l_false: // conflict + for (; it != end; ++it, ++it2) { + *it2 = *it; + } + SASSERT(s().inconsistent()); + xors->set_end(it2); + return; + case l_undef: // watch literal was swapped + break; + case l_true: // unit propagation, keep watching the literal + if (it2 != it) { + *it2 = *it; + } + ++it2; + break; + } + } + xors->set_end(it2); + if (xors->empty()) { + m_var_infos[l.var()].m_xor_watch = set_tag_empty(xors); + } + } + + if (x != 0 && !s().inconsistent()) { + init_watch(*x, !l.sign()); + } + } + void card_extension::normalize_active_coeffs() { while (!m_active_var_set.empty()) m_active_var_set.erase(); @@ -551,7 +933,15 @@ namespace sat { ++m_stats.m_num_xor_resolves; } else if (is_pb_index(index)) { - NOT_IMPLEMENTED_YET(); + pb& p = index2pb(index); + m_lemma.reset(); + m_bound += offset; + inc_coeff(consequent, offset); + get_pb_antecedents(consequent, p, m_lemma); + for (unsigned i = 0; i < m_lemma.size(); ++i) { + process_antecedent(~m_lemma[i], offset); + } + ++m_stats.m_num_pb_resolves; } else { UNREACHABLE(); @@ -810,10 +1200,10 @@ namespace sat { } void card_extension::add_pb_ge(bool_var v, svector const& wlits, unsigned k) { - unsigned index = 4*m_pb.size() + 0x11; + unsigned index = 4*m_pbs.size() + 0x11; literal lit = v == null_bool_var ? null_literal : literal(v, false); pb* p = new (memory::allocate(pb::get_obj_size(wlits.size()))) pb(index, lit, wlits, k); - m_pb.push_back(p); + m_pbs.push_back(p); if (v == null_bool_var) { init_watch(*p, true); m_pb_axioms.push_back(p); @@ -946,6 +1336,25 @@ namespace sat { TRACE("sat", tout << r << "\n";); } + void card_extension::get_pb_antecedents(literal l, pb const& p, literal_vector& r) { + if (p.lit() != null_literal) r.push_back(p.lit()); + SASSERT(p.lit() == null_literal || value(p.lit()) == l_true); + unsigned k = p.k(); + unsigned max_sum = p.max_sum(); + for (unsigned i = p.size(); i > 0 && max_sum >= k; ) { + --i; + literal lit = p[i].second; + if (lit == l) { + max_sum -= p[i].first; + } + else if (value(lit) == l_false) { + r.push_back(~p[i].second); + max_sum -= p[i].first; + } + } + SASSERT(max_sum < k); + } + void card_extension::get_antecedents(literal l, ext_justification_idx idx, literal_vector & r) { if (is_card_index(idx)) { card& c = index2card(idx); @@ -984,7 +1393,8 @@ namespace sat { } } else if (is_pb_index(idx)) { - NOT_IMPLEMENTED_YET(); + pb const& p = index2pb(idx); + get_pb_antecedents(l, p, r); } else { UNREACHABLE(); @@ -1054,9 +1464,11 @@ namespace sat { if (v >= m_var_infos.size()) return; var_info& vinfo = m_var_infos[v]; ptr_vector* cards = vinfo.m_card_watch[!l.sign()]; + ptr_vector* xors = vinfo.m_xor_watch; + ptr_vector* pbs = vinfo.m_pb_watch[!l.sign()]; + pb* p = vinfo.m_pb; card* crd = vinfo.m_card; xor* x = vinfo.m_xor; - ptr_vector* xors = vinfo.m_xor_watch; if (!is_tag_empty(cards)) { ptr_vector::iterator begin = cards->begin(); @@ -1093,51 +1505,17 @@ namespace sat { if (crd != 0 && !s().inconsistent()) { init_watch(*crd, !l.sign()); } + + if ((!is_tag_empty(pbs) || p) && !s().inconsistent()) { + asserted_pb(l, pbs, p); + } + if (m_has_xor && !s().inconsistent()) { asserted_xor(l, xors, x); } } - void card_extension::asserted_xor(literal l, ptr_vector* xors, xor* x) { - TRACE("sat", tout << l << " " << !is_tag_empty(xors) << " " << (x != 0) << "\n";); - if (!is_tag_empty(xors)) { - ptr_vector::iterator begin = xors->begin(); - ptr_vector::iterator it = begin, it2 = it, end = xors->end(); - for (; it != end; ++it) { - xor& c = *(*it); - if (c.lit() != null_literal && value(c.lit()) != l_true) { - continue; - } - switch (add_assign(c, ~l)) { - case l_false: // conflict - for (; it != end; ++it, ++it2) { - *it2 = *it; - } - SASSERT(s().inconsistent()); - xors->set_end(it2); - return; - case l_undef: // watch literal was swapped - break; - case l_true: // unit propagation, keep watching the literal - if (it2 != it) { - *it2 = *it; - } - ++it2; - break; - } - } - xors->set_end(it2); - if (xors->empty()) { - m_var_infos[l.var()].m_xor_watch = set_tag_empty(xors); - } - } - - if (x != 0 && !s().inconsistent()) { - init_watch(*x, !l.sign()); - } - } - check_result card_extension::check() { return CR_DONE; } void card_extension::push() { @@ -1153,13 +1531,17 @@ namespace sat { m_var_trail.pop_back(); if (v != null_bool_var) { card* c = m_var_infos[v].m_card; - clear_watch(*c); - m_var_infos[v].m_card = 0; - dealloc(c); + if (c) { + clear_watch(*c); + m_var_infos[v].m_card = 0; + dealloc(c); + } xor* x = m_var_infos[v].m_xor; - clear_watch(*x); - m_var_infos[v].m_xor = 0; - dealloc(x); + if (x) { + clear_watch(*x); + m_var_infos[v].m_xor = 0; + dealloc(x); + } } } m_var_lim.resize(new_lim); @@ -1182,15 +1564,8 @@ namespace sat { bool_var v = c.lit() == null_literal ? null_bool_var : c.lit().var(); result->add_at_least(v, lits, c.k()); } - for (unsigned i = 0; i < m_xors.size(); ++i) { - literal_vector lits; - xor& x = *m_xors[i]; - for (unsigned i = 0; i < x.size(); ++i) { - lits.push_back(x[i]); - } - bool_var v = x.lit() == null_literal ? null_bool_var : x.lit().var(); - result->add_xor(v, lits); - } + copy_xor(*result); + copy_pb(*result); return result; } @@ -1347,7 +1722,15 @@ namespace sat { } } else if (is_pb_index(idx)) { - NOT_IMPLEMENTED_YET(); + pb& p = index2pb(idx); + out << "pb " << p.lit() << ": "; + for (unsigned i = 0; i < p.size(); ++i) { + if (p[i].first != 1) { + out << p[i].first << " "; + } + out << p[i].second << " "; + } + out << ">= " << p.k(); } else { UNREACHABLE(); @@ -1362,6 +1745,9 @@ namespace sat { st.update("xor propagations", m_stats.m_num_xor_propagations); st.update("xor conflicts", m_stats.m_num_xor_conflicts); st.update("xor resolves", m_stats.m_num_xor_resolves); + st.update("pb propagations", m_stats.m_num_pb_propagations); + st.update("pb conflicts", m_stats.m_num_pb_conflicts); + st.update("pb resolves", m_stats.m_num_pb_resolves); } bool card_extension::validate_conflict(card& c) { @@ -1457,7 +1843,12 @@ namespace sat { if (lxor != null_literal) p.push(~lxor, offset); } else if (is_pb_index(index)) { - NOT_IMPLEMENTED_YET(); + pb& pb = index2pb(index); + p.reset(pb.k()); + for (unsigned i = 0; i < pb.size(); ++i) { + p.push(pb[i].second, pb[i].first); + } + if (pb.lit() != null_literal) p.push(~pb.lit(), pb.k()); } else { UNREACHABLE(); diff --git a/src/sat/card_extension.h b/src/sat/card_extension.h index 6f8b6d120..0e229a056 100644 --- a/src/sat/card_extension.h +++ b/src/sat/card_extension.h @@ -36,6 +36,9 @@ namespace sat { unsigned m_num_xor_propagations; unsigned m_num_xor_conflicts; unsigned m_num_xor_resolves; + unsigned m_num_pb_propagations; + unsigned m_num_pb_conflicts; + unsigned m_num_pb_resolves; stats() { reset(); } void reset() { memset(this, 0, sizeof(*this)); } }; @@ -66,6 +69,9 @@ namespace sat { literal m_lit; unsigned m_k; unsigned m_size; + unsigned m_slack; + unsigned m_num_watch; + unsigned m_max_sum; wliteral m_wlits[0]; public: static size_t get_obj_size(unsigned num_lits) { return sizeof(card) + num_lits * sizeof(wliteral); } @@ -75,6 +81,11 @@ namespace sat { wliteral operator[](unsigned i) const { return m_wlits[i]; } unsigned k() const { return m_k; } unsigned size() const { return m_size; } + unsigned slack() const { return m_slack; } + void set_slack(unsigned s) { m_slack = s; } + unsigned num_watch() const { return m_num_watch; } + unsigned max_sum() const { return m_max_sum; } + void set_num_watch(unsigned s) { m_num_watch = s; } void swap(unsigned i, unsigned j) { std::swap(m_wlits[i], m_wlits[j]); } void negate(); }; @@ -153,7 +164,7 @@ namespace sat { ptr_vector m_cards; ptr_vector m_xors; - ptr_vector m_pb; + ptr_vector m_pbs; scoped_ptr_vector m_card_axioms; scoped_ptr_vector m_pb_axioms; @@ -175,6 +186,9 @@ namespace sat { bool m_has_xor; unsigned_vector m_parity_marks; literal_vector m_parity_trail; + + unsigned_vector m_pb_undef; + void ensure_parity_size(bool_var v); unsigned get_parity(bool_var v); void inc_parity(bool_var v); @@ -193,6 +207,7 @@ namespace sat { void unwatch_literal(literal w, card* c); // xor specific functionality + void copy_xor(card_extension& result); void clear_watch(xor& x); void watch_literal(xor& x, literal lit); void unwatch_literal(literal w, xor* x); @@ -202,17 +217,28 @@ namespace sat { bool parity(xor const& x, unsigned offset) const; lbool add_assign(xor& x, literal alit); void asserted_xor(literal l, ptr_vector* xors, xor* x); + void get_xor_antecedents(literal l, unsigned index, justification js, literal_vector& r); + bool is_card_index(unsigned idx) const { return 0x00 == (idx & 0x11); } bool is_xor_index(unsigned idx) const { return 0x01 == (idx & 0x11); } bool is_pb_index(unsigned idx) const { return 0x11 == (idx & 0x11); } card& index2card(unsigned idx) const { SASSERT(is_card_index(idx)); return *m_cards[idx >> 2]; } xor& index2xor(unsigned idx) const { SASSERT(!is_card_index(idx)); return *m_xors[idx >> 2]; } - pb& index2pb(unsigned idx) const { SASSERT(is_pb_index(idx)); return *m_pb[idx >> 2]; } + pb& index2pb(unsigned idx) const { SASSERT(is_pb_index(idx)); return *m_pbs[idx >> 2]; } - void get_xor_antecedents(literal l, unsigned inddex, justification js, literal_vector& r); + // pb functionality + void copy_pb(card_extension& result); + void asserted_pb(literal l, ptr_vector* pbs, pb* p); void init_watch(pb& p, bool is_true); + lbool add_assign(pb& p, literal alit); + void watch_literal(pb& p, wliteral lit); + void clear_watch(pb& p); + void set_conflict(pb& p, literal lit); + void assign(pb& p, literal l); + void unwatch_literal(literal w, pb* p); + void get_pb_antecedents(literal l, pb const& p, literal_vector & r); template @@ -260,6 +286,7 @@ namespace sat { void display(std::ostream& out, ineq& p) const; void display(std::ostream& out, card& c, bool values) const; + void display(std::ostream& out, pb& p, bool values) const; void display(std::ostream& out, xor& c, bool values) const; void display_watch(std::ostream& out, bool_var v) const; void display_watch(std::ostream& out, bool_var v, bool sign) const; diff --git a/src/sat/sat_simplifier.cpp b/src/sat/sat_simplifier.cpp index 407475280..2687c59e5 100644 --- a/src/sat/sat_simplifier.cpp +++ b/src/sat/sat_simplifier.cpp @@ -191,7 +191,7 @@ namespace sat { m_elim_counter = m_res_limit; m_old_num_elim_vars = m_num_elim_vars; - scoped_finalize _scoped_finalize(*this); + // scoped_finalize _scoped_finalize(*this); do { if (m_subsumption) From c49550ff2dcc12f3c4cc4f959af3c1587bbacbae Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Sun, 7 May 2017 18:03:38 -0700 Subject: [PATCH 562/562] enable pb solver Signed-off-by: Nikolaj Bjorner --- src/ast/rewriter/pb2bv_rewriter.cpp | 33 +++++++ src/sat/card_extension.cpp | 3 + src/sat/sat_params.pyg | 1 + src/sat/sat_solver/inc_sat_solver.cpp | 3 +- src/sat/tactic/goal2sat.cpp | 121 ++++++++++++++++++++++++-- 5 files changed, 154 insertions(+), 7 deletions(-) diff --git a/src/ast/rewriter/pb2bv_rewriter.cpp b/src/ast/rewriter/pb2bv_rewriter.cpp index 3a134a13c..5cef19234 100644 --- a/src/ast/rewriter/pb2bv_rewriter.cpp +++ b/src/ast/rewriter/pb2bv_rewriter.cpp @@ -53,6 +53,7 @@ struct pb2bv_rewriter::imp { rational m_k; vector m_coeffs; bool m_keep_cardinality_constraints; + bool m_keep_pb_constraints; unsigned m_min_arity; template @@ -565,6 +566,7 @@ struct pb2bv_rewriter::imp { m_trail(m), m_args(m), m_keep_cardinality_constraints(false), + m_keep_pb_constraints(false), m_min_arity(2) {} @@ -701,11 +703,33 @@ struct pb2bv_rewriter::imp { if (m_keep_cardinality_constraints && f->get_arity() >= m_min_arity) return false; result = m_sort.ge(full, pb.get_k(f).get_unsigned(), sz, args); } + else if (pb.is_eq(f) && pb.get_k(f).is_unsigned() && has_small_coefficients(f) && m_keep_pb_constraints) { + return false; + } + else if (pb.is_le(f) && pb.get_k(f).is_unsigned() && has_small_coefficients(f) && m_keep_pb_constraints) { + return false; + } + else if (pb.is_ge(f) && pb.get_k(f).is_unsigned() && has_small_coefficients(f) && m_keep_pb_constraints) { + return false; + } else { result = mk_bv(f, sz, args); } return true; } + + bool has_small_coefficients(func_decl* f) { + unsigned sz = f->get_arity(); + unsigned sum = 0; + for (unsigned i = 0; i < sz; ++i) { + rational c = pb.get_coeff(f, i); + if (!c.is_unsigned()) return false; + unsigned sum1 = sum + c.get_unsigned(); + if (sum1 < sum) return false; + sum = sum1; + } + return true; + } // definitions used for sorting network literal mk_false() { return m.mk_false(); } @@ -733,6 +757,10 @@ struct pb2bv_rewriter::imp { void keep_cardinality_constraints(bool f) { m_keep_cardinality_constraints = f; } + + void keep_pb_constraints(bool f) { + m_keep_pb_constraints = f; + } }; struct card2bv_rewriter_cfg : public default_rewriter_cfg { @@ -745,6 +773,7 @@ struct pb2bv_rewriter::imp { } card2bv_rewriter_cfg(imp& i, ast_manager & m):m_r(i, m) {} void keep_cardinality_constraints(bool f) { m_r.keep_cardinality_constraints(f); } + void keep_pb_constraints(bool f) { m_r.keep_pb_constraints(f); } }; class card_pb_rewriter : public rewriter_tpl { @@ -754,6 +783,7 @@ struct pb2bv_rewriter::imp { rewriter_tpl(m, false, m_cfg), m_cfg(i, m) {} void keep_cardinality_constraints(bool f) { m_cfg.keep_cardinality_constraints(f); } + void keep_pb_constraints(bool f) { m_cfg.keep_pb_constraints(f); } }; card_pb_rewriter m_rw; @@ -764,14 +794,17 @@ struct pb2bv_rewriter::imp { m_num_translated(0), m_rw(*this, m) { m_rw.keep_cardinality_constraints(p.get_bool("keep_cardinality_constraints", false)); + m_rw.keep_pb_constraints(p.get_bool("keep_pb_constraints", false)); } void updt_params(params_ref const & p) { m_params.append(p); m_rw.keep_cardinality_constraints(m_params.get_bool("keep_cardinality_constraints", false)); + m_rw.keep_pb_constraints(m_params.get_bool("keep_pb_constraints", false)); } void collect_param_descrs(param_descrs& r) const { r.insert("keep_cardinality_constraints", CPK_BOOL, "(default: true) retain cardinality constraints (don't bit-blast them) and use built-in cardinality solver"); + r.insert("keep_pb_constraints", CPK_BOOL, "(default: true) retain pb constraints (don't bit-blast them) and use built-in pb solver"); } unsigned get_num_steps() const { return m_rw.get_num_steps(); } diff --git a/src/sat/card_extension.cpp b/src/sat/card_extension.cpp index 30206a441..bd32a7c40 100644 --- a/src/sat/card_extension.cpp +++ b/src/sat/card_extension.cpp @@ -49,6 +49,9 @@ namespace sat { m_max_sum(0) { for (unsigned i = 0; i < wlits.size(); ++i) { m_wlits[i] = wlits[i]; + if (m_max_sum + wlits[i].first < m_max_sum) { + throw default_exception("addition of pb coefficients overflows"); + } m_max_sum += wlits[i].first; } } diff --git a/src/sat/sat_params.pyg b/src/sat/sat_params.pyg index a13a8e8b5..226c79642 100644 --- a/src/sat/sat_params.pyg +++ b/src/sat/sat_params.pyg @@ -27,6 +27,7 @@ def_module_params('sat', ('drat.file', SYMBOL, '', 'file to dump DRAT proofs'), ('drat.check', BOOL, False, 'build up internal proof and check'), ('cardinality.solver', BOOL, False, 'use cardinality solver'), + ('pb.solver', BOOL, False, 'use pb solver'), ('xor.solver', BOOL, False, 'use xor solver'), ('local_search_threads', UINT, 0, 'number of local search threads to find satisfiable solution'), ('local_search', BOOL, False, 'use local search instead of CDCL'), diff --git a/src/sat/sat_solver/inc_sat_solver.cpp b/src/sat/sat_solver/inc_sat_solver.cpp index 0c0f82537..0d12c0a94 100644 --- a/src/sat/sat_solver/inc_sat_solver.cpp +++ b/src/sat/sat_solver/inc_sat_solver.cpp @@ -216,7 +216,8 @@ public: m_params.append(p); sat_params p1(p); m_params.set_bool("elim_vars", false); - m_params.set_bool("keep_cardinality_constraints", p1.cardinality_solver()); + m_params.set_bool("keep_cardinality_constraints", p1.pb_solver() || p1.cardinality_solver()); + m_params.set_bool("keep_pb_constraints", p1.pb_solver()); m_params.set_bool("xor_solver", p1.xor_solver()); m_solver.updt_params(m_params); m_optimize_model = m_params.get_bool("optimize_model", false); diff --git a/src/sat/tactic/goal2sat.cpp b/src/sat/tactic/goal2sat.cpp index 971843d55..9d07ec3e6 100644 --- a/src/sat/tactic/goal2sat.cpp +++ b/src/sat/tactic/goal2sat.cpp @@ -419,6 +419,103 @@ struct goal2sat::imp { } } + typedef std::pair wliteral; + + void check_unsigned(rational const& c) { + if (!c.is_unsigned()) { + throw default_exception("unsigned coefficient expected"); + } + } + + void convert_to_wlits(app* t, sat::literal_vector const& lits, svector& wlits) { + for (unsigned i = 0; i < lits.size(); ++i) { + rational c = pb.get_coeff(t, i); + check_unsigned(c); + wlits.push_back(std::make_pair(c.get_unsigned(), lits[i])); + } + } + + void convert_pb_args(app* t, svector& wlits) { + sat::literal_vector lits; + convert_pb_args(t->get_num_args(), lits); + convert_to_wlits(t, lits, wlits); + } + + void convert_pb_ge(app* t, bool root, bool sign) { + rational k = pb.get_k(t); + check_unsigned(k); + svector wlits; + convert_pb_args(t, wlits); + unsigned sz = m_result_stack.size(); + if (root) { + m_result_stack.reset(); + m_ext->add_pb_ge(sat::null_bool_var, wlits, k.get_unsigned()); + } + else { + sat::bool_var v = m_solver.mk_var(true); + sat::literal lit(v, sign); + m_ext->add_pb_ge(v, wlits, k.get_unsigned()); + TRACE("goal2sat", tout << "root: " << root << " lit: " << lit << "\n";); + m_result_stack.shrink(sz - t->get_num_args()); + m_result_stack.push_back(lit); + } + } + + void convert_pb_le(app* t, bool root, bool sign) { + rational k = pb.get_k(t); + k.neg(); + svector wlits; + convert_pb_args(t, wlits); + for (unsigned i = 0; i < wlits.size(); ++i) { + wlits[i].second.neg(); + k += rational(wlits[i].first); + } + check_unsigned(k); + unsigned sz = m_result_stack.size(); + if (root) { + m_result_stack.reset(); + m_ext->add_pb_ge(sat::null_bool_var, wlits, k.get_unsigned()); + } + else { + sat::bool_var v = m_solver.mk_var(true); + sat::literal lit(v, sign); + m_ext->add_pb_ge(v, wlits, k.get_unsigned()); + TRACE("goal2sat", tout << "root: " << root << " lit: " << lit << "\n";); + m_result_stack.shrink(sz - t->get_num_args()); + m_result_stack.push_back(lit); + } + } + + void convert_pb_eq(app* t, bool root, bool sign) { + rational k = pb.get_k(t); + SASSERT(k.is_unsigned()); + svector wlits; + convert_pb_args(t, wlits); + sat::bool_var v1 = root ? sat::null_bool_var : m_solver.mk_var(true); + sat::bool_var v2 = root ? sat::null_bool_var : m_solver.mk_var(true); + m_ext->add_pb_ge(v1, wlits, k.get_unsigned()); + k.neg(); + for (unsigned i = 0; i < wlits.size(); ++i) { + wlits[i].second.neg(); + k += rational(wlits[i].first); + } + check_unsigned(k); + m_ext->add_pb_ge(v2, wlits, k.get_unsigned()); + if (root) { + m_result_stack.reset(); + } + else { + sat::literal l1(v1, false), l2(v2, false); + sat::bool_var v = m_solver.mk_var(); + sat::literal l(v, false); + mk_clause(~l, l1); + mk_clause(~l, l2); + mk_clause(~l1, ~l2, l); + m_result_stack.shrink(m_result_stack.size() - t->get_num_args()); + m_result_stack.push_back(l); + } + } + void convert_at_least_k(app* t, rational k, bool root, bool sign) { SASSERT(k.is_unsigned()); sat::literal_vector lits; @@ -529,16 +626,28 @@ struct goal2sat::imp { convert_at_least_k(t, pb.get_k(t), root, sign); break; case OP_PB_LE: - SASSERT(pb.has_unit_coefficients(t)); - convert_at_most_k(t, pb.get_k(t), root, sign); + if (pb.has_unit_coefficients(t)) { + convert_at_most_k(t, pb.get_k(t), root, sign); + } + else { + convert_pb_le(t, root, sign); + } break; case OP_PB_GE: - SASSERT(pb.has_unit_coefficients(t)); - convert_at_least_k(t, pb.get_k(t), root, sign); + if (pb.has_unit_coefficients(t)) { + convert_at_least_k(t, pb.get_k(t), root, sign); + } + else { + convert_pb_ge(t, root, sign); + } break; case OP_PB_EQ: - SASSERT(pb.has_unit_coefficients(t)); - convert_eq_k(t, pb.get_k(t), root, sign); + if (pb.has_unit_coefficients(t)) { + convert_eq_k(t, pb.get_k(t), root, sign); + } + else { + convert_pb_eq(t, root, sign); + } break; default: UNREACHABLE();