caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: David.Teller@ens-lyon.org
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Feature request : Tuples vs. records
Date: Fri, 23 Feb 2007 10:39:45 +0900 (JST)	[thread overview]
Message-ID: <20070223.103945.99613677.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <45DDC1CB.2090401@ens-lyon.org>

From: David Teller <David.Teller@ens-lyon.org>
> Frederic GAVA a écrit :
> > Another difference is that (If I remember) record of float are
> > unboxed and not tuple of float. But perhaps it could be done.
> >   
> I assume that's not a real difficulty. I might be wrong.

Not theoretically difficult, since it is already done for float
arrays, but there could be a severe performance hit: since
a tuple can be created inside a polymorphic function, one would need
to check all components to see whether they are floats or not.
(The situation is better with float arrays: one only needs to check
the first component, since all the others are bound to have the same
type.)
So I would say that unboxing tuples of floats would not be worth it.

More generally, one has much more freedom for using clever
representations when using nominal (declared) types than when using
structural ones. This also includes the use of embedded mutable
fields, for instance.

Another advantage of nominal typing is that the intended types become
explicit. This way you can immedietely see how a value is supposed to
be used. This also gets you early error message for missing fields when
you change a type definition.

So nominal typing has some advantages both in terms of efficiency and
detection of errors, the second advantage being particularly helpful
for beginners.

On the other hand structural typing avoids extra definitions, and
allows more code sharing and polymorphism. One cannot imagine ML
without structural tuples. In ocaml, you have also objects that can
mimic records, and polymorphic variants for sum types.

> I'm more concerned about having to
> * declare every record type I use -- that looks to me clumsy and Java-like

See above for one rationale.

> * differenciate between records and tuples during pattern-matching

Hard to avoid when one type is structural and the other nominal.

> * having to learn/teach two different implementations of what is 
> essentially the same concept

Again, they have both the same set-theoretic interpretation, but the
distinction nominal/structural is fundamental at the type level.
I agree with you that this will be hard to explain this to beginners.

> * having to learn/teach that third meaning of operator = (the first one 
> being comparison between values and the second one being its use in let 
> x = ...).

Ah, syntax...
Unfortunately, it is as it is, and this from the beginning of ML.
Even SML shares the same syntax.

If you want a far fetched a posteriori explanation: a record is
basically the same thing as a first class module with only value
components. Field access uses the same syntax. So you can see
  {x = 3; y = 5}
as a shorthand for 
  struct let x = 3 let y = 5 end
See the "=" in both?
Of course this does not explain other differences between records and
modules...

Cheers,

Jacques Garrigue


  reply	other threads:[~2007-02-23  1:40 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-22 15:34 Frederic GAVA
2007-02-22 16:16 ` David Teller
2007-02-23  1:39   ` Jacques Garrigue [this message]
2007-02-23 13:34     ` Richard Jones
2007-02-23 13:43       ` Till Varoquaux
2007-02-23 14:14         ` Nicolas Pouillard
2007-02-23  1:45   ` Jon Harrop
2007-02-23 16:32     ` Lukasz Stafiniak
2007-02-24 13:43       ` Lukasz Stafiniak
2007-02-24 15:50         ` Brian Hurt
2007-02-24 18:14           ` skaller
  -- strict thread matches above, loose matches on Subject: below --
2007-02-22 10:25 David Teller
2007-02-22 10:42 ` [Caml-list] " Andrej Bauer
2007-02-22 12:41   ` skaller
2007-02-22 13:55     ` David Teller
2007-02-22 15:44       ` Jon Harrop
2007-02-22 19:45       ` Tom
2007-02-22 23:26         ` skaller
2007-02-22 15:28     ` Andreas Rossberg
2007-02-22 15:57       ` Till Varoquaux
     [not found]         ` <45DDC424.2020804@ens-lyon.org>
2007-02-22 16:57           ` Till Varoquaux
2007-02-22 17:19             ` brogoff

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=20070223.103945.99613677.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=David.Teller@ens-lyon.org \
    --cc=caml-list@yquem.inria.fr \
    /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;
as well as URLs for NNTP newsgroup(s).