Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Dynamically evaluating OCaml code
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Basile Starynkevitch <basile.starynkevitch@i...>
Subject: Re: [Caml-list] Dynamically evaluating OCaml code
On Wed, Apr 07, 2004 at 06:47:30PM +0000, John Goerzen wrote:

> I am moving from Python to OCaml and one of the things I miss is
> Python's eval() call.  It takes a string representing a bit of Python
> source code, evaluates it, and returns the result.  I would like to be
> able to do similar things with OCaml.

As observed by others, there is a big typing issue around this (what
would be the type of the eval function)?

If you really need something, you could hack the toplevel (at your own
risk). I don't think it is a good idea, unless you did very well
understood what you really want to do.

However, the functional values are usually enough to avoid the "eval",
and an "eval" won't be really safe.

Also, you could use metaocaml, which provide constructs (inspired by
eval) to typefully meta-program, ie generate programs at runtime in a
rather safe manner.

See http://www.cs.rice.edu/~taha/MetaOCaml/ for more.

dynamics (like old work from Leroy & Mauny - see X.Leroy's publication
pages on http://cristal.inria.fr/~xleroy ) and runtime type
information (like in Jun Furse GCaml) might also help - and are in
some remote way a bit related to eval's typing.

In addition to typing issues, there is also a runtime issue: dynamic
code generation really requires garbage collection of executable code,
which is not available in Ocaml (and would be terrible to implement -
it would mean rewrite most of the system)

________________


However, for the beginner, the good answer (at least as given by Ocaml
gurus here) to the usual "I want eval" request is simply "no you don't
really need it"



-- 
Basile STARYNKEVITCH -- basile dot starynkevitch at inria dot fr
Project cristal.inria.fr - INRIA Rocquencourt
http://cristal.inria.fr/~starynke --- all opinions are only mine 

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