Fix total pattern match verification
[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-syntax test
11   (syntax-rules ()
12     ((_ a e)
13      (begin
14        (display (quote a))
15        (newline)
16        (test-f equal? a e)))))
17
18 (define-syntax test-types
19   (syntax-rules ()
20     ((_ a e)
21      (begin
22        (display (quote a))
23        (newline)
24        (test-f types-equal? a e)))))
25
26 (define (read-file file)
27   (call-with-input-file file
28     (lambda (p)
29       (let loop ((next (read-char p))
30                  (result ""))
31         (if (eof-object? next)
32             result
33             (loop (read-char p) (string-append result (string next))))))))
34
35 (define (test-prog prog exit-code)
36   (display prog)
37   (newline)
38   (compile-to-binary prog "/tmp/test-prog" host-os)
39   (test-f equal? (system "/tmp/test-prog") exit-code))
40
41 (define (test-expr prog exit-code)
42   (test-prog (list prog) exit-code))
43
44 (define (test-prog-stdout prog output)
45   (display prog)
46   (newline)
47   (compile-to-binary prog "/tmp/test-prog" host-os)
48   (system "/tmp/test-prog > /tmp/test-output.txt")
49   (let ((str (read-file "/tmp/test-output.txt")))
50     (test-f equal? str output)))
51
52 (define-syntax test-exception
53   (syntax-rules ()
54     ((_ f)
55      (begin
56        (display (quote f))
57        (newline)
58        (call/cc (lambda (k)
59                   (with-exception-handler
60                    (lambda (x)
61                      (when (eqv? 'no-exception x)
62                        (error #f "test failed: no exception thrown"))
63                      (k))
64                    (lambda ()
65                      (begin
66                        f
67                        (raise 'no-exception))))))))))
68
69 (test (data-tors '(A . ((foo Int Bool)
70                         (bar Bool))))
71       '((foo (A foo constructor)
72              abs Int (abs Bool A))
73         (foo~0 (A foo 0 Int) abs A Int)
74         (foo~1 (A foo 1 Bool) abs A Bool)
75         (bar (A bar constructor) abs Bool A)
76         (bar~0 (A bar 0 Bool) abs A Bool)))
77
78 (test (data-tors-type-env
79        '(A . ((foo Int Bool)
80               (bar Bool))))
81       '((foo . (abs Int (abs Bool A)))
82         (foo~0 . (abs A Int))
83         (foo~1 . (abs A Bool))
84         (bar . (abs Bool A))
85         (bar~0 . (abs A Bool))))
86
87 (test (expand-pattern-matches
88        '((data A (foo Int Int))
89          (let ([(foo x y) (foo 123 234)] [z (f 123)]) x)))
90       '((data A (foo Int Int))
91         (let ([x (foo~0 (foo 123 234))]
92               [y (foo~1 (foo 123 234))]
93               [z (f 123)])
94           x)))
95
96 (test (singletons '((A [foo] [bar Int]) (B [baz Int] [qux])))
97       '(foo qux))
98
99 (test (map (lambda (x) (data-sum-tag '((A [foo] [bar Int]) (B [baz Int] [qux])) x))
100            '(foo bar baz qux))
101       '(0 1 0 1))
102
103 (test-exception
104  (expand-pattern-matches '((data A (foo Int Int))
105                            (let ([(foo x) (foo 123 234)])
106                              x))))
107
108 (test-types (typecheck '((lambda (x) (+ ((lambda (y) (x y 3)) 5) 2))))
109             '(abs (abs Int (abs Int Int)) Int))
110
111                                         ; recursive types
112
113 (test-types (substitute '((t1 . (abs t1 t10))) 't1) '(abs t1 t10))
114
115 (test-types (typecheck '((let ([bar (lambda (y) y)]
116                                [foo (lambda (x) (foo (bar true)))])
117                            foo)))
118             '(abs Bool a))
119
120 (test-types (typecheck '((let ([bar (lambda (y) y)]
121                                [foo (lambda (x) (foo (bar true)))])
122                            bar)))
123             '(abs a a))
124
125 (test-types (typecheck '((let ([foo 3]
126                                [bar (+ foo baz)]
127                                [baz (- bar 1)])
128                            bar)))
129             'Int)
130
131 (test-types (typecheck '((let ([foo 3]
132                                [bar (baz foo)]
133                                [baz (lambda (x) x)])
134                            baz)))
135             '(abs a a))
136
137 (test-types (typecheck '((let ([foo 3]
138                                [bar (baz foo)]
139                                [baz (lambda (x) x)])
140                            bar)))
141             'Int)
142
143                                         ; mutual recursion
144 (test-types (typecheck '((let ([f (lambda (n) (if (= n 0)
145                                                   0
146                                                   (+ 1 (g (- n 1)))))]
147                                [g (lambda (m) (f m))])
148                            (f 10))))
149             'Int)
150
151 (test-types (typecheck '((let ([pow (lambda (p y)
152                                       (let ([go (lambda (n x)
153                                                   (if (= n 0)
154                                                       x
155                                                       (go (- n 1) (* x y))))])
156                                         (go p 1)))])
157                            (pow 4 2))))
158             'Int)
159
160                                         ; ADTs
161
162
163 (test-types
164  (typecheck
165   '((data A [foo Int])
166     (let ([x (foo 42)]
167           [(foo y) x])
168       x)))
169  'A)
170
171 (test-types
172  (typecheck
173   '((data A [foo Int])
174     (let ([x (foo 42)]
175           [(foo y) x])
176       y)))
177  'Int)
178
179                                         ; case statements
180 (test-types
181  (typecheck '((data A (foo B))
182               (data B (bar Int))
183               (case (foo (bar 32))
184                 [(foo x) x])))
185  'B)
186
187 (test-types
188  (typecheck '((case 42
189                 [23 (= 1 2)]
190                 [x (= x 1)])))
191  'Bool)
192
193                                         ; type annotations
194
195 (test (annotate-types
196        '((let ([x 42]
197                [y (+ 1 x)])
198            (- y x))))
199
200       '((let ()
201           ((let ((x (42 : Int))
202                  (y (((+ : (abs Int (abs Int Int))) (1 : Int) (x : Int)) : Int)))
203              (((- : (abs Int (abs Int Int))) (y : Int) (x : Int)) : Int)) : Int)) : Int))
204
205 (test-expr '(+ 1 2) 3)
206 (test-expr '(bool->int (= 2 0)) 0)
207 (test-expr '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
208
209 (test-expr '(* 10 5) 50)
210
211 (test-expr '(let ((x (+ 1 32))
212                   (y x))
213               ((lambda (z) (+ 2 z)) (* y x)))
214            67) ; exit code modulos at 256
215 (test-expr '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)
216
217 (test-prog-stdout '((if (= 3 2) 1 (let () (print "hello world!") 0))) "hello world!")
218
219 (test-expr '((lambda (x y) (+ x y)) 1 2) 3)
220 (test-expr '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
221
222                                         ; passing closures about
223 (test-expr '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)
224
225                                         ; passing builtins about
226 (test-expr '((lambda (x) ((lambda (a b) (a b 3)) + x)) 3) 6)
227 (test-expr '(bool->int ((lambda (x) (x false)) !)) 1)
228 (test-expr '((lambda (f) (f true)) bool->int) 1)
229 (test-prog-stdout '((let () ((lambda (f) (f "foo")) print) 0)) "foo")
230 (test-expr '((lambda (f) (f 3 3)) (lambda (x y) (bool->int (= x y)))) 1)
231 (test-expr '(bool->int ((lambda (f) (! (f 2 3))) =)) 1)
232
233                                         ; recursion
234 (test-expr '(let [(inc (lambda (f n x)
235                          (if (= n 0)
236                              x
237                              (f f (- n 1) (+ x 1)))))]
238               (inc inc 3 2))
239            5)
240
241 (test-expr '(let ([go (lambda (n m x)
242                         (if (= n 0)
243                             x
244                             (go (- n 1) m (* x m))))]
245                   [pow (lambda (p y) (go p y 1))])
246               
247               (pow 3 2))
248            8)
249
250 (test-expr '(let ([pow (lambda (p y)
251                          (let ([go (lambda (n x)
252                                      (if (= n 0)
253                                          x
254                                          (go (- n 1) (* x y))))])
255                            (go p 1)))])
256               (pow 4 2))
257            16)
258
259                                         ; mutual recursion
260 ;; (test-prog-stdout '((let ([f (lambda (n)
261 ;;                            (if (= n 0)
262 ;;                                0
263 ;;                                (let ()
264 ;;                                  (print "a")
265 ;;                                  (g (- n 1)))))]
266 ;;                       [g (lambda (m)
267 ;;                            (let ()
268 ;;                              (print "b")
269 ;;                               (f (- m 1))))])
270 ;;                       (f 10))) "ababababab")
271
272                                         ; adts and pattern matching
273
274 (test-prog '((data A [foo Bool Int])
275              (let ([(foo x y) (foo (= 3 3) 42)])
276                y))
277            42)
278
279 (test-exception (expand-pattern-matches
280                  '((data A [foo Int]
281                    [bar Bool])
282                    (let ([(foo x) (foo 0)]) x))))
283
284 (test-prog '((data A [foo Int])
285              (let ([x (foo 42)])
286                (let ([(foo y) x])
287                  (+ 2 y))))
288            44)
289
290 (test-prog '((data A [foo Bool Int Int])
291              (let ([x (foo (= 2 1) 123 45)]
292                    [(foo a b c) x])
293                (+ b c)))
294            (+ 123 45))
295
296 (test-prog '((data A [foo Int])
297              (data B [bar A])
298              (let ([(bar (foo x)) (bar (foo 42))])
299                x))
300            42)
301
302 (test-prog '((data Foo [a] [b] [c])
303              (let ([x b])
304                (case x
305                  [a 3]
306                  [b 2]
307                  [c 1])))
308            2)
309
310 (test-prog '((data Foo [foo Int Int] [bar Bool])
311              (case (foo 42 12)
312                [(foo 20 x) 0]
313                [(foo 42 x) x]
314                [(foo y x) 0]
315                [(bar x) 0]))
316            12)
317
318 (test-prog '((data Foo [foo Int])
319              (data Bar [bar Foo])
320              (case (bar (foo 42))
321                [(bar (foo x)) x]))
322            42)
323
324
325                                         ; mix of singleton and non singleton constructors
326 (test-prog '((data A [foo Int] [bar])
327              (case (foo 42)
328                [(foo x) x]
329                [bar 0]))
330            42)
331
332 (test-prog '((data A [foo Int] [bar])
333              (case bar
334                [(foo x) 0]
335                [bar 12]))
336              12)
337                                         ; todo: make this error for incomplete pattern match 
338 (test-exception
339  (codegen '((data A [foo Int] [bar Int B])
340             (data B [baz Int])
341             (let ([val (bar 42 (baz 12))])
342               (case val
343                 [(foo 42) 0]
344                 [(bar 32 (baz 12)) 1]
345                 [(bar 42 (baz x)) x]
346                 [(foo x) 2])))))
347