Check for let pattern matches that they have only one sum
authorLuke Lau <luke_lau@icloud.com>
Tue, 13 Aug 2019 13:09:45 +0000 (14:09 +0100)
committerLuke Lau <luke_lau@icloud.com>
Tue, 13 Aug 2019 13:09:45 +0000 (14:09 +0100)
ast.scm
tests.scm

diff --git a/ast.scm b/ast.scm
index b3f21bd07d6d07ba2b64017cff0a365cbf77abd4..342fe453a4d9276c36237e43e1a7069b5ec75670 100644 (file)
--- a/ast.scm
+++ b/ast.scm
 
                   [type (data-tor-type data-layouts sum-name)]
 
-                  [sum (assoc sum-name (cdr (assoc type data-layouts)))]
+                  [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))
index fa5b195e1b2575c4d14f9e385c854aad53dd0823..6dfbd9f4459cc1e46b3258caab6d5c0ba0ff2e27 100644 (file)
--- a/tests.scm
+++ b/tests.scm
 
 (test-types
  (typecheck
-  '((data A
-         [foo Int]
-         [bar Bool])
+  '((data A [foo Int])
     (let ([x (foo 42)]
          [(foo y) x])
       x)))
 
 (test-types
  (typecheck
-  '((data A
-         [foo Int]
-         [bar Bool])
+  '((data A [foo Int])
     (let ([x (foo 42)]
          [(foo y) x])
       y)))
             (let ([(foo x y) (foo (= 3 3) 42)])
               y))
           42)
+
+(test-exception (expand-pattern-matches
+                '((data A [foo Int]
+                  [bar Bool])
+                 (let ([(foo x) (foo 0)]) x))))