Adding simple Algorithm W implementation (no recursive functions)
This commit is contained in:
@ -25,3 +25,7 @@
|
||||
(test
|
||||
(name testingTypeFunParser)
|
||||
(libraries miniFun))
|
||||
|
||||
(test
|
||||
(name testingPolyFunParser)
|
||||
(libraries miniFun))
|
||||
|
||||
11
test/testingPolyFunParser.expected
Normal file
11
test/testingPolyFunParser.expected
Normal file
@ -0,0 +1,11 @@
|
||||
Error absent assignment program: error (success)
|
||||
Error wrong input type program: error (success)
|
||||
Error not a function program: error (success)
|
||||
Error if branches with different types program: error (success)
|
||||
Error if guard is not a boolean program: error (success)
|
||||
Identity program: success
|
||||
Constant program: success
|
||||
Partial application of function program: success
|
||||
Passing functions to functions program: success
|
||||
Scope program: success
|
||||
Rand program: success
|
||||
138
test/testingPolyFunParser.ml
Normal file
138
test/testingPolyFunParser.ml
Normal file
@ -0,0 +1,138 @@
|
||||
open MiniFun
|
||||
|
||||
let get_result x =
|
||||
Lexing.from_string x
|
||||
|> Parser.prg Lexer.lex
|
||||
|> TypeChecker.typecheck_polymorphic
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Error absent assignment program *)
|
||||
|
||||
let program =
|
||||
"lambda a: int -> int => x"
|
||||
;;
|
||||
|
||||
Printf.printf "Error absent assignment program: ";
|
||||
match get_result program with
|
||||
Error (`AbsentAssignment _) -> Printf.printf "error (success)\n"
|
||||
| _ -> Printf.printf "failed\n"
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Error wrong input type program *)
|
||||
|
||||
let program =
|
||||
"(lambda a: int -> int => a) false"
|
||||
;;
|
||||
|
||||
Printf.printf "Error wrong input type program: ";
|
||||
match get_result program with
|
||||
Error (`WrongType _) -> Printf.printf "error (success)\n"
|
||||
| _ -> Printf.printf " failed\n"
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Error not a function program *)
|
||||
|
||||
let program =
|
||||
"0 false"
|
||||
;;
|
||||
|
||||
Printf.printf "Error not a function program: ";
|
||||
match get_result program with
|
||||
Error (`WrongType _) -> Printf.printf "error (success)\n"
|
||||
| _ -> Printf.printf " failed\n"
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Error if branches with different types program *)
|
||||
|
||||
let program =
|
||||
"lambda x: int -> int => if 1 == 2 then true else 1"
|
||||
;;
|
||||
|
||||
Printf.printf "Error if branches with different types program: ";
|
||||
match get_result program with
|
||||
Error (`WrongType _) -> Printf.printf "error (success)\n"
|
||||
| _ -> Printf.printf " failed\n"
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Error if guard is not a boolean program *)
|
||||
|
||||
let program =
|
||||
"lambda x: int -> int => (if 1 then 2 else 1)"
|
||||
;;
|
||||
|
||||
Printf.printf "Error if guard is not a boolean program: ";
|
||||
match get_result program with
|
||||
Error (`WrongType _) -> Printf.printf "error (success)\n"
|
||||
| _ -> Printf.printf " failed\n"
|
||||
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Identity program *)
|
||||
let program =
|
||||
"lambda a: int -> int => a"
|
||||
;;
|
||||
|
||||
Printf.printf "Identity program: ";
|
||||
match get_result program with
|
||||
Ok _ -> Printf.printf "success\n"
|
||||
| _ -> Printf.printf " failed\n"
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Constant program *)
|
||||
let program =
|
||||
"lambda a: int -> int => 1"
|
||||
;;
|
||||
|
||||
Printf.printf "Constant program: ";
|
||||
match get_result program with
|
||||
Ok _ -> Printf.printf "success\n"
|
||||
| _ -> Printf.printf " failed\n"
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Partial application of function program *)
|
||||
let program =
|
||||
"let f = lambda x: int -> int -> int => lambda y: int -> int => x + y in f 3"
|
||||
;;
|
||||
|
||||
Printf.printf "Partial application of function program: ";
|
||||
match get_result program with
|
||||
Ok _ -> Printf.printf "success\n"
|
||||
| _ -> Printf.printf " failed\n"
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Passing functions to functions program *)
|
||||
let program =
|
||||
"let f =
|
||||
\\z: (int -> int) -> (int -> int) -> int -> int =>
|
||||
\\y: (int -> int) -> int -> int =>
|
||||
\\x: int -> int =>
|
||||
if x < 0 then y x else z x
|
||||
in (f (\\x: int -> int => x + 1)) (\\x: int -> int => x - 1)"
|
||||
;;
|
||||
|
||||
Printf.printf "Passing functions to functions program: ";
|
||||
match get_result program with
|
||||
Ok _ -> Printf.printf "success\n"
|
||||
| _ -> Printf.printf " failed\n"
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Scope program *)
|
||||
let program =
|
||||
"let f = let a = 1 in fun y: int -> int => y + a in let a = 2 in f"
|
||||
;;
|
||||
|
||||
Printf.printf "Scope program: ";
|
||||
match get_result program with
|
||||
Ok _ -> Printf.printf "success\n"
|
||||
| _ -> Printf.printf " failed\n"
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Rand program *)
|
||||
let program =
|
||||
"fun x: int -> int => rand x"
|
||||
;;
|
||||
|
||||
Printf.printf "Rand program: ";
|
||||
match get_result program with
|
||||
Ok _ -> Printf.printf "success\n"
|
||||
| _ -> Printf.printf " failed\n"
|
||||
Reference in New Issue
Block a user