If record were structurally typed, there would be an ambiguity as to,
for example, what this function mean:
let access_foo t = t.foo
What would the type of `access_foo` be ? { foo : 'a } -> 'a ? { foo :
'a; bar : 'b } -> 'a ?
Should `access_foo { foo = 1 }` be typable? And `access_foo { foo = 1;
bar = true }` ?
In this situation, you want record subtyping. This get more
complicated than the relatively simple OCaml records. In fact, OCaml
has structurally typed structures with named fields : objects
# let access_foo t = t#foo;;
val access_foo : < foo : 'a; .. > -> 'a = <fun>
# access_foo (object method foo = 1 method bar = true end);;
- : int = 1
Tuples don't have this issue as they don't have an universal accessor
function : there is no way to get the first element of a tuple
whatever its width is. Therefore, all tuples manipulation make the
structure concrete, and structural subtyping comes at no complexity
cost ... if you accept the absence of universal accessors. SML has
them (#1 #2 etc.) and I'm not sure how they handle this.
On Sun, Oct 16, 2011 at 8:51 PM, Matej Kosik <[email protected]> wrote:
> Hello,
>
> When I try to compile the following program:
>
> let f1 = function
> {re=a;im=b} ->
> print_float a;
> print_float b
> in
> f1 {re=10.0;im=20.0
>
> I get an error message:
>
> File "tmp.ml", line 2, characters 2-13:
> Error: Unbound record field label re
>
> I know how to "fix" the program to be compilable---I have to add
>
> type whatever = {re:float; im:float}
>
> at the top.
> Then type inference is able to figure out that "f1" has type "whatever ->
> unit".
>
> Why did language designers chosen:
> - typing by structure in case of tuples (tuples can be viewed as records with
> unlabled fields),
> - typing by name in case of records.
>
> Why wasn't typing by structure chosen both for tuples and for records?
> If it were, then type of "f1" would be "{re=float;im=float}".
>
> Best regards.
> --
> Matej Košík
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
--
Caml-list mailing list. Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs