This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Another polymorphism puzzle
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2005-04-01 (04:31) From: Jacques Garrigue Subject: Re: [Caml-list] Another polymorphism puzzle
```From: Manos Renieris <er@cs.brown.edu>
> Worse:
>
> # let f x = { () with foo = 3};;
> val f : 'a -> int t = <fun>
> # let f x = { 42 with foo = 3};;
> val f : 'a -> int t = <fun>
>
> I think this qualifies as a bug. Especially since the documentation
> says the expr on the left hand side of with has to be a record.

What do you mean by worse? You just exhibit an immediate consequence
of the typing of f below.

> Thu, Mar 31, 2005 at 09:02:14PM -0500, Yaron Minsky wrote:
> > # type 'a t = { foo: 'a };;
> > type 'a t = { foo : 'a; }
> > # let f x = { x with foo = 3 };;
> > val f : 'a -> int t = <fun>

Now, why does it happen?
The reason is simply that the typing of {x with fields} follows
closely its semantics:
build a record from the given fields, extracting values from x
when a field is missing.
Consequence: if you provide all the fields, then there is no missing
field, so x can be completely ignored.
Following the manual, the typing could be more restrictive, but the
current typing is perfectly sound.

Jacques Garrigue

```