The source code and dockerfile for the GSW2024 AI Lab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

213 lines
5.2 KiB

2 months ago
  1. %module FormulaClasses
  2. %{
  3. #include <carl/formula/Formula.h>
  4. #include <carl/formula/Constraint.h>
  5. #include <carl/core/SimpleConstraint.h>
  6. #include "gmpxx.h"
  7. #include <carl/core/RationalFunction.h>
  8. #include <carl/core/Relation.h>
  9. typedef mpq_class Rational;
  10. typedef carl::Monomial::Arg Monomial;
  11. typedef carl::Term<Rational> Term;
  12. typedef carl::MultivariatePolynomial<Rational> Polynomial;
  13. typedef carl::FactorizedPolynomial<Polynomial> FactorizedPolynomial;
  14. typedef carl::RationalFunction<Polynomial> RationalFunction;
  15. typedef carl::RationalFunction<FactorizedPolynomial> FactorizedRationalFunction;
  16. typedef carl::PolynomialFactorizationPair<Polynomial> FactorizationPair;
  17. typedef carl::Formula<Polynomial> FormulaPoly;
  18. typedef std::vector<FormulaPoly> FormulaVector;
  19. %}
  20. %include "std_string.i"
  21. %import "variable.i"
  22. %import "polynomial.i"
  23. %import "rationalfunction.i"
  24. %import "factorizedpolynomial.i"
  25. %include "std_vector.i"
  26. %include "std_map.i"
  27. typedef carl::MultivariatePolynomial<Rational> Polynomial;
  28. typedef std::vector<FormulaPoly> FormulaVector;
  29. %template(FormulaVector) std::vector<carl::Formula<carl::MultivariatePolynomial<Rational>>>;
  30. typedef carl::Formula<Polynomial> FormulaPoly;
  31. typedef mpq_class Rational;
  32. typedef carl::Monomial::Arg Monomial;
  33. typedef carl::Term<Rational> Term;
  34. typedef carl::FactorizedPolynomial<Polynomial> FactorizedPolynomial;
  35. typedef carl::RationalFunction<Polynomial> RationalFunction;
  36. typedef carl::RationalFunction<FactorizedPolynomial> FactorizedRationalFunction;
  37. typedef carl::PolynomialFactorizationPair<Polynomial> FactorizationPair;
  38. /*%typemap(in) std::vector<FormulaPoly> {
  39. }*/
  40. namespace carl {
  41. //typedef typename Polynomial::UnderlyingNumberType<Rational>::type Polynomial::NumberType;
  42. enum FormulaType {
  43. // Generic
  44. ITE, EXISTS, FORALL,
  45. // Core Theory
  46. TRUE, FALSE,
  47. BOOL,
  48. NOT, IMPLIES, AND, OR, XOR,
  49. IFF,
  50. // Arithmetic Theory
  51. CONSTRAINT,
  52. // Bitvector Theory
  53. BITVECTOR,
  54. // Uninterpreted Theory
  55. UEQ
  56. };
  57. enum Relation { EQ = 0, NEQ = 1, LESS = 2, LEQ = 4, GREATER = 3, GEQ = 5 };
  58. /* TODO: wrap this toString
  59. inline std::ostream& operator<<(std::ostream& os, const Relation& r) {
  60. switch (r) {
  61. case Relation::EQ: os << "="; break;
  62. case Relation::NEQ: os << "<>"; break;
  63. case Relation::LESS: os << "<"; break;
  64. case Relation::LEQ: os << "<="; break;
  65. case Relation::GREATER: os << ">"; break;
  66. case Relation::GEQ: os << ">="; break;
  67. }
  68. return os;
  69. } */
  70. template<typename Pol> class Constraint {
  71. public:
  72. Constraint( bool _valid = true );
  73. explicit Constraint( carl::Variable::Arg _var, carl::Relation _rel, const typename Pol::NumberType & _bound = constant_zero<Rational>::get() );
  74. explicit Constraint( const Pol& _lhs, carl::Relation _rel );
  75. unsigned satisfiedBy( const std::map<Variable,Rational>& _assignment ) const;
  76. std::string toString( unsigned _unequalSwitch = 0, bool _infix = true, bool _friendlyVarNames = true ) const;
  77. const Pol& lhs() const;
  78. carl::Relation relation() const;
  79. //TODO: boolean connectives
  80. };
  81. template<typename LhsType> class SimpleConstraint {
  82. public:
  83. SimpleConstraint(bool v) : mLhs(v ? 0 : 1), mRelation(carl::Relation::EQ) {}
  84. SimpleConstraint(const LhsType& lhs, carl::Relation rel) : mLhs(lhs), mRelation(rel)
  85. {}
  86. const LhsType& lhs() const {return mLhs;}
  87. const carl::Relation& rel() const {return mRelation;}
  88. %extend {
  89. std::string toString() {
  90. std::stringstream ss;
  91. ss << *$self;
  92. return ss.str();
  93. }
  94. }
  95. };
  96. //TODO: wrap EvaluationMap, Formulas, and the other types that are missing!
  97. template<typename Pol> class Formula {
  98. public:
  99. explicit Formula( carl::Variable::Arg _booleanVar ):
  100. Formula( carl::FormulaPool<Pol>::getInstance().create( _booleanVar ) )
  101. {}
  102. explicit Formula( const carl::Constraint<Pol>& _constraint ):
  103. Formula( carl::FormulaPool<Pol>::getInstance().create( _constraint ) )
  104. {}
  105. explicit Formula( carl::FormulaType _type, const Formula& _subformula ):
  106. Formula(carl::FormulaPool<Pol>::getInstance().create(_type, std::move(Formula(_subformula))))
  107. {}
  108. explicit Formula( carl::FormulaType _type, const std::vector<Formula<Pol>>& _subasts ):
  109. Formula( carl::FormulaPool<Pol>::getInstance().create( _type, _subasts ) )
  110. {}
  111. //Apparently satisfiedBy is no longer existent
  112. //unsigned satisfiedBy( const carl::EvaluationMap<typename Pol::NumberType>& _assignment ) const;
  113. std::string toString( bool _withActivity = false, unsigned _resolveUnequal = 0, const std::string _init = "", bool _oneline = true, bool _infix = false, bool _friendlyNames = true, bool _withVariableDefinition = false ) const;
  114. size_t size() const;
  115. carl::FormulaType getType() const
  116. {
  117. return mpContent->mType;
  118. }
  119. //TODO: operators and iterator!
  120. };
  121. }
  122. %include "std_map.i"
  123. %template(FormulaPoly) carl::Formula<Polynomial>;
  124. %template(ConstraintPoly) carl::Constraint<Polynomial>;
  125. %template(SimpleConstraintPoly) carl::SimpleConstraint<Polynomial>;
  126. %template(SimpleConstraintFunc) carl::SimpleConstraint<FactorizedRationalFunction>;