-
Notifications
You must be signed in to change notification settings - Fork 0
/
type-check-gradual.rkt
358 lines (329 loc) · 13.4 KB
/
type-check-gradual.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#lang racket
(require "utilities.rkt")
(require "type-check-Lany.rkt")
(require "type-check-Cany.rkt")
(provide type-check-gradual type-check-gradual-class type-check-gradual-mixin
type-check-Lany-proxy type-check-Lany-proxy-class
type-check-Cany-proxy type-check-Cany-proxy-class
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (type-check-gradual-mixin super-class)
(class super-class
(super-new)
(define/public (join t1 t2)
(match* (t1 t2)
[('Integer 'Integer) 'Integer]
[('Boolean 'Boolean) 'Boolean]
[('Void 'Void) 'Void]
[('Any t2) t2]
[(t1 'Any) t1]
[(`(Vector ,ts1 ...) `(Vector ,ts2 ...))
`(Vector ,@(for/list ([t1 ts1] [t2 ts2]) (join t1 t2)))]
[(`(Vectorof ,t1) `(Vectorof ,t2))
`(Vectorof ,(join t1 t2))]
[(`(,ts1 ... -> ,rt1) `(,ts2 ... -> ,rt2))
`(,@(for/list ([t1 ts1] [t2 ts2]) (join t1 t2))
-> ,(join rt1 rt2))]
[(other wise) (error 'join "unhandled case types ~a ~a" t1 t2)]
))
(define/public (meet t1 t2)
(match* (t1 t2)
[('Integer 'Integer) 'Integer]
[('Boolean 'Boolean) 'Boolean]
[('Void 'Void) 'Void]
[('Any t2) 'Any]
[(t1 'Any) 'Any]
[(`(Vector ,ts1 ...) `(Vector ,ts2 ...))
`(Vector ,@(for/list ([t1 ts1] [t2 ts2]) (meet t1 t2)))]
[(`(Vectorof ,t1) `(Vectorof ,t2))
`(Vectorof ,(meet t1 t2))]
[(`(,ts1 ... -> ,rt1) `(,ts2 ... -> ,rt2))
`(,@(for/list ([t1 ts1] [t2 ts2]) (meet t1 t2))
-> ,(meet rt1 rt2))]
[(other wise) (error 'meet "unhandled case types ~a ~a" t1 t2)]
))
))
(define type-check-gradual-class
(class (type-check-gradual-mixin type-check-Lany-class)
(super-new)
(inherit operator-types type-predicates join meet)
(define/override (fun-def-type d)
(match d
[(Def f params rt info body)
(debug 'fun-def-type "parameters:" params)
(define ps
(for/list ([p params])
(match p
[`[,x : ,T] T]
[(? symbol?) 'Any]
[else (error 'fun-def-type "unmatched parameter ~a" p)])))
`(,@ps -> ,rt)]
[else
(error 'fun-def-type "ill-formed function definition in ~a" d)]))
(define/public (consistent? t1 t2)
(match* (t1 t2)
[('Integer 'Integer) #t]
[('Boolean 'Boolean) #t]
[('Void 'Void) #t]
[('Any t2) #t]
[(t1 'Any) #t]
[(`(Vector ,ts1 ...) `(Vector ,ts2 ...))
(for/and ([t1 ts1] [t2 ts2]) (consistent? t1 t2))]
[(`(Vectorof ,t1) `(Vectorof ,t2))
(consistent? t1 t2)]
[(`(,ts1 ... -> ,rt1) `(,ts2 ... -> ,rt2))
(and (for/and ([t1 ts1] [t2 ts2]) (consistent? t1 t2))
(consistent? rt1 rt2))]
[(other wise) #f]))
(define/public (check-consistent? t1 t2 e)
(unless (consistent? t1 t2)
(error 'type-check "~a is inconsistent with ~a\nin ~v" t1 t2 e)))
;; Override type-check-op to check for consistency instead of equality.
(define/override (type-check-op op arg-types e)
(match (dict-ref (operator-types) op)
[`(,param-types . ,return-type)
(for ([at arg-types] [pt param-types])
(check-consistent? at pt e))
return-type]
[else (error 'type-check-op "unrecognized ~a" op)]))
;; These primitive operators are handled explicitly in the
;; type checkers, so don't use type-check-op on them.
(define explicit-prim-ops
(set-union
(type-predicates)
(set 'procedure-arity 'eq? 'and 'or
'vector 'vector-length 'vector-ref 'vector-set!
'make-vector
'any-vector-length 'any-vector-ref 'any-vector-set!)))
(define/override (type-check-exp env)
(lambda (e)
(verbose "gradual/type-check-exp" e)
(define recur (type-check-exp env))
(match e
;; Lvar
[(Prim op es)
#:when (not (set-member? explicit-prim-ops op))
(define-values (new-es ts)
(for/lists (exprs types) ([e es])
(recur e)))
(define t-ret (type-check-op op ts e))
(values (Prim op new-es) t-ret)]
;; Lif
[(Prim 'eq? (list e1 e2))
(define-values (e1^ t1) (recur e1))
(define-values (e2^ t2) (recur e2))
(check-consistent? t1 t2 e)
(define T (meet t1 t2))
(values (Prim 'eq? (list e1^ e2^)) 'Boolean)]
[(Prim 'and (list e1 e2))
(recur (If e1 e2 (Bool #f)))]
[(Prim 'or (list e1 e2))
(define tmp (gensym 'tmp))
(recur (Let tmp e1 (If (Var tmp) (Var tmp) e2)))]
[(If e1 e2 e3)
(define-values (e1^ T1) (recur e1))
(define-values (e2^ T2) (recur e2))
(define-values (e3^ T3) (recur e3))
(check-consistent? T1 'Boolean e)
(check-consistent? T2 T3 e)
(define Tif (meet T2 T3))
(values (If e1^ e2^ e3^) Tif)]
;; Lwhile
[(SetBang x e1)
(define-values (e1^ T1) (recur e1))
(define varT (dict-ref env x))
(check-consistent? T1 varT e)
(values (SetBang x e1^) 'Void)]
[(WhileLoop e1 e2)
(define-values (e1^ T1) (recur e1))
(check-consistent? T1 'Boolean e)
(define-values (e2^ T2) ((type-check-exp env) e2))
(values (WhileLoop e1^ e2^) 'Void)]
;; Lvec
[(Prim 'vector-length (list e1))
(define-values (e1^ t) (recur e1))
(match t
[`(Vector ,ts ...)
(values (Prim 'vector-length (list e1^)) 'Integer)]
['Any (values (Prim 'vector-length (list e1^)) 'Integer)]
[`(Vectorof ,elt-type)
(error 'type-check "unhandled Vectorof in vector-length")]
)]
[(Prim 'vector-ref (list e1 e2))
(define-values (e1^ t1) (recur e1))
(define-values (e2^ t2) (recur e2))
(check-consistent? t2 'Integer e)
(match t1
[`(Vector ,ts ...)
(match e2^
[(Int i)
(unless (and (0 . <= . i) (i . < . (length ts)))
(error 'type-check "invalid index ~a in ~a" i e))
(values (Prim 'vector-ref (list e1^ (Int i))) (list-ref ts i))]
[else (values (Prim 'vector-ref (list e1^ e2^)) 'Any)])]
[`(Vectorof ,elt-type)
(values (Prim 'vector-ref (list e1^ e2^)) elt-type)]
['Any
(values (Prim 'vector-ref (list e1^ e2^)) 'Any)]
[else (error 'type-check "expected vector not ~a\nin ~v" t1 e)])]
[(Prim 'vector-set! (list e1 e2 e3) )
(define-values (e1^ t1) (recur e1))
(define-values (e2^ t2) (recur e2))
(define-values (e3^ t3) (recur e3))
(check-consistent? t2 'Integer e)
(match t1
[`(Vector ,ts ...)
(match e2^
[(Int i)
(unless (and (0 . <= . i) (i . < . (length ts)))
(error 'type-check "invalid index ~a in ~a" i e))
(check-consistent? (list-ref ts i) t3 e)
(values (Prim 'vector-set! (list e1^ (Int i) e3^)) 'Void)]
[else
(values (Prim 'vector-set! (list e1^ e2^ e3^)) 'Void)])]
[`(Vectorof ,elt-type)
(values (Prim 'vector-set! (list e1^ e2^ e3^)) 'Void)]
['Any
(values (Prim 'vector-set! (list e1^ e2^ e3^)) 'Void)]
[else (error 'type-check "expected vector not ~a\nin ~v" t1 e)])]
;; Llambda
[(Apply e1 e2s)
(define-values (e1^ T1) (recur e1))
(define-values (e2s^ T2s) (for/lists (e* ty*) ([e2 e2s]) (recur e2)))
(match T1
[`(,T1ps ... -> ,T1rt)
(for ([T2 T2s] [Tp T1ps])
(check-consistent? T2 Tp e))
(values (Apply e1^ e2s^) T1rt)]
[`Any
(values (Apply e1^ e2s^) 'Any)]
[else (error 'type-check "expected function not ~a\nin ~v" T1 e)])]
[(Lambda params Tr e1)
(define-values (xs Ts) (for/lists (l1 l2) ([p params])
(match p
[`[,x : ,T] (values x T)]
[(? symbol? x) (values x 'Any)])))
(define-values (e1^ T1)
((type-check-exp (append (map cons xs Ts) env)) e1))
(check-consistent? Tr T1 e)
(values (Lambda (for/list ([x xs] [T Ts]) `[,x : ,T]) Tr e1^)
`(,@Ts -> ,Tr))]
[else ((super type-check-exp env) e)]
)))
(define/override (type-check-def env)
(lambda (e)
(match e
[(Def f params rt info body)
(define-values (xs ps) (for/lists (l1 l2) ([p params])
(match p
[`[,x : ,T] (values x T)]
[(? symbol? x) (values x 'Any)])))
(define new-env (append (map cons xs ps) env))
(define-values (body^ ty^) ((type-check-exp new-env) body))
(check-consistent? ty^ rt e)
(Def f (for/list ([x xs] [T ps]) `[,x : ,T]) rt info body^)]
[else (error 'type-check "ill-formed function definition ~a" e)]
)))
(define/override (type-check-program e)
(match e
[(Program info body)
(define-values (body^ ty) ((type-check-exp '()) body))
(check-consistent? ty 'Integer e)
(ProgramDefsExp info '() body^)]
[(ProgramDefsExp info ds body)
(define new-env (for/list ([d ds])
(cons (Def-name d) (fun-def-type d))))
(define ds^ (for/list ([d ds])
((type-check-def new-env) d)))
(define-values (body^ ty) ((type-check-exp new-env) body))
(check-consistent? ty 'Integer e)
(ProgramDefsExp info ds^ body^)]
[else (super type-check-program e)]))
))
(define (type-check-gradual p)
(send (new type-check-gradual-class) type-check-program p))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; type-check-Lany-proxy
(define (type-check-Lany-proxy-mixin super-class)
(class super-class
(super-new)
(inherit check-type-equal?)
(define/override (flat-ty? ty)
(match ty
[`(PVector ,ts ...)
(for/and ([t ts]) (eq? t 'Any))]
[else (super flat-ty? ty)]))
(define/override (type-equal? t1 t2)
(match (list t1 t2)
[(list `(PVector ,ts1 ...) `(PVector ,ts2 ...))
(for/and ([t1 ts1] [t2 ts2])
(type-equal? t1 t2))]
[else (super type-equal? t1 t2)]))
(define/override ((type-check-exp env) e)
(define recur (type-check-exp env))
(match e
[(Prim 'inject-vector (list e1))
(define-values (e1^ T1) (recur e1))
(match T1
[`(Vector ,ts ...)
(values (Prim 'inject-vector (list e1^)) `(PVector ,@ts))]
)]
[(Prim 'inject-proxy (list e1))
(define-values (e1^ T1) (recur e1))
(match T1
[`(Vector (PVector ,ts0 ...) (Vector (,ts1 -> ,ts2) ...) ,ws)
(values (Prim 'inject-proxy (list e1^)) `(PVector ,@ts2))]
;; after closure conversion
[`(Vector (PVector ,ts0 ...)
(Vector (Vector (,clos ,ts1 -> ,ts2)) ...) ,ws)
(values (Prim 'inject-proxy (list e1^)) `(PVector ,@ts2))]
)]
[(Prim 'proxy? (list e1))
(define-values (e1^ T1) (recur e1))
(match T1
[`(PVector ,ts ...)
(values (Prim 'proxy? (list e1^)) 'Boolean)]
)]
[(Prim 'project-vector (list e1))
(define-values (e1^ T1) (recur e1))
(match T1
[`(PVector ,ts ...)
(values (Prim 'project-vector (list e1^)) `(Vector ,@ts))]
)]
[(Prim 'proxy-vector-length (list e1))
(define-values (e1^ T1) (recur e1))
(match T1
[`(PVector ,ts ...)
(values (Prim 'proxy-vector-length (list e1^)) 'Integer)])]
[(Prim 'proxy-vector-ref (list e1 e2))
(define-values (e1^ T1) (recur e1))
(define-values (e2^ T2) (recur e2))
(match (list T1 e2^)
[(list `(PVector ,ts ...) (Int i))
(unless (and (0 . <= . i) (i . < . (length ts)))
(error 'type-check "invalid index ~a in ~a" i e))
(values (Prim 'proxy-vector-ref (list e1^ e2^))
(list-ref ts i))])]
[(Prim 'proxy-vector-set! (list e1 e2 e3))
(define-values (e1^ T1) (recur e1))
(define-values (e2^ T2) (recur e2))
(define-values (e3^ T3) (recur e3))
(match (list T1 e2^)
[(list `(PVector ,ts ...) (Int i))
(unless (and (0 . <= . i) (i . < . (length ts)))
(error 'type-check "invalid index ~a in ~a" i e))
(check-type-equal? (list-ref ts i) T3 e)
(values (Prim 'proxy-vector-set! (list e1^ e2^ e3^))
'Void)])]
[else ((super type-check-exp env) e)]))
))
(define type-check-Lany-proxy-class
(type-check-Lany-proxy-mixin type-check-Lany-class))
(define (type-check-Lany-proxy p)
(send (new type-check-Lany-proxy-class) type-check-program p))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; type-check-Cany-proxy
(define type-check-Cany-proxy-class
(type-check-Lany-proxy-mixin type-check-Cany-class))
(define (type-check-Cany-proxy p)
(send (new type-check-Cany-proxy-class) type-check-program p))