-
Notifications
You must be signed in to change notification settings - Fork 1
/
qdata.h
348 lines (281 loc) · 7.08 KB
/
qdata.h
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
/*
IL : Intlog Language
Object Oriented Prolog Project
Copyright (C) 1992-2021 - Ing. Capelli Carlo
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef QDATA_H_
#define QDATA_H_
////////////////////////////////////////////
// internal query evaluator data structures
// stack: Proof Tree, Vars binding, Trail
// other data for variables and status
////////////////////////////////////////////
#include "iafx.h"
#include "query.h"
#include "dbintlog.h"
//////////////////////////////////
// stack base behaviour
// a vector of allocation blocks
//
template<class T>
class IAFX_API BaseStack : protected vect<T> {
public:
// allocate top elements
void push(unsigned n = 1) {
for (unsigned c = 0; c < n; ++c) {
this->push_back(T());
++free;
}
}
// release top element(s)
stkpos pop(unsigned n = 1) {
assert(free >= n);
free -= n;
return free;
}
// return current dimension
stkpos curr_dim() const { return free; }
// ensure empty on create
void init() {
}
protected:
unsigned free = 0;
};
// stack vector entry
struct elbind {
// binder Env reference
stkpos envp;
// shared or ground: check envp
union {
stkpos share;
TermData value;
};
};
/////////////////////////////
// keep unification bindings
//
class IAFX_API BindStack : public BaseStack<elbind> {
public:
BindStack() {
init();
}
// alloc and return base of reserved cells
stkpos reserve(unsigned);
// return valued var from <off> to base
Term getvar(stkpos, stkpos *, stkpos *) const;
// change offset var from base
void setvar(stkpos off, Term t, stkpos envp) {
assert(envp != STKNULL);
elbind *vval = getptr(off);
vval->value = t;
vval->envp = envp;
}
// make a shared variable cell
void setshare(stkpos cell, stkpos rpos) {
assert(rpos != STKNULL);
assert(cell != rpos);
elbind *e = getptr(cell);
e->share = rpos;
e->envp = STKNULL;
}
// clear binding of requested cell
void clearcell(stkpos cell) {
elbind *e = getptr(cell);
e->value = f_NOTERM;
e->envp = 0;
}
// display current proof segment
void show(ostream &) const;
};
/////////////////////////////////////////////
// register bindings to undo on backtracking
//
class IAFX_API TrailStack : public BaseStack<stkpos> {
public:
TrailStack() {
init();
}
// register a bind
void bind(unsigned v) {
push();
set(free - 1, v);
}
// access n-th element
stkpos get(stkpos n) const {
return getat(n);
}
void set(stkpos n, stkpos v) {
setat(n, v);
}
// display current bindings segment
void show(std::ostream &) const;
};
/////////////////////////////
// Proof main data structure
// keep track of a try
//
struct Env {
// current RHB procedure
CallData* call;
// location in DB (if DBPRED) or other infos
union {
const e_DbList* dbpos;
BltinData* ptr;
};
// father of this (-1 if none)
stkpos father;
// query variables start
stkpos vspos;
// binded trail start index
stkpos trail;
};
//////////////////////////////////////////////////////
// encapsulate proof tree navigation
// not a full stack:
// access needed to elements with incomplete searchs
//
class IAFX_API ProofStack : public BaseStack<Env> {
public:
ProofStack() {
init();
}
// get element at pos from base
Env* get(stkpos pos) const {
return getptr(pos);
}
// return <nth> from top
Env* topget() const {
return get(free - 1);
}
// insert a new element
stkpos push(stkpos from) {
BaseStack::push();
stkpos ret = free - 1;
Env *e = get(ret);
e->father = from;
return ret;
}
// find base of required, following fathers' chain
stkpos baseof(stkpos = STKNULL) const;
// true if <a> is ancestor of <n>
int isancestor(stkpos a, stkpos n) const;
// display current proof segment
void show(ostream &) const;
// attempt to recover variables env
const Env *findVarEnv(stkpos) const;
};
/////////////////////////////////
// keep current execution status
//
class IAFX_API ProofStatus {
public:
ProofStatus(IntlogExec *pe) {
p = pe;
save();
}
void save();
void restore();
int ok() const {
return psdim != STKNULL;
}
int empty() const {
return psdim == 0;
}
stkpos pdim() const {
return psdim;
}
stkpos vdim() const {
return vsdim;
}
stkpos tdim() const {
return tsdim;
}
IntlogExec *eng() const {
return p;
}
private:
void init() {
psdim = STKNULL;
}
IntlogExec *p;
stkpos psdim, vsdim, tsdim;
};
/////////////////////////////////////////////////////////////
// enable storage of builtins context sensitive data
// data will be deleted when tried bltin become unreachable
//
class BltinData {
public:
virtual ~BltinData();
};
/////////////////////////////////////////////////
// temporary terms/clauses/builtins data storage
//
class ElemTmp : public e_slist {
friend class IntlogExec;
union {
TermData t;
Clause* c;
BltinData* d;
};
enum {
vT, vC, vD
} type;
stkpos spos;
ElemTmp(Term ts, stkpos atp) {
t = TermData(ts);
spos = atp;
type = vT;
}
ElemTmp(Clause *cs, stkpos atp) {
c = cs;
spos = atp;
type = vC;
}
ElemTmp(BltinData *ds, stkpos atp) {
d = ds;
spos = atp;
type = vD;
}
~ElemTmp() override;
};
//////////////////////////////////////////////
// visit reindexing on linear search position
//
class OffVars : vect<Var> {
public:
OffVars(Term& t, unsigned off = 0) {
SetTerm(t, off);
}
void SetTerm(Term&, unsigned);
Var old2new(Var oldv) const {
return Var(search(oldv));
}
Var new2old(Var newv) const {
return getat(unsigned(newv));
}
unsigned nvars() const {
return count;
}
private:
unsigned search(Var v) const {
for (unsigned i = 0; i < count; i++)
if (getat(i) == v)
return i;
return unsigned(-1);
}
Var lookup(Var);
unsigned count;
};
#endif