I am a bit confused by the word ‘module’. My initial understanding of the word ‘module’ is that we want to define a module, i.e.
module foo = struct ... end
However, in OCaml - The module system we have the following example:
# module Set =
functor (Elt: ORDERED_TYPE) ->
struct
type element = Elt.t
type set = element list
let empty = []
let rec add x s =
match s with
[] -> [x]
| hd::tl ->
match Elt.compare x hd with
Equal -> s (* x is already in s *)
| Less -> x :: s (* x is smaller than all elements of s *)
| Greater -> hd :: add x tl
let rec member x s =
match s with
[] -> false
| hd::tl ->
match Elt.compare x hd with
Equal -> true (* x belongs to s *)
| Less -> false (* x is smaller than all elements of s *)
| Greater -> member x tl
end;;
module Set :
functor (Elt : ORDERED_TYPE) ->
sig
type element = Elt.t
type set = element list
val empty : 'a list
val add : Elt.t -> Elt.t list -> Elt.t list
val member : Elt.t -> Elt.t list -> bool
end
In particular, let us look at the type of module Set
, it is a functor which takes one “module” as input, and produces a 2nd “module” as output. This is very different from say, what a Java package or a Rust module is.
I am trying to think of the correct way to understand the word “module” ; is it as follows?
We have two worlds: values & modules.
In the values world, we have: primitives + records + enums, types, functions.
In the modules world, we have: structs, signatures, functors.
In such an model, the word ‘module’ merely means “we are now going to define something in the ‘modules world’” ?
Is this the right way to think of the keyword ‘modules’ ? If not, what is the correct interpretation ?