为什么无法将函子参数模块中的模块类型包含在函子的最终模块中的模块类型中?

问题描述

OCaml不提供通过functor直接创建模块类型的功能。但是,它允许对嵌套模块类型进行一些操作。因此,我可以使用参数中的模块类型来绑定结果中的模块类型:

module CONTAINER = struct
  module type S = sig
    val a : int
  end
end

module copY (CONTAINER : sig module type S end) = struct
  module type S = CONTAINER.S
end 

module type S_FROM_CONTAINER = copY(CONTAINER).S
    

S_FROM_CONTAINER不是抽象的。必须提供a才能应用S_FROM_CONTAINER

(*
module Impl : S_FROM_CONTAINER = struct
end 

> Error: Signature mismatch:
> Modules do not match: sig end is not included in S_FROM_CONTAINER  
> The value `a' is required but not provided
*)

module Impl : S_FROM_CONTAINER = struct
  let a = 42
end 

此外,我可以将S中的CONTAINER用作结果模块中模块类型中的一种模块类型:

module AS_TYPE_OF_IMPL (CONTAINER : sig module type S end) = struct
  module type S = sig
    module Impl : CONTAINER.S 
  end
end 

module type IMPL_CONTAINER_S = 
  AS_TYPE_OF_IMPL(CONTAINER).S 
Impl中的

IMPL_CONTAINER_S模块也不是抽象模块类型:

(* module ImplContainer : IMPL_CONTAINER_S = struct
     module Impl = struct end
   end

> Error: Signature mismatch:
> Modules do not match:
>   sig module Impl : sig end end
> is not included in
>   IMPL_CONTAINER_S
> In module Impl:
> Modules do not match: sig end is not included in CONTAINER.S
> In module Impl:
> The value `a' is required but not provided
*)

module ImplContainer : IMPL_CONTAINER_S = struct
  module Impl = struct let a = 42 end
end

很好。我认为这是一个强大的工具。但是我发现了一个让我难过的限制。不可能将此模块类型包含在另一个模块中。

module EXTEND_S (CONTAINER : sig module type S end) = struct
  module type S = sig
    include CONTAINER.S
    val extention : int
  end
end

module AS_PART_OF_IMPL (CONTAINER : sig module type S end) = struct
  module type S = sig
    module Impl : sig
      include CONTAINER.S
      val extention : int
    end 
  end 
end

include CONTAINER.S
^^^^^^^^^^^
错误:此模块类型不是签名

为什么我不能这样做?对我来说,这看起来有些差距,不仅在编译器中,而且在语言概念上也是如此。我想了解什么?

解决方法

简而言之,include不是模块类型系统的一部分,它们通过内联签名内容来处理签名。在模块系统中添加include表达式并不是那么简单,因为在任何上下文中都不能包含任何模块类型。例如考虑:

module F(X:sig module type x module type y end) = struct
  module type result = sig 
    include x
    include y
  end
end

并非始终有效。例如考虑,

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

然后

module type t = sig
  include x
  include y
end

产生错误

Error: Illegal shadowing of included type t/863 by t/865
      Line 2,characters 2-11:
        Type t/863 came from this include
      Line 3,characters 2-10:
        The value x has no valid type if t/863 is shadowed

因此include s并不总是有效的,要使其包含抽象类型的模块成为可能,就需要改进模块类型系统以捕获这种情况。

此外,具有动态数量的运行时组件的模块类型将需要不同的编译方案,因为查询模块内部的路径不再可以简化为查询块中的统计已知索引。