Chapter 11 The OCaml language

12 Compilation units

unit-interface::= { specification [;;] }
unit-implementation::= [ module-items ]

Compilation units bridge the module system and the separate compilation system. A compilation unit is composed of two parts: an interface and an implementation. The interface contains a sequence of specifications, just as the inside of a sigend signature expression. The implementation contains a sequence of definitions and expressions, just as the inside of a structend module expression. A compilation unit also has a name unit-name, derived from the names of the files containing the interface and the implementation (see chapter ‍13 for more details). A compilation unit behaves roughly as the module definition

module unit-name : sig unit-interface end = struct unit-implementation end

A compilation unit can refer to other compilation units by their names, as if they were regular modules. For instance, if U is a compilation unit that defines a type t, other compilation units can refer to that type under the name U.t; they can also refer to U as a whole structure. Except for names of other compilation units, a unit interface or unit implementation must not have any other free variables. In other terms, the type-checking and compilation of an interface or implementation proceeds in the initial environment

name1 : sig specification1 endnamen : sig specificationn end

where name1namen are the names of the other compilation units available in the search path (see chapter ‍13 for more details) and specification1specificationn are their respective interfaces.