blob: 91c8ac94970bef90263ef0966107b7fb2b610583 (
plain)
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
|
;;; -*- mode: lisp; coding: utf-8 -*-
;;; Étoilisp/Mulklisp, a Common Lisp subset for the Étoilé runtime.
;;; Copyright (C) 2008 Matthias Andreas Benkard.
;;;
;;; This program is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
(in-package #:common-lisp)
(export '(most-positive-fixnum most-negative-fixnum type-of typep
subtypep and or not satisfies symbol fixnum bignum real
complex float integer ratio rational number short-float
long-float single-float double-float null symbol list cons
standard-char base-char extended-char string vector bit-vector
array simple-array simple-vector simple-string
simple-bit-vector sequence two-way-stream stream echo-stream
broadcast-stream file-stream synonym-stream string-stream
concatenated-stream deftype))
(setq *subtype-supertypes-dict*
(let ((relationship-alist
'((base-char . (character))
(bignum . (integer))
(bit-vector . (vector))
(broadcast-stream . (stream))
(complex . (number))
(concatenated-stream . (stream))
(cons . (list))
(double-float . (float))
(echo-stream . (stream))
(extended-char . (character))
(file-stream . (stream))
(fixnum . (integer))
(float . (real))
(integer . (rational))
(list . (sequence))
(long-float . (float))
(null . (list symbol))
(ratio . (rational))
(rational . (real))
(real . (number))
(short-float . (float))
(simple-array . (array))
(simple-bit-vector . (simple-array))
(simple-string . (simple-array))
(simple-vector . (simple-array))
(single-float . (float))
(standard-char . (base-char))
(string . (vector))
(string-stream . (stream))
(synonym-stream . (stream))
(two-way-stream . (stream))
(vector . (array sequence))))
(dict (send-by-name (find-objc-class "NSMutableDictionary")
"dictionary")))
(dolist (pair relationship-alist dict)
(send-by-name dict "setObject:forKey:" (cdr pair) (nullify (car pair))))))
(setq *type-expanders* (send-by-name (find-objc-class "NSMutableDictionary")
"dictionary"))
(setq most-positive-fixnum 32767)
(setq most-negative-fixnum -32768)
(defun type-of (thing)
(let ((primitive-type (primitive-type-of thing)))
(case primitive-type
((null symbol cons single-float double-float function package)
primitive-type)
(integer
(if (and (send-by-name -1 "isEqual:" (send-by-name thing
"compare:"
most-positive-fixnum))
(send-by-name -1 "isEqual:" (send-by-name most-negative-fixnum
"compare:"
thing)))
'fixnum
'bignum))
(base-char 'base-char) ;FIXME
(sys::lexical-context 'sys::lexical-context)
(sys::binding 'sys::binding)
(stream 'stream) ;FIXME
(sys::exception 'sys::exception)
(array 'array) ;FIXME
(otherwise t)))) ;FIXME: classes and struct types, DEFTYPE
(defun some1 (function list)
(and (not (null list))
(or (funcall function (first list))
(some1 function (rest list)))))
(defun every1 (function list)
(or (null list)
(and (funcall function (first list))
(every1 function (rest list)))))
(defmacro deftype (type-name lambda-list &body body)
`(send-by-name *type-expanders*
"setObject:forKey:"
(destructuring-lambda ,lambda-list ,@body)
',type-name))
(defun expand-type (type &optional environment)
(let* ((env (if rest (car rest) nil))
(expansion-1 (expand-type-1 object env))
(expansion-2 (expand-type-1 expansion-1 env)))
(if (list-eqp expansion-1 expansion-2)
expansion-1
(expand-type expansion-2))))
(defun expand-type-1 (type &optional environment)
(let ((expander (send-by-name *type-expanders* "objectForKey:" (first type))))
(if expander
(apply expander (rest type))
type)))
(defun typep (thing typespec &optional environment)
;;FIXME: DEFTYPE
(let ((type (type-of thing))
(typespec (expand-type typespec environment)))
(cond ((eq typespec t) t)
((consp typespec)
(case (first typespec)
(and (every1 (lambda (x) (typep thing x environment)) (rest typespec)))
(or (some1 (lambda (x) (typep thing x environment)) (rest typespec)))
(not (not (typep thing (second typespec) environment)))
(satisfies (funcall (second typespec) thing))
(otherwise
(subtypep type typespec environment))))
(t (subtypep type typespec environment)))))
(defun subtypep (type1 type2 &optional environment)
(let ((type1 (expand-type type1 environment))
(type2 (expand-type type2 environment)))
(cond ((eq type2 t) t)
((eq type1 nil) t)
((consp type1)
(case (first type1)
(and (some1 (lambda (x) (subtypep x type2 environment)) (rest type1)))
(or (every1 (lambda (x) (subtypep x type2 environment)) (rest type1)))
(not (not (subtypep (second type1) type2 environment)))
(satisfies nil) ;FIXME?
(otherwise
;;FIXME!!
(subtypep (first type1) type2 environment))))
((consp type2)
(case (first type2)
(and (every1 (lambda (x) (subtypep type1 x environment)) (rest type2)))
(or (some1 (lambda (x) (subtypep type1 x environment)) (rest type2)))
(not nil) ;FIXME
(satisfies nil) ;FIXME?
(otherwise
;;FIXME?
(subtypep type1 (first type2) environment))))
(t (or (eq type1 type2)
(let ((supertypes (send-by-name *subtype-supertypes-dict*
"objectForKey:"
type1))) ;strictly, this should be (nullify type1),
;but type1 can't be NIL here
(some1 (lambda (x) (subtypep x type2 environment)) supertypes)))))))
|