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 PAA22892; Mon, 28 May 2001 15:33:02 +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 PAA22901 for ; Mon, 28 May 2001 15:33:01 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f4SDWuj10598; Mon, 28 May 2001 15:32:56 +0200 (MET DST) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id PAA22895; Mon, 28 May 2001 15:32:56 +0200 (MET DST) Date: Mon, 28 May 2001 15:32:56 +0200 From: Xavier Leroy To: Chris Hecker Cc: Miles Egan , caml-list@inria.fr Subject: Re: [Caml-list] lisp -> ocaml Message-ID: <20010528153256.C21783@pauillac.inria.fr> References: <20010522093341.A97425@caddr.com> <20010522093341.A97425@caddr.com> <20010527150119.A8468@pauillac.inria.fr> <4.3.2.7.2.20010527170201.02754820@shell16.ba.best.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: <4.3.2.7.2.20010527170201.02754820@shell16.ba.best.com>; from checker@d6.com on Sun, May 27, 2001 at 05:06:12PM -0700 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > Right, I just meant in a program known to be running under the > toplevel, would it be possible to use the typing information that's > already there? Those functions aren't available right now, but I > don't see why they couldn't be. The "print" function cannot be a regular function because it needs both the value to be printed and the (representation of) the type of this value. In other terms, just having val print: 'a -> unit isn't enough, because the actual type of the argument would not be known to the print function. > > val new_property: unit -> 'a property > >val put_property: atom -> 'a property -> 'a -> unit > >val get_property: atom -> 'a property -> 'a > >... > > I'm confused by what this code is trying to do. Maybe I don't know > enough about lisp to know how this code maps to it, but what's an > example code snippet you'd use this code for? Property lists in classic Lisp are association lists attached to atoms. They provide a simple way to attach information to atoms. The keys of the a-list are arbitrary Lisp values (I think) but in practice are generally atoms or integers. The associated data are arbitrary Lisp values. The natural way to handle this in Caml is to parameterize the type of atoms by the types of the keys and data of the property list: type ('a, 'b) atom = { name : string; mutable properties : ('a * 'b) list } but this constraints quite a lot the data that can be put in property lists. If you're translating old Lisp code verbatim, it may be a problem. The mysterious code I sent works around this limitation by allowing data of several types to be stored in property lists, without being reflected in the type of atoms. Of course, to preserve type safety, property keys need to be typed, so that when we recover a data from a property list, its type is known. Here is how you'd use all this. Start with Lisp code where property A can be associated with integers or strings, and property B can be associated with booleans. You'd first define three Caml properties and assign them types: let prop_A_int = (new_property : int property) let prop_A_string = (new_property : string property) let prop_B = (new_property : bool property) Then you can add properties to any atom: put_property atom prop_A_int 12; put_property atom prop_B true; put_property atom2 prop_A_string "Hello" And recover the values in a type-safe way: get_property atom prop_A_int (--> type int, value 12 ) get_property atom prop_A_string ( --> type string, raises Not_found) Silly trick, but I find this quite enjoyable. - Xavier Leroy ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr