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 MAA00182; Wed, 31 Mar 2004 12:05:23 +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 MAA30816 for ; Wed, 31 Mar 2004 12:05:22 +0200 (MET DST) Received: from qrnik.knm.org.pl (paf87.warszawa.sdi.tpnet.pl [217.96.225.87]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i2VA5IYM025258 for ; Wed, 31 Mar 2004 12:05:20 +0200 Received: from qrnik ([192.168.0.1] ident=qrczak) by qrnik.knm.org.pl with esmtp (Exim 3.36 #1) id 1B8cau-0006iq-00 for caml-list@inria.fr; Wed, 31 Mar 2004 12:05:12 +0200 Subject: Re: [Caml-list] extensible records again From: "Marcin 'Qrczak' Kowalczyk" To: caml-list@inria.fr In-Reply-To: <20040321062143.BE7D29BBA2@orchestra.cs.caltech.edu> References: <20040321062143.BE7D29BBA2@orchestra.cs.caltech.edu> Content-Type: text/plain; charset=ISO-8859-2 Message-Id: <1080727512.13334.16.camel@qrnik> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.5.5 Date: Wed, 31 Mar 2004 12:05:12 +0200 Content-Transfer-Encoding: quoted-printable 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 extensible:01 marcin:01 'qrczak':01 kowalczyk:01 qrczak:01 vanier:01 type-safe:01 descriptor:01 descriptor:01 ids:01 dynamically:01 generic:01 indirection:01 bootstrap:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk W li=B6cie z sob, 20-03-2004, godz. 22:21 -0800, Michael Vanier napisa=B3: > I've written a dynamically-typed language in ocaml, and I want to be able= to > add new types to the language without having to add new primitive types t= o > the system. When I had this problem, I found no satisfactory type-safe way, and had resorted to Obj.magic. Each object type has a common first field which points to a descriptor. The descriptor contains type id (unique integer) and implementation of operations common to all objects (in my case it's only application to a sequence of arguments; for performance there are separate entries for known small numbers of arguments and one entry taking a list). Type ids for new types are allocated dynamically, using a global counter. I ignore the possibility of wraparound, one would have to make 2**(word_size-1) new types (possible, as types can be defined locally and then forgotten, but improbable). The OCaml type which can represent an arbitrary object is a record type consisting of the descriptor only. Other types are longer records, which are Object.magic'ed to and from the generic type. Having the descriptor alone you can apply common operations or check the type. Unfortunately I found no other representation which is as flexible and as efficient. It must support performing common operations without knowing the type, taking some type id suitable for indexing a dictionary. New types must be definable without having to change the interpreter modules, and they can be defined both in OCaml code and from my language. Objects should not require much indirection or duplication. (The interpreter is now dead, I used it to bootstrap a compiler.) --=20 __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/ ------------------- 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