(load "codegen.scm")
(load "typecheck.scm")
-(define (test actual expected)
- (when (not (equal? actual expected))
+(define (test-f pred actual expected)
+ (when (not (pred actual expected))
(error #f
(format "test failed:\nexpected: ~a\nactual: ~a"
expected actual))))
+(define-syntax test
+ (syntax-rules ()
+ ((_ a e)
+ (begin
+ (display (quote a))
+ (newline)
+ (test-f equal? a e)))))
+
+(define-syntax test-types
+ (syntax-rules ()
+ ((_ a e)
+ (begin
+ (display (quote a))
+ (newline)
+ (test-f types-equal? a e)))))
+
(define (read-file file)
(call-with-input-file file
(lambda (p)
(define (test-prog prog exit-code)
(display prog)
(newline)
- (compile-to-binary prog "/tmp/test-prog")
- (test (system "/tmp/test-prog") exit-code))
+ (compile-to-binary prog "/tmp/test-prog" host-os)
+ (test-f equal? (system "/tmp/test-prog") exit-code))
+
+(define (test-expr prog exit-code)
+ (test-prog (list prog) exit-code))
(define (test-prog-stdout prog output)
(display prog)
(newline)
- (compile-to-binary prog "/tmp/test-prog")
+ (compile-to-binary prog "/tmp/test-prog" host-os)
(system "/tmp/test-prog > /tmp/test-output.txt")
(let ((str (read-file "/tmp/test-output.txt")))
- (test str output)))
+ (test-f equal? str output)))
+
+(define-syntax test-exception
+ (syntax-rules ()
+ ((_ f)
+ (begin
+ (display (quote f))
+ (newline)
+ (call/cc (lambda (k)
+ (with-exception-handler
+ (lambda (x)
+ (when (eqv? 'no-exception x)
+ (error #f "test failed: no exception thrown"))
+ (k))
+ (lambda ()
+ (begin
+ f
+ (raise 'no-exception))))))))))
+
+(test (data-tors '(A . ((foo Int Bool)
+ (bar Bool))))
+ '((foo (A foo constructor)
+ abs Int (abs Bool A))
+ (foo~0 (A foo 0 Int) abs A Int)
+ (foo~1 (A foo 1 Bool) abs A Bool)
+ (bar (A bar constructor) abs Bool A)
+ (bar~0 (A bar 0 Bool) abs A Bool)))
+
+(test (data-tors-type-env
+ '(A . ((foo Int Bool)
+ (bar Bool))))
+ '((foo . (abs Int (abs Bool A)))
+ (foo~0 . (abs A Int))
+ (foo~1 . (abs A Bool))
+ (bar . (abs Bool A))
+ (bar~0 . (abs A Bool))))
+
+(test (expand-pattern-matches
+ '((data A (foo Int Int))
+ (let ([(foo x y) (foo 123 234)] [z (f 123)]) x)))
+ '((data A (foo Int Int))
+ (let ([x (foo~0 (foo 123 234))]
+ [y (foo~1 (foo 123 234))]
+ [z (f 123)])
+ x)))
+
+(test (singletons '((A [foo] [bar Int]) (B [baz Int] [qux])))
+ '(foo qux))
+
+(test (map (lambda (x) (data-sum-tag '((A [foo] [bar Int]) (B [baz Int] [qux])) x))
+ '(foo bar baz qux))
+ '(0 1 0 1))
+
+(test-exception
+ (expand-pattern-matches '((data A (foo Int Int))
+ (let ([(foo x) (foo 123 234)])
+ x))))
+
+(test-types (typecheck '((lambda (x) (+ ((lambda (y) (x y 3)) 5) 2))))
+ '(abs (abs Int (abs Int Int)) Int))
+
+ ; recursive types
+
+(test-types (substitute '((t1 . (abs t1 t10))) 't1) '(abs t1 t10))
+
+(test-types (typecheck '((let ([bar (lambda (y) y)]
+ [foo (lambda (x) (foo (bar true)))])
+ foo)))
+ '(abs Bool a))
+
+(test-types (typecheck '((let ([bar (lambda (y) y)]
+ [foo (lambda (x) (foo (bar true)))])
+ bar)))
+ '(abs a a))
+
+(test-types (typecheck '((let ([foo 3]
+ [bar (+ foo baz)]
+ [baz (- bar 1)])
+ bar)))
+ 'Int)
+
+(test-types (typecheck '((let ([foo 3]
+ [bar (baz foo)]
+ [baz (lambda (x) x)])
+ baz)))
+ '(abs a a))
+
+(test-types (typecheck '((let ([foo 3]
+ [bar (baz foo)]
+ [baz (lambda (x) x)])
+ bar)))
+ 'Int)
+
+ ; mutual recursion
+(test-types (typecheck '((let ([f (lambda (n) (if (= n 0)
+ 0
+ (+ 1 (g (- n 1)))))]
+ [g (lambda (m) (f m))])
+ (f 10))))
+ 'Int)
+
+(test-types (typecheck '((let ([pow (lambda (p y)
+ (let ([go (lambda (n x)
+ (if (= n 0)
+ x
+ (go (- n 1) (* x y))))])
+ (go p 1)))])
+ (pow 4 2))))
+ 'Int)
+
+ ; ADTs
+
-(test (typecheck '(lambda (x) (+ ((lambda (y) (x y 3)) 5) 2)))
- '(abs (abs int (abs int int)) int))
+(test-types
+ (typecheck
+ '((data A [foo Int])
+ (let ([x (foo 42)]
+ [(foo y) x])
+ x)))
+ 'A)
-(test-prog '(+ 1 2) 3)
-(test-prog '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
+(test-types
+ (typecheck
+ '((data A [foo Int])
+ (let ([x (foo 42)]
+ [(foo y) x])
+ y)))
+ 'Int)
-(test-prog '(* 10 5) 50)
+ ; case statements
+(test-types
+ (typecheck '((data A (foo B))
+ (data B (bar Int))
+ (case (foo (bar 32))
+ [(foo x) x])))
+ 'B)
-(test-prog '(let ((x (+ 1 32))
+(test-types
+ (typecheck '((case 42
+ [23 (= 1 2)]
+ [x (= x 1)])))
+ 'Bool)
+
+ ; type annotations
+
+(test (annotate-types
+ '((let ([x 42]
+ [y (+ 1 x)])
+ (- y x))))
+
+ '((let ()
+ ((let ((x (42 : Int))
+ (y (((+ : (abs Int (abs Int Int))) (1 : Int) (x : Int)) : Int)))
+ (((- : (abs Int (abs Int Int))) (y : Int) (x : Int)) : Int)) : Int)) : Int))
+
+(test-expr '(+ 1 2) 3)
+(test-expr '(bool->int (= 2 0)) 0)
+(test-expr '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
+
+(test-expr '(* 10 5) 50)
+
+(test-expr '(let ((x (+ 1 32))
(y x))
((lambda (z) (+ 2 z)) (* y x)))
67) ; exit code modulos at 256
-(test-prog '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)
+(test-expr '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)
-(test-prog-stdout '(if (= 3 2) 1 (let () (print "hello world!") 0)) "hello world!")
+(test-prog-stdout '((if (= 3 2) 1 (let () (print "hello world!") 0))) "hello world!")
-(test-prog '((lambda (x y) (+ x y)) 1 2) 3)
-(test-prog '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
+(test-expr '((lambda (x y) (+ x y)) 1 2) 3)
+(test-expr '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
; passing closures about
-(test-prog '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)
+(test-expr '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)
+
+ ; passing builtins about
+(test-expr '((lambda (x) ((lambda (a b) (a b 3)) + x)) 3) 6)
+(test-expr '(bool->int ((lambda (x) (x false)) !)) 1)
+(test-expr '((lambda (f) (f true)) bool->int) 1)
+(test-prog-stdout '((let () ((lambda (f) (f "foo")) print) 0)) "foo")
+(test-expr '((lambda (f) (f 3 3)) (lambda (x y) (bool->int (= x y)))) 1)
+(test-expr '(bool->int ((lambda (f) (! (f 2 3))) =)) 1)
+
+ ; recursion
+(test-expr '(let [(inc (lambda (f n x)
+ (if (= n 0)
+ x
+ (f f (- n 1) (+ x 1)))))]
+ (inc inc 3 2))
+ 5)
+
+(test-expr '(let ([go (lambda (n m x)
+ (if (= n 0)
+ x
+ (go (- n 1) m (* x m))))]
+ [pow (lambda (p y) (go p y 1))])
+
+ (pow 3 2))
+ 8)
+
+(test-expr '(let ([pow (lambda (p y)
+ (let ([go (lambda (n x)
+ (if (= n 0)
+ x
+ (go (- n 1) (* x y))))])
+ (go p 1)))])
+ (pow 4 2))
+ 16)
+
+ ; mutual recursion
+;; (test-prog-stdout '((let ([f (lambda (n)
+;; (if (= n 0)
+;; 0
+;; (let ()
+;; (print "a")
+;; (g (- n 1)))))]
+;; [g (lambda (m)
+;; (let ()
+;; (print "b")
+;; (f (- m 1))))])
+;; (f 10))) "ababababab")
+
+ ; adts and pattern matching
+
+(test-prog '((data A [foo Bool Int])
+ (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))))
+
+(test-prog '((data A [foo Int])
+ (let ([x (foo 42)])
+ (let ([(foo y) x])
+ (+ 2 y))))
+ 44)
+
+(test-prog '((data A [foo Bool Int Int])
+ (let ([x (foo (= 2 1) 123 45)]
+ [(foo a b c) x])
+ (+ b c)))
+ (+ 123 45))
+
+(test-prog '((data A [foo Int])
+ (data B [bar A])
+ (let ([(bar (foo x)) (bar (foo 42))])
+ x))
+ 42)
+
+(test-prog '((data Foo [a] [b] [c])
+ (let ([x b])
+ (case x
+ [a 3]
+ [b 2]
+ [c 1])))
+ 2)
+
+(test-prog '((data Foo [foo Int Int] [bar Bool])
+ (case (foo 42 12)
+ [(foo 20 x) 0]
+ [(foo 42 x) x]
+ [(foo y x) 0]
+ [(bar x) 0]))
+ 12)
+
+(test-prog '((data Foo [foo Int])
+ (data Bar [bar Foo])
+ (case (bar (foo 42))
+ [(bar (foo x)) x]))
+ 42)
+
+
+ ; mix of singleton and non singleton constructors
+(test-prog '((data A [foo Int] [bar])
+ (case (foo 42)
+ [(foo x) x]
+ [bar 0]))
+ 42)
+
+(test-prog '((data A [foo Int] [bar])
+ (case bar
+ [(foo x) 0]
+ [bar 12]))
+ 12)
+ ; todo: make this error for incomplete pattern match
+(test-exception
+ (codegen '((data A [foo Int] [bar Int B])
+ (data B [baz Int])
+ (let ([val (bar 42 (baz 12))])
+ (case val
+ [(foo 42) 0]
+ [(bar 32 (baz 12)) 1]
+ [(bar 42 (baz x)) x]
+ [(foo x) 2])))))
+