-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicocaml.c
357 lines (292 loc) · 6.43 KB
/
picocaml.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
/*
* OCaml bindings for picosat 936
*
* * compile with e.g.
* $ gcc -I<PathToOcamlHeaders> -I <PathToPicosat> -c picocaml.c
* * to build an interactive interpreter supporting picosat queries issue
* $ ocamlmktop -custom -o picocaml picocaml.o \
* <PathToPicosat>/picosat.o <PathToPicosat>/version.o picocaml.ml
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdarg.h>
#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include <picosat.h>
/* Declarations */
static int out(FILE *f, char *fmt, ...);
/* Types */
typedef struct cls { int *lits; } cls;
typedef cls * cblk;
/* Macros, Constants, ... */
#define BLK_S 1024 /* size of clause blocks */
#define CLS_S 256 /* initial size of clause buffer */
/* Globals */
static cblk *cbuf=NULL;
static unsigned int sccbuf=0;
static unsigned int lccbuf=0;
static int *clbuf=NULL;
static unsigned int sclbuf=0;
static unsigned int fclbuf=0;
static unsigned int lastrv=0;
static unsigned char initialized=0; /* 0: uninitialized, !=0: initialized */
static unsigned char tr_enabled=0; /* 0: no traces, !=0: traces enabled */
static int last_res=PICOSAT_UNKNOWN;
/*
* Function definitions
*/
void ck_clbuf() {
if (clbuf==NULL && sclbuf==0) {
clbuf=(int *)malloc(CLS_S*sizeof(int));
sclbuf=CLS_S;
}
assert(clbuf);
}
void rsz_clbuf() {
ck_clbuf();
if (fclbuf>=sclbuf) {
int *new=realloc(clbuf, sclbuf*2*sizeof(int));
assert(new);
clbuf=new;
sclbuf*=2;
}
}
void psh_clbuf(int i) {
ck_clbuf();
if (fclbuf<sclbuf)
clbuf[fclbuf++]=i;
else {
rsz_clbuf();
psh_clbuf(i);
}
}
void print_clbuf() {
int i=0;
unsigned char fst=1;
putchar('[');
while (i<fclbuf) {
printf("%s%d", fst==1 ? "" : ", ", clbuf[i++]);
if (fst)
fst=0;
}
puts("]");
fflush(stdout);
}
void cl_clbuf() {
fclbuf=0;
}
void ck_cbuf() {
if (cbuf==NULL && sccbuf==0) {
cbuf=(cblk*)malloc(BLK_S*sizeof(cblk));
sccbuf=BLK_S;
}
assert(cbuf);
}
void rsz_cbuf() {
ck_cbuf();
if (lccbuf>=sccbuf) {
cblk *n=NULL;
while (sccbuf<=lccbuf)
sccbuf+=BLK_S;
n=realloc(cbuf, sccbuf);
assert(n);
cbuf=n;
}
}
void psh_cbuf(cls *c) {
ck_cbuf();
if (lccbuf<sccbuf)
cbuf[lccbuf++]=c;
else {
rsz_cbuf();
psh_cbuf(c);
}
}
cls *new_cls() {
cls *rv=(cls *)malloc(sizeof(cls));
assert(rv);
rv->lits=(int *)malloc((fclbuf+1)*sizeof(int));
assert(rv->lits);
memcpy(rv->lits, clbuf, fclbuf*sizeof(int));
rv->lits[fclbuf]=0;
return rv;
}
void del_cls(cls *c) {
free(c->lits);
free(c);
}
/* call this one when resetting the solver */
void tear_down() {
int i=0;
/* delete all clauses */
for (; i<lccbuf; i++)
del_cls(cbuf[i]);
free(cbuf);
cbuf=NULL;
/* clear clause buffer */
free(clbuf);
clbuf=NULL;
/* clear all length & size counters */
lccbuf=0;
sccbuf=0;
sclbuf=0;
fclbuf=0;
}
int add_picosat(cls *c) {
int i=0;
assert(c);
while (c->lits[i])
picosat_add(c->lits[i++]);
return picosat_add(0);
}
/* add_clause: int list -> unit */
value add_cls(value ls) {
CAMLparam1(ls);
CAMLlocal1(p);
cls *c=NULL;
if (!initialized) {
out(stderr, "Solver not yet initialized, no clause added!\n");
CAMLreturn(Val_unit);
}
p=ls;
cl_clbuf();
while (p!=Val_int(0)) {
psh_clbuf(Int_val(Field(p, 0)));
p=Field(p,1);
}
print_clbuf();
c=new_cls();
assert(add_picosat(c)==lccbuf);
psh_cbuf(c);
cl_clbuf();
CAMLreturn(Val_unit);
}
/* add_clauses: int list list -> unit */
value add_clss(value ls) {
CAMLparam1(ls);
CAMLlocal1(p);
if (!initialized) {
out(stderr, "Solver not yet initialized, no clauses added!\n");
CAMLreturn(Val_unit);
}
p = ls;
while (p!=Val_int(0)) {
add_cls(Field(p, 0));
p=Field(p, 1);
}
CAMLreturn(Val_unit);
}
value cons(value v1, value v2) {
CAMLparam2(v1, v2);
CAMLlocal1(rv);
rv = caml_alloc(2,0);
Store_field(rv, 0, v1);
Store_field(rv, 1, v2);
CAMLreturn(rv);
}
/* get_clauses: unit -> int list list */
value get_clauses(value unit) {
CAMLparam1(unit);
CAMLlocal2(rv,acc);
int i=lccbuf-1;
rv=Val_emptylist;
while (i>=0) {
cls *c=cbuf[i--];
int j=0;
acc=Val_emptylist;
while (c->lits[j])
acc=cons(Val_int(c->lits[j++]), acc);
rv=cons(acc, rv);
}
CAMLreturn(rv);
}
value ps_unsat_core(value clauselim) {
CAMLparam1(clauselim);
CAMLlocal2(rv, acc);
int i=Int_val(clauselim);
if (i<0 || i>=lccbuf)
i=lccbuf-1;
rv=Val_emptylist;
if (!initialized) {
out(stderr, "Solver not yet initialized, no unsat core available!\n");
CAMLreturn(rv);
}
if (last_res==PICOSAT_UNSATISFIABLE) {
while (i>=0) {
if (picosat_coreclause(i)) {
int j=0;
cls *c=cbuf[i];
acc=Val_emptylist;
while (c->lits[j])
acc=cons(Val_int(c->lits[j++]), acc);
rv=cons(acc, rv);
}
i--;
}
}
CAMLreturn(rv);
}
value ps_get_model(value unit) {
CAMLparam1(unit);
CAMLlocal1(rv);
rv=Val_emptylist;
if (!initialized) {
out(stderr, "Solver not yet initialized, no model available!\n");
CAMLreturn(rv);
}
if (last_res==PICOSAT_SATISFIABLE) {
int i=1, lit=0;
for (; i<=picosat_variables(); i++)
if ((lit=i*picosat_deref(i))!=0)
rv=cons(Val_int(lit), rv);
}
CAMLreturn(rv);
}
value ps_init(value unit) {
CAMLparam1(unit);
if (initialized) {
out(stderr, "Solver already initialized!\n");
CAMLreturn(Val_unit);
}
initialized=1;
picosat_init();
if (!(tr_enabled=picosat_enable_trace_generation()))
out(stderr, "Solver doesn't support trace generation!\n");
else
out(stdout, "Solver with trace generation support prepared.\n");
CAMLreturn(Val_unit);
}
value ps_reset(value unit) {
CAMLparam1(unit);
if (!initialized) {
out(stderr, "Solver not initialized, couldn't reset it!\n");
CAMLreturn(Val_unit);
}
tear_down();
initialized=0;
tr_enabled=0;
last_res=PICOSAT_UNKNOWN;
picosat_reset();
CAMLreturn(Val_unit);
}
value ps_sat(value timeout) {
CAMLparam1(timeout);
if (!initialized) {
out(stderr, "Solver not initialized, couldn't check satisfiability!\n");
CAMLreturn(Val_int(PICOSAT_UNKNOWN));
}
last_res=picosat_sat(Int_val(timeout));
CAMLreturn(Val_int(last_res));
}
static int out(FILE *f, char *fmt, ...) {
int rv;
va_list ap;
va_start(ap, fmt);
rv=vfprintf(f, fmt, ap);
fflush(f);
va_end(ap);
return rv;
}