Re: type sharing

From: Tyng-Ruey Chuang (trc@iis.sinica.edu.tw)
Date: Sun Mar 15 1998 - 09:10:38 MET


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

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!
>
>
>
>
>



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:14 MET