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 env x)
94 ; takes in the current environment and a scc
95 ; returns new environment with scc's types added in
96 (let* ([components (reverse (sccs (graph (let-bindings x))))]
100 ; create a new env with tvars for each component
102 ; scc-env = ((x . t0) (y . t1))
106 (env-insert acc c (fresh-tvar)))
108 ; typecheck each component
112 (let ([body (cadr (assoc c (let-bindings x)))])
113 (check scc-env body)))
115 ; collect all the constraints in the scc
121 ; unify with tvars from scc-env
123 (~ (env-lookup scc-env c) (cadr res))
126 '() type-results comps)]
127 ; substitute *only* the bindings in this scc
130 (if (memv (car x) comps)
131 (cons (car x) (substitute cs (cdr x)))
135 [new-env (fold-left process-component env components)])
136 (check new-env (last (let-body x)))))
138 (define (check env x)
139 ;; (display "check: ")
147 ('int-literal (list '() 'Int))
148 ('bool-literal (list '() 'Bool))
149 ('string-literal (list '() 'String))
150 ('builtin (list '() (builtin-type x)))
153 (let* ((cond-type-res (check env (cadr x)))
154 (then-type-res (check env (caddr x)))
155 (else-type-res (check env (cadddr x)))
156 (then-eq-else-cs (~ (cadr then-type-res)
157 (cadr else-type-res)))
158 (cs (constraint-merge
160 (constraint-merge (~ (cadr cond-type-res) 'Bool)
161 (constraint-merge (car else-type-res)
163 (return-type (substitute cs (cadr then-type-res))))
164 (list cs return-type)))
166 ('var (list '() (env-lookup env x)))
167 ('let (check-let env x))
171 (let* [(new-env (env-insert env (lambda-arg x) (fresh-tvar)))
173 (body-type-res (check new-env (lambda-body x)))
174 (cs (car body-type-res))
175 (subd-env (substitute-env (car body-type-res) new-env))
176 (arg-type (env-lookup subd-env (lambda-arg x)))
177 (resolved-arg-type (substitute cs arg-type))]
178 ;; (display "lambda:\n\t")
183 ;; (display (format "subd-env: ~a\n" subd-env))
184 ;; (display resolved-arg-type)
186 (list (car body-type-res)
189 (cadr body-type-res)))))
192 (if (eqv? (car x) (cadr x))
193 ; recursive function (f f)
194 (let* [(func-type (env-lookup env (car x)))
195 (return-type (fresh-tvar))
196 (other-func-type `(abs ,func-type ,return-type))
197 (cs (~ func-type other-func-type))
198 (resolved-return-type (substitute cs return-type))]
199 (list cs resolved-return-type)))
202 (let* ((arg-type-res (check env (cadr x)))
203 (arg-type (cadr arg-type-res))
204 (func-type-res (check env (car x)))
205 (func-type (cadr func-type-res))
209 (substitute (car arg-type-res) func-type)
210 `(abs ,arg-type ,(fresh-tvar))))
211 (cs (constraint-merge
212 (constraint-merge func-c (car arg-type-res))
213 (car func-type-res)))
215 (resolved-func-type (substitute cs func-type))
216 (resolved-return-type (caddr resolved-func-type)))
217 ;; (display "app:\n")
220 ;; (display func-type)
222 ;; (display resolved-func-type)
224 ;; (display arg-type-res)
226 (if (abs? resolved-func-type)
227 (let ((return-type (substitute cs (caddr resolved-func-type))))
228 (list cs return-type))
229 (error #f "not a function")))))))
230 ;; (display "result of ")
233 ;; (display (pretty-type (cadr res)))
235 ;; (display (pretty-constraints (car res)))
239 ; we typecheck the lambda calculus only (only single arg lambdas)
240 (define (typecheck prog)
242 (let ([init-env (flat-map data-tors (program-datas prog))])
245 (cadr (check init-env (normalize (program-body prog))))))
248 ; returns a list of constraints
250 (let ([res (unify? a b)])
254 (format "couldn't unify ~a ~~ ~a" a b)))))
257 (cond [(eq? a b) '()]
258 [(tvar? a) (list (cons a b))]
259 [(tvar? b) (list (cons b a))]
260 [(and (abs? a) (abs? b))
261 (let* [(arg-cs (unify? (cadr a) (cadr b)))
262 (body-cs (unify? (substitute arg-cs (caddr a))
263 (substitute arg-cs (caddr b))))]
264 (constraint-merge body-cs arg-cs))]
267 (define (substitute cs t)
273 [(abs? t) `(abs ,(substitute cs (cadr t))
274 ,(substitute cs (caddr t)))]
277 ; applies substitutions to all variables in environment
278 (define (substitute-env cs env)
279 (map (lambda (x) (cons (car x) (substitute cs (cdr x)))) env))
281 ; composes constraints a onto b and merges, i.e. applies a to b
282 ; a should be the "more important" constraints
283 (define (constraint-merge a b)
284 (define (f cs constraint)
285 (cons (car constraint)
286 (substitute cs (cdr constraint))))
288 (define (most-concrete a b)
292 [(and (abs? a) (abs? b))
293 `(abs ,(most-concrete (cadr a) (cadr b))
294 ,(most-concrete (caddr a) (caddr b)))]
299 ; for any two constraints that clash, e.g. t1 ~ abs t2 t3
300 ; and t1 ~ abs int t3
301 ; prepend the most concrete version of the type to the
302 ; list of constraints
305 (if (assoc (car x) a)
306 (cons (cons (car x) (most-concrete (cdr (assoc (car x) a))
310 (fold-left gen '() b))
313 (append (filter (lambda (x) (not (assoc (car x) p)))
316 (append (clashes) (union a (map (lambda (z) (f a z)) b))))
319 ;; ; a1 -> a2 ~ a3 -> a4;
320 ;; ; a1 -> a2 !~ Bool -> Bool
321 ;; ; basically can the tvars be renamed
322 (define (types-equal? x y)
323 (let ([cs (unify? x y)])
326 ([test (lambda (acc c)
328 (tvar? (car c)) ; the only substitutions allowed are tvar -> tvar
330 (fold-left test #t cs)))))
332 ; input: a list of binds ((x . y) (y . 3))
333 ; returns: pair of verts, edges ((x y) . (x . y))
335 (define (go bs orig-bs)
336 (define (find-refs prog)
340 ; only count a reference if its a binding
341 ['var (if (assoc x orig-bs) (list x) '())]
346 (let* [(bind (car bs))
349 (refs (find-refs (cdr bind)))
350 (edges (map (lambda (x) (cons vert x))
353 (rest (if (null? (cdr bs))
355 (go (cdr bs) orig-bs)))
356 (total-verts (cons vert (car rest)))
357 (total-edges (append edges (cdr rest)))]
358 (cons total-verts total-edges))))
361 (define (successors graph v)
365 (if (eqv? v (caar E))
366 (cons (cdar E) (go v (cdr E)))
370 ; takes in a graph (pair of vertices, edges)
371 ; returns a list of strongly connected components
373 ; ((x y w) . ((x . y) (x . w) (w . x))
383 ; this uses tarjan's algorithm, to get reverse
384 ; topological sorting for free
387 (let* ([indices (make-hash-table)]
388 [lowlinks (make-hash-table)]
389 [on-stack (make-hash-table)]
395 (get-hash-table indices v #f))
397 (get-hash-table lowlinks v #f))
403 (put-hash-table! indices v current)
404 (put-hash-table! lowlinks v current)
405 (set! current (+ current 1))
407 (put-hash-table! on-stack v #t)
411 (if (not (hashtable-contains? indices w))
412 ; successor w has not been visited, recurse
415 (put-hash-table! lowlinks
417 (min (lowlink v) (lowlink w))))
418 ; successor w has been visited
419 (when (get-hash-table on-stack w #f)
420 (put-hash-table! lowlinks v (min (lowlink v) (index w))))))
421 (successors graph v))
423 (when (= (index v) (lowlink v))
426 (let ([w (pop! stack)])
427 (put-hash-table! on-stack w #f)
430 (cons w (new-scc)))))])
431 (set! result (cons scc result))))))])
434 (when (not (hashtable-contains? indices v)) ; v.index == -1