Zubeen Lalani
Zubeen Lalani

Reputation: 331

Type definitions across modules

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

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

Upvotes: 2

Views: 112

Answers (2)

Chris Conway
Chris Conway

Reputation: 55979

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... *)

Upvotes: 2

Pascal Cuoq
Pascal Cuoq

Reputation: 80255

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

Upvotes: 5

Related Questions