eyemind 0.5.23 → 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 +60 -27
- package/package.json +1 -1
- package/src/chains.js +2 -2
- package/src/closure.js +79 -11
- package/src/engine.js +5 -4
- package/src/fidelity.js +15 -7
- 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`.
|
|
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
|
|
|
@@ -2040,6 +2046,18 @@ clause order. Search each rule's body in its place, answering inner calls
|
|
|
2040
2046
|
as above. Each table MUST equal its expansion: the same number of answers,
|
|
2041
2047
|
each identical up to variable renaming to the answer in the same position.
|
|
2042
2048
|
|
|
2049
|
+
A backward rule appended by a template ([Section 6.2](#62-inline-rule-templates))
|
|
2050
|
+
answers through its instances. A rule whose enclosing generators are not
|
|
2051
|
+
all in the program has none. Otherwise, search the generator's part of its
|
|
2052
|
+
body first, with none of the call's bindings, and take its solutions in
|
|
2053
|
+
order, skipping a solution whose captured values are identical to an
|
|
2054
|
+
earlier one's. For each, unify the call with the head and search the
|
|
2055
|
+
template's part of the body. This is the order in which the run made the
|
|
2056
|
+
instances only when the generator's search consults no table and no
|
|
2057
|
+
listing, and its generators have no guard and introduce no witnesses;
|
|
2058
|
+
otherwise the search is unsettled. Captured values that are not ground
|
|
2059
|
+
also leave it unsettled.
|
|
2060
|
+
|
|
2043
2061
|
The boundary **rests on** every table and listing its search consults,
|
|
2044
2062
|
including those consulted transitively by table expansions and listing
|
|
2045
2063
|
closure checks. No table it rests on may depend on itself through the
|
|
@@ -2065,17 +2083,26 @@ requires:
|
|
|
2065
2083
|
`T`, one per solution of `G`, in order, each renamed apart. Repeated
|
|
2066
2084
|
solutions remain repeated list elements. The instance test allows later
|
|
2067
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.
|
|
2068
2088
|
|
|
2069
2089
|
**Certificate limits.** Settlement also requires all of the following:
|
|
2070
2090
|
|
|
2071
|
-
- No
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
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.
|
|
2098
|
+
- The search reaches no source inspection.
|
|
2099
|
+
- No consulted listing's relation is concluded by a forward rule within a
|
|
2100
|
+
template's scope ([Section 6.2](#62-inline-rule-templates)) that
|
|
2101
|
+
introduces witnesses, or whose generators have a guard or introduce
|
|
2102
|
+
witnesses. A rule appended by a template fires once for each instance, so
|
|
2103
|
+
closure searches its whole body, generator's part first. A rule whose
|
|
2104
|
+
enclosing generators are not all in the program never fires, and closure
|
|
2105
|
+
skips it.
|
|
2079
2106
|
|
|
2080
2107
|
For example, the saved
|
|
2081
2108
|
[Nixon diamond proof](https://eyereasoner.github.io/eyemind/examples/proof/nixon-diamond.js)
|
|
@@ -2161,7 +2188,7 @@ with these differences:
|
|
|
2161
2188
|
2. Negation MUST enforce [Section 4.2](#42-controls)'s binding restriction at the actual
|
|
2162
2189
|
call. C9's treatment of an unbound named variable as anonymous MUST NOT
|
|
2163
2190
|
be used. A source search that raises an error fails its certificate.
|
|
2164
|
-
3. `once` and
|
|
2191
|
+
3. `once` and the collections keep their [Section 4.2](#42-controls) meanings, so the first answer
|
|
2165
2192
|
or collected answers come from the actual inner call. [Section 11.12](#1112-c9-search-closure)'s
|
|
2166
2193
|
restriction on order-sensitive uses of derived listings still applies.
|
|
2167
2194
|
The order of unrelated answers of an ordinary question need not be
|
|
@@ -2218,8 +2245,9 @@ with the variables grounded before it:
|
|
|
2218
2245
|
- `once(G)` grounds nothing. `G` MUST satisfy these conditions. Each named
|
|
2219
2246
|
variable of `G` that occurs elsewhere in the clause MUST be grounded before
|
|
2220
2247
|
it, so the choice of a first answer binds nothing another goal uses.
|
|
2221
|
-
- `findall(T, G, L)`
|
|
2222
|
-
|
|
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`.
|
|
2223
2251
|
- A primitive whose variables are all grounded before it is a test, and
|
|
2224
2252
|
grounds nothing. `unify/2` with one side ground grounds the other, and
|
|
2225
2253
|
with neither ground grounds nothing. Otherwise the primitive MUST be
|
|
@@ -2255,15 +2283,17 @@ this analysis.
|
|
|
2255
2283
|
|
|
2256
2284
|
**Replayed listings.** A listing the mode analysis does not support still
|
|
2257
2285
|
supports C10 when every fact of every consulted listing **replays**. A fact
|
|
2258
|
-
replays when its step cites a forward rule
|
|
2259
|
-
|
|
2286
|
+
replays when its step cites a forward rule that is not restricted, does
|
|
2287
|
+
not introduce witnesses within a template's scope, and, if appended by a
|
|
2288
|
+
template, has all its enclosing generators in the program, none with a
|
|
2289
|
+
guard or witnesses; and when a search of that rule's body in
|
|
2260
2290
|
source order, starting with no bindings, solves each body goal as the
|
|
2261
2291
|
step's corresponding use records:
|
|
2262
2292
|
|
|
2263
2293
|
- A primitive runs on the bindings of that point of the search. Its first
|
|
2264
2294
|
solution under which the goal can still unify with the use is taken.
|
|
2265
2295
|
`compare/3` with a nonground compared term does not replay.
|
|
2266
|
-
- `once/1`, `not/1` and `
|
|
2296
|
+
- `once/1`, `not/1`, `findall/3` and `findall_sorted/3` take the answers of the goal, as it
|
|
2267
2297
|
stands at that point, from the call tables and listings, as in checking
|
|
2268
2298
|
the original calls. A negation with an unbound named variable does not
|
|
2269
2299
|
replay, and neither does a first answer or a collection whose order rests
|
|
@@ -2271,11 +2301,14 @@ step's corresponding use records:
|
|
|
2271
2301
|
with the use.
|
|
2272
2302
|
- `call/1` and a disjunction replay the goals their `'control'` step uses,
|
|
2273
2303
|
for a disjunction from the side those uses form.
|
|
2274
|
-
- A call to an ordinary relation follows the use's step
|
|
2304
|
+
- A call to an ordinary relation follows the use's step, whose rule MUST
|
|
2305
|
+
meet the conditions above other than direction. A forward rule's
|
|
2275
2306
|
conclusion MUST itself replay, and is then unified with the goal. A
|
|
2276
2307
|
source fact, renamed apart, is unified with the goal. A backward rule,
|
|
2277
2308
|
renamed apart, has its head unified with the goal and its body replayed
|
|
2278
|
-
against its step's uses.
|
|
2309
|
+
against its step's uses. For a backward rule appended by a template, the
|
|
2310
|
+
generator's part of the body replays first, before the head meets the
|
|
2311
|
+
goal, and the template's part after. A use with no step is unified with a source fact
|
|
2279
2312
|
of the relation, renamed apart. A replay that meets the same use under
|
|
2280
2313
|
the same call again does not replay.
|
|
2281
2314
|
- After each goal, the goal MUST still unify with its use, since a step
|
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.
|
|
@@ -43,6 +43,16 @@ function instanceOf(term, pattern) {
|
|
|
43
43
|
return unify(pattern, term, env) && variant(copyResolved(term, env)) === variant(term);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
// Whether the generators of a rule appended by a template fire as ordinary
|
|
47
|
+
// rules: no restricted guard, whose firings depend on the order of rule
|
|
48
|
+
// applications, and no witnesses, which the derived rule would share.
|
|
49
|
+
export function plainGenerators(program, clause) {
|
|
50
|
+
return clause.scopeIds.every((id) => {
|
|
51
|
+
const generator = program.byId.get(id);
|
|
52
|
+
return !generator || (generator.guard === null && !generator.witnessNames.length);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
46
56
|
// The name and arity of every relation a forward rule concludes.
|
|
47
57
|
export function forwardKeys(program) {
|
|
48
58
|
const keys = new Set();
|
|
@@ -82,10 +92,13 @@ export class Settler {
|
|
|
82
92
|
// only failure is the boundary's own result, and the tables and listings it
|
|
83
93
|
// rests on.
|
|
84
94
|
settle(boundary) {
|
|
85
|
-
const collecting =
|
|
95
|
+
const collecting = isCollection(boundary);
|
|
96
|
+
const sorting = isTerm(boundary, 'findall_sorted', 3);
|
|
86
97
|
const answering = isTerm(boundary, 'answers', 1);
|
|
87
98
|
const goal = collecting ? boundary.args[1] : boundary.args[0];
|
|
88
|
-
|
|
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);
|
|
89
102
|
const outcome = { status: 'settled', failures: [], boundaryOnly: false, tables: [], listings: [] };
|
|
90
103
|
let found = [];
|
|
91
104
|
try {
|
|
@@ -102,6 +115,11 @@ export class Settler {
|
|
|
102
115
|
outcome.failures.push([`search certificate leads to an error: ${error.message}`, boundary]);
|
|
103
116
|
found = null;
|
|
104
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
|
+
}
|
|
105
123
|
let unsettled = false;
|
|
106
124
|
const tables = new Set();
|
|
107
125
|
const listings = new Set();
|
|
@@ -178,15 +196,26 @@ export class Settler {
|
|
|
178
196
|
inner.return();
|
|
179
197
|
env.undo(mark);
|
|
180
198
|
if (!once && first.done) yield;
|
|
181
|
-
} else if (goalKey === 'findall/3') {
|
|
199
|
+
} else if (goalKey === 'findall/3' || goalKey === 'findall_sorted/3') {
|
|
200
|
+
const sorting = goalKey === 'findall_sorted/3';
|
|
182
201
|
const items = [];
|
|
183
|
-
|
|
184
|
-
|
|
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
|
|
185
207
|
items.push(freshTerm(copyResolved(goal.args[0], env), `collection${++this.serial}`));
|
|
186
208
|
}
|
|
187
|
-
|
|
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
|
+
}
|
|
188
217
|
env.undo(mark);
|
|
189
|
-
if (unify(goal.args[2], listFromItems(items), env)) yield;
|
|
218
|
+
if (unify(goal.args[2], listFromItems(sorting ? sortCollected(items) : items), env)) yield;
|
|
190
219
|
} else if (PRIMITIVE_KEYS.has(goalKey)) {
|
|
191
220
|
// Relative names of unbound variables are not preserved by alpha
|
|
192
221
|
// renaming of a table. A name-dependent comparison needs more evidence.
|
|
@@ -239,14 +268,46 @@ export class Settler {
|
|
|
239
268
|
const clauses = this.program.hasTemplates ? this.program.groups.get(goalKey) || []
|
|
240
269
|
: this.program.candidates(call, env, goalKey);
|
|
241
270
|
for (const clause of clauses) {
|
|
242
|
-
|
|
243
|
-
|
|
271
|
+
if (clause.parentId !== null) {
|
|
272
|
+
yield* this.instances(clause, call, env, reliance);
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
244
275
|
const [head, body] = freshClause(clause, `clause${++this.serial}`);
|
|
245
276
|
if (unify(call, head, env)) yield* this.solveAll(body, 0, env, reliance);
|
|
246
277
|
env.undo(mark);
|
|
247
278
|
}
|
|
248
279
|
}
|
|
249
280
|
|
|
281
|
+
// A backward rule appended by a template answers through its instances:
|
|
282
|
+
// one for each solution of its generator's body with captures not met
|
|
283
|
+
// before, in the order the generator found them, each searching only the
|
|
284
|
+
// template's body. The generator ran with none of the caller's bindings,
|
|
285
|
+
// so its body is solved before the head meets the call. Its order is the
|
|
286
|
+
// order of the run only when it consults no derived facts or tables.
|
|
287
|
+
* instances(clause, call, env, reliance) {
|
|
288
|
+
// Without its generator in the program, a rule has no instances.
|
|
289
|
+
if (!clause.scopeIds.every((id) => this.program.byId.has(id))) return;
|
|
290
|
+
if (!plainGenerators(this.program, clause)) throw new Unsettled();
|
|
291
|
+
const mark = env.mark();
|
|
292
|
+
const [head, body, names] = freshClause(clause, `clause${++this.serial}`);
|
|
293
|
+
const captures = listFromItems(clause.captures.map((name) => names.get(name) ?? new Var(name)));
|
|
294
|
+
const generator = new Reliance();
|
|
295
|
+
const seen = new Set();
|
|
296
|
+
for (const _ of this.solveAll(body.slice(0, clause.prefixLength), 0, env, generator)) { // eslint-disable-line no-unused-vars
|
|
297
|
+
if (generator.tables.size || generator.listings.size) throw new Unsettled();
|
|
298
|
+
const values = copyResolved(captures, env);
|
|
299
|
+
if (!isGround(values)) throw new Unsettled();
|
|
300
|
+
const id = identity(values);
|
|
301
|
+
if (seen.has(id)) continue;
|
|
302
|
+
seen.add(id);
|
|
303
|
+
const inner = env.mark();
|
|
304
|
+
if (unify(call, head, env)) yield* this.solveAll(body.slice(clause.prefixLength), 0, env, reliance);
|
|
305
|
+
env.undo(inner);
|
|
306
|
+
}
|
|
307
|
+
if (generator.tables.size || generator.listings.size) throw new Unsettled();
|
|
308
|
+
env.undo(mark);
|
|
309
|
+
}
|
|
310
|
+
|
|
250
311
|
* solveAll(goals, index, env, reliance) {
|
|
251
312
|
if (index === goals.length) {
|
|
252
313
|
yield;
|
|
@@ -325,7 +386,14 @@ export class Settler {
|
|
|
325
386
|
}
|
|
326
387
|
for (const rule of this.program.forward) {
|
|
327
388
|
if (!rule.plainHeads.some((head) => isCallable(head) && key(head) === goalKey)) continue;
|
|
328
|
-
|
|
389
|
+
// Without its generator in the program, an appended rule never fires.
|
|
390
|
+
if (!rule.scopeIds.every((id) => this.program.byId.has(id))) continue;
|
|
391
|
+
if (!plainGenerators(this.program, rule)) throw new Unsettled();
|
|
392
|
+
// A rule appended by a template fires once for each instance, so as a
|
|
393
|
+
// rule over facts it is its full body. Witnesses that depend on a
|
|
394
|
+
// captured context are not recomputed here.
|
|
395
|
+
if ((rule.parentId === null && rule.captures !== null) ||
|
|
396
|
+
((rule.parentId !== null || rule.templates.length) && rule.witnessNames.length)) throw new Unsettled();
|
|
329
397
|
const missing = this.closes(rule, goalKey, listed, entry.reliance);
|
|
330
398
|
if (missing !== null) {
|
|
331
399
|
entry.failure = `derived facts of ${goalKey} leave out ${text(missing)}, which clause ${rule.id} derives`;
|
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
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
// each claim is an answer, and each answer is claimed. Unlike logical steps,
|
|
3
3
|
// these searches start before a final substitution.
|
|
4
4
|
import { PRIMITIVE_KEYS, primitive } from './builtins.js';
|
|
5
|
-
import { Settler } from './closure.js';
|
|
5
|
+
import { Settler, plainGenerators } from './closure.js';
|
|
6
6
|
import { freshClause, text, variant } from './common.js';
|
|
7
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;
|
|
@@ -226,8 +226,12 @@ export function replayed(program, settler, listingKeys, stepOf) {
|
|
|
226
226
|
const active = new Set();
|
|
227
227
|
let serial = 0;
|
|
228
228
|
const fresh = (term) => freshTerm(term, `replay${++serial}`);
|
|
229
|
-
|
|
230
|
-
|
|
229
|
+
// A rule appended by a template replays as its full body, generator first.
|
|
230
|
+
// Witnesses that depend on a captured context are not recomputed here.
|
|
231
|
+
const supportedRule = (clause) => clause && clause.guard === null &&
|
|
232
|
+
clause.scopeIds.every((id) => program.byId.has(id)) && plainGenerators(program, clause) &&
|
|
233
|
+
(clause.parentId !== null || clause.captures === null) &&
|
|
234
|
+
!((clause.parentId !== null || clause.templates.length) && clause.witnessNames.length);
|
|
231
235
|
function compatible(goal, use, env) {
|
|
232
236
|
const mark = env.mark();
|
|
233
237
|
const agrees = unify(goal, fresh(use), env);
|
|
@@ -253,7 +257,7 @@ export function replayed(program, settler, listingKeys, stepOf) {
|
|
|
253
257
|
});
|
|
254
258
|
return side !== undefined && all(flattenConjunction(goal.args[side]), step.uses, env);
|
|
255
259
|
}
|
|
256
|
-
if (['once/1', 'not/1', 'findall/3'].includes(goalKey)) {
|
|
260
|
+
if (['once/1', 'not/1', 'findall/3', 'findall_sorted/3'].includes(goalKey)) {
|
|
257
261
|
const outcome = settler.answers(copyResolved(goal, env));
|
|
258
262
|
if (outcome.status !== 'settled' || !outcome.answers.length) return false;
|
|
259
263
|
return unify(goal, fresh(outcome.answers[0]), env) && compatible(goal, use, env);
|
|
@@ -292,7 +296,11 @@ export function replayed(program, settler, listingKeys, stepOf) {
|
|
|
292
296
|
const call = `${text(use)} ${text(copyResolved(goal, env))}`;
|
|
293
297
|
if (active.has(call)) return false;
|
|
294
298
|
active.add(call);
|
|
295
|
-
|
|
299
|
+
// A generator ran before any caller: its premises replay before the head
|
|
300
|
+
// meets the call.
|
|
301
|
+
const prefix = clause.prefixLength;
|
|
302
|
+
const solved = step.uses.length === body.length && all(body.slice(0, prefix), step.uses.slice(0, prefix), env) &&
|
|
303
|
+
unify(goal, head, env) && all(body.slice(prefix), step.uses.slice(prefix), env) && compatible(goal, use, env);
|
|
296
304
|
active.delete(call);
|
|
297
305
|
return solved;
|
|
298
306
|
}
|
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';
|