summaryrefslogtreecommitdiff
path: root/src/proof/lrat/lrat_proof.h
blob: 1c065a08e3c7018fd95a00ac9fe60ed427964a6c (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
/*********************                                                        */
/*! \file lrat_proof.h
 ** \verbatim
 ** Top contributors (to current version):
 **   Alex Ozdemir, Mathias Preiner
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2020 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 LRAT Proof Format
 **
 ** Declares C++ types that represent a LRAT proof.
 ** Defines serialization for these types.
 **
 ** Represents an **abstract** LRAT proof.
 ** Does **not** represent an LFSC LRAT proof, or an LRAT proof being used to
 ** prove things about bit-vectors.
 **
 ** Paper on LRAT: https://www.cs.utexas.edu/~marijn/publications/lrat.pdf
 **/

#include "cvc4_private.h"

#ifndef CVC4__PROOF__LRAT__LRAT_PROOF_H
#define CVC4__PROOF__LRAT__LRAT_PROOF_H

#include <iosfwd>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include "proof/clause_id.h"
// Included because we need operator<< for the SAT types
#include "prop/sat_solver.h"
#include "util/statistics_registry.h"

namespace CVC4 {
namespace proof {
namespace lrat {

// Refers to clause position within an LRAT proof
using ClauseIdx = size_t;

// This is conceptually an Either<Addition,Deletion>
class LratInstruction
{
 public:
  /**
   * Write this LRAT instruction in textual format
   *
   * @param out the stream to write to
   */
  virtual void outputAsText(std::ostream& out) const = 0;
  /**
   * Write this LRAT instruction as an LFSC value
   *
   * @param out the stream to write to
   * @param closeParen the stream to write any closing parentheses to
   *
   */
  virtual void outputAsLfsc(std::ostream& o,
                            std::ostream& closeParen) const = 0;
  virtual ~LratInstruction() = default;
};

class LratDeletion : public LratInstruction
{
 public:
  LratDeletion(ClauseIdx idxOfClause, std::vector<ClauseIdx>&& clauses)
      : d_idxOfClause(idxOfClause), d_clauses(clauses)
  {
    // Nothing left to do
  }

  LratDeletion() = default;

  void outputAsText(std::ostream& out) const override;
  void outputAsLfsc(std::ostream& o, std::ostream& closeParen) const override;

 private:
  // This idx doesn't really matter, but it's in the format anyway, so we parse
  // it.
  ClauseIdx d_idxOfClause;

  // Clauses to delete
  std::vector<ClauseIdx> d_clauses;
};

// A sequence of locations that will contain unit clauses during unit
// propegation
using LratUPTrace = std::vector<ClauseIdx>;

class LratAddition : public LratInstruction
{
 public:
  LratAddition(ClauseIdx idxOfClause,
               prop::SatClause&& clause,
               LratUPTrace&& atTrace,
               std::vector<std::pair<ClauseIdx, LratUPTrace>> resolvants)
      : d_idxOfClause(idxOfClause),
        d_clause(clause),
        d_atTrace(atTrace),
        d_resolvants(resolvants)
  {
    // Nothing left to do
  }

  void outputAsText(std::ostream& out) const override;
  void outputAsLfsc(std::ostream& o, std::ostream& closeParen) const override;

 private:
  // The idx for the new clause
  ClauseIdx d_idxOfClause;
  // The new clause
  prop::SatClause d_clause;
  // UP trace based on the negation of that clause
  LratUPTrace d_atTrace;

  // Clauses that can resolve with `clause` on its first variable,
  // together with a UP trace after that resolution.
  // Used for RAT checks.
  std::vector<std::pair<ClauseIdx, LratUPTrace>> d_resolvants;
};

class LratProof
{
 public:
  /**
   * @brief Construct an LRAT proof from a DRAT proof, using drat-trim
   *
   * @param clauses A store of clauses that might be in our formula
   * @param usedIds the ids of clauses that are actually in our formula
   * @param dratBinary  The DRAT proof from the SAT solver, as a binary stream.
   *
   * @return an LRAT proof an a timer for how long it took to run drat-trim
   */
  static LratProof fromDratProof(
      const std::unordered_map<ClauseId, prop::SatClause>& clauses,
      const std::vector<ClauseId> usedIds,
      const std::string& dratBinary,
      TimerStat& toolTimer);
  /**
   * @brief Construct an LRAT proof from its textual representation
   *
   * @param textualProof the textual encoding of the LRAT proof. See the paper
   *                     in the file's header comment.
   */
  LratProof(std::istream& textualProof);

  /**
   * Construct a LRAT proof from an explicit instruction list
   *
   * @param instructions
   */
  LratProof(std::vector<std::unique_ptr<LratInstruction>>&& instructions)
      : d_instructions(std::move(instructions))
  {
    // Nothing else
  }

  const std::vector<std::unique_ptr<LratInstruction>>& getInstructions() const
  {
    return d_instructions;
  }

  void outputAsLfsc(std::ostream& o) const;

 private:
  // The instructions in the proof. Each is a deletion or addition.
  std::vector<std::unique_ptr<LratInstruction>> d_instructions;
};

// Prints the LRAT proof in textual format
std::ostream& operator<<(std::ostream& o, const LratProof& p);
std::ostream& operator<<(std::ostream& o, const LratInstruction& i);

}  // namespace lrat
}  // namespace proof
}  // namespace CVC4

#endif
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback