1 /********************* */
2 /*! \file normal_form.h
4 ** Original author: taking
5 ** Major contributors: none
6 ** Minor contributors (to current version): dejan, mdeters
7 ** This file is part of the CVC4 prototype.
8 ** Copyright (c) 2009-2012 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** information.\endverbatim
12 ** \brief [[ Add one-line brief description here ]]
14 ** [[ Add lengthier description here ]]
15 ** \todo document this file
18 #include "cvc4_private.h"
20 #ifndef __CVC4__THEORY__ARITH__NORMAL_FORM_H
21 #define __CVC4__THEORY__ARITH__NORMAL_FORM_H
23 #include "expr/node.h"
24 #include "expr/node_self_iterator.h"
25 #include "util/rational.h"
26 #include "theory/theory.h"
27 #include "theory/arith/arith_utilities.h"
31 #include <ext/algorithm>
37 /***********************************************/
38 /***************** Normal Form *****************/
39 /***********************************************/
40 /***********************************************/
43 * Section 1: Languages
44 * The normal form for arithmetic nodes is defined by the language
45 * accepted by the following BNFs with some guard conditions.
46 * (The guard conditions are in Section 3 for completeness.)
50 * n.isVar() or is foreign
51 * n.getType() \in {Integer, Real}
55 * n.getKind() == kind::CONST_RATIONAL
57 * var_list := variable | (* [variable])
60 * isSorted varOrder [variable]
62 * monomial := constant | var_list | (* constant' var_list')
64 * \f$ constant' \not\in {0,1} \f$
66 * polynomial := monomial' | (+ [monomial])
69 * isStrictlySorted monoOrder [monomial]
70 * forall (\x -> x != 0) [monomial]
72 * rational_cmp := (|><| qpolynomial constant)
75 * not (exists constantMonomial (monomialList qpolynomial))
76 * (exists realMonomial (monomialList qpolynomial))
77 * abs(monomialCoefficient (head (monomialList qpolynomial))) == 1
79 * integer_cmp := (<= zpolynomial constant)
81 * not (exists constantMonomial (monomialList zpolynomial))
82 * (forall integerMonomial (monomialList zpolynomial))
83 * the gcd of all numerators of coefficients is 1
84 * the denominator of all coefficients and the constant is 1
86 * rational_eq := (= qvarlist qpolynomial)
88 * let allMonomials = (cons qvarlist (monomialList zpolynomial))
89 * let variableMonomials = (drop constantMonomial allMonomials)
90 * isStrictlySorted variableMonomials
91 * exists realMonomial variableMonomials
92 * is not empty qvarlist
94 * integer_eq := (= zmonomial zpolynomial)
96 * let allMonomials = (cons zmonomial (monomialList zpolynomial))
97 * let variableMonomials = (drop constantMonomial allMonomials)
98 * not (constantMonomial zmonomial)
99 * (forall integerMonomial allMonomials)
100 * isStrictlySorted variableMonomials
101 * the gcd of all numerators of coefficients is 1
102 * the denominator of all coefficients and the constant is 1
103 * the coefficient of monomial is positive
104 * the value of the coefficient of monomial is minimal in variableMonomials
106 * comparison := TRUE | FALSE
107 * | rational_cmp | (not rational_cmp)
108 * | rational_eq | (not rational_eq)
109 * | integer_cmp | (not integer_cmp)
110 * | integer_eq | (not integer_eq)
112 * Normal Form for terms := polynomial
113 * Normal Form for atoms := comparison
117 * Section 2: Helper Classes
118 * The langauges accepted by each of these defintions
119 * roughly corresponds to one of the following helper classes:
127 * Each of the classes obeys the following contracts/design decisions:
128 * -Calling isMember(Node node) on a node returns true iff that node is a
129 * a member of the language. Note: isMember is O(n).
130 * -Calling isNormalForm() on a helper class object returns true iff that
131 * helper class currently represents a normal form object.
132 * -If isNormalForm() is false, then this object must have been made
133 * using a mk*() factory function.
134 * -If isNormalForm() is true, calling getNode() on all of these classes
135 * returns a node that would be accepted by the corresponding language.
136 * And if isNormalForm() is false, returns Node::null().
137 * -Each of the classes is immutable.
138 * -Public facing constuctors have a 1-to-1 correspondence with one of
139 * production rules in the above grammar.
140 * -Public facing constuctors are required to fail in debug mode when the
141 * guards of the production rule are not strictly met.
142 * For example: Monomial(Constant(1),VarList(Variable(x))) must fail.
143 * -When a class has a Class parseClass(Node node) function,
144 * if isMember(node) is true, the function is required to return an instance
145 * of the helper class, instance, s.t. instance.getNode() == node.
146 * And if isMember(node) is false, this throws an assertion failure in debug
147 * mode and has undefined behaviour if not in debug mode.
148 * -Only public facing constructors, parseClass(node), and mk*() functions are
149 * considered privileged functions for the helper class.
150 * -Only privileged functions may use private constructors, and access
151 * private data members.
152 * -All non-privileged functions are considered utility functions and
153 * must use a privileged function in order to create an instance of the class.
157 * Section 3: Guard Conditions Misc.
160 * variable_order x y =
161 * if (meta_kind_variable x) and (meta_kind_variable y)
162 * then node_order x y
163 * else if (meta_kind_variable x)
165 * else if (meta_kind_variable y)
167 * else node_order x y
172 * | (* [variable]) -> len [variable]
176 * Empty -> (0,Node::null())
177 * | NonEmpty(vl) -> (var_list_len vl, vl)
179 * var_listOrder a b = tuple_cmp (order a) (order b)
181 * monomialVarList monomial =
182 * match monomial with
184 * | var_list -> NonEmpty(var_list)
185 * | (* constant' var_list') -> NonEmpty(var_list')
187 * monoOrder m0 m1 = var_listOrder (monomialVarList m0) (monomialVarList m1)
189 * integerMonomial mono =
190 * forall varHasTypeInteger (monomialVarList mono)
192 * realMonomial mono = not (integerMonomial mono)
194 * constantMonomial monomial =
195 * match monomial with
197 * | var_list -> false
198 * | (* constant' var_list') -> false
200 * monomialCoefficient monomial =
201 * match monomial with
202 * constant -> constant
203 * | var_list -> Constant(1)
204 * | (* constant' var_list') -> constant'
206 * monomialList polynomial =
207 * match polynomial with
208 * monomial -> monomial::[]
209 * | (+ [monomial]) -> [monomial]
213 * A NodeWrapper is a class that is a thinly veiled container of a Node object.
219 NodeWrapper(Node n
) : node(n
) {}
220 const Node
& getNode() const { return node
; }
221 };/* class NodeWrapper */
224 class Variable
: public NodeWrapper
{
226 Variable(Node n
) : NodeWrapper(n
) {
227 Assert(isMember(getNode()));
230 // TODO: check if it's a theory leaf also
231 static bool isMember(Node n
) {
232 Kind k
= n
.getKind();
234 case kind::CONST_RATIONAL
:
236 case kind::INTS_DIVISION
:
237 case kind::INTS_MODULUS
:
239 case kind::INTS_DIVISION_TOTAL
:
240 case kind::INTS_MODULUS_TOTAL
:
241 case kind::DIVISION_TOTAL
:
242 return isDivMember(n
);
244 return (!isRelationOperator(k
)) &&
245 (Theory::isLeafOf(n
, theory::THEORY_ARITH
));
249 static bool isDivMember(Node n
);
250 bool isDivLike() const{
251 return isDivMember(getNode());
254 bool isNormalForm() { return isMember(getNode()); }
256 bool isIntegral() const {
257 return getNode().getType().isInteger();
260 bool isMetaKindVariable() const {
261 return getNode().isVar();
264 bool operator<(const Variable
& v
) const {
265 bool thisIsVariable
= isMetaKindVariable();
266 bool vIsVariable
= v
.isMetaKindVariable();
268 if(thisIsVariable
== vIsVariable
){
269 bool thisIsInteger
= isIntegral();
270 bool vIsInteger
= v
.isIntegral();
271 if(thisIsInteger
== vIsInteger
){
272 return getNode() < v
.getNode();
274 return thisIsInteger
&& !vIsInteger
;
277 return thisIsVariable
&& !vIsVariable
;
281 bool operator==(const Variable
& v
) const { return getNode() == v
.getNode();}
283 };/* class Variable */
286 class Constant
: public NodeWrapper
{
288 Constant(Node n
) : NodeWrapper(n
) {
289 Assert(isMember(getNode()));
292 static bool isMember(Node n
) {
293 return n
.getKind() == kind::CONST_RATIONAL
;
296 bool isNormalForm() { return isMember(getNode()); }
298 static Constant
mkConstant(Node n
) {
299 Assert(n
.getKind() == kind::CONST_RATIONAL
);
303 static Constant
mkConstant(const Rational
& rat
) {
304 return Constant(mkRationalNode(rat
));
307 static Constant
mkZero() {
308 return mkConstant(Rational(0));
311 static Constant
mkOne() {
312 return mkConstant(Rational(1));
315 const Rational
& getValue() const {
316 return getNode().getConst
<Rational
>();
319 bool isIntegral() const { return getValue().isIntegral(); }
321 int sgn() const { return getValue().sgn(); }
323 bool isZero() const { return sgn() == 0; }
324 bool isNegative() const { return sgn() < 0; }
325 bool isPositive() const { return sgn() > 0; }
327 bool isOne() const { return getValue() == 1; }
329 Constant
operator*(const Rational
& other
) const {
330 return mkConstant(getValue() * other
);
333 Constant
operator*(const Constant
& other
) const {
334 return mkConstant(getValue() * other
.getValue());
336 Constant
operator+(const Constant
& other
) const {
337 return mkConstant(getValue() + other
.getValue());
339 Constant
operator-() const {
340 return mkConstant(-getValue());
343 Constant
inverse() const{
345 return mkConstant(getValue().inverse());
348 bool operator<(const Constant
& other
) const {
349 return getValue() < other
.getValue();
352 bool operator==(const Constant
& other
) const {
353 //Rely on node uniqueness.
354 return getNode() == other
.getNode();
357 Constant
abs() const {
365 uint32_t length() const{
366 Assert(isIntegral());
367 return getValue().getNumerator().length();
370 };/* class Constant */
373 template <class GetNodeIterator
>
374 inline Node
makeNode(Kind k
, GetNodeIterator start
, GetNodeIterator end
) {
377 while(start
!= end
) {
378 nb
<< (*start
).getNode();
383 }/* makeNode<GetNodeIterator>(Kind, iterator, iterator) */
386 template <class GetNodeIterator
, class T
>
387 static void copy_range(GetNodeIterator begin
, GetNodeIterator end
, std::vector
<T
>& result
){
389 result
.push_back(*begin
);
394 template <class GetNodeIterator
, class T
>
395 static void merge_ranges(GetNodeIterator first1
,
396 GetNodeIterator last1
,
397 GetNodeIterator first2
,
398 GetNodeIterator last2
,
399 std::vector
<T
>& result
) {
401 while(first1
!= last1
&& first2
!= last2
){
402 if( (*first1
) < (*first2
) ){
403 result
.push_back(*first1
);
406 result
.push_back(*first2
);
410 copy_range(first1
, last1
, result
);
411 copy_range(first2
, last2
, result
);
415 * A VarList is a sorted list of variables representing a product.
416 * If the VarList is empty, it represents an empty product or 1.
417 * If the VarList has size 1, it represents a single variable.
419 * A non-sorted VarList can never be successfully made in debug mode.
421 class VarList
: public NodeWrapper
{
424 static Node
multList(const std::vector
<Variable
>& list
) {
425 Assert(list
.size() >= 2);
427 return makeNode(kind::MULT
, list
.begin(), list
.end());
430 VarList() : NodeWrapper(Node::null()) {}
432 VarList(Node n
) : NodeWrapper(n
) {
433 Assert(isSorted(begin(), end()));
436 typedef expr::NodeSelfIterator internal_iterator
;
438 internal_iterator
internalBegin() const {
440 return expr::NodeSelfIterator::self(getNode());
442 return getNode().begin();
446 internal_iterator
internalEnd() const {
448 return expr::NodeSelfIterator::selfEnd(getNode());
450 return getNode().end();
458 internal_iterator d_iter
;
461 explicit iterator(internal_iterator i
) : d_iter(i
) {}
463 inline Variable
operator*() {
464 return Variable(*d_iter
);
467 bool operator==(const iterator
& i
) {
468 return d_iter
== i
.d_iter
;
471 bool operator!=(const iterator
& i
) {
472 return d_iter
!= i
.d_iter
;
475 iterator
operator++() {
480 iterator
operator++(int) {
481 return iterator(d_iter
++);
485 iterator
begin() const {
486 return iterator(internalBegin());
489 iterator
end() const {
490 return iterator(internalEnd());
493 Variable
getHead() const {
498 VarList(Variable v
) : NodeWrapper(v
.getNode()) {
499 Assert(isSorted(begin(), end()));
502 VarList(const std::vector
<Variable
>& l
) : NodeWrapper(multList(l
)) {
503 Assert(l
.size() >= 2);
504 Assert(isSorted(begin(), end()));
507 static bool isMember(Node n
);
509 bool isNormalForm() const {
513 static VarList
mkEmptyVarList() {
518 /** There are no restrictions on the size of l */
519 static VarList
mkVarList(const std::vector
<Variable
>& l
) {
521 return mkEmptyVarList();
522 } else if(l
.size() == 1) {
523 return VarList((*l
.begin()).getNode());
529 bool empty() const { return getNode().isNull(); }
530 bool singleton() const {
531 return !empty() && getNode().getKind() != kind::MULT
;
538 return getNode().getNumChildren();
541 static VarList
parseVarList(Node n
);
543 VarList
operator*(const VarList
& vl
) const;
545 int cmp(const VarList
& vl
) const;
547 bool operator<(const VarList
& vl
) const { return cmp(vl
) < 0; }
549 bool operator==(const VarList
& vl
) const { return cmp(vl
) == 0; }
551 bool isIntegral() const {
552 for(iterator i
= begin(), e
=end(); i
!= e
; ++i
){
554 if(!var
.isIntegral()){
562 bool isSorted(iterator start
, iterator end
);
564 };/* class VarList */
567 class Monomial
: public NodeWrapper
{
571 Monomial(Node n
, const Constant
& c
, const VarList
& vl
):
572 NodeWrapper(n
), constant(c
), varList(vl
)
574 Assert(!c
.isZero() || vl
.empty() );
575 Assert( c
.isZero() || !vl
.empty() );
577 Assert(!c
.isOne() || !multStructured(n
));
580 static Node
makeMultNode(const Constant
& c
, const VarList
& vl
) {
584 return NodeManager::currentNM()->mkNode(kind::MULT
, c
.getNode(), vl
.getNode());
587 static bool multStructured(Node n
) {
588 return n
.getKind() == kind::MULT
&&
589 n
[0].getKind() == kind::CONST_RATIONAL
&&
590 n
.getNumChildren() == 2;
595 Monomial(const Constant
& c
):
596 NodeWrapper(c
.getNode()), constant(c
), varList(VarList::mkEmptyVarList())
599 Monomial(const VarList
& vl
):
600 NodeWrapper(vl
.getNode()), constant(Constant::mkConstant(1)), varList(vl
)
602 Assert( !varList
.empty() );
605 Monomial(const Constant
& c
, const VarList
& vl
):
606 NodeWrapper(makeMultNode(c
,vl
)), constant(c
), varList(vl
)
608 Assert( !c
.isZero() );
609 Assert( !c
.isOne() );
610 Assert( !varList
.empty() );
612 Assert(multStructured(getNode()));
615 static bool isMember(TNode n
);
617 /** Makes a monomial with no restrictions on c and vl. */
618 static Monomial
mkMonomial(const Constant
& c
, const VarList
& vl
);
620 static Monomial
mkMonomial(const Variable
& v
){
621 return Monomial(VarList(v
));
624 static Monomial
parseMonomial(Node n
);
626 static Monomial
mkZero() {
627 return Monomial(Constant::mkConstant(0));
629 static Monomial
mkOne() {
630 return Monomial(Constant::mkConstant(1));
632 const Constant
& getConstant() const { return constant
; }
633 const VarList
& getVarList() const { return varList
; }
635 bool isConstant() const {
636 return varList
.empty();
639 bool isZero() const {
640 return constant
.isZero();
643 bool coefficientIsOne() const {
644 return constant
.isOne();
647 bool absCoefficientIsOne() const {
648 return coefficientIsOne() || constant
.getValue() == -1;
651 bool constantIsPositive() const {
652 return getConstant().isPositive();
655 Monomial
operator*(const Rational
& q
) const;
656 Monomial
operator*(const Constant
& c
) const;
657 Monomial
operator*(const Monomial
& mono
) const;
659 Monomial
operator-() const{
660 return (*this) * Rational(-1);
664 int cmp(const Monomial
& mono
) const {
665 return getVarList().cmp(mono
.getVarList());
668 bool operator<(const Monomial
& vl
) const {
672 bool operator==(const Monomial
& vl
) const {
676 static bool isSorted(const std::vector
<Monomial
>& m
) {
677 return __gnu_cxx::is_sorted(m
.begin(), m
.end());
680 static bool isStrictlySorted(const std::vector
<Monomial
>& m
) {
681 return isSorted(m
) && std::adjacent_find(m
.begin(),m
.end()) == m
.end();
685 * The variable product
687 bool integralVariables() const {
688 return getVarList().isIntegral();
692 * The coefficient of the monomial is integral.
694 bool integralCoefficient() const {
695 return getConstant().isIntegral();
699 * A Monomial is an "integral" monomial if the constant is integral.
701 bool isIntegral() const {
702 return integralCoefficient() && integralVariables();
706 * Given a sorted list of monomials, this function transforms this
707 * into a strictly sorted list of monomials that does not contain zero.
709 static std::vector
<Monomial
> sumLikeTerms(const std::vector
<Monomial
>& monos
);
711 bool absLessThan(const Monomial
& other
) const{
712 return getConstant().abs() < other
.getConstant().abs();
715 uint32_t coefficientLength() const{
716 return getConstant().length();
720 static void printList(const std::vector
<Monomial
>& list
);
722 };/* class Monomial */
727 class Polynomial
: public NodeWrapper
{
731 Polynomial(TNode n
) : NodeWrapper(n
), d_singleton(Monomial::isMember(n
)) {
732 Assert(isMember(getNode()));
735 static Node
makePlusNode(const std::vector
<Monomial
>& m
) {
736 Assert(m
.size() >= 2);
738 return makeNode(kind::PLUS
, m
.begin(), m
.end());
741 typedef expr::NodeSelfIterator internal_iterator
;
743 internal_iterator
internalBegin() const {
745 return expr::NodeSelfIterator::self(getNode());
747 return getNode().begin();
751 internal_iterator
internalEnd() const {
753 return expr::NodeSelfIterator::selfEnd(getNode());
755 return getNode().end();
759 bool singleton() const { return d_singleton
; }
762 static bool isMember(TNode n
) {
763 if(Monomial::isMember(n
)){
765 }else if(n
.getKind() == kind::PLUS
){
766 Assert(n
.getNumChildren() >= 2);
767 Node::iterator currIter
= n
.begin(), end
= n
.end();
768 Node prev
= *currIter
;
769 if(!Monomial::isMember(prev
)){
773 Monomial mprev
= Monomial::parseMonomial(prev
);
775 for(; currIter
!= end
; ++currIter
){
776 Node curr
= *currIter
;
777 if(!Monomial::isMember(curr
)){
780 Monomial mcurr
= Monomial::parseMonomial(curr
);
781 if(!(mprev
< mcurr
)){
794 internal_iterator d_iter
;
797 explicit iterator(internal_iterator i
) : d_iter(i
) {}
799 inline Monomial
operator*() {
800 return Monomial::parseMonomial(*d_iter
);
803 bool operator==(const iterator
& i
) {
804 return d_iter
== i
.d_iter
;
807 bool operator!=(const iterator
& i
) {
808 return d_iter
!= i
.d_iter
;
811 iterator
operator++() {
816 iterator
operator++(int) {
817 return iterator(d_iter
++);
821 iterator
begin() const { return iterator(internalBegin()); }
822 iterator
end() const { return iterator(internalEnd()); }
824 Polynomial(const Monomial
& m
):
825 NodeWrapper(m
.getNode()), d_singleton(true)
828 Polynomial(const std::vector
<Monomial
>& m
):
829 NodeWrapper(makePlusNode(m
)), d_singleton(false)
831 Assert( m
.size() >= 2);
832 Assert( Monomial::isStrictlySorted(m
) );
835 static Polynomial
mkPolynomial(const Variable
& v
){
836 return Monomial::mkMonomial(v
);
839 static Polynomial
mkPolynomial(const std::vector
<Monomial
>& m
) {
841 return Polynomial(Monomial::mkZero());
842 } else if(m
.size() == 1) {
843 return Polynomial((*m
.begin()));
845 return Polynomial(m
);
849 static Polynomial
parsePolynomial(Node n
) {
850 return Polynomial(n
);
853 static Polynomial
mkZero() {
854 return Polynomial(Monomial::mkZero());
856 static Polynomial
mkOne() {
857 return Polynomial(Monomial::mkOne());
859 bool isZero() const {
860 return singleton() && (getHead().isZero());
863 bool isConstant() const {
864 return singleton() && (getHead().isConstant());
867 bool containsConstant() const {
868 return getHead().isConstant();
871 uint32_t size() const{
875 Assert(getNode().getKind() == kind::PLUS
);
876 return getNode().getNumChildren();
880 Monomial
getHead() const {
884 Polynomial
getTail() const {
885 Assert(! singleton());
887 iterator tailStart
= begin();
889 std::vector
<Monomial
> subrange
;
890 copy_range(tailStart
, end(), subrange
);
891 return mkPolynomial(subrange
);
894 Monomial
minimumVariableMonomial() const;
895 bool variableMonomialAreStrictlyGreater(const Monomial
& m
) const;
897 void printList() const {
898 if(Debug
.isOn("normal-form")){
899 Debug("normal-form") << "start list" << std::endl
;
900 for(iterator i
= begin(), oend
= end(); i
!= oend
; ++i
) {
901 const Monomial
& m
=*i
;
904 Debug("normal-form") << "end list" << std::endl
;
908 /** A Polynomial is an "integral" polynomial if all of the monomials are integral. */
909 bool allIntegralVariables() const {
910 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
911 if(!(*i
).integralVariables()){
919 * A Polynomial is an "integral" polynomial if all of the monomials are integral
920 * and all of the coefficients are Integral. */
921 bool isIntegral() const {
922 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
923 if(!(*i
).isIntegral()){
931 * Selects a minimal monomial in the polynomial by the absolute value of
934 Monomial
selectAbsMinimum() const;
936 /** Returns true if the absolute value of the head coefficient is one. */
937 bool leadingCoefficientIsAbsOne() const;
938 bool leadingCoefficientIsPositive() const;
939 bool denominatorLCMIsOne() const;
940 bool numeratorGCDIsOne() const;
943 * Returns the Least Common Multiple of the denominators of the coefficients
946 Integer
denominatorLCM() const;
949 * Returns the GCD of the numerators of the monomials.
950 * Requires this to be an isIntegral() polynomial.
952 Integer
numeratorGCD() const;
955 * Returns the GCD of the coefficients of the monomials.
956 * Requires this to be an isIntegral() polynomial.
960 Polynomial
exactDivide(const Integer
& z
) const {
961 Assert(isIntegral());
962 Constant invz
= Constant::mkConstant(Rational(1,z
));
963 Polynomial prod
= (*this) * Monomial(invz
);
964 Assert(prod
.isIntegral());
968 Polynomial
operator+(const Polynomial
& vl
) const;
969 Polynomial
operator-(const Polynomial
& vl
) const;
970 Polynomial
operator-() const{
971 return (*this) * Rational(-1);
974 Polynomial
operator*(const Rational
& q
) const;
975 Polynomial
operator*(const Constant
& c
) const;
976 Polynomial
operator*(const Monomial
& mono
) const;
978 Polynomial
operator*(const Polynomial
& poly
) const;
981 * Viewing the integer polynomial as a list [(* coeff_i mono_i)]
982 * The quotient and remainder of p divided by the non-zero integer z is:
983 * q := [(* floor(coeff_i/z) mono_i )]
984 * r := [(* rem(coeff_i/z) mono_i)]
985 * computeQR(p,z) returns the node (+ q r).
987 * q and r are members of the Polynomial class.
989 * computeQR( p = (+ 5 (* 3 x) (* 8 y)) , z = 2) returns
990 * (+ (+ 2 x (* 4 y)) (+ 1 x))
992 static Node
computeQR(const Polynomial
& p
, const Integer
& z
);
994 /** Returns the coefficient associated with the VarList in the polynomial. */
995 Constant
getCoefficient(const VarList
& vl
) const;
997 uint32_t maxLength() const{
998 iterator i
= begin(), e
=end();
1002 uint32_t max
= (*i
).coefficientLength();
1005 uint32_t curr
= (*i
).coefficientLength();
1014 uint32_t numMonomials() const {
1015 if( getNode().getKind() == kind::PLUS
){
1016 return getNode().getNumChildren();
1024 const Rational
& asConstant() const{
1025 Assert(isConstant());
1026 return getNode().getConst
<Rational
>();
1027 //return getHead().getConstant().getValue();
1030 bool isVarList() const {
1032 return VarList::isMember(getNode());
1038 VarList
asVarList() const {
1039 Assert(isVarList());
1040 return getHead().getVarList();
1043 friend class SumPair
;
1044 friend class Comparison
;
1046 /** Returns a node that if asserted ensures v is the abs of this polynomial.*/
1047 Node
makeAbsCondition(Variable v
){
1048 return makeAbsCondition(v
, *this);
1051 /** Returns a node that if asserted ensures v is the abs of p.*/
1052 static Node
makeAbsCondition(Variable v
, Polynomial p
);
1054 };/* class Polynomial */
1058 * SumPair is a utility class that extends polynomials for use in computations.
1059 * A SumPair is always a combination of (+ p c) where
1060 * c is a constant and p is a polynomial such that p = 0 or !p.containsConstant().
1062 * These are a useful utility for representing the equation p = c as (+ p -c) where the pair
1063 * is known to implicitly be equal to 0.
1065 * SumPairs do not have unique representations due to the potential for p = 0.
1066 * This makes them inappropriate for normal forms.
1068 class SumPair
: public NodeWrapper
{
1070 static Node
toNode(const Polynomial
& p
, const Constant
& c
){
1071 return NodeManager::currentNM()->mkNode(kind::PLUS
, p
.getNode(), c
.getNode());
1077 Assert(isNormalForm());
1082 SumPair(const Polynomial
& p
):
1083 NodeWrapper(toNode(p
, Constant::mkConstant(0)))
1085 Assert(isNormalForm());
1088 SumPair(const Polynomial
& p
, const Constant
& c
):
1089 NodeWrapper(toNode(p
, c
))
1091 Assert(isNormalForm());
1094 static bool isMember(TNode n
) {
1095 if(n
.getKind() == kind::PLUS
&& n
.getNumChildren() == 2){
1096 if(Constant::isMember(n
[1])){
1097 if(Polynomial::isMember(n
[0])){
1098 Polynomial p
= Polynomial::parsePolynomial(n
[0]);
1099 return p
.isZero() || (!p
.containsConstant());
1111 bool isNormalForm() const {
1112 return isMember(getNode());
1115 Polynomial
getPolynomial() const {
1116 return Polynomial::parsePolynomial(getNode()[0]);
1119 Constant
getConstant() const {
1120 return Constant::mkConstant((getNode())[1]);
1123 SumPair
operator+(const SumPair
& other
) const {
1124 return SumPair(getPolynomial() + other
.getPolynomial(),
1125 getConstant() + other
.getConstant());
1128 SumPair
operator*(const Constant
& c
) const {
1129 return SumPair(getPolynomial() * c
, getConstant() * c
);
1132 SumPair
operator-(const SumPair
& other
) const {
1133 return (*this) + (other
* Constant::mkConstant(-1));
1136 static SumPair
mkSumPair(const Polynomial
& p
);
1138 static SumPair
mkSumPair(const Variable
& var
){
1139 return SumPair(Polynomial::mkPolynomial(var
));
1142 static SumPair
parseSumPair(TNode n
){
1146 bool isIntegral() const{
1147 return getConstant().isIntegral() && getPolynomial().isIntegral();
1150 bool isConstant() const {
1151 return getPolynomial().isZero();
1154 bool isZero() const {
1155 return getConstant().isZero() && isConstant();
1159 * Returns the greatest common divisor of gcd(getPolynomial()) and getConstant().
1160 * The SumPair must be integral.
1162 Integer
gcd() const {
1163 Assert(isIntegral());
1164 return (getPolynomial().gcd()).gcd(getConstant().getValue().getNumerator());
1167 uint32_t maxLength() const {
1168 Assert(isIntegral());
1169 return std::max(getPolynomial().maxLength(), getConstant().length());
1172 static SumPair
mkZero() {
1173 return SumPair(Polynomial::mkZero(), Constant::mkConstant(0));
1176 static Node
computeQR(const SumPair
& sp
, const Integer
& div
);
1178 };/* class SumPair */
1180 /* class OrderedPolynomialPair { */
1182 /* Polynomial d_first; */
1183 /* Polynomial d_second; */
1185 /* OrderedPolynomialPair(const Polynomial& f, const Polynomial& s) */
1190 /* /\** Returns the first part of the pair. *\/ */
1191 /* const Polynomial& getFirst() const { */
1192 /* return d_first; */
1195 /* /\** Returns the second part of the pair. *\/ */
1196 /* const Polynomial& getSecond() const { */
1197 /* return d_second; */
1200 /* OrderedPolynomialPair operator*(const Constant& c) const; */
1201 /* OrderedPolynomialPair operator+(const Polynomial& p) const; */
1203 /* /\** Returns true if both of the polynomials are constant. *\/ */
1204 /* bool isConstant() const; */
1207 /* * Evaluates an isConstant() ordered pair as if */
1208 /* * (k getFirst() getRight()) */
1210 /* bool evaluateConstant(Kind k) const; */
1213 /* * Returns the Least Common Multiple of the monomials */
1214 /* * on the lefthand side and the constant on the right. */
1216 /* Integer denominatorLCM() const; */
1218 /* /\** Constructs a SumPair. *\/ */
1219 /* SumPair toSumPair() const; */
1222 /* OrderedPolynomialPair divideByGCD() const; */
1223 /* OrderedPolynomialPair multiplyConstant(const Constant& c) const; */
1226 /* * Returns true if all of the variables are integers, */
1227 /* * and the coefficients are integers. */
1229 /* bool isIntegral() const; */
1231 /* /\** Returns true if all of the variables are integers. *\/ */
1232 /* bool allIntegralVariables() const { */
1233 /* return getFirst().allIntegralVariables() && getSecond().allIntegralVariables(); */
1237 class Comparison
: public NodeWrapper
{
1240 static Node
toNode(Kind k
, const Polynomial
& l
, const Constant
& c
);
1241 static Node
toNode(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1243 Comparison(TNode n
);
1246 * Creates a node in normal form equivalent to (= l 0).
1247 * All variables in l are integral.
1249 static Node
mkIntEquality(const Polynomial
& l
);
1252 * Creates a comparison equivalent to (k l 0).
1253 * k is either GT or GEQ.
1254 * All variables in l are integral.
1256 static Node
mkIntInequality(Kind k
, const Polynomial
& l
);
1259 * Creates a node equivalent to (= l 0).
1260 * It is not the case that all variables in l are integral.
1262 static Node
mkRatEquality(const Polynomial
& l
);
1265 * Creates a comparison equivalent to (k l 0).
1266 * k is either GT or GEQ.
1267 * It is not the case that all variables in l are integral.
1269 static Node
mkRatInequality(Kind k
, const Polynomial
& l
);
1273 Comparison(bool val
) :
1274 NodeWrapper(NodeManager::currentNM()->mkConst(val
))
1278 * Given a literal to TheoryArith return a single kind to
1279 * to indicate its underlying structure.
1280 * The function returns the following in each case:
1281 * - (K left right) -> K where is either EQUAL, GT, or GEQ
1282 * - (CONST_BOOLEAN b) -> CONST_BOOLEAN
1283 * - (NOT (EQUAL left right)) -> DISTINCT
1284 * - (NOT (GT left right)) -> LEQ
1285 * - (NOT (GEQ left right)) -> LT
1286 * If none of these match, it returns UNDEFINED_KIND.
1288 static Kind
comparisonKind(TNode literal
);
1290 Kind
comparisonKind() const { return comparisonKind(getNode()); }
1292 static Comparison
mkComparison(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1294 /** Returns true if the comparison is a boolean constant. */
1295 bool isBoolean() const;
1298 * Returns true if the comparison is either a boolean term,
1299 * in integer normal form or mixed normal form.
1301 bool isNormalForm() const;
1304 bool isNormalGT() const;
1305 bool isNormalGEQ() const;
1307 bool isNormalLT() const;
1308 bool isNormalLEQ() const;
1310 bool isNormalEquality() const;
1311 bool isNormalDistinct() const;
1312 bool isNormalEqualityOrDisequality() const;
1314 bool allIntegralVariables() const {
1315 return getLeft().allIntegralVariables() && getRight().allIntegralVariables();
1317 bool rightIsConstant() const;
1320 Polynomial
getLeft() const;
1321 Polynomial
getRight() const;
1323 /* /\** Normal form check if at least one variable is real. *\/ */
1324 /* bool isMixedCompareNormalForm() const; */
1326 /* /\** Normal form check if at least one variable is real. *\/ */
1327 /* bool isMixedEqualsNormalForm() const; */
1329 /* /\** Normal form check is all variables are integer.*\/ */
1330 /* bool isIntegerCompareNormalForm() const; */
1332 /* /\** Normal form check is all variables are integer.*\/ */
1333 /* bool isIntegerEqualsNormalForm() const; */
1337 * Returns true if all of the variables are integers, the coefficients are integers,
1338 * and the right hand coefficient is an integer.
1340 bool debugIsIntegral() const;
1342 static Comparison
parseNormalForm(TNode n
);
1344 inline static bool isNormalAtom(TNode n
){
1345 Comparison parse
= Comparison::parseNormalForm(n
);
1346 return parse
.isNormalForm();
1349 SumPair
toSumPair() const;
1351 Polynomial
normalizedVariablePart() const;
1352 DeltaRational
normalizedDeltaRational() const;
1354 };/* class Comparison */
1356 }/* CVC4::theory::arith namespace */
1357 }/* CVC4::theory namespace */
1358 }/* CVC4 namespace */
1360 #endif /* __CVC4__THEORY__ARITH__NORMAL_FORM_H */