summaryrefslogtreecommitdiff
path: root/src/proof/lrat/lrat_proof.cpp
blob: 1685ad1c3067682660e3390e7601b1daa2ee6ca8 (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
/*********************                                                        */
/*! \file lrat_proof.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Alex Ozdemir
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2019 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.\endverbatim
 **
 ** \brief DRAT Proof Format
 **
 ** Defines deserialization for DRAT proofs.
 **/

#include "proof/lrat/lrat_proof.h"

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <unordered_map>

#include "base/cvc4_assert.h"
#include "base/output.h"
#include "proof/dimacs.h"
#include "proof/lfsc_proof_printer.h"
#include "util/utility.h"

#if CVC4_USE_DRAT2ER
#include "drat2er_options.h"
#include "drat_trim_interface.h"
#endif

namespace CVC4 {
namespace proof {
namespace lrat {

using prop::SatClause;
using prop::SatLiteral;
using prop::SatVariable;

namespace {

// Prints the trace as a space-separated list of (+) ints with a space at the
// end.
std::ostream& operator<<(std::ostream& o, const LratUPTrace& trace)
{
  for (const auto& i : trace)
  {
    o << i << " ";
  }
  return o;
}

/**
 * Print a list of clause indices to go to while doing UP.
 *
 * i.e. a value of type Trace
 *
 * @param o where to print to
 * @param trace the trace (list of clauses) to print
 */
void printTrace(std::ostream& o, const LratUPTrace& trace)
{
  for (ClauseIdx idx : trace)
  {
    o << "(Tracec " << idx << " ";
  }
  o << "Tracen";
  std::fill_n(std::ostream_iterator<char>(o), trace.size(), ')');
}

/**
 * Print the RAT hints for a clause addition.
 *
 * i.e. prints an LFSC value of type RATHints
 *
 * @param o where to print to
 * @param hints the RAT hints to print
 */
void printHints(std::ostream& o,
                const std::vector<std::pair<ClauseIdx, LratUPTrace>>& hints)
{
  for (auto& hint : hints)
  {
    o << "\n    (RATHintsc " << hint.first << " ";
    printTrace(o, hint.second);
    o << " ";
  }
  o << "RATHintsn";
  std::fill_n(std::ostream_iterator<char>(o), hints.size(), ')');
}

/**
 * Print an index list
 *
 * i.e. prints an LFSC value of type CIList
 *
 * @param o where to print to
 * @param indices the list of indices to print
 */
void printIndices(std::ostream& o, const std::vector<ClauseIdx>& indices)
{
  Assert(indices.size() > 0);
  // Verify that the indices are sorted!
  for (size_t i = 0, n = indices.size() - 1; i < n; ++i)
  {
    Assert(indices[i] < indices[i + 1]);
  }

  for (ClauseIdx idx : indices)
  {
    o << "(CIListc " << idx << " ";
  }
  o << "CIListn";
  std::fill_n(std::ostream_iterator<char>(o), indices.size(), ')');
}

}  // namespace

// Prints the LRAT addition line in textual format

LratProof LratProof::fromDratProof(
    const std::unordered_map<ClauseId, prop::SatClause>& clauses,
    const std::vector<ClauseId> usedIds,
    const std::string& dratBinary,
    TimerStat& toolTimer)
{
  std::ostringstream cmd;
  std::string formulaFilename("cvc4-dimacs-XXXXXX");
  std::string dratFilename("cvc4-drat-XXXXXX");
  std::string lratFilename("cvc4-lrat-XXXXXX");

  std::unique_ptr<std::fstream> formStream = openTmpFile(&formulaFilename);
  printDimacs(*formStream, clauses, usedIds);
  formStream->close();

  std::unique_ptr<std::fstream> dratStream = openTmpFile(&dratFilename);
  (*dratStream) << dratBinary;
  dratStream->close();

  std::unique_ptr<std::fstream> lratStream = openTmpFile(&lratFilename);

  {
    CodeTimer blockTimer{toolTimer};
#if CVC4_USE_DRAT2ER
    drat2er::drat_trim::CheckAndConvertToLRAT(
        formulaFilename, dratFilename, lratFilename, drat2er::options::QUIET);
#else
    Unimplemented(
        "LRAT proof production requires drat2er.\n"
        "Run contrib/get-drat2er, reconfigure with --drat2er, and rebuild");
#endif
  }

  LratProof lrat(*lratStream);
  remove(formulaFilename.c_str());
  remove(dratFilename.c_str());
  remove(lratFilename.c_str());
  return lrat;
}

std::istream& operator>>(std::istream& in, SatLiteral& l)
{
  int64_t i;
  in >> i;
  l = SatLiteral(std::abs(i), i < 0);
  return in;
}

// This parser is implemented to parse the textual RAT format found in
// "Efficient Certified RAT Verification", by Cruz-Filipe et. All
LratProof::LratProof(std::istream& textualProof)
{
  for (size_t line = 0;; ++line)
  {
    // Read beginning of instruction. EOF indicates that we're done.
    size_t clauseIdx;
    textualProof >> clauseIdx;
    if (textualProof.eof())
    {
      return;
    }

    // Read the first word of the instruction. A 'd' indicates deletion.
    std::string first;
    textualProof >> first;
    Trace("pf::lrat") << "First word: " << first << std::endl;
    Assert(textualProof.good());
    if (first == "d")
    {
      std::vector<ClauseIdx> clauses;
      while (true)
      {
        ClauseIdx di;
        textualProof >> di;
        Assert(textualProof.good());
        if (di == 0)
        {
          break;
        }
        clauses.push_back(di);
      }
      if (clauses.size() > 0)
      {
        std::sort(clauses.begin(), clauses.end());
        std::unique_ptr<LratInstruction> instr(
            new LratDeletion(clauseIdx, std::move(clauses)));
        d_instructions.push_back(std::move(instr));
      }
    }
    else
    {
      // We need to reparse the first word as a literal to read the clause
      // we're parsing. It ends with a 0;
      std::istringstream firstS(first);
      SatLiteral lit;
      firstS >> lit;
      Trace("pf::lrat") << "First lit: " << lit << std::endl;
      Assert(!firstS.fail(), "Couldn't parse first literal from addition line");

      SatClause clause;
      for (; lit != 0; textualProof >> lit)
      {
        Assert(textualProof.good());
        clause.emplace_back(lit.getSatVariable() - 1, lit.isNegated());
      }

      // Now we read the AT UP trace. It ends at the first non-(+) #
      std::vector<ClauseIdx> atTrace;
      int64_t i;
      textualProof >> i;
      for (; i > 0; textualProof >> i)
      {
        Assert(textualProof.good());
        atTrace.push_back(i);
      }

      // For each RAT hint... (each RAT hint starts with a (-)).
      std::vector<std::pair<ClauseIdx, LratUPTrace>> resolvants;
      for (; i<0; textualProof>> i)
      {
        Assert(textualProof.good());
        // Create an entry in the RAT hint list
        resolvants.emplace_back(-i, std::vector<ClauseIdx>());

        // Record the UP trace. It ends with a (-) or 0.
        textualProof >> i;
        for (; i > 0; textualProof >> i)
        {
          resolvants.back().second.push_back(i);
        }
      }
      // Pairs compare based on the first element, so this sorts by the
      // resolution target index
      std::sort(resolvants.begin(), resolvants.end());
      std::unique_ptr<LratInstruction> instr(
          new LratAddition(clauseIdx,
                           std::move(clause),
                           std::move(atTrace),
                           std::move(resolvants)));
      d_instructions.push_back(std::move(instr));
    }
  }
}

void LratProof::outputAsLfsc(std::ostream& o) const
{
  std::ostringstream closeParen;
  for (const auto& i : d_instructions)
  {
    i->outputAsLfsc(o, closeParen);
  }
  o << "LRATProofn";
  o << closeParen.str();
}

void LratAddition::outputAsText(std::ostream& o) const
{
  o << d_idxOfClause << " ";
  textOut(o, d_clause) << " ";
  o << d_atTrace;  // Inludes a space at the end.
  for (const auto& rat : d_resolvants)
  {
    o << "-" << rat.first << " ";
    o << rat.second;  // Includes a space at the end.
  }
  o << "0\n";
}

void LratAddition::outputAsLfsc(std::ostream& o, std::ostream& closeParen) const
{
  o << "\n    (LRATProofa " << d_idxOfClause << " ";
  closeParen << ")";
  LFSCProofPrinter::printSatClause(d_clause, o, "bb");
  o << " ";
  printTrace(o, d_atTrace);
  o << " ";
  printHints(o, d_resolvants);
  o << " ";
}

void LratDeletion::outputAsText(std::ostream& o) const
{
  o << d_idxOfClause << " d ";
  for (const auto& idx : d_clauses)
  {
    o << idx << " ";
  }
  o << "0\n";
}

void LratDeletion::outputAsLfsc(std::ostream& o, std::ostream& closeParen) const
{
  o << "\n    (LRATProofd ";
  closeParen << ")";
  printIndices(o, d_clauses);
  o << " ";
}

std::ostream& operator<<(std::ostream& o, const LratProof& p)
{
  for (const auto& instr : p.getInstructions())
  {
    o << *instr;
  }
  return o;
}

std::ostream& operator<<(std::ostream& o, const LratInstruction& i)
{
  i.outputAsText(o);
  return o;
}

}  // namespace lrat
}  // namespace proof
}  // namespace CVC4
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback