16 Compilation Units

So far, we've been using .ml files for signatures; however, there's another way. You can extract signatures to .mli files.

The .mli file is the interface file, that holds the signature, and the .ml file is the implementation file, that holds the implementation.

Traditionally, comments for the end-user go in the .mli file, and comments for maintainers go into the .ml file. This is known as a compilation unit.

A compilation unit is the combination of a .ml and .mli file with the same base name.

If myfile.ml has contents DM, and myfile.mli has contents DS, then the OCaml compiler thinks of the compilation unit as:

module Myfile [: sig DS end] = struct
    DM
end

Note that this is an anonymous signature, there's no name for it!

We'll start using compilation units later.

By the way, if you ever want to look at the source for something in the standard library, you can browse the source on GitHub, specifically in the stdlib directory. Note that each has an .mli file and an .ml file, for example, list.ml and list.mli.