Commit 73502961 authored by Taddeus Kroes's avatar Taddeus Kroes

funclang series5: Prettified test functions so that the test result is more readable.

parent e74be202
......@@ -45,7 +45,13 @@ let rec expr2string =
^ " then " ^ expr2string(e1)
^ " else " ^ expr2string(e2)
| Fun (arg, e) -> "fun " ^ arg ^ " -> " ^ expr2string(e)
| FunAp (e1, e2) -> "(" ^ expr2string(e1) ^ " " ^ expr2string(e2) ^ ")"
| FunAp (e1, e2) ->
(* Add parenthesis around argument when needed *)
let eval_e2 = expr2string(e2) in
let eval_e2 = (if (String.get eval_e2 0) != '('
&& String.contains eval_e2 ' '
then "(" ^ eval_e2 ^ ")" else eval_e2) in
"(" ^ expr2string(e1) ^ " " ^ eval_e2 ^ ")"
| Let (var, e1, e2) -> "let " ^ var ^ " = " ^ expr2string(e1)
^ " in " ^ expr2string(e2)
| LetRec (var, e1, e2) -> "let rec " ^ var ^ " = " ^ expr2string(e1)
......
#use "ass13.ml";;
(* let a = 1 in b *)
print_endline (expr2string (Let ("a", (Num 1), (Var "b"))));;
(* a + 3 *)
print_endline (expr2string (BinopAp (Add, Var "a", Num 3)));;
(* -a *)
print_endline (expr2string (MonopAp (Neg, Var "a")));;
let show_freevars e =
let free = match freevars e with
#use "ass13.ml"
let test tested_method e expect yield =
if yield = expect then
print_endline "SUCCESS"
else
Printf.printf "FAILURE: Method: %s
Input: %s
Expected: %s
Got: %s\n"
tested_method (expr2string e) expect yield
let test_expr2string expect e = test "expr2string" e expect (expr2string e)
let test_freevars expect e =
let yield = match freevars e with
[] -> ""
| [x] -> x
| h::t -> List.fold_left (fun a b -> a ^ ", " ^ b) h t
in
print_endline ("\nFree variables in:\n" ^ (expr2string e)
^ "\nare: [" ^ free ^ "]")
;;
test "freevars" e expect yield
let test_eval expect e = test "eval" e expect (expr2string (eval e));;
test_expr2string "let a = 1 in b" (Let ("a", (Num 1), (Var "b")));;
test_expr2string "a + 3" (BinopAp (Add, Var "a", Num 3));;
test_expr2string "-a" (MonopAp (Neg, Var "a"));;
test_expr2string "a * 3" (BinopAp (Mul, Var "a", Num 3));;
(* Write the expressions of assignment series 1 in our data types *)
let l arg body = Fun (arg, body);;
let app func arg = FunAp (func, arg);;
(* Define resursive factorial function for test usage *)
let fac =
let eq = BinopAp (Eq, Var "n", Num 0) in
let sub = BinopAp (Sub, Var "n", Num 1) in
let mul = BinopAp (Mul, Var "n", app (Var "fac") sub) in
let body = Cond (eq, Num 1, mul) in
LetRec ("fac", Fun ("n", body), Var "fac");;
test_expr2string ("let rec fac = fun n -> if n = 0 then 1 else "
^ "n * (fac (n - 1)) in fac") fac;;
(* Write the expressions of assignment series 1 in our data types *)
let ass1a =
let c = Var "c" in
let bcc = l "b" (l "c" c) in
......@@ -47,46 +64,20 @@ let ass1b =
let c = Var "c" in
(app (app (app (app uvw xyz) a) b) c);;
(* [a] *)
show_freevars (Fun ("b", (BinopAp (Add, Var "a", Num 3))));;
(* [] *)
show_freevars (Fun ("a", (BinopAp (Add, Var "a", Num 3))));;
test_freevars "a" (Fun ("b", (BinopAp (Add, Var "a", Num 3))));;
test_freevars "" (Fun ("a", (BinopAp (Add, Var "a", Num 3))));;
test_freevars "" ass1a;;
test_freevars "a, b, c" ass1b;;
let l arg body = Fun (arg, body);;
let app f e = FunAp (f, e);;
(* [] *)
show_freevars ass1a;;
(* [a, b, c] *)
show_freevars ass1b;;
let show_eval e =
print_endline ("\nEvaluation of:\n" ^ (expr2string e)
^ "\nis:\n" ^ (expr2string (eval e)))
;;
(* -5 *)
show_eval (BinopAp (Sub, Num 4, Num 9));;
test_eval "-5" (BinopAp (Sub, Num 4, Num 9));;
test_eval "false" (BinopAp (Lt, Num 6, Num 5));;
test_eval "true" (BinopAp (Or, Bool true, Bool false));;
test_eval "fun u -> fun w -> fun a -> a" ass1a;;
test_eval "(a (b c))" ass1b;;
test_eval "c + b" (Let ("a", Var "c", (BinopAp (Add, Var "a", Var "b"))));;
(* false *)
show_eval (BinopAp (Lt, Num 6, Num 5));;
(* true *)
show_eval (BinopAp (Or, Bool true, Bool false));;
(* fun u -> fun w -> fun a -> a *)
show_eval ass1a;;
(* (a (b c)) *)
show_eval ass1b;;
(* fun y' -> fun z -> ((y y') z) *)
let x = Var "x" in
let y = Var "y" in
let z = Var "z" in
show_eval (app (l "x" (l "y" (l "z" (app (app x y) z)))) y);;
(* c + b *)
show_eval (Let ("a", Var "c", (BinopAp (Add, Var "a", Var "b"))));;
test_eval "fun y' -> fun z -> ((y y') z)"
(app (l "x" (l "y" (l "z" (app (app x y) z)))) y);;
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment