GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/preprocessing/passes/non_clausal_simp.cpp Lines: 216 224 96.4 %
Date: 2021-05-22 Branches: 488 1178 41.4 %

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
9459
NonClausalSimp::Statistics::Statistics()
44
9459
    : d_numConstantProps(smtStatisticsRegistry().registerInt(
45
9459
        "preprocessing::passes::NonClausalSimp::NumConstantProps"))
46
{
47
9459
}
48
49
50
/* -------------------------------------------------------------------------- */
51
52
9459
NonClausalSimp::NonClausalSimp(PreprocessingPassContext* preprocContext)
53
    : PreprocessingPass(preprocContext, "non-clausal-simp"),
54
9459
      d_pnm(preprocContext->getProofNodeManager()),
55
9459
      d_llpg(d_pnm ? new smt::PreprocessProofGenerator(
56
                         d_pnm,
57
3600
                         preprocContext->getUserContext(),
58
3600
                         "NonClausalSimp::llpg")
59
                   : nullptr),
60
9459
      d_llra(d_pnm ? new LazyCDProof(d_pnm,
61
                                     nullptr,
62
3600
                                     preprocContext->getUserContext(),
63
3600
                                     "NonClausalSimp::llra")
64
                   : nullptr),
65
52236
      d_tsubsList(preprocContext->getUserContext())
66
{
67
9459
}
68
69
10277
PreprocessingPassResult NonClausalSimp::applyInternal(
70
    AssertionPipeline* assertionsToPreprocess)
