<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2006/02/4a8e69b079aae473911bfc0b65e93fab"
  from="Virgile Prevosto &lt;virgile.prevosto@m...&gt;"
  author="Virgile Prevosto"
  date="2006-02-21T12:14:10"
  subject="Re: [Caml-list] Rephrasing of dynamic module selection problem"
  prev="2006/02/368bb3c5a0430f6328ee3b5f7ee8bd03"
  next="2006/02/1c42b4ff5827e2d40a11d13c9acec68c"
  prev-in-thread="2006/02/368bb3c5a0430f6328ee3b5f7ee8bd03"
  prev-thread="2006/02/c00cd9129fc81180e4199583825836cf"
  next-thread="2006/02/e6065b7bd89d7f0ba4c9a94c8db316b9"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="Rephrasing of dynamic module selection problem">
<msg 
  url="2006/02/6f98d2c26e5095e71bb6eaa96087bdde"
  from="Nathan Cooprider &lt;coop@c...&gt;"
  author="Nathan Cooprider"
  date="2006-02-21T00:54:44"
  subject="Rephrasing of dynamic module selection problem">
<msg 
  url="2006/02/166e73eb9483d32d01c6586a068b6d68"
  from="Martin Jambon &lt;martin_jambon@e...&gt;"
  author="Martin Jambon"
  date="2006-02-21T03:50:53"
  subject="Re: &quot;ocaml_beginners&quot;::[] Rephrasing of dynamic module selection problem">
</msg>
<msg 
  url="2006/02/0010bbc7928d8dab73de090fe6fab8fc"
  from="brogoff &lt;brogoff@s...&gt;"
  author="brogoff"
  date="2006-02-21T03:55:53"
  subject="Re: [Caml-list] Rephrasing of dynamic module selection problem">
<msg 
  url="2006/02/368bb3c5a0430f6328ee3b5f7ee8bd03"
  from="Andreas Rossberg &lt;AndreasRossberg@w...&gt;"
  author="Andreas Rossberg"
  date="2006-02-21T12:13:55"
  subject="Re: [Caml-list] Rephrasing of dynamic module selection problem">
</msg>
</msg>
<msg 
  url="2006/02/4a8e69b079aae473911bfc0b65e93fab"
  from="Virgile Prevosto &lt;virgile.prevosto@m...&gt;"
  author="Virgile Prevosto"
  date="2006-02-21T12:14:10"
  subject="Re: [Caml-list] Rephrasing of dynamic module selection problem">
</msg>
</msg>
</thread>

<contents>
Hello,

Le lun 20 fév 2006 17:54:32 CET, Nathan Cooprider a écrit:
&gt; [coop@ender example]$ cat main.ml
&gt; module Hello1 = struct
&gt;   #include "hello1.ml"
&gt; end ;;
&gt; module Hello2 = struct
&gt;   #include "hello2.ml"
&gt; end ;;

This is not needed: the file hello1.ml implicitely defines a module Hello1
(same for hello2.ml and Hello2).

&gt; (* This works . . . *)
&gt; module H = Hello1
&gt; (* But I would like this to be something like this instead:
&gt; let parameter = 1
&gt; module H =
&gt;   match parameter with
&gt;     1 -&gt; Hello1
&gt;   | _ -&gt; Hello2
&gt;  *)

This might not completely solve your problem, but the following camlp4
extension allows you to use the following expression:
choose_module H = 
match parameter with
 1 -&gt; Hello1
| _ -&gt; Hello2
in 
let argument = 42 in 
let main () = H.print (H.of_int argument) in
main ();;

Note that the scope of H is the expression following the first "in" and
not all the remaining of main.ml. This might be an issue, but it could
be solved by the use of functors (e.g.:
module type Hello = sig ... end
module F (H: Hello) = struct ... end
...
choose_module H = ... in
let module FH = F(H) in ... ;;
)

-------- choose_module.ml4 ----------
(* compile it with
ocamlc -c -I +camlp4 -pp "camlp4o pa_extend.cmo q_MLast.cmo -impl" -impl
choose_modules.ml4

to use it,
ocamlc -c -pp "camlp4o choose_modules.cmo" main.ml 
Note that by default, camlp4 doesn't search for extension in the current
directory, so that you may have to add a "-I ." directive
*) 
open Pcaml;;

let make_one_choice a mod_expr exp =
  let _loc = Lexing.dummy_pos, Lexing.dummy_pos in 
    &lt;:expr&lt;let module $uid:a$ = $mod_expr$ in $exp$&gt;&gt; 

let choices = Grammar.Entry.create Pcaml.gram "choices" 

EXTEND
   expr: [[
            "choose_module"; a = UIDENT; "="; "match" ; cond = expr;
            "with"; OPT "|"; l = LIST1 choices SEP "|";  
            "in"; e = expr -&gt; 
              let new_l = List.map 
                            (fun (patt,optwhen,expr) -&gt; 
                               (patt,optwhen, make_one_choice a expr e)) l
              in  &lt;:expr&lt; match $cond$ with [$list:new_l$] &gt;&gt; ]];
  choices: [[ p=patt; w = OPT ["when"; e = expr -&gt; e]; "-&gt;"; 
              m = module_expr -&gt; (p,w,m)
            ]];
END
-------------------------------------
-- 
E tutto per oggi, a la prossima volta
Virgile

</contents>

</message>

