summaryrefslogtreecommitdiff
path: root/src/theory/relevance_manager.cpp
blob: 9acf2dffc066c943c96283f2f020fd46dccf6f0a (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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/******************************************************************************
 * 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 "expr/term_context_stack.h"
#include "options/smt_options.h"
#include "smt/env.h"

using namespace cvc5::kind;

namespace cvc5 {
namespace theory {

RelevanceManager::RelevanceManager(Env& env, Valuation val)
    : EnvObj(env),
      d_val(val),
      d_input(userContext()),
      d_rset(context()),
      d_inFullEffortCheck(false),
      d_success(false),
      d_trackRSetExp(false),
      d_miniscopeTopLevel(true),
      d_rsetExp(context()),
      d_jcache(context())
{
  if (options().smt.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, bool isInput)
{
  // 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);
  // notify the difficulty manager if these are input assertions
  if (isInput && d_dman != nullptr)
  {
    d_dman->notifyInputAssertions(assertions);
  }
}

void RelevanceManager::notifyPreprocessedAssertion(Node n, bool isInput)
{
  std::vector<Node> toProcess;
  toProcess.push_back(n);
  notifyPreprocessedAssertions(toProcess, isInput);
}

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::beginRound() { d_inFullEffortCheck = true; }

void RelevanceManager::endRound() { d_inFullEffortCheck = false; }

void RelevanceManager::computeRelevance()
{
  // if not at full effort, should be tracking something else, e.g. explanation
  // for why literals are relevant.
  Assert(d_inFullEffortCheck || d_trackRSetExp);
  Trace("rel-manager") << "RelevanceManager::computeRelevance, full effort = "
                       << d_inFullEffortCheck << "..." << std::endl;
  for (const Node& node: d_input)
  {
    TNode n = node;
    int32_t val = justify(n);
    if (val != 1)
    {
      // if we are in full effort check and fail to justify, then we should
      // give a failure and set success to false, or otherwise calls to
      // isRelevant cannot be trusted.
      if (d_inFullEffortCheck)
      {
        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;
        return;
      }
    }
  }
  if (Trace.isOn("rel-manager"))
  {
    if (d_inFullEffortCheck)
    {
      Trace("rel-manager") << "...success (full), size = " << d_rset.size()
                           << std::endl;
    }
    else
    {
      Trace("rel-manager") << "...success, exp size = " << d_rsetExp.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(const RlvPair& cur,
                                              std::vector<int32_t>& childrenJustify)
{
  // 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.first.getNumChildren();
  Assert(isBooleanConnective(cur.first));
  size_t index = childrenJustify.size();
  Assert(index < nchildren);
  Kind k = cur.first.getKind();
  // Lookup the last child's value in the overall cache, we may choose to
  // add this to childrenJustify if we return true.
  RlvPair cp(cur.first[index],
             d_ptctx.computeValue(cur.first, cur.second, index));
  Assert(d_jcache.find(cp) != d_jcache.end());
  int32_t lastChildJustify = d_jcache[cp];
  if (k == NOT)
  {
    d_jcache[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))
      {
        d_jcache[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;
        }
      }
      d_jcache[cur] = ret;
    }
    else
    {
      // continue
      childrenJustify.push_back(lastChildJustify);
      return true;
    }
  }
  else if (lastChildJustify == 0)
  {
    // all other cases, an unknown child implies we are unknown
    d_jcache[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
      d_jcache[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);
      d_jcache[cur] =
          ((k == XOR ? -1 : 1) * lastChildJustify == childrenJustify[0]) ? 1
                                                                         : -1;
    }
  }
  return false;
}

int32_t RelevanceManager::justify(TNode n)
{
  // The set of nodes that we have computed currently have no value. Those
  // that are marked as having no value in d_jcache must be recomputed, since
  // the values for SAT literals may have changed.
  std::unordered_set<RlvPair, RlvPairHashFunction> noJustify;
  // the vector of values of children
  std::unordered_map<RlvPair, std::vector<int32_t>, RlvPairHashFunction>
      childJustify;
  RlvPairIntMap::iterator it;
  std::unordered_map<RlvPair, std::vector<int32_t>, RlvPairHashFunction>::iterator
      itc;
  RlvPair cur;
  TCtxStack visit(&d_ptctx);
  visit.pushInitial(n);
  do
  {
    cur = visit.getCurrent();
    // should always have Boolean type
    Assert(cur.first.getType().isBoolean());
    it = d_jcache.find(cur);
    if (it != d_jcache.end())
    {
      if (it->second != 0 || noJustify.find(cur) != noJustify.end())
      {
        visit.pop();
        // 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.first))
      {
        // initialize its children justify vector as empty
        childJustify[cur].clear();
        // start with the first child
        visit.pushChild(cur.first, cur.second, 0);
      }
      else
      {
        visit.pop();
        // 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.first, value))
        {
          ret = value ? 1 : -1;
          bool hasPol, pol;
          PolarityTermContext::getFlags(cur.second, hasPol, pol);
          // relevant if weakly matches polarity
          if (!hasPol || pol == value)
          {
            d_rset.insert(cur.first);
            if (d_trackRSetExp)
            {
              d_rsetExp[cur.first] = n;
              Trace("rel-manager-exp")
                  << "Reason for " << cur.first << " is " << n
                  << ", polarity is " << hasPol << "/" << pol << std::endl;
            }
          }
        }
        d_jcache[cur] = ret;
        if (ret == 0)
        {
          noJustify.insert(cur);
        }
      }
    }
    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))
      {
        Assert(itc->second.size() < cur.first.getNumChildren());
        visit.pushChild(cur.first, cur.second, itc->second.size());
      }
      else
      {
        visit.pop();
        Assert(d_jcache.find(cur) != d_jcache.end());
        if (d_jcache[cur] == 0)
        {
          noJustify.insert(cur);
        }
      }
    }
  } while (!visit.empty());
  RlvPair ci(n, d_ptctx.initialValue());
  Assert(d_jcache.find(ci) != d_jcache.end());
  return d_jcache[ci];
}

bool RelevanceManager::isRelevant(Node lit)
{
  Assert(d_inFullEffortCheck);
  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();
}

std::unordered_set<TNode> RelevanceManager::getRelevantAssertions(bool& success)
{
  computeRelevance();
  // update success flag
  success = d_success;
  std::unordered_set<TNode> rset;
  if (success)
  {
    for (const Node& a : d_rset)
    {
      rset.insert(a);
    }
  }
  return rset;
}

void RelevanceManager::notifyLemma(Node n)
{
  // notice that we may be in FULL or STANDARD effort here.
  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(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