+(define case-switch cadr)
+(define case-cases cddr)
+
+(define (constructor? data-layouts x)
+ (and (eqv? (ast-type x) 'var)
+ (assoc x (flat-map cdr data-layouts))))
+
+(define (all-cases data-layouts type)
+ (let ([sums (assoc type data-layouts)])
+ (if sums
+ (flat-map (lambda (sum)
+ (let* ([sum-name (car sum)]
+ [products (cdr sum)]
+ [product-cases (map (lambda (y) (all-cases data-layouts y)) products)])
+ (if (null? product-cases)
+ (list sum-name) ; singletons aren't enclosed in a list [(foo x) 42] vs [foo 42]
+ (apply combinations (cons (list sum-name) product-cases)))))
+ (cdr sums))
+ '(:binding))))
+
+ ; does a cover b
+(define (case-covers? data-layouts a b)
+ (let ([a-binding? (and (eqv? (ast-type a) 'var) (not (constructor? data-layouts a)))])
+ (if (eqv? ':binding b)
+ a-binding?
+ (if a-binding?
+ #t
+ (if (eqv? (ast-type b) 'var)
+ (eqv? b a)
+ (all (map (lambda (p q)
+ (case-covers? data-layouts p q))
+ (cdr a) (cdr b))))))))
+
+(define (verify-cases data-layouts annotated-program)
+
+ ;; (define (check-pattern switch-type pat)
+
+ ;; (define (impossible-match)
+ ;; (error "Can't pattern match ~a with ~a" switch-type (ann-expr pat)))
+
+ ;; (if (assoc switch-type data-layouts)
+ ;; (begin
+ ;; (let ([sums (cdr (assoc switch-type data-layouts))])
+ ;; (unless (eqv? (ast-type (ann-expr pat)) 'var) (impossible-match))
+ ;; (unless (assoc (car (ann-expr pat)) sums) (impossible-match))
+ ;; (unless
+ ;; )
+ ;; (begin
+ ;; (unless (assoc switch-type allowed-match-ast-types)
+ ;; (error #f "Can't pattern match on ~a" switch-type))
+
+ ;; (let ([allowed (cdr (assoc switch-type allowed-match-ast-types))])
+ ;; (unless (assoc (ast-type (ann-expr pat)) allowed) (impossible-match)))))))
+
+
+ (let ([expr (ann-expr annotated-program)])
+ (case (ast-type expr)
+ ['case
+ (let* ([switch-type (ann-type (case-switch expr))]
+ [cases (map car (case-cases expr))]
+ [case-covered?
+ (lambda (x) (any (lambda (y) (case-covers? data-layouts y x)) cases))])
+ (unless (all (map case-covered? (all-cases data-layouts switch-type)))
+ (error #f "not all cases covered")))]
+ [else (ast-traverse (lambda (x) (verify-cases data-layouts x)) expr)])))
+
+
+ ; (let ([(foo a b) (foo 123 345)]) a)
+ ; |
+ ; v
+ ; (let ([a (foo~0 (foo 123 345)]
+ ; [b (foo~1 (foo 123 345)]) a)
+(define (expand-pattern-matches program)
+ (define (go x)
+ (define (let-pattern-match binding)
+ (let ([binding-name (car binding)]
+ [body (cadr binding)])
+ (if (eqv? (ast-type binding-name) 'var)
+ (list (list binding-name body))
+
+ (let* ([sum-name (car binding-name)]
+ [destructor (lambda (i) (dtor-name sum-name i))]
+ [products (cdr binding-name)]
+
+ [data-layouts (program-data-layouts program)]
+
+ [type (data-tor-type data-layouts sum-name)]
+
+ [sums (cdr (assoc type data-layouts))]
+ [sum (assoc sum-name sums)]
+
+ [expected-number (length (cdr sum))])
+
+ ; assert that we only do a let pattern match on an ADT with exactly one sum
+ (when (not (= 1 (length sums)))
+ (error #f (format "Cannot pattern match a ~a in a let since it has ~a possible constructors"
+ type
+ (length sums))))
+
+ ; assert that there is the correct number of bindings
+ (when (not (= (length products)
+ expected-number))
+ (error #f (format "Got ~a bindings: expected ~a for ~a"
+ (length products)
+ expected-number
+ binding)))
+
+ (flat-map (lambda (y i)
+ (let-pattern-match (list y `(,(destructor i) ,body))))
+ products
+ (range 0 (length products)))))))
+
+ (case (ast-type x)
+ ['let `(let ,(flat-map let-pattern-match (let-bindings x))
+ ,@(map go (let-body x)))]
+ [else (ast-traverse go x)]))
+ (program-map-exprs go program))
+