eyemind 0.5.24 → 0.5.25
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 +5 -5
- package/SPEC.md +31 -19
- package/package.json +1 -1
- package/src/chains.js +2 -2
- package/src/closure.js +27 -8
- package/src/engine.js +5 -4
- package/src/fidelity.js +3 -3
- package/src/goals.js +3 -3
- package/src/lang.js +1 -0
- package/src/n3.js +14 -1
- package/src/program.js +4 -4
- package/src/proof.js +3 -3
- package/src/srl.js +3 -3
- package/src/templates.js +2 -2
- package/src/terms.js +16 -3
- package/src/version.js +1 -1
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ eyemind is a **JavaScript rule language** with forward and backward reasoning an
|
|
|
7
7
|
|
|
8
8
|
[Playground](https://eyereasoner.github.io/eyemind/playground/) · [Specification](https://eyereasoner.github.io/eyemind/SPEC) · [Conformance](https://eyereasoner.github.io/eyemind/conformance/) · [Examples](https://eyereasoner.github.io/eyemind/examples/)
|
|
9
9
|
|
|
10
|
-
The playground runs programs in your browser; the specification defines the language, its canonical text, the proof format and the C1–C10 checks. The examples are 135 runnable programs, each with a card deck and saved conclusions, proofs and check reports. The conformance suite has
|
|
10
|
+
The playground runs programs in your browser; the specification defines the language, its canonical text, the proof format and the C1–C10 checks. The examples are 135 runnable programs, each with a card deck and saved conclusions, proofs and check reports. The conformance suite has 1388 cases that test any implementation of the specification through its command line; run it [in your browser](https://eyereasoner.github.io/eyemind/playground/conformance).
|
|
11
11
|
|
|
12
12
|
## Contents
|
|
13
13
|
|
|
@@ -125,7 +125,7 @@ Now add `fact(parent('carol', 'dave'))`, or remove a rule, and watch which answe
|
|
|
125
125
|
| `contradiction(...Body)` | Stop with exit code 65 when `Body` holds: an integrity constraint. |
|
|
126
126
|
| `facts_from(path)`, `facts_from({text})` | State every expression of a document as a fact, without running it. |
|
|
127
127
|
|
|
128
|
-
**An array is a conjunction:** `implies([p(X), q(X)], [r(X), s(X)])`. Disjunction is `or(A, B)`, negation is `not(G)`, and collection is `findall(T, G, L)`, alongside `call` and `once`.
|
|
128
|
+
**An array is a conjunction:** `implies([p(X), q(X)], [r(X), s(X)])`. Disjunction is `or(A, B)`, negation is `not(G)`, and collection is `findall(T, G, L)`, or `findall_sorted(T, G, L)` for the same list in the standard order of terms, alongside `call` and `once`.
|
|
129
129
|
|
|
130
130
|
**Text is atoms:** `atom_chars`, `atom_concat` and `atom_length` take them apart, `atom_lower` and `atom_upper` change their case, `regex_match(P, T)`, `regex_find(P, T, Groups)` and `regex_replace(P, T, R, U)` search them with ECMAScript regular expressions, `digest('sha256', A, Hex)` hashes them, and `term_text(T, A)` writes a ground term as its canonical text.
|
|
131
131
|
|
|
@@ -205,7 +205,7 @@ eyemind --program examples/family-n3.n3 > /tmp/family.js # the compiled program,
|
|
|
205
205
|
eyemind -c examples/family-n3.n3 # a proof of it, checked
|
|
206
206
|
```
|
|
207
207
|
|
|
208
|
-
Triples are `rdf(Subject, Predicate, Object)`, with IRIs, blank nodes and literals written as in the RDF table above. N3 integers and decimals become eyemind numbers; float and double literals retain their RDF datatype, including numbers written with an exponent. Decimals use binary64 storage; `math:sum` adds their canonical decimal values before rounding its result. `=>` becomes `implies`, or `restricted` when the conclusion has blank nodes of its own, `<=` becomes `implied_by`, `{ ... } => false` becomes `contradiction`, and scoped negation and collection become `not` and `findall
|
|
208
|
+
Triples are `rdf(Subject, Predicate, Object)`, with IRIs, blank nodes and literals written as in the RDF table above. N3 integers and decimals become eyemind numbers; float and double literals retain their RDF datatype, including numbers written with an exponent. Decimals use binary64 storage; `math:sum` adds their canonical decimal values before rounding its result. `=>` becomes `implies`, or `restricted` when the conclusion has blank nodes of its own, `<=` becomes `implied_by`, `{ ... } => false` becomes `contradiction`, and scoped negation and collection become `not` and `findall` (`findall_sorted` when the collected list is only counted with `list:length` or added with `math:sum`), so an N3 run has a proof that eyemind checks like any other. Derived rules can trigger inference fuses too.
|
|
209
209
|
|
|
210
210
|
Quoted formulas are graph terms, read as in D. Arndt and P.-A. Champin,
|
|
211
211
|
["Notation3: Graph Terms for RDF"](https://arxiv.org/abs/2610.31266).
|
|
@@ -347,7 +347,7 @@ eyemind --all rules.srl facts.ttl # the base graph too
|
|
|
347
347
|
eyemind -c rules.srl facts.ttl # a proof of it, checked
|
|
348
348
|
```
|
|
349
349
|
|
|
350
|
-
Triples are `rdf(Subject, Predicate, Object)`, with `base/3` marking the base graph and `inf/3` the inference graph. A rule becomes `implies`, `NOT` becomes `not` of a helper, `GROUP` becomes `
|
|
350
|
+
Triples are `rdf(Subject, Predicate, Object)`, with `base/3` marking the base graph and `inf/3` the inference graph. A rule becomes `implies`, `NOT` becomes `not` of a helper, `GROUP` becomes `findall_sorted`, which leaves aggregates independent of derivation order, a blank node in a rule head becomes the Skolem term of its firing, and a SPARQL error is a goal that fails, so `FILTER` rejects the solution and `SET` drops it. The W3C SPARQL 1.2 RL test suite runs in `npm test` from `.cache/w3c/sparql-rl`: all 241 tests pass.
|
|
351
351
|
|
|
352
352
|
SRL integers and decimals use exact integer arithmetic. Decimal addition,
|
|
353
353
|
subtraction, multiplication, comparison, rounding and terminating division
|
|
@@ -480,7 +480,7 @@ A negation `not(G)` concludes from what is *absent* and a collection `findall(T,
|
|
|
480
480
|
- **Negate only what is bound.** The variables of `not(G)` must be bound when it runs; write `not(q(X, _))` for "no value at all".
|
|
481
481
|
- **Check the search, not just its result.** A proof ends with a search certificate: `searched(Call, Answers)` lists each call's answers and `derived(Pattern, Facts)` each consulted relation. The checker expands each call one level against the program, so a wrong table shows up as a disagreement.
|
|
482
482
|
- **Say what was taken on trust.** Anything the certificate cannot settle becomes an obligation, and evidence in the proof can still refute it (C6).
|
|
483
|
-
- **Certify the original questions.** Supported, closed listings establish the original calls, and that the claims are all their answers, when no rule they depend on can observe bindings or evaluation order. A mode analysis checks this: primitives such as arithmetic run with their inputs bound, negations with their variables bound, and `once` and `findall` bind nothing other goals use. Where it does not apply, as for tests of whether a variable is bound, the checker replays each derived fact's recorded firing in source order instead. A proof within these bounds can be `checked` with no obligation, even under `--strict-proof`. Rules generated by templates are checked through their instances.
|
|
483
|
+
- **Certify the original questions.** Supported, closed listings establish the original calls, and that the claims are all their answers, when no rule they depend on can observe bindings or evaluation order. A mode analysis checks this: primitives such as arithmetic run with their inputs bound, negations with their variables bound, and `once` and `findall` bind nothing other goals use. Where it does not apply, as for tests of whether a variable is bound, the checker replays each derived fact's recorded firing in source order instead. A proof within these bounds can be `checked` with no obligation, even under `--strict-proof`. Rules generated by templates are checked through their instances. A `findall_sorted` of ground items is certified from the set of derived facts alone. `findall` and first answers over derived facts, whose order a listing cannot show, and `restricted` rules with new witnesses keep an obligation.
|
|
484
484
|
|
|
485
485
|
## Checking the translation
|
|
486
486
|
|
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.
|
|
5
|
+
Version: eyemind 0.5.25
|
|
6
6
|
```
|
|
7
7
|
|
|
8
8
|
This document specifies the syntax and meaning of eyemind programs and data
|
|
@@ -153,9 +153,10 @@ Programs:
|
|
|
153
153
|
- **Head** and **body**: the conclusions of a clause, and its premises.
|
|
154
154
|
- **Goal**: an atom, a compound, a variable bound to one of them, or a
|
|
155
155
|
conjunction of goals, to be solved ([Section 4.1](#41-goal-syntax)).
|
|
156
|
-
- **Control**: `and`, `or`, `not`, `call`, `once
|
|
157
|
-
A **negation** is a `not(G)`
|
|
158
|
-
`findall(T, G, L)`
|
|
156
|
+
- **Control**: `and`, `or`, `not`, `call`, `once`, `findall` or
|
|
157
|
+
`findall_sorted` ([Section 4.2](#42-controls)). A **negation** is a `not(G)`
|
|
158
|
+
goal and a **collection** a `findall(T, G, L)` or `findall_sorted(T, G, L)`
|
|
159
|
+
goal.
|
|
159
160
|
- **Primitive**: a built-in goal such as `is` or `atom_concat` ([Section 4.3](#43-primitives)).
|
|
160
161
|
- **Rank** and **stratum**: a clause's rank is at least that of every
|
|
161
162
|
clause it depends on, and above that of every clause a negation or
|
|
@@ -501,7 +502,7 @@ Array-to-conjunction notation applies in these positions:
|
|
|
501
502
|
|
|
502
503
|
- a rule body and each argument of `query` or `contradiction`;
|
|
503
504
|
- each argument of `and`, `or`, `not`, `call` and `once`;
|
|
504
|
-
- the second argument of `findall`;
|
|
505
|
+
- the second argument of `findall` and `findall_sorted`;
|
|
505
506
|
- both arguments of a rule template or source inspection pattern.
|
|
506
507
|
|
|
507
508
|
It also applies to a forward head, where the conjunction means several
|
|
@@ -530,6 +531,7 @@ primitive, inspection form nor any clause head has no solution.
|
|
|
530
531
|
| `call(G)` | `call(G)` | The solutions of `G`. |
|
|
531
532
|
| `once(G)` | `once(G)` | The first solution of `G`. |
|
|
532
533
|
| `findall(T, G, L)` | `findall(T, G, L)` | Unifies `L` with the list of instances of `T`, one per solution of `G`, in order, each renamed apart. |
|
|
534
|
+
| `findall_sorted(T, G, L)` | `findall_sorted(T, G, L)` | As `findall`, with the list sorted in the standard order ([Section 2.4](#24-standard-order)), any two variables comparing equal. Items that compare equal keep their search order; repeated items are kept. |
|
|
533
535
|
|
|
534
536
|
Anonymous variables are `_` occurrences and source variables named `_`
|
|
535
537
|
followed by digits ([Section 3.3](#33-term-construction)), including their renamed-apart copies.
|
|
@@ -745,7 +747,7 @@ Nested `implies(Body, Head)` and `implied_by(Head, Body)` calls construct rule
|
|
|
745
747
|
templates. A rule value in a conclusion MUST occur as a top-level conjunct of
|
|
746
748
|
an `implies` conclusion; in a body, `implies` and `implied_by` are inspection
|
|
747
749
|
goals ([Section 6.3](#63-source-rule-inspection)). A rule value anywhere else in a clause, such as an
|
|
748
|
-
argument of an ordinary head or goal, or the template or result of
|
|
750
|
+
argument of an ordinary head or goal, or the template or result of a collection,
|
|
749
751
|
MUST be rejected. The conclusion MAY combine ordinary heads and several
|
|
750
752
|
templates. Each template's
|
|
751
753
|
body and head MAY themselves be conjunctions, written as arrays. A backward
|
|
@@ -881,7 +883,8 @@ not chain-stratified. A rule has:
|
|
|
881
883
|
- **positive goals**: its body goals other than primitives, negations,
|
|
882
884
|
collections and `'true'`, and other than a restricted rule's guard
|
|
883
885
|
([Section 7.2](#72-variables-and-witnesses));
|
|
884
|
-
- **negated groups**: for each `not(G)` and `findall(T, G, L)`
|
|
886
|
+
- **negated groups**: for each `not(G)` and collection `findall(T, G, L)` or
|
|
887
|
+
`findall_sorted(T, G, L)` body goal,
|
|
885
888
|
the goals of the conjunction `G` other than primitives; a group with a
|
|
886
889
|
control or a variable makes the program not chain-stratified, and an
|
|
887
890
|
empty group is left out;
|
|
@@ -1068,6 +1071,9 @@ search solutions, including repeated derivations and nonground instances;
|
|
|
1068
1071
|
its meaning is not determined by a set of ground atoms alone. Bindings
|
|
1069
1072
|
made while solving its goal do not escape except through the copied result
|
|
1070
1073
|
list. Clause order and duplicate source clauses can affect that list.
|
|
1074
|
+
`findall_sorted` collects the same solutions in the standard order, so the
|
|
1075
|
+
order in which they were found decides its list only among items equal up to
|
|
1076
|
+
their variables.
|
|
1071
1077
|
|
|
1072
1078
|
### 7.5 Sets and operational behavior
|
|
1073
1079
|
|
|
@@ -1282,7 +1288,7 @@ differently; they are ignored when the output is read ([Section 9.1](#91-accepte
|
|
|
1282
1288
|
When a proof is requested, each solution records how each of its goals was
|
|
1283
1289
|
justified: by a fact or rule of the program, by a derived fact (whose own
|
|
1284
1290
|
record is kept), by a primitive, by a control, by an absence (a negation) or
|
|
1285
|
-
by a collection
|
|
1291
|
+
by a collection. For each conclusion the first derivation found is
|
|
1286
1292
|
recorded. Each original question gets call-fidelity evidence (Sections [10.5](#105-call-fidelity-certificates) and [11.13](#1113-c10-call-fidelity)),
|
|
1287
1293
|
when supported. Each negation and collection of a proof gets the search certificate
|
|
1288
1294
|
that settles it (Sections [10.4](#104-search-certificates) and [11.12](#1112-c9-search-closure)) whenever [Section 11.12](#1112-c9-search-closure) allows one. A
|
|
@@ -1500,7 +1506,7 @@ order. `By` is one of:
|
|
|
1500
1506
|
| `called(Called)` | `Goal` is a state test ([Section 8.7](#87-proof-generation)) that held as `Called`, the goal when it ran, its then unbound variables written as variables. | empty | empty |
|
|
1501
1507
|
| `'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 |
|
|
1502
1508
|
| `'absent'` | `Goal` is a negation, settled by the search certificate or taken on trust. | empty | empty |
|
|
1503
|
-
| `'collected'` | `Goal` is a
|
|
1509
|
+
| `'collected'` | `Goal` is a collection, settled by the search certificate or taken on trust. | empty | empty |
|
|
1504
1510
|
|
|
1505
1511
|
`Bindings` is an object from each variable name of the cited clause, as
|
|
1506
1512
|
the program wrote it, to its value in this step: `{X: 'socrates'}`. A key
|
|
@@ -1811,7 +1817,7 @@ forms MUST have no bindings and no uses:
|
|
|
1811
1817
|
| `'builtin'` | A primitive |
|
|
1812
1818
|
| `called(Called)` | A state test |
|
|
1813
1819
|
| `'absent'` | A negation `not(G)` |
|
|
1814
|
-
| `'collected'` | A collection `findall(T, G, L)` |
|
|
1820
|
+
| `'collected'` | A collection `findall(T, G, L)` or `findall_sorted(T, G, L)` |
|
|
1815
1821
|
|
|
1816
1822
|
A well-formed negation or collection step is called a **boundary**. A step
|
|
1817
1823
|
that fails C3 is not a trusted boundary: it counts in neither `trusted`, C6
|
|
@@ -1885,7 +1891,7 @@ it does not establish absence.
|
|
|
1885
1891
|
conjunction, whose every goal has evidence. It fails C6 when every goal of
|
|
1886
1892
|
`G` unifies with some of its evidence; the anonymous variables of `G`
|
|
1887
1893
|
unify with anything.
|
|
1888
|
-
- A collection `findall(T, G, L)` fails C6 when `L` is not a list, and is
|
|
1894
|
+
- A collection `findall(T, G, L)` or `findall_sorted(T, G, L)` fails C6 when `L` is not a list, and is
|
|
1889
1895
|
decided when `G` is a single goal with evidence. It fails C6 when an
|
|
1890
1896
|
instance of `T` for some evidence of `G` unifies with no element of `L`.
|
|
1891
1897
|
|
|
@@ -2077,13 +2083,18 @@ requires:
|
|
|
2077
2083
|
`T`, one per solution of `G`, in order, each renamed apart. Repeated
|
|
2078
2084
|
solutions remain repeated list elements. The instance test allows later
|
|
2079
2085
|
goals to have bound variables in the copied results.
|
|
2086
|
+
- For `findall_sorted(T, G, L)`, `L` is an instance of that list sorted as
|
|
2087
|
+
[Section 4.2](#42-controls) specifies.
|
|
2080
2088
|
|
|
2081
2089
|
**Certificate limits.** Settlement also requires all of the following:
|
|
2082
2090
|
|
|
2083
|
-
- No
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2091
|
+
- No `findall`, `once`, or `findall_sorted` collecting an item that is not
|
|
2092
|
+
ground, in the boundary's search, a relied-on table or a closure check,
|
|
2093
|
+
uses a table whose answers rest on a forward listing. This format checks
|
|
2094
|
+
support and closure of derived facts, but cannot establish their
|
|
2095
|
+
publication order for order-sensitive operations. A `findall_sorted` of
|
|
2096
|
+
ground items depends on the solutions and how often each occurs, not on
|
|
2097
|
+
their order.
|
|
2087
2098
|
- The search reaches no source inspection.
|
|
2088
2099
|
- No consulted listing's relation is concluded by a forward rule within a
|
|
2089
2100
|
template's scope ([Section 6.2](#62-inline-rule-templates)) that
|
|
@@ -2177,7 +2188,7 @@ with these differences:
|
|
|
2177
2188
|
2. Negation MUST enforce [Section 4.2](#42-controls)'s binding restriction at the actual
|
|
2178
2189
|
call. C9's treatment of an unbound named variable as anonymous MUST NOT
|
|
2179
2190
|
be used. A source search that raises an error fails its certificate.
|
|
2180
|
-
3. `once` and
|
|
2191
|
+
3. `once` and the collections keep their [Section 4.2](#42-controls) meanings, so the first answer
|
|
2181
2192
|
or collected answers come from the actual inner call. [Section 11.12](#1112-c9-search-closure)'s
|
|
2182
2193
|
restriction on order-sensitive uses of derived listings still applies.
|
|
2183
2194
|
The order of unrelated answers of an ordinary question need not be
|
|
@@ -2234,8 +2245,9 @@ with the variables grounded before it:
|
|
|
2234
2245
|
- `once(G)` grounds nothing. `G` MUST satisfy these conditions. Each named
|
|
2235
2246
|
variable of `G` that occurs elsewhere in the clause MUST be grounded before
|
|
2236
2247
|
it, so the choice of a first answer binds nothing another goal uses.
|
|
2237
|
-
- `findall(T, G, L)`
|
|
2238
|
-
|
|
2248
|
+
- A collection `findall(T, G, L)` or `findall_sorted(T, G, L)` follows the
|
|
2249
|
+
same rule for `T` and `G`. It grounds the variables of `L` when `G`
|
|
2250
|
+
grounds every variable of `T`.
|
|
2239
2251
|
- A primitive whose variables are all grounded before it is a test, and
|
|
2240
2252
|
grounds nothing. `unify/2` with one side ground grounds the other, and
|
|
2241
2253
|
with neither ground grounds nothing. Otherwise the primitive MUST be
|
|
@@ -2281,7 +2293,7 @@ step's corresponding use records:
|
|
|
2281
2293
|
- A primitive runs on the bindings of that point of the search. Its first
|
|
2282
2294
|
solution under which the goal can still unify with the use is taken.
|
|
2283
2295
|
`compare/3` with a nonground compared term does not replay.
|
|
2284
|
-
- `once/1`, `not/1` and `
|
|
2296
|
+
- `once/1`, `not/1`, `findall/3` and `findall_sorted/3` take the answers of the goal, as it
|
|
2285
2297
|
stands at that point, from the call tables and listings, as in checking
|
|
2286
2298
|
the original calls. A negation with an unbound named variable does not
|
|
2287
2299
|
replay, and neither does a first answer or a collection whose order rests
|
package/package.json
CHANGED
package/src/chains.js
CHANGED
|
@@ -119,12 +119,12 @@ function ruleOf(clause) {
|
|
|
119
119
|
if (goal === 'true') continue;
|
|
120
120
|
if (!isCallable(goal)) return null;
|
|
121
121
|
const goalKey = key(goal);
|
|
122
|
-
if (goalKey === 'not/1' || goalKey === 'findall/3') {
|
|
122
|
+
if (goalKey === 'not/1' || goalKey === 'findall/3' || goalKey === 'findall_sorted/3') {
|
|
123
123
|
const group = [];
|
|
124
124
|
for (const inner of flattenConjunction(goalKey === 'not/1' ? goal.args[0] : goal.args[1])) {
|
|
125
125
|
if (inner instanceof Var || !isCallable(inner)) return null;
|
|
126
126
|
if (PRIMITIVE_KEYS.has(key(inner))) continue;
|
|
127
|
-
if (['not/1', 'findall/3', 'or/2', 'once/1', 'call/1'].includes(key(inner))) return null;
|
|
127
|
+
if (['not/1', 'findall/3', 'findall_sorted/3', 'or/2', 'once/1', 'call/1'].includes(key(inner))) return null;
|
|
128
128
|
group.push(inner);
|
|
129
129
|
}
|
|
130
130
|
if (group.length) negated.push(group);
|
package/src/closure.js
CHANGED
|
@@ -20,7 +20,7 @@ import { freshClause, 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,
|
|
23
|
-
isGroundButAnonymous, listFromItems, unify, variables,
|
|
23
|
+
isCollection, isGroundButAnonymous, listFromItems, sortCollected, unify, variables,
|
|
24
24
|
} from './terms.js';
|
|
25
25
|
|
|
26
26
|
// A search the certificate cannot vouch for: its boundary stays an obligation.
|
|
@@ -92,10 +92,13 @@ export class Settler {
|
|
|
92
92
|
// only failure is the boundary's own result, and the tables and listings it
|
|
93
93
|
// rests on.
|
|
94
94
|
settle(boundary) {
|
|
95
|
-
const collecting =
|
|
95
|
+
const collecting = isCollection(boundary);
|
|
96
|
+
const sorting = isTerm(boundary, 'findall_sorted', 3);
|
|
96
97
|
const answering = isTerm(boundary, 'answers', 1);
|
|
97
98
|
const goal = collecting ? boundary.args[1] : boundary.args[0];
|
|
98
|
-
|
|
99
|
+
// A sorted collection of ground items depends on its answers, not their
|
|
100
|
+
// order, so it does not need the order a listing cannot show.
|
|
101
|
+
const root = new Reliance(collecting && !sorting);
|
|
99
102
|
const outcome = { status: 'settled', failures: [], boundaryOnly: false, tables: [], listings: [] };
|
|
100
103
|
let found = [];
|
|
101
104
|
try {
|
|
@@ -112,6 +115,11 @@ export class Settler {
|
|
|
112
115
|
outcome.failures.push([`search certificate leads to an error: ${error.message}`, boundary]);
|
|
113
116
|
found = null;
|
|
114
117
|
}
|
|
118
|
+
if (sorting && found !== null) {
|
|
119
|
+
// Items equal up to their variables keep their search order.
|
|
120
|
+
if (!found.every((item) => isGround(item))) for (const item of root.tables) root.ordered.add(item);
|
|
121
|
+
found = sortCollected(found);
|
|
122
|
+
}
|
|
115
123
|
let unsettled = false;
|
|
116
124
|
const tables = new Set();
|
|
117
125
|
const listings = new Set();
|
|
@@ -188,15 +196,26 @@ export class Settler {
|
|
|
188
196
|
inner.return();
|
|
189
197
|
env.undo(mark);
|
|
190
198
|
if (!once && first.done) yield;
|
|
191
|
-
} else if (goalKey === 'findall/3') {
|
|
199
|
+
} else if (goalKey === 'findall/3' || goalKey === 'findall_sorted/3') {
|
|
200
|
+
const sorting = goalKey === 'findall_sorted/3';
|
|
192
201
|
const items = [];
|
|
193
|
-
|
|
194
|
-
|
|
202
|
+
// A sorted collection relies on the order of its tables only when an
|
|
203
|
+
// item has variables, which can make items equal up to their variables.
|
|
204
|
+
const inner = sorting ? new Reliance() : reliance;
|
|
205
|
+
if (!sorting) reliance.inner++;
|
|
206
|
+
for (const _ of this.solve(goal.args[1], env, inner)) { // eslint-disable-line no-unused-vars
|
|
195
207
|
items.push(freshTerm(copyResolved(goal.args[0], env), `collection${++this.serial}`));
|
|
196
208
|
}
|
|
197
|
-
|
|
209
|
+
if (sorting) {
|
|
210
|
+
for (const item of inner.tables) reliance.tables.add(item);
|
|
211
|
+
for (const item of inner.listings) reliance.listings.add(item);
|
|
212
|
+
for (const item of inner.ordered) reliance.ordered.add(item);
|
|
213
|
+
if (!items.every((item) => isGround(item))) for (const item of inner.tables) reliance.ordered.add(item);
|
|
214
|
+
} else {
|
|
215
|
+
reliance.inner--;
|
|
216
|
+
}
|
|
198
217
|
env.undo(mark);
|
|
199
|
-
if (unify(goal.args[2], listFromItems(items), env)) yield;
|
|
218
|
+
if (unify(goal.args[2], listFromItems(sorting ? sortCollected(items) : items), env)) yield;
|
|
200
219
|
} else if (PRIMITIVE_KEYS.has(goalKey)) {
|
|
201
220
|
// Relative names of unbound variables are not preserved by alpha
|
|
202
221
|
// renaming of a table. A name-dependent comparison needs more evidence.
|
package/src/engine.js
CHANGED
|
@@ -10,7 +10,7 @@ import { CONTROL_KEYS, Program } from './program.js';
|
|
|
10
10
|
import {
|
|
11
11
|
Env, EyemindError, Struct, Var, conjunction, copyResolved, deref, flattenConjunction, freshTerm,
|
|
12
12
|
identity, indexKey, isAnonymous, isCallable, isGround, isGroundButAnonymous, isTerm, key, keyArity, listFromItems,
|
|
13
|
-
unify, variables,
|
|
13
|
+
sortCollected, unify, variables,
|
|
14
14
|
} from './terms.js';
|
|
15
15
|
import { Lettering, SourceNaming, write } from './writer.js';
|
|
16
16
|
|
|
@@ -433,7 +433,7 @@ export class Solver {
|
|
|
433
433
|
if (!absent) return null;
|
|
434
434
|
return this.advance(frame, recording ? new Node(goal, ABSENT) : null);
|
|
435
435
|
}
|
|
436
|
-
if (goalKey === 'findall/3') {
|
|
436
|
+
if (goalKey === 'findall/3' || goalKey === 'findall_sorted/3') {
|
|
437
437
|
const items = [];
|
|
438
438
|
const scoped = [];
|
|
439
439
|
const uses = recording ? new Set() : null;
|
|
@@ -450,7 +450,7 @@ export class Solver {
|
|
|
450
450
|
if (recording) clausesUsed(nodes, uses);
|
|
451
451
|
}
|
|
452
452
|
env.undo(mark);
|
|
453
|
-
if (!unify(goal.args[2], listFromItems(items), env)) {
|
|
453
|
+
if (!unify(goal.args[2], listFromItems(goalKey === 'findall/3' ? items : sortCollected(items)), env)) {
|
|
454
454
|
env.undo(mark);
|
|
455
455
|
return null;
|
|
456
456
|
}
|
|
@@ -713,7 +713,8 @@ export class Solver {
|
|
|
713
713
|
// and stratification rejects variables a forward rule can reach.
|
|
714
714
|
for (const goal of clause.localBody) {
|
|
715
715
|
const goalKey = key(goal);
|
|
716
|
-
if (PRIMITIVE_KEYS.has(goalKey) || INSPECTION_KEYS.has(goalKey) || goalKey === 'not/1' || goalKey === 'findall/3'
|
|
716
|
+
if (PRIMITIVE_KEYS.has(goalKey) || INSPECTION_KEYS.has(goalKey) || goalKey === 'not/1' || goalKey === 'findall/3' ||
|
|
717
|
+
goalKey === 'findall_sorted/3') {
|
|
717
718
|
keys.push(null);
|
|
718
719
|
} else if (CONTROL_KEYS.has(goalKey) || (this.program.groups.get(goalKey) || []).some((item) => item.body.length)) {
|
|
719
720
|
keys = null;
|
package/src/fidelity.js
CHANGED
|
@@ -8,7 +8,7 @@ import { goalTerm } from './goals.js';
|
|
|
8
8
|
import { INSPECTION_KEYS } from './inspection.js';
|
|
9
9
|
import {
|
|
10
10
|
Env, EyemindError, Struct, Var, conjunction, copyResolved, deref, flattenConjunction, freshTerm, identity, isAnonymous, isCallable,
|
|
11
|
-
isGround, isTerm, key, listFromItems, unify, variables,
|
|
11
|
+
isCollection, isGround, isTerm, key, listFromItems, unify, variables,
|
|
12
12
|
} from './terms.js';
|
|
13
13
|
import { Lettering, write } from './writer.js';
|
|
14
14
|
|
|
@@ -146,7 +146,7 @@ function groundedListings(program, listings, loose) {
|
|
|
146
146
|
const inner = bound(goal.args[0], grounded, negated);
|
|
147
147
|
return inner === null || shared(goal).some((name) => !grounded.has(name)) ? null : grounded;
|
|
148
148
|
}
|
|
149
|
-
if (
|
|
149
|
+
if (isCollection(goal)) {
|
|
150
150
|
const inner = bound(goal.args[1], grounded, negated);
|
|
151
151
|
const collected = new Struct('-', [goal.args[0], goal.args[1]]);
|
|
152
152
|
if (inner === null || shared(collected).some((name) => !grounded.has(name))) return null;
|
|
@@ -257,7 +257,7 @@ export function replayed(program, settler, listingKeys, stepOf) {
|
|
|
257
257
|
});
|
|
258
258
|
return side !== undefined && all(flattenConjunction(goal.args[side]), step.uses, env);
|
|
259
259
|
}
|
|
260
|
-
if (['once/1', 'not/1', 'findall/3'].includes(goalKey)) {
|
|
260
|
+
if (['once/1', 'not/1', 'findall/3', 'findall_sorted/3'].includes(goalKey)) {
|
|
261
261
|
const outcome = settler.answers(copyResolved(goal, env));
|
|
262
262
|
if (outcome.status !== 'settled' || !outcome.answers.length) return false;
|
|
263
263
|
return unify(goal, fresh(outcome.answers[0]), env) && compatible(goal, use, env);
|
package/src/goals.js
CHANGED
|
@@ -15,13 +15,13 @@ export const RULE_NAMES = new Set(['implies', 'implied_by', 'forward', 'backward
|
|
|
15
15
|
|
|
16
16
|
// The goal positions directly inside a control goal, or null when the goal is
|
|
17
17
|
// not a control: both sides of and and or, the argument of not, call and
|
|
18
|
-
// once, and the goal of findall.
|
|
18
|
+
// once, and the goal of findall and findall_sorted.
|
|
19
19
|
export function subgoals(goal) {
|
|
20
20
|
if (!(goal instanceof Struct)) return null;
|
|
21
21
|
const { name, args } = goal;
|
|
22
22
|
if ((name === 'and' || name === 'or') && args.length === 2) return args;
|
|
23
23
|
if ((name === 'not' || name === 'call' || name === 'once') && args.length === 1) return args;
|
|
24
|
-
if (name === 'findall' && args.length === 3) return [args[1]];
|
|
24
|
+
if ((name === 'findall' || name === 'findall_sorted') && args.length === 3) return [args[1]];
|
|
25
25
|
return null;
|
|
26
26
|
}
|
|
27
27
|
|
|
@@ -48,7 +48,7 @@ export function normalizeGoal(term) {
|
|
|
48
48
|
if ((name === 'not' || name === 'call' || name === 'once') && args.length === 1) {
|
|
49
49
|
return rebuild(term, [normalizeGoal(args[0])]);
|
|
50
50
|
}
|
|
51
|
-
if (name === 'findall' && args.length === 3) {
|
|
51
|
+
if ((name === 'findall' || name === 'findall_sorted') && args.length === 3) {
|
|
52
52
|
return rebuild(term, [args[0], normalizeGoal(args[1]), args[2]]);
|
|
53
53
|
}
|
|
54
54
|
return term;
|
package/src/lang.js
CHANGED
|
@@ -363,6 +363,7 @@ function exportsFor(io) {
|
|
|
363
363
|
call: (...goals) => new Struct('call', [joined(goals)]),
|
|
364
364
|
once: (...goals) => new Struct('once', [joined(goals)]),
|
|
365
365
|
findall: (template, goal, result) => new Struct('findall', [toTerm(template), goalOf(goal), toTerm(result)]),
|
|
366
|
+
findall_sorted: (template, goal, result) => new Struct('findall_sorted', [toTerm(template), goalOf(goal), toTerm(result)]),
|
|
366
367
|
fail: 'fail',
|
|
367
368
|
eyemind: LIBRARY,
|
|
368
369
|
};
|
package/src/n3.js
CHANGED
|
@@ -540,6 +540,18 @@ function compileRule(body, head, kind, needed, views = true, values = [], witnes
|
|
|
540
540
|
return keys;
|
|
541
541
|
};
|
|
542
542
|
const goals = (triples, negated) => triples.flatMap((x) => goal(x, negated));
|
|
543
|
+
// A collected list used only as the subject of list:length or math:sum
|
|
544
|
+
// gives the same count and sum in any order, so it is collected sorted,
|
|
545
|
+
// which a proof can certify over derived triples (a sum of doubles may
|
|
546
|
+
// round differently in another order).
|
|
547
|
+
const orderFree = (x, list) => {
|
|
548
|
+
if (list.t !== 'var' && list.t !== 'bnode') return false;
|
|
549
|
+
const [listKey] = keysOf(list);
|
|
550
|
+
if (headKeys.has(listKey)) return false;
|
|
551
|
+
return bodyTriples.every((y) => y === x || !keysOfTriple(y).has(listKey) ||
|
|
552
|
+
((y.p.v === `${LIST}length` || y.p.v === `${MATH}sum`) && y.s.t === list.t && keysOf(y.s).has(listKey) &&
|
|
553
|
+
!keysOf(y.o).has(listKey)));
|
|
554
|
+
};
|
|
543
555
|
// A body triple with variables, none of which another body triple, or an
|
|
544
556
|
// enclosing rule, can bind.
|
|
545
557
|
const unconstrained = (x) => {
|
|
@@ -619,7 +631,8 @@ function compileRule(body, head, kind, needed, views = true, values = [], witnes
|
|
|
619
631
|
try {
|
|
620
632
|
if (p === `${LOG}collectAllIn`) {
|
|
621
633
|
const [template, where, list] = x.s.items;
|
|
622
|
-
|
|
634
|
+
const collect = orderFree(x, list) ? 'findall_sorted' : 'findall';
|
|
635
|
+
return [`${collect}(${ctx.term(template)}, ${conj(goals(where.triples, negated))}, ${ctx.term(list)})`];
|
|
623
636
|
}
|
|
624
637
|
const [where, then] = x.s.items;
|
|
625
638
|
if (!negated) markAnonymous([where, then], x);
|
package/src/program.js
CHANGED
|
@@ -6,11 +6,11 @@ import { subgoals } from './goals.js';
|
|
|
6
6
|
import { hasInspection, inspectionTerm } from './inspection.js';
|
|
7
7
|
import {
|
|
8
8
|
EMPTY, Env, EyemindError, Struct, Var, conjunction, deref, flattenConjunction, freshTerm, identity, indexKey, isCallable,
|
|
9
|
-
isGround, isTerm, key, keyArity, listFromItems, unify, variables,
|
|
9
|
+
isCollection, isGround, isTerm, key, keyArity, listFromItems, unify, variables,
|
|
10
10
|
} from './terms.js';
|
|
11
11
|
import { write } from './writer.js';
|
|
12
12
|
|
|
13
|
-
export const CONTROL_KEYS = new Set(['and/2', 'or/2', 'not/1', 'call/1', 'once/1', 'findall/3']);
|
|
13
|
+
export const CONTROL_KEYS = new Set(['and/2', 'or/2', 'not/1', 'call/1', 'once/1', 'findall/3', 'findall_sorted/3']);
|
|
14
14
|
export const RESERVED_KEYS = new Set(['step/4', 'clause/2', 'searched/2', 'derived/2', 'called/2', 'activated/2', 'implies/2', 'implied_by/2', 'forward/2', 'backward/2']);
|
|
15
15
|
|
|
16
16
|
// The statements that state a forward rule, and how an error names them.
|
|
@@ -344,7 +344,7 @@ function bucketEntries(found) {
|
|
|
344
344
|
function dependencies(goal, closed = false, out = []) {
|
|
345
345
|
const inner = subgoals(goal);
|
|
346
346
|
if (inner !== null) {
|
|
347
|
-
const inside = closed || isTerm(goal, 'not', 1) ||
|
|
347
|
+
const inside = closed || isTerm(goal, 'not', 1) || isCollection(goal);
|
|
348
348
|
for (const part of inner) dependencies(part, inside, out);
|
|
349
349
|
} else if (goal instanceof Var || (isCallable(goal) && !PRIMITIVE_KEYS.has(key(goal)))) {
|
|
350
350
|
out.push([goal, closed]);
|
|
@@ -409,7 +409,7 @@ function restrains(first, second) {
|
|
|
409
409
|
|
|
410
410
|
// Whether a goal could contribute a closed dependency or a dynamic call.
|
|
411
411
|
function mayNeedStratifying(goal) {
|
|
412
|
-
if (goal instanceof Var || isTerm(goal, 'not', 1) ||
|
|
412
|
+
if (goal instanceof Var || isTerm(goal, 'not', 1) || isCollection(goal)) return true;
|
|
413
413
|
const inner = subgoals(goal);
|
|
414
414
|
return inner !== null && inner.some(mayNeedStratifying);
|
|
415
415
|
}
|
package/src/proof.js
CHANGED
|
@@ -21,7 +21,7 @@ import { CONTROL_KEYS, Program } from './program.js';
|
|
|
21
21
|
import { readProofTerms } from './native-proof.js';
|
|
22
22
|
import {
|
|
23
23
|
Env, EyemindError, Struct, Var, conjunction, copyResolved, deref, flattenConjunction, freshTerm,
|
|
24
|
-
isCallable, isFloat, isGround, isTerm, key, keyArity, keyName, listFromItems, properListItems, unify, variables,
|
|
24
|
+
isCallable, isCollection, isFloat, isGround, isTerm, key, keyArity, keyName, listFromItems, properListItems, unify, variables,
|
|
25
25
|
} from './terms.js';
|
|
26
26
|
import { Lettering, letterName, quote, write, writeNotingVariables } from './writer.js';
|
|
27
27
|
|
|
@@ -336,7 +336,7 @@ function settleBoundaries(program, steps, certificate, spellings) {
|
|
|
336
336
|
}
|
|
337
337
|
|
|
338
338
|
function isBoundary({ goal, by, bindings, uses }) {
|
|
339
|
-
return ((by === 'absent' && isTerm(goal, 'not', 1)) || (by === 'collected' &&
|
|
339
|
+
return ((by === 'absent' && isTerm(goal, 'not', 1)) || (by === 'collected' && isCollection(goal))) &&
|
|
340
340
|
!bindings.length && !uses.length;
|
|
341
341
|
}
|
|
342
342
|
|
|
@@ -545,7 +545,7 @@ function checkSteps(program, steps, allowTrusted, fail, spellings, name, outcome
|
|
|
545
545
|
} else {
|
|
546
546
|
tally.composed++;
|
|
547
547
|
}
|
|
548
|
-
} else if ((by === 'absent' && isTerm(goal, 'not', 1)) || (by === 'collected' &&
|
|
548
|
+
} else if ((by === 'absent' && isTerm(goal, 'not', 1)) || (by === 'collected' && isCollection(goal))) {
|
|
549
549
|
if (bindings.length || uses.length) {
|
|
550
550
|
fail.add('C3', 'trusted boundaries cannot have bindings or uses', goal);
|
|
551
551
|
continue;
|
package/src/srl.js
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
// NOT { P } not(srl_not_N(Bound...)), with
|
|
19
19
|
// implied_by(srl_not_N(Bound...), P)
|
|
20
20
|
// FILTER(E), SET(?v := E) goals that bind E's value
|
|
21
|
-
// GROUP BY ?k { P } [ ?a := F ]
|
|
21
|
+
// GROUP BY ?k { P } [ ?a := F ] findall_sorted over P, then the aggregate
|
|
22
22
|
//
|
|
23
23
|
// A blank node in a rule head is a variable only the conclusion mentions,
|
|
24
24
|
// so eyemind makes it the Skolem term of the firing; the extra conclusion
|
|
@@ -464,11 +464,11 @@ class RuleCompiler {
|
|
|
464
464
|
const distinct = this.fresh('Groups');
|
|
465
465
|
this.need('srl_distinct');
|
|
466
466
|
this.need('srl_member');
|
|
467
|
-
goals.push(`
|
|
467
|
+
goals.push(`findall_sorted([${clause.keys.map((k) => own.name(k)).join(', ')}], ${ownConj}, ${all})`,
|
|
468
468
|
`srl_distinct(${all}, ${distinct})`, `srl_member(${keys}, ${distinct})`);
|
|
469
469
|
}
|
|
470
470
|
const rows = this.fresh('Rows');
|
|
471
|
-
goals.push(`
|
|
471
|
+
goals.push(`findall_sorted([${distinctColumns.map((v) => inner.name(v)).join(', ')}], ${conj}, ${rows})`);
|
|
472
472
|
this.need('srl_aggregates');
|
|
473
473
|
this.need('srl_column');
|
|
474
474
|
for (const k of clause.keys) scope.bind(k);
|
package/src/templates.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { RULE_NAMES, normalizeGoal, subgoals } from './goals.js';
|
|
4
4
|
import { isInspection } from './inspection.js';
|
|
5
5
|
import {
|
|
6
|
-
EMPTY, EyemindError, Struct, Var, conjunction, flattenConjunction, isCallable, isTerm, listFromItems, variables,
|
|
6
|
+
EMPTY, EyemindError, Struct, Var, conjunction, flattenConjunction, isCallable, isCollection, isTerm, listFromItems, variables,
|
|
7
7
|
} from './terms.js';
|
|
8
8
|
|
|
9
9
|
const isTemplate = (term) => term instanceof Struct && term.args.length === 2 && RULE_NAMES.has(term.name);
|
|
@@ -26,7 +26,7 @@ function checkGoal(goal, line, generated = false) {
|
|
|
26
26
|
if (!(goal instanceof Struct)) return;
|
|
27
27
|
if (isInspection(goal)) return;
|
|
28
28
|
if (isTemplate(goal)) throw new EyemindError(`line ${line}: rule templates belong in an implies() conclusion`);
|
|
29
|
-
if (
|
|
29
|
+
if (isCollection(goal) && (containsTemplate(goal.args[0]) || containsTemplate(goal.args[2]))) {
|
|
30
30
|
throw new EyemindError(`line ${line}: rule values in a body must be inspection goals`);
|
|
31
31
|
}
|
|
32
32
|
const inner = subgoals(goal);
|
package/src/terms.js
CHANGED
|
@@ -595,8 +595,9 @@ export function compareNumbers(a, b) {
|
|
|
595
595
|
|
|
596
596
|
// Standard order: variables < numbers < atoms < compound terms. Numbers are
|
|
597
597
|
// ordered by value, and a float precedes an integer of equal value. Distinct
|
|
598
|
-
// variables are ordered by name, so the order is the same in every comparison
|
|
599
|
-
|
|
598
|
+
// variables are ordered by name, so the order is the same in every comparison;
|
|
599
|
+
// with variablesEqual, any two variables compare equal instead.
|
|
600
|
+
export function compareTerms(left, right, variablesEqual = false) {
|
|
600
601
|
const pending = [left, right];
|
|
601
602
|
while (pending.length) {
|
|
602
603
|
const r = pending.pop();
|
|
@@ -609,7 +610,7 @@ export function compareTerms(left, right) {
|
|
|
609
610
|
if (order) return order;
|
|
610
611
|
if (typeof l !== typeof r) return typeof l === 'bigint' ? 1 : -1;
|
|
611
612
|
} else if (lr === 0) {
|
|
612
|
-
const order = compareText(l.name, r.name);
|
|
613
|
+
const order = variablesEqual ? 0 : compareText(l.name, r.name);
|
|
613
614
|
if (order) return order;
|
|
614
615
|
} else if (lr === 2) {
|
|
615
616
|
const order = compareText(l, r);
|
|
@@ -623,3 +624,15 @@ export function compareTerms(left, right) {
|
|
|
623
624
|
}
|
|
624
625
|
return 0;
|
|
625
626
|
}
|
|
627
|
+
|
|
628
|
+
// findall/3 and findall_sorted/3: the collections.
|
|
629
|
+
export function isCollection(goal) {
|
|
630
|
+
return isTerm(goal, 'findall', 3) || isTerm(goal, 'findall_sorted', 3);
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
// The items of findall_sorted/3 in the standard order, any two variables
|
|
634
|
+
// comparing equal: collected items have fresh variables, whose names say
|
|
635
|
+
// nothing. Items that compare equal keep their search order.
|
|
636
|
+
export function sortCollected(items) {
|
|
637
|
+
return [...items].sort((a, b) => compareTerms(a, b, true));
|
|
638
|
+
}
|
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.
|
|
2
|
+
export const VERSION = '0.5.25';
|