-
Notifications
You must be signed in to change notification settings - Fork 20
/
drat-trim.c
1501 lines (1314 loc) · 58.1 KB
/
drat-trim.c
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************************[drat-trim.c]
Copyright (c) 2014 Marijn Heule and Nathan Wetzler, The University of Texas at Austin.
Copyright (c) 2015-2024 Marijn Heule, Carnegie Mellon University
Last edit: April 21, 2024
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/time.h>
//#define PARTIALPROOF
#define TIMEOUT 40000
#define BIGINIT 1000000
#define INIT 4
#define END 0
#define UNSAT 0
#define SAT 1
#define DERIVATION 2
#define ID -1
#define PIVOT -2
#define MAXDEP -3
#define EXTRA 4 // ID + PIVOT + MAXDEP + terminating 0
#define INFOBITS 2 // could be 1 for SAT, must be 2 for QBF
#define DBIT 1
#define ASSUMED 2
#define MARK 3
#define ERROR -1
#define ACTIVE 1
#define FORWARD_SAT 10
#define FORWARD_UNSAT 20
#define BACKWARD_UNSAT 30
#define SUCCESS 40
#define FAILED 50
#define FIXPOINT 60
#define NOWARNING 70
#define HARDWARNING 80
#define COMPRESS
struct solver { FILE *inputFile, *proofFile, *lratFile, *traceFile, *activeFile;
int *DB, nVars, timeout, mask, delete, *falseStack, *falseA, *forced, binMode, optimize, binOutput,
*processed, *assigned, count, *used, *max, COREcount, RATmode, RATcount, nActive, *lratTable,
nLemmas, maxRAT, *preRAT, maxDependencies, nDependencies, bar, backforce, reduce,
*dependencies, maxVar, maxSize, mode, verb, unitSize, unitStackSize, prep, *current, nRemoved, warning,
delProof, *setMap, *setTruth, rupOnly;
char *coreStr, *lemmaStr;
struct timeval start_time;
long mem_used, time, nClauses, nStep, nOpt, nAlloc, *unitStack, *reason, lemmas, nResolve, *RATset,
nReads, nWrites, lratSize, lratAlloc, *lratLookup, **wlist, *optproof, *formula, *proof; };
static inline void assign (struct solver* S, int lit) {
S->falseA[-lit] = 1; *(S->assigned++) = -lit; }
int compare (const void *a, const void *b) {
return (*(int*)a - *(int*)b); }
int abscompare (const void *a, const void *b) {
return (abs(*(int*)a) - abs(*(int*)b)); }
static inline void printClause (int* clause) {
printf ("[%i] ", clause[ID]);
while (*clause) printf ("%i ", *clause++); printf ("0\n"); }
static inline void addWatchPtr (struct solver* S, int lit, long watch) {
if (S->used[lit] + 1 == S->max[lit]) { S->max[lit] *= 1.5;
S->wlist[lit] = (long *) realloc (S->wlist[lit], sizeof (long) * S->max[lit]);
if (S->wlist[lit] == NULL) { printf("c MEMOUT: reallocation failed for watch list of %i\n", lit); exit (0); } }
S->wlist[lit][ S->used[lit]++ ] = watch | S->mask;
S->wlist[lit][ S->used[lit] ] = END; }
static inline void addWatch (struct solver* S, int* clause, int index) {
addWatchPtr (S, clause[index], ((long) (((clause) - S->DB)) << 1)); }
static inline void removeWatch (struct solver* S, int* clause, int index) {
int i, lit = clause[index];
if ((S->used[lit] > INIT) && (S->max[lit] > 2 * S->used[lit])) {
S->max[lit] = (3 * S->used[lit]) >> 1;
S->wlist[lit] = (long *) realloc (S->wlist[lit], sizeof (long) * S->max[lit]);
assert(S->wlist[lit] != NULL); }
long *watch = S->wlist[lit];
for (i = 0; i < S->used[lit]; i++) {
int* _clause = S->DB + (*(watch++) >> 1);
if (_clause == clause) {
watch[-1] = S->wlist[lit][ --S->used[lit] ];
S->wlist[lit][ S->used[lit] ] = END; return; } } }
static inline void addUnit (struct solver* S, long index) {
// printf("c adding unit %i\n", S->DB[index]);
if (S->unitSize >= S->unitStackSize) {
S->unitStackSize = (S->unitStackSize * 3) >> 1;
S->unitStack = (long*) realloc (S->unitStack, sizeof(long) * S->unitStackSize);
if (S->unitStack == NULL) {
printf ("c failed to reallocate unit stack\n"); exit (0); } }
S->unitStack[S->unitSize++] = index; }
static inline void removeUnit (struct solver* S, int lit) {
int i, found = 0;
for (i = 0; i < S->unitSize; i++) {
if (found) S->unitStack[i - 1] = S->unitStack[i];
if (S->DB[ S->unitStack[i] ] == lit) found = 1; }
S->unitSize--; }
static inline void unassignUnit (struct solver* S, int lit) {
if (S->verb)
printf ("\rc removing unit %i\n", lit);
while (S->falseA[-lit]) {
if (S->verb)
printf ("\rc removing unit %i (%i)\n", S->forced[-1], lit);
S->falseA[*(--S->forced)] = 0;
S->reason[abs(*S->forced)] = 0; }
S->processed = S->assigned = S->forced; }
static inline void markWatch (struct solver* S, int* clause, int index, int offset) {
long* watch = S->wlist[ clause[ index ] ];
for (;;) {
int *_clause = (S->DB + (*(watch++) >> 1) + (long) offset);
if (_clause == clause) { watch[ID] |= ACTIVE; return; } } }
static inline void addDependency (struct solver* S, int dep, int forced) {
if (1 || S->traceFile || S->lratFile) { // temporary for MAXDEP
if (S->nDependencies == S->maxDependencies) {
S->maxDependencies = (S->maxDependencies * 3) >> 1;
// printf ("c dependencies increased to %i\n", S->maxDependencies);
S->dependencies = realloc (S->dependencies, sizeof (int) * S->maxDependencies);
if (S->dependencies == NULL) { printf ("c MEMOUT: dependencies reallocation failed\n"); exit (0); } }
// printf("c adding dep %i\n", (dep << 1) + forced);
S->dependencies[S->nDependencies++] = (dep << 1) + forced; } }
static inline void markClause (struct solver* S, int* clause, int index) {
S->nResolve++;
addDependency (S, clause[index - 1] >> 1, (S->assigned > S->forced));
if ((clause[index + ID] & ACTIVE) == 0) {
S->nActive++;
clause[index + ID] |= ACTIVE;
//#ifdef PARTIALPROOF
// if ((clause + index) > (S->DB + S->firstLemma)) // don't delete original clauses
//#endif
if ((S->mode == BACKWARD_UNSAT) && clause[index + 1]) {
S->optproof[S->nOpt++] = (((long) (clause - S->DB) + index) << INFOBITS) + 1; }
if (clause[1 + index] == 0) return;
markWatch (S, clause, index, -index);
markWatch (S, clause, 1 + index, -index); }
while (*clause) S->falseA[*(clause++)] = MARK; }
void analyze (struct solver* S, int* clause, int index) { // Mark all clauses involved in conflict
markClause (S, clause, index);
while (S->assigned > S->falseStack) {
int lit = *(--S->assigned);
if (S->falseA[lit] == MARK) {
if (S->reason[abs (lit)]) {
markClause (S, S->DB + S->reason[abs (lit)], -1);
if (S->assigned >= S->forced)
S->reason[abs (lit)] = 0; } }
#ifndef PARTIALPROOF
else if (S->falseA[lit] == ASSUMED && !S->RATmode && S->reduce && !S->lratFile) { // Remove unused literal
S->nRemoved++;
int *tmp = S->current;
while (*tmp != lit) tmp++;
while (*tmp) { tmp[0] = tmp[1]; tmp++; }
tmp[-1] = 0; }
#endif
if (S->assigned >= S->forced) S->reason[abs (lit)] = 0;
S->falseA[lit] = (S->assigned < S->forced); }
S->processed = S->assigned = S->forced; }
void noAnalyze (struct solver* S) {
while (S->assigned > S->falseStack) {
int lit = *(--S->assigned);
if (S->assigned >= S->forced) S->reason[abs (lit)] = 0;
S->falseA[lit] = (S->assigned < S->forced); }
S->processed = S->assigned = S->forced; }
int propagate (struct solver* S, int init, int mark) { // Performs unit propagation (init not used?)
int *start[2];
int check = 0, mode = !S->prep;
int i, lit, _lit = 0; long *watch, *_watch;
start[0] = start[1] = S->processed;
flip_check:;
check ^= 1;
while (start[check] < S->assigned) { // While unprocessed false literals
lit = *(start[check]++); // Get first unprocessed literal
if (lit == _lit) watch = _watch;
else watch = S->wlist[ lit ]; // Obtain the first watch pointer
while (*watch != END) { // While there are watched clauses (watched by lit)
if ((*watch & mode) != check) {
watch++; continue; }
int *clause = S->DB + (*watch >> 1); // Get the clause from DB
if (S->falseA[ -clause[0] ] ||
S->falseA[ -clause[1] ]) {
watch++; continue; }
if (clause[0] == lit) clause[0] = clause[1]; // Ensure that the other watched literal is in front
for (i = 2; clause[i]; ++i) // Scan the non-watched literals
if (S->falseA[ clause[i] ] == 0) { // When clause[j] is not false, it is either true or unset
clause[1] = clause[i]; clause[i] = lit; // Swap literals
addWatchPtr (S, clause[1], *watch); // Add the watch to the list of clause[1]
*watch = S->wlist[lit][ --S->used[lit] ]; // Remove pointer
S->wlist[lit][ S->used[lit] ] = END;
goto next_clause; } // Goto the next watched clause
clause[1] = lit; watch++; // Set lit at clause[1] and set next watch
if (!S->falseA[ clause[0] ]) { // If the other watched literal is falsified,
assign (S, clause[0]); // A unit clause is found, and the reason is set
S->reason[abs (clause[0])] = ((long) ((clause)-S->DB)) + 1;
if (!check) {
start[0]--; _lit = lit; _watch = watch;
goto flip_check; } }
else if (!mark) { noAnalyze (S); return UNSAT; }
else { analyze (S, clause, 0); return UNSAT; } // Found a root level conflict -> UNSAT
next_clause: ; } } // Set position for next clause
if (check) goto flip_check;
S->processed = S->assigned;
return SAT; } // Finally, no conflict was found
// Propagate top level units
static inline int propagateUnits (struct solver* S, int init) {
int i;
// printf("c propagateUnits %i\n", S->unitSize);
while (S->forced > S->falseStack) {
S->falseA[*(--S->forced)] = 0;
S->reason[abs (*S->forced)] = 0; }
S->forced = S->assigned = S->processed = S->falseStack;
for (i = 0; i < S->unitSize; i++) {
int lit = S->DB[ S->unitStack[i] ];
S->reason[abs (lit)] = S->unitStack[i] + 1;
assign (S, lit); }
if (propagate (S, init, 1) == UNSAT) { return UNSAT; }
S->forced = S->processed;
return SAT; }
// Put falsified literals at the end and returns the size under the current
// assignment: negative size means satisfied, size = 0 means falsified
int sortSize (struct solver *S, int *lemma) {
unsigned int size = 0, last = 0, sat = 1;
while (lemma[last]) {
int lit = lemma[last++];
if (S->falseA[lit] == 0) {
if (S->falseA[-lit]) sat = -1;
lemma[last-1] = lemma[ size ];
lemma[size++] = lit; } }
return sat * size; }
// print the core clauses to coreFile in DIMACS format
void printCore (struct solver *S) {
int i, j;
for (i = 0; i < S->nClauses; i++) {
int *clause = S->DB + (S->formula[i] >> INFOBITS);
if (clause[ID] & ACTIVE) S->COREcount++; }
printf ("\rc %i of %li clauses in core \n", S->COREcount, S->nClauses);
if (S->coreStr) {
FILE *coreFile = fopen (S->coreStr, "w");
fprintf (coreFile, "p cnf %i %i\n", S->nVars, S->COREcount);
for (i = 0; i < S->nClauses; i++) {
int *clause = S->DB + (S->formula[i] >> INFOBITS);
if (clause[ID] & ACTIVE) {
while (*clause) fprintf (coreFile, "%i ", *clause++);
fprintf (coreFile, "0\n"); } }
fclose (coreFile); } }
void write_lit (struct solver *S, FILE *output, int lit) { // change to long?
unsigned int l = abs (lit) << 1;
if (lit < 0) l++;
do {
if (l <= 127) { fputc ((char) l, output); }
else { fputc ((char) (128 + (l & 127)), output); }
S->nWrites++;
l = l >> 7; }
while (l); }
void printLRATline (struct solver *S, int time) {
int *line = S->lratTable + S->lratLookup[time];
if (S->binOutput) {
fputc ('a', S->lratFile); S->nWrites++;
while (*line) write_lit (S, S->lratFile, *line++);
write_lit (S, S->lratFile, *line++);
while (*line) write_lit (S, S->lratFile, *line++);
write_lit (S, S->lratFile, *line++); }
else {
while (*line) fprintf (S->lratFile, "%i ", *line++);
fprintf (S->lratFile, "%i ", *line++);
while (*line) fprintf (S->lratFile, "%i ", *line++);
fprintf (S->lratFile, "%i\n", *line++); } }
// print the core lemmas to lemmaFile in DRAT format
void printProof (struct solver *S) {
int step;
printf ("\rc %i of %i lemmas in core using %lu resolution steps\n", S->nActive - S->COREcount + 1, S->nLemmas + 1, S->nResolve);
printf ("\rc %d RAT lemmas in core; %i redundant literals in core lemmas\n", S->RATcount, S->nRemoved);
// NB: not yet working with forward checking
if (S->mode == FORWARD_UNSAT) {
printf ("\rc optimized proofs are not supported for forward checking\n");
return; }
// replace S->proof by S->optproof
if (S->mode == BACKWARD_UNSAT) {
if (S->nOpt > S->nAlloc) {
S->nAlloc = S->nOpt;
S->proof = (long*) realloc (S->proof, sizeof (long) * S->nAlloc);
if (S->proof == NULL) { printf("c MEMOUT: reallocation of proof list failed\n"); exit (0); } }
S->nStep = 0;
S->nLemmas = 0;
for (step = S->nOpt - 1; step >= 0; step--) {
long ad = S->optproof[step];
int *lemmas = S->DB + (ad >> INFOBITS);
if ((ad & 1) == 0) S->nLemmas++;
// if (lemmas[ID] & ACTIVE) lemmas[ID] ^= ACTIVE; // only useful for checking multiple times?
S->proof[S->nStep++] = S->optproof[step]; } } // why not reuse ad?
if (S->lemmaStr) {
FILE *lemmaFile = fopen (S->lemmaStr, "w");
for (step = 0; step < S->nStep; step++) {
long ad = S->proof[step];
int *lemmas = S->DB + (ad >> INFOBITS);
if (!lemmas[1] && (ad & 1)) continue; // don't delete unit clauses
if (S->binOutput) {
S->nWrites++;
if (ad & 1) fputc ('d', lemmaFile);
else fputc ('a', lemmaFile); }
else if (ad & 1) fprintf (lemmaFile, "d ");
int reslit = lemmas[PIVOT];
while (*lemmas) {
int lit = *lemmas++;
if (lit == reslit) {
if (S->binOutput) write_lit (S, lemmaFile, lit);
else fprintf (lemmaFile, "%i ", lit); } }
lemmas = S->DB + (ad >> INFOBITS);
while (*lemmas) {
int lit = *lemmas++;
if (lit != reslit) {
if (S->binOutput) write_lit (S, lemmaFile, lit);
else fprintf (lemmaFile, "%i ", lit); } }
if (S->binOutput) write_lit (S, lemmaFile, 0);
else fprintf (lemmaFile, "0\n"); }
if (S->binOutput) { fputc ('a', lemmaFile); write_lit (S, lemmaFile, 0); }
else fprintf (lemmaFile, "0\n");
fclose (lemmaFile); }
if (S->lratFile) {
int lastAdded = S->nClauses;
int flag = 0;
for (step = 0; step < S->nStep; step++) {
long ad = S->proof[step];
int *lemmas = S->DB + (ad >> INFOBITS);
if ((ad & 1) == 0) {
if (lastAdded == 0) {
if (S->binOutput) {
write_lit (S, S->lratFile, 0); }
else {
fprintf (S->lratFile, "0\n"); } }
lastAdded = lemmas[ID] >> 1;
printLRATline (S, lastAdded); }
else if (lastAdded == S->nClauses) continue;
else if (!lemmas[1] && (ad & 1)) continue; // don't delete unit clauses
else if (ad & 1) {
if (lastAdded != 0) {
if (S->binOutput) {
fputc ('d', S->lratFile); S->nWrites++; }
else {
fprintf (S->lratFile, "%i d ", lastAdded); } }
lastAdded = 0;
if (S->binOutput) {
write_lit (S, S->lratFile, lemmas[ID] >> 1); }
else {
fprintf (S->lratFile, "%i ", lemmas[ID] >> 1); } } }
if (lastAdded != S->nClauses) {
if (S->binOutput) {
write_lit (S, S->lratFile, 0); }
else {
fprintf(S->lratFile, "0\n"); } }
printLRATline (S, S->count);
fclose (S->lratFile);
if (S->nWrites)
printf ("c wrote optimized proof in LRAT format of %li bytes\n", S->nWrites); } }
void printNoCore (struct solver *S) {
if (S->lratFile) {
if (S->binOutput) {
fputc ('d', S->lratFile); S->nWrites++; }
else {
fprintf (S->lratFile, "%ld d ", S->nClauses); }
int i;
for (i = 0; i < S->nClauses; i++) {
int *clause = S->DB + (S->formula[i] >> INFOBITS);
if ((clause[ID] & ACTIVE) == 0) {
if (S->binOutput) {
write_lit (S, S->lratFile, clause[ID] >> 1); }
else {
fprintf (S->lratFile, "%i ", clause[ID] >> 1); } } }
if (S->binOutput) {
write_lit (S, S->lratFile, 0); }
else {
fprintf (S->lratFile, "0\n"); } } }
// print the dependency graph to traceFile in TraceCheck+ format
// this procedure adds the active clauses at the end of the trace
void printTrace (struct solver *S) {
if (S->traceFile) { int i;
for (i = 0; i < S->nClauses; i++) {
int *clause = S->DB + (S->formula[i] >> INFOBITS);
if (clause[ID] & ACTIVE) {
fprintf (S->traceFile, "%i ", i + 1);
while (*clause) fprintf (S->traceFile, "%i ", *clause++);
fprintf (S->traceFile, "0 0\n"); } }
fclose (S->traceFile); } }
void printActive (struct solver *S) {
int i, j;
if (S->activeFile) {
for (i = -S->maxVar; i <= S->maxVar; i++)
if (i != 0)
for (j = 0; j < S->used[i]; j++) {
int *clause = S->DB + (S->wlist[i][j] >> 1);
if (*clause == i) {
while (*clause)
fprintf (S->activeFile, "%i ", *clause++);
fprintf (S->activeFile, "0\n"); } } } }
void postprocess (struct solver *S) {
printNoCore (S); // print before proof optimization
printActive (S);
printCore (S);
printTrace (S); // closes traceFile
printProof (S); } // closes lratFile
void lratAdd (struct solver *S, int elem) {
if (S->lratSize == S->lratAlloc) {
S->lratAlloc = S->lratAlloc * 3 >> 1;
S->lratTable = (int *) realloc (S->lratTable, sizeof (int) * S->lratAlloc);
if (S->lratTable == NULL) {
printf ("c MEMOUT: failed to reallocate lrat table\n"); exit (0); } }
S->lratTable[S->lratSize++] = elem; }
void printDependenciesFile (struct solver *S, int* clause, int RATflag, int mode) {
FILE *file = NULL;
if (mode == 0) file = S->traceFile;
if (mode == 1) file = S->lratFile;
if (file) {
int i, j, k;
int tmp = S->lratSize;
if (clause != NULL) {
S->lratLookup[clause[ID] >> 1] = S->lratSize; }
else { S->lratLookup[S->count] = S->lratSize; }
if (clause != NULL) {
int size = 0;
int *sortClause;
sortClause = (int *) malloc (sizeof(int) * S->maxSize);
assert (sortClause != NULL);
lratAdd (S, S->time >> 1); // NB: long to ing
int reslit = clause[PIVOT];
while (*clause) {
if (*clause == reslit)
lratAdd (S, reslit);
sortClause[size++] = *clause++; }
qsort (sortClause, size, sizeof (int), abscompare);
for (i = 0; i < size; i++) {
int lit = sortClause[i];
if (lit != reslit)
lratAdd (S, lit); }
free (sortClause); }
else { lratAdd (S, S->count); }
lratAdd (S, 0);
int isRUP = 1;
for (i = 0; i < S->nDependencies; i++)
if (S->dependencies[i] < 0) { isRUP = 0; break; }
if (isRUP) {
for (i = S->nDependencies - 1; i >= 0; i--)
lratAdd (S, S->dependencies[i] >> 1);
lratAdd (S, 0);
goto printLine; }
// first print the preRAT units in order of becoming unit
int size = 0;
for (i = 0; i < S->nDependencies; i++) {
if (S->dependencies[i] > 0) continue;
for (j = i - 1; j >= 0 && S->dependencies[j] > 0; j--) {
int flag = 0;
int cls = S->dependencies[j];
if (cls & 1) continue;
for (k = 0; k < size; k++)
if (S->preRAT[k] == cls) flag = 1;
if (!flag) {
S->preRAT[size++] = cls;
lratAdd (S, cls >> 1); } } }
// print dependencies in order of becoming unit
for (i = S->nDependencies - 1; i >= 0; i--) {
int cls = S->dependencies[i];
if ((mode == 0) && (cls < 0)) continue;
if (mode == 0) {
int flag = 0;
for (j = 0; j < size; j++)
if (S->preRAT[j] == cls) flag = 1;
if (!flag) {
S->preRAT[size++] = cls;
lratAdd (S, cls >> 1); } }
if ((mode == 1) && (cls & 1))
lratAdd (S, cls >> 1); }
lratAdd (S, 0);
printLine:;
if (mode == 0) {
for (i = tmp; i < S->lratSize; i++)
fprintf (file, "%d ", S->lratTable[i]);
S->lratSize = tmp;
fprintf (file, "\n"); } } }
void printDependencies (struct solver *S, int* clause, int RATflag) {
if (clause != NULL) {
int i;
clause[MAXDEP] = 0;
for (i = 0; i < S->nDependencies; i++) {
if (S->dependencies[i] > clause[MAXDEP])
clause[MAXDEP] = S->dependencies[i]; }
assert (clause[MAXDEP] < clause[ID]); }
printDependenciesFile (S, clause, RATflag, 0);
printDependenciesFile (S, clause, RATflag, 1); }
int checkRAT (struct solver *S, int pivot, int mark) {
int i, j, nRAT = 0;
// Loop over all literals to calculate resolution candidates
for (i = -S->maxVar; i <= S->maxVar; i++) {
if (i == 0) continue;
// Loop over all watched clauses for literal
for (j = 0; j < S->used[i]; j++) {
int* watched = S->DB + (S->wlist[i][j] >> 1);
int id = watched[ID] >> 1;
int active = watched[ID] & ACTIVE;
if (*watched == i) { // If watched literal is in first position
while (*watched)
if (*watched++ == -pivot) {
if ((S->mode == BACKWARD_UNSAT) && !active) {
// printf ("\rc RAT check ignores unmarked clause : "); printClause (S->DB + (S->wlist[i][j] >> 1));
continue; }
if (nRAT == S->maxRAT) {
S->maxRAT = (S->maxRAT * 3) >> 1;
S->RATset = realloc (S->RATset, sizeof (long) * S->maxRAT);
assert (S->RATset != NULL); }
S->RATset[nRAT++] = S->wlist[i][j] >> 1;
break; } } } }
// S->prep = 1;
// Check all clauses in RATset for RUP
int flag = 1;
qsort (S->RATset, nRAT, sizeof (long), compare);
S->nDependencies = 0;
for (i = nRAT - 1; i >= 0; i--) {
int* RATcls = (int*) (S->DB + S->RATset[i]);
int id = RATcls[ID] >> 1;
int blocked = 0;
long int reason = 0;
if (S->verb) {
printf ("\rc RAT clause: "); printClause (RATcls); }
while (*RATcls) {
int lit = *RATcls++;
if (lit != -pivot && S->falseA[-lit])
if (!blocked || reason > S->reason[abs (lit)])
blocked = lit, reason = S->reason[abs (lit)]; }
if (blocked && reason) {
analyze (S, S->DB + reason, -1);
S->reason[abs (blocked)] = 0; }
if (!blocked) {
RATcls = (int*) (S->DB + S->RATset[i]);
while (*RATcls) {
int lit = *RATcls++;
if (lit != -pivot && !S->falseA[lit]) {
assign (S, -lit); S->reason[abs (lit)] = 0; } }
if (propagate (S, 0, mark) == SAT) { flag = 0; break; } }
addDependency (S, -id, 1); }
if (flag == 0) {
while (S->forced < S->assigned) {
S->falseA[*(--S->assigned)] = 0;
S->reason[abs (*S->assigned)] = 0; }
if (S->verb) printf ("\rc RAT check on pivot %i failed\n", pivot);
return FAILED; }
return SUCCESS; }
int redundancyCheck (struct solver *S, int *clause, int size, int mark) {
int i, indegree;
int falsePivot = S->falseA[clause[PIVOT]];
if (S->verb) { printf ("\rc checking lemma (%i, %i) ", size, clause[PIVOT]); printClause (clause); }
if (S->mode != FORWARD_UNSAT) {
if ((clause[ID] & ACTIVE) == 0) return SUCCESS; } // redundant?
// clause[PIVOT] ^= ACTIVE; }
if (size < 0) {
S->DB[ S->reason[abs (*clause)] - 2] |= 1;
return SUCCESS; }
indegree = S->nResolve;
S->RATmode = 0;
S->nDependencies = 0;
for (i = 0; i < size; ++i) {
if (S->falseA[-clause[i]]) { // should only occur in forward mode
if (S->warning != NOWARNING) {
printf ("\rc WARNING: found a tautological clause in proof: "); printClause (clause); }
if (S->warning == HARDWARNING) exit (HARDWARNING);
while (S->forced < S->assigned) {
S->falseA[*(--S->assigned)] = 0;
S->reason[abs (*S->assigned)] = 0; }
return SUCCESS; }
S->falseA[clause[i]] = ASSUMED;
*(S->assigned++) = clause[i];
S->reason[abs (clause[i])] = 0; }
S->current = clause;
if (propagate (S, 0, mark) == UNSAT) {
indegree = S->nResolve - indegree;
if (indegree <= 2 && S->prep == 0) {
S->prep = 1; if (S->verb) printf ("\rc [%li] preprocessing checking mode on\n", S->time); }
if (indegree > 2 && S->prep == 1) {
S->prep = 0; if (S->verb) printf ("\rc [%li] preprocessing checking mode off\n", S->time); }
if (S->verb) printf ("\rc lemma has RUP\n");
printDependencies (S, clause, 0);
return SUCCESS; }
// Failed RUP check. Now test RAT.
// printf ("RUP check failed. Starting RAT check.\n");
if (S->rupOnly) {
printf ("\rc RUP check failed\n");
return FAILED; }
int reslit = clause[PIVOT];
if (S->verb)
printf ("\rc RUP checked failed; starting RAT check on pivot %d.\n", reslit);
if (falsePivot) return FAILED;
int* savedForced = S->forced;
S->RATmode = 1;
S->forced = S->assigned;
int failed = 0;
if (checkRAT (S, reslit, mark) == FAILED) {
failed = 1;
if (S->warning != NOWARNING) {
printf ("\rc WARNING: RAT check on proof pivot failed : "); printClause (clause); }
if (S->warning == HARDWARNING) exit (HARDWARNING);
for (i = 0; i < size; i++) {
if (clause[i] == reslit) continue;
if (checkRAT (S, clause[i], mark) == SUCCESS) {
clause[PIVOT] = clause[i];
failed = 0; break; } } }
if (failed == 0)
printDependencies (S, clause, 1);
S->processed = S->forced = savedForced;
while (S->forced < S->assigned) {
S->falseA[*(--S->assigned)] = 0;
S->reason[abs (*S->assigned)] = 0; }
if (failed) {
printf ("c RAT check failed on all possible pivots\n");
return FAILED; }
if (mark) S->RATcount++;
if (S->verb) printf ("\rc lemma has RAT on %i\n", clause[PIVOT]);
return SUCCESS; }
int init (struct solver *S) {
S->forced = S->falseStack; // Points inside *falseStack at first decision (unforced literal)
S->processed = S->falseStack; // Points inside *falseStack at first unprocessed literal
S->assigned = S->falseStack; // Points inside *falseStack at last unprocessed literal
// initialize watch pointers on the original clauses
S->RATmode = 0;
S->nRemoved = 0;
S->nOpt = 0;
S->nResolve = 0;
S->RATcount = 0;
S->nActive = 0;
S->COREcount = 0;
S->unitSize = 0;
int i;
for (i = 1; i <= S->maxVar; ++i) {
S->reason [i] = 0;
S->falseStack[i] = 0;
S->falseA[i] = S->falseA[-i] = 0;
S->used [i] = S->used [-i] = 0;
S->wlist [i][0] = S->wlist [-i][0] = END; }
for (i = 0; i < S->nClauses; i++) {
int *clause = S->DB + (S->formula[i] >> INFOBITS);
if (clause[ID] & ACTIVE) clause[ID] ^= ACTIVE;
if (clause[0] == 0) {
printf ("\rc formula contains empty clause\n");
if (S->coreStr) {
FILE *coreFile = fopen (S->coreStr, "w");
fprintf (coreFile, "p cnf 0 1\n 0\n");
fclose (coreFile); }
if (S->lemmaStr) {
FILE *lemmaFile = fopen (S->lemmaStr, "w");
fprintf (lemmaFile, "0\n");
fclose (lemmaFile); }
return UNSAT; }
if (clause[1]) { addWatch (S, clause, 0); addWatch (S, clause, 1); }
else if (S->falseA[clause[0]]) {
printf ("\rc found complementary unit clauses: %i\n", clause[0]);
if (S->coreStr) {
FILE *coreFile = fopen (S->coreStr, "w");
fprintf (coreFile, "p cnf %i 2\n%i 0\n%i 0\n", abs (clause[0]), clause[0], -clause[0]);
fclose (coreFile); }
if (S->lemmaStr) {
FILE *lemmaFile = fopen (S->lemmaStr, "w");
fprintf (lemmaFile, "0\n");
fclose (lemmaFile); }
if (S->lratFile) {
int j;
for (j = 0; j < i; j++) {
int *_clause = S->DB + (S->formula[j] >> INFOBITS);
if ((_clause[0] == -clause[0]) && !_clause[1]) break; }
fprintf (S->lratFile, "%li 0 %i %i 0\n", S->nClauses + 1, j + 1, i + 1); }
return UNSAT; }
else if (!S->falseA[ -clause[0] ]) {
addUnit (S, (long) (clause - S->DB));
assign (S, clause[0]); } }
S->nDependencies = 0;
S->time = S->count; // Alternative time init
if (propagateUnits (S, 1) == UNSAT) {
printf ("\rc UNSAT via unit propagation on the input instance\n");
printDependencies (S, NULL, 0);
postprocess (S); return UNSAT; }
return SAT; }
int verify (struct solver *S, int begin, int end) {
int top_flag = 1;
if (init (S) == UNSAT) return UNSAT;
if (S->mode == FORWARD_UNSAT) {
if (begin == end)
printf ("\rc start forward verification\n"); }
int step;
int adds = 0;
int active = S->nClauses;
for (step = 0; step < S->nStep; step++) {
if (step >= begin && step < end) continue;
long ad = S->proof[step]; long d = ad & 1;
int *lemmas = S->DB + (ad >> INFOBITS);
S->time = lemmas[ID];
if (d) { active--; }
else { active++; adds++; }
if (S->mode == FORWARD_SAT && S->verb) printf ("\rc %i active clauses\n", active);
if (!lemmas[1]) { // found a unit
int lit = lemmas[0];
if (S->verb)
printf ("\rc found unit in proof %i [%li]\n", lit, S->time);
if (d) {
if (S->mode == FORWARD_SAT) {
removeUnit (S, lit); propagateUnits (S, 0); }
else { // no need to remove units while checking UNSAT
if (S->verb) { printf("c removing proof step: d "); printClause(lemmas); }
S->proof[step] = 0; continue; } }
else {
// printf ("c unit %i\n", lit);
// if (S->falseA[-lit]) S->reason[abs(lit)] = (lemmas - S->DB) + 1;
if (S->mode == BACKWARD_UNSAT && S->falseA[-lit]) { S->proof[step] = 0; continue; }
else { addUnit (S, (long) (lemmas - S->DB)); } } }
if (d && lemmas[1]) { // if delete and not unit
if ((S->reason[abs (lemmas[0])] - 1) == (lemmas - S->DB)) { // what is this check?
if (S->mode != FORWARD_SAT) { // ignore pseudo unit clause deletion
if (S->warning != NOWARNING) {
if (S->verb) { printf ("\rc WARNING: ignoring deletion instruction %li: ", (lemmas - S->DB)); printClause (lemmas); } }
if (S->warning == HARDWARNING) exit (HARDWARNING);
if (S->verb) { printf ("c ignoring deletion instruction %li: ", (lemmas - S->DB)); printClause (lemmas); }
// if (S->mode == BACKWARD_UNSAT) { // ignore pseudo unit clause deletion
S->proof[step] = 0; }
else { // if (S->mode == FORWARD_SAT) { // also for FORWARD_UNSAT?
removeWatch (S, lemmas, 0), removeWatch (S, lemmas, 1);
propagateUnits (S, 0); } }
else {
removeWatch (S, lemmas, 0), removeWatch (S, lemmas, 1); }
if (S->mode == FORWARD_UNSAT ) continue; // Ignore deletion of top-level units
if (S->mode == BACKWARD_UNSAT) continue; }
int size = sortSize (S, lemmas); // after removal of watches
if (d && S->mode == FORWARD_SAT) {
if (size == -1) propagateUnits (S, 0); // necessary?
if (redundancyCheck (S, lemmas, size, 1) == FAILED) {
printf ("c failed at proof line %i (modulo deletion errors)\n", step + 1);
return SAT; }
continue; }
if (d == 0 && S->mode == FORWARD_UNSAT) {
if (step > end) {
if (size < 0) continue; // Fix of bus error: 10
if (redundancyCheck (S, lemmas, size, 1) == FAILED) {
printf ("c failed at proof line %i (modulo deletion errors)\n", step + 1);
return SAT; }
size = sortSize (S, lemmas);
S->nDependencies = 0; } }
if (lemmas[1])
addWatch (S, lemmas, 0), addWatch (S, lemmas, 1);
if (size == 0) { printf ("\rc conflict claimed, but not detected\n"); return SAT; } // change to FAILED?
if (size == 1) {
if (S->verb) printf ("\rc found unit %i\n", lemmas[0]);
assign (S, lemmas[0]); S->reason[abs (lemmas[0])] = ((long) ((lemmas)-S->DB)) + 1;
if (propagate (S, 1, 1) == UNSAT) goto start_verification;
S->forced = S->processed; } }
if (S->mode == FORWARD_SAT && active == 0) {
postprocess (S); return UNSAT; }
if (S->mode == FORWARD_UNSAT) {
if (begin == end) {
postprocess (S);
printf ("\rc VERIFIED derivation: all lemmas preserve satisfiability\n");
if (S->warning != NOWARNING) {
printf ("\rc WARNING: no empty clause detected, this is not a refutation\n"); }
return DERIVATION; }
return SAT; }
if (S->mode == BACKWARD_UNSAT) {
if (S->backforce) {
int s;
for (s = 0; s < step; s++) {
long ad = S->proof[s];
int *clause = S->DB + (ad >> INFOBITS);
if (sortSize(S, clause) >= 0) {
if ( (ad & 1) && (clause[ID] & ACTIVE)) clause[ID] ^= ACTIVE;
if (!(ad & 1)) clause[ID] |= ACTIVE; } } }
if (!S->backforce) {
printf ("\rc ERROR: no conflict\n");
return SAT;
} }
start_verification:;
if (S->mode == FORWARD_UNSAT) {
printDependencies (S, NULL, 0);
postprocess (S); return UNSAT; }
if (!S->backforce)
printDependencies (S, NULL, 0);
if (S->mode == FORWARD_SAT) {
printf ("\rc ERROR: found empty clause during SAT check\n"); exit (0); }
printf ("\rc detected empty clause; start verification via backward checking\n");
S->forced = S->processed;
assert (S->mode == BACKWARD_UNSAT); // only reachable in BACKWARD_UNSAT mode
S->nOpt = 0;
int checked = 0, skipped = 0;
double max = (double) adds;
struct timeval backward_time;
gettimeofday (&backward_time, NULL);
for (; step >= 0; step--) {
struct timeval current_time;
gettimeofday (¤t_time, NULL);
int seconds = (int) (current_time.tv_sec - S->start_time.tv_sec);
if ((seconds > S->timeout) && (S->optimize == 0)) printf ("s TIMEOUT\n"), exit (0);
if (S->bar)
if ((adds % 1000) == 0) {
int f;
long runtime = (current_time.tv_sec - backward_time.tv_sec ) * 1000000 +
(current_time.tv_usec - backward_time.tv_usec);
double time = (double) (runtime / 1000000.0);
double fraction = (adds * 1.0) / max;
printf("\rc %.2f%% [", 100.0 * (1.0 - fraction));
for (f = 1; f <= 20; f++) {
if ((1.0 - fraction) * 20.0 < 1.0 * f) printf(" ");
else printf("="); }
printf("] time remaining: %.2f seconds ", time / (1.0 - fraction) - time);
if (step == 0) printf("\n");
fflush (stdout); }
long ad = S->proof[step]; long d = ad & 1;
int *clause = S->DB + (ad >> INFOBITS);
if (ad == 0) continue; // Skip lemma that has been removed from proof
if ( d == 0) {
adds--;
if (clause[1]) {
removeWatch (S, clause, 0), removeWatch (S, clause, 1);
if (S->reason[abs (clause[0])] == (clause + 1 - S->DB)) { // use this check also for units?
unassignUnit (S, clause[0]); } }
else unassignUnit (S, clause[0]); }
int size = sortSize (S, clause);
if (d) {
if (S->verb) { printf ("\rc adding clause (%i) ", size); printClause (clause); }
addWatch (S, clause, 0), addWatch (S, clause, 1); continue; }
S->time = clause[ID];
if ((S->time & ACTIVE) == 0) {
skipped++;
// if ((skipped % 100) == 0) printf("c skipped %i, checked %i\n", skipped, checked);
continue; } // If not marked, continue
assert (size >= 1);
int *_clause = clause + size;
while (*_clause++) { S->nRemoved++; }
clause[size] = 0;
if (S->verb) {
printf ("\rc validating clause (%i, %i): ", clause[PIVOT], size); printClause (clause); }
if (redundancyCheck (S, clause, size, 1) == FAILED) {
printf ("c failed at proof line %i (modulo deletion errors)\n", step + 1);
return SAT; }
checked++;
S->optproof[S->nOpt++] = ad; }
postprocess (S);
return UNSAT; }
long matchClause (struct solver* S, long *clauselist, int listsize, int* input, int size) {
int i, j;
for (i = 0; i < listsize; ++i) {
int *clause = S->DB + clauselist[i];
for (j = 0; j <= size; j++)
if (clause[j] != input[j]) goto match_next;
long result = clauselist[i];
clauselist[i] = clauselist[--listsize];
return result;
match_next:; }
return 0; }
unsigned int getHash (int* input) {
unsigned int sum = 0, prod = 1, xor = 0;
while (*input) {
prod *= *input; sum += *input; xor ^= *input; input++; }
return (1023 * sum + prod ^ (31 * xor)) % BIGINIT; }
int read_lit (struct solver *S, int *lit) {
int l = 0, lc, shift = 0;
do {
lc = getc_unlocked (S->proofFile);
S->nReads++;
if ((shift == 0) && (lc == EOF)) return EOF;
l |= (lc & 127) << shift;
shift += 7; }
while (lc > 127);
if (l % 2) *lit = (l >> 1) * -1;
else *lit = (l >> 1);
return 1; }
void deactivate (struct solver *S) {
S->nActive = 0;
int step;
for (step = 0; step < S->nStep; step++) {
if ((S->proof[step] & 1) == 0) {