Ocaml破坏性替换错误

jul*_*o65 3 ocaml module

type ('a, 'r) loop
type 'e task
type ('a, 'e) tmpl'

module type COMPONENT =
sig
    type t
    type event
    type r

    val update : t -> event -> r
    val view : (t, event) tmpl'
end

module type MAIN_COMPONENT =
sig
    type t
    type event
    type r

    include COMPONENT
        with type t := t
        with type event := event
        with type r := (t * event task option, r) loop
end
Run Code Online (Sandbox Code Playgroud)

我在尝试替换类型时遇到此错误r:

Error: Only type constructors with identical parameters can be substituted.

但是,这有效:

module type MAIN_COMPONENT =
sig
    type t
    type event
    type r
    type r' = (t * event task option, r) loop

    include COMPONENT
        with type t := t
        with type event := event
        with type r := r'
end
Run Code Online (Sandbox Code Playgroud)

为什么?

我该如何摆脱这种r'类型?

ivg*_*ivg 5

这是破坏性替换的限制 - 您只能使用具有语法相同类型参数的类型构造函数替换,而不能替换为任意类型表达式.这不是一个基本限制,因为您可以轻松解决它,如您在示例中所示.

但是,有一些代码重复,你可以通过使用抽象(在类型级别)摆脱.在OCaml中,您可以使用仿函数来创建类型级抽象,即采用类型和返回类型的函数.语法有点重,但如果选择正确的名称和签名,它可以很好.

让我们写的Loop抽象,将构建类型(t1 * t2 task option, t3) loop从提供的类型t1,t2以及t3:

module type T3 = sig type t1 type t2 type t3 end

module Loop(T : T3) = struct
  type t = (T.t1 * T.t2 task option, T.t3) loop
end
Run Code Online (Sandbox Code Playgroud)

根据这个定义,我们可以使用破坏性替换,就像F(T).t没有参数的类型构造函数一样,例如,

 module type MAIN_COMPONENT = sig
    type t
    type event
    type r

    module Types : T3 with type t1 = t
                       and type t2 = event
                       and type t3 = r

    include COMPONENT
        with type t := t
        with type event := event
        with type r := Loop(Types).t
end
Run Code Online (Sandbox Code Playgroud)

缺点是您需要定义Types模块,因为您无法在模块签名中直接在仿函数应用程序中构造模块.

当然,在您的情况下,解决方案比您提出的解决方案要大得多,因此,使用您的解决方案可能更好.但是如果你的定义很大,你可以使用仿函数.