caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Problem with Functors and Module Types
@ 2008-08-01 11:10 Christian Sternagel
  2008-08-01 11:58 ` [Caml-list] " Julien Signoles
  0 siblings, 1 reply; 2+ messages in thread
From: Christian Sternagel @ 2008-08-01 11:10 UTC (permalink / raw)
  To: caml-list

Hello,

once again I have some problems using functors with module types. I 
produced following (almost?) minimal example:

     module type MT_M = sig
      type s
      type t = A of s | B of t list
      val f : t -> t
     end

     module type MT_N = sig
      module M : MT_M
      val f : 'a -> M.t
     end

     module type MT_A = sig
      type t
     end

     module MakeM (A : MT_A) : MT_M with type s = A.t = struct
      type s = A.t
      type t = A of s | B of t list
      let f x = x
     end

     module MakeN (A : MT_A) (* : MT_N *) = struct
      module M = MakeM (A)
      let f _ = M.B []
     end

     module A = struct
      type t = int
     end

     module M = MakeM (A)
     module N = MakeN (A)

     let _ = (M.f (N.f 1))

This file compiles, but when I comment-in the module type restriction 
for `MakeN' the error

This expression has type N.M.t = MakeN(A).M.t but is here used with type
   M.t = MakeM(A).t

occurs. I understand that in the current setting the two types are not 
equal for the compiler. However, my question is: Is there a way to tell 
the compiler that the two types should be equal?

cheers

christian


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

* Re: [Caml-list] Problem with Functors and Module Types
  2008-08-01 11:10 Problem with Functors and Module Types Christian Sternagel
@ 2008-08-01 11:58 ` Julien Signoles
  0 siblings, 0 replies; 2+ messages in thread
From: Julien Signoles @ 2008-08-01 11:58 UTC (permalink / raw)
  To: Christian Sternagel; +Cc: caml-list

Hello,

> once again I have some problems using functors with module types. I produced 
> following (almost?) minimal example:
>
>    module type MT_M = sig
>     type s
>     type t = A of s | B of t list
>     val f : t -> t
>    end
>
>    module type MT_N = sig
>     module M : MT_M
>     val f : 'a -> M.t
>    end
>
>    module type MT_A = sig
>     type t
>    end
>
>    module MakeM (A : MT_A) : MT_M with type s = A.t = struct
>     type s = A.t
>     type t = A of s | B of t list
>     let f x = x
>    end
>
>    module MakeN (A : MT_A) (* : MT_N *) = struct
>     module M = MakeM (A)
>     let f _ = M.B []
>    end
>
>    module A = struct
>     type t = int
>    end
>
>    module M = MakeM (A)
>    module N = MakeN (A)
>
>    let _ = (M.f (N.f 1))
>
> This expression has type N.M.t = MakeN(A).M.t but is here used with type
>  M.t = MakeM(A).t

There are two different solutions (at least).

===
1) Externalize the sum type and use the "with type" construct:

type 'a m = A of 'a | B of 'a m list
module type MT_M = sig type s type t = s m val f : t -> t end
...
module MakeM(A:MT_A):MT_M with type s = A.t = struct
   type s = A.t
   type t = s m
   let f x = x
end
module MakeN(A:MT_A):MT_N with type M.s = A.t and type M.t = A.t m = struct
   ...
end
...
===
2) Use the "with module construct"
module MakeN(A:MT_A):MT_N with module M = MakeM(A) = struct
   ...
end
===

The second solution is more elegant.

Hope this helps,
Julien Signoles


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

end of thread, other threads:[~2008-08-01 11:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-01 11:10 Problem with Functors and Module Types Christian Sternagel
2008-08-01 11:58 ` [Caml-list] " Julien Signoles

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