From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by sympa.inria.fr (Postfix) with ESMTPS id 0EA8A7EDC2 for ; Tue, 20 Nov 2012 10:54:25 +0100 (CET) Received-SPF: None (mail4-smtp-sop.national.inria.fr: no sender authenticity information available from domain of gabriel.scherer@gmail.com) identity=pra; client-ip=209.85.223.182; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="gabriel.scherer@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail4-smtp-sop.national.inria.fr: domain of gabriel.scherer@gmail.com designates 209.85.223.182 as permitted sender) identity=mailfrom; client-ip=209.85.223.182; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="gabriel.scherer@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail4-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-ie0-f182.google.com) identity=helo; client-ip=209.85.223.182; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="postmaster@mail-ie0-f182.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ar4CAEFSq1DRVd+2kWdsb2JhbAA8CbATkjwIIwEBAQEJCQ0HEimCHgEBBAEnGQEbEgsBAwwGBQsDCg0hIgERAQUBChIGExKHaAEDCQYLoVWMM4FugQqFIAoZJwMKWYh1AQUMizxpEYRkA5QrgVGBHIoWgzAWKYQS X-IronPort-AV: E=Sophos;i="4.83,284,1352070000"; d="scan'208";a="163108957" Received: from mail-ie0-f182.google.com ([209.85.223.182]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 20 Nov 2012 10:54:23 +0100 Received: by mail-ie0-f182.google.com with SMTP id s9so2897552iec.27 for ; Tue, 20 Nov 2012 01:54:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=AtvJ+fdXvXPoGtdz5o9VuHbmSBTbLypvLxJwkZAKO6A=; b=q9paeL+SRWtaXrejvs+6uigFxvlZgG7NrJogdI+LNAYVt32B1S4Eq/UyA74LpuvM7J BvbJfUPO2sWg0OLouo+FvyHGPK2xEbdGsqL9DS89SreUmW2ggemjJRtcVe5x09CJMVGz 0XZ6h7M/ksaSOnUgQjc5ZvOk4q78Q7tBDzTadQvkSqTZ2ahQrekmoPArzeNcacyVX1BK MPm9NqjXazb0rnw9iXZNpYEJDSRALcDTm7P1agPI7ex1RegRGoBTO8fW6Mqrxp+Ae0gB 43u0Q4NNvrHc81wh+EHlRe/xnLNfc5gFztovdJ4gJZxKAh0KKHs2JIXBDKVGMlmYnJVE tevw== Received: by 10.50.187.197 with SMTP id fu5mr9280997igc.70.1353405263006; Tue, 20 Nov 2012 01:54:23 -0800 (PST) MIME-Version: 1.0 Received: by 10.50.184.199 with HTTP; Tue, 20 Nov 2012 01:53:42 -0800 (PST) In-Reply-To: References: <1353347369.78785.YahooMailNeo@web111510.mail.gq1.yahoo.com> <50AA7427.5080104@etorok.net> <1353349126.4420.YahooMailNeo@web111512.mail.gq1.yahoo.com> From: Gabriel Scherer Date: Tue, 20 Nov 2012 10:53:42 +0100 Message-ID: To: David House Cc: Dario Teixeira , =?ISO-8859-1?Q?T=F6r=F6k_Edwin?= , "caml-list@inria.fr" Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [Caml-list] The verdict on "%identity" On Mon, Nov 19, 2012 at 7:28 PM, David House wrote: > If you wanted to investigate this yourself, you could compile with -S > and look at the generated assembly. For such short functions, this is > generally not very hard. Indeed: cat test.ml module type SIG1 = sig type t val of_string: string -> t end module type SIG2 = sig type t external of_string: string -> t = "%identity" end module Impl1 = struct type t = string let of_string x = x end module Impl2 = struct type t = string external of_string: string -> t = "%identity" end module A: SIG1 = Impl1 module B: SIG1 = Impl2 (* module C: SIG2 = Impl1 *) module D: SIG2 = Impl2 let testA = A.of_string "foo" let testB = B.of_string "bar" let testD = D.of_string "baz" (I commented C out because it makes no sense to me, semantically, and it's rejected by the compiler.) ocamlopt -c -S test.ml less test.s The (relevant part of the) result on my machine, that correspond to compilation of testA, testB, testD: camlTest__entry: [...] movl $camlTest__3, %eax movl %eax, camlTest + 20 movl $camlTest__2, %eax movl %eax, camlTest + 24 movl $camlTest__1, %eax movl %eax, camlTest + 28 [...] All compiled in the same way. There may be a difference for calls across compilation units: in absence of the .cmx, no inlining would be performed. My guess would be that in presence of the .cmx we should get the same final result, but I must say I don't really care for performances on this front. Note that there is however an important difference with private definitions: with private, the cast from t to string is not only erased by the compiler, it is a *coercion* that can be lifted to casts to larger datatypes. You can coerce a (list t) into a (list string) and this is also a no-op in the dynamic semantics. That's much stronger than what you get from %identity. (This suggest that, with the explicit subtyping we have in OCaml, there would be a case for inter-coercible types: a way to define a t such that for example (t :> string) and (string :> t), but not (t = string). This doesn't increase the type safety of arbitrary programs but allow programmers to force abstraction-breaking to be explicit, with no performance cost in both directions.) > > On Mon, Nov 19, 2012 at 6:18 PM, Dario Teixeira wrote: >> Hi, >> >>> Wouldn't 'type t = private string' help the compiler optimize this? >> >> >> Possibly, though the semantics would change: what before was >> an abstract type is now translucent (ie, not quite transparent). >> >> Regards, >> Dario >> >> -- >> Caml-list mailing list. Subscription management and archives: >> https://sympa.inria.fr/sympa/arc/caml-list >> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners >> Bug reports: http://caml.inria.fr/bin/caml-bugs > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs