caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] load modules by name
@ 2003-01-21  7:22 Pietro Abate
  2003-01-21 10:48 ` Stefano Zacchiroli
  2003-01-22 16:41 ` [Caml-list] " Michaël Grünewald
  0 siblings, 2 replies; 3+ messages in thread
From: Pietro Abate @ 2003-01-21  7:22 UTC (permalink / raw)
  To: caml-list

hi list,

I'm trying to figure out how I can build a kind of associative list
to load a specific module at runtime. I've reread few old messages about
first-class modules and other oddities, but I've the sensation my
problem is easier.

I have a library (compiled), an application (compiled) and a bunch of
user defined modules (that are compiled as well, but these may be vary
in number). At the moment the application select the rigth module
statically regarding a command line argument. However this way I must
recompile my application everytime I add a new module.

What I'd like to do is reading all .cmo files (or .ml files and compile
them on the fly) in a directory and build a kind of assoc list with 
(name,module) and at runtime select the right module.

let's say that now I have

let algo = 
match command_line_option with
| "mod1" -> Module1.algo
| "mod2" -> Module2.algo
...
| _ -> failwith "module not defined"
in ...

but I'd rather have something like

let algo =
	try
		List.assoc "command_line_option" assoclist
	with Not_found -> failwith "Module not defined"
in ...

That would not require any recompilation of my application.

It would also nice to compile on the fly these modules...
I'm thinking of something like eval in perl...

is it possible ?

p

-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] load modules by name
  2003-01-21  7:22 [Caml-list] load modules by name Pietro Abate
@ 2003-01-21 10:48 ` Stefano Zacchiroli
  2003-01-22 16:41 ` [Caml-list] " Michaël Grünewald
  1 sibling, 0 replies; 3+ messages in thread
From: Stefano Zacchiroli @ 2003-01-21 10:48 UTC (permalink / raw)
  To: caml-list; +Cc: Pietro Abate

On Tue, Jan 21, 2003 at 06:22:28PM +1100, Pietro Abate wrote:
> is it possible ?

Yes, just use the Dynlink module.

You can find a great usage example in the O'Reilly book [1] (debian
package available in debian/unstable ocaml-book-{en,fr}).

Anyway, returning to your example you should have an association list
ref, say "algos", and you should use it as

  let algo =
    try
      List.assoc "command_line_option" !algos
   with Not_found -> failwith "Module not loaded"

Then you should have a set of .cmos that you should load using
'Dynlink.loadfile', each module should update the 'algos' association
list adding itself to it.

Hope this helps,
Cheers.

[1] http://caml.inria.fr/oreilly-book/html/

-- 
Stefano Zacchiroli  -  Undergraduate Student of CS @ Uni. Bologna, Italy
zack@{cs.unibo.it,debian.org,bononia.it}  -  http://www.bononia.it/zack/
"  I know you believe you understood what you think I said, but I am not
sure you realize that what you heard is not what I meant!  " -- G.Romney
-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Caml-list] Re: load modules by name
  2003-01-21  7:22 [Caml-list] load modules by name Pietro Abate
  2003-01-21 10:48 ` Stefano Zacchiroli
@ 2003-01-22 16:41 ` Michaël Grünewald
  1 sibling, 0 replies; 3+ messages in thread
From: Michaël Grünewald @ 2003-01-22 16:41 UTC (permalink / raw)
  To: caml-list

Pietro Abate <Pietro.Abate@anu.edu.au> writes:

> hi list,
> 
> I'm trying to figure out how I can build a kind of associative list
> to load a specific module at runtime. I've reread few old messages about
> first-class modules and other oddities, but I've the sensation my
> problem is easier.
> 
> I have a library (compiled), an application (compiled) and a bunch of
> user defined modules (that are compiled as well, but these may be vary
> in number). At the moment the application select the rigth module
> statically regarding a command line argument. However this way I must
> recompile my application everytime I add a new module.

Why don't you build a little bunch of software that maintain a 'record
book' of given modules/files? Thus the client software find easily a
list in the 'record book', while you just need to update this book each
time you add a new algorithm into your collection (or remove one).

In the main code part, you may have a module

module Algo =
struct

    type t = int -> int list (* e.g. *)
    type id = int

    let assoc = ref [] (* (id * algo) list -- assoc *)
    let subscribe a b = assoc := (a,b) :: !assoc
    let get i = List.assoc i !assoc

end

each module dynamically loaded has a registration part that says

let _ = Algo.subscription my_id my_algo

where my_id is an unique identification key known in the record book,
thus you can get back the algorithm with the Algo.get function, while
you keep away from your code any reference to hardwired module name.
-- 
Michaël Grünewald <michael-grunewald@wanadoo.fr>
-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-01-23 10:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-21  7:22 [Caml-list] load modules by name Pietro Abate
2003-01-21 10:48 ` Stefano Zacchiroli
2003-01-22 16:41 ` [Caml-list] " Michaël Grünewald

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).