1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, Morgan Deters, Aina Niemetz |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
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. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* Implementation of theory uf candidate generator class. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "theory/quantifiers/ematching/candidate_generator.h" |
17 |
|
|
18 |
|
#include "expr/dtype.h" |
19 |
|
#include "expr/dtype_cons.h" |
20 |
|
#include "options/quantifiers_options.h" |
21 |
|
#include "smt/smt_engine.h" |
22 |
|
#include "smt/smt_engine_scope.h" |
23 |
|
#include "theory/datatypes/datatypes_rewriter.h" |
24 |
|
#include "theory/quantifiers/first_order_model.h" |
25 |
|
#include "theory/quantifiers/quantifiers_state.h" |
26 |
|
#include "theory/quantifiers/term_database.h" |
27 |
|
#include "theory/quantifiers/term_registry.h" |
28 |
|
#include "theory/quantifiers/term_util.h" |
29 |
|
|
30 |
|
using namespace cvc5::kind; |
31 |
|
|
32 |
|
namespace cvc5 { |
33 |
|
namespace theory { |
34 |
|
namespace quantifiers { |
35 |
|
namespace inst { |
36 |
|
|
37 |
35103 |
CandidateGenerator::CandidateGenerator(QuantifiersState& qs, TermRegistry& tr) |
38 |
35103 |
: d_qs(qs), d_treg(tr) |
39 |
|
{ |
40 |
35103 |
} |
41 |
|
|
42 |
3769987 |
bool CandidateGenerator::isLegalCandidate( Node n ){ |
43 |
11309961 |
return d_treg.getTermDatabase()->isTermActive(n) |
44 |
15079948 |
&& (!options::cegqi() || !quantifiers::TermUtil::hasInstConstAttr(n)); |
45 |
|
} |
46 |
|
|
47 |
35025 |
CandidateGeneratorQE::CandidateGeneratorQE(QuantifiersState& qs, |
48 |
|
TermRegistry& tr, |
49 |
35025 |
Node pat) |
50 |
|
: CandidateGenerator(qs, tr), |
51 |
|
d_term_iter(-1), |
52 |
|
d_term_iter_limit(0), |
53 |
35025 |
d_mode(cand_term_none) |
54 |
|
{ |
55 |
35025 |
d_op = d_treg.getTermDatabase()->getMatchOperator(pat); |
56 |
35025 |
Assert(!d_op.isNull()); |
57 |
35025 |
} |
58 |
|
|
59 |
491280 |
void CandidateGeneratorQE::reset(Node eqc) { resetForOperator(eqc, d_op); } |
60 |
|
|
61 |
517341 |
void CandidateGeneratorQE::resetForOperator(Node eqc, Node op) |
62 |
|
{ |
63 |
517341 |
d_term_iter = 0; |
64 |
517341 |
d_eqc = eqc; |
65 |
517341 |
d_op = op; |
66 |
517341 |
d_term_iter_limit = d_treg.getTermDatabase()->getNumGroundTerms(d_op); |
67 |
517341 |
if( eqc.isNull() ){ |
68 |
118120 |
d_mode = cand_term_db; |
69 |
|
}else{ |
70 |
399221 |
if( isExcludedEqc( eqc ) ){ |
71 |
|
d_mode = cand_term_none; |
72 |
|
}else{ |
73 |
399221 |
eq::EqualityEngine* ee = d_qs.getEqualityEngine(); |
74 |
399221 |
if( ee->hasTerm( eqc ) ){ |
75 |
399221 |
TNodeTrie* tat = d_treg.getTermDatabase()->getTermArgTrie(eqc, op); |
76 |
399221 |
if( tat ){ |
77 |
|
//create an equivalence class iterator in eq class eqc |
78 |
388012 |
Node rep = ee->getRepresentative( eqc ); |
79 |
194006 |
d_eqc_iter = eq::EqClassIterator( rep, ee ); |
80 |
194006 |
d_mode = cand_term_eqc; |
81 |
|
}else{ |
82 |
205215 |
d_mode = cand_term_none; |
83 |
|
} |
84 |
|
}else{ |
85 |
|
//the only match is this term itself |
86 |
|
d_mode = cand_term_ident; |
87 |
|
} |
88 |
|
} |
89 |
|
} |
90 |
517341 |
} |
91 |
855935 |
bool CandidateGeneratorQE::isLegalOpCandidate( Node n ) { |
92 |
855935 |
if( n.hasOperator() ){ |
93 |
772424 |
if( isLegalCandidate( n ) ){ |
94 |
429574 |
return d_treg.getTermDatabase()->getMatchOperator(n) == d_op; |
95 |
|
} |
96 |
|
} |
97 |
426361 |
return false; |
98 |
|
} |
99 |
|
|
100 |
3202091 |
Node CandidateGeneratorQE::getNextCandidate(){ |
101 |
3202091 |
return getNextCandidateInternal(); |
102 |
|
} |
103 |
|
|
104 |
3253209 |
Node CandidateGeneratorQE::getNextCandidateInternal() |
105 |
|
{ |
106 |
3253209 |
if( d_mode==cand_term_db ){ |
107 |
2653136 |
Debug("cand-gen-qe") << "...get next candidate in tbd" << std::endl; |
108 |
|
//get next candidate term in the uf term database |
109 |
3501142 |
while( d_term_iter<d_term_iter_limit ){ |
110 |
3419478 |
Node n = d_treg.getTermDatabase()->getGroundTerm(d_op, d_term_iter); |
111 |
2995475 |
d_term_iter++; |
112 |
2995475 |
if( isLegalCandidate( n ) ){ |
113 |
2595446 |
if (d_treg.getTermDatabase()->hasTermCurrent(n)) |
114 |
|
{ |
115 |
2595446 |
if( d_exclude_eqc.empty() ){ |
116 |
2500853 |
return n; |
117 |
|
}else{ |
118 |
118567 |
Node r = d_qs.getRepresentative(n); |
119 |
94593 |
if( d_exclude_eqc.find( r )==d_exclude_eqc.end() ){ |
120 |
70619 |
Debug("cand-gen-qe") << "...returning " << n << std::endl; |
121 |
70619 |
return n; |
122 |
|
} |
123 |
|
} |
124 |
|
} |
125 |
|
} |
126 |
|
} |
127 |
600073 |
}else if( d_mode==cand_term_eqc ){ |
128 |
391797 |
Debug("cand-gen-qe") << "...get next candidate in eqc" << std::endl; |
129 |
1523197 |
while( !d_eqc_iter.isFinished() ){ |
130 |
1421635 |
Node n = *d_eqc_iter; |
131 |
855935 |
++d_eqc_iter; |
132 |
855935 |
if( isLegalOpCandidate( n ) ){ |
133 |
290235 |
Debug("cand-gen-qe") << "...returning " << n << std::endl; |
134 |
290235 |
return n; |
135 |
|
} |
136 |
|
} |
137 |
208276 |
}else if( d_mode==cand_term_ident ){ |
138 |
3061 |
Debug("cand-gen-qe") << "...get next candidate identity" << std::endl; |
139 |
3061 |
if (!d_eqc.isNull()) |
140 |
|
{ |
141 |
2139 |
Node n = d_eqc; |
142 |
2048 |
d_eqc = Node::null(); |
143 |
2048 |
if( isLegalOpCandidate( n ) ){ |
144 |
1957 |
return n; |
145 |
|
} |
146 |
|
} |
147 |
|
} |
148 |
389545 |
return Node::null(); |
149 |
|
} |
150 |
|
|
151 |
7 |
CandidateGeneratorQELitDeq::CandidateGeneratorQELitDeq(QuantifiersState& qs, |
152 |
|
TermRegistry& tr, |
153 |
7 |
Node mpat) |
154 |
7 |
: CandidateGenerator(qs, tr), d_match_pattern(mpat) |
155 |
|
{ |
156 |
7 |
Assert(d_match_pattern.getKind() == EQUAL); |
157 |
7 |
d_match_pattern_type = d_match_pattern[0].getType(); |
158 |
7 |
} |
159 |
|
|
160 |
34 |
void CandidateGeneratorQELitDeq::reset( Node eqc ){ |
161 |
34 |
eq::EqualityEngine* ee = d_qs.getEqualityEngine(); |
162 |
68 |
Node falset = NodeManager::currentNM()->mkConst(false); |
163 |
34 |
d_eqc_false = eq::EqClassIterator(falset, ee); |
164 |
34 |
} |
165 |
|
|
166 |
112 |
Node CandidateGeneratorQELitDeq::getNextCandidate(){ |
167 |
|
//get next candidate term in equivalence class |
168 |
169 |
while( !d_eqc_false.isFinished() ){ |
169 |
154 |
Node n = (*d_eqc_false); |
170 |
97 |
++d_eqc_false; |
171 |
97 |
if( n.getKind()==d_match_pattern.getKind() ){ |
172 |
252 |
if (n[0].getType().isComparableTo(d_match_pattern_type) |
173 |
252 |
&& isLegalCandidate(n)) |
174 |
|
{ |
175 |
|
//found an iff or equality, try to match it |
176 |
|
//DO_THIS: cache to avoid redundancies? |
177 |
|
//DO_THIS: do we need to try the symmetric equality for n? or will it also exist in the eq class of false? |
178 |
40 |
return n; |
179 |
|
} |
180 |
|
} |
181 |
|
} |
182 |
15 |
return Node::null(); |
183 |
|
} |
184 |
|
|
185 |
71 |
CandidateGeneratorQEAll::CandidateGeneratorQEAll(QuantifiersState& qs, |
186 |
|
TermRegistry& tr, |
187 |
71 |
Node mpat) |
188 |
71 |
: CandidateGenerator(qs, tr), d_match_pattern(mpat) |
189 |
|
{ |
190 |
71 |
d_match_pattern_type = mpat.getType(); |
191 |
71 |
Assert(mpat.getKind() == INST_CONSTANT); |
192 |
71 |
d_f = quantifiers::TermUtil::getInstConstAttr( mpat ); |
193 |
71 |
d_index = mpat.getAttribute(InstVarNumAttribute()); |
194 |
71 |
d_firstTime = false; |
195 |
71 |
} |
196 |
|
|
197 |
177 |
void CandidateGeneratorQEAll::reset( Node eqc ) { |
198 |
177 |
d_eq = eq::EqClassesIterator(d_qs.getEqualityEngine()); |
199 |
177 |
d_firstTime = true; |
200 |
177 |
} |
201 |
|
|
202 |
279 |
Node CandidateGeneratorQEAll::getNextCandidate() { |
203 |
279 |
quantifiers::TermDb* tdb = d_treg.getTermDatabase(); |
204 |
4659 |
while( !d_eq.isFinished() ){ |
205 |
4556 |
TNode n = (*d_eq); |
206 |
2366 |
++d_eq; |
207 |
2366 |
if( n.getType().isComparableTo( d_match_pattern_type ) ){ |
208 |
176 |
TNode nh = tdb->getEligibleTermInEqc(n); |
209 |
176 |
if( !nh.isNull() ){ |
210 |
176 |
if (options::instMaxLevel() != -1) |
211 |
|
{ |
212 |
|
nh = d_treg.getModel()->getInternalRepresentative(nh, d_f, d_index); |
213 |
|
//don't consider this if already the instantiation is ineligible |
214 |
|
if (!nh.isNull() && !tdb->isTermEligibleForInstantiation(nh, d_f)) |
215 |
|
{ |
216 |
|
nh = Node::null(); |
217 |
|
} |
218 |
|
} |
219 |
176 |
if( !nh.isNull() ){ |
220 |
176 |
d_firstTime = false; |
221 |
|
//an equivalence class with the same type as the pattern, return it |
222 |
176 |
return nh; |
223 |
|
} |
224 |
|
} |
225 |
|
} |
226 |
|
} |
227 |
103 |
if( d_firstTime ){ |
228 |
|
//must return something |
229 |
24 |
d_firstTime = false; |
230 |
24 |
return d_treg.getTermForType(d_match_pattern_type); |
231 |
|
} |
232 |
79 |
return Node::null(); |
233 |
|
} |
234 |
|
|
235 |
3017 |
CandidateGeneratorConsExpand::CandidateGeneratorConsExpand(QuantifiersState& qs, |
236 |
|
TermRegistry& tr, |
237 |
3017 |
Node mpat) |
238 |
3017 |
: CandidateGeneratorQE(qs, tr, mpat) |
239 |
|
{ |
240 |
3017 |
Assert(mpat.getKind() == APPLY_CONSTRUCTOR); |
241 |
3017 |
d_mpat_type = mpat.getType(); |
242 |
3017 |
} |
243 |
|
|
244 |
4113 |
void CandidateGeneratorConsExpand::reset(Node eqc) |
245 |
|
{ |
246 |
4113 |
d_term_iter = 0; |
247 |
4113 |
if (eqc.isNull()) |
248 |
|
{ |
249 |
2065 |
d_mode = cand_term_db; |
250 |
|
} |
251 |
|
else |
252 |
|
{ |
253 |
2048 |
d_eqc = eqc; |
254 |
2048 |
d_mode = cand_term_ident; |
255 |
2048 |
Assert(d_eqc.getType() == d_mpat_type); |
256 |
|
} |
257 |
4113 |
} |
258 |
|
|
259 |
5126 |
Node CandidateGeneratorConsExpand::getNextCandidate() |
260 |
|
{ |
261 |
|
// get the next term from the base class |
262 |
10252 |
Node curr = getNextCandidateInternal(); |
263 |
5126 |
if (curr.isNull() || (curr.hasOperator() && curr.getOperator() == d_op)) |
264 |
|
{ |
265 |
3804 |
return curr; |
266 |
|
} |
267 |
|
// expand it |
268 |
1322 |
NodeManager* nm = NodeManager::currentNM(); |
269 |
2644 |
std::vector<Node> children; |
270 |
1322 |
const DType& dt = d_mpat_type.getDType(); |
271 |
1322 |
Assert(dt.getNumConstructors() == 1); |
272 |
1322 |
children.push_back(d_op); |
273 |
3998 |
for (unsigned i = 0, nargs = dt[0].getNumArgs(); i < nargs; i++) |
274 |
|
{ |
275 |
|
Node sel = nm->mkNode( |
276 |
5352 |
APPLY_SELECTOR_TOTAL, dt[0].getSelectorInternal(d_mpat_type, i), curr); |
277 |
2676 |
children.push_back(sel); |
278 |
|
} |
279 |
1322 |
return nm->mkNode(APPLY_CONSTRUCTOR, children); |
280 |
|
} |
281 |
|
|
282 |
2048 |
bool CandidateGeneratorConsExpand::isLegalOpCandidate(Node n) |
283 |
|
{ |
284 |
2048 |
return isLegalCandidate(n); |
285 |
|
} |
286 |
|
|
287 |
262 |
CandidateGeneratorSelector::CandidateGeneratorSelector(QuantifiersState& qs, |
288 |
|
TermRegistry& tr, |
289 |
262 |
Node mpat) |
290 |
262 |
: CandidateGeneratorQE(qs, tr, mpat) |
291 |
|
{ |
292 |
262 |
Trace("sel-trigger") << "Selector trigger: " << mpat << std::endl; |
293 |
262 |
Assert(mpat.getKind() == APPLY_SELECTOR); |
294 |
|
// NOTE: could use qs.getValuation().getPreprocessedTerm(mpat); when |
295 |
|
// expand definitions is eliminated, however, this also requires avoiding |
296 |
|
// term formula removal. |
297 |
524 |
Node mpatExp = datatypes::DatatypesRewriter::expandApplySelector(mpat); |
298 |
262 |
Trace("sel-trigger") << "Expands to: " << mpatExp << std::endl; |
299 |
262 |
if (mpatExp.getKind() == ITE) |
300 |
|
{ |
301 |
262 |
Assert(mpatExp[1].getKind() == APPLY_SELECTOR_TOTAL); |
302 |
262 |
Assert(mpatExp[2].getKind() == APPLY_UF); |
303 |
262 |
d_selOp = d_treg.getTermDatabase()->getMatchOperator(mpatExp[1]); |
304 |
262 |
d_ufOp = d_treg.getTermDatabase()->getMatchOperator(mpatExp[2]); |
305 |
|
} |
306 |
|
else if (mpatExp.getKind() == APPLY_SELECTOR_TOTAL) |
307 |
|
{ |
308 |
|
// corner case of datatype with one constructor |
309 |
|
d_selOp = d_treg.getTermDatabase()->getMatchOperator(mpatExp); |
310 |
|
} |
311 |
|
else |
312 |
|
{ |
313 |
|
// corner case of a wrongly applied selector as a trigger |
314 |
|
Assert(mpatExp.getKind() == APPLY_UF); |
315 |
|
d_ufOp = d_treg.getTermDatabase()->getMatchOperator(mpatExp); |
316 |
|
} |
317 |
262 |
Assert(d_selOp != d_ufOp); |
318 |
262 |
} |
319 |
|
|
320 |
16818 |
void CandidateGeneratorSelector::reset(Node eqc) |
321 |
|
{ |
322 |
16818 |
Trace("sel-trigger-debug") << "Reset in eqc=" << eqc << std::endl; |
323 |
|
// start with d_selOp, if it exists |
324 |
16818 |
resetForOperator(eqc, !d_selOp.isNull()? d_selOp : d_ufOp); |
325 |
16818 |
} |
326 |
|
|
327 |
45992 |
Node CandidateGeneratorSelector::getNextCandidate() |
328 |
|
{ |
329 |
91984 |
Node nextc = getNextCandidateInternal(); |
330 |
45992 |
if (!nextc.isNull()) |
331 |
|
{ |
332 |
27709 |
Trace("sel-trigger-debug") << "...next candidate is " << nextc << std::endl; |
333 |
27709 |
return nextc; |
334 |
|
} |
335 |
18283 |
else if (d_op == d_selOp) |
336 |
|
{ |
337 |
9243 |
if (d_ufOp.isNull()) |
338 |
|
{ |
339 |
|
// corner case: selector cannot be wrongly applied (1-cons case) |
340 |
|
d_op = Node::null(); |
341 |
|
} |
342 |
|
else |
343 |
|
{ |
344 |
|
// finished correctly applied selectors, now try incorrectly applied ones |
345 |
9243 |
resetForOperator(d_eqc, d_ufOp); |
346 |
9243 |
return getNextCandidate(); |
347 |
|
} |
348 |
|
} |
349 |
9040 |
Trace("sel-trigger-debug") << "...finished" << std::endl; |
350 |
|
// no more candidates |
351 |
9040 |
return Node::null(); |
352 |
|
} |
353 |
|
|
354 |
|
} // namespace inst |
355 |
|
} // namespace quantifiers |
356 |
|
} // namespace theory |
357 |
29502 |
} // namespace cvc5 |