26#include "absl/container/flat_hash_set.h"
27#include "absl/log/check.h"
28#include "absl/strings/numbers.h"
29#include "absl/strings/str_split.h"
30#include "absl/strings/string_view.h"
31#include "absl/time/clock.h"
32#include "absl/types/span.h"
43DratChecker::Clause::Clause(
int first_literal_index,
int num_literals)
44 : first_literal_index(first_literal_index), num_literals(num_literals) {}
46std::size_t DratChecker::ClauseHash::operator()(
47 const ClauseIndex clause_index)
const {
49 for (Literal
literal : checker->Literals(checker->clauses_[clause_index])) {
55bool DratChecker::ClauseEquiv::operator()(
56 const ClauseIndex clause_index1,
const ClauseIndex clause_index2)
const {
57 return checker->Literals(checker->clauses_[clause_index1]) ==
58 checker->Literals(checker->clauses_[clause_index2]);
61DratChecker::DratChecker()
63 clause_set_(0, ClauseHash(this), ClauseEquiv(this)),
66bool DratChecker::Clause::IsDeleted(ClauseIndex clause_index)
const {
67 return deleted_index <= clause_index;
72 const ClauseIndex clause_index = AddClause(clause);
74 const auto it = clause_set_.find(clause_index);
75 if (it != clause_set_.end()) {
76 clauses_[*it].num_copies += 1;
79 clause_set_.insert(clause_index);
84 const ClauseIndex infered_clause_index = AddClause(clause);
86 first_infered_clause_index_ = infered_clause_index;
89 const auto it = clause_set_.find(infered_clause_index);
90 if (it != clause_set_.end()) {
91 clauses_[*it].num_copies += 1;
92 if (*it >= first_infered_clause_index_ && !clause.empty()) {
93 CHECK_EQ(clauses_[*it].rat_literal_index, clause[0].Index());
97 clauses_[infered_clause_index].rat_literal_index =
99 clause_set_.insert(infered_clause_index);
103ClauseIndex DratChecker::AddClause(absl::Span<const Literal> clause) {
104 const int first_literal_index = literals_.size();
105 literals_.insert(literals_.end(), clause.begin(), clause.end());
108 std::sort(literals_.begin() + first_literal_index, literals_.end());
110 std::unique(literals_.begin() + first_literal_index, literals_.end()),
113 for (
int i = first_literal_index + 1;
i < literals_.size(); ++
i) {
114 CHECK(literals_[
i] != literals_[
i - 1].Negated());
117 Clause(first_literal_index, literals_.size() - first_literal_index));
118 if (!clause.empty()) {
120 std::max(num_variables_, literals_.back().Variable().value() + 1);
122 return ClauseIndex(clauses_.size() - 1);
127 const auto it = clause_set_.find(AddClause(clause));
128 if (it != clause_set_.end()) {
129 Clause& existing_clause = clauses_[*it];
130 existing_clause.num_copies -= 1;
131 if (existing_clause.num_copies == 0) {
132 DCHECK(existing_clause.deleted_index == std::numeric_limits<int>::max());
133 existing_clause.deleted_index = clauses_.size() - 1;
134 if (clauses_.back().num_literals >= 2) {
135 clauses_[ClauseIndex(clauses_.size() - 2)].deleted_clauses.
push_back(
138 clause_set_.erase(it);
141 LOG(WARNING) <<
"Couldn't find deleted clause";
147void DratChecker::RemoveLastClause() {
148 literals_.resize(clauses_.back().first_literal_index);
157 if (clauses_.empty() || first_infered_clause_index_ ==
kNoClauseIndex ||
158 clauses_.back().num_literals != 0) {
161 clauses_.back().is_needed_for_proof =
true;
168 const int64_t start_time_nanos = absl::GetCurrentTimeNanos();
171 for (ClauseIndex
i(clauses_.size() - 1);
i >= first_infered_clause_index_;
176 const Clause& clause = clauses_[
i];
179 for (
const ClauseIndex j : clause.deleted_clauses) {
182 if (!clause.is_needed_for_proof) {
186 if (HasRupProperty(
i, Literals(clause))) {
203 std::vector<Literal> resolvent;
204 for (ClauseIndex j(0); j <
i; ++j) {
205 if (!clauses_[j].IsDeleted(
i) &&
209 if (!
Resolve(Literals(clause), Literals(clauses_[j]),
210 Literal(clause.rat_literal_index), &tmp_assignment_,
212 !HasRupProperty(
i, resolvent)) {
218 LogStatistics(absl::GetCurrentTimeNanos() - start_time_nanos);
223 return GetClausesNeededForProof(ClauseIndex(0), first_infered_clause_index_);
227 return GetClausesNeededForProof(first_infered_clause_index_,
228 ClauseIndex(clauses_.size()));
231std::vector<std::vector<Literal>> DratChecker::GetClausesNeededForProof(
232 ClauseIndex begin, ClauseIndex
end)
const {
233 std::vector<std::vector<Literal>> result;
234 for (ClauseIndex
i = begin;
i <
end; ++
i) {
235 const Clause& clause = clauses_[
i];
236 if (clause.is_needed_for_proof) {
237 const absl::Span<const Literal>& literals = Literals(clause);
238 result.emplace_back(literals.begin(), literals.end());
240 const int rat_literal_clause_index =
241 std::find(literals.begin(), literals.end(),
242 Literal(clause.rat_literal_index)) -
244 std::swap(result.back()[0], result.back()[rat_literal_clause_index]);
251absl::Span<const Literal> DratChecker::Literals(
const Clause& clause)
const {
252 return absl::Span<const Literal>(
253 literals_.data() + clause.first_literal_index, clause.num_literals);
256void DratChecker::Init() {
258 assignment_.
Resize(num_variables_);
260 high_priority_literals_to_assign_.clear();
261 low_priority_literals_to_assign_.clear();
262 watched_literals_.clear();
263 watched_literals_.
resize(2 * num_variables_);
264 single_literal_clauses_.clear();
266 tmp_assignment_.
Resize(num_variables_);
269 for (ClauseIndex clause_index(0); clause_index < clauses_.size();
271 Clause& clause = clauses_[clause_index];
272 if (clause.num_literals >= 2) {
275 if (clause.deleted_index == std::numeric_limits<int>::max()) {
276 WatchClause(clause_index);
278 }
else if (clause.num_literals == 1) {
279 single_literal_clauses_.push_back(clause_index);
284void DratChecker::WatchClause(ClauseIndex clause_index) {
285 const Literal* clause_literals =
286 literals_.data() + clauses_[clause_index].first_literal_index;
287 watched_literals_[clause_literals[0].Index()].
push_back(clause_index);
288 watched_literals_[clause_literals[1].Index()].
push_back(clause_index);
291bool DratChecker::HasRupProperty(ClauseIndex num_clauses,
292 absl::Span<const Literal> clause) {
294 for (
const Literal
literal : clause) {
302 for (
const ClauseIndex clause_index : single_literal_clauses_) {
303 const Clause& clause = clauses_[clause_index];
306 if (clause_index < num_clauses && !clause.IsDeleted(num_clauses)) {
307 if (clause.is_needed_for_proof) {
308 high_priority_literals_to_assign_.push_back(
309 {literals_[clause.first_literal_index], clause_index});
311 low_priority_literals_to_assign_.push_back(
312 {literals_[clause.first_literal_index], clause_index});
317 while (!(high_priority_literals_to_assign_.empty() &&
318 low_priority_literals_to_assign_.empty()) &&
320 std::vector<LiteralToAssign>& stack =
321 high_priority_literals_to_assign_.empty()
322 ? low_priority_literals_to_assign_
323 : high_priority_literals_to_assign_;
324 const LiteralToAssign literal_to_assign = stack.back();
330 conflict = literal_to_assign.source_clause_index;
337 unit_stack_.push_back(literal_to_assign.source_clause_index);
338 conflict = AssignAndPropagate(num_clauses, literal_to_assign.literal,
339 literal_to_assign.source_clause_index);
342 MarkAsNeededForProof(&clauses_[conflict]);
345 for (
const Literal
literal : assigned_) {
349 high_priority_literals_to_assign_.clear();
350 low_priority_literals_to_assign_.clear();
356ClauseIndex DratChecker::AssignAndPropagate(ClauseIndex num_clauses,
358 ClauseIndex source_clause_index) {
361 assignment_source_[
literal.Variable()] = source_clause_index;
363 const Literal false_literal =
literal.Negated();
364 std::vector<ClauseIndex>& watched = watched_literals_[false_literal.Index()];
365 int new_watched_size = 0;
367 for (
const ClauseIndex clause_index : watched) {
368 if (clause_index >= num_clauses) {
373 Clause& clause = clauses_[clause_index];
374 DCHECK(!clause.IsDeleted(num_clauses));
376 watched[new_watched_size++] = clause_index;
380 Literal* clause_literals = literals_.data() + clause.first_literal_index;
381 const Literal other_watched_literal(LiteralIndex(
383 clause_literals[1].
Index().
value() ^ false_literal.Index().value()));
385 watched[new_watched_size++] = clause_index;
389 bool new_watched_literal_found =
false;
390 for (
int i = 2;
i < clause.num_literals; ++
i) {
392 clause_literals[0] = other_watched_literal;
393 clause_literals[1] = clause_literals[
i];
394 clause_literals[
i] = false_literal;
395 watched_literals_[clause_literals[1].Index()].
push_back(clause_index);
396 new_watched_literal_found =
true;
401 if (!new_watched_literal_found) {
406 conflict_index = clause_index;
413 if (clause.is_needed_for_proof) {
414 high_priority_literals_to_assign_.push_back(
415 {other_watched_literal, clause_index});
417 low_priority_literals_to_assign_.push_back(
418 {other_watched_literal, clause_index});
421 watched[new_watched_size++] = clause_index;
424 watched.
resize(new_watched_size);
425 return conflict_index;
428void DratChecker::MarkAsNeededForProof(Clause* clause) {
429 const auto mark_clause_and_sources = [&](Clause* clause) {
430 clause->is_needed_for_proof =
true;
431 for (
const Literal
literal : Literals(*clause)) {
432 const ClauseIndex source_clause_index =
433 assignment_source_[
literal.Variable()];
435 clauses_[source_clause_index].tmp_is_needed_for_proof_step =
true;
439 mark_clause_and_sources(clause);
440 for (
int i = unit_stack_.size() - 1;
i >= 0; --
i) {
441 Clause& unit_clause = clauses_[unit_stack_[
i]];
442 if (unit_clause.tmp_is_needed_for_proof_step) {
443 mark_clause_and_sources(&unit_clause);
447 unit_clause.tmp_is_needed_for_proof_step =
false;
452void DratChecker::LogStatistics(int64_t duration_nanos)
const {
453 int problem_clauses_needed_for_proof = 0;
454 int infered_clauses_needed_for_proof = 0;
455 for (ClauseIndex
i(0);
i < clauses_.size(); ++
i) {
456 if (clauses_[
i].is_needed_for_proof) {
457 if (
i < first_infered_clause_index_) {
458 ++problem_clauses_needed_for_proof;
460 ++infered_clauses_needed_for_proof;
464 LOG(INFO) << problem_clauses_needed_for_proof
465 <<
" problem clauses needed for proof, out of "
466 << first_infered_clause_index_;
467 LOG(INFO) << infered_clauses_needed_for_proof
468 <<
" infered clauses needed for proof, out of "
469 << clauses_.size() - first_infered_clause_index_;
470 LOG(INFO) << num_rat_checks_ <<
" RAT infered clauses";
471 LOG(INFO) <<
"verification time: " << 1e-9 * duration_nanos <<
" s";
475 return std::find(clause.begin(), clause.end(),
literal) != clause.end();
479 absl::Span<const Literal> other_clause,
481 std::vector<Literal>* resolvent) {
487 if (
literal != complementary_literal) {
495 for (
const Literal other_literal : other_clause) {
496 if (other_literal != complementary_literal.
Negated()) {
501 resolvent->push_back(other_literal);
508 if (
literal != complementary_literal) {
520 std::vector<Literal> literals;
521 std::ifstream
file(file_path);
526 std::vector<absl::string_view> words =
527 absl::StrSplit(
line, absl::ByAnyChar(
" \t"), absl::SkipWhitespace());
528 if (words.empty() || words[0] ==
"c") {
532 if (words[0] ==
"p") {
533 if (num_clauses > 0 || words.size() != 4 || words[1] !=
"cnf" ||
535 !absl::SimpleAtoi(words[3], &num_clauses) || num_clauses <= 0) {
536 LOG(ERROR) <<
"Invalid content '" <<
line <<
"' at line " << line_number
537 <<
" of " << file_path;
544 for (
int i = 0;
i < words.size(); ++
i) {
546 if (!absl::SimpleAtoi(words[
i], &signed_value) ||
548 (signed_value == 0 &&
i != words.size() - 1)) {
549 LOG(ERROR) <<
"Invalid content '" <<
line <<
"' at line " << line_number
550 <<
" of " << file_path;
554 if (signed_value != 0) {
555 literals.push_back(
Literal(signed_value));
567 bool ends_with_empty_clause =
false;
568 std::vector<Literal> literals;
569 std::ifstream
file(file_path);
574 std::vector<absl::string_view> words =
575 absl::StrSplit(
line, absl::ByAnyChar(
" \t"), absl::SkipWhitespace());
576 bool delete_clause = !words.empty() && words[0] ==
"d";
578 for (
int i = (delete_clause ? 1 : 0);
i < words.size(); ++
i) {
580 if (!absl::SimpleAtoi(words[
i], &signed_value) ||
581 (signed_value == 0 &&
i != words.size() - 1)) {
582 LOG(ERROR) <<
"Invalid content '" <<
line <<
"' at line " << line_number
583 <<
" of " << file_path;
587 if (signed_value != 0) {
588 literals.push_back(
Literal(signed_value));
593 ends_with_empty_clause =
false;
596 ends_with_empty_clause = literals.empty();
599 if (!ends_with_empty_clause) {
607 absl::Span<
const std::vector<Literal>> clauses,
609 std::ofstream output_stream(file_path, std::ofstream::out);
611 output_stream <<
"p cnf " <<
num_variables <<
" " << clauses.size() <<
"\n";
613 for (
const auto& clause : clauses) {
615 output_stream <<
literal.SignedValue() <<
" ";
617 output_stream <<
"0\n";
619 output_stream.close();
620 return output_stream.good();
void AddProblemClause(absl::Span< const Literal > clause)
std::vector< std::vector< Literal > > GetUnsatSubProblem() const
Status Check(double max_time_in_seconds)
See Algorithm of Fig. 8 in 'Trimming while Checking Clausal Proofs'.
std::vector< std::vector< Literal > > GetOptimizedProof() const
void AddInferedClause(absl::Span< const Literal > clause)
void DeleteClause(absl::Span< const Literal > clause)
int num_variables() const
bool LiteralIsAssigned(Literal literal) const
bool LiteralIsFalse(Literal literal) const
void AssignFromTrueLiteral(Literal literal)
void UnassignLiteral(Literal literal)
void Resize(int num_variables)
bool LiteralIsTrue(Literal literal) const
void push_back(const value_type &val)
void resize(size_type new_size)
bool AddInferedAndDeletedClauses(const std::string &file_path, DratChecker *drat_checker)
bool AddProblemClauses(const std::string &file_path, DratChecker *drat_checker)
const LiteralIndex kNoLiteralIndex(-1)
bool Resolve(absl::Span< const Literal > clause, absl::Span< const Literal > other_clause, Literal complementary_literal, VariablesAssignment *assignment, std::vector< Literal > *resolvent)
SatFormat
The file formats that can be used to save a list of clauses.
bool PrintClauses(const std::string &file_path, SatFormat format, absl::Span< const std::vector< Literal > > clauses, int num_variables)
bool ContainsLiteral(absl::Span< const Literal > clause, Literal literal)
const ClauseIndex kNoClauseIndex(-1)
In SWIG mode, we don't want anything besides these top-level includes.
uint64_t Hash(uint64_t num, uint64_t c)
std::optional< int64_t > end