<?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/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"
  prev="2006/02/914157bc1b72bff5223801de1345011b"
  next="2006/02/0010bbc7928d8dab73de090fe6fab8fc"
  prev-in-thread="2006/02/6f98d2c26e5095e71bb6eaa96087bdde"
  next-in-thread="2006/02/0010bbc7928d8dab73de090fe6fab8fc"
  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>
On Mon, 20 Feb 2006, Nathan Cooprider wrote:

&gt; So I am still trying to get modules to be dynamically (run-time)
&gt; selectable instead of only statically (compile-time). The closest I have
&gt; come to so far is bellow. I want to be able to choose between a set of
&gt; modules (hello1 and hello2 in this example) fairly transparently.
&gt;
&gt; [coop@ender example]$ cat hello1.ml
&gt; type t = int
&gt; let of_int i =
&gt;  i
&gt; let print i =
&gt;  print_int i;
&gt;  print_string " says Hello1\n"
&gt;
&gt; [coop@ender example]$ cat hello2.ml
&gt; type t = float
&gt; let of_int i =
&gt;  float_of_int i
&gt; let print i =
&gt;  print_float i;
&gt;  print_string " says Hello2\n"
&gt;
&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 ;;
&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; *)

That's not possible because Hello1 and Hello2 don't have the same "type" 
(same module signature though): what I mean is that if you represent them 
as objects, the objects would have different types since Hello1.t and 
Hello2.t are incompatible.

Try the following code (which fails when I try to mix the objects):

module type HELLO =
sig
   type t
   val of_int : int -&gt; t
   val print : t -&gt; unit
   val obj : &lt; of_int : int -&gt; t; print : t -&gt; unit &gt;
end

module Hello1 : HELLO =
struct
   type t = int
   let of_int i =
     i
   let print i =
     print_int i;
     print_string " says Hello1\n"

   let obj =
   object
     method of_int = of_int
     method print = print
   end
end

module Hello2 : HELLO =
struct
   type t = float
   let of_int i =
     float_of_int i
   let print i =
     print_float i;
     print_string " says Hello2\n"

   let obj =
   object
     method of_int = of_int
     method print = print
   end
end

let param = 2 (* defined at runtime *)
let obj =
   match param with
       1 -&gt; Hello1.obj
     | 2 -&gt; Hello2.obj
     | _ -&gt; assert false;;

         Characters 63-73:
       | 2 -&gt; Hello2.obj
              ^^^^^^^^^^
This expression has type
   &lt; of_int : int -&gt; Hello2.t; print : Hello2.t -&gt; unit &gt;
but is here used with type
   &lt; of_int : int -&gt; Hello1.t; print : Hello1.t -&gt; unit &gt;
Types for method of_int are incompatible


So if you want to do that, you must give the same type to your objects.
That would work if your modules Hello* all use a common type t instead of 
their own. In this case you just have to include the same code at the end 
of each Hello* module so that obj is defined. You can also add a line 
which places obj in a global table, e.g.
   let obj = ...
   let _ = Hashtbl.add Hello_modules.tbl 1 obj

And your main program would be:

   let param = ...
   let obj = Hashtbl.find Hello_modules.tbl param in
   ...


Martin

--
Martin Jambon, PhD
http://martin.jambon.free.fr

Visit http://wikiomics.org, the Bioinformatics Howto Wiki

</contents>

</message>

