eyemind 0.2.3 → 0.2.5

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
@@ -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/)**: 556 cases that test any
21
+ - **[Conformance suite](conformance/)**: 1270 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.3
5
+ Version: eyemind 0.2.5
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)
@@ -164,7 +165,8 @@ Lists use bracket syntax:
164
165
  For example, `[1, 2]` and `[1, ...[2]]` denote the same list. A **proper
165
166
  list** ends in `[]`. A tail left as a variable gives an **open list**;
166
167
  a non-list tail gives an **improper list**. Only the final item may be a
167
- spread tail. In document syntax, `[...Tail]` denotes `Tail` itself.
168
+ spread tail; a program that spreads a logical variable or compound before
169
+ the last item, as in `[...T, 1]`, MUST raise an error. In document syntax, `[...Tail]` denotes `Tail` itself.
168
170
  Program spread expressions also follow Section 3.3.
169
171
 
170
172
  The empty list is the atom whose text is `[]`, so `'[]'` denotes the same
@@ -210,7 +212,10 @@ A program consists of one or more non-strict JavaScript scripts. Each script
210
212
  may use JavaScript declarations, expressions, functions and control flow to
211
213
  construct its clauses. No import is required for the statements and term
212
214
  constructors defined here. Clauses are stated in the order their statement
213
- calls occur; multiple input scripts are processed in input order.
215
+ calls occur; multiple input scripts are processed in input order. Each
216
+ script runs in a scope of its own: a name one script declares is, in
217
+ another script, resolved as Section 3.2 specifies, so scripts share no
218
+ declarations.
214
219
 
215
220
  The supplied names are the statements of Section 3.4, `forward`, `backward`,
216
221
  `preds`, `vars`, `struct`, the controls and primitives of Section 4, the
@@ -246,7 +251,9 @@ names `step`, `clause`, `activated`, `forward`, `backward`, `struct`,
246
251
 
247
252
  A top-level expression statement that calls a predicate supplied by rule 4,
248
253
  such as a misspelled `fcat(p(1))`, states nothing; a loader MUST report it as
249
- an error rather than ignore it.
254
+ an error rather than ignore it. Only statements at the top level of a script
255
+ are checked: inside a function or a block, such a call builds a term the
256
+ script discards, and a loader MAY accept it.
250
257
 
251
258
  ### 3.3 Term construction
252
259
 
@@ -267,6 +274,9 @@ reading a data document (Section 9.1).
267
274
  | `[]`, `[T1, ..., Tn]`, `[Head, ...Tail]` | Lists (Section 2.2). |
268
275
 
269
276
  Thus `2.0` in a program is an integer; use `float(2)` for the float `2.0`.
277
+ Applied to anything other than a JavaScript number, such as a `BigInt`, a
278
+ string or a variable, `float(...)` is the ordinary compound `float/1`, the
279
+ arithmetic function of Section 5.2: `is(Y, float(2n))` binds `Y` to `2.0`.
270
280
  Use a `BigInt` literal for an integer outside JavaScript's exact number
271
281
  range, for example `9007199254740993n`. Numeric expressions are evaluated by
272
282
  JavaScript before conversion to terms.
@@ -352,7 +362,9 @@ statement call begins; facts read by `facts_from` use that call's line.
352
362
 
353
363
  A clause MUST be rejected when:
354
364
 
355
- - a head is not an atom or compound;
365
+ - a head is not an atom or compound, or is a list, the atom `'[]'` or a
366
+ `'.'/2` compound, which as a goal would be a conjunction (Section 4.1) and
367
+ could never call the clause;
356
368
  - a head uses `step/4` or `clause/2`, reserved for proof documents, or
357
369
  explicitly states the reserved predicate `activated/2`;
358
370
  - a head is a primitive or control, except the head `'true'` of a query or
@@ -372,7 +384,15 @@ specified in Section 6.
372
384
 
373
385
  A goal is an atom, a compound, a variable to be bound to a callable term,
374
386
  or a non-empty proper array of goals denoting their conjunction. A number,
375
- an empty array or an open or improper list is not a goal.
387
+ an empty array or an open or improper list is not a goal. An array of one
388
+ goal denotes that goal: `[p(X)]` is `p(X)`. Arrays of several goals denote
389
+ the left-nested `and` chain, also in goal text, so the goal text
390
+ `once([p(X), q(X)])` is answered as `once(and(p(1), q(1)))`.
391
+
392
+ A variable called as a goal MUST be bound when it runs: an unbound variable,
393
+ or one bound to a number, MUST raise an error. A variable bound to an atom or
394
+ compound calls that term as it is; a list bound at run time is the compound
395
+ `'.'/2`, not a conjunction, and so has no solution.
376
396
 
377
397
  Array-to-conjunction notation applies in these positions:
378
398
 
@@ -450,13 +470,13 @@ primitive called outside its patterns raises an error.
450
470
  | `is_atom(@X)`, `is_number(@X)` | | `X` is an atom, or a number. |
