open MiniFun.Semantics open MiniFun.Types (* -------------------------------------------------------------------------- *) (* Identity program *) let program = Function ("a", FunctionType (IntegerType, IntegerType), (Variable "a") ) ;; 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), (Integer 1) ) ;; 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", FunctionType (IntegerType, FunctionType (IntegerType, IntegerType)), (Function ("y", FunctionType (IntegerType, IntegerType), Plus (Variable "x", Variable "y")) ) )), (Application (Variable "f", Integer 3)) ) ;; 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), Plus (Variable "x", Variable "y"))), (Application (Variable "f", Integer 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 *) let program = LetIn ("f", (Function ( "z", FunctionType (FunctionType (IntegerType, IntegerType), IntegerType), (Function ( "y", FunctionType (FunctionType (IntegerType, IntegerType), IntegerType), Function ( "x", FunctionType (IntegerType, IntegerType), (IfThenElse ( CmpLess (Variable "x", Integer 0), (Application (Variable "y", Variable "x")), (Application (Variable "z", Variable "x")) ))) )) )), (Application ( (Application (Variable "f", Function ("x", FunctionType (IntegerType, IntegerType), Plus (Variable "x", Integer 1)) ) ), Function ("x", FunctionType (IntegerType, IntegerType), Minus (Variable "x", Integer 1)) ) ) ) ;; 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))))), (Variable "f") ) ;; 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 2, Variable "f")) ) ;; 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))))), (Variable "f") ) ;; 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", "input", FunctionType (TupleType (IntegerType, IntegerType), IntegerType), ( 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", Tuple (Variable "x", Integer 1)))) ) ;; 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 *) let program = LetFun ( "sum", "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))), (IfThenElse ((CmpLessEq (Variable "n", Integer 1)), (Integer 0), (Application (Variable "sum", Minus (Variable "n", Integer 1)))) )) ), (Variable "sum") ) ;; 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), Rand (Variable "x") ) ;; 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", 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 ) ) ) ;; match reduce program 48 with Ok o -> Printf.printf "Fibonacci program: %d\n" o | Error _ -> Printf.printf "Fibonacci program: error\n"