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