Le Saturday 03 Sep 2011 à 12:31:59 (+0200), Christophe Papazian a écrit :
> The type of [] is 'a list. This is (strong) polymorphism. Calling a
> function 'int list -> ?' on [] doesn't not
> remove the polymorphism of []. However, an '_a list array is not
> polymorphism, it's just the compiler who don't know
> yet the type inside the lists. And as you give him [], he can't
> deduce the type.
>
> As an example you can do this :
>
> let s = [] in 1::s, 'a'::s, [|s|] ;;
>
> And it still doesn't know...
Ah, right... not the most obvious case though for let-polymorphism.
A perhaps more talking example would be
let f x = x in (f 1, f "1")
While let-polymorphism makes obvious sense when using f as a function,
it's less intuitive when it used on a function argument such as [],
though quite logical.
Personnally, I find it quite weird to have the value restriction on list
refs, but not on lists:
# let x = [];;
val x : 'a list = []
# let x = ref [];;
val x : '_a list ref = {contents = []}
# let x = [] in (List.map print_string x), (List.map print_int x);;
- : unit list * unit list = ([], [])
# let x = ref [] in (List.map print_string !x), (List.map print_int
!x);;
Error: This expression has type string list
but an expression was expected of type int list
Using the print function on a state which doesn't have [] as first
component of the tuple would likely fixate properly the '_a, though.
--
Guillaume Yziquel
--
Caml-list mailing list. Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs