LinuxCommandLibrary

ocaml

Compile and run OCaml programs

TLDR

Read OCaml commands from the user and execute them

$ ocaml
copy

Read OCaml commands from a file and execute them
$ ocaml [path/to/file.ml]
copy

Run OCaml script with modules
$ ocaml [module1] [module2] [path/to/file.ml]
copy

SYNOPSIS

ocaml [options]

PARAMETERS

-I dir
    Add dir to the list of directories searched for compiled interface files (.cmi) and bytecode files (.cmo).

-init file
    Load the script file at the beginning of the session.

-version
    Print version information and exit.

-vnum
    Print the OCaml version number and exit.

-noprompt
    Do not display the prompt, useful for non-interactive execution.

-stdin
    Read input from standard input instead of the terminal.

-safe-string
    Treat string literals as immutable. This is the default behavior since OCaml 4.02.

-labels
    Allow optional and named parameters for function calls (default in recent OCaml versions).

-no-labels
    Disallow optional and named parameters for function calls.

-warn-error list
    Treat specified warnings as errors, where list is a sequence of warning flags.

-w list
    Enable or disable specific warnings, where list is a sequence of warning flags.

-help
    Display a short usage summary and exit.

DESCRIPTION

The ocaml command launches the interactive top-level environment for the OCaml programming language. It serves as a Read-Eval-Print Loop (REPL), allowing users to input OCaml expressions, definitions, and declarations directly, which are then immediately evaluated and their results (type and value) displayed. This makes it an invaluable tool for experimenting with code snippets, learning the language's syntax and semantics, and quickly debugging small functions or modules.

The top-level automatically loads the standard library, providing access to fundamental data structures and functions. Users can enter multi-line code, ending inputs with ;; (double semi-colon) followed by Enter. It also supports special directives, prefixed with #, for tasks like loading compiled modules (#load), executing scripts (#use), or changing directories (#cd). While ocaml is excellent for interactive development, compiled OCaml code (generated by ocamlc or ocamlopt) is generally used for building and deploying applications, offering superior performance.

CAVEATS

The ocaml interactive interpreter is designed for rapid prototyping, learning, and debugging, not for executing performance-critical applications. Code run in the interpreter is typically slower than natively compiled code (generated by ocamlopt). Additionally, certain advanced features, such as linking with external C libraries, can be more complex to manage within the interactive environment compared to standard compilation workflows.

TOP-LEVEL DIRECTIVES

The ocaml interactive top-level understands special commands, known as directives, prefixed with a hash symbol (#) and terminated by double semicolons (;;). These directives facilitate various tasks within the interpreter environment:

#use "file.ml";;
Executes the OCaml code contained in the specified file.

#load "module.cmo";;
Loads a compiled bytecode module, making its contents available in the current session.

#cd "dir";;
Changes the current working directory of the interpreter.

#quit;;
Exits the OCaml interactive top-level.

#help;;
Displays a list of available top-level directives and their purposes.

#trace function;;
Enables tracing for a specified function, showing its calls and returns during execution.

HISTORY

OCaml originated from the Caml (Categorical Abstract Machine Language) family of languages, initially developed at INRIA (French National Institute for Research in Computer Science and Automation) in the early 1990s. The 'O' in OCaml stands for 'Objective,' reflecting its extension with object-oriented programming features, which were added to Caml Light. The first stable release of OCaml was in 1996. It has since undergone continuous development, evolving into a mature, multi-paradigm language widely used in research, education, and industry, particularly for compilers, static analysis tools, and financial applications.

SEE ALSO

ocamlc(1), ocamlopt(1), ocamlfind(1), ocamldep(1), ocamlyacc(1), ocamllex(1)

Copied to clipboard