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 mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id A1781BC58 for ; Wed, 15 Sep 2010 21:10:08 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Al8BAOa2kEzZSMDji2dsb2JhbAChbhUBAQEKCwoHDwUfxlGFQQSNKoJh X-IronPort-AV: E=Sophos;i="4.56,372,1280700000"; d="scan'208";a="59337567" Received: from fmmailgate02.web.de ([217.72.192.227]) by mail2-smtp-roc.national.inria.fr with ESMTP; 15 Sep 2010 21:10:08 +0200 Received: from smtp02.web.de ( [172.20.0.184]) by fmmailgate02.web.de (Postfix) with ESMTP id 61B53170AD57A; Wed, 15 Sep 2010 21:10:07 +0200 (CEST) Received: from [78.43.204.177] (helo=frosties.localdomain) by smtp02.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.110 #24) id 1OvxMt-0005TG-00; Wed, 15 Sep 2010 21:10:07 +0200 Received: from mrvn by frosties.localdomain with local (Exim 4.72) (envelope-from ) id 1OvxMs-0003c4-Tx; Wed, 15 Sep 2010 21:10:07 +0200 From: Goswin von Brederlow To: Radu Grigore Cc: Andreas Rossberg , caml-list@yquem.inria.fr Subject: Re: [Caml-list] mutable and polymorphism References: Date: Wed, 15 Sep 2010 21:10:06 +0200 In-Reply-To: (Radu Grigore's message of "Wed, 15 Sep 2010 18:59:01 +0100") Message-ID: <878w32g6wh.fsf@frosties.localdomain> User-Agent: Gnus/5.110009 (No Gnus v0.9) XEmacs/21.4.22 (linux, no MULE) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Sender: goswin-v-b@web.de X-Sender: goswin-v-b@web.de X-Provags-ID: V01U2FsdGVkX1+w3BaBVIwfgseQqc8XXLZL9910n7oXWsDKyfOf DRmU3V4fagzmwRU7L/7qUkkRQD6orTuR0eyrSiUr6X3oM+FDJ9 E0iHIt5pc= X-Spam: no; 0.00; mutable:01 polymorphism:01 rossberg:01 rossberg:01 type-checked:01 checker:01 typecheck:01 compiler:01 mfg:98 polymorphic:01 polymorphic:01 wrote:01 faq:01 andreas:01 caml-list:01 Radu Grigore writes: > On Wed, Sep 15, 2010 at 6:40 PM, Andreas Rossberg wrote: >> Have a look at this variant of c.ml and its "potential" if it type-checked: >>>  let f = let xs = ref [] in fun x -> xs := x :: !xs in f 1; f 'a' > > Interesting. If I would be a type checker, here's what I'd do. > 1. "ref []" tells me that (xs : '_a), because I know refs can't be polymorphic. > 2. "x :: !xs" tells me that (xs : '_a list) and (x : '_a) > 3. "x -> x :: !xs" tells me that (f : '_a -> '_a list) > 4. "f 1" tells me that (f : int -> int list) > 5. "f 'a'" tells me... OOPS > > Moreover, I'd have similar thoughts about a variant of a.ml (and a.ml > does typecheck!) > let xs = ref [] in let f = fun x -> xs:=x::!xs in f 1; f 'a' In your original a.ml you didn't use the reference inside f so f was polymorphic. That is was then further below the scope of a reference didn't matter since the application of f was too. > On the other hand, here's what I'd do for the original c.ml, which > with a few names added is > let f = let x = ref () in fun y -> () in f 1; f 'a' > 1. "ref ()" tells me that (x : unit ref) > 2. "y -> ()" tells me that (f : 'a -> unit) 2. "y -> ()" tells me that (f : '_a -> unit) Because the compiler is stupid and things with a ref can't polymorphic. It isn't smart enough to see that the type of the ref is independent of the type of the function and there could remain polymorphic. > 3. "f 1" returns () > 4. "f 'a'" return () > > In other words, I would have thought that in your example the problem > is that you tried to use a polymorphic reference. (And this problem > even appears in the FAQ.) The solution is called lifting I think? Anyway, you need to move the polymorphic argument outside the scope of the reference: let f y = let x = ref () in (fun y -> ()) y in f 1; f 'a' > regards, > radu MfG Goswin