GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/shared_terms_database.cpp Lines: 135 158 85.4 %
Date: 2021-08-01 Branches: 224 698 32.1 %

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
9838
SharedTermsDatabase::SharedTermsDatabase(TheoryEngine* theoryEngine,
28
                                         context::Context* context,
29
                                         context::UserContext* userContext,
30
9838
                                         ProofNodeManager* pnm)
31
    : ContextNotifyObj(context),
32
      d_statSharedTerms(
33
19676
          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
29514
      d_pnm(pnm)
47
{
48
9838
}
49
50
9838
void SharedTermsDatabase::setEqualityEngine(eq::EqualityEngine* ee)
51
{
52
9838
  Assert(ee != nullptr);
53
9838
  d_equalityEngine = ee;
54
  // if proofs are enabled, make the proof equality engine if necessary
55
9838
  if (d_pnm != nullptr)
56
  {
57
1241
    d_pfee = d_equalityEngine->getProofEqualityEngine();
58
1241
    if (d_pfee == nullptr)
59
    {
60
2468
      d_pfeeAlloc.reset(
61
1234
          new eq::ProofEqEngine(d_satContext, d_userContext, *ee, d_pnm));
62
1234
      d_pfee = d_pfeeAlloc.get();
63
1234
      d_equalityEngine->setProofEqualityEngine(d_pfee);
64
    }
65
  }
66
9838
}
67
68
9838
bool SharedTermsDatabase::needsEqualityEngine(EeSetupInfo& esi)
69
{
70
9838
  esi.d_notify = &d_EENotify;
71
9838
  esi.d_name = "shared::ee";
72
9838
  return true;
73
}
74
75
433249
void SharedTermsDatabase::addEqualityToPropagate(TNode equality) {
76
433249
  Assert(d_equalityEngine != nullptr);
77
433249
  d_registeredEqualities.insert(equality);
78
433249
  d_equalityEngine->addTriggerPredicate(equality);
79
433249
  checkForConflict();
80
433249
}
81
82
1120200
void SharedTermsDatabase::addSharedTerm(TNode atom,
83
                                        TNode term,
84
                                        TheoryIdSet theories)
85
{
86
2240400
  Debug("register") << "SharedTermsDatabase::addSharedTerm(" << atom << ", "
87
2240400
                    << term << ", " << TheoryIdSetUtil::setToString(theories)
88
1120200
                    << ")" << std::endl;
89
90
2240400
  std::pair<TNode, TNode> search_pair(atom, term);
91
1120200
  SharedTermsTheoriesMap::iterator find = d_termsToTheories.find(search_pair);
92
1120200
  if (find == d_termsToTheories.end()) {
93
    // First time for this term and this atom
94
1117385
    d_atomsToTerms[atom].push_back(term);
95
1117385
    d_addedSharedTerms.push_back(atom);
96
1117385
    d_addedSharedTermsSize = d_addedSharedTermsSize + 1;
97
1117385
    d_termsToTheories[search_pair] = theories;
98
  } else {
99
2815
    Assert(theories != (*find).second);
100
2815
    d_termsToTheories[search_pair] =
101
5630
        TheoryIdSetUtil::setUnion(theories, (*find).second);
102
  }
103
1120200
}
104
105
4413855
SharedTermsDatabase::shared_terms_iterator SharedTermsDatabase::begin(TNode atom) const {
106
4413855
  Assert(hasSharedTerms(atom));
107
4413855
  return d_atomsToTerms.find(atom)->second.begin();
108
}
109
110
4413855
SharedTermsDatabase::shared_terms_iterator SharedTermsDatabase::end(TNode atom) const {
111
4413855
  Assert(hasSharedTerms(atom));
112
4413855
  return d_atomsToTerms.find(atom)->second.end();
113
}
114
115
15802913
bool SharedTermsDatabase::hasSharedTerms(TNode atom) const {
116
15802913
  return d_atomsToTerms.find(atom) != d_atomsToTerms.end();
117
}
118
119
3016811
void SharedTermsDatabase::backtrack() {
120
4134196
  for (int i = d_addedSharedTerms.size() - 1, i_end = (int)d_addedSharedTermsSize; i >= i_end; -- i) {
121
2234770
    TNode atom = d_addedSharedTerms[i];
122
1117385
    shared_terms_list& list = d_atomsToTerms[atom];
123
1117385
    list.pop_back();
124
1117385
    if (list.empty()) {
125
446527
      d_atomsToTerms.erase(atom);
126
    }
127
  }
128
3016811
  d_addedSharedTerms.resize(d_addedSharedTermsSize);
129
3016811
}
130
131
11705379
TheoryIdSet SharedTermsDatabase::getTheoriesToNotify(TNode atom,
132
                                                     TNode term) const
133
{
134
  // Get the theories that share this term from this atom
135
23410758
  std::pair<TNode, TNode> search_pair(atom, term);
136
11705379
  SharedTermsTheoriesMap::iterator find = d_termsToTheories.find(search_pair);
137
11705379
  Assert(find != d_termsToTheories.end());
138
139
  // Get the theories that were already notified
140
11705379
  TheoryIdSet alreadyNotified = 0;
141
11705379
  AlreadyNotifiedMap::iterator theoriesFind = d_alreadyNotifiedMap.find(term);
142
11705379
  if (theoriesFind != d_alreadyNotifiedMap.end()) {
143
10897960
    alreadyNotified = (*theoriesFind).second;
144
  }
145
146
  // Return the ones that haven't been notified yet
147
23410758
  return TheoryIdSetUtil::setDifference((*find).second, alreadyNotified);
148
}
149
150
TheoryIdSet SharedTermsDatabase::getNotifiedTheories(TNode term) const
151
{
152
  // Get the theories that were already notified
153
  AlreadyNotifiedMap::iterator theoriesFind = d_alreadyNotifiedMap.find(term);
154
  if (theoriesFind != d_alreadyNotifiedMap.end()) {
155
    return (*theoriesFind).second;
156
  } else {
157
    return 0;
158
  }
159
}
160
161
3129439
bool SharedTermsDatabase::propagateSharedEquality(TheoryId theory, TNode a, TNode b, bool value)
162
{
163
3129439
  Debug("shared-terms-database") << "SharedTermsDatabase::newEquality(" << theory << "," << a << "," << b << ", " << (value ? "true" : "false") << ")" << endl;
164
165
3129439
  if (d_inConflict) {
166
    return false;
167
  }
168
169
  // Propagate away
170
6258878
  Node equality = a.eqNode(b);
171
3129439
  if (value) {
172
2146277
    d_theoryEngine->assertToTheory(equality, equality, theory, THEORY_BUILTIN);
173
  } else {
174
983162
    d_theoryEngine->assertToTheory(equality.notNode(), equality.notNode(), theory, THEORY_BUILTIN);
175
  }
176
177
  // As you were
178
3129439
  return true;
179
}
180
181
11705379
void SharedTermsDatabase::markNotified(TNode term, TheoryIdSet theories)
182
{
183
  // Find out if there are any new theories that were notified about this term
184
11705379
  TheoryIdSet alreadyNotified = 0;
185
11705379
  AlreadyNotifiedMap::iterator theoriesFind = d_alreadyNotifiedMap.find(term);
186
11705379
  if (theoriesFind != d_alreadyNotifiedMap.end()) {
187
10897960
    alreadyNotified = (*theoriesFind).second;
188
  }
189
  TheoryIdSet newlyNotified =
190
11705379
      TheoryIdSetUtil::setDifference(theories, alreadyNotified);
191
192
  // If no new theories were notified, we are done
193
11705379
  if (newlyNotified == 0) {
194
21774450
    return;
195
  }
196
197
818154
  Debug("shared-terms-database") << "SharedTermsDatabase::markNotified(" << term << ")" << endl;
198
199
  // First update the set of notified theories for this term
200
818154
  d_alreadyNotifiedMap[term] =
201
1636308
      TheoryIdSetUtil::setUnion(newlyNotified, alreadyNotified);
202
203
818154
  if (d_equalityEngine == nullptr)
204
  {
205
    // if we are not assigned an equality engine, there is nothing to do
206
    return;
207
  }
208
209
  // Mark the shared terms in the equality engine
210
  theory::TheoryId currentTheory;
211
1646650
  while ((currentTheory = TheoryIdSetUtil::setPop(newlyNotified))
212
2464804
         != THEORY_LAST)
213
  {
214
1646650
    d_equalityEngine->addTriggerTerm(term, currentTheory);
215
  }
216
217
  // Check for any conflits
218
818154
  checkForConflict();
219
}
220
221
1103763
bool SharedTermsDatabase::areEqual(TNode a, TNode b) const {
222
1103763
  Assert(d_equalityEngine != nullptr);
223
1103763
  if (d_equalityEngine->hasTerm(a) && d_equalityEngine->hasTerm(b))
224
  {
225
1103763
    return d_equalityEngine->areEqual(a, b);
226
  } else {
227
    Assert(d_equalityEngine->hasTerm(a) || a.isConst());
228
    Assert(d_equalityEngine->hasTerm(b) || b.isConst());
229
    // since one (or both) of them is a constant, and the other is in the equality engine, they are not same
230
    return false;
231
  }
232
}
233
234
925311
bool SharedTermsDatabase::areDisequal(TNode a, TNode b) const {
235
925311
  Assert(d_equalityEngine != nullptr);
236
925311
  if (d_equalityEngine->hasTerm(a) && d_equalityEngine->hasTerm(b))
237
  {
238
925311
    return d_equalityEngine->areDisequal(a, b, false);
239
  } else {
240
    Assert(d_equalityEngine->hasTerm(a) || a.isConst());
241
    Assert(d_equalityEngine->hasTerm(b) || b.isConst());
242
    // one (or both) are in the equality engine
243
    return false;
244
  }
245
}
246
247
theory::eq::EqualityEngine* SharedTermsDatabase::getEqualityEngine()
248
{
249
  return d_equalityEngine;
250
}
251
252
5325701
void SharedTermsDatabase::assertShared(TNode n, bool polarity, TNode reason)
253
{
254
5325701
  Assert(d_equalityEngine != nullptr);
255
10651402
  Debug("shared-terms-database::assert")
256
5325701
      << "SharedTermsDatabase::assertShared(" << n << ", "
257
5325701
      << (polarity ? "true" : "false") << ", " << reason << ")" << endl;
258
  // Add it to the equality engine
259
5325701
  if (n.getKind() == kind::EQUAL)
260
  {
261
5325701
    d_equalityEngine->assertEquality(n, polarity, reason);
262
  }
263
  else
264
  {
265
    d_equalityEngine->assertPredicate(n, polarity, reason);
266
  }
267
  // Check for conflict
268
5325701
  checkForConflict();
269
5325701
}
270
271
4095013
bool SharedTermsDatabase::propagateEquality(TNode equality, bool polarity) {
272
4095013
  if (polarity) {
273
1733190
    d_theoryEngine->propagate(equality, THEORY_BUILTIN);
274
  } else {
275
2361823
    d_theoryEngine->propagate(equality.notNode(), THEORY_BUILTIN);
276
  }
277
4095013
  return true;
278
}
279
280
6577104
void SharedTermsDatabase::checkForConflict()
281
{
282
6577104
  if (!d_inConflict)
283
  {
284
6564947
    return;
285
  }
286
12157
  d_inConflict = false;
287
24314
  TrustNode trnc;
288
12157
  if (d_pfee != nullptr)
289
  {
290
1808
    Node conflict = d_conflictLHS.eqNode(d_conflictRHS);
291
904
    conflict = d_conflictPolarity ? conflict : conflict.notNode();
292
904
    trnc = d_pfee->assertConflict(conflict);
293
  }
294
  else
295
  {
296
    // standard explain
297
22506
    std::vector<TNode> assumptions;
298
22506
    d_equalityEngine->explainEquality(
299
11253
        d_conflictLHS, d_conflictRHS, d_conflictPolarity, assumptions);
300
22506
    Node conflictNode = NodeManager::currentNM()->mkAnd(assumptions);
301
11253
    trnc = TrustNode::mkTrustConflict(conflictNode, nullptr);
302
  }
303
12157
  d_theoryEngine->conflict(trnc, THEORY_BUILTIN);
304
12157
  d_conflictLHS = d_conflictRHS = Node::null();
305
}
306
307
bool SharedTermsDatabase::isKnown(TNode literal) const {
308
  Assert(d_equalityEngine != nullptr);
309
  bool polarity = literal.getKind() != kind::NOT;
310
  TNode equality = polarity ? literal : literal[0];
311
  if (polarity) {
312
    return d_equalityEngine->areEqual(equality[0], equality[1]);
313
  } else {
314
    return d_equalityEngine->areDisequal(equality[0], equality[1], false);
315
  }
316
}
317
318
108694
TrustNode SharedTermsDatabase::explain(TNode literal) const
319
{
320
108694
  if (d_pfee != nullptr)
321
  {
322
    // use the proof equality engine if it exists
323
13151
    return d_pfee->explain(literal);
324
  }
325
  // otherwise, explain without proofs
326
191086
  Node exp = d_equalityEngine->mkExplainLit(literal);
327
  // no proof generator
328
95543
  return TrustNode::mkTrustPropExp(literal, exp, nullptr);
329
}
330
331
29280
}  // namespace cvc5