forked from Attempto/APE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruleml2tptp.xslt
517 lines (443 loc) · 18.3 KB
/
ruleml2tptp.xslt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:r="http://ruleml.org/spec">
<!-- The input of this translator must be a normalized RuleML instance. -->
<xsl:param name="tptp-lang" select="'qmf'" as="xs:string" required="no"/>
<!-- line break -->
<xsl:param name="nl" select="'
'" as="xs:string" required="no"/>
<xsl:output method="text" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="comment()" mode="#all">
<xsl:if test="not(matches(., '^\s*$'))">
<!-- trim lines -->
<xsl:variable name="step-trim-ends"
select="replace(., '(^[ \t]+)|([ \t]+$)', '')"/>
<xsl:variable name="step-trim-lines"
select="replace($step-trim-ends, '[ \t]*(\r?\n)[ \t]*', '$1')"/>
<!-- trim first and last lines if they are blank -->
<xsl:variable name="step-trim-first-blank-line"
select="replace($step-trim-lines, '^\r?\n', '')"/>
<xsl:variable name="step-trim-last-blank-line"
select="replace($step-trim-first-blank-line, '\r?\n$', '')"/>
<!-- insert '% ' -->
<xsl:variable name="step-insert-spaces"
select="replace($step-trim-last-blank-line, '^%', '% ', 'm')"/>
<xsl:variable name="step-insert-first"
select="replace($step-insert-spaces, '^([^%])', '% $1')"/>
<xsl:variable name="step-insert-last"
select="replace($step-insert-first, '(\r?\n)$', '$1% ')"/>
<xsl:variable name="step-insert"
select="replace($step-insert-last, '(\r?\n)([^%])', '$1% $2')"/>
<xsl:value-of select="$step-insert"/>
<!-- switch to a new line after the comment -->
<xsl:value-of select="$nl"/>
</xsl:if>
</xsl:template>
<!-- break the line with indentation -->
<xsl:template name="break-line">
<xsl:param name="depth" required="yes" as="xs:integer"/>
<!-- somtime we need to go backwards some spaces from the indention -->
<xsl:param name="retreat" select="0" required="no" as="xs:integer"/>
<xsl:variable name="indent" as="xs:string *">
<xsl:text> </xsl:text> <!-- two leading spaces -->
<xsl:for-each select="1 to $depth">
<xsl:text> </xsl:text> <!-- two spaces for every depth -->
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$nl"/>
<xsl:value-of select="substring(string-join($indent, ''), $retreat + 1)"/>
</xsl:template>
<xsl:template match="r:*">
<xsl:apply-templates select="r:*"/>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="comment()"/>
<xsl:apply-templates select="r:RuleML"/>
</xsl:template>
<xsl:template match="r:RuleML">
<xsl:apply-templates select="comment()"/>
<xsl:apply-templates select="r:act"/>
</xsl:template>
<xsl:template match="r:act">
<xsl:apply-templates select="comment()"/>
<xsl:apply-templates select="r:Assert | r:Query">
<xsl:with-param name="act-index" select="@index" tunnel="yes"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="r:Assert">
<xsl:apply-templates select="comment()"/>
<xsl:apply-templates select="r:formula | r:termula" mode="fof-formula">
<xsl:with-param name="formula-type" select="local-name()"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="r:Query">
<xsl:apply-templates select="comment()"/>
<xsl:apply-templates select="r:formula | r:termula" mode="fof-formula">
<xsl:with-param name="formula-type" select="local-name()"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="r:formula | r:termula" mode="fof-formula">
<xsl:param name="act-index" required="yes" tunnel="yes" as="xs:integer"/>
<xsl:param name="formula-type" required="yes" as="xs:string"/>
<xsl:variable name="depth" select="1" as="xs:integer"/>
<xsl:apply-templates select="comment()"/>
<xsl:value-of select="$tptp-lang"/>
<xsl:text>(</xsl:text>
<!-- formula name -->
<xsl:value-of select="concat('act', $act-index, '_formula', count(preceding-sibling::r:formula) + count(preceding-sibling::r:termula) + 1)"/>
<xsl:text>,</xsl:text>
<!-- formula role -->
<xsl:choose>
<xsl:when test="$formula-type = 'Assert'">
<xsl:text>axiom</xsl:text>
</xsl:when>
<xsl:when test="$formula-type = 'Query'">
<xsl:text>conjecture</xsl:text>
</xsl:when>
</xsl:choose>
<xsl:text>,</xsl:text>
<xsl:variable name="content-sequence" as="xs:string*">
<xsl:apply-templates>
<xsl:with-param name="depth" select="$depth" tunnel="yes"/>
<!-- this param indicates if the children can break the line at the very
beginning -->
<xsl:with-param name="line-breaking" select="false()" tunnel="yes"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:variable name="content" select="string-join($content-sequence, '')" as="xs:string"/>
<xsl:variable name="need-extra-parens" select="not(starts-with($content, '( '))" as="xs:boolean"/>
<xsl:if test="$need-extra-parens">
<xsl:text>(</xsl:text>
</xsl:if>
<xsl:call-template name="break-line">
<xsl:with-param name="depth" select="$depth"/>
</xsl:call-template>
<xsl:value-of select="$content"/>
<xsl:choose>
<xsl:when test="$need-extra-parens">
<xsl:text> )).</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>).</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="$nl"/>
<xsl:value-of select="$nl"/>
</xsl:template>
<xsl:template match="r:Forall">
<xsl:call-template name="quantified">
<xsl:with-param name="quantifier" select="'!'"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="r:Exists">
<xsl:call-template name="quantified">
<xsl:with-param name="quantifier" select="'?'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="quantified">
<xsl:param name="depth" required="yes" as="xs:integer" tunnel="yes"/>
<xsl:param name="line-breaking" required="yes" as="xs:boolean" tunnel="yes"/>
<xsl:param name="quantifier" required="yes" as="xs:string"/>
<xsl:param name="last-quantifier-depth" select="-1" required="no" as="xs:integer" tunnel="yes"/>
<xsl:variable name="final-depth" as="xs:integer">
<xsl:choose>
<xsl:when test="$depth = $last-quantifier-depth + 1">
<xsl:value-of select="$last-quantifier-depth"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$depth"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="$line-breaking">
<xsl:call-template name="break-line">
<xsl:with-param name="depth" select="$final-depth"/>
</xsl:call-template>
</xsl:if>
<xsl:value-of select="$quantifier"/>
<xsl:text> [</xsl:text>
<xsl:call-template name="declare-list"/>
<xsl:text>] :</xsl:text>
<xsl:variable name="content-sequence" as="xs:string*">
<xsl:apply-templates select="r:formula | r:termula">
<xsl:with-param name="depth" select="$final-depth + 1" tunnel="yes"/>
<xsl:with-param name="line-breaking" select="true()" tunnel="yes"/>
<xsl:with-param name="last-quantifier-depth" select="$final-depth" tunnel="yes"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:variable name="content" select="string-join($content-sequence, '')" as="xs:string"/>
<xsl:if test="not(starts-with($content, $nl))">
<xsl:text> </xsl:text>
</xsl:if>
<xsl:value-of select="$content"/>
</xsl:template>
<xsl:template match="r:Neg"> <!-- FOL -->
<xsl:param name="depth" required="yes" as="xs:integer" tunnel="yes"/>
<xsl:param name="line-breaking" required="yes" as="xs:boolean" tunnel="yes"/>
<xsl:if test="$line-breaking">
<xsl:call-template name="break-line">
<xsl:with-param name="depth" select="$depth"/>
</xsl:call-template>
</xsl:if>
<xsl:text>~ </xsl:text>
<xsl:apply-templates select="r:strong">
<xsl:with-param name="depth" select="$depth + 1" tunnel="yes"/>
<xsl:with-param name="line-breaking" select="false()" tunnel="yes"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="r:Implies">
<xsl:param name="depth" required="yes" as="xs:integer" tunnel="yes"/>
<xsl:param name="line-breaking" required="yes" as="xs:boolean" tunnel="yes"/>
<xsl:if test="$line-breaking">
<xsl:call-template name="break-line">
<xsl:with-param name="depth" select="$depth"/>
</xsl:call-template>
</xsl:if>
<xsl:text>( </xsl:text>
<xsl:choose>
<xsl:when test="(r:if | r:then)[1] = r:if">
<xsl:apply-templates select="r:if">
<xsl:with-param name="depth" select="$depth + 1" tunnel="yes"/>
<xsl:with-param name="line-breaking" select="false()" tunnel="yes"/>
</xsl:apply-templates>
<xsl:call-template name="break-line">
<xsl:with-param name="depth" select="$depth"/>
<xsl:with-param name="retreat" select="1"/>
</xsl:call-template>
<xsl:text>=> </xsl:text>
<xsl:apply-templates select="r:then">
<xsl:with-param name="depth" select="$depth + 1" tunnel="yes"/>
<xsl:with-param name="line-breaking" select="false()" tunnel="yes"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="r:then">
<xsl:with-param name="depth" select="$depth + 1" tunnel="yes"/>
<xsl:with-param name="line-breaking" select="false()" tunnel="yes"/>
</xsl:apply-templates>
<xsl:call-template name="break-line">
<xsl:with-param name="depth" select="$depth"/>
<xsl:with-param name="retreat" select="1"/>
</xsl:call-template>
<xsl:text><= </xsl:text>
<xsl:apply-templates select="r:if">
<xsl:with-param name="depth" select="$depth + 1" tunnel="yes"/>
<xsl:with-param name="line-breaking" select="false()" tunnel="yes"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
<xsl:text> )</xsl:text>
</xsl:template>
<xsl:template match="r:Equivalent"> <!-- FOL -->
<xsl:param name="depth" required="yes" as="xs:integer" tunnel="yes"/>
<xsl:param name="line-breaking" required="yes" as="xs:boolean" tunnel="yes"/>
<xsl:if test="$line-breaking">
<xsl:call-template name="break-line">
<xsl:with-param name="depth" select="$depth"/>
</xsl:call-template>
</xsl:if>
<xsl:text>( </xsl:text>
<xsl:apply-templates select="r:torso[1]">
<xsl:with-param name="depth" select="$depth + 1" tunnel="yes"/>
<xsl:with-param name="line-breaking" select="false()" tunnel="yes"/>
</xsl:apply-templates>
<xsl:call-template name="break-line">
<xsl:with-param name="depth" select="$depth"/>
<xsl:with-param name="retreat" select="2"/>
</xsl:call-template>
<xsl:text><=> </xsl:text>
<xsl:apply-templates select="r:torso[last()]">
<xsl:with-param name="depth" select="$depth + 1" tunnel="yes"/>
<xsl:with-param name="line-breaking" select="false()" tunnel="yes"/>
</xsl:apply-templates>
<xsl:text> )</xsl:text>
</xsl:template>
<xsl:template match="r:Equal">
<xsl:apply-templates select="r:left"/>
<xsl:text> = </xsl:text>
<xsl:apply-templates select="r:right"/>
</xsl:template>
<xsl:template match="r:And">
<xsl:call-template name="associated">
<xsl:with-param name="connective" select="'&'"/>
<xsl:with-param name="empty-form" select="'$true'"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="r:Or">
<xsl:call-template name="associated">
<xsl:with-param name="connective" select="'|'"/>
<xsl:with-param name="empty-form" select="'$false'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="associated">
<xsl:param name="depth" required="yes" as="xs:integer" tunnel="yes"/>
<xsl:param name="line-breaking" required="yes" as="xs:boolean" tunnel="yes"/>
<xsl:param name="connective" required="yes" as="xs:string"/>
<xsl:param name="empty-form" required="yes" as="xs:string"/>
<xsl:variable name="termulas" select="r:formula | r:termula"/>
<xsl:choose>
<xsl:when test="count($termulas) > 1">
<xsl:if test="$line-breaking">
<xsl:call-template name="break-line">
<xsl:with-param name="depth" select="$depth"/>
</xsl:call-template>
</xsl:if>
<xsl:text>( </xsl:text>
<xsl:for-each select="$termulas">
<xsl:if test="preceding-sibling::r:formula | preceding-sibling::r:termula">
<xsl:call-template name="break-line">
<xsl:with-param name="depth" select="$depth"/>
</xsl:call-template>
<xsl:value-of select="$connective"/>
<xsl:text> </xsl:text>
</xsl:if>
<xsl:apply-templates>
<xsl:with-param name="depth" select="$depth + 1" tunnel="yes"/>
<xsl:with-param name="line-breaking" select="false()" tunnel="yes"/>
</xsl:apply-templates>
</xsl:for-each>
<xsl:text> )</xsl:text>
</xsl:when>
<xsl:when test="r:formula | r:termula">
<xsl:apply-templates>
<xsl:with-param name="depth" select="$depth + 1" tunnel="yes"/>
<xsl:with-param name="line-breaking" select="false()" tunnel="yes"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$empty-form"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="r:Atom | r:Expr"> <!-- Expr is in Hornlog. -->
<xsl:apply-templates select="r:op"/>
<!-- sorted args -->
<xsl:for-each select="r:arg">
<xsl:choose>
<xsl:when test="preceding-sibling::r:arg">
<xsl:choose>
<xsl:when test="child::r:Data"></xsl:when>
<xsl:otherwise>
<xsl:text>,</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:text>(</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="r:Ind | r:Var | r:Expr"/>
<xsl:if test="not(following-sibling::r:arg)">
<xsl:text>)</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- variables in the TPTP language start with a uppercase letter -->
<xsl:template match="r:Var">
<xsl:variable name="normalized-text" select="normalize-space(text())" as="xs:string"/>
<xsl:value-of select="concat(upper-case(substring($normalized-text, 1, 1)), substring($normalized-text, 2))"/>
</xsl:template>
<!-- constants and functors in the TPTP language start with a lowercase letter or is single-quoted -->
<xsl:template match="r:Rel | r:Ind | r:Fun | r:Const"> <!-- Fun is in Hornlog. -->
<xsl:choose>
<xsl:when test="string(@iri)">
<xsl:call-template name="normalize-text">
<xsl:with-param name="text" select="@iri"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="normalize-text">
<xsl:with-param name="text" select="string(text())"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- a comma-separated list of declarations -->
<xsl:template name="declare-list">
<xsl:for-each select="r:declare">
<xsl:if test="preceding-sibling::r:declare">
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:apply-templates select="r:*"/>
</xsl:for-each>
</xsl:template>
<xsl:template name="normalize-text">
<xsl:param name="text" required="yes" as="xs:string"/>
<xsl:variable name="normalized-text" select="normalize-space($text)" as="xs:string"/>
<xsl:choose>
<xsl:when test="matches($normalized-text, '^[a-z][a-z0-9_]*$', 'i')">
<xsl:value-of select="concat(lower-case(substring($normalized-text, 1, 1)), substring($normalized-text, 2))"/>
</xsl:when>
<xsl:otherwise>
<!-- escape \ and ' then single quote -->
<xsl:value-of select='concat("'",
replace(replace($text, "\\", "\\\\"), "'", "\\'"), "'")'/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Higher Order -->
<xsl:template match="r:Uniterm">
<xsl:text>(</xsl:text>
<xsl:call-template name="join">
<xsl:with-param name="list" select="r:*"/>
<xsl:with-param name="separator" select="' @ '" />
</xsl:call-template>
<xsl:text>)</xsl:text>
</xsl:template>
<xsl:template match="r:Lambda">
<xsl:text>(</xsl:text>
<xsl:text>^ [</xsl:text>
<xsl:call-template name="join">
<xsl:with-param name="list" select="r:declare" />
<xsl:with-param name="separator" select="','" />
</xsl:call-template>
<xsl:text>] : </xsl:text>
<xsl:apply-templates select="r:termula"/>
<xsl:text>)</xsl:text>
</xsl:template>
<xsl:template match="r:Entitype">
<xsl:apply-templates select="r:entity"/>
<xsl:text>:</xsl:text>
<xsl:apply-templates select="r:type"/>
</xsl:template>
<xsl:template match="r:Tmap">
<xsl:text>(</xsl:text>
<xsl:apply-templates select="r:domain"/>
<xsl:text>></xsl:text>
<xsl:apply-templates select="r:codomain"/>
<xsl:text>)</xsl:text>
</xsl:template>
<xsl:template match="r:Tbase">
<xsl:text>$</xsl:text>
<xsl:value-of select="text()"/>
</xsl:template>
<!-- Modal -->
<xsl:template match="r:Modal">
<xsl:text>(</xsl:text>
<xsl:apply-templates select="r:variety"/>
<xsl:text>:</xsl:text>
<xsl:text>(</xsl:text> <!-- This pair of parens is required by THF but not FOL. Put them here as a interim solution. -->
<xsl:apply-templates select="r:proposition"/>
<xsl:text>)</xsl:text>
<xsl:text>)</xsl:text>
</xsl:template>
<xsl:template match="r:Box">
<xsl:text>#box</xsl:text>
</xsl:template>
<xsl:template match="r:Dia">
<xsl:text>#dia</xsl:text>
</xsl:template>
<!-- Utilities -->
<xsl:template name="join">
<xsl:param name="list"/>
<xsl:param name="separator" as="xs:string"/>
<xsl:for-each select="$list">
<xsl:apply-templates/>
<xsl:if test="position() != last()">
<xsl:value-of select="$separator"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>