From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 85523BBAF for ; Tue, 4 May 2010 15:37:23 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ag8CAOS/30vRVaA0kGdsb2JhbACDF413gVqKQQgVAQEBAQkJDAcRAx+qaDiCAIVNLohOAQEDBYEhgTSBS24E X-IronPort-AV: E=Sophos;i="4.52,327,1270418400"; d="scan'208";a="50415643" Received: from mail-pw0-f52.google.com ([209.85.160.52]) by mail2-smtp-roc.national.inria.fr with ESMTP; 04 May 2010 15:37:22 +0200 Received: by pwi1 with SMTP id 1so1722603pwi.39 for ; Tue, 04 May 2010 06:37:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=fsyzvzkhJ+cLXrXwTH/7qtyjFefbBqngbvsF+T2hgBc=; b=QmFrC8yDtA0EqPurhYrUSjLAanMrEBojVarEQh/CkjxA5cyqmf4r2BiJrAhxmre2/Q Ijt/x+chBWdOZvGgzbxFS7OVn5lQ+Wj7qxuLtuKelr8RXNL9dF36kEZHjEV7z90ZIjJW Lc8uNiKDL76tPicgWjNjDGXUi0md6pm3TZeyU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; b=BlZJ9XBe9REhQC+IEMwyvHix7jOmo6AV0FCuQfwyUHPxsvQ4nwhgWsZWngs98JNF// 4XjFxHJBqd8WsEyLMPGwD9r1574eQNAFZBgCJmzb/dSlMBlHg/DXITkBGvSU2xV4kS1e PwxJSOKGLV5hyiFGWvaGvn0odNx7sWqPUv8ZU= Received: by 10.143.27.41 with SMTP id e41mr10309615wfj.343.1272980241354; Tue, 04 May 2010 06:37:21 -0700 (PDT) Received: from [192.168.1.98] ([72.44.99.175]) by mx.google.com with ESMTPS id 22sm5362985iwn.12.2010.05.04.06.37.19 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 04 May 2010 06:37:20 -0700 (PDT) Message-ID: <4BE02308.2080902@gmail.com> Date: Tue, 04 May 2010 09:37:12 -0400 From: Edgar Friendly User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.10pre) Gecko/20100407 Shredder/3.0.5pre MIME-Version: 1.0 To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Re: Subtyping structurally-equivalent records, or something like it? References: <602616.65342.qm@web111501.mail.gq1.yahoo.com> <4429.86797211251$1272970133@news.gmane.org> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam: no; 0.00; subtyping:01 lri:01 compiler:01 compiler:01 ocamlopt:01 ocaml:01 syntax:01 subtyping:01 sig:01 val:01 val:01 struct:01 inlining:01 runtime:01 edgar:98 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 end 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. E.