eyemind 0.9.5 → 0.9.7
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/LE-SPEC.md +53 -2
- package/LS-SPEC.md +1 -1
- package/README.md +8 -5
- package/package.json +1 -1
- package/src/cli.js +17 -0
- package/src/index.js +1 -0
- package/src/le-explain.js +395 -0
- package/src/le.js +26 -5
- package/src/proof.js +10 -0
- package/src/version.js +1 -1
package/LE-SPEC.md
CHANGED
|
@@ -70,7 +70,11 @@ line, and `/* ... */` is a block comment. Words are compared without case.
|
|
|
70
70
|
A token is a word (letters, digits and underscores, joined by hyphens or
|
|
71
71
|
apostrophes, as in `JE-201` and `don't`), a final `'s` as a word of its own, a
|
|
72
72
|
number (`42`, `3.14`, `1,000,000`), a date (`2021-10-09`), a quoted text
|
|
73
|
-
(`"..."` or `'...'`),
|
|
73
|
+
(`"..."` or `'...'`), a web address (`https://example.org/a::b`, up to white
|
|
74
|
+
space and without a final `.`, `,`, `;` or `:`), a date followed at once by a
|
|
75
|
+
time (`2018-06-29T10:30:00Z`, `2018-06-29:00:00`), or a punctuation sign. A web
|
|
76
|
+
address and a date with a time are words of a name, written as they are; only
|
|
77
|
+
a date has a calendar meaning. The indentation of a line counts a
|
|
74
78
|
space as one and a tab as four, as LE's editor shows it.
|
|
75
79
|
|
|
76
80
|
## Templates
|
|
@@ -144,7 +148,9 @@ The tokens in a place are a term:
|
|
|
144
148
|
- A phrase that names a constant of `the constants are:` is its value, unless a
|
|
145
149
|
variable of the clause has that name.
|
|
146
150
|
- Other words are a name, written as they are, such as `Socrates` or
|
|
147
|
-
`cash at bank`.
|
|
151
|
+
`cash at bank`. After its first word, a name may hold words in brackets, as
|
|
152
|
+
in `Core Statistical Areas (Branch)`; brackets around anything else make a
|
|
153
|
+
formula.
|
|
148
154
|
|
|
149
155
|
In the place of a `that`, a sentence of a template is a term: the template's
|
|
150
156
|
relation with its arguments, as data.
|
|
@@ -468,6 +474,51 @@ establishes each answer with the assumptions it lists. That the program reads th
|
|
|
468
474
|
as intended is a matter of the compiler and its tests; a checked proof
|
|
469
475
|
establishes the answers under the compiled rules.
|
|
470
476
|
|
|
477
|
+
## Explanations
|
|
478
|
+
|
|
479
|
+
`--explain` writes each answer with the derivation its checked proof records,
|
|
480
|
+
in English: under each query's heading, the answer's sentence, then `because`
|
|
481
|
+
and the conditions it rests on, joined by `and`, each relation that a rule
|
|
482
|
+
concludes explained the same way on the lines indented below it. The last
|
|
483
|
+
line of an answer ends with a full stop.
|
|
484
|
+
|
|
485
|
+
```text
|
|
486
|
+
% query q with scenario s
|
|
487
|
+
Socrates owes 7 by 2024-03-16
|
|
488
|
+
because Socrates is mortal
|
|
489
|
+
because Socrates is human
|
|
490
|
+
and it is not the case that Socrates is exempt
|
|
491
|
+
and Socrates charged 6 for wine on 2024-02-15
|
|
492
|
+
and 2024-03-16 is 30 days after 2024-02-15
|
|
493
|
+
and 10 is the sum of each fee such that Socrates charged the fee for a thing on a day, namely 4 and 6
|
|
494
|
+
and 7 = 10 - 6 / 2.
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
The proof is made and checked first; a proof that does not check explains
|
|
498
|
+
nothing. An answer of a call table is explained by expanding the table's call
|
|
499
|
+
one level against the program, as the checker does, and taking the first
|
|
500
|
+
solution that gives the answer; a fact that forward rules derive, by the
|
|
501
|
+
firing its step records. The inner calls are answered by the proof's own
|
|
502
|
+
tables and listings, so nothing is searched for. Each condition is written
|
|
503
|
+
through its template, with the values of that solution:
|
|
504
|
+
|
|
505
|
+
- a variable left open is the phrase that named it, `a thing` and then
|
|
506
|
+
`the thing`, or the noun of its place when it had none, as in `an article`;
|
|
507
|
+
a variable named by an id is the id;
|
|
508
|
+
- a negation is `it is not the case that` and the sentence it denies; it rests
|
|
509
|
+
on the complete tables of the proof, and has no derivation of its own;
|
|
510
|
+
- an aggregate gives its value, its condition and the values it collected, after
|
|
511
|
+
`namely`;
|
|
512
|
+
- a comparison of dates is `is before`, `is after` or `is the same day as`, a
|
|
513
|
+
date count `is N days after`, and arithmetic the formula with its values;
|
|
514
|
+
- a sentence a relation states at another level of an unfolding, or the base
|
|
515
|
+
relation of an `only if` rule, gives its own conditions instead of
|
|
516
|
+
repeating the answer;
|
|
517
|
+
- a fact, and a sentence already explained earlier in the same answer, ends
|
|
518
|
+
its line.
|
|
519
|
+
|
|
520
|
+
Answers that rest on assumptions, and flip queries, are not explained.
|
|
521
|
+
|
|
471
522
|
## Not supported
|
|
472
523
|
|
|
473
524
|
These belong to Logical English 2, or to the Prolog that LE1 can embed, and
|
package/LS-SPEC.md
CHANGED
package/README.md
CHANGED
|
@@ -251,9 +251,11 @@ relation takes the scenario as its first argument, so one program and one
|
|
|
251
251
|
proof answer every query in every scenario. A scenario can state the answers
|
|
252
252
|
it expects, `q expects answers ["..."].`; the output then notes any answer set
|
|
253
253
|
that differs. Proofs and check reports are `.js` and check the compiled LS; that
|
|
254
|
-
the program reads the English as intended is a matter of the compiler.
|
|
255
|
-
|
|
256
|
-
|
|
254
|
+
the program reads the English as intended is a matter of the compiler.
|
|
255
|
+
`--explain` writes each answer with the derivation its checked proof records,
|
|
256
|
+
in English: the answer, then `because` and the conditions it rests on, each
|
|
257
|
+
explained in turn below it. Several `.le` files form one document, without
|
|
258
|
+
other input formats or RDF logic switches.
|
|
257
259
|
|
|
258
260
|
Of Logical English 2, eyemind also reads constants, functions, included files
|
|
259
261
|
and web addresses, Prolog resources and embedded Prolog goals with LE2's system
|
|
@@ -625,7 +627,7 @@ A clause is listed when no conclusion's proof uses it; a clause only a negation
|
|
|
625
627
|
|
|
626
628
|
The [examples](https://eyereasoner.github.io/eyemind/examples/) range from classics (Ackermann, the zebra puzzle) to planning, policy reasoning, mathematics, RDF 1.2 data and a hundred-thousand-level taxonomy. Each has its conclusions saved in `examples/output/`, all but the two streamed examples their proof and check report in `examples/proof/` and `examples/check/`, and each a card deck for a wide audience. Change a fact, run it, and compare with the saved output.
|
|
627
629
|
|
|
628
|
-
The [playground](https://eyereasoner.github.io/eyemind/playground/) runs eyemind in a worker in your browser, so **Stop** ends a long run. Choose an example, edit it, pick an output (conclusions, proof, checked answers, check of a saved proof, unused clauses, or independent RDF Messages) and press **Run** or Ctrl+Enter. A line an error names is marked red, the options panel shows the equivalent command, and **Share link** copies a link with the program and options.
|
|
630
|
+
The [playground](https://eyereasoner.github.io/eyemind/playground/) runs eyemind in a worker in your browser, so **Stop** ends a long run. Choose an example, edit it, pick an output (conclusions, proof, checked answers, check of a saved proof, unused clauses, Logical English answers explained, or independent RDF Messages) and press **Run** or Ctrl+Enter. A line an error names is marked red, the options panel shows the equivalent command, and **Share link** copies a link with the program and options.
|
|
629
631
|
|
|
630
632
|
## Command line
|
|
631
633
|
|
|
@@ -642,6 +644,7 @@ eyemind [OPTION ...] [FILE ...]
|
|
|
642
644
|
| `--json` | With a checking mode, print the report as JSON. |
|
|
643
645
|
| `--goal GOAL` | Ask `GOAL` after forward reasoning; may be repeated. |
|
|
644
646
|
| `--unused` | List the clauses that make no difference to the conclusions. |
|
|
647
|
+
| `--explain` | Explain each Logical English answer in English, from its checked proof. |
|
|
645
648
|
| `--stream-messages` | Read an RDF Message Log and run the rules independently per message; Node reads files, stdin and HTTP(S) incrementally. With an RDF logic profile, the other inputs are a background closed once and the rules are specialized per message shape. |
|
|
646
649
|
| `--shacl-in FILE` | With `--stream-messages` and an RDF logic profile, trust the SHACL shapes in `FILE` to describe the messages: the rules are planned and specialized for them before the first message, and a closed shape drops triples of other properties. |
|
|
647
650
|
| `--shacl-out FILE` | With an RDF logic profile, plan only the rules that conclude triples the SHACL shapes in `FILE` describe, or what those rules read, and write only those triples. |
|
|
@@ -765,7 +768,7 @@ console.log(result.bindings);
|
|
|
765
768
|
console.log(checkProof(socrates, result.proof, { goals }).valid);
|
|
766
769
|
```
|
|
767
770
|
|
|
768
|
-
`run()` returns `conclusions`, `answers`, `bindings`, `inferred`, `stdout`, `proof`, `proofReport`, `stats` and `haltCode`, and takes `goal`, `goals`, `proof`, `maxDepth`, `maxIterations` and `maxInferences`; exceeding a bound throws an `EyemindError`. `conclusions` holds the answer terms; `answers`, `bindings` and `stdout` are rendered when first read. Ground N3 conclusions are written directly from their terms. `loadSources([[text, filename], ...], io)` loads several sources as one program; pass `nodeIO` from `eyemind/node` as `io` to let `facts_from(path)` read files. `checkProof(program, document, { goals })` checks a proof. `checkReport`, `readTerm`, `readTerms` and `write` format reports and read and write the canonical text. `compileLe([[text, filename], ...], io)` returns the compiled LS `program` and standalone JavaScript `text`, reading included files with `io.readFile` when given; `writeLe(run(loaded.program), loaded)` writes the answers as LE sentences, given the value `loaded` that `compileLe` returned.
|
|
771
|
+
`run()` returns `conclusions`, `answers`, `bindings`, `inferred`, `stdout`, `proof`, `proofReport`, `stats` and `haltCode`, and takes `goal`, `goals`, `proof`, `maxDepth`, `maxIterations` and `maxInferences`; exceeding a bound throws an `EyemindError`. `conclusions` holds the answer terms; `answers`, `bindings` and `stdout` are rendered when first read. Ground N3 conclusions are written directly from their terms. `loadSources([[text, filename], ...], io)` loads several sources as one program; pass `nodeIO` from `eyemind/node` as `io` to let `facts_from(path)` read files. `checkProof(program, document, { goals })` checks a proof. `checkReport`, `readTerm`, `readTerms` and `write` format reports and read and write the canonical text. `compileLe([[text, filename], ...], io)` returns the compiled LS `program` and standalone JavaScript `text`, reading included files with `io.readFile` when given; `writeLe(run(loaded.program), loaded)` writes the answers as LE sentences, given the value `loaded` that `compileLe` returned, and `explainLe(loaded, run(loaded.program, { proof: true }).proof)` explains each answer in English, as `--explain` does, after checking the proof.
|
|
769
772
|
|
|
770
773
|
Further entry points serve applications built on the formats and profiles:
|
|
771
774
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eyemind",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.7",
|
|
4
4
|
"description": "A dependency-free Logic Script (LS) reasoner with forward and backward reasoning, checkable proofs, and RDF 1.2, Notation3 (N3), SPARQL 1.2 RL (SRL) and Logical English (LE) interoperability.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
package/src/cli.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// The eyemind command line.
|
|
2
2
|
import { run, unusedClauses } from './engine.js';
|
|
3
3
|
import { OUTPUT_FORMATS, RDF_PROFILES, defaultFormat, inputKind, loadInputs, loadMessageReasoner, rdfMessageFormat, scopeOf, writeConclusions } from './formats.js';
|
|
4
|
+
import { explainLe } from './le-explain.js';
|
|
4
5
|
import { isRdfMessageLog, replayMessages, RdfMessageReader } from './messages.js';
|
|
5
6
|
import { checkProof, checkReport, publicReport } from './proof.js';
|
|
6
7
|
import { writeRdf } from './rdf.js';
|
|
@@ -21,6 +22,8 @@ all compiled to LS.
|
|
|
21
22
|
--goal GOAL Ask a backward goal after forward reasoning; with
|
|
22
23
|
--check-proof, the goal the proof answers
|
|
23
24
|
--unused List the clauses that make no difference to the conclusions
|
|
25
|
+
--explain Explain each Logical English answer in English, from its
|
|
26
|
+
checked proof
|
|
24
27
|
--stats Print reasoning statistics to stderr
|
|
25
28
|
--max-depth N Bound backward recursion (default 1000000)
|
|
26
29
|
--max-iterations N Bound forward rounds per stratum (default 1000)
|
|
@@ -73,6 +76,7 @@ export function main(argv, io) {
|
|
|
73
76
|
let stats = false;
|
|
74
77
|
let asJson = false;
|
|
75
78
|
let unused = false;
|
|
79
|
+
let explain = false;
|
|
76
80
|
let proveAndCheck = false;
|
|
77
81
|
let format = null;
|
|
78
82
|
let all = false;
|
|
@@ -103,6 +107,8 @@ export function main(argv, io) {
|
|
|
103
107
|
asJson = true;
|
|
104
108
|
} else if (arg === '--unused') {
|
|
105
109
|
unused = true;
|
|
110
|
+
} else if (arg === '--explain') {
|
|
111
|
+
explain = true;
|
|
106
112
|
} else if (arg === '--all') {
|
|
107
113
|
all = true;
|
|
108
114
|
} else if (arg === '--program') {
|
|
@@ -154,6 +160,9 @@ export function main(argv, io) {
|
|
|
154
160
|
if (unused && (proofFile !== null || options.proof || proveAndCheck)) {
|
|
155
161
|
throw new UsageError('--unused cannot be combined with --proof, --check-proof or --prove-and-check');
|
|
156
162
|
}
|
|
163
|
+
if (explain && (proofFile !== null || options.proof || proveAndCheck || unused || goals.length || format !== null || streamMessages)) {
|
|
164
|
+
throw new UsageError('--explain writes Logical English explanations; it cannot be combined with --proof, --check-proof, --prove-and-check, --unused, --goal, --to or --stream-messages');
|
|
165
|
+
}
|
|
157
166
|
if (proofFile === '-' && (!files.length || files.includes('-'))) {
|
|
158
167
|
throw new UsageError('stdin holds the proof; name the program as files');
|
|
159
168
|
}
|
|
@@ -247,6 +256,14 @@ export function main(argv, io) {
|
|
|
247
256
|
stdout(unusedClauses(program, { goals, ...options }));
|
|
248
257
|
return 0;
|
|
249
258
|
}
|
|
259
|
+
if (explain) {
|
|
260
|
+
if (loaded.kind !== 'le') throw new UsageError('--explain explains the answers of Logical English (.le) inputs');
|
|
261
|
+
// The explanations are read from the proof, which is checked first.
|
|
262
|
+
const result = run(program, { ...options, proof: true });
|
|
263
|
+
stdout(explainLe(loaded, result.proof));
|
|
264
|
+
if (stats) stderr(`${JSON.stringify(result.stats)}\n`);
|
|
265
|
+
return result.haltCode || 0;
|
|
266
|
+
}
|
|
250
267
|
const result = run(program, { goals, ...options, proof: options.proof || proveAndCheck });
|
|
251
268
|
if (proveAndCheck) {
|
|
252
269
|
// Check the rendered certificate against the input, just as a saved
|
package/src/index.js
CHANGED
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
// Explanations of Logical English answers, in English, read from a checked
|
|
2
|
+
// proof. An answer is explained by the derivation its proof records: a call
|
|
3
|
+
// table's answer by expanding the table's call one level against the program,
|
|
4
|
+
// as the checker does, and taking the first solution that gives the answer;
|
|
5
|
+
// a derived fact by the firing its step records. Each condition of that
|
|
6
|
+
// solution is written through the document's templates, and each relation it
|
|
7
|
+
// used is explained in turn, below it. Nothing is searched for: inner calls
|
|
8
|
+
// are answered by the proof's own tables and listings.
|
|
9
|
+
import { Reliance, Settler, Unsettled } from './closure.js';
|
|
10
|
+
import { freshClause, variant } from './common.js';
|
|
11
|
+
import { callQuestions } from './fidelity.js';
|
|
12
|
+
import { leValue, sentence } from './le.js';
|
|
13
|
+
import { checkProof, readProof } from './proof.js';
|
|
14
|
+
import { Env, EyemindError, Struct, Var, copyResolved, deref, freshTerm, key, unify } from './terms.js';
|
|
15
|
+
import { write } from './writer.js';
|
|
16
|
+
|
|
17
|
+
const ANSWER = 'le:answer';
|
|
18
|
+
// Goals that test or thread, and say nothing a reader needs.
|
|
19
|
+
const SILENT = new Set(['le:scenario/1', 'is_var/1', 'is_nonvar/1', 'true/0']);
|
|
20
|
+
const AGGREGATE_WORDS = { 'le:sum': 'sum', 'le:count': 'count', 'le:average': 'average', 'le:min': 'min', 'le:max': 'max' };
|
|
21
|
+
const OPERATORS = { add: '+', sub: '-', mul: '*', div: '/', floordiv: '//', mod: 'mod' };
|
|
22
|
+
const COMPARISONS = { lt: '<', gt: '>', le: '=<', ge: '>=', eq: '=', ne: '!=' };
|
|
23
|
+
|
|
24
|
+
// The explanations of every answer of a Logical English run, as LE text: under
|
|
25
|
+
// each query's heading, each answer as a sentence followed by "because" and
|
|
26
|
+
// the conditions it rests on, each indented under the condition it explains.
|
|
27
|
+
// The proof is checked first; an explanation is only given for a checked one.
|
|
28
|
+
export function explainLe(loaded, document) {
|
|
29
|
+
const { compiled, program } = loaded;
|
|
30
|
+
if (compiled.abductive) throw new EyemindError('--explain does not explain answers that rest on assumptions; ask for the answers or a proof instead');
|
|
31
|
+
if (compiled.queries.some((query) => query.flip)) throw new EyemindError('--explain does not explain flip queries; ask for the answers or a proof instead');
|
|
32
|
+
const report = checkProof(program, document);
|
|
33
|
+
if (!report.valid) throw new EyemindError(`the proof does not check, so it explains nothing: ${report.failures[0]?.detail ?? 'it is not valid'}`);
|
|
34
|
+
const { claims, certificate, stepOf } = readProof(document);
|
|
35
|
+
const settler = new Settler(program, {
|
|
36
|
+
answers: (call) => certificate.calls.get(variant(call, true)) ?? null,
|
|
37
|
+
facts: (goalKey) => certificate.listings.get(goalKey) ?? null,
|
|
38
|
+
});
|
|
39
|
+
for (const question of callQuestions(program, null, claims)) settler.answers(question);
|
|
40
|
+
// Each answer of a checked table, with the table that lists it.
|
|
41
|
+
const tableOf = new Map();
|
|
42
|
+
for (const entry of settler.tables.values()) {
|
|
43
|
+
for (const answer of entry.answers || []) {
|
|
44
|
+
const id = variant(answer);
|
|
45
|
+
if (!tableOf.has(id)) tableOf.set(id, entry);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const explainer = new Explainer(program, compiled, settler, tableOf, stepOf);
|
|
49
|
+
const { templates, scenarios, threaded, queries } = compiled;
|
|
50
|
+
const found = new Map();
|
|
51
|
+
for (const claim of claims) {
|
|
52
|
+
if (!(claim instanceof Struct) || claim.name !== ANSWER) continue;
|
|
53
|
+
const [name, ...rest] = claim.args;
|
|
54
|
+
const scenario = threaded ? rest.shift() : null;
|
|
55
|
+
const query = queries.find((q) => q.name === name);
|
|
56
|
+
if (!query) continue;
|
|
57
|
+
const id = `${scenario}\u0000${name}`;
|
|
58
|
+
if (!found.has(id)) found.set(id, new Map());
|
|
59
|
+
const text = sentence(query, rest, templates);
|
|
60
|
+
if (!found.get(id).has(text)) found.get(id).set(text, explainer.answer(claim, text));
|
|
61
|
+
}
|
|
62
|
+
const blocks = [];
|
|
63
|
+
for (const scenario of threaded ? scenarios : [null]) {
|
|
64
|
+
for (const query of queries) {
|
|
65
|
+
const answers = found.get(`${scenario}\u0000${query.name}`) ?? new Map();
|
|
66
|
+
const lines = [scenario === null ? `% query ${query.name}` : `% query ${query.name} with scenario ${scenario}`];
|
|
67
|
+
[...answers.values()].forEach((explained, i) => lines.push(...(i ? ['', ...explained] : explained)));
|
|
68
|
+
if (!answers.size) lines.push('% no answers');
|
|
69
|
+
blocks.push(lines.join('\n'));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return blocks.length ? blocks.join('\n\n') + '\n' : '% no queries\n';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
class Explainer {
|
|
76
|
+
constructor(program, compiled, settler, tableOf, stepOf) {
|
|
77
|
+
this.program = program;
|
|
78
|
+
this.compiled = compiled;
|
|
79
|
+
this.settler = settler;
|
|
80
|
+
this.tableOf = tableOf;
|
|
81
|
+
this.stepOf = stepOf;
|
|
82
|
+
this.serial = 0;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// An answer's lines: its sentence, then what it rests on. A query of one
|
|
86
|
+
// condition is that condition, explained directly.
|
|
87
|
+
answer(claim, text) {
|
|
88
|
+
this.explained = new Set();
|
|
89
|
+
const conditions = this.derivation(claim) ?? [];
|
|
90
|
+
const relations = conditions.filter((c) => c.goal);
|
|
91
|
+
const only = conditions.length === 1 && relations.length === 1 ? relations[0] : null;
|
|
92
|
+
const lines = [text];
|
|
93
|
+
const below = only ? this.because(only.goal) : conditions;
|
|
94
|
+
this.lines(below, 1, lines);
|
|
95
|
+
lines[lines.length - 1] += '.';
|
|
96
|
+
return lines;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// The conditions a goal rests on, each { text, goal } where goal is a
|
|
100
|
+
// relation to explain in turn, or null for a fact or a goal without a
|
|
101
|
+
// derivation in the proof.
|
|
102
|
+
because(goal) {
|
|
103
|
+
const id = variant(goal);
|
|
104
|
+
if (this.explained.has(id)) return null;
|
|
105
|
+
this.explained.add(id);
|
|
106
|
+
return this.unfolded(goal, this.derivation(goal));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// A relation unfolded into levels (NAME#k), or concluded through the base
|
|
110
|
+
// relation of an "only if" rule (NAME:if), says the same as the goal it
|
|
111
|
+
// explains: its own conditions stand in its place.
|
|
112
|
+
unfolded(goal, conditions) {
|
|
113
|
+
if (conditions === null) return null;
|
|
114
|
+
const same = (other) => other && level(other) === level(goal) && other.args.every((arg, i) => variant(arg) === variant(goal.args[i]));
|
|
115
|
+
const flat = conditions.flatMap((condition) => same(condition.goal) ? this.unfolded(condition.goal, this.derivation(condition.goal)) ?? [] : [condition]);
|
|
116
|
+
return flat.length ? flat : null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
lines(conditions, depth, out) {
|
|
120
|
+
(conditions ?? []).forEach((condition, i) => {
|
|
121
|
+
out.push(`${' '.repeat(depth)}${i ? 'and' : 'because'} ${condition.text}`);
|
|
122
|
+
if (condition.goal) this.lines(this.because(condition.goal), depth + 1, out);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
derivation(goal) {
|
|
127
|
+
const goalKey = key(goal);
|
|
128
|
+
const step = this.settler.derivedKeys.has(goalKey) ? this.stepOf(goal) : undefined;
|
|
129
|
+
if (step) return this.fired(goal, step);
|
|
130
|
+
const entry = this.tableOf.get(variant(goal));
|
|
131
|
+
if (!entry) return null;
|
|
132
|
+
for (const clause of this.program.groups.get(goalKey) || []) {
|
|
133
|
+
if (clause.forward) continue;
|
|
134
|
+
const env = new Env();
|
|
135
|
+
const [head, body, names] = freshClause(clause, `explain${++this.serial}`);
|
|
136
|
+
const call = freshTerm(entry.call, `call${this.serial}`);
|
|
137
|
+
if (!unify(call, head, env)) continue;
|
|
138
|
+
// A fact of the program, or of a scenario, rests on nothing.
|
|
139
|
+
if (!body.length) {
|
|
140
|
+
if (variant(copyResolved(call, env)) === variant(goal)) return null;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
const found = this.first(body, env, names, () => variant(copyResolved(call, env)) === variant(goal));
|
|
144
|
+
if (found) return found;
|
|
145
|
+
}
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// A derived fact rests on the firing its step records: the rule, with the
|
|
150
|
+
// step's bindings, solved again over the proof's tables and listings.
|
|
151
|
+
fired(goal, step) {
|
|
152
|
+
const id = step.by instanceof Struct && step.by.name === 'clause' ? Number(step.by.args[0]) : null;
|
|
153
|
+
const clause = id === null ? null : this.program.byId.get(id) ?? this.program.byId.get(BigInt(id));
|
|
154
|
+
if (!clause) return null;
|
|
155
|
+
const env = new Env();
|
|
156
|
+
const [head, body, names] = freshClause(clause, `explain${++this.serial}`);
|
|
157
|
+
if (!unify(head, goal, env)) return null;
|
|
158
|
+
for (const pair of step.bindings) {
|
|
159
|
+
const [name, value] = pair instanceof Struct ? pair.args : pair;
|
|
160
|
+
if (names.has(name) && !unify(names.get(name), freshTerm(value, `bound${this.serial}`), env)) return null;
|
|
161
|
+
}
|
|
162
|
+
return this.first(body, env, names, () => true);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// The conditions of the first solution of a body that passes accept, or
|
|
166
|
+
// null; each is written while its bindings hold.
|
|
167
|
+
first(body, env, names, accept) {
|
|
168
|
+
const path = [];
|
|
169
|
+
try {
|
|
170
|
+
for (const _ of this.all(body, 0, env, path)) { // eslint-disable-line no-unused-vars
|
|
171
|
+
if (accept()) return this.describe(path, env, names);
|
|
172
|
+
}
|
|
173
|
+
} catch (error) {
|
|
174
|
+
// A goal the proof's tables cannot settle leaves the goal unexplained.
|
|
175
|
+
if (!(error instanceof EyemindError) && !(error instanceof Unsettled)) throw error;
|
|
176
|
+
}
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
* all(goals, index, env, path) {
|
|
181
|
+
if (index === goals.length) {
|
|
182
|
+
yield;
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
for (const _ of this.walk(goals[index], env, path)) yield* this.all(goals, index + 1, env, path); // eslint-disable-line no-unused-vars
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// A goal solved as the checker solves it, with each solved condition kept
|
|
189
|
+
// on the path while its solution holds; "and" and "or" are walked here, so
|
|
190
|
+
// the path shows which alternative held.
|
|
191
|
+
* walk(goal, env, path) {
|
|
192
|
+
goal = deref(goal, env);
|
|
193
|
+
const goalKey = goal instanceof Struct ? key(goal) : null;
|
|
194
|
+
if (goalKey === 'and/2') {
|
|
195
|
+
for (const _ of this.walk(goal.args[0], env, path)) yield* this.walk(goal.args[1], env, path); // eslint-disable-line no-unused-vars
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (goalKey === 'or/2') {
|
|
199
|
+
yield* this.walk(goal.args[0], env, path);
|
|
200
|
+
yield* this.walk(goal.args[1], env, path);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
// A relation forward rules derive is read from the proof's listing, as a
|
|
204
|
+
// replay reads it; other calls come from the proof's tables.
|
|
205
|
+
const derived = goalKey !== null && this.settler.derivedKeys.has(goalKey);
|
|
206
|
+
const solutions = derived ? this.settler.expand(goal, env, new Reliance()) : this.settler.solve(goal, env, new Reliance());
|
|
207
|
+
for (const _ of solutions) { // eslint-disable-line no-unused-vars
|
|
208
|
+
path.push(goal);
|
|
209
|
+
try {
|
|
210
|
+
yield;
|
|
211
|
+
} finally {
|
|
212
|
+
path.pop();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// The solved conditions in English. A helper that computes a value for a
|
|
218
|
+
// later condition, such as a collection for its sum, is written there.
|
|
219
|
+
describe(path, env, names) {
|
|
220
|
+
const writer = new Writer(this.compiled, env, names);
|
|
221
|
+
const conditions = [];
|
|
222
|
+
for (const goal of path) {
|
|
223
|
+
const goalKey = key(goal);
|
|
224
|
+
if (SILENT.has(goalKey)) continue;
|
|
225
|
+
const text = writer.condition(goal);
|
|
226
|
+
if (text === null) continue;
|
|
227
|
+
const relation = !goal.name.startsWith('le:') && !PRIMITIVE_WORDS.has(goal.name) &&
|
|
228
|
+
(this.program.groups.has(goalKey) || this.settler.derivedKeys.has(goalKey));
|
|
229
|
+
conditions.push({ text, goal: relation ? copyResolved(goal, env) : null });
|
|
230
|
+
}
|
|
231
|
+
return conditions;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// The relation of a goal, without the level an unfolding gives it.
|
|
236
|
+
const level = (goal) => `${goal.name.replace(/#\d+$/, '').replace(/:if$/, '')}/${goal.args.length}`;
|
|
237
|
+
const ID = /^[A-Z][A-Z0-9]{0,5}$/;
|
|
238
|
+
const PRIMITIVE_WORDS = new Set(['not', 'findall', 'findall_sorted', 'is', 'compare', 'unify', 'not_unify', 'not_identical', ...Object.keys(COMPARISONS)]);
|
|
239
|
+
|
|
240
|
+
// Terms and conditions in English, with the bindings of one solution. A
|
|
241
|
+
// variable that is still open is written as the phrase it was named by: "a
|
|
242
|
+
// charge" the first time, "the charge" after; an anonymous one as the noun of
|
|
243
|
+
// its template's place.
|
|
244
|
+
class Writer {
|
|
245
|
+
constructor(compiled, env, names) {
|
|
246
|
+
this.compiled = compiled;
|
|
247
|
+
this.env = env;
|
|
248
|
+
this.names = new Map([...names].map(([name, variable]) => [variable, name]));
|
|
249
|
+
this.pending = new Map();
|
|
250
|
+
this.seen = new Set();
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
phrase(variable, noun = null) {
|
|
254
|
+
const name = this.names.get(variable);
|
|
255
|
+
// A variable named by an id, such as T, is written as its id.
|
|
256
|
+
if (name && ID.test(name)) return name;
|
|
257
|
+
const words = name && !name.startsWith('_') && !name.endsWith('_')
|
|
258
|
+
? name.replace(/([a-z0-9])([A-Z])/g, '$1 $2').toLowerCase() : noun ?? 'something';
|
|
259
|
+
if (this.seen.has(variable)) return `the ${words}`;
|
|
260
|
+
this.seen.add(variable);
|
|
261
|
+
return /^[aeiou]/.test(words) ? `an ${words}` : `a ${words}`;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
value(term, noun = null) {
|
|
265
|
+
// A value a helper computed is written as the formula it came from.
|
|
266
|
+
if (term instanceof Var && typeof this.pending.get(term) === 'string') return this.pending.get(term);
|
|
267
|
+
term = deref(term, this.env);
|
|
268
|
+
if (term instanceof Var) return this.pending.get(term) ?? this.phrase(term, noun);
|
|
269
|
+
if (term instanceof Struct && OPERATORS[term.name] && term.args.length === 2) {
|
|
270
|
+
const side = (arg) => {
|
|
271
|
+
const text = this.value(arg);
|
|
272
|
+
const inner = deref(arg, this.env);
|
|
273
|
+
return inner instanceof Struct && OPERATORS[inner.name] ? `(${text})` : text;
|
|
274
|
+
};
|
|
275
|
+
return `${side(term.args[0])} ${OPERATORS[term.name]} ${side(term.args[1])}`;
|
|
276
|
+
}
|
|
277
|
+
if (term instanceof Struct && (term.name === 'min' || term.name === 'max') && term.args.length === 2) {
|
|
278
|
+
return `the ${term.name === 'min' ? 'minimum' : 'maximum'} of ${this.value(term.args[0])} and ${this.value(term.args[1])}`;
|
|
279
|
+
}
|
|
280
|
+
if (term instanceof Struct && this.template(term)) return this.relation(term);
|
|
281
|
+
return leValue(copyResolved(term, this.env), this.compiled.templates);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// The template of a relation, and the arguments of its places. A relation
|
|
285
|
+
// compiled for a level (NAME#k), an "only if" rule (NAME:if), a source scope
|
|
286
|
+
// (NAME:sc) or assumptions (NAME:ab) reads as NAME; the last two take one
|
|
287
|
+
// more argument, which no place shows.
|
|
288
|
+
template(term) {
|
|
289
|
+
const name = term.name.replace(/#\d+$/, '').replace(/:(if|sc|ab|w)$/, '');
|
|
290
|
+
let args = this.compiled.threaded ? term.args.slice(1) : term.args;
|
|
291
|
+
if (/:(sc|ab|w)$/.test(term.name)) args = args.slice(0, -1);
|
|
292
|
+
const template = this.compiled.templates.find((t) => t.name === name && t.arity === args.length);
|
|
293
|
+
return template ? { template, args } : null;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
relation(term) {
|
|
297
|
+
const { template, args } = this.template(term) ?? { template: null, args: this.compiled.threaded ? term.args.slice(1) : term.args };
|
|
298
|
+
if (!template) {
|
|
299
|
+
if (term.name === 'is_a' && args.length === 2) return `${this.value(args[0])} is a ${this.value(args[1])}`;
|
|
300
|
+
if (term.name === 'is_admissible_under' && args.length === 2) return `${this.value(args[0])} is admissible under ${this.value(args[1])}`;
|
|
301
|
+
return write(copyResolved(term, this.env));
|
|
302
|
+
}
|
|
303
|
+
let n = 0;
|
|
304
|
+
return template.parts.map((p) => p.place ? this.value(args[n++], p.name) : p.text).join(' ');
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// A condition as a sentence, or null when it only computes a value that a
|
|
308
|
+
// later condition writes.
|
|
309
|
+
condition(goal) {
|
|
310
|
+
const { name, args } = goal;
|
|
311
|
+
if (name === 'not' && args.length === 1) {
|
|
312
|
+
const inner = this.conjunction(args[0]);
|
|
313
|
+
return inner === null ? null : `it is not the case that ${inner}`;
|
|
314
|
+
}
|
|
315
|
+
if (name === 'le:member' && args.length === 2) return `${this.value(args[0])} is in ${this.value(args[1])}`;
|
|
316
|
+
if (name === 'le:reverse' && args.length === 2) return `${this.value(args[1])} is the reverse of ${this.value(args[0])}`;
|
|
317
|
+
if (name === 'le:append' && args.length === 3) return `appending ${this.value(args[0])} then ${this.value(args[1])} gives ${this.value(args[2])}`;
|
|
318
|
+
if ((name === 'findall' || name === 'findall_sorted') && args.length === 3) {
|
|
319
|
+
const list = deref(args[2], this.env);
|
|
320
|
+
const item = deref(args[0], this.env);
|
|
321
|
+
const named = item instanceof Var ? this.names.get(item) : undefined;
|
|
322
|
+
const each = named === undefined ? null : ID.test(named) ? named : named.replace(/([a-z0-9])([A-Z])/g, '$1 $2').toLowerCase();
|
|
323
|
+
if (item instanceof Var) this.seen.add(item);
|
|
324
|
+
const such = `each ${each ?? this.value(item)} such that ${this.conjunction(args[1])}`;
|
|
325
|
+
const items = this.value(list);
|
|
326
|
+
this.pending.set(args[2] instanceof Var ? args[2] : list, { such, items });
|
|
327
|
+
return null;
|
|
328
|
+
}
|
|
329
|
+
if (AGGREGATE_WORDS[name] && args.length === 2) {
|
|
330
|
+
const collected = this.collected(args[0]);
|
|
331
|
+
const total = this.value(args[1]);
|
|
332
|
+
if (collected) return `${total} is the ${AGGREGATE_WORDS[name]} of ${collected.such}, namely ${collected.items}`;
|
|
333
|
+
return name === 'le:count' ? `the length of ${this.value(args[0])} is ${total}` : `${total} is the ${AGGREGATE_WORDS[name]} of ${this.value(args[0])}`;
|
|
334
|
+
}
|
|
335
|
+
if (name === 'le:divide' && args.length === 3) {
|
|
336
|
+
// The quotient is written where the formula that divides uses it.
|
|
337
|
+
const formula = `${this.value(args[0])} / ${this.value(args[1])}`;
|
|
338
|
+
if (args[2] instanceof Var) {
|
|
339
|
+
this.pending.set(args[2], formula);
|
|
340
|
+
return null;
|
|
341
|
+
}
|
|
342
|
+
return `${this.value(args[2])} = ${formula}`;
|
|
343
|
+
}
|
|
344
|
+
if (name === 'le:days_after' && args.length === 3) return `${this.value(args[0])} is ${this.value(args[1])} days after ${this.value(args[2])}`;
|
|
345
|
+
if (name === 'is' && args.length === 2) {
|
|
346
|
+
const right = this.value(args[1]);
|
|
347
|
+
const left = leValue(copyResolved(args[0], this.env), this.compiled.templates);
|
|
348
|
+
return left === right ? null : `${left} = ${right}`;
|
|
349
|
+
}
|
|
350
|
+
if (name === 'compare' && args.length === 3) {
|
|
351
|
+
const op = deref(args[0], this.env);
|
|
352
|
+
const [a, b] = [this.value(args[1]), this.value(args[2])];
|
|
353
|
+
const date = (t) => { t = deref(t, this.env); return t instanceof Struct && t.name === 'date'; };
|
|
354
|
+
const words = date(args[1]) || date(args[2]) ? { '<': 'is before', '>': 'is after', '=': 'is the same day as' } : { '<': '<', '>': '>', '=': '=' };
|
|
355
|
+
return `${a} ${words[op] ?? op} ${b}`;
|
|
356
|
+
}
|
|
357
|
+
if (COMPARISONS[name] && args.length === 2) return `${this.value(args[0])} ${COMPARISONS[name]} ${this.value(args[1])}`;
|
|
358
|
+
if (name === 'unify' && args.length === 2) {
|
|
359
|
+
// Equating a term with itself, as a rule does to build or take apart a
|
|
360
|
+
// list, says nothing once both sides are known.
|
|
361
|
+
const [left, right] = [this.value(args[0]), this.value(args[1])];
|
|
362
|
+
return left === right ? null : `${left} = ${right}`;
|
|
363
|
+
}
|
|
364
|
+
if ((name === 'not_unify' || name === 'not_identical') && args.length === 2) return `${this.value(args[0])} is different from ${this.value(args[1])}`;
|
|
365
|
+
if (name.startsWith('le:') && !this.template(goal)) return null;
|
|
366
|
+
return this.relation(goal);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// The items a collection gathered, for the aggregate that takes them.
|
|
370
|
+
collected(list) {
|
|
371
|
+
const pending = this.pending.get(list instanceof Var ? list : deref(list, this.env));
|
|
372
|
+
if (!pending || typeof pending === 'string') return null;
|
|
373
|
+
const items = copyResolved(list, this.env);
|
|
374
|
+
const values = [];
|
|
375
|
+
let rest = items;
|
|
376
|
+
while (rest instanceof Struct && rest.name === '.' && rest.args.length === 2) {
|
|
377
|
+
values.push(leValue(rest.args[0], this.compiled.templates));
|
|
378
|
+
rest = rest.args[1];
|
|
379
|
+
}
|
|
380
|
+
const namely = !values.length ? 'none' : values.length === 1 ? values[0] : `${values.slice(0, -1).join(', ')} and ${values.at(-1)}`;
|
|
381
|
+
return { such: pending.such, items: namely };
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// A goal under a negation or a collection, its parts joined by "and" and "or".
|
|
385
|
+
conjunction(goal) {
|
|
386
|
+
goal = deref(goal, this.env);
|
|
387
|
+
if (goal instanceof Struct && (goal.name === 'and' || goal.name === 'or') && goal.args.length === 2) {
|
|
388
|
+
const parts = goal.args.map((arg) => this.conjunction(arg)).filter((part) => part !== null);
|
|
389
|
+
return parts.join(` ${goal.name} `) || null;
|
|
390
|
+
}
|
|
391
|
+
if (goal instanceof Struct && SILENT.has(key(goal))) return null;
|
|
392
|
+
if (Array.isArray(goal)) return goal.map((g) => this.conjunction(g)).filter((p) => p !== null).join(' and ');
|
|
393
|
+
return goal instanceof Struct ? this.condition(goal) : this.value(goal);
|
|
394
|
+
}
|
|
395
|
+
}
|
package/src/le.js
CHANGED
|
@@ -112,6 +112,12 @@ const DESIGNATOR = /^\d+(?:\.\d+)*\.(?=[ \t]*\p{L})/u;
|
|
|
112
112
|
// the conditions below them.
|
|
113
113
|
const GROUPS = [[['at', 'least', 'one', 'of'], 'or'], [['any', 'of'], 'or'], [['either'], 'or'], [['all', 'of'], 'and']];
|
|
114
114
|
const DATE = /^(\d{4})-(\d{2})-(\d{2})(?![\p{L}\p{N}_])/u;
|
|
115
|
+
// A date followed at once by a time is a name, as written: only dates have a
|
|
116
|
+
// calendar meaning.
|
|
117
|
+
const DATE_TIME = /^\d{4}-\d{2}-\d{2}[T:]\d{2}(?::\d{2}){1,2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?(?![\p{L}\p{N}_])/u;
|
|
118
|
+
// A web address is a name up to white space, without a final full stop or
|
|
119
|
+
// comma, so that it can end a sentence.
|
|
120
|
+
const ADDRESS = /^[a-z][a-z0-9+.-]*:\/\/\S*[^\s.,;:]/i;
|
|
115
121
|
// Numbers may group thousands with commas, as in 1,000,000.
|
|
116
122
|
const NUMBER = /^(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?(?:[eE][+-]?\d+)?(?![\p{L}\p{N}_])/u;
|
|
117
123
|
const SYMBOLS = ['\\==', '>=', '=<', '<=', '==', '!=', '\\=', '//'];
|
|
@@ -145,6 +151,10 @@ function tokenize(text, file) {
|
|
|
145
151
|
} else if (bol && DESIGNATOR.test(tail)) {
|
|
146
152
|
// A numbered condition starts its line with a designator such as 4.2.1.
|
|
147
153
|
token = { text: DESIGNATOR.exec(tail)[0], kind: 'designator' };
|
|
154
|
+
} else if (ADDRESS.test(tail)) {
|
|
155
|
+
token = { text: ADDRESS.exec(tail)[0], kind: 'word' };
|
|
156
|
+
} else if (DATE_TIME.test(tail)) {
|
|
157
|
+
token = { text: DATE_TIME.exec(tail)[0], kind: 'word' };
|
|
148
158
|
} else if (DATE.test(tail)) {
|
|
149
159
|
const [raw, y, m, d] = DATE.exec(tail);
|
|
150
160
|
token = { text: raw, kind: 'date', value: [BigInt(y), BigInt(m), BigInt(d)] };
|
|
@@ -168,6 +178,17 @@ function tokenize(text, file) {
|
|
|
168
178
|
return tokens;
|
|
169
179
|
}
|
|
170
180
|
|
|
181
|
+
// A name may hold words in brackets after its first word, as in "Core Based
|
|
182
|
+
// Statistical Areas (Branch)"; brackets around anything else are a formula.
|
|
183
|
+
function bracketedName(tokens) {
|
|
184
|
+
let open = false;
|
|
185
|
+
return tokens.every((t, i) => {
|
|
186
|
+
if (t.text === '(' && t.kind === 'punct') return i > 0 && !open && tokens[i + 1]?.kind === 'word' && (open = true);
|
|
187
|
+
if (t.text === ')' && t.kind === 'punct') return open && tokens[i - 1]?.text !== '(' && !(open = false);
|
|
188
|
+
return t.kind === 'word' || t.kind === 'number' || t.text === '&';
|
|
189
|
+
}) && !open;
|
|
190
|
+
}
|
|
191
|
+
|
|
171
192
|
const lower = (token) => token.kind === 'word' ? token.text.toLowerCase() : token.text;
|
|
172
193
|
const isWord = (token, ...words) => token.kind === 'word' && words.includes(token.text.toLowerCase());
|
|
173
194
|
// Words joined as written: no space before a comma, a closing bracket or 's.
|
|
@@ -425,7 +446,7 @@ class Compiler {
|
|
|
425
446
|
const words = tokens.slice(1, -1), named = INTRODUCING.has(lower(words[0])) || isWord(words[0], 'the') ? words.slice(1) : words;
|
|
426
447
|
return this.mark(scope, scope.find(named) ?? scope.introduce(named.length ? named : words), this.offset(tokens), tokens.length);
|
|
427
448
|
}
|
|
428
|
-
if (!tokens.every((t) => t.kind === 'word' || t.kind === 'number' || t.text === '&')) {
|
|
449
|
+
if (!tokens.every((t) => t.kind === 'word' || t.kind === 'number' || t.text === '&') && !bracketedName(tokens)) {
|
|
429
450
|
return tokens.some((t) => t.kind === 'value') ? this.call(tokens, scope) : null;
|
|
430
451
|
}
|
|
431
452
|
// A lone which, what, who, when or where asks for a value.
|
|
@@ -444,7 +465,7 @@ class Compiler {
|
|
|
444
465
|
const found = scope.find(rest);
|
|
445
466
|
if (found) return this.mark(scope, found, at, tokens.length);
|
|
446
467
|
if (constant !== undefined) return constant;
|
|
447
|
-
return this.call(tokens, scope) ?? ['the', ...rest.map((t) => t.text)]
|
|
468
|
+
return this.call(tokens, scope) ?? joined(['the', ...rest.map((t) => t.text)]);
|
|
448
469
|
}
|
|
449
470
|
const found = scope.find(tokens);
|
|
450
471
|
if (found) return this.mark(scope, found, at, tokens.length);
|
|
@@ -452,7 +473,7 @@ class Compiler {
|
|
|
452
473
|
const called = this.call(tokens, scope);
|
|
453
474
|
if (called !== null) return called;
|
|
454
475
|
if (tokens.length === 1 && BARE_ID.test(first.text) && !this.fact) return this.mark(scope, scope.introduce(tokens), at, 1);
|
|
455
|
-
return tokens.map((t) => t.text)
|
|
476
|
+
return joined(tokens.map((t) => t.text));
|
|
456
477
|
}
|
|
457
478
|
|
|
458
479
|
// Where a slice starts in the sentence a query renders its answers from.
|
|
@@ -2140,7 +2161,7 @@ function javascript(term) {
|
|
|
2140
2161
|
|
|
2141
2162
|
// A value as LE writes it: names as they are, dates as YYYY-MM-DD, lists in
|
|
2142
2163
|
// brackets and a sentence through its template.
|
|
2143
|
-
function leValue(term, templates) {
|
|
2164
|
+
export function leValue(term, templates) {
|
|
2144
2165
|
if (term instanceof Var) return '_';
|
|
2145
2166
|
if (typeof term === 'string') return term === EMPTY ? '[]' : term;
|
|
2146
2167
|
if (typeof term === 'bigint' || typeof term === 'number') return write(term);
|
|
@@ -2166,7 +2187,7 @@ function leValue(term, templates) {
|
|
|
2166
2187
|
|
|
2167
2188
|
// The answers of a query as sentences: the query's words, with each variable
|
|
2168
2189
|
// it found a value for written as that value.
|
|
2169
|
-
function sentence(query, values, templates) {
|
|
2190
|
+
export function sentence(query, values, templates) {
|
|
2170
2191
|
const words = [];
|
|
2171
2192
|
for (let i = 0; i < query.tokens.length; i++) {
|
|
2172
2193
|
const mark = query.marks.find((m) => m.at === i);
|
package/src/proof.js
CHANGED
|
@@ -194,6 +194,16 @@ export function checkProof(program, document, { goals = null } = {}) {
|
|
|
194
194
|
return check(program, claims, steps, certificate, goals, fail, spellings, text);
|
|
195
195
|
}
|
|
196
196
|
|
|
197
|
+
// The parts of a proof document, for reading a checked proof: its claims,
|
|
198
|
+
// its steps by goal, the step of a goal, and its certificate.
|
|
199
|
+
export function readProof(document) {
|
|
200
|
+
const fail = new Failures();
|
|
201
|
+
const spellings = new Spellings();
|
|
202
|
+
const [claims, steps, certificate] = readDocument(document, fail, spellings);
|
|
203
|
+
if (fail.items.length) throw new EyemindError(fail.items[0][0]);
|
|
204
|
+
return { claims, certificate, stepOf: (goal) => steps.get(stepIdOf(steps, spellings, goal)) };
|
|
205
|
+
}
|
|
206
|
+
|
|
197
207
|
// Check a proof as renderProof(..., true) gave it: the report checkProof()
|
|
198
208
|
// gives for its document, without reading the document back. A reasoner
|
|
199
209
|
// checks its own proofs this way; their text is covered by the tests that
|
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.9.
|
|
2
|
+
export const VERSION = '0.9.7';
|