451
471
  | `is_int(@X)`, `is_float(@X)` | | `X` is an integer, or a float. |
452
472
  | `is_compound(@X)` | | `X` is a compound; a non-empty list is one. |
453
- | `functor` | `functor(+T, ?N, ?A)`, `functor(-T, +N, +A)` | Name and arity of `T`, or `T` built with fresh variable arguments. When constructing, `A` MUST be an integer from 0 to 1024. |
454
- | `arg` | `arg(+N, +T, ?A)` | `A` is the `N`th argument of `T`, counting from 1. |
455
- | `univ` | `univ(+T, ?L)`, `univ(-T, +L)` | `L` is the list of `T`'s name and arguments. |
473
+ | `functor` | `functor(+T, ?N, ?A)`, `functor(-T, +N, +A)` | Name and arity of `T`, or `T` built with fresh variable arguments. When constructing, `A` MUST be an integer from 0 to 1024; with `A` 0, `N` MUST be atomic and `T` is `N` itself, so `functor(T, 3, 0)` gives `3`; with `A` above 0, `N` MUST be an atom. |
474
+ | `arg` | `arg(+N, +T, ?A)` | `A` is the `N`th argument of `T`, counting from 1. `N` MUST be an integer from 1 to 1024. An atomic `T`, or an `N` above `T`'s arity, has no such argument, and `arg` fails. |
475
+ | `univ` | `univ(+T, ?L)`, `univ(-T, +L)` | `L` is the list of `T`'s name and arguments; for atomic `T`, `[T]`. When constructing, `L` MUST be a non-empty proper list whose first element is an atom, or a one-element list of a number; a partial list raises an error. |
456
476
  | `atom_chars` | `atom_chars(+A, ?L)`, `atom_chars(-A, +L)` | `L` is the list of `A`'s characters as atoms. |
457
- | `atom_codes` | `atom_codes(+A, ?L)`, `atom_codes(-A, +L)` | `L` is the list of `A`'s character code points. |
477
+ | `atom_codes` | `atom_codes(+A, ?L)`, `atom_codes(-A, +L)` | `L` is the list of `A`'s character code points. Each code MUST be an integer from 0 to 1114111 (0x10FFFF); a code of a lone surrogate gives that lone surrogate. |
458
478
  | `atom_length` | `atom_length(+A, ?N)` | `N` is the number of characters of `A`. |
459
- | `number_chars` | `number_chars(?N, +L)`, `number_chars(+N, -L)` | A bound `L` is read as a number: an optional sign and decimal digits give an integer, and with a fraction or an exponent a float, as in `'007'`, `'-21.5'`, `'.5'` and `'1E3'`; other text fails. Otherwise `L` spells `N` in its canonical text (Section 9.2). |
479
+ | `number_chars` | `number_chars(?N, +L)`, `number_chars(+N, -L)` | A bound `L` is read as a number: an optional sign and decimal digits give an integer, and with a fraction or an exponent a float, as in `'007'`, `'-21.5'`, `'.5'`, `'1.'` and `'2E-2'`; other text fails, as does text whose value is too large for a finite float, such as `'1e400'`. Otherwise `L` spells `N` in its canonical text (Section 9.2). |
460
480
  | `atom_concat` | `atom_concat(+A, +B, ?AB)`, `atom_concat(?A, ?B, +AB)` | `AB` is `A` followed by `B`; with `AB` given, every split, shortest `A` first. |
461
481
 
462
482
  A character is a Unicode code point. Every primitive except `atom_concat` has
@@ -524,14 +544,16 @@ Integer arithmetic is exact and unbounded. Integer operands give an integer
524
544
  result except for `div`, a negative exponent in `pow`, and functions defined
525
545
  to return a float. Mixed `add`, `sub`, `mul`, `div`, `floordiv`, `mod` and
526
546
  `pow` give a float. Integer-conversion functions return integers; `min` and
527
- `max` return the selected operand with its original kind. No operation
547
+ `max` return the selected operand with its original kind, and among equal
548
+ operands the first: `min(1, 1.0)` is `1` and `min(1.0, 1)` is `1.0`. No operation
528
549
  implicitly makes an integer and a float unify.
529
550
 
530
551
  `floordiv` rounds down and `mod` has the divisor's sign:
531
552
  `floordiv(-7, 3)` is -3 and `mod(-7, 3)` is 2. `div` of integers rounds
532
553
  their exact quotient once to the nearest binary64 value, with halfway cases
533
554
  rounded to even, including in the subnormal range. Underflow to zero keeps
534
- the sign. `round(X)` gives an integer; `round(X, Digits)` rounds to the
555
+ the sign, and a zero quotient has the sign binary64 division gives it:
556
+ `div(0, -5)` is `-0.0`. `sqrt(-0.0)` is `-0.0`. `round(X)` gives an integer; `round(X, Digits)` rounds to the
535
557
  specified number of decimal places. Float spelling is defined in Section 9.2.
536
558
 
537
559
  Numeric comparison is exact across integers and floats. Bitwise operations
