Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Polymorphism and the "for" loop
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-10-21 (19:13)
From: Ville-Pertti Keinonen <will@e...>
Subject: Re: [Caml-list] Polymorphism and the "for" loop
Jon Harrop wrote:

>Considering the following code objects to the expression in the "for" loop not 
>having the type "unit":
># for i=0 to 3 do 1 done;;
>Warning: this expression should have type unit.
>- : unit = ()
>why is "g" in the following code inferred to have the polymorphic type (unit 
>-> 'a) rather than (unit -> unit)?
># let f g = for i=0 to 3 do g () done;;
>val f : (unit -> 'a) -> unit = <fun>
Consider the fact that it's a warning as opposed to an error.  An 
expression with a result that isn't used (fairly rare in OCaml) is a 
special case; any type is permitted, but an explicitly non-unit type 
generates a warning, since it's probably an error.

If the type system were more advanced, your second example would infer a 
type of "unit -> 'a <warning-if-non-unit>".

If your second example produced a type of unit -> unit for g, I think 
your first example should fail with an error (rather than be accepted 
with a warning).

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: