-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray.lisp
More file actions
126 lines (120 loc) · 4.13 KB
/
array.lisp
File metadata and controls
126 lines (120 loc) · 4.13 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
(in-package #:sandalphon.types)
(defclass array-type (type)
(;; upgraded
(element-type :initarg :element-type
:accessor array-type-element-type)
;; normalized to a list
(dimensions :initarg :dimensions
:accessor array-type-dimensions)
(simplicity :initarg :simple :accessor array-type-simple-p)))
(defun array-type-dimension-< (d1 d2)
(or (eq d2 '*)
(and (not (eq d2 '*) d1) (= d1 d2))))
(defmethod typep (obj (type array-type))
(and (if (array-type-simple-p type)
;; this is implementation dependent per
;; clhs simple-array.
;; the following conditions make our simple-array,
;; at worst, a strict subset of the implementation's.
(not (or (array-has-fill-pointer-p obj)
(array-displacement obj)
(adjustable-array-p obj)))
t)
(if (eq (array-type-dimensions type) '*)
t ; any dims are ok
(and (= (array-rank obj)
(length (array-type-dimensions type)))
(every #'array-type-dimension-<
(array-dimensions obj)
(array-type-dimensions type))))
(if (eq (array-type-element-type type) '*)
t
;; use of parse-type this early is gross but apparently
;; necessary.
;; type= is trivalent, so that's more gross,
;; but probably won't come up much in practice
(values (type= (parse-type (array-element-type type))
(array-type-element-type type))))))
(defmethod subtypep tri/combine ((t1 array-type) (t2 array-type))
(when (and (array-type-simple-p t2)
(not (array-type-simple-p t1)))
(return-from subtypep (values nil t)))
(unless (eq (array-type-dimensions t2) '*)
(when (eq (array-type-dimensions t1) '*)
(return-from subtypep (values nil t)))
(unless (= (length (array-type-dimensions t1))
(length (array-type-dimensions t2)))
(return-from subtypep (values nil t)))
(unless (every #'array-type-dimension-<
(array-type-dimensions t1)
(array-type-dimensions t2))
(return-from subtypep (values nil t))))
(cond ((eq (array-type-element-type t2) '*)
(values t t))
((eq (array-type-element-type t1) '*)
(values nil t))
(t (type= (array-type-element-type t1)
(array-type-element-type t2)))))
(defmethod conjoin/2 ((t1 array-type) (t2 array-type))
(multiple-value-bind (result certainty)
(type= (array-type-element-type t1) (array-type-element-type t2))
(cond (result
(let ((dims
(cond ((eq (array-type-dimensions t2) '*)
(array-type-dimensions t1))
((eq (array-type-dimensions t1) '*)
(array-type-dimensions t2))
(t
(if (= (length (array-type-dimensions t1))
(length
(array-type-dimensions t2)))
(mapcar
(lambda (d1 d2)
(cond ((eq d1 '*) d2)
((eq d2 '*) d1)
((= d1 d2) d1)
(t
(return-from conjoin/2
*the-type-nil*))))
(array-type-dimensions t1)
(array-type-dimensions t2))
(return-from conjoin/2
*the-type-nil*))))))
(make-instance 'array-type
:element-type
(array-type-element-type t1)
:dimensions dims
:simple (and
(array-type-simple-p t1)
(array-type-simple-p t2)))))
(certainty *the-type-nil*)
;; I think the best thing to do on uncertainty on the type= is punt.
(t (call-next-method)))))
(defmethod disjoin/2 ((t1 array-type) (t2 array-type))
(multiple-value-bind (result certainty)
(type= (array-type-element-type t1)
(array-type-element-type t2))
(if result
(let ((dims
(cond ((eq (array-type-dimensions t2) '*) '*)
((eq (array-type-dimensions t1) '*) '*)
(t (if (= (length (array-type-dimensions t1))
(length (array-type-dimensions t2)))
(mapcar (lambda (d1 d2)
(cond
((eq d1 '*) d1)
((eq d2 '*) d2)
((= d1 d2) d1)
(t
(return-from disjoin/2
(call-next-method)))))
(array-type-dimensions t1)
(array-type-dimensions t2))
(return-from disjoin/2
(call-next-method)))))))
(make-instance 'array-type
:element-type (array-type-element-type t1)
:dimensions dims
:simple (or (array-type-simple-p t1)
(array-type-simple-p t2))))
(call-next-method))))