如何合并定义相同类型的 OCaml 模块类型(签名)?
How to merge OCaml module types (signatures) defining the same type?
在 OCaml 中,我有两个模块类型定义一个类型 t
:
module type Asig = sig
type t
val a : t
end
module type Bsig = sig
type t
val b : t
end
我想自动创建一个模块类型来合并它们。我想创建一个模块类型等同于:
module type ABsig_manual = sig
type t
val a : t
val b : t
end
我试过了
module type ABsig = sig
include Asig
include Bsig
end
但这失败了Error: Multiple definition of the type name t
。似乎不可能向 include
添加类型约束,所以我被卡住了。
上下文:我有一个模块 AB
确实实现了两个签名,我想将它提供给仿函数,例如:
module MakeC(AB) = struct
type t = AB.t list
let c = [AB.a; AB.b]
end
module C = MakeC(AB)
我可以使用两个参数,例如:
module UglyMakeC(A : Asig)(B : Bsig with type t = A.t) = struct
type t = A.t list
let c = [A.a; B.b]
end
module C = UglyMakeC(AB)(AB)
但这(很丑陋并且)不能很好地扩展到更多仿函数或更多要合并的签名。
那么,如何自动合并这两种模块类型?我可以根据需要修改 A 和 B,但我想将它们分开。另外,也许我的方法是完全错误的,在那种情况下,我希望能指出更好的方向。
Type sharing in OCaml - typechecker error 相关但合并模块,而不是模块类型。
方法如下:
module type Asig = sig
type t
val a : t
end
module type Bsig = sig
type t
val b : t
end
module type ABsig = sig
include Asig
include Bsig with type t := t
end
叫做"destructive substitution"。
在 OCaml 中,我有两个模块类型定义一个类型 t
:
module type Asig = sig
type t
val a : t
end
module type Bsig = sig
type t
val b : t
end
我想自动创建一个模块类型来合并它们。我想创建一个模块类型等同于:
module type ABsig_manual = sig
type t
val a : t
val b : t
end
我试过了
module type ABsig = sig
include Asig
include Bsig
end
但这失败了Error: Multiple definition of the type name t
。似乎不可能向 include
添加类型约束,所以我被卡住了。
上下文:我有一个模块 AB
确实实现了两个签名,我想将它提供给仿函数,例如:
module MakeC(AB) = struct
type t = AB.t list
let c = [AB.a; AB.b]
end
module C = MakeC(AB)
我可以使用两个参数,例如:
module UglyMakeC(A : Asig)(B : Bsig with type t = A.t) = struct
type t = A.t list
let c = [A.a; B.b]
end
module C = UglyMakeC(AB)(AB)
但这(很丑陋并且)不能很好地扩展到更多仿函数或更多要合并的签名。
那么,如何自动合并这两种模块类型?我可以根据需要修改 A 和 B,但我想将它们分开。另外,也许我的方法是完全错误的,在那种情况下,我希望能指出更好的方向。
Type sharing in OCaml - typechecker error 相关但合并模块,而不是模块类型。
方法如下:
module type Asig = sig
type t
val a : t
end
module type Bsig = sig
type t
val b : t
end
module type ABsig = sig
include Asig
include Bsig with type t := t
end
叫做"destructive substitution"。