eyemind 0.9.8 → 0.9.10

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/LE-SPEC.md CHANGED
@@ -13,6 +13,11 @@ extension: the meaning of the compiled program is defined by the
13
13
  document says how eyemind reads LE and what it compiles each part to. Where the
14
14
  two LE implementations differ, it says which reading eyemind follows.
15
15
 
16
+ The [Logical English conformance suite](https://eyereasoner.github.io/eyemind/le-conformance/)
17
+ has cases for each section of this reference. Run them with
18
+ `node le-conformance/run.js` or in the
19
+ [LE browser runner](https://eyereasoner.github.io/eyemind/playground/conformance?suite=le).
20
+
16
21
  ## Documents
17
22
 
18
23
  A document is a sequence of sections. Each section starts with a header that
@@ -510,7 +515,8 @@ through its template, with the values of that solution:
510
515
  - an aggregate gives its value, its condition and the values it collected, after
511
516
  `namely`;
512
517
  - a comparison of dates is `is before`, `is after` or `is the same day as`, a
513
- date count `is N days after`, and arithmetic the formula with its values;
518
+ date count `is N days after`, and arithmetic the formula with its values and
519
+ parentheses to preserve the order of operations;
514
520
  - a sentence a relation states at another level of an unfolding, or the base
515
521
  relation of an `only if` rule, gives its own conditions instead of
516
522
  repeating the answer;
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.8
5
+ Version: eyemind 0.9.10
6
6
  ```
7
7
 
8
8
  This document specifies Logic Script (LS): the syntax and meaning of its
package/README.md CHANGED
@@ -17,9 +17,12 @@ program, a JavaScript library and a browser application.
17
17
  the two streamed examples also save proofs and check reports.
18
18
  - [Specification](https://eyereasoner.github.io/eyemind/LS-SPEC): the
19
19
  language, the proof format and how a proof is checked.
20
- - [Conformance](https://eyereasoner.github.io/eyemind/conformance/): the
21
- 1247 cases that test language and proof behavior, also run
22
- [in your browser](https://eyereasoner.github.io/eyemind/playground/conformance).
20
+ - [Logic Script conformance](https://eyereasoner.github.io/eyemind/conformance/)
21
+ has 1247 Logic Script cases, including proof checking.
22
+ [Run LS in your browser](https://eyereasoner.github.io/eyemind/playground/conformance).
23
+ - [Logical English conformance](https://eyereasoner.github.io/eyemind/le-conformance/)
24
+ has 599 Logical English cases for the LE reference.
25
+ [Run LE in your browser](https://eyereasoner.github.io/eyemind/playground/conformance?suite=le).
23
26
  - [Book](https://eyereasoner.github.io/eyemind/BOOK): The Art of eyemind, an
24
27
  illustrated introduction for a wide audience, and
25
28
  [The Art of Logical English](https://eyereasoner.github.io/eyemind/LE-BOOK),
@@ -807,7 +810,8 @@ npm test -- -k skolem # only tests whose line co
807
810
  EYEMIND_PROPERTY_SCALE=20 npm test -- -k properties # deeper generated tests
808
811
  node tools/update-examples.js # regenerate example artifacts after an intended change
809
812
  npm run sync # refresh the cached upstream test suites and the QUDT catalog
810
- node conformance/run.js --command "my-eyemind" # run the conformance suite against another implementation
813
+ node conformance/run.js --command "my-eyemind" # test a Logic Script implementation
814
+ node le-conformance/run.js --command "my-le" # test a Logical English implementation
811
815
  npm run bench # the RDF reasoning benchmark; see bench/README.md
812
816
  ./release patch "Describe the changes" # test, tag and publish a release
813
817
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eyemind",
3
- "version": "0.9.8",
3
+ "version": "0.9.10",
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/le-explain.js CHANGED
@@ -20,7 +20,6 @@ const SILENT = new Set(['le:scenario/1', 'is_var/1', 'is_nonvar/1', 'true/0']);
20
20
  const AGGREGATE_WORDS = { 'le:sum': 'sum', 'le:count': 'count', 'le:average': 'average', 'le:min': 'min', 'le:max': 'max' };
21
21
  const OPERATORS = { add: '+', sub: '-', mul: '*', div: '/', floordiv: '//', mod: 'mod' };
22
22
  const PRECEDENCE = { add: 1, sub: 1, mul: 2, div: 2, floordiv: 2, mod: 2 };
23
- const ASSOCIATIVE = new Set(['add', 'mul']);
24
23
  const COMPARISONS = { lt: '<', gt: '>', le: '=<', ge: '>=', eq: '=', ne: '!=' };
25
24
 
26
25
  // The explanations of every answer of a Logical English run, as LE text: under
@@ -263,22 +262,19 @@ class Writer {
263
262
  return /^[aeiou]/.test(words) ? `an ${words}` : `a ${words}`;
264
263
  }
265
264
 
266
- value(term, noun = null) {
265
+ value(term, noun = null, minPrecedence = 0) {
267
266
  // A value a helper computed is written as the formula it came from.
268
- if (term instanceof Var && typeof this.pending.get(term) === 'string') return this.pending.get(term);
269
- term = deref(term, this.env);
270
- if (term instanceof Var) return this.pending.get(term) ?? this.phrase(term, noun);
267
+ const pending = term instanceof Var ? this.pending.get(term) : null;
268
+ term = pending instanceof Struct ? pending : deref(term, this.env);
269
+ if (term instanceof Var) return this.phrase(term, noun);
271
270
  if (term instanceof Struct && OPERATORS[term.name] && term.args.length === 2) {
272
- // Brackets only where precedence needs them: around a lower operator,
273
- // or an equal one on the right of a subtraction or division.
274
- const side = (arg, right) => {
275
- const text = this.value(arg);
276
- const inner = deref(arg, this.env);
277
- if (!(inner instanceof Struct && OPERATORS[inner.name] && inner.args.length === 2)) return text;
278
- const [outer, own] = [PRECEDENCE[term.name], PRECEDENCE[inner.name]];
279
- return own < outer || (right && own === outer && !ASSOCIATIVE.has(term.name)) ? `(${text})` : text;
280
- };
281
- return `${side(term.args[0], false)} ${OPERATORS[term.name]} ${side(term.args[1], true)}`;
271
+ // LE reads operators of equal precedence from left to right. Keep a
272
+ // grouped right operand even for + and *, since floats can round.
273
+ const precedence = PRECEDENCE[term.name];
274
+ const left = this.value(term.args[0], null, precedence);
275
+ const right = this.value(term.args[1], null, precedence + 1);
276
+ const text = `${left} ${OPERATORS[term.name]} ${right}`;
277
+ return precedence < minPrecedence ? `(${text})` : text;
282
278
  }
283
279
  if (term instanceof Struct && (term.name === 'min' || term.name === 'max') && term.args.length === 2) {
284
280
  return `the ${term.name === 'min' ? 'minimum' : 'maximum'} of ${this.value(term.args[0])} and ${this.value(term.args[1])}`;
@@ -328,8 +324,7 @@ class Writer {
328
324
  const each = named === undefined ? null : ID.test(named) ? named : named.replace(/([a-z0-9])([A-Z])/g, '$1 $2').toLowerCase();
329
325
  if (item instanceof Var) this.seen.add(item);
330
326
  const such = `each ${each ?? this.value(item)} such that ${this.conjunction(args[1])}`;
331
- const items = this.value(list);
332
- this.pending.set(args[2] instanceof Var ? args[2] : list, { such, items });
327
+ this.pending.set(args[2] instanceof Var ? args[2] : list, { such });
333
328
  return null;
334
329
  }
335
330
  if (AGGREGATE_WORDS[name] && args.length === 2) {
@@ -340,12 +335,12 @@ class Writer {
340
335
  }
341
336
  if (name === 'le:divide' && args.length === 3) {
342
337
  // The quotient is written where the formula that divides uses it.
343
- const formula = `${this.value(args[0])} / ${this.value(args[1])}`;
338
+ const formula = new Struct('div', args.slice(0, 2));
344
339
  if (args[2] instanceof Var) {
345
340
  this.pending.set(args[2], formula);
346
341
  return null;
347
342
  }
348
- return `${this.value(args[2])} = ${formula}`;
343
+ return `${this.value(args[2])} = ${this.value(formula)}`;
349
344
  }
350
345
  if (name === 'le:days_after' && args.length === 3) return `${this.value(args[0])} is ${this.value(args[1])} days after ${this.value(args[2])}`;
351
346
  if (name === 'is' && args.length === 2) {
@@ -380,7 +375,7 @@ class Writer {
380
375
  // The items a collection gathered, for the aggregate that takes them.
381
376
  collected(list) {
382
377
  const pending = this.pending.get(list instanceof Var ? list : deref(list, this.env));
383
- if (!pending || typeof pending === 'string') return null;
378
+ if (!pending || pending instanceof Struct) return null;
384
379
  const items = copyResolved(list, this.env);
385
380
  const values = [];
386
381
  let rest = items;
package/src/le.js CHANGED
@@ -192,7 +192,7 @@ function bracketedName(tokens) {
192
192
  const lower = (token) => token.kind === 'word' ? token.text.toLowerCase() : token.text;
193
193
  const isWord = (token, ...words) => token.kind === 'word' && words.includes(token.text.toLowerCase());
194
194
  // Words joined as written: no space before a comma, a closing bracket or 's.
195
- const joined = (words) => words.join(' ').replace(/ ([,:\])]) ?/g, '$1 ').replace(/([[(]) /g, '$1').replace(/ (['’]s)(?= |$)/g, '$1').replace(/ +$/, '');
195
+ const joined = (words) => words.join(' ').replace(/ ([,:\])])/g, '$1').replace(/([[(]) /g, '$1').replace(/ (['’]s)(?= |$)/g, '$1').replace(/ +$/, '');
196
196
  const phrase = (tokens) => joined(tokens.map((t) => t.text));
197
197
  // Whether tokens start with these words, compared without case.
198
198
  const startsWith = (tokens, words, at = 0) => words.every((word, i) => tokens[at + i] && lower(tokens[at + i]) === word);
@@ -2209,8 +2209,12 @@ export function writeLe(result, loaded) {
2209
2209
  // on; an answer that also holds without any is definite.
2210
2210
  const found = new Map();
2211
2211
  // A negative assumption reads "it is not the case that S".
2212
- const unknown = (term) => term instanceof Struct && term.name === 'le:not' && term.args.length === 1 ? `it is not the case that ${unknown(term.args[0])}`
2213
- : leValue(threaded && term instanceof Struct ? new Struct(term.name, term.args.slice(1)) : term, templates);
2212
+ const unknown = (term) => {
2213
+ if (term instanceof Struct && term.name === 'le:not' && term.args.length === 1) return `it is not the case that ${unknown(term.args[0])}`;
2214
+ // Without a scenario argument a sentence with no places is an atom.
2215
+ if (typeof term === 'string') return leValue(new Struct(term, []), templates);
2216
+ return leValue(threaded && term instanceof Struct ? new Struct(term.name, term.args.slice(1)) : term, templates);
2217
+ };
2214
2218
  for (const answer of result.answers) {
2215
2219
  const term = readTerm(answer);
2216
2220
  if (!(term instanceof Struct) || term.name !== ANSWER) continue;
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.9.8';
2
+ export const VERSION = '0.9.10';