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.0 required=5.0 tests=none autolearn=disabled version=3.1.3 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 5F9B1BC6B for ; Wed, 27 Jun 2007 10:40:35 +0200 (CEST) Received: from orion.metastack.com (no-dns-yet.demon.co.uk [80.177.38.218] (may be forged)) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l5R8eXNo007119 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Wed, 27 Jun 2007 10:40:34 +0200 Received: from treble (cpc2-cmbg6-0-0-cust535.cmbg.cable.ntl.com [81.107.34.24]) (authenticated bits=0) by orion.metastack.com (8.13.4/8.13.3) with ESMTP id l5R8XIub016804 (version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=NO) for ; Wed, 27 Jun 2007 09:33:19 +0100 From: "David Allsopp" To: "OCaml List" Subject: let rec and polymorphic functions Date: Wed, 27 Jun 2007 09:40:31 +0100 Organization: MetaStack Solutions Ltd. Message-ID: <000e01c7b896$d3372440$6a7ba8c0@treble> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 Thread-Index: Ace4ltJSL5O9cM8JSaauVWHQN+MWhg== X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028 X-Miltered: at concorde with ID 46822281.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; recursive:01 o'caml:01 printf:01 printf:01 polymorphic:01 polymorphic:01 rec:01 rec:01 functions:01 functions:01 inferred:02 behaves:02 inferred:02 parameters:02 construct:02 Why is let rec apparently unable to infer polymorphic function types? In both the expressions below, I'd expect [out] to have type [('a, unit, string, unit) format4 -> 'a]. Why when used in a [let rec] construct is it clearly inferred as [('_a, unit, string, unit) format4 -> '_a] and then instantiated as [(unit, unit, string, unit) format4 -> unit] by the first call to [out] in [f]? It seems to contradict the end of Section 6.7.1 of the manual. I know that [out] and [f] are not mutually recursive so there's no need to use [let rec] but I tend to use [let rec] in situations where I'm defining two functions where one uses the other at the [let ... in] level as it saves writing the extra [in]! This appears potentially to be a mistake, though... As ever, a technical explanation of why the type system behaves this way much appreciated! I won't make judgement on the hours of time wasted by the cryptic type errors in this case ;o) Just in case it matters, I'm using O'Caml 3.09.3... David (* * This first example works. *) let out line = Printf.printf line in let f () = (* * [out] is clearly polymorphic *) out "TEST"; out "%d" 0; out "%b" false; in f ();; (* * This second example does not. Why? *) let rec out line = Printf.printf line and f () = (* * [out] gets inferred as string -> unit here... *) out "TEST"; (* * ... and so we get a "too many parameters" error here. *) out "%d" 0; in f ();;