eyemind 0.2.2 → 0.2.4
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 +1 -1
- package/SPEC.md +75 -5
- package/package.json +1 -1
- package/src/builtins.js +1 -0
- package/src/engine.js +4 -2
- package/src/proof.js +52 -5
- package/src/reader.js +1 -2
- package/src/version.js +1 -1
- package/src/writer.js +3 -1
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ JavaScript library, and a browser application, with no dependencies.
|
|
|
18
18
|
- **[Examples](https://eyereasoner.github.io/eyemind/examples/)**:
|
|
19
19
|
94 runnable programs, each with a card deck and saved conclusions, proofs
|
|
20
20
|
and check reports.
|
|
21
|
-
- **[Conformance suite](conformance/)**:
|
|
21
|
+
- **[Conformance suite](conformance/)**: 1201 cases that test any
|
|
22
22
|
implementation of the specification through its command line.
|
|
23
23
|
Run the suite [in your browser](https://eyereasoner.github.io/eyemind/playground/conformance).
|
|
24
24
|
|
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.2.
|
|
5
|
+
Version: eyemind 0.2.4
|
|
6
6
|
```
|
|
7
7
|
|
|
8
8
|
This document specifies the syntax and meaning of eyemind programs and data
|
|
@@ -81,6 +81,7 @@ proof.
|
|
|
81
81
|
- [13.1 Invocation and options](#131-invocation-and-options)
|
|
82
82
|
- [13.2 Unused clauses](#132-unused-clauses)
|
|
83
83
|
- [14. Conformance](#14-conformance)
|
|
84
|
+
- [14.1 Conformance test suite](#141-conformance-test-suite)
|
|
84
85
|
- [15. Security](#15-security)
|
|
85
86
|
- [Appendix A. Document Grammar](#appendix-a-document-grammar)
|
|
86
87
|
- [Appendix B. Example](#appendix-b-example)
|
|
@@ -999,8 +1000,8 @@ Object keys may also use these reserved names, since keys denote atom text.
|
|
|
999
1000
|
Otherwise use `e`, an explicit exponent sign and at least two exponent
|
|
1000
1001
|
digits, such as `1e+22` or `1e-05`.
|
|
1001
1002
|
- A variable is written as its name when the name is an ASCII identifier
|
|
1002
|
-
(letters, digits and `_`, not starting with a digit), not a reserved
|
|
1003
|
-
of
|
|
1003
|
+
(letters, digits and `_`, not starting with a digit), not a reserved name
|
|
1004
|
+
of Section 9.1, not `_` and does not begin with `VAR_`. Any other name is
|
|
1004
1005
|
written `VAR_` followed by the name with every ASCII letter and digit kept,
|
|
1005
1006
|
every `_` doubled and every other character `c` replaced by `_hex_` where
|
|
1006
1007
|
`hex` is the lowercase hexadecimal code point of `c`. The variable name `A-B`
|
|
@@ -1011,7 +1012,7 @@ Object keys may also use these reserved names, since keys denote atom text.
|
|
|
1011
1012
|
### 9.3 Canonical compounds
|
|
1012
1013
|
|
|
1013
1014
|
A compound is written `name(A1, A2, ...)` when its name is an ASCII
|
|
1014
|
-
identifier, not a reserved
|
|
1015
|
+
identifier, not a reserved name of Section 9.1 and not `struct`, and
|
|
1015
1016
|
`struct('name', A1, ...)` otherwise. Arguments are separated by `, `.
|
|
1016
1017
|
|
|
1017
1018
|
A chain of conjunctions or disjunctions built from the left,
|
|
@@ -1172,6 +1173,16 @@ and its uses MUST be identical, in order, to the conjuncts of the argument of
|
|
|
1172
1173
|
`call` or `once`, or of one side of a disjunction. When trusted boundaries are
|
|
1173
1174
|
forbidden ("strict"), each `'absent'` or `'collected'` step fails C5.
|
|
1174
1175
|
|
|
1176
|
+
Host math is the one exception to identical re-decision. A `'builtin'` step
|
|
1177
|
+
`is(V, E)`, where `V` is a float and `E` applies `exp`, `log`, `log2`,
|
|
1178
|
+
`log10`, a trigonometric or hyperbolic function, or `pow`, also passes C5
|
|
1179
|
+
when `E` evaluates to a float at most 4 units in the last place from `V`,
|
|
1180
|
+
that is, with at most 3 floats between them. Such results may differ between
|
|
1181
|
+
conforming implementations (Section 14), so a proof made by one still checks
|
|
1182
|
+
with another. Every other recomputed result MUST be identical; a difference
|
|
1183
|
+
that grows beyond this bound, for example through cancellation, MAY make a
|
|
1184
|
+
proof fail on an implementation other than the one that made it.
|
|
1185
|
+
|
|
1175
1186
|
### 11.7 C6 Boundary consistency
|
|
1176
1187
|
|
|
1177
1188
|
A trusted boundary cannot be proved, but the evidence at hand can refute it.
|
|
@@ -1393,11 +1404,70 @@ Canonical term spelling, conclusion order, proof contents and report counts
|
|
|
1393
1404
|
are required as specified. Comment wording and failure `Detail` text are
|
|
1394
1405
|
informative. Fresh variable names are unspecified where Section 2.4 says
|
|
1395
1406
|
so. Float results of `exp`, logarithms, trigonometric and hyperbolic
|
|
1396
|
-
functions, and `pow` with floats MAY differ in the last
|
|
1407
|
+
functions, and `pow` with floats MAY differ by one unit in the last place,
|
|
1408
|
+
and C5 accepts a result of such a function within 4 units (Section 11.6).
|
|
1397
1409
|
|
|
1398
1410
|
The conformance cases and saved example documents exercise these
|
|
1399
1411
|
requirements. They do not replace the definitions in this specification.
|
|
1400
1412
|
|
|
1413
|
+
### 14.1 Conformance test suite
|
|
1414
|
+
|
|
1415
|
+
The conformance test suite tests an implementation of the command line of
|
|
1416
|
+
Section 13.1 as a black box: each case creates files, runs the command and
|
|
1417
|
+
compares its exit code and output with what this specification requires.
|
|
1418
|
+
The suite is the `conformance` directory of the eyemind repository. Its
|
|
1419
|
+
expected outputs are derived from this specification; where a case and this
|
|
1420
|
+
specification disagree, the specification governs and the case is wrong.
|
|
1421
|
+
|
|
1422
|
+
**Manifest.** `manifest.json` is a JSON array that lists the case files in
|
|
1423
|
+
the order they run. Each element is an object with three string members:
|
|
1424
|
+
`file`, the name of a case file in the same directory; `spec`, the sections
|
|
1425
|
+
of this specification it tests, separated by commas; and `topic`, a
|
|
1426
|
+
description. The cases of a file run in the order they appear in it.
|
|
1427
|
+
|
|
1428
|
+
**Case files.** A case file is a document (Section 9.1), read as data and
|
|
1429
|
+
never executed, whatever its file name extension. Each of its terms is one
|
|
1430
|
+
case, `conformance(Name, Settings)`, where `Name` is an atom unique in the
|
|
1431
|
+
suite and `Settings` is a proper list of the settings below. A **block** is a
|
|
1432
|
+
proper list of atoms, each one line; its text is each line followed by a
|
|
1433
|
+
line feed, so `[]` is empty text. A file MUST NOT contain any other term,
|
|
1434
|
+
and a setting other than these is an error in the suite.
|
|
1435
|
+
|
|
1436
|
+
| Setting | Meaning | When absent |
|
|
1437
|
+
| --- | --- | --- |
|
|
1438
|
+
| `spec(S)` | `S`, an atom: the sections the case tests, separated by commas. | |
|
|
1439
|
+
| `args(A)` | `A`, an atom: the command's arguments, split into words as described below. | `'program.js'` |
|
|
1440
|
+
| `exit(N)` | `N`, an integer: the exit code required. | `0` |
|
|
1441
|
+
| `stderr(M)` | Standard error: `'empty'`, no text; `'nonempty'`, some text other than white space; `'json'`, one JSON value; `prefix(T)`, text that starts with the atom `T`. | Not compared |
|
|
1442
|
+
| `file(Name, Block)` | A file the case creates before running; `Name` is a relative path whose `/` separates directories. | |
|
|
1443
|
+
| `stdin(Block)` | Standard input. | Empty |
|
|
1444
|
+
| `stdout(Block)` | Standard output, compared line by line. | Not compared |
|
|
1445
|
+
| `stdout('nonempty')` | Standard output contains text other than white space. | |
|
|
1446
|
+
| `stdout_json(Block)` | Standard output, compared as JSON. | |
|
|
1447
|
+
| `stdout_text(T)` | Standard output given as the atom `T`, for text a block cannot show, such as text without a final line feed; compared as `stdout` compares. | |
|
|
1448
|
+
|
|
1449
|
+
Each setting appears at most once in a case, except `file`.
|
|
1450
|
+
|
|
1451
|
+
**Running a case.** A case runs in a new empty directory. Each `file`
|
|
1452
|
+
setting creates its file there, with any directories its path names. The
|
|
1453
|
+
command then runs in that directory, with the words of `args` after the
|
|
1454
|
+
command's own words and with the `stdin` text on standard input. `args` is
|
|
1455
|
+
split into words as a POSIX shell splits a command line without expansions:
|
|
1456
|
+
white space separates words; text between single quotes is taken as is;
|
|
1457
|
+
between double quotes a backslash escapes only `"`, `\`, `$`, `` ` `` and a
|
|
1458
|
+
line feed; elsewhere a backslash escapes the next character; and adjacent
|
|
1459
|
+
quoted and unquoted parts join into one word.
|
|
1460
|
+
|
|
1461
|
+
**Passing.** A case passes when the exit code equals `exit` and standard
|
|
1462
|
+
output and standard error meet the settings that compare them. Line by line,
|
|
1463
|
+
the expected and actual standard output MUST have the same number of lines,
|
|
1464
|
+
and each expected line MUST equal the actual one, except that each `…`
|
|
1465
|
+
(U+2026) in an expected line matches any sequence of characters within that
|
|
1466
|
+
line; this specification leaves such text, for example a failure detail, to
|
|
1467
|
+
the implementation (Section 14). As JSON, both outputs MUST parse, and they
|
|
1468
|
+
MUST be equal as JSON values: arrays element by element, objects with the
|
|
1469
|
+
same member names whatever their order, and numbers by their values.
|
|
1470
|
+
|
|
1401
1471
|
## 15. Security
|
|
1402
1472
|
|
|
1403
1473
|
Programs execute JavaScript and require the caller's trust. Documents,
|
package/package.json
CHANGED
package/src/builtins.js
CHANGED
|
@@ -60,6 +60,7 @@ function functor(args, resolved, env, bind) {
|
|
|
60
60
|
function arg(args, resolved, env, bind) {
|
|
61
61
|
const [position, term] = resolved;
|
|
62
62
|
const index = boundedInteger(position, 1, 1024);
|
|
63
|
+
if (term instanceof Var) throw new EyemindError('arg/3 needs a bound term');
|
|
63
64
|
return term instanceof Struct && index <= term.args.length && bind(args[2], term.args[index - 1]);
|
|
64
65
|
}
|
|
65
66
|
|
package/src/engine.js
CHANGED
|
@@ -1036,8 +1036,10 @@ function reason(program, goalInputs, options) {
|
|
|
1036
1036
|
}
|
|
1037
1037
|
let report = null;
|
|
1038
1038
|
if (document !== null) {
|
|
1039
|
-
// The generated proof is checked before it is returned
|
|
1040
|
-
|
|
1039
|
+
// The generated proof is checked before it is returned, against the goals
|
|
1040
|
+
// as they were given: an atom goal is a string, which read again as goal
|
|
1041
|
+
// text would be a variable or no term at all.
|
|
1042
|
+
report = checkRendered(program, rendered[1], goalInputs);
|
|
1041
1043
|
if (!report.valid) throw new EyemindError(`cannot certify this result: ${report.failures[0].detail}`);
|
|
1042
1044
|
}
|
|
1043
1045
|
let behind = null;
|
package/src/proof.js
CHANGED
|
@@ -16,7 +16,7 @@ import { CONTROL_KEYS, Program } from './program.js';
|
|
|
16
16
|
import { readTerms } from './reader.js';
|
|
17
17
|
import {
|
|
18
18
|
Env, EyemindError, Struct, Var, conjunction, copyResolved, deref, flattenConjunction, freshTerm,
|
|
19
|
-
isCallable, isGround, isTerm, key, listFromItems, properListItems, unify, variables,
|
|
19
|
+
isCallable, isFloat, isGround, isTerm, key, listFromItems, properListItems, unify, variables,
|
|
20
20
|
} from './terms.js';
|
|
21
21
|
import { Lettering, letterName, quote, write, writeNotingVariables } from './writer.js';
|
|
22
22
|
|
|
@@ -52,6 +52,51 @@ function sameValue(a, b) {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
// Whether term, read through env, is exactly target, variables included.
|
|
55
|
+
// The functions whose float results may differ between hosts' math libraries
|
|
56
|
+
// (SPEC Section 14), and how far apart, in units in the last place, a claimed
|
|
57
|
+
// result of one of them may be from the recomputed one (Section 11.6).
|
|
58
|
+
const HOST_MATH = new Set(['exp', 'log', 'log2', 'log10', 'sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'atan2',
|
|
59
|
+
'sinh', 'cosh', 'tanh', 'pow']);
|
|
60
|
+
const HOST_MATH_ULPS = 4n;
|
|
61
|
+
|
|
62
|
+
function usesHostMath(expression) {
|
|
63
|
+
const pending = [expression];
|
|
64
|
+
while (pending.length) {
|
|
65
|
+
const term = pending.pop();
|
|
66
|
+
if (!(term instanceof Struct)) continue;
|
|
67
|
+
if (HOST_MATH.has(term.name)) return true;
|
|
68
|
+
pending.push(...term.args);
|
|
69
|
+
}
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// A float's position among all floats, so that adjacent floats differ by one.
|
|
74
|
+
const BITS = new DataView(new ArrayBuffer(8));
|
|
75
|
+
function floatPosition(value) {
|
|
76
|
+
BITS.setFloat64(0, value);
|
|
77
|
+
const bits = BITS.getBigInt64(0);
|
|
78
|
+
return bits < 0n ? -(bits & 0x7fffffffffffffffn) : bits;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// is(Claimed, Expression) with a float claimed for an expression that uses
|
|
82
|
+
// host math agrees when the recomputed float lies within HOST_MATH_ULPS of it.
|
|
83
|
+
function nearHostMath(goal) {
|
|
84
|
+
if (!isTerm(goal, 'is', 2) || !isFloat(goal.args[0]) || !usesHostMath(goal.args[1])) return false;
|
|
85
|
+
const result = new Var('Result');
|
|
86
|
+
const env = new Env();
|
|
87
|
+
try {
|
|
88
|
+
for (const solution of primitive(new Struct('is', [result, goal.args[1]]), env)) {
|
|
89
|
+
const value = deref(result, solution);
|
|
90
|
+
if (!isFloat(value)) return false;
|
|
91
|
+
const distance = floatPosition(value) - floatPosition(goal.args[0]);
|
|
92
|
+
return (distance < 0n ? -distance : distance) <= HOST_MATH_ULPS;
|
|
93
|
+
}
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (!(error instanceof EyemindError)) throw error;
|
|
96
|
+
}
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
|
|
55
100
|
function resolvesTo(term, env, target) {
|
|
56
101
|
const pending = [term, target];
|
|
57
102
|
while (pending.length) {
|
|
@@ -294,7 +339,7 @@ function readDocument(program, document, fail, spellings) {
|
|
|
294
339
|
const [goal, by, bindingList, useList] = term.args;
|
|
295
340
|
const bindings = properListItems(bindingList, empty);
|
|
296
341
|
const uses = properListItems(useList, empty);
|
|
297
|
-
if (bindings === null || uses === null) {
|
|
342
|
+
if (bindings === null || uses === null || !bindings.every((pair) => isTerm(pair, '=', 2))) {
|
|
298
343
|
fail.add('C3', 'step bindings must be an object and uses an array', goal);
|
|
299
344
|
continue;
|
|
300
345
|
}
|
|
@@ -366,6 +411,7 @@ function checkSteps(program, steps, allowTrusted, fail, spellings, name) {
|
|
|
366
411
|
// A primitive that cannot be recomputed does not pass C5.
|
|
367
412
|
if (!(error instanceof EyemindError)) throw error;
|
|
368
413
|
}
|
|
414
|
+
if (!agrees) agrees = nearHostMath(goal);
|
|
369
415
|
if (!agrees) fail.add('C5', `primitive disagrees: ${text(goal)}`, goal);
|
|
370
416
|
else tally.redecided++;
|
|
371
417
|
} else if (by === CONTROL_BY) {
|
|
@@ -614,15 +660,16 @@ function checkBoundaries(program, steps, boundaries, fail) {
|
|
|
614
660
|
const solved = parts.every((part, i) => shown[i].some((item) => unify(freshTerm(part, `absent${nextSerial()}`), item, new Env())));
|
|
615
661
|
if (solved) fail.add('C6', `absence contradicted by evidence: ${text(boundary)}`, boundary);
|
|
616
662
|
} else {
|
|
663
|
+
const parts = flattenConjunction(boundary.args[1]);
|
|
664
|
+
const shown = parts.length === 1 ? evidence(parts[0]) : null;
|
|
665
|
+
// Whether the collection is decided does not depend on its result.
|
|
666
|
+
if (shown !== null) confronted++;
|
|
617
667
|
const items = properListItems(boundary.args[2], new Env());
|
|
618
668
|
if (items === null) {
|
|
619
669
|
fail.add('C6', `collected result is not a proper list: ${text(boundary)}`, boundary);
|
|
620
670
|
continue;
|
|
621
671
|
}
|
|
622
|
-
const parts = flattenConjunction(boundary.args[1]);
|
|
623
|
-
const shown = parts.length === 1 ? evidence(parts[0]) : null;
|
|
624
672
|
if (shown === null) continue;
|
|
625
|
-
confronted++;
|
|
626
673
|
for (const item of shown) {
|
|
627
674
|
const names = new Map();
|
|
628
675
|
const number = nextSerial();
|
package/src/reader.js
CHANGED
|
@@ -9,7 +9,6 @@ import { EMPTY, EyemindError, Struct, Var, nonfinite } from './terms.js';
|
|
|
9
9
|
import { CHAINED, RESERVED_WORDS, decodeName } from './writer.js';
|
|
10
10
|
|
|
11
11
|
// Names that a program reads as JavaScript values, so a document may not use them.
|
|
12
|
-
const JS_VALUES = new Set(['undefined', 'NaN', 'Infinity']);
|
|
13
12
|
|
|
14
13
|
// The name of the variable an identifier writes: names starting VAR_ are
|
|
15
14
|
// encoded (Section 9.2).
|
|
@@ -222,7 +221,7 @@ export class Reader {
|
|
|
222
221
|
else throw tokens.error('a minus sign applies only to a number', token.line);
|
|
223
222
|
state = 'after';
|
|
224
223
|
} else if (token.kind === 'name') {
|
|
225
|
-
if (RESERVED_WORDS.has(token.text) ||
|
|
224
|
+
if (RESERVED_WORDS.has(token.text) || token.text.includes('$')) {
|
|
226
225
|
throw tokens.error(`${token.text} is not a term`, token.line);
|
|
227
226
|
}
|
|
228
227
|
if (tokens.at('(')) {
|
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.2.
|
|
2
|
+
export const VERSION = '0.2.4';
|
package/src/writer.js
CHANGED
|
@@ -9,8 +9,10 @@
|
|
|
9
9
|
// plain identifier is written struct('name', Arg, ...).
|
|
10
10
|
import { Struct, Var, deref } from './terms.js';
|
|
11
11
|
|
|
12
|
-
// Words a
|
|
12
|
+
// Words a document cannot use as a plain name: JavaScript's reserved words,
|
|
13
|
+
// and the names of its values that are not terms (Section 9.1).
|
|
13
14
|
export const RESERVED_WORDS = new Set([
|
|
15
|
+
'undefined', 'NaN', 'Infinity',
|
|
14
16
|
'await', 'break', 'case', 'catch', 'class', 'const', 'continue', 'debugger', 'default', 'delete',
|
|
15
17
|
'do', 'else', 'enum', 'export', 'extends', 'false', 'finally', 'for', 'function', 'if',
|
|
16
18
|
'implements', 'import', 'in', 'instanceof', 'interface', 'let', 'new', 'null', 'package',
|