caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] loading files in toplevel
@ 2001-07-24 13:38 Emmanuel Chailloux
  0 siblings, 0 replies; 2+ messages in thread
From: Emmanuel Chailloux @ 2001-07-24 13:38 UTC (permalink / raw)
  To: caml-list


Hello,

For on-line applications,  I want to write a function load  : string -> unit 
which loads  ml files in the O'Caml toplevel.  For example : 

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

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

  My first try was : 

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

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

BUT I can't acces to the global symbol f1_succ : 
  # f1_succ;;
==>
  Unbound value f1_succ

This symbol belongs to the toplevel environment :  
   # Toploop.getvalue "f1_succ";;
==>
  - : Obj.t = <abstr>

And this is the good definition : 
  # ( (Obj.obj (Toploop.getvalue "f1_succ")) : int -> int) 1;;
==>
  - : int = 2 

When I trace the function "Toploop.execute_phrase" : 
  # #trace Toploop.execute_phrase;;
==>
  Toploop.execute_phrase is now traced.

the trace output is visible when I use my  function "load" but not when I use 
the directive "#use". 

# load "f1.ml";;
Toploop.execute_phrase <-- true
Toploop.execute_phrase --> <fun>
Toploop.execute_phrase* <-- <abstr>
Toploop.execute_phrase* --> <fun>
Toploop.execute_phrase** <-- <abstr>
val f1_succ : int -> int = <fun>
Toploop.execute_phrase** --> true
- : unit = ()
# f1_succ;;
Unbound value f1_succ
# #use "f1.ml";;
val f1_succ : int -> int = <fun>
# f1_succ;;
- : int -> int = <fun>

When I read the files toplevel/toploop.ml and toplevel/topdirs.ml from the
distribution (3.01) I don't understand why the  directive "#use" doesn't call
Toploop.execute_phrase.

Is there a special mode for the directives? 

Have you any idea?


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


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

* Re: [Caml-list] loading files in toplevel
@ 2001-08-24 14:02 Emmanuel.Chailloux
  0 siblings, 0 replies; 2+ messages in thread
From: Emmanuel.Chailloux @ 2001-08-24 14:02 UTC (permalink / raw)
  To: caml-list; +Cc: Emmanuel.Chailloux


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


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

end of thread, other threads:[~2001-08-24 13:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-24 13:38 [Caml-list] loading files in toplevel Emmanuel Chailloux
2001-08-24 14:02 Emmanuel.Chailloux

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