eyemind 0.5.0 → 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.
@@ -0,0 +1,584 @@
1
+ // SPARQL expressions and aggregates as eyemind backward rules.
2
+ //
3
+ // An expression compiles to goals that bind its value; a function is a goal
4
+ // srl_<name>(Arguments..., Result) whose definition is written here in
5
+ // eyemind itself, so the compiled program is self-contained and every
6
+ // function step is an ordinary, checkable derivation. A SPARQL error is a
7
+ // goal that fails: FILTER then rejects the solution and SET drops it, as
8
+ // SPARQL 1.2 RL evaluates them. A definition lists the definitions it needs;
9
+ // the compiler copies those a program uses into it.
10
+ //
11
+ // Values are RDF terms as the compiler writes them: an xsd:integer with a
12
+ // canonical lexical form is an eyemind integer, every other literal is
13
+ // literal(Lexical, datatype(IRI)) or literal(Text, lang(Tag)). Decimals,
14
+ // floats and doubles are computed as binary64 floats and written back with
15
+ // their datatype.
16
+
17
+ const XSD = 'http://www.w3.org/2001/XMLSchema#';
18
+ const RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
19
+
20
+ const INTEGER_TYPES = ['integer', 'int', 'long', 'short', 'byte', 'nonNegativeInteger', 'positiveInteger',
21
+ 'nonPositiveInteger', 'negativeInteger', 'unsignedLong', 'unsignedInt', 'unsignedShort', 'unsignedByte'];
22
+
23
+ const str = (text) => `literal(${text}, datatype('${XSD}string'))`;
24
+ const TRUE = `literal('true', datatype('${XSD}boolean'))`;
25
+ const FALSE = `literal('false', datatype('${XSD}boolean'))`;
26
+
27
+ // The test name_(Args) as a boolean: name_t(Args, T) gives T 'true' when
28
+ // the test holds and 'false' when it does not.
29
+ const tested = (name, args) => String.raw`
30
+ implied_by(${name}_t(${args}, T), or([${name}_(${args}), unify(T, 'true')], [not(${name}_(${args})), unify(T, 'false')]))`;
31
+
32
+ export const DEFINITIONS = {
33
+ // Numbers.
34
+ srl_num: {
35
+ text: String.raw`
36
+ // srl_num(T, K, V): T is a number of kind K (integer, decimal, float or
37
+ // double) with value V; decimals, floats and doubles have float values.
38
+ 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), atom_chars(L, Cs), number_chars(N, Cs), srl_num_value(K, N, V)])
40
+ ${INTEGER_TYPES.map((t) => `fact(srl_numeric_type('${XSD}${t}', 'integer'))`).join('\n')}
41
+ fact(srl_numeric_type('${XSD}decimal', 'decimal'), srl_numeric_type('${XSD}float', 'float'), srl_numeric_type('${XSD}double', 'double'))
42
+ implied_by(srl_num_value('integer', N, N), is_int(N))
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'))
45
+ // Numeric type promotion: integer, decimal, float, double.
46
+ fact(srl_rank('integer', 0), srl_rank('decimal', 1), srl_rank('float', 2), srl_rank('double', 3))
47
+ implied_by(srl_promote(A, B, K), [srl_rank(A, I), srl_rank(B, J), is(M, max(I, J)), srl_rank(K, M)])`,
48
+ },
49
+ srl_make: {
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)])
55
+ implied_by(srl_float_text('double', A, A), 'true')
56
+ implied_by(srl_float_text('float', A, A), 'true')
57
+ implied_by(srl_float_text('decimal', A, A), not(regex_match('e', A)))
58
+ implied_by(srl_float_text('decimal', A, L), [regex_find('^(-?)(\\d+)(?:\\.(\\d+))?e([+-]?\\d+)$', A, [_, S, I, F, E]), atom_concat(I, F, Ds), atom_length(I, NI), atom_chars(E, ECs), number_chars(X, ECs), is(P, add(NI, X)), atom_length(Ds, ND), srl_point(Ds, ND, P, B), atom_concat(S, B, L)])
59
+ // srl_point(Ds, N, P, B): the N digits Ds with a decimal point after P of them.
60
+ implied_by(srl_point(Ds, N, P, B), [ge(P, N), is(Z, sub(P, N)), srl_zeros(Z, Zs), atom_concat(Ds, Zs, H), atom_concat(H, '.0', B)])
61
+ 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
+ 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
+ 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)])`,
65
+ needs: ['srl_num'],
66
+ },
67
+ srl_finite: {
68
+ text: String.raw`
69
+ // srl_finite(Op, X, Y): Op on X and Y has a finite result, so computing it
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'] },
84
+ srl_div: {
85
+ text: String.raw`
86
+ // Division by zero is an error; integers divide to a decimal.
87
+ implied_by(srl_div(A, B, R), [srl_num(A, KA, X), srl_num(B, KB, Y), ne(Y, 0), srl_promote(KA, KB, K0), srl_div_kind(K0, K), srl_finite('div', X, Y), is(V, div(X, Y)), srl_make(K, V, R)])
88
+ 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', 'srl_finite'],
90
+ },
91
+ srl_neg: {
92
+ text: String.raw`
93
+ implied_by(srl_neg(A, R), [srl_num(A, K, X), is(V, neg(X)), srl_make(K, V, R)])`,
94
+ needs: ['srl_make'],
95
+ },
96
+ srl_pos: {
97
+ text: String.raw`
98
+ implied_by(srl_pos(A, A), srl_num(A, _, _))`,
99
+ needs: ['srl_num'],
100
+ },
101
+
102
+ // Booleans and effective boolean values.
103
+ srl_bool: {
104
+ text: String.raw`
105
+ // srl_bool(T, B): B is the xsd:boolean term for 'true' or 'false'.
106
+ fact(srl_bool('true', ${TRUE}), srl_bool('false', ${FALSE}))
107
+ fact(srl_flip('true', 'false'), srl_flip('false', 'true'))`,
108
+ },
109
+ srl_ebv: {
110
+ text: String.raw`
111
+ // srl_ebv(T, B): the effective boolean value of T is B, 'true' or 'false';
112
+ // a term without one, such as an IRI, has none.
113
+ implied_by(srl_ebv(T, B), once(srl_ebv_(T, B)))
114
+ 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')])])
116
+ implied_by(srl_ebv_(literal(L, datatype('${XSD}string')), B), srl_nonempty(L, B))
117
+ implied_by(srl_ebv_(literal(L, lang(_)), B), srl_nonempty(L, B))
118
+ implied_by(srl_ebv_(literal(L, lang(_, _)), B), srl_nonempty(L, B))
119
+ implied_by(srl_nonempty(L, B), [atom_length(L, N), or([eq(N, 0), unify(B, 'false')], [gt(N, 0), unify(B, 'true')])])
120
+ // An invalid boolean lexical form has the value false.
121
+ implied_by(srl_boolean_text(L, B), once(or([srl_true_text(L), unify(B, 'true')], unify(B, 'false'))))
122
+ fact(srl_true_text('true'), srl_true_text('1'), srl_false_text('false'), srl_false_text('0'))
123
+ implied_by(srl_true(T), srl_ebv(T, 'true'))
124
+ // srl_ebv3(T, B): B is the effective boolean value, or 'error'.
125
+ implied_by(srl_ebv3(T, B), once(or(srl_ebv(T, B), unify(B, 'error'))))`,
126
+ needs: ['srl_num'],
127
+ },
128
+ srl_not: {
129
+ text: String.raw`
130
+ implied_by(srl_not(A, R), [srl_ebv(A, B), srl_flip(B, F), srl_bool(F, R)])`,
131
+ needs: ['srl_ebv', 'srl_bool'],
132
+ },
133
+ srl_and: {
134
+ text: String.raw`
135
+ // && and || over 'true', 'false' and 'error': an error is absorbed only by
136
+ // a value that decides the result alone.
137
+ implied_by(srl_and(A, B, R), [srl_and3(A, B, T), srl_bool(T, R)])
138
+ fact(srl_and3('true', 'true', 'true'), srl_and3('false', _, 'false'), srl_and3('true', 'false', 'false'), srl_and3('error', 'false', 'false'))`,
139
+ needs: ['srl_bool'],
140
+ },
141
+ srl_or: {
142
+ text: String.raw`
143
+ implied_by(srl_or(A, B, R), [srl_or3(A, B, T), srl_bool(T, R)])
144
+ fact(srl_or3('false', 'false', 'false'), srl_or3('true', _, 'true'), srl_or3('false', 'true', 'true'), srl_or3('error', 'true', 'true'))`,
145
+ needs: ['srl_bool'],
146
+ },
147
+
148
+ // Comparison.
149
+ srl_equal: {
150
+ text: String.raw`
151
+ // srl_equal(A, B, T): T is 'true' when A and B are equal and 'false' when
152
+ // they are not; literals that cannot be compared have no answer, an error.
153
+ implied_by(srl_equal(A, B, T), once(srl_equal_(A, B, T)))
154
+ implied_by(srl_equal_(A, B, T), [srl_num(A, _, X), srl_num(B, _, Y), or([eq(X, Y), unify(T, 'true')], [ne(X, Y), unify(T, 'false')])])
155
+ implied_by(srl_equal_(A, B, 'true'), identical(A, B))
156
+ 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
+ implied_by(srl_equal_(A, B, 'false'), [srl_text_kind(A, _), srl_text_kind(B, _)])
158
+ implied_by(srl_equal_(A, B, 'false'), [srl_node(A), srl_term(B)])
159
+ implied_by(srl_equal_(A, B, 'false'), [srl_term(A), srl_node(B)])
160
+ implied_by(srl_equal_(literal(_, datatype(D)), literal(_, datatype(D)), 'false'), srl_ordered_type(D))
161
+ implied_by(srl_boolean(literal(L, datatype('${XSD}boolean')), 'true'), srl_true_text(L))
162
+ implied_by(srl_boolean(literal(L, datatype('${XSD}boolean')), 'false'), srl_false_text(L))
163
+ fact(srl_true_text('true'), srl_true_text('1'), srl_false_text('false'), srl_false_text('0'))
164
+ // A string-like literal: a simple string or a language-tagged string.
165
+ implied_by(srl_text_kind(literal(L, datatype('${XSD}string')), 'string'), 'true')
166
+ implied_by(srl_text_kind(literal(L, lang(T)), lang(T)), 'true')
167
+ implied_by(srl_text_kind(literal(L, lang(T, D)), lang(T, D)), 'true')
168
+ // IRIs, blank nodes and triple terms; srl_term is any RDF term.
169
+ implied_by(srl_node(iri(_)), 'true')
170
+ implied_by(srl_node(bnode(_, _)), 'true')
171
+ implied_by(srl_node(skolem(_, _, _)), 'true')
172
+ implied_by(srl_node(triple(_, _, _)), 'true')
173
+ implied_by(srl_term(T), is_number(T))
174
+ implied_by(srl_term(literal(_, _)), 'true')
175
+ implied_by(srl_term(T), srl_node(T))
176
+ ${['dateTime', 'date', 'time', 'gYear', 'gYearMonth', 'gMonthDay', 'gMonth', 'gDay', 'duration', 'dayTimeDuration',
177
+ 'yearMonthDuration'].map((t) => `fact(srl_ordered_type('${XSD}${t}'))`).join('\n')}`,
178
+ needs: ['srl_num'],
179
+ },
180
+ srl_eq: {
181
+ text: String.raw`
182
+ implied_by(srl_eq(A, B, R), [srl_equal(A, B, T), srl_bool(T, R)])`,
183
+ needs: ['srl_equal', 'srl_bool'],
184
+ },
185
+ srl_ne: {
186
+ text: String.raw`
187
+ implied_by(srl_ne(A, B, R), [srl_equal(A, B, T), srl_flip(T, F), srl_bool(F, R)])`,
188
+ needs: ['srl_equal', 'srl_bool'],
189
+ },
190
+ srl_order: {
191
+ text: String.raw`
192
+ // srl_order(A, B, O): O is '<', '=' or '>' when A and B are ordered:
193
+ // numbers by value, simple strings by code point, booleans with false
194
+ // first, and date and time values of one datatype by their lexical forms.
195
+ implied_by(srl_order(A, B, O), once(srl_order_(A, B, O)))
196
+ implied_by(srl_order_(A, B, O), [srl_num(A, _, X), srl_num(B, _, Y), srl_compare(X, Y, O)])
197
+ implied_by(srl_order_(literal(X, datatype('${XSD}string')), literal(Y, datatype('${XSD}string')), O), compare(O, X, Y))
198
+ implied_by(srl_order_(A, B, O), [srl_boolean(A, X), srl_boolean(B, Y), compare(O, X, Y)])
199
+ implied_by(srl_order_(literal(X, datatype(D)), literal(Y, datatype(D)), O), [srl_ordered_type(D), compare(O, X, Y)])
200
+ implied_by(srl_compare(X, Y, O), or([lt(X, Y), unify(O, '<')], [eq(X, Y), unify(O, '=')], [gt(X, Y), unify(O, '>')]))
201
+ fact(srl_lt_('<', 'true'), srl_lt_('=', 'false'), srl_lt_('>', 'false'))
202
+ fact(srl_le_('<', 'true'), srl_le_('=', 'true'), srl_le_('>', 'false'))
203
+ fact(srl_gt_('<', 'false'), srl_gt_('=', 'false'), srl_gt_('>', 'true'))
204
+ fact(srl_ge_('<', 'false'), srl_ge_('=', 'true'), srl_ge_('>', 'true'))`,
205
+ needs: ['srl_equal'],
206
+ },
207
+ srl_lt: { text: compare('srl_lt'), needs: ['srl_order', 'srl_bool'] },
208
+ srl_le: { text: compare('srl_le'), needs: ['srl_order', 'srl_bool'] },
209
+ srl_gt: { text: compare('srl_gt'), needs: ['srl_order', 'srl_bool'] },
210
+ srl_ge: { text: compare('srl_ge'), needs: ['srl_order', 'srl_bool'] },
211
+ srl_in: {
212
+ text: String.raw`
213
+ // srl_eq3(A, B, T): T is 'true', 'false' or 'error'.
214
+ implied_by(srl_eq3(A, B, T), once(or(srl_equal(A, B, T), unify(T, 'error'))))
215
+ // IN is true when an item is equal, an error when none is and one is an
216
+ // error, and false otherwise; NOT IN is its negation.
217
+ implied_by(srl_in(Ts, R), [srl_in3(Ts, T), srl_bool(T, R)])
218
+ implied_by(srl_not_in(Ts, R), [srl_in3(Ts, T), srl_flip(T, F), srl_bool(F, R)])
219
+ implied_by(srl_in3(Ts, T), once(or([srl_member('true', Ts), unify(T, 'true')], [not(srl_member('error', Ts)), unify(T, 'false')])))`,
220
+ needs: ['srl_equal', 'srl_bool', 'srl_member'],
221
+ },
222
+ srl_same_term: {
223
+ text: String.raw`
224
+ implied_by(srl_same_term(A, B, R), [or([identical(A, B), unify(T, 'true')], [not_identical(A, B), unify(T, 'false')]), srl_bool(T, R)])`,
225
+ needs: ['srl_bool'],
226
+ },
227
+
228
+ // Term tests and accessors.
229
+ srl_kind_tests: {
230
+ text: String.raw`
231
+ implied_by(srl_is_iri(T, R), [srl_is_iri_(T, B), srl_bool(B, R)])
232
+ implied_by(srl_is_iri_(T, B), once(or([unify(T, iri(_)), unify(B, 'true')], unify(B, 'false'))))
233
+ implied_by(srl_is_blank(T, R), [srl_is_blank_(T, B), srl_bool(B, R)])
234
+ implied_by(srl_is_blank_(T, B), once(or([or(unify(T, bnode(_, _)), unify(T, skolem(_, _, _))), unify(B, 'true')], unify(B, 'false'))))
235
+ implied_by(srl_is_literal(T, R), [srl_is_literal_(T, B), srl_bool(B, R)])
236
+ implied_by(srl_is_literal_(T, B), once(or([or(is_number(T), unify(T, literal(_, _))), unify(B, 'true')], unify(B, 'false'))))
237
+ implied_by(srl_is_numeric(T, R), [srl_is_numeric_(T, B), srl_bool(B, R)])
238
+ implied_by(srl_is_numeric_(T, B), once(or([srl_num(T, _, _), unify(B, 'true')], unify(B, 'false'))))
239
+ implied_by(srl_is_triple(T, R), [srl_is_triple_(T, B), srl_bool(B, R)])
240
+ implied_by(srl_is_triple_(T, B), once(or([unify(T, triple(_, _, _)), unify(B, 'true')], unify(B, 'false'))))
241
+ implied_by(srl_has_lang(T, R), [srl_term(T), srl_has_lang_(T, B), srl_bool(B, R)])
242
+ implied_by(srl_has_lang_(T, B), once(or([or(unify(T, literal(_, lang(_))), unify(T, literal(_, lang(_, _)))), unify(B, 'true')], unify(B, 'false'))))
243
+ implied_by(srl_has_langdir(T, R), [srl_term(T), srl_has_langdir_(T, B), srl_bool(B, R)])
244
+ implied_by(srl_has_langdir_(T, B), once(or([unify(T, literal(_, lang(_, _))), unify(B, 'true')], unify(B, 'false'))))`,
245
+ needs: ['srl_bool', 'srl_num', 'srl_equal'],
246
+ },
247
+ srl_str: {
248
+ text: String.raw`
249
+ // srl_str(T, R): the lexical form of a literal or the text of an IRI, as a
250
+ // simple string.
251
+ implied_by(srl_str(T, ${str('A')}), srl_text(T, A))
252
+ implied_by(srl_text(iri(A), A), 'true')
253
+ implied_by(srl_text(literal(A, _), A), 'true')
254
+ implied_by(srl_text(T, A), [is_int(T), number_chars(T, Cs), atom_chars(A, Cs)])`,
255
+ },
256
+ srl_lang: {
257
+ text: String.raw`
258
+ implied_by(srl_lang(T, ${str('L')}), once(srl_lang_(T, L)))
259
+ implied_by(srl_lang_(literal(_, lang(L)), L), 'true')
260
+ implied_by(srl_lang_(literal(_, lang(L, _)), L), 'true')
261
+ implied_by(srl_lang_(literal(_, datatype(_)), ''), 'true')
262
+ implied_by(srl_lang_(T, ''), is_number(T))
263
+ implied_by(srl_langdir(T, ${str('D')}), once(srl_langdir_(T, D)))
264
+ implied_by(srl_langdir_(literal(_, lang(_, D)), D), 'true')
265
+ implied_by(srl_langdir_(literal(_, lang(_)), ''), 'true')
266
+ implied_by(srl_langdir_(literal(_, datatype(_)), ''), 'true')
267
+ implied_by(srl_langdir_(T, ''), is_number(T))`,
268
+ },
269
+ srl_langmatches: {
270
+ text: String.raw`
271
+ // LANGMATCHES: * matches every non-empty tag; a range matches the tag
272
+ // itself and its subtags, ignoring case.
273
+ implied_by(srl_langmatches(${str('T')}, ${str('G')}, R), [atom_lower(T, LT), atom_lower(G, LG), or([srl_langmatches_(LT, LG), unify(B, 'true')], [not(srl_langmatches_(LT, LG)), unify(B, 'false')]), srl_bool(B, R)])
274
+ implied_by(srl_langmatches_(T, '*'), [atom_length(T, N), gt(N, 0)])
275
+ implied_by(srl_langmatches_(T, G), [not_identical(G, '*'), identical(T, G)])
276
+ implied_by(srl_langmatches_(T, G), [not_identical(G, '*'), atom_concat(G, '-', P), atom_concat(P, _, T)])`,
277
+ needs: ['srl_bool'],
278
+ },
279
+ srl_datatype: {
280
+ text: String.raw`
281
+ implied_by(srl_datatype(T, iri('${XSD}integer')), is_int(T))
282
+ implied_by(srl_datatype(literal(_, datatype(D)), iri(D)), 'true')
283
+ implied_by(srl_datatype(literal(_, lang(_)), iri('${RDF}langString')), 'true')
284
+ implied_by(srl_datatype(literal(_, lang(_, _)), iri('${RDF}dirLangString')), 'true')`,
285
+ },
286
+ srl_iri: {
287
+ text: String.raw`
288
+ implied_by(srl_iri(iri(A), iri(A)), 'true')
289
+ implied_by(srl_iri(${str('A')}, iri(A)), 'true')`,
290
+ },
291
+ srl_strdt: {
292
+ text: String.raw`
293
+ implied_by(srl_strdt(${str('L')}, iri(D), T), srl_typed(L, D, T))`,
294
+ needs: ['srl_typed'],
295
+ },
296
+ srl_typed: {
297
+ text: String.raw`
298
+ // srl_typed(L, D, T): T is the literal with lexical form L and datatype D;
299
+ // a canonical xsd:integer is an integer.
300
+ implied_by(srl_typed(L, D, T), once(srl_typed_(L, D, T)))
301
+ implied_by(srl_typed_(L, '${XSD}integer', N), [regex_match('^(?:0|-?[1-9][0-9]*)$', L), atom_chars(L, Cs), number_chars(N, Cs)])
302
+ implied_by(srl_typed_(L, D, literal(L, datatype(D))), 'true')`,
303
+ },
304
+ srl_strlang: {
305
+ text: String.raw`
306
+ implied_by(srl_strlang(${str('L')}, ${str('T')}, literal(L, lang(G))), [atom_length(T, N), gt(N, 0), atom_lower(T, G)])
307
+ implied_by(srl_strlangdir(${str('L')}, ${str('T')}, ${str('D')}, literal(L, lang(G, D))), [atom_length(T, N), gt(N, 0), atom_lower(T, G), srl_direction(D)])
308
+ fact(srl_direction('ltr'), srl_direction('rtl'))`,
309
+ },
310
+ srl_triple: {
311
+ text: String.raw`
312
+ implied_by(srl_triple(S, iri(P), O, triple(S, iri(P), O)), [srl_term(S), srl_term(O)])
313
+ implied_by(srl_subject(triple(S, _, _), S), 'true')
314
+ implied_by(srl_predicate(triple(_, P, _), P), 'true')
315
+ implied_by(srl_object(triple(_, _, O), O), 'true')`,
316
+ needs: ['srl_equal'],
317
+ },
318
+
319
+ // Strings.
320
+ srl_string: {
321
+ text: String.raw`
322
+ // srl_string(T, A, K): T is a simple or language-tagged string with text A
323
+ // and annotation K.
324
+ implied_by(srl_string(literal(A, datatype('${XSD}string')), A, datatype('${XSD}string')), 'true')
325
+ implied_by(srl_string(literal(A, lang(T)), A, lang(T)), 'true')
326
+ implied_by(srl_string(literal(A, lang(T, D)), A, lang(T, D)), 'true')
327
+ // Two arguments are compatible when the second is a simple string or has
328
+ // the language tag of the first.
329
+ implied_by(srl_compatible(_, datatype('${XSD}string')), 'true')
330
+ implied_by(srl_compatible(K, K), not_identical(K, datatype('${XSD}string')))`,
331
+ },
332
+ srl_strlen: {
333
+ text: String.raw`
334
+ implied_by(srl_strlen(T, N), [srl_string(T, A, _), atom_length(A, N)])`,
335
+ needs: ['srl_string'],
336
+ },
337
+ srl_ucase: {
338
+ text: String.raw`
339
+ implied_by(srl_ucase(T, literal(U, K)), [srl_string(T, A, K), atom_upper(A, U)])
340
+ implied_by(srl_lcase(T, literal(U, K)), [srl_string(T, A, K), atom_lower(A, U)])`,
341
+ needs: ['srl_string'],
342
+ },
343
+ srl_strstarts: {
344
+ text: String.raw`
345
+ ${tested('srl_strstarts', 'A, B')}
346
+ implied_by(srl_strstarts_(A, B), atom_concat(B, _, A))
347
+ ${tested('srl_strends', 'A, B')}
348
+ implied_by(srl_strends_(A, B), once(atom_concat(_, B, A)))
349
+ ${tested('srl_contains', 'A, B')}
350
+ implied_by(srl_contains_(A, B), once([atom_concat(_, R, A), atom_concat(B, _, R)]))
351
+ implied_by(srl_strstarts(X, Y, R), [srl_string(X, A, K), srl_string(Y, B, L), srl_compatible(K, L), srl_strstarts_t(A, B, T), srl_bool(T, R)])
352
+ implied_by(srl_strends(X, Y, R), [srl_string(X, A, K), srl_string(Y, B, L), srl_compatible(K, L), srl_strends_t(A, B, T), srl_bool(T, R)])
353
+ implied_by(srl_contains(X, Y, R), [srl_string(X, A, K), srl_string(Y, B, L), srl_compatible(K, L), srl_contains_t(A, B, T), srl_bool(T, R)])`,
354
+ needs: ['srl_string', 'srl_bool'],
355
+ },
356
+ srl_strbefore: {
357
+ text: String.raw`
358
+ // STRBEFORE and STRAFTER: the text around the first match, keeping the
359
+ // first argument's tag; no match gives the empty simple string.
360
+ implied_by(srl_strbefore(X, Y, R), [srl_string(X, A, K), srl_string(Y, B, L), srl_compatible(K, L), once(or([atom_concat(H, Rest, A), atom_concat(B, _, Rest), unify(R, literal(H, K))], [not(srl_contains_(A, B)), unify(R, ${str("''")})]))])
361
+ implied_by(srl_strafter(X, Y, R), [srl_string(X, A, K), srl_string(Y, B, L), srl_compatible(K, L), once(or([atom_concat(_, Rest, A), atom_concat(B, T, Rest), unify(R, literal(T, K))], [not(srl_contains_(A, B)), unify(R, ${str("''")})]))])
362
+ implied_by(srl_contains_(A, B), once([atom_concat(_, R, A), atom_concat(B, _, R)]))`,
363
+ needs: ['srl_string'],
364
+ },
365
+ srl_concat: {
366
+ text: String.raw`
367
+ // CONCAT keeps a language tag all its arguments share.
368
+ implied_by(srl_concat(Ts, literal(A, K)), [srl_concat_(Ts, '', A, K0), srl_concat_kind(K0, K)])
369
+ fact(srl_concat_([], A, A, 'none'))
370
+ implied_by(srl_concat_([T, ...Ts], A0, A, K), [srl_string(T, B, K1), atom_concat(A0, B, A1), srl_concat_(Ts, A1, A, K2), srl_join_kind(K1, K2, K)])
371
+ implied_by(srl_join_kind(K, 'none', K), 'true')
372
+ implied_by(srl_join_kind(K, K, K), not_identical(K, 'none'))
373
+ implied_by(srl_join_kind(K1, K2, datatype('${XSD}string')), [not_identical(K2, 'none'), not_identical(K1, K2)])
374
+ implied_by(srl_concat_kind('none', datatype('${XSD}string')), 'true')
375
+ implied_by(srl_concat_kind(K, K), not_identical(K, 'none'))`,
376
+ needs: ['srl_string'],
377
+ },
378
+ srl_substr: {
379
+ text: String.raw`
380
+ // 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), srl_round_half_up(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)])
382
+ implied_by(srl_substr(T, S, L, literal(B, K)), [srl_string(T, A, K), srl_num(S, _, X), srl_num(L, _, Y), srl_round_half_up(X, I), srl_round_half_up(Y, J), is(E, add(I, J)), atom_chars(A, Cs), srl_slice(Cs, 1, I, E, Bs), atom_chars(B, Bs)])
383
+ // srl_slice(Cs, P, I, E, Bs): Bs are the characters of Cs, the first at
384
+ // position P, whose positions are at least I and below E.
385
+ fact(srl_slice([], _, _, _, []))
386
+ 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
+ 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
+ implied_by(srl_round_half_up(X, I), is(I, floor(add(X, float(0.5)))))`,
389
+ needs: ['srl_string', 'srl_num'],
390
+ },
391
+ srl_replace: {
392
+ text: String.raw`
393
+ implied_by(srl_replace(T, ${str('P')}, ${str('W')}, literal(U, K)), [srl_string(T, A, K), regex_replace(P, A, W, U)])
394
+ implied_by(srl_replace_i(T, ${str('P')}, ${str('W')}, literal(U, K)), [srl_string(T, A, K), atom_lower(A, L), regex_replace(P, L, W, U)])`,
395
+ needs: ['srl_string'],
396
+ },
397
+ srl_regex: {
398
+ text: String.raw`
399
+ implied_by(srl_regex(T, ${str('P')}, R), [srl_string(T, A, _), or([regex_match(P, A), unify(B, 'true')], [not(regex_match(P, A)), unify(B, 'false')]), srl_bool(B, R)])
400
+ implied_by(srl_regex_i(T, ${str('P')}, R), [srl_string(T, A, _), atom_lower(A, L), or([regex_match(P, L), unify(B, 'true')], [not(regex_match(P, L)), unify(B, 'false')]), srl_bool(B, R)])`,
401
+ needs: ['srl_string', 'srl_bool'],
402
+ },
403
+ srl_encode_for_uri: {
404
+ text: String.raw`
405
+ // ENCODE_FOR_URI percent-encodes the UTF-8 bytes of every character but
406
+ // the unreserved ones.
407
+ implied_by(srl_encode_for_uri(T, ${str('E')}), [srl_string(T, A, _), atom_codes(A, Cs), srl_encode_(Cs, Es), srl_atoms_concat(Es, E)])
408
+ fact(srl_encode_([], []))
409
+ implied_by(srl_encode_([C, ...Cs], [E, ...Es]), [srl_encode_code(C, E), srl_encode_(Cs, Es)])
410
+ implied_by(srl_encode_code(C, E), [srl_unreserved(C), atom_codes(E, [C])])
411
+ implied_by(srl_encode_code(C, E), [not(srl_unreserved(C)), srl_utf8(C, Bs), srl_percent(Bs, Ps), srl_atoms_concat(Ps, E)])
412
+ implied_by(srl_unreserved(C), or([ge(C, 65), le(C, 90)], [ge(C, 97), le(C, 122)], [ge(C, 48), le(C, 57)], eq(C, 45), eq(C, 46), eq(C, 95), eq(C, 126)))
413
+ implied_by(srl_utf8(C, [C]), lt(C, 128))
414
+ implied_by(srl_utf8(C, [A, B]), [ge(C, 128), lt(C, 2048), is(A, or(192, shr(C, 6))), is(B, or(128, and(C, 63)))])
415
+ implied_by(srl_utf8(C, [A, B, D]), [ge(C, 2048), lt(C, 65536), is(A, or(224, shr(C, 12))), is(B, or(128, and(shr(C, 6), 63))), is(D, or(128, and(C, 63)))])
416
+ implied_by(srl_utf8(C, [A, B, D, F]), [ge(C, 65536), is(A, or(240, shr(C, 18))), is(B, or(128, and(shr(C, 12), 63))), is(D, or(128, and(shr(C, 6), 63))), is(F, or(128, and(C, 63)))])
417
+ fact(srl_percent([], []))
418
+ implied_by(srl_percent([B, ...Bs], [P, ...Ps]), [is(H, shr(B, 4)), is(L, and(B, 15)), srl_hex(H, X), srl_hex(L, Y), atom_concat('%', X, P0), atom_concat(P0, Y, P), srl_percent(Bs, Ps)])
419
+ ${[...'0123456789ABCDEF'].map((d, i) => `fact(srl_hex(${i}, '${d}'))`).join('\n')}`,
420
+ needs: ['srl_string', 'srl_atoms_concat'],
421
+ },
422
+ srl_atoms_concat: {
423
+ text: String.raw`
424
+ fact(srl_atoms_concat([], ''))
425
+ implied_by(srl_atoms_concat([A, ...As], C), [srl_atoms_concat(As, C0), atom_concat(A, C0, C)])`,
426
+ },
427
+ // Numeric functions.
428
+ srl_abs: {
429
+ text: String.raw`
430
+ implied_by(srl_abs(T, R), [srl_num(T, K, X), is(V, abs(X)), srl_make(K, V, R)])
431
+ implied_by(srl_ceil(T, R), [srl_num(T, K, X), is(V, ceil(X)), srl_make(K, V, R)])
432
+ implied_by(srl_floor(T, R), [srl_num(T, K, X), is(V, floor(X)), srl_make(K, V, R)])
433
+ // ROUND rounds halves up, toward positive infinity.
434
+ implied_by(srl_round(T, R), [srl_num(T, K, X), srl_round_(K, X, V), srl_make(K, V, R)])
435
+ implied_by(srl_round_('integer', X, X), 'true')
436
+ implied_by(srl_round_(K, X, V), [srl_float_kind(K), is(V, floor(add(X, float(0.5))))])`,
437
+ needs: ['srl_make'],
438
+ },
439
+
440
+ // Dates and times.
441
+ srl_datetime: {
442
+ text: String.raw`
443
+ // The parts of an xsd:dateTime or xsd:date: [Year, Month, Day, Hours,
444
+ // Minutes, Seconds, Zone], each as text, '' where absent.
445
+ implied_by(srl_dt_parts(literal(L, datatype('${XSD}dateTime')), G), regex_find('^(-?\\d{4,})-(\\d\\d)-(\\d\\d)T(\\d\\d):(\\d\\d):(\\d\\d(?:\\.\\d+)?)(Z|[+-]\\d\\d:\\d\\d)?$', L, [_, ...G]))
446
+ implied_by(srl_dt_parts(literal(L, datatype('${XSD}date')), [Y, M, D, '', '', '', Z]), regex_find('^(-?\\d{4,})-(\\d\\d)-(\\d\\d)(Z|[+-]\\d\\d:\\d\\d)?$', L, [_, Y, M, D, Z]))
447
+ implied_by(srl_dt_int(T, I, N), [srl_dt_parts(T, G), srl_nth(I, G, A), atom_length(A, K), gt(K, 0), atom_chars(A, Cs), number_chars(N, Cs)])
448
+ implied_by(srl_year(T, N), srl_dt_int(T, 1, N))
449
+ implied_by(srl_month(T, N), srl_dt_int(T, 2, N))
450
+ implied_by(srl_day(T, N), srl_dt_int(T, 3, N))
451
+ implied_by(srl_hours(T, N), srl_dt_int(T, 4, N))
452
+ implied_by(srl_minutes(T, N), srl_dt_int(T, 5, N))
453
+ implied_by(srl_seconds(T, literal(S, datatype('${XSD}decimal'))), [srl_dt_parts(T, G), srl_nth(6, G, S), atom_length(S, K), gt(K, 0)])
454
+ implied_by(srl_tz(T, ${str('Z')}), [srl_dt_parts(T, G), srl_nth(7, G, Z)])
455
+ // TIMEZONE gives the offset as an xsd:dayTimeDuration.
456
+ implied_by(srl_timezone(T, literal(D, datatype('${XSD}dayTimeDuration'))), [srl_dt_parts(T, G), srl_nth(7, G, Z), srl_zone_duration(Z, D)])
457
+ implied_by(srl_zone_duration('Z', 'PT0S'), 'true')
458
+ implied_by(srl_zone_duration(Z, D), [regex_find('^([+-])(\\d\\d):(\\d\\d)$', Z, [_, S, H, M]), atom_chars(H, HC), number_chars(HN, HC), atom_chars(M, MC), number_chars(MN, MC), srl_zone_sign(S, P), srl_zone_text(HN, MN, B), atom_concat(P, B, D)])
459
+ fact(srl_zone_sign('+', ''), srl_zone_sign('-', '-'))
460
+ implied_by(srl_zone_text(0, 0, 'PT0S'), 'true')
461
+ implied_by(srl_zone_text(H, 0, D), [gt(H, 0), srl_int_text(H, A), atom_concat('PT', A, P), atom_concat(P, 'H', D)])
462
+ implied_by(srl_zone_text(0, M, D), [gt(M, 0), srl_int_text(M, A), atom_concat('PT', A, P), atom_concat(P, 'M', D)])
463
+ implied_by(srl_zone_text(H, M, D), [gt(H, 0), gt(M, 0), srl_int_text(H, A), srl_int_text(M, B), atom_concat('PT', A, P), atom_concat(P, 'H', Q), atom_concat(Q, B, R), atom_concat(R, 'M', D)])
464
+ implied_by(srl_int_text(N, A), [number_chars(N, Cs), atom_chars(A, Cs)])`,
465
+ needs: ['srl_nth'],
466
+ },
467
+
468
+ // Casts to XSD datatypes.
469
+ srl_cast: {
470
+ text: String.raw`
471
+ // srl_cast(D, T, R): T cast to the XSD datatype D, as SPARQL casts.
472
+ implied_by(srl_cast(D, T, R), once(srl_cast_(D, T, R)))
473
+ implied_by(srl_cast_('${XSD}string', T, ${str('A')}), srl_cast_text(T, A))
474
+ implied_by(srl_cast_text(iri(A), A), 'true')
475
+ implied_by(srl_cast_text(literal(A, _), A), 'true')
476
+ 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), is(V, trunc(X))])
478
+ 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), [regex_match('^[+-]?[0-9]+$', L), atom_chars(L, Cs), number_chars(V, Cs)])
480
+ implied_by(srl_cast_(D, T, R), [srl_float_type(D, K), srl_num(T, _, X), srl_make(K, X, R)])
481
+ 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), [regex_match('^[+-]?(?:[0-9]+(?:\\.[0-9]*)?|\\.[0-9]+)$', L), atom_chars(L, Cs), number_chars(X, Cs), srl_make('decimal', X, R)])
483
+ implied_by(srl_cast_(D, literal(L, datatype('${XSD}string')), R), [srl_float_type(D, K), srl_float_kind_double(K), regex_match('^[+-]?(?:[0-9]+(?:\\.[0-9]*)?|\\.[0-9]+)(?:[eE][+-]?[0-9]+)?$', L), atom_chars(L, Cs), number_chars(X, Cs), srl_make(K, X, R)])
484
+ 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)])
486
+ 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
+ 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
+ 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)])
489
+ fact(srl_text_or_datetime('${XSD}string'), srl_text_or_datetime('${XSD}dateTime'), srl_text_or_date('${XSD}string'), srl_text_or_date('${XSD}date'))
490
+ fact(srl_float_type('${XSD}decimal', 'decimal'), srl_float_type('${XSD}float', 'float'), srl_float_type('${XSD}double', 'double'))
491
+ fact(srl_float_kind_double('float'), srl_float_kind_double('double'))
492
+ fact(srl_bit('true', 1), srl_bit('false', 0))`,
493
+ needs: ['srl_make', 'srl_equal', 'srl_bool'],
494
+ },
495
+
496
+ // Aggregates.
497
+ srl_aggregates: {
498
+ text: String.raw`
499
+ // Aggregates over a group's column of values, repeats included.
500
+ implied_by(srl_count(Vs, N), srl_length(Vs, N))
501
+ implied_by(srl_count_distinct(Vs, N), [srl_distinct(Vs, Ds), srl_length(Ds, N)])
502
+ implied_by(srl_sum(Vs, S), srl_sum_(Vs, 0, S))
503
+ fact(srl_sum_([], S, S))
504
+ implied_by(srl_sum_([V, ...Vs], A, S), [srl_add(A, V, B), srl_sum_(Vs, B, S)])
505
+ implied_by(srl_min([V, ...Vs], M), srl_extreme(Vs, '<', V, M))
506
+ implied_by(srl_max([V, ...Vs], M), srl_extreme(Vs, '>', V, M))
507
+ fact(srl_extreme([], _, M, M))
508
+ implied_by(srl_extreme([V, ...Vs], O, A, M), [srl_rank_order(V, A, P), srl_pick(P, O, V, A, B), srl_extreme(Vs, O, B, M)])
509
+ implied_by(srl_pick(P, P, V, _, V), 'true')
510
+ implied_by(srl_pick(P, O, _, A, A), not_identical(P, O))
511
+ // The order of ORDER BY: blank nodes, IRIs, then literals; comparable
512
+ // literals by value, others by their terms.
513
+ implied_by(srl_rank_order(A, B, O), once(or(srl_order(A, B, O), [srl_term_rank(A, I), srl_term_rank(B, J), or([ne(I, J), compare(O, I, J)], [eq(I, J), compare(O, A, B)])])))
514
+ implied_by(srl_term_rank(T, 0), or(unify(T, bnode(_, _)), unify(T, skolem(_, _, _))))
515
+ implied_by(srl_term_rank(iri(_), 1), 'true')
516
+ implied_by(srl_term_rank(T, 2), or(is_number(T), unify(T, literal(_, _))))
517
+ implied_by(srl_term_rank(triple(_, _, _), 3), 'true')`,
518
+ needs: ['srl_add', 'srl_order', 'srl_length', 'srl_distinct'],
519
+ },
520
+
521
+ // Lists.
522
+ srl_member: {
523
+ text: String.raw`
524
+ fact(srl_member(X, [X, ..._]))
525
+ implied_by(srl_member(X, [_, ...T]), srl_member(X, T))`,
526
+ },
527
+ srl_length: {
528
+ text: String.raw`
529
+ implied_by(srl_length(L, N), srl_length_(L, 0, N))
530
+ fact(srl_length_([], N, N))
531
+ implied_by(srl_length_([_, ...T], A, N), [is(B, add(A, 1)), srl_length_(T, B, N)])`,
532
+ },
533
+ srl_distinct: {
534
+ text: String.raw`
535
+ // srl_distinct(L, D): the distinct terms of L, in order of first occurrence.
536
+ implied_by(srl_distinct(L, D), srl_distinct_(L, [], D))
537
+ fact(srl_distinct_([], _, []))
538
+ implied_by(srl_distinct_([X, ...T], Seen, D), or([srl_seen(X, Seen), srl_distinct_(T, Seen, D)], [not(srl_seen(X, Seen)), unify(D, [X, ...D1]), srl_distinct_(T, [X, ...Seen], D1)]))
539
+ implied_by(srl_seen(X, [Y, ..._]), identical(X, Y))
540
+ implied_by(srl_seen(X, [Y, ...T]), [not_identical(X, Y), srl_seen(X, T)])`,
541
+ },
542
+ srl_nth: {
543
+ text: String.raw`
544
+ fact(srl_nth(1, [X, ..._], X))
545
+ implied_by(srl_nth(N, [_, ...T], X), [gt(N, 1), is(M, sub(N, 1)), srl_nth(M, T, X)])`,
546
+ },
547
+ srl_column: {
548
+ text: String.raw`
549
+ // srl_column(Rows, I, Vs): the I-th value of each row.
550
+ fact(srl_column([], _, []))
551
+ implied_by(srl_column([R, ...Rs], I, [V, ...Vs]), [srl_nth(I, R, V), srl_column(Rs, I, Vs)])`,
552
+ needs: ['srl_nth'],
553
+ },
554
+ };
555
+
556
+ function arith(name, op) {
557
+ return String.raw`
558
+ implied_by(${name}(A, B, R), [srl_num(A, KA, X), srl_num(B, KB, Y), srl_promote(KA, KB, K), srl_finite('${op}', X, Y), is(V, ${op}(X, Y)), srl_make(K, V, R)])`;
559
+ }
560
+
561
+ function compare(name) {
562
+ return String.raw`
563
+ implied_by(${name}(A, B, R), [srl_order(A, B, O), ${name}_(O, T), srl_bool(T, R)])`;
564
+ }
565
+
566
+ // The text of the definitions a program needs, and of those they need.
567
+ export function libraryText(needed) {
568
+ const done = new Set();
569
+ const out = [];
570
+ const seenLines = new Set();
571
+ const visit = (name) => {
572
+ if (done.has(name)) return;
573
+ done.add(name);
574
+ const entry = DEFINITIONS[name];
575
+ if (!entry) throw new Error(`no definition ${name}`);
576
+ for (const dep of entry.needs ?? []) visit(dep);
577
+ // Small shared facts appear in several definitions; state each once.
578
+ const lines = entry.text.trim().split('\n').filter((line) => line.startsWith('//') || !seenLines.has(line));
579
+ lines.forEach((line) => seenLines.add(line));
580
+ out.push(lines.join('\n'));
581
+ };
582
+ for (const name of [...needed].sort()) visit(name);
583
+ return out.join('\n');
584
+ }