Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //5V2 - let-rec
- test("letRecSameVarDiffTypes") {
- intercept[ParseException] {
- typeOf("""
- (letrec
- (
- ((x : Num) 1)
- ((x : Num) 2)
- )
- (* 2 x)
- )
- """)
- }
- }
- test("letRecBadDiffTypes") {
- intercept[TypeException] {
- typeOf("""
- (letrec
- (
- ((x : Num) 1)
- ((y : Bool) x)
- )
- (* 2 x)
- )
- """)
- }
- }
- test("letRecGood"){
- assertResult(NumT()) {
- typeOf("""
- (letrec
- (
- ((x : Num) 2)
- ((y : Num) (* 2 x))
- )
- (* 2 y)
- )
- """)
- }
- }
- test("let test - recursion") {
- val eq = """
- (letrec (((f : ((Num) -> Num)) (lambda ((x : Num))
- (if (num= x 0)
- 1
- (* x (f (- x 1))))))
- ((g : ((Num) -> Num)) (lambda ((y : Num))
- (f y))))
- (g 5))
- """
- assertResult(NumT()) {
- typeOf(eq)
- }
- }
- test("letrec parse err") {
- val eq = "(letrec ((f (lambda ((x : Bool)) (if (num= x 0) 1 (* x (f (- x 1)))))) (g (lambda ((y : Num)) (f y)))) (g 5))"
- assertThrows[ParseException] {
- typeOf(eq)
- }
- }
- test("letRecBadUnbound"){
- intercept[TypeException] {
- typeOf("""
- (letrec
- (
- ((x : Num) 2)
- ((y : Num) (* 2 x))
- )
- z
- )
- """)
- }
- }
- test("letRecForwardRefGOOD") {
- assertResult(NumT()) {
- typeOf("""
- (letrec
- (
- ((x : Num) y)
- ((y : Num) 2)
- )
- (+ x y)
- )
- """)
- }
- intercept[InterpException] {
- safeInterp("""
- (letrec
- (
- ((x : Num) y)
- ((y : Num) 2)
- )
- (+ x y)
- )
- """)
- }
- }
- test("letRecBackwarddRefGOOD") {
- assertResult(NumT()) {
- typeOf("""
- (letrec
- (
- ((x : Num) 2)
- ((y : Num) x)
- )
- (+ x y)
- )
- """)
- }
- assertResult(NumV(4)) {
- safeInterp("""
- (letrec
- (
- ((x : Num) 2)
- ((y : Num) x)
- )
- (+ x y)
- )
- """)
- }
- }
- test("letrecBadDiffTypes") {
- intercept[TypeException] {
- typeOf("""
- (letrec
- (
- ((x : Num) y)
- ((y : Bool) true)
- )
- (+ x y)
- )
- """)
- }
- }
- test("letRecDoesNotType") {
- intercept[TypeException] {
- typeOf("""
- (letrec (((x : Bool) 1)) x)
- """)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement