eyemind 0.8.11 → 0.8.12
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/LS-SPEC.md +1 -1
- package/README.md +19 -2
- package/package.json +1 -1
- package/src/formats.js +26 -7
- package/src/rdf-logic.js +19 -8
- package/src/specialize.js +145 -35
- package/src/version.js +1 -1
package/LS-SPEC.md
CHANGED
package/README.md
CHANGED
|
@@ -314,11 +314,18 @@ and the whole program runs instead when new triples could have any property,
|
|
|
314
314
|
as `owl:sameAs` between individuals makes, or when a negation could retract
|
|
315
315
|
a background conclusion. `--program` prints the specialized program of each
|
|
316
316
|
new shape, and `--stats` says whether each message used one. Streamed
|
|
317
|
-
messages have no proofs.
|
|
317
|
+
messages have no proofs.
|
|
318
|
+
|
|
319
|
+
An [output contract](#rdf-logic-profiles) prunes the specialized rules to
|
|
320
|
+
those that can contribute to a triple it asks for, following premises back
|
|
321
|
+
to the rules concluding them. A value counts only where a reader uses it:
|
|
322
|
+
a triple that `owl:sameAs` copies to an equal object serves no rule that
|
|
323
|
+
reads only its subject. See the
|
|
318
324
|
[observations](https://eyereasoner.github.io/eyemind/examples/deck/observations-owl2rl) deck.
|
|
319
325
|
|
|
320
326
|
```sh
|
|
321
|
-
eyemind --owl2rl --stream-messages ontology.ttl messages.trig
|
|
327
|
+
eyemind --owl2rl --stream-messages ontology.ttl messages.trig # each message's new triples
|
|
328
|
+
eyemind --owl2rl --stream-messages ontology.ttl contract.js messages.trig # only those the contract asks for
|
|
322
329
|
```
|
|
323
330
|
|
|
324
331
|
### SPARQL 1.2 RL
|
|
@@ -343,6 +350,16 @@ Four switches add standalone LS rules to RDF inputs; vocabulary alone never
|
|
|
343
350
|
enables one. They combine, inference closes before validation, and the output
|
|
344
351
|
is Turtle with the new triples and reports.
|
|
345
352
|
|
|
353
|
+
A `.js` input is an output contract: a Logic Script program whose statements
|
|
354
|
+
are `query(rdf(S, P, O))` or `query(rdf(S, P, O, G))`. The output then holds
|
|
355
|
+
only the new triples matching one of them, and a proof answers those queries.
|
|
356
|
+
|
|
357
|
+
```js
|
|
358
|
+
const sosa = (name) => iri('http://www.w3.org/ns/sosa/' + name);
|
|
359
|
+
query(rdf(O, sosa('madeBySensor'), S));
|
|
360
|
+
query(rdf(O, iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), sosa('Observation')));
|
|
361
|
+
```
|
|
362
|
+
|
|
346
363
|
- `--owl2rl` adds the RDFS and [OWL 2 RL](https://www.w3.org/TR/owl2-profiles/#Reasoning_in_OWL_2_RL_and_RDF_Graphs)
|
|
347
364
|
rules, including the datatype map of its table 8: value equality and
|
|
348
365
|
difference, membership across lexical forms, and ill-typed literals for the
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eyemind",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.12",
|
|
4
4
|
"description": "A dependency-free Logic Script (LS) reasoner with forward and backward reasoning, checkable proofs, and RDF 1.2, Notation3 (N3), SPARQL 1.2 RL (SRL) and Logical English (LE) interoperability.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
package/src/formats.js
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
// The compiled program is ordinary LS: proofs, checking and --unused
|
|
20
20
|
// apply to it. SRL execution functions capture a context that a saved proof
|
|
21
21
|
// retains; compiling with that context reproduces the same program later.
|
|
22
|
-
import { EyemindError, Struct } from './terms.js';
|
|
22
|
+
import { EyemindError, Struct, Var } from './terms.js';
|
|
23
23
|
import { loadSources } from './lang.js';
|
|
24
24
|
import { readTerms } from './reader.js';
|
|
25
25
|
import { DEFAULT_GRAPH, parseRdf, rdfDocument, rdfFormat, writeRdf } from './rdf.js';
|
|
@@ -63,19 +63,38 @@ export function loadInputs(inputs, io = null, options = {}) {
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
// The RDF inputs of the logic profiles as one graph
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
66
|
+
// The RDF inputs of the logic profiles as one graph, and the output contract
|
|
67
|
+
// a Logic Script input states, if any.
|
|
68
|
+
function profileGraph(inputs, kinds, rdf, baseOf, switches, io) {
|
|
69
|
+
if (inputs.some(([, name], k) => kinds[k] !== 'rdf' && kinds[k] !== 'js' && name !== '<stdin>')) {
|
|
70
|
+
throw new EyemindError(`${switches} takes RDF inputs, and a Logic Script output contract; standard input is Turtle`);
|
|
70
71
|
}
|
|
72
|
+
const contracts = inputs.filter(([, name], k) => kinds[k] === 'js' && name !== '<stdin>');
|
|
73
|
+
const wanted = contracts.length ? contracts.flatMap(([text, name]) => outputContract(text, name, io)) : null;
|
|
74
|
+
if (contracts.length && !wanted.length) throw new EyemindError(`${switches} needs a query(rdf(S, P, O)) in its output contract`);
|
|
71
75
|
const prefixes = {}, quads = [];
|
|
72
76
|
inputs.forEach(([text, name], k) => {
|
|
77
|
+
if (kinds[k] === 'js' && name !== '<stdin>') return;
|
|
73
78
|
const parsed = name === '<stdin>' ? parseRdf(text, { format: 'turtle', base: baseOf(name), scope: scopeOf(inputs, name, k) }) : rdf(text, name, k);
|
|
74
79
|
if (parsed.triples) throw new EyemindError(`${switches} takes RDF graphs, not RDF Message Logs`);
|
|
75
80
|
Object.assign(prefixes, parsed.prefixes);
|
|
76
81
|
quads.push(...parsed.quads);
|
|
77
82
|
});
|
|
78
|
-
return { quads, prefixes };
|
|
83
|
+
return { quads, prefixes, wanted };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// The triple patterns an output contract asks for: a Logic Script program
|
|
87
|
+
// whose statements are query(rdf(S, P, O)) or query(rdf(S, P, O, G)).
|
|
88
|
+
function outputContract(text, name, io) {
|
|
89
|
+
const program = loadSources([[text, name]], io);
|
|
90
|
+
return program.clauses.map((clause) => {
|
|
91
|
+
const goal = clause.forward && clause.heads.length === 1 && clause.heads[0] === 'true' && clause.statedBody.length === 1
|
|
92
|
+
? clause.statedBody[0] : null;
|
|
93
|
+
if (!(goal instanceof Struct) || goal.name !== 'rdf' || ![3, 4].includes(goal.args.length)) {
|
|
94
|
+
throw new EyemindError(`${name}: an output contract states only query(rdf(S, P, O)) or query(rdf(S, P, O, G))${clause.line ? `, not line ${clause.line}` : ''}`);
|
|
95
|
+
}
|
|
96
|
+
return goal.args.length === 4 ? goal : new Struct('rdf', [...goal.args, new Var(`G#contract${clause.id}`)]);
|
|
97
|
+
});
|
|
79
98
|
}
|
|
80
99
|
|
|
81
100
|
// The reasoner the logic profiles run each RDF Message with, given the
|
|
@@ -108,7 +127,7 @@ function load(inputs, io, { base = null, messageInputs = null, srlContext = null
|
|
|
108
127
|
}
|
|
109
128
|
if (profiles.length && odrl) throw new EyemindError(`--odrl cannot be combined with ${switches}`);
|
|
110
129
|
if (profiles.length) {
|
|
111
|
-
const graph = profileGraph(inputs, kinds, rdf, baseOf, switches);
|
|
130
|
+
const graph = profileGraph(inputs, kinds, rdf, baseOf, switches, io);
|
|
112
131
|
const source = inputs.map(([, n]) => n).join(', ');
|
|
113
132
|
if (messages) return messageReasoner(graph, { profiles, source });
|
|
114
133
|
return compileRdfLogic(graph, { profiles, source, context: srlContext });
|
package/src/rdf-logic.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// feed back into it; LS stratification closes inference before validation.
|
|
3
3
|
import { loadText } from './lang.js';
|
|
4
4
|
import { rdfDocument } from './rdf.js';
|
|
5
|
-
import { write } from './writer.js';
|
|
5
|
+
import { Lettering, write } from './writer.js';
|
|
6
6
|
import { owl2rlRules } from './owl2rl.js';
|
|
7
7
|
import { skosRules } from './skos.js';
|
|
8
8
|
import { qudtRules } from './qudt.js';
|
|
@@ -12,12 +12,13 @@ import { executionContext } from './srl.js';
|
|
|
12
12
|
import { rdfValueRules } from './rdf-values.js';
|
|
13
13
|
import { run } from './engine.js';
|
|
14
14
|
import { Specializer } from './specialize.js';
|
|
15
|
-
import { Struct, identity } from './terms.js';
|
|
15
|
+
import { Env, Struct, freshTerm, identity, unify } from './terms.js';
|
|
16
16
|
|
|
17
17
|
export const ENTAILMENT = 'https://eyereasoner.github.io/eyemind/entailment#';
|
|
18
18
|
export const RDF_PROFILES = ['owl2rl', 'qudt', 'skos', 'shacl'];
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
// wanted: rdf/4 patterns of the output contract, or null for every new triple.
|
|
21
|
+
export function compileRdfLogic({ quads, prefixes, shapes = quads, wanted = null }, { profiles, source = '<input>', disallows, evaluate, context, stream = false }) {
|
|
21
22
|
quads = [...new Map(quads.map((q) => [q.map((t) => write(t)).join(' '), q])).values()];
|
|
22
23
|
const selected = new Set(profiles), needs = new Set();
|
|
23
24
|
// SHACL node expressions may call SPARQL functions beyond the value rules.
|
|
@@ -35,7 +36,7 @@ export function compileRdfLogic({ quads, prefixes, shapes = quads }, { profiles,
|
|
|
35
36
|
(selected.has('owl2rl') ? owl2rlRules : '') +
|
|
36
37
|
(selected.has('skos') ? skosRules : '') +
|
|
37
38
|
(selected.has('qudt') ? qudtRules : '') +
|
|
38
|
-
shacl + (evaluate ? "query(sh_evaluation(R));\n" : REPORTS + (stream ? '' : RESULT));
|
|
39
|
+
shacl + (evaluate ? "query(sh_evaluation(R));\n" : REPORTS + (stream ? '' : wanted === null ? RESULT : contracted(wanted)));
|
|
39
40
|
return { kind: profiles[0], text, program: loadText(text, source),
|
|
40
41
|
prefixes: { ...prefixes, ent: ENTAILMENT, ...(selected.has('skos') ? { skos: 'http://www.w3.org/2004/02/skos/core#' } : {}),
|
|
41
42
|
...(selected.has('qudt') ? { qconv: 'https://eyereasoner.github.io/eyemind/qudt#', qudt: 'http://qudt.org/schema/qudt/', unit: 'http://qudt.org/vocab/unit/' } : {}),
|
|
@@ -47,10 +48,16 @@ export function compileRdfLogic({ quads, prefixes, shapes = quads }, { profiles,
|
|
|
47
48
|
// alone concludes. The background is closed once and the rules are
|
|
48
49
|
// specialized to the kinds of triples a message has (specialize.js); a
|
|
49
50
|
// program that cannot be specialized runs whole for every message instead.
|
|
50
|
-
|
|
51
|
+
// With an output contract, only the rules that can contribute to its
|
|
52
|
+
// patterns are specialized, and only triples matching one are written.
|
|
53
|
+
export function messageReasoner({ quads, prefixes, wanted = null }, { profiles, source = '<input>' }) {
|
|
51
54
|
const options = { profiles, source };
|
|
52
55
|
const background = compileRdfLogic({ quads, prefixes }, { ...options, stream: true });
|
|
53
|
-
const specializer = new Specializer(background.program, { quads: ['rdf/4', 'ent_base/4']
|
|
56
|
+
const specializer = new Specializer(background.program, { quads: ['rdf/4', 'ent_base/4'],
|
|
57
|
+
wanted: wanted && wanted.flatMap((pattern) => [pattern, new Struct('ent_report', pattern.args)]) });
|
|
58
|
+
let serial = 0;
|
|
59
|
+
const kept = (quad) => wanted === null ||
|
|
60
|
+
wanted.some((pattern) => unify(new Struct('rdf', quad), freshTerm(pattern, `w${++serial}`), new Env()));
|
|
54
61
|
let alone = null;
|
|
55
62
|
const answers = (program, limits) => run(program, limits).conclusions.filter((t) => t instanceof Struct && t.name === 'result_rdf');
|
|
56
63
|
return {
|
|
@@ -67,13 +74,13 @@ export function messageReasoner({ quads, prefixes }, { profiles, source = '<inpu
|
|
|
67
74
|
const result = run(special.program, limits);
|
|
68
75
|
const added = result.derived.map((node) => node.goal).filter((t) => !special.old.has(identity(t)) &&
|
|
69
76
|
(t.name === 'ent_report' || (t.name === 'rdf' && RESOURCES.has(t.args[0].name) && t.args[1].name === 'iri')));
|
|
70
|
-
return { quads: added.map((t) => t.args), specialized: true, stats: result.stats, haltCode: result.haltCode,
|
|
77
|
+
return { quads: added.map((t) => t.args).filter(kept), specialized: true, stats: result.stats, haltCode: result.haltCode,
|
|
71
78
|
sources: special.fresh ? special.sources : null };
|
|
72
79
|
}
|
|
73
80
|
alone ??= new Set(answers(compileRdfLogic({ quads, prefixes }, options).program, limits).map(identity));
|
|
74
81
|
const result = run(compileRdfLogic({ quads: [...quads, ...message], prefixes, shapes: quads }, options).program, limits);
|
|
75
82
|
const added = result.conclusions.filter((t) => t instanceof Struct && t.name === 'result_rdf' && !alone.has(identity(t)));
|
|
76
|
-
return { quads: added.map((t) => t.args), specialized: false, stats: result.stats, haltCode: result.haltCode, sources: null };
|
|
83
|
+
return { quads: added.map((t) => t.args).filter(kept), specialized: false, stats: result.stats, haltCode: result.haltCode, sources: null };
|
|
77
84
|
},
|
|
78
85
|
};
|
|
79
86
|
}
|
|
@@ -105,6 +112,10 @@ implies(ent_conflict(R, X, P, Y, G), [
|
|
|
105
112
|
]);
|
|
106
113
|
`;
|
|
107
114
|
|
|
115
|
+
// The result restricted to an output contract: one query per pattern.
|
|
116
|
+
const contracted = (wanted) => RESULT.replace(/^query\(.*\n/m, '') +
|
|
117
|
+
wanted.map((pattern) => `${write(new Struct('query', [new Struct('result_rdf', pattern.args)]), null, new Lettering())};\n`).join('');
|
|
118
|
+
|
|
108
119
|
const RESULT = String.raw`implies([rdf(S, iri(P), O, G), ent_resource(S), not(ent_base(S, iri(P), O, G))], result_rdf(S, iri(P), O, G));
|
|
109
120
|
implies(ent_report(S, P, O, G), result_rdf(S, P, O, G));
|
|
110
121
|
query(result_rdf(S, P, O, G));
|
package/src/specialize.js
CHANGED
|
@@ -129,8 +129,11 @@ export function programText(sources) {
|
|
|
129
129
|
|
|
130
130
|
export class Specializer {
|
|
131
131
|
// quads: the keys whose arguments are subject, property, object and graph.
|
|
132
|
-
|
|
132
|
+
// wanted: patterns of the new facts asked for, or null for all; rules that
|
|
133
|
+
// cannot contribute to one are left out.
|
|
134
|
+
constructor(program, { quads = [], wanted = null } = {}) {
|
|
133
135
|
this.quads = new Set(quads);
|
|
136
|
+
this.wanted = wanted;
|
|
134
137
|
this.cache = new Map();
|
|
135
138
|
this.reason = null;
|
|
136
139
|
try {
|
|
@@ -293,45 +296,45 @@ export class Specializer {
|
|
|
293
296
|
}
|
|
294
297
|
if (changed) this.readsMemo = new Map();
|
|
295
298
|
}
|
|
299
|
+
const variants = [];
|
|
300
|
+
for (const rule of this.rules) this.variants(rule, (heads, body, newIndex, free) => variants.push([heads, body, newIndex, free]));
|
|
296
301
|
const sources = [];
|
|
297
302
|
const needed = new Map();
|
|
298
303
|
const need = (goal) => {
|
|
299
304
|
for (const fact of this.matches(goal, new Env())) needed.set(identity(fact), fact);
|
|
300
305
|
};
|
|
301
|
-
for (const
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
return old(goal);
|
|
307
|
-
}
|
|
308
|
-
return mode === 'new' ? goal : this.rewrite(goal, false, need);
|
|
309
|
-
});
|
|
310
|
-
// A new premise first: it has the fewest facts to try.
|
|
311
|
-
const ordered = newIndex < 0 ? goals : [goals[newIndex], ...goals.filter((_, i) => i !== newIndex)];
|
|
312
|
-
const inBody = new Set();
|
|
313
|
-
for (const goal of ordered) for (const name of variables(goal).keys()) inBody.add(name);
|
|
314
|
-
if (heads.some((head) => [...variables(head).keys()].some((name) => !inBody.has(name)))) {
|
|
315
|
-
// Witnesses are shared by the conclusions of one firing.
|
|
316
|
-
sources.push(new Source('forward', conjunction(heads), ordered));
|
|
317
|
-
return;
|
|
318
|
-
}
|
|
319
|
-
for (const head of heads) {
|
|
320
|
-
if (head === 'false') {
|
|
321
|
-
sources.push(new Source('contradiction', 'false', ordered));
|
|
322
|
-
continue;
|
|
323
|
-
}
|
|
324
|
-
// An old conclusion is not new: say so when the old facts it
|
|
325
|
-
// could be are few enough to copy.
|
|
326
|
-
const could = this.matches(head, new Env(), MANY);
|
|
327
|
-
if (could !== null && could.length) {
|
|
328
|
-
need(head);
|
|
329
|
-
sources.push(new Source('forward', head, [...ordered, new Struct('not', [old(head)])]));
|
|
330
|
-
} else {
|
|
331
|
-
sources.push(new Source('forward', head, ordered));
|
|
332
|
-
}
|
|
306
|
+
for (const [heads, body, newIndex] of this.relevant(variants)) {
|
|
307
|
+
const goals = body.map(([goal, mode]) => {
|
|
308
|
+
if (mode === 'old') {
|
|
309
|
+
need(goal);
|
|
310
|
+
return old(goal);
|
|
333
311
|
}
|
|
312
|
+
return mode === 'new' ? goal : this.rewrite(goal, false, need);
|
|
334
313
|
});
|
|
314
|
+
// A new premise first: it has the fewest facts to try.
|
|
315
|
+
const ordered = newIndex < 0 ? goals : [goals[newIndex], ...goals.filter((_, i) => i !== newIndex)];
|
|
316
|
+
const inBody = new Set();
|
|
317
|
+
for (const goal of ordered) for (const name of variables(goal).keys()) inBody.add(name);
|
|
318
|
+
if (heads.some((head) => [...variables(head).keys()].some((name) => !inBody.has(name)))) {
|
|
319
|
+
// Witnesses are shared by the conclusions of one firing.
|
|
320
|
+
sources.push(new Source('forward', conjunction(heads), ordered));
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
for (const head of heads) {
|
|
324
|
+
if (head === 'false') {
|
|
325
|
+
sources.push(new Source('contradiction', 'false', ordered));
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
// An old conclusion is not new: say so when the old facts it
|
|
329
|
+
// could be are few enough to copy.
|
|
330
|
+
const could = this.matches(head, new Env(), MANY);
|
|
331
|
+
if (could !== null && could.length) {
|
|
332
|
+
need(head);
|
|
333
|
+
sources.push(new Source('forward', head, [...ordered, new Struct('not', [old(head)])]));
|
|
334
|
+
} else {
|
|
335
|
+
sources.push(new Source('forward', head, ordered));
|
|
336
|
+
}
|
|
337
|
+
}
|
|
335
338
|
}
|
|
336
339
|
for (const clause of this.backward) {
|
|
337
340
|
sources.push(clause.body.length
|
|
@@ -342,6 +345,113 @@ export class Specializer {
|
|
|
342
345
|
return { sources };
|
|
343
346
|
}
|
|
344
347
|
|
|
348
|
+
// The variants that can contribute to a wanted fact: one with a wanted
|
|
349
|
+
// conclusion, or a contradiction, and then those concluding what its new
|
|
350
|
+
// premises and the goals it calls read, to a fixpoint. Each wanted fact
|
|
351
|
+
// says which of its positions matter to its reader: a value matters when
|
|
352
|
+
// it reaches a position some reader uses, or joins goals. A conclusion
|
|
353
|
+
// does not serve a reader when the variant reading it would conclude only
|
|
354
|
+
// its own premises, as reflexive owl:sameAs does in replacement rules, or
|
|
355
|
+
// when one of its own premises already agrees with it wherever the reader
|
|
356
|
+
// looks, as a triple owl:sameAs copies to an equal object has the subject
|
|
357
|
+
// its original has. All variants without wanted patterns.
|
|
358
|
+
relevant(variants) {
|
|
359
|
+
if (this.wanted === null) return variants;
|
|
360
|
+
const wanted = this.wanted.map((pattern) => ({ goal: renamed(pattern), heads: null, premises: null, positions: pattern.args.map(() => true) }));
|
|
361
|
+
const want = (goal, heads, premises, positions, free = null) => {
|
|
362
|
+
const names = new Map();
|
|
363
|
+
const suffix = `r${++fresh}`;
|
|
364
|
+
const copy = freshTerm(goal, suffix, names);
|
|
365
|
+
// The premise open where the rule has a variable whose value does
|
|
366
|
+
// not matter: any value there serves alike.
|
|
367
|
+
const open = free === null ? copy : new Struct(copy.name, copy.args.map((arg, i) => free[i] && !positions[i] ? variable() : arg));
|
|
368
|
+
wanted.push({ goal: copy, open, heads: heads && heads.map((head) => freshTerm(head, suffix, names)),
|
|
369
|
+
premises: premises && premises.map((premise) => freshTerm(premise, suffix, names)), positions });
|
|
370
|
+
};
|
|
371
|
+
const env = new Env();
|
|
372
|
+
const serves = (head, own, { goal, open, heads, premises, positions }) => {
|
|
373
|
+
const mark = env.mark();
|
|
374
|
+
try {
|
|
375
|
+
if (!unify(head, goal, env)) return false;
|
|
376
|
+
if (heads === null) return true;
|
|
377
|
+
// Every firing whose conclusion is read here has such a premise when
|
|
378
|
+
// the premise, as the firing binds it, is an instance of the read one.
|
|
379
|
+
if (own.some((premise) => {
|
|
380
|
+
const bound = copyResolved(premise, env);
|
|
381
|
+
return bound instanceof Struct && instance(open, bound) &&
|
|
382
|
+
positions.every((matters, i) => !matters || identical(bound.args[i], head.args[i], env));
|
|
383
|
+
})) return false;
|
|
384
|
+
return heads.some((item) => item === 'false' || !premises.some((premise) => identical(premise, item, env)));
|
|
385
|
+
} finally {
|
|
386
|
+
env.undo(mark);
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
// For each kept variant, the positions of each conclusion some reader uses.
|
|
390
|
+
const usage = new Map();
|
|
391
|
+
const checked = new Map();
|
|
392
|
+
for (let changed = true; changed;) {
|
|
393
|
+
changed = false;
|
|
394
|
+
for (const variant of variants) {
|
|
395
|
+
const [heads, body, , free] = variant;
|
|
396
|
+
const own = body.filter(([, mode]) => mode === 'new' || mode === 'old').map(([goal]) => goal);
|
|
397
|
+
let use = usage.get(variant);
|
|
398
|
+
let grew = false;
|
|
399
|
+
const from = checked.get(variant) ?? 0;
|
|
400
|
+
checked.set(variant, wanted.length);
|
|
401
|
+
heads.forEach((head, h) => {
|
|
402
|
+
if (head === 'false') {
|
|
403
|
+
if (use === undefined) usage.set(variant, use = heads.map((item) => item instanceof Struct ? item.args.map(() => false) : []));
|
|
404
|
+
if (from === 0) grew = true;
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
for (let w = from; w < wanted.length; w++) {
|
|
408
|
+
if (!serves(head, own, wanted[w])) continue;
|
|
409
|
+
if (use === undefined) usage.set(variant, use = heads.map((item) => item instanceof Struct ? item.args.map(() => false) : []));
|
|
410
|
+
wanted[w].positions.forEach((matters, i) => {
|
|
411
|
+
if (matters && !use[h][i]) use[h][i] = grew = true;
|
|
412
|
+
});
|
|
413
|
+
if (!use[h].length) grew = true;
|
|
414
|
+
}
|
|
415
|
+
});
|
|
416
|
+
if (!grew) continue;
|
|
417
|
+
changed = true;
|
|
418
|
+
// The values that matter: those at used positions of the
|
|
419
|
+
// conclusions, and those shared with another goal of the body.
|
|
420
|
+
const values = (except) => {
|
|
421
|
+
const names = new Set();
|
|
422
|
+
heads.forEach((head, h) => {
|
|
423
|
+
if (head instanceof Struct) head.args.forEach((arg, i) => { if (use[h][i]) for (const name of variables(arg).keys()) names.add(name); });
|
|
424
|
+
});
|
|
425
|
+
for (const [goal] of body) if (goal !== except) for (const name of variables(goal).keys()) names.add(name);
|
|
426
|
+
return names;
|
|
427
|
+
};
|
|
428
|
+
for (const [at, [goal, mode]] of body.entries()) {
|
|
429
|
+
if (mode === 'new') {
|
|
430
|
+
const names = values(goal);
|
|
431
|
+
const counts = new Map();
|
|
432
|
+
for (const arg of goal.args) for (const name of variables(arg).keys()) counts.set(name, (counts.get(name) ?? 0) + 1);
|
|
433
|
+
want(goal, heads, own, goal.args.map((arg) => [...variables(arg).keys()].some((name) => names.has(name) || counts.get(name) > 1)), free[at]);
|
|
434
|
+
} else if (mode === null) {
|
|
435
|
+
for (const read of this.reads(goal)) want(read, null, null, read.args.map(() => true));
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
return variants.filter((variant) => usage.has(variant));
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// The data goals that a goal which is no premise reads and that can be
|
|
444
|
+
// new: inside control goals, and in the backward clauses it calls.
|
|
445
|
+
reads(goal, seen = new Set()) {
|
|
446
|
+
if (!(goal instanceof Struct)) return [];
|
|
447
|
+
const goalKey = key(goal);
|
|
448
|
+
if (this.data.has(goalKey)) return this.mayBeNew(goal) ? [goal] : [];
|
|
449
|
+
if (CONTROL_KEYS.has(goalKey)) return goal.args.flatMap((arg) => this.reads(arg, seen));
|
|
450
|
+
if (!this.backwardKeys.has(goalKey) || seen.has(goalKey)) return [];
|
|
451
|
+
seen.add(goalKey);
|
|
452
|
+
return this.clausesOf.get(goalKey).flatMap((clause) => clause.body.flatMap((item) => this.reads(item, seen)));
|
|
453
|
+
}
|
|
454
|
+
|
|
345
455
|
// Whether a goal that is no premise may hold: false when no clause can
|
|
346
456
|
// answer it within depth steps, or a type test fails. A goal only one fact
|
|
347
457
|
// can answer is bound to it in env, so the goals after it see its values.
|
|
@@ -456,9 +566,9 @@ export class Specializer {
|
|
|
456
566
|
resolved.forEach((goal, i) => {
|
|
457
567
|
if (modes[i] === 'fact') return;
|
|
458
568
|
if (modes[i] === 'new' && newIndex < 0) newIndex = list.length;
|
|
459
|
-
list.push([goal, modes[i]]);
|
|
569
|
+
list.push([goal, modes[i], body[i]]);
|
|
460
570
|
});
|
|
461
|
-
emit(kept, list, newIndex);
|
|
571
|
+
emit(kept, list, newIndex, list.map(([, , original]) => original instanceof Struct ? original.args.map((arg) => arg instanceof Var) : []));
|
|
462
572
|
};
|
|
463
573
|
// The variables in the property, or rdf:type class, of the conclusions
|
|
464
574
|
// and the other premises: unfolding an old premise that binds one makes
|
package/src/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// The release this checkout is; ./release keeps it in step with package.json.
|
|
2
|
-
export const VERSION = '0.8.
|
|
2
|
+
export const VERSION = '0.8.12';
|