How to use the command "ocaml" (with examples)
The “ocaml” command is the OCaml repl (read-evaluate-print-loop), which allows users to interactively execute OCaml commands. It can also be used to read and execute OCaml commands from a file or to run an OCaml script with modules.
Use case 1: Read OCaml commands from the user and execute them
Code:
ocaml
Motivation: This use case is helpful when you want to quickly test and execute OCaml commands without the need to write a separate script or file. It provides a convenient way to experiment with OCaml code interactively.
Explanation:
ocaml
: This command starts the OCaml repl and waits for input. It allows you to enter OCaml commands line by line and executes them immediately.
Example output:
OCaml version 4.x.x
# let x = 2 + 3;;
val x : int = 5
# let square n = n * n;;
val square : int -> int = <fun>
# square 4;;
- : int = 16
Use case 2: Read OCaml commands from a file and execute them
Code:
ocaml path/to/file.ml
Motivation: This use case is useful when you have a script or a file containing a sequence of OCaml commands that you want to execute all at once. Instead of manually entering each command, you can provide the file to the “ocaml” command, which will read and execute the commands sequentially.
Explanation:
ocaml
: This command opens the OCaml repl.path/to/file.ml
: This argument specifies the path to the file containing OCaml commands that you want to execute.
Example output:
Suppose we have a file named hello.ml
with the following content:
print_string "Hello, World!\n";;
let x = 2 + 3;;
print_int x;;
Executing the command ocaml hello.ml
will produce the following output:
OCaml version 4.x.x
Hello, World!
val x : int = 5
5- : unit = ()
The “Hello, World!” message is printed, the variable x
is assigned the value 5, and the value of x
is printed.
Use case 3: Run OCaml script with modules
Code:
ocaml module1 module2 path/to/file.ml
Motivation: This use case becomes handy when working on larger OCaml projects that involve multiple modules. By using the “ocaml” command with multiple module arguments, you can load the necessary modules before running an OCaml script.
Explanation:
ocaml
: This command starts the OCaml repl.module1 module2
: These arguments specify the names of the required OCaml modules to load before executing the script.path/to/file.ml
: This argument is the path to the OCaml file that you want to run.
Example output:
Suppose we have two files, math.ml
and main.ml
.
math.ml:
let square x = x * x
let cube x = x * x * x
main.ml:
let x = 5
let square_x = Math.square x
let cube_x = Math.cube x
print_int x;;
print_string "\n";
print_int square_x;;
print_string "\n";
print_int cube_x;;
Executing the command ocaml Math Main main.ml
will produce the following output:
OCaml version 4.x.x
5
25
125- : unit = ()
The value of x
is printed, followed by the squared value of x
, and finally the cubed value of x
. The modules Math
and Main
are used to access the functions square
and cube
respectively.