From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 254DCBC57 for ; Tue, 14 Dec 2010 21:30:48 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AnABAFtiB01SgmMamWdsb2JhbACVao4pFQEBAQEBCAsKBxEpwkyFSgSKe4Mc X-IronPort-AV: E=Sophos;i="4.59,344,1288566000"; d="asc'?scan'208";a="82667984" Received: from edge20.ethz.ch ([82.130.99.26]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/AES128-SHA; 14 Dec 2010 21:30:47 +0100 Received: from CAS20.d.ethz.ch (172.31.51.110) by edge20.ethz.ch (82.130.99.26) with Microsoft SMTP Server (TLS) id 14.1.218.12; Tue, 14 Dec 2010 21:30:40 +0100 Received: from localhost (213.144.157.83) by CAS20.d.ethz.ch (172.31.51.110) with Microsoft SMTP Server (TLS) id 14.1.218.12; Tue, 14 Dec 2010 21:30:47 +0100 Date: Tue, 14 Dec 2010 21:30:45 +0100 From: Alexander Bernauer To: Subject: type of high order functions Message-ID: <20101214203043.GA9093@apus.ethz.ch> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="GID0FwUMdk1T2AWN" Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) X-Originating-IP: [213.144.157.83] X-Spam: no; 0.00; ocaml:01 compiler:01 foo:01 omitting:01 foo:01 compile:01 functions:01 int:01 int:01 expression:02 expression:02 parameter:02 string:02 string:02 seems:03 X-Attachments: type="application/pgp-signature" name="signature.asc" --GID0FwUMdk1T2AWN Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi if I specify the (I think) correct type for a high order function the OCaml compiler (version 3.11.2) rejects the *second* usage of that function. 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--- So the first usage of f seems to fix the type of its first parameter to int -> string. I could understand that. But what I don't get is that omitting the type restriction on f fixes the problem. ---8<--- let foo ():string = let f = 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--- And moving f to global scope fixes the problem, too: ---8<--- let f: ('a -> string) -> 'a -> string = fun g v -> g v let foo ():string = 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--- Why is it that the first example does not compile while the later ones do? regards Alex --GID0FwUMdk1T2AWN Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) iEYEARECAAYFAk0H0/UACgkQZ3o+CqNf/NfE7QCg71duWHd/iZxQnhdmb1MTMY+M REkAoMeB6CjVhMFl/yGoRsYRh6yNMqC8 =NcIr -----END PGP SIGNATURE----- --GID0FwUMdk1T2AWN--