-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfer.rkt
347 lines (290 loc) · 13.2 KB
/
infer.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
(require racket)
(define type-name (compose car unbox))
(define type-params (compose cdr unbox))
(define-macro (trace func)
`(set! ,func (trace-function ,func ',func)))
(define tt box)
; checks whether a symbol is a type variable (generated symbol) or a regular identifier
(define typevar? (compose not symbol-interned? type-name))
; resolves a type in case it is a type variable
; in which case the indirection chain is followed until either an unbound variable or a concrete type is found
(define (resolve t)
(if (and (typevar? t) (car (type-params t)))
(let ([res (resolve (car (type-params t)))])
(set-box! t (list (type-name t) res))
res)
t))
; unifies two types
(define (unify t1 t2)
(let ([t1 (resolve t1)] [t2 (resolve t2)])
(if (typevar? t1)
(unless (eq? t1 t2) ; no need to unify if types are equal
(if (occurs t1 t2) ; check if t1 is a parameter of t2
(error (format "recursive unification; for types '~A' and '~A'\n" (type->string t1) (type->string t2)))
(set-box! t1 (list (type-name t1) t2))))
(if (typevar? t2)
(unify t2 t1)
(cond
[(not (eq? (type-name t1) (type-name t2)))
(error (format "type mismatch; unable to unify different types '~A' and '~A'\n" (type->string t1) (type->string t2)))]
[(not (eq? (length (type-params t1)) (length (type-params t2))))
(error (format "type mismatch; unable to unify type '~A' with different arities: '~A' and '~A'\n" (type-name t1) (type->string t1) (type->string t2)))]
; unify the parameters
[else (for-each unify (type-params t1) (type-params t2))])))))
; creates a type variable
; internally stored as (name value) with
; - name: uninterned symbol
; - value: initially #f, replaced with type when variable is bound
(define (typevar) (tt `(,(gensym) #f)))
; creates a function type with the specified arguments and result type
; (ft (a b) -> c) => (-> (-> c b) a)
(define-macro (ft args _ res) (foldr (lambda (a b) `(tt (list '-> ,b ,a))) res (if (list? args) args (list args))))
; checks whether a type occurs in another type's parameters
(define (occurs needle haystack)
(let ([haystack (resolve haystack)])
(or (eq? needle haystack)
(memv needle (type-params haystack)))))
; duplicates a type
(define (duplicate type env ngen)
(let ([type (resolve type)])
(if (typevar? type)
(if (not (null? (filter (lambda (t) (occurs type t)) ngen)))
type
(let ([pair (assoc type (unbox env))])
(if pair
(cadr pair)
(let ([newtype (typevar)])
(set-box! env (cons (list type newtype) (unbox env)))
newtype))))
(tt `(,(type-name type) ,@(map (lambda (t) (duplicate t env ngen)) (type-params type)))))))
; basic types are nullary
(define int (tt `(int)))
(define bool (tt `(bool)))
(define str (tt `(str)))
(define unit (tt `(unit)))
(define bottom (typevar)) ; from a typing point of view, the bottom/never type is forall a. a
(define default-env
(box `(
; integer primitives
(+ ,(ft (int int) -> int))
(- ,(ft (int int) -> int))
(* ,(ft (int int) -> int))
(/ ,(ft (int int) -> int))
(modulo ,(ft (int int) -> int))
(= ,(ft (int int) -> bool))
(zero ,(ft int -> bool))
(succ ,(ft int -> int))
(pred ,(ft int -> int))
; boolean primitives
(and ,(ft (bool bool) -> bool))
(or ,(ft (bool bool) -> bool))
(error ,(ft str -> bottom))
,@(let ([t1 (typevar)] [t2 (typevar)] [t3 (typevar)] [t4 (typevar)])
`(
(if ,(ft (bool t1 t1) -> t1))
; pairs
(pair ,(ft (t1 t2) -> (tt `(* ,t1 ,t2))))
(car ,(ft ((tt `(* ,t1 ,t2))) -> t1))
(cdr ,(ft ((tt `(* ,t1 ,t2))) -> t2))
; lists
(nil ,(tt `(list ,t1)))
(cons ,(ft (t1 (tt `(list ,t1))) -> (tt `(list ,t1))))
(hd ,(ft ((tt `(list ,t1))) -> t1))
(tl ,(ft ((tt `(list ,t1))) -> (tt `(list ,t1))))
(null? ,(ft ((tt `(list ,t1))) -> bool))
(map ,(ft ((ft t1 -> t2) (tt `(list ,t1))) -> (tt `(list ,t2))))
(for-each ,(ft ((ft t1 -> unit) (tt `(list ,t1))) -> unit))
; Either a b
(left ,(ft t1 -> (tt `(either ,t1 ,t2))))
(right ,(ft t2 -> (tt `(either ,t1 ,t2))))
(either ,(ft ((tt `(either ,t1 ,t2)) (ft t1 -> t3) (ft t2 -> t4)) -> (tt `(either ,t3 ,t4))))
; Maybe a
(just ,(ft t1 -> (tt `(option ,t1))))
(nothing ,(tt `(option ,t1)))
(maybe ,(ft ((tt `(option ,t1)) (ft t1 -> t2)) -> (tt `(option ,t2))))
))
)))
(define (analyze expr)
(letrec ([aux (lambda (expr env ngen)
(define (find-sym sym)
(let ([val (assoc sym (unbox env))])
(if (eq? #f val)
(error (format "unknown symbol '~A', available are ~A\n" expr (map car (unbox env))))
(cadr val))))
(define (self expr) (aux expr env ngen))
(cond
((pair? expr)
(case (car expr)
; Special forms
; let: create all the bindings, evaluate their values, then evaluate the body
('let (aux (caddr expr) (box (append (map (lambda (p) (list (car p) (self (cadr p)))) (cadr expr)) (unbox env))) ngen))
; let*: same as let, but bindings are evaluated in order with each having access to the previously evaluated ones
('let*
(let* (
[vars (cadr expr)]
[var (car vars)]
[body (caddr expr)]
[remaining (cdr vars)]
)
(if (null? remaining)
; (let* ((a b)) x)
(aux body (box (cons (list (car var) (self (cadr var))) (unbox env))) ngen)
; (let* ((a b) (c d)) x) gets rewritten into (let* ((a b)) (let* ((c d)) x))
(self `(let* (,var) (let* ,remaining ,body))))))
; letrec: creates all the bindings, associate them with free type variables, then evaluate their values, then evaluate the body
('letrec (let* (
[ftypes (map (lambda (p) (list (cadr p) (car p) (typevar))) (cadr expr))]
[newenv (box (append (map cdr ftypes) (unbox env)))]
[newngen (append (map third ftypes) ngen)]
)
(for-each (lambda (t) (unify (caddr t) (aux (car t) newenv newngen))) ftypes)
(aux (caddr expr) newenv ngen)
))
; lambda: bind the parameter to a free type variable, evaluate the body, build the function's type out of it
('lambda
(let* (
[params (cadr expr)]
[param (car params)]
[body (caddr expr)]
[remaining (cdr params)]
)
(if (null? remaining)
; (lambda (a) x)
(let ([argtype (typevar)])
(ft (argtype) -> (aux body (box (cons (list (car params) argtype) (unbox env))) (cons argtype ngen))))
; (lambda (a b) x) gets rewritten into (lambda (a) (lambda (b) x))
(self `(lambda (,param) (lambda ,remaining ,body))))))
(else
(if (null? (cddr expr))
; simple function application: (f a)
(let* (
; find type of f
[val (self (car expr))]
; create a free type variable for the return type
[rettype (typevar)]
; evaluate the argument's type
[argtype (self (cadr expr))]
; build a function type
[functype (tt `(-> ,rettype ,argtype))])
(begin
; try unifying the built type with the function's type
(unify functype val)
; result is simply the return type post-unification
rettype))
; n-ary function application: (f a b) gets rewritten into ((f a) b)
(self (cons (list (car expr) (cadr expr)) (cddr expr))))
)))
((symbol? expr) (duplicate (find-sym expr) (tt '()) ngen))
((number? expr) int)
((string? expr) str)
((boolean? expr) bool)
(else (error (format "unknown type for Scheme object '~A'\n" expr)))))])
(aux expr default-env '())))
; Pretty-prints a type
; Type parameters are written using lowercase letters starting with 'a'
(define (type->string type)
(let ([letter 97] [names '()])
(define (aux t)
(let ([t (resolve t)])
(case (type-name t)
; special cases
; function type: (a -> b)
['-> (format "(~A -> ~A)"
(aux (cadr (type-params t)))
(aux (car (type-params t))))]
; pair type: (a * b)
['* (format "(~A)" (string-join (map aux (type-params t)) " * "))]
; general case
[else
(if (typevar? t)
; type variable: assign a letter
(let ([existing (assoc (type-name t) names)])
(if (eq? #f existing)
(let ([char (~a (integer->char letter))])
(set! letter (+ 1 letter))
(set! names (cons (list (type-name t) char) names))
char)
(cadr existing)))
(if (null? (type-params t))
; nullary type: int, bool, ...
(~a (type-name t))
; parameterized type: (list a)
(format "(~A ~A)" (type-name t) (string-join (map aux (type-params t)) " "))))])))
(aux type)))
(for-each
(lambda (x) (displayln (format "~A <= ~A" (~a #:min-width 36 (type->string (analyze x))) x)))
'(
(error "this returns the bottom type (forall a. a)")
; int
(lambda (x) (if (zero x) (error "divide by zero") (/ 1 x)))
; (a -> (int * a))
(pair 6)
; ((int -> a) -> a)
(lambda (f) (f 5))
; (a -> a)
(lambda (f) f)
; (a -> (b -> a))
(lambda (x y) x)
; int
;((lambda (f) f) 5)
; bool
((lambda (x y) x) #t 6)
; int
(let ([five 5]) (let ([g (lambda (f) f)]) (g five)))
; (int * bool)
(let ([f (lambda (x) x)]) (pair (f 4) (f #t)))
; invalid (recursive)
;(lambda (f) (f f))
; int
(let ([g (lambda (f) 5)]) (g g))
; (a -> (a * a))
(lambda (x) (pair x x))
; (int * int)
((lambda (g) (pair 9 g)) 5)
; int
(cdr ((lambda (g) (pair 9 g)) 5))
; (a -> (b -> (b * a)))
(lambda (x y) (pair y x))
; (a -> (a * a))
(lambda (g) (let ((f (lambda (x) g))) (pair (f 3) (f #t))))
; ((a -> b) -> ((b -> c) -> (a -> c)))
(lambda (f) (lambda (g) (lambda (arg) (g (f arg)))))
; int
(letrec ((factorial (lambda (n) (if (zero n) 1 (* n (factorial (- n 1))))))) (factorial 5))
; ((list a) -> int)
(lambda (l) (letrec ((length (lambda (l) (if (null? l) 0 (+ 1 (length (tl l))))))) (length l)))
; (list int)
(tl (cons 5 nil))
; (option bool)
(let ([x (just 123)])
(maybe x (= 123)))
; (option bool)
(let ([x nothing])
(maybe x (= 123)))
; (either bool int)
(let ([x (left 5)])
(either x (= 123) (lambda (bool) 456)))
; invalid (can't use + on bool)
;(let ([x (right #t)])
; (either x (= 123) (lambda (bool) (+ bool 5))))
; (int * bool)
(let* (
[kons (lambda (a b) (lambda (f) (f a b)))]
[kar (lambda (p) (p (lambda (a d) a)))]
[kdr (lambda (p) (p (lambda (a d) d)))]
[test (kons 8 #t)]
)
(pair (kar test) (kdr test)))
; bool
(let (
[multiple (lambda (k) (lambda(x) (= (modulo x k) 0)))]
[singleton =]
[union (lambda (a b) (lambda (x) (or (a x) (b x))))]
[in? (lambda (n ens) (ens n))]
)
(in? 1 (union (multiple 5) (singleton 2))))
; fails
;(let ([a 1] [b a]) b)
; succeeds
;(let* ([a 1] [b a]) b)
))