caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Francois Pottier <francois.pottier@inria.fr>
To: Ohad Rodeh <ORODEH@il.ibm.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] mutability analysis too strict?
Date: Mon, 10 Dec 2001 11:50:40 +0100	[thread overview]
Message-ID: <20011210115040.A31678@pauillac.inria.fr> (raw)
In-Reply-To: <OFF96B3E7D.145DD6FC-ONC2256B1E.00379C10@telaviv.ibm.com>; from ORODEH@il.ibm.com on Mon, Dec 10, 2001 at 12:13:19PM +0200


On Mon, Dec 10, 2001 at 12:13:19PM +0200, Ohad Rodeh wrote:
> 
> How do you propose building a repository of polymorphic values then?

Unless I'm mistaken, the short answer is, you can't. As pointed out by
Rémi, your proposed interface:

  module type Repos = sig
    val put : 'a -> 'b -> unit
    val get : 'a -> 'b
  end

is inherently unsafe. One solution is to use several repositories
(i.e. several hash tables). You can wrap them up into a single module if you
wish, but the client will then need to tell which repository (i.e. which hash
table) he wishes to access:

  module type Repositories = sig
    type ('a, 'b) repository
    val put : ('a, 'b) repository -> 'a -> 'b -> unit
    val get : ('a, 'b) repository -> 'a -> 'b
  end

Not very useful, though, since this merely paraphrases the interface for hash
tables.

Another solution is to create a single repository where keys and/or objects
belong to a suitably defined `universal' (also known as `dynamic') type. That
is, the repository would be a hash table of type [(univ, univ) Hashtbl.t],
where the abstract type [univ] supports the following operations:

  type univ
  type 'a tag

  val new_tag: unit -> 'a tag
  val in: 'a tag -> 'a -> univ
  val out: 'a tag -> univ -> 'a

A value of arbitrary type can be turned into a [univ] value by attaching it
with an explicit tag, using [in]. It can later be retrieved by matching it
against the same tag, using [out]. ([out] may fail dynamically if the tags do
not match.) [out] is type-safe because it requires a tag; it would be unsound
for [out] to have type unit -> 'a (same problem as for [get] above).

One possible implementation for [univ], where a tag is implemented as a
reference cell, was posted to the list a while ago, but I can't find it at the
moment. Another possible implementation implements tags using exception names.
(In this variant, the type [tag] disappears, and [new_tag] is a functor which
produces specialized versions of [in] and [out].) These solutions are rather
tricky to implement; furthermore, they are inefficient and unsafe, because
[out] involves a run-time check. You are probably better off using several
hash tables.

-- 
François Pottier
Francois.Pottier@inria.fr
http://pauillac.inria.fr/~fpottier/
-------------------
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-12-10 10:50 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-12-10 10:13 Ohad Rodeh
2001-12-10 10:30 ` Basile STARYNKEVITCH
2001-12-10 10:50 ` Francois Pottier [this message]
2001-12-10 11:21 ` Mark Seaborn
  -- strict thread matches above, loose matches on Subject: below --
2001-12-10  9:12 Ohad Rodeh
2001-12-10  9:28 ` Remi VANICAT
2001-12-09 15:43 Ohad Rodeh
2001-12-10  8:13 ` Francois Pottier
2001-12-09 12:14 [Caml-list] Mutability " Ohad Rodeh

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=20011210115040.A31678@pauillac.inria.fr \
    --to=francois.pottier@inria.fr \
    --cc=ORODEH@il.ibm.com \
    --cc=caml-list@inria.fr \
    /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).