eyemind 0.2.10 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/SPEC.md +164 -47
- package/package.json +1 -1
- package/src/cli.js +1 -1
- package/src/closure.js +409 -0
- package/src/engine.js +120 -1
- package/src/index.js +1 -1
- package/src/program.js +1 -1
- package/src/proof.js +157 -43
- package/src/version.js +1 -1
package/src/closure.js
ADDED
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
// Search certificates: what settles a negation or a collection.
|
|
2
|
+
//
|
|
3
|
+
// A negation holds when its goal has no solution, and a collection lists
|
|
4
|
+
// every solution; no step can show either. A search certificate can. For each
|
|
5
|
+
// call the search behind the boundary made, it lists every answer, in search
|
|
6
|
+
// order: searched(Call, Answers). For each predicate that forward rules derive
|
|
7
|
+
// and the search consulted, it lists every derived fact: derived(Pattern,
|
|
8
|
+
// Facts). A table is checked by expanding its call one level against the
|
|
9
|
+
// program, as a search would, with every inner call answered by the table of
|
|
10
|
+
// that call, so no table vouches for itself and a wrong one shows as a
|
|
11
|
+
// disagreement. A listing is checked by its facts' own steps, which show each
|
|
12
|
+
// was derived, and by closure: under the listed facts no forward rule derives
|
|
13
|
+
// a fact of that predicate the listing leaves out. Nothing is searched for.
|
|
14
|
+
//
|
|
15
|
+
// The order of derived facts cannot be checked this way, so a collection or
|
|
16
|
+
// a once/1 whose answers rest on a listing stays an obligation, and so does a
|
|
17
|
+
// boundary whose search reaches rule templates or inspection.
|
|
18
|
+
import { PRIMITIVE_KEYS, primitive } from './builtins.js';
|
|
19
|
+
import { freshClause, text, variant } from './common.js';
|
|
20
|
+
import { INSPECTION_KEYS } from './inspection.js';
|
|
21
|
+
import {
|
|
22
|
+
Env, EyemindError, Struct, Var, copyResolved, deref, flattenConjunction, freshTerm, identity, isCallable, isGround, isTerm, key,
|
|
23
|
+
listFromItems, unify, variables,
|
|
24
|
+
} from './terms.js';
|
|
25
|
+
|
|
26
|
+
// A search the certificate cannot vouch for: its boundary stays an obligation.
|
|
27
|
+
export class Unsettled extends Error {}
|
|
28
|
+
|
|
29
|
+
// What one expansion relied on: the tables it read, the listings it read and
|
|
30
|
+
// the tables whose answer order it needed.
|
|
31
|
+
export class Reliance {
|
|
32
|
+
constructor(ordered = false) {
|
|
33
|
+
this.tables = new Set();
|
|
34
|
+
this.listings = new Set();
|
|
35
|
+
this.ordered = new Set();
|
|
36
|
+
this.inner = ordered ? 1 : 0;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Whether term is an instance of pattern, whose variables are its own.
|
|
41
|
+
function instanceOf(term, pattern) {
|
|
42
|
+
const env = new Env();
|
|
43
|
+
return unify(pattern, term, env) && variant(copyResolved(term, env)) === variant(term);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// The name and arity of every relation a forward rule concludes.
|
|
47
|
+
export function forwardKeys(program) {
|
|
48
|
+
const keys = new Set();
|
|
49
|
+
for (const clause of program.forward) {
|
|
50
|
+
for (const head of clause.plainHeads) {
|
|
51
|
+
if (isCallable(head) && head !== 'true' && head !== 'false') keys.add(key(head));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return keys;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Settles boundaries against a source of certificate statements:
|
|
58
|
+
// answers(call) gives the answers listed for a call, or null; facts(goalKey)
|
|
59
|
+
// the facts listed for a predicate forward rules derive, or null; and
|
|
60
|
+
// supported(fact) whether the proof has a step for a listed fact. Tables and
|
|
61
|
+
// listings are checked once, however many boundaries rest on them.
|
|
62
|
+
export class Settler {
|
|
63
|
+
constructor(program, source) {
|
|
64
|
+
this.program = program;
|
|
65
|
+
this.source = source;
|
|
66
|
+
this.derivedKeys = forwardKeys(program);
|
|
67
|
+
this.tables = new Map();
|
|
68
|
+
this.listings = new Map();
|
|
69
|
+
this.serial = 0;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// The outcome for one negation or collection: status 'settled',
|
|
73
|
+
// 'unsettled' or 'failed', the failures as [detail, term] pairs, whether the
|
|
74
|
+
// only failure is the boundary's own result, and the tables and listings it
|
|
75
|
+
// rests on.
|
|
76
|
+
settle(boundary) {
|
|
77
|
+
const collecting = isTerm(boundary, 'findall', 3);
|
|
78
|
+
const goal = collecting ? boundary.args[1] : boundary.args[0];
|
|
79
|
+
const root = new Reliance(collecting);
|
|
80
|
+
const outcome = { status: 'settled', failures: [], boundaryOnly: false, tables: [], listings: [] };
|
|
81
|
+
let found = [];
|
|
82
|
+
try {
|
|
83
|
+
const env = new Env();
|
|
84
|
+
const copied = freshTerm(collecting ? new Struct('-', [boundary.args[0], goal]) : goal, `root${++this.serial}`);
|
|
85
|
+
const [template, searched] = collecting ? copied.args : [null, copied];
|
|
86
|
+
for (const _ of this.solve(searched, env, root)) { // eslint-disable-line no-unused-vars
|
|
87
|
+
found.push(collecting ? freshTerm(copyResolved(template, env), `item${++this.serial}`) : null);
|
|
88
|
+
if (!collecting) break;
|
|
89
|
+
}
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (error instanceof Unsettled) return { ...outcome, status: 'unsettled' };
|
|
92
|
+
if (!(error instanceof EyemindError)) throw error;
|
|
93
|
+
outcome.failures.push([`search certificate leads to an error: ${error.message}`, boundary]);
|
|
94
|
+
found = null;
|
|
95
|
+
}
|
|
96
|
+
let unsettled = false;
|
|
97
|
+
const tables = new Set();
|
|
98
|
+
const listings = new Set();
|
|
99
|
+
const ordered = new Set(root.ordered);
|
|
100
|
+
const pendingTables = [...root.tables];
|
|
101
|
+
const pendingListings = [];
|
|
102
|
+
while (pendingTables.length || pendingListings.length) {
|
|
103
|
+
const fromTable = pendingTables.length > 0;
|
|
104
|
+
const entryKey = fromTable ? pendingTables.pop() : pendingListings.pop();
|
|
105
|
+
const seen = fromTable ? tables : listings;
|
|
106
|
+
if (seen.has(entryKey)) continue;
|
|
107
|
+
seen.add(entryKey);
|
|
108
|
+
const entry = fromTable ? this.checkTable(entryKey) : this.checkListing(entryKey);
|
|
109
|
+
if (entry.unsettled) unsettled = true;
|
|
110
|
+
if (entry.failure !== null) {
|
|
111
|
+
// A table or listing that fails is reported for the first boundary
|
|
112
|
+
// resting on it; later ones fail with it, without repeating it.
|
|
113
|
+
if (!entry.reported) outcome.failures.push([entry.failure, entry.subject]);
|
|
114
|
+
entry.reported = true;
|
|
115
|
+
outcome.failed = true;
|
|
116
|
+
}
|
|
117
|
+
pendingTables.push(...entry.reliance.tables);
|
|
118
|
+
pendingListings.push(...entry.reliance.listings);
|
|
119
|
+
for (const item of entry.reliance.ordered) ordered.add(item);
|
|
120
|
+
}
|
|
121
|
+
outcome.tables = [...tables];
|
|
122
|
+
outcome.listings = [...listings];
|
|
123
|
+
const circular = this.circularity(tables);
|
|
124
|
+
if (circular !== null) {
|
|
125
|
+
outcome.failures.push([`search certificate is circular at ${text(circular.call)}`, circular.call]);
|
|
126
|
+
}
|
|
127
|
+
if (outcome.failures.length || outcome.failed) return { ...outcome, status: 'failed' };
|
|
128
|
+
if (unsettled || [...ordered].some((item) => !this.exact(item))) return { ...outcome, status: 'unsettled' };
|
|
129
|
+
if (!collecting && found.length) {
|
|
130
|
+
outcome.failures.push([`absence contradicted by its search: ${text(boundary)}`, boundary]);
|
|
131
|
+
return { ...outcome, status: 'failed', boundaryOnly: true };
|
|
132
|
+
}
|
|
133
|
+
// A collection's list is the search's, renamed apart, bound perhaps by
|
|
134
|
+
// later goals. A step shows its goal as the whole solution left it, so a
|
|
135
|
+
// goal a later goal instantiated may no longer be the goal searched: a
|
|
136
|
+
// list that disagrees is left to the evidence rather than refuted.
|
|
137
|
+
if (collecting && !instanceOf(boundary.args[2], listFromItems(found))) return { ...outcome, status: 'unsettled' };
|
|
138
|
+
return outcome;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Each solution of a goal, its bindings made in env and undone before the
|
|
142
|
+
// next one, as a search finds them; inner calls are answered by tables.
|
|
143
|
+
* solve(goal, env, reliance) {
|
|
144
|
+
goal = deref(goal, env);
|
|
145
|
+
if (goal instanceof Var) throw new EyemindError('a called variable must be bound');
|
|
146
|
+
if (!isCallable(goal)) throw new EyemindError(`expected a callable goal, got ${text(goal)}`);
|
|
147
|
+
const goalKey = key(goal);
|
|
148
|
+
const mark = env.mark();
|
|
149
|
+
if (goalKey === 'and/2') {
|
|
150
|
+
for (const _ of this.solve(goal.args[0], env, reliance)) yield* this.solve(goal.args[1], env, reliance); // eslint-disable-line no-unused-vars
|
|
151
|
+
} else if (goalKey === 'or/2') {
|
|
152
|
+
yield* this.solve(goal.args[0], env, reliance);
|
|
153
|
+
yield* this.solve(goal.args[1], env, reliance);
|
|
154
|
+
} else if (goalKey === 'call/1') {
|
|
155
|
+
yield* this.solve(goal.args[0], env, reliance);
|
|
156
|
+
} else if (goalKey === 'once/1' || goalKey === 'not/1') {
|
|
157
|
+
// A negation's unbound variables stand for any value, as anonymous
|
|
158
|
+
// ones do; a reasoner would not have run one with a named variable.
|
|
159
|
+
const once = goalKey === 'once/1';
|
|
160
|
+
if (once) reliance.inner++;
|
|
161
|
+
const inner = this.solve(goal.args[0], env, reliance);
|
|
162
|
+
const first = inner.next();
|
|
163
|
+
if (once) reliance.inner--;
|
|
164
|
+
if (once && !first.done) yield;
|
|
165
|
+
inner.return();
|
|
166
|
+
env.undo(mark);
|
|
167
|
+
if (!once && first.done) yield;
|
|
168
|
+
} else if (goalKey === 'findall/3') {
|
|
169
|
+
const items = [];
|
|
170
|
+
reliance.inner++;
|
|
171
|
+
for (const _ of this.solve(goal.args[1], env, reliance)) { // eslint-disable-line no-unused-vars
|
|
172
|
+
items.push(freshTerm(copyResolved(goal.args[0], env), `collection${++this.serial}`));
|
|
173
|
+
}
|
|
174
|
+
reliance.inner--;
|
|
175
|
+
env.undo(mark);
|
|
176
|
+
if (unify(goal.args[2], listFromItems(items), env)) yield;
|
|
177
|
+
} else if (PRIMITIVE_KEYS.has(goalKey)) {
|
|
178
|
+
for (const _ of primitive(goal, env)) { // eslint-disable-line no-unused-vars
|
|
179
|
+
yield;
|
|
180
|
+
env.undo(mark);
|
|
181
|
+
}
|
|
182
|
+
} else if (INSPECTION_KEYS.has(goalKey)) {
|
|
183
|
+
throw new Unsettled();
|
|
184
|
+
} else if (!this.derivedKeys.has(goalKey) &&
|
|
185
|
+
(this.program.groups.get(goalKey) || []).every((clause) => !clause.body.length && clause.parentId === null)) {
|
|
186
|
+
// Facts alone are scanned where they stand, with no table needed.
|
|
187
|
+
yield* this.expand(goal, env, reliance);
|
|
188
|
+
} else {
|
|
189
|
+
const call = copyResolved(goal, env);
|
|
190
|
+
const callKey = variant(call);
|
|
191
|
+
let entry = this.tables.get(callKey);
|
|
192
|
+
if (entry === undefined) {
|
|
193
|
+
const answers = this.source.answers(call);
|
|
194
|
+
if (answers === null) throw new Unsettled();
|
|
195
|
+
entry = { call, answers, checked: false, failure: null, subject: call, unsettled: false, reported: false, reliance: null };
|
|
196
|
+
this.tables.set(callKey, entry);
|
|
197
|
+
}
|
|
198
|
+
reliance.tables.add(callKey);
|
|
199
|
+
if (reliance.inner) reliance.ordered.add(callKey);
|
|
200
|
+
for (const answer of entry.answers) {
|
|
201
|
+
if (unify(goal, freshTerm(answer, `answer${++this.serial}`), env)) yield;
|
|
202
|
+
env.undo(mark);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
env.undo(mark);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// A call expanded one level, as a search would: the derived facts of its
|
|
209
|
+
// relation in the order derived, then the program's facts and backward
|
|
210
|
+
// rules in clause order, each rule's body solved in its place.
|
|
211
|
+
* expand(call, env, reliance) {
|
|
212
|
+
const goalKey = key(call);
|
|
213
|
+
const mark = env.mark();
|
|
214
|
+
if (this.derivedKeys.has(goalKey)) {
|
|
215
|
+
const facts = this.listing(goalKey);
|
|
216
|
+
reliance.listings.add(goalKey);
|
|
217
|
+
for (const fact of facts) {
|
|
218
|
+
if (unify(call, fact, env)) yield;
|
|
219
|
+
env.undo(mark);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
for (const clause of this.program.groups.get(goalKey) || []) {
|
|
223
|
+
// A derived rule instance answers only through the run that made it.
|
|
224
|
+
if (clause.parentId !== null) throw new Unsettled();
|
|
225
|
+
const [head, body] = freshClause(clause, `clause${++this.serial}`);
|
|
226
|
+
if (unify(call, head, env)) yield* this.solveAll(body, 0, env, reliance);
|
|
227
|
+
env.undo(mark);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
* solveAll(goals, index, env, reliance) {
|
|
232
|
+
if (index === goals.length) {
|
|
233
|
+
yield;
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
for (const _ of this.solve(goals[index], env, reliance)) yield* this.solveAll(goals, index + 1, env, reliance); // eslint-disable-line no-unused-vars
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
listing(goalKey) {
|
|
240
|
+
let entry = this.listings.get(goalKey);
|
|
241
|
+
if (entry === undefined) {
|
|
242
|
+
const facts = this.source.facts(goalKey);
|
|
243
|
+
if (facts === null) throw new Unsettled();
|
|
244
|
+
entry = { facts, checked: false, failure: null, subject: null, unsettled: false, reported: false, reliance: null };
|
|
245
|
+
this.listings.set(goalKey, entry);
|
|
246
|
+
}
|
|
247
|
+
return entry.facts;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// A table holds when expanding its call gives exactly its answers, in order.
|
|
251
|
+
checkTable(callKey) {
|
|
252
|
+
const entry = this.tables.get(callKey);
|
|
253
|
+
if (entry.checked) return entry;
|
|
254
|
+
entry.checked = true;
|
|
255
|
+
entry.reliance = new Reliance();
|
|
256
|
+
try {
|
|
257
|
+
const call = freshTerm(entry.call, `table${++this.serial}`);
|
|
258
|
+
const env = new Env();
|
|
259
|
+
const found = [];
|
|
260
|
+
for (const _ of this.expand(call, env, entry.reliance)) found.push(copyResolved(call, env)); // eslint-disable-line no-unused-vars
|
|
261
|
+
const listed = entry.answers;
|
|
262
|
+
let i = 0;
|
|
263
|
+
while (i < found.length && i < listed.length && variant(found[i]) === variant(listed[i])) i++;
|
|
264
|
+
if (i < found.length && i < listed.length) {
|
|
265
|
+
entry.failure = `the search of ${text(entry.call)} answers ${text(found[i])} where its table lists ${text(listed[i])}`;
|
|
266
|
+
} else if (i < found.length) {
|
|
267
|
+
entry.failure = `the table of ${text(entry.call)} leaves out the answer ${text(found[i])}`;
|
|
268
|
+
} else if (i < listed.length) {
|
|
269
|
+
entry.failure = `the table of ${text(entry.call)} lists ${text(listed[i])}, which its search does not answer`;
|
|
270
|
+
}
|
|
271
|
+
} catch (error) {
|
|
272
|
+
if (error instanceof Unsettled) entry.unsettled = true;
|
|
273
|
+
else if (error instanceof EyemindError) entry.failure = `the search of ${text(entry.call)} leads to an error: ${error.message}`;
|
|
274
|
+
else {
|
|
275
|
+
// Checked again once whatever interrupted it is dealt with.
|
|
276
|
+
entry.checked = false;
|
|
277
|
+
throw error;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return entry;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// A listing holds when each fact has a step and no forward rule derives,
|
|
284
|
+
// under the listed facts, a fact of that relation it leaves out.
|
|
285
|
+
checkListing(goalKey) {
|
|
286
|
+
const entry = this.listings.get(goalKey);
|
|
287
|
+
if (entry.checked) return entry;
|
|
288
|
+
entry.checked = true;
|
|
289
|
+
entry.reliance = new Reliance();
|
|
290
|
+
const listed = new Set();
|
|
291
|
+
try {
|
|
292
|
+
for (const fact of entry.facts) {
|
|
293
|
+
if (!isCallable(fact) || key(fact) !== goalKey) {
|
|
294
|
+
entry.failure = `derived facts of ${goalKey} list ${text(fact)}`;
|
|
295
|
+
entry.subject = fact;
|
|
296
|
+
return entry;
|
|
297
|
+
}
|
|
298
|
+
// A pattern an inspection derived is not a fact closure can compare.
|
|
299
|
+
if (!isGround(fact)) throw new Unsettled();
|
|
300
|
+
if (!this.source.supported(fact)) {
|
|
301
|
+
entry.failure = `derived fact without a step: ${text(fact)}`;
|
|
302
|
+
entry.subject = fact;
|
|
303
|
+
return entry;
|
|
304
|
+
}
|
|
305
|
+
listed.add(identity(fact));
|
|
306
|
+
}
|
|
307
|
+
for (const rule of this.program.forward) {
|
|
308
|
+
if (!rule.plainHeads.some((head) => isCallable(head) && key(head) === goalKey)) continue;
|
|
309
|
+
if (rule.parentId !== null || rule.templates.length || rule.captures !== null) throw new Unsettled();
|
|
310
|
+
const missing = this.closes(rule, goalKey, listed, entry.reliance);
|
|
311
|
+
if (missing !== null) {
|
|
312
|
+
entry.failure = `derived facts of ${goalKey} leave out ${text(missing)}, which clause ${rule.id} derives`;
|
|
313
|
+
entry.subject = missing;
|
|
314
|
+
return entry;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
} catch (error) {
|
|
318
|
+
if (error instanceof Unsettled) entry.unsettled = true;
|
|
319
|
+
else if (error instanceof EyemindError) entry.failure = `the closure of ${goalKey} leads to an error: ${error.message}`;
|
|
320
|
+
else {
|
|
321
|
+
entry.checked = false;
|
|
322
|
+
throw error;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return entry;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// The first conclusion of rule with key goalKey, its witnesses made as a
|
|
329
|
+
// forward round makes them, that is neither listed nor a ground fact of the
|
|
330
|
+
// program; null when there is none.
|
|
331
|
+
closes(rule, goalKey, listed, reliance) {
|
|
332
|
+
const [head, body, names] = freshClause(rule, `closure${++this.serial}`);
|
|
333
|
+
const witnesses = new Set(rule.witnessNames.map((name) => names.get(name).name));
|
|
334
|
+
const heads = flattenConjunction(head).filter((item) => isCallable(item) && key(item) === goalKey);
|
|
335
|
+
const env = new Env();
|
|
336
|
+
for (const _ of this.solveAll(body, 0, env, reliance)) { // eslint-disable-line no-unused-vars
|
|
337
|
+
const mark = env.mark();
|
|
338
|
+
if (witnesses.size) {
|
|
339
|
+
const values = [];
|
|
340
|
+
for (const variable of variables(head).values()) {
|
|
341
|
+
if (witnesses.has(variable.name)) continue;
|
|
342
|
+
const value = copyResolved(variable, env);
|
|
343
|
+
if (!isGround(value)) throw new Unsettled();
|
|
344
|
+
values.push(value);
|
|
345
|
+
}
|
|
346
|
+
const firing = listFromItems(values);
|
|
347
|
+
for (const name of rule.witnessNames) {
|
|
348
|
+
unify(names.get(name), new Struct('skolem', [BigInt(rule.id), name, firing]), env);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
for (const item of heads) {
|
|
352
|
+
const conclusion = copyResolved(item, env);
|
|
353
|
+
if (!isGround(conclusion)) throw new Unsettled();
|
|
354
|
+
const conclusionId = identity(conclusion);
|
|
355
|
+
if (!listed.has(conclusionId) && !this.program.groundFactKeys.has(conclusionId)) return conclusion;
|
|
356
|
+
}
|
|
357
|
+
env.undo(mark);
|
|
358
|
+
}
|
|
359
|
+
return null;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// The first table found on a cycle of tables, or null: a search that met
|
|
363
|
+
// its own call again would never have finished.
|
|
364
|
+
circularity(tables) {
|
|
365
|
+
const state = new Map();
|
|
366
|
+
for (const start of tables) {
|
|
367
|
+
if (state.has(start)) continue;
|
|
368
|
+
const stack = [[start, [...this.tables.get(start).reliance.tables], 0]];
|
|
369
|
+
state.set(start, 'open');
|
|
370
|
+
while (stack.length) {
|
|
371
|
+
const top = stack[stack.length - 1];
|
|
372
|
+
if (top[2] >= top[1].length) {
|
|
373
|
+
state.set(top[0], 'done');
|
|
374
|
+
stack.pop();
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
const next = top[1][top[2]++];
|
|
378
|
+
if (state.get(next) === 'open') return this.tables.get(next);
|
|
379
|
+
if (state.has(next)) continue;
|
|
380
|
+
state.set(next, 'open');
|
|
381
|
+
const entry = this.tables.get(next);
|
|
382
|
+
stack.push([next, entry.reliance ? [...entry.reliance.tables] : [], 0]);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
return null;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Whether a table's answers keep the order a search finds them in: none of
|
|
389
|
+
// them, through any table it rests on, came from a listing.
|
|
390
|
+
exact(callKey) {
|
|
391
|
+
const memo = this.exactness ??= new Map();
|
|
392
|
+
if (memo.has(callKey)) return memo.get(callKey);
|
|
393
|
+
const order = [];
|
|
394
|
+
const pending = [callKey];
|
|
395
|
+
const seen = new Set();
|
|
396
|
+
while (pending.length) {
|
|
397
|
+
const item = pending.pop();
|
|
398
|
+
if (seen.has(item) || memo.has(item)) continue;
|
|
399
|
+
seen.add(item);
|
|
400
|
+
order.push(item);
|
|
401
|
+
pending.push(...this.tables.get(item).reliance.tables);
|
|
402
|
+
}
|
|
403
|
+
for (let i = order.length - 1; i >= 0; i--) {
|
|
404
|
+
const reliance = this.tables.get(order[i]).reliance;
|
|
405
|
+
memo.set(order[i], !reliance.listings.size && [...reliance.tables].every((item) => memo.get(item) !== false));
|
|
406
|
+
}
|
|
407
|
+
return memo.get(callKey);
|
|
408
|
+
}
|
|
409
|
+
}
|
package/src/engine.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Backward resolution, forward fixpoints and proof recording.
|
|
2
2
|
import { PRIMITIVE_KEYS, primitive } from './builtins.js';
|
|
3
|
+
import { Reliance, Settler, Unsettled } from './closure.js';
|
|
3
4
|
import { freshClause, matchHead, renaming, text, variant } from './common.js';
|
|
4
5
|
import { goalTerm, subgoals } from './goals.js';
|
|
5
6
|
import { INSPECTION_KEYS } from './inspection.js';
|
|
@@ -918,6 +919,123 @@ export function skolemTerms(clause, head, unresolved, env, context = null) {
|
|
|
918
919
|
return [...unresolved.keys()].map((name) => new Struct('skolem', [BigInt(clause.id), named.get(name), argumentsList]));
|
|
919
920
|
}
|
|
920
921
|
|
|
922
|
+
// The search certificate of a run's negations and collections. Each call
|
|
923
|
+
// their searches make is answered by expanding it one level, every inner call
|
|
924
|
+
// by its own table, so the tables cost what the searches did; the checker
|
|
925
|
+
// then checks each table again, and each boundary against what the run found.
|
|
926
|
+
// A boundary the certificate cannot settle stays an obligation; one the run
|
|
927
|
+
// and its certificate disagree on is an error, since a proof must not hide it.
|
|
928
|
+
function certify(solver, boundaries) {
|
|
929
|
+
const program = solver.program;
|
|
930
|
+
const computed = new Map();
|
|
931
|
+
const met = new Map();
|
|
932
|
+
const open = new Set();
|
|
933
|
+
const listed = new Map();
|
|
934
|
+
let budget = 0;
|
|
935
|
+
let depth = 0;
|
|
936
|
+
const settler = new Settler(program, {
|
|
937
|
+
answers(call) {
|
|
938
|
+
const callKey = variant(call);
|
|
939
|
+
if (computed.has(callKey)) return computed.get(callKey);
|
|
940
|
+
if (!met.has(callKey)) met.set(callKey, met.size);
|
|
941
|
+
// A search that met its own call again would not have finished, and
|
|
942
|
+
// one that keeps meeting new calls is cut off.
|
|
943
|
+
if (open.has(callKey) || ++budget > solver.maxInferences) throw new Unsettled();
|
|
944
|
+
// Deep searches are taken up again from an explicit stack.
|
|
945
|
+
if (depth >= CERTIFY_DEPTH) throw new Missing(call);
|
|
946
|
+
depth++;
|
|
947
|
+
open.add(callKey);
|
|
948
|
+
try {
|
|
949
|
+
const term = freshTerm(call, `certificate${++settler.serial}`);
|
|
950
|
+
const env = new Env();
|
|
951
|
+
const answers = [];
|
|
952
|
+
for (const _ of settler.expand(term, env, new Reliance())) { // eslint-disable-line no-unused-vars
|
|
953
|
+
if (++budget > solver.maxInferences) throw new Unsettled();
|
|
954
|
+
// Each answer is written with variables of its own.
|
|
955
|
+
answers.push(freshTerm(copyResolved(term, env), `answer${++settler.serial}`));
|
|
956
|
+
}
|
|
957
|
+
computed.set(callKey, answers);
|
|
958
|
+
return answers;
|
|
959
|
+
} catch (error) {
|
|
960
|
+
// A full table can search past where the run stopped, as under once/1,
|
|
961
|
+
// and meet an error the run never did: such a boundary stays trusted.
|
|
962
|
+
if (error instanceof EyemindError) throw new Unsettled();
|
|
963
|
+
throw error;
|
|
964
|
+
} finally {
|
|
965
|
+
depth--;
|
|
966
|
+
open.delete(callKey);
|
|
967
|
+
}
|
|
968
|
+
},
|
|
969
|
+
facts(goalKey) {
|
|
970
|
+
const nodes = solver.facts.get(goalKey) || NO_NODES;
|
|
971
|
+
if (nodes.some((node) => node.generic)) throw new Unsettled();
|
|
972
|
+
listed.set(goalKey, nodes);
|
|
973
|
+
return nodes.map((node) => node.goal);
|
|
974
|
+
},
|
|
975
|
+
supported: () => true,
|
|
976
|
+
});
|
|
977
|
+
// Answer a call too deep to reach directly, and the deeper calls it needs.
|
|
978
|
+
function ensure(start) {
|
|
979
|
+
const pending = [start];
|
|
980
|
+
const waiting = new Set([variant(start)]);
|
|
981
|
+
while (pending.length) {
|
|
982
|
+
const call = pending[pending.length - 1];
|
|
983
|
+
try {
|
|
984
|
+
settler.source.answers(call);
|
|
985
|
+
waiting.delete(variant(pending.pop()));
|
|
986
|
+
} catch (error) {
|
|
987
|
+
if (!(error instanceof Missing)) throw error;
|
|
988
|
+
const deeper = variant(error.call);
|
|
989
|
+
if (waiting.has(deeper)) throw new Unsettled();
|
|
990
|
+
waiting.add(deeper);
|
|
991
|
+
pending.push(error.call);
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
const tables = new Set();
|
|
996
|
+
const listings = new Set();
|
|
997
|
+
for (const boundary of boundaries) {
|
|
998
|
+
let outcome = null;
|
|
999
|
+
budget = 0;
|
|
1000
|
+
while (outcome === null) {
|
|
1001
|
+
try {
|
|
1002
|
+
outcome = settler.settle(boundary);
|
|
1003
|
+
} catch (error) {
|
|
1004
|
+
if (!(error instanceof Missing)) throw error;
|
|
1005
|
+
try {
|
|
1006
|
+
ensure(error.call);
|
|
1007
|
+
} catch (failure) {
|
|
1008
|
+
if (!(failure instanceof Unsettled)) throw failure;
|
|
1009
|
+
outcome = { status: 'unsettled' };
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
if (outcome.status === 'settled') {
|
|
1014
|
+
for (const item of outcome.tables) tables.add(item);
|
|
1015
|
+
for (const item of outcome.listings) listings.add(item);
|
|
1016
|
+
} else if (outcome.status === 'failed') {
|
|
1017
|
+
throw new EyemindError(`cannot certify this result: ${outcome.failures[0]?.[0] ?? 'its search certificate fails'}`);
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
return {
|
|
1021
|
+
// Tables in the order their calls were first met.
|
|
1022
|
+
tables: [...settler.tables].filter(([callKey]) => tables.has(callKey))
|
|
1023
|
+
.sort(([a], [b]) => met.get(a) - met.get(b)).map(([, entry]) => [entry.call, entry.answers]),
|
|
1024
|
+
listings: [...settler.listings.keys()].filter((goalKey) => listings.has(goalKey)).map((goalKey) => [goalKey, listed.get(goalKey)]),
|
|
1025
|
+
};
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
// How many calls deep a certificate's tables are made by direct recursion.
|
|
1029
|
+
const CERTIFY_DEPTH = 100;
|
|
1030
|
+
|
|
1031
|
+
// A call whose table is not made yet, too deep to make it directly.
|
|
1032
|
+
class Missing extends Error {
|
|
1033
|
+
constructor(call) {
|
|
1034
|
+
super('missing table');
|
|
1035
|
+
this.call = call;
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
|
|
921
1039
|
// What a run concluded. answers are the conclusions as printable text and
|
|
922
1040
|
// bindings one object per answer of the goal's variables; with a proof, proof
|
|
923
1041
|
// is the document and proofReport its check, clausesUsed the ids of the
|
|
@@ -1023,7 +1141,8 @@ function reason(program, goalInputs, options) {
|
|
|
1023
1141
|
for (const [name, value] of answer) out[name] = write(value, null, lettering);
|
|
1024
1142
|
return out;
|
|
1025
1143
|
});
|
|
1026
|
-
const rendered = options.proof
|
|
1144
|
+
const rendered = options.proof
|
|
1145
|
+
? renderProof(program, claims, roots, true, heading, (boundaries) => certify(solver, boundaries)) : null;
|
|
1027
1146
|
const document = rendered !== null ? rendered[0] : null;
|
|
1028
1147
|
// A rule that reports a claim is used by it, though a proof records only
|
|
1029
1148
|
// the claim's support.
|
package/src/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// Facts and rules are JavaScript. Forward rules materialize conclusions until
|
|
4
4
|
// a fixpoint, backward rules are decided when a goal asks for them, and every
|
|
5
5
|
// answer can come with a proof that a separate checker verifies against the
|
|
6
|
-
// program, condition by condition (C1-
|
|
6
|
+
// program, condition by condition (C1-C9).
|
|
7
7
|
export { VERSION } from './version.js';
|
|
8
8
|
export { Result, run, unusedClauses } from './engine.js';
|
|
9
9
|
export { readGoal } from './goals.js';
|
package/src/program.js
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
import { write } from './writer.js';
|
|
11
11
|
|
|
12
12
|
export const CONTROL_KEYS = new Set(['and/2', 'or/2', 'not/1', 'call/1', 'once/1', 'findall/3']);
|
|
13
|
-
export const RESERVED_KEYS = new Set(['step/4', 'clause/2', 'activated/2', 'implies/2', 'implied_by/2', 'forward/2', 'backward/2']);
|
|
13
|
+
export const RESERVED_KEYS = new Set(['step/4', 'clause/2', 'searched/2', 'derived/2', 'activated/2', 'implies/2', 'implied_by/2', 'forward/2', 'backward/2']);
|
|
14
14
|
|
|
15
15
|
// The statements that state a forward rule, and how an error names them.
|
|
16
16
|
const FORWARD_KINDS = { forward: 'a forward rule', query: 'query()', contradiction: 'contradiction()' };
|