GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/shared_terms_database.cpp Lines: 128 150 85.3 %
Date: 2021-05-22 Branches: 221 690 32.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Dejan Jovanovic, Morgan Deters
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
 * [[ Add lengthier description here ]]
14
 * \todo document this file
15
 */
16
17
#include "theory/shared_terms_database.h"
18
19
#include "smt/smt_statistics_registry.h"
20
#include "theory/theory_engine.h"
21
22
using namespace std;
23
using namespace cvc5::theory;
24
25
namespace cvc5 {
26
27
9460
SharedTermsDatabase::SharedTermsDatabase(TheoryEngine* theoryEngine,
28
                                         context::Context* context,
29
                                         context::UserContext* userContext,
30
9460
                                         ProofNodeManager* pnm)
31
    : ContextNotifyObj(context),
32
      d_statSharedTerms(
33
18920
          smtStatisticsRegistry().registerInt("theory::shared_terms")),
34
      d_addedSharedTermsSize(context, 0),
35
      d_termsToTheories(context),
36
      d_alreadyNotifiedMap(context),
37
      d_registeredEqualities(context),
38
      d_EENotify(*this),
39
      d_theoryEngine(theoryEngine),
40
      d_inConflict(context, false),
41
      d_conflictPolarity(),
42
      d_satContext(context),
43
      d_userContext(userContext),
44
      d_equalityEngine(nullptr),
45
      d_pfee(nullptr),
46
28380
      d_pnm(pnm)
47
{
48
9460
}
49
50
9460
void SharedTermsDatabase::setEqualityEngine(eq::EqualityEngine* ee)
51
{
52
9460
  Assert(ee != nullptr);
53
9460
  d_equalityEngine = ee;
54
  // if proofs are enabled, make the proof equality engine
55
9460
  if (d_pnm != nullptr)
56
  {
57
2384
    d_pfee.reset(
58
1192
        new eq::ProofEqEngine(d_satContext, d_userContext, *ee, d_pnm));
59
  }
60
9460
}
61
62
9460
bool SharedTermsDatabase::needsEqualityEngine(EeSetupInfo& esi)
63
{
64
9460
  esi.d_notify = &d_EENotify;
65
9460
  esi.d_name = "SharedTermsDatabase";
66
9460
  return true;
67
}
68
69
414309
void SharedTermsDatabase::addEqualityToPropagate(TNode equality) {
70
414309
  Assert(d_equalityEngine != nullptr);
71
414309
  d_registeredEqualities.insert(equality);
72
414309
  d_equalityEngine->addTriggerPredicate(equality);
73
414309
  checkForConflict();
74
414309
}
75
76
1063016
void SharedTermsDatabase::addSharedTerm(TNode atom,
77
                                        TNode term,
78
                                        TheoryIdSet theories)
79
{
80
2126032
  Debug("register") << "SharedTermsDatabase::addSharedTerm(" << atom << ", "
81
2126032
                    << term << ", " << TheoryIdSetUtil::setToString(theories)
82
1063016
                    << ")" << std::endl;
83
84
2126032
  std::pair<TNode, TNode> search_pair(atom, term);
85
1063016
  SharedTermsTheoriesMap::iterator find = d_termsToTheories.find(search_pair);
86
1063016
  if (find == d_termsToTheories.end()) {
87
    // First time for this term and this atom
88
1060699
    d_atomsToTerms[atom].push_back(term);
89
1060699
    d_addedSharedTerms.push_back(atom);
90
1060699
    d_addedSharedTermsSize = d_addedSharedTermsSize + 1;
91
1060699
    d_termsToTheories[search_pair] = theories;
92
  } else {
93
2317
    Assert(theories != (*find).second);
94
2317
    d_termsToTheories[search_pair] =
95
4634
        TheoryIdSetUtil::setUnion(theories, (*find).second);
96
  }
97
1063016
}
98
99
4119610
SharedTermsDatabase::shared_terms_iterator SharedTermsDatabase::begin(TNode atom) const {
100
4119610
  Assert(hasSharedTerms(atom));
101
4119610
  return d_atomsToTerms.find(atom)->second.begin();
102
}
103
104
4119610
SharedTermsDatabase::shared_terms_iterator SharedTermsDatabase::end(TNode atom) const {
105
4119610
  Assert(hasSharedTerms(atom));
106
4119610
  return d_atomsToTerms.find(atom)->second.end();
107
}
108
109
14869326
bool SharedTermsDatabase::hasSharedTerms(TNode atom) const {
110
14869326
  return d_atomsToTerms.find(atom) != d_atomsToTerms.end();
111
}
112
113
2264656
void SharedTermsDatabase::backtrack() {
114
3325355
  for (int i = d_addedSharedTerms.size() - 1, i_end = (int)d_addedSharedTermsSize; i >= i_end; -- i) {
115
2121398
    TNode atom = d_addedSharedTerms[i];
116
1060699
    shared_terms_list& list = d_atomsToTerms[atom];
117
1060699
    list.pop_back();
118
1060699
    if (list.empty()) {
119
396649
      d_atomsToTerms.erase(atom);
120
    }
121
  }
122
2264656
  d_addedSharedTerms.resize(d_addedSharedTermsSize);
123
2264656
}
124
125
11677769
TheoryIdSet SharedTermsDatabase::getTheoriesToNotify(TNode atom,
126
                                                     TNode term) const
127
{
128
  // Get the theories that share this term from this atom
129
23355538
  std::pair<TNode, TNode> search_pair(atom, term);
130
11677769
  SharedTermsTheoriesMap::iterator find = d_termsToTheories.find(search_pair);
131
11677769
  Assert(find != d_termsToTheories.end());
132
133
  // Get the theories that were already notified
134
11677769
  TheoryIdSet alreadyNotified = 0;
135
11677769
  AlreadyNotifiedMap::iterator theoriesFind = d_alreadyNotifiedMap.find(term);
136
11677769
  if (theoriesFind != d_alreadyNotifiedMap.end()) {
137
10848223
    alreadyNotified = (*theoriesFind).second;
138
  }
139
140
  // Return the ones that haven't been notified yet
141
23355538
  return TheoryIdSetUtil::setDifference((*find).second, alreadyNotified);
142
}
143
144
TheoryIdSet SharedTermsDatabase::getNotifiedTheories(TNode term) const
145
{
146
  // Get the theories that were already notified
147
  AlreadyNotifiedMap::iterator theoriesFind = d_alreadyNotifiedMap.find(term);
148
  if (theoriesFind != d_alreadyNotifiedMap.end()) {
149
    return (*theoriesFind).second;
150
  } else {
151
    return 0;
152
  }
153
}
154
155
2847848
bool SharedTermsDatabase::propagateSharedEquality(TheoryId theory, TNode a, TNode b, bool value)
156
{
157
2847848
  Debug("shared-terms-database") << "SharedTermsDatabase::newEquality(" << theory << "," << a << "," << b << ", " << (value ? "true" : "false") << ")" << endl;
158
159
2847848
  if (d_inConflict) {
160
    return false;
161
  }
162
163
  // Propagate away
164
5695696
  Node equality = a.eqNode(b);
165
2847848
  if (value) {
166
1931488
    d_theoryEngine->assertToTheory(equality, equality, theory, THEORY_BUILTIN);
167
  } else {
168
916360
    d_theoryEngine->assertToTheory(equality.notNode(), equality.notNode(), theory, THEORY_BUILTIN);
169
  }
170
171
  // As you were
172
2847848
  return true;
173
}
174
175
11677769
void SharedTermsDatabase::markNotified(TNode term, TheoryIdSet theories)
176
{
177
  // Find out if there are any new theories that were notified about this term
178
11677769
  TheoryIdSet alreadyNotified = 0;
179
11677769
  AlreadyNotifiedMap::iterator theoriesFind = d_alreadyNotifiedMap.find(term);
180
11677769
  if (theoriesFind != d_alreadyNotifiedMap.end()) {
181
10848223
    alreadyNotified = (*theoriesFind).second;
182
  }
183
  TheoryIdSet newlyNotified =
184
11677769
      TheoryIdSetUtil::setDifference(theories, alreadyNotified);
185
186
  // If no new theories were notified, we are done
187
11677769
  if (newlyNotified == 0) {
188
21674774
    return;
189
  }
190
191
840382
  Debug("shared-terms-database") << "SharedTermsDatabase::markNotified(" << term << ")" << endl;
192
193
  // First update the set of notified theories for this term
194
840382
  d_alreadyNotifiedMap[term] =
195
1680764
      TheoryIdSetUtil::setUnion(newlyNotified, alreadyNotified);
196
197
840382
  if (d_equalityEngine == nullptr)
198
  {
199
    // if we are not assigned an equality engine, there is nothing to do
200
    return;
201
  }
202
203
  // Mark the shared terms in the equality engine
204
  theory::TheoryId currentTheory;
205
1693044
  while ((currentTheory = TheoryIdSetUtil::setPop(newlyNotified))
206
2533426
         != THEORY_LAST)
207
  {
208
1693044
    d_equalityEngine->addTriggerTerm(term, currentTheory);
209
  }
210
211
  // Check for any conflits
212
840382
  checkForConflict();
213
}
214
215
1146622
bool SharedTermsDatabase::areEqual(TNode a, TNode b) const {
216
1146622
  Assert(d_equalityEngine != nullptr);
217
1146622
  if (d_equalityEngine->hasTerm(a) && d_equalityEngine->hasTerm(b))
218
  {
219
1146622
    return d_equalityEngine->areEqual(a, b);
220
  } else {
221
    Assert(d_equalityEngine->hasTerm(a) || a.isConst());
222
    Assert(d_equalityEngine->hasTerm(b) || b.isConst());
223
    // since one (or both) of them is a constant, and the other is in the equality engine, they are not same
224
    return false;
225
  }
226
}
227
228
969151
bool SharedTermsDatabase::areDisequal(TNode a, TNode b) const {
229
969151
  Assert(d_equalityEngine != nullptr);
230
969151
  if (d_equalityEngine->hasTerm(a) && d_equalityEngine->hasTerm(b))
231
  {
232
969151
    return d_equalityEngine->areDisequal(a, b, false);
233
  } else {
234
    Assert(d_equalityEngine->hasTerm(a) || a.isConst());
235
    Assert(d_equalityEngine->hasTerm(b) || b.isConst());
236
    // one (or both) are in the equality engine
237
    return false;
238
  }
239
}
240
241
theory::eq::EqualityEngine* SharedTermsDatabase::getEqualityEngine()
242
{
243
  return d_equalityEngine;
244
}
245
246
4977028
void SharedTermsDatabase::assertEquality(TNode equality, bool polarity, TNode reason)
247
{
248
4977028
  Assert(d_equalityEngine != nullptr);
249
4977028
  Debug("shared-terms-database::assert") << "SharedTermsDatabase::assertEquality(" << equality << ", " << (polarity ? "true" : "false") << ", " << reason << ")" << endl;
250
  // Add it to the equality engine
251
4977028
  d_equalityEngine->assertEquality(equality, polarity, reason);
252
  // Check for conflict
253
4977028
  checkForConflict();
254
4977028
}
255
256
3827793
bool SharedTermsDatabase::propagateEquality(TNode equality, bool polarity) {
257
3827793
  if (polarity) {
258
1604109
    d_theoryEngine->propagate(equality, THEORY_BUILTIN);
259
  } else {
260
2223684
    d_theoryEngine->propagate(equality.notNode(), THEORY_BUILTIN);
261
  }
262
3827793
  return true;
263
}
264
265
6231719
void SharedTermsDatabase::checkForConflict()
266
{
267
6231719
  if (!d_inConflict)
268
  {
269
6219395
    return;
270
  }
271
12324
  d_inConflict = false;
272
24648
  TrustNode trnc;
273
12324
  if (d_pfee != nullptr)
274
  {
275
3124
    Node conflict = d_conflictLHS.eqNode(d_conflictRHS);
276
1562
    conflict = d_conflictPolarity ? conflict : conflict.notNode();
277
1562
    trnc = d_pfee->assertConflict(conflict);
278
  }
279
  else
280
  {
281
    // standard explain
282
21524
    std::vector<TNode> assumptions;
283
21524
    d_equalityEngine->explainEquality(
284
10762
        d_conflictLHS, d_conflictRHS, d_conflictPolarity, assumptions);
285
21524
    Node conflictNode = NodeManager::currentNM()->mkAnd(assumptions);
286
10762
    trnc = TrustNode::mkTrustConflict(conflictNode, nullptr);
287
  }
288
12324
  d_theoryEngine->conflict(trnc, THEORY_BUILTIN);
289
12324
  d_conflictLHS = d_conflictRHS = Node::null();
290
}
291
292
bool SharedTermsDatabase::isKnown(TNode literal) const {
293
  Assert(d_equalityEngine != nullptr);
294
  bool polarity = literal.getKind() != kind::NOT;
295
  TNode equality = polarity ? literal : literal[0];
296
  if (polarity) {
297
    return d_equalityEngine->areEqual(equality[0], equality[1]);
298
  } else {
299
    return d_equalityEngine->areDisequal(equality[0], equality[1], false);
300
  }
301
}
302
303
127246
theory::TrustNode SharedTermsDatabase::explain(TNode literal) const
304
{
305
127246
  if (d_pfee != nullptr)
306
  {
307
    // use the proof equality engine if it exists
308
22280
    return d_pfee->explain(literal);
309
  }
310
  // otherwise, explain without proofs
311
209932
  Node exp = d_equalityEngine->mkExplainLit(literal);
312
  // no proof generator
313
104966
  return TrustNode::mkTrustPropExp(literal, exp, nullptr);
314
}
315
316
28194
}  // namespace cvc5