+(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)])))
+
+