English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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: 2004-04-07 (20:37)
From: Samuel Mimram <samuel.mimram@e...>
Subject: Re: [Caml-list] Dynamically evaluating OCaml code
Le Wed, 7 Apr 2004 18:47:30 +0000 (UTC) John Goerzen
<jgoerzen@complete.org> a écrit :

> Hello,
> 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.
> I have observed that /usr/bin/ocaml, the interactive top-level, is
> itself written in OCaml, which suggests that this should be possible.
> Although I have tried to study the source for this, it seems extremely
> complex and I can't figure out a way to do the simple evaluation
> described above.
> Can anyone help me out here?
> Thanks,
> John

Such a function cannot be programmed in OCaml because it would break the
typing system: what would the type of "eval"? 'a?
The following code could then lead to segfaults:

let n = ref "toto" in
n := eval "9"

because the typing system is borken: n is a string ref and gets an
integer! And there's no way to avoid that.

The only thing you can do is to dynamically load compiled bytecode files
(cf. the Dynlink module).


Samuel Mimram