eyemind 0.5.24 → 0.5.26
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 +53 -28
- package/package.json +1 -1
- package/src/chains.js +2 -2
- package/src/closure.js +49 -16
- package/src/engine.js +5 -5
- package/src/fidelity.js +22 -9
- 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 +28 -8
- 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, and source rule inspection through the program's catalogue of rules. 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.26
|
|
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
|
|
|
@@ -2029,6 +2035,10 @@ separately checks the bindings with which the original question called it.
|
|
|
2029
2035
|
renaming. Rename each answer apart before using it.
|
|
2030
2036
|
- Answer a relation defined by source facts alone from those facts, in
|
|
2031
2037
|
clause order; it needs no table.
|
|
2038
|
+
- Answer an inspection goal from the catalogue of [Section 6.3](#63-source-rule-inspection),
|
|
2039
|
+
in source order, each rule renamed apart; it needs no table.
|
|
2040
|
+
- Use a listed derived pattern ([Section 6.3](#63-source-rule-inspection))
|
|
2041
|
+
renamed apart, as it is freshened at every use.
|
|
2032
2042
|
- Inside this search, negation treats unbound variables as anonymous.
|
|
2033
2043
|
This does not establish that a named variable was bound at the original
|
|
2034
2044
|
call; that is a C10 requirement.
|
|
@@ -2058,12 +2068,18 @@ closure checks. No table it rests on may depend on itself through the
|
|
|
2058
2068
|
tables consulted by its expansion.
|
|
2059
2069
|
|
|
2060
2070
|
**Supporting and closing listings.** Every listed fact MUST have a step,
|
|
2061
|
-
have the listing's name and arity, and be ground
|
|
2062
|
-
|
|
2063
|
-
rule
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2071
|
+
have the listing's name and arity, and be ground, unless the program
|
|
2072
|
+
inspects source rules and the fact is a derived pattern
|
|
2073
|
+
([Section 6.3](#63-source-rule-inspection)): its step is then the step of a
|
|
2074
|
+
pattern identical to it up to the renaming of its variables. Its step is
|
|
2075
|
+
subject to the other proof conditions. A listing is **closed** when, for
|
|
2076
|
+
every forward rule with a head of its relation, each solution of the body
|
|
2077
|
+
under the certificate gives head instances that are listed, a pattern up to
|
|
2078
|
+
the renaming of its variables, or are ground source facts. Bind the rule's
|
|
2079
|
+
witnesses to their specified Skolem terms when checking this closure
|
|
2080
|
+
([Section 8.2](#82-forward-evaluation-and-witnesses)). A head instance that
|
|
2081
|
+
keeps a variable no inspection or listed pattern supplied leaves the
|
|
2082
|
+
boundary unsettled: the run would have bound it to a Skolem term.
|
|
2067
2083
|
|
|
2068
2084
|
Support rules out invented facts; closure rules out omitted facts. For
|
|
2069
2085
|
example, an empty answer table is insufficient if a forward rule could
|
|
@@ -2077,14 +2093,18 @@ requires:
|
|
|
2077
2093
|
`T`, one per solution of `G`, in order, each renamed apart. Repeated
|
|
2078
2094
|
solutions remain repeated list elements. The instance test allows later
|
|
2079
2095
|
goals to have bound variables in the copied results.
|
|
2096
|
+
- For `findall_sorted(T, G, L)`, `L` is an instance of that list sorted as
|
|
2097
|
+
[Section 4.2](#42-controls) specifies.
|
|
2080
2098
|
|
|
2081
2099
|
**Certificate limits.** Settlement also requires all of the following:
|
|
2082
2100
|
|
|
2083
|
-
- No
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2101
|
+
- No `findall`, `once`, or `findall_sorted` collecting an item that is not
|
|
2102
|
+
ground, in the boundary's search, a relied-on table or a closure check,
|
|
2103
|
+
uses a table whose answers rest on a forward listing. This format checks
|
|
2104
|
+
support and closure of derived facts, but cannot establish their
|
|
2105
|
+
publication order for order-sensitive operations. A `findall_sorted` of
|
|
2106
|
+
ground items depends on the solutions and how often each occurs, not on
|
|
2107
|
+
their order.
|
|
2088
2108
|
- No consulted listing's relation is concluded by a forward rule within a
|
|
2089
2109
|
template's scope ([Section 6.2](#62-inline-rule-templates)) that
|
|
2090
2110
|
introduces witnesses, or whose generators have a guard or introduce
|
|
@@ -2131,7 +2151,7 @@ from evidence that cannot settle the boundary:
|
|
|
2131
2151
|
| A table differs from its expansion; a listed fact lacks a step or has the wrong name or arity; a listing is not closed; tables are circular; the certificate's search raises an error; or the checked search finds a solution to a negated goal. | C9 fails. |
|
|
2132
2152
|
| A needed table or listing is missing, evidence is incomplete, or a certificate limit above applies. | The boundary is not settled. |
|
|
2133
2153
|
| A collection's list disagrees with the certificate's search results. | The boundary is not settled; C6 may refute it from available evidence. |
|
|
2134
|
-
| A listing contains a nonground fact. | At the first such fact in listing order, leave the boundary unsettled rather than fail it. |
|
|
2154
|
+
| A listing contains a nonground fact, and the program inspects no source rules. | At the first such fact in listing order, leave the boundary unsettled rather than fail it. |
|
|
2135
2155
|
|
|
2136
2156
|
An incorrect table or listing is reported once, for the first boundary
|
|
2137
2157
|
resting on it. Any boundary C9 did not settle counts as trusted, including
|
|
@@ -2177,7 +2197,7 @@ with these differences:
|
|
|
2177
2197
|
2. Negation MUST enforce [Section 4.2](#42-controls)'s binding restriction at the actual
|
|
2178
2198
|
call. C9's treatment of an unbound named variable as anonymous MUST NOT
|
|
2179
2199
|
be used. A source search that raises an error fails its certificate.
|
|
2180
|
-
3. `once` and
|
|
2200
|
+
3. `once` and the collections keep their [Section 4.2](#42-controls) meanings, so the first answer
|
|
2181
2201
|
or collected answers come from the actual inner call. [Section 11.12](#1112-c9-search-closure)'s
|
|
2182
2202
|
restriction on order-sensitive uses of derived listings still applies.
|
|
2183
2203
|
The order of unrelated answers of an ordinary question need not be
|
|
@@ -2234,8 +2254,9 @@ with the variables grounded before it:
|
|
|
2234
2254
|
- `once(G)` grounds nothing. `G` MUST satisfy these conditions. Each named
|
|
2235
2255
|
variable of `G` that occurs elsewhere in the clause MUST be grounded before
|
|
2236
2256
|
it, so the choice of a first answer binds nothing another goal uses.
|
|
2237
|
-
- `findall(T, G, L)`
|
|
2238
|
-
|
|
2257
|
+
- A collection `findall(T, G, L)` or `findall_sorted(T, G, L)` follows the
|
|
2258
|
+
same rule for `T` and `G`. It grounds the variables of `L` when `G`
|
|
2259
|
+
grounds every variable of `T`.
|
|
2239
2260
|
- A primitive whose variables are all grounded before it is a test, and
|
|
2240
2261
|
grounds nothing. `unify/2` with one side ground grounds the other, and
|
|
2241
2262
|
with neither ground grounds nothing. Otherwise the primitive MUST be
|
|
@@ -2281,7 +2302,7 @@ step's corresponding use records:
|
|
|
2281
2302
|
- A primitive runs on the bindings of that point of the search. Its first
|
|
2282
2303
|
solution under which the goal can still unify with the use is taken.
|
|
2283
2304
|
`compare/3` with a nonground compared term does not replay.
|
|
2284
|
-
- `once/1`, `not/1` and `
|
|
2305
|
+
- `once/1`, `not/1`, `findall/3` and `findall_sorted/3` take the answers of the goal, as it
|
|
2285
2306
|
stands at that point, from the call tables and listings, as in checking
|
|
2286
2307
|
the original calls. A negation with an unbound named variable does not
|
|
2287
2308
|
replay, and neither does a first answer or a collection whose order rests
|
|
@@ -2299,12 +2320,16 @@ step's corresponding use records:
|
|
|
2299
2320
|
goal, and the template's part after. A use with no step is unified with a source fact
|
|
2300
2321
|
of the relation, renamed apart. A replay that meets the same use under
|
|
2301
2322
|
the same call again does not replay.
|
|
2323
|
+
- An inspection follows its use's `source(N)` step: rule `N` of the
|
|
2324
|
+
catalogue, renamed apart, is unified with the goal.
|
|
2302
2325
|
- After each goal, the goal MUST still unify with its use, since a step
|
|
2303
2326
|
shows its goals after the whole solution's bindings.
|
|
2304
2327
|
|
|
2305
2328
|
Each witness is then bound to its Skolem term
|
|
2306
2329
|
([Section 8.2](#82-forward-evaluation-and-witnesses)), and the fact MUST be
|
|
2307
|
-
identical to an instance of one of the rule's heads
|
|
2330
|
+
identical to an instance of one of the rule's heads, or for a derived
|
|
2331
|
+
pattern identical up to the renaming of its variables, every one of which an
|
|
2332
|
+
inspection or a listed pattern supplied. Replay re-executes each
|
|
2308
2333
|
recorded firing, so a test of whether a variable is bound sees what a search
|
|
2309
2334
|
sees, a first answer is the search's first answer, and host math is
|
|
2310
2335
|
computed rather than accepted within a tolerance. C2 keeps the steps a
|
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,12 @@ function instanceOf(term, pattern) {
|
|
|
43
43
|
return unify(pattern, term, env) && variant(copyResolved(term, env)) === variant(term);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
// How a listing identifies a fact: a ground fact by identity, a derived
|
|
47
|
+
// pattern up to the renaming of its variables (Section 6.3).
|
|
48
|
+
function listedKey(fact) {
|
|
49
|
+
return isGround(fact) ? identity(fact) : `pattern ${variant(fact)}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
46
52
|
// Whether the generators of a rule appended by a template fire as ordinary
|
|
47
53
|
// rules: no restricted guard, whose firings depend on the order of rule
|
|
48
54
|
// applications, and no witnesses, which the derived rule would share.
|
|
@@ -92,10 +98,13 @@ export class Settler {
|
|
|
92
98
|
// only failure is the boundary's own result, and the tables and listings it
|
|
93
99
|
// rests on.
|
|
94
100
|
settle(boundary) {
|
|
95
|
-
const collecting =
|
|
101
|
+
const collecting = isCollection(boundary);
|
|
102
|
+
const sorting = isTerm(boundary, 'findall_sorted', 3);
|
|
96
103
|
const answering = isTerm(boundary, 'answers', 1);
|
|
97
104
|
const goal = collecting ? boundary.args[1] : boundary.args[0];
|
|
98
|
-
|
|
105
|
+
// A sorted collection of ground items depends on its answers, not their
|
|
106
|
+
// order, so it does not need the order a listing cannot show.
|
|
107
|
+
const root = new Reliance(collecting && !sorting);
|
|
99
108
|
const outcome = { status: 'settled', failures: [], boundaryOnly: false, tables: [], listings: [] };
|
|
100
109
|
let found = [];
|
|
101
110
|
try {
|
|
@@ -112,6 +121,11 @@ export class Settler {
|
|
|
112
121
|
outcome.failures.push([`search certificate leads to an error: ${error.message}`, boundary]);
|
|
113
122
|
found = null;
|
|
114
123
|
}
|
|
124
|
+
if (sorting && found !== null) {
|
|
125
|
+
// Items equal up to their variables keep their search order.
|
|
126
|
+
if (!found.every((item) => isGround(item))) for (const item of root.tables) root.ordered.add(item);
|
|
127
|
+
found = sortCollected(found);
|
|
128
|
+
}
|
|
115
129
|
let unsettled = false;
|
|
116
130
|
const tables = new Set();
|
|
117
131
|
const listings = new Set();
|
|
@@ -188,15 +202,26 @@ export class Settler {
|
|
|
188
202
|
inner.return();
|
|
189
203
|
env.undo(mark);
|
|
190
204
|
if (!once && first.done) yield;
|
|
191
|
-
} else if (goalKey === 'findall/3') {
|
|
205
|
+
} else if (goalKey === 'findall/3' || goalKey === 'findall_sorted/3') {
|
|
206
|
+
const sorting = goalKey === 'findall_sorted/3';
|
|
192
207
|
const items = [];
|
|
193
|
-
|
|
194
|
-
|
|
208
|
+
// A sorted collection relies on the order of its tables only when an
|
|
209
|
+
// item has variables, which can make items equal up to their variables.
|
|
210
|
+
const inner = sorting ? new Reliance() : reliance;
|
|
211
|
+
if (!sorting) reliance.inner++;
|
|
212
|
+
for (const _ of this.solve(goal.args[1], env, inner)) { // eslint-disable-line no-unused-vars
|
|
195
213
|
items.push(freshTerm(copyResolved(goal.args[0], env), `collection${++this.serial}`));
|
|
196
214
|
}
|
|
197
|
-
|
|
215
|
+
if (sorting) {
|
|
216
|
+
for (const item of inner.tables) reliance.tables.add(item);
|
|
217
|
+
for (const item of inner.listings) reliance.listings.add(item);
|
|
218
|
+
for (const item of inner.ordered) reliance.ordered.add(item);
|
|
219
|
+
if (!items.every((item) => isGround(item))) for (const item of inner.tables) reliance.ordered.add(item);
|
|
220
|
+
} else {
|
|
221
|
+
reliance.inner--;
|
|
222
|
+
}
|
|
198
223
|
env.undo(mark);
|
|
199
|
-
if (unify(goal.args[2], listFromItems(items), env)) yield;
|
|
224
|
+
if (unify(goal.args[2], listFromItems(sorting ? sortCollected(items) : items), env)) yield;
|
|
200
225
|
} else if (PRIMITIVE_KEYS.has(goalKey)) {
|
|
201
226
|
// Relative names of unbound variables are not preserved by alpha
|
|
202
227
|
// renaming of a table. A name-dependent comparison needs more evidence.
|
|
@@ -207,7 +232,12 @@ export class Settler {
|
|
|
207
232
|
env.undo(mark);
|
|
208
233
|
}
|
|
209
234
|
} else if (INSPECTION_KEYS.has(goalKey)) {
|
|
210
|
-
|
|
235
|
+
// The catalogue of source rules, in source order, each renamed apart:
|
|
236
|
+
// its variables are scoped pattern variables (Section 6.3).
|
|
237
|
+
for (const source of this.program.inspections.get(goalKey) || []) {
|
|
238
|
+
if (unify(goal, freshTerm(source.term, `inspect${++this.serial}`), env)) yield;
|
|
239
|
+
env.undo(mark);
|
|
240
|
+
}
|
|
211
241
|
} else if (!this.derivedKeys.has(goalKey) &&
|
|
212
242
|
(this.program.groups.get(goalKey) || []).every((clause) => !clause.body.length && clause.parentId === null)) {
|
|
213
243
|
// Facts alone are scanned where they stand, with no table needed.
|
|
@@ -242,7 +272,8 @@ export class Settler {
|
|
|
242
272
|
const facts = this.listing(goalKey);
|
|
243
273
|
reliance.listings.add(goalKey);
|
|
244
274
|
for (const fact of facts) {
|
|
245
|
-
|
|
275
|
+
// A derived pattern is freshened at every use.
|
|
276
|
+
if (unify(call, isGround(fact) ? fact : freshTerm(fact, `pattern${++this.serial}`), env)) yield;
|
|
246
277
|
env.undo(mark);
|
|
247
278
|
}
|
|
248
279
|
}
|
|
@@ -356,14 +387,14 @@ export class Settler {
|
|
|
356
387
|
entry.subject = fact;
|
|
357
388
|
return entry;
|
|
358
389
|
}
|
|
359
|
-
//
|
|
360
|
-
if (!isGround(fact)) throw new Unsettled();
|
|
390
|
+
// Only a program that inspects source rules derives patterns.
|
|
391
|
+
if (!isGround(fact) && !this.program.inspectionEnabled) throw new Unsettled();
|
|
361
392
|
if (!this.source.supported(fact)) {
|
|
362
393
|
entry.failure = `derived fact without a step: ${text(fact)}`;
|
|
363
394
|
entry.subject = fact;
|
|
364
395
|
return entry;
|
|
365
396
|
}
|
|
366
|
-
listed.add(
|
|
397
|
+
listed.add(listedKey(fact));
|
|
367
398
|
}
|
|
368
399
|
for (const rule of this.program.forward) {
|
|
369
400
|
if (!rule.plainHeads.some((head) => isCallable(head) && key(head) === goalKey)) continue;
|
|
@@ -418,9 +449,11 @@ export class Settler {
|
|
|
418
449
|
}
|
|
419
450
|
for (const item of heads) {
|
|
420
451
|
const conclusion = copyResolved(item, env);
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
if (
|
|
452
|
+
// Only variables an inspection supplied stay free in a conclusion;
|
|
453
|
+
// any other would have been bound to a Skolem term.
|
|
454
|
+
if ([...variables(conclusion).keys()].some((name) => !/#(?:inspect|pattern)\d+$/.test(name))) throw new Unsettled();
|
|
455
|
+
const conclusionId = listedKey(conclusion);
|
|
456
|
+
if (!listed.has(conclusionId) && !(isGround(conclusion) && this.program.groundFactKeys.has(conclusionId))) return conclusion;
|
|
424
457
|
}
|
|
425
458
|
env.undo(mark);
|
|
426
459
|
}
|
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;
|
|
@@ -1131,7 +1132,6 @@ function certify(solver, boundaries, operational = false) {
|
|
|
1131
1132
|
},
|
|
1132
1133
|
facts(goalKey) {
|
|
1133
1134
|
const nodes = solver.facts.get(goalKey) || NO_NODES;
|
|
1134
|
-
if (nodes.some((node) => node.generic)) throw new Unsettled();
|
|
1135
1135
|
listed.set(goalKey, nodes);
|
|
1136
1136
|
return nodes.map((node) => node.goal);
|
|
1137
1137
|
},
|
package/src/fidelity.js
CHANGED
|
@@ -3,12 +3,13 @@
|
|
|
3
3
|
// these searches start before a final substitution.
|
|
4
4
|
import { PRIMITIVE_KEYS, primitive } from './builtins.js';
|
|
5
5
|
import { Settler, plainGenerators } from './closure.js';
|
|
6
|
+
import { stepIdOf } from './proof.js';
|
|
6
7
|
import { freshClause, text, variant } from './common.js';
|
|
7
8
|
import { goalTerm } from './goals.js';
|
|
8
9
|
import { INSPECTION_KEYS } from './inspection.js';
|
|
9
10
|
import {
|
|
10
11
|
Env, EyemindError, Struct, Var, conjunction, copyResolved, deref, flattenConjunction, freshTerm, identity, isAnonymous, isCallable,
|
|
11
|
-
isGround, isTerm, key, listFromItems, unify, variables,
|
|
12
|
+
isCollection, isGround, isTerm, key, listFromItems, unify, variables,
|
|
12
13
|
} from './terms.js';
|
|
13
14
|
import { Lettering, write } from './writer.js';
|
|
14
15
|
|
|
@@ -146,7 +147,7 @@ function groundedListings(program, listings, loose) {
|
|
|
146
147
|
const inner = bound(goal.args[0], grounded, negated);
|
|
147
148
|
return inner === null || shared(goal).some((name) => !grounded.has(name)) ? null : grounded;
|
|
148
149
|
}
|
|
149
|
-
if (
|
|
150
|
+
if (isCollection(goal)) {
|
|
150
151
|
const inner = bound(goal.args[1], grounded, negated);
|
|
151
152
|
const collected = new Struct('-', [goal.args[0], goal.args[1]]);
|
|
152
153
|
if (inner === null || shared(collected).some((name) => !grounded.has(name))) return null;
|
|
@@ -257,7 +258,7 @@ export function replayed(program, settler, listingKeys, stepOf) {
|
|
|
257
258
|
});
|
|
258
259
|
return side !== undefined && all(flattenConjunction(goal.args[side]), step.uses, env);
|
|
259
260
|
}
|
|
260
|
-
if (['once/1', 'not/1', 'findall/3'].includes(goalKey)) {
|
|
261
|
+
if (['once/1', 'not/1', 'findall/3', 'findall_sorted/3'].includes(goalKey)) {
|
|
261
262
|
const outcome = settler.answers(copyResolved(goal, env));
|
|
262
263
|
if (outcome.status !== 'settled' || !outcome.answers.length) return false;
|
|
263
264
|
return unify(goal, fresh(outcome.answers[0]), env) && compatible(goal, use, env);
|
|
@@ -274,7 +275,12 @@ export function replayed(program, settler, listingKeys, stepOf) {
|
|
|
274
275
|
}
|
|
275
276
|
return false;
|
|
276
277
|
}
|
|
277
|
-
if (INSPECTION_KEYS.has(goalKey))
|
|
278
|
+
if (INSPECTION_KEYS.has(goalKey)) {
|
|
279
|
+
// An inspection reads the source rule its step cites, renamed apart.
|
|
280
|
+
const step = stepOf(use);
|
|
281
|
+
const source = step && isTerm(step.by, 'source', 1) ? program.inspectedById.get(Number(step.by.args[0])) : null;
|
|
282
|
+
return Boolean(source) && unify(goal, freshTerm(source.term, `inspect${++serial}`), env) && compatible(goal, use, env);
|
|
283
|
+
}
|
|
278
284
|
const step = stepOf(use);
|
|
279
285
|
if (!step) {
|
|
280
286
|
// A use without a step is an instance of a source fact.
|
|
@@ -290,7 +296,7 @@ export function replayed(program, settler, listingKeys, stepOf) {
|
|
|
290
296
|
if (!isTerm(step.by, 'clause', 1)) return false;
|
|
291
297
|
const clause = program.byId.get(Number(step.by.args[0]));
|
|
292
298
|
if (!supportedRule(clause)) return false;
|
|
293
|
-
if (clause.forward) return fact(use) && unify(goal,
|
|
299
|
+
if (clause.forward) return fact(use) && unify(goal, freshTerm(use, `pattern${++serial}`), env);
|
|
294
300
|
const [head, body] = freshClause(clause, `replay${++serial}`);
|
|
295
301
|
if (!body.length) return unify(goal, head, env) && compatible(goal, use, env);
|
|
296
302
|
const call = `${text(use)} ${text(copyResolved(goal, env))}`;
|
|
@@ -331,8 +337,15 @@ export function replayed(program, settler, listingKeys, stepOf) {
|
|
|
331
337
|
unify(names.get(name), new Struct('skolem', [BigInt(clause.id), name, listFromItems(values)]), env);
|
|
332
338
|
}
|
|
333
339
|
}
|
|
334
|
-
|
|
335
|
-
|
|
340
|
+
// A derived pattern is identified up to the renaming of its variables,
|
|
341
|
+
// and keeps only variables an inspection supplied: the run binds any
|
|
342
|
+
// other head variable a firing leaves unbound to a Skolem term.
|
|
343
|
+
const same = isGround(goal) ? (item) => identity(item) === identity(goal) : (item) => variant(item) === variant(goal);
|
|
344
|
+
holds = holds && flattenConjunction(head).some((item) => {
|
|
345
|
+
if (!isCallable(item)) return false;
|
|
346
|
+
const conclusion = copyResolved(item, env);
|
|
347
|
+
return [...variables(conclusion).keys()].every((name) => /#(?:inspect|pattern)\d+$/.test(name)) && same(conclusion);
|
|
348
|
+
});
|
|
336
349
|
active.delete(id);
|
|
337
350
|
}
|
|
338
351
|
confirmed.set(id, holds);
|
|
@@ -351,10 +364,10 @@ export function checkCalls(program, claims, goals, certificate, steps, spellings
|
|
|
351
364
|
const settler = new Settler(program, {
|
|
352
365
|
answers: (call) => certificate.calls.get(variant(call, true)) ?? null,
|
|
353
366
|
facts: (goalKey) => certificate.listings.get(goalKey) ?? null,
|
|
354
|
-
supported: (fact) => steps.has(spellings
|
|
367
|
+
supported: (fact) => steps.has(stepIdOf(steps, spellings, fact)),
|
|
355
368
|
}, true);
|
|
356
369
|
if (claims.includes('false')) return { settler, outcomes: [], unresolved: [], covered: 0 };
|
|
357
|
-
const stepOf = (goal) => steps.get(spellings
|
|
370
|
+
const stepOf = (goal) => steps.get(stepIdOf(steps, spellings, goal));
|
|
358
371
|
// Replay runs host math exactly, so it needs no exclusion for tolerance.
|
|
359
372
|
const supported = (listings) => (!(inexact && listings.length) && positiveListings(program, listings)) ||
|
|
360
373
|
replayed(program, settler, listings, stepOf);
|
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
|
|
|
@@ -192,12 +192,16 @@ export function renderProof(program, claims, roots, checkable = false, heading =
|
|
|
192
192
|
const spellings = new Spellings();
|
|
193
193
|
const steps = new Map();
|
|
194
194
|
const cited = new Set();
|
|
195
|
-
const walk = (starts) => {
|
|
195
|
+
const walk = (starts, listed = false) => {
|
|
196
196
|
const pending = [...starts].reverse();
|
|
197
197
|
while (pending.length) {
|
|
198
198
|
const node = pending.pop();
|
|
199
199
|
const nodeId = spellings.spell(node.goal);
|
|
200
200
|
if (steps.has(nodeId)) continue;
|
|
201
|
+
// A listed derived pattern a claim's step already shows, up to the
|
|
202
|
+
// renaming of its variables, needs no second step.
|
|
203
|
+
if (listed && !isGround(node.goal) &&
|
|
204
|
+
[...steps.values()].some((step) => !isGround(step.goal) && variant(step.goal) === variant(node.goal))) continue;
|
|
201
205
|
steps.set(nodeId, node);
|
|
202
206
|
if (isTerm(node.by, 'clause', 1) || isTerm(node.by, 'source', 1)) {
|
|
203
207
|
const id = Number(node.by.args[0]);
|
|
@@ -216,7 +220,7 @@ export function renderProof(program, claims, roots, checkable = false, heading =
|
|
|
216
220
|
// Their negations and collections need certificates too, which can list
|
|
217
221
|
// further facts: certify again until no new boundary appears.
|
|
218
222
|
for (;;) {
|
|
219
|
-
walk(certificate.listings.flatMap(([, nodes]) => nodes));
|
|
223
|
+
walk(certificate.listings.flatMap(([, nodes]) => nodes), true);
|
|
220
224
|
if (certify === null || boundariesOf().length === boundaries.length) break;
|
|
221
225
|
boundaries = boundariesOf();
|
|
222
226
|
certificate = certify(boundaries);
|
|
@@ -319,6 +323,22 @@ export function checkRendered(program, checkable, goals = null, allowTrusted = t
|
|
|
319
323
|
return check(program, claims, steps, certificate, goals, allowTrusted, new Failures(name), spellings, name);
|
|
320
324
|
}
|
|
321
325
|
|
|
326
|
+
// The step of a goal: by its spelling, or for a derived pattern up to the
|
|
327
|
+
// renaming of its variables (Section 6.3), since a listing may spell a
|
|
328
|
+
// pattern's variables differently from the step that derives it.
|
|
329
|
+
const patternSteps = new WeakMap();
|
|
330
|
+
export function stepIdOf(steps, spellings, goal) {
|
|
331
|
+
const stepId = spellings.spell(goal);
|
|
332
|
+
if (steps.has(stepId) || isGround(goal)) return stepId;
|
|
333
|
+
let index = patternSteps.get(steps);
|
|
334
|
+
if (index === undefined) {
|
|
335
|
+
index = new Map();
|
|
336
|
+
for (const [id, step] of steps) if (!isGround(step.goal) && !index.has(variant(step.goal))) index.set(variant(step.goal), id);
|
|
337
|
+
patternSteps.set(steps, index);
|
|
338
|
+
}
|
|
339
|
+
return index.get(variant(goal)) ?? stepId;
|
|
340
|
+
}
|
|
341
|
+
|
|
322
342
|
// Settle each negation and collection that is well formed by its search
|
|
323
343
|
// certificate (C9), before the steps are checked: a settled one is no
|
|
324
344
|
// obligation.
|
|
@@ -326,7 +346,7 @@ function settleBoundaries(program, steps, certificate, spellings) {
|
|
|
326
346
|
const settler = new Settler(program, {
|
|
327
347
|
answers: (call) => certificate.tables.get(variant(call)) ?? null,
|
|
328
348
|
facts: (goalKey) => certificate.listings.get(goalKey) ?? null,
|
|
329
|
-
supported: (fact) => steps.has(spellings
|
|
349
|
+
supported: (fact) => steps.has(stepIdOf(steps, spellings, fact)),
|
|
330
350
|
});
|
|
331
351
|
const outcomes = new Map();
|
|
332
352
|
for (const [stepId, step] of steps) {
|
|
@@ -336,7 +356,7 @@ function settleBoundaries(program, steps, certificate, spellings) {
|
|
|
336
356
|
}
|
|
337
357
|
|
|
338
358
|
function isBoundary({ goal, by, bindings, uses }) {
|
|
339
|
-
return ((by === 'absent' && isTerm(goal, 'not', 1)) || (by === 'collected' &&
|
|
359
|
+
return ((by === 'absent' && isTerm(goal, 'not', 1)) || (by === 'collected' && isCollection(goal))) &&
|
|
340
360
|
!bindings.length && !uses.length;
|
|
341
361
|
}
|
|
342
362
|
|
|
@@ -545,7 +565,7 @@ function checkSteps(program, steps, allowTrusted, fail, spellings, name, outcome
|
|
|
545
565
|
} else {
|
|
546
566
|
tally.composed++;
|
|
547
567
|
}
|
|
548
|
-
} else if ((by === 'absent' && isTerm(goal, 'not', 1)) || (by === 'collected' &&
|
|
568
|
+
} else if ((by === 'absent' && isTerm(goal, 'not', 1)) || (by === 'collected' && isCollection(goal))) {
|
|
549
569
|
if (bindings.length || uses.length) {
|
|
550
570
|
fail.add('C3', 'trusted boundaries cannot have bindings or uses', goal);
|
|
551
571
|
continue;
|
|
@@ -1034,7 +1054,7 @@ function checkRelevance(program, claims, steps, goals, fail, spellings, settleme
|
|
|
1034
1054
|
const reach = [];
|
|
1035
1055
|
for (const claim of claims) for (const part of flattenConjunction(claim)) reach.push(spellings.spell(part));
|
|
1036
1056
|
for (const entry of settlement.calls.settler.listings.values()) {
|
|
1037
|
-
for (const fact of entry.facts) reach.push(spellings
|
|
1057
|
+
for (const fact of entry.facts) reach.push(stepIdOf(steps, spellings, fact));
|
|
1038
1058
|
}
|
|
1039
1059
|
while (reach.length) {
|
|
1040
1060
|
const stepId = reach.pop();
|
|
@@ -1047,7 +1067,7 @@ function checkRelevance(program, claims, steps, goals, fail, spellings, settleme
|
|
|
1047
1067
|
const outcome = settlement.outcomes.get(stepId);
|
|
1048
1068
|
if (outcome?.status === 'settled') {
|
|
1049
1069
|
for (const goalKey of outcome.listings) {
|
|
1050
|
-
for (const fact of settlement.settler.listings.get(goalKey).facts) reach.push(spellings
|
|
1070
|
+
for (const fact of settlement.settler.listings.get(goalKey).facts) reach.push(stepIdOf(steps, spellings, fact));
|
|
1051
1071
|
}
|
|
1052
1072
|
}
|
|
1053
1073
|
}
|
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.26';
|