caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Martin Jambon <martin_jambon@emailuser.net>
To: ocaml_beginners@yahoogroups.com
Cc: caml-list@yquem.inria.fr
Subject: Re: "ocaml_beginners"::[] Rephrasing of dynamic module selection problem
Date: Mon, 20 Feb 2006 19:49:08 -0800 (PST)	[thread overview]
Message-ID: <Pine.LNX.4.63.0602201922330.9335@droopy> (raw)
In-Reply-To: <43FA64C8.1050704@cs.utah.edu>

On Mon, 20 Feb 2006, Nathan Cooprider wrote:

> So I am still trying to get modules to be dynamically (run-time)
> selectable instead of only statically (compile-time). The closest I have
> come to so far is bellow. I want to be able to choose between a set of
> modules (hello1 and hello2 in this example) fairly transparently.
>
> [coop@ender example]$ cat hello1.ml
> type t = int
> let of_int i =
>  i
> let print i =
>  print_int i;
>  print_string " says Hello1\n"
>
> [coop@ender example]$ cat hello2.ml
> type t = float
> let of_int i =
>  float_of_int i
> let print i =
>  print_float i;
>  print_string " says Hello2\n"
>
> [coop@ender example]$ cat main.ml
> module Hello1 = struct
>  #include "hello1.ml"
> end ;;
> module Hello2 = struct
>  #include "hello2.ml"
> end ;;
> (* This works . . . *)
> module H = Hello1
> (* But I would like this to be something like this instead:
> let parameter = 1
> module H =
>  match parameter with
>    1 -> Hello1
>  | _ -> Hello2
> *)

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 -> t
   val print : t -> unit
   val obj : < of_int : int -> t; print : t -> unit >
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 -> Hello1.obj
     | 2 -> Hello2.obj
     | _ -> assert false;;

         Characters 63-73:
       | 2 -> Hello2.obj
              ^^^^^^^^^^
This expression has type
   < of_int : int -> Hello2.t; print : Hello2.t -> unit >
but is here used with type
   < of_int : int -> Hello1.t; print : Hello1.t -> unit >
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


  reply	other threads:[~2006-02-21  3:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-02-21  0:54 Nathan Cooprider
2006-02-21  3:49 ` Martin Jambon [this message]
2006-02-21  3:55 ` [Caml-list] " brogoff
2006-02-21  9:07   ` Andreas Rossberg
2006-02-21  9:55 ` Virgile Prevosto

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=Pine.LNX.4.63.0602201922330.9335@droopy \
    --to=martin_jambon@emailuser.net \
    --cc=caml-list@yquem.inria.fr \
    --cc=ocaml_beginners@yahoogroups.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).