caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Type sharing...
@ 2000-11-15 13:15 Chris Quinn
  0 siblings, 0 replies; 2+ messages in thread
From: Chris Quinn @ 2000-11-15 13:15 UTC (permalink / raw)
  To: caml-list

I have a question for the implementors:

Being acquainted only with the sharing involved between a Types.class_declaration's cty_params and its cty_type (ditto type_declaration and cltype_declaration), I'd be interested to know of the other cases of sharing amongst the constituents of types.mli, if any?  

Thanks, 
Chris Quinn



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: type sharing
@ 1998-03-15  8:10 Tyng-Ruey Chuang
  0 siblings, 0 replies; 2+ messages in thread
From: Tyng-Ruey Chuang @ 1998-03-15  8:10 UTC (permalink / raw)
  To: caml-list, trc

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





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2000-11-15 21:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-11-15 13:15 Type sharing Chris Quinn
  -- strict thread matches above, loose matches on Subject: below --
1998-03-15  8:10 type sharing Tyng-Ruey Chuang

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).