caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Andreas Rossberg <rossberg@ps.uni-sb.de>
To: caml-list@inria.fr
Cc: Charles Martin <joelisp@yahoo.com>
Subject: Re: [Caml-list] polymorphic variant question
Date: Fri, 21 Sep 2001 10:39:52 +0200	[thread overview]
Message-ID: <3BAAFCD8.DA77FF80@ps.uni-sb.de> (raw)
In-Reply-To: <20010921001418.59933.qmail@web9208.mail.yahoo.com>

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


  parent reply	other threads:[~2001-09-21  8:40 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-09-21  0:14 Charles Martin
2001-09-21  6:28 ` Francois Pottier
2001-09-21  8:39 ` Andreas Rossberg [this message]
2002-08-29 16:03 nadji
2002-08-29 16:41 ` Dan Schmidt
2002-08-29 17:16 ` Remi VANICAT
2002-08-29 19:05   ` nadji

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3BAAFCD8.DA77FF80@ps.uni-sb.de \
    --to=rossberg@ps.uni-sb.de \
    --cc=caml-list@inria.fr \
    --cc=joelisp@yahoo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).