This repository was archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecker.cc
More file actions
546 lines (433 loc) · 12.3 KB
/
checker.cc
File metadata and controls
546 lines (433 loc) · 12.3 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// --- Includes ----------------------------------------------------------------
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
// --- Types and constants -----------------------------------------------------
typedef uint32_t u32;
typedef int64_t i64;
typedef u32 variable_t;
typedef enum { POSITIVE, NEGATIVE } polarity_t;
typedef std::pair<polarity_t, variable_t> literal_t;
typedef u32 index_t;
typedef std::vector<literal_t> literals_t;
typedef literals_t clause_t;
// ordered map - traversal must be in index order
typedef std::map<index_t, clause_t> formula_t;
typedef std::vector<index_t> indices_t;
typedef struct {
indices_t indices;
} delete_t;
typedef std::vector<index_t> rups_t;
typedef std::pair<index_t, rups_t> rat_t;
typedef std::vector<rat_t> rats_t;
typedef struct {
index_t index;
clause_t clause;
rups_t rups;
rats_t rats;
} extend_t;
typedef enum { DELETE, EXTEND } type_t;
typedef struct {
type_t type;
delete_t delete_;
extend_t extend;
} step_t;
typedef std::unordered_map<index_t, index_t> index_map_t;
template <class T> using min_heap_t =
std::priority_queue<T, std::vector<T>, std::greater<T>>;
typedef min_heap_t<index_t> recycler_t;
class fail_ : public std::ostringstream {
public:
[[noreturn]] ~fail_() {
std::cerr << str();
::exit(1);
}
};
#define fail fail_{}
// --- Macros and inline functions ---------------------------------------------
// --- Globals -----------------------------------------------------------------
static variable_t g_n_vars = 0;
static index_t g_n_clauses = 0;
static formula_t g_f;
static u32 g_n_steps = 0;
static index_map_t g_index_map;
static recycler_t g_recycler;
// --- Helper declarations -----------------------------------------------------
static literal_t make_lit(i64 val);
static literal_t flip_lit(const literal_t &l);
static void read_formula(const char *path);
static void read_header(std::ifstream &ifs);
static void read_body(std::ifstream &ifs);
static void check_proof(const char *path);
static void read_step(step_t &s, std::ifstream &ifs);
static void read_delete(step_t &s, std::ifstream &ifs);
static void read_extend(step_t &s, index_t i, i64 val, std::ifstream &ifs);
static void read_clause(clause_t &c, i64 &val, std::ifstream &ifs);
static void read_rup(rups_t &rups, i64 &val, std::ifstream &ifs);
static void read_rat(rats_t &rats, i64 &val, std::ifstream &ifs);
static void remap_step(step_t &s);
static void remap_delete(delete_t &dp);
static void remap_extend(extend_t &ep);
static index_t get_index(index_t i0);
static void put_index(index_t i0, index_t i);
static void map_index(index_t &i);
static void run_step(const step_t &s);
static void run_delete(const delete_t &dp);
static void run_extend(const extend_t &ep);
static bool run_rup(clause_t &c, const rups_t &rups);
static clause_t minus(const clause_t &c1, const clause_t &c2);
static void run_rat(clause_t &c, const rats_t &rats);
static bool needs_check(const clause_t &cf, const literal_t ¬_l);
static void check_rat_rup(const clause_t &cf, const clause_t &c, const literal_t &l, const literal_t ¬_l, const rats_t &rats, index_t i, u32 i_rat);
static void check_complement(const clause_t &cf, const clause_t &c, const literal_t &l);
static void check_resolvent(const clause_t &cf, const clause_t &c, const literal_t ¬_l, const rups_t &rups);
static clause_t resolvent(const clause_t &c, const clause_t &cf, const literal_t ¬_l);
// --- API ---------------------------------------------------------------------
int main(int argc, char *argv[])
{
if (argc != 3) {
fail << "usage: checker foobar.cnf foobar.lrat" << std::endl;
}
read_formula(argv[1]);
check_proof(argv[2]);
return 0;
}
// --- Helpers -----------------------------------------------------------------
static literal_t make_lit(i64 val)
{
literal_t l = val < 0 ?
std::make_pair(NEGATIVE, (variable_t)-val) :
std::make_pair(POSITIVE, (variable_t)val);
if (l.second > g_n_vars) {
g_n_vars = l.second;
}
--l.second;
return l;
}
static literal_t flip_lit(const literal_t &l)
{
return l.first == POSITIVE ?
std::make_pair(NEGATIVE, l.second) :
std::make_pair(POSITIVE, l.second);
}
static void read_formula(const char *path)
{
std::ifstream ifs(path, std::ios::in);
if (!ifs.is_open()) {
fail << "failed to open formula file " << path << std::endl;
}
read_header(ifs);
read_body(ifs);
}
static void read_header(std::ifstream &ifs)
{
std::string token;
u32 dummy;
if (!(ifs >> token) || token != "p" || !(ifs >> token) || token != "cnf" ||
!(ifs >> dummy) || !(ifs >> dummy)) {
fail << "invalid header" << std::endl;
}
}
static void read_body(std::ifstream &ifs)
{
index_t i0 = 0;
clause_t c;
i64 val;
while (ifs >> val) {
if (val == 0) {
index_t i = get_index(++i0);
g_f.insert(std::make_pair(i, c));
c.clear();
} else {
literal_t l = make_lit(val);
c.push_back(l);
}
}
if (!ifs.eof() || val != 0) {
fail << "invalid body" << std::endl;
}
}
static void check_proof(const char *path)
{
std::ifstream ifs(path, std::ios::in);
if (!ifs.is_open()) {
fail << "failed to open proof file " << path << std::endl;
}
while (true) {
step_t s;
++g_n_steps;
read_step(s, ifs);
remap_step(s);
run_step(s);
if (s.type == EXTEND && s.extend.clause.empty()) {
break;
}
}
}
static void read_step(step_t &s, std::ifstream &ifs)
{
index_t i;
std::string str;
if (!(ifs >> i) || !(ifs >> str)) {
fail << "invalid step" << std::endl;
}
if (str == "d") {
read_delete(s, ifs);
} else {
i64 val = strtoll(str.c_str(), NULL, 10);
read_extend(s, i, val, ifs);
}
}
static void read_delete(step_t &s, std::ifstream &ifs)
{
s.type = DELETE;
delete_t &dp = s.delete_;
index_t i;
while (ifs >> i) {
if (i == 0) {
return;
}
dp.indices.push_back(i);
}
fail << "invalid delete step" << std::endl;
}
static void read_extend(step_t &s, index_t i, i64 val, std::ifstream &ifs)
{
s.type = EXTEND;
extend_t &ep = s.extend;
ep.index = i;
read_clause(ep.clause, val, ifs);
read_rup(ep.rups, val, ifs);
if (val != 0) {
read_rat(ep.rats, val, ifs);
}
}
static void read_clause(clause_t &c, i64 &val, std::ifstream &ifs)
{
while (val != 0) {
literal_t l = make_lit(val);
c.push_back(l);
if (!(ifs >> val)) {
fail << "invalid clause" << std::endl;
}
}
}
static void read_rup(rups_t &rups, i64 &val, std::ifstream &ifs)
{
while (true) {
if (!(ifs >> val)) {
fail << "invalid RUP hints" << std::endl;
}
if (val <= 0) {
return;
}
rups.push_back((index_t)val);
}
}
static void read_rat(rats_t &rats, i64 &val, std::ifstream &ifs)
{
index_t i = (index_t)-val;
rups_t rups;
while (true) {
if (!(ifs >> val)) {
fail << "invalid RAT hints" << std::endl;
}
if (val > 0) {
rups.push_back((index_t)val);
} else {
rats.push_back(std::make_pair(i, rups));
if (val == 0) {
return;
}
i = (index_t)-val;
rups.clear();
}
}
}
static void remap_step(step_t &s)
{
if (s.type == DELETE) {
remap_delete(s.delete_);
} else {
remap_extend(s.extend);
}
}
static void remap_delete(delete_t &dp)
{
for (auto &i : dp.indices) {
index_t i0 = i;
map_index(i);
put_index(i0, i);
}
}
static void remap_extend(extend_t &ep)
{
ep.index = get_index(ep.index);
for (auto &i : ep.rups) {
map_index(i);
}
for (auto &[i, rups] : ep.rats) {
map_index(i);
for (auto &k : rups) {
map_index(k);
}
}
std::sort(ep.rats.begin(), ep.rats.end()); // indices must remain in order
}
static index_t get_index(index_t i0)
{
index_t i;
if (!g_recycler.empty()) {
i = g_recycler.top();
g_recycler.pop();
} else {
i = g_n_clauses++;
}
g_index_map.insert(std::make_pair(i0, i));
return i;
}
static void put_index(index_t i0, index_t i)
{
size_t n = g_index_map.erase(i0);
assert (n == 1);
g_recycler.push(i);
}
static void map_index(index_t &i)
{
const auto it = g_index_map.find(i);
if (it == g_index_map.end()) {
fail << "failed to map index " << i << " in step " << g_n_steps <<
std::endl;
}
i = it->second;
}
static void run_step(const step_t &s)
{
if (s.type == DELETE) {
run_delete(s.delete_);
} else {
run_extend(s.extend);
}
}
static void run_delete(const delete_t &dp)
{
for (const auto &i : dp.indices) {
if (g_f.erase(i) == 0) {
fail << "invalid delete index " << i << " in step " << g_n_steps <<
std::endl;
}
}
}
static void run_extend(const extend_t &ep)
{
clause_t c = ep.clause;
if (!run_rup(c, ep.rups)) {
run_rat(c, ep.rats);
}
g_f.insert(std::make_pair(ep.index, ep.clause));
}
static bool run_rup(clause_t &c, const rups_t &rups)
{
for (const auto &i : rups) {
const auto it = g_f.find(i);
if (it == g_f.end()) {
fail << "invalid RUP index " << i << " in step " << g_n_steps <<
std::endl;
}
const auto &diff = minus(it->second, c);
if (diff.size() == 0) {
return true;
}
if (diff.size() > 1) {
fail << "non-unit clause for RUP index " << i << " in step " <<
g_n_steps << std::endl;
}
c.push_back(flip_lit(diff.front()));
}
return false;
}
static clause_t minus(const clause_t &c1, const clause_t &c2)
{
clause_t diff;
for (const auto &lc1 : c1) {
if (std::find(c2.cbegin(), c2.cend(), lc1) == c2.cend()) {
diff.push_back(lc1);
}
}
return diff;
}
static void run_rat(clause_t &c, const rats_t &rats)
{
if (c.empty()) {
fail << "RAT check with empty clause in step " << g_n_steps <<
std::endl;
}
literal_t l = c.front();
literal_t not_l = flip_lit(l);
u32 i_rat = 0;
for (const auto &[i, cf] : g_f) {
if (needs_check(cf, not_l)) {
check_rat_rup(cf, c, l, not_l, rats, i, i_rat);
++i_rat;
}
}
}
static bool needs_check(const clause_t &cf, const literal_t ¬_l)
{
return std::find(cf.cbegin(), cf.cend(), not_l) != cf.cend();
}
static void check_rat_rup(const clause_t &cf, const clause_t &c,
const literal_t &l, const literal_t ¬_l, const rats_t &rats,
index_t i, u32 i_rat)
{
if (i_rat >= rats.size() || rats[i_rat].first != i) {
fail << "invalid RAT hints in step " << g_n_steps << std::endl;
}
const rups_t &rups = rats[i_rat].second;
if (rups.empty()) {
check_complement(cf, c, l);
} else {
check_resolvent(cf, c, not_l, rups);
}
}
static void check_complement(const clause_t &cf, const clause_t &c,
const literal_t &l)
{
for (const auto &lc : c) {
if (lc != l &&
std::find(cf.cbegin(), cf.cend(), flip_lit(lc)) != cf.cend()) {
return;
}
}
fail << "complement check failed in step " << g_n_steps << std::endl;
}
static void check_resolvent(const clause_t &cf, const clause_t &c,
const literal_t ¬_l, const rups_t &rups)
{
clause_t &&cr = resolvent(c, cf, not_l);
if (!run_rup(cr, rups)) {
fail << "resolvent check failed in step " << g_n_steps << std::endl;
}
}
static clause_t resolvent(const clause_t &c, const clause_t &cf,
const literal_t ¬_l)
{
clause_t cr = c;
for (const auto &lcf : cf) {
if (lcf != not_l) {
cr.push_back(lcf);
}
}
return cr;
}