@@ -540,8 +562,10 @@ use unbounded two's-complement integers.
540
562
  ### 5.4 Arithmetic errors and limits
541
563
 
542
564
  An evaluation MUST raise an error, rather than produce a value, on a division
543
- or modulo by zero, a domain error (such as `sqrt(-1)` or `log(0)`), a bitwise
544
- operation, `gcd` or `lcm` on a float, a negative shift count, when the result
565
+ or modulo by zero, a domain error (such as `sqrt(-1)`, `log(0)`, or a
566
+ logarithm `log(X, B)` whose `X` or base `B` is not positive or whose `B` is
567
+ 1), a bitwise operation, `isqrt`, `gcd` or `lcm` on a float, a negative shift
568
+ count, when the result
545
569
  is not a finite real number,
546
570
  or when a power or left shift would need more than 2^26 bits: for a power of
547
571
  integers `pow(A, B)` with `B > 0` and `abs(A) > 1`, when `B` times the bit
@@ -636,7 +660,8 @@ intermediate enabling steps: the generator's premises are direct uses of
636
660
  the derived rule's step. A nested instance retains all enclosing premises.
637
661
 
638
662
  For witness construction, a generating head's **scope projection** replaces
639
- each template conjunct by the list of its captured variables, leaving ordinary
663
+ each template conjunct by the list of its captured variables, in order of
664
+ their first occurrence in the template as written, leaving ordinary
640
665
  heads in place. These lists are compiler data and are never concluded as
641
666
  facts. Only variables of this projection participate in that firing's witness
642
667
  construction (Section 8.2). A witness introduced by the generator remains
@@ -811,7 +836,10 @@ In a round, clauses run in clause order. An appended clause runs its derived
811
836
  instances in the order they were created; a potential clause with no instance
812
837
  is skipped. Each instance searches only its own body with its captures bound.
813
838
  All instances of one clause search the same current state, and all solutions
814
- are collected before any conclusion of that clause is published:
839
+ are collected before any conclusion of that clause is published. They are
840
+ published solution by solution, in the order found, and the heads of each
841
+ solution in their stated order: `implies(p(X), [a(X), b(X)])` over `p(1)`
842
+ and `p(2)` publishes `a(1)`, `b(1)`, `a(2)`, `b(2)`. For each head:
815
843
 
816
844
  - a head variable the solution leaves unbound is bound to a **Skolem term**
817
845
  `skolem(Rule, Variable, Arguments)`: `Rule` is the clause number of the
@@ -863,13 +891,30 @@ A run is bounded by `max_depth`, the depth of nested rule bodies (default
863
891
  1000000), by `max_inferences`, the number of goals started (default
864
892
  1000000), and by `max_iterations`, the rounds per stratum (default 1000).
865
893
  Exceeding a bound MUST raise an error; a partial result MUST NOT be returned
866
- as complete.
894
+ as complete. The bounds count as follows:
895
+
896
+ - A goal solved by a rule is one level deeper than the goal whose body it is
897
+ in, and a goal asked from outside or a forward rule's premise is at level
898
+ 0; solving a goal through more than `max_depth` nested rule bodies exceeds
899
+ the bound. A goal solved by a fact adds no level.
900
+ - Every goal the search starts counts once toward `max_inferences`,
901
+ including each conjunction, control and primitive, in forward premises as
902
+ in goals: `[p(X), p(Y)]` against one fact starts three goals.
903
+ - Every forward round of a stratum counts toward `max_iterations`,
904
+ including the final round that adds nothing; a stratum without forward
905
+ rules has no rounds.
906
+
907
+ A run that exceeds a bound concludes nothing: the command line writes no
908
+ conclusions to standard output, even answers found before the bound was
909
+ reached, and exits with code 1 (Section 13.1).
867
910
 
868
911
  Each supplied bound MUST be a positive integer no greater than 9007199254740991.
869
912
 
870
913
  ### 8.4 External goals
871
914
 
872
- A run MAY be given goals. After forward reasoning, each goal is solved in
915
+ A run MAY be given goals. Each goal is read on its own, so goals share no
916
+ variables: `p(X)` and `q(X)` given separately ask two unrelated questions.
917
+ After forward reasoning, each goal is solved in
873
918
  turn, and each solution is reported as the instance of its goal, unless an
874
919
  instance identical up to renaming of its variables was already reported for
875
920
  this goal or an earlier one of the same run.
@@ -934,7 +979,8 @@ expression per line. Readers accept exactly:
934
979
  float literal containing a point or exponent: a number;
935
980
  - a name: the variable of that name, where each `_` is a new anonymous
936
981
  variable (Section 4.2) and a name starting with `VAR_` is decoded
937
- (Section 9.2), with malformed encodings rejected;
982
+ (Section 9.2), with malformed encodings rejected; a decoded name is a
983
+ named variable even when it is `_`, so `VAR___` twice is one variable;
938
984
  - a call of a plain name: the compound, or the atom when there are no
