Skip to content

Commit

Permalink
smt expr: add helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
nunoplopes committed Dec 28, 2024
1 parent 0c8b62a commit bb18e1b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
30 changes: 29 additions & 1 deletion smt/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ expr expr::cmp_eq(const expr &rhs, bool simplify) const {
return false;
return rhs == *this;
}
// constants on rhs from now.
// constants on rhs from now on.

if (rhs.isTrue())
return *this;
Expand Down Expand Up @@ -1649,6 +1649,14 @@ void expr::operator|=(const expr &rhs) {
*this = *this || rhs;
}

expr expr::mk_and(const vector<expr> &vals) {
expr ret(true);
for (auto &e : vals) {
ret &= e;
}
return ret;
}

expr expr::mk_and(const set<expr> &vals) {
expr ret(true);
for (auto &e : vals) {
Expand Down Expand Up @@ -2235,6 +2243,12 @@ expr expr::subst(const vector<pair<expr, expr>> &repls) const {
return Z3_substitute(ctx(), ast(), repls.size(), from.get(), to.get());
}

expr expr::subst_simplify(const vector<pair<expr, expr>> &repls) const {
if (repls.empty())
return *this;
return subst(repls).simplify();
}

expr expr::subst(const expr &from, const expr &to) const {
C(from, to);
auto f = from();
Expand All @@ -2248,6 +2262,20 @@ expr expr::subst_var(const expr &repl) const {
return Z3_substitute_vars(ctx(), ast(), 1, &r);
}

expr expr::propagate(const AndExpr &constraints) const {
C();
auto from = make_unique<Z3_ast[]>(constraints.exprs.size());
auto to = make_unique<Z3_ast[]>(constraints.exprs.size());
expr true_expr(true);
unsigned i = 0;
for (auto &e : constraints.exprs) {
C2(e);
from[i] = e();
to[i++] = true_expr();
}
return Z3_substitute(ctx(), ast(), i, from.get(), to.get());
}

set<expr> expr::vars() const {
return vars({ this });
}
Expand Down
7 changes: 7 additions & 0 deletions smt/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ typedef struct _Z3_sort* Z3_sort;

namespace smt {

class AndExpr;

class expr {
uintptr_t ptr = 0;

Expand Down Expand Up @@ -288,6 +290,7 @@ class expr {
void operator&=(const expr &rhs);
void operator|=(const expr &rhs);

static expr mk_and(const std::vector<expr> &vals);
static expr mk_and(const std::set<expr> &vals);
static expr mk_or(const std::set<expr> &vals);

Expand Down Expand Up @@ -363,11 +366,15 @@ class expr {

// replace v1 -> v2
expr subst(const std::vector<std::pair<expr, expr>> &repls) const;
expr subst_simplify(const std::vector<std::pair<expr, expr>> &repls) const;
expr subst(const expr &from, const expr &to) const;

// replace the 1st quantified variable
expr subst_var(const expr &repl) const;

// turn all expressions in 'constraints' into true
expr propagate(const AndExpr &constraints) const;

std::set<expr> vars() const;
static std::set<expr> vars(const std::vector<const expr*> &exprs);

Expand Down
7 changes: 7 additions & 0 deletions smt/exprs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "smt/exprs.h"
#include "smt/smt.h"
#include "util/compiler.h"
#include <algorithm>
#include <vector>

using namespace std;
Expand Down Expand Up @@ -45,6 +46,12 @@ void AndExpr::del(const AndExpr &other) {
exprs.erase(e);
}

expr AndExpr::propagate(const AndExpr &other) const {
vector<expr> ret;
ranges::set_difference(exprs, other.exprs, back_inserter(ret), less{});
return expr::mk_and(ret).propagate(other);
}

void AndExpr::reset() {
exprs.clear();
}
Expand Down
2 changes: 2 additions & 0 deletions smt/exprs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class AndExpr {
void add(expr &&e, unsigned limit = 16);
void add(const AndExpr &other);
void del(const AndExpr &other);
expr propagate(const AndExpr &other) const;
void reset();
bool contains(const expr &e) const;
expr operator()() const;
Expand All @@ -37,6 +38,7 @@ class AndExpr {
auto operator<=>(const AndExpr&) const = default;
friend std::ostream &operator<<(std::ostream &os, const AndExpr &e);
template<typename T> friend class DisjointExpr;
friend class expr;
};


Expand Down

0 comments on commit bb18e1b

Please sign in to comment.