From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id A78F7BC57 for ; Thu, 5 Aug 2010 18:35:47 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AucBAF2FWkzB/BfTkWdsb2JhbACDFZ05AQEBAQkLCgcRAx+1RJFkgSaDIXMEiSw X-IronPort-AV: E=Sophos;i="4.55,323,1278280800"; d="scan'208";a="55135366" Received: from msa02.smtpout.orange.fr (HELO msa.smtpout.orange.fr) ([193.252.23.211]) by mail3-smtp-sop.national.inria.fr with ESMTP; 05 Aug 2010 18:35:47 +0200 Received: from [192.168.1.90] ([83.199.87.87]) by mwinf5c05 with ME id qgbm1e01m1t47Lr01gbmXG; Thu, 05 Aug 2010 18:35:46 +0200 Message-ID: <4C5AE867.9070908@frisch.fr> Date: Thu, 05 Aug 2010 18:35:51 +0200 From: Alain Frisch User-Agent: Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.9.2.7) Gecko/20100713 Thunderbird/3.1.1 MIME-Version: 1.0 To: Dario Teixeira Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Emulating width subtyping with 1st-class modules References: <607998.18398.qm@web111514.mail.gq1.yahoo.com> In-Reply-To: <607998.18398.qm@web111514.mail.gq1.yahoo.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; frisch:01 frisch:01 subtyping:01 subtyping:01 ocaml's:01 verbose:01 camlp:01 camlp:01 compiler:01 algebra:01 unpack:01 inference:01 unpacking:01 ocaml:01 sig:01 On 08/05/2010 05:02 PM, Dario Teixeira wrote: > I have a problem where some form of width subtyping for records would be > useful. At the present I'm taking advantage of the structural subtyping nature > of Ocaml's object system to emulate the width subtyping. This works and is > reasonably compact, but I'm still open to other approaches. It has occurred > to me that 3.12's modules-as-first-class-values provide yet another solution: >... > While this approach seems awfully verbose, I reckon it could be made much > more palatable via some Camlp4 sugaring. Nevertheless, I have a question: > just how heavy would this approach be when compared to the object one? > And how would it fare in comparison to regular records? It depends on what you call "heavy". For the syntactic aspects, as you say, Camlp4 can be helpful. One could also imagine future extensions in the compiler itself, e.g.: - Extending the pattern algebra to directly unpack modules (allowing to write: "let print_brief (module M : BRIEF) = ....") - Adding more type inference, to avoid some of the verbosity of first-class module. Jacques Garrigue had an experimental extension to allow implicit unpacking (when the context gives enough information to infer the module type). See the implicit-unpack branch in the OCaml Subversion repository. Compared to object types, modules have some extra flexibility thanks to the "include" statement (you cannot "inherit" fields from an existing object, only from a class). For instance, you can write a function to merge two modules without having to enumerate all their fields: module type S = sig val x: int (* ... *) end module type T = sig val y: int (* ... *) end module type S_T = sig include S include T end let merge s t = (module struct include (val s : S) include (val t : T) end : S_T) Similar to object types, you cannot use pattern matching to deconstruct modules and filter on their fields. Concerning performance, modules imply some copying to enable width subtyping used (in order to forget extra fields), which is not the case for objects. The runtime representation for modules is the same as for records; field access and value construction are very cheap. Alain