4 (and (list? t) (eq? (car t) 'abs)))
7 (and (not (list? t)) (not (concrete? t)) (symbol? t)))
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))))
26 (define (env-lookup env n)
27 (if (null? env) (error #f "empty env") ; it's a type equality
28 (if (eq? (caar env) n)
30 (env-lookup (cdr env) n))))
32 (define (env-insert env n t)
33 (cons (cons n t) env))
40 (set! cur-tvar (+ cur-tvar 1))
42 (string-append "t" (number->string (- cur-tvar 1))))))
49 (define (normalize prog) ; (+ a b) -> ((+ a) b)
52 ; (lambda (x y) (+ x y)) -> (lambda (x) (lambda (y) (+ x y)))
53 (if (> (length (lambda-args prog)) 1)
54 (list 'lambda (list (car (lambda-args prog)))
55 (normalize (list 'lambda (cdr (lambda-args prog)) (caddr prog))))
56 (list 'lambda (lambda-args prog) (normalize (caddr prog)))))
58 (if (null? (cddr prog))
59 `(,(normalize (car prog)) ,(normalize (cadr prog))) ; (f a)
60 `(,(list (normalize (car prog)) (normalize (cadr prog)))
61 ,(normalize (caddr prog))))) ; (f a b)
64 (map (lambda (x) `(,(car x) ,(normalize (cadr x))))
66 (map normalize (let-body prog))))
67 (else (ast-traverse normalize prog))))
69 (define (builtin-type x)
71 ('+ '(abs int (abs int int)))
72 ('- '(abs int (abs int int)))
73 ('* '(abs int (abs int int)))
75 ('= '(abs int (abs int bool)))
76 ('bool->int '(abs bool int))
79 ; we typecheck the lambda calculus only (only single arg lambdas)
80 (define (typecheck prog)
82 ;; (display "check: ")
90 ('int-literal (list '() 'int))
91 ('bool-literal (list '() 'bool))
92 ('builtin (list '() (builtin-type x)))
95 (let* ((cond-type-res (check env (cadr x)))
96 (then-type-res (check env (caddr x)))
97 (else-type-res (check env (cadddr x)))
98 (then-eq-else-cs (unify (cadr then-type-res)
99 (cadr else-type-res)))
102 (consolidate (car else-type-res)
104 (return-type (substitute cs (cadr then-type-res))))
105 (when (not (eqv? (cadr cond-type-res) 'bool))
106 (error #f "if condition isn't bool"))
107 (list cs return-type)))
109 ('var (list '() (env-lookup env x)))
111 (let ((new-env (fold-left
114 (env-insert acc (car bind) (fresh-tvar))
116 (env-insert acc (car bind) (cadr t))))
117 env (let-bindings x))))
118 (check new-env (last (let-body x)))))
122 (let* ((new-env (env-insert env (lambda-arg x) (fresh-tvar)))
123 (body-type-res (check new-env (lambda-body x)))
124 (cs (car body-type-res))
125 (subd-env (substitute-env (car body-type-res) new-env))
126 (arg-type (env-lookup subd-env (lambda-arg x)))
127 (resolved-arg-type (substitute cs arg-type)))
128 ;; (display "lambda:\n\t")
133 ;; (display resolved-arg-type)
135 (list (car body-type-res)
138 (cadr body-type-res)))))
141 (let* ((arg-type-res (check env (cadr x)))
142 (arg-type (cadr arg-type-res))
143 (func-type-res (check env (car x)))
144 (func-type (cadr func-type-res))
147 (func-c (unify func-type
152 (consolidate func-c (car arg-type-res))
153 (car func-type-res)))
155 (resolved-func-type (substitute cs func-type))
156 (resolved-return-type (caddr resolved-func-type)))
157 ;; (display "app:\n")
160 ;; (display func-type)
162 ;; (display resolved-func-type)
164 ;; (display arg-type-res)
166 (if (abs? resolved-func-type)
167 (let ((return-type (substitute cs (caddr resolved-func-type))))
168 (list cs return-type))
169 (error #f "not a function")))))))
170 ;; (display "result of ")
173 ;; (display (cadr res))
175 ;; (display (car res))
178 (cadr (check '() (normalize prog))))
180 ; returns a list of pairs of constraints
182 (cond ((eq? a b) '())
183 ((or (tvar? a) (tvar? b)) (~ a b))
184 ((and (abs? a) (abs? b))
185 (consolidate (unify (cadr a) (cadr b))
186 (unify (caddr a) (caddr b))))
187 (else (error #f "could not unify"))))
189 ; TODO: what's the most appropriate substitution?
190 ; should all constraints just be limited to a pair?
191 (define (substitute cs t)
192 ; gets the first concrete type
193 ; otherwise returns the last type variable
195 (define (get-concrete c)
196 (let ((last (null? (cdr c))))
197 (if (not (tvar? (car c)))
199 (substitute cs (car c))
203 (get-concrete (cdr c))))))
206 (substitute cs (cadr t))
207 (substitute cs (caddr t))))
216 (define (substitute-env cs env)
217 (map (lambda (x) (cons (car x) (substitute cs (cdr x)))) env))
222 (define (consolidate x y)
226 (else (if (member (car b) a)
228 (cons (car b) (merge a (cdr b)))))))
229 (define (overlap? a b)
230 (if (or (null? a) (null? b))
232 (if (fold-left (lambda (acc v)
233 (or acc (eq? v (car a))))
236 (overlap? (cdr a) b))))
240 (else (let* ((a (car y))
250 (filter (lambda (b) (not (eq? b (cdr merged)))) x)
253 (consolidate removed (cons (car merged) (cdr y)))
254 (consolidate (cons a x) (cdr y)))))))