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 `(,(list (normalize (car prog)) (normalize (cadr prog)))
62 ,(normalize (caddr 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 (let ((new-env (fold-left
117 (env-insert acc (car bind) (fresh-tvar))
119 (env-insert acc (car bind) (cadr t))))
120 env (let-bindings x))))
121 (check new-env (last (let-body x)))))
125 (let* ((new-env (env-insert env (lambda-arg x) (fresh-tvar)))
126 (body-type-res (check new-env (lambda-body x)))
127 (cs (car body-type-res))
128 (subd-env (substitute-env (car body-type-res) new-env))
129 (arg-type (env-lookup subd-env (lambda-arg x)))
130 (resolved-arg-type (substitute cs arg-type)))
131 ;; (display "lambda:\n\t")
136 ;; (display resolved-arg-type)
138 (list (car body-type-res)
141 (cadr body-type-res)))))
144 (let* ((arg-type-res (check env (cadr x)))
145 (arg-type (cadr arg-type-res))
146 (func-type-res (check env (car x)))
147 (func-type (cadr func-type-res))
150 (func-c (unify func-type
155 (consolidate func-c (car arg-type-res))
156 (car func-type-res)))
158 (resolved-func-type (substitute cs func-type))
159 (resolved-return-type (caddr resolved-func-type)))
160 ;; (display "app:\n")
163 ;; (display func-type)
165 ;; (display resolved-func-type)
167 ;; (display arg-type-res)
169 (if (abs? resolved-func-type)
170 (let ((return-type (substitute cs (caddr resolved-func-type))))
171 (list cs return-type))
172 (error #f "not a function")))))))
173 ;; (display "result of ")
176 ;; (display (cadr res))
178 ;; (display (car res))
181 (cadr (check '() (normalize prog))))
183 ; returns a list of pairs of constraints
185 (cond ((eq? a b) '())
186 ((or (tvar? a) (tvar? b)) (~ a b))
187 ((and (abs? a) (abs? b))
188 (consolidate (unify (cadr a) (cadr b))
189 (unify (caddr a) (caddr b))))
190 (else (error #f "could not unify"))))
192 ; TODO: what's the most appropriate substitution?
193 ; should all constraints just be limited to a pair?
194 (define (substitute cs t)
195 ; gets the first concrete type
196 ; otherwise returns the last type variable
198 (define (get-concrete c)
199 (let ((last (null? (cdr c))))
200 (if (not (tvar? (car c)))
202 (substitute cs (car c))
206 (get-concrete (cdr c))))))
209 (substitute cs (cadr t))
210 (substitute cs (caddr t))))
219 (define (substitute-env cs env)
220 (map (lambda (x) (cons (car x) (substitute cs (cdr x)))) env))
225 (define (consolidate x y)
229 (else (if (member (car b) a)
231 (cons (car b) (merge a (cdr b)))))))
232 (define (overlap? a b)
233 (if (or (null? a) (null? b))
235 (if (fold-left (lambda (acc v)
236 (or acc (eq? v (car a))))
239 (overlap? (cdr a) b))))
243 (else (let* ((a (car y))
253 (filter (lambda (b) (not (eq? b (cdr merged)))) x)
256 (consolidate removed (cons (car merged) (cdr y)))
257 (consolidate (cons a x) (cdr y)))))))