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 KAA26997; Fri, 21 Sep 2001 10:40:01 +0200 (MET DST) 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 KAA26993 for ; Fri, 21 Sep 2001 10:40:00 +0200 (MET DST) Received: from uni-sb.de (uni-sb.de [134.96.252.33]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f8L8dxn12580 for ; Fri, 21 Sep 2001 10:39:59 +0200 (MET DST) Received: from cs.uni-sb.de (cs.uni-sb.de [134.96.252.31]) by uni-sb.de (8.11.6/2001082200) with ESMTP id f8L8dvP27248; Fri, 21 Sep 2001 10:39:57 +0200 (CEST) Received: from mail.cs.uni-sb.de (IDENT:3bkk7vE5uYTtp0Oe4POHCyKFixoMSeDX@mail.cs.uni-sb.de [134.96.254.200]) by cs.uni-sb.de (8.11.6/2001081600) with ESMTP id f8L8dud28059; Fri, 21 Sep 2001 10:39:56 +0200 (CEST) Received: from ps.uni-sb.de (grizzly.ps.uni-sb.de [134.96.186.68]) by mail.cs.uni-sb.de (8.11.6/2001082200) with ESMTP id f8L8dr527608; Fri, 21 Sep 2001 10:39:53 +0200 (CEST) X-Authentication-Warning: email: Host grizzly.ps.uni-sb.de [134.96.186.68] claimed to be ps.uni-sb.de Received: from ps.uni-sb.de (zoidberg.ps.uni-sb.de [134.96.186.121]) by ps.uni-sb.de (8.11.2/8.11.0) with ESMTP id f8L8dqJ20828; Fri, 21 Sep 2001 10:39:52 +0200 Message-ID: <3BAAFCD8.DA77FF80@ps.uni-sb.de> Date: Fri, 21 Sep 2001 10:39:52 +0200 From: Andreas Rossberg Organization: =?iso-8859-1?Q?Universit=E4t?= des Saarlandes X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.4.3-12 i686) X-Accept-Language: de, en MIME-Version: 1.0 To: caml-list@inria.fr CC: Charles Martin Subject: Re: [Caml-list] polymorphic variant question References: <20010921001418.59933.qmail@web9208.mail.yahoo.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Charles Martin wrote: > > I have a number of modules A, B, C that share a single data structure > between them, all under the control of a central module. Each wants to > store different kinds of data in the data structure. I can create a > variant type to classify the data by module: > > data.mli: > type ('a, 'b, 'c) t = A of 'a | B of 'b | C of 'c > > So each of the individual modules then has code that looks like this: > > a.ml: > type ('b, 'c) t = { > data : (int, 'b, 'c) Data.t list ref; > } > > b.ml: > type ('a, 'c) t = { > data : ('a, string, 'c) Data.t list ref; > } > > Then the central module can have code like this: > > let data = ref [] in > let a = { A.data = data } in > let b = { B.data = data } in > let c = { C.data = data } in > > The trouble with this approach is that every time I add a new module, I > must add a new tag to the classify data type, and update all of my > signatures and type declarations in modules A, B, C, etc. This seems wrong > since the whole point is to separate these abstractions. > > I am hoping I can use polymorphic variants to escape this trap. Thus, the > code for an individual module would look like this: > > a.ml: > type t = { > data : [`A of int] list ref; > } > > Of course, then in the central module the type [`A of a_data] list ref > conflicts with the type [`B of b_data] list ref and [`C of c_data] list > ref. Did you try something along the lines of (* data.mli *) type 'a t = {data : 'a list ref} (* a.mli *) type 'a t = 'a Data.t constraint 'a = [> `A of a_data] (* b.mli *) type 'a t = 'a Data.t constraint 'a = [> `B of b_data] (* central.ml *) let data = {data = ref []} let a = data : 'a A.t let b = data : 'a B.t Best regards, - Andreas -- Andreas Rossberg, rossberg@ps.uni-sb.de "Computer games don't affect kids; I mean if Pac Man affected us as kids, we would all be running around in darkened rooms, munching magic pills, and listening to repetitive electronic music." - Kristian Wilson, Nintendo Inc. ------------------- 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