eyemind 0.7.0 → 0.7.1
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/{MATHEMATICAL-ENGLISH.md → LOGICAL-ENGLISH.md} +91 -18
- package/README.md +26 -22
- package/SPEC.md +1 -1
- package/package.json +2 -2
- package/src/cli.js +3 -3
- package/src/formats.js +14 -14
- package/src/index.js +1 -1
- package/src/le-odrl.js +145 -0
- package/src/{me.js → le.js} +48 -19
- package/src/version.js +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Logical English
|
|
2
2
|
|
|
3
|
-
eyemind reads `.
|
|
3
|
+
eyemind reads `.le` files as the language defined here and compiles
|
|
4
4
|
them to ordinary Logic Script (LS). This is an input extension; the meaning of
|
|
5
5
|
the compiled program is defined by the [LS specification](https://eyereasoner.github.io/eyemind/SPEC).
|
|
6
6
|
It supports statements and computations in a fixed grammar. It does not parse
|
|
@@ -51,13 +51,14 @@ nesting is limited to 256 levels.
|
|
|
51
51
|
The following forms have the indicated LS meanings. `T`, `U` and the arguments
|
|
52
52
|
stand for terms; `class` and `relation` stand for names as defined above.
|
|
53
53
|
|
|
54
|
-
|
|
|
54
|
+
| LE form | LS term |
|
|
55
55
|
| --- | --- |
|
|
56
56
|
| `T is a class` | `class(T)` |
|
|
57
57
|
| `T is an class` | `class(T)` |
|
|
58
58
|
| `T is class` | `class(T)` |
|
|
59
59
|
| `T is a relation of U` | `relation(T, U)` |
|
|
60
60
|
| `T is an relation of U` | `relation(T, U)` |
|
|
61
|
+
| `T has relation U` | `relation(T, U)` |
|
|
61
62
|
| `relation(T, U, ...)` | The corresponding compound goal. |
|
|
62
63
|
| `relation` | An atom used as a goal, with arity zero. |
|
|
63
64
|
|
|
@@ -95,7 +96,7 @@ actual values must also satisfy LS's runtime requirements.
|
|
|
95
96
|
|
|
96
97
|
These additional forms are available in premises and questions:
|
|
97
98
|
|
|
98
|
-
|
|
|
99
|
+
| LE form | LS goal |
|
|
99
100
|
| --- | --- |
|
|
100
101
|
| `Y is the value of E` | `is(Y, E)` |
|
|
101
102
|
| `X is greater than E` | `gt(X, E)` |
|
|
@@ -174,9 +175,81 @@ derived conclusions, following LS's default; given facts alone are not printed.
|
|
|
174
175
|
run's `'false'` conclusion. Contradictions halt reasoning with exit code 65;
|
|
175
176
|
they do not produce the continuing RDF conflict reports of `--owl-rl`.
|
|
176
177
|
|
|
178
|
+
## ODRL snapshots
|
|
179
|
+
|
|
180
|
+
An LE file can explicitly select the existing ODRL snapshot evaluator:
|
|
181
|
+
|
|
182
|
+
```text
|
|
183
|
+
Use ODRL snapshot logic with namespace "https://example.org/handicap-card#".
|
|
184
|
+
|
|
185
|
+
evaluation is an evaluation.
|
|
186
|
+
evaluation has policy eligibility.
|
|
187
|
+
evaluation has request issueAlice.
|
|
188
|
+
evaluation has conflictStrategy prohibit.
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
This directive must be the first sentence of one file. The remainder contains
|
|
192
|
+
ground facts using the snapshot vocabulary, without rules or questions. The
|
|
193
|
+
compiler maps those facts to RDF and includes the validation, evaluation and
|
|
194
|
+
reporting LS of `src/odrl.js`. The same
|
|
195
|
+
[snapshot profile](https://eyereasoner.github.io/eyemind/#odrl-snapshots) applies
|
|
196
|
+
to Turtle and LE, including missing observations, unsupported policy features
|
|
197
|
+
and conflict strategies. Vocabulary by itself does not enable evaluation.
|
|
198
|
+
|
|
199
|
+
Names expand as follows, with case preserved:
|
|
200
|
+
|
|
201
|
+
| LE form or name | RDF mapping |
|
|
202
|
+
| --- | --- |
|
|
203
|
+
| An ordinary individual name | An IRI formed by appending it to the declared namespace. |
|
|
204
|
+
| Classes `evaluation`, `request`, `state` | `eval:Evaluation`, `eval:Request`, `eval:State`. |
|
|
205
|
+
| Classes `policy`, `agreement`, `permission`, `prohibition`, `constraint`, `action` | `odrl:Set`, `odrl:Agreement`, `odrl:Permission`, `odrl:Prohibition`, `odrl:Constraint`, `odrl:Action`. |
|
|
206
|
+
| Classes `assetCollection`, `partyCollection` | ODRL collection classes, rejected by the snapshot profile. |
|
|
207
|
+
| Properties `policy`, `request`, `state`, `subject`, `conflictStrategy` | The corresponding `eval:` properties. |
|
|
208
|
+
| Properties `profile`, `permission`, `prohibition`, `assignee`, `assigner`, `action`, `target`, `constraint`, `leftOperand`, `operator`, `rightOperand` | The corresponding `odrl:` properties. |
|
|
209
|
+
| Property `policyConflict` | `odrl:conflict`, the policy-local strategy. |
|
|
210
|
+
| Property `type` | `rdf:type`. |
|
|
211
|
+
| An otherwise unknown class or property | An IRI in the declared namespace; validation still applies. |
|
|
212
|
+
| Values `snapshotProfile`, `perm`, `prohibit`, `invalid`, `eq`, `neq`, `gt`, `gteq`, `lt`, `lteq` | `eval:SnapshotProfile` or the corresponding ODRL strategy/operator IRIs. |
|
|
213
|
+
| Values `true`, `false` | Boolean RDF literals. Quoting does not change an LE name into an RDF string literal. |
|
|
214
|
+
| An integer or float | An exact RDF integer or an `xsd:double` literal. |
|
|
215
|
+
|
|
216
|
+
The unsupported ODRL properties `duty`, `obligation`, `inheritFrom`, `includedIn`,
|
|
217
|
+
`partOf` and `refinement` retain their ODRL IRIs so the shared validator can
|
|
218
|
+
report them. Explicit RDF values such as `iri("https://example.org/item")` and
|
|
219
|
+
`literal("text", datatype("http://www.w3.org/2001/XMLSchema#string"))` retain
|
|
220
|
+
their terms. `rdf(S, P, O)` facts allow any explicit RDF predicate without an
|
|
221
|
+
abbreviation. A snapshot still has one default graph.
|
|
222
|
+
|
|
223
|
+
The [handicap-card LE example](https://eyereasoner.github.io/eyemind/examples/deck/handicap-card-le)
|
|
224
|
+
contains the complete policies, requests and SOTW. Run it without CLI switches:
|
|
225
|
+
|
|
226
|
+
```sh
|
|
227
|
+
eyemind examples/handicap-card-le.le
|
|
228
|
+
eyemind --program examples/handicap-card-le.le > /tmp/handicap-card-le.js
|
|
229
|
+
eyemind --to turtle /tmp/handicap-card-le.js
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
Its default output is an LE report whose first sentence is
|
|
233
|
+
`Use ODRL vocabulary with namespace "https://example.org/handicap-card#".`.
|
|
234
|
+
That directive reads RDF data without evaluating policies. This makes the report
|
|
235
|
+
readable as input again, preserving every RDF fact and witness. Report properties
|
|
236
|
+
use `has`; for example `issueAlice has decision allowed.`. Status values
|
|
237
|
+
`allowed`, `prohibited`, `notApplicable`, `undetermined`, `valid` and
|
|
238
|
+
`invalidReport` name the corresponding `eval:` IRIs. Report classes include
|
|
239
|
+
`evaluationReport`, `conflict`, `validationError` and `uncertainty`.
|
|
240
|
+
`conflict` as a property means `eval:conflict`; `applicablePermission`,
|
|
241
|
+
`applicableProhibition` and `reportConstraint` distinguish the report's
|
|
242
|
+
`eval:permission`, `eval:prohibition` and `eval:constraint` links from ODRL
|
|
243
|
+
input properties. Other report fields retain their `eval:` names. An IRI with
|
|
244
|
+
no unambiguous abbreviation stays explicit, including IRIs inside witness keys.
|
|
245
|
+
|
|
246
|
+
Both ODRL directives take a single LE file and reject mixed-in ordinary LE
|
|
247
|
+
rules or questions. `--goal` can query their compiled LS program. The normal
|
|
248
|
+
`--odrl` switch continues to select the Turtle snapshot compiler.
|
|
249
|
+
|
|
177
250
|
## Output, compilation and checking
|
|
178
251
|
|
|
179
|
-
|
|
252
|
+
LE inputs default to `.le` conclusions. Each answer becomes a sentence:
|
|
180
253
|
|
|
181
254
|
```text
|
|
182
255
|
Socrates is a mortal.
|
|
@@ -186,7 +259,7 @@ Alice is an owner of witness(rule 4, Y, Alice).
|
|
|
186
259
|
|
|
187
260
|
`witness(rule 4, Y, Alice)` names the witness for existential variable `Y` of
|
|
188
261
|
rule 4 with argument Alice. It reads back as `skolem(4, 'Y', ['Alice'])` in LS.
|
|
189
|
-
The label `Y` in this position is a name, independent of any declared
|
|
262
|
+
The label `Y` in this position is a name, independent of any declared LE
|
|
190
263
|
variable. A label outside the variable-name syntax is quoted. Additional
|
|
191
264
|
arguments follow as more comma-separated terms; a witness without arguments
|
|
192
265
|
is `witness(rule 4, Y)`. The rule number, label and arguments all contribute
|
|
@@ -196,28 +269,28 @@ Unary and binary relations use the class and relation-of forms; relations with
|
|
|
196
269
|
more arguments use calls. Names are quoted when required to preserve their
|
|
197
270
|
meaning. Compound names are quoted. Variable answers use `For every X0 and X1,
|
|
198
271
|
...`, consistently renaming their variables while retaining sharing. A conjunctive
|
|
199
|
-
answer uses `and`. These sentences can be loaded as facts in another `.
|
|
272
|
+
answer uses `and`. These sentences can be loaded as facts in another `.le`
|
|
200
273
|
program. An empty answer set is `# No conclusions.`. The writer rejects goals
|
|
201
274
|
outside these relational forms instead of silently changing their meaning.
|
|
202
275
|
|
|
203
276
|
```sh
|
|
204
|
-
eyemind examples/socrates-
|
|
205
|
-
eyemind --program examples/socrates-
|
|
206
|
-
eyemind --to
|
|
207
|
-
eyemind --proof examples/socrates-
|
|
208
|
-
eyemind --check-proof /tmp/socrates-
|
|
277
|
+
eyemind examples/socrates-le.le
|
|
278
|
+
eyemind --program examples/socrates-le.le > /tmp/socrates-le.js
|
|
279
|
+
eyemind --to le /tmp/socrates-le.js
|
|
280
|
+
eyemind --proof examples/socrates-le.le > /tmp/socrates-le-proof.js
|
|
281
|
+
eyemind --check-proof /tmp/socrates-le-proof.js examples/socrates-le.le
|
|
209
282
|
```
|
|
210
283
|
|
|
211
|
-
Multiple `.
|
|
212
|
-
input formats or the RDF logic switches. Standard input remains LS; `--to
|
|
213
|
-
can write supported LS conclusions as
|
|
214
|
-
defaults to LS output; add `--to
|
|
215
|
-
output, without changing
|
|
284
|
+
Multiple `.le` files form one program in file order; they do not mix with other
|
|
285
|
+
input formats or the RDF logic switches. Standard input remains LS; `--to le`
|
|
286
|
+
can write supported LS conclusions as LE. `--goal` uses LS goal syntax and
|
|
287
|
+
defaults to LS output; add `--to le` for LE output. `--all` applies to RDF and N3
|
|
288
|
+
output, without changing LE's selected conclusions.
|
|
216
289
|
|
|
217
290
|
The compiler reads data without executing it and emits standalone `.js` LS
|
|
218
291
|
using explicit constructors to retain names and numeric types. Comments in the
|
|
219
292
|
compiled text identify the source file and sentence line. Parser errors name
|
|
220
|
-
that location.
|
|
293
|
+
that location. LE runs use LS's usual work bounds, which can be overridden by
|
|
221
294
|
the CLI. Existential or recursive rules can still have an infinite closure.
|
|
222
295
|
|
|
223
296
|
Proofs and check reports are `.js` documents and use the existing LS checker.
|
package/README.md
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
[](https://doi.org/10.5281/zenodo.23824021)
|
|
4
4
|
[](https://www.npmjs.com/package/eyemind)
|
|
5
5
|
|
|
6
|
-
eyemind is a dependency-free reasoner for **Logic Script (LS)**, a rule language with JavaScript syntax. LS programs state facts, rules, and queries; eyemind draws conclusions using forward and backward reasoning, and can produce proofs that a separate checker verifies against your program. It interoperates with **RDF 1.2**, **Notation3 (N3)** and **SPARQL 1.2 RL (SRL)**: RDF supplies data, and N3 and SRL compile to LS programs whose proofs use the same checker.
|
|
6
|
+
eyemind is a dependency-free reasoner for **Logic Script (LS)**, a rule language with JavaScript syntax. LS programs state facts, rules, and queries; eyemind draws conclusions using forward and backward reasoning, and can produce proofs that a separate checker verifies against your program. It interoperates with **RDF 1.2**, **Notation3 (N3)** and **SPARQL 1.2 RL (SRL)**: RDF supplies data, and N3 and SRL compile to LS programs whose proofs use the same checker. **Logical English (LE)** also compiles to LS and writes `.le` conclusions. It runs as a command-line program, a JavaScript library, and a browser application.
|
|
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 how a proof is checked. The examples are
|
|
10
|
+
The playground runs programs in your browser; the specification defines the language, its canonical text, the proof format and how a proof is checked. The examples are 142 runnable programs, each with a card deck and saved conclusions, proofs and check reports. The conformance suite has 1246 cases that test language and proof behavior through a command adapter, together with implementation interface checks; run it [in your browser](https://eyereasoner.github.io/eyemind/playground/conformance).
|
|
11
11
|
|
|
12
12
|
## Contents
|
|
13
13
|
|
|
@@ -18,7 +18,7 @@ The playground runs programs in your browser; the specification defines the lang
|
|
|
18
18
|
- [Terms and names](#terms-and-names)
|
|
19
19
|
- [Deriving rules](#deriving-rules)
|
|
20
20
|
- [Inspecting rules](#inspecting-rules)
|
|
21
|
-
- [
|
|
21
|
+
- [Logical English](#logical-english)
|
|
22
22
|
- [RDF 1.2](#rdf-12)
|
|
23
23
|
- [Notation3](#notation3)
|
|
24
24
|
- [RDF Messages](#rdf-messages)
|
|
@@ -180,9 +180,9 @@ query(test(X, Y))
|
|
|
180
180
|
|
|
181
181
|
The answer is `test(a(A), c(A))`, with the shared variable preserved. Inspection sees ordinary source rules only, not facts, queries, generators or itself, and its proof step names the source rule it matched. See the [rule inspection deck](https://eyereasoner.github.io/eyemind/examples/deck/rule-inspection) and [SPEC §6.3](https://eyereasoner.github.io/eyemind/SPEC#63-source-rule-inspection).
|
|
182
182
|
|
|
183
|
-
###
|
|
183
|
+
### Logical English
|
|
184
184
|
|
|
185
|
-
A `.
|
|
185
|
+
A `.le` file is [Logical English](https://eyereasoner.github.io/eyemind/LOGICAL-ENGLISH),
|
|
186
186
|
compiled deterministically into LS. Facts, class rules, quantified if/then rules,
|
|
187
187
|
calculations, explicit absence tests, existential conclusions, questions and
|
|
188
188
|
contradictions have a fixed grammar. Unsupported prose is an error.
|
|
@@ -194,31 +194,35 @@ Find every X such that X is mortal.
|
|
|
194
194
|
```
|
|
195
195
|
|
|
196
196
|
```sh
|
|
197
|
-
eyemind examples/socrates-
|
|
198
|
-
eyemind --program examples/socrates-
|
|
199
|
-
eyemind --to
|
|
200
|
-
eyemind --proof examples/socrates-
|
|
201
|
-
eyemind --check-proof /tmp/socrates-
|
|
197
|
+
eyemind examples/socrates-le.le > /tmp/socrates-le.le
|
|
198
|
+
eyemind --program examples/socrates-le.le > /tmp/socrates-le.js
|
|
199
|
+
eyemind --to le /tmp/socrates-le.js
|
|
200
|
+
eyemind --proof examples/socrates-le.le > /tmp/socrates-le-proof.js
|
|
201
|
+
eyemind --check-proof /tmp/socrates-le-proof.js examples/socrates-le.le
|
|
202
202
|
```
|
|
203
203
|
|
|
204
|
-
Conclusions default to `.
|
|
205
|
-
another
|
|
204
|
+
Conclusions default to `.le`: `Socrates is a mortal.`. They can supply facts to
|
|
205
|
+
another LE program. `--to le` also writes supported LS conclusions in this
|
|
206
206
|
format. Proofs and check reports remain `.js`, and the standalone compilation
|
|
207
207
|
contains all its LS clauses with source sentence locations. The existing checker
|
|
208
208
|
checks the compiled LS; the documented grammar and compiler tests define the
|
|
209
209
|
English translation.
|
|
210
210
|
|
|
211
|
-
Multiple `.
|
|
211
|
+
Multiple `.le` files form one program. Names are case-sensitive, variables such
|
|
212
212
|
as `X` and `Y1` are explicitly declared within each sentence, and integers stay
|
|
213
213
|
exact. Arithmetic and absence tests require their inputs from earlier premises;
|
|
214
214
|
absence is written `it is not known that`, with LS's stratified meaning. A rule
|
|
215
|
-
can introduce witnesses with `there exists`.
|
|
215
|
+
can introduce witnesses with `there exists`. LE uses the ordinary LS resource
|
|
216
216
|
bounds; recursive and existential rules can still have an infinite closure.
|
|
217
217
|
|
|
218
|
-
The playground loads
|
|
219
|
-
Try [Socrates](https://eyereasoner.github.io/eyemind/examples/deck/socrates-
|
|
220
|
-
[exact squares](https://eyereasoner.github.io/eyemind/examples/deck/squares-
|
|
221
|
-
and [record creation](https://eyereasoner.github.io/eyemind/examples/deck/records-
|
|
218
|
+
The playground loads LE examples and downloads conclusions as `output.le`.
|
|
219
|
+
Try [Socrates](https://eyereasoner.github.io/eyemind/examples/deck/socrates-le),
|
|
220
|
+
[exact squares](https://eyereasoner.github.io/eyemind/examples/deck/squares-le)
|
|
221
|
+
and [record creation](https://eyereasoner.github.io/eyemind/examples/deck/records-le).
|
|
222
|
+
The [handicap-card LE snapshot](https://eyereasoner.github.io/eyemind/examples/deck/handicap-card-le)
|
|
223
|
+
explicitly selects the shared ODRL evaluator with `Use ODRL snapshot logic with
|
|
224
|
+
namespace ...`. It expresses the Turtle example's policies and SOTW as English
|
|
225
|
+
property facts and writes the same decisions and conflicts as an LE report.
|
|
222
226
|
|
|
223
227
|
### RDF 1.2
|
|
224
228
|
|
|
@@ -711,7 +715,7 @@ eyemind [OPTION ...] [FILE ...]
|
|
|
711
715
|
|
|
712
716
|
| Option | Meaning |
|
|
713
717
|
| --- | --- |
|
|
714
|
-
| (none) | Load the files, or standard input, as one program and print its conclusions. `.js` files and standard input are Logic Script, `.ttl`, `.trig`, `.nt` and `.nq` files RDF data, `.n3` Notation3, `.srl` a SPARQL 1.2 RL rule set and `.
|
|
718
|
+
| (none) | Load the files, or standard input, as one program and print its conclusions. `.js` files and standard input are Logic Script, `.ttl`, `.trig`, `.nt` and `.nq` files RDF data, `.n3` Notation3, `.srl` a SPARQL 1.2 RL rule set and `.le` Logical English. |
|
|
715
719
|
| `-p`, `--proof` | Print the proof instead. |
|
|
716
720
|
| `-c`, `--prove-and-check` | Generate a proof, check it, and print the checked answers with the report. |
|
|
717
721
|
| `--check-proof FILE` | Check a saved proof (`-` for standard input) against the program. |
|
|
@@ -721,7 +725,7 @@ eyemind [OPTION ...] [FILE ...]
|
|
|
721
725
|
| `--stream-messages` | Read an RDF Message Log and run the rules independently per message; Node reads files, stdin and HTTP(S) incrementally. |
|
|
722
726
|
| `--stats` | Print reasoning statistics as JSON to standard error. |
|
|
723
727
|
| `--max-depth N`, `--max-iterations N`, `--max-inferences N` | Bound backward depth (1000000), forward rounds per stratum (1000) and goals started (1000000); a compiled N3, SRL, ODRL, RDFS or OWL-RL program has 1000000, 100000 and 1000000000. |
|
|
724
|
-
| `--to FORMAT` | Write the conclusions as `turtle`, `trig`, `n-triples`, `n-quads`, `n3`, `
|
|
728
|
+
| `--to FORMAT` | Write the conclusions as `turtle`, `trig`, `n-triples`, `n-quads`, `n3`, `le` or `eyemind`; by default LE for LE, N3 for N3, Turtle for SRL, ODRL, RDFS and OWL-RL, eyemind otherwise. |
|
|
725
729
|
| `--all` | With RDF or N3 output, write the given data too. |
|
|
726
730
|
| `--program` | Print the program the inputs compile to. |
|
|
727
731
|
| `--odrl` | Add the ODRL snapshot profile's validation and evaluation LS to one Turtle input. |
|
|
@@ -736,7 +740,7 @@ each goal as a data document, with an array denoting conjunction. With
|
|
|
736
740
|
`--check-proof`, supply the original goals the proof answers.
|
|
737
741
|
An option that takes a value requires it immediately afterward; another
|
|
738
742
|
option, including a short alias such as `-v`, is reported as a missing value.
|
|
739
|
-
With `--goal`, answers default to eyemind terms even for
|
|
743
|
+
With `--goal`, answers default to eyemind terms even for LE, N3, SRL, ODRL, RDFS and OWL-RL inputs;
|
|
740
744
|
use `--to` to request another output format.
|
|
741
745
|
|
|
742
746
|
With `--prove-and-check`, reasoning bounds and `--stats` apply to proof
|
|
@@ -837,7 +841,7 @@ console.log(result.bindings);
|
|
|
837
841
|
console.log(checkProof(socrates, result.proof, { goals }).valid);
|
|
838
842
|
```
|
|
839
843
|
|
|
840
|
-
`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. `
|
|
844
|
+
`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], ...])` returns the compiled LS `program` and standalone JavaScript `text`; `writeLe(run(program))` writes its conclusions as LE.
|
|
841
845
|
|
|
842
846
|
## What is deliberately absent
|
|
843
847
|
|
package/SPEC.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eyemind",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "A dependency-free Logic Script (LS) reasoner with forward and backward reasoning, checkable proofs, and RDF 1.2, Notation3 (N3) and SPARQL 1.2 RL (SRL) interoperability.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"src/",
|
|
18
18
|
"README.md",
|
|
19
19
|
"SPEC.md",
|
|
20
|
-
"
|
|
20
|
+
"LOGICAL-ENGLISH.md",
|
|
21
21
|
"LICENSE.md"
|
|
22
22
|
],
|
|
23
23
|
"scripts": {
|
package/src/cli.js
CHANGED
|
@@ -10,7 +10,7 @@ import { srlContextFromProof } from './srl.js';
|
|
|
10
10
|
export const HELP = `Usage: eyemind [OPTION ...] [FILE ...]
|
|
11
11
|
Logic Script (LS) programs use JavaScript syntax. Forward rules run to a fixpoint.
|
|
12
12
|
Files are read by extension: .js as LS, .ttl, .trig, .nt and .nq as RDF 1.2 data,
|
|
13
|
-
.n3 as Notation3, .srl as SPARQL 1.2 RL and .
|
|
13
|
+
.n3 as Notation3, .srl as SPARQL 1.2 RL and .le as Logical English,
|
|
14
14
|
all compiled to LS.
|
|
15
15
|
-p, --proof Print a proof of the conclusions
|
|
16
16
|
-c, --prove-and-check Generate a proof and check it against the input program
|
|
@@ -24,7 +24,7 @@ all compiled to LS.
|
|
|
24
24
|
--max-iterations N Bound forward rounds per stratum (default 1000)
|
|
25
25
|
--max-inferences N Bound reasoning work (default 1000000)
|
|
26
26
|
--to FORMAT Write the conclusions as turtle, trig, n-triples,
|
|
27
|
-
n-quads, n3,
|
|
27
|
+
n-quads, n3, le or eyemind (default: le for LE, n3 for N3, turtle
|
|
28
28
|
for SRL, ODRL, RDFS or OWL-RL, eyemind otherwise or with --goal)
|
|
29
29
|
--all With RDF or N3 output, write the given data too
|
|
30
30
|
--program Print the program the inputs compile to
|
|
@@ -200,7 +200,7 @@ export function main(argv, io) {
|
|
|
200
200
|
return 0;
|
|
201
201
|
}
|
|
202
202
|
// N3, SRL and the RDF logic extensions have larger default bounds.
|
|
203
|
-
if (!['js', '
|
|
203
|
+
if (!['js', 'le'].includes(loaded.kind)) for (const [name, value] of Object.entries(COMPILED_LIMITS)) options[name] ??= value;
|
|
204
204
|
if (proofFile !== null) {
|
|
205
205
|
const report = checkProof(program, document, { goals });
|
|
206
206
|
stdout(asJson ? `${JSON.stringify(publicReport(report), null, 2)}\n` : checkReport(report));
|
package/src/formats.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// The inputs of a run in other formats than Logic Script, and its output as
|
|
2
|
-
// RDF, N3 or
|
|
2
|
+
// RDF, N3 or Logical English.
|
|
3
3
|
//
|
|
4
4
|
// A file's extension says what it holds:
|
|
5
5
|
//
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
// rule set, its base graph
|
|
11
11
|
// .n3 Notation3, compiled to an LS program
|
|
12
12
|
// .srl a SPARQL 1.2 RL rule set, compiled to one
|
|
13
|
-
// .
|
|
13
|
+
// .le Logical English, compiled to LS
|
|
14
14
|
//
|
|
15
15
|
// The odrl option adds the snapshot profile's validation and evaluation LS
|
|
16
16
|
// to one Turtle input. Its vocabulary alone never enables the evaluator.
|
|
@@ -27,18 +27,18 @@ import { resultToN3, compileSources } from './n3.js';
|
|
|
27
27
|
import { compileSrl, srlAnswers, srlQuads } from './srl.js';
|
|
28
28
|
import { compileOdrl } from './odrl.js';
|
|
29
29
|
import { compileEntailment } from './entailment.js';
|
|
30
|
-
import {
|
|
30
|
+
import { compileLe, writeLe } from './le.js';
|
|
31
31
|
import { write } from './writer.js';
|
|
32
32
|
import { isRdfMessageLog, parseRdfMessages } from './messages.js';
|
|
33
33
|
|
|
34
|
-
export const OUTPUT_FORMATS = ['eyemind', 'turtle', 'trig', 'n-triples', 'n-quads', 'n3', '
|
|
34
|
+
export const OUTPUT_FORMATS = ['eyemind', 'turtle', 'trig', 'n-triples', 'n-quads', 'n3', 'le'];
|
|
35
35
|
|
|
36
|
-
// What a file holds: 'js', 'rdf', 'n3', 'srl' or '
|
|
36
|
+
// What a file holds: 'js', 'rdf', 'n3', 'srl' or 'le'.
|
|
37
37
|
export function inputKind(name) {
|
|
38
38
|
if (rdfFormat(name)) return 'rdf';
|
|
39
39
|
if (/\.n3$/i.test(name)) return 'n3';
|
|
40
40
|
if (/\.srl$/i.test(name)) return 'srl';
|
|
41
|
-
if (/\.
|
|
41
|
+
if (/\.le$/i.test(name)) return 'le';
|
|
42
42
|
return 'js';
|
|
43
43
|
}
|
|
44
44
|
|
|
@@ -50,7 +50,7 @@ export function rdfMessageFormat(name) {
|
|
|
50
50
|
// Load inputs, given as [text, name] pairs, as one program. io may supply
|
|
51
51
|
// readFile, for the documents N3 and IMPORTS name, and fileIri(name), the
|
|
52
52
|
// base IRI of a file. Returns { kind, program, text, prefixes, compiled }:
|
|
53
|
-
// kind is 'js', 'n3', 'srl', '
|
|
53
|
+
// kind is 'js', 'n3', 'srl', 'le', 'odrl', 'rdfs' or 'owl-rl', text the compiled program,
|
|
54
54
|
// and compiled what writing the conclusions needs.
|
|
55
55
|
export function loadInputs(inputs, io = null, options = {}) {
|
|
56
56
|
try {
|
|
@@ -71,11 +71,11 @@ function load(inputs, io, { base = null, messageInputs = null, srlContext = null
|
|
|
71
71
|
format: rdfFormat(name) ?? 'trig', base: baseOf(name), scope: scopeOf(inputs, name, k),
|
|
72
72
|
});
|
|
73
73
|
// These rule sets are added only when requested, never from vocabulary.
|
|
74
|
-
if (kinds.includes('
|
|
75
|
-
if (kinds.some((kind) => kind !== '
|
|
76
|
-
throw new EyemindError('a
|
|
74
|
+
if (kinds.includes('le')) {
|
|
75
|
+
if (kinds.some((kind) => kind !== 'le') || odrl || rdfs || owlRl) {
|
|
76
|
+
throw new EyemindError('a Logical English run takes .le files without RDF logic switches or other input formats');
|
|
77
77
|
}
|
|
78
|
-
return
|
|
78
|
+
return compileLe(inputs);
|
|
79
79
|
}
|
|
80
80
|
if ((rdfs || owlRl) && odrl) throw new EyemindError('--odrl cannot be combined with --rdfs or --owl-rl');
|
|
81
81
|
if (rdfs || owlRl) {
|
|
@@ -153,15 +153,15 @@ function importResolver(io) {
|
|
|
153
153
|
};
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
-
// Default output:
|
|
156
|
+
// Default output: LE for LE, N3 for N3, Turtle for SRL and the RDF logic
|
|
157
157
|
// extensions, and eyemind's own text for an LS program.
|
|
158
|
-
export const defaultFormat = (kind) => ({
|
|
158
|
+
export const defaultFormat = (kind) => ({ le: 'le', n3: 'n3', srl: 'turtle', odrl: 'turtle', rdfs: 'turtle', 'owl-rl': 'turtle' })[kind] ?? 'eyemind';
|
|
159
159
|
|
|
160
160
|
// The conclusions of a run in an output format; null for eyemind's own text.
|
|
161
161
|
// all also writes given RDF or N3 data. Rejects conclusions the format cannot represent.
|
|
162
162
|
export function writeConclusions(loaded, result, { format, all = false, blankNodePrefix = '' } = {}) {
|
|
163
163
|
if (format === 'eyemind') return null;
|
|
164
|
-
if (format === '
|
|
164
|
+
if (format === 'le') return writeLe(result, { odrl: loaded.compiled?.odrl ?? null });
|
|
165
165
|
if (format === 'n3') {
|
|
166
166
|
if (loaded.kind !== 'n3') throw new EyemindError('--to n3 writes the conclusions of an N3 run');
|
|
167
167
|
return resultToN3(loaded.compiled.doc, result, { mode: all ? 'all' : 'new', blankNodePrefix }).n3;
|
package/src/index.js
CHANGED
|
@@ -14,4 +14,4 @@ export { readTerm, readTerms } from './reader.js';
|
|
|
14
14
|
export { EyemindError, Struct, Var } from './terms.js';
|
|
15
15
|
export { write } from './writer.js';
|
|
16
16
|
export { RdfMessageReader, parseRdfMessages } from './messages.js';
|
|
17
|
-
export {
|
|
17
|
+
export { compileLe, writeLe } from './le.js';
|
package/src/le-odrl.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// LE names for the existing ODRL snapshot profile. This module maps data and
|
|
2
|
+
// reports; validation and evaluation remain the rules of odrl.js.
|
|
3
|
+
import { compileOdrl, ODRL_EVAL } from './odrl.js';
|
|
4
|
+
import { loadText } from './lang.js';
|
|
5
|
+
import { DEFAULT_GRAPH, writeRdf } from './rdf.js';
|
|
6
|
+
import { EyemindError, Struct, isGround } from './terms.js';
|
|
7
|
+
import { readTerm } from './reader.js';
|
|
8
|
+
import { write } from './writer.js';
|
|
9
|
+
|
|
10
|
+
const ODRL = 'http://www.w3.org/ns/odrl/2/';
|
|
11
|
+
const RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
|
|
12
|
+
const XSD = 'http://www.w3.org/2001/XMLSchema#';
|
|
13
|
+
const iri = (value) => new Struct('iri', [value]);
|
|
14
|
+
const literal = (value, type) => new Struct('literal', [value, new Struct('datatype', [XSD + type])]);
|
|
15
|
+
const NAME = /^[a-z][A-Za-z0-9_]*$/;
|
|
16
|
+
const TYPES = new Map(Object.entries({
|
|
17
|
+
evaluation: ODRL_EVAL + 'Evaluation', policy: ODRL + 'Set', agreement: ODRL + 'Agreement',
|
|
18
|
+
permission: ODRL + 'Permission', prohibition: ODRL + 'Prohibition', constraint: ODRL + 'Constraint',
|
|
19
|
+
request: ODRL_EVAL + 'Request', state: ODRL_EVAL + 'State', action: ODRL + 'Action',
|
|
20
|
+
assetCollection: ODRL + 'AssetCollection', partyCollection: ODRL + 'PartyCollection',
|
|
21
|
+
evaluationReport: ODRL_EVAL + 'EvaluationReport', conflict: ODRL_EVAL + 'Conflict',
|
|
22
|
+
validationError: ODRL_EVAL + 'ValidationError', uncertainty: ODRL_EVAL + 'Uncertainty',
|
|
23
|
+
}));
|
|
24
|
+
const PROPERTIES = new Map();
|
|
25
|
+
for (const name of ['profile', 'permission', 'prohibition', 'assignee', 'assigner', 'action', 'target',
|
|
26
|
+
'constraint', 'leftOperand', 'operator', 'rightOperand', 'duty', 'obligation', 'inheritFrom',
|
|
27
|
+
'includedIn', 'partOf', 'refinement']) PROPERTIES.set(name, ODRL + name);
|
|
28
|
+
for (const name of ['policy', 'request', 'state', 'subject', 'conflictStrategy', 'decision', 'canHappen',
|
|
29
|
+
'validationStatus', 'invalidPolicy', 'reason', 'unmetConstraint', 'uncertainty', 'rule', 'property',
|
|
30
|
+
'validationError', 'focus', 'code', 'permissionPolicy', 'permissionRule', 'prohibitionPolicy',
|
|
31
|
+
'prohibitionRule', 'conflictCount']) PROPERTIES.set(name, ODRL_EVAL + name);
|
|
32
|
+
// A conflict is an evaluation report link; policy-local conflict has a
|
|
33
|
+
// separate name so neither meaning silently replaces the other.
|
|
34
|
+
PROPERTIES.set('conflict', ODRL_EVAL + 'conflict');
|
|
35
|
+
PROPERTIES.set('policyConflict', ODRL + 'conflict');
|
|
36
|
+
PROPERTIES.set('applicablePermission', ODRL_EVAL + 'permission');
|
|
37
|
+
PROPERTIES.set('applicableProhibition', ODRL_EVAL + 'prohibition');
|
|
38
|
+
PROPERTIES.set('reportConstraint', ODRL_EVAL + 'constraint');
|
|
39
|
+
PROPERTIES.set('type', RDF + 'type');
|
|
40
|
+
const VALUES = new Map(Object.entries({
|
|
41
|
+
snapshotProfile: ODRL_EVAL + 'SnapshotProfile',
|
|
42
|
+
perm: ODRL + 'perm', prohibit: ODRL + 'prohibit', invalid: ODRL + 'invalid',
|
|
43
|
+
eq: ODRL + 'eq', neq: ODRL + 'neq', gt: ODRL + 'gt', gteq: ODRL + 'gteq', lt: ODRL + 'lt', lteq: ODRL + 'lteq',
|
|
44
|
+
allowed: ODRL_EVAL + 'Allowed', prohibited: ODRL_EVAL + 'Prohibited',
|
|
45
|
+
notApplicable: ODRL_EVAL + 'NotApplicable', undetermined: ODRL_EVAL + 'Undetermined',
|
|
46
|
+
valid: ODRL_EVAL + 'Valid', invalidReport: ODRL_EVAL + 'Invalid',
|
|
47
|
+
}));
|
|
48
|
+
const reverse = (map) => new Map([...map].map(([name, value]) => [value, name]));
|
|
49
|
+
const TYPE_NAMES = reverse(TYPES), PROPERTY_NAMES = reverse(PROPERTIES), VALUE_NAMES = reverse(VALUES);
|
|
50
|
+
|
|
51
|
+
function rdfValue(term, namespace) {
|
|
52
|
+
if (typeof term === 'string') {
|
|
53
|
+
if (term === 'true' || term === 'false') return literal(term, 'boolean');
|
|
54
|
+
return iri(VALUES.get(term) ?? namespace + term);
|
|
55
|
+
}
|
|
56
|
+
if (typeof term === 'bigint') return term;
|
|
57
|
+
if (typeof term === 'number') return literal(write(term), 'double');
|
|
58
|
+
if (term instanceof Struct && ['iri', 'bnode', 'literal', 'triple', 'skolem'].includes(term.name)) return term;
|
|
59
|
+
throw new EyemindError('LE ODRL values need names, numbers or explicit RDF terms such as iri(...) and literal(...)');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function resource(term) {
|
|
63
|
+
return term instanceof Struct && ['iri', 'bnode', 'triple', 'skolem'].includes(term.name);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function compileOdrlLe(statements, profile, source) {
|
|
67
|
+
const { namespace, evaluate } = profile;
|
|
68
|
+
const quads = [];
|
|
69
|
+
for (const statement of statements) {
|
|
70
|
+
if (statement.kind !== 'fact') throw new EyemindError(`${statement.file}: line ${statement.line}: an LE ODRL snapshot or report contains ground facts, not rules or questions`);
|
|
71
|
+
for (const head of statement.heads) {
|
|
72
|
+
if (!(head instanceof Struct) || !isGround(head)) throw new EyemindError(`${statement.file}: line ${statement.line}: an LE ODRL fact needs a ground class or property statement`);
|
|
73
|
+
let s, p, o;
|
|
74
|
+
if (head.name === 'rdf' && head.args.length === 3) [s, p, o] = head.args;
|
|
75
|
+
else if (head.args.length === 1) {
|
|
76
|
+
s = rdfValue(head.args[0], namespace); p = iri(RDF + 'type');
|
|
77
|
+
o = iri(TYPES.get(head.name) ?? namespace + head.name);
|
|
78
|
+
} else if (head.args.length === 2) {
|
|
79
|
+
s = rdfValue(head.args[0], namespace); p = iri(PROPERTIES.get(head.name) ?? namespace + head.name);
|
|
80
|
+
o = rdfValue(head.args[1], namespace);
|
|
81
|
+
} else throw new EyemindError(`${statement.file}: line ${statement.line}: use class facts, binary properties or explicit rdf(S, P, O) facts in LE ODRL`);
|
|
82
|
+
if (!resource(s) || !(p instanceof Struct && p.name === 'iri' && p.args.length === 1)) {
|
|
83
|
+
throw new EyemindError(`${statement.file}: line ${statement.line}: an LE ODRL triple needs a resource subject and an IRI property`);
|
|
84
|
+
}
|
|
85
|
+
quads.push([s, p, o, DEFAULT_GRAPH]);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// Validate RDF constructors without rewriting terms or witness identities.
|
|
89
|
+
const pending = quads.flatMap((quad) => quad.slice(0, 3));
|
|
90
|
+
while (pending.length) {
|
|
91
|
+
const term = pending.pop();
|
|
92
|
+
if (!(term instanceof Struct) || ['skolem', 'bnode'].includes(term.name)) continue;
|
|
93
|
+
if (term.name === 'iri' || term.name === 'datatype') {
|
|
94
|
+
const [value] = term.args;
|
|
95
|
+
if (term.args.length !== 1 || typeof value !== 'string' || !/^[A-Za-z][A-Za-z0-9+.-]*:/.test(value) || /[\s<>"{}|^`\\]/u.test(value)) {
|
|
96
|
+
throw new EyemindError('LE ODRL IRIs and datatype IRIs must be absolute, without whitespace or forbidden characters');
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
pending.push(...term.args);
|
|
100
|
+
}
|
|
101
|
+
writeRdf(quads, { format: 'n-triples' });
|
|
102
|
+
const prefixes = { '': namespace, odrl: ODRL, eval: ODRL_EVAL };
|
|
103
|
+
let loaded;
|
|
104
|
+
if (evaluate) loaded = compileOdrl({ quads, prefixes }, { source });
|
|
105
|
+
else {
|
|
106
|
+
const facts = quads.map(([s, p, o]) => write(new Struct('rdf', [s, p, o]))).join('\n');
|
|
107
|
+
const text = `// RDF facts from an LE ODRL vocabulary document.\nfacts_from({ text: ${JSON.stringify(facts)} })\n`;
|
|
108
|
+
loaded = { text, program: loadText(text, source), prefixes };
|
|
109
|
+
}
|
|
110
|
+
return { ...loaded, kind: 'le', compiled: { statements, odrl: profile, quads } };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function leValue(term, namespace) {
|
|
114
|
+
if (term instanceof Struct && term.name === 'iri' && term.args.length === 1) {
|
|
115
|
+
const value = term.args[0], known = VALUE_NAMES.get(value);
|
|
116
|
+
if (known) return known;
|
|
117
|
+
if (value.startsWith(namespace)) {
|
|
118
|
+
const name = value.slice(namespace.length);
|
|
119
|
+
if (/^[A-Za-z][A-Za-z0-9_]*$/.test(name) && !VALUES.has(name) && name !== 'true' && name !== 'false') return name;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (term instanceof Struct && term.name === 'literal' && term.args.length === 2 &&
|
|
123
|
+
term.args[1] instanceof Struct && term.args[1].name === 'datatype' && term.args[1].args[0] === XSD + 'boolean' &&
|
|
124
|
+
['true', 'false'].includes(term.args[0])) return term.args[0];
|
|
125
|
+
return term;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function odrlLeReport(result, namespace) {
|
|
129
|
+
const answers = result.answers.map((answer) => {
|
|
130
|
+
const term = readTerm(answer);
|
|
131
|
+
if (!(term instanceof Struct) || term.name !== 'rdf' || term.args.length !== 3) throw new EyemindError('LE ODRL reports need rdf(S, P, O) conclusions');
|
|
132
|
+
const [s, p, o] = term.args;
|
|
133
|
+
if (p instanceof Struct && p.name === 'iri') {
|
|
134
|
+
if (p.args[0] === RDF + 'type' && o instanceof Struct && o.name === 'iri' && TYPE_NAMES.has(o.args[0])) {
|
|
135
|
+
return write(new Struct(TYPE_NAMES.get(o.args[0]), [leValue(s, namespace)]));
|
|
136
|
+
}
|
|
137
|
+
const name = PROPERTY_NAMES.get(p.args[0]) ?? (p.args[0].startsWith(namespace) ? p.args[0].slice(namespace.length) : null);
|
|
138
|
+
if (name && NAME.test(name) && (PROPERTIES.get(name) ?? namespace + name) === p.args[0]) {
|
|
139
|
+
return write(new Struct(name, [leValue(s, namespace), leValue(o, namespace)]));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return answer;
|
|
143
|
+
});
|
|
144
|
+
return { answers };
|
|
145
|
+
}
|
package/src/{me.js → le.js}
RENAMED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
//
|
|
1
|
+
// Logical English: an explicit grammar, ordinary LS clauses,
|
|
2
2
|
// and conclusions that can be read back as facts. No input text is executed.
|
|
3
3
|
import { PRIMITIVE_KEYS } from './builtins.js';
|
|
4
4
|
import { CONTROL_KEYS, Program, RESERVED_KEYS, Source } from './program.js';
|
|
5
5
|
import { readTerm } from './reader.js';
|
|
6
6
|
import { EMPTY, EyemindError, Struct, Var, conjunction, flattenConjunction } from './terms.js';
|
|
7
7
|
import { write } from './writer.js';
|
|
8
|
+
import { compileOdrlLe, odrlLeReport } from './le-odrl.js';
|
|
8
9
|
|
|
9
10
|
const st = (name, ...args) => new Struct(name, args);
|
|
10
11
|
const VARIABLE = /^[A-Z][0-9]*$/;
|
|
11
12
|
const NAME = /^[a-z][A-Za-z0-9_]*$/;
|
|
12
|
-
const SENTENCE_WORDS = new Set(['every', 'for', 'if', 'find', 'there', 'it', 'contradiction']);
|
|
13
|
+
const SENTENCE_WORDS = new Set(['every', 'for', 'if', 'find', 'there', 'it', 'contradiction', 'use']);
|
|
13
14
|
const RESERVED = new Set([...PRIMITIVE_KEYS, ...CONTROL_KEYS, ...RESERVED_KEYS].map((key) => key.split('/')[0]));
|
|
14
15
|
for (const name of ['true', 'false', 'fail']) RESERVED.add(name);
|
|
15
16
|
const OPERATORS = { '+': ['add', 1], '-': ['sub', 1], '*': ['mul', 2], '/': ['div', 2] };
|
|
@@ -53,7 +54,7 @@ class Parser {
|
|
|
53
54
|
}
|
|
54
55
|
|
|
55
56
|
get token() { return this.tokens[this.at]; }
|
|
56
|
-
error(message, line = this.token?.line ?? 1) { throw new EyemindError(`${this.file}: line ${line}:
|
|
57
|
+
error(message, line = this.token?.line ?? 1) { throw new EyemindError(`${this.file}: line ${line}: Logical English: ${message}`); }
|
|
57
58
|
is(word) { return this.token.kind !== 'string' && this.token.value.toLowerCase() === word; }
|
|
58
59
|
take(word) { if (!this.is(word)) return false; this.at++; return true; }
|
|
59
60
|
need(word) { if (!this.take(word)) this.error(`expected ${JSON.stringify(word)}, found ${JSON.stringify(this.token.raw || 'end of input')}`); }
|
|
@@ -166,6 +167,10 @@ class Parser {
|
|
|
166
167
|
return st('not', this.atomic(false));
|
|
167
168
|
}
|
|
168
169
|
const subject = this.term();
|
|
170
|
+
if (this.take('has')) {
|
|
171
|
+
const name = this.name();
|
|
172
|
+
return st(name, subject, this.term());
|
|
173
|
+
}
|
|
169
174
|
if (this.take('is')) {
|
|
170
175
|
if (this.is('not')) this.error('ordinary "not" is unsupported; use "it is not known that" in a premise for absence, or a separate relation for explicit negative evidence');
|
|
171
176
|
if (!body && this.is('the')) this.error('put calculations in a premise, as in "Y is the value of X + 1", then state the result relation');
|
|
@@ -209,6 +214,20 @@ class Parser {
|
|
|
209
214
|
sentence() {
|
|
210
215
|
const line = this.token.line;
|
|
211
216
|
this.scope = new Map();
|
|
217
|
+
if (this.is('use') && this.tokens[this.at + 1]?.kind === 'word' && this.tokens[this.at + 1].value.toLowerCase() === 'odrl') {
|
|
218
|
+
this.at++;
|
|
219
|
+
this.need('odrl');
|
|
220
|
+
let evaluate = false;
|
|
221
|
+
if (this.take('snapshot')) { this.need('logic'); evaluate = true; }
|
|
222
|
+
else this.need('vocabulary');
|
|
223
|
+
this.need('with'); this.need('namespace');
|
|
224
|
+
if (this.token.kind !== 'string') this.error('an ODRL namespace must be a quoted absolute IRI');
|
|
225
|
+
const namespace = this.token.value;
|
|
226
|
+
try { new URL(namespace); } catch { this.error('an ODRL namespace must be an absolute IRI'); }
|
|
227
|
+
if (/[\s<>"{}|^`\\]/u.test(namespace)) this.error('an ODRL namespace must be an absolute IRI without whitespace or forbidden characters');
|
|
228
|
+
this.at++; this.need('.');
|
|
229
|
+
return { kind: 'odrl', namespace, evaluate, line };
|
|
230
|
+
}
|
|
212
231
|
if (this.take('every')) {
|
|
213
232
|
const from = this.name(); this.need('is');
|
|
214
233
|
const x = new Var('X'), heads = [];
|
|
@@ -271,12 +290,18 @@ function javascript(term) {
|
|
|
271
290
|
return JSON.stringify(term);
|
|
272
291
|
}
|
|
273
292
|
|
|
274
|
-
export function
|
|
293
|
+
export function compileLe(inputs) {
|
|
275
294
|
const clauses = [], statements = [], sources = [];
|
|
295
|
+
let profile = null;
|
|
276
296
|
for (const [text, file] of inputs) {
|
|
277
297
|
const parser = new Parser(String(text), file);
|
|
278
298
|
while (parser.token.kind !== 'end') {
|
|
279
299
|
const sentence = parser.sentence(), { kind, heads, body, line } = sentence;
|
|
300
|
+
if (kind === 'odrl') {
|
|
301
|
+
if (profile || statements.length || inputs.length !== 1) parser.error('an ODRL directive must be the first sentence of one LE file', line);
|
|
302
|
+
profile = { namespace: sentence.namespace, evaluate: sentence.evaluate };
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
280
305
|
statements.push({ ...sentence, file });
|
|
281
306
|
clauses.push(`// ${String(file).replace(/[\r\n\u2028\u2029]/g, ' ')}: line ${line}`);
|
|
282
307
|
if (kind === 'fact') {
|
|
@@ -293,52 +318,56 @@ export function compileMe(inputs) {
|
|
|
293
318
|
}
|
|
294
319
|
}
|
|
295
320
|
}
|
|
296
|
-
|
|
321
|
+
if (profile) return compileOdrlLe(statements, profile, inputs[0][1]);
|
|
322
|
+
return { kind: 'le', text: '// Compiled from Logical English to Logic Script.\n\n' + clauses.join('\n') + '\n',
|
|
297
323
|
program: new Program(sources), prefixes: {}, compiled: { statements } };
|
|
298
324
|
}
|
|
299
325
|
|
|
300
|
-
function
|
|
326
|
+
function leTerm(term, names) {
|
|
301
327
|
if (term instanceof Var) return names.get(term.name);
|
|
302
328
|
if (term === EMPTY) return '[]';
|
|
303
329
|
if (typeof term === 'string') {
|
|
304
330
|
return /^[A-Za-z][A-Za-z0-9_]*$/.test(term) && !VARIABLE.test(term) && !SENTENCE_WORDS.has(term.toLowerCase()) ? term : JSON.stringify(term);
|
|
305
331
|
}
|
|
306
332
|
if (typeof term === 'bigint' || typeof term === 'number') return write(term);
|
|
307
|
-
if (!(term instanceof Struct)) throw new EyemindError('cannot write this term as
|
|
333
|
+
if (!(term instanceof Struct)) throw new EyemindError('cannot write this term as Logical English');
|
|
308
334
|
if (term.name === 'skolem' && term.args.length === 3) {
|
|
309
335
|
const [rule, label, bindings] = term.args, args = listItems(bindings);
|
|
310
336
|
if (typeof rule === 'bigint' && rule > 0n && typeof label === 'string' && args !== null) {
|
|
311
337
|
const name = VARIABLE.test(label) ? label : JSON.stringify(label);
|
|
312
|
-
return `witness(rule ${rule}, ${name}${args.map((arg) => ', ' +
|
|
338
|
+
return `witness(rule ${rule}, ${name}${args.map((arg) => ', ' + leTerm(arg, names)).join('')})`;
|
|
313
339
|
}
|
|
314
340
|
}
|
|
315
341
|
if (term.name === '.') {
|
|
316
342
|
const items = listItems(term);
|
|
317
|
-
if (items !== null) return `[${items.map((item) =>
|
|
343
|
+
if (items !== null) return `[${items.map((item) => leTerm(item, names)).join(', ')}]`;
|
|
318
344
|
// Improper lists remain explicit compounds.
|
|
319
345
|
}
|
|
320
|
-
return `${JSON.stringify(term.name)}(${term.args.map((arg) =>
|
|
346
|
+
return `${JSON.stringify(term.name)}(${term.args.map((arg) => leTerm(arg, names)).join(', ')})`;
|
|
321
347
|
}
|
|
322
348
|
|
|
323
|
-
function
|
|
349
|
+
function leGoal(term, names, properties) {
|
|
324
350
|
if (term === 'false') return 'Contradiction';
|
|
325
|
-
if (typeof term === 'string' && NAME.test(term) && !RESERVED.has(term)) return
|
|
351
|
+
if (typeof term === 'string' && NAME.test(term) && !RESERVED.has(term)) return leTerm(term, names);
|
|
326
352
|
if (!(term instanceof Struct) || !NAME.test(term.name) || RESERVED.has(term.name)) {
|
|
327
|
-
throw new EyemindError(`${write(term)} is not a
|
|
353
|
+
throw new EyemindError(`${write(term)} is not a Logical English relation; use --to eyemind`);
|
|
328
354
|
}
|
|
329
355
|
const article = /^[aeiou]/.test(term.name) ? 'an' : 'a';
|
|
330
|
-
if (term.args.length === 1) return `${
|
|
331
|
-
if (term.args.length === 2) return `${
|
|
332
|
-
|
|
356
|
+
if (term.args.length === 1) return `${leTerm(term.args[0], names)} is ${article} ${term.name}`;
|
|
357
|
+
if (properties && term.args.length === 2) return `${leTerm(term.args[0], names)} has ${term.name} ${leTerm(term.args[1], names)}`;
|
|
358
|
+
if (term.args.length === 2) return `${leTerm(term.args[0], names)} is ${article} ${term.name} of ${leTerm(term.args[1], names)}`;
|
|
359
|
+
return `${term.name}(${term.args.map((arg) => leTerm(arg, names)).join(', ')})`;
|
|
333
360
|
}
|
|
334
361
|
|
|
335
|
-
export function
|
|
362
|
+
export function writeLe(result, { odrl = null } = {}) {
|
|
363
|
+
if (odrl) result = odrlLeReport(result, odrl.namespace);
|
|
336
364
|
const lines = [];
|
|
337
365
|
for (const answer of result.answers) {
|
|
338
366
|
const term = readTerm(answer);
|
|
339
367
|
const names = new Map([...variablesIn([term])].map((name, i) => [name, `X${i}`]));
|
|
340
368
|
const quantifier = names.size ? `For every ${[...names.values()].join(' and ')}, ` : '';
|
|
341
|
-
lines.push(quantifier + flattenConjunction(term).map((goal) =>
|
|
369
|
+
lines.push(quantifier + flattenConjunction(term).map((goal) => leGoal(goal, names, odrl !== null)).join(' and ') + '.');
|
|
342
370
|
}
|
|
343
|
-
|
|
371
|
+
const header = odrl ? `Use ODRL vocabulary with namespace ${JSON.stringify(odrl.namespace)}.\n\n` : '';
|
|
372
|
+
return header + (lines.length ? lines.join('\n') + '\n' : '# No conclusions.\n');
|
|
344
373
|
}
|
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.7.
|
|
2
|
+
export const VERSION = '0.7.1';
|