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))))))
50 (define (normalize prog) ; (+ a b) -> ((+ a) b)
52 ; (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)
63 ;; (list (list (normalize (car prog))
64 ;; (normalize (cadr prog))) (normalize (caddr prog))))) ; (f a b)
67 (map (lambda (x) `(,(car x) ,(normalize (cadr x))))
69 (map normalize (let-body prog))))
72 (define (builtin-type x)
74 ('+ '(abs int (abs int int)))
75 ('- '(abs int (abs int int)))
76 ('* '(abs int (abs int int)))
78 ('bool->int '(abs bool int))
81 ; we typecheck the lambda calculus only (only single arg lambdas)
82 (define (typecheck prog)
84 ;; (display "check: ")
92 ((integer? x) (list '() 'int))
93 ((boolean? x) (list '() 'bool))
94 ((builtin-type x) (list '() (builtin-type x)))
95 ((symbol? x) (list '() (env-lookup env x)))
97 (let ((new-env (fold-left
100 (env-insert acc (car bind) (fresh-tvar))
102 (env-insert acc (car bind) (cadr t))))
103 env (let-bindings x))))
104 (check new-env (last (let-body x)))))
108 (let* ((new-env (env-insert env (lambda-arg x) (fresh-tvar)))
109 (body-type-res (check new-env (lambda-body x)))
110 (cs (car body-type-res))
111 (subd-env (substitute-env (car body-type-res) new-env))
112 (arg-type (env-lookup subd-env (lambda-arg x)))
113 (resolved-arg-type (substitute cs arg-type)))
114 ;; (display "lambda:\n\t")
119 ;; (display resolved-arg-type)
121 (list (car body-type-res)
124 (cadr body-type-res)))))
127 (let* ((arg-type-res (check env (cadr x)))
128 (arg-type (cadr arg-type-res))
129 (func-type-res (check env (car x)))
130 (func-type (cadr func-type-res))
133 (func-c (unify func-type
138 (consolidate func-c (car arg-type-res))
139 (car func-type-res)))
141 (resolved-func-type (substitute cs func-type))
142 (resolved-return-type (caddr resolved-func-type)))
143 ;; (display "app:\n")
146 ;; (display func-type)
148 ;; (display resolved-func-type)
150 ;; (display arg-type-res)
152 (if (abs? resolved-func-type)
153 (let ((return-type (substitute cs (caddr resolved-func-type))))
154 (list cs return-type))
155 (error #f "not a function")))))))
156 ;; (display "result of ")
159 ;; (display (cadr res))
161 ;; (display (car res))
164 (cadr (check '() (normalize prog))))
166 ; returns a list of pairs of constraints
168 (cond ((eq? a b) '())
169 ((or (tvar? a) (tvar? b)) (~ a b))
170 ((and (abs? a) (abs? b))
171 (consolidate (unify (cadr a) (cadr b))
172 (unify (caddr a) (caddr b))))
173 (else (error #f "could not unify"))))
175 ; TODO: what's the most appropriate substitution?
176 ; should all constraints just be limited to a pair?
177 (define (substitute cs t)
178 ; gets the first concrete type
179 ; otherwise returns the last type variable
181 (define (get-concrete c)
182 (let ((last (null? (cdr c))))
183 (if (not (tvar? (car c)))
185 (substitute cs (car c))
189 (get-concrete (cdr c))))))
192 (substitute cs (cadr t))
193 (substitute cs (caddr t))))
202 (define (substitute-env cs env)
203 (map (lambda (x) (cons (car x) (substitute cs (cdr x)))) env))
208 (define (consolidate x y)
212 (else (if (member (car b) a)
214 (cons (car b) (merge a (cdr b)))))))
215 (define (overlap? a b)
216 (if (or (null? a) (null? b))
218 (if (fold-left (lambda (acc v)
219 (or acc (eq? v (car a))))
222 (overlap? (cdr a) b))))
226 (else (let* ((a (car y))
236 (filter (lambda (b) (not (eq? b (cdr merged)))) x)
239 (consolidate removed (cons (car merged) (cdr y)))
240 (consolidate (cons a x) (cdr y)))))))