eyemind 0.5.32 → 0.6.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/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, replayed } from './fidelity.js';
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 search certificate of a run's negations and collections. Each call
1082
- // their searches make is answered by expanding it one level, every inner call
1083
- // by its own table, so the tables cost what the searches did; the checker
1084
- // then checks each table again, and each boundary against what the run found.
1085
- // A boundary the certificate cannot settle stays an obligation; one the run
1086
- // and its certificate disagree on is an error, since a proof must not hide it.
1087
- function certify(solver, boundaries, operational = false) {
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 = operational ? Math.min(solver.maxInferences, 500000) : solver.maxInferences;
1097
- const callKeyOf = (call) => variant(call, operational);
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 (operational && (volume += text(answer).length) > 8 * 1024 * 1024) throw new Unsettled();
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: such a boundary stays trusted.
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
- supported: () => true,
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
- // The recorded derivation of a listed fact or one of its premises, for the
1159
- // replay that supports the original calls.
1160
- let recorded = null;
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
- return recorded.get(text(goal));
1195
+ const step = recorded.get(text(goal));
1196
+ if (step) followed.add(text(goal));
1197
+ return step;
1174
1198
  };
1175
- const tables = new Set();
1176
- const listings = new Set();
1177
- for (const boundary of boundaries) {
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
- while (outcome === null) {
1183
- try {
1184
- outcome = operational ? settler.answers(boundary) : settler.settle(boundary);
1185
- } catch (error) {
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
- if (outcome.status === 'settled' && (!operational || replayed(program, settler, outcome.listings, stepOf))) {
1196
- for (const item of outcome.tables) tables.add(item);
1197
- for (const item of outcome.listings) listings.add(item);
1198
- } else if (outcome.status === 'failed' && solver.haltCode === null) {
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, roots, true, heading, (boundaries) => {
1327
- const ordinary = certify(solver, boundaries);
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
- // C10: answers of the original questions, checked from source call tables:
2
- // each claim is an answer, and each answer is claimed. Unlike logical steps,
3
- // these searches start before a final substitution.
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 { stepIdOf } from './proof.js';
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, identity, isCallable,
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
- function instance(goal, pattern) {
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
- // Whether each fact of these listings is the conclusion of a firing that a
43
- // search makes, replaying in source order the derivation its steps record.
44
- // Primitives run on the bindings of that point of the search, so a test of
45
- // whether a variable is bound sees what the search saw. A relation call
46
- // follows its use's own step, which C2 keeps acyclic; a listed use is
47
- // replayed in turn. once/1, not/1 and findall/3 are decided by the call
48
- // tables, through Settler.answers, which refuses an order resting on a
49
- // listing. A use is compared with what its goal can still become, since steps
50
- // show goals after the whole solution's bindings. stepOf(goal) gives the
51
- // recorded justification and uses of a goal, or nothing.
52
- export function replayed(program, settler, listingKeys, stepOf) {
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 (INSPECTION_KEYS.has(goalKey)) {
126
- // An inspection reads the source rule its step cites, renamed apart.
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
- const source = step && isTerm(step.by, 'source', 1) ? program.inspectedById.get(Number(step.by.args[0])) : null;
129
- return Boolean(source) && unify(goal, freshTerm(source.term, `inspect${++serial}`), env) && compatible(goal, use, env);
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)) && compatible(goal, use, 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
- // A derived pattern is identified up to the renaming of its variables,
188
- // and keeps only variables an inspection supplied: the run binds any
189
- // other head variable a firing leaves unbound to a Skolem term.
190
- const same = isGround(goal) ? (item) => identity(item) === identity(goal) : (item) => variant(item) === variant(goal);
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+$/.test(name)) && same(conclusion);
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 listingKeys.every((goalKey) => (settler.listings.get(goalKey)?.facts || []).every((goal) => finish(fact(goal))));
237
+ return (goal) => finish(fact(goal));
202
238
  }
203
239
 
204
- // Both directions of C10: each claim is an answer of an original question,
205
- // and each answer of a certified question is claimed. When the conclusions
206
- // are the derived facts, a question's answers that are not derived (source
207
- // facts, backward answers) are no conclusions, so only its listed facts are.
208
- export function checkCalls(program, claims, goals, certificate, steps, spellings, fail, allowTrusted, name) {
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
- supported: (fact) => steps.has(stepIdOf(steps, spellings, fact)),
213
- }, true);
214
- if (claims.includes('false')) return { settler, outcomes: [], unresolved: [], covered: 0 };
215
- const stepOf = (goal) => steps.get(stepIdOf(steps, spellings, goal));
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
- if (outcome.status === 'settled' && !replayed(program, settler, outcome.listings, stepOf)) {
219
- return { question, ...outcome, status: 'unsupported' };
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
- let detail = null;
228
- if (matching.some(({ status }) => status === 'failed')) detail = 'the original call has an invalid search certificate';
229
- else if (!matching.some(({ status, answers }) => status === 'settled' &&
230
- answers.some((answer) => sameVariant(answer, claim)))) {
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 === 'failed') {
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('C10', `the proof omits an answer of the original call: ${write(answer, null, new Lettering())}`, answer);
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 { settler, outcomes, unresolved, covered };
302
+ return used;
269
303
  }
package/src/index.js CHANGED
@@ -2,14 +2,14 @@
2
2
  //
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
- // answer can come with a proof that a separate checker verifies against the
6
- // program, condition by condition (C1-C10).
5
+ // certifiable answer can come with a proof that a separate checker verifies
6
+ // against the program, using its call tables and recorded firings.
7
7
  export { VERSION } from './version.js';
8
8
  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, strict, verdictText } from './proof.js';
12
+ export { checkProof, checkReport, publicReport, verdictText } from './proof.js';
13
13
  export { readTerm, readTerms } from './reader.js';
14
14
  export { EyemindError, Struct, Var } from './terms.js';
15
15
  export { write } from './writer.js';
package/src/program.js CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  import { write } from './writer.js';
12
12
 
13
13
  export const CONTROL_KEYS = new Set(['and/2', 'or/2', 'not/1', 'call/1', 'once/1', 'findall/3', 'findall_sorted/3']);
14
- export const RESERVED_KEYS = new Set(['step/4', 'clause/2', 'searched/2', 'derived/2', 'called/2', 'activated/2', 'implies/2', 'implied_by/2', 'forward/2', 'backward/2']);
14
+ export const RESERVED_KEYS = new Set(['step/4', 'derived/2', 'called/2', 'activated/2', 'implies/2', 'implied_by/2', 'forward/2', 'backward/2']);
15
15
 
16
16
  // The statements that state a forward rule, and how an error names them.
17
17
  const FORWARD_KINDS = { forward: 'a forward rule', restricted: 'restricted()', query: 'query()', contradiction: 'contradiction()' };