15 #include <boost/variant.hpp>
16 #include "../bitvector/BVConstraintPool.h"
17 #include "../bitvector/BVConstraint.h"
18 #include <boost/intrusive/unordered_set.hpp>
20 #define SIMPLIFY_FORMULA
25 template<
typename Pol>
31 template<
typename Pol>
49 std::unique_ptr<typename underlying_set::bucket_type[]>
mPoolBuckets;
54 mutable std::recursive_mutex mMutexPool;
63 #define FORMULA_POOL_LOCK_GUARD std::lock_guard<std::recursive_mutex> lock( mMutexPool );
64 #define FORMULA_POOL_LOCK mMutexPool.lock();
65 #define FORMULA_POOL_UNLOCK mMutexPool.unlock();
67 #define FORMULA_POOL_LOCK_GUARD
68 #define FORMULA_POOL_LOCK
69 #define FORMULA_POOL_UNLOCK
99 std::cout <<
"Formula pool contains:" << std::endl;
100 for (
const auto& ele:
mPool) {
101 std::cout << ele->mId <<
" @ " <<
static_cast<const void*
>(ele) <<
" [usages=" << ele->mUsages <<
"]: " << *ele <<
", negation " <<
static_cast<const void*
>(ele->mNegation) << std::endl;
103 std::cout <<
"Tseitin variables:" << std::endl;
106 if( tvVar.second !=
nullptr )
108 std::cout <<
"id " << tvVar.first->mId <<
" -> " << tvVar.second->mId <<
" [remapping: ";
113 std::cout << iter->first->mId <<
" -> " << iter->second->first->mId <<
"]" << std::endl;
116 std::cout <<
"not yet remapped!" << std::endl;
119 std::cout <<
"id " << tvVar.first->mId <<
" -> nullptr" << std::endl;
121 std::cout << std::endl;
140 iter.first->second = hi;
169 return std::get<UEquality>(f->
mContent) < std::get<UEquality>(f->
mNegation->mContent);
176 assert(f !=
nullptr);
178 CARL_LOG_TRACE(
"carl.formula",
"Base formula of " <<
static_cast<const void*
>(f) <<
" / " << *f <<
" is " << *f->
mNegation);
183 CARL_LOG_TRACE(
"carl.formula",
"Base formula of " <<
static_cast<const void*
>(f) <<
" / " << *f <<
" is " << *f);
186 CARL_LOG_TRACE(
"carl.formula",
"Base formula of " <<
static_cast<const void*
>(f) <<
" / " << *f <<
" is " << *f->
mNegation);
190 CARL_LOG_TRACE(
"carl.formula",
"Base formula of " <<
static_cast<const void*
>(f) <<
" / " << *f <<
" is " << *f);
237 #ifdef SIMPLIFY_FORMULA
238 switch (_constraint.is_consistent()) {
277 #ifdef SIMPLIFY_FORMULA
278 if (_constraint.isAlwaysConsistent())
return trueFormula();
279 if (_constraint.isAlwaysInconsistent())
return falseFormula();
299 assert(
false);
break;
305 assert(
false);
break;
307 return _subFormula.mpContent->mNegation;
309 assert(
false);
break;
313 return _subFormula.mpContent;
319 assert(
false);
break;
322 assert(
false);
break;
325 assert(
false);
break;
345 return createITE(std::move(_subformulas));
353 assert(
false);
break;
360 return createNAry(_type, std::move(_subformulas));
370 assert(
false);
break;
409 if( _subformulas.size() == 1 )
411 return _subformulas.begin()->mpContent;
414 auto lastSubFormula = _subformulas.begin();
415 auto subFormula = lastSubFormula;
418 while( subFormula != _subformulas.end() )
420 if( *lastSubFormula == *subFormula )
426 if( counter % 2 == 1 )
428 subFormulas.insert( subFormulas.end(), *lastSubFormula );
430 lastSubFormula = subFormula;
435 if( counter % 2 == 1 )
437 subFormulas.insert( subFormulas.end(), *lastSubFormula );
444 #ifdef SIMPLIFY_FORMULA
468 assert( tmp->mUsages > 0 );
470 CARL_LOG_TRACE(
"carl.formula",
"Usage of " <<
static_cast<const void*
>(tmp) <<
" / " <<
static_cast<const void*
>(tmp->mNegation) <<
" (coming from " <<
static_cast<const void*
>(_elem) <<
"): " << tmp->mUsages);
471 if( tmp->mUsages == 1 )
473 CARL_LOG_DEBUG(
"carl.formula",
"Actually freeing " << *tmp <<
" from pool");
474 bool stillStoredAsTseitinVariable =
false;
476 stillStoredAsTseitinVariable =
true;
478 stillStoredAsTseitinVariable =
true;
479 if( !stillStoredAsTseitinVariable )
481 CARL_LOG_TRACE(
"carl.formula",
"Deleting " << tmp <<
" / " << tmp->mNegation <<
" from pool");
486 assert(
mPool.find(*tmp->mNegation) ==
mPool.end());
487 auto it =
mPool.find(*tmp);
488 assert(it !=
mPool.end());
490 delete tmp->mNegation;
498 bool stillStoredAsTseitinVariable =
false;
503 if( tvIter->second->mUsages == 1 )
510 CARL_LOG_TRACE(
"carl.formula",
"Deleting " <<
static_cast<const void*
>(tmp) <<
" / " <<
static_cast<const void*
>(tmp->
mNegation) <<
" from pool");
516 stillStoredAsTseitinVariable =
true;
532 CARL_LOG_TRACE(
"carl.formula",
"Deleting " <<
static_cast<const void*
>(tmp) <<
" / " <<
static_cast<const void*
>(tmp->
mNegation) <<
" from pool");
538 stillStoredAsTseitinVariable =
true;
541 return stillStoredAsTseitinVariable;
549 assert( tmp !=
nullptr );
550 assert( tmp->mUsages < std::numeric_limits<size_t>::max() );
553 CARL_LOG_TRACE(
"carl.formula",
"Is a constraint, increasing again");
556 CARL_LOG_TRACE(
"carl.formula",
"Increased usage of " <<
static_cast<const void*
>(tmp) <<
" / " <<
static_cast<const void*
>(tmp->mNegation) <<
"(based on " <<
static_cast<const void*
>(_elem) <<
")" <<
" to " << tmp->mUsages);
560 template<
typename ArgType>
603 auto new_buckets =
new typename underlying_set::bucket_type[rehash.second];
604 mPool.rehash(
typename underlying_set::bucket_traits(new_buckets, rehash.second));
#define FORMULA_POOL_LOCK_GUARD
#define CARL_LOG_TRACE(channel, msg)
#define CARL_LOG_DEBUG(channel, msg)
carl is the main namespace for the library.
Coeff content(const UnivariatePolynomial< Coeff > &p)
The content of a polynomial is the gcd of the coefficients of the normal part of a polynomial.
FormulaType
Represent the type of a formula to allow faster/specialized processing.
std::size_t hash_value(const carl::Monomial &monomial)
std::optional< BasicConstraint< Poly > > as_constraint(const VariableComparison< Poly > &f)
Convert this variable comparison "v < root(..)" into a simpler polynomial (in)equality against zero "...
std::vector< Formula< Poly > > Formulas
void visit(const Formula< Pol > &formula, Visitor func)
Recursively calls func on every subformula.
Variable fresh_boolean_variable() noexcept
std::multiset< Formula< Poly > > FormulasMulti
std::unordered_map< const T1 *, T2, pointerHash< T1 >, pointerEqual< T1 > > FastPointerMap
auto & get(const std::string &name)
A Variable represents an algebraic variable that can be used throughout carl.
VariableAssignment negation() const
Represent a sum type/variant of an (in)equality between a variable on the left-hand side and multivar...
VariableComparison negation() const
Mimics stdlibs default rehash policy for hashtables.
std::pair< bool, std::size_t > needRehash(std::size_t numBuckets, std::size_t numElements) const
Base class that implements a singleton.
Represent a polynomial (in)equality against zero.
Constraint negation() const
Represent an SMT formula, which can be an atom for some background theory or a boolean combination of...
const FormulaContent< Pol > * mpContent
The content of this formula.
const FormulaContent< Pol > * falseFormula() const
const FormulaContent< Pol > * create(FormulaType _type, Formulas< Pol > &&_subformulas)
const FormulaContent< Pol > * create(FormulaType _type)
Create formula representing a boolean value.
const FormulaContent< Pol > * createNAry(FormulaType _type, Formulas< Pol > &&_subformulas)
const FormulaContent< Pol > * add(FormulaContent< Pol > &&_formula)
Adds the given formula to the pool, if it does not yet occur in there.
Formula< Pol > getTseitinVar(const Formula< Pol > &_formula)
const FormulaContent< Pol > * create(const BVConstraint &_constraint)
FormulaPool(unsigned _capacity=10000)
Constructor of the formula pool.
const FormulaContent< Pol > * create(const VariableAssignment< Pol > &_variableAssignment)
const FormulaContent< Pol > * trueFormula() const
bool isBaseFormula(const FormulaContent< Pol > *f) const
FormulaContent< Pol > * createNegatedContent(const FormulaContent< Pol > *f) const
const FormulaContent< Pol > * create(UEquality &&eq)
bool formulasInverse(const Formula< Pol > &_subformulaA, const Formula< Pol > &_subformulaB)
const FormulaContent< Pol > * create(const UTerm &_lhs, const UTerm &_rhs, bool _negated)
void reg(const FormulaContent< Pol > *_elem) const
const FormulaContent< Pol > * create(const VariableComparison< Pol > &_variableComparison)
const FormulaContent< Pol > * create(FormulaType _type, const std::initializer_list< Formula< Pol >> &_subformulas)
FormulaContent< Pol > * mpFalse
The unique formula representing false.
const FormulaContent< Pol > * create(FormulaType _type, std::vector< Variable > &&_vars, const Formula< Pol > &_term)
Formula< Pol > createTseitinVar(const Formula< Pol > &_formula)
void free(const FormulaContent< Pol > *_elem)
const FormulaContent< Pol > * create(Constraint< Pol > &&_constraint)
const FormulaContent< Pol > * create(const FormulasMulti< Pol > &_subformulas)
const FormulaContent< Pol > * create(FormulaType _type, const Formulas< Pol > &_subformulas)
Create formula representing a nary function.
const FormulaContent< Pol > * getBaseFormula(const FormulaContent< Pol > *f) const
const FormulaContent< Pol > * create(const Constraint< Pol > &_constraint)
void forallDo(void(*_func)(ArgType *, const Formula< Pol > &), ArgType *_arg) const
bool isBaseFormula(const VariableComparison< Pol > &vc) const
unsigned mIdAllocator
id allocator
FastPointerMap< FormulaContent< Pol >, typename FastPointerMap< FormulaContent< Pol >, const FormulaContent< Pol > * >::iterator > mTseitinVarToFormula
const FormulaContent< Pol > * createITE(Formulas< Pol > &&_subformulas)
bool isBaseFormula(const Constraint< Pol > &c) const
bool isBaseFormula(const VariableAssignment< Pol > &va) const
std::unique_ptr< typename underlying_set::bucket_type[]> mPoolBuckets
FormulaContent< Pol > * mpTrue
The unique formula representing true.
pool::RehashPolicy mRehashPolicy
const FormulaContent< Pol > * create(VariableAssignment< Pol > &&_variableAssignment)
const FormulaContent< Pol > * create(BVConstraint &&_constraint)
const FormulaContent< Pol > * create(FormulaType _type, Formula< Pol > &&_subFormula)
Create formula representing a unary function.
bool freeTseitinVariable(const FormulaContent< Pol > *_toDelete)
FastPointerMap< FormulaContent< Pol >, const FormulaContent< Pol > * > mTseitinVars
boost::intrusive::unordered_set< FormulaContent< Pol > > underlying_set
const FormulaContent< Pol > * create(VariableComparison< Pol > &&_variableComparison)
const FormulaContent< Pol > * createImplication(Formulas< Pol > &&_subformulas)
Create formula representing an implication.
const FormulaContent< Pol > * create(Variable _variable)
Create formula representing a boolean variable.
underlying_set mPool
The formula pool.
bool isBaseFormula(const UEquality &ueq) const
FormulaType mType
The type of this formula.
size_t mUsages
The number of formulas existing with this content.
std::variant< carl::Variable, Constraint< Pol >, VariableComparison< Pol >, VariableAssignment< Pol >, BVConstraint, UEquality, Formula< Pol >, Formulas< Pol >, QuantifierContent< Pol > > mContent
The content of this formula.
const FormulaContent< Pol > * mNegation
The negation.
Implements an uninterpreted equality, that is an equality of either two uninterpreted function instan...
Implements an uninterpreted term, that is either an uninterpreted variable or an uninterpreted functi...