caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "David Allsopp" <dra-news@metastack.com>
To: "'Dario Teixeira'" <darioteixeira@yahoo.com>, <caml-list@yquem.inria.fr>
Subject: RE: [Caml-list] Lazy modules
Date: Wed, 17 Mar 2010 17:42:55 -0000	[thread overview]
Message-ID: <00d801cac5f9$474281f0$d5c785d0$@romulus.metastack.com> (raw)
In-Reply-To: <571731.77500.qm@web111510.mail.gq1.yahoo.com>

Dario Teixeira wrote:
> I've come across a problem which though novel to me, I presume must be
> familiar to those with more ML baggage.  Namely, I need a module whose
> values are not known at the initialisation stage, since they can only be
> determined after reading a configuration file.  If this were a lone
> value, I would declare it as a lazy value which when forced would read
> from the configuration file.  But how can I achieve similar semantics
> with a whole module?

I've hit this similarly with databases - if for example you have a signature DB and modules, say, OracleDB, MSSQLDB, PostgresDB. What I'd wanted in the past is a module DB which is one of those three but determined at runtime when a configuration file is loaded. I couldn't find a satisfactory solution with OCaml 3.09 at the time which didn't involve recompiling on each change.

> I do have a solution which though hackish and relying on a language
> extension (local modules) seems to work: I create the module on demand
> via a functor parameterised over an empty sig:

AFAIK local modules is a syntax extension not a compiler extension - I expect (not looked at it) that the syntax extension simply alpha renames all the local module declarations to make them unique and puts them globally... a very useful extension but no expressive power added. 

> module Socket_maker (S: sig end) : Client.SOCKET = struct
>     let sockaddr = !Config.sockaddr
>     let sockdomain = !Config.sockdomain
>     let socktype = !Config.socktype
>     let sockproto = !Config.sockproto
> end
> 
> let foo =
>     let module Socket = Socket_maker (struct end) in
>     ...
> 
> But I wonder a) what's the penalty associated with the runtime
> application of the functor, and

The module system at present is a compile time feature (I think that's universally true - even with weird things like recursive modules) - functors are simply a way of introducing more modules so there is no runtime overhead in using a functor. There are I believe some performance overheads in using modules if you care about speed as some optimisations in ocamlopt can't happen across module boundaries. My impression has been that if you're that worried about these slight performance hits then maybe OCaml is the wrong language for you :o)

> b) if there's some better way of doing this.  Any thoughts?

I believe that the module system due for OCaml 3.12.0 will allow this kind of runtime application of functors as modules are first class values.

Hope that's helpful - the inability to do what you're wanting to do is one of the reasons that I've never delved deeply into the module system - powerful as it may be, it didn't seem to be able to help performing a simple task (similar to yours) that I wanted it to do (I have also in the past wanted to exactly what you're doing - i.e. a module of loaded configuration values).


David


  parent reply	other threads:[~2010-03-17 17:43 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-17 17:04 Dario Teixeira
2010-03-17 17:36 ` [Caml-list] " Alain Frisch
2010-03-17 18:18   ` Dario Teixeira
2010-03-17 17:42 ` David Allsopp [this message]
2010-03-17 18:23   ` Alain Frisch
2010-03-18 11:10     ` David Allsopp
2010-03-22 18:08     ` Yoann Padioleau
2010-03-17 18:37   ` Dario Teixeira
2010-03-18 11:10     ` David Allsopp
2010-03-18 14:22       ` Goswin von Brederlow

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='00d801cac5f9$474281f0$d5c785d0$@romulus.metastack.com' \
    --to=dra-news@metastack.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=darioteixeira@yahoo.com \
    /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).