Adding more tests, fixing one test for miniImp, adding more functionality to miniFun

This commit is contained in:
elvis
2024-10-21 17:46:19 +02:00
parent 8b274ebda4
commit 8d327a08bb
6 changed files with 138 additions and 43 deletions

View File

@ -175,7 +175,7 @@ and evaluate_b (mem: memory) (exp_b: b_exp) =
) )
let reduce (iin : int) (program: p_exp) = let reduce (program: p_exp) (iin : int) =
match program with match program with
Main (vin, vout, expression) -> ( Main (vin, vout, expression) -> (
let mem : memory = {assignments = (VariableMap.empty |> VariableMap.add vin iin)} in let mem : memory = {assignments = (VariableMap.empty |> VariableMap.add vin iin)} in

View File

@ -41,4 +41,4 @@ type memory = {
exception AbsentAssignment of string exception AbsentAssignment of string
val reduce : int -> p_exp -> int val reduce : p_exp -> int -> int

View File

@ -1,7 +1,13 @@
1 Identity program: 1
1 Constant program: 1
5 Partial application of function program: 5
6 Partial application of function program: 6
4 Passing functions to functions program 1: 4
-4 Passing functions to functions program 2: -4
55 Recursive function program: 55
Scope program: 5
Factorial program: 3628800
Hailstone sequence's lenght program: 351
Sum multiples of 3 and 5 program: 35565945
Rand program: true
Fibonacci program: 4807526976

View File

@ -9,7 +9,7 @@ let program =
) )
;; ;;
Printf.printf "%d\n" (reduce program 1) Printf.printf "Identity program: %d\n" (reduce program 1)
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
(* Constant program *) (* Constant program *)
@ -20,7 +20,7 @@ let program =
) )
;; ;;
Printf.printf "%d\n" (reduce program 10) Printf.printf "Constant program: %d\n" (reduce program 10)
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
(* Partial application of function program *) (* Partial application of function program *)
@ -32,7 +32,7 @@ let program =
) )
;; ;;
Printf.printf "%d\n" (reduce program 2) Printf.printf "Partial application of function program: %d\n" (reduce program 2)
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
(* Partial application of function program *) (* Partial application of function program *)
@ -45,7 +45,7 @@ let program =
) )
;; ;;
Printf.printf "%d\n" (reduce program 3) Printf.printf "Partial application of function program: %d\n" (reduce program 3)
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
(* Passing functions to functions program *) (* Passing functions to functions program *)
@ -78,8 +78,8 @@ let program =
) )
;; ;;
Printf.printf "%d\n" (reduce program (3)); Printf.printf "Passing functions to functions program 1: %d\n" (reduce program (3));
Printf.printf "%d\n" (reduce program (-3)) Printf.printf "Passing functions to functions program 2: %d\n" (reduce program (-3))
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
(* Recursive function program *) (* Recursive function program *)
@ -92,7 +92,7 @@ let program =
) )
;; ;;
Printf.printf "%d\n" (reduce program 10) Printf.printf "Recursive function program: %d\n" (reduce program 10)
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
(* Scope program *) (* Scope program *)
@ -104,4 +104,93 @@ let program =
) )
;; ;;
Printf.printf "%d\n" (reduce program 4) Printf.printf "Scope program: %d\n" (reduce program 4)
(* -------------------------------------------------------------------------- *)
(* Factorial program *)
let program =
LetFun (
"f",
["x"],
(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" (reduce program 10)
;;
(* -------------------------------------------------------------------------- *)
(* Hailstone sequence's lenght program *)
let program =
LetFun (
"collatz",
["n"; "count"],
(
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"], Application (Variable "collatz", [Variable "x"; Integer 1])))
)
;;
Printf.printf "Hailstone sequence's lenght program: %d\n" (reduce program 77031)
;;
(* -------------------------------------------------------------------------- *)
(* Sum multiples of 3 and 5 program *)
let program =
LetFun (
"sum",
["n"],
(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: %d\n" (reduce program 12345)
;;
(* -------------------------------------------------------------------------- *)
(* Rand program *)
let program =
Function (
["x"],
Rand (Variable "x")
)
;;
Printf.printf "Rand program: %b\n" ((reduce program 10) < 10)
;;
(* -------------------------------------------------------------------------- *)
(* Fibonacci program *)
let program =
LetFun (
"fib",
["i"; "a"; "b"],
(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])))
)
;;
Printf.printf "Fibonacci program: %d\n" (reduce program 48)
;;

View File

@ -1,9 +1,9 @@
1 Identity program: 1
The variable y is not defined. y not defined program: The variable y is not defined.
3628800 Factorial program: 3628800
351 Hailstone sequence's lenght program: 351
35553600 Sum multiples of 3 and 5 program: 35565945
true Rand program: true
4807526976 Fibonacci program: 4807526976
0 Miller-Rabin primality test program: 0
1 Miller-Rabin primality test program: 1

View File

@ -10,7 +10,7 @@ let program =
) )
;; ;;
Printf.printf "%d\n" (reduce 1 program) Printf.printf "Identity program: %d\n" (reduce program 1)
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
(* y not defined program *) (* y not defined program *)
@ -28,13 +28,13 @@ let program =
;; ;;
try try
Printf.printf "%d\n" (reduce 100 program) Printf.printf "y not defined program: %d\n" (reduce program 100)
with AbsentAssignment s -> with AbsentAssignment s ->
Printf.printf "%s\n" s Printf.printf "y not defined program: %s\n" s
;; ;;
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
(* factorial program *) (* Factorial program *)
let program = let program =
Main Main
("a", ("a",
@ -53,11 +53,11 @@ let program =
) )
;; ;;
Printf.printf "%d\n" (reduce 10 program) Printf.printf "Factorial program: %d\n" (reduce program 10)
;; ;;
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
(* hailstone sequence's lenght program *) (* Hailstone sequence's lenght program *)
let program = let program =
Main Main
("a", ("a",
@ -79,11 +79,11 @@ let program =
) )
;; ;;
Printf.printf "%d\n" (reduce 77031 program) Printf.printf "Hailstone sequence's lenght program: %d\n" (reduce program 77031)
;; ;;
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
(* sum multiples of 3 and 5 program *) (* Sum multiples of 3 and 5 program *)
let program = let program =
Main Main
("a", ("a",
@ -92,7 +92,7 @@ let program =
(Assignment ("b", (Integer 0))), (Assignment ("b", (Integer 0))),
(For ( (For (
(Assignment ("i", Integer 0)), (Assignment ("i", Integer 0)),
(BCmpLess (Variable "i", Variable "a")), (BCmpLessEq (Variable "i", Variable "a")),
(Assignment ("i", Plus (Variable "i", Integer 1))), (Assignment ("i", Plus (Variable "i", Integer 1))),
(If ( (If (
(BOr ((BCmp (Modulo (Variable "i", Integer 3), Integer 0)), (BOr ((BCmp (Modulo (Variable "i", Integer 3), Integer 0)),
@ -105,11 +105,11 @@ let program =
) )
;; ;;
Printf.printf "%d\n" (reduce 12345 program) Printf.printf "Sum multiples of 3 and 5 program: %d\n" (reduce program 12345)
;; ;;
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
(* rand program *) (* Rand program *)
let program = let program =
Main Main
("a", ("a",
@ -118,11 +118,11 @@ let program =
) )
;; ;;
Printf.printf "%b\n" ((reduce 10 program) < 10) Printf.printf "Rand program: %b\n" ((reduce program 10) < 10)
;; ;;
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
(* fibonacci program *) (* Fibonacci program *)
let program = let program =
Main Main
("n", ("n",
@ -148,7 +148,7 @@ let program =
) )
;; ;;
Printf.printf "%d\n" (reduce 48 program) Printf.printf "Fibonacci program: %d\n" (reduce program 48)
;; ;;
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
@ -215,8 +215,8 @@ let program =
;; ;;
(* should return 0 because prime *) (* should return 0 because prime *)
Printf.printf "%d\n" (reduce 179424673 program) Printf.printf "Miller-Rabin primality test program: %d\n" (reduce program 179424673)
;; ;;
(* sould return 1 because not prime *) (* should return 1 because not prime *)
Printf.printf "%d\n" (reduce 179424675 program) Printf.printf "Miller-Rabin primality test program: %d\n" (reduce program 179424675)
;; ;;