Removed multiple input functions, added tuples, fixed parser
This commit is contained in:
@ -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"
|
||||
|
||||
Reference in New Issue
Block a user