/
prolog.js
525 lines (448 loc) · 15.9 KB
/
prolog.js
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
function Prolog(module, args) {
this.module = module;
this.args = args;
this.bindings = {};
this._bind();
this._initialise();
}
const PL_VARIABLE = (1);
const PL_ATOM = (2);
const PL_INTEGER = (3);
const PL_RATIONAL = (4);
const PL_FLOAT = (5);
const PL_STRING = (6);
const PL_TERM = (7);
const PL_NIL = (8);
const PL_BLOB = (9);
const PL_LIST_PAIR = (10);
const PL_FUNCTOR = (11);
const PL_LIST = (12);
const PL_CHARS = (13);
const PL_POINTER = (14);
const PL_CODE_LIST = (15);
const PL_CHAR_LIST = (16);
const PL_BOOL = (17);
const PL_FUNCTOR_CHARS = (18);
const _PL_PREDICATE_INDICATOR = (19);
const PL_SHORT = (20);
const PL_INT = (21);
const PL_LONG = (22);
const PL_DOUBLE = (23);
const PL_NCHARS = (24);
const PL_UTF8_CHARS = (25);
const PL_UTF8_STRING = (26);
const PL_INT64 = (27);
const PL_NUTF8_CHARS = (28);
const PL_NUTF8_CODES = (29);
const PL_NUTF8_STRING = (30);
const PL_NWCHARS = (31);
const PL_NWCODES = (32);
const PL_NWSTRING = (33);
const PL_MBCHARS = (34);
const PL_MBCODES = (35);
const PL_MBSTRING = (36);
const PL_INTPTR = (37);
const PL_CHAR = (38);
const PL_CODE = (39);
const PL_BYTE = (40);
const PL_PARTIAL_LIST = (41);
const PL_CYCLIC_TERM = (42);
const PL_NOT_A_LIST = (43);
const PL_DICT = (44);
const REP_ISO_LATIN_1 = 0x00000000;
const REP_UTF8 = 0x00100000;
const REP_MB = 0x00200000;
const REP_FN = REP_UTF8;
const CVT_ATOM = 0x00000001;
const CVT_STRING = 0x00000002;
const CVT_LIST = 0x00000004;
const CVT_INTEGER = 0x00000008;
const CVT_RATIONAL = 0x00000010;
const CVT_FLOAT = 0x00000020;
const CVT_VARIABLE = 0x00000040;
const CVT_NUMBER = (CVT_INTEGER|CVT_RATIONAL|CVT_FLOAT);
const CVT_ATOMIC = (CVT_NUMBER|CVT_ATOM|CVT_STRING);
const CVT_WRITE = 0x00000080;
const CVT_WRITE_CANONICAL = 0x00000100;
const CVT_WRITEQ = 0x00000200;
const CVT_ALL = (CVT_ATOMIC|CVT_LIST);
const CVT_MASK = 0x00000fff;
const CVT_EXCEPTION = 0x00001000;
const CVT_VARNOFAIL = 0x00002000;
const BUF_DISCARDABLE = 0x00000000;
const BUF_STACK = 0x00010000;
const BUF_MALLOC = 0x00020000;
const BUF_ALLOW_STACK = 0x00040000;
const PL_Q_NORMAL = 0x0002;
const PL_Q_NODEBUG = 0x0004;
const PL_Q_CATCH_EXCEPTION = 0x0008;
const PL_Q_PASS_EXCEPTION = 0x0010;
const PL_Q_ALLOW_YIELD = 0x0020;
const PL_Q_EXT_STATUS = 0x0040;
const PL_S_EXCEPTION = -1;
const PL_S_FALSE = 0;
const PL_S_TRUE = 1;
const PL_S_LAST = 2;
const PL_S_YIELD = 255;
const PL_WRT_QUOTED = 0x0001;
const PL_WRT_NEWLINE = 0x2000;
// Creates bindings to the SWI foreign API.
Prolog.prototype._bind = function() {
this.bindings._PL_streams = this.module.cwrap(
'_PL_streams', 'number', []);
this.bindings.PL_atom_chars = this.module.cwrap(
'PL_atom_chars', 'number', ['number']);
this.bindings.PL_functor_arity = this.module.cwrap(
'PL_functor_arity', 'number', ['number']);
this.bindings.PL_functor_name = this.module.cwrap(
'PL_functor_name', 'number', ['number']);
this.bindings.PL_get_functor = this.module.cwrap(
'PL_get_functor', 'number', ['number', 'number']);
this.bindings.PL_get_chars = this.module.cwrap(
'PL_get_chars', 'number', ['number', 'number', 'number']);
this.bindings.PL_get_arg = this.module.cwrap(
'PL_get_arg', 'number', ['number', 'number', 'number']);
this.bindings.PL_get_integer = this.module.cwrap(
'PL_get_integer', 'number', ['number', 'number']);
this.bindings.PL_put_chars = this.module.cwrap(
'PL_put_chars', 'number', ['number', 'number', 'number', 'number']);
this.bindings.PL_unify = this.module.cwrap(
'PL_unify', 'number', ['number', 'number']);
this.bindings.PL_is_string = this.module.cwrap(
'PL_is_string', 'number', ['number']);
this.bindings.PL_initialise = this.module.cwrap(
'PL_initialise', 'number', ['number', 'number']);
this.bindings.PL_new_atom = this.module.cwrap(
'PL_new_atom', 'number', ['string']);
this.bindings.PL_register_atom = this.module.cwrap(
'PL_register_atom', null, ['number']);
this.bindings.PL_unregister_atom = this.module.cwrap(
'PL_unregister_atom', null, ['number']);
this.bindings.PL_new_module = this.module.cwrap(
'PL_new_module', 'number', ['number']);
this.bindings.PL_new_functor = this.module.cwrap(
'PL_new_functor', 'number', ['number', 'number']);
this.bindings.PL_new_term_ref = this.module.cwrap(
'PL_new_term_ref', 'number', []);
this.bindings.PL_put_functor = this.module.cwrap(
'PL_put_functor', 'number', ['number', 'number']);
this.bindings.PL_put_term_from_chars = this.module.cwrap(
'PL_put_term_from_chars', 'number',['number','number','number','string']);
this.bindings.PL_write_term = this.module.cwrap(
'PL_write_term', 'number', ['number', 'number', 'number', 'number']);
this.bindings.PL_call = this.module.cwrap(
'PL_call', 'number', ['number', 'number']);
this.bindings.PL_unify_arg = this.module.cwrap(
'PL_unify_arg', 'number', ['number', 'number', 'number']);
this.bindings.PL_open_foreign_frame = this.module.cwrap(
'PL_open_foreign_frame', 'number', []);
this.bindings.PL_close_foreign_frame = this.module.cwrap(
'PL_close_foreign_frame', 'number', ['number']);
this.bindings.PL_predicate = this.module.cwrap(
'PL_predicate', 'number', ['number', 'number', 'number']);
this.bindings.PL_open_query = this.module.cwrap(
'PL_open_query', 'number', ['number', 'number', 'number', 'number']);
this.bindings.PL_next_solution = this.module.cwrap(
'PL_next_solution', 'number', ['number']);
this.bindings.PL_close_query = this.module.cwrap(
'PL_close_query', 'number', ['number']);
this.bindings.WASM_ttymode = this.module.cwrap(
'WASM_ttymode', 'number', []);
this.bindings.WASM_yield_request = this.module.cwrap(
'WASM_yield_request', 'number', []);
this.bindings.WASM_set_yield_result = this.module.cwrap(
'WASM_set_yield_result', 'number', ['number']);
};
// See http://www.swi-prolog.org/pldoc/doc_for?object=c(%27PL_initialise%27)
Prolog.prototype._initialise = function() {
let argv0 = this.args || [];
argv0.unshift("swipl");
let argv = argv0.map(function(arg) {
return this.module.allocate(
this.module.intArrayFromString(arg),
'i8', this.module.ALLOC_NORMAL);
}, this);
var ptr = this.module._malloc(argv.length * 4);
argv.forEach(function(arg, i) {
this.module.setValue(ptr + i * 4, arg, '*');
}, this);
if (!this.bindings.PL_initialise(argv.length, ptr)) {
throw new Error('SWI-Prolog initialisation failed.');
}
this.call("set_prolog_flag(color_term, false).");
};
/**
* Call a Prolog goal. This function deals with many variations to
* call Prolog.
*
* @param {String} [goal] Goal to run
* @param {String} [opts.module] Module in which to call Goal
* @param {Boolean} [opts.async] Call as yieldable
*/
Prolog.prototype.call = function(goal, opts)
{ opts = opts||{};
if ( typeof(goal) === "string" )
{ return this.with_frame(function()
{ const term = this.new_term_ref();
if ( !this.chars_to_term(goal, term) )
throw new Error('Query has a syntax error: ' + query);
if ( !opts.async )
{ const module = opts.module ? this.new_module(opts.module) : 0;
return !!this.bindings.PL_call(term, module);
} else
{ return this.call_yieldable(term, opts.module);
}
});
}
}
/**
* Call code while reclaiming possibly allocated term_t references.
* @param `f` function to be called
*/
Prolog.prototype.with_frame = function(f) {
const fid = this.bindings.PL_open_foreign_frame();
if ( fid )
{ const rc = f.call(this);
this.bindings.PL_close_foreign_frame(fid);
return rc;
}
return false; /* Throw? */
}
Prolog.prototype.string_to_c = function(string)
{ const len = this.module.lengthBytesUTF8(string);
const ptr = this.module._malloc(len+1);
return { ptr: this.module.stringToUTF8(string, ptr, len),
length: len
}
}
/**
* Get a reference to a predicate
* @param name {String} Name of the predicate
* @param arity {Integer} Arity of the predicate
* @param module {String} Module to resolve the predicate
*/
Prolog.prototype.predicate = function(name, arity, module)
{ const c_name = allocateUTF8(name);
const c_module = allocateUTF8(module);
const pred = this.bindings.PL_predicate(c_name, arity, c_module);
this.module._free(c_name);
this.module._free(c_module);
return pred;
}
/**
* Lookup a Prolog module by name
*
* @param {String} name Name of the module
* @return {module_t} Handle to the Prolog module
*/
Prolog.prototype.new_module = function(name)
{ const c_atom = this.new_atom(name);
const module = this.bindings.PL_new_module(c_atom);
this.unregister_atom(c_atom);
return module;
}
/**
* Open a new query
*
* @param {String} [module] Optional module name
*/
Prolog.prototype.open_query = function(module, flags, pred, argv)
{ const c_module = module ? this.new_module(module) : 0;
return this.bindings.PL_open_query(c_module, flags, pred, argv);
}
Prolog.prototype.close_query = function(query)
{ return this.bindings.PL_close_query(query);
}
Prolog.prototype.next_solution = function(query)
{ return this.bindings.PL_next_solution(query);
}
/**
* @return {IOSTREAM*} as a number
*/
Prolog.prototype.stream = function(name)
{ const iob = this.bindings._PL_streams(); /* IOSTREAM** */
let offset = undefined;
switch(name)
{ case "user_input": offset = 0; break;
case "user_output": offset = 1; break;
case "user_error": offset = 2; break;
default: throw(`Unknown stream ${name}`);
}
return this.module.getValue(iob+offset*4, 'i32');
}
Prolog.prototype.write = function(term, opts)
{ opts = opts||{};
const precedence = opts.precedence||1200;
const flags = opts.flags == undefined ? PL_WRT_QUOTED|PL_WRT_NEWLINE
: flags;
let s = undefined;
if ( opts.stream )
{ if ( typeof(stream) === "string" )
s = this.stream(opts.stream);
} else
{ s = this.stream("user_output");
}
return this.bindings.PL_write_term(s, term, precedence, flags);
}
// Return the arity of the given functor.
Prolog.prototype.functor_arity = function(functor) {
return this.bindings.PL_functor_arity(functor);
};
// Return an atom representing the name of the given functor.
Prolog.prototype.functor_name = function(functor) {
return this.bindings.PL_functor_name(functor);
};
// Returns functor of the given term.
// Returns null when the term is not a compound.
Prolog.prototype.get_functor = function(term) {
var ptr = this.module._malloc(4);
if (this.bindings.PL_get_functor(term, ptr)) {
var functor = this.module.getValue(ptr, 'i32');
this.module._free(ptr);
return functor;
} else {
this.module._free(ptr);
return null;
}
};
// Returns integer number for the given term.
// Returns null when the term is not an integer.
Prolog.prototype.get_integer = function(term) {
var ptr = this.module._malloc(4);
if (this.bindings.PL_get_integer(term, ptr)) {
var number = this.module.getValue(ptr, 'i32');
this.module._free(ptr);
return number;
} else {
this.module._free(ptr);
return null;
}
};
// Implements PL_put_chars for string case.
Prolog.prototype.put_chars_string = function(term, string) {
var c = this.string_to_c(string);
var ret = !!this.bindings.PL_put_chars(term, PL_STRING|REP_UTF8, c.length, c.ptr);
this.module._free(c.ptr);
return ret;
};
// Unifies the terms. Returns false if the terms
// do not unify.
Prolog.prototype.unify = function(term1, term2) {
return !!this.bindings.PL_unify(term1, term2);
};
// Returns whether the term is a string.
Prolog.prototype.is_string = function(term) {
return !!this.bindings.PL_is_string(term);
};
// Return a C-string for the text represented by the given atom.
// FIXME: Return from atom is ISO Latin 1.
Prolog.prototype.atom_chars = function(atom) {
var ptr = this.bindings.PL_atom_chars(atom);
if (ptr === 0) {
return null;
} else {
return this.module.UTF8ToString(ptr);
}
};
// Get the TTY mode as one of "notty", "raw" or "cooked"
Prolog.prototype.ttymode = function() {
return this.module.UTF8ToString(this.bindings.WASM_ttymode());
}
Prolog.prototype.yield_request = function()
{ return this.module.UTF8ToString(this.bindings.WASM_yield_request());
};
Prolog.prototype.set_yield_result = function(string)
{ const c_str = allocateUTF8(string);
this.bindings.WASM_set_yield_result(c_str);
this.module._free(c_str);
};
/**
* Call a predicate that may yield.
*
* @param {term_t} [term] Prolog goal to be called
* @param {String} [module] Module in which to call the goal.
* @return one of `false` (call failed), `true` (call succeeded),
* `undefined` (raised an exception (TBD)) or an object if the
* predicated yielded control back.
*/
Prolog.prototype.call_yieldable = function(term, module) {
var pred_call1;
if ( !pred_call1 )
pred_call1 = this.predicate("call", 1, "system");
const q = this.open_query(module, PL_Q_NORMAL|PL_Q_ALLOW_YIELD|PL_Q_EXT_STATUS,
pred_call1, term);
function next(prolog)
{ const rc = prolog.next_solution(q);
if ( rc == PL_S_YIELD )
{ return { yield: prolog.yield_request(),
query: q,
resume: (value) =>
{ prolog.set_yield_result(value);
return next(prolog);
}
};
} else
{ prolog.close_query(q);
return ( rc == PL_S_FALSE ? false :
rc == PL_S_EXCEPTION ? undefined :
true
);
}
}
return next(this);
};
/**
* Parse string and put the resulting Prolog term into the term t.
*/
Prolog.prototype.chars_to_term = function(string, t) {
return this.bindings.PL_put_term_from_chars(t, REP_UTF8, -1, string);
};
// Converts the argument term to a string.
Prolog.prototype.get_chars = function(term) {
var ptr = this.module._malloc(4);
var flags = CVT_ALL|CVT_WRITEQ|CVT_EXCEPTIONATOM;
if (this.bindings.PL_get_chars(term, ptr, flags)) {
// TODO properly free.
return this.module.UTF8ToString(this.module.getValue(ptr, 'i32'));
} else {
return null;
}
};
// If t is compound and index is between 1 and arity (inclusive),
// assign a with a term reference to the argument.
Prolog.prototype.get_arg = function(index, term, arg) {
return this.bindings.PL_get_arg(index, term, arg);
};
// Return an atom handle for the given C-string.
Prolog.prototype.new_atom = function(string) {
return this.bindings.PL_new_atom(string);
};
/**
* Register an atom
*/
Prolog.prototype.register_atom = function(atom) {
this.bindings.PL_register_atom(atom);
return atom;
};
Prolog.prototype.unregister_atom = function(atom) {
this.bindings.PL_unregister_atom(atom);
};
// Returns a functor identifier, a handle for the name/arity pair.
Prolog.prototype.new_functor = function(atom, arity) {
return this.bindings.PL_new_functor(atom, arity);
};
// Return a fresh reference to a term.
Prolog.prototype.new_term_ref = function() {
return this.bindings.PL_new_term_ref();
};
// Create a new compound term from functor and bind t to this term.
Prolog.prototype.put_functor = function(term, functor) {
return this.bindings.PL_put_functor(term, functor);
};
// Unifies the index-th argument (1-based) of term with arg.
Prolog.prototype.unify_arg = function(index, term, arg) {
return this.bindings.PL_unify_arg(index, term, arg);
};
Module.onRuntimeInitialized = function() {
Module.prolog = new Prolog(Module, Module.arguments);
};