Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] loading files in toplevel
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Emmanuel.Chailloux@p...
Subject: Re: [Caml-list] loading files in toplevel

Hello,

    One month ago, I posted a question about loading files in toplevel.
    My goal was to write functions to select files to load :

    let choose x = if x < 0 then load "f1.ml" else load "f2.ml";;

  with
    f1.ml :  let f1_succ x = x + 1;;
    f2.ml :  let f2_succ x = x + 1;;

I wrote the following "load" function :

  let load file = Topdirs.dir_use Format.std_formatter file;;

which seems to run :

   #  choose 2;;
   val f2_succ : int -> int = <fun>
   - : unit = ()

but the global symbol f2_succ was unknown at the toplevel.

   # f2_succ;;
   Unbound value f2_succ


After my vacations, I  modified the Toploop.execute_phrase in the 
following way ( only 2 lines :  (* MODIF *) ) :

--------
let execute_phrase print_outcome ppf phr =
   match phr with
   | Ptop_def sstr ->
(* MODIF *) let oldenv = !toplevel_env in
       let (str, sg, newenv) = Typemod.type_structure !toplevel_env sstr 
in
       let lam = Translmod.transl_toplevel_definition str in
       Warnings.check_fatal ();
       let res = load_lambda ppf lam in
       begin match res with
       | Result v ->
           if print_outcome then begin
             match str with
             | [Tstr_eval exp] ->
                 fprintf ppf "@[- : %a@ =@ %a@]@."
                 Printtyp.type_scheme exp.exp_type
                 (print_value newenv v) exp.exp_type
             | [] -> ()
             | _ ->
                 fprintf ppf "@[<v>%a@]@."
                 (print_items newenv) sg
           end;
(* MODIF *) if oldenv <> newenv then  toplevel_env := newenv;
           true
....
------------

and my old "load" function runs now :

# load "f2.ml";;
val f2_succ : int -> int = <fun>
# f2_succ;;
- : int -> int = <fun>


This small modification can be integrated inside the future ocaml 
distribution ?


Best regards.
Emmanuel...
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr