Move expanding pattern matches to its own transformations
authorLuke Lau <luke_lau@icloud.com>
Mon, 12 Aug 2019 15:26:21 +0000 (16:26 +0100)
committerLuke Lau <luke_lau@icloud.com>
Mon, 12 Aug 2019 15:26:21 +0000 (16:26 +0100)
I think this will make it easier to understand how pattern matching
works

ast.scm
codegen.scm
tests.scm
typecheck.scm

diff --git a/ast.scm b/ast.scm
index 9af36531e7ef38bb1affc652eb09c5afa46a3dcc..8a986173d675f43eb58cd0c5f7c6c7c679175d9f 100644 (file)
--- a/ast.scm
+++ b/ast.scm
     ['stack (either (p x) (inner (caddr x)))]
     [else (p x)]))
 
-(define (let-bindings e)
-  (define (pattern-match binding body)
-    (if (eqv? (ast-type binding) 'var)
-       (list (cons binding body))
-       (let* ([constructor (car binding)]
-              [destructor (lambda (i) (dtor-name constructor i))])
-         (flat-map (lambda (y i)
-                     (pattern-match y `((,(destructor i) ,@body))))
-                   (cdr binding)
-                   (range 0 (length (cdr binding)))))))
-  (flat-map (lambda (x) (pattern-match (car x) (cdr x))) (cadr e)))
+(define let-bindings cadr)
 (define let-body cddr)
 
+                                       ; (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 x)  
+  (define (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))])
+           (flat-map (lambda (y i)
+                       (pattern-match (list y `(,(destructor i) ,body))))
+                     (cdr binding-name)
+                     (range 0 (length (cdr binding-name))))))))
+  (case (ast-type x)
+      ['let `(let ,(flat-map pattern-match (let-bindings x))
+                 ,@(map expand-pattern-matches (let-body x)))]
+      [else (ast-traverse expand-pattern-matches x)]))
+
 (define (lambda? x)
   (and (list? x) (eq? (car x) 'lambda)))
 
   (filter (lambda (x) (eqv? (statement-type x) 'defines))
          program))
 
+(define (program-map-exprs f program)
+  (map (lambda (x)
+        (case (statement-type x)
+          ['expr (f x)]
+          [else x]))
+       program))
+
 (define (program-body program)
   ; hack to have multi-expression bodies
   `(let ()
index 7a37097096882bc4c60d5262c6c81a4c3b9ba371..8e3a7d5c7191488c5a11843c07e7c5f168672407 100644 (file)
   (set! cur-lambda 0)
   (let* ([data-layouts (program-data-layouts program)]
 
-        [type-annotated (annotate-types program)]
+        [pattern-matched (program-map-exprs
+                          expand-pattern-matches
+                          program)]
+        [type-annotated (annotate-types pattern-matched)]
         [stack-annotated (annotate-stack-values data-layouts
                                                 type-annotated)]
         
index 2b6e09a4c2c63207707f061f722ca4ca3c377a66..bcd878a11dfccb8368d930931d5ad00721d8c626 100644 (file)
--- a/tests.scm
+++ b/tests.scm
        (bar . (abs Bool A))
        (bar~0 . (abs A Bool))))
 
+(test (expand-pattern-matches '(let ([(foo x y) (foo 123 234)] [z (f 123)]) x))
+      '(let ([x (foo~0 (foo 123 234))]
+            [y (foo~1 (foo 123 234))]
+            [z (f 123)])
+        x))
+
 (test-types (typecheck '((lambda (x) (+ ((lambda (y) (x y 3)) 5) 2))))
            '(abs (abs Int (abs Int Int)) Int))
 
  'Int)
 
 
-                                       ; pattern matching
-(test (let-bindings '(let ([(foo x) a]) x))
-      '((x (foo~0 a))))
-
-(test (let-bindings '(let ([x (foo 42)] [(foo y) x]) x))
-      '((x (foo 42))
-       (y (foo~0 x))))
-
                                        ; type annotations
 
 (test (annotate-types
index 35e818866df9603a3ecaa4cc721b548035d26abc..a526620e7139665fd72273394914462419cb45d0 100644 (file)
 
                                        ; we typecheck the lambda calculus only (only single arg lambdas)
 (define (typecheck prog)
-  (cadr (check (init-adts-env prog) (normalize (program-body prog)))))
+  (let ([expanded (program-map-exprs expand-pattern-matches prog)])
+    (cadr (check (init-adts-env expanded) (normalize (program-body expanded))))))
 
 
                                        ; before passing annotated types onto codegen
 
 (define ann-expr car)
 (define ann-type caddr)
+
+                                       ; prerequisites: expand-pattern-matches
 (define (annotate-types prog)
-  (denormalize (program-body prog)
+  (denormalize
+   (program-body prog)
    (caddr (check (init-adts-env prog)(normalize (program-body prog))))))