-
Notifications
You must be signed in to change notification settings - Fork 3
/
definition.c
165 lines (161 loc) · 5.21 KB
/
definition.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
#include "definition.h"
#include "macros.h"
#include "ruler.h"
#include "simplify.h"
static bool find_binary_and_gate_clauses (struct simplifier *simplifier,
unsigned lit,
struct clause *clause,
struct clauses *gate,
struct clauses *nogate) {
assert (!clause->garbage);
struct ruler *ruler = simplifier->ruler;
size_t clause_size_limit = ruler->limits.clause_size_limit;
if (clause->size > clause_size_limit)
return false;
CLEAR (*gate);
CLEAR (*nogate);
signed char *marks = simplifier->marks;
for (all_literals_in_clause (other, clause))
if (other != lit)
marks[other] = 1;
unsigned not_lit = NOT (lit);
struct clauses *not_lit_clauses = &OCCURRENCES (not_lit);
unsigned marked = 0;
for (all_clauses (not_lit_clause, *not_lit_clauses))
if (is_binary_pointer (not_lit_clause)) {
unsigned other = other_pointer (not_lit_clause);
unsigned not_other = NOT (other);
if (marks[not_other]) {
PUSH (*gate, not_lit_clause);
marks[not_other] = 0;
marked++;
} else
PUSH (*nogate, not_lit_clause);
} else
PUSH (*nogate, not_lit_clause);
for (all_literals_in_clause (other, clause))
if (other != lit)
marks[other] = 0;
assert (marked < clause->size);
return marked + 1 == clause->size;
}
static struct clause *find_and_gate (struct simplifier *simplifier,
unsigned lit, struct clauses *gate,
struct clauses *nogate) {
struct ruler *ruler = simplifier->ruler;
for (all_clauses (clause, OCCURRENCES (lit)))
if (!is_binary_pointer (clause))
if (find_binary_and_gate_clauses (simplifier, lit, clause, gate,
nogate))
return clause;
return 0;
}
static unsigned find_equivalence_gate (struct simplifier *simplifier,
unsigned lit) {
signed char *marks = simplifier->marks;
struct ruler *ruler = simplifier->ruler;
for (all_clauses (clause, OCCURRENCES (lit)))
if (is_binary_pointer (clause))
marks[other_pointer (clause)] = 1;
unsigned not_lit = NOT (lit);
unsigned res = INVALID;
for (all_clauses (clause, OCCURRENCES (not_lit)))
if (is_binary_pointer (clause)) {
unsigned other = other_pointer (clause);
unsigned not_other = NOT (other);
if (marks[not_other]) {
res = other;
break;
}
}
for (all_clauses (clause, OCCURRENCES (lit)))
if (is_binary_pointer (clause))
marks[other_pointer (clause)] = 0;
return res;
}
bool find_definition (struct simplifier *simplifier, unsigned lit) {
struct ruler *ruler = simplifier->ruler;
struct clauses *gate = simplifier->gate;
struct clauses *nogate = simplifier->nogate;
{
unsigned other = find_equivalence_gate (simplifier, lit);
if (other != INVALID) {
ROG ("found equivalence %s equal to %s", ROGLIT (lit),
ROGLIT (other));
{
CLEAR (gate[0]);
CLEAR (nogate[0]);
unsigned not_other = NOT (other);
struct clause *lit_clause = tag_binary (false, lit, not_other);
bool found = false;
PUSH (gate[0], lit_clause);
for (all_clauses (clause, OCCURRENCES (lit)))
if (clause == lit_clause)
found = true;
else
PUSH (nogate[0], clause);
assert (found), (void) found;
}
{
CLEAR (gate[1]);
CLEAR (nogate[1]);
unsigned not_lit = NOT (lit);
struct clause *not_lit_clause = tag_binary (false, not_lit, other);
bool found = false;
PUSH (gate[1], not_lit_clause);
for (all_clauses (clause, OCCURRENCES (not_lit)))
if (clause == not_lit_clause)
found = true;
else
PUSH (nogate[1], clause);
assert (found), (void) found;
}
return true;
}
}
unsigned resolve = lit;
struct clause *base =
find_and_gate (simplifier, resolve, &gate[1], &nogate[1]);
if (base) {
assert (SIZE (gate[1]) == base->size - 1);
CLEAR (gate[0]);
CLEAR (nogate[0]);
PUSH (gate[0], base);
for (all_clauses (clause, OCCURRENCES (resolve)))
if (clause != base)
PUSH (nogate[0], clause);
} else {
resolve = NOT (lit);
base = find_and_gate (simplifier, resolve, &gate[0], &nogate[0]);
if (base) {
assert (SIZE (gate[0]) == base->size - 1);
CLEAR (gate[1]);
CLEAR (nogate[1]);
PUSH (gate[1], base);
for (all_clauses (clause, OCCURRENCES (resolve)))
if (clause != base)
PUSH (nogate[1], clause);
}
}
if (!base)
return false;
#ifdef LOGGING
do {
ROGPREFIX ("found %u-ary and-gate with %s defined as ", base->size - 1,
ROGLIT (resolve));
bool first = true;
for (all_literals_in_clause (other, base)) {
if (other == resolve)
continue;
if (first)
first = false;
else
fputs (" & ", stdout);
unsigned not_other = NOT (other);
fputs (ROGLIT (not_other), stdout);
}
ROGSUFFIX ();
} while (0);
#endif
return true;
}