A primary motivation for modules is to package together related
definitions (such as the definitions of a data type and associated
operations over that type) and enforce a consistent naming scheme for
these definitions. This avoids running out of names or accidentally
confusing names. Such a package is called a structure and
is introduced by the struct…end construct, which contains an
arbitrary sequence of definitions. The structure is usually given a
name with the module binding. Here is for instance a structure
packaging together a type of priority queues and their operations:

Outside the structure, its components can be referred to using the
“dot notation”, that is, identifiers qualified by a structure name.
For instance, PrioQueue.insert in a value context is
the function insert defined inside the structure
PrioQueue. Similarly, PrioQueue.queue in a type context is the
type queue defined in PrioQueue.

Signatures are interfaces for structures. A signature specifies
which components of a structure are accessible from the outside, and
with which type. It can be used to hide some components of a structure
(e.g. local function definitions) or export some components with a
restricted type. For instance, the signature below specifies the three
priority queue operations empty, insert and extract, but not the
auxiliary function remove_top. Similarly, it makes the queue type
abstract (by not providing its actual representation as a concrete type).

Restricting the PrioQueue structure by this signature results in
another view of the PrioQueue structure where the remove_top
function is not accessible and the actual representation of priority
queues is hidden:

Functors are “functions” from structures to structures. They are used to
express parameterized structures: a structure A parameterized by a
structure B is simply a functor F with a formal parameter
B (along with the expected signature for B) which returns
the actual structure A itself. The functor F can then be
applied to one or several implementations B1 …Bn
of B, yielding the corresponding structures
A1 …An.

For instance, here is a structure implementing sets as sorted lists,
parameterized by a structure providing the type of the set elements
and an ordering function over this type (used to keep the sets
sorted):

As in the PrioQueue example, it would be good style to hide the
actual implementation of the type set, so that users of the
structure will not rely on sets being lists, and we can switch later
to another, more efficient representation of sets without breaking
their code. This can be achieved by restricting Set by a suitable
functor signature:

The problem here is that SET specifies the type element
abstractly, so that the type equality between element in the result
of the functor and t in its argument is forgotten. Consequently,
WrongStringSet.element is not the same type as string, and the
operations of WrongStringSet cannot be applied to strings.
As demonstrated above, it is important that the type element in the
signature SET be declared equal to Elt.t; unfortunately, this is
impossible above since SET is defined in a context where Elt does
not exist. To overcome this difficulty, Objective Caml provides a
with type construct over signatures that allows to enrich a signature
with extra type equalities:

Abstracting a type component in a functor result is a powerful
technique that provides a high degree of type safety, as we now
illustrate. Consider an ordering over character strings that is
different from the standard ordering implemented in the
OrderedString structure. For instance, we compare strings without
distinguishing upper and lower case.

Notice that the two types AbstractStringSet.set and
NoCaseStringSet.set are not compatible, and values of these
two types do not match. This is the correct behavior: even though both
set types contain elements of the same type (strings), both are built
upon different orderings of that type, and different invariants need
to be maintained by the operations (being strictly increasing for the
standard ordering and for the case-insensitive ordering). Applying
operations from AbstractStringSet to values of type
NoCaseStringSet.set could give incorrect results, or build
lists that violate the invariants of NoCaseStringSet.

All examples of modules so far have been given in the context of the
interactive system. However, modules are most useful for large,
batch-compiled programs. For these programs, it is a practical
necessity to split the source into several files, called compilation
units, that can be compiled separately, thus minimizing recompilation
after changes.

In Objective Caml, compilation units are special cases of structures
and signatures, and the relationship between the units can be
explained easily in terms of the module system. A compilation unit A
comprises two files:

the implementation file A.ml, which contains a sequence
of definitions, analogous to the inside of a struct…end
construct;

the interface file A.mli, which contains a sequence of
specifications, analogous to the inside of a sig…end
construct.

Both files define a structure named A as if
the following definition was entered at top-level:

The files defining the compilation units can be compiled separately
using the ocamlc -c command (the -c option means “compile only, do
not try to link”); this produces compiled interface files (with
extension .cmi) and compiled object code files (with extension
.cmo). When all units have been compiled, their .cmo files are
linked together using the ocaml command. For instance, the following
commands compile and link a program composed of two compilation units
Aux and Main:

In particular, Main can refer to Aux: the definitions and
declarations contained in Main.ml and Main.mli can refer to
definition in Aux.ml, using the Aux.ident notation, provided
these definitions are exported in Aux.mli.

The order in which the .cmo files are given to ocaml during the
linking phase determines the order in which the module definitions
occur. Hence, in the example above, Aux appears first and Main can
refer to it, but Aux cannot refer to Main.

Notice that only top-level structures can be mapped to
separately-compiled files, but not functors nor module types.
However, all module-class objects can appear as components of a
structure, so the solution is to put the functor or module type
inside a structure, which can then be mapped to a file.