Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Arnaud Spiwack <Arnaud.Spiwack@l...>
Subject: Re: [Caml-list] Why does value restriction not apply to the empty list ?

> It's a strange one ... when the if statement appears as a toplevel
> statement, OCaml infers the type 'a list for the list:
>   
It is not anyhow strange, it is how OCaml always does. It generalises 
types of variables introduced by a let (or equivalently at toplevel), 
types of other variables are not polymorphic.

In the case of list it should be fairly clear, by the way, that the 
empty list is the only one that has type 'a list for all 'a.
> # let el = [] ;;
> val el : 'a list = []
> # if List.length el > 0 then (List.hd el)+(int_of_string (List.hd el)) else 0;;
> - : int = 0
> # el ;;
> - : 'a list = []
>
> 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
>
> Rich.
>
>