如何在扩展模块中包含签名?

Nic*_*ber 2 ocaml

假设我们想要扩展现有的模块List.

我们通常只是include想要扩展的模块,然后将新方法添加到其结构中:

# module List = struct
    include List
    let addOne x = x + 1
  end;;
Run Code Online (Sandbox Code Playgroud)

这给了我们签名:

module List :
  sig
    val length : 'a list -> int
    val compare_lengths : 'a list -> 'b list -> int
    val compare_length_with : 'a list -> int -> int
    :
    val addOne : int -> int
  end
Run Code Online (Sandbox Code Playgroud)

现在,如果我想明确扩展模块的签名,我会尝试做类似的事情:

# module List : sig
    val addOne : int -> int
  end = struct
    include List
    let addOne x = x + 1
  end;;
Run Code Online (Sandbox Code Playgroud)

但现在我们看到我们的签名变为:

module List :
  sig
    val addOne : int -> int
  end
Run Code Online (Sandbox Code Playgroud)

通过像这样定义我们自己的签名,我们排除了List签名,因此我们的模块结构现在不包含任何原始List方法.

现在,如果这不是 List我个人创建的模块,那么我们可以单独定义签名并将其包含在我们扩展的模块的签名中.

# module List : sig
    include MY_LIST
    val addOne : int -> int
  end = struct
    include MyList
    let addOne x = x + 1
  end;;
Run Code Online (Sandbox Code Playgroud)

但是当我们有类似的东西List,或者我们正在消费的其他第三方模块时,有没有办法扩展它包含我们自己的签名?

是否可以这样做,如果没有,是否有一种常用于实现类似行为的解决方法/是否有规范的方法来处理这些类型的情况?

oct*_*ron 5

module type of这些情况:

module List: sig
  include module type of List
  val more: unit
end = struct
  include List
  let more = ()
end
Run Code Online (Sandbox Code Playgroud)