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
Why does value restriction not apply to the empty list ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-01-11 (16:32)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: [Caml-list] Why does value restriction not apply to the empty list ?
Antoine Delignat-Lavaud wrote:

> I chose to solve the problem of polymorphic references by adding value
> restriction* to my inferer, using ocaml to check my results.
> Not knowing whether the empty list should be considered a value or an
> expression, I copied Ocaml's behavior and made it a value.

Yes, the empty list is a value, like all other constants.

> As a result, my inferer gave the following expression the integer type :
> let el = [] in if hd el then 1 else hd el ;;
> which is the expected result since el has polymorphic type 'a list
> but does not look right because it is used as both a bool list and an
> int list.

It is perfectly right.  The empty list can of course be used both as a
bool list and an int list; that's exactly what parametric polymorphism
is all about.

Richard Jones wrote:

> But the same if statement within a function definition causes an error:
> # let f el =            
>   if List.length el > 0 then (List.hd el)+(int_of_string (List.hd el)) else 0;;
>                                                           ^^^^^^^^^^
> This expression has type int but is here used with type string

This is Hindley-Milner polymorphism at work: only "let"-bound
variables can have polymorphic types, while function parameters are

- Xavier Leroy