1 /********************* */
2 /*! \file normal_form.h
4 ** Original author: Tim King
5 ** Major contributors: none
6 ** Minor contributors (to current version): Dejan Jovanovic, Morgan Deters
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2013 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
85 * the leading coefficient is positive
87 * rational_eq := (= qvarlist qpolynomial)
89 * let allMonomials = (cons qvarlist (monomialList zpolynomial))
90 * let variableMonomials = (drop constantMonomial allMonomials)
91 * isStrictlySorted variableMonomials
92 * exists realMonomial variableMonomials
93 * is not empty qvarlist
95 * integer_eq := (= zmonomial zpolynomial)
97 * let allMonomials = (cons zmonomial (monomialList zpolynomial))
98 * let variableMonomials = (drop constantMonomial allMonomials)
99 * not (constantMonomial zmonomial)
100 * (forall integerMonomial allMonomials)
101 * isStrictlySorted variableMonomials
102 * the gcd of all numerators of coefficients is 1
103 * the denominator of all coefficients and the constant is 1
104 * the coefficient of monomial is positive
105 * the value of the coefficient of monomial is minimal in variableMonomials
107 * comparison := TRUE | FALSE
108 * | rational_cmp | (not rational_cmp)
109 * | rational_eq | (not rational_eq)
110 * | integer_cmp | (not integer_cmp)
111 * | integer_eq | (not integer_eq)
113 * Normal Form for terms := polynomial
114 * Normal Form for atoms := comparison
118 * Section 2: Helper Classes
119 * The langauges accepted by each of these defintions
120 * roughly corresponds to one of the following helper classes:
128 * Each of the classes obeys the following contracts/design decisions:
129 * -Calling isMember(Node node) on a node returns true iff that node is a
130 * a member of the language. Note: isMember is O(n).
131 * -Calling isNormalForm() on a helper class object returns true iff that
132 * helper class currently represents a normal form object.
133 * -If isNormalForm() is false, then this object must have been made
134 * using a mk*() factory function.
135 * -If isNormalForm() is true, calling getNode() on all of these classes
136 * returns a node that would be accepted by the corresponding language.
137 * And if isNormalForm() is false, returns Node::null().
138 * -Each of the classes is immutable.
139 * -Public facing constuctors have a 1-to-1 correspondence with one of
140 * production rules in the above grammar.
141 * -Public facing constuctors are required to fail in debug mode when the
142 * guards of the production rule are not strictly met.
143 * For example: Monomial(Constant(1),VarList(Variable(x))) must fail.
144 * -When a class has a Class parseClass(Node node) function,
145 * if isMember(node) is true, the function is required to return an instance
146 * of the helper class, instance, s.t. instance.getNode() == node.
147 * And if isMember(node) is false, this throws an assertion failure in debug
148 * mode and has undefined behaviour if not in debug mode.
149 * -Only public facing constructors, parseClass(node), and mk*() functions are
150 * considered privileged functions for the helper class.
151 * -Only privileged functions may use private constructors, and access
152 * private data members.
153 * -All non-privileged functions are considered utility functions and
154 * must use a privileged function in order to create an instance of the class.
158 * Section 3: Guard Conditions Misc.
161 * variable_order x y =
162 * if (meta_kind_variable x) and (meta_kind_variable y)
163 * then node_order x y
164 * else if (meta_kind_variable x)
166 * else if (meta_kind_variable y)
168 * else node_order x y
173 * | (* [variable]) -> len [variable]
177 * Empty -> (0,Node::null())
178 * | NonEmpty(vl) -> (var_list_len vl, vl)
180 * var_listOrder a b = tuple_cmp (order a) (order b)
182 * monomialVarList monomial =
183 * match monomial with
185 * | var_list -> NonEmpty(var_list)
186 * | (* constant' var_list') -> NonEmpty(var_list')
188 * monoOrder m0 m1 = var_listOrder (monomialVarList m0) (monomialVarList m1)
190 * integerMonomial mono =
191 * forall varHasTypeInteger (monomialVarList mono)
193 * realMonomial mono = not (integerMonomial mono)
195 * constantMonomial monomial =
196 * match monomial with
198 * | var_list -> false
199 * | (* constant' var_list') -> false
201 * monomialCoefficient monomial =
202 * match monomial with
203 * constant -> constant
204 * | var_list -> Constant(1)
205 * | (* constant' var_list') -> constant'
207 * monomialList polynomial =
208 * match polynomial with
209 * monomial -> monomial::[]
210 * | (+ [monomial]) -> [monomial]
214 * A NodeWrapper is a class that is a thinly veiled container of a Node object.
220 NodeWrapper(Node n
) : node(n
) {}
221 const Node
& getNode() const { return node
; }
222 };/* class NodeWrapper */
225 class Variable
: public NodeWrapper
{
227 Variable(Node n
) : NodeWrapper(n
) {
228 Assert(isMember(getNode()));
231 // TODO: check if it's a theory leaf also
232 static bool isMember(Node n
) {
233 Kind k
= n
.getKind();
235 case kind::CONST_RATIONAL
:
237 case kind::INTS_DIVISION
:
238 case kind::INTS_MODULUS
:
240 case kind::INTS_DIVISION_TOTAL
:
241 case kind::INTS_MODULUS_TOTAL
:
242 case kind::DIVISION_TOTAL
:
243 return isDivMember(n
);
245 return (!isRelationOperator(k
)) &&
246 (Theory::isLeafOf(n
, theory::THEORY_ARITH
));
250 static bool isDivMember(Node n
);
251 bool isDivLike() const{
252 return isDivMember(getNode());
255 bool isNormalForm() { return isMember(getNode()); }
257 bool isIntegral() const {
258 return getNode().getType().isInteger();
261 bool isMetaKindVariable() const {
262 return getNode().isVar();
265 bool operator<(const Variable
& v
) const {
266 bool thisIsVariable
= isMetaKindVariable();
267 bool vIsVariable
= v
.isMetaKindVariable();
269 if(thisIsVariable
== vIsVariable
){
270 bool thisIsInteger
= isIntegral();
271 bool vIsInteger
= v
.isIntegral();
272 if(thisIsInteger
== vIsInteger
){
273 return getNode() < v
.getNode();
275 return thisIsInteger
&& !vIsInteger
;
278 return thisIsVariable
&& !vIsVariable
;
282 bool operator==(const Variable
& v
) const { return getNode() == v
.getNode();}
284 };/* class Variable */
287 class Constant
: public NodeWrapper
{
289 Constant(Node n
) : NodeWrapper(n
) {
290 Assert(isMember(getNode()));
293 static bool isMember(Node n
) {
294 return n
.getKind() == kind::CONST_RATIONAL
;
297 bool isNormalForm() { return isMember(getNode()); }
299 static Constant
mkConstant(Node n
) {
300 Assert(n
.getKind() == kind::CONST_RATIONAL
);
304 static Constant
mkConstant(const Rational
& rat
) {
305 return Constant(mkRationalNode(rat
));
308 static Constant
mkZero() {
309 return mkConstant(Rational(0));
312 static Constant
mkOne() {
313 return mkConstant(Rational(1));
316 const Rational
& getValue() const {
317 return getNode().getConst
<Rational
>();
320 bool isIntegral() const { return getValue().isIntegral(); }
322 int sgn() const { return getValue().sgn(); }
324 bool isZero() const { return sgn() == 0; }
325 bool isNegative() const { return sgn() < 0; }
326 bool isPositive() const { return sgn() > 0; }
328 bool isOne() const { return getValue() == 1; }
330 Constant
operator*(const Rational
& other
) const {
331 return mkConstant(getValue() * other
);
334 Constant
operator*(const Constant
& other
) const {
335 return mkConstant(getValue() * other
.getValue());
337 Constant
operator+(const Constant
& other
) const {
338 return mkConstant(getValue() + other
.getValue());
340 Constant
operator-() const {
341 return mkConstant(-getValue());
344 Constant
inverse() const{
346 return mkConstant(getValue().inverse());
349 bool operator<(const Constant
& other
) const {
350 return getValue() < other
.getValue();
353 bool operator==(const Constant
& other
) const {
354 //Rely on node uniqueness.
355 return getNode() == other
.getNode();
358 Constant
abs() const {
366 uint32_t length() const{
367 Assert(isIntegral());
368 return getValue().getNumerator().length();
371 };/* class Constant */
374 template <class GetNodeIterator
>
375 inline Node
makeNode(Kind k
, GetNodeIterator start
, GetNodeIterator end
) {
378 while(start
!= end
) {
379 nb
<< (*start
).getNode();
384 }/* makeNode<GetNodeIterator>(Kind, iterator, iterator) */
387 template <class GetNodeIterator
, class T
>
388 static void copy_range(GetNodeIterator begin
, GetNodeIterator end
, std::vector
<T
>& result
){
390 result
.push_back(*begin
);
395 template <class GetNodeIterator
, class T
>
396 static void merge_ranges(GetNodeIterator first1
,
397 GetNodeIterator last1
,
398 GetNodeIterator first2
,
399 GetNodeIterator last2
,
400 std::vector
<T
>& result
) {
402 while(first1
!= last1
&& first2
!= last2
){
403 if( (*first1
) < (*first2
) ){
404 result
.push_back(*first1
);
407 result
.push_back(*first2
);
411 copy_range(first1
, last1
, result
);
412 copy_range(first2
, last2
, result
);
416 * A VarList is a sorted list of variables representing a product.
417 * If the VarList is empty, it represents an empty product or 1.
418 * If the VarList has size 1, it represents a single variable.
420 * A non-sorted VarList can never be successfully made in debug mode.
422 class VarList
: public NodeWrapper
{
425 static Node
multList(const std::vector
<Variable
>& list
) {
426 Assert(list
.size() >= 2);
428 return makeNode(kind::MULT
, list
.begin(), list
.end());
431 VarList() : NodeWrapper(Node::null()) {}
433 VarList(Node n
) : NodeWrapper(n
) {
434 Assert(isSorted(begin(), end()));
437 typedef expr::NodeSelfIterator internal_iterator
;
439 internal_iterator
internalBegin() const {
441 return expr::NodeSelfIterator::self(getNode());
443 return getNode().begin();
447 internal_iterator
internalEnd() const {
449 return expr::NodeSelfIterator::selfEnd(getNode());
451 return getNode().end();
459 internal_iterator d_iter
;
462 explicit iterator(internal_iterator i
) : d_iter(i
) {}
464 inline Variable
operator*() {
465 return Variable(*d_iter
);
468 bool operator==(const iterator
& i
) {
469 return d_iter
== i
.d_iter
;
472 bool operator!=(const iterator
& i
) {
473 return d_iter
!= i
.d_iter
;
476 iterator
operator++() {
481 iterator
operator++(int) {
482 return iterator(d_iter
++);
486 iterator
begin() const {
487 return iterator(internalBegin());
490 iterator
end() const {
491 return iterator(internalEnd());
494 Variable
getHead() const {
499 VarList(Variable v
) : NodeWrapper(v
.getNode()) {
500 Assert(isSorted(begin(), end()));
503 VarList(const std::vector
<Variable
>& l
) : NodeWrapper(multList(l
)) {
504 Assert(l
.size() >= 2);
505 Assert(isSorted(begin(), end()));
508 static bool isMember(Node n
);
510 bool isNormalForm() const {
514 static VarList
mkEmptyVarList() {
519 /** There are no restrictions on the size of l */
520 static VarList
mkVarList(const std::vector
<Variable
>& l
) {
522 return mkEmptyVarList();
523 } else if(l
.size() == 1) {
524 return VarList((*l
.begin()).getNode());
530 bool empty() const { return getNode().isNull(); }
531 bool singleton() const {
532 return !empty() && getNode().getKind() != kind::MULT
;
539 return getNode().getNumChildren();
542 static VarList
parseVarList(Node n
);
544 VarList
operator*(const VarList
& vl
) const;
546 int cmp(const VarList
& vl
) const;
548 bool operator<(const VarList
& vl
) const { return cmp(vl
) < 0; }
550 bool operator==(const VarList
& vl
) const { return cmp(vl
) == 0; }
552 bool isIntegral() const {
553 for(iterator i
= begin(), e
=end(); i
!= e
; ++i
){
555 if(!var
.isIntegral()){
563 bool isSorted(iterator start
, iterator end
);
565 };/* class VarList */
568 class Monomial
: public NodeWrapper
{
572 Monomial(Node n
, const Constant
& c
, const VarList
& vl
):
573 NodeWrapper(n
), constant(c
), varList(vl
)
575 Assert(!c
.isZero() || vl
.empty() );
576 Assert( c
.isZero() || !vl
.empty() );
578 Assert(!c
.isOne() || !multStructured(n
));
581 static Node
makeMultNode(const Constant
& c
, const VarList
& vl
) {
585 return NodeManager::currentNM()->mkNode(kind::MULT
, c
.getNode(), vl
.getNode());
588 static bool multStructured(Node n
) {
589 return n
.getKind() == kind::MULT
&&
590 n
[0].getKind() == kind::CONST_RATIONAL
&&
591 n
.getNumChildren() == 2;
596 Monomial(const Constant
& c
):
597 NodeWrapper(c
.getNode()), constant(c
), varList(VarList::mkEmptyVarList())
600 Monomial(const VarList
& vl
):
601 NodeWrapper(vl
.getNode()), constant(Constant::mkConstant(1)), varList(vl
)
603 Assert( !varList
.empty() );
606 Monomial(const Constant
& c
, const VarList
& vl
):
607 NodeWrapper(makeMultNode(c
,vl
)), constant(c
), varList(vl
)
609 Assert( !c
.isZero() );
610 Assert( !c
.isOne() );
611 Assert( !varList
.empty() );
613 Assert(multStructured(getNode()));
616 static bool isMember(TNode n
);
618 /** Makes a monomial with no restrictions on c and vl. */
619 static Monomial
mkMonomial(const Constant
& c
, const VarList
& vl
);
621 static Monomial
mkMonomial(const Variable
& v
){
622 return Monomial(VarList(v
));
625 static Monomial
parseMonomial(Node n
);
627 static Monomial
mkZero() {
628 return Monomial(Constant::mkConstant(0));
630 static Monomial
mkOne() {
631 return Monomial(Constant::mkConstant(1));
633 const Constant
& getConstant() const { return constant
; }
634 const VarList
& getVarList() const { return varList
; }
636 bool isConstant() const {
637 return varList
.empty();
640 bool isZero() const {
641 return constant
.isZero();
644 bool coefficientIsOne() const {
645 return constant
.isOne();
648 bool absCoefficientIsOne() const {
649 return coefficientIsOne() || constant
.getValue() == -1;
652 bool constantIsPositive() const {
653 return getConstant().isPositive();
656 Monomial
operator*(const Rational
& q
) const;
657 Monomial
operator*(const Constant
& c
) const;
658 Monomial
operator*(const Monomial
& mono
) const;
660 Monomial
operator-() const{
661 return (*this) * Rational(-1);
665 int cmp(const Monomial
& mono
) const {
666 return getVarList().cmp(mono
.getVarList());
669 bool operator<(const Monomial
& vl
) const {
673 bool operator==(const Monomial
& vl
) const {
677 static bool isSorted(const std::vector
<Monomial
>& m
) {
678 return __gnu_cxx::is_sorted(m
.begin(), m
.end());
681 static bool isStrictlySorted(const std::vector
<Monomial
>& m
) {
682 return isSorted(m
) && std::adjacent_find(m
.begin(),m
.end()) == m
.end();
686 * The variable product
688 bool integralVariables() const {
689 return getVarList().isIntegral();
693 * The coefficient of the monomial is integral.
695 bool integralCoefficient() const {
696 return getConstant().isIntegral();
700 * A Monomial is an "integral" monomial if the constant is integral.
702 bool isIntegral() const {
703 return integralCoefficient() && integralVariables();
707 * Given a sorted list of monomials, this function transforms this
708 * into a strictly sorted list of monomials that does not contain zero.
710 static std::vector
<Monomial
> sumLikeTerms(const std::vector
<Monomial
>& monos
);
712 bool absLessThan(const Monomial
& other
) const{
713 return getConstant().abs() < other
.getConstant().abs();
716 uint32_t coefficientLength() const{
717 return getConstant().length();
721 static void printList(const std::vector
<Monomial
>& list
);
723 };/* class Monomial */
728 class Polynomial
: public NodeWrapper
{
732 Polynomial(TNode n
) : NodeWrapper(n
), d_singleton(Monomial::isMember(n
)) {
733 Assert(isMember(getNode()));
736 static Node
makePlusNode(const std::vector
<Monomial
>& m
) {
737 Assert(m
.size() >= 2);
739 return makeNode(kind::PLUS
, m
.begin(), m
.end());
742 typedef expr::NodeSelfIterator internal_iterator
;
744 internal_iterator
internalBegin() const {
746 return expr::NodeSelfIterator::self(getNode());
748 return getNode().begin();
752 internal_iterator
internalEnd() const {
754 return expr::NodeSelfIterator::selfEnd(getNode());
756 return getNode().end();
760 bool singleton() const { return d_singleton
; }
763 static bool isMember(TNode n
) {
764 if(Monomial::isMember(n
)){
766 }else if(n
.getKind() == kind::PLUS
){
767 Assert(n
.getNumChildren() >= 2);
768 Node::iterator currIter
= n
.begin(), end
= n
.end();
769 Node prev
= *currIter
;
770 if(!Monomial::isMember(prev
)){
774 Monomial mprev
= Monomial::parseMonomial(prev
);
776 for(; currIter
!= end
; ++currIter
){
777 Node curr
= *currIter
;
778 if(!Monomial::isMember(curr
)){
781 Monomial mcurr
= Monomial::parseMonomial(curr
);
782 if(!(mprev
< mcurr
)){
795 internal_iterator d_iter
;
798 explicit iterator(internal_iterator i
) : d_iter(i
) {}
800 inline Monomial
operator*() {
801 return Monomial::parseMonomial(*d_iter
);
804 bool operator==(const iterator
& i
) {
805 return d_iter
== i
.d_iter
;
808 bool operator!=(const iterator
& i
) {
809 return d_iter
!= i
.d_iter
;
812 iterator
operator++() {
817 iterator
operator++(int) {
818 return iterator(d_iter
++);
822 iterator
begin() const { return iterator(internalBegin()); }
823 iterator
end() const { return iterator(internalEnd()); }
825 Polynomial(const Monomial
& m
):
826 NodeWrapper(m
.getNode()), d_singleton(true)
829 Polynomial(const std::vector
<Monomial
>& m
):
830 NodeWrapper(makePlusNode(m
)), d_singleton(false)
832 Assert( m
.size() >= 2);
833 Assert( Monomial::isStrictlySorted(m
) );
836 static Polynomial
mkPolynomial(const Variable
& v
){
837 return Monomial::mkMonomial(v
);
840 static Polynomial
mkPolynomial(const std::vector
<Monomial
>& m
) {
842 return Polynomial(Monomial::mkZero());
843 } else if(m
.size() == 1) {
844 return Polynomial((*m
.begin()));
846 return Polynomial(m
);
850 static Polynomial
parsePolynomial(Node n
) {
851 return Polynomial(n
);
854 static Polynomial
mkZero() {
855 return Polynomial(Monomial::mkZero());
857 static Polynomial
mkOne() {
858 return Polynomial(Monomial::mkOne());
860 bool isZero() const {
861 return singleton() && (getHead().isZero());
864 bool isConstant() const {
865 return singleton() && (getHead().isConstant());
868 bool containsConstant() const {
869 return getHead().isConstant();
872 uint32_t size() const{
876 Assert(getNode().getKind() == kind::PLUS
);
877 return getNode().getNumChildren();
881 Monomial
getHead() const {
885 Polynomial
getTail() const {
886 Assert(! singleton());
888 iterator tailStart
= begin();
890 std::vector
<Monomial
> subrange
;
891 copy_range(tailStart
, end(), subrange
);
892 return mkPolynomial(subrange
);
895 Monomial
minimumVariableMonomial() const;
896 bool variableMonomialAreStrictlyGreater(const Monomial
& m
) const;
898 void printList() const {
899 if(Debug
.isOn("normal-form")){
900 Debug("normal-form") << "start list" << std::endl
;
901 for(iterator i
= begin(), oend
= end(); i
!= oend
; ++i
) {
902 const Monomial
& m
=*i
;
905 Debug("normal-form") << "end list" << std::endl
;
909 /** A Polynomial is an "integral" polynomial if all of the monomials are integral. */
910 bool allIntegralVariables() const {
911 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
912 if(!(*i
).integralVariables()){
920 * A Polynomial is an "integral" polynomial if all of the monomials are integral
921 * and all of the coefficients are Integral. */
922 bool isIntegral() const {
923 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
924 if(!(*i
).isIntegral()){
932 * Selects a minimal monomial in the polynomial by the absolute value of
935 Monomial
selectAbsMinimum() const;
937 /** Returns true if the absolute value of the head coefficient is one. */
938 bool leadingCoefficientIsAbsOne() const;
939 bool leadingCoefficientIsPositive() const;
940 bool denominatorLCMIsOne() const;
941 bool numeratorGCDIsOne() const;
943 bool signNormalizedReducedSum() const {
944 return leadingCoefficientIsPositive() && denominatorLCMIsOne() && numeratorGCDIsOne();
948 * Returns the Least Common Multiple of the denominators of the coefficients
951 Integer
denominatorLCM() const;
954 * Returns the GCD of the numerators of the monomials.
955 * Requires this to be an isIntegral() polynomial.
957 Integer
numeratorGCD() const;
960 * Returns the GCD of the coefficients of the monomials.
961 * Requires this to be an isIntegral() polynomial.
965 Polynomial
exactDivide(const Integer
& z
) const {
966 Assert(isIntegral());
967 Constant invz
= Constant::mkConstant(Rational(1,z
));
968 Polynomial prod
= (*this) * Monomial(invz
);
969 Assert(prod
.isIntegral());
973 Polynomial
operator+(const Polynomial
& vl
) const;
974 Polynomial
operator-(const Polynomial
& vl
) const;
975 Polynomial
operator-() const{
976 return (*this) * Rational(-1);
979 Polynomial
operator*(const Rational
& q
) const;
980 Polynomial
operator*(const Constant
& c
) const;
981 Polynomial
operator*(const Monomial
& mono
) const;
983 Polynomial
operator*(const Polynomial
& poly
) const;
986 * Viewing the integer polynomial as a list [(* coeff_i mono_i)]
987 * The quotient and remainder of p divided by the non-zero integer z is:
988 * q := [(* floor(coeff_i/z) mono_i )]
989 * r := [(* rem(coeff_i/z) mono_i)]
990 * computeQR(p,z) returns the node (+ q r).
992 * q and r are members of the Polynomial class.
994 * computeQR( p = (+ 5 (* 3 x) (* 8 y)) , z = 2) returns
995 * (+ (+ 2 x (* 4 y)) (+ 1 x))
997 static Node
computeQR(const Polynomial
& p
, const Integer
& z
);
999 /** Returns the coefficient associated with the VarList in the polynomial. */
1000 Constant
getCoefficient(const VarList
& vl
) const;
1002 uint32_t maxLength() const{
1003 iterator i
= begin(), e
=end();
1007 uint32_t max
= (*i
).coefficientLength();
1010 uint32_t curr
= (*i
).coefficientLength();
1019 uint32_t numMonomials() const {
1020 if( getNode().getKind() == kind::PLUS
){
1021 return getNode().getNumChildren();
1029 const Rational
& asConstant() const{
1030 Assert(isConstant());
1031 return getNode().getConst
<Rational
>();
1032 //return getHead().getConstant().getValue();
1035 bool isVarList() const {
1037 return VarList::isMember(getNode());
1043 VarList
asVarList() const {
1044 Assert(isVarList());
1045 return getHead().getVarList();
1048 friend class SumPair
;
1049 friend class Comparison
;
1051 /** Returns a node that if asserted ensures v is the abs of this polynomial.*/
1052 Node
makeAbsCondition(Variable v
){
1053 return makeAbsCondition(v
, *this);
1056 /** Returns a node that if asserted ensures v is the abs of p.*/
1057 static Node
makeAbsCondition(Variable v
, Polynomial p
);
1059 };/* class Polynomial */
1063 * SumPair is a utility class that extends polynomials for use in computations.
1064 * A SumPair is always a combination of (+ p c) where
1065 * c is a constant and p is a polynomial such that p = 0 or !p.containsConstant().
1067 * These are a useful utility for representing the equation p = c as (+ p -c) where the pair
1068 * is known to implicitly be equal to 0.
1070 * SumPairs do not have unique representations due to the potential for p = 0.
1071 * This makes them inappropriate for normal forms.
1073 class SumPair
: public NodeWrapper
{
1075 static Node
toNode(const Polynomial
& p
, const Constant
& c
){
1076 return NodeManager::currentNM()->mkNode(kind::PLUS
, p
.getNode(), c
.getNode());
1082 Assert(isNormalForm());
1087 SumPair(const Polynomial
& p
):
1088 NodeWrapper(toNode(p
, Constant::mkConstant(0)))
1090 Assert(isNormalForm());
1093 SumPair(const Polynomial
& p
, const Constant
& c
):
1094 NodeWrapper(toNode(p
, c
))
1096 Assert(isNormalForm());
1099 static bool isMember(TNode n
) {
1100 if(n
.getKind() == kind::PLUS
&& n
.getNumChildren() == 2){
1101 if(Constant::isMember(n
[1])){
1102 if(Polynomial::isMember(n
[0])){
1103 Polynomial p
= Polynomial::parsePolynomial(n
[0]);
1104 return p
.isZero() || (!p
.containsConstant());
1116 bool isNormalForm() const {
1117 return isMember(getNode());
1120 Polynomial
getPolynomial() const {
1121 return Polynomial::parsePolynomial(getNode()[0]);
1124 Constant
getConstant() const {
1125 return Constant::mkConstant((getNode())[1]);
1128 SumPair
operator+(const SumPair
& other
) const {
1129 return SumPair(getPolynomial() + other
.getPolynomial(),
1130 getConstant() + other
.getConstant());
1133 SumPair
operator*(const Constant
& c
) const {
1134 return SumPair(getPolynomial() * c
, getConstant() * c
);
1137 SumPair
operator-(const SumPair
& other
) const {
1138 return (*this) + (other
* Constant::mkConstant(-1));
1141 static SumPair
mkSumPair(const Polynomial
& p
);
1143 static SumPair
mkSumPair(const Variable
& var
){
1144 return SumPair(Polynomial::mkPolynomial(var
));
1147 static SumPair
parseSumPair(TNode n
){
1151 bool isIntegral() const{
1152 return getConstant().isIntegral() && getPolynomial().isIntegral();
1155 bool isConstant() const {
1156 return getPolynomial().isZero();
1159 bool isZero() const {
1160 return getConstant().isZero() && isConstant();
1164 * Returns the greatest common divisor of gcd(getPolynomial()) and getConstant().
1165 * The SumPair must be integral.
1167 Integer
gcd() const {
1168 Assert(isIntegral());
1169 return (getPolynomial().gcd()).gcd(getConstant().getValue().getNumerator());
1172 uint32_t maxLength() const {
1173 Assert(isIntegral());
1174 return std::max(getPolynomial().maxLength(), getConstant().length());
1177 static SumPair
mkZero() {
1178 return SumPair(Polynomial::mkZero(), Constant::mkConstant(0));
1181 static Node
computeQR(const SumPair
& sp
, const Integer
& div
);
1183 };/* class SumPair */
1185 /* class OrderedPolynomialPair { */
1187 /* Polynomial d_first; */
1188 /* Polynomial d_second; */
1190 /* OrderedPolynomialPair(const Polynomial& f, const Polynomial& s) */
1195 /* /\** Returns the first part of the pair. *\/ */
1196 /* const Polynomial& getFirst() const { */
1197 /* return d_first; */
1200 /* /\** Returns the second part of the pair. *\/ */
1201 /* const Polynomial& getSecond() const { */
1202 /* return d_second; */
1205 /* OrderedPolynomialPair operator*(const Constant& c) const; */
1206 /* OrderedPolynomialPair operator+(const Polynomial& p) const; */
1208 /* /\** Returns true if both of the polynomials are constant. *\/ */
1209 /* bool isConstant() const; */
1212 /* * Evaluates an isConstant() ordered pair as if */
1213 /* * (k getFirst() getRight()) */
1215 /* bool evaluateConstant(Kind k) const; */
1218 /* * Returns the Least Common Multiple of the monomials */
1219 /* * on the lefthand side and the constant on the right. */
1221 /* Integer denominatorLCM() const; */
1223 /* /\** Constructs a SumPair. *\/ */
1224 /* SumPair toSumPair() const; */
1227 /* OrderedPolynomialPair divideByGCD() const; */
1228 /* OrderedPolynomialPair multiplyConstant(const Constant& c) const; */
1231 /* * Returns true if all of the variables are integers, */
1232 /* * and the coefficients are integers. */
1234 /* bool isIntegral() const; */
1236 /* /\** Returns true if all of the variables are integers. *\/ */
1237 /* bool allIntegralVariables() const { */
1238 /* return getFirst().allIntegralVariables() && getSecond().allIntegralVariables(); */
1242 class Comparison
: public NodeWrapper
{
1245 static Node
toNode(Kind k
, const Polynomial
& l
, const Constant
& c
);
1246 static Node
toNode(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1248 Comparison(TNode n
);
1251 * Creates a node in normal form equivalent to (= l 0).
1252 * All variables in l are integral.
1254 static Node
mkIntEquality(const Polynomial
& l
);
1257 * Creates a comparison equivalent to (k l 0).
1258 * k is either GT or GEQ.
1259 * All variables in l are integral.
1261 static Node
mkIntInequality(Kind k
, const Polynomial
& l
);
1264 * Creates a node equivalent to (= l 0).
1265 * It is not the case that all variables in l are integral.
1267 static Node
mkRatEquality(const Polynomial
& l
);
1270 * Creates a comparison equivalent to (k l 0).
1271 * k is either GT or GEQ.
1272 * It is not the case that all variables in l are integral.
1274 static Node
mkRatInequality(Kind k
, const Polynomial
& l
);
1278 Comparison(bool val
) :
1279 NodeWrapper(NodeManager::currentNM()->mkConst(val
))
1283 * Given a literal to TheoryArith return a single kind to
1284 * to indicate its underlying structure.
1285 * The function returns the following in each case:
1286 * - (K left right) -> K where is either EQUAL, GT, or GEQ
1287 * - (CONST_BOOLEAN b) -> CONST_BOOLEAN
1288 * - (NOT (EQUAL left right)) -> DISTINCT
1289 * - (NOT (GT left right)) -> LEQ
1290 * - (NOT (GEQ left right)) -> LT
1291 * If none of these match, it returns UNDEFINED_KIND.
1293 static Kind
comparisonKind(TNode literal
);
1295 Kind
comparisonKind() const { return comparisonKind(getNode()); }
1297 static Comparison
mkComparison(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1299 /** Returns true if the comparison is a boolean constant. */
1300 bool isBoolean() const;
1303 * Returns true if the comparison is either a boolean term,
1304 * in integer normal form or mixed normal form.
1306 bool isNormalForm() const;
1309 bool isNormalGT() const;
1310 bool isNormalGEQ() const;
1312 bool isNormalLT() const;
1313 bool isNormalLEQ() const;
1315 bool isNormalEquality() const;
1316 bool isNormalDistinct() const;
1317 bool isNormalEqualityOrDisequality() const;
1319 bool allIntegralVariables() const {
1320 return getLeft().allIntegralVariables() && getRight().allIntegralVariables();
1322 bool rightIsConstant() const;
1325 Polynomial
getLeft() const;
1326 Polynomial
getRight() const;
1328 /* /\** Normal form check if at least one variable is real. *\/ */
1329 /* bool isMixedCompareNormalForm() const; */
1331 /* /\** Normal form check if at least one variable is real. *\/ */
1332 /* bool isMixedEqualsNormalForm() const; */
1334 /* /\** Normal form check is all variables are integer.*\/ */
1335 /* bool isIntegerCompareNormalForm() const; */
1337 /* /\** Normal form check is all variables are integer.*\/ */
1338 /* bool isIntegerEqualsNormalForm() const; */
1342 * Returns true if all of the variables are integers, the coefficients are integers,
1343 * and the right hand coefficient is an integer.
1345 bool debugIsIntegral() const;
1347 static Comparison
parseNormalForm(TNode n
);
1349 inline static bool isNormalAtom(TNode n
){
1350 Comparison parse
= Comparison::parseNormalForm(n
);
1351 return parse
.isNormalForm();
1354 SumPair
toSumPair() const;
1356 Polynomial
normalizedVariablePart() const;
1357 DeltaRational
normalizedDeltaRational() const;
1359 };/* class Comparison */
1361 }/* CVC4::theory::arith namespace */
1362 }/* CVC4::theory namespace */
1363 }/* CVC4 namespace */
1365 #endif /* __CVC4__THEORY__ARITH__NORMAL_FORM_H */