Accueil     À propos     Téléchargement     Ressources     Contactez-nous

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Labels and polymorphism
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2007-03-08 (19:41) From: Roland Zumkeller Subject: Re: [Caml-list] Labels and polymorphism
```On 08/03/07, Nathaniel Gray <n8gray@gmail.com> wrote:
> # let f ~x = x;;
> val f : x:'a -> 'a = <fun>
> # f ~x:1;;
> - : int = 1
> # f 1;;
> - : x:(int -> 'a) -> 'a = <fun>

The "1" is assumed to be an argument to "f ~x:a" (where "a" is yet to
be given). Therefore "f ~x:a" must be of type "int -> ...". Now "f
~x:a" is of the same type as (and here actually reduces to)  "a".
Hence this type for the x-labeled argument.

Perhaps looking at this might help, too:
# f 1 1 1;;
- : x:(int -> int -> int -> 'a) -> 'a = <fun>

Now you ask, why are things as they are? Why can't OCaml guess that
the "1" was meant for the label "x"?

Probably it could, but with such a typing rule we would have to write
"(fun ~x:a -> f ~x:a 1)" for what is currently "f 1" (partial
application would be essentially lost here). In contrast, becoming
able to write "f x" instead of "f ~x:1" seems not enough of a gain to
compensate this.

Best,

Roland

--
http://www.lix.polytechnique.fr/~zumkeller/

```