From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA08707 for caml-redistribution; Mon, 16 Mar 1998 16:04:08 +0100 (MET) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id JAA15629 for ; Sun, 15 Mar 1998 09:19:21 +0100 (MET) Received: from iis.sinica.edu.tw (iiserv.iis.sinica.edu.tw [140.109.20.250]) by nez-perce.inria.fr (8.8.7/8.8.5) with SMTP id JAA08669 for ; Sun, 15 Mar 1998 09:11:29 +0100 (MET) Received: from ccs1.iis.sinica.edu.tw by iis.sinica.edu.tw (4.1/IIS-1.0) id AA10008; Sun, 15 Mar 98 16:10:30 CST Received: from iota.iis.sinica.edu.tw by ccs1.iis.sinica.edu.tw (SMI-8.6/SMI-SVR4) id QAA20566; Sun, 15 Mar 1998 16:09:39 +0800 Received: by iota.iis.sinica.edu.tw (SMI-8.6/SMI-SVR4) id QAA05077; Sun, 15 Mar 1998 16:10:38 +0800 Date: Sun, 15 Mar 1998 16:10:38 +0800 From: trc@iis.sinica.edu.tw (Tyng-Ruey Chuang) Message-Id: <199803150810.QAA05077@iota.iis.sinica.edu.tw> To: caml-list@inria.fr, trc@iis.sinica.edu.tw Subject: Re: type sharing Sender: weis I find a get-around for the problem I posed eariler. I wonder if there are other ways to get around it. best wishes, Tyng-Ruey ----------- Now the last line of the following ocaml code will type-check. But if you remove the "with module Quad = B.Quad" in the specification of functor Dtree, it won't. That is exactly what I want. ===== start Ocaml code ===== module type QUAD = sig type ('a, 'b) t = AA of 'a * 'a | AB of 'a * 'b | BA of 'b * 'a | BB of 'b * 'b end module Quad: QUAD = struct type ('a, 'b) t = AA of 'a * 'a | AB of 'a * 'b | BA of 'b * 'a | BB of 'b * 'b end module type BTREE = sig module Quad: QUAD type ('a, 'b) quad = ('a, 'b) Quad.t end module Btree(Q: QUAD): BTREE = struct module Quad = Q type ('a, 'b) quad = ('a, 'b) Quad.t end module Dtree(B: BTREE): BTREE with module Quad = B.Quad = struct module Quad = B.Quad type ('a, 'b) quad = ('a, 'b) B.quad end module B = Btree(Quad) module D = Dtree(B) let b = B.Quad.AA (1, 1) let d = D.Quad.AA (1, 1) let u = b = d ==== end Ocaml code === > From trc@ccs1 Sun Mar 15 14:33:05 1998 > To: caml-list@inria.fr > Cc: trc@ccs1 > > Hello, > > I am trying to translate some SML modules with type sharing constraints > into corresponding Ocaml modules but without success. > I wonder if there is a good fix. Thanks in advance. > > best wishes, > > Tyng-Ruey Chuang > > -------------- > > Here is the detailed description. > > In SML, one can do the following: > > ===== start SML code ===== > signature BTREE > = > sig > datatype ('a, 'b) quad = AA of 'a * 'a > | AB of 'a * 'b > | BA of 'b * 'a > | BB of 'b * 'b > end > > > structure Btree:> BTREE > = > struct > datatype ('a, 'b) quad = AA of 'a * 'a > | AB of 'a * 'b > | BA of 'b * 'a > | BB of 'b * 'b > end > > > functor Dtree(B: BTREE):> BTREE where type ('a, 'b) quad = ('a, 'b) B.quad > = > struct > open B > end > > structure B = Btree > structure D = Dtree(B) > > val u = B.AA (1, 1) = D.AA (1, 1) > ===== end SML code ===== > > The last line of code will type-check. If the "where ..." clause > in the specification of functor Dtree is deleted, then the same line > will not type-check. I think this is quite nice as it allows one to > control the exposure of type sharing information among the modules. > I intend to keep this mechanism in my Ocaml translation. > > The direct translation of the above SML code to Ocaml is: > > ==== start Ocaml code ===== > module type BTREE > = > sig > type ('a, 'b) quad = AA of 'a * 'a > | AB of 'a * 'b > | BA of 'b * 'a > | BB of 'b * 'b > end > > > module Btree: BTREE > = > struct > type ('a, 'b) quad = AA of 'a * 'a > | AB of 'a * 'b > | BA of 'b * 'a > | BB of 'b * 'b > end > > > module Dtree(B: BTREE): BTREE with type ('a, 'b) quad = ('a, 'b) B.quad > = > struct > open B > end > > module B = Btree > module D = DistBtree(B) > > let b = B.AA (1, 1) > let d = D.AA (1, 1) > let u = b = d > ==== end Ocaml code ==== > > This will not work because "open" has a different semantics in Ocaml > than in SML. The best I can do so far is the following (where > type quad in module type BTREE becomes abstract): > > ==== start Ocaml code ==== > module type BTREE > = > sig > type ('a, 'b) quad > > val aa: 'a * 'a -> ('a, 'b) quad > end > > > module Btree: BTREE > = > struct > type ('a, 'b) quad = AA of 'a * 'a > | AB of 'a * 'b > | BA of 'b * 'a > | BB of 'b * 'b > let aa (u, v) = AA (u, v) > end > > > module Dtree(B: BTREE): BTREE with type ('a, 'b) quad = ('a, 'b) B.quad > = > struct > type ('a, 'b) quad = ('a, 'b) B.quad > > let aa (u, v) = B.aa (u, v) > end > > > module B = Btree > module D = Dtree(B) > > let b = B.aa (1, 1) > let d = D.aa (1, 1) > let v = b = d > ===== end Ocaml code ==== > > But now I lose the nice constructors AA, etc. (I can no longer > pattern-match them.) > > Is there an easy fix to this problem? Many thanks! > > > > >