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 (02:28) From: Karl Zilles Subject: Re: [Caml-list] Another polymorphism puzzle
```Yaron Minsky wrote:
> The last example I posted about had to do with variant types.  Here's
> another interseting little typing surprise having to do with record
> types:
>
> # type 'a t = { foo: 'a };;
> type 'a t = { foo : 'a; }
> # let f x = { x with foo = 3 };;
> val f : 'a -> int t = <fun>
>
> I can kind of see how this happened, since after all, the result of (f
> x) does not depend in any way on x.  And yet, it seems like the
> unification rules should force f to have time ('a t -> int t).

Wow.  This is pretty cool.  Also

# type 'a t = { foo: 'a; bar: int };;
type 'a t = { foo : 'a; bar : int; }

# let f x = { x with foo = 3 };;
val f : 'a t -> int t = <fun>
(now the domain is limited to 'a t)

# f { foo= "blah"; bar=100 };;
- : int t' = {foo = 3; bar = 100}

```