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();
 
  300                         assert(
false); 
break;
 
  306                         assert(
false); 
break;
 
  308                         return _subFormula.mpContent->mNegation;
 
  310                         assert(
false); 
break;
 
  314                         return _subFormula.mpContent;
 
  320                         assert(
false); 
break;
 
  323                         assert(
false); 
break;
 
  326                         assert(
false); 
break;
 
  346                         return createITE(std::move(_subformulas));
 
  354                         assert(
false); 
break;
 
  361                         return createNAry(_type, std::move(_subformulas));
 
  371                         assert(
false); 
break;
 
  420                 if( _subformulas.size() == 1 )
 
  422                     return _subformulas.begin()->mpContent;
 
  425                 auto lastSubFormula = _subformulas.begin();
 
  426                 auto subFormula = lastSubFormula;
 
  429                 while( subFormula != _subformulas.end() )
 
  431                     if( *lastSubFormula == *subFormula )
 
  437                         if( counter % 2 == 1 )
 
  439                             subFormulas.insert( subFormulas.end(), *lastSubFormula ); 
 
  441                         lastSubFormula = subFormula;
 
  446                 if( counter % 2 == 1 )
 
  448                     subFormulas.insert( subFormulas.end(), *lastSubFormula );
 
  455                 #ifdef SIMPLIFY_FORMULA 
  479                 assert( tmp->mUsages > 0 );
 
  481                 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);
 
  482                 if( tmp->mUsages == 1 )
 
  484                     CARL_LOG_DEBUG(
"carl.formula", 
"Actually freeing " << *tmp << 
" from pool");
 
  485                     bool stillStoredAsTseitinVariable = 
false;
 
  487                         stillStoredAsTseitinVariable = 
true;
 
  489                         stillStoredAsTseitinVariable = 
true;
 
  490                     if( !stillStoredAsTseitinVariable )
 
  492                         CARL_LOG_TRACE(
"carl.formula", 
"Deleting " << tmp << 
" / " << tmp->mNegation << 
" from pool");
 
  497                         assert(
mPool.find(*tmp->mNegation) == 
mPool.end());
 
  498                         auto it = 
mPool.find(*tmp);
 
  499                         assert(it != 
mPool.end());
 
  501                         delete tmp->mNegation;
 
  509                 bool stillStoredAsTseitinVariable = 
false;
 
  514                     if( tvIter->second->mUsages == 1 )
 
  521                         CARL_LOG_TRACE(
"carl.formula", 
"Deleting " << 
static_cast<const void*
>(tmp) << 
" / " << 
static_cast<const void*
>(tmp->
mNegation) << 
" from pool");
 
  527                         stillStoredAsTseitinVariable = 
true;
 
  543                             CARL_LOG_TRACE(
"carl.formula", 
"Deleting " << 
static_cast<const void*
>(tmp) << 
" / " << 
static_cast<const void*
>(tmp->
mNegation) << 
" from pool");
 
  549                             stillStoredAsTseitinVariable = 
true;
 
  552                 return stillStoredAsTseitinVariable;
 
  560                 assert( tmp != 
nullptr );
 
  561                 assert( tmp->mUsages < std::numeric_limits<size_t>::max() );
 
  564                     CARL_LOG_TRACE(
"carl.formula", 
"Is a constraint, increasing again");
 
  567                 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);
 
  571             template<
typename ArgType>
 
  614                     auto new_buckets = 
new typename underlying_set::bucket_type[rehash.second];
 
  615                     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
const FormulaContent< Pol > * create(FormulaType _type, std::vector< Variable > &&_vars, const Formula< Pol > &_aux_term, const Formula< Pol > &_term)
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.
const FormulaContent< Pol > * mNegation
The negation.
std::variant< carl::Variable, Constraint< Pol >, VariableComparison< Pol >, VariableAssignment< Pol >, BVConstraint, UEquality, Formula< Pol >, Formulas< Pol >, QuantifierContent< Pol >, AuxQuantifierContent< Pol > > mContent
The content of this formula.
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...