eyemind 0.5.1 → 0.5.3

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.
@@ -0,0 +1,622 @@
1
+ // N3 built-ins as eyemind backward rules.
2
+ //
3
+ // Each built-in predicate of a rule body becomes a goal n3_<ns>_<name>(S, O)
4
+ // whose definition is written here in eyemind itself, so the compiled program
5
+ // is self-contained and every built-in step is an ordinary, checkable
6
+ // derivation. A definition lists the helpers it needs; the compiler copies
7
+ // the definitions a program uses, and their helpers, into the program.
8
+
9
+ const XSD = 'http://www.w3.org/2001/XMLSchema#';
10
+ const RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
11
+ const LOG = 'http://www.w3.org/2000/10/swap/log#';
12
+ const MATH = 'http://www.w3.org/2000/10/swap/math#';
13
+ const STRING = 'http://www.w3.org/2000/10/swap/string#';
14
+ const LIST = 'http://www.w3.org/2000/10/swap/list#';
15
+ const INVALID_IRI = '[<>"{}|^`\\\\\\x00-\\x20]';
16
+
17
+ const NUMERIC_TYPES = ['integer', 'decimal', 'double', 'float', 'int', 'long', 'short', 'byte',
18
+ 'nonNegativeInteger', 'positiveInteger', 'nonPositiveInteger', 'negativeInteger',
19
+ 'unsignedLong', 'unsignedInt', 'unsignedShort', 'unsignedByte'];
20
+
21
+ // Helpers shared by the built-ins.
22
+ export const HELPERS = {
23
+ n3_fuse: {
24
+ text: `contradiction(n3_fuse(literal('false', datatype('${XSD}boolean'))))`,
25
+ },
26
+ n3_num: {
27
+ text: `
28
+ // n3_num(T, N): N is the numeric value of the term T.
29
+ implied_by(n3_num(T, N), [is_number(T), unify(N, T)])
30
+ implied_by(n3_num(T, N), [is_compound(T), unify(T, literal(L, datatype(D))), n3_numeric_type(D), atom_chars(L, Cs), number_chars(V, Cs), n3_numeric_value(D, V, N)])
31
+ implied_by(n3_numeric_value(D, V, N), [n3_floating_type(D), is(N, float(V))])
32
+ implied_by(n3_numeric_value(D, V, V), not(n3_floating_type(D)))
33
+ fact(n3_floating_type('${XSD}decimal'), n3_floating_type('${XSD}double'), n3_floating_type('${XSD}float'))
34
+ ${NUMERIC_TYPES.map((t) => `fact(n3_numeric_type('${XSD}${t}'))`).join('\n')}`,
35
+ },
36
+ n3_nums: {
37
+ needs: ['n3_num'],
38
+ text: `
39
+ // n3_nums(L, Ns): the numeric values of a list of terms.
40
+ implied_by(n3_nums(L, Ns), [is_nonvar(L), n3_nums_(L, Ns)])
41
+ fact(n3_nums_([], []))
42
+ implied_by(n3_nums_([T, ...R], [N, ...Ns]), [n3_num(T, N), n3_nums_(R, Ns)])`,
43
+ },
44
+ n3_decimal: {
45
+ text: `
46
+ // A decimal's canonical text as an integer coefficient and a power of ten.
47
+ implied_by(n3_decimal(N, I, E), [number_chars(N, Cs), atom_chars(A, Cs), regex_find('^([+-]?)([0-9]+)(?:[.]([0-9]+))?(?:e([+-]?[0-9]+))?$', A, [_, Sign, Whole, Frac, Exp]), atom_concat(Sign, Whole, W), atom_concat(W, Frac, Digits), atom_chars(Digits, Ds), number_chars(I, Ds), atom_length(Frac, D), n3_decimal_exp(Exp, X), is(E, sub(X, D))])
48
+ fact(n3_decimal_exp('', 0))
49
+ implied_by(n3_decimal_exp(A, E), [not_identical(A, ''), atom_chars(A, Cs), number_chars(E, Cs)])
50
+ implied_by(n3_decimal_add(I, E, J, F, K, G), [is(G, min(E, F)), is(K, add(mul(I, pow(10, sub(E, G))), mul(J, pow(10, sub(F, G)))))])
51
+ implied_by(n3_decimal_value(I, E, N), [number_chars(I, Cs), atom_chars(A, Cs), number_chars(E, Es), atom_chars(B, Es), atom_concat(A, 'e', P), atom_concat(P, B, T), atom_chars(T, Ts), number_chars(N, Ts)])`,
52
+ },
53
+ n3_result: {
54
+ needs: ['n3_num'],
55
+ text: `
56
+ // n3_result(V, O): an unbound object becomes V, a bound one must equal it
57
+ // in value, so 2 and 2.0 agree.
58
+ implied_by(n3_result(V, O), [is_var(O), unify(O, V)])
59
+ implied_by(n3_result(V, O), [is_nonvar(O), n3_num(O, N), eq(V, N)])`,
60
+ },
61
+ n3_list_result: {
62
+ needs: ['n3_num'],
63
+ text: `
64
+ // A bound list result compares numeric elements by value; an unbound
65
+ // element keeps the term the input supplied.
66
+ fact(n3_list_result([], []))
67
+ implied_by(n3_list_result([X, ...Xs], [Y, ...Ys]), [once(or(unify(X, Y), and(n3_num(X, A), n3_num(Y, B), eq(A, B)))), n3_list_result(Xs, Ys)])`,
68
+ },
69
+ n3_str: {
70
+ text: `
71
+ // n3_str(T, A): A is the text of the term T: a literal's lexical form, an
72
+ // IRI, or a number's canonical spelling.
73
+ implied_by(n3_str(T, A), [is_compound(T), unify(T, literal(A, _))])
74
+ implied_by(n3_str(T, A), [is_compound(T), unify(T, iri(A))])
75
+ implied_by(n3_str(T, A), [is_int(T), number_chars(T, Cs), atom_chars(A, Cs)])
76
+ // A float with an integral value below 10^15 is written as an integer, 29
77
+ // rather than 29.0, as N3 reasoners write decimals in strings.
78
+ implied_by(n3_str(T, A), [is_float(T), n3_integral(T, F), n3_float_text(F, T, A)])
79
+ implied_by(n3_integral(T, 'yes'), [eq(T, floor(T)), lt(abs(T), float(1e15))])
80
+ implied_by(n3_integral(T, 'no'), or(ne(T, floor(T)), ge(abs(T), float(1e15))))
81
+ implied_by(n3_float_text('yes', T, A), [is(I, int(T)), number_chars(I, Cs), atom_chars(A, Cs)])
82
+ implied_by(n3_float_text('no', T, A), [number_chars(T, Cs), atom_chars(A, Cs)])`,
83
+ },
84
+ n3_strs: {
85
+ needs: ['n3_str'],
86
+ text: `
87
+ implied_by(n3_strs(L, As), [is_nonvar(L), n3_strs_(L, As)])
88
+ fact(n3_strs_([], []))
89
+ implied_by(n3_strs_([T, ...R], [A, ...As]), [n3_str(T, A), n3_strs_(R, As)])`,
90
+ },
91
+ n3_pattern: {
92
+ text: `
93
+ implied_by(n3_regex_pattern(A, P), once(or(n3_pattern(A, P), unify(A, P))))`,
94
+ },
95
+ n3_str_result: {
96
+ needs: ['n3_str'],
97
+ text: `
98
+ // n3_str_result(A, O): an unbound object becomes the string A, a bound one
99
+ // must have the text A.
100
+ implied_by(n3_str_result(A, O), [is_var(O), unify(O, literal(A, datatype('${XSD}string')))])
101
+ implied_by(n3_str_result(A, O), [is_nonvar(O), n3_str(O, A)])`,
102
+ },
103
+ n3_lower: {
104
+ text: `
105
+ // n3_lower(A, L), n3_upper(A, U): Unicode case conversion; comparing lower
106
+ // cases ignores case, as 'É' and 'é' are the same letter.
107
+ implied_by(n3_lower(A, L), atom_lower(A, L))
108
+ implied_by(n3_upper(A, U), atom_upper(A, U))`,
109
+ },
110
+ n3_contains: {
111
+ text: `
112
+ // n3_contains(A, B): the text B occurs in the text A.
113
+ implied_by(n3_contains(A, B), once(atom_concat(_, R, A), atom_concat(B, _, R)))`,
114
+ },
115
+ n3_append: {
116
+ text: `
117
+ fact(n3_append([], L, L))
118
+ implied_by(n3_append([H, ...T], L, [H, ...R]), n3_append(T, L, R))`,
119
+ },
120
+ n3_member: {
121
+ text: `
122
+ implied_by(n3_member(X, [X, ..._]), 'true')
123
+ implied_by(n3_member(X, [_, ...T]), n3_member(X, T))`,
124
+ },
125
+ n3_length: {
126
+ text: `
127
+ fact(n3_length([], 0))
128
+ implied_by(n3_length([_, ...T], N), [n3_length(T, M), is(N, add(M, 1))])`,
129
+ },
130
+ n3_formulas: {
131
+ needs: ['n3_sort', 'n3_includes'],
132
+ text: `
133
+ // n3_formulas(A, B, F): F is yes when A and B are both formulas, else no; call
134
+ // it with F unbound, as a bound F would let the second branch answer for it.
135
+ // n3_same_formula(A, B): each triple of either is a triple of the other,
136
+ // variables and blank nodes matched by unification.
137
+ implied_by(n3_formulas(A, B, F), once(or(and(is_nonvar(A), is_nonvar(B), unify(A, graph(_)), unify(B, graph(_)), unify(F, 'yes')), unify(F, 'no'))))
138
+ implied_by(n3_triple_set(graph(Ts), S), n3_sort(Ts, S))
139
+ implied_by(n3_same_formula(graph(As), graph(Bs)), [n3_all_members(As, Bs), n3_all_members(Bs, As)])`,
140
+ },
141
+ n3_conclusion_result: {
142
+ needs: ['n3_sort', 'n3_formulas'],
143
+ text: `
144
+ // n3_conclusion_result(G, O): an unbound object becomes the formula G, a
145
+ // bound one must hold the same triples.
146
+ implied_by(n3_conclusion_result(C, O), [is_var(O), unify(O, C)])
147
+ implied_by(n3_conclusion_result(graph(Ts), O), [is_nonvar(O), n3_sort(Ts, S), n3_triple_set(O, S)])`,
148
+ },
149
+ n3_member_at: {
150
+ needs: ['n3_result'],
151
+ text: `
152
+ // n3_member_at(L, K, I, X): X is at index I of L, counting from K.
153
+ implied_by(n3_member_at([X, ..._], K, I, X), n3_result(K, I))
154
+ implied_by(n3_member_at([_, ...R], K, I, X), [is(K1, add(K, 1)), n3_member_at(R, K1, I, X)])`,
155
+ },
156
+ n3_as_list: {
157
+ needs: ['n3_member'],
158
+ text: `
159
+ // n3_as_list(T, L): a collection written as rdf:first and rdf:rest triples
160
+ // from a blank node is read as the list L; any other T is L itself.
161
+ implied_by(n3_as_list(T, L), [once(n3_list_view(T, K)), n3_as_list_(K, T, L)])
162
+ implied_by(n3_list_view(T, 'as_is'), is_var(T))
163
+ implied_by(n3_list_view(iri('${RDF}nil'), 'described'), 'true')
164
+ implied_by(n3_list_view(bnode(_, _), 'described'), 'true')
165
+ implied_by(n3_list_view(skolem(_, _, _), 'described'), 'true')
166
+ implied_by(n3_list_view(_, 'as_is'), 'true')
167
+ implied_by(n3_as_list_('as_is', T, T), 'true')
168
+ implied_by(n3_as_list_('described', T, L), once(or(n3_described(T, [], L), unify(L, T))))
169
+ // Seen holds the nodes already read, so a cycle is no list.
170
+ implied_by(n3_described(iri('${RDF}nil'), _, []), 'true')
171
+ implied_by(n3_described(T, Seen, [X, ...R]), [not(n3_member(T, Seen)), rdf(T, iri('${RDF}first'), Y), n3_as_list(Y, X), rdf(T, iri('${RDF}rest'), N), n3_described(N, [T, ...Seen], R)])`,
172
+ },
173
+ n3_includes: {
174
+ needs: ['n3_member'],
175
+ text: `
176
+ // n3_includes(G, F): every triple of the formula F is a triple of the formula G.
177
+ implied_by(n3_includes(G, F), [is_nonvar(G), is_nonvar(F), n3_includes_(G, F)])
178
+ // No triple of F matches G, with F's unbound variables standing for any
179
+ // value: an empty collection, which unlike a negation allows them.
180
+ implied_by(n3_not_includes(G, F), [is_nonvar(G), is_nonvar(F), findall('found', n3_includes_(G, F), [])])
181
+ implied_by(n3_includes_(graph(Ts), graph(Ps)), n3_all_members(Ps, Ts))
182
+ fact(n3_all_members([], _))
183
+ implied_by(n3_all_members([P, ...Ps], Ts), [n3_member(P, Ts), n3_all_members(Ps, Ts)])`,
184
+ },
185
+ n3_round: {
186
+ text: `
187
+ // n3_round(E, R): the value of E rounded to an integer, halves upward, so
188
+ // -1.5 is -1; an integer is itself, however large.
189
+ implied_by(n3_round(E, R), [is(X, E), n3_round_(X, R)])
190
+ implied_by(n3_round_(X, X), is_int(X))
191
+ implied_by(n3_round_(X, R), [is_float(X), is(R, floor(add(X, float(0.5))))])`,
192
+ },
193
+ n3_tquot: {
194
+ text: `
195
+ // n3_tquot(X, Y, Q): integer quotient truncated toward zero.
196
+ implied_by(n3_tquot(X, Y, Q), [ge(mul(X, Y), 0), is(Q, floordiv(abs(X), abs(Y)))])
197
+ implied_by(n3_tquot(X, Y, Q), [lt(mul(X, Y), 0), is(Q, neg(floordiv(abs(X), abs(Y))))])`,
198
+ },
199
+ n3_sort: {
200
+ text: `
201
+ // n3_sort(L, S): L in the standard order, without duplicates.
202
+ fact(n3_sort([], []))
203
+ implied_by(n3_sort([X], [X]), 'true')
204
+ implied_by(n3_sort([X, Y, ...R], S), [n3_halve([X, Y, ...R], A, B), n3_sort(A, SA), n3_sort(B, SB), n3_merge(SA, SB, S)])
205
+ fact(n3_halve([], [], []))
206
+ implied_by(n3_halve([X], [X], []), 'true')
207
+ implied_by(n3_halve([X, Y, ...R], [X, ...A], [Y, ...B]), n3_halve(R, A, B))
208
+ fact(n3_merge([], L, L))
209
+ implied_by(n3_merge([X, ...R], [], [X, ...R]), 'true')
210
+ implied_by(n3_merge([X, ...R], [Y, ...T], S), [n3_cmp(O, X, Y), n3_merge_(O, X, R, Y, T, S)])
211
+ implied_by(n3_merge_('<', X, R, Y, T, [X, ...S]), n3_merge(R, [Y, ...T], S))
212
+ implied_by(n3_merge_('=', X, R, _, T, [X, ...S]), n3_merge(R, T, S))
213
+ implied_by(n3_merge_('>', X, R, Y, T, [Y, ...S]), n3_merge([X, ...R], T, S))
214
+ // Numbers compare by value, other terms by the standard order.
215
+ implied_by(n3_cmp(O, X, Y), [n3_numbers(X, Y, F), n3_cmp_(F, O, X, Y)])
216
+ implied_by(n3_numbers(X, Y, F), once(or(and(is_number(X), is_number(Y), unify(F, 'yes')), unify(F, 'no'))))
217
+ implied_by(n3_cmp_('yes', O, X, Y), [lt(X, Y), unify(O, '<')])
218
+ implied_by(n3_cmp_('yes', O, X, Y), [gt(X, Y), unify(O, '>')])
219
+ implied_by(n3_cmp_('yes', O, X, Y), [eq(X, Y), compare(O, X, Y)])
220
+ implied_by(n3_cmp_('no', O, X, Y), compare(O, X, Y))`,
221
+ },
222
+ n3_digits: {
223
+ text: `
224
+ // n3_fixed(X, D, A): A spells X with D digits after the point.
225
+ implied_by(n3_fixed(X, 0, A), [n3_round(X, I), number_chars(I, Cs), atom_chars(A, Cs)])
226
+ implied_by(n3_fixed(X, D, A), [gt(D, 0), n3_round(mul(abs(X), pow(10, D)), J), number_chars(J, Cs), n3_pad(Cs, D, Ps), n3_split_at_end(Ps, D, Int, Frac), n3_sign(X, J, Sign), n3_concat_chars([Sign, Int, ['.'], Frac], Out), atom_chars(A, Out)])
227
+ implied_by(n3_sign(X, J, ['-']), [lt(X, 0), gt(J, 0)])
228
+ implied_by(n3_sign(X, J, []), or(ge(X, 0), eq(J, 0)))
229
+ // n3_pad(Cs, D, Ps): Cs with leading zeros to at least D + 1 digits.
230
+ implied_by(n3_pad(Cs, D, Ps), [n3_length(Cs, N), le(N, D), n3_pad(['0', ...Cs], D, Ps)])
231
+ implied_by(n3_pad(Cs, D, Cs), [n3_length(Cs, N), gt(N, D)])
232
+ implied_by(n3_split_at_end(L, D, A, B), [n3_length(L, N), is(K, sub(N, D)), n3_take(K, L, A, B)])
233
+ implied_by(n3_take(0, L, [], L), 'true')
234
+ implied_by(n3_take(K, [X, ...R], [X, ...A], B), [gt(K, 0), is(K1, sub(K, 1)), n3_take(K1, R, A, B)])
235
+ fact(n3_concat_chars([], []))
236
+ implied_by(n3_concat_chars([L, ...Ls], R), [n3_concat_chars(Ls, R0), n3_append(L, R0, R)])`,
237
+ needs: ['n3_round', 'n3_length', 'n3_append'],
238
+ },
239
+ n3_format: {
240
+ needs: ['n3_str', 'n3_num', 'n3_digits', 'n3_length', 'n3_append'],
241
+ text: `
242
+ // n3_format(Cs, Args, Out): printf-style formatting of %s, %d, %f, %.Nf,
243
+ // %e and %%, with an optional width and - flag for %s and %d.
244
+ fact(n3_format([], [], []))
245
+ implied_by(n3_format(['%', '%', ...R], As, ['%', ...O]), n3_format(R, As, O))
246
+ implied_by(n3_format(['%', ...R], [A, ...As], O), [n3_spec(R, Flags, Width, Prec, Conv, Rest), n3_conv(Conv, Prec, A, Cs), n3_width(Flags, Width, Cs, Ws), n3_format(Rest, As, O0), n3_append(Ws, O0, O)])
247
+ implied_by(n3_format([C, ...R], As, [C, ...O]), [not_identical(C, '%'), n3_format(R, As, O)])
248
+ // A conversion specification: flags, width, precision and the conversion.
249
+ implied_by(n3_spec(Cs, Flag, W, P, Conv, Rest), [n3_flag(Cs, Flag, Cs1), n3_int_prefix(Cs1, W, Cs2), n3_prec(Cs2, P, [Conv, ...Rest])])
250
+ implied_by(n3_flag(['-', ...R], '-', R), 'true')
251
+ implied_by(n3_flag(['0', ...R], '0', R), 'true')
252
+ implied_by(n3_flag([C, ...R], 'none', [C, ...R]), [not_identical(C, '-'), not_identical(C, '0')])
253
+ implied_by(n3_prec(['.', ...R], P, Rest), n3_int_prefix(R, P, Rest))
254
+ implied_by(n3_prec([C, ...R], 'none', [C, ...R]), not_identical(C, '.'))
255
+ implied_by(n3_int_prefix(Cs, N, Rest), [n3_digit_prefix(Cs, Ds, Rest), n3_digits_value(Ds, N)])
256
+ implied_by(n3_digit_prefix([D, ...R], [D, ...Ds], Rest), [n3_digit(D), n3_digit_prefix(R, Ds, Rest)])
257
+ implied_by(n3_digit_prefix([C, ...R], [], [C, ...R]), not(n3_digit(C)))
258
+ fact(n3_digit_prefix([], [], []))
259
+ fact(n3_digits_value([], 'none'))
260
+ implied_by(n3_digits_value([D, ...Ds], N), number_chars(N, [D, ...Ds]))
261
+ ${'0123456789'.split('').map((d) => `fact(n3_digit('${d}'))`).join('\n')}
262
+ implied_by(n3_conv('s', 'none', A, Cs), [n3_str(A, T), atom_chars(T, Cs)])
263
+ implied_by(n3_conv('s', P, A, Cs), [is_int(P), n3_str(A, T), atom_chars(T, All), n3_prefix_n(All, P, Cs)])
264
+ implied_by(n3_conv('d', _, A, Cs), [n3_num(A, N), n3_round(N, I), number_chars(I, Cs)])
265
+ implied_by(n3_conv('f', 'none', A, Cs), [n3_num(A, N), n3_fixed(N, 6, T), atom_chars(T, Cs)])
266
+ implied_by(n3_conv('f', P, A, Cs), [is_int(P), n3_num(A, N), n3_fixed(N, P, T), atom_chars(T, Cs)])
267
+ implied_by(n3_conv('e', _, A, Cs), [n3_num(A, N), is(F, float(N)), number_chars(F, Cs)])
268
+ implied_by(n3_conv('g', _, A, Cs), [n3_num(A, N), number_chars(N, Cs)])
269
+ implied_by(n3_conv('G', _, A, Cs), [n3_num(A, N), number_chars(N, Cs)])
270
+ implied_by(n3_prefix_n(L, N, L), [n3_length(L, M), le(M, N)])
271
+ implied_by(n3_prefix_n(L, N, P), [n3_length(L, M), gt(M, N), n3_take(N, L, P, _)])
272
+ implied_by(n3_width(_, 'none', Cs, Cs), 'true')
273
+ implied_by(n3_width(_, W, Cs, Cs), [is_int(W), n3_length(Cs, N), ge(N, W)])
274
+ implied_by(n3_width(F, W, Cs, Ws), [is_int(W), n3_length(Cs, N), lt(N, W), is(K, sub(W, N)), n3_fill(F, K, Cs, Ws)])
275
+ implied_by(n3_fill('-', K, Cs, Ws), [n3_spaces(K, ' ', Sp), n3_append(Cs, Sp, Ws)])
276
+ implied_by(n3_fill('0', K, ['-', ...Cs], ['-', ...Ws]), [n3_spaces(K, '0', Sp), n3_append(Sp, Cs, Ws)])
277
+ implied_by(n3_fill('0', K, [C, ...Cs], Ws), [not_identical(C, '-'), n3_spaces(K, '0', Sp), n3_append(Sp, [C, ...Cs], Ws)])
278
+ implied_by(n3_fill('none', K, Cs, Ws), [n3_spaces(K, ' ', Sp), n3_append(Sp, Cs, Ws)])
279
+ fact(n3_spaces(0, _, []))
280
+ implied_by(n3_spaces(K, C, [C, ...S]), [gt(K, 0), is(K1, sub(K, 1)), n3_spaces(K1, C, S)])`,
281
+ },
282
+ };
283
+
284
+ // Built-in predicates: IRI -> { goal, needs, text }.
285
+ export const BUILTINS = new Map();
286
+
287
+ function def(iri, goal, text, needs = []) {
288
+ BUILTINS.set(iri, { goal, text, needs });
289
+ }
290
+
291
+ // math: two-operand functions over a list (A B).
292
+ for (const [name, expr] of [['difference', 'sub(X, Y)'], ['exponentiation', 'pow(X, Y)'], ['atan2', 'atan2(X, Y)'],
293
+ ['logarithm', 'log(X, Y)']]) {
294
+ const goal = `n3_math_${name}`;
295
+ def(`${MATH}${name}`, goal, `
296
+ implied_by(${goal}([A, B], O), [n3_num(A, X), n3_num(B, Y), is(V, ${expr}), n3_result(V, O)])`, ['n3_num', 'n3_result']);
297
+ }
298
+ // (A B) math:exponentiation C with B unknown solves for it.
299
+ BUILTINS.get(`${MATH}exponentiation`).text += `
300
+ implied_by(n3_math_exponentiation([A, B], O), [is_var(B), n3_num(A, X), n3_num(O, Z), is(B, log(Z, X))])`;
301
+ BUILTINS.get(`${MATH}exponentiation`).text = BUILTINS.get(`${MATH}exponentiation`).text.replace(
302
+ '[n3_num(A, X), n3_num(B, Y)', '[is_nonvar(B), n3_num(A, X), n3_num(B, Y)');
303
+
304
+ def(`${MATH}sum`, 'n3_math_sum', `
305
+ implied_by(n3_math_sum(L, O), [n3_nums(L, Ns), n3_sum_kind(L, K), n3_sum_value(K, Ns, V), n3_result(V, O)])
306
+ // Decimal inputs add integer coefficients; float and double use binary64.
307
+ implied_by(n3_sum_kind(L, K), once(or(and(n3_member(literal(_, datatype(D)), L), or(unify(D, '${XSD}float'), unify(D, '${XSD}double')), unify(K, 'binary')), unify(K, 'decimal'))))
308
+ implied_by(n3_sum_value('binary', Ns, V), n3_sum(Ns, 0, V))
309
+ implied_by(n3_sum_value('decimal', Ns, V), [n3_decimal_sum(Ns, 0, 0, I, E), n3_sum_result(Ns, I, E, V)])
310
+ fact(n3_decimal_sum([], I, E, I, E))
311
+ implied_by(n3_decimal_sum([N, ...Ns], I, E, J, F), [n3_decimal(N, K, G), n3_decimal_add(I, E, K, G, A, B), n3_decimal_sum(Ns, A, B, J, F)])
312
+ implied_by(n3_sum_result(Ns, I, E, V), [once(or(and(n3_member(N, Ns), is_float(N), unify(K, 'float')), unify(K, 'int'))), n3_sum_number(K, I, E, V)])
313
+ implied_by(n3_sum_number('int', I, E, V), is(V, mul(I, pow(10, E))))
314
+ implied_by(n3_sum_number('float', I, E, V), n3_decimal_value(I, E, V))
315
+ fact(n3_sum([], V, V))
316
+ implied_by(n3_sum([N], 0, N), 'true')
317
+ implied_by(n3_sum([N, M, ...R], 0, V), [is(A, add(N, M)), n3_sum(R, A, V)])
318
+ implied_by(n3_sum([N, ...R], A, V), [not_identical(A, 0), is(B, add(A, N)), n3_sum(R, B, V)])`, ['n3_nums', 'n3_result', 'n3_member', 'n3_decimal']);
319
+ def(`${MATH}product`, 'n3_math_product', `
320
+ implied_by(n3_math_product(L, O), [n3_nums(L, [N, ...Ns]), n3_product(Ns, N, V), n3_result(V, O)])
321
+ implied_by(n3_math_product([], O), n3_result(1, O))
322
+ fact(n3_product([], V, V))
323
+ implied_by(n3_product([N, ...R], A, V), [is(B, mul(A, N)), n3_product(R, B, V)])`, ['n3_nums', 'n3_result']);
324
+ def(`${MATH}quotient`, 'n3_math_quotient', `
325
+ // Integers that divide exactly give an integer.
326
+ implied_by(n3_math_quotient([A, B], O), [n3_num(A, X), n3_num(B, Y), ne(Y, 0), n3_quotient(X, Y, V), n3_result(V, O)])
327
+ implied_by(n3_quotient(X, Y, V), [is_int(X), is_int(Y), eq(mod(X, Y), 0), is(V, floordiv(X, Y))])
328
+ implied_by(n3_quotient(X, Y, V), [not(and(is_int(X), is_int(Y), eq(mod(X, Y), 0))), is(V, div(X, Y))])`, ['n3_num', 'n3_result']);
329
+ def(`${MATH}integerQuotient`, 'n3_math_integerQuotient', `
330
+ implied_by(n3_math_integerQuotient([A, B], O), [n3_num(A, X0), n3_num(B, Y0), ne(Y0, 0), is(X, int(X0)), is(Y, int(Y0)), n3_tquot(X, Y, V), n3_result(V, O)])`, ['n3_num', 'n3_result', 'n3_tquot']);
331
+ def(`${MATH}remainder`, 'n3_math_remainder', `
332
+ implied_by(n3_math_remainder([A, B], O), [n3_num(A, X), n3_num(B, Y), ne(Y, 0), is_int(X), is_int(Y), n3_tquot(X, Y, Q), is(V, sub(X, mul(Y, Q))), n3_result(V, O)])
333
+ implied_by(n3_math_remainder([A, B], O), [n3_num(A, X), n3_num(B, Y), ne(Y, 0), not(and(is_int(X), is_int(Y))), is(V, fmod(X, Y)), n3_result(V, O)])`, ['n3_num', 'n3_result', 'n3_tquot']);
334
+
335
+ // math: one-operand functions, S math:f O, solved forward and, where an
336
+ // inverse exists, backward.
337
+ for (const [name, expr, inverse] of [
338
+ ['negation', 'neg(X)', 'neg(Y)'], ['absoluteValue', 'abs(X)'], ['floor', 'floor(X)'], ['ceiling', 'ceil(X)'],
339
+ ['sin', 'sin(X)', 'asin(Y)'], ['cos', 'cos(X)', 'acos(Y)'], ['tan', 'tan(X)', 'atan(Y)'],
340
+ ['asin', 'asin(X)', 'sin(Y)'], ['acos', 'acos(X)', 'cos(Y)'], ['atan', 'atan(X)', 'tan(Y)'],
341
+ ['sinh', 'sinh(X)', 'log(add(Y, sqrt(add(mul(Y, Y), 1))))'], ['cosh', 'cosh(X)', 'log(add(Y, sqrt(sub(mul(Y, Y), 1))))'],
342
+ ['tanh', 'tanh(X)', 'mul(float(0.5), log(div(add(1, Y), sub(1, Y))))'],
343
+ ['degrees', 'degrees(X)', 'radians(Y)'], ['radians', 'radians(X)', 'degrees(Y)'],
344
+ ['sqrt', 'sqrt(X)'], ['exp', 'exp(X)'], ['ln', 'log(X)'],
345
+ ]) {
346
+ const goal = `n3_math_${name}`;
347
+ let text = `
348
+ implied_by(${goal}(S, O), [is_nonvar(S), n3_num(S, X), is(V, ${expr}), n3_result(V, O)])`;
349
+ if (inverse) text += `
350
+ implied_by(${goal}(S, O), [is_var(S), n3_num(O, Y), is(S, ${inverse})])`;
351
+ def(`${MATH}${name}`, goal, text, ['n3_num', 'n3_result']);
352
+ }
353
+ def(`${MATH}rounded`, 'n3_math_rounded', `
354
+ implied_by(n3_math_rounded(S, O), [n3_num(S, X), n3_round(X, V), n3_result(V, O)])`, ['n3_num', 'n3_result', 'n3_round']);
355
+ for (const [name, fn] of [['min', 'min'], ['max', 'max']]) {
356
+ def(`${MATH}${name}`, `n3_math_${name}`, `
357
+ implied_by(n3_math_${name}(L, O), [n3_nums(L, [N, ...Ns]), n3_${name}(Ns, N, V), n3_result(V, O)])
358
+ fact(n3_${name}([], V, V))
359
+ implied_by(n3_${name}([N, ...R], A, V), [is(B, ${fn}(A, N)), n3_${name}(R, B, V)])`, ['n3_nums', 'n3_result']);
360
+ }
361
+ // Comparisons with "INF" or "-INF" as xsd:float or xsd:double, which
362
+ // eyemind numbers cannot hold, order [Rank, Value] pairs: -INF ranks 0, a
363
+ // number 1, INF 2.
364
+ HELPERS.n3_infinite = {
365
+ needs: ['n3_num'],
366
+ text: `
367
+ implied_by(n3_extended(T, [1, V]), n3_num(T, V))
368
+ implied_by(n3_extended(literal('INF', datatype(D)), [2, 0]), n3_float_type(D))
369
+ implied_by(n3_extended(literal('-INF', datatype(D)), [0, 0]), n3_float_type(D))
370
+ fact(n3_float_type('${XSD}float'), n3_float_type('${XSD}double'))
371
+ implied_by(n3_infinite_order(S, O, C), [is_nonvar(S), is_nonvar(O), n3_extended(S, [R1, V1]), n3_extended(O, [R2, V2]), once(or(ne(R1, 1), ne(R2, 1))), compare(C, [R1, V1], [R2, V2])])`,
372
+ };
373
+ for (const [name, op, orders] of [['greaterThan', 'gt', ['>']], ['lessThan', 'lt', ['<']], ['notGreaterThan', 'le', ['<', '=']],
374
+ ['notLessThan', 'ge', ['>', '=']], ['equalTo', 'eq', ['=']], ['notEqualTo', 'ne', ['<', '>']]]) {
375
+ def(`${MATH}${name}`, `n3_math_${name}`, `
376
+ implied_by(n3_math_${name}(S, O), [n3_num(S, X), n3_num(O, Y), ${op}(X, Y)])
377
+ implied_by(n3_math_${name}(S, O), [n3_infinite_order(S, O, C), ${orders.length === 1 ? `unify(C, '${orders[0]}')` : `or(unify(C, '${orders[0]}'), unify(C, '${orders[1]}'))`}])
378
+ // (A B) math:${name} true compares A and B.
379
+ implied_by(n3_math_${name}([A, B], literal('true', datatype('${XSD}boolean'))), [n3_num(A, X), n3_num(B, Y), ${op}(X, Y)])`, ['n3_num', 'n3_infinite']);
380
+ }
381
+
382
+ // string: regular expressions, eyemind's ECMAScript patterns.
383
+ def(`${STRING}matches`, 'n3_string_matches', `
384
+ implied_by(n3_string_matches(S, O), [n3_str(S, A), n3_str(O, P), regex_match(P, A)])`, ['n3_str']);
385
+ def(`${STRING}notMatches`, 'n3_string_notMatches', `
386
+ implied_by(n3_string_notMatches(S, O), [n3_str(S, A), n3_str(O, P), not(regex_match(P, A))])`, ['n3_str']);
387
+ def(`${STRING}replace`, 'n3_string_replace', `
388
+ // (Text Pattern Replacement) string:replace Result, every match replaced.
389
+ implied_by(n3_string_replace([S, P0, R0], O), [n3_str(S, A), n3_str(P0, Raw), n3_regex_pattern(Raw, P), n3_str(R0, R), atom_chars(R, Cs), n3_replacement(Cs, Ds), atom_chars(D, Ds), regex_replace(P, A, D, B), n3_str_result(B, O)])
390
+ // A backslash escapes a dollar sign or backslash in N3 replacements;
391
+ // ECMAScript spells a literal dollar sign as $$.
392
+ fact(n3_replacement([], []))
393
+ implied_by(n3_replacement([${JSON.stringify('\\')}, '$', ...R], ['$', '$', ...U]), n3_replacement(R, U))
394
+ implied_by(n3_replacement([${JSON.stringify('\\')}, ${JSON.stringify('\\')}, ...R], [${JSON.stringify('\\')}, ...U]), n3_replacement(R, U))
395
+ implied_by(n3_replacement([C, ...R], [C, ...U]), [not_unify([C, ...R], [${JSON.stringify('\\')}, '$', ..._]), not_unify([C, ...R], [${JSON.stringify('\\')}, ${JSON.stringify('\\')}, ..._]), n3_replacement(R, U)])`, ['n3_str', 'n3_str_result', 'n3_pattern']);
396
+ def(`${STRING}scrape`, 'n3_string_scrape', `
397
+ // (Text Pattern) string:scrape Group: the first group of the first match.
398
+ implied_by(n3_string_scrape([S, P0], O), [n3_str(S, A), n3_str(P0, P), once(regex_find(P, A, [_, G, ..._])), n3_str_result(G, O)])`, ['n3_str', 'n3_str_result']);
399
+
400
+ // string:
401
+ def(`${STRING}concatenation`, 'n3_string_concatenation', `
402
+ implied_by(n3_string_concatenation(L, O), [n3_strs(L, As), n3_concat(As, A), n3_str_result(A, O)])
403
+ fact(n3_concat([], ''))
404
+ implied_by(n3_concat([A, ...R], C), [n3_concat(R, B), atom_concat(A, B, C)])`, ['n3_strs', 'n3_str_result']);
405
+ def(`${STRING}length`, 'n3_string_length', `
406
+ implied_by(n3_string_length(S, O), [n3_str(S, A), atom_length(A, N), n3_result(N, O)])`, ['n3_str', 'n3_result']);
407
+ def(`${STRING}contains`, 'n3_string_contains', `
408
+ implied_by(n3_string_contains(S, O), [n3_str(S, A), n3_str(O, B), n3_contains(A, B)])`, ['n3_str', 'n3_contains']);
409
+ def(`${STRING}containsIgnoringCase`, 'n3_string_containsIgnoringCase', `
410
+ implied_by(n3_string_containsIgnoringCase(S, O), [n3_str(S, A0), n3_str(O, B0), n3_lower(A0, A), n3_lower(B0, B), n3_contains(A, B)])`, ['n3_str', 'n3_contains', 'n3_lower']);
411
+ def(`${STRING}startsWith`, 'n3_string_startsWith', `
412
+ implied_by(n3_string_startsWith(S, O), [n3_str(S, A), n3_str(O, B), once(atom_concat(B, _, A))])`, ['n3_str']);
413
+ def(`${STRING}endsWith`, 'n3_string_endsWith', `
414
+ implied_by(n3_string_endsWith(S, O), [n3_str(S, A), n3_str(O, B), once(atom_concat(_, B, A))])`, ['n3_str']);
415
+ def(`${STRING}equalIgnoringCase`, 'n3_string_equalIgnoringCase', `
416
+ implied_by(n3_string_equalIgnoringCase(S, O), [n3_str(S, A0), n3_str(O, B0), n3_lower(A0, A), n3_lower(B0, A)])`, ['n3_str', 'n3_lower']);
417
+ def(`${STRING}notEqualIgnoringCase`, 'n3_string_notEqualIgnoringCase', `
418
+ implied_by(n3_string_notEqualIgnoringCase(S, O), [n3_str(S, A0), n3_str(O, B0), n3_lower(A0, A), n3_lower(B0, B), not_identical(A, B)])`, ['n3_str', 'n3_lower']);
419
+ def(`${STRING}upperCase`, 'n3_string_upperCase', `
420
+ implied_by(n3_string_upperCase(S, O), [n3_str(S, A), n3_upper(A, U), n3_str_result(U, O)])`, ['n3_str', 'n3_lower', 'n3_str_result']);
421
+ def(`${STRING}lowerCase`, 'n3_string_lowerCase', `
422
+ implied_by(n3_string_lowerCase(S, O), [n3_str(S, A), n3_lower(A, L), n3_str_result(L, O)])`, ['n3_str', 'n3_lower', 'n3_str_result']);
423
+ for (const [name, test] of [['lessThan', "unify(C, '<')"], ['greaterThan', "unify(C, '>')"], ['notLessThan', "not_identical(C, '<')"],
424
+ ['notGreaterThan', "not_identical(C, '>')"], ['equalTo', "unify(C, '=')"], ['notEqualTo', "not_identical(C, '=')"]]) {
425
+ def(`${STRING}${name}`, `n3_string_${name}`, `
426
+ implied_by(n3_string_${name}(S, O), [n3_str(S, A), n3_str(O, B), compare(C, A, B), ${test}])`, ['n3_str']);
427
+ }
428
+ def(`${STRING}substring`, 'n3_string_substring', `
429
+ // (S Start Length) string:substring O, counting from 1.
430
+ implied_by(n3_string_substring([S, I], O), [n3_str(S, A), atom_length(A, N), n3_num(I, I0), is(L, sub(add(N, 1), I0)), n3_string_substring([S, I, L], O)])
431
+ implied_by(n3_string_substring([S, I, L], O), [n3_str(S, A), n3_num(I, I0), n3_num(L, L0), atom_chars(A, Cs), is(K, max(0, sub(I0, 1))), n3_take(K, Cs, _, Rest), n3_prefix_n(Rest, L0, Sub), atom_chars(T, Sub), n3_str_result(T, O)])`, ['n3_str', 'n3_num', 'n3_str_result', 'n3_digits', 'n3_format']);
432
+ // (S I) string:charAt C and (S I C) string:setCharAt T, counting from 0; an
433
+ // index out of range gives "" and leaves S as it is.
434
+ def(`${STRING}charAt`, 'n3_string_charAt', `
435
+ implied_by(n3_string_charAt([S, I0], O), [n3_str(S, A), n3_num(I0, N), is(I, trunc(N)), atom_chars(A, Cs), n3_char_at(Cs, I, C), n3_str_result(C, O)])
436
+ implied_by(n3_char_at(Cs, I, C), [n3_length(Cs, L), n3_in_range(I, L, F), n3_char_at_(F, Cs, I, C)])
437
+ implied_by(n3_char_at_('yes', Cs, I, C), n3_take(I, Cs, _, [C, ..._]))
438
+ implied_by(n3_char_at_('no', _, _, ''), 'true')
439
+ implied_by(n3_in_range(I, L, F), once(or(and(ge(I, 0), lt(I, L), unify(F, 'yes')), unify(F, 'no'))))`,
440
+ ['n3_str', 'n3_num', 'n3_str_result', 'n3_length', 'n3_digits']);
441
+ def(`${STRING}setCharAt`, 'n3_string_setCharAt', `
442
+ implied_by(n3_string_setCharAt([S, I0, C0], O), [n3_str(S, A), n3_num(I0, N), is(I, trunc(N)), n3_str(C0, C), atom_chars(A, Cs), atom_chars(C, Ds), n3_length(Cs, L), n3_in_range(I, L, F), n3_set_at(F, Cs, I, Ds, Out), atom_chars(T, Out), n3_str_result(T, O)])
443
+ implied_by(n3_set_at('no', Cs, _, _, Cs), 'true')
444
+ implied_by(n3_set_at('yes', Cs, I, Ds, Out), [n3_take(I, Cs, Pre, [_, ...Post]), n3_first_char(Ds, Fs), n3_append(Fs, Post, Rest), n3_append(Pre, Rest, Out)])
445
+ fact(n3_first_char([], []))
446
+ implied_by(n3_first_char([D, ..._], [D]), 'true')
447
+ implied_by(n3_in_range(I, L, F), once(or(and(ge(I, 0), lt(I, L), unify(F, 'yes')), unify(F, 'no'))))`,
448
+ ['n3_str', 'n3_num', 'n3_str_result', 'n3_length', 'n3_digits', 'n3_append']);
449
+
450
+ def(`${STRING}format`, 'n3_string_format', `
451
+ implied_by(n3_string_format([F, ...Args], O), [n3_str(F, A), atom_chars(A, Cs), n3_format(Cs, Args, Out), atom_chars(T, Out), n3_str_result(T, O)])`, ['n3_str', 'n3_str_result', 'n3_format']);
452
+
453
+ // list:
454
+ def(`${LIST}first`, 'n3_list_first', `
455
+ implied_by(n3_list_first(L, X), [is_nonvar(L), unify(L, [X, ..._])])`);
456
+ def(`${LIST}rest`, 'n3_list_rest', `
457
+ implied_by(n3_list_rest(L, R), [is_nonvar(L), unify(L, [_, ...R])])`);
458
+ def(`${LIST}firstRest`, 'n3_list_firstRest', `
459
+ implied_by(n3_list_firstRest([X, ...R], [X, R]), 'true')`);
460
+ def(`${LIST}last`, 'n3_list_last', `
461
+ implied_by(n3_list_last(L, X), [is_nonvar(L), n3_last(L, X)])
462
+ implied_by(n3_last([X], X), 'true')
463
+ implied_by(n3_last([_, Y, ...R], X), n3_last([Y, ...R], X))`);
464
+ def(`${LIST}length`, 'n3_list_length', `
465
+ implied_by(n3_list_length(L, O), [is_nonvar(L), n3_length(L, N), n3_integer_result(N, O)])
466
+ implied_by(n3_integer_result(N, O), [is_var(O), unify(O, N)])
467
+ implied_by(n3_integer_result(N, O), [is_nonvar(O), n3_num(O, V), is_int(V), eq(N, V)])`, ['n3_length', 'n3_num']);
468
+ def(`${LIST}member`, 'n3_list_member', `
469
+ implied_by(n3_list_member(L, X), [is_nonvar(L), n3_member(X, L)])`, ['n3_member']);
470
+ def(`${LIST}in`, 'n3_list_in', `
471
+ implied_by(n3_list_in(X, L), [is_nonvar(L), n3_member(X, L)])`, ['n3_member']);
472
+ def(`${LIST}notMember`, 'n3_list_notMember', `
473
+ implied_by(n3_list_notMember(L, X), not(n3_member(X, L)))`, ['n3_member']);
474
+ def(`${LIST}append`, 'n3_list_append', `
475
+ // (L1 ... Ln) list:append L; with L given and two lists, every split.
476
+ implied_by(n3_list_append(Ls, L), [is_ground(Ls), n3_appends(Ls, R), n3_list_result(R, L)])
477
+ implied_by(n3_list_append(Ls, L), [is_nonvar(Ls), unify(Ls, [A, B]), n3_ground_flag([A, B], F), unify(F, 'no'), is_nonvar(L), n3_append(A, B, L)])
478
+ // n3_ground_flag(T, F): F is yes or no as T is ground, decided without
479
+ // negation, which needs bound variables.
480
+ implied_by(n3_ground_flag(T, F), once(or(and(is_ground(T), unify(F, 'yes')), unify(F, 'no'))))
481
+ fact(n3_appends([], []))
482
+ implied_by(n3_appends([L, ...Ls], R), [n3_appends(Ls, R0), n3_append(L, R0, R)])`, ['n3_append', 'n3_list_result']);
483
+ def(`${LIST}memberAt`, 'n3_list_memberAt', `
484
+ // (L I) list:memberAt X, counting from 0.
485
+ implied_by(n3_list_memberAt([L, I], X), [is_nonvar(L), n3_member_at(L, 0, I, X)])`, ['n3_member_at']);
486
+ def(`${LIST}iterate`, 'n3_list_iterate', `
487
+ // L list:iterate (I X): X is at index I, counting from 0.
488
+ implied_by(n3_list_iterate(L, [I, X]), [is_nonvar(L), n3_member_at(L, 0, I, X)])`, ['n3_member_at']);
489
+ def(`${LIST}remove`, 'n3_list_remove', `
490
+ // (L X) list:remove R: R is L without any element equal to X.
491
+ implied_by(n3_list_remove([L, X], R), [is_ground(L), n3_remove(L, X, R)])
492
+ fact(n3_remove([], _, []))
493
+ implied_by(n3_remove([Y, ...T], X, R), [unify(X, Y), n3_remove(T, X, R)])
494
+ implied_by(n3_remove([Y, ...T], X, [Y, ...R]), [not_unify(X, Y), n3_remove(T, X, R)])`);
495
+ def(`${LIST}reverse`, 'n3_list_reverse', `
496
+ implied_by(n3_list_reverse(L, R), [is_nonvar(L), n3_reverse(L, [], R)])
497
+ fact(n3_reverse([], A, A))
498
+ implied_by(n3_reverse([X, ...T], A, R), n3_reverse(T, [X, ...A], R))`);
499
+ def(`${LIST}sort`, 'n3_list_sort', `
500
+ implied_by(n3_list_sort(L, S), [is_nonvar(L), n3_sort(L, S)])`, ['n3_sort']);
501
+ def(`${LIST}unique`, 'n3_list_unique', `
502
+ implied_by(n3_list_unique(L, S), [is_nonvar(L), n3_unique(L, [], S)])
503
+ fact(n3_unique([], _, []))
504
+ implied_by(n3_unique([X, ...T], Seen, R), [n3_member(X, Seen), n3_unique(T, Seen, R)])
505
+ implied_by(n3_unique([X, ...T], Seen, [X, ...R]), [not(n3_member(X, Seen)), n3_unique(T, [X, ...Seen], R)])`, ['n3_member']);
506
+
507
+ // rdf:first and rdf:rest also take a collection apart.
508
+ def(`${RDF}first`, 'n3_rdf_first', `
509
+ implied_by(n3_rdf_first(S, O), rdf(S, iri('${RDF}first'), O))
510
+ implied_by(n3_rdf_first(S, O), [is_compound(S), unify(S, [O, ..._])])`);
511
+ def(`${RDF}rest`, 'n3_rdf_rest', `
512
+ implied_by(n3_rdf_rest(S, O), rdf(S, iri('${RDF}rest'), O))
513
+ implied_by(n3_rdf_rest(S, O), [is_compound(S), unify(S, [_, ...O])])`);
514
+
515
+ // time: the parts of an xsd:dateTime or xsd:date, YYYY-MM-DDThh:mm:ss.
516
+ const TIME = 'http://www.w3.org/2000/10/swap/time#';
517
+ for (const [name, from, length] of [['year', 0, 4], ['month', 5, 2], ['day', 8, 2], ['hour', 11, 2], ['minute', 14, 2], ['second', 17, 2]]) {
518
+ def(`${TIME}${name}`, `n3_time_${name}`, `
519
+ implied_by(n3_time_${name}(S, O), [n3_str(S, A), atom_chars(A, Cs), n3_take(${from}, Cs, _, R), n3_take(${length}, R, Ds, _), number_chars(N, Ds), n3_result(N, O)])`,
520
+ ['n3_str', 'n3_result', 'n3_digits']);
521
+ }
522
+ def(`${TIME}timeZone`, 'n3_time_timeZone', `
523
+ implied_by(n3_time_timeZone(S, O), [n3_str(S, A), atom_chars(A, Cs), n3_take(19, Cs, _, R), n3_zone(R, Z), atom_chars(T, Z), n3_str_result(T, O)])
524
+ implied_by(n3_zone(['.', ...R], Z), [n3_digit_prefix(R, _, Rest), n3_zone(Rest, Z)])
525
+ implied_by(n3_zone([C, ...R], [C, ...R]), not_identical(C, '.'))`, ['n3_str', 'n3_str_result', 'n3_digits', 'n3_format']);
526
+
527
+ // crypto: digests of a string's UTF-8 bytes, in lowercase hexadecimal.
528
+ const CRYPTO = 'http://www.w3.org/2000/10/swap/crypto#';
529
+ for (const [name, algorithm] of [['md5', 'md5'], ['sha', 'sha1'], ['sha1', 'sha1'], ['sha256', 'sha256'], ['sha512', 'sha512']]) {
530
+ def(`${CRYPTO}${name}`, `n3_crypto_${name}`, `
531
+ implied_by(n3_crypto_${name}(S, O), [n3_str(S, A), digest('${algorithm}', A, H), n3_str_result(H, O)])`, ['n3_str', 'n3_str_result']);
532
+ }
533
+
534
+ // log:skolem names a ground term by the SHA-256 of its canonical text, so the
535
+ // same term always gets the same IRI and different terms different ones.
536
+ def(`${LOG}skolem`, 'n3_log_skolem', `
537
+ implied_by(n3_log_skolem(S, O), [is_ground(S), term_text(S, T), digest('sha256', T, H),
538
+ atom_concat('https://eyereasoner.github.io/.well-known/genid/', H, I), unify(O, iri(I))])`);
539
+
540
+ // F log:conclusion C: C is the deductive closure of F, worked out while
541
+ // compiling for every formula the document states (n3_conclusion_of facts);
542
+ // a formula only built while reasoning has none.
543
+ def(`${LOG}conclusion`, 'n3_log_conclusion', `
544
+ implied_by(n3_log_conclusion(F, O), [is_nonvar(F), n3_conclusion_of(F, C), n3_conclusion_result(C, O)])`, ['n3_conclusion_result']);
545
+
546
+ // The documents a program names, read before compiling: their formula,
547
+ // text, or the error that kept them from being read.
548
+ def(`${LOG}semantics`, 'n3_log_semantics', `
549
+ implied_by(n3_log_semantics(S, O), [is_nonvar(S), n3_semantics(S, G), n3_conclusion_result(G, O)])`, ['n3_conclusion_result']);
550
+ def(`${LOG}semanticsOrError`, 'n3_log_semanticsOrError', `
551
+ implied_by(n3_log_semanticsOrError(S, O), [is_nonvar(S), n3_semantics(S, G), n3_conclusion_result(G, O)])
552
+ implied_by(n3_log_semanticsOrError(S, O), [is_nonvar(S), n3_document_error(S, E), unify(O, literal(E, datatype('${XSD}string')))])`, ['n3_conclusion_result']);
553
+ def(`${LOG}parsedAsN3`, 'n3_log_parsedAsN3', `
554
+ implied_by(n3_log_parsedAsN3(S, O), [is_nonvar(S), n3_parsed(S, G), n3_conclusion_result(G, O)])`, ['n3_conclusion_result']);
555
+ def(`${LOG}content`, 'n3_log_content', `
556
+ implied_by(n3_log_content(S, O), [is_nonvar(S), n3_content(S, T), n3_str_result(T, O)])`, ['n3_str_result']);
557
+
558
+ // (F1 ... Fn) log:conjunction F: F holds the triples of every Fi, once each.
559
+ def(`${LOG}conjunction`, 'n3_log_conjunction', `
560
+ implied_by(n3_log_conjunction(L, O), [is_ground(L), n3_conj(L, Ts), n3_sort(Ts, S), n3_conj_result(S, O)])
561
+ fact(n3_conj([], []))
562
+ implied_by(n3_conj([graph(Ts), ...R], All), [n3_conj(R, Rest), n3_append(Ts, Rest, All)])
563
+ implied_by(n3_conj([literal('true', datatype('${XSD}boolean')), ...R], All), n3_conj(R, All))
564
+ implied_by(n3_conj_result(S, O), [is_var(O), unify(O, graph(S))])
565
+ implied_by(n3_conj_result(S, O), [is_nonvar(O), n3_triple_set(O, S)])`, ['n3_sort', 'n3_append', 'n3_formulas']);
566
+
567
+ // log:
568
+ // Formulas are equal when they hold the same triples, in any order.
569
+ def(`${LOG}equalTo`, 'n3_log_equalTo', `
570
+ implied_by(n3_log_equalTo(A, B), [n3_formulas(A, B, F), n3_equal(F, A, B)])
571
+ implied_by(n3_equal('yes', A, B), n3_same_formula(A, B))
572
+ implied_by(n3_equal('no', A, B), unify(A, B))`, ['n3_formulas']);
573
+ def(`${LOG}notEqualTo`, 'n3_log_notEqualTo', `
574
+ implied_by(n3_log_notEqualTo(A, B), [n3_formulas(A, B, F), n3_not_equal(F, A, B)])
575
+ // An empty collection rather than a negation, so a formula's own variables
576
+ // need not be bound.
577
+ implied_by(n3_not_equal('yes', A, B), findall('same', n3_same_formula(A, B), []))
578
+ implied_by(n3_not_equal('no', A, B), not_unify(A, B))`, ['n3_formulas']);
579
+ def(`${LOG}uri`, 'n3_log_uri', `
580
+ implied_by(n3_log_uri(S, O), [is_nonvar(S), unify(S, iri(U)), n3_str_result(U, O)])
581
+ implied_by(n3_log_uri(S, O), [is_var(S), n3_str(O, U), not(regex_match(${JSON.stringify(INVALID_IRI)}, U)), unify(S, iri(U))])`, ['n3_str', 'n3_str_result']);
582
+ def(`${LOG}rawType`, 'n3_log_rawType', `
583
+ implied_by(n3_log_rawType(S, O), [is_nonvar(S), once(n3_raw_type(S, T)), unify(O, iri(T))])
584
+ implied_by(n3_raw_type(graph(_), '${LOG}Formula'), 'true')
585
+ implied_by(n3_raw_type(literal(_, _), '${LOG}Literal'), 'true')
586
+ implied_by(n3_raw_type(S, '${LOG}Literal'), is_number(S))
587
+ implied_by(n3_raw_type([], '${RDF}List'), 'true')
588
+ implied_by(n3_raw_type([_, ..._], '${RDF}List'), 'true')
589
+ implied_by(n3_raw_type(iri(_), '${LOG}Other'), 'true')
590
+ implied_by(n3_raw_type(bnode(_, _), '${LOG}LabeledBlankNode'), 'true')
591
+ implied_by(n3_raw_type(skolem(_, _, _), '${LOG}UnlabeledBlankNode'), 'true')`);
592
+ def(`${LOG}dtlit`, 'n3_log_dtlit', `
593
+ // (Lexical Datatype) log:dtlit Literal.
594
+ implied_by(n3_log_dtlit([S, iri(D)], O), [is_nonvar(S), n3_str(S, A), n3_typed(A, D, O)])
595
+ implied_by(n3_log_dtlit([L, D], O), [is_nonvar(O), n3_lit_parts(O, A, U), unify(L, literal(A, datatype('${XSD}string'))), unify(D, iri(U))])
596
+ // xsd:integer, xsd:decimal and xsd:double with a valid lexical form are
597
+ // numbers, as when they are read.
598
+ implied_by(n3_typed(A, D, O), [n3_number_type(D, K), atom_chars(A, Cs), number_chars(N, Cs), n3_number_kind(N, K), n3_typed_number(K, A, D, N, O)])
599
+ implied_by(n3_typed(A, D, literal(A, datatype(D))), not(and(n3_number_type(D, _1), atom_chars(A, _2), number_chars(_3, _2), n3_number_kind(_3, _1))))
600
+ fact(n3_number_type('${XSD}integer', 'int'), n3_number_type('${XSD}decimal', 'float'), n3_number_type('${XSD}double', 'typed'), n3_number_type('${XSD}float', 'typed'))
601
+ implied_by(n3_number_kind(N, 'int'), is_int(N))
602
+ implied_by(n3_number_kind(N, 'float'), is_number(N))
603
+ implied_by(n3_number_kind(N, 'typed'), is_number(N))
604
+ implied_by(n3_typed_number('int', _, _, N, O), unify(N, O))
605
+ implied_by(n3_typed_number('float', _, _, N, O), [is(V, float(N)), unify(V, O)])
606
+ implied_by(n3_typed_number('typed', A, D, _, O), [is_var(O), unify(O, literal(A, datatype(D)))])
607
+ implied_by(n3_typed_number('typed', _, D, N, O), [is_nonvar(O), unify(O, literal(_, datatype(D))), n3_num(O, V), eq(N, V)])
608
+ implied_by(n3_lit_parts(literal(A, datatype(U)), A, U), 'true')
609
+ implied_by(n3_lit_parts(literal(A, lang(_)), A, '${RDF}langString'), 'true')
610
+ implied_by(n3_lit_parts(literal(A, lang(_, _)), A, '${RDF}dirLangString'), 'true')
611
+ implied_by(n3_lit_parts(N, A, '${XSD}integer'), [is_int(N), number_chars(N, Cs), atom_chars(A, Cs)])
612
+ implied_by(n3_lit_parts(N, A, '${XSD}decimal'), [is_float(N), number_chars(N, Cs), atom_chars(A, Cs)])`, ['n3_str', 'n3_num']);
613
+ def(`${LOG}langlit`, 'n3_log_langlit', `
614
+ // (Lexical Language) log:langlit Literal.
615
+ implied_by(n3_log_langlit([S, T], O), [is_nonvar(S), n3_str(S, A), n3_str(T, L), unify(O, literal(A, lang(L)))])
616
+ implied_by(n3_log_langlit([S, T], O), [is_var(S), unify(O, literal(A, lang(L))), unify(S, literal(A, datatype('${XSD}string'))), unify(T, literal(L, datatype('${XSD}string')))])`, ['n3_str']);
617
+
618
+ // Built-ins that need what eyemind deliberately leaves out: clocks and printing.
619
+ export const UNSUPPORTED = new Map([
620
+ [`${LOG}trace`, 'printing while reasoning'],
621
+ ]);
622
+ for (const name of ['localTime', 'gmTime', 'currentTime']) UNSUPPORTED.set(`http://www.w3.org/2000/10/swap/time#${name}`, 'clock');