-
Notifications
You must be signed in to change notification settings - Fork 0
/
type-check-gradual.rkt
374 lines (345 loc) · 14.8 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#lang racket
(require "utilities.rkt")
(require "type-check-Lwhile.rkt")
(require "type-check-Cwhile.rkt")
(provide type-check-gradual type-check-gradual-class
type-check-Lwhile-proxy type-check-Lwhile-proxy-class
type-check-Cwhile-proxy type-check-Cwhile-proxy-class
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define type-check-gradual-class
(class type-check-Lwhile-class
(super-new)
(inherit operator-types type-predicates)
(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))]
[(`(,ts1 ... -> ,rt1) `(,ts2 ... -> ,rt2))
(and (for/and ([t1 ts1] [t2 ts2]) (consistent? t1 t2))
(consistent? rt1 rt2))]
[(other wise) #f]))
(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)))]
[(`(,ts1 ... -> ,rt1) `(,ts2 ... -> ,rt2))
`(,@(for/list ([t1 ts1] [t2 ts2]) (join t1 t2))
-> ,(join rt1 rt2))]))
(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)))]
[(`(,ts1 ... -> ,rt1) `(,ts2 ... -> ,rt2))
`(,@(for/list ([t1 ts1] [t2 ts2]) (meet t1 t2))
-> ,(meet rt1 rt2))]))
(define/public (make-cast e src tgt)
(cond [(equal? src tgt) e] [else (Cast e src tgt)]))
(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 add args parameter so that they can
;; be translated using cast. -Jeremy
(define/override (type-check-op op arg-types args e)
(match (dict-ref (operator-types) op)
[`(,param-types . ,return-type)
(for ([at arg-types] [pt param-types])
(check-consistent? at pt e))
(values return-type
(for/list ([e args] [s arg-types] [t param-types])
(make-cast e s t)))]
[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?
'vector 'vector-length 'vector-ref 'vector-set!
'any-vector-length 'any-vector-ref 'any-vector-set!)))
(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/override (type-check-exp env)
(lambda (e)
(verbose "gradual/type-check-exp" e)
(define recur (type-check-exp env))
(match e
[(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 'any-vector-length (list e1^)) 'Integer)])]
[(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 (define e1^^ (make-cast e1^ t1 'Any))
(define e2^^ (make-cast e2^ t2 'Integer))
(values (Prim 'any-vector-ref (list e1^^ e2^^)) 'Any)])]
['Any
(define e2^^ (make-cast e2^ t2 'Integer))
(values (Prim 'any-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)
(define e3^^ (make-cast e3^ t3 (list-ref ts i)))
(values (Prim 'vector-set! (list e1^ (Int i) e3^^)) 'Void)]
[else
(define e1^^ (make-cast e1^ t1 'Any))
(define e2^^ (make-cast e2^ t2 'Integer))
(define e3^^ (make-cast e3^ t3 'Any))
(values (Prim 'any-vector-set! (list e1^^ e2^^ e3^^)) 'Void)])]
['Any
(define e2^^ (make-cast e2^ t2 'Integer))
(define e3^^ (make-cast e3^ t3 'Any))
(values (Prim 'any-vector-set! (list e1^ e2^^ e3^^)) 'Void)]
[else (error 'type-check "expected vector not ~a\nin ~v" t1 e)])]
[(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 (make-cast e1^ t1 T) (make-cast e2^ t2 T)))
'Boolean)]
[(Prim 'not (list e1))
(define-values (e1^ t1) (recur e1))
(match t1
['Any
(recur (If (Prim 'eq? (list e1 (Inject (Bool #f) 'Boolean)))
(Bool #t) (Bool #f)))]
[else
(define-values (t-ret new-es^)
(type-check-op 'not (list t1) (list e1^) e))
(values (Prim 'not new-es^) t-ret)])]
[(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)))]
[(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-values (t-ret new-es^) (type-check-op op ts new-es e))
(values (Prim op new-es^) t-ret)]
[(If e1 e2 e3)
(define-values (e1^ T1) (recur e1))
(define-values (e2^ T2) (recur e2))
(define-values (e3^ T3) (recur e3))
(check-consistent? T2 T3 e)
(match T1
['Boolean
(define Tif (join T2 T3))
(values (If e1^ (make-cast e2^ T2 Tif)
(make-cast e3^ T3 Tif)) Tif)]
['Any
(define Tif (meet T2 T3))
(values (If (Prim 'eq? (list e1^ (Inject (Bool #f) 'Boolean)))
(make-cast e3^ T3 Tif) (make-cast e2^ T2 Tif))
Tif)]
[else (error 'type-check "expected Boolean not ~a\nin ~v" T1 e)])]
[(HasType e1 T)
(define-values (e1^ T1) (recur e1))
(check-consistent? T1 T)
(values (make-cast e1^ T1 T) T)]
[(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))
(define e2s^^ (for/list ([e2 e2s^] [src T2s] [tgt T1ps])
(make-cast e2 src tgt)))
(values (Apply e1^ e2s^^) T1rt)]
[`Any
(define e1^^ (make-cast e1^ 'Any
`(,@(for/list ([e e2s]) 'Any) -> Any)))
(define e2s^^ (for/list ([e2 e2s^] [src T2s])
(make-cast e2 src '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
(make-cast e1^ T1 Tr)) `(,@Ts -> ,Tr))]
[(SetBang x e1)
(define-values (e1^ T1) (recur e1))
(define varT (dict-ref env x))
(check-consistent? T1 varT e)
(values (SetBang x (make-cast e1^ T1 varT)) '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 (make-cast e1^ T1 'Boolean) e2^) 'Void)]
[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
(make-cast body^ ty^ rt))]
[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 '() (make-cast body^ ty 'Integer))]
[(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^ (make-cast body^ ty 'Integer))]
[else (super type-check-program e)]))
))
(define (type-check-gradual p)
(send (new type-check-gradual-class) type-check-program p))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; type-check-Lwhile-proxy
(define (type-check-Lwhile-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-Lwhile-proxy-class
(type-check-Lwhile-proxy-mixin type-check-Lwhile-class))
(define (type-check-Lwhile-proxy p)
(send (new type-check-Lwhile-proxy-class) type-check-program p))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; type-check-Cwhile-proxy
(define type-check-Cwhile-proxy-class
(type-check-Lwhile-proxy-mixin type-check-Cwhile-class))
(define (type-check-Cwhile-proxy p)
(send (new type-check-Cwhile-proxy-class) type-check-program p))