Module constructor scope in OCaml

I defined the following interface and module:

module type TYPE = sig type t end module Type = (struct type t = | TBot | T of int | TTop end: TYPE) 

Now I understand that if I write outside Type.T 5 , the compiler will give me an error Error: Unbound constructor Type.T If I delete the signature and save the module, the error will disappear.

1) So, my first question is: how do I change the signature so that I can use the constructors outside?

2) One way is to define the constructor explicitly as follows, do you think this is the usual way? One drawback that I see now is that it does not allow building a TBot or TTop .

 module type TYPE = sig type t val make : int -> t end module Type = (struct ... let make (i: int) : t = T i end: TYPE) 

3) Is it always necessary that the outside can build the value inside the module?

+6
source share
1 answer

1) You must export the type declaration, otherwise t is considered abstract, and then you need to define and export the constructors (see 2)):

 module type TYPE = sig type t = | TBot | T of int | TTop end module Type : TYPE = struct type t = | TBot | T of int | TTop end 

2) Yes, this is a great way. To define the top and bottom sides, you just need to define (and export) the new constructors:

 module type TYPE = sig ... val top: t val bot: t end module Type = struct ... let bot = TBot let top = TTop end 

3) I do not understand your question

+6
source

Source: https://habr.com/ru/post/906378/


All Articles