Module 在include'时处理类型名称冲突;在OCaml中调用两个模块

Module 在include'时处理类型名称冲突;在OCaml中调用两个模块,module,include,ocaml,Module,Include,Ocaml,有人能帮我做Jason Hickey书的练习12.5吗 基本上,问题是如何避免在实践中因“包含”而出现以下冲突问题?谢谢 # module type XSig = sig type t val x : t end;; # module A : XSig = struct type t = int let x = 0 end;; # module B : XSig = struct type t = int let x = 1 end;; # module C = stru

有人能帮我做Jason Hickey书的练习12.5吗

基本上,问题是如何避免在实践中因“包含”而出现以下冲突问题?谢谢

# module type XSig = sig 
type t 
val x : t 
end;; 
# module A : XSig = struct 
type t = int 
let x = 0 
end;; 
# module B : XSig = struct 
type t = int 
let x = 1 
end;; 
# module C = struct 
include A 
include B 
end;; 

我不知道问题到底说了什么,但是关于您的代码片段,有各种非常不同的方式来解释您试图做什么

您当前正在做的是将
A
B
密封在两个抽象的、不兼容的签名下,然后尝试将它们混合在一个模块中,结果导致名称冲突

也许你只是想避免名字冲突。当然,最简单的解决方案是不要对两种类型使用相同的名称
A.t
B.t
。然后您可以“不包括
B
”:

更好的解决方案是使用OCaml 3.12破坏性替换签名,
类型为t:=foo
,以屏蔽模块
B
中的类型
t

module C = struct
  include A
  let x_b = B.x
end
module C = struct
  include A
  type u = B.t (* rename B.t into 'u' to avoid name conflict *)
  include (B : XSig with type t := u)  (* replaces 't' by 'u' *)
end
您可能还希望模块
A
B
的类型兼容。在这种情况下,不能用抽象类型密封它们

module type XSig = sig 
  type t 
  val x : t 
end

module A = struct 
  type t = int 
  let x = 0 
end

(* if you want to check that A can be sealed by XSig, do it here,
   outside the main declaration *)
let _ = (module A : XSig)

module B = struct 
  type t = int 
  let x = 1 
end

module C = struct 
  include (A : XSig with type t := int)
  include (B : XSig with type t := int)
end
(* module C : sig
     val x = int
   end *)
在本例中,破坏性子替换
:=
删除了
A.t
B.t
两种类型。如果您希望模块
C
具有类型
t
,您可以编写以下任一项:

module C = struct 
  type t = int
  include (A : XSig with type t := t)
  include (B : XSig with type t := t)
end
或者,使用非破坏性替换(更改类型定义而不是删除它):

有关更多详细信息和比较,请参阅

module C = struct 
  include (A : XSig with type t = int)
  include (B : XSig with type t := t)
end