SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
ModuleInput.h
Go to the documentation of this file.
1 /**
2  * @file ModuleInput.h
3  * @author Florian Corzilius
4  * @version 2014-05-16
5  */
6 
7 #pragma once
8 
9 
10 #include <algorithm>
11 #include <list>
12 #include <vector>
13 #include <set>
14 #include <iterator>
16 #include <smtrat-common/model.h>
17 
18 namespace smtrat
19 {
20  /// Stores a formula along with its origins.
22  {
23  friend class ModuleInput;
24  // Member
25 
26  /// The formula.
28  /// The formulas origins.
29  std::shared_ptr<FormulasT> mOrigins;
30  /// The deduction flag, which indicates, that this formula g is a direct sub-formula of
31  /// a conjunction of formulas (and g f_1 .. f_n), and, that (implies (and f_1 .. f_n) g) holds.
32  mutable bool mDeducted;
33 
34  public:
35 
36  FormulaWithOrigins(); // Default constructor disabled.
37 
38  /**
39  * Constructs a formula with empty origins.
40  * @param _formula The formula of the formula with origins to construct.
41  */
42  FormulaWithOrigins( const FormulaT& _formula ):
43  mFormula( _formula ),
44  mOrigins(nullptr),
45  mDeducted( false )
46  {}
47 
48  /**
49  * Constructs a formula with the given origins.
50  * @param _formula The formula of the formula with origins to construct.
51  * @param _origins The origins of the formula with origins to construct.
52  */
53  FormulaWithOrigins( const FormulaT& _formula, const std::shared_ptr<FormulasT>& _origins ):
54  mFormula( _formula ),
55  mOrigins( _origins ),
56  mDeducted( false )
57  {}
58 
59  FormulaWithOrigins( const FormulaWithOrigins& ); // Copy constructor disabled.
60 
61  /**
62  * @param _fwoA The first formula with origins to compare.
63  * @param _fwoB The second formula with origins to compare.
64  * @return true, if the formula of the first formula with origins is less than the formula of the second formula with origins;
65  * false, otherwise.
66  */
67  friend bool operator<( const FormulaWithOrigins& _fwoA, const FormulaWithOrigins& _fwoB )
68  {
69  return _fwoA.formula() < _fwoB.formula();
70  }
71 
72  /**
73  * @param _fwoA The first formula with origins to compare.
74  * @param _fwoB The second formula with origins to compare.
75  * @return true, if the formula of the first formula with origins and the formula of the second formula with origins are equal;
76  * false, otherwise.
77  */
78  friend bool operator==( const FormulaWithOrigins& _fwoA, const FormulaWithOrigins& _fwoB )
79  {
80  return _fwoA.formula() == _fwoB.formula();
81  }
82 
83  /**
84  * @return A constant reference to the formula.
85  */
86  const FormulaT& formula() const
87  {
88  return mFormula;
89  }
90 
91  /**
92  * @return true, if this sub-formula of the module input has origins.
93  */
94  bool hasOrigins() const
95  {
96  return mOrigins != nullptr;
97  }
98 
99  /**
100  * @return A constant reference to the origins.
101  */
102  const FormulasT& origins() const
103  {
104  return *mOrigins;
105  }
106 
107  /**
108  * Sets the deduction flag to the given value.
109  * @param _deducted The value to set the deduction flag to.
110  */
111  void setDeducted( bool _deducted ) const
112  {
113  mDeducted = _deducted;
114  }
115 
116  /**
117  * @return The deduction flag, which indicates, that this formula g is a direct sub-formula of
118  * a conjunction of formulas (and g f_1 .. f_n), and, that (implies (and f_1 .. f_n) g) holds.
119  */
120  bool deducted() const
121  {
122  return mDeducted;
123  }
124  };
125 
126  /**
127  * The input formula a module has to consider for it's satisfiability check. It is a list of formulas
128  * and semantically considered as their conjunction.
129  */
130  class ModuleInput : private std::list<FormulaWithOrigins>
131  {
132  friend class Module;
133 
134  friend class Manager;
135 
136  private:
137 
138  typedef std::list<FormulaWithOrigins> super;
139 
140 
141  public:
142 
143  /// Passing through the list::iterator.
144  typedef super::iterator iterator;
145  /// Passing through the list::const_iterator.
146  typedef super::const_iterator const_iterator;
147 
148  private:
149 
150  // Member.
151  /// Store some properties about the conjunction of the stored formulas.
152  carl::Condition mProperties;
153  /// A flag indicating whether the properties of this module input are updated.
155  /// Maps all formulas occurring (in the origins) at pos i in this module input to i. This is for a faster access.
156  carl::FastMap<FormulaT,iterator> mFormulaPositionMap;
157 
158  public:
159 
160  /**
161  * Constructs a module input.
162  */
164  std::list<FormulaWithOrigins>(),
165  mProperties(),
166  mPropertiesUpdated(false),
168  {}
169 
170  // Methods.
171 
172  /**
173  * @return All known properties of the underlying formula of this module input.
174  */
175  carl::Condition properties() const
176  {
177  assert( mPropertiesUpdated );
178  return mProperties;
179  }
180 
181  /**
182  * @return true, if this formula is a conjunction of constraints;
183  * false, otherwise.
184  */
186  {
187  if( carl::PROP_IS_PURE_CONJUNCTION <= mProperties )
188  return !(carl::PROP_CONTAINS_BOOLEAN <= mProperties) && !(carl::PROP_CONTAINS_UNINTERPRETED_EQUATIONS <= mProperties);
189  else
190  return false;
191  }
192 
193  /**
194  * @return true, if this formula is a conjunction of literals of constraints;
195  * false, otherwise.
196  */
198  {
199  if( carl::PROP_IS_LITERAL_CONJUNCTION <= mProperties )
200  return !(carl::PROP_CONTAINS_BOOLEAN <= mProperties) && !(carl::PROP_CONTAINS_UNINTERPRETED_EQUATIONS <= mProperties);
201  else
202  return false;
203  }
204 
205  /**
206  * @return true, if this formula is a conjunction of real constraints;
207  * false, otherwise.
208  */
210  {
211  return is_constraint_conjunction() && !(carl::PROP_CONTAINS_INTEGER_VALUED_VARS <= mProperties) && !(carl::PROP_CONTAINS_PSEUDOBOOLEAN <= mProperties);
212  }
213 
214  /**
215  * @return true, if this formula is a conjunction of literals of real constraints;
216  * false, otherwise.
217  */
219  {
220  return isConstraintLiteralConjunction() && !(carl::PROP_CONTAINS_INTEGER_VALUED_VARS <= mProperties);
221  }
222 
223  /**
224  * @return true, if this formula is a conjunction of integer constraints;
225  * false, otherwise.
226  */
228  {
229  return is_constraint_conjunction() && !(carl::PROP_CONTAINS_REAL_VALUED_VARS <= mProperties);
230  }
231 
232  /**
233  * @return true, if this formula is a conjunction of literals of integer constraints;
234  * false, otherwise.
235  */
237  {
238  return isConstraintLiteralConjunction() && !(carl::PROP_CONTAINS_REAL_VALUED_VARS <= mProperties);
239  }
240 
241  /**
242  * @return true, if this formula contains bit vector constraints;
243  * false, otherwise.
244  */
246  {
247  return carl::PROP_CONTAINS_BITVECTOR <= mProperties;
248  }
249 
250  /**
251  * @return true, if this formula contains Boolean variables;
252  * false, otherwise.
253  */
255  {
256  return carl::PROP_CONTAINS_BOOLEAN <= mProperties;
257  }
258 
259  /**
260  * @return true, if this formula contains Boolean variables;
261  * false, otherwise.
262  */
264  {
265  return carl::PROP_CONTAINS_REAL_VALUED_VARS <= mProperties;
266  }
267 
268  /**
269  * @return true, if this formula contains Boolean variables;
270  * false, otherwise.
271  */
273  {
274  return carl::PROP_CONTAINS_INTEGER_VALUED_VARS <= mProperties;
275  }
276 
277  /**
278  * @return true, if this formula contains uninterpreted equations;
279  * false, otherwise.
280  */
282  {
283  return carl::PROP_CONTAINS_UNINTERPRETED_EQUATIONS <= mProperties;
284  }
285 
286  /**
287  * @return true, if this formula is propositional;
288  * false, otherwise.
289  */
291  {
292  return !(carl::PROP_CONTAINS_BITVECTOR <= mProperties)
293  && !(carl::PROP_CONTAINS_UNINTERPRETED_EQUATIONS <= mProperties)
294  && !(carl::PROP_CONTAINS_INTEGER_VALUED_VARS <= mProperties)
295  && !(carl::PROP_CONTAINS_REAL_VALUED_VARS <= mProperties)
296  && !(carl::PROP_CONTAINS_PSEUDOBOOLEAN <= properties());
297  }
298 
299  /**
300  * @param _assignment The model to check conjunction of the stored formulas against.
301  * @return 1, if the conjunction of the stored formulas is satisfied by the given model;
302  * 0, if the given model conflicts the conjunction of the stored formulas;
303  * 2, if it cannot be determined cheaply, whether the given model conflicts or satisfies
304  * the conjunction of the stored formulas.
305  */
306  unsigned satisfiedBy( const Model& _assignment ) const;
307 
308  /**
309  * @param _assignment The assignment to check conjunction of the stored formulas against.
310  * @return 1, if the conjunction of the stored formulas is satisfied by the given assignment;
311  * 0, if the given assignment conflicts the conjunction of the stored formulas;
312  * 2, if it cannot be determined cheaply, whether the given assignment conflicts or satisfies
313  * the conjunction of the stored formulas.
314  */
315  unsigned satisfiedBy( const RationalAssignment& _assignment ) const;
316 
317  const auto& back() const {
318  return super::back();
319  }
320 
322  {
323  return super::begin();
324  }
325 
327  {
328  return super::end();
329  }
330 
332  {
333  return super::begin();
334  }
335 
337  {
338  return super::end();
339  }
340 
341  auto rbegin() const {
342  return super::rbegin();
343  }
344 
345  auto rend() const {
346  return super::rend();
347  }
348 
349  bool empty() const
350  {
351  return super::empty();
352  }
353 
354  size_t size() const
355  {
356  return super::size();
357  }
358 
359  iterator find( const FormulaT& _formula );
360 
361  const_iterator find( const FormulaT& _formula ) const;
362 
363  iterator find( const_iterator _hint, const FormulaT& _formula );
364 
365  const_iterator find( const_iterator _hint, const FormulaT& _formula ) const;
366 
367  /**
368  * @param _subformula The formula for which to check whether it is one of the stored formulas.
369  * @return true, if the given formula is one of the stored formulas;
370  * false, otherwise.
371  */
372  bool contains( const FormulaT& _subformula ) const
373  {
374  return mFormulaPositionMap.find( _subformula ) != mFormulaPositionMap.end();
375  }
376 
377  /**
378  * Updates all properties of the formula underlying this module input.
379  */
380  void updateProperties();
381 
382  void gatherVariables(carl::carlVariables& vars) const {
383  for (const auto& f: *this) {
384  carl::variables(f.formula(), vars);
385  }
386  }
387 
389  {
391  {
392  return (*i1) < (*i2);
393  }
394  };
395 
396  explicit operator FormulaT() const
397  {
398  FormulasT subFormulas;
399  for( auto& fwo : *this )
400  subFormulas.push_back( fwo.formula() );
401  return FormulaT( carl::FormulaType::AND, subFormulas );
402  }
403 
404  void addOrigin( iterator _formula, const FormulaT& _origin )
405  {
406  assert( _formula != end() );
407  if( !_formula->hasOrigins() )
408  {
409  _formula->mOrigins = std::shared_ptr<FormulasT>( new FormulasT() );
410  }
411  _formula->mOrigins->push_back( _origin );
412  }
413 
414  // @todo: we want a const_iterator here, but gcc 4.8 doesn't allow us :( even though it should
415  iterator erase( iterator _formula );
416 
417  void clearOrigins( iterator _formula )
418  {
419  assert( _formula != end() );
420  if( _formula->hasOrigins() )
421  {
422  _formula->mOrigins = nullptr;
423  }
424  }
425 
426  bool removeOrigin( iterator _formula, const FormulaT& _origin );
427 
428  bool removeOrigins( iterator _formula, const std::shared_ptr<FormulasT>& _origins );
429 
430  std::pair<iterator,bool> add( const FormulaT& _formula, bool _mightBeConjunction = true )
431  {
432  return add( _formula, false, FormulaT( carl::FormulaType::FALSE ), nullptr, _mightBeConjunction );
433  }
434 
435  std::pair<iterator,bool> add( const FormulaT& _formula, const FormulaT& _origins, bool _mightBeConjunction = true )
436  {
437  return add( _formula, true, _origins, nullptr, _mightBeConjunction );
438  }
439 
440  std::pair<iterator,bool> add( const FormulaT& _formula, const std::shared_ptr<FormulasT>& _origins, bool _mightBeConjunction = true )
441  {
442  return add( _formula, false, FormulaT( carl::FormulaType::FALSE ), _origins, _mightBeConjunction );
443  }
444 
445  std::pair<iterator,bool> add( const FormulaT& _formula, bool _hasSingleOrigin, const FormulaT& _origin, const std::shared_ptr<FormulasT>& _origins, bool _mightBeConjunction = true );
446  };
447 
448  inline std::ostream& operator<<(std::ostream& os, const ModuleInput& mi) {
449  os << "(and";
450  for (const auto& fwo : mi) os << " " << fwo.formula();
451  os << ")";
452  return os;
453  }
454 
455  template<typename AnnotationType>
456  void annotateFormula( const FormulaT&, const std::vector<AnnotationType>& );
457 } // namespace smtrat
Stores a formula along with its origins.
Definition: ModuleInput.h:22
FormulaWithOrigins(const FormulaWithOrigins &)
std::shared_ptr< FormulasT > mOrigins
The formulas origins.
Definition: ModuleInput.h:29
const FormulaT & formula() const
Definition: ModuleInput.h:86
bool mDeducted
The deduction flag, which indicates, that this formula g is a direct sub-formula of a conjunction of ...
Definition: ModuleInput.h:32
friend bool operator==(const FormulaWithOrigins &_fwoA, const FormulaWithOrigins &_fwoB)
Definition: ModuleInput.h:78
FormulaWithOrigins(const FormulaT &_formula)
Constructs a formula with empty origins.
Definition: ModuleInput.h:42
void setDeducted(bool _deducted) const
Sets the deduction flag to the given value.
Definition: ModuleInput.h:111
friend bool operator<(const FormulaWithOrigins &_fwoA, const FormulaWithOrigins &_fwoB)
Definition: ModuleInput.h:67
FormulaT mFormula
The formula.
Definition: ModuleInput.h:27
const FormulasT & origins() const
Definition: ModuleInput.h:102
FormulaWithOrigins(const FormulaT &_formula, const std::shared_ptr< FormulasT > &_origins)
Constructs a formula with the given origins.
Definition: ModuleInput.h:53
Base class for solvers.
Definition: Manager.h:34
The input formula a module has to consider for it's satisfiability check.
Definition: ModuleInput.h:131
super::const_iterator const_iterator
Passing through the list::const_iterator.
Definition: ModuleInput.h:146
void addOrigin(iterator _formula, const FormulaT &_origin)
Definition: ModuleInput.h:404
bool containsBooleanVariables() const
Definition: ModuleInput.h:254
bool is_real_constraint_conjunction() const
Definition: ModuleInput.h:209
auto rbegin() const
Definition: ModuleInput.h:341
bool isConstraintLiteralConjunction() const
Definition: ModuleInput.h:197
bool containsUninterpretedEquations() const
Definition: ModuleInput.h:281
void gatherVariables(carl::carlVariables &vars) const
Definition: ModuleInput.h:382
bool is_only_propositional() const
Definition: ModuleInput.h:290
carl::Condition mProperties
Store some properties about the conjunction of the stored formulas.
Definition: ModuleInput.h:152
auto rend() const
Definition: ModuleInput.h:345
iterator find(const FormulaT &_formula)
Definition: ModuleInput.cpp:46
bool removeOrigins(iterator _formula, const std::shared_ptr< FormulasT > &_origins)
void updateProperties()
Updates all properties of the formula underlying this module input.
bool contains(const FormulaT &_subformula) const
Definition: ModuleInput.h:372
bool containsIntegerVariables() const
Definition: ModuleInput.h:272
const_iterator end() const
Definition: ModuleInput.h:336
void clearOrigins(iterator _formula)
Definition: ModuleInput.h:417
std::pair< iterator, bool > add(const FormulaT &_formula, const std::shared_ptr< FormulasT > &_origins, bool _mightBeConjunction=true)
Definition: ModuleInput.h:440
bool containsRealVariables() const
Definition: ModuleInput.h:263
std::pair< iterator, bool > add(const FormulaT &_formula, const FormulaT &_origins, bool _mightBeConjunction=true)
Definition: ModuleInput.h:435
unsigned satisfiedBy(const Model &_assignment) const
Definition: ModuleInput.cpp:33
bool isIntegerConstraintLiteralConjunction() const
Definition: ModuleInput.h:236
carl::Condition properties() const
Definition: ModuleInput.h:175
const_iterator begin() const
Definition: ModuleInput.h:331
carl::FastMap< FormulaT, iterator > mFormulaPositionMap
Maps all formulas occurring (in the origins) at pos i in this module input to i. This is for a faster...
Definition: ModuleInput.h:156
bool is_integer_constraint_conjunction() const
Definition: ModuleInput.h:227
std::pair< iterator, bool > add(const FormulaT &_formula, bool _hasSingleOrigin, const FormulaT &_origin, const std::shared_ptr< FormulasT > &_origins, bool _mightBeConjunction=true)
std::list< FormulaWithOrigins > super
Definition: ModuleInput.h:138
bool mPropertiesUpdated
A flag indicating whether the properties of this module input are updated.
Definition: ModuleInput.h:154
const auto & back() const
Definition: ModuleInput.h:317
bool containsBitVectorConstraints() const
Definition: ModuleInput.h:245
iterator erase(iterator _formula)
Definition: ModuleInput.cpp:98
size_t size() const
Definition: ModuleInput.h:354
bool removeOrigin(iterator _formula, const FormulaT &_origin)
bool empty() const
Definition: ModuleInput.h:349
super::iterator iterator
Passing through the list::iterator.
Definition: ModuleInput.h:144
ModuleInput()
Constructs a module input.
Definition: ModuleInput.h:163
bool is_constraint_conjunction() const
Definition: ModuleInput.h:185
bool isRealConstraintLiteralConjunction() const
Definition: ModuleInput.h:218
std::pair< iterator, bool > add(const FormulaT &_formula, bool _mightBeConjunction=true)
Definition: ModuleInput.h:430
A base class for all kind of theory solving methods.
Definition: Module.h:70
std::vector< carl::Variable > vars(const std::pair< QuantifierType, std::vector< carl::Variable >> &p)
Definition: QEQuery.h:43
Class to create the formulas for axioms.
carl::Formula< Poly > FormulaT
Definition: types.h:37
carl::Assignment< Rational > RationalAssignment
Definition: types.h:45
carl::Model< Rational, Poly > Model
Definition: model.h:13
std::ostream & operator<<(std::ostream &os, CMakeOptionPrinter cmop)
carl::Formulas< Poly > FormulasT
Definition: types.h:39
void annotateFormula(const FormulaT &, const std::vector< AnnotationType > &)
bool operator()(const_iterator i1, const_iterator i2) const
Definition: ModuleInput.h:390