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 QAA15570; Fri, 13 Aug 2004 16:20:20 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id QAA15520 for ; Fri, 13 Aug 2004 16:20:19 +0200 (MET DST) Received: from relaissmtp.ens-lyon.fr (pluvier.ens-lyon.fr [140.77.167.5]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i7DEKJmL020755 for ; Fri, 13 Aug 2004 16:20:19 +0200 Received: from mouette.ens-lyon.fr ([140.77.167.4]) by relaissmtp.ens-lyon.fr with esmtp (Exim 3.35 #1 (Debian)) id 1Bvcum-00037Z-00 for ; Fri, 13 Aug 2004 16:20:16 +0200 Received: by mouette.ens-lyon.fr (Postfix, from userid 33) id 9BC9E3C61C2; Fri, 13 Aug 2004 16:20:16 +0200 (CEST) To: caml-list@pauillac.inria.fr Subject: Re: [Caml-list] Managing a polymorphic environment Message-ID: <1092406816.411cce207d09b@mouette.ens-lyon.fr> Date: Fri, 13 Aug 2004 16:20:16 +0200 (CEST) From: Jean-Baptiste Rouquier MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit User-Agent: IMP/PHP IMAP webmail program 2.2.6 X-Originating-IP: 68.40.185.95 X-Miltered: at nez-perce with ID 411CCE23.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 ens-lyon:01 pointers:01 val:01 printf:01 printf:01 val:01 int:01 int:01 distinguish:01 polymorphic:01 trivial:01 float:02 float:02 objects:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk >The first problem is that it doesn't work. (==) reduces to (=) for >simple types > >let x = 5 >let y = 5 >let env = Env.add x "x" env >let env = Env.add y "y" env > ># Env.get_string x env ;; >- : string = "y" Because it's the same internal representation : "5" is stored in one word, exactly the same word for all the values storing "5". int isn't boxed, there are no pointers involved here. So (==) and (=) have no choice but being the same operation on int. You have to box your type if you want to distinguish between x and y. And once they are boxed, why not adding the description in this type ? There are several choices: trivial tuple type, property lists, objects... Then, since you look for efficiency and thus want to work on int or float, you need conversion functions between this boxed type (which makes (==) work as you want) and int / float (more efficient). You need two distinct types. For instance here you will also need a fonction 'a result -> 'a, and complex_computation will have type int -> int -> int. >># type 'a result = 'a * string;; >># let new_result (a:'a) description = ((a,description) : 'a result);; >>val new_result : 'a -> string -> 'a result = >># let print_int ((a,d): int result) = Printf.printf "%s is %d\n%!" d a;; >>val print_int : int result -> unit = >># let print_float = ... >># let x = new_result 10 "the number of elements in the knapsack";; >>val x : int result = ... >># print_int x;; >>the number of elements in the knapsack is 10 >>- : unit = () > > >Here is the problem of this approach > ># let complex_computation = fun x y -> x + y >val complex_computation : int -> int -> int = > >let x = simple_computation () and > y = simple_computation () > >let z = complex_computation x y > >Now I want to add a comment to x to print intermediate results and >check the first computation is correct. In your approach, this lifts x >: int on x : int result. But what will then be the type of >complex_computation ? Jean-Baptiste. ------------------- 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