108 lines
2.6 KiB
OCaml
108 lines
2.6 KiB
OCaml
|
|
open Lang.MiniFun
|
||
|
|
|
||
|
|
(* -------------------------------------------------------------------------- *)
|
||
|
|
(* Identity program *)
|
||
|
|
let program =
|
||
|
|
Function
|
||
|
|
(["a"],
|
||
|
|
(Variable "a")
|
||
|
|
)
|
||
|
|
;;
|
||
|
|
|
||
|
|
Printf.printf "%d\n" (reduce program 1)
|
||
|
|
|
||
|
|
(* -------------------------------------------------------------------------- *)
|
||
|
|
(* Constant program *)
|
||
|
|
let program =
|
||
|
|
Function
|
||
|
|
(["a"],
|
||
|
|
(Integer 1)
|
||
|
|
)
|
||
|
|
;;
|
||
|
|
|
||
|
|
Printf.printf "%d\n" (reduce program 10)
|
||
|
|
|
||
|
|
(* -------------------------------------------------------------------------- *)
|
||
|
|
(* Partial application of function program *)
|
||
|
|
let program =
|
||
|
|
LetIn
|
||
|
|
("f",
|
||
|
|
(Function (["x"; "y"], Plus (Variable "x", Variable "y"))),
|
||
|
|
(Application (Variable "f", [Integer 3]))
|
||
|
|
)
|
||
|
|
;;
|
||
|
|
|
||
|
|
Printf.printf "%d\n" (reduce program 2)
|
||
|
|
|
||
|
|
(* -------------------------------------------------------------------------- *)
|
||
|
|
(* Partial application of function program *)
|
||
|
|
let program =
|
||
|
|
LetFun
|
||
|
|
("f",
|
||
|
|
["x"],
|
||
|
|
(Function (["y"], Plus (Variable "x", Variable "y"))),
|
||
|
|
(Application (Variable "f", [Integer 3]))
|
||
|
|
)
|
||
|
|
;;
|
||
|
|
|
||
|
|
Printf.printf "%d\n" (reduce program 3)
|
||
|
|
|
||
|
|
(* -------------------------------------------------------------------------- *)
|
||
|
|
(* Passing functions to functions program *)
|
||
|
|
let program =
|
||
|
|
LetIn
|
||
|
|
("f",
|
||
|
|
(Function (
|
||
|
|
["z"],
|
||
|
|
(Function (
|
||
|
|
["y"],
|
||
|
|
Function (
|
||
|
|
["x"],
|
||
|
|
(IfThenElse (
|
||
|
|
CmpLess (Variable "x", Integer 0),
|
||
|
|
(Application (Variable "y", [Variable "x"])),
|
||
|
|
(Application (Variable "z", [Variable "x"]))
|
||
|
|
)))
|
||
|
|
))
|
||
|
|
)),
|
||
|
|
(Application
|
||
|
|
(
|
||
|
|
(Application
|
||
|
|
(Variable "f",
|
||
|
|
[Function (["x"], Plus (Variable "x", Integer 1))]
|
||
|
|
)
|
||
|
|
),
|
||
|
|
[Function (["x"], Minus (Variable "x", Integer 1))]
|
||
|
|
)
|
||
|
|
)
|
||
|
|
)
|
||
|
|
;;
|
||
|
|
|
||
|
|
Printf.printf "%d\n" (reduce program (3));
|
||
|
|
Printf.printf "%d\n" (reduce program (-3))
|
||
|
|
|
||
|
|
(* -------------------------------------------------------------------------- *)
|
||
|
|
(* Recursive function program *)
|
||
|
|
let program =
|
||
|
|
LetFun
|
||
|
|
("f",
|
||
|
|
["x"],
|
||
|
|
(IfThenElse (CmpLess (Variable "x", Integer 2),Integer 1, Plus (Variable "x", Application (Variable "f", [Minus (Variable "x", Integer 1)])))),
|
||
|
|
(Variable "f")
|
||
|
|
)
|
||
|
|
;;
|
||
|
|
|
||
|
|
Printf.printf "%d\n" (reduce program 10)
|
||
|
|
|
||
|
|
(* -------------------------------------------------------------------------- *)
|
||
|
|
(* Scope program *)
|
||
|
|
let program =
|
||
|
|
LetIn
|
||
|
|
("f",
|
||
|
|
(LetIn ("a", Integer 1, (Function (["y"], Plus (Variable "y", Variable "a"))))),
|
||
|
|
(LetIn ("a", Integer 2, Variable "f"))
|
||
|
|
)
|
||
|
|
;;
|
||
|
|
|
||
|
|
Printf.printf "%d\n" (reduce program 4)
|