71
{
72
10277
  d_preprocContext->spendResource(Resource::PreprocessStep);
73
74
  theory::booleans::CircuitPropagator* propagator =
75
10277
      d_preprocContext->getCircuitPropagator();
76
77
93255
  for (size_t i = 0, size = assertionsToPreprocess->size(); i < size; ++i)
78
  {
79
165956
    Trace("non-clausal-simplify") << "Assertion #" << i << " : "
80
82978
                                  << (*assertionsToPreprocess)[i] << std::endl;
81
  }
82
83
10277
  if (propagator->getNeedsFinish())
84
  {
85
3705
    propagator->finish();
86
3705
    propagator->setNeedsFinish(false);
87
  }
88
10277
  propagator->initialize();
89
90
  // Assert all the assertions to the propagator
91
10277
  Trace("non-clausal-simplify") << "asserting to propagator" << std::endl;
92
93255
  for (size_t i = 0, size = assertionsToPreprocess->size(); i < size; ++i)
93
  {
94
82978
    Assert(Rewriter::rewrite((*assertionsToPreprocess)[i])
95
           == (*assertionsToPreprocess)[i]);
96
    // Don't reprocess substitutions
97
82978
    if (assertionsToPreprocess->isSubstsIndex(i))
98
    {
99
2591
      continue;
100
    }
101
160774
    Trace("non-clausal-simplify")
102
80387
        << "asserting " << (*assertionsToPreprocess)[i] << std::endl;
103
160774
    Debug("cores") << "propagator->assertTrue: " << (*assertionsToPreprocess)[i]
104
80387
                   << std::endl;
105
80387
    propagator->assertTrue((*assertionsToPreprocess)[i]);
106
  }
107
108
10277
  Trace("non-clausal-simplify") << "propagating" << std::endl;
109
20554
  TrustNode conf = propagator->propagate();
110
10277
  if (!conf.isNull())
111
  {
112
    // If in conflict, just return false
113
1802
    Trace("non-clausal-simplify")
114
901
        << "conflict in non-clausal propagation" << std::endl;
115
901
    assertionsToPreprocess->clear();
116
901
    assertionsToPreprocess->pushBackTrusted(conf);
117
901
    propagator->setNeedsFinish(true);
118
901
    return PreprocessingPassResult::CONFLICT;
119
  }
120
121
18752
  Trace("non-clausal-simplify")
122
18752
      << "Iterate through " << propagator->getLearnedLiterals().size()
123
9376
      << " learned literals." << std::endl;
124
  // No conflict, go through the literals and solve them
125
9376
  context::Context* u = d_preprocContext->getUserContext();
126
9376
  TrustSubstitutionMap& ttls = d_preprocContext->getTopLevelSubstitutions();
127
9376
  CVC5_UNUSED SubstitutionMap& top_level_substs = ttls.get();
128
  // constant propagations
129
  std::shared_ptr<TrustSubstitutionMap> constantPropagations =
130
      std::make_shared<TrustSubstitutionMap>(
131
18752
          u, d_pnm, "NonClausalSimp::cprop", PfRule::PREPROCESS_LEMMA);
132
9376
  SubstitutionMap& cps = constantPropagations->get();
133
  // new substitutions
134
  std::shared_ptr<TrustSubstitutionMap> newSubstitutions =
135
      std::make_shared<TrustSubstitutionMap>(
136
18752
          u, d_pnm, "NonClausalSimp::newSubs", PfRule::PREPROCESS_LEMMA);
137
9376
  SubstitutionMap& nss = newSubstitutions->get();
138
139
9376
  size_t j = 0;
140
9376
  std::vector<TrustNode>& learned_literals = propagator->getLearnedLiterals();
141
  // if proofs are enabled, we will need to track the proofs of learned literals
142
9376
  if (isProofEnabled())
143
  {
144
1755
    d_tsubsList.push_back(constantPropagations);
145
1755
    d_tsubsList.push_back(newSubstitutions);
146
30438
    for (const TrustNode& tll : learned_literals)
147
    {
148
28683
      d_llpg->notifyNewTrustedAssert(tll);
149
    }
150
  }
151
122944
  for (size_t i = 0, size = learned_literals.size(); i < size; ++i)
152
  {
153
    // Simplify the literal we learned wrt previous substitutions
154
226618
    Node learnedLiteral = learned_literals[i].getNode();
155
113721
    Assert(Rewriter::rewrite(learnedLiteral) == learnedLiteral);
156
113721
    Assert(top_level_substs.apply(learnedLiteral) == learnedLiteral);
157
    // process the learned literal with substitutions and const propagations
158
113721
    learnedLiteral = processLearnedLit(
159
        learnedLiteral, newSubstitutions.get(), constantPropagations.get());
160
227442
    Trace("non-clausal-simplify")
161
113721
        << "Process learnedLiteral, after constProp : " << learnedLiteral
162
113721
        << std::endl;
163
    // It might just simplify to a constant
164
113721
    if (learnedLiteral.isConst())
165
    {
166
824
      if (learnedLiteral.getConst<bool>())
167
      {
168
        // If the learned literal simplifies to true, it's redundant
169
671
        continue;
170
      }
171
      else
172
      {
173
        // If the learned literal simplifies to false, we're in conflict
174
306
        Trace("non-clausal-simplify")
175
153
            << "conflict with " << learned_literals[i].getNode() << std::endl;
176
153
        assertionsToPreprocess->clear();
177
306
        Node n = NodeManager::currentNM()->mkConst<bool>(false);
178
153
        assertionsToPreprocess->push_back(n, false, false, d_llpg.get());
179
153
        propagator->setNeedsFinish(true);
180
153
        return PreprocessingPassResult::CONFLICT;
181
      }
182
    }
183
184
    // Solve it with the corresponding theory, possibly adding new
185
    // substitutions to newSubstitutions
186
112897
    Trace("non-clausal-simplify") << "solving " << learnedLiteral << std::endl;
187
188
    TrustNode tlearnedLiteral =
189
225794
        TrustNode::mkTrustLemma(learnedLiteral, d_llpg.get());
190
    Theory::PPAssertStatus solveStatus =
191
338691
        d_preprocContext->getTheoryEngine()->solve(tlearnedLiteral,
192
225794
                                                   *newSubstitutions.get());
193
194
112897
    switch (solveStatus)
195
    {
196
48236
      case Theory::PP_ASSERT_STATUS_SOLVED:
197
      {
198
        // The literal should rewrite to true
199
96472
        Trace("non-clausal-simplify")
200
48236
            << "solved " << learnedLiteral << std::endl;
201
48236
        Assert(Rewriter::rewrite(nss.apply(learnedLiteral)).isConst());
202
        // else fall through
203
48236
        break;
204
      }
205
      case Theory::PP_ASSERT_STATUS_CONFLICT:
206
      {
207
        // If in conflict, we return false
208
        Trace("non-clausal-simplify")
209
            << "conflict while solving " << learnedLiteral << std::endl;
210
        assertionsToPreprocess->clear();
211
        Node n = NodeManager::currentNM()->mkConst<bool>(false);
212
        assertionsToPreprocess->push_back(n);
213
        propagator->setNeedsFinish(true);
214
        return PreprocessingPassResult::CONFLICT;
215
      }
216
64661
      default:
217
129322
        if (learnedLiteral.getKind() == kind::EQUAL
218
129322
            && (learnedLiteral[0].isConst() || learnedLiteral[1].isConst()))
219
        {
220
          // constant propagation
221
11052
          TNode t;
222
11052
          TNode c;
223
5526
          if (learnedLiteral[0].isConst())
224
          {
225
198
            t = learnedLiteral[1];
226
198
            c = learnedLiteral[0];
227
          }
228
          else
229
          {
230
5328
            t = learnedLiteral[0];
231
5328
            c = learnedLiteral[1];
232
          }
233
5526
          Assert(!t.isConst());
234
5526
          Assert(cps.apply(t, true) == t);
235
5526
          Assert(top_level_substs.apply(t) == t);
236
5526
          Assert(nss.apply(t) == t);
237
          // also add to learned literal
238
11052
          ProofGenerator* cpg = constantPropagations->addSubstitutionSolved(
239
5526
              t, c, tlearnedLiteral);
240
          // We need to justify (= t c) as a literal, since it is reasserted
241
          // to the assertion pipeline below. We do this with the proof
242
          // generator returned by the above call.
243
5526
          if (isProofEnabled())
244
          {
245
1440
            d_llpg->notifyNewAssert(t.eqNode(c), cpg);
246
          }
247
        }
248
        else
249
        {
250
          // Keep the literal
251
59135
          learned_literals[j++] = learned_literals[i];
252
        }
253
64661
        break;
254
    }
255
  }
256
257
#ifdef CVC5_ASSERTIONS
258
  // NOTE: When debugging this code, consider moving this check inside of the
259
  // loop over propagator->getLearnedLiterals(). This check has been moved
260
  // outside because it is costly for certain inputs (see bug 508).
261
  //
262
  // Check data structure invariants:
263
  // 1. for each lhs of top_level_substs, does not appear anywhere in rhs of
264
  // top_level_substs or anywhere in constantPropagations
265
  // 2. each lhs of constantPropagations rewrites to itself
266
  // 3. if l -> r is a constant propagation and l is a subterm of l' with l' ->
267
  // r' another constant propagation, then l'[l/r] -> r' should be a
268
  //    constant propagation too
269
  // 4. each lhs of constantPropagations is different from each rhs
270
56864
  for (SubstitutionMap::iterator pos = nss.begin(); pos != nss.end(); ++pos)
271
  {
272
47641
    Assert((*pos).first.isVar());
273
47641
    Assert(top_level_substs.apply((*pos).first) == (*pos).first);
274
47641
    Assert(top_level_substs.apply((*pos).second) == (*pos).second);
275
95282
    Node app = nss.apply((*pos).second);
276
47641
    Assert(nss.apply(app) == app);
277
  }
278
14731
  for (SubstitutionMap::iterator pos = cps.begin(); pos != cps.end(); ++pos)
279
  {
280
5508
    Assert((*pos).second.isConst());
281
5508
    Assert(Rewriter::rewrite((*pos).first) == (*pos).first);
282
5508
    Assert(cps.apply((*pos).second) == (*pos).second);
283
  }
284
#endif /* CVC5_ASSERTIONS */
285
286
  // Resize the learnt
287
18446
  Trace("non-clausal-simplify")
288
9223
      << "Resize non-clausal learned literals to " << j << std::endl;
289
9223
  learned_literals.resize(j);
290
291
18446
  std::unordered_set<TNode> s;
292
87442
  for (size_t i = 0, size = assertionsToPreprocess->size(); i < size; ++i)
293
  {
294
156438
    Node assertion = (*assertionsToPreprocess)[i];
295
156438
    TrustNode assertionNew = newSubstitutions->applyTrusted(assertion);
296
78219
    Trace("non-clausal-simplify") << "assertion = " << assertion << std::endl;
297
78219
    if (!assertionNew.isNull())
298
    {
299
57534
      Trace("non-clausal-simplify")
300
28767
          << "assertionNew = " << assertionNew.getNode() << std::endl;
301
28767
      assertionsToPreprocess->replaceTrusted(i, assertionNew);
302
28767
      assertion = assertionNew.getNode();
303
28767
      Assert(Rewriter::rewrite(assertion) == assertion);
304
    }
305
    for (;;)
306
    {
307
80301
      assertionNew = constantPropagations->applyTrusted(assertion);
308
79260
      if (assertionNew.isNull())
309
      {
310
78219
        break;
311
      }
312
1041
      Assert(assertionNew.getNode() != assertion);
313
1041
      assertionsToPreprocess->replaceTrusted(i, assertionNew);
314
1041
      assertion = assertionNew.getNode();
315
1041
      d_statistics.d_numConstantProps += 1;
316
2082
      Trace("non-clausal-simplify")
317
1041
          << "assertionNew = " << assertion << std::endl;
318
    }
319
78219
    s.insert(assertion);
320
156438
    Trace("non-clausal-simplify")
321
78219
        << "non-clausal preprocessed: " << assertion << std::endl;
322
  }
323
324
  // If necessary, add as assertions if needed (when incremental). This is
325
  // necessary because certain variables cannot truly be eliminated when
326
  // we are in incremental mode. For example, say our first call to check-sat
327
  // is a formula F containing variable x. On the second call to check-sat,
328
  // say we solve a top-level assertion (= x t). Since the solver already has
329
  // constraints involving x, we must still keep (= x t) as an assertion.
330
  // However, notice that we do not retract the substitution { x -> t }. This
331
  // means that all *subsequent* assertions after (= x t) will replace x by t.
332
9223
  if (assertionsToPreprocess->storeSubstsInAsserts())
333
  {
334
5112
    for (const std::pair<const Node, const Node>& pos: nss)
335
    {
336
5448
      Node lhs = pos.first;
337
      // If using incremental, we must check whether this variable has occurred
338
      // before now. If it has, we must add as an assertion.
339
2724
      if (d_preprocContext->getSymsInAssertions().contains(lhs))
340
      {
341
        // if it has, the substitution becomes an assertion
342
2682
        TrustNode trhs = newSubstitutions->applyTrusted(lhs);
343
1341
        Assert(!trhs.isNull());
344
2682
        Trace("non-clausal-simplify")
345
1341
            << "substitute: will notify SAT layer of substitution: "
346
1341
            << trhs.getProven() << std::endl;
347
1341
        assertionsToPreprocess->addSubstitutionNode(trhs.getProven(),
348
                                                    trhs.getGenerator());
349
      }
350
    }
351
  }
352
353
9223
  Assert(assertionsToPreprocess->getRealAssertionsEnd()
354
         <= assertionsToPreprocess->size());
355
  // Learned literals to conjoin. If proofs are enabled, all these are
356
  // justified by d_llpg.
357
18446
  std::vector<Node> learnedLitsToConjoin;
358
359
67802
  for (size_t i = 0; i < learned_literals.size(); ++i)
360
  {
361
96142
    Node learned = learned_literals[i].getNode();
362
58579
    Assert(top_level_substs.apply(learned) == learned);
363
    // process learned literal
364
58579
    learned = processLearnedLit(
365
        learned, newSubstitutions.get(), constantPropagations.get());
366
58579
    if (s.find(learned) != s.end())
367
    {
368
21016
      continue;
369
    }
370
37563
    s.insert(learned);
371
37563
    learnedLitsToConjoin.push_back(learned);
372
75126
    Trace("non-clausal-simplify")
373
37563
        << "non-clausal learned : " << learned << std::endl;
374
  }
375
9223
  learned_literals.clear();
376
377
14731
  for (SubstitutionMap::iterator pos = cps.begin(); pos != cps.end(); ++pos)
378
  {
379
10909
    Node cProp = (*pos).first.eqNode((*pos).second);
380
5508
    Assert(top_level_substs.apply(cProp) == cProp);
381
    // process learned literal (substitutions only)
382
5508
    cProp = processLearnedLit(cProp, newSubstitutions.get(), nullptr);
383
5508
    if (s.find(cProp) != s.end())
384
    {
385
107
      continue;
386
    }
387
5401
    s.insert(cProp);
388
5401
    learnedLitsToConjoin.push_back(cProp);
389
10802
    Trace("non-clausal-simplify")
390
5401
        << "non-clausal constant propagation : " << cProp << std::endl;
391
  }
392
393
  // Add new substitutions to topLevelSubstitutions
394
  // Note that we don't have to keep rhs's in full solved form
395
  // because SubstitutionMap::apply does a fixed-point iteration when
396
  // substituting
397
9223
  ttls.addSubstitutions(*newSubstitutions.get());
398
399
9223
  if (!learnedLitsToConjoin.empty())
400
  {
401
2165
    size_t replIndex = assertionsToPreprocess->getRealAssertionsEnd() - 1;
402
4330
    Node newConj = NodeManager::currentNM()->mkAnd(learnedLitsToConjoin);
403
4330
    Trace("non-clausal-simplify")
404
2165
        << "non-clausal simplification, reassert: " << newConj << std::endl;
405
2165
    ProofGenerator* pg = nullptr;
406
2165
    if (isProofEnabled())
407
    {
408
      // justify in d_llra
409
15352
      for (const Node& lit : learnedLitsToConjoin)
410
      {
411
14928
        d_llra->addLazyStep(lit, d_llpg.get());
412
      }
413
424
      if (learnedLitsToConjoin.size() > 1)
414
      {
415
319
        d_llra->addStep(newConj, PfRule::AND_INTRO, learnedLitsToConjoin, {});
416
319
        pg = d_llra.get();
417
      }
418
      else
419
      {
420
        // otherwise we ask the learned literal proof generator directly
421
105
        pg = d_llpg.get();
422
      }
423
    }
424
    // ------- from d_llpg    --------- from d_llpg
425
    //  conj[0]       ....    d_conj[n]
426
    // -------------------------------- AND_INTRO
427
    //  newConj
428
    // where newConj is conjoined at the given index
429
2165
    assertionsToPreprocess->conjoin(replIndex, newConj, pg);
430
  }
431
432
9223
  propagator->setNeedsFinish(true);
433
434
  // Note that typically ttls.apply(assert)==assert here.
435
  // However, this invariant is invalidated for cases where we use explicit
436
  // equality assertions for variables solved in incremental mode that already
437
  // exist in assertions, as described above.
438
439
9223
  return PreprocessingPassResult::NO_CONFLICT;
440
}
441
442
58158
bool NonClausalSimp::isProofEnabled() const { return d_pnm != nullptr; }
443
444
177808
Node NonClausalSimp::processLearnedLit(Node lit,
445
                                       theory::TrustSubstitutionMap* subs,
446
                                       theory::TrustSubstitutionMap* cp)