939
985
  arguments; `struct('name', ...)` as in Section 9.3; `and` and `or` with
940
986
  more than two arguments as in Section 4.2;
@@ -950,7 +996,12 @@ outside these forms MUST be rejected, including operators other than a
950
996
  numeric minus, property or subscript access, spread call arguments,
951
997
  assignments, declarations, functions and template literals. Numeric literals
952
998
  MUST be decimal without separators; integers other than `0` MUST NOT start
953
- with `0`. Reading canonical text MUST recover the original term.
999
+ with `0`, and neither may the digits before a float's point or exponent, so
1000
+ `00.5` and `05.5` MUST be rejected while `0.5` and `0e1` are floats. A float
1001
+ literal denotes the nearest binary64 value: one that underflows reads as a
1002
+ zero of its sign, as `1e-400` reads as `0.0`, and one whose value is too
1003
+ large for a finite float MUST be rejected. Reading canonical text MUST
1004
+ recover the original term.
954
1005
 
955
1006
  A call's name and opening `(` MUST NOT be separated by a line feed outside
956
1007
  a comment. The same restriction applies between a numeric minus and its
@@ -966,7 +1017,9 @@ conjunction rules of Section 4.1.
966
1017
  String escapes are `\\`, `\'`, `\"`, `\n`, `\r`, `\t`, `\b`, `\f`,
967
1018
  `\v`, `\0` (not followed by a decimal digit), `\xHH`, `\uHHHH` and
968
1019
  `\u{H...}` with one to six hexadecimal digits denoting a Unicode code point.
969
- Other escapes and raw line breaks in strings MUST be rejected. Outside
1020
+ Other escapes and raw line breaks in strings MUST be rejected; a raw line
1021
+ break is a line feed or a carriage return, while the characters U+2028 and
1022
+ U+2029 are allowed in a string as themselves. Outside
970
1023
  strings, layout consists of spaces, tabs, carriage returns, line feeds and
971
1024
  the byte-order-mark character, plus comments. A line feed outside brackets
972
1025
  ends the current expression statement.
@@ -999,8 +1052,8 @@ Object keys may also use these reserved names, since keys denote atom text.
999
1052
  Otherwise use `e`, an explicit exponent sign and at least two exponent
1000
1053
  digits, such as `1e+22` or `1e-05`.
1001
1054
  - 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 word
1003
- of JavaScript, not `_` and does not begin with `VAR_`. Any other name is
1055
+ (letters, digits and `_`, not starting with a digit), not a reserved name
1056
+ of Section 9.1, not `_` and does not begin with `VAR_`. Any other name is
1004
1057
  written `VAR_` followed by the name with every ASCII letter and digit kept,
1005
1058
  every `_` doubled and every other character `c` replaced by `_hex_` where
1006
1059
  `hex` is the lowercase hexadecimal code point of `c`. The variable name `A-B`
@@ -1011,7 +1064,7 @@ Object keys may also use these reserved names, since keys denote atom text.
1011
1064
  ### 9.3 Canonical compounds
1012
1065
 
1013
1066
  A compound is written `name(A1, A2, ...)` when its name is an ASCII
1014
- identifier, not a reserved word of JavaScript and not `struct`, and
1067
+ identifier, not a reserved name of Section 9.1 and not `struct`, and
1015
1068
  `struct('name', A1, ...)` otherwise. Arguments are separated by `, `.
1016
1069
 
1017
1070
  A chain of conjunctions or disjunctions built from the left,
@@ -1061,6 +1114,9 @@ array of heads, and `G1, ...` are the body goals themselves. Displays are not
1061
1114
  authority: a checker MUST compare each with the program it checks against.
1062
1115
 
1063
1116
  Each argument of a multi-argument `fact` has its own `fact(Head)` display.
1117
+ A body is displayed with its conjunctions flattened (Section 3.5), whether
1118
+ the program wrote them as arrays or with `and`: `implied_by(r, and(a, b))`
1119
+ has display `implied_by('r', ['a', 'b'])`.
1064
1120
  Conjunctive query and contradiction bodies are displayed as separate goal
1065
1121
  arguments: `query([p(X), q(X)])` has display `query(p(X), q(X))`.
1066
1122
 
@@ -1089,12 +1145,14 @@ order. `By` is one of:
1089
1145
  | `clause(N)` | `Goal` is an instance of a head of clause `N`, a fact or a rule, whose body instance is `Uses`. | the clause's variables | the body instance, empty for a fact |
1090
1146
  | `source(N)` | `Goal` is an instance of the inspectable source rule `N` as `implies(Body, Head)` or `implied_by(Head, Body)`. | empty | empty |
1091
1147
  | `'builtin'` | `Goal` is a primitive that holds. | empty | empty |
1092
- | `'control'` | `Goal` is `call`, `once` or a disjunction, solved by `Uses`. | empty | the solved goals |
1148
+ | `'control'` | `Goal` is `call`, `once` or a disjunction, solved by `Uses`. | empty | the solved goals; for a disjunction, the side that was solved, so `or(or(A, B), C)` solved by `A` uses `or(A, B)`, which has a step of its own |
1093
1149
  | `'absent'` | `Goal` is a negation taken on trust. | empty | empty |
