caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Dynamic loading of bytecode
@ 2006-08-14 15:21 Tom
  2006-08-14 15:42 ` [Caml-list] " David MENTRE
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Tom @ 2006-08-14 15:21 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 559 bytes --]

There is a really stupid question I want to ask, I have been searching
online for some days now and couldn't get it answered...

How can you load the whole module hiearchy (many modules, dependent on one
another) with a single command in toploop?

Say I have files a.ml:

  let f x = x - 1

and b.ml:

  let g x y = x + f y - f x


and I compile them into bytecode

  ocamlc -c a.ml
  ocamlc -c b.ml

and then I want to be able to load them into the toploop so that I could
access both modules A and B:

  # let simplify x = B.g x - A.f x


How do I do that?

[-- Attachment #2: Type: text/html, Size: 764 bytes --]

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

* Re: [Caml-list] Dynamic loading of bytecode
  2006-08-14 15:21 Dynamic loading of bytecode Tom
@ 2006-08-14 15:42 ` David MENTRE
  2006-08-17 11:48 ` Xavier Leroy
  2006-08-18  1:15 ` Pietro Abate
  2 siblings, 0 replies; 4+ messages in thread
From: David MENTRE @ 2006-08-14 15:42 UTC (permalink / raw)
  To: Tom; +Cc: caml-list

Hello,

Tom <tom.primozic@gmail.com> writes:

> Say I have files a.ml:
>
>   let f x = x - 1
>
> and b.ml:
>
>   let g x y = x + f y - f x

    let g x y = x + A.f y - A.f x

>
> and I compile them into bytecode
>
>   ocamlc -c a.ml
>   ocamlc -c b.ml
>
> and then I want to be able to load them into the toploop so that I could
> access both modules A and B:
>
>   # let simplify x = B.g x - A.f x
>
>
> How do I do that?

* In command line given to ocaml toplevel:

$ ocaml a.cmo b.cmo
        Objective Caml version 3.08.3

# B.g 1 2;;


* Inside the toplevel, you need to #load each bytecode file:

$ ocaml
        Objective Caml version 3.08.3

# #load "a.cmo";;
# #load "b.cmo";;
# let simplify x = B.g x x - A.f x;;
val simplify : int -> int = <fun>


TFM: http://caml.inria.fr/pub/docs/manual-ocaml/manual023.html

Best wishes,
d.

PS: This kind of question might better go to caml-beginners@ mailing
    list. 
-- 
GPG/PGP key: A3AD7A2A David MENTRE <dmentre@linux-france.org>
 5996 CC46 4612 9CA4 3562  D7AC 6C67 9E96 A3AD 7A2A


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

* Re: [Caml-list] Dynamic loading of bytecode
  2006-08-14 15:21 Dynamic loading of bytecode Tom
  2006-08-14 15:42 ` [Caml-list] " David MENTRE
@ 2006-08-17 11:48 ` Xavier Leroy
  2006-08-18  1:15 ` Pietro Abate
  2 siblings, 0 replies; 4+ messages in thread
From: Xavier Leroy @ 2006-08-17 11:48 UTC (permalink / raw)
  To: Tom; +Cc: caml-list

> How can you load the whole module hiearchy (many modules, dependent on
> one another) with a single command in toploop?

You cannot.  However, you could build a .cma archive via a Makefile:

    ocamlc -a -o mylib.cma a.cmo b.cmo

and issue the toplevel command #load "mylib.cma", which will load both
module implementations at once.

- Xavier Leroy


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

* Re: [Caml-list] Dynamic loading of bytecode
  2006-08-14 15:21 Dynamic loading of bytecode Tom
  2006-08-14 15:42 ` [Caml-list] " David MENTRE
  2006-08-17 11:48 ` Xavier Leroy
@ 2006-08-18  1:15 ` Pietro Abate
  2 siblings, 0 replies; 4+ messages in thread
From: Pietro Abate @ 2006-08-18  1:15 UTC (permalink / raw)
  To: caml-list

On Mon, Aug 14, 2006 at 05:21:25PM +0200, Tom wrote:
> How can you load the whole module hiearchy (many modules, dependent on one
> another) with a single command in toploop?

I use these two functions to resolve linear dependencies. 
I'm sure there is a better solution... 

let modules = Hashtbl.create 17;;
let are_loading = Hashtbl.create 17;;

let find_in_path path name =
 let filename = ((String.uncapitalize name) ^ ".cmo") in
 if not (Filename.is_implicit filename) then
 if Sys.file_exists filename then filename else raise Not_found
 else
 begin
 let rec try_dir = function
 | [] -> raise Not_found
 | dir::rem ->
 let fullname = Filename.concat dir filename in
 if Sys.file_exists fullname then fullname
 else try_dir rem
 in try_dir path
 end

let rec load_module modname path =
 try
 Hashtbl.find modules modname
 with
 Not_found ->
 try
 Hashtbl.add modules modname ();
 Hashtbl.add are_loading modname ();
 (* Printf.printf "Loading: %s ..." modname; *)
 Dynlink.loadfile (modname);
 (* print_endline "done."; *)
 Hashtbl.remove are_loading modname
 with
 | Dynlink.Error(Dynlink.Unavailable_unit(depend))
 | Dynlink.Error(
 Dynlink.Linking_error(_,Dynlink.Undefined_global(depend))
 ) ->
 begin
 try
 if Hashtbl.mem are_loading depend
 then failwith ("Crossing with "^depend);
 load_module (find_in_path path depend) path;
 Hashtbl.remove modules modname;
 load_module modname path
 with Not_found ->
 failwith ("Cannot find "
 ^String.lowercase(depend)^" in "^
 (List.fold_left (fun s x -> s^x) " " path))
 end
 | Dynlink.Error(e) -> failwith (Dynlink.error_message e)
;;

-- 
++ Blog: http://blog.rsise.anu.edu.au/?q=pietro
++ 
++ "All great truths begin as blasphemies." -George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
   See http://www.fsf.org/philosophy/no-word-attachments.html


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

end of thread, other threads:[~2006-08-18  1:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-08-14 15:21 Dynamic loading of bytecode Tom
2006-08-14 15:42 ` [Caml-list] " David MENTRE
2006-08-17 11:48 ` Xavier Leroy
2006-08-18  1:15 ` Pietro Abate

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