4 (and (list? t) (eq? (car t) 'abs)))
7 (and (not (list? t)) (not (concrete? t)) (symbol? t)))
16 (define (pretty-type t)
20 (string-append "(" (pretty-type (cadr t)) ")")
21 (pretty-type (cadr t)))
23 (pretty-type (caddr t))))
24 (else (symbol->string t))))
27 (define (env-lookup env n)
28 (if (null? env) (error #f "empty env") ; it's a type equality
29 (if (eq? (caar env) n)
31 (env-lookup (cdr env) n))))
33 (define (env-insert env n t)
34 (cons (cons n t) env))
41 (set! cur-tvar (+ cur-tvar 1))
43 (string-append "t" (number->string (- cur-tvar 1))))))
50 (define (normalize prog) ; (+ a b) -> ((+ a) b)
53 ; (lambda (x y) (+ x y)) -> (lambda (x) (lambda (y) (+ x y)))
54 (if (> (length (lambda-args prog)) 1)
55 (list 'lambda (list (car (lambda-args prog)))
56 (normalize (list 'lambda (cdr (lambda-args prog)) (caddr prog))))
57 (list 'lambda (lambda-args prog) (normalize (caddr prog)))))
59 (if (null? (cddr prog))
60 `(,(normalize (car prog)) ,(normalize (cadr prog))) ; (f a)
61 (normalize `(,(list (normalize (car prog)) (normalize (cadr prog)))
62 ,@(cddr prog))))) ; (f a b)
65 (map (lambda (x) `(,(car x) ,(normalize (cadr x))))
67 (map normalize (let-body prog))))
68 (else (ast-traverse normalize prog))))
70 (define (builtin-type x)
72 ('+ '(abs int (abs int int)))
73 ('- '(abs int (abs int int)))
74 ('* '(abs int (abs int int)))
76 ('= '(abs int (abs int bool)))
77 ('bool->int '(abs bool int))
78 ('print '(abs string void))
81 ; we typecheck the lambda calculus only (only single arg lambdas)
82 (define (typecheck prog)
84 ;; (display "check: ")
92 ('int-literal (list '() 'int))
93 ('bool-literal (list '() 'bool))
94 ('string-literal (list '() 'string))
95 ('builtin (list '() (builtin-type x)))
98 (let* ((cond-type-res (check env (cadr x)))
99 (then-type-res (check env (caddr x)))
100 (else-type-res (check env (cadddr x)))
101 (then-eq-else-cs (unify (cadr then-type-res)
102 (cadr else-type-res)))
105 (consolidate (car else-type-res)
107 (return-type (substitute cs (cadr then-type-res))))
108 (when (not (eqv? (cadr cond-type-res) 'bool))
109 (error #f "if condition isn't bool"))
110 (list cs return-type)))
112 ('var (list '() (env-lookup env x)))
114 ; takes in the current environment and a scc
115 ; returns new environment with scc's types added in
116 (let* ([components (reverse (sccs (graph (let-bindings x))))]
120 ; create a new env with tvars for each component
122 ; scc-env = ((x . t0) (y . t1))
126 (env-insert acc c (fresh-tvar)))
128 ; typecheck each component
132 (let ([body (cadr (assoc c (let-bindings x)))])
133 (check scc-env body)))
135 ; collect all the constraints in the scc
141 (consolidate (car res)
142 ; unify with tvars from scc-env
144 (unify (cadr res) (env-lookup scc-env c)))))
145 '() type-results comps)]
146 ; substitute *only* the bindings in this scc
149 (if (memv (car x) comps)
150 (cons (car x) (substitute cs (cdr x)))
154 [new-env (fold-left process-component env components)])
155 (check new-env (last (let-body x)))))
158 (let* [(new-env (env-insert env (lambda-arg x) (fresh-tvar)))
160 (body-type-res (check new-env (lambda-body x)))
161 (cs (car body-type-res))
162 (subd-env (substitute-env (car body-type-res) new-env))
163 (arg-type (env-lookup subd-env (lambda-arg x)))
164 (resolved-arg-type (substitute cs arg-type))]
165 ;; (display "lambda:\n\t")
170 ;; (display resolved-arg-type)
172 (list (car body-type-res)
175 (cadr body-type-res)))))
178 (if (eqv? (car x) (cadr x))
179 ; recursive function (f f)
180 (let* [(func-type (env-lookup env (car x)))
181 (return-type (fresh-tvar))
182 (other-func-type `(abs ,func-type ,return-type))
183 (cs (unify func-type other-func-type))]
184 (list cs return-type))
187 (let* ((arg-type-res (check env (cadr x)))
188 (arg-type (cadr arg-type-res))
189 (func-type-res (check env (car x)))
190 (func-type (cadr func-type-res))
193 (func-c (unify func-type
198 (consolidate func-c (car arg-type-res))
199 (car func-type-res)))
201 (resolved-func-type (substitute cs func-type))
202 (resolved-return-type (caddr resolved-func-type)))
203 ;; (display "app:\n")
206 ;; (display func-type)
208 ;; (display resolved-func-type)
210 ;; (display arg-type-res)
212 (if (abs? resolved-func-type)
213 (let ((return-type (substitute cs (caddr resolved-func-type))))
214 (list cs return-type))
215 (error #f "not a function"))))))))
216 ;; (display "result of ")
219 ;; (display (pretty-type (cadr res)))
221 ;; (display (car res))
224 (cadr (check '() (normalize prog))))
226 ; returns a list of pairs of constraints
228 (let ([res (unify? a b)])
232 (format "couldn't unify ~a ~~ ~a" a b)))))
235 (cond ((eq? a b) '())
236 ((or (tvar? a) (tvar? b)) (~ a b))
237 ((and (abs? a) (abs? b))
238 (let* [(arg-cs (unify (cadr a) (cadr b)))
239 (body-cs (unify (substitute arg-cs (caddr a))
240 (substitute arg-cs (caddr b))))]
241 (consolidate arg-cs body-cs)))
244 ; TODO: what's the most appropriate substitution?
245 ; should all constraints just be limited to a pair?
246 (define (substitute cs t)
247 ; gets the first concrete type
248 ; otherwise returns the last type variable
252 (filter (lambda (x) (not (eqv? t x))) c))
255 (define (get-concrete c)
256 (let [(last (null? (cdr c)))]
257 (if (not (tvar? (car c)))
259 (substitute cs-without-t (car c))
263 (get-concrete (cdr c))))))
267 (substitute cs (cadr t))
268 (substitute cs (caddr t))))
277 (define (substitute-env cs env)
278 (map (lambda (x) (cons (car x) (substitute cs (cdr x)))) env))
283 (define (consolidate x y)
287 (else (if (member (car b) a)
289 (cons (car b) (merge a (cdr b)))))))
290 (define (overlap? a b)
291 (if (or (null? a) (null? b))
293 (if (fold-left (lambda (acc v)
294 (or acc (eq? v (car a))))
297 (overlap? (cdr a) b))))
312 (filter (lambda (b) (not (eq? b (cdr merged)))) x)
315 (consolidate removed (cons (car merged) (cdr y)))
316 (consolidate (cons a x) (cdr y)))))))
318 ; a1 -> a2 ~ a3 -> a4;
319 ; a1 -> a2 !~ bool -> bool
320 ; basically can the tvars be renamed
321 (define (types-equal? x y)
322 (let ([cs (unify? x y)])
327 (if (tvar? c) acc #f))]
328 [test (lambda (acc c)
329 (and acc (fold-left test-kind #t c)))])
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 (find-refs prog)
339 ; only count a reference if its a binding
340 ['var (if (assoc x bs) (list x) '())]
345 (let* [(bind (car bs))
348 (refs (find-refs (cdr bind)))
349 (edges (map (lambda (x) (cons vert x))
352 (rest (if (null? (cdr bs))
355 (total-verts (cons vert (car rest)))
356 (total-edges (append edges (cdr rest)))]
357 (cons total-verts total-edges))))
359 (define (successors graph v)
363 (if (eqv? v (caar E))
364 (cons (cdar E) (go v (cdr E)))
368 ; takes in a graph (pair of vertices, edges)
369 ; returns a list of strongly connected components
371 ; ((x y w) . ((x . y) (x . w) (w . x))
381 ; this uses tarjan's algorithm, to get reverse
382 ; topological sorting for free
385 (let* ([indices (make-hash-table)]
386 [lowlinks (make-hash-table)]
387 [on-stack (make-hash-table)]
393 (get-hash-table indices v #f))
395 (get-hash-table lowlinks v #f))
401 (put-hash-table! indices v current)
402 (put-hash-table! lowlinks v current)
403 (set! current (+ current 1))
405 (put-hash-table! on-stack v #t)
409 (if (not (hashtable-contains? indices w))
410 ; successor w has not been visited, recurse
413 (put-hash-table! lowlinks
415 (min (lowlink v) (lowlink w))))
416 ; successor w has been visited
417 (when (get-hash-table on-stack w #f)
418 (put-hash-table! lowlinks v (min (lowlink v) (index w))))))
419 (successors graph v))
421 (when (= (index v) (lowlink v))
424 (let ([w (pop! stack)])
425 (put-hash-table! on-stack w #f)
428 (cons w (new-scc)))))])
429 (set! result (cons scc result))))))])
432 (when (not (hashtable-contains? indices v)) ; v.index == -1