Version française
Home     About     Download     Resources     Contact us    
Browse thread
Question about polymorphic variants
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Clerc <xcforum@f...>
Subject: Re: [Caml-list] Question about polymorphic variants

Le 28 oct. 05 à 13:59, Jacques Garrigue a écrit :

> From: Xavier Clerc <xcforum@free.fr>
>
>> Then, I use this function in the following expression :
>>
>> List.map f
>>
>> which is in turn inferred as: _[< `Off | `On ] list -> int list
>>
>> My question is about the meaning of the leading underscore in the
>> inferred type (given that I understand the meaning of the underscore
>> in an expression such as "Stack.create ()" that is inferred as: '_a
>> Stack.t).
>>
>
> This is exactly the same meaning: [< `Off | `On] has some form of
> flexibility left, which you might see as a type variable, and as such
> it obeys the same rules as type variables.
>
> An example close to the above one would be:
>
> # List.map (fun (x,y) -> x+1);;
> - : (int * '_a) list -> int list = <fun>
>
> Now you might wonder why '_a cannot be polymorphic in the above
> example. That is, could there really be a definition of List.map such
> that the polymorphic type would be dangerous (causing a segmentation
> fault for instance.)

This is indeed the question I was asking to myself ...


> The answer is yes, with a counter-example using
> the difference in representation between normal arrays and float
> arrays.

In this counter-example, it is not clear to me whether the possible  
problem is type-related or runtime-related. I mean, would this  
counter-example still hold if arrays were both boxed or both unboxed ?


> Actually, since this counter-example wouldn't apply to the above case
> of polymorphic variants, this would probably be safe to leave the
> polymorphic variant type as polymorphic...

Does this mean that inferring "[< `Off | `On] list -> int list" would  
be perfectly safe in the example above ?
(by saying so, I am not pleading for any compiler change, I am just  
trying to organize my thoughts)



Thanks for you clear (and fast) answer.

Regards