eyemind 0.5.26 → 0.5.28

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/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.26
5
+ Version: eyemind 0.5.28
6
6
  ```
7
7
 
8
8
  This document specifies the syntax and meaning of eyemind programs and data
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eyemind",
3
- "version": "0.5.26",
3
+ "version": "0.5.28",
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/closure.js CHANGED
@@ -16,7 +16,7 @@
16
16
  // a once/1 whose answers rest on a listing stays an obligation, and so does a
17
17
  // boundary whose search reaches rule templates or inspection.
18
18
  import { PRIMITIVE_KEYS, primitive } from './builtins.js';
19
- import { freshClause, text, variant } from './common.js';
19
+ import { freshClause, sameVariant, text, variant } from './common.js';
20
20
  import { INSPECTION_KEYS } from './inspection.js';
21
21
  import {
22
22
  Env, EyemindError, Struct, Var, copyResolved, deref, flattenConjunction, freshTerm, identity, isCallable, isGround, isTerm, key,
@@ -352,7 +352,7 @@ export class Settler {
352
352
  for (const _ of this.expand(call, env, entry.reliance)) found.push(copyResolved(call, env)); // eslint-disable-line no-unused-vars
353
353
  const listed = entry.answers;
354
354
  let i = 0;
355
- while (i < found.length && i < listed.length && this.variant(found[i]) === this.variant(listed[i])) i++;
355
+ while (i < found.length && i < listed.length && sameVariant(found[i], listed[i], this.operational)) i++;
356
356
  if (i < found.length && i < listed.length) {
357
357
  entry.failure = `the search of ${text(entry.call)} answers ${text(found[i])} where its table lists ${text(listed[i])}`;
358
358
  } else if (i < found.length) {
package/src/common.js CHANGED
@@ -183,16 +183,62 @@ function build(term, values, fresh) {
183
183
  return new Struct(term.name, term.args.map((arg) => build(arg, values, fresh)));
184
184
  }
185
185
 
186
- // A key that preserves sharing while forgetting variable names.
186
+ // Whether two terms are variants of each other, as their variant keys would
187
+ // say, compared directly: one consistent renaming in each direction.
188
+ export function sameVariant(left, right, anonymous = false) {
189
+ const forward = new Map();
190
+ const backward = new Map();
191
+ const pending = [left, right];
192
+ while (pending.length) {
193
+ const b = pending.pop();
194
+ const a = pending.pop();
195
+ if (a instanceof Var || b instanceof Var) {
196
+ if (!(a instanceof Var && b instanceof Var)) return false;
197
+ if (anonymous && isAnonymous(a) !== isAnonymous(b)) return false;
198
+ const mapped = forward.get(a.name);
199
+ if (mapped === undefined) {
200
+ if (backward.has(b.name)) return false;
201
+ forward.set(a.name, b.name);
202
+ backward.set(b.name, a.name);
203
+ } else if (mapped !== b.name) {
204
+ return false;
205
+ }
206
+ } else if (a instanceof Struct || b instanceof Struct) {
207
+ if (!(a instanceof Struct && b instanceof Struct) || a.name !== b.name || a.args.length !== b.args.length) return false;
208
+ for (let i = a.args.length - 1; i >= 0; i--) pending.push(a.args[i], b.args[i]);
209
+ } else if (typeof a !== typeof b || !Object.is(a, b)) {
210
+ return false;
211
+ }
212
+ }
213
+ return true;
214
+ }
215
+
216
+ // A key that preserves sharing while forgetting variable names: variables
217
+ // are numbered in order of first occurrence, anonymous ones apart when asked.
218
+ // It encodes the term as identity does, without writing canonical text.
187
219
  export function variant(term, anonymous = false) {
188
220
  const names = new Map();
189
- function visit(t) {
190
- if (t instanceof Var) {
191
- if (!names.has(t.name)) names.set(t.name, new Var(`${anonymous && isAnonymous(t) ? '_' : 'V'}${names.size}`));
192
- return names.get(t.name);
221
+ const out = [];
222
+ const pending = [term];
223
+ while (pending.length) {
224
+ const item = pending.pop();
225
+ if (item instanceof Var) {
226
+ let name = names.get(item.name);
227
+ if (name === undefined) {
228
+ name = `${anonymous && isAnonymous(item) ? '_' : 'V'}${names.size}`;
229
+ names.set(item.name, name);
230
+ }
231
+ out.push('\u0000v', name);
232
+ } else if (item instanceof Struct) {
233
+ out.push('\u0000c', String(item.name.length), item.name, String(item.args.length));
234
+ for (let i = item.args.length - 1; i >= 0; i--) pending.push(item.args[i]);
235
+ } else if (typeof item === 'string') {
236
+ out.push('\u0000a', String(item.length), item);
237
+ } else if (typeof item === 'bigint') {
238
+ out.push('\u0000i', String(item));
239
+ } else {
240
+ out.push('\u0000f', Object.is(item, -0) ? '-0' : String(item));
193
241
  }
194
- if (t instanceof Struct) return new Struct(t.name, t.args.map(visit));
195
- return t;
196
242
  }
197
- return text(visit(term));
243
+ return out.join('\u0001');
198
244
  }
package/src/fidelity.js CHANGED
@@ -4,7 +4,7 @@
4
4
  import { PRIMITIVE_KEYS, primitive } from './builtins.js';
5
5
  import { Settler, plainGenerators } from './closure.js';
6
6
  import { stepIdOf } from './proof.js';
7
- import { freshClause, text, variant } from './common.js';
7
+ import { freshClause, sameVariant, text, variant } from './common.js';
8
8
  import { goalTerm } from './goals.js';
9
9
  import { INSPECTION_KEYS } from './inspection.js';
10
10
  import {
@@ -385,7 +385,7 @@ export function checkCalls(program, claims, goals, certificate, steps, spellings
385
385
  let detail = null;
386
386
  if (matching.some(({ status }) => status === 'failed')) detail = 'the original call has an invalid search certificate';
387
387
  else if (!matching.some(({ status, answers }) => status === 'settled' &&
388
- answers.some((answer) => variant(answer) === variant(claim)))) {
388
+ answers.some((answer) => sameVariant(answer, claim)))) {
389
389
  if (matching.length && matching.every(({ status }) => status === 'settled')) {
390
390
  detail = 'the original call does not return this answer';
391
391
  } else {
@@ -189,18 +189,22 @@ function srlData(document) {
189
189
  if (t.t === 'iri' && t.v === `${RDF}nil`) return { t: 'list', items: [] };
190
190
  if (t.t === 'triple') return { ...t, s: expand(t.s, ancestors), p: expand(t.p, ancestors), o: expand(t.o, ancestors) };
191
191
  if (t.t !== 'bnode' || !cells.has(t.v)) return t;
192
- const path = new Set(ancestors);
192
+ // Keep only the active path: copying all ancestors for each nested list
193
+ // makes a long list of encoded terms quadratic. Siblings may share cells.
194
+ const path = [];
193
195
  const items = [];
194
196
  while (t.t === 'bnode' && cells.has(t.v)) {
195
- if (path.has(t.v)) fail('cyclic collection');
196
- path.add(t.v);
197
+ if (ancestors.has(t.v)) fail('cyclic collection');
198
+ ancestors.add(t.v);
199
+ path.push(t.v);
197
200
  const cell = cells.get(t.v);
198
201
  if (cell.size !== 2) fail('an RDF collection needs first and rest');
199
202
  used.add(t.v);
200
- items.push(expand(cell.get(`${RDF}first`), path));
203
+ items.push(expand(cell.get(`${RDF}first`), ancestors));
201
204
  t = cell.get(`${RDF}rest`);
202
205
  }
203
206
  if (t.t !== 'iri' || t.v !== `${RDF}nil`) fail('an RDF collection must end in rdf:nil');
207
+ for (const id of path) ancestors.delete(id);
204
208
  return { t: 'list', items };
205
209
  };
206
210
  const out = triples.filter((t) => !(t.s.t === 'bnode' && cells.has(t.s.v) && [`${RDF}first`, `${RDF}rest`].includes(t.p.v)))
@@ -246,6 +250,21 @@ function compare(actual, expected) {
246
250
  for (const [key, value] of e) if (!same(a.get(key), value)) fail(`a displayed proof property differs from its record: ${key}`);
247
251
  }
248
252
 
253
+ // Read the graph reconstructed from records with N3's direct collections,
254
+ // avoiding SRL's expansion into first/rest cells and folding them back again.
255
+ // SRL folds every rdf:nil, including an explicit IRI, to ().
256
+ function expectedData(terms, format) {
257
+ const parsed = parseN3(writeN3(graph(terms, format), { strings: false }), { preserveNumericLiterals: true });
258
+ if (format === 'n3') return parsed;
259
+ const fold = (t) => {
260
+ if (t.t === 'iri' && t.v === `${RDF}nil`) return { t: 'list', items: [] };
261
+ if (t.t === 'list') return { ...t, items: t.items.map(fold) };
262
+ if (t.t === 'triple') return { ...t, s: fold(t.s), p: fold(t.p), o: fold(t.o) };
263
+ return t;
264
+ };
265
+ return { triples: parsed.triples.map((t) => ({ s: fold(t.s), p: fold(t.p), o: fold(t.o) })) };
266
+ }
267
+
249
268
  function readNative(document) {
250
269
  let parsed, format;
251
270
  try { parsed = parseN3(document, { preserveNumericLiterals: true }); format = 'n3'; }
@@ -271,7 +290,7 @@ function readNative(document) {
271
290
  if (id.t !== 'iri' || id.v !== `${ROOT}#record${i + 1}`) fail('proof records must be numbered in document order');
