-
Notifications
You must be signed in to change notification settings - Fork 3
/
logic.cpp
197 lines (180 loc) · 5.03 KB
/
logic.cpp
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
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
#define LEND (-1)
#define LVAR 0
#define LNEG 1
#define LAND 2
#define LOR 3
#define LIMP 4
#define LBIMP 5
#define LOPEN 6
#define LCLOSE 7
#define PREC(op) (10 - (op)) // ugly but sufficient
struct subf {
int op;
int arg[2];
};
vector<subf> subfs; // recursive descent parser automatically enumerates all subformulas
unordered_map<string, int> map;
[[noreturn]] void error(const char *str) {
fputs(str, stderr);
exit(1);
}
string name;
int next_token = LEND;
int get_token() {
if (next_token != LEND) {
int k = next_token;
next_token = LEND;
return k;
}
int c;
do {
c = getchar();
} while (isspace(c));
switch (c) {
case EOF: return LEND;
case '(': return LOPEN;
case ')': return LCLOSE;
case '~': return LNEG;
case '&': return LAND;
case '|': return LOR;
case '-':
if (getchar() != '>')
error("unknown token");
return LIMP;
case '<':
if (getchar() != '-')
error("unknown token");
if (getchar() != '>')
error("unknown token");
return LBIMP;
default:
if (isalpha(c) || c == '_') {
name = "";
do {
name += c;
c = getchar();
} while (isalpha(c) || c == '_');
ungetc(c, stdin);
return LVAR;
}
error("unknown token");
}
}
int peek_token() {
if (next_token == LEND)
next_token = get_token();
return next_token;
}
int parse();
int parse_primary() {
int k = get_token();
if (k == LNEG) {
k = parse_primary();
subfs.push_back({ LNEG, k });
return subfs.size() - 1;
} else if (k == LVAR) {
auto it = map.find(name);
if (it != map.end()) {
return it->second;
}
subfs.push_back({ LVAR });
return map.insert({ move(name), subfs.size() - 1 }).first->second;
} else if (k == LOPEN) {
k = parse();
if (get_token() != LCLOSE)
error("unexpected ')'");
return k;
} else {
error("syntax error");
}
}
int parse_1(int lhs, int prec) {
while (1) {
int k = peek_token();
if (! (k == LAND || (k == LOR && PREC(k) >= prec) || (k == LIMP && PREC(k) >= prec) || (k == LBIMP && PREC(k) > prec)))
break;
int op = k;
get_token(); // consume
int rhs = parse_primary();
while (1) {
k = peek_token();
if (k == LBIMP && op == LBIMP)
error("syntax error");
if (! ((k == LAND && PREC(k) > PREC(op)) || (k == LOR && PREC(k) > PREC(op)) || (k == LIMP && PREC(k) == PREC(op))))
break;
rhs = parse_1(rhs, PREC(op));
}
subfs.push_back({ op, lhs, rhs });
lhs = subfs.size() - 1;
}
return lhs;
}
int parse() {
int k = parse_primary();
return parse_1(k, 0);
}
// void print() {
// for (int i = 1; i < subfs.size(); ++i) {
// auto [op, arg] = subfs[i];
// if (op == LVAR)
// continue;
// int p = arg[0], q = arg[1];
// switch (op) {
// case LNEG: fprintf(stderr, "%d <-> ~%d\n", i, p); break;
// case LAND: fprintf(stderr, "%d <-> %d & %d\n", i, p, q); break;
// case LOR: fprintf(stderr, "%d <-> %d | %d\n", i, p, q); break;
// case LIMP: fprintf(stderr, "%d <-> %d -> %d\n", i, p, q); break;
// case LBIMP: fprintf(stderr, "%d <-> (%d <-> %d)\n", i, p, q); break;
// }
// }
// }
int main() {
subfs.push_back({}); // avoid 0
int k = parse();
// Tseitin transformation
vector<vector<int>> db;
for (int i = 1; i < subfs.size(); ++i) {
auto [op, arg] = subfs[i];
if (op == LVAR)
continue;
int r = i, p = arg[0], q = arg[1];
switch (op) {
case LNEG: // r<->~p = r->~p & ~p->r = ~r|~p & p|r
db.push_back({ r, p });
db.push_back({ -r, -p });
break;
case LIMP: // r<->p->q = r->~p|q
p = -p;
[[fallthrough]];
case LOR: // r<->p|q = ~r<->~p&~q
r = -r;
p = -p;
q = -q;
[[fallthrough]];
case LAND: // r<->p&q = r->p&q & p&q->r = ~r|p&q & ~p|~q|r = ~r|p & ~r|q & ~p|~q|r
db.push_back({ -r, p });
db.push_back({ -r, q });
db.push_back({ r, -p, -q });
break;
case LBIMP: // r<->(p<->q) = r->(p<->q) & (p<->q)->r = ~r|((p|~q)&(~p|q)) & ~((p&q)|(~p&~q))|r
db.push_back({ -r, p, -q });
db.push_back({ -r, -p, q });
db.push_back({ r, -p, -q });
db.push_back({ r, p, q });
break;
}
}
db.push_back({ k });
printf("p cnf %lu %lu\n", subfs.size() - 1, db.size());
for (auto & clause : db) {
for (auto lit : clause)
printf("%d ", lit);
printf("0\n");
}
}