1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds |
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 |
|
* Abstract management of models for TheoryEngine. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "theory/model_manager.h" |
17 |
|
|
18 |
|
#include "options/smt_options.h" |
19 |
|
#include "options/theory_options.h" |
20 |
|
#include "prop/prop_engine.h" |
21 |
|
#include "smt/env.h" |
22 |
|
#include "theory/quantifiers/first_order_model.h" |
23 |
|
#include "theory/quantifiers/fmf/model_builder.h" |
24 |
|
#include "theory/quantifiers_engine.h" |
25 |
|
#include "theory/theory_engine.h" |
26 |
|
|
27 |
|
namespace cvc5 { |
28 |
|
namespace theory { |
29 |
|
|
30 |
8954 |
ModelManager::ModelManager(TheoryEngine& te, Env& env, EqEngineManager& eem) |
31 |
|
: d_te(te), |
32 |
|
d_env(env), |
33 |
|
d_eem(eem), |
34 |
|
d_modelEqualityEngine(nullptr), |
35 |
|
d_modelEqualityEngineAlloc(nullptr), |
36 |
|
d_model(new TheoryModel( |
37 |
8954 |
env, "DefaultModel", options::assignFunctionValues())), |
38 |
|
d_modelBuilder(nullptr), |
39 |
|
d_modelBuilt(false), |
40 |
17908 |
d_modelBuiltSuccess(false) |
41 |
|
{ |
42 |
8954 |
} |
43 |
|
|
44 |
8954 |
ModelManager::~ModelManager() {} |
45 |
|
|
46 |
8954 |
void ModelManager::finishInit(eq::EqualityEngineNotify* notify) |
47 |
|
{ |
48 |
|
// construct the model |
49 |
8954 |
const LogicInfo& logicInfo = d_env.getLogicInfo(); |
50 |
|
// Initialize the model and model builder. |
51 |
8954 |
if (logicInfo.isQuantified()) |
52 |
|
{ |
53 |
6286 |
QuantifiersEngine* qe = d_te.getQuantifiersEngine(); |
54 |
6286 |
Assert(qe != nullptr); |
55 |
6286 |
d_modelBuilder = qe->getModelBuilder(); |
56 |
|
} |
57 |
|
|
58 |
|
// make the default builder, e.g. in the case that the quantifiers engine does |
59 |
|
// not have a model builder |
60 |
8954 |
if (d_modelBuilder == nullptr) |
61 |
|
{ |
62 |
2668 |
d_alocModelBuilder.reset(new TheoryEngineModelBuilder); |
63 |
2668 |
d_modelBuilder = d_alocModelBuilder.get(); |
64 |
|
} |
65 |
|
// notice that the equality engine of the model has yet to be assigned. |
66 |
8954 |
initializeModelEqEngine(notify); |
67 |
8954 |
} |
68 |
|
|
69 |
18663 |
void ModelManager::resetModel() |
70 |
|
{ |
71 |
18663 |
d_modelBuilt = false; |
72 |
18663 |
d_modelBuiltSuccess = false; |
73 |
|
// Reset basic information on the model object |
74 |
18663 |
d_model->reset(); |
75 |
18663 |
} |
76 |
|
|
77 |
23480 |
bool ModelManager::buildModel() |
78 |
|
{ |
79 |
23480 |
if (d_modelBuilt) |
80 |
|
{ |
81 |
|
// already computed |
82 |
8330 |
return d_modelBuiltSuccess; |
83 |
|
} |
84 |
|
// reset the flags now |
85 |
15150 |
d_modelBuilt = true; |
86 |
15150 |
d_modelBuiltSuccess = false; |
87 |
|
|
88 |
|
// prepare the model, which is specific to the manager |
89 |
15150 |
if (!prepareModel()) |
90 |
|
{ |
91 |
2 |
Trace("model-builder") << "ModelManager: fail prepare model" << std::endl; |
92 |
2 |
return false; |
93 |
|
} |
94 |
|
|
95 |
|
// now, finish building the model |
96 |
15147 |
d_modelBuiltSuccess = finishBuildModel(); |
97 |
|
|
98 |
15147 |
if (Trace.isOn("model-builder")) |
99 |
|
{ |
100 |
|
Trace("model-builder") << "Final model:" << std::endl; |
101 |
|
Trace("model-builder") << d_model->debugPrintModelEqc() << std::endl; |
102 |
|
} |
103 |
|
|
104 |
30294 |
Trace("model-builder") << "ModelManager: model built success is " |
105 |
15147 |
<< d_modelBuiltSuccess << std::endl; |
106 |
|
|
107 |
15147 |
return d_modelBuiltSuccess; |
108 |
|
} |
109 |
|
|
110 |
|
bool ModelManager::isModelBuilt() const { return d_modelBuilt; } |
111 |
|
|
112 |
6138 |
void ModelManager::postProcessModel(bool incomplete) |
113 |
|
{ |
114 |
6138 |
if (!d_modelBuilt) |
115 |
|
{ |
116 |
|
// model not built, nothing to do |
117 |
4944 |
return; |
118 |
|
} |
119 |
1194 |
Trace("model-builder") << "ModelManager: post-process model..." << std::endl; |
120 |
|
// model construction should always succeed unless lemmas were added |
121 |
1194 |
AlwaysAssert(d_modelBuiltSuccess); |
122 |
1194 |
if (!options::produceModels()) |
123 |
|
{ |
124 |
345 |
return; |
125 |
|
} |
126 |
|
// Do post-processing of model from the theories (used for THEORY_SEP |
127 |
|
// to construct heap model) |
128 |
11886 |
for (TheoryId theoryId = theory::THEORY_FIRST; theoryId < theory::THEORY_LAST; |
129 |
|
++theoryId) |
130 |
|
{ |
131 |
11037 |
Theory* t = d_te.theoryOf(theoryId); |
132 |
11037 |
if (t == nullptr) |
133 |
|
{ |
134 |
|
// theory not active, skip |
135 |
|
continue; |
136 |
|
} |
137 |
22074 |
Trace("model-builder-debug") |
138 |
11037 |
<< " PostProcessModel on theory: " << theoryId << std::endl; |
139 |
11037 |
t->postProcessModel(d_model.get()); |
140 |
|
} |
141 |
|
// also call the model builder's post-process model |
142 |
849 |
d_modelBuilder->postProcessModel(incomplete, d_model.get()); |
143 |
|
} |
144 |
|
|
145 |
260807 |
theory::TheoryModel* ModelManager::getModel() { return d_model.get(); } |
146 |
|
|
147 |
15147 |
bool ModelManager::collectModelBooleanVariables() |
148 |
|
{ |
149 |
15147 |
Trace("model-builder") << " CollectModelInfo boolean variables" << std::endl; |
150 |
|
// Get value of the Boolean variables |
151 |
15147 |
prop::PropEngine* propEngine = d_te.getPropEngine(); |
152 |
30294 |
std::vector<TNode> boolVars; |
153 |
15147 |
propEngine->getBooleanVariables(boolVars); |
154 |
15147 |
std::vector<TNode>::iterator it, iend = boolVars.end(); |
155 |
|
bool hasValue, value; |
156 |
73394 |
for (it = boolVars.begin(); it != iend; ++it) |
157 |
|
{ |
158 |
116494 |
TNode var = *it; |
159 |
58247 |
hasValue = propEngine->hasValue(var, value); |
160 |
|
// Should we assert that hasValue is true? |
161 |
58247 |
if (!hasValue) |
162 |
|
{ |
163 |
20292 |
Trace("model-builder-assertions") |
164 |
10146 |
<< " has no value : " << var << std::endl; |
165 |
10146 |
value = false; |
166 |
|
} |
167 |
116494 |
Trace("model-builder-assertions") |
168 |
116494 |
<< "(assert" << (value ? " " : " (not ") << var |
169 |
58247 |
<< (value ? ");" : "));") << std::endl; |
170 |
58247 |
if (!d_model->assertPredicate(var, value)) |
171 |
|
{ |
172 |
|
return false; |
173 |
|
} |
174 |
|
} |
175 |
15147 |
return true; |
176 |
|
} |
177 |
|
|
178 |
111998 |
void ModelManager::collectAssertedTerms(TheoryId tid, |
179 |
|
std::set<Node>& termSet, |
180 |
|
bool includeShared) const |
181 |
|
{ |
182 |
111998 |
Theory* t = d_te.theoryOf(tid); |
183 |
|
// Collect all terms appearing in assertions |
184 |
111998 |
context::CDList<Assertion>::const_iterator assert_it = t->facts_begin(), |
185 |
111998 |
assert_it_end = t->facts_end(); |
186 |
4246028 |
for (; assert_it != assert_it_end; ++assert_it) |
187 |
|
{ |
188 |
2067015 |
collectTerms(tid, *assert_it, termSet); |
189 |
|
} |
190 |
|
|
191 |
111998 |
if (includeShared) |
192 |
|
{ |
193 |
|
// Add terms that are shared terms |
194 |
111998 |
context::CDList<TNode>::const_iterator shared_it = t->shared_terms_begin(), |
195 |
|
shared_it_end = |
196 |
111998 |
t->shared_terms_end(); |
197 |
1335308 |
for (; shared_it != shared_it_end; ++shared_it) |
198 |
|
{ |
199 |
611655 |
collectTerms(tid, *shared_it, termSet); |
200 |
|
} |
201 |
|
} |
202 |
111998 |
} |
203 |
|
|
204 |
2678670 |
void ModelManager::collectTerms(TheoryId tid, |
205 |
|
TNode n, |
206 |
|
std::set<Node>& termSet) const |
207 |
|
{ |
208 |
2678670 |
const std::set<Kind>& irrKinds = d_model->getIrrelevantKinds(); |
209 |
5357340 |
std::vector<TNode> visit; |
210 |
5357340 |
TNode cur; |
211 |
2678670 |
visit.push_back(n); |
212 |
6830396 |
do |
213 |
|
{ |
214 |
9509066 |
cur = visit.back(); |
215 |
9509066 |
visit.pop_back(); |
216 |
9509066 |
if (termSet.find(cur) != termSet.end()) |
217 |
|
{ |
218 |
|
// already visited |
219 |
4633238 |
continue; |
220 |
|
} |
221 |
4875828 |
Kind k = cur.getKind(); |
222 |
|
// only add to term set if a relevant kind |
223 |
4875828 |
if (irrKinds.find(k) == irrKinds.end()) |
224 |
|
{ |
225 |
2361596 |
termSet.insert(cur); |
226 |
|
} |
227 |
|
// traverse owned terms, don't go under quantifiers |
228 |
13512084 |
if ((k == kind::NOT || k == kind::EQUAL || Theory::theoryOf(cur) == tid) |
229 |
14294387 |
&& !cur.isClosure()) |
230 |
|
{ |
231 |
4511899 |
visit.insert(visit.end(), cur.begin(), cur.end()); |
232 |
|
} |
233 |
9509066 |
} while (!visit.empty()); |
234 |
2678670 |
} |
235 |
|
|
236 |
|
} // namespace theory |
237 |
27735 |
} // namespace cvc5 |