Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* A second functor question
@ 2004-11-20  4:14 John F. Hughes
  2004-11-20  7:45 ` [Caml-list] " Alain Frisch
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: John F. Hughes @ 2004-11-20  4:14 UTC (permalink / raw)
  To: caml-list

I'd like to write a signature like this:

module type P = 
  sig
     type t
     val foo : t -> t
     val z:int
  end;;

And make two modules matching that signature:

module P1 : P = 
   struct
      type t = int
      let foo x:t = x
      let z = 1
   end;;

module P2 : P = 
   struct 
      type t = int
      let foo x:t = x
      let z = 2 
   end;;

I now want to apply a functor to those two modules...but a functor
wants a single module, so I make a signature for a "joined" type:

module type COMBINE =
   sig
      module A : P
      module B : P
   end;;
 with A.t = B.t;;

and create a module of that type:

module C : COMBINE = 
   struct
      module A = P1 
      module B = P2
   end;;

And now I can write a functor:

module Fun = 
   functor (Z : COMBINE) -> 
     struct
        let f x:Z.A.t = Z.B.foo x 
     end;;

This will fail because Z.B.foo expects a B.t, but is being handed an
A.t. 

I'd like it to work. In other words, I'd like a way to promise to the
type system
that A.t and B.t (within a COMBINE) are always the same. I tried

module type COMBINE =
   sig
      module A : P
      module B : P
   end with A.t = B.t

I tried telling it they were the same when I created the module C:

module C : COMBINE = 
   struct
      module A = P1 
      module B = P2
   end with A.t = B.t;;

Neither worked. Can someone suggest a way to make this work, or am I
asking too
much of the module system. (I used to be able to do this in ML, using
the 
"sharing type" construct, but...)

---John



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

* Re: [Caml-list] A second functor question
  2004-11-20  4:14 A second functor question John F. Hughes
@ 2004-11-20  7:45 ` Alain Frisch
  2004-11-20 14:19 ` Christophe TROESTLER
  2004-11-20 14:48 ` Andreas Rossberg
  2 siblings, 0 replies; 4+ messages in thread
From: Alain Frisch @ 2004-11-20  7:45 UTC (permalink / raw)
  To: John F. Hughes; +Cc: caml-list

John F. Hughes wrote:
> I'd like it to work. In other words, I'd like a way to promise to the
> type system
> that A.t and B.t (within a COMBINE) are always the same.

module type COMBINE =
    sig
       module A : P
       module B : P with type t = A.t
    end;;

You need to keep the P1.t and P2.t fields public, otherwise the 
constraint cannot be checked:

module P1 = struct ... end;;
module P2 = struct ... end;;

or:

module P1 : P with type t = int = ...
module P2 : P with type t = int = ...

Btw, maybe you did it on purpose, but the type annotation in:

         let f x:Z.A.t = Z.B.foo x

is upon the result type of f, not its argument (in your case, it's the 
same).


Note also you don't need to combine the two structures in a single 
module, you can just define a functor with several arguments:

module Fun(A : P)(B : P with type t = A.t) = struct
   let f x = B.foo x
end


-- Alain


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

* Re: [Caml-list] A second functor question
  2004-11-20  4:14 A second functor question John F. Hughes
  2004-11-20  7:45 ` [Caml-list] " Alain Frisch
@ 2004-11-20 14:19 ` Christophe TROESTLER
  2004-11-20 14:48 ` Andreas Rossberg
  2 siblings, 0 replies; 4+ messages in thread
From: Christophe TROESTLER @ 2004-11-20 14:19 UTC (permalink / raw)
  To: jfh; +Cc: caml-list

On Fri, 19 Nov 2004, "John F. Hughes" <jfh@cs.brown.edu> wrote:
> 
> I'd like to write a signature like this:
> 
> module type P = 
>   sig
>      type t
>      val foo : t -> t
>      val z:int
>   end;;
>
> I now want to apply a functor to those two modules...but a functor
> wants a single module,

Just like functions want a single argument.

> module type COMBINE =

This is unnecessary.

> I'd like a way to promise to the type system that A.t and B.t are
> always the same.

Does this fit your needs:

module F(A : P) (B : P with type t = A.t) =
struct
  let f x = A.foo(B.foo x)
  (* ... *)
end

ChriS


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

* Re: [Caml-list] A second functor question
  2004-11-20  4:14 A second functor question John F. Hughes
  2004-11-20  7:45 ` [Caml-list] " Alain Frisch
  2004-11-20 14:19 ` Christophe TROESTLER
@ 2004-11-20 14:48 ` Andreas Rossberg
  2 siblings, 0 replies; 4+ messages in thread
From: Andreas Rossberg @ 2004-11-20 14:48 UTC (permalink / raw)
  To: John F. Hughes, caml-list

John F. Hughes <jfh@cs.brown.edu> wrote:
> 
> module type COMBINE =
>    sig
>       module A : P
>       module B : P
>    end;;
>  with A.t = B.t;;
> 
> [...]
> 
> module Fun = 
>    functor (Z : COMBINE) -> 
>      struct
>         let f x:Z.A.t = Z.B.foo x 
>      end;;
> 
> This will fail because Z.B.foo expects a B.t, but is being handed an
> A.t. 

It works when you define COMBINE as

 module type COMBINE =
    sig
       module A : P
       module B : P with type t = A.t
    end

Hope this helps,

    - Andreas


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

end of thread, other threads:[~2004-11-20 20:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-20  4:14 A second functor question John F. Hughes
2004-11-20  7:45 ` [Caml-list] " Alain Frisch
2004-11-20 14:19 ` Christophe TROESTLER
2004-11-20 14:48 ` Andreas Rossberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox