-
Notifications
You must be signed in to change notification settings - Fork 9
/
cc.cpp
265 lines (245 loc) · 6.51 KB
/
cc.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
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
#include <functional>
#include <iostream>
using namespace std;
typedef std::function<bool(int&, bool)> atom;
typedef std::function<bool(int&, int&)> comp;
const char KNRM[] = "\x1B[0m";
const char KRED[] = "\x1B[31m";
const char KGRN[] = "\x1B[32m";
const char KYEL[] = "\x1B[33m";
const char KBLU[] = "\x1B[34m";
const char KMAG[] = "\x1B[35m";
const char KCYN[] = "\x1B[36m";
const char KWHT[] = "\x1B[37m";
comp* sb;
atom compile_atom(int p) {
int val = p, state = 0;
bool bound = false;
std::function<bool(int&)> u;
if (p < 0)
u = [p, val, bound, state](int& x) mutable {
auto env = [=]() {
cout << KMAG << 41 << ": ";
std::cout << "p"" = " << (p) << '\t';
std::cout << "val"" = " << (val) << '\t';
std::cout << "bound"" = " << (bound) << '\t';
std::cout << "state"" = " << (state) << '\t';
std::cout << "x"" = " << (x) << '\t';
cout << KNRM <<endl;
};
env();
std::cout << KGRN << "switch (state) { case 0: if (!bound) { (val = x, bound = true, state = 1); return true; } case 1: (bound = false, state = 2); return val == x; } return false;" << KNRM << endl;
switch (state) {
case 0:
if (!bound) {
(val = x, bound = true, state = 1);
return true;
}
case 1:
(bound = false, state = 2);
return val == x;
}
return false;
;
};
else u = [p, state](int& x) mutable {
auto env = [=]() {
cout << KMAG << 55 << ": ";
std::cout << "p"" = " << (p) << '\t';
std::cout << "state"" = " << (state) << '\t';
std::cout << "x"" = " << (x) << '\t';
cout << KNRM <<endl;
};
env();
std::cout << KGRN << "return x == p" << KNRM << endl;
return x == p;
};
return [p, u, val, state](int& x, bool unify) mutable {
auto env = [=]() {
cout << KMAG << 60 << ": ";
std::cout << "p"" = " << (p) << '\t';
std::cout << "val"" = " << (val) << '\t';
std::cout << "state"" = " << (state) << '\t';
std::cout << "x"" = " << (x) << '\t';
std::cout << "unify"" = " << (unify) << '\t';
std::cout << "(sb-(comp*)&u)"" = " << ((sb-(comp*)&u)) << '\t';
cout << KNRM <<endl;
};
env();
std::cout << KGRN << "if (unify) return u(x); if (!state) { (x = val, state = 1); return true; } return false;" << KNRM << endl;
if (unify) return u(x);
if (!state) {
(x = val, state = 1);
return true;
}
return false;
;
};
}
comp compile_triple(atom s, atom o) {
int state = 0;
return [s, o, state](int& _s, int& _o) mutable {
auto env = [=]() {
cout << KMAG << 75 << ": ";
std::cout << "state"" = " << (state) << '\t';
std::cout << "_s"" = " << (_s) << '\t';
std::cout << "_o"" = " << (_o) << '\t';
std::cout << "(sb-(comp*)&s)"" = " << ((sb-(comp*)&s)) << '\t';
std::cout << "(sb-(comp*)&o)"" = " << ((sb-(comp*)&o)) << '\t';
cout << KNRM <<endl;
};
env();
std::cout << KGRN << "switch (state) { case 0: while (s(_s, true)) { while (o(_o, true)) { state = 1; return true; case 1: ; } } state = 2; } return false;" << KNRM << endl;
switch (state) {
case 0:
while (s(_s, true)) {
while (o(_o, true)) {
state = 1;
return true;
case 1:
;
}
}
state = 2;
}
return false;
;
};
}
comp compile_unify(comp x, comp y) {
uint state = 0;
int ss = 0, so = 0;
return [x, y, state, ss, so](int& s, int& o) mutable {
auto env = [=]() {
cout << KMAG << 96 << ": ";
std::cout << "s"" = " << (s) << '\t';
std::cout << "o"" = " << (o) << '\t';
std::cout << "state"" = " << (state) << '\t';
std::cout << "ss"" = " << (ss) << '\t';
std::cout << "so"" = " << (so) << '\t';
std::cout << "(sb-(comp*)&x)"" = " << ((sb-(comp*)&x)) << '\t';
std::cout << "(sb-(comp*)&y)"" = " << ((sb-(comp*)&y)) << '\t';
cout << KNRM <<endl;
};
env();
std::cout << KGRN << "switch (state) { case 0: while (x(ss, so)) { while (y(ss, so)) { state = 1; return true; case 1: ; } state = 2; return false; } default: return false; }" << KNRM << endl;
switch (state) {
case 0:
while (x(ss, so)) {
while (y(ss, so)) {
state = 1;
return true;
case 1:
;
}
state = 2;
return false;
}
default:
return false;
}
;
};
}
comp compile_match(comp x, comp y, int& a) {
uint state = 0;
return [x, y, a, state](int& s, int& o) mutable {
auto env = [=]() {
cout << KMAG << 118 << ": ";
std::cout << "a"" = " << (a) << '\t';
std::cout << "s"" = " << (s) << '\t';
std::cout << "o"" = " << (o) << '\t';
std::cout << "state"" = " << (state) << '\t';
std::cout << "(sb-(comp*)&x)"" = " << ((sb-(comp*)&x)) << '\t';
std::cout << "(sb-(comp*)&y)"" = " << ((sb-(comp*)&y)) << '\t';
cout << KNRM <<endl;
};
switch (state) {
case 0:
while (x(a, o)) {
while (y(s, a)) {
return (bool)(state = 1);
case 1:
;
}
state = 2;
return false;
}
default:
return false;
}
};
}
comp compile_triples(comp f, comp r) {
uint state = 0;
return [f, r, state](int& s, int& o) mutable {
auto env = [=]() {
cout << KMAG << 137 << ": ";
std::cout << "state"" = " << (state) << '\t';
std::cout << "s"" = " << (s) << '\t';
std::cout << "o"" = " << (o) << '\t';
std::cout << "(sb-(comp*)&f)"" = " << ((sb-(comp*)&f)) << '\t';
std::cout << "(sb-(comp*)&r)"" = " << ((sb-(comp*)&r)) << '\t';
cout << KNRM <<endl;
};
env();
std::cout << KGRN << "switch (state) { case 0: while (f(s, o)) { return (bool)(state = 1); case 1: ; } while (r(s, o)) { return (bool)(state = 2); case 2: ; } state = 3; default: return false; }" << KNRM << endl;
switch (state) {
case 0:
while (f(s, o)) {
return (bool)(state = 1);
case 1:
;
}
while (r(s, o)) {
return (bool)(state = 2);
case 2:
;
}
state = 3;
default:
return false;
}
};
}
comp nil = [](int&, int&) {
return false;
};
void test() {
comp c = nil;
int n = 0, s, o;
for (; n < 4; ++n)
c = compile_triples(compile_triple(compile_atom(n), compile_atom(n+1)), c);
n = 0;
auto env = [=]() {
cout << KMAG << 163 << ": ";
std::cout << "n"" = " << (n) << '\t';
std::cout << "s"" = " << (s) << '\t';
std::cout << "o"" = " << (o) << '\t';
cout << KNRM <<endl;
};
while (c(s, o)) {
cout << "##";
env();
n++;
}
}
int main() {
cout << endl;
comp kb = nil;
sb = &kb;
test();
return 0;
atom x = compile_atom(1);
atom y = compile_atom(2);
atom z = compile_atom(3);
atom v = compile_atom(-1);
comp t1 = compile_triple(x, y);
comp t2 = compile_triple(v, y);
comp t3 = compile_triple(z, z);
kb = compile_triples(t1, compile_triples(t2, compile_triples(t3, nil)));
int ii = 1;
comp m = compile_unify(t1, t2);
int s, o;
while (m(s, o)) cout << s << ' ' << o << endl;
}