GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/preprocessing/passes/non_clausal_simp.cpp Lines: 214 222 96.4 %
Date: 2021-09-29 Branches: 491 1182 41.5 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Aina Niemetz, Andrew Reynolds, Gereon Kremer
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
 * Non-clausal simplification preprocessing pass.
14
 *
15
 * Run the nonclausal solver and try to solve all assigned theory literals.
16
 */
17
18
#include "preprocessing/passes/non_clausal_simp.h"
19
20
#include <vector>
21
22
#include "context/cdo.h"
23
#include "options/smt_options.h"
24
#include "preprocessing/assertion_pipeline.h"
25
#include "preprocessing/preprocessing_pass_context.h"
26
#include "smt/preprocess_proof_generator.h"
27
#include "smt/smt_statistics_registry.h"
28
#include "theory/booleans/circuit_propagator.h"
29
#include "theory/theory.h"
30
#include "theory/theory_engine.h"
31
#include "theory/theory_model.h"
32
#include "theory/trust_substitutions.h"
33
34
using namespace cvc5;
35
using namespace cvc5::theory;
36
37
namespace cvc5 {
38
namespace preprocessing {
39
namespace passes {
40
41
/* -------------------------------------------------------------------------- */
42
43
6271
NonClausalSimp::Statistics::Statistics(StatisticsRegistry& reg)
44
    : d_numConstantProps(reg.registerInt(
45
6271
        "preprocessing::passes::NonClausalSimp::NumConstantProps"))
46
{
47
6271
}
48
49
50
/* -------------------------------------------------------------------------- */
51
52
6271
NonClausalSimp::NonClausalSimp(PreprocessingPassContext* preprocContext)
53
    : PreprocessingPass(preprocContext, "non-clausal-simp"),
54
      d_statistics(statisticsRegistry()),
55
6271
      d_pnm(preprocContext->getProofNodeManager()),
56
6271
      d_llpg(d_pnm ? new smt::PreprocessProofGenerator(
57
286
                 d_pnm, userContext(), "NonClausalSimp::llpg")
58
                   : nullptr),
59
6271
      d_llra(d_pnm ? new LazyCDProof(
60
286
                 d_pnm, nullptr, userContext(), "NonClausalSimp::llra")
61
                   : nullptr),
62
25656
      d_tsubsList(userContext())
63
{
64
6271
}
65
66
8955
PreprocessingPassResult NonClausalSimp::applyInternal(
67
    AssertionPipeline* assertionsToPreprocess)
68
{
69
8955
  d_preprocContext->spendResource(Resource::PreprocessStep);
70
71
  theory::booleans::CircuitPropagator* propagator =
72
8955
      d_preprocContext->getCircuitPropagator();
73
74
74932
  for (size_t i = 0, size = assertionsToPreprocess->size(); i < size; ++i)
75
  {
76
131954
    Trace("non-clausal-simplify") << "Assertion #" << i << " : "
77
65977
                                  << (*assertionsToPreprocess)[i] << std::endl;
78
  }
79
80
8955
  if (propagator->getNeedsFinish())
81
  {
82
3304
    propagator->finish();
83
3304
    propagator->setNeedsFinish(false);
84
  }
85
8955
  propagator->initialize();
86
87
  // Assert all the assertions to the propagator
88
8955
  Trace("non-clausal-simplify") << "asserting to propagator" << std::endl;
89
74932
  for (size_t i = 0, size = assertionsToPreprocess->size(); i < size; ++i)
90
  {
91
65977
    Assert(rewrite((*assertionsToPreprocess)[i])
92
           == (*assertionsToPreprocess)[i]);
93
    // Don't reprocess substitutions
94
65977
    if (assertionsToPreprocess->isSubstsIndex(i))
95
    {
96
2366
      continue;
97
    }
98
127222
    Trace("non-clausal-simplify")
99
63611
        << "asserting " << (*assertionsToPreprocess)[i] << std::endl;
100
127222
    Debug("cores") << "propagator->assertTrue: " << (*assertionsToPreprocess)[i]
101
63611
                   << std::endl;
102
63611
    propagator->assertTrue((*assertionsToPreprocess)[i]);
103
  }
104
105
8955
  Trace("non-clausal-simplify") << "propagating" << std::endl;
106
17910
  TrustNode conf = propagator->propagate();
107
8955
  if (!conf.isNull())
108
  {
109
    // If in conflict, just return false
110
1232
    Trace("non-clausal-simplify")
111
616
        << "conflict in non-clausal propagation" << std::endl;
112
616
    assertionsToPreprocess->clear();
113
616
    assertionsToPreprocess->pushBackTrusted(conf);
114
616
    propagator->setNeedsFinish(true);
115
616
    return PreprocessingPassResult::CONFLICT;
116
  }
117
118
16678
  Trace("non-clausal-simplify")
119
16678
      << "Iterate through " << propagator->getLearnedLiterals().size()
120
8339
      << " learned literals." << std::endl;
121
  // No conflict, go through the literals and solve them
122
8339
  context::Context* u = userContext();
123
8339
  TrustSubstitutionMap& ttls = d_preprocContext->getTopLevelSubstitutions();
124
8339
  CVC5_UNUSED SubstitutionMap& top_level_substs = ttls.get();
125
  // constant propagations
126
  std::shared_ptr<TrustSubstitutionMap> constantPropagations =
127
      std::make_shared<TrustSubstitutionMap>(
128
16678
          u, d_pnm, "NonClausalSimp::cprop", PfRule::PREPROCESS_LEMMA);
129
8339
  SubstitutionMap& cps = constantPropagations->get();
130
  // new substitutions
131
  std::shared_ptr<TrustSubstitutionMap> newSubstitutions =
132
      std::make_shared<TrustSubstitutionMap>(
133
16678
          u, d_pnm, "NonClausalSimp::newSubs", PfRule::PREPROCESS_LEMMA);
134
8339
  SubstitutionMap& nss = newSubstitutions->get();
135
136
8339
  size_t j = 0;
137
8339
  std::vector<TrustNode>& learned_literals = propagator->getLearnedLiterals();
138
  // if proofs are enabled, we will need to track the proofs of learned literals
139
8339
  if (isProofEnabled())
140
  {
141
59
    d_tsubsList.push_back(constantPropagations);
142
59
    d_tsubsList.push_back(newSubstitutions);
143
990
    for (const TrustNode& tll : learned_literals)
144
    {
145
931
      d_llpg->notifyNewTrustedAssert(tll);
146
    }
147
  }
148
95419
  for (size_t i = 0, size = learned_literals.size(); i < size; ++i)
149
  {
150
    // Simplify the literal we learned wrt previous substitutions
151
173859
    Node learnedLiteral = learned_literals[i].getNode();
152
87184
    Assert(rewrite(learnedLiteral) == learnedLiteral);
153
87184
    Assert(top_level_substs.apply(learnedLiteral) == learnedLiteral);
154
    // process the learned literal with substitutions and const propagations
155
87184
    learnedLiteral = processLearnedLit(
156
        learnedLiteral, newSubstitutions.get(), constantPropagations.get());
157
174368
    Trace("non-clausal-simplify")
158
87184
        << "Process learnedLiteral, after constProp : " << learnedLiteral
159
87184
        << std::endl;
160
    // It might just simplify to a constant
161
87184
    if (learnedLiteral.isConst())
162
    {
163
509
      if (learnedLiteral.getConst<bool>())
164
      {
165
        // If the learned literal simplifies to true, it's redundant
166
405
        continue;
167
      }
168
      else
169
      {
170
        // If the learned literal simplifies to false, we're in conflict
171
208
        Trace("non-clausal-simplify")
172
104
            << "conflict with " << learned_literals[i].getNode() << std::endl;
173
104
        assertionsToPreprocess->clear();
174
208
        Node n = NodeManager::currentNM()->mkConst<bool>(false);
175
104
        assertionsToPreprocess->push_back(n, false, false, d_llpg.get());
176
104
        propagator->setNeedsFinish(true);
177
104
        return PreprocessingPassResult::CONFLICT;
178
      }
179
    }
180
181
    // Solve it with the corresponding theory, possibly adding new
182
    // substitutions to newSubstitutions
183
86675
    Trace("non-clausal-simplify") << "solving " << learnedLiteral << std::endl;
184
185
    TrustNode tlearnedLiteral =
186
173350
        TrustNode::mkTrustLemma(learnedLiteral, d_llpg.get());
187
    Theory::PPAssertStatus solveStatus =
188
260025
        d_preprocContext->getTheoryEngine()->solve(tlearnedLiteral,
189
173350
                                                   *newSubstitutions.get());
190
191
86675
    switch (solveStatus)
192
    {
193
43544
      case Theory::PP_ASSERT_STATUS_SOLVED:
194
      {
195
        // The literal should rewrite to true
196
87088
        Trace("non-clausal-simplify")
197
43544
            << "solved " << learnedLiteral << std::endl;
198
43544
        Assert(rewrite(nss.apply(learnedLiteral)).isConst());
199
        // else fall through
200
43544
        break;
201
      }
202
      case Theory::PP_ASSERT_STATUS_CONFLICT:
203
      {
204
        // If in conflict, we return false
205
        Trace("non-clausal-simplify")
206
            << "conflict while solving " << learnedLiteral << std::endl;
207
        assertionsToPreprocess->clear();
208
        Node n = NodeManager::currentNM()->mkConst<bool>(false);
209
        assertionsToPreprocess->push_back(n);
210
        propagator->setNeedsFinish(true);
211
        return PreprocessingPassResult::CONFLICT;
212
      }
213
43131
      default:
214
86262
        if (learnedLiteral.getKind() == kind::EQUAL
215
86262
            && (learnedLiteral[0].isConst() || learnedLiteral[1].isConst()))
216
        {
217
          // constant propagation
218
8236
          TNode t;
219
8236
          TNode c;
220
4118
          if (learnedLiteral[0].isConst())
221
          {
222
154
            t = learnedLiteral[1];
223
154
            c = learnedLiteral[0];
224
          }
225
          else
226
          {
227
3964
            t = learnedLiteral[0];
228
3964
            c = learnedLiteral[1];
229
          }
230
4118
          Assert(!t.isConst());
231
4118
          Assert(cps.apply(t, true) == t);
232
4118
          Assert(top_level_substs.apply(t) == t);
233
4118
          Assert(nss.apply(t) == t);
234
          // also add to learned literal
235
8236
          ProofGenerator* cpg = constantPropagations->addSubstitutionSolved(
236
4118
              t, c, tlearnedLiteral);
237
          // We need to justify (= t c) as a literal, since it is reasserted
238
          // to the assertion pipeline below. We do this with the proof
239
          // generator returned by the above call.
240
4118
          if (isProofEnabled())
241
          {
242
6
            d_llpg->notifyNewAssert(t.eqNode(c), cpg);
243
          }
244
        }
245
        else
246
        {
247
          // Keep the literal
248
39013
          learned_literals[j++] = learned_literals[i];
249
          // Its a literal that could not be processed as a substitution or
250
          // conflict. In this case, we notify the context of the learned
251
          // literal, which will process it with the learned literal manager.
252
39013
          d_preprocContext->notifyLearnedLiteral(learnedLiteral);
253
        }
254
43131
        break;
255
    }
256
  }
257
258
#ifdef CVC5_ASSERTIONS
259
  // NOTE: When debugging this code, consider moving this check inside of the
260
  // loop over propagator->getLearnedLiterals(). This check has been moved
261
  // outside because it is costly for certain inputs (see bug 508).
262
  //
263
  // Check data structure invariants:
264
  // 1. for each lhs of top_level_substs, does not appear anywhere in rhs of
265
  // top_level_substs or anywhere in constantPropagations
266
  // 2. each lhs of constantPropagations rewrites to itself
267
  // 3. if l -> r is a constant propagation and l is a subterm of l' with l' ->
268
  // r' another constant propagation, then l'[l/r] -> r' should be a
269
  //    constant propagation too
270
  // 4. each lhs of constantPropagations is different from each rhs
271
51423
  for (SubstitutionMap::iterator pos = nss.begin(); pos != nss.end(); ++pos)
272
  {
273
43188
    Assert((*pos).first.isVar());
274
43188
    Assert(top_level_substs.apply((*pos).first) == (*pos).first);
275
43188
    Assert(top_level_substs.apply((*pos).second) == (*pos).second);
276
86376
    Node app = nss.apply((*pos).second);
277
43188
    Assert(nss.apply(app) == app);
278
  }
279
12337
  for (SubstitutionMap::iterator pos = cps.begin(); pos != cps.end(); ++pos)
280
  {
281
4102
    Assert((*pos).second.isConst());
282
4102
    Assert(rewrite((*pos).first) == (*pos).first);
283
4102
    Assert(cps.apply((*pos).second) == (*pos).second);
284
  }
285
#endif /* CVC5_ASSERTIONS */
286
287
  // Resize the learnt
288
16470
  Trace("non-clausal-simplify")
289
8235
      << "Resize non-clausal learned literals to " << j << std::endl;
290
8235
  learned_literals.resize(j);
291
292
16470
  std::unordered_set<TNode> s;
293
71210
  for (size_t i = 0, size = assertionsToPreprocess->size(); i < size; ++i)
294
  {
295
125950
    Node assertion = (*assertionsToPreprocess)[i];
296
125950
    TrustNode assertionNew = newSubstitutions->applyTrusted(assertion);
297
62975
    Trace("non-clausal-simplify") << "assertion = " << assertion << std::endl;
298
62975
    if (!assertionNew.isNull())
299
    {
300
49404
      Trace("non-clausal-simplify")
301
24702
          << "assertionNew = " << assertionNew.getNode() << std::endl;
302
24702
      assertionsToPreprocess->replaceTrusted(i, assertionNew);
303
24702
      assertion = assertionNew.getNode();
304
24702
      Assert(rewrite(assertion) == assertion);
305
    }
306
    for (;;)
307
    {
308
64761
      assertionNew = constantPropagations->applyTrusted(assertion);
309
63868
      if (assertionNew.isNull())
310
      {
311
62975
        break;
312
      }
313
893
      Assert(assertionNew.getNode() != assertion);
314
893
      assertionsToPreprocess->replaceTrusted(i, assertionNew);
315
893
      assertion = assertionNew.getNode();
316
893
      d_statistics.d_numConstantProps += 1;
317
1786
      Trace("non-clausal-simplify")
318
893
          << "assertionNew = " << assertion << std::endl;
319
    }
320
62975
    s.insert(assertion);
321
125950
    Trace("non-clausal-simplify")
322
62975
        << "non-clausal preprocessed: " << assertion << std::endl;
323
  }
324
325
  // If necessary, add as assertions if needed (when incremental). This is
326
  // necessary because certain variables cannot truly be eliminated when
327
  // we are in incremental mode. For example, say our first call to check-sat
328
  // is a formula F containing variable x. On the second call to check-sat,
329
  // say we solve a top-level assertion (= x t). Since the solver already has
330
  // constraints involving x, we must still keep (= x t) as an assertion.
331
  // However, notice that we do not retract the substitution { x -> t }. This
332
  // means that all *subsequent* assertions after (= x t) will replace x by t.
333
8235
  if (assertionsToPreprocess->storeSubstsInAsserts())
334
  {
335
4130
    for (const std::pair<const Node, const Node>& pos: nss)
336
    {
337
3810
      Node lhs = pos.first;
338
      // If using incremental, we must check whether this variable has occurred
339
      // before now. If it has, we must add as an assertion.
340
1905
      if (d_preprocContext->getSymsInAssertions().contains(lhs))
341
      {
342
        // if it has, the substitution becomes an assertion
343
1886
        TrustNode trhs = newSubstitutions->applyTrusted(lhs);
344
943
        Assert(!trhs.isNull());
345
1886
        Trace("non-clausal-simplify")
346
943
            << "substitute: will notify SAT layer of substitution: "
347
943
            << trhs.getProven() << std::endl;
348
943
        assertionsToPreprocess->addSubstitutionNode(trhs.getProven(),
349
                                                    trhs.getGenerator());
350
      }
351
    }
352
  }
353
354
8235
  Assert(assertionsToPreprocess->getRealAssertionsEnd()
355
         <= assertionsToPreprocess->size());
356
  // Learned literals to conjoin. If proofs are enabled, all these are
357
  // justified by d_llpg.
358
16470
  std::vector<Node> learnedLitsToConjoin;
359
360
46946
  for (size_t i = 0; i < learned_literals.size(); ++i)
361
  {
362
63545
    Node learned = learned_literals[i].getNode();
363
38711
    Assert(top_level_substs.apply(learned) == learned);
364
    // process learned literal
365
38711
    learned = processLearnedLit(
366
        learned, newSubstitutions.get(), constantPropagations.get());
367
38711
    if (s.find(learned) != s.end())
368
    {
369
13877
      continue;
370
    }
371
24834
    s.insert(learned);
372
24834
    learnedLitsToConjoin.push_back(learned);
373
49668
    Trace("non-clausal-simplify")
374
24834
        << "non-clausal learned : " << learned << std::endl;
375
  }
376
8235
  learned_literals.clear();
377
378
12337
  for (SubstitutionMap::iterator pos = cps.begin(); pos != cps.end(); ++pos)
379
  {
380
8134
    Node cProp = (*pos).first.eqNode((*pos).second);
381
4102
    Assert(top_level_substs.apply(cProp) == cProp);
382
    // process learned literal (substitutions only)
383
4102
    cProp = processLearnedLit(cProp, newSubstitutions.get(), nullptr);
384
4102
    if (s.find(cProp) != s.end())
385
    {
386
70
      continue;
387
    }
388
4032
    s.insert(cProp);
389
4032
    learnedLitsToConjoin.push_back(cProp);
390
8064
    Trace("non-clausal-simplify")
391
4032
        << "non-clausal constant propagation : " << cProp << std::endl;
392
  }
393
394
  // Add new substitutions to topLevelSubstitutions
395
  // Note that we don't have to keep rhs's in full solved form
396
  // because SubstitutionMap::apply does a fixed-point iteration when
397
  // substituting
398
8235
  ttls.addSubstitutions(*newSubstitutions.get());
399
400
8235
  if (!learnedLitsToConjoin.empty())
401
  {
402
2100
    size_t replIndex = assertionsToPreprocess->getRealAssertionsEnd() - 1;
403
4200
    Node newConj = NodeManager::currentNM()->mkAnd(learnedLitsToConjoin);
404
4200
    Trace("non-clausal-simplify")
405
2100
        << "non-clausal simplification, reassert: " << newConj << std::endl;
406
2100
    ProofGenerator* pg = nullptr;
407
2100
    if (isProofEnabled())
408
    {
409
      // justify in d_llra
410
74
      for (const Node& lit : learnedLitsToConjoin)
411
      {
412
59
        d_llra->addLazyStep(lit, d_llpg.get());
413
      }
414
15
      if (learnedLitsToConjoin.size() > 1)
415
      {
416
13
        d_llra->addStep(newConj, PfRule::AND_INTRO, learnedLitsToConjoin, {});
417
13
        pg = d_llra.get();
418
      }
419
      else
420
      {
421
        // otherwise we ask the learned literal proof generator directly
422
2
        pg = d_llpg.get();
423
      }
424
    }
425
    // ------- from d_llpg    --------- from d_llpg
426
    //  conj[0]       ....    d_conj[n]
427
    // -------------------------------- AND_INTRO
428
    //  newConj
429
    // where newConj is conjoined at the given index
430
2100
    assertionsToPreprocess->conjoin(replIndex, newConj, pg);
431
  }
432
433
8235
  propagator->setNeedsFinish(true);
434
435
  // Note that typically ttls.apply(assert)==assert here.
436
  // However, this invariant is invalidated for cases where we use explicit
437
  // equality assertions for variables solved in incremental mode that already
438
  // exist in assertions, as described above.
439
440
8235
  return PreprocessingPassResult::NO_CONFLICT;
441
}
442
443
40849
bool NonClausalSimp::isProofEnabled() const { return d_pnm != nullptr; }
444
445
129997
Node NonClausalSimp::processLearnedLit(Node lit,
446
                                       theory::TrustSubstitutionMap* subs,
447
                                       theory::TrustSubstitutionMap* cp)
448
{
449
259994
  TrustNode tlit;
450
129997
  if (subs != nullptr)
451
  {
452
129997
    tlit = subs->applyTrusted(lit);
453
129997
    if (!tlit.isNull())
454
    {
455
26183
      lit = processRewrittenLearnedLit(tlit);
456
    }
457
259994
    Trace("non-clausal-simplify")
458
129997
        << "Process learnedLiteral, after newSubs : " << lit << std::endl;
459
  }
460
  // apply to fixed point
461
129997
  if (cp != nullptr)
462
  {
463
    for (;;)
464
    {
465
126113
      tlit = cp->applyTrusted(lit);
466
126004
      if (tlit.isNull())
467
      {
468
125895
        break;
469
      }
470
109
      Assert(lit != tlit.getNode());
471
109
      lit = processRewrittenLearnedLit(tlit);
472
109
      d_statistics.d_numConstantProps += 1;
473
    }
474
  }
475
259994
  return lit;
476
}
477
478
26292
Node NonClausalSimp::processRewrittenLearnedLit(TrustNode trn)
479
{
480
26292
  if (isProofEnabled())
481
  {
482
632
    d_llpg->notifyTrustedPreprocessed(trn);
483
  }
484
  // return the node
485
26292
  return trn.getNode();
486
}
487
488
}  // namespace passes
489
}  // namespace preprocessing
490
22746
}  // namespace cvc5