summaryrefslogtreecommitdiff
path: root/src/theory/relevance_manager.cpp
blob: e1a3421788564226231f626ba677e5575c254f5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/******************************************************************************
 * Top contributors (to current version):
 *   Andrew Reynolds, Aina Niemetz
 *
 * This file is part of the cvc5 project.
 *
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
 * in the top-level source directory and their institutional affiliations.
 * All rights reserved.  See the file COPYING in the top-level source
 * directory for licensing information.
 * ****************************************************************************
 *
 * Implementation of relevance manager.
 */

#include "theory/relevance_manager.h"

#include <sstream>

#include "options/smt_options.h"
#include "smt/env.h"

using namespace cvc5::kind;

namespace cvc5 {
namespace theory {

RelevanceManager::RelevanceManager(context::UserContext* userContext,
                                   Valuation val)
    : d_val(val),
      d_input(userContext),
      d_computed(false),
      d_success(false),
      d_trackRSetExp(false),
      d_miniscopeTopLevel(true)
{
  if (options::produceDifficulty())
  {
    d_dman.reset(new DifficultyManager(userContext, val));
    d_trackRSetExp = true;
    // we cannot miniscope AND at the top level, since we need to
    // preserve the exact form of preprocessed assertions so the dependencies
    // are tracked.
    d_miniscopeTopLevel = false;
  }
}

void RelevanceManager::notifyPreprocessedAssertions(
    const std::vector<Node>& assertions)
{
  // add to input list, which is user-context dependent
  std::vector<Node> toProcess;
  for (const Node& a : assertions)
  {
    if (d_miniscopeTopLevel && a.getKind() == AND)
    {
      // split top-level AND
      for (const Node& ac : a)
      {
        toProcess.push_back(ac);
      }
    }
    else
    {
      d_input.push_back(a);
    }
  }
  addAssertionsInternal(toProcess);
}

void RelevanceManager::notifyPreprocessedAssertion(Node n)
{
  std::vector<Node> toProcess;
  toProcess.push_back(n);
  addAssertionsInternal(toProcess);
}

void RelevanceManager::addAssertionsInternal(std::vector<Node>& toProcess)
{
  size_t i = 0;
  while (i < toProcess.size())
  {
    Node a = toProcess[i];
    if (d_miniscopeTopLevel && a.getKind() == AND)
    {
      // difficulty tracking disables miniscoping of AND
      Assert(d_dman == nullptr);
      // split AND
      for (const Node& ac : a)
      {
        toProcess.push_back(ac);
      }
    }
    else
    {
      // note that a could be a literal, in which case we could add it to
      // an "always relevant" set here.
      d_input.push_back(a);
    }
    i++;
  }
}

void RelevanceManager::resetRound()
{
  d_computed = false;
}

void RelevanceManager::computeRelevance()
{
  d_computed = true;
  d_rset.clear();
  d_rsetExp.clear();
  Trace("rel-manager") << "RelevanceManager::computeRelevance..." << std::endl;
  std::unordered_map<TNode, int> cache;
  for (const Node& node: d_input)
  {
    TNode n = node;
    int val = justify(n, cache);
    if (val != 1)
    {
      std::stringstream serr;
      serr << "RelevanceManager::computeRelevance: WARNING: failed to justify "
           << n;
      Trace("rel-manager") << serr.str() << std::endl;
      Assert(false) << serr.str();
      d_success = false;
      d_rset.clear();
      return;
    }
  }
  Trace("rel-manager") << "...success, size = " << d_rset.size() << std::endl;
  d_success = true;
}

bool RelevanceManager::isBooleanConnective(TNode cur)
{
  Kind k = cur.getKind();
  return k == NOT || k == IMPLIES || k == AND || k == OR || k == ITE || k == XOR
         || (k == EQUAL && cur[0].getType().isBoolean());
}

bool RelevanceManager::updateJustifyLastChild(
    TNode cur,
    std::vector<int>& childrenJustify,
    std::unordered_map<TNode, int>& cache)
{
  // This method is run when we are informed that child index of cur
  // has justify status lastChildJustify. We return true if we would like to
  // compute the next child, in this case we push the status of the current
  // child to childrenJustify.
  size_t nchildren = cur.getNumChildren();
  Assert(isBooleanConnective(cur));
  size_t index = childrenJustify.size();
  Assert(index < nchildren);
  Assert(cache.find(cur[index]) != cache.end());
  Kind k = cur.getKind();
  // Lookup the last child's value in the overall cache, we may choose to
  // add this to childrenJustify if we return true.
  int lastChildJustify = cache[cur[index]];
  if (k == NOT)
  {
    cache[cur] = -lastChildJustify;
  }
  else if (k == IMPLIES || k == AND || k == OR)
  {
    if (lastChildJustify != 0)
    {
      // See if we short circuited? The value for short circuiting is false if
      // we are AND or the first child of IMPLIES.
      if (lastChildJustify
          == ((k == AND || (k == IMPLIES && index == 0)) ? -1 : 1))
      {
        cache[cur] = k == AND ? -1 : 1;
        return false;
      }
    }
    if (index + 1 == nchildren)
    {
      // finished all children, compute the overall value
      int ret = k == AND ? 1 : -1;
      for (int cv : childrenJustify)
      {
        if (cv == 0)
        {
          ret = 0;
          break;
        }
      }
      cache[cur] = ret;
    }
    else
    {
      // continue
      childrenJustify.push_back(lastChildJustify);
      return true;
    }
  }
  else if (lastChildJustify == 0)
  {
    // all other cases, an unknown child implies we are unknown
    cache[cur] = 0;
  }
  else if (k == ITE)
  {
    if (index == 0)
    {
      Assert(lastChildJustify != 0);
      // continue with branch
      childrenJustify.push_back(lastChildJustify);
      if (lastChildJustify == -1)
      {
        // also mark first branch as don't care
        childrenJustify.push_back(0);
      }
      return true;
    }
    else
    {
      // should be in proper branch
      Assert(childrenJustify[0] == (index == 1 ? 1 : -1));
      // we are the value of the branch
      cache[cur] = lastChildJustify;
    }
  }
  else
  {
    Assert(k == XOR || k == EQUAL);
    Assert(nchildren == 2);
    Assert(lastChildJustify != 0);
    if (index == 0)
    {
      // must compute the other child
      childrenJustify.push_back(lastChildJustify);
      return true;
    }
    else
    {
      // both children known, compute value
      Assert(childrenJustify.size() == 1 && childrenJustify[0] != 0);
      cache[cur] =
          ((k == XOR ? -1 : 1) * lastChildJustify == childrenJustify[0]) ? 1
                                                                         : -1;
    }
  }
  return false;
}

int RelevanceManager::justify(TNode n, std::unordered_map<TNode, int>& cache)
{
  // the vector of values of children
  std::unordered_map<TNode, std::vector<int>> childJustify;
  std::unordered_map<TNode, int>::iterator it;
  std::unordered_map<TNode, std::vector<int>>::iterator itc;
  std::vector<TNode> visit;
  TNode cur;
  visit.push_back(n);
  do
  {
    cur = visit.back();
    // should always have Boolean type
    Assert(cur.getType().isBoolean());
    it = cache.find(cur);
    if (it != cache.end())
    {
      visit.pop_back();
      // already computed value
      continue;
    }
    itc = childJustify.find(cur);
    // have we traversed to children yet?
    if (itc == childJustify.end())
    {
      // are we not a Boolean connective (including NOT)?
      if (isBooleanConnective(cur))
      {
        // initialize its children justify vector as empty
        childJustify[cur].clear();
        // start with the first child
        visit.push_back(cur[0]);
      }
      else
      {
        visit.pop_back();
        // The atom case, lookup the value in the valuation class to
        // see its current value in the SAT solver, if it has one.
        int ret = 0;
        // otherwise we look up the value
        bool value;
        if (d_val.hasSatValue(cur, value))
        {
          ret = value ? 1 : -1;
          d_rset.insert(cur);
          if (d_trackRSetExp)
          {
            d_rsetExp[cur] = n;
          }
        }
        cache[cur] = ret;
      }
    }
    else
    {
      // this processes the impact of the current child on the value of cur,
      // and possibly requests that a new child is computed.
      if (updateJustifyLastChild(cur, itc->second, cache))
      {
        Assert(itc->second.size() < cur.getNumChildren());
        TNode nextChild = cur[itc->second.size()];
        visit.push_back(nextChild);
      }
      else
      {
        visit.pop_back();
      }
    }
  } while (!visit.empty());
  Assert(cache.find(n) != cache.end());
  return cache[n];
}

bool RelevanceManager::isRelevant(Node lit)
{
  if (!d_computed)
  {
    computeRelevance();
  }
  if (!d_success)
  {
    // always relevant if we failed to compute
    return true;
  }
  // agnostic to negation
  while (lit.getKind() == NOT)
  {
    lit = lit[0];
  }
  return d_rset.find(lit) != d_rset.end();
}

const std::unordered_set<TNode>& RelevanceManager::getRelevantAssertions(
    bool& success)
{
  if (!d_computed)
  {
    computeRelevance();
  }
  // update success flag
  success = d_success;
  return d_rset;
}

void RelevanceManager::notifyLemma(Node n)
{
  if (d_dman != nullptr)
  {
    // ensure we know which literals are relevant, and why
    computeRelevance();
    d_dman->notifyLemma(d_rsetExp, n);
  }
}

void RelevanceManager::notifyCandidateModel(TheoryModel* m)
{
  if (d_dman != nullptr)
  {
    d_dman->notifyCandidateModel(d_input, m);
  }
}

void RelevanceManager::getDifficultyMap(std::map<Node, Node>& dmap)
{
  if (d_dman != nullptr)
  {
    d_dman->getDifficultyMap(dmap);
  }
}

}  // namespace theory
}  // namespace cvc5
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback