English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Re: [Caml-list] Feature request : Tuples vs. records
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-02-23 (01:40)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Feature request : Tuples vs. records
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
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


Jacques Garrigue