-
Notifications
You must be signed in to change notification settings - Fork 3
/
portable-proclaim.lisp
1981 lines (1736 loc) · 85.8 KB
/
portable-proclaim.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
;;; -*- SYNTAX: COMMON-LISP; MODE: LISP; BASE: 10; PACKAGE: *SIM-I; MUSER: YES -*-
(in-package :*sim-i)
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;;>
;;;> The Thinking Machines *Lisp Simulator is in the public domain.
;;;> You are free to do whatever you like with it, including but
;;;> not limited to distributing, modifying, and copying.
;;;> Bugs, comments and revisions due to porting can be sent to:
;;;> [email protected]. Other than to Thinking Machines'
;;;> customers, no promise of support is intended or implied.
;;;>
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;; Author: JP Massar.
#|
This defines the Starlisp type system.
*PROCLAIM.
This is a user level macro. It is used to
tell Starlisp (mostly the Starlisp compiler) various things,
like type information.
Evaluating a *PROCLAIM will have the side effect of
changing various property lists of symbols and internal
lists that Starlisp keeps around.
*DECLAIM
Like *PROCLAIM, but takes a set of proclamations which
do not have to be quoted.
PROCLAIM, DECLAIM
These shadow their Common Lisp counterparts, enabling *Lisp
to record information (mostly type information) provided by
these constructs. Each ends up calling its Lisp counterpart
eventually.
UNPROCLAIM
This is a user level function. it is used to undo the
effects of a *PROCLAIM, *DECLAIM, DECLAIM or a PROCLAIM.
STANDARD-TYPE-P
For Starlisp's internal use. Whether a type name is a
standard Common Lisp type or not.
CANONICAL-TYPE
For Starlisp's internal use. Returns a symbol or form
which is the canonical representation for the type
specified by the input.
PROCLAIMED-TYPE
Returns the type a symbol has been proclaimed to have.
PROCLAIMED-FTYPE
Returns the function type a symbol has been proclaimed to have.
PROCLAIMED-SPECIAL-P
Returns whether a symbol have been proclaimed special.
PVAR
This is redefined as a Common Lisp deftype.
The Common Lisp function typep then works
using this deftype definition.
A canonical type is one of the following:
(pvar boolean)
(pvar (unsigned-byte <length>)), where length is *, or an expression.
(pvar (signed-byte <length>)), where length is *, or an expression.
(pvar (defined-float <mantissa> <exponent>)), where mantissa and exponent are *, or an expression.
(pvar (complex (defined-float <mantissa> <exponent>))), where mantissa and exponent are *, or an expression.
(pvar (array <element-type> <dimensions>)), where element-type is * or a canonical pvar element-type,
and where dimensions are *, or a list of *, or expressions.
(pvar (structure <name>)), where name is *, or a symbol that has been defined by *defstruct.
(pvar string-char)
(pvar character)
(pvar front-end)
(pvar t)
(pvar *)
CANONICAL-PVAR-TYPE
For Starlisp's internal use. Returns a symbol or form
which is the canonical representation of the pvar type
specified by the input, or NIL if the input is not
a legal canonical pvar type.
VALID-PVAR-TYPE-P
For Starlisp's internal use. Same as CANONICAL-PVAR-TYPE
except that it takes an extra optional argument, error,
which defaults to T. If error is T, then if the type
passed in is not a legal pvar type, the routine will
error out and print out a message as to why the type
was not legal.
LENGTH-PVAR-TYPE
For Starlisp's internal use. Returns the length in bits
of a type or * if the type specifies an indeterminate
length.
CANONICAL-PVAR-ELEMENT-TYPE
This will return one of boolean, unsigned-byte, signed-byte, defined-float,
complex, string-char, character, array, structure, t, front-end, or *.
MAKE-CANONICAL-PVAR-TYPE
This takes a pvar element type as defined in CANONICAL-TYPE-ELEMENT-TYPE
and other parameters and returns a canonical pvar type.
MAKE-*DEFUN-FUNCTION
Given a symbol foo, this returns another symbol which is the
name of an internal function which would be defined if
*DEFUN were used to define foo.
ARRAY-PVAR-TYPE-P
STRUCTURE-PVAR-TYPE-P
FLOAT-PVAR-TYPE-P
COMPLEX-PVAR-TYPE-P
BOOLEAN-PVAR-TYPE-P
FRONT-END-PVAR-TYPE-P
GENERAL-PVAR-TYPE-P
STRING-CHAR-PVAR-TYPE-P
CHARACTER-PVAR-TYPE-P
SIGNED-PVAR-TYPE-P
UNSIGNED-PVAR-TYPE-P
These all decide if a pvar type is of the
appropriate class. They only work on
canonical pvar types.
FLOAT-PVAR-TYPE-MANTISSA
FLOAT-PVAR-TYPE-EXPONENT
COMPLEX-PVAR-TYPE-MANTISSA
COMPLEX-PVAR-TYPE-EXPONENT
ARRAY-PVAR-TYPE-DIMENSIONS
ARRAY-PVAR-TYPE-ELEMENT-TYPE
STRUCTURE-PVAR-TYPE-NAME
These extract parameters from a canonical
pvar type of the proper type.
|#
(declaim (special *speed* *safety* *space* *compilation-speed* *verbose*))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defconstant short-float-mantissa 15)
(defconstant short-float-exponent 8)
(defconstant single-float-mantissa 23)
(defconstant single-float-exponent 8)
(defconstant double-float-mantissa 52)
(defconstant double-float-exponent 11)
(defconstant long-float-mantissa 74)
(defconstant long-float-exponent 21)
(defconstant extended-float-mantissa 96)
(defconstant extended-float-exponent 31)
)
(defmacro *proclaim (decl-spec)
`(eval-when (:compile-toplevel :load-toplevel :execute)
(starlisp-proclaim ,decl-spec)))
(defun proclaim (proclamation)
(starlisp-proclaim proclamation)
)
(defmacro *declaim (&rest decl-specs)
`(eval-when (:compile-toplevel :load-toplevel :execute)
,@(mapcar #'(lamda (ds) `(starlisp-proclaim ',ds)) decl-specs)
))
;(defmacro declaim (&rest decl-specs) `(*declaim ,@decl-specs))
(defmacro declaim (&rest decl-specs) ())
(defun proclaimed-type (symbol &optional (default-type 't))
(get symbol :*lisp-type default-type))
(defun proclaimed-ftype (symbol &optional lexical-only-p)
(or (cdr (assoc symbol (list-of-slc-function-types)))
(and (not lexical-only-p) (get-function-type symbol))))
(defun proclaimed-declaration-p (symbol)
(get symbol :*lisp-declaration))
(defun proclaimed-special-p (symbol)
(get symbol :*lisp-special))
;;;
;;; This is so that we can capture deftype information for starlisp-proclaim to do type expansion.
;;;
(defmacro deftype (name lambda-list &rest body)
`(progn
(setf (get ',name :*lisp-deftype)
#'(lambda (&rest form)
(destructuring-bind ,lambda-list form ,@body)))
(cl:deftype ,name ,lambda-list ,@body)))
(defun expand-one-deftype (type)
(if (atom type) (setq type (list type)))
(let ((expander (get (first type) :*lisp-deftype)))
(if (null expander)
nil
(apply expander (rest type))
)))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun parse-body-macroexpand-1 (form env)
(let ((*compiling* nil) (*compilep* nil))
(declare (special *compiling* *compilep*))
(macroexpand-1 form env)
)))
(defun check-ftype-proclamation (ftype-proclamation)
(let ((*print-level* 10))
(flet
((ftype-error (string)
(format
t
"~%~%The FTYPE proclamation, ~S, is not of the form ~@
(ftype (function (&rest <argument types>) <result type>) &rest function-names).~@
You might have your parentheses misplaced."
ftype-proclamation
)
(error "~A" string)
))
(cond
((= (length ftype-proclamation) 1) (ftype-error "There is nothing after the FTYPE."))
((= (length ftype-proclamation) 2) (ftype-error "No function-names were provided."))
((not (listp (second ftype-proclamation))) (ftype-error "The FUNCTION specification is not a list"))
((not (every #'symbolp (cddr ftype-proclamation))) (ftype-error "The FUNCTION-NAMES are not all symbols"))
)
(let ((function-spec (second ftype-proclamation)))
(cond
((not (eq 'function (first function-spec))) (ftype-error "The FUNCTION specification does not begin with FUNCTION"))
((< (length function-spec) 3) (ftype-error "The FUNCTION specification does not specify a result type"))
((> (length function-spec) 3) (ftype-error "The FUNCTION specification specifies more than one result type"))
((not (listp (second function-spec))) (ftype-error "The FUNCTION specification argument list is not a list"))
)))
nil
))
(defun starlisp-proclaim (decl-spec)
(case (car decl-spec)
(special
(dolist (variable (cdr decl-spec))
(setf (get variable :*lisp-special) t)))
(ftype
;; Syntax like, (ftype (function (...) type) &rest foos)
(let ((ftype (cadr decl-spec)))
(check-ftype-proclamation decl-spec)
(let* ((argument-types
(mapcar #'(lambda (type)
(if (member type lambda-list-keywords) type (canonical-type type nil t)))
(cadr ftype)))
(rtype (caddr ftype))
(return-type (if (and (consp rtype) (member (car rtype) '(satisfies and or not member values)))
rtype
(canonical-type rtype nil t))))
(dolist (function (cddr decl-spec))
(set-function-type function `(function ,argument-types ,return-type)))
(setq decl-spec
`(ftype (function ,(mapcar #'(lambda (type) (if (and (consp type) (eq (car type) 'pvar)) 'pvar type))
argument-types)
,(if (and (consp return-type) (eq (car return-type) 'pvar)) 'pvar return-type))
,@(cddr decl-spec))))))
(function
(if (and (cddr decl-spec) (listp (caddr decl-spec)))
(let ((ftype `(function ,(caddr decl-spec) ,@(cdddr decl-spec))))
(set-function-type (cadr decl-spec) ftype))
(dolist (variable (cdr decl-spec))
(setf (get variable :*lisp-type) 'function))))
(inline)
(notinline)
((optimize *optimize)
(dolist (quality-value (cdr decl-spec))
(let ((quality (if (consp quality-value) (car quality-value) quality-value))
(value (if (consp quality-value) (cadr quality-value) 3)))
(case quality
(speed (setq *speed* value))
(safety (setq *safety* value))
(space (setq *space* value))
(compilation-speed (setq *compilation-speed* value))
(otherwise
(cerror "Ignore declaration." "Invalid quality ~S for optimize proclamation." quality))))))
(declaration
(dolist (declaration (cdr decl-spec))
(setf (get declaration :*lisp-declaration) t)))
(ignore
(dolist (variable (cdr decl-spec))
(setf (get variable ':*lisp-ignore) t)))
(ignorable
(dolist (variable (cdr decl-spec))
(setf (get variable ':*lisp-ignorable) t)))
(type
(let ((type (canonical-type (cadr decl-spec) nil t)))
(if (and (null type) (cadr decl-spec))
(cerror "Ignore declaration." "~S is not a valid type in proclaim decl-spec ~S."
(cadr decl-spec) decl-spec)
(dolist (variable (cddr decl-spec))
;; Put the type in an easy to find place.
(setf (get variable :*lisp-type) type)
;; If type is a pvar type, then put a slc-pvar descriptor on the descriptor property.
;; If the type is not a pvar type, then add a vanilla descriptor to the descriptor property.
(if (and (consp type) (eq (car type) 'pvar))
;; Type is a pvar type.
(progn
(set-variable-descriptor
variable (make-pvar-for-compiler :type type :reference variable :read-only t))
(funcall 'cl:proclaim `(type pvar ,variable)))
;; Type is not a pvar, add vanilla descriptor.
(progn
(set-variable-descriptor
variable (make-descriptor-for-compiler :type type :reference variable))
(funcall 'cl:proclaim `(type ,type ,variable))))))))
(*defun
(dolist (function (cdr decl-spec))
(funcall 'proclaim-*defun-1 function)))
(*option
(let* ((keyword (cadr decl-spec)) (value (caddr decl-spec))
(option (find-compiler-keyword keyword)))
(if option
(deal-with-compiler-option-value option value keyword)
(cerror "Ignore declaration." "Unknown starlisp compiler option ~S." keyword))))
(otherwise
(cond ((not (symbolp (car decl-spec)))
(warn "Invalid decl-spec ~S to proclaim, retrying with (TYPE ~{~S~^ ~})." decl-spec decl-spec)
(return-from starlisp-proclaim (proclaim (cons 'type decl-spec))))
((standard-type-p (car decl-spec))
(let ((type (car (canonical-type decl-spec))))
(dolist (variable (cdr decl-spec))
;; If variable was previously proclaimed to be a pvar, then get rid of pvar declaration.
(setf (get variable :*lisp-type) type)
(set-variable-descriptor variable (make-descriptor-for-compiler :type type :reference variable)))))
((proclaimed-declaration-p (car decl-spec))
nil
)
(t
(warn "Invalid decl-spec ~S to proclaim, retrying with (TYPE ~{~S~^ ~})." decl-spec decl-spec)
(return-from starlisp-proclaim (proclaim (cons 'type decl-spec)))
))))
(unless (or (eq (car decl-spec) 'type) (eq (car decl-spec) '*defun))
(funcall 'cl:proclaim decl-spec))
nil
)
(defun unproclaim (decl-spec)
(starlisp-unproclaim decl-spec))
;; this should call lucid unproclaim afterwards, same as *proclaim.
(defun starlisp-unproclaim (decl-spec)
(case (car decl-spec)
(special
(dolist (variable (cdr decl-spec))
(remprop variable 'special)))
(ftype
(dolist (function (cddr decl-spec))
(remove-function-type function)))
(function
(if (and (cddr decl-spec) (listp (caddr decl-spec)))
(remprop (cadr decl-spec) 'function-type)
(dolist (variable (cdr decl-spec))
(remprop variable 'type))))
((inline notinline))
((optimize *optimize)
(dolist (quality-value (cdr decl-spec))
(let ((quality (if (consp quality-value) (car quality-value) quality-value)))
(case quality
(speed (setq *speed* 1))
(safety (setq *safety* 1))
(space (setq *space* 1))
(compilation-speed (setq *compilation-speed* 1))
(otherwise
(error "Invalid quality ~S for optimize proclamation.~
Only SPEED, SAFETY, COMPILATION-SPACE, and SPACE are allowed." quality))))))
(declaration
(dolist (declaration (cdr decl-spec))
(remprop declaration 'declaration)))
(ignore
(dolist (variable (cdr decl-spec))
(remprop variable 'ignore)))
(type
(dolist (variable (cddr decl-spec))
(remprop variable 'type)
(remove-variable-descriptor variable)))
(*defun
(dolist (function (cdr decl-spec)) nil)) ;(unproclaim-*defun-1 function)
(otherwise
(cond ((not (symbolp (car decl-spec)))
(warn "Invalid decl-spec ~S to unproclaim, retrying with (TYPE ~{~S~^ ~})." decl-spec decl-spec)
(return-from starlisp-unproclaim (unproclaim (cons 'type decl-spec))))
((standard-type-p (car decl-spec))
(dolist (variable (cdr decl-spec))
(remprop variable 'type)
(remove-variable-descriptor variable)))
((proclaimed-declaration-p (car decl-spec))
nil
)
(t
(warn "Invalid decl-spec ~S to unproclaim, retrying with (TYPE ~{~S~^ ~})." decl-spec decl-spec)
(return-from starlisp-unproclaim (unproclaim (cons 'type decl-spec)))))))
nil)
(defun standard-type-p (type)
(and (member type
'(array atom bignum bit bit-vector character common compiled-function
complex cons double-float fixnum float
function hash-table integer keyword list
long-float nil null number package pathname random-state ratio
rational readtable sequence short-float simple-array
simple-bit-vector simple-string simple-vector
single-float standard-char stream string string-char
symbol t vector mod signed-byte unsigned-byte))
t))
(defun canonical-front-end-type (type)
(case (if (consp type) (car type) type)
((boolean standard-char string-char character t null number nil)
type)
(integer
(if (consp type)
`(integer ,(or (cadr type) '*) ,(or (caddr type) '*))
'(integer * *)))
(mod
(let ((limit (if (consp type) (or (cadr type) '*) '*)))
`(integer 0 ,(if (eq limit '*) '* (list limit)))))
(unsigned-byte
(let ((limit (if (consp type) (or (cadr type) '*) '*)))
`(integer 0 ,(if (eq limit '*) '* (list (simplify-expression (expt2-symbol) limit))))))
(signed-byte
(let* ((limit (if (consp type) (or (cadr type) '*) '*))
(real-limit (if (eq limit '*) '* (simplify-expression (expt2-1-symbol) limit))))
`(integer ,(if (integerp real-limit)
(- real-limit)
(list (simplify-expression '- -1 real-limit)))
(,real-limit))))
(bit '(integer 0 1))
(fixnum '(integer #.most-negative-fixnum #.most-positive-fixnum))
(bignum '(or (integer * (#.most-negative-fixnum)) (integer (#.most-positive-fixnum) *)))
((float short-float single-float double-float long-float)
(if (consp type)
`(,(car type) ,(or (cadr type) '*) ,(or (caddr type) '*))
`(,type * *)))
(list
`(or cons null))
(rational
`(or integer ratio))
(sequence
`(or (array * (*)) cons null))
;; string, simple-string, bit-vector, simple-bit-vector
(string
`(string ,@(if (and (consp type) (cdr type)) (cdr type) '(*))))
(simple-string
`(simple-string ,@(if (and (consp type) (cdr type)) (cdr type) '(*))))
(bit-vector
`(bit-vector ,@(if (and (consp type) (cdr type)) (cdr type) '(*))))
(simple-bit-vector
`(simple-bit-vector ,@(if (and (consp type) (cdr type)) (cdr type) '(*))))
(array
`(array
,(if (and (consp type) (cadr type)) (or (cadr (canonical-pvar-type `(pvar ,(cadr type)))) (cadr type) '*) '*)
,@(if (and (consp type) (cddr type)) (cddr type) '(*))))
(simple-array
`(simple-array
,(if (and (consp type) (cadr type)) (or (cadr (canonical-pvar-type `(pvar ,(cadr type)))) (cadr type) '*) '*)
,@(if (and (consp type) (cddr type)) (cddr type) '(*))))
(vector
`(array
,(if (and (consp type) (cadr type)) (or (cadr (canonical-pvar-type `(pvar ,(cadr type)))) (cadr type) '*) '*)
(,@(if (and (consp type) (cddr type)) (cddr type) '(*)))))
(simple-vector
`(array t (,@(if (and (consp type) (cdr type)) (cdr type) '(*)))))
;; Complex.?
(otherwise type)))
;
; A canonical type is one of the following:
; (pvar boolean)
; (pvar (unsigned-byte <length>)), where length is *, or an expression.
; (pvar (signed-byte <length>)), where length is *, or an expression.
; (pvar (defined-float <mantissa> <exponent>)), where mantissa and exponent are *, or an expression.
; (pvar (complex (defined-float <mantissa> <exponent>))), where mantissa and exponent are *, or an expression.
; (pvar (array <element-type> <dimensions>)), where element-type is * or a canonical pvar element-type,
; and where dimensions are *, or a list of *, or expressions.
; (pvar (structure <name>)), where name is *, or a symbol that has been defined by *defstruct.
; (pvar string-char)
; (pvar character)
; (pvar front-end)
; (pvar t)
; (pvar *)
;
(defun canonical-type (type &optional canonical-front-end-type (syntax-check nil))
(cond ((standard-type-p type)
(if canonical-front-end-type (canonical-front-end-type type) type))
((eq type 'pvar)
'(pvar *))
((eq type 'fixnum)
`(integer #.most-negative-fixnum #.most-positive-fixnum))
((or (equal type '(member nil t)) (equal type '(member t nil)) (eq type 'boolean))
'boolean)
((eq type 'front-end)
'front-end)
((eq type 'structure)
(if canonical-front-end-type '(structure *) 'structure))
((eq type 'defined-float) (if canonical-front-end-type '(float * *) 'float))
#+symbolics
((and (symbolp type) (get type 'si:defstruct-description))
(if canonical-front-end-type `(structure ,type) type))
#+lucid
((and (symbolp type) (lucid::structure-type-p type))
(if canonical-front-end-type `(structure ,type) type))
#+cmu
((and (symbolp type) (get type 'lisp::%structure-definition))
(if canonical-front-end-type `(structure ,type) type))
((atom type)
(canonical-type (or (expand-one-deftype type) (return-from canonical-type nil))))
((eq (car type) 'member)
(let ((members (cdr type)))
(cond ((every #'integerp members)
(canonical-type `(integer ,(apply #'min members) ,(apply #'max members)) canonical-front-end-type syntax-check))
((every #'characterp members)
(if (every #'string-char-p members)
'string-char
'character))
(t type))))
((eq (car type) 'defined-float)
`(defined-float ,(or (cadr type) '*) ,(or (caddr type) '*)))
((standard-type-p (car type))
(if canonical-front-end-type (canonical-front-end-type type) type))
((member (car type) '(and or not member satisfies))
type)
((eq (car type) 'structure) ; wonder if I should bother with this?
(if (or (null (cadr type)) (eq (cadr type) '*))
'structure
(if canonical-front-end-type type (cadr type))))
((eq (car type) 'pvar)
(let* ((pvar-type (if syntax-check (canonical-pvar-type (valid-pvar-type-p type t)) (canonical-pvar-type type)))
(atomic-element-type nil)
(element-type (cadr pvar-type)))
(case (setq atomic-element-type (if (consp element-type) (car element-type) element-type))
((unsigned-byte signed-byte)
(let ((length (cadr element-type)))
(if (numberp length)
(if (integerp length)
(if (<= 1 length)
(check-paris-restriction nil length '*maximum-integer-length*
"Certain operations can not handle integer pvars with length")
(error "Integer pvars must have a positive length instead of ~S." length))
(error "Integer pvars must have an integer length instead of ~S." length)))))
((defined-float complex)
(let* ((complexp (eq atomic-element-type 'complex))
(element-type (if complexp (cadr element-type) element-type))
(mantissa (cadr element-type)) (exponent (caddr element-type)))
(if (numberp mantissa)
(if (integerp mantissa)
(if (<= 1 mantissa)
(check-paris-restriction nil mantissa '*maximum-significand-length*
(if complexp
"Certain operations can not handle complex pvars with mantissa"
"Certain operations can not handle float pvars with mantissa"))
(error "~:[Float~;Complex~] pvars must have a positive mantissa length instead of ~S."
complexp mantissa))
(error "~:[Float~;Complex~] pvars must have an integer mantissa length instead of ~S."
complexp mantissa)))
(if (numberp exponent)
(if (integerp exponent)
(if (<= 1 exponent)
(check-paris-restriction nil exponent '*maximum-exponent-length*
(if complexp
"Certain operations can not handle complex pvars with exponent"
"Certain operations can not handle float pvars with exponent"))
(error "~:[Float~;Complex~] pvars must have a positive exponent length instead of ~S."
complexp exponent))
(error "~:[Float~;Complex~] pvars must have an integer exponent length instead of ~S."
complexp exponent)))
(if (and (integerp exponent) (integerp mantissa))
(if (not (and (>= exponent 2) (>= mantissa 1) (>= (expt 2 (1- exponent)) (1+ mantissa))))
(warn "Certain operations can not handle ~:[float~;complex~] pvars with format {~D,~D}."
complexp mantissa exponent))))))
pvar-type))
((member (car type)
`(#+symbolics si:xr-bq-list #+symbolics si:xr-bq-append #+symbolics si:xr-bq-nconc
#+symbolics si:xr-bq-cons #+symbolics si:xr-bq-list*
#+lcl3.0 lrs:bq-list #+lcl3.0 lrs:bq-append #+lcl3.0 lrs:bq-nconc
#+lcl3.0 lrs:bq-cons #+lcl3.0 lrs:bq-list*
cons nth nthcdr first second third fourth fifth sixth seventh eighth ninth tenth
car cdr caar cadr cdar cddr caaar caadr cadar caddr cdaar cdadr cddar cdddr
caaaar caaadr caadar caaddr cadaar cadadr caddar cadddr
cdaaar cdaadr cdadar cdaddr cddaar cddadr cdddar cddddr
last list list* append copy-list copy-alist copy-tree revappend nconc nreconc butlast nbutlast
ldiff rplaca rplacd subst subst-if subst-if-not nsubst nsubst-if nsubst-if-not sublis
nsublis assoc assoc-if assoc-if-not rassoc rassoc-if rassoc-if-not
))
t)
(t (canonical-type (or (expand-one-deftype type)
(return-from canonical-type nil))))))
(defun check-pvar-element-type (object element-type)
(let ((type (if (consp element-type) (car element-type) element-type))
(rest (if (consp element-type) (cdr element-type) ()))
(pvar-type (pvar-type object)))
(flet ((float-pvar-type (pvar rest mantissa exponent &optional (pvar-types '(:float :number-float)))
(and (member (pvar-type pvar) pvar-types)
(or (eql mantissa '*)
(eql (pvar-mantissa-length pvar) mantissa))
(or (eql exponent '*)
(eql (pvar-exponent-length pvar) exponent))
(or (null rest)
(eql (car rest) '*))
(or (null (cdr rest))
(eql (cadr rest) '*))))
(check-integer-range (rest low high)
(and (or (null rest)
(eql (car rest) '*)
(if (consp (car rest))
(eql (1+ (caar rest)) low)
(eql (car rest) low)))
(or (null (cdr rest))
(eql (cadr rest) '*)
(if (consp (cadr rest))
(eql (1- (caadr rest)) high)
(eql (cadr rest) high))))))
(case type
((t)
(eql pvar-type :general))
(boolean
(eql pvar-type :boolean))
(front-end
(eql pvar-type :front-end))
(number
;; (pvar number), number type does not specialize.
(member pvar-type '(:field :signed :number-float :number-signed :float)))
(integer
(case pvar-type
(:field
(check-integer-range rest 0 (1- (expt 2 (pvar-length object)))))
(:signed
(let ((expt (expt 2 (1- (pvar-length object)))))
(check-integer-range rest (- expt) (1- expt))))
;; obsolete.
(:number-signed
t)
(otherwise nil)))
(signed-byte
(and (member pvar-type '(:signed :number-signed))
(or (null rest)
(eql (car rest) '*)
(eql (pvar-length object) (car rest)))))
(unsigned-byte
(and (eql pvar-type :field)
(or (null rest)
(eql (car rest) '*)
(eql (pvar-length object) (car rest)))))
(mod
(and (eql pvar-type :field)
(or (null rest)
(eql (car rest) '*)
(eql (1- (expt 2 (pvar-length object))) (car rest)))))
(float
(float-pvar-type object rest '* '*))
(short-float
(float-pvar-type object rest short-float-mantissa short-float-exponent))
(single-float
(float-pvar-type object rest single-float-mantissa single-float-exponent))
(double-float
(float-pvar-type object rest double-float-mantissa double-float-exponent))
(long-float
(float-pvar-type object rest long-float-mantissa long-float-exponent))
(defined-float
(float-pvar-type object () (if (null rest) '* (car rest)) (if (null (cdr rest)) '* (cadr rest))))
((nil)
nil)
;; Otherwise, I didn't understand the element-type, try again to see if it
;; is a user defined deftype.
(otherwise (check-pvar-element-type object (or (expand-pvar-element-type element-type)
(return-from check-pvar-element-type nil))))))))
(defun pvarp (object &optional (element-type '*))
(if (internal-pvarp object)
(if (eql element-type '*)
t
(check-pvar-element-type object element-type))))
(defun boolean-pvarp (arg)
(and (internal-pvarp arg)
(eql (pvar-type arg) :boolean)))
(defun front-end-pvarp (arg)
(and (internal-pvarp arg)
(eql (pvar-type arg) :front-end)))
(defun signed-pvarp (arg &optional (length '*))
(and (internal-pvarp arg)
(eql (pvar-type arg) :signed)
(or (eql length '*)
(eql (pvar-length arg) length))))
(defun unsigned-pvarp (arg &optional (length '*))
(and (internal-pvarp arg)
(eql (pvar-type arg) :field)
(or (eql length '*)
(eql (pvar-length arg) length))))
(defun float-pvarp (arg &optional (mantissa '*) (exponent '*))
(and (internal-pvarp arg)
(eql (pvar-type arg) :float)
(or (eql mantissa '*)
(eql (pvar-mantissa-length arg) mantissa))
(or (eql exponent '*)
(eql (pvar-exponent-length arg) exponent))))
(defun single-float-pvarp (arg)
(and (internal-pvarp arg)
(eql (pvar-type arg) :float)
(eql (pvar-mantissa-length arg) single-float-mantissa)
(eql (pvar-exponent-length arg) single-float-exponent)))
(defun short-float-pvarp (arg)
(and (internal-pvarp arg)
(eql (pvar-type arg) :float)
(eql (pvar-mantissa-length arg) short-float-mantissa)
(eql (pvar-exponent-length arg) short-float-exponent)))
(defun double-float-pvarp (arg)
(and (internal-pvarp arg)
(eql (pvar-type arg) :float)
(eql (pvar-mantissa-length arg) double-float-mantissa)
(eql (pvar-exponent-length arg) double-float-exponent)))
(defun long-float-pvarp (arg)
(and (internal-pvarp arg)
(eql (pvar-type arg) :float)
(eql (pvar-mantissa-length arg) long-float-mantissa)
(eql (pvar-exponent-length arg) long-float-exponent)))
(defun extended-float-pvarp (arg)
(and (internal-pvarp arg)
(eql (pvar-type arg) :float)
(eql (pvar-mantissa-length arg) extended-float-mantissa)
(eql (pvar-exponent-length arg) extended-float-exponent)))
(defun character-pvarp (arg)
(and (internal-pvarp arg)
(eql (pvar-type arg) :character)))
(defun string-char-pvarp (arg)
(and (internal-pvarp arg)
(eql (pvar-type arg) :string-char)))
(defun general-pvarp (arg)
(and (internal-pvarp arg)
(eql (pvar-type arg) :general)
(not (void-pvar-p arg))))
(defun complex-pvarp (arg &optional (mantissa '*) (exponent '*))
(and (internal-pvarp arg)
(eql (pvar-type arg) :complex)
(or (eql mantissa '*)
(eql (pvar-mantissa-length arg) mantissa))
(or (eql exponent '*)
(eql (pvar-exponent-length arg) exponent))))
(defun single-complex-pvarp (arg)
(and (internal-pvarp arg)
(eql (pvar-type arg) :complex)
(eql (pvar-mantissa-length arg) single-float-mantissa)
(eql (pvar-exponent-length arg) single-float-exponent)))
(defun short-complex-pvarp (arg)
(and (internal-pvarp arg)
(eql (pvar-type arg) :complex)
(eql (pvar-mantissa-length arg) short-float-mantissa)
(eql (pvar-exponent-length arg) short-float-exponent)))
(defun double-complex-pvarp (arg)
(and (internal-pvarp arg)
(eql (pvar-type arg) :complex)
(eql (pvar-mantissa-length arg) double-float-mantissa)
(eql (pvar-exponent-length arg) double-float-exponent)))
(defun long-complex-pvarp (arg)
(and (internal-pvarp arg)
(eql (pvar-type arg) :complex)
(eql (pvar-mantissa-length arg) long-float-mantissa)
(eql (pvar-exponent-length arg) long-float-exponent)))
(defun extended-complex-pvarp (arg)
(and (internal-pvarp arg)
(eql (pvar-type arg) :complex)
(eql (pvar-mantissa-length arg) extended-float-mantissa)
(eql (pvar-exponent-length arg) extended-float-exponent)))
(defun array-pvarp (arg &optional (element-type '*) (dimensions '*))
(and (internal-pvarp arg)
(eql (pvar-type arg) :array)
(or (eql element-type '*)
(equal (cadr (portable-pvar-array-element-type arg)) element-type) )
(or (eql dimensions '*)
(array-pvarp-check-dimensions (portable-pvar-array-dimensions arg) dimensions))))
(defun array-pvarp-check-dimensions (arg-array-dimensions array-dimensions)
(do ((arg-array-dimensions arg-array-dimensions (cdr arg-array-dimensions))
(array-dimensions array-dimensions (cdr array-dimensions)))
((or (null arg-array-dimensions) (null array-dimensions))
(and (null arg-array-dimensions) (null array-dimensions)))
(unless (or (eq (car array-dimensions) '*) (equal (eval (car array-dimensions)) (car arg-array-dimensions)))
(return nil))))
(defun structure-pvarp (arg &optional (name '*))
(and (internal-pvarp arg)
(eql (pvar-type arg) :structure)
(or (eq name '*) (funcall 'included-*defstruct-p (portable-pvar-structure-name arg) name))))
(defparameter unsigned-pvarp-limit 0)
(defparameter unsigned-pvarps nil)
(defparameter signed-pvarp-limit 0)
(defparameter signed-pvarps nil)
(defun unsigned-pvarp-closure (length)
#'(lambda (arg)
(and (internal-pvarp arg)
(eql (pvar-type arg) :field)
(eql (pvar-length arg) length))))
(defun signed-pvarp-closure (length)
#'(lambda (arg)
(and (internal-pvarp arg)
(eql (pvar-type arg) :signed)
(eql (pvar-length arg) length))))
(defun initialize-integer-pvarps (limit)
(setq unsigned-pvarp-limit limit)
(setq signed-pvarp-limit limit)
(setq unsigned-pvarps (make-array limit))
(setq signed-pvarps (make-array limit))
(let ((*print-case* :upcase))
(dotimes (index limit)
(setf (symbol-function
(setf (aref unsigned-pvarps index)
(intern (format nil "UNSIGNED~D-PVARP" index) *starlisp-internal-package-name*)))
(unsigned-pvarp-closure index))
(setf (symbol-function
(setf (aref signed-pvarps index)
(intern (format nil "SIGNED~D-PVARP" index) *starlisp-internal-package-name*)))
(signed-pvarp-closure index)))))
(initialize-integer-pvarps 129)
#+lucid
(defvar save-normalize-type #'lucid::normalize-type)
#+symbolics
(proclaim '(special *dont-use-closure-for-pvar-types*))
#+lucid
(defvar *dont-use-closure-for-pvar-types* nil)
#+lucid
(defun lucid::normalize-type (type)
(let ((*dont-use-closure-for-pvar-types* t))
(funcall save-normalize-type type)))
(defparameter *pvar-type-predicates* ())
(defun add-pvar-type-predicate (pvar-type predicate)
(unless (symbolp predicate)
(error "predicate ~S must be a symbol." predicate))
(setq pvar-type (canonical-pvar-type pvar-type))
(unless (and (consp pvar-type) (eq (car pvar-type) 'pvar))
(error "~S must be a pvar type." pvar-type))
(pushnew (cons (cadr pvar-type) predicate) *pvar-type-predicates* :test #'equal)
(values))
(defun pvar-type-predicate (element-type)
(or (cdr (assoc element-type *pvar-type-predicates* :test #'equal))
(case (if (consp element-type) (car element-type) element-type)
(* 'internal-pvarp)
(signed-byte
(cond ((and (integerp (cadr element-type)) (>= (cadr element-type) 0) (< (cadr element-type) signed-pvarp-limit))
(aref signed-pvarps (cadr element-type)))
((or #+lucid *dont-use-closure-for-pvar-types*
(atom element-type) (eql (cadr element-type) '*))
'signed-pvarp)
(t `(lambda (object) (signed-pvarp object ,(cadr element-type))))))
(unsigned-byte
(cond ((and (integerp (cadr element-type)) (>= (cadr element-type) 0) (< (cadr element-type) unsigned-pvarp-limit))
(aref unsigned-pvarps (cadr element-type)))
((or #+lucid *dont-use-closure-for-pvar-types*
(atom element-type) (null (cdr element-type)) (eql (cadr element-type) '*))
'unsigned-pvarp)
(t `(lambda (object) (unsigned-pvarp object ,(cadr element-type))))))
(boolean
'boolean-pvarp)
(front-end
'front-end-pvarp)
(character
'character-pvarp)
(string-char
'string-char-pvarp)
((t)
'general-pvarp)
(single-float
'single-float-pvarp)
(short-float
'short-float-pvarp)
(double-float
'double-float-pvarp)
(long-float
'long-float-pvarp)
(extended-float
'extended-float-pvarp)
(structure
(cond ((or (null (cadr element-type)) (eq (cadr element-type) '*))
'structure-pvarp)
((get (cadr element-type) '*defstruct-global-predicate-function))
#+lucid
(*dont-use-closure-for-pvar-types*
'structure-pvarp)
(t `(lambda (object) (structure-pvarp object ',(cadr element-type))))))
(array
(if (or #+lucid *dont-use-closure-for-pvar-types*
(and (eq (cadr element-type) '*) (eq (caddr element-type) '*)))
'array-pvarp
`(lambda (object) (array-pvarp object ',(cadr element-type) ',(caddr element-type)))))
(defined-float
(cond ((or (atom element-type)
(and (or (null (cdr element-type)) (eql (cadr element-type) '*))
(or (null (cddr element-type)) (eql (caddr element-type) '*))))
'float-pvarp)
((and (eql (cadr element-type) short-float-mantissa) (eql (caddr element-type) short-float-exponent))
'short-float-pvarp)
((and (eql (cadr element-type) single-float-mantissa) (eql (caddr element-type) single-float-exponent))
'single-float-pvarp)
((and (eql (cadr element-type) double-float-mantissa) (eql (caddr element-type) double-float-exponent))
'double-float-pvarp)
((and (eql (cadr element-type) long-float-mantissa) (eql (caddr element-type) long-float-exponent))
'long-float-pvarp)
((and (eql (cadr element-type) extended-float-mantissa) (eql (caddr element-type) extended-float-exponent))
'extended-float-pvarp)
#+lucid (*dont-use-closure-for-pvar-types* 'float-pvarp)
(t `(lambda (object)
(float-pvarp object
,(if (eq (cadr element-type) '*) ''* (cadr element-type))
,(if (eq (caddr element-type) '*) ''* (caddr element-type)))))))
(complex
(let ((element-type (cadr element-type)))
(case (if (consp element-type) (car element-type) element-type)
(single-float
'single-complex-pvarp)
(short-float
'short-complex-pvarp)
(double-float
'double-complex-pvarp)
(long-float
'long-complex-pvarp)
(extended-float
'extended-complex-pvarp)
(defined-float
(cond ((or (atom element-type)
(and (or (null (cdr element-type)) (eql (cadr element-type) '*))
(or (null (cddr element-type)) (eql (caddr element-type) '*))))
'complex-pvarp)
((and (eql (cadr element-type) short-float-mantissa) (eql (caddr element-type) short-float-exponent))
'short-complex-pvarp)
((and (eql (cadr element-type) single-float-mantissa) (eql (caddr element-type) single-float-exponent))
'single-complex-pvarp)
((and (eql (cadr element-type) double-float-mantissa) (eql (caddr element-type) double-float-exponent))
'double-complex-pvarp)
((and (eql (cadr element-type) long-float-mantissa) (eql (caddr element-type) long-float-exponent))
'long-complex-pvarp)
((and (eql (cadr element-type) extended-float-mantissa) (eql (caddr element-type) extended-float-exponent))