GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/smt/proof_manager.cpp Lines: 71 137 51.8 %
Date: 2021-09-29 Branches: 97 514 18.9 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Haniel Barbosa, Diego Della Rocca de Camargos
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
 * The proof manager of the SMT engine.
14
 */
15
16
#include "smt/proof_manager.h"
17
18
#include "options/base_options.h"
19
#include "options/main_options.h"
20
#include "options/proof_options.h"
21
#include "options/smt_options.h"
22
#include "proof/alethe/alethe_node_converter.h"
23
#include "proof/alethe/alethe_post_processor.h"
24
#include "proof/dot/dot_printer.h"
25
#include "proof/proof_checker.h"
26
#include "proof/proof_node_algorithm.h"
27
#include "proof/proof_node_manager.h"
28
#include "smt/assertions.h"
29
#include "smt/difficulty_post_processor.h"
30
#include "smt/env.h"
31
#include "smt/preprocess_proof_generator.h"
32
#include "smt/proof_post_processor.h"
33
34
namespace cvc5 {
35
namespace smt {
36
37
143
PfManager::PfManager(Env& env)
38
    : EnvObj(env),
39
      d_pchecker(new ProofChecker(
40
143
          options().proof.proofCheck == options::ProofCheckMode::EAGER,
41
286
          options().proof.proofPedantic)),
42
143
      d_pnm(new ProofNodeManager(d_pchecker.get())),
43
      d_pppg(new PreprocessProofGenerator(
44
286
          d_pnm.get(), env.getUserContext(), "smt::PreprocessProofGenerator")),
45
      d_pfpp(nullptr),
46
858
      d_finalProof(nullptr)
47
{
48
  // enable proof support in the environment/rewriter
49
143
  d_env.setProofNodeManager(d_pnm.get());
50
  // Now, initialize the proof postprocessor with the environment.
51
  // By default the post-processor will update all assumptions, which
52
  // can lead to SCOPE subproofs of the form
53
  //   A
54
  //  ...
55
  //   B1    B2
56
  //  ...   ...
57
  // ------------
58
  //      C
59
  // ------------- SCOPE [B1, B2]
60
  // B1 ^ B2 => C
61
  //
62
  // where A is an available assumption from outside the scope (note
63
  // that B1 was an assumption of this SCOPE subproof but since it could
64
  // be inferred from A, it was updated). This shape is problematic for
65
  // the Alethe reconstruction, so we disable the update of scoped
66
  // assumptions (which would disable the update of B1 in this case).
67
286
  d_pfpp.reset(new ProofPostproccess(
68
      env,
69
143
      d_pppg.get(),
70
      nullptr,
71
143
      options::proofFormatMode() != options::ProofFormatMode::ALETHE));
72
73
  // add rules to eliminate here
74
143
  if (options::proofGranularityMode() != options::ProofGranularityMode::OFF)
75
  {
76
11
    d_pfpp->setEliminateRule(PfRule::MACRO_SR_EQ_INTRO);
77
11
    d_pfpp->setEliminateRule(PfRule::MACRO_SR_PRED_INTRO);
78
11
    d_pfpp->setEliminateRule(PfRule::MACRO_SR_PRED_ELIM);
79
11
    d_pfpp->setEliminateRule(PfRule::MACRO_SR_PRED_TRANSFORM);
80
11
    d_pfpp->setEliminateRule(PfRule::MACRO_RESOLUTION_TRUST);
81
11
    d_pfpp->setEliminateRule(PfRule::MACRO_RESOLUTION);
82
11
    d_pfpp->setEliminateRule(PfRule::MACRO_ARITH_SCALE_SUM_UB);
83
22
    if (options::proofGranularityMode()
84
11
        != options::ProofGranularityMode::REWRITE)
85
    {
86
11
      d_pfpp->setEliminateRule(PfRule::SUBS);
87
11
      d_pfpp->setEliminateRule(PfRule::REWRITE);
88
22
      if (options::proofGranularityMode()
89
11
          != options::ProofGranularityMode::THEORY_REWRITE)
90
      {
91
        // this eliminates theory rewriting steps with finer-grained DSL rules
92
        d_pfpp->setEliminateRule(PfRule::THEORY_REWRITE);
93
      }
94
    }
95
    // theory-specific lazy proof reconstruction
96
11
    d_pfpp->setEliminateRule(PfRule::STRING_INFERENCE);
97
11
    d_pfpp->setEliminateRule(PfRule::BV_BITBLAST);
98
  }
99
143
  d_false = NodeManager::currentNM()->mkConst(false);
100
143
}
101
102
286
PfManager::~PfManager() {}
103
104
69
void PfManager::setFinalProof(std::shared_ptr<ProofNode> pfn, Assertions& as)
105
{
106
  // Note this assumes that setFinalProof is only called once per unsat
107
  // response. This method would need to cache its result otherwise.
108
69
  Trace("smt-proof") << "SmtEngine::setFinalProof(): get proof body...\n";
109
110
69
  if (Trace.isOn("smt-proof-debug"))
111
  {
112
    Trace("smt-proof-debug")
113
        << "SmtEngine::setFinalProof(): Proof node for false:\n";
114
    Trace("smt-proof-debug") << *pfn.get() << std::endl;
115
    Trace("smt-proof-debug") << "=====" << std::endl;
116
  }
117
118
138
  std::vector<Node> assertions;
119
69
  getAssertions(as, assertions);
120
121
69
  if (Trace.isOn("smt-proof"))
122
  {
123
    Trace("smt-proof") << "SmtEngine::setFinalProof(): get free assumptions..."
124
                       << std::endl;
125
    std::vector<Node> fassumps;
126
    expr::getFreeAssumptions(pfn.get(), fassumps);
127
    Trace("smt-proof")
128
        << "SmtEngine::setFinalProof(): initial free assumptions are:\n";
129
    for (const Node& a : fassumps)
130
    {
131
      Trace("smt-proof") << "- " << a << std::endl;
132
    }
133
134
    Trace("smt-proof") << "SmtEngine::setFinalProof(): assertions are:\n";
135
    for (const Node& n : assertions)
136
    {
137
      Trace("smt-proof") << "- " << n << std::endl;
138
    }
139
    Trace("smt-proof") << "=====" << std::endl;
140
  }
141
142
69
  Trace("smt-proof") << "SmtEngine::setFinalProof(): postprocess...\n";
143
69
  Assert(d_pfpp != nullptr);
144
69
  d_pfpp->process(pfn);
145
146
69
  Trace("smt-proof") << "SmtEngine::setFinalProof(): make scope...\n";
147
148
  // Now make the final scope, which ensures that the only open leaves of the
149
  // proof are the assertions.
150
69
  d_finalProof = d_pnm->mkScope(pfn, assertions);
151
69
  Trace("smt-proof") << "SmtEngine::setFinalProof(): finished.\n";
152
69
}
153
154
5
void PfManager::printProof(std::ostream& out,
155
                           std::shared_ptr<ProofNode> pfn,
156
                           Assertions& as)
157
{
158
5
  Trace("smt-proof") << "PfManager::printProof: start" << std::endl;
159
10
  std::shared_ptr<ProofNode> fp = getFinalProof(pfn, as);
160
  // if we are in incremental mode, we don't want to invalidate the proof
161
  // nodes in fp, since these may be reused in further check-sat calls
162
10
  if (options::incrementalSolving()
163
5
      && options::proofFormatMode() != options::ProofFormatMode::NONE)
164
  {
165
    fp = d_pnm->clone(fp);
166
  }
167
  // TODO (proj #37) according to the proof format, post process the proof node
168
  // TODO (proj #37) according to the proof format, print the proof node
169
170
  // according to the proof format, post process and print the proof node
171
5
  if (options::proofFormatMode() == options::ProofFormatMode::DOT)
172
  {
173
    proof::DotPrinter dotPrinter;
174
    dotPrinter.print(out, fp.get());
175
  }
176
5
  else if (options::proofFormatMode() == options::ProofFormatMode::ALETHE)
177
  {
178
    proof::AletheNodeConverter anc;
179
    proof::AletheProofPostprocess vpfpp(d_pnm.get(), anc);
180
    vpfpp.process(fp);
181
  }
182
5
  else if (options::proofFormatMode() == options::ProofFormatMode::TPTP)
183
  {
184
    out << "% SZS output start Proof for " << options().driver.filename
185
        << std::endl;
186
    // TODO (proj #37) print in TPTP compliant format
187
    out << *fp << std::endl;
188
    out << "% SZS output end Proof for " << options().driver.filename
189
        << std::endl;
190
  }
191
  else
192
  {
193
    // otherwise, print using default printer
194
5
    out << "(proof\n";
195
5
    out << *fp;
196
5
    out << "\n)\n";
197
  }
198
5
}
199
void PfManager::checkProof(std::shared_ptr<ProofNode> pfn, Assertions& as)
200
{
201
  Trace("smt-proof") << "PfManager::checkProof: start" << std::endl;
202
  std::shared_ptr<ProofNode> fp = getFinalProof(pfn, as);
203
  Trace("smt-proof-debug") << "PfManager::checkProof: returned " << *fp.get()
204
                           << std::endl;
205
}
206
207
4
void PfManager::translateDifficultyMap(std::map<Node, Node>& dmap,
208
                                       Assertions& as)
209
{
210
4
  Trace("difficulty") << "PfManager::translateDifficultyMap" << std::endl;
211
4
  if (dmap.empty())
212
  {
213
4
    return;
214
  }
215
  std::map<Node, Node> dmapp = dmap;
216
  dmap.clear();
217
  std::vector<Node> ppAsserts;
218
  for (const std::pair<const Node, Node>& ppa : dmapp)
219
  {
220
    Trace("difficulty") << "  preprocess difficulty: " << ppa.second << " for "
221
                        << ppa.first << std::endl;
222
    ppAsserts.push_back(ppa.first);
223
  }
224
  // assume a SAT refutation from all input assertions that were marked
225
  // as having a difficulty
226
  CDProof cdp(d_pnm.get());
227
  Node fnode = NodeManager::currentNM()->mkConst(false);
228
  cdp.addStep(fnode, PfRule::SAT_REFUTATION, ppAsserts, {});
229
  std::shared_ptr<ProofNode> pf = cdp.getProofFor(fnode);
230
  std::shared_ptr<ProofNode> fpf = getFinalProof(pf, as);
231
  Trace("difficulty-debug") << "Final proof is " << *fpf.get() << std::endl;
232
  Assert(fpf->getRule() == PfRule::SCOPE);
233
  fpf = fpf->getChildren()[0];
234
  // analyze proof
235
  Assert(fpf->getRule() == PfRule::SAT_REFUTATION);
236
  const std::vector<std::shared_ptr<ProofNode>>& children = fpf->getChildren();
237
  DifficultyPostprocessCallback dpc;
238
  ProofNodeUpdater dpnu(d_pnm.get(), dpc);
239
  // For each child of SAT_REFUTATION, we increment the difficulty on all
240
  // "source" free assumptions (see DifficultyPostprocessCallback) by the
241
  // difficulty of the preprocessed assertion.
242
  for (const std::shared_ptr<ProofNode>& c : children)
243
  {
244
    Node res = c->getResult();
245
    Assert(dmapp.find(res) != dmapp.end());
246
    Trace("difficulty-debug") << "  process: " << res << std::endl;
247
    Trace("difficulty-debug") << "  .dvalue: " << dmapp[res] << std::endl;
248
    Trace("difficulty-debug") << "  ..proof: " << *c.get() << std::endl;
249
    if (!dpc.setCurrentDifficulty(dmapp[res]))
250
    {
251
      continue;
252
    }
253
    dpnu.process(c);
254
  }
255
  // get the accumulated difficulty map from the callback
256
  dpc.getDifficultyMap(dmap);
257
}
258
259
ProofChecker* PfManager::getProofChecker() const { return d_pchecker.get(); }
260
261
ProofNodeManager* PfManager::getProofNodeManager() const { return d_pnm.get(); }
262
263
rewriter::RewriteDb* PfManager::getRewriteDatabase() const { return nullptr; }
264
265
143
smt::PreprocessProofGenerator* PfManager::getPreprocessProofGenerator() const
266
{
267
143
  return d_pppg.get();
268
}
269
270
69
std::shared_ptr<ProofNode> PfManager::getFinalProof(
271
    std::shared_ptr<ProofNode> pfn, Assertions& as)
272
{
273
69
  setFinalProof(pfn, as);
274
69
  Assert(d_finalProof);
275
69
  return d_finalProof;
276
}
277
278
69
void PfManager::getAssertions(Assertions& as,
279
                              std::vector<Node>& assertions)
280
{
281
69
  context::CDList<Node>* al = as.getAssertionList();
282
69
  Assert(al != nullptr);
283
1281
  for (context::CDList<Node>::const_iterator i = al->begin(); i != al->end();
284
       ++i)
285
  {
286
1212
    assertions.push_back(*i);
287
  }
288
69
}
289
290
}  // namespace smt
291
22746
}  // namespace cvc5