-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprolog.h
253 lines (230 loc) · 8.04 KB
/
prolog.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
#include <stdlib.h>
#include <stdio.h>
#include "prolog_config.h"
/*
* # USING THIS LIBRARY:
*
* First, create a `PrologDatabase` with `prolog_database_init()`.
* Then, add facts (`Term`s) to it with `prolog_addFact()`.
* To create `Term`s, call `prolog_newAtom()`, `prolog_newVariable()`,
* and `prolog_newCompund()` using &(database->arena).
*
* Once your database is built, create a `PrologGoal` with
* `prolog_goal_init()`. Now add to your query the same way you added to your
* database, using the same functions you used to construct `Term`s. Pass
* `&(query->arena)` as the first argument. Then add what you want to prove to
* the query with `prolog_addToQuery()`.
*
* Once you're ready, call `prolog_resolve()`. This will attempt to prove the
* theorem by unifying the query against facts in the database.
*
* You can also call `prolog_unify()`. It will return if both terms can be
* true, and which variables need to be bound to what for that to happen.
* If all you need to do is unify some terms, then `prolog_unify()` does
* not actually require that you create a database.
*
* Finally, clean up the memory. The `PrologArena`s inside `PrologDatabase`
* and `PrologGoal` are fixed size, and are allocated by the user. You are
* the one who knows how to clean them up. do not need to be cleaned up. They
* hold all the memory used by the `prolog_newX()` functions used to create
* `Term`s, and so those don't need to be cleaned up either. They're cleaned
* up implicitly when the database or goal you created is freed. However, the
* results of `prolog_unify()` and `prolog_resolve()` do allocate memory to
* store their result lists. Destroy them with `prolog_unificationDestroy()`
* and `prolog_resolutionDestroy()` respectively.
*
* Memory (specifically bound variables) inside the database and query are
* modified by `prolog_resolve()`, and `prolog_unify()` modifies the arenas
* that back each `Term` attempting to unify. Therefore `PrologResolution` and
* `PrologUnification` that you receive back respectively contain pointers
* into the `PrologQuery` and `PrologDatabase`. This means that you should not
* run `prolog_resolve()` or `prolog_unify()` on the same query or same
* database across multiple threads, and you should not destroy either until
* you're done with the result. You can however choose to give each thread
* its own query and its own copy of the database.
*/
DECL(Term);
STRUCT(Variable) {
char* name;
Term* bound;
};
STRUCT(Atom) {
char* name;
};
STRUCT(Compound) {
char* name;
size_t arity;
Term** components;
};
STRUCT(Term) {
union {
Atom a;
Variable v;
Compound c;
};
#define PROLOG_KIND_ATOM 1
#define PROLOG_KIND_VARIABLE 2
#define PROLOG_KIND_COMPOUND 3
unsigned char kind;
};
STRUCT(PrologArena) {
char buf[PROLOG_ARENA_SIZE];
size_t bump;
} arena;
STRUCT(PrologDatabase) {
PrologArena arena;
struct {
Term** facts;
size_t size;
size_t cap;
};
};
STRUCT(PrologGoal) {
PrologArena arena;
};
PROLOG_PRIVATE size_t _prolog_align(size_t n, size_t align) { return (n + align - 1) & -align; }
PROLOG_PRIVATE char* prolog_alloc(PrologArena* a, size_t bytes, size_t align) {
size_t from = _prolog_align(a->bump, align);
a->bump = from + bytes;
#if PROLOG_CATCH_OOM
if ((from + bytes) >= PROLOG_ARENA_SIZE)
fprintf(stderr, "Prolog OOM.\n"), exit(1);
#endif
return a->buf + from;
}
PROLOG_PUBLIC PrologDatabase* prolog_database_init(PrologDatabase* db) {
db->bump = 0;
db->facts = NULL;
db->size = 0;
db->cap = 0;
return ctx;
}
PROLOG_PUBLIC Term* prolog_newVariable(PrologArena* a, char* name) {
Term* t = (Term*)prolog_alloc(a, sizeof(Term), _Alignof(Term));
char* n = (char*)prolog_alloc(a, strlen(name), 1);
t.kind = PROLOG_KIND_VARIABLE;
t.v.name = strcpy(n, name);
t.v.bound = NULL;
return t;
}
PROLOG_PUBLIC Term* prolog_newAtom(PrologArena* a, char* name) {
Term* t = (Term*)prolog_alloc(a, sizeof(Term), _Alignof(Term));
char* n = (char*)prolog_alloc(a, strlen(name), 1);
t.kind = PROLOG_KIND_ATOM;
t.a.name = strcpy(n, name);
return t;
}
PROLOG_PUBLIC Term* prolog_newCompound(PrologArena* ctx, char* name, size_t arity, ...) {
Term* t = (Term*)prolog_alloc(a, sizeof(Term), _Alignof(Term));
char* n = (char*)prolog_alloc(a, strlen(name), 1);
Term** c = (Term**)prolog_alloc(a, sizeof(Term*) * arity, _Alignof(Term*));
t.kind = PROLOG_KIND_COMPOUND;
t.c.arity = arity;
t.c.name = strcpy(n, name);
t.c.components = c;
va_list argp;
va_start(argp, arity);
for (size_t i = 0; i < arity; i++)
t.c.components[i] = va_arg(argp, Term*);
va_end(argp);
return t;
}
PROLOG_PUBLIC void prolog_addFact(PrologDatabase* db, Term* fact) {
if (!db->facts) {
db->facts = (Term**)malloc(sizeof(Term) * 64);
db->cap = 64;
#if PROLOG_CATCH_OOM
if (!db->facts)
fprintf(stderr, "Prolog OOM.\n"), exit(1);
#endif
} else if (db->size >= db->cap) {
db->facts = (Term**)realloc(sizeof(Term) * (db->cap *= 2))
#if PROLOG_CATCH_OOM
if (!db->facts)
fprintf(stderr, "Prolog OOM.\n"), exit(1);
#endif
}
db->facts[db->size++] = fact;
}
STRUCT(PrologUnification) {
int success;
TermPair* unifiers;
size_t size;
size_t _cap;
};
PROLOG_PUBLIC void prolog_unificationDestroy(PrologUnification* u) {
if (u->unifiers)
free(u->unifiers);
u->unifiers = NULL;
u->size = 0;
u->_cap = 0;
}
PROLOG_PRIVATE void _prolog_replaceVars(Term* in, Term* with) {
if (in->kind == PROLOG_KIND_VARIABLE)
in->v.bound = with;
else if (in->kind == PROLOG_KIND_COMPOUND)
for (size_t i = 0; i < in->c.arity; i++)
_prolog_replaceVars(in->components[i], with);
else { /* Do nothing for Atoms */ (void)with; }
}
PROLOG_PRIVATE void _prolog_push_unifier(PrologUnification* u, Term* a, Term* b) {
if (u->size >= u->_cap) {
u->unifiers = (Term**)realloc(u->unifiers, sizeof(Term*) * (u->_cap *= 2));
#if PROLOG_CATCH_OOM
if (!u->unifiers)
fprintf(stderr, "Prolog OOM.\n"), exit(1);
#endif
}
u->unifiers[u->size++] = (TermPair){a, b};
}
PROLOG_PUBLIC PrologUnification prolog_unify(PrologContext* ctx, Term* t1, Term* t2) {
typedef struct { Term* x; Term* y; } TermPair;
PrologUnification q = {1, (TermPair**)malloc(sizeof(TermPair) * 64), 0, 64};
#if PROLOG_CATCH_OOM
if (!q.unifiers)
fprintf(stderr, "Prolog OOM.\n"), exit(1);
#endif
TermPair stack[PROLOG_STACK_SIZE];
stack[0].x = t1, stack[0].y = t2;
size_t stack_height = 1;
while (stack_height) {
if (stack_height >= PROLOG_STACK_HEIGHT)
fprintf("Prolog stack overflow.\n"), exit(1);
TermPair tp = stack[--stack_height];
if (((tp.x.kind == PROLOG_KIND_VARIABLE) & (tp.y.kind != PROLOG_KIND_VARIABLE)) |
((tp.x.kind == PROLOG_KIND_VARIABLE) & (tp.x.kind != PROLOG_KIND_VARIABLE))) {
Term* var = (tp.x.kind == PROLOG_KIND_VARIABLE) ? tp.x : tp.y;
/* Substitute variable in stack */
for (size_t i = 0; i < stack_height; i++) {
_prolog_replaceVars(stack[i].x, var);
_prolog_replaceVars(stack[i].y, var);
}
_prolog_push_unifier(&q, tp.x, tp.y);
}
else if (((tp.x.kind == PROLOG_KIND_VARIABLE) | (tp.x.kind == PROLOG_KIND_ATOM)) &
((tp.y.kind == PROLOG_KIND_VARIABLE) | (tp.y.kind == PROLOG_KIND_ATOM))) {
Term* xbound = (tp.x.kind == PROLOG_KIND_VARIABLE) ? tp.x.v.bound : tp.x.a.bound;
Term* ybound = (tp.y.kind == PROLOG_KIND_VARIABLE) ? tp.y.v.bound : tp.y.a.bound;
/* intentional ptr comp, unify if x and y bound to same term, or both to no term. */
if (xbound == ybound)
continue;
}
else if (((tp.x.kind == PROLOG_KIND_COMPOUND) & (tp.y.kind == PROLOG_KIND_COMPOUND))
&& (tp.x.c.arity) && (tp.x.c.arity == tp.y.c.arity)) {
/* Unify all subterms */
for (size_t i = 0; i < tp.x.c.arity; i++)
stack[stack_height++] = (TermPair){tp.x.c.components[i], tp.y.c.components[i]};
}
else {
q.success = 0;
break;
}
}
if (!q.success)
prolog_unificationDestroy(&q);
return q;
}
PROLOG_PRIVATE _prolog_backtrack(PrologContext* ctx) {
}
PROLOG_PUBLIC prolog_resolve(PrologContext* ctx, Goal* query) {
}