Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Record field label locality
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-08-14 (09:03)
From: Brighten Godfrey <pbg@c...>
Subject: Re: [Caml-list] Record field label locality
On Aug 13, 2008, at 5:48 AM, Edgar Friendly wrote:

> Brighten Godfrey wrote:
>> Two things come to mind:
>> (1) The type of get_f1 is handled analogously to the way it is  
>> handled
>> for objects, something like this:
>>     val get_f1 : < x : 'a; .. > -> 'a = <fun>
>> I'm guessing that if you did this, you would have to "instantiate"
>> `get_f1' each time it is applied to a new record type, which I  
>> assume is
>> inconvenient (or not?).
> Yes - this breaks separate compilation.

Makes sense.

>> (2) Require that all record field accesses refer to a globally-unique
>> record type, making conversion to a record field index is easy.   
>> So the
>> example code Edgar gave would result in a compilation error  
>> because the
>> compiler cannot determine which `.f1' field the access refers to.   
>> But
>> consider this code:
>>     let return_garlic () =
>>         let x = {M2.f2=5; M2.f1="garlic"} in
>>         x.f1
>> In line 2, globally unique record field names are given, which allows
>> the compiler to tag variable `x' with type `M2.t2'.  Then in line  
>> 3, the
>> record field access `x.f1' can only mean `x.M2.f1'.
> In this situation, the type information influences what code is
> generated.  The OCaml developers have been as careful as possible to
> avoid this.  The typing stage of compilation acts as a filter to
> eliminate incorrect programs, and that's it.
> I've had my share of ideas of how typing information could usefully
> connect with code generation, but have been shut down because the  
> ocaml
> developers (probably rightly) don't want to bridge this separation.
> (Probably because the quality of the compiler would go down the tubes
> right quick.)  Although...  hmmm..  I guess type information is  
> used for
>  some optimization (specializing = for ints and such).

This is a good point.  Thanks for the explanation.  I'm having a hard  
time thinking of any case other than =,<,> etc where type information  
would be necessary to determine code generation.  On the other hand  
if you break the separation for those operators, maybe it's OK to  
break it for record names as well.

> Also you lose the compositionality as before - you can't break this
> function into two parts because the second line "needs" the first line
> to work.

It can still work, for example this would work:

     let garlic_part_1 () = {M2.f2=5; M2.f1="garlic"}
     let garlic_part_2 x = x.f1

     let return_garlic () =
	garlic_part_2 (garlic_part_1 ())

Using this notation the programmer could of course choose to always  
specify the full record name (x.M2.f1) if desired.