Version française
Home     About     Download     Resources     Contact us    
Browse thread
Problem with Functors and Module Types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Julien Signoles <Julien.Signoles@l...>
Subject: Re: [Caml-list] Problem with Functors and Module Types
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