caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: xcforum@free.fr
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Question about polymorphic variants
Date: Sat, 29 Oct 2005 09:26:49 +0900 (JST)	[thread overview]
Message-ID: <20051029.092649.55723981.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <E2B22BDD-C8AE-4D16-B805-5231D086A942@free.fr>

From: Xavier Clerc <xcforum@free.fr>

> > An example close to the above one would be:
> >
> > # List.map (fun (x,y) -> x+1);;
> > - : (int * '_a) list -> int list = <fun>
> >
> > Now you might wonder why '_a cannot be polymorphic in the above
> > example. That is, could there really be a definition of List.map such
> > that the polymorphic type would be dangerous (causing a segmentation
> > fault for instance.)
> 
> This is indeed the question I was asking to myself ...
> 
> 
> > The answer is yes, with a counter-example using
> > the difference in representation between normal arrays and float
> > arrays.
> 
> In this counter-example, it is not clear to me whether the possible  
> problem is type-related or runtime-related. I mean, would this  
> counter-example still hold if arrays were both boxed or both unboxed ?

No, the difference in representation is essential here.

But at the type level, this problem can be expressed a bit
differently: whether it is safe to add a "top" to the type system,
allowing any type to be coerced to it. While some languages might
allow that, there is no fundamental reason it should be so (an this is
indeed not the case in ocaml.)
If we cannot assume the existence of such a type, then we cannot prove
that polymorphism in this case would be safe.

Note that the property used by the relaxed value restriction, that
one can always add safely a "bottom" to the type system, is much more
reasonable, as it's only assumption is that the representation of
values depend only on (typed) values, not on types alone. This is
valid in ocaml, but some other language might still break it.
> 
> > Actually, since this counter-example wouldn't apply to the above case
> > of polymorphic variants, this would probably be safe to leave the
> > polymorphic variant type as polymorphic...
> 
> Does this mean that inferring "[< `Off | `On] list -> int list" would  
> be perfectly safe in the example above ?
> (by saying so, I am not pleading for any compiler change, I am just  
> trying to organize my thoughts)

I believe so, but I don't have a proof ready for that.
One way to do it would be to prove that any instance of [< `Off | `On]
is a subtype of [ `Off | `On ], which sounds trivial in this case.
So I would say this should apply to both [< ... ] types (closed
polymorphic variants, including the [< ... > ...] case) and <...; ..>
types (extensible object types.) But not to [> ...] types (open
polymorphic variants.)

Jacques Garrigue


  reply	other threads:[~2005-10-29  0:25 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-10-28  9:57 Xavier Clerc
2005-10-28 11:59 ` [Caml-list] " Jacques Garrigue
2005-10-28 12:27   ` Xavier Clerc
2005-10-29  0:26     ` Jacques Garrigue [this message]
2005-10-31 17:08       ` Xavier Clerc
2005-11-01  0:27         ` Jacques Garrigue
2005-11-04 13:20           ` Xavier Clerc
2005-11-07  3:11             ` Jacques Garrigue
2005-11-07 13:39               ` Xavier Clerc

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=20051029.092649.55723981.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=xcforum@free.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).