}
-RewriteResponse ArithRewriter::preRewritePlus(TNode t){
+RewriteResponse ArithRewriter::preRewritePlus(TNode t)
+{
Assert(t.getKind() == kind::ADD);
return RewriteResponse(REWRITE_DONE, expr::algorithm::flatten(t));
}
-RewriteResponse ArithRewriter::postRewritePlus(TNode t){
+RewriteResponse ArithRewriter::postRewritePlus(TNode t)
+{
Assert(t.getKind() == kind::ADD);
Assert(t.getNumChildren() > 1);
- {
- Node flat = expr::algorithm::flatten(t);
- if (flat != t)
- {
- return RewriteResponse(REWRITE_AGAIN, flat);
- }
- }
-
- Rational rational;
- RealAlgebraicNumber ran;
- std::vector<Monomial> monomials;
- std::vector<Polynomial> polynomials;
-
- for (const auto& child : t)
- {
- if (child.isConst())
- {
- if (child.getConst<Rational>().isZero())
- {
- continue;
- }
- rational += child.getConst<Rational>();
- }
- else if (child.getKind() == Kind::REAL_ALGEBRAIC_NUMBER)
- {
- ran += child.getOperator().getConst<RealAlgebraicNumber>();
- }
- else if (Monomial::isMember(child))
- {
- monomials.emplace_back(Monomial::parseMonomial(child));
- }
- else
- {
- polynomials.emplace_back(Polynomial::parsePolynomial(child));
- }
- }
-
- if(!monomials.empty()){
- Monomial::sort(monomials);
- Monomial::combineAdjacentMonomials(monomials);
- polynomials.emplace_back(Polynomial::mkPolynomial(monomials));
- }
- if (!rational.isZero())
- {
- polynomials.emplace_back(
- Polynomial::mkPolynomial(Constant::mkConstant(rational)));
- }
-
- Polynomial poly = Polynomial::sumPolynomials(polynomials);
-
- if (isZero(ran))
- {
- return RewriteResponse(REWRITE_DONE, poly.getNode());
- }
- if (poly.containsConstant())
- {
- ran += RealAlgebraicNumber(poly.getHead().getConstant().getValue());
- if (!poly.isConstant())
- {
- poly = poly.getTail();
- }
- }
+ std::vector<TNode> children;
+ expr::algorithm::flatten(t, children);
- auto* nm = NodeManager::currentNM();
- if (poly.isConstant())
+ rewriter::Sum sum;
+ for (const auto& child : children)
{
- return RewriteResponse(REWRITE_DONE, nm->mkRealAlgebraicNumber(ran));
+ rewriter::addToSum(sum, child);
}
- return RewriteResponse(
- REWRITE_DONE,
- nm->mkNode(Kind::ADD, nm->mkRealAlgebraicNumber(ran), poly.getNode()));
+ return RewriteResponse(REWRITE_DONE, rewriter::collectSum(sum));
}
RewriteResponse ArithRewriter::preRewriteMult(TNode node)
static RewriteResponse rewriteNeg(TNode t, bool pre);
/** rewrite binary minus */
static RewriteResponse rewriteSub(TNode t);
+ /** preRewrite addition */
+ static RewriteResponse preRewritePlus(TNode t);
+ /** postRewrite addition */
+ static RewriteResponse postRewritePlus(TNode t);
static RewriteResponse rewriteDiv(TNode t, bool pre);
static RewriteResponse rewriteAbs(TNode t);
static RewriteResponse rewriteIntsDivMod(TNode t, bool pre);
/** Entry for applications of to_int and is_int */
static RewriteResponse rewriteExtIntegerOp(TNode t);
- static RewriteResponse preRewritePlus(TNode t);
- static RewriteResponse postRewritePlus(TNode t);
-
static RewriteResponse preRewriteMult(TNode t);
static RewriteResponse postRewriteMult(TNode t);
}
std::vector<Node> prod;
prod.emplace_back(mkConst(multiplicity));
- prod.insert(prod.end(), monomial.begin(), monomial.end());
+ if (monomial.getKind() == Kind::MULT || monomial.getKind() == Kind::NONLINEAR_MULT)
+ {
+ prod.insert(prod.end(), monomial.begin(), monomial.end());
+ }
+ else
+ {
+ prod.emplace_back(monomial);
+ }
Assert(prod.size() >= 2);
return NodeManager::currentNM()->mkNode(Kind::NONLINEAR_MULT, prod);
}