forked from naturalog/tauchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unifiers.cpp
259 lines (240 loc) · 7.87 KB
/
unifiers.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
#include "prover.h"
bool prover::unify(termid _s, const subs& ssub, termid _d, subs& dsub) {
PROFILE(++unifs);
if (!_s || !_d) return !_s == !_d;
setproc(L"unify_bind");
termid v;
bool r, ns = false;
const term& d = *_d, &s = *_s;
TRACE(dout << "Trying to unify " << format(_s) << " sub: " << formats(ssub) << " with " << format(_d) << " sub: " << formats(dsub) << endl);
if (ISVAR(s)) {
v = evalvar(s, ssub);
r = v ? (ISVAR(d) ? unify_snovar_dvar(*v, ssub, d, dsub) : unify_sdnovar(*v, ssub, d, dsub)) : true;
}
else if (ISVAR(d)) {
v = evalvar(d, dsub);
if (v) r = unify_sdnovar(s, ssub, *v, dsub);
else {
dsub.emplace(d.p, evaluate(s, ssub));
TRACE(dout<<"new sub:"<<formats(dsub)<<endl);
r = ns = true;
}
}
else if (!(s.p == d.p && !s.s == !d.s && !s.o == !d.o)) r = false;
else if (!s.s) r = true;
else if ((r = unify(s.s, ssub, d.s, dsub)))
r = unify(s.o, ssub, d.o, dsub);
TRACE(
dout << "Trial to unify " << format(s) << " sub: " << formats(ssub) << " with " << format(d) << " sub: " << formats(dsub) << " : ";
if (r) {
dout << "passed";
if (ns) dout << " with new substitution: " << dstr(d.p) << " / " << format(dsub[d.p]);
} else dout << "failed";
dout << endl);
return r;
}
bool prover::unify_ep(termid _s, const subs& ssub, const term& d, const subs& dsub) {
PROFILE(++unifs);
if (!_s) return false;
setproc(L"unify_ep");
const term& s = *_s;
termid v;
bool r;
if (ISVAR(s)) {
v = evalvar(s, ssub);
r = v ? (ISVAR(d) ? unify_snovar_dvar_ep(*v, ssub, d, dsub) : unify_sdnovar_ep(*v, ssub, d, dsub)) : true;
}
else if (ISVAR(d)) {
v = evalvar(d, dsub);
r = v ? unify_sdnovar_ep(s, ssub, *v, dsub) : true;
}
else if (!(s.p == d.p && !s.s == !d.s && !s.o == !d.o)) r = false;
else if (!s.s) r = true;
else if ((r = unify_ep(s.s, ssub, *d.s, dsub)))
r = unify_ep(s.o, ssub, *d.o, dsub);
TRACE(dout << "Trying to unify " << format(s) << " sub: " << formats(ssub)
<< " with " << format(d) << " sub: " << formats(dsub) << " : ";
if (r) dout << "passed"; else dout << "failed";
dout << endl);
return r;
}
bool prover::unify_ep(termid _s, const term& d) {
PROFILE(++unifs);
if (!_s) return false;
setproc(L"unify_ep");
const term& s = *_s;
bool r;
if (ISVAR(s) || ISVAR(d)) return true;
else if (!(s.p == d.p && !s.s == !d.s && !s.o == !d.o)) r = false;
else if (!s.s) r = true;
else if ((r = unify_ep(s.s, *d.s)))
r = unify_ep(s.o, *d.o);
return r;
}
bool prover::unify_snovar_ep(const term& s, const subs& ssub, const term& d) {
PROFILE(++unifs);
if (ISVAR(d)) return true;
if (!(s.p == d.p && !s.s == !d.s && !s.o == !d.o)) return false;
if (!s.s) return true;
if (!unify_ep(s.s, ssub, *d.s)) return false;
return unify_ep(s.o, ssub, *d.o);
}
bool prover::unify_ep(termid _s, const subs& ssub, const term& d) {
PROFILE(++unifs);
if (!_s) return false;
setproc(L"unify_ep");
const term& s = *_s;
termid v;
bool r;
if (ISVAR(s)) {
v = evalvar(s, ssub);
r = v ? unify_snovar_ep(*v, ssub, d) : true;
}
else if (ISVAR(d)) return true;
else if (!(s.p == d.p && !s.s == !d.s && !s.o == !d.o)) r = false;
else if (!s.s) r = true;
else if ((r = unify_ep(s.s, ssub, *d.s)))
r = unify_ep(s.o, ssub, *d.o);
return r;
}
bool prover::unify_ep(termid _s, const term& d, const subs& dsub) {
PROFILE(++unifs);
if (!_s) return false;
setproc(L"unify_ep");
const term& s = *_s;
termid v;
bool r;
if (ISVAR(s)) return true;
else if (ISVAR(d)) {
v = evalvar(d, dsub);
r = v ? unify_sdnovar_ep(s, *v, dsub) : true;
}
else if (!(s.p == d.p && !s.s == !d.s && !s.o == !d.o)) r = false;
else if (!s.s) r = true;
else if ((r = unify_ep(s.s, *d.s, dsub)))
r = unify_ep(s.o, *d.o, dsub);
return r;
}
bool prover::unify_snovar(const term& s, const subs& ssub, termid _d, subs& dsub) {
PROFILE(++unifs);
const term& d = *_d;
if (ISVAR(d)) {
termid v = evalvar(d, dsub);
if (v) return unify_sdnovar(s, ssub, *v, dsub);
// dsub[d.p] = evaluate(s, ssub);
dsub.emplace(d.p, evaluate(s, ssub));
TRACE(dout<<"new sub:"<<format(dsub[d.p])<<endl);
return true;
}
if (!(s.p == d.p && !s.s == !d.s && !s.o == !d.o)) return false;
if (!s.s) return true;
if (!unify(s.s, ssub, d.s, dsub)) return false;
return unify(s.o, ssub, d.o, dsub);
}
bool prover::unify_snovar_dvar(const term& s, const subs& ssub, const term& d, subs& dsub) {
PROFILE(++unifs);
termid v = evalvar(d, dsub);
if (v) return unify_sdnovar(s, ssub, *v, dsub);
// dsub[d.p] = evaluate(s, ssub);
dsub.emplace(d.p, evaluate(s, ssub));
TRACE(dout<<"new sub:"<<format(dsub[d.p])<<endl);
return true;
}
bool prover::unify_dnovar(termid _s, const subs& ssub, const term& d, subs& dsub) {
PROFILE(++unifs);
const term& s = *_s;
if (ISVAR(s)) {
termid v = evalvar(s, ssub);
return v ? unify_sdnovar(*v, ssub, d, dsub) : true;
}
if (!(s.p == d.p && !s.s == !d.s && !s.o == !d.o)) return false;
if (!s.s) return true;
if (!unify(s.s, ssub, d.s, dsub)) return false;
return unify(s.o, ssub, d.o, dsub);
}
bool prover::unify_snovar_dvar_ep(const term& s, const subs& ssub, const term& d, const subs& dsub) {
PROFILE(++unifs);
termid v = evalvar(d, dsub);
return v ? unify_sdnovar_ep(s, ssub, *v, dsub) : true;
}
bool prover::unify_dnovar_ep(termid _s, const subs& ssub, const term& d, const subs& dsub) {
PROFILE(++unifs);
setproc(L"unify");
const term& s = *_s;
if (ISVAR(s)) {
termid v = evalvar(s, ssub);
return v ? unify_sdnovar_ep(*v, ssub, d, dsub) : true;
}
if (!(s.p == d.p && !s.s == !d.s && !s.o == !d.o)) return false;
if (!s.s) return true;
if (!unify_ep(s.s, ssub, *d.s, dsub)) return false;
return unify_ep(s.o, ssub, *d.o, dsub);
}
bool prover::unify(termid _s, termid _d, subs& dsub) {
PROFILE(++unifs);
if (!_s || !_d) return !_s == !_d;
const term& d = *_d, &s = *_s;
if (ISVAR(s)) return true;
if (ISVAR(d)) {
termid v = evalvar(d, dsub);
if (v) return unify_sdnovar(s, *v, dsub);
//dsub[d.p] = evaluate(s);
dsub.emplace(d.p, evaluate(s));
TRACE(dout<<"new sub:"<<format(dsub[d.p])<<endl);
return true;
}
if (!(s.p == d.p && !s.s == !d.s && !s.o == !d.o)) return false;
if (!s.s) return true;
if (!unify(s.s, d.s, dsub)) return false;
return unify(s.o, d.o, dsub);
}
bool prover::unify_dnovar(termid _s, const term& d, subs& dsub) {
PROFILE(++unifs);
setproc(L"unify");
const term& s = *_s;
if (ISVAR(s)) return true;
if (!(s.p == d.p && !s.s == !d.s && !s.o == !d.o)) return false;
if (!s.s) return true;
if (!unify(s.s, d.s, dsub)) return false;
return unify(s.o, d.o, dsub);
}
bool prover::unify_sdnovar(const term& s, const subs& ssub, const term& d, subs& dsub) {
PROFILE(++unifs);
setproc(L"unify");
if (!(s.p == d.p && !s.s == !d.s && !s.o == !d.o)) return false;
if (!s.s) return true;
if (!unify(s.s, ssub, d.s, dsub)) return false;
return unify(s.o, ssub, d.o, dsub);
}
bool prover::unify_sdnovar_ep(const term& s, const subs& ssub, const term& d) {
PROFILE(++unifs);
setproc(L"unify");
if (!(s.p == d.p && !s.s == !d.s && !s.o == !d.o)) return false;
if (!s.s) return true;
if (!unify_ep(s.s, ssub, *d.s)) return false;
return unify_ep(s.o, ssub, *d.o);
}
bool prover::unify_sdnovar_ep(const term& s, const term& d, const subs& dsub) {
PROFILE(++unifs);
setproc(L"unify");
if (!(s.p == d.p && !s.s == !d.s && !s.o == !d.o)) return false;
if (!s.s) return true;
if (!unify_ep(s.s, *d.s, dsub)) return false;
return unify_ep(s.o, *d.o, dsub);
}
bool prover::unify_sdnovar_ep(const term& s, const subs& ssub, const term& d, const subs& dsub) {
PROFILE(++unifs);
setproc(L"unify");
if (!(s.p == d.p && !s.s == !d.s && !s.o == !d.o)) return false;
if (!s.s) return true;
if (!unify_ep(s.s, ssub, *d.s, dsub)) return false;
return unify_ep(s.o, ssub, *d.o, dsub);
}
bool prover::unify_sdnovar(const term& s, const term& d, subs& dsub) {
PROFILE(++unifs);
setproc(L"unify");
if (!(s.p == d.p && !s.s == !d.s && !s.o == !d.o)) return false;
if (!s.s) return true;
if (!unify(s.s, d.s, dsub)) return false;
return unify(s.o, d.o, dsub);
}