4 (and (list? t) (eq? (car t) 'abs)))
13 (char-upper-case? (string-ref (symbol->string t) 0))))
15 (define (pretty-type t)
19 (string-append "(" (pretty-type (cadr t)) ")")
20 (pretty-type (cadr t)))
22 (pretty-type (caddr t))))
23 (else (symbol->string t))))
25 (define (pretty-constraints cs)
27 (fold-left string-append
39 (define (env-lookup env n)
40 (if (null? env) (error #f "empty env" env n) ; it's a type equality
41 (if (eq? (caar env) n)
43 (env-lookup (cdr env) n))))
45 (define (env-insert env n t)
46 (cons (cons n t) env))
53 (set! cur-tvar (+ cur-tvar 1))
55 (string-append "t" (number->string (- cur-tvar 1))))))
62 (define (normalize prog) ; (+ a b) -> ((+ a) b)
65 ; (lambda (x y) (+ x y)) -> (lambda (x) (lambda (y) (+ x y)))
66 (if (> (length (lambda-args prog)) 1)
67 (list 'lambda (list (car (lambda-args prog)))
68 (normalize (list 'lambda (cdr (lambda-args prog)) (caddr prog))))
69 (list 'lambda (lambda-args prog) (normalize (caddr prog)))))
71 (if (null? (cddr prog))
72 `(,(normalize (car prog)) ,(normalize (cadr prog))) ; (f a)
73 (normalize `(,(list (normalize (car prog)) (normalize (cadr prog)))
74 ,@(cddr prog))))) ; (f a b)
77 (map (lambda (x) `(,(car x) ,(normalize (cadr x))))
79 (map normalize (let-body prog))))
80 (else (ast-traverse normalize prog))))
82 (define (builtin-type x)
84 ('+ '(abs Int (abs Int Int)))
85 ('- '(abs Int (abs Int Int)))
86 ('* '(abs Int (abs Int Int)))
88 ('= '(abs Int (abs Int Bool)))
89 ('bool->int '(abs Bool Int))
90 ('print '(abs String Void))
91 (else (error #f "Couldn't find type for builtin" x))))
93 (define (check-let dls env x)
95 ; acc is a pair of (env . annotated bindings)
96 (define (process-component acc comps)
98 ; create a new env with tvars for each component
100 ; scc-env = ((x . t0) (y . t1))
104 (env-insert acc c (fresh-tvar)))
106 ; typecheck each component
110 (let ([body (cadr (assoc c (let-bindings x)))])
111 (check dls scc-env body)))
113 ; collect all the constraints in the scc
119 ; unify with tvars from scc-env
121 (~ (env-lookup scc-env c) (cadr res))
124 '() type-results comps)]
125 ; substitute *only* the bindings in this scc
128 (if (memv (car x) comps)
129 (cons (car x) (substitute cs (cdr x)))
133 [annotated-bindings (append (cdr acc) ; the previous annotated bindings
136 (map caddr type-results)))])
137 (cons new-env annotated-bindings)))
138 ; takes in the current environment and a scc
139 ; returns new environment with scc's types added in
140 (let* ([components (reverse (sccs (graph (let-bindings x))))]
141 [results (fold-left process-component (cons env '()) components)]
142 [new-env (car results)]
143 [annotated-bindings (cdr results)]
145 [body-results (map (lambda (body) (check dls new-env body)) (let-body x))]
146 [let-type (cadr (last body-results))]
147 [cs (fold-left (lambda (acc cs) (constraint-merge acc cs)) '() (map car body-results))]
149 [annotated `((let ,annotated-bindings ,@(map caddr body-results)) : ,let-type)])
150 (list cs let-type annotated)))
152 (define (check-app dls env x)
153 (if (eqv? (car x) (cadr x))
154 ; recursive function (f f)
155 ; TODO: what about ((f a) f)????
156 (let* ([func-type (env-lookup env (car x))]
157 [return-type (fresh-tvar)]
158 [other-func-type `(abs ,func-type ,return-type)]
159 [cs (~ func-type other-func-type)]
160 [resolved-return-type (substitute cs return-type)]
162 [annotated `(((,(car x) : ,func-type)
163 (,(cadr x) : ,func-type)) : ,resolved-return-type)])
164 (list cs resolved-return-type annotated)))
167 (let* ([arg-type-res (check dls env (cadr x))]
168 [arg-type (cadr arg-type-res)]
169 [func-type-res (check dls env (car x))]
170 [func-type (cadr func-type-res)]
174 (substitute (car arg-type-res) func-type)
175 `(abs ,arg-type ,(fresh-tvar)))]
176 [cs (constraint-merge
177 (constraint-merge func-c (car arg-type-res))
178 (car func-type-res))]
180 [resolved-func-type (substitute cs func-type)]
181 [resolved-return-type (caddr resolved-func-type)]
183 [annotated `((,(caddr func-type-res)
184 ,(caddr arg-type-res)) : ,resolved-return-type)])
186 (if (abs? resolved-func-type)
187 (let ((return-type (substitute cs (caddr resolved-func-type))))
188 (list cs return-type annotated))
189 (error #f "not a function"))))
191 (define (check-case dls env x)
193 (define (check-match switch-type x)
194 (let ([pattern (car x)]
196 (case (ast-type pattern)
198 ; a pattern match with bindings
199 (let ([sum (assoc (car pattern) (cdr (assoc switch-type dls)))])
200 (unless sum (error #f "can't pattern match ~a with ~a" switch-type pattern))
201 (let* ([names (cdr pattern)]
203 [new-env (fold-left env-insert env names types)])
204 (check dls new-env expr)))]
205 ; pattern match with binding and no constructor
206 ['var (check dls (env-insert env pattern switch-type) expr)]
207 ; a pattern match without bindings
208 [else (check dls env expr)])))
210 (let* ([switch-type-res (check dls env (case-switch x))]
211 [switch-type (cadr switch-type-res)]
213 [case-expr-type-res (map (lambda (x) (check-match switch-type x)) (case-cases x))]
214 [case-expr-types (map cadr case-expr-type-res)]
216 [case-expr-equality-cs (fold-left constraint-merge '()
217 (map (lambda (t) (~ t (car case-expr-types)))
218 (cdr case-expr-types)))]
220 [resolved-type (substitute case-expr-equality-cs (car case-expr-types))]
222 [annotated `((case ,(caddr switch-type-res)
223 ,@(map (lambda (c e et)
225 (map car (case-cases x))
226 (map cadr (case-cases x))
227 case-expr-types)) : ,resolved-type)]
229 [cs (fold-left constraint-merge '()
230 (cons (car switch-type-res) case-expr-equality-cs))])
231 (list cs resolved-type annotated)))
233 ; returns a list (constraints type annotated)
234 (define (check dls env x)
235 (define (make-result cs type)
236 (list cs type `(,x : ,type)))
237 ;; (display "check: ")
245 ('int-literal (make-result '() 'Int))
246 ('bool-literal (make-result '() 'Bool))
247 ('string-literal (make-result '() 'String))
248 ('builtin (make-result '() (builtin-type x)))
251 (let* ((cond-type-res (check dls env (cadr x)))
252 (then-type-res (check dls env (caddr x)))
253 (else-type-res (check dls env (cadddr x)))
254 (then-eq-else-cs (~ (cadr then-type-res)
255 (cadr else-type-res)))
256 (cs (constraint-merge
258 (constraint-merge (~ (cadr cond-type-res) 'Bool)
259 (constraint-merge (car else-type-res)
261 (return-type (substitute cs (cadr then-type-res)))
262 [annotated `((if ,(caddr cond-type-res)
263 ,(caddr then-type-res)
264 ,(caddr else-type-res)) : ,return-type)])
265 (list cs return-type annotated)))
267 ('var (make-result '() (env-lookup env x)))
268 ('let (check-let dls env x))
272 (let* ([new-env (env-insert env (lambda-arg x) (fresh-tvar))]
274 [body-type-res (check dls new-env (lambda-body x))]
275 [cs (car body-type-res)]
276 [subd-env (substitute-env (car body-type-res) new-env)]
277 [arg-type (env-lookup subd-env (lambda-arg x))]
278 [resolved-arg-type (substitute cs arg-type)]
280 [lambda-type `(abs ,resolved-arg-type ,(cadr body-type-res))]
282 [annotated `((lambda (,(lambda-arg x)) ,(caddr body-type-res)) : ,lambda-type)])
284 (list (car body-type-res) ; constraints
289 ('app (check-app dls env x))
290 ['case (check-case dls env x)])))
293 ;; (display "result of ")
296 ;; (display (pretty-type (cadr res)))
298 ;; (display (pretty-constraints (car res)))
302 (define (init-adts-env prog)
303 (flat-map data-tors-type-env (program-data-layouts prog)))
305 ; we typecheck the lambda calculus only (only single arg lambdas)
306 (define (typecheck prog)
307 (let ([expanded (expand-pattern-matches prog)])
308 (cadr (check (program-data-layouts prog)
309 (init-adts-env expanded)
310 (normalize (program-body expanded))))))
313 ; before passing annotated types onto codegen
314 ; we need to restore the pre-normalization structure
315 ; (this is important for function arity etc)
316 (define (denormalize orig normed)
318 (define (collapse-lambdas n x)
322 (let* ([inner-lambda (lambda-body (ann-expr x))]
323 [arg (lambda-arg (ann-expr x))]
324 [inner-collapsed (ann-expr (collapse-lambdas (- n 1) inner-lambda))])
325 `((lambda ,(cons arg (lambda-args inner-collapsed))
326 ,(lambda-body inner-collapsed)) : ,(ann-type x)))]))
328 (define (collapse-apps n x)
330 [-1 (error #f "nullary functions not handled yet")]
333 (let* ([inner-app (car (ann-expr x))]
334 [inner-collapsed (collapse-apps (- n 1) inner-app)])
335 `(,(append (ann-expr inner-collapsed) (cdr (ann-expr x))) : ,(ann-type x)))]))
337 (case (ast-type orig)
339 (let ([collapsed (collapse-lambdas (- (length (lambda-args orig)) 1) normed)])
340 `((lambda ,(lambda-args (ann-expr collapsed))
341 ,(denormalize (lambda-body orig)
342 (lambda-body (ann-expr collapsed)))) : ,(ann-type collapsed)))]
344 (let ([collapsed (collapse-apps (- (length orig) 2) normed)])
345 `(,(map (lambda (o n) (denormalize o n)) orig (ann-expr collapsed))
346 : ,(ann-type collapsed)))]
348 `((let ,(map (lambda (o n) (list (car o) (denormalize (cadr o) (cadr n))))
350 (let-bindings (ann-expr normed)))
353 (let-body (ann-expr normed)))) : ,(ann-type normed))]
354 ['if `((if ,@(map denormalize (cdr orig) (cdr (ann-expr normed))))
355 : ,(ann-type normed))]
356 ['case `((case ,(denormalize (case-switch orig) (case-switch (ann-expr normed)))
357 ,@(map (lambda (o n) (cons (car o) (denormalize (cadr o) (cadr n))))
358 (case-cases orig) (case-cases (ann-expr normed))))
359 : ,(ann-type normed))]
362 (define ann-expr car)
363 (define ann-type caddr)
365 ; prerequisites: expand-pattern-matches
366 (define (annotate-types prog)
369 (caddr (check (program-data-layouts prog)
371 (normalize (program-body prog))))))
374 ; returns a list of constraints
376 (let ([res (unify? a b)])
380 (format "couldn't unify ~a ~~ ~a" a b)))))
383 (cond [(eq? a b) '()]
384 [(tvar? a) (list (cons a b))]
385 [(tvar? b) (list (cons b a))]
386 [(and (abs? a) (abs? b))
387 (let* [(arg-cs (unify? (cadr a) (cadr b)))
388 (body-cs (unify? (substitute arg-cs (caddr a))
389 (substitute arg-cs (caddr b))))]
390 (constraint-merge body-cs arg-cs))]
393 (define (substitute cs t)
399 [(abs? t) `(abs ,(substitute cs (cadr t))
400 ,(substitute cs (caddr t)))]
403 ; applies substitutions to all variables in environment
404 (define (substitute-env cs env)
405 (map (lambda (x) (cons (car x) (substitute cs (cdr x)))) env))
407 ; composes constraints a onto b and merges, i.e. applies a to b
408 ; a should be the "more important" constraints
409 (define (constraint-merge a b)
410 (define (f cs constraint)
411 (cons (car constraint)
412 (substitute cs (cdr constraint))))
414 (define (most-concrete a b)
418 [(and (abs? a) (abs? b))
419 `(abs ,(most-concrete (cadr a) (cadr b))
420 ,(most-concrete (caddr a) (caddr b)))]
425 ; for any two constraints that clash, e.g. t1 ~ abs t2 t3
426 ; and t1 ~ abs int t3
427 ; prepend the most concrete version of the type to the
428 ; list of constraints
431 (if (assoc (car x) a)
432 (cons (cons (car x) (most-concrete (cdr (assoc (car x) a))
436 (fold-left gen '() b))
439 (append (filter (lambda (x) (not (assoc (car x) p)))
442 (append (clashes) (union a (map (lambda (z) (f a z)) b))))
445 ;; ; a1 -> a2 ~ a3 -> a4;
446 ;; ; a1 -> a2 !~ Bool -> Bool
447 ;; ; basically can the tvars be renamed
448 (define (types-equal? x y)
449 (let ([cs (unify? x y)])
452 ([test (lambda (acc c)
454 (tvar? (car c)) ; the only substitutions allowed are tvar -> tvar
456 (fold-left test #t cs)))))
458 ; input: a list of binds ((x . y) (y . 3))
459 ; returns: pair of verts, edges ((x y) . (x . y))