Re: type declaration in */mli & *.ml

Xavier Leroy (Xavier.Leroy@inria.fr)
Wed, 23 Oct 1996 11:29:26 +0200 (MET DST)

From: Xavier Leroy <Xavier.Leroy@inria.fr>
Message-Id: <199610230929.LAA23661@pauillac.inria.fr>
Subject: Re: type declaration in */mli & *.ml
To: Basile.Starynkevitch@cea.fr (Basile STARYNKEVITCH)
Date: Wed, 23 Oct 1996 11:29:26 +0200 (MET DST)
In-Reply-To: <199610210923.LAA01989@soleil.serma.cea.fr> from "Basile STARYNKEVITCH" at Oct 21, 96 11:23:25 am

> It seems that when a type is declared in the interface of a module Moo
> -ie in the file moo.mli- it should also be declared in the
> implementation of the same module Moo -ie in the file moo.ml-.
> I don't understand why is it so, and I feel such a redundancy is very
> annoying.

It's a pain, all right, but the explanation is very simple in terms of
module interfaces (specifications) and module implementations.
When a .mli file, or equivalently a sig ... end module type, contains

type t = A of ... | B of ...

that's just a specification saying that any implementation of this
interface (that is, the .ml file or any structure that is matched
against the interface) must define a type t with the same
constructors and arguments. The type t could also be specified
abstract, as in:

type t

in which case the implementation can define t as any constant type.

In both cases, the implementation must provide a definition for t.
Of course, if t is specified manifest, this does not leave much
flexibility for the definition of t in the implementation, which is
usually identical to the specification in the interface.

When using the full module calculus, an implementation (struct...end)
can have several interfaces (sig...end), and an interface can have
several implementations, so it's not possible in general to have automatic
definitions of types in the implementation based on the manifest
declarations in the interface.

> (perhaps I could paraphrase my question: does an implementation file
> moo.ml of a module Moo contain an implicit open Moo;; ??)

The answer is no, and this would not make sense, since "open" can only
apply to an implementation, not an interface (again, there might be
several implementations of a given interface), so you would be opening
the implementation you're currently defining.

- Xavier Leroy