GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/shared_terms_database.cpp Lines: 135 158 85.4 %
Date: 2021-09-30 Branches: 223 696 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
6278
SharedTermsDatabase::SharedTermsDatabase(TheoryEngine* theoryEngine,
28
                                         context::Context* context,
29
                                         context::UserContext* userContext,
30
6278
                                         ProofNodeManager* pnm)
31
    : ContextNotifyObj(context),
32
      d_statSharedTerms(
33
12556
          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
18834
      d_pnm(pnm)
47
{
48
6278
}
49
50
6278
void SharedTermsDatabase::setEqualityEngine(eq::EqualityEngine* ee)
51
{
52
6278
  Assert(ee != nullptr);
53
6278
  d_equalityEngine = ee;
54
  // if proofs are enabled, make the proof equality engine if necessary
55
6278
  if (d_pnm != nullptr)
56
  {
57
60
    d_pfee = d_equalityEngine->getProofEqualityEngine();
58
60
    if (d_pfee == nullptr)
59
    {
60
120
      d_pfeeAlloc.reset(
61
60
          new eq::ProofEqEngine(d_satContext, d_userContext, *ee, d_pnm));
62
60
      d_pfee = d_pfeeAlloc.get();
63
60
      d_equalityEngine->setProofEqualityEngine(d_pfee);
64
    }
65
  }
66
6278
}
67
68
6278
bool SharedTermsDatabase::needsEqualityEngine(EeSetupInfo& esi)
69
{
70
6278
  esi.d_notify = &d_EENotify;
71
6278
  esi.d_name = "shared::ee";
72
6278
  return true;
73
}
74
75
244969
void SharedTermsDatabase::addEqualityToPropagate(TNode equality) {
76
244969
  Assert(d_equalityEngine != nullptr);
77
244969
  d_registeredEqualities.insert(equality);
78
244969
  d_equalityEngine->addTriggerPredicate(equality);
79
244969
  checkForConflict();
80
244969
}
81
82
702611
void SharedTermsDatabase::addSharedTerm(TNode atom,
83
                                        TNode term,
84
                                        TheoryIdSet theories)
85
{
86
1405222
  Debug("register") << "SharedTermsDatabase::addSharedTerm(" << atom << ", "
87
1405222
                    << term << ", " << TheoryIdSetUtil::setToString(theories)
88
702611
                    << ")" << std::endl;
89
90
1405222
  std::pair<TNode, TNode> search_pair(atom, term);
91
702611
  SharedTermsTheoriesMap::iterator find = d_termsToTheories.find(search_pair);
92
702611
  if (find == d_termsToTheories.end()) {
93
    // First time for this term and this atom
94
700425
    d_atomsToTerms[atom].push_back(term);
95
700425
    d_addedSharedTerms.push_back(atom);
96
700425
    d_addedSharedTermsSize = d_addedSharedTermsSize + 1;
97
700425
    d_termsToTheories[search_pair] = theories;
98
  } else {
99
2186
    Assert(theories != (*find).second);
100
2186
    d_termsToTheories[search_pair] =
101
4372
        TheoryIdSetUtil::setUnion(theories, (*find).second);
102
  }
103
702611
}
104
105
3667730
SharedTermsDatabase::shared_terms_iterator SharedTermsDatabase::begin(TNode atom) const {
106
3667730
  Assert(hasSharedTerms(atom));
107
3667730
  return d_atomsToTerms.find(atom)->second.begin();
108
}
109
110
3667730
SharedTermsDatabase::shared_terms_iterator SharedTermsDatabase::end(TNode atom) const {
111
3667730
  Assert(hasSharedTerms(atom));
112
3667730
  return d_atomsToTerms.find(atom)->second.end();
113
}
114
115
13157258
bool SharedTermsDatabase::hasSharedTerms(TNode atom) const {
116
13157258
  return d_atomsToTerms.find(atom) != d_atomsToTerms.end();
117
}
118
119
1907383
void SharedTermsDatabase::backtrack() {
120
2607808
  for (int i = d_addedSharedTerms.size() - 1, i_end = (int)d_addedSharedTermsSize; i >= i_end; -- i) {
121
1400850
    TNode atom = d_addedSharedTerms[i];
122
700425
    shared_terms_list& list = d_atomsToTerms[atom];
123
700425
    list.pop_back();
124
700425
    if (list.empty()) {
125
264015
      d_atomsToTerms.erase(atom);
126
    }
127
  }
128
1907383
  d_addedSharedTerms.resize(d_addedSharedTermsSize);
129
1907383
}
130
131
10167936
TheoryIdSet SharedTermsDatabase::getTheoriesToNotify(TNode atom,
132
                                                     TNode term) const
133
{
134
  // Get the theories that share this term from this atom
135
20335872
  std::pair<TNode, TNode> search_pair(atom, term);
136
10167936
  SharedTermsTheoriesMap::iterator find = d_termsToTheories.find(search_pair);
137
10167936
  Assert(find != d_termsToTheories.end());
138
139
  // Get the theories that were already notified
140
10167936
  TheoryIdSet alreadyNotified = 0;
141
10167936
  AlreadyNotifiedMap::iterator theoriesFind = d_alreadyNotifiedMap.find(term);
142
10167936
  if (theoriesFind != d_alreadyNotifiedMap.end()) {
143
9622815
    alreadyNotified = (*theoriesFind).second;
144
  }
145
146
  // Return the ones that haven't been notified yet
147
20335872
  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
2609377
bool SharedTermsDatabase::propagateSharedEquality(TheoryId theory, TNode a, TNode b, bool value)
162
{
163
2609377
  Debug("shared-terms-database") << "SharedTermsDatabase::newEquality(" << theory << "," << a << "," << b << ", " << (value ? "true" : "false") << ")" << endl;
164
165
2609377
  if (d_inConflict) {
166
    return false;
167
  }
168
169
  // Propagate away
170
5218754
  Node equality = a.eqNode(b);
171
2609377
  if (value) {
172
1669995
    d_theoryEngine->assertToTheory(equality, equality, theory, THEORY_BUILTIN);
173
  } else {
174
939382
    d_theoryEngine->assertToTheory(equality.notNode(), equality.notNode(), theory, THEORY_BUILTIN);
175
  }
176
177
  // As you were
178
2609377
  return true;
179
}
180
181
10167936
void SharedTermsDatabase::markNotified(TNode term, TheoryIdSet theories)
182
{
183
  // Find out if there are any new theories that were notified about this term
184
10167936
  TheoryIdSet alreadyNotified = 0;
185
10167936
  AlreadyNotifiedMap::iterator theoriesFind = d_alreadyNotifiedMap.find(term);
186
10167936
  if (theoriesFind != d_alreadyNotifiedMap.end()) {
187
9622815
    alreadyNotified = (*theoriesFind).second;
188
  }
189
  TheoryIdSet newlyNotified =
190
10167936
      TheoryIdSetUtil::setDifference(theories, alreadyNotified);
191
192
  // If no new theories were notified, we are done
193
10167936
  if (newlyNotified == 0) {
194
19198684
    return;
195
  }
196
197
568594
  Debug("shared-terms-database") << "SharedTermsDatabase::markNotified(" << term << ")" << endl;
198
199
  // First update the set of notified theories for this term
200
568594
  d_alreadyNotifiedMap[term] =
201
1137188
      TheoryIdSetUtil::setUnion(newlyNotified, alreadyNotified);
202
203
568594
  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
1129907
  while ((currentTheory = TheoryIdSetUtil::setPop(newlyNotified))
212
1698501
         != THEORY_LAST)
213
  {
214
1129907
    d_equalityEngine->addTriggerTerm(term, currentTheory);
215
  }
216
217
  // Check for any conflits
218
568594
  checkForConflict();
219
}
220
221
536787
bool SharedTermsDatabase::areEqual(TNode a, TNode b) const {
222
536787
  Assert(d_equalityEngine != nullptr);
223
536787
  if (d_equalityEngine->hasTerm(a) && d_equalityEngine->hasTerm(b))
224
  {
225
536787
    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
449669
bool SharedTermsDatabase::areDisequal(TNode a, TNode b) const {
235
449669
  Assert(d_equalityEngine != nullptr);
236
449669
  if (d_equalityEngine->hasTerm(a) && d_equalityEngine->hasTerm(b))
237
  {
238
449669
    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
4085232
void SharedTermsDatabase::assertShared(TNode n, bool polarity, TNode reason)
253
{
254
4085232
  Assert(d_equalityEngine != nullptr);
255
8170464
  Debug("shared-terms-database::assert")
256
4085232
      << "SharedTermsDatabase::assertShared(" << n << ", "
257
4085232
      << (polarity ? "true" : "false") << ", " << reason << ")" << endl;
258
  // Add it to the equality engine
259
4085232
  if (n.getKind() == kind::EQUAL)
260
  {
261
4085232
    d_equalityEngine->assertEquality(n, polarity, reason);
262
  }
263
  else
264
  {
265
    d_equalityEngine->assertPredicate(n, polarity, reason);
266
  }
267
  // Check for conflict
268
4085232
  checkForConflict();
269
4085232
}
270
271
3116028
bool SharedTermsDatabase::propagateEquality(TNode equality, bool polarity) {
272
3116028
  if (polarity) {
273
1294235
    d_theoryEngine->propagate(equality, THEORY_BUILTIN);
274
  } else {
275
1821793
    d_theoryEngine->propagate(equality.notNode(), THEORY_BUILTIN);
276
  }
277
3116028
  return true;
278
}
279
280
4898795
void SharedTermsDatabase::checkForConflict()
281
{
282
4898795
  if (!d_inConflict)
283
  {
284
4889299
    return;
285
  }
286
9496
  d_inConflict = false;
287
18992
  TrustNode trnc;
288
9496
  if (d_pfee != nullptr)
289
  {
290
86
    Node conflict = d_conflictLHS.eqNode(d_conflictRHS);
291
43
    conflict = d_conflictPolarity ? conflict : conflict.notNode();
292
43
    trnc = d_pfee->assertConflict(conflict);
293
  }
294
  else
295
  {
296
    // standard explain
297
18906
    std::vector<TNode> assumptions;
298
18906
    d_equalityEngine->explainEquality(
299
9453
        d_conflictLHS, d_conflictRHS, d_conflictPolarity, assumptions);
300
18906
    Node conflictNode = NodeManager::currentNM()->mkAnd(assumptions);
301
9453
    trnc = TrustNode::mkTrustConflict(conflictNode, nullptr);
302
  }
303
9496
  d_theoryEngine->conflict(trnc, THEORY_BUILTIN);
304
9496
  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
76496
TrustNode SharedTermsDatabase::explain(TNode literal) const
319
{
320
76496
  if (d_pfee != nullptr)
321
  {
322
    // use the proof equality engine if it exists
323
779
    return d_pfee->explain(literal);
324
  }
325
  // otherwise, explain without proofs
326
151434
  Node exp = d_equalityEngine->mkExplainLit(literal);
327
  // no proof generator
328
75717
  return TrustNode::mkTrustPropExp(literal, exp, nullptr);
329
}
330
331
22755
}  // namespace cvc5