1 /********************* */
2 /*! \file theory_sets_private.cpp
4 ** Original author: Kshitij Bansal
5 ** Major contributors: none
6 ** Minor contributors (to current version): Andrew Reynolds
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2014 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 Sets theory implementation.
14 ** Sets theory implementation.
17 #include <boost/foreach.hpp>
19 #include "theory/theory_model.h"
20 #include "theory/sets/scrutinize.h"
21 #include "theory/sets/theory_sets.h"
22 #include "theory/sets/theory_sets_private.h"
24 #include "theory/sets/options.h"
25 #include "theory/sets/expr_patterns.h" // ONLY included here
27 #include "util/emptyset.h"
28 #include "util/result.h"
31 using namespace CVC4::expr::pattern
;
37 const char* element_of_str
= " \u2208 ";
39 /**************************** TheorySetsPrivate *****************************/
40 /**************************** TheorySetsPrivate *****************************/
41 /**************************** TheorySetsPrivate *****************************/
43 void TheorySetsPrivate::check(Theory::Effort level
) {
45 CodeTimer
checkCodeTimer(d_statistics
.d_checkTime
);
47 while(!d_external
.done() && !d_conflict
) {
48 // Get all the assertions
49 Assertion assertion
= d_external
.get();
50 TNode fact
= assertion
.assertion
;
52 Debug("sets") << "\n\n[sets] TheorySetsPrivate::check(): processing "
55 bool polarity
= fact
.getKind() != kind::NOT
;
56 TNode atom
= polarity
? fact
: fact
[0];
58 if (!assertion
.isPreregistered
) {
59 if (atom
.getKind() == kind::EQUAL
) {
60 if (!d_equalityEngine
.hasTerm(atom
[0])) {
61 Assert(atom
[0].isConst());
62 d_equalityEngine
.addTerm(atom
[0]);
63 d_termInfoManager
->addTerm(atom
[0]);
65 if (!d_equalityEngine
.hasTerm(atom
[1])) {
66 Assert(atom
[1].isConst());
67 d_equalityEngine
.addTerm(atom
[1]);
68 d_termInfoManager
->addTerm(atom
[1]);
74 switch(atom
.getKind()) {
76 Debug("sets") << atom
[0] << " should " << (polarity
? "":"NOT ")
77 << "be equal to " << atom
[1] << std::endl
;
78 assertEquality(fact
, fact
, /* learnt = */ false);
82 Debug("sets") << atom
[0] << " should " << (polarity
? "":"NOT ")
83 << "be in " << atom
[1] << std::endl
;
84 assertMemebership(fact
, fact
, /* learnt = */ false);
88 Unhandled(fact
.getKind());
92 Debug("sets") << "[sets] in conflict = " << d_conflict
<< std::endl
;
93 // Assert( d_conflict ^ d_equalityEngine.consistent() );
94 // ^ doesn't hold when we propagate equality/disequality between shared terms
95 // and that leads to conflict (externally).
96 if(d_conflict
) { return; }
97 Debug("sets") << "[sets] is complete = " << isComplete() << std::endl
;
100 if( (level
== Theory::EFFORT_FULL
|| options::setsEagerLemmas() ) && !isComplete()) {
101 d_external
.d_out
->lemma(getLemma());
105 // if we are here, there is no conflict and we are complete
106 if(Debug
.isOn("sets-scrutinize")) { d_scrutinize
->postCheckInvariants(); }
109 }/* TheorySetsPrivate::check() */
112 void TheorySetsPrivate::assertEquality(TNode fact
, TNode reason
, bool learnt
)
114 Debug("sets-assert") << "\n[sets-assert] adding equality: " << fact
116 << ", " << learnt
<< std::endl
;
118 bool polarity
= fact
.getKind() != kind::NOT
;
119 TNode atom
= polarity
? fact
: fact
[0];
121 // fact already holds
122 if( holds(atom
, polarity
) ) {
123 Debug("sets-assert") << "[sets-assert] already present, skipping" << std::endl
;
127 // assert fact & check for conflict
129 registerReason(reason
, /*save=*/ true);
131 d_equalityEngine
.assertEquality(atom
, polarity
, reason
);
133 if(!d_equalityEngine
.consistent()) {
134 Debug("sets-assert") << "[sets-assert] running into a conflict" << std::endl
;
139 if(!polarity
&& atom
[0].getType().isSet()) {
143 }/* TheorySetsPrivate::assertEquality() */
146 void TheorySetsPrivate::assertMemebership(TNode fact
, TNode reason
, bool learnt
)
148 Debug("sets-assert") << "\n[sets-assert] adding membership: " << fact
150 << ", " << learnt
<< std::endl
;
152 bool polarity
= fact
.getKind() == kind::NOT
? false : true;
153 TNode atom
= polarity
? fact
: fact
[0];
155 // fact already holds
156 if( holds(atom
, polarity
) ) {
157 Debug("sets-assert") << "[sets-assert] already present, skipping" << std::endl
;
161 // assert fact & check for conflict
163 registerReason(reason
, true);
165 d_equalityEngine
.assertPredicate(atom
, polarity
, reason
);
167 if(!d_equalityEngine
.consistent()) {
168 Debug("sets-assert") << "[sets-assert] running into a conflict" << std::endl
;
173 // update term info data structures
174 d_termInfoManager
->notifyMembership(fact
);
177 TNode x
= d_equalityEngine
.getRepresentative(atom
[0]);
178 eq::EqClassIterator
j(d_equalityEngine
.getRepresentative(atom
[1]),
181 Node cur_atom
= MEMBER(x
, S
);
184 * It is sufficient to do emptyset propagation outside the loop as
185 * constant term is guaranteed to be class representative.
187 if(polarity
&& S
.getKind() == kind::EMPTYSET
) {
188 Debug("sets-prop") << "[sets-prop] something in empty set? conflict."
190 learnLiteral(cur_atom
, false, cur_atom
);
196 * Disequality propagation if element type is set
198 if(x
.getType().isSet()) {
200 const CDTNodeList
* l
= d_termInfoManager
->getNonMembers(S
);
201 for(typeof(l
->begin()) it
= l
->begin(); it
!= l
->end(); ++it
) {
203 learnLiteral( /* atom = */ EQUAL(x
, n
),
204 /* polarity = */ false,
205 /* reason = */ AND( MEMBER(x
, S
), NOT( MEMBER(n
, S
)) ) );
208 const CDTNodeList
* l
= d_termInfoManager
->getMembers(S
);
209 for(typeof(l
->begin()) it
= l
->begin(); it
!= l
->end(); ++it
) {
211 learnLiteral( /* atom = */ EQUAL(x
, n
),
212 /* polarity = */ false,
213 /* reason = */ AND( NOT(MEMBER(x
, S
)), MEMBER(n
, S
)) );
218 for(; !j
.isFinished(); ++j
) {
220 Node cur_atom
= MEMBER(x
, S
);
222 // propagation : children
223 Debug("sets-prop") << "[sets-prop] Propagating 'down' for "
224 << x
<< element_of_str
<< S
<< std::endl
;
225 if(S
.getKind() == kind::UNION
||
226 S
.getKind() == kind::INTERSECTION
||
227 S
.getKind() == kind::SETMINUS
||
228 S
.getKind() == kind::SINGLETON
) {
229 doSettermPropagation(x
, S
);
230 if(d_conflict
) return;
231 }// propagation: children
234 // propagation : parents
235 Debug("sets-prop") << "[sets-prop] Propagating 'up' for "
236 << x
<< element_of_str
<< S
<< std::endl
;
237 const CDTNodeList
* parentList
= d_termInfoManager
->getParents(S
);
238 for(typeof(parentList
->begin()) k
= parentList
->begin();
239 k
!= parentList
->end(); ++k
) {
240 doSettermPropagation(x
, *k
);
241 if(d_conflict
) return;
242 }// propagation : parents
247 }/* TheorySetsPrivate::assertMemebership() */
250 void TheorySetsPrivate::doSettermPropagation(TNode x
, TNode S
)
252 Debug("sets-prop") << "[sets-prop] doSettermPropagation("
253 << x
<< ", " << S
<< std::endl
;
255 Assert(S
.getType().isSet() && S
.getType().getSetElementType() == x
.getType(),
256 ( std::string("types of S and x are ") + S
.getType().toString() +
257 std::string(" and ") + x
.getType().toString() +
258 std::string(" respectively") ).c_str() );
260 Node literal
, left_literal
, right_literal
;
262 // axiom: literal <=> left_literal AND right_literal
263 switch(S
.getKind()) {
264 case kind::INTERSECTION
:
265 literal
= MEMBER(x
, S
) ;
266 left_literal
= MEMBER(x
, S
[0]) ;
267 right_literal
= MEMBER(x
, S
[1]) ;
270 literal
= NOT( MEMBER(x
, S
) );
271 left_literal
= NOT( MEMBER(x
, S
[0]) );
272 right_literal
= NOT( MEMBER(x
, S
[1]) );
275 literal
= MEMBER(x
, S
) ;
276 left_literal
= MEMBER(x
, S
[0]) ;
277 right_literal
= NOT( MEMBER(x
, S
[1]) );
279 case kind::SINGLETON
: {
280 Node atom
= MEMBER(x
, S
);
281 if(holds(atom
, true)) {
282 learnLiteral(EQUAL(x
, S
[0]), true, atom
);
283 } else if(holds(atom
, false)) {
284 learnLiteral(EQUAL(x
, S
[0]), false, NOT(atom
));
292 Debug("sets-prop-details")
293 << "[sets-prop-details] " << literal
<< " IFF " << left_literal
294 << " AND " << right_literal
<< std::endl
;
296 Debug("sets-prop-details")
297 << "[sets-prop-details] "
298 << (holds(literal
) ? "yes" : (holds(literal
.negate()) ? " no" : " _ "))
300 << (holds(left_literal
) ? "yes" : (holds(left_literal
.negate()) ? "no " : " _ "))
302 << (holds(right_literal
) ? "yes" : (holds(right_literal
.negate()) ? "no " : " _ "))
305 Assert( present( MEMBER(x
, S
) ) ||
306 present( MEMBER(x
, S
[0]) ) ||
307 present( MEMBER(x
, S
[1]) ) );
309 if( holds(literal
) ) {
310 // 1a. literal => left_literal
311 Debug("sets-prop") << "[sets-prop] ... via case 1a. ..." << std::endl
;
312 learnLiteral(left_literal
, literal
);
313 if(d_conflict
) return;
315 // 1b. literal => right_literal
316 Debug("sets-prop") << "[sets-prop] ... via case 1b. ..." << std::endl
;
317 learnLiteral(right_literal
, literal
);
318 if(d_conflict
) return;
320 // subsumption requirement met, exit
323 else if( holds(literal
.negate() ) ) {
324 // 4. neg(literal), left_literal => neg(right_literal)
325 if( holds(left_literal
) ) {
326 Debug("sets-prop") << "[sets-prop] ... via case 4. ..." << std::endl
;
327 learnLiteral(right_literal
.negate(), AND( literal
.negate(),
329 if(d_conflict
) return;
332 // 5. neg(literal), right_literal => neg(left_literal)
333 if( holds(right_literal
) ) {
334 Debug("sets-prop") << "[sets-prop] ... via case 5. ..." << std::endl
;
335 learnLiteral(left_literal
.negate(), AND( literal
.negate(),
337 if(d_conflict
) return;
341 // 2,3. neg(left_literal) v neg(right_literal) => neg(literal)
342 if(holds(left_literal
.negate())) {
343 Debug("sets-prop") << "[sets-prop] ... via case 2. ..." << std::endl
;
344 learnLiteral(literal
.negate(), left_literal
.negate());
345 if(d_conflict
) return;
347 else if(holds(right_literal
.negate())) {
348 Debug("sets-prop") << "[sets-prop] ... via case 3. ..." << std::endl
;
349 learnLiteral(literal
.negate(), right_literal
.negate());
350 if(d_conflict
) return;
353 // 6. the axiom itself:
354 else if(holds(left_literal
) && holds(right_literal
)) {
355 Debug("sets-prop") << "[sets-prop] ... via case 6. ..." << std::endl
;
356 learnLiteral(literal
, AND(left_literal
, right_literal
) );
357 if(d_conflict
) return;
361 // check fulfilling rule
363 switch(S
.getKind()) {
365 if( holds(MEMBER(x
, S
)) &&
366 !present( MEMBER(x
, S
[0]) ) )
367 addToPending( MEMBER(x
, S
[0]) );
369 case kind::SETMINUS
: // intentional fallthrough
370 case kind::INTERSECTION
:
371 if( holds(MEMBER(x
, S
[0])) &&
372 !present( MEMBER(x
, S
[1]) ))
373 addToPending( MEMBER(x
, S
[1]) );
376 Assert(false, "MembershipEngine::doSettermPropagation");
381 void TheorySetsPrivate::learnLiteral(TNode atom
, bool polarity
, Node reason
) {
382 Debug("sets-learn") << "[sets-learn] learnLiteral(" << atom
383 << ", " << polarity
<< ")" << std::endl
;
385 if( holds(atom
, polarity
) ) {
386 Debug("sets-learn") << "[sets-learn] \u2514 already known, skipping" << std::endl
;
390 if( holds(atom
, !polarity
) ) {
391 Debug("sets-learn") << "[sets-learn] \u2514 conflict found" << std::endl
;
393 registerReason(reason
, /*save=*/ true);
395 if(atom
.getKind() == kind::EQUAL
) {
396 d_equalityEngine
.assertEquality(atom
, polarity
, reason
);
398 d_equalityEngine
.assertPredicate(atom
, polarity
, reason
);
400 Assert(d_conflict
); // should be marked due to equality engine
404 Assert(atom
.getKind() == kind::EQUAL
|| atom
.getKind() == kind::MEMBER
);
406 Node learnt_literal
= polarity
? Node(atom
) : NOT(atom
);
407 d_propagationQueue
.push_back( make_pair(learnt_literal
, reason
) );
408 }/*TheorySetsPrivate::learnLiteral(...)*/
411 /************************ Sharing ************************/
412 /************************ Sharing ************************/
413 /************************ Sharing ************************/
415 void TheorySetsPrivate::addSharedTerm(TNode n
) {
416 Debug("sets") << "[sets] ThoerySetsPrivate::addSharedTerm( " << n
<< ")" << std::endl
;
417 d_equalityEngine
.addTriggerTerm(n
, THEORY_SETS
);
420 void TheorySetsPrivate::dumpAssertionsHumanified() const
422 std::string tag
= "sets-assertions";
423 context::CDList
<Assertion
>::const_iterator it
= d_external
.facts_begin(), it_end
= d_external
.facts_end();
425 std::map
<TNode
, std::set
<TNode
> > equalities
;
426 std::set
< pair
<TNode
, TNode
> > disequalities
;
427 std::map
<TNode
, std::pair
<std::set
<TNode
>, std::set
<TNode
> > > members
;
428 static std::map
<TNode
, int> numbering
;
429 static int number
= 0;
431 for (unsigned i
= 0; it
!= it_end
; ++ it
, ++i
) {
432 TNode ass
= (*it
).assertion
;
433 // Trace("sets-care-dump") << AssertCommand(ass.toExpr()) << endl;
434 bool polarity
= ass
.getKind() != kind::NOT
;
435 ass
= polarity
? ass
: ass
[0];
436 Assert( ass
.getNumChildren() == 2);
437 TNode left
= d_equalityEngine
.getRepresentative(ass
[0]);
438 TNode right
= d_equalityEngine
.getRepresentative(ass
[1]);
439 if(numbering
[left
] == 0) numbering
[left
] = ++number
;
440 if(numbering
[right
] == 0) numbering
[right
] = ++number
;
441 equalities
[left
].insert(ass
[0]);
442 equalities
[right
].insert(ass
[1]);
443 if(ass
.getKind() == kind::EQUAL
) {
445 Assert(left
== right
);
447 if(left
> right
) std::swap(left
, right
);
448 disequalities
.insert(make_pair(left
, right
));
450 } else if(ass
.getKind() == kind::MEMBER
) {
451 (polarity
? members
[right
].first
: members
[right
].second
).insert(left
);
454 #define FORIT(it, container) for(typeof((container).begin()) it=(container).begin(); (it) != (container).end(); ++(it))
455 FORIT(kt
, equalities
) {
456 Trace(tag
) << " Eq class of t" << numbering
[(*kt
).first
] << ": " << std::endl
;
457 FORIT(jt
, (*kt
).second
) {
459 if( S
.getKind() != kind::UNION
&& S
.getKind() != kind::INTERSECTION
&& S
.getKind() != kind::SETMINUS
) {
460 Trace(tag
) << " " << *jt
<< ((*jt
).getType().isSet() ? "\n": " ");
463 if(S
[0].isConst() || numbering
.find(d_equalityEngine
.getRepresentative(S
[0])) == numbering
.end()) {
466 Trace(tag
) << "t" << numbering
[d_equalityEngine
.getRepresentative(S
[0])];
468 Trace(tag
) << " " << (S
.getKind() == kind::UNION
? "|" : (S
.getKind() == kind::INTERSECTION
? "&" : "-")) << " ";
469 if(S
[1].isConst() || numbering
.find(d_equalityEngine
.getRepresentative(S
[1])) == numbering
.end()) {
472 Trace(tag
) << "t" << numbering
[d_equalityEngine
.getRepresentative(S
[1])];
474 Trace(tag
) << std::endl
;
477 Trace(tag
) << std::endl
;
479 FORIT(kt
, disequalities
) Trace(tag
) << "NOT(t"<<numbering
[(*kt
).first
]<<" = t" <<numbering
[(*kt
).second
] <<")"<< std::endl
;
481 if( (*kt
).second
.first
.size() > 0) {
482 Trace(tag
) << "IN t" << numbering
[(*kt
).first
] << ": ";
483 FORIT(jt
, (*kt
).second
.first
) {
485 if(x
.isConst() || numbering
.find(d_equalityEngine
.getRepresentative(x
)) == numbering
.end()) {
488 Trace(tag
) << "x" << numbering
[d_equalityEngine
.getRepresentative(x
)] << ", ";
491 Trace(tag
) << std::endl
;
493 if( (*kt
).second
.second
.size() > 0) {
494 Trace(tag
) << "NOT IN t" << numbering
[(*kt
).first
] << ": ";
495 FORIT(jt
, (*kt
).second
.second
) {
497 if(x
.isConst() || numbering
.find(d_equalityEngine
.getRepresentative(x
)) == numbering
.end()) {
500 Trace(tag
) << "x" << numbering
[d_equalityEngine
.getRepresentative(x
)] << ", ";
503 Trace(tag
) << std::endl
;
506 Trace(tag
) << std::endl
;
510 void TheorySetsPrivate::computeCareGraph() {
511 Debug("sharing") << "Theory::computeCareGraph<" << d_external
.identify() << ">()" << endl
;
513 if(Trace
.isOn("sets-assertions")) {
514 // dump our understanding of assertions
515 dumpAssertionsHumanified();
519 if(options::setsCare1()) { i_st
= d_ccg_i
; }
520 for (unsigned i
= i_st
; i
< d_external
.d_sharedTerms
.size(); ++ i
) {
521 TNode a
= d_external
.d_sharedTerms
[i
];
522 TypeNode aType
= a
.getType();
524 unsigned j_st
= i
+ 1;
525 if(options::setsCare1()) { if(i
== d_ccg_i
) j_st
= d_ccg_j
+ 1; }
527 for (unsigned j
= j_st
; j
< d_external
.d_sharedTerms
.size(); ++ j
) {
528 TNode b
= d_external
.d_sharedTerms
[j
];
529 if (b
.getType() != aType
) {
530 // We don't care about the terms of different types
534 switch (d_external
.d_valuation
.getEqualityStatus(a
, b
)) {
535 case EQUALITY_TRUE_AND_PROPAGATED
:
536 // If we know about it, we should have propagated it, so we can skip
537 Trace("sets-care") << "[sets-care] Know: " << EQUAL(a
, b
) << std::endl
;
539 case EQUALITY_FALSE_AND_PROPAGATED
:
540 // If we know about it, we should have propagated it, so we can skip
541 Trace("sets-care") << "[sets-care] Know: " << NOT(EQUAL(a
, b
)) << std::endl
;
545 Assert(false, "ERROR: Equality status true/false but not propagated (sets care graph computation).");
547 case EQUALITY_TRUE_IN_MODEL
:
548 d_external
.addCarePair(a
, b
);
549 case EQUALITY_FALSE_IN_MODEL
:
550 if(Trace
.isOn("sets-care-performance-test")) {
551 // TODO: delete these lines, only for performance testing for now
552 d_external
.addCarePair(a
, b
);
555 case EQUALITY_UNKNOWN
:
557 d_external
.addCarePair(a
, b
);
558 if(options::setsCare1()) {
571 EqualityStatus
TheorySetsPrivate::getEqualityStatus(TNode a
, TNode b
) {
572 Assert(d_equalityEngine
.hasTerm(a
) && d_equalityEngine
.hasTerm(b
));
573 if (d_equalityEngine
.areEqual(a
, b
)) {
574 // The terms are implied to be equal
575 return EQUALITY_TRUE
;
577 if (d_equalityEngine
.areDisequal(a
, b
, false)) {
578 // The terms are implied to be dis-equal
579 return EQUALITY_FALSE
;
581 if( d_external
.d_valuation
.getModelValue(a
) == d_external
.d_valuation
.getModelValue(b
) ) {
582 // Ther term are true in current model
583 return EQUALITY_TRUE_IN_MODEL
;
585 return EQUALITY_FALSE_IN_MODEL
;
587 // //TODO: can we be more precise sometimes?
588 // return EQUALITY_UNKNOWN;
591 /******************** Model generation ********************/
592 /******************** Model generation ********************/
593 /******************** Model generation ********************/
595 const TheorySetsPrivate::Elements
& TheorySetsPrivate::getElements
596 (TNode setterm
, SettermElementsMap
& settermElementsMap
) const {
597 SettermElementsMap::const_iterator it
= settermElementsMap
.find(setterm
);
598 bool alreadyCalculated
= (it
!= settermElementsMap
.end());
599 if( !alreadyCalculated
) {
601 Kind k
= setterm
.getKind();
602 unsigned numChildren
= setterm
.getNumChildren();
604 if(numChildren
== 2) {
605 const Elements
& left
= getElements(setterm
[0], settermElementsMap
);
606 const Elements
& right
= getElements(setterm
[1], settermElementsMap
);
609 if(left
.size() >= right
.size()) {
610 cur
= left
; cur
.insert(right
.begin(), right
.end());
612 cur
= right
; cur
.insert(left
.begin(), left
.end());
615 case kind::INTERSECTION
:
616 std::set_intersection(left
.begin(), left
.end(), right
.begin(), right
.end(),
617 std::inserter(cur
, cur
.begin()) );
620 std::set_difference(left
.begin(), left
.end(), right
.begin(), right
.end(),
621 std::inserter(cur
, cur
.begin()) );
627 Assert(k
== kind::VARIABLE
|| k
== kind::APPLY_UF
|| k
== kind::SKOLEM
,
628 (std::string("Expect variable or UF got ") + kindToString(k
)).c_str() );
629 /* assign emptyset, which is default */
632 it
= settermElementsMap
.insert(SettermElementsMap::value_type(setterm
, cur
)).first
;
638 bool TheorySetsPrivate::checkModel(const SettermElementsMap
& settermElementsMap
, TNode S
) const {
640 Debug("sets-model") << "[sets-model] checkModel(..., " << S
<< "): "
643 Assert(S
.getType().isSet());
645 const Elements emptySetOfElements
;
646 const Elements
& saved
=
647 d_equalityEngine
.getRepresentative(S
).getKind() == kind::EMPTYSET
||
648 settermElementsMap
.find(d_equalityEngine
.getRepresentative(S
)) == settermElementsMap
.end() ?
650 settermElementsMap
.find(d_equalityEngine
.getRepresentative(S
))->second
;
651 Debug("sets-model") << "[sets-model] saved : { ";
652 BOOST_FOREACH(TNode element
, saved
) { Debug("sets-model") << element
<< ", "; }
653 Debug("sets-model") << " }" << std::endl
;
655 if(S
.getNumChildren() == 2) {
659 const Elements
& left
=
660 d_equalityEngine
.getRepresentative(S
[0]).getKind() == kind::EMPTYSET
||
661 settermElementsMap
.find(d_equalityEngine
.getRepresentative(S
[0])) == settermElementsMap
.end() ?
663 settermElementsMap
.find(d_equalityEngine
.getRepresentative(S
[0]))->second
;
665 const Elements
& right
=
666 d_equalityEngine
.getRepresentative(S
[1]).getKind() == kind::EMPTYSET
||
667 settermElementsMap
.find(d_equalityEngine
.getRepresentative(S
[1])) == settermElementsMap
.end() ?
669 settermElementsMap
.find(d_equalityEngine
.getRepresentative(S
[1]))->second
;
671 switch(S
.getKind()) {
673 if(left
.size() >= right
.size()) {
674 cur
= left
; cur
.insert(right
.begin(), right
.end());
676 cur
= right
; cur
.insert(left
.begin(), left
.end());
679 case kind::INTERSECTION
:
680 std::set_intersection(left
.begin(), left
.end(), right
.begin(), right
.end(),
681 std::inserter(cur
, cur
.begin()) );
684 std::set_difference(left
.begin(), left
.end(), right
.begin(), right
.end(),
685 std::inserter(cur
, cur
.begin()) );
691 Debug("sets-model") << "[sets-model] cur : { ";
692 BOOST_FOREACH(TNode element
, cur
) { Debug("sets-model") << element
<< ", "; }
693 Debug("sets-model") << " }" << std::endl
;
696 Debug("sets-model") << "[sets-model] *** ERROR *** cur != saved "
699 << "[sets-model] FYI: "
700 << " [" << S
<< "] = " << d_equalityEngine
.getRepresentative(S
) << ", "
701 << " [" << S
[0] << "] = " << d_equalityEngine
.getRepresentative(S
[0]) << ", "
702 << " [" << S
[1] << "] = " << d_equalityEngine
.getRepresentative(S
[1]) << "\n";
710 Node
TheorySetsPrivate::elementsToShape(Elements elements
, TypeNode setType
) const
712 NodeManager
* nm
= NodeManager::currentNM();
714 if(elements
.size() == 0) {
715 return nm
->mkConst(EmptySet(nm
->toType(setType
)));
717 Elements::iterator it
= elements
.begin();
718 Node cur
= SINGLETON(*it
);
719 while( ++it
!= elements
.end() ) {
720 cur
= nm
->mkNode(kind::UNION
, cur
, SINGLETON(*it
));
725 Node
TheorySetsPrivate::elementsToShape(set
<Node
> elements
, TypeNode setType
) const
727 NodeManager
* nm
= NodeManager::currentNM();
729 if(elements
.size() == 0) {
730 return nm
->mkConst(EmptySet(nm
->toType(setType
)));
732 typeof(elements
.begin()) it
= elements
.begin();
733 Node cur
= SINGLETON(*it
);
734 while( ++it
!= elements
.end() ) {
735 cur
= nm
->mkNode(kind::UNION
, cur
, SINGLETON(*it
));
741 void TheorySetsPrivate::collectModelInfo(TheoryModel
* m
, bool fullModel
)
743 Debug("sets-model") << "[sets-model] collectModelInfo(..., fullModel="
744 << (fullModel
? "true)" : "false)") << std::endl
;
748 // Compute terms appearing assertions and shared terms
749 d_external
.computeRelevantTerms(terms
);
751 // Compute for each setterm elements that it contains
752 SettermElementsMap settermElementsMap
;
753 TNode true_atom
= NodeManager::currentNM()->mkConst
<bool>(true);
754 TNode false_atom
= NodeManager::currentNM()->mkConst
<bool>(false);
755 for(eq::EqClassIterator
it_eqclasses(true_atom
, &d_equalityEngine
);
756 ! it_eqclasses
.isFinished() ; ++it_eqclasses
) {
757 TNode n
= (*it_eqclasses
);
758 if(n
.getKind() == kind::MEMBER
) {
759 Assert(d_equalityEngine
.areEqual(n
, true_atom
));
760 TNode x
= d_equalityEngine
.getRepresentative(n
[0]);
761 TNode S
= d_equalityEngine
.getRepresentative(n
[1]);
762 settermElementsMap
[S
].insert(x
);
764 if(Debug
.isOn("sets-model-details")) {
765 vector
<TNode
> explanation
;
766 d_equalityEngine
.explainPredicate(n
, true, explanation
);
767 Debug("sets-model-details")
768 << "[sets-model-details] > node: " << n
<< ", explanation:" << std::endl
;
769 BOOST_FOREACH(TNode m
, explanation
) {
770 Debug("sets-model-details") << "[sets-model-details] >> " << m
<< std::endl
;
775 if(Debug
.isOn("sets-model-details")) {
776 for(eq::EqClassIterator
it_eqclasses(false_atom
, &d_equalityEngine
);
777 ! it_eqclasses
.isFinished() ; ++it_eqclasses
) {
778 TNode n
= (*it_eqclasses
);
779 vector
<TNode
> explanation
;
780 d_equalityEngine
.explainPredicate(n
, false, explanation
);
781 Debug("sets-model-details")
782 << "[sets-model-details] > node: not: " << n
<< ", explanation:" << std::endl
;
783 BOOST_FOREACH(TNode m
, explanation
) {
784 Debug("sets-model-details") << "[sets-model-details] >> " << m
<< std::endl
;
789 // Assert equalities and disequalities to the model
790 m
->assertEqualityEngine(&d_equalityEngine
, &terms
);
792 // Loop over terms to collect set-terms for which we generate models
793 set
<Node
> settermsModEq
;
794 BOOST_FOREACH(TNode term
, terms
) {
795 TNode n
= term
.getKind() == kind::NOT
? term
[0] : term
;
797 Debug("sets-model-details") << "[sets-model-details] > " << n
<< std::endl
;
799 if(n
.getType().isSet()) {
800 n
= d_equalityEngine
.getRepresentative(n
);
802 settermsModEq
.insert(n
);
808 if(Debug
.isOn("sets-model")) {
809 BOOST_FOREACH( TNode node
, settermsModEq
) {
810 Debug("sets-model") << "[sets-model] " << node
<< std::endl
;
814 BOOST_FOREACH( SettermElementsMap::value_type
&it
, settermElementsMap
) {
815 BOOST_FOREACH( TNode element
, it
.second
/* elements */ ) {
816 Debug("sets-model-details") << "[sets-model-details] > " <<
817 (it
.first
/* setterm */) << ": " << element
<< std::endl
;
821 // assign representatives to equivalence class
822 BOOST_FOREACH( TNode setterm
, settermsModEq
) {
823 Elements elements
= getElements(setterm
, settermElementsMap
);
824 Node shape
= elementsToShape(elements
, setterm
.getType());
825 shape
= theory::Rewriter::rewrite(shape
);
826 m
->assertEquality(shape
, setterm
, true);
827 m
->assertRepresentative(shape
);
830 #ifdef CVC4_ASSERTIONS
831 bool checkPassed
= true;
832 BOOST_FOREACH(TNode term
, terms
) {
833 if( term
.getType().isSet() ) {
834 checkPassed
&= checkModel(settermElementsMap
, term
);
837 if(Debug
.isOn("sets-checkmodel-ignore")) {
838 Debug("sets-checkmodel-ignore") << "[sets-checkmodel-ignore] checkPassed value was " << checkPassed
<< std::endl
;
841 "THEORY_SETS check-model failed. Run with -d sets-model for details." );
846 Node
TheorySetsPrivate::getModelValue(TNode n
)
848 CodeTimer
codeTimer(d_statistics
.d_getModelValueTime
);
849 return d_termInfoManager
->getModelValue(n
);
852 /********************** Helper functions ***************************/
853 /********************** Helper functions ***************************/
854 /********************** Helper functions ***************************/
856 Node
mkAnd(const std::vector
<TNode
>& conjunctions
) {
857 Assert(conjunctions
.size() > 0);
861 for (unsigned i
= 0; i
< conjunctions
.size(); ++i
) {
862 TNode t
= conjunctions
[i
];
864 if (t
.getKind() == kind::AND
) {
865 for(TNode::iterator child_it
= t
.begin();
866 child_it
!= t
.end(); ++child_it
) {
867 Assert((*child_it
).getKind() != kind::AND
);
868 all
.insert(*child_it
);
877 Assert(all
.size() > 0);
879 if (all
.size() == 1) {
880 // All the same, or just one
881 return conjunctions
[0];
884 NodeBuilder
<> conjunction(kind::AND
);
885 std::set
<TNode
>::const_iterator it
= all
.begin();
886 std::set
<TNode
>::const_iterator it_end
= all
.end();
887 while (it
!= it_end
) {
896 TheorySetsPrivate::Statistics::Statistics() :
897 d_checkTime("theory::sets::time")
898 , d_getModelValueTime("theory::sets::getModelValueTime")
900 StatisticsRegistry::registerStat(&d_checkTime
);
901 StatisticsRegistry::registerStat(&d_getModelValueTime
);
905 TheorySetsPrivate::Statistics::~Statistics() {
906 StatisticsRegistry::unregisterStat(&d_checkTime
);
907 StatisticsRegistry::unregisterStat(&d_getModelValueTime
);
911 bool TheorySetsPrivate::present(TNode atom
) {
912 return holds(atom
) || holds(atom
.notNode());
916 bool TheorySetsPrivate::holds(TNode atom
, bool polarity
) {
917 Node polarity_atom
= NodeManager::currentNM()->mkConst
<bool>(polarity
);
919 Node atomModEq
= NodeManager::currentNM()->mkNode
920 (atom
.getKind(), d_equalityEngine
.getRepresentative(atom
[0]),
921 d_equalityEngine
.getRepresentative(atom
[1]) );
923 d_equalityEngine
.addTerm(atomModEq
);
925 return d_equalityEngine
.areEqual(atomModEq
, polarity_atom
);
929 void TheorySetsPrivate::registerReason(TNode reason
, bool save
)
931 if(save
) d_nodeSaver
.insert(reason
);
933 if(reason
.getKind() == kind::AND
) {
934 Assert(reason
.getNumChildren() == 2);
935 registerReason(reason
[0], false);
936 registerReason(reason
[1], false);
937 } else if(reason
.getKind() == kind::NOT
) {
938 registerReason(reason
[0], false);
939 } else if(reason
.getKind() == kind::MEMBER
) {
940 d_equalityEngine
.addTerm(reason
);
941 Assert(present(reason
));
942 } else if(reason
.getKind() == kind::EQUAL
) {
943 d_equalityEngine
.addTerm(reason
);
944 Assert(present(reason
));
945 } else if(reason
.getKind() == kind::CONST_BOOLEAN
) {
946 // That's OK, already in EqEngine
952 void TheorySetsPrivate::finishPropagation()
954 while(!d_conflict
&& !d_settermPropagationQueue
.empty()) {
955 std::pair
<TNode
,TNode
> np
= d_settermPropagationQueue
.front();
956 d_settermPropagationQueue
.pop();
957 doSettermPropagation(np
.first
, np
.second
);
959 while(!d_conflict
&& !d_propagationQueue
.empty()) {
960 std::pair
<Node
,Node
> np
= d_propagationQueue
.front();
961 d_propagationQueue
.pop();
962 TNode atom
= np
.first
.getKind() == kind::NOT
? np
.first
[0] : np
.first
;
963 if(atom
.getKind() == kind::MEMBER
) {
964 assertMemebership(np
.first
, np
.second
, /* learnt = */ true);
966 assertEquality(np
.first
, np
.second
, /* learnt = */ true);
971 void TheorySetsPrivate::addToPending(Node n
) {
972 Debug("sets-pending") << "[sets-pending] addToPending " << n
<< std::endl
;
973 if(d_pendingEverInserted
.find(n
) == d_pendingEverInserted
.end()) {
974 if(n
.getKind() == kind::MEMBER
) {
975 Debug("sets-pending") << "[sets-pending] \u2514 added to member queue"
979 Debug("sets-pending") << "[sets-pending] \u2514 added to equality queue"
981 Assert(n
.getKind() == kind::EQUAL
);
982 d_pendingDisequal
.push(n
);
984 d_external
.d_out
->lemma(getLemma());
985 Assert(isComplete());
989 bool TheorySetsPrivate::isComplete() {
990 // while(!d_pending.empty() &&
991 // (d_pendingEverInserted.find(d_pending.front()) != d_pendingEverInserted.end()
992 // || present(d_pending.front()) ) ) {
993 // Debug("sets-pending") << "[sets-pending] removing as already present: "
994 // << d_pending.front() << std::endl;
997 return d_pending
.empty() && d_pendingDisequal
.empty();
1000 Node
TheorySetsPrivate::getLemma() {
1001 Assert(!d_pending
.empty() || !d_pendingDisequal
.empty());
1005 if(!d_pending
.empty()) {
1006 n
= d_pending
.front();
1008 d_pendingEverInserted
.insert(n
);
1010 Assert(!present(n
));
1011 Assert(n
.getKind() == kind::MEMBER
);
1013 lemma
= OR(n
, NOT(n
));
1015 n
= d_pendingDisequal
.front();
1016 d_pendingDisequal
.pop();
1017 d_pendingEverInserted
.insert(n
);
1019 Assert(n
.getKind() == kind::EQUAL
&& n
[0].getType().isSet());
1020 Node x
= NodeManager::currentNM()->mkSkolem("sde_", n
[0].getType().getSetElementType() );
1021 Node l1
= MEMBER(x
, n
[0]), l2
= MEMBER(x
, n
[1]);
1023 lemma
= OR(n
, AND(l1
, NOT(l2
)), AND(NOT(l1
), l2
));
1026 Debug("sets-lemma") << "[sets-lemma] Generating for " << n
<< ", lemma: " << lemma
<< std::endl
;
1032 TheorySetsPrivate::TheorySetsPrivate(TheorySets
& external
,
1033 context::Context
* c
,
1034 context::UserContext
* u
):
1035 d_external(external
),
1037 d_equalityEngine(d_notify
, c
, "theory::sets::TheorySetsPrivate"),
1039 d_termInfoManager(NULL
),
1040 d_propagationQueue(c
),
1041 d_settermPropagationQueue(c
),
1044 d_pendingDisequal(c
),
1045 d_pendingEverInserted(u
),
1051 d_termInfoManager
= new TermInfoManager(*this, c
, &d_equalityEngine
);
1053 d_equalityEngine
.addFunctionKind(kind::UNION
);
1054 d_equalityEngine
.addFunctionKind(kind::INTERSECTION
);
1055 d_equalityEngine
.addFunctionKind(kind::SETMINUS
);
1057 d_equalityEngine
.addFunctionKind(kind::MEMBER
);
1058 d_equalityEngine
.addFunctionKind(kind::SUBSET
);
1060 if( Debug
.isOn("sets-scrutinize") ) {
1061 d_scrutinize
= new TheorySetsScrutinize(this);
1063 }/* TheorySetsPrivate::TheorySetsPrivate() */
1066 TheorySetsPrivate::~TheorySetsPrivate()
1068 delete d_termInfoManager
;
1069 if( Debug
.isOn("sets-scrutinize") ) {
1070 Assert(d_scrutinize
!= NULL
);
1071 delete d_scrutinize
;
1076 bool TheorySetsPrivate::propagate(TNode literal
) {
1077 Debug("sets-prop") << " propagate(" << literal
<< ")" << std::endl
;
1079 // If already in conflict, no more propagation
1081 Debug("sets-prop") << "TheoryUF::propagate(" << literal
<< "): already in conflict" << std::endl
;
1086 bool ok
= d_external
.d_out
->propagate(literal
);
1092 }/* TheorySetsPropagate::propagate(TNode) */
1095 void TheorySetsPrivate::setMasterEqualityEngine(eq::EqualityEngine
* eq
) {
1096 d_equalityEngine
.setMasterEqualityEngine(eq
);
1099 void TheorySetsPrivate::conflict(TNode a
, TNode b
)
1101 if (a
.getKind() == kind::CONST_BOOLEAN
) {
1102 d_conflictNode
= explain(a
.iffNode(b
));
1104 d_conflictNode
= explain(a
.eqNode(b
));
1106 d_external
.d_out
->conflict(d_conflictNode
);
1107 Debug("sets") << "[sets] conflict: " << a
<< " iff " << b
1108 << ", explaination " << d_conflictNode
<< std::endl
;
1112 Node
TheorySetsPrivate::explain(TNode literal
)
1114 Debug("sets") << "TheorySetsPrivate::explain(" << literal
<< ")"
1117 bool polarity
= literal
.getKind() != kind::NOT
;
1118 TNode atom
= polarity
? literal
: literal
[0];
1119 std::vector
<TNode
> assumptions
;
1121 if(atom
.getKind() == kind::EQUAL
|| atom
.getKind() == kind::IFF
) {
1122 d_equalityEngine
.explainEquality(atom
[0], atom
[1], polarity
, assumptions
);
1123 } else if(atom
.getKind() == kind::MEMBER
) {
1124 if( !d_equalityEngine
.hasTerm(atom
)) {
1125 d_equalityEngine
.addTerm(atom
);
1127 d_equalityEngine
.explainPredicate(atom
, polarity
, assumptions
);
1129 Debug("sets") << "unhandled: " << literal
<< "; (" << atom
<< ", "
1130 << polarity
<< "); kind" << atom
.getKind() << std::endl
;
1134 return mkAnd(assumptions
);
1137 void TheorySetsPrivate::preRegisterTerm(TNode node
)
1139 Debug("sets") << "TheorySetsPrivate::preRegisterTerm(" << node
<< ")"
1142 switch(node
.getKind()) {
1144 // TODO: what's the point of this
1145 d_equalityEngine
.addTriggerEquality(node
);
1148 // TODO: what's the point of this
1149 d_equalityEngine
.addTriggerPredicate(node
);
1152 d_termInfoManager
->addTerm(node
);
1153 d_equalityEngine
.addTriggerTerm(node
, THEORY_SETS
);
1154 // d_equalityEngine.addTerm(node);
1156 if(node
.getKind() == kind::SINGLETON
) {
1157 Node true_node
= NodeManager::currentNM()->mkConst
<bool>(true);
1158 learnLiteral(MEMBER(node
[0], node
), true, true_node
);
1159 //intentional fallthrough
1165 /**************************** eq::NotifyClass *****************************/
1166 /**************************** eq::NotifyClass *****************************/
1167 /**************************** eq::NotifyClass *****************************/
1170 bool TheorySetsPrivate::NotifyClass::eqNotifyTriggerEquality(TNode equality
, bool value
)
1172 Debug("sets-eq") << "[sets-eq] eqNotifyTriggerEquality: equality = " << equality
1173 << " value = " << value
<< std::endl
;
1175 return d_theory
.propagate(equality
);
1177 // We use only literal triggers so taking not is safe
1178 return d_theory
.propagate(equality
.notNode());
1182 bool TheorySetsPrivate::NotifyClass::eqNotifyTriggerPredicate(TNode predicate
, bool value
)
1184 Debug("sets-eq") << "[sets-eq] eqNotifyTriggerPredicate: predicate = " << predicate
1185 << " value = " << value
<< std::endl
;
1187 return d_theory
.propagate(predicate
);
1189 return d_theory
.propagate(predicate
.notNode());
1193 bool TheorySetsPrivate::NotifyClass::eqNotifyTriggerTermEquality(TheoryId tag
, TNode t1
, TNode t2
, bool value
)
1195 Debug("sets-eq") << "[sets-eq] eqNotifyTriggerTermEquality: tag = " << tag
1196 << " t1 = " << t1
<< " t2 = " << t2
<< " value = " << value
<< std::endl
;
1198 d_theory
.d_termInfoManager
->mergeTerms(t1
, t2
);
1200 d_theory
.propagate( value
? EQUAL(t1
, t2
) : NOT(EQUAL(t1
, t2
)) );
1204 void TheorySetsPrivate::NotifyClass::eqNotifyConstantTermMerge(TNode t1
, TNode t2
)
1206 Debug("sets-eq") << "[sets-eq] eqNotifyConstantTermMerge " << " t1 = " << t1
<< " t2 = " << t2
<< std::endl
;
1207 d_theory
.conflict(t1
, t2
);
1210 // void TheorySetsPrivate::NotifyClass::eqNotifyNewClass(TNode t)
1212 // Debug("sets-eq") << "[sets-eq] eqNotifyNewClass:" << " t = " << t << std::endl;
1215 // void TheorySetsPrivate::NotifyClass::eqNotifyPreMerge(TNode t1, TNode t2)
1217 // Debug("sets-eq") << "[sets-eq] eqNotifyPreMerge:" << " t1 = " << t1 << " t2 = " << t2 << std::endl;
1220 // void TheorySetsPrivate::NotifyClass::eqNotifyPostMerge(TNode t1, TNode t2)
1222 // Debug("sets-eq") << "[sets-eq] eqNotifyPostMerge:" << " t1 = " << t1 << " t2 = " << t2 << std::endl;
1225 // void TheorySetsPrivate::NotifyClass::eqNotifyDisequal(TNode t1, TNode t2, TNode reason)
1227 // Debug("sets-eq") << "[sets-eq] eqNotifyDisequal:" << " t1 = " << t1 << " t2 = " << t2 << " reason = " << reason << std::endl;
1231 /**************************** TermInfoManager *****************************/
1232 /**************************** TermInfoManager *****************************/
1233 /**************************** TermInfoManager *****************************/
1235 void TheorySetsPrivate::TermInfoManager::mergeLists
1236 (CDTNodeList
* la
, const CDTNodeList
* lb
) const {
1237 // straight from theory/arrays/array_info.cpp
1238 std::set
<TNode
> temp
;
1239 CDTNodeList::const_iterator it
;
1240 for(it
= la
->begin() ; it
!= la
->end(); it
++ ) {
1244 for(it
= lb
->begin() ; it
!= lb
->end(); it
++ ) {
1245 if(temp
.count(*it
) == 0) {
1251 TheorySetsPrivate::TermInfoManager::TermInfoManager
1252 (TheorySetsPrivate
& theory
, context::Context
* satContext
, eq::EqualityEngine
* eq
):
1254 d_context(satContext
),
1256 d_terms(satContext
),
1260 TheorySetsPrivate::TermInfoManager::~TermInfoManager() {
1261 for( typeof(d_info
.begin()) it
= d_info
.begin();
1262 it
!= d_info
.end(); ++it
) {
1263 delete (*it
).second
;
1267 void TheorySetsPrivate::TermInfoManager::notifyMembership(TNode fact
) {
1268 bool polarity
= fact
.getKind() != kind::NOT
;
1269 TNode atom
= polarity
? fact
: fact
[0];
1271 TNode x
= d_eqEngine
->getRepresentative(atom
[0]);
1272 TNode S
= d_eqEngine
->getRepresentative(atom
[1]);
1274 Debug("sets-terminfo") << "[sets-terminfo] Adding membership " << x
1275 << " in " << S
<< " " << polarity
<< std::endl
;
1277 d_info
[S
]->addToElementList(x
, polarity
);
1278 d_info
[x
]->addToSetList(S
, polarity
);
1280 d_theory
.d_modelCache
.clear();
1283 const CDTNodeList
* TheorySetsPrivate::TermInfoManager::getParents(TNode x
) {
1284 return d_info
[x
]->parents
;
1287 const CDTNodeList
* TheorySetsPrivate::TermInfoManager::getMembers(TNode S
) {
1288 return d_info
[S
]->elementsInThisSet
;
1291 const CDTNodeList
* TheorySetsPrivate::TermInfoManager::getNonMembers(TNode S
) {
1292 return d_info
[S
]->elementsNotInThisSet
;
1295 void TheorySetsPrivate::TermInfoManager::addTerm(TNode n
) {
1296 unsigned numChild
= n
.getNumChildren();
1298 if(!d_terms
.contains(n
)) {
1300 d_info
[n
] = new TheorySetsTermInfo(d_context
);
1303 if(n
.getKind() == kind::UNION
||
1304 n
.getKind() == kind::INTERSECTION
||
1305 n
.getKind() == kind::SETMINUS
) {
1307 for(unsigned i
= 0; i
< numChild
; ++i
) {
1308 if(d_terms
.contains(n
[i
])) {
1309 Debug("sets-parent") << "Adding " << n
<< " to parent list of "
1310 << n
[i
] << std::endl
;
1311 d_info
[n
[i
]]->parents
->push_back(n
);
1318 void TheorySetsPrivate::TermInfoManager::pushToSettermPropagationQueue
1319 (TNode x
, TNode S
, bool polarity
)
1321 Node cur_atom
= MEMBER(x
, S
);
1323 // propagation : empty set
1324 if(polarity
&& S
.getKind() == kind::EMPTYSET
) {
1325 Debug("sets-prop") << "[sets-prop] something in empty set? conflict."
1327 d_theory
.learnLiteral(cur_atom
, false, cur_atom
);
1329 }// propagation: empty set
1331 // propagation : children
1332 if(S
.getKind() == kind::UNION
||
1333 S
.getKind() == kind::INTERSECTION
||
1334 S
.getKind() == kind::SETMINUS
||
1335 S
.getKind() == kind::SINGLETON
) {
1336 d_theory
.d_settermPropagationQueue
.push_back(std::make_pair(x
, S
));
1337 }// propagation: children
1339 // propagation : parents
1340 const CDTNodeList
* parentList
= getParents(S
);
1341 for(typeof(parentList
->begin()) k
= parentList
->begin();
1342 k
!= parentList
->end(); ++k
) {
1343 d_theory
.d_settermPropagationQueue
.push_back(std::make_pair(x
, *k
));
1344 }// propagation : parents
1348 void TheorySetsPrivate::TermInfoManager::pushToSettermPropagationQueue
1349 (TNode x
, CDTNodeList
* l
, bool polarity
)
1351 set
<TNode
> alreadyProcessed
;
1353 BOOST_FOREACH(TNode S
, (*l
) ) {
1354 Debug("sets-prop") << "[sets-terminfo] setterm todo: "
1355 << MEMBER(x
, d_eqEngine
->getRepresentative(S
))
1358 TNode repS
= d_eqEngine
->getRepresentative(S
);
1359 if(alreadyProcessed
.find(repS
) != alreadyProcessed
.end()) {
1362 alreadyProcessed
.insert(repS
);
1365 d_eqEngine
->addTerm(MEMBER(d_eqEngine
->getRepresentative(x
), repS
));
1367 for(eq::EqClassIterator
j(d_eqEngine
->getRepresentative(S
), d_eqEngine
);
1368 !j
.isFinished(); ++j
) {
1370 pushToSettermPropagationQueue(x
, *j
, polarity
);
1376 void TheorySetsPrivate::TermInfoManager::pushToSettermPropagationQueue
1377 (CDTNodeList
* l
, TNode S
, bool polarity
)
1379 BOOST_FOREACH(TNode x
, (*l
) ) {
1380 Debug("sets-prop") << "[sets-terminfo] setterm todo: "
1381 << MEMBER(x
, d_eqEngine
->getRepresentative(S
))
1384 d_eqEngine
->addTerm(MEMBER(d_eqEngine
->getRepresentative(x
),
1385 d_eqEngine
->getRepresentative(S
)));
1387 for(eq::EqClassIterator
j(d_eqEngine
->getRepresentative(S
), d_eqEngine
);
1388 !j
.isFinished(); ++j
) {
1390 pushToSettermPropagationQueue(x
, *j
, polarity
);
1400 void TheorySetsPrivate::TermInfoManager::mergeTerms(TNode a
, TNode b
) {
1402 Debug("sets-terminfo") << "[sets-terminfo] Merging (into) a = " << a
1403 << ", b = " << b
<< std::endl
;
1404 Debug("sets-terminfo") << "[sets-terminfo] reps"
1405 << ", a: " << d_eqEngine
->getRepresentative(a
)
1406 << ", b: " << d_eqEngine
->getRepresentative(b
)
1409 typeof(d_info
.begin()) ita
= d_info
.find(a
);
1410 typeof(d_info
.begin()) itb
= d_info
.find(b
);
1412 Assert(ita
!= d_info
.end());
1413 Assert(itb
!= d_info
.end());
1415 /* elements in this sets */
1416 pushToSettermPropagationQueue( (*ita
).second
->elementsInThisSet
, b
, true );
1417 pushToSettermPropagationQueue( (*ita
).second
->elementsNotInThisSet
, b
, false );
1418 pushToSettermPropagationQueue( (*itb
).second
->elementsNotInThisSet
, a
, false );
1419 pushToSettermPropagationQueue( (*itb
).second
->elementsInThisSet
, a
, true );
1420 mergeLists((*ita
).second
->elementsInThisSet
,
1421 (*itb
).second
->elementsInThisSet
);
1422 mergeLists((*ita
).second
->elementsNotInThisSet
,
1423 (*itb
).second
->elementsNotInThisSet
);
1425 /* sets containing this element */
1426 // pushToSettermPropagationQueue( b, (*ita).second->setsContainingThisElement, true);
1427 // pushToSettermPropagationQueue( b, (*ita).second->setsNotContainingThisElement, false);
1428 pushToSettermPropagationQueue( a
, (*itb
).second
->setsNotContainingThisElement
, false);
1429 pushToSettermPropagationQueue( a
, (*itb
).second
->setsContainingThisElement
, true);
1430 mergeLists( (*ita
).second
->setsContainingThisElement
,
1431 (*itb
).second
->setsContainingThisElement
);
1432 mergeLists( (*ita
).second
->setsNotContainingThisElement
,
1433 (*itb
).second
->setsNotContainingThisElement
);
1435 d_theory
.d_modelCache
.clear();
1438 Node
TheorySetsPrivate::TermInfoManager::getModelValue(TNode n
)
1440 Assert(n
.getType().isSet());
1441 set
<Node
> elements
, elements_const
;
1442 Node S
= d_eqEngine
->getRepresentative(n
);
1443 typeof(d_theory
.d_modelCache
.begin()) it
= d_theory
.d_modelCache
.find(S
);
1444 if(it
!= d_theory
.d_modelCache
.end()) {
1445 return (*it
).second
;
1447 const CDTNodeList
* l
= getMembers(S
);
1448 for(typeof(l
->begin()) it
= l
->begin(); it
!= l
->end(); ++it
) {
1450 elements
.insert(d_eqEngine
->getRepresentative(n
));
1452 BOOST_FOREACH(TNode e
, elements
) {
1454 elements_const
.insert(e
);
1456 elements_const
.insert(d_theory
.d_external
.d_valuation
.getModelValue(e
));
1459 Node v
= d_theory
.elementsToShape(elements_const
, n
.getType());
1460 d_theory
.d_modelCache
[n
] = v
;
1464 }/* CVC4::theory::sets namespace */
1465 }/* CVC4::theory namespace */
1466 }/* CVC4 namespace */