-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathparse.lisp
More file actions
3494 lines (3201 loc) · 116 KB
/
parse.lisp
File metadata and controls
3494 lines (3201 loc) · 116 KB
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
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -*- Mode: Lisp -*- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; parse.lisp --
;; Author : Sam Owre
;; Created On : Thu Oct 21 19:36:29 1993
;; Last Modified By: Sam Owre
;; Last Modified On: Wed Nov 4 18:13:31 1998
;; Update Count : 54
;; Status : Stable
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; --------------------------------------------------------------------
;; PVS
;; Copyright (C) 2006, SRI International. All Rights Reserved.
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 2
;; of the License, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
;; --------------------------------------------------------------------
(in-package :pvs)
(defvar *allowed-ids* nil)
(defvar *allowed-typed-names* nil)
(defvar *newline-comments* nil)
(defmacro place-bind (vars place &rest body)
`(destructuring-bind ,vars (coerce ,place 'list) ,@body))
;;; Makes extensive use of the following functions from ERGO:
;;; sim-term-op - returns the symbol which is the operator of a term
;;; is-sop - checks whether a given symbol is the operator of a term
;;; term-args - returns the list of arguments of a term
;;; ds-id - returns the symbol of an id term
;;; Parsing
(defun parse (&rest keys)
(let* ((*current-file* (or (cadr (member :file keys))
*current-file*))
(start-time (when *current-file* (get-internal-real-time)))
(*newline-comments* nil)
(*last-escaped-quote-place* nil))
(init-parser)
(multiple-value-bind (term error? place msg args)
(handler-case
(apply #'pvs-parse :return-errors t keys)
#+sbcl
(sb-int:stream-decoding-error (err)
(let ((msg
(format nil
"Error: ~a~%This error usually is due to a character that is not UTF-8~
~%Run 'file ~a', and if it is not ASCII or UTF-8,~
~%change it using something like~
~% iconv -f latin1 -t UTF-8 < ~a > ~a.copy~
~%and move the .copy file after checking it with 'file'"
err *current-file* *current-file* *current-file*)))
(parse-error nil msg))))
(when error?
(let ((pargs (mapcar #'(lambda (arg)
(if (stringp arg)
(protect-format-string arg)
arg))
args)))
(parse-error place (parse-error-msg msg pargs))))
(let* ((nt (cadr (member :nt keys)))
(fn (if nt
(makesym #+(and allegro (version>= 6)) "xt-~(~a~)"
#-(and allegro (version>= 6)) "XT-~a"
(sim-term-op term))
#'xt-adt-or-modules))
(*parsing-or-unparsing* t)
(parsed-object (funcall fn term)))
;;(assert-places-set)
(values parsed-object
(if *current-file*
(realtime-since start-time)
0)
(nreverse *newline-comments*))))))
(defun init-parser ()
(setq *table-bracket-counter* 0)
(setq *double-braces-counter* 0)
(setq *elsif-places* nil)
(setq *operator-places* nil))
(defun parse-error-msg (message args)
(multiple-value-bind (found expected)
(cond ((initial-error? message)
(values (car args) (cadr args)))
((lam-error? message)
(values (cadr args) (car args)))
((medial-error? message)
(values (car args) (cadr args))))
(if found
(format nil "Found '~A' when expecting '~A'~
~:[~;~% Note: May need a ';' before declaring an infix operator~]~
~@[~% Note: '~a' is now a keyword~]~
~@[~% Note: '~a' is only allowed in theory declarations (not importings)~]~
~@[~% Note: This may be due to a string that maybe should have ended around ~a~]"
found expected
(string= found ":")
(when (string= found "AS") found)
(when (and (member found '("::=" "=") :test #'string=)
;; Check that ':=' is expected
(>= (length (string (cadr args))) 2)
(string= (cadr args) ":=" :end1 2))
found)
(when *last-escaped-quote-place*
(let ((line (sbrt::place-linenumber *last-escaped-quote-place*))
(col (sbrt::place-charnumber *last-escaped-quote-place*)))
(format nil "(line ~a, col ~a)" line col))))
(format nil "~?" message args))))
(defun initial-error? (message)
(and (stringp message)
(>= (length message) 16)
(string= message "Initial error." :start1 2 :end1 16)))
(defun lam-error? (message)
(and (stringp message)
(>= (length message) 27)
(string= message "Look ahead set match error." :start1 0 :end1 27)))
(defun medial-error? (message)
(and (stringp message)
(>= (length message) 13)
(string= message "Medial error." :start1 0 :end1 13)))
(defun parse-expr (string)
(init-parser)
(pvs-parse :string string :nt 'expr))
(defun psa (string &optional (nt 'adt-or-modules))
(init-parser)
(pvs-parse :string string :nt nt))
(defun ps (string &optional (nt 'adt-or-modules))
(init-parser)
(let* ((term (pvs-parse :string string :nt nt))
(fn (makesym "XT-~a" (sim-term-op term))))
(funcall fn term)))
(defvar *escaped-operators-used* nil)
(defun xt-adt-or-modules (adt-or-modules)
(mapcar #'(lambda (arg) (funcall #'xt-adt-or-module arg))
(term-args adt-or-modules)))
(defun xt-adt-or-module (adt-or-module)
(let* ((*escaped-operators-used* nil)
(id (ds-vid (term-arg0 adt-or-module)))
(formals (xt-theory-formals (term-arg1 adt-or-module)))
(adt-or-mod (term-arg2 adt-or-module))
(aorm (case (sim-term-op adt-or-mod)
(MODULE (funcall #'xt-module adt-or-mod id))
((DATATYPE CODATATYPE)
(funcall #'xt-datatype (sim-term-op adt-or-mod)
adt-or-mod id))
((DATATYPES CODATATYPES)
(funcall #'xt-datatypes (sim-term-op adt-or-mod)
adt-or-mod id)))))
(setf (id aorm) id
(formals aorm) formals
(place aorm) (term-place adt-or-module))
;;(assert (every #'place (formals aorm)))
(when *escaped-operators-used*
(let ((*current-context* (make-new-context aorm)))
(pvs-info "This theory uses the operator~p ~:*~{~a~^ ~}~
~%Remember that the backslash character must be doubled ~
in strings~%E.g., (expand \"/\\\\\")"
*escaped-operators-used*)))
(when *current-file*
(setf (filename aorm) (pathname-name *current-file*)))
(setf (context-path aorm) *default-pathname-defaults*)
aorm))
(defun xt-datatypes (class datatype &optional id inline)
(let* ((subtypes-term (term-arg0 datatype))
(subtypes (mapcar #'(lambda (st)
(make-instance 'type-name
:id (xt-idop (xt-pidop st))
:place (term-place st)))
(term-args subtypes-term)))
(adtbody (term-arg1 datatype))
(endidop (xt-pidop (term-arg2 datatype)))
(endid (ds-vid (term-arg0 endidop))))
(unless (or (null id)
(eq id endid))
(parse-error endidop
"End id ~a does not match datatype id ~a" endid id))
(multiple-value-bind (imps assuming adtcases)
(xt-adtbody (term-args adtbody))
(check-subtypes-constructors-consistency id subtypes adtcases)
(make-instance (if (or (null id) inline)
(if (eq class 'DATATYPES)
'inline-datatype-with-subtypes
'inline-codatatype-with-subtypes)
(if (eq class 'DATATYPES)
'datatype-with-subtypes
'codatatype-with-subtypes))
:subtypes subtypes
:assuming assuming
:importings imps
:constructors adtcases
:place (term-place datatype)))))
(defun check-subtypes-constructors-consistency (id subtypes adtcases)
(let ((badsubtype (find id subtypes :key #'id)))
(when badsubtype
(parse-error badsubtype "May not use datatype name as a subtype")))
(let ((dup (duplicates? subtypes :test #'same-id)))
(when dup
(parse-error dup "Duplicate subtypes are not allowed")))
(let ((badconstr (find-if #'(lambda (x)
(or (not (typep x 'constructor-with-subtype))
(not (member (subtype x) subtypes
:test #'same-id))))
adtcases)))
(when badconstr
(if (typep badconstr 'constructor-with-subtype)
(parse-error (subtype badconstr)
"The subtype for this constructor was not declared above")
(parse-error badconstr
"Must specify a subtype for this constructor"))))
(let ((badsubtype (find-if-not #'(lambda (s)
(some #'(lambda (a)
(same-id s (subtype a)))
adtcases))
subtypes)))
(when badsubtype
(parse-error badsubtype
"This subtype has no associated constructors"))))
(defun xt-theory-formals (theory-formals &optional decl-formal?)
(mapcan #'(lambda (ta) (xt-theory-formal ta decl-formal?))
(term-args theory-formals)))
(defun change-to-decl-formal (decl)
(change-class decl
(typecase decl
(formal-nonempty-struct-subtype-decl 'decl-formal-nonempty-struct-subtype)
(formal-struct-subtype-decl 'decl-formal-struct-subtype)
(formal-nonempty-subtype-decl 'decl-formal-nonempty-subtype)
(formal-subtype-decl 'decl-formal-subtype)
(formal-nonempty-type-decl 'decl-formal-nonempty-type)
(formal-nonempty-type-appl-decl 'decl-formal-nonempty-type-appl)
(formal-type-appl-decl 'decl-formal-type-appl)
(formal-type-decl 'decl-formal-type)
(formal-const-decl 'decl-formal-const-decl)
(formal-theory-decl 'decl-formal-theory-decl))))
(defun xt-theory-formal (theory-formal &optional decl-formal?)
(let* ((importing (term-arg0 theory-formal))
(idops (xt-check-periods (term-arg1 theory-formal)))
(decl-body (term-arg2 theory-formal)))
(multiple-value-bind (decl dtype)
(xt-declaration-body decl-body)
(when decl-formal?
;; (unless (formal-type-decl? decl)
;; (parse-error theory-formal
;; "Only type declarations allowed for declaration parameters"))
(change-to-decl-formal decl))
(append (unless (is-sop 'THEORY-FORMAL-NULL-1 importing)
(xt-formal-importing-elt importing))
(xt-chained-decls (term-args idops) nil dtype nil decl
theory-formal)))))
(defun xt-datatype (class datatype &optional id inline)
(let* ((adtbody (term-arg0 datatype))
(endidop (xt-pidop (term-arg1 datatype)))
(endid (ds-vid (term-arg0 endidop))))
(unless (or (null id)
(eq id endid))
(parse-error (term-arg1 datatype)
"End id ~a does not match datatype id ~a" endid id))
(multiple-value-bind (imps assuming adtcases)
(xt-adtbody (term-args adtbody))
(make-instance (if (or (null id) inline)
(if (eq class 'DATATYPE)
'inline-datatype
'inline-codatatype)
(if (eq class 'DATATYPE)
'datatype
'codatatype))
:assuming assuming
:importings imps
:constructors adtcases
:place (term-place datatype)))))
(defun xt-adtbody (adtbody &optional imps assuming adtcases)
(if (null adtbody)
(values imps assuming (nreverse adtcases))
(case (sim-term-op (car adtbody))
(ID-THEORY-DECL
(if (or assuming adtcases)
(parse-error (car adtbody) "Cannot have assuming here")
(let ((id (xt-check-periods (term-arg0 (car adtbody))))
(tdecl (xt-theory-decl (term-arg1 (car adtbody)))))
(setf (id tdecl) (xt-idop (xt-pidop id)))
(xt-adtbody (cdr adtbody) (cons tdecl imps) assuming adtcases))))
(IMPORTING-ELT
(if (or assuming adtcases)
(parse-error (car adtbody) "Cannot have assuming here")
(let ((imp (xt-importing-elt (car adtbody))))
(xt-adtbody (cdr adtbody) (append imp imps) assuming adtcases))))
(ASSUMING
(if (or assuming adtcases)
(parse-error (car adtbody) "Cannot have assuming here")
(let ((ass (xt-assumings (car adtbody))))
(xt-adtbody (cdr adtbody) imps ass adtcases))))
(ID-ADTCASE
(let ((adtcase (xt-id-adtcase (car adtbody))))
(xt-adtbody (cdr adtbody) imps assuming (cons adtcase adtcases))))
(t (error "Huh?")))))
(defun xt-id-adtcase (id-adtcase)
(let* ((constrid (term-arg0 id-adtcase))
(dformals (xt-theory-formals (term-arg1 id-adtcase)))
(adtcase (term-arg2 id-adtcase))
(constructor (term-arg0 adtcase))
(recognizer (term-arg1 adtcase))
(constr (xt-constructor constrid constructor))
(subtype (when (is-sop 'ADTCASE-SUBTYPE adtcase)
(xt-check-periods (term-arg2 adtcase))
(make-instance 'type-name
:id (xt-idop (xt-pidop (term-arg2 adtcase)))
:place (term-place (term-arg2 adtcase))))))
(xt-check-periods recognizer)
(when subtype
(change-class constr 'constructor-with-subtype)
(setf (subtype constr) subtype))
(setf (decl-formals constr) dformals)
(setf (recognizer constr) (xt-idop (xt-pidop recognizer)))
(setf (place constr) (term-place constructor))
constr))
(defun xt-constructor (id adtdecls)
(let ((idop (xt-check-periods id)))
(make-instance 'simple-constructor
:id (xt-idop (xt-pidop idop))
:arguments (unless (is-sop 'CONSTRUCTOR-NULL-1 adtdecls)
(xt-adtdecls adtdecls))
:place (term-place-interval id adtdecls))))
(defun xt-adtdecls (decls)
(mapcan #'xt-adtdecl (term-args decls)))
(defun xt-adtdecl (adtdecl)
(let* ((idops (xt-check-periods (term-arg0 adtdecl)))
(type (term-arg1 adtdecl))
(dtype (xt-not-enum-type-expr type)))
(xt-chained-decls (term-args idops)
nil
type
nil
(make-instance 'adtdecl
:declared-type dtype
:place (term-place type))
adtdecl)))
(defun xt-module (theory &optional id)
(let* ((exp-t (term-arg0 theory))
(exp (if (is-sop 'NOEXP exp-t)
(make-instance 'exporting
:kind 'default)
(xt-exporting exp-t)))
(assum-t (term-arg1 theory))
(assum (unless (is-sop 'NOASS assum-t)
(xt-assumings assum-t)))
(tpart-t (term-arg2 theory))
(tpart (unless (is-sop 'NOTHEORY tpart-t)
(xt-theory-part tpart-t)))
(endid (ds-vid (term-arg3 theory))))
(unless (or (null id) (eq id endid))
(parse-error (term-arg3 theory)
"End id ~a does not match theory id ~a" endid id))
;; (assert (every #'place (modules exp)))
;; (assert (every #'place assum))
;; (assert (every #'place tpart))
(let ((th (make-instance 'module
:exporting exp
:assuming assum
:theory tpart
:place (term-place theory))))
(dolist (decl assum)
(when (declaration? decl)
(setf (module decl) th)))
(dolist (decl tpart)
(when (declaration? decl)
(setf (module decl) th)))
th)))
(defun xt-exporting (exporting)
(let ((names (term-arg0 exporting))
(modnames (term-arg1 exporting)))
(make-instance 'exporting
:names (case (sim-term-op names)
(EXPORTING-ALL 'all)
(EXPNAMES (mapcar #'xt-expname (term-args names)))
(t (break "Something's wrong with the parser")))
:but-names (when (and (is-sop 'EXPORTING-ALL names)
(not (is-sop 'NOEXPBUTS (term-arg0 names))))
(mapcar #'xt-expname
(term-args (term-arg0 names))))
:kind (case (sim-term-op modnames)
(EXPMODALL 'all)
(EXPMODCLOSURE 'closure)
(t nil))
:modules (case (sim-term-op modnames)
(NOEXPORTINGMODS nil)
(EXPMODALL nil)
(EXPMODCLOSURE nil)
(t (mapcar #'xt-modname (term-args modnames))))
:place (term-place exporting))))
(defun xt-expname (expname)
(let* ((pid (xt-pidop (term-arg0 expname)))
(id (xt-idop pid))
(kind (term-arg1 expname)))
(make-instance 'expname
:id id
:kind (case (sim-term-op kind)
(NOEXPKIND nil)
(EXPTYPE 'type)
(EXPFORMULA 'formula)
(t (xt-not-enum-type-expr kind)))
:place (term-place expname))))
(defun xt-assuming-part (assumings)
(mapcan #'(lambda (ass)
(if (is-sop 'IMPORTING-ELT ass)
(xt-importing-elt ass)
(xt-assuming ass)))
(term-args assumings)))
(defun xt-assumings (assumings)
(mapcan #'(lambda (ass)
(if (is-sop 'IMPORTING-ELT ass)
(xt-importing-elt ass)
(xt-assuming ass)))
(term-args assumings)))
(defun xt-assuming (assuming)
(let* ((_decl (term-arg0 assuming))
(idops (xt-check-periods (term-arg1 assuming)))
(decl-params (unless (is-sop 'NO-THEORY-FORMALS (term-arg2 assuming))
(xt-theory-formals (term-arg2 assuming) t)))
(formals (term-arg3 assuming))
(pdformals (unless (is-sop 'NOFORMALS formals)
(xt-pdf formals)))
(decl (term-arg4 assuming))
(semi (term-arg5 assuming)))
(when (and (cdr (term-args idops)) pdformals)
(parse-error formals ": expected here"))
(case (sim-term-op decl)
((LIB-DECL THEORY-DECL TYPE-DECL DATATYPE)
(let ((badid (find-if #'(lambda (tid)
(assq (ds-id (term-arg0 tid))
*pvs-operators*))
(term-args idops))))
(when badid
(parse-error badid "Id expected here")))))
(multiple-value-bind (pdecl tdecl)
(xt-declaration-body decl idops)
(let ((decls (xt-chained-decls (term-args idops)
decl-params
tdecl
pdformals
pdecl
assuming)))
(setf (semi pdecl)
(if (is-sop 'SEMIC semi)
(if (is-sop 'ATDECL _decl)
'both t)
(if (is-sop 'ATDECL _decl)
'_decl nil)))
decls))))
(defun xt-theory-part (theory)
(mapcan #'(lambda (decl)
(case (sim-term-op decl)
(IMPORTING-ELT (xt-importing-elt decl))
(JUDGEMENT-ELT (xt-judgement-elt decl))
(CONVERSION-ELT (xt-conversion-elt decl))
(AUTO-REWRITE-ELT (xt-auto-rewrite-elt decl))
(t (xt-theory decl))))
(term-args theory)))
(defun xt-theory (tdecl)
(let* ((_decl (term-arg0 tdecl))
(idops (xt-check-periods (term-arg1 tdecl)))
(decl-params (unless (is-sop 'NO-THEORY-FORMALS (term-arg2 tdecl))
(xt-theory-formals (term-arg2 tdecl) t)))
(formals (term-arg3 tdecl))
(pformals (unless (is-sop 'NOFORMALS formals)
(xt-pdf formals)))
(decl (term-arg4 tdecl))
(semi (term-arg5 tdecl)))
(when (and (cdr (term-args idops)) pformals)
(parse-error formals ": expected here"))
(when (and decl-params
(memq (sim-term-op decl) '(LIB-DECL VAR-DECL)))
(parse-error decl-params "Declaration formals not allowed here"))
(case (sim-term-op decl)
((LIB-DECL THEORY-DECL TYPE-DECL DATATYPE CODATATYPE)
(let ((badid (find-if #'(lambda (tid)
(assq (ds-id (term-arg0 tid))
*pvs-operators*))
(term-args idops))))
(when badid
(parse-error badid "Id expected here")))))
(case (sim-term-op decl)
((DATATYPE CODATATYPE)
(xt-theory-datatype idops decl-params formals decl semi))
((DATATYPES CODATATYPES) ;; with subtypes
(xt-theory-datatypes idops decl-params formals decl semi))
(t (multiple-value-bind (pdecl type-decl)
(xt-declaration-body decl idops)
(let ((decls (xt-chained-decls (term-args idops)
decl-params
type-decl
pformals
pdecl
tdecl)))
#+pvsdebug ; Careful! nil is a valid PVS id
(assert (every #'(lambda (d)
(or (not (const-decl? d))
(id d)))
decls))
(assert (every #'place decls))
(setf (semi pdecl)
(if (is-sop 'SEMIC semi)
(if (is-sop 'ATDECL _decl)
'both t)
(if (is-sop 'ATDECL _decl)
'_decl nil)))
decls))))))
(defun xt-pidop (pidop)
(if (cdr (term-args pidop))
(let ((idop (mk-ergo-term 'IDOP
(list (mk-id (makesym "~{~a~^.~}"
(mapcar #'xt-idop (term-args pidop))))))))
(setf (term-place idop) (term-place pidop))
idop)
(term-arg0 pidop)))
(defun xt-check-periods (pidops)
(let ((nterm (mk-ergo-term (sim-term-op pidops)
(mapcar
#'(lambda (pidop)
(if (is-sop 'IDOPAPPL pidop)
(parse-error pidops "parens not allowed here")
(if (cdr (term-args pidop)) ;; have periods
(if *xt-periods-allowed*
(xt-pidop pidop)
(parse-error pidops
"periods not allowed here"))
(if (is-sop 'PIDOP pidop)
(term-arg0 pidop)
pidop))))
(term-args pidops)))))
(setf (term-place nterm) (term-place pidops))
nterm))
(defun xt-theory-datatypes (idops decl-params formals decl semi)
(cond ((is-sop 'PDF formals)
(parse-error (term-arg0 formals)
"Inline datatypes may not have parameters"))
((cdr (term-args idops))
(parse-error (term-arg0 (term-arg1 idops))
"Only one id allowed for datatypes"))
((not (is-id (term-arg0 (term-arg0 idops))))
(parse-error (term-arg0 idops)
"Datatype identifier must be an id"))
(t (let* ((id (ds-vid (term-arg0 (term-arg0 idops))))
(adt (funcall #'xt-datatypes (sim-term-op decl)
decl id t)))
(setf (id adt) id
(formals adt) (xt-theory-formals formals t)
(decl-formals adt) decl-params
(semi adt) (when (is-sop 'SEMIC semi) t))
(list adt)))))
(defun xt-theory-datatype (idops decl-params formals decl semi)
(cond ((is-sop 'PDF formals)
(parse-error (term-arg0 formals)
"Inline (co)datatypes may not have theory parameters"))
((cdr (term-args idops))
(parse-error (term-arg0 (term-arg1 idops))
"Only one id allowed for datatypes"))
((not (is-id (term-arg0 (term-arg0 idops))))
(parse-error (term-arg0 idops)
"Datatype identifier must be an id"))
(t (let* ((id (ds-vid (term-arg0 (term-arg0 idops))))
(adt (funcall #'xt-datatype (sim-term-op decl)
decl id t)))
(setf (id adt) id
(formals adt) (xt-theory-formals formals t)
(decl-formals adt) decl-params
(semi adt) (when (is-sop 'SEMIC semi) t))
(list adt)))))
(defun xt-importing-elt (importing-elt)
(let* ((importing (term-arg0 importing-elt))
(imp-place (term-place importing))
(importings
(mapcar #'(lambda (item)
(let* ((it-place (term-place item))
(place (vector (starting-row imp-place)
(starting-col imp-place)
(ending-row it-place)
(ending-col it-place))))
(if (is-sop 'THEORY-ABBREVIATION-DECL item)
(make-instance 'theory-abbreviation-decl
:id (xt-idop (xt-pidop (term-arg1 item)))
:theory-name (xt-modname (term-arg0 item))
:place place
:semi (when (is-sop 'SEMIC
(term-arg1 importing-elt))
t)
:chain? t)
(make-instance 'importing
:theory-name (xt-modname item)
:place place
:semi (when (is-sop 'SEMIC
(term-arg1 importing-elt))
t)
:chain? t))))
(term-args importing))))
(setf (chain? (car (last importings))) nil)
importings))
(defun xt-formal-importing-elt (importing)
(let ((lib-decl
(unless (is-sop 'NOLIB (term-arg0 importing))
(break "formal importing with lib: needs fixing")))
(importings
(mapcar #'(lambda (item)
(let* ((imp-place (term-place importing))
(it-place (term-place item))
(place (vector (starting-row imp-place)
(starting-col imp-place)
(ending-row it-place)
(ending-col it-place))))
(if (is-sop 'THEORY-ABBREVIATION-DECL item)
(make-instance 'theory-abbreviation-decl
:id (ds-id (term-arg1 item))
:theory-name (xt-modname (term-arg0 item))
:place place
:chain? t)
(make-instance 'importing
:theory-name (xt-modname item)
:place place
:chain? t))))
(term-args (term-arg1 importing)))))
(setf (chain? (car (last importings))) nil)
(if lib-decl
(cons lib-decl importings)
importings)))
(defun xt-judgement-elt (decl)
(let ((jdecls (xt-judgement (term-arg0 decl))))
(dolist (jdecl jdecls)
(setf (semi jdecl) (when (is-sop 'SEMIC (term-arg1 decl)) t)))
jdecls))
(defun xt-judgement-decl (decl idops)
(multiple-value-bind (jdecls dtype)
(xt-judgement decl)
(cond ((cdr (term-args idops))
(parse-error idops
"May not declare multiple ids for a judgement"))
((cdr jdecls)
(parse-error decl
"May not declare multiple judgements when an id is given"))
(t (values (car jdecls) dtype)))))
(defun xt-judgement (decl)
(let* ((jdecls (term-args (term-arg0 decl)))
(class (ds-id (term-arg1 decl)))
(type (term-arg2 decl))
(dtype (xt-not-enum-type-expr type))
(place (term-place decl)))
(values (case class
(HAS-TYPE
(xt-const-judgement jdecls dtype place))
(SUBTYPE-OF
(xt-subtype-judgement jdecls dtype place))
(REC-HAS-TYPE
(xt-rec-judgement jdecls dtype place))
(REC-SUBTYPE-OF
(parse-error decl
"RECURSIVE not allowed for subtype judgements")))
type)))
(defun xt-subtype-judgement (jdecls dtype place)
(let ((decls (mapcar #'(lambda (jdecl)
(xt-subtype-judgement* jdecl dtype place))
jdecls)))
(setf (chain? (car (last decls))) nil)
decls))
(defun appl-judgement-form? (expr-term)
(cond ((is-sop 'APPLICATION expr-term)
(and (cond ((is-sop 'NAME-EXPR (term-arg0 expr-term))
(and (is-sop 'NOTYPE (term-arg1 (term-arg0 expr-term)))
(is-sop 'NOPRED (term-arg2 (term-arg0 expr-term)))))
((is-sop 'APPLICATION (term-arg0 expr-term))
(appl-judgement-form? (term-arg0 expr-term)))
(t nil))
(is-sop 'FUN-ARGUMENTS (term-arg1 expr-term))
(term-args (term-arg1 expr-term))
(every #'(lambda (fa)
(or (is-sop 'NAME-EXPR fa)
(and (is-sop 'TUPLE-EXPR fa)
(and (term-args fa)
(every #'(lambda (tu) (is-sop 'NAME-EXPR tu))
(term-args fa))))))
(term-args (term-arg1 expr-term)))))
((is-sop 'UNARY-TERM-EXPR expr-term)
(and (cond ((is-sop 'TUPLE-EXPR (term-arg1 expr-term))
(every #'(lambda (fa)
(or (is-sop 'NAME-EXPR fa)
(and (is-sop 'TUPLE-EXPR fa)
(and (term-args fa)
(every #'(lambda (tu) (is-sop 'NAME-EXPR tu))
(term-args fa))))))
(term-args (term-arg1 expr-term))))
(t nil))))
((is-sop 'TERM-EXPR expr-term)
)))
(defun xt-subtype-judgement* (jdecl dtype place)
(cond ((is-sop 'JDECL-EXPR jdecl)
(let* ((*allowed-typed-names*
(appl-judgement-form? (term-arg0 jdecl)))
(ex (xt-expr (term-arg0 jdecl)))
(type (xt-convert-expr-to-type-expr ex)))
(assert (place type))
(make-instance 'subtype-judgement
:declared-subtype type
:declared-type dtype
:chain? t
:place place)))
(t
(assert (is-sop 'JDECL-TYPE jdecl))
(let ((type (xt-type-expr (term-arg0 jdecl))))
(make-instance 'subtype-judgement
:declared-subtype type
:declared-type dtype
:chain? t
:place place)))))
(defun xt-convert-expr-to-type-expr (ex)
(let ((etype (xt-convert-expr-to-type-expr* ex)))
(unless (place etype)
(setf (place etype) (place ex)))
etype))
(defmethod xt-convert-expr-to-type-expr* :around ((ex expr))
(if (plusp (parens ex))
(make-instance 'expr-as-type :expr ex)
(call-next-method)))
(defmethod xt-convert-expr-to-type-expr* ((ex name-expr))
(change-class ex 'type-name))
(defmethod xt-convert-expr-to-type-expr* ((ex set-expr))
(change-class ex 'subtype
:predicate (copy ex)))
(defmethod xt-convert-expr-to-type-expr* ((ex application))
(if (name-expr? (operator ex))
(let* ((tn (change-class (operator ex) 'type-name))
(args (arguments ex)))
(make-instance 'type-application
:type tn
:parameters args
:place (place ex)))
(parse-error (place ex) "Type application must be of the form T(a, b, ...)")))
(defmethod xt-convert-expr-to-type-expr* ((ex expr))
(parse-error ex "Type expected here"))
(defun xt-const-judgement (jdecls dtype place)
(let ((decls (mapcar #'(lambda (jdecl)
(xt-const-judgement* jdecl dtype place))
jdecls)))
(setf (chain? (car (last decls))) nil)
decls))
(defun xt-const-judgement* (jdecl dtype place)
(case (sim-term-op jdecl)
(JDECL-EXPR
(let* ((*allowed-typed-names*
(appl-judgement-form? (term-arg0 jdecl)))
(ex (xt-expr (term-arg0 jdecl))))
(typecase ex
(number-expr
(make-instance 'number-judgement
:number-expr ex
:declared-type dtype
:chain? t
:place place))
(bind-decl
(make-instance 'expr-judgement
:declared-type dtype
:chain? t
:expr ex
:place place))
(name-expr
(make-instance 'name-judgement
:name ex
:declared-type dtype
:chain? t
:place place))
(application
(if (and *allowed-typed-names*
;; Have an application, possibly curried, check if every
;; argument is a name-expr that is not a binding
(some #'(lambda (args)
(some #'bind-decl? args))
(arguments* ex)))
;; Found a typed-name need to convert other name-exprs to
;; untyped-bind-decls
(let* ((invalid-arg (find-if #'(lambda (args) (find-if-not #'name? args))
(arguments* ex)))
(formals (unless invalid-arg
(create-formals-from-arguments (arguments* ex)))))
(if invalid-arg
(parse-error invalid-arg "Must use bindings for application judgements")
(make-instance 'application-judgement
:name (operator* ex)
:formals formals
:declared-type dtype
:chain? t
:place place)))
(make-instance 'expr-judgement
:declared-type dtype
:chain? t
:expr ex
:place place)))
(t (make-instance 'expr-judgement
:declared-type dtype
:chain? t
:expr ex
:place place)))))))
;; Arguments come from parsing decl as an expr. Now want to convert to
;; pdformals+
;; pdformals ::= '(' adformals ')'
;; adformals ::= adformal++','
;; adformal ::= typed-id | '(' typed-ids ')'
;; typed-id ::= idop [':' type-expr] ['|' expr]
;; typed-ids ::= idops [':' type-expr] ['|' expr]
;; Problems:
;; - allowing "idop | expr" causes problems, so must include the type
;; -
(defun create-formals-from-arguments (args-list &optional formals)
;; args-list should be a list of lists
(if (null args-list)
(nreverse formals)
(let ((args (create-formals-from-arguments* (car args-list))))
(assert (consp args))
(create-formals-from-arguments (cdr args-list) (cons args formals)))))
(defmethod create-formals-from-arguments* ((args cons) &optional formals)
(let ((fml (create-formal-from-argument (car args))))
(assert (bind-decl? fml))
(create-formals-from-arguments* (cdr args) (cons fml formals))))
(defmethod create-formals-from-arguments* ((args null) &optional formals)
(let ((fmls (nreverse formals)))
;; chain? is t by default - reset to nil if there is no following arg
;; or it is in parens
(xt-set-declared-types fmls)
fmls))
(defmethod create-formals-from-arguments* ((args tuple-expr) &optional formals)
;; Not sure this is possible - but break just in case
(declare (ignore formals))
(break "create-formals-from-arguments* (tuple-expr)"))
(defmethod create-formals-from-arguments* ((args t) &optional formals)
(declare (ignore formals))
(parse-error args "Illegal formal parameter"))
(defmethod create-formal-from-argument ((arg name-expr))
(change-class arg 'untyped-bind-decl :chain? t))
(defmethod create-formal-from-argument ((arg bind-decl))
arg)
(defmethod create-formal-from-argument ((arg t))
(break "Bad arg in create-formal-from-argument"))
(defun xt-jexprdecl (jdecl)
(when (and (> (length (term-args (term-arg0 jdecl))) 1)
(xt-lambda-formals-check (term-arg0 jdecl)))
(parse-error (term-arg0 jdecl) "commas not allowed"))
(let ((bindings (xt-lambda-formals (term-arg0 jdecl) nil)))
(make-instance 'expr-judgement
:formals (xt-flatten-bindings (car bindings) 0)
:expr (xt-expr (term-arg1 jdecl)))))
(defun xt-rec-judgement (jdecls dtype place)
(let ((decls (mapcar #'(lambda (jdecl)
(xt-rec-judgement* jdecl dtype place))
jdecls)))
(setf (chain? (car (last decls))) nil)
decls))
(defun xt-rec-judgement* (jdecl dtype place)
(if (eq (sim-term-op jdecl) 'JDECL-EXPR)
(let ((*allowed-typed-names*
(appl-judgement-form? (term-arg0 jdecl))))
(unless *allowed-typed-names*
(parse-error jdecl "Recursive judgements are only for applications"))
(let ((ex (xt-expr (term-arg0 jdecl))))
(cond ((not (application? ex))
(parse-error jdecl "Recursive judgements are only for applications"))
((not (name-expr? (operator* ex)))
(parse-error jdecl "Recursive judgements are only for named applications"))
(t
(let* ((invalid-arg (find-if #'(lambda (args) (find-if-not #'name? args))
(arguments* ex)))
(formals (unless invalid-arg
(create-formals-from-arguments (arguments* ex)))))
(if invalid-arg
(parse-error invalid-arg
"Must use variables or bindings for recursive judgements")
(make-instance 'rec-application-judgement
:name (operator* ex)
:formals formals
:declared-type dtype
:chain? t
:place place)))))))
(parse-error jdecl "Recursive judgements are not for types")))
;;; Conversions
(defun xt-conversion-elt (decl)
(let ((cdecls (xt-conversion (term-arg0 decl))))
(dolist (cdecl cdecls)
(setf (semi cdecl) (when (is-sop 'SEMIC (term-arg1 decl)) t)))
cdecls))
(defun xt-conversion (decl)
(xt-conversion* (term-args decl) (sim-term-op decl)))
(defun xt-conversion* (exprs convkey &optional result)
(cond ((null exprs)
(setf (chain? (car result)) nil)
(nreverse result))
(t (let ((cdecl (xt-conversion** (car exprs)
(null (cdr exprs))
convkey)))
(xt-conversion* (cdr exprs) convkey
(cons cdecl result))))))
(defun xt-conversion** (texpr last? convkey)
(let ((expr (xt-expr texpr)))
(case convkey
(CONVERSIONPLUS
(make-instance 'conversionplus-decl
:id (when (name-expr? expr) (id expr))
:expr expr
:chain? (not last?)
:place (term-place texpr)))
(CONVERSIONMINUS
(make-instance 'conversionminus-decl
:id (when (name-expr? expr) (id expr))
:expr expr
:chain? (not last?)
:place (term-place texpr)))
(t