eyemind 0.5.31 → 0.6.0
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 +79 -159
- package/SPEC.md +382 -1099
- package/package.json +1 -1
- package/src/cli.js +7 -12
- package/src/closure.js +81 -99
- package/src/common.js +36 -1
- package/src/engine.js +64 -51
- package/src/fidelity.js +141 -107
- package/src/formats.js +0 -12
- package/src/index.js +1 -2
- package/src/n3-parser.js +6 -9
- package/src/program.js +1 -1
- package/src/proof.js +114 -960
- package/src/srl.js +12 -11
- package/src/version.js +1 -1
- package/src/native-proof.js +0 -316
package/src/engine.js
CHANGED
|
@@ -3,7 +3,7 @@ import { PRIMITIVE_KEYS, STATE_TESTS, primitive } from './builtins.js';
|
|
|
3
3
|
import { Reliance, Settler, Unsettled } from './closure.js';
|
|
4
4
|
import { freshClause, matchHead, renaming, text, variant } from './common.js';
|
|
5
5
|
import { goalTerm, subgoals } from './goals.js';
|
|
6
|
-
import { callQuestions,
|
|
6
|
+
import { callQuestions, replayer } from './fidelity.js';
|
|
7
7
|
import { INSPECTION_KEYS } from './inspection.js';
|
|
8
8
|
import { checkRendered, clauseDisplay, renderProof } from './proof.js';
|
|
9
9
|
import { CONTROL_KEYS, Program } from './program.js';
|
|
@@ -1078,13 +1078,16 @@ export function skolemTerms(clause, head, unresolved, env, context = null) {
|
|
|
1078
1078
|
return [...unresolved.keys()].map((name) => new Struct('skolem', [BigInt(clause.id), named.get(name), argumentsList]));
|
|
1079
1079
|
}
|
|
1080
1080
|
|
|
1081
|
-
// The
|
|
1082
|
-
//
|
|
1083
|
-
// by its own table, so the tables cost what the searches
|
|
1084
|
-
//
|
|
1085
|
-
//
|
|
1086
|
-
// and
|
|
1087
|
-
|
|
1081
|
+
// The certificate of a run's answers: a call table for every call its
|
|
1082
|
+
// original questions make, each answered by expanding the call one level,
|
|
1083
|
+
// every inner call by its own table, so the tables cost what the searches
|
|
1084
|
+
// did; and the listings of derived facts those searches consult. Every listed
|
|
1085
|
+
// fact must replay its recorded firing, which consults further tables for its
|
|
1086
|
+
// negations and collections. firings are the nodes of a halted run's claims,
|
|
1087
|
+
// which are replayed instead. What the certificate cannot establish is left
|
|
1088
|
+
// out, and checking the proof then fails; an invalid one is an error, since a
|
|
1089
|
+
// proof must not hide it.
|
|
1090
|
+
function certify(solver, questions, firings) {
|
|
1088
1091
|
const program = solver.program;
|
|
1089
1092
|
const computed = new Map();
|
|
1090
1093
|
const met = new Map();
|
|
@@ -1093,8 +1096,9 @@ function certify(solver, boundaries, operational = false) {
|
|
|
1093
1096
|
let budget = 0;
|
|
1094
1097
|
let depth = 0;
|
|
1095
1098
|
let volume = 0;
|
|
1096
|
-
const limit =
|
|
1097
|
-
const callKeyOf = (call) => variant(call,
|
|
1099
|
+
const limit = Math.min(solver.maxInferences, 500000);
|
|
1100
|
+
const callKeyOf = (call) => variant(call, true);
|
|
1101
|
+
let recorded = null;
|
|
1098
1102
|
const settler = new Settler(program, {
|
|
1099
1103
|
answers(call) {
|
|
1100
1104
|
const callKey = callKeyOf(call);
|
|
@@ -1115,14 +1119,14 @@ function certify(solver, boundaries, operational = false) {
|
|
|
1115
1119
|
if (++budget > limit) throw new Unsettled();
|
|
1116
1120
|
// Each answer is written with variables of its own.
|
|
1117
1121
|
const answer = freshTerm(copyResolved(term, env), `answer${++settler.serial}`);
|
|
1118
|
-
if (
|
|
1122
|
+
if ((volume += text(answer).length) > 8 * 1024 * 1024) throw new Unsettled();
|
|
1119
1123
|
answers.push(answer);
|
|
1120
1124
|
}
|
|
1121
1125
|
computed.set(callKey, answers);
|
|
1122
1126
|
return answers;
|
|
1123
1127
|
} catch (error) {
|
|
1124
1128
|
// A full table can search past where the run stopped, as under once/1,
|
|
1125
|
-
// and meet an error the run never did
|
|
1129
|
+
// and meet an error the run never did.
|
|
1126
1130
|
if (error instanceof EyemindError) throw new Unsettled();
|
|
1127
1131
|
throw error;
|
|
1128
1132
|
} finally {
|
|
@@ -1133,10 +1137,10 @@ function certify(solver, boundaries, operational = false) {
|
|
|
1133
1137
|
facts(goalKey) {
|
|
1134
1138
|
const nodes = solver.facts.get(goalKey) || NO_NODES;
|
|
1135
1139
|
listed.set(goalKey, nodes);
|
|
1140
|
+
recorded = null;
|
|
1136
1141
|
return nodes.map((node) => node.goal);
|
|
1137
1142
|
},
|
|
1138
|
-
|
|
1139
|
-
}, operational);
|
|
1143
|
+
});
|
|
1140
1144
|
// Answer a call too deep to reach directly, and the deeper calls it needs.
|
|
1141
1145
|
function ensure(start) {
|
|
1142
1146
|
const pending = [start];
|
|
@@ -1155,57 +1159,66 @@ function certify(solver, boundaries, operational = false) {
|
|
|
1155
1159
|
}
|
|
1156
1160
|
}
|
|
1157
1161
|
}
|
|
1158
|
-
//
|
|
1159
|
-
//
|
|
1160
|
-
|
|
1162
|
+
// Run work to its end, making the tables too deep for it first; null when
|
|
1163
|
+
// the certificate cannot be made.
|
|
1164
|
+
function attempt(work) {
|
|
1165
|
+
for (;;) {
|
|
1166
|
+
try {
|
|
1167
|
+
return work();
|
|
1168
|
+
} catch (error) {
|
|
1169
|
+
if (!(error instanceof Missing)) throw error;
|
|
1170
|
+
try {
|
|
1171
|
+
ensure(error.call);
|
|
1172
|
+
} catch (failure) {
|
|
1173
|
+
if (!(failure instanceof Unsettled)) throw failure;
|
|
1174
|
+
return null;
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
// The recorded derivation of a listed fact or a halted claim and of its
|
|
1180
|
+
// premises, for the replay of its firing; followed keeps the goals whose
|
|
1181
|
+
// steps a replay followed, the steps the proof holds.
|
|
1182
|
+
const followed = new Set();
|
|
1161
1183
|
const stepOf = (goal) => {
|
|
1162
1184
|
if (recorded === null) {
|
|
1163
1185
|
recorded = new Map();
|
|
1164
|
-
const pending = [...listed.values()].flat();
|
|
1186
|
+
const pending = [...[...listed.values()].flat(), ...firings];
|
|
1165
1187
|
while (pending.length) {
|
|
1166
1188
|
const node = pending.pop();
|
|
1167
1189
|
const goalText = text(node.goal);
|
|
1168
1190
|
if (recorded.has(goalText)) continue;
|
|
1169
|
-
recorded.set(goalText, { by: node.by, uses: node.children.map((child) => child.goal) });
|
|
1191
|
+
recorded.set(goalText, { by: node.by, bindings: node.bindings, uses: node.children.map((child) => child.goal) });
|
|
1170
1192
|
pending.push(...node.children);
|
|
1171
1193
|
}
|
|
1172
1194
|
}
|
|
1173
|
-
|
|
1195
|
+
const step = recorded.get(text(goal));
|
|
1196
|
+
if (step) followed.add(text(goal));
|
|
1197
|
+
return step;
|
|
1174
1198
|
};
|
|
1175
|
-
const
|
|
1176
|
-
const
|
|
1177
|
-
for (const
|
|
1178
|
-
recorded = null;
|
|
1179
|
-
let outcome = null;
|
|
1199
|
+
const used = { tables: new Set(), listings: new Set() };
|
|
1200
|
+
const replays = replayer(program, settler, stepOf, used);
|
|
1201
|
+
for (const question of questions) {
|
|
1180
1202
|
budget = 0;
|
|
1181
1203
|
volume = 0;
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
if (!(error instanceof Missing)) throw error;
|
|
1187
|
-
try {
|
|
1188
|
-
ensure(error.call);
|
|
1189
|
-
} catch (failure) {
|
|
1190
|
-
if (!(failure instanceof Unsettled)) throw failure;
|
|
1191
|
-
outcome = { status: 'unsettled' };
|
|
1192
|
-
}
|
|
1193
|
-
}
|
|
1204
|
+
const outcome = attempt(() => settler.answers(question));
|
|
1205
|
+
if (outcome === null || outcome.status === 'unsettled') continue;
|
|
1206
|
+
if (outcome.status === 'failed') {
|
|
1207
|
+
throw new EyemindError(`cannot certify this result: ${outcome.failures[0]?.[0] ?? 'its certificate fails'}`);
|
|
1194
1208
|
}
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
throw new EyemindError(`cannot certify this result: ${outcome.failures[0]?.[0] ?? 'its search certificate fails'}`);
|
|
1209
|
+
for (const item of outcome.tables) used.tables.add(item);
|
|
1210
|
+
for (const item of outcome.listings) used.listings.add(item);
|
|
1211
|
+
for (const goalKey of outcome.listings) {
|
|
1212
|
+
for (const fact of settler.listings.get(goalKey).facts) attempt(() => replays(fact));
|
|
1200
1213
|
}
|
|
1201
|
-
// A halted run stopped before its fixpoint, so its derived facts need not
|
|
1202
|
-
// be closed under the rules; a boundary they cannot settle stays trusted.
|
|
1203
1214
|
}
|
|
1215
|
+
for (const node of firings) attempt(() => replays(node.goal));
|
|
1204
1216
|
return {
|
|
1205
1217
|
// Tables in the order their calls were first met.
|
|
1206
|
-
tables: [...settler.tables].filter(([callKey]) => tables.has(callKey))
|
|
1218
|
+
tables: [...settler.tables].filter(([callKey]) => used.tables.has(callKey))
|
|
1207
1219
|
.sort(([a], [b]) => met.get(a) - met.get(b)).map(([, entry]) => [entry.call, entry.answers]),
|
|
1208
|
-
listings: [...settler.listings.keys()].filter((goalKey) => listings.has(goalKey)).map((goalKey) => [goalKey, listed.get(goalKey)]),
|
|
1220
|
+
listings: [...settler.listings.keys()].filter((goalKey) => used.listings.has(goalKey)).map((goalKey) => [goalKey, listed.get(goalKey)]),
|
|
1221
|
+
followed,
|
|
1209
1222
|
};
|
|
1210
1223
|
}
|
|
1211
1224
|
|
|
@@ -1322,12 +1335,12 @@ function reason(program, goalInputs, options) {
|
|
|
1322
1335
|
let answers = null;
|
|
1323
1336
|
let answerBindings = null;
|
|
1324
1337
|
let stdout = null;
|
|
1338
|
+
// A halted run's claims are replayed from their firings; the questions of
|
|
1339
|
+
// any other run are answered by the certificate.
|
|
1340
|
+
const firings = solver.haltCode !== null ? roots : [];
|
|
1325
1341
|
const rendered = options.proof
|
|
1326
|
-
? renderProof(program, claims,
|
|
1327
|
-
|
|
1328
|
-
const calls = certify(solver, callQuestions(program, goalInputs, claims), true);
|
|
1329
|
-
return { ...ordinary, calls: calls.tables, listings: [...new Map([...ordinary.listings, ...calls.listings])] };
|
|
1330
|
-
}) : null;
|
|
1342
|
+
? renderProof(program, claims, certify(solver, callQuestions(program, goalInputs, claims), firings), firings, true, heading)
|
|
1343
|
+
: null;
|
|
1331
1344
|
const document = rendered !== null ? rendered[0] : null;
|
|
1332
1345
|
// A rule that reports a claim is used by it, though a proof records only
|
|
1333
1346
|
// the claim's support.
|
package/src/fidelity.js
CHANGED
|
@@ -1,23 +1,19 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// The check of a proof: the answers of the original questions, from call
|
|
2
|
+
// tables checked against the source program, are exactly the claims, and
|
|
3
|
+
// every derived fact those answers rest on replays the firing its steps
|
|
4
|
+
// record. These searches start from the original calls, before any final
|
|
5
|
+
// substitution, so they see the bindings a run saw.
|
|
4
6
|
import { PRIMITIVE_KEYS, primitive } from './builtins.js';
|
|
5
7
|
import { Settler, plainGenerators } from './closure.js';
|
|
6
|
-
import {
|
|
7
|
-
import { freshClause, sameVariant, text, variant } from './common.js';
|
|
8
|
+
import { freshClause, isInstance, sameVariant, text, variant } from './common.js';
|
|
8
9
|
import { goalTerm } from './goals.js';
|
|
9
|
-
import { INSPECTION_KEYS } from './inspection.js';
|
|
10
10
|
import {
|
|
11
|
-
Env, EyemindError, Struct, conjunction, copyResolved, deref, flattenConjunction, freshTerm,
|
|
11
|
+
Env, EyemindError, Struct, conjunction, copyResolved, deref, flattenConjunction, freshTerm, isCallable,
|
|
12
12
|
isGround, isTerm, key, listFromItems, unify, variables,
|
|
13
13
|
} from './terms.js';
|
|
14
14
|
import { Lettering, write } from './writer.js';
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
const renamed = freshTerm(pattern, 'callQuestion');
|
|
18
|
-
const env = new Env();
|
|
19
|
-
return unify(renamed, goal, env) && text(copyResolved(renamed, env)) === text(goal);
|
|
20
|
-
}
|
|
16
|
+
const instance = (goal, pattern) => isInstance(goal, pattern, 'callQuestion');
|
|
21
17
|
|
|
22
18
|
// The original questions of Section 8.6, without specializing them to claims.
|
|
23
19
|
// A halted run answers none of them.
|
|
@@ -39,21 +35,30 @@ export function callQuestions(program, goals, claims) {
|
|
|
39
35
|
});
|
|
40
36
|
}
|
|
41
37
|
|
|
42
|
-
//
|
|
43
|
-
// search makes, replaying in source order the
|
|
44
|
-
//
|
|
45
|
-
// whether a variable is bound sees what the search saw.
|
|
46
|
-
//
|
|
47
|
-
//
|
|
48
|
-
//
|
|
49
|
-
//
|
|
50
|
-
//
|
|
51
|
-
//
|
|
52
|
-
|
|
38
|
+
// A function telling whether a derived fact is the conclusion of a firing
|
|
39
|
+
// that a search makes, replaying in source order the firing its step
|
|
40
|
+
// records. Each goal runs with the bindings of that point of the search, so
|
|
41
|
+
// a test of whether a variable is bound sees what the search saw. Primitives
|
|
42
|
+
// run; other goals are answered by the call tables, through Settler.answers,
|
|
43
|
+
// which refuses an order resting on a listing. A call whose search consults
|
|
44
|
+
// derived facts follows its use's step instead, never in a cycle, so derived
|
|
45
|
+
// facts support each other only through steps. A use is compared with what
|
|
46
|
+
// its goal can still become, since steps show goals after the whole
|
|
47
|
+
// solution's bindings. stepOf(goal) gives the recorded justification and
|
|
48
|
+
// uses of a goal, or nothing; used collects the tables and listings the
|
|
49
|
+
// replays consult.
|
|
50
|
+
export function replayer(program, settler, stepOf, used) {
|
|
53
51
|
const confirmed = new Map();
|
|
54
52
|
const active = new Set();
|
|
53
|
+
// The steps a firing's replay followed, each as [replayed, recorded]: once
|
|
54
|
+
// the whole firing has replayed, each must be its step up to renaming.
|
|
55
|
+
let followed = [];
|
|
56
|
+
const exactly = ([replayed, recorded], env) => variant(copyResolved(replayed, env)) === variant(recorded);
|
|
55
57
|
let serial = 0;
|
|
56
58
|
const fresh = (term) => freshTerm(term, `replay${++serial}`);
|
|
59
|
+
// An answer's variables are pattern variables only when its search read
|
|
60
|
+
// the inspected rules or a derived pattern.
|
|
61
|
+
const answer = (outcome, item) => freshTerm(item, `${outcome.inspected ? 'inspect' : 'replay'}${++serial}`);
|
|
57
62
|
// A rule appended by a template replays as its full body, generator first.
|
|
58
63
|
// Witnesses that depend on a captured context are not recomputed here.
|
|
59
64
|
const supportedRule = (clause) => clause && clause.guard === null &&
|
|
@@ -84,6 +89,38 @@ export function replayed(program, settler, listingKeys, stepOf) {
|
|
|
84
89
|
}
|
|
85
90
|
return value;
|
|
86
91
|
}
|
|
92
|
+
// A step's bindings name distinct variables of its clause, each with a
|
|
93
|
+
// value that agrees with what the replay gave it; they bind nothing.
|
|
94
|
+
function bound(step, names, env) {
|
|
95
|
+
const pairs = step.bindings.map((pair) => (pair instanceof Struct ? pair.args : pair));
|
|
96
|
+
if (new Set(pairs.map(([name]) => name)).size !== pairs.length) return false;
|
|
97
|
+
const values = fresh(new Struct('bindings', pairs.map(([, value]) => value))).args;
|
|
98
|
+
const mark = env.mark();
|
|
99
|
+
const agrees = pairs.every(([name], i) => typeof name === 'string' && names.has(name) && unify(names.get(name), values[i], env));
|
|
100
|
+
env.undo(mark);
|
|
101
|
+
return agrees;
|
|
102
|
+
}
|
|
103
|
+
// A goal answered by the call tables with the bindings it has: true when
|
|
104
|
+
// an answer agrees with the use; null when its search consults derived
|
|
105
|
+
// facts, whose support only steps show.
|
|
106
|
+
function answered(goal, use, env) {
|
|
107
|
+
const outcome = settler.answers(copyResolved(goal, env));
|
|
108
|
+
if (outcome.status === 'unsettled' || (outcome.status === 'settled' && outcome.listings.length)) return null;
|
|
109
|
+
for (const item of outcome.tables) used.tables.add(item);
|
|
110
|
+
if (outcome.status !== 'settled') return false;
|
|
111
|
+
// The answer that is the use, up to renaming, is the one the firing
|
|
112
|
+
// took; failing that, the first that can still become it.
|
|
113
|
+
const target = variant(use);
|
|
114
|
+
for (const exact of [true, false]) {
|
|
115
|
+
for (const item of outcome.answers) {
|
|
116
|
+
const mark = env.mark();
|
|
117
|
+
if (unify(goal, answer(outcome, item), env) && compatible(goal, use, env) &&
|
|
118
|
+
(!exact || variant(copyResolved(goal, env)) === target)) return true;
|
|
119
|
+
env.undo(mark);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
87
124
|
function* all(goals, uses, env) {
|
|
88
125
|
if (goals.length !== uses.length) return false;
|
|
89
126
|
for (let i = 0; i < goals.length; i++) if (!(yield one(goals[i], uses[i], env))) return false;
|
|
@@ -94,22 +131,6 @@ export function replayed(program, settler, listingKeys, stepOf) {
|
|
|
94
131
|
if (!isCallable(goal) || !isCallable(use)) return false;
|
|
95
132
|
const goalKey = key(goal);
|
|
96
133
|
if (goalKey !== key(use)) return false;
|
|
97
|
-
if (goalKey === 'call/1' || goalKey === 'or/2') {
|
|
98
|
-
const step = stepOf(use);
|
|
99
|
-
if (!step || step.by !== 'control') return false;
|
|
100
|
-
const used = step.uses.map((item) => text(item));
|
|
101
|
-
const sides = goalKey === 'call/1' ? [0] : [0, 1];
|
|
102
|
-
const side = sides.find((i) => {
|
|
103
|
-
const parts = flattenConjunction(use.args[i]);
|
|
104
|
-
return parts.length === used.length && parts.every((part, j) => text(part) === used[j]);
|
|
105
|
-
});
|
|
106
|
-
return side !== undefined && (yield all(flattenConjunction(goal.args[side]), step.uses, env));
|
|
107
|
-
}
|
|
108
|
-
if (['once/1', 'not/1', 'findall/3', 'findall_sorted/3'].includes(goalKey)) {
|
|
109
|
-
const outcome = settler.answers(copyResolved(goal, env));
|
|
110
|
-
if (outcome.status !== 'settled' || !outcome.answers.length) return false;
|
|
111
|
-
return unify(goal, fresh(outcome.answers[0]), env) && compatible(goal, use, env);
|
|
112
|
-
}
|
|
113
134
|
if (PRIMITIVE_KEYS.has(goalKey)) {
|
|
114
135
|
// A comparison of variables orders them by name, which renaming changes.
|
|
115
136
|
if (goalKey === 'compare/3' && (!isGround(goal.args[1], env) || !isGround(goal.args[2], env))) return false;
|
|
@@ -122,30 +143,36 @@ export function replayed(program, settler, listingKeys, stepOf) {
|
|
|
122
143
|
}
|
|
123
144
|
return false;
|
|
124
145
|
}
|
|
125
|
-
if (
|
|
126
|
-
|
|
146
|
+
if (['once/1', 'not/1', 'findall/3', 'findall_sorted/3'].includes(goalKey)) {
|
|
147
|
+
const outcome = settler.answers(copyResolved(goal, env));
|
|
148
|
+
for (const item of outcome.tables) used.tables.add(item);
|
|
149
|
+
for (const item of outcome.listings) used.listings.add(item);
|
|
150
|
+
if (outcome.status !== 'settled' || !outcome.answers.length) return false;
|
|
151
|
+
return unify(goal, answer(outcome, outcome.answers[0]), env) && compatible(goal, use, env);
|
|
152
|
+
}
|
|
153
|
+
const decided = answered(goal, use, env);
|
|
154
|
+
if (decided !== null) return decided;
|
|
155
|
+
if (goalKey === 'call/1' || goalKey === 'or/2') {
|
|
127
156
|
const step = stepOf(use);
|
|
128
|
-
|
|
129
|
-
|
|
157
|
+
if (!step || step.by !== 'control' || step.bindings.length) return false;
|
|
158
|
+
const used = step.uses.map((item) => text(item));
|
|
159
|
+
const sides = goalKey === 'call/1' ? [0] : [0, 1];
|
|
160
|
+
const side = sides.find((i) => {
|
|
161
|
+
const parts = flattenConjunction(use.args[i]);
|
|
162
|
+
return parts.length === used.length && parts.every((part, j) => text(part) === used[j]);
|
|
163
|
+
});
|
|
164
|
+
if (side === undefined) return false;
|
|
165
|
+
const parts = flattenConjunction(goal.args[side]);
|
|
166
|
+
followed.push([new Struct('step', [goal, ...parts]), new Struct('step', [use, ...step.uses])]);
|
|
167
|
+
return yield all(parts, step.uses, env);
|
|
130
168
|
}
|
|
131
169
|
const step = stepOf(use);
|
|
132
|
-
if (!step)
|
|
133
|
-
// A use without a step is an instance of a source fact.
|
|
134
|
-
for (const clause of program.groups.get(goalKey) || []) {
|
|
135
|
-
if (clause.body.length || clause.forward || clause.parentId !== null) continue;
|
|
136
|
-
const mark = env.mark();
|
|
137
|
-
const [head] = freshClause(clause, `replay${++serial}`);
|
|
138
|
-
if (unify(goal, head, env) && compatible(goal, use, env)) return true;
|
|
139
|
-
env.undo(mark);
|
|
140
|
-
}
|
|
141
|
-
return false;
|
|
142
|
-
}
|
|
143
|
-
if (!isTerm(step.by, 'clause', 1)) return false;
|
|
170
|
+
if (!step || !isTerm(step.by, 'clause', 1)) return false;
|
|
144
171
|
const clause = program.byId.get(Number(step.by.args[0]));
|
|
145
172
|
if (!supportedRule(clause)) return false;
|
|
146
173
|
if (clause.forward) return (yield fact(use)) && unify(goal, freshTerm(use, `pattern${++serial}`), env);
|
|
147
|
-
const [head, body] = freshClause(clause, `replay${++serial}`);
|
|
148
|
-
if (!body.length) return unify(goal, head, env) && compatible(goal, use, env);
|
|
174
|
+
const [head, body, names] = freshClause(clause, `replay${++serial}`);
|
|
175
|
+
if (!body.length) return unify(goal, head, env) && bound(step, names, env) && compatible(goal, use, env);
|
|
149
176
|
const id = text(use);
|
|
150
177
|
if (active.has(id)) return false;
|
|
151
178
|
active.add(id);
|
|
@@ -153,7 +180,9 @@ export function replayed(program, settler, listingKeys, stepOf) {
|
|
|
153
180
|
// meets the call.
|
|
154
181
|
const prefix = clause.prefixLength;
|
|
155
182
|
const solved = step.uses.length === body.length && (yield all(body.slice(0, prefix), step.uses.slice(0, prefix), env)) &&
|
|
156
|
-
unify(goal, head, env) && (yield all(body.slice(prefix), step.uses.slice(prefix), env)) &&
|
|
183
|
+
unify(goal, head, env) && (yield all(body.slice(prefix), step.uses.slice(prefix), env)) && bound(step, names, env) &&
|
|
184
|
+
compatible(goal, use, env);
|
|
185
|
+
if (solved) followed.push([new Struct('step', [head, ...body]), new Struct('step', [use, ...step.uses])]);
|
|
157
186
|
active.delete(id);
|
|
158
187
|
return solved;
|
|
159
188
|
}
|
|
@@ -167,6 +196,8 @@ export function replayed(program, settler, listingKeys, stepOf) {
|
|
|
167
196
|
let holds = false;
|
|
168
197
|
if (supportedRule(clause) && clause.forward) {
|
|
169
198
|
active.add(id);
|
|
199
|
+
const outer = followed;
|
|
200
|
+
followed = [];
|
|
170
201
|
const [head, body, names] = freshClause(clause, `replay${++serial}`);
|
|
171
202
|
const env = new Env();
|
|
172
203
|
holds = yield all(body, step.uses, env);
|
|
@@ -184,86 +215,89 @@ export function replayed(program, settler, listingKeys, stepOf) {
|
|
|
184
215
|
unify(names.get(name), new Struct('skolem', [BigInt(clause.id), name, listFromItems(values)]), env);
|
|
185
216
|
}
|
|
186
217
|
}
|
|
187
|
-
|
|
188
|
-
//
|
|
189
|
-
//
|
|
190
|
-
|
|
218
|
+
holds = holds && bound(step, names, env);
|
|
219
|
+
// The firing is its step up to renaming: the fact is an instance of one
|
|
220
|
+
// of the rule's heads and the uses its body, variables shared as they
|
|
221
|
+
// are. A derived pattern keeps only variables an inspection supplied:
|
|
222
|
+
// the run binds any other head variable a firing leaves unbound to a
|
|
223
|
+
// Skolem term.
|
|
224
|
+
const recorded = new Struct('step', [goal, ...step.uses]);
|
|
191
225
|
holds = holds && flattenConjunction(head).some((item) => {
|
|
192
226
|
if (!isCallable(item)) return false;
|
|
193
227
|
const conclusion = copyResolved(item, env);
|
|
194
|
-
return [...variables(conclusion).keys()].every((name) => /#(?:inspect|pattern)\d
|
|
195
|
-
|
|
228
|
+
return [...variables(conclusion).keys()].every((name) => /#(?:inspect|pattern)\d+(?:#|$)/.test(name)) &&
|
|
229
|
+
exactly([new Struct('step', [item, ...body]), recorded], env);
|
|
230
|
+
}) && followed.every((pair) => exactly(pair, env));
|
|
231
|
+
followed = outer;
|
|
196
232
|
active.delete(id);
|
|
197
233
|
}
|
|
198
234
|
confirmed.set(id, holds);
|
|
199
235
|
return holds;
|
|
200
236
|
}
|
|
201
|
-
return
|
|
237
|
+
return (goal) => finish(fact(goal));
|
|
202
238
|
}
|
|
203
239
|
|
|
204
|
-
//
|
|
205
|
-
//
|
|
206
|
-
//
|
|
207
|
-
//
|
|
208
|
-
|
|
240
|
+
// Check the claims against the original questions, adding failures to fail.
|
|
241
|
+
// Each question's answers come from its certificate; every derived fact they
|
|
242
|
+
// rest on must replay; each claim must be an answer and each answer claimed.
|
|
243
|
+
// A halted run stopped before its fixpoint, so its claims are each replayed
|
|
244
|
+
// instead, which shows that they follow but not that they are every answer.
|
|
245
|
+
// Returns the tables and listings the check consulted.
|
|
246
|
+
export function checkAnswers(program, claims, goals, certificate, stepOf, fail) {
|
|
209
247
|
const settler = new Settler(program, {
|
|
210
248
|
answers: (call) => certificate.calls.get(variant(call, true)) ?? null,
|
|
211
249
|
facts: (goalKey) => certificate.listings.get(goalKey) ?? null,
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
250
|
+
});
|
|
251
|
+
const used = { tables: new Set(), listings: new Set() };
|
|
252
|
+
const replays = replayer(program, settler, stepOf, used);
|
|
253
|
+
if (claims.includes('false')) {
|
|
254
|
+
for (const claim of claims) {
|
|
255
|
+
if (!replays(claim)) fail.add(`the claim does not replay a recorded firing: ${text(claim)}`, claim);
|
|
256
|
+
}
|
|
257
|
+
return used;
|
|
258
|
+
}
|
|
216
259
|
const outcomes = callQuestions(program, goals, claims).map((question) => {
|
|
217
260
|
const outcome = settler.answers(question);
|
|
218
|
-
|
|
219
|
-
|
|
261
|
+
for (const item of outcome.tables) used.tables.add(item);
|
|
262
|
+
for (const item of outcome.listings) used.listings.add(item);
|
|
263
|
+
for (const [detail, term] of outcome.failures) fail.add(detail, term);
|
|
264
|
+
if (outcome.status === 'unsettled') {
|
|
265
|
+
fail.add(`the certificate cannot establish the answers of ${text(question)}`, question);
|
|
266
|
+
} else if (outcome.status === 'settled') {
|
|
267
|
+
for (const goalKey of outcome.listings) {
|
|
268
|
+
for (const fact of settler.listings.get(goalKey)?.facts || []) {
|
|
269
|
+
if (!replays(fact)) {
|
|
270
|
+
fail.add(`the derived fact does not replay a recorded firing: ${text(fact)}`, fact);
|
|
271
|
+
outcome.status = 'failed';
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
220
275
|
}
|
|
221
276
|
return { question, ...outcome };
|
|
222
277
|
});
|
|
223
|
-
const unresolved = [];
|
|
224
|
-
let covered = 0;
|
|
225
278
|
for (const claim of claims) {
|
|
226
279
|
const matching = outcomes.filter(({ question }) => instance(claim, question));
|
|
227
|
-
|
|
228
|
-
if (matching.
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
if (matching.length && matching.every(({ status }) => status === 'settled')) {
|
|
232
|
-
detail = 'the original call does not return this answer';
|
|
233
|
-
} else {
|
|
234
|
-
unresolved.push([{ kind: 'call_fidelity', reason: 'original_call', conclusion: name(claim) }, claim]);
|
|
235
|
-
if (!allowTrusted) fail.add('C10', `call fidelity remains unproved: ${name(claim)}`, claim);
|
|
236
|
-
continue;
|
|
237
|
-
}
|
|
280
|
+
if (!matching.length) fail.add(`the claim answers no question: ${text(claim)}`, claim);
|
|
281
|
+
else if (matching.every(({ status }) => status === 'settled') &&
|
|
282
|
+
!matching.some(({ answers }) => answers.some((answer) => sameVariant(answer, claim)))) {
|
|
283
|
+
fail.add(`the question does not return this answer: ${text(claim)}`, claim);
|
|
238
284
|
}
|
|
239
|
-
covered++;
|
|
240
|
-
if (detail !== null) fail.add('C10', `${detail}: ${text(claim)}`, claim);
|
|
241
285
|
}
|
|
286
|
+
// When the conclusions are the derived facts, a question's answers that are
|
|
287
|
+
// not derived (source facts, backward answers) are no conclusions, so only
|
|
288
|
+
// its listed facts are.
|
|
242
289
|
const derivedOnly = !(goals || []).length && !program.forward.some((clause) => clause.heads.includes('true'));
|
|
243
290
|
const claimed = new Set(claims.map((claim) => variant(claim)));
|
|
244
291
|
for (const { question, status, answers } of outcomes) {
|
|
245
|
-
if (status
|
|
246
|
-
// A claim of this question already failed with its certificate.
|
|
247
|
-
if (!claims.some((claim) => instance(claim, question))) {
|
|
248
|
-
fail.add('C10', `the original call has an invalid search certificate: ${text(question)}`, question);
|
|
249
|
-
}
|
|
250
|
-
covered++;
|
|
251
|
-
continue;
|
|
252
|
-
}
|
|
253
|
-
if (status !== 'settled') {
|
|
254
|
-
unresolved.push([{ kind: 'call_fidelity', reason: 'all_answers', conclusion: text(question) }, question]);
|
|
255
|
-
if (!allowTrusted) fail.add('C10', `answer completeness remains unproved: ${text(question)}`, question);
|
|
256
|
-
continue;
|
|
257
|
-
}
|
|
292
|
+
if (status !== 'settled') continue;
|
|
258
293
|
const required = derivedOnly
|
|
259
294
|
? (settler.listings.get(key(question))?.facts || []).filter((fact) => instance(fact, question))
|
|
260
295
|
: answers;
|
|
261
|
-
covered++;
|
|
262
296
|
for (const answer of required) {
|
|
263
297
|
if (!claimed.has(variant(answer))) {
|
|
264
|
-
fail.add(
|
|
298
|
+
fail.add(`the proof omits an answer of ${text(question)}: ${write(answer, null, new Lettering())}`, answer);
|
|
265
299
|
}
|
|
266
300
|
}
|
|
267
301
|
}
|
|
268
|
-
return
|
|
302
|
+
return used;
|
|
269
303
|
}
|
package/src/formats.js
CHANGED
|
@@ -22,8 +22,6 @@ import { resultToN3, compileSources } from './n3.js';
|
|
|
22
22
|
import { compileSrl, srlAnswers, srlQuads } from './srl.js';
|
|
23
23
|
import { write } from './writer.js';
|
|
24
24
|
import { isRdfMessageLog, parseRdfMessages } from './messages.js';
|
|
25
|
-
import { writeNativeProof } from './native-proof.js';
|
|
26
|
-
import { checkProof } from './proof.js';
|
|
27
25
|
|
|
28
26
|
export const OUTPUT_FORMATS = ['eyemind', 'turtle', 'trig', 'n-triples', 'n-quads', 'n3'];
|
|
29
27
|
|
|
@@ -124,16 +122,6 @@ function importResolver(io) {
|
|
|
124
122
|
// eyemind's own text for a program.
|
|
125
123
|
export const defaultFormat = (kind) => ({ n3: 'n3', srl: 'turtle' })[kind] ?? 'eyemind';
|
|
126
124
|
|
|
127
|
-
// The core certificate keeps its specified JavaScript form. Native inputs
|
|
128
|
-
// expose the same evidence as N3 or SRL data at the format boundary.
|
|
129
|
-
export function writeProof(loaded, result, { goals = null } = {}) {
|
|
130
|
-
if (loaded.kind === 'js') return result.proof;
|
|
131
|
-
const document = writeNativeProof(result.proof, { format: loaded.kind, prefixes: loaded.prefixes });
|
|
132
|
-
const report = checkProof(loaded.program, document, { goals });
|
|
133
|
-
if (!report.valid) throw new EyemindError(`generated ${loaded.kind} proof failed verification: ${report.failures[0].detail}`);
|
|
134
|
-
return document;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
125
|
// The conclusions of a run in an output format; null for eyemind's own text.
|
|
138
126
|
// all also writes the given data. Throws when a conclusion is no triple.
|
|
139
127
|
export function writeConclusions(loaded, result, { format, all = false, blankNodePrefix = '' } = {}) {
|
package/src/index.js
CHANGED
|
@@ -9,8 +9,7 @@ export { Result, run, unusedClauses } from './engine.js';
|
|
|
9
9
|
export { readGoal } from './goals.js';
|
|
10
10
|
export { FloatLiteral, build, loadSources, loadText, toTerm } from './lang.js';
|
|
11
11
|
export { Program } from './program.js';
|
|
12
|
-
export { checkProof, checkReport, publicReport,
|
|
13
|
-
export { readNativeProof, readProofTerms, writeNativeProof } from './native-proof.js';
|
|
12
|
+
export { checkProof, checkReport, publicReport, verdictText } from './proof.js';
|
|
14
13
|
export { readTerm, readTerms } from './reader.js';
|
|
15
14
|
export { EyemindError, Struct, Var } from './terms.js';
|
|
16
15
|
export { write } from './writer.js';
|
package/src/n3-parser.js
CHANGED
|
@@ -248,9 +248,8 @@ export const DEFAULT_PREFIXES = {
|
|
|
248
248
|
};
|
|
249
249
|
|
|
250
250
|
// Parse a document: { triples, prefixes, base }. Prefixes map names to IRIs
|
|
251
|
-
// in declaration order.
|
|
252
|
-
|
|
253
|
-
export function parseN3(src, { base = '', preserveNumericLiterals = false } = {}) {
|
|
251
|
+
// in declaration order.
|
|
252
|
+
export function parseN3(src, { base = '' } = {}) {
|
|
254
253
|
src = String(src);
|
|
255
254
|
const input = tokens(src);
|
|
256
255
|
const ahead = [];
|
|
@@ -327,7 +326,7 @@ export function parseN3(src, { base = '', preserveNumericLiterals = false } = {}
|
|
|
327
326
|
if (isP('^^')) {
|
|
328
327
|
next();
|
|
329
328
|
const dt = iriTerm();
|
|
330
|
-
return typedLiteral(text, dt.v
|
|
329
|
+
return typedLiteral(text, dt.v);
|
|
331
330
|
}
|
|
332
331
|
return { t: 'literal', v: text, dt: `${XSD}string` };
|
|
333
332
|
}
|
|
@@ -381,7 +380,7 @@ export function parseN3(src, { base = '', preserveNumericLiterals = false } = {}
|
|
|
381
380
|
if (tok.k === 'var') return { t: 'var', v: tok.v, scope: 0 };
|
|
382
381
|
if (tok.k === 'bnode') return { t: 'bnode', v: tok.v, scope: scope.id };
|
|
383
382
|
if (tok.k === 'string') return literalTail(tok.v);
|
|
384
|
-
if (tok.k === 'number') return numberTerm(tok.v
|
|
383
|
+
if (tok.k === 'number') return numberTerm(tok.v);
|
|
385
384
|
if (tok.k === 'word') {
|
|
386
385
|
if (tok.v === 'true' || tok.v === 'false') return { t: 'literal', v: tok.v, dt: `${XSD}boolean` };
|
|
387
386
|
throw fail(`unexpected word '${tok.v}'; prefixed names need a colon, as in :${tok.v}`, tok);
|
|
@@ -510,9 +509,8 @@ export function parseN3(src, { base = '', preserveNumericLiterals = false } = {}
|
|
|
510
509
|
return { triples: top.triples, prefixes, base };
|
|
511
510
|
}
|
|
512
511
|
|
|
513
|
-
function numberTerm(text
|
|
512
|
+
function numberTerm(text) {
|
|
514
513
|
if (/^[+-]?[0-9]+$/.test(text)) return { t: 'int', v: BigInt(text) };
|
|
515
|
-
if (preserve) return { t: 'literal', v: text, dt: `${XSD}${/[eE]/.test(text) ? 'double' : 'decimal'}` };
|
|
516
514
|
if (/[eE]/.test(text)) return { t: 'literal', v: String(Number(text)), dt: `${XSD}double` };
|
|
517
515
|
return { t: 'float', v: Number(text) };
|
|
518
516
|
}
|
|
@@ -522,10 +520,9 @@ const FLOAT_TYPES = new Set(['decimal']);
|
|
|
522
520
|
|
|
523
521
|
// Native numbers represent integer and decimal. Float and double retain
|
|
524
522
|
// their datatype so triple matching and log:equalTo distinguish them.
|
|
525
|
-
export function typedLiteral(text, dt
|
|
523
|
+
export function typedLiteral(text, dt) {
|
|
526
524
|
if (dt.startsWith(XSD)) {
|
|
527
525
|
const local = dt.slice(XSD.length);
|
|
528
|
-
if (preserve && ['decimal', 'float', 'double'].includes(local)) return { t: 'literal', v: text, dt };
|
|
529
526
|
if (INTEGER_TYPES.has(local) && /^[+-]?[0-9]+$/.test(text)) return { t: 'int', v: BigInt(text) };
|
|
530
527
|
if (FLOAT_TYPES.has(local) && /^[+-]?([0-9]+(\.[0-9]*)?|\.[0-9]+)([eE][+-]?[0-9]+)?$/.test(text)) return { t: 'float', v: Number(text) };
|
|
531
528
|
if ((local === 'double' || local === 'float') && Number.isFinite(Number(text)) && /^[+-]?([0-9]+(\.[0-9]*)?|\.[0-9]+)([eE][+-]?[0-9]+)?$/.test(text)) return { t: 'literal', v: String(Number(text)), dt };
|