From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.3 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 9D22ABC69 for ; Wed, 25 Apr 2007 00:22:33 +0200 (CEST) Received: from smtp5-g19.free.fr (smtp5-g19.free.fr [212.27.42.35]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l3OMMX1x009439 for ; Wed, 25 Apr 2007 00:22:33 +0200 Received: from [192.168.1.222] (vil93-4-82-227-140-227.fbx.proxad.net [82.227.140.227]) by smtp5-g19.free.fr (Postfix) with ESMTP id 237F842F83; Wed, 25 Apr 2007 00:22:33 +0200 (CEST) Message-ID: <462E8327.3020101@philippewang.info> Date: Wed, 25 Apr 2007 00:22:31 +0200 From: Philippe Wang User-Agent: Thunderbird 1.5.0.10 (Macintosh/20070221) MIME-Version: 1.0 To: micha , ocaml ml Subject: Re: [Caml-list] typing question References: <20070424201435.38f2f5f9@hmmm.de> In-Reply-To: <20070424201435.38f2f5f9@hmmm.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at discorde with ID 462E8329.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; cheers:01 expansive:01 expansive:01 forall:01 runtime:01 ocaml:01 wrote:01 typing:01 caml-list:01 int:01 int:01 expression:02 expression:02 callback:02 callback:02 micha wrote: > Hi, > > why is the type of register1 'a -> string but the type of register2 > _'a -> string? > > > cheers > Michael > > > > let symbol_id = ref 0;; > let register1 fkt = > let name = "symbol-" ^ (string_of_int !symbol_id) in > incr symbol_id; > Callback.register name fkt; > name > ;; > > let register2 = > let symbol_id = ref 1 in > fun fkt -> let name = "symbol-" ^ (string_of_int !symbol_id) in > incr symbol_id; > Callback.register name fkt; > name > ;; > It is because register2 is detected as an "expansive" expression. "Expansive expressions" are those that we can't generalize without taking the risk to allow type errors at execution, so its type variable remains unknown and can't be "forall 'a. 'a". Take this simple example : let x = ref [] So x : '_a ref, because if x were 'a ref, then you could write x := [343] ; x := ["hello"] and there would be a type error at runtime. Thus, even if an expression such as id2 in : let id x = x ;; let id2 = id id ;; does exactly the same computing as id, it can't be 'a -> 'a because id2 is an "application" and ocaml doesn't generalize applications... Good luck, -- Philippe Wang