1094
1150
  | `'collected'` | `Goal` is a `findall` taken on trust. | empty | empty |
1095
1151
 
1096
1152
  `Bindings` is an object from each variable name of the cited clause, as
1097
- the program wrote it, to its value in this step: `{X: 'socrates'}`. A
1153
+ the program wrote it, to its value in this step: `{X: 'socrates'}`. A key
1154
+ that is a plain name of Section 9.2 is written bare, and any other name as a
1155
+ string, such as `{'A-B': 1}`. A
1098
1156
  reasoner writes every variable of the clause, in the order the clause display
1099
1157
  first shows them (for `implies`, the premise first);
1100
1158
  a checker MUST accept partial bindings when the goal and uses determine the
@@ -1117,22 +1175,25 @@ detail and, where there is one, the term concerned.
1117
1175
 
1118
1176
  The document MUST read (Section 9.1); otherwise C3 fails and the document has
1119
1177
  no claims and no steps. Each `clause(N, Display)` MUST name a clause of the
1120
- program whose display (Section 10.2) is identical to `Display`; otherwise C1
1121
- fails. Each `step` MUST have bindings that read as a list of `'='/2` pairs,
1178
+ program whose display (Section 10.2) is identical to `Display`, variable
1179
+ names included, so a display that merely renames the clause's variables
1180
+ differs; otherwise C1 fails. Each `step` MUST have bindings that read as a list of `'='/2` pairs,
1122
1181
  as an object literal does, and a list of uses, and no two steps may have
1123
1182
  identical goals; otherwise C3 fails and the step is set aside. Every other
1124
1183
  statement is a claim (Section 10.1).
1125
1184
 
1126
1185
  ### 11.2 C1 Resolution
1127
1186
 
1128
- For a `clause(N)` step, `N` MUST be a clause of the program. The clause,
1187
+ For a `clause(N)` step, `N` MUST be a clause of the program; an `N` that is
1188
+ not such a clause number, such as `'x'` or `1.0`, fails C1. The clause,
1129
1189
  renamed apart, MUST satisfy: each binding names a distinct variable of the
1130
1190
  clause and unifies it with its value; and for one of the clause's heads
