-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswi_robdd.c
361 lines (311 loc) · 9.31 KB
/
swi_robdd.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
/*****************************************************************
File : swi_robdd.c
Author : Peter Schachte
Purpose : Interface robdd package to SWI Prolog
Copyright: (c) 2017 Peter Schachte. All rights reserved.
*****************************************************************/
#include <SWI-Prolog.h>
#include "bryantPrint.h"
#include <SWI-Stream.h> /* for debugging printfs */
#include "bryant.h"
#define VAR_LIMIT 256 /* max number of variables we can handle in a
* list or as term arguments.
*/
static foreign_t
pl_max_variable(term_t num_term) {
return PL_unify_integer(num_term, max_variable());
}
static foreign_t
pl_init_rep() {
initRep();
PL_succeed;
}
static foreign_t
pl_conclude_rep() {
concludeRep();
PL_succeed;
}
static foreign_t
pl_bdd_variable(term_t var_term, term_t bdd) {
int var;
if (PL_get_integer(var_term, &var)) {
node *nd = variableRep(var);
return PL_unify_pointer(bdd, nd);
} else {
PL_fail;
}
}
static foreign_t
pl_bdd_true(term_t bdd) {
return PL_unify_pointer(bdd, (void *)TRUE);
}
static foreign_t
pl_bdd_false(term_t bdd) {
return PL_unify_pointer(bdd, (void *)FALSE);
}
static foreign_t
pl_bdd_and(term_t f, term_t g, term_t result_term) {
void *f_nd, *g_nd;
if (PL_is_integer(f)
&& PL_is_integer(g)
&& PL_get_pointer(f, &f_nd)
&& PL_get_pointer(g, &g_nd)) {
node *result = glb((node *)f_nd, (node *)g_nd);
return PL_unify_pointer(result_term, (void *)result);
} else {
PL_fail;
}
}
static foreign_t
pl_bdd_or(term_t f, term_t g, term_t result_term) {
void *f_nd, *g_nd;
if (PL_is_integer(f)
&& PL_is_integer(g)
&& PL_get_pointer(f, &f_nd)
&& PL_get_pointer(g, &g_nd)) {
node *result = lub((node *)f_nd, (node *)g_nd);
return PL_unify_pointer(result_term, (void *)result);
} else {
PL_fail;
}
}
static foreign_t
pl_bdd_implies(term_t f, term_t g, term_t result_term) {
void *f_nd, *g_nd;
if (PL_is_integer(f)
&& PL_is_integer(g)
&& PL_get_pointer(f, &f_nd)
&& PL_get_pointer(g, &g_nd)) {
node *result = implies((node *)f_nd, (node *)g_nd);
return PL_unify_pointer(result_term, (void *)result);
} else {
PL_fail;
}
}
/* XXX We don't actually use this for groundness analysis.
* static foreign_t
* pl_bdd_ite(term_t f, term_t g, term_t h, term_t result_term) {
* void *f_nd, *g_nd, *h_nd;
* if (PL_is_integer(f)
* && PL_is_integer(g)
* && PL_is_integer(h)
* && PL_get_pointer(f, &f_nd)
* && PL_get_pointer(g, &g_nd)
* && PL_get_pointer(h, &h_nd)) {
* node *result = (void *) ite((node *)f, (node *)g, (node *)h);
* return PL_unify_pointer(result_term, result);
* } else {
* PL_fail;
* }
* }
*/
static foreign_t
pl_bdd_project(term_t thresh_term, term_t f, term_t result_term) {
void *f_nd;
int thresh;
if ( PL_get_pointer(f, &f_nd)
&& PL_get_integer(thresh_term, &thresh)) {
node *result = projectThresh(thresh, (node *)f_nd);
return PL_unify_pointer(result_term, (void *)result);
} else {
PL_fail;
}
}
static foreign_t
pl_bdd_projected_and(term_t thresh_term, term_t f, term_t g, term_t result_term) {
void *f_nd, *g_nd;
int thresh;
if ( PL_get_pointer(f, &f_nd)
&& PL_get_pointer(g, &g_nd)
&& PL_get_integer(thresh_term, &thresh)) {
node *result = projected_glb(thresh, (node *)f_nd, (node *)g_nd);
return PL_unify_pointer(result_term, (void *)result);
} else {
PL_fail;
}
}
static foreign_t
pl_bdd_print(term_t bdd) {
void *nd;
if (PL_is_integer(bdd)
&& PL_get_pointer(bdd, &nd)) {
printOut(stdout, (node *)nd);
PL_succeed;
} else {
PL_fail;
}
}
static foreign_t
pl_bdd_print_stderr(term_t bdd) {
void *nd;
if (PL_is_integer(bdd)
&& PL_get_pointer(bdd, &nd)) {
printOut(stderr, (node *)nd);
PL_succeed;
} else {
PL_fail;
}
}
/* forward declarations to keep C happy */
static int list_to_array(term_t l, int arr[], int *n);
static int explode_term(term_t term, int arr[], int *n);
static foreign_t
pl_bdd_and_vars(term_t list, term_t result_term) {
int n = VAR_LIMIT;
int arr[VAR_LIMIT];
if (list_to_array(list, arr, &n)) {
node *result = glb_array(n, arr);
return PL_unify_pointer(result_term, (void *)result);
} else {
PL_fail;
}
}
static foreign_t
pl_rename_term(term_t bdd, term_t renaming, term_t result_term) {
int n = VAR_LIMIT;
int mapping[VAR_LIMIT];
void *nd;
if (PL_is_integer(bdd)
&& PL_get_pointer(bdd, &nd)
&& explode_term(renaming, mapping, &n)) {
node *result = renameArray(nd, n, mapping);
return PL_unify_pointer(result_term, (void *)result);
} else {
PL_fail;
}
}
static foreign_t
pl_reverse_rename_term(term_t bdd_term, term_t renaming, term_t result_term) {
int n = VAR_LIMIT;
int mapping[VAR_LIMIT];
void *nd;
if (PL_is_integer(bdd_term)
&& PL_get_pointer(bdd_term, &nd)
&& explode_term(renaming, mapping, &n)) {
node *result = reverseRenameArray(nd, n, mapping);
return PL_unify_pointer(result_term, (void *)result);
} else {
PL_fail;
}
}
static foreign_t
pl_iff_conj_list(term_t var_term, term_t list_term, term_t result_term) {
int n = VAR_LIMIT;
int arr[VAR_LIMIT];
int var;
if ( PL_get_integer(var_term, &var)
&& list_to_array(list_term, arr, &n)) {
node *result = iff_conj_array(var, n, arr);
return PL_unify_pointer(result_term, (void *)result);
} else {
PL_fail;
}
}
static foreign_t
pl_abstract_unify_list(term_t context_term, term_t v0_term, term_t list_term,
term_t thresh_term, term_t result_term) {
void *context;
int v0;
int n = VAR_LIMIT;
int arr[VAR_LIMIT];
int thresh;
if ( PL_get_pointer(context_term, &context)
&& PL_get_integer(v0_term, &v0)
&& PL_get_integer(thresh_term, &thresh)
&& list_to_array(list_term, arr, &n)) {
node *result = abstract_unify(context, v0, n, arr, thresh);
return PL_unify_pointer(result_term, (void *)result);
} else {
PL_fail;
}
}
static foreign_t
pl_abstract_exit_term(term_t context_term, term_t f_term, term_t term,
term_t thresh_term, term_t result_term) {
int n = VAR_LIMIT;
int arr[VAR_LIMIT];
int thresh;
void *context;
void *f;
if ( PL_get_pointer(context_term, &context)
&& PL_get_pointer(f_term, &f)
&& PL_get_integer(thresh_term, &thresh)
&& explode_term(term, arr, &n)) {
node *result = abstract_exit(context, f, n, arr, thresh, identity_renaming);
return PL_unify_pointer(result_term, (void *)result);
} else {
PL_fail;
}
}
/* Copy Prolog list of ints l into a array arr of length n. Return FALSE if
* any element of the list is not an integer, or if the list is too long;
* otherwise return TRUE and reassign n to the actual length of the list.
*/
static int
list_to_array(term_t list_term, int arr[], int *n) {
term_t head = PL_new_term_ref(); /* the elements */
term_t list = PL_copy_term_ref(list_term); /* copy (we modify list) */
int i = 0;
while( PL_get_list(list, head, list) ) {
int num;
if (i > *n) return FALSE;
if ( PL_get_integer(head, &num) ) {
arr[i++] = num;
} else {
return FALSE;
}
}
*n = i;
return PL_get_nil(list); /* test end for [] */
}
/* Copy Prolog term term, all of whose arguments are ints, into array arr
* of length *n at indices 1 - arity, and set *n to the arity. Note that
* we never use array slot 0. Return FALSE if term is not a compound term
* (or an atom), or any argument is not an integer, or if the arity is
* greater or equal to *n.
*/
static int
explode_term(term_t term, int arr[], int *n) {
atom_t name;
int arity;
int i = 1;
if( PL_get_name_arity(term, &name, &arity)
&& arity < *n) {
for (i=1; i<=arity; ++i) {
term_t arg_term = PL_new_term_ref();
int num;
if ( !( PL_get_arg(i, term, arg_term)
&& PL_get_integer(arg_term, &num)) ) {
return FALSE;
}
arr[i] = num;
}
*n = arity;
return TRUE;
} else {
return FALSE;
}
}
install_t
install_swi_robdd() {
PL_register_foreign("max_variable", 1, pl_max_variable, 0);
PL_register_foreign("init_rep", 0, pl_init_rep, 0);
PL_register_foreign("conclude_rep", 0, pl_conclude_rep, 0);
PL_register_foreign("variable_rep", 2, pl_bdd_variable, 0);
PL_register_foreign("anz_top", 1, pl_bdd_true, 0);
PL_register_foreign("anz_bottom", 1, pl_bdd_false, 0);
PL_register_foreign("anz_meet", 3, pl_bdd_and, 0);
PL_register_foreign("anz_join", 3, pl_bdd_or, 0);
PL_register_foreign("anz_implies", 3, pl_bdd_implies, 0);
/* PL_register_foreign("bdd_ite", 4, pl_bdd_ite, 0); */
PL_register_foreign("project_threshold", 3, pl_bdd_project, 0);
PL_register_foreign("anz_meet", 4, pl_bdd_projected_and, 0);
PL_register_foreign("anz_print", 1, pl_bdd_print, 0);
PL_register_foreign("anz_print_stderr", 1, pl_bdd_print_stderr, 0);
PL_register_foreign("anz_meet_vars", 2, pl_bdd_and_vars, 0);
PL_register_foreign("rename_term", 3, pl_rename_term, 0);
PL_register_foreign("reverse_rename_term", 3, pl_reverse_rename_term, 0);
PL_register_foreign("anz_iffconj", 3, pl_iff_conj_list, 0);
PL_register_foreign("abstract_unify_list", 5, pl_abstract_unify_list, 0);
PL_register_foreign("analyze_call", 5, pl_abstract_exit_term, 0);
}