eyemind 0.5.15 → 0.5.17
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 +3 -2
- package/SPEC.md +78 -12
- package/package.json +1 -1
- package/src/engine.js +54 -5
- package/src/fidelity.js +27 -16
- package/src/lang.js +5 -0
- package/src/program.js +49 -0
- 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 1376 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
|
|
|
@@ -392,7 +392,7 @@ The statement chooses the direction:
|
|
|
392
392
|
| **Forward** | `implies`, `restricted`, `query`, `contradiction` | From the facts, round after round, until a round adds nothing new. |
|
|
393
393
|
| **Backward** | `implied_by`, `fact` | When a goal asks: depth-first, left to right, in the order the clauses are written. |
|
|
394
394
|
|
|
395
|
-
Forward rules suit closures and classifications: `implies([path(X, Y), edge(Y, Z)], path(X, Z))` terminates on a cyclic graph. A variable only a forward rule's conclusion mentions gets a fresh value, a Skolem term, for each firing. `restricted` makes one only where none is known: `restricted(human(X), [father(X, F), man(F)])` gives no new father to a human whose known father is a man. Its other rules run first in each stratum, and its proof shows each new value is needed, as the restricted chase of N. Küchenmeister, A. Ivliev, D. Arndt and M. Krötzsch, ["Stratified Negation in RDF Rules"](https://arxiv.org/abs/2607.28778), requires. Which firings it makes can depend on the order of rule applications, so call fidelity stays an obligation. Backward rules suit computations driven by their inputs, such as arithmetic recursion and list processing. Backward chaining keeps no table of answers, so a backward rule that calls itself with the same goal runs until a bound stops it; such recursion belongs in a forward rule.
|
|
395
|
+
Forward rules suit closures and classifications: `implies([path(X, Y), edge(Y, Z)], path(X, Z))` terminates on a cyclic graph. A variable only a forward rule's conclusion mentions gets a fresh value, a Skolem term, for each firing. `restricted` makes one only where none is known: `restricted(human(X), [father(X, F), man(F)])` gives no new father to a human whose known father is a man. Its other rules run first in each stratum, a restricted rule whose conclusion can satisfy another's runs before it, a run whose new value later facts make unneeded stops with an error, and its proof shows each new value is needed, as the restricted chase of N. Küchenmeister, A. Ivliev, D. Arndt and M. Krötzsch, ["Stratified Negation in RDF Rules"](https://arxiv.org/abs/2607.28778), requires. Which firings it makes can depend on the order of rule applications, so call fidelity stays an obligation. Backward rules suit computations driven by their inputs, such as arithmetic recursion and list processing. Backward chaining keeps no table of answers, so a backward rule that calls itself with the same goal runs until a bound stops it; such recursion belongs in a forward rule.
|
|
396
396
|
|
|
397
397
|
The two work together: a forward rule's premise can call a backward definition, and a backward goal sees the facts forward rules derived. Forward rules run stratum by stratum, so a negation or collection waits until everything it inspects is final. A rule with a variable in every position, such as `implies([rdf(P, 'spo', Q), rdf(X, P, Y)], rdf(X, Q, Y))`, reaches every negation of its relation, and a cycle through it has no strata. Such a program is still accepted when it is chain-stratified, as Küchenmeister, Ivliev, Arndt and Krötzsch define it: no chain of rule applications from a rule with a negation can make one of its negated goals true without also satisfying a `contradiction`. Its rules then run in ranks, the Datalog rules first, and a rule fires only while no lower-ranked rule has a firing left ([SPEC §6.4](https://eyereasoner.github.io/eyemind/SPEC#64-chain-stratification)). `--max-depth`, `--max-iterations` and `--max-inferences` bound a run, and exceeding one is an error, never a partial answer. [SPEC §8](https://eyereasoner.github.io/eyemind/SPEC#8-evaluation-and-conclusions) defines the details.
|
|
398
398
|
|
|
@@ -480,6 +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 relational negation.** Supported, closed listings can establish the original calls of ground relational rules with bound negations, including inside conjunctions and disjunctions. A proof within this fragment can be `checked` with no obligation, even under `--strict-proof`. Collections of derived facts, whose order a listing cannot show, and dependencies on `restricted` rules with new witnesses keep an obligation.
|
|
483
484
|
|
|
484
485
|
## Checking the translation
|
|
485
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.17
|
|
6
6
|
```
|
|
7
7
|
|
|
8
8
|
This document specifies the syntax and meaning of eyemind programs and data
|
|
@@ -1005,6 +1005,12 @@ concludes `man('bob')` and invents no father for `'jo'`, since `'bob'`
|
|
|
1005
1005
|
already satisfies the heads once the third rule has run. A restricted
|
|
1006
1006
|
rule's display omits its guard ([Section 10.2](#102-clause-displays)).
|
|
1007
1007
|
|
|
1008
|
+
A `restricted` call states a clause and is no rule value: a `restricted`
|
|
1009
|
+
call nested in another statement MUST be rejected. Its premise MUST bind
|
|
1010
|
+
every variable of its heads other than the head-only ones when the guard is
|
|
1011
|
+
solved; a solution that leaves one unbound, as a fact with variables can,
|
|
1012
|
+
is an error.
|
|
1013
|
+
|
|
1008
1014
|
This gives the specified Skolemized reading of existential conclusions.
|
|
1009
1015
|
The original existential rule need not have a least model over its original
|
|
1010
1016
|
vocabulary. A witness is an ordinary compound in subsequent reasoning; it
|
|
@@ -1039,6 +1045,20 @@ holds when `G` has no solution, with the binding restrictions of [Section
|
|
|
1039
1045
|
4.2](#42-controls). This is absence relative to the program, not falsity in the world.
|
|
1040
1046
|
For relational goals this gives the stratified least-model reading.
|
|
1041
1047
|
|
|
1048
|
+
A chain-stratified program ([Section 6.4](#64-chain-stratification)) has no such ranks. Its meaning
|
|
1049
|
+
is the set of facts [Section 8.2](#82-forward-evaluation-and-witnesses)'s chain strata derive. When no
|
|
1050
|
+
contradiction holds, that set is a **stable model**: the least model of
|
|
1051
|
+
the program's rules with every negation decided by the set itself. Chain
|
|
1052
|
+
stratification is what makes it the only one, so the order of rule
|
|
1053
|
+
applications cannot change it.
|
|
1054
|
+
|
|
1055
|
+
A restricted rule ([Section 7.2](#72-variables-and-witnesses)) reads its conclusion existentially: for each
|
|
1056
|
+
solution of its premise, some values satisfy its heads. A result is **lean**
|
|
1057
|
+
when each witness a restricted rule made is needed, no other values
|
|
1058
|
+
satisfying those heads. [Section 8.2](#82-forward-evaluation-and-witnesses) derives one lean result or fails;
|
|
1059
|
+
another order of rule applications could derive another, so a lean result
|
|
1060
|
+
is not determined by the program alone.
|
|
1061
|
+
|
|
1042
1062
|
Collection also waits for its dependencies. `findall` collects ordered
|
|
1043
1063
|
search solutions, including repeated derivations and nonground instances;
|
|
1044
1064
|
its meaning is not determined by a set of ground atoms alone. Bindings
|
|
@@ -1146,13 +1166,40 @@ having added nothing. A rule thus never fires while a rule of a lower rank
|
|
|
1146
1166
|
has a firing left.
|
|
1147
1167
|
|
|
1148
1168
|
A stratum with restricted rules alternates two kinds of round. Its other
|
|
1149
|
-
rules run in rounds as above; a round of its restricted rules, in
|
|
1169
|
+
rules run in rounds as above; a round of its restricted rules, in restraint
|
|
1150
1170
|
order, follows only a round of the other rules that adds nothing, and is
|
|
1151
1171
|
followed by rounds of the other rules again when it adds something. The
|
|
1152
1172
|
stratum ends with a round of its restricted rules that adds nothing. The
|
|
1153
1173
|
other rules of a stratum thus run before its restricted rules, whose guards
|
|
1154
1174
|
see what they derive.
|
|
1155
1175
|
|
|
1176
|
+
A restricted rule `A` **restrains** a restricted rule `B`, both renamed
|
|
1177
|
+
apart, when for some head `h` of `B` with a head-only variable of `B`, the
|
|
1178
|
+
heads of `B` sharing a head-only variable with `h` (`h` included) unify, one
|
|
1179
|
+
after another under one substitution, each with some head of `A`: firing
|
|
1180
|
+
`A` can then satisfy `B`'s heads. The **restraint order** of a set of
|
|
1181
|
+
restricted rules repeatedly takes the first rule, in clause order, that no
|
|
1182
|
+
other remaining rule restrains, or the first remaining rule when every one
|
|
1183
|
+
is restrained. A chain stratum ([Section 6.4](#64-chain-stratification)) runs its restricted rules after its
|
|
1184
|
+
other rules, in restraint order.
|
|
1185
|
+
|
|
1186
|
+
When reasoning ends without a contradiction, every guard of a published
|
|
1187
|
+
restricted firing MUST hold again, with the firing's bindings, in the final
|
|
1188
|
+
facts. Otherwise later facts satisfy the firing's heads, so its witness is
|
|
1189
|
+
not needed and the result depends on the order in which rules were applied:
|
|
1190
|
+
the run MUST fail with an error. For example,
|
|
1191
|
+
|
|
1192
|
+
```js
|
|
1193
|
+
fact(f('a'))
|
|
1194
|
+
restricted(f(X), h(W))
|
|
1195
|
+
implies(g(X, Y), h(Y))
|
|
1196
|
+
restricted(f(X), g(X, W))
|
|
1197
|
+
```
|
|
1198
|
+
|
|
1199
|
+
fails: the second rule makes `h(skolem(2, 'W', []))`, and the last rule's
|
|
1200
|
+
`g('a', skolem(4, 'W', ['a']))` then gives `h(skolem(4, 'W', ['a']))`,
|
|
1201
|
+
which satisfies the second rule's head too.
|
|
1202
|
+
|
|
1156
1203
|
For a generator, use its scope projection ([Section 6.2](#62-inline-rule-templates)) as its head when
|
|
1157
1204
|
constructing witnesses. For an appended forward clause of [Section 6.2](#62-inline-rule-templates),
|
|
1158
1205
|
prepend one element to
|
|
@@ -1604,6 +1651,16 @@ boundary is established by its search certificate. An unconditional
|
|
|
1604
1651
|
`checked` verdict also establishes operational query fidelity
|
|
1605
1652
|
([Section 11.2](#112-operational-query-fidelity)).
|
|
1606
1653
|
|
|
1654
|
+
For a program with negation, "follows" is relative to the facts the
|
|
1655
|
+
certificate lists: C9 establishes that they are supported by steps, closed
|
|
1656
|
+
under the forward rules and consistent with every settled negation. In the
|
|
1657
|
+
ground relational fragment of [Section 11.13](#1113-c10-call-fidelity), a stratified or
|
|
1658
|
+
chain-stratified program has only one such result ([Section 7.4](#74-negation-and-collection)). With restricted
|
|
1659
|
+
witnesses, different lean results can have these properties. A claim whose
|
|
1660
|
+
forward dependency closure includes a restricted rule with head-only
|
|
1661
|
+
variables therefore retains a call-fidelity obligation, and a strict check
|
|
1662
|
+
rejects it. Independent claims may still check strictly.
|
|
1663
|
+
|
|
1607
1664
|
It does not establish:
|
|
1608
1665
|
|
|
1609
1666
|
- that the proof contains all conclusions of the program;
|
|
@@ -2098,8 +2155,8 @@ with these differences:
|
|
|
2098
2155
|
4. A comparison `compare/3` with either compared term nonground is not
|
|
2099
2156
|
supported by this certificate format: alpha renaming of a table need
|
|
2100
2157
|
not preserve the order of variable names. Such a search leaves call fidelity unproved.
|
|
2101
|
-
5. A consulted forward listing MUST have a dependency closure in the
|
|
2102
|
-
|
|
2158
|
+
5. A consulted forward listing MUST have a dependency closure in the
|
|
2159
|
+
supported fragment described below. A final listing alone MUST NOT certify
|
|
2103
2160
|
binding-sensitive or order-sensitive forward firings outside that fragment.
|
|
2104
2161
|
|
|
2105
2162
|
For the `once` example in [Section 11.2](#112-operational-query-fidelity),
|
|
@@ -2117,14 +2174,18 @@ first, and `once` selects it. Thus `q('a')` is an answer and `q('b')` is
|
|
|
2117
2174
|
excluded. Substituting `called(p('b'), [p('b')])` cannot establish the
|
|
2118
2175
|
different call `p(X)`.
|
|
2119
2176
|
|
|
2120
|
-
**Supported forward listings.** A forward listing's **ground
|
|
2121
|
-
|
|
2122
|
-
of every source fact, backward rule or forward rule
|
|
2123
|
-
in that closure. To use a listing for C10:
|
|
2177
|
+
**Supported forward listings.** A forward listing's **ground dependency
|
|
2178
|
+
closure** contains its relation and each ordinary body relation, inside a
|
|
2179
|
+
negation or not, of every source fact, backward rule or forward rule
|
|
2180
|
+
defining any relation in that closure. To use a listing for C10:
|
|
2124
2181
|
|
|
2125
2182
|
- All source facts in the closure MUST be ground.
|
|
2126
|
-
- Rule bodies MUST contain only ordinary calls, `'true'`, conjunction
|
|
2127
|
-
disjunction. Templates, captures and source
|
|
2183
|
+
- Rule bodies MUST contain only ordinary calls, `'true'`, conjunction,
|
|
2184
|
+
disjunction and negations of these. Templates, captures and source
|
|
2185
|
+
inspection are excluded.
|
|
2186
|
+
- A negation grounds nothing. Each of its variables other than anonymous
|
|
2187
|
+
ones MUST be grounded by the body goals before it, and it MUST NOT contain
|
|
2188
|
+
a negation.
|
|
2128
2189
|
- A body call to an ordinary relation grounds its variables. Conjunction
|
|
2129
2190
|
accumulates the grounded variables of its parts; disjunction grounds only
|
|
2130
2191
|
those grounded by both alternatives.
|
|
@@ -2136,8 +2197,13 @@ in that closure. To use a listing for C10:
|
|
|
2136
2197
|
restricted rule makes can depend on the order of rule applications.
|
|
2137
2198
|
|
|
2138
2199
|
Supported facts and closed listings then establish the least ground
|
|
2139
|
-
positive closure
|
|
2140
|
-
|
|
2200
|
+
positive closure and, with negations, a set of facts that is supported,
|
|
2201
|
+
closed under the rules and consistent with each negation, every negation
|
|
2202
|
+
being decided by the listed facts alone. A stratified or chain-stratified
|
|
2203
|
+
program has exactly one such set ([Section 7.4](#74-negation-and-collection)), its result, so the
|
|
2204
|
+
listings are the facts the original calls consult. More general forward
|
|
2205
|
+
execution requires publication evidence this format does not supply,
|
|
2206
|
+
leaving call fidelity unproved.
|
|
2141
2207
|
|
|
2142
2208
|
**Deciding claims.** Every relevant call certificate MUST be valid. Each
|
|
2143
2209
|
claim MUST be identical, up to variable renaming, to an answer of at least
|
package/package.json
CHANGED
package/src/engine.js
CHANGED
|
@@ -165,8 +165,10 @@ function unboundNegation(goal, env) {
|
|
|
165
165
|
const shown = write(resolved, null, naming);
|
|
166
166
|
const unbound = [...new Set([...variables(resolved).values()]
|
|
167
167
|
.filter((variable) => !isAnonymous(variable)).map((variable) => naming.get(variable.name)))];
|
|
168
|
-
|
|
168
|
+
const error = new EyemindError('negation requires a goal whose variables are bound, except for anonymous ones: ' +
|
|
169
169
|
`${unbound.join(', ')} ${unbound.length === 1 ? 'is' : 'are'} unbound in ${shown}`);
|
|
170
|
+
error.goal = goal;
|
|
171
|
+
return error;
|
|
170
172
|
}
|
|
171
173
|
|
|
172
174
|
// Resolve a proof forest with an explicit stack: a derivation chain is as deep
|
|
@@ -286,6 +288,7 @@ export class Solver {
|
|
|
286
288
|
this.genericKeys = new Set();
|
|
287
289
|
this.genericPredicates = new Set();
|
|
288
290
|
this.derived = [];
|
|
291
|
+
this.guards = [];
|
|
289
292
|
this.reported = new Map();
|
|
290
293
|
this.stats = { inferences: 0, rounds: 0, derived: 0 };
|
|
291
294
|
this.haltCode = null;
|
|
@@ -767,6 +770,22 @@ export class Solver {
|
|
|
767
770
|
return first < order.length && order[first] < now.get(goalKey);
|
|
768
771
|
}
|
|
769
772
|
|
|
773
|
+
// The solutions of a rule's body. A restricted rule's guard is a negation
|
|
774
|
+
// the program did not write: when its premise leaves a variable of its
|
|
775
|
+
// heads unbound, the error names the rule rather than the guard.
|
|
776
|
+
*guarded(clause, guard, solutions) {
|
|
777
|
+
if (clause.guard === null) {
|
|
778
|
+
yield* solutions;
|
|
779
|
+
return;
|
|
780
|
+
}
|
|
781
|
+
try {
|
|
782
|
+
yield* solutions;
|
|
783
|
+
} catch (error) {
|
|
784
|
+
if (!(error instanceof EyemindError) || error.goal !== guard) throw error;
|
|
785
|
+
throw new EyemindError(`restricted rule ${clause.id} needs its premise to bind the variables of its conclusion other than its new values`);
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
770
789
|
// Each solution of a renamed rule's body, as what it concludes, with the
|
|
771
790
|
// alternatives its goals took when the body is searched in a window.
|
|
772
791
|
// Bindings last only until the next answer is requested, and adding facts
|
|
@@ -774,7 +793,7 @@ export class Solver {
|
|
|
774
793
|
// first and concluded afterwards.
|
|
775
794
|
scan(clause, head, body, names, env, prefix) {
|
|
776
795
|
const found = [];
|
|
777
|
-
for (const [answerEnv, nodes] of this.solve(body, env)) {
|
|
796
|
+
for (const [answerEnv, nodes] of this.guarded(clause, body[body.length - 1], this.solve(body, env))) {
|
|
778
797
|
const mark = answerEnv.mark();
|
|
779
798
|
// Head variables the body leaves unbound become Skolem terms, shared
|
|
780
799
|
// between the heads of one conclusion.
|
|
@@ -810,7 +829,9 @@ export class Solver {
|
|
|
810
829
|
}
|
|
811
830
|
if (this.recording || clause.templates.length) bindings = [...names].map(([name, value]) => [name, copyResolved(value, answerEnv)]);
|
|
812
831
|
const claim = conclusions.some((item) => item === 'true') ? copyResolved(conjunction(body), answerEnv) : null;
|
|
813
|
-
|
|
832
|
+
// A restricted firing keeps its guard, checked again once reasoning ends.
|
|
833
|
+
const guard = clause.guard === null || !unresolved.size ? null : copyResolved(body[body.length - 1], answerEnv);
|
|
834
|
+
found.push([[conclusions, children, bindings, claim, guard], this.chosen !== null ? [...this.chosen] : null]);
|
|
814
835
|
answerEnv.undo(mark);
|
|
815
836
|
}
|
|
816
837
|
return found;
|
|
@@ -822,12 +843,39 @@ export class Solver {
|
|
|
822
843
|
// stratum, restricted rules run only in a round where the other rules
|
|
823
844
|
// derive nothing, so their guards see what those rules derive.
|
|
824
845
|
forward(reporting = true) {
|
|
846
|
+
this.forwardRounds(reporting);
|
|
847
|
+
// A restricted rule's witness must still be needed once reasoning ends:
|
|
848
|
+
// otherwise later facts satisfy its heads, and the result depends on
|
|
849
|
+
// the order in which rules were applied (SPEC Section 8.2).
|
|
850
|
+
if (this.haltCode !== null) return;
|
|
851
|
+
for (const [clause, guard, conclusions] of this.guards) {
|
|
852
|
+
if (this.holds(guard)) continue;
|
|
853
|
+
throw new EyemindError(`restricted rule ${clause.id} concluded ${text(conclusions[0])}, whose witness later facts make unneeded; ` +
|
|
854
|
+
'the result depends on the order of rule applications');
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
// Whether a goal holds in the current state.
|
|
859
|
+
holds(goal) {
|
|
860
|
+
for (const _ of this.solve([goal], new Env())) return true; // eslint-disable-line no-unused-vars
|
|
861
|
+
return false;
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
forwardRounds(reporting) {
|
|
825
865
|
const layers = new Map();
|
|
826
866
|
for (const clause of this.program.forward) {
|
|
827
867
|
const rank = this.program.strata.get(clause.id) || 0;
|
|
828
868
|
if (!layers.has(rank)) layers.set(rank, []);
|
|
829
869
|
layers.get(rank).push(clause);
|
|
830
870
|
}
|
|
871
|
+
// In a chain stratum too, a restricted rule runs after those whose heads
|
|
872
|
+
// can satisfy its own.
|
|
873
|
+
if (this.program.layered) {
|
|
874
|
+
for (const [rank, rules] of layers) {
|
|
875
|
+
const restricted = this.program.restraining(rules.filter((clause) => clause.restricted));
|
|
876
|
+
layers.set(rank, [...rules.filter((clause) => !clause.restricted), ...restricted]);
|
|
877
|
+
}
|
|
878
|
+
}
|
|
831
879
|
const ranks = [...layers.keys()].sort((a, b) => a - b);
|
|
832
880
|
const maxIterations = this.options.maxIterations || 1000;
|
|
833
881
|
let rounds = 0;
|
|
@@ -851,7 +899,7 @@ export class Solver {
|
|
|
851
899
|
for (const rank of ranks) {
|
|
852
900
|
rounds = 0;
|
|
853
901
|
const all = layers.get(rank);
|
|
854
|
-
const restricted = all.filter((clause) => clause.restricted);
|
|
902
|
+
const restricted = this.program.restraining(all.filter((clause) => clause.restricted));
|
|
855
903
|
const ordinary = restricted.length ? all.filter((clause) => !clause.restricted) : all;
|
|
856
904
|
let changed = true;
|
|
857
905
|
let lean = false;
|
|
@@ -879,7 +927,8 @@ export class Solver {
|
|
|
879
927
|
// All instances of this template see the same state. Publishing one
|
|
880
928
|
// instance's conclusions during another's search changes recursive
|
|
881
929
|
// bodies and their observable order.
|
|
882
|
-
for (const [clause, [conclusions, children, bindings, claim]] of firings) {
|
|
930
|
+
for (const [clause, [conclusions, children, bindings, claim, guard]] of firings) {
|
|
931
|
+
if (guard) this.guards.push([clause, guard, conclusions]);
|
|
883
932
|
for (const [position, conclusion] of conclusions.entries()) {
|
|
884
933
|
const template = clause.templates.find((item) => item.position === position);
|
|
885
934
|
if (template) {
|
package/src/fidelity.js
CHANGED
|
@@ -6,7 +6,7 @@ import { text, variant } from './common.js';
|
|
|
6
6
|
import { goalTerm } from './goals.js';
|
|
7
7
|
import { INSPECTION_KEYS } from './inspection.js';
|
|
8
8
|
import {
|
|
9
|
-
Env, conjunction, copyResolved, freshTerm, isCallable, isGround, isTerm, key, unify, variables,
|
|
9
|
+
Env, conjunction, copyResolved, freshTerm, isAnonymous, isCallable, isGround, isTerm, key, unify, variables,
|
|
10
10
|
} from './terms.js';
|
|
11
11
|
|
|
12
12
|
function instance(goal, pattern) {
|
|
@@ -34,27 +34,39 @@ export function callQuestions(program, goals, claims) {
|
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
// C9's support plus closure establishes the least ground positive model
|
|
38
|
-
//
|
|
39
|
-
//
|
|
37
|
+
// C9's support plus closure establishes the least ground positive model,
|
|
38
|
+
// and with negations the one stable model of a stratified or
|
|
39
|
+
// chain-stratified program: a negation whose named variables earlier goals
|
|
40
|
+
// bind is decided by the listed facts alone. For forward rules that observe
|
|
41
|
+
// bindings or order, and for restricted rules, whose guard another order of
|
|
42
|
+
// firings could decide otherwise, a final listing alone does not establish
|
|
43
|
+
// which firings occurred. Refuse that stronger promise.
|
|
40
44
|
export function positiveListings(program, listings) {
|
|
41
45
|
const pending = [...listings];
|
|
42
46
|
const seen = new Set();
|
|
43
|
-
function bound(goal) {
|
|
44
|
-
if (goal === 'true') return
|
|
47
|
+
function bound(goal, grounded, negated = false) {
|
|
48
|
+
if (goal === 'true') return grounded;
|
|
45
49
|
if (isTerm(goal, 'and', 2) || isTerm(goal, 'or', 2)) {
|
|
46
|
-
const left = bound(goal.args[0]);
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
+
const left = bound(goal.args[0], grounded, negated);
|
|
51
|
+
if (left === null) return null;
|
|
52
|
+
const right = bound(goal.args[1], goal.name === 'and' ? left : grounded, negated);
|
|
53
|
+
if (right === null) return null;
|
|
54
|
+
return goal.name === 'and' ? right
|
|
50
55
|
: new Set([...left].filter((name) => right.has(name)));
|
|
51
56
|
}
|
|
57
|
+
if (isTerm(goal, 'not', 1)) {
|
|
58
|
+
if (negated) return null;
|
|
59
|
+
for (const [name, variable] of variables(goal)) {
|
|
60
|
+
if (!isAnonymous(variable) && !grounded.has(name)) return null;
|
|
61
|
+
}
|
|
62
|
+
return bound(goal.args[0], grounded, true) === null ? null : grounded;
|
|
63
|
+
}
|
|
52
64
|
if (!isCallable(goal)) return null;
|
|
53
65
|
const goalKey = key(goal);
|
|
54
66
|
if (PRIMITIVE_KEYS.has(goalKey) || INSPECTION_KEYS.has(goalKey) ||
|
|
55
67
|
['not/1', 'findall/3', 'once/1', 'call/1'].includes(goalKey)) return null;
|
|
56
68
|
pending.push(goalKey);
|
|
57
|
-
return new Set(variables(goal).keys());
|
|
69
|
+
return new Set([...grounded, ...variables(goal).keys()]);
|
|
58
70
|
}
|
|
59
71
|
while (pending.length) {
|
|
60
72
|
const goalKey = pending.pop();
|
|
@@ -63,16 +75,15 @@ export function positiveListings(program, listings) {
|
|
|
63
75
|
const clauses = [...(program.groups.get(goalKey) || []), ...program.forward.filter((clause) =>
|
|
64
76
|
clause.plainHeads.some((head) => isCallable(head) && key(head) === goalKey))];
|
|
65
77
|
for (const clause of clauses) {
|
|
66
|
-
if (clause.parentId !== null || clause.templates.length || clause.captures !== null) return false;
|
|
78
|
+
if (clause.parentId !== null || clause.templates.length || clause.captures !== null || clause.guard !== null) return false;
|
|
67
79
|
if (!clause.forward && !clause.body.length) {
|
|
68
80
|
if (!isGround(clause.head)) return false;
|
|
69
81
|
continue;
|
|
70
82
|
}
|
|
71
|
-
|
|
83
|
+
let grounded = new Set();
|
|
72
84
|
for (const goal of clause.body) {
|
|
73
|
-
|
|
74
|
-
if (
|
|
75
|
-
for (const name of names) grounded.add(name);
|
|
85
|
+
grounded = bound(goal, grounded);
|
|
86
|
+
if (grounded === null) return false;
|
|
76
87
|
}
|
|
77
88
|
const witnesses = new Set(clause.forward ? clause.witnessNames : []);
|
|
78
89
|
for (const name of variables(clause.head).keys()) {
|
package/src/lang.js
CHANGED
|
@@ -183,6 +183,11 @@ class Builder {
|
|
|
183
183
|
const term = pending.pop();
|
|
184
184
|
if (!(term instanceof Struct)) continue;
|
|
185
185
|
const source = this.rules.get(term);
|
|
186
|
+
// A restricted rule is a statement only: nested, it would be read as
|
|
187
|
+
// a term and its rule dropped.
|
|
188
|
+
if (source !== undefined && source.kind === 'restricted') {
|
|
189
|
+
throw new EyemindError('restricted() states a rule and cannot be nested in another statement');
|
|
190
|
+
}
|
|
186
191
|
if (source !== undefined) this.embedded.add(source);
|
|
187
192
|
pending.push(...term.args);
|
|
188
193
|
}
|
package/src/program.js
CHANGED
|
@@ -200,6 +200,28 @@ export class Program {
|
|
|
200
200
|
this.layered = this.strata.layered === true;
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
+
// Restricted rules in the order they run: one whose heads can satisfy
|
|
204
|
+
// another's, its witnesses free, before that one, and otherwise, as on a
|
|
205
|
+
// cycle of such rules, in clause order (SPEC Section 8.2).
|
|
206
|
+
restraining(rules) {
|
|
207
|
+
if (rules.length < 2) return rules;
|
|
208
|
+
const before = new Map(rules.map((rule) => [rule.id, new Set()]));
|
|
209
|
+
for (const first of rules) {
|
|
210
|
+
for (const second of rules) {
|
|
211
|
+
if (first !== second && restrains(first, second)) before.get(second.id).add(first.id);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
const order = [];
|
|
215
|
+
const placed = new Set();
|
|
216
|
+
while (order.length < rules.length) {
|
|
217
|
+
const ready = rules.find((rule) => !placed.has(rule.id) && [...before.get(rule.id)].every((id) => placed.has(id)))
|
|
218
|
+
?? rules.find((rule) => !placed.has(rule.id));
|
|
219
|
+
order.push(ready);
|
|
220
|
+
placed.add(ready.id);
|
|
221
|
+
}
|
|
222
|
+
return order;
|
|
223
|
+
}
|
|
224
|
+
|
|
203
225
|
prepareInspections() {
|
|
204
226
|
if (this.inspectionCatalogue !== null) return;
|
|
205
227
|
this.inspectionCatalogue = new Map([['implies/2', []], ['implied_by/2', []]]);
|
|
@@ -358,6 +380,33 @@ function restriction(ordinal, head, heads, body) {
|
|
|
358
380
|
return new Struct('not', [conjunction([...heads.map(rename), other])]);
|
|
359
381
|
}
|
|
360
382
|
|
|
383
|
+
// Whether firing a restricted rule can satisfy another's heads: a head of
|
|
384
|
+
// the first matches a head of the second that holds a witness, and the
|
|
385
|
+
// first's heads also match every other head of the second sharing that
|
|
386
|
+
// witness.
|
|
387
|
+
function restrains(first, second) {
|
|
388
|
+
const witnesses = new Set(second.witnessNames.map((name) => `${name}#restrained`));
|
|
389
|
+
const targets = flattenConjunction(freshTerm(conjunction(second.plainHeads), 'restrained'));
|
|
390
|
+
const own = flattenConjunction(freshTerm(conjunction(first.plainHeads), 'restraining'));
|
|
391
|
+
const witnessesOf = (term) => [...variables(term).keys()].filter((name) => witnesses.has(name));
|
|
392
|
+
const matchAll = (goals, env) => {
|
|
393
|
+
if (!goals.length) return true;
|
|
394
|
+
for (const head of own) {
|
|
395
|
+
const mark = env.mark();
|
|
396
|
+
if (unify(head, goals[0], env) && matchAll(goals.slice(1), env)) return true;
|
|
397
|
+
env.undo(mark);
|
|
398
|
+
}
|
|
399
|
+
return false;
|
|
400
|
+
};
|
|
401
|
+
for (const target of targets) {
|
|
402
|
+
const shared = witnessesOf(target);
|
|
403
|
+
if (!shared.length) continue;
|
|
404
|
+
const piece = targets.filter((other) => witnessesOf(other).some((name) => shared.includes(name)));
|
|
405
|
+
if (matchAll(piece, new Env())) return true;
|
|
406
|
+
}
|
|
407
|
+
return false;
|
|
408
|
+
}
|
|
409
|
+
|
|
361
410
|
// Whether a goal could contribute a closed dependency or a dynamic call.
|
|
362
411
|
function mayNeedStratifying(goal) {
|
|
363
412
|
if (goal instanceof Var || isTerm(goal, 'not', 1) || isTerm(goal, 'findall', 3)) return true;
|
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.17';
|