Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: type declaration in */mli & *.ml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: type declaration in */mli & *.ml

> 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