SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
ClauseChain.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include <smtrat-common/model.h>
5 
6 namespace smtrat {
7 namespace mcsat {
8 
9 
10 /**
11  * An explanation is either a single clause or a chain of clauses, satisfying the following properties:
12  * - If the clauses are inserted and propagated in the chain's order, at least the last clause should be conflicting.
13  * - Each conflicting clause is already conflicting after preceding clauses have been inserted and propagated.
14  * - Tseitin vars should be used so that a Tseitin variable C is equivalent to its corresponding subformula F (or at least C -> F).
15  */
16 class ClauseChain {
17  public:
18  struct Link {
19  private:
21  std::optional<FormulaT> mImpliedTseitinLiteral;
22 
23  public:
26 
27  Link(const FormulaT&& clause) :
28  mClause(clause), mImpliedTseitinLiteral(std::nullopt) {};
29 
30  bool isPropagating() const {
31  return mImpliedTseitinLiteral && (*mImpliedTseitinLiteral).type() != carl::FormulaType::FALSE;
32  }
33 
34  bool isConflicting() const {
35  return mImpliedTseitinLiteral && (*mImpliedTseitinLiteral).type() == carl::FormulaType::FALSE;
36  }
37 
38  bool isOptional() const {
39  return !mImpliedTseitinLiteral;
40  }
41 
42  const FormulaT& clause() const {
43  return mClause;
44  }
45 
47  assert(isPropagating());
48  return *mImpliedTseitinLiteral;
49  }
50 
51  friend std::ostream& operator<< (std::ostream& stream, const Link& link);
52  };
53 
54  using const_iterator = typename std::vector<Link>::const_iterator;
55 
56  friend std::ostream& operator<< (std::ostream& stream, const ClauseChain& chain);
57 
58  private:
59  std::vector<Link> mClauseChain;
60  std::unordered_set<FormulaT> mTseitinVars;
61 
62  bool isTseitinVar(const FormulaT& var) const {
63  return mTseitinVars.find(var) != mTseitinVars.end();
64  }
65 
66  public:
68 
69  /**
70  * Create a Tseitin variable for the given formula.
71  * The returned variable C should be used so that C <-> formula or at least ~formula -> ~C
72  */
74  FormulaT var = carl::FormulaPool<smtrat::Poly>::getInstance().createTseitinVar(formula);
75  mTseitinVars.insert(var);
76  return var;
77  }
78 
79  /**
80  * Append a clause that implies impliedTseitinLiteral under the current assignment.
81  * Note that impliedTseitinLiteral = ~v for a Tseitin variable v obtained via createTseitinVar.
82  */
83  void appendPropagating(const FormulaT&& clause, const FormulaT&& impliedTseitinLiteral) {
84  mClauseChain.emplace_back(std::move(clause), std::move(impliedTseitinLiteral));
85  }
86 
87  /**
88  * Append a conflicting clause (regarding the current assignment).
89  */
90  void appendConflicting(const FormulaT&& clause) {
91  mClauseChain.emplace_back(std::move(clause), FormulaT(carl::FormulaType::FALSE));
92  }
93 
94  /**
95  * Append an additional clause which is neither propagating nor conflicting. Can be used to
96  * pass additional knowledge.
97  */
98  void appendOptional(const FormulaT&& clause) {
99  mClauseChain.emplace_back(std::move(clause));
100  }
101 
102  /**
103  * @return A vector representing this chain.
104  */
105  const std::vector<Link>& chain() const {
106  return mClauseChain;
107  }
108 
109  /**
110  * @return A constant iterator to the beginning of this chain.
111  */
113  return mClauseChain.begin();
114  }
115 
116  /**
117  * @return A constant iterator to the end of this chain.
118  */
119  const_iterator end() const {
120  return mClauseChain.end();
121  }
122 
123  /**
124  * Performs resolution on the chain. Uses the last clause for resolution.
125  * @return A single clause conflicting under the current assignment.
126  */
127  FormulaT resolve() const;
128 
129  /**
130  * Transforms the clause chain into a formula (containing Tseitin variables).
131  */
132  FormulaT to_formula() const;
133 
134  /**
135  * Transforms a given Boolean conjunction over AND and OR to CNF via Tseitin-Transformation
136  * so that, if the input formula is conflicting under the current assignment, after all clauses
137  * in "implications" have been propagated in the given order, the returned formula evaluates to false.
138  */
139  static ClauseChain from_formula(const FormulaT& f, const Model& model, bool with_equivalence);
140 };
141 
142 inline std::ostream& operator<< (std::ostream& stream, const ClauseChain::Link& link) {
143  if (link.isOptional()) {
144  stream << link.clause();
145  } else if (link.isPropagating()) {
146  stream << link.clause() << " -> " << link.impliedTseitinLiteral();
147  } else if (link.isConflicting()) {
148  stream << link.clause() << " -> CONFLICT";
149  }
150  return stream;
151 }
152 
153 inline std::ostream& operator<< (std::ostream& stream, const ClauseChain& chain) {
154  stream << "[";
155  for (auto iter = chain.begin(); iter != chain.end(); iter++) {
156  stream << (*iter);
157  if (iter != chain.end()-1)
158  stream << ", ";
159  }
160  stream << "]" << std::endl;
161  return stream;
162 }
163 
164 }
165 }
An explanation is either a single clause or a chain of clauses, satisfying the following properties:
Definition: ClauseChain.h:16
std::unordered_set< FormulaT > mTseitinVars
Definition: ClauseChain.h:60
static ClauseChain from_formula(const FormulaT &f, const Model &model, bool with_equivalence)
Transforms a given Boolean conjunction over AND and OR to CNF via Tseitin-Transformation so that,...
FormulaT to_formula() const
Transforms the clause chain into a formula (containing Tseitin variables).
Definition: ClauseChain.cpp:62
FormulaT resolve() const
Performs resolution on the chain.
Definition: ClauseChain.cpp:5
void appendConflicting(const FormulaT &&clause)
Append a conflicting clause (regarding the current assignment).
Definition: ClauseChain.h:90
FormulaT createTseitinVar(const FormulaT &formula)
Create a Tseitin variable for the given formula.
Definition: ClauseChain.h:73
bool isTseitinVar(const FormulaT &var) const
Definition: ClauseChain.h:62
void appendPropagating(const FormulaT &&clause, const FormulaT &&impliedTseitinLiteral)
Append a clause that implies impliedTseitinLiteral under the current assignment.
Definition: ClauseChain.h:83
const_iterator end() const
Definition: ClauseChain.h:119
typename std::vector< Link >::const_iterator const_iterator
Definition: ClauseChain.h:54
std::vector< Link > mClauseChain
Definition: ClauseChain.h:59
const std::vector< Link > & chain() const
Definition: ClauseChain.h:105
const_iterator begin() const
Definition: ClauseChain.h:112
friend std::ostream & operator<<(std::ostream &stream, const ClauseChain &chain)
Definition: ClauseChain.h:153
void appendOptional(const FormulaT &&clause)
Append an additional clause which is neither propagating nor conflicting.
Definition: ClauseChain.h:98
int var(Lit p)
Definition: SolverTypes.h:64
std::ostream & operator<<(std::ostream &os, const Bookkeeping &bk)
Definition: Bookkeeping.h:107
Class to create the formulas for axioms.
carl::Formula< Poly > FormulaT
Definition: types.h:37
carl::Model< Rational, Poly > Model
Definition: model.h:13