-
Notifications
You must be signed in to change notification settings - Fork 29
/
screamer.lisp
executable file
·7233 lines (6517 loc) · 311 KB
/
screamer.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
;;; -*- Mode: LISP; Package: (SCREAMER :USE CL :COLON-MODE :EXTERNAL); Base: 10; Syntax: Ansi-common-lisp -*-
;;; LaHaShem HaAretz U'Mloah
;;; Screamer
;;; A portable efficient implementation of nondeterministic Common Lisp
;;; Based on original version 3.20 by:
;;;
;;; Jeffrey Mark Siskind (Department of Computer Science, University of Toronto)
;;; David Allen McAllester (MIT Artificial Intelligence Laboratory)
;;;
;;; Copyright 1991 Massachusetts Institute of Technology. All rights reserved.
;;; Copyright 1992, 1993 University of Pennsylvania. All rights reserved.
;;; Copyright 1993 University of Toronto. All rights reserved.
;;;
;;; Permission is hereby granted, free of charge, to any person obtaining a copy of
;;; this software and associated documentation files (the "Software"), to deal in
;;; the Software without restriction, including without limitation the rights to
;;; use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
;;; the Software, and to permit persons to whom the Software is furnished to do so,
;;; subject to the following conditions:
;;;
;;; The above copyright and authorship notice and this permission notice shall be
;;; included in all copies or substantial portions of the Software.
;;;
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
;;; FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
;;; COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
;;; IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
;;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
;;; Important notice: In this version of Screamer, if Screamer is already
;;; loaded and you wish to recompile the entire file, the recompilation will
;;; proceed much faster if you first do:
;;; (CLRHASH SCREAMER::*FUNCTION-RECORD-TABLE*)
(in-package :screamer)
(declaim (declaration magic))
(defmacro define-screamer-package (defined-package-name &body options)
"Convenience wrapper around DEFPACKAGE. Passes its argument directly
to DEFPACKAGE, and automatically injects two additional options:
\(:shadowing-import-from :screamer :defun :multiple-value-bind :y-or-n-p)
\(:use :cl :screamer)"
`(defpackage ,defined-package-name
,@options
(:shadowing-import-from :screamer :defun :multiple-value-bind :y-or-n-p)
(:use :cl :screamer)))
(define-screamer-package :screamer-user)
(defmacro defstruct-compile-time (options &body items)
`(eval-when (:compile-toplevel :load-toplevel :execute)
(defstruct ,options ,@items)))
(defmacro defvar-compile-time (name &optional initial-value documentation)
`(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar ,name ,initial-value ,documentation)))
(defmacro defun-compile-time (function-name lambda-list &body body)
`(eval-when (:compile-toplevel :load-toplevel :execute)
(cl:defun ,function-name ,lambda-list ,@body)
(eval-when (:compile-toplevel) (compile ',function-name))))
;;; Needed because Allegro has some bogosity whereby (MACRO-FUNCTION <m> <e>)
;;; returns NIL during compile time when <m> is a macro being defined for the
;;; first time in the file being compiled.
(defmacro defmacro-compile-time (function-name lambda-list &body body)
`(eval-when (:compile-toplevel :load-toplevel :execute)
(defmacro ,function-name ,lambda-list ,@body)))
(defparameter *screamer-version* (asdf:component-version (asdf:find-system :screamer))
"The version of Screamer which is loaded.")
(defvar-compile-time *dynamic-extent?*
;; SBCL cannot stack-allocate LET-bound lambdas that screamer
;; currently uses, so setting dynamic-extent to T will only
;; generate compiler notes about it inability to do so.
#-sbcl t
#+sbcl nil
"Set to T to enable the dynamic extent optimization, NIL to
disable it. Default is platform dependent.")
(defvar *iscream?* nil
"T if Screamer is running under ILisp/GNUEmacs with iscream.el loaded.")
(defvar *nondeterministic?* nil "This must be globally NIL.")
(defvar-compile-time *screamer?* nil
"This must be NIL except when defining internal Screamer functions.")
(defvar-compile-time *nondeterministic-context?* nil
"This must be globally NIL.")
(defvar-compile-time *local?* nil "This must be globally NIL.")
(defvar-compile-time *block-tags* '() "This must be globally NIL.")
(defvar-compile-time *tagbody-tags* '() "This must be globally NIL.")
(defvar *trail* (make-array 4096 :adjustable t :fill-pointer 0) "The trail.")
(defvar-compile-time *function-record-table* (make-hash-table :test #'equal)
"The function record table.")
(defvar-compile-time *ordered-lambda-list-keywords*
'(&optional &rest &key &allow-other-keys &aux)
"The allowed lambda list keywords in order.")
(defmacro-compile-time choice-point-internal (form)
`(catch '%fail
(let ((*nondeterministic?* t))
(unwind-protect ,form
(unwind-trail-to trail-pointer)))))
(defmacro-compile-time choice-point-external (&rest forms)
;; note: Is it really better to use VECTOR-PUSH-EXTEND than CONS for the
;; trail?
`(let ((trail-pointer (fill-pointer *trail*))) ,@forms))
(defmacro-compile-time choice-point (form)
`(choice-point-external (choice-point-internal ,form)))
(defstruct-compile-time function-record
function-name
(lambda-list nil)
(body nil)
(callees nil)
(deterministic? t)
(old-deterministic? nil)
(screamer? *screamer?*))
(defstruct-compile-time (nondeterministic-function
(:print-function print-nondeterministic-function)
(:predicate nondeterministic-function?-internal))
function)
(defun-compile-time screamer-error (header &rest args)
(apply
#'error
(concatenate
'string
header
"~2%There are eight types of nondeterministic contexts:
1. the body of a function defined with SCREAMER::DEFUN
2. the body of a FOR-EFFECTS macro invocation
3. the body of an ALL-VALUES macro invocation
4. the first argument of a ONE-VALUE macro invocation
5. the body of a PRINT-VALUES macro invocation
6. the second argument of an ITH-VALUE macro invocation
7. the body of a POSSIBLY? macro invocation
8. the body of a NECESSARILY? macro invocation.
Note that the default forms of &OPTIONAL and &KEY arguments and the
initialization forms of &AUX variables are always deterministic
contexts even though they may appear inside a SCREAMER::DEFUN.") args))
(defun-compile-time get-function-record (function-name)
(or (gethash function-name *function-record-table*)
(setf (gethash function-name *function-record-table*)
(make-function-record :function-name function-name))))
(defun-compile-time peal-off-documentation-string-and-declarations
(body &optional documentation-string?)
;; note: This will need to be done as well for LOCALLY and MACROLET when we
;; eventually implement them.
;; needs work: This requires that the documentation string preceed all
;; declarations which needs to be fixed.
(let (documentation-string declarations)
(when (and documentation-string?
(not (null body))
(not (null (rest body)))
(stringp (first body)))
(setf documentation-string (first body))
(setf body (rest body)))
(loop (unless (and (not (null body))
(consp (first body))
(eq (first (first body)) 'declare))
(return))
(push (first body) declarations)
(pop body))
(values body (reverse declarations) documentation-string)))
(defun-compile-time self-evaluating? (thing)
(and (not (consp thing))
(or (not (symbolp thing))
(null thing)
(eq thing t)
(eq (symbol-package thing) (symbol-package :x)))))
(defun-compile-time quotify (thing)
(if (self-evaluating? thing) thing `',thing))
(defun-compile-time lambda-expression? (form)
(and (consp form)
(eq (first form) 'lambda)
(or (and (null (rest (last form)))
(>= (length form) 2)
(listp (second form)))
(error "Invalid syntax for LAMBDA expression: ~S" form))))
(defun-compile-time valid-function-name? (function-name)
(or (and (symbolp function-name) (not (null function-name)))
(and (consp function-name)
(eq (first function-name) 'setf)
(null (rest (last function-name)))
(= (length function-name) 2)
(symbolp (second function-name))
(not (null (second function-name))))))
(defun-compile-time check-function-name (function-name)
(unless (valid-function-name? function-name)
(error "Invalid function name: ~S" function-name)))
(defun-compile-time every-other (list)
(cond ((null list) list)
((null (rest list)) list)
(t (cons (first list) (every-other (rest (rest list)))))))
(defun-compile-time check-lambda-list-internal (lambda-list &optional mode)
(cond
((null lambda-list))
((member (first lambda-list) *ordered-lambda-list-keywords* :test #'eq)
(check-lambda-list-internal (rest lambda-list) (first lambda-list)))
(t (let ((parameter (first lambda-list)))
(ecase mode
((nil)
(unless (symbolp parameter)
(error "Invalid parameter: ~S" parameter)))
(&optional
(unless (or (symbolp parameter)
(and (consp parameter)
(null (rest (last parameter)))
(or (= (length parameter) 1)
(= (length parameter) 2)
(and (= (length parameter) 3)
(symbolp (third parameter))))
(symbolp (first parameter))))
(error "Invalid &OPTIONAL parameter: ~S" parameter)))
(&rest
(unless (symbolp parameter)
(error "Invalid &REST parameter: ~S" parameter)))
(&key
(unless (or (symbolp parameter)
(and (consp parameter)
(null (rest (last parameter)))
(or (= (length parameter) 1)
(= (length parameter) 2)
(and (= (length parameter) 3)
(symbolp (third parameter))))
(or (symbolp (first parameter))
(and (consp (first parameter))
(null (rest (last (first parameter))))
(= (length (first parameter)) 2)
(symbolp (first (first parameter)))
(symbolp (second (first parameter)))))))
(error "Invalid &KEY parameter: ~S" parameter)))
(&aux
(unless (or (symbolp parameter)
(and (consp parameter)
(null (rest (last parameter)))
(or (= (length parameter) 1)
(= (length parameter) 2))
(symbolp (first parameter))))
(error "Invalid &AUX parameter: ~S" parameter)))))
(check-lambda-list-internal (rest lambda-list) mode))))
(defun-compile-time check-lambda-list (lambda-list)
(unless (null (rest (last lambda-list)))
(error "Improper lambda-list: ~S" lambda-list))
(let ((rest (member '&rest lambda-list :test #'eq)))
(if rest
(let ((rest (rest rest)))
(unless (not (member '&rest rest :test #'eq))
(error "&REST cannot appear more than once: ~S" lambda-list))
(unless (and (not (null rest))
(not (member (first rest) lambda-list-keywords :test #'eq))
(or (null (rest rest))
(member (first (rest rest)) lambda-list-keywords
:test #'eq)))
(error "&REST must be followed by exactly one variable: ~S"
lambda-list)))))
(let ((allow-other-keys (member '&allow-other-keys lambda-list :test #'eq)))
(if allow-other-keys
(unless (or (null (rest allow-other-keys))
(member (first (rest allow-other-keys)) lambda-list-keywords
:test #'eq))
(error "&ALLOW-OTHER-KEYS must not be followed by a parameter: ~S"
lambda-list))))
(let ((keywords
(remove-if-not #'(lambda (argument)
(member argument lambda-list-keywords :test #'eq))
lambda-list)))
(unless (every #'(lambda (keyword)
(member keyword *ordered-lambda-list-keywords* :test #'eq))
keywords)
(error "Invalid lambda list keyword: ~S" lambda-list))
(unless (every #'(lambda (x y)
(member y (member x *ordered-lambda-list-keywords*
:test #'eq)
:test #'eq))
keywords
(rest keywords))
(error "Invalid order for lambda list keywords: ~S" lambda-list)))
(check-lambda-list-internal lambda-list))
(defun-compile-time walk-lambda-list-reducing
(map-function reduce-function screamer? partial? nested? lambda-list
environment &optional mode)
(cond
((null lambda-list) (funcall reduce-function))
((member (first lambda-list) *ordered-lambda-list-keywords* :test #'eq)
(walk-lambda-list-reducing map-function
reduce-function
screamer?
partial?
nested?
(rest lambda-list)
environment
(first lambda-list)))
(t (ecase mode
((nil &rest &allow-other-keys &aux)
(walk-lambda-list-reducing map-function
reduce-function
screamer?
partial?
nested?
(rest lambda-list)
environment
mode))
((&optional &key)
(if (and (consp (first lambda-list))
(consp (rest (first lambda-list))))
(funcall
reduce-function
(walk map-function reduce-function screamer? partial? nested?
(second (first lambda-list)) environment)
(walk-lambda-list-reducing map-function
reduce-function
screamer?
partial?
nested?
(rest lambda-list)
environment
mode))
(walk-lambda-list-reducing map-function
reduce-function
screamer?
partial?
nested?
(rest lambda-list)
environment
mode)))))))
(defun-compile-time walk-lambda-list
(map-function reduce-function screamer? partial? nested? lambda-list
environment)
(check-lambda-list lambda-list)
(if reduce-function
(funcall
reduce-function
(funcall map-function lambda-list 'lambda-list)
(walk-lambda-list-reducing map-function
reduce-function
screamer?
partial?
nested?
lambda-list
environment))
(funcall map-function lambda-list 'lambda-list)))
(defun-compile-time walk-block
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form))) (error "Improper BLOCK: ~S" form))
(unless (>= (length form) 2)
(error "BLOCK must have at least one argument, a NAME: ~S" form))
(unless (symbolp (second form)) (error "NAME must be a symbol: ~S" form))
(if reduce-function
(funcall reduce-function
(funcall map-function form 'block)
(reduce reduce-function
(mapcar #'(lambda (subform)
(walk map-function
reduce-function
screamer?
partial?
nested?
subform
environment))
(rest (rest form)))))
(funcall map-function form 'block)))
(defun-compile-time walk-catch
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form))) (error "Improper PROGN: ~S" form))
(unless (>= (length form) 2)
(error "CATCH must have at least one argument, a TAG: ~S" form))
(if reduce-function
(funcall reduce-function
(funcall map-function form 'catch)
(reduce reduce-function
(mapcar #'(lambda (subform)
(walk map-function
reduce-function
screamer?
partial?
nested?
subform
environment))
(rest form))))
(funcall map-function form 'catch)))
(defun-compile-time walk-eval-when
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form))) (error "Improper EVAL-WHEN: ~S" form))
(unless (>= (length form) 2)
(error "EVAL-WHEN must have at least one argument: ~S" form))
(unless (listp (second form))
(error "First argument of EVAL-WHEN must be a list: ~S" form))
(unless (null (rest (last (second form))))
(error "Improper list of SITUATIONS: ~S" form))
(unless (every #'(lambda (situation)
(member situation '(:compile-toplevel
:load-toplevel
:execute
compile
load
evel)
:test #'eq))
(second form))
(error "Invalid SITUATION: ~S" form))
(if (member :execute (second form) :test #'eq)
(walk-progn map-function
reduce-function
screamer?
partial?
nested?
`(progn ,@(rest (rest form)))
environment)
(funcall map-function nil 'quote)))
(defun-compile-time walk-flet/labels
(map-function reduce-function screamer? partial? nested? form environment
form-type)
(unless (null (rest (last form))) (error "Improper ~S: ~S" form-type form))
(unless (>= (length form) 2)
(error "~S must have BINDINGS: ~S" form-type form))
(unless (and (listp (second form))
(null (rest (last (second form))))
(every #'(lambda (binding)
(and (consp binding)
(null (rest (last binding)))
(>= (length binding) 2)
(valid-function-name? (first binding))
(listp (second binding))))
(second form)))
(error "Invalid BINDINGS for ~S: ~S" form-type form))
(if reduce-function
(funcall
reduce-function
(funcall map-function form form-type)
(if nested?
(funcall
reduce-function
(reduce
reduce-function
(mapcar
#'(lambda (binding)
(funcall reduce-function
(walk-lambda-list map-function
reduce-function
screamer?
partial?
nested?
(second binding)
environment)
(mapcar
#'(lambda (subform)
(walk map-function
reduce-function
screamer?
partial?
nested?
subform
environment))
(peal-off-documentation-string-and-declarations
(rest (rest binding)) t))))
(second form)))
(reduce reduce-function
(mapcar #'(lambda (subform)
(walk map-function
reduce-function
screamer?
partial?
nested?
subform
environment))
(rest (rest form)))))
(reduce reduce-function
(mapcar #'(lambda (subform)
(walk map-function
reduce-function
screamer?
partial?
nested?
subform
environment))
(rest (rest form))))))
(funcall map-function form form-type)))
(defun-compile-time walk-function
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form))) (error "Improper FUNCTION: ~S" form))
(unless (= (length form) 2)
(error "FUNCTION must have one argument: ~S" form))
(cond ((lambda-expression? (second form))
(if (and reduce-function nested?)
(funcall
reduce-function
(funcall map-function form 'function-lambda)
(funcall
reduce-function
(walk-lambda-list map-function
reduce-function
screamer?
partial?
nested?
(second (second form))
environment)
(reduce
reduce-function
(mapcar #'(lambda (subform)
(walk map-function
reduce-function
screamer?
partial?
nested?
subform
environment))
(peal-off-documentation-string-and-declarations
(rest (rest (second form))) t)))))
(funcall map-function form 'function-lambda)))
((valid-function-name? (second form))
(cond
((symbolp (second form))
(if (or (special-operator-p (second form))
(macro-function (second form) environment))
(error "You can't reference the FUNCTION of a special form or~%~
macro: ~S"
form)
(funcall map-function form 'function-symbol)))
(t (funcall map-function form 'function-setf))))
(t (error "Invalid argument to FUNCTION: ~S" form))))
(defun-compile-time walk-go (map-function form)
(unless (null (rest (last form))) (error "Improper GO: ~S" form))
(unless (= (length form) 2) (error "GO must have one argument: ~S" form))
(unless (or (symbolp (second form)) (integerp (second form)))
(error "TAG of GO must be a symbol or integer: ~S" form))
(funcall map-function form 'go))
(defun-compile-time walk-if
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form))) (error "Improper IF: ~S" form))
(unless (or (= (length form) 3) (= (length form) 4))
(error "IF must have two or three arguments: ~S" form))
(if reduce-function
(if (= (length form) 4)
(funcall reduce-function
(funcall map-function form 'if)
(funcall reduce-function
(walk map-function
reduce-function
screamer?
partial?
nested?
(second form)
environment)
(funcall reduce-function
(walk map-function
reduce-function
screamer?
partial?
nested?
(third form)
environment)
(walk map-function
reduce-function
screamer?
partial?
nested?
(fourth form)
environment))))
(funcall reduce-function
(funcall map-function form 'if)
(funcall reduce-function
(walk map-function
reduce-function
screamer?
partial?
nested?
(second form)
environment)
(walk map-function
reduce-function
screamer?
partial?
nested?
(third form)
environment))))
(funcall map-function form 'if)))
(defun-compile-time walk-let/let*
(map-function reduce-function screamer? partial? nested? form environment
form-type)
(unless (null (rest (last form))) (error "Improper ~S: ~S" form-type form))
(unless (>= (length form) 2)
(error "~S must have BINDINGS: ~S" form-type form))
(unless (and (listp (second form))
(null (rest (last (second form))))
(every #'(lambda (binding)
(or (symbolp binding)
(and (consp binding)
(null (rest (last binding)))
(or (= (length binding) 1)
(= (length binding) 2))
(symbolp (first binding)))))
(second form)))
(error "Invalid BINDINGS for ~S: ~S" form-type form))
(if reduce-function
(funcall
reduce-function
(funcall map-function form form-type)
(funcall reduce-function
(reduce reduce-function
(mapcar #'(lambda (binding)
(walk map-function
reduce-function
screamer?
partial?
nested?
(second binding)
environment))
(remove-if-not
#'(lambda (binding)
(and (consp binding)
(= (length binding) 2)))
(second form))))
(reduce reduce-function
(mapcar #'(lambda (subform)
(walk map-function
reduce-function
screamer?
partial?
nested?
subform
environment))
(peal-off-documentation-string-and-declarations
(rest (rest form)))))))
(funcall map-function form form-type)))
(defun-compile-time walk-multiple-value-call
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form)))
(error "Improper MULTIPLE-VALUE-CALL: ~S" form))
(unless (>= (length form) 2)
(error "MULTIPLE-VALUE-CALL must have at least one argument, a FUNCTION: ~S"
form))
(if reduce-function
(funcall reduce-function
(funcall map-function form 'multiple-value-call)
(reduce reduce-function
(mapcar #'(lambda (subform)
(walk map-function
reduce-function
screamer?
partial?
nested?
subform
environment))
(rest form))))
(funcall map-function form 'multiple-value-call)))
(defun-compile-time walk-multiple-value-prog1
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form)))
(error "Improper MULTIPLE-VALUE-PROG1: ~S" form))
(unless (>= (length form) 2)
(error "MULTIPLE-VALUE-PROG1 must have at least one argument, a FORM: ~S"
form))
(if reduce-function
(funcall reduce-function
(funcall map-function form 'multiple-value-prog1)
(reduce reduce-function
(mapcar #'(lambda (subform)
(walk map-function
reduce-function
screamer?
partial?
nested?
subform
environment))
(rest form))))
(funcall map-function form 'multiple-value-prog1)))
(defun-compile-time walk-progn
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form))) (error "Improper PROGN: ~S" form))
(if reduce-function
(funcall reduce-function
(funcall map-function form 'progn)
(reduce reduce-function
(mapcar #'(lambda (subform)
(walk map-function
reduce-function
screamer?
partial?
nested?
subform
environment))
(rest form))))
(funcall map-function form 'progn)))
(defun-compile-time walk-progv
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form))) (error "Improper PROGV: ~S" form))
(unless (>= (length form) 3)
(error "PROGV must have at least two arguments: ~S" form))
(if reduce-function
(funcall reduce-function
(funcall map-function form 'progv)
(funcall reduce-function
(funcall reduce-function
(walk map-function
reduce-function
screamer?
partial?
nested?
(second form)
environment)
(walk map-function
reduce-function
screamer?
partial?
nested?
(third form)
environment))
(reduce reduce-function
(mapcar #'(lambda (subform)
(walk map-function
reduce-function
screamer?
partial?
nested?
subform
environment))
(rest (rest (rest form)))))))
(funcall map-function form 'progv)))
(defun-compile-time walk-quote (map-function form)
(unless (null (rest (last form))) (error "Improper QUOTE: ~S" form))
(unless (= (length form) 2)
(error "QUOTE must have one argument: ~S" form))
(funcall map-function (second form) 'quote))
(defun-compile-time walk-return-from
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form))) (error "Improper RETURN-FROM: ~S" form))
(unless (or (= (length form) 2) (= (length form) 3))
(error "RETURN-FROM must have one or two arguments,~%~
a NAME and an optional RESULT: ~S" form))
(unless (symbolp (second form)) (error "NAME must be a symbol: ~S" form))
(if reduce-function
(funcall reduce-function
(funcall map-function form 'return-from)
(walk map-function
reduce-function
screamer?
partial?
nested?
(if (= (length form) 3) (third form) nil)
environment))
(funcall map-function form 'return-from)))
(defun-compile-time walk-setq
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form))) (error "Improper SETQ: ~S" form))
(unless (every #'symbolp (every-other (rest form)))
(error "Invalid destination for SETQ: ~S" form))
(unless (evenp (length (rest form)))
(error "Odd number of arguments to SETQ: ~S" form))
(if reduce-function
(funcall reduce-function
(funcall map-function form 'setq)
(reduce reduce-function
(mapcar #'(lambda (subform)
(walk map-function
reduce-function
screamer?
partial?
nested?
subform
environment))
(every-other (rest (rest form))))))
(funcall map-function form 'setq)))
(defun-compile-time walk-tagbody
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form))) (error "Improper TAGBODY: ~S" form))
(unless (every #'(lambda (subform)
(or (symbolp subform) (integerp subform) (listp subform)))
(rest form))
(error "A subforms of a TAGBODY must be symbols, integers or lists: ~S"
form))
(let ((tags (remove-if #'consp (rest form))))
(unless (= (length tags) (length (remove-duplicates tags)))
(error "TAGBODY has duplicate TAGs: ~S" form)))
(if reduce-function
(funcall reduce-function
(funcall map-function form 'tagbody)
(reduce reduce-function
(mapcar #'(lambda (subform)
(walk map-function
reduce-function
screamer?
partial?
nested?
subform
environment))
(remove-if-not #'consp (rest form)))))
(funcall map-function form 'tagbody)))
(defun-compile-time walk-the
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form))) (error "Improper THE: ~S" form))
(unless (= (length form) 3) (error "THE must have two arguments: ~S" form))
(if reduce-function
(funcall reduce-function
(walk map-function
reduce-function
screamer?
partial?
nested?
(third form)
environment)
(funcall map-function form 'the))
(funcall map-function form 'the)))
(defun-compile-time walk-throw
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form))) (error "Improper THROW: ~S" form))
(unless (= (length form) 3)
(error "THROW must have two arguments, a TAG and a RESULT: ~S" form))
(if reduce-function
(funcall reduce-function
(funcall map-function form 'throw)
(funcall reduce-function
(walk map-function
reduce-function
screamer?
partial?
nested?
(second form)
environment)
(walk map-function
reduce-function
screamer?
partial?
nested?
(third form)
environment)))
(funcall map-function form 'throw)))
(defun-compile-time walk-unwind-protect
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form))) (error "Improper UNWIND-PROTECT: ~S" form))
(unless (>= (length form) 2)
(error "UNWIND-PROTECT must have at least one argument, a PROTECTED-FORM: ~S"
form))
(if reduce-function
(funcall
reduce-function
(funcall map-function form 'unwind-protect)
(funcall reduce-function
(walk map-function
reduce-function
screamer?
partial?
nested?
(second form)
environment)
(reduce reduce-function
(mapcar #'(lambda (subform)
(walk map-function
reduce-function
screamer?
partial?
nested?
subform
environment))
(rest (rest form))))))
(funcall map-function form 'unwind-protect)))
(defun-compile-time walk-for-effects
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form))) (error "Improper FOR-EFFECTS: ~S" form))
;; note: We used to think that we should never walk the body of FOR-EFFECTS
;; as we thought that the walker would get confused on the code
;; generated by FOR-EFFECTS and that FOR-EFFECTS called
;; CPS-CONVERT-PROGN on its body and that CPS-CONVERT-PROGN did the
;; walk for us. But that was wrong since FORM-CALLEES also walks and
;; thus would miss functions called in the body of a FOR-EFFECTS. So now
;; we walk the body of a FOR-EFFECTS without macro-expanding it, but
;; only when NESTED? is true which is essentially only for FORM-CALLEES
;; since DETERMINISTIC? must not walk the body of FOR-EFFECTS or else
;; it will mistakingly report that that a FOR-EFFECTS form is
;; nondeterministic when its body is nondeterministic.
(if (and reduce-function nested?)
(funcall reduce-function
(funcall map-function form 'for-effects)
(reduce reduce-function
(mapcar #'(lambda (subform)
(walk map-function
reduce-function
screamer?
partial?
nested?
subform
environment))
(rest form))))
(funcall map-function form 'for-effects)))
(defun-compile-time walk-setf
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form))) (error "Improper SETF: ~S" form))
(unless (evenp (length (rest form)))
(error "Odd number of arguments to SETF: ~S" form))
(if *local?*
(if reduce-function
(funcall reduce-function
(funcall map-function form 'local-setf)
(reduce reduce-function
(mapcar #'(lambda (subform)
(walk map-function
reduce-function
screamer?
partial?
nested?
subform
environment))
(every-other (rest (rest form))))))
(funcall map-function form 'local-setf))
(walk map-function
reduce-function
screamer?
partial?
nested?
(let ((*macroexpand-hook* #'funcall))
(macroexpand-1 form environment))
environment)))
(defun-compile-time walk-multiple-value-call-nondeterministic
(map-function reduce-function screamer? partial? nested? form environment)
(unless (null (rest (last form)))
(error "Improper MULTIPLE-VALUE-CALL-NONDETERMINISTIC: ~S" form))
(unless (>= (length form) 2)
(error "MULTIPLE-VALUE-CALL-NONDETERMINISTIC must have at least one ~
argument, a FUNCTION: ~S"
form))
(if reduce-function
(funcall reduce-function
(funcall map-function form 'multiple-value-call-nondeterministic)
(reduce reduce-function
(mapcar #'(lambda (subform)
(walk map-function
reduce-function
screamer?
partial?
nested?
subform
environment))
(rest form))))
(funcall map-function form 'multiple-value-call-nondeterministic)))
(defun-compile-time walk-full (map-function form)
(unless (null (rest (last form))) (error "Improper FULL: ~S" form))
(unless (= (length form) 2)
(error "FULL must have exactly one argument, a FORM: ~S" form))
(funcall map-function form 'full))
(defun-compile-time walk-macro-call
(map-function reduce-function screamer? partial? nested? form environment)
(if reduce-function
(funcall reduce-function
(funcall map-function form 'macro-call)
(walk map-function