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.8 required=5.0 tests=AWL,SPF_FAIL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 4EDCCBBAF for ; Sat, 10 Jan 2009 13:59:36 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtYCAOspaElQRFuwgWdsb2JhbACUDwEBFiK8OYVv X-IronPort-AV: E=Sophos;i="4.37,244,1231110000"; d="scan'208";a="33541378" Received: from furbychan.cocan.org ([80.68.91.176]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/AES256-SHA; 10 Jan 2009 13:59:36 +0100 Received: from rich by furbychan.cocan.org with local (Exim 4.63) (envelope-from ) id 1LLdR0-0001xo-8y; Sat, 10 Jan 2009 12:59:26 +0000 Date: Sat, 10 Jan 2009 12:59:26 +0000 To: Antoine Delignat-Lavaud Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Why does value restriction not apply to the empty list ? Message-ID: <20090110125925.GA6231@annexia.org> References: <496887BE.8030804@dptinfo.ens-cachan.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <496887BE.8030804@dptinfo.ens-cachan.fr> User-Agent: Mutt/1.5.13 (2006-08-11) From: Richard Jones X-Spam: no; 0.00; 0100,:01 ocaml:01 ocaml:01 toplevel:01 infers:01 val:01 10,:98 2009:98 wrote:01 caml-list:01 int:01 int:01 expression:02 string:02 string:02 On Sat, Jan 10, 2009 at 12:34:22PM +0100, Antoine Delignat-Lavaud wrote: > In Ocaml, the program > let el = [] in if List.length el > 0 then (List.hd el)+(int_of_string > (List.hd el)) else 0 ;; > yields not type error and returns 0 despite the use of el as both an int > list and a string list. > > Thus, I am wondering why does value restriction not apply to the empty > list in Ocaml. I don't think it's possible to do a cast with the empty > list (it is empty after all) but I don't see any benefit in doing so. It's a strange one ... when the if statement appears as a toplevel statement, OCaml infers the type 'a list for the list: # let el = [] ;; val el : 'a list = [] # if List.length el > 0 then (List.hd el)+(int_of_string (List.hd el)) else 0;; - : int = 0 # el ;; - : 'a list = [] But the same if statement within a function definition causes an error: # let f el = if List.length el > 0 then (List.hd el)+(int_of_string (List.hd el)) else 0;; ^^^^^^^^^^ This expression has type int but is here used with type string Rich. -- Richard Jones Red Hat