eyemind 0.9.16 → 0.9.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LS-SPEC.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ```text
4
4
  Title: The Logic Script (LS) Language and Proof Format
5
- Version: eyemind 0.9.16
5
+ Version: eyemind 0.9.18
6
6
  ```
7
7
 
8
8
  This document specifies Logic Script (LS): the syntax and meaning of its
package/README.md CHANGED
@@ -331,6 +331,11 @@ ordered envelopes in the `eymsg:` vocabulary, with proofs;
331
331
  standard input or HTTP(S) incrementally. See the
332
332
  [message-boundaries](https://eyereasoner.github.io/eyemind/examples/deck/message-boundaries-n3)
333
333
  and [message-alerts](https://eyereasoner.github.io/eyemind/examples/deck/message-alerts-n3) decks.
334
+ Every `npm test` scores the
335
+ [RDF Messages tests](https://w3c-cg.github.io/rsp/spec/messages-tests) as
336
+ coverage: all of the Turtle, TriG, N-Triples and N-Quads cases pass, a wrong
337
+ result fails the run, and the NDJSON-LD, RDF/XML and serialization cases are
338
+ counted as unsupported.
334
339
 
335
340
  With an [RDF logic profile](#rdf-logic-profiles), `--stream-messages` treats
336
341
  the other inputs as a background graph and writes, for each message, the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eyemind",
3
- "version": "0.9.16",
3
+ "version": "0.9.18",
4
4
  "description": "A dependency-free Logic Script (LS) reasoner with forward and backward reasoning, checkable proofs, and RDF 1.2, Notation3 (N3), SPARQL 1.2 RL (SRL) and Logical English (LE) interoperability.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
package/src/engine.js CHANGED
@@ -861,12 +861,7 @@ export class Solver {
861
861
  if (this.recording || keys === null || keys.length !== 1 || keys[0] === null ||
862
862
  clause.parentId !== null || clause.templates.length || clause.captures !== null || clause.witnessNames.length) return null;
863
863
  const goalKey = keys[0];
864
- let ground = this.groundCalls.get(goalKey);
865
- if (ground === undefined) {
866
- ground = (this.program.groups.get(goalKey) || []).every((candidate) => isGround(candidate.head));
867
- this.groundCalls.set(goalKey, ground);
868
- }
869
- if (!ground) return null;
864
+ if (!this.groundHeads(goalKey)) return null;
870
865
  if (++this.stats.inferences > this.maxInferences) throw new EyemindError('reasoning exceeded max_inferences');
871
866
  const goal = clause.localBody[0];
872
867
  const facts = this.facts.get(goalKey) || NO_NODES;
@@ -903,15 +898,17 @@ export class Solver {
903
898
  joinable(clause, keys) {
904
899
  if (this.recording || keys === null || clause.parentId !== null || clause.templates.length ||
905
900
  clause.captures !== null || this.program.hasTemplates || this.program.inspectionEnabled) return false;
906
- return keys.every((goalKey) => {
907
- if (goalKey === null) return true;
908
- let ground = this.groundCalls.get(goalKey);
909
- if (ground === undefined) {
910
- ground = (this.program.groups.get(goalKey) || []).every((candidate) => isGround(candidate.head));
911
- this.groundCalls.set(goalKey, ground);
912
- }
913
- return ground;
914
- });
901
+ return keys.every((goalKey) => goalKey === null || this.groundHeads(goalKey));
902
+ }
903
+
904
+ // Whether every clause of a relation has a ground head, cached per relation.
905
+ groundHeads(goalKey) {
906
+ let ground = this.groundCalls.get(goalKey);
907
+ if (ground === undefined) {
908
+ ground = (this.program.groups.get(goalKey) || []).every((candidate) => isGround(candidate.head));
909
+ this.groundCalls.set(goalKey, ground);
910
+ }
911
+ return ground;
915
912
  }
916
913
 
917
914
  // The solutions of a joinable body, as solve() finds them: each goal tries
package/src/le.js CHANGED
@@ -13,11 +13,10 @@ import { PRIMITIVE_KEYS } from './builtins.js';
13
13
  import { CONTROL_KEYS, Program, RESERVED_KEYS, Source } from './program.js';
14
14
  import { loadText } from './lang.js';
15
15
  import { readTerm } from './reader.js';
16
- import { EMPTY, EyemindError, Struct, Var, conjunction } from './terms.js';
16
+ import { EMPTY, EyemindError, Struct, Var, conjunction, listFromItems } from './terms.js';
17
17
  import { write } from './writer.js';
18
18
 
19
19
  const st = (name, ...args) => new Struct(name, args);
20
- const list = (items) => items.reduceRight((tail, head) => st('.', head, tail), EMPTY);
21
20
  const RESERVED = new Set([...PRIMITIVE_KEYS, ...CONTROL_KEYS, ...RESERVED_KEYS].map((key) => key.split('/')[0]));
22
21
  const SCENARIO = 'le:scenario', ANSWER = 'le:answer';
23
22
  const INTRODUCING = new Set(['a', 'an', 'some', 'each', 'which']);
@@ -439,7 +438,7 @@ class Compiler {
439
438
  items.push(item);
440
439
  start = i + 1;
441
440
  }
442
- return list(items);
441
+ return listFromItems(items);
443
442
  }
444
443
  // LE2's explicit variable: *a name*.
445
444
  if (first.text === '*' && tokens.at(-1).text === '*' && tokens.length > 2 && tokens.slice(1, -1).every((t) => t.kind === 'word')) {
@@ -1691,6 +1690,31 @@ export function compileLe(inputs, io = null) {
1691
1690
  program: new Program(sources), compiled: { templates, scenarios, threaded, queries, expectations, abductive } };
1692
1691
  }
1693
1692
 
1693
+ // The relation of a goal, as name/arity, or null for a variable.
1694
+ const keyOf = (term) => term instanceof Struct ? `${term.name}/${term.args.length}` : null;
1695
+
1696
+ // Tarjan's strongly connected components of a graph, given its nodes and the
1697
+ // nodes each one leads to: each node maps to the members of its component.
1698
+ function components(nodes, next) {
1699
+ const index = new Map(), low = new Map(), stack = [], onStack = new Set(), component = new Map();
1700
+ let counter = 0;
1701
+ const visit = (v) => {
1702
+ index.set(v, counter); low.set(v, counter++); stack.push(v); onStack.add(v);
1703
+ for (const w of next(v)) {
1704
+ if (!index.has(w)) { visit(w); low.set(v, Math.min(low.get(v), low.get(w))); }
1705
+ else if (onStack.has(w)) low.set(v, Math.min(low.get(v), index.get(w)));
1706
+ }
1707
+ if (low.get(v) === index.get(v)) {
1708
+ const members = [];
1709
+ let w;
1710
+ do { w = stack.pop(); onStack.delete(w); members.push(w); } while (w !== v);
1711
+ for (const m of members) component.set(m, members);
1712
+ }
1713
+ };
1714
+ for (const v of nodes) if (!index.has(v)) visit(v);
1715
+ return component;
1716
+ }
1717
+
1694
1718
  // LE runs recursive rules with tabling (LE1) or a loop check (LE2), so a rule
1695
1719
  // that depends on itself, directly or through others, ends where Prolog's
1696
1720
  // search would not. LS forward rules reach the least fixpoint that tabling
@@ -1700,7 +1724,6 @@ export function compileLe(inputs, io = null) {
1700
1724
  // called, such as a counter that goes down, stays backward.
1701
1725
  function forwardRecursion(pending, templates, threaded, assumable = new Set()) {
1702
1726
  const keys = new Set([...templates.map((t) => `${t.name}/${t.arity + (threaded ? 1 : 0)}`), `is_a/${threaded ? 3 : 2}`]);
1703
- const keyOf = (term) => term instanceof Struct ? `${term.name}/${term.args.length}` : null;
1704
1727
  const calls = (term, out = new Set()) => {
1705
1728
  if (term instanceof Struct) {
1706
1729
  if (keys.has(keyOf(term))) out.add(keyOf(term));
@@ -1715,23 +1738,7 @@ function forwardRecursion(pending, templates, threaded, assumable = new Set()) {
1715
1738
  if (!edges.has(from)) edges.set(from, new Set());
1716
1739
  for (const goal of rule.body) for (const to of calls(goal)) edges.get(from).add(to);
1717
1740
  }
1718
- // Tarjan's strongly connected components of the relations.
1719
- const index = new Map(), low = new Map(), stack = [], onStack = new Set(), component = new Map();
1720
- let counter = 0;
1721
- const visit = (v) => {
1722
- index.set(v, counter); low.set(v, counter++); stack.push(v); onStack.add(v);
1723
- for (const w of edges.get(v) ?? []) {
1724
- if (!index.has(w)) { visit(w); low.set(v, Math.min(low.get(v), low.get(w))); }
1725
- else if (onStack.has(w)) low.set(v, Math.min(low.get(v), index.get(w)));
1726
- }
1727
- if (low.get(v) === index.get(v)) {
1728
- const members = [];
1729
- let w;
1730
- do { w = stack.pop(); onStack.delete(w); members.push(w); } while (w !== v);
1731
- for (const m of members) component.set(m, members);
1732
- }
1733
- };
1734
- for (const v of edges.keys()) if (!index.has(v)) visit(v);
1741
+ const component = components(edges.keys(), (v) => edges.get(v) ?? []);
1735
1742
  const recursive = (key) => component.get(key)?.length > 1 || edges.get(key)?.has(key);
1736
1743
  const bound = (rule) => {
1737
1744
  const relations = new Set();
@@ -1753,7 +1760,6 @@ function forwardRecursion(pending, templates, threaded, assumable = new Set()) {
1753
1760
  // at most three changes that makes the goal hold.
1754
1761
  const FLIP_LIMIT = 3;
1755
1762
  function flipWorlds(pending, { templates, flips, scenarios, helpers, emit }) {
1756
- const keyOf = (term) => term instanceof Struct ? `${term.name}/${term.args.length}` : null;
1757
1763
  const concluded = new Set(pending.filter((c) => c.guard?.rule).map((c) => keyOf(c.head)));
1758
1764
  const marked = templates.filter((t) => t.scenarioElement);
1759
1765
  const changeable = (marked.length ? marked : templates.filter((t) => !t.function && !concluded.has(`${t.name}/${t.arity + 1}`)))
@@ -1803,11 +1809,11 @@ function flipWorlds(pending, { templates, flips, scenarios, helpers, emit }) {
1803
1809
  if (!facts.some((f) => write(f) === write(sentence))) candidates.push(st('add', sentence));
1804
1810
  }
1805
1811
  }
1806
- emit('fact', st('le:candidates', scenario, list(candidates)), [], null, null);
1812
+ emit('fact', st('le:candidates', scenario, listFromItems(candidates)), [], null, null);
1807
1813
  const subsets = (from, k) => k === 0 ? [[]] : candidates.slice(from).flatMap((c, i) => subsets(from + i + 1, k - 1).map((rest) => [c, ...rest]));
1808
1814
  for (let k = 0; k <= FLIP_LIMIT; k++) {
1809
1815
  for (const changes of subsets(0, k)) {
1810
- const world = st('le:flip', scenario, list(changes));
1816
+ const world = st('le:flip', scenario, listFromItems(changes));
1811
1817
  const removed = new Set(changes.filter((c) => c.name === 'remove').map((c) => write(c.args[0])));
1812
1818
  const worldFacts = [...facts.filter((f) => !removed.has(write(f))), ...changes.filter((c) => c.name === 'add').map((c) => c.args[0])];
1813
1819
  size += worldFacts.length + 1;
@@ -1819,7 +1825,7 @@ function flipWorlds(pending, { templates, flips, scenarios, helpers, emit }) {
1819
1825
  }
1820
1826
  helpers.add('le:choose');
1821
1827
  helpers.add('le:member');
1822
- const sizes = list(Array.from({ length: FLIP_LIMIT + 1 }, (_, k) => BigInt(k)));
1828
+ const sizes = listFromItems(Array.from({ length: FLIP_LIMIT + 1 }, (_, k) => BigInt(k)));
1823
1829
  for (const { name, goals, S: world, changes, line, file } of flips) {
1824
1830
  const size = new Var('K_'), candidates = new Var('Candidates_');
1825
1831
  emit('backward', st('le:flips', name, world, size, changes),
@@ -1840,7 +1846,6 @@ function flipWorlds(pending, { templates, flips, scenarios, helpers, emit }) {
1840
1846
  // NAME:sc, with the scope as one more argument; in it, a scenario fact holds
1841
1847
  // when its source is admissible under the scope.
1842
1848
  function scopeProofs(pending, { templates, threaded, emit }) {
1843
- const keyOf = (term) => term instanceof Struct ? `${term.name}/${term.args.length}` : null;
1844
1849
  const user = new Set([...templates.map((t) => `${t.name}/${t.arity + (threaded ? 1 : 0)}`), `is_a/${threaded ? 3 : 2}`, `${ADMISSIBLE}/${threaded ? 3 : 2}`]);
1845
1850
  const placeholders = [];
1846
1851
  const find = (term) => {
@@ -1900,7 +1905,6 @@ function scopeProofs(pending, { templates, threaded, emit }) {
1900
1905
  const DEPTH = 8;
1901
1906
  function unfold(pending, templates, threaded) {
1902
1907
  const keys = new Set([...templates.map((t) => `${t.name}/${t.arity + (threaded ? 1 : 0)}`), `is_a/${threaded ? 3 : 2}`, `le_is/${threaded ? 3 : 2}`]);
1903
- const keyOf = (term) => term instanceof Struct ? `${term.name}/${term.args.length}` : null;
1904
1908
  const closedControl = (term) => term instanceof Struct && ((term.name === 'not' && term.args.length === 1) || ((term.name === 'findall' || term.name === 'findall_sorted') && term.args.length === 3));
1905
1909
  const edges = new Map();
1906
1910
  const visitCalls = (term, from, closed) => {
@@ -1914,22 +1918,7 @@ function unfold(pending, templates, threaded) {
1914
1918
  for (const goal of rule.body ?? []) visitCalls(goal, keyOf(rule.head), false);
1915
1919
  }
1916
1920
  // The strongly connected components, and those with a closed edge inside.
1917
- const index = new Map(), low = new Map(), stack = [], onStack = new Set(), component = new Map();
1918
- let counter = 0;
1919
- const visit = (v) => {
1920
- index.set(v, counter); low.set(v, counter++); stack.push(v); onStack.add(v);
1921
- for (const [w] of edges.get(v) ?? []) {
1922
- if (!index.has(w)) { visit(w); low.set(v, Math.min(low.get(v), low.get(w))); }
1923
- else if (onStack.has(w)) low.set(v, Math.min(low.get(v), index.get(w)));
1924
- }
1925
- if (low.get(v) === index.get(v)) {
1926
- const members = [];
1927
- let w;
1928
- do { w = stack.pop(); onStack.delete(w); members.push(w); } while (w !== v);
1929
- for (const m of members) component.set(m, members);
1930
- }
1931
- };
1932
- for (const v of edges.keys()) if (!index.has(v)) visit(v);
1921
+ const component = components(edges.keys(), (v) => (edges.get(v) ?? []).map(([w]) => w));
1933
1922
  const unfolded = new Set();
1934
1923
  for (const [from, list] of edges) {
1935
1924
  for (const [to, closed] of list) if (closed && component.get(from)?.includes(to)) for (const m of component.get(from)) unfolded.add(m);
@@ -1974,7 +1963,6 @@ function unfold(pending, templates, threaded) {
1974
1963
  // assumes a sentence and its negation, denies a proved sentence, or breaks a
1975
1964
  // constraint that more assumptions cannot repair.
1976
1965
  function abduce(pending, { templates, threaded, assumable, abducibles, constraints, denials, helpers, emit }) {
1977
- const keyOf = (term) => term instanceof Struct ? `${term.name}/${term.args.length}` : null;
1978
1966
  const user = new Set([...templates.map((t) => `${t.name}/${t.arity + (threaded ? 1 : 0)}`), `is_a/${threaded ? 3 : 2}`,
1979
1967
  `le_is/${threaded ? 3 : 2}`, ...assumable]);
1980
1968
  const calls = (term, out = new Set()) => {
@@ -2063,7 +2051,7 @@ function abduce(pending, { templates, threaded, assumable, abducibles, constrain
2063
2051
  const assume = (sentence, line, file) => {
2064
2052
  const open = st('findall', 'x', st('is_ground', sentence), EMPTY);
2065
2053
  const closed = conjunction([st('is_ground', sentence), st('not', sentence), st('not', st('le:denied', sentence))]);
2066
- emit('backward', new Struct(`${sentence.name}:ab`, [...sentence.args, list([sentence])]), [st('or', closed, open)], line, file);
2054
+ emit('backward', new Struct(`${sentence.name}:ab`, [...sentence.args, listFromItems([sentence])]), [st('or', closed, open)], line, file);
2067
2055
  };
2068
2056
  for (const template of templates.filter((t) => t.assumable)) {
2069
2057
  const args = Array.from({ length: template.arity + (threaded ? 1 : 0) }, (_, i) => new Var(i === 0 && threaded ? 'Scenario' : `A${i}`));
package/src/messages.js CHANGED
@@ -6,7 +6,7 @@ import { readSources } from './n3.js';
6
6
  import { tripleToEyemind } from './n3-writer.js';
7
7
  import { EyemindError } from './terms.js';
8
8
 
9
- export const MESSAGE_NS = 'https://eyereasoner.github.io/eyeling/vocab/message#';
9
+ export const MESSAGE_NS = 'https://eyereasoner.github.io/eyemind/vocab/message#';
10
10
  const RDF_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
11
11
  const LOG_NAME = 'http://www.w3.org/2000/10/swap/log#nameOf';
12
12
  const GRAPH_CONTENT = 'https://eyereasoner.github.io/eyemind/graph#content';
package/src/n3-writer.js CHANGED
@@ -60,10 +60,6 @@ export function writeN3(triples, { prefixes = new Map(), strings = 'auto', blank
60
60
  return [...head, ...body].map((line) => `${line}\n`).join('');
61
61
  }
62
62
 
63
- export function eyemindToN3(text, options = {}) {
64
- return writeN3(triplesFromEyemind(text).triples, options);
65
- }
66
-
67
63
  // A parsed N3 term as the eyemind term n3-to-eyemind writes for it.
68
64
  export function termToEyemind(t) {
69
65
  const st = (name, ...args) => new Struct(name, args);