From: Alexander Bernauer <bernauer@inf.ethz.ch>
To: <caml-list@yquem.inria.fr>
Subject: type of high order functions
Date: Tue, 14 Dec 2010 21:30:45 +0100 [thread overview]
Message-ID: <20101214203043.GA9093@apus.ethz.ch> (raw)
[-- Attachment #1: Type: text/plain, Size: 1272 bytes --]
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
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
next reply other threads:[~2010-12-14 20:30 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-12-14 20:30 Alexander Bernauer [this message]
2010-12-15 8:31 ` [Caml-list] " Florent Ouchet
2010-12-16 8:32 ` Alexander Bernauer
2010-12-16 8:46 ` bluestorm
2010-12-15 8:32 ` Alain Frisch
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20101214203043.GA9093@apus.ethz.ch \
--to=bernauer@inf.ethz.ch \
--cc=caml-list@yquem.inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox