1 /********************* */
2 /*! \file theory_sets_private.cpp
4 ** Original author: Kshitij Bansal
5 ** Major contributors: none
6 ** Minor contributors (to current version): none
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2013-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]);
64 if (!d_equalityEngine
.hasTerm(atom
[1])) {
65 Assert(atom
[1].isConst());
66 d_equalityEngine
.addTerm(atom
[1]);
72 switch(atom
.getKind()) {
74 Debug("sets") << atom
[0] << " should " << (polarity
? "":"NOT ")
75 << "be equal to " << atom
[1] << std::endl
;
76 assertEquality(fact
, fact
, /* learnt = */ false);
80 Debug("sets") << atom
[0] << " should " << (polarity
? "":"NOT ")
81 << "be in " << atom
[1] << std::endl
;
82 assertMemebership(fact
, fact
, /* learnt = */ false);
86 Unhandled(fact
.getKind());
90 Debug("sets") << "[sets] in conflict = " << d_conflict
<< std::endl
;
91 Assert( d_conflict
^ d_equalityEngine
.consistent() );
92 if(d_conflict
) { return; }
93 Debug("sets") << "[sets] is complete = " << isComplete() << std::endl
;
96 if( (Theory::EFFORT_FULL
|| options::setsEagerLemmas() ) && !isComplete()) {
97 d_external
.d_out
->lemma(getLemma());
101 // if we are here, there is no conflict and we are complete
102 if(Debug
.isOn("sets-scrutinize")) { d_scrutinize
->postCheckInvariants(); }
105 }/* TheorySetsPrivate::check() */
108 void TheorySetsPrivate::assertEquality(TNode fact
, TNode reason
, bool learnt
)
110 Debug("sets-assert") << "\n[sets-assert] adding equality: " << fact
112 << ", " << learnt
<< std::endl
;
114 bool polarity
= fact
.getKind() != kind::NOT
;
115 TNode atom
= polarity
? fact
: fact
[0];
117 // fact already holds
118 if( holds(atom
, polarity
) ) {
119 Debug("sets-assert") << "[sets-assert] already present, skipping" << std::endl
;
123 // assert fact & check for conflict
125 registerReason(reason
, /*save=*/ true);
127 d_equalityEngine
.assertEquality(atom
, polarity
, reason
);
129 if(!d_equalityEngine
.consistent()) {
130 Debug("sets-assert") << "[sets-assert] running into a conflict" << std::endl
;
135 if(!polarity
&& atom
[0].getType().isSet()) {
139 }/* TheorySetsPrivate::assertEquality() */
142 void TheorySetsPrivate::assertMemebership(TNode fact
, TNode reason
, bool learnt
)
144 Debug("sets-assert") << "\n[sets-assert] adding membership: " << fact
146 << ", " << learnt
<< std::endl
;
148 bool polarity
= fact
.getKind() == kind::NOT
? false : true;
149 TNode atom
= polarity
? fact
: fact
[0];
151 // fact already holds
152 if( holds(atom
, polarity
) ) {
153 Debug("sets-assert") << "[sets-assert] already present, skipping" << std::endl
;
157 // assert fact & check for conflict
159 registerReason(reason
, true);
161 d_equalityEngine
.assertPredicate(atom
, polarity
, reason
);
163 if(!d_equalityEngine
.consistent()) {
164 Debug("sets-assert") << "[sets-assert] running into a conflict" << std::endl
;
169 // update term info data structures
170 d_termInfoManager
->notifyMembership(fact
);
173 TNode x
= d_equalityEngine
.getRepresentative(atom
[0]);
174 eq::EqClassIterator
j(d_equalityEngine
.getRepresentative(atom
[1]),
177 Node cur_atom
= MEMBER(x
, S
);
180 * It is sufficient to do emptyset propagation outside the loop as
181 * constant term is guaranteed to be class representative.
183 if(polarity
&& S
.getKind() == kind::EMPTYSET
) {
184 Debug("sets-prop") << "[sets-prop] something in empty set? conflict."
186 learnLiteral(cur_atom
, false, cur_atom
);
191 for(; !j
.isFinished(); ++j
) {
193 Node cur_atom
= MEMBER(x
, S
);
195 // propagation : children
196 Debug("sets-prop") << "[sets-prop] Propagating 'down' for "
197 << x
<< element_of_str
<< S
<< std::endl
;
198 if(S
.getKind() == kind::UNION
||
199 S
.getKind() == kind::INTERSECTION
||
200 S
.getKind() == kind::SETMINUS
||
201 S
.getKind() == kind::SET_SINGLETON
) {
202 doSettermPropagation(x
, S
);
203 if(d_conflict
) return;
204 }// propagation: children
207 // propagation : parents
208 Debug("sets-prop") << "[sets-prop] Propagating 'up' for "
209 << x
<< element_of_str
<< S
<< std::endl
;
210 const CDTNodeList
* parentList
= d_termInfoManager
->getParents(S
);
211 for(typeof(parentList
->begin()) k
= parentList
->begin();
212 k
!= parentList
->end(); ++k
) {
213 doSettermPropagation(x
, *k
);
214 if(d_conflict
) return;
215 }// propagation : parents
220 }/* TheorySetsPrivate::assertMemebership() */
223 void TheorySetsPrivate::doSettermPropagation(TNode x
, TNode S
)
225 Debug("sets-prop") << "[sets-prop] doSettermPropagation("
226 << x
<< ", " << S
<< std::endl
;
228 Assert(S
.getType().isSet() && S
.getType().getSetElementType() == x
.getType());
230 Node literal
, left_literal
, right_literal
;
232 // axiom: literal <=> left_literal AND right_literal
233 switch(S
.getKind()) {
234 case kind::INTERSECTION
:
235 literal
= MEMBER(x
, S
) ;
236 left_literal
= MEMBER(x
, S
[0]) ;
237 right_literal
= MEMBER(x
, S
[1]) ;
240 literal
= NOT( MEMBER(x
, S
) );
241 left_literal
= NOT( MEMBER(x
, S
[0]) );
242 right_literal
= NOT( MEMBER(x
, S
[1]) );
245 literal
= MEMBER(x
, S
) ;
246 left_literal
= MEMBER(x
, S
[0]) ;
247 right_literal
= NOT( MEMBER(x
, S
[1]) );
249 case kind::SET_SINGLETON
: {
250 Node atom
= MEMBER(x
, S
);
251 if(holds(atom
, true)) {
252 learnLiteral(EQUAL(x
, S
[0]), true, atom
);
253 } else if(holds(atom
, false)) {
254 learnLiteral(EQUAL(x
, S
[0]), false, NOT(atom
));
262 Debug("sets-prop-details")
263 << "[sets-prop-details] " << literal
<< " IFF " << left_literal
264 << " AND " << right_literal
<< std::endl
;
266 Debug("sets-prop-details")
267 << "[sets-prop-details] "
268 << (holds(literal
) ? "yes" : (holds(literal
.negate()) ? " no" : " _ "))
270 << (holds(left_literal
) ? "yes" : (holds(left_literal
.negate()) ? "no " : " _ "))
272 << (holds(right_literal
) ? "yes" : (holds(right_literal
.negate()) ? "no " : " _ "))
275 Assert( present( MEMBER(x
, S
) ) ||
276 present( MEMBER(x
, S
[0]) ) ||
277 present( MEMBER(x
, S
[1]) ) );
279 if( holds(literal
) ) {
280 // 1a. literal => left_literal
281 Debug("sets-prop") << "[sets-prop] ... via case 1a. ..." << std::endl
;
282 learnLiteral(left_literal
, literal
);
283 if(d_conflict
) return;
285 // 1b. literal => right_literal
286 Debug("sets-prop") << "[sets-prop] ... via case 1b. ..." << std::endl
;
287 learnLiteral(right_literal
, literal
);
288 if(d_conflict
) return;
290 // subsumption requirement met, exit
293 else if( holds(literal
.negate() ) ) {
294 // 4. neg(literal), left_literal => neg(right_literal)
295 if( holds(left_literal
) ) {
296 Debug("sets-prop") << "[sets-prop] ... via case 4. ..." << std::endl
;
297 learnLiteral(right_literal
.negate(), AND( literal
.negate(),
299 if(d_conflict
) return;
302 // 5. neg(literal), right_literal => neg(left_literal)
303 if( holds(right_literal
) ) {
304 Debug("sets-prop") << "[sets-prop] ... via case 5. ..." << std::endl
;
305 learnLiteral(left_literal
.negate(), AND( literal
.negate(),
307 if(d_conflict
) return;
311 // 2,3. neg(left_literal) v neg(right_literal) => neg(literal)
312 if(holds(left_literal
.negate())) {
313 Debug("sets-prop") << "[sets-prop] ... via case 2. ..." << std::endl
;
314 learnLiteral(literal
.negate(), left_literal
.negate());
315 if(d_conflict
) return;
317 else if(holds(right_literal
.negate())) {
318 Debug("sets-prop") << "[sets-prop] ... via case 3. ..." << std::endl
;
319 learnLiteral(literal
.negate(), right_literal
.negate());
320 if(d_conflict
) return;
323 // 6. the axiom itself:
324 else if(holds(left_literal
) && holds(right_literal
)) {
325 Debug("sets-prop") << "[sets-prop] ... via case 6. ..." << std::endl
;
326 learnLiteral(literal
, AND(left_literal
, right_literal
) );
327 if(d_conflict
) return;
331 // check fulfilling rule
333 switch(S
.getKind()) {
335 if( holds(MEMBER(x
, S
)) &&
336 !present( MEMBER(x
, S
[0]) ) )
337 addToPending( MEMBER(x
, S
[0]) );
339 case kind::SETMINUS
: // intentional fallthrough
340 case kind::INTERSECTION
:
341 if( holds(MEMBER(x
, S
[0])) &&
342 !present( MEMBER(x
, S
[1]) ))
343 addToPending( MEMBER(x
, S
[1]) );
346 Assert(false, "MembershipEngine::doSettermPropagation");
351 void TheorySetsPrivate::learnLiteral(TNode atom
, bool polarity
, Node reason
) {
352 Debug("sets-learn") << "[sets-learn] learnLiteral(" << atom
353 << ", " << polarity
<< ")" << std::endl
;
355 if( holds(atom
, polarity
) ) {
356 Debug("sets-learn") << "[sets-learn] \u2514 already known, skipping" << std::endl
;
360 if( holds(atom
, !polarity
) ) {
361 Debug("sets-learn") << "[sets-learn] \u2514 conflict found" << std::endl
;
363 registerReason(reason
, /*save=*/ true);
365 if(atom
.getKind() == kind::EQUAL
) {
366 d_equalityEngine
.assertEquality(atom
, polarity
, reason
);
368 d_equalityEngine
.assertPredicate(atom
, polarity
, reason
);
370 Assert(d_conflict
); // should be marked due to equality engine
374 Assert(atom
.getKind() == kind::EQUAL
|| atom
.getKind() == kind::MEMBER
);
376 Node learnt_literal
= polarity
? Node(atom
) : NOT(atom
);
377 d_propagationQueue
.push_back( make_pair(learnt_literal
, reason
) );
378 }/*TheorySetsPrivate::learnLiteral(...)*/
381 /******************** Model generation ********************/
382 /******************** Model generation ********************/
383 /******************** Model generation ********************/
385 const TheorySetsPrivate::Elements
& TheorySetsPrivate::getElements
386 (TNode setterm
, SettermElementsMap
& settermElementsMap
) const {
387 SettermElementsMap::const_iterator it
= settermElementsMap
.find(setterm
);
388 bool alreadyCalculated
= (it
!= settermElementsMap
.end());
389 if( !alreadyCalculated
) {
391 Kind k
= setterm
.getKind();
392 unsigned numChildren
= setterm
.getNumChildren();
394 if(numChildren
== 2) {
395 const Elements
& left
= getElements(setterm
[0], settermElementsMap
);
396 const Elements
& right
= getElements(setterm
[1], settermElementsMap
);
399 if(left
.size() >= right
.size()) {
400 cur
= left
; cur
.insert(right
.begin(), right
.end());
402 cur
= right
; cur
.insert(left
.begin(), left
.end());
405 case kind::INTERSECTION
:
406 std::set_intersection(left
.begin(), left
.end(), right
.begin(), right
.end(),
407 std::inserter(cur
, cur
.begin()) );
410 std::set_difference(left
.begin(), left
.end(), right
.begin(), right
.end(),
411 std::inserter(cur
, cur
.begin()) );
417 Assert(k
== kind::VARIABLE
|| k
== kind::APPLY_UF
);
418 /* assign emptyset, which is default */
421 it
= settermElementsMap
.insert(SettermElementsMap::value_type(setterm
, cur
)).first
;
427 bool TheorySetsPrivate::checkModel(const SettermElementsMap
& settermElementsMap
, TNode S
) const {
429 Debug("sets-model") << "[sets-model] checkModel(..., " << S
<< "): "
432 Assert(S
.getType().isSet());
434 const Elements emptySetOfElements
;
435 const Elements
& saved
=
436 d_equalityEngine
.getRepresentative(S
).getKind() == kind::EMPTYSET
||
437 settermElementsMap
.find(d_equalityEngine
.getRepresentative(S
)) == settermElementsMap
.end() ?
439 settermElementsMap
.find(d_equalityEngine
.getRepresentative(S
))->second
;
440 Debug("sets-model") << "[sets-model] saved : { ";
441 BOOST_FOREACH(TNode element
, saved
) { Debug("sets-model") << element
<< ", "; }
442 Debug("sets-model") << " }" << std::endl
;
444 if(S
.getNumChildren() == 2) {
448 const Elements
& left
=
449 d_equalityEngine
.getRepresentative(S
[0]).getKind() == kind::EMPTYSET
||
450 settermElementsMap
.find(d_equalityEngine
.getRepresentative(S
[0])) == settermElementsMap
.end() ?
452 settermElementsMap
.find(d_equalityEngine
.getRepresentative(S
[0]))->second
;
454 const Elements
& right
=
455 d_equalityEngine
.getRepresentative(S
[1]).getKind() == kind::EMPTYSET
||
456 settermElementsMap
.find(d_equalityEngine
.getRepresentative(S
[1])) == settermElementsMap
.end() ?
458 settermElementsMap
.find(d_equalityEngine
.getRepresentative(S
[1]))->second
;
460 switch(S
.getKind()) {
462 if(left
.size() >= right
.size()) {
463 cur
= left
; cur
.insert(right
.begin(), right
.end());
465 cur
= right
; cur
.insert(left
.begin(), left
.end());
468 case kind::INTERSECTION
:
469 std::set_intersection(left
.begin(), left
.end(), right
.begin(), right
.end(),
470 std::inserter(cur
, cur
.begin()) );
473 std::set_difference(left
.begin(), left
.end(), right
.begin(), right
.end(),
474 std::inserter(cur
, cur
.begin()) );
480 Debug("sets-model") << "[sets-model] cur : { ";
481 BOOST_FOREACH(TNode element
, cur
) { Debug("sets-model") << element
<< ", "; }
482 Debug("sets-model") << " }" << std::endl
;
485 Debug("sets-model") << "[sets-model] *** ERROR *** cur != saved "
488 << "[sets-model] FYI: "
489 << " [" << S
<< "] = " << d_equalityEngine
.getRepresentative(S
) << ", "
490 << " [" << S
[0] << "] = " << d_equalityEngine
.getRepresentative(S
[0]) << ", "
491 << " [" << S
[1] << "] = " << d_equalityEngine
.getRepresentative(S
[1]) << "\n";
499 Node
TheorySetsPrivate::elementsToShape(Elements elements
, TypeNode setType
) const
501 NodeManager
* nm
= NodeManager::currentNM();
503 if(elements
.size() == 0) {
504 return nm
->mkConst(EmptySet(nm
->toType(setType
)));
506 Elements::iterator it
= elements
.begin();
507 Node cur
= SET_SINGLETON(*it
);
508 while( ++it
!= elements
.end() ) {
509 cur
= nm
->mkNode(kind::UNION
, cur
, SET_SINGLETON(*it
));
515 void TheorySetsPrivate::collectModelInfo(TheoryModel
* m
, bool fullModel
)
517 Debug("sets-model") << "[sets-model] collectModelInfo(..., fullModel="
518 << (fullModel
? "true)" : "false)") << std::endl
;
522 // Compute terms appearing assertions and shared terms
523 d_external
.computeRelevantTerms(terms
);
525 // Compute for each setterm elements that it contains
526 SettermElementsMap settermElementsMap
;
527 TNode true_atom
= NodeManager::currentNM()->mkConst
<bool>(true);
528 TNode false_atom
= NodeManager::currentNM()->mkConst
<bool>(false);
529 for(eq::EqClassIterator
it_eqclasses(true_atom
, &d_equalityEngine
);
530 ! it_eqclasses
.isFinished() ; ++it_eqclasses
) {
531 TNode n
= (*it_eqclasses
);
532 if(n
.getKind() == kind::MEMBER
) {
533 Assert(d_equalityEngine
.areEqual(n
, true_atom
));
534 TNode x
= d_equalityEngine
.getRepresentative(n
[0]);
535 TNode S
= d_equalityEngine
.getRepresentative(n
[1]);
536 settermElementsMap
[S
].insert(x
);
538 if(Debug
.isOn("sets-model-details")) {
539 vector
<TNode
> explanation
;
540 d_equalityEngine
.explainPredicate(n
, true, explanation
);
541 Debug("sets-model-details")
542 << "[sets-model-details] > node: " << n
<< ", explanation:" << std::endl
;
543 BOOST_FOREACH(TNode m
, explanation
) {
544 Debug("sets-model-details") << "[sets-model-details] >> " << m
<< std::endl
;
549 if(Debug
.isOn("sets-model-details")) {
550 for(eq::EqClassIterator
it_eqclasses(false_atom
, &d_equalityEngine
);
551 ! it_eqclasses
.isFinished() ; ++it_eqclasses
) {
552 TNode n
= (*it_eqclasses
);
553 vector
<TNode
> explanation
;
554 d_equalityEngine
.explainPredicate(n
, false, explanation
);
555 Debug("sets-model-details")
556 << "[sets-model-details] > node: not: " << n
<< ", explanation:" << std::endl
;
557 BOOST_FOREACH(TNode m
, explanation
) {
558 Debug("sets-model-details") << "[sets-model-details] >> " << m
<< std::endl
;
563 // Assert equalities and disequalities to the model
564 m
->assertEqualityEngine(&d_equalityEngine
, &terms
);
566 // Loop over terms to collect set-terms for which we generate models
567 set
<Node
> settermsModEq
;
568 BOOST_FOREACH(TNode term
, terms
) {
569 TNode n
= term
.getKind() == kind::NOT
? term
[0] : term
;
571 Debug("sets-model-details") << "[sets-model-details] > " << n
<< std::endl
;
573 if(n
.getType().isSet()) {
574 n
= d_equalityEngine
.getRepresentative(n
);
576 settermsModEq
.insert(n
);
582 if(Debug
.isOn("sets-model")) {
583 BOOST_FOREACH( TNode node
, settermsModEq
) {
584 Debug("sets-model") << "[sets-model] " << node
<< std::endl
;
588 BOOST_FOREACH( SettermElementsMap::value_type
&it
, settermElementsMap
) {
589 BOOST_FOREACH( TNode element
, it
.second
/* elements */ ) {
590 Debug("sets-model-details") << "[sets-model-details] > " <<
591 (it
.first
/* setterm */) << ": " << element
<< std::endl
;
595 // assign representatives to equivalence class
596 BOOST_FOREACH( TNode setterm
, settermsModEq
) {
597 Elements elements
= getElements(setterm
, settermElementsMap
);
598 Node shape
= elementsToShape(elements
, setterm
.getType());
599 m
->assertEquality(shape
, setterm
, true);
600 m
->assertRepresentative(shape
);
603 #ifdef CVC4_ASSERTIONS
604 bool checkPassed
= true;
605 BOOST_FOREACH(TNode term
, terms
) {
606 if( term
.getType().isSet() ) {
607 checkPassed
&= checkModel(settermElementsMap
, term
);
610 if(Debug
.isOn("sets-checkmodel-ignore")) {
611 Debug("sets-checkmodel-ignore") << "[sets-checkmodel-ignore] checkPassed value was " << checkPassed
<< std::endl
;
614 "THEORY_SETS check-model failed. Run with -d sets-model for details." );
620 /********************** Helper functions ***************************/
621 /********************** Helper functions ***************************/
622 /********************** Helper functions ***************************/
624 Node
mkAnd(const std::vector
<TNode
>& conjunctions
) {
625 Assert(conjunctions
.size() > 0);
629 for (unsigned i
= 0; i
< conjunctions
.size(); ++i
) {
630 TNode t
= conjunctions
[i
];
632 if (t
.getKind() == kind::AND
) {
633 for(TNode::iterator child_it
= t
.begin();
634 child_it
!= t
.end(); ++child_it
) {
635 Assert((*child_it
).getKind() != kind::AND
);
636 all
.insert(*child_it
);
645 Assert(all
.size() > 0);
647 if (all
.size() == 1) {
648 // All the same, or just one
649 return conjunctions
[0];
652 NodeBuilder
<> conjunction(kind::AND
);
653 std::set
<TNode
>::const_iterator it
= all
.begin();
654 std::set
<TNode
>::const_iterator it_end
= all
.end();
655 while (it
!= it_end
) {
664 TheorySetsPrivate::Statistics::Statistics() :
665 d_checkTime("theory::sets::time") {
667 StatisticsRegistry::registerStat(&d_checkTime
);
671 TheorySetsPrivate::Statistics::~Statistics() {
672 StatisticsRegistry::unregisterStat(&d_checkTime
);
676 bool TheorySetsPrivate::present(TNode atom
) {
677 return holds(atom
) || holds(atom
.notNode());
681 bool TheorySetsPrivate::holds(TNode atom
, bool polarity
) {
682 Node polarity_atom
= NodeManager::currentNM()->mkConst
<bool>(polarity
);
684 Node atomModEq
= NodeManager::currentNM()->mkNode
685 (atom
.getKind(), d_equalityEngine
.getRepresentative(atom
[0]),
686 d_equalityEngine
.getRepresentative(atom
[1]) );
688 d_equalityEngine
.addTerm(atomModEq
);
690 return d_equalityEngine
.areEqual(atomModEq
, polarity_atom
);
694 void TheorySetsPrivate::registerReason(TNode reason
, bool save
)
696 if(save
) d_nodeSaver
.insert(reason
);
698 if(reason
.getKind() == kind::AND
) {
699 Assert(reason
.getNumChildren() == 2);
700 registerReason(reason
[0], false);
701 registerReason(reason
[1], false);
702 } else if(reason
.getKind() == kind::NOT
) {
703 registerReason(reason
[0], false);
704 } else if(reason
.getKind() == kind::MEMBER
) {
705 d_equalityEngine
.addTerm(reason
);
706 Assert(present(reason
));
707 } else if(reason
.getKind() == kind::EQUAL
) {
708 d_equalityEngine
.addTerm(reason
);
709 Assert(present(reason
));
710 } else if(reason
.getKind() == kind::CONST_BOOLEAN
) {
711 // That's OK, already in EqEngine
717 void TheorySetsPrivate::finishPropagation()
719 while(!d_conflict
&& !d_settermPropagationQueue
.empty()) {
720 std::pair
<TNode
,TNode
> np
= d_settermPropagationQueue
.front();
721 d_settermPropagationQueue
.pop();
722 doSettermPropagation(np
.first
, np
.second
);
724 while(!d_conflict
&& !d_propagationQueue
.empty()) {
725 std::pair
<Node
,Node
> np
= d_propagationQueue
.front();
726 d_propagationQueue
.pop();
727 TNode atom
= np
.first
.getKind() == kind::NOT
? np
.first
[0] : np
.first
;
728 if(atom
.getKind() == kind::MEMBER
) {
729 assertMemebership(np
.first
, np
.second
, /* learnt = */ true);
731 assertEquality(np
.first
, np
.second
, /* learnt = */ true);
736 void TheorySetsPrivate::addToPending(Node n
) {
737 Debug("sets-pending") << "[sets-pending] addToPending " << n
<< std::endl
;
738 if(d_pendingEverInserted
.find(n
) == d_pendingEverInserted
.end()) {
739 if(n
.getKind() == kind::MEMBER
) {
740 Debug("sets-pending") << "[sets-pending] \u2514 added to member queue"
744 Debug("sets-pending") << "[sets-pending] \u2514 added to equality queue"
746 Assert(n
.getKind() == kind::EQUAL
);
747 d_pendingDisequal
.push(n
);
752 bool TheorySetsPrivate::isComplete() {
753 while(!d_pending
.empty() && present(d_pending
.front())) {
754 Debug("sets-pending") << "[sets-pending] removing as already present: "
755 << d_pending
.front() << std::endl
;
758 return d_pending
.empty() && d_pendingDisequal
.empty();
761 Node
TheorySetsPrivate::getLemma() {
762 Assert(!d_pending
.empty() || !d_pendingDisequal
.empty());
766 if(!d_pending
.empty()) {
767 n
= d_pending
.front();
769 d_pendingEverInserted
.insert(n
);
772 Assert(n
.getKind() == kind::MEMBER
);
774 lemma
= OR(n
, NOT(n
));
776 n
= d_pendingDisequal
.front();
777 d_pendingDisequal
.pop();
778 d_pendingEverInserted
.insert(n
);
780 Assert(n
.getKind() == kind::EQUAL
&& n
[0].getType().isSet());
781 Node x
= NodeManager::currentNM()->mkSkolem("sde_$$", n
[0].getType().getSetElementType());
782 Node l1
= MEMBER(x
, n
[0]), l2
= MEMBER(x
, n
[1]);
784 lemma
= OR(n
, AND(l1
, NOT(l2
)), AND(NOT(l1
), l2
));
787 Debug("sets-lemma") << "[sets-lemma] Generating for " << n
<< ", lemma: " << lemma
<< std::endl
;
793 TheorySetsPrivate::TheorySetsPrivate(TheorySets
& external
,
795 context::UserContext
* u
):
796 d_external(external
),
798 d_equalityEngine(d_notify
, c
, "theory::sets::TheorySetsPrivate"),
800 d_termInfoManager(NULL
),
801 d_propagationQueue(c
),
802 d_settermPropagationQueue(c
),
805 d_pendingDisequal(u
),
806 d_pendingEverInserted(u
),
809 d_termInfoManager
= new TermInfoManager(*this, c
, &d_equalityEngine
);
811 d_equalityEngine
.addFunctionKind(kind::UNION
);
812 d_equalityEngine
.addFunctionKind(kind::INTERSECTION
);
813 d_equalityEngine
.addFunctionKind(kind::SETMINUS
);
815 d_equalityEngine
.addFunctionKind(kind::MEMBER
);
816 d_equalityEngine
.addFunctionKind(kind::SUBSET
);
818 if( Debug
.isOn("sets-scrutinize") ) {
819 d_scrutinize
= new TheorySetsScrutinize(this);
821 }/* TheorySetsPrivate::TheorySetsPrivate() */
824 TheorySetsPrivate::~TheorySetsPrivate()
826 delete d_termInfoManager
;
827 if( Debug
.isOn("sets-scrutinize") ) {
828 Assert(d_scrutinize
!= NULL
);
834 bool TheorySetsPrivate::propagate(TNode literal
) {
835 Debug("sets-prop") << " propagate(" << literal
<< ")" << std::endl
;
837 // If already in conflict, no more propagation
839 Debug("sets-prop") << "TheoryUF::propagate(" << literal
<< "): already in conflict" << std::endl
;
844 bool ok
= d_external
.d_out
->propagate(literal
);
850 }/* TheorySetsPropagate::propagate(TNode) */
853 void TheorySetsPrivate::addSharedTerm(TNode n
) {
854 Debug("sets") << "[sets] ThoerySetsPrivate::addSharedTerm( " << n
<< ")" << std::endl
;
855 d_equalityEngine
.addTriggerTerm(n
, THEORY_SETS
);
858 void TheorySetsPrivate::conflict(TNode a
, TNode b
)
860 if (a
.getKind() == kind::CONST_BOOLEAN
) {
861 d_conflictNode
= explain(a
.iffNode(b
));
863 d_conflictNode
= explain(a
.eqNode(b
));
865 d_external
.d_out
->conflict(d_conflictNode
);
866 Debug("sets") << "[sets] conflict: " << a
<< " iff " << b
867 << ", explaination " << d_conflictNode
<< std::endl
;
871 Node
TheorySetsPrivate::explain(TNode literal
)
873 Debug("sets") << "TheorySetsPrivate::explain(" << literal
<< ")"
876 bool polarity
= literal
.getKind() != kind::NOT
;
877 TNode atom
= polarity
? literal
: literal
[0];
878 std::vector
<TNode
> assumptions
;
880 if(atom
.getKind() == kind::EQUAL
|| atom
.getKind() == kind::IFF
) {
881 d_equalityEngine
.explainEquality(atom
[0], atom
[1], polarity
, assumptions
);
882 } else if(atom
.getKind() == kind::MEMBER
) {
883 if( !d_equalityEngine
.hasTerm(atom
)) {
884 d_equalityEngine
.addTerm(atom
);
886 d_equalityEngine
.explainPredicate(atom
, polarity
, assumptions
);
888 Debug("sets") << "unhandled: " << literal
<< "; (" << atom
<< ", "
889 << polarity
<< "); kind" << atom
.getKind() << std::endl
;
893 return mkAnd(assumptions
);
896 void TheorySetsPrivate::preRegisterTerm(TNode node
)
898 Debug("sets") << "TheorySetsPrivate::preRegisterTerm(" << node
<< ")"
901 switch(node
.getKind()) {
903 // TODO: what's the point of this
904 d_equalityEngine
.addTriggerEquality(node
);
907 // TODO: what's the point of this
908 d_equalityEngine
.addTriggerPredicate(node
);
911 d_termInfoManager
->addTerm(node
);
912 d_equalityEngine
.addTriggerTerm(node
, THEORY_SETS
);
913 // d_equalityEngine.addTerm(node);
915 if(node
.getKind() == kind::SET_SINGLETON
) {
916 Node true_node
= NodeManager::currentNM()->mkConst
<bool>(true);
917 learnLiteral(MEMBER(node
[0], node
), true, true_node
);
918 //intentional fallthrough
924 /**************************** eq::NotifyClass *****************************/
925 /**************************** eq::NotifyClass *****************************/
926 /**************************** eq::NotifyClass *****************************/
929 bool TheorySetsPrivate::NotifyClass::eqNotifyTriggerEquality(TNode equality
, bool value
)
931 Debug("sets-eq") << "[sets-eq] eqNotifyTriggerEquality: equality = " << equality
932 << " value = " << value
<< std::endl
;
934 return d_theory
.propagate(equality
);
936 // We use only literal triggers so taking not is safe
937 return d_theory
.propagate(equality
.notNode());
941 bool TheorySetsPrivate::NotifyClass::eqNotifyTriggerPredicate(TNode predicate
, bool value
)
943 Debug("sets-eq") << "[sets-eq] eqNotifyTriggerPredicate: predicate = " << predicate
944 << " value = " << value
<< std::endl
;
946 return d_theory
.propagate(predicate
);
948 return d_theory
.propagate(predicate
.notNode());
952 bool TheorySetsPrivate::NotifyClass::eqNotifyTriggerTermEquality(TheoryId tag
, TNode t1
, TNode t2
, bool value
)
954 Debug("sets-eq") << "[sets-eq] eqNotifyTriggerTermEquality: tag = " << tag
955 << " t1 = " << t1
<< " t2 = " << t2
<< " value = " << value
<< std::endl
;
957 d_theory
.d_termInfoManager
->mergeTerms(t1
, t2
);
962 void TheorySetsPrivate::NotifyClass::eqNotifyConstantTermMerge(TNode t1
, TNode t2
)
964 Debug("sets-eq") << "[sets-eq] eqNotifyConstantTermMerge " << " t1 = " << t1
<< " t2 = " << t2
<< std::endl
;
965 d_theory
.conflict(t1
, t2
);
968 // void TheorySetsPrivate::NotifyClass::eqNotifyNewClass(TNode t)
970 // Debug("sets-eq") << "[sets-eq] eqNotifyNewClass:" << " t = " << t << std::endl;
973 // void TheorySetsPrivate::NotifyClass::eqNotifyPreMerge(TNode t1, TNode t2)
975 // Debug("sets-eq") << "[sets-eq] eqNotifyPreMerge:" << " t1 = " << t1 << " t2 = " << t2 << std::endl;
978 // void TheorySetsPrivate::NotifyClass::eqNotifyPostMerge(TNode t1, TNode t2)
980 // Debug("sets-eq") << "[sets-eq] eqNotifyPostMerge:" << " t1 = " << t1 << " t2 = " << t2 << std::endl;
983 // void TheorySetsPrivate::NotifyClass::eqNotifyDisequal(TNode t1, TNode t2, TNode reason)
985 // Debug("sets-eq") << "[sets-eq] eqNotifyDisequal:" << " t1 = " << t1 << " t2 = " << t2 << " reason = " << reason << std::endl;
989 /**************************** TermInfoManager *****************************/
990 /**************************** TermInfoManager *****************************/
991 /**************************** TermInfoManager *****************************/
993 void TheorySetsPrivate::TermInfoManager::mergeLists
994 (CDTNodeList
* la
, const CDTNodeList
* lb
) const {
995 // straight from theory/arrays/array_info.cpp
996 std::set
<TNode
> temp
;
997 CDTNodeList::const_iterator it
;
998 for(it
= la
->begin() ; it
!= la
->end(); it
++ ) {
1002 for(it
= lb
->begin() ; it
!= lb
->end(); it
++ ) {
1003 if(temp
.count(*it
) == 0) {
1009 TheorySetsPrivate::TermInfoManager::TermInfoManager
1010 (TheorySetsPrivate
& theory
, context::Context
* satContext
, eq::EqualityEngine
* eq
):
1012 d_context(satContext
),
1014 d_terms(satContext
),
1018 TheorySetsPrivate::TermInfoManager::~TermInfoManager() {
1019 for( typeof(d_info
.begin()) it
= d_info
.begin();
1020 it
!= d_info
.end(); ++it
) {
1021 delete (*it
).second
;
1025 void TheorySetsPrivate::TermInfoManager::notifyMembership(TNode fact
) {
1026 bool polarity
= fact
.getKind() != kind::NOT
;
1027 TNode atom
= polarity
? fact
: fact
[0];
1029 TNode x
= d_eqEngine
->getRepresentative(atom
[0]);
1030 TNode S
= d_eqEngine
->getRepresentative(atom
[1]);
1032 Debug("sets-terminfo") << "[sets-terminfo] Adding membership " << x
1033 << " in " << S
<< " " << polarity
<< std::endl
;
1035 d_info
[S
]->addToElementList(x
, polarity
);
1038 const CDTNodeList
* TheorySetsPrivate::TermInfoManager::getParents(TNode x
) {
1039 return d_info
[x
]->parents
;
1042 void TheorySetsPrivate::TermInfoManager::addTerm(TNode n
) {
1043 unsigned numChild
= n
.getNumChildren();
1045 if(!d_terms
.contains(n
)) {
1047 d_info
[n
] = new TheorySetsTermInfo(d_context
);
1050 if(n
.getKind() == kind::UNION
||
1051 n
.getKind() == kind::INTERSECTION
||
1052 n
.getKind() == kind::SETMINUS
||
1053 n
.getKind() == kind::SET_SINGLETON
) {
1055 for(unsigned i
= 0; i
< numChild
; ++i
) {
1056 if(d_terms
.contains(n
[i
])) {
1057 Debug("sets-parent") << "Adding " << n
<< " to parent list of "
1058 << n
[i
] << std::endl
;
1059 d_info
[n
[i
]]->parents
->push_back(n
);
1066 void TheorySetsPrivate::TermInfoManager::pushToSettermPropagationQueue
1067 (CDTNodeList
* l
, TNode S
, bool polarity
)
1069 BOOST_FOREACH(TNode x
, (*l
) ) {
1070 Debug("sets-prop") << "[sets-terminfo] setterm todo: "
1071 << MEMBER(x
, d_eqEngine
->getRepresentative(S
))
1074 d_eqEngine
->addTerm(MEMBER(d_eqEngine
->getRepresentative(x
),
1075 d_eqEngine
->getRepresentative(S
)));
1077 for(eq::EqClassIterator
j(d_eqEngine
->getRepresentative(S
), d_eqEngine
);
1078 !j
.isFinished(); ++j
) {
1081 Node cur_atom
= MEMBER(x
, S
);
1083 // propagation : empty set
1084 if(polarity
&& S
.getKind() == kind::EMPTYSET
) {
1085 Debug("sets-prop") << "[sets-prop] something in empty set? conflict."
1087 d_theory
.learnLiteral(cur_atom
, false, cur_atom
);
1089 }// propagation: empty set
1091 // propagation : children
1092 if(S
.getKind() == kind::UNION
||
1093 S
.getKind() == kind::INTERSECTION
||
1094 S
.getKind() == kind::SETMINUS
||
1095 S
.getKind() == kind::SET_SINGLETON
) {
1096 d_theory
.d_settermPropagationQueue
.push_back(std::make_pair(x
, S
));
1097 }// propagation: children
1099 // propagation : parents
1100 const CDTNodeList
* parentList
= getParents(S
);
1101 for(typeof(parentList
->begin()) k
= parentList
->begin();
1102 k
!= parentList
->end(); ++k
) {
1103 d_theory
.d_settermPropagationQueue
.push_back(std::make_pair(x
, *k
));
1104 }// propagation : parents
1113 void TheorySetsPrivate::TermInfoManager::mergeTerms(TNode a
, TNode b
) {
1116 if(!a
.getType().isSet()) return;
1118 Debug("sets-terminfo") << "[sets-terminfo] Merging (into) a = " << a
1119 << ", b = " << b
<< std::endl
;
1120 Debug("sets-terminfo") << "[sets-terminfo] reps"
1121 << ", a: " << d_eqEngine
->getRepresentative(a
)
1122 << ", b: " << d_eqEngine
->getRepresentative(b
)
1125 typeof(d_info
.begin()) ita
= d_info
.find(a
);
1126 typeof(d_info
.begin()) itb
= d_info
.find(b
);
1128 Assert(ita
!= d_info
.end());
1129 Assert(itb
!= d_info
.end());
1131 pushToSettermPropagationQueue( (*ita
).second
->elementsInThisSet
, b
, true );
1132 pushToSettermPropagationQueue( (*ita
).second
->elementsNotInThisSet
, b
, false );
1133 pushToSettermPropagationQueue( (*itb
).second
->elementsNotInThisSet
, a
, false );
1134 pushToSettermPropagationQueue( (*itb
).second
->elementsInThisSet
, a
, true );
1136 mergeLists((*ita
).second
->elementsInThisSet
,
1137 (*itb
).second
->elementsInThisSet
);
1139 mergeLists((*ita
).second
->elementsNotInThisSet
,
1140 (*itb
).second
->elementsNotInThisSet
);
1144 }/* CVC4::theory::sets namespace */
1145 }/* CVC4::theory namespace */
1146 }/* CVC4 namespace */