caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: trc@iis.sinica.edu.tw (Tyng-Ruey Chuang)
To: caml-list@inria.fr, trc@iis.sinica.edu.tw
Subject: Re: type sharing
Date: Sun, 15 Mar 1998 16:10:38 +0800	[thread overview]
Message-ID: <199803150810.QAA05077@iota.iis.sinica.edu.tw> (raw)

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





             reply	other threads:[~1998-03-16 15:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-03-15  8:10 Tyng-Ruey Chuang [this message]
2000-11-15 13:15 Type sharing Chris Quinn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=199803150810.QAA05077@iota.iis.sinica.edu.tw \
    --to=trc@iis.sinica.edu.tw \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).