From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id CAA18897; Thu, 8 Apr 2004 02:17:41 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id CAA19118 for ; Thu, 8 Apr 2004 02:17:40 +0200 (MET DST) Received: from ptb-relay03.plus.net (ptb-relay03.plus.net [212.159.14.214]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i380HdYM014148 for ; Thu, 8 Apr 2004 02:17:39 +0200 Received: from [80.229.56.224] (helo=chetara) by ptb-relay03.plus.net with esmtp (Exim) id 1BBNEg-000FgR-Pk for caml-list@inria.fr; Thu, 08 Apr 2004 00:17:38 +0000 From: Jon Harrop Organization: University of Cambridge To: caml-list@inria.fr Subject: Re: [Caml-list] Dynamically evaluating OCaml code Date: Thu, 8 Apr 2004 01:17:46 +0100 User-Agent: KMail/1.5.4 References: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200404080117.46643.jdh30@cam.ac.uk> X-Miltered: at concorde by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 dynamically:01 2004:99 python:01 python's:01 python:01 bbc:99 run-time:01 mismatch:01 evaluates:01 ocaml:01 ocaml:01 imho:01 distinguish:01 raises:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk X-Status: X-Keywords: X-UID: 121 On Wednesday 07 April 2004 7:47 pm, 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. I know exactly what you mean. I used to use an equivalent "EVAL" function in BBC BASIC. I notice that some of the responses to your post stated that this would break the type system. I think that this is incorrect because it is failing to distinguish between "eval" and dynamic code loading. Obviously we want the latter to be typesafe, but the former blurs the distinction between compile- and run-time, and valid results of calling "eval" would be a syntax error in the given code or, equally, a type mismatch in the result. I suspect you want a function which returns one of a set of possible values, or raises exceptions in the case of invalid code or return type. Clement Capel's response is the most interesting so far (IMHO), but I would be interested to see a safe version which allowed you to get at the resulting data. :-) Just out of curiosity, what kind of stuff do you want to evaluate? Simple mathematical expressions, or actual programs? Cheers, Jon. ------------------- 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