-
Notifications
You must be signed in to change notification settings - Fork 0
/
ld.lisp
5354 lines (4727 loc) · 229 KB
/
ld.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
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
; ACL2 Version 8.6 -- A Computational Logic for Applicative Common Lisp
; Copyright (C) 2024, Regents of the University of Texas
; This version of ACL2 is a descendent of ACL2 Version 1.9, Copyright
; (C) 1997 Computational Logic, Inc. See the documentation topic NOTE-2-0.
; This program is free software; you can redistribute it and/or modify
; it under the terms of the LICENSE file distributed with ACL2.
; 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
; LICENSE for more details.
; Written by: Matt Kaufmann and J Strother Moore
; email: [email protected] and [email protected]
; Department of Computer Science
; University of Texas at Austin
; Austin, TX 78712 U.S.A.
(in-package "ACL2")
; This file, ld.lisp, provides the definition of the ACL2 macro ld,
; which implements both the ACL2 read-eval-print loop and the ACL2
; file loader.
(defrec ld-prompt-memo
; There is no need to memoize the binding of #\r for the purpose of checking if
; the prompt is current, since it never changes during a given session. Of
; course, #\r is bound in the alist.
((current-package ld-level . ld-skip-proofsp)
mode
not-gc-off
#+:non-standard-analysis
script-mode
.
alist)
t)
(defun default-print-prompt (channel state)
; This is the default function for printing the ACL2 ld loop prompt. A typical
; prompt looks like: ACL2 !>, where the number of >'s indicates the ld-level.
; The prompt is printed by (fmt "~@0~sr ~@1~*2" a channel state nil), where a
; is an alist computed from current-package, ld-level, default-defun-mode,
; guard-checking-on, and ld-skip-proofsp, and #\r is bound to "" except for the
; #+:non-standard-analysis version, where it is bound to "(r)". To keep from
; consing up this alist every time, we memoize it, storing in 'prompt-memo the
; tuple (pkg level skipp defun-mode+ gc-on a), where defun-mode+ is the
; default-defun-mode except in raw-mode, where defun-mode+ is nil. Thus, if
; the current settings are as in the memo, we use the a in the memo.
; Otherwise, we compute and store a new memo.
; Warning: If you change the default prompt format, be sure to change it
; in eval-event-lst, where we print it by hand.
(let ((prompt-memo (f-get-global 'prompt-memo state)))
(cond
((and prompt-memo
(equal (access ld-prompt-memo prompt-memo :current-package)
(f-get-global 'current-package state))
(equal (access ld-prompt-memo prompt-memo :ld-level)
(f-get-global 'ld-level state))
(eq (access ld-prompt-memo prompt-memo :ld-skip-proofsp)
(f-get-global 'ld-skip-proofsp state))
(eq (access ld-prompt-memo prompt-memo :mode)
(and (not (raw-mode-p state))
(default-defun-mode (w state))))
; In the following, we could use iff instead of eq, because the dependence of
; defun-mode-prompt on (f-get-global 'guard-checking-on state) is restricted to
; whether or not the latter is nil/:none. But it's cheap to update the
; prompt-memo so we keep the more restrictive eq test for robustness, in case
; the code for defun-mode-prompt changes.
(eq (access ld-prompt-memo prompt-memo :not-gc-off)
(f-get-global 'guard-checking-on state))
#+:non-standard-analysis
(eq (access ld-prompt-memo prompt-memo :script-mode)
(f-get-global 'script-mode state)))
(fmt1 "~@0~sr ~@1~*2"
(access ld-prompt-memo prompt-memo :alist)
0 channel state nil))
(t
(let ((alist
(list (cons #\0 (f-get-global 'current-package state))
(cons #\1 (defun-mode-prompt-string state))
(cons #\2 (list "" ">" ">" ">"
(make-list-ac (f-get-global 'ld-level state)
nil nil)))
(cons #\r
#+:non-standard-analysis
(if (f-get-global 'script-mode state)
""
"(r)")
#-:non-standard-analysis ""))))
(pprogn
(f-put-global
'prompt-memo
(make ld-prompt-memo
:current-package (f-get-global 'current-package state)
:ld-level (f-get-global 'ld-level state)
:ld-skip-proofsp (f-get-global 'ld-skip-proofsp state)
:mode (and (not (raw-mode-p state))
(default-defun-mode (w state)))
:not-gc-off (not (gc-off state))
#+:non-standard-analysis
:script-mode
#+:non-standard-analysis
(f-get-global 'script-mode state)
:alist alist)
state)
(fmt1 "~@0~sr ~@1~*2" alist 0 channel state nil)))))))
(defun print-prompt (prompt output-channel state)
(with-output-forced
output-channel
(col state)
(let ((prompt-fn (cond ((null prompt) nil)
((eq prompt t)
(f-get-global 'prompt-function state))
(t prompt))))
(cond
((null prompt-fn) (mv 0 state))
((eq prompt-fn 'default-print-prompt)
(default-print-prompt output-channel state))
(t (mv-let (erp trans-ans state)
; We could call trans-eval-no-warning here instead, to avoid horrible warnings
; appearing as the prompt is printed. But if that printing modifies a user
; stobj, then probably it would be most appropriate for the superior call of ld
; to specify :ld-user-stobjs-modified-warning nil.
(trans-eval-default-warning (list prompt-fn
(list 'quote output-channel)
'state)
'print-prompt state t)
; If erp is non-nil, trans-ans is of the form (stobjs-out . valx). We
; strongly expect that stobjs-out is (nil state). (That is true if
; prompt is in fact ld-prompt.) That being the case, we expect
; valx to be (col replaced-state).
(cond
((or erp
(not (and (equal (car trans-ans) '(nil state))
(integerp (car (cdr trans-ans))))))
(fmt1 "~%~%Bad Prompt~%See :DOC ld-prompt>"
nil 0 output-channel state nil))
(t (mv (car (cdr trans-ans)) state)))))))))
(defun initialize-timers (state)
(pprogn
(set-timer 'prove-time '(0) state)
(set-timer 'print-time '(0) state)
(set-timer 'proof-tree-time '(0) state)
(set-timer 'other-time '(0) state)))
(defun world-extended-p (old-wrld wrld)
; This function returns nil if wrld = old-wrld; :trivial if wrld extends
; old-wrld only with essentially no-op tuples; and t otherwise. Note that it
; would be extremely rare to be inside certify-book here, since we are doing
; this in support of adding a command-landmark.
; We considered asserting that old-wrld starts with (command-landmark
; global-value . _), at least if we're not in the boot-strap. But we have seen
; that fail when make-event calls ld, as in
; books/kestrel/utilities/verify-guards-program-tests.lisp.
(cond ((endp wrld)
(er hard 'world-extended-p1
"Implementation error: Unexpected empty world!"))
((eq old-wrld wrld)
; The EQ call just above is of course a guard violation. We probably could use
; equal without much loss of efficiency.
:trivial)
(t
; See the Essay on Fast-cert for discussion of this case.
(let ((trip2 (car wrld)))
(case-match trip2
(('ACL2-SYSTEM-TABLE 'TABLE-ALIST ('EMPTY-EVENT-KEY . &))
; Added by maybe-add-event-landmark.
(world-extended-p old-wrld (cdr wrld)))
(('EVENT-LANDMARK 'GLOBAL-VALUE . &)
(world-extended-p old-wrld (cdr wrld)))
(('EVENT-INDEX 'GLOBAL-VALUE . &)
(world-extended-p old-wrld (cdr wrld)))
(('TOP-LEVEL-CLTL-COMMAND-STACK 'GLOBAL-VALUE . &)
(world-extended-p old-wrld (cdr wrld)))
(& t))))))
(defun maybe-add-command-landmark (old-wrld old-default-defun-mode form
trans-ans state)
; Old-wrld is the world before the trans-evaluation of form. That
; trans-evaluation returned trans-ans, which is thus of the form (stobjs-out
; . valx). If stobjs-out suggests that valx contains a state (so, it must in
; fact contain the ACL2 state), and the current world of that state does not
; end with a command landmark and is essentially different from old-wrld, we
; add a command landmark for form.
; We pass in old-default-defun-mode as the default-defun-mode of old-wrld.
; This way, we can compute that value at a time that old-wrld is still
; installed, so that the corresponding getprop will be fast.
(let* ((wrld (w state))
(ext (and (member-eq 'state (car trans-ans))
(not (and (eq (caar wrld) 'command-landmark)
(eq (cadar wrld) 'global-value)))
(world-extended-p old-wrld wrld))))
(cond ((eq ext t)
(er-progn
(get-and-chk-last-make-event-expansion
; For purposes of tracking make-event, we allow time$ only at the top level.
; If there is user demand, we could consider allowing it in arbitrary positions
; of embedded event forms, though in that case we should be careful to check
; that nested calls work well. Note that we look for time$, not for
; return-last, because we are looking at a user-supplied form, not its
; macroexpansion.
(cond ((consp form)
(case (car form)
(time$ (cadr form))
(otherwise form)))
(t form))
wrld 'top-level state
(primitive-event-macros))
(pprogn
(cond ((raw-mode-p state)
; If we are in raw mode, then it is scary to imagine that we have changed the
; logical world.
(warning$ 'top-level "Raw"
"The ACL2 world is being modified while in raw ~
mode. See :DOC set-raw-mode. Further ~
computation in this ACL2 session may have some ~
surprising results."))
(t state))
(set-w 'extension
(add-command-landmark
old-default-defun-mode
form
(f-get-global 'connected-book-directory state)
(f-get-global 'last-make-event-expansion state)
wrld)
state)
(value nil))))
((eq ext :trivial)
; See the Essay on Fast-cert; but here is a summary of the current situation.
; In this case, the world was extended only to support the possibility that we
; are constructing the certification world for the later use of fast-cert mode,
; by extending the top-level-cltl-command-stack when encountering redundant
; events within a progn or the second pass of an encapsulate. But there were
; no actual events added to the world, so there's no such information that is
; appropriate to record.
(pprogn (set-w! old-wrld state)
(value nil)))
(t (value nil)))))
(defun replace-last-cdr (x val)
(cond ((atom x) val)
((atom (cdr x)) (cons (car x) val))
(t (cons (car x) (replace-last-cdr (cdr x) val)))))
(defun ld-standard-oi-missing (val file-name ld-missing-input-ok ctx state)
(cond ((eq ld-missing-input-ok t)
(value nil))
(t (let ((msg (msg "~@0 It is likely that the file you requested, ~
~x1, does not exist."
(msg *ld-special-error*
'standard-oi val)
file-name)))
(cond (ld-missing-input-ok ; not t, so :warn
(pprogn (warning$ ctx "ld-missing-input" "~@0" msg)
(value nil)))
(t (er soft ctx "~@0" msg)))))))
(defun chk-acceptable-ld-fn1-pair (pair ld-missing-input-ok ctx state
co-string co-channel)
; Warning: Keep this in sync with f-get-ld-specials, f-put-ld-specials,
; *initial-ld-special-bindings*, ld-alist-raw, wormhole, and ld.
; We check that pair, which is of the form (var . val) where var is a symbolp,
; specifies a legitimate "binding" for the LD special var. This means that we
; check that var is one of the state globals that LD appears to bind (i.e.,
; push and pop in an unwind-protected way) and that val is a reasonable value
; of that global. For example, 'standard-oi is an LD special but must be bound
; to a true-list of objects or an open object input channel.
; Co-string and co-channel are here to provide a very subtle feature of LD. If
; the same string is specified for both standard-co and proofs-co then we open
; one channel and use it in both places. Our caller, chk-acceptable-ld-fn1, is
; responsible for maintaining these two accumulators as we map down the list of
; pairs. It puts into co-string and co-channel the string and returned channel
; for the first of standard-co or proofs-co encountered.
(let* ((var (car pair))
(val (cdr pair))
(file-name (and (member-eq var
'(standard-oi standard-co proofs-co))
(stringp val) ; else not file-name is not used
(extend-pathname
(f-get-global 'connected-book-directory state)
val
state))))
; The first three LD specials, namely the three channels, are special because
; we may have to open a channel and create a new pair. Once we get past those
; three, we can just use the standard checkers and return the existing pair.
(case var
(standard-oi
(cond
((and (symbolp val)
(open-input-channel-p val :object state))
(value pair))
((true-listp val)
(value pair))
((stringp val)
(mv-let (ch state)
(open-input-channel file-name :object state)
(cond (ch (value (cons 'standard-oi ch)))
(t (ld-standard-oi-missing
val file-name ld-missing-input-ok ctx
state)))))
((consp val)
(let ((last-cons (last val)))
(cond
((and (symbolp (cdr last-cons))
(open-input-channel-p (cdr last-cons) :object state))
(value pair))
((stringp (cdr last-cons))
(let ((file-name (extend-pathname
(f-get-global 'connected-book-directory
state)
(cdr last-cons)
state)))
(mv-let (ch state)
(open-input-channel file-name :object state)
(cond
(ch (value (cons 'standard-oi
(replace-last-cdr val ch))))
(t (ld-standard-oi-missing
val file-name ld-missing-input-ok ctx
state))))))
(t (er soft ctx *ld-special-error* 'standard-oi val)))))
(t (er soft ctx *ld-special-error* 'standard-oi val))))
(standard-co
(cond
((and (symbolp val)
(open-output-channel-p val :character state))
(value pair))
((equal val co-string)
(value (cons 'standard-co co-channel)))
((stringp val)
(mv-let (ch state)
(open-output-channel file-name :character state)
(cond (ch (value (cons 'standard-co ch)))
(t (er soft ctx *ld-special-error* 'standard-co
val)))))
(t (er soft ctx *ld-special-error* 'standard-co val))))
(proofs-co
(cond
((and (symbolp val)
(open-output-channel-p val :character state))
(value pair))
((stringp val)
(cond
((equal file-name co-string)
(value (cons 'proofs-co co-channel)))
(t
(mv-let (ch state)
(open-output-channel file-name :character state)
(cond
(ch (value (cons 'proofs-co ch)))
(t (er soft ctx *ld-special-error* 'proofs-co val)))))))
(t (er soft ctx *ld-special-error* 'proofs-co val))))
(current-package
(er-progn (chk-current-package val ctx state)
(value pair)))
(useless-runes (value pair)) ; updated in chk-acceptable-ld-fn1
(ld-skip-proofsp
(er-progn (chk-ld-skip-proofsp val ctx state)
(value pair)))
(ld-redefinition-action
(er-progn (chk-ld-redefinition-action val ctx state)
(value pair)))
(ld-prompt
(er-progn (chk-ld-prompt val ctx state)
(value pair)))
(ld-missing-input-ok
(er-progn (chk-ld-missing-input-ok val ctx state)
(value pair)))
(ld-always-skip-top-level-locals
(er-progn (chk-ld-always-skip-top-level-locals val ctx state)
(value pair)))
(ld-pre-eval-filter
(er-progn (chk-ld-pre-eval-filter val ctx state)
(value pair)))
(ld-pre-eval-print
(er-progn (chk-ld-pre-eval-print val ctx state)
(value pair)))
(ld-post-eval-print
(er-progn (chk-ld-post-eval-print val ctx state)
(value pair)))
(ld-evisc-tuple
(er-progn (chk-evisc-tuple val ctx state)
(value pair)))
(ld-error-triples
(er-progn (chk-ld-error-triples val ctx state)
(value pair)))
(ld-error-action
(er-progn (chk-ld-error-action val ctx state)
(value pair)))
(ld-query-control-alist
(er-progn (chk-ld-query-control-alist val ctx state)
(value pair)))
(ld-verbose
(er-progn (chk-ld-verbose val ctx state)
(value pair)))
(ld-user-stobjs-modified-warning
(er-progn (chk-ld-user-stobjs-modified-warning val ctx state)
(value pair)))
(otherwise
(er soft ctx
"The variable ~x0 is not an authorized LD special and ~
hence cannot be bound by LD."
var)))))
(defun close-channels (channel-closing-alist state)
; It is necessary to close the channels that we open. We must in fact
; record them somewhere in state so that if we abort LD with a hard error or
; user interrupt that throws us into the unwind-protect code of LP, they are
; still closed. To enable such "remote closings" we invent the notion of a
; "channel closing alist" which is an alist that pairs opened channels to
; their "types", where a type is either 'oi (object input) or 'co (character
; output). Given such an alist we close each channel in it, if the channel
; is in fact open.
(cond
((null channel-closing-alist) state)
(t (pprogn
(cond
((eq (cdar channel-closing-alist) 'oi)
(cond
((open-input-channel-p (caar channel-closing-alist) :object state)
(close-input-channel (caar channel-closing-alist) state))
(t state)))
((eq (cdar channel-closing-alist) 'co)
(cond
((open-output-channel-p (caar channel-closing-alist)
:character state)
(close-output-channel (caar channel-closing-alist) state))
(t state)))
(t (let ((temp (er hard 'close-channels
"The channel ~x0 was tagged with an unimplemented ~
channel type, ~x1."
(caar channel-closing-alist)
(cdar channel-closing-alist))))
(declare (ignore temp))
state)))
(close-channels (cdr channel-closing-alist) state)))))
(defun chk-acceptable-ld-fn1 (alist ld-missing-input-ok ctx state co-string
co-channel new-alist channel-closing-alist
standard-oi0)
; We copy alist (reversing it) onto new-alist, checking that each pair in it
; binds an LD special to a legitimate value. We open the requested files as we
; go and replace the file names with the open channels. We also accumulate
; into channel-closing-alist the pairs necessary to close (with close-channels)
; the channels we have opened. We return a pair consisting of the new-alist
; and the final channel-closing-alist. See chk-acceptable-ld-fn1-pair for an
; explanation of co-string and co-channel.
; Note that standard-oi0 reflects the :dir argument supplied to ld, if any.
; Implementation Note: This odd structure has the single redeeming feature that
; if any given pair of alist causes an error, we have in our hands enough
; information to close any channels we might have opened thus far. If we get
; all the way down alist without causing an error, the channel-closing-alist
; will be used in the acl2-unwind-protect cleanup form and enable us to "close
; on pop" -- which was its original purpose. But an earlier coding of this
; function suffered from the problem that we could open several channels and
; then, right here, cause an error (e.g., the proposed 'current-package setting
; is bad). If that happened, those open channels would never be closed. It is
; still possible to "lose" an opened channel: abort this function after some
; files have been opened.
; This flaw cannot be fixed, at least with the current set of primitives. To
; close a channel we must have the channel. We don't have the channel until
; after we have opened it, i.e., the way we get our hands on a channel in ACL2
; is to open a file, but the way we close a channel is to call
; close-output-channel on the channel object (rather than the file). Thus,
; there is no way we can unwind protect code that opens a channel so as to
; guarantee to close the channel because we can't get the object we are to
; "cleanup" (the channel) until after we have "modified" (opened) it. So there
; is a window of vulnerability between the time we open the channel and the
; time we stash it away in some location known to our cleanup form. During
; that window an abort can cause us to lose a channel in the sense that we do
; not close it. Now we can make that window much smaller than it is now. As
; things stand now we are vulnerable to aborts from the time we start
; processing alist here until we finish and enter the acl2-unwind-protect in
; ld-fn that "binds" the ld specials. But all this vulnerability means is that
; lisp fails to close some opened channels during an abort. If such a thing
; happens, the user could detect it with some poking around. For example, he
; could just type
; (open-output-channel-p 'ACL2-OUTPUT-CHANNEL::STANDARD-CHARACTER-OUTPUT-i
; :character state)
; for a bunch of i starting at 0 and see if there are some he doesn't know
; about. This is not a catastrophic error. It is as though the abort placed
; in the open-output-channels field of the state an additional channel or two.
; The only way, as far as we can see, that this can be a problem is in the
; sense of resource exhaustion: operating systems (and thus lisps) generally
; allow a finite number of open channels.
; If we someday endeavor to plug this hole some additional care must be taken
; because the act of opening an ACL2 channel (in raw lisp) is non-atomic -- we
; have to open the stream, generate a channel symbol, and store some stuff on
; the property list of the symbol. So an abort there can cause an
; irretrievable loss of an open channel unless the problem is addressed down
; there as well.
; Finally we would just like to note that soft errors are handled perfectly
; here in the sense that if some channels are opened and then we get a soft
; error, we close the channels. And aborts are handled perfectly once we get
; outside of the window of vulnerability discussed.
(cond
((null alist)
(er-let* ((u-r-pair (value (assoc-eq 'useless-runes new-alist)))
(useless-runes
(let* ((useless-runes-r/w (cdr u-r-pair))
(useless-runes-r/w-p (consp u-r-pair)))
(initial-useless-runes standard-oi0
useless-runes-r/w useless-runes-r/w-p
t 'ld state))))
(let ((new-alist
; We always put a useless-runes pair into the result, even if useless-runes is
; nil, since we do not want to inherit useless-runes (via the state global's
; value) from a superior call of certify-book (which for example can call
; wormhole, and hence ld, during a proof) or ld.
(put-assoc-eq 'useless-runes useless-runes new-alist)))
(value (cons new-alist channel-closing-alist)))))
(t (mv-let
(erp pair state)
(chk-acceptable-ld-fn1-pair (car alist) ld-missing-input-ok ctx state
co-string co-channel)
(cond
(erp (pprogn
(close-channels channel-closing-alist state)
(mv t nil state)))
((null pair)
(assert$ (eq (caar alist) 'standard-oi)
(pprogn
(close-channels channel-closing-alist state)
(mv nil nil state))))
(t
(chk-acceptable-ld-fn1
(cdr alist) ld-missing-input-ok ctx state
(cond ((and (null co-string)
(or (eq (car pair) 'standard-co)
(eq (car pair) 'proofs-co))
(stringp (cdr (car alist))))
(extend-pathname
(f-get-global 'connected-book-directory state)
(cdr (car alist))
state))
(t co-string))
(cond ((and (null co-channel)
(or (eq (car pair) 'standard-co)
(eq (car pair) 'proofs-co))
(stringp (cdr (car alist))))
(cdr pair))
(t co-channel))
(cons pair new-alist)
(cond
((eq (car pair) 'standard-oi)
(cond ((stringp (cdr (car alist)))
(cons (cons (cdr pair) 'oi) channel-closing-alist))
((and (consp (cdr (car alist)))
(stringp (cdr (last (cdr (car alist))))))
(cons (cons (cdr (last (cdr pair))) 'oi)
channel-closing-alist))
(t channel-closing-alist)))
((and (or (eq (car pair) 'standard-co)
(eq (car pair) 'proofs-co))
(stringp (cdr (car alist))))
(cons (cons (cdr pair) 'co) channel-closing-alist))
(t channel-closing-alist))
standard-oi0)))))))
(defun chk-acceptable-ld-fn (alist standard-oi0 state)
; Alist is an alist that pairs LD specials with proposed values. We check
; that those values are legitimate and that only authorized LD specials are
; bound. If strings are supplied for the specials standard-oi, standard-co,
; and proofs-co, we open corresponding channels and put those channels in
; for the values in the alist. We return a pair consisting of the modified
; alist and a channel closing alist that pairs opened channels with the
; type information it takes to close them.
; Note that standard-oi0 reflects the :dir argument supplied to ld, if any.
(let ((ctx 'ld))
(er-progn
(cond
((or (null (f-boundp-global 'current-acl2-world state))
(null (w state)))
(er soft ctx
"The theorem prover's database has not yet been initialized. To ~
initialize ACL2 to its full theory, which currently takes about 3 ~
minutes on a Sparc 2 (Dec. 1992), invoke (initialize-acl2) from ~
Common Lisp."))
(t (value nil)))
(cond ((symbol-alistp alist) (value nil))
(t (er soft ctx
"The argument to ld-fn must be a symbol-alistp and ~x0 is ~
not."
alist)))
(cond ((assoc-eq 'standard-oi alist) (value nil))
(t (er soft ctx
"The alist argument to ld-fn must specify a value ~
for 'standard-oi and ~x0 does not."
alist)))
(cond ((not (duplicate-keysp-eq alist)) (value nil))
(t (er soft ctx
"The alist argument to ld-fn must contain no duplications ~
among the LD specials to be bound. Your alist contains ~
duplicate values for ~&0."
(duplicates (strip-cars alist)))))
(chk-acceptable-ld-fn1 alist
(cdr (assoc-eq 'ld-missing-input-ok alist))
ctx state nil nil nil nil standard-oi0))))
(defun f-put-ld-specials (alist uurp state)
; Warning: Keep this in sync with f-get-ld-specials,
; chk-acceptable-ld-fn1-pair, *initial-ld-special-bindings*, ld-alist-raw,
; wormhole, and ld.
; Alist is an alist that pairs state globals, most of them LD specials (but
; current-package and useless-runes are exceptions), with their new values. We
; f-put-global each special. Because f-put-global requires an explicitly
; quoted variable, we case split on the authorized LD-specials (and
; exceptions). This is easier and safer than making translate give us special
; treatment. To add a new LD-special you must change this function, as well as
; f-get-ld-specials and the checker chk-acceptable-ld-fn1-pair.
; Uurp stands for "update useless-runes property". It should be nil when
; f-put-ld-specials is called early in ld, before processing forms, and it
; should be t when cleaning up in ld after processing forms. It was added to
; handle useless-runes properly in the case that ld is called inside
; certify-book or another call of ld. It's unfortunate to give such arcane
; handling to one specific state global here, but there's a bit of precedent in
; that current-package is also not an LD special. Unlike current package,
; however, we want to do some cleaning up when updating useless-runes (in
; particular when exiting, to restore the old value).
; Warning: Somebody else better have checked that the values assigned are
; legitimate. For example, we here set 'current-package to whatever we are
; told to set it. This is not a function the user should call!
(cond
((null alist)
(cond (uurp (update-useless-runes nil state))
(t state)))
((eq (caar alist) 'useless-runes)
(pprogn (cond (uurp
(update-useless-runes (cdar alist) state))
(t (f-put-global 'useless-runes (cdar alist) state)))
(f-put-ld-specials (cdr alist) nil state)))
(t (pprogn
(case
(caar alist)
(standard-oi
(f-put-global 'standard-oi (cdar alist) state))
(standard-co
(f-put-global 'standard-co (cdar alist) state))
(proofs-co
(f-put-global 'proofs-co (cdar alist) state))
(current-package
(f-put-global 'current-package (cdar alist) state))
(ld-skip-proofsp
(f-put-global 'ld-skip-proofsp (cdar alist) state))
(ld-redefinition-action
(f-put-global 'ld-redefinition-action (cdar alist) state))
(ld-prompt
(f-put-global 'ld-prompt (cdar alist) state))
(ld-missing-input-ok
(f-put-global 'ld-missing-input-ok (cdar alist) state))
(ld-always-skip-top-level-locals
(f-put-global 'ld-always-skip-top-level-locals (cdar alist) state))
(ld-pre-eval-filter
(if (and (f-boundp-global 'ld-pre-eval-filter state); for boot-strap
(eq (f-get-global 'ld-pre-eval-filter state) :illegal-state))
state ; See the Essay on Illegal-states.
(f-put-global 'ld-pre-eval-filter (cdar alist) state)))
(ld-pre-eval-print
(f-put-global 'ld-pre-eval-print (cdar alist) state))
(ld-post-eval-print
(f-put-global 'ld-post-eval-print (cdar alist) state))
(ld-evisc-tuple
(f-put-global 'ld-evisc-tuple (cdar alist) state))
(ld-error-triples
(f-put-global 'ld-error-triples (cdar alist) state))
(ld-error-action
(f-put-global 'ld-error-action (cdar alist) state))
(ld-query-control-alist
(f-put-global 'ld-query-control-alist (cdar alist) state))
(ld-verbose
(f-put-global 'ld-verbose (cdar alist) state))
(ld-user-stobjs-modified-warning
(if (eq (cdar alist) :same)
state
(f-put-global 'ld-user-stobjs-modified-warning (cdar alist) state)))
(otherwise
(let ((x (er hard 'f-put-ld-specials
"Someone is using ~x0 as an unauthorized LD-special."
(caar alist))))
(declare (ignore x))
state)))
(f-put-ld-specials (cdr alist) uurp state)))))
(defun f-get-ld-specials (state)
; Warning: Keep this in sync with chk-acceptable-ld-fn1-pair,
; f-put-ld-specials, *initial-ld-special-bindings*, ld-alist-raw, wormhole, and
; ld.
; Make an alist, suitable for giving to f-put-ld-specials, that records the
; current values of all LD-specials (and selected other state globals). To add
; a new LD-special you must change this function, f-put-ld-specials, and the
; checker chk-acceptable-ld-fn1-pair.
(list (cons 'standard-oi
(f-get-global 'standard-oi state))
(cons 'standard-co
(f-get-global 'standard-co state))
(cons 'proofs-co
(f-get-global 'proofs-co state))
(cons 'current-package
(f-get-global 'current-package state))
(cons 'useless-runes
(f-get-global 'useless-runes state))
(cons 'ld-skip-proofsp
(f-get-global 'ld-skip-proofsp state))
(cons 'ld-redefinition-action
(f-get-global 'ld-redefinition-action state))
(cons 'ld-prompt
(f-get-global 'ld-prompt state))
(cons 'ld-missing-input-ok
(f-get-global 'ld-missing-input-ok state))
(cons 'ld-always-skip-top-level-locals
(f-get-global 'ld-always-skip-top-level-locals state))
(cons 'ld-pre-eval-filter
(f-get-global 'ld-pre-eval-filter state))
(cons 'ld-pre-eval-print
(f-get-global 'ld-pre-eval-print state))
(cons 'ld-post-eval-print
(f-get-global 'ld-post-eval-print state))
(cons 'ld-evisc-tuple
(f-get-global 'ld-evisc-tuple state))
(cons 'ld-error-triples
(f-get-global 'ld-error-triples state))
(cons 'ld-error-action
(f-get-global 'ld-error-action state))
(cons 'ld-query-control-alist
(f-get-global 'ld-query-control-alist state))
(cons 'ld-verbose
(f-get-global 'ld-verbose state))
(cons 'ld-user-stobjs-modified-warning
(f-get-global 'ld-user-stobjs-modified-warning state))))
(defun ld-read-keyword-command1 (n state)
(cond
((= n 0) (value nil))
(t (mv-let (eofp obj state)
(read-standard-oi state)
(cond
(eofp (er soft 'ld-read-keyword-command
"Unfinished keyword command at eof on (standard-oi ~
state)."))
(t
(er-let*
((rst (ld-read-keyword-command1 (1- n) state)))
; Note: We take advantage of the fact that this function ALWAYS returns a list
; of quoted objects. See the call of strip-cadrs in ld-read-keyword-command
; below. So if you optimize away some of the quotes, beware!
(value (cons (list 'quote obj) rst)))))))))
(defun exit-ld (state)
; This is the function most commonly aliased to the keyword command :q. Its
; evaluation causes LD to terminate immediately. Any function that returns
; three results, the first of which is nil, the second of which is :q and the
; third of which is STATE will do the same.
(value :q))
(defun ld-read-keyword-command (key state)
; ld supports the convention that when a keyword :key is typed
; as a command and the corresponding symbol in the "ACL2" package,
; ACL2::key is a function or macro of arity n, we read n more
; objects, quote them, and apply the ACL2 function or macro.
; Thus,
; MY-PKG !>:ubt foo
; is the same thing as
; MY-PKG !>(ACL2::UBT 'foo)
; We require that the macro not have any lambda keyword arguments, since
; that makes it hard or impossible to determine how many things we should
; read.
; We also support the convention that if :key is bound on 'ld-keyword-aliases
; in state, say in the entry (:key n fn), we manufacture (fn 'x1 ... 'xn)
; instead of requiring that key be a function and returning (key 'x1 ... 'xn).
; This function returns four results, (mv erp keyp form state). If erp is t an
; error was caused and the message has been printed. Otherwise, keyp is
; non-nil or nil according to whether the keyword hack was involved. Form is
; the parsed form of the command read, e.g., (acl2::ubt 'foo). If non-nil,
; keyp is the actual list of objects read, e.g., (:ubt foo).
(let ((temp (assoc-eq key (ld-keyword-aliases state))))
(cond
(temp
(mv-let (erp args state)
(ld-read-keyword-command1 (cadr temp) state)
(cond
(erp (mv t nil nil state))
(t (mv nil
(cons key (strip-cadrs args))
(cons (caddr temp) args)
state)))))
((eq key :q)
; Here is the only place we recognize :q as a special command. Essentially :q
; is an alias for (exit-ld state) except it is overridden by any other aliases
; for :q.
(mv nil '(:q) '(exit-ld state) state))
(t
(let* ((sym (intern (symbol-name key) "ACL2"))
(wrld (w state))
(len (cond ((function-symbolp sym wrld)
(length (formals sym wrld)))
((getpropc sym 'macro-body nil wrld)
(macro-minimal-arity
sym
`(:error "See LD-READ-KEYWORD-COMMAND.")
wrld))
(t nil))))
(cond (len (mv-let (erp args state)
(ld-read-keyword-command1 len state)
(cond (erp (mv t nil nil state))
(t (mv nil
(cons key (strip-cadrs args))
(cons sym args)
state)))))
(t (mv-let (erp val state)
(er soft 'LD
"Unrecognized keyword command ~x0."
key)
(declare (ignore erp val))
(mv t nil nil state)))))))))
(defun ld-fix-command (form)
#-acl2-loop-only
(when (and (consp form)
(eq (car form) 'defconst) ; optimization
(f-get-global 'boot-strap-flg *the-live-state*))
(case-match form
(('defconst name ('quote val) . &)
(assert (boundp name))
(let ((old-val (symbol-value name)))
; Note that we are in the boot-strap, where we presumably don't use
; redefinition. If we later do so, we should see this assertion fire and then
; we can figure out what to do.
(assert (equal val old-val))
(when (not (eq val old-val))
(let ((caddr-form (caddr form))) ; (quote val)
(setf (cadr caddr-form)
old-val)))))))
form)
(defun ld-read-command (state)
; This function reads an ld command from the standard-oi channel of state and
; returns it. It implements the keyword command hack. We return five results:
; (mv eofp erp keyp form state). Eofp means we exhausted standard-oi. Erp,
; when t, indicates that an error occurred, e.g., an ill-formed keyword command
; was read. The error message has been printed. Keyp, when non-nil, indicates
; that form is the parsed form of a keyword command. The list of objects
; actually read is the non-nil value of keyp and that list, without the
; enclosing parentheses, should be printed instead of form. Thus, if :kons is
; an alias for cons, then :kons x y will parse into (cons 'x 'y) and keyp will
; be (:kons x y).
(pprogn
; We need the following call, or the corresponding one in waterfall-step, or
; both, for successful certification of community book
; books/demos/brr-test-book.
(brr-evisc-tuple-oracle-update state)
(prog2$ (and (int= (f-get-global 'ld-level state) 1)
(semi-initialize-brr-wormhole state))
(mv-let (eofp val state)
(read-standard-oi state)
(pprogn
(cond ((int= (f-get-global 'ld-level state) 1)
(let ((last-index (iprint-last-index state)))
(cond ((> last-index (iprint-soft-bound state))
(rollover-iprint-ar nil last-index state))
(t state))))
(t state))
(cond (eofp (mv t nil nil nil state))
((keywordp val)
(mv-let (erp keyp form state)
(ld-read-keyword-command val state)
(mv nil erp keyp form state)))
((stringp val)
(let ((upval (string-upcase val)))
(cond ((find-non-hidden-package-entry
upval
(global-val 'known-package-alist (w state)))
(mv nil nil nil `(in-package ,upval) state))
(t (mv nil nil nil val state)))))
(t (mv nil nil nil (ld-fix-command val) state))))))))
(defun ld-print-command (keyp form col state)
(with-base-10
(mv-let (col state)
(cond
((not (eq (ld-pre-eval-print state) t)) (mv col state))
(keyp
(fmt1 "~*0~|"
(list (cons #\0 (list "" "~x*" "~x* " "~x* " keyp)))
col
(standard-co state)
state
(ld-evisc-tuple state)))
(t
(fmt1 "~q0~|"
(list (cons #\0 form))
col
(standard-co state)
state
(ld-evisc-tuple state))))
(declare (ignore col))
state)))
(defmacro continue-from-illegal-state ()
; See the Essay on Illegal-states.
'(er-progn (set-ld-pre-eval-filter :all state)
(value :continuing)))
(defun ld-filter-command (form state)
(let ((filter (ld-pre-eval-filter state)))
(cond ((eq filter :all) (value t))
((eq filter :query)
(acl2-query :filter
'("~#0~[~Y12?~/Eval?~]"
:y t :n nil :r :return :q :error
:? ("We are in the LD read-eval-print loop, ~
processing the forms in standard-oi. The ~
form printed above is one of those forms. Do ~
you want to evaluate it (Y) or not (N)? You ~
may also answer R, meaning ``return ~
immediately from LD (without reading or ~
evaluating any more forms)'' or Q meaning ~
``return immediately from LD, signaling an ~