Type checking for miniFun, adding some tests

This commit is contained in:
elvis
2024-10-24 15:35:42 +02:00
parent 8d327a08bb
commit 0c9490780a
6 changed files with 401 additions and 13 deletions

View File

@ -4,4 +4,8 @@
(test
(name testingFun)
(libraries lang))
(test
(name testingTypeFun)
(libraries lang))

View File

@ -5,6 +5,7 @@ open Lang.MiniFun
let program =
Function
(["a"],
FunctionType ([IntegerType], IntegerType),
(Variable "a")
)
;;
@ -16,6 +17,7 @@ Printf.printf "Identity program: %d\n" (reduce program 1)
let program =
Function
(["a"],
FunctionType ([IntegerType], IntegerType),
(Integer 1)
)
;;
@ -27,7 +29,9 @@ Printf.printf "Constant program: %d\n" (reduce program 10)
let program =
LetIn
("f",
(Function (["x"; "y"], Plus (Variable "x", Variable "y"))),
(Function (["x"; "y"],
FunctionType ([IntegerType; IntegerType], IntegerType),
Plus (Variable "x", Variable "y"))),
(Application (Variable "f", [Integer 3]))
)
;;
@ -40,7 +44,10 @@ let program =
LetFun
("f",
["x"],
(Function (["y"], Plus (Variable "x", Variable "y"))),
FunctionType ([IntegerType], IntegerType),
(Function (["y"],
FunctionType ([IntegerType], IntegerType),
Plus (Variable "x", Variable "y"))),
(Application (Variable "f", [Integer 3]))
)
;;
@ -54,10 +61,13 @@ let program =
("f",
(Function (
["z"],
FunctionType ([FunctionType ([IntegerType], IntegerType)], IntegerType),
(Function (
["y"],
FunctionType ([FunctionType ([IntegerType], IntegerType)], IntegerType),
Function (
["x"],
FunctionType ([IntegerType], IntegerType),
(IfThenElse (
CmpLess (Variable "x", Integer 0),
(Application (Variable "y", [Variable "x"])),
@ -69,10 +79,10 @@ let program =
(
(Application
(Variable "f",
[Function (["x"], Plus (Variable "x", Integer 1))]
[Function (["x"], FunctionType ([IntegerType], IntegerType), Plus (Variable "x", Integer 1))]
)
),
[Function (["x"], Minus (Variable "x", Integer 1))]
[Function (["x"], FunctionType ([IntegerType], IntegerType), Minus (Variable "x", Integer 1))]
)
)
)
@ -87,6 +97,7 @@ 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")
)
@ -99,7 +110,7 @@ Printf.printf "Recursive function program: %d\n" (reduce program 10)
let program =
LetIn
("f",
(LetIn ("a", Integer 1, (Function (["y"], Plus (Variable "y", Variable "a"))))),
(LetIn ("a", Integer 1, (Function (["y"], FunctionType ([IntegerType], IntegerType), Plus (Variable "y", Variable "a"))))),
(LetIn ("a", Integer 2, Variable "f"))
)
;;
@ -112,6 +123,7 @@ 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")
)
@ -127,6 +139,7 @@ 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),
@ -134,7 +147,9 @@ let program =
Application (Variable "collatz", [(Plus (Integer 1, Times (Integer 3, Variable "n"))); Plus (Integer 1, Variable "count")]))),
(Variable "count"))
),
(Function (["x"], Application (Variable "collatz", [Variable "x"; Integer 1])))
(Function (["x"],
FunctionType ([IntegerType], IntegerType),
Application (Variable "collatz", [Variable "x"; Integer 1])))
)
;;
@ -148,6 +163,7 @@ 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)),
@ -167,6 +183,7 @@ Printf.printf "Sum multiples of 3 and 5 program: %d\n" (reduce program 12345)
let program =
Function (
["x"],
FunctionType ([IntegerType], IntegerType),
Rand (Variable "x")
)
@ -181,13 +198,16 @@ 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"], (Application (Variable "fib", [Variable "x"; Integer 0; Integer 1])))
Function (["x"],
FunctionType ([IntegerType], IntegerType),
(Application (Variable "fib", [Variable "x"; Integer 0; Integer 1])))
)
;;

View File

@ -0,0 +1,12 @@
Identity program: true
Constant program: true
Partial application of function program: true
Partial application of function program: true
Passing functions to functions program: true
Recursive function program: true
Scope program: true
Factorial program: true
Hailstone sequence's lenght program: true
Sum multiples of 3 and 5 program: true
Rand program: true
Fibonacci program: true

215
test/testingTypeFun.ml Normal file
View File

@ -0,0 +1,215 @@
open Lang.MiniFun
(* -------------------------------------------------------------------------- *)
(* 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)
;;