272
291
  return decode(property(id.v, `${TERM_NS}term`));
273
292
  });
274
- const expected = format === 'n3' ? parseN3(render(terms, format), { preserveNumericLiterals: true }) : srlData(render(terms, format));
293
+ const expected = expectedData(terms, format);
275
294
  compare(parsed.triples, expected.triples);
276
295
  return terms;
277
296
  }
@@ -284,7 +303,14 @@ export function readNativeProof(document) {
284
303
  }
285
304
  }
286
305
 
306
+ // The command line reads an SRL proof's execution context before checking
307
+ // the same document, so the last native document read is kept.
308
+ let lastNative = null;
309
+
287
310
  export function readProofTerms(document) {
288
- if (/^\s*(?:#|@|PREFIX\b|BASE\b|DATA\b|<|_:)/i.test(document)) return readNativeProof(document).map((term) => [term, 0]);
311
+ if (/^\s*(?:#|@|PREFIX\b|BASE\b|DATA\b|<|_:)/i.test(document)) {
312
+ if (lastNative?.document !== document) lastNative = { document, terms: readNativeProof(document) };
313
+ return lastNative.terms.map((term) => [term, 0]);
314
+ }
289
315
  return readTerms(document);
290
316
  }
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.26';
2
+ export const VERSION = '0.5.28';