eyemind 0.8.14 → 0.8.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LS-SPEC.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ```text
4
4
  Title: The Logic Script (LS) Language and Proof Format
5
- Version: eyemind 0.8.14
5
+ Version: eyemind 0.8.16
6
6
  ```
7
7
 
8
8
  This document specifies Logic Script (LS): the syntax and meaning of its
package/README.md CHANGED
@@ -12,8 +12,8 @@ program, a JavaScript library and a browser application.
12
12
 
13
13
  [Playground](https://eyereasoner.github.io/eyemind/playground/) · [Examples](https://eyereasoner.github.io/eyemind/examples/) · [Specification](https://eyereasoner.github.io/eyemind/LS-SPEC) · [Conformance](https://eyereasoner.github.io/eyemind/conformance/)
14
14
 
15
- The examples are 157 runnable programs, each with a card deck and saved
16
- conclusions; all but one streamed example also save proofs and check reports. The specification defines the language,
15
+ The examples are 158 runnable programs, each with a card deck and saved
16
+ conclusions; all but the two streamed examples also save proofs and check reports. The specification defines the language,
17
17
  the proof format and how a proof is checked; its conformance suite has
18
18
  1247 cases that test language and proof behavior, and runs
19
19
  [in your browser](https://eyereasoner.github.io/eyemind/playground/conformance).
@@ -52,32 +52,42 @@ the proof format and how a proof is checked; its conformance suite has
52
52
 
53
53
  ## Why eyemind?
54
54
 
55
- Prolog, Datalog, N3 and SRL already exist, and eyemind adds no new logic: rules run to a least fixpoint, with stratified negation and collection. What it adds is that it does not stop at the answer. Every conclusion can come with a proof that a separate checker verifies against your program, without calling the reasoner.
56
-
57
- 1. **The proof covers everything that went into the answer.**
58
- - Most engines show an answer, or at best a trace you have to trust.
59
- - eyemind proofs include the hard parts:
60
- - "nothing matched" (negation and collections), decided from complete answer tables whose listings of derived facts must be closed;
61
- - what a rule says exists, as the Skolem term of that rule firing;
62
- - whether each answer really answers the question asked, and whether every answer of the question is there.
63
- - A forged proof must never pass the check, so a result is something you can hand to someone who does not trust your engine.
64
- 2. **Logic Script programs are JavaScript scripts.**
65
- - There is no syntax to learn, no parser to install and no dependency.
66
- - The same program runs on the command line, as a library and in a browser tab.
67
- - Conclusions and proofs are data that is read back into terms without being run.
68
- 3. **Forward and backward reasoning in one program.**
69
- - Classic Datalog is forward-only and has no function terms; Prolog is backward-only.
70
- - eyemind does both: forward rounds compute what follows, backward search answers questions, the two call each other, and what a rule says exists becomes a Skolem term.
71
- 4. **A specification written for other implementers.**
72
- - The specification and its black-box [conformance suite](https://eyereasoner.github.io/eyemind/conformance/) let someone else build an independent LS implementation and test it.
73
- - Agreement between independent implementations is what makes a proof worth more than "trust our code".
74
- 5. **Exact numbers.** Integers are BigInts and are never silently turned into floats.
75
-
76
- When eyemind is not the right tool:
77
-
78
- - If you do not need proofs, use the mature tools: SWI-Prolog and XSB have tabling, and Soufflé is fast at scale.
79
- - N3 and SRL are designed around RDF and Web data. eyemind does not compete there: it reads them, and compiles them to LS (see [Other formats](#other-formats)).
80
- - eyemind is for cases where someone else has to be able to check why a conclusion holds: audits, compliance, and decisions between parties that do not trust each other.
55
+ eyemind adds no new logic: as in Datalog, Prolog, N3 and SRL, rules run to a
56
+ least fixpoint, with stratified negation and collection. What it adds is
57
+ evidence. Every conclusion can come with a proof that a separate checker
58
+ verifies against your program, without running the reasoner.
59
+
60
+ 1. **Proofs that cover the whole answer.** Most engines give an answer, or a
61
+ trace you have to trust. An eyemind proof also justifies the hard parts:
62
+ - that nothing matched, for negation and collections, from complete answer
63
+ tables whose listings of derived facts must be closed;
64
+ - what a rule says exists, as the Skolem term of its firing;
65
+ - that each answer answers the question asked, and that none is missing.
66
+
67
+ A forged proof must never pass the check, so you can hand a result to
68
+ someone who does not trust your engine.
69
+ 2. **Programs are JavaScript.** No syntax to learn, no parser to install, no
70
+ dependencies. One program runs on the command line, as a library and in a
71
+ browser tab; conclusions and proofs are data, read back without being run.
72
+ 3. **Forward and backward reasoning together.** Datalog is forward-only and
73
+ has no function terms; Prolog is backward-only. In eyemind, forward rounds
74
+ derive what follows, backward search answers questions, and each calls the
75
+ other.
76
+ 4. **Your formats, the same proofs.** RDF 1.2, N3, SRL, Logical English and
77
+ the OWL 2 RL, SKOS, QUDT and SHACL profiles compile to LS, so their
78
+ conclusions get the same checkable proofs.
79
+ 5. **Written for other implementers.** The specification and its black-box
80
+ [conformance suite](https://eyereasoner.github.io/eyemind/conformance/) let
81
+ others build an independent LS implementation. Agreement between
82
+ implementations is what makes a proof worth more than "trust our code".
83
+ 6. **Exact numbers.** Integers are BigInts and never silently become floats.
84
+
85
+ **When to use something else.** If you do not need proofs, mature tools are
86
+ faster: SWI-Prolog and XSB have tabling, and Soufflé scales. N3 and SRL are
87
+ built for Web data; eyemind reads and compiles them rather than competing
88
+ (see [Other formats](#other-formats)). eyemind is for conclusions someone else
89
+ must be able to check: audits, compliance, and decisions between parties that
90
+ do not trust each other.
81
91
 
82
92
  ## Install and run
83
93
 
@@ -387,7 +397,14 @@ query(rdf(O, iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), sosa('Obser
387
397
  [MobiBench](https://github.com/william-vw/mobibench) OWL 2 RL suite.
388
398
  - `--qudt` normalizes QUDT quantity values and CDT literals to SI units from
389
399
  an offline catalog adapted from [QUDT.org](https://qudt.org/) (CC BY 4.0):
390
- `"36 km/h"^^cdt:speed` yields `"10.0 m/s"^^cdt:speed`.
400
+ `"36 km/h"^^cdt:speed` yields `"10.0 m/s"^^cdt:speed`. Input metadata adds
401
+ units: with `qudt:conversionMultiplier` and `qudt:hasDimensionVector`,
402
+ composed from QUDT units with `qudt:hasFactorUnit` or `qudt:prefix` and
403
+ `qudt:scalingOf`, or aligned with `owl:sameAs` or `skos:exactMatch`; QUDT
404
+ names such as `unit:NanoGM` resolve as a prefix and a catalog unit. A
405
+ dimension without a CDT datatype normalizes to its coherent SI unit as a
406
+ `cdt:ucum` literal, and a quantity whose unit cannot be resolved, such as a
407
+ currency, is reported with `qconv:unresolvedUnit`.
391
408
  - `--skos` adds positive [SKOS Core](https://www.w3.org/TR/skos-reference/)
392
409
  entailments, without integrity checks or SKOS-XL.
393
410
  - `--shacl` compiles shapes to LS and writes a standard `sh:ValidationReport`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eyemind",
3
- "version": "0.8.14",
3
+ "version": "0.8.16",
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/qudt.js CHANGED
@@ -1,6 +1,8 @@
1
1
  // Offline QUDT/CDT normalization. Metadata facts are a small, explicit selection
2
2
  // from QUDT.org (CC BY 4.0), https://qudt.org/vocab/unit/ . Input metadata can
3
3
  // extend or override it; there is no ontology download during compilation.
4
+ import { write } from './writer.js';
5
+
4
6
  const Q = 'http://qudt.org/schema/qudt/';
5
7
  const U = 'http://qudt.org/vocab/unit/';
6
8
  const C = 'https://eyereasoner.github.io/eyemind/qudt#';
@@ -67,7 +69,7 @@ for (const [name, base, exponent, code] of [['B', 10, 1, 'B'], ['DeciB', 10, 0.1
67
69
  definitions.push(`fact(qu_builtin_token(${text(code)}, ${iri(U + name)}));`);
68
70
  }
69
71
 
70
- export const qudtRules = '\n' + definitions.join('\n') + String.raw`
72
+ const qudtCatalog = '\n' + definitions.join('\n') + String.raw`
71
73
  const qu = (n) => iri('http://qudt.org/schema/qudt/' + n);
72
74
  const qc = (n) => iri('https://eyereasoner.github.io/eyemind/qudt#' + n);
73
75
  const quDouble = 'http://www.w3.org/2001/XMLSchema#double';
@@ -119,4 +121,125 @@ implies([qu_input(S, P, Q, Kind, U, V, T, Code, DT, G), qu_ratio(V, U, G, R), qu
119
121
  rdf(B, iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), qu('QuantityValue'), G),
120
122
  rdf(B, qu('numericValue'), Number, G), rdf(B, qu('unit'), T, G), rdf(B, qc('sourceProperty'), P, G), rdf(B, Kind, Q, G), rdf(B, qc('sourceUnit'), U, G)
121
123
  ]);
124
+ // Units composed in the input from factor units or a prefix (qudtUnits).
125
+ implied_by(qu_unit(U, G, D, M, float(0)), [qu_derived(U, G, D, M), not(qu_custom(U, G))]);
126
+ // A quantity whose unit is not known, composed or aligned is reported.
127
+ implied_by(qu_resolved(L, G), qu_align(L, _, G));
128
+ implies([ent_base(S, P, Q, G), rdf(Q, iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), qu('QuantityValue'), G), rdf(Q, qu('unit'), L, G), not(qu_resolved(L, G))], ent_report(Q, qc('unresolvedUnit'), L, G));
122
129
  `;
130
+
131
+ // The dimension exponents of a QUDT dimension vector, in its order.
132
+ const BASES = ['A', 'E', 'L', 'I', 'M', 'H', 'T'];
133
+ const vector = (name) => {
134
+ const match = /^A(-?[0-9.]+)E(-?[0-9.]+)L(-?[0-9.]+)I(-?[0-9.]+)M(-?[0-9.]+)H(-?[0-9.]+)T(-?[0-9.]+)D[01]$/.exec(name);
135
+ return match ? match.slice(1).map(Number) : null;
136
+ };
137
+ const vectorName = (exponents) => BASES.map((base, i) => base + (exponents[i] + 0)).join('') + (exponents.every((e) => e === 0) ? 'D1' : 'D0');
138
+ // The coherent SI unit of a dimension, in UCUM: kg, m, s, A, K, mol and cd.
139
+ const UCUM = [['M', 'kg'], ['L', 'm'], ['T', 's'], ['E', 'A'], ['H', 'K'], ['A', 'mol'], ['I', 'cd']];
140
+ // Positive exponents come first, as in cd.m-2.
141
+ const ucumOf = (exponents) => {
142
+ const factors = UCUM.map(([base, code]) => [code, exponents[BASES.indexOf(base)]]).filter(([, e]) => e !== 0);
143
+ return [...factors.filter(([, e]) => e > 0), ...factors.filter(([, e]) => e < 0)]
144
+ .map(([code, e]) => code + (e === 1 ? '' : e)).join('.') || '1';
145
+ };
146
+ const PREFIXES = { Quecto: 1e-30, Ronto: 1e-27, Yocto: 1e-24, Zepto: 1e-21, Atto: 1e-18, Femto: 1e-15, Pico: 1e-12, Nano: 1e-9,
147
+ Micro: 1e-6, Milli: 1e-3, Centi: 1e-2, Deci: 1e-1, Deca: 1e1, Hecto: 1e2, Kilo: 1e3, Mega: 1e6, Giga: 1e9, Tera: 1e12,
148
+ Peta: 1e15, Exa: 1e18, Zetta: 1e21, Yotta: 1e24, Ronna: 1e27, Quetta: 1e30 };
149
+ // A product of multipliers without the rounding error of the
150
+ // multiplications when it is a short decimal, as 1e-12 for a nanogram.
151
+ const exact = (n) => {
152
+ const rounded = Number(n.toPrecision(15));
153
+ return rounded.toExponential().replace(/e.*$/, '').replace(/[-.]/g, '').length <= 12 ? rounded : n;
154
+ };
155
+ // A unit IRI named by an SI prefix and a catalog unit without an offset.
156
+ const prefixed = (id) => {
157
+ if (!id.startsWith(U)) return null;
158
+ const name = id.slice(U.length);
159
+ for (const [prefix, scale] of Object.entries(PREFIXES)) {
160
+ const base = builtin.get(U + name.slice(prefix.length));
161
+ if (name.startsWith(prefix) && base && base.offset === 0) return [scale, base];
162
+ }
163
+ return null;
164
+ };
165
+ const builtin = new Map(units.map(([name, dim, mult, offset]) => [U + name, { exponents: vector(dimensions[dim]), mult, offset }]));
166
+ const profiled = new Set(targets.map(([dim]) => dimensions[dim]));
167
+
168
+ // Facts for the units of these quads that QUDT composes from other units:
169
+ // qudt:hasFactorUnit with qudt:hasUnit and qudt:exponent, or qudt:prefix
170
+ // with qudt:scalingOf. Their multiplier is the product of their factors'
171
+ // multipliers, each to its exponent, and their dimension the sum of their
172
+ // factors' dimensions times it; a factor with an offset cannot compose. A
173
+ // unit of a dimension without a target gets the dimension's coherent SI unit.
174
+ export function qudtUnits(quads) {
175
+ const about = new Map();
176
+ for (const quad of quads) {
177
+ if (quad[0]?.name !== 'iri' && quad[0]?.name !== 'bnode') continue;
178
+ const id = write(quad[0]);
179
+ if (!about.has(id)) about.set(id, []);
180
+ about.get(id).push(quad);
181
+ }
182
+ const values = (node, property) => (about.get(write(node)) ?? []).filter((q) => q[1].args?.[0] === Q + property).map((q) => q[2]);
183
+ const numberOf = (term) => typeof term === 'bigint' ? Number(term)
184
+ : term?.name === 'literal' && /^[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/.test(term.args[0]) ? Number(term.args[0]) : null;
185
+ const known = new Map(), derived = [], dims = new Set();
186
+ // The graph of a unit's metadata.
187
+ const graphOf = (unit) => write(about.get(write(unit))[0][3]);
188
+ const resolve = (unit, seen = new Set()) => {
189
+ if (unit?.name !== 'iri') return null;
190
+ const id = unit.args[0];
191
+ if (known.has(id)) return known.get(id);
192
+ if (seen.has(id)) return null;
193
+ seen.add(id);
194
+ let found = null;
195
+ const multiplier = numberOf(values(unit, 'conversionMultiplier')[0]);
196
+ const dimension = values(unit, 'hasDimensionVector')[0]?.args?.[0]?.slice(D.length);
197
+ if (multiplier !== null && dimension && vector(dimension)) {
198
+ found = { exponents: vector(dimension), mult: multiplier, offset: numberOf(values(unit, 'conversionOffset')[0]) ?? 0 };
199
+ dims.add(dimension);
200
+ } else if (builtin.has(id)) {
201
+ found = builtin.get(id);
202
+ } else if (prefixed(id)) {
203
+ // QUDT names a prefixed unit by its prefix and unit, as NanoGM.
204
+ const [scale, base] = prefixed(id);
205
+ found = { ...base, mult: exact(scale * base.mult) };
206
+ derived.push(`fact(qu_builtin(${iri(id)}, ${iri(D + vectorName(found.exponents))}, ${number(found.mult)}, ${number(0)}));`);
207
+ dims.add(vectorName(found.exponents));
208
+ } else {
209
+ const factors = values(unit, 'hasFactorUnit');
210
+ const prefix = values(unit, 'prefix')[0], base = values(unit, 'scalingOf')[0];
211
+ let parts = null;
212
+ if (factors.length) parts = factors.map((f) => [values(f, 'hasUnit')[0], numberOf(values(f, 'exponent')[0])]);
213
+ else if (prefix && base) {
214
+ const scale = numberOf(values(prefix, 'prefixMultiplier')[0]) ?? PREFIXES[prefix.args?.[0]?.slice('http://qudt.org/vocab/prefix/'.length)];
215
+ if (scale !== undefined && scale !== null) parts = [[base, 1, scale]];
216
+ }
217
+ if (parts && parts.every(([u, e]) => u && e !== null)) {
218
+ const resolved = parts.map(([u, e, scale = 1]) => [resolve(u, seen), e, scale]);
219
+ if (resolved.every(([r]) => r && r.offset === 0)) {
220
+ const exponents = BASES.map((_, i) => resolved.reduce((sum, [r, e]) => sum + r.exponents[i] * e, 0));
221
+ const mult = exact(resolved.reduce((product, [r, e, scale]) => product * scale * r.mult ** e, 1));
222
+ found = { exponents, mult, offset: 0 };
223
+ derived.push(`fact(qu_derived(${iri(id)}, ${graphOf(unit)}, ${iri(D + vectorName(exponents))}, ${number(mult)}));`);
224
+ dims.add(vectorName(exponents));
225
+ }
226
+ }
227
+ }
228
+ known.set(id, found);
229
+ return found;
230
+ };
231
+ for (const id of about.keys()) {
232
+ const subject = about.get(id)[0][0];
233
+ if (['hasFactorUnit', 'scalingOf', 'hasDimensionVector'].some((p) => values(subject, p).length)) resolve(subject);
234
+ }
235
+ for (const quad of quads) if (quad[1].args?.[0] === Q + 'unit') resolve(quad[2]);
236
+ const profiles = [...dims].filter((dim) => !profiled.has(dim)).map((dim) => {
237
+ const code = ucumOf(vector(dim)), unit = C + 'unit-' + code;
238
+ return `fact(qu_builtin(${iri(unit)}, ${iri(D + dim)}, ${number(1)}, ${number(0)}));\n` +
239
+ `fact(qu_builtin_token(${text(code)}, ${iri(unit)}));\n` +
240
+ `fact(qu_builtin_profile(${iri(C + 'Profile-' + code)}, ${text('https://w3id.org/cdt/ucum')}, ${iri(unit)}, ${text(code)}));`;
241
+ });
242
+ return [...derived, ...profiles].map((line) => line + '\n').join('');
243
+ }
244
+
245
+ export const qudtRules = (quads) => qudtCatalog + qudtUnits(quads);
package/src/rdf-logic.js CHANGED
@@ -35,7 +35,7 @@ export function compileRdfLogic({ quads, prefixes, shapes = quads, wanted = null
35
35
  (execution ? `implied_by(srl_execution(${JSON.stringify(execution.now)}, ${JSON.stringify(execution.seed)}), 'true');\n` : '') +
36
36
  (selected.has('owl2rl') ? owl2rlRules : '') +
37
37
  (selected.has('skos') ? skosRules : '') +
38
- (selected.has('qudt') ? qudtRules : '') +
38
+ (selected.has('qudt') ? qudtRules(quads) : '') +
39
39
  shacl + (evaluate ? "query(sh_evaluation(R));\n" : REPORTS + (stream ? '' : wanted === null ? RESULT : contracted(wanted)));
40
40
  return { kind: profiles[0], text, program: loadText(text, source),
41
41
  prefixes: { ...prefixes, ent: ENTAILMENT, ...(selected.has('skos') ? { skos: 'http://www.w3.org/2004/02/skos/core#' } : {}),
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.8.14';
2
+ export const VERSION = '0.8.16';