Changeset 279
- Timestamp:
- Jan 13, 2004, 4:56:51 PM (21 years ago)
- File:
-
- 1 edited
-
trunk/ccl/level-1/l1-typesys.lisp (modified) (61 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ccl/level-1/l1-typesys.lisp
r117 r279 137 137 arglist)) 138 138 139 (eval-when (:compile-toplevel) 140 (warn "Fix EVAL-WHEN in EXPAND-TYPE-MACRO")) 141 139 142 (defun expand-type-macro (definer name arglist body env) 140 143 (setq name (require-type name 'symbol)) 141 144 (multiple-value-bind (lambda doc) 142 145 (parse-macro-internal name arglist body env '*) 143 `(eval-when ( :compile-toplevel:load-toplevel :execute)146 `(eval-when (#|:compile-toplevel|# :load-toplevel :execute) 144 147 (,definer ',name 145 148 (nfunction ,name ,lambda) … … 310 313 (defun vanilla-union (type1 type2) 311 314 (cond ((csubtypep type1 type2) type2) 312 ((csubtypep type2 type1) type1) 313 (t nil))) 314 315 ((csubtypep type2 type1) type1) 316 (t nil))) 317 318 (defun hierarchical-intersection2 (type1 type2) 319 (multiple-value-bind (subtypep1 win1) (csubtypep type1 type2) 320 (multiple-value-bind (subtypep2 win2) (csubtypep type2 type1) 321 (cond (subtypep1 type1) 322 (subtypep2 type2) 323 ((and win1 win2) *empty-type*) 324 (t nil))))) 325 326 (defun hierarchical-union2 (type1 type2) 327 (cond ((csubtypep type1 type2) type2) 328 ((csubtypep type2 type1) type1) 329 (t nil))) 315 330 316 331 ;;; DELEGATE-COMPLEX-{SUBTYPEP-ARG2,INTERSECTION} -- Interface … … 335 350 (if (and method (not (eq method #'delegate-complex-intersection))) 336 351 (funcall method type2 type1) 337 ( vanilla-intersectiontype1 type2))))352 (hierarchical-intersection2 type1 type2)))) 338 353 339 354 ;;; HAS-SUPERCLASSES-COMPLEX-SUBTYPEP-ARG1 -- Internal … … 396 411 ); eval-when (compile eval) 397 412 413 414 (defun reparse-unknown-ctype (type) 415 (if (unknown-ctype-p type) 416 (specifier-type (type-specifier type)) 417 type)) 418 419 (defun swapped-args-fun (f) 420 #'(lambda (x y) 421 (funcall f y x))) 422 423 (defun equal-but-no-car-recursion (x y) 424 (cond ((eql x y) t) 425 ((consp x) 426 (and (consp y) 427 (eql (car x) (car y)) 428 (equal-but-no-car-recursion (cdr x) (cdr y)))) 429 (t nil))) 430 431 (defun any/type (op thing list) 432 (declare (type function op)) 433 (let ((certain? t)) 434 (dolist (i list (values nil certain?)) 435 (multiple-value-bind (sub-value sub-certain?) (funcall op thing i) 436 (if sub-certain? 437 (when sub-value (return (values t t))) 438 (setf certain? nil)))))) 439 440 (defun every/type (op thing list) 441 (declare (type function op)) 442 (let ((certain? t)) 443 (dolist (i list (if certain? (values t t) (values nil nil))) 444 (multiple-value-bind (sub-value sub-certain?) (funcall op thing i) 445 (if sub-certain? 446 (unless sub-value (return (values nil t))) 447 (setf certain? nil)))))) 448 449 (defun invoke-complex-=-other-method (type1 type2) 450 (let* ((type-class (ctype-class-info type1)) 451 (method-fun (type-class-complex-= type-class))) 452 (if method-fun 453 (funcall (the function method-fun) type2 type1) 454 (values nil t)))) 455 456 (defun invoke-complex-subtypep-arg1-method (type1 type2 &optional subtypep win) 457 (let* ((type-class (ctype-class-info type1)) 458 (method-fun (type-class-complex-subtypep-arg1 type-class))) 459 (if method-fun 460 (funcall (the function method-fun) type1 type2) 461 (values subtypep win)))) 462 463 (defun type-might-contain-other-types-p (type) 464 (or (hairy-ctype-p type) 465 (negation-ctype-p type) 466 (union-ctype-p type) 467 (intersection-ctype-p type))) 468 469 398 470 (eval-when (:compile-toplevel :execute) 399 471 … … 502 574 (define-type-method (values :simple-=) (type1 type2) 503 575 (let ((rest1 (args-ctype-rest type1)) 504 (rest2 (args-ctype-rest type2)))576 (rest2 (args-ctype-rest type2))) 505 577 (cond ((or (args-ctype-keyp type1) (args-ctype-keyp type2) 506 (args-ctype-allowp type1) (args-ctype-allowp type2))578 (args-ctype-allowp type1) (args-ctype-allowp type2)) 507 579 (values nil nil)) 508 580 ((and rest1 rest2 (type/= rest1 rest2)) … … 512 584 (t 513 585 (multiple-value-bind (req-val req-win) 514 (type=-list (values-ctype-required type1)515 (values-ctype-required type2))586 (type=-list (values-ctype-required type1) 587 (values-ctype-required type2)) 516 588 (multiple-value-bind (opt-val opt-win) 517 (type=-list (values-ctype-optional type1)518 (values-ctype-optional type2))589 (type=-list (values-ctype-optional type1) 590 (values-ctype-optional type2)) 519 591 (values (and req-val opt-val) (and req-win opt-win)))))))) 520 592 … … 566 638 ;;; of each other. 567 639 ;;; 640 568 641 (define-type-method (function :simple-subtypep) (type1 type2) 569 (declare (ignore type1 type2)) 570 (values t t)) 642 (flet ((fun-type-simple-p (type) 643 (not (or (function-ctype-rest type) 644 (function-ctype-keyp type)))) 645 (every-csubtypep (types1 types2) 646 (loop 647 for a1 in types1 648 for a2 in types2 649 do (multiple-value-bind (res sure-p) 650 (csubtypep a1 a2) 651 (unless res (return (values res sure-p)))) 652 finally (return (values t t))))) 653 (macrolet ((3and (x y) 654 `(multiple-value-bind (val1 win1) ,x 655 (if (and (not val1) win1) 656 (values nil t) 657 (multiple-value-bind (val2 win2) ,y 658 (if (and val1 val2) 659 (values t t) 660 (values nil (and win2 (not val2))))))))) 661 (3and (values-subtypep (function-ctype-returns type1) 662 (function-ctype-returns type2)) 663 (cond ((function-ctype-wild-args type2) (values t t)) 664 ((function-ctype-wild-args type1) 665 (cond ((function-ctype-keyp type2) (values nil nil)) 666 ((not (function-ctype-rest type2)) (values nil t)) 667 ((not (null (function-ctype-required type2))) (values nil t)) 668 (t (3and (type= *universal-type* (function-ctype-rest type2)) 669 (every/type #'type= *universal-type* 670 (function-ctype-optional type2)))))) 671 ((not (and (fun-type-simple-p type1) 672 (fun-type-simple-p type2))) 673 (values nil nil)) 674 (t (multiple-value-bind (min1 max1) (function-type-nargs type1) 675 (multiple-value-bind (min2 max2) (function-type-nargs type2) 676 (cond ((or (> max1 max2) (< min1 min2)) 677 (values nil t)) 678 ((and (= min1 min2) (= max1 max2)) 679 (3and (every-csubtypep (function-ctype-required type1) 680 (function-ctype-required type2)) 681 (every-csubtypep (function-ctype-optional type1) 682 (function-ctype-optional type2)))) 683 (t (every-csubtypep 684 (concatenate 'list 685 (function-ctype-required type1) 686 (function-ctype-optional type1)) 687 (concatenate 'list 688 (function-ctype-required type2) 689 (function-ctype-optional type2))))))))))))) 690 571 691 572 692 … … 575 695 576 696 ;;; The union or intersection of two FUNCTION types is FUNCTION. 697 ;;; (unless the types are type=) 577 698 ;;; 578 699 (define-type-method (function :simple-union) (type1 type2) 579 (declare (ignore type1 type2)) 580 (specifier-type 'function)) 700 (if (type= type1 type2) 701 type1 702 (specifier-type 'function))) 703 581 704 ;;; 582 705 (define-type-method (function :simple-intersection) (type1 type2) 583 (declare (ignore type1 type2)) 584 (values (specifier-type 'function) t)) 706 (if (type= type1 type2) 707 type1 708 (specifier-type 'function))) 585 709 586 710 … … 644 768 (dolist (key keys) 645 769 (when (or (atom key) (/= (length key) 2)) 646 ( error "Keyword type description is not a two-list: ~S." key))770 (signal-program-error "Keyword type description is not a two-list: ~S." key)) 647 771 (let ((kwd (first key))) 648 772 (when (member kwd key-info :test #'eq :key #'(lambda (x) (key-info-name x))) 649 ( error "Repeated keyword ~S in lambda list: ~S." kwd lambda-list))773 (signal-program-error "Repeated keyword ~S in lambda list: ~S." kwd lambda-list)) 650 774 (push (make-key-info :name kwd 651 775 :type (specifier-type (second key))) key-info))) … … 684 808 (nreverse result))) 685 809 686 (def-type-translator function (&optional args result)810 (def-type-translator function (&optional (args '*) (result '*)) 687 811 (let ((res (make-function-ctype 688 812 :returns (values-specifier-type result)))) … … 695 819 (let ((res (make-values-ctype))) 696 820 (parse-args-types values res) 821 (when (or (values-ctype-keyp res) (values-ctype-allowp res)) 822 (signal-program-error "&KEY or &ALLOW-OTHER-KEYS in values type: ~s" 823 res)) 697 824 res)) 698 825 … … 706 833 (cond ((values-ctype-p type) 707 834 (or (car (args-ctype-required type)) 708 (car (args-ctype-optional type)) 835 (if (args-ctype-optional type) 836 (type-union (car (args-ctype-optional type)) 837 (specifier-type 'null))) 709 838 (args-ctype-rest type) 710 *universal-type*))839 (specifier-type 'null))) 711 840 ((eq type *wild-type*) 712 841 *universal-type*) … … 732 861 (values nil nil))) 733 862 734 ;;; cons-ctype735 (defun wild-ctype-to-universal-ctype (c)736 (if (type= c *wild-type*)737 *universal-type*738 c))739 740 (defun make-cons-ctype (car-ctype-value cdr-ctype-value)741 (%istruct 'cons-ctype742 (type-class-or-lose 'cons)743 nil744 (wild-ctype-to-universal-ctype car-ctype-value)745 (wild-ctype-to-universal-ctype cdr-ctype-value)))746 747 (def-type-translator cons (&optional (car-type-spec '*) (cdr-type-spec '*))748 (make-cons-ctype (specifier-type car-type-spec)749 (specifier-type cdr-type-spec)))750 751 (define-type-method (cons :unparse) (type)752 (let* ((car-spec (type-specifier (cons-ctype-car-ctype type)))753 (cdr-spec (type-specifier (cons-ctype-cdr-ctype type))))754 (if (and (member car-spec '(t *))755 (member cdr-spec '(t *)))756 'cons757 `(cons ,car-spec ,cdr-spec))))758 759 (define-type-method (cons :simple-=) (type1 type2)760 (declare (cons-ctype type1 type2))761 (and (type= (cons-ctype-car-ctype type1) (cons-ctype-car-ctype type2))762 (type= (cons-ctype-cdr-ctype type1) (cons-ctype-cdr-ctype type2))))763 764 (define-type-method (cons :simple-subtypep) (type1 type2)765 (declare (cons-ctype type1 type2))766 (multiple-value-bind (car-is-subtype car-definitely)767 (csubtypep (cons-ctype-car-ctype type1) (cons-ctype-car-ctype type2))768 (multiple-value-bind (cdr-is-subtype cdr-definitely)769 (csubtypep (cons-ctype-cdr-ctype type1) (cons-ctype-cdr-ctype type2))770 (if (and car-is-subtype cdr-is-subtype)771 (values t t)772 (values nil (or cdr-definitely car-definitely))))))773 863 774 864 ;;; Values-Types -- Interface … … 802 892 ;;; keywords or rest, *empty-type*. 803 893 ;;; 804 (defun values-type-types (type )894 (defun values-type-types (type &optional (default-type *empty-type*)) 805 895 (declare (type values-type type)) 806 896 (values (append (args-ctype-required type) 807 (args-ctype-optional type))897 (args-ctype-optional type)) 808 898 (cond ((args-ctype-keyp type) *universal-type*) 809 ((args-ctype-rest type)) 810 (t 811 *empty-type*)))) 899 ((args-ctype-rest type)) 900 (t default-type)))) 812 901 813 902 … … 823 912 (values (mapcar #'(lambda (t1 t2) 824 913 (multiple-value-bind (res win) 825 (funcall operation t1 t2)914 (funcall operation t1 t2) 826 915 (unless win (setq exact nil)) 827 916 res)) 828 917 types1 829 918 (append types2 830 (make-list (- (length types1) (length types2))831 :initial-element rest2)))919 (make-list (- (length types1) (length types2)) 920 :initial-element rest2))) 832 921 exact))) 833 922 … … 841 930 (if (values-ctype-p type) 842 931 type 843 (make-values-ctype :required (list type) :rest *universal-type*)))932 (make-values-ctype :required (list type)))) 844 933 845 934 … … 873 962 ;;; doesn't mean the result is exact. 874 963 ;;; 875 (defun args-type-op (type1 type2 operation nreq) 876 (declare (type ctype type1 type2) (type function operation nreq)) 877 (if (or (values-ctype-p type1) (values-ctype-p type2)) 878 (let ((type1 (coerce-to-values type1)) 964 (defun args-type-op (type1 type2 operation nreq default-type) 965 (declare (type ctype type1 type2 default-type) 966 (type function operation nreq)) 967 (if (eq type1 type2) 968 (values type1 t) 969 (if (or (values-ctype-p type1) (values-ctype-p type2)) 970 (let ((type1 (coerce-to-values type1)) 879 971 (type2 (coerce-to-values type2))) 880 972 (multiple-value-bind (types1 rest1) 881 (values-type-types type1)973 (values-type-types type1 default-type) 882 974 (multiple-value-bind (types2 rest2) 883 (values-type-types type2)975 (values-type-types type2 default-type) 884 976 (multiple-value-bind (rest rest-exact) 885 (funcall operation rest1 rest2)977 (funcall operation rest1 rest2) 886 978 (multiple-value-bind 887 979 (res res-exact) 888 980 (if (< (length types1) (length types2)) 889 (fixed-values-op types2 types1 rest1 operation)890 (fixed-values-op types1 types2 rest2 operation))891 (let* ((req (funcall nreq892 (length (args-ctype-required type1))893 (length (args-ctype-required type2))))894 (required (subseq res 0 req))895 (opt (subseq res req))896 (opt-last (position rest opt :test-not #'type=897 :from-end t)))898 (if (find *empty-type* required :test #'type=)899 (values *empty-type* t)900 (values (make-values-ctype901 :required required902 :optional (if opt-last903 (subseq opt 0 (1+ opt-last))904 ())905 :rest (if (eq rest *empty-type*) nil rest))906 (and rest-exact res-exact)))))))))907 (funcall operation type1 type2)))981 (fixed-values-op types2 types1 rest1 operation) 982 (fixed-values-op types1 types2 rest2 operation)) 983 (let* ((req (funcall nreq 984 (length (args-ctype-required type1)) 985 (length (args-ctype-required type2)))) 986 (required (subseq res 0 req)) 987 (opt (subseq res req)) 988 (opt-last (position rest opt :test-not #'type= 989 :from-end t))) 990 (if (find *empty-type* required :test #'type=) 991 (values *empty-type* t) 992 (values (make-values-ctype 993 :required required 994 :optional (if opt-last 995 (subseq opt 0 (1+ opt-last)) 996 ()) 997 :rest (if (eq rest *empty-type*) nil rest)) 998 (and rest-exact res-exact))))))))) 999 (funcall operation type1 type2)))) 908 1000 909 1001 ;;; Values-Type-Union, Values-Type-Intersection -- Interface … … 918 1010 (declare (type ctype type1 type2)) 919 1011 (cond ((or (eq type1 *wild-type*) (eq type2 *wild-type*)) *wild-type*) 920 ((eq type1 *empty-type*) type2)921 ((eq type2 *empty-type*) type1)922 (t923 (values (args-type-op type1 type2 #'type-union #'min)))))1012 ((eq type1 *empty-type*) type2) 1013 ((eq type2 *empty-type*) type1) 1014 (t 1015 (values (args-type-op type1 type2 #'type-union #'min *empty-type*))))) 924 1016 925 1017 (defun values-type-intersection (type1 type2) … … 928 1020 ((eq type2 *wild-type*) (values type1 t)) 929 1021 (t 930 (args-type-op type1 type2 #'type-intersection #'max)))) 1022 (args-type-op type1 type2 #'type-intersection #'max 1023 (specifier-type 'null))))) 931 1024 932 1025 … … 957 1050 (declare (type ctype type1 type2)) 958 1051 (cond ((eq type2 *wild-type*) (values t t)) 959 ((eq type1 *wild-type*)960 (values (eq type2 *universal-type*) t))961 ((not (values-types-intersect type1 type2))962 (values nil t))963 (t964 (if (or (values-ctype-p type1) (values-ctype-p type2))965 (let ((type1 (coerce-to-values type1))966 (type2 (coerce-to-values type2)))967 (multiple-value-bind (types1 rest1)968 (values-type-types type1)969 (multiple-value-bind (types2 rest2)970 (values-type-types type2)971 (cond ((< (length (values-ctype-required type1))972 (length (values-ctype-required type2)))973 (values nil t))974 ((< (length types1) (length types2))975 (values nil nil))976 ((or (values-ctype-keyp type1)977 (values-ctype-keyp type2))978 (values nil nil))979 (t980 (do ((t1 types1 (rest t1))981 (t2 types2 (rest t2)))982 ((null t2)983 (csubtypep rest1 rest2))984 (multiple-value-bind985 (res win-p)986 (csubtypep (first t1) (first t2))987 (unless win-p988 (return (values nil nil)))989 (unless res990 (return (values nil t))))))))))991 (csubtypep type1 type2)))))1052 ((eq type1 *wild-type*) 1053 (values (eq type2 *universal-type*) t)) 1054 ((not (values-types-intersect type1 type2)) 1055 (values nil t)) 1056 (t 1057 (if (or (values-ctype-p type1) (values-ctype-p type2)) 1058 (let ((type1 (coerce-to-values type1)) 1059 (type2 (coerce-to-values type2))) 1060 (multiple-value-bind (types1 rest1) 1061 (values-type-types type1) 1062 (multiple-value-bind (types2 rest2) 1063 (values-type-types type2) 1064 (cond ((< (length (values-ctype-required type1)) 1065 (length (values-ctype-required type2))) 1066 (values nil t)) 1067 ((< (length types1) (length types2)) 1068 (values nil nil)) 1069 ((or (values-ctype-keyp type1) 1070 (values-ctype-keyp type2)) 1071 (values nil nil)) 1072 (t 1073 (do ((t1 types1 (rest t1)) 1074 (t2 types2 (rest t2))) 1075 ((null t2) 1076 (csubtypep rest1 rest2)) 1077 (multiple-value-bind 1078 (res win-p) 1079 (csubtypep (first t1) (first t2)) 1080 (unless win-p 1081 (return (values nil nil))) 1082 (unless res 1083 (return (values nil t)))))))))) 1084 (csubtypep type1 type2))))) 992 1085 993 1086 … … 1006 1099 (report-bad-arg type2 'ctype)) 1007 1100 (cond ((or (eq type1 type2) 1008 (eq type1 *empty-type*)1009 (eq type2 *wild-type*))1010 (values t t))1011 ((or (eq type1 *wild-type*)1012 (eq type2 *empty-type*))1013 (values nil t))1014 (t1015 (invoke-type-method :simple-subtypep :complex-subtypep-arg21016 type1 type21017 :complex-arg1 :complex-subtypep-arg1))))1101 (eq type1 *empty-type*) 1102 (eq type2 *wild-type*)) 1103 (values t t)) 1104 ((or (eq type1 *wild-type*) 1105 (eq type2 *empty-type*)) 1106 (values nil t)) 1107 (t 1108 (invoke-type-method :simple-subtypep :complex-subtypep-arg2 1109 type1 type2 1110 :complex-arg1 :complex-subtypep-arg1)))) 1018 1111 ;;; Type= -- Interface 1019 1112 ;;; … … 1038 1131 (declare (type ctype type1 type2)) 1039 1132 (multiple-value-bind (res win) 1040 (type= type1 type2)1133 (type= type1 type2) 1041 1134 (if win 1042 1135 (values (not res) t) … … 1052 1145 ;;; 1053 1146 1054 (defun type-union (type1 type2) 1147 (defun type-union (&rest input-types) 1148 (%type-union input-types)) 1149 1150 (defun %type-union (input-types) 1151 (let* ((simplified (simplify-unions input-types))) 1152 (cond ((null simplified) *empty-type*) 1153 ((null (cdr simplified)) (car simplified)) 1154 (t (make-union-ctype simplified))))) 1155 1156 (defun simplify-unions (types) 1157 (when types 1158 (multiple-value-bind (first rest) 1159 (if (union-ctype-p (car types)) 1160 (values (car (union-ctype-types (car types))) 1161 (append (cdr (union-ctype-types (car types))) 1162 (cdr types))) 1163 (values (car types) (cdr types))) 1164 (let ((rest (simplify-unions rest)) u) 1165 (dolist (r rest (cons first rest)) 1166 (when (setq u (type-union2 first r)) 1167 (return (simplify-unions (nsubstitute u r rest))))))))) 1168 1169 (defun type-union2 (type1 type2) 1055 1170 (declare (type ctype type1 type2)) 1056 (if (eq type1 type2) 1057 type1 1058 (let ((res (invoke-type-method :simple-union :complex-union 1059 type1 type2 1060 :default :vanilla))) 1061 (cond ((eq res :vanilla) 1062 (or (vanilla-union type1 type2) 1063 (make-union-ctype (list type1 type2)))) 1064 (res) 1065 (t 1066 (make-union-ctype (list type1 type2))))))) 1171 (setq type1 (reparse-unknown-ctype type1)) 1172 (setq type2 (reparse-unknown-ctype type2)) 1173 (cond ((eq type1 type2) type1) 1174 ((csubtypep type1 type2) type2) 1175 ((csubtypep type2 type1) type1) 1176 (t 1177 (flet ((1way (x y) 1178 (invoke-type-method :simple-union :complex-union 1179 x y 1180 :default nil))) 1181 (or (1way type1 type2) 1182 (1way type2 type1)))))) 1183 1184 ;;; Return as restrictive and simple a type as we can discover that is 1185 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At 1186 ;;; worst, we arbitrarily return one of the arguments as the first 1187 ;;; value (trying not to return a hairy type). 1188 (defun type-approx-intersection2 (type1 type2) 1189 (cond ((type-intersection2 type1 type2)) 1190 ((hairy-ctype-p type1) type2) 1191 (t type1))) 1192 1067 1193 1068 1194 ;;; Type-Intersection -- Interface … … 1074 1200 ;;; 1075 1201 1076 (defun type-intersection (type1 type2) 1202 (defun type-intersection (&rest input-types) 1203 (%type-intersection input-types)) 1204 1205 (defun %type-intersection (input-types) 1206 (let ((simplified (simplify-intersections input-types))) 1207 ;(declare (type (vector ctype) simplified)) 1208 ;; We want to have a canonical representation of types (or failing 1209 ;; that, punt to HAIRY-TYPE). Canonical representation would have 1210 ;; intersections inside unions but not vice versa, since you can 1211 ;; always achieve that by the distributive rule. But we don't want 1212 ;; to just apply the distributive rule, since it would be too easy 1213 ;; to end up with unreasonably huge type expressions. So instead 1214 ;; we try to generate a simple type by distributing the union; if 1215 ;; the type can't be made simple, we punt to HAIRY-TYPE. 1216 (if (and (cdr simplified) (some #'union-ctype-p simplified)) 1217 (let* ((first-union (find-if #'union-ctype-p simplified)) 1218 (other-types (remove first-union simplified)) 1219 (distributed (maybe-distribute-one-union first-union other-types))) 1220 (if distributed 1221 (apply #'type-union distributed) 1222 (make-hairy-ctype 1223 :specifier `(and ,@(mapcar #'type-specifier simplified))))) 1224 (cond 1225 ((null simplified) *universal-type*) 1226 ((null (cdr simplified)) (car simplified)) 1227 (t (make-intersection-ctype 1228 (some #'(lambda (c) (ctype-enumerable c)) simplified) 1229 simplified)))))) 1230 1231 (defun simplify-intersections (types) 1232 (when types 1233 (multiple-value-bind (first rest) 1234 (if (intersection-ctype-p (car types)) 1235 (values (car (intersection-ctype-types (car types))) 1236 (append (cdr (intersection-ctype-types (car types))) 1237 (cdr types))) 1238 (values (car types) (cdr types))) 1239 (let ((rest (simplify-intersections rest)) u) 1240 (dolist (r rest (cons first rest)) 1241 (when (setq u (type-intersection2 first r)) 1242 (return (simplify-intersections (nsubstitute u r rest))))))))) 1243 1244 (defun type-intersection2 (type1 type2) 1077 1245 (declare (type ctype type1 type2)) 1078 (if (eq type1 type2) 1079 (values type1 t) 1080 (invoke-type-method :simple-intersection :complex-intersection 1081 type1 type2 1082 :default (values *empty-type* t)))) 1246 (setq type1 (reparse-unknown-ctype type1)) 1247 (setq type2 (reparse-unknown-ctype type2)) 1248 (cond ((eq type1 type2) 1249 type1) 1250 ((or (intersection-ctype-p type1) 1251 (intersection-ctype-p type2)) 1252 ;; Intersections of INTERSECTION-TYPE should have the 1253 ;; INTERSECTION-CTYPE-TYPES values broken out and intersected 1254 ;; separately. The full TYPE-INTERSECTION function knows how 1255 ;; to do that, so let it handle it. 1256 (type-intersection type1 type2)) 1257 ;; 1258 ;; (AND (FUNCTION (T) T) GENERIC-FUNCTION) for instance, but 1259 ;; not (AND (FUNCTION (T) T) (FUNCTION (T) T)). 1260 ((let ((function (specifier-type 'function))) 1261 (or (and (function-ctype-p type1) 1262 (not (or (function-ctype-p type2) (eq function type2))) 1263 (csubtypep type2 function) 1264 (not (csubtypep function type2))) 1265 (and (function-ctype-p type2) 1266 (not (or (function-ctype-p type1) (eq function type1))) 1267 (csubtypep type1 function) 1268 (not (csubtypep function type1))))) 1269 nil) 1270 (t 1271 (flet ((1way (x y) 1272 (invoke-type-method :simple-intersection 1273 :complex-intersection 1274 x y 1275 :default :no-type-method-found))) 1276 (let ((xy (1way type1 type2))) 1277 (or (and (not (eql xy :no-type-method-found)) xy) 1278 (let ((yx (1way type2 type1))) 1279 (or (and (not (eql yx :no-type-method-found)) yx) 1280 (cond ((and (eql xy :no-type-method-found) 1281 (eql yx :no-type-method-found)) 1282 *empty-type*) 1283 (t 1284 nil)))))))))) 1285 1286 1287 1288 (defun maybe-distribute-one-union (union-type types) 1289 (let* ((intersection (apply #'type-intersection types)) 1290 (union (mapcar (lambda (x) (type-intersection x intersection)) 1291 (union-ctype-types union-type)))) 1292 (if (notany (lambda (x) 1293 (or (hairy-ctype-p x) 1294 (intersection-ctype-p x))) 1295 union) 1296 union 1297 nil))) 1083 1298 1084 1299 ;;; Types-Intersect -- Interface … … 1093 1308 (if (or (eq type1 *empty-type*) (eq type2 *empty-type*)) 1094 1309 (values t t) 1095 (multiple-value-bind (val winp) 1096 (type-intersection type1 type2) 1097 (cond ((not winp) 1310 (let ((intersection2 (type-intersection2 type1 type2))) 1311 (cond ((not intersection2) 1098 1312 (if (or (csubtypep *universal-type* type1) 1099 1313 (csubtypep *universal-type* type2)) 1100 1314 (values t t) 1101 1315 (values t nil))) 1102 ((eq val*empty-type*) (values nil t))1316 ((eq intersection2 *empty-type*) (values nil t)) 1103 1317 (t (values t t)))))) 1104 1318 … … 1218 1432 (let ((res (values-specifier-type x))) 1219 1433 (when (values-ctype-p res) 1220 ( error "VALUES type illegal in this context:~% ~S" x))1434 (signal-program-error "VALUES type illegal in this context:~% ~S" x)) 1221 1435 res)) 1222 1436 1437 (defun single-value-specifier-type (x) 1438 (let ((res (specifier-type x))) 1439 (if (eq res *wild-type*) 1440 *universal-type* 1441 res))) 1442 1443 (defun modified-numeric-type (base 1444 &key 1445 (class (numeric-ctype-class base)) 1446 (format (numeric-ctype-format base)) 1447 (complexp (numeric-ctype-complexp base)) 1448 (low (numeric-ctype-low base)) 1449 (high (numeric-ctype-high base)) 1450 (enumerable (ctype-enumerable base))) 1451 (make-numeric-ctype :class class 1452 :format format 1453 :complexp complexp 1454 :low low 1455 :high high 1456 :enumerable enumerable)) 1223 1457 1224 1458 ;;; Precompute-Types -- Interface … … 1262 1496 (values (eq type1 type2) t)) 1263 1497 1498 (define-type-method (named :complex-=) (type1 type2) 1499 (cond 1500 ((and (eq type2 *empty-type*) 1501 (intersection-ctype-p type1) 1502 ;; not allowed to be unsure on these... FIXME: keep the list 1503 ;; of CL types that are intersection types once and only 1504 ;; once. 1505 (not (or (type= type1 (specifier-type 'ratio)) 1506 (type= type1 (specifier-type 'keyword))))) 1507 ;; things like (AND (EQL 0) (SATISFIES ODDP)) or (AND FUNCTION 1508 ;; STREAM) can get here. In general, we can't really tell 1509 ;; whether these are equal to NIL or not, so 1510 (values nil nil)) 1511 ((type-might-contain-other-types-p type1) 1512 (invoke-complex-=-other-method type1 type2)) 1513 (t (values nil t)))) 1514 1515 1264 1516 (define-type-method (named :simple-subtypep) (type1 type2) 1265 1517 (values (or (eq type1 *empty-type*) (eq type2 *wild-type*)) t)) 1266 1518 1267 1519 (define-type-method (named :complex-subtypep-arg1) (type1 type2) 1268 (assert (not (hairy-ctype-p type2))) 1269 (values (eq type1 *empty-type*) t)) 1520 (cond ((eq type1 *empty-type*) 1521 t) 1522 (;; When TYPE2 might be the universal type in disguise 1523 (type-might-contain-other-types-p type2) 1524 ;; Now that the UNION and HAIRY COMPLEX-SUBTYPEP-ARG2 methods 1525 ;; can delegate to us (more or less as CALL-NEXT-METHOD) when 1526 ;; they're uncertain, we can't just barf on COMPOUND-TYPE and 1527 ;; HAIRY-TYPEs as we used to. Instead we deal with the 1528 ;; problem (where at least part of the problem is cases like 1529 ;; (SUBTYPEP T '(SATISFIES FOO)) 1530 ;; or 1531 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR))) 1532 ;; where the second type is a hairy type like SATISFIES, or 1533 ;; is a compound type which might contain a hairy type) by 1534 ;; returning uncertainty. 1535 (values nil nil)) 1536 (t 1537 ;; By elimination, TYPE1 is the universal type. 1538 (assert (or (eq type1 *wild-type*) (eq type1 *universal-type*))) 1539 ;; This case would have been picked off by the SIMPLE-SUBTYPEP 1540 ;; method, and so shouldn't appear here. 1541 (assert (not (eq type2 *universal-type*))) 1542 ;; Since TYPE2 is not EQ *UNIVERSAL-TYPE* and is not the 1543 ;; universal type in disguise, TYPE2 is not a superset of TYPE1. 1544 (values nil t)))) 1545 1270 1546 1271 1547 (define-type-method (named :complex-subtypep-arg2) (type1 type2) 1272 (if (hairy-ctype-p type1) 1273 (values nil nil) 1274 (values (not (eq type2 *empty-type*)) t))) 1548 (assert (not (eq type2 *wild-type*))) ; * isn't really a type. 1549 (cond ((eq type2 *universal-type*) 1550 (values t t)) 1551 ((type-might-contain-other-types-p type1) 1552 ;; those types can be *EMPTY-TYPE* or *UNIVERSAL-TYPE* in 1553 ;; disguise. So we'd better delegate. 1554 (invoke-complex-subtypep-arg1-method type1 type2)) 1555 (t 1556 ;; FIXME: This seems to rely on there only being 2 or 3 1557 ;; NAMED-TYPE values, and the exclusion of various 1558 ;; possibilities above. It would be good to explain it and/or 1559 ;; rewrite it so that it's clearer. 1560 (values (not (eq type2 *empty-type*)) t)))) 1561 1275 1562 1276 1563 (define-type-method (named :complex-intersection) (type1 type2) 1277 ( vanilla-intersectiontype1 type2))1564 (hierarchical-intersection2 type1 type2)) 1278 1565 1279 1566 (define-type-method (named :unparse) (x) … … 1284 1571 ;;;; Hairy and unknown types: 1285 1572 1286 ;;; The Hairy-Type represents anything too wierd to be described reasonably or1287 ;;; to be useful, such as AND, NOT and SATISFIES and unknown types. We just1288 ;;; rememberthe original type spec.1573 ;;; The Hairy-Type represents anything too wierd to be described 1574 ;;; reasonably or to be useful, such as SATISFIES. We just remember 1575 ;;; the original type spec. 1289 1576 ;;; 1290 1577 … … 1305 1592 (let ((hairy-spec1 (hairy-ctype-specifier type1)) 1306 1593 (hairy-spec2 (hairy-ctype-specifier type2))) 1307 (cond ((and (consp hairy-spec1) (eq (car hairy-spec1) 'not) 1308 (consp hairy-spec2) (eq (car hairy-spec2) 'not)) 1309 (csubtypep (specifier-type (cadr hairy-spec2)) 1310 (specifier-type (cadr hairy-spec1)))) 1311 ((equal hairy-spec1 hairy-spec2) 1594 (cond ((equal-but-no-car-recursion hairy-spec1 hairy-spec2) 1312 1595 (values t t)) 1313 1596 (t … … 1315 1598 1316 1599 (define-type-method (hairy :complex-subtypep-arg2) (type1 type2) 1317 (let ((hairy-spec (hairy-ctype-specifier type2))) 1318 (cond 1319 ((and (consp hairy-spec) (eq (car hairy-spec) 'not)) 1320 (multiple-value-bind (val win) 1321 (type-intersection type1 (specifier-type (cadr hairy-spec))) 1322 (if win 1323 (values (eq val *empty-type*) t) 1324 (values nil nil)))) 1325 ((and (consp hairy-spec) (eq (car hairy-spec) 'and)) 1326 (block PUNT 1327 (values (every-type-op csubtypep type1 1328 (mapcar #'specifier-type (cdr hairy-spec))) 1329 t))) 1330 (t 1331 (values nil nil))))) 1600 (invoke-complex-subtypep-arg1-method type1 type2)) 1332 1601 1333 1602 (define-type-method (hairy :complex-subtypep-arg1) (type1 type2) 1334 (let ((hairy-spec (hairy-ctype-specifier type1)))1335 (cond1336 ((and (consp hairy-spec) (eq (car hairy-spec) 'not))1337 ;; We're definitely not (exactly) what we're not, and1338 ;; definitely not a UNION type that contains exactly1339 ;; what we're not; after that, it gets harder.1340 ;; I wonder whether it makes more sense to implement ATGM1341 ;; and RATIO some other way enirely.1342 (let* ((negated-ctype (specifier-type (cadr hairy-spec))))1343 (if (or (eq type2 negated-ctype)1344 (and (typep type2 'union-ctype)1345 (member negated-ctype (union-ctype-types type2))))1346 (values nil t)1347 (values nil nil))))1348 ((and (consp hairy-spec) (eq (car hairy-spec) 'and))1349 (block PUNT1350 (if (any-type-op csubtypep type21351 (mapcar #'specifier-type (cdr hairy-spec))1352 :list-first t)1353 (values t t)1354 (values nil nil))))1355 (t1356 (values nil nil)))))1357 1358 (define-type-method (hairy :complex-=)1359 (type1 type2)1360 1603 (declare (ignore type1 type2)) 1361 1604 (values nil nil)) 1362 1605 1606 (define-type-method (hairy :complex-=) (type1 type2) 1607 (if (and (unknown-ctype-p type2) 1608 (let* ((specifier2 (unknown-ctype-specifier type2)) 1609 (name2 (if (consp specifier2) 1610 (car specifier2) 1611 specifier2))) 1612 (info-type-kind name2))) 1613 (let ((type2 (specifier-type (unknown-ctype-specifier type2)))) 1614 (if (unknown-ctype-p type2) 1615 (values nil nil) 1616 (type= type1 type2))) 1617 (values nil nil))) 1618 1363 1619 (define-type-method (hairy :simple-intersection :complex-intersection) 1364 1620 (type1 type2) 1621 (if (type= type1 type2) 1622 type1 1623 nil)) 1624 1625 1626 (define-type-method (hairy :simple-union) 1627 (type1 type2) 1628 (if (type= type1 type2) 1629 type1 1630 nil)) 1631 1632 (define-type-method (hairy :simple-=) (type1 type2) 1633 (if (equal-but-no-car-recursion (hairy-ctype-specifier type1) 1634 (hairy-ctype-specifier type2)) 1635 (values t t) 1636 (values nil nil))) 1637 1638 1639 1640 (def-type-translator satisfies (&whole x fun) 1641 (unless (symbolp fun) 1642 (report-bad-arg fun 'symbol)) 1643 (make-hairy-ctype :specifier x)) 1644 1645 1646 1647 ;;; Negation Ctypes 1648 (defun make-negation-ctype (&key type (enumerable t)) 1649 (%istruct 'negation-ctype 1650 (type-class-or-lose 'negation) 1651 enumerable 1652 type)) 1653 1654 (defun negation-ctype-p (x) 1655 (istruct-typep x 'negation-ctype)) 1656 1657 (setf (type-predicate 'negation-ctype) 'negation-ctype-p) 1658 1659 1660 (define-type-method (negation :unparse) (x) 1661 `(not ,(type-specifier (negation-ctype-type x)))) 1662 1663 (define-type-method (negation :simple-subtypep) (type1 type2) 1664 (csubtypep (negation-ctype-type type2) (negation-ctype-type type1))) 1665 1666 (define-type-method (negation :complex-subtypep-arg2) (type1 type2) 1667 (let* ((complement-type2 (negation-ctype-type type2)) 1668 (intersection2 (type-intersection type1 complement-type2))) 1669 (if intersection2 1670 ;; FIXME: if uncertain, maybe try arg1? 1671 (type= intersection2 *empty-type*) 1672 (invoke-complex-subtypep-arg1-method type1 type2)))) 1673 1674 (define-type-method (negation :complex-subtypep-arg1) (type1 type2) 1675 (block nil 1676 ;; (Several logical truths in this block are true as long as 1677 ;; b/=T. As of sbcl-0.7.1.28, it seems impossible to construct a 1678 ;; case with b=T where we actually reach this type method, but 1679 ;; we'll test for and exclude this case anyway, since future 1680 ;; maintenance might make it possible for it to end up in this 1681 ;; code.) 1682 (multiple-value-bind (equal certain) 1683 (type= type2 *universal-type*) 1684 (unless certain 1685 (return (values nil nil))) 1686 (when equal 1687 (return (values t t)))) 1688 (let ((complement-type1 (negation-ctype-type type1))) 1689 ;; Do the special cases first, in order to give us a chance if 1690 ;; subtype/supertype relationships are hairy. 1691 (multiple-value-bind (equal certain) 1692 (type= complement-type1 type2) 1693 ;; If a = b, ~a is not a subtype of b (unless b=T, which was 1694 ;; excluded above). 1695 (unless certain 1696 (return (values nil nil))) 1697 (when equal 1698 (return (values nil t)))) 1699 ;; KLUDGE: ANSI requires that the SUBTYPEP result between any 1700 ;; two built-in atomic type specifiers never be uncertain. This 1701 ;; is hard to do cleanly for the built-in types whose 1702 ;; definitions include (NOT FOO), i.e. CONS and RATIO. However, 1703 ;; we can do it with this hack, which uses our global knowledge 1704 ;; that our implementation of the type system uses disjoint 1705 ;; implementation types to represent disjoint sets (except when 1706 ;; types are contained in other types). (This is a KLUDGE 1707 ;; because it's fragile. Various changes in internal 1708 ;; representation in the type system could make it start 1709 ;; confidently returning incorrect results.) -- WHN 2002-03-08 1710 (unless (or (type-might-contain-other-types-p complement-type1) 1711 (type-might-contain-other-types-p type2)) 1712 ;; Because of the way our types which don't contain other 1713 ;; types are disjoint subsets of the space of possible values, 1714 ;; (SUBTYPEP '(NOT AA) 'B)=NIL when AA and B are simple (and B 1715 ;; is not T, as checked above). 1716 (return (values nil t))) 1717 ;; The old (TYPE= TYPE1 TYPE2) branch would never be taken, as 1718 ;; TYPE1 and TYPE2 will only be equal if they're both NOT types, 1719 ;; and then the :SIMPLE-SUBTYPEP method would be used instead. 1720 ;; But a CSUBTYPEP relationship might still hold: 1721 (multiple-value-bind (equal certain) 1722 (csubtypep complement-type1 type2) 1723 ;; If a is a subtype of b, ~a is not a subtype of b (unless 1724 ;; b=T, which was excluded above). 1725 (unless certain 1726 (return (values nil nil))) 1727 (when equal 1728 (return (values nil t)))) 1729 (multiple-value-bind (equal certain) 1730 (csubtypep type2 complement-type1) 1731 ;; If b is a subtype of a, ~a is not a subtype of b. (FIXME: 1732 ;; That's not true if a=T. Do we know at this point that a is 1733 ;; not T?) 1734 (unless certain 1735 (return (values nil nil))) 1736 (when equal 1737 (return (values nil t)))) 1738 ;; old CSR comment ca. 0.7.2, now obsoleted by the SIMPLE-CTYPE? 1739 ;; KLUDGE case above: Other cases here would rely on being able 1740 ;; to catch all possible cases, which the fragility of this type 1741 ;; system doesn't inspire me; for instance, if a is type= to ~b, 1742 ;; then we want T, T; if this is not the case and the types are 1743 ;; disjoint (have an intersection of *empty-type*) then we want 1744 ;; NIL, T; else if the union of a and b is the *universal-type* 1745 ;; then we want T, T. So currently we still claim to be unsure 1746 ;; about e.g. (subtypep '(not fixnum) 'single-float). 1747 ;; 1748 ;; OTOH we might still get here: 1749 (values nil nil)))) 1750 1751 (define-type-method (negation :complex-=) (type1 type2) 1752 ;; (NOT FOO) isn't equivalent to anything that's not a negation 1753 ;; type, except possibly a type that might contain it in disguise. 1365 1754 (declare (ignore type2)) 1366 (values type1 nil)) 1367 1368 (define-type-method (hairy :complex-union) (type1 type2) 1369 (make-union-ctype (list type1 type2))) 1370 1371 (define-type-method (hairy :simple-=) (type1 type2) 1372 (if (equal (hairy-ctype-specifier type1) 1373 (hairy-ctype-specifier type2)) 1374 (values t t) 1375 (values nil nil))) 1376 1377 (def-type-translator not (&whole x type) 1378 (declare (ignore type)) 1379 (make-hairy-ctype :specifier x)) 1380 1381 (def-type-translator satisfies (&whole x fun) 1382 (declare (ignore fun)) 1383 (make-hairy-ctype :specifier x)) 1384 1385 ;;; An UNKNOWN-TYPE is a type not known to the type system (not yet defined). 1386 ;;; We make this distinction since we don't want to complain about types that 1387 ;;; are hairy but defined. 1388 ;;; 1389 1390 (defun make-unknown-ctype (&key specifier (enumerable t)) 1391 (%istruct 'unknown-ctype 1392 (type-class-or-lose 'hairy) 1393 enumerable 1394 specifier)) 1395 1396 (defun unknown-ctype-p (x) 1397 (istruct-typep x 'unknown-ctype)) 1398 1399 (setf (type-predicate 'unknown-ctype) 'unknown-ctype-p) 1755 (if (type-might-contain-other-types-p type1) 1756 (values nil nil) 1757 (values nil t))) 1758 1759 (define-type-method (negation :simple-intersection) (type1 type2) 1760 (let ((not1 (negation-ctype-type type1)) 1761 (not2 (negation-ctype-type type2))) 1762 (cond 1763 ((csubtypep not1 not2) type2) 1764 ((csubtypep not2 not1) type1) 1765 ;; Why no analagous clause to the disjoint in the SIMPLE-UNION2 1766 ;; method, below? The clause would read 1767 ;; 1768 ;; ((EQ (TYPE-UNION NOT1 NOT2) *UNIVERSAL-TYPE*) *EMPTY-TYPE*) 1769 ;; 1770 ;; but with proper canonicalization of negation types, there's 1771 ;; no way of constructing two negation types with union of their 1772 ;; negations being the universal type. 1773 (t 1774 nil)))) 1775 1776 (define-type-method (negation :complex-intersection) (type1 type2) 1777 (cond 1778 ((csubtypep type1 (negation-ctype-type type2)) *empty-type*) 1779 ((eq (type-intersection type1 (negation-ctype-type type2)) *empty-type*) 1780 type1) 1781 (t nil))) 1782 1783 (define-type-method (negation :simple-union) (type1 type2) 1784 (let ((not1 (negation-ctype-type type1)) 1785 (not2 (negation-ctype-type type2))) 1786 (cond 1787 ((csubtypep not1 not2) type1) 1788 ((csubtypep not2 not1) type2) 1789 ((eq (type-intersection not1 not2) *empty-type*) 1790 *universal-type*) 1791 (t nil)))) 1792 1793 (define-type-method (negation :complex-union) (type1 type2) 1794 (cond 1795 ((csubtypep (negation-ctype-type type2) type1) *universal-type*) 1796 ((eq (type-intersection type1 (negation-ctype-type type2)) *empty-type*) 1797 type2) 1798 (t nil))) 1799 1800 (define-type-method (negation :simple-=) (type1 type2) 1801 (type= (negation-ctype-type type1) (negation-ctype-type type2))) 1802 1803 (def-type-translator not (typespec) 1804 (let* ((not-type (specifier-type typespec)) 1805 (spec (type-specifier not-type))) 1806 (cond 1807 ;; canonicalize (NOT (NOT FOO)) 1808 ((and (listp spec) (eq (car spec) 'not)) 1809 (specifier-type (cadr spec))) 1810 ;; canonicalize (NOT NIL) and (NOT T) 1811 ((eq not-type *empty-type*) *universal-type*) 1812 ((eq not-type *universal-type*) *empty-type*) 1813 ((and (numeric-ctype-p not-type) 1814 (null (numeric-ctype-low not-type)) 1815 (null (numeric-ctype-high not-type))) 1816 (make-negation-ctype :type not-type)) 1817 ((numeric-ctype-p not-type) 1818 (type-union 1819 (make-negation-ctype 1820 :type (modified-numeric-type not-type :low nil :high nil)) 1821 (cond 1822 ((null (numeric-ctype-low not-type)) 1823 (modified-numeric-type 1824 not-type 1825 :low (let ((h (numeric-ctype-high not-type))) 1826 (if (consp h) (car h) (list h))) 1827 :high nil)) 1828 ((null (numeric-ctype-high not-type)) 1829 (modified-numeric-type 1830 not-type 1831 :low nil 1832 :high (let ((l (numeric-ctype-low not-type))) 1833 (if (consp l) (car l) (list l))))) 1834 (t (type-union 1835 (modified-numeric-type 1836 not-type 1837 :low nil 1838 :high (let ((l (numeric-ctype-low not-type))) 1839 (if (consp l) (car l) (list l)))) 1840 (modified-numeric-type 1841 not-type 1842 :low (let ((h (numeric-ctype-high not-type))) 1843 (if (consp h) (car h) (list h))) 1844 :high nil)))))) 1845 ((intersection-ctype-p not-type) 1846 (apply #'type-union 1847 (mapcar #'(lambda (x) 1848 (specifier-type `(not ,(type-specifier x)))) 1849 (intersection-ctype-types not-type)))) 1850 ((union-ctype-p not-type) 1851 (apply #'type-intersection 1852 (mapcar #'(lambda (x) 1853 (specifier-type `(not ,(type-specifier x)))) 1854 (union-ctype-types not-type)))) 1855 ((member-ctype-p not-type) 1856 (let ((members (member-ctype-members not-type))) 1857 (if (some #'floatp members) 1858 (let (floats) 1859 (dolist (pair '((0.0f0 . -0.0f0) (0.0d0 . -0.0d0))) 1860 (when (member (car pair) members) 1861 (assert (not (member (cdr pair) members))) 1862 (push (cdr pair) floats) 1863 (setf members (remove (car pair) members))) 1864 (when (member (cdr pair) members) 1865 (assert (not (member (car pair) members))) 1866 (push (car pair) floats) 1867 (setf members (remove (cdr pair) members)))) 1868 (apply #'type-intersection 1869 (if (null members) 1870 *universal-type* 1871 (make-negation-ctype 1872 :type (make-member-ctype :members members))) 1873 (mapcar 1874 (lambda (x) 1875 (let ((type (ctype-of x))) 1876 (type-union 1877 (make-negation-ctype 1878 :type (modified-numeric-type type 1879 :low nil :high nil)) 1880 (modified-numeric-type type 1881 :low nil :high (list x)) 1882 (make-member-ctype :members (list x)) 1883 (modified-numeric-type type 1884 :low (list x) :high nil)))) 1885 floats))) 1886 (make-negation-ctype :type not-type)))) 1887 ((and (cons-ctype-p not-type) 1888 (eq (cons-ctype-car-ctype not-type) *universal-type*) 1889 (eq (cons-ctype-cdr-ctype not-type) *universal-type*)) 1890 (make-negation-ctype :type not-type)) 1891 ((cons-ctype-p not-type) 1892 (type-union 1893 (make-negation-ctype :type (specifier-type 'cons)) 1894 (cond 1895 ((and (not (eq (cons-ctype-car-ctype not-type) *universal-type*)) 1896 (not (eq (cons-ctype-cdr-ctype not-type) *universal-type*))) 1897 (type-union 1898 (make-cons-ctype 1899 (specifier-type `(not ,(type-specifier 1900 (cons-ctype-car-ctype not-type)))) 1901 *universal-type*) 1902 (make-cons-ctype 1903 *universal-type* 1904 (specifier-type `(not ,(type-specifier 1905 (cons-ctype-cdr-ctype not-type))))))) 1906 ((not (eq (cons-ctype-car-ctype not-type) *universal-type*)) 1907 (make-cons-ctype 1908 (specifier-type `(not ,(type-specifier 1909 (cons-ctype-car-ctype not-type)))) 1910 *universal-type*)) 1911 ((not (eq (cons-ctype-cdr-ctype not-type) *universal-type*)) 1912 (make-cons-ctype 1913 *universal-type* 1914 (specifier-type `(not ,(type-specifier 1915 (cons-ctype-cdr-ctype not-type)))))) 1916 (t (error "Weird CONS type ~S" not-type))))) 1917 (t (make-negation-ctype :type not-type))))) 1918 1400 1919 1401 1920 ;;;; Numeric types. … … 1452 1971 (eq (numeric-ctype-format type1) (numeric-ctype-format type2)) 1453 1972 (eq (numeric-ctype-complexp type1) (numeric-ctype-complexp type2)) 1454 (equal (numeric-ctype-low type1) (numeric-ctype-low type2))1455 (equal (numeric-ctype-high type1) (numeric-ctype-high type2)))1973 (equalp (numeric-ctype-low type1) (numeric-ctype-low type2)) 1974 (equalp (numeric-ctype-high type1) (numeric-ctype-high type2))) 1456 1975 t)) 1457 1976 … … 1716 2235 1717 2236 (def-bounded-type rational rational nil) 2237 2238 (defun coerce-bound (bound type inner-coerce-bound-fun) 2239 (declare (type function inner-coerce-bound-fun)) 2240 (cond ((eql bound '*) 2241 bound) 2242 ((consp bound) 2243 (destructuring-bind (inner-bound) bound 2244 (list (funcall inner-coerce-bound-fun inner-bound type)))) 2245 (t 2246 (funcall inner-coerce-bound-fun bound type)))) 2247 2248 (defun inner-coerce-real-bound (bound type) 2249 (ecase type 2250 (rational (rationalize bound)) 2251 (float (if (floatp bound) 2252 bound 2253 ;; Coerce to the widest float format available, to 2254 ;; avoid unnecessary loss of precision: 2255 (coerce bound 'long-float))))) 2256 2257 (defun coerced-real-bound (bound type) 2258 (coerce-bound bound type #'inner-coerce-real-bound)) 2259 2260 (defun coerced-float-bound (bound type) 2261 (coerce-bound bound type #'coerce)) 2262 2263 (def-type-translator real (&optional (low '*) (high '*)) 2264 (specifier-type `(or (float ,(coerced-real-bound low 'float) 2265 ,(coerced-real-bound high 'float)) 2266 (rational ,(coerced-real-bound low 'rational) 2267 ,(coerced-real-bound high 'rational))))) 2268 2269 (def-type-translator float (&optional (low '*) (high '*)) 2270 (specifier-type 2271 `(or (single-float ,(coerced-float-bound low 'single-float) 2272 ,(coerced-float-bound high 'single-float)) 2273 (double-float ,(coerced-float-bound low 'double-float) 2274 ,(coerced-float-bound high 'double-float))))) 2275 1718 2276 (def-bounded-type float float nil) 1719 2277 (def-bounded-type real nil nil) … … 1730 2288 (declare (type numeric-ctype type1 type2)) 1731 2289 (let* ((class1 (numeric-ctype-class type1)) 1732 (class2 (numeric-ctype-class type2))1733 (complexp1 (numeric-ctype-complexp type1))1734 (complexp2 (numeric-ctype-complexp type2))1735 (format1 (numeric-ctype-format type1))1736 (format2 (numeric-ctype-format type2))1737 (low1 (numeric-ctype-low type1))1738 (high1 (numeric-ctype-high type1))1739 (low2 (numeric-ctype-low type2))1740 (high2 (numeric-ctype-high type2)))2290 (class2 (numeric-ctype-class type2)) 2291 (complexp1 (numeric-ctype-complexp type1)) 2292 (complexp2 (numeric-ctype-complexp type2)) 2293 (format1 (numeric-ctype-format type1)) 2294 (format2 (numeric-ctype-format type2)) 2295 (low1 (numeric-ctype-low type1)) 2296 (high1 (numeric-ctype-high type1)) 2297 (low2 (numeric-ctype-low type2)) 2298 (high2 (numeric-ctype-high type2))) 1741 2299 ;; 1742 2300 ;; If one is complex and the other isn't, then they are disjoint. 1743 2301 (cond ((not (or (eq complexp1 complexp2) 1744 (null complexp1) (null complexp2)))1745 nil)1746 ;;1747 ;; If either type is a float, then the other must either be specified1748 ;; to be a float or unspecified. Otherwise, they are disjoint.1749 ((and (eq class1 'float) (not (member class2 '(float nil)))) nil)1750 ((and (eq class2 'float) (not (member class1 '(float nil)))) nil)1751 ;;1752 ;; If the float formats are specified and different, the types1753 ;; are disjoint.1754 ((not (or (eq format1 format2) (null format1) (null format2)))1755 nil)1756 (t1757 ;;1758 ;; Check the bounds. This is a bit odd because we must always have1759 ;; the outer bound of the interval as the second arg.1760 (if (numeric-bound-test high1 high2 <= <)1761 (or (and (numeric-bound-test low1 low2 >= >)1762 (numeric-bound-test* low1 high2 <= <))1763 (and (numeric-bound-test low2 low1 >= >)1764 (numeric-bound-test* low2 high1 <= <)))1765 (or (and (numeric-bound-test* low2 high1 <= <)1766 (numeric-bound-test low2 low1 >= >))1767 (and (numeric-bound-test high2 high1 <= <)1768 (numeric-bound-test* high2 low1 >= >))))))))2302 (null complexp1) (null complexp2))) 2303 nil) 2304 ;; 2305 ;; If either type is a float, then the other must either be specified 2306 ;; to be a float or unspecified. Otherwise, they are disjoint. 2307 ((and (eq class1 'float) (not (member class2 '(float nil)))) nil) 2308 ((and (eq class2 'float) (not (member class1 '(float nil)))) nil) 2309 ;; 2310 ;; If the float formats are specified and different, the types 2311 ;; are disjoint. 2312 ((not (or (eq format1 format2) (null format1) (null format2))) 2313 nil) 2314 (t 2315 ;; 2316 ;; Check the bounds. This is a bit odd because we must always have 2317 ;; the outer bound of the interval as the second arg. 2318 (if (numeric-bound-test high1 high2 <= <) 2319 (or (and (numeric-bound-test low1 low2 >= >) 2320 (numeric-bound-test* low1 high2 <= <)) 2321 (and (numeric-bound-test low2 low1 >= >) 2322 (numeric-bound-test* low2 high1 <= <))) 2323 (or (and (numeric-bound-test* low2 high1 <= <) 2324 (numeric-bound-test low2 low1 >= >)) 2325 (and (numeric-bound-test high2 high1 <= <) 2326 (numeric-bound-test* high2 low1 >= >)))))))) 1769 2327 1770 2328 ;;; Round-Numeric-Bound -- Internal … … 1795 2353 (integer 1796 2354 (if (and (consp x) (integerp cx)) 1797 (if up-p (1+ cx) (1- cx))1798 (if up-p (ceiling cx) (floor cx))))2355 (if up-p (1+ cx) (1- cx)) 2356 (if up-p (ceiling cx) (floor cx)))) 1799 2357 (float 1800 2358 (let ((res (if format (coerce cx format) (float cx)))) … … 1823 2381 (if (numeric-types-intersect type1 type2) 1824 2382 (let* ((class1 (numeric-ctype-class type1)) 1825 (class2 (numeric-ctype-class type2)) 1826 (class (ecase class1 1827 ((nil) class2) 1828 ((integer float) class1) 1829 (rational (if (eq class2 'integer) 'integer 'rational)))) 1830 (format (or (numeric-ctype-format type1) 1831 (numeric-ctype-format type2)))) 1832 (values 1833 (make-numeric-ctype 1834 :class class 1835 :format format 1836 :complexp (or (numeric-ctype-complexp type1) 1837 (numeric-ctype-complexp type2)) 1838 :low (numeric-bound-max 1839 (round-numeric-bound (numeric-ctype-low type1) 1840 class format t) 1841 (round-numeric-bound (numeric-ctype-low type2) 1842 class format t) 1843 >= > nil) 1844 :high (numeric-bound-max 1845 (round-numeric-bound (numeric-ctype-high type1) 1846 class format nil) 1847 (round-numeric-bound (numeric-ctype-high type2) 1848 class format nil) 1849 <= < nil)) 1850 t)) 1851 (values *empty-type* t))) 2383 (class2 (numeric-ctype-class type2)) 2384 (class (ecase class1 2385 ((nil) class2) 2386 ((integer float) class1) 2387 (rational (if (eq class2 'integer) 'integer 'rational)))) 2388 (format (or (numeric-ctype-format type1) 2389 (numeric-ctype-format type2)))) 2390 (make-numeric-ctype 2391 :class class 2392 :format format 2393 :complexp (or (numeric-ctype-complexp type1) 2394 (numeric-ctype-complexp type2)) 2395 :low (numeric-bound-max 2396 (round-numeric-bound (numeric-ctype-low type1) 2397 class format t) 2398 (round-numeric-bound (numeric-ctype-low type2) 2399 class format t) 2400 >= > nil) 2401 :high (numeric-bound-max 2402 (round-numeric-bound (numeric-ctype-high type1) 2403 class format nil) 2404 (round-numeric-bound (numeric-ctype-high type2) 2405 class format nil) 2406 <= < nil))) 2407 *empty-type*)) 1852 2408 1853 2409 ;;; Float-Format-Max -- Interface … … 1883 2439 (complexp2 (numeric-ctype-complexp type2))) 1884 2440 (cond ((or (null complexp1) 1885 (null complexp2))2441 (null complexp2)) 1886 2442 (specifier-type 'number)) 1887 2443 ((eq class1 'float) … … 1891 2447 (float (float-format-max format1 format2)) 1892 2448 ((integer rational) format1) 1893 ((nil) nil)) 2449 ((nil) 2450 ;; A double-float with any real number is a 2451 ;; double-float. 2452 (if (eq format1 'double-float) 2453 'double-float 2454 nil))) 1894 2455 :complexp (if (or (eq complexp1 :complex) 1895 (eq complexp2 :complex))1896 :complex1897 :real)))2456 (eq complexp2 :complex)) 2457 :complex 2458 :real))) 1898 2459 ((eq class2 'float) (numeric-contagion type2 type1)) 1899 2460 ((and (eq complexp1 :real) (eq complexp2 :real)) … … 1904 2465 (specifier-type 'number)))) 1905 2466 (specifier-type 'number))) 2467 2468 1906 2469 1907 2470 … … 1942 2505 1943 2506 (define-type-method (array :simple-=) (type1 type2) 1944 (values (and (equal (array-ctype-dimensions type1) 1945 (array-ctype-dimensions type2)) 1946 (eq (array-ctype-complexp type1) 1947 (array-ctype-complexp type2)) 1948 (type= (specialized-element-type-maybe type1) 1949 (specialized-element-type-maybe type2))) 1950 t)) 2507 (if (or (unknown-ctype-p (array-ctype-element-type type1)) 2508 (unknown-ctype-p (array-ctype-element-type type2))) 2509 (multiple-value-bind (equalp certainp) 2510 (type= (array-ctype-element-type type1) 2511 (array-ctype-element-type type2)) 2512 (assert (not (and (not equalp) certainp))) 2513 (values equalp certainp)) 2514 (values (and (equal (array-ctype-dimensions type1) 2515 (array-ctype-dimensions type2)) 2516 (eq (array-ctype-complexp type1) 2517 (array-ctype-complexp type2)) 2518 (type= (specialized-element-type-maybe type1) 2519 (specialized-element-type-maybe type2))) 2520 t))) 1951 2521 1952 2522 (define-type-method (array :unparse) (type) … … 1970 2540 (bit `(bit-vector ,(car dims))) 1971 2541 (base-char `(base-string ,(car dims))) 1972 #|(character `(string ,(car dims)))|#2542 (character `(string ,(car dims))) 1973 2543 (t `(vector ,eltype ,(car dims))))) 1974 2544 (if (eq (car dims) '*) … … 1992 2562 (define-type-method (array :simple-subtypep) (type1 type2) 1993 2563 (let ((dims1 (array-ctype-dimensions type1)) 1994 (dims2 (array-ctype-dimensions type2)) 1995 (complexp2 (array-ctype-complexp type2))) 1996 ;; 1997 ;; See if dimensions are compatible. 1998 (cond ((not (or (eq dims2 '*) 1999 (and (not (eq dims1 '*)) 2000 (= (length dims1) (length dims2)) 2001 (every #'(lambda (x y) 2002 (or (eq y '*) (eql x y))) 2003 dims1 dims2)))) 2004 (values nil t)) 2005 ;; 2006 ;; See if complexp is compatible. 2007 ((not (or (eq complexp2 '*) 2008 (eq (array-ctype-complexp type1) complexp2))) 2009 (values nil t)) 2010 ;; 2011 ;; If the type2 eltype is wild, we win. Otherwise, the types must be 2012 ;; identical. 2013 ((or (eq (array-ctype-element-type type2) *wild-type*) 2014 (type= (specialized-element-type-maybe type1) 2015 (specialized-element-type-maybe type2))) 2016 (values t t)) 2017 (t 2018 (values nil t))))) 2564 (dims2 (array-ctype-dimensions type2)) 2565 (complexp2 (array-ctype-complexp type2))) 2566 (cond (;; not subtypep unless dimensions are compatible 2567 (not (or (eq dims2 '*) 2568 (and (not (eq dims1 '*)) 2569 (= (length (the list dims1)) 2570 (length (the list dims2))) 2571 (every (lambda (x y) 2572 (or (eq y '*) (eql x y))) 2573 (the list dims1) 2574 (the list dims2))))) 2575 (values nil t)) 2576 ;; not subtypep unless complexness is compatible 2577 ((not (or (eq complexp2 :maybe) 2578 (eq (array-ctype-complexp type1) complexp2))) 2579 (values nil t)) 2580 ;; Since we didn't fail any of the tests above, we win 2581 ;; if the TYPE2 element type is wild. 2582 ((eq (array-ctype-element-type type2) *wild-type*) 2583 (values t t)) 2584 (;; Since we didn't match any of the special cases above, we 2585 ;; can't give a good answer unless both the element types 2586 ;; have been defined. 2587 (or (unknown-ctype-p (array-ctype-element-type type1)) 2588 (unknown-ctype-p (array-ctype-element-type type2))) 2589 (values nil nil)) 2590 (;; Otherwise, the subtype relationship holds iff the 2591 ;; types are equal, and they're equal iff the specialized 2592 ;; element types are identical. 2593 t 2594 (values (type= (specialized-element-type-maybe type1) 2595 (specialized-element-type-maybe type2)) 2596 t))))) 2019 2597 2020 2598 ; (define-superclasses array (string string) (vector vector) (array)) … … 2024 2602 (declare (type array-ctype type1 type2)) 2025 2603 (let ((dims1 (array-ctype-dimensions type1)) 2026 (dims2 (array-ctype-dimensions type2)) 2027 (complexp1 (array-ctype-complexp type1)) 2028 (complexp2 (array-ctype-complexp type2))) 2029 ;; 2030 ;; See if dimensions are compatible. 2604 (dims2 (array-ctype-dimensions type2)) 2605 (complexp1 (array-ctype-complexp type1)) 2606 (complexp2 (array-ctype-complexp type2))) 2607 ;; See whether dimensions are compatible. 2031 2608 (cond ((not (or (eq dims1 '*) (eq dims2 '*) 2032 (and (= (length dims1) (length dims2)) 2033 (every #'(lambda (x y) 2034 (or (eq x '*) (eq y '*) (= x y))) 2035 dims1 dims2)))) 2036 (values nil t)) 2037 ;; 2038 ;; See if complexp is compatible. 2039 ((not (or (eq complexp1 '*) (eq complexp2 '*) 2040 (eq complexp1 complexp2))) 2041 (values nil t)) 2042 ;; 2043 ;; If either element type is wild, then they intersect. Otherwise, 2044 ;; the types must be identical. 2045 ((or (eq (array-ctype-element-type type1) *wild-type*) 2046 (eq (array-ctype-element-type type2) *wild-type*) 2047 (type= (specialized-element-type-maybe type1) 2048 (specialized-element-type-maybe type2))) 2049 2050 (values t t)) 2051 (t 2052 (values nil t))))) 2609 (and (= (length dims1) (length dims2)) 2610 (every (lambda (x y) 2611 (or (eq x '*) (eq y '*) (= x y))) 2612 dims1 dims2)))) 2613 (values nil t)) 2614 ;; See whether complexpness is compatible. 2615 ((not (or (eq complexp1 :maybe) 2616 (eq complexp2 :maybe) 2617 (eq complexp1 complexp2))) 2618 (values nil t)) 2619 ((or (eq (array-ctype-specialized-element-type type1) *wild-type*) 2620 (eq (array-ctype-specialized-element-type type2) *wild-type*) 2621 (type= (specialized-element-type-maybe type1) 2622 (specialized-element-type-maybe type2))) 2623 (values t t)) 2624 (t 2625 (values nil t))))) 2053 2626 2054 2627 (define-type-method (array :simple-intersection) (type1 type2) … … 2061 2634 (eltype1 (array-ctype-element-type type1)) 2062 2635 (eltype2 (array-ctype-element-type type2))) 2063 (values 2064 (specialize-array-type 2636 (specialize-array-type 2065 2637 (make-array-ctype 2066 2638 :dimensions (cond ((eq dims1 '*) dims2) 2067 ((eq dims2 '*) dims1)2068 (t2069 (mapcar #'(lambda (x y) (if (eq x '*) y x))2070 dims1 dims2)))2639 ((eq dims2 '*) dims1) 2640 (t 2641 (mapcar #'(lambda (x y) (if (eq x '*) y x)) 2642 dims1 dims2))) 2071 2643 :complexp (if (eq complexp1 '*) complexp2 complexp1) 2072 :element-type (if (eq eltype1 *wild-type*) eltype2 eltype1))) 2073 t)) 2074 (values *empty-type* t))) 2644 :element-type (if (eq eltype1 *wild-type*) eltype2 eltype1)))) 2645 *empty-type*)) 2075 2646 2076 2647 ;;; Check-Array-Dimensions -- Internal … … 2083 2654 (integer 2084 2655 (when (minusp dims) 2085 ( error "Arrays can't have a negative number of dimensions: ~D." dims))2656 (signal-program-error "Arrays can't have a negative number of dimensions: ~D." dims)) 2086 2657 (when (>= dims array-rank-limit) 2087 ( error "Array type has too many dimensions: ~S." dims))2658 (signal-program-error "Array type has too many dimensions: ~S." dims)) 2088 2659 (make-list dims :initial-element '*)) 2089 2660 (list 2090 2661 (when (>= (length dims) array-rank-limit) 2091 ( error "Array type has too many dimensions: ~S." dims))2662 (signal-program-error "Array type has too many dimensions: ~S." dims)) 2092 2663 (dolist (dim dims) 2093 2664 (unless (eq dim '*) 2094 2665 (unless (and (integerp dim) 2095 2666 (>= dim 0) (< dim array-dimension-limit)) 2096 ( error "Bad dimension in array type: ~S." dim))))2667 (signal-program-error "Bad dimension in array type: ~S." dim)))) 2097 2668 dims) 2098 2669 (t 2099 ( error "Array dimensions is not a list, integer or *:~% ~S"2100 dims))))2670 (signal-program-error "Array dimensions is not a list, integer or *:~% ~S" 2671 dims)))) 2101 2672 2102 2673 (def-type-translator array (&optional element-type dimensions) 2103 2674 (specialize-array-type 2104 2675 (make-array-ctype :dimensions (check-array-dimensions dimensions) 2105 :element-type (specifier-type element-type)))) 2676 :complexp :maybe 2677 :element-type (specifier-type element-type)))) 2106 2678 2107 2679 (def-type-translator simple-array (&optional element-type dimensions) … … 2148 2720 2149 2721 (define-type-method (member :unparse) (type) 2150 (let ((members (member-ctype-members type))) 2151 (if (equal members '(nil)) 2722 (if (type= type (specifier-type 'standard-char)) 2723 'standard-char 2724 (let ((members (member-ctype-members type))) 2725 (if (equal members '(nil)) 2152 2726 'null 2153 `(member ,@members)))) 2727 `(member ,@members))))) 2154 2728 2155 2729 (define-type-method (member :simple-subtypep) (type1 type2) … … 2159 2733 2160 2734 (define-type-method (member :complex-subtypep-arg1) (type1 type2) 2161 (block PUNT 2162 (values (every-type-op ctypep type2 (member-ctype-members type1) 2163 :list-first t) 2164 t))) 2735 (every/type (swapped-args-fun #'ctypep) 2736 type2 2737 (member-ctype-members type1))) 2165 2738 2166 2739 ;;; We punt if the odd type is enumerable and intersects with the member type. … … 2169 2742 (define-type-method (member :complex-subtypep-arg2) (type1 type2) 2170 2743 (cond ((not (ctype-enumerable type1)) (values nil t)) 2171 ((types-intersect type1 type2) (values nil nil)) 2744 ((types-intersect type1 type2) 2745 (invoke-complex-subtypep-arg1-method type1 type2)) 2172 2746 (t 2173 2747 (values nil t)))) … … 2175 2749 (define-type-method (member :simple-intersection) (type1 type2) 2176 2750 (let ((mem1 (member-ctype-members type1)) 2177 (mem2 (member-ctype-members type2)))2751 (mem2 (member-ctype-members type2))) 2178 2752 (values (cond ((subsetp mem1 mem2) type1) 2179 ((subsetp mem2 mem1) type2)2180 (t2181 (let ((res (intersection mem1 mem2)))2182 (if res2183 (make-member-ctype :members res)2184 *empty-type*))))2185 t)))2753 ((subsetp mem2 mem1) type2) 2754 (t 2755 (let ((res (intersection mem1 mem2))) 2756 (if res 2757 (make-member-ctype :members res) 2758 *empty-type*)))) 2759 t))) 2186 2760 2187 2761 (define-type-method (member :complex-intersection) (type1 type2) 2188 2762 (block PUNT 2189 ( let*((members))2763 (collect ((members)) 2190 2764 (let ((mem2 (member-ctype-members type2))) 2191 (dolist (member mem2) 2192 (multiple-value-bind (val win) 2193 (ctypep member type1) 2194 (unless win 2195 (return-from PUNT (values type2 nil))) 2196 (when val (push member members)))) 2197 2198 (values (cond ((subsetp mem2 members) type2) 2199 ((null members) *empty-type*) 2200 (t 2201 (make-member-ctype :members members))) 2202 t))))) 2203 2765 (dolist (member mem2) 2766 (multiple-value-bind (val win) (ctypep member type1) 2767 (unless win 2768 (return-from punt nil)) 2769 (when val (members member)))) 2770 (cond ((subsetp mem2 (members)) type2) 2771 ((null (members)) *empty-type*) 2772 (t 2773 (make-member-ctype :members (members)))))))) 2204 2774 2205 2775 ;;; We don't need a :COMPLEX-UNION, since the only interesting case is a union … … 2208 2778 (define-type-method (member :simple-union) (type1 type2) 2209 2779 (let ((mem1 (member-ctype-members type1)) 2210 (mem2 (member-ctype-members type2)))2780 (mem2 (member-ctype-members type2))) 2211 2781 (cond ((subsetp mem1 mem2) type2) 2212 ((subsetp mem2 mem1) type1)2213 (t2214 (make-member-ctype :members (union mem1 mem2))))))2782 ((subsetp mem2 mem1) type1) 2783 (t 2784 (make-member-ctype :members (union mem1 mem2)))))) 2215 2785 2216 2786 2217 2787 (define-type-method (member :simple-=) (type1 type2) 2218 2788 (let ((mem1 (member-ctype-members type1)) 2219 (mem2 (member-ctype-members type2)))2789 (mem2 (member-ctype-members type2))) 2220 2790 (values (and (subsetp mem1 mem2) (subsetp mem2 mem1)) 2221 t)))2791 t))) 2222 2792 2223 2793 (define-type-method (member :complex-=) (type1 type2) … … 2231 2801 2232 2802 (def-type-translator member (&rest members) 2233 (let ((mem (remove-duplicates members))) 2234 (if mem 2235 (make-member-ctype :members mem) 2236 *empty-type*))) 2803 (if members 2804 (collect ((non-numbers) (numbers)) 2805 (dolist (m (remove-duplicates members)) 2806 (if (and (numberp m) 2807 (not (and (floatp m) (zerop m)))) 2808 (numbers (ctype-of m)) 2809 (non-numbers m))) 2810 (apply #'type-union 2811 (if (non-numbers) 2812 (make-member-ctype :members (non-numbers)) 2813 *empty-type*) 2814 (numbers))) 2815 *empty-type*)) 2816 2237 2817 2238 2818 … … 2262 2842 (define-type-method (union :unparse) (type) 2263 2843 (declare (type ctype type)) 2264 (if (type= type (specifier-type 'list)) 2265 'list 2266 `(or ,@(mapcar #'type-specifier (union-ctype-types type))))) 2267 2268 2269 2270 ;;; Two union types are equal if every type in one is equal to some type in the 2271 ;;; other. 2272 ;;; 2844 (cond 2845 ((type= type (specifier-type 'list)) 'list) 2846 ((type= type (specifier-type 'float)) 'float) 2847 ((type= type (specifier-type 'real)) 'real) 2848 ((type= type (specifier-type 'sequence)) 'sequence) 2849 ((type= type (specifier-type 'bignum)) 'bignum) 2850 (t `(or ,@(mapcar #'type-specifier (union-ctype-types type)))))) 2851 2852 2853 2273 2854 (define-type-method (union :simple-=) (type1 type2) 2274 (block PUNT 2275 (let ((types1 (union-ctype-types type1)) 2276 (types2 (union-ctype-types type2))) 2277 (values (and (dolist (type1 types1 t) 2278 (unless (any-type-op type= type1 types2) 2279 (return nil))) 2280 (dolist (type2 types2 t) 2281 (unless (any-type-op type= type2 types1) 2282 (return nil)))) 2283 t)))) 2284 2285 2286 ;;; Similarly, a union type is a subtype of another if every element of Type1 2287 ;;; is a subtype of some element of Type2. 2288 ;;; 2855 (multiple-value-bind (subtype certain?) 2856 (csubtypep type1 type2) 2857 (if subtype 2858 (csubtypep type2 type1) 2859 (if certain? 2860 (values nil t) 2861 (multiple-value-bind (subtype certain?) 2862 (csubtypep type2 type1) 2863 (declare (ignore subtype)) 2864 (values nil certain?)))))) 2865 2866 2867 (define-type-method (union :complex-=) (type1 type2) 2868 (declare (ignore type1)) 2869 (if (some #'type-might-contain-other-types-p 2870 (union-ctype-types type2)) 2871 (values nil nil) 2872 (values nil t))) 2873 2874 2875 (defun union-simple-subtypep (type1 type2) 2876 (every/type (swapped-args-fun #'union-complex-subtypep-arg2) 2877 type2 2878 (union-ctype-types type1))) 2879 2289 2880 (define-type-method (union :simple-subtypep) (type1 type2) 2290 (block PUNT 2291 (let ((types2 (union-ctype-types type2))) 2292 (values (dolist (type1 (union-ctype-types type1) t) 2293 (unless (any-type-op csubtypep type1 types2) 2294 (return nil))) 2295 t)))) 2296 2881 (union-simple-subtypep type1 type2)) 2882 2883 (defun union-complex-subtypep-arg1 (type1 type2) 2884 (every/type (swapped-args-fun #'csubtypep) 2885 type2 2886 (union-ctype-types type1))) 2297 2887 2298 2888 (define-type-method (union :complex-subtypep-arg1) (type1 type2) 2299 (block PUNT 2300 (values (every-type-op csubtypep type2 (union-ctype-types type1) 2301 :list-first t) 2302 t))) 2889 (union-complex-subtypep-arg1 type1 type2)) 2890 2891 (defun union-complex-subtypep-arg2 (type1 type2) 2892 (multiple-value-bind (sub-value sub-certain?) 2893 (progn 2894 (assert (union-ctype-p type2)) 2895 (assert (not (union-ctype-p type1))) 2896 (type= type1 2897 (apply #'type-union 2898 (mapcar (lambda (x) (type-intersection type1 x)) 2899 (union-ctype-types type2))))) 2900 (if sub-certain? 2901 (values sub-value sub-certain?) 2902 (invoke-complex-subtypep-arg1-method type1 type2)))) 2303 2903 2304 2904 (define-type-method (union :complex-subtypep-arg2) (type1 type2) 2305 (block PUNT 2306 (values (any-type-op csubtypep type1 (union-ctype-types type2)) t))) 2307 2308 2309 (define-type-method (union :complex-union) (type1 type2) 2310 (let* ((class1 (ctype-class-info type1))) 2311 (let* ((res)) 2312 (let ((this-type type1)) 2313 (dolist (type (union-ctype-types type2) 2314 (if res 2315 (make-union-ctype (cons this-type (nreverse res))) 2316 this-type)) 2317 (cond ((eq (ctype-class-info type) class1) 2318 (let ((union (funcall (type-class-simple-union class1) 2319 this-type type))) 2320 (if union 2321 (setq this-type union) 2322 (push type res)))) 2323 ((csubtypep type this-type)) 2324 ((csubtypep type1 type) (return type2)) 2325 (t 2326 (push type res)))))))) 2327 2328 ;;; For the union of union types, we let the :COMPLEX-UNION method do the work. 2329 ;;; 2330 (define-type-method (union :simple-union) (type1 type2) 2331 (let ((res type1)) 2332 (dolist (t2 (union-ctype-types type2) res) 2333 (setq res (type-union res t2))))) 2334 2905 (union-complex-subtypep-arg2 type1 type2)) 2335 2906 2336 2907 (define-type-method (union :simple-intersection :complex-intersection) 2337 (type1 type2) 2338 (let ((res *empty-type*) 2339 (win t)) 2340 (dolist (type (union-ctype-types type2) (values res win)) 2341 (multiple-value-bind (int w) 2342 (type-intersection type1 type) 2343 (setq res (type-union res int)) 2344 (unless w (setq win nil)))))) 2345 2346 (def-type-translator or (&rest types) 2347 (reduce #'type-union 2348 (mapcar #'specifier-type types) 2349 :initial-value *empty-type*)) 2350 2351 ;;; We don't actually have intersection types, since the result of 2352 ;;; reasonable type intersections is always describable as a union of simple 2353 ;;; types. If something is too hairy to fit this mold, then we make a hairy 2354 ;;; type. 2355 2356 (def-type-translator and (&whole spec &rest types) 2357 (let ((res *wild-type*)) 2358 (dolist (type types res) 2359 (let ((ctype (specifier-type type))) 2360 (multiple-value-bind (int win) 2361 (type-intersection res ctype) 2362 (unless win 2363 (return (make-hairy-ctype :specifier spec))) 2364 (setq res int)))))) 2908 (type1 type2) 2909 (assert (union-ctype-p type2)) 2910 (cond ((and (union-ctype-p type1) 2911 (union-simple-subtypep type1 type2)) type1) 2912 ((and (union-ctype-p type1) 2913 (union-simple-subtypep type2 type1)) type2) 2914 ((and (not (union-ctype-p type1)) 2915 (union-complex-subtypep-arg2 type1 type2)) 2916 type1) 2917 ((and (not (union-ctype-p type1)) 2918 (union-complex-subtypep-arg1 type2 type1)) 2919 type2) 2920 (t 2921 (let ((accumulator *empty-type*)) 2922 (dolist (t2 (union-ctype-types type2) accumulator) 2923 (setf accumulator 2924 (type-union accumulator 2925 (type-intersection type1 t2)))))))) 2926 2927 2928 2929 (def-type-translator or (&rest type-specifiers) 2930 (apply #'type-union 2931 (mapcar #'specifier-type type-specifiers))) 2932 2933 2934 2935 ;;; Intersection types 2936 (defun make-intersection-ctype (enumerable types) 2937 (%istruct 'intersection-ctype 2938 (type-class-or-lose 'intersection) 2939 enumerable 2940 types)) 2941 2942 (defun intersection-ctype-p (x) 2943 (istruct-typep x 'intersection-ctype)) 2944 (setf (type-predicate 'intersection-ctype) 'intersection-ctype-p) 2945 2946 (define-type-method (intersection :unparse) (type) 2947 (declare (type ctype type)) 2948 (or (find type '(ratio keyword) :key #'specifier-type :test #'type=) 2949 `(and ,@(mapcar #'type-specifier (intersection-ctype-types type))))) 2950 2951 ;;; shared machinery for type equality: true if every type in the set 2952 ;;; TYPES1 matches a type in the set TYPES2 and vice versa 2953 (defun type=-set (types1 types2) 2954 (flet (;; true if every type in the set X matches a type in the set Y 2955 (type<=-set (x y) 2956 (declare (type list x y)) 2957 (every (lambda (xelement) 2958 (position xelement y :test #'type=)) 2959 x))) 2960 (values (and (type<=-set types1 types2) 2961 (type<=-set types2 types1)) 2962 t))) 2963 2964 (define-type-method (intersection :simple-=) (type1 type2) 2965 (type=-set (intersection-ctype-types type1) 2966 (intersection-ctype-types type2))) 2967 2968 (defun %intersection-complex-subtypep-arg1 (type1 type2) 2969 (type= type1 (type-intersection type1 type2))) 2970 2971 (defun %intersection-simple-subtypep (type1 type2) 2972 (every/type #'%intersection-complex-subtypep-arg1 2973 type1 2974 (intersection-ctype-types type2))) 2975 2976 (define-type-method (intersection :simple-subtypep) (type1 type2) 2977 (%intersection-simple-subtypep type1 type2)) 2978 2979 (define-type-method (intersection :complex-subtypep-arg1) (type1 type2) 2980 (%intersection-complex-subtypep-arg1 type1 type2)) 2981 2982 (defun %intersection-complex-subtypep-arg2 (type1 type2) 2983 (every/type #'csubtypep type1 (intersection-ctype-types type2))) 2984 2985 (define-type-method (intersection :complex-subtypep-arg2) (type1 type2) 2986 (%intersection-complex-subtypep-arg2 type1 type2)) 2987 2988 (define-type-method (intersection :simple-union :complex-union) 2989 (type1 type2) 2990 (assert (intersection-ctype-p type2)) 2991 (cond ((and (intersection-ctype-p type1) 2992 (%intersection-simple-subtypep type1 type2)) type2) 2993 ((and (intersection-ctype-p type1) 2994 (%intersection-simple-subtypep type2 type1)) type1) 2995 ((and (not (intersection-ctype-p type1)) 2996 (%intersection-complex-subtypep-arg2 type1 type2)) 2997 type2) 2998 ((and (not (intersection-ctype-p type1)) 2999 (%intersection-complex-subtypep-arg1 type2 type1)) 3000 type1) 3001 ((and (csubtypep type2 (specifier-type 'ratio)) 3002 (numeric-ctype-p type1) 3003 (csubtypep type1 (specifier-type 'integer)) 3004 (csubtypep type2 3005 (make-numeric-ctype 3006 :class 'rational 3007 :complexp nil 3008 :low (if (null (numeric-ctype-low type1)) 3009 nil 3010 (list (1- (numeric-ctype-low type1)))) 3011 :high (if (null (numeric-ctype-high type1)) 3012 nil 3013 (list (1+ (numeric-ctype-high type1))))))) 3014 (type-union type1 3015 (apply #'type-intersection 3016 (remove (specifier-type '(not integer)) 3017 (intersection-ctype-types type2) 3018 :test #'type=)))) 3019 (t 3020 (let ((accumulator *universal-type*)) 3021 (do ((t2s (intersection-ctype-types type2) (cdr t2s))) 3022 ((null t2s) accumulator) 3023 (let ((union (type-union type1 (car t2s)))) 3024 (when (union-ctype-p union) 3025 (if (and (eq accumulator *universal-type*) 3026 (null (cdr t2s))) 3027 (return union) 3028 (return nil))) 3029 (setf accumulator 3030 (type-intersection accumulator union)))))))) 3031 3032 (def-type-translator and (&rest type-specifiers) 3033 (apply #'type-intersection 3034 (mapcar #'specifier-type 3035 type-specifiers))) 3036 3037 ;;; cons-ctype 3038 (defun wild-ctype-to-universal-ctype (c) 3039 (if (type= c *wild-type*) 3040 *universal-type* 3041 c)) 3042 3043 (defun make-cons-ctype (car-ctype-value cdr-ctype-value) 3044 (if (or (eq car-ctype-value *empty-type*) 3045 (eq cdr-ctype-value *empty-type*)) 3046 *empty-type* 3047 (%istruct 'cons-ctype 3048 (type-class-or-lose 'cons) 3049 nil 3050 (wild-ctype-to-universal-ctype car-ctype-value) 3051 (wild-ctype-to-universal-ctype cdr-ctype-value)))) 3052 3053 (defun cons-ctype-p (x) 3054 (istruct-typep x 'cons-ctype)) 3055 3056 (setf (type-predicate 'cons-ctype) 'cons-ctype-p) 3057 3058 (def-type-translator cons (&optional (car-type-spec '*) (cdr-type-spec '*)) 3059 (make-cons-ctype (specifier-type car-type-spec) 3060 (specifier-type cdr-type-spec))) 3061 3062 (define-type-method (cons :unparse) (type) 3063 (let* ((car-spec (type-specifier (cons-ctype-car-ctype type))) 3064 (cdr-spec (type-specifier (cons-ctype-cdr-ctype type)))) 3065 (if (and (member car-spec '(t *)) 3066 (member cdr-spec '(t *))) 3067 'cons 3068 `(cons ,car-spec ,cdr-spec)))) 3069 3070 (define-type-method (cons :simple-=) (type1 type2) 3071 (declare (cons-ctype type1 type2)) 3072 (and (type= (cons-ctype-car-ctype type1) (cons-ctype-car-ctype type2)) 3073 (type= (cons-ctype-cdr-ctype type1) (cons-ctype-cdr-ctype type2)))) 3074 3075 (define-type-method (cons :simple-subtypep) (type1 type2) 3076 (declare (cons-ctype type1 type2)) 3077 (multiple-value-bind (val-car win-car) 3078 (csubtypep (cons-ctype-car-ctype type1) (cons-ctype-car-ctype type2)) 3079 (multiple-value-bind (val-cdr win-cdr) 3080 (csubtypep (cons-ctype-cdr-ctype type1) (cons-ctype-cdr-ctype type2)) 3081 (if (and val-car val-cdr) 3082 (values t (and win-car win-cdr)) 3083 (values nil (or win-car win-cdr)))))) 3084 3085 (define-type-method (cons :simple-union) (type1 type2) 3086 (declare (type cons-ctype type1 type2)) 3087 (let ((car-type1 (cons-ctype-car-ctype type1)) 3088 (car-type2 (cons-ctype-car-ctype type2)) 3089 (cdr-type1 (cons-ctype-cdr-ctype type1)) 3090 (cdr-type2 (cons-ctype-cdr-ctype type2))) 3091 (macrolet ((frob-car (car1 car2 cdr1 cdr2) 3092 `(type-union 3093 (make-cons-ctype ,car1 (type-union ,cdr1 ,cdr2)) 3094 (make-cons-ctype 3095 (type-intersection ,car2 3096 (specifier-type 3097 `(not ,(type-specifier ,car1)))) 3098 ,cdr2)))) 3099 (cond ((type= car-type1 car-type2) 3100 (make-cons-ctype car-type1 3101 (type-union cdr-type1 cdr-type2))) 3102 ((type= cdr-type1 cdr-type2) 3103 (make-cons-ctype (type-union car-type1 car-type2) 3104 cdr-type1)) 3105 ((csubtypep car-type1 car-type2) 3106 (frob-car car-type1 car-type2 cdr-type1 cdr-type2)) 3107 ((csubtypep car-type2 car-type1) 3108 (frob-car car-type2 car-type1 cdr-type2 cdr-type1)))))) 3109 3110 (define-type-method (cons :simple-intersection) (type1 type2) 3111 (declare (type cons-type type1 type2)) 3112 (let ((car-int2 (type-intersection2 (cons-ctype-car-ctype type1) 3113 (cons-ctype-car-ctype type2))) 3114 (cdr-int2 (type-intersection2 (cons-ctype-cdr-ctype type1) 3115 (cons-ctype-cdr-ctype type2)))) 3116 (cond ((and car-int2 cdr-int2) 3117 (make-cons-ctype car-int2 cdr-int2)) 3118 (car-int2 3119 (make-cons-ctype car-int2 3120 (type-intersection (cons-ctype-cdr-ctype type1) 3121 (cons-ctype-cdr-ctype type2)))) 3122 (cdr-int2 3123 (make-cons-ctype (type-intersection (cons-ctype-car-ctype type1) 3124 (cons-ctype-car-ctype type2)) 3125 cdr-int2))))) 3126 3127 3128 3129 ;;; An UNKNOWN-TYPE is a type not known to the type system (not yet defined). 3130 ;;; We make this distinction since we don't want to complain about types that 3131 ;;; are hairy but defined. 3132 ;;; 3133 3134 (defun make-unknown-ctype (&key specifier (enumerable t)) 3135 (%istruct 'unknown-ctype 3136 (type-class-or-lose 'hairy) 3137 enumerable 3138 specifier)) 3139 3140 (defun unknown-ctype-p (x) 3141 (istruct-typep x 'unknown-ctype)) 3142 3143 (setf (type-predicate 'unknown-ctype) 'unknown-ctype-p) 3144 3145 3146 3147 2365 3148 2366 3149 ;;;; foreign-type types … … 2449 3232 ((subclassp class2 class1) 2450 3233 (values type2 t)) 2451 (t (values 2452 (make-hairy-ctype :specifier `(and ,class1 ,class2) 2453 :enumerable nil) 2454 t))) 3234 (t (values nil t))) 2455 3235 (values nil t)))) 2456 3236 … … 2481 3261 (defun type-difference (x y) 2482 3262 (let ((x-types (if (union-ctype-p x) (union-ctype-types x) (list x))) 2483 (y-types (if (union-ctype-p y) (union-ctype-types y) (list y))))2484 ( let*((res))3263 (y-types (if (union-ctype-p y) (union-ctype-types y) (list y)))) 3264 (collect ((res)) 2485 3265 (dolist (x-type x-types) 2486 (if (member-ctype-p x-type)2487 ( let*((members))3266 (if (member-ctype-p x-type) 3267 (collect ((members)) 2488 3268 (dolist (mem (member-ctype-members x-type)) 2489 (multiple-value-bind (val win) 2490 (ctypep mem y) 2491 (unless win (return-from type-difference nil)) 2492 (unless val 2493 (push mem members)))) 2494 (when members 2495 (push (make-member-ctype :members (nreverse members)) res))) 2496 (dolist (y-type y-types (push x-type res)) 2497 (multiple-value-bind (val win) 2498 (csubtypep x-type y-type) 3269 (multiple-value-bind (val win) (ctypep mem y) 2499 3270 (unless win (return-from type-difference nil)) 2500 (when val (return)) 2501 (when (types-intersect x-type y-type) 2502 (return-from type-difference nil)))))) 2503 3271 (unless val 3272 (members mem)))) 3273 (when (members) 3274 (res (make-member-ctype :members (members))))) 3275 (dolist (y-type y-types (res x-type)) 3276 (multiple-value-bind (val win) (csubtypep x-type y-type) 3277 (unless win (return-from type-difference nil)) 3278 (when val (return)) 3279 (when (types-intersect x-type y-type) 3280 (return-from type-difference nil)))))) 2504 3281 (let ((y-mem (find-if #'member-ctype-p y-types))) 2505 (when y-mem 2506 (let ((members (member-ctype-members y-mem))) 2507 (dolist (x-type x-types) 2508 (unless (member-ctype-p x-type) 2509 (dolist (member members) 2510 (multiple-value-bind (val win) 2511 (ctypep member x-type) 2512 (when (or (not win) val) 2513 (return-from type-difference nil))))))))) 2514 (setq res (nreverse res)) 2515 (cond ((null res) *empty-type*) 2516 ((null (rest res)) (first res)) 2517 (t 2518 (make-union-ctype res)))))) 3282 (when y-mem 3283 (let ((members (member-ctype-members y-mem))) 3284 (dolist (x-type x-types) 3285 (unless (member-ctype-p x-type) 3286 (dolist (member members) 3287 (multiple-value-bind (val win) (ctypep member x-type) 3288 (when (or (not win) val) 3289 (return-from type-difference nil))))))))) 3290 (apply #'type-union (res))))) 2519 3291 2520 3292 ;;; CTypep -- Interface … … 2537 3309 ) 2538 3310 (union-ctype 2539 (dolist (mem (union-ctype-types type) (values nil t)) 2540 (multiple-value-bind (val win) 2541 (ctypep obj mem) 2542 (unless win (return (values nil nil))) 2543 (when val (return (values t t)))))) 3311 (any/type #'ctypep obj (union-ctype-types type))) 3312 (intersection-ctype 3313 (every/type #'ctypep obj (intersection-ctype-types type))) 2544 3314 (function-ctype 2545 3315 (values (functionp obj) t)) 2546 3316 (unknown-ctype 2547 3317 (values nil nil)) 2548 #|2549 3318 (foreign-ctype 2550 3319 (values (foreign-typep obj (foreign-ctype-foreign-type type)) t)) 2551 |# 3320 (negation-ctype 3321 (multiple-value-bind (res win) 3322 (ctypep obj (negation-ctype-type type)) 3323 (if win 3324 (values (not res) t) 3325 (values nil nil)))) 2552 3326 (hairy-ctype 2553 3327 ;; Now the tricky stuff. 2554 3328 (let* ((hairy-spec (hairy-ctype-specifier type)) 2555 (symbol (if (consp hairy-spec) (car hairy-spec) hairy-spec)))3329 (symbol (if (consp hairy-spec) (car hairy-spec) hairy-spec))) 2556 3330 (ecase symbol 2557 (and2558 (if (atom hairy-spec)2559 (values t t)2560 (dolist (spec (cdr hairy-spec) (values t t))2561 (multiple-value-bind (res win)2562 (ctypep obj (specifier-type spec))2563 (unless win (return (values nil nil)))2564 (unless res (return (values nil t)))))))2565 (not 3331 (and ; how would this get there ? 3332 (if (atom hairy-spec) 3333 (values t t) 3334 (dolist (spec (cdr hairy-spec) (values t t)) 3335 (multiple-value-bind (res win) 3336 (ctypep obj (specifier-type spec)) 3337 (unless win (return (values nil nil))) 3338 (unless res (return (values nil t))))))) 3339 (not ; how would this get there ? 2566 3340 (multiple-value-bind 2567 3341 (res win) 2568 (ctypep obj (specifier-type (cadr hairy-spec)))3342 (ctypep obj (specifier-type (cadr hairy-spec))) 2569 3343 (if win 2570 (values (not res) t)2571 (values nil nil))))3344 (values (not res) t) 3345 (values nil nil)))) 2572 3346 (satisfies 2573 3347 (let ((fun (second hairy-spec))) 2574 (cond ((and (consp fun) (eq (car fun) 'lambda)) 2575 (values (not (null (funcall (coerce fun 'function) obj))) 2576 t)) 2577 ((and (symbolp fun) (fboundp fun)) 2578 (values (not (null (funcall fun obj))) t)) 2579 (t 2580 (values nil nil)))))))))) 3348 (cond ((and (symbolp fun) (fboundp fun)) 3349 (values (not (null (ignore-errors (funcall fun obj)))) t)) 3350 (t 3351 (values nil nil)))))))))) 2581 3352 2582 3353 ;;; %TYPEP -- internal. … … 2589 3360 (%%typep object 2590 3361 (if (typep specifier 'ctype) 2591 specifier2592 (specifier-type specifier))))3362 specifier 3363 (specifier-type specifier)))) 2593 3364 2594 3365 … … 2641 3412 ((t) (not (typep object 'simple-array))) 2642 3413 ((nil) (typep object 'simple-array)) 2643 ( *t))3414 ((* :maybe) t)) 2644 3415 (or (eq (array-ctype-dimensions type) '*) 2645 3416 (do ((want (array-ctype-dimensions type) (cdr want)) … … 2661 3432 (when (%%typep object type) 2662 3433 (return t)))) 3434 (intersection-ctype 3435 (every (lambda (type) (%%typep object type)) 3436 (intersection-ctype-types type))) 2663 3437 (cons-ctype 2664 3438 (and (consp object) … … 2672 3446 (unknown-ctype-specifier reparse)) 2673 3447 (%%typep object reparse)))) 3448 (negation-ctype 3449 (not (%%typep object (negation-ctype-type type)))) 2674 3450 (hairy-ctype 2675 3451 ;; Now the tricky stuff. … … 2715 3491 ;;; 2716 3492 3493 (defun float-format-name (x) 3494 (declare (float x)) 3495 (etypecase x 3496 (single-float "SINGLE-FLOAT") 3497 (double-float "DOUBLE-FLOAT"))) 3498 3499 (defun ctype-of-number (x) 3500 (let ((num (if (complexp x) (realpart x) x))) 3501 (multiple-value-bind (complexp low high) 3502 (if (complexp x) 3503 (let ((imag (imagpart x))) 3504 (values :complex (min num imag) (max num imag))) 3505 (values :real num num)) 3506 (make-numeric-ctype :class (etypecase num 3507 (integer 'integer) 3508 (rational 'rational) 3509 (float 'float)) 3510 :format (and (floatp num) (float-format-name num)) 3511 :complexp complexp 3512 :low low 3513 :high high)))) 3514 2717 3515 (defun ctype-of (x) 2718 3516 (typecase x 2719 (function (specifier-type 'function)) 3517 (function (specifier-type 'function)) ; GFs .. 2720 3518 (symbol 2721 3519 (make-member-ctype :members (list x))) 2722 (number 2723 (let* ((num (if (complexp x) (realpart x) x)) 2724 (res (make-numeric-ctype 2725 :class (etypecase num 2726 (integer 'integer) 2727 (rational 'rational) 2728 (float 'float)) 2729 :format (if (floatp num) 2730 (if (typep x 'short-float) 2731 'short-float 2732 'double-float) 2733 nil)))) 2734 (cond ((complexp x) 2735 (setf (numeric-ctype-complexp res) :complex) 2736 (let ((imag (imagpart x))) 2737 (setf (numeric-ctype-low res) (min num imag)) 2738 (setf (numeric-ctype-high res) (max num imag)))) 2739 (t 2740 (setf (numeric-ctype-low res) num) 2741 (setf (numeric-ctype-high res) num))) 2742 res)) 3520 (number (ctype-of-number x)) 2743 3521 (array 2744 3522 (let ((etype (specifier-type (array-element-type x)))) 2745 3523 (make-array-ctype :dimensions (array-dimensions x) 2746 :complexp (not (typep x 'simple-array))2747 :element-type etype2748 :specialized-element-type etype)))3524 :complexp (not (typep x 'simple-array)) 3525 :element-type etype 3526 :specialized-element-type etype))) 2749 3527 (t 2750 3528 (%class.ctype (class-of x))))) … … 2769 3547 `(integer ,(- bound) ,(1- bound)))) 2770 3548 (t 2771 ( error "Bad size specified for SIGNED-BYTE type specifier: ~S." s))))3549 (signal-program-error "Bad size specified for SIGNED-BYTE type specifier: ~S." s)))) 2772 3550 2773 3551 (deftype unsigned-byte (&optional s) … … 2933 3711 (unsigned-byte 8) (unsigned-byte 16) (unsigned-byte 32) 2934 3712 (signed-byte 8) (signed-byte 16) (signed-byte 32) 2935 ;would be nice to add these on demand vs ad hoc ly2936 ; and isn't or function symbol = or symbol function2937 3713 (or function symbol) 2938 (NOT NUMBER) 2939 (AND (NOT NUMBER) (NOT MACPTR)) 2940 (OR FIXNUM CHARACTER SYMBOL (AND (NOT NUMBER) (NOT MACPTR))))) 3714 )) 2941 3715 2942 3716 (precompute-types *cl-types*)
Note:
See TracChangeset
for help on using the changeset viewer.
