eyemind 0.5.15 → 0.5.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -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 1369 cases that test any implementation of the specification through its command line; run it [in your browser](https://eyereasoner.github.io/eyemind/playground/conformance).
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 1371 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
 
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.15
5
+ Version: eyemind 0.5.16
6
6
  ```
7
7
 
8
8
  This document specifies the syntax and meaning of eyemind programs and data
@@ -1146,13 +1146,40 @@ having added nothing. A rule thus never fires while a rule of a lower rank
1146
1146
  has a firing left.
1147
1147
 
1148
1148
  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 clause
1149
+ rules run in rounds as above; a round of its restricted rules, in restraint
1150
1150
  order, follows only a round of the other rules that adds nothing, and is
1151
1151
  followed by rounds of the other rules again when it adds something. The
1152
1152
  stratum ends with a round of its restricted rules that adds nothing. The
1153
1153
  other rules of a stratum thus run before its restricted rules, whose guards
1154
1154
  see what they derive.
1155
1155
 
1156
+ A restricted rule `A` **restrains** a restricted rule `B`, both renamed
1157
+ apart, when for some head `h` of `B` with a head-only variable of `B`, the
1158
+ heads of `B` sharing a head-only variable with `h` (`h` included) unify, one
1159
+ after another under one substitution, each with some head of `A`: firing
1160
+ `A` can then satisfy `B`'s heads. The **restraint order** of a set of
1161
+ restricted rules repeatedly takes the first rule, in clause order, that no
1162
+ other remaining rule restrains, or the first remaining rule when every one
1163
+ is restrained. A chain stratum ([Section 6.4](#64-chain-stratification)) runs its restricted rules after its
1164
+ other rules, in restraint order.
1165
+
1166
+ When reasoning ends without a contradiction, every guard of a published
1167
+ restricted firing MUST hold again, with the firing's bindings, in the final
1168
+ facts. Otherwise later facts satisfy the firing's heads, so its witness is
1169
+ not needed and the result depends on the order in which rules were applied:
1170
+ the run MUST fail with an error. For example,
1171
+
1172
+ ```js
1173
+ fact(f('a'))
1174
+ restricted(f(X), h(W))
1175
+ implies(g(X, Y), h(Y))
1176
+ restricted(f(X), g(X, W))
1177
+ ```
1178
+
1179
+ fails: the second rule makes `h(skolem(2, 'W', []))`, and the last rule's
1180
+ `g('a', skolem(4, 'W', ['a']))` then gives `h(skolem(4, 'W', ['a']))`,
1181
+ which satisfies the second rule's head too.
1182
+
1156
1183
  For a generator, use its scope projection ([Section 6.2](#62-inline-rule-templates)) as its head when
1157
1184
  constructing witnesses. For an appended forward clause of [Section 6.2](#62-inline-rule-templates),
1158
1185
  prepend one element to
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eyemind",
3
- "version": "0.5.15",
3
+ "version": "0.5.16",
4
4
  "description": "A dependency-free JavaScript rule language with forward and backward reasoning and checkable proofs.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
package/src/engine.js CHANGED
@@ -286,6 +286,7 @@ export class Solver {
286
286
  this.genericKeys = new Set();
287
287
  this.genericPredicates = new Set();
288
288
  this.derived = [];
289
+ this.guards = [];
289
290
  this.reported = new Map();
290
291
  this.stats = { inferences: 0, rounds: 0, derived: 0 };
291
292
  this.haltCode = null;
@@ -810,7 +811,9 @@ export class Solver {
810
811
  }
811
812
  if (this.recording || clause.templates.length) bindings = [...names].map(([name, value]) => [name, copyResolved(value, answerEnv)]);
812
813
  const claim = conclusions.some((item) => item === 'true') ? copyResolved(conjunction(body), answerEnv) : null;
813
- found.push([[conclusions, children, bindings, claim], this.chosen !== null ? [...this.chosen] : null]);
814
+ // A restricted firing keeps its guard, checked again once reasoning ends.
815
+ const guard = clause.guard === null || !unresolved.size ? null : copyResolved(body[body.length - 1], answerEnv);
816
+ found.push([[conclusions, children, bindings, claim, guard], this.chosen !== null ? [...this.chosen] : null]);
814
817
  answerEnv.undo(mark);
815
818
  }
816
819
  return found;
@@ -822,12 +825,39 @@ export class Solver {
822
825
  // stratum, restricted rules run only in a round where the other rules
823
826
  // derive nothing, so their guards see what those rules derive.
824
827
  forward(reporting = true) {
828
+ this.forwardRounds(reporting);
829
+ // A restricted rule's witness must still be needed once reasoning ends:
830
+ // otherwise later facts satisfy its heads, and the result depends on
831
+ // the order in which rules were applied (SPEC Section 8.2).
832
+ if (this.haltCode !== null) return;
833
+ for (const [clause, guard, conclusions] of this.guards) {
834
+ if (this.holds(guard)) continue;
835
+ throw new EyemindError(`restricted rule ${clause.id} concluded ${text(conclusions[0])}, whose witness later facts make unneeded; ` +
836
+ 'the result depends on the order of rule applications');
837
+ }
838
+ }
839
+
840
+ // Whether a goal holds in the current state.
841
+ holds(goal) {
842
+ for (const _ of this.solve([goal], new Env())) return true; // eslint-disable-line no-unused-vars
843
+ return false;
844
+ }
845
+
846
+ forwardRounds(reporting) {
825
847
  const layers = new Map();
826
848
  for (const clause of this.program.forward) {
827
849
  const rank = this.program.strata.get(clause.id) || 0;
828
850
  if (!layers.has(rank)) layers.set(rank, []);
829
851
  layers.get(rank).push(clause);
830
852
  }
853
+ // In a chain stratum too, a restricted rule runs after those whose heads
854
+ // can satisfy its own.
855
+ if (this.program.layered) {
856
+ for (const [rank, rules] of layers) {
857
+ const restricted = this.program.restraining(rules.filter((clause) => clause.restricted));
858
+ layers.set(rank, [...rules.filter((clause) => !clause.restricted), ...restricted]);
859
+ }
860
+ }
831
861
  const ranks = [...layers.keys()].sort((a, b) => a - b);
832
862
  const maxIterations = this.options.maxIterations || 1000;
833
863
  let rounds = 0;
@@ -851,7 +881,7 @@ export class Solver {
851
881
  for (const rank of ranks) {
852
882
  rounds = 0;
853
883
  const all = layers.get(rank);
854
- const restricted = all.filter((clause) => clause.restricted);
884
+ const restricted = this.program.restraining(all.filter((clause) => clause.restricted));
855
885
  const ordinary = restricted.length ? all.filter((clause) => !clause.restricted) : all;
856
886
  let changed = true;
857
887
  let lean = false;
@@ -879,7 +909,8 @@ export class Solver {
879
909
  // All instances of this template see the same state. Publishing one
880
910
  // instance's conclusions during another's search changes recursive
881
911
  // bodies and their observable order.
882
- for (const [clause, [conclusions, children, bindings, claim]] of firings) {
912
+ for (const [clause, [conclusions, children, bindings, claim, guard]] of firings) {
913
+ if (guard) this.guards.push([clause, guard, conclusions]);
883
914
  for (const [position, conclusion] of conclusions.entries()) {
884
915
  const template = clause.templates.find((item) => item.position === position);
885
916
  if (template) {
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.15';
2
+ export const VERSION = '0.5.16';