Repository navigation
Expand file tree
/
Copy pathpairwise.lisp
More file actions
238 lines (219 loc) · 9.65 KB
/
Copy pathpairwise.lisp
File metadata and controls
238 lines (219 loc) · 9.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
(in-package #:ctype)
;;;; Pair methods
;;;; That is, methods on two specific ctype classes.
;;; cclass ctypes are excluded from several other ctypes (when things are
;;; normalized correctly), so we can mark their conjunctions as empty, etc.
(defmacro defexclusive/2 (class1 class2)
`(progn
(define-commutative-method subctypep (client (ct1 ,class1) (ct2 ,class2))
(declare (ignore client))
(values nil t))
(define-commutative-method disjointp (client (ct1 ,class1) (ct2 ,class2))
(declare (ignore client))
(values t t))
(define-commutative-method conjointp (client (ct1 ,class1) (ct2 ,class2))
(declare (ignore client))
(values nil t))))
(defmacro defexclusive (&rest classes)
`(progn
,@(loop for (class1 . rest) on classes
nconc (loop for class2 in rest
collect `(defexclusive/2 ,class1 ,class2)))))
(defmacro defexclusives (main &rest classes)
`(progn ,@(loop for class in classes
collect `(defexclusive ,main ,class))))
(defexclusive range ccomplex carray charset cfunction)
(defexclusives cclass carray range ccomplex charset)
(defexclusives fpzero cmember ccomplex carray charset cfunction)
(macrolet ((consxclusive/1 (class)
`(progn
(defmethod subctypep (client (ct1 ccons) (ct2 ,class))
;; ct1 and ct2 are basically exclusive, so if ct1 is
;; definitely NOT bottom, they really are exclusive.
;; That's why this is different from the general method above.
(emptyp client ct1))
(defmethod subctypep (client (ct1 ,class) (ct2 ccons))
(declare (ignore client))
(values nil t))
(define-commutative-method disjointp
(client (ct1 ccons) (ct2 ,class))
(declare (ignore client))
(values t t))))
(consxclusive (&rest classes)
`(progn ,@(loop for class in classes
collect `(consxclusive/1 ,class)))))
(consxclusive range ccomplex carray cclass charset cfunction fpzero))
(macrolet ((defnonconjoint/2 (c1 c2)
`(define-commutative-method conjointp (client (ct1 ,c1) (ct2 ,c2))
(declare (ignore client))
(values nil t)))
(defnonconjoint (&rest classes)
`(progn
,@(loop for (class1 . rest) on classes
nconc (loop for class2 in rest
collect `(defnonconjoint/2
,class1 ,class2))))))
(defnonconjoint cclass ccons range fpzero ccomplex cmember carray
charset cfunction))
;;; CONS is a subclass of SEQUENCE. Therefore, all CONS types are subtypes of
;;; SEQUENCE, regardless of whether they actually describe proper sequences,
;;; and even if they don't.
(defmethod subctypep (client (ct1 ccons) (ct2 csequence))
(declare (ignore client))
(values t t))
(defmethod subctypep (client (ct1 csequence) (ct2 ccons))
(declare (ignore client))
(values nil t))
(define-commutative-method disjointp (client (ct1 ccons) (ct2 csequence))
(declare (ignore client))
(values nil t))
(define-commutative-method conjoin/2 (client (ct1 csequence) (ct2 ccons))
(declare (ignore client))
ct2)
(define-commutative-method disjoin/2 (client (ct1 csequence) (ct2 ccons))
(declare (ignore client))
ct1)
(defmethod subtract (client (ct1 ccons) (ct2 csequence))
(declare (ignore client))
(bot))
;;; NULL is (MEMBER NIL), and cmember methods should already handle things.
(defmethod subctypep (client (ct1 carray) (ct2 csequence))
(declare (ignore client))
(values (let ((dims (carray-dims ct1)))
(and (listp dims) (= (length dims) 1)))
t))
(defmethod subctypep (client (ct1 cclass) (ct2 carray))
(declare (ignore client))
(values nil t))
(define-commutative-method disjointp (client (ct1 carray) (ct2 csequence))
(declare (ignore client))
(values (not (let ((dims (carray-dims ct1)))
(or (eq dims '*) (= (length dims) 1))))
t))
(define-commutative-method conjoin/2 (client (cs csequence) (carray carray))
(declare (ignore client))
(let ((dims (carray-dims carray)))
(cond ((eq dims '*)
(carray (carray-simplicity carray)
(carray-uaet carray) (carray-eaet carray)
'(*)))
((= (length dims) 1) carray)
(t (bot)))))
(defmethod subtract (client (ct1 csequence) (ct2 carray))
(declare (ignore client))
(let ((dims (carray-dims ct2)))
(if (or (eq dims '*) (= (length dims) 1))
nil
ct1)))
(defmethod subtract (client (ct1 carray) (ct2 csequence))
(declare (ignore client))
(let ((dims (carray-dims ct1)))
(cond ((eq dims '*) nil)
((= (length dims) 1) (bot))
(t ct1))))
(defun subfunction-cclass-p (client cclass)
;; FIXME: We skip the env here, is that okay?
(subclassp client (cclass-class cclass) (find-class client 'function t)))
(defmethod subctypep (client (ct1 cfunction) (ct2 cclass))
(declare (ignore client))
;; FUNCTION itself is never a cclass, so
(values nil t))
(defmethod subctypep (client (ct1 cclass) (ct2 cfunction))
(if (subfunction-cclass-p client ct1)
(if (function-top-p ct2) (values t t) (values nil nil))
(values nil t)))
(define-commutative-method conjoin/2 (client (ct1 cclass) (ct2 cfunction))
(if (subfunction-cclass-p client ct1)
(if (function-top-p ct2) ct1 nil)
(bot)))
(defmethod subtract (client (ct1 cclass) (ct2 cfunction))
(if (subfunction-cclass-p client ct1)
(if (function-top-p ct2) (bot) nil)
ct1))
(defmethod subtract (client (ct1 cfunction) (ct2 cclass))
(if (subfunction-cclass-p client ct2)
nil
ct1))
;;; We normalize characters out of member types, so members never contain
;;; characters. charsets are not infinite, though.
;;; Reals are also normalized out.
(defexclusives cmember charset range)
;;; Resolve some (subtypep '(not X) '(member ...)) questions negatively.
(defmethod subctypep (client (ct1 negation) (ct2 cmember))
(multiple-value-bind (cofinitep surety)
(cofinitep client (negation-ctype ct1))
(if (and surety (not cofinitep))
(values nil t)
(values nil nil))))
;;; These methods exist so that disjoin-cmember doesn't produce nested
;;; disjunctions, e.g. from (or boolean list) => (or (eql t) (or cons null))
(define-commutative-method disjoin/2 (client (cmember cmember) (disjunction disjunction))
(let* ((scts (junction-ctypes disjunction))
(non (loop for elem in (cmember-members cmember)
unless (loop for sct in scts
thereis (ctypep client elem sct))
collect elem)))
;; We use disjoin instead of creating a disjunction in case one of our
;; disjunction ctypes is another cmember to be merged.
;; Inefficient? Probably.
(apply #'disjoin client (apply #'cmember non) scts)))
;;; Deal with fpzeros and ranges.
(defmethod subctypep (client (ct1 fpzero) (ct2 range))
(values (ctypep client (fpzero-zero ct1) ct2) t))
(defmethod subctypep (client (ct1 range) (ct2 fpzero))
(declare (ignore client))
(values nil t))
(define-commutative-method disjointp (client (ct1 fpzero) (ct2 range))
(values (not (ctypep client (fpzero-zero ct1) ct2)) t))
(define-commutative-method conjoin/2 (client (ct1 fpzero) (ct2 range))
(if (ctypep client (fpzero-zero ct1) ct2)
ct1
(bot)))
(define-commutative-method disjoin/2 (client (ct1 fpzero) (ct2 range))
(if (ctypep client (fpzero-zero ct1) ct2)
ct2
nil))
(defmethod subtract (client (ct1 fpzero) (ct2 range))
(if (ctypep client (fpzero-zero ct1) ct2)
(bot)
ct1))
(defmethod subtract (client (ct1 range) (ct2 fpzero))
(let ((zero (fpzero-zero ct2)))
(if (ctypep client zero ct1)
;; Here's the pain.
(let ((k (range-kind ct1))
(low (range-low ct1)) (lxp (range-low-exclusive-p ct1))
(high (range-high ct1)) (hxp (range-high-exclusive-p ct1)))
(cond ((and low (= zero low))
(disjunction (fpzero k (- zero))
(range k low t high hxp)))
((and high (= zero high))
(disjunction (fpzero k (- zero))
(range k low lxp high t)))
(t
(disjunction (fpzero k (- zero))
(range k low lxp zero t)
(range k zero t high hxp)))))
nil)))
;;; dumb KLUDGE: the standard requires that (subtypep 'keyword nil)
;;; and (subtypep 'compiled-function nil) give valid answers (i.e., false)
;;; but there's no natural way to represent these types.
;;; We use SATISFIES, which is hard to do anything with.
;;; Here we special case some SATISFIES disjointp queries so that these two
;;; types are known to be above NIL. I think this is the last bad way to do it.
;;; Note that we do _not_ define subctypep for the admittedly dumb reason that
;;; (and function (satisfies ...)) is easier to work with in some ways than
;;; (satisfies compiled-function-p) would be, and ditto keywordp.
(define-commutative-method disjointp (client (class1 csatisfies) (class2 cclass))
(declare (ignore client))
(if (and (eq (csatisfies-fname class1) 'keywordp)
(eq (class-name (cclass-class class2)) 'symbol))
(values nil t)
(values nil nil)))
(define-commutative-method disjointp
(client (class1 csatisfies) (class2 cfunction))
(declare (ignore client))
(if (and (eq (csatisfies-fname class1) 'compiled-function-p)
(function-top-p class2))
(values nil t)
(values nil nil)))