Ocaml tries to force you to separate the interface (.mli
) from the implementation (.ml
. Most of the time, this is a good thing; for values, you publish the type in the interface, and keep the code in the implementation. You could say that Ocaml is enforcing a certain amount of abstraction (interfaces must be published; no code in interfaces).
For types, very often, the implementation is the same as the interface: both state that the type has a particular representation (and perhaps that the type declaration is generative). Here, there can be no abstraction, because the implementer doesn't have any information about the type that he doesn't want to publish. (The exception is basically when you declare an abstract type.)
One way to look at it is that the interface already contains enough information to write the implementation. Given the interface type foobar = Bool of bool | Float of float | Int of int
, there is only one possible implementation. So don't write an implementation!
A common idiom is to have a module that is dedicated to type declarations, and make it have only a .mli
. Since types don't depend on values, this module typically comes in very early in the dependency chain. Most compilation tools cope well with this; for example ocamldep
will do the right thing. (This is one advantage over having only a .ml
.)
The limitation of this approach is when you also need a few module definitions here and there. (A typical example is defining a type foo
, then an OrderedFoo : Map.OrderedType
module with type t = foo
, then a further type declaration involving'a Map.Make(OrderedFoo).t
.) These can't be put in interface files. Sometimes it's acceptable to break down your definitions into several chunks, first a bunch of types (types1.mli
), then a module (mod1.mli
and mod1.ml
), then more types (types2.mli
). Other times (for example if the definitions are recursive) you have to live with either a .ml
without a .mli
or duplication.