Files
lci/test/testingImp.ml

223 lines
6.9 KiB
OCaml

open Lang.MiniImp
(* -------------------------------------------------------------------------- *)
(* Identity program *)
let program =
Main
("a",
"b",
(Assignment ("b", (Variable "a")))
)
;;
Printf.printf "Identity program: %d\n" (reduce program 1)
(* -------------------------------------------------------------------------- *)
(* y not defined program *)
let program =
Main
("a",
"b",
(Sequence (
(Assignment ("x", (Integer 1))),
(Assignment ("b",
(Plus ((Plus (Variable "a", Variable "x")), (Variable "y")))))
)
)
)
;;
try
Printf.printf "y not defined program: %d\n" (reduce program 100)
with AbsentAssignment s ->
Printf.printf "y not defined program: %s\n" s
;;
(* -------------------------------------------------------------------------- *)
(* 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: %d\n" (reduce program 10)
;;
(* -------------------------------------------------------------------------- *)
(* 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: %d\n" (reduce program 77031)
;;
(* -------------------------------------------------------------------------- *)
(* 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: %d\n" (reduce program 12345)
;;
(* -------------------------------------------------------------------------- *)
(* Rand program *)
let program =
Main
("a",
"b",
(Assignment ("b", Rand (Variable "a")))
)
;;
Printf.printf "Rand program: %b\n" ((reduce program 10) < 10)
;;
(* -------------------------------------------------------------------------- *)
(* 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: %d\n" (reduce program 48)
;;
(* -------------------------------------------------------------------------- *)
(* 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: %d\n" (reduce program 179424673)
;;
(* should return 1 because not prime *)
Printf.printf "Miller-Rabin primality test program: %d\n" (reduce program 179424675)
;;