1131
1191
  (each conjunct of a forward rule's head), the head unifies with `Goal` and
1132
1192
  the body, of the same length as `Uses`, unifies with `Uses` pairwise, such
1133
1193
  that afterwards the head is identical to `Goal` and each body goal to its
1134
1194
  use. A step's own terms MUST NOT need further instantiation to match: a
1135
- source clause `same(X, X)` cannot justify `same('a', 'b')`.
1195
+ source clause `same(X, X)` cannot justify `same('a', 'b')`. Identity keeps
1196
+ the sign of zero, so a fact `z(0.0)` cannot justify the goal `z(-0.0)`.
1136
1197
 
1137
1198
  For a derived clause step, each enclosing generator MUST belong to the
1138
1199
  program. Its bindings may name variables of those generators' scope
@@ -1149,20 +1210,28 @@ verified C1 steps and cite the usual source clause display.
1149
1210
 
1150
1211
  ### 11.3 C2 Well-foundedness
1151
1212
 
1152
- No step MAY depend on itself through the uses of steps.
1213
+ No step MAY depend on itself through the uses of steps. A checker finds
1214
+ the dependencies by a depth-first search that starts from each step not yet
1215
+ visited, in document order, and follows each step's uses in order, each
1216
+ conjunct of a conjunctive use separately, to the steps whose goals they are.
1217
+ Each use that leads back to a step on the current search path is one C2
1218
+ failure, whose conclusion is the goal of that step.
1153
1219
 
1154
1220
  ### 11.4 C3 Justification
1155
1221
 
1156
1222
  `By` MUST be one of the forms of Section 10.3. A `'builtin'` step MUST have
1157
1223
  no bindings and no uses and a goal that is a primitive; an `'absent'` or
1158
1224
  `'collected'` step MUST have no bindings and no uses and a goal that is a
1159
- negation or a `findall`, respectively.
1225
+ negation or a `findall`, respectively. A step that fails C3 is not a trusted
1226
+ boundary: it counts in neither `trusted` nor C6.
1160
1227
 
1161
1228
  ### 11.5 C4 Coverage
1162
1229
 
1163
1230
  Each conjunct of each claim MUST have a step. Each use, and each conjunct of
1164
1231
  a use, MUST have a step or be identical to an instance of a fact of the
1165
- program. A document without claims is valid and certifies nothing.
1232
+ program. The conclusion of a failure for a use without a step is the goal of
1233
+ the step that uses it. A document without claims is valid and certifies
1234
+ nothing.
1166
1235
 
1167
1236
  ### 11.6 C5 Re-decision
1168
1237
 
@@ -1228,7 +1297,8 @@ bound to `skolem(N, Name, Args)`, where:
1228
1297
  the values that do contain one hold a witness of this firing.
1229
1298
 
1230
1299
  When a forward clause has any such variables, its full head under the step's
1231
- substitution MUST be ground. A generic inspection pattern cannot share one
1300
+ substitution MUST be ground; a step that leaves such a variable unbound, or
1301
+ bound to anything but its Skolem term, fails C8 rather than C1. A generic inspection pattern cannot share one
1232
1302
  existential witness across all its instances (Section 6.3).
1233
1303
 
1234
1304
  For a generator, use its scope projection (Section 6.2) as the head for this
@@ -1328,6 +1398,8 @@ texts, including unverified claims when `valid` is false. It also has `valid` (b
1328
1398
  (a list of `{kind, conclusion}`), `failures` (a list of
1329
1399
  `{condition, detail, conclusion?}`) and `conditions` (a list of
1330
1400
  `{id, name, covered, failed}`), where `conclusion` is a canonical text.
1401
+ Its variables keep the names the document gives them, rather than being
1402
+ renamed for each report fact as in the text form.
1331
1403
 
1332
1404
  ## 13. Command-Line Interface
1333
1405
 
@@ -1378,8 +1450,11 @@ without a clause leaves every other clause its number (Section 3.4), so the
1378
1450
  Skolem terms of the other rules (Section 8.2) do not change.
1379
1451
 
1380
1452
  Unused clauses are reported one per line, in clause order, as
1381
- `unused(line(L), Display)`, where `L` is the clause's line (Section 3.4) and
1382
- `Display` as in Section 10.2.
1453
+ `unused(line(L), Display)`, where `L` is the clause's line (Section 3.4) in
1454
+ the script that states it, and `Display` as in Section 10.2; with several
1455
+ scripts, clause order tells which script a line belongs to. The listing is
1456
+ the result whether or not the program halts with `'false'`, and the command
1457
+ exits with code 0 after it.
1383
1458
 
1384
1459
  "Unused" is relative to the recorded derivations, which are the first ones
1385
1460
  found (Section 8.7). A clause that only offers a second derivation of a
@@ -1387,7 +1462,9 @@ conclusion, such as a duplicate rule, is unused, though leaving out the
1387
1462
  clause that offers the first one would make it used; leaving out several
1388
1463
  unused clauses together can therefore change the conclusions. A
1389
1464
  `contradiction` rule that never fires changes no conclusion and is unused,
1390
- though it guards the program against data that would make it fire.
1465
+ though it guards the program against data that would make it fire; so are
1466
+ the clauses only its negations or collections could consult, since they are
1467
+ not in the proof.
1391
1468
 
1392
1469
  ## 14. Conformance
1393
1470
 
@@ -1409,6 +1486,66 @@ and C5 accepts a result of such a function within 4 units (Section 11.6).
1409
1486
  The conformance cases and saved example documents exercise these
1410
1487
  requirements. They do not replace the definitions in this specification.
1411
1488
 
1489
+ ### 14.1 Conformance test suite
1490
+
1491
+ The conformance test suite tests an implementation of the command line of
1492
+ Section 13.1 as a black box: each case creates files, runs the command and
1493
+ compares its exit code and output with what this specification requires.
1494
+ The suite is the `conformance` directory of the eyemind repository. Its
1495
+ expected outputs are derived from this specification; where a case and this
1496
+ specification disagree, the specification governs and the case is wrong.
1497
+
1498
+ **Manifest.** `manifest.json` is a JSON array that lists the case files in
1499
+ the order they run. Each element is an object with three string members:
1500
+ `file`, the name of a case file in the same directory; `spec`, the sections
1501
+ of this specification it tests, separated by commas; and `topic`, a
1502
+ description. The cases of a file run in the order they appear in it.
1503
+
1504
+ **Case files.** A case file is a document (Section 9.1), read as data and
1505
+ never executed, whatever its file name extension. Each of its terms is one
1506
+ case, `conformance(Name, Settings)`, where `Name` is an atom unique in the
1507
+ suite and `Settings` is a proper list of the settings below. A **block** is a
1508
+ proper list of atoms, each one line; its text is each line followed by a
1509
+ line feed, so `[]` is empty text. A file MUST NOT contain any other term,
1510
+ and a setting other than these is an error in the suite.
1511
+
1512
+ | Setting | Meaning | When absent |
1513
+ | --- | --- | --- |
1514
+ | `spec(S)` | `S`, an atom: the sections the case tests, separated by commas. | |
1515
+ | `args(A)` | `A`, an atom: the command's arguments, split into words as described below. | `'program.js'` |
1516
+ | `exit(N)` | `N`, an integer: the exit code required. | `0` |
1517
+ | `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 |
1518
+ | `file(Name, Block)` | A file the case creates before running; `Name` is a relative path whose `/` separates directories. | |
1519
+ | `stdin(Block)` | Standard input. | Empty |
1520
+ | `stdout(Block)` | Standard output, compared line by line. | Not compared |
1521
+ | `stdout('nonempty')` | Standard output contains text other than white space. | |
1522
+ | `stdout_json(Block)` | Standard output, compared as JSON. | |
1523
+ | `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. | |
1524
+
1525
+ Each setting appears at most once in a case, except `file`.
1526
+
1527
+ **Running a case.** A case runs in a new empty directory. Each `file`
1528
+ setting creates its file there, with any directories its path names. The
1529
+ command then runs in that directory, with the words of `args` after the
1530
+ command's own words and with the `stdin` text on standard input. `args` is
1531
+ split into words as a POSIX shell splits a command line without expansions:
1532
+ white space separates words; text between single quotes is taken as is;
1533
+ between double quotes a backslash escapes only `"`, `\`, `$`, `` ` `` and a
1534
+ line feed; elsewhere a backslash escapes the next character; and adjacent
1535
+ quoted and unquoted parts join into one word.
1536
+
1537
+ **Passing.** A case passes when the exit code equals `exit` and standard
1538
+ output and standard error meet the settings that compare them. Line by line,
1539
+ the expected and actual standard output MUST have the same number of lines,
1540
+ and each expected line MUST equal the actual one, except that each `…`
1541
+ (U+2026) in an expected line matches any sequence of characters within that
1542
+ line; this specification leaves such text, for example a failure detail, to
1543
+ the implementation (Section 14). As JSON, both outputs MUST parse, and they
1544
+ MUST be equal as JSON values: arrays element by element, objects with the
1545
+ same member names whatever their order, numbers by their values, and
1546
+ strings as lines are, so that an expected string containing `…` matches any
1547
+ string of that form.
1548
+
1412
1549
  ## 15. Security
1413
1550
 
1414
1551
  Programs execute JavaScript and require the caller's trust. Documents,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eyemind",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
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/builtins.js CHANGED
@@ -13,7 +13,7 @@
13
13
  import * as arith from './arith.js';
14
14
  import {
15
15
  EyemindError, Struct, Var, codePoints, compareTerms, copyResolved, deref, identical, isGround, key,
16
- listFromItems, nonfinite, properListItems, unify,
16
+ listFromItems, properListItems, unify,
17
17
  } from './terms.js';
18
18
  import { floatText } from './writer.js';
19
19
 
@@ -49,6 +49,7 @@ function functor(args, resolved, env, bind) {
49
49
  const count = boundedInteger(arity, 0, 1024);
50
50
  if (name instanceof Var) throw new EyemindError('functor/3 needs a bound name');
51
51
  if (count > 0 && typeof name !== 'string') throw new EyemindError('functor/3 needs an atom name');
52
+ if (name instanceof Struct) throw new EyemindError('functor/3 needs an atomic name');
52
53
  // Unique names across primitive invocations preserve variable isolation.
53
54
  functorSerial++;
54
55
  if (count === 0) return bind(args[0], name);
@@ -60,6 +61,7 @@ function functor(args, resolved, env, bind) {
60
61
  function arg(args, resolved, env, bind) {
61
62
  const [position, term] = resolved;
62
63
  const index = boundedInteger(position, 1, 1024);
64
+ if (term instanceof Var) throw new EyemindError('arg/3 needs a bound term');
63
65
  return term instanceof Struct && index <= term.args.length && bind(args[2], term.args[index - 1]);
64
66
  }
65
67
 
@@ -128,7 +130,8 @@ function numberChars(args, resolved, env, bind) {
128
130
  if (!match) return false;
129
131
  if (match[1] !== undefined) return bind(args[0], BigInt(text));
130
132
  const value = Number(text);
131
- if (!Number.isFinite(value)) throw nonfinite(value, 'number_chars/2: ');
133
+ // Text too large for a finite float names no number term.
134
+ if (!Number.isFinite(value)) return false;
132
135
  return bind(args[0], value);
133
136
  }
134
137
 
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
- report = checkRendered(program, rendered[1], goals);
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/lang.js CHANGED
@@ -153,16 +153,18 @@ class Builder {
153
153
  this.embedded = new Set();
154
154
  }
155
155
 
156
- add(kind, head, body, line = null) {
156
+ // anonymous says whether the program's _ still needs numbering; a document's
157
+ // terms come numbered, and its variable decoded as _ is a named one.
158
+ add(kind, head, body, line = null, anonymous = true) {
157
159
  if (line === null) line = statedLine !== null ? statedLine : this.locate();
158
160
  const pending = [head, ...body];
159
161
  const counter = new Anonymous(head, body);
160
162
  // In the order the statement writes them: a forward rule, implies(),
161
163
  // writes its premise first.
162
- if (kind !== 'backward') {
164
+ if (anonymous && kind !== 'backward') {
163
165
  body = body.map((goal) => renameAnonymous(goal, counter));
164
166
  head = renameAnonymous(head, counter);
165
- } else {
167
+ } else if (anonymous) {
166
168
  head = renameAnonymous(head, counter);
167
169
  body = body.map((goal) => renameAnonymous(goal, counter));
168
170
  }
@@ -322,7 +324,7 @@ function exportsFor(io) {
322
324
  }
323
325
  // Each fact is stated by this call, so it records the call's line, as
324
326
  // every clause records the line of the statement that states it.
325
- for (const [term] of terms) builder.add('fact', term, [], line);
327
+ for (const [term] of terms) builder.add('fact', term, [], line, false);
326
328
  },
327
329
  struct: (name, ...args) => {
328
330
  if (typeof name !== 'string') throw new EyemindError('struct() needs a name string first');
package/src/program.js CHANGED
@@ -4,9 +4,10 @@ import { containsTemplate, expandTemplates } from './templates.js';
4
4
  import { subgoals } from './goals.js';
5
5
  import { hasInspection, inspectionTerm } from './inspection.js';
6
6
  import {
7
- Env, EyemindError, Struct, Var, deref, flattenConjunction, freshTerm, identity, isCallable,
7
+ EMPTY, Env, EyemindError, Struct, Var, deref, flattenConjunction, freshTerm, identity, isCallable,
8
8
  isGround, isTerm, key, keyArity, unify, variables,
9
9
  } from './terms.js';
10
+ import { write } from './writer.js';
10
11
 
11
12
  export const CONTROL_KEYS = new Set(['and/2', 'or/2', 'not/1', 'call/1', 'once/1', 'findall/3']);
12
13
  export const RESERVED_KEYS = new Set(['step/4', 'clause/2', 'activated/2', 'implies/2', 'implied_by/2', 'forward/2', 'backward/2']);
@@ -134,6 +135,10 @@ export class Program {
134
135
  for (const [position, item] of heads.entries()) {
135
136
  if (templatePositions.has(position)) continue;
136
137
  if (!isCallable(item)) throw new EyemindError(`line ${source.line}: a head must be an atom or a compound term`);
138
+ // A list goal is a conjunction, so a clause with a list head could never be called.
139
+ if (item === EMPTY || isTerm(item, '.', 2)) {
140
+ throw new EyemindError(`line ${source.line}: a head cannot be a list; name the relation, such as items(${write(item)})`);
141
+ }
137
142
  const itemKey = keys.of(item);
138
143
  headKey = itemKey;
139
144
  if (allowed.has(itemKey)) continue;
package/src/proof.js CHANGED
@@ -339,7 +339,7 @@ function readDocument(program, document, fail, spellings) {
339
339
  const [goal, by, bindingList, useList] = term.args;
340
340
  const bindings = properListItems(bindingList, empty);
341
341
  const uses = properListItems(useList, empty);
342
- if (bindings === null || uses === null) {
342
+ if (bindings === null || uses === null || !bindings.every((pair) => isTerm(pair, '=', 2))) {
343
343
  fail.add('C3', 'step bindings must be an object and uses an array', goal);
344
344
  continue;
345
345
  }
@@ -660,15 +660,16 @@ function checkBoundaries(program, steps, boundaries, fail) {
660
660
  const solved = parts.every((part, i) => shown[i].some((item) => unify(freshTerm(part, `absent${nextSerial()}`), item, new Env())));
661
661
  if (solved) fail.add('C6', `absence contradicted by evidence: ${text(boundary)}`, boundary);
662
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++;
663
667
  const items = properListItems(boundary.args[2], new Env());
664
668
  if (items === null) {
665
669
  fail.add('C6', `collected result is not a proper list: ${text(boundary)}`, boundary);
666
670
  continue;
667
671
  }
668
- const parts = flattenConjunction(boundary.args[1]);
669
- const shown = parts.length === 1 ? evidence(parts[0]) : null;
670
672
  if (shown === null) continue;
671
- confronted++;
672
673
  for (const item of shown) {
673
674
  const names = new Map();
674
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).
@@ -80,6 +79,7 @@ class Tokens {
80
79
  if (digits.length > 1 && digits[0] === '0') throw this.error(`an integer has no leading zero: ${digits}`, line);
81
80
  return { kind: 'int', text: digits, line };
82
81
  }
82
+ if (/^0\d/.test(match[2])) throw this.error(`a number has no leading zero: ${match[2]}`, line);
83
83
  return { kind: 'float', text: match[2], line };
84
84
  } else if (isNameStart(code)) {
85
85
  const start = pos;
@@ -222,7 +222,7 @@ export class Reader {
222
222
  else throw tokens.error('a minus sign applies only to a number', token.line);
223
223
  state = 'after';
224
224
  } else if (token.kind === 'name') {
225
- if (RESERVED_WORDS.has(token.text) || JS_VALUES.has(token.text) || token.text.includes('$')) {
225
+ if (RESERVED_WORDS.has(token.text) || token.text.includes('$')) {
226
226
  throw tokens.error(`${token.text} is not a term`, token.line);
227
227
  }
228
228
  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.3';
2
+ export const VERSION = '0.2.5';
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 JavaScript expression cannot use as a plain name.
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',