caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* type of high order functions
@ 2010-12-14 20:30 Alexander Bernauer
  2010-12-15  8:31 ` [Caml-list] " Florent Ouchet
  2010-12-15  8:32 ` Alain Frisch
  0 siblings, 2 replies; 5+ messages in thread
From: Alexander Bernauer @ 2010-12-14 20:30 UTC (permalink / raw)
  To: caml-list

[-- 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 --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-12-16  8:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-12-14 20:30 type of high order functions Alexander Bernauer
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).