252 lines
7.4 KiB
OCaml
252 lines
7.4 KiB
OCaml
open MiniFun
|
|
|
|
let get_result x =
|
|
Lexing.from_string x |> Parser.prg Lexer.lex |> TypeChecker.typecheck
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* 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 type program *)
|
|
|
|
let program =
|
|
"lambda a: (int, int) -> int => a"
|
|
;;
|
|
|
|
Printf.printf "Error wrong type program: ";
|
|
match get_result program with
|
|
Error (`WrongTypeSpecification _) -> Printf.printf "error (success)\n"
|
|
| _ -> Printf.printf " failed\n"
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Error wrong return type program *)
|
|
|
|
let program =
|
|
"lambda a: int -> bool => a"
|
|
;;
|
|
|
|
Printf.printf "Error wrong return type program: ";
|
|
match get_result program with
|
|
Error (`WrongTypeSpecification _) -> Printf.printf "error (success)\n"
|
|
| _ -> Printf.printf " failed\n"
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Error wrong specification program *)
|
|
|
|
let program =
|
|
"lambda a: int => a"
|
|
;;
|
|
|
|
Printf.printf "Error wrong specification program: ";
|
|
match get_result program with
|
|
Error (`WrongTypeSpecification _) -> 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 1: ";
|
|
match get_result program with
|
|
Ok _ -> Printf.printf "success\n"
|
|
| _ -> Printf.printf " failed\n"
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Partial application of function program *)
|
|
let program =
|
|
"let rec f x: int -> int -> int = lambda y: int -> int => x + y in f 3"
|
|
;;
|
|
|
|
Printf.printf "Partial application of function program 2: ";
|
|
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"
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Recursive function program *)
|
|
let program =
|
|
"let rec f x: int -> int = if x < 2 then 1 else x + f (x - 1) in f"
|
|
;;
|
|
|
|
Printf.printf "Recursive function 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"
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Factorial program *)
|
|
let program =
|
|
"let rec f x: int -> int = if x <= 0 then 1 else x * f (x - 1) in f"
|
|
;;
|
|
|
|
Printf.printf "Factorial program: ";
|
|
match get_result program with
|
|
Ok _ -> Printf.printf "success\n"
|
|
| _ -> Printf.printf " failed\n"
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Hailstone sequence's lenght program *)
|
|
|
|
let program =
|
|
"let rec collatz input: (int, int) -> int =
|
|
if not (fst input == 1) then
|
|
if fst input % 2 == 0 then collatz ((fst input / 2), (snd input + 1))
|
|
else collatz ((1 + fst input * 3), (snd input + 1))
|
|
else snd input
|
|
in fun x: int -> int => collatz (x, 1)
|
|
"
|
|
;;
|
|
|
|
Printf.printf "Hailstone sequence's lenght program: ";
|
|
match get_result program with
|
|
Ok _ -> Printf.printf "success\n"
|
|
| _ -> Printf.printf " failed\n"
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Sum multiples of 3 and 5 program *)
|
|
|
|
let program =
|
|
"let rec sum n: int -> int = if n % 3 == 0 || n % 5 == 0 then n + sum (n - 1) else if n < 1 then 0 else sum (n - 1) in sum"
|
|
;;
|
|
|
|
Printf.printf "Sum multiples of 3 and 5 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"
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Fibonacci program *)
|
|
let program =
|
|
"let rec fib input:
|
|
int, int, int -> int =
|
|
if fst fst input == 0
|
|
then snd fst input
|
|
else fib (((fst fst input - 1), snd input), (snd fst input + snd input))
|
|
in lambda x: int -> int => fib ((x, 0), 1)"
|
|
;;
|
|
|
|
Printf.printf "Fibonacci program: ";
|
|
match get_result program with
|
|
Ok _ -> Printf.printf "success\n"
|
|
| _ -> Printf.printf " failed\n"
|