unification of array types
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
Let's define a few modules to play with functors:
# module type MS = sig type t end;;
module type MS = sig type t end
# module M1 = struct type t = int end;;
module M1 : sig type t = int end
# module M2 = struct type t = int end;;
module M2 : sig type t = int end
The first functor build an array type:
# module type S = sig module M : MS type a = { foo : M.t } end;;
module type S = sig module M : MS type a = { foo : M.t; } end
# module MAKE_S (M : MS) : S with module M = M = struct module M = M type a = { foo : M.t } end;;
module MAKE_S :
functor (M : MS) ->
sig module M : sig type t = M.t end type a = { foo : M.t; } end
# module S1 = MAKE_S(M1);;
module S1 :
sig
module M : sig type t = M1.t end
type a = MAKE_S(M1).a = { foo : M.t; }
end
# module S2 = MAKE_S(M2);;
module S2 :
sig
module M : sig type t = M2.t end
type a = MAKE_S(M2).a = { foo : M.t; }
end
# let a1 : S1.a = { foo = 1 };;
val a1 : S1.a = {S1.foo = 1}
# let a2 : S2.a = { foo = 2 };;
val a2 : S2.a = {S2.foo = 2}
# a1 == a2;;
Error: This expression has type S2.a = MAKE_S(M2).a
but an expression was expected of type S1.a = MAKE_S(M1).a
So the compiler knows the types of the arrays, but seems to refuse to "unify" the field names.
The same test with a tuple instead of an array works as expected:
# module MAKE_S' (M : MS) : S' with module M = M = struct module M = M type a = M.t * float end;;
module MAKE_S' :
functor (M : MS) ->
sig module M : sig type t = M.t end type a = M.t * float end
# module S1' = MAKE_S'(M1);;
module S1' : sig module M : sig type t = M1.t end type a = M.t * float end
# module S2' = MAKE_S'(M2);;
module S2' : sig module M : sig type t = M2.t end type a = M.t * float end
# let a1 : S1'.a = 1,1.;;
val a1 : S1'.a = (1, 1.)
# let a2 : S2'.a = 1,1.;;
val a2 : S2'.a = (1, 1.)
# a1 == a2;;
- : bool = false
Is there anything that can be done to still use arrays in this situation?