1 /********************* */
2 /*! \file bounded_integers.h
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Mathias Preiner, Mudathir Mohamed
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8 ** in the top-level source directory and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
12 ** [[ Add lengthier description here ]]
13 ** \todo document this file
16 #include "cvc4_private.h"
18 #ifndef CVC4__BOUNDED_INTEGERS_H
19 #define CVC4__BOUNDED_INTEGERS_H
21 #include "theory/quantifiers/quant_module.h"
23 #include "context/cdhashmap.h"
24 #include "context/context.h"
25 #include "expr/attribute.h"
26 #include "theory/decision_strategy.h"
32 class DecisionManager
;
35 * Attribute set to 1 for literals that comprise the bounds of a quantified
36 * formula. For example, for:
37 * forall x. ( 0 <= x ^ x <= n ) => P( x )
38 * the literals 0 <= x and x <= n are marked 1.
40 struct BoundIntLitAttributeId
43 typedef expr::Attribute
<BoundIntLitAttributeId
, uint64_t> BoundIntLitAttribute
;
45 namespace quantifiers
{
47 class BoundedIntegers
: public QuantifiersModule
49 typedef context::CDHashMap
<Node
, bool, NodeHashFunction
> NodeBoolMap
;
50 typedef context::CDHashMap
<Node
, int, NodeHashFunction
> NodeIntMap
;
51 typedef context::CDHashMap
<Node
, Node
, NodeHashFunction
> NodeNodeMap
;
52 typedef context::CDHashMap
<int, bool> IntBoolMap
;
54 //for determining bounds
55 bool hasNonBoundVar( Node f
, Node b
, std::map
< Node
, bool >& visited
);
56 bool hasNonBoundVar( Node f
, Node b
);
57 /** The bound type for each quantified formula, variable pair */
58 std::map
<Node
, std::map
<Node
, BoundVarType
>> d_bound_type
;
60 * The ordered list of variables that are finitely bound, for each quantified
61 * formulas. Variables that occur later in this list may depend on having
62 * finite bounds for variables earlier in this list.
64 std::map
< Node
, std::vector
< Node
> > d_set
;
65 std::map
< Node
, std::map
< Node
, int > > d_set_nums
;
66 std::map
< Node
, std::map
< Node
, Node
> > d_range
;
67 std::map
< Node
, std::map
< Node
, Node
> > d_nground_range
;
68 //integer lower/upper bounds
69 std::map
< Node
, std::map
< Node
, Node
> > d_bounds
[2];
70 //set membership range
71 std::map
< Node
, std::map
< Node
, Node
> > d_setm_range
;
72 std::map
< Node
, std::map
< Node
, Node
> > d_setm_range_lit
;
73 /** set membership element choice functions
75 * For each set S and integer n, d_setm_choice[S][n] is the canonical
76 * representation for the (n+1)^th member of set S. It is of the form:
77 * witness x. (|S| <= n OR ( x in S AND
78 * distinct( x, d_setm_choice[S][0], ..., d_setm_choice[S][n-1] ) ) )
80 std::map
<Node
, std::vector
<Node
> > d_setm_choice
;
81 //fixed finite set range
82 std::map
< Node
, std::map
< Node
, std::vector
< Node
> > > d_fixed_set_gr_range
;
83 std::map
< Node
, std::map
< Node
, std::vector
< Node
> > > d_fixed_set_ngr_range
;
84 void process( Node q
, Node n
, bool pol
,
85 std::map
< Node
, unsigned >& bound_lit_type_map
,
86 std::map
< int, std::map
< Node
, Node
> >& bound_lit_map
,
87 std::map
< int, std::map
< Node
, bool > >& bound_lit_pol_map
,
88 std::map
< int, std::map
< Node
, Node
> >& bound_int_range_term
,
89 std::map
< Node
, std::vector
< Node
> >& bound_fixed_set
);
90 bool processEqDisjunct( Node q
, Node n
, Node
& v
, std::vector
< Node
>& v_cases
);
91 void processMatchBoundVars( Node q
, Node n
, std::vector
< Node
>& bvs
, std::map
< Node
, bool >& visited
);
92 std::vector
< Node
> d_bound_quants
;
95 * This decision strategy is used for minimizing the value of an integer
96 * arithmetic term t. It decides positively on literals of the form
97 * t < 0, t <= 0, t <= 1, t <=2, and so on.
99 class IntRangeDecisionHeuristic
: public DecisionStrategyFmf
102 IntRangeDecisionHeuristic(Node r
,
107 /** make the n^th literal of this strategy */
108 Node
mkLiteral(unsigned n
) override
;
110 std::string
identify() const override
112 return std::string("bound_int_range");
114 /** Returns the current proxy lemma if one exists (see below). */
115 Node
proxyCurrentRangeLemma();
118 /** The range we are minimizing */
120 /** a proxy of the range
122 * When option::fmfBoundLazy is enabled, this class uses a lazy strategy
123 * for enforcing the bounds on term t by using a fresh variable x of type
124 * integer. The point of this variable is to serve as a proxy for t, so
125 * that we can decide on literals of the form x <= c instead of t <= c. The
126 * advantage of this is that we avoid unfairness, say, if t is constrained
127 * to be strictly greater c. Then, at full effort check, we add "proxy
128 * lemmas" of the form: (t <= c) <=> (x <= c) for the current minimal
129 * upper bound c for x.
132 /** ranges that have been proxied
134 * This is a user-context-dependent cache that stores which value we have
135 * added proxy lemmas for.
137 IntBoolMap d_ranges_proxied
;
140 //information for minimizing ranges
141 std::vector
< Node
> d_ranges
;
142 /** Decision heuristics for each integer range */
143 std::map
<Node
, std::unique_ptr
<IntRangeDecisionHeuristic
>> d_rms
;
146 //class to store whether bounding lemmas have been added
150 std::map
< Node
, BoundInstTrie
> d_children
;
151 bool hasInstantiated( std::vector
< Node
> & vals
, int index
= 0, bool madeNew
= false ){
152 if( index
>=(int)vals
.size() ){
155 Node n
= vals
[index
];
156 if( d_children
.find(n
)==d_children
.end() ){
159 return d_children
[n
].hasInstantiated(vals
,index
+1,madeNew
);
163 std::map
< Node
, std::map
< Node
, BoundInstTrie
> > d_bnd_it
;
166 BoundedIntegers(QuantifiersEngine
* qe
,
167 QuantifiersState
& qs
,
168 QuantifiersInferenceManager
& qim
,
169 QuantifiersRegistry
& qr
,
170 DecisionManager
* dm
);
171 virtual ~BoundedIntegers();
173 void presolve() override
;
174 bool needsCheck(Theory::Effort e
) override
;
175 void check(Theory::Effort e
, QEffort quant_e
) override
;
176 void checkOwnership(Node q
) override
;
178 * Is v a variable of quantified formula q that this class has inferred to
179 * have a finite bound?
181 bool isBound(Node q
, Node v
) const;
183 * Get the type of bound that was inferred for variable v of quantified
184 * formula q, or BOUND_NONE if no bound was inferred.
186 BoundVarType
getBoundVarType(Node q
, Node v
) const;
188 * Get the indices of bound variables, in the order they should be processed
189 * in a RepSetIterator. For example, for q:
190 * forall xyz. 0 <= x < 5 ^ 0 <= z <= x+7 => P(x,y,z)
191 * this would add {1,3} to the vector indices, indicating that x has a finite
192 * bound, z has a finite bound assuming x has a finite bound, and y does not
193 * have a finite bound.
195 void getBoundVarIndices(Node q
, std::vector
<unsigned>& indices
) const;
199 * This gets the (finite) enumeration of the range of variable v of quantified
200 * formula q and adds it into the vector elements in the context of the
201 * iteration being performed by rsi. It returns true if it could successfully
202 * determine this range.
204 * This method determines the range of a variable depending on the current
205 * state of the iterator rsi and flag initial (which is true when rsi is
206 * being initialized). For example, if q is:
207 * forall xy. 0 <= x < 5 ^ 0 <= y <= x+7 => P(x,y)
208 * v is y, and rsi currently maps x to 4, then we add the elements 0...11 to
209 * the vector elements.
211 bool getBoundElements(RepSetIterator
* rsi
,
215 std::vector
<Node
>& elements
);
216 /** Identify this module */
217 std::string
identify() const override
{ return "BoundedIntegers"; }
221 * Set that variable v of quantified formula q has a finite bound, where
222 * bound_type indicates how that bound was inferred.
224 void setBoundedVar(Node f
, Node v
, BoundVarType bound_type
);
226 Node
getLowerBound( Node q
, Node v
){ return d_bounds
[0][q
][v
]; }
227 Node
getUpperBound( Node q
, Node v
){ return d_bounds
[1][q
][v
]; }
228 void getBounds( Node f
, Node v
, RepSetIterator
* rsi
, Node
& l
, Node
& u
);
229 void getBoundValues( Node f
, Node v
, RepSetIterator
* rsi
, Node
& l
, Node
& u
);
230 bool isGroundRange(Node f
, Node v
);
232 Node
getSetRange( Node q
, Node v
, RepSetIterator
* rsi
);
233 Node
getSetRangeValue( Node q
, Node v
, RepSetIterator
* rsi
);
234 Node
matchBoundVar( Node v
, Node t
, Node e
);
236 bool getRsiSubsitution( Node q
, Node v
, std::vector
< Node
>& vars
, std::vector
< Node
>& subs
, RepSetIterator
* rsi
);
237 /** Pointer to the decision manager */
238 DecisionManager
* d_dm
;