From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA19063; Wed, 4 Aug 2004 19:22:19 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id TAA19081 for ; Wed, 4 Aug 2004 19:22:18 +0200 (MET DST) Received: from mail.ece.ucsb.edu (mail.ece.ucsb.edu [128.111.56.51]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i74HMGmL005942 for ; Wed, 4 Aug 2004 19:22:17 +0200 Received: from [192.168.0.204] (ip68-6-103-180.sb.sd.cox.net [68.6.103.180]) (authenticated bits=0) by mail.ece.ucsb.edu (8.13.1/8.13.1) with ESMTP id i74HMCXu006967 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 4 Aug 2004 10:22:12 -0700 (PDT) Message-ID: <41111B43.8040806@ece.ucsb.edu> Date: Wed, 04 Aug 2004 10:22:11 -0700 From: Shivkumar Chandrasekaran User-Agent: Mozilla Thunderbird 0.7.1 (X11/20040729) X-Accept-Language: en-us, en MIME-Version: 1.0 To: caml-list@pauillac.inria.fr Subject: Re: [Caml-list] Conditional Modules References: <20040804160247.GA13965@annexia.org> In-Reply-To: X-Enigmail-Version: 0.84.2.0 X-Enigmail-Supports: pgp-inline, pgp-mime Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 41111B48.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; shivkumar:01 shiv:01 caml-list:01 params:01 struct:01 params:01 predicate:01 predicate:01 functors:01 --shiv--:01 prevost:01 runtime:01 struct:01 model:01 verbose:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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