1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Mudathir Mohamed, 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 bags state object. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "theory/bags/solver_state.h" |
17 |
|
|
18 |
|
#include "expr/attribute.h" |
19 |
|
#include "expr/bound_var_manager.h" |
20 |
|
#include "expr/skolem_manager.h" |
21 |
|
#include "theory/uf/equality_engine.h" |
22 |
|
|
23 |
|
using namespace std; |
24 |
|
using namespace cvc5::kind; |
25 |
|
|
26 |
|
namespace cvc5 { |
27 |
|
namespace theory { |
28 |
|
namespace bags { |
29 |
|
|
30 |
9927 |
SolverState::SolverState(Env& env, Valuation val) : TheoryState(env, val) |
31 |
|
{ |
32 |
9927 |
d_true = NodeManager::currentNM()->mkConst(true); |
33 |
9927 |
d_false = NodeManager::currentNM()->mkConst(false); |
34 |
9927 |
d_nm = NodeManager::currentNM(); |
35 |
9927 |
} |
36 |
|
|
37 |
1072 |
void SolverState::registerBag(TNode n) |
38 |
|
{ |
39 |
1072 |
Assert(n.getType().isBag()); |
40 |
1072 |
d_bags.insert(n); |
41 |
1072 |
} |
42 |
|
|
43 |
3139 |
void SolverState::registerCountTerm(TNode n) |
44 |
|
{ |
45 |
3139 |
Assert(n.getKind() == BAG_COUNT); |
46 |
6278 |
Node element = getRepresentative(n[0]); |
47 |
6278 |
Node bag = getRepresentative(n[1]); |
48 |
3139 |
d_bagElements[bag].insert(element); |
49 |
3139 |
} |
50 |
|
|
51 |
31490 |
const std::set<Node>& SolverState::getBags() { return d_bags; } |
52 |
|
|
53 |
3019 |
const std::set<Node>& SolverState::getElements(Node B) |
54 |
|
{ |
55 |
6038 |
Node bag = getRepresentative(B); |
56 |
6038 |
return d_bagElements[bag]; |
57 |
|
} |
58 |
|
|
59 |
15745 |
const std::set<Node>& SolverState::getDisequalBagTerms() { return d_deq; } |
60 |
|
|
61 |
15745 |
void SolverState::reset() |
62 |
|
{ |
63 |
15745 |
d_bagElements.clear(); |
64 |
15745 |
d_bags.clear(); |
65 |
15745 |
d_deq.clear(); |
66 |
15745 |
} |
67 |
|
|
68 |
15745 |
void SolverState::initialize() |
69 |
|
{ |
70 |
15745 |
reset(); |
71 |
15745 |
collectBagsAndCountTerms(); |
72 |
15745 |
collectDisequalBagTerms(); |
73 |
15745 |
} |
74 |
|
|
75 |
15745 |
void SolverState::collectBagsAndCountTerms() |
76 |
|
{ |
77 |
15745 |
eq::EqClassesIterator repIt = eq::EqClassesIterator(d_ee); |
78 |
87271 |
while (!repIt.isFinished()) |
79 |
|
{ |
80 |
71526 |
Node eqc = (*repIt); |
81 |
35763 |
Trace("bags-eqc") << "Eqc [ " << eqc << " ] = { "; |
82 |
|
|
83 |
35763 |
if (eqc.getType().isBag()) |
84 |
|
{ |
85 |
1072 |
registerBag(eqc); |
86 |
|
} |
87 |
|
|
88 |
35763 |
eq::EqClassIterator it = eq::EqClassIterator(eqc, d_ee); |
89 |
124513 |
while (!it.isFinished()) |
90 |
|
{ |
91 |
88750 |
Node n = (*it); |
92 |
44375 |
Trace("bags-eqc") << (*it) << " "; |
93 |
44375 |
Kind k = n.getKind(); |
94 |
44375 |
if (k == MK_BAG) |
95 |
|
{ |
96 |
|
// for terms (bag x c) we need to store x by registering the count term |
97 |
|
// (bag.count x (bag x c)) |
98 |
512 |
Node count = d_nm->mkNode(BAG_COUNT, n[0], n); |
99 |
256 |
registerCountTerm(count); |
100 |
512 |
Trace("SolverState::collectBagsAndCountTerms") |
101 |
256 |
<< "registered " << count << endl; |
102 |
|
} |
103 |
44375 |
if (k == BAG_COUNT) |
104 |
|
{ |
105 |
|
// this takes care of all count terms in each equivalent class |
106 |
2883 |
registerCountTerm(n); |
107 |
5766 |
Trace("SolverState::collectBagsAndCountTerms") |
108 |
2883 |
<< "registered " << n << endl; |
109 |
|
} |
110 |
44375 |
++it; |
111 |
|
} |
112 |
35763 |
Trace("bags-eqc") << " } " << std::endl; |
113 |
35763 |
++repIt; |
114 |
|
} |
115 |
|
|
116 |
15745 |
Trace("bags-eqc") << "bag representatives: " << d_bags << endl; |
117 |
15745 |
Trace("bags-eqc") << "bag elements: " << d_bagElements << endl; |
118 |
15745 |
} |
119 |
|
|
120 |
15745 |
void SolverState::collectDisequalBagTerms() |
121 |
|
{ |
122 |
15745 |
eq::EqClassIterator it = eq::EqClassIterator(d_false, d_ee); |
123 |
49695 |
while (!it.isFinished()) |
124 |
|
{ |
125 |
33950 |
Node n = (*it); |
126 |
16975 |
if (n.getKind() == EQUAL && n[0].getType().isBag()) |
127 |
|
{ |
128 |
344 |
Trace("bags-eqc") << "Disequal terms: " << n << std::endl; |
129 |
344 |
d_deq.insert(n); |
130 |
|
} |
131 |
16975 |
++it; |
132 |
|
} |
133 |
15745 |
} |
134 |
|
|
135 |
|
} // namespace bags |
136 |
|
} // namespace theory |
137 |
29505 |
} // namespace cvc5 |