-(define (is-app? x)
- (and (list? x) (not (eq? (car x) 'lambda))))
+(load "ast.scm")
+(define (abs? t)
+ (and (list? t) (eq? (car t) 'abs)))
-(define (is-lambda? x)
- (and (list? x) (eq? (car x) 'lambda)))
+(define (tvar? t)
+ (and (not (list? t))
+ (not (concrete? t))
+ (symbol? t)))
+
+(define (concrete? t)
+ (and (symbol? t)
+ (char-upper-case? (string-ref (symbol->string t) 0))))
-(define lambda-arg cadr)
-(define lambda-body caddr)
+(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))))
+
+(define (pretty-constraints cs)
+ (string-append "{"
+ (fold-left string-append
+ ""
+ (map (lambda (c)
+ (string-append
+ (pretty-type (car c))
+ ": "
+ (pretty-type (cdr c))
+ ", "))
+ cs))
+ "}"))
; ('a, ('b, 'a))
-(define (env-lookup env x)
- (if (null? env) (error #f "empty env") ; it's a type equality
- (if (eq? (caar env) x)
+(define (env-lookup env n)
+ (if (null? env) (error #f "empty env" env n) ; it's a type equality
+ (if (eq? (caar env) n)
(cdar env)
- (env-lookup (cdr env) x))))
+ (env-lookup (cdr env) n))))
+
+(define (env-insert env n t)
+ (cons (cons n t) env))
(define abs-arg cadr)
(string->symbol
(string-append "t" (number->string (- cur-tvar 1))))))
-(define (typecheck env x)
- (display "typechecking:\n\t")
- (display x)
- (display "\t")
- (display env)
- (display "\n")
+(define (last xs)
+ (if (null? (cdr xs))
+ (car xs)
+ (last (cdr xs))))
+
+(define (normalize prog) ; (+ a b) -> ((+ a) b)
+ (case (ast-type prog)
+ ('lambda
+ ; (lambda (x y) (+ x y)) -> (lambda (x) (lambda (y) (+ x y)))
+ (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
+ (if (null? (cddr prog))
+ `(,(normalize (car prog)) ,(normalize (cadr prog))) ; (f a)
+ (normalize `(,(list (normalize (car prog)) (normalize (cadr prog)))
+ ,@(cddr prog))))) ; (f a b)
+ ('let
+ (append (list 'let
+ (map (lambda (x) `(,(car x) ,(normalize (cadr x))))
+ (let-bindings prog)))
+ (map normalize (let-body prog))))
+ (else (ast-traverse normalize 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))
+ ('print '(abs String Void))
+ (else (error #f "Couldn't find type for builtin" x))))
+
+(define (check-let env x)
+
+ ; acc is a pair of (env . annotated bindings)
+ (define (process-component acc comps)
+ (let*
+ ; create a new env with tvars for each component
+ ; e.g. scc of (x y)
+ ; scc-env = ((x . t0) (y . t1))
+ ([scc-env
+ (fold-left
+ (lambda (acc c)
+ (env-insert acc c (fresh-tvar)))
+ (car acc) comps)]
+ ; typecheck each component
+ [type-results
+ (map
+ (lambda (c)
+ (let ([body (cadr (assoc c (let-bindings x)))])
+ (check scc-env body)))
+ comps)]
+ ; collect all the constraints in the scc
+ [cs
+ (fold-left
+ (lambda (acc res c)
+ (constraint-merge
+ (constraint-merge
+ ; unify with tvars from scc-env
+ ; result ~ tvar
+ (~ (env-lookup scc-env c) (cadr res))
+ (car res))
+ acc))
+ '() type-results comps)]
+ ; substitute *only* the bindings in this scc
+ [new-env
+ (map (lambda (x)
+ (if (memv (car x) comps)
+ (cons (car x) (substitute cs (cdr x)))
+ x))
+ scc-env)]
+
+ [annotated-bindings (append (cdr acc) ; the previous annotated bindings
+ (map list
+ comps
+ (map caddr type-results)))])
+ (cons new-env annotated-bindings)))
+ ; takes in the current environment and a scc
+ ; returns new environment with scc's types added in
+ (let* ([components (reverse (sccs (graph (let-bindings x))))]
+ [results (fold-left process-component (cons env '()) components)]
+ [new-env (car results)]
+ [annotated-bindings (cdr results)]
+
+ [body-results (map (lambda (body) (check new-env body)) (let-body x))]
+ [let-type (cadr (last body-results))]
+ [cs (fold-left (lambda (acc cs) (constraint-merge acc cs)) '() (map car body-results))]
+
+ [annotated `((let ,annotated-bindings ,@(map caddr body-results)) : ,let-type)])
+ (list cs let-type annotated)))
+
+(define (check-app env x)
+ (if (eqv? (car x) (cadr x))
+ ; recursive function (f f)
+ ; TODO: what about ((f a) f)????
+ (let* ([func-type (env-lookup env (car x))]
+ [return-type (fresh-tvar)]
+ [other-func-type `(abs ,func-type ,return-type)]
+ [cs (~ func-type other-func-type)]
+ [resolved-return-type (substitute cs return-type)]
+
+ [annotated `(((,(car x) : ,func-type)
+ (,(cadr x) : ,func-type)) : ,resolved-return-type)])
+ (list cs resolved-return-type annotated)))
+
+ ; regular function
+ (let* ([arg-type-res (check env (cadr x))]
+ [arg-type (cadr arg-type-res)]
+ [func-type-res (check env (car x))]
+ [func-type (cadr func-type-res)]
+
+ ; f ~ a -> t0
+ [func-c (~
+ (substitute (car arg-type-res) func-type)
+ `(abs ,arg-type ,(fresh-tvar)))]
+ [cs (constraint-merge
+ (constraint-merge func-c (car arg-type-res))
+ (car func-type-res))]
+
+ [resolved-func-type (substitute cs func-type)]
+ [resolved-return-type (caddr resolved-func-type)]
+
+ [annotated `((,(caddr func-type-res)
+ ,(caddr arg-type-res)) : ,resolved-return-type)])
+
+ (if (abs? resolved-func-type)
+ (let ((return-type (substitute cs (caddr resolved-func-type))))
+ (list cs return-type annotated))
+ (error #f "not a function"))))
+
+; returns a list (constraints type annotated)
+(define (check env x)
+ (define (make-result cs type)
+ (list cs type `(,x : ,type)))
+ ;; (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)))
- ((symbol? x) (list '() (env-lookup env x)))
-
- ((is-lambda? x)
- (let* ((new-env (cons (cons (lambda-arg x) (fresh-tvar)) env))
- (body-type-res (typecheck new-env (lambda-body x)))
- (subd-env (substitute (car body-type-res) new-env)))
- (display "lambda: ")
- (display body-type-res)
- (display "\n")
- (list (car body-type-res)
- (list 'abs
- (env-lookup subd-env (lambda-arg x))
- (cadr body-type-res)))))
-
- ((is-app? x) ; (f a)
- (let* ((arg-type-res (typecheck env (cadr x)))
- ; typecheck f with the knowledge that f : a -> x
- (func-type-res (typecheck env (car x)))
- (func-type (cadr func-type-res))
- (c (unify func-type
- (list 'abs
- (cadr arg-type-res)
- (fresh-tvar))))
- (new-env (substitute c env))
- (resolved-func-type (env-lookup new-env (car x))))
- (display "is-app:\n")
- (display c)
- (display "\n")
- (display new-env)
- (display "\n")
- (display resolved-func-type)
- (display "\n")
- (display arg-type-res)
- (display "\n")
- (if (abs? resolved-func-type)
- (list (append c
- (unify (cadr arg-type-res)
- (cadr resolved-func-type)))
- (caddr resolved-func-type))
- (error #f "wah")))))))
- (display "result of ")
- (display x)
- (display ":\n\t")
- (display (cadr res))
- (display "[")
- (display (car res))
- (display "]\n")
+ (case (ast-type x)
+ ('int-literal (make-result '() 'Int))
+ ('bool-literal (make-result '() 'Bool))
+ ('string-literal (make-result '() 'String))
+ ('builtin (make-result '() (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 (~ (cadr then-type-res)
+ (cadr else-type-res)))
+ (cs (constraint-merge
+ (car then-type-res)
+ (constraint-merge (~ (cadr cond-type-res) 'Bool)
+ (constraint-merge (car else-type-res)
+ then-eq-else-cs))))
+ (return-type (substitute cs (cadr then-type-res)))
+ [annotated `((if ,(caddr cond-type-res)
+ ,(caddr then-type-res)
+ ,(caddr else-type-res)) : ,return-type)])
+ (list cs return-type annotated)))
+
+ ('var (make-result '() (env-lookup env x)))
+ ('let (check-let env x))
+
+
+ ('lambda
+ (let* ([new-env (env-insert env (lambda-arg x) (fresh-tvar))]
+
+ [body-type-res (check new-env (lambda-body x))]
+ [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)]
+
+ [lambda-type `(abs ,resolved-arg-type ,(cadr body-type-res))]
+
+ [annotated `((lambda (,(lambda-arg x)) ,(caddr body-type-res)) : ,lambda-type)])
+
+ (list (car body-type-res) ; constraints
+ lambda-type ; type
+ annotated)))
+
+
+ ('app (check-app env x))
+ ['case
+ (let* ([expr-type-res (check env (case-expr x))]
+ [expr-type (cadr env)]
+ [case-match-type-res (map (lambda (x) (check env x))
+ (map car (case-cases x)))]
+ [case-match-types (map cadr case-match-type-res)]
+
+ [case-expr-type-res (map (lambda (x) (check env x))
+ (map cadr (case-cases x)))]
+ [case-expr-types (map cadr case-expr-types-res)]
+
+ [case-match-equality-cs (fold-left constraint-merge '()
+ (map (lambda (t) (~ t expr-type)) case-match-types))]
+
+ [case-expr-equality-cs (fold-left constraint-merge '()
+ (map (lambda (t) (~ t (car case-expr-type-res)))
+ (cdr case-expr-type-res)))]
+
+ [resolved-type (substitute case-expr-eqaulity-cs (car case-expr-type-res))]
+
+ [annotated `((case (,(case-expr x) : ,expr-type)
+ ,(map (lambda (c e et)
+ `(,c (,e : ,et)))
+ (map car (case-cases x))
+ (map cadr (case-cases x))
+ case-expr-types)) : ,resolved-type)]
+
+ [cs (fold-left constraint-merge '()
+ (append case-match-equality-cs
+ case-expr-equality-cs
+ (cadr expr-type-res)))])
+ (list cs resolved-type annotated))])))
+
+ ;; (display "result of ")
+ ;; (display x)
+ ;; (display ":\n\t")
+ ;; (display (pretty-type (cadr res)))
+ ;; (display "\n\t[")
+ ;; (display (pretty-constraints (car res)))
+ ;; (display "]\n")
res))
+(define (init-adts-env prog)
+ (flat-map data-tors-type-env (program-data-layouts prog)))
-(define (abs? t)
- (and (list? t) (eq? (car t) 'abs)))
+ ; we typecheck the lambda calculus only (only single arg lambdas)
+(define (typecheck prog)
+ (let ([expanded (expand-pattern-matches prog)])
+ (cadr (check (init-adts-env expanded) (normalize (program-body expanded))))))
-(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) '())
- ((or (tvar? a) (tvar? b)) (list (cons a b)))
- ((and (abs? a) (abs? b))
- (append (unify (cadr a) (cadr b))
- (unify (caddr a) (caddr b))))
- (else (error #f "could not unify"))))
-
- ; takes a list of constraints and a type environment, and makes it work
-(define (substitute c env)
- (let ((go (lambda (x) (let ((tv (cdr x))
- (n (car x)))
- ;; (display tv)
- ;; (display "\n")
- ;; (display n)
- (cons n (fold-left
- (lambda (a y)
- ;; (display y)
- ;; (display ":")
- ;; (display a)
- (cond ((eq? a (car y)) (cdr y))
- ((eq? a (cdr y)) (car y))
- (else a)))
- tv c))))))
- (map go env)))
+ ; before passing annotated types onto codegen
+ ; we need to restore the pre-normalization structure
+ ; (this is important for function arity etc)
+(define (denormalize orig normed)
+
+ (define (collapse-lambdas n x)
+ (case n
+ [0 x]
+ [else
+ (let* ([inner-lambda (lambda-body (ann-expr x))]
+ [arg (lambda-arg (ann-expr x))]
+ [inner-collapsed (ann-expr (collapse-lambdas (- n 1) inner-lambda))])
+ `((lambda ,(cons arg (lambda-args inner-collapsed))
+ ,(lambda-body inner-collapsed)) : ,(ann-type x)))]))
+
+ (define (collapse-apps n x)
+ (case n
+ [-1 (error #f "nullary functions not handled yet")]
+ [0 x]
+ [else
+ (let* ([inner-app (car (ann-expr x))]
+ [inner-collapsed (collapse-apps (- n 1) inner-app)])
+ `(,(append (ann-expr inner-collapsed) (cdr (ann-expr x))) : ,(ann-type x)))]))
+
+ (case (ast-type orig)
+ ['lambda
+ (let ([collapsed (collapse-lambdas (- (length (lambda-args orig)) 1) normed)])
+ `((lambda ,(lambda-args (ann-expr collapsed))
+ ,(denormalize (lambda-body orig)
+ (lambda-body (ann-expr collapsed)))) : ,(ann-type collapsed)))]
+ ['app
+ (let ([collapsed (collapse-apps (- (length orig) 2) normed)])
+ `(,(map (lambda (o n) (denormalize o n)) orig (ann-expr collapsed))
+ : ,(ann-type collapsed)))]
+ ['let
+ `((let ,(map (lambda (o n) (list (car o) (denormalize (cadr o) (cadr n))))
+ (let-bindings orig)
+ (let-bindings (ann-expr normed)))
+ ,@(map (lambda (o n) (denormalize o n))
+ (let-body orig)
+ (let-body (ann-expr normed)))) : ,(ann-type normed))]
+ ['if `((if ,@(map denormalize (cdr orig) (cdr (ann-expr normed))))
+ : (ann-type normed))]
+ [else normed]))
+
+(define ann-expr car)
+(define ann-type caddr)
+
+ ; prerequisites: expand-pattern-matches
+(define (annotate-types prog)
+ (denormalize
+ (program-body prog)
+ (caddr (check (init-adts-env prog) (normalize (program-body prog))))))
+
+
+ ; returns a list of constraints
+(define (~ a b)
+ (let ([res (unify? a b)])
+ (if res
+ res
+ (error #f
+ (format "couldn't unify ~a ~~ ~a" a b)))))
+
+(define (unify? a b)
+ (cond [(eq? a b) '()]
+ [(tvar? a) (list (cons a b))]
+ [(tvar? b) (list (cons b a))]
+ [(and (abs? a) (abs? b))
+ (let* [(arg-cs (unify? (cadr a) (cadr b)))
+ (body-cs (unify? (substitute arg-cs (caddr a))
+ (substitute arg-cs (caddr b))))]
+ (constraint-merge body-cs arg-cs))]
+ [else #f]))
+
+(define (substitute cs t)
+ (cond
+ [(tvar? t)
+ (if (assoc t cs)
+ (cdr (assoc t cs))
+ t)]
+ [(abs? t) `(abs ,(substitute cs (cadr t))
+ ,(substitute cs (caddr t)))]
+ [else t]))
+
+ ; applies substitutions to all variables in environment
+(define (substitute-env cs env)
+ (map (lambda (x) (cons (car x) (substitute cs (cdr x)))) env))
+
+ ; composes constraints a onto b and merges, i.e. applies a to b
+ ; a should be the "more important" constraints
+(define (constraint-merge a b)
+ (define (f cs constraint)
+ (cons (car constraint)
+ (substitute cs (cdr constraint))))
+
+ (define (most-concrete a b)
+ (cond
+ [(tvar? a) b]
+ [(tvar? b) a]
+ [(and (abs? a) (abs? b))
+ `(abs ,(most-concrete (cadr a) (cadr b))
+ ,(most-concrete (caddr a) (caddr b)))]
+ [(abs? a) b]
+ [(abs? b) a]
+ [else a]))
+
+ ; for any two constraints that clash, e.g. t1 ~ abs t2 t3
+ ; and t1 ~ abs int t3
+ ; prepend the most concrete version of the type to the
+ ; list of constraints
+ (define (clashes)
+ (define (gen acc x)
+ (if (assoc (car x) a)
+ (cons (cons (car x) (most-concrete (cdr (assoc (car x) a))
+ (cdr x)))
+ acc)
+ acc))
+ (fold-left gen '() b))
+
+ (define (union p q)
+ (append (filter (lambda (x) (not (assoc (car x) p)))
+ q)
+ p))
+ (append (clashes) (union a (map (lambda (z) (f a z)) b))))
+
+
+;; ; a1 -> a2 ~ a3 -> a4;
+;; ; a1 -> a2 !~ Bool -> Bool
+;; ; basically can the tvars be renamed
+(define (types-equal? x y)
+ (let ([cs (unify? x y)])
+ (if (not cs) #f
+ (let*
+ ([test (lambda (acc c)
+ (and acc
+ (tvar? (car c)) ; the only substitutions allowed are tvar -> tvar
+ (tvar? (cdr c))))])
+ (fold-left test #t cs)))))
+
+ ; input: a list of binds ((x . y) (y . 3))
+ ; returns: pair of verts, edges ((x y) . (x . y))
+