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 JAA03324; Wed, 11 Jul 2001 09:52:39 +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 JAA03319 for ; Wed, 11 Jul 2001 09:52:38 +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 f6B7qb909559; Wed, 11 Jul 2001 09:52:37 +0200 (MET DST) Received: (from fpottier@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id JAA03312; Wed, 11 Jul 2001 09:52:36 +0200 (MET DST) Date: Wed, 11 Jul 2001 09:52:36 +0200 From: Francois Pottier To: Mark Seaborn Cc: caml-list@inria.fr, =?iso-8859-1?Q?Fran=E7ois_Pottier?= Subject: Re: [Caml-list] Dynamic types, casts, and rights amplification Message-ID: <20010711095236.A3033@pauillac.inria.fr> Reply-To: Francois.Pottier@inria.fr References: <20010703214914J.mrs35@cam.ac.uk> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="ikeVEW9yuYc//A+q" Content-Transfer-Encoding: 8bit X-Mailer: Mutt 1.0i In-Reply-To: <20010703214914J.mrs35@cam.ac.uk>; from mrs35@cam.ac.uk on Tue, Jul 03, 2001 at 09:49:14PM +0100 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk --ikeVEW9yuYc//A+q Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Mark, > Here's a nice use of OCaml's casts to provide what can be viewed as an > open dynamic type (except that variant tags -- called brands here -- > are created at run time). There are ways of implementing such a dynamic type without using unsafe type casts (i.e. Obj.magic). I have attached a file containing two sample implementations. The first one generates exceptions dynamically and uses them as tags. The fact that they are exceptions is irrelevant; what matters is the ability to generate new tags at runtime. Unfortunately, this piece of code isn't accepted by O'Caml, because it doesn't recognize the type variable 'a in the exception declaration as bound by the preceding type annotation. The second one uses references as temporary buffers for data exchange. The idea was posted a while ago on this mailing list, but I couldn't find the exact reference. Instead of being tagged, a value is encoded within a function whose effect is to write the value into a reference. The second implementation is interesting, because it makes it easy to see why one shouldn't create polymorphic brands: brands *are* references in this approach. -- François Pottier Francois.Pottier@inria.fr http://pauillac.inria.fr/~fpottier/ --ikeVEW9yuYc//A+q Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="dyn.ml" module type Dyn = sig type 'a brand type t val make_brand : unit -> 'a brand val pack : 'a brand -> 'a -> t val unpack : 'a brand -> t -> 'a option val null : t end module Dyn : Dyn = struct type 'a brand = ('a -> exn) * (exn -> 'a option) type t = exn let make_brand : unit -> 'a brand = function () -> let module X = struct exception E of 'a let pack x = E x let unpack = function | E x -> Some x | _ -> None end in X.pack, X.unpack let pack = fst let unpack = snd exception Null let null = Null end module Dyn : Dyn = struct type 'a brand = ('a option) ref type t = unit -> unit let make_brand () = ref None let pack b x () = b := Some x let unpack b f = b := None; f(); !b let null () = () end --ikeVEW9yuYc//A+q-- ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr