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 3BE24BC57 for ; Wed, 15 Dec 2010 09:32:50 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AmcEAM4LCE3B/BfTWmdsb2JhbACkLwEWCgsHEgIhwTeFSgSKe4MW X-IronPort-AV: E=Sophos;i="4.59,347,1288566000"; d="scan'208";a="83941279" Received: from msa02.smtpout.orange.fr (HELO msa.smtpout.orange.fr) ([193.252.23.211]) by mail2-smtp-roc.national.inria.fr with ESMTP; 15 Dec 2010 09:32:49 +0100 Received: from [192.168.1.63] ([83.199.16.217]) by mwinf5d25 with ME id jLYo1f00D4gzykb03LYoF0; Wed, 15 Dec 2010 09:32:49 +0100 Message-ID: <4D087D30.1000507@frisch.fr> Date: Wed, 15 Dec 2010 09:32:48 +0100 From: Alain Frisch User-Agent: Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.9.2.13) Gecko/20101207 Thunderbird/3.1.7 MIME-Version: 1.0 To: Alexander Bernauer Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] type of high order functions References: <20101214203043.GA9093@apus.ethz.ch> In-Reply-To: <20101214203043.GA9093@apus.ethz.ch> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; frisch:01 frisch:01 foo:01 foo:01 ocaml:01 syntax:01 recursion:01 polymorphic:01 wrote:01 abstract:01 caml-list:01 functions:01 int:01 int:01 alain:01 On 12/14/2010 09:30 PM, Alexander Bernauer wrote: > The code > ---8<--- > let foo ():string = > let f: ('a -> string) -> 'a -> string = fun g v -> g v > in let h = string_of_int > in let i = string_of_float > in let x = f h 23 > in let y = f i 23.0 > in x ^ y > --->8--- > > leads to the following error message > > ---8<--- > File "test.ml", line 6, characters 14-15: > Error: This expression has type float -> string > but an expression was expected of type int -> string > --->8--- The scope for type variables is implicitly defined as the smallest surrounding structure item. In your case, the scope for 'a is the "let foo..." declaration, not the local declaration "let f". There are two solution to do what you want in OCaml 3.12. * Use the syntax introduced for polymorphic recursion: let f: 'a. ('a -> string) -> 'a -> string = fun g v -> g v * Create a locally abstract type: let f (type a) (g : a -> string) (v : a) : string = g v -- Alain