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: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Question about polymorphic variants
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.) The answer is yes, with a counter-example using
the difference in representation between normal arrays and float
arrays.
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...

Jacques Garrigue