Gabriel Scherer <[email protected]> writes:

> Indeed, a let-bound [] is generalized to a polymorphic ('a list), while a
> let-bound (ref []), or (Array.make n []), is not generalized. It would be
> unsound to do so as if you had a reference to a polymorphic ('a list) you 
> could
> add elements of different types in the list.
>
> When a type variable is not generalized (polymorphic), it stays an "unknown" 
> of
> the type system until it is unified to a known type:
>
> # let r = ref [];;
> val r : '_a list ref = {contents = []}
> # r := 1 :: !r;;
> - : unit = ()
> # r;;
> - : int list ref = {contents = [1]}
>
> '_a is sometimes called a "weakly polymorphic variable", in fact it is not
> polymorphic at all. It is just an unknown.
>
> In the code example above, the real type for '_a could be determined after two
> phrases in the top level. It may also happen as a compilation unit (if the 
> code
> was written in a file instead of fed phrase-per-phrase to the toplevel). We
> wouldn't observe the intermediary '_a inferred type, as the type are inferred
> globally.

I think you are missing the point.

I totaly get why it must be '_a instead if 'a. My question is why it
doesn't infer the full type from the call to the print function.

In my case the type inferences outputs something like this (using the
above example):

> # let r = ref [];;
> val r : '_a list ref = {contents = []}
> # r := 1 :: !r;;
> - : unit = ()
> # r;;
> - : '_a list ref = {contents = [1]}

I see my print call as equivalent to '1 :: !r'.

MfG
        Goswin

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

Reply via email to