caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: mattias.waldau@abc.se
Cc: caml-list@inria.fr
Subject: Re: Same label in different types, how do people solve this?
Date: Fri, 08 Dec 2000 10:22:29 +0900	[thread overview]
Message-ID: <20001208102229N.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <HDEEKOMJILGEIHIMAPCDMEJMDLAA.mattias.waldau@abc.se>

From: "Mattias Waldau" <mattias.waldau@abc.se>

> In Ocaml, you cannot have the same label in different types, see the example
> below where
> point_2d hides point_3d.
> 
> How do people normally code around this restriction? One solution is using
> objects, but what other solutions are there? Can 'Polymorphic variants'
> solve this?

There are various ways. The most common is to differentiate label
names (i.e. use x_3d and x_2d instead of x). Another more subtle one
is to put the definitions in deifferent modules (Point2d and Point3d)
so that you can specify exactly which label you want (Point2d.x or
Point3d.x).

The last solution might be nicer if we had a
    let open Module in
construct (would require 10 lines in the compiler)

Polymorphic variants cannot help, but if you do not care about
efficiency you can use objects:

class point_3d ~x ~y ~z = object
  method x : float = x
  method y : float = y
  method z : float = z
end

class point_2d ~x ~y = object
  method x : float = x
  method y : float = y
end

Note that objects provide you more than overloading: you can define
truly polymorphic functions, working on both point_3d and point_2d,
and you can even coerce a point_3d to a point_2d.

> Also, I am a bit curious why it doesn't help to type explicitely, i.e. to
> write
> let x:point_3d={x=10.;y=20.;z=30.} ???

Because this is not the way it works. Labels are defined as are values
or constructors, this is only afterwards that the compiler checks that
they all belong to the same type. That is, the type cannot influence the
choice between x(point_3d) and x(point_2d), only the order of
definition can.

There has been already various proposals for allowing types to be taken
into account when typing record labels. The difficulty is that as soon
as you make it into something useful, you loose the principal type
property, and that some theoreticians working on ocaml wouldn't like
it.

A very restrictive approach would still work: use type annotations,
but only when they are directly on the creation/access/modification
construct.

That is
     ({x=1.;y=1.;z=1.}:point_3d)
     let p : point_3d = {x=1.;y=1.;z=1.}
     (p : point_3d).x
     {p : point_3d with x = 2.}

Here, there is no ambiguity at all. But this is very weak:

     let p : point_3d = {x=1.;y=1.;z=1.} in p.x +. p.y +. p.z

would still be illegal...

Jacques
---------------------------------------------------------------------------
Jacques Garrigue      Kyoto University     garrigue at kurims.kyoto-u.ac.jp
		<A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>



  parent reply	other threads:[~2000-12-08  9:15 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-12-06 21:22 Mattias Waldau
2000-12-07 16:49 ` John Max Skaller
2000-12-07 18:34 ` Maxence Guesdon
2000-12-07 23:02 ` Gerd Stolpmann
2000-12-08  1:22 ` Jacques Garrigue [this message]
2000-12-08  9:31   ` Sven LUTHER
2000-12-08  9:36     ` Pierre Weis
2000-12-08  9:48       ` Sven LUTHER
2000-12-08 18:41       ` John Max Skaller
2000-12-08  9:40     ` Nicolas barnier
2000-12-08 16:36 ` Brian Rogoff
2000-12-11 17:19   ` Pierre Weis
2000-12-10 12:49 ` Mattias Waldau
2000-12-11 18:23   ` Chris Hecker
2000-12-11 19:17     ` Pierre Weis
2000-12-12 10:02       ` Sven LUTHER
2000-12-12  3:25     ` Chet Murthy
2000-12-12 17:43       ` John Max Skaller
2000-12-12 19:24         ` Functions must be explicitly typed, (was Same label in different types, how do people solve this?) Mattias Waldau
2000-12-13  0:51           ` John Max Skaller
2000-12-15 10:13             ` Andreas Rossberg
2000-12-15 12:50             ` Frank Atanassow
2000-12-14 18:42           ` Stefan Monnier
2000-12-15 12:47             ` Pierre Weis
2000-12-15 13:39               ` Mattias Waldau
2000-12-15 23:37                 ` Brian Rogoff
2000-12-16 14:10                 ` ROverloading John Max Skaller
2000-12-15 21:51         ` Same label in different types, how do people solve this? Bruce Hoult
2000-12-12 17:19   ` John Max Skaller
2000-12-10 14:57 Ohad Rodeh
2000-12-13 13:17 Dave Berry
2000-12-13 14:31 ` Mattias Waldau
2000-12-15 10:01   ` John Max Skaller

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=20001208102229N.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=mattias.waldau@abc.se \
    /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).