X-Git-Url: http://git.lukelau.me/?p=scheme.git;a=blobdiff_plain;f=typecheck.scm;h=f6d7c86465f4869043b3d2fcb9560e42603258d7;hp=064f65f696459d08b38f3e20e00f8ff180b23bc7;hb=3d94d4500167a8327473e15cf477727968ee36a2;hpb=e66dabdbc34b1bf5d59cf2596ced19525661a378 diff --git a/typecheck.scm b/typecheck.scm index 064f65f..f6d7c86 100644 --- a/typecheck.scm +++ b/typecheck.scm @@ -191,6 +191,20 @@ (define (check-case dls env x) (define (check-match switch-type x) + + (define (get-bindings product-types pattern) + (define (go product-type product) + (case (ast-type product) + ['var (list (cons product product-type))] + ; an inner pattern match + ['app (let* ([inner-sum (car product)] + [inner-sums (cdr (assoc product-type dls))] + [inner-product-types (cdr (assoc inner-sum inner-sums))]) + (get-bindings inner-product-types product))] + [else '()])) + (flat-map go product-types (cdr pattern))) + + (let ([pattern (car x)] [expr (cadr x)]) (case (ast-type pattern) @@ -199,8 +213,9 @@ (let ([sum (assoc (car pattern) (cdr (assoc switch-type dls)))]) (unless sum (error #f "can't pattern match ~a with ~a" switch-type pattern)) (let* ([names (cdr pattern)] - [types (cdr sum)] - [new-env (fold-left env-insert env names types)]) + [product-types (cdr sum)] + [new-env (append (get-bindings product-types pattern) env)]) + (check dls new-env expr)))] ; pattern match with binding and no constructor ['var (check dls (env-insert env pattern switch-type) expr)] @@ -219,7 +234,7 @@ [resolved-type (substitute case-expr-equality-cs (car case-expr-types))] - [annotated `((case (,(case-switch x) : ,switch-type) + [annotated `((case ,(caddr switch-type-res) ,@(map (lambda (c e et) `(,c ((,e : ,et)))) (map car (case-cases x))