eyemind 0.5.22 → 0.5.23

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 CHANGED
@@ -7,7 +7,7 @@ eyemind is a **JavaScript rule language** with forward and backward reasoning an
7
7
 
8
8
  [Playground](https://eyereasoner.github.io/eyemind/playground/) · [Specification](https://eyereasoner.github.io/eyemind/SPEC) · [Conformance](https://eyereasoner.github.io/eyemind/conformance/) · [Examples](https://eyereasoner.github.io/eyemind/examples/)
9
9
 
10
- The playground runs programs in your browser; the specification defines the language, its canonical text, the proof format and the C1–C10 checks. The examples are 135 runnable programs, each with a card deck and saved conclusions, proofs and check reports. The conformance suite has 1381 cases that test any implementation of the specification through its command line; run it [in your browser](https://eyereasoner.github.io/eyemind/playground/conformance).
10
+ The playground runs programs in your browser; the specification defines the language, its canonical text, the proof format and the C1–C10 checks. The examples are 135 runnable programs, each with a card deck and saved conclusions, proofs and check reports. The conformance suite has 1382 cases that test any implementation of the specification through its command line; run it [in your browser](https://eyereasoner.github.io/eyemind/playground/conformance).
11
11
 
12
12
  ## Contents
13
13
 
@@ -480,7 +480,7 @@ A negation `not(G)` concludes from what is *absent* and a collection `findall(T,
480
480
  - **Negate only what is bound.** The variables of `not(G)` must be bound when it runs; write `not(q(X, _))` for "no value at all".
481
481
  - **Check the search, not just its result.** A proof ends with a search certificate: `searched(Call, Answers)` lists each call's answers and `derived(Pattern, Facts)` each consulted relation. The checker expands each call one level against the program, so a wrong table shows up as a disagreement.
482
482
  - **Say what was taken on trust.** Anything the certificate cannot settle becomes an obligation, and evidence in the proof can still refute it (C6).
483
- - **Certify the original questions.** Supported, closed listings establish the original calls, and that the claims are all their answers, when no rule they depend on can observe bindings or evaluation order. A mode analysis checks this: primitives such as arithmetic run with their inputs bound, negations with their variables bound, and `once` and `findall` bind nothing other goals use. A proof within this fragment can be `checked` with no obligation, even under `--strict-proof`. Tests of whether a variable is bound, collections of derived facts, whose order a listing cannot show, templates and `restricted` rules with new witnesses keep an obligation.
483
+ - **Certify the original questions.** Supported, closed listings establish the original calls, and that the claims are all their answers, when no rule they depend on can observe bindings or evaluation order. A mode analysis checks this: primitives such as arithmetic run with their inputs bound, negations with their variables bound, and `once` and `findall` bind nothing other goals use. Where it does not apply, as for tests of whether a variable is bound, the checker replays each derived fact's recorded firing in source order instead. A proof within these bounds can be `checked` with no obligation, even under `--strict-proof`. Collections and first answers over derived facts, whose order a listing cannot show, templates and `restricted` rules with new witnesses keep an obligation.
484
484
 
485
485
  ## Checking the translation
486
486
 
package/SPEC.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ```text
4
4
  Title: The eyemind Rule Language and Proof Format
5
- Version: eyemind 0.5.22
5
+ Version: eyemind 0.5.23
6
6
  ```
7
7
 
8
8
  This document specifies the syntax and meaning of eyemind programs and data
@@ -2250,17 +2250,55 @@ whose ground instances C5 re-decides as holding. `number_chars/2` re-decides
2250
2250
  a ground instance by reading its characters, so only that direction is
2251
2251
  listed. A host-math result accepted by C5 only within its tolerance
2252
2252
  ([Section 11.8](#118-c5-re-decision)) need not be the result a search
2253
- finds. A proof with such a step therefore supports no listing for C10.
2253
+ finds. A proof with such a step therefore supports no listing for C10 by
2254
+ this analysis.
2255
+
2256
+ **Replayed listings.** A listing the mode analysis does not support still
2257
+ supports C10 when every fact of every consulted listing **replays**. A fact
2258
+ replays when its step cites a forward rule, other than a template, a rule
2259
+ with captures or a restricted rule, and a search of that rule's body in
2260
+ source order, starting with no bindings, solves each body goal as the
2261
+ step's corresponding use records:
2262
+
2263
+ - A primitive runs on the bindings of that point of the search. Its first
2264
+ solution under which the goal can still unify with the use is taken.
2265
+ `compare/3` with a nonground compared term does not replay.
2266
+ - `once/1`, `not/1` and `findall/3` take the answers of the goal, as it
2267
+ stands at that point, from the call tables and listings, as in checking
2268
+ the original calls. A negation with an unbound named variable does not
2269
+ replay, and neither does a first answer or a collection whose order rests
2270
+ on a listing. The first answer is taken, and the goal MUST still unify
2271
+ with the use.
2272
+ - `call/1` and a disjunction replay the goals their `'control'` step uses,
2273
+ for a disjunction from the side those uses form.
2274
+ - A call to an ordinary relation follows the use's step. A forward rule's
2275
+ conclusion MUST itself replay, and is then unified with the goal. A
2276
+ source fact, renamed apart, is unified with the goal. A backward rule,
2277
+ renamed apart, has its head unified with the goal and its body replayed
2278
+ against its step's uses. A use with no step is unified with a source fact
2279
+ of the relation, renamed apart. A replay that meets the same use under
2280
+ the same call again does not replay.
2281
+ - After each goal, the goal MUST still unify with its use, since a step
2282
+ shows its goals after the whole solution's bindings.
2283
+
2284
+ Each witness is then bound to its Skolem term
2285
+ ([Section 8.2](#82-forward-evaluation-and-witnesses)), and the fact MUST be
2286
+ identical to an instance of one of the rule's heads. Replay re-executes each
2287
+ recorded firing, so a test of whether a variable is bound sees what a search
2288
+ sees, a first answer is the search's first answer, and host math is
2289
+ computed rather than accepted within a tolerance. C2 keeps the steps a
2290
+ replay follows acyclic.
2254
2291
 
2255
2292
  Supported facts and closed listings then establish the least ground
2256
2293
  closure and, with negations, a set of facts that is supported, closed under
2257
2294
  the rules and consistent with each negation, every negation being decided
2258
2295
  by the listed facts alone. A stratified or chain-stratified program has
2259
2296
  exactly one such set ([Section 7.4](#74-negation-and-collection)), its
2260
- result, so the listings are the facts the original calls consult. A rule
2261
- outside the analysis, for example one that tests whether a variable is
2262
- bound or keeps the first of several answers it uses, requires publication
2263
- evidence this format does not supply, leaving call fidelity unproved.
2297
+ result, so the listings are the facts the original calls consult. A
2298
+ collection or first answer over facts that forward rules derive depends on
2299
+ their publication order, which this format does not establish, and a
2300
+ restricted rule's firings can depend on the order of rule applications:
2301
+ both leave call fidelity unproved.
2264
2302
 
2265
2303
  **Deciding claims.** Every relevant call certificate MUST be valid. Each
2266
2304
  claim MUST be identical, up to variable renaming, to an answer of at least
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eyemind",
3
- "version": "0.5.22",
3
+ "version": "0.5.23",
4
4
  "description": "A dependency-free JavaScript rule language with forward and backward reasoning and checkable proofs.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
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, positiveListings } from './fidelity.js';
6
+ import { callQuestions, positiveListings, replayed } 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';
@@ -1130,7 +1130,6 @@ function certify(solver, boundaries, operational = false) {
1130
1130
  }
1131
1131
  },
1132
1132
  facts(goalKey) {
1133
- if (operational && !positiveListings(program, [goalKey])) throw new Unsettled();
1134
1133
  const nodes = solver.facts.get(goalKey) || NO_NODES;
1135
1134
  if (nodes.some((node) => node.generic)) throw new Unsettled();
1136
1135
  listed.set(goalKey, nodes);
@@ -1156,9 +1155,27 @@ function certify(solver, boundaries, operational = false) {
1156
1155
  }
1157
1156
  }
1158
1157
  }
1158
+ // The recorded derivation of a listed fact or one of its premises, for the
1159
+ // replay that supports a listing beyond the mode analysis.
1160
+ let recorded = null;
1161
+ const stepOf = (goal) => {
1162
+ if (recorded === null) {
1163
+ recorded = new Map();
1164
+ const pending = [...listed.values()].flat();
1165
+ while (pending.length) {
1166
+ const node = pending.pop();
1167
+ const goalText = text(node.goal);
1168
+ if (recorded.has(goalText)) continue;
1169
+ recorded.set(goalText, { by: node.by, uses: node.children.map((child) => child.goal) });
1170
+ pending.push(...node.children);
1171
+ }
1172
+ }
1173
+ return recorded.get(text(goal));
1174
+ };
1159
1175
  const tables = new Set();
1160
1176
  const listings = new Set();
1161
1177
  for (const boundary of boundaries) {
1178
+ recorded = null;
1162
1179
  let outcome = null;
1163
1180
  budget = 0;
1164
1181
  volume = 0;
@@ -1175,7 +1192,8 @@ function certify(solver, boundaries, operational = false) {
1175
1192
  }
1176
1193
  }
1177
1194
  }
1178
- if (outcome.status === 'settled' && (!operational || positiveListings(program, outcome.listings))) {
1195
+ if (outcome.status === 'settled' && (!operational || positiveListings(program, outcome.listings) ||
1196
+ replayed(program, settler, outcome.listings, stepOf))) {
1179
1197
  for (const item of outcome.tables) tables.add(item);
1180
1198
  for (const item of outcome.listings) listings.add(item);
1181
1199
  } else if (outcome.status === 'failed' && solver.haltCode === null) {
package/src/fidelity.js CHANGED
@@ -1,13 +1,14 @@
1
1
  // C10: answers of the original questions, checked from source call tables:
2
2
  // each claim is an answer, and each answer is claimed. Unlike logical steps,
3
3
  // these searches start before a final substitution.
4
- import { PRIMITIVE_KEYS } from './builtins.js';
4
+ import { PRIMITIVE_KEYS, primitive } from './builtins.js';
5
5
  import { Settler } from './closure.js';
6
- import { text, variant } from './common.js';
6
+ import { freshClause, text, variant } from './common.js';
7
7
  import { goalTerm } from './goals.js';
8
8
  import { INSPECTION_KEYS } from './inspection.js';
9
9
  import {
10
- Env, Struct, Var, conjunction, copyResolved, freshTerm, isAnonymous, isCallable, isGround, isTerm, key, listFromItems, unify, variables,
10
+ Env, EyemindError, Struct, Var, conjunction, copyResolved, deref, flattenConjunction, freshTerm, identity, isAnonymous, isCallable,
11
+ isGround, isTerm, key, listFromItems, unify, variables,
11
12
  } from './terms.js';
12
13
  import { Lettering, write } from './writer.js';
13
14
 
@@ -210,6 +211,128 @@ function groundedListings(program, listings, loose) {
210
211
  return true;
211
212
  }
212
213
 
214
+ // Whether each fact of these listings is the conclusion of a firing that a
215
+ // search makes, replaying in source order the derivation its steps record.
216
+ // Primitives run on the bindings of that point of the search, so a test of
217
+ // whether a variable is bound sees what the search saw. A relation call
218
+ // follows its use's own step, which C2 keeps acyclic; a listed use is
219
+ // replayed in turn. once/1, not/1 and findall/3 are decided by the call
220
+ // tables, through Settler.answers, which refuses an order resting on a
221
+ // listing. A use is compared with what its goal can still become, since steps
222
+ // show goals after the whole solution's bindings. stepOf(goal) gives the
223
+ // recorded justification and uses of a goal, or nothing.
224
+ export function replayed(program, settler, listingKeys, stepOf) {
225
+ const confirmed = new Map();
226
+ const active = new Set();
227
+ let serial = 0;
228
+ const fresh = (term) => freshTerm(term, `replay${++serial}`);
229
+ const supportedRule = (clause) => clause && clause.parentId === null && !clause.templates.length &&
230
+ clause.captures === null && clause.guard === null;
231
+ function compatible(goal, use, env) {
232
+ const mark = env.mark();
233
+ const agrees = unify(goal, fresh(use), env);
234
+ env.undo(mark);
235
+ return agrees;
236
+ }
237
+ function all(goals, uses, env) {
238
+ return goals.length === uses.length && goals.every((goal, i) => one(goal, uses[i], env));
239
+ }
240
+ function one(goal, use, env) {
241
+ goal = deref(goal, env);
242
+ if (!isCallable(goal) || !isCallable(use)) return false;
243
+ const goalKey = key(goal);
244
+ if (goalKey !== key(use)) return false;
245
+ if (goalKey === 'call/1' || goalKey === 'or/2') {
246
+ const step = stepOf(use);
247
+ if (!step || step.by !== 'control') return false;
248
+ const used = step.uses.map((item) => text(item));
249
+ const sides = goalKey === 'call/1' ? [0] : [0, 1];
250
+ const side = sides.find((i) => {
251
+ const parts = flattenConjunction(use.args[i]);
252
+ return parts.length === used.length && parts.every((part, j) => text(part) === used[j]);
253
+ });
254
+ return side !== undefined && all(flattenConjunction(goal.args[side]), step.uses, env);
255
+ }
256
+ if (['once/1', 'not/1', 'findall/3'].includes(goalKey)) {
257
+ const outcome = settler.answers(copyResolved(goal, env));
258
+ if (outcome.status !== 'settled' || !outcome.answers.length) return false;
259
+ return unify(goal, fresh(outcome.answers[0]), env) && compatible(goal, use, env);
260
+ }
261
+ if (PRIMITIVE_KEYS.has(goalKey)) {
262
+ // A comparison of variables orders them by name, which renaming changes.
263
+ if (goalKey === 'compare/3' && (!isGround(goal.args[1], env) || !isGround(goal.args[2], env))) return false;
264
+ try {
265
+ for (const _ of primitive(goal, env)) { // eslint-disable-line no-unused-vars
266
+ if (compatible(goal, use, env)) return true;
267
+ }
268
+ } catch (error) {
269
+ if (!(error instanceof EyemindError)) throw error;
270
+ }
271
+ return false;
272
+ }
273
+ if (INSPECTION_KEYS.has(goalKey)) return false;
274
+ const step = stepOf(use);
275
+ if (!step) {
276
+ // A use without a step is an instance of a source fact.
277
+ for (const clause of program.groups.get(goalKey) || []) {
278
+ if (clause.body.length || clause.forward || clause.parentId !== null) continue;
279
+ const mark = env.mark();
280
+ const [head] = freshClause(clause, `replay${++serial}`);
281
+ if (unify(goal, head, env) && compatible(goal, use, env)) return true;
282
+ env.undo(mark);
283
+ }
284
+ return false;
285
+ }
286
+ if (!isTerm(step.by, 'clause', 1)) return false;
287
+ const clause = program.byId.get(Number(step.by.args[0]));
288
+ if (!supportedRule(clause)) return false;
289
+ if (clause.forward) return fact(use) && unify(goal, fresh(use), env);
290
+ const [head, body] = freshClause(clause, `replay${++serial}`);
291
+ if (!body.length) return unify(goal, head, env) && compatible(goal, use, env);
292
+ const call = `${text(use)} ${text(copyResolved(goal, env))}`;
293
+ if (active.has(call)) return false;
294
+ active.add(call);
295
+ const solved = unify(goal, head, env) && all(body, step.uses, env) && compatible(goal, use, env);
296
+ active.delete(call);
297
+ return solved;
298
+ }
299
+ // A listed fact: a firing of the forward rule its step cites.
300
+ function fact(goal) {
301
+ const id = text(goal);
302
+ if (confirmed.has(id)) return confirmed.get(id);
303
+ if (active.has(id)) return false;
304
+ const step = stepOf(goal);
305
+ const clause = step && isTerm(step.by, 'clause', 1) ? program.byId.get(Number(step.by.args[0])) : null;
306
+ let holds = false;
307
+ if (supportedRule(clause) && clause.forward) {
308
+ active.add(id);
309
+ const [head, body, names] = freshClause(clause, `replay${++serial}`);
310
+ const env = new Env();
311
+ holds = all(body, step.uses, env);
312
+ if (holds && clause.witnessNames.length) {
313
+ // Each witness is the Skolem term of this firing (Section 8.2).
314
+ const witnesses = new Set(clause.witnessNames.map((name) => names.get(name).name));
315
+ const values = [];
316
+ for (const variable of variables(head).values()) {
317
+ if (witnesses.has(variable.name)) continue;
318
+ const value = copyResolved(variable, env);
319
+ if (!isGround(value)) holds = false;
320
+ values.push(value);
321
+ }
322
+ for (const name of clause.witnessNames) {
323
+ unify(names.get(name), new Struct('skolem', [BigInt(clause.id), name, listFromItems(values)]), env);
324
+ }
325
+ }
326
+ holds = holds && flattenConjunction(head).some((item) => isCallable(item) &&
327
+ identity(copyResolved(item, env)) === identity(goal));
328
+ active.delete(id);
329
+ }
330
+ confirmed.set(id, holds);
331
+ return holds;
332
+ }
333
+ return listingKeys.every((goalKey) => (settler.listings.get(goalKey)?.facts || []).every(fact));
334
+ }
335
+
213
336
  // Both directions of C10: each claim is an answer of an original question,
214
337
  // and each answer of a certified question is claimed. When the conclusions
215
338
  // are the derived facts, a question's answers that are not derived (source
@@ -223,9 +346,13 @@ export function checkCalls(program, claims, goals, certificate, steps, spellings
223
346
  supported: (fact) => steps.has(spellings.spell(fact)),
224
347
  }, true);
225
348
  if (claims.includes('false')) return { settler, outcomes: [], unresolved: [], covered: 0 };
349
+ const stepOf = (goal) => steps.get(spellings.spell(goal));
350
+ // Replay runs host math exactly, so it needs no exclusion for tolerance.
351
+ const supported = (listings) => (!(inexact && listings.length) && positiveListings(program, listings)) ||
352
+ replayed(program, settler, listings, stepOf);
226
353
  const outcomes = callQuestions(program, goals, claims).map((question) => {
227
354
  const outcome = settler.answers(question);
228
- if (outcome.status === 'settled' && ((inexact && outcome.listings.length) || !positiveListings(program, outcome.listings))) {
355
+ if (outcome.status === 'settled' && !supported(outcome.listings)) {
229
356
  return { question, ...outcome, status: 'unsupported' };
230
357
  }
231
358
  return { question, ...outcome };
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.5.22';
2
+ export const VERSION = '0.5.23';