364 lines
10 KiB
OCaml
364 lines
10 KiB
OCaml
open Lang.MiniFun
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Error absent assignment program *)
|
|
|
|
let program =
|
|
Function
|
|
(["a"],
|
|
FunctionType ([IntegerType], IntegerType),
|
|
(Variable "x")
|
|
)
|
|
;;
|
|
|
|
try
|
|
Printf.printf "Error absent assignment program: %b\n" (typecheck program)
|
|
with AbsentAssignment _ ->
|
|
Printf.printf "Error absent assignment program: error\n"
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Error wrong arity program *)
|
|
|
|
let program =
|
|
Function
|
|
(["a"],
|
|
FunctionType ([IntegerType; IntegerType], IntegerType),
|
|
(Variable "a")
|
|
)
|
|
;;
|
|
|
|
try
|
|
Printf.printf "Error wrong arity program 1: %b\n" (typecheck program)
|
|
with WrongTypeSpecification _ ->
|
|
Printf.printf "Error wrong arity program 1: error\n"
|
|
|
|
let program =
|
|
LetFun
|
|
("f",
|
|
["a"; "b"],
|
|
FunctionType ([IntegerType; IntegerType], IntegerType),
|
|
(Variable "a"),
|
|
Function (["x"], FunctionType ([IntegerType], IntegerType), Application (Variable "f", [Integer 1; Integer 2; Variable "x"]))
|
|
)
|
|
;;
|
|
|
|
try
|
|
Printf.printf "Error wrong arity program 2: %b\n" (typecheck program)
|
|
with WrongArity _ ->
|
|
Printf.printf "Error wrong arity program 2: error\n"
|
|
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Error wrong return type program *)
|
|
|
|
let program =
|
|
Function
|
|
(["a"],
|
|
FunctionType ([IntegerType], BooleanType),
|
|
(Variable "a")
|
|
)
|
|
;;
|
|
|
|
try
|
|
Printf.printf "Error wrong return type program: %b\n" (typecheck program)
|
|
with WrongTypeSpecification _ ->
|
|
Printf.printf "Error wrong return type program: error\n"
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Error wrong specification program *)
|
|
|
|
let program =
|
|
Function
|
|
(["a"],
|
|
IntegerType,
|
|
(Variable "a")
|
|
)
|
|
;;
|
|
|
|
try
|
|
Printf.printf "Error wrong specification program: %b\n" (typecheck program)
|
|
with WrongTypeSpecification _ ->
|
|
Printf.printf "Error wrong specification program: error\n"
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Error wrong input type program *)
|
|
|
|
let program =
|
|
Application (
|
|
Function
|
|
(["a"; "b"],
|
|
FunctionType ([IntegerType; IntegerType], IntegerType),
|
|
(Variable "a")
|
|
),
|
|
[Boolean false]
|
|
)
|
|
;;
|
|
|
|
try
|
|
Printf.printf "Error wrong input type program: %b\n" (typecheck program)
|
|
with WrongType _ ->
|
|
Printf.printf "Error wrong input type program: error\n"
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Error not a function program *)
|
|
|
|
let program =
|
|
Application (
|
|
Integer 0,
|
|
[Boolean false]
|
|
)
|
|
;;
|
|
|
|
try
|
|
Printf.printf "Error not a function program: %b\n" (typecheck program)
|
|
with WrongType _ ->
|
|
Printf.printf "Error not a function program: error\n"
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Error if branches with different types program *)
|
|
|
|
let program =
|
|
Function (
|
|
["x"],
|
|
FunctionType ([IntegerType], IntegerType),
|
|
IfThenElse (Cmp (Integer 1, Integer 2), Boolean true, Integer 1)
|
|
)
|
|
;;
|
|
|
|
try
|
|
Printf.printf "Error if branches with different types program: %b\n" (typecheck program)
|
|
with WrongType _ ->
|
|
Printf.printf "Error if branches with different types program: error\n"
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Error if guard is not a boolean program *)
|
|
|
|
let program =
|
|
Function (
|
|
["x"],
|
|
FunctionType ([IntegerType], IntegerType),
|
|
IfThenElse (Integer 1, Integer 2, Integer 1)
|
|
)
|
|
;;
|
|
|
|
try
|
|
Printf.printf "Error if guard is not a boolean program: %b\n" (typecheck program)
|
|
with WrongType _ ->
|
|
Printf.printf "Error if guard is not a boolean program: error\n"
|
|
|
|
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Identity program *)
|
|
let program =
|
|
Function
|
|
(["a"],
|
|
FunctionType ([IntegerType], IntegerType),
|
|
(Variable "a")
|
|
)
|
|
;;
|
|
|
|
Printf.printf "Identity program: %b\n" (typecheck program)
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Constant program *)
|
|
let program =
|
|
Function
|
|
(["a"],
|
|
FunctionType ([IntegerType], IntegerType),
|
|
(Integer 1)
|
|
)
|
|
;;
|
|
|
|
Printf.printf "Constant program: %b\n" (typecheck program)
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Partial application of function program *)
|
|
let program =
|
|
LetIn
|
|
("f",
|
|
(Function (["x"; "y"],
|
|
FunctionType ([IntegerType; IntegerType], IntegerType),
|
|
Plus (Variable "x", Variable "y"))),
|
|
(Application (Variable "f", [Integer 3]))
|
|
)
|
|
;;
|
|
|
|
Printf.printf "Partial application of function program: %b\n" (typecheck program)
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Partial application of function program *)
|
|
let program =
|
|
LetFun
|
|
("f",
|
|
["x"],
|
|
FunctionType ([IntegerType], FunctionType ([IntegerType], IntegerType)),
|
|
(Function (["y"],
|
|
FunctionType ([IntegerType], IntegerType),
|
|
Plus (Variable "x", Variable "y"))),
|
|
(Application (Variable "f", [Integer 3]))
|
|
)
|
|
;;
|
|
|
|
Printf.printf "Partial application of function program: %b\n" (typecheck program)
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Passing functions to functions program *)
|
|
let program =
|
|
LetIn
|
|
("f",
|
|
(Function (
|
|
["z"],
|
|
FunctionType ([FunctionType ([IntegerType], IntegerType)], FunctionType ([FunctionType ([IntegerType], IntegerType)], FunctionType ([IntegerType], IntegerType))),
|
|
(Function (
|
|
["y"],
|
|
FunctionType ([FunctionType ([IntegerType], IntegerType)], FunctionType ([IntegerType], IntegerType)),
|
|
Function (
|
|
["x"],
|
|
FunctionType ([IntegerType], IntegerType),
|
|
(IfThenElse (
|
|
CmpLess (Variable "x", Integer 0),
|
|
(Application (Variable "y", [Variable "x"])),
|
|
(Application (Variable "z", [Variable "x"]))
|
|
)))
|
|
))
|
|
)),
|
|
(Application
|
|
(
|
|
(Application
|
|
(Variable "f",
|
|
[Function (["x"], FunctionType ([IntegerType], IntegerType), Plus (Variable "x", Integer 1))]
|
|
)
|
|
),
|
|
[Function (["x"], FunctionType ([IntegerType], IntegerType), Minus (Variable "x", Integer 1))]
|
|
)
|
|
)
|
|
)
|
|
;;
|
|
|
|
Printf.printf "Passing functions to functions program: %b\n" (typecheck program)
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Recursive function program *)
|
|
let program =
|
|
LetFun
|
|
("f",
|
|
["x"],
|
|
FunctionType ([IntegerType], IntegerType),
|
|
(IfThenElse (CmpLess (Variable "x", Integer 2),Integer 1, Plus (Variable "x", Application (Variable "f", [Minus (Variable "x", Integer 1)])))),
|
|
(Variable "f")
|
|
)
|
|
;;
|
|
|
|
Printf.printf "Recursive function program: %b\n" (typecheck program)
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Scope program *)
|
|
let program =
|
|
LetIn
|
|
("f",
|
|
(LetIn ("a", Integer 1, (Function (["y"], FunctionType ([IntegerType], IntegerType), Plus (Variable "y", Variable "a"))))),
|
|
(LetIn ("a", Integer 2, Variable "f"))
|
|
)
|
|
;;
|
|
|
|
Printf.printf "Scope program: %b\n" (typecheck program)
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Factorial program *)
|
|
let program =
|
|
LetFun (
|
|
"f",
|
|
["x"],
|
|
FunctionType ([IntegerType], IntegerType),
|
|
(IfThenElse (CmpLessEq (Variable "x", Integer 0), Integer 1, Times (Variable "x", Application (Variable "f", [Minus (Variable "x", Integer 1)])))),
|
|
(Variable "f")
|
|
)
|
|
;;
|
|
|
|
Printf.printf "Factorial program: %b\n" (typecheck program)
|
|
;;
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Hailstone sequence's lenght program *)
|
|
|
|
let program =
|
|
LetFun (
|
|
"collatz",
|
|
["n"; "count"],
|
|
FunctionType ([IntegerType; IntegerType], IntegerType),
|
|
(
|
|
IfThenElse (BNot (Cmp (Variable "n", Integer 1)),
|
|
(IfThenElse (Cmp (Modulo (Variable "n", Integer 2), Integer 0),
|
|
Application (Variable "collatz", [Division (Variable "n", Integer 2); Plus (Integer 1, Variable "count")]),
|
|
Application (Variable "collatz", [(Plus (Integer 1, Times (Integer 3, Variable "n"))); Plus (Integer 1, Variable "count")]))),
|
|
(Variable "count"))
|
|
),
|
|
(Function (["x"],
|
|
FunctionType ([IntegerType], IntegerType),
|
|
Application (Variable "collatz", [Variable "x"; Integer 1])))
|
|
)
|
|
;;
|
|
|
|
Printf.printf "Hailstone sequence's lenght program: %b\n" (typecheck program)
|
|
;;
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Sum multiples of 3 and 5 program *)
|
|
|
|
let program =
|
|
LetFun (
|
|
"sum",
|
|
["n"],
|
|
FunctionType ([IntegerType], IntegerType),
|
|
(IfThenElse ((BOr (Cmp (Modulo (Variable "n", Integer 3), Integer 0), Cmp (Modulo (Variable "n", Integer 5), Integer 0))),
|
|
Plus (Variable "n", Application (Variable "sum", [Minus (Variable "n", Integer 1)])),
|
|
(IfThenElse ((CmpLessEq (Variable "n", Integer 1)),
|
|
(Integer 0),
|
|
(Application (Variable "sum", [Minus (Variable "n", Integer 1)])))
|
|
))
|
|
),
|
|
(Variable "sum")
|
|
)
|
|
;;
|
|
|
|
Printf.printf "Sum multiples of 3 and 5 program: %b\n" (typecheck program)
|
|
;;
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Rand program *)
|
|
let program =
|
|
Function (
|
|
["x"],
|
|
FunctionType ([IntegerType], IntegerType),
|
|
Rand (Variable "x")
|
|
)
|
|
|
|
;;
|
|
|
|
Printf.printf "Rand program: %b\n" (typecheck program)
|
|
;;
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
(* Fibonacci program *)
|
|
let program =
|
|
LetFun (
|
|
"fib",
|
|
["i"; "a"; "b"],
|
|
FunctionType ([IntegerType; IntegerType; IntegerType], IntegerType),
|
|
(IfThenElse (Cmp (Variable "i", Integer 0),
|
|
Variable "a",
|
|
Application (Variable "fib", [Minus (Variable "i", Integer 1);
|
|
Variable "b";
|
|
Plus (Variable "a", Variable "b")])
|
|
)),
|
|
Function (["x"],
|
|
FunctionType ([IntegerType], IntegerType),
|
|
(Application (Variable "fib", [Variable "x"; Integer 0; Integer 1])))
|
|
)
|
|
|
|
;;
|
|
|
|
Printf.printf "Fibonacci program: %b\n" (typecheck program)
|
|
;;
|