OCAML(1)
๐ธ OCAML(1) โ the OCaml interactive toplevel, bestie edition ๐ธ
Okay so this is the ocaml(1) page from the General Commands Manual, and it's literally the OCaml interactive toplevel. Grab a lil snack, we're going through all of it. ๐
โจ NAME โจ
ocaml โ the OCaml interactive toplevel. Yes bestie, that's the whole name section. Iconic and brief.
๐ SYNOPSIS ๐
ocaml [ options ] [ object-files ] [ script-file ]
๐ฆ DESCRIPTION ๐ฆ
The ocaml(1) command is the toplevel system for OCaml. It lets you use OCaml interactively through a read-eval-print loop. In this mode, the system repeatedly reads OCaml phrases from standard input, then typechecks, compiles and evaluates them, then prints the inferred type and result value, if any. End-of-file on standard input terminates ocaml(1). That's the whole loop, lowkey main character energy. ๐
Input to the toplevel can span several lines. It begins after the # (sharp) prompt printed by the system and is terminated by ;; (a double-semicolon) followed by optional white space and an end of line. The toplevel input consists of one or several toplevel phrases.
If one or more object-files (ending in .cmo or .cma) are given on the command line, they are loaded silently before starting the toplevel. No dramatic announcement, they just show up ready to go. ๐ซถ
If a script-file is given, phrases are read silently from the file, errors printed on standard error. ocaml(1) exits after the execution of the last phrase.
๐ OPTIONS ๐
The following command-line options are recognized by ocaml(1). Yes, all of them. Stay with me. ๐
-absnameโ Show absolute filenames in error messages. No more "where is that file tho" energy.-no-absnameโ Do not try to show absolute filenames in error messages.-i-varianceโ Cause the compiler to print the computed variance of type parameters for every type declaration. Without this option, variance is printed only for private types or types without a manifest (e.g., abstract types).-I directoryโ Add the given directory to the list of directories searched for source and compiled files. By default, the current directory is searched first, then the standard library directory. Directories added with-Iare searched after the current directory, in the order in which they were given on the command line, but before the standard library directory.
If the given directory starts with +, it is taken relative to the standard library directory. For instance, -I +compiler-libs adds the subdirectory compiler-libs of the standard library to the search path.
Directories can also be added to the search path once the toplevel is running with the #directory directive.
-init fileโ Load the given file instead of the default initialization file. See the "Initialization file" section below.-labelsโ Labels are not ignored in types, labels may be used in applications, and labelled parameters can be given in any order. This is the default.-no-app-functโ Deactivates the applicative behaviour of functors. With this option, each functor application generates new types in its result and applying the same functor twice to the same argument yields two incompatible structures. messy, but that's the deal. ๐ญ-noassertโ Do not compile assertion checks. Note that the special formassert falseis always compiled because it is typed specially.-noinitโ Do not load any initialization file. See the "Initialization file" section below.-nolabelsโ Ignore non-optional labels in types. Labels cannot be used in applications, and parameter order becomes strict.-nopromptโ Do not display any prompt when waiting for input.-nopromptcontโ Do not display the secondary prompt when waiting for continuation lines in multi-line inputs. This should be used e.g. when runningocaml(1)in anemacs(1)window.-nostdlibโ Do not include the standard library directory in the list of directories searched for source and compiled files.-open moduleโ Opens the given module before starting the toplevel. If several-openoptions are given, they are processed in order, just as if the statements
open! module1;; ... open! moduleN;;
were input.
-ppx commandโ After parsing, pipe the abstract syntax tree through the preprocessor command. The moduleAst_mapper(3)implements the external interface of a preprocessor.-principalโ Check information path during type-checking, to make sure that all types are derived in a principal way. When using labelled arguments and/or polymorphic methods, this flag is required to ensure future versions of the compiler will be able to infer types correctly, even if internal algorithms change. All programs accepted in-principalmode are also accepted in the default mode with equivalent types, but different binary signatures, and this may slow down type checking; yet it is a good idea to use it once before publishing source code. future-proofing slay. โจ-no-principalโ Do not check principality of type inference. This is the default.-rectypesโ Allow arbitrary recursive types during type-checking. By default, only recursive types where the recursion goes through an object type are supported.-no-rectypesโ Do not allow arbitrary recursive types during type-checking. This is the default.-safe-stringโ Enforce the separation between typesstringandbytes, thereby making strings read-only. This is the default.-safer-matchingโ Do not use type information to optimize pattern-matching. This allows to detect match failures even if a pattern-matching was wrongly assumed to be exhaustive. This only impacts GADT and polymorphic variant compilation.-short-pathsโ When a type is visible under several module-paths, use the shortest one when printing the type's name in inferred interfaces and error and warning messages.-stdinโ Read the standard input as a script file rather than starting an interactive session.-strict-sequenceโ Force the left-hand part of each sequence to have typeunit.-no-strict-sequenceโ Left-hand part of a sequence need not have typeunit. This is the default.-unboxed-typesโ When a type is unboxable (i.e. a record with a single argument or a concrete datatype with a single constructor of one argument) it will be unboxed unless annotated with[@@ocaml.boxed].-no-unboxed-typesโ When a type is unboxable it will be boxed unless annotated with[@@ocaml.unboxed]. This is the default.-unsafeโ Turn bound checking off on array and string accesses (thev.(i)ands.[i]constructs). Programs compiled with-unsafeare therefore slightly faster, but unsafe: anything can happen if the program accesses an array or string outside of its bounds.
bestie, "anything can happen" is not a cute vibe here. it's giving undefined behaviour. ๐
-unsafe-stringโ Identify the typesstringandbytes, thereby making strings writable. This is intended for compatibility with old source code and should not be used with new software.
fr fr, do not use this with new software. it is explicitly for old source code compatibility.
-versionโ Print version string and exit.-vnumโ Print short version number and exit.-no-versionโ Do not print the version banner at startup.-w warning-listโ Enable or disable warnings according to the argument warning-list. Seeocamlc(1)for the syntax of the warning-list argument.-warn-error warning-listโ Mark as fatal the warnings described by the argument warning-list. Note that a warning is not triggered (and does not trigger an error) if it is disabled by the-woption. Seeocamlc(1)for the syntax of the warning-list argument.-color modeโ Enable or disable colors in compiler messages (especially warnings and errors). The following modes are supported:autoโ use heuristics to enable colors only if the output supports them (an ANSI-compatible tty terminal);alwaysโ enable colors unconditionally;neverโ disable color output.
The environment variable OCAML_COLOR is considered if -color is not provided. Its values are auto/always/never as above.
If -color is not provided, OCAML_COLOR is not set and the environment variable NO_COLOR is set, then color output is disabled. Otherwise, the default setting is auto, and the current heuristic checks that the TERM environment variable exists and is not empty or "dumb", and that isatty(stderr) holds.
-error-style modeโ Control the way error messages and warnings are printed. The following modes are supported:shortโ only print the error and its location;contextualโ likeshort, but also display the source code snippet corresponding to the location of the error.
The default setting is contextual.
The environment variable OCAML_ERROR_STYLE is considered if -error-style is not provided. Its values are short/contextual as above.
-warn-helpโ Show the description of all available warning numbers.- fileโ Use file as a script file name, even when it starts with a hyphen (-).-helpor--helpโ Display a short usage summary and exit.
๐ INITIALIZATION FILE ๐
When ocaml(1) is invoked as a toplevel, it will read phrases from an initialization file before giving control to the user. This does not happen when running a script, or when using -e.
The file read is the first found of:
.ocamlinitin the current directory;XDG_CONFIG_HOME/ocaml/init.ml, ifXDG_CONFIG_HOMEis an absolute path;- otherwise, on Unix,
HOME/ocaml/init.mlor, on Windows,ocaml\init.mlunder LocalAppData (e.g.C:\Users\Bactrian\AppData\Local\ocaml\init.ml); ocaml/init.mlunder any of the absolute paths inXDG_CONFIG_DIRS. Paths inXDG_CONFIG_DIRSare colon-delimited on Unix, and semicolon-delimited on Windows;- if
XDG_CONFIG_DIRScontained no absolute paths,/usr/xdg/ocaml/init.mlon Unix or,ocaml\init.mlunder any of LocalAppData (e.g.C:\Users\Bactrian\AppData\Local), RoamingAppData (e.g.C:\Users\Bactrian\AppData\Roaming), or ProgramData (e.g.C:\ProgramData) on Windows; HOME/.ocamlinit, ifHOMEis non-empty;
You can specify a different initialization file by using the -init file option, and disable initialization files by using the -noinit option.
Note that you can also use the #use directive to read phrases from a file. cute. ๐
๐ธ ENVIRONMENT VARIABLES ๐ธ
okay so the toplevel is literally clocking your environment, and hereโs the tea โโจ
OCAMLTOP_UTF_8โ when printing string values, non-ascii bytes (>0x7E) are printed as a decimal escape sequence ifOCAMLTOP_UTF_8is set tofalse. Otherwise, theyโre printed unescaped. no cap ๐TERMโ when printing error messages, the toplevel system attempts to underline visually the location of the error. It consults theTERMvariable to determine the type of output terminal and look up its capabilities in the terminal database. itโs giving โknow your terminal, know yourselfโ ๐XDG_CONFIG_HOME,HOME,XDG_CONFIG_DIRSโ seeINITIALIZATION FILEabove, bestie ๐ซถ
๐ SEE ALSO ๐
ocamlc(1)โ sibling page, go read it ๐ocamlopt(1)โ sibling page, go read it ๐ocamlrun(1)โ sibling page, go read it ๐- The OCaml userโs manual, chapter โThe toplevel systemโ โ the lore, the documentation, the main character ๐๐
Thatโs the whole OCAML(1) page, bestie โ source/version and date are sitting pretty in the footer, and yes, they ate ๐
โจ๐
๐ฆ where this page comes from
- arch rolling: ocaml 5.5.0-1 ยท LGPL2.1 AND custom: QPL-1.0
- nixpkgs unstable: ocaml 5.5.1
- nixpkgs unstable: ocaml-solo5 1.3.5
- Copyright 1996 Institut National de Recherche en Informatique et
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms