caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] scripting ocaml from inside of ocaml
@ 2003-06-27 16:51 Chris GauthierDickey
  2003-06-27 18:00 ` Yamagata Yoriyuki
  2003-07-01  9:47 ` [Caml-list] scripting ocaml from inside of ocaml Jean-Christophe Filliatre
  0 siblings, 2 replies; 5+ messages in thread
From: Chris GauthierDickey @ 2003-06-27 16:51 UTC (permalink / raw)
  To: caml-list

Hi all,

  Just a disclaimer--this is a long message.

  I've searched the FAQs, manuals, and mailing lists to no avail. I have an
application that I'm writing in ocaml, and I need to do some scripting for
the program. I'd like to use ocaml as the scripting language. I've seen
references to using ocaml to interpret scripts from files, etc, but these
all assume that your environment is closed to the scripting. In particular,
I would like to be able to use ocaml to interpret scripts, have scripts
refer to objects that I import into their environment, and be able to take
values from their bindings in the scripts environment and use them in my
ocaml code.

  In some ways, the toplevel program does some of what I want, so using the
actual ocaml source, I created a library with the following functions:


exception ScriptError

let eval_script doprint ppf script =
  (* first, create a lexical buffer *)
  let lbuf = Lexing.from_string script in
    try
      let phrase = try !parse_toplevel_phrase lbuf with Exit -> raise
ScriptError in
         if !Clflags.dump_parsetree then Printast.top_phrase ppf phrase;
         ignore(execute_phrase true ppf phrase)
    with
      | End_of_file -> exit 0
      | Sys.Break -> fprintf ppf "Interrupted.@."
      | ScriptError -> ()
      | x -> Errors.report_error ppf x

let init_scripting () =
  Sys.interactive := false;
  Compile.init_path();
  initialize_toplevel_env ()

In this way, I can use Toploop.toplevel_env to store the environment that is
created from evaluating the script. Toploop also has a few other functions
to check the environment, such as:

Toploop.getvalue : str -> Obj.t
and
Toploop.setvalue : str -> Obj.t -> unit

so, all of these allow me to do something like:

let main () =
  (* set up the environment for scripting *)
  init_scripting();
  run_script true Format.std_formatter "let add x y = x + y;;";
  run_script true Format.std_formatter "let x = add 2 3;;"
in
  main () ;;

and this adds a binding to add for the function, and x for the result in
Toploop.toplevel_env.

However, my dilema is this: How can I add my own bindings to Obj.t in
Toploop.toplevel_env
and how can I retrieve values from Obj.t?  Taking a look under the bytecomp
directory under
the source distribution seems to indicate that Obj.t is an abstract type
that represents all types
in bytecode. Does anyone know of a way I can manipulate Obj.t and create my
own bindings
and extract the results?

Thanks for listening (=,
Chris


-------------------
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] 5+ messages in thread

* Re: [Caml-list] scripting ocaml from inside of ocaml
  2003-06-27 16:51 [Caml-list] scripting ocaml from inside of ocaml Chris GauthierDickey
@ 2003-06-27 18:00 ` Yamagata Yoriyuki
  2003-06-28  0:47   ` [Caml-list] Operation complexity Zed Sereg
  2003-07-01  9:47 ` [Caml-list] scripting ocaml from inside of ocaml Jean-Christophe Filliatre
  1 sibling, 1 reply; 5+ messages in thread
From: Yamagata Yoriyuki @ 2003-06-27 18:00 UTC (permalink / raw)
  To: chrisg; +Cc: caml-list

From: "Chris GauthierDickey" <chrisg@cs.uoregon.edu>
Subject: [Caml-list] scripting ocaml from inside of ocaml
Date: Fri, 27 Jun 2003 09:51:35 -0700

> However, my dilema is this: How can I add my own bindings to Obj.t in
> Toploop.toplevel_env
> and how can I retrieve values from Obj.t?

"Obj.repr : 'a -> Obj.t" and "Obj.obj : Obj.t -> 'a".  They are not
type safe, so use with care!

--
Yamagata Yoriyuki

-------------------
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] 5+ messages in thread

* [Caml-list] Operation complexity
  2003-06-27 18:00 ` Yamagata Yoriyuki
@ 2003-06-28  0:47   ` Zed Sereg
  2003-06-28  1:17     ` Karl Zilles
  0 siblings, 1 reply; 5+ messages in thread
From: Zed Sereg @ 2003-06-28  0:47 UTC (permalink / raw)
  To: caml-list

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

Hello World, I have a obfuscating question, for whih I can found no answer.

I have to handle huge string arrays in a kind of database parsing. 
At the end, i have a code of the shape : 
let request_word s = ...
val request_word : string -> string array list

let associate v =
    let n = Array.length v in
    let v' = array.make n (request_word v.(0)) in
    for i = 1 to n-1 do
        v'.(i) <- (request_word v.(i));
    done;
    v'
;;

val associate : string array -> string array list array = <fun>

Yeah, it 's obfuscating, but I had no time to find another solution.And this is awfully slow... (In order to know, the computating time of each request separately is around 2 secs. I waited 5 minutes for a call of associate with a 3 elements vector.)

I would like to know if there is a way to do the same thing, without replacing a value in v' at each iteration. Would it really be better to switch the structure from an array to a temporary list, and after, copying the list into an array with Array.of_list.

Thanks 

Zed


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

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

* Re: [Caml-list] Operation complexity
  2003-06-28  0:47   ` [Caml-list] Operation complexity Zed Sereg
@ 2003-06-28  1:17     ` Karl Zilles
  0 siblings, 0 replies; 5+ messages in thread
From: Karl Zilles @ 2003-06-28  1:17 UTC (permalink / raw)
  To: Zed Sereg; +Cc: caml-list

Zed Sereg wrote:
> Hello World, I have a obfuscating question, for whih I can found no answer.
>  
> I have to handle huge string arrays in a kind of database parsing.
> At the end, i have a code of the shape :
> let request_word s = ...
> val request_word : string -> string array list
>  
> let associate v =
>     let n = Array.length v in
>     let v' = array.make n (request_word v.(0)) in
>     for i = 1 to n-1 ledo
>         v'.(i) <- (request_word v.(i));
>     done;
>     v'
> ;;
>  
> val associate : string array -> string array list array = <fun>

Hi Zed,

Have you looked at Array.map?

let associate = Array.map request_word;;

>  
> Yeah, it 's obfuscating, but I had no time to find another solution.And 
> this is awfully slow... (In order to know, the computating time of each 
> request separately is around 2 secs. I waited 5 minutes for a call of 
> associate with a 3 elements vector.)

I have trouble believing that associate is your problem.  Time to fire 
up the profiler to find out where that time is coming from?

>  
> I would like to know if there is a way to do the same thing, without 
> replacing a value in v' at each iteration. Would it really be better to 
> switch the structure from an array to a temporary list, and after, 
> copying the list into an array with Array.of_list.

Your code looks ok to me.  I think your problem is elsewhere.

Karl

-------------------
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] 5+ messages in thread

* Re: [Caml-list] scripting ocaml from inside of ocaml
  2003-06-27 16:51 [Caml-list] scripting ocaml from inside of ocaml Chris GauthierDickey
  2003-06-27 18:00 ` Yamagata Yoriyuki
@ 2003-07-01  9:47 ` Jean-Christophe Filliatre
  1 sibling, 0 replies; 5+ messages in thread
From: Jean-Christophe Filliatre @ 2003-07-01  9:47 UTC (permalink / raw)
  To: Chris GauthierDickey; +Cc: caml-list


Chris GauthierDickey writes:
 > 
 >   I've searched the FAQs, manuals, and mailing lists to no avail. I have an
 > application that I'm writing in ocaml, and I need to do some scripting for
 > the program. I'd like to use ocaml as the scripting language. I've seen
 > references to using ocaml to interpret scripts from files, etc, but these
 > all assume that your environment is closed to the scripting. In particular,
 > I would like to be able to use ocaml to interpret scripts, have scripts
 > refer to objects that I import into their environment, and be able to take
 > values from their bindings in the scripts environment and use them in my
 > ocaml code.

There may  be another solution  to your problem  if you are  fine with
running bytecode only:  you can build your program on  top of an ocaml
toplevel (with ocamlmktop) and  then use Topdirs.dir_use (the function
which realizes #use in the toplevel) to interpret your ocaml scripts.

Hope this helps,
-- 
Jean-Christophe Filliâtre (http://www.lri.fr/~filliatr)

-------------------
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] 5+ messages in thread

end of thread, other threads:[~2003-07-01  9:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-06-27 16:51 [Caml-list] scripting ocaml from inside of ocaml Chris GauthierDickey
2003-06-27 18:00 ` Yamagata Yoriyuki
2003-06-28  0:47   ` [Caml-list] Operation complexity Zed Sereg
2003-06-28  1:17     ` Karl Zilles
2003-07-01  9:47 ` [Caml-list] scripting ocaml from inside of ocaml Jean-Christophe Filliatre

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