English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Invoking the standard library ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-04-30 (00:06)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Invoking the standard library ?
From: Eric Cooper <ecc@cmu.edu>
Subject: Re: [Caml-list] Invoking the standard library ?
Date: Tue, 29 Apr 2008 14:19:58 -0400

> On Tue, Apr 29, 2008 at 04:26:45PM +0200, David Teller wrote:
> > modules String, Stream, etc. For this, I need to include the original
> > module, as provided in the standard library, and add stuff. Now, the
> > trick is that I'd like to keep the same name as the original module.
> My first thought was that the usual shadowing of bindings could
> be used, and indeed the following works fine in the toplevel:
> module String =
>   struct
>     include String
>     let my_extension = ...
>   end
> But when I tried to compile it separately (just
>     include String
>     let my_extension = ...
> in a file "string.ml"), I got an error "Unbound module String".
> I don't understand why the behavior is different in these two cases;
> apparently the batch compiler binds String as soon as it starts compiling
> "string.ml", whereas the top-level does so only after compiling the
> struct body.  Is this a bug?

This is a limitation of ocaml's mapping between file names and
modules: a program cannot contain two files with the same name.
Even if you somehow succeed in doing so by tricking the compiler,
you're on your way for lots of trouble.

So the answer is that the new string module should be in a file with a
different name. This is actually what is done in the distribution for
labelled versions of standard library modules: they are first defined
with different names (ListLabels, ...), and then wrapper modules are
defined containing them with standard names (StdLabels). Of course
once you've done "open StdLabels" there is no way to access the
original List module (except if you've first defined an alias for it
with a different name).

It has been discussed at times that putting the standard library in a
packed module would alleviate this problem. However, this would make
it monolithic, meaning that all programs would have to include all the
standard library.


Jacques Garrigue