Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: AUGER Cedric <Cedric.Auger@lri.fr>
To: "Matej Košík" <kosik@fiit.stuba.sk>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Ocaml: typing by name vs. typing by structure
Date: Mon, 17 Oct 2011 15:53:35 +0200	[thread overview]
Message-ID: <20111017155335.48595755@lri.fr> (raw)
In-Reply-To: <4E9B3D66.6080203@fiit.stuba.sk>

Le Sun, 16 Oct 2011 22:24:06 +0200,
Matej Košík <kosik@fiit.stuba.sk> a écrit :

> On 10/16/2011 09:24 PM, Gabriel Scherer wrote:
> > 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 ?
> 
> { foo : 'a } -> 'a
> 
> seems plasible
> 
> > 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.
> 

So, now, how would you type:

let f x = x.bar + x.foo

let g x = x.bar + x.foobar

?

-> typing error for g, since "x is of type {bar:int; foo:int} while it
                              expected to be of type {bar:int;
                                                      foobar:int}
-> parse all the file to find out that the whished type is:
   "{foo:int; bar:int; foobar:int}"
   and thus leading to:
   1. modularity break, since fields could be defined/used out of the
      file
   2. extra care when using fields to avoid doing stuff like:

      let f x = x.bar + x.foo
      let g x = x.bsr + x.foobar

      where bsr is a typo, leading to disjoint types/extra field
      making data inconstistent.
-> other stuff

For me, only the first is acceptable (or maybe otherstuff?),
and so you need to declare the type somewhere (and you lost your
ability to hide type definitions) or ensure that the
first function uses all its arguments.

Furthermore, it is always wishable for someone reading your code to
have some place where they can find ALL fields, to know their names,
or understand what it represents.

> This may be too troublesome to support but I guess it is plausible
> language change and it would enable additional Ocaml terseness. Let
> me clarify.
> 
> At the moment, it is not possible to do this:
> 
>   type t = {l1:int} * {l2:int};;
> 
> It is a syntax error. We are forced to do it gradually:
> 
>   type t1 = { l1 : int; }
>   type t2 = { l2 : int; }
>   type t = t1 * t
> 
> This is somewhat cumbersome.
> 
> Similarly, you cannot do this
> 
>   type t = A of {l1:int}
>          | B of {l2:float}
>          | C of {l1:int; l2:float}
> 
> Only this works:
> 
>   type t1 = { l1 : int}
>   type t2 = { l2 : float}
>   type t3 = { l1 : int; l2: float}
>   type t = A of t1
>          | B of t2
>          | C of t3
> 

In fact I guess it doesn't work due to "field collision":
you cannot use the same field name for different records.

Note that your first example is not pertinent to me, since I see no
point in naming structures which have only one field, so I would prefer

type t = {l1:int;l2:int}
or
type t = int * int

for the sum types, that means, you may have difficulties to do:

let f x = match x with
 | A x1 -> g x1
 | B x2 -> h x2
 | C x3 -> i x3

since g, h and i won't have type, as x1, x2 and x3 won't have one
either.


> We were forced to introduce three superfluous type-names [t1;t2;t3].
> How would you feel if you were forced to do that for every tuple-type?
> 
> Sometimes tuples are not ideal
> (when they have lots of members)

I would rather say
 "when they have lots of members sharing the same type"

> Records are better in those cases because you can access elements by
> their label and when you construct records with lots of fields, the
> labels clarify which field has what meaning. But switching from
> tuples to records is not smooth. What could be gained in readability
> (labeled fields) is lost by cluttering the program by auxiliary
> record type definitions.
> 
> > 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
> 
> Interesting.
> 
> > 
> > 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.
> 



      parent reply	other threads:[~2011-10-17 13:53 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
2011-10-16 20:24   ` Matej Košík
2011-10-16 20:54     ` Gabriel Scherer
2011-10-17 13:53     ` AUGER Cedric [this message]

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=20111017155335.48595755@lri.fr \
    --to=cedric.auger@lri.fr \
    --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