From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Matej Kosik <kosik@fiit.stuba.sk>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Ocaml: typing by name vs. typing by structure
Date: Sun, 16 Oct 2011 21:24:44 +0200 [thread overview]
Message-ID: <CAPFanBHM=HEmbVwEDmyS=r7O2DY_dKm+kB5abAtNWBW=o8sinQ@mail.gmail.com> (raw)
In-Reply-To: <4E9B27A1.5070309@fiit.stuba.sk>
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 <kosik@fiit.stuba.sk> 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
>
>
next prev parent reply other threads:[~2011-10-16 19:25 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-10-16 18:51 Matej Kosik
2011-10-16 19:24 ` Gabriel Scherer [this message]
2011-10-16 20:24 ` Matej Košík
2011-10-16 20:54 ` Gabriel Scherer
2011-10-17 13:53 ` AUGER Cedric
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAPFanBHM=HEmbVwEDmyS=r7O2DY_dKm+kB5abAtNWBW=o8sinQ@mail.gmail.com' \
--to=gabriel.scherer@gmail.com \
--cc=caml-list@inria.fr \
--cc=kosik@fiit.stuba.sk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox