eyemind 0.3.11 → 0.4.0
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 -1
- package/SPEC.md +48 -13
- package/package.json +1 -1
- package/src/builtins.js +63 -1
- package/src/engine.js +23 -13
- package/src/program.js +14 -15
- package/src/proof.js +33 -3
- package/src/terms.js +40 -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 94 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 94 runnable programs, each with a card deck and saved conclusions, proofs and check reports. The conformance suite has 1344 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
|
|
|
@@ -123,6 +123,8 @@ Now add `fact(parent('carol', 'dave'))`, or remove a rule, and watch which answe
|
|
|
123
123
|
|
|
124
124
|
**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`.
|
|
125
125
|
|
|
126
|
+
**Text is atoms:** `atom_chars`, `atom_concat` and `atom_length` take them apart, and `regex_match(P, T)`, `regex_find(P, T, Groups)` and `regex_replace(P, T, R, U)` search them with ECMAScript regular expressions.
|
|
127
|
+
|
|
126
128
|
### Terms and names
|
|
127
129
|
|
|
128
130
|
- **Nothing to declare.** An undeclared name is a variable when it starts with a capital or `_` (`X`, `_Rest`) and a predicate otherwise (`parent`). `_` is a fresh variable at each use. JavaScript globals such as `Math` and `JSON` keep their meaning; `const range = preds('range')` declares a predicate whose name would otherwise clash. A misspelled statement such as `fcat(p(1))` states nothing, so eyemind stops with an error.
|
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
|
+
Version: eyemind 0.4.0
|
|
6
6
|
```
|
|
7
7
|
|
|
8
8
|
This document specifies the syntax and meaning of eyemind programs and data
|
|
@@ -575,9 +575,24 @@ primitive called outside its patterns raises an error.
|
|
|
575
575
|
| `atom_length` | `atom_length(+A, ?N)` | `N` is the number of characters of `A`. |
|
|
576
576
|
| `number_chars` | `number_chars(?N, +L)`, `number_chars(+N, -L)` | A bound `L` is read as a number: an optional sign and decimal digits give an integer, and with a fraction or an exponent a float, as in `'007'`, `'-21.5'`, `'.5'`, `'1.'` and `'2E-2'`; other text fails, as does text whose value is too large for a finite float, such as `'1e400'`. Otherwise `L` spells `N` in its canonical text ([Section 9.2](#92-canonical-atoms-numbers-variables-and-lists)). |
|
|
577
577
|
| `atom_concat` | `atom_concat(+A, +B, ?AB)`, `atom_concat(?A, ?B, +AB)` | `AB` is `A` followed by `B`; with `AB` given, every split, shortest `A` first. |
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
578
|
+
| `regex_match` | `regex_match(+P, +T)` | Some part of the atom `T` matches the regular expression `P`. |
|
|
579
|
+
| `regex_find` | `regex_find(+P, +T, ?G)` | One solution per match of `P` in `T`, from left to right: `G` is the list of the matched text followed by each capture group's text, `''` for a group that took no part in the match. |
|
|
580
|
+
| `regex_replace` | `regex_replace(+P, +T, +R, ?U)` | `U` is `T` with every match of `P` replaced by `R`. |
|
|
581
|
+
|
|
582
|
+
A character is a Unicode code point. Every primitive except `atom_concat`
|
|
583
|
+
and `regex_find` has at most one solution.
|
|
584
|
+
|
|
585
|
+
A regular expression `P` is an atom read as an ECMAScript pattern
|
|
586
|
+
(ECMA-262, the `RegExp` pattern grammar and matching semantics) with only the
|
|
587
|
+
`u` flag, so it matches by code point: `.` matches `'\u{1F600}'` as one
|
|
588
|
+
character, `^` and `$` match only at the ends of the text, and matching is
|
|
589
|
+
case-sensitive. A pattern that is not valid in that mode, such as `'('`,
|
|
590
|
+
raises an error. `regex_find` and `regex_replace` take the matches as a
|
|
591
|
+
global search does: after an empty match the search resumes one code point
|
|
592
|
+
later, so `regex_find('a*', 'baa', G)` gives `['']`, `['aa']` and `['']`. In
|
|
593
|
+
the replacement `R`, `$&` stands for the match, `$1` to `$99` and `$<Name>`
|
|
594
|
+
for its groups, `` $` `` and `$'` for the text before and after it, and `$$`
|
|
595
|
+
for `$`; any other `$` is itself. Primitives MUST be pure: their solutions depend only on
|
|
581
596
|
their arguments. A primitive given an argument of the wrong kind for its
|
|
582
597
|
patterns raises an error, as `atom_length(123, N)` does; one given arguments
|
|
583
598
|
of the right kinds that has no solution fails, as `arg(5, f(1), A)` does.
|
|
@@ -1057,9 +1072,10 @@ and MUST fail with an error rather than return a proof that does not pass. A
|
|
|
1057
1072
|
run without conclusions has a proof without claims, which is valid and
|
|
1058
1073
|
certifies nothing.
|
|
1059
1074
|
|
|
1060
|
-
A
|
|
1061
|
-
|
|
1062
|
-
|
|
1075
|
+
A **state test** is `is_var/1`, `not_identical/2` or `compare/3`: a test a
|
|
1076
|
+
later binding can make false. A goal such as `is_var(X)` followed by
|
|
1077
|
+
`unify(X, 'a')` holds when it runs, but its final instance `is_var('a')` does
|
|
1078
|
+
not, so the proof records it as it was called ([Section 10.3](#103-steps)).
|
|
1063
1079
|
|
|
1064
1080
|
## 9. Document Syntax and Canonical Text
|
|
1065
1081
|
|
|
@@ -1255,6 +1271,7 @@ order. `By` is one of:
|
|
|
1255
1271
|
| `clause(N)` | `Goal` is an instance of a head of clause `N`, a fact or a rule, whose body instance is `Uses`. | the clause's variables | the body instance, empty for a fact |
|
|
1256
1272
|
| `source(N)` | `Goal` is an instance of the inspectable source rule `N` as `implies(Body, Head)` or `implied_by(Head, Body)`. | empty | empty |
|
|
1257
1273
|
| `'builtin'` | `Goal` is a primitive that holds. | empty | empty |
|
|
1274
|
+
| `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 |
|
|
1258
1275
|
| `'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 |
|
|
1259
1276
|
| `'absent'` | `Goal` is a negation, settled by the search certificate or taken on trust. | empty | empty |
|
|
1260
1277
|
| `'collected'` | `Goal` is a `findall`, settled by the search certificate or taken on trust. | empty | empty |
|
|
@@ -1268,8 +1285,17 @@ first shows them (for `implies`, the premise first);
|
|
|
1268
1285
|
a checker MUST accept partial bindings when the goal and uses determine the
|
|
1269
1286
|
omitted values needed by C1 and C8. Values needed only by an enclosing
|
|
1270
1287
|
witness scope must be supplied when those terms do not determine them.
|
|
1271
|
-
A primitive has no variables of its own, so `'builtin'`
|
|
1272
|
-
empty bindings. A
|
|
1288
|
+
A primitive has no variables of its own, so `'builtin'` and `called`
|
|
1289
|
+
steps have empty bindings. A reasoner writes `called(Called)` exactly for a
|
|
1290
|
+
state test whose goal, when it held, had a variable the solution later
|
|
1291
|
+
bound; `Called` is that goal after the test, so `compare(O, X, 'b')` run
|
|
1292
|
+
with `X` unbound is written `called(compare('<', A, 'b'))`. Every other
|
|
1293
|
+
primitive step, and a state test no later binding changed, is `'builtin'`:
|
|
1294
|
+
|
|
1295
|
+
```js
|
|
1296
|
+
implied_by(p(X), [is_var(X), unify(X, 'a')])
|
|
1297
|
+
// step(is_var('a'), called(is_var(A)), {}, [])
|
|
1298
|
+
``` A derived clause's bindings also include variables of its
|
|
1273
1299
|
enclosing generators' scope projections, so their witnesses can be checked.
|
|
1274
1300
|
These variables are renamed together with the clause's head and full body.
|
|
1275
1301
|
|
|
@@ -1395,7 +1421,8 @@ failure, whose conclusion is the goal of that step.
|
|
|
1395
1421
|
### 11.4 C3 Justification
|
|
1396
1422
|
|
|
1397
1423
|
`By` MUST be one of the forms of [Section 10.3](#103-steps). A `'builtin'` step MUST have
|
|
1398
|
-
no bindings and no uses and a goal that is a primitive;
|
|
1424
|
+
no bindings and no uses and a goal that is a primitive; a `called` step MUST
|
|
1425
|
+
have no bindings and no uses and a goal that is a state test; an `'absent'` or
|
|
1399
1426
|
`'collected'` step MUST have no bindings and no uses and a goal that is a
|
|
1400
1427
|
negation or a `findall`, respectively. A step that fails C3 is not a trusted
|
|
1401
1428
|
boundary: it counts in neither `trusted`, C6 nor C9.
|
|
@@ -1411,7 +1438,13 @@ nothing.
|
|
|
1411
1438
|
### 11.6 C5 Re-decision
|
|
1412
1439
|
|
|
1413
1440
|
A `'builtin'` step's goal, solved as a primitive with no prior bindings, MUST
|
|
1414
|
-
succeed without binding anything.
|
|
1441
|
+
succeed without binding anything. For a `called(Called)` step, `Called`
|
|
1442
|
+
MUST have the goal's name and arity; `Called`, renamed apart and solved as a
|
|
1443
|
+
primitive with no prior bindings, MUST succeed without binding anything; and
|
|
1444
|
+
the goal MUST be an instance of `Called`, so only the variables `Called`
|
|
1445
|
+
leaves unbound may differ. C5 does not establish that the test ran in that
|
|
1446
|
+
state: like the choice of a `once` step, the call state is operational
|
|
1447
|
+
([Section 11.13](#1113-operational-query-fidelity)), and C10 checks the original questions. A `'control'` step MUST have no bindings,
|
|
1415
1448
|
and its uses MUST be identical, in order, to the conjuncts of the argument of
|
|
1416
1449
|
`call` or `once`, or of one side of a disjunction. When trusted boundaries are
|
|
1417
1450
|
forbidden ("strict"), each `'absent'` or `'collected'` step fails C5.
|
|
@@ -1620,7 +1653,7 @@ settled nor refuted with the evidence ([Section 11.7](#117-c6-boundary-consisten
|
|
|
1620
1653
|
|
|
1621
1654
|
A check also counts the **steps** it read, the **claims**, the `clause` steps
|
|
1622
1655
|
**verified** by C1, the `source` inspection steps also **verified** by C1,
|
|
1623
|
-
the `'builtin'` steps **recomputed** by C5, the
|
|
1656
|
+
the `'builtin'` and `called` steps **recomputed** by C5, the
|
|
1624
1657
|
`'control'` steps **composed** by C5, and the `'absent'` and `'collected'`
|
|
1625
1658
|
steps **trusted**, those C9 did not settle, plus each claim whose call
|
|
1626
1659
|
fidelity remains unproved by C10.
|
|
@@ -1974,7 +2007,9 @@ Programs execute JavaScript and require the caller's trust. Documents,
|
|
|
1974
2007
|
including proofs, reports, goals and `facts_from` input, MUST be read as data
|
|
1975
2008
|
without executing code. A deployment accepting untrusted documents SHOULD
|
|
1976
2009
|
bound their size, nesting depth, processing time and memory, in addition to
|
|
1977
|
-
the reasoning bounds of [Section 8.3](#83-resource-bounds). A
|
|
2010
|
+
the reasoning bounds of [Section 8.3](#83-resource-bounds). A regular expression can take time exponential in
|
|
2011
|
+
the length of the text it searches ([Section 4.3](#43-primitives)), and no reasoning bound counts
|
|
2012
|
+
that time, so a deployment running untrusted programs SHOULD also bound it. A valid proof establishes only the
|
|
1978
2013
|
claims and assumptions described in [Section 11.12](#1112-scope-of-a-valid-proof).
|
|
1979
2014
|
|
|
1980
2015
|
## Appendix A. Document Grammar
|
package/package.json
CHANGED
package/src/builtins.js
CHANGED
|
@@ -149,6 +149,48 @@ function is(args, resolved, env, bind) {
|
|
|
149
149
|
return bind(args[0], arith.evaluate(args[1], env));
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
+
// Regular expressions use ECMAScript's syntax and matching (ECMA-262), always
|
|
153
|
+
// in its Unicode mode, so a pattern means the same on every host. A pattern
|
|
154
|
+
// compiles once.
|
|
155
|
+
const patterns = new Map();
|
|
156
|
+
|
|
157
|
+
function pattern(name, term, global) {
|
|
158
|
+
if (typeof term !== 'string') throw new EyemindError(`${name} needs an atom pattern`);
|
|
159
|
+
const cacheKey = `${global ? 'g' : '-'}${term}`;
|
|
160
|
+
let compiled = patterns.get(cacheKey);
|
|
161
|
+
if (compiled === undefined) {
|
|
162
|
+
try {
|
|
163
|
+
compiled = new RegExp(term, global ? 'gu' : 'u');
|
|
164
|
+
} catch (error) {
|
|
165
|
+
throw new EyemindError(`${name} needs a valid regular expression: ${error.message}`);
|
|
166
|
+
}
|
|
167
|
+
if (patterns.size > 1000) patterns.clear();
|
|
168
|
+
patterns.set(cacheKey, compiled);
|
|
169
|
+
}
|
|
170
|
+
compiled.lastIndex = 0;
|
|
171
|
+
return compiled;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function text(name, term, what = 'text') {
|
|
175
|
+
if (typeof term !== 'string') throw new EyemindError(`${name} needs an atom ${what}`);
|
|
176
|
+
return term;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// regex_match(+P, +T): some part of T matches P.
|
|
180
|
+
function regexMatch(args, resolved) {
|
|
181
|
+
return pattern('regex_match/2', resolved[0], false).test(text('regex_match/2', resolved[1]));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// regex_replace(+P, +T, +R, ?Out): Out is T with every match of P replaced
|
|
185
|
+
// by R, where $& is the match, $1 ... $99 and $<name> its groups, $` and $'
|
|
186
|
+
// the text before and after it, and $$ a dollar sign.
|
|
187
|
+
function regexReplace(args, resolved, env, bind) {
|
|
188
|
+
const compiled = pattern('regex_replace/4', resolved[0], true);
|
|
189
|
+
const source = text('regex_replace/4', resolved[1]);
|
|
190
|
+
const replacement = text('regex_replace/4', resolved[2], 'replacement');
|
|
191
|
+
return bind(args[3], source.replace(compiled, replacement));
|
|
192
|
+
}
|
|
193
|
+
|
|
152
194
|
const isNumberValue = (term) => typeof term === 'bigint' || typeof term === 'number';
|
|
153
195
|
|
|
154
196
|
export const TESTS = new Map([
|
|
@@ -182,6 +224,8 @@ export const TESTS = new Map([
|
|
|
182
224
|
['atom_length/2', atomLength],
|
|
183
225
|
['number_chars/2', numberChars],
|
|
184
226
|
['compare/3', compare],
|
|
227
|
+
['regex_match/2', regexMatch],
|
|
228
|
+
['regex_replace/4', regexReplace],
|
|
185
229
|
]);
|
|
186
230
|
|
|
187
231
|
function* atomConcat(args, resolved, env, mark) {
|
|
@@ -199,7 +243,25 @@ function* atomConcat(args, resolved, env, mark) {
|
|
|
199
243
|
}
|
|
200
244
|
}
|
|
201
245
|
|
|
202
|
-
|
|
246
|
+
// regex_find(+P, +T, ?Groups): one solution per match of P in T, from left
|
|
247
|
+
// to right without overlaps; Groups is the match followed by its groups, a
|
|
248
|
+
// group that took no part in the match being ''.
|
|
249
|
+
function* regexFind(args, resolved, env, mark) {
|
|
250
|
+
const compiled = pattern('regex_find/3', resolved[0], true);
|
|
251
|
+
const source = text('regex_find/3', resolved[1]);
|
|
252
|
+
for (const match of source.matchAll(compiled)) {
|
|
253
|
+
if (unify(args[2], listFromItems(Array.from(match, (group) => group ?? '')), env)) yield env;
|
|
254
|
+
env.undo(mark);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export const RELATIONS = new Map([['atom_concat/3', atomConcat], ['regex_find/3', regexFind]]);
|
|
259
|
+
|
|
260
|
+
// Tests that a later binding can make false: is_var(X) holds while X is
|
|
261
|
+
// unbound, not_identical(X, a) until X is a, and compare orders a variable
|
|
262
|
+
// before every other term. A proof records such a step by the goal as it
|
|
263
|
+
// was called, called(Goal), since its final instance need not hold.
|
|
264
|
+
export const STATE_TESTS = new Set(['is_var/1', 'not_identical/2', 'compare/3']);
|
|
203
265
|
export const PRIMITIVE_KEYS = new Set([...TESTS.keys(), ...RELATIONS.keys()]);
|
|
204
266
|
|
|
205
267
|
// Solve a primitive goal: a generator yielding env once per solution.
|
package/src/engine.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Backward resolution, forward fixpoints and proof recording.
|
|
2
|
-
import { PRIMITIVE_KEYS, primitive } from './builtins.js';
|
|
2
|
+
import { PRIMITIVE_KEYS, STATE_TESTS, primitive } from './builtins.js';
|
|
3
3
|
import { Reliance, Settler, Unsettled } from './closure.js';
|
|
4
4
|
import { freshClause, matchHead, renaming, text, variant } from './common.js';
|
|
5
5
|
import { goalTerm, subgoals } from './goals.js';
|
|
@@ -9,7 +9,7 @@ import { checkRendered, clauseDisplay, renderProof } from './proof.js';
|
|
|
9
9
|
import { CONTROL_KEYS, Program } from './program.js';
|
|
10
10
|
import {
|
|
11
11
|
Env, EyemindError, Struct, Var, conjunction, copyResolved, deref, flattenConjunction, freshTerm,
|
|
12
|
-
identity, isAnonymous, isCallable, isGround, isGroundButAnonymous, isTerm, key, keyArity, listFromItems,
|
|
12
|
+
identity, indexKey, isAnonymous, isCallable, isGround, isGroundButAnonymous, isTerm, key, keyArity, listFromItems,
|
|
13
13
|
unify, variables,
|
|
14
14
|
} from './terms.js';
|
|
15
15
|
import { Lettering, SourceNaming, write } from './writer.js';
|
|
@@ -107,7 +107,10 @@ const QUIET = new Pending(null, null, NO_NODES, null);
|
|
|
107
107
|
const SPLICE = 'splice';
|
|
108
108
|
|
|
109
109
|
function resolveOne(node, env) {
|
|
110
|
-
|
|
110
|
+
const goal = copyResolved(node.goal, env);
|
|
111
|
+
// A state test whose goal no later binding changed is an ordinary builtin.
|
|
112
|
+
const by = isTerm(node.by, 'called', 1) && variant(goal) === variant(node.by.args[0]) ? BUILTIN : node.by;
|
|
113
|
+
return new Node(goal, by,
|
|
111
114
|
node.bindings.map(([name, value]) => [name, copyResolved(value, env)]),
|
|
112
115
|
new Array(node.children.length).fill(null), node.collectedUses, false,
|
|
113
116
|
node.scoped.map((term) => copyResolved(term, env)));
|
|
@@ -197,7 +200,7 @@ export function clausesUsed(nodes, used = new Set()) {
|
|
|
197
200
|
const node = pending.pop();
|
|
198
201
|
if (seen.has(node)) continue;
|
|
199
202
|
seen.add(node);
|
|
200
|
-
if (node.by
|
|
203
|
+
if (isTerm(node.by, 'clause', 1) || isTerm(node.by, 'source', 1)) used.add(Number(node.by.args[0]));
|
|
201
204
|
if (node.collectedUses) for (const id of node.collectedUses) used.add(id);
|
|
202
205
|
for (const child of node.children) pending.push(child);
|
|
203
206
|
}
|
|
@@ -268,8 +271,6 @@ function compareChosen(a, b) {
|
|
|
268
271
|
return a.length - b.length;
|
|
269
272
|
}
|
|
270
273
|
|
|
271
|
-
const isIndexable = (value) => typeof value === 'string' || typeof value === 'bigint' || typeof value === 'number';
|
|
272
|
-
|
|
273
274
|
export class Solver {
|
|
274
275
|
constructor(program, options) {
|
|
275
276
|
this.program = program;
|
|
@@ -541,9 +542,10 @@ export class Solver {
|
|
|
541
542
|
this.factIndex.set(goalKey, positions);
|
|
542
543
|
}
|
|
543
544
|
node.goal.args.forEach((arg, position) => {
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
545
|
+
const argument = indexKey(arg);
|
|
546
|
+
if (argument !== undefined) {
|
|
547
|
+
const bucket = positions[position].get(argument);
|
|
548
|
+
if (bucket === undefined) positions[position].set(argument, [number]);
|
|
547
549
|
else bucket.push(number);
|
|
548
550
|
}
|
|
549
551
|
});
|
|
@@ -559,9 +561,8 @@ export class Solver {
|
|
|
559
561
|
let best = null;
|
|
560
562
|
let size = this.facts.get(goalKey).length;
|
|
561
563
|
for (let position = 0; position < goal.args.length; position++) {
|
|
562
|
-
|
|
563
|
-
if (arg
|
|
564
|
-
if (isIndexable(arg)) {
|
|
564
|
+
const arg = indexKey(goal.args[position], env);
|
|
565
|
+
if (arg !== undefined) {
|
|
565
566
|
const bucket = positions[position].get(arg) || NO_NODES;
|
|
566
567
|
if (bucket.length < size) {
|
|
567
568
|
best = bucket;
|
|
@@ -573,6 +574,15 @@ export class Solver {
|
|
|
573
574
|
return best;
|
|
574
575
|
}
|
|
575
576
|
|
|
577
|
+
// How a solved primitive is justified: a state test whose goal still has a
|
|
578
|
+
// variable keeps its instance as called, renamed apart from later bindings.
|
|
579
|
+
primitiveBy(goal, env) {
|
|
580
|
+
if (!STATE_TESTS.has(key(goal))) return BUILTIN;
|
|
581
|
+
const called = copyResolved(goal, env);
|
|
582
|
+
if (isGround(called)) return BUILTIN;
|
|
583
|
+
return new Struct('called', [freshTerm(called, `called${++this.serial}`, new Map(), true)]);
|
|
584
|
+
}
|
|
585
|
+
|
|
576
586
|
// Take the next untried alternative of a choice point, or null. The caller
|
|
577
587
|
// has already undone the bindings of the previous one.
|
|
578
588
|
retry(point, env) {
|
|
@@ -584,7 +594,7 @@ export class Solver {
|
|
|
584
594
|
this.chosen[point.frame.index] = [0, point.position];
|
|
585
595
|
point.position++;
|
|
586
596
|
}
|
|
587
|
-
return this.advance(point.frame, recording ? new Node(point.goal,
|
|
597
|
+
return this.advance(point.frame, recording ? new Node(point.goal, this.primitiveBy(point.goal, env)) : null);
|
|
588
598
|
}
|
|
589
599
|
const count = point.count();
|
|
590
600
|
while (point.position < count) {
|
package/src/program.js
CHANGED
|
@@ -4,7 +4,7 @@ import { containsTemplate, expandTemplates } from './templates.js';
|
|
|
4
4
|
import { subgoals } from './goals.js';
|
|
5
5
|
import { hasInspection, inspectionTerm } from './inspection.js';
|
|
6
6
|
import {
|
|
7
|
-
EMPTY, Env, EyemindError, Struct, Var, deref, flattenConjunction, freshTerm, identity, isCallable,
|
|
7
|
+
EMPTY, Env, EyemindError, Struct, Var, deref, flattenConjunction, freshTerm, identity, indexKey, isCallable,
|
|
8
8
|
isGround, isTerm, key, keyArity, unify, variables,
|
|
9
9
|
} from './terms.js';
|
|
10
10
|
import { write } from './writer.js';
|
|
@@ -14,7 +14,6 @@ export const RESERVED_KEYS = new Set(['step/4', 'clause/2', 'searched/2', 'deriv
|
|
|
14
14
|
|
|
15
15
|
// The statements that state a forward rule, and how an error names them.
|
|
16
16
|
const FORWARD_KINDS = { forward: 'a forward rule', query: 'query()', contradiction: 'contradiction()' };
|
|
17
|
-
const isIndexable = (value) => typeof value === 'string' || typeof value === 'bigint' || typeof value === 'number';
|
|
18
17
|
|
|
19
18
|
export class Clause {
|
|
20
19
|
constructor(id, head, heads, body, forward, line, captures = null) {
|
|
@@ -228,9 +227,8 @@ export class Program {
|
|
|
228
227
|
for (let position = 0; position < positions.length; position++) {
|
|
229
228
|
const index = positions[position];
|
|
230
229
|
if (index === null) continue;
|
|
231
|
-
|
|
232
|
-
if (argument
|
|
233
|
-
if (!isIndexable(argument)) continue;
|
|
230
|
+
const argument = indexKey(args[position], env);
|
|
231
|
+
if (argument === undefined) continue;
|
|
234
232
|
const bucket = bucketEntries(index[0].get(argument));
|
|
235
233
|
const size = bucket.length + index[1].length;
|
|
236
234
|
if (size < count) {
|
|
@@ -256,11 +254,11 @@ export class Program {
|
|
|
256
254
|
}
|
|
257
255
|
}
|
|
258
256
|
|
|
259
|
-
// An index per argument position where enough entries carry an
|
|
260
|
-
//
|
|
261
|
-
//
|
|
262
|
-
//
|
|
263
|
-
// unification does. A value with one entry maps to the entry itself, and one
|
|
257
|
+
// An index per argument position where enough entries carry an index key:
|
|
258
|
+
// an atom, a number or a small ground compound (indexKey). Each index is
|
|
259
|
+
// [keyed, other]: a map from key to the entries with that key there, and the
|
|
260
|
+
// entries with anything else there. Map keys keep integers and floats
|
|
261
|
+
// distinct while equating signed zeros, just as unification does. A value with one entry maps to the entry itself, and one
|
|
264
262
|
// with more to a Bucket of them: most values of a large program head a single
|
|
265
263
|
// clause, and an array for each would only be more for memory to keep.
|
|
266
264
|
export function positionIndexes(entries, headOf, arity) {
|
|
@@ -268,7 +266,7 @@ export function positionIndexes(entries, headOf, arity) {
|
|
|
268
266
|
if (arity) {
|
|
269
267
|
for (let i = 0; i < entries.length; i++) {
|
|
270
268
|
const args = headOf(entries[i]).args;
|
|
271
|
-
for (let position = 0; position < arity; position++) if (
|
|
269
|
+
for (let position = 0; position < arity; position++) if (indexKey(args[position]) !== undefined) counts[position]++;
|
|
272
270
|
}
|
|
273
271
|
}
|
|
274
272
|
const positions = counts.map((count) => (count * 2 < entries.length ? null : [new Map(), []]));
|
|
@@ -278,8 +276,8 @@ export function positionIndexes(entries, headOf, arity) {
|
|
|
278
276
|
for (let position = 0; position < arity; position++) {
|
|
279
277
|
const index = positions[position];
|
|
280
278
|
if (index === null) continue;
|
|
281
|
-
const argument = args[position];
|
|
282
|
-
if (
|
|
279
|
+
const argument = indexKey(args[position]);
|
|
280
|
+
if (argument === undefined) {
|
|
283
281
|
index[1].push(entry);
|
|
284
282
|
continue;
|
|
285
283
|
}
|
|
@@ -346,8 +344,9 @@ function headCandidates(entry, goal) {
|
|
|
346
344
|
if (entry === undefined) return [];
|
|
347
345
|
let chosen = entry.pairs;
|
|
348
346
|
entry.positions.forEach((bucket, position) => {
|
|
349
|
-
|
|
350
|
-
|
|
347
|
+
const argument = goal instanceof Struct ? indexKey(goal.args[position]) : undefined;
|
|
348
|
+
if (bucket === null || argument === undefined) return;
|
|
349
|
+
const matched = bucketEntries(bucket[0].get(argument));
|
|
351
350
|
if (matched.length + bucket[1].length < chosen.length) chosen = [...matched, ...bucket[1]];
|
|
352
351
|
});
|
|
353
352
|
return chosen;
|
package/src/proof.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
//
|
|
10
10
|
// Verification follows recorded uses; it never asks the solver to find a
|
|
11
11
|
// missing derivation. Only pure primitives are independently recomputed.
|
|
12
|
-
import { PRIMITIVE_KEYS, primitive } from './builtins.js';
|
|
12
|
+
import { PRIMITIVE_KEYS, STATE_TESTS, primitive } from './builtins.js';
|
|
13
13
|
import { Settler } from './closure.js';
|
|
14
14
|
import { freshClause, text, variant } from './common.js';
|
|
15
15
|
import { goalTerm } from './goals.js';
|
|
@@ -197,7 +197,7 @@ export function renderProof(program, claims, roots, checkable = false, heading =
|
|
|
197
197
|
const nodeId = spellings.spell(node.goal);
|
|
198
198
|
if (steps.has(nodeId)) continue;
|
|
199
199
|
steps.set(nodeId, node);
|
|
200
|
-
if (node.by
|
|
200
|
+
if (isTerm(node.by, 'clause', 1) || isTerm(node.by, 'source', 1)) {
|
|
201
201
|
const id = Number(node.by.args[0]);
|
|
202
202
|
cited.add(id);
|
|
203
203
|
for (const parent of program.byId.get(id)?.scopeIds || []) cited.add(parent);
|
|
@@ -228,7 +228,8 @@ export function renderProof(program, claims, roots, checkable = false, heading =
|
|
|
228
228
|
}
|
|
229
229
|
for (const node of steps.values()) {
|
|
230
230
|
const uses = `[${node.children.map((child) => spellings.lettered(child.goal, lettering)).join(', ')}]`;
|
|
231
|
-
|
|
231
|
+
const by = isTerm(node.by, 'called', 1) ? spellings.lettered(node.by, lettering) : text(node.by);
|
|
232
|
+
lines.push(`step(${spellings.lettered(node.goal, lettering)}, ${by}, ` +
|
|
232
233
|
`${bindingsText(node.bindings, lettering)}, ${uses})`);
|
|
233
234
|
}
|
|
234
235
|
const read = { tables: new Map(), calls: new Map(), listings: new Map(), order: [] };
|
|
@@ -510,6 +511,13 @@ function checkSteps(program, steps, allowTrusted, fail, spellings, name, outcome
|
|
|
510
511
|
const agrees = redecides(goal);
|
|
511
512
|
if (!agrees) fail.add('C5', `primitive disagrees: ${text(goal)}`, goal);
|
|
512
513
|
else tally.redecided++;
|
|
514
|
+
} else if (isTerm(by, 'called', 1)) {
|
|
515
|
+
if (bindings.length || uses.length || !isCallable(goal) || !STATE_TESTS.has(key(goal))) {
|
|
516
|
+
fail.add('C3', 'invalid called justification', goal);
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
if (!redecidesCalled(goal, by.args[0])) fail.add('C5', `primitive disagrees as called: ${text(by.args[0])}`, goal);
|
|
520
|
+
else tally.redecided++;
|
|
513
521
|
} else if (by === CONTROL_BY) {
|
|
514
522
|
let candidates = [];
|
|
515
523
|
if (isTerm(goal, 'call', 1) || isTerm(goal, 'once', 1)) candidates = [goal.args[0]];
|
|
@@ -551,6 +559,25 @@ function redecides(goal) {
|
|
|
551
559
|
return nearHostMath(goal);
|
|
552
560
|
}
|
|
553
561
|
|
|
562
|
+
// A state test recorded as called: the called goal, renamed apart, holds as
|
|
563
|
+
// a primitive without binding anything, and the step's goal is an instance
|
|
564
|
+
// of it, the same primitive with only its variables since bound.
|
|
565
|
+
function redecidesCalled(goal, called) {
|
|
566
|
+
if (!isCallable(called) || key(called) !== key(goal)) return false;
|
|
567
|
+
const renamed = freshTerm(called, 'calledCheck');
|
|
568
|
+
try {
|
|
569
|
+
let holds = false;
|
|
570
|
+
for (const env of primitive(renamed, new Env())) {
|
|
571
|
+
holds = resolvesTo(renamed, env, renamed);
|
|
572
|
+
break;
|
|
573
|
+
}
|
|
574
|
+
return holds && isInstance(goal, called, 'calledInstance');
|
|
575
|
+
} catch (error) {
|
|
576
|
+
if (!(error instanceof EyemindError)) throw error;
|
|
577
|
+
return false;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
554
581
|
const isConstant = (term) => typeof term === 'string' || typeof term === 'bigint' || typeof term === 'number';
|
|
555
582
|
|
|
556
583
|
// Shortcuts the checker may take; tests turn them off to compare with the
|
|
@@ -818,6 +845,9 @@ class WitnessProvenance {
|
|
|
818
845
|
} else if (step.by === BUILTIN_BY) {
|
|
819
846
|
if (step.bindings.length || step.uses.length || !isCallable(goal) ||
|
|
820
847
|
!PRIMITIVE_KEYS.has(key(goal)) || !redecides(goal)) return false;
|
|
848
|
+
} else if (isTerm(step.by, 'called', 1)) {
|
|
849
|
+
if (step.bindings.length || step.uses.length || !isCallable(goal) ||
|
|
850
|
+
!STATE_TESTS.has(key(goal)) || !redecidesCalled(goal, step.by.args[0])) return false;
|
|
821
851
|
} else if (step.by === CONTROL_BY) {
|
|
822
852
|
const candidates = isTerm(goal, 'call', 1) || isTerm(goal, 'once', 1) ? [goal.args[0]]
|
|
823
853
|
: isTerm(goal, 'or', 2) ? goal.args : [];
|
package/src/terms.js
CHANGED
|
@@ -482,6 +482,46 @@ export function identity(term) {
|
|
|
482
482
|
return out.join('\u0001');
|
|
483
483
|
}
|
|
484
484
|
|
|
485
|
+
// The key an argument index files a term under: an atom or a number is its
|
|
486
|
+
// own key, and a small ground compound, such as iri('http://e/a'), the text
|
|
487
|
+
// of its structure. Signed zeros share a key, as they unify. A variable, a
|
|
488
|
+
// compound holding one, or one of more than INDEX_NODES nodes has none and
|
|
489
|
+
// constrains nothing. A key is only a filter, so an atom whose text happens
|
|
490
|
+
// to equal a compound's key could at most add candidates, never lose one.
|
|
491
|
+
const INDEX_NODES = 64;
|
|
492
|
+
|
|
493
|
+
export function indexKey(term, env = null) {
|
|
494
|
+
if (term instanceof Var) {
|
|
495
|
+
if (env === null) return undefined;
|
|
496
|
+
term = deref(term, env);
|
|
497
|
+
if (term instanceof Var) return undefined;
|
|
498
|
+
}
|
|
499
|
+
if (!(term instanceof Struct)) return term;
|
|
500
|
+
const out = ['\u0000k'];
|
|
501
|
+
const pending = [term];
|
|
502
|
+
let nodes = 0;
|
|
503
|
+
while (pending.length) {
|
|
504
|
+
let item = pending.pop();
|
|
505
|
+
if (item instanceof Var) {
|
|
506
|
+
if (env === null) return undefined;
|
|
507
|
+
item = deref(item, env);
|
|
508
|
+
if (item instanceof Var) return undefined;
|
|
509
|
+
}
|
|
510
|
+
if (++nodes > INDEX_NODES) return undefined;
|
|
511
|
+
if (item instanceof Struct) {
|
|
512
|
+
out.push('c', String(item.name.length), item.name, String(item.args.length));
|
|
513
|
+
for (let i = item.args.length - 1; i >= 0; i--) pending.push(item.args[i]);
|
|
514
|
+
} else if (typeof item === 'string') {
|
|
515
|
+
out.push('a', String(item.length), item);
|
|
516
|
+
} else if (typeof item === 'bigint') {
|
|
517
|
+
out.push('i', String(item));
|
|
518
|
+
} else {
|
|
519
|
+
out.push('f', String(item));
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return out.join('\u0001');
|
|
523
|
+
}
|
|
524
|
+
|
|
485
525
|
// The variables of a term by name, in order of first appearance.
|
|
486
526
|
export function variables(term, result = new Map()) {
|
|
487
527
|
const pending = [term];
|
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.
|
|
2
|
+
export const VERSION = '0.4.0';
|