From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id DB9A8BB9A for ; Sat, 29 Oct 2005 02:25:57 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j9T0PvVU013159 for ; Sat, 29 Oct 2005 02:25:57 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id CAA31030 for ; Sat, 29 Oct 2005 02:25:56 +0200 (MET DST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j9T0Psun024046 for ; Sat, 29 Oct 2005 02:25:56 +0200 Received: from localhost (suiren [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.13.1/8.13.1) with ESMTP id j9T0PoFX012214; Sat, 29 Oct 2005 09:25:50 +0900 (JST) Date: Sat, 29 Oct 2005 09:26:49 +0900 (JST) Message-Id: <20051029.092649.55723981.garrigue@math.nagoya-u.ac.jp> To: xcforum@free.fr Cc: caml-list@inria.fr Subject: Re: [Caml-list] Question about polymorphic variants From: Jacques Garrigue In-Reply-To: References: <589B1E9E-D4EC-4E03-9375-30670ED82BEB@free.fr> <20051028.205957.68033883.garrigue@math.nagoya-u.ac.jp> X-Mailer: Mew version 4.2 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 4362C195.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 4362C192.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 variants:01 arrays:01 arrays:01 type-related:01 unboxed:01 coerced:01 ocaml:01 polymorphism:01 ocaml:01 variants:01 compiler:01 subtype:01 trivial:01 ...:98 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 From: Xavier Clerc > > An example close to the above one would be: > > > > # List.map (fun (x,y) -> x+1);; > > - : (int * '_a) list -> int list = > > > > 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