Greg Nisbet
Greg Nisbet

Reputation: 6994

OCaml refer to module in ml file inside mli

Here I have a file set.ml with a module called IntSet. How do I refer to the module IntSet inside the corresponding interface file set.mli?

module IntSet = struct
  type t = int list;;
  let empty = [];;
  let rec is_member key = function
    | [] -> false
    | (x::xs) -> (
      if x = key then true
      else is_member key xs
    );;
end;;

let join xs ys = xs @ ys;;

Here is set.mli

val join : IntSet.t -> IntSet.t -> IntSet.t

If I try to compile it, I get an error claiming that the module IntSet is unbound.

% corebuild set.native
+ ocamlfind ocamlc -c -w A-4-33-40-41-42-43-34-44 -strict-sequence -g -bin-annot -short-paths -thread -package core -ppx 'ppx-jane -as-ppx' -o set.cmi set.mli
File "set.mli", line 1, characters 11-19:
Error: Unbound module IntSet
Command exited with code 2.
Hint: Recursive traversal of subdirectories was not enabled for this build,
  as the working directory does not look like an ocamlbuild project (no
  '_tags' or 'myocamlbuild.ml' file). If you have modules in subdirectories,
  you should add the option "-r" or create an empty '_tags' file.

  To enable recursive traversal for some subdirectories only, you can use the
  following '_tags' file:

      true: -traverse
      <dir1> or <dir2>: traverse

Compilation unsuccessful after building 3 targets (1 cached) in 00:00:00.

How do I expose a module defined in set.ml so I can use it in definitions?

Upvotes: 4

Views: 888

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66793

I changed set.mli to this, and the compiler seems happy:

module IntSet : sig type t end
val join : IntSet.t -> IntSet.t -> IntSet.t

There is probably more work to make things usable. There's no way to make a value of type IntSet.t, for example.

Upvotes: 2

Related Questions