(load "ast.scm")
+
+(define (abs? t)
+ (and (list? t) (eq? (car t) 'abs)))
+
+(define (tvar? t)
+ (and (not (list? t)) (not (concrete? t)) (symbol? t)))
+
+(define (concrete? t)
+ (case t
+ ('int #t)
+ ('bool #t)
+ (else #f)))
+
+(define (pretty-type t)
+ (cond ((abs? t)
+ (string-append
+ (if (abs? (cadr t))
+ (string-append "(" (pretty-type (cadr t)) ")")
+ (pretty-type (cadr t)))
+ " -> "
+ (pretty-type (caddr t))))
+ (else (symbol->string t))))
+
; ('a, ('b, 'a))
(define (env-lookup env n)
(if (null? env) (error #f "empty env") ; it's a type equality
(define (normalize prog) ; (+ a b) -> ((+ a) b)
- (cond
+ (case (ast-type prog)
+ ('lambda
; (lambda (x y) (+ x y)) -> (lambda (x) (lambda (y) (+ x y)))
- ((lambda? prog)
(if (> (length (lambda-args prog)) 1)
(list 'lambda (list (car (lambda-args prog)))
(normalize (list 'lambda (cdr (lambda-args prog)) (caddr prog))))
(list 'lambda (lambda-args prog) (normalize (caddr prog)))))
- ((app? prog)
+ ('app
(if (null? (cddr prog))
- (cons (normalize (car prog)) (normalize (cdr prog))) ; (f a)
- (list (list (normalize (car prog)) (normalize (cadr prog))) (normalize (caddr prog))))) ; (f a b)
- ((let? prog)
+ `(,(normalize (car prog)) ,(normalize (cadr prog))) ; (f a)
+ `(,(list (normalize (car prog)) (normalize (cadr prog)))
+ ,(normalize (caddr prog))))) ; (f a b)
+ ;; (list (list (normalize (car prog))
+ ;; (normalize (cadr prog))) (normalize (caddr prog))))) ; (f a b)
+ ('let
(append (list 'let
- (map (lambda (x) (cons (car x) (normalize (cdr x))))
+ (map (lambda (x) `(,(car x) ,(normalize (cadr x))))
(let-bindings prog)))
(map normalize (let-body prog))))
+ ('if `(if ,(normalize (cadr prog))
+ ,(normalize (caddr prog))
+ ,(normalize (cadddr prog))))
(else prog)))
+(define (builtin-type x)
+ (case x
+ ('+ '(abs int (abs int int)))
+ ('- '(abs int (abs int int)))
+ ('* '(abs int (abs int int)))
+ ('! '(abs bool bool))
+ ('= '(abs int (abs int bool)))
+ ('bool->int '(abs bool int))
+ (else #f)))
; we typecheck the lambda calculus only (only single arg lambdas)
(define (typecheck prog)
(define (check env x)
- (display "check: ")
- (display x)
- (display "\n\t")
- (display env)
- (newline)
+ ;; (display "check: ")
+ ;; (display x)
+ ;; (display "\n\t")
+ ;; (display env)
+ ;; (newline)
(let
((res
- (cond
- ((integer? x) (list '() 'int))
- ((boolean? x) (list '() 'bool))
- ((eq? x 'inc) (list '() '(abs int int)))
- ((eq? x '+) (list '() '(abs int (abs int int))))
- ((symbol? x) (list '() (env-lookup env x)))
+ (case (ast-type x)
+ ('int-literal (list '() 'int))
+ ('bool-literal (list '() 'bool))
+ ('builtin (list '() (builtin-type x)))
+
+ ('if
+ (let* ((cond-type-res (check env (cadr x)))
+ (then-type-res (check env (caddr x)))
+ (else-type-res (check env (cadddr x)))
+ (then-eq-else-cs (unify (cadr then-type-res)
+ (cadr else-type-res)))
+ (cs (consolidate
+ (car then-type-res)
+ (consolidate (car else-type-res)
+ then-eq-else-cs)))
+ (return-type (substitute cs (cadr then-type-res))))
+ (when (not (eqv? (cadr cond-type-res) 'bool))
+ (error #f "if condition isn't bool"))
+ (list cs return-type)))
- ((let? x)
+ ('var (list '() (env-lookup env x)))
+ ('let
(let ((new-env (fold-left
(lambda (acc bind)
(let ((t (check
(check new-env (last (let-body x)))))
- ((lambda? x)
+ ('lambda
(let* ((new-env (env-insert env (lambda-arg x) (fresh-tvar)))
(body-type-res (check new-env (lambda-body x)))
- (subd-env (substitute-env (car body-type-res) new-env)))
- (display "lambda: ")
- (display body-type-res)
- (display "\n")
- (display subd-env)
- (display "\n")
+ (cs (car body-type-res))
+ (subd-env (substitute-env (car body-type-res) new-env))
+ (arg-type (env-lookup subd-env (lambda-arg x)))
+ (resolved-arg-type (substitute cs arg-type)))
+ ;; (display "lambda:\n\t")
+ ;; (display prog)
+ ;; (display "\n\t")
+ ;; (display cs)
+ ;; (display "\n\t")
+ ;; (display resolved-arg-type)
+ ;; (newline)
(list (car body-type-res)
(list 'abs
- (env-lookup subd-env (lambda-arg x))
+ resolved-arg-type
(cadr body-type-res)))))
- ((app? x) ; (f a)
+ ('app ; (f a)
(let* ((arg-type-res (check env (cadr x)))
(arg-type (cadr arg-type-res))
(func-type-res (check env (car x)))
(list 'abs
arg-type
(fresh-tvar))))
- (cs (append func-c (car arg-type-res) (car func-type-res)))
+ (cs (consolidate
+ (consolidate func-c (car arg-type-res))
+ (car func-type-res)))
(resolved-func-type (substitute cs func-type))
(resolved-return-type (caddr resolved-func-type)))
(let ((return-type (substitute cs (caddr resolved-func-type))))
(list cs return-type))
(error #f "not a function")))))))
- (display "result of ")
- (display x)
- (display ":\n\t")
- (display (cadr res))
- (display "[")
- (display (car res))
- (display "]\n")
+ ;; (display "result of ")
+ ;; (display x)
+ ;; (display ":\n\t")
+ ;; (display (cadr res))
+ ;; (display "[")
+ ;; (display (car res))
+ ;; (display "]\n")
res))
(cadr (check '() (normalize prog))))
-
-(define (abs? t)
- (and (list? t) (eq? (car t) 'abs)))
-
-(define (tvar? t)
- (and (not (list? t)) (not (concrete? t)) (symbol? t)))
-
-(define (concrete? t)
- (case t
- ('int #t)
- ('bool #t)
- (else #f)))
-
; returns a list of pairs of constraints
(define (unify a b)
(cond ((eq? a b) '())
(unify (caddr a) (caddr b))))
(else (error #f "could not unify"))))
-
; TODO: what's the most appropriate substitution?
; should all constraints just be limited to a pair?
(define (substitute cs t)
; gets the first concrete type
; otherwise returns the last type variable
+
(define (get-concrete c)
- (if (null? (cdr c))
- (car c)
+ (let ((last (null? (cdr c))))
(if (not (tvar? (car c)))
+ (if (abs? (car c))
+ (substitute cs (car c))
+ (car c))
+ (if last
(car c)
- (get-concrete (cdr c)))))
+ (get-concrete (cdr c))))))
+ (cond
+ ((abs? t) (list 'abs
+ (substitute cs (cadr t))
+ (substitute cs (caddr t))))
+ (else
(fold-left
(lambda (t c)
(if (member t c)
(get-concrete c)
t))
- t cs))
+ t cs))))
(define (substitute-env cs env)
(map (lambda (x) (cons (car x) (substitute cs (cdr x)))) env))