caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ivan Gotovchits <ivg@ieee.org>
To: "Daniel Bünzli" <daniel.buenzli@erratique.ch>
Cc: Alexey Egorov <electreg@list.ru>, caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Installing library with hidden modules
Date: Wed, 10 May 2017 08:17:19 -0400	[thread overview]
Message-ID: <CALdWJ+xumBOYUuLh+992OeZPfPdVE-TPO_nryCqrFvD4X=VAGw@mail.gmail.com> (raw)
In-Reply-To: <53B38F9A1D3D4732A24E8EF40B907B3F@erratique.ch>

[-- Attachment #1: Type: text/plain, Size: 3920 bytes --]

Hi Daniel,

You're absolutely right in all details. And yes the devil is in details. I
should be more precise and disclose all the ingredients of my recipe.
Indeed, we are not using module aliases, and yes hiding the cmi file, of
course, will not fix the flat namespace of compilation units. So, the
solution that we are using for our libraries in CMU is pretty simple, for a
library/project named LIB do the following:

1. prefix all internal modules with the `LIB_`
2. provide a public module `LIB.ml` that reexports some of the internals
3. describe the public interface in `LIB.mli`
4. do not install `cmi` and `mli` files for the internal modules.

The public interface, of course, should not mention any internals.

Given a concrete example, suppose we want to implement a Monad library and
name it `monad`. The project structure would be the following:

```
monad_types.ml
monad_common.ml
monad_state.ml
monad_state.mli
monad_reader.ml
monad_reader.mli
...
monad.ml
monad.mli
```

A possible contents of the `monad.ml`:

```
include Monad_types
module State = Monad_state
module Reader = Monad_reader
...
```

The contents of the `monad.mli`

```
(** Lot's of docs *)
module type S = sig
   type 'a t
   val map : 'a t -> ('a -> 'b) -> 'b t
   val bind : 'a t -> ('a -> 'b t) -> 'b t
   val return : 'a -> 'a t
   ...
end

...

module State : sig
    type ('a,'e) t
    include S2 with type ('a,'e) t := ('a,'e) t
    ...
    val run : ('a,'e) t -> 'e -> 'a * 'e
end

...
```



The most controversial part is the duplication of the `mli` files. Of
course, one can omit `mli` for the internals, but I myself is a strong
proponent of writing mli files, and will not go that way. Thus, we decided
to support the two sets of mli files. The internal set, describes our
internal developer's interface, that is sometimes richer and less
stable than the external. The external defines the public API, that is
stable and versioned, it also must be well-documented. The internal
interfaces usually do not contain documents, except technical notes and
implementation details.


For a real life example, look at the graphlib library:

library structure:
https://github.com/BinaryAnalysisPlatform/bap/tree/master/lib/graphlib
oasis file:
https://github.com/BinaryAnalysisPlatform/bap/blob/master/oasis/graphlib


P.S. This is not the only solution. Another, probably better, solution,
that I was using before, is to use packing. It is much nicer in the sense,
that it solves the flat namespace issue. However, OASIS messes with the
packed modules, so we were forced to use this, more ad-hoc approach.

P.P.S. Strictly speaking we do not need to install even the `cmx` of the
internal modules, however, their absence will prevent cross-module
optimization if my understanding of the latter is correct.

Best wishes,
Ivan


On Tue, May 9, 2017 at 5:01 PM, Daniel Bünzli <daniel.buenzli@erratique.ch>
wrote:

> On Monday, 8 May 2017 at 14:49, Ivan Gotovchits wrote:
> > An absence of the cmi file, will prevent users from accessing the module
> directly. The OASIS system provides an easy way to hide modules, with the
> `InternalModules` field. OASIS will install all the necessary parts of the
> module (i.e., cmxs, cma, o, a, etc), but will not install the interface
> part.
>
>
> If I'm not mistaken you can't do that with module aliases, you'll need the
> cmi files of the right hand side of the alias (`module M : sig end = M` is
> invalid syntax).
>
> Besides it's not because you hide the cmi files that the names won't show
> up at link time. If your "hidden" toplevel names are generic (let's say
> `Config`), they will still prevent linking with other libraries that define
> the same generic names.
>
> Again, you can't hide toplevel names using module aliases.
>
> Best,
>
> Daniel
>
>
>

[-- Attachment #2: Type: text/html, Size: 5600 bytes --]

  reply	other threads:[~2017-05-10 12:17 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-07  5:24 Alexey Egorov
2017-05-08 10:56 ` Daniel Bünzli
2017-05-08 12:49   ` Ivan Gotovchits
2017-05-09 21:01     ` Daniel Bünzli
2017-05-10 12:17       ` Ivan Gotovchits [this message]
2017-05-10 12:43         ` Daniel Bünzli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CALdWJ+xumBOYUuLh+992OeZPfPdVE-TPO_nryCqrFvD4X=VAGw@mail.gmail.com' \
    --to=ivg@ieee.org \
    --cc=caml-list@inria.fr \
    --cc=daniel.buenzli@erratique.ch \
    --cc=electreg@list.ru \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).