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
Re: [Caml-list] Subtyping structurally-equivalent records, or something like it?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-05-04 (13:37)
From: Edgar Friendly <thelema314@g...>
Subject: Re: [Caml-list] Re: Subtyping structurally-equivalent records, or something like it?
On 05/04/2010 07:53 AM, Sylvain Le Gall wrote:
> On 04-05-2010, AUGER Cédric<>  wrote:
>> type momentum = Moment of kinematic
>> That is does the constructor introduce an overhead or not?
>> As there is only one constructor, no overhead should be done in an
>> optimized compiler.
> This is not about optimized compiler in this case but about data
> representation. Even if you use an optimized compiler (which is not
> really the case with ocamlopt), you won't change datastructure
> representation to optimize.
The OCaml compiler *could* special-case this kind of constructor, but as 
there's the syntax:

type momentum = kinematic

Which produces the non-boxed kinematic values, the authors probably 
decided to follow the maxim "Do what the programmer says" for singleton 
variant types.  The question becomes whether phantom types solve this 
problem sufficiently or do we need another type-level construct - 
explicit subtyping relationships.  Forever ago I suggested this to 
achieve a similar goal, and was given yet another solution:

module M : sig
	type momentum
	val of_kin : kinematic -> momentum
	val to_kin : momentum -> kinematic
end = struct
	type momentum = kinematic
	let of_kin x = x
	let to_kin x = x

Yes, it's a lot of boilerplate for each type, but you only have to write 
it once (per type), and cross-module inlining should give zero runtime 
cost.  If not, use "%identity", and expose it in the interface.  This 
method is along the lines of Anthony's proposal #4.