Removed multiple input functions, added tuples, fixed parser

This commit is contained in:
elvis
2024-11-15 17:23:04 +01:00
parent 0ff17560ee
commit 7ad217dfb0
15 changed files with 808 additions and 307 deletions

View File

@ -13,3 +13,7 @@
(test
(name testingTypeFun)
(libraries miniFun))
(test
(name testingTypeFunParser)
(libraries miniFun))

View File

@ -5,55 +5,66 @@ open MiniFun.Types
(* Identity program *)
let program =
Function
(["a"],
FunctionType ([IntegerType], IntegerType),
("a",
FunctionType (IntegerType, IntegerType),
(Variable "a")
)
;;
Printf.printf "Identity program: %d\n" (Result.get_ok (reduce program 1))
match reduce program 1 with
Ok o -> Printf.printf "Identity program: %d\n" o
| Error _ -> Printf.printf "Identity program: error\n"
(* -------------------------------------------------------------------------- *)
(* Constant program *)
let program =
Function
(["a"],
FunctionType ([IntegerType], IntegerType),
("a",
FunctionType (IntegerType, IntegerType),
(Integer 1)
)
;;
Printf.printf "Constant program: %d\n" (Result.get_ok (reduce program 10))
match reduce program 10 with
Ok o -> Printf.printf "Constant program: %d\n" o
| Error _ -> Printf.printf "Constant program: error\n"
(* -------------------------------------------------------------------------- *)
(* 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]))
(Function ("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: %d\n" (Result.get_ok (reduce program 2))
match reduce program 2 with
Ok o -> Printf.printf "Partial application of function program: %d\n" o
| Error _ -> Printf.printf "Partial application of function program: error\n"
(* -------------------------------------------------------------------------- *)
(* Partial application of function program *)
let program =
LetFun
("f",
["x"],
FunctionType ([IntegerType], IntegerType),
(Function (["y"],
FunctionType ([IntegerType], IntegerType),
"x",
FunctionType (IntegerType, IntegerType),
(Function ("y",
FunctionType (IntegerType, IntegerType),
Plus (Variable "x", Variable "y"))),
(Application (Variable "f", [Integer 3]))
(Application (Variable "f", Integer 3))
)
;;
Printf.printf "Partial application of function program: %d\n" (Result.get_ok (reduce program 3))
match reduce program 3 with
Ok o -> Printf.printf "Partial application of function program: %d\n" o
| Error _ -> Printf.printf "Partial application of function program: error\n"
(* -------------------------------------------------------------------------- *)
(* Passing functions to functions program *)
@ -61,18 +72,18 @@ let program =
LetIn
("f",
(Function (
["z"],
FunctionType ([FunctionType ([IntegerType], IntegerType)], IntegerType),
"z",
FunctionType (FunctionType (IntegerType, IntegerType), IntegerType),
(Function (
["y"],
FunctionType ([FunctionType ([IntegerType], IntegerType)], IntegerType),
"y",
FunctionType (FunctionType (IntegerType, IntegerType), IntegerType),
Function (
["x"],
FunctionType ([IntegerType], IntegerType),
"x",
FunctionType (IntegerType, IntegerType),
(IfThenElse (
CmpLess (Variable "x", Integer 0),
(Application (Variable "y", [Variable "x"])),
(Application (Variable "z", [Variable "x"]))
(Application (Variable "y", Variable "x")),
(Application (Variable "z", Variable "x"))
)))
))
)),
@ -80,82 +91,100 @@ let program =
(
(Application
(Variable "f",
[Function (["x"], FunctionType ([IntegerType], IntegerType), Plus (Variable "x", Integer 1))]
Function ("x", FunctionType (IntegerType, IntegerType), Plus (Variable "x", Integer 1))
)
),
[Function (["x"], FunctionType ([IntegerType], IntegerType), Minus (Variable "x", Integer 1))]
Function ("x", FunctionType (IntegerType, IntegerType), Minus (Variable "x", Integer 1))
)
)
)
;;
Printf.printf "Passing functions to functions program 1: %d\n" (Result.get_ok (reduce program (3)));
Printf.printf "Passing functions to functions program 2: %d\n" (Result.get_ok (reduce program (-3)))
match reduce program (3) with
Ok o -> Printf.printf "Passing functions to functions program 1: %d\n" o
| Error _ -> Printf.printf "Passing functions to functions program 1: error\n";;
match reduce program (-3) with
Ok o -> Printf.printf "Passing functions to functions program 2: %d\n" o
| Error _ -> Printf.printf "Passing functions to functions program 2: error\n"
(* -------------------------------------------------------------------------- *)
(* 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)])))),
"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: %d\n" (Result.get_ok (reduce program 10))
match reduce program 10 with
Ok o -> Printf.printf "Recursive function program: %d\n" o
| Error _ -> Printf.printf "Recursive function program: error\n"
(* -------------------------------------------------------------------------- *)
(* Scope program *)
let program =
LetIn
("f",
(LetIn ("a", Integer 1, (Function (["y"], FunctionType ([IntegerType], IntegerType), 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"))
)
;;
Printf.printf "Scope program: %d\n" (Result.get_ok (reduce program 4))
match reduce program 4 with
Ok o -> Printf.printf "Scope program: %d\n" o
| Error _ -> Printf.printf "Scope program: error\n"
(* -------------------------------------------------------------------------- *)
(* 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)])))),
"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: %d\n" (Result.get_ok (reduce program 10))
;;
match reduce program 10 with
Ok o -> Printf.printf "Factorial program: %d\n" o
| Error _ -> Printf.printf "Factorial program: error\n"
(* -------------------------------------------------------------------------- *)
(* Hailstone sequence's lenght program *)
let program =
LetFun (
"collatz",
["n"; "count"],
FunctionType ([IntegerType; IntegerType], IntegerType),
"input",
FunctionType (TupleType (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"))
IfThenElse (BNot (Cmp (First (Variable "input"), Integer 1)),
(IfThenElse (Cmp (Modulo (First (Variable "input"), Integer 2), Integer 0),
Application (Variable "collatz",
Tuple (
Division (First (Variable "input"), Integer 2),
Plus (Integer 1, Second (Variable "input")))),
Application (Variable "collatz",
Tuple (
Plus (Integer 1, Times (Integer 3, First (Variable "input"))),
Plus (Integer 1, Second (Variable "input")))))),
(Second (Variable "input")))
),
(Function (["x"],
FunctionType ([IntegerType], IntegerType),
Application (Variable "collatz", [Variable "x"; Integer 1])))
(Function ("x",
FunctionType (IntegerType, IntegerType),
Application (Variable "collatz", Tuple (Variable "x", Integer 1))))
)
;;
Printf.printf "Hailstone sequence's lenght program: %d\n" (Result.get_ok (reduce program 77031))
;;
match reduce program 77031 with
Ok o -> Printf.printf "Hailstone sequence's lenght program: %d\n" o
| Error _ -> Printf.printf "Hailstone sequence's lenght program: error\n"
(* -------------------------------------------------------------------------- *)
(* Sum multiples of 3 and 5 program *)
@ -163,55 +192,81 @@ Printf.printf "Hailstone sequence's lenght program: %d\n" (Result.get_ok (reduce
let program =
LetFun (
"sum",
["n"],
FunctionType ([IntegerType], IntegerType),
"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)])),
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)])))
(Application (Variable "sum", Minus (Variable "n", Integer 1))))
))
),
(Variable "sum")
)
;;
Printf.printf "Sum multiples of 3 and 5 program: %d\n" (Result.get_ok (reduce program 12345))
;;
match reduce program 12345 with
Ok o -> Printf.printf "Sum multiples of 3 and 5 program: %d\n" o
| Error _ -> Printf.printf "Sum multiples of 3 and 5 program: error\n"
(* -------------------------------------------------------------------------- *)
(* Rand program *)
let program =
Function (
["x"],
FunctionType ([IntegerType], IntegerType),
"x",
FunctionType (IntegerType, IntegerType),
Rand (Variable "x")
)
;;
Printf.printf "Rand program: %b\n" ((Result.get_ok (reduce program 10) < 10))
;;
match reduce program 10 with
Ok o -> if o < 10 then Printf.printf "Rand program: %b\n" true
else Printf.printf "Rand program: %b\n" false
| Error _ -> Printf.printf "Rand program: error\n"
(* -------------------------------------------------------------------------- *)
(* 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])))
"i",
FunctionType (IntegerType, FunctionType (IntegerType, FunctionType (IntegerType, IntegerType))),
Function (
"a",
FunctionType (IntegerType, FunctionType (IntegerType, IntegerType)),
Function (
"b",
FunctionType (IntegerType, IntegerType),
(IfThenElse (Cmp (Variable "i", Integer 0),
Variable "a",
Application (
Application (
Application (
Variable "fib",
Minus (Variable "i", Integer 1)),
Variable "b"),
Plus (Variable "a", Variable "b")
)
))
)
),
Function ("x",
FunctionType (IntegerType, IntegerType),
Application (
Application (
Application (
Variable "fib",
Variable "x"
),
Integer 0
),
Integer 1
)
)
)
;;
Printf.printf "Fibonacci program: %d\n" (Result.get_ok (reduce program 48))
;;
match reduce program 48 with
Ok o -> Printf.printf "Fibonacci program: %d\n" o
| Error _ -> Printf.printf "Fibonacci program: error\n"

View File

@ -1,7 +1,7 @@
open MiniImp
let get_result x =
Lexing.from_string x |> Parser.prg MiniImp.Lexer.lex |> Semantics.reduce
Lexing.from_string x |> Parser.prg Lexer.lex |> Semantics.reduce
(* -------------------------------------------------------------------------- *)
(* Identity program *)
@ -118,7 +118,6 @@ let program =
if (not y == 1) {result := 1} else {skip}
}
}"
;;
(* should return 0 because prime *)

View File

@ -1,6 +1,4 @@
Error absent assignment program: error (success)
Error wrong arity program 1: error (success)
Error wrong arity program 2: error (success)
Error wrong return type program: error (success)
Error wrong specification program: error (success)
Error wrong input type program: error (success)

View File

@ -6,8 +6,8 @@ open MiniFun.Types
let program =
Function
(["a"],
FunctionType ([IntegerType], IntegerType),
("a",
FunctionType (IntegerType, IntegerType),
Variable "x"
)
;;
@ -16,42 +16,13 @@ match typecheck program with
Error (`AbsentAssignment _) -> Printf.printf "Error absent assignment program: error (success)\n"
| _ -> Printf.printf "Error absent assignment program: failed\n"
(* -------------------------------------------------------------------------- *)
(* Error wrong arity program *)
let program =
Function
(["a"],
FunctionType ([IntegerType; IntegerType], IntegerType),
(Variable "a")
)
;;
match typecheck program with
Error (`WrongTypeSpecification _) -> Printf.printf "Error wrong arity program 1: error (success)\n"
| _ -> Printf.printf "Error wrong arity program 1: failed\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"]))
)
;;
match typecheck program with
Error (`WrongArity _) -> Printf.printf "Error wrong arity program 2: error (success)\n"
| _ -> Printf.printf "Error wrong arity program 2: failed\n"
(* -------------------------------------------------------------------------- *)
(* Error wrong return type program *)
let program =
Function
(["a"],
FunctionType ([IntegerType], BooleanType),
("a",
FunctionType (IntegerType, BooleanType),
(Variable "a")
)
;;
@ -65,7 +36,7 @@ match typecheck program with
let program =
Function
(["a"],
("a",
IntegerType,
(Variable "a")
)
@ -81,11 +52,11 @@ match typecheck program with
let program =
Application (
Function
(["a"; "b"],
FunctionType ([IntegerType; IntegerType], IntegerType),
(Variable "a")
("a",
FunctionType (IntegerType, FunctionType (IntegerType, IntegerType)),
Function ("b", FunctionType (IntegerType, IntegerType), Variable "a")
),
[Boolean false]
Boolean false
)
;;
@ -99,7 +70,7 @@ match typecheck program with
let program =
Application (
Integer 0,
[Boolean false]
Boolean false
)
;;
@ -112,8 +83,8 @@ match typecheck program with
let program =
Function (
["x"],
FunctionType ([IntegerType], IntegerType),
"x",
FunctionType (IntegerType, IntegerType),
IfThenElse (Cmp (Integer 1, Integer 2), Boolean true, Integer 1)
)
;;
@ -127,8 +98,8 @@ match typecheck program with
let program =
Function (
["x"],
FunctionType ([IntegerType], IntegerType),
"x",
FunctionType (IntegerType, IntegerType),
IfThenElse (Integer 1, Integer 2, Integer 1)
)
;;
@ -142,8 +113,8 @@ match typecheck program with
(* Identity program *)
let program =
Function
(["a"],
FunctionType ([IntegerType], IntegerType),
("a",
FunctionType (IntegerType, IntegerType),
(Variable "a")
)
;;
@ -156,8 +127,8 @@ match typecheck program with
(* Constant program *)
let program =
Function
(["a"],
FunctionType ([IntegerType], IntegerType),
("a",
FunctionType (IntegerType, IntegerType),
(Integer 1)
)
;;
@ -171,10 +142,15 @@ match typecheck program with
let program =
LetIn
("f",
(Function (["x"; "y"],
FunctionType ([IntegerType; IntegerType], IntegerType),
Plus (Variable "x", Variable "y"))),
(Application (Variable "f", [Integer 3]))
(Function (
"x",
FunctionType (IntegerType, FunctionType (IntegerType, IntegerType)),
Function ("y",
FunctionType (IntegerType, IntegerType),
Plus (Variable "x", Variable "y")
)
)),
(Application (Variable "f", Integer 3))
)
;;
@ -187,12 +163,12 @@ match typecheck program with
let program =
LetFun
("f",
["x"],
FunctionType ([IntegerType], FunctionType ([IntegerType], IntegerType)),
(Function (["y"],
FunctionType ([IntegerType], IntegerType),
"x",
FunctionType (IntegerType, FunctionType (IntegerType, IntegerType)),
(Function ("y",
FunctionType (IntegerType, IntegerType),
Plus (Variable "x", Variable "y"))),
(Application (Variable "f", [Integer 3]))
(Application (Variable "f", Integer 3))
)
;;
@ -206,18 +182,18 @@ let program =
LetIn
("f",
(Function (
["z"],
FunctionType ([FunctionType ([IntegerType], IntegerType)], FunctionType ([FunctionType ([IntegerType], IntegerType)], FunctionType ([IntegerType], IntegerType))),
"z",
FunctionType (FunctionType (IntegerType, IntegerType), FunctionType (FunctionType (IntegerType, IntegerType), FunctionType (IntegerType, IntegerType))),
(Function (
["y"],
FunctionType ([FunctionType ([IntegerType], IntegerType)], FunctionType ([IntegerType], IntegerType)),
"y",
FunctionType (FunctionType (IntegerType, IntegerType), FunctionType (IntegerType, IntegerType)),
Function (
["x"],
FunctionType ([IntegerType], IntegerType),
"x",
FunctionType (IntegerType, IntegerType),
(IfThenElse (
CmpLess (Variable "x", Integer 0),
(Application (Variable "y", [Variable "x"])),
(Application (Variable "z", [Variable "x"]))
(Application (Variable "y", Variable "x")),
(Application (Variable "z", Variable "x"))
)))
))
)),
@ -225,10 +201,10 @@ let program =
(
(Application
(Variable "f",
[Function (["x"], FunctionType ([IntegerType], IntegerType), Plus (Variable "x", Integer 1))]
Function ("x", FunctionType (IntegerType, IntegerType), Plus (Variable "x", Integer 1))
)
),
[Function (["x"], FunctionType ([IntegerType], IntegerType), Minus (Variable "x", Integer 1))]
Function ("x", FunctionType (IntegerType, IntegerType), Minus (Variable "x", Integer 1))
)
)
)
@ -243,9 +219,9 @@ match typecheck program with
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)])))),
"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")
)
;;
@ -259,7 +235,7 @@ match typecheck program with
let program =
LetIn
("f",
(LetIn ("a", Integer 1, (Function (["y"], FunctionType ([IntegerType], IntegerType), 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"))
)
;;
@ -273,9 +249,9 @@ match typecheck program with
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)])))),
"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")
)
;;
@ -290,18 +266,25 @@ match typecheck program with
let program =
LetFun (
"collatz",
["n"; "count"],
FunctionType ([IntegerType; IntegerType], IntegerType),
"input",
FunctionType (TupleType (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"))
IfThenElse (BNot (Cmp (First (Variable "input"), Integer 1)),
(IfThenElse (Cmp (Modulo (First (Variable "input"), Integer 2), Integer 0),
Application (Variable "collatz",
Tuple (
Division (First (Variable "input"), Integer 2),
Plus (Integer 1, Second (Variable "input")))),
Application (Variable "collatz",
Tuple (
Plus (Integer 1, Times (Integer 3, First (Variable "input"))),
Plus (Integer 1, Second (Variable "input")))))),
(Second (Variable "input")))
),
(Function (["x"],
FunctionType ([IntegerType], IntegerType),
Application (Variable "collatz", [Variable "x"; Integer 1])))
(Function ("x",
FunctionType (IntegerType, IntegerType),
Application (Variable "collatz", Tuple (Variable "x", Integer 1)))
)
)
;;
@ -315,13 +298,13 @@ match typecheck program with
let program =
LetFun (
"sum",
["n"],
FunctionType ([IntegerType], IntegerType),
"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)])),
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)])))
(Application (Variable "sum", Minus (Variable "n", Integer 1))))
))
),
(Variable "sum")
@ -336,8 +319,8 @@ match typecheck program with
(* Rand program *)
let program =
Function (
["x"],
FunctionType ([IntegerType], IntegerType),
"x",
FunctionType (IntegerType, IntegerType),
Rand (Variable "x")
)
@ -352,19 +335,20 @@ match typecheck program with
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")])
"input",
FunctionType (TupleType (TupleType (IntegerType, IntegerType), IntegerType), IntegerType),
(IfThenElse (Cmp (First (First (Variable "input")), Integer 0),
Second (First (Variable "input")),
Application (Variable "fib",
Tuple ( Tuple (
Minus (First (First (Variable "input")), Integer 1),
Second (Variable "input")),
Plus (Second (First (Variable "input")), Second (Variable "input"))))
)),
Function (["x"],
FunctionType ([IntegerType], IntegerType),
(Application (Variable "fib", [Variable "x"; Integer 0; Integer 1])))
Function ("x",
FunctionType (IntegerType, IntegerType),
(Application (Variable "fib", Tuple (Tuple (Variable "x", Integer 0), Integer 1))))
)
;;
match typecheck program with

View File

@ -0,0 +1,20 @@
Error absent assignment program: error (success)
Error wrong type program: error (success)
Error wrong return type program: error (success)
Error wrong specification 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 1: success
Partial application of function program 2: success
Passing functions to functions program: success
Recursive function program: success
Scope program: success
Factorial program: success
Hailstone sequence's lenght program: success
Sum multiples of 3 and 5 program: success
Rand program: success
Fibonacci program: success

View File

@ -0,0 +1,251 @@
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"