GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/prop/sat_proof_manager.cpp Lines: 311 430 72.3 %
Date: 2021-09-10 Branches: 629 2076 30.3 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Haniel Barbosa, Andrew Reynolds
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
 * Implementation of the proof manager for Minisat.
14
 */
15
16
#include "prop/sat_proof_manager.h"
17
18
#include "options/proof_options.h"
19
#include "proof/proof_node_algorithm.h"
20
#include "proof/theory_proof_step_buffer.h"
21
#include "prop/cnf_stream.h"
22
#include "prop/minisat/minisat.h"
23
24
namespace cvc5 {
25
namespace prop {
26
27
1249
SatProofManager::SatProofManager(Minisat::Solver* solver,
28
                                 CnfStream* cnfStream,
29
                                 context::UserContext* userContext,
30
1249
                                 ProofNodeManager* pnm)
31
    : d_solver(solver),
32
      d_cnfStream(cnfStream),
33
      d_pnm(pnm),
34
      d_resChains(pnm, true, userContext),
35
      d_resChainPg(userContext, pnm),
36
      d_assumptions(userContext),
37
1249
      d_conflictLit(undefSatVariable)
38
{
39
1249
  d_true = NodeManager::currentNM()->mkConst(true);
40
1249
  d_false = NodeManager::currentNM()->mkConst(false);
41
1249
}
42
43
void SatProofManager::printClause(const Minisat::Clause& clause)
44
{
45
  for (unsigned i = 0, size = clause.size(); i < size; ++i)
46
  {
47
    SatLiteral satLit = MinisatSatSolver::toSatLiteral(clause[i]);
48
    Trace("sat-proof") << satLit << " ";
49
  }
50
}
51
52
84283
Node SatProofManager::getClauseNode(SatLiteral satLit)
53
{
54
84283
  Assert(d_cnfStream->getNodeCache().find(satLit)
55
         != d_cnfStream->getNodeCache().end())
56
      << "SatProofManager::getClauseNode: literal " << satLit
57
      << " undefined.\n";
58
84283
  return d_cnfStream->getNodeCache()[satLit];
59
}
60
61
481941
Node SatProofManager::getClauseNode(const Minisat::Clause& clause)
62
{
63
963882
  std::vector<Node> clauseNodes;
64
2798728
  for (unsigned i = 0, size = clause.size(); i < size; ++i)
65
  {
66
2316787
    SatLiteral satLit = MinisatSatSolver::toSatLiteral(clause[i]);
67
2316787
    Assert(d_cnfStream->getNodeCache().find(satLit)
68
           != d_cnfStream->getNodeCache().end())
69
        << "SatProofManager::getClauseNode: literal " << satLit
70
        << " undefined\n";
71
2316787
    clauseNodes.push_back(d_cnfStream->getNodeCache()[satLit]);
72
  }
73
  // order children by node id
74
481941
  std::sort(clauseNodes.begin(), clauseNodes.end());
75
963882
  return NodeManager::currentNM()->mkNode(kind::OR, clauseNodes);
76
}
77
78
23843
void SatProofManager::startResChain(const Minisat::Clause& start)
79
{
80
23843
  if (Trace.isOn("sat-proof"))
81
  {
82
    Trace("sat-proof") << "SatProofManager::startResChain: ";
83
    printClause(start);
84
    Trace("sat-proof") << "\n";
85
  }
86
23843
  d_resLinks.emplace_back(getClauseNode(start), Node::null(), true);
87
23843
}
88
89
179998
void SatProofManager::addResolutionStep(Minisat::Lit lit, bool redundant)
90
{
91
179998
  SatLiteral satLit = MinisatSatSolver::toSatLiteral(lit);
92
359996
  Node litNode = d_cnfStream->getNodeCache()[satLit];
93
179998
  bool negated = satLit.isNegated();
94
179998
  Assert(!negated || litNode.getKind() == kind::NOT);
95
179998
  if (!redundant)
96
  {
97
288218
    Trace("sat-proof") << "SatProofManager::addResolutionStep: {"
98
288218
                       << satLit.isNegated() << "} [" << satLit << "] "
99
144109
                       << ~satLit << "\n";
100
    // if lit is negated then the chain resolution construction will use it as a
101
    // pivot occurring as is in the second clause and the node under the
102
    // negation in the first clause
103
288218
    d_resLinks.emplace_back(d_cnfStream->getNodeCache()[~satLit],
104
288218
                            negated ? litNode[0] : litNode,
105
432327
                            !satLit.isNegated());
106
  }
107
  else
108
  {
109
71778
    Trace("sat-proof") << "SatProofManager::addResolutionStep: redundant lit "
110
35889
                       << satLit << " stored\n";
111
35889
    d_redundantLits.push_back(satLit);
112
  }
113
179998
}
114
115
293062
void SatProofManager::addResolutionStep(const Minisat::Clause& clause,
116
                                        Minisat::Lit lit)
117
{
118
293062
  SatLiteral satLit = MinisatSatSolver::toSatLiteral(lit);
119
586124
  Node litNode = d_cnfStream->getNodeCache()[satLit];
120
293062
  bool negated = satLit.isNegated();
121
293062
  Assert(!negated || litNode.getKind() == kind::NOT);
122
586124
  Node clauseNode = getClauseNode(clause);
123
  // if lit is negative then the chain resolution construction will use it as a
124
  // pivot occurring as is in the second clause and the node under the
125
  // negation in the first clause, which means that the third argument of the
126
  // tuple must be false
127
293062
  d_resLinks.emplace_back(clauseNode, negated ? litNode[0] : litNode, negated);
128
293062
  if (Trace.isOn("sat-proof"))
129
  {
130
    Trace("sat-proof") << "SatProofManager::addResolutionStep: {"
131
                       << satLit.isNegated() << "} [" << ~satLit << "] ";
132
    printClause(clause);
133
    Trace("sat-proof") << "\nSatProofManager::addResolutionStep:\t"
134
                       << clauseNode << "\n";
135
  }
136
293062
}
137
138
2821
void SatProofManager::endResChain(Minisat::Lit lit)
139
{
140
2821
  SatLiteral satLit = MinisatSatSolver::toSatLiteral(lit);
141
5642
  Trace("sat-proof") << "SatProofManager::endResChain: chain_res for "
142
2821
                     << satLit;
143
2821
  endResChain(getClauseNode(satLit), {satLit});
144
2821
}
145
146
21022
void SatProofManager::endResChain(const Minisat::Clause& clause)
147
{
148
21022
  if (Trace.isOn("sat-proof"))
149
  {
150
    Trace("sat-proof") << "SatProofManager::endResChain: chain_res for ";
151
    printClause(clause);
152
  }
153
42044
  std::set<SatLiteral> clauseLits;
154
257523
  for (unsigned i = 0, size = clause.size(); i < size; ++i)
155
  {
156
236501
    clauseLits.insert(MinisatSatSolver::toSatLiteral(clause[i]));
157
  }
158
21022
  endResChain(getClauseNode(clause), clauseLits);
159
21022
}
160
161
23843
void SatProofManager::endResChain(Node conclusion,
162
                                  const std::set<SatLiteral>& conclusionLits)
163
{
164
23843
  Trace("sat-proof") << ", " << conclusion << "\n";
165
23843
  if (d_resChains.hasGenerator(conclusion))
166
  {
167
870
    Trace("sat-proof")
168
435
        << "SatProofManager::endResChain: skip repeated proof of " << conclusion
169
435
        << "\n";
170
    // clearing
171
435
    d_resLinks.clear();
172
435
    d_redundantLits.clear();
173
435
    return;
174
  }
175
  // first process redundant literals
176
46679
  std::set<SatLiteral> visited;
177
23408
  unsigned pos = d_resLinks.size();
178
58646
  for (SatLiteral satLit : d_redundantLits)
179
  {
180
35238
    processRedundantLit(satLit, conclusionLits, visited, pos);
181
  }
182
23408
  d_redundantLits.clear();
183
  // build resolution chain
184
  // the conclusion is stored already in the arguments because of the
185
  // possibility of reordering
186
46679
  std::vector<Node> children, args{conclusion};
187
580651
  for (unsigned i = 0, size = d_resLinks.size(); i < size; ++i)
188
  {
189
1114486
    Node clause, pivot;
190
    bool posFirst;
191
557243
    std::tie(clause, pivot, posFirst) = d_resLinks[i];
192
557243
    children.push_back(clause);
193
557243
    Trace("sat-proof") << "SatProofManager::endResChain:   ";
194
557243
    if (i > 0)
195
    {
196
1067670
      Trace("sat-proof") << "{" << posFirst << "} ["
197
533835
                         << d_cnfStream->getTranslationCache()[pivot] << "] ";
198
    }
199
    // special case for clause (or l1 ... ln) being a single literal
200
    // corresponding itself to a clause, which is indicated by the pivot being
201
    // of the form (not (or l1 ... ln))
202
1114486
    if (clause.getKind() == kind::OR
203
1671729
        && !(pivot.getKind() == kind::NOT && pivot[0].getKind() == kind::OR
204
557243
             && pivot[0] == clause))
205
    {
206
2199541
      for (unsigned j = 0, sizeJ = clause.getNumChildren(); j < sizeJ; ++j)
207
      {
208
1811725
        Trace("sat-proof") << d_cnfStream->getTranslationCache()[clause[j]];
209
1811725
        if (j < sizeJ - 1)
210
        {
211
1423909
          Trace("sat-proof") << ", ";
212
        }
213
      }
214
    }
215
    else
216
    {
217
338854
      Assert(d_cnfStream->getTranslationCache().find(clause)
218
             != d_cnfStream->getTranslationCache().end())
219
169427
          << "clause node " << clause
220
169427
          << " treated as unit has no literal. Pivot is " << pivot << "\n";
221
169427
      Trace("sat-proof") << d_cnfStream->getTranslationCache()[clause];
222
    }
223
557243
    Trace("sat-proof") << " : ";
224
557243
    if (i > 0)
225
    {
226
533835
      args.push_back(posFirst ? d_true : d_false);
227
533835
      args.push_back(pivot);
228
533835
      Trace("sat-proof") << "{" << posFirst << "} [" << pivot << "] ";
229
    }
230
557243
    Trace("sat-proof") << clause << "\n";
231
  }
232
  // clearing
233
23408
  d_resLinks.clear();
234
  // whether no-op
235
23408
  if (children.size() == 1)
236
  {
237
4
    Trace("sat-proof") << "SatProofManager::endResChain: no-op. The conclusion "
238
2
                       << conclusion << " is set-equal to premise "
239
2
                       << children[0] << "\n";
240
2
    return;
241
  }
242
  // whether trivial cycle
243
580242
  for (const Node& child : children)
244
  {
245
556971
    if (conclusion == child)
246
    {
247
270
      Trace("sat-proof")
248
135
          << "SatProofManager::endResChain: no-op. The conclusion "
249
135
          << conclusion << " is equal to a premise\n";
250
135
      return;
251
    }
252
  }
253
  // since the conclusion can be both reordered and without duplicates and the
254
  // SAT solver does not record this information, we use a MACRO_RESOLUTION
255
  // step, which bypasses these. Note that we could generate a chain resolution
256
  // rule here by explicitly computing the detailed steps, but leave this for
257
  // post-processing.
258
46542
  ProofStep ps(PfRule::MACRO_RESOLUTION_TRUST, children, args);
259
  // note that we must tell the proof generator to overwrite if repeated
260
23271
  d_resChainPg.addStep(conclusion, ps);
261
  // the premises of this resolution may not have been justified yet, so we do
262
  // not pass assumptions to check closedness
263
23271
  d_resChains.addLazyStep(conclusion, &d_resChainPg);
264
}
265
266
141301
void SatProofManager::processRedundantLit(
267
    SatLiteral lit,
268
    const std::set<SatLiteral>& conclusionLits,
269
    std::set<SatLiteral>& visited,
270
    unsigned pos)
271
{
272
282602
  Trace("sat-proof") << push
273
141301
                     << "SatProofManager::processRedundantLit: Lit: " << lit
274
141301
                     << "\n";
275
141301
  if (visited.count(lit))
276
  {
277
34613
    Trace("sat-proof") << "already visited\n" << pop;
278
97337
    return;
279
  }
280
  Minisat::Solver::TCRef reasonRef =
281
106688
      d_solver->reason(Minisat::var(MinisatSatSolver::toMinisatLit(lit)));
282
106688
  if (reasonRef == Minisat::Solver::TCRef_Undef)
283
  {
284
56222
    Trace("sat-proof") << "unit, add link to lit " << lit << " at pos: " << pos
285
28111
                       << "\n"
286
28111
                       << pop;
287
28111
    visited.insert(lit);
288
56222
    Node litNode = d_cnfStream->getNodeCache()[lit];
289
28111
    bool negated = lit.isNegated();
290
28111
    Assert(!negated || litNode.getKind() == kind::NOT);
291
292
84333
    d_resLinks.emplace(d_resLinks.begin() + pos,
293
56222
                       d_cnfStream->getNodeCache()[~lit],
294
56222
                       negated ? litNode[0] : litNode,
295
140555
                       !negated);
296
28111
    return;
297
  }
298
78577
  Assert(reasonRef >= 0 && reasonRef < d_solver->ca.size())
299
      << "reasonRef " << reasonRef << " and d_satSolver->ca.size() "
300
      << d_solver->ca.size() << "\n";
301
78577
  const Minisat::Clause& reason = d_solver->ca[reasonRef];
302
78577
  if (Trace.isOn("sat-proof"))
303
  {
304
    Trace("sat-proof") << "reason: ";
305
    printClause(reason);
306
    Trace("sat-proof") << "\n";
307
  }
308
  // Since processRedundantLit calls can reallocate memory in the SAT solver due
309
  // to explaining stuff, we directly get the literals and the clause node here
310
157154
  std::vector<SatLiteral> toProcess;
311
257311
  for (unsigned i = 1, size = reason.size(); i < size; ++i)
312
  {
313
178734
    toProcess.push_back(MinisatSatSolver::toSatLiteral(reason[i]));
314
  }
315
157154
  Node clauseNode = getClauseNode(reason);
316
    // check if redundant literals in the reason. The first literal is the one we
317
  // will be eliminating, so we check the others
318
257311
  for (unsigned i = 0, size = toProcess.size(); i < size; ++i)
319
  {
320
178734
    SatLiteral satLit = toProcess[i];
321
    // if literal does not occur in the conclusion we process it as well
322
178734
    if (!conclusionLits.count(satLit))
323
    {
324
106063
      processRedundantLit(satLit, conclusionLits, visited, pos);
325
    }
326
  }
327
78577
  Assert(!visited.count(lit));
328
78577
  visited.insert(lit);
329
157154
  Trace("sat-proof") << "clause, add link to lit " << lit << " at pos: " << pos
330
78577
                     << "\n"
331
78577
                     << pop;
332
  // add the step before steps for children. Note that the step is with the
333
  // reason, not only with ~lit, since the learned clause is built under the
334
  // assumption that the redundant literal is removed via the resolution with
335
  // the explanation of its negation
336
157154
  Node litNode = d_cnfStream->getNodeCache()[lit];
337
78577
  bool negated = lit.isNegated();
338
78577
  Assert(!negated || litNode.getKind() == kind::NOT);
339
235731
  d_resLinks.emplace(d_resLinks.begin() + pos,
340
                     clauseNode,
341
157154
                     negated ? litNode[0] : litNode,
342
314308
                     !negated);
343
}
344
345
33737
void SatProofManager::explainLit(SatLiteral lit,
346
                                 std::unordered_set<TNode>& premises)
347
{
348
33737
  Trace("sat-proof") << push << "SatProofManager::explainLit: Lit: " << lit;
349
51801
  Node litNode = getClauseNode(lit);
350
33737
  Trace("sat-proof") << " [" << litNode << "]\n";
351
  // Note that if we had two literals for (= a b) and (= b a) and we had already
352
  // a proof for (= a b) this test would return true for (= b a), which could
353
  // lead to open proof. However we should never have two literals like this in
354
  // the SAT solver since they'd be rewritten to the same one
355
33737
  if (d_resChainPg.hasProofFor(litNode))
356
  {
357
20546
    Trace("sat-proof") << "SatProofManager::explainLit: already justified "
358
10273
                       << lit << ", ABORT\n"
359
10273
                       << pop;
360
10273
    return;
361
  }
362
  Minisat::Solver::TCRef reasonRef =
363
23464
      d_solver->reason(Minisat::var(MinisatSatSolver::toMinisatLit(lit)));
364
23464
  if (reasonRef == Minisat::Solver::TCRef_Undef)
365
  {
366
5398
    Trace("sat-proof") << "SatProofManager::explainLit: no SAT reason\n" << pop;
367
5398
    return;
368
  }
369
18066
  Assert(reasonRef >= 0 && reasonRef < d_solver->ca.size())
370
      << "reasonRef " << reasonRef << " and d_satSolver->ca.size() "
371
      << d_solver->ca.size() << "\n";
372
18066
  const Minisat::Clause& reason = d_solver->ca[reasonRef];
373
18066
  unsigned size = reason.size();
374
18066
  if (Trace.isOn("sat-proof"))
375
  {
376
    Trace("sat-proof") << "SatProofManager::explainLit: with clause: ";
377
    printClause(reason);
378
    Trace("sat-proof") << "\n";
379
  }
380
#ifdef CVC5_ASSERTIONS
381
  // pedantically check that the negation of the literal to explain *does not*
382
  // occur in the reason, otherwise we will loop forever
383
64594
  for (unsigned i = 0; i < size; ++i)
384
  {
385
46528
    AlwaysAssert(~MinisatSatSolver::toSatLiteral(reason[i]) != lit)
386
        << "cyclic justification\n";
387
  }
388
#endif
389
  // add the reason clause first
390
36130
  std::vector<Node> children{getClauseNode(reason)}, args;
391
  // save in the premises
392
18066
  premises.insert(children.back());
393
  // Since explainLit calls can reallocate memory in the
394
  // SAT solver, we directly get the literals we need to explain so we no
395
  // longer depend on the reference to reason
396
36130
  std::vector<Node> toExplain{children.back().begin(), children.back().end()};
397
18066
  Trace("sat-proof") << push;
398
64594
  for (unsigned i = 0; i < size; ++i)
399
  {
400
#ifdef CVC5_ASSERTIONS
401
    // pedantically make sure that the reason stays the same
402
46528
    const Minisat::Clause& reloadedReason = d_solver->ca[reasonRef];
403
46528
    AlwaysAssert(size == static_cast<unsigned>(reloadedReason.size()));
404
46528
    AlwaysAssert(children[0] == getClauseNode(reloadedReason));
405
#endif
406
46528
    SatLiteral currLit = d_cnfStream->getTranslationCache()[toExplain[i]];
407
    // ignore the lit we are trying to explain...
408
46528
    if (currLit == lit)
409
    {
410
18066
      continue;
411
    }
412
56924
    std::unordered_set<TNode> childPremises;
413
28462
    explainLit(~currLit, childPremises);
414
    // save to resolution chain premises / arguments
415
28462
    Assert(d_cnfStream->getNodeCache().find(currLit)
416
           != d_cnfStream->getNodeCache().end());
417
28462
    children.push_back(d_cnfStream->getNodeCache()[~currLit]);
418
56924
    Node currLitNode = d_cnfStream->getNodeCache()[currLit];
419
28462
    bool negated = currLit.isNegated();
420
28462
    Assert(!negated || currLitNode.getKind() == kind::NOT);
421
    // note this is the opposite of what is done in addResolutionStep. This is
422
    // because here the clause, which contains the literal being analyzed, is
423
    // the first clause rather than the second
424
28462
    args.push_back(!negated ? d_true : d_false);
425
28462
    args.push_back(negated ? currLitNode[0] : currLitNode);
426
    // add child premises and the child itself
427
28462
    premises.insert(childPremises.begin(), childPremises.end());
428
28462
    premises.insert(d_cnfStream->getNodeCache()[~currLit]);
429
  }
430
18066
  if (Trace.isOn("sat-proof"))
431
  {
432
    Trace("sat-proof") << pop << "SatProofManager::explainLit: chain_res for "
433
                       << lit << ", " << litNode << " with clauses:\n";
434
    for (unsigned i = 0, csize = children.size(); i < csize; ++i)
435
    {
436
      Trace("sat-proof") << "SatProofManager::explainLit:   " << children[i];
437
      if (i > 0)
438
      {
439
        Trace("sat-proof") << " [" << args[i - 1] << "]";
440
      }
441
      Trace("sat-proof") << "\n";
442
    }
443
  }
444
  // if justification of children contains the expected conclusion, avoid the
445
  // cyclic proof by aborting.
446
18066
  if (premises.count(litNode))
447
  {
448
4
    Trace("sat-proof") << "SatProofManager::explainLit: CYCLIC PROOF of " << lit
449
2
                       << " [" << litNode << "], ABORT\n"
450
2
                       << pop;
451
2
    return;
452
  }
453
18064
  Trace("sat-proof") << pop;
454
  // create step
455
18064
  args.insert(args.begin(), litNode);
456
36128
  ProofStep ps(PfRule::MACRO_RESOLUTION_TRUST, children, args);
457
18064
  d_resChainPg.addStep(litNode, ps);
458
  // the premises in the limit of the justification may correspond to other
459
  // links in the chain which have, themselves, literals yet to be justified. So
460
  // we are not ready yet to check closedness w.r.t. CNF transformation of the
461
  // preprocessed assertions
462
18064
  d_resChains.addLazyStep(litNode, &d_resChainPg);
463
}
464
465
1360
void SatProofManager::finalizeProof(Node inConflictNode,
466
                                    const std::vector<SatLiteral>& inConflict)
467
{
468
2720
  Trace("sat-proof")
469
1360
      << "SatProofManager::finalizeProof: conflicting clause node: "
470
1360
      << inConflictNode << "\n";
471
  // nothing to do
472
1360
  if (inConflictNode == d_false)
473
  {
474
414
    return;
475
  }
476
946
  if (Trace.isOn("sat-proof-debug2"))
477
  {
478
    Trace("sat-proof-debug2")
479
        << push << "SatProofManager::finalizeProof: saved proofs in chain:\n";
480
    std::map<Node, std::shared_ptr<ProofNode>> links = d_resChains.getLinks();
481
    std::unordered_set<Node> skip;
482
    for (const std::pair<const Node, std::shared_ptr<ProofNode>>& link : links)
483
    {
484
      if (skip.count(link.first))
485
      {
486
        continue;
487
      }
488
      auto it = d_cnfStream->getTranslationCache().find(link.first);
489
      if (it != d_cnfStream->getTranslationCache().end())
490
      {
491
        Trace("sat-proof-debug2")
492
            << "SatProofManager::finalizeProof:  " << it->second;
493
      }
494
      // a refl step added due to double elim negation, ignore
495
      else if (link.second->getRule() == PfRule::REFL)
496
      {
497
        continue;
498
      }
499
      // a clause
500
      else
501
      {
502
        Trace("sat-proof-debug2") << "SatProofManager::finalizeProof:";
503
        Assert(link.first.getKind() == kind::OR) << link.first;
504
        for (const Node& n : link.first)
505
        {
506
          it = d_cnfStream->getTranslationCache().find(n);
507
          Assert(it != d_cnfStream->getTranslationCache().end());
508
          Trace("sat-proof-debug2") << it->second << " ";
509
        }
510
      }
511
      Trace("sat-proof-debug2") << "\n";
512
      Trace("sat-proof-debug2")
513
          << "SatProofManager::finalizeProof: " << link.first << "\n";
514
      // get resolution
515
      Node cur = link.first;
516
      std::shared_ptr<ProofNode> pfn = link.second;
517
      while (pfn->getRule() != PfRule::MACRO_RESOLUTION_TRUST)
518
      {
519
        Assert(pfn->getChildren().size() == 1
520
               && pfn->getChildren()[0]->getRule() == PfRule::ASSUME)
521
            << *link.second.get() << "\n"
522
            << *pfn.get();
523
        cur = pfn->getChildren()[0]->getResult();
524
        // retrieve justification of assumption in the links
525
        Assert(links.find(cur) != links.end());
526
        pfn = links[cur];
527
        // ignore it in the rest of the outside loop
528
        skip.insert(cur);
529
      }
530
      std::vector<Node> fassumps;
531
      expr::getFreeAssumptions(pfn.get(), fassumps);
532
      Trace("sat-proof-debug2") << push;
533
      for (const Node& fa : fassumps)
534
      {
535
        Trace("sat-proof-debug2") << "SatProofManager::finalizeProof:   - ";
536
        it = d_cnfStream->getTranslationCache().find(fa);
537
        if (it != d_cnfStream->getTranslationCache().end())
538
        {
539
          Trace("sat-proof-debug2") << it->second << "\n";
540
          continue;
541
        }
542
        // then it's a clause
543
        Assert(fa.getKind() == kind::OR);
544
        for (const Node& n : fa)
545
        {
546
          it = d_cnfStream->getTranslationCache().find(n);
547
          Assert(it != d_cnfStream->getTranslationCache().end());
548
          Trace("sat-proof-debug2") << it->second << " ";
549
        }
550
        Trace("sat-proof-debug2") << "\n";
551
      }
552
      Trace("sat-proof-debug2") << pop;
553
      Trace("sat-proof-debug2")
554
          << "SatProofManager::finalizeProof:  " << *pfn.get() << "\n=======\n";
555
      ;
556
    }
557
    Trace("sat-proof-debug2") << pop;
558
  }
559
  // We will resolve away of the literals l_1...l_n in inConflict. At this point
560
  // each ~l_i must be either explainable, the result of a previously saved
561
  // resolution chain, or an input. In account of it possibly being the first,
562
  // we call explainLit on each ~l_i while accumulating the children and
563
  // arguments for the resolution step to conclude false.
564
1892
  std::vector<Node> children{inConflictNode}, args;
565
1892
  std::unordered_set<TNode> premises;
566
4546
  for (unsigned i = 0, size = inConflict.size(); i < size; ++i)
567
  {
568
3600
    Assert(d_cnfStream->getNodeCache().find(inConflict[i])
569
           != d_cnfStream->getNodeCache().end());
570
7200
    std::unordered_set<TNode> childPremises;
571
3600
    explainLit(~inConflict[i], childPremises);
572
7200
    Node negatedLitNode = d_cnfStream->getNodeCache()[~inConflict[i]];
573
    // save to resolution chain premises / arguments
574
3600
    children.push_back(negatedLitNode);
575
7200
    Node litNode = d_cnfStream->getNodeCache()[inConflict[i]];
576
3600
    bool negated = inConflict[i].isNegated();
577
3600
    Assert(!negated || litNode.getKind() == kind::NOT);
578
    // note this is the opposite of what is done in addResolutionStep. This is
579
    // because here the clause, which contains the literal being analyzed, is
580
    // the first clause rather than the second
581
3600
    args.push_back(!negated ? d_true : d_false);
582
3600
    args.push_back(negated ? litNode[0] : litNode);
583
    // add child premises and the child itself
584
3600
    premises.insert(childPremises.begin(), childPremises.end());
585
3600
    premises.insert(negatedLitNode);
586
3600
    Trace("sat-proof") << "===========\n";
587
  }
588
946
  if (Trace.isOn("sat-proof"))
589
  {
590
    Trace("sat-proof") << "SatProofManager::finalizeProof: chain_res for false "
591
                          "with clauses:\n";
592
    for (unsigned i = 0, size = children.size(); i < size; ++i)
593
    {
594
      Trace("sat-proof") << "SatProofManager::finalizeProof:   " << children[i];
595
      if (i > 0)
596
      {
597
        Trace("sat-proof") << " [" << args[i - 1] << "]";
598
      }
599
      Trace("sat-proof") << "\n";
600
    }
601
  }
602
  // create step
603
946
  args.insert(args.begin(), d_false);
604
1892
  ProofStep ps(PfRule::MACRO_RESOLUTION_TRUST, children, args);
605
946
  d_resChainPg.addStep(d_false, ps);
606
  // not yet ready to check closedness because maybe only now we will justify
607
  // literals used in resolutions
608
946
  d_resChains.addLazyStep(d_false, &d_resChainPg);
609
  // Fix point justification of literals in leaves of the proof of false
610
  bool expanded;
611
1199
  do
612
  {
613
1199
    expanded = false;
614
1199
    Trace("sat-proof") << "expand assumptions to prove false\n";
615
2398
    std::shared_ptr<ProofNode> pfn = d_resChains.getProofFor(d_false);
616
1199
    Assert(pfn);
617
1199
    Trace("sat-proof-debug") << "sat proof of flase: " << *pfn.get() << "\n";
618
2398
    std::vector<Node> fassumps;
619
1199
    expr::getFreeAssumptions(pfn.get(), fassumps);
620
1199
    if (Trace.isOn("sat-proof"))
621
    {
622
      for (const Node& fa : fassumps)
623
      {
624
        Trace("sat-proof") << "- ";
625
        auto it = d_cnfStream->getTranslationCache().find(fa);
626
        if (it != d_cnfStream->getTranslationCache().end())
627
        {
628
          Trace("sat-proof") << it->second << "\n";
629
          Trace("sat-proof") << "- " << fa << "\n";
630
          continue;
631
        }
632
        // then it's a clause
633
        Assert(fa.getKind() == kind::OR);
634
        for (const Node& n : fa)
635
        {
636
          it = d_cnfStream->getTranslationCache().find(n);
637
          Assert(it != d_cnfStream->getTranslationCache().end());
638
          Trace("sat-proof") << it->second << " ";
639
        }
640
        Trace("sat-proof") << "\n";
641
        Trace("sat-proof") << "- " << fa << "\n";
642
      }
643
    }
644
645
    // for each assumption, see if it has a reason
646
109402
    for (const Node& fa : fassumps)
647
    {
648
      // ignore already processed assumptions
649
136992
      if (premises.count(fa))
650
      {
651
28789
        Trace("sat-proof") << "already processed assumption " << fa << "\n";
652
135317
        continue;
653
      }
654
      // ignore input assumptions. This is necessary to avoid rare collisions
655
      // between input clauses and literals that are equivalent at the node
656
      // level. In trying to justify the literal below if, it was previously
657
      // propagated (say, in a previous check-sat call that survived the
658
      // user-context changes) but no longer holds, then we may introduce a
659
      // bogus proof for it, rather than keeping it as an input.
660
157153
      if (d_assumptions.contains(fa))
661
      {
662
77739
        Trace("sat-proof") << "input assumption " << fa << "\n";
663
77739
        continue;
664
      }
665
      // ignore non-literals
666
1675
      auto it = d_cnfStream->getTranslationCache().find(fa);
667
1675
      if (it == d_cnfStream->getTranslationCache().end())
668
      {
669
        Trace("sat-proof") << "no lit assumption " << fa << "\n";
670
        premises.insert(fa);
671
        continue;
672
      }
673
3350
      Trace("sat-proof") << "lit assumption (" << it->second << "), " << fa
674
1675
                         << "\n";
675
      // mark another iteration for the loop, as some resolution link may be
676
      // connected because of the new justifications
677
1675
      expanded = true;
678
3350
      std::unordered_set<TNode> childPremises;
679
1675
      explainLit(it->second, childPremises);
680
      // add the premises used in the justification. We know they will have
681
      // been as expanded as possible
682
1675
      premises.insert(childPremises.begin(), childPremises.end());
683
      // add free assumption itself
684
1675
      premises.insert(fa);
685
    }
686
  } while (expanded);
687
  // now we should be able to close it
688
946
  if (options::proofCheck() == options::ProofCheckMode::EAGER)
689
  {
690
    std::vector<Node> assumptionsVec;
691
    for (const Node& a : d_assumptions)
692
    {
693
      assumptionsVec.push_back(a);
694
    }
695
    d_resChains.addLazyStep(d_false, &d_resChainPg, assumptionsVec);
696
  }
697
}
698
699
48
void SatProofManager::storeUnitConflict(Minisat::Lit inConflict)
700
{
701
48
  Assert(d_conflictLit == undefSatVariable);
702
48
  d_conflictLit = MinisatSatSolver::toSatLiteral(inConflict);
703
48
}
704
705
48
void SatProofManager::finalizeProof()
706
{
707
48
  Assert(d_conflictLit != undefSatVariable);
708
96
  Trace("sat-proof")
709
48
      << "SatProofManager::finalizeProof: conflicting (lazy) satLit: "
710
48
      << d_conflictLit << "\n";
711
48
  finalizeProof(getClauseNode(d_conflictLit), {d_conflictLit});
712
48
}
713
714
469
void SatProofManager::finalizeProof(Minisat::Lit inConflict, bool adding)
715
{
716
469
  SatLiteral satLit = MinisatSatSolver::toSatLiteral(inConflict);
717
938
  Trace("sat-proof") << "SatProofManager::finalizeProof: conflicting satLit: "
718
469
                     << satLit << "\n";
719
938
  Node clauseNode = getClauseNode(satLit);
720
469
  if (adding)
721
  {
722
469
    registerSatAssumptions({clauseNode});
723
  }
724
469
  finalizeProof(clauseNode, {satLit});
725
469
}
726
727
843
void SatProofManager::finalizeProof(const Minisat::Clause& inConflict,
728
                                    bool adding)
729
{
730
843
  if (Trace.isOn("sat-proof"))
731
  {
732
    Trace("sat-proof")
733
        << "SatProofManager::finalizeProof: conflicting clause: ";
734
    printClause(inConflict);
735
    Trace("sat-proof") << "\n";
736
  }
737
1686
  std::vector<SatLiteral> clause;
738
4340
  for (unsigned i = 0, size = inConflict.size(); i < size; ++i)
739
  {
740
3497
    clause.push_back(MinisatSatSolver::toSatLiteral(inConflict[i]));
741
  }
742
1686
  Node clauseNode = getClauseNode(inConflict);
743
843
  if (adding)
744
  {
745
19
    registerSatAssumptions({clauseNode});
746
  }
747
843
  finalizeProof(clauseNode, clause);
748
843
}
749
750
2823
std::shared_ptr<ProofNode> SatProofManager::getProof()
751
{
752
2823
  std::shared_ptr<ProofNode> pfn = d_resChains.getProofFor(d_false);
753
2823
  if (!pfn)
754
  {
755
864
    pfn = d_pnm->mkAssume(d_false);
756
  }
757
2823
  return pfn;
758
}
759
760
23604
void SatProofManager::registerSatLitAssumption(Minisat::Lit lit)
761
{
762
47208
  Trace("sat-proof") << "SatProofManager::registerSatLitAssumption: - "
763
47208
                     << getClauseNode(MinisatSatSolver::toSatLiteral(lit))
764
23604
                     << "\n";
765
23604
  d_assumptions.insert(getClauseNode(MinisatSatSolver::toSatLiteral(lit)));
766
23604
}
767
768
626878
void SatProofManager::registerSatAssumptions(const std::vector<Node>& assumps)
769
{
770
1253756
  for (const Node& a : assumps)
771
  {
772
1253756
    Trace("sat-proof") << "SatProofManager::registerSatAssumptions: - " << a
773
626878
                       << "\n";
774
626878
    d_assumptions.insert(a);
775
  }
776
626878
}
777
778
}  // namespace prop
779
29502
}  // namespace cvc5