caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Same label in different types, how do people solve this?
@ 2000-12-06 21:22 Mattias Waldau
  2000-12-07 16:49 ` John Max Skaller
                   ` (5 more replies)
  0 siblings, 6 replies; 33+ messages in thread
From: Mattias Waldau @ 2000-12-06 21:22 UTC (permalink / raw)
  To: Caml-List

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?

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.} ???

/mattias


type point_3d = {
    x:float;
    y:float;
    z:float;
  }

type point_2d = {
    x:float;
    y:float;
  }

# {x=10.;y=20.;z=30.};;
Characters 0-19:
The record field label z belongs to the type point_3d
but is here mixed with labels of type point_2d
# {x=10.;y=20.};;
- : point_2d = {x=10.000000; y=20.000000}




^ permalink raw reply	[flat|nested] 33+ messages in thread
* Re: Same label in different types, how do people solve this?
@ 2000-12-10 14:57 Ohad Rodeh
  0 siblings, 0 replies; 33+ messages in thread
From: Ohad Rodeh @ 2000-12-10 14:57 UTC (permalink / raw)
  To: caml-list

List,

> ... stuff removed ...
> 
> > 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.

The loss of the pricipal type theorem has also recently been discussed in
comp.lang.ml and comp.lang.functional. Using type-annotation by the
compiler prior (call this the PRIOR approach) to unification can help
typing expression that cannot be typed otherwise. For example, cases of
polymoriphic recursion. In fact, this is the way this is handled in
Haskell (this topic has also appeared in this list previously). 
Although I wouldn't go running to implement all my code in Haskell :-), I
do think that using PRIOR could be an improvement. 

Can someone provide an example of how principal typing is lost once
anotations are used in a non-trivial way? 

	Ohad.



^ permalink raw reply	[flat|nested] 33+ messages in thread
* RE: Same label in different types, how do people solve this?
@ 2000-12-13 13:17 Dave Berry
  2000-12-13 14:31 ` Mattias Waldau
  0 siblings, 1 reply; 33+ messages in thread
From: Dave Berry @ 2000-12-13 13:17 UTC (permalink / raw)
  To: Chris Hecker, Mattias Waldau, Caml-List

The same issue arises with variant types -- a constructor defined in one
type cannot be defined in a later type.  Do you find this to be a problem as
well?  One could make an argument that languages should be consistent in how
they treat these two cases.  OCaml was consistent until it added polymorphic
variants.  SML is not consistent, because it forbids redefinition of
constructors but permits fields to be used in different record types.
(Technically, SML record types are types in their own right, unlike CAML
record types which must be named).

Also, I'm curious as to why the module approach doesn't meet your
requirements.  Do you really need to use different types with the same label
in the same module?

Dave.


-----Original Message-----
From: Chris Hecker [mailto:checker@d6.com]
Sent: Monday, December 11, 2000 18:24
To: Mattias Waldau; Caml-List
Subject: RE: Same label in different types, how do people solve this?



>I understand that all you functional experts thinks this restriction is
>obvious, but for me it is more like a bug/misfeature. So this 'misfeature'
>should actually be stated for all us who aren't interested how types are
>infered in functional programming.

I'm with Mattias on this one.  I'm practical above theoretical.  All of the
workarounds for this problem seem like they generate way more tedious work
for the programmer, and they still don't quite accomplish the goal 100%.
This characteristic of doing more work and only asymptotically approaching
your goal is a bad taste I associate with C++.

Anyway, my "vote" would be to allow specification, with : if it's possible
since it's the obvious syntax, but even with @@ if necessary (even though I
think it's really ugly).

Chris



^ permalink raw reply	[flat|nested] 33+ messages in thread

end of thread, other threads:[~2000-12-18 14:50 UTC | newest]

Thread overview: 33+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-12-06 21:22 Same label in different types, how do people solve this? 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
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

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).