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)
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))))]
125 (env-insert acc c (fresh-tvar)))
130 (begin (display scc-env) (newline)
131 (let ([body (cadr (assoc c (let-bindings x)))])
132 (display body)(newline)(check scc-env body))))
139 (unify (cadr res) (env-lookup scc-env c))))
140 '() type-results comps)])
141 (display "process-component env:\n")
142 (display (substitute-env cs scc-env))
144 (substitute-env cs scc-env)))]
145 [new-env (fold-left process-component env components)])
146 (check new-env (last (let-body x)))))
148 ;; (let ((new-env (fold-left
149 ;; (lambda (acc bind)
150 ;; (let* [(bind-tvar (fresh-tvar))
151 ;; (env-with-tvar (env-insert acc (car bind) bind-tvar))
152 ;; (bind-res (check env-with-tvar (cadr bind)))
153 ;; (bind-type (cadr bind-res))
154 ;; (cs (consolidate (car bind-res)
155 ;; (unify bind-type bind-tvar)))]
156 ;; (substitute-env cs env-with-tvar)))
157 ;; env (let-bindings x))))
158 ;; (display "sccs of graph\n")
159 ;; (display (sccs (graph (let-bindings x))))
161 ;; (display "env when checking body:\n\t")
164 ;; (check new-env (last (let-body x)))))
168 (let* [(new-env (env-insert env (lambda-arg x) (fresh-tvar)))
170 (body-type-res (check new-env (lambda-body x)))
171 (cs (car body-type-res))
172 (subd-env (substitute-env (car body-type-res) new-env))
173 (arg-type (env-lookup subd-env (lambda-arg x)))
174 (resolved-arg-type (substitute cs arg-type))]
175 ;; (display "lambda:\n\t")
180 ;; (display resolved-arg-type)
182 (list (car body-type-res)
185 (cadr body-type-res)))))
188 (if (eqv? (car x) (cadr x))
189 ; recursive function (f f)
190 (let* [(func-type (env-lookup env (car x)))
191 (return-type (fresh-tvar))
192 (other-func-type `(abs ,func-type ,return-type))
193 (cs (unify func-type other-func-type))]
194 (list cs return-type))
197 (let* ((arg-type-res (check env (cadr x)))
198 (arg-type (cadr arg-type-res))
199 (func-type-res (check env (car x)))
200 (func-type (cadr func-type-res))
203 (func-c (unify func-type
208 (consolidate func-c (car arg-type-res))
209 (car func-type-res)))
211 (resolved-func-type (substitute cs func-type))
212 (resolved-return-type (caddr resolved-func-type)))
213 ;; (display "app:\n")
216 ;; (display func-type)
218 ;; (display resolved-func-type)
220 ;; (display arg-type-res)
222 (if (abs? resolved-func-type)
223 (let ((return-type (substitute cs (caddr resolved-func-type))))
224 (list cs return-type))
225 (error #f "not a function"))))))))
226 (display "result of ")
229 (display (pretty-type (cadr res)))
234 (cadr (check '() (normalize prog))))
236 ; returns a list of pairs of constraints
238 (cond ((eq? a b) '())
239 ((or (tvar? a) (tvar? b)) (~ a b))
240 ((and (abs? a) (abs? b))
241 (let* [(arg-cs (unify (cadr a) (cadr b)))
242 (body-cs (unify (substitute arg-cs (caddr a))
243 (substitute arg-cs (caddr b))))]
244 (consolidate arg-cs body-cs)))
245 (else (error #f "could not unify"))))
247 ; TODO: what's the most appropriate substitution?
248 ; should all constraints just be limited to a pair?
249 (define (substitute cs t)
250 ; gets the first concrete type
251 ; otherwise returns the last type variable
255 (filter (lambda (x) (not (eqv? t x))) c))
258 (define (get-concrete c)
259 (let [(last (null? (cdr c)))]
260 (if (not (tvar? (car c)))
262 (substitute cs-without-t (car c))
266 (get-concrete (cdr c))))))
270 (substitute cs (cadr t))
271 (substitute cs (caddr t))))
280 (define (substitute-env cs env)
281 (map (lambda (x) (cons (car x) (substitute cs (cdr x)))) env))
286 (define (consolidate x y)
290 (else (if (member (car b) a)
292 (cons (car b) (merge a (cdr b)))))))
293 (define (overlap? a b)
294 (if (or (null? a) (null? b))
296 (if (fold-left (lambda (acc v)
297 (or acc (eq? v (car a))))
300 (overlap? (cdr a) b))))
315 (filter (lambda (b) (not (eq? b (cdr merged)))) x)
318 (consolidate removed (cons (car merged) (cdr y)))
319 (consolidate (cons a x) (cdr y)))))))
321 ; a1 -> a2 ~ a3 -> a4;
322 ; a1 -> a2 !~ bool -> bool
323 ; basically can the tvars be renamed
324 (define (types-equal? x y)
327 ; input: a list of binds ((x . y) (y . 3))
328 ; returns: pair of verts, edges ((x y) . (x . y))
330 (define (find-refs prog)
334 ; only count a reference if its a binding
335 ['var (if (assoc x bs) (list x) '())]
338 (let* [(bind (car bs))
341 (refs (find-refs (cdr bind)))
342 (edges (map (lambda (x) (cons vert x))
345 (rest (if (null? (cdr bs))
348 (total-verts (cons vert (car rest)))
349 (total-edges (append edges (cdr rest)))]
350 (cons total-verts total-edges)))
352 (define (successors graph v)
356 (if (eqv? v (caar E))
357 (cons (cdar E) (go v (cdr E)))
361 ; takes in a graph (pair of vertices, edges)
362 ; returns a list of strongly connected components
364 ; ((x y w) . ((x . y) (x . w) (w . x))
374 ; this uses tarjan's algorithm, to get reverse
375 ; topological sorting for free
378 (let* ([indices (make-hash-table)]
379 [lowlinks (make-hash-table)]
380 [on-stack (make-hash-table)]
386 (get-hash-table indices v #f))
388 (get-hash-table lowlinks v #f))
394 (put-hash-table! indices v current)
395 (put-hash-table! lowlinks v current)
396 (set! current (+ current 1))
398 (put-hash-table! on-stack v #t)
402 (if (not (hashtable-contains? indices w))
403 ; successor w has not been visited, recurse
406 (put-hash-table! lowlinks
408 (min (lowlink v) (lowlink w))))
409 ; successor w has been visited
410 (when (get-hash-table on-stack w #f)
411 (put-hash-table! lowlinks v (min (lowlink v) (index w))))))
412 (successors graph v))
414 (when (= (index v) (lowlink v))
417 (let ([w (pop! stack)])
418 (put-hash-table! on-stack w #f)
421 (cons w (new-scc)))))])
422 (set! result (cons scc result))))))])
426 (when (not (hashtable-contains? indices v)) ; v.index == -1