-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.lisp
972 lines (836 loc) · 24.1 KB
/
parse.lisp
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
(in-package :dialogues)
;;; The lexer
(define-condition lexer-error (yacc-runtime-error)
((character :initarg :character :reader lexer-error-character))
(:report (lambda (e stream)
(format stream "Lexing failed~@[: unexpected character ~S~]"
(lexer-error-character e)))))
(defun lexer-error (char)
(error (make-condition 'lexer-error :character char)))
(defun maybe-unread (char stream)
(when char
(unread-char char stream)))
(defun read-word (stream)
(let ((v '()))
(loop
(let ((c (read-char stream nil nil)))
(when (or (null c)
(not (or (digit-char-p c)
(alpha-char-p c)
(char-equal c #\_))))
(maybe-unread c stream)
(when (null v)
(lexer-error c))
(return-from read-word (coerce (nreverse v) 'string)))
(push c v)))))
(defun read-quoted-atom (stream)
(loop
:with v = nil
:with first-single-quote-read = nil
:for c = (read-char stream nil nil)
:do
(cond ((null c)
(lexer-error (if (null v)
"read-quoted-atom failed"
(car v))))
((char= c #\')
(when first-single-quote-read
(return-from read-quoted-atom
(coerce (nreverse v) 'string)))
(setf first-single-quote-read t))
(t
(push c v)))))
(defun read-integer (stream)
(loop
:with v = nil
:for c = (read-char stream nil nil)
:do
(cond ((null c)
(lexer-error (if (null v)
"read-integer failed"
(car v))))
((digit-char-p c)
(push c v))
(t
(unread-char c stream)
(return-from read-integer
(parse-integer (coerce (nreverse v) 'string)))))))
(defparameter *tptp-keywords*
(list
;; annotated formula keywords
"fof"
"cnf"
"include"
;; formula roles
))
(defparameter *formula-roles*
(list "axiom"
"hypothesis"
"definition"
"assumption"
"lemma"
"theorem"
"conjecture"
"negated_conjecture"
"plain"
"fi_domain"
"fi_functors"
"fi_predicates"
"type"
"unknown"
))
(defparameter *whitespace-characters*
'(#\Space #\Tab #\Newline #\Return))
(let (toplevel-p num-commas-read within-include)
(defun initialize-lexer ()
(setf toplevel-p t)
(setf num-commas-read 0)
(setf within-include nil)
t)
(defun lexer (stream)
(loop
for c = (read-char stream nil nil)
do
(cond
((null c)
(return-from lexer (values nil nil)))
((char= c #\%)
(read-line stream)) ;; consume comment lines
((member c *whitespace-characters*)) ;; consume whitespace
((digit-char-p c)
(unread-char c stream)
(return-from lexer (values (intern "integer" :dialogues)
(read-integer stream))))
((char= c #\')
(unread-char c stream)
(let ((quoted (read-quoted-atom stream)))
(return-from lexer (values (intern "single-quoted" :dialogues) quoted))))
((char= c #\$)
(return-from lexer (values (intern "$" :dialogues) "$")))
((member c '(#\( #\) #\. #\[ #\] #\: #\! #\? #\, #\< #\~ #\= #\&))
;; (break "Got a symbol: ~a" c)
(when (char= c #\.)
(initialize-lexer))
(when (char= c #\,)
(incf num-commas-read)
(return-from lexer (values (intern "," :dialogues) ",")))
(when (char= c #\~)
(let ((after-~ (read-char stream nil nil)))
(cond ((null after-~)
(lexer-error #\~))
((member after-~ *whitespace-characters*)
(unread-char after-~ stream)
(return-from lexer (values (intern "~" :dialogues) "~")))
((char= after-~ #\&)
(return-from lexer (values (intern "~&" :dialogues) "~&")))
(t
(unread-char after-~ stream)
(return-from lexer (values (intern "~" :dialogues) "~"))))))
(when (char= c #\!)
(let ((after-! (read-char stream nil nil)))
(cond ((null after-!)
(lexer-error #\!))
((member after-! *whitespace-characters*)
(unread-char after-! stream)
(return-from lexer (values (intern "!" :dialogues) "!")))
((char= after-! #\=)
(return-from lexer (values (intern "!=" :dialogues) "!=")))
(t
(unread-char after-! stream)
(return-from lexer (values (intern "!" :dialogues) "!"))))))
(when (char= c #\<)
(let ((after-< (read-char stream nil nil)))
(cond ((null after-<)
(lexer-error #\<))
((member after-< *whitespace-characters*)
(lexer-error after-<))
((char= after-< #\=)
(let ((after-after-< (read-char stream nil nil)))
(cond ((null after-after-<)
(lexer-error after-<))
((char= after-after-< #\>)
(return-from lexer (values (intern "<=>" :dialogues) "<=>")))
(t
(return-from lexer (values (intern "<=" :dialogues) "<="))))))
((char= after-< #\~)
(let ((after-after-< (read-char stream nil nil)))
(cond ((null after-after-<)
(lexer-error after-<))
((char= after-after-< #\>)
(return-from lexer (values (intern "<~>" :dialogues) "<~>")))
(t
(lexer-error after-<)))))
(t
(lexer-error #\<)))))
(when (char= c #\=)
(let ((d (read-char stream nil nil)))
(if d
(if (char= d #\>)
(return-from lexer (values (intern "=>" :dialogues) "=>"))
(progn
(unread-char d stream)
(return-from lexer (values (intern "=" :dialogues) "="))))
(lexer-error d))))
(return-from lexer (values (intern (string c) :dialogues) (string c))))
((char= c #\|)
(return-from lexer (values (intern "|" :dialogues) "|")))
;; try to read an atom
(toplevel-p
(unread-char c stream)
(let ((next-word (read-word stream)))
(if (member next-word *tptp-keywords* :test #'string=)
(progn
(when (string= next-word "include")
(setf within-include t))
(setf toplevel-p nil)
(return-from lexer (values (intern next-word :dialogues) next-word)))
(error "Don't know how to handle the toplevel word '~a'." next-word))))
((and (= num-commas-read 1)
(not within-include))
(unread-char c stream)
(let ((next-word (read-word stream)))
(if (member next-word *formula-roles* :test #'string=)
(progn
(return-from lexer (values (intern next-word :dialogues) next-word)))
(error "Unknown formula role '~a'." next-word))))
((alpha-char-p c)
(unread-char c stream)
(let ((next-word (read-word stream)))
;; (break "next-word = ~a" next-word)
(cond ((lower-case-p c)
(return-from lexer (values (intern "lower-word" :dialogues) next-word)))
((upper-case-p c)
(return-from lexer (values (intern "upper-word" :dialogues) next-word)))
(t
(error "Don't know how to handle '~a'." next-word)))))
(t
(lexer-error c))))))
;;; The parser and semantic actions
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun k-2-3 (a b c)
"Second out of three."
(declare (ignore a c))
b)
)
(define-parser *tptp-v5.4.0.0-parser*
(:start-symbol tptp-file)
(:terminals (|fof|
|cnf|
|(|
|)|
|.|
|,|
|[|
|]|
|=|
|lower-word|
|upper-word|
|$|
|'|
|axiom|
|hypothesis|
|definition|
|assumption|
|lemma|
|theorem|
|conjecture|
|negated_conjecture|
|plain|
|fi_domain|
|fi_functors|
|fi_predicates|
|type|
|unknown|
|single-quoted|
|include|
|integer|
|<=>|
|=>|
|<=|
|<~>|
|~&|
|:|
|!|
|?|
|~|
|!=|
|&|
|\||
|axiom_of_choice|
|tautology|
))
(tptp-file
tptp-inputs)
(tptp-inputs
()
(tptp-input tptp-inputs
#'(lambda (arg-1 arg-2)
(make-instance 'tptp-db
:formulas (cons arg-1
(when arg-2
(formulas arg-2)))))))
(tptp-input
annotated-formula
include)
(include
(|include| |(| file-name formula-selection |)| |.|
#'(lambda (word left-paren file-name formula-selection right-paren full-stop)
(declare (ignore left-paren right-paren full-stop))
(unless (string= word "include")
(error "We are expecting 'include', but we received '~a'." word))
(make-instance 'include-instruction
:file file-name
:selection formula-selection)))
)
(file-name
(|single-quoted| #'(lambda (x)
(format nil "'~a'" x))))
(formula-selection
()
(|,| |[| name-list |]|
#'(lambda (comma left-bracket stuff right-bracket)
(declare (ignore comma left-bracket right-bracket))
stuff)))
(name-list
(name #'list)
(name |,| name-list
#'(lambda (head comma tail)
(declare (ignore comma))
(cons head tail))))
(name
atomic-word
integer)
(annotated-formula
fof-annotated
cnf-annotated)
(fof-annotated
(|fof| |(| name |,| formula-role |,| fof-formula |)| |.|
#'(lambda (fof-symbol left-paren name comma-1 role comma-2 formula right-paren full-stop)
(declare (ignore fof-symbol
left-paren
comma-1
comma-2
right-paren
full-stop))
(make-instance 'fof
:name name
:role role
:formula formula)))
(|fof| |(| name |,| formula-role |,| fof-formula |,| source |)| |.|
#'(lambda (fof-symbol left-paren name comma-1 role comma-2 formula comma-3 source right-paren full-stop)
(declare (ignore fof-symbol
left-paren
comma-1
comma-2
comma-3
right-paren
full-stop))
(make-instance 'fof
:name name
:role role
:formula formula
:source source)))
(|fof| |(| name |,| formula-role |,| fof-formula |,| source |,| optional-info|)| |.|
#'(lambda (fof-symbol left-paren name comma-1 role comma-2 formula comma-3 source comma-4 optional-info right-paren full-stop)
(declare (ignore fof-symbol
left-paren
comma-1
comma-2
comma-3
comma-4
right-paren
full-stop))
(make-instance 'fof
:name name
:role role
:formula formula
:source source
:optional-info optional-info))))
(cnf-annotated
(|cnf| |(| name |,| formula-role |,| cnf-formula |)| |.|
#'(lambda (cnf-symbol left-paren name comma-1 role comma-2 formula right-paren full-stop)
(declare (ignore cnf-symbol
left-paren
comma-1
comma-2
right-paren
full-stop))
(make-instance 'cnf
:name name
:role role
:formula formula)))
(|cnf| |(| name |,| formula-role |,| cnf-formula |,| source |)| |.|
#'(lambda (cnf-symbol left-paren name comma-1 role comma-2 formula comma-3 source right-paren full-stop)
(declare (ignore cnf-symbol
left-paren
comma-1
comma-2
comma-3
right-paren
full-stop))
(make-instance 'cnf
:name name
:role role
:formula formula
:source source)))
(|cnf| |(| name |,| formula-role |,| cnf-formula |,| source |,| optional-info |)| |.|
#'(lambda (cnf-symbol left-paren name comma-1 role comma-2 formula comma-3 source comma-4 optional-info right-paren full-stop)
(declare (ignore cnf-symbol
left-paren
comma-1
comma-2
comma-3
comma-4
right-paren
full-stop))
(make-instance 'cnf
:name name
:role role
:formula formula
:source source
:optional-info optional-info))))
(optional-info
useful-info
())
(source
(general-term
#'(lambda (x)
(if (typep x 'atomic-expression)
(with-slots (head arguments)
x
(if (and (string= (stringify head) "inference")
(length= 3 arguments)
(typep (third arguments) 'general-list))
(make-instance 'inference-record
:rule (first arguments)
:useful-info (second arguments)
:parents (third arguments))
x))
x))))
(sources
source
(source |,| sources))
(internal-source
(|introduced| |(| intro-type optional-info |)|))
(intro-type
|definition|
|axiom_of_choice|
|tautology|
|assumption|)
(external-source
file-source
theory
creator-source)
(file-source
;; (|file| |(| file-name file-info |)|)
(|lower-word| |(| file-name file-info |)|)) ;; the content of the
;; lower-word should be
;; the string "file"
(file-info
(|,| name)
()
)
(dag-source
name
inference-record)
(inference-record
;; the content of the first lower-word should be "inference"
(|lower-word| |(| inference-rule |,| useful-info |,| |[| parent-list |]| |)|)
;; (|inference| |(| inference-rule |,| useful-info |,| |[| parent-list |]|)
)
(parent-list
parent-info
(parent-info |,| parent-list))
(parent-info
(source parent-details))
(parent-details
(|:| general-list)
())
(inference-rule
atomic-word)
(useful-info
general-list)
(general-list
(|[| |]|
#'(lambda (left-bracket right-bracket)
(declare (ignore left-bracket right-bracket))
(make-instance 'general-list
:terms nil)))
(|[| general-terms |]|
#'(lambda (left-bracket stuff right-bracket)
(declare (ignore left-bracket right-bracket))
(make-instance 'general-list
:terms stuff))))
(general-terms
(general-term #'(lambda (x) (list x)))
(general-term |,| general-terms
#'(lambda (head comma tail)
(declare (ignore comma))
(cons head tail))))
(general-term
general-data
(general-data |:| general-term)
general-list)
(general-data
atomic-word
general-function
variable
number
;; distinct-object (not yet supported)
;; formula-data (not yet supported)
)
(general-function
(atomic-word |(| general-terms |)|
#'(lambda (word left-paren terms right-paren)
(declare (ignore left-paren right-paren))
(make-instance 'atomic-expression
:head (intern word :dialogues)
:arguments terms))))
(name
atomic-word
|integer|)
(number
|integer|
;; rational (not yet supported)
;; real (not yet supported)
)
(atomic-word
|lower-word|
|single-quoted|)
(formula-role
|axiom|
|hypothesis|
|definition|
|assumption|
|lemma|
|theorem|
|conjecture|
|negated_conjecture|
|plain|
|fi_domain|
|fi_functors|
|fi_predicates|
|type|
|unknown|)
(fof-formula
fof-logic-formula
fof-sequent)
(cnf-formula
(|(| disjunction |)|
#'(lambda (left-paren x right-paren)
(declare (ignore left-paren right-paren))
x))
disjunction)
(disjunction
literal
(literal |\|| disjunction
#'(lambda (head vbar tail)
(declare (ignore vbar))
(make-instance 'binary-disjunction
:lhs head
:rhs tail))))
(literal
atomic-formula
(|~| atomic-formula
#'(lambda (neg formula)
(declare (ignore neg))
(if (typep formula 'equation)
(make-instance 'disequation
:lhs (lhs formula)
:rhs (rhs formula))
(make-instance 'negation
:argument formula))))
fol-infix-unary)
(fof-logic-formula
fof-binary-formula
fof-unitary-formula)
(fof-binary-formula
fof-binary-nonassoc
fof-binary-assoc)
(fof-binary-nonassoc
(fof-unitary-formula fof-binary-connective fof-unitary-formula
#'(lambda (lhs connective rhs)
(make-instance (cond ((string= connective "<=>")
'equivalence)
((string= connective "<~>")
'nonequivalence)
((string= connective "=>")
'implication)
((string= connective "<=")
'reverse-implication))
:lhs lhs
:rhs rhs))))
(fof-binary-assoc
fof-or-formula
fof-and-formula)
(fof-or-formula
(fof-unitary-formula |\|| fof-unitary-formula
#'(lambda (disjunct-1 vbar disjunct-2)
(declare (ignore vbar))
(make-instance 'binary-disjunction
:lhs disjunct-1
:rhs disjunct-2)))
(fof-or-formula |\|| fof-unitary-formula
#'(lambda (disjunction vbar disjunct)
(declare (ignore vbar))
(make-instance 'binary-disjunction
:lhs disjunction
:rhs disjunct))))
(fof-and-formula
(fof-unitary-formula |&| fof-unitary-formula
#'(lambda (conjunct-1 ampersand conjunct-2)
(declare (ignore ampersand))
(make-instance 'binary-conjunction
:lhs conjunct-1
:rhs conjunct-2)))
(fof-and-formula |&| fof-unitary-formula
#'(lambda (conjunction ampersand conjunct)
(declare (ignore ampersand))
(make-instance 'binary-conjunction
:lhs conjunction
:rhs conjunct))))
(fof-binary-connective
|<=>|
|=>|
|<=|
|<~>|
;; |~\||
|~&|)
(fof-unitary-formula
(|(| fof-logic-formula |)|
#'(lambda (left-paren formula right-paren)
(declare (ignore left-paren right-paren))
formula))
fof-quantified-formula
fof-unary-formula
atomic-formula)
(fof-quantified-formula
(fol-quantifier |[| fof-variable-list |]| |:| fof-unitary-formula
#'(lambda (quantifier left-bracket variable-list right-bracket colon matrix)
(declare (ignore left-bracket right-bracket colon))
(make-instance (cond ((string= quantifier "!")
'universal-generalization)
((string= quantifier "?")
'existential-generalization)
(t
(error "Unknown quantifier '~a'." quantifier)))
:bindings variable-list
:matrix matrix))))
(atomic-formula
plain-atomic-formula
defined-atomic-formula
;; system-atomic-formula
)
(plain-atomic-formula
(plain-term #'(lambda (x)
(let ((head (stringify (head x)))
(args (arguments x)))
(if (string= head "=")
(make-instance 'equation
:lhs (first args)
:rhs (second args))
(make-instance 'atomic-formula
:head head
:arguments (arguments x)))))))
(defined-atomic-formula
defined-plain-formula
defined-infix-formula)
(defined-infix-formula
(term defined-infix-pred term
#'(lambda (left pred right)
(unless (string= (format nil "~a" pred) "=")
(error "Unknown infix predicate '~a'." pred))
(make-instance 'equation
:head (intern "=" :dialogues)
:arguments (list left right)
:lhs left
:rhs right))))
(defined-infix-pred
infix-equality)
(infix-equality
(|=| #'(lambda (x)
(declare (ignore x))
(intern "=" :dialogues))))
(defined-plain-formula
defined-prop
(defined-pred |(| arguments |)|))
(defined-prop
;; should be only "$true" and "$false"
(atomic-defined-word #'(lambda (word)
(cond ((string= word "true") (make-instance 'verum))
((string= word "false") (make-instance 'falsum))
(t
(error "Unknown atomic defined word '~a'." word))))))
(atomic-defined-word
dollar-word)
(dollar-word
(|$| |lower-word|
#'(lambda (dollar word)
(declare (ignore dollar))
word)))
(fol-quantifier
|!|
|?|)
(fof-variable-list
(variable
#'(lambda (x) (list x)))
(variable |,| fof-variable-list
#'(lambda (head-variable comma more-variables)
(declare (ignore comma))
(cons head-variable more-variables))))
(variable
(|upper-word| #'(lambda (x)
(make-instance 'variable-term
:head (stringify x)))))
(fof-unary-formula
(unary-connective fof-unitary-formula
#'(lambda (connective argument)
(unless (string= connective "~")
(error "Unknown unary connective '~a'." connective))
(if (typep argument 'equation)
(make-instance 'disequation
:lhs (lhs argument)
:rhs (rhs argument))
(make-instance 'negation
:argument argument))))
fol-infix-unary)
(unary-connective
|~|)
(fol-infix-unary
(term infix-inequality term
#'(lambda (lhs disequality-symbol rhs)
(declare (ignore disequality-symbol))
(make-instance 'disequation
:lhs lhs
:rhs rhs))))
(term
function-term
variable
;; conditional-term
;; let-term
)
(function-term
(plain-term #'(lambda (x)
(make-instance 'function-term
:head (stringify (head x))
:arguments (arguments x))))
defined-term
system-term)
(plain-term
(constant
#'(lambda (c)
(make-instance 'atomic-expression
:head (stringify c)
:arguments nil)))
(functor |(| arguments |)|
#'(lambda (f lparen args rparen)
(declare (ignore lparen rparen))
(make-instance 'function-term
:head (stringify f)
:arguments args))))
(constant
functor)
(functor
atomic-word)
(arguments
(term #'(lambda (x)
(list x)))
(term |,| arguments
#'(lambda (head-term comma more-terms)
(declare (ignore comma))
(cons head-term more-terms))))
(defined-term
defined-atom
defined-atomic-term)
(defined-atom
;; number
;; distinct-object
)
(defined-atomic-term
defined-plain-term)
(defined-plain-term
defined-constant
(defined-functor |(| arguments |)|))
(defined-constant
defined-functor
defined-type)
(infix-inequality
|!=|)
)
;;; The evaluator
(define-condition evaluator-error (yacc-runtime-error)
((expression :initarg :expression :reader evaluator-error-expression))
(:report (lambda (e stream)
(format stream "Couldn't evaluate expression ~S"
(evaluator-error-expression e)))))
;;; The toplevel loop
(defgeneric parse-tptp (tptp))
(defmethod parse-tptp ((tptp-string string))
(with-input-from-string (string tptp-string)
(initialize-lexer)
(parse-with-lexer #'(lambda ()
(lexer string))
*tptp-v5.4.0.0-parser*)))
(defmethod parse-tptp ((tptp-path pathname))
(with-open-file (tptp-stream tptp-path
:direction :input
:if-does-not-exist :error)
(initialize-lexer)
(parse-with-lexer #'(lambda ()
(lexer tptp-stream))
*tptp-v5.4.0.0-parser*)))
(defmethod parse-tptp :around ((tptp-path pathname))
(let ((db (call-next-method)))
(setf (path db) tptp-path)
db))
(defmethod parse-tptp ((tptp tptp-db))
tptp)
(defun lex-tptp-formula (string)
(with-input-from-string (stream string)
(loop
initially (initialize-lexer)
for token = (lexer stream)
collect token into tokens
unless token return tokens)))
(defmethod problematize ((path pathname))
(problematize (parse-tptp path)))
(defmethod formula-with-name ((tptp-path pathname) name)
(formula-with-name (parse-tptp tptp-path) name))
(defmethod expand-includes ((tptp-db pathname))
(expand-includes (parse-tptp tptp-db)))
(defmethod has-include-instruction-p ((problem pathname))
(has-include-instruction-p (parse-tptp problem)))
(defmethod expand-include ((include include-instruction) root-dir)
(with-slots (selection file)
include
(when (null selection)
(return-from expand-include nil))
(let ((real-file nil)
(new-formulas nil))
(cond ((file-exists-p file)
(setf real-file file))
((directory-exists-p root-dir)
(let ((file-in-dir (merge-pathnames file root-dir)))
(if (file-exists-p file-in-dir)
(setf real-file file-in-dir)
(error "No file found at '~a', nor could we find '~a'." (namestring file) (namestring file-in-dir)))))
((null root-dir)
(error "No file at '~a', and no root directory was supplied." (namestring file)))
(t
(error "No file at '~a', and to boot a bogus directory (~a) was supplied." (namestring file) root-dir)))
(let ((included-db (handler-case (parse-tptp real-file)
(error () nil))))
(unless included-db
(error "Error parsing '~a' as a TPTP file." (namestring real-file)))
(setf included-db (expand-includes included-db))
(loop
for selected in selection
for corresponding = (formula-with-name included-db
selected)
do
(when (null corresponding)
(error "There is no formula in the TPTP problem '~a' with the name '~a'." (namestring real-file) selected))
(push corresponding new-formulas)
finally
(return (reverse new-formulas)))))))
;;; parse.lisp ends here