From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id oBSExkTX024279 for ; Tue, 28 Dec 2010 15:59:46 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqwCAAOKGU3UGyoDkWdsb2JhbACkMBUBAQEBCQsKBxEDIb8ehUoEiwSDHRo X-IronPort-AV: E=Sophos;i="4.60,239,1291590000"; d="scan'208";a="83531737" Received: from smtp3-g21.free.fr ([212.27.42.3]) by mail4-smtp-sop.national.inria.fr with ESMTP; 28 Dec 2010 15:59:40 +0100 Received: from [192.168.0.11] (unknown [78.192.0.38]) by smtp3-g21.free.fr (Postfix) with ESMTP id BC5E5A618D; Tue, 28 Dec 2010 15:59:33 +0100 (CET) Message-ID: <4D19FB54.3000607@frisch.fr> Date: Tue, 28 Dec 2010 15:59:32 +0100 From: Alain Frisch User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101207 Lightning/1.0b2 Thunderbird/3.1.7 MIME-Version: 1.0 To: Lauri Alanko CC: caml-list@inria.fr References: <20101228140247.GB12021@melkinpaasi.cs.helsinki.fi> In-Reply-To: <20101228140247.GB12021@melkinpaasi.cs.helsinki.fi> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [Caml-list] Avoiding ml/mli code duplication On 12/28/2010 3:02 PM, Lauri Alanko wrote: > In this situation, I'm not quite sure what's the best way to > proceed. Here is another possibility: =================================================================== (** BarDefs.ml **) module type S = sig type t type u = A of t end (** Bar.mli **) include BarDefs.S val x: u list (** Bar.ml **) type t = X module rec S_impl : BarDefs.S with type t := t = S_impl include S_impl let x = [A X] =================================================================== I can also mention a local extension we use at LexiFi: a compilation unit (.ml) for which an explicit signature (.mli) exists is type-checked as a recursive module with itself. (Currently, we disallow recursive references to value-like components.) So in the example above, the definition of the module type S could be put in Bar.mli: =================================================================== (** Bar.mli **) module type S = sig type t type u = A of t end include S val x: u list (** Bar.ml **) module type S = Bar.S type t = X module rec S_impl : S with type t := t = S_impl include S_impl let x = [A X] =================================================================== The same extension allows for easy recursion between, say, type and class type definitions: =================================================================== (** Bar.mli **) type t = A of Bar.u class type u = object method foo: t end ..... (** Bar.ml **) type t = A of Bar.u class type u = Bar.u ...... =================================================================== What could be useful would be an extra extension to have an "include" statement for specific type declarations, so that the Bar.ml above could be written directly: type t = include Bar.t (* same as type t = Bar.t = A of Bar.u *) class type u = Bar.u ..... -- Alain