caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Shivkumar Chandrasekaran <shiv@ece.ucsb.edu>
To: caml-list@pauillac.inria.fr
Subject: Re: [Caml-list] Conditional Modules
Date: Wed, 04 Aug 2004 10:22:11 -0700	[thread overview]
Message-ID: <41111B43.8040806@ece.ucsb.edu> (raw)
In-Reply-To: <d849ad2a040804094559770a34@mail.gmail.com>

I find the following paradigm useful sometimes:

module Params = struct
    let param1 = ... (* from command line args *)
    let param2 = ...
end

module M1 = F1 (Params, ...) (* specialize modules to command line args *)
module M2 = F2 (Params, ...)
module M3 ...

(* call correct main routine *)
if predicate1 args then
    M1.main ()
else if predicate2 args then
    M2.main ()
else if ...
    ...

In other words, fold the whole main function into the functors. Then 
only the main routine in each module must return same type and the 
individual modules themselves are free of each other.

Of course you can do this for functions other than main too.

--shiv--


John Prevost wrote:

>If you're choosing between options at runtime, you might consider
>thinking about a plugin architecture instead.  Modules are pretty much
>all set at link time.
>
>Here's a simple example of the strategy:
>
>module M = struct
>  exception No_plugin
>  type entry = { reg_f : int -> int }
>  let current_entry : entry option = ref None
>  let register e = current_entry := Some e
>  let get_entry () = match !current_entry with
>    | None -> raise No_plugin
>    | Some e -> e
>  let f x = (get_entry ()).f x
>end
>
>module M1 = struct
>  let f x = x + 1
>  let m1_entry = { M.reg_f = f }
>  let _ = M.register m1_entry  
>end
>
>This model puts more constraints on types than using modules would,
>but does have a variety of different ways to be useful.  For example,
>M1 could instead of registering automatically wait until told, or M
>could keep a list of registered plugins and take the first one that
>"works" for an input, etc.
>
>And finally, you may also be able to do things differently using
>functors--one of which you seem to be using in your example.  Here's
>an example of my idea here:
>
>Original:
>
>module M = if arg then M1 else M2
>
>type t = M.t
>
>let f = M.f
>let g = M.g
>...
>
>can instead be written:
>
>module X (M : M_T) : R_T with type t = (* something *) = struct
>  (* definitions involving M *)
>end
>
>module X1 = X(M1)
>module X2 = X(M2)
>
>type t = X1.t
>
>let f = if arg then X1.f else X2.f
>let g = if arg then X1.g else X2.g
>
>This approach is a little verbose, but should work reasonably well. 
>Again, your constraint here is that X1 and X2 will have to have
>identical types, not just similar types.  (And that's the real reason
>that you can't do conditionals to choose one of several modules: you
>can't do conditionals at runtime on types!)
>
>John.
>
>-------------------
>To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
>Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
>Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2004-08-04 17:22 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-04 14:58 Ross Duncan
2004-08-04 16:02 ` Richard Jones
2004-08-04 16:29   ` Michel Mauny
2004-08-04 16:45   ` John Prevost
2004-08-04 17:22     ` Shivkumar Chandrasekaran [this message]
2004-08-05 11:23     ` Ross Duncan
2004-08-05 12:34       ` Alain Frisch
2004-08-05 15:10       ` Jean-Baptiste Rouquier
2004-08-05 15:54         ` Christophe Raffalli
2004-08-06  7:53           ` Alain Frisch
2004-08-04 17:23 ` henri dubois-ferriere
2004-08-04 17:24 ` james woodyatt
2004-08-04 18:01   ` John Prevost

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=41111B43.8040806@ece.ucsb.edu \
    --to=shiv@ece.ucsb.edu \
    --cc=caml-list@pauillac.inria.fr \
    /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).