开发者

Type definitions across modules

开发者 https://www.devze.com 2023-01-22 20:36 出处:网络
开发者_如何学JAVAmodule type ELEMENT = sig type element_i end module Element:ELEMENT = struct type element_i =N of int | CNN of cnn
开发者_如何学JAVAmodule 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

How can I create objects of the type element_i inside Semiring module here?


You can allow the programmer to create values of type element_i inside Semiring by not hiding the constructors of this type, as you are currently doing.

Instead, define the signature ELEMENT as:

module type ELEMENT =
sig
    type element_i = N of int | CNN of cnn
end

This makes your functor Semiring expect more of its Element argument: instead of any type Element.element_i, it now only accepts a type with exactly these constructors. But on the plus side it can now apply the constructors to build values of this type, for instance Element.N 12


There's actually two problems with your example. The first is pointed out by Pascal (i.e., the constructors of element_i are hidden by the signature). The second is that the module Element in the functor is not the same as module Element you declared above. The Element argument to the functor is a "hiding" the definition of Element the same way a function parameter would "hide" a variable:

let x = 0

let f = fun x -> (* x here is a different x... *)

module type T = sig (* ... *) end

module M : T = struct (* ... *) end

module F = functor (M : T) -> (* M here is a different M... *)
0

精彩评论

暂无评论...
验证码 换一张
取 消