跨模块键入定义

Zub*_*ani 2 ocaml types module

module type ELEMENT =
sig
    type element_i
end

module Element:ELEMENT =
struct  
    type element_i =  N of int | CNN of cnn
end

module type SEMIRING =
functor (E:ELEMENT)->
sig
    type elements
end

module Semiring:SEMIRING =
functor(Element:ELEMENT) ->
struct
        let zero = (Element.element_i  0) (*ERROR: Unbounded Value; Same with N 0*)
end
Run Code Online (Sandbox Code Playgroud)

如何在Semiring模块中创建element_i类型的对象?

Pas*_*uoq 5

您可以允许程序员通过不隐藏此类型的构造函数来创建element_i内部Semiring类型的值,就像您当前所做的那样.

相反,将签名定义ELEMENT为:

module type ELEMENT =
sig
    type element_i = N of int | CNN of cnn
end
Run Code Online (Sandbox Code Playgroud)

这使得你的算符Semiring期望更多的Element参数:Element.element_i它现在只接受具有这些构造函数的类型,而不是任何类型.但从好的方面来说,它现在可以应用构造函数来构建这种类型的值Element.N 12