ADT codegen working for simple types
[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-types (typecheck '((lambda (x) (+ ((lambda (y) (x y 3)) 5) 2))))
65             '(abs (abs Int (abs Int Int)) Int))
66
67                                         ; recursive types
68
69 (test-types (substitute '((t1 . (abs t1 t10))) 't1) '(abs t1 t10))
70
71 (test-types (typecheck '((let ([bar (lambda (y) y)]
72                                [foo (lambda (x) (foo (bar #t)))])
73                            foo)))
74             '(abs Bool a))
75
76 (test-types (typecheck '((let ([bar (lambda (y) y)]
77                                [foo (lambda (x) (foo (bar #t)))])
78                            bar)))
79             '(abs a a))
80
81 (test-types (typecheck '((let ([foo 3]
82                                [bar (+ foo baz)]
83                                [baz (- bar 1)])
84                            bar)))
85             'Int)
86
87 (test-types (typecheck '((let ([foo 3]
88                                [bar (baz foo)]
89                                [baz (lambda (x) x)])
90                            baz)))
91             '(abs a a))
92
93 (test-types (typecheck '((let ([foo 3]
94                                [bar (baz foo)]
95                                [baz (lambda (x) x)])
96                            bar)))
97             'Int)
98
99                                         ; mutual recursion
100 (test-types (typecheck '((let ([f (lambda (n) (if (= n 0)
101                                                   0
102                                                   (+ 1 (g (- n 1)))))]
103                                [g (lambda (m) (f m))])
104                            (f 10))))
105             'Int)
106
107 (test-types (typecheck '((let ([pow (lambda (p y)
108                                       (let ([go (lambda (n x)
109                                                   (if (= n 0)
110                                                       x
111                                                       (go (- n 1) (* x y))))])
112                                         (go p 1)))])
113                            (pow 4 2))))
114             'Int)
115
116                                         ; ADTs
117
118
119 (test-types
120  (typecheck
121   '((data A
122           [foo Int]
123           [bar Bool])
124     (let ([x (foo 42)]
125           [(foo y) x])
126       x)))
127  'A)
128
129 (test-types
130  (typecheck
131   '((data A
132           [foo Int]
133           [bar Bool])
134     (let ([x (foo 42)]
135           [(foo y) x])
136       y)))
137  'Int)
138
139
140                                         ; pattern matching
141 (test (let-bindings '(let ([(foo x) a]) x))
142       '((x (foo~0 a))))
143
144 (test (let-bindings '(let ([x (foo 42)] [(foo y) x]) x))
145       '((x (foo 42))
146         (y (foo~0 x))))
147
148                                         ; type annotations
149
150 (test (annotate-types
151        '((let ([x 42]
152                [y (+ 1 x)])
153            (- y x))))
154
155       '((let ()
156           ((let ((x (42 : Int))
157                  (y (((+ : (abs Int (abs Int Int))) (1 : Int) (x : Int)) : Int)))
158              (((- : (abs Int (abs Int Int))) (y : Int) (x : Int)) : Int)) : Int)) : Int))
159
160 (test-expr '(+ 1 2) 3)
161 (test-expr '(bool->int (= 2 0)) 0)
162 (test-expr '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
163
164 (test-expr '(* 10 5) 50)
165
166 (test-expr '(let ((x (+ 1 32))
167                   (y x))
168               ((lambda (z) (+ 2 z)) (* y x)))
169            67) ; exit code modulos at 256
170 (test-expr '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)
171
172 (test-prog-stdout '((if (= 3 2) 1 (let () (print "hello world!") 0))) "hello world!")
173
174 (test-expr '((lambda (x y) (+ x y)) 1 2) 3)
175 (test-expr '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
176
177                                         ; passing closures about
178 (test-expr '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)
179
180                                         ; passing builtins about
181 (test-expr '((lambda (x) ((lambda (a b) (a b 3)) + x)) 3) 6)
182 (test-expr '(bool->int ((lambda (x) (x #f)) !)) 1)
183 (test-expr '((lambda (f) (f #t)) bool->int) 1)
184 (test-prog-stdout '((let () ((lambda (f) (f "foo")) print) 0)) "foo")
185 (test-expr '((lambda (f) (f 3 3)) (lambda (x y) (bool->int (= x y)))) 1)
186 (test-expr '(bool->int ((lambda (f) (! (f 2 3))) =)) 1)
187
188                                         ; recursion
189 (test-expr '(let [(inc (lambda (f n x)
190                          (if (= n 0)
191                              x
192                              (f f (- n 1) (+ x 1)))))]
193               (inc inc 3 2))
194            5)
195
196 (test-expr '(let ([go (lambda (n m x)
197                         (if (= n 0)
198                             x
199                             (go (- n 1) m (* x m))))]
200                   [pow (lambda (p y) (go p y 1))])
201               
202               (pow 3 2))
203            8)
204
205 (test-expr '(let ([pow (lambda (p y)
206                          (let ([go (lambda (n x)
207                                      (if (= n 0)
208                                          x
209                                          (go (- n 1) (* x y))))])
210                            (go p 1)))])
211               (pow 4 2))
212            16)
213
214                                         ; mutual recursion
215 ;; (test-prog-stdout '((let ([f (lambda (n)
216 ;;                            (if (= n 0)
217 ;;                                0
218 ;;                                (let ()
219 ;;                                  (print "a")
220 ;;                                  (g (- n 1)))))]
221 ;;                       [g (lambda (m)
222 ;;                            (let ()
223 ;;                              (print "b")
224 ;;                               (f (- m 1))))])
225 ;;                       (f 10))) "ababababab")
226
227                                         ; adts and pattern matching
228
229 (test-prog '((data A [foo Bool Int])
230              (let ([(foo x y) (foo (= 3 3) 42)])
231                y))
232            42)