X-Git-Url: http://git.lukelau.me/?p=scheme.git;a=blobdiff_plain;f=typecheck.scm;h=821035230affddf4d813074316f5aecaeaa2efc5;hp=219b010202a7f7a0127847e8372ea7e064a05efb;hb=844dcd052c6f551d9936693c2b4c49cf920c7051;hpb=e561cf1ba9b2986a22cb08250247e402ac2a5871 diff --git a/typecheck.scm b/typecheck.scm index 219b010..8210352 100644 --- a/typecheck.scm +++ b/typecheck.scm @@ -4,14 +4,13 @@ (and (list? t) (eq? (car t) 'abs))) (define (tvar? t) - (and (not (list? t)) (not (concrete? t)) (symbol? t))) + (and (not (list? t)) + (not (concrete? t)) + (symbol? t))) (define (concrete? t) - (case t - ('int #t) - ('bool #t) - ('void #t) - (else #f))) + (and (symbol? t) + (char-upper-case? (string-ref (symbol->string t) 0)))) (define (pretty-type t) (cond ((abs? t) @@ -23,9 +22,22 @@ (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 n) - (if (null? env) (error #f "empty env") ; it's a type equality + (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) n)))) @@ -69,48 +81,16 @@ (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 #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) - (let - ((res - (case (ast-type x) - ('int-literal (list '() 'int)) - ('bool-literal (list '() 'bool)) - ('string-literal (list '() 'string)) - ('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 (~ (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))) - - ('var (list '() (env-lookup env x))) - ('let + ('+ '(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) ; 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))))] @@ -136,12 +116,13 @@ [cs (fold-left (lambda (acc res c) - (consolidate - acc - (consolidate (car res) + (constraint-merge + (constraint-merge ; unify with tvars from scc-env ; result ~ tvar - (~ (cadr res) (env-lookup scc-env c))))) + (~ (env-lookup scc-env c) (cadr res)) + (car res)) + acc)) '() type-results comps)] ; substitute *only* the bindings in this scc [new-env @@ -154,6 +135,38 @@ [new-env (fold-left process-component env components)]) (check new-env (last (let-body x))))) +(define (check env x) + ;; (display "check: ") + ;; (display x) + ;; (display "\n\t") + ;; (display env) + ;; (newline) + (let + ((res + (case (ast-type x) + ('int-literal (list '() 'Int)) + ('bool-literal (list '() 'Bool)) + ('string-literal (list '() 'String)) + ('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 (~ (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)))) + (list cs return-type))) + + ('var (list '() (env-lookup env x))) + ('let (check-let env x)) + + ('lambda (let* [(new-env (env-insert env (lambda-arg x) (fresh-tvar))) @@ -167,6 +180,7 @@ ;; (display "\n\t") ;; (display cs) ;; (display "\n\t") + ;; (display (format "subd-env: ~a\n" subd-env)) ;; (display resolved-arg-type) ;; (newline) (list (car body-type-res) @@ -182,7 +196,7 @@ (other-func-type `(abs ,func-type ,return-type)) (cs (~ func-type other-func-type)) (resolved-return-type (substitute cs return-type))] - (list cs resolved-return-type)) + (list cs resolved-return-type))) ; regular function (let* ((arg-type-res (check env (cadr x))) @@ -192,12 +206,10 @@ ; f ~ a -> t0 (func-c (~ - func-type - (list 'abs - arg-type - (fresh-tvar)))) - (cs (consolidate - (consolidate func-c (car arg-type-res)) + (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)) @@ -214,18 +226,26 @@ (if (abs? resolved-func-type) (let ((return-type (substitute cs (caddr resolved-func-type)))) (list cs return-type)) - (error #f "not a function")))))))) + (error #f "not a function"))))))) ;; (display "result of ") ;; (display x) ;; (display ":\n\t") ;; (display (pretty-type (cadr res))) ;; (display "\n\t[") - ;; (display (car res)) + ;; (display (pretty-constraints (car res))) ;; (display "]\n") res)) - (cadr (check '() (normalize prog)))) - ; returns a list of pairs of constraints + ; we typecheck the lambda calculus only (only single arg lambdas) +(define (typecheck prog) + + (let ([init-env (flat-map data-tors (program-datas prog))]) + (display init-env) + (newline) + (cadr (check init-env (normalize (program-body prog)))))) + + + ; returns a list of constraints (define (~ a b) (let ([res (unify? a b)]) (if res @@ -235,116 +255,90 @@ (define (unify? a b) (cond [(eq? a b) '()] - [(or (tvar? a) (tvar? b)) (list (list 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))))] - (consolidate arg-cs body-cs))] + (constraint-merge body-cs arg-cs))] [else #f])) - ; TODO: what's the most appropriate substitution? - ; should all constraints just be limited to a pair? - ; this is currently horrific and i don't know what im doing. - ; should probably use ast-find here or during consolidation - ; to detect substitutions more than one layer deep - ; e.g. (abs t1 int) ~ (abs bool int) - ; substituting these constraints with t1 should resolve t1 with bool (define (substitute cs t) - ; gets the first concrete type - ; otherwise returns the last type variable - - ; removes t itself from cs, to prevent infinite recursion - (define cs-without-t - (map (lambda (c) - (filter (lambda (x) (not (eqv? t x))) c)) - cs)) - - (define (get-concrete c) - (let [(last (null? (cdr c)))] - (if (not (tvar? (car c))) - (if (abs? (car c)) - (substitute cs-without-t (car c)) - (car c)) - (if last - (car 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)))) - + [(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)) -(define (consolidate x y) - (define (merge a b) - (cond ((null? a) b) - ((null? b) a) - (else (if (member (car b) a) - (merge a (cdr b)) - (cons (car b) (merge a (cdr b))))))) - (define (overlap? a b) - (if (or (null? a) (null? b)) - #f - (if (fold-left (lambda (acc v) - (or acc (eq? v (car a)))) - #f b) - #t - (overlap? (cdr a) b)))) - - (cond ((null? y) x) - ((null? x) y) - (else - (let* ((a (car y)) - (merged (fold-left - (lambda (acc b) - (if acc - acc - (if (overlap? a b) - (cons (merge a b) b) - #f))) - #f x)) - (removed (if merged - (filter (lambda (b) (not (eq? b (cdr merged)))) x) - x))) - (if merged - (consolidate removed (cons (car merged) (cdr y))) - (consolidate (cons a x) (cdr y))))))) - - ; a1 -> a2 ~ a3 -> a4; - ; a1 -> a2 !~ bool -> bool - ; basically can the tvars be renamed + ; 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-kind - (lambda (acc c) - (if (tvar? c) acc #f))] - [test (lambda (acc c) + ([test (lambda (acc c) (and acc - (fold-left test-kind #t c) ; check only tvar substitutions - (<= (length c) 2)))]) ; check maximum 2 subs per equality group + (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)) (define (graph bs) + (define (go bs orig-bs) (define (find-refs prog) (ast-collect (lambda (x) (case (ast-type x) ; only count a reference if its a binding - ['var (if (assoc x bs) (list x) '())] + ['var (if (assoc x orig-bs) (list x) '())] [else '()])) prog)) (if (null? bs) @@ -358,10 +352,11 @@ (rest (if (null? (cdr bs)) (cons '() '()) - (graph (cdr bs)))) + (go (cdr bs) orig-bs))) (total-verts (cons vert (car rest))) (total-edges (append edges (cdr rest)))] (cons total-verts total-edges)))) + (go bs bs)) (define (successors graph v) (define (go v E)