eyemind 0.5.12 → 0.5.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -5
- package/SPEC.md +84 -4
- package/package.json +1 -1
- package/src/builtins.js +30 -0
- package/src/cli.js +6 -3
- package/src/engine.js +12 -3
- package/src/formats.js +4 -4
- package/src/lang.js +4 -3
- package/src/n3-builtins.js +21 -0
- package/src/n3-parser.js +59 -13
- package/src/n3.js +234 -22
- package/src/native-proof.js +6 -4
- package/src/program.js +49 -7
- package/src/proof.js +1 -1
- package/src/srl-decimal.js +66 -0
- package/src/srl-functions.js +67 -54
- package/src/srl-parser.js +9 -7
- package/src/srl.js +70 -13
- package/src/version.js +1 -1
package/src/program.js
CHANGED
|
@@ -4,8 +4,8 @@ import { containsTemplate, expandTemplates } from './templates.js';
|
|
|
4
4
|
import { subgoals } from './goals.js';
|
|
5
5
|
import { hasInspection, inspectionTerm } from './inspection.js';
|
|
6
6
|
import {
|
|
7
|
-
EMPTY, Env, EyemindError, Struct, Var, deref, flattenConjunction, freshTerm, identity, indexKey, isCallable,
|
|
8
|
-
isGround, isTerm, key, keyArity, unify, variables,
|
|
7
|
+
EMPTY, Env, EyemindError, Struct, Var, conjunction, deref, flattenConjunction, freshTerm, identity, indexKey, isCallable,
|
|
8
|
+
isGround, isTerm, key, keyArity, listFromItems, unify, variables,
|
|
9
9
|
} from './terms.js';
|
|
10
10
|
import { write } from './writer.js';
|
|
11
11
|
|
|
@@ -13,7 +13,7 @@ export const CONTROL_KEYS = new Set(['and/2', 'or/2', 'not/1', 'call/1', 'once/1
|
|
|
13
13
|
export const RESERVED_KEYS = new Set(['step/4', 'clause/2', 'searched/2', 'derived/2', 'called/2', 'activated/2', 'implies/2', 'implied_by/2', 'forward/2', 'backward/2']);
|
|
14
14
|
|
|
15
15
|
// The statements that state a forward rule, and how an error names them.
|
|
16
|
-
const FORWARD_KINDS = { forward: 'a forward rule', query: 'query()', contradiction: 'contradiction()' };
|
|
16
|
+
const FORWARD_KINDS = { forward: 'a forward rule', restricted: 'restricted()', query: 'query()', contradiction: 'contradiction()' };
|
|
17
17
|
|
|
18
18
|
export class Clause {
|
|
19
19
|
constructor(id, head, heads, body, forward, line, captures = null) {
|
|
@@ -26,6 +26,9 @@ export class Clause {
|
|
|
26
26
|
this.line = line;
|
|
27
27
|
this.captures = captures;
|
|
28
28
|
this.parentId = null;
|
|
29
|
+
this.restricted = false;
|
|
30
|
+
this.guard = null;
|
|
31
|
+
this.statedBody = body;
|
|
29
32
|
this.prefixLength = 0;
|
|
30
33
|
this.localBody = body;
|
|
31
34
|
this.templates = [];
|
|
@@ -50,7 +53,8 @@ export class Clause {
|
|
|
50
53
|
}
|
|
51
54
|
|
|
52
55
|
// One clause as a program states it, before validation. kind is 'fact',
|
|
53
|
-
// 'backward', 'forward', 'query' or 'contradiction'; body is a
|
|
56
|
+
// 'backward', 'forward', 'restricted', 'query' or 'contradiction'; body is a
|
|
57
|
+
// list of goals.
|
|
54
58
|
export class Source {
|
|
55
59
|
constructor(kind, head, body, line = null, file = null) {
|
|
56
60
|
this.kind = kind;
|
|
@@ -116,9 +120,9 @@ export class Program {
|
|
|
116
120
|
}
|
|
117
121
|
if (source.kind === 'fact' && body.length) throw new EyemindError(`line ${source.line}: a fact has no body`);
|
|
118
122
|
const heads = forward ? flattenConjunction(source.head) : [source.head];
|
|
119
|
-
if (source.kind === 'forward' && heads.some((item) => item === 'true' || item === 'false')) {
|
|
123
|
+
if ((source.kind === 'forward' || source.kind === 'restricted') && heads.some((item) => item === 'true' || item === 'false')) {
|
|
120
124
|
// query() and contradiction() state these, and say so.
|
|
121
|
-
throw new EyemindError(`line ${source.line}: implies() cannot conclude 'true' or 'false'; ` +
|
|
125
|
+
throw new EyemindError(`line ${source.line}: ${source.kind === 'forward' ? 'implies' : 'restricted'}() cannot conclude 'true' or 'false'; ` +
|
|
122
126
|
'write query(Premise) or contradiction(Premise)');
|
|
123
127
|
}
|
|
124
128
|
for (const goal of body) {
|
|
@@ -148,8 +152,15 @@ export class Program {
|
|
|
148
152
|
}
|
|
149
153
|
allowed.add(itemKey);
|
|
150
154
|
}
|
|
155
|
+
const stated = body.length;
|
|
156
|
+
const guard = source.kind === 'restricted' ? restriction(ordinal, source.head, heads, body) : null;
|
|
157
|
+
if (guard !== null) body.push(guard);
|
|
151
158
|
const clause = new Clause(ordinal, source.head, forward ? heads : null, body, forward, source.line, source.captures ?? null);
|
|
159
|
+
clause.guard = guard;
|
|
160
|
+
clause.statedBody = guard === null ? body : body.slice(0, stated);
|
|
152
161
|
clause.parentId = source.parentId ?? null;
|
|
162
|
+
// A restricted rule does not fire where its conclusion already holds.
|
|
163
|
+
clause.restricted = source.kind === 'restricted';
|
|
153
164
|
clause.prefixLength = source.prefixLength ?? 0;
|
|
154
165
|
clause.localBody = clause.prefixLength ? body.slice(clause.prefixLength) : body;
|
|
155
166
|
clause.templates = source.templates || [];
|
|
@@ -316,6 +327,34 @@ function dependencies(goal, closed = false, out = []) {
|
|
|
316
327
|
return out;
|
|
317
328
|
}
|
|
318
329
|
|
|
330
|
+
// The guard of a restricted rule with witnesses, or null:
|
|
331
|
+
// not([Heads', not_unify(Witnesses', Skolems)]), where Heads' are its heads
|
|
332
|
+
// with each witness an anonymous variable of the guard and Skolems the terms
|
|
333
|
+
// a firing gives them (SPEC Section 8.2). It holds when nothing but the
|
|
334
|
+
// firing's own witnesses satisfies the heads: before the firing, when no
|
|
335
|
+
// known values do; after it, when the witnesses are needed.
|
|
336
|
+
function restriction(ordinal, head, heads, body) {
|
|
337
|
+
const inBody = new Set();
|
|
338
|
+
for (const goal of body) for (const name of variables(goal).keys()) inBody.add(name);
|
|
339
|
+
const all = variables(head);
|
|
340
|
+
const witnesses = [...all.values()].filter((variable) => !inBody.has(variable.name));
|
|
341
|
+
if (!witnesses.length) return null;
|
|
342
|
+
const used = new Set(all.keys());
|
|
343
|
+
for (const name of inBody) used.add(name);
|
|
344
|
+
let next = 0;
|
|
345
|
+
const anonymous = new Map(witnesses.map((variable) => {
|
|
346
|
+
while (used.has(`_${next}`)) next++;
|
|
347
|
+
used.add(`_${next}`);
|
|
348
|
+
return [variable.name, new Var(`_${next}`)];
|
|
349
|
+
}));
|
|
350
|
+
const rename = (term) => (term instanceof Var ? anonymous.get(term.name) ?? term
|
|
351
|
+
: term instanceof Struct ? new Struct(term.name, term.args.map(rename)) : term);
|
|
352
|
+
const frontier = listFromItems([...all.values()].filter((variable) => !anonymous.has(variable.name)));
|
|
353
|
+
const skolems = witnesses.map((variable) => new Struct('skolem', [BigInt(ordinal), variable.name, frontier]));
|
|
354
|
+
const other = new Struct('not_unify', [listFromItems([...anonymous.values()]), listFromItems(skolems)]);
|
|
355
|
+
return new Struct('not', [conjunction([...heads.map(rename), other])]);
|
|
356
|
+
}
|
|
357
|
+
|
|
319
358
|
// Whether a goal could contribute a closed dependency or a dynamic call.
|
|
320
359
|
function mayNeedStratifying(goal) {
|
|
321
360
|
if (goal instanceof Var || isTerm(goal, 'not', 1) || isTerm(goal, 'findall', 3)) return true;
|
|
@@ -372,7 +411,10 @@ function stratify(clauses, stratifying) {
|
|
|
372
411
|
outgoing.set(clause.id, [edge]);
|
|
373
412
|
}
|
|
374
413
|
for (const goal of clause.localBody) {
|
|
375
|
-
|
|
414
|
+
// A restricted rule's guard reads its heads once the rules deriving
|
|
415
|
+
// them have run: it depends on them as on a premise.
|
|
416
|
+
for (const [depGoal, inside] of dependencies(goal)) {
|
|
417
|
+
const closed = inside && goal !== clause.guard;
|
|
376
418
|
const named = !(depGoal instanceof Var);
|
|
377
419
|
if (!named) dynamic.add(clause.id);
|
|
378
420
|
const candidates = named ? headCandidates(index.get(key(depGoal)), depGoal) : everyHead;
|
package/src/proof.js
CHANGED
|
@@ -45,7 +45,7 @@ export function clauseDisplay(clause) {
|
|
|
45
45
|
if (clause.head === 'true') return new Struct('query', clause.body);
|
|
46
46
|
if (clause.head === 'false') return new Struct('contradiction', clause.body);
|
|
47
47
|
const heads = flattenConjunction(clause.originalHead ?? clause.head);
|
|
48
|
-
return new Struct('implies', [goalsDisplay(clause.body), goalsDisplay(clause.templates.length ? heads.map(templateDisplay) : heads)]);
|
|
48
|
+
return new Struct(clause.restricted ? 'restricted' : 'implies', [goalsDisplay(clause.statedBody ?? clause.body), goalsDisplay(clause.templates.length ? heads.map(templateDisplay) : heads)]);
|
|
49
49
|
}
|
|
50
50
|
if (clause.body.length) return new Struct('implied_by', [clause.head, goalsDisplay(clause.body)]);
|
|
51
51
|
return new Struct('fact', [clause.head]);
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// Exact decimal values as decimal(Coefficient, Scale), meaning C / 10^S.
|
|
2
|
+
// These are ordinary rules: the proof checker re-decides their integer
|
|
3
|
+
// arithmetic without a decimal implementation or calls to the reasoner.
|
|
4
|
+
export const decimalRules = String.raw`
|
|
5
|
+
implied_by(srl_decimal_text(L, decimal(C, S)), [regex_find('^[ \\t\\n\\r]*([+-]?)([0-9]*)(?:\\.([0-9]*))?[ \\t\\n\\r]*$', L, [_, Sign, I, F]), atom_concat(I, F, Ds), atom_length(Ds, N), gt(N, 0), atom_chars(Ds, Cs), number_chars(A, Cs), atom_length(F, S), srl_decimal_sign(Sign, A, C)])
|
|
6
|
+
implied_by(srl_decimal_sign('', A, A), 'true')
|
|
7
|
+
implied_by(srl_decimal_sign('+', A, A), 'true')
|
|
8
|
+
implied_by(srl_decimal_sign('-', A, C), is(C, neg(A)))
|
|
9
|
+
implied_by(srl_decimal_normal(decimal(C, S), decimal(N, T)), once(srl_decimal_normal_(C, S, N, T)))
|
|
10
|
+
implied_by(srl_decimal_normal_(0, _, 0, 0), 'true')
|
|
11
|
+
implied_by(srl_decimal_normal_(C, S, N, T), [ne(C, 0), gt(S, 0), eq(mod(C, 10), 0), is(A, floordiv(C, 10)), is(B, sub(S, 1)), srl_decimal_normal_(A, B, N, T)])
|
|
12
|
+
implied_by(srl_decimal_normal_(C, S, C, S), or(eq(S, 0), ne(mod(C, 10), 0)))
|
|
13
|
+
implied_by(srl_decimal_lexical(V, L), [srl_decimal_normal(V, decimal(C, S)), is(A, abs(C)), number_chars(A, Cs), atom_chars(Ds, Cs), atom_length(Ds, N), is(P, sub(N, S)), srl_point(Ds, N, P, B), srl_decimal_prefix(C, Sign), atom_concat(Sign, B, L)])
|
|
14
|
+
implied_by(srl_decimal_prefix(C, '-'), lt(C, 0))
|
|
15
|
+
implied_by(srl_decimal_prefix(C, ''), ge(C, 0))
|
|
16
|
+
implied_by(srl_decimal_value(N, decimal(N, 0)), is_int(N))
|
|
17
|
+
implied_by(srl_decimal_value(decimal(C, S), decimal(C, S)), 'true')
|
|
18
|
+
implied_by(srl_decimal_value(F, D), [is_float(F), number_chars(F, Cs), atom_chars(A, Cs), srl_float_text('decimal', A, L), srl_decimal_text(L, D)])
|
|
19
|
+
implied_by(srl_float_value(N, F), [srl_float_operand(N), float64('convert', N, 0, F)])
|
|
20
|
+
implied_by(srl_float_operand(N), is_number(N))
|
|
21
|
+
implied_by(srl_float_operand(N), srl_float_special(N))
|
|
22
|
+
fact(srl_float_special('INF'), srl_float_special('-INF'), srl_float_special('NaN'))
|
|
23
|
+
implied_by(srl_float_lexical(N, L), [is_float(N), number_chars(N, Cs), atom_chars(L, Cs)])
|
|
24
|
+
implied_by(srl_float_lexical(N, N), srl_float_special(N))
|
|
25
|
+
implied_by(srl_float_value(D, F), [unify(D, decimal(_, _)), srl_decimal_lexical(D, L), float64('parse', L, 0, F)])
|
|
26
|
+
implied_by(srl_numeric_value('integer', N, N), is_int(N))
|
|
27
|
+
implied_by(srl_numeric_value('decimal', V, D), srl_decimal_value(V, D))
|
|
28
|
+
implied_by(srl_numeric_value(K, V, F), [srl_float_kind(K), srl_float_value(V, F)])
|
|
29
|
+
implied_by(srl_pair(A, B, K, X, Y), [srl_num(A, KA, VA), srl_num(B, KB, VB), srl_promote(KA, KB, K), srl_numeric_value(K, VA, X), srl_numeric_value(K, VB, Y)])
|
|
30
|
+
implied_by(srl_decimal_align(decimal(A, S), decimal(B, T), X, Y, U), [is(U, max(S, T)), is(X, mul(A, pow(10, sub(U, S)))), is(Y, mul(B, pow(10, sub(U, T))))])
|
|
31
|
+
implied_by(srl_calculate('add', decimal(A, S), decimal(B, T), decimal(C, U)), [srl_decimal_align(decimal(A, S), decimal(B, T), X, Y, U), is(C, add(X, Y))])
|
|
32
|
+
implied_by(srl_calculate('sub', decimal(A, S), decimal(B, T), decimal(C, U)), [srl_decimal_align(decimal(A, S), decimal(B, T), X, Y, U), is(C, sub(X, Y))])
|
|
33
|
+
implied_by(srl_calculate('mul', decimal(A, S), decimal(B, T), decimal(C, U)), [is(C, mul(A, B)), is(U, add(S, T))])
|
|
34
|
+
// Reduce a quotient first. Terminating division is exact; otherwise keep
|
|
35
|
+
// at least 34 significant digits, rounded to nearest with ties to even.
|
|
36
|
+
implied_by(srl_calculate('div', decimal(A, S), decimal(B, T), decimal(C, U)), [ne(B, 0), is(N, mul(A, pow(10, T))), is(D, mul(B, pow(10, S))), is(G, gcd(N, D)), is(P, floordiv(abs(N), G)), is(Q, floordiv(abs(D), G)), srl_decimal_places(Q, P, U), is(Z, mul(P, pow(10, U))), is(H, floordiv(Z, Q)), is(R, mod(Z, Q)), srl_decimal_round(H, R, Q, V), srl_decimal_quotient_sign(N, D, V, C)])
|
|
37
|
+
implied_by(srl_decimal_places(Q, P, U), [srl_decimal_factors(Q, 2, 0, A, I), srl_decimal_factors(A, 5, 0, B, J), srl_decimal_precision(B, P, Q, I, J, U)])
|
|
38
|
+
implied_by(srl_decimal_factors(N, D, I, R, K), once(srl_decimal_factors_(N, D, I, R, K)))
|
|
39
|
+
implied_by(srl_decimal_factors_(N, D, I, R, K), [eq(mod(N, D), 0), is(M, floordiv(N, D)), is(J, add(I, 1)), srl_decimal_factors_(M, D, J, R, K)])
|
|
40
|
+
implied_by(srl_decimal_factors_(N, D, I, N, I), ne(mod(N, D), 0))
|
|
41
|
+
implied_by(srl_decimal_precision(1, _, _, I, J, U), is(U, max(I, J)))
|
|
42
|
+
implied_by(srl_decimal_precision(B, P, Q, _, _, U), [ne(B, 1), number_chars(P, Ps), number_chars(Q, Qs), srl_length(Ps, NP), srl_length(Qs, NQ), is(U, max(0, add(34, sub(NQ, NP))))])
|
|
43
|
+
implied_by(srl_decimal_round(H, R, Q, V), [is(D, sub(mul(R, 2), Q)), srl_decimal_round_(D, H, V)])
|
|
44
|
+
implied_by(srl_decimal_round_(D, H, H), lt(D, 0))
|
|
45
|
+
implied_by(srl_decimal_round_(D, H, V), [gt(D, 0), is(V, add(H, 1))])
|
|
46
|
+
implied_by(srl_decimal_round_(0, H, V), is(V, add(H, mod(H, 2))))
|
|
47
|
+
implied_by(srl_decimal_quotient_sign(N, D, V, C), [lt(mul(N, D), 0), is(C, neg(V))])
|
|
48
|
+
implied_by(srl_decimal_quotient_sign(N, D, V, V), ge(mul(N, D), 0))
|
|
49
|
+
implied_by(srl_value_compare(decimal(A, S), decimal(B, T), O), [srl_decimal_align(decimal(A, S), decimal(B, T), X, Y, _), srl_compare(X, Y, O)])
|
|
50
|
+
implied_by(srl_value_compare(X, Y, O), [is_int(X), is_int(Y), srl_compare(X, Y, O)])
|
|
51
|
+
implied_by(srl_value_compare(X, Y, O), [srl_float_operand(X), srl_float_operand(Y), not(and(is_int(X), is_int(Y))), float64('compare', X, Y, O)])
|
|
52
|
+
implied_by(srl_value_zero(decimal(C, _), C), 'true')
|
|
53
|
+
implied_by(srl_value_zero(N, N), is_number(N))
|
|
54
|
+
fact(srl_value_zero('NaN', 0), srl_value_zero('INF', 1), srl_value_zero('-INF', 1))
|
|
55
|
+
implied_by(srl_unary('neg', decimal(C, S), decimal(N, S)), is(N, neg(C)))
|
|
56
|
+
implied_by(srl_unary('abs', decimal(C, S), decimal(N, S)), is(N, abs(C)))
|
|
57
|
+
implied_by(srl_unary('floor', decimal(C, S), N), is(N, floordiv(C, pow(10, S))))
|
|
58
|
+
implied_by(srl_unary('ceil', decimal(C, S), N), is(N, neg(floordiv(neg(C), pow(10, S)))))
|
|
59
|
+
implied_by(srl_unary('trunc', decimal(C, S), N), [is(A, floordiv(abs(C), pow(10, S))), srl_decimal_quotient_sign(C, 1, A, N)])
|
|
60
|
+
implied_by(srl_unary('round', decimal(C, S), N), [is(D, pow(10, S)), is(N, floordiv(add(mul(C, 2), D), mul(D, 2)))])
|
|
61
|
+
${['neg', 'abs', 'floor', 'ceil', 'trunc'].map((op) => `implied_by(srl_unary('${op}', X, V), [is_int(X), is(V, ${op}(X))])`).join('\n')}
|
|
62
|
+
implied_by(srl_unary('round', X, X), is_int(X))
|
|
63
|
+
implied_by(srl_unary(Op, X, V), [srl_float_operand(X), not(is_int(X)), float64(Op, X, 0, V)])
|
|
64
|
+
${['add', 'sub', 'mul'].map((op) => `implied_by(srl_calculate('${op}', X, Y, V), [is_int(X), is_int(Y), is(V, ${op}(X, Y))])`).join('\n')}
|
|
65
|
+
implied_by(srl_calculate(Op, X, Y, V), [srl_float_operand(X), srl_float_operand(Y), not(and(is_int(X), is_int(Y))), float64(Op, X, Y, V)])
|
|
66
|
+
`;
|
package/src/srl-functions.js
CHANGED
|
@@ -10,9 +10,11 @@
|
|
|
10
10
|
//
|
|
11
11
|
// Values are RDF terms as the compiler writes them: an xsd:integer with a
|
|
12
12
|
// canonical lexical form is an eyemind integer, every other literal is
|
|
13
|
-
// literal(Lexical, datatype(IRI)) or literal(Text, lang(Tag)).
|
|
14
|
-
//
|
|
15
|
-
// their datatype.
|
|
13
|
+
// literal(Lexical, datatype(IRI)) or literal(Text, lang(Tag)). Exact
|
|
14
|
+
// decimals use integer coefficients and scales; floats and doubles
|
|
15
|
+
// use binary64 values. Results retain their promoted datatype.
|
|
16
|
+
|
|
17
|
+
import { decimalRules } from './srl-decimal.js';
|
|
16
18
|
|
|
17
19
|
const XSD = 'http://www.w3.org/2001/XMLSchema#';
|
|
18
20
|
const RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
|
|
@@ -30,28 +32,38 @@ const tested = (name, args) => String.raw`
|
|
|
30
32
|
implied_by(${name}_t(${args}, T), or([${name}_(${args}), unify(T, 'true')], [not(${name}_(${args})), unify(T, 'false')]))`;
|
|
31
33
|
|
|
32
34
|
export const DEFINITIONS = {
|
|
35
|
+
srl_now: {
|
|
36
|
+
text: String.raw`
|
|
37
|
+
implied_by(srl_now(literal(Now, datatype('${XSD}dateTime'))), srl_execution(Now, _))`,
|
|
38
|
+
},
|
|
39
|
+
srl_uuid: {
|
|
40
|
+
text: String.raw`
|
|
41
|
+
// A random execution seed, a call site and its incoming solution identify
|
|
42
|
+
// one UUID. Repeated reasoning rounds and certificate checks keep it fixed.
|
|
43
|
+
implied_by(srl_uuid(Rule, Site, Solution, Id), [srl_execution(_, Seed), term_text([Seed, Rule, Site, Solution], Text), digest('sha256', Text, Hex), regex_find('^(.{8})(.{4}).(.{3})(.)(.{3})(.{12})', Hex, [_, A, B, C, V, D, E]), srl_uuid_variant(V, W), srl_atoms_concat([A, '-', B, '-4', C, '-', W, D, '-', E], Id)])
|
|
44
|
+
${[...'0123456789abcdef'].map((c, i) => `fact(srl_uuid_variant('${c}', '${'89ab'[i % 4]}'))`).join('\n')}`,
|
|
45
|
+
needs: ['srl_atoms_concat'],
|
|
46
|
+
},
|
|
33
47
|
// Numbers.
|
|
34
48
|
srl_num: {
|
|
35
49
|
text: String.raw`
|
|
36
50
|
// srl_num(T, K, V): T is a number of kind K (integer, decimal, float or
|
|
37
|
-
// double) with value V;
|
|
51
|
+
// double) with value V; a decimal is decimal(Coefficient, Scale).
|
|
38
52
|
implied_by(srl_num(T, 'integer', T), is_int(T))
|
|
39
|
-
implied_by(srl_num(literal(L, datatype(D)), K, V), [srl_numeric_type(D, K),
|
|
53
|
+
implied_by(srl_num(literal(L, datatype(D)), K, V), [srl_numeric_type(D, K), srl_read_number(K, L, V)])
|
|
54
|
+
implied_by(srl_read_number('decimal', L, V), srl_decimal_text(L, V))
|
|
55
|
+
implied_by(srl_read_number('integer', L, V), [regex_find('^[ \\t\\n\\r]*([+-]?[0-9]+)[ \\t\\n\\r]*$', L, [_, I]), atom_chars(I, Cs), number_chars(V, Cs)])
|
|
56
|
+
implied_by(srl_read_number(K, L, V), [srl_float_kind(K), float64('parse', L, 0, V)])
|
|
40
57
|
${INTEGER_TYPES.map((t) => `fact(srl_numeric_type('${XSD}${t}', 'integer'))`).join('\n')}
|
|
41
58
|
fact(srl_numeric_type('${XSD}decimal', 'decimal'), srl_numeric_type('${XSD}float', 'float'), srl_numeric_type('${XSD}double', 'double'))
|
|
42
|
-
|
|
43
|
-
implied_by(srl_num_value(K, N, V), [srl_float_kind(K), is(V, float(N))])
|
|
44
|
-
fact(srl_float_kind('decimal'), srl_float_kind('float'), srl_float_kind('double'))
|
|
59
|
+
fact(srl_float_kind('float'), srl_float_kind('double'))
|
|
45
60
|
// Numeric type promotion: integer, decimal, float, double.
|
|
46
61
|
fact(srl_rank('integer', 0), srl_rank('decimal', 1), srl_rank('float', 2), srl_rank('double', 3))
|
|
47
62
|
implied_by(srl_promote(A, B, K), [srl_rank(A, I), srl_rank(B, J), is(M, max(I, J)), srl_rank(K, M)])`,
|
|
63
|
+
needs: ['srl_decimal'],
|
|
48
64
|
},
|
|
49
|
-
|
|
50
|
-
text: String.raw`
|
|
51
|
-
// srl_make(K, V, T): T is the term for the value V of kind K. A float is
|
|
52
|
-
// written as its shortest text; a decimal never has an exponent.
|
|
53
|
-
implied_by(srl_make('integer', V, V), is_int(V))
|
|
54
|
-
implied_by(srl_make(K, V, literal(L, datatype(D))), [srl_float_kind(K), srl_numeric_type(D, K), lt(abs(V), float(1.7976931348623157e308)), is(F, float(V)), number_chars(F, Cs), atom_chars(A, Cs), srl_float_text(K, A, L)])
|
|
65
|
+
srl_decimal: {
|
|
66
|
+
text: decimalRules + String.raw`
|
|
55
67
|
implied_by(srl_float_text('double', A, A), 'true')
|
|
56
68
|
implied_by(srl_float_text('float', A, A), 'true')
|
|
57
69
|
implied_by(srl_float_text('decimal', A, A), not(regex_match('e', A)))
|
|
@@ -61,36 +73,34 @@ implied_by(srl_point(Ds, N, P, B), [ge(P, N), is(Z, sub(P, N)), srl_zeros(Z, Zs)
|
|
|
61
73
|
implied_by(srl_point(Ds, N, P, B), [le(P, 0), is(Z, neg(P)), srl_zeros(Z, Zs), atom_concat('0.', Zs, H), atom_concat(H, Ds, B)])
|
|
62
74
|
implied_by(srl_point(Ds, N, P, B), [gt(P, 0), lt(P, N), once([atom_concat(H, T, Ds), atom_length(H, P)]), atom_concat(H, '.', H1), atom_concat(H1, T, B)])
|
|
63
75
|
fact(srl_zeros(0, ''))
|
|
64
|
-
implied_by(srl_zeros(N, Z), [gt(N, 0), is(M, sub(N, 1)), srl_zeros(M, Z0), atom_concat('0', Z0, Z)])
|
|
76
|
+
implied_by(srl_zeros(N, Z), [gt(N, 0), is(M, sub(N, 1)), srl_zeros(M, Z0), atom_concat('0', Z0, Z)])
|
|
77
|
+
implied_by(srl_compare(X, Y, O), or([lt(X, Y), unify(O, '<')], [eq(X, Y), unify(O, '=')], [gt(X, Y), unify(O, '>')]))`,
|
|
78
|
+
needs: ['srl_length'],
|
|
79
|
+
},
|
|
80
|
+
srl_make: {
|
|
81
|
+
text: String.raw`
|
|
82
|
+
// srl_make(K, V, T): T is the term for the value V of kind K. A float is
|
|
83
|
+
// written as its shortest text; a decimal never has an exponent.
|
|
84
|
+
implied_by(srl_make('integer', V, V), is_int(V))
|
|
85
|
+
implied_by(srl_make('decimal', V, literal(L, datatype('${XSD}decimal'))), [srl_decimal_value(V, D), srl_decimal_lexical(D, L)])
|
|
86
|
+
implied_by(srl_make(K, V, literal(L, datatype(D))), [srl_float_kind(K), srl_numeric_type(D, K), srl_float_value(V, N), srl_float_lexical(N, L)])
|
|
87
|
+
`,
|
|
65
88
|
needs: ['srl_num'],
|
|
66
89
|
},
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
// raises no error; a float result that would overflow is a SPARQL error.
|
|
71
|
-
implied_by(srl_finite(Op, X, Y), once(srl_finite_(Op, X, Y)))
|
|
72
|
-
implied_by(srl_finite_(_, X, Y), [is_int(X), is_int(Y)])
|
|
73
|
-
implied_by(srl_finite_('add', X, Y), [lt(abs(X), 8e307), lt(abs(Y), 8e307)])
|
|
74
|
-
implied_by(srl_finite_('sub', X, Y), [lt(abs(X), 8e307), lt(abs(Y), 8e307)])
|
|
75
|
-
implied_by(srl_finite_('mul', X, Y), or(eq(X, 0), eq(Y, 0)))
|
|
76
|
-
implied_by(srl_finite_('mul', X, Y), [srl_magnitude(X, M), srl_magnitude(Y, N), lt(add(M, N), 308)])
|
|
77
|
-
implied_by(srl_finite_('div', X, Y), eq(X, 0))
|
|
78
|
-
implied_by(srl_finite_('div', X, Y), [srl_magnitude(X, M), srl_magnitude(Y, N), lt(sub(M, N), 308)])
|
|
79
|
-
implied_by(srl_magnitude(X, M), [ne(X, 0), lt(abs(X), 1e308), is(M, log10(abs(X)))])`,
|
|
80
|
-
},
|
|
81
|
-
srl_add: { text: arith('srl_add', 'add'), needs: ['srl_make', 'srl_finite'] },
|
|
82
|
-
srl_sub: { text: arith('srl_sub', 'sub'), needs: ['srl_make', 'srl_finite'] },
|
|
83
|
-
srl_mul: { text: arith('srl_mul', 'mul'), needs: ['srl_make', 'srl_finite'] },
|
|
90
|
+
srl_add: { text: arith('srl_add', 'add'), needs: ['srl_make'] },
|
|
91
|
+
srl_sub: { text: arith('srl_sub', 'sub'), needs: ['srl_make'] },
|
|
92
|
+
srl_mul: { text: arith('srl_mul', 'mul'), needs: ['srl_make'] },
|
|
84
93
|
srl_div: {
|
|
85
94
|
text: String.raw`
|
|
86
|
-
//
|
|
87
|
-
|
|
95
|
+
// Integers divide to a decimal. Decimal division by zero is an error;
|
|
96
|
+
// floats and doubles retain their IEEE-754 exceptional results.
|
|
97
|
+
implied_by(srl_div(A, B, R), [srl_pair(A, B, K0, X0, Y0), srl_div_kind(K0, K), srl_numeric_value(K, X0, X), srl_numeric_value(K, Y0, Y), srl_calculate('div', X, Y, V), srl_make(K, V, R)])
|
|
88
98
|
fact(srl_div_kind('integer', 'decimal'), srl_div_kind('decimal', 'decimal'), srl_div_kind('float', 'float'), srl_div_kind('double', 'double'))`,
|
|
89
|
-
needs: ['srl_make'
|
|
99
|
+
needs: ['srl_make'],
|
|
90
100
|
},
|
|
91
101
|
srl_neg: {
|
|
92
102
|
text: String.raw`
|
|
93
|
-
implied_by(srl_neg(A, R), [srl_num(A, K, X),
|
|
103
|
+
implied_by(srl_neg(A, R), [srl_num(A, K, X), srl_unary('neg', X, V), srl_make(K, V, R)])`,
|
|
94
104
|
needs: ['srl_make'],
|
|
95
105
|
},
|
|
96
106
|
srl_pos: {
|
|
@@ -112,7 +122,7 @@ fact(srl_flip('true', 'false'), srl_flip('false', 'true'))`,
|
|
|
112
122
|
// a term without one, such as an IRI, has none.
|
|
113
123
|
implied_by(srl_ebv(T, B), once(srl_ebv_(T, B)))
|
|
114
124
|
implied_by(srl_ebv_(literal(L, datatype('${XSD}boolean')), B), srl_boolean_text(L, B))
|
|
115
|
-
implied_by(srl_ebv_(T, B), [srl_num(T, _, V), or([eq(V, 0), unify(B, 'false')], [ne(V, 0), unify(B, 'true')])])
|
|
125
|
+
implied_by(srl_ebv_(T, B), [srl_num(T, _, X), srl_value_zero(X, V), or([eq(V, 0), unify(B, 'false')], [ne(V, 0), unify(B, 'true')])])
|
|
116
126
|
implied_by(srl_ebv_(literal(L, datatype('${XSD}string')), B), srl_nonempty(L, B))
|
|
117
127
|
implied_by(srl_ebv_(literal(L, lang(_)), B), srl_nonempty(L, B))
|
|
118
128
|
implied_by(srl_ebv_(literal(L, lang(_, _)), B), srl_nonempty(L, B))
|
|
@@ -151,7 +161,7 @@ fact(srl_or3('false', 'false', 'false'), srl_or3('true', _, 'true'), srl_or3('fa
|
|
|
151
161
|
// srl_equal(A, B, T): T is 'true' when A and B are equal and 'false' when
|
|
152
162
|
// they are not; literals that cannot be compared have no answer, an error.
|
|
153
163
|
implied_by(srl_equal(A, B, T), once(srl_equal_(A, B, T)))
|
|
154
|
-
implied_by(srl_equal_(A, B, T), [
|
|
164
|
+
implied_by(srl_equal_(A, B, T), [srl_pair(A, B, _, X, Y), srl_value_compare(X, Y, O), or([identical(O, '='), unify(T, 'true')], [not_identical(O, '='), unify(T, 'false')])])
|
|
155
165
|
implied_by(srl_equal_(A, B, 'true'), identical(A, B))
|
|
156
166
|
implied_by(srl_equal_(A, B, T), [srl_boolean(A, X), srl_boolean(B, Y), or([identical(X, Y), unify(T, 'true')], [not_identical(X, Y), unify(T, 'false')])])
|
|
157
167
|
implied_by(srl_equal_(A, B, 'false'), [srl_text_kind(A, _), srl_text_kind(B, _)])
|
|
@@ -193,14 +203,18 @@ implied_by(srl_ne(A, B, R), [srl_equal(A, B, T), srl_flip(T, F), srl_bool(F, R)]
|
|
|
193
203
|
// numbers by value, simple strings by code point, booleans with false
|
|
194
204
|
// first, and date and time values of one datatype by their lexical forms.
|
|
195
205
|
implied_by(srl_order(A, B, O), once(srl_order_(A, B, O)))
|
|
196
|
-
implied_by(srl_order_(A, B, O), [
|
|
206
|
+
implied_by(srl_order_(A, B, O), [srl_pair(A, B, _, X, Y), srl_value_compare(X, Y, O)])
|
|
197
207
|
implied_by(srl_order_(literal(X, datatype('${XSD}string')), literal(Y, datatype('${XSD}string')), O), compare(O, X, Y))
|
|
198
208
|
implied_by(srl_order_(A, B, O), [srl_boolean(A, X), srl_boolean(B, Y), compare(O, X, Y)])
|
|
199
209
|
implied_by(srl_order_(literal(X, datatype(D)), literal(Y, datatype(D)), O), [srl_ordered_type(D), compare(O, X, Y)])
|
|
200
210
|
implied_by(srl_compare(X, Y, O), or([lt(X, Y), unify(O, '<')], [eq(X, Y), unify(O, '=')], [gt(X, Y), unify(O, '>')]))
|
|
211
|
+
fact(srl_lt_('unordered', 'false'))
|
|
201
212
|
fact(srl_lt_('<', 'true'), srl_lt_('=', 'false'), srl_lt_('>', 'false'))
|
|
213
|
+
fact(srl_le_('unordered', 'false'))
|
|
202
214
|
fact(srl_le_('<', 'true'), srl_le_('=', 'true'), srl_le_('>', 'false'))
|
|
215
|
+
fact(srl_gt_('unordered', 'false'))
|
|
203
216
|
fact(srl_gt_('<', 'false'), srl_gt_('=', 'false'), srl_gt_('>', 'true'))
|
|
217
|
+
fact(srl_ge_('unordered', 'false'))
|
|
204
218
|
fact(srl_ge_('<', 'false'), srl_ge_('=', 'true'), srl_ge_('>', 'true'))`,
|
|
205
219
|
needs: ['srl_equal'],
|
|
206
220
|
},
|
|
@@ -378,14 +392,14 @@ implied_by(srl_concat_kind(K, K), not_identical(K, 'none'))`,
|
|
|
378
392
|
srl_substr: {
|
|
379
393
|
text: String.raw`
|
|
380
394
|
// SUBSTR counts characters from 1, rounding its numeric arguments.
|
|
381
|
-
implied_by(srl_substr(T, S, literal(B, K)), [srl_string(T, A, K), srl_num(S, _, X),
|
|
382
|
-
implied_by(srl_substr(T, S, L, literal(B, K)), [srl_string(T, A, K), srl_num(S, _, X), srl_num(L, _, Y),
|
|
395
|
+
implied_by(srl_substr(T, S, literal(B, K)), [srl_string(T, A, K), srl_num(S, _, X), srl_unary('round', X, I), atom_chars(A, Cs), atom_length(A, N), is(E, add(N, 1)), srl_slice(Cs, 1, I, E, Bs), atom_chars(B, Bs)])
|
|
396
|
+
implied_by(srl_substr(T, S, L, literal(B, K)), [srl_string(T, A, K), srl_num(S, _, X), srl_num(L, _, Y), srl_unary('round', X, I), srl_unary('round', Y, J), is(E, add(I, J)), atom_chars(A, Cs), srl_slice(Cs, 1, I, E, Bs), atom_chars(B, Bs)])
|
|
383
397
|
// srl_slice(Cs, P, I, E, Bs): Bs are the characters of Cs, the first at
|
|
384
398
|
// position P, whose positions are at least I and below E.
|
|
385
399
|
fact(srl_slice([], _, _, _, []))
|
|
386
400
|
implied_by(srl_slice([C, ...Cs], P, I, E, [C, ...Bs]), [ge(P, I), lt(P, E), is(Q, add(P, 1)), srl_slice(Cs, Q, I, E, Bs)])
|
|
387
401
|
implied_by(srl_slice([_, ...Cs], P, I, E, Bs), [or(lt(P, I), ge(P, E)), is(Q, add(P, 1)), srl_slice(Cs, Q, I, E, Bs)])
|
|
388
|
-
|
|
402
|
+
`,
|
|
389
403
|
needs: ['srl_string', 'srl_num'],
|
|
390
404
|
},
|
|
391
405
|
srl_replace: {
|
|
@@ -427,13 +441,12 @@ implied_by(srl_atoms_concat([A, ...As], C), [srl_atoms_concat(As, C0), atom_conc
|
|
|
427
441
|
// Numeric functions.
|
|
428
442
|
srl_abs: {
|
|
429
443
|
text: String.raw`
|
|
430
|
-
implied_by(srl_abs(T, R), [srl_num(T, K, X),
|
|
431
|
-
implied_by(srl_ceil(T, R), [srl_num(T, K, X),
|
|
432
|
-
implied_by(srl_floor(T, R), [srl_num(T, K, X),
|
|
444
|
+
implied_by(srl_abs(T, R), [srl_num(T, K, X), srl_unary('abs', X, V), srl_make(K, V, R)])
|
|
445
|
+
implied_by(srl_ceil(T, R), [srl_num(T, K, X), srl_unary('ceil', X, V), srl_make(K, V, R)])
|
|
446
|
+
implied_by(srl_floor(T, R), [srl_num(T, K, X), srl_unary('floor', X, V), srl_make(K, V, R)])
|
|
433
447
|
// ROUND rounds halves up, toward positive infinity.
|
|
434
|
-
implied_by(srl_round(T, R), [srl_num(T, K, X),
|
|
435
|
-
|
|
436
|
-
implied_by(srl_round_(K, X, V), [srl_float_kind(K), is(V, floor(add(X, float(0.5))))])`,
|
|
448
|
+
implied_by(srl_round(T, R), [srl_num(T, K, X), srl_unary('round', X, V), srl_make(K, V, R)])
|
|
449
|
+
`,
|
|
437
450
|
needs: ['srl_make'],
|
|
438
451
|
},
|
|
439
452
|
|
|
@@ -474,15 +487,15 @@ implied_by(srl_cast_('${XSD}string', T, ${str('A')}), srl_cast_text(T, A))
|
|
|
474
487
|
implied_by(srl_cast_text(iri(A), A), 'true')
|
|
475
488
|
implied_by(srl_cast_text(literal(A, _), A), 'true')
|
|
476
489
|
implied_by(srl_cast_text(T, A), [is_int(T), number_chars(T, Cs), atom_chars(A, Cs)])
|
|
477
|
-
implied_by(srl_cast_('${XSD}integer', T, V), [srl_num(T, _, X),
|
|
490
|
+
implied_by(srl_cast_('${XSD}integer', T, V), [srl_num(T, _, X), srl_unary('trunc', X, V)])
|
|
478
491
|
implied_by(srl_cast_('${XSD}integer', T, V), [srl_boolean(T, B), srl_bit(B, V)])
|
|
479
|
-
implied_by(srl_cast_('${XSD}integer', literal(L, datatype('${XSD}string')), V), [
|
|
492
|
+
implied_by(srl_cast_('${XSD}integer', literal(L, datatype('${XSD}string')), V), [regex_find('^[ \\t\\n\\r]*([+-]?[0-9]+)[ \\t\\n\\r]*$', L, [_, I]), atom_chars(I, Cs), number_chars(V, Cs)])
|
|
480
493
|
implied_by(srl_cast_(D, T, R), [srl_float_type(D, K), srl_num(T, _, X), srl_make(K, X, R)])
|
|
481
494
|
implied_by(srl_cast_(D, T, R), [srl_float_type(D, K), srl_boolean(T, B), srl_bit(B, V), srl_make(K, V, R)])
|
|
482
|
-
implied_by(srl_cast_('${XSD}decimal', literal(L, datatype('${XSD}string')), R), [
|
|
483
|
-
implied_by(srl_cast_(D, literal(L, datatype('${XSD}string')), R), [srl_float_type(D, K), srl_float_kind_double(K),
|
|
495
|
+
implied_by(srl_cast_('${XSD}decimal', literal(L, datatype('${XSD}string')), R), [srl_decimal_text(L, X), srl_make('decimal', X, R)])
|
|
496
|
+
implied_by(srl_cast_(D, literal(L, datatype('${XSD}string')), R), [srl_float_type(D, K), srl_float_kind_double(K), float64('parse', L, 0, X), srl_make(K, X, R)])
|
|
484
497
|
implied_by(srl_cast_('${XSD}boolean', T, R), [srl_boolean(T, B), srl_bool(B, R)])
|
|
485
|
-
implied_by(srl_cast_('${XSD}boolean', T, R), [srl_num(T, _, X), or([eq(X, 0), unify(B, 'false')], [ne(X, 0), unify(B, 'true')]), srl_bool(B, R)])
|
|
498
|
+
implied_by(srl_cast_('${XSD}boolean', T, R), [srl_num(T, _, V), srl_value_zero(V, X), or([eq(X, 0), unify(B, 'false')], [ne(X, 0), unify(B, 'true')]), srl_bool(B, R)])
|
|
486
499
|
implied_by(srl_cast_('${XSD}boolean', literal(L, datatype('${XSD}string')), R), [or([srl_true_text(L), unify(B, 'true')], [srl_false_text(L), unify(B, 'false')]), srl_bool(B, R)])
|
|
487
500
|
implied_by(srl_cast_('${XSD}dateTime', literal(L, datatype(D)), literal(L, datatype('${XSD}dateTime'))), [srl_text_or_datetime(D), regex_match('^-?\\d{4,}-\\d\\d-\\d\\dT\\d\\d:\\d\\d:\\d\\d(?:\\.\\d+)?(?:Z|[+-]\\d\\d:\\d\\d)?$', L)])
|
|
488
501
|
implied_by(srl_cast_('${XSD}date', literal(L, datatype(D)), literal(L, datatype('${XSD}date'))), [srl_text_or_date(D), regex_match('^-?\\d{4,}-\\d\\d-\\d\\d(?:Z|[+-]\\d\\d:\\d\\d)?$', L)])
|
|
@@ -555,7 +568,7 @@ implied_by(srl_column([R, ...Rs], I, [V, ...Vs]), [srl_nth(I, R, V), srl_column(
|
|
|
555
568
|
|
|
556
569
|
function arith(name, op) {
|
|
557
570
|
return String.raw`
|
|
558
|
-
implied_by(${name}(A, B, R), [
|
|
571
|
+
implied_by(${name}(A, B, R), [srl_pair(A, B, K, X, Y), srl_calculate('${op}', X, Y, V), srl_make(K, V, R)])`;
|
|
559
572
|
}
|
|
560
573
|
|
|
561
574
|
function compare(name) {
|
package/src/srl-parser.js
CHANGED
|
@@ -59,7 +59,9 @@ class Parser {
|
|
|
59
59
|
this.parseImports();
|
|
60
60
|
} else if (this.matchWord('DATA')) {
|
|
61
61
|
this.expectValue('{');
|
|
62
|
-
|
|
62
|
+
// eyemind: native certificates can expand into more triples than a
|
|
63
|
+
// host permits as function arguments to one push(...triples).
|
|
64
|
+
for (const triple of this.parseTriplesBlock({ allowPath: false, context: 'data' })) data.push(triple);
|
|
63
65
|
} else if (this.matchWord('RULE')) {
|
|
64
66
|
rules.push(this.parseRule());
|
|
65
67
|
} else {
|
|
@@ -153,7 +155,7 @@ class Parser {
|
|
|
153
155
|
parseTriplesBlock(options = {}) {
|
|
154
156
|
const triples = [];
|
|
155
157
|
while (!this.matchValue('}')) {
|
|
156
|
-
|
|
158
|
+
for (const triple of this.parseTripleStatement(options)) triples.push(triple);
|
|
157
159
|
this.consumeOptionalDot();
|
|
158
160
|
}
|
|
159
161
|
return triples;
|
|
@@ -162,7 +164,7 @@ class Parser {
|
|
|
162
164
|
parseTripleStatement(options = {}) {
|
|
163
165
|
const subjectNode = this.parseGraphNode({ ...options, position: 'subject' });
|
|
164
166
|
const triples = [...subjectNode.triples];
|
|
165
|
-
|
|
167
|
+
for (const triple of this.parsePropertyListForSubject(subjectNode.term, options)) triples.push(triple);
|
|
166
168
|
return triples;
|
|
167
169
|
}
|
|
168
170
|
|
|
@@ -175,10 +177,10 @@ class Parser {
|
|
|
175
177
|
const predicate = options.allowPath ? this.parseVerbPathOrSimple(options) : this.parseVerbTerm(options);
|
|
176
178
|
do {
|
|
177
179
|
const objectNode = this.parseGraphNode({ ...options, position: 'object' });
|
|
178
|
-
|
|
180
|
+
for (const triple of objectNode.triples) triples.push(triple);
|
|
179
181
|
const baseTriple = { s: subject, p: predicate, o: objectNode.term };
|
|
180
182
|
triples.push(baseTriple);
|
|
181
|
-
|
|
183
|
+
for (const triple of this.parseAnnotationsForTriple(baseTriple, options)) triples.push(triple);
|
|
182
184
|
} while (this.matchValue(','));
|
|
183
185
|
|
|
184
186
|
if (this.matchValue(';')) {
|
|
@@ -216,7 +218,7 @@ class Parser {
|
|
|
216
218
|
const triples = [];
|
|
217
219
|
while (true) {
|
|
218
220
|
const item = this.parseGraphNode(options);
|
|
219
|
-
|
|
221
|
+
for (const triple of item.triples) triples.push(triple);
|
|
220
222
|
triples.push({ s: current, p: iri(RDF_FIRST), o: item.term });
|
|
221
223
|
if (this.matchValue(')')) {
|
|
222
224
|
triples.push({ s: current, p: iri(RDF_REST), o: iri(RDF_NIL) });
|
|
@@ -253,7 +255,7 @@ class Parser {
|
|
|
253
255
|
if (this.checkValue('|}')) throw this.error('Annotation blocks may not be empty');
|
|
254
256
|
const annotationSubject = currentReifier || this.freshGraphNode(options);
|
|
255
257
|
triples.push({ s: annotationSubject, p: iri(RDF_REIFIES), o: reified });
|
|
256
|
-
|
|
258
|
+
for (const triple of this.parsePropertyListForSubject(annotationSubject, options, ['|}'])) triples.push(triple);
|
|
257
259
|
this.expectValue('|}');
|
|
258
260
|
}
|
|
259
261
|
}
|
package/src/srl.js
CHANGED
|
@@ -28,7 +28,9 @@
|
|
|
28
28
|
// head triple term with variables) needs no more: the rule set's analysis
|
|
29
29
|
// has rejected those that depend on their own conclusions, and the others
|
|
30
30
|
// conclude the same when they run alongside what they read, because SET and
|
|
31
|
-
// Skolem terms give the same values in every round.
|
|
31
|
+
// Skolem terms give the same values in every round. Execution functions
|
|
32
|
+
// retain their captured context and identify each UUID by its incoming
|
|
33
|
+
// solution and expression occurrence.
|
|
32
34
|
import { analyze } from './srl-analyze.js';
|
|
33
35
|
import { BUILTIN_SIGNATURES, canonicalBuiltinName } from './srl-builtin-names.js';
|
|
34
36
|
import { libraryText } from './srl-functions.js';
|
|
@@ -38,6 +40,7 @@ import { loadText } from './lang.js';
|
|
|
38
40
|
import { DEFAULT_GRAPH } from './rdf.js';
|
|
39
41
|
import { readTerms } from './reader.js';
|
|
40
42
|
import { Struct } from './terms.js';
|
|
43
|
+
import { readProofTerms } from './native-proof.js';
|
|
41
44
|
import { write } from './writer.js';
|
|
42
45
|
|
|
43
46
|
export class SrlError extends Error {}
|
|
@@ -96,14 +99,15 @@ const RESERVED = new Set(['Math', 'JSON', 'BigInt', 'Infinity', 'NaN', 'undefine
|
|
|
96
99
|
|
|
97
100
|
// Compile a rule set's text. Options: base (IRI), filename, resolve (for
|
|
98
101
|
// IMPORTS, see readRuleSet), data (the base graph, as RDF quads from
|
|
99
|
-
// parseRdf, in its default graph)
|
|
100
|
-
|
|
102
|
+
// parseRdf, in its default graph), context (a recorded execution's now and
|
|
103
|
+
// seed). Returns { program, prefixes, warnings, context }.
|
|
104
|
+
export function compileSrl(text, { base = null, filename = '<input>', resolve = null, data = [], context = null } = {}) {
|
|
101
105
|
const ruleSet = readRuleSet(text, { base, filename, resolve });
|
|
102
|
-
return compileRuleSet(ruleSet, { data, source: filename });
|
|
106
|
+
return compileRuleSet(ruleSet, { data, source: filename, context });
|
|
103
107
|
}
|
|
104
108
|
|
|
105
109
|
// Compile a parsed rule set over a base graph of RDF quads.
|
|
106
|
-
export function compileRuleSet(ruleSet, { data = [], source = '<input>' } = {}) {
|
|
110
|
+
export function compileRuleSet(ruleSet, { data = [], source = '<input>', context = null } = {}) {
|
|
107
111
|
const needed = new Set();
|
|
108
112
|
const warnings = [];
|
|
109
113
|
const lines = [
|
|
@@ -122,10 +126,49 @@ export function compileRuleSet(ruleSet, { data = [], source = '<input>' } = {})
|
|
|
122
126
|
ruleSet.rules.forEach((rule, index) => {
|
|
123
127
|
lines.push(...new RuleCompiler(rule, index + 1, { needed, warnings }).compile());
|
|
124
128
|
});
|
|
129
|
+
if (needed.has('srl_now') || needed.has('srl_uuid')) {
|
|
130
|
+
context = executionContext(context);
|
|
131
|
+
lines.push(`fact(srl_execution(${q(context.now)}, ${q(context.seed)}))`);
|
|
132
|
+
}
|
|
125
133
|
lines.push('query(inf(S, P, O))');
|
|
126
134
|
const library = libraryText(needed);
|
|
127
135
|
if (library) lines.push('', '// SPARQL functions and aggregates.', library);
|
|
128
|
-
return { program: `${lines.join('\n')}\n`, prefixes: ruleSet.prefixes, warnings };
|
|
136
|
+
return { program: `${lines.join('\n')}\n`, prefixes: ruleSet.prefixes, warnings, context };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Clock and entropy are inputs captured once when the rule set is compiled.
|
|
140
|
+
// A saved proof carries this one fact; replay validates its shape and then
|
|
141
|
+
// recompiles the source. No other clause display becomes source authority.
|
|
142
|
+
function executionContext(context) {
|
|
143
|
+
if (context === null) {
|
|
144
|
+
const bytes = new Uint8Array(32);
|
|
145
|
+
if (globalThis.crypto?.getRandomValues) globalThis.crypto.getRandomValues(bytes);
|
|
146
|
+
else for (let i = 0; i < bytes.length; i++) bytes[i] = Math.floor(Math.random() * 256);
|
|
147
|
+
context = { now: new Date().toISOString(), seed: [...bytes].map((b) => b.toString(16).padStart(2, '0')).join('') };
|
|
148
|
+
}
|
|
149
|
+
if (typeof context?.now !== 'string' || !/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$/.test(context.now) ||
|
|
150
|
+
!Number.isFinite(Date.parse(context.now)) || new Date(context.now).toISOString() !== context.now ||
|
|
151
|
+
typeof context.seed !== 'string' || !/^[0-9a-f]{64}$/.test(context.seed)) {
|
|
152
|
+
throw new SrlError('an SRL execution context needs a UTC now timestamp and a 64-digit lowercase hexadecimal seed');
|
|
153
|
+
}
|
|
154
|
+
return { now: context.now, seed: context.seed };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function srlContextFromProof(document) {
|
|
158
|
+
try {
|
|
159
|
+
const contexts = readProofTerms(document).map(([term]) => term)
|
|
160
|
+
.filter((t) => t instanceof Struct && t.name === 'clause' && t.args.length === 2)
|
|
161
|
+
.map((t) => t.args[1])
|
|
162
|
+
.filter((t) => t instanceof Struct && t.name === 'fact' && t.args.length === 1)
|
|
163
|
+
.map((t) => t.args[0])
|
|
164
|
+
.filter((t) => t instanceof Struct && t.name === 'srl_execution' && t.args.length === 2);
|
|
165
|
+
if (contexts.length !== 1) return null;
|
|
166
|
+
return executionContext({ now: contexts[0].args[0], seed: contexts[0].args[1] });
|
|
167
|
+
} catch {
|
|
168
|
+
// The ordinary checker reports malformed certificates, including native
|
|
169
|
+
// displays that disagree with their complete encoded records.
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
129
172
|
}
|
|
130
173
|
|
|
131
174
|
// RDF terms as eyemind terms. Blank nodes of data are bnode(Scope, Label).
|
|
@@ -173,6 +216,7 @@ class RuleCompiler {
|
|
|
173
216
|
this.helpers = [];
|
|
174
217
|
this.helperCount = 0;
|
|
175
218
|
this.terms = new Terms('srl');
|
|
219
|
+
this.sites = new WeakMap();
|
|
176
220
|
}
|
|
177
221
|
|
|
178
222
|
compile() {
|
|
@@ -507,10 +551,22 @@ class RuleCompiler {
|
|
|
507
551
|
};
|
|
508
552
|
}
|
|
509
553
|
case 'NOW':
|
|
510
|
-
|
|
554
|
+
return this.call('srl_now', []);
|
|
511
555
|
case 'UUID':
|
|
512
|
-
case 'STRUUID':
|
|
513
|
-
|
|
556
|
+
case 'STRUUID': {
|
|
557
|
+
this.need('srl_uuid');
|
|
558
|
+
if (!this.sites.has(expr)) this.sites.set(expr, ++this.helperCount);
|
|
559
|
+
const site = this.sites.get(expr);
|
|
560
|
+
const solution = [...scope.allBound()].sort().map((v) => scope.name(v));
|
|
561
|
+
const id = this.fresh('Uuid');
|
|
562
|
+
const value = this.fresh('R');
|
|
563
|
+
const goals = [`srl_uuid(${this.number}, ${site}, [${solution.join(', ')}], ${id})`];
|
|
564
|
+
if (name === 'UUID') {
|
|
565
|
+
const iri = this.fresh('Iri');
|
|
566
|
+
goals.push(`atom_concat('urn:uuid:', ${id}, ${iri})`, `unify(${value}, iri(${iri}))`);
|
|
567
|
+
} else goals.push(`unify(${value}, literal(${id}, datatype('${XSD}string')))`);
|
|
568
|
+
return { goals, value };
|
|
569
|
+
}
|
|
514
570
|
default:
|
|
515
571
|
throw new SrlError(`${name} is not supported`);
|
|
516
572
|
}
|
|
@@ -551,7 +607,8 @@ class Scope {
|
|
|
551
607
|
}
|
|
552
608
|
|
|
553
609
|
allBound() {
|
|
554
|
-
|
|
610
|
+
const inherited = this.parent ? [...this.parent.allBound()].filter((v) => this.keys?.has(v)) : [];
|
|
611
|
+
return new Set([...inherited, ...this.bound]);
|
|
555
612
|
}
|
|
556
613
|
|
|
557
614
|
term(t) {
|
|
@@ -617,10 +674,10 @@ export const SRL_LIMITS = { maxDepth: 1000000, maxIterations: 100000, maxInferen
|
|
|
617
674
|
// Returns { quads, program, prefixes, warnings, result }: the quads are the
|
|
618
675
|
// inference graph's, and with proof, result.proof is eyemind's checked
|
|
619
676
|
// proof of the compiled program.
|
|
620
|
-
export function reasonSrl(text, { base = null, filename = '<input>', resolve = null, data = [], all = false, limits = {}, proof = false } = {}) {
|
|
621
|
-
const { program, prefixes, warnings } = compileSrl(text, { base, filename, resolve, data });
|
|
677
|
+
export function reasonSrl(text, { base = null, filename = '<input>', resolve = null, data = [], all = false, limits = {}, proof = false, context = null } = {}) {
|
|
678
|
+
const { program, prefixes, warnings, context: execution } = compileSrl(text, { base, filename, resolve, data, context });
|
|
622
679
|
const result = run(loadText(program, 'compiled.js'), { ...SRL_LIMITS, ...limits, proof });
|
|
623
|
-
return { quads: srlQuads(program, result.answers, { all }), program, prefixes, warnings, result };
|
|
680
|
+
return { quads: srlQuads(program, result.answers, { all }), program, prefixes, warnings, result, context: execution };
|
|
624
681
|
}
|
|
625
682
|
|
|
626
683
|
// The inference graph of a run's answers, as quads in the default graph:
|
package/src/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// The release this checkout is; ./release keeps it in step with package.json.
|
|
2
|
-
export const VERSION = '0.5.
|
|
2
|
+
export const VERSION = '0.5.14';
|