Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* RE: anonymous record types in variants
@ 1999-02-22 16:37 Manuel Fahndrich
  1999-02-22 17:56 ` Pierre Weis
  0 siblings, 1 reply; 14+ messages in thread
From: Manuel Fahndrich @ 1999-02-22 16:37 UTC (permalink / raw)
  To: caml-list


I don't agree with Anton. The reason I want variants with anonymous record
arguments is to name the fields explicitly. I don't want to incur a runtime
cost of an extra indirection. The compilation of named fields vs. tuples
would be the same. That's why a construction like 

>         match x with A r -> ... r.x ... r.y ...

would not be desirable, since it requires the record r to be stored as  a
separate block from the A r value. If you restrict record access for these
anonymous records as Xavier pointed out

>         match x with A{lbl1 = x; lbl2 = y} -> ...


then you can implement them as efficiently as a variant with a tuple
argument.

-Manuel

-----Original Message-----
From: Anton Moscal [mailto:msk@post.tepkom.ru]

> It could be implemented this way.  However, if you declare the
> datatype as
>
>         type foo = A of {lbl1 : int; lbl2 : int}
>
> you would be forced to pattern-match it as follows
>
>         match x with A{lbl1 = x; lbl2 = y} -> ...
>
> but you can't get access to the record itself and use the dot notation
> on it, as in
>
>         match x with A r -> ... r.x ... r.y ...

But why? Natural semantic for anonymous types is the following: each
anonymous record or algebraic types declaration introduces new type
declaration in the current scope with some temporary type name. I.e.

        type ('a, 'b) foo = A of 'a * {l : 'b; l2: int}

is a shortcut for:

        type ('a, 'b) temp_name = {l1 : 'b; l2: int}
        and  ('a, 'b) foo = A of 'a * ('a, 'b) temp_name

Regards,
Anton Moscal




^ permalink raw reply	[flat|nested] 14+ messages in thread
* RE: anonymous record types in variants
@ 1999-02-18 14:03 Don Syme
  0 siblings, 0 replies; 14+ messages in thread
From: Don Syme @ 1999-02-18 14:03 UTC (permalink / raw)
  To: 'Xavier Leroy', Christopher Jeris, caml-list


> >   type foo_one = {one: int}
> >   type foo_two = {two: string}
> >   type foo = One of foo_one | Two of foo_two
> > 
> > But, just out of curiosity, is there a quick explanation of 
> why it is this
> > way?
> 
> Basically, because "{one : int}" is not a type expression, and the
> argument of a constructor must be a type expression.
> 
> The reason why "{one : int}" is not a type expression but must be
> declared and bound to a type name have to do with type inference
> and the existence of principal types.  If you allow record types in
> type expressions (as in SML), some functions have no principal type,
> such as fun x -> x.l.

I think all we're thinking of is a mechanism so the user doesn't have to
write
nonsense type names such as foo_one and foo_two as in the above example.  
Within a (mutually recursive) type declaration, shouldn't it
be feasible to use tuples, records and variants freely, as long as all the
names 
of all the fields and constructors are used at only one place in the
declaration?
Of course you would always need named types for recursive type constructors.

Cheers & thanks,
Don

------------------------------------------------------------------------
At the lab:                                     At home:
Microsoft Research Cambridge                    11 John St
St George House                                 CB1 1DT
Cambridge, CB2 3NH, UK
Ph: +44 (0) 1223 744797                         Ph: +44 (0) 1223 722244
http://research.microsoft.com/users/dsyme
email: dsyme@microsoft.com
   "You've been chosen as an extra in the movie
        adaptation of the sequel to your life"  -- Pavement, Shady Lane
------------------------------------------------------------------------




^ permalink raw reply	[flat|nested] 14+ messages in thread
* Re: anonymous record types in variants
@ 1999-02-18 10:13 Frank A. Christoph
  0 siblings, 0 replies; 14+ messages in thread
From: Frank A. Christoph @ 1999-02-18 10:13 UTC (permalink / raw)
  To: caml-list

Xavier Leroy wrote:
>> Probably an optimal solution would
>> be to allow anonymous types in any context.
>
>You can kiss type inference goodbye, then.
>
>Don Syme wrote:
>
>> BTW, the type language of Mercury (and probably some other FP languages?)
>> allows variants and records pretty much anywhere in a type structure. 
>
>What kind of type inference do they have?

The undecidable kind. :/

--FC



^ permalink raw reply	[flat|nested] 14+ messages in thread
* RE: anonymous record types in variants
@ 1999-02-16 17:40 Don Syme
  0 siblings, 0 replies; 14+ messages in thread
From: Don Syme @ 1999-02-16 17:40 UTC (permalink / raw)
  To: 'Anton Moscal', Christopher Jeris; +Cc: caml-list


Again, I've also needed to factorize the common fields of variants as in
Anton's example.

BTW, the type language of Mercury (and probably some other FP languages?)
allows variants and records pretty much anywhere in a type structure. 

Don

> 
> On Fri, 12 Feb 1999, Christopher Jeris wrote:
> 
> > The argument of a variant type constructor cannot be an 
> anonymous record
> > type, i.e.:
> > 
> >   type foo = One of {one: int} | Two of {two: string}
> > 
> 
> I agree. I often need the following usage of anonymous types:
> 
> type text = int(*length*) * (File of string | Str of string | ...)
> 
> (factorizing of the common fields in the variant types).
> 
> SML allow this construction. Probably an optimal solution would
> be to allow anonymous types in any context.
> 
> Regards,
> Anton Moscal
> 




^ permalink raw reply	[flat|nested] 14+ messages in thread
* RE: anonymous record types in variants
@ 1999-02-15 18:03 Manuel Fahndrich
  0 siblings, 0 replies; 14+ messages in thread
From: Manuel Fahndrich @ 1999-02-15 18:03 UTC (permalink / raw)
  To: caml-list


I also find this the major annoying feature of CAML records. I frequently
use variant datatypes with a number of fields. Without records, the field
order matters and has to be remembered when creating and accessing such
variants. Anonymous records would be ideal on variants, since they would not
introduce the extra level of indirection that you get when you declare an
explicit record type.

-Manuel 

-----Original Message-----
From: Don Syme [mailto:dsyme@microsoft.com]
Sent: Monday, February 15, 1999 2:30 AM
To: 'Christopher Jeris'; caml-list@inria.fr
Subject: RE: anonymous record types in variants



FTR, I've also had a few situations where it seemed most natural to write
similar constructs, but I couldn't.

Cheers,
Don
> 
> The argument of a variant type constructor cannot be an 
> anonymous record
> type, i.e.:
> 
>   type foo = One of {one: int} | Two of {two: string}
> 
> is rejected at the first {.  Of course this is easy to work 
> around, just
> give the record types names:
> 
>   type foo_one = {one: int}
>   type foo_two = {two: string}
>   type foo = One of foo_one | Two of foo_two
> 
> But, just out of curiosity, is there a quick explanation of 
> why it is this
> way?
> 
> thanks & peace,
> 
> Chris Jeris	MIT math grad student and novice OCaml music programmer
> 




^ permalink raw reply	[flat|nested] 14+ messages in thread
* RE: anonymous record types in variants
@ 1999-02-15 10:30 Don Syme
  0 siblings, 0 replies; 14+ messages in thread
From: Don Syme @ 1999-02-15 10:30 UTC (permalink / raw)
  To: 'Christopher Jeris', caml-list


FTR, I've also had a few situations where it seemed most natural to write
similar constructs, but I couldn't.

Cheers,
Don
> 
> The argument of a variant type constructor cannot be an 
> anonymous record
> type, i.e.:
> 
>   type foo = One of {one: int} | Two of {two: string}
> 
> is rejected at the first {.  Of course this is easy to work 
> around, just
> give the record types names:
> 
>   type foo_one = {one: int}
>   type foo_two = {two: string}
>   type foo = One of foo_one | Two of foo_two
> 
> But, just out of curiosity, is there a quick explanation of 
> why it is this
> way?
> 
> thanks & peace,
> 
> Chris Jeris	MIT math grad student and novice OCaml music programmer
> 




^ permalink raw reply	[flat|nested] 14+ messages in thread
* anonymous record types in variants
@ 1999-02-12 20:53 Christopher Jeris
  1999-02-16 10:57 ` Anton Moscal
  1999-02-17  9:32 ` Xavier Leroy
  0 siblings, 2 replies; 14+ messages in thread
From: Christopher Jeris @ 1999-02-12 20:53 UTC (permalink / raw)
  To: caml-list


(Sorry only in English.)

The argument of a variant type constructor cannot be an anonymous record
type, i.e.:

  type foo = One of {one: int} | Two of {two: string}

is rejected at the first {.  Of course this is easy to work around, just
give the record types names:

  type foo_one = {one: int}
  type foo_two = {two: string}
  type foo = One of foo_one | Two of foo_two

But, just out of curiosity, is there a quick explanation of why it is this
way?

thanks & peace,

Chris Jeris	MIT math grad student and novice OCaml music programmer




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

end of thread, other threads:[~1999-02-22 17:56 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-02-22 16:37 anonymous record types in variants Manuel Fahndrich
1999-02-22 17:56 ` Pierre Weis
  -- strict thread matches above, loose matches on Subject: below --
1999-02-18 14:03 Don Syme
1999-02-18 10:13 Frank A. Christoph
1999-02-16 17:40 Don Syme
1999-02-15 18:03 Manuel Fahndrich
1999-02-15 10:30 Don Syme
1999-02-12 20:53 Christopher Jeris
1999-02-16 10:57 ` Anton Moscal
1999-02-17  9:32 ` Xavier Leroy
1999-02-17 18:09   ` Christopher Jeris
1999-02-17 19:14     ` Didier Remy
1999-02-22  8:44   ` Anton Moscal
1999-02-22 13:00     ` Pierre Weis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox