Files
lci/test/testingImp.ml

277 lines
9.2 KiB
OCaml
Raw Normal View History

open MiniImp.Semantics
open MiniImp.Types
(* -------------------------------------------------------------------------- *)
(* Identity program *)
let program =
Main
("a",
"b",
(Assignment ("b", (Variable "a")))
)
;;
Printf.printf "Identity program: ";
match reduce program 1 with
Ok d -> Printf.printf "%d\n" d
| Error `AbsentAssignment msg -> Printf.printf "error -> %s\n" msg
| Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg
;;
(* -------------------------------------------------------------------------- *)
(* y not defined program *)
let program =
Main
("a",
"b",
(Sequence (
(Assignment ("x", (Integer 1))),
(Assignment ("b",
(Plus ((Plus (Variable "a", Variable "x")),
(Variable "y")))))
)
)
)
;;
Printf.printf "y not defined program: ";
match reduce program 100 with
Ok d -> Printf.printf "error: %d\n" d
| Error `AbsentAssignment msg -> Printf.printf "%s\n" msg
| Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg
;;
(* -------------------------------------------------------------------------- *)
(* Factorial program *)
let program =
Main
("a",
"b",
(Sequence (
(Assignment ("b", (Integer 1))),
(For (
(Assignment ("i", (Integer 1))),
(BCmpLessEq (Variable "i", Variable "a")),
(Assignment ("i", (Plus ((Variable "i"), (Integer 1))))),
(Assignment ("b", (Times (Variable "b", Variable "i"))))
)
)
)
)
)
;;
Printf.printf "Factorial program: ";
match reduce program 10 with
Ok d -> Printf.printf "%d\n" d
| Error `AbsentAssignment msg -> Printf.printf "error -> %s\n" msg
| Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg
;;
(* -------------------------------------------------------------------------- *)
(* Hailstone sequence's lenght program *)
let program =
Main
("a",
"b",
(Sequence (
(Assignment ("b", (Integer 1))),
(While (
(BNot (BCmp ((Variable "a"), (Integer 1)))),
(Sequence (
(Assignment ("b", (Plus (Variable "b", Integer 1)))),
(If (
(BCmp (Modulo (Variable "a", Integer 2), Integer 1)),
(Assignment ("a", Plus (Times (Integer 3, Variable "a"),
Integer 1))),
(Assignment ("a", Division (Variable "a", Integer 2)))
))
))
))
))
)
;;
Printf.printf "Hailstone sequence's lenght program: ";
match reduce program 77031 with
Ok d -> Printf.printf "%d\n" d
| Error `AbsentAssignment msg -> Printf.printf "error -> %s\n" msg
| Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg
;;
(* -------------------------------------------------------------------------- *)
(* Sum multiples of 3 and 5 program *)
let program =
Main
("a",
"b",
(Sequence (
(Assignment ("b", (Integer 0))),
(For (
(Assignment ("i", Integer 0)),
(BCmpLessEq (Variable "i", Variable "a")),
(Assignment ("i", Plus (Variable "i", Integer 1))),
(If (
(BOr ((BCmp (Modulo (Variable "i", Integer 3), Integer 0)),
(BCmp (Modulo (Variable "i", Integer 5), Integer 0)))),
(Assignment ("b", Plus (Variable "b", Variable "i"))),
(Skip)
))
))
))
)
;;
Printf.printf "Sum multiples of 3 and 5 program: ";
match reduce program 12345 with
Ok d -> Printf.printf "%d\n" d
| Error `AbsentAssignment msg -> Printf.printf "error -> %s\n" msg
| Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg
;;
(* -------------------------------------------------------------------------- *)
(* Rand program *)
let program =
Main
("a",
"b",
(Assignment ("b", Rand (Variable "a")))
)
;;
Printf.printf "Rand program: ";
match reduce program 10 with
Ok d -> Printf.printf "%b\n" (d < 10)
| Error `AbsentAssignment msg -> Printf.printf "error -> %s\n" msg
| Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg
;;
(* -------------------------------------------------------------------------- *)
(* Fibonacci program *)
let program =
Main
("n",
"fnext",
(Sequence (
Sequence (
(Assignment ("fnow", Integer 0)),
(Assignment ("fnext", Integer 1))
),
(While (
(BCmpGreater (Variable "n", Integer 1)),
(Sequence (
(Sequence (
(Assignment ("tmp", Plus (Variable "fnow",
Variable "fnext"))),
(Assignment ("fnow", Variable "fnext"))
)),
(Sequence (
(Assignment ("fnext", Variable "tmp")),
(Assignment ("n", Minus (Variable "n", Integer 1)))
))
))))
))
)
;;
Printf.printf "Fibonacci program: ";
match reduce program 48 with
Ok d -> Printf.printf "%d\n" d
| Error `AbsentAssignment msg -> Printf.printf "error -> %s\n" msg
| Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg
;;
(* -------------------------------------------------------------------------- *)
(* Miller-Rabin primality test program *)
let program =
Main
("n",
"result",
Sequence (
(Assignment ("result", Integer 0)),
(Sequence (
(Sequence (
(Assignment ("s", Integer 0)),
(While (
(BCmp (Integer 0,
Modulo (
Division (
(Minus (Variable "n", Integer 1)),
(Power (Integer 2, Variable "s"))),
(Integer 2)
)
)
),
(Assignment ("s", Plus (Variable "s", Integer 1)))
))
)),
(Sequence (
(Assignment ("d", Division (Minus (Variable "n", Integer 1),
Power (Integer 2, Variable "s")))),
(For (
(Assignment ("i", Integer 20)),
(BCmpGreater (Variable "i", Integer 0)),
(Assignment ("i", Minus (Variable "i", Integer 1))),
(Sequence (
Sequence (
(Assignment
("a",
Plus (Rand (Minus (Variable "n", Integer 4)),
Integer 2))),
(Assignment ("x", PowerMod (Variable "a",
Variable "d",
Variable "n")))),
Sequence (
(For (
(Assignment ("j", Integer 0)),
(BCmpLess (Variable "j", Variable "s")),
(Assignment ("j", Plus (Variable "j", Integer 1))),
(Sequence (
Sequence (
(Assignment ("y", PowerMod (Variable "x",
Integer 2,
Variable "n"))),
(If (
(BAnd
(BAnd (BCmp (Variable "y", Integer 1),
BNot (BCmp (Variable "x",
Integer 1))),
BNot (BCmp (Variable "x",
Minus (Variable "n",
Integer 1))))),
(Assignment ("result", Integer 1)),
(Skip)
))),
(Assignment ("x", Variable "y"))
))
)),
(If (
(BNot (BCmp (Variable "y", Integer 1))),
(Assignment ("result", Integer 1)),
(Skip)
)))
))
))
))
))
)
)
;;
(* should return 0 because prime *)
Printf.printf "Miller-Rabin primality test program 1: ";
match reduce program 179424673 with
Ok d -> Printf.printf "%d\n" d
| Error `AbsentAssignment msg -> Printf.printf "error -> %s\n" msg
| Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg
;;
(* should return 1 because not prime *)
Printf.printf "Miller-Rabin primality test program 2: ";
match reduce program 179424675 with
Ok d -> Printf.printf "%d\n" d
| Error `AbsentAssignment msg -> Printf.printf "error -> %s\n" msg
| Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg
;;