447
{
448
355616
  TrustNode tlit;
449
177808
  if (subs != nullptr)
450
  {
451
177808
    tlit = subs->applyTrusted(lit);
452
177808
    if (!tlit.isNull())
453
    {
454
40951
      lit = processRewrittenLearnedLit(tlit);
455
    }
456
355616
    Trace("non-clausal-simplify")
457
177808
        << "Process learnedLiteral, after newSubs : " << lit << std::endl;
458
  }
459
  // apply to fixed point
460
177808
  if (cp != nullptr)
461
  {
462
    for (;;)
463
    {
464
172580
      tlit = cp->applyTrusted(lit);
465
172440
      if (tlit.isNull())
466
      {
467
172300
        break;
468
      }
469
140
      Assert(lit != tlit.getNode());
470
140
      lit = processRewrittenLearnedLit(tlit);
471
140
      d_statistics.d_numConstantProps += 1;
472
    }
473
  }
474
355616
  return lit;
475
}
476
477
41091
Node NonClausalSimp::processRewrittenLearnedLit(theory::TrustNode trn)
478
{
479
41091
  if (isProofEnabled())
480
  {
481
15386
    d_llpg->notifyTrustedPreprocessed(trn);
482
  }
483
  // return the node
484
41091
  return trn.getNode();
485
}
486
487
}  // namespace passes
488
}  // namespace preprocessing
489
28191
}  // namespace cvc5