Move expanding pattern matches to its own transformations
[scheme.git] / tests.scm
1 (load "codegen.scm")
2 (load "typecheck.scm")
3
4 (define (test-f pred actual expected)
5   (when (not (pred actual expected))
6     (error #f
7            (format "test failed:\nexpected: ~a\nactual:   ~a"
8                    expected actual))))
9
10 (define (test . xs) (apply test-f (cons equal? xs)))
11
12 (define-syntax test-types
13   (syntax-rules ()
14     ((_ a e)
15      (begin
16        (display (quote a))
17        (newline)
18        (test-f types-equal? a e)))))
19
20 (define (read-file file)
21   (call-with-input-file file
22     (lambda (p)
23       (let loop ((next (read-char p))
24                  (result ""))
25         (if (eof-object? next)
26             result
27             (loop (read-char p) (string-append result (string next))))))))
28
29 (define (test-prog prog exit-code)
30   (display prog)
31   (newline)
32   (compile-to-binary prog "/tmp/test-prog" host-os)
33   (test (system "/tmp/test-prog") exit-code))
34
35 (define (test-expr prog exit-code)
36   (test-prog (list prog) exit-code))
37
38 (define (test-prog-stdout prog output)
39   (display prog)
40   (newline)
41   (compile-to-binary prog "/tmp/test-prog" host-os)
42   (system "/tmp/test-prog > /tmp/test-output.txt")
43   (let ((str (read-file "/tmp/test-output.txt")))
44     (test str output)))
45
46 (test (data-tors '(A . ((foo Int Bool)
47                         (bar Bool))))
48       '((foo (A foo constructor)
49              abs Int (abs Bool A))
50         (foo~0 (A foo 0) abs A Int)
51         (foo~1 (A foo 1) abs A Bool)
52         (bar (A bar constructor) abs Bool A)
53         (bar~0 (A bar 0) abs A Bool)))
54
55 (test (data-tors-type-env
56        '(A . ((foo Int Bool)
57               (bar Bool))))
58       '((foo . (abs Int (abs Bool A)))
59         (foo~0 . (abs A Int))
60         (foo~1 . (abs A Bool))
61         (bar . (abs Bool A))
62         (bar~0 . (abs A Bool))))
63
64 (test (expand-pattern-matches '(let ([(foo x y) (foo 123 234)] [z (f 123)]) x))
65       '(let ([x (foo~0 (foo 123 234))]
66              [y (foo~1 (foo 123 234))]
67              [z (f 123)])
68          x))
69
70 (test-types (typecheck '((lambda (x) (+ ((lambda (y) (x y 3)) 5) 2))))
71             '(abs (abs Int (abs Int Int)) Int))
72
73                                         ; recursive types
74
75 (test-types (substitute '((t1 . (abs t1 t10))) 't1) '(abs t1 t10))
76
77 (test-types (typecheck '((let ([bar (lambda (y) y)]
78                                [foo (lambda (x) (foo (bar #t)))])
79                            foo)))
80             '(abs Bool a))
81
82 (test-types (typecheck '((let ([bar (lambda (y) y)]
83                                [foo (lambda (x) (foo (bar #t)))])
84                            bar)))
85             '(abs a a))
86
87 (test-types (typecheck '((let ([foo 3]
88                                [bar (+ foo baz)]
89                                [baz (- bar 1)])
90                            bar)))
91             'Int)
92
93 (test-types (typecheck '((let ([foo 3]
94                                [bar (baz foo)]
95                                [baz (lambda (x) x)])
96                            baz)))
97             '(abs a a))
98
99 (test-types (typecheck '((let ([foo 3]
100                                [bar (baz foo)]
101                                [baz (lambda (x) x)])
102                            bar)))
103             'Int)
104
105                                         ; mutual recursion
106 (test-types (typecheck '((let ([f (lambda (n) (if (= n 0)
107                                                   0
108                                                   (+ 1 (g (- n 1)))))]
109                                [g (lambda (m) (f m))])
110                            (f 10))))
111             'Int)
112
113 (test-types (typecheck '((let ([pow (lambda (p y)
114                                       (let ([go (lambda (n x)
115                                                   (if (= n 0)
116                                                       x
117                                                       (go (- n 1) (* x y))))])
118                                         (go p 1)))])
119                            (pow 4 2))))
120             'Int)
121
122                                         ; ADTs
123
124
125 (test-types
126  (typecheck
127   '((data A
128           [foo Int]
129           [bar Bool])
130     (let ([x (foo 42)]
131           [(foo y) x])
132       x)))
133  'A)
134
135 (test-types
136  (typecheck
137   '((data A
138           [foo Int]
139           [bar Bool])
140     (let ([x (foo 42)]
141           [(foo y) x])
142       y)))
143  'Int)
144
145
146                                         ; type annotations
147
148 (test (annotate-types
149        '((let ([x 42]
150                [y (+ 1 x)])
151            (- y x))))
152
153       '((let ()
154           ((let ((x (42 : Int))
155                  (y (((+ : (abs Int (abs Int Int))) (1 : Int) (x : Int)) : Int)))
156              (((- : (abs Int (abs Int Int))) (y : Int) (x : Int)) : Int)) : Int)) : Int))
157
158 (test-expr '(+ 1 2) 3)
159 (test-expr '(bool->int (= 2 0)) 0)
160 (test-expr '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
161
162 (test-expr '(* 10 5) 50)
163
164 (test-expr '(let ((x (+ 1 32))
165                   (y x))
166               ((lambda (z) (+ 2 z)) (* y x)))
167            67) ; exit code modulos at 256
168 (test-expr '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)
169
170 (test-prog-stdout '((if (= 3 2) 1 (let () (print "hello world!") 0))) "hello world!")
171
172 (test-expr '((lambda (x y) (+ x y)) 1 2) 3)
173 (test-expr '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
174
175                                         ; passing closures about
176 (test-expr '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)
177
178                                         ; passing builtins about
179 (test-expr '((lambda (x) ((lambda (a b) (a b 3)) + x)) 3) 6)
180 (test-expr '(bool->int ((lambda (x) (x #f)) !)) 1)
181 (test-expr '((lambda (f) (f #t)) bool->int) 1)
182 (test-prog-stdout '((let () ((lambda (f) (f "foo")) print) 0)) "foo")
183 (test-expr '((lambda (f) (f 3 3)) (lambda (x y) (bool->int (= x y)))) 1)
184 (test-expr '(bool->int ((lambda (f) (! (f 2 3))) =)) 1)
185
186                                         ; recursion
187 (test-expr '(let [(inc (lambda (f n x)
188                          (if (= n 0)
189                              x
190                              (f f (- n 1) (+ x 1)))))]
191               (inc inc 3 2))
192            5)
193
194 (test-expr '(let ([go (lambda (n m x)
195                         (if (= n 0)
196                             x
197                             (go (- n 1) m (* x m))))]
198                   [pow (lambda (p y) (go p y 1))])
199               
200               (pow 3 2))
201            8)
202
203 (test-expr '(let ([pow (lambda (p y)
204                          (let ([go (lambda (n x)
205                                      (if (= n 0)
206                                          x
207                                          (go (- n 1) (* x y))))])
208                            (go p 1)))])
209               (pow 4 2))
210            16)
211
212                                         ; mutual recursion
213 ;; (test-prog-stdout '((let ([f (lambda (n)
214 ;;                            (if (= n 0)
215 ;;                                0
216 ;;                                (let ()
217 ;;                                  (print "a")
218 ;;                                  (g (- n 1)))))]
219 ;;                       [g (lambda (m)
220 ;;                            (let ()
221 ;;                              (print "b")
222 ;;                               (f (- m 1))))])
223 ;;                       (f 10))) "ababababab")
224
225                                         ; adts and pattern matching
226
227 (test-prog '((data A [foo Bool Int])
228              (let ([(foo x y) (foo (= 3 3) 42)])
229                